:root {
  --bg: #f4f5f7;
  --card: #ffffff;
  --text: #1c1d21;
  --muted: #6b7280;
  --border: #e2e4e9;
  --accent: #d0332e; /* Caltrain red */
  --ontime-bg: #e7f6ec;
  --ontime-fg: #1a7f43;
  --late-bg: #fdeceb;
  --late-fg: #c0322c;
  --early-bg: #eef2fb;
  --early-fg: #2f5bd0;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.08);
  /* Service colors (match the printed timetable) */
  --svc-express: #d0332e; /* red */
  --svc-limited: #2f6bd6; /* blue */
  --svc-local: #9aa0ac; /* gray */
  --alert-info-bg: #eef2fb;
  --alert-info-fg: #274690;
  --alert-severe-bg: #fdeceb;
  --alert-severe-fg: #a8231d;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #16171b;
    --card: #212329;
    --text: #f2f3f5;
    --muted: #9aa0ac;
    --border: #32343c;
    --accent: #ff5b52;
    --ontime-bg: #17301f;
    --ontime-fg: #62d68e;
    --late-bg: #351b1a;
    --late-fg: #ff7d75;
    --early-bg: #1b2540;
    --early-fg: #7ba0f5;
    --shadow: 0 1px 3px rgba(0, 0, 0, 0.4);
    --svc-express: #ff5b52;
    --svc-limited: #6ea0ff;
    --svc-local: #9aa0ac;
    --alert-info-bg: #1b2540;
    --alert-info-fg: #b9cbf7;
    --alert-severe-bg: #351b1a;
    --alert-severe-fg: #ff9d96;
  }
}

* {
  box-sizing: border-box;
}
[hidden] {
  display: none !important;
}

body {
  margin: 0;
  background: var(--bg);
  color: var(--text);
  font: 15px/1.5 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica,
    Arial, sans-serif;
}

.app {
  max-width: 640px;
  margin: 0 auto;
  padding: 24px 16px 48px;
}

.app__header {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 20px;
}

/* Compact station/destination summary — mobile only, shown when collapsed. */
.config-bar {
  display: none;
  width: 100%;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-bottom: 16px;
  padding: 10px 14px;
  font-size: 0.95rem;
  font-family: inherit;
  color: var(--text);
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  box-shadow: var(--shadow);
  cursor: pointer;
  text-align: left;
}
.config-bar__text {
  font-weight: 600;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}
.config-bar__gear {
  flex: none;
  color: var(--muted);
  font-size: 1.1rem;
}
/* Close button to hide the selectors (mobile only, once a station is chosen). */
.config-close {
  display: none;
  position: absolute;
  top: -2px;
  right: 0;
  width: 30px;
  height: 30px;
  font-size: 1.4rem;
  line-height: 1;
  color: var(--muted);
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
}
@media (max-width: 560px) {
  .app.is-collapsed .app__header {
    display: none;
  }
  .app.is-collapsed .config-bar {
    display: flex;
  }
  .app.has-station .app__header {
    position: relative;
    padding-right: 40px;
  }
  .app.has-station .config-close {
    display: flex;
    align-items: center;
    justify-content: center;
  }
}

.station-picker {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 220px;
  flex: 1;
}
.station-picker__label {
  font-size: 0.78rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--muted);
}
.station-picker select {
  appearance: none;
  width: 100%;
  padding: 9px 12px;
  font-size: 1rem;
  color: var(--text);
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 8px;
  box-shadow: var(--shadow);
}

.banner {
  margin-bottom: 16px;
  padding: 10px 14px;
  border-radius: 8px;
  background: var(--late-bg);
  color: var(--late-fg);
  font-size: 0.9rem;
}

