:root {
  --bg: #f6f7fb;
  --surface: #ffffff;
  --surface-2: #f1f3f8;
  --surface-3: #e6e9f1;
  --text: #121826;
  --text-2: #5b6475;
  --text-3: #8a93a5;
  --border: #e2e6ee;
  --accent: #2f6bff;
  --accent-2: #1f4fd6;
  --accent-soft: #e7eeff;
  --on-accent: #ffffff;
  --free: #12a150;
  --free-soft: #e3f6eb;
  --busy: #e08a00;
  --busy-soft: #fdf1dc;
  --out: #d93636;
  --out-soft: #fbe6e6;
  --unknown: #8a93a5;
  --unknown-soft: #eef0f4;
  --route: #2f6bff;
  --route-alt: #9aa7c2;
  --shadow: 0 10px 30px rgba(16, 24, 40, 0.14), 0 2px 6px rgba(16, 24, 40, 0.08);
  --radius: 14px;
  --panel-w: 410px;
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0d1117;
    --surface: #151b24;
    --surface-2: #1c2430;
    --surface-3: #263041;
    --text: #e8edf5;
    --text-2: #a3adbf;
    --text-3: #758095;
    --border: #273142;
    --accent: #5b8cff;
    --accent-2: #7aa2ff;
    --accent-soft: #1d2a4a;
    --on-accent: #0b1020;
    --free: #2fcf74;
    --free-soft: #12301f;
    --busy: #f5a524;
    --busy-soft: #36290f;
    --out: #f26464;
    --out-soft: #3a1a1c;
    --unknown: #8792a6;
    --unknown-soft: #232b38;
    --route: #6b98ff;
    --route-alt: #56627a;
    --shadow: 0 10px 30px rgba(0, 0, 0, 0.45), 0 2px 6px rgba(0, 0, 0, 0.3);
    color-scheme: dark;
  }
}

:root[data-theme="dark"] {
  --bg: #0d1117;
  --surface: #151b24;
  --surface-2: #1c2430;
  --surface-3: #263041;
  --text: #e8edf5;
  --text-2: #a3adbf;
  --text-3: #758095;
  --border: #273142;
  --accent: #5b8cff;
  --accent-2: #7aa2ff;
  --accent-soft: #1d2a4a;
  --on-accent: #0b1020;
  --free: #2fcf74;
  --free-soft: #12301f;
  --busy: #f5a524;
  --busy-soft: #36290f;
  --out: #f26464;
  --out-soft: #3a1a1c;
  --unknown: #8792a6;
  --unknown-soft: #232b38;
  --route: #6b98ff;
  --route-alt: #56627a;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.45), 0 2px 6px rgba(0, 0, 0, 0.3);
  color-scheme: dark;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.4 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  overflow: hidden;
}

button, input { font: inherit; color: inherit; }
button { cursor: pointer; }
[hidden] { display: none !important; }

#map {
  position: fixed;
  inset: 0;
  background: var(--surface-2);
}

/* ---------------- panel / bottom sheet ---------------- */

.panel {
  position: fixed;
  z-index: 1000;
  left: 0;
  right: 0;
  bottom: 0;
  height: var(--sheet-h, 52vh);
  background: var(--surface);
  border-radius: 20px 20px 0 0;
  box-shadow: var(--shadow);
  display: flex;
  flex-direction: column;
  transition: height 0.25s cubic-bezier(0.2, 0.8, 0.2, 1);
  padding-bottom: env(safe-area-inset-bottom);
}
.panel.dragging { transition: none; }

.sheet-handle {
  flex: none;
  height: 26px;
  display: grid;
  place-items: center;
  cursor: grab;
  touch-action: none;
}
.sheet-handle span {
  width: 42px;
  height: 5px;
  border-radius: 3px;
  background: var(--surface-3);
}

.panel-scroll {
  flex: 1;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 0 16px 24px;
  -webkit-overflow-scrolling: touch;
}

@media (min-width: 900px) {
  .panel {
    top: 12px;
    left: 12px;
    bottom: 12px;
    right: auto;
    width: var(--panel-w);
    height: auto;
    border-radius: 18px;
    padding-bottom: 0;
  }
  .sheet-handle { display: none; }
  .panel-scroll { padding-top: 16px; }
}

