:root {
  color-scheme: light;
  --bg: #f6f6f4;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --border: #e2e1dc;
  --text: #0b0b0b;
  --text-2: #52514e;
  --text-3: #7a7974;
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --accent-soft: #e6effb;
  --danger: #c4302f;
  --danger-soft: #fbeaea;
  --good: #1d7f3c;
  --warn: #a86b00;
  --grid: #e8e7e3;
  --axis: #9a9993;
  --shadow: 0 1px 2px rgb(0 0 0 / .05), 0 4px 16px rgb(0 0 0 / .04);

  --series-1: #2a78d6; --series-2: #eb6834; --series-3: #1baf7a; --series-4: #eda100;
  --series-5: #e87ba4; --series-6: #008300; --series-7: #6250d6; --series-8: #e34948;

  --seq-0: #f0efec; --seq-1: #d6e6f8; --seq-2: #a9c9ef; --seq-3: #6fa3e3; --seq-4: #2a78d6; --seq-5: #1a4f94;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #121211; --surface: #1a1a19; --surface-2: #242422; --border: #33332f;
    --text: #f0efec; --text-2: #c3c2b7; --text-3: #8f8e86;
    --accent: #3987e5; --accent-ink: #ffffff; --accent-soft: #1d2d42;
    --danger: #e66767; --danger-soft: #3a1f1f; --good: #4cc477; --warn: #e0a83a;
    --grid: #2a2a27; --axis: #6b6a64; --shadow: 0 1px 2px rgb(0 0 0 / .3);
    --series-1: #3987e5; --series-2: #d95926; --series-3: #199e70; --series-4: #c98500;
    --series-5: #d55181; --series-6: #008300; --series-7: #9085e9; --series-8: #e66767;
    --seq-0: #242422; --seq-1: #1f3550; --seq-2: #24507f; --seq-3: #2f6db0; --seq-4: #3987e5; --seq-5: #8ab8f2;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #121211; --surface: #1a1a19; --surface-2: #242422; --border: #33332f;
  --text: #f0efec; --text-2: #c3c2b7; --text-3: #8f8e86;
  --accent: #3987e5; --accent-ink: #ffffff; --accent-soft: #1d2d42;
  --danger: #e66767; --danger-soft: #3a1f1f; --good: #4cc477; --warn: #e0a83a;
  --grid: #2a2a27; --axis: #6b6a64; --shadow: 0 1px 2px rgb(0 0 0 / .3);
  --series-1: #3987e5; --series-2: #d95926; --series-3: #199e70; --series-4: #c98500;
  --series-5: #d55181; --series-6: #008300; --series-7: #9085e9; --series-8: #e66767;
  --seq-0: #242422; --seq-1: #1f3550; --seq-2: #24507f; --seq-3: #2f6db0; --seq-4: #3987e5; --seq-5: #8ab8f2;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text);
  font: 15px/1.5 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3 { line-height: 1.25; margin: 0; }
h1 { font-size: 1.5rem; }
h2 { font-size: 1.1rem; }
h3 { font-size: .95rem; }
[hidden] { display: none !important; }
.muted { color: var(--text-2); }
.faint { color: var(--text-3); }
.small { font-size: .85rem; }
.mono { font-family: ui-monospace, SFMono-Regular, Consolas, monospace; }
.nowrap { white-space: nowrap; }
.num { font-variant-numeric: tabular-nums; }

/* ---------- top bar ---------- */
.topbar {
  display: flex; align-items: center; gap: 24px; padding: 10px 24px;
  background: var(--surface); border-bottom: 1px solid var(--border);
  position: sticky; top: 0; z-index: 10;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; color: var(--text); font-size: 1.05rem; }
.brand:hover { text-decoration: none; }
.brand svg { stroke: var(--accent); stroke-width: 2; stroke-linecap: round; fill: none; }
.topbar nav { display: flex; gap: 4px; flex: 1; }
.topbar nav a { color: var(--text-2); padding: 6px 12px; border-radius: 8px; font-weight: 500; }
.topbar nav a:hover { background: var(--surface-2); text-decoration: none; }
.topbar nav a.active { color: var(--text); background: var(--surface-2); }

main { max-width: 1120px; margin: 0 auto; padding: 24px; outline: none; }

/* ---------- layout bits ---------- */
.page-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 16px; margin-bottom: 20px; flex-wrap: wrap; }
.page-head .sub { margin-top: 4px; }
.row { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.stack { display: flex; flex-direction: column; gap: 12px; }
.spacer { flex: 1; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: 12px;
  padding: 20px; box-shadow: var(--shadow); margin-bottom: 20px; min-width: 0;
}
.card-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-bottom: 14px; flex-wrap: wrap; }
.grid-2 { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
.back { display: inline-block; margin-bottom: 8px; font-size: .9rem; }

/* ---------- forms ---------- */
button, .button {
  font: inherit; font-weight: 600; cursor: pointer; border-radius: 8px;
  padding: 8px 14px; border: 1px solid var(--accent); background: var(--accent); color: var(--accent-ink);
  display: inline-flex; align-items: center; gap: 6px; text-decoration: none; line-height: 1.3;
}
button:hover, .button:hover { filter: brightness(1.06); text-decoration: none; }
button:disabled { opacity: .55; cursor: default; }
button.ghost, .button.ghost { background: transparent; color: var(--text); border-color: var(--border); }
button.ghost:hover, .button.ghost:hover { background: var(--surface-2); filter: none; }
button.danger { background: var(--danger); border-color: var(--danger); }
button.ghost.danger { background: transparent; color: var(--danger); border-color: var(--border); }
button.small, .button.small { padding: 4px 10px; font-size: .85rem; }
button.link { background: none; border: none; color: var(--accent); padding: 0; font-weight: 500; }

label { display: flex; flex-direction: column; gap: 4px; font-size: .85rem; font-weight: 600; color: var(--text-2); }
label.check { flex-direction: row; align-items: center; gap: 8px; font-weight: 500; }
input, select, textarea {
  font: inherit; color: var(--text); background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; padding: 8px 10px; min-width: 0; font-weight: 400;
}
input:focus, select:focus, textarea:focus, button:focus-visible, a:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}
input[type="checkbox"] { width: 16px; height: 16px; padding: 0; accent-color: var(--accent); }
textarea { resize: vertical; min-height: 70px; }
.form-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; align-items: end; }
.error { color: var(--danger); font-size: .9rem; min-height: 1.2em; }

