/* RaceDash — layout + theme tokens (light/dark from the reference palette) */
/* Palette switches by attribute, never light-dark(): charts.js reads these
   tokens with getComputedStyle, and an unregistered custom property hands
   back the literal "light-dark(...)" string, which the chart engine cannot
   parse. Same reason there is no color-mix()/oklch() in any token below. */
:root, :root[data-theme="light"] {
  color-scheme: light;
  --surface-1: #fcfcfb;
  --page: #f9f9f7;
  --ink-primary: #0b0b0b;
  --ink-secondary: #52514e;
  --ink-muted: #6e6c66;          /* 4.98:1 on --page (was #898781 at 3.41) */
  --gridline: #e1e0d9;
  --baseline: #c3c2b7;           /* chart hairlines only */
  --control-border: #8e8c85;     /* 3.28:1 — interactive borders */
  --border: rgba(11, 11, 11, 0.10);
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --series-4: #eda100;
  --series-5: #e87ba4;
  --series-6: #008300;
  --series-7: #4a3aa7;
  --series-8: #e34948;
  --accent: #2670cc;             /* 4.92:1 white-on (was #2a78d6 at 4.42) */
  --danger: #c62f2f;
  --ok: #0a7a0a;
  --appbar-h: 52px;
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --surface-1: #1a1a19;
  --page: #0d0d0d;
  --ink-primary: #ffffff;
  --ink-secondary: #c3c2b7;
  --ink-muted: #a3a19a;
  --gridline: #2c2c2a;
  --baseline: #383835;
  --control-border: #6b6b64;
  --border: rgba(255, 255, 255, 0.10);
  --series-1: #3987e5;
  --series-2: #d95926;
  --series-3: #199e70;
  --series-4: #c98500;
  --series-5: #d55181;
  --series-6: #4caf50;
  --series-7: #9085e9;
  --series-8: #e66767;
  --accent: #3987e5;
  --danger: #e57b7b;
  --ok: #4caf50;
}

* { box-sizing: border-box; }

/* The UA sheet's [hidden]{display:none} is in the user-agent origin, so ANY
   author display declaration beats it regardless of specificity. Without this,
   a rule like .password-form{display:flex} silently un-hides an element that
   markup says is hidden. */
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body {
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--page);
  color: var(--ink-primary);
  font-size: 14px;
}

/* --- app bar (the only persistent chrome) --- */
.appbar {
  position: sticky;
  top: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  gap: 10px;
  height: calc(var(--appbar-h) + env(safe-area-inset-top));
  padding: env(safe-area-inset-top) 14px 0;
  background: var(--surface-1);
  border-bottom: 1px solid var(--border);
}
.appbar .brand { font-size: 16px; font-weight: 650; margin: 0; }
.current-race {
  color: var(--ink-secondary);
  font-size: 13px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  min-width: 0;
}
.settings-btn { margin-left: auto; display: flex; align-items: center; gap: 6px; flex-shrink: 0; }
.icon-btn {
  font: inherit;
  font-size: 18px;
  line-height: 1;
  min-width: 40px;
  min-height: 40px;
  border: none;
  border-radius: 8px;
  background: none;
  color: var(--ink-primary);
  cursor: pointer;
}
.icon-btn:hover { background: color-mix(in oklab, var(--accent) 12%, transparent); }
.nav-only { display: none; }

.skip-link {
  position: absolute;
  left: -9999px;
  top: 0;
  background: var(--surface-1);
  padding: 10px 14px;
  z-index: 100;
}
.skip-link:focus { left: 8px; top: 8px; }

.layout { display: flex; align-items: flex-start; }

.sidebar {
  width: 250px;
  flex-shrink: 0;
  padding: 16px;
  border-right: 1px solid var(--border);
  background: var(--surface-1);
  position: sticky;
  top: calc(var(--appbar-h) + env(safe-area-inset-top));
  height: calc(100svh - var(--appbar-h) - env(safe-area-inset-top));
  overflow-y: auto;
}
.scrim { display: none; }
.panel { margin-bottom: 22px; }
.panel h2 { font-size: 12px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--ink-muted); margin: 0 0 8px; }

