/* runloop — desktop is a fixed sidebar beside the map; mobile is a draggable
   bottom sheet over a full-bleed map. */

:root {
  --bg: #0e1116;
  --panel: #151922;
  --sunken: #1c212c;
  --line: #262d3a;
  --text: #e8ecf3;
  --muted: #8d97a9;
  --accent: #4ade80;
  --accent-ink: #06240f;
  --warn: #fbbf24;
  --danger: #f87171;
  --shadow: 0 6px 28px rgb(0 0 0 / 0.5);
  --r: 11px;
  --tap: 44px; /* minimum comfortable touch target */
  color-scheme: dark;
}

@media (prefers-color-scheme: light) {
  :root {
    --bg: #eef1f6;
    --panel: #ffffff;
    --sunken: #f3f5f9;
    --line: #e0e5ee;
    --text: #131720;
    --muted: #5f6979;
    --accent: #15a349;
    --accent-ink: #ffffff;
    --warn: #b45309;
    --danger: #dc2626;
    --shadow: 0 6px 28px rgb(20 30 50 / 0.16);
    color-scheme: light;
  }
}

* { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  font: 15px/1.5 ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  display: grid;
  grid-template-columns: 380px 1fr;
  overflow: hidden;
  /* Stops iOS Safari double-tap-to-zoom eating button taps. */
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

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

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

#map { grid-column: 2; grid-row: 1; height: 100dvh; background: var(--bg); }
.leaflet-container { background: var(--bg); font: inherit; }
.leaflet-control-attribution { display: none; }
.leaflet-control-zoom a { width: 32px; height: 32px; line-height: 32px; }

#map-style {
  position: absolute;
  top: max(14px, env(safe-area-inset-top));
  right: 14px;
  z-index: 500;
  display: flex;
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 9px;
  overflow: hidden;
  box-shadow: var(--shadow);
}
#map-style button {
  border: 0;
  border-radius: 0;
  padding: 8px 14px;
  font-size: 12.5px;
  color: var(--muted);
  background: transparent;
}
#map-style button + button { border-left: 1px solid var(--line); }
#map-style button.on { color: var(--text); background: var(--sunken); font-weight: 600; }

.start-pin {
  width: 20px; height: 20px;
  border-radius: 50%;
  background: var(--accent);
  border: 3px solid var(--panel);
  box-shadow: 0 0 0 1px var(--accent), 0 2px 8px rgb(0 0 0 / .5);
  cursor: grab;
}
.arrow { display: grid; place-items: center; pointer-events: none; }

/* ---------- panel ---------- */

#panel {
  grid-column: 1;
  grid-row: 1;
  background: var(--panel);
  border-right: 1px solid var(--line);
  padding: 20px;
  overflow-y: auto;
  overscroll-behavior: contain;
  display: flex;
  flex-direction: column;
  gap: 18px;
}

#grip { display: none; }

h1 {
  margin: 0;
  font-size: 21px;
  letter-spacing: -0.02em;
  display: flex;
  align-items: center;
  gap: 8px;
}
h1 .mark { color: var(--accent); font-size: 17px; }
.tag { margin: 3px 0 0; color: var(--muted); font-size: 13px; }

h2 {
  margin: 0 0 10px;
  font-size: 13px;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 8px;
}

.num {
  display: grid;
  place-items: center;
  width: 19px; height: 19px;
  border-radius: 50%;
  background: var(--sunken);
  border: 1px solid var(--line);
  color: var(--muted);
  font-size: 11px;
  font-weight: 700;
}
.step.done .num { background: var(--accent); border-color: var(--accent); color: var(--accent-ink); }

.hint { margin: 9px 0 0; font-size: 12.5px; color: var(--muted); }

/* ---------- controls ---------- */

button, input, select {
  font: inherit;
  color: var(--text);
  background: transparent;
  border: 1px solid var(--line);
  border-radius: var(--r);
  padding: 9px 12px;
}