/* segmented control */
.seg { display: inline-flex; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; background: var(--surface); }
.seg button { background: transparent; color: var(--text-2); border: none; border-radius: 0; padding: 6px 12px; font-weight: 500; }
.seg button + button { border-left: 1px solid var(--border); }
.seg button.on { background: var(--accent-soft); color: var(--text); font-weight: 600; }

/* ---------- tables ---------- */
.table-wrap { overflow-x: auto; margin: 0 -4px; }
table { border-collapse: collapse; width: 100%; font-size: .9rem; }
th { text-align: left; font-weight: 600; color: var(--text-2); font-size: .8rem; text-transform: uppercase; letter-spacing: .03em; }
th, td { padding: 9px 8px; border-bottom: 1px solid var(--border); vertical-align: middle; }
tbody tr:last-child td { border-bottom: none; }
tr.deleted td { color: var(--text-3); text-decoration: line-through; }
tr.deleted td.actions { text-decoration: none; }
td.actions { text-align: right; white-space: nowrap; }
tr.clickable { cursor: pointer; }
tr.clickable:hover td { background: var(--surface-2); }
th.r, td.r { text-align: right; }

/* ---------- pills / status ---------- */
.pill {
  display: inline-flex; align-items: center; gap: 5px; font-size: .78rem; font-weight: 600;
  padding: 2px 8px; border-radius: 999px; background: var(--surface-2); color: var(--text-2); white-space: nowrap;
}
.pill svg { width: 12px; height: 12px; }
.pill.good { color: var(--good); }
.pill.warn { color: var(--warn); }
.pill.bad { color: var(--danger); }
.swatch { display: inline-block; width: 10px; height: 10px; border-radius: 3px; flex: none; }

/* ---------- stat tiles ---------- */
.tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; margin-bottom: 20px; }
.tile { background: var(--surface); border: 1px solid var(--border); border-radius: 12px; padding: 14px 16px; box-shadow: var(--shadow); }
.tile .label { font-size: .8rem; color: var(--text-2); font-weight: 600; }
.tile .value { font-size: 1.7rem; font-weight: 700; font-variant-numeric: tabular-nums; margin-top: 2px; }
.tile .hint { font-size: .8rem; color: var(--text-3); }

/* ---------- client cards ---------- */
.client-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(250px, 1fr)); gap: 16px; }
.client-card {
  display: block; color: var(--text); background: var(--surface); border: 1px solid var(--border);
  border-radius: 12px; padding: 16px 18px; box-shadow: var(--shadow); transition: border-color .15s;
}
.client-card:hover { border-color: var(--accent); text-decoration: none; }
.client-card .name { font-weight: 700; font-size: 1.05rem; }
.client-card .nums { display: flex; gap: 20px; margin-top: 10px; }
.client-card .nums b { display: block; font-size: 1.35rem; font-variant-numeric: tabular-nums; }
.client-card .nums span { font-size: .78rem; color: var(--text-2); }
.client-card .foot { margin-top: 10px; font-size: .8rem; color: var(--text-3); }
.client-card.archived { opacity: .6; }
.empty { text-align: center; padding: 40px 20px; color: var(--text-2); }

