/* Metro-North Radar dashboard styles */

:root {
  --bg: #0e1116;
  --panel: #161b22;
  --panel-border: #30363d;
  --text: #e6edf3;
  --muted: #8b949e;
  --late: #ff5454;
  --very-late: #ff0033;
  --early: #58a6ff;
  --on-time: #3fb950;
  --held: #d29922;
  --hudson: #009B3A;
  --harlem: #0039A6;
  --new-haven: #EE0034;
  --new-canaan: #EE0034;
  --danbury: #EE0034;
  --waterbury: #EE0034;
  --amtrak: #00537E;
}

* { box-sizing: border-box; }
html, body {
  margin: 0;
  /* Fill the viewport exactly. 100dvh accounts for iOS standalone mode's
     home-indicator zone properly; 100vh fallback for older browsers. */
  height: 100vh;
  height: 100dvh;
}
/* Critical for iOS PWA standalone: extend the html box past the safe areas
   so iOS actually populates env(safe-area-inset-*) values. Without this,
   iOS Safari in portrait mode returns 0 for all insets, defeating the
   safe-area-aware UI positioning below. See:
   https://developer.apple.com/forums/thread/699415 */
html {
  min-height: calc(100% + env(safe-area-inset-top) + env(safe-area-inset-bottom));
}
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  overflow: hidden;
}
#map { position: absolute; inset: 0; }

/* ── Left panel ─────────────────────────────────────────────────────────── */
.panel {
  position: absolute;
  top: calc(16px + env(safe-area-inset-top));
  left: calc(16px + env(safe-area-inset-left));
  width: 320px;
  /* Subtract both top and bottom insets so a notched iPhone in standalone
     mode doesn't make the panel scroll off-screen. */
  max-height: calc(100vh - 32px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
  max-height: calc(100dvh - 32px - env(safe-area-inset-top) - env(safe-area-inset-bottom));
  overflow-y: auto;
  background: rgba(22, 27, 34, 0.92);
  backdrop-filter: blur(8px);
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  padding: 16px;
  font-size: 13px;
  line-height: 1.4;
  z-index: 1;
}
.panel h1 {
  margin: 0 0 12px;
  font-size: 14px;
  font-weight: 600;
  letter-spacing: 0.5px;
  text-transform: uppercase;
}
.panel-nav {
  display: flex;
  gap: 6px;
  margin-bottom: 12px;
  flex-wrap: wrap;
}
.nav-chip {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  border: 1px solid var(--panel-border);
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.02);
  color: var(--text);
  text-decoration: none;
  font-size: 12px;
  font-weight: 500;
  font-family: inherit;
  cursor: pointer;
  transition: background-color 0.12s, border-color 0.12s, color 0.12s;
}
.nav-chip:hover {
  background: rgba(88, 166, 255, 0.08);
  border-color: var(--early);
  color: var(--early);
}
.nav-chip svg {
  flex-shrink: 0;
  color: var(--muted);
  transition: color 0.12s;
}
.nav-chip:hover svg { color: var(--early); }

.stat-row {
  display: flex;
  justify-content: space-between;
  padding: 4px 0;
  border-bottom: 1px solid #21262d;
}
.stat-row:last-of-type { border-bottom: 0; }
.stat-row .label { color: var(--muted); }
.stat-row .value { font-variant-numeric: tabular-nums; }

/* Live stats block — anchored to the bottom of the panel. Sits below the
   filter group and is visually quieter since the user looks here less often
   than at the filters. */
.panel-stats {
  margin-top: 16px;
  padding-top: 10px;
  border-top: 1px solid var(--panel-border);
  font-size: 12px;
  opacity: 0.85;
}

/* Disclaimer footer inside the panel — quieter than the stats, but readable.
   Keeps the user's expectation aligned (this is independent, not official). */
.panel-disclaimer {
  margin-top: 14px;
  padding-top: 10px;
  border-top: 1px solid var(--panel-border);
  font-size: 10.5px;
  line-height: 1.4;
  color: var(--muted);
}
.panel-disclaimer p { margin: 0 0 6px; }
.panel-disclaimer p:last-child { margin-bottom: 0; }
.contact-link {
  color: var(--early);
  text-decoration: none;
}
.contact-link:hover { text-decoration: underline; }

/* ── Contact modal ──────────────────────────────────────────────────────── */
.contact-modal {
  position: fixed;
  inset: 0;
  z-index: 1000;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}
