/* color tokens: light by default, dark by system preference or explicit choice */
:root {
  --bg: #fafaf8; --panel: #fff; --ink: #222; --ink2: #444; --muted: #777; --line: #e3e3de; --line2: #f0f0ec;
  --grid: #eee; --tick: #888; --soft: #f1f1ec; --hover: #f3f8fc; --accent: #2c7fb8; --bad: #c0392b; --good: #2e8b57;
  --track: #f3f3f0; --axis: #999; --tipbg: rgba(30, 30, 30, 0.92); --tipink: #fff; --shadow: rgba(0, 0, 0, 0.12);
  --badge-ok-bg: #e3f3e8; --badge-ok: #2e7d32; --badge-warn-bg: #fdecea;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15171a; --panel: #1d2024; --ink: #e6e6e3; --ink2: #c8c8c4; --muted: #9a9a96; --line: #31353b; --line2: #272a2f;
    --grid: #2b2f34; --tick: #8b8f95; --soft: #2a2d32; --hover: #1f2a35; --accent: #5aa9e6; --bad: #e57368; --good: #4fbf85;
    --track: #2a2d32; --axis: #6b7077; --tipbg: rgba(240, 240, 238, 0.95); --tipink: #111; --shadow: rgba(0, 0, 0, 0.5);
    --badge-ok-bg: #1f3a29; --badge-ok: #7fd49f; --badge-warn-bg: #45211e;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #15171a; --panel: #1d2024; --ink: #e6e6e3; --ink2: #c8c8c4; --muted: #9a9a96; --line: #31353b; --line2: #272a2f;
  --grid: #2b2f34; --tick: #8b8f95; --soft: #2a2d32; --hover: #1f2a35; --accent: #5aa9e6; --bad: #e57368; --good: #4fbf85;
  --track: #2a2d32; --axis: #6b7077; --tipbg: rgba(240, 240, 238, 0.95); --tipink: #111; --shadow: rgba(0, 0, 0, 0.5);
  --badge-ok-bg: #1f3a29; --badge-ok: #7fd49f; --badge-warn-bg: #45211e;
  color-scheme: dark;
}

:root { font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; font-size: 14px; color: var(--ink); }
* { box-sizing: border-box; }
body { margin: 0; background: var(--bg); color: var(--ink); }
.muted { color: var(--muted); }
.clickable { cursor: pointer; border-bottom: 1px dotted var(--muted); }
.clickable:hover { color: var(--accent); }
code { font-family: ui-monospace, Menlo, monospace; font-size: 12.5px; background: var(--soft); padding: 1px 4px; border-radius: 3px; }

/* header: tabs on the left, title on the right */
header { position: sticky; top: 0; z-index: 20; background: var(--panel); border-bottom: 1px solid var(--line);
  display: flex; align-items: flex-end; justify-content: space-between; gap: 16px; padding: 8px 16px 0; flex-wrap: wrap; }
.tabs { display: flex; gap: 2px; flex-wrap: wrap; }
.tabs button { border: 1px solid transparent; border-bottom: none; background: none; color: var(--ink2); padding: 7px 12px 8px;
  border-radius: 6px 6px 0 0; font-size: 13px; margin-bottom: -1px; }
.tabs button:hover { color: var(--accent); background: var(--hover); }
.tabs button.active { background: var(--bg); border-color: var(--line); color: var(--ink); font-weight: 600; }
.head-right { display: flex; align-items: center; gap: 14px; padding-bottom: 9px; margin-left: auto; text-align: right; }
.head-right .brand { font-size: 15px; }
.head-right .about { color: var(--accent); font-size: 13px; }
button.theme { width: 30px; height: 26px; padding: 0; font-size: 14px; line-height: 1; }

.layout { display: grid; grid-template-columns: 300px 1fr; gap: 0; }
#controls { position: sticky; top: 45px; height: calc(100vh - 45px); overflow-y: auto; background: var(--panel);
  border-right: 1px solid var(--line); padding: 12px 14px 40px; }