.race-list { list-style: none; margin: 0; padding: 0; }
.race-list li {
  padding: 7px 10px;
  border-radius: 6px;
  cursor: pointer;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  color: var(--ink-secondary);
}
.race-list li:hover { background: color-mix(in oklab, var(--accent) 10%, transparent); }
.race-list li.active { background: color-mix(in oklab, var(--accent) 16%, transparent); color: var(--ink-primary); font-weight: 600; }
.race-list li .race-progress {
  display: block;
  font-size: 11px;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}
.race-list li .race-progress:empty { display: none; }

.hint { color: var(--ink-muted); font-size: 12px; margin: 8px 0 0; }
.hint code { font-size: 11px; }

/* --- auth pages --- */
.auth-body { display: flex; align-items: center; justify-content: center; min-height: 100vh; }
.auth-card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 28px 30px;
  width: min(380px, calc(100vw - 32px));
}
.auth-card .brand { margin: 0 0 4px; font-size: 20px; }
.auth-sub { color: var(--ink-secondary); margin: 0 0 20px; font-size: 13px; }
.auth-field { display: block; margin-bottom: 14px; }
.auth-field span { display: block; font-size: 12px; color: var(--ink-secondary); margin-bottom: 4px; }
.auth-field input {
  width: 100%;
  padding: 8px 10px;
  border: 1px solid var(--baseline);
  border-radius: 7px;
  background: var(--page);
  color: var(--ink-primary);
  font: inherit;
}
.auth-field input:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.auth-error { color: #d03b3b; font-size: 12px; margin: 8px 0; }
.btn-primary { background: var(--accent); color: #fff; border-color: var(--accent); width: 100%; padding: 9px; }
.btn-primary:disabled { opacity: 0.6; cursor: default; }
.btn-small { font-size: 12px; padding: 4px 8px; }

.user-name { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }

/* --- settings sheet --- */
.sheet {
  border: none;
  padding: 0;
  border-radius: 12px;
  background: var(--surface-1);
  color: var(--ink-primary);
  width: min(460px, calc(100vw - 32px));
  max-height: 88svh;
  box-shadow: 0 18px 48px rgba(0, 0, 0, 0.28);
}
.sheet::backdrop { background: rgba(0, 0, 0, 0.45); }
.sheet-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 14px 18px;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background: var(--surface-1);
  z-index: 1;
}
.sheet-head h2 { margin: 0; font-size: 16px; }
.sheet-head .btn { margin-left: auto; }
.sheet-body {
  padding: 4px 18px 18px;
  overflow-y: auto;
  overscroll-behavior: contain;
  max-height: calc(88svh - 58px);
}
.sheet-compact { width: min(400px, calc(100vw - 32px)); }
.sheet-compact .sheet-body { padding: 20px; max-height: none; }
.sheet-compact h2 { margin: 0 0 8px; font-size: 16px; }

.settings-group { padding: 16px 0; border-bottom: 1px solid var(--border); }
.settings-group:last-child { border-bottom: none; }
.settings-group h3 {
  margin: 0 0 8px;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--ink-muted);
}
.settings-group > .hint { margin: 0 0 10px; }
.settings-sep { border: none; border-top: 1px solid var(--border); margin: 14px 0; }

.field-stack { display: block; margin-bottom: 10px; font-size: 13px; color: var(--ink-secondary); }
.field-stack input {
  display: block;
  width: 100%;
  margin-top: 4px;
  padding: 8px 10px;
  border: 1px solid var(--control-border);
  border-radius: 7px;
  background: var(--page);
  color: var(--ink-primary);
  font: inherit;
}
.password-form { display: flex; flex-direction: column; }
.password-form .btn-primary { width: auto; align-self: flex-start; }
.hint.ok { color: var(--ok); }
.hint.bad { color: var(--danger); }

