/*
 * Mobile-first styling for the transit map.
 *
 * The map fills the viewport and every control floats above it. Layout uses
 * safe-area insets so nothing hides behind a notch or a home indicator, and
 * touch targets are kept at 44 px or larger.
 */

:root {
  --bg: #0b0f14;
  --surface: rgba(19, 26, 35, 0.94);
  --surface-solid: #131a23;
  --border: #253243;
  --text: #e8eef5;
  --text-dim: #93a6ba;
  --text-faint: #6b7f94;
  --accent: #2c8fe0;
  --live: #37d67a;
  --scheduled: #f0a742;
  --danger: #e5484d;

  --radius: 14px;
  --shadow: 0 6px 24px rgba(0, 0, 0, 0.45);

  --safe-top: env(safe-area-inset-top, 0px);
  --safe-bottom: env(safe-area-inset-bottom, 0px);
  --safe-left: env(safe-area-inset-left, 0px);
  --safe-right: env(safe-area-inset-right, 0px);
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  overscroll-behavior: none;
}

/* Prevent the page itself scrolling; the map handles all gestures. */
body {
  position: fixed;
  inset: 0;
}

.hidden {
  display: none !important;
}

.map {
  position: absolute;
  inset: 0;
  background: var(--bg);
  z-index: 0;
}

/* The train overlay sits above the map but must not swallow gestures. */
.train-canvas {
  position: absolute;
  top: 0;
  left: 0;
  pointer-events: none;
  z-index: 450;
}

/* ---------------------------------------------------------------------------
   Top bar
   --------------------------------------------------------------------------- */

.topbar {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  z-index: 1000;
  padding: calc(var(--safe-top) + 8px) calc(var(--safe-right) + 10px) 8px
    calc(var(--safe-left) + 10px);
  background: linear-gradient(180deg, rgba(11, 15, 20, 0.94) 60%, rgba(11, 15, 20, 0));
  pointer-events: none;
}

.topbar-main {
  display: flex;
  align-items: center;
  gap: 8px;
}

.counts {
  display: flex;
  gap: 8px;
  flex: 1;
  min-width: 0;
}

.count {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  border-radius: 999px;
  background: var(--surface);
  border: 1px solid var(--border);
  font-size: 13px;
  color: var(--text-dim);
  white-space: nowrap;
  pointer-events: auto;
}

.count b {
  color: var(--text);
  font-weight: 600;
}

.dot {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  flex: none;
}

.dot-live {
  background: var(--live);
  /* A soft halo distinguishes observed data at a glance. */
  box-shadow: 0 0 0 3px rgba(55, 214, 122, 0.2);
}

.dot-scheduled {
  background: var(--scheduled);
  box-shadow: 0 0 0 3px rgba(240, 167, 66, 0.18);
}

.feed-state {
  margin-top: 6px;
  font-size: 11px;
  color: var(--text-faint);
  pointer-events: none;
}

.feed-state.warn {
  color: var(--scheduled);
}

.feed-state.error {
  color: var(--danger);
}

/* ---------------------------------------------------------------------------
   Floating action buttons
   --------------------------------------------------------------------------- */

.fabs {
  position: absolute;
  right: calc(var(--safe-right) + 12px);
  bottom: calc(var(--safe-bottom) + 84px);
  z-index: 1000;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

.fab,
.icon-button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--border);
  background: var(--surface);
  color: var(--text);
  border-radius: 50%;
  box-shadow: var(--shadow);
  cursor: pointer;
  pointer-events: auto;
  padding: 0;
}

.fab {
  width: 48px;
  height: 48px;
}

.icon-button {
  width: 32px;
  height: 32px;
  font-size: 15px;
  font-weight: 600;
  border-radius: 50%;
}

.fab svg {
  width: 22px;
  height: 22px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.9;
  stroke-linecap: round;
}

.fab-primary.active {
  background: var(--accent);
  border-color: var(--accent);
  color: #fff;
}

.fab.locating svg {
  animation: pulse 1.1s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.35; }
}

/* ---------------------------------------------------------------------------
   Bottom sheets
   --------------------------------------------------------------------------- */

.sheet {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 1100;
  background: var(--surface);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  border-top: 1px solid var(--border);
  border-radius: var(--radius) var(--radius) 0 0;
  box-shadow: var(--shadow);
  padding-bottom: var(--safe-bottom);
  transition: transform 0.24s ease;
}

/* Line chips ---------------------------------------------------------------*/

.chip {
  display: inline-block;
  min-width: 26px;
  padding: 2px 6px;
  border-radius: 5px;
  font-size: 11px;
  font-weight: 700;
  text-align: center;
  line-height: 1.4;
}