.ctl-section { margin-bottom: 14px; }
.ctl-section summary { font-weight: 600; cursor: pointer; margin-bottom: 6px; }
.hint { font-size: 12px; color: var(--muted); margin: 2px 0 8px; }
.lbl { display: block; font-size: 12px; color: var(--muted); margin: 6px 0 3px; }
select, input[type=number] { width: 100%; padding: 4px; font: inherit; background: var(--panel); color: var(--ink);
  border: 1px solid var(--line); border-radius: 4px; }
.preset-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 4px; }
.preset-head .lbl { margin: 0; }
.preset-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 6px; }
.preset { display: flex; flex-direction: column; align-items: center; gap: 3px; padding: 7px 3px 6px; min-height: 66px;
  border: 1px solid var(--line); border-radius: 8px; background: var(--panel); color: var(--ink2); font-size: 11px; line-height: 1.15; text-align: center; }
.preset svg { color: var(--accent); flex: none; }
.preset:hover { border-color: var(--accent); background: var(--hover); color: var(--ink); }
.preset.active { border: 2px solid var(--accent); padding: 6px 2px 5px; background: var(--hover); color: var(--ink); font-weight: 600; }
.preset-desc { font-size: 12px; line-height: 1.45; color: var(--ink2); margin: 8px 0 4px; padding: 7px 9px; background: var(--soft); border-radius: 6px; }
button { font: inherit; padding: 4px 10px; border: 1px solid var(--line); background: var(--panel); color: var(--ink);
  border-radius: 4px; cursor: pointer; }
button:hover { border-color: var(--accent); color: var(--accent); }
button.small { width: 58px; padding: 4px 0; }
.status { font-size: 12px; margin-top: 6px; min-height: 16px; }
.ctl { margin: 8px 0 10px; }
.ctl-top { display: flex; justify-content: space-between; font-size: 13px; }
.ctl-val { font-variant-numeric: tabular-nums; font-weight: 600; }
.ctl-slider { position: relative; }
.ctl-slider input, .ctl input[type=range] { width: 100%; margin: 2px 0 0; accent-color: var(--accent); }
.mark { position: absolute; top: 15px; width: 2px; height: 7px; background: var(--accent); transform: translateX(-1px); pointer-events: none; }
.ctl-hint { font-size: 11px; color: var(--muted); }