/* Weather */
.weather:empty {
  display: none;
}
.weather {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 0.95rem;
  color: var(--text);
  white-space: nowrap;
}
.weather__icon {
  font-size: 1.3rem;
  line-height: 1;
}
.weather__temp {
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.weather__meta {
  color: var(--muted);
  font-size: 0.85rem;
}

/* Service alerts */
.alerts-wrap {
  margin-bottom: 18px;
}
.alerts-wrap__title {
  margin: 0 0 8px;
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
}
.alerts {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.alert {
  padding: 12px 14px;
  border-radius: 8px;
  border-left: 4px solid transparent;
}
.alert--none {
  background: var(--card);
  border: 1px solid var(--border);
  color: var(--muted);
  font-size: 0.9rem;
}
.alert--info {
  background: var(--alert-info-bg);
  color: var(--alert-info-fg);
  border-left-color: var(--svc-limited);
}
.alert--severe {
  background: var(--alert-severe-bg);
  color: var(--alert-severe-fg);
  border-left-color: var(--svc-express);
}
.alert__title {
  font-weight: 700;
}
.alert__title a {
  color: inherit;
  font-weight: 600;
  font-size: 0.85rem;
}
.alert__body {
  margin-top: 4px;
  font-size: 0.9rem;
  white-space: pre-line;
}

/* Two-direction boards */
.board-wrap {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 18px;
  align-items: start;
}
.board-wrap--single {
  grid-template-columns: 1fr;
}
@media (max-width: 560px) {
  .board-wrap {
    grid-template-columns: 1fr;
  }
}
.board-col__title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  width: 100%;
  margin: 0 0 10px;
  padding: 0;
  font-family: inherit;
  font-size: 0.95rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
  background: none;
  border: none;
  cursor: pointer;
}
.board-col__caret {
  transition: transform 0.15s ease;
}
.board-col--collapsed .board-col__caret {
  transform: rotate(-90deg);
}
.board-col--collapsed .board {
  display: none;
}

.board {
  display: flex;
  flex-direction: column;
  gap: 10px;
}
.board__empty {
  color: var(--muted);
  text-align: center;
  padding: 32px 0;
}

.train {
  display: grid;
  grid-template-columns: auto 1fr auto;
  align-items: center;
  gap: 4px 14px;
  padding: 14px 16px;
  background: var(--card);
  border: 1px solid var(--border);
  border-left: 4px solid var(--svc-local);
  border-radius: 10px;
  box-shadow: var(--shadow);
}
.train--express {
  border-left-color: var(--svc-express);
}
.train--limited {
  border-left-color: var(--svc-limited);
}
.train--local {
  border-left-color: var(--svc-local);
}
.train--express .train__num { color: var(--svc-express); }
.train--limited .train__num { color: var(--svc-limited); }
.train {
  cursor: pointer;
}
.train:focus-visible {
  outline: 2px solid var(--svc-limited);
  outline-offset: 2px;
}

/* Destination arrival line (spans the full card width) */
.train__destarr {
  grid-column: 1 / -1;
  margin-top: 2px;
  padding-top: 8px;
  border-top: 1px dashed var(--border);
  font-size: 0.85rem;
  color: var(--muted);
}
.train__destarr strong {
  color: var(--text);
  font-variant-numeric: tabular-nums;
}

/* Expanded details: live location + onward stops */
.train__details {
  grid-column: 1 / -1;
  margin-top: 6px;
  padding-top: 10px;
  border-top: 1px solid var(--border);
}
.train__details-loading,
.train__details-empty {
  color: var(--muted);
  font-size: 0.85rem;
}
.train__loc {
  display: inline-block;
  font-size: 0.85rem;
  color: var(--svc-limited);
  text-decoration: none;
  padding: 0;
  background: none;
  border: none;
  cursor: pointer;
  font-family: inherit;
}
button.train__loc:hover {
  text-decoration: underline;
}
.train__loc--none {
  color: var(--muted);
  cursor: default;
}
.stops__label {
  margin: 10px 0 4px;
  font-size: 0.72rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--muted);
}
.stops {
  list-style: none;
  margin: 0;
  padding: 0;
}
.stops__item {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  padding: 4px 0;
  border-bottom: 1px solid var(--border);
  font-size: 0.9rem;
}
.stops__item:last-child {
  border-bottom: none;
}
.stops__time {
  color: var(--muted);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}
