/* =====================================================================
   LeadFlow X AI — Premium UI System
   HSL theming · Dark/Light · Glassmorphism · Responsive
   ===================================================================== */

* { box-sizing: border-box; }
*::before, *::after { box-sizing: border-box; }

:root {
  --ah: 239; --as: 84%; --al: 67%;

  /* Accent ramp derived from --ah/--as/--al */
  --accent:        hsl(var(--ah) var(--as) var(--al));
  --accent-hover:  hsl(var(--ah) var(--as) calc(var(--al) - 6%));
  --accent-soft:   hsl(var(--ah) var(--as) 96%);
  --accent-text:   #ffffff;
  --accent-tint:   hsl(var(--ah) var(--as) 50% / 0.10);

  /* Light theme */
  --bg-base:    hsl(var(--ah) 30% 98.5%);
  --bg-surface: #ffffff;
  --bg-elevated:#ffffff;
  --bg-muted:   hsl(var(--ah) 20% 96%);
  --bg-hover:   hsl(var(--ah) 20% 95%);

  --text-primary:   hsl(var(--ah) 25% 12%);
  --text-secondary: hsl(var(--ah) 12% 38%);
  --text-muted:     hsl(var(--ah) 8%  56%);
  --text-on-accent: #fff;

  --border:        hsl(var(--ah) 20% 90%);
  --border-strong: hsl(var(--ah) 20% 82%);

  --shadow-xs: 0 1px 2px hsl(var(--ah) 30% 20% / 0.05);
  --shadow-sm: 0 2px 6px hsl(var(--ah) 30% 20% / 0.06), 0 1px 2px hsl(var(--ah) 30% 20% / 0.04);
  --shadow-md: 0 6px 20px hsl(var(--ah) 30% 20% / 0.08), 0 2px 6px hsl(var(--ah) 30% 20% / 0.05);
  --shadow-lg: 0 20px 50px hsl(var(--ah) 30% 20% / 0.12), 0 4px 12px hsl(var(--ah) 30% 20% / 0.06);

  --success: hsl(150 65% 42%);
  --warning: hsl(38 92% 50%);
  --danger:  hsl(0 75% 55%);
  --info:    hsl(210 90% 56%);

  --r-sm: 6px;  --r-md: 10px; --r-lg: 14px; --r-xl: 20px; --r-2xl: 28px; --r-pill: 999px;

  --space-1: 4px; --space-2: 8px; --space-3: 12px; --space-4: 16px;
  --space-5: 20px; --space-6: 24px; --space-8: 32px; --space-10: 40px;

  --font-sans: -apple-system, BlinkMacSystemFont, "Inter", "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;

  --sidebar-w: 248px;
  --topbar-h: 60px;

  --transition: 160ms cubic-bezier(0.4, 0, 0.2, 1);
}

[data-theme="dark"] {
  --bg-base:    hsl(var(--ah) 18% 8%);
  --bg-surface: hsl(var(--ah) 16% 11%);
  --bg-elevated:hsl(var(--ah) 14% 14%);
  --bg-muted:   hsl(var(--ah) 14% 16%);
  --bg-hover:   hsl(var(--ah) 12% 19%);

  --text-primary:   hsl(var(--ah) 15% 96%);
  --text-secondary: hsl(var(--ah) 10% 70%);
  --text-muted:     hsl(var(--ah) 8%  52%);

  --border:        hsl(var(--ah) 12% 22%);
  --border-strong: hsl(var(--ah) 12% 30%);

  --accent-soft:   hsl(var(--ah) var(--as) 22% / 0.5);
  --accent-tint:   hsl(var(--ah) var(--as) 50% / 0.16);

  --shadow-xs: 0 1px 2px hsl(0 0% 0% / 0.4);
  --shadow-sm: 0 2px 6px hsl(0 0% 0% / 0.45);
  --shadow-md: 0 6px 20px hsl(0 0% 0% / 0.5);
  --shadow-lg: 0 20px 50px hsl(0 0% 0% / 0.6);
}

