/* Admin v3 — design tokens, layout, components. No dependencies. */
:root {
  color-scheme: light;
  --bg: #f5f5f3;
  --surface: #ffffff;
  --surface-2: #fafaf8;
  --surface-3: #f0efec;
  --line: #e4e3df;
  --line-strong: #cfcdc7;
  --text: #0f0f0e;
  --text-2: #52514e;
  --text-3: #85847e;
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --accent-soft: #e6f0fc;
  --good: #0d7a4f;   --good-soft: #def5ea;
  --warn: #9a6200;   --warn-soft: #fdf0d2;
  --bad: #c43130;    --bad-soft: #fde4e3;
  --info: #2a78d6;   --info-soft: #e6f0fc;
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100;
  --s5: #e87ba4; --s6: #008300; --s7: #4a3aa7; --s8: #e34948;
  --seq-0: #cde2fb; --seq-1: #86b6ef; --seq-2: #3987e5; --seq-3: #1c5cab; --seq-4: #104281;
  --shadow: 0 1px 2px rgba(15, 15, 14, .05), 0 4px 16px rgba(15, 15, 14, .06);
  --radius: 10px;
  --radius-sm: 7px;
  --sidebar-w: 232px;
  --font: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
}
@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --bg: #121211; --surface: #1a1a19; --surface-2: #20201f; --surface-3: #2a2a28;
    --line: #2e2e2c; --line-strong: #43433f;
    --text: #f4f3ee; --text-2: #c3c2b7; --text-3: #8d8c84;
    --accent: #3987e5; --accent-soft: #162a44;
    --good: #4cc590; --good-soft: #12301f; --warn: #e8b440; --warn-soft: #33290d;
    --bad: #f07070; --bad-soft: #3a1818; --info: #3987e5; --info-soft: #162a44;
    --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
    --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
    --seq-0: #184f95; --seq-1: #1c5cab; --seq-2: #3987e5; --seq-3: #6da7ec; --seq-4: #b7d3f6;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 4px 16px rgba(0, 0, 0, .35);
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121211; --surface: #1a1a19; --surface-2: #20201f; --surface-3: #2a2a28;
  --line: #2e2e2c; --line-strong: #43433f;
  --text: #f4f3ee; --text-2: #c3c2b7; --text-3: #8d8c84;
  --accent: #3987e5; --accent-soft: #162a44;
  --good: #4cc590; --good-soft: #12301f; --warn: #e8b440; --warn-soft: #33290d;
  --bad: #f07070; --bad-soft: #3a1818; --info: #3987e5; --info-soft: #162a44;
  --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
  --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
  --seq-0: #184f95; --seq-1: #1c5cab; --seq-2: #3987e5; --seq-3: #6da7ec; --seq-4: #b7d3f6;
  --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 4px 16px rgba(0, 0, 0, .35);
}

