:root {
  --bg: #111317;
  --field: #15181c;
  --panel: #1b1f25;
  --panel-2: #232830;
  --line: #2a2f36;
  --ink: #ece9e2;
  --ink-2: #a9ada8;
  --ink-3: #6f746f;
  --warn: #ffb454;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
@supports (height: 100dvh) { body { height: 100dvh; } } /* iOS Safari: the toolbar must not cover the page bottom */
body {
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.4 -apple-system, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

.bar {
  display: flex;
  align-items: baseline;
  gap: 10px;
  padding: 10px 16px;
  border-bottom: 1px solid var(--line);
}
.bar h1 { font-size: 17px; font-weight: 600; margin: 0 8px 0 0; letter-spacing: -0.01em; }
.status { margin: 0; flex: 1; color: var(--ink-2); font-variant-numeric: tabular-nums; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.status.warn { color: var(--warn); }

.credit { color: var(--ink-3); font-size: 13px; text-decoration: none; white-space: nowrap; }
.credit:hover { color: var(--ink); text-decoration: underline; }

.ghost {
  background: none;
  border: 1px solid var(--line);
  color: var(--ink-2);
  border-radius: 6px;
  padding: 4px 10px;
  font: inherit;
  font-size: 13px;
  cursor: pointer;
  white-space: nowrap;
}
.ghost:hover { color: var(--ink); border-color: var(--ink-3); }
.ghost[aria-pressed="true"] { color: var(--ink); background: var(--panel-2); border-color: var(--ink-3); }
.ghost:focus-visible, .row:focus-visible, .name:focus-visible { outline: 2px solid var(--ink); outline-offset: 2px; }

.stage { flex: 1; display: flex; min-height: 0; position: relative; }
#map { flex: 1; min-width: 0; background: var(--field); }
.leaflet-container { background: var(--field); font-family: inherit; }
.leaflet-container:focus-visible { outline: 2px solid var(--ink); outline-offset: -2px; }
body.base-streets .leaflet-container { background: #e9e5df; }
/* Dark basemap: the standard OSM tiles inverted and muted, one composited layer. */
body.base-dark .leaflet-tile-pane { filter: invert(1) hue-rotate(180deg) brightness(0.78) contrast(0.92) saturate(0.3); }
.leaflet-bar { border: 1px solid var(--line); box-shadow: none; }
.leaflet-bar a { background: var(--panel); color: var(--ink-2); border-bottom-color: var(--line); }
.leaflet-bar a:hover, .leaflet-bar a:focus { background: #262b33; color: var(--ink); }
.leaflet-bar a.leaflet-disabled { background: var(--panel); color: var(--ink-3); }
.leaflet-control-attribution { background: rgba(27, 31, 37, 0.85); color: var(--ink-3); font-size: 11px; }
.leaflet-control-attribution a { color: var(--ink-2); }
body.base-streets .leaflet-control-attribution { background: rgba(255, 255, 255, 0.8); color: #555; }
body.base-streets .leaflet-control-attribution a { color: #333; }

/* ---- side list ---- */
.list { width: 320px; border-left: 1px solid var(--line); background: var(--panel); overflow-y: auto; padding: 0 0 12px; }
body.list-hidden .list { display: none; }
.filters { display: flex; flex-wrap: wrap; gap: 6px; align-items: center; padding: 10px 14px; border-bottom: 1px solid var(--line); position: sticky; top: 0; background: var(--panel); z-index: 1; }
.filters-label { color: var(--ink-3); font-size: 13px; margin-right: 2px; }
.filters .ghost { padding: 2px 8px; font-size: 12px; }

.route { border-bottom: 1px solid var(--line); }
.route h2 { font-size: 13px; font-weight: 600; margin: 0; display: flex; align-items: center; gap: 8px; color: var(--ink-2); padding: 8px 14px 8px 12px; }
.route h2 .vis { margin: 0; width: 14px; height: 14px; accent-color: var(--ink-2); cursor: pointer; flex: none; }
.route h2 .name { flex: 1; min-width: 0; background: none; border: 0; color: inherit; font: inherit; text-align: left; padding: 0; cursor: pointer; display: flex; align-items: center; gap: 8px; }
.route h2 .name .label { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.route h2 .count { color: var(--ink-3); font-weight: 500; font-variant-numeric: tabular-nums; }
.route h2 .chev { color: var(--ink-3); width: 14px; text-align: center; transition: transform 120ms; }
.route.collapsed h2 .chev { transform: rotate(-90deg); }
.route.collapsed ul, .route.off ul { display: none; }
.route.off h2 { color: var(--ink-3); }
.group { border-bottom: 1px solid var(--line); }
.group > h2 { font-size: 13px; font-weight: 700; margin: 0; display: flex; align-items: center; gap: 8px; color: var(--ink); padding: 9px 14px 9px 12px; }
.group > h2 .vis { margin: 0; width: 15px; height: 15px; accent-color: var(--ink-2); cursor: pointer; flex: none; }
.group > h2 .name { flex: 1; min-width: 0; background: none; border: 0; color: inherit; font: inherit; text-align: left; padding: 0; cursor: pointer; display: flex; align-items: center; gap: 8px; }
.group > h2 .count { color: var(--ink-3); font-weight: 500; font-variant-numeric: tabular-nums; }
.group > h2 .chev { color: var(--ink-3); width: 14px; text-align: center; transition: transform 120ms; }
.group.collapsed > h2 .chev { transform: rotate(-90deg); }
.group.collapsed .group-body { display: none; }
.group.off > h2 { color: var(--ink-3); }
.group .route { border-bottom: 0; }
.group .route h2 { padding-left: 26px; font-weight: 500; gap: 6px; }
.group .route .row { padding-left: 64px; }
.group .route .none { padding-left: 64px; }

.eye { width: 26px; height: 26px; padding: 0; border: 0; background: none; color: var(--ink-2); cursor: pointer; display: grid; place-items: center; flex: none; border-radius: 6px; touch-action: manipulation; }
.eye svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; }
.eye .pupil { fill: currentColor; stroke: none; }
.eye .slash { display: none; }
.eye.closed { color: var(--ink-3); }
.eye.closed .pupil { display: none; }
.eye.closed .slash { display: block; }
.eye.mixed { color: var(--ink-3); }
.eye:hover { color: var(--ink); background: rgba(255, 255, 255, 0.06); }
.eye:focus-visible { outline: 2px solid var(--ink); outline-offset: 1px; }
.pip-btn { border: 0; padding: 0; cursor: pointer; width: 12px; height: 12px; border-radius: 3px; outline-offset: 2px; }
.pip-btn:hover { box-shadow: 0 0 0 2px var(--ink-2); }
.route.unseen h2 .label, .group.unseen > h2 .label { text-decoration: line-through; color: var(--ink-3); }
.eye.hiddeneye { visibility: hidden; }
.route.micro h2 .name { display: flex; align-items: center; gap: 8px; flex: 1; min-width: 0; }
.route.micro .micro-note:empty { display: none; }
.group .route.micro .none { padding: 0 14px 6px 64px; }
.route.unseen .pip-btn { opacity: 0.3; }
.group > h2 .vis, .route h2 .vis { margin-left: 2px; }

.train-icon .nose { stroke: var(--field); stroke-width: 1; }
.train-icon .num { font-size: 11px; font-weight: 600; fill: var(--ink); paint-order: stroke; stroke: var(--field); stroke-width: 3px; stroke-linejoin: round; pointer-events: none; }
body.base-streets .train-icon .num { fill: #111; stroke: #fff; }
.train-icon.dim .nose, .train-icon.dim .num { opacity: 0.25; }
.route.off h2 .pip { opacity: 0.35; }
.route.idle h2 .label { color: var(--ink-3); }
.route.idle h2 .count { font-variant-numeric: normal; font-weight: 400; }
.route.idle h2 .pip { opacity: 0.35; }
.group.idle > h2 .label { color: var(--ink-3); }
.group.idle > h2 .count { font-variant-numeric: normal; font-weight: 400; }
.idle-wrap.collapsed .idle-body { display: none; }
.idle-toggle { margin: 0; font-size: 12px; font-weight: 500; color: var(--ink-3); padding: 2px 14px 2px 26px; display: flex; align-items: center; }
.idle-toggle .name { flex: 1; min-width: 0; min-height: 32px; background: none; border: 0; color: inherit; font: inherit; text-align: left; padding: 0; cursor: pointer; display: flex; align-items: center; gap: 8px; }
.idle-toggle .count { font-variant-numeric: tabular-nums; }
.idle-toggle .chev { width: 14px; text-align: center; transition: transform 120ms; }
.idle-wrap.collapsed .idle-toggle .chev { transform: rotate(-90deg); }
.idle-body .route h2 { padding-left: 40px; }
.legal { font-size: 11px; line-height: 1.4; color: var(--ink-3); margin: 0; padding: 12px 14px 16px; border-top: 1px solid var(--line, rgba(255,255,255,0.08)); }
.route.focus h2 { color: var(--ink); }
.pip { width: 10px; height: 10px; border-radius: 3px; display: inline-block; flex: none; }
.list ul { list-style: none; margin: 0; padding: 0 0 6px; }
.row {
  display: grid; grid-template-columns: 40px minmax(0, 1fr) auto; gap: 8px; align-items: baseline;
  padding: 4px 14px 4px 34px; font-size: 13px; cursor: pointer;
}
.row:hover, .row.hl { background: rgba(255, 255, 255, 0.06); }
.row.sel { background: rgba(255, 255, 255, 0.1); }
.row .vid { font-variant-numeric: tabular-nums; }
.row .dest { color: var(--ink-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.row .late { color: var(--ink-3); font-variant-numeric: tabular-nums; }
.row .late.behind { color: var(--warn); }
.row.stale .vid, .row.stale .dest { color: var(--ink-3); }
.none { padding: 2px 14px 8px 34px; color: var(--ink-3); font-size: 12px; }
.empty { padding: 20px 14px; color: var(--ink-3); font-size: 13px; }

/* ---- selected train card ---- */
.detail {
  position: absolute; left: 12px; bottom: 28px; z-index: 900; width: min(340px, calc(100% - 24px));
  background: var(--panel); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px 10px;
  box-shadow: 0 8px 28px rgba(0, 0, 0, 0.5); font-size: 13px; line-height: 1.4;
}
.d-head { display: flex; align-items: center; gap: 8px; }
.d-head .pip:empty:not([style*="transparent"]) { }
.d-head .pip.pips { width: auto; height: auto; display: inline-flex; gap: 3px; background: none; }
.pip-mini { width: 10px; height: 10px; border-radius: 3px; display: inline-block; }
.d-board li.board-row { align-items: center; gap: 8px; padding: 5px 0; cursor: pointer; border-top: 1px solid var(--line); }
.d-board li.board-row:first-child { border-top: 0; }
.d-board li.board-row:hover { color: var(--ink); }
.d-board .eta { margin-left: auto; color: var(--ink); white-space: nowrap; font-variant-numeric: tabular-nums; }
.d-board .eta.est { color: var(--ink-3); }
.d-facts dd.muted { color: var(--ink-3); }
.station-hit { cursor: pointer; }
.d-route { font-weight: 600; flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.d-close { padding: 2px 8px; font-size: 12px; }
.d-title { color: var(--ink); margin-top: 4px; }
.d-facts { display: grid; grid-template-columns: max-content minmax(0, 1fr); gap: 3px 10px; margin: 8px 0 0; }
.d-facts dt { color: var(--ink-3); }
.d-facts dd { margin: 0; color: var(--ink-2); min-width: 0; }
.d-facts dd.strong { color: var(--ink); }
.d-facts dd .behind { color: var(--warn); }
.d-facts .eta { color: var(--ink); font-variant-numeric: tabular-nums; }
.d-facts .src { color: var(--ink-3); }
.d-upcoming { margin: 0; padding: 0; list-style: none; }
.d-upcoming li { display: flex; gap: 8px; }
.d-upcoming li span:last-child { margin-left: auto; color: var(--ink-3); font-variant-numeric: tabular-nums; white-space: nowrap; }
.d-actions { display: flex; align-items: center; gap: 10px; margin-top: 10px; }
.d-note { color: var(--ink-3); font-size: 12px; }

/* ---- map glyphs ---- */
.line-hit { cursor: pointer; }
.train-icon svg { display: block; overflow: visible; }
.train-icon .hit { fill: transparent; }
.train-icon .body { stroke: var(--field); stroke-width: var(--sw, 1.3); transition: none; }
body.base-streets .train-icon .body { stroke: #ffffff; }
.train-icon.hl .body, .train-icon:hover .body { stroke: var(--ink); stroke-width: calc(var(--sw, 1.3) + 0.3); }
.train-icon.sel .body { stroke: var(--ink); stroke-width: calc(var(--sw, 1.3) + 0.8); }
body.base-streets .train-icon.hl .body, body.base-streets .train-icon:hover .body, body.base-streets .train-icon.sel .body { stroke: #111; }
.train-icon.unsnapped .body { opacity: 0.6; stroke-dasharray: 1.6 1.2; }
.train-icon.stale .body { opacity: 0.35; }
.train-icon.off { display: none; }
.train-icon.dim .body { opacity: 0.25; }
.train-icon.dim.stale .body { opacity: 0.12; }

.station-label {
  white-space: nowrap; font-size: 11px; line-height: 14px; color: var(--ink-2); cursor: pointer; pointer-events: auto;
  text-shadow: 0 0 3px var(--field), 0 0 3px var(--field), 0 0 2px var(--field);
}
.station-label.focus { color: var(--ink); }
body.base-streets .station-label { color: #333; text-shadow: 0 0 3px #fff, 0 0 3px #fff, 0 0 2px #fff; }

.tip {
  position: absolute; z-index: 1000; pointer-events: none;
  background: var(--panel); border: 1px solid var(--line); border-radius: 8px;
  padding: 8px 10px; font-size: 13px; line-height: 1.35; max-width: 280px;
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.45);
}
.tip .t1 { font-weight: 600; display: flex; align-items: center; gap: 8px; }
.tip .t2 { color: var(--ink-2); margin-top: 2px; }
.tip .muted { color: var(--ink-3); }
.tip .behind { color: var(--warn); }

/* ---- phone layout: map full screen, list and train card as bottom sheets ---- */
.map-tools { display: none; }
.sheet-close { display: none; }
.ghost { touch-action: manipulation; }

@media (max-width: 720px) {
  .bar { gap: 10px; padding: 8px 12px; align-items: center; min-height: 44px; }
  .bar h1 { font-size: 15px; margin: 0; }
  .status { font-size: 13px; }
  #basemap, #fit, #toggle-list { display: none; }
  .credit { font-size: 12px; }

  .map-tools { display: flex; flex-direction: column; gap: 8px; position: absolute; top: 10px; right: 10px; z-index: 960; }
  .map-tools .ghost { width: 44px; height: 40px; padding: 0; border-radius: 10px; background: var(--panel); color: var(--ink); display: grid; place-items: center; font-size: 13px; box-shadow: 0 2px 10px rgba(0, 0, 0, 0.4); }
  .map-tools svg { width: 22px; height: 22px; fill: none; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; }
  #m-list[aria-pressed="true"] { background: var(--ink); color: var(--bg); }
  .leaflet-control-zoom { display: none; }

  .list {
    position: absolute; left: 0; right: 0; bottom: 0; top: auto; width: auto; height: min(62vh, 560px);
    border-left: 0; border-top: 1px solid var(--line); border-radius: 14px 14px 0 0;
    box-shadow: 0 -8px 30px rgba(0, 0, 0, 0.5); z-index: 900; padding-bottom: calc(12px + env(safe-area-inset-bottom));
    transform: translateY(0); transition: transform 180ms ease-out;
  }
  body.list-hidden .list { display: block; transform: translateY(110%); }
  body.has-selection .list { display: none; }
  .filters { padding: 12px 14px 10px; gap: 8px; }
  .filters .ghost { padding: 6px 12px; font-size: 13px; min-height: 34px; }
  .sheet-close { display: inline-block; margin-left: auto; }
  .route h2 { min-height: 46px; padding: 4px 14px 4px 12px; font-size: 14px; }
  .idle-toggle { font-size: 13px; }
  .idle-toggle .name { min-height: 44px; }
  .route h2 .vis { width: 20px; height: 20px; }
  .row { min-height: 44px; align-items: center; padding: 6px 14px 6px 40px; font-size: 14px; }
  .none { padding-left: 40px; }

  .detail {
    left: 0; right: 0; bottom: 0; width: auto; max-height: min(58vh, 520px); overflow: auto;
    border-radius: 14px 14px 0 0; border-left: 0; border-right: 0; border-bottom: 0;
    padding: 10px 16px calc(14px + env(safe-area-inset-bottom)); z-index: 950; font-size: 14px;
  }
  .detail::before { content: ""; display: block; width: 36px; height: 4px; border-radius: 2px; background: var(--ink-3); margin: 0 auto 10px; }
  .d-close { min-height: 36px; padding: 6px 12px; }
  .d-actions .ghost { min-height: 36px; }
  .tip { display: none; }
}
@media (pointer: coarse) {
  .ghost { min-height: 36px; }
}