.segmented {
  display: inline-flex;
  border: 1px solid var(--control-border);
  border-radius: 8px;
  overflow: hidden;
}
.segmented button {
  font: inherit;
  font-size: 13px;
  padding: 7px 14px;
  min-height: 40px;
  border: none;
  background: var(--surface-1);
  color: var(--ink-secondary);
  cursor: pointer;
}
.segmented button + button { border-left: 1px solid var(--control-border); }
.segmented button[aria-checked="true"] { background: var(--accent); color: #fff; }

.invite-result { display: flex; gap: 8px; margin-top: 10px; }
.invite-result input {
  flex: 1;
  min-width: 0;
  padding: 8px 10px;
  border: 1px solid var(--control-border);
  border-radius: 7px;
  background: var(--page);
  color: var(--ink-primary);
  font: inherit;
  font-size: 12px;
}
.btn-danger { background: var(--danger); color: #fff; border-color: var(--danger); }
.btn-danger-ghost { color: var(--danger); border-color: var(--control-border); }
.confirm-actions { justify-content: flex-end; margin-top: 16px; }

/* --- toast (replaces alert()) --- */
.toast {
  position: fixed;
  left: 50%;
  bottom: calc(18px + env(safe-area-inset-bottom));
  transform: translateX(-50%) translateY(12px);
  z-index: 60;
  max-width: min(420px, calc(100vw - 24px));
  padding: 11px 16px;
  border-radius: 10px;
  background: var(--ink-primary);
  color: var(--page);
  font-size: 13px;
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.3);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.18s, transform 0.18s;
}
.toast[data-show] { opacity: 1; transform: translateX(-50%) translateY(0); pointer-events: auto; }
.toast[data-kind="error"] { background: var(--danger); color: #fff; }

/* --- analysis progress, in main so it survives the drawer closing --- */
.analysis-state {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 22px;
  margin-bottom: 16px;
}
.analysis-state h2 { margin: 0 0 6px; font-size: 15px; }
.analysis-stage { color: var(--ink-secondary); font-size: 13px; margin: 0 0 12px; }
.analysis-state progress { width: 100%; height: 8px; }
.analysis-error { margin-top: 14px; color: var(--danger); font-size: 13px; }

.chip-settings {
  margin-left: auto;
  font: inherit;
  font-size: 11px;
  color: var(--ink-secondary);
  background: none;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 3px 10px;
  min-height: 28px;
  cursor: pointer;
}
.chip-settings:hover { border-color: var(--accent); color: var(--ink-primary); }

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

.upload-drop {
  display: block;
  margin-top: 10px;
  padding: 14px 10px;
  border: 1.5px dashed var(--baseline);
  border-radius: 8px;
  text-align: center;
  font-size: 13px;
  color: var(--ink-secondary);
  cursor: pointer;
}
.upload-drop:hover, .upload-drop.dragover { border-color: var(--accent); color: var(--ink-primary); }
.upload-drop .hint { margin: 2px 0 0; }
.upload-drop.busy { opacity: 0.6; cursor: progress; }

.race-list li .race-del {
  float: right;
  color: var(--ink-muted);
  border: none;
  background: none;
  cursor: pointer;
  font-size: 13px;
  padding: 0 2px;
}
.race-list li:hover .race-del { visibility: visible; }
.race-list li .race-del:hover { color: var(--danger); }
/* Hover-hiding only where hover exists — on a phone this button is the only
   way to delete a ride, and a hidden control is an unreachable one. */
@media (hover: hover) and (pointer: fine) {
  .race-list li .race-del { visibility: hidden; }
  .race-list li:hover .race-del { visibility: visible; }
}
@media (pointer: coarse) {
  .race-list li .race-del { visibility: visible; min-width: 44px; min-height: 44px; }
}

.field { display: flex; align-items: center; gap: 6px; margin-bottom: 8px; color: var(--ink-secondary); }
.field input {
  margin-left: auto;
  width: 76px;
  padding: 5px 7px;
  border: 1px solid var(--control-border);
  border-radius: 6px;
  background: var(--page);
  color: var(--ink-primary);
  font: inherit;
}
.unit { color: var(--ink-muted); font-size: 11px; }

.btn {
  font: inherit;
  padding: 6px 10px;
  border-radius: 6px;
  border: 1px solid var(--baseline);
  background: var(--surface-1);
  color: var(--ink-primary);
  cursor: pointer;
}
.btn:hover { border-color: var(--accent); }
.btn.armed { background: var(--accent); color: #fff; border-color: var(--accent); }
.btn-ghost { border-color: transparent; color: var(--ink-secondary); }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; }

.main { flex: 1; padding: 18px 22px; min-width: 0; }
.empty-state { color: var(--ink-muted); padding: 60px; text-align: center; font-size: 16px; }

.tile-row { display: grid; grid-template-columns: repeat(auto-fit, minmax(120px, 1fr)); gap: 12px; margin-bottom: 16px; }
.tile {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
}
.tile .tile-label { color: var(--ink-muted); font-size: 11px; text-transform: uppercase; letter-spacing: 0.05em; }
.tile .tile-value { font-size: 22px; font-weight: 650; margin-top: 2px; }
.tile .tile-sub { color: var(--ink-secondary); font-size: 12px; }

/* Wide screens: the fold does not exist at all. */
.more-tiles { display: contents; }
.more-tiles > summary { display: none; }
@media (max-width: 899px) {
  .more-tiles {
    display: block;
    grid-column: 1 / -1;
    background: var(--surface-1);
    border: 1px solid var(--border);
    border-radius: 10px;
    padding: 10px 14px;
  }
  .more-tiles > summary {
    display: list-item;
    cursor: pointer;
    font-size: 13px;
    color: var(--ink-secondary);
    min-height: 32px;
  }
  .more-tiles[open] { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
  .more-tiles[open] > summary { grid-column: 1 / -1; }
  .more-tiles .tile { border: none; padding: 4px 0; }
}

.card {
  background: var(--surface-1);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px 16px;
  margin-bottom: 16px;
}
.card-head { display: flex; align-items: center; gap: 12px; margin-bottom: 8px; flex-wrap: wrap; }
.card-head h2 { font-size: 15px; margin: 0; margin-right: auto; }
.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
@media (max-width: 1100px) { .two-col { grid-template-columns: 1fr; } }

.chart { width: 100%; height: 300px; }
.chart-tall { height: 420px; }
.chart-section { height: 470px; }
@media (max-width: 699px) { .chart-section { height: 620px; } }
.chart-corr { height: 200px; }

.card-head.sub-head { margin-top: 14px; border-top: 1px solid var(--border); padding-top: 12px; }
.card-head h3 { font-size: 13px; margin: 0; color: var(--ink-secondary); }
.card-head .hint-inline { margin-left: auto; margin-top: 0; }
.meta {
  color: var(--ink-secondary);
  font-size: 12px;
  margin: 0 0 6px;
  font-variant-numeric: tabular-nums;
}
.meta .sep { color: var(--ink-muted); margin: 0 7px; }
.meta .warn { color: var(--ink-muted); }

.map-wrap { position: relative; }
.map { width: 100%; height: 520px; border-radius: 8px; overflow: hidden; }

.map-legend {
  position: absolute;
  left: 10px;
  bottom: 10px;
  z-index: 5;
  background: color-mix(in oklab, var(--surface-1) 88%, transparent);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 12px;
  font-size: 11px;
  color: var(--ink-secondary);
  backdrop-filter: blur(3px);
  max-width: 280px;
}
.map-legend .legend-title { font-weight: 600; color: var(--ink-primary); margin-bottom: 5px; }
.map-legend .legend-bar { height: 10px; border-radius: 5px; margin-bottom: 3px; }
.map-legend .legend-scale { display: flex; justify-content: space-between; font-variant-numeric: tabular-nums; }
.map-legend .legend-zones { display: grid; grid-template-columns: auto auto 1fr; gap: 2px 7px; align-items: center; }
.map-legend .legend-zones .swatch { width: 11px; height: 11px; border-radius: 3px; }
.map-legend .legend-zones .range { font-variant-numeric: tabular-nums; color: var(--ink-muted); }
.controls { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; }
.select {
  font: inherit;
  padding: 5px 8px;
  border-radius: 6px;
  border: 1px solid var(--baseline);
  background: var(--surface-1);
  color: var(--ink-primary);
}
.check { display: flex; gap: 5px; align-items: center; color: var(--ink-secondary); }

.lap-toggles { display: flex; gap: 8px; flex-wrap: wrap; margin-bottom: 8px; }
.lap-chip {
  display: flex; align-items: center; gap: 6px;
  padding: 3px 10px;
  border-radius: 999px;
  border: 1px solid var(--baseline);
  cursor: pointer;
  color: var(--ink-secondary);
  user-select: none;
}
.lap-chip.off { opacity: 0.35; }
.lap-chip .swatch { width: 10px; height: 10px; border-radius: 3px; }

/* --- focus, motion, responsive --- */
/* :where() keeps specificity at 0 so component rules still win. */
:where(a, button, input, select, textarea, [tabindex]):focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 4px;
}
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    transition-duration: 0.01ms !important;
    scroll-behavior: auto !important;
  }
}