/* ---------------------------------------------------------------------------
   Train detail
   --------------------------------------------------------------------------- */

.sheet-detail {
  padding: 14px 16px 16px;
}

.sheet-close {
  position: absolute;
  top: 8px;
  right: 10px;
  width: 32px;
  height: 32px;
  border: none;
  background: none;
  color: var(--text-dim);
  font-size: 24px;
  line-height: 1;
  cursor: pointer;
}

.train-detail h2 {
  margin: 0 0 2px;
  font-size: 17px;
  display: flex;
  align-items: center;
  gap: 8px;
}

.train-detail .destination {
  margin: 0 0 10px;
  color: var(--text-dim);
  font-size: 14px;
}

.badge {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  padding: 3px 8px;
  border-radius: 999px;
  font-size: 11px;
  font-weight: 600;
  text-transform: uppercase;
  letter-spacing: 0.4px;
}

.badge-live {
  background: rgba(55, 214, 122, 0.14);
  color: var(--live);
  border: 1px solid rgba(55, 214, 122, 0.35);
}

.badge-scheduled {
  background: rgba(240, 167, 66, 0.13);
  color: var(--scheduled);
  border: 1px solid rgba(240, 167, 66, 0.32);
}

.facts {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 4px 12px;
  margin: 10px 0 0;
  font-size: 13px;
}

.facts dt {
  color: var(--text-faint);
}

.facts dd {
  margin: 0;
  color: var(--text);
}

/* Countdown -----------------------------------------------------------------
   The single figure most people open a train for, so it gets its own line and
   tabular digits — proportional ones make a ticking clock jitter sideways. */

.countdown {
  margin: 8px 0 0;
  font-size: 14px;
  color: var(--text-dim);
}

.countdown strong {
  font-size: 22px;
  font-weight: 700;
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

.countdown-estimated strong {
  color: var(--text-dim);
}

.countdown-idle {
  font-size: 13px;
  color: var(--text-faint);
}

/* Station arrivals ----------------------------------------------------------*/

.station-detail h2 {
  margin: 0 0 6px;
  font-size: 17px;
}

.station-lines {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  margin-bottom: 10px;
}

.arrivals {
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: 42vh;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
}

.arrival-row {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 9px 6px;
  background: none;
  border: none;
  border-radius: 8px;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
}

.arrival-row:active {
  background: rgba(255, 255, 255, 0.06);
}

.arrival-destination {
  flex: 1;
  min-width: 0;
  font-size: 14px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.arrival-time {
  flex: none;
  font-size: 15px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}

/* Dimmed rather than hidden: an inferred countdown is still worth having, it
   just should not read as authoritative next to a published one. */
.arrival-estimated {
  font-weight: 600;
  color: var(--text-faint);
}

/* Crowding ------------------------------------------------------------------
   Drawn as the train itself: one box per carriage, in order, each filling from
   the floor up. The numbers are usually lopsided — the leading carriage often
   runs several times fuller than the trailing one — and that imbalance is the
   entire reason to show this per carriage rather than as a single figure. */

.occupancy {
  margin: 12px 0 0;
  padding-top: 10px;
  border-top: 1px solid var(--border);
}

.occupancy-head {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  font-size: 12px;
  font-weight: 600;
  color: var(--text-dim);
}

.occupancy-overall {
  font-weight: 500;
  color: var(--text-faint);
  font-variant-numeric: tabular-nums;
}

.carriages {
  display: flex;
  gap: 4px;
  margin-top: 8px;
}

.carriage-cell {
  display: flex;
  flex: 1 1 0;
  min-width: 0;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

.carriage {
  position: relative;
  display: flex;
  width: 100%;
  height: 42px;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  border: 1.5px solid var(--border);
  border-radius: 4px;
  background: rgba(255, 255, 255, 0.05);
}

/* Rounding only the outer ends makes the row read as one train rather than as
   a bar chart that happens to be adjacent. */
.carriage-cell:first-child .carriage {
  border-top-left-radius: 13px;
  border-bottom-left-radius: 13px;
}

.carriage-cell:last-child .carriage {
  border-top-right-radius: 13px;
  border-bottom-right-radius: 13px;
}

.carriage-fill {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  transition: height 0.3s ease;
}

.car-quiet .carriage-fill { background: rgba(58, 180, 110, 0.55); }
.car-busy .carriage-fill  { background: rgba(230, 170, 50, 0.55); }
.car-full .carriage-fill  { background: rgba(226, 86, 74, 0.6); }

.carriage-value {
  position: relative;
  font-size: 11px;
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.65);
}

.carriage-label {
  font-size: 10px;
  color: var(--text-faint);
}

.occupancy-note {
  margin: 8px 0 0;
  font-size: 11px;
  color: var(--text-faint);
}

@media (prefers-reduced-motion: reduce) {
  .carriage-fill { transition: none; }
}

.provenance {
  margin: 12px 0 0;
  padding-top: 10px;
  border-top: 1px solid var(--border);
  font-size: 12px;
  color: var(--text-faint);
}

/* ---------------------------------------------------------------------------
   Panels (line filter, about)
   --------------------------------------------------------------------------- */

.panel {
  position: absolute;
  z-index: 1200;
  left: calc(var(--safe-left) + 10px);
  right: calc(var(--safe-right) + 10px);
  bottom: calc(var(--safe-bottom) + 10px);
  max-height: 70vh;
  display: flex;
  flex-direction: column;
  background: var(--surface-solid);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  overflow: hidden;
}

.panel-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 12px 12px 10px 14px;
  border-bottom: 1px solid var(--border);
  font-size: 14px;
  font-weight: 600;
}

.panel-actions {
  display: flex;
  align-items: center;
  gap: 6px;
}

.link-button {
  background: none;
  border: none;
  color: var(--accent);
  font: inherit;
  font-size: 13px;
  font-weight: 500;
  cursor: pointer;
  padding: 6px 4px;
}

.panel-body {
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  padding: 8px;
}

.panel-prose {
  padding: 14px;
  font-size: 13px;
  color: var(--text-dim);
}

.panel-prose h3 {
  margin: 14px 0 4px;
  font-size: 13px;
  color: var(--text);
}

.panel-prose h3:first-child {
  margin-top: 0;
}

.panel-prose p {
  margin: 0 0 8px;
}

.panel-prose a {
  color: var(--accent);
}

/* Line toggles -------------------------------------------------------------*/

.operator-group {
  margin-bottom: 10px;
}

.operator-label {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 4px 6px 8px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.6px;
  color: var(--text-faint);
}

.line-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(78px, 1fr));
  gap: 6px;
}

