/* ---------- Tokens ---------- */
:root {
  color-scheme: light;
  --page: #f6f6f3;
  --surface: #fcfcfb;
  --surface-2: #f1f0ec;
  --border: rgba(11, 11, 11, 0.1);
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #6f6d68;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --critical: #d03b3b;
  --good-ink: #006300;
  --bad-ink: #b42323;
  --good-bg: rgba(12, 163, 12, 0.1);
  --bad-bg: rgba(208, 59, 59, 0.1);
  --warn-bg: #fff4d6;
  --warn-ink: #6b4a00;
  --skeleton: #ebeae5;
  --skeleton-hi: #f6f5f1;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04), 0 1px 1px rgba(0, 0, 0, 0.03);
  --radius: 12px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #232321;
    --border: rgba(255, 255, 255, 0.1);
    --text: #ffffff;
    --text-2: #c3c2b7;
    --muted: #9a988f;
    --grid: #2c2c2a;
    --axis: #383835;
    --accent: #3987e5;
    --series-1: #3987e5;
    --series-2: #d95926;
    --critical: #e66767;
    --good-ink: #2fc42f;
    --bad-ink: #ff8a8a;
    --good-bg: rgba(12, 163, 12, 0.16);
    --bad-bg: rgba(230, 103, 103, 0.16);
    --warn-bg: #3a2f10;
    --warn-ink: #ffd877;
    --skeleton: #262624;
    --skeleton-hi: #30302d;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #1a1a19;
  --surface-2: #232321;
  --border: rgba(255, 255, 255, 0.1);
  --text: #ffffff;
  --text-2: #c3c2b7;
  --muted: #9a988f;
  --grid: #2c2c2a;
  --axis: #383835;
  --accent: #3987e5;
  --series-1: #3987e5;
  --series-2: #d95926;
  --critical: #e66767;
  --good-ink: #2fc42f;
  --bad-ink: #ff8a8a;
  --good-bg: rgba(12, 163, 12, 0.16);
  --bad-bg: rgba(230, 103, 103, 0.16);
  --warn-bg: #3a2f10;
  --warn-ink: #ffd877;
  --skeleton: #262624;
  --skeleton-hi: #30302d;
  --shadow: none;
}

/* ---------- Base ---------- */
*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--page);
  color: var(--text);
  font: 14px/1.45 var(--font);
  -webkit-font-smoothing: antialiased;
}
button, input, select { font: inherit; color: inherit; }
a { color: var(--accent); }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.btn {
  display: inline-flex; align-items: center; gap: 6px;
  height: 36px; padding: 0 14px;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); cursor: pointer; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: 0.55; cursor: progress; }
