:root {
  color-scheme: light;
  --bg: #fff; --text: #222; --text-2: #444; --text-3: #5f5f5f;
  --surface: #fff; --surface-2: #f5f5f5; --surface-3: #fafafa; --footer-bg: #f2f2f2;
  --border: #ddd; --border-2: #eee; --border-3: #f0f0f0; --input-border: #bbb; --input-bg: #fff;
  --hover: #f3f7ff; --sel: #fff3c4; --link: #1a55b0; --accent: #1a55b0; --accent-text: #fff;
  --hint-bg: #eef4ff; --warn: #8a4b00; --ok-text: #1a7a1a; --disabled: #aaa; --shadow: rgba(0,0,0,.3);
  --banner-bg: #7a1f1f; --banner-text: #fff;
  --cal-text: #1b1b1b;
  --cal-green-bg: #c9ecc9; --cal-green-bd: #8fcf8f; --cal-green-tx: #1b1b1b;
  --cal-red-bg: #f8c9c9; --cal-red-bd: #e59a9a; --cal-red-tx: #1b1b1b;
  --cal-past-bg: #e4e4e4; --cal-past-bd: #cfcfcf; --cal-past-tx: #666;
  --cal-nodata-a: #fff; --cal-nodata-b: #d8d8d8; --cal-nodata-bd: #ccc; --cal-nodata-tx: #6b6b6b;
  --ring-in: #fff; --ring-out: #000;
  --map-bg: #f2efe9;
}
/* Auto: the OS theme paints the first frame with no JS; data-theme="light" opts out. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #14171c; --text: #e8eaed; --text-2: #c9cdd3; --text-3: #a9b0ba;
    --surface: #1e2228; --surface-2: #272c34; --surface-3: #23282f; --footer-bg: #191c21;
    --border: #3b424c; --border-2: #30363e; --border-3: #2a2f37; --input-border: #667080; --input-bg: #14171c;
    --hover: #2a3444; --sel: #4a4012; --link: #8ab4f8; --accent: #2f62c4; --accent-text: #fff;
    --hint-bg: #1f2b40; --warn: #f0b35c; --ok-text: #72d27e; --disabled: #6b7480; --shadow: rgba(0,0,0,.6);
    --banner-bg: #5a1717; --banner-text: #fff;
    --cal-text: #e8eaed;
    --cal-green-bg: #1f4d2b; --cal-green-bd: #3f8d56; --cal-green-tx: #e6f6e9;
    --cal-red-bg: #5e2427; --cal-red-bd: #a84f54; --cal-red-tx: #ffe6e6;
    --cal-past-bg: #2f343b; --cal-past-bd: #4a515b; --cal-past-tx: #aab2bc;
    --cal-nodata-a: #1e2228; --cal-nodata-b: #343a42; --cal-nodata-bd: #4a515b; --cal-nodata-tx: #b4bcc6;
    --ring-in: #14171c; --ring-out: #fff;
    --map-bg: #14171c;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #14171c; --text: #e8eaed; --text-2: #c9cdd3; --text-3: #a9b0ba;
  --surface: #1e2228; --surface-2: #272c34; --surface-3: #23282f; --footer-bg: #191c21;
  --border: #3b424c; --border-2: #30363e; --border-3: #2a2f37; --input-border: #667080; --input-bg: #14171c;
  --hover: #2a3444; --sel: #4a4012; --link: #8ab4f8; --accent: #2f62c4; --accent-text: #fff;
  --hint-bg: #1f2b40; --warn: #f0b35c; --ok-text: #72d27e; --disabled: #6b7480; --shadow: rgba(0,0,0,.6);
  --banner-bg: #5a1717; --banner-text: #fff;
  --cal-text: #e8eaed;
  --cal-green-bg: #1f4d2b; --cal-green-bd: #3f8d56; --cal-green-tx: #e6f6e9;
  --cal-red-bg: #5e2427; --cal-red-bd: #a84f54; --cal-red-tx: #ffe6e6;
  --cal-past-bg: #2f343b; --cal-past-bd: #4a515b; --cal-past-tx: #aab2bc;
  --cal-nodata-a: #1e2228; --cal-nodata-b: #343a42; --cal-nodata-bd: #4a515b; --cal-nodata-tx: #b4bcc6;
  --ring-in: #14171c; --ring-out: #fff;
  --map-bg: #14171c;
}
:root[data-theme="light"] { color-scheme: light; }

:root { --panel-w: min(360px, calc(100vw - 200px)); }

/* ---------- base ---------- */
* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; overflow: hidden; overscroll-behavior: none; }
html, body { height: 100%; margin: 0; font-family: system-ui, sans-serif; }
body {
  height: 100vh; height: 100dvh; /* dvh: the layout follows the mobile browser bars */
  display: flex; flex-direction: column; min-width: 0;
  overflow: hidden; overscroll-behavior: none;
  background: var(--bg); color: var(--text);
  -webkit-tap-highlight-color: transparent;
}
a { color: var(--link); }
button { font-family: inherit; }
.vh { position: absolute; width: 1px; height: 1px; margin: -1px; padding: 0; overflow: hidden; clip: rect(0 0 0 0); clip-path: inset(50%); white-space: nowrap; border: 0; }

