:root {
  --bg: #f4f6f8;
  --surface: #ffffff;
  --surface-2: #eef1f4;
  --text: #0f1720;
  --muted: #5b6673;
  --line: #e1e5ea;
  --accent: #2563eb;
  --go: #16a34a;
  --warn: #d97706;
  --stop: #dc2626;
  --shadow: 0 1px 2px rgba(15, 23, 32, .06), 0 4px 16px rgba(15, 23, 32, .06);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b1016;
    --surface: #131a22;
    --surface-2: #1b242e;
    --text: #e8edf2;
    --muted: #8d99a6;
    --line: #26313c;
    --accent: #60a5fa;
    --shadow: 0 1px 2px rgba(0, 0, 0, .3), 0 4px 16px rgba(0, 0, 0, .25);
    color-scheme: dark;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-tap-highlight-color: transparent;
  font-variant-numeric: tabular-nums;
}
button { font: inherit; color: inherit; cursor: pointer; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 13px; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 1000;
  display: flex; align-items: center; gap: 4px;
  padding: calc(env(safe-area-inset-top) + 8px) 12px 8px;
  background: var(--surface); border-bottom: 1px solid var(--line);
}
.tab {
  border: 0; background: none; padding: 8px 12px; border-radius: 999px;
  font-weight: 600; color: var(--muted);
}
.tab.active { background: var(--surface-2); color: var(--text); }
.spacer { flex: 1; }
.units-btn {
  border: 1px solid var(--line); background: var(--surface); color: var(--text);
  font-size: 12px; font-weight: 700; padding: 4px 10px; border-radius: 999px; min-width: 52px;
}
.icon-btn { border: 0; background: none; padding: 8px; border-radius: 999px; display: grid; place-items: center; color: var(--muted); }
.icon-btn:hover { background: var(--surface-2); }
.gps-badge {
  font-size: 12px; font-weight: 600; padding: 4px 9px; border-radius: 999px;
  background: var(--surface-2); color: var(--muted); white-space: nowrap;
}
.gps-badge.good { background: rgba(22, 163, 74, .15); color: var(--go); }
.gps-badge.ok { background: rgba(217, 119, 6, .15); color: var(--warn); }
.gps-badge.bad { background: rgba(220, 38, 38, .15); color: var(--stop); }

/* ---------- views ---------- */
.view { display: none; }
.view.active { display: block; }

.map-wrap { position: relative; }
.map { height: 46vh; min-height: 260px; width: 100%; background: var(--surface-2); }
.map-btn {
  position: absolute; z-index: 800; right: 10px; bottom: 26px;
  width: 42px; height: 42px; border-radius: 50%; border: 0;
  background: var(--surface); color: var(--muted); box-shadow: var(--shadow);
  display: grid; place-items: center;
}
.map-btn.on { color: var(--accent); }

.panel { padding: 14px 16px calc(env(safe-area-inset-bottom) + 32px); max-width: 980px; margin: 0 auto; }
.panel-full { padding-top: 20px; }

@media (min-width: 960px) {
  #view-record.active, #view-detail.active { display: grid; grid-template-columns: minmax(0, 1fr) 480px; height: calc(100vh - 57px); }
  #view-record .map-wrap, #view-detail .map-wrap { height: 100%; }
  #view-record .map, #view-detail .map { height: 100%; }
  #view-record .panel, #view-detail .panel { overflow-y: auto; height: 100%; max-width: none; margin: 0; border-left: 1px solid var(--line); background: var(--bg); }
}

/* ---------- hero numbers ---------- */
.panel > .hero:first-child { margin-top: 2px; }
.hero {
  display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px;
  margin: 14px 0 12px; padding: 14px 8px; background: var(--surface);
  border-radius: 16px; box-shadow: var(--shadow); text-align: center;
}
.hero-val { font-size: clamp(26px, 8vw, 40px); font-weight: 750; letter-spacing: -.02em; line-height: 1.05; }
.hero-lbl { font-size: 12px; color: var(--muted); text-transform: uppercase; letter-spacing: .06em; margin-top: 4px; }
.is-recording .hero { outline: 2px solid rgba(220, 38, 38, .5); }
.is-paused .hero { outline: 2px solid rgba(217, 119, 6, .5); }

