:root {
  --bg: #f4f5f7;
  --card: #ffffff;
  --ink: #17202c;
  --muted: #5d6877;
  --line: #dfe3e8;
  --accent: #16325c;
  --live: #0a7d3c;
  --live-bg: #e2f4e9;
  --sched: #6b7480;
  --sched-bg: #eceef1;
  --dep-bg: #e8f0fb;
  --dep-ink: #16325c;
  --warn-bg: #fff3d6;
  --warn-ink: #6b4a00;
  --bad: #b3261e;
  --radius: 12px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f141b; --card: #182029; --ink: #e8ecf1; --muted: #9aa5b3; --line: #2a3542;
    --accent: #8fb4ff; --live: #5fd38d; --live-bg: #123323; --sched: #a5aeb9; --sched-bg: #252e39;
    --warn-bg: #3a2e10; --warn-ink: #ffd877; --bad: #ff8a80; --dep-bg: #1b2a3f; --dep-ink: #d6e4ff; color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f141b; --card: #182029; --ink: #e8ecf1; --muted: #9aa5b3; --line: #2a3542;
  --accent: #8fb4ff; --live: #5fd38d; --live-bg: #123323; --sched: #a5aeb9; --sched-bg: #252e39;
  --warn-bg: #3a2e10; --warn-ink: #ffd877; --bad: #ff8a80; --dep-bg: #1b2a3f; --dep-ink: #d6e4ff; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg); color: var(--ink);
  font: 16px/1.4 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  min-height: 100dvh; display: flex; flex-direction: column;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) 0 env(safe-area-inset-left);
}
a { color: var(--accent); }
button { font: inherit; color: inherit; }

.top {
  position: sticky; top: 0; z-index: 1000;
  display: flex; align-items: center; gap: 10px; padding: 10px 16px;
  background: var(--card); border-bottom: 1px solid var(--line);
}
.top h1 { font-size: 18px; margin: 0; white-space: nowrap; }
.tabs { display: flex; gap: 4px; margin-right: auto; }
.tabs button {
  border: 0; background: none; padding: 6px 10px; border-radius: 8px; cursor: pointer; color: var(--muted);
}
.tabs button[aria-selected="true"] { background: var(--sched-bg); color: var(--ink); font-weight: 600; }
.status {
  display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--line); background: none;
  border-radius: 999px; padding: 3px 10px; font-size: 13px; color: var(--muted); cursor: pointer; white-space: nowrap;
}
.status .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--sched); }
.status.live { color: var(--live); border-color: var(--live); }
.status.live .dot { background: var(--live); animation: pulse 2s infinite; }
.status.bad { color: var(--bad); border-color: var(--bad); }
.status.bad .dot { background: var(--bad); }
@keyframes pulse { 50% { opacity: .35; } }
@media (prefers-reduced-motion: reduce) { .status.live .dot { animation: none; } }
@media (max-width: 380px) { .top h1 { display: none; } }

.banner { background: var(--warn-bg); color: var(--warn-ink); padding: 8px 16px; font-size: 14px; }
main { flex: 1; display: flex; flex-direction: column; }
.view { padding: 12px 16px; max-width: 640px; width: 100%; margin: 0 auto; }
#view-map { padding: 0; max-width: none; flex: 1; position: relative; display: flex; flex-direction: column; }
#view-map[hidden] { display: none; }