.btn-primary { background: var(--accent); border-color: transparent; color: var(--accent-ink); }
.btn-primary:hover { background: var(--accent); filter: brightness(1.08); }
.btn-sm { height: 30px; padding: 0 10px; font-size: 13px; }
.btn-icon { width: 36px; padding: 0; justify-content: center; }
.btn svg { width: 16px; height: 16px; flex: none; }
.spin svg { animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.input, .select {
  height: 36px; padding: 0 10px;
  border: 1px solid var(--border); border-radius: 8px;
  background: var(--surface); min-width: 0;
}

/* ---------- Header ---------- */
.topbar {
  position: sticky; top: 0; z-index: 20;
  background: color-mix(in srgb, var(--page) 88%, transparent);
  backdrop-filter: blur(8px);
  border-bottom: 1px solid var(--border);
}
.topbar-inner {
  max-width: 1400px; margin: 0 auto; padding: 12px 24px;
  display: flex; flex-wrap: wrap; align-items: center; gap: 10px 12px;
}
.brand { display: flex; align-items: center; gap: 10px; margin-right: auto; font-weight: 650; font-size: 16px; }
.brand-mark {
  width: 28px; height: 28px; border-radius: 8px; display: grid; place-items: center;
  background: var(--accent); color: var(--accent-ink);
}
.brand-mark svg { width: 16px; height: 16px; }
.badge {
  display: inline-flex; align-items: center; gap: 4px;
  font-size: 11px; font-weight: 600; letter-spacing: 0.02em; text-transform: uppercase;
  padding: 2px 8px; border-radius: 999px; background: var(--warn-bg); color: var(--warn-ink);
}
.controls { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.updated { font-size: 12px; color: var(--muted); min-width: 120px; }

/* Combobox */
.combo { position: relative; width: 300px; }
.combo .input { width: 100%; padding-right: 30px; }
.combo-caret { position: absolute; right: 10px; top: 50%; translate: 0 -50%; pointer-events: none; color: var(--muted); }
.combo-list {
  position: absolute; top: calc(100% + 4px); left: 0; right: 0; z-index: 30;
  max-height: 340px; overflow: auto; margin: 0; padding: 4px; list-style: none;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  box-shadow: 0 10px 30px rgba(0, 0, 0, 0.15);
}
.combo-list li { padding: 8px 10px; border-radius: 6px; cursor: pointer; }
.combo-list li[aria-selected="true"], .combo-list li:hover { background: var(--surface-2); }
.combo-list .opt-name { display: block; font-weight: 550; }
.combo-list .opt-sub { display: block; font-size: 12px; color: var(--muted); }
.combo-list .empty { color: var(--muted); cursor: default; }

.range { display: flex; align-items: center; gap: 6px; }
.range .input[type="date"] { width: 140px; }

/* ---------- Layout ---------- */
main { max-width: 1400px; margin: 0 auto; padding: 20px 24px 48px; }
.banner {
  display: flex; gap: 10px; align-items: flex-start;
  padding: 10px 14px; border-radius: 10px; margin-bottom: 16px;
  background: var(--bad-bg); color: var(--bad-ink); border: 1px solid color-mix(in srgb, var(--bad-ink) 25%, transparent);
}
.banner.warn { background: var(--warn-bg); color: var(--warn-ink); border-color: color-mix(in srgb, var(--warn-ink) 25%, transparent); }
.banner button { margin-left: auto; }

.info-strip {
  display: flex; flex-wrap: wrap; gap: 8px 28px; align-items: baseline;
  padding: 14px 18px; margin-bottom: 16px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
}
.info-strip h1 { font-size: 18px; margin: 0; font-weight: 650; flex-basis: 100%; }
.info-item { font-size: 13px; color: var(--text-2); }
.info-item b { color: var(--muted); font-weight: 500; margin-right: 6px; }
.pill { display: inline-block; padding: 1px 8px; border-radius: 999px; background: var(--surface-2); font-size: 12px; text-transform: capitalize; }

.kpis { display: grid; grid-template-columns: repeat(5, minmax(0, 1fr)); gap: 12px; margin-bottom: 16px; }
.kpi {
  padding: 14px 16px; min-height: 112px;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
  display: flex; flex-direction: column; gap: 4px;
}
.kpi-label { font-size: 12.5px; color: var(--text-2); display: flex; align-items: center; gap: 6px; }
.kpi-src { font-size: 10px; text-transform: uppercase; letter-spacing: 0.04em; color: var(--muted); border: 1px solid var(--border); border-radius: 4px; padding: 0 4px; }
.kpi-value { font-size: 26px; font-weight: 650; letter-spacing: -0.01em; }
.kpi-value.na { color: var(--muted); }
.kpi-delta { font-size: 12px; display: inline-flex; align-items: center; gap: 4px; padding: 2px 6px; border-radius: 6px; align-self: flex-start; }
.kpi-delta.good { color: var(--good-ink); background: var(--good-bg); }
.kpi-delta.bad { color: var(--bad-ink); background: var(--bad-bg); }
.kpi-delta.flat { color: var(--muted); background: var(--surface-2); }
.kpi-note { font-size: 11.5px; color: var(--muted); }

.grid { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 16px; }
.span-2 { grid-column: span 2; }
.span-3 { grid-column: span 3; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 16px 18px; min-width: 0; display: flex; flex-direction: column;
}
.card-head { display: flex; align-items: flex-start; gap: 12px; margin-bottom: 12px; flex-wrap: wrap; }
.card-head h2 { font-size: 15px; margin: 0; font-weight: 620; }
.card-head p { margin: 2px 0 0; font-size: 12.5px; color: var(--muted); }
.card-head .actions { margin-left: auto; display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.card-body { position: relative; flex: 1; min-height: 120px; }
.chart-box { position: relative; height: 260px; }
.chart-box.sm { height: 150px; }

/* Section states */
.state { display: grid; place-items: center; text-align: center; padding: 28px 12px; color: var(--muted); font-size: 13px; min-height: 140px; }
.state .icon { width: 28px; height: 28px; margin: 0 auto 8px; opacity: 0.7; }
.state.error { color: var(--bad-ink); }
.state button { margin-top: 10px; }
.skeleton { display: grid; gap: 10px; padding: 4px 0; }
.sk {
  height: 14px; border-radius: 6px;
  background: linear-gradient(90deg, var(--skeleton) 25%, var(--skeleton-hi) 37%, var(--skeleton) 63%);
  background-size: 400% 100%; animation: shimmer 1.4s ease infinite;
}
.sk.tall { height: 180px; }
.sk.w60 { width: 60%; } .sk.w40 { width: 40%; } .sk.w80 { width: 80%; }
.kpi .sk { height: 26px; width: 70%; }
@keyframes shimmer { 0% { background-position: 100% 50%; } 100% { background-position: 0 50%; } }
@media (prefers-reduced-motion: reduce) { .sk, .spin svg { animation: none; } }

/* Funnel */
.funnel { display: grid; gap: 10px; }
.f-row { display: grid; grid-template-columns: 92px minmax(0, 1fr) 170px; align-items: center; gap: 12px; }
.f-label { font-size: 13px; color: var(--text-2); }
.f-track { height: 24px; background: var(--surface-2); border-radius: 4px; overflow: hidden; }
.f-bar { height: 100%; background: var(--series-1); border-radius: 0 4px 4px 0; min-width: 2px; transition: width 0.4s ease; }
.f-bar.na { background: transparent; }
.f-nums { font-size: 13px; font-variant-numeric: tabular-nums; display: flex; flex-direction: column; line-height: 1.25; }
.f-nums b { font-weight: 620; }
.f-nums span { font-size: 11.5px; color: var(--muted); }

/* Legend */
.legend { display: flex; gap: 14px; font-size: 12px; color: var(--text-2); }
.legend i { display: inline-block; width: 12px; height: 3px; border-radius: 2px; margin-right: 6px; vertical-align: middle; }
.legend .l1 { background: var(--series-1); }
.legend .l2 { background: var(--series-2); }

/* Big number */
.big { font-size: 34px; font-weight: 650; line-height: 1.1; }
.big-sub { font-size: 12.5px; color: var(--muted); margin-bottom: 10px; }

/* Tabs */
.tabs { display: inline-flex; padding: 3px; background: var(--surface-2); border-radius: 9px; gap: 2px; }
.tab { border: 0; background: transparent; padding: 5px 12px; border-radius: 7px; cursor: pointer; font-size: 13px; color: var(--text-2); }
.tab[aria-selected="true"] { background: var(--surface); color: var(--text); box-shadow: var(--shadow); font-weight: 560; }
.tab .count { color: var(--muted); font-variant-numeric: tabular-nums; margin-left: 4px; }

/* Table */
.table-tools { display: flex; gap: 8px; align-items: center; margin-bottom: 10px; flex-wrap: wrap; }
.table-tools .input { width: 240px; max-width: 100%; }
.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: 10px; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { padding: 9px 12px; text-align: left; border-bottom: 1px solid var(--border); white-space: nowrap; }
tr:last-child td { border-bottom: 0; }
th { font-weight: 560; color: var(--text-2); background: var(--surface-2); position: sticky; top: 0; }
th.sortable { cursor: pointer; user-select: none; }
th.sortable:hover { color: var(--text); }
th .arrow { opacity: 0.6; margin-left: 4px; font-size: 11px; }
td.num, td.phone { font-variant-numeric: tabular-nums; }
.pager { display: flex; align-items: center; gap: 8px; justify-content: flex-end; margin-top: 10px; font-size: 12.5px; color: var(--muted); }

/* Toggle switch */
.switch { display: inline-flex; align-items: center; gap: 8px; font-size: 13px; cursor: pointer; color: var(--text-2); }
.switch input { appearance: none; width: 32px; height: 18px; border-radius: 999px; background: var(--axis); position: relative; cursor: pointer; transition: background 0.2s; margin: 0; }
.switch input::after { content: ""; position: absolute; top: 2px; left: 2px; width: 14px; height: 14px; border-radius: 50%; background: #fff; transition: transform 0.2s; }
.switch input:checked { background: var(--accent); }
.switch input:checked::after { transform: translateX(14px); }

/* Login */
.login-page { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: 100%; max-width: 360px; padding: 28px; }
.login-card h1 { font-size: 20px; margin: 14px 0 4px; }
.login-card p { margin: 0 0 18px; color: var(--muted); font-size: 13px; }
.login-card label { display: block; font-size: 13px; color: var(--text-2); margin-bottom: 6px; }
.login-card .input { width: 100%; margin-bottom: 14px; height: 40px; }
.login-card .btn { width: 100%; justify-content: center; height: 40px; }
.form-error { color: var(--bad-ink); font-size: 13px; min-height: 20px; margin-top: 10px; }

footer { max-width: 1400px; margin: 0 auto; padding: 0 24px 32px; font-size: 12px; color: var(--muted); }

/* ---------- Responsive ---------- */
@media (max-width: 1100px) {
  .kpis { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  .grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .span-3 { grid-column: span 2; }
}
@media (max-width: 760px) {
  .topbar-inner, main, footer { padding-left: 16px; padding-right: 16px; }
  .topbar { position: static; }
  .brand { flex-basis: 100%; }
  .controls { width: 100%; }
  .combo { width: 100%; }
  .range { flex-wrap: wrap; width: 100%; }
  .range .select { flex: 1; }
  .range .input[type="date"] { flex: 1; width: auto; }
  .grid { grid-template-columns: minmax(0, 1fr); }
  .span-2, .span-3 { grid-column: auto; }
  .kpis { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .kpi-value { font-size: 22px; }
  .f-row { grid-template-columns: 72px minmax(0, 1fr); }
  .f-nums { grid-column: 2; flex-direction: row; gap: 8px; align-items: baseline; }
  .table-tools .input { width: 100%; }
}

.kpis > .card { grid-column: 1 / -1; }

/* Cost card + wallet */
.stat-list { list-style: none; margin: 4px 0 0; padding: 0; display: grid; gap: 0; }
.stat-list li { display: flex; justify-content: space-between; gap: 12px; padding: 8px 0; border-top: 1px solid var(--border); font-size: 13px; }
.stat-list li span { color: var(--text-2); }
.stat-list li b { font-weight: 600; font-variant-numeric: tabular-nums; }
.stat-heading { margin: 16px 0 2px; font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: 0.04em; }
.wallet { font-size: 12px; padding: 4px 10px; border-radius: 999px; background: var(--surface-2); color: var(--text-2); white-space: nowrap; }
.wallet b { color: var(--text); font-weight: 600; }

/* New vs returning */
.split { display: flex; gap: 2px; height: 24px; margin: 4px 0 10px; }
.split div { border-radius: 4px; min-width: 4px; }
.split .s-new { background: var(--series-1); }
.split .s-ret { background: var(--series-2); }
.split-legend { display: flex; flex-wrap: wrap; gap: 6px 24px; margin-bottom: 14px; }
.split-legend div { display: flex; align-items: baseline; gap: 8px; }
.split-legend i { width: 10px; height: 10px; border-radius: 3px; display: inline-block; align-self: center; }
.split-legend .i-new { background: var(--series-1); }
.split-legend .i-ret { background: var(--series-2); }
.split-legend b { font-size: 22px; font-weight: 650; }
.split-legend span { color: var(--muted); font-size: 12.5px; }
.compare { width: 100%; font-size: 13px; }
.compare th, .compare td { padding: 7px 10px; }
.compare td.num, .compare th.num { text-align: right; }
.mix-foot { margin-top: 10px; font-size: 12.5px; color: var(--muted); }

/* Campaign report */
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: var(--surface-2); }
tr.clickable.current td { background: color-mix(in srgb, var(--accent) 10%, transparent); }
.status { display: inline-flex; align-items: center; gap: 6px; font-weight: 550; }
.status::before { content: ""; width: 8px; height: 8px; border-radius: 50%; }
.status.ok { color: var(--good-ink); }
.status.ok::before { background: #0ca30c; }
.status.bad { color: var(--bad-ink); }
.status.bad::before { background: #d03b3b; }
.report-totals { display: flex; flex-wrap: wrap; gap: 8px 28px; margin-bottom: 12px; font-size: 13px; color: var(--text-2); }
.report-totals b { color: var(--text); font-size: 15px; }

/* Replies */
.reply-text { white-space: normal; }
.warn-pill { background: var(--warn-bg); color: var(--warn-ink); text-transform: none; margin-left: 6px; }
[data-section="replies"] td { white-space: normal; }
[data-section="replies"] td.phone, [data-section="replies"] td.num { white-space: nowrap; }

/* Replies: kinds + by-campaign */
.reply-kinds { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 12px; margin-bottom: 18px; }
.reply-kind { border: 1px solid var(--border); border-radius: 10px; padding: 12px 14px; background: var(--surface-2); }
.reply-kind p { margin: 6px 0 10px; font-size: 12.5px; color: var(--text-2); }
.rk-head { display: flex; align-items: center; gap: 8px; font-weight: 600; }
.rk-head b { margin-left: auto; font-size: 18px; }
.rk-dot { width: 10px; height: 10px; border-radius: 3px; display: inline-block; flex: none; }
.rk-quoted { background: var(--series-1); }
.rk-chat { background: var(--series-2); }
.rk-tag { display: inline-flex; align-items: center; gap: 6px; }
.rk-example { display: inline-block; background: var(--surface); border: 1px solid var(--border); border-radius: 8px; padding: 6px 10px; font-size: 13px; }
.rk-quote { display: block; font-size: 11px; color: var(--muted); border-left: 3px solid var(--series-1); padding-left: 6px; margin-bottom: 3px; }
.sub-h { font-size: 13.5px; font-weight: 600; margin: 4px 0 8px; }
.sub-h + .table-wrap { margin-bottom: 18px; }
table.by-campaign td.num, table.by-campaign th.num { text-align: right; }
@media (max-width: 760px) { .reply-kinds { grid-template-columns: minmax(0, 1fr); } }
