/* Race ETA Planner: map-first, high contrast, large numbers. */

:root {
  --bg: #f3f4f6;
  --surface: #ffffff;
  --surface-2: #f6f7f9;
  --text: #0f1115;
  --muted: #5b6270;
  --border: #e2e5ea;
  --accent: #ff4f1a;
  --accent-ink: #ffffff;
  --accent-soft: #fff0ea;
  --focus: #1a5cff;
  --danger: #c62828;
  --header-bg: #0f1115;
  --header-ink: #ffffff;
  --shadow: 0 1px 2px rgba(15, 17, 21, .06), 0 8px 24px rgba(15, 17, 21, .08);
  --radius: 14px;
  --sidebar: 368px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #0b0d10;
    --surface: #15181d;
    --surface-2: #1c2027;
    --text: #f2f4f7;
    --muted: #a3aab7;
    --border: #2a2f38;
    --accent: #ff6a3d;
    --accent-ink: #120700;
    --accent-soft: #2a1710;
    --focus: #7aa2ff;
    --danger: #ff7a70;
    --header-bg: #000000;
    --shadow: 0 1px 2px rgba(0, 0, 0, .4), 0 8px 24px rgba(0, 0, 0, .35);
    color-scheme: dark;
  }
}

*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: var(--font);
  background: var(--bg);
  color: var(--text);
  -webkit-font-smoothing: antialiased;
  -webkit-text-size-adjust: 100%;
}
h1, h2, p { margin: 0; }
svg { flex: none; }
[hidden] { display: none !important; }

.visually-hidden {
  position: absolute !important; width: 1px; height: 1px; padding: 0; margin: -1px;
  overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; border: 0;
}

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

/* ------------------------------------------------------------------ layout */

.app {
  display: grid;
  grid-template-columns: var(--sidebar) 1fr;
  grid-template-rows: auto 1fr;
  grid-template-areas: "header header" "sidebar map";
  height: 100vh;
  height: 100dvh;
}

.app-header {
  grid-area: header;
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 16px;
  padding-top: max(10px, env(safe-area-inset-top));
  background: var(--header-bg);
  color: var(--header-ink);
  z-index: 1001;
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.brand-mark { width: 26px; height: 26px; color: var(--accent); }
.brand h1 { font-size: 1.05rem; font-weight: 750; letter-spacing: -.01em; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.app-header .btn-ghost { color: var(--header-ink); border-color: rgba(255,255,255,.22); background: transparent; }
.app-header .btn-ghost:hover { background: rgba(255,255,255,.08); }

.map-wrap { grid-area: map; position: relative; min-height: 0; background: var(--surface-2); }
#map { position: absolute; inset: 0; background: var(--surface-2); }

.sidebar {
  grid-area: sidebar;
  overflow-y: auto;
  padding: 14px;
  display: flex; flex-direction: column; gap: 12px;
  border-right: 1px solid var(--border);
  background: var(--bg);
}

/* ------------------------------------------------------------------ cards */

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
}
.card-title {
  font-size: .78rem; font-weight: 700; letter-spacing: .08em; text-transform: uppercase;
  color: var(--muted); margin-bottom: 12px;
}
.note { color: var(--muted); font-size: .88rem; line-height: 1.4; margin-top: 8px; }
.track-name { font-weight: 650; font-size: 1.02rem; margin-bottom: 10px; overflow-wrap: anywhere; }

/* ------------------------------------------------------------------ buttons */

.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 44px; padding: 0 16px;
  border-radius: 12px; border: 1px solid transparent;
  font: inherit; font-weight: 650; font-size: .95rem;
  cursor: pointer; user-select: none; text-decoration: none;
  transition: background-color .15s, border-color .15s, transform .05s;
}
.btn:active { transform: translateY(1px); }
.btn svg { width: 18px; height: 18px; }
.btn-primary { background: var(--accent); color: var(--accent-ink); }
.btn-primary:hover { filter: brightness(1.05); }
.btn-ghost { background: var(--surface); color: var(--text); border-color: var(--border); }
.btn-ghost:hover { background: var(--surface-2); }
.btn-link { background: none; color: var(--text); text-decoration: underline; text-underline-offset: 3px; font-weight: 600; }
.btn-sm { min-height: 44px; padding: 0 12px; font-size: .88rem; }
.btn-block { width: 100%; margin-top: 12px; }
.btn-row { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 12px; }