#results { padding: 16px 26px 80px; min-width: 0; }
section { background: var(--panel); border: 1px solid var(--line); border-radius: 8px; padding: 14px 18px; margin-bottom: 16px; }
section[hidden] { display: none; }
h2 { font-size: 18px; margin: 0 0 10px; display: inline-block; }
h3 { font-size: 13px; font-weight: 600; margin: 6px 0; color: var(--ink2); }
.lede { margin: 10px 0 0; color: var(--ink2); }
.caveat { margin: 8px 0 0; padding: 8px 12px; border-left: 3px solid var(--accent, #c0392b); color: var(--ink2); font-size: 0.92em; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 18px; }
.grid3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 12px; margin-top: 8px; }
@media (max-width: 1100px) { .grid2, .grid3 { grid-template-columns: 1fr; } .layout { grid-template-columns: 260px 1fr; } }
.chartbox { width: 100%; min-height: 60px; }
.legend { font-size: 12px; color: var(--muted); font-weight: normal; }
.legend i, .split-legend i { display: inline-block; width: 10px; height: 10px; margin: 0 3px 0 8px; vertical-align: -1px; border-radius: 2px; }

.cards { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); gap: 10px; }
.card { border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; cursor: pointer; background: var(--panel); }
.card:hover { border-color: var(--accent); }
.card.big { background: var(--hover); }
.card-label { font-size: 12px; color: var(--muted); }
.card-value { font-size: 26px; font-weight: 650; font-variant-numeric: tabular-nums; margin: 2px 0; }
.card.big .card-value { font-size: 34px; color: var(--accent); }
.card-sub { font-size: 11.5px; color: var(--muted); }
.badge { display: inline-block; font-size: 11px; padding: 1px 6px; border-radius: 9px; margin-left: 4px; vertical-align: 2px; }
.badge.ok { background: var(--badge-ok-bg); color: var(--badge-ok); }
.badge.warn { background: var(--badge-warn-bg); color: var(--bad); font-weight: 600; }
.losssplit { margin-top: 14px; }
.split-title { display: inline-block; font-size: 13px; margin-bottom: 4px; }
.split-bar { display: flex; height: 26px; border-radius: 4px; overflow: hidden; background: var(--track); }
.split-bar .seg { color: #fff; font-size: 12px; line-height: 26px; padding-left: 6px; white-space: nowrap; overflow: hidden; }
.split-bar .seg.keep { background: var(--accent); }
.split-legend { font-size: 12px; margin-top: 4px; display: flex; gap: 4px; flex-wrap: wrap; align-items: center; }
.split-legend .muted { margin-left: auto; }

svg.chart { display: block; overflow: visible; }
svg .grid { stroke: var(--grid); }
svg .tick { font-size: 10.5px; fill: var(--tick); }
svg .tick.clickable { fill: var(--ink2); text-decoration: underline dotted; cursor: pointer; }
svg .lab { font-size: 12px; fill: var(--ink); }
svg .lab.clickable { cursor: pointer; text-decoration: underline dotted; }
svg .val { font-size: 11.5px; fill: var(--ink); font-variant-numeric: tabular-nums; }
svg .axis0 { stroke: var(--axis); }
svg .hover { stroke: var(--axis); stroke-dasharray: 2 2; }
svg .connector { stroke: var(--axis); stroke-dasharray: 2 2; }
svg .band { fill: var(--track); }
svg .xband { fill: var(--accent); opacity: 0.1; }
#tip { position: fixed; z-index: 50; display: none; background: var(--tipbg); color: var(--tipink); padding: 6px 9px; border-radius: 5px;
  font-size: 12px; line-height: 1.45; pointer-events: none; max-width: 320px; }

.org-controls { display: flex; gap: 20px; align-items: center; flex-wrap: wrap; margin-bottom: 8px; font-size: 13px; }
.org-controls select { width: auto; }
.org-wrap { display: grid; grid-template-columns: 1fr 320px; gap: 16px; align-items: start; }
@media (max-width: 1100px) { .org-wrap { grid-template-columns: 1fr; } }
#org canvas { image-rendering: pixelated; cursor: crosshair; max-width: 100%; }
#unit-detail h4 { margin: 4px 0 8px; }

table.kv { border-collapse: collapse; width: 100%; font-size: 13px; }
table.kv th { text-align: left; font-weight: 600; color: var(--ink2); border-bottom: 1px solid var(--line); padding: 4px 8px; }
table.kv td { border-bottom: 1px solid var(--line2); padding: 4px 8px; font-variant-numeric: tabular-nums; }
#signals table.kv td:nth-child(2), #signals table.kv td:nth-child(3) { text-align: right; width: 140px; }

#c-ideas { width: 100%; height: 300px; }
.axisnote { font-size: 11.5px; color: var(--muted); text-align: center; }
progress { display: none; width: 220px; vertical-align: middle; accent-color: var(--accent); }
.stale { opacity: 0.45; }
.stale::before { content: "Parameters changed — rerun to update."; display: block; color: var(--bad); font-size: 12px; margin-bottom: 6px; }
button.try { padding: 1px 8px; font-size: 12px; }