.stops__time--late {
  color: var(--svc-express);
}
.stops__late {
  font-size: 0.8rem;
}

/* Inline live map */
.train__mapwrap {
  grid-column: 1 / -1;
  margin-top: 8px;
}
.train__mapinfo {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  margin-bottom: 6px;
  font-size: 0.9rem;
}
.train__mapspd {
  font-weight: 700;
  font-variant-numeric: tabular-nums;
}
.train__mapage {
  color: var(--muted);
  font-size: 0.78rem;
}
.train__map {
  width: 100%;
  height: 300px;
  border-radius: 10px;
  overflow: hidden;
  border: 1px solid var(--border);
  background: var(--bg);
}
/* Leaflet controls/popup tuned to the app theme */
.train__map .leaflet-popup-content-wrapper,
.train__map .leaflet-popup-tip {
  background: var(--card);
  color: var(--text);
}

/* Vehicle marker: a round badge with the train number, plus a directional
   pointer that rotates around it to show travel direction. */
.tmarker {
  position: relative;
  width: 46px;
  height: 46px;
}
/* Rotating layer holding the pointer; its arrow starts pointing up (north). */
.tmarker__dir {
  position: absolute;
  inset: 0;
  transition: transform 0.4s ease;
}
.tmarker__dir::before {
  content: "";
  position: absolute;
  top: 1px;
  left: 50%;
  transform: translateX(-50%);
  border-left: 8px solid transparent;
  border-right: 8px solid transparent;
  border-bottom: 12px solid currentColor;
  filter: drop-shadow(0 1px 1px rgba(0, 0, 0, 0.45));
}
/* Upright, centered number badge. */
.tmarker__badge {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 30px;
  height: 30px;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-weight: 800;
  font-size: 0.72rem;
  font-variant-numeric: tabular-nums;
  color: #fff;
  border: 2px solid rgba(255, 255, 255, 0.9);
  box-shadow: 0 1px 5px rgba(0, 0, 0, 0.5);
}
.tmarker--express { color: var(--svc-express); }
.tmarker--limited { color: var(--svc-limited); }
.tmarker--local { color: #6b7280; }
.tmarker--express .tmarker__badge { background: var(--svc-express); }
.tmarker--limited .tmarker__badge { background: var(--svc-limited); }
.tmarker--local .tmarker__badge { background: #4b5563; }
.train__num {
  grid-row: span 2;
  font-weight: 700;
  font-size: 1.15rem;
  font-variant-numeric: tabular-nums;
  min-width: 3ch;
}
.train__dest {
  font-weight: 600;
}
.train__service {
  grid-column: 2;
  color: var(--muted);
  font-size: 0.85rem;
}
.train__eta {
  grid-row: span 2;
  text-align: right;
}
.train__eta-rel {
  font-weight: 700;
  font-size: 1.1rem;
  font-variant-numeric: tabular-nums;
}
.train__eta-abs {
  color: var(--muted);
  font-size: 0.82rem;
}

.badge {
  display: inline-block;
  margin-top: 4px;
  padding: 2px 8px;
  border-radius: 999px;
  font-size: 0.78rem;
  font-weight: 600;
}
.badge--ontime {
  background: var(--ontime-bg);
  color: var(--ontime-fg);
}
.badge--late {
  background: var(--late-bg);
  color: var(--late-fg);
}
.badge--early {
  background: var(--early-bg);
  color: var(--early-fg);
}
.badge--unmonitored {
  background: var(--border);
  color: var(--muted);
}

.app__footer {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin-top: 22px;
  min-height: 32px;
}
.status {
  color: var(--muted);
  font-size: 0.82rem;
}
.refresh {
  padding: 7px 14px;
  font-size: 0.85rem;
  color: var(--text);
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
}
.refresh:hover {
  border-color: var(--accent);
}
