:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --wash: rgba(11, 11, 11, 0.04);
  --accent: #2a78d6;

  /* Track states. Order validated for colour-blind separation: do not reshuffle. */
  --st-charge: #2a78d6;
  --st-test: #eda100;
  --st-approved: #1baf7a;
  --st-brakes: #eb6834;
  --st-release: #6250d6;
  --st-vent: #898781;
  --st-problem: #d03b3b;
  --st-idle: #c3c2b7;
  --problem-wash: rgba(208, 59, 59, 0.08);
  --ready-wash: rgba(27, 175, 122, 0.14);
}

/* Dark values are listed twice: once for the OS setting, once for the theme button. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --ink: #f0efec;
    --ink-2: #c3c2b7;
    --muted: #898781;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.10);
    --wash: rgba(255, 255, 255, 0.05);
    --accent: #3987e5;

    --st-charge: #3987e5;
    --st-test: #c98500;
    --st-approved: #199e70;
    --st-brakes: #d95926;
    --st-release: #9085e9;
    --st-vent: #898781;
    --st-problem: #d03b3b;
    --st-idle: #383835;
    --problem-wash: rgba(208, 59, 59, 0.16);
    --ready-wash: rgba(25, 158, 112, 0.20);
  }
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d;
  --surface: #1a1a19;
  --ink: #f0efec;
  --ink-2: #c3c2b7;
  --muted: #898781;
  --grid: #2c2c2a;
  --axis: #383835;
  --border: rgba(255, 255, 255, 0.10);
  --wash: rgba(255, 255, 255, 0.05);
  --accent: #3987e5;

  --st-charge: #3987e5;
  --st-test: #c98500;
  --st-approved: #199e70;
  --st-brakes: #d95926;
  --st-release: #9085e9;
  --st-vent: #898781;
  --st-problem: #d03b3b;
  --st-idle: #383835;
  --problem-wash: rgba(208, 59, 59, 0.16);
  --ready-wash: rgba(25, 158, 112, 0.20);
}

* { box-sizing: border-box; }

body {
  margin: 0;
  background: var(--page);
  color: var(--ink);
  font: 14px/1.45 system-ui, -apple-system, "Segoe UI", sans-serif;
}

h1, h2, p, ol, figure { margin: 0; }
ol { padding: 0; list-style: none; }
h1 { font-size: 18px; font-weight: 650; }
h2 { font-size: 14px; font-weight: 650; }
code { font: 12px/1.4 ui-monospace, "SF Mono", Menlo, monospace; }
.muted { color: var(--muted); }
.small { font-size: 12px; }

/* ---------- header and replay controls ---------- */

.top {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  gap: 16px;
  flex-wrap: wrap;
  padding: 16px 20px 10px;
}
.top-title p { color: var(--ink-2); font-size: 13px; }
.clock { display: flex; align-items: baseline; gap: 10px; font-variant-numeric: tabular-nums; }
#clock-date { color: var(--ink-2); }
#clock-time { font-size: 26px; font-weight: 650; letter-spacing: -0.01em; }

.pill {
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  padding: 2px 8px;
  border-radius: 999px;
  border: 1px solid var(--border);
  color: var(--ink-2);
}

.pill.is-live { color: var(--ink); border-color: var(--st-approved); }
.pill.is-live::before, .pill.is-down::before {
  content: "";
  display: inline-block;
  width: 7px;
  height: 7px;
  margin-right: 6px;
  border-radius: 50%;
  background: var(--st-approved);
}
.pill.is-down { border-color: var(--st-problem); }
.pill.is-down::before { background: var(--st-problem); }

.transport {
  position: sticky;
  top: 0;
  z-index: 5;
  display: flex;
  align-items: center;
  gap: 12px;
  flex-wrap: wrap;
  padding: 8px 20px;
  background: var(--surface);
  border-block: 1px solid var(--border);
}
.scrub { flex: 1 1 320px; display: flex; align-items: center; gap: 8px; font-size: 12px; }
.scrub input { flex: 1; min-width: 120px; accent-color: var(--accent); }
.field { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--ink-2); }