/* ---------- charts ---------- */
.chart { width: 100%; position: relative; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart text { fill: var(--text-2); font-size: 11px; font-family: inherit; }
.chart .grid line { stroke: var(--grid); stroke-width: 1; }
.chart .baseline { stroke: var(--axis); stroke-width: 1; }
.chart .hit { fill: transparent; }
.chart .hit:hover + g .bar, .chart .bar-col:hover .bar { filter: brightness(1.12); }
.chart .bar { stroke: var(--surface); stroke-width: 1; }
.chart .cursor { fill: var(--text); opacity: .05; }
.s1 { fill: var(--series-1); background: var(--series-1); } .s2 { fill: var(--series-2); background: var(--series-2); }
.s3 { fill: var(--series-3); background: var(--series-3); } .s4 { fill: var(--series-4); background: var(--series-4); }
.s5 { fill: var(--series-5); background: var(--series-5); } .s6 { fill: var(--series-6); background: var(--series-6); }
.s7 { fill: var(--series-7); background: var(--series-7); } .s8 { fill: var(--series-8); background: var(--series-8); }
.s9 { fill: var(--axis); background: var(--axis); }
.h0 { fill: var(--seq-0); background: var(--seq-0); } .h1 { fill: var(--seq-1); background: var(--seq-1); }
.h2 { fill: var(--seq-2); background: var(--seq-2); } .h3 { fill: var(--seq-3); background: var(--seq-3); }
.h4 { fill: var(--seq-4); background: var(--seq-4); } .h5 { fill: var(--seq-5); background: var(--seq-5); }
.heat rect.cell { stroke: var(--surface); stroke-width: 2; }
.heat rect.cell:hover { stroke: var(--text); stroke-width: 1.5; }
.legend { display: flex; flex-wrap: wrap; gap: 6px 16px; font-size: .85rem; color: var(--text-2); }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.scale-legend { display: flex; align-items: center; gap: 4px; font-size: .78rem; color: var(--text-2); }
.scale-legend i { width: 16px; height: 12px; border-radius: 3px; display: inline-block; }
details.table-view { margin-top: 12px; }
details.table-view summary { cursor: pointer; font-size: .85rem; color: var(--text-2); }
details.table-view .table-wrap { max-height: 280px; overflow-y: auto; margin-top: 8px; }

#tooltip {
  position: fixed; z-index: 100; pointer-events: none; background: var(--text); color: var(--bg);
  padding: 7px 10px; border-radius: 8px; font-size: .82rem; line-height: 1.4; max-width: 260px;
  box-shadow: 0 4px 14px rgb(0 0 0 / .2);
}
#tooltip .tt-title { font-weight: 700; margin-bottom: 2px; }
#tooltip .tt-row { display: flex; align-items: center; gap: 6px; }
#tooltip .tt-row b { margin-left: auto; padding-left: 12px; font-variant-numeric: tabular-nums; }

/* ---------- token box / dialog ---------- */
.token-box {
  background: var(--surface-2); border: 1px dashed var(--border); border-radius: 8px; padding: 12px;
  display: flex; gap: 10px; align-items: center; word-break: break-all;
}
.token-box code { flex: 1; font-size: .95rem; }
.callout { background: var(--accent-soft); border-radius: 8px; padding: 12px 14px; font-size: .9rem; }
.callout.warn { background: var(--danger-soft); }
ol.steps { padding-left: 20px; margin: 8px 0 0; }
ol.steps li { margin-bottom: 6px; }

dialog {
  border: 1px solid var(--border); border-radius: 14px; padding: 0; background: var(--surface); color: var(--text);
  width: min(560px, calc(100vw - 32px)); box-shadow: 0 20px 60px rgb(0 0 0 / .25);
}
dialog::backdrop { background: rgb(0 0 0 / .4); }
dialog .dlg-body { padding: 22px; display: flex; flex-direction: column; gap: 14px; }
dialog .dlg-actions { display: flex; gap: 8px; justify-content: flex-end; flex-wrap: wrap; }

/* ---------- login ---------- */
.login-wrap { min-height: 80vh; display: grid; place-items: center; }
.login-card { width: min(380px, 100%); }
.login-card .brand { justify-content: center; font-size: 1.4rem; margin-bottom: 18px; }

/* ---------- toast ---------- */
#toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 200;
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 10px; font-size: .9rem;
  opacity: 0; transition: opacity .2s; pointer-events: none; max-width: calc(100vw - 32px);
}
#toast.show { opacity: 1; }
#toast.err { background: var(--danger); color: #fff; }

@media (max-width: 760px) {
  main { padding: 16px; }
  .topbar { padding: 10px 16px; gap: 12px; flex-wrap: wrap; }
  .topbar nav { order: 3; flex-basis: 100%; }
  .grid-2 { grid-template-columns: 1fr; }
  .hide-sm { display: none; }
  .card { padding: 16px; }
}