button { cursor: pointer; transition: border-color .12s, background .12s, color .12s; }
button:hover { border-color: var(--muted); }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

.secondary {
  width: 100%;
  min-height: var(--tap);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
}
.secondary .pin { color: var(--accent); font-size: 15px; }

.primary {
  position: relative;
  background: var(--accent);
  color: var(--accent-ink);
  border-color: var(--accent);
  font-weight: 650;
  font-size: 15px;
  min-height: 48px;
  width: 100%;
}
.primary:hover { filter: brightness(1.07); border-color: var(--accent); }
.primary:disabled { cursor: progress; filter: saturate(.5) brightness(.9); }
.primary .spinner { display: none; }
.primary.busy .label { visibility: hidden; }
.primary.busy .spinner {
  display: block;
  position: absolute;
  inset: 0;
  margin: auto;
  width: 17px; height: 17px;
  border: 2px solid color-mix(in srgb, var(--accent-ink) 35%, transparent);
  border-top-color: var(--accent-ink);
  border-radius: 50%;
  animation: spin .7s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
@media (prefers-reduced-motion: reduce) {
  * { animation-duration: .01ms !important; transition-duration: .01ms !important; }
}

.chips { display: grid; grid-template-columns: repeat(4, 1fr); gap: 7px; margin-bottom: 9px; }
.chips button {
  min-height: var(--tap);
  padding: 8px 0;
  font-size: 16px;
  font-weight: 600;
  line-height: 1.1;
  background: var(--sunken);
}
.chips button small { display: block; font-size: 10px; font-weight: 500; color: var(--muted); }
.chips button.on {
  border-color: var(--accent);
  color: var(--accent);
  background: color-mix(in srgb, var(--accent) 13%, transparent);
}
.chips button.on small { color: inherit; opacity: .8; }

.stepper { display: flex; align-items: stretch; gap: 7px; }
.stepper > button {
  width: var(--tap);
  min-height: var(--tap);
  font-size: 20px;
  line-height: 1;
  padding: 0;
  background: var(--sunken);
}
.readout {
  flex: 1;
  display: flex;
  align-items: baseline;
  justify-content: center;
  gap: 4px;
  border: 1px solid var(--line);
  border-radius: var(--r);
  background: var(--sunken);
  padding: 7px;
}
#km {
  width: 66px;
  border: 0;
  background: transparent;
  padding: 0;
  text-align: right;
  font-size: 19px;
  font-weight: 650;
  font-variant-numeric: tabular-nums;
}
#km::-webkit-outer-spin-button, #km::-webkit-inner-spin-button { appearance: none; margin: 0; }
#km { -moz-appearance: textfield; }
.unit { color: var(--muted); font-size: 13px; }

details#opts {
  border: 1px solid var(--line);
  border-radius: var(--r);
  padding: 0 13px;
  background: var(--sunken);
}
details#opts summary {
  cursor: pointer;
  padding: 13px 0;
  font-size: 13px;
  font-weight: 600;
  color: var(--muted);
  list-style: none;
}
details#opts summary::-webkit-details-marker { display: none; }
details#opts summary::after { content: " ▾"; }
details#opts[open] summary { border-bottom: 1px solid var(--line); margin-bottom: 11px; }
details#opts[open] summary::after { content: " ▴"; }
details#opts > *:last-child { margin-bottom: 13px; }

.check {
  display: flex;
  align-items: center;
  gap: 10px;
  font-size: 14px;
  cursor: pointer;
  min-height: var(--tap);
}
.check input { width: 18px; height: 18px; accent-color: var(--accent); padding: 0; flex: none; }

.field { display: flex; align-items: center; gap: 8px; min-height: var(--tap); }
.field label { color: var(--muted); font-size: 13px; flex: 1; }
.field input, .field select { padding: 7px 9px; background: var(--panel); }
#pace { width: 68px; text-align: center; font-variant-numeric: tabular-nums; }