.contact-modal[hidden] { display: none; }
.contact-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.65);
  /* No blur — keeps it cheap to render and consistent across browsers */
}
.contact-dialog {
  position: relative;
  background: #161b22;
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  padding: 22px 24px 20px;
  width: 100%;
  max-width: 440px;
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.6);
}
.contact-dialog h2 {
  margin: 0 0 16px;
  font-size: 16px;
  font-weight: 600;
  letter-spacing: 0.3px;
}
.contact-close {
  position: absolute;
  top: 8px;
  right: 12px;
  background: transparent;
  border: 0;
  color: var(--muted);
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  padding: 6px;
}
.contact-close:hover { color: var(--text); }
.contact-form label {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin-bottom: 12px;
}
.contact-form label span {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--muted);
  font-weight: 600;
}
.contact-form input,
.contact-form textarea {
  background: #0d1117;
  border: 1px solid var(--panel-border);
  border-radius: 5px;
  padding: 8px 10px;
  color: var(--text);
  font-family: inherit;
  font-size: 14px;
  outline: none;
  transition: border-color 0.12s;
}
.contact-form input:focus,
.contact-form textarea:focus { border-color: #58a6ff; }
.contact-form textarea { resize: vertical; }
.contact-actions {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
  margin-top: 8px;
}
.contact-cancel,
.contact-send {
  font-family: inherit;
  font-size: 13px;
  font-weight: 500;
  padding: 8px 16px;
  border-radius: 5px;
  cursor: pointer;
  border: 1px solid var(--panel-border);
}
.contact-cancel {
  background: transparent;
  color: var(--text);
}
.contact-cancel:hover { background: rgba(255, 255, 255, 0.04); }
.contact-send {
  background: var(--early);
  color: #0d1117;
  border-color: var(--early);
  font-weight: 600;
}
.contact-send:hover { background: #79b8ff; }
.contact-note {
  margin: 10px 0 0;
  font-size: 11px;
  color: var(--muted);
  text-align: right;
}

@media (max-width: 600px) {
  .contact-dialog { padding: 18px 18px 16px; }
  .contact-form input,
  .contact-form textarea { font-size: 16px; /* prevent iOS zoom */ }
}

.branches { margin-top: 12px; }
.branch-row {
  display: grid;
  grid-template-columns: 12px 1fr auto auto;
  gap: 8px;
  align-items: center;
  padding: 3px 0;
  font-size: 12px;
}
.swatch { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.badge {
  background: #21262d;
  padding: 1px 6px;
  border-radius: 3px;
  font-size: 11px;
  font-variant-numeric: tabular-nums;
}
.badge.late { background: rgba(255, 84, 84, 0.25); color: var(--late); }

#status-dot {
  display: inline-block;
  width: 6px;
  height: 6px;
  border-radius: 50%;
  background: var(--on-time);
  margin-right: 6px;
  vertical-align: middle;
  animation: pulse 2s infinite;
}
@keyframes pulse { 50% { opacity: 0.35; } }

/* ── Chips and status colors ────────────────────────────────────────────── */
.chip {
  display: inline-block;
  padding: 1px 6px;
  border-radius: 10px;
  font-size: 10px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.5px;
}
.chip.on-time   { background: rgba(63, 185, 80, 0.2); color: var(--on-time); }
.chip.late      { background: rgba(255, 84, 84, 0.2); color: var(--late); }
.chip.very-late { background: rgba(255, 0, 51, 0.25); color: var(--very-late); }
.chip.early     { background: rgba(88, 166, 255, 0.2); color: var(--early); }
.chip.held      { background: rgba(210, 153, 34, 0.25); color: var(--held); }
.chip.canceled  { background: rgba(255, 84, 84, 0.35); color: #fff; }
.chip.deadhead  { background: rgba(157, 166, 177, 0.25); color: #c9d1d9; }

/* ── Filter / power segmented ───────────────────────────────────────────── */
.filter {
  margin-top: 14px;
  padding-top: 12px;
  border-top: 1px solid var(--panel-border);
}
.filter h2 {
  margin: 0 0 8px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.5px;
  text-transform: uppercase;
  color: var(--muted);
}

/* Collapsible filter sections — header is a clickable button that toggles
   data-collapsed on its parent .filter-group. Bodies are display:none when
   collapsed; no animation since the panel is short and the open/close needs
   to feel snappy. */
.filter-group + .filter-group {
  margin-top: 8px;
  border-top: 1px solid #21262d;
  padding-top: 8px;
}
.filter-group-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  padding: 6px 0;
  background: transparent;
  border: 0;
  color: var(--muted);
  font-family: inherit;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.5px;
  text-transform: uppercase;
  cursor: pointer;
  text-align: left;
  transition: color 0.12s;
}
.filter-group-header:hover { color: var(--text); }
.filter-group-chevron {
  font-size: 10px;
  transition: transform 0.15s;
  color: var(--muted);
}
.filter-group[data-collapsed="false"] .filter-group-chevron {
  transform: rotate(180deg);
}
.filter-group-body {
  padding-top: 6px;
}
.filter-group[data-collapsed="true"] .filter-group-body {
  display: none;
}
.seg {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 4px;
  background: #0d1117;
  border: 1px solid var(--panel-border);
  border-radius: 6px;
  padding: 3px;
  margin-bottom: 10px;
}
.seg button {
  background: transparent;
  color: var(--muted);
  border: 0;
  padding: 5px 8px;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.3px;
  text-transform: uppercase;
  border-radius: 4px;
  cursor: pointer;
  font-family: inherit;
  transition: background 0.15s, color 0.15s;
}
.seg button:hover { color: var(--text); }
.seg button.active { background: #21262d; color: var(--text); }
.seg button.active.electric { color: #58a6ff; }
.seg button.active.diesel { color: #d29922; }
.seg button.active.revenue { color: #3fb950; }
.seg button.active.deadhead { color: #9da6b1; }

.sub-filter { max-height: 220px; overflow-y: auto; margin-bottom: 4px; }
.sub-filter-heading {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--muted);
  padding: 6px 0 2px;
  font-weight: 600;
}
.sub-filter-heading:first-child { padding-top: 0; }
.sub-filter:empty::before {
  content: "—";
  color: var(--muted);
  font-size: 11px;
  font-style: italic;
}
.filter-row {
  display: grid;
  grid-template-columns: 16px 1fr auto;
  gap: 8px;
  align-items: center;
  padding: 3px 4px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 12px;
  user-select: none;
}
.filter-row:hover { background: #21262d; }
.filter-row input { margin: 0; accent-color: #58a6ff; cursor: pointer; }
.filter-row .name { font-variant-numeric: tabular-nums; }
.filter-row .count { color: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }

.branch-dot {
  display: inline-block;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}
.branch-filter .filter-row {
  grid-template-columns: 16px 10px 1fr auto;
}
.branch-dot[data-branch="Hudson"]      { background: #009B3A; }
.branch-dot[data-branch="Harlem"]      { background: #0039A6; }
.branch-dot[data-branch="New Haven"]   { background: #EE0034; }
.branch-dot[data-branch="New Canaan"]  { background: transparent; border: 2px solid #EE0034; box-sizing: border-box; }
.branch-dot[data-branch="Danbury"]     { background: transparent; border: 2px solid #EE0034; box-sizing: border-box; }
.branch-dot[data-branch="Waterbury"]   { background: transparent; border: 2px solid #EE0034; box-sizing: border-box; }
.branch-dot[data-branch="Amtrak"]      { background: #00537E; }
.filter-actions { display: flex; gap: 8px; margin-top: 6px; }
.filter-actions button {
  flex: 1;
  background: transparent;
  color: var(--muted);
  border: 1px solid var(--panel-border);
  border-radius: 4px;
  padding: 4px 6px;
  font-size: 11px;
  cursor: pointer;
  font-family: inherit;
}
.filter-actions button:hover { color: var(--text); border-color: #484f58; }

.toggle-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 6px 4px;
  font-size: 12px;
  cursor: pointer;
  user-select: none;
}
.toggle-row:hover { background: #21262d; border-radius: 4px; }
.toggle-row input { margin: 0; accent-color: #58a6ff; cursor: pointer; }

/* Track feed hide state — only hides the cards; the toggle header stays */
.track-feed-cards.hidden { display: none; }

/* ── Floating search box (top-right of map) ────────────────────────────── */
.search-box {
  position: absolute;
  top: 16px;
  right: 16px;
  width: 280px;
  z-index: 2;
  background: rgba(22, 27, 34, 0.95);
  backdrop-filter: blur(8px);
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  display: flex;
  align-items: center;
  padding: 0 10px;
  transition: border-color 0.15s;
}
.search-box:focus-within {
  border-color: #58a6ff;
}
.search-icon {
  color: var(--muted);
  flex-shrink: 0;
  margin-right: 6px;
}
.search-box input {
  flex: 1;
  background: transparent;
  color: var(--text);
  border: 0;
  padding: 9px 0;
  font-size: 13px;
  font-family: inherit;
  font-variant-numeric: tabular-nums;
  outline: none;
  min-width: 0;
}
.search-box input::placeholder { color: var(--muted); }
.search-box button#search-clear {
  visibility: hidden;
  background: transparent;
  border: 0;
  color: var(--muted);
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  padding: 4px 6px;
  font-family: inherit;
  flex-shrink: 0;
}
.search-box button#search-clear:hover { color: var(--text); }
.search-box.has-value button#search-clear { visibility: visible; }

.search-suggestions {
  position: absolute;
  top: calc(100% + 4px);
  left: 0;
  right: 0;
  background: rgba(22, 27, 34, 0.98);
  backdrop-filter: blur(8px);
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  max-height: 320px;
  overflow-y: auto;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
}
.search-suggestions:not([hidden]) { display: block; }
.search-suggestion {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 8px;
  align-items: center;
  padding: 8px 12px;
  cursor: pointer;
  font-size: 12px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.04);
}
.search-suggestion:last-child { border-bottom: 0; }
.search-suggestion:hover,
.search-suggestion.active {
  background: rgba(88, 166, 255, 0.12);
}
.search-suggestion .branch-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex-shrink: 0;
}
.search-suggestion .sug-main {
  display: flex;
  flex-direction: column;
  min-width: 0;
}
.search-suggestion .sug-train {
  font-weight: 600;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}
.search-suggestion .sug-sub {
  font-size: 11px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.search-suggestion .sug-meta {
  font-size: 10px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.search-suggestion-empty {
  padding: 12px;
  font-size: 12px;
  color: var(--muted);
  text-align: center;
}

/* Push Mapbox zoom/compass controls down so they don't collide with the
   floating search box anchored at top-right. */
.mapboxgl-ctrl-top-right {
  top: calc(56px + env(safe-area-inset-top));
}

@media (max-width: 600px) {
  .search-box {
    /* Move below the playback bar (which sits at top: 18px + safe-area inset).
       The playback bar is ~40px tall, so anchor the search just below it.
       Add safe-area inset so notched iPhones in standalone mode push it down
       proportionally. */
    top: 66px;
    top: calc(66px + constant(safe-area-inset-top));
    top: calc(66px + env(safe-area-inset-top));
    left: calc(16px + env(safe-area-inset-left));
    right: calc(16px + env(safe-area-inset-right));
    width: auto;
  }
  /* Mapbox controls need to clear the now-lower search box on mobile */
  .mapboxgl-ctrl-top-right {
    top: calc(126px + env(safe-area-inset-top)) !important;
  }
}

/* ── Consist diagram (shared between popup + detail panel) ──────────────── */
.cars-strip {
  display: flex;
  gap: 2px;
  flex-wrap: wrap;
}
.car {
  position: relative;
  flex: 1 1 0;
  min-width: 28px;
  max-width: 44px;
  aspect-ratio: 0.78;
  border-radius: 3px;
  border: 1px solid rgba(255, 255, 255, 0.15);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  font-size: 9px;
  font-variant-numeric: tabular-nums;
  line-height: 1.1;
  color: #fff;
  text-shadow: 0 1px 1px rgba(0, 0, 0, 0.5);
  overflow: hidden;
  /* Cars render as <a> in the detail panel (linking to history pages).
     Suppress default underline and ensure they remain visually card-like. */
  text-decoration: none;
  cursor: pointer;
  transition: transform 0.08s, box-shadow 0.12s;
}
a.car:hover {
  transform: translateY(-1px);
  box-shadow: 0 2px 6px rgba(0, 0, 0, 0.5);
  border-color: rgba(255, 255, 255, 0.45);
}
.car.empty      { background: #1a7f37; }
.car.many-seats { background: #2da44e; }
.car.few-seats  { background: #bf8700; }
.car.standing   { background: #cf222e; }
.car.crushed    { background: #a40e26; }
.car.no-data    { background: #30363d; color: var(--muted); }
.car.locomotive { background: #1f2937; border: 1px solid #58a6ff; color: #fff; }
.car.locomotive.heritage {
  /* style attr provides the gradient and gold border */
  color: #fff;
  text-shadow: 0 1px 2px rgba(0,0,0,0.85), 0 0 4px rgba(0,0,0,0.7);
}
.car-heritage {
  position: absolute;
  top: 2px;
  right: 3px;
  font-size: 9px;
  color: #f5c842;
  text-shadow: 0 0 3px rgba(0, 0, 0, 0.9);
  line-height: 1;
}
.car .car-num  { font-weight: 700; font-size: 10px; }
.car .car-type { font-size: 7px; opacity: 0.85; letter-spacing: 0.3px; margin-top: 1px; }
.car .restroom { position: absolute; top: 1px; right: 2px; font-size: 8px; opacity: 0.9; }
.car .pax      { font-size: 7px; opacity: 0.85; margin-top: 1px; }

.consist-legend {
  display: flex;
  gap: 10px;
  margin-top: 8px;
  font-size: 10px;
  color: var(--muted);
  flex-wrap: wrap;
}
.consist-legend span {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}
.consist-legend .dot {
  width: 8px;
  height: 8px;
  border-radius: 2px;
  display: inline-block;
}

/* ── Detail panel ───────────────────────────────────────────────────────── */
.detail-panel {
  position: absolute;
  top: 16px;
  right: 16px;
  width: 360px;
  max-height: calc(100vh - 32px);
  background: rgba(22, 27, 34, 0.95);
  backdrop-filter: blur(8px);
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  padding: 0;
  overflow-y: auto;
  z-index: 2;
  display: none;
  flex-direction: column;
}
.detail-panel.open { display: flex; }
.dp-head {
  padding: 14px 16px;
  border-bottom: 1px solid var(--panel-border);
}
.dp-title-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}
.dp-train {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 16px;
  font-weight: 600;
}
.dp-branch {
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  padding: 2px 6px;
  border-radius: 3px;
  background: #21262d;
}
.dp-branch[data-branch="Hudson"]     { color: var(--hudson); }
.dp-branch[data-branch="Harlem"]     { color: var(--harlem); }
.dp-branch[data-branch="New Haven"]  { color: var(--new-haven); }
.dp-branch[data-branch="New Canaan"] { color: var(--new-canaan); }
.dp-branch[data-branch="Danbury"]    { color: var(--danbury); }
.dp-branch[data-branch="Waterbury"]  { color: var(--waterbury); }
.dp-branch[data-branch="Amtrak"]     { color: var(--amtrak); }
.dp-number { font-variant-numeric: tabular-nums; }
.dp-close {
  background: transparent;
  border: 0;
  color: var(--muted);
  font-size: 22px;
  line-height: 1;
  cursor: pointer;
  padding: 0 4px;
}
.dp-close:hover { color: var(--text); }
.dp-headsign { color: var(--muted); margin-top: 6px; font-size: 13px; }
.dp-meta {
  display: flex;
  gap: 14px;
  margin-top: 10px;
  font-size: 12px;
}
.dp-meta > span { display: flex; flex-direction: column; }
.dp-meta .l { color: var(--muted); font-size: 10px; text-transform: uppercase; letter-spacing: 0.3px; }
.dp-meta .v { font-variant-numeric: tabular-nums; }

/* Heritage-livery badge — shown only when locomotive is a wrapped P32 */
.dp-heritage {
  display: flex;
  gap: 10px;
  align-items: center;
  margin-top: 12px;
  padding: 8px 10px;
  border-radius: 6px;
  background: linear-gradient(135deg, rgba(245, 200, 66, 0.1) 0%, rgba(245, 200, 66, 0.03) 100%);
  border: 1px solid rgba(245, 200, 66, 0.4);
}
.dp-heritage-swatches {
  display: flex;
  flex-direction: column;
  gap: 2px;
  flex-shrink: 0;
}
.dp-heritage-swatches .swatch {
  display: block;
  width: 22px;
  height: 11px;
  border-radius: 2px;
  border: 1px solid rgba(255, 255, 255, 0.15);
}
.dp-heritage-text { flex: 1; min-width: 0; }
.dp-heritage-label {
  font-size: 9px;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: #f5c842;
}
.dp-heritage-scheme {
  font-size: 13px;
  font-weight: 600;
  color: var(--text);
  margin-top: 2px;
}
.dp-heritage-desc {
  font-size: 11px;
  color: var(--muted);
  margin-top: 1px;
  line-height: 1.3;
}

.dp-section {
  padding: 14px 16px;
  border-bottom: 1px solid var(--panel-border);
}
.dp-section:last-child { border-bottom: 0; }
.dp-section-title {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--muted);
  margin-bottom: 8px;
}
.dp-section-hint {
  font-size: 11px;
  color: #9ec2eb;
  margin: -4px 0 8px;
  display: flex;
  align-items: center;
  gap: 5px;
  opacity: 0.85;
}
.dp-section-hint::before {
  content: "💡";
  font-size: 11px;
}

.stops-list { display: flex; flex-direction: column; }
.stop-row {
  display: grid;
  grid-template-columns: 14px 1fr 36px 96px;
  align-items: center;
  gap: 8px;
  padding: 4px 0;
  font-size: 12px;
  border-left: 2px solid transparent;
  padding-left: 6px;
  position: relative;
}
.stop-row.current { border-left-color: var(--early); background: rgba(88, 166, 255, 0.08); }
.stop-row.departed { opacity: 0.55; }
.stop-row.berthed  { background: rgba(63, 185, 80, 0.07); }
.stop-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #484f58;
  justify-self: center;
}
.stop-row.departed .stop-dot { background: var(--on-time); }
.stop-row.berthed .stop-dot  { background: var(--early); box-shadow: 0 0 0 3px rgba(88,166,255,0.3); }
.stop-row.future .stop-dot   { background: transparent; border: 2px solid #484f58; }
.stop-name {
  display: flex;
  align-items: baseline;
  gap: 6px;
  min-width: 0;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}
.stop-code { color: var(--muted); font-size: 10px; font-variant-numeric: tabular-nums; }
.stop-track {
  text-align: center;
  font-variant-numeric: tabular-nums;
  font-weight: 600;
  font-size: 12px;
  padding: 2px 4px;
  border-radius: 3px;
  background: #21262d;
}
.stop-track.predicted { color: var(--muted); font-weight: 400; font-style: italic; }
.stop-track.changed   { color: var(--held); background: rgba(210, 153, 34, 0.2); }
.stop-time { text-align: right; font-variant-numeric: tabular-nums; line-height: 1.3; }
.stop-time .sched  { color: var(--text); }
.stop-time .actual { color: var(--muted); font-size: 11px; }
.stop-time .otp.ontime { color: var(--on-time); }
.stop-time .otp.late   { color: var(--late); }
.stop-time .otp.early  { color: var(--early); }

.alert {
  background: rgba(210, 153, 34, 0.08);
  border-left: 2px solid var(--held);
  padding: 8px 10px;
  margin-bottom: 6px;
  border-radius: 0 4px 4px 0;
  font-size: 12px;
}
.alert-status {
  font-size: 10px;
  text-transform: uppercase;
  letter-spacing: 0.5px;
  color: var(--held);
  margin-bottom: 4px;
}
.alert-text { line-height: 1.4; }
.alert-text p { margin: 0 0 6px; }
.alert-duration { font-size: 10px; color: var(--muted); margin-top: 4px; }

/* ── Track-change feed ──────────────────────────────────────────────────── */
.track-feed {
  position: absolute;
  /* Add bottom safe-area inset so the track-change alerts and toggle don't
     sit under the iPhone home indicator in standalone PWA mode. */
  bottom: calc(16px + env(safe-area-inset-bottom));
  right: calc(16px + env(safe-area-inset-right));
  width: 280px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  z-index: 1;
  pointer-events: none;
}
.track-feed-header {
  pointer-events: auto;
  background: rgba(22, 27, 34, 0.85);
  border: 1px solid var(--panel-border);
  border-radius: 4px;
  padding: 5px 9px;
  font-size: 11px;
  order: -1;
  align-self: flex-end;   /* shrink-to-fit instead of spanning full feed width */
  transition: background-color 0.12s, border-color 0.12s, opacity 0.12s;
}
.track-feed-header:hover {
  background: rgba(22, 27, 34, 1);
  border-color: #484f58;
}
.track-feed-header .toggle-row {
  margin: 0;
  cursor: pointer;
  color: var(--muted);
  display: flex;
  align-items: center;
  gap: 6px;
}
.track-feed-header .toggle-row input { margin: 0; cursor: pointer; }
.track-feed-header:has(input:checked) .toggle-row { color: var(--text); }
.track-feed-cards {
  display: flex;
  flex-direction: column-reverse;
  gap: 6px;
}
.tc-card {
  pointer-events: auto;
  background: rgba(22, 27, 34, 0.95);
  border: 1px solid var(--panel-border);
  border-left: 3px solid var(--held);
  border-radius: 4px;
  padding: 8px 10px;
  cursor: pointer;
  transition: opacity 0.6s ease, transform 0.6s ease;
  font-size: 12px;
}
.tc-card.tc-posted { border-left-color: var(--early); }
.tc-card:hover { background: rgba(22, 27, 34, 1); }
.tc-card.tc-fade { opacity: 0; transform: translateX(20px); }
.tc-when { color: var(--muted); font-size: 10px; }
.tc-head { margin: 2px 0 4px; }
.tc-train { font-weight: 600; }
.tc-stop  { color: var(--muted); }
.tc-body  { font-variant-numeric: tabular-nums; }

/* ── Hamburger toggle (visible only on mobile) ───────────────────────────── */
.panel-toggle {
  position: fixed;
  /* Account for iOS notch/Dynamic Island in standalone PWA mode with extra
     breathing room. */
  top: 18px;
  top: calc(18px + constant(safe-area-inset-top));
  top: calc(18px + env(safe-area-inset-top));
  left: 12px;
  left: calc(12px + constant(safe-area-inset-left));
  left: calc(12px + env(safe-area-inset-left));
  width: 40px;
  height: 40px;
  background: rgba(22, 27, 34, 0.95);
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  cursor: pointer;
  /* Higher than playback bar so a notched-iOS user can always reach the
     menu, even if the playback bar overlaps it horizontally. */
  z-index: 6;
  padding: 0;
}
.panel-toggle span {
  display: block;
  width: 18px;
  height: 2px;
  background: var(--text);
  border-radius: 1px;
}
.panel-backdrop {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.5);
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.2s;
  z-index: 3;
}
.panel-backdrop.open {
  opacity: 1;
  pointer-events: auto;
}

/* ── Playback bar ────────────────────────────────────────────────────────── */
.playback-bar {
  position: absolute;
  /* Clear the iOS notch / Dynamic Island in standalone PWA mode with extra
     breathing room so the bar isn't kissing the dynamic island. */
  top: 18px;
  top: calc(18px + constant(safe-area-inset-top));
  top: calc(18px + env(safe-area-inset-top));
  left: 50%;
  transform: translateX(-50%);
  display: flex;
  align-items: center;
  gap: 10px;
  background: rgba(22, 27, 34, 0.92);
  backdrop-filter: blur(8px);
  border: 1px solid var(--panel-border);
  border-radius: 24px;
  padding: 6px 14px 6px 6px;
  font-size: 12px;
  z-index: 3;
  min-width: 380px;
  max-width: calc(100vw - 720px);
}
@media (max-width: 1100px) {
  .playback-bar { max-width: 70vw; }
}
.playback-bar.paused { border-color: var(--held); }

.pb-live {
  display: flex;
  align-items: center;
  gap: 6px;
  background: transparent;
  border: 0;
  color: var(--muted);
  padding: 6px 10px;
  border-radius: 16px;
  font-family: inherit;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: 0.5px;
  cursor: pointer;
  transition: background 0.15s;
}
.pb-live:hover { background: rgba(255, 255, 255, 0.05); }
.pb-live-dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--muted);
  display: inline-block;
}
.playback-bar.live .pb-live {
  color: var(--on-time);
  cursor: default;
}
.playback-bar.live .pb-live-dot {
  background: var(--on-time);
  animation: pulse 2s infinite;
}
.playback-bar.paused .pb-live {
  color: var(--text);
  background: rgba(255, 255, 255, 0.08);
}
.playback-bar.paused .pb-live-dot {
  background: var(--held);
  animation: none;
}

.pb-steps {
  display: flex;
  gap: 2px;
}
.pb-steps button {
  background: transparent;
  border: 0;
  color: var(--muted);
  font-size: 12px;
  width: 22px;
  height: 22px;
  border-radius: 50%;
  cursor: pointer;
  font-family: inherit;
  padding: 0;
  line-height: 1;
}
.pb-steps button:hover {
  background: rgba(255, 255, 255, 0.1);
  color: var(--text);
}
.pb-steps button.pb-play {
  width: 26px;
  height: 26px;
  font-size: 13px;
  background: rgba(255, 255, 255, 0.08);
  color: var(--text);
  margin: 0 2px;
}
.pb-steps button.pb-play:hover {
  background: rgba(255, 255, 255, 0.15);
}
.playback-bar.playing .pb-play {
  background: var(--held);
  color: #161b22;
}
.pb-speed {
  background: #0d1117;
  color: var(--text);
  border: 1px solid var(--panel-border);
  border-radius: 4px;
  padding: 3px 4px;
  font-size: 11px;
  font-family: inherit;
  font-variant-numeric: tabular-nums;
  cursor: pointer;
  outline: none;
}
.pb-speed:hover { border-color: #484f58; }
.pb-speed:focus { border-color: #58a6ff; }

/* Date picker — calendar icon with a transparent <input type="datetime-local">
   overlaid on top so the user's tap goes directly to the input. This is
   essential for iOS Safari, which doesn't support showPicker() from a click
   handler — a real tap on the input itself is the only reliable trigger. */
.pb-datepicker-wrap {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 28px;
  height: 28px;
  border-radius: 50%;
  cursor: pointer;
  transition: background-color 0.12s;
}
.pb-datepicker-wrap:hover { background: rgba(255, 255, 255, 0.1); }
.pb-datepicker-icon {
  font-size: 14px;
  line-height: 1;
  color: var(--muted);
  pointer-events: none;
  /* Center vertically with the rest of the playback bar's icons */
  display: flex;
  align-items: center;
}
.pb-datepicker {
  /* Invisible but interactive: covers the entire icon wrap so any tap or
     click lands on the real datetime-local input, which the browser handles
     natively (including iOS Safari's wheel picker). */
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  opacity: 0;
  cursor: pointer;
  font-family: inherit;
  /* Some browsers add their own widgets that can poke out — clip them */
  overflow: hidden;
}
/* Hide the default chrome (clear button, spin buttons) for cleaner look
   in browsers that show them. */
.pb-datepicker::-webkit-calendar-picker-indicator { opacity: 0; }
.pb-datepicker::-webkit-inner-spin-button { display: none; }
.pb-datepicker::-webkit-clear-button { display: none; }

.pb-slider {
  flex: 1;
  -webkit-appearance: none;
  appearance: none;
  height: 4px;
  background: #30363d;
  border-radius: 2px;
  outline: none;
  cursor: pointer;
}
.pb-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 14px;
  height: 14px;
  background: #fff;
  border-radius: 50%;
  cursor: pointer;
  border: 2px solid #161b22;
  box-shadow: 0 0 0 1px var(--panel-border);
}
.pb-slider::-moz-range-thumb {
  width: 14px;
  height: 14px;
  background: #fff;
  border-radius: 50%;
  cursor: pointer;
  border: 2px solid #161b22;
}
.playback-bar.live .pb-slider::-webkit-slider-thumb { background: var(--on-time); }
.playback-bar.live .pb-slider::-moz-range-thumb     { background: var(--on-time); }
.playback-bar.paused .pb-slider::-webkit-slider-thumb { background: var(--held); }
.playback-bar.paused .pb-slider::-moz-range-thumb     { background: var(--held); }

.pb-time {
  font-variant-numeric: tabular-nums;
  min-width: 130px;
  text-align: right;
  font-size: 11px;
  color: var(--muted);
}
.pb-time .now { color: var(--on-time); font-weight: 600; }
.pb-time .ago { color: var(--held); font-weight: 600; }
.pb-time .error { color: var(--late); }

/* ════════════════════════════════════════════════════════════════════════ */
/* MOBILE LAYOUT — narrow screens up to 768px                                */
/* ════════════════════════════════════════════════════════════════════════ */
@media (max-width: 768px) {

  /* Hamburger button visible */
  .panel-toggle { display: flex; }

  /* Side panel becomes a drawer that slides in from the left */
  .panel {
    top: 0;
    left: 0;
    bottom: 0;
    width: min(85vw, 340px);
    max-height: 100vh;
    max-height: 100dvh;
    border-radius: 0;
    border: 0;
    border-right: 1px solid var(--panel-border);
    transform: translateX(-100%);
    transition: transform 0.25s ease;
    /* Leave room for the hamburger button area at the top, AND for the
       iOS notch/Dynamic Island in standalone PWA mode. */
    padding-top: calc(60px + env(safe-area-inset-top));
    /* Respect the home indicator zone at the bottom too. */
    padding-bottom: calc(16px + env(safe-area-inset-bottom));
    z-index: 4;
  }
  .panel.open {
    transform: translateX(0);
  }

  /* Detail panel becomes a bottom sheet */
  .detail-panel {
    top: auto;
    right: 0;
    left: 0;
    bottom: 0;
    width: 100%;
    max-width: 100%;
    max-height: 70vh;
    max-height: 70dvh;
    border-radius: 14px 14px 0 0;
    border-bottom: 0;
    border-left: 0;
    border-right: 0;
    /* Pad bottom for the home indicator zone so the panel's last content
       (e.g. the timeline's last stop) stays visible above it. */
    padding-bottom: env(safe-area-inset-bottom);
    z-index: 4;
    transform: translateY(100%);
    transition: transform 0.25s ease;
    display: flex;   /* always in flow, hide via transform */
  }
  .detail-panel.open {
    transform: translateY(0);
  }
  /* Drag-handle indicator */
  .detail-panel::before {
    content: "";
    position: absolute;
    top: 6px;
    left: 50%;
    transform: translateX(-50%);
    width: 36px;
    height: 4px;
    background: #484f58;
    border-radius: 2px;
    z-index: 1;
  }
  .dp-head { padding-top: 18px; }

  /* Playback bar — compact, full-width row near the top */
  .playback-bar {
    top: 12px;
    left: 60px;          /* clear the hamburger */
    right: 12px;
    transform: none;
    min-width: 0;
    max-width: none;
    width: auto;
    padding: 4px 10px 4px 4px;
    gap: 6px;
    border-radius: 20px;
  }
  /* Hide non-essential controls to fit */
  .pb-steps button[data-step="-10000"],
  .pb-steps button[data-step="10000"],
  .pb-speed {
    display: none;
  }
  .pb-live {
    padding: 5px 8px;
    font-size: 10px;
  }
  .pb-live-label { display: none; }   /* dot is enough */
  .pb-time {
    min-width: 0;
    font-size: 10px;
    flex-shrink: 0;
    text-align: right;
  }
  .pb-time .now,
  .pb-time .ago,
  .pb-time .error {
    display: inline;
  }

  /* Track-change feed: smaller, less obnoxious */
  .track-feed {
    bottom: 12px;
    right: 12px;
    left: 12px;
    width: auto;
    max-height: 30vh;
    overflow-y: auto;
  }
  .tc-card { font-size: 11px; padding: 6px 8px; }

  /* When the detail panel is open, push the track feed up out of the way */
  body.detail-open .track-feed { display: none; }

  /* Larger tap targets in the panel */
  .seg button {
    padding: 8px 8px;
    font-size: 12px;
  }
  .filter-row {
    padding: 6px 4px;
  }
  .filter-actions button {
    padding: 7px 6px;
    font-size: 12px;
  }
  .search-box input {
    font-size: 14px;     /* prevent iOS zoom on focus */
  }

  /* Mapbox built-in controls (zoom, compass) — Mapbox places them top-right
     by default; nudge them down so they clear the playback bar AND the
     floating search box that now sits at top-right. Account for safe-area
     so notched iPhones in standalone PWA mode push them further down. */
  .mapboxgl-ctrl-top-right {
    top: calc(120px + env(safe-area-inset-top)) !important;
  }
  /* And tuck Mapbox attribution below the track feed AND above the home
     indicator zone so it isn't covered or clipped. */
  .mapboxgl-ctrl-bottom-right {
    bottom: calc(36vh + env(safe-area-inset-bottom)) !important;
  }

  /* Train number labels: keep them readable but a hair smaller */
  /* (Set in JS via map.setLayoutProperty if needed — CSS can't touch
     Mapbox WebGL text. Leaving the desktop sizing for now.) */
}

/* Very narrow phones — extra compression */
@media (max-width: 380px) {
  .playback-bar {
    left: 56px;
    right: 8px;
    padding: 3px 8px 3px 3px;
  }
  .pb-time { font-size: 9px; }
  .panel-toggle {
    width: 38px;
    height: 38px;
    top: 10px;
    left: 10px;
  }
}

/* iOS PWA standalone-mode safety net.
 *
 * The iOS env(safe-area-inset-top) value is unreliable in portrait mode
 * for installed PWAs — multiple iOS versions return 0 even when the page
 * extends past the safe area. Rather than fight that quirk, we
 * additionally apply a hardcoded top offset whenever we detect standalone
 * mode (no Safari chrome). The value is calibrated for the Dynamic Island
 * height plus breathing room; on older iPhones (notched or not) the extra
 * space is harmless.
 *
 * Combined with env() above, the top offset is roughly:
 *   • Regular Safari (display-mode: browser): 18px (env evaluates to 0,
 *     browser chrome handles status bar)
 *   • iOS PWA (display-mode: standalone): 18px + 60px = 78px hardcoded
 *     fallback IF env() is broken; or 18px + actual env if env works
 */
@media all and (display-mode: standalone) and (max-width: 768px) {
  /* Dashboard (main map page) — absolutely positioned UI elements need their
     top values pushed below the Dynamic Island. */
  .playback-bar { top: 62px; }
  .panel-toggle { top: 62px; }
  .search-box   { top: 114px; }
  .mapboxgl-ctrl-top-right { top: 174px !important; }

  /* Subpage headers (schedule, board, engines, cars, consist-history) are
     static-positioned at the top of the document. Add top padding so the
     header content clears the Dynamic Island. !important is needed because
     each subpage's CSS sets `padding:` shorthand which would otherwise win
     by load order. */
  .schedule-header,
  .board-header,
  .engines-header {
    padding-top: 64px !important;
  }
}