.btn, select, .seg-control button {
  font: inherit;
  font-size: 13px;
  color: var(--ink);
  background: var(--surface);
  border: 1px solid var(--axis);
  border-radius: 6px;
  padding: 5px 12px;
  cursor: pointer;
}
.btn:hover, .seg-control button:hover { background: var(--wash); }
.btn.primary { min-width: 76px; background: var(--accent); border-color: var(--accent); color: #fff; font-weight: 600; }
.btn.primary:hover { background: var(--accent); filter: brightness(1.08); }
.btn.small { padding: 2px 8px; font-size: 12px; }
select { padding: 5px 6px; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

.seg-control { display: inline-flex; }
.seg-control button { border-radius: 0; margin-left: -1px; padding: 3px 10px; font-size: 12px; }
.seg-control button:first-child { border-radius: 6px 0 0 6px; margin-left: 0; }
.seg-control button:last-child { border-radius: 0 6px 6px 0; }
.seg-control button.on { background: var(--ink); border-color: var(--ink); color: var(--surface); }

/* ---------- layout ---------- */

main { padding: 16px 20px 40px; display: grid; gap: 16px; max-width: 1600px; margin: 0 auto; }
.columns { display: grid; grid-template-columns: minmax(0, 1fr) 340px; gap: 16px; align-items: start; }
.col-main, .col-side { display: grid; gap: 16px; min-width: 0; }
@media (max-width: 1000px) { .columns { grid-template-columns: minmax(0, 1fr); } }

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px 16px 16px;
  min-width: 0;
}
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; margin-bottom: 12px; }

/* ---------- stat tiles ---------- */

.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: 12px; }
.kpi {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px 14px 12px;
}
.card .kpi { background: transparent; }
.kpi-label { color: var(--ink-2); font-size: 12px; }
.kpi-value { font-size: 26px; font-weight: 650; line-height: 1.2; }
.kpi-note { color: var(--muted); font-size: 12px; }

/* ---------- track board ---------- */

.board { display: grid; grid-template-columns: repeat(auto-fill, minmax(168px, 1fr)); gap: 10px; }

.tile {
  --c: var(--st-idle);
  position: relative;
  display: grid;
  gap: 2px;
  text-align: left;
  font: inherit;
  color: inherit;
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 5px solid var(--c);
  border-radius: 8px;
  padding: 8px 10px 9px 11px;
  min-height: 104px;
  cursor: pointer;
}
.tile:hover { background: var(--wash); }
.tile.selected { outline: 2px solid var(--accent); outline-offset: 1px; }
.tile[data-group="idle"] { color: var(--muted); }
.tile[data-group="problem"] { background: var(--problem-wash); }
.tile.is-ready { background: var(--ready-wash); }

[data-group="charge"]   { --c: var(--st-charge); }
[data-group="test"]     { --c: var(--st-test); }
[data-group="approved"] { --c: var(--st-approved); }
[data-group="brakes"]   { --c: var(--st-brakes); }
[data-group="release"]  { --c: var(--st-release); }
[data-group="vent"]     { --c: var(--st-vent); }
[data-group="problem"]  { --c: var(--st-problem); }
[data-group="idle"]     { --c: var(--st-idle); }

.tile-head { display: flex; justify-content: space-between; align-items: center; }
.tile-no { font-size: 22px; font-weight: 650; line-height: 1.1; color: var(--ink); }
.tile[data-group="idle"] .tile-no { color: var(--ink-2); }
.tile-no small { font-size: 11px; font-weight: 500; color: var(--muted); margin-right: 4px; }
.tile-state { font-weight: 600; }
.tile-meta, .tile-test { font-size: 12px; color: var(--ink-2); font-variant-numeric: tabular-nums; }
.tile[data-group="idle"] .tile-meta { color: var(--muted); }

.badge {
  font-size: 11px;
  font-weight: 650;
  padding: 1px 7px;
  border-radius: 999px;
  background: var(--st-approved);
  color: #fff;
}

/* ---------- timeline ---------- */

.timeline { width: 100%; cursor: crosshair; }
.timeline svg { display: block; width: 100%; }
.timeline text { font-size: 11px; fill: var(--muted); font-variant-numeric: tabular-nums; }
.timeline .row-label { fill: var(--ink); font-weight: 600; text-decoration: underline; pointer-events: none; }
.timeline .row-label.on { fill: var(--accent); }
.timeline .row-hit { cursor: pointer; }
.timeline .row-hit:hover { fill: var(--wash); }
.timeline .gridline { stroke: var(--grid); stroke-width: 1; }
.timeline .rowline { stroke: var(--grid); stroke-width: 1; }
.timeline .seg { fill: var(--c); }
.timeline .seg:hover { filter: brightness(1.12); }
.timeline .now { stroke: var(--ink); stroke-width: 1.5; }
.timeline .row-hit { fill: transparent; }
.timeline .row-selected { fill: var(--wash); }

.legend { display: flex; flex-wrap: wrap; gap: 4px 14px; margin: 10px 0 6px; font-size: 12px; color: var(--ink-2); }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.legend i { width: 10px; height: 10px; border-radius: 2px; background: var(--c); }