#status { margin: -6px 0 0; font-size: 12.5px; color: var(--muted); min-height: 1.3em; }
#status.error { color: var(--danger); }

/* ---------- results ---------- */

#results { display: flex; flex-direction: column; gap: 8px; }

.card {
  border: 1px solid var(--line);
  border-left: 4px solid var(--dot);
  border-radius: var(--r);
  padding: 12px 13px;
  cursor: pointer;
  transition: border-color .12s, background .12s;
}
.card:hover { background: var(--sunken); }
.card.on { border-color: var(--dot); background: color-mix(in srgb, var(--dot) 12%, transparent); }

.card .top { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; }
.card .dist { font-size: 20px; font-weight: 650; letter-spacing: -0.02em; font-variant-numeric: tabular-nums; }
.card .time { font-size: 13px; color: var(--muted); font-variant-numeric: tabular-nums; }

.badges { display: flex; flex-wrap: wrap; gap: 5px; margin-top: 8px; }
.badge {
  font-size: 11px;
  padding: 3px 7px;
  border-radius: 6px;
  background: var(--sunken);
  color: var(--muted);
  border: 1px solid var(--line);
}
.badge.spot { color: var(--accent); border-color: color-mix(in srgb, var(--accent) 45%, transparent); }
.badge.off { color: var(--warn); border-color: color-mix(in srgb, var(--warn) 45%, transparent); }

.card .gpx { margin-top: 10px; width: 100%; min-height: var(--tap); font-size: 13px; font-weight: 600; }
.card.on .gpx { border-color: var(--muted); }

footer { margin-top: auto; padding-top: 4px; font-size: 11px; color: var(--muted); }
footer a { color: var(--muted); }

/* ---------- mobile: map fills the screen, panel is a sheet over it ---------- */

@media (max-width: 820px) {
  body {
    display: block;
    overflow: hidden;
  }

  #map {
    position: fixed;
    inset: 0;
    height: 100dvh;
    width: 100vw;
  }

  #panel {
    position: fixed;
    left: 0;
    right: 0;
    bottom: 0;
    z-index: 600;
    border-right: none;
    border-top: 1px solid var(--line);
    border-radius: 18px 18px 0 0;
    box-shadow: var(--shadow);
    padding: 0 16px calc(16px + env(safe-area-inset-bottom));
    gap: 16px;
    transition: max-height .22s ease;
  }

  /* Three snap heights. "search" shows the whole form; "results" hands half the
     screen back to the map so you can see the route you are picking; "full" is
     the flick-up state for reading everything. */
  #panel[data-sheet="search"]  { max-height: 62dvh; }
  #panel[data-sheet="results"] { max-height: 50dvh; }
  #panel[data-sheet="full"]    { max-height: 92dvh; }

  /* Grab handle: tap to toggle, drag to flick between the two heights. */
  #grip {
    display: block;
    position: sticky;
    top: 0;
    z-index: 1;
    width: calc(100% + 32px);
    margin: 0 -16px;
    padding: 10px 0 6px;
    border: 0;
    border-radius: 18px 18px 0 0;
    background: var(--panel);
    touch-action: none;
  }
  #grip .bar {
    display: block;
    width: 40px; height: 4px;
    margin: 0 auto;
    border-radius: 2px;
    background: var(--line);
  }

  header { margin-top: -6px; }
  h1 { font-size: 19px; }
  /* The tagline is decoration; the map is more valuable on a phone. */
  .tag { display: none; }

  #map-style {
    top: max(12px, env(safe-area-inset-top));
    right: 12px;
  }
  .leaflet-control-zoom { display: none; } /* pinch to zoom instead */

  footer { padding-bottom: 4px; }
}

/* Landscape phones: almost no vertical room, so go back to side-by-side. */
@media (max-width: 820px) and (orientation: landscape) and (max-height: 520px) {
  #panel,
  #panel[data-sheet] { max-height: 100dvh; width: 320px; right: auto; top: 0; border-radius: 0; }
  #grip { display: none; }
}
