/* ---------------------------------------------------------------------
   SRT LIVE — token system
   Grounded in two real references: SRT's own oxblood-maroon identity,
   and the amber split-flap departure boards used at Thai stations.
   ------------------------------------------------------------------ */
:root {
  --rail-maroon: #5e1b20;
  --maroon-deep: #3d1114;
  --signal-amber: #e8a33d;
  --amber-dim: #a97a34;
  --signal-teal: #4fb3bf;
  --teal-dim: #3a8791;
  --platform-charcoal: #14171c;
  --panel-steel: #1e232b;
  --steel-line: #3a424d;
  --chalk: #ede6d6;
  --chalk-dim: #b8b2a3;

  --font-display: "IBM Plex Mono", ui-monospace, monospace;
  --font-body: "IBM Plex Sans Thai", "IBM Plex Sans", system-ui, sans-serif;

  --header-h: 56px;
  --sidebar-w: 320px;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  background: var(--platform-charcoal);
  color: var(--chalk);
  font-family: var(--font-body);
}

:focus-visible {
  outline: 2px solid var(--signal-amber);
  outline-offset: 2px;
}

@media (prefers-reduced-motion: reduce) {
  * {
    animation-duration: 0.001ms !important;
    transition-duration: 0.001ms !important;
  }
}

/* ---------- header ---------- */

#header {
  height: var(--header-h);
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 0 16px;
  background: linear-gradient(180deg, var(--rail-maroon), var(--maroon-deep));
  border-bottom: 1px solid rgba(232, 163, 61, 0.25);
  box-shadow: 0 2px 8px rgba(0, 0, 0, 0.4);
  position: relative;
  z-index: 20;
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.brand-mark {
  color: var(--signal-amber);
  font-size: 20px;
  line-height: 1;
}

.brand-text {
  display: flex;
  flex-direction: column;
  line-height: 1.15;
  min-width: 0;
}

.brand-title {
  font-family: var(--font-display);
  font-weight: 700;
  font-size: 15px;
  letter-spacing: 0.08em;
  color: var(--chalk);
}

.brand-sub {
  font-size: 12px;
  color: var(--chalk-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.header-live {
  display: flex;
  align-items: baseline;
  gap: 8px;
  font-family: var(--font-display);
  padding: 4px 12px;
  border: 1px solid rgba(232, 163, 61, 0.35);
  border-radius: 3px;
  background: rgba(0, 0, 0, 0.2);
}

.live-dot {
  width: 7px;
  height: 7px;
  border-radius: 50%;
  background: var(--signal-amber);
  align-self: center;
  box-shadow: 0 0 6px 1px rgba(232, 163, 61, 0.7);
  animation: pulse 2.4s ease-in-out infinite;
}

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

.live-count {
  font-weight: 700;
  font-size: 16px;
  color: var(--signal-amber);
  min-width: 1.6em;
  text-align: right;
}

.live-label {
  font-family: var(--font-body);
  font-size: 12px;
  color: var(--chalk-dim);
  white-space: nowrap;
}

.info-pill,
.panel-toggle {
  font-family: var(--font-body);
  font-size: 12.5px;
  color: var(--chalk-dim);
  background: transparent;
  border: 1px solid var(--steel-line);
  border-radius: 999px;
  padding: 6px 12px;
  cursor: pointer;
  white-space: nowrap;
}

.info-pill:hover,
.panel-toggle:hover {
  border-color: var(--signal-amber);
  color: var(--chalk);
}

.panel-toggle {
  display: none;
  margin-left: auto;
}

.info-note {
  margin: 0;
  padding: 10px 16px;
  background: var(--panel-steel);
  border-bottom: 1px solid var(--steel-line);
  color: var(--chalk-dim);
  font-size: 13px;
  line-height: 1.5;
}

.info-note strong {
  color: var(--chalk);
}

/* ---------- layout ---------- */

#layout {
  position: relative;
  height: calc(100% - var(--header-h));
  display: flex;
}

#map {
  flex: 1;
  min-width: 0;
  background: var(--platform-charcoal);
}

/* Leaflet chrome, restyled to sit inside the dark frame */
.leaflet-control-zoom a {
  background: var(--panel-steel) !important;
  color: var(--chalk) !important;
  border-color: var(--steel-line) !important;
}
.leaflet-control-zoom a:hover {
  background: var(--rail-maroon) !important;
}
.leaflet-control-attribution {
  background: rgba(20, 23, 28, 0.75) !important;
  color: var(--chalk-dim) !important;
}
.leaflet-control-attribution a {
  color: var(--chalk) !important;
}
.leaflet-popup-content-wrapper {
  background: var(--panel-steel);
  color: var(--chalk);
  border: 1px solid var(--steel-line);
  border-radius: 4px;
}
.leaflet-popup-tip {
  background: var(--panel-steel);
}
.leaflet-popup-content {
  font-family: var(--font-body);
  font-size: 13px;
  line-height: 1.5;
}
.popup-train-no {
  font-family: var(--font-display);
  color: var(--signal-amber);
  font-weight: 700;
}
.popup-route-od {
  font-family: var(--font-display);
  font-size: 11px;
  letter-spacing: 0.04em;
  color: var(--amber-dim);
}

/* ---------- permanent labels (station names / train numbers, shown past
   LABEL_ZOOM_THRESHOLD in app.js) — plain text over the map, not Leaflet's
   default white speech-bubble tooltip ---------- */
.leaflet-tooltip.station-label,
.leaflet-tooltip.train-label {
  background: transparent;
  border: none;
  box-shadow: none;
  padding: 0;
  pointer-events: none;
  white-space: nowrap;
}
.leaflet-tooltip.station-label::before,
.leaflet-tooltip.train-label::before {
  display: none;
}

