/* Light = "Quest Log" (cobalt, gold side quests). Dark = "Night Run" (streetlight amber).
   The app sets data-theme on <html>; the media query only covers the moment before JS runs. */
:root {
  --bg: #e8eef6;
  --surface: #ffffff;
  --field: #f3f5fb;
  --ink: #141a33;
  --ink-soft: #5d6482;
  --line: #e1e5f0;
  --accent: #3b4cf5;
  --accent-ink: #ffffff;
  --primary: #141a33;
  --primary-ink: #ffffff;
  --start: #3b4cf5;
  --end: #e0452f;
  --route: #3b4cf5;
  --casing: #ffffff;
  --workout: #ff8a00;
  --quest: #8f5a00; /* dark gold: readable on the light cream card */
  --quest-bg: #fff6e3;
  --quest-ink: #5f4a17;
  --danger: #c7331f;
  --radius: 18px;
  --radius-sm: 10px;
  --display: "Syne", "Trebuchet MS", system-ui, sans-serif;
  --font: "Karla", ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;
  --panel-w: 370px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0e1621; --surface: #0b111a; --field: #131d2b; --ink: #e9edf2; --ink-soft: #8a9ab0;
    --line: #1f2b3b; --accent: #f2b632; --accent-ink: #0b111a; --primary: #f2b632; --primary-ink: #0b111a;
    --start: #f2b632; --end: #ff6b57; --route: #f2b632; --casing: #0b111a; --workout: #6fb3ff;
    --quest: #f2b632; --quest-bg: #131d2b; --quest-ink: #c3cdda; --danger: #ff6b57;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0e1621; --surface: #0b111a; --field: #131d2b; --ink: #e9edf2; --ink-soft: #8a9ab0;
  --line: #1f2b3b; --accent: #f2b632; --accent-ink: #0b111a; --primary: #f2b632; --primary-ink: #0b111a;
  --start: #f2b632; --end: #ff6b57; --route: #f2b632; --casing: #0b111a; --workout: #6fb3ff;
  --quest: #f2b632; --quest-bg: #131d2b; --quest-ink: #c3cdda; --danger: #ff6b57;
  color-scheme: dark;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
/* The panel is a scrolling flex column: never squash its children (text overlapped cards). */
.panel > *, .form > * { flex-shrink: 0; }
html, body { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--ink); font: 15px/1.45 var(--font); overflow: hidden; }
#map { position: fixed; inset: 0; background: var(--bg); }

.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }

/* Panel: left sidebar on desktop, bottom sheet on phones */
.panel {
  position: fixed; top: 16px; left: 16px; bottom: 16px; width: var(--panel-w);
  background: var(--surface); border-radius: var(--radius); box-shadow: 0 8px 30px rgba(10, 20, 50, .14);
  padding: 18px 18px 12px; overflow-y: auto; overflow-x: hidden; display: flex; flex-direction: column; gap: 12px;
  z-index: 2;
}
.panel-handle { display: none; }
.panel-head { display: flex; align-items: center; justify-content: space-between; }
.wordmark { font: 800 22px/1 var(--display); letter-spacing: -0.01em; margin: 0; }
.wordmark span { color: var(--accent); }
.tagline { margin: -6px 0 0; color: var(--ink-soft); }
.tagline:empty, .fineprint:empty { display: none; }

.field { display: flex; flex-direction: column; gap: 4px; min-width: 0; }
.row > .field { flex: 1; }
.field label { font-size: 13px; color: var(--ink-soft); font-weight: 600; }
.hint { font-size: 12px; color: var(--ink-soft); margin: 0; }
.unit { color: var(--ink-soft); }
input, select {
  font: inherit; color: var(--ink); background: var(--field); border: 1px solid transparent;
  border-radius: var(--radius-sm); padding: 9px 10px; width: 100%; min-height: 42px;
}
input:focus-visible, select:focus-visible, button:focus-visible, a:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.row { display: flex; gap: 10px; }
.combo { display: flex; gap: 2px; }
.combo input { border-radius: var(--radius-sm) 0 0 var(--radius-sm); min-width: 0; }
.combo select { width: auto; border-radius: 0 var(--radius-sm) var(--radius-sm) 0; padding-right: 4px; }

