/* Dark by default, light-aware. Tabular numerals everywhere a number can change,
   so digits do not jitter as they update. */

:root {
  --bg: #0e1116;
  --bg-raised: #161b23;
  --bg-sunken: #0a0d12;
  --line: #262d38;
  --line-soft: #1c222c;
  --fg: #e8edf5;
  --fg-dim: #97a3b6;
  --fg-faint: #66718a;
  --accent: #5aa9ff;
  --red: #e5484d;
  --red-deep: #8c1c20;
  --green: #26b06b;
  --green-deep: #0f6b3f;
  --amber: #e0a33a;
  --radius: 12px;
  --mono: ui-monospace, "SF Mono", "Cascadia Mono", "JetBrains Mono", Consolas, monospace;
  --sans: system-ui, -apple-system, "Segoe UI", Inter, Roboto, sans-serif;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #f6f8fb;
    --bg-raised: #ffffff;
    --bg-sunken: #eceff4;
    --line: #d8dee8;
    --line-soft: #e6eaf1;
    --fg: #141922;
    --fg-dim: #566072;
    --fg-faint: #8a94a6;
    --accent: #1f6feb;
    --red: #d92b30;
    --red-deep: #b3181d;
    --green: #1a9556;
    --green-deep: #107a44;
    --amber: #a86c07;
  }
}

* { box-sizing: border-box; }

body {
  margin: 0;
  padding: 0 1rem 3rem;
  background: var(--bg);
  color: var(--fg);
  font-family: var(--sans);
  font-size: 16px;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  /* Rapid tapping must never select text or trigger double-tap zoom. */
  touch-action: manipulation;
}

h1, h2 { line-height: 1.2; margin: 0; }

.skip-link {
  position: absolute; left: -9999px; top: 0; padding: .6rem 1rem;
  background: var(--accent); color: #fff; z-index: 10; border-radius: 0 0 8px 0;
}
.skip-link:focus { left: 0; }

/* ---- head ---------------------------------------------------------------- */

.site-head { max-width: 780px; margin: 0 auto; padding: 2.2rem 0 1.2rem; text-align: center; }
.site-head h1 { font-size: clamp(1.6rem, 4vw, 2.2rem); letter-spacing: -0.02em; }
.tagline { color: var(--fg-dim); margin: .5rem 0 1rem; }

.banner {
  display: inline-block; margin: 0; padding: .5rem .85rem;
  border: 1px solid var(--line); border-radius: 999px;
  background: var(--bg-raised); color: var(--fg-dim);
  font-size: .8rem; font-family: var(--mono); font-variant-numeric: tabular-nums;
  text-align: left;
}
.banner.is-ok { border-color: color-mix(in srgb, var(--green) 45%, var(--line)); }
.banner.is-warn { border-color: color-mix(in srgb, var(--amber) 55%, var(--line)); color: var(--fg); }

/* ---- tabs ---------------------------------------------------------------- */