.icon-btn {
  display: inline-grid; place-items: center;
  width: 44px; height: 44px; flex: none;
  border: 0; border-radius: 12px; background: transparent; color: var(--muted); cursor: pointer;
}
.icon-btn:hover { background: var(--surface-2); color: var(--text); }
.icon-btn svg { width: 20px; height: 20px; }

.chip {
  min-height: 44px; min-width: 56px; padding: 0 12px;
  border-radius: 999px; border: 1px solid var(--border);
  background: var(--surface); color: var(--text);
  font: inherit; font-weight: 650; font-variant-numeric: tabular-nums; cursor: pointer;
}
.chip:hover { border-color: var(--muted); }
.chip[aria-pressed="true"] { background: var(--text); color: var(--surface); border-color: var(--text); }

/* ------------------------------------------------------------------ form */

.race-form { display: flex; flex-direction: column; gap: 12px; }
.field-row { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.field { display: flex; flex-direction: column; gap: 6px; min-width: 0; }
.field-label { font-size: .85rem; font-weight: 600; color: var(--muted); }
.field-label .hint { font-weight: 500; opacity: .8; margin-left: 4px; }
.field input, .save-form input, .jump-form input {
  width: 100%; min-height: 48px; padding: 10px 12px;
  border: 1.5px solid var(--border); border-radius: 12px;
  background: var(--surface-2); color: var(--text);
  font: inherit; font-size: 1.05rem; font-weight: 600; font-variant-numeric: tabular-nums;
  -webkit-appearance: none; appearance: none;
}
.field input:focus, .save-form input:focus, .jump-form input:focus { border-color: var(--focus); outline: none; box-shadow: 0 0 0 3px color-mix(in srgb, var(--focus) 30%, transparent); }
.field input[aria-invalid="true"] { border-color: var(--danger); }
#target-time { font-size: 1.35rem; letter-spacing: .02em; }
.field-error { color: var(--danger); font-size: .85rem; font-weight: 600; }
.presets { display: flex; flex-wrap: wrap; gap: 8px; }

.kv { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; margin: 14px 0 0; }
.kv > div { background: var(--surface-2); border-radius: 12px; padding: 10px 12px; min-width: 0; }
.kv dt { font-size: .78rem; color: var(--muted); font-weight: 600; }
.kv dd { margin: 2px 0 0; font-size: 1.1rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; font-weight: 750; font-variant-numeric: tabular-nums; }
.kv dd small { font-size: .75rem; color: var(--muted); font-weight: 600; }

.toggle-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-top: 12px; flex-wrap: wrap; }
.switch { display: inline-flex; align-items: center; gap: 10px; min-height: 44px; cursor: pointer; font-weight: 600; font-size: .92rem; position: relative; }
.switch input { position: absolute; opacity: 0; width: 44px; height: 26px; margin: 0; cursor: pointer; }
.switch-track { width: 44px; height: 26px; border-radius: 999px; background: var(--border); position: relative; transition: background .15s; flex: none; }
.switch-track::after { content: ""; position: absolute; top: 3px; left: 3px; width: 20px; height: 20px; border-radius: 50%; background: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.3); transition: transform .15s; }
.switch input:checked + .switch-track { background: var(--accent); }
.switch input:checked + .switch-track::after { transform: translateX(18px); }
.switch input:focus-visible + .switch-track { outline: 3px solid var(--focus); outline-offset: 2px; }

/* ------------------------------------------------------------------ result */