.search { position: relative; }
.search-results {
  position: absolute; top: calc(100% + 4px); left: 0; right: 0; margin: 0; padding: 4px; list-style: none;
  background: var(--surface); border: 1px solid var(--line); border-radius: var(--radius-sm); z-index: 5;
  max-height: 280px; overflow-y: auto; box-shadow: 0 8px 24px rgba(10, 20, 50, .16);
}
.search-results li { padding: 8px 10px; border-radius: 8px; cursor: pointer; }
.search-results li small { display: block; color: var(--ink-soft); }
.search-results li[aria-selected="true"], .search-results li:hover { background: var(--field); }

button { font: inherit; cursor: pointer; }
.text-btn { align-self: flex-start; background: none; border: 0; padding: 0; color: var(--accent); margin-top: -4px; font-weight: 600; }
.icon-btn { background: none; border: 0; color: var(--ink-soft); padding: 6px; border-radius: 8px; line-height: 0; }
.icon-btn.small { font-size: 18px; line-height: 1; padding: 2px 6px; }
.icon-btn:hover { color: var(--ink); background: var(--field); }
.primary {
  background: var(--primary); color: var(--primary-ink); border: 0; border-radius: var(--radius-sm);
  padding: 11px 14px; font-weight: 700; min-height: 46px;
}
.primary:disabled { opacity: .4; cursor: not-allowed; }
.secondary {
  background: var(--field); color: var(--ink); border: 0; border-radius: var(--radius-sm);
  padding: 8px 12px; min-height: 40px; font-weight: 600; text-decoration: none; display: inline-flex; align-items: center;
}
.secondary:hover { filter: brightness(.97); }
.music { align-self: flex-start; }
.music::before { content: '♫'; margin-right: 6px; color: var(--accent); }

.segmented { display: flex; border-radius: var(--radius-sm); padding: 3px; background: var(--field); }
.segmented label { flex: 1; position: relative; }
.segmented input { position: absolute; opacity: 0; pointer-events: none; }
.segmented span { display: block; text-align: center; padding: 7px; border-radius: 8px; cursor: pointer; color: var(--ink-soft); font-weight: 600; }
.segmented input:checked + span { background: var(--accent); color: var(--accent-ink); }
.segmented input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 2px; }