/* pinned cards */
.card {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 12px; margin-bottom: 10px;
}
.card-head { display: flex; align-items: center; gap: 8px; }
.card-head .where { flex: 1; min-width: 0; }
.card-head .stop { font-weight: 600; }
.card-head .dir, .town { color: var(--muted); font-size: 14px; }
.badge {
  display: inline-flex; align-items: center; justify-content: center; min-width: 30px; height: 26px; padding: 0 6px;
  border-radius: 6px; font-weight: 700; font-size: 15px; flex: none;
}
.deps { display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap; }
.dep {
  flex: 1 1 0; min-width: 92px; border-radius: 8px; padding: 6px 8px; background: var(--dep-bg); color: var(--dep-ink);
}
.dep .in { font-size: 20px; font-weight: 700; font-variant-numeric: tabular-nums; }
.dep .meta { font-size: 12px; opacity: .8; display: flex; gap: 6px; align-items: center; flex-wrap: wrap; }
.dep.cancel .in { text-decoration: line-through; color: var(--bad); }
.cancel-tag { color: var(--bad); font-weight: 700; }
.gps { display: inline-flex; align-items: center; gap: 4px; margin-top: 3px; font-size: 12px; font-weight: 700; color: var(--live); white-space: nowrap; }
.gps-ico { width: 12px; height: 12px; flex: none; }
.none { color: var(--muted); font-size: 14px; margin-top: 8px; }
.alert { margin-top: 8px; font-size: 13px; background: var(--warn-bg); color: var(--warn-ink); border-radius: 8px; padding: 6px 8px; white-space: pre-line; }
.alert summary { cursor: pointer; font-weight: 600; }