.traces { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
@media (max-width: 1100px) { .traces { grid-template-columns: 1fr; } }
.trace { border: 1px solid var(--line); border-radius: 6px; padding: 8px 12px; }
.trace h4 { margin: 2px 0 6px; display: inline-block; }
.trace p { margin: 4px 0; }
.approver { margin: 8px 0; }
.ap-h { font-size: 12.5px; margin-bottom: 3px; }
.ok { color: var(--good); } .bad { color: var(--bad); }
.tb { display: grid; grid-template-columns: 110px 1fr 54px; align-items: center; font-size: 12px; height: 17px; }
.tbl { color: var(--ink2); }
.tbbar { position: relative; height: 10px; background: var(--track); }
.tbbar::after { content: ""; position: absolute; left: 50%; top: -2px; bottom: -2px; width: 1px; background: var(--axis); }
.tbbar span { position: absolute; top: 0; bottom: 0; }
.tbv { text-align: right; font-variant-numeric: tabular-nums; }
ol.tight, ul.tight { padding-left: 20px; margin: 4px 0 10px; }
ol.tight li, ul.tight li { margin-bottom: 4px; }

#drawer { position: fixed; top: 0; right: 0; height: 100vh; width: 420px; max-width: 92vw; background: var(--panel); color: var(--ink); z-index: 40;
  box-shadow: -6px 0 24px var(--shadow); padding: 20px 22px; transform: translateX(105%); transition: transform 0.2s ease; overflow-y: auto; }
#drawer.open { transform: none; }
#drawer-close { position: absolute; top: 10px; right: 12px; font-size: 20px; border: none; background: none; }
#drawer h3 { font-size: 17px; margin-top: 4px; color: var(--ink); }
#drawer .model { background: var(--soft); border-radius: 6px; padding: 8px 10px; margin: 10px 0; font-size: 12.5px; }
#drawer .model code { background: none; padding: 0; }
body.busy #headline .card:not(.big) { opacity: 0.85; }
.busy-banner { display: none; position: sticky; top: 52px; z-index: 10; margin-bottom: 12px; padding: 8px 14px; border-radius: 6px;
  background: var(--hover); border: 1px solid var(--accent); color: var(--ink); font-size: 13px; }
body.busy .busy-banner { display: block; }
.busy-bar { height: 4px; margin-top: 6px; background: var(--track); border-radius: 2px; overflow: hidden; }
.busy-bar > div { height: 100%; width: 0; background: var(--accent); transition: width 0.25s; }
.ov-charts { margin-top: 14px; }
.live-wait { padding: 8px 12px; border: 1px dashed var(--line); border-radius: 6px; }
/* the dot's place is always reserved, so the header does not reflow when a run starts */
header .brand b::after { content: " ●"; color: transparent; font-size: 12px; }
body.busy header .brand b::after { color: var(--accent); animation: pulse 1.2s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: 0.3; } }
.busy-banner::before { content: ""; display: inline-block; width: 10px; height: 10px; margin-right: 8px; border-radius: 50%;
  border: 2px solid var(--accent); border-right-color: transparent; animation: spin 0.8s linear infinite; vertical-align: -1px; }
@keyframes spin { to { transform: rotate(360deg); } }

/* time cursor shared by the over-time charts */
svg .cursor { stroke: var(--accent); stroke-width: 1.5; opacity: 0.8; pointer-events: none; }

