:root {
  --bg: #f6f7f9; --panel: #ffffff; --ink: #111827; --muted: #6b7280; --line: #e5e7eb;
  --soft: #f3f4f6; --brand: #1d4ed8; --brand-ink: #ffffff; --ok: #15803d; --ok-soft: #dcfce7;
  --warn: #b45309; --warn-soft: #fef3c7; --bad: #b91c1c; --bad-soft: #fee2e2;
  --info-soft: #dbeafe; --shadow: 0 1px 2px rgba(0,0,0,.05), 0 4px 16px rgba(0,0,0,.04);
  --radius: 10px;
  font-family: Inter, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b0f17; --panel: #121826; --ink: #e5e7eb; --muted: #9ca3af; --line: #253046;
    --soft: #1a2232; --brand: #3b82f6; --ok: #4ade80; --ok-soft: #14321f; --warn: #fbbf24;
    --warn-soft: #3a2a0a; --bad: #f87171; --bad-soft: #3b1414; --info-soft: #172a4a;
    --shadow: none; color-scheme: dark;
  }
}
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--ink); font-size: 14px; line-height: 1.45; }
a { color: var(--brand); text-decoration: none; }
a:hover { text-decoration: underline; }
h1 { font-size: 20px; margin: 0 0 4px; }
h2 { font-size: 16px; margin: 0 0 12px; }
h3 { font-size: 14px; margin: 0 0 8px; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
.mono { font-family: ui-monospace, SFMono-Regular, Menlo, monospace; font-size: 12.5px; }

/* Layout */
.shell { display: grid; grid-template-columns: 220px 1fr; min-height: 100vh; }
.side { background: var(--panel); border-right: 1px solid var(--line); padding: 16px 10px;
        position: sticky; top: 0; height: 100vh; overflow: auto; }
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; padding: 4px 10px 16px; }
.brand-mark { width: 28px; height: 28px; border-radius: 8px; background: var(--brand);
              color: #fff; display: grid; place-items: center; font-size: 14px; }
.nav a { display: flex; justify-content: space-between; padding: 8px 10px; border-radius: 8px;
         color: var(--ink); margin-bottom: 2px; }
.nav a:hover { background: var(--soft); text-decoration: none; }
.nav a.on { background: var(--info-soft); color: var(--brand); font-weight: 600; }
.nav .group { margin: 14px 10px 4px; font-size: 11px; text-transform: uppercase;
              letter-spacing: .06em; color: var(--muted); }
.me { margin-top: 18px; padding: 10px; border-top: 1px solid var(--line); font-size: 12px; }
.main { padding: 24px 28px 60px; min-width: 0; }
.head { display: flex; align-items: flex-end; justify-content: space-between; gap: 12px;
        margin-bottom: 18px; flex-wrap: wrap; }
.grid { display: grid; gap: 14px; }
.g2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.g3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.g4 { grid-template-columns: repeat(4, minmax(0, 1fr)); }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius);
        padding: 16px; box-shadow: var(--shadow); min-width: 0; }
.stat .n { font-size: 24px; font-weight: 700; margin-top: 2px; font-variant-numeric: tabular-nums; }
.stat .l { color: var(--muted); font-size: 12px; }
.chart-box { position: relative; height: 260px; }

/* Controls */
.btn { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--line);
       background: var(--panel); color: var(--ink); padding: 7px 12px; border-radius: 8px;
       cursor: pointer; font: inherit; font-weight: 500; }
.btn:hover { background: var(--soft); }
.btn.primary { background: var(--brand); color: var(--brand-ink); border-color: var(--brand); }
.btn.danger { color: var(--bad); }
.btn.sm { padding: 4px 8px; font-size: 12px; }
.btn:disabled { opacity: .5; cursor: default; }
input, select, textarea { font: inherit; color: var(--ink); background: var(--panel);
       border: 1px solid var(--line); border-radius: 8px; padding: 7px 10px; width: 100%; }