.result { border: 2px solid var(--text); padding: 14px 16px 16px; }
.result-hint { font-size: 1.05rem; line-height: 1.4; }
.result-warn { font-size: 1.1rem; font-weight: 700; color: var(--danger); margin: 6px 0 4px; }
.result-top { display: flex; align-items: flex-start; justify-content: space-between; gap: 8px; margin: -4px -8px 0 0; }
.km { display: flex; align-items: baseline; gap: 8px; }
.km-label { font-size: .85rem; font-weight: 800; letter-spacing: .1em; color: var(--accent); }
.km-value { font-size: 2rem; font-weight: 800; letter-spacing: -.02em; font-variant-numeric: tabular-nums; line-height: 1.1; }

.eta-block { margin: 2px 0 12px; }
.eta-time { display: flex; align-items: baseline; flex-wrap: wrap; gap: 0 2px; font-variant-numeric: tabular-nums; line-height: 1; }
.eta-hm { font-size: clamp(3.4rem, 9vw, 4.4rem); font-weight: 850; letter-spacing: -.035em; }
.eta-sec { font-size: 1.6rem; font-weight: 700; color: var(--muted); }
.eta-day { margin-left: 10px; font-size: .85rem; font-weight: 700; background: var(--accent-soft); color: var(--accent); padding: 4px 8px; border-radius: 999px; align-self: center; }
.eta-label { font-size: .95rem; font-weight: 650; margin-top: 6px; }
.muted { color: var(--muted); font-weight: 500; }

.stats { display: grid; grid-template-columns: repeat(auto-fit, minmax(110px, 1fr)); gap: 8px; margin: 0 0 12px; }
.stats > div { background: var(--surface-2); border-radius: 12px; padding: 8px 12px; }
.stats dt { font-size: .75rem; color: var(--muted); font-weight: 650; }
.stats dd { margin: 2px 0 0; font-size: 1.15rem; font-weight: 750; font-variant-numeric: tabular-nums; }

.also { margin: 0 0 12px; padding: 10px 12px; border-radius: 12px; background: var(--accent-soft); }
.also-label { display: block; font-size: .82rem; font-weight: 700; color: var(--accent); margin-bottom: 8px; }
.also-list { display: flex; flex-wrap: wrap; gap: 8px; }
.chip-alt { background: var(--surface); }

.save-form { display: flex; gap: 8px; }
.save-form input { flex: 1; min-width: 0; font-size: 1rem; font-weight: 500; }
.save-form .btn { flex: none; }

.jump-form { display: flex; align-items: center; gap: 8px; margin-top: 12px; padding-top: 12px; border-top: 1px solid var(--border); }
.jump-form label { font-size: .88rem; font-weight: 650; color: var(--muted); white-space: nowrap; }
.jump-form input { min-height: 44px; font-size: 1rem; max-width: 120px; }

/* ------------------------------------------------------------------ spectator points */