html, body { height: 100%; }
body {
  margin: 0;
  font-family: var(--font-sans);
  font-size: 14px;
  line-height: 1.55;
  color: var(--text-primary);
  background: var(--bg-base);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

a { color: var(--accent); text-decoration: none; }
a:hover { color: var(--accent-hover); }
button { font-family: inherit; }
img { max-width: 100%; display: block; }

/* ===================================================================
   Sidebar
   =================================================================== */
.sidebar {
  position: fixed; inset: 0 auto 0 0;
  width: var(--sidebar-w);
  background: var(--bg-surface);
  border-right: 1px solid var(--border);
  display: flex; flex-direction: column;
  z-index: 50;
  transition: transform var(--transition);
}
.sidebar-brand {
  display: flex; align-items: center; gap: 10px;
  padding: 18px 20px; border-bottom: 1px solid var(--border);
}
.brand-mark {
  width: 36px; height: 36px; border-radius: 10px;
  background: linear-gradient(135deg, var(--accent), hsl(var(--ah) var(--as) calc(var(--al) - 12%)));
  color: white;
  display: grid; place-items: center;
  box-shadow: 0 4px 12px hsl(var(--ah) var(--as) 50% / 0.35);
}
.brand-name { font-weight: 700; font-size: 14.5px; color: var(--text-primary); letter-spacing: -0.01em; }
.brand-sub { font-size: 11px; color: var(--text-muted); letter-spacing: 0.02em; }

.sidebar-nav { flex: 1; padding: 12px 10px; overflow-y: auto; }
.nav-section {
  font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: 0.06em;
  color: var(--text-muted);
  padding: 14px 10px 6px;
}
.nav-item {
  display: flex; align-items: center; gap: 10px;
  padding: 8px 10px; border-radius: var(--r-md);
  color: var(--text-secondary);
  font-weight: 500; font-size: 13.5px;
  transition: var(--transition);
  margin-bottom: 1px;
  position: relative;
}
.nav-item:hover { background: var(--bg-hover); color: var(--text-primary); }
.nav-item.active {
  background: var(--accent-tint);
  color: var(--accent);
}
.nav-icon { display: grid; place-items: center; width: 20px; height: 20px; }
.nav-label { flex: 1; }
.nav-badge {
  background: var(--danger); color: white;
  font-size: 10.5px; font-weight: 700;
  padding: 1px 7px; border-radius: var(--r-pill);
  min-width: 18px; text-align: center;
}
.nav-item.active .nav-badge { background: var(--accent); }

.sidebar-foot {
  border-top: 1px solid var(--border);
  padding: 10px;
  display: flex; align-items: center; gap: 8px;
}
.user-card {
  flex: 1; display: flex; align-items: center; gap: 10px;
  padding: 6px; border-radius: var(--r-md);
  color: var(--text-primary);
  transition: var(--transition);
}
.user-card:hover { background: var(--bg-hover); color: var(--text-primary); }
.avatar {
  width: 34px; height: 34px; border-radius: 50%;
  background: linear-gradient(135deg, var(--accent), hsl(var(--ah) var(--as) calc(var(--al) - 12%)));
  color: white;
  display: grid; place-items: center;
  font-weight: 700; font-size: 12.5px; letter-spacing: 0.02em;
  flex-shrink: 0;
}
.user-info { min-width: 0; }
.user-name { font-weight: 600; font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.user-role { font-size: 11px; color: var(--text-muted); }
.logout-btn {
  width: 34px; height: 34px; border-radius: var(--r-md);
  display: grid; place-items: center;
  color: var(--text-muted);
  transition: var(--transition);
}
.logout-btn:hover { background: var(--bg-hover); color: var(--danger); }

/* ===================================================================
   Main + Topbar
   =================================================================== */
.main {
  margin-left: var(--sidebar-w);
  min-height: 100vh;
  display: flex; flex-direction: column;
}
.topbar {
  height: var(--topbar-h);
  display: flex; align-items: center; gap: 12px;
  padding: 0 24px;
  background: rgba(255, 255, 255, 0.72);
  backdrop-filter: saturate(180%) blur(14px);
  -webkit-backdrop-filter: saturate(180%) blur(14px);
  border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 30;
}
[data-theme="dark"] .topbar { background: hsl(var(--ah) 16% 11% / 0.7); }
.page-title { font-size: 18px; font-weight: 600; letter-spacing: -0.01em; margin: 0; flex: 1; }
.topbar-actions { display: flex; align-items: center; gap: 6px; }
.sidebar-toggle { display: none; }

.icon-btn {
  width: 36px; height: 36px; border-radius: var(--r-md);
  background: transparent; border: 1px solid transparent;
  color: var(--text-secondary);
  display: grid; place-items: center;
  cursor: pointer;
  transition: var(--transition);
}
.icon-btn:hover { background: var(--bg-muted); color: var(--text-primary); border-color: var(--border); }
.theme-toggle .ic-moon { display: none; }
[data-theme="dark"] .theme-toggle .ic-sun { display: none; }
[data-theme="dark"] .theme-toggle .ic-moon { display: block; }

.page { padding: 24px; flex: 1; }
.page-flash { padding: 0 24px; }

.flash {
  margin: 16px 0 0;
  padding: 10px 14px; border-radius: var(--r-md);
  font-size: 13.5px; font-weight: 500;
  border: 1px solid var(--border);
  background: var(--bg-surface);
}
.flash-success { background: hsl(150 60% 96%); color: hsl(150 60% 24%); border-color: hsl(150 60% 80%); }
.flash-error   { background: hsl(0 80% 97%); color: hsl(0 65% 35%); border-color: hsl(0 70% 85%); }
.flash-info    { background: hsl(210 80% 97%); color: hsl(210 70% 30%); border-color: hsl(210 80% 85%); }
[data-theme="dark"] .flash-success { background: hsl(150 40% 16%); color: hsl(150 60% 75%); border-color: hsl(150 30% 26%); }
[data-theme="dark"] .flash-error   { background: hsl(0 40% 18%); color: hsl(0 70% 85%); border-color: hsl(0 30% 30%); }
[data-theme="dark"] .flash-info    { background: hsl(210 40% 18%); color: hsl(210 70% 85%); border-color: hsl(210 30% 30%); }

/* ===================================================================
   Cards
   =================================================================== */
.card {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-xs);
  overflow: hidden;
}
.card-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; gap: 12px;
}
.card-title { margin: 0; font-size: 15px; font-weight: 600; flex: 1; letter-spacing: -0.005em; }
.card-body { padding: 20px; }
.card-footer { padding: 14px 20px; border-top: 1px solid var(--border); background: var(--bg-muted); }

/* ===================================================================
   Buttons
   =================================================================== */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  height: 36px; padding: 0 14px;
  border-radius: var(--r-md);
  font-size: 13.5px; font-weight: 500;
  border: 1px solid var(--border); background: var(--bg-surface);
  color: var(--text-primary);
  cursor: pointer; user-select: none;
  transition: var(--transition);
  white-space: nowrap;
  text-decoration: none;
}
.btn:hover { background: var(--bg-hover); border-color: var(--border-strong); }
.btn:active { transform: translateY(0.5px); }
.btn:disabled { opacity: 0.5; cursor: not-allowed; }