/* ---------- buttons ---------- */
.controls { display: flex; gap: 8px; flex-wrap: wrap; }
.btn {
  flex: 1 1 0; border: 0; border-radius: 14px; padding: 15px 18px;
  font-size: 17px; font-weight: 700; color: #fff; min-width: 120px;
  transition: transform .06s ease, filter .15s;
}
.btn:active { transform: scale(.98); }
.btn:hover { filter: brightness(1.05); }
.btn-go { background: var(--go); }
.btn-warn { background: var(--warn); }
.btn-stop { background: var(--stop); }
.btn-ghost { background: var(--surface); color: var(--text); border: 1px solid var(--line); }
.btn-sm { padding: 9px 12px; font-size: 14px; font-weight: 600; flex: 0 1 auto; min-width: 0; }
.btn.danger { color: var(--stop); }

.status-msg { margin: 10px 2px 4px; font-size: 13px; color: var(--muted); min-height: 1.4em; }
.status-msg[data-kind="rec"] { color: var(--stop); font-weight: 600; }
.status-msg[data-kind="rec"]::before { content: ""; display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--stop); margin-right: 7px; vertical-align: 1px; animation: blink 1.2s infinite; }
.status-msg[data-kind="warn"] { color: var(--warn); }
.status-msg[data-kind="err"] { color: var(--stop); font-weight: 600; }
.status-msg[data-kind="ok"] { color: var(--go); }
@keyframes blink { 50% { opacity: .2; } }