/* ---------------- header ---------------- */

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 2px 0 14px;
}
.logo {
  width: 38px;
  height: 38px;
  border-radius: 11px;
  display: grid;
  place-items: center;
  color: var(--on-accent);
  background: linear-gradient(135deg, var(--accent), #12b5a6);
  flex: none;
}
.brand-text { min-width: 0; flex: 1; }
.brand h1 { margin: 0; font-size: 18px; letter-spacing: -0.01em; }
.brand p { margin: 0; color: var(--text-2); font-size: 12.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

.data-status {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  border: 1px solid var(--border);
  background: var(--surface-2);
  border-radius: 999px;
  padding: 4px 10px;
  font-size: 12px;
  color: var(--text-2);
  flex: none;
}
.data-status .dot { width: 8px; height: 8px; border-radius: 50%; background: var(--busy); }
.data-status.ok .dot { background: var(--free); box-shadow: 0 0 0 3px var(--free-soft); }
.data-status.err .dot { background: var(--out); }

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

.card {
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  margin-bottom: 12px;
}

.stops { display: flex; flex-direction: column; gap: 8px; }

.stop {
  position: relative;
  display: flex;
  align-items: center;
  gap: 8px;
}
.stop-icon {
  flex: none;
  width: 22px;
  display: grid;
  place-items: center;
}
.stop-icon i {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  border: 3px solid var(--free);
  background: var(--surface);
}
.stop.to .stop-icon i { border-color: var(--out); border-radius: 3px; }
.stop.via .stop-icon i { border-color: var(--text-3); width: 10px; height: 10px; }

.stop input {
  flex: 1;
  min-width: 0;
  height: 44px;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 11px;
  padding: 0 12px;
  outline: none;
  font-size: 16px; /* avoids iOS zoom on focus */
}
.stop input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.stop input::placeholder { color: var(--text-3); }

.icon-btn {
  flex: none;
  width: 44px;
  height: 44px;
  border-radius: 11px;
  border: 1px solid var(--border);
  background: var(--surface);
  display: grid;
  place-items: center;
  color: var(--text-2);
}
.icon-btn:hover { color: var(--accent); border-color: var(--accent); }
.icon-btn.busy svg { animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.suggestions {
  position: absolute;
  z-index: 20;
  top: 48px;
  left: 30px;
  right: 0;
  list-style: none;
  margin: 0;
  padding: 6px;
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 12px;
  box-shadow: var(--shadow);
  max-height: 320px;
  overflow-y: auto;
}
.suggestions li {
  padding: 9px 10px;
  border-radius: 8px;
  cursor: pointer;
  display: flex;
  gap: 10px;
  align-items: flex-start;
}
.suggestions li[aria-selected="true"], .suggestions li:hover { background: var(--surface-2); }
.suggestions .s-main { font-weight: 600; }
.suggestions .s-sub { color: var(--text-2); font-size: 12.5px; }
.suggestions .s-flag { flex: none; font-size: 12px; color: var(--text-3); font-weight: 700; margin-top: 2px; width: 20px; }
.suggestions .s-loc { color: var(--accent); font-weight: 600; }
.suggestions .s-empty { color: var(--text-3); cursor: default; }

.trip-actions {
  display: flex;
  gap: 4px;
  margin: 8px 0 0 26px;
}
.link-btn {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  border: 0;
  background: none;
  color: var(--accent);
  font-weight: 600;
  font-size: 13.5px;
  padding: 6px 8px;
  border-radius: 8px;
}
.link-btn:hover { background: var(--accent-soft); }
#clearTrip { margin-left: auto; color: var(--text-2); }

.route-options {
  display: flex;
  gap: 8px;
  overflow-x: auto;
  margin-top: 10px;
  padding-bottom: 2px;
}
.route-opt {
  flex: none;
  text-align: left;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 11px;
  padding: 8px 12px;
}
.route-opt b { display: block; font-size: 15px; }
.route-opt span { color: var(--text-2); font-size: 12.5px; }
.route-opt[aria-pressed="true"] { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-soft); }
a.route-opt { text-decoration: none; color: inherit; }
a.route-opt b { color: var(--accent); }
.route-opt[aria-pressed="true"] b { color: var(--accent); }

/* ---------------- filters ---------------- */

.filter-row + .filter-row { margin-top: 10px; }
.filter-label { font-size: 12px; font-weight: 700; color: var(--text-2); text-transform: uppercase; letter-spacing: 0.04em; margin-bottom: 6px; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.chip {
  min-height: 36px;
  padding: 0 12px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: var(--surface);
  font-weight: 600;
  font-size: 13.5px;
  color: var(--text);
}
.chip[aria-pressed="true"] { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }

.km-range { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.km-range label { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--text-2); }
.km-range input {
  width: 84px;
  height: 38px;
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 10px;
  padding: 0 10px;
  font-size: 16px; /* avoids iOS zoom on focus */
  font-variant-numeric: tabular-nums;
  outline: none;
}
.km-range input:focus { border-color: var(--accent); box-shadow: 0 0 0 3px var(--accent-soft); }
.km-range .unit { font-size: 13px; color: var(--text-2); }
.hint { font-size: 12px; color: var(--text-3); margin-top: 5px; }

.toggle {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-top: 12px;
  font-size: 13.5px;
  color: var(--text-2);
  cursor: pointer;
}
.toggle input { position: absolute; opacity: 0; pointer-events: none; }
.switch {
  flex: none;
  width: 38px;
  height: 22px;
  border-radius: 999px;
  background: var(--surface-3);
  position: relative;
  transition: background 0.15s;
}
.switch::after {
  content: "";
  position: absolute;
  top: 3px;
  left: 3px;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: #fff;
  box-shadow: 0 1px 3px rgba(0, 0, 0, 0.3);
  transition: transform 0.15s;
}
.toggle input:checked + .switch { background: var(--accent); }
.toggle input:checked + .switch::after { transform: translateX(16px); }
.toggle input:focus-visible + .switch { outline: 2px solid var(--accent); outline-offset: 2px; }

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

.results-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: 4px 2px 10px;
}
.results-title { font-weight: 700; font-size: 16px; }
.results-sub { color: var(--text-2); font-size: 13px; }

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 40px;
  padding: 0 14px;
  border-radius: 11px;
  border: 1px solid var(--border);
  background: var(--surface);
  font-weight: 650;
  flex: none;
}
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--on-accent); }
.btn.primary.active { background: var(--out); border-color: var(--out); }
.btn.ghost { background: var(--surface-2); }
.btn.small { min-height: 34px; padding: 0 10px; font-size: 13px; border-radius: 9px; }
.btn.wide { width: 100%; margin-top: 8px; }