.tabs {
  display: flex; gap: .4rem; justify-content: center; flex-wrap: wrap;
  max-width: 780px; margin: 0 auto 1.6rem;
}
.tab {
  display: inline-flex; align-items: center; gap: .5rem;
  padding: .6rem 1.1rem; border: 1px solid var(--line); border-radius: 999px;
  background: var(--bg-raised); color: var(--fg-dim);
  font: inherit; font-size: .95rem; cursor: pointer;
}
.tab:hover { color: var(--fg); border-color: var(--fg-faint); }
.tab.is-active { background: var(--accent); border-color: var(--accent); color: #fff; }
.tab-key {
  font-family: var(--mono); font-size: .7rem; opacity: .6;
  border: 1px solid currentColor; border-radius: 4px; padding: 0 .3rem;
}

/* ---- panels -------------------------------------------------------------- */

main { max-width: 780px; margin: 0 auto; }
.panel[hidden] { display: none; }
.panel-head { margin-bottom: 1.2rem; }
.panel-head h2 { font-size: 1.35rem; letter-spacing: -0.01em; }
.lede { color: var(--fg-dim); margin: .4rem 0 0; }

/* ---- stage --------------------------------------------------------------- */

.stage {
  position: relative;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: .5rem;
  width: 100%; min-height: 340px; padding: 2rem 1rem;
  border: 1px solid var(--line); border-radius: var(--radius);
  background: var(--bg-raised); color: var(--fg);
  font: inherit; text-align: center; cursor: pointer;
  user-select: none; -webkit-user-select: none;
  /* No transition, ever: a fade would smear the red→green onset and inflate
     every reaction time measured against it. */
  transition: none;
}
.stage:focus-visible { outline: 2px solid var(--accent); outline-offset: 3px; }

.stage-title {
  font-family: var(--mono); font-variant-numeric: tabular-nums;
  font-size: clamp(2rem, 9vw, 3.6rem); font-weight: 600; letter-spacing: -0.02em;
}
.stage-sub { color: var(--fg-dim); font-size: .95rem; max-width: 34ch; }

/* The display size exists for numbers. Phases whose title is a sentence get a
   readable heading size instead. */
.stage[data-phase="idle"] .stage-title,
.stage[data-phase="waiting"] .stage-title,
.stage[data-phase="running"] .stage-title,
.stage[data-phase="false"] .stage-title,
.stage[data-phase="void"] .stage-title {
  font-family: var(--sans); font-size: clamp(1.25rem, 3.4vw, 1.75rem);
  font-weight: 600; letter-spacing: -0.01em; max-width: 22ch;
}

.stage[data-phase="waiting"] { background: var(--red-deep); border-color: var(--red); color: #fff; }
.stage[data-phase="waiting"] .stage-sub { color: #ffd7d8; }
.stage[data-phase="go"] { background: var(--green); border-color: var(--green); color: #fff; }
.stage[data-phase="false"] { background: var(--red); border-color: var(--red); color: #fff; }
.stage[data-phase="false"] .stage-sub { color: #ffe6e6; }
.stage[data-phase="void"] { background: var(--red-deep); border-color: var(--red); color: #fff; }
.stage[data-phase="void"] .stage-sub { color: #ffd7d8; }
.stage[data-phase="running"] { background: var(--bg-sunken); }
.stage[data-phase="capture"] { background: var(--bg-sunken); border-color: var(--accent); }
.stage[data-phase="leadin"] { background: var(--bg-sunken); }
/* Once a result is on screen the real readout is below; the stage is only the
   affordance to run again, so it stops dominating the page. */
.stage[data-phase="result"] {
  border-color: color-mix(in srgb, var(--accent) 50%, var(--line));
  min-height: 150px;
}
.stage[data-phase="result"] .stage-title { font-size: clamp(1.5rem, 5vw, 2.1rem); }

/* Metronome flash. Cosmetic only — the audio clock is authoritative. */
.stage-pulse {
  position: absolute; inset: 0; border-radius: inherit;
  background: var(--accent); opacity: 0; pointer-events: none;
}
.stage-pulse.is-on { opacity: .18; }
.stage-pulse.is-on.is-accent { opacity: .32; }

@media (prefers-reduced-motion: reduce) {
  .stage-pulse { display: none; }
}

/* ---- hud + controls ------------------------------------------------------ */

.hud {
  display: flex; gap: 1rem; justify-content: space-between;
  margin-bottom: .6rem; font-family: var(--mono); font-size: .85rem;
  font-variant-numeric: tabular-nums; color: var(--fg-dim);
}
.hud-item.is-warn { color: var(--amber); }

.controls {
  display: flex; flex-direction: column; gap: .9rem;
  padding: 1rem; margin-bottom: 1rem;
  border: 1px solid var(--line); border-radius: var(--radius); background: var(--bg-raised);
}
.control-row { display: flex; align-items: center; gap: .8rem; flex-wrap: wrap; }
.control-label { color: var(--fg-dim); font-size: .9rem; min-width: 8.5rem; }
.range { flex: 1 1 180px; accent-color: var(--accent); }
.range-value {
  font-family: var(--mono); font-variant-numeric: tabular-nums;
  min-width: 2.5ch; text-align: right;
}
.num-input {
  width: 6.5rem; padding: .4rem .55rem;
  border: 1px solid var(--line); border-radius: 8px;
  background: var(--bg-sunken); color: var(--fg);
  font-family: var(--mono); font-size: .95rem; font-variant-numeric: tabular-nums;
}
.num-input:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.num-input::placeholder { color: var(--fg-faint); }
.input-unit { color: var(--fg-faint); font-family: var(--mono); font-size: .85rem; margin-left: -.4rem; }

.switch { display: inline-flex; align-items: center; gap: .55rem; color: var(--fg-dim); font-size: .9rem; cursor: pointer; }
.switch input { accent-color: var(--accent); width: 1.05rem; height: 1.05rem; }
.control-hint { margin: 0; color: var(--fg-faint); font-size: .82rem; }

.tempo-row { display: grid; grid-template-columns: repeat(2, 1fr); gap: .6rem; }
.tempo-btn {
  display: flex; flex-direction: column; align-items: center; gap: .15rem;
  padding: .9rem .5rem; border: 1px solid var(--line); border-radius: 10px;
  background: var(--bg-sunken); color: var(--fg); font: inherit; cursor: pointer;
}
.tempo-btn:hover { border-color: var(--accent); }
.tempo-btn.is-active { background: var(--accent); border-color: var(--accent); color: #fff; }
.tempo-ms { font-family: var(--mono); font-size: 1.15rem; font-weight: 600; font-variant-numeric: tabular-nums; }
.tempo-bpm { font-size: .75rem; color: var(--fg-faint); }
.tempo-btn.is-active .tempo-bpm { color: #dbeaff; }

/* ---- results ------------------------------------------------------------- */

.results:empty { display: none; }
.results { margin-top: 1.4rem; display: flex; flex-direction: column; gap: 1.2rem; }

.headline { text-align: center; }
.headline-value {
  display: flex; align-items: baseline; justify-content: center; gap: .35rem;
  font-family: var(--mono); font-variant-numeric: tabular-nums;
}
.headline-number { font-size: clamp(2.6rem, 12vw, 4.2rem); font-weight: 700; letter-spacing: -0.03em; }
.headline-unit { font-size: 1.2rem; color: var(--fg-dim); }
.headline-caption { color: var(--fg-dim); font-size: .92rem; }

.metrics {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: .6rem; margin: 0;
}
.metric {
  padding: .75rem .85rem; border: 1px solid var(--line-soft);
  border-radius: 10px; background: var(--bg-raised);
}
.metric dt { color: var(--fg-dim); font-size: .78rem; text-transform: uppercase; letter-spacing: .04em; }
.metric dd {
  margin: .2rem 0 0; font-family: var(--mono); font-variant-numeric: tabular-nums;
  font-size: 1.15rem; font-weight: 600;
}
.metric-hint { margin: .3rem 0 0; color: var(--fg-faint); font-size: .76rem; line-height: 1.35; }
.metric.is-primary { border-color: var(--accent); }
.metric.is-primary dd { color: var(--accent); }
.metric.is-good dd, .metric.is-excellent dd { color: var(--green); }
.metric.is-average dd { color: var(--amber); }
.metric.is-poor dd { color: var(--red); }

.notice {
  padding: .75rem .9rem; border-radius: 10px; font-size: .9rem;
  border: 1px solid var(--line);
}
.notice.is-error { border-color: var(--red); background: color-mix(in srgb, var(--red) 12%, var(--bg-raised)); }
.notice.is-warn { border-color: var(--amber); background: color-mix(in srgb, var(--amber) 12%, var(--bg-raised)); }

.link-btn {
  background: none; border: none; padding: 0; font: inherit;
  color: var(--accent); text-decoration: underline; cursor: pointer;
}

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

.chart { width: 100%; height: auto; display: block; }
.chart-caption { margin: 0; color: var(--fg-faint); font-size: .82rem; }
.chart-zero { stroke: var(--fg-faint); stroke-width: 1; stroke-dasharray: 3 3; }
.chart-mean { stroke: var(--accent); stroke-width: 1.2; stroke-dasharray: 4 3; }
.chart-band { fill: var(--accent); opacity: .12; }
.chart-bar { fill: var(--accent); }
.chart-bar.is-late { fill: var(--amber); }
.chart-bar.is-early { fill: var(--accent); }
.chart-bar.is-trial { fill: var(--accent); opacity: .85; }
.chart-label {
  fill: var(--fg-faint); font-family: var(--mono); font-size: 11px;
  font-variant-numeric: tabular-nums;
}
.chart-label.is-zero { fill: var(--fg-dim); }
.chart-label.is-value { fill: var(--fg-dim); font-size: 11px; }

.trend { display: flex; align-items: center; gap: .8rem; flex-wrap: wrap; }
.trend-label { color: var(--fg-faint); font-size: .82rem; }
.sparkline { width: 160px; height: 34px; }
.sparkline-line { fill: none; stroke: var(--accent); stroke-width: 1.6; stroke-linejoin: round; }
.sparkline-dot { fill: var(--accent); }

/* ---- tables -------------------------------------------------------------- */

.raw-details > summary {
  cursor: pointer; color: var(--fg-dim); font-size: .9rem; padding: .4rem 0;
}
.data-table { width: 100%; border-collapse: collapse; font-size: .88rem; margin-top: .5rem; }
.data-table th {
  text-align: left; color: var(--fg-faint); font-weight: 500; font-size: .76rem;
  text-transform: uppercase; letter-spacing: .04em;
  border-bottom: 1px solid var(--line); padding: .4rem .5rem;
}
.data-table td { padding: .35rem .5rem; border-bottom: 1px solid var(--line-soft); }
.data-table td.num { font-family: var(--mono); font-variant-numeric: tabular-nums; text-align: right; }
.data-table td.is-late { color: var(--amber); }
.data-table td.is-early { color: var(--accent); }
.data-table td.tag { color: var(--fg-faint); font-size: .78rem; }

/* ---- foot ---------------------------------------------------------------- */

.site-foot {
  max-width: 780px; margin: 3rem auto 0; padding-top: 1.2rem;
  border-top: 1px solid var(--line); color: var(--fg-faint); font-size: .85rem;
}
.foot-row { display: flex; justify-content: space-between; align-items: center; gap: 1rem; flex-wrap: wrap; }
.foot-actions { display: flex; gap: .5rem; }
.run-count { font-family: var(--mono); font-variant-numeric: tabular-nums; }
.ghost-btn {
  padding: .35rem .8rem; border: 1px solid var(--line); border-radius: 999px;
  background: none; color: var(--fg-dim); font: inherit; font-size: .82rem; cursor: pointer;
}
.ghost-btn:hover { color: var(--fg); border-color: var(--fg-faint); }
.disclaimer { line-height: 1.5; }
.disclaimer strong { color: var(--fg-dim); }
.shortcuts { display: flex; gap: .3rem; align-items: center; flex-wrap: wrap; }
kbd {
  font-family: var(--mono); font-size: .75rem; padding: .1rem .35rem;
  border: 1px solid var(--line); border-radius: 4px; background: var(--bg-raised);
}

@media (max-width: 520px) {
  .stage { min-height: 300px; }
  .control-label { min-width: 100%; }
  .metrics { grid-template-columns: repeat(auto-fit, minmax(140px, 1fr)); }
}
