/* Typing Sprint - dark theme with restrained neon accents.
   All foreground/background pairs are checked against WCAG 2.1 AA (>= 4.5:1
   for body text, >= 3:1 for large text and UI borders). See docs/frontend.md. */

:root {
  --bg: #0b0e14;
  --surface: #121722;
  --surface-2: #1a2130;
  --surface-3: #222b3d;
  /* --border outlines decorative panels. --border-strong is used wherever the
     boundary itself identifies a control, so it clears WCAG 1.4.11 (3:1). */
  --border: #2b3448;
  --border-strong: #6d7b96;

  --text: #e8eefc;
  --text-muted: #9fadc6;
  --text-dim: #7b89a3;

  --accent: #4fe3d1;
  --accent-strong: #7ff0e2;
  --accent-dim: #1e6f66;
  --correct: #6ee89a;
  --error: #ff7b88;
  --error-dim: #7a2a33;

  --font-ui: ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  --font-mono: ui-monospace, SFMono-Regular, "SF Mono", Menlo, Consolas, "Liberation Mono", monospace;

  --shell-max: 68rem;
  --radius: 0.625rem;
  --radius-sm: 0.375rem;

  --passage-font: 1.5rem;
  --passage-line: 2.6rem;
  --passage-lines: 3;

  --space-1: 0.25rem;
  --space-2: 0.5rem;
  --space-3: 0.75rem;
  --space-4: 1rem;
  --space-5: 1.5rem;
  --space-6: 2rem;
  --space-7: 3rem;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html {
  -webkit-text-size-adjust: 100%;
}

body {
  margin: 0;
  min-height: 100dvh;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-ui);
  font-size: 1rem;
  line-height: 1.65;
  -webkit-font-smoothing: antialiased;
}

/* One page-level scrollbar only: no inner scroll containers anywhere. */

h1, h2, h3, h4 {
  line-height: 1.25;
  margin: 0 0 var(--space-3);
  font-weight: 650;
  letter-spacing: -0.01em;
}

h1 { font-size: clamp(1.75rem, 1.2rem + 2.2vw, 2.6rem); }
h2 { font-size: clamp(1.35rem, 1.1rem + 1.1vw, 1.75rem); }
h3 { font-size: 1.125rem; }
h4 { font-size: 1rem; }

p { margin: 0 0 var(--space-4); }

a {
  color: var(--accent);
  text-underline-offset: 0.2em;
}

a:hover { color: var(--accent-strong); }

kbd {
  font-family: var(--font-mono);
  font-size: 0.82em;
  padding: 0.1em 0.4em;
  border: 1px solid var(--border-strong);
  border-bottom-width: 2px;
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  color: var(--text);
  white-space: nowrap;
}

:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 2px;
  border-radius: var(--radius-sm);
}

.visually-hidden,
.sr-status {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
  border: 0;
}

.skip-link {
  position: absolute;
  left: var(--space-4);
  top: -3.5rem;
  z-index: 20;
  padding: var(--space-2) var(--space-4);
  background: var(--surface-2);
  color: var(--text);
  border: 1px solid var(--accent);
  border-radius: var(--radius-sm);
  text-decoration: none;
  transition: top 120ms ease;
}

.skip-link:focus-visible { top: var(--space-3); }

.shell {
  width: 100%;
  max-width: var(--shell-max);
  margin-inline: auto;
  padding-inline: var(--space-5);
}

/* ---------- header ---------- */

.site-header {
  border-bottom: 1px solid var(--border);
  background: color-mix(in srgb, var(--bg) 88%, black);
}

.site-header__inner {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-4);
  align-items: center;
  justify-content: space-between;
  padding-block: var(--space-4);
}

.brand {
  display: inline-flex;
  align-items: center;
  gap: var(--space-2);
  color: var(--text);
  text-decoration: none;
  font-weight: 700;
  letter-spacing: -0.01em;
}

.brand__mark {
  width: 0.75rem;
  height: 1.35rem;
  border-radius: 2px;
  background: var(--accent);
  box-shadow: 0 0 12px color-mix(in srgb, var(--accent) 45%, transparent);
}

.site-nav {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-4);
  font-size: 0.95rem;
}

.site-nav a {
  color: var(--text-muted);
  text-decoration: none;
}

.site-nav a:hover,
.site-nav a:focus-visible { color: var(--accent); }

/* ---------- hero ---------- */

.hero { padding-block: var(--space-6) var(--space-4); }

.intro {
  max-width: 60ch;
  color: var(--text-muted);
  font-size: 1.0625rem;
}

.intro strong { color: var(--text); }

/* ---------- toolbar ---------- */

.app { padding-bottom: var(--space-7); }

.toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-4) var(--space-6);
  padding: var(--space-4);
  margin-bottom: var(--space-4);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.toolbar__group {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: var(--space-2) var(--space-3);
}

.toolbar__label {
  font-size: 0.78rem;
  letter-spacing: 0.09em;
  text-transform: uppercase;
  color: var(--text-dim);
}

.chips {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
}