/* before-your-change ghosts and the compared scenario */
.ghost-note { font-size: 12px; color: var(--muted); min-height: 0; margin-bottom: 6px; }
.ghost-note a, .tl a, .traces a, #idea-probe a { color: var(--accent); cursor: pointer; text-decoration: underline dotted; }
.ghost-note .gsw { display: inline-block; width: 18px; border-top: 2px dashed #999; vertical-align: 3px; margin-right: 6px; }
.card-ghost { font-size: 11px; color: var(--muted); margin-top: 3px; }
.card-cmp { font-size: 11px; color: #8e44ad; margin-top: 2px; }

/* timelapse */
.tl { outline: none; }
.tl-bar { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; font-size: 13px; margin: 6px 0; }
.tl-bar select { width: auto; }
.tl-bar .tl-play { width: 40px; font-size: 14px; }
.tl-when { font-variant-numeric: tabular-nums; color: var(--ink2); min-width: 150px; }
.tl-filters { display: flex; flex-wrap: wrap; gap: 12px; font-size: 12px; margin-bottom: 6px; align-items: center; }
.tl-under { display: grid; grid-template-columns: 260px 1fr; gap: 20px; align-items: start; margin-top: 8px; }
.tl-canvas-wrap { overflow-x: auto; }
.tl-canvas { display: block; cursor: pointer; }
.tl-scrub { display: block; width: 100%; height: 56px; cursor: ew-resize; margin-top: 6px; border-top: 1px solid var(--line); touch-action: none; }
.tl-side { font-size: 12.5px; max-height: 320px; overflow-y: auto; }
.tl-side h4 { margin: 0 0 4px; }
.tl-led { display: flex; justify-content: space-between; align-items: baseline; font-size: 13px; }
.tl-led b { font-size: 22px; color: var(--accent); }
.tl-tick { padding: 3px 0; border-bottom: 1px solid var(--line2); cursor: pointer; }
.tl-tick:hover { background: var(--hover); }
.tl-why { margin-left: 4px; }
.tl-detail { margin-top: 10px; border-top: 1px solid var(--line); padding-top: 6px; }
.tl-legend { font-size: 11.5px; line-height: 1.5; }
.spark { display: flex; align-items: center; gap: 6px; font-size: 11px; color: var(--muted); }
.spark span { width: 80px; }
.idea-probe { margin-top: 10px; }
@media (max-width: 900px) { .tl-under { grid-template-columns: 1fr; } .tl-side { max-height: none; } }

/* guided tour */
header .tour { font-size: 12.5px; padding: 3px 10px; color: var(--accent); border-color: var(--accent); }
header .plain { font-size: 12px; color: var(--ink2); white-space: nowrap; }
.tourbox { position: fixed; right: 18px; bottom: 18px; z-index: 45; width: 380px; max-width: calc(100vw - 32px); display: none;
  background: var(--panel); border: 1px solid var(--accent); border-radius: 8px; box-shadow: 0 6px 24px var(--shadow); padding: 12px 14px; font-size: 13.5px; line-height: 1.45; }
.tourbox.open { display: block; }
.tour-nav { display: flex; gap: 8px; align-items: center; margin-top: 10px; }
.tour-nav #tour-step { margin-right: auto; }
section.tour-focus { outline: 3px solid var(--accent); outline-offset: 2px; }

/* phones: controls on top, one column, the header scrolls sideways */
@media (max-width: 760px) {
  .layout { grid-template-columns: 1fr; }
  #controls { position: static; height: auto; max-height: none; border-right: none; border-bottom: 1px solid var(--line); }
  header { flex-wrap: nowrap; overflow-x: auto; align-items: center; padding: 6px 10px 0; }
  .tabs { flex-wrap: nowrap; }
  .tabs button { padding: 6px 8px 7px; white-space: nowrap; }
  .head-right { padding-bottom: 6px; gap: 8px; }
  .head-right .brand .muted, header .plain { display: none; }
  #results { padding: 10px 10px 60px; }
  section { padding: 10px 12px; }
  .cards { grid-template-columns: repeat(2, 1fr); }
  .card-value { font-size: 22px; }
  .org-wrap { grid-template-columns: 1fr; }
  .traces { grid-template-columns: 1fr; }
  .busy-banner { top: 44px; }
  #signals table.kv td:nth-child(2), #signals table.kv td:nth-child(3) { width: auto; }
  table.kv { font-size: 12px; }
}
@media (max-width: 1700px) { .head-right .brand .muted { display: none; } }
.tl-under > *, .grid2 > *, .grid3 > * { min-width: 0; }
.analysis .table, #signals, .org-wrap { overflow-x: auto; }