*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body { margin: 0; font: 14px/1.45 var(--font); color: var(--text); background: var(--bg); }
[hidden] { display: none !important; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { margin: 0; line-height: 1.2; font-weight: 650; letter-spacing: -.01em; }
h1 { font-size: 22px; } h2 { font-size: 16px; } h3 { font-size: 14px; }
button, input, select, textarea { font: inherit; color: inherit; }
.mono { font-family: var(--mono); font-size: 12.5px; }
.muted { color: var(--text-3); }
.nowrap { white-space: nowrap; }
.right { text-align: right; }
.num { font-variant-numeric: tabular-nums; text-align: right; }
.grow { flex: 1 1 auto; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.stack { display: flex; flex-direction: column; gap: 14px; }

/* ── shell ───────────────────────────────────────────── */
#app { display: grid; grid-template-columns: var(--sidebar-w) 1fr; min-height: 100vh; }
.sidebar { position: sticky; top: 0; height: 100vh; background: var(--surface); border-right: 1px solid var(--line);
  display: flex; flex-direction: column; padding: 14px 10px; gap: 4px; overflow-y: auto; }
.brand { display: flex; align-items: center; gap: 10px; padding: 6px 10px 14px; font-weight: 700; font-size: 15px; }
.brand svg { width: 26px; height: 26px; flex: none; }
.nav-group { margin: 12px 10px 4px; font-size: 11px; text-transform: uppercase; letter-spacing: .08em; color: var(--text-3); }
.nav a { display: flex; align-items: center; gap: 10px; padding: 8px 10px; border-radius: var(--radius-sm); color: var(--text-2); font-weight: 500; }
.nav a svg { width: 18px; height: 18px; flex: none; opacity: .85; }
.nav a:hover { background: var(--surface-3); text-decoration: none; color: var(--text); }
.nav a.active { background: var(--accent-soft); color: var(--accent); }
.nav .count { margin-left: auto; font-size: 11px; font-weight: 650; background: var(--bad); color: #fff; border-radius: 99px; padding: 1px 7px; }
.sidebar .foot { margin-top: auto; padding: 10px; border-top: 1px solid var(--line); display: flex; flex-direction: column; gap: 8px; font-size: 12.5px; }
.main { min-width: 0; display: flex; flex-direction: column; }
.topbar { position: sticky; top: 0; z-index: 20; display: flex; align-items: center; gap: 12px; padding: 10px 24px;
  background: color-mix(in srgb, var(--bg) 86%, transparent); backdrop-filter: blur(10px); border-bottom: 1px solid var(--line); }
.topbar h1 { font-size: 17px; }
.content { padding: 22px 24px 60px; max-width: 1500px; width: 100%; }
.topbar .menu-btn { display: none; }
@media (max-width: 860px) {
  #app { grid-template-columns: 1fr; }
  .sidebar { position: fixed; z-index: 40; inset: 0 auto 0 0; width: 260px; transform: translateX(-100%); transition: transform .18s; }
  body.nav-open .sidebar { transform: none; box-shadow: var(--shadow); }
  .topbar .menu-btn { display: inline-flex; }
  body.nav-open::after { content: ''; position: fixed; inset: 0; z-index: 30; background: rgba(0, 0, 0, .35); }
  .topbar, .content { padding-left: 14px; padding-right: 14px; }
}

/* ── controls ────────────────────────────────────────── */
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; height: 34px; padding: 0 13px; border-radius: var(--radius-sm);
  border: 1px solid var(--line-strong); background: var(--surface); color: var(--text); font-weight: 550; cursor: pointer; white-space: nowrap; }
.btn:hover { background: var(--surface-3); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }
.btn.primary:hover { filter: brightness(1.08); }
.btn.danger { color: var(--bad); border-color: color-mix(in srgb, var(--bad) 45%, var(--line)); }
.btn.danger.solid { background: var(--bad); color: #fff; border-color: var(--bad); }
.btn.good { background: var(--good); color: #fff; border-color: var(--good); }
.btn.sm { height: 28px; padding: 0 9px; font-size: 12.5px; }
.btn.ghost { border-color: transparent; background: transparent; }
.btn svg { width: 16px; height: 16px; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
.input, select.input, textarea.input { height: 34px; padding: 0 10px; border-radius: var(--radius-sm); border: 1px solid var(--line-strong);
  background: var(--surface); min-width: 0; }
textarea.input { height: auto; padding: 8px 10px; resize: vertical; width: 100%; }
.input:focus { outline: 2px solid var(--accent); outline-offset: -1px; border-color: var(--accent); }
.field { display: flex; flex-direction: column; gap: 4px; min-width: 130px; }
.field > label { font-size: 11.5px; font-weight: 600; color: var(--text-3); text-transform: uppercase; letter-spacing: .04em; }
.check { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; user-select: none; }
.seg { display: inline-flex; border: 1px solid var(--line-strong); border-radius: var(--radius-sm); overflow: hidden; background: var(--surface); }
.seg button { border: 0; background: transparent; padding: 0 12px; height: 32px; cursor: pointer; font-weight: 550; color: var(--text-2); }
.seg button + button { border-left: 1px solid var(--line); }
.seg button.on { background: var(--accent); color: var(--accent-ink); }
.seg button:not(.on):hover { background: var(--surface-3); }

/* ── containers ──────────────────────────────────────── */
.card { background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: 0 1px 0 rgba(0,0,0,.02); }
.card > .hd { display: flex; align-items: center; gap: 10px; padding: 13px 16px; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.card > .bd { padding: 16px; }
.grid { display: grid; gap: 14px; }
.grid.k4 { grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); }
.grid.k2 { grid-template-columns: repeat(auto-fit, minmax(380px, 1fr)); }
.filters { display: flex; flex-wrap: wrap; gap: 10px 12px; align-items: flex-end; padding: 14px 16px; }
.kpi { padding: 14px 16px; }
.kpi .l { font-size: 12px; color: var(--text-3); font-weight: 600; }
.kpi .v { font-size: 26px; font-weight: 680; letter-spacing: -.02em; margin-top: 2px; font-variant-numeric: tabular-nums; }
.kpi .d { font-size: 12px; margin-top: 2px; color: var(--text-3); }
.d.up { color: var(--good); } .d.down { color: var(--bad); }

/* ── table ───────────────────────────────────────────── */
.tablewrap { overflow-x: auto; }
table.t { width: 100%; border-collapse: collapse; }
.t th { text-align: left; font-size: 11.5px; text-transform: uppercase; letter-spacing: .04em; color: var(--text-3); font-weight: 650;
  padding: 9px 12px; border-bottom: 1px solid var(--line); white-space: nowrap; background: var(--surface-2); position: sticky; top: 0; }
.t th.sort { cursor: pointer; } .t th.sort:hover { color: var(--text); }
.t td { padding: 9px 12px; border-bottom: 1px solid var(--line); vertical-align: middle; }
.t tbody tr:hover { background: var(--surface-2); }
.t tr.click { cursor: pointer; }
.t td.num, .t th.num { text-align: right; font-variant-numeric: tabular-nums; }
.pager { display: flex; align-items: center; gap: 10px; padding: 10px 14px; border-top: 1px solid var(--line); font-size: 13px; color: var(--text-2); flex-wrap: wrap; }
.empty { padding: 36px 16px; text-align: center; color: var(--text-3); }

/* ── bits ────────────────────────────────────────────── */
.badge { display: inline-flex; align-items: center; gap: 4px; padding: 1px 8px; border-radius: 99px; font-size: 11.5px; font-weight: 650; background: var(--surface-3); color: var(--text-2); white-space: nowrap; }
.badge.good { background: var(--good-soft); color: var(--good); }
.badge.warn { background: var(--warn-soft); color: var(--warn); }
.badge.bad { background: var(--bad-soft); color: var(--bad); }
.badge.info { background: var(--info-soft); color: var(--info); }
.avatar { width: 38px; height: 38px; border-radius: 50%; object-fit: cover; background: var(--surface-3); flex: none; display: block; }
.avatar.lg { width: 72px; height: 72px; }
.thumb { width: 100%; aspect-ratio: 1; object-fit: cover; border-radius: var(--radius-sm); background: var(--surface-3); display: block; cursor: zoom-in; }
.who { display: flex; align-items: center; gap: 10px; min-width: 0; }
.who b { display: block; font-weight: 600; } .who small { color: var(--text-3); }
.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--line); margin: 0 0 14px; overflow-x: auto; }
.tabs button { background: none; border: 0; padding: 9px 14px; cursor: pointer; font-weight: 600; color: var(--text-3); border-bottom: 2px solid transparent; margin-bottom: -1px; white-space: nowrap; }
.tabs button.on { color: var(--accent); border-bottom-color: var(--accent); }
.tabs button:hover:not(.on) { color: var(--text); }
dl.kv { display: grid; grid-template-columns: minmax(110px, 160px) 1fr; gap: 7px 14px; margin: 0; }
dl.kv dt { color: var(--text-3); } dl.kv dd { margin: 0; min-width: 0; overflow-wrap: anywhere; }
.alert { padding: 10px 14px; border-radius: var(--radius-sm); background: var(--warn-soft); color: var(--warn); font-weight: 550; }
.alert.bad { background: var(--bad-soft); color: var(--bad); }
.alert.info { background: var(--info-soft); color: var(--info); }

/* skeleton + spinner */
.skel { background: linear-gradient(90deg, var(--surface-3) 25%, var(--surface-2) 50%, var(--surface-3) 75%); background-size: 200% 100%; animation: sk 1.2s infinite; border-radius: 6px; min-height: 14px; }
@keyframes sk { to { background-position: -200% 0; } }
.spin { width: 18px; height: 18px; border: 2px solid var(--line-strong); border-top-color: var(--accent); border-radius: 50%; animation: rot .7s linear infinite; display: inline-block; }
@keyframes rot { to { transform: rotate(360deg); } }

/* drawer / modal / toast */
.scrim { position: fixed; inset: 0; background: rgba(10, 10, 9, .45); z-index: 60; animation: fade .12s; }
.drawer { position: fixed; z-index: 61; top: 0; right: 0; bottom: 0; width: min(760px, 100vw); background: var(--bg); box-shadow: var(--shadow); display: flex; flex-direction: column; animation: slide .16s; }
.drawer > header { display: flex; align-items: center; gap: 10px; padding: 14px 18px; background: var(--surface); border-bottom: 1px solid var(--line); }
.drawer > .body { flex: 1; overflow-y: auto; padding: 18px; }
.modal { position: fixed; z-index: 61; top: 50%; left: 50%; transform: translate(-50%, -50%); width: min(520px, calc(100vw - 24px)); max-height: 90vh; overflow: auto; background: var(--surface); border-radius: 14px; box-shadow: var(--shadow); padding: 20px; animation: fade .12s; }
.modal h2 { margin-bottom: 10px; }
.modal .actions { display: flex; gap: 8px; justify-content: flex-end; margin-top: 16px; }
.lightbox { position: fixed; inset: 0; z-index: 80; background: rgba(0, 0, 0, .88); display: grid; place-items: center; cursor: zoom-out; }
.lightbox img { max-width: 94vw; max-height: 92vh; border-radius: 6px; }
@keyframes fade { from { opacity: 0; } } @keyframes slide { from { transform: translateX(30px); opacity: 0; } }
#toasts { position: fixed; z-index: 90; right: 16px; bottom: 16px; display: flex; flex-direction: column; gap: 8px; }
.toast { background: var(--text); color: var(--bg); padding: 10px 14px; border-radius: 9px; box-shadow: var(--shadow); font-weight: 550; max-width: 380px; animation: fade .15s; }
.toast.bad { background: var(--bad); color: #fff; }

/* ── charts ──────────────────────────────────────────── */
.chart { position: relative; width: 100%; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart .grid-line { stroke: var(--line); stroke-width: 1; }
.chart .axis-label { fill: var(--text-3); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .hit { fill: transparent; }
.chart .cross { stroke: var(--line-strong); stroke-dasharray: 3 3; }
.chart-tip { position: absolute; z-index: 5; pointer-events: none; background: var(--surface); border: 1px solid var(--line-strong); border-radius: 8px;
  box-shadow: var(--shadow); padding: 8px 10px; font-size: 12.5px; min-width: 130px; transform: translate(-50%, -100%); white-space: nowrap; }
.chart-tip b { display: block; margin-bottom: 4px; }
.chart-tip .r { display: flex; align-items: center; gap: 8px; justify-content: space-between; }
.chart-tip .r i { width: 9px; height: 9px; border-radius: 3px; display: inline-block; margin-right: 6px; }
.legend { display: flex; flex-wrap: wrap; gap: 6px 14px; font-size: 12.5px; color: var(--text-2); margin-top: 10px; }
.legend button { display: inline-flex; align-items: center; gap: 6px; background: none; border: 0; cursor: pointer; padding: 2px 0; color: inherit; }
.legend button.off { opacity: .4; }
.legend i { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }
.hbar { display: grid; grid-template-columns: 110px 1fr 70px; gap: 10px; align-items: center; padding: 4px 0; }
.hbar .bar { height: 10px; background: var(--surface-3); border-radius: 5px; overflow: hidden; }
.hbar .bar > i { display: block; height: 100%; border-radius: 5px; background: var(--s1); }

/* conversation bubbles */
.msgs { display: flex; flex-direction: column; gap: 8px; padding: 6px 2px; }
.bubble { max-width: 78%; padding: 8px 12px; border-radius: 14px; background: var(--surface-3); align-self: flex-start; overflow-wrap: anywhere; white-space: pre-wrap; }
.bubble.anim { align-self: flex-end; background: var(--accent-soft); }
.bubble.inj { outline: 2px dashed var(--warn); }
.bubble small { display: block; color: var(--text-3); margin-top: 3px; font-size: 11px; }

.login { min-height: 100vh; display: grid; place-items: center; padding: 20px; }
.login .card { width: min(400px, 100%); padding: 28px; display: flex; flex-direction: column; gap: 14px; }
@media print { .sidebar, .topbar, .filters { display: none; } #app { display: block; } }