.btn-primary {
  background: var(--accent); color: var(--text-on-accent);
  border-color: var(--accent);
  box-shadow: 0 1px 2px hsl(var(--ah) var(--as) 30% / 0.2);
}
.btn-primary:hover { background: var(--accent-hover); border-color: var(--accent-hover); color: var(--text-on-accent); }

.btn-danger { background: var(--danger); border-color: var(--danger); color: white; }
.btn-danger:hover { background: hsl(0 75% 48%); border-color: hsl(0 75% 48%); color: white; }

.btn-ghost { background: transparent; border-color: transparent; }
.btn-ghost:hover { background: var(--bg-hover); border-color: transparent; }

.btn-sm { height: 30px; padding: 0 10px; font-size: 12.5px; border-radius: var(--r-sm); }
.btn-lg { height: 42px; padding: 0 18px; font-size: 14.5px; border-radius: var(--r-md); }
.btn-block { width: 100%; }

/* ===================================================================
   Forms
   =================================================================== */
.form-group { margin-bottom: 16px; }
.form-label {
  display: block;
  font-size: 12.5px; font-weight: 500;
  color: var(--text-secondary);
  margin-bottom: 6px;
}
.form-control, .form-select, .form-textarea {
  width: 100%;
  height: 40px; padding: 0 12px;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  color: var(--text-primary);
  font-family: inherit; font-size: 13.5px;
  transition: var(--transition);
  outline: none;
}
.form-textarea { height: auto; padding: 10px 12px; resize: vertical; min-height: 80px; }
.form-control:focus, .form-select:focus, .form-textarea:focus {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px hsl(var(--ah) var(--as) 50% / 0.18);
}
.form-control::placeholder, .form-textarea::placeholder { color: var(--text-muted); }
.form-help { font-size: 11.5px; color: var(--text-muted); margin-top: 4px; }
.form-row { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.form-row-3 { display: grid; grid-template-columns: repeat(3, 1fr); gap: 14px; }

.checkbox-row, .radio-row { display: flex; align-items: center; gap: 8px; font-size: 13.5px; cursor: pointer; }
.checkbox-row input, .radio-row input { accent-color: var(--accent); width: 16px; height: 16px; }

/* ===================================================================
   Tables
   =================================================================== */
.table-wrap { overflow-x: auto; }
.table {
  width: 100%; border-collapse: collapse;
  font-size: 13.5px;
}
.table thead th {
  text-align: left; padding: 10px 16px;
  font-size: 11.5px; font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase; letter-spacing: 0.04em;
  background: var(--bg-muted);
  border-bottom: 1px solid var(--border);
}
.table tbody td {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  vertical-align: middle;
}
.table tbody tr:hover { background: var(--bg-hover); }
.table tbody tr:last-child td { border-bottom: 0; }

/* ===================================================================
   Badges & Chips
   =================================================================== */
.badge {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 2px 8px;
  font-size: 11.5px; font-weight: 600;
  border-radius: var(--r-pill);
  background: var(--bg-muted);
  color: var(--text-secondary);
  white-space: nowrap;
}
.badge-dot::before {
  content: ''; width: 6px; height: 6px; border-radius: 50%; background: currentColor;
}

.status-new        { background: hsl(210 90% 95%); color: hsl(210 80% 35%); }
.status-contacted  { background: hsl(280 60% 95%); color: hsl(280 50% 38%); }
.status-interested { background: hsl(38 95% 92%);  color: hsl(38 80% 38%); }
.status-appointment{ background: hsl(180 60% 92%); color: hsl(180 60% 30%); }
.status-converted  { background: hsl(150 60% 92%); color: hsl(150 60% 30%); }
.status-lost       { background: hsl(0 70% 95%);   color: hsl(0 60% 42%); }

[data-theme="dark"] .status-new        { background: hsl(210 50% 20%); color: hsl(210 80% 78%); }
[data-theme="dark"] .status-contacted  { background: hsl(280 30% 22%); color: hsl(280 60% 80%); }
[data-theme="dark"] .status-interested { background: hsl(38 50% 22%);  color: hsl(38 90% 75%); }
[data-theme="dark"] .status-appointment{ background: hsl(180 40% 20%); color: hsl(180 70% 75%); }
[data-theme="dark"] .status-converted  { background: hsl(150 40% 20%); color: hsl(150 60% 75%); }
[data-theme="dark"] .status-lost       { background: hsl(0 40% 22%);   color: hsl(0 70% 80%); }

.label-hot   { background: hsl(0 80% 92%);  color: hsl(0 70% 40%); }
.label-warm  { background: hsl(38 90% 90%); color: hsl(28 75% 38%); }
.label-cold  { background: hsl(210 70% 92%);color: hsl(210 65% 38%); }
[data-theme="dark"] .label-hot   { background: hsl(0 50% 24%);  color: hsl(0 75% 80%); }
[data-theme="dark"] .label-warm  { background: hsl(38 50% 22%); color: hsl(38 90% 78%); }
[data-theme="dark"] .label-cold  { background: hsl(210 40% 24%);color: hsl(210 80% 80%); }

.chip {
  display: inline-flex; align-items: center; gap: 4px;
  padding: 3px 9px; border-radius: var(--r-pill);
  font-size: 11.5px; font-weight: 500;
  background: var(--bg-muted);
  color: var(--text-secondary);
  border: 1px solid var(--border);
  cursor: default;
}
.chip-tag { color: white; border: 0; }
.chip-rm { cursor: pointer; opacity: 0.7; margin-left: 2px; }
.chip-rm:hover { opacity: 1; }

/* ===================================================================
   KPI / Stats
   =================================================================== */
.stats-grid {
  display: grid; gap: 16px;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  margin-bottom: 24px;
}
.stat-card {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  padding: 18px 20px;
  box-shadow: var(--shadow-xs);
  position: relative; overflow: hidden;
}
.stat-card::before {
  content: ''; position: absolute; inset: 0 0 auto 0; height: 3px;
  background: var(--accent);
  opacity: 0.6;
}
.stat-label { font-size: 12px; color: var(--text-muted); font-weight: 500; text-transform: uppercase; letter-spacing: 0.04em; }
.stat-value { font-size: 28px; font-weight: 700; letter-spacing: -0.02em; margin-top: 6px; color: var(--text-primary); }
.stat-trend { font-size: 12px; color: var(--text-secondary); margin-top: 4px; }

/* ===================================================================
   Toolbar (filter row above tables)
   =================================================================== */
.toolbar {
  display: flex; flex-wrap: wrap; align-items: center; gap: 10px;
  padding: 14px 16px;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  margin-bottom: 16px;
}
.toolbar .form-control, .toolbar .form-select { height: 36px; }
.toolbar .grow { flex: 1; min-width: 200px; }
.toolbar .right { margin-left: auto; display: flex; gap: 8px; }

/* ===================================================================
   Tabs
   =================================================================== */
.tabs {
  display: flex; gap: 4px;
  border-bottom: 1px solid var(--border);
  margin-bottom: 16px;
}
.tab {
  padding: 9px 14px;
  font-size: 13.5px; font-weight: 500;
  color: var(--text-secondary);
  border-bottom: 2px solid transparent;
  cursor: pointer;
  transition: var(--transition);
  background: none; border-top: 0; border-left: 0; border-right: 0;
}
.tab:hover { color: var(--text-primary); }
.tab.active { color: var(--accent); border-bottom-color: var(--accent); }

/* ===================================================================
   Modal
   =================================================================== */
.modal-overlay {
  position: fixed; inset: 0; z-index: 100;
  background: hsl(var(--ah) 30% 10% / 0.5);
  backdrop-filter: blur(4px);
  display: grid; place-items: center;
  padding: 20px;
  animation: fadeIn 160ms ease;
}
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes slideUp { from { transform: translateY(10px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }

.modal {
  background: var(--bg-elevated);
  border-radius: var(--r-xl);
  box-shadow: var(--shadow-lg);
  border: 1px solid var(--border);
  width: 100%; max-width: 560px;
  max-height: calc(100vh - 40px);
  display: flex; flex-direction: column;
  animation: slideUp 200ms cubic-bezier(0.4,0,0.2,1);
}
.modal-lg { max-width: 820px; }
.modal-header {
  padding: 16px 20px;
  border-bottom: 1px solid var(--border);
  display: flex; align-items: center; gap: 12px;
}
.modal-title { margin: 0; font-size: 16px; font-weight: 600; flex: 1; }
.modal-body { padding: 20px; overflow-y: auto; }
.modal-footer { padding: 14px 20px; border-top: 1px solid var(--border); display: flex; gap: 8px; justify-content: flex-end; }
.modal-close {
  width: 28px; height: 28px; border-radius: var(--r-sm);
  background: transparent; border: 0; color: var(--text-muted);
  cursor: pointer; display: grid; place-items: center;
}
.modal-close:hover { background: var(--bg-hover); color: var(--text-primary); }

/* ===================================================================
   Kanban
   =================================================================== */
.kanban {
  display: grid; gap: 14px;
  grid-template-columns: repeat(6, minmax(240px, 1fr));
  overflow-x: auto;
  padding-bottom: 8px;
}
.kanban-col {
  background: var(--bg-muted);
  border-radius: var(--r-lg);
  padding: 12px;
  min-height: 200px;
}
.kanban-col-head {
  display: flex; align-items: center; gap: 8px;
  font-size: 13px; font-weight: 600;
  margin-bottom: 10px;
  color: var(--text-primary);
}
.kanban-count {
  background: var(--bg-surface); color: var(--text-secondary);
  padding: 1px 8px; border-radius: var(--r-pill);
  font-size: 11px; font-weight: 600;
}
.kanban-card {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--r-md);
  padding: 11px 12px;
  margin-bottom: 8px;
  cursor: grab;
  transition: var(--transition);
  box-shadow: var(--shadow-xs);
}
.kanban-card:hover { box-shadow: var(--shadow-sm); transform: translateY(-1px); }
.kanban-card.dragging { opacity: 0.5; cursor: grabbing; }
.kanban-card-name { font-weight: 600; font-size: 13.5px; margin-bottom: 2px; }
.kanban-card-meta { font-size: 11.5px; color: var(--text-muted); display: flex; gap: 8px; }
.kanban-col.drag-over { outline: 2px dashed var(--accent); outline-offset: -4px; }

/* ===================================================================
   WhatsApp inbox
   =================================================================== */
.wa-layout {
  display: grid;
  grid-template-columns: 320px 1fr;
  gap: 0;
  height: calc(100vh - var(--topbar-h) - 120px);
  min-height: 500px;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  overflow: hidden;
}
.wa-sidebar { border-right: 1px solid var(--border); display: flex; flex-direction: column; min-height: 0; }
.wa-search { padding: 12px; border-bottom: 1px solid var(--border); flex-shrink: 0; }
.wa-list { flex: 1; overflow-y: auto; min-height: 0; }
.wa-conv {
  display: flex; align-items: center; gap: 10px;
  padding: 10px 14px;
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: var(--transition);
  text-decoration: none;
  color: inherit;
}
.wa-conv:hover { background: var(--bg-hover); }
.wa-conv.active { background: var(--accent-tint); }
.wa-conv-info { flex: 1; min-width: 0; }
.wa-conv-name { font-weight: 600; font-size: 13.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.wa-conv-preview { font-size: 12px; color: var(--text-muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.wa-conv-meta { display: flex; flex-direction: column; align-items: flex-end; gap: 4px; flex-shrink: 0; }
.wa-time { font-size: 10.5px; color: var(--text-muted); }
.wa-unread {
  background: var(--accent); color: white;
  font-size: 10px; font-weight: 700;
  padding: 1px 6px; border-radius: var(--r-pill); min-width: 18px; text-align: center;
}

.wa-main { display: flex; flex-direction: column; background: var(--bg-base); min-height: 0; }
.wa-header {
  padding: 12px 18px;
  border-bottom: 1px solid var(--border);
  background: var(--bg-surface);
  display: flex; align-items: center; gap: 12px;
  flex-shrink: 0;
}
.wa-thread { flex: 1; overflow-y: auto; padding: 18px; min-height: 0; }
.wa-msg { max-width: 70%; margin-bottom: 8px; padding: 8px 12px; border-radius: 12px; font-size: 13.5px; line-height: 1.45; word-wrap: break-word; }
.wa-msg-in  { background: var(--bg-surface); border: 1px solid var(--border); border-bottom-left-radius: 4px; align-self: flex-start; }
.wa-msg-out { background: var(--accent); color: white; border-bottom-right-radius: 4px; align-self: flex-end; margin-left: auto; }
.wa-msg-time { font-size: 10px; opacity: 0.7; margin-top: 3px; }
.wa-thread-inner { display: flex; flex-direction: column; }

.wa-compose {
  border-top: 1px solid var(--border);
  background: var(--bg-surface);
  padding: 12px;
  display: flex; gap: 10px;
  flex-shrink: 0;
  align-items: flex-end;
}
.wa-compose textarea {
  flex: 1; border: 1px solid var(--border); border-radius: var(--r-md);
  padding: 10px 12px; resize: none; min-height: 42px; max-height: 140px;
  background: var(--bg-base); color: var(--text-primary); font-family: inherit; font-size: 13.5px;
}
.wa-compose textarea:focus { outline: none; border-color: var(--accent); box-shadow: 0 0 0 3px hsl(var(--ah) var(--as) 50% / 0.18); }

/* ===================================================================
   Calendar
   =================================================================== */
.calendar {
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--r-lg);
  overflow: hidden;
}
.cal-head {
  display: flex; align-items: center; gap: 10px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
}
.cal-month { font-weight: 600; font-size: 15px; flex: 1; }
.cal-grid {
  display: grid; grid-template-columns: repeat(7, 1fr);
}
.cal-dow {
  padding: 8px; text-align: center; font-size: 11px; font-weight: 600;
  color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.04em;
  background: var(--bg-muted); border-bottom: 1px solid var(--border);
}
.cal-day {
  position: relative;
  min-height: 84px;
  padding: 8px;
  border-right: 1px solid var(--border);
  border-bottom: 1px solid var(--border);
  cursor: pointer;
  transition: var(--transition);
}
.cal-day:nth-child(7n) { border-right: 0; }
.cal-day:hover { background: var(--bg-hover); }
.cal-day-num { font-size: 12.5px; font-weight: 500; }
.cal-day.other-month .cal-day-num { color: var(--text-muted); }
.cal-day.today { background: var(--accent-tint); }
.cal-day.today .cal-day-num { color: var(--accent); font-weight: 700; }
.cal-day.has-appt::after {
  content: ''; position: absolute; left: 8px; bottom: 8px;
  width: 6px; height: 6px; border-radius: 50%; background: var(--accent);
}
.cal-day.is-today-appt { background: hsl(150 60% 92%); }
[data-theme="dark"] .cal-day.is-today-appt { background: hsl(150 40% 18%); }

/* ===================================================================
   Slots
   =================================================================== */
.slot-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(90px, 1fr)); gap: 8px; margin-top: 10px; }
.slot-btn {
  padding: 8px; border: 1px solid var(--border); border-radius: var(--r-md);
  background: var(--bg-surface); color: var(--text-primary);
  cursor: pointer; font-size: 12.5px; font-weight: 500;
  transition: var(--transition);
}
.slot-btn:hover { border-color: var(--accent); }
.slot-btn.selected { background: var(--accent); color: white; border-color: var(--accent); }
.slot-btn.unavailable { opacity: 0.4; cursor: not-allowed; }

/* ===================================================================
   Login page
   =================================================================== */
.auth-page {
  min-height: 100vh; display: grid; place-items: center;
  padding: 20px; background:
    radial-gradient(circle at 0% 0%, hsl(var(--ah) var(--as) 95%) 0%, transparent 50%),
    radial-gradient(circle at 100% 100%, hsl(calc(var(--ah) + 40) var(--as) 95%) 0%, transparent 50%),
    var(--bg-base);
}
[data-theme="dark"] .auth-page {
  background:
    radial-gradient(circle at 0% 0%, hsl(var(--ah) 40% 18%) 0%, transparent 50%),
    radial-gradient(circle at 100% 100%, hsl(calc(var(--ah) + 40) 40% 18%) 0%, transparent 50%),
    var(--bg-base);
}
.auth-card {
  width: 100%; max-width: 420px;
  background: var(--bg-surface);
  border: 1px solid var(--border);
  border-radius: var(--r-xl);
  box-shadow: var(--shadow-lg);
  padding: 36px 32px;
}
.auth-brand { display: flex; align-items: center; gap: 10px; margin-bottom: 22px; }
.auth-title { font-size: 22px; font-weight: 700; letter-spacing: -0.02em; margin: 0 0 4px; }
.auth-sub { color: var(--text-muted); font-size: 13.5px; margin: 0 0 22px; }

/* ===================================================================
   Empty state
   =================================================================== */
.empty {
  text-align: center; padding: 48px 20px;
  color: var(--text-muted);
}
.empty-icon { width: 56px; height: 56px; border-radius: 50%; background: var(--bg-muted); display: grid; place-items: center; margin: 0 auto 12px; }
.empty-title { font-weight: 600; color: var(--text-primary); margin-bottom: 4px; }

/* ===================================================================
   Bulk action bar
   =================================================================== */
.bulk-bar {
  position: sticky; top: var(--topbar-h);
  z-index: 20;
  display: none;
  align-items: center; gap: 12px;
  padding: 12px 16px;
  margin-bottom: 12px;
  background: var(--accent-tint);
  border: 1px solid var(--accent);
  border-radius: var(--r-lg);
  box-shadow: var(--shadow-sm);
}
.bulk-bar .bulk-info { flex: 1; font-size: 13.5px; font-weight: 500; color: var(--text-primary); }
.bulk-bar .bulk-info strong { color: var(--accent); }
.row-check, .select-all { accent-color: var(--accent); width: 16px; height: 16px; cursor: pointer; }
.col-check { width: 36px; text-align: center; }
tr.row-selected { background: var(--accent-tint) !important; }
.bulk-table thead th.col-check { padding: 10px 8px; }
.bulk-table tbody td.col-check { padding: 12px 8px; }

/* ===================================================================
   Toast
   =================================================================== */
#toast-root {
  position: fixed; right: 16px; bottom: 16px; z-index: 200;
  display: flex; flex-direction: column; gap: 8px;
}
.toast {
  background: var(--bg-elevated); border: 1px solid var(--border);
  padding: 10px 14px; border-radius: var(--r-md);
  box-shadow: var(--shadow-md);
  font-size: 13px; font-weight: 500;
  min-width: 220px;
  animation: slideUp 200ms ease;
}
.toast-success { border-left: 3px solid var(--success); }
.toast-error   { border-left: 3px solid var(--danger); }

/* ===================================================================
   Stars
   =================================================================== */
.stars { display: inline-flex; gap: 2px; }
.star { color: hsl(var(--ah) 10% 80%); font-size: 18px; }
.star.filled { color: hsl(38 95% 55%); }

/* ===================================================================
   Util
   =================================================================== */
.text-muted { color: var(--text-muted); }
.text-secondary { color: var(--text-secondary); }
.text-danger { color: var(--danger); }
.text-success { color: var(--success); }
.text-center { text-align: center; }
.text-right { text-align: right; }
.fw-600 { font-weight: 600; }
.mt-0 { margin-top: 0; } .mt-2 { margin-top: 8px; } .mt-3 { margin-top: 12px; } .mt-4 { margin-top: 16px; }
.mb-0 { margin-bottom: 0; } .mb-2 { margin-bottom: 8px; } .mb-3 { margin-bottom: 12px; } .mb-4 { margin-bottom: 16px; }
.flex { display: flex; }
.items-center { align-items: center; }
.gap-2 { gap: 8px; } .gap-3 { gap: 12px; } .gap-4 { gap: 16px; }
.flex-1 { flex: 1; }
.hidden { display: none !important; }

/* ===================================================================
   Responsive — Complete Mobile Overhaul
   =================================================================== */

/* Sidebar overlay — real DOM element */
.sidebar-overlay {
  display: none;
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  z-index: 49;
  opacity: 0;
  transition: opacity 0.3s ease;
}
body.sidebar-open .sidebar-overlay {
  display: block;
  opacity: 1;
}

/* Sidebar close X button — only on mobile */
.sidebar-close {
  display: none;
  position: absolute;
  top: 12px;
  right: 12px;
  width: 38px;
  height: 38px;
  background: #FEE2E2;
  border: none;
  border-radius: 10px;
  color: #DC2626;
  cursor: pointer;
  align-items: center;
  justify-content: center;
  transition: all 0.2s;
  z-index: 2;
}
.sidebar-close:hover {
  background: #DC2626;
  color: white;
  transform: rotate(90deg);
}

@media (max-width: 960px) {
  .sidebar {
    transform: translateX(-100%);
    transition: transform 0.3s cubic-bezier(0.4,0,0.2,1);
    z-index: 50;
  }
  .sidebar-close { display: flex; }
  body.sidebar-open .sidebar {
    transform: translateX(0);
    box-shadow: var(--shadow-lg);
  }
  .main { margin-left: 0; }
  .sidebar-toggle { display: grid; }
  .topbar { padding: 0 16px; }
  .page { padding: 16px; }
  .form-row, .form-row-3 { grid-template-columns: 1fr; }
  .kanban { grid-template-columns: repeat(6, 84vw); }

  /* WhatsApp mobile: show list when no active chat, show chat when active */
  .wa-layout { grid-template-columns: 1fr; }
  .wa-layout .wa-sidebar { display: flex !important; }
  .wa-layout .wa-main { display: none; }
  /* When a conversation is selected (has .has-active class OR URL has phone) */
  .wa-layout.has-active .wa-sidebar { display: none !important; }
  .wa-layout.has-active .wa-main { display: flex; }

  /* Back button for WA chat */
  .wa-back-btn {
    display: inline-flex !important;
    align-items: center;
    gap: 6px;
    padding: 6px 10px;
    border: 1px solid #E5E7EB;
    border-radius: 8px;
    background: white;
    font-size: 13px;
    cursor: pointer;
    margin-right: 8px;
  }

  body.sidebar-open { overflow: hidden; }
}


/* ============================================================
   NUCLEAR MOBILE FIX — overrides everything inline
   ============================================================ */

/* Prevent horizontal page scroll completely */
html, body { overflow-x: hidden !important; max-width: 100vw; }
.main { overflow-x: hidden; }
.page { box-sizing: border-box; max-width: 100%; }

/* ===== TABLET (≤1024px) ===== */
@media (max-width: 1024px) {
  .stat-grid, .stats-grid, .stats-row {
    grid-template-columns: repeat(2, 1fr) !important;
  }
  .global-search-wrap { display: none !important; }
}

/* ===== MOBILE (≤768px) — AGGRESSIVE ===== */
@media (max-width: 768px) {

  /* === LAYOUT BASE === */
  .topbar { padding: 0 12px !important; gap: 8px; height: 56px !important; }
  .page { padding: 12px !important; }
  .page-title { font-size: 16px !important; }
  body { font-size: 14px; }

  /* === STATS GRIDS — force 2-col === */
  .stat-grid, .stats-grid, .stats-row, .qc-stats {
    grid-template-columns: repeat(2, 1fr) !important;
    gap: 10px !important;
  }
  .stat-card, .qc-stat { padding: 14px !important; }
  .stat-card .stat-value, .qc-stat-val { font-size: 22px !important; }

  /* === ALL 2-COLUMN GRIDS STACK === */
  /* This is the most important rule - covers Dashboard, Lead view, Patient view, Appointments, Reports */
  [style*="grid-template-columns: 2fr 1fr"],
  [style*="grid-template-columns:2fr 1fr"],
  [style*="grid-template-columns: 1fr 2fr"],
  [style*="grid-template-columns:1fr 2fr"],
  [style*="grid-template-columns: 1fr 1fr"],
  [style*="grid-template-columns:1fr 1fr"],
  [style*="grid-template-columns:repeat(2"],
  [style*="grid-template-columns: repeat(2"] {
    grid-template-columns: 1fr !important;
  }

  /* Auto-fit grids — keep 2-col */
  [style*="auto-fit,minmax(180px"],
  [style*="auto-fit, minmax(180px"],
  [style*="auto-fit,minmax(200px"],
  [style*="auto-fit, minmax(200px"],
  [style*="auto-fit,minmax(220px"],
  [style*="auto-fit, minmax(220px"] {
    grid-template-columns: repeat(2, 1fr) !important;
  }

  /* === TOOLBAR === */
  .toolbar {
    flex-wrap: wrap !important;
    gap: 8px !important;
    padding: 12px !important;
  }
  .toolbar > h2, .toolbar > h3 {
    width: 100% !important;
    flex: 1 1 100% !important;
    font-size: 16px !important;
    margin: 0 0 4px 0 !important;
  }
  .toolbar form,
  .toolbar .grow,
  .toolbar > form.flex {
    width: 100% !important;
    flex: 1 1 100% !important;
    flex-wrap: wrap !important;
    gap: 6px !important;
    min-width: 0 !important;
  }
  .toolbar .form-control,
  .toolbar input[type="search"],
  .toolbar input[type="text"] {
    width: 100% !important;
    min-width: 0 !important;
    flex: 1 1 100% !important;
    max-width: 100% !important;
  }
  .toolbar .form-select,
  .toolbar select {
    flex: 1 1 calc(33.333% - 4px) !important;
    min-width: 0 !important;
    max-width: 100% !important;
    width: auto !important;
    font-size: 12px !important;
    padding: 6px 24px 6px 8px !important;
  }
  .toolbar .btn {
    flex: 1 1 auto !important;
    min-width: 0 !important;
    justify-content: center !important;
    white-space: nowrap !important;
    font-size: 12px !important;
    padding: 8px 10px !important;
  }
  .toolbar .right {
    width: 100% !important;
    margin-left: 0 !important;
    flex-wrap: wrap !important;
  }

  /* === TABLES === */
  .table-wrap, .table-container, .qc-tbl {
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
    width: 100% !important;
    max-width: 100% !important;
  }
  table {
    min-width: auto !important;
    width: 100% !important;
    font-size: 13px !important;
  }
  table th {
    font-size: 11px !important;
    padding: 8px 6px !important;
    white-space: nowrap;
  }
  table td {
    padding: 10px 6px !important;
    vertical-align: top !important;
    word-break: break-word !important;
  }
  table td .fw-600,
  table td strong {
    word-break: keep-all !important;
    white-space: normal !important;
  }
  .hide-mobile { display: none !important; }

  /* === REVIEWS TABLE — long text === */
  table td p,
  table td .review-text,
  .review-text {
    white-space: normal !important;
    word-wrap: break-word !important;
    max-width: 100% !important;
    font-size: 13px !important;
    line-height: 1.5 !important;
  }

  /* === CARDS === */
  .card { border-radius: 12px !important; }
  .card-header, .card-body { padding: 14px !important; }

  /* === BUTTONS === */
  .btn { white-space: nowrap; padding: 8px 12px; font-size: 13px; }
  .btn-sm { padding: 5px 10px !important; font-size: 11px !important; }
  .btn-lg { padding: 10px 18px !important; font-size: 14px !important; }

  /* === MODAL === */
  .modal {
    max-width: 95vw !important;
    margin: 10px !important;
    max-height: 90vh;
    overflow-y: auto;
  }

  /* === TABS === */
  .tabs, .tabbar, .nav-tabs {
    overflow-x: auto !important;
    flex-wrap: nowrap !important;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }
  .tabs::-webkit-scrollbar, .tabbar::-webkit-scrollbar { display: none; }
  .tab, .tabbar a {
    white-space: nowrap !important;
    flex-shrink: 0 !important;
  }

  /* === FORMS === */
  .form-row, .form-row-3, .form-row-2 {
    grid-template-columns: 1fr !important;
    gap: 10px !important;
  }

  /* === FILTERS === */
  .filters-bar, .filter-bar {
    flex-direction: column !important;
    align-items: stretch !important;
    gap: 8px !important;
  }
  .filters-bar select,
  .filters-bar input,
  .filters-bar button,
  .filter-bar > * {
    width: 100% !important;
    max-width: 100% !important;
  }

  /* === iOS no-zoom on input focus === */
  input, select, textarea, .form-control, .form-select {
    font-size: 16px !important;
  }

  /* === WHATSAPP INBOX MOBILE === */
  .wa-layout {
    grid-template-columns: 1fr !important;
    height: calc(100vh - 130px) !important;
  }
  /* Default state: show conv list, hide chat */
  .wa-layout .wa-sidebar {
    display: flex !important;
    width: 100% !important;
    border-right: none !important;
  }
  .wa-layout .wa-main {
    display: none !important;
  }
  /* When .has-active class: show chat, hide list */
  .wa-layout.has-active .wa-sidebar {
    display: none !important;
  }
  .wa-layout.has-active .wa-main {
    display: flex !important;
    flex-direction: column !important;
    height: calc(100vh - 130px) !important;
  }
  /* WA Header */
  .wa-header {
    padding: 10px 12px !important;
    gap: 8px !important;
  }
  .wa-back-btn { display: inline-flex !important; }
  /* WA messages */
  .wa-thread {
    padding: 12px !important;
    flex: 1 !important;
    overflow-y: auto !important;
  }
  .wa-msg {
    max-width: 85% !important;
    font-size: 13px !important;
  }
  /* WA composer */
  .wa-compose {
    padding: 8px !important;
    gap: 6px !important;
  }
  .wa-compose textarea {
    min-height: 38px !important;
    font-size: 14px !important;
  }
  /* WA tabs (Inbox/Templates/Automations/Bulk) - horizontal scroll */
  .tabbar {
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
  }

  /* === KANBAN === */
  .kanban { grid-template-columns: repeat(6, 84vw); }

  /* === DASHBOARD specific - notif bell area === */
  .notif-bell, .notif-icon { flex-shrink: 0; }

  /* === Sidebar (already working) === */
  .sidebar {
    transform: translateX(-100%);
    transition: transform 0.3s cubic-bezier(0.4,0,0.2,1);
    z-index: 50;
  }
  .sidebar-close { display: flex !important; }
  body.sidebar-open .sidebar {
    transform: translateX(0);
    box-shadow: var(--shadow-lg);
  }
  .main { margin-left: 0 !important; }
  .sidebar-toggle { display: grid; }
  body.sidebar-open { overflow: hidden; }

  /* === Featured / pricing / testimonials grids === */
  .features-grid, .testimonials-grid, .pricing-grid,
  .form-cards-grid, .review-cards {
    grid-template-columns: 1fr !important;
  }

  /* === Cells with very long content === */
  td:has(.review-text), td .review-text {
    max-width: 240px !important;
  }

  /* === WhatsApp thread inside Lead/Patient view (NOT inbox) === */
  /* Lead view ke andar wala chat box chota karo */
  .wa-thread[style*="380px"] {
    height: 200px !important;
    max-height: 200px !important;
    min-height: 200px !important;
  }

  /* === Status pipeline buttons in Lead/Patient view === */
  /* Card header / pipeline area — prevent right-side cut */
  .card-header {
    flex-wrap: wrap !important;
    gap: 8px !important;
  }
  .card-header .badge {
    flex-shrink: 0;
  }
  /* Pipeline status buttons */
  .card-body > div[style*="flex-wrap:wrap"] .btn,
  .card-body > div[style*="flex-wrap: wrap"] .btn {
    flex: 0 0 auto;
    font-size: 12px !important;
    padding: 6px 10px !important;
  }

  /* === Cards inside content area — no horizontal overflow === */
  .card, .card-body, .card-header {
    max-width: 100% !important;
    overflow-x: hidden;
  }
}

/* ===== VERY SMALL (≤480px) ===== */
@media (max-width: 480px) {
  /* Single column stats */
  .stat-grid, .stats-grid, .stats-row, .qc-stats {
    grid-template-columns: 1fr !important;
  }

  /* Auto-fit single col */
  [style*="auto-fit,minmax(180px"],
  [style*="auto-fit, minmax(180px"],
  [style*="auto-fit,minmax(200px"],
  [style*="auto-fit, minmax(200px"],
  [style*="auto-fit,minmax(220px"],
  [style*="auto-fit, minmax(220px"],
  [style*="grid-template-columns:repeat(2"],
  [style*="grid-template-columns: repeat(2"] {
    grid-template-columns: 1fr !important;
  }

  /* Toolbar selects 2-col instead of 3 */
  .toolbar .form-select,
  .toolbar select {
    flex: 1 1 calc(50% - 4px) !important;
  }

  .topbar { height: 52px !important; padding: 0 10px !important; }
  .page-title { font-size: 15px !important; }
  .page { padding: 10px !important; }

  table th, table td { padding: 6px 4px !important; font-size: 12px !important; }
  .btn-sm { padding: 4px 8px !important; font-size: 10px !important; }

  /* Modal full screen on tiny phones */
  .modal {
    max-width: 100vw !important;
    margin: 0 !important;
    min-height: 100vh;
    border-radius: 0 !important;
  }
}

/* Print */
@media print {
  .sidebar, .topbar, .toolbar, .sidebar-overlay { display: none; }
  .main { margin-left: 0; }
  .page { padding: 0; }
}
