:root {
  --bg: #0f172a;
  --panel: #111827;
  --panel-soft: #172033;
  --panel-elevated: #1f2937;
  --text: #f8fafc;
  --text-soft: #cbd5e1;
  --muted: #94a3b8;
  --faint: #64748b;
  --border: rgba(255,255,255,0.08);
  --border-strong: rgba(255,255,255,0.16);
  --accent: #22d3ee;
  --accent-strong: #06b6d4;
  --accent-soft: rgba(34,211,238,0.14);
  --danger: #fb7185;
  --shadow: 0 18px 50px rgba(0,0,0,0.34);
  --sidebar-width: 340px;
  --radius-lg: 22px;
  --radius-md: 16px;
  --radius-sm: 12px;
}

* { box-sizing: border-box; }

html, body {
  height: 100%;
  margin: 0;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: var(--text);
  background: var(--bg);
}

body { overflow: hidden; }

button, input {
  font: inherit;
}

button {
  border: 0;
  cursor: pointer;
}

.app-shell {
  display: grid;
  grid-template-columns: var(--sidebar-width) 1fr;
  width: 100vw;
  height: 100vh;
  overflow: hidden;
  background: var(--bg);
}

.sidebar {
  position: relative;
  z-index: 900;
  height: 100vh;
  padding: 18px;
  overflow: auto;
  background:
    radial-gradient(circle at 0 0, rgba(34,211,238,0.12), transparent 28rem),
    linear-gradient(180deg, #111827 0%, #0b1120 100%);
  border-right: 1px solid var(--border);
  box-shadow: 14px 0 42px rgba(0,0,0,0.20);
}

.sidebar::-webkit-scrollbar,
.station-list::-webkit-scrollbar { width: 8px; }
.sidebar::-webkit-scrollbar-thumb,
.station-list::-webkit-scrollbar-thumb {
  background: rgba(148,163,184,0.28);
  border-radius: 999px;
}

.sidebar-header {
  padding: 4px 2px 12px;
}

.eyebrow {
  color: var(--accent);
  font-size: 11px;
  line-height: 1;
  font-weight: 900;
  letter-spacing: 0.2em;
  text-transform: uppercase;
}

.title-row {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 10px;
  margin-top: 7px;
}

h1 {
  margin: 0;
  font-size: 29px;
  line-height: 1.04;
  letter-spacing: -0.04em;
}

h2 {
  margin: 0;
  color: var(--text-soft);
  font-size: 12px;
  font-weight: 900;
  letter-spacing: 0.11em;
  text-transform: uppercase;
}

p {
  margin: 7px 0 0;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.45;
}

.status-badge {
  flex: 0 0 auto;
  margin-top: 2px;
  padding: 5px 8px;
  border: 1px solid rgba(34,211,238,0.22);
  border-radius: 999px;
  color: #a5f3fc;
  background: rgba(34,211,238,0.09);
  font-size: 10px;
  font-weight: 900;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.card {
  margin-top: 12px;
  padding: 13px;
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  background: rgba(15,23,42,0.72);
  box-shadow: 0 10px 26px rgba(0,0,0,0.14);
  backdrop-filter: blur(12px);
}

.compact-card { padding: 12px; }

.section-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-bottom: 10px;
}

.segmented-control {
  display: grid;
  gap: 6px;
  padding: 5px;
  border: 1px solid var(--border);
  border-radius: 15px;
  background: rgba(2,6,23,0.42);
}

.segmented-control.two { grid-template-columns: 1fr 1fr; }
.segmented-control.basemaps { grid-template-columns: repeat(4, 1fr); }

.segment {
  min-height: 34px;
  padding: 8px 9px;
  border-radius: 11px;
  color: var(--muted);
  background: transparent;
  font-size: 12px;
  font-weight: 850;
  transition: background .16s ease, color .16s ease, transform .16s ease;
}

.segment:hover { color: var(--text); background: rgba(255,255,255,0.055); }
.segment.active {
  color: #041217;
  background: linear-gradient(180deg, #67e8f9 0%, var(--accent) 100%);
  box-shadow: 0 10px 20px rgba(34,211,238,0.18);
}

.hint {
  margin-top: 9px;
  font-size: 12px;
  color: var(--muted);
}

.icon-button,
.link-button {
  color: var(--muted);
  background: transparent;
}

.icon-button {
  display: inline-grid;
  width: 24px;
  height: 24px;
  place-items: center;
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 12px;
  font-weight: 900;
}

.icon-button:hover,
.link-button:hover { color: var(--accent); }

.link-button {
  padding: 0;
  font-size: 12px;
  font-weight: 850;
}

.search-wrap {
  display: grid;
  grid-template-columns: 26px 1fr;
  align-items: center;
  height: 38px;
  margin-bottom: 10px;
  padding: 0 11px;
  border: 1px solid var(--border);
  border-radius: 14px;
  background: rgba(2,6,23,0.32);
}

.search-icon {
  color: var(--faint);
  font-weight: 900;
}

#stationSearch {
  width: 100%;
  min-width: 0;
  color: var(--text);
  border: 0;
  outline: 0;
  background: transparent;
  font-size: 13px;
}
#stationSearch::placeholder { color: var(--faint); }

.station-list {
  display: grid;
  gap: 7px;
  max-height: min(34vh, 312px);
  overflow: auto;
  padding-right: 3px;
}

.station-item {
  display: grid;
  grid-template-columns: 28px 1fr auto;
  gap: 9px;
  align-items: center;
  width: 100%;
  padding: 8px 9px;
  border: 1px solid var(--border);
  border-radius: 14px;
  color: var(--text-soft);
  text-align: left;
  background: rgba(255,255,255,0.035);
  transition: background .14s ease, border-color .14s ease, transform .14s ease;
}
.station-item:hover {
  transform: translateY(-1px);
  color: var(--text);
  background: rgba(255,255,255,0.07);
  border-color: var(--border-strong);
}
.station-item.active {
  color: var(--text);
  background: var(--accent-soft);
  border-color: rgba(34,211,238,0.55);
  box-shadow: inset 3px 0 0 var(--accent);
}

.station-dot {
  display: grid;
  width: 24px;
  height: 24px;
  place-items: center;
  border-radius: 999px;
  color: #071118;
  background: var(--accent);
  font-size: 11px;
  font-weight: 1000;
}
.station-name {
  overflow: hidden;
  white-space: nowrap;
  text-overflow: ellipsis;
  font-size: 13px;
  font-weight: 750;
}
.station-arrow { color: var(--faint); font-weight: 900; }

.selected-card {
  margin-top: 10px;
  padding: 10px 11px;
  border: 1px solid rgba(34,211,238,0.22);
  border-radius: 15px;
  background: rgba(34,211,238,0.08);
}
.selected-card strong { display: block; font-size: 13px; }
.selected-card span { display: block; margin-top: 3px; color: var(--muted); font-size: 12px; }
.is-hidden { display: none !important; }

.mini-actions {
  display: flex;
  gap: 10px;
  align-items: center;
}

.time-grid {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 7px;
}

.time-chip {
  --chip-color: var(--accent);
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  min-height: 34px;
  padding: 7px 8px;
  border: 1px solid var(--border);
  border-radius: 999px;
  color: var(--muted);
  background: rgba(255,255,255,0.035);
  font-size: 12px;
  font-weight: 900;
}
.time-chip:hover { border-color: rgba(255,255,255,0.22); color: var(--text); }
.time-chip.active {
  color: var(--text);
  border-color: color-mix(in srgb, var(--chip-color), white 18%);
  background: color-mix(in srgb, var(--chip-color), transparent 78%);
}
.time-chip::before {
  content: "";
  width: 9px;
  height: 9px;
  border-radius: 999px;
  background: var(--chip-color);
  box-shadow: 0 0 0 2px rgba(255,255,255,0.10);
}

summary {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  list-style: none;
  cursor: pointer;
  color: var(--text-soft);
  font-size: 12px;
  font-weight: 900;
  letter-spacing: 0.11em;
  text-transform: uppercase;
}
summary::-webkit-details-marker { display: none; }
summary::after {
  content: "+";
  color: var(--muted);
  font-size: 16px;
  letter-spacing: 0;
}
details[open] summary::after { content: "−"; }

.legend {
  display: grid;
  gap: 7px;
  margin-top: 10px;
}
.legend-item {
  display: grid;
  grid-template-columns: 34px 1fr;
  gap: 9px;
  align-items: center;
  color: var(--text-soft);
  font-size: 12px;
}
.legend-swatch {
  height: 12px;
  border-radius: 999px;
  border: 1px solid rgba(255,255,255,0.32);
}
.credit {
  margin-top: 8px;
  color: var(--faint);
  font-size: 11px;
}

.map-area {
  position: relative;
  min-width: 0;
  height: 100vh;
  background: #0b1120;
}
#map {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

.map-toolbar {
  position: absolute;
  top: 16px;
  right: 16px;
  z-index: 700;
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
}
.toolbar-button {
  min-height: 36px;
  padding: 8px 12px;
  border: 1px solid rgba(15,23,42,0.12);
  border-radius: 999px;
  color: #0f172a;
  background: rgba(255,255,255,0.88);
  box-shadow: 0 12px 26px rgba(15,23,42,0.12);
  backdrop-filter: blur(10px);
  font-size: 12px;
  font-weight: 900;
}
.toolbar-button:hover { background: #fff; }
.lang-button { min-width: 44px; }
.mobile-only { display: none; }

.floating-card {
  position: absolute;
  z-index: 650;
  border: 1px solid rgba(255,255,255,0.14);
  border-radius: var(--radius-lg);
  color: var(--text);
  background: rgba(15,23,42,0.86);
  box-shadow: var(--shadow);
  backdrop-filter: blur(16px);
}
.info-panel {
  right: 16px;
  top: 62px;
  width: min(330px, calc(100vw - var(--sidebar-width) - 48px));
  padding: 15px 16px;
}
.info-kicker {
  color: var(--accent);
  font-size: 10px;
  font-weight: 1000;
  letter-spacing: 0.16em;
  text-transform: uppercase;
}
.info-panel strong {
  display: block;
  margin-top: 4px;
  color: var(--text);
  font-size: 17px;
  line-height: 1.2;
}
.info-panel p { margin-top: 5px; font-size: 12px; }

.loading-overlay,
.error-overlay {
  position: absolute;
  inset: 0;
  z-index: 1200;
  display: grid;
  place-items: center;
  background: rgba(15,23,42,0.64);
  backdrop-filter: blur(8px);
}
.loader-card,
.error-card {
  width: min(360px, calc(100vw - 40px));
  padding: 22px;
  border: 1px solid var(--border-strong);
  border-radius: 24px;
  text-align: center;
  background: rgba(15,23,42,0.94);
  box-shadow: var(--shadow);
}
.loader-card strong,
.error-card strong { display: block; margin-top: 10px; }
.loader-card span { display: block; margin-top: 5px; color: var(--muted); font-size: 13px; }
.spinner {
  width: 34px;
  height: 34px;
  margin: 0 auto;
  border: 3px solid rgba(255,255,255,0.16);
  border-top-color: var(--accent);
  border-radius: 999px;
  animation: spin .9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }

/* Critical Leaflet layout fallback; prevents random tile positioning if CDN CSS loads late/fails. */
.leaflet-container { position: relative; overflow: hidden; touch-action: none; background: #dfe6ea; outline: 0; }
.leaflet-pane,
.leaflet-tile,
.leaflet-marker-icon,
.leaflet-marker-shadow,
.leaflet-tile-container,
.leaflet-pane > svg,
.leaflet-pane > canvas,
.leaflet-zoom-box,
.leaflet-image-layer,
.leaflet-layer { position: absolute; left: 0; top: 0; }
.leaflet-tile { user-select: none; -webkit-user-drag: none; border: 0; }
.leaflet-container img { max-width: none !important; max-height: none !important; }
.leaflet-tile-container img { width: 256px; height: 256px; }
.leaflet-control { position: relative; z-index: 800; pointer-events: auto; }
.leaflet-top, .leaflet-bottom { position: absolute; z-index: 1000; pointer-events: none; }
.leaflet-top { top: 0; } .leaflet-right { right: 0; } .leaflet-bottom { bottom: 0; } .leaflet-left { left: 0; }
.leaflet-control-zoom {
  border: 1px solid rgba(15,23,42,0.16);
  border-radius: 14px;
  overflow: hidden;
  margin: 16px;
  box-shadow: 0 12px 26px rgba(15,23,42,0.16);
}
.leaflet-control-zoom a {
  display: block;
  width: 34px;
  height: 34px;
  line-height: 34px;
  text-align: center;
  text-decoration: none;
  background: rgba(255,255,255,0.92);
  color: #0f172a;
  font-weight: 1000;
}
.leaflet-control-zoom a:hover { background: white; }
.leaflet-control-attribution {
  margin: 0;
  padding: 4px 7px;
  background: rgba(255,255,255,0.78);
  color: #111827;
  font-size: 11px;
}
.leaflet-popup-content-wrapper,
.leaflet-popup-tip { background: #111827; color: var(--text); }
.leaflet-popup-content { margin: 12px 14px; font-size: 13px; }

.station-marker {
  position: relative;
  display: grid;
  width: 28px;
  height: 28px;
  place-items: center;
  border: 2px solid #ffffff;
  border-radius: 999px;
  color: #061018;
  background: var(--accent);
  font-size: 10px;
  font-weight: 1000;
  box-shadow: 0 8px 20px rgba(15,23,42,0.36);
}
.station-marker::after {
  content: "";
  position: absolute;
  inset: -5px;
  border: 1px solid rgba(34,211,238,0.34);
  border-radius: inherit;
  opacity: 0;
  transform: scale(.75);
  transition: .16s ease;
}
.station-marker.selected {
  width: 34px;
  height: 34px;
  color: #fff;
  border-color: #071118;
  background: #ef4444;
  box-shadow: 0 0 0 4px rgba(239,68,68,0.20), 0 12px 28px rgba(15,23,42,0.45);
}
.station-marker.selected::after { opacity: 1; transform: scale(1); border-color: rgba(239,68,68,0.45); }

@media (max-width: 900px) {
  .app-shell { grid-template-columns: 1fr; }
  .sidebar {
    position: fixed;
    left: 12px;
    right: 12px;
    bottom: 12px;
    width: auto;
    height: min(72vh, 640px);
    padding: 14px;
    border: 1px solid var(--border);
    border-radius: 28px;
    transform: translateY(calc(100% + 20px));
    transition: transform .22s ease;
  }
  body.panel-open .sidebar { transform: translateY(0); }
  .map-area { height: 100vh; }
  .mobile-only { display: inline-flex; align-items: center; }
  .map-toolbar { left: 12px; right: 12px; justify-content: space-between; }
  .info-panel {
    left: 12px;
    right: 12px;
    top: 62px;
    width: auto;
  }
  .station-list { max-height: 220px; }
}

@media (max-width: 520px) {
  .time-grid { grid-template-columns: repeat(2, 1fr); }
  .segmented-control.basemaps { grid-template-columns: repeat(2, 1fr); }
  .toolbar-button { padding: 7px 10px; }
  .status-badge { display: none; }
}

/* v5 refinements */
body.light-ui {
  --bg: #eef3f8;
  --panel: #ffffff;
  --panel-soft: #f8fafc;
  --panel-elevated: #eef2f7;
  --text: #0f172a;
  --text-soft: #1e293b;
  --muted: #64748b;
  --faint: #94a3b8;
  --border: rgba(15,23,42,0.10);
  --border-strong: rgba(15,23,42,0.18);
  --accent: #0891b2;
  --accent-strong: #0e7490;
  --accent-soft: rgba(8,145,178,0.12);
  --shadow: 0 18px 50px rgba(15,23,42,0.16);
}

body.light-ui .sidebar {
  background:
    radial-gradient(circle at 0 0, rgba(8,145,178,0.12), transparent 28rem),
    linear-gradient(180deg, #ffffff 0%, #eef3f8 100%);
  box-shadow: 14px 0 42px rgba(15,23,42,0.10);
}
body.light-ui .card { background: rgba(255,255,255,0.78); }
body.light-ui .segmented-control { background: rgba(15,23,42,0.035); }
body.light-ui .segment:hover { background: rgba(15,23,42,0.055); }
body.light-ui .station-item { background: rgba(15,23,42,0.035); }
body.light-ui .station-item:hover { background: rgba(15,23,42,0.07); }
body.light-ui .floating-card,
body.light-ui .loader-card,
body.light-ui .error-card,
body.light-ui .modal-card {
  color: var(--text);
  background: rgba(255,255,255,0.92);
  border-color: var(--border);
}
body.light-ui .leaflet-popup-content-wrapper,
body.light-ui .leaflet-popup-tip { background: #ffffff; color: var(--text); }

.station-panel summary { min-height: 30px; }
.station-panel-body { margin-top: 12px; }
.tight-row { align-items: flex-start; margin-bottom: 8px; }
.tight-row .hint { margin: 0; }
.summary-pill {
  display: inline-grid;
  min-width: 28px;
  height: 22px;
  place-items: center;
  padding: 0 8px;
  border: 1px solid var(--border-strong);
  border-radius: 999px;
  color: var(--accent);
  background: var(--accent-soft);
  font-size: 11px;
  letter-spacing: 0;
}
.station-panel details:not([open]) { margin-bottom: -2px; }

.theme-button,
.info-button { min-width: 38px; padding-inline: 0; }
.info-button {
  font-family: Georgia, serif;
  font-size: 16px;
  font-weight: 900;
  font-style: italic;
}

.modal-overlay {
  position: absolute;
  inset: 0;
  z-index: 1500;
  display: grid;
  place-items: center;
  padding: 20px;
  background: rgba(2,6,23,0.48);
  backdrop-filter: blur(8px);
}
.modal-card {
  width: min(560px, calc(100vw - 40px));
  max-height: min(760px, calc(100vh - 40px));
  overflow: auto;
  padding: 20px;
  border: 1px solid var(--border-strong);
  border-radius: 26px;
  color: var(--text);
  background: rgba(15,23,42,0.94);
  box-shadow: var(--shadow);
}
.modal-header {
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 18px;
  padding-bottom: 14px;
  border-bottom: 1px solid var(--border);
}
.modal-title {
  margin-top: 5px;
  color: var(--text);
  font-size: 24px;
  line-height: 1.1;
  letter-spacing: -0.04em;
  text-transform: none;
}
.modal-close {
  display: grid;
  width: 36px;
  height: 36px;
  place-items: center;
  border: 1px solid var(--border);
  border-radius: 999px;
  color: var(--text);
  background: rgba(255,255,255,0.05);
  font-size: 24px;
  line-height: 1;
}
.modal-close:hover { background: rgba(255,255,255,0.10); }
.modal-section {
  padding: 15px 0 0;
}
.modal-section h3 {
  margin: 0;
  color: var(--text-soft);
  font-size: 12px;
  font-weight: 1000;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}
.modal-section p {
  margin-top: 7px;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.55;
}
.leaflet-control-attribution { display: none !important; }

@media (max-width: 900px) {
  .modal-overlay { align-items: end; }
  .modal-card { width: 100%; border-radius: 24px; }
}
