:root {
  --lion: #ffd200;
  --lion-deep: #e6b800;
  --claw: #d7192a;
  --ink: #141414;
  --bg: #f4f1e8;
  --surface: #fffdf7;
  --surface-2: #ece7da;
  --text: #1b1b1b;
  --muted: #6b675d;
  --line: #d9d2c1;
  --cobble: #6e5b45;
  --focus: #1f6feb;
  /* gradient bands */
  --g0: #9fd4a8;
  --g1: #ffd200;
  --g2: #ff9a1f;
  --g3: #e5332a;
  --g4: #a3123a;
  --g5: #3b0a26;
  --shadow: 0 1px 2px rgb(0 0 0 / .06), 0 4px 16px rgb(0 0 0 / .06);
  --radius: 10px;
  --font-display: "Barlow Condensed", "Arial Narrow", system-ui, sans-serif;
  --font: Inter, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #121212;
    --surface: #1b1b1a;
    --surface-2: #262522;
    --text: #f1ede3;
    --muted: #a39e91;
    --line: #36342f;
    --cobble: #b59a7a;
    --shadow: 0 1px 2px rgb(0 0 0 / .4), 0 6px 20px rgb(0 0 0 / .35);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #121212; --surface: #1b1b1a; --surface-2: #262522; --text: #f1ede3;
  --muted: #a39e91; --line: #36342f; --cobble: #b59a7a; color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { height: 100%; margin: 0; }
body {
  display: flex; flex-direction: column;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
}
button { font: inherit; color: inherit; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
h1, h2, h3, h4 { font-family: var(--font-display); letter-spacing: .01em; margin: 0; line-height: 1.1; }
a { color: inherit; text-decoration-color: var(--lion-deep); text-underline-offset: 2px; }
small { font-size: .82em; }

/* ---------- layout ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 16px; background: var(--ink); color: #fff;
  border-bottom: 4px solid var(--lion);
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.brand-mark { width: 36px; height: 36px; flex: none; }
.brand h1 { font-size: 26px; font-weight: 800; text-transform: uppercase; }
.brand h1 span { color: var(--lion); }
.subtitle { margin: 0; font-size: 12px; color: #c9c4b6; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.topbar { flex: none; }
.layout {
  flex: 1; min-height: 0;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 420px;
  grid-template-rows: minmax(0, 1fr) 210px;
  grid-template-areas: "map panel" "profile panel";
}
.mapwrap { grid-area: map; position: relative; min-height: 0; }
#map { position: absolute; inset: 0; }
.profile { grid-area: profile; background: var(--surface); border-top: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }
.panel { grid-area: panel; background: var(--surface); border-left: 1px solid var(--line); display: flex; flex-direction: column; min-height: 0; }

/* ---------- controls ---------- */
.seg { display: inline-flex; background: var(--surface-2); border-radius: 999px; padding: 3px; gap: 2px; }
.seg button {
  border: 0; background: transparent; padding: 5px 12px; border-radius: 999px; cursor: pointer;
  font-weight: 600; font-size: 12.5px; color: var(--muted);
}
.seg button[aria-checked="true"] { background: var(--ink); color: #fff; }
:root[data-theme="dark"] .seg button[aria-checked="true"] { background: var(--lion); color: var(--ink); }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .seg button[aria-checked="true"] { background: var(--lion); color: var(--ink); } }
.chip {
  border: 1px solid var(--line); background: var(--surface); padding: 5px 12px; border-radius: 999px;
  cursor: pointer; font-weight: 600; font-size: 12.5px; box-shadow: var(--shadow);
}
.chip[aria-pressed="true"] { background: var(--lion); border-color: var(--lion-deep); color: var(--ink); }
.btn {
  display: inline-flex; align-items: center; gap: 8px; border: 0; cursor: pointer;
  background: var(--ink); color: #fff; padding: 9px 16px; border-radius: 999px; font-weight: 600;
}
.btn.lion { background: var(--lion); color: var(--ink); }
.btn:hover { filter: brightness(1.08); }

.race-switch { display: inline-flex; background: #2a2a2a; border-radius: 999px; padding: 3px; flex: none; }
.race-switch button {
  border: 0; background: transparent; color: #cfcab9; padding: 6px 14px; border-radius: 999px;
  cursor: pointer; font-weight: 700; font-family: var(--font-display); font-size: 17px; letter-spacing: .02em;
}
.race-switch button small { font-family: var(--font); font-weight: 500; font-size: 11px; opacity: .75; margin-left: 2px; }
.race-switch button[aria-checked="true"] { background: var(--lion); color: var(--ink); }

.map-tools { position: absolute; top: 12px; left: 12px; display: flex; gap: 8px; flex-wrap: wrap; z-index: 2; }
.map-tools .seg { box-shadow: var(--shadow); background: var(--surface); }
.map-legend {
  position: absolute; left: 12px; bottom: 12px; z-index: 2; background: var(--surface); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 8px 10px; font-size: 12px; display: grid; gap: 4px;
}
.map-legend .row { display: flex; align-items: center; gap: 8px; cursor: pointer; user-select: none; }
.map-legend .row input { margin: 0; accent-color: var(--ink); }
.swatch { width: 22px; height: 6px; border-radius: 3px; flex: none; }
.swatch.route { background: var(--lion); box-shadow: 0 0 0 1.5px var(--ink); }
.swatch.cobbles { background: repeating-linear-gradient(90deg, var(--ink) 0 4px, transparent 4px 7px); height: 4px; }
.swatch.dot { width: 12px; height: 12px; border-radius: 50%; }

/* ---------- profile ---------- */
.profile-head { display: flex; align-items: center; gap: 12px; padding: 8px 12px 4px; flex-wrap: wrap; }
.profile-readout { flex: 1; min-width: 200px; font-size: 12.5px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.profile-readout b { color: var(--text); }
.grad-key { display: flex; gap: 2px; font-size: 10.5px; font-weight: 600; }
.grad-key span { padding: 1px 5px; border-radius: 3px; background: var(--c); color: #111; }
.grad-key span:nth-child(n+4) { color: #fff; }
.profile-canvas { position: relative; flex: 1; min-height: 0; }
.profile-canvas canvas { position: absolute; inset: 0; width: 100%; height: 100%; }
#profile-top { cursor: crosshair; touch-action: none; }

/* ---------- panel ---------- */
.tabs { display: flex; border-bottom: 1px solid var(--line); padding: 0 8px; flex: none; }
.tabs button {
  flex: 1; border: 0; background: none; padding: 12px 4px 10px; cursor: pointer; font-family: var(--font-display);
  font-weight: 700; font-size: 17px; text-transform: uppercase; letter-spacing: .03em; color: var(--muted);
  border-bottom: 3px solid transparent; margin-bottom: -1px;
}
.tabs button[aria-selected="true"] { color: var(--text); border-bottom-color: var(--lion); }
.panel-body { overflow-y: auto; flex: 1; min-height: 0; scroll-behavior: smooth; }
.tabpane { padding: 16px 16px 40px; }
.tabpane h2 { font-size: 26px; font-weight: 800; text-transform: uppercase; margin-bottom: 4px; }
.tabpane h3 { font-size: 19px; font-weight: 700; margin: 22px 0 8px; text-transform: uppercase; letter-spacing: .03em; }
.lede { color: var(--muted); margin: 4px 0 14px; }
.kpis { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 12px 0; }
.kpi { background: var(--surface-2); border-radius: var(--radius); padding: 10px; }
.kpi b { display: block; font-family: var(--font-display); font-size: 26px; font-weight: 800; line-height: 1; }
.kpi span { font-size: 11.5px; color: var(--muted); }
.note { font-size: 12px; color: var(--muted); }
.callout { border-left: 4px solid var(--lion); background: var(--surface-2); padding: 10px 12px; border-radius: 0 var(--radius) var(--radius) 0; margin: 12px 0; }

/* route waypoints list */
.waypoints { list-style: none; padding: 0; margin: 0; position: relative; }
.waypoints::before { content: ""; position: absolute; left: 47px; top: 6px; bottom: 6px; width: 2px; background: var(--line); }
.wp { display: grid; grid-template-columns: 40px 16px 1fr; gap: 0 8px; padding: 6px 0; cursor: pointer; border-radius: 6px; }
.wp:hover { background: var(--surface-2); }
.wp .km { font-variant-numeric: tabular-nums; font-size: 12px; color: var(--muted); text-align: right; padding-top: 2px; }
.wp .dot { width: 12px; height: 12px; border-radius: 50%; background: var(--surface); border: 2px solid var(--ink); margin-top: 4px; position: relative; z-index: 1; }
.wp.climb .dot { background: var(--claw); border-color: var(--claw); }
.wp.sector .dot { background: var(--cobble); border-color: var(--cobble); border-radius: 3px; }
.wp.place .dot { background: var(--lion); }
.wp.start .dot, .wp.finish .dot { background: var(--ink); width: 14px; height: 14px; margin-left: -1px; }
.wp .t b { font-weight: 600; }
.wp .t div { font-size: 12px; color: var(--muted); }

/* climbs */
.climb-list { display: grid; gap: 8px; }
.climb-card {
  display: grid; grid-template-columns: 34px 1fr auto; gap: 10px; align-items: center; text-align: left;
  border: 1px solid var(--line); background: var(--surface); border-radius: var(--radius); padding: 10px; cursor: pointer;
  width: 100%;
}
.climb-card:hover { border-color: var(--ink); }
.climb-card .num {
  width: 34px; height: 34px; border-radius: 50%; background: var(--ink); color: var(--lion); display: grid; place-items: center;
  font-family: var(--font-display); font-weight: 800; font-size: 18px;
}
.climb-card h4 { font-size: 18px; font-weight: 700; }
.climb-card .meta { font-size: 12px; color: var(--muted); }
.climb-card .max { font-family: var(--font-display); font-weight: 800; font-size: 22px; text-align: right; line-height: 1; }
.climb-card .max small { display: block; font-family: var(--font); font-weight: 500; font-size: 10.5px; color: var(--muted); }
.badge { display: inline-block; font-size: 10.5px; font-weight: 700; text-transform: uppercase; letter-spacing: .04em; padding: 1px 6px; border-radius: 4px; background: var(--surface-2); color: var(--muted); margin-left: 4px; vertical-align: 2px; }
.badge.cob { background: var(--cobble); color: #fff; }
.back { border: 0; background: none; cursor: pointer; color: var(--muted); padding: 0; margin-bottom: 8px; font-weight: 600; }
.climb-detail .stats { width: 100%; border-collapse: collapse; font-size: 13px; margin: 10px 0; }
.climb-detail .stats th, .climb-detail .stats td { padding: 5px 6px; border-bottom: 1px solid var(--line); text-align: right; font-variant-numeric: tabular-nums; }
.climb-detail .stats th:first-child, .climb-detail .stats td:first-child { text-align: left; color: var(--muted); font-weight: 500; }
.climb-detail .stats thead th { font-size: 11px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.climbchart { width: 100%; height: auto; display: block; margin: 8px 0 4px; }
.climbchart text { font: 600 10px var(--font); fill: var(--text); }
.climbchart .axis { stroke: var(--line); }
.climbchart .lbl { fill: var(--muted); font-weight: 500; }
.story p { margin: 0 0 10px; }
.passages { display: flex; gap: 6px; flex-wrap: wrap; margin: 6px 0; }

/* race */
.results { width: 100%; border-collapse: collapse; font-size: 13px; }
.results td { padding: 5px 4px; border-bottom: 1px solid var(--line); }
.results td:first-child { width: 26px; font-family: var(--font-display); font-weight: 800; font-size: 16px; }
.results td:last-child { text-align: right; font-variant-numeric: tabular-nums; color: var(--muted); }
.results tr:first-child td { font-weight: 700; }
.results tr:first-child td:last-child { color: var(--text); }
.timeline { list-style: none; margin: 0; padding: 0; }
.timeline li { position: relative; padding: 0 0 14px 22px; cursor: pointer; }
.timeline li::before { content: ""; position: absolute; left: 4px; top: 6px; width: 10px; height: 10px; border-radius: 50%; background: var(--lion); box-shadow: 0 0 0 2px var(--ink); }
.timeline li::after { content: ""; position: absolute; left: 8px; top: 18px; bottom: 0; width: 2px; background: var(--line); }
.timeline li:last-child::after { display: none; }
.timeline li.active::before { background: var(--claw); }
.timeline .togo { font-size: 11.5px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.timeline b { display: block; }

/* history */
.era { border-left: 3px solid var(--line); padding: 0 0 12px 12px; margin-left: 4px; }
.era h4 { font-size: 17px; font-weight: 700; }
.era .yrs { font-size: 11.5px; color: var(--muted); font-weight: 600; }
.era p { margin: 4px 0 0; }
.winners-tools { display: flex; gap: 8px; margin: 8px 0; align-items: center; flex-wrap: wrap; }
.winners-tools input, .winners-tools select {
  font: inherit; padding: 6px 10px; border-radius: 8px; border: 1px solid var(--line); background: var(--surface); color: var(--text); min-width: 0; flex: 1;
}
.winners { width: 100%; border-collapse: collapse; font-size: 13px; }
.winners td { padding: 4px 4px; border-bottom: 1px solid var(--line); }
.winners td:first-child { font-variant-numeric: tabular-nums; color: var(--muted); width: 44px; }
.winners td:last-child { text-align: right; color: var(--muted); font-size: 12px; }
.nat-bars { display: grid; gap: 4px; margin: 8px 0; }
.nat-bar { display: grid; grid-template-columns: 90px 1fr 28px; gap: 8px; align-items: center; font-size: 12.5px; }
.nat-bar .bar { height: 10px; border-radius: 5px; background: var(--ink); }
:root[data-theme="dark"] .nat-bar .bar { background: var(--lion); }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .nat-bar .bar { background: var(--lion); } }
.nat-bar b { text-align: right; font-variant-numeric: tabular-nums; }
.records { padding-left: 18px; margin: 6px 0; }
.records li { margin-bottom: 6px; }
.quote { font-family: var(--font-display); font-size: 20px; font-weight: 600; line-height: 1.2; margin: 14px 0 4px; }
.quote + .note { margin-bottom: 14px; }
.sources { font-size: 12px; color: var(--muted); }
.sources li { margin-bottom: 4px; }

/* map popups */
.maplibregl-popup-content { background: var(--surface); color: var(--text); border-radius: var(--radius); box-shadow: var(--shadow); padding: 10px 12px; font: 13px/1.45 var(--font); max-width: 280px; }
.maplibregl-popup-tip { border-top-color: var(--surface) !important; border-bottom-color: var(--surface) !important; }
.maplibregl-popup-close-button { font-size: 18px; color: var(--muted); padding: 0 6px; }
.pop h4 { font-size: 17px; font-weight: 700; margin-bottom: 2px; }
.pop .k { font-size: 11px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: .04em; }
.pop p { margin: 6px 0 0; }
.climb-marker {
  width: 24px; height: 24px; border-radius: 50%; background: var(--ink); color: var(--lion); border: 2px solid var(--lion);
  display: grid; place-items: center; font: 800 13px var(--font-display); cursor: pointer; box-shadow: 0 1px 4px rgb(0 0 0 / .4);
}
.climb-marker.cob { background: var(--cobble); color: #fff; border-color: #fff; }
.cursor-marker { width: 16px; height: 16px; border-radius: 50%; background: var(--claw); border: 3px solid #fff; box-shadow: 0 0 0 1.5px var(--ink), 0 2px 8px rgb(0 0 0 / .4); pointer-events: none; }
.rider-marker { width: 22px; height: 22px; border-radius: 50%; background: var(--lion); border: 3px solid var(--ink); box-shadow: 0 0 0 3px rgb(255 210 0 / .35), 0 2px 10px rgb(0 0 0 / .5); pointer-events: none; }

.replay-hud {
  position: absolute; left: 50%; transform: translateX(-50%); bottom: 16px; z-index: 3; display: flex; align-items: center; gap: 12px;
  background: var(--ink); color: #fff; border-radius: 14px; padding: 10px 14px; box-shadow: var(--shadow); width: min(560px, calc(100% - 24px));
}
.hud-km { text-align: center; flex: none; min-width: 58px; }
.hud-km span { display: block; font: 800 30px/1 var(--font-display); color: var(--lion); }
.hud-km small { font-size: 10px; color: #bdb8aa; text-transform: uppercase; letter-spacing: .05em; }
.hud-text { flex: 1; font-size: 13px; line-height: 1.35; min-height: 2.7em; }
.hud-text b { color: var(--lion); }
.replay-hud .chip { background: #2a2a2a; color: #fff; border-color: #444; box-shadow: none; }

/* ---------- responsive ---------- */
@media (max-width: 960px) {
  body { height: auto; display: block; }
  .layout {
    grid-template-columns: 100%;
    grid-template-rows: 58vh 190px auto;
    grid-template-areas: "map" "profile" "panel";
  }
  .panel { border-left: 0; border-top: 1px solid var(--line); }
  .panel-body { overflow: visible; }
  .tabs { position: sticky; top: 0; background: var(--surface); z-index: 5; }
  .subtitle { display: none; }
  .brand h1 { font-size: 21px; }
  .topbar { padding: 8px 12px; }
  .grad-key { display: none; }
  .map-legend { display: none; }
}
@media (max-width: 520px) {
  .race-switch button { padding: 5px 10px; font-size: 15px; }
  .race-switch button small { display: none; }
  .brand-mark { width: 30px; height: 30px; }
  .kpis { grid-template-columns: repeat(2, 1fr); }
  .tabs button { font-size: 15px; }
}
@media (prefers-reduced-motion: reduce) { .panel-body { scroll-behavior: auto; } }
.map-fallback { position: absolute; inset: 0; display: grid; place-items: center; padding: 24px; text-align: center; color: var(--muted);
  background: repeating-linear-gradient(135deg, var(--surface-2) 0 14px, var(--bg) 14px 28px); }
.map-fallback b { color: var(--text); display: block; font-family: var(--font-display); font-size: 22px; text-transform: uppercase; }
.map-legend-inline { display: inline-flex; gap: 8px; align-items: center; margin: 2px 0; cursor: pointer; }
/* First World War */
.swatch.poppy, .poppy-dot { background: radial-gradient(circle, #141414 0 22%, #c8102e 24%); border: 1.5px solid #141414; }
.poppy-dot { display: inline-block; width: 11px; height: 11px; border-radius: 50%; vertical-align: -1px; }
.wp.ww1 .dot { background: radial-gradient(circle, #141414 0 22%, #c8102e 24%); border-color: #141414; }
.wp.ww1 .t b { color: #a50d27; }
.pop.ww1 .k:first-child { color: #c8102e; }
:root[data-theme="dark"] .wp.ww1 .t b { color: #ff6b7f; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .wp.ww1 .t b { color: #ff6b7f; } }