.drive-bar {
  background: var(--accent-soft);
  border: 1px solid var(--accent);
  color: var(--text);
  border-radius: var(--radius);
  padding: 10px 12px;
  margin-bottom: 10px;
  font-size: 13.5px;
}
.drive-bar b { color: var(--accent); white-space: nowrap; }
.drive-bar .warn { color: var(--out); font-weight: 600; }
.drive-bar .row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.drive-bar .row + .row { margin-top: 8px; }

.passed-note {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  font-size: 13px;
  color: var(--text-2);
  margin: 0 2px 8px;
}
.passed-note .link-btn { flex: none; }

.station-list { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 8px; }

.station {
  border: 1px solid var(--border);
  border-radius: var(--radius);
  background: var(--surface);
  overflow: hidden;
}
.station.selected { border-color: var(--accent); box-shadow: 0 0 0 2px var(--accent-soft); }
.station.passed { opacity: 0.5; }

.station-main {
  width: 100%;
  border: 0;
  background: none;
  text-align: left;
  padding: 10px 12px;
  display: grid;
  grid-template-columns: 52px 1fr auto;
  gap: 10px;
  align-items: center;
}
.km {
  text-align: center;
  font-variant-numeric: tabular-nums;
}
.km b { display: block; font-size: 15px; }
.km span { font-size: 11px; color: var(--text-3); text-transform: uppercase; letter-spacing: 0.03em; }
.st-body { min-width: 0; }
.st-name { font-weight: 650; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.station.selected .st-name, .station.selected .st-meta { white-space: normal; }
.st-meta { color: var(--text-2); font-size: 12.5px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.st-tags { display: flex; flex-wrap: wrap; gap: 5px; margin-top: 5px; }
.tag {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  height: 22px;
  padding: 0 8px;
  border-radius: 999px;
  font-size: 12px;
  font-weight: 650;
  background: var(--surface-2);
  color: var(--text-2);
}
.tag.free { background: var(--free-soft); color: var(--free); }
.tag.busy { background: var(--busy-soft); color: var(--busy); }
.tag.out { background: var(--out-soft); color: var(--out); }
.tag.unknown { background: var(--unknown-soft); color: var(--text-2); }
.tag.restricted { background: var(--busy-soft); color: var(--busy); }
.tag.opposite { background: var(--surface-3); color: var(--text-2); }
.tag i { width: 7px; height: 7px; border-radius: 50%; background: currentColor; }

.power {
  font-weight: 800;
  font-size: 15px;
  font-variant-numeric: tabular-nums;
  text-align: right;
}
.power small { display: block; font-size: 11px; color: var(--text-3); font-weight: 600; }

.st-details {
  border-top: 1px solid var(--border);
  padding: 10px 12px 12px;
  font-size: 13.5px;
}
.st-details dl { display: grid; grid-template-columns: 84px 1fr; gap: 4px 10px; margin: 0 0 10px; }
.st-details dt { color: var(--text-3); }
.st-details dd { margin: 0; overflow-wrap: anywhere; }
.points { list-style: none; margin: 0 0 10px; padding: 0; display: flex; flex-direction: column; gap: 4px; }
.points li { display: flex; align-items: center; justify-content: space-between; gap: 8px; padding: 6px 8px; border-radius: 8px; background: var(--surface-2); }
.points li.other { opacity: 0.6; }
.points .pt-status { display: flex; gap: 4px; flex-wrap: wrap; justify-content: flex-end; }
.actions { display: flex; flex-wrap: wrap; gap: 6px; }
.actions a { text-decoration: none; color: inherit; }

.empty {
  text-align: center;
  color: var(--text-2);
  padding: 24px 12px;
  border: 1px dashed var(--border);
  border-radius: var(--radius);
}

.legend { display: flex; flex-wrap: wrap; gap: 12px; color: var(--text-2); font-size: 12px; margin: 14px 2px 4px; }
.legend span { display: inline-flex; align-items: center; gap: 5px; }
.lg { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.lg.free { background: var(--free); }
.lg.busy { background: var(--busy); }
.lg.out { background: var(--out); }
.lg.unknown { background: var(--unknown); }
.sources { color: var(--text-3); font-size: 11.5px; margin: 6px 2px 0; }
.sources a { color: inherit; }

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

.map-buttons {
  position: fixed;
  z-index: 900;
  right: 12px;
  bottom: calc(var(--sheet-h, 52vh) + 12px);
  display: flex;
  flex-direction: column;
  gap: 8px;
  transition: bottom 0.25s cubic-bezier(0.2, 0.8, 0.2, 1);
}
@media (min-width: 900px) {
  .map-buttons { bottom: 96px; }
}
.fab {
  width: 46px;
  height: 46px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  box-shadow: var(--shadow);
  display: grid;
  place-items: center;
}
.fab.on { color: var(--accent); }

.leaflet-container { font: inherit; background: var(--surface-2); }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .leaflet-tile-pane { filter: invert(1) hue-rotate(180deg) brightness(0.92) contrast(0.88) saturate(0.6); }
}
:root[data-theme="dark"] .leaflet-tile-pane { filter: invert(1) hue-rotate(180deg) brightness(0.92) contrast(0.88) saturate(0.6); }
.leaflet-control-attribution { font-size: 10px; }
.leaflet-bar a { background: var(--surface); color: var(--text); border-color: var(--border); }

.pin {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 34px;
  height: 22px;
  padding: 0 6px;
  border-radius: 11px;
  color: #fff;
  font-weight: 800;
  font-size: 11.5px;
  font-variant-numeric: tabular-nums;
  border: 2px solid #fff;
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.35);
  transform: translate(-50%, -50%);
  white-space: nowrap;
}
.pin.free { background: var(--free); }
.pin.busy { background: var(--busy); }
.pin.out { background: var(--out); }
.pin.unknown { background: var(--unknown); }
.pin.selected { outline: 3px solid var(--accent); outline-offset: 1px; transform: translate(-50%, -50%) scale(1.15); }
.pin-wrap { width: 0 !important; height: 0 !important; }

.endpoint {
  width: 18px;
  height: 18px;
  border-radius: 50%;
  border: 4px solid var(--free);
  background: #fff;
  box-shadow: 0 1px 4px rgba(0, 0, 0, 0.4);
  transform: translate(-50%, -50%);
}
.endpoint.to { border-color: var(--out); border-radius: 4px; }
.endpoint.via { border-color: #6b7280; width: 14px; height: 14px; }

.me {
  position: relative;
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--accent);
  border: 3px solid #fff;
  box-shadow: 0 0 0 6px rgba(47, 107, 255, 0.25), 0 1px 4px rgba(0, 0, 0, 0.4);
  transform: translate(-50%, -50%);
}
/* Heading arrow: a zero-size box at the dot's centre, rotated to the direction of travel. */
.me-heading { position: absolute; left: 50%; top: 50%; width: 0; height: 0; display: none; }
.me-heading::before {
  content: "";
  position: absolute;
  left: -7px;
  top: -25px;
  border-left: 7px solid transparent;
  border-right: 7px solid transparent;
  border-bottom: 11px solid var(--accent);
}
.me.moving .me-heading { display: block; }

.leaflet-popup-content-wrapper { background: var(--surface); color: var(--text); border-radius: 12px; }
.leaflet-popup-tip { background: var(--surface); }
.leaflet-popup-content { margin: 12px 14px; min-width: 200px; }
.popup-name { font-weight: 700; margin-bottom: 2px; }
.popup-meta { color: var(--text-2); font-size: 12.5px; margin-bottom: 8px; }
.popup .actions { margin-top: 8px; }

.toast {
  position: fixed;
  z-index: 2000;
  left: 50%;
  top: calc(12px + env(safe-area-inset-top));
  transform: translateX(-50%);
  max-width: calc(100vw - 32px);
  background: var(--text);
  color: var(--bg);
  padding: 10px 14px;
  border-radius: 12px;
  font-size: 14px;
  box-shadow: var(--shadow);
}

@media (prefers-reduced-motion: reduce) {
  .panel, .map-buttons { transition: none; }
}