.icon-btn {
  border: 0; background: none; font-size: 22px; line-height: 1; padding: 4px 6px; cursor: pointer; color: var(--muted);
}
.icon-btn.on { color: #e0a100; }
.pin-label { font-weight: 700; font-size: 17px; margin-bottom: 6px; }
.pin-name {
  width: 100%; font: inherit; font-size: 16px; padding: 6px 10px; margin-bottom: 8px; border: 1px solid var(--line);
  border-radius: 8px; background: var(--bg); color: var(--ink);
}
.edit-tools { display: flex; gap: 2px; }
.empty { background: var(--card); border: 1px dashed var(--line); border-radius: var(--radius); padding: 16px; }
.home-tools { display: flex; gap: 8px; flex-wrap: wrap; margin: 4px 0 10px; }
.home-tools button, .sheet button.txt {
  border: 1px solid var(--line); background: var(--card); border-radius: 8px; padding: 6px 12px; cursor: pointer;
}
.legend { color: var(--muted); font-size: 13px; }

/* map */
.line-chips {
  display: flex; gap: 6px; padding: 8px 16px; overflow-x: auto; background: var(--card); border-bottom: 1px solid var(--line);
}
.line-chips button { border: 2px solid transparent; cursor: pointer; }
.line-chips button.dim { opacity: .35; }
.chip-all {
  height: 26px; padding: 0 10px; border-radius: 6px; background: var(--sched-bg); color: var(--ink); font-weight: 600; font-size: 14px; flex: none;
}
.searchbar { position: relative; padding: 8px 16px 0; background: var(--card); }
.searchbar input {
  width: 100%; font: inherit; font-size: 16px; padding: 8px 12px; border: 1px solid var(--line); border-radius: 10px;
  background: var(--bg); color: var(--ink);
}
.results {
  position: absolute; left: 16px; right: 16px; top: 100%; z-index: 1200; background: var(--card);
  border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 8px 24px rgba(0,0,0,.18); max-height: 60vh; overflow-y: auto;
}
.results button {
  display: flex; align-items: center; gap: 4px; flex-wrap: wrap; width: 100%; text-align: left; border: 0; border-bottom: 1px solid var(--line);
  background: none; padding: 10px 12px; cursor: pointer;
}
.results button:last-child { border-bottom: 0; }
.results .r-name { font-weight: 600; }
.results .r-lines { display: inline-flex; gap: 3px; margin-left: auto; }
.results .r-lines .badge { min-width: 22px; height: 20px; font-size: 12px; }
.results .none { padding: 10px 12px; }
.line-chips button.sel { border-color: var(--ink); }
#map { flex: 1; min-height: 300px; background: var(--sched-bg); }
.leaflet-container { font: inherit; }
/* selected stop: slow 'breathing' */
.stop-sel { transform-box: fill-box; transform-origin: center; animation: breathe 4s ease-in-out infinite; }
@keyframes breathe { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.1); } }
@media (prefers-reduced-motion: reduce) { .stop-sel { animation: none; } }
.sheet {
  position: fixed; left: 0; right: 0; bottom: 0; z-index: 1100; display: flex; flex-direction: column;
  background: var(--card); border-top: 1px solid var(--line); border-radius: 16px 16px 0 0;
  box-shadow: 0 -6px 24px rgba(0,0,0,.18); transition: height .2s ease;
}
.sheet[hidden] { display: none; }
.sheet { max-height: 55%; }
.sheet-handle {
  flex: none; height: 26px; display: flex; align-items: center; justify-content: center; cursor: grab; touch-action: none;
}
.sheet-handle span { width: 44px; height: 5px; border-radius: 3px; background: var(--line); }
#sheet-body { flex: 1; min-height: 0; overflow-y: auto; padding: 0 16px calc(16px + env(safe-area-inset-bottom)); overscroll-behavior: contain; }
.sheet.sz-min #sheet-body { overflow: hidden; }
@media (min-width: 700px) { .sheet { left: auto; right: 16px; bottom: 16px; width: 420px; border-radius: 16px; max-height: 75%; } }
.sheet-close { position: absolute; top: 2px; right: 8px; border: 0; background: none; font-size: 28px; cursor: pointer; color: var(--muted); z-index: 1; }
.sheet-lines { display: flex; gap: 6px; flex-wrap: wrap; margin: 4px 0 6px; }
.badge-btn { border: 2px solid transparent; background: none; padding: 0; border-radius: 8px; cursor: pointer; line-height: 0; }
.badge-btn.sel { border-color: var(--ink); }
.badge-btn.dim { opacity: .35; }
.sheet-lines .chip-all { border: 2px solid transparent; cursor: pointer; }
.sheet-lines .chip-all.sel { border-color: var(--ink); }
.sheet h2 { margin: 0 32px 2px 0; font-size: 19px; }
.sheet .group { border-top: 1px solid var(--line); padding: 10px 0; }
.sheet .group:first-of-type { border-top: 0; }
.sheet .group.here { box-shadow: inset 4px 0 0 #89cff0; padding-left: 12px; }   /* departures from the tapped side of the road */
.sheet-actions { display: flex; gap: 8px; flex-wrap: wrap; margin: 10px 0 4px; }
.sheet .txt.back { border: 0; padding: 0; margin-bottom: 6px; color: var(--accent); background: none; }
.plan-ends { display: flex; flex-direction: column; gap: 2px; margin: 6px 0 10px; }
.plan-ends .txt { align-self: flex-start; margin-top: 6px; }
.plan-q {
  width: 100%; font: inherit; font-size: 16px; padding: 8px 12px; border: 1px solid var(--line); border-radius: 10px;
  background: var(--bg); color: var(--ink);
}
.results.inline { position: static; box-shadow: none; margin-top: 6px; max-height: none; border: 0; }
.results.inline:empty { display: none; }
.plan-pins-title { margin: 12px 0 0; font-size: 13px; font-weight: 700; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
#plan-pins button { color: var(--ink); }
.pin-star { color: #e0a100; }
.journey { border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; margin-bottom: 10px; }
.journey[data-j] { cursor: pointer; }
.journey[data-j]:hover { border-color: var(--accent); }
.journey.sel { border-color: var(--accent); box-shadow: inset 0 0 0 1px var(--accent); }
.map-ico { background: none; border: 0; }
.map-ico svg { display: block; filter: drop-shadow(0 1px 1.5px rgba(0,0,0,.35)); }
.j-sum { font-weight: 700; font-size: 17px; }
.j-meta { color: var(--muted); font-weight: 400; font-size: 14px; }
.legs { list-style: none; margin: 8px 0 0; padding: 0 0 0 12px; border-left: 3px solid var(--line); display: flex; flex-direction: column; gap: 8px; }
.leg.walk { color: var(--muted); font-size: 14px; }
.leg-line { font-size: 14px; margin-top: 2px; }
.leg-warn { font-size: 13px; background: var(--warn-bg); color: var(--warn-ink); border-radius: 6px; padding: 3px 6px; margin-bottom: 4px; }

.foot { padding: 12px 16px calc(12px + env(safe-area-inset-bottom)); font-size: 12px; color: var(--muted); display: flex; flex-direction: column; gap: 2px; text-align: center; }
body.map-mode .foot { display: none; }
body.map-mode { height: 100dvh; }
.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 2000;
  background: var(--ink); color: var(--bg); padding: 8px 14px; border-radius: 8px; font-size: 14px;
}
