:root {
  --bg: #f0f2f7;
  --surface: #ffffff;
  --sidebar: #1a2744;
  --primary: #2563eb;
  --primary-h: #1d4ed8;
  --accent: #3b82f6;
  --text: #111827;
  --muted: #6b7280;
  --border: #e5e7eb;
  --warn: #f59e0b;
  --danger: #ef4444;
  --success: #10b981;
  --r: 12px;
  --shadow: 0 1px 3px rgba(0,0,0,.07), 0 4px 14px rgba(0,0,0,.05);
}

*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; overflow: hidden; }
body { font-family: 'DM Sans', sans-serif; background: var(--bg); color: var(--text); font-size: 14px; line-height: 1.5; }

#app { display: flex; height: 100vh; }

/* ── Sidebar ── */
.sidebar {
  width: 220px; min-width: 220px;
  background: var(--sidebar);
  display: flex; flex-direction: column;
}
.sidebar-logo {
  display: flex; align-items: center; gap: 10px;
  padding: 22px 18px;
  border-bottom: 1px solid rgba(255,255,255,.07);
}
.logo-mark {
  width: 34px; height: 34px;
  background: var(--primary); border-radius: 8px;
  display: flex; align-items: center; justify-content: center; flex-shrink: 0;
}
.logo-mark svg { width: 20px; height: 20px; }
.logo-text { color: white; font-size: 14px; font-weight: 400; }
.logo-text strong { font-weight: 700; color: #60a5fa; }

.sidebar-nav { flex: 1; padding: 10px; display: flex; flex-direction: column; gap: 2px; }
.nav-item {
  display: flex; align-items: center; gap: 10px;
  padding: 9px 12px; border-radius: 8px;
  color: rgba(255,255,255,.55); text-decoration: none; font-size: 13px; font-weight: 500;
  transition: all .15s; position: relative; cursor: pointer;
}
.nav-item svg { width: 17px; height: 17px; flex-shrink: 0; }
.nav-item:hover { color: white; background: rgba(255,255,255,.07); }
.nav-item.active { color: white; background: var(--primary); }
.nav-badge { margin-left: auto; background: #ef4444; color: white; font-size: 10px; font-weight: 700; padding: 1px 5px; border-radius: 20px; }
.nav-badge.hidden { display: none; }

.sidebar-footer {
  display: flex; align-items: center; gap: 8px;
  padding: 14px 18px; border-top: 1px solid rgba(255,255,255,.07);
  font-size: 11px; color: rgba(255,255,255,.35);
}
.status-dot { width: 7px; height: 7px; border-radius: 50%; background: #4b5563; flex-shrink: 0; transition: background .3s; }
.status-dot.online { background: var(--success); }
.status-dot.warn { background: var(--warn); }

/* ── Main ── */
.main { flex: 1; display: flex; flex-direction: column; overflow: hidden; }
.topbar {
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 28px; height: 60px;
  background: var(--surface); border-bottom: 1px solid var(--border); flex-shrink: 0;
}
.topbar h1 { font-size: 19px; font-weight: 800; }
.topbar-right { display: flex; align-items: center; gap: 8px; }
.icon-btn { width: 36px; height: 36px; border: none; background: transparent; border-radius: 8px; cursor: pointer; display: flex; align-items: center; justify-content: center; color: var(--muted); transition: background .15s; }
.icon-btn svg { width: 17px; height: 17px; }
.icon-btn:hover { background: var(--bg); }
.avatar { width: 34px; height: 34px; background: var(--primary); border-radius: 50%; display: flex; align-items: center; justify-content: center; color: white; font-weight: 700; font-size: 13px; cursor: pointer; }

.content { flex: 1; overflow-y: auto; padding: 22px 28px; }
.page { display: none; }
.page.active { display: block; }

/* ── Stats ── */
.stats-row { display: grid; grid-template-columns: repeat(4,1fr); gap: 14px; margin-bottom: 18px; }
.stat-card { background: var(--surface); border-radius: var(--r); padding: 18px 20px; box-shadow: var(--shadow); border: 1px solid var(--border); cursor: pointer; transition: transform .15s; }
.stat-card:hover { transform: translateY(-1px); }
.stat-card.warn { border-left: 3px solid var(--warn); }
.stat-label { font-size: 12px; color: var(--muted); font-weight: 600; margin-bottom: 4px; }
.stat-value { font-size: 34px; font-weight: 800; line-height: 1; margin-bottom: 5px; font-family: 'DM Mono', monospace; }
.stat-sub { font-size: 12px; color: var(--muted); }
.stat-sub.positive { color: var(--success); font-weight: 600; }
.stat-sub.link { color: var(--primary); font-weight: 600; cursor: pointer; }
.stat-sub.warn-text { color: var(--warn); font-weight: 600; }

/* ── Grid ── */
.dashboard-grid { display: grid; grid-template-columns: 1fr 1.15fr 1fr; gap: 14px; margin-bottom: 14px; }
.bottom-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }

/* ── Card ── */
.card { background: var(--surface); border-radius: var(--r); padding: 18px 20px; box-shadow: var(--shadow); border: 1px solid var(--border); }
.card-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 12px; }
.card-header h3 { font-size: 14px; font-weight: 700; }

/* ── Tables ── */
.data-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.data-table th { text-align: left; color: var(--muted); font-size: 11px; font-weight: 600; text-transform: uppercase; letter-spacing: .4px; padding: 0 6px 8px; border-bottom: 1px solid var(--border); }
.data-table td { padding: 9px 6px; border-bottom: 1px solid #f3f4f6; vertical-align: middle; }
.data-table tr:last-child td { border-bottom: none; }
.data-table.full { font-size: 14px; }
.fw { font-weight: 600; }
.mono { font-family: 'DM Mono', monospace; font-size: 12px; }
.empty-row { text-align: center; color: var(--muted); padding: 20px; font-size: 13px; }

/* ── Source badges ── */
.source-badge { display: inline-block; padding: 2px 8px; border-radius: 20px; font-size: 11px; font-weight: 600; background: #eff6ff; color: var(--primary); }
.source-badge.google { background: #fef3c7; color: #d97706; }
.source-badge.facebook { background: #ede9fe; color: #7c3aed; }
.source-badge.call { background: #d1fae5; color: #047857; }

/* ── Status select ── */
.status-select { border: 1.5px solid var(--border); border-radius: 6px; padding: 3px 6px; font-size: 12px; font-weight: 600; font-family: 'DM Sans', sans-serif; cursor: pointer; background: white; }
.status-select.status-new { color: #1d4ed8; border-color: #bfdbfe; background: #eff6ff; }
.status-select.status-contacted { color: #047857; border-color: #a7f3d0; background: #d1fae5; }
.status-select.status-booked { color: #6d28d9; border-color: #ddd6fe; background: #ede9fe; }
.status-select.status-lost { color: #b91c1c; border-color: #fecaca; background: #fee2e2; }

/* ── Conversations ── */
.conv-card { display: flex; flex-direction: column; }
.conv-list { flex: 1; overflow-y: auto; max-height: 220px; margin-bottom: 10px; }
.conv-item { display: flex; gap: 10px; padding: 8px; border-radius: 8px; cursor: pointer; transition: background .15s; }
.conv-item:hover, .conv-item.active-conv { background: #f0f9ff; }
.conv-av { width: 34px; height: 34px; border-radius: 50%; background: var(--primary); color: white; font-weight: 700; font-size: 13px; display: flex; align-items: center; justify-content: center; flex-shrink: 0; }
.conv-info { flex: 1; overflow: hidden; }
.conv-name { font-weight: 600; font-size: 13px; }
.conv-preview { font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.chat-thread { max-height: 180px; overflow-y: auto; display: flex; flex-direction: column; gap: 6px; padding: 6px 0; }
.bubble { max-width: 85%; padding: 8px 12px; border-radius: 12px; font-size: 13px; line-height: 1.45; position: relative; }
.bubble.in  { background: #f3f4f6; border-radius: 12px 12px 12px 2px; }
.bubble.out { background: var(--primary); color: white; border-radius: 12px 12px 2px 12px; align-self: flex-end; }
.bubble-time { display: block; font-size: 10px; opacity: .6; margin-top: 2px; }
.conv-compose { display: flex; gap: 8px; margin-top: auto; }
.conv-compose input { flex: 1; border: 1px solid var(--border); border-radius: 22px; padding: 9px 14px; font-size: 13px; font-family: 'DM Sans', sans-serif; outline: none; }
.conv-compose input:focus { border-color: var(--primary); }

/* ── Appointments ── */
.cal-date-label { font-size: 13px; font-weight: 600; color: var(--primary); margin-bottom: 10px; }
.appt-list { display: flex; flex-direction: column; }
.appt-row { display: flex; align-items: center; gap: 10px; padding: 8px 0; border-bottom: 1px solid var(--border); font-size: 13px; }
.appt-row:last-child { border-bottom: none; }
.appt-time { font-family: 'DM Mono', monospace; font-size: 12px; color: var(--muted); min-width: 62px; }
.link-text { color: var(--primary); font-weight: 600; cursor: pointer; }
.link-text:hover { text-decoration: underline; }
.ml-auto { margin-left: auto; }

/* ── Missed calls ── */
.missed-box { background: #fff7ed; border-radius: 8px; padding: 12px 14px; border: 1px solid #fed7aa; }
.missed-inner { display: flex; align-items: center; gap: 8px; }
.missed-badge { width: 22px; height: 22px; background: #ef4444; color: white; border-radius: 50%; display: flex; align-items: center; justify-content: center; font-size: 11px; font-weight: 700; }
.missed-sub { font-size: 12px; color: #92400e; margin-top: 6px; }

/* ── Review list ── */
.review-list { display: flex; flex-direction: column; }
.review-row { display: flex; align-items: center; gap: 10px; padding: 9px 0; border-bottom: 1px solid var(--border); font-size: 13px; }
.review-row:last-child { border-bottom: none; }
.rev-av { width: 30px; height: 30px; border-radius: 50%; background: var(--bg); display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 12px; color: var(--muted); border: 1px solid var(--border); }
.rev-name { font-weight: 600; flex: 1; }
.rev-status { font-size: 11px; color: var(--muted); }

/* ── Buttons ── */
.btn-primary { background: var(--primary); color: white; border: none; border-radius: 8px; padding: 8px 16px; font-size: 13px; font-weight: 600; font-family: 'DM Sans', sans-serif; cursor: pointer; transition: background .15s; white-space: nowrap; }
.btn-primary:hover { background: var(--primary-h); }
.btn-outline { background: transparent; color: var(--primary); border: 1.5px solid var(--primary); border-radius: 8px; padding: 7px 15px; font-size: 13px; font-weight: 600; font-family: 'DM Sans', sans-serif; cursor: pointer; transition: all .15s; white-space: nowrap; }
.btn-outline:hover { background: var(--primary); color: white; }
.btn-sm { background: #eff6ff; color: var(--primary); border: none; border-radius: 6px; padding: 4px 10px; font-size: 12px; font-weight: 600; cursor: pointer; font-family: 'DM Sans', sans-serif; transition: background .15s; }
.btn-sm:hover { background: var(--primary); color: white; }
.action-link { background: none; border: none; color: var(--primary); font-weight: 600; font-size: 12px; cursor: pointer; padding: 3px 7px; border-radius: 5px; transition: background .12s; text-decoration: none; }
.action-link:hover { background: #eff6ff; }
.action-link.danger { color: var(--danger); }
.action-link.danger:hover { background: #fee2e2; }

/* ── Page toolbar ── */
.page-toolbar { display: flex; gap: 10px; align-items: center; margin-bottom: 14px; flex-wrap: wrap; }
.page-toolbar input, .page-toolbar select { border: 1px solid var(--border); border-radius: 8px; padding: 7px 12px; font-size: 13px; font-family: 'DM Sans', sans-serif; outline: none; background: var(--surface); transition: border-color .15s; }
.page-toolbar input:focus, .page-toolbar select:focus { border-color: var(--primary); }
.page-toolbar input[type="search"] { min-width: 200px; }

/* ── Calendar ── */
.cal-grid-header { display: grid; grid-template-columns: repeat(7,1fr); text-align: center; font-size: 11px; font-weight: 700; text-transform: uppercase; letter-spacing: .4px; color: var(--muted); padding-bottom: 8px; border-bottom: 1px solid var(--border); margin-bottom: 6px; }
.cal-grid { display: grid; grid-template-columns: repeat(7,1fr); gap: 2px; }
.cal-day { min-height: 78px; padding: 5px; border-radius: 6px; font-size: 12px; cursor: pointer; transition: background .12s; }
.cal-day:hover { background: var(--bg); }
.cal-day.other-month { color: #d1d5db; }
.cal-day.today { background: #eff6ff; }
.cal-day.today .day-num { color: var(--primary); font-weight: 700; }
.day-num { font-weight: 600; font-size: 13px; margin-bottom: 3px; }
.cal-event { background: var(--primary); color: white; border-radius: 3px; padding: 1px 4px; font-size: 10px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; margin-bottom: 2px; }

/* ── Messages ── */
.messages-layout { display: grid; grid-template-columns: 270px 1fr; gap: 14px; height: calc(100vh - 120px); }
.conv-sidebar { background: var(--surface); border-radius: var(--r); border: 1px solid var(--border); display: flex; flex-direction: column; overflow: hidden; }
.conv-search input { width: 100%; border: none; border-bottom: 1px solid var(--border); padding: 12px 14px; font-size: 13px; font-family: 'DM Sans', sans-serif; outline: none; }
#conv-sidebar-list { flex: 1; overflow-y: auto; }
.conv-sidebar-item { display: flex; gap: 10px; padding: 12px 14px; cursor: pointer; border-bottom: 1px solid var(--border); transition: background .12s; }
.conv-sidebar-item:hover, .conv-sidebar-item.active { background: #eff6ff; }
.conv-main { background: var(--surface); border-radius: var(--r); border: 1px solid var(--border); display: flex; flex-direction: column; }
.conv-empty { flex: 1; display: flex; align-items: center; justify-content: center; color: var(--muted); font-size: 15px; }
.conv-main-header { padding: 14px 18px; border-bottom: 1px solid var(--border); display: flex; align-items: center; gap: 10px; }
.conv-main-header strong { font-size: 15px; }
.text-muted { color: var(--muted); font-size: 12px; }
.conv-main-msgs { flex: 1; overflow-y: auto; padding: 14px 18px; display: flex; flex-direction: column; gap: 8px; }
.conv-main-compose { padding: 14px 18px; border-top: 1px solid var(--border); display: flex; gap: 8px; }
.conv-main-compose input { flex: 1; border: 1px solid var(--border); border-radius: 22px; padding: 9px 14px; font-size: 13px; font-family: 'DM Sans', sans-serif; outline: none; }
.conv-main-compose input:focus { border-color: var(--primary); }

/* ── Automations ── */
.automations-grid { display: grid; grid-template-columns: repeat(3,1fr); gap: 14px; }
.auto-card { background: var(--surface); border-radius: var(--r); padding: 20px; border: 1px solid var(--border); }
.auto-icon { font-size: 24px; margin-bottom: 8px; }
.auto-card h3 { font-size: 14px; font-weight: 700; margin-bottom: 6px; }
.auto-card p { font-size: 13px; color: var(--muted); line-height: 1.5; margin-bottom: 10px; }
.auto-footer { margin-top: 12px; padding-top: 10px; border-top: 1px solid var(--border); font-size: 12px; }

/* ── Settings ── */
.settings-wrap { max-width: 1100px; }
.settings-wrap h2 { font-size: 20px; font-weight: 800; margin-bottom: 4px; }
.settings-desc { color: var(--muted); font-size: 13px; margin-bottom: 20px; }
.settings-desc code { background: #f3f4f6; padding: 1px 5px; border-radius: 4px; font-family: 'DM Mono', monospace; font-size: 12px; }
.settings-grid { display: grid; grid-template-columns: repeat(3,1fr); gap: 14px; }
.settings-card { background: var(--surface); border-radius: var(--r); padding: 18px; border: 1px solid var(--border); }
.settings-card h3 { font-size: 13px; font-weight: 700; margin-bottom: 14px; }
.form-group { margin-bottom: 12px; }
.form-group label { display: block; font-size: 11px; font-weight: 600; color: var(--muted); text-transform: uppercase; letter-spacing: .4px; margin-bottom: 4px; }
.form-group input, .form-group select, .form-group textarea { width: 100%; border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; font-size: 13px; font-family: 'DM Sans', sans-serif; outline: none; transition: border-color .15s; }
.form-group input:focus, .form-group select:focus, .form-group textarea:focus { border-color: var(--primary); }
.form-group textarea { resize: vertical; }
.settings-hint { font-size: 11px; color: var(--muted); margin: 6px 0; line-height: 1.6; }
.settings-hint code { background: #f3f4f6; padding: 1px 4px; border-radius: 3px; font-family: 'DM Mono', monospace; }
.test-result { display: block; font-size: 12px; margin-top: 6px; font-weight: 600; }
.test-result.ok { color: var(--success); }
.test-result.err { color: var(--danger); }
.test-result.warn { color: var(--warn); }

/* ── Modal ── */
.modal-overlay { position: fixed; inset: 0; background: rgba(0,0,0,.4); display: none; align-items: center; justify-content: center; z-index: 1000; backdrop-filter: blur(2px); }
.modal-overlay.open { display: flex; }
.modal { background: var(--surface); border-radius: 16px; padding: 24px; width: 460px; max-width: 95vw; box-shadow: 0 20px 60px rgba(0,0,0,.2); animation: modal-in .2s ease; }
@keyframes modal-in { from { transform: translateY(18px); opacity: 0; } to { transform: translateY(0); opacity: 1; } }
.modal-header { display: flex; align-items: center; justify-content: space-between; margin-bottom: 18px; }
.modal-header h3 { font-size: 17px; font-weight: 700; }
.modal-close { background: var(--bg); border: none; width: 28px; height: 28px; border-radius: 50%; cursor: pointer; font-size: 13px; color: var(--muted); display: flex; align-items: center; justify-content: center; }
.modal-close:hover { background: var(--border); }
.form-actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 18px; }

/* ── Toast ── */
.toast { position: fixed; bottom: 22px; right: 22px; background: #111827; color: white; padding: 11px 18px; border-radius: 10px; font-size: 13px; font-weight: 500; opacity: 0; transform: translateY(10px); transition: all .28s ease; z-index: 2000; pointer-events: none; max-width: 340px; }
.toast.show { opacity: 1; transform: translateY(0); }
.toast.toast-error { background: #991b1b; }

/* ── Scrollbar ── */
::-webkit-scrollbar { width: 5px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #d1d5db; border-radius: 3px; }

/* ── Responsive ── */
@media (max-width: 1200px) {
  .dashboard-grid { grid-template-columns: 1fr 1fr; }
  .settings-grid { grid-template-columns: repeat(2,1fr); }
  .automations-grid { grid-template-columns: repeat(2,1fr); }
}
@media (max-width: 900px) {
  .sidebar { width: 56px; min-width: 56px; }
  .logo-text, .nav-item span:not(.nav-badge), #api-label { display: none; }
  .nav-item { justify-content: center; padding: 10px; }
  .dashboard-grid { grid-template-columns: 1fr; }
  .stats-row { grid-template-columns: repeat(2,1fr); }
  .messages-layout { grid-template-columns: 1fr; }
}