.chip {
  width: auto;
  min-height: 2.25rem;
  padding: 0.35rem 0.85rem;
  font: inherit;
  font-size: 0.9rem;
  color: var(--text-muted);
  background: var(--surface-2);
  border: 1px solid var(--border-strong);
  border-radius: 999px;
  cursor: pointer;
  transition: background-color 120ms ease, color 120ms ease, border-color 120ms ease;
}

.chip:hover:not(:disabled) {
  color: var(--text);
  border-color: var(--accent);
}

.chip[aria-pressed="true"] {
  color: var(--bg);
  background: var(--accent);
  border-color: var(--accent);
  font-weight: 600;
}

.chip:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

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

.stage {
  padding: var(--space-5);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.stage__status {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: var(--space-3) var(--space-5);
  margin-bottom: var(--space-4);
  min-height: 2.25rem;
}

.stage__timer {
  margin: 0;
  display: flex;
  align-items: baseline;
  gap: 0.35rem;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}

.stage__timer-value {
  font-size: 2rem;
  font-weight: 700;
  line-height: 1;
}

.stage__timer-unit {
  font-size: 0.85rem;
  color: var(--text-dim);
  letter-spacing: 0.05em;
}

.stage__live,
.stage__today {
  margin: 0;
  font-size: 0.9rem;
  color: var(--text-muted);
  font-variant-numeric: tabular-nums;
}

.stage__live-value {
  color: var(--text);
  font-weight: 650;
}

.stage__today { margin-inline-start: auto; }

/* The passage box has a fixed height and scrolls by transform, so finishing a
   line or a run never shifts anything below it. */
.passage {
  position: relative;
  border-radius: var(--radius-sm);
  background: var(--surface-2);
  border: 1px solid var(--border);
  padding: var(--space-4) var(--space-5);
}

.passage__viewport {
  position: relative;
  height: calc(var(--passage-line) * var(--passage-lines));
  overflow: hidden;
}

.passage__words {
  display: flex;
  flex-wrap: wrap;
  align-content: flex-start;
  gap: 0 0.6ch;
  font-family: var(--font-mono);
  font-size: var(--passage-font);
  line-height: var(--passage-line);
  color: var(--text-dim);
  will-change: transform;
  transition: transform 160ms ease;
}

.word { white-space: nowrap; }

.word--active { border-bottom: 2px solid var(--accent-dim); }

.word--bad { text-decoration: underline wavy var(--error-dim); text-underline-offset: 0.3em; }

.ch--correct { color: var(--correct); }
.ch--wrong { color: var(--error); }
.ch--extra { color: var(--error); opacity: 0.75; }

.caret {
  position: absolute;
  top: 0;
  left: 0;
  width: 2px;
  height: calc(var(--passage-line) * 0.8);
  margin-top: calc(var(--passage-line) * 0.1);
  background: var(--accent);
  border-radius: 1px;
  transform: translate(0, 0);
  transition: transform 80ms linear;
  animation: caret-pulse 1.1s ease-in-out infinite;
}

.is-running .caret { animation: none; }

@keyframes caret-pulse {
  0%, 45% { opacity: 1; }
  55%, 100% { opacity: 0.15; }
}

/* The real input sits under the passage, invisible but focusable and reachable
   by assistive tech - never display:none, which would remove it from the tab
   order and break keyboard operation. */
.passage__input {
  position: absolute;
  inset: 0;
  width: 100%;
  max-width: 100%;
  height: 100%;
  opacity: 0;
  border: 0;
  background: transparent;
  color: transparent;
  font: inherit;
  cursor: text;
}

.passage__overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  border-radius: var(--radius-sm);
  background: color-mix(in srgb, var(--surface-2) 82%, transparent);
  backdrop-filter: blur(2px);
  color: var(--text-muted);
  font-size: 0.95rem;
  pointer-events: none;
}

.passage__input:focus + .passage__overlay,
.is-running .passage__overlay { opacity: 0; }

.stage__hint {
  margin: var(--space-3) 0 0;
  font-size: 0.875rem;
  color: var(--text-dim);
}

.stage__actions {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-3);
  margin-top: var(--space-4);
}

.btn {
  width: auto;
  min-height: 2.5rem;
  padding: 0.5rem 1.1rem;
  font: inherit;
  font-size: 0.95rem;
  font-weight: 600;
  color: var(--text);
  background: var(--surface-3);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm);
  cursor: pointer;
  transition: background-color 120ms ease, border-color 120ms ease;
}

.btn:hover:not(:disabled) { border-color: var(--accent); }

.btn--primary {
  color: var(--bg);
  background: var(--accent);
  border-color: var(--accent);
}

.btn--primary:hover:not(:disabled) { background: var(--accent-strong); }

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

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

.results {
  margin-top: var(--space-5);
  padding: var(--space-5);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.results__title {
  margin: 0 0 var(--space-4);
  font-size: 0.78rem;
  letter-spacing: 0.09em;
  text-transform: uppercase;
  color: var(--text-dim);
}

.metrics {
  list-style: none;
  margin: 0 0 var(--space-5);
  padding: 0;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(11rem, 1fr));
  gap: var(--space-3);
}

.metric {
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
  min-height: 6rem;
  padding: var(--space-3) var(--space-4);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}