.leaflet-tooltip.station-label {
  font-family: var(--font-body);
  font-size: 10.5px;
  color: var(--chalk-dim);
  text-shadow: 0 0 3px var(--platform-charcoal), 0 0 3px var(--platform-charcoal), 0 1px 2px var(--platform-charcoal);
}

.leaflet-tooltip.train-label {
  font-family: var(--font-display);
  font-size: 10px;
  font-weight: 700;
  color: var(--signal-amber);
  text-shadow: 0 0 3px var(--platform-charcoal), 0 0 3px var(--platform-charcoal), 0 1px 2px var(--platform-charcoal);
}
.leaflet-tooltip.train-label.trip-2 {
  color: var(--signal-teal);
}

/* ---------- markers ---------- */
/* Stations render as L.circleMarker (see app.js), styled entirely via
   Leaflet path options — no CSS class needed for them. */

.train-icon {
  color: var(--signal-amber);
  filter: drop-shadow(0 0 3px rgba(0, 0, 0, 0.7));
  transition: transform 0.3s ease;
}

/* trip 1 = เที่ยวไป (outbound), trip 2 = เที่ยวกลับ (return) — see
   fetchTimetableMatrix.ts. Amber/teal chosen as a colorblind-safe pair. */
.train-icon.trip-2 {
  color: var(--signal-teal);
}

.train-icon.is-at-station {
  filter: brightness(1.4) drop-shadow(0 0 4px var(--chalk));
}

.trip-legend {
  display: flex;
  gap: 12px;
  padding: 6px 10px;
  background: rgba(20, 23, 28, 0.75);
  border: 1px solid var(--steel-line);
  border-radius: 4px;
  font-family: var(--font-body);
  font-size: 12px;
  color: var(--chalk-dim);
}
.trip-legend-item {
  display: inline-flex;
  align-items: center;
  gap: 5px;
}
.trip-legend-swatch {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--signal-amber);
}
.trip-legend-swatch.trip-2 {
  background: var(--signal-teal);
}

/* ---------- sidebar: split-flap departure board ---------- */

#sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  background: var(--panel-steel);
  border-left: 1px solid var(--steel-line);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  transition: width 0.25s ease;
}

#sidebar.is-collapsed {
  width: 0;
  border-left-color: transparent;
}

.sidebar-handle {
  flex-shrink: 0;
  width: 18px;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--panel-steel);
  border: none;
  border-left: 1px solid var(--steel-line);
  color: var(--chalk-dim);
  font-size: 15px;
  line-height: 1;
  cursor: pointer;
}

.sidebar-handle:hover {
  color: var(--signal-amber);
}

.board-head {
  display: grid;
  grid-template-columns: 44px 1fr 72px;
  gap: 8px;
  padding: 10px 14px;
  font-size: 11px;
  letter-spacing: 0.08em;
  color: var(--chalk-dim);
  border-bottom: 1px solid var(--steel-line);
  text-transform: uppercase;
}

.board-head-status {
  text-align: right;
}

.board-list {
  list-style: none;
  margin: 0;
  padding: 0;
  overflow-y: auto;
  flex: 1;
}

.board-row {
  display: grid;
  grid-template-columns: 44px 1fr 72px;
  gap: 8px;
  align-items: center;
  padding: 9px 14px;
  border-bottom: 1px solid rgba(58, 66, 77, 0.5);
  cursor: pointer;
}

.board-row:hover,
.board-row:focus-visible {
  background: rgba(232, 163, 61, 0.08);
}

.board-row.is-selected {
  background: rgba(232, 163, 61, 0.14);
}

.board-no {
  font-family: var(--font-display);
  font-weight: 600;
  font-size: 13px;
  color: var(--signal-amber);
}

.board-route {
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.board-route-od {
  font-family: var(--font-display);
  font-size: 10.5px;
  letter-spacing: 0.04em;
  color: var(--amber-dim);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.board-route-line {
  font-size: 12.5px;
  color: var(--chalk);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.board-route-class {
  font-size: 11px;
  color: var(--chalk-dim);
}

.board-status {
  font-family: var(--font-display);
  font-size: 11px;
  text-align: right;
  color: var(--chalk-dim);
}

.board-status.at-station {
  color: var(--chalk);
}

/* flip animation played when a row's data changes on refresh */
@keyframes flip {
  0% {
    transform: rotateX(90deg);
    opacity: 0;
  }
  60% {
    transform: rotateX(0deg);
    opacity: 1;
  }
}

.board-row.did-update .board-status,
.board-row.did-update .board-no {
  animation: flip 0.35s ease-out;
  transform-origin: center;
}

.board-empty {
  margin: 0;
  padding: 24px 16px;
  color: var(--chalk-dim);
  font-size: 13px;
  text-align: center;
}

/* ---------- responsive ---------- */

@media (max-width: 860px) {
  .brand-sub {
    display: none;
  }
  .header-live .live-label {
    display: none;
  }
  .info-pill span:not([aria-hidden]) {
    display: none;
  }

  .panel-toggle {
    display: inline-block;
  }

  .sidebar-handle {
    display: none;
  }

  #layout {
    position: relative;
  }

  #sidebar {
    position: absolute;
    inset: 0 0 0 auto;
    width: min(85vw, 360px);
    height: 100%;
    z-index: 15;
    transform: translateX(100%);
    transition: transform 0.25s ease;
    box-shadow: -8px 0 24px rgba(0, 0, 0, 0.4);
  }

  #sidebar.is-collapsed {
    width: min(85vw, 360px);
    border-left-color: var(--steel-line);
  }

  #sidebar.is-open {
    transform: translateX(0);
  }
}