.points { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }
.point { display: flex; align-items: center; gap: 4px; background: var(--surface-2); border-radius: 12px; border: 2px solid transparent; }
.point.is-selected { border-color: var(--accent); }
.point-main {
  flex: 1; min-width: 0; min-height: 56px; display: flex; align-items: center; gap: 12px;
  padding: 8px 12px; border: 0; background: transparent; color: inherit; font: inherit; text-align: left; cursor: pointer; border-radius: 12px;
}
.point-eta { font-size: 1.5rem; font-weight: 800; font-variant-numeric: tabular-nums; letter-spacing: -.02em; min-width: 4.2ch; }
.point-meta { display: flex; flex-direction: column; min-width: 0; }
.point-name { font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.point-sub { font-size: .82rem; color: var(--muted); font-variant-numeric: tabular-nums; }

.privacy-footer { display: flex; gap: 8px; align-items: flex-start; font-size: .82rem; color: var(--muted); line-height: 1.4; padding: 4px 4px 12px; }
.privacy-footer svg { width: 16px; height: 16px; margin-top: 1px; }

/* ------------------------------------------------------------------ map overlays */

.empty-state { position: absolute; inset: 0; display: grid; place-items: center; padding: 16px; z-index: 900; background: color-mix(in srgb, var(--bg) 55%, transparent); backdrop-filter: blur(2px); }
.empty-card {
  background: var(--surface); border: 2px dashed var(--border); border-radius: 20px;
  padding: 28px 24px; max-width: 360px; width: 100%;
  display: flex; flex-direction: column; align-items: center; gap: 10px; text-align: center; box-shadow: var(--shadow);
}
.empty-icon { width: 56px; height: 56px; color: var(--accent); }
.empty-title { font-size: 1.3rem; font-weight: 800; letter-spacing: -.01em; }
.empty-sub { color: var(--muted); margin-bottom: 6px; }
.empty-card { position: relative; max-height: calc(100% - 8px); overflow-y: auto; }
.empty-card .btn-primary { width: 100%; }
.picker-close { position: absolute; top: 8px; right: 8px; }
@media (pointer: coarse) { .drop-hint { display: none; } }
.course-list { list-style: none; margin: 4px 0 0; padding: 0; width: 100%; display: flex; flex-direction: column; gap: 8px; }
.course-btn {
  width: 100%; min-height: 56px; padding: 10px 14px; text-align: left; cursor: pointer;
  display: flex; flex-direction: column; gap: 2px;
  background: var(--surface-2); color: var(--text); font: inherit;
  border: 2px solid var(--border); border-radius: 12px;
}
.course-btn:hover { border-color: var(--muted); }
.course-btn[aria-current="true"] { border-color: var(--accent); }
.course-name { font-weight: 750; }
.course-meta { font-size: .85rem; color: var(--muted); font-variant-numeric: tabular-nums; }
.picker-or { width: 100%; display: flex; align-items: center; gap: 10px; color: var(--muted); font-size: .82rem; margin: 6px 0 2px; }
.picker-or::before, .picker-or::after { content: ""; flex: 1; height: 1px; background: var(--border); }
.privacy-note { font-size: .82rem; color: var(--muted); line-height: 1.45; margin-top: 6px; }

.drop-overlay {
  position: fixed; inset: 0; z-index: 2000; display: grid; place-items: center;
  background: color-mix(in srgb, var(--accent) 22%, transparent); border: 4px dashed var(--accent);
  pointer-events: none;
}
.drop-overlay span { background: var(--accent); color: var(--accent-ink); font-weight: 800; font-size: 1.3rem; padding: 14px 22px; border-radius: 14px; }
.map-loading { position: absolute; top: 12px; left: 50%; transform: translateX(-50%); z-index: 950; background: var(--text); color: var(--surface); padding: 8px 14px; border-radius: 999px; font-weight: 700; font-size: .9rem; }

.toast {
  position: fixed; left: 50%; bottom: max(20px, env(safe-area-inset-bottom)); transform: translateX(-50%);
  z-index: 3000; max-width: calc(100vw - 32px);
  background: var(--text); color: var(--surface);
  padding: 12px 18px; border-radius: 12px; font-weight: 650; box-shadow: var(--shadow);
}
.toast.is-error { background: var(--danger); color: #fff; }

/* ------------------------------------------------------------------ leaflet markers */

.leaflet-container { font-family: var(--font); background: var(--surface-2); }
/* Map controls use the app's tokens so they keep contrast in light and dark mode.
   (A blanket `.leaflet-container a { color: inherit }` made the zoom glyphs light-on-white in dark mode.) */
.leaflet-bar, .leaflet-touch .leaflet-bar { border: 1px solid var(--border); box-shadow: var(--shadow); border-radius: 12px; overflow: hidden; }
.leaflet-bar a, .leaflet-touch .leaflet-bar a {
  width: 44px; height: 44px; line-height: 44px;
  background: var(--surface); color: var(--text);
  border-bottom: 1px solid var(--border);
  font-size: 22px; font-weight: 700;
}
.leaflet-bar a:last-child { border-bottom: 0; }
.leaflet-bar a:hover, .leaflet-bar a:focus-visible { background: var(--surface-2); color: var(--text); }
.leaflet-bar a.leaflet-disabled { background: var(--surface-2); color: var(--muted); opacity: .6; }
.leaflet-control-attribution { background: color-mix(in srgb, var(--surface) 85%, transparent) !important; color: var(--muted); }
.leaflet-control-attribution a { color: var(--text); }
.leaflet-popup-content a { color: inherit; }

.sf-marker span {
  display: grid; place-items: center; width: 28px; height: 28px; border-radius: 50%;
  font-weight: 850; font-size: 13px; color: #fff; border: 3px solid #fff; box-shadow: 0 1px 4px rgba(0,0,0,.45);
}
.sf-marker.start span { background: #0f9d58; }
.sf-marker.finish span { background: #0f1115; }

.km-marker span {
  display: grid; place-items: center; width: 22px; height: 22px; border-radius: 50%;
  background: #fff; color: #0f1115; border: 2px solid #0f1115;
  font-size: 10px; font-weight: 800; font-variant-numeric: tabular-nums;
}

.sel-marker span {
  display: block; width: 30px; height: 30px; border-radius: 50%;
  background: var(--accent); border: 4px solid #fff;
  box-shadow: 0 0 0 3px rgba(15,17,21,.85), 0 3px 10px rgba(0,0,0,.4);
  cursor: grab;
}
.sel-marker:focus-visible { outline: none; }
.sel-marker:focus-visible span { box-shadow: 0 0 0 3px var(--focus), 0 3px 10px rgba(0,0,0,.4); }

.spot-marker { position: relative; }
.spot-marker .dot { position: absolute; inset: 0; border-radius: 50%; background: #1a5cff; border: 3px solid #fff; box-shadow: 0 1px 4px rgba(0,0,0,.45); }
.spot-marker .lbl {
  position: absolute; left: 20px; top: 50%; transform: translateY(-50%);
  background: #fff; color: #0f1115; padding: 2px 8px; border-radius: 8px; white-space: nowrap;
  font-size: 12px; font-weight: 700; box-shadow: 0 1px 4px rgba(0,0,0,.3);
  max-width: 160px; overflow: hidden; text-overflow: ellipsis;
}

.pp { min-width: 150px; font-family: var(--font); }
.pp-km { font-weight: 850; font-size: 1.05rem; margin-bottom: 4px; }
.pp-row { display: flex; justify-content: space-between; gap: 12px; font-variant-numeric: tabular-nums; }
.pp-row span { color: #5b6270; }

/* ------------------------------------------------------------------ tablet and mobile */

@media (max-width: 900px) {
  .app { display: block; height: auto; min-height: 100dvh; }
  .app-header { position: sticky; top: 0; }
  .map-wrap { height: 55vh; height: 55svh; min-height: 300px; }
  .sidebar {
    border-right: 0; overflow: visible; padding: 0 12px 12px;
    display: grid; grid-template-columns: 1fr 1fr; align-items: start; gap: 12px;
  }
  .sidebar > * { grid-column: 1 / -1; }
  /* Result card as a bottom sheet pulled over the lower edge of the map. */
  .result {
    position: relative; z-index: 950; margin-top: -18px;
    border-radius: 20px 20px var(--radius) var(--radius);
    border-width: 0; border-top: 4px solid var(--accent);
    box-shadow: 0 -8px 24px rgba(0,0,0,.18);
  }
  .result::before { content: ""; display: block; width: 40px; height: 4px; border-radius: 4px; background: var(--border); margin: -6px auto 10px; }
  .app[data-state="EMPTY"] .sidebar { padding-top: 12px; }
}

@media (min-width: 600px) and (max-width: 900px) {
  .sidebar > .card:not(.result) { grid-column: auto; }
  .sidebar > #points-card { grid-column: 1 / -1; }
}

@media (max-width: 599px) {
  .brand h1 { font-size: .98rem; }
  .app-header .btn-ghost span { display: none; }
  .app-header .btn-ghost { padding: 0 12px; }
  .app-header .btn-ghost svg { width: 20px; height: 20px; }
  .app-header .btn-ghost::after { content: "GPX"; font-size: .9rem; }
  .card { padding: 14px; }
  .km-value { font-size: 1.8rem; }
  .save-form { flex-direction: column; }
  .save-form .btn { width: 100%; }
}

@media (prefers-reduced-motion: reduce) {
  * { transition: none !important; scroll-behavior: auto !important; }
}
