:root {
  color-scheme: light;
  --bg: #f6f6f4;
  --surface-1: #fcfcfb;
  --surface-2: #f0efec;
  --border: #e2e1dc;
  --text-primary: #0b0b0b;
  --text-secondary: #52514e;
  --text-muted: #7a7974;
  --accent: #2a78d6;
  --grid: #e8e7e2;
  --good: #0ca30c;
  --critical: #d03b3b;
  --s1: #2a78d6; --s2: #eb6834; --s3: #1baf7a; --s4: #eda100;
  --s5: #e87ba4; --s6: #008300; --s7: #6250d6; --s8: #e34948;
  --radius: 10px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --bg: #121211; --surface-1: #1a1a19; --surface-2: #242423; --border: #33332f;
    --text-primary: #f0efec; --text-secondary: #c3c2b7; --text-muted: #8f8e86;
    --accent: #3987e5; --grid: #2c2c29;
    --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
    --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121211; --surface-1: #1a1a19; --surface-2: #242423; --border: #33332f;
  --text-primary: #f0efec; --text-secondary: #c3c2b7; --text-muted: #8f8e86;
  --accent: #3987e5; --grid: #2c2c29;
  --s1: #3987e5; --s2: #d95926; --s3: #199e70; --s4: #c98500;
  --s5: #d55181; --s6: #008300; --s7: #9085e9; --s8: #e66767;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body { background: var(--bg); color: var(--text-primary); font: 14px/1.45 var(--font); }
a { color: var(--accent); }
button, select, input { font: inherit; color: inherit; }
h1, h2, h3 { margin: 0; line-height: 1.2; }
h2 { font-size: 16px; font-weight: 600; }
small, .muted { color: var(--text-muted); }

/* Top bar */
.top { position: sticky; top: 0; z-index: 10; background: var(--surface-1); border-bottom: 1px solid var(--border); }
.top-inner { max-width: 1200px; margin: 0 auto; padding: 10px 16px; display: flex; align-items: center; gap: 12px; }
.brand { font-weight: 700; font-size: 15px; }
.brand span { color: var(--text-muted); font-weight: 400; }
.spacer { flex: 1; }
.who { color: var(--text-secondary); font-size: 13px; }
.btn { background: var(--surface-2); border: 1px solid var(--border); border-radius: 8px; padding: 6px 12px; cursor: pointer; }
.btn:hover { border-color: var(--text-muted); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.ghost { background: transparent; }

/* Filters: one row above the charts */
.filters { max-width: 1200px; margin: 0 auto; padding: 12px 16px 0; display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.filters select, .filters input { background: var(--surface-1); border: 1px solid var(--border); border-radius: 8px; padding: 6px 8px; max-width: 100%; }
.filters label { font-size: 12px; color: var(--text-muted); display: flex; flex-direction: column; gap: 2px; min-width: 0; }
.filters .clear { align-self: flex-end; }

main { max-width: 1200px; margin: 0 auto; padding: 12px 16px 48px; display: grid; gap: 16px; }
.card { background: var(--surface-1); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; min-width: 0; }
.card-head { display: flex; align-items: baseline; gap: 12px; margin-bottom: 12px; flex-wrap: wrap; }
.card-head p { margin: 0; color: var(--text-muted); font-size: 13px; }

/* Owner tiles */
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 10px; }
.tile { text-align: left; background: var(--surface-1); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px; cursor: pointer; min-width: 0; }
.tile:hover { border-color: var(--text-muted); }
.tile.active { outline: 2px solid var(--accent); outline-offset: -1px; }
.tile .name { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--text-secondary); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.tile .num { font-size: 28px; font-weight: 600; margin: 4px 0 2px; font-variant-numeric: tabular-nums; }
.tile .trend { font-size: 12px; color: var(--text-secondary); }
.tile .share { font-size: 12px; color: var(--text-muted); }
.dot { width: 10px; height: 10px; border-radius: 3px; flex: none; display: inline-block; }

/* Chart */
.chart-box { position: relative; height: 320px; }
.legend { display: flex; flex-wrap: wrap; gap: 6px 14px; margin-top: 10px; font-size: 12px; color: var(--text-secondary); }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.toggle-table { margin-left: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { text-align: left; padding: 8px 6px; border-bottom: 1px solid var(--border); vertical-align: top; }
th { color: var(--text-muted); font-weight: 500; font-size: 12px; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
.table-wrap { overflow-x: auto; }
.code { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12px; color: var(--text-secondary); white-space: nowrap; }
tr.click { cursor: pointer; }
tr.click:hover td { background: var(--surface-2); }
.spark { display: block; }
.pill { display: inline-block; padding: 1px 8px; border-radius: 999px; background: var(--surface-2); border: 1px solid var(--border); font-size: 12px; color: var(--text-secondary); white-space: nowrap; }

/* Conversations */
.conv-list td:first-child { width: 40%; }
.conv-sub { font-weight: 500; }
.conv-sum { color: var(--text-muted); font-size: 12px; }
.pager { display: flex; gap: 8px; align-items: center; justify-content: flex-end; margin-top: 10px; }

/* Detail drawer */
.drawer-bg { position: fixed; inset: 0; background: rgba(0,0,0,.35); z-index: 20; }
.drawer { position: fixed; top: 0; right: 0; bottom: 0; width: min(720px, 100%); background: var(--surface-1); z-index: 21; overflow-y: auto; border-left: 1px solid var(--border); }
.drawer header { position: sticky; top: 0; background: var(--surface-1); border-bottom: 1px solid var(--border); padding: 12px 16px; display: flex; gap: 12px; align-items: flex-start; }
.drawer header h2 { flex: 1; }
.drawer .facts { padding: 12px 16px; display: flex; flex-wrap: wrap; gap: 6px; border-bottom: 1px solid var(--border); }
.drawer .summary { padding: 12px 16px; color: var(--text-secondary); border-bottom: 1px solid var(--border); }
.msg { margin: 12px 16px; border: 1px solid var(--border); border-radius: var(--radius); }
.msg.out { background: var(--surface-2); }
.msg .meta { padding: 8px 12px; font-size: 12px; color: var(--text-muted); border-bottom: 1px solid var(--border); display: flex; gap: 8px; flex-wrap: wrap; }
.msg .meta b { color: var(--text-primary); }
.msg pre { margin: 0; padding: 12px; white-space: pre-wrap; word-wrap: break-word; font: inherit; }

.empty { color: var(--text-muted); padding: 24px 0; text-align: center; }
.error-box { background: var(--surface-2); border: 1px solid var(--critical); color: var(--text-primary); padding: 12px; border-radius: var(--radius); }

/* Login */
.login-body { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.login-card { width: min(380px, 100%); background: var(--surface-1); border: 1px solid var(--border); border-radius: var(--radius); padding: 24px; }
.login-card h1 { font-size: 20px; margin: 12px 0 6px; }
.login-card form { display: grid; gap: 8px; }
.login-card input { background: var(--bg); border: 1px solid var(--border); border-radius: 8px; padding: 10px; }
.login-hint { color: var(--text-secondary); margin: 0 0 8px; }
.login-code { letter-spacing: .3em; font-size: 20px; text-align: center; }
.login-error { color: var(--critical); }

@media (max-width: 640px) {
  .chart-box { height: 260px; }
  .top-inner { flex-wrap: wrap; }
  .who { display: none; }
  .hide-sm { display: none; }
  .conv-list td:first-child { width: auto; }
}