/* Visible keyboard focus everywhere, pressed feedback instead of hover-only cues */
:where(button, a, input, select, summary, [tabindex]):focus-visible { outline: 3px solid var(--link); outline-offset: 2px; }
.cal-day:focus-visible { outline-offset: -2px; }
.chip:active, .dep-btn:active:not(:disabled), .more-btn:active:not(:disabled), .swap-btn:active, .locate-btn:active, .theme-btn:active { filter: brightness(.9); }
.row:active, .drow:active, .trow:active, .stop-item:active { background: var(--hover); }
.cal-day:active:not(.nodata) { filter: brightness(.92); }

/* ---------- banner ---------- */
.brand { display: inline-flex; align-items: center; gap: 6px; margin-right: 4px; }
.brand img { display: block; background: #fff; /* white badge keeps the teal logo legible in both themes */ border-radius: 50%; padding: 2px; }
.brand-name { font-weight: 700; font-size: 16px; letter-spacing: .3px; }
.banner {
  position: relative; z-index: 2000; flex: none; align-items: center;
  background: var(--banner-bg); color: var(--banner-text); font-size: 14px;
  display: flex; flex-wrap: wrap; gap: 4px 12px;
  padding: max(8px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) 8px max(12px, env(safe-area-inset-left));
}
.banner-full { display: contents; }
.banner-short, .info-btn, .banner-credits { display: none; }
.banner-full a { color: inherit; }
.theme-btn { order: 1; margin-left: auto; display: inline-flex; align-items: center; justify-content: center; gap: 6px; padding: 3px 10px; font: inherit; font-size: 13px; color: var(--banner-text); background: rgba(255,255,255,.12); border: 1px solid rgba(255,255,255,.7); border-radius: 14px; cursor: pointer; }
.theme-btn svg { width: 16px; height: 16px; flex: none; }
.theme-btn:focus-visible, .info-btn:focus-visible { outline: 3px solid #ffd400; outline-offset: 2px; }
.sim-info { font-weight: 700; background: rgba(255,255,255,.18); padding: 1px 8px; border-radius: 10px; }

/* ---------- footer ---------- */
.footer { flex: none; background: var(--footer-bg); color: var(--text-2); font-size: 12px; border-top: 1px solid var(--border); overflow-wrap: anywhere;
  padding: 6px max(12px, env(safe-area-inset-right)) max(6px, env(safe-area-inset-bottom)) max(12px, env(safe-area-inset-left)); }
.footer a { margin-left: 6px; }

/* ---------- legal dialog ---------- */
.legal { width: min(640px, calc(100vw - 32px)); max-height: 85vh; max-height: 85dvh; padding: 0; border: 1px solid var(--border); border-radius: 10px; color: var(--text); background: var(--surface); }
.legal[open] { display: flex; flex-direction: column; }
.legal::backdrop { background: rgba(0, 0, 0, .45); }
.legal-head { flex: none; display: flex; justify-content: space-between; align-items: center; padding: 10px 16px; border-bottom: 1px solid var(--border); background: var(--surface); }
.legal-head h2 { margin: 0; font-size: 17px; }
.legal-close { border: 0; background: none; color: var(--text); font-size: 26px; line-height: 1; cursor: pointer; padding: 0 6px; }
.legal-body { flex: 1; min-height: 0; padding: 4px 16px 16px; font-size: 14px; line-height: 1.45; overflow-y: auto; overscroll-behavior: contain; overflow-wrap: anywhere; }
.legal-body h3 { margin: 14px 0 4px; font-size: 14px; }
.legal-body p, .legal-body ul { margin: 0; }
.legal-body ul { padding-left: 20px; }

/* ---------- stage, map, panel (desktop and tablets: panel on the right) ---------- */
#stage { flex: 1; min-height: 0; position: relative; overflow: hidden; }
#map { position: absolute; inset: 0; }

.legend { position: absolute; top: 10px; right: max(10px, env(safe-area-inset-right)); bottom: 10px; width: var(--panel-w); z-index: 1000; display: flex; flex-direction: column; background: var(--surface); color: var(--text); border-radius: 8px; box-shadow: 0 2px 10px var(--shadow); overflow: hidden; container-type: inline-size; }
.legend-toggle { all: unset; box-sizing: border-box; position: relative; flex: none; display: flex; justify-content: space-between; align-items: center; gap: 8px; padding: 10px 12px; font-weight: 600; font-size: 14px; color: var(--text); background: var(--surface-2); border-bottom: 1px solid var(--border); cursor: default; }
.legend-toggle[aria-disabled="true"] { pointer-events: none; } /* side panel: the header is only a title */
.legend-toggle:focus-visible { outline: 3px solid var(--link); outline-offset: -3px; }
.grabber { display: none; }
.chev { display: none; width: 8px; height: 8px; border: solid var(--text); border-width: 0 2px 2px 0; transform: rotate(45deg); }
.sheet-scroll { flex: 1; min-height: 0; display: flex; flex-direction: column; }
.legend-body { display: flex; flex-direction: column; flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; }
.legend-body[hidden] { display: none; }
.search { position: relative; padding: 8px; border-bottom: 1px solid var(--border-2); }
.search input { width: 100%; padding: 7px 28px 7px 8px; font-size: 14px; border: 1px solid var(--input-border); border-radius: 6px; background: var(--input-bg); color: var(--text); }
.search button { position: absolute; right: 12px; top: 50%; transform: translateY(-50%); border: 0; background: none; font-size: 20px; line-height: 1; color: var(--text-3); cursor: pointer; padding: 0 4px; }
.legend-list { flex: 1; overflow-y: auto; overscroll-behavior: contain; }
.buses-section .legend-list { max-height: 320px; }
.buses-section[hidden], .key[hidden], .sim-info[hidden], .locate-msg[hidden] { display: none; }
.buses-section { border-top: 1px solid var(--border-2); padding-bottom: 4px; }
.row { display: flex; align-items: center; gap: 8px; padding: 6px 10px; font-size: 13px; border-bottom: 1px solid var(--border-3); cursor: pointer; }
.row.sel { background: var(--sel); }
.key { display: inline-flex; align-items: center; gap: 4px; font-weight: 400; font-size: 12px; color: var(--text-2); }
.op-bar { display: flex; flex-wrap: wrap; gap: 6px; padding: 8px 8px 0; }
.chip { border: 1px solid var(--input-border); background: var(--surface); color: var(--text); border-radius: 12px; padding: 3px 10px; font-size: 12px; cursor: pointer; }
.chip.active { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.row .head { display: flex; flex-direction: column; }
.row .head small, .drow .head small.op { color: var(--text-3); font-size: 11px; }
.dot { flex: none; width: 11px; height: 11px; border-radius: 50%; border: 1px solid rgba(0,0,0,.35); }
.code { flex: none; min-width: 44px; font-weight: 600; overflow-wrap: anywhere; }
.head { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; color: var(--text); }
/* headsigns wrap to two lines instead of being cut; operator lines get an ellipsis */
.row .head > span, .drow .head > span { display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 2; line-clamp: 2; overflow: hidden; white-space: normal; overflow-wrap: anywhere; }
.row .head small, .drow .head small { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.note { padding: 10px; font-size: 12px; color: var(--text-3); text-align: center; }

.stops { max-height: min(180px, 30vh); max-height: min(180px, 30dvh); overflow-y: auto; overscroll-behavior: contain; margin: 6px 0 0; padding: 0; list-style: none; font-size: 12px; }
.stops li { display: flex; justify-content: space-between; gap: 10px; padding: 1px 0; overflow-wrap: anywhere; }
.stops li.next { font-weight: 700; background: var(--sel); }
.stops .t { flex: none; font-variant-numeric: tabular-nums; color: var(--text-2); }
.leaflet-popup-content { min-width: min(190px, 60vw); }

.stop-search { position: relative; }
.stop-search input, .tf input { width: 100%; padding: 7px 8px; font-size: 14px; border: 1px solid var(--input-border); border-radius: 6px; background: var(--input-bg); color: var(--text); }
/* dropdowns scale with the visible height so they stay usable above an on-screen keyboard (--vvh is set by sheet.js) */
.stop-results { margin-top: 3px; max-height: min(220px, calc(var(--vvh, 100dvh) * .4)); overflow-y: auto; overscroll-behavior: contain; background: var(--surface); border: 1px solid var(--border); border-radius: 6px; }
.stop-results[hidden] { display: none; }
.stop-item { padding: 7px 10px; font-size: 13px; border-bottom: 1px solid var(--border-3); cursor: pointer; display: flex; flex-direction: column; overflow-wrap: anywhere; }
.stop-item small { color: var(--text-3); }
.stop-item.active { background: var(--hover); }
.stop-note { padding: 10px; font-size: 12px; color: var(--text-3); text-align: center; }

.dep-panel { display: flex; flex-direction: column; flex: 1; min-height: 0; }
.dep-panel[hidden] { display: none; }
.dep-head { display: flex; justify-content: space-between; padding: 6px 8px; border-bottom: 1px solid var(--border-2); background: var(--surface); }
.dep-btn { border: 0; background: none; font-size: 14px; color: var(--link); cursor: pointer; padding: 2px 6px; }
.dep-title { padding: 8px 12px; display: flex; flex-direction: column; border-bottom: 1px solid var(--border-2); font-size: 14px; overflow-wrap: anywhere; }
.dep-title small { color: var(--text-3); font-size: 12px; }
.drow { display: flex; align-items: center; gap: 8px; padding: 6px 10px; font-size: 13px; border-bottom: 1px solid var(--border-3); cursor: pointer; }
.drow.sel { background: var(--sel); }
.drow .time { flex: none; width: 44px; font-weight: 600; font-variant-numeric: tabular-nums; }
.drow .in { flex: none; width: 86px; white-space: nowrap; color: var(--text-2); font-size: 12px; }
.drow .head { display: flex; flex-direction: column; }
.drow .head small { color: var(--ok-text); font-size: 11px; }
.stop-pin { background: #d0021b; border: 3px solid #fff; border-radius: 50%; box-shadow: 0 0 0 2px #d0021b, 0 1px 6px rgba(0,0,0,.5); }

/* date selectors: caption on top, then arrow - input - arrow - Today */
.date-bar, .trip-date { display: grid; grid-template-columns: auto minmax(0, 1fr) auto auto; align-items: center; gap: 4px 6px; font-size: 13px; }
.date-bar { padding: 8px 8px 0; }
.trip-date { margin-top: 8px; }
.date-bar > label, .trip-date > label { grid-column: 1 / -1; font-size: 11px; color: var(--text-3); margin: 0; }
.date-bar input, .trip-date input { min-width: 0; width: 100%; padding: 5px 6px; font-size: 14px; border: 1px solid var(--input-border); border-radius: 6px; background: var(--input-bg); color: var(--text); }
.date-note { grid-column: 1 / -1; font-size: 12px; color: var(--warn); }
.date-note[hidden] { display: none; }

/* day calendar inside the departures panel */
.cal { flex: none; border-bottom: 1px solid var(--border-2); padding: 4px 8px 6px; }
.cal[hidden] { display: none; }
.cal-head { display: flex; align-items: center; gap: 2px; }
.cal-month { flex: 1; text-align: center; font-size: 13px; }
.cal-toggle { font-size: 12px; }
.dep-btn:disabled { color: var(--disabled); cursor: default; }
.dep-btn.arrow { font-size: 22px; line-height: 1; padding: 0 8px; }
.day-nav { display: flex; justify-content: space-between; padding: 2px 8px; border-bottom: 1px solid var(--border-2); }
.cal-grid { display: grid; grid-template-columns: repeat(7, minmax(0, 1fr)); gap: 2px; }
.cal-wd span { text-align: center; font-size: 10px; color: var(--text-3); padding: 2px 0; }
.cal-day, .cal-blank { height: 32px; }
.cal-day { display: flex; flex-direction: column; align-items: center; justify-content: center; line-height: 1.05; padding: 0; min-width: 0; border: 1px solid transparent; border-radius: 5px; font-family: inherit; color: var(--cal-text); cursor: pointer; }
.cal-day .n { font-size: 12px; font-weight: 600; }
.cal-day .c { font-size: 9px; font-variant-numeric: tabular-nums; }
.cal-day.green, .sw.green { background: var(--cal-green-bg); border-color: var(--cal-green-bd); color: var(--cal-green-tx); }
.cal-day.red, .sw.red { background: var(--cal-red-bg); border-color: var(--cal-red-bd); color: var(--cal-red-tx); }
.cal-day.past, .sw.past { background: var(--cal-past-bg); border-color: var(--cal-past-bd); color: var(--cal-past-tx); }
.cal-day.nodata, .sw.nodata { background: repeating-linear-gradient(45deg, var(--cal-nodata-a), var(--cal-nodata-a) 3px, var(--cal-nodata-b) 3px, var(--cal-nodata-b) 4px); border-color: var(--cal-nodata-bd); color: var(--cal-nodata-tx); cursor: not-allowed; }
.cal-day.today { outline: 2px solid var(--link); outline-offset: -2px; }
.cal-day.selected { box-shadow: 0 0 0 2px var(--ring-in), 0 0 0 4px var(--ring-out); position: relative; z-index: 1; }
.cal-legend { display: flex; flex-wrap: wrap; gap: 2px 10px; list-style: none; margin: 6px 0 0; padding: 0; font-size: 11px; color: var(--text-2); }
.cal-legend li { display: inline-flex; align-items: center; gap: 4px; }
.sw { display: inline-block; width: 12px; height: 12px; border: 1px solid; border-radius: 3px; }
.cal-note { margin-top: 4px; font-size: 11px; color: var(--warn); }
.cal-note[hidden], #cal-body[hidden] { display: none; }
.day-title { padding: 6px 12px; font-size: 12px; font-weight: 600; border-bottom: 1px solid var(--border-2); background: var(--surface-3); }
.day-title:empty { display: none; }
.route-note { padding: 4px 12px; }
.drow.noeta .head { margin-left: 0; }

/* search panel */
.sec { padding: 8px 10px 10px; border-bottom: 1px solid var(--border-2); }
.sec-title { margin: 0 0 6px; font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .4px; color: var(--text-3); }
.trip-hint { margin: 0 0 8px; font-size: 12px; color: var(--text-2); background: var(--hint-bg); border-radius: 6px; padding: 4px 8px; }
/* From / To stacked on the left, swap button centred on the right */
.trip-fields { position: relative; display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 6px 6px; align-items: center; }
.tf { position: relative; display: flex; flex-direction: column; min-width: 0; grid-column: 1; }
#tf-from { grid-row: 1; }
#tf-to { grid-row: 2; }
.tf label { font-size: 11px; color: var(--text-3); margin-bottom: 2px; }
.swap-btn { grid-column: 2; grid-row: 1 / 3; align-self: center; width: 34px; height: 34px; border: 1px solid var(--input-border); border-radius: 17px; background: var(--surface); font-size: 16px; line-height: 1; cursor: pointer; color: var(--link); padding: 0; }
.toggle { display: flex; align-items: center; gap: 8px; font-size: 13px; cursor: pointer; }
.toggle input { width: 16px; height: 16px; flex: none; }
.stop-item .me { color: var(--link); font-weight: 600; }
.stop-item small b { font-weight: 600; color: var(--text); }
.trip-results { margin-top: 8px; font-size: 13px; }
.tr-group { margin-top: 8px; border: 1px solid var(--border); border-radius: 8px; overflow: hidden; }
.tr-ghead { display: flex; justify-content: space-between; gap: 8px; padding: 6px 10px; background: var(--surface-2); font-size: 13px; overflow-wrap: anywhere; }
.tr-ghead small { color: var(--text-3); white-space: nowrap; }
.trow { display: flex; flex-direction: column; gap: 1px; padding: 6px 10px; border-top: 1px solid var(--border-3); cursor: pointer; font-size: 13px; overflow-wrap: anywhere; }
.trow.sel { background: var(--sel); }
.trow .l1 { display: flex; flex-wrap: wrap; align-items: center; gap: 2px 8px; }
.trow .tm { font-weight: 600; font-variant-numeric: tabular-nums; white-space: nowrap; }
.trow .eta, .trow small { color: var(--text-3); font-size: 11px; }
.trow .eta { margin-left: auto; white-space: nowrap; }
.more-btn { display: block; width: 100%; margin-top: 8px; padding: 7px; border: 1px solid var(--link); border-radius: 6px; background: var(--surface); color: var(--link); font-size: 13px; cursor: pointer; }
.more-btn:disabled { color: var(--text-3); border-color: var(--border); cursor: default; }
.end-note { margin-top: 8px; text-align: center; font-size: 12px; color: var(--text-3); }

/* locate control and user position */
.locate-btn { display: block; width: 34px; height: 34px; background: var(--surface); border: 0; cursor: pointer; font-size: 18px; color: var(--text); padding: 0; }
.locate-btn.on { color: var(--link); }
.locate-btn.busy { opacity: .5; cursor: progress; }
.locate-msg { position: absolute; top: 10px; left: 56px; z-index: 1000; max-width: 300px; background: var(--surface); color: var(--text); border-radius: 8px; box-shadow: 0 2px 10px var(--shadow); padding: 8px 12px; font-size: 13px; }
.me-dot { background: #1a73e8; border: 3px solid #fff; border-radius: 50%; box-shadow: 0 0 0 2px #1a73e8, 0 1px 6px rgba(0,0,0,.5); }
.pin-from, .pin-to { width: 22px; height: 22px; border-radius: 50%; border: 3px solid #fff; box-shadow: 0 1px 6px rgba(0,0,0,.6); color: #fff; font-weight: 700; font-size: 12px; display: flex; align-items: center; justify-content: center; }
.pin-from { background: #1b8a3a; }
.pin-to { background: #111; }

/* Leaflet: map background, controls, popups, tooltips, attribution */
.leaflet-container { background: var(--map-bg); font-family: inherit; }
/* Only the OpenStreetMap raster fallback is darkened (class set by app.js); the vector map has its own dark style. */
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .base-raster .leaflet-tile-pane { filter: invert(1) hue-rotate(180deg) brightness(.95) contrast(.9); } }
:root[data-theme="dark"] .base-raster .leaflet-tile-pane { filter: invert(1) hue-rotate(180deg) brightness(.95) contrast(.9); }
.leaflet-container a { color: var(--link); }
.leaflet-bar a, .leaflet-bar a:hover, .leaflet-bar a:focus { background-color: var(--surface); color: var(--text); border-bottom-color: var(--border); }
.leaflet-bar a:hover, .leaflet-bar a:focus { background-color: var(--hover); }
.leaflet-bar a.leaflet-disabled { background-color: var(--surface-2); color: var(--disabled); }
.leaflet-touch .leaflet-bar { border-color: var(--border); }
.leaflet-container .leaflet-control-attribution { background: var(--surface); color: var(--text-2); opacity: .92; max-width: calc(100vw - 80px); white-space: normal; }
/* the attribution must never sit under the side panel */
.leaflet-bottom.leaflet-right { margin-right: calc(var(--panel-w) + max(10px, env(safe-area-inset-right)) + 4px); }
.leaflet-popup-content-wrapper, .leaflet-popup-tip { background: var(--surface); color: var(--text); box-shadow: 0 3px 14px var(--shadow); }
.leaflet-popup-content-wrapper { max-width: calc(100vw - 48px); }
.leaflet-popup-content { overflow-wrap: anywhere; }
.leaflet-container a.leaflet-popup-close-button { color: var(--text-3); }
.leaflet-container a.leaflet-popup-close-button:hover { color: var(--text); background: transparent; }
.leaflet-tooltip { background: var(--surface); color: var(--text); border-color: var(--border); box-shadow: 0 1px 3px var(--shadow); max-width: 70vw; white-space: normal; }
.leaflet-tooltip-top:before { border-top-color: var(--surface); }
.leaflet-tooltip-bottom:before { border-bottom-color: var(--surface); }
.leaflet-tooltip-left:before { border-left-color: var(--surface); }
.leaflet-tooltip-right:before { border-right-color: var(--surface); }

/* hover tints only where a real hover exists (no sticky hover after a tap) */
@media (hover: hover) {
  .row:hover, .drow:hover, .trow:hover, .stop-item:hover { background: var(--hover); }
  .stop-item.active:hover { background: var(--hover); }
  .row.sel:hover, .drow.sel:hover, .trow.sel:hover { background: var(--sel); }
  .swap-btn:hover, .more-btn:hover:not(:disabled), .locate-btn:hover { background: var(--hover); }
  .theme-btn:hover { background: rgba(255,255,255,.24); }
}

/* ---------- narrow panels (container query: works for the side panel and the bottom sheet) ---------- */
@container (max-width: 379px) {
  /* departure rows: ETA on its own first line, route and headsign below */
  .drow { display: grid; grid-template-columns: auto 11px auto minmax(0, 1fr); gap: 2px 8px; align-items: center; }
  .drow .time, .drow .in { width: auto; }
  .drow.has-eta { grid-template-columns: 11px auto minmax(0, 1fr); }
  .drow.has-eta .time { grid-column: 1 / 3; }
  .drow.has-eta .in { grid-column: 3; }
  .drow.has-eta .dot { grid-column: 1; }
  .drow.has-eta .code { grid-column: 2; }
  .drow.has-eta .head { grid-column: 3; }
  .code { min-width: 0; }
}
@container (max-width: 335px) {
  .cal { padding-left: 0; padding-right: 0; }
  .cal-grid { gap: 0; }
}

/* ---------- touch ergonomics: 44 x 44 CSS px targets, 16 px inputs ---------- */
@media (pointer: coarse) {
  input[type="search"], input[type="date"], input[type="text"] { font-size: 16px; min-height: 44px; }
  .stop-search input, .tf input, .search input { padding: 8px 10px; }
  .search input { padding-right: 48px; }
  .search button { right: 8px; width: 44px; height: 44px; font-size: 26px; padding: 0; }
  .dep-btn { min-width: 44px; min-height: 44px; padding: 4px 12px; font-size: 15px; display: inline-flex; align-items: center; justify-content: center; }
  .dep-btn.arrow { font-size: 26px; padding: 0 10px; }
  .cal-toggle { font-size: 14px; }
  .cal-day, .cal-blank { height: 44px; }
  .cal-day .n { font-size: 14px; }
  .cal-day .c { font-size: 11px; }
  .cal-wd span { font-size: 11px; }
  .chip { min-height: 44px; min-width: 44px; padding: 4px 16px; font-size: 14px; border-radius: 22px; }
  .row, .drow, .trow { min-height: 44px; }
  .row, .drow { padding-top: 8px; padding-bottom: 8px; }
  .trow { padding-top: 8px; padding-bottom: 8px; }
  .stop-item { min-height: 44px; justify-content: center; padding: 8px 12px; }
  .more-btn { min-height: 44px; font-size: 15px; }
  .swap-btn { width: 44px; height: 44px; border-radius: 22px; font-size: 20px; }
  .toggle { min-height: 44px; font-size: 15px; gap: 12px; }
  .toggle input { width: 24px; height: 24px; }
  .locate-btn { width: 44px; height: 44px; }
  .locate-btn svg { width: 22px; height: 22px; }
  .leaflet-touch .leaflet-bar a { width: 44px; height: 44px; line-height: 44px; font-size: 22px; }
  .leaflet-container a.leaflet-popup-close-button { width: 44px; height: 44px; font-size: 28px; line-height: 44px; text-align: center; padding: 0; top: 0; right: 0; }
  .leaflet-popup-content { margin-right: 44px; font-size: 14px; }
  .legal-close { min-width: 48px; min-height: 48px; font-size: 32px; }
  .legal-body { font-size: 15px; }
  .footer .legal-link { display: inline-flex; align-items: center; min-height: 44px; margin-left: 0; }
  .date-note, .cal-note { font-size: 13px; }
  .theme-btn { min-height: 44px; min-width: 44px; padding: 0 14px; }
}

/* ---------- compact banner, footer folded into the banner details: phones, short landscape screens and small tablets ---------- */
@media (max-width: 820px), (max-height: 500px) and (orientation: landscape) {
  /* banner: one row with the key warning, an info button and an icon-only theme button */
  .banner { flex-wrap: nowrap; gap: 6px; padding-top: max(4px, env(safe-area-inset-top)); padding-bottom: 4px; }
  .brand { flex: none; margin-right: 0; }
  .banner-short { display: block; flex: 1; min-width: 0; margin: 0; font-size: 12px; line-height: 1.25; font-weight: 600; }
  .info-btn { display: inline-flex; flex: none; align-items: center; justify-content: center; width: 44px; height: 44px; padding: 0; font: italic 700 18px/1 Georgia, serif; color: var(--banner-text); background: transparent; border: 0; cursor: pointer; }
  .info-btn span { display: inline-flex; width: 26px; height: 26px; align-items: center; justify-content: center; border: 2px solid currentColor; border-radius: 50%; }
  .banner.info-open .info-btn span { background: rgba(255,255,255,.25); }
  .theme-btn { order: 0; margin-left: 0; flex: none; width: 44px; height: 44px; min-width: 44px; min-height: 44px; padding: 0; border-radius: 50%; }
  #theme-label { position: absolute; width: 1px; height: 1px; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); clip-path: inset(50%); white-space: nowrap; }
  .banner-full { display: none; position: absolute; top: 100%; left: 0; right: 0; flex-direction: column; align-items: flex-start; gap: 8px; padding: 10px max(14px, env(safe-area-inset-right)) 12px max(14px, env(safe-area-inset-left)); background: var(--banner-bg); color: var(--banner-text); font-size: 13px; line-height: 1.4; box-shadow: 0 10px 18px var(--shadow); border-top: 1px solid rgba(255,255,255,.3); max-height: calc(100dvh - 60px); overflow-y: auto; }
  .banner.info-open .banner-full { display: flex; }
  .banner-credits { display: block; font-size: 12px; opacity: .95; }
  .banner-credits .legal-link { display: flex; width: fit-content; align-items: center; min-height: 44px; margin-top: 2px; font-weight: 700; font-size: 14px; }
  .footer { display: none; }

}

/* ---------- compact layouts: phones in portrait (bottom sheet) and short landscape screens ---------- */
@media (max-width: 700px) and (orientation: portrait), (max-width: 559px), (max-height: 500px) and (orientation: landscape) {
  /* the panel scrolls as one block; the section title of the stop search is not needed */
  .sheet-scroll { display: block; overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; padding-bottom: env(safe-area-inset-bottom); }
  .legend-body, .dep-panel { display: block; overflow: visible; flex: none; }
  .legend-body[hidden], .dep-panel[hidden] { display: none; }
  .dep-panel .legend-list, .buses-section .legend-list { overflow: visible; max-height: none; flex: none; }
  .dep-head { position: sticky; top: 0; z-index: 3; }
  .sheet-scroll { scroll-padding-top: 56px; } /* focus / scrollIntoView clear the sticky header */
  .cal-day, .cal-blank { height: 44px; }

  /* legal notice: full screen */
  .legal { width: 100%; max-width: none; height: 100vh; height: 100dvh; max-height: none; margin: 0; border: 0; border-radius: 0; }
  .legal-head { padding: max(8px, env(safe-area-inset-top)) max(12px, env(safe-area-inset-right)) 8px max(16px, env(safe-area-inset-left)); }
  .legal-close { min-width: 48px; min-height: 48px; font-size: 32px; }
  .legal-body { padding: 4px max(16px, env(safe-area-inset-right)) max(24px, env(safe-area-inset-bottom)) max(16px, env(safe-area-inset-left)); }
  .locate-msg { left: 64px; right: 8px; max-width: none; }
  /* pinch-zoom is enough on phones; the locate button stays */
  .leaflet-touch .leaflet-control-zoom { display: none; }
}

/* ---------- bottom sheet (phones in portrait) ---------- */
@media (max-width: 700px) and (orientation: portrait), (max-width: 559px) {
  #sec-stop { position: absolute; width: 1px; height: 1px; margin: -1px; overflow: hidden; clip: rect(0 0 0 0); clip-path: inset(50%); white-space: nowrap; }
  .legend {
    left: 0; right: 0; top: auto; bottom: 0; width: auto;
    height: calc(100% - 64px);
    border-radius: 16px 16px 0 0; box-shadow: 0 -2px 14px var(--shadow);
    transform: translate3d(0, calc(100% - 108px), 0);
    transition: transform .24s cubic-bezier(.2, .8, .2, 1);
    will-change: transform;
    padding-left: env(safe-area-inset-left); padding-right: env(safe-area-inset-right);
  }
  .legend.dragging { transition: none; }
  .legend-toggle { min-height: 48px; padding: 20px 14px 6px; cursor: pointer; touch-action: none; user-select: none; -webkit-user-select: none; border-bottom: 0; background: var(--surface); }
  .grabber { display: block; position: absolute; top: 8px; left: 50%; width: 40px; height: 5px; margin-left: -20px; border-radius: 3px; background: var(--text-3); opacity: .6; }
  .legend-toggle:active .grabber { opacity: 1; }
  .chev { display: block; margin-right: 4px; transform: rotate(-135deg); transition: transform .2s; }
  .legend[data-state="full"] .chev { transform: rotate(45deg); }
  .legend[data-state="peek"] .sheet-scroll, .legend[data-state="half"] .sheet-scroll { overflow-y: hidden; touch-action: none; }
  .legend[data-state="full"] .sheet-scroll { touch-action: pan-y; }
  .legend.dragging, .legend.dragging * { user-select: none; -webkit-user-select: none; }
  .legend-body { border-top: 1px solid var(--border-2); }
  /* the map attribution rides on top of the sheet (translated by sheet.js) */
  .leaflet-bottom.leaflet-right { margin-right: 0; transition: transform .24s cubic-bezier(.2, .8, .2, 1); }
  .leaflet-bottom.leaflet-right.no-anim { transition: none; }
}

/* ---------- short landscape screens (side panel, compact chrome) ---------- */
@media (max-height: 500px) and (orientation: landscape) and (min-width: 560px) {
  .legend-toggle { padding: 6px 12px; }
  .sec { padding-top: 6px; }
}

/* very small widths */
@media (max-width: 380px) {
  .brand-name { display: none; }
  .legend-toggle { font-size: 13px; }
}