.points { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.point { display: flex; align-items: center; gap: 10px; min-height: 32px; }
.point-label { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.dot { width: 12px; height: 12px; border-radius: 50%; flex: none; }
.dot-start { background: var(--start); }
.dot-end { background: var(--end); }

.form { display: flex; flex-direction: column; gap: 12px; }
.summary { display: flex; align-items: center; gap: 10px; background: var(--field); border-radius: 12px; padding: 10px 12px; }
.summary-text { flex: 1; min-width: 0; font-weight: 600; }
/* "Find a route" stays in reach at the bottom of the sheet while the form scrolls. */
.go { position: sticky; bottom: 4px; z-index: 3; box-shadow: 0 -10px 16px -6px var(--surface); }
.progress { height: 4px; border-radius: 2px; background: var(--field); overflow: hidden; }
.progress span { display: block; height: 100%; width: 0; background: var(--accent); transition: width .4s ease; }
.status { margin: 0; min-height: 1.4em; color: var(--ink-soft); font-size: 14px; }
.status[data-kind="error"] { color: var(--danger); }
.results { display: flex; flex-direction: column; gap: 10px; }
.route-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.route-card {
  width: 100%; text-align: left; background: var(--field); color: var(--ink); border: 1.5px solid transparent;
  border-radius: 12px; padding: 10px 12px; display: grid; grid-template-columns: 1fr auto; gap: 2px 10px;
}
.route-card[aria-pressed="true"] { border-color: var(--accent); }
.route-card .dist { font: 700 17px/1.2 var(--display); font-variant-numeric: tabular-nums; }
.route-card .kcal { font: 700 13px/1.2 var(--display); color: var(--accent); align-self: center; }
.route-card .meta { grid-column: 1 / -1; color: var(--ink-soft); font-size: 13px; font-variant-numeric: tabular-nums; }
.route-card .note { grid-column: 1 / -1; color: var(--ink-soft); font-size: 12px; }
.route-card .note.good { color: var(--accent); }
.route-card .note.warn { color: var(--danger); font-weight: 600; }
.text-btn.inline { margin: 0; font-size: inherit; }
.elev { display: block; width: 100%; height: 64px; }
.elev-area { fill: var(--accent); opacity: .15; }
.elev-line { fill: none; stroke: var(--accent); stroke-width: 1.8; stroke-linejoin: round; }
.elev-label { fill: var(--ink-soft); font: 12px var(--font); }
.workout-note { margin: 0; font-size: 13px; border-left: 3px solid var(--workout); padding: 2px 0 2px 10px; }
.legend { list-style: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; gap: 4px 12px; font-size: 12px; color: var(--ink-soft); }
.legend li { display: flex; align-items: center; gap: 5px; }
.legend .key { width: 10px; height: 10px; border-radius: 50%; border: 1.5px solid #fff; flex: none; }
.legend .key.line { width: 16px; height: 4px; border: 0; border-radius: 2px; background: var(--route); }
.legend .key.line.alt { background: #7d8288; }
.legend .key.line.workout { background: var(--workout); }
.stops { margin: 0; font-size: 13px; color: var(--ink-soft); }
.maplibregl-popup-content { font: 13px/1.4 var(--font); color: #141a33; padding: 8px 10px; border-radius: 8px; }
.highlights { display: flex; flex-direction: column; gap: 8px; }
.highlight { background: var(--quest-bg); color: var(--quest-ink); border-radius: 12px; padding: 9px 11px; }
.highlight .kicker { font: 700 12px/1 var(--display); letter-spacing: .06em; text-transform: uppercase; color: var(--quest); }
.highlight h3 { margin: 3px 0 2px; font-size: 14px; color: var(--ink); }
.highlight p { margin: 0 0 3px; font-size: 13px; }
.highlight a { font-size: 12px; color: var(--ink-soft); display: inline-block; padding: 6px 0; }
.highlight[role="button"] { cursor: pointer; }
.highlight[role="button"]:hover { outline: 1.5px solid var(--quest); }
.go-actions { display: flex; flex-direction: column; gap: 8px; }
.result-actions { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.result-actions .text-btn { margin: 0 0 0 auto; }
.panel-foot { margin-top: auto; }
/* Fixed height reserved up front so enabling ads never shifts the layout. */
.ad-slot { min-height: 100px; border-top: 1px solid var(--line); padding-top: 12px; }
.fineprint { font-size: 12px; color: var(--ink-soft); margin: 0; }
.panel-foot { display: flex; flex-direction: column; gap: 6px; }
.legal { font-size: 12px; color: var(--ink-soft); }
.legal a { color: var(--ink-soft); }

/* Map markers */
.marker { width: 18px; height: 18px; border-radius: 50%; border: 3px solid #fff; box-shadow: 0 1px 4px rgba(0,0,0,.35); cursor: grab; }
.marker-start { background: var(--start); }
.marker-end { background: var(--end); }
.marker-me { width: 16px; height: 16px; background: #2f6fde; border-width: 3px; cursor: default; box-shadow: 0 0 0 6px rgba(47, 111, 222, .25); }

dialog { border: 0; border-radius: var(--radius); background: var(--surface); color: var(--ink); padding: 20px; width: min(380px, calc(100vw - 32px)); }
dialog::backdrop { background: rgba(5, 10, 25, .45); }
.settings-form { display: flex; flex-direction: column; gap: 14px; }
.settings-form h2 { margin: 0; font: 800 20px/1.2 var(--display); }
.dialog-actions { display: flex; justify-content: flex-end; }
.qr { align-self: center; background: #fff; padding: 12px; border-radius: 12px; line-height: 0; }
.qr img, .qr canvas { display: block; }
.history-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; max-height: 55vh; overflow-y: auto; }
.history-item { display: flex; align-items: center; gap: 6px; }
.history-open { flex: 1; min-width: 0; text-align: left; background: var(--field); color: var(--ink); border: 0; border-radius: 12px; padding: 10px 12px; }
.history-open strong { display: block; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.history-open span { font-size: 13px; color: var(--ink-soft); font-variant-numeric: tabular-nums; }
.history-empty { color: var(--ink-soft); font-size: 14px; }
.choice-list { display: flex; flex-direction: column; gap: 8px; }
.choice { text-align: left; background: var(--field); color: var(--ink); border: 1.5px solid transparent; border-radius: 12px; padding: 11px 12px; }
.choice strong { display: block; }
.choice span { font-size: 13px; color: var(--ink-soft); }
.choice:hover, .choice:focus-visible { border-color: var(--accent); }

/* Run mode: full-screen map with a stats bar on top and controls at the bottom */
.run { position: fixed; inset: 0; z-index: 10; pointer-events: none; display: flex; flex-direction: column; justify-content: space-between; }
/* On wide screens keep the run card phone-sized instead of stretching edge to edge. */
.run-stats, .run-controls { width: min(440px, calc(100% - 20px)); align-self: center; margin-left: 0; margin-right: 0; }
.run-stat span { display: block; margin-top: 2px; }
.run > * { pointer-events: auto; }
.run-stats {
  background: var(--surface); margin: calc(10px + env(safe-area-inset-top, 0px)) 10px 0; border-radius: var(--radius);
  padding: 12px 14px; display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px 10px; box-shadow: 0 8px 30px rgba(10, 20, 50, .2);
}
.run-time { grid-column: 1 / -1; font: 800 40px/1 var(--display); font-variant-numeric: tabular-nums; }
.run-stat b { display: block; font: 700 20px/1.1 var(--display); font-variant-numeric: tabular-nums; }
.run-stat span { font-size: 12px; color: var(--ink-soft); }
.run-msg { grid-column: 1 / -1; margin: 2px 0 0; font-size: 14px; font-weight: 600; }
.run-msg:empty { display: none; }
.run-hint { grid-column: 1 / -1; margin: 2px 0 0; font-size: 12px; color: var(--ink-soft); }
.run-msg[data-kind="warn"] { color: var(--danger); }
.run-msg[data-kind="done"] { color: var(--accent); }
.run-controls { display: flex; gap: 10px; margin: 0 10px calc(14px + env(safe-area-inset-bottom, 0px)); }
.run-controls button { flex: 1; min-height: 56px; font-size: 17px; justify-content: center; }
#run-voice { flex: 0.8; font-size: 15px; }

/* Search box with the "my location" button inside it */
.search input { padding-right: 44px; }
.locate-btn {
  position: absolute; right: 4px; top: 4px; width: 34px; height: 34px; border: 0; border-radius: 8px;
  background: none; color: var(--accent); display: grid; place-items: center;
}
.locate-btn:hover { background: var(--surface); }
.locate-btn.busy svg { animation: pulse 1s ease-in-out infinite; }
@keyframes pulse { 50% { opacity: .35; } }

/* Desktop light switch: a small physical rocker, sun side and moon side */
.head-tools { display: flex; align-items: center; gap: 6px; }
.lightswitch { background: none; border: 0; padding: 4px; border-radius: 12px; }
.ls-track {
  display: block; position: relative; width: 52px; height: 28px; border-radius: 14px;
  background: linear-gradient(180deg, #cfd6e6, #e9edf5);
  box-shadow: inset 0 2px 4px rgba(20, 30, 60, .35), inset 0 -1px 0 rgba(255, 255, 255, .7);
  transition: background .25s ease;
}
.ls-knob {
  position: absolute; top: 3px; left: 3px; width: 22px; height: 22px; border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #fff 0 30%, #ffd166 31% 100%);
  box-shadow: 0 2px 3px rgba(0, 0, 0, .35), inset 0 -2px 2px rgba(0, 0, 0, .12);
  transition: transform .25s ease, background .25s ease;
}
.lightswitch[aria-checked="true"] .ls-track { background: linear-gradient(180deg, #0a0f18, #1a2536); box-shadow: inset 0 2px 4px rgba(0, 0, 0, .6), inset 0 -1px 0 rgba(255, 255, 255, .08); }
.lightswitch[aria-checked="true"] .ls-knob {
  transform: translateX(24px);
  background: radial-gradient(circle at 62% 38%, #1a2536 0 32%, #f2e6c2 33% 100%);
}
@media (prefers-reduced-motion: reduce) { .ls-track, .ls-knob { transition: none; } }

/* "Which do you prefer?" */
.question { background: var(--field); border-radius: 12px; padding: 10px 12px; display: flex; flex-direction: column; gap: 8px; }
.question-text { margin: 0; font-weight: 700; }
.question-options { display: flex; flex-wrap: wrap; gap: 6px; }
.question-options { flex-direction: column; }
.question-option { text-align: left; background: var(--surface); color: var(--ink); border: 1.5px solid var(--line); border-radius: 12px; padding: 9px 12px; }
.question-option strong { display: block; }
.question-option span { font-size: 13px; color: var(--ink-soft); }
.question-option:hover { border-color: var(--accent); }
.question-options .question-away { flex-basis: 100%; text-align: left; background: none; border: 0; padding: 2px 0 0; color: var(--ink-soft); font-weight: 400; font-size: 13px; text-decoration: underline; }
.question-options .question-away:hover { color: var(--ink); }

.maplibregl-ctrl-attrib { font-size: 11px; }

@media (max-width: 720px) {
  /* Bottom sheet: js/sheet.js sets its height to peek, half or full. */
  .panel {
    top: auto; left: 0; right: 0; bottom: 0; width: auto; height: 50vh;
    border-radius: var(--radius) var(--radius) 0 0;
    padding: 0 16px calc(12px + env(safe-area-inset-bottom));
    transition: height .28s cubic-bezier(.2, .8, .2, 1);
  }
  .panel.dragging { transition: none; }
  .panel-head { touch-action: none; }
  .panel-handle {
    display: block; align-self: stretch; width: 100%; height: 26px; border: 0; background: none; padding: 0; flex: none;
    touch-action: none; cursor: grab;
  }
  .panel-handle::before { content: ""; display: block; width: 36px; height: 4px; margin: 7px auto; border-radius: 2px; background: var(--line); }
  .maplibregl-ctrl-bottom-right, .maplibregl-ctrl-bottom-left { bottom: 118px; }
}
@media (prefers-reduced-motion: reduce) { .panel { transition: none; } }
/* Thumb-sized targets on touch screens (44 px is the usual minimum). */
@media (pointer: coarse) {
  .icon-btn { min-width: 44px; min-height: 44px; display: inline-grid; place-items: center; }
  .icon-btn.small { min-width: 44px; min-height: 40px; }
  .search input { min-height: 46px; }
  .locate-btn { width: 42px; height: 42px; top: 2px; right: 2px; }
  .legal a { display: inline-block; padding: 10px 4px; }
  .point { min-height: 44px; }
}