/* ---------- exceptions feed and track log ---------- */

.feed, .log { display: grid; gap: 2px; max-height: 420px; overflow: auto; }
.log { max-height: 640px; }
.feed li button {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 0 10px;
  width: 100%;
  text-align: left;
  font: inherit;
  color: inherit;
  background: none;
  border: 0;
  border-left: 3px solid var(--st-problem);
  border-radius: 0 6px 6px 0;
  padding: 5px 8px;
  cursor: pointer;
}
.feed li button:hover { background: var(--wash); }
.feed .when { grid-row: span 2; color: var(--ink-2); font-size: 12px; font-variant-numeric: tabular-nums; white-space: nowrap; }
.feed .what { font-weight: 600; }
.feed .where { font-size: 12px; color: var(--ink-2); }
.feed-empty, .log-empty { color: var(--muted); padding: 6px 0; }

.log li { --c: var(--st-idle); border-left: 3px solid var(--c); padding: 4px 0 4px 8px; }
.log .line { display: grid; grid-template-columns: auto 1fr; gap: 0 10px; align-items: baseline; }
.log .extra { grid-column: 2; }
.log .when { color: var(--ink-2); font-size: 12px; font-variant-numeric: tabular-nums; white-space: nowrap; }
.log .what { font-weight: 600; }
.log .extra { font-size: 12px; color: var(--ink-2); }
.log details { margin-top: 2px; }
.log code { display: block; color: var(--muted); font-size: 11px; word-break: break-all; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 3px 10px; margin: 6px 0 8px; font-size: 12px; }
.kv dt { color: var(--muted); white-space: nowrap; }
.kv dd { margin: 0; min-width: 0; }
.msg { margin: 0 0 6px; padding: 6px 8px; background: var(--wash); border-radius: 6px; font-size: 12px; }
.msg-head { display: flex; align-items: center; gap: 8px; }
.msg-title { color: var(--muted); }
.msg-value { font-size: 13px; font-weight: 600; margin-top: 2px; }
.msg pre { background: var(--surface); }
.link { font: inherit; color: var(--accent); background: none; border: 0; padding: 0; text-align: left; text-decoration: underline; cursor: pointer; }
.log pre .link { font: inherit; word-break: break-all; }
.raw { margin: 0 0 8px; padding: 6px 8px; border: 1px solid var(--axis); border-left: 3px solid var(--accent); border-radius: 6px; font-size: 12px; }
.raw-title { font-weight: 600; }
.raw .kv { margin: 4px 0; }
.raw-value dd { font-weight: 600; }
.raw p { margin: 4px 0 0; }
.chip { font: 11px/1.4 ui-monospace, "SF Mono", Menlo, monospace; padding: 0 6px; border: 1px solid var(--axis); border-radius: 999px; color: var(--ink-2); }
.log summary { font-size: 12px; color: var(--muted); cursor: pointer; }
.log pre {
  margin: 4px 0 2px;
  padding: 8px;
  max-height: 260px;
  overflow: auto;
  background: var(--wash);
  border-radius: 6px;
  font: 11px/1.45 ui-monospace, "SF Mono", Menlo, monospace;
  white-space: pre-wrap;
  word-break: break-word;
}

/* ---------- charts ---------- */

.charts { display: grid; grid-template-columns: repeat(auto-fit, minmax(320px, 1fr)); gap: 24px; margin-top: 18px; }
figcaption { font-weight: 600; margin-bottom: 6px; }
.chart svg { display: block; width: 100%; }
.chart text { font-size: 11px; fill: var(--muted); font-variant-numeric: tabular-nums; }
.chart .gridline { stroke: var(--grid); stroke-width: 1; }
.chart .baseline { stroke: var(--axis); stroke-width: 1; }
.chart .bar { fill: var(--c); }
.chart .hit { fill: transparent; }
.chart .hit:hover { fill: var(--wash); }

/* ---------- tooltip, empty state ---------- */

.tip {
  position: fixed;
  z-index: 20;
  pointer-events: none;
  max-width: 280px;
  background: var(--ink);
  color: var(--surface);
  border-radius: 6px;
  padding: 6px 9px;
  font-size: 12px;
  line-height: 1.4;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.25);
}
.tip b { display: block; }

body.booting main, body.booting .transport { display: none; }
.empty .btn { margin-top: 14px; }

.empty { max-width: 520px; margin: 80px auto; text-align: center; color: var(--ink-2); }
.empty h2 { font-size: 18px; margin-bottom: 8px; color: var(--ink); }

#detail-card { scroll-margin-top: 64px; } /* clear the sticky replay bar */