.metric__label {
  font-size: 0.78rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-dim);
}

.metric__value {
  font-size: 1.75rem;
  font-weight: 700;
  line-height: 1.15;
  font-variant-numeric: tabular-nums;
  color: var(--text);
}

.metric--hero .metric__value { color: var(--accent); }

.metric__note {
  font-size: 0.8rem;
  color: var(--text-muted);
}

.panels {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(19rem, 1fr));
  gap: var(--space-4);
}

.panel {
  padding: var(--space-4);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}

.panel__title {
  margin: 0 0 var(--space-3);
  font-size: 0.78rem;
  letter-spacing: 0.09em;
  text-transform: uppercase;
  color: var(--text-dim);
}

#wpm-chart {
  display: block;
  width: 100%;
  max-width: 100%;
  height: 13.75rem;
  border-radius: var(--radius-sm);
  background: var(--surface);
}

.panel__caption {
  margin: var(--space-3) 0 0;
  font-size: 0.82rem;
  color: var(--text-muted);
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-3);
}

.legend {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
}

.legend__swatch {
  width: 0.8rem;
  height: 0.8rem;
  border-radius: 3px;
  border: 1px solid var(--border-strong);
}

.legend__swatch--none { background: var(--surface-3); }
.legend__swatch--mid { background: #653847; }
.legend__swatch--high { background: #a84652; }

/* Fixed-height heatmap: reserved before the first run so nothing shifts. */
.heatmap {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  min-height: 13.75rem;
  justify-content: center;
}

.heatmap__row {
  display: flex;
  justify-content: center;
  gap: var(--space-2);
}

.heatmap__key {
  flex: 0 0 auto;
  width: 2.1rem;
  height: 2.1rem;
  display: grid;
  place-items: center;
  font-family: var(--font-mono);
  font-size: 0.8rem;
  color: var(--text);
  background: var(--surface-3);
  border: 1px solid var(--border);
  border-radius: var(--radius-sm);
}

.heatmap__key--space {
  width: 10rem;
}

.results__actions {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-3);
  margin-top: var(--space-5);
}

/* ---------- history ---------- */

.history {
  margin-top: var(--space-5);
  padding: var(--space-5);
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
}

.history__title {
  margin: 0 0 var(--space-4);
  font-size: 0.78rem;
  letter-spacing: 0.09em;
  text-transform: uppercase;
  color: var(--text-dim);
}

.bests {
  list-style: none;
  margin: 0 0 var(--space-4);
  padding: 0;
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
}

.bests li {
  padding: 0.3rem 0.7rem;
  font-size: 0.85rem;
  color: var(--text-muted);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 999px;
}

.bests b {
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}

/* Wide tables get a horizontal scroller on narrow screens. This is a genuine
   scroll region (no fixed-height ancestor, no vertical scrolling). */
.history__table-wrap {
  overflow-x: auto; /* layout-check: allow */
  -webkit-overflow-scrolling: touch;
}

.history__table {
  width: 100%;
  min-width: 40rem;
  border-collapse: collapse;
  font-size: 0.875rem;
  font-variant-numeric: tabular-nums;
}

.history__table th,
.history__table td {
  padding: var(--space-2) var(--space-3);
  text-align: left;
  border-bottom: 1px solid var(--border);
  white-space: nowrap;
}

.history__table th {
  color: var(--text-dim);
  font-size: 0.78rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  font-weight: 600;
}

.history__table td { color: var(--text-muted); }

.history__table tbody tr:last-child td { border-bottom: 0; }

.history__empty td {
  color: var(--text-dim);
  font-style: italic;
  white-space: normal;
}

.history__row--best td:first-child::after {
  content: " PB";
  color: var(--accent);
  font-weight: 700;
  font-style: normal;
}

/* ---------- prose sections ---------- */

.prose {
  padding-block: var(--space-6);
  max-width: 100%;
}

.prose > * { max-width: 72ch; }

.prose ol,
.prose ul {
  margin: 0 0 var(--space-4);
  padding-left: 1.35rem;
  color: var(--text-muted);
}

.prose li { margin-bottom: var(--space-2); }

.prose li strong { color: var(--text); }

.prose h3 {
  margin-top: var(--space-5);
  color: var(--text);
}

.prose p { color: var(--text-muted); }

/* ---------- footer ---------- */

.site-footer {
  margin-top: var(--space-6);
  padding-block: var(--space-5);
  border-top: 1px solid var(--border);
  color: var(--text-muted);
  font-size: 0.9rem;
}

.site-footer p { margin: 0 0 var(--space-2); }

.site-footer__links {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-4);
}

/* ---------- motion ---------- */

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
    scroll-behavior: auto !important;
  }
}

@media (max-width: 40rem) {
  :root {
    --passage-font: 1.25rem;
    --passage-line: 2.2rem;
  }

  .shell { padding-inline: var(--space-4); }

  .stage__today { margin-inline-start: 0; }

  .heatmap__key {
    width: 1.7rem;
    height: 1.7rem;
    font-size: 0.7rem;
  }

  .heatmap__key--space { width: 8rem; }
}