/* ---------- stat grid ---------- */
.stat-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 8px; margin-top: 12px; }
.stat { background: var(--surface); border-radius: 12px; padding: 10px 12px; border: 1px solid var(--line); min-width: 0; }
.stat.wide { grid-column: span 2; }
.stat-lbl { font-size: 11.5px; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.stat-val { font-size: 19px; font-weight: 700; margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.stat-val small { font-size: 12px; font-weight: 500; color: var(--muted); }
.stat[data-g="Speed"] { border-left: 3px solid #3b82f6; }
.stat[data-g="Pace"] { border-left: 3px solid #8b5cf6; }
.stat[data-g="Time"] { border-left: 3px solid #64748b; }
.stat[data-g="Elevation"] { border-left: 3px solid #10b981; }
.stat[data-g="Position"] { border-left: 3px solid #f59e0b; }
.stat[data-g="Distance"] { border-left: 3px solid #ef4444; }
.stat[data-g="GPS"] { border-left: 3px solid #06b6d4; }

.section-h { font-size: 13px; text-transform: uppercase; letter-spacing: .06em; color: var(--muted); margin: 22px 2px 8px; }
.chart { width: 100%; height: 150px; display: block; background: var(--surface); border-radius: 12px; border: 1px solid var(--line); }

/* ---------- splits ---------- */
.splits table { width: 100%; border-collapse: collapse; background: var(--surface); border-radius: 12px; overflow: hidden; border: 1px solid var(--line); font-size: 14px; }
.splits th, .splits td { padding: 8px 10px; text-align: left; border-bottom: 1px solid var(--line); white-space: nowrap; }
.splits th { font-size: 11.5px; color: var(--muted); text-transform: uppercase; letter-spacing: .05em; font-weight: 600; }
.splits tr:last-child td { border-bottom: 0; }
.splits tr.best td { color: var(--go); font-weight: 700; }
.splits tr.partial td { color: var(--muted); font-style: italic; }
.splits .barcell { width: 30%; }
.splits .bar { display: block; height: 8px; border-radius: 4px; background: var(--accent); opacity: .7; }

/* ---------- history ---------- */
.history-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.history-head h2 { margin: 0 0 12px; font-size: 22px; }
.history-list { display: grid; gap: 10px; }
.hist-item {
  display: flex; gap: 12px; align-items: center; text-align: left; width: 100%;
  padding: 10px; background: var(--surface); border: 1px solid var(--line); border-radius: 14px;
}
.hist-item:hover { border-color: var(--accent); }
.thumb { flex: 0 0 64px; width: 64px; height: 64px; background: var(--surface-2); border-radius: 10px; }
.thumb path { fill: none; stroke: var(--accent); stroke-width: 4; stroke-linejoin: round; stroke-linecap: round; }
.hist-main { min-width: 0; flex: 1; }
.hist-name { font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.hist-nums { display: flex; flex-wrap: wrap; gap: 4px 14px; margin-top: 4px; font-size: 13px; color: var(--muted); }
.hist-nums b { color: var(--text); }
.empty { text-align: center; padding: 48px 16px; background: var(--surface); border-radius: 16px; border: 1px dashed var(--line); }

/* ---------- detail ---------- */
.detail-head { display: flex; gap: 12px; align-items: flex-start; }
.detail-title { min-width: 0; }
.detail-title h2 { margin: 0; font-size: 20px; overflow-wrap: anywhere; }
.detail-head + .hero { margin-top: 12px; }
.detail-head ~ .controls { margin-bottom: 4px; }
.legend {
  position: absolute; z-index: 800; left: 10px; bottom: 26px;
  display: flex; align-items: center; gap: 6px; padding: 5px 9px;
  background: var(--surface); border-radius: 999px; box-shadow: var(--shadow); font-size: 12px;
}
.legend .ramp { width: 80px; height: 8px; border-radius: 4px; background: linear-gradient(90deg, rgb(59,130,246), rgb(16,185,129) 35%, rgb(234,179,8) 65%, rgb(239,68,68)); }

/* ---------- map pins ---------- */
.pin {
  width: 26px; height: 26px; border-radius: 50%; display: grid; place-items: center;
  color: #fff; font: 700 12px/1 system-ui, sans-serif; border: 2px solid #fff;
  box-shadow: 0 1px 4px rgba(0, 0, 0, .4);
}
.pin-start { background: #16a34a; }
.pin-end { background: #111827; }
.pin-fast { background: #ef4444; }
.pin-high { background: #10b981; }
.pin svg { display: block; }
.pin-split { background: #fff; color: #111827; width: 22px; height: 22px; font-size: 11px; border-color: #111827; margin: 2px; }

/* ---------- settings ---------- */
dialog {
  border: 0; border-radius: 18px; padding: 0; width: min(440px, calc(100vw - 32px));
  background: var(--surface); color: var(--text); box-shadow: 0 20px 60px rgba(0, 0, 0, .35);
}
dialog::backdrop { background: rgba(0, 0, 0, .45); }
.settings { padding: 20px; display: grid; gap: 14px; }
.settings h2 { margin: 0; }
.settings .row { display: grid; gap: 6px; font-weight: 600; font-size: 14px; }
.settings .row.check { display: flex; align-items: center; gap: 10px; font-weight: 500; }
.settings .row.check input { width: 20px; height: 20px; accent-color: var(--accent); }
.settings select, .settings input[type=number] {
  font: inherit; font-weight: 400; padding: 10px; border-radius: 10px; border: 1px solid var(--line);
  background: var(--surface-2); color: var(--text); width: 100%;
}

/* ---------- toast ---------- */
.toast {
  position: fixed; left: 50%; bottom: calc(env(safe-area-inset-bottom) + 24px); z-index: 2000;
  transform: translate(-50%, 20px); opacity: 0; pointer-events: none;
  background: #111827; color: #fff; padding: 10px 16px; border-radius: 999px; font-weight: 600;
  transition: opacity .2s, transform .2s; max-width: calc(100vw - 32px);
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

/* Leaflet controls follow the theme */
.leaflet-control-layers, .leaflet-bar a { background: var(--surface); color: var(--text); }
.leaflet-container { font: inherit; }