textarea { min-height: 90px; resize: vertical; }
label { display: block; font-size: 12px; color: var(--muted); margin: 10px 0 4px; }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.row > * { flex: 0 0 auto; }
.filters { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 12px; }
.filters input, .filters select { width: auto; min-width: 140px; }
.filters input[type=search] { min-width: 240px; }
.filters-break { flex-basis: 100%; height: 0; }
table.sub { margin: 2px 0 6px; font-size: 12.5px; }
table.sub th { font-weight: 600; }
.schema td input, .schema td select { min-width: 0; }

/* Tables */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; }
th { text-align: left; font-size: 12px; color: var(--muted); font-weight: 600;
     border-bottom: 1px solid var(--line); padding: 8px 10px; white-space: nowrap; }
td { border-bottom: 1px solid var(--line); padding: 9px 10px; vertical-align: top; }
tr.click { cursor: pointer; }
tr.click:hover td { background: var(--soft); }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }

/* Badges */
.pill { display: inline-block; padding: 1px 8px; border-radius: 999px; font-size: 11.5px;
        background: var(--soft); color: var(--ink); white-space: nowrap; margin: 1px 2px 1px 0; }
.pill.ok { background: var(--ok-soft); color: var(--ok); }
.pill.warn { background: var(--warn-soft); color: var(--warn); }
.pill.bad { background: var(--bad-soft); color: var(--bad); }
.pill.info { background: var(--info-soft); color: var(--brand); }
.app-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 4px; }

/* 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(780px, 100vw);
          background: var(--bg); z-index: 21; overflow: auto; padding: 22px;
          box-shadow: -8px 0 30px rgba(0,0,0,.15); }
.tabs { display: flex; gap: 4px; border-bottom: 1px solid var(--line); margin: 14px 0; }
.tabs button { background: none; border: 0; padding: 8px 12px; cursor: pointer; font: inherit;
               color: var(--muted); border-bottom: 2px solid transparent; }
.tabs button.on { color: var(--brand); border-bottom-color: var(--brand); font-weight: 600; }
.timeline { list-style: none; padding: 0; margin: 0; }
.timeline li { padding: 10px 0; border-bottom: 1px solid var(--line); }
.timeline .meta { font-size: 12px; color: var(--muted); }
.timeline .body { white-space: pre-wrap; margin-top: 3px; }
.kv { display: grid; grid-template-columns: 130px 1fr; gap: 6px 12px; font-size: 13px; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; word-break: break-word; }
.msg { padding: 10px 12px; border-radius: 10px; margin: 8px 0; background: var(--panel);
       border: 1px solid var(--line); white-space: pre-wrap; }
.msg.staff { background: var(--info-soft); }
.msg.internal { background: var(--warn-soft); }
.msg.system { background: transparent; border-style: dashed; font-size: 12px; color: var(--muted); }

/* Toast and flash */
#toast { position: fixed; bottom: 18px; left: 50%; transform: translateX(-50%); z-index: 50;
         padding: 10px 16px; border-radius: 10px; background: var(--ink); color: var(--bg);
         display: none; max-width: 90vw; }
#toast.bad { background: var(--bad); color: #fff; }
.flash { padding: 10px 12px; border-radius: 8px; margin-bottom: 12px; }
.flash.error { background: var(--bad-soft); color: var(--bad); }
.flash.ok { background: var(--ok-soft); color: var(--ok); }
.empty { padding: 30px; text-align: center; color: var(--muted); }
.keybox { padding: 12px; border: 1px dashed var(--warn); background: var(--warn-soft);
          border-radius: 8px; word-break: break-all; margin: 10px 0; }

/* Sign-in pages */
.auth { min-height: 100vh; display: grid; place-items: center; padding: 16px; }
.auth .card { width: min(420px, 100%); padding: 28px; }
.auth .qr svg { width: 220px; height: 220px; max-width: 100%; background: #fff; border-radius: 8px; }

@media (max-width: 900px) {
  .shell { grid-template-columns: 1fr; }
  .side { position: static; height: auto; display: flex; flex-wrap: wrap; gap: 4px; padding: 8px; }
  .side .group, .side .me { display: none; }
  .brand { padding: 6px; }
  .nav { display: flex; flex-wrap: wrap; gap: 2px; }
  .main { padding: 16px; }
  .g2, .g3, .g4 { grid-template-columns: 1fr; }
  .kv { grid-template-columns: 1fr; }
}