/* Two columns whenever there is room for them — not at a viewport width,
   which asks the wrong question once the sidebar collapses. */
.two-col { grid-template-columns: repeat(auto-fit, minmax(min(420px, 100%), 1fr)); }

.chart-corr-wrap { overflow-x: auto; overscroll-behavior-x: contain; }
.map-caption { margin: 8px 2px 0; }
.finish-tool { display: inline-flex; gap: 8px; }
.pick-mobile { display: none; }

@media (max-width: 899px) {
  .nav-only { display: inline-flex; align-items: center; justify-content: center; }
  .layout { display: block; }
  .scrim {
    display: block;
    position: fixed;
    inset: 0;
    z-index: 45;
    background: rgba(0, 0, 0, 0.45);
  }
  .sidebar {
    position: fixed;
    inset: calc(var(--appbar-h) + env(safe-area-inset-top)) auto 0 0;
    z-index: 50;
    width: min(86vw, 320px);
    height: auto;
    transform: translateX(-102%);
    transition: transform 0.2s ease;
    box-shadow: 0 0 40px rgba(0, 0, 0, 0.25);
  }
  .sidebar[data-open] { transform: none; }
  .main { padding: 14px; }
  .tile-row { grid-template-columns: repeat(2, 1fr); gap: 8px; }
  /* svh, never dvh: dvh re-resolves as the URL bar collapses, which makes the
     map jitter and fires a resize storm. */
  .map { height: min(58svh, 420px); }
  .card { padding: 12px; }
  .controls { width: 100%; }
  .pick-desktop { display: none; }
  .pick-mobile { display: block; }
  .sheet {
    width: 100vw;
    max-width: none;
    margin: auto auto 0;
    border-radius: 16px 16px 0 0;
    max-height: 92svh;
  }
  .sheet-body { max-height: calc(92svh - 58px); padding-bottom: calc(18px + env(safe-area-inset-bottom)); }
}

/* 16px minimum on touch controls: Safari force-zooms anything smaller and
   never zooms back out. user-scalable=no is not an acceptable alternative. */
@media (pointer: coarse) {
  input, select, textarea { font-size: 16px; }
  .btn, .icon-btn, .select, .segmented button { min-height: 44px; }
}

.maplibregl-popup-content {
  font-family: inherit;
  background: var(--surface-1);
  color: var(--ink-primary);
  border-radius: 8px;
  box-shadow: 0 2px 12px rgba(0,0,0,0.25);
}
.maplibregl-popup-content table { border-collapse: collapse; }
.maplibregl-popup-content td { padding: 1px 6px 1px 0; }
.maplibregl-popup-content td:last-child { text-align: right; font-variant-numeric: tabular-nums; }
