/* ─── Reset & base ────────────────────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --sidebar-w: 220px;
  --sidebar-bg: #1e3a5f;
  --sidebar-hover: #2d5286;
  --sidebar-active: #3b6cbf;
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --primary-light: #dbeafe;
  --accent: #0ea5e9;
  --success: #10b981;
  --warning: #f59e0b;
  --danger: #ef4444;
  --purple: #8b5cf6;
  --bg: #f1f5f9;
  --card: #ffffff;
  --border: #e2e8f0;
  --text: #1e293b;
  --text-muted: #64748b;
  --text-light: #94a3b8;
  --radius: 8px;
  --shadow: 0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.04);
  --shadow-md: 0 4px 6px rgba(0,0,0,.07), 0 2px 4px rgba(0,0,0,.05);
}

html, body {
  height: 100%;
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, sans-serif;
  font-size: 14px;
  color: var(--text);
  background: var(--bg);
  line-height: 1.5;
}

a { color: var(--primary); text-decoration: none; }
a:hover { text-decoration: underline; }

button { cursor: pointer; font-family: inherit; font-size: 14px; }
input, select, textarea { font-family: inherit; font-size: 14px; }

/* ─── Login ───────────────────────────────────────────────────────────── */
#login-screen {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  background: linear-gradient(135deg, #1e3a5f 0%, #2563eb 100%);
}

.login-card {
  background: #fff;
  border-radius: 12px;
  padding: 40px 48px;
  width: 100%;
  max-width: 400px;
  box-shadow: 0 20px 60px rgba(0,0,0,.2);
}

.login-logo {
  text-align: center;
  margin-bottom: 28px;
}

.login-logo h1 {
  font-size: 28px;
  font-weight: 700;
  color: var(--sidebar-bg);
  letter-spacing: -0.5px;
}

.login-logo h1 span { color: var(--primary); }
.login-logo p { color: var(--text-muted); font-size: 13px; margin-top: 4px; }

.login-error {
  background: #fef2f2;
  border: 1px solid #fecaca;
  color: var(--danger);
  padding: 10px 14px;
  border-radius: var(--radius);
  font-size: 13px;
  margin-bottom: 16px;
  display: none;
}

/* ─── App shell ───────────────────────────────────────────────────────── */
#app {
  display: flex;
  min-height: 100vh;
}

/* ─── Sidebar ─────────────────────────────────────────────────────────── */
#sidebar {
  width: var(--sidebar-w);
  background: var(--sidebar-bg);
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0; left: 0; bottom: 0;
  z-index: 100;
  transition: transform .25s;
}

.sidebar-logo {
  padding: 20px 20px 16px;
  border-bottom: 1px solid rgba(255,255,255,.08);
}

.sidebar-logo h2 {
  font-size: 18px;
  font-weight: 700;
  color: #fff;
  letter-spacing: -0.3px;
}