.line-toggle {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 8px 8px;
  border: 1px solid var(--border);
  border-radius: 10px;
  background: rgba(255, 255, 255, 0.03);
  color: var(--text-dim);
  font: inherit;
  font-size: 12px;
  cursor: pointer;
}

.line-toggle.on {
  border-color: currentColor;
  color: var(--text);
  background: rgba(255, 255, 255, 0.08);
}

.line-toggle .swatch {
  width: 12px;
  height: 12px;
  border-radius: 3px;
  flex: none;
}

.line-toggle.off .swatch {
  opacity: 0.3;
}

/* ---------------------------------------------------------------------------
   Toast
   --------------------------------------------------------------------------- */

.toast {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(var(--safe-bottom) + 140px);
  z-index: 1500;
  max-width: min(92vw, 420px);
  padding: 11px 16px;
  border-radius: 10px;
  background: var(--surface-solid);
  border: 1px solid var(--border);
  box-shadow: var(--shadow);
  font-size: 13px;
  color: var(--text-dim);
  text-align: center;
}

.toast.error {
  border-color: rgba(229, 72, 77, 0.5);
  color: #ffd4d6;
}

/* ---------------------------------------------------------------------------
   Leaflet overrides
   --------------------------------------------------------------------------- */

.leaflet-container {
  background: var(--bg);
  font: inherit;
}

.leaflet-control-attribution {
  background: rgba(11, 15, 20, 0.75) !important;
  color: var(--text-faint) !important;
  font-size: 10px !important;
  padding: 2px 6px !important;
}

.leaflet-control-attribution a {
  color: var(--text-dim) !important;
}

/* Zoom buttons are redundant on touch and eat screen space. */
.leaflet-control-zoom {
  display: none;
}

.leaflet-bottom.leaflet-right {
  margin-bottom: calc(var(--safe-bottom) + 44px);
}

/* User location marker -----------------------------------------------------*/

.user-dot {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--accent);
  border: 2.5px solid #fff;
  box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.35), 0 2px 6px rgba(0, 0, 0, 0.5);
}

/* Wider screens ------------------------------------------------------------*/

@media (min-width: 700px) {
  .sheet {
    left: auto;
    right: 12px;
    bottom: 12px;
    width: 340px;
    border-radius: var(--radius);
    border: 1px solid var(--border);
  }

  .panel {
    left: auto;
    width: 380px;
  }

  .fabs {
    bottom: calc(var(--safe-bottom) + 16px);
    right: 372px;
  }
}