.sidebar-logo h2 span { color: #60a5fa; }
.sidebar-logo p { color: rgba(255,255,255,.4); font-size: 11px; margin-top: 2px; }

.sidebar-nav {
  flex: 1;
  padding: 8px 0;
  overflow-y: auto;
}

.sidebar-nav a {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 9px 20px;
  color: rgba(255,255,255,.7);
  text-decoration: none;
  font-size: 13.5px;
  transition: background .15s, color .15s;
  border-radius: 0;
}

.sidebar-nav a:hover { background: var(--sidebar-hover); color: #fff; text-decoration: none; }
.sidebar-nav a.active { background: var(--sidebar-active); color: #fff; font-weight: 500; }

.sidebar-nav .nav-icon { font-size: 16px; width: 20px; text-align: center; flex-shrink: 0; }

.sidebar-nav .nav-section {
  padding: 14px 20px 4px;
  font-size: 10px;
  font-weight: 600;
  letter-spacing: .08em;
  text-transform: uppercase;
  color: rgba(255,255,255,.3);
}

.sidebar-user {
  padding: 14px 20px;
  border-top: 1px solid rgba(255,255,255,.08);
}

.sidebar-user-info { color: rgba(255,255,255,.7); font-size: 12px; margin-bottom: 8px; }
.sidebar-user-info strong { display: block; color: #fff; font-size: 13px; }

.btn-logout {
  width: 100%;
  padding: 7px 12px;
  background: rgba(255,255,255,.08);
  border: 1px solid rgba(255,255,255,.12);
  border-radius: var(--radius);
  color: rgba(255,255,255,.7);
  font-size: 12px;
  transition: background .15s;
}

.btn-logout:hover { background: rgba(255,255,255,.15); color: #fff; }

/* ─── Main content ────────────────────────────────────────────────────── */
#main {
  margin-left: var(--sidebar-w);
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.topbar {
  padding: 16px 28px;
  background: var(--card);
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  position: sticky;
  top: 0;
  z-index: 50;
}

.topbar-title { font-size: 16px; font-weight: 600; color: var(--text); }
.topbar-actions { display: flex; gap: 8px; align-items: center; }

#content {
  padding: 24px 28px;
  flex: 1;
}

/* ─── Buttons ─────────────────────────────────────────────────────────── */
.btn {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 8px 16px;
  border-radius: var(--radius);
  font-weight: 500;
  border: 1px solid transparent;
  transition: all .15s;
  white-space: nowrap;
}

.btn-primary { background: var(--primary); color: #fff; border-color: var(--primary); }
.btn-primary:hover { background: var(--primary-dark); border-color: var(--primary-dark); }

.btn-secondary { background: #fff; color: var(--text); border-color: var(--border); }
.btn-secondary:hover { background: var(--bg); }

.btn-danger { background: var(--danger); color: #fff; border-color: var(--danger); }
.btn-danger:hover { background: #dc2626; }

.btn-success { background: var(--success); color: #fff; border-color: var(--success); }
.btn-success:hover { background: #059669; }

.btn-sm { padding: 5px 10px; font-size: 12px; }
.btn-xs { padding: 3px 8px; font-size: 11px; }

.btn:disabled { opacity: .5; cursor: not-allowed; }

/* ─── Forms ───────────────────────────────────────────────────────────── */
.form-group { margin-bottom: 16px; }

.form-label {
  display: block;
  font-weight: 500;
  font-size: 13px;
  color: var(--text);
  margin-bottom: 5px;
}

.form-label .required { color: var(--danger); }

.form-control {
  width: 100%;
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: #fff;
  color: var(--text);
  transition: border-color .15s, box-shadow .15s;
}

.form-control:focus {
  outline: none;
  border-color: var(--primary);
  box-shadow: 0 0 0 3px rgba(37,99,235,.1);
}

.form-control[readonly] { background: var(--bg); color: var(--text-muted); }
.form-hint { font-size: 11px; color: var(--text-muted); margin-top: 4px; }

/* ─── Cards ───────────────────────────────────────────────────────────── */
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
}

.card-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.card-title { font-weight: 600; font-size: 15px; }
.card-body { padding: 20px; }

/* ─── Stats ───────────────────────────────────────────────────────────── */
.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 16px;
  margin-bottom: 24px;
}

.stat-card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 18px 20px;
  box-shadow: var(--shadow);
}

.stat-card .stat-label { font-size: 12px; color: var(--text-muted); font-weight: 500; text-transform: uppercase; letter-spacing: .04em; }
.stat-card .stat-value { font-size: 28px; font-weight: 700; color: var(--text); margin-top: 4px; }
.stat-card .stat-sub { font-size: 11px; color: var(--text-muted); margin-top: 2px; }
.stat-card.stat-primary .stat-value { color: var(--primary); }
.stat-card.stat-warning .stat-value { color: var(--warning); }
.stat-card.stat-success .stat-value { color: var(--success); }
.stat-card.stat-danger  .stat-value { color: var(--danger); }

/* ─── Tables ──────────────────────────────────────────────────────────── */
.table-wrapper { overflow-x: auto; }

table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
}

table th {
  text-align: left;
  padding: 10px 14px;
  background: var(--bg);
  color: var(--text-muted);
  font-weight: 600;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: .04em;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

table td {
  padding: 11px 14px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}

table tr:last-child td { border-bottom: none; }
table tbody tr:hover { background: #f8fafc; }
table.table-fixed { table-layout: fixed; }

/* ─── Status badges ───────────────────────────────────────────────────── */
.badge {
  display: inline-flex;
  align-items: center;
  gap: 4px;
  padding: 3px 8px;
  border-radius: 20px;
  font-size: 11px;
  font-weight: 600;
  white-space: nowrap;
}

.badge-pending   { background: #fef3c7; color: #92400e; }
.badge-planned   { background: #dbeafe; color: #1e40af; }
.badge-confirmed { background: #d1fae5; color: #065f46; }
.badge-done      { background: #f1f5f9; color: #475569; }
.badge-refused   { background: #fee2e2; color: #991b1b; }
.badge-postponed { background: #ede9fe; color: #5b21b6; }
.badge-overdue   { background: #fee2e2; color: #991b1b; }
.badge-open      { background: #dbeafe; color: #1e40af; }
.badge-suggestion{ background: #fef9c3; color: #854d0e; }
.badge-migrated  { background: #f0fdf4; color: #166534; border: 1px solid #bbf7d0; }

.badge-normal   { background: #f1f5f9; color: #475569; }
.badge-urgent   { background: #fff7ed; color: #c2410c; }
.badge-critical { background: #fee2e2; color: #991b1b; }

/* ─── Filters bar ─────────────────────────────────────────────────────── */
.filters-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 16px;
  align-items: flex-end;
}

.filters-bar .form-control { width: auto; min-width: 130px; }
.filters-bar .form-group { margin-bottom: 0; }

/* ─── Pagination ──────────────────────────────────────────────────────── */
.pagination {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 6px;
  padding: 12px 20px;
  border-top: 1px solid var(--border);
  font-size: 13px;
  color: var(--text-muted);
}

.pagination button {
  padding: 5px 10px;
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: #fff;
  color: var(--text);
}

.pagination button:disabled { opacity: .4; cursor: default; }
.pagination button.active { background: var(--primary); color: #fff; border-color: var(--primary); }

/* ─── Modal ───────────────────────────────────────────────────────────── */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,.4);
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}

.modal {
  background: #fff;
  border-radius: 10px;
  box-shadow: 0 20px 60px rgba(0,0,0,.2);
  width: 100%;
  max-width: 580px;
  max-height: 90vh;
  display: flex;
  flex-direction: column;
}

.modal-lg { max-width: 800px; }
.modal-xl { max-width: 1000px; }

.modal-header {
  padding: 18px 24px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.modal-title { font-size: 16px; font-weight: 600; }

.modal-close {
  background: none;
  border: none;
  font-size: 20px;
  color: var(--text-muted);
  line-height: 1;
  padding: 0 4px;
}

.modal-close:hover { color: var(--text); }

.modal-body {
  padding: 20px 24px;
  overflow-y: auto;
  flex: 1;
}

.modal-footer {
  padding: 14px 24px;
  border-top: 1px solid var(--border);
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

/* ─── Toast ───────────────────────────────────────────────────────────── */
#toast-container {
  position: fixed;
  bottom: 24px;
  right: 24px;
  z-index: 999;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.toast {
  padding: 12px 18px;
  border-radius: var(--radius);
  font-size: 13px;
  font-weight: 500;
  box-shadow: var(--shadow-md);
  animation: toast-in .2s ease;
  max-width: 360px;
}

.toast-success { background: #ecfdf5; border: 1px solid #6ee7b7; color: #065f46; }
.toast-error   { background: #fef2f2; border: 1px solid #fca5a5; color: #991b1b; }
.toast-info    { background: #eff6ff; border: 1px solid #93c5fd; color: #1e40af; }

@keyframes toast-in { from { opacity: 0; transform: translateX(20px); } to { opacity: 1; } }

/* ─── Grid helpers ────────────────────────────────────────────────────── */
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.grid-3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 16px; }
.flex { display: flex; }
.flex-col { display: flex; flex-direction: column; }
.gap-8 { gap: 8px; }
.gap-12 { gap: 12px; }
.gap-16 { gap: 16px; }
.gap-24 { gap: 24px; }
.items-center { align-items: center; }
.justify-between { justify-content: space-between; }
.mt-8 { margin-top: 8px; }
.mt-16 { margin-top: 16px; }
.mt-24 { margin-top: 24px; }
.mb-8 { margin-bottom: 8px; }
.mb-16 { margin-bottom: 16px; }
.mb-24 { margin-bottom: 24px; }

/* ─── Empty state ─────────────────────────────────────────────────────── */
.empty-state {
  text-align: center;
  padding: 48px 20px;
  color: var(--text-muted);
}

.empty-state .empty-icon { font-size: 40px; margin-bottom: 12px; }
.empty-state h3 { font-size: 16px; color: var(--text); margin-bottom: 6px; }
.empty-state p { font-size: 13px; }

/* ─── Calendar ────────────────────────────────────────────────────────── */
.calendar-grid {
  display: grid;
  grid-template-columns: repeat(7, 1fr);
  gap: 1px;
  background: var(--border);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}

.calendar-day-header {
  background: var(--bg);
  padding: 8px;
  text-align: center;
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
}

.calendar-day {
  background: var(--card);
  min-height: 100px;
  padding: 6px;
  vertical-align: top;
}

.calendar-day.other-month { background: #f8fafc; }
.calendar-day.today { background: #eff6ff; }

.calendar-date {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-muted);
  margin-bottom: 4px;
}

.calendar-day.today .calendar-date {
  background: var(--primary);
  color: #fff;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
}

.cal-event {
  font-size: 11px;
  padding: 2px 5px;
  border-radius: 3px;
  margin-bottom: 2px;
  cursor: pointer;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  border-left: 3px solid;
}

.cal-event.confirmed { background: #d1fae5; border-color: var(--success); color: #065f46; }
.cal-event.planned   { background: #dbeafe; border-color: var(--primary); color: #1e40af; border-style: dashed; }
.cal-event.pending   { background: #fef3c7; border-color: var(--warning); color: #92400e; }

/* ─── Map ─────────────────────────────────────────────────────────────── */
#map-container { height: calc(100vh - 120px); border-radius: var(--radius); overflow: hidden; }

/* ─── Audit timeline ──────────────────────────────────────────────────── */
.timeline { position: relative; padding-left: 24px; }
.timeline::before {
  content: '';
  position: absolute;
  left: 7px;
  top: 0; bottom: 0;
  width: 2px;
  background: var(--border);
}

.timeline-item { position: relative; padding-bottom: 16px; }

.timeline-dot {
  position: absolute;
  left: -24px;
  top: 3px;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--primary);
  border: 2px solid #fff;
  box-shadow: 0 0 0 2px var(--border);
}

.timeline-dot.dot-status  { background: var(--primary); }
.timeline-dot.dot-photo   { background: var(--success); }
.timeline-dot.dot-followup{ background: var(--warning); }
.timeline-dot.dot-comment { background: var(--accent); }
.timeline-dot.dot-created { background: var(--purple); }

/* History timeline colors */
.timeline-dot.timeline-dot-green  { background: var(--success); }
.timeline-dot.timeline-dot-blue   { background: var(--primary); }
.timeline-dot.timeline-dot-amber  { background: var(--warning); }
.timeline-dot.timeline-dot-red    { background: var(--danger); }

.timeline-time { font-size: 11px; color: var(--text-muted); }
.timeline-body { font-size: 13px; color: var(--text); margin-top: 1px; }
.timeline-body strong { font-weight: 600; }

/* ─── Photo gallery ───────────────────────────────────────────────────── */
.photo-gallery {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(160px, 1fr));
  gap: 12px;
}

.photo-thumb {
  position: relative;
  border-radius: var(--radius);
  overflow: visible;
  background: var(--bg);
  border: 1px solid var(--border);
  display: flex;
  flex-direction: column;
}

.photo-thumb img {
  width: 100%;
  height: 120px;
  object-fit: cover;
  display: block;
  border-radius: var(--radius) var(--radius) 0 0;
}

.photo-thumb .photo-actions {
  position: absolute;
  top: 0; left: 0; right: 0;
  height: 120px;
  background: rgba(0,0,0,.5);
  display: none;
  align-items: center;
  justify-content: center;
  gap: 8px;
  border-radius: var(--radius) var(--radius) 0 0;
}

.photo-thumb:hover .photo-actions { display: flex; }
.photo-thumb a { display: block; height: 120px; overflow: hidden; }
.photo-caption-input {
  width: 100%;
  border: none;
  border-top: 1px solid var(--border);
  background: var(--bg);
  font-size: 11px;
  padding: 4px 6px;
  color: var(--text-muted);
  outline: none;
  box-sizing: border-box;
}
.photo-caption-input:focus { background: #fff; color: var(--text); }

/* ─── Product table (visit detail) ───────────────────────────────────── */
.product-family-header {
  background: var(--bg);
  font-weight: 600;
  font-size: 12px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: .04em;
  padding: 6px 14px;
  border-bottom: 1px solid var(--border);
}

.product-status-select {
  padding: 4px 8px;
  border: 1px solid var(--border);
  border-radius: 5px;
  background: #fff;
  font-size: 12px;
}

/* ─── Comment thread ──────────────────────────────────────────────────── */
.comment-item {
  display: flex;
  gap: 10px;
  margin-bottom: 14px;
}

.comment-avatar {
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: var(--primary);
  color: #fff;
  font-size: 13px;
  font-weight: 600;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
}

.comment-bubble {
  background: var(--bg);
  border-radius: 0 var(--radius) var(--radius) var(--radius);
  padding: 10px 14px;
  flex: 1;
}

.comment-meta { font-size: 11px; color: var(--text-muted); margin-bottom: 4px; }
.comment-text { font-size: 13px; white-space: pre-wrap; }

/* ─── Follow-up list ──────────────────────────────────────────────────── */
.followup-item {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 10px 0;
  border-bottom: 1px solid var(--border);
}

.followup-item:last-child { border-bottom: none; }
.followup-check { margin-top: 2px; }

/* ─── Tabs ────────────────────────────────────────────────────────────── */
.tabs { display: flex; gap: 0; border-bottom: 2px solid var(--border); margin-bottom: 20px; }

.tab-btn {
  padding: 10px 18px;
  background: none;
  border: none;
  border-bottom: 2px solid transparent;
  margin-bottom: -2px;
  color: var(--text-muted);
  font-size: 13px;
  font-weight: 500;
  transition: color .15s, border-color .15s;
}

.tab-btn:hover { color: var(--text); }
.tab-btn.active { color: var(--primary); border-bottom-color: var(--primary); }
.tab-panel { display: none; }
.tab-panel.active { display: block; }

/* ─── Inline spreadsheet (products) ──────────────────────────────────── */
.spreadsheet-cell {
  padding: 0 !important;
}

.spreadsheet-cell input,
.spreadsheet-cell select {
  width: 100%;
  padding: 8px 12px;
  border: none;
  background: transparent;
  font-size: 13px;
}

.spreadsheet-cell input:focus,
.spreadsheet-cell select:focus {
  outline: none;
  background: #eff6ff;
  box-shadow: inset 0 0 0 2px var(--primary);
}

/* ─── Scrollbar ───────────────────────────────────────────────────────── */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: var(--border); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--text-light); }

/* ─── Hamburger (mobile only) ─────────────────────────────────────────── */
.hamburger {
  display: none;
  background: none;
  border: none;
  font-size: 20px;
  cursor: pointer;
  color: var(--text);
  padding: 4px 8px;
  line-height: 1;
  flex-shrink: 0;
}

/* ─── Responsive ──────────────────────────────────────────────────────── */
@media (max-width: 768px) {
  #sidebar {
    transform: translateX(-100%);
    transition: transform .25s ease;
    position: fixed;
    z-index: 200;
    top: 0; bottom: 0; left: 0;
    box-shadow: 2px 0 12px rgba(0,0,0,.15);
  }
  #sidebar.open { transform: translateX(0); }
  #main { margin-left: 0 !important; }
  .grid-2, .grid-3 { grid-template-columns: 1fr; }
  #content { padding: 12px; }
  .hamburger { display: flex; align-items: center; }
  .topbar { gap: 8px; }
  .stats-grid { grid-template-columns: 1fr 1fr; }
  .filters-bar { flex-wrap: wrap; gap: 8px; }
  .filters-bar .form-group { min-width: 140px; }
  .table-wrapper { overflow-x: auto; }
  .modal { max-width: 95vw; max-height: 90vh; overflow-y: auto; }
  .tabs { flex-wrap: wrap; gap: 4px; }
  .tab-btn { font-size: 12px; padding: 6px 10px; }
}

/* ─── Loading spinner ─────────────────────────────────────────────────── */
.spinner {
  width: 24px;
  height: 24px;
  border: 3px solid var(--border);
  border-top-color: var(--primary);
  border-radius: 50%;
  animation: spin .6s linear infinite;
  display: inline-block;
}

@keyframes spin { to { transform: rotate(360deg); } }

.loading-center {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 48px;
}

/* ─── Text utilities ──────────────────────────────────────────────────── */
.text-muted { color: var(--text-muted); }
.text-sm { font-size: 12px; }
.text-xs { font-size: 11px; }
.font-bold { font-weight: 600; }
.truncate { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
