/* Tracks On Time — live train map page.
   Builds on the tokens and components in app.css (cards, chips, buttons, .tl).
   Markers: MapLibre owns the transform and inline opacity of each marker's root
   element, so every visual change (scale, opacity, rotation) targets inner elements
   and nothing here ever transitions a marker's transform.
   ---------------------------------------------------------------------- */

/* ------------------------------------------------------------- layout */
body.page-map { overflow: hidden; overscroll-behavior: none; }
body.page-map main { min-height: 0; }
html:has(body.page-map) { overscroll-behavior: none; }

.mapwrap {
  position: fixed; left: 0; right: 0; z-index: 1;
  top: var(--topbar-h);
  bottom: calc(var(--tabbar-h) + var(--safe-b));
  overflow: hidden;
  background: #eef0ee;
  --panel-w: 380px;
}
@media (min-width: 900px) { .mapwrap { bottom: 0; } }
@media (prefers-color-scheme: dark) { .mapwrap { background: #101114; } }
.map { position: absolute; inset: 0; }
.mapwrap.no-map .map { visibility: hidden; }
/* app.css gives .btn / .chip / .filter-chip explicit display values, which beat the
   browser's [hidden] rule. Make `hidden` dependable everywhere on this page. */
.mapwrap [hidden] { display: none !important; }

/* ------------------------------------------------- status pill + filters */
.map-top {
  position: absolute; z-index: 5; top: 10px; left: 0; right: 0;
  display: flex; flex-direction: column; align-items: flex-start; gap: 8px;
  padding: 0 12px; padding-left: max(12px, env(safe-area-inset-left));
  pointer-events: none;
}
.map-top > * { pointer-events: auto; }
.map-status {
  display: inline-flex; align-items: center; gap: 8px;
  height: 32px; padding: 0 12px; border-radius: 16px;
  background: var(--card); color: var(--ink-2); box-shadow: var(--shadow);
  border: 1px solid color-mix(in srgb, var(--line) 70%, transparent);
  font-size: .82rem; font-weight: 700; white-space: nowrap; font-variant-numeric: tabular-nums;
}
.map-status.is-loading .live-dot { background: var(--muted); animation: none; }
.map-status.is-stale { color: var(--warn); }
.map-filters { max-width: 100%; margin: 0 -12px; padding: 2px 12px 8px; }
.map-filters:empty { display: none; }
.map-filters .filter-chip { height: 34px; padding: 0 12px; gap: 6px; box-shadow: var(--shadow); font-size: .85rem; }
.map-filters .cnt { font-size: .76rem; font-weight: 700; opacity: .7; font-variant-numeric: tabular-nums; }
.map-filters .dot { width: 9px; height: 9px; box-shadow: 0 0 0 1.5px var(--card); }

/* -------------------------------------------------------- side buttons */
.map-fabs {
  position: absolute; z-index: 5; right: 12px; right: max(12px, env(safe-area-inset-right)); top: 104px;
  display: flex; flex-direction: column; gap: 10px;
}
.fab {
  display: grid; place-items: center; width: 44px; height: 44px; padding: 0;
  border-radius: 14px; border: 1px solid color-mix(in srgb, var(--line) 70%, transparent);
  background: var(--card); color: var(--ink); box-shadow: var(--shadow);
}
.fab:hover { background: var(--card-2); }
.fab[aria-busy="true"] .ic { animation: fab-spin 1.1s linear infinite; }
.fab[aria-expanded="true"] { color: var(--brand-2); }
@keyframes fab-spin { to { transform: rotate(360deg); } }
.no-map .map-fabs .fab:not(#fab-list) { display: none; }

@media (min-width: 900px) {
  .map-top { flex-direction: row; align-items: center; right: 72px; top: 12px; padding-left: 12px; transition: padding-left .3s cubic-bezier(.2, .8, .2, 1); }
  .map-filters { margin: 0; padding: 2px 2px 8px; min-width: 0; }
  .map-status { flex: none; margin-bottom: 6px; }
  .mapwrap.has-sheet .map-top { padding-left: calc(var(--panel-w) + 12px); }
  .map-fabs { top: 12px; }
}

/* ------------------------------------------- saved stations / commute strip */
/* Chips along the bottom of the map (filled from local storage by map.js). Hidden by an
   open sheet on phones; on desktop it slides right of the side panel. It sits one row above
   MapLibre's attribution (24px + 10px margin), which opens expanded on first load, so the
   credits stay readable and nothing hides behind the chips. */
.map-quick {
  position: absolute; z-index: 5; left: 0; right: 0; bottom: 36px;
  display: flex; gap: 8px; overflow-x: auto; scrollbar-width: none; -webkit-overflow-scrolling: touch;
  padding: 4px max(12px, env(safe-area-inset-right)) 4px max(12px, env(safe-area-inset-left));
  pointer-events: none;
}
.map-quick::-webkit-scrollbar { display: none; }
.map-quick > * { pointer-events: auto; }
.qchip {
  flex: none; display: inline-flex; align-items: center; gap: 6px; max-width: 70vw;
  height: 38px; padding: 0 14px 0 11px; border-radius: 19px;
  border: 1px solid color-mix(in srgb, var(--line) 70%, transparent); background: var(--card); color: var(--ink);
  box-shadow: var(--shadow); font-weight: 700; font-size: .88rem; cursor: pointer; text-decoration: none;
}
.qchip span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.qchip .ic { width: 16px; height: 16px; color: var(--brand-2); flex: none; }
.qchip:hover { background: var(--card-2); text-decoration: none; }
.qchip:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
.qchip-trip { background: var(--brand-2); border-color: var(--brand-2); color: #fff; }
.qchip-trip .ic { color: #fff; }
.qchip-trip:hover { background: var(--brand); }
.mapwrap.has-sheet .map-quick { display: none; }
@media (min-width: 900px) {
  .map-quick { right: 60px; padding-right: 12px; }       /* clear the zoom control */
  .mapwrap.has-sheet .map-quick { display: flex; left: var(--panel-w); }
}

/* -------------------------------------------------------- train markers */
.tmk {
  width: 40px; height: 40px; padding: 0; margin: 0;               /* 40×40 hit area */
  border: 0; background: none; color: inherit; cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.tmk-in { position: absolute; inset: 0; display: grid; place-items: center; }
.tmk-dot {
  position: relative; width: 24px; height: 24px; border-radius: 50%;
  background: var(--c, #475569); border: 2px solid #fff;
  box-shadow: 0 1px 2px rgba(0, 0, 0, .35), 0 2px 6px rgba(0, 0, 0, .25);
}
.tmk-rot { position: absolute; inset: 0; }                         /* rotated by JS to the bearing */
.tmk-arrow {
  position: absolute; top: 1px; left: 50%; width: 12px; height: 8px; margin-left: -6px; overflow: visible;
  fill: var(--c, #475569); stroke: #fff; stroke-width: 1.6; stroke-linejoin: round;
  filter: drop-shadow(0 0 1px rgba(0, 0, 0, .35));
}
.tmk-delay {
  display: none; position: absolute; top: 5px; right: 5px; width: 11px; height: 11px;
  border-radius: 50%; border: 2px solid #fff; box-shadow: 0 0 2px rgba(0, 0, 0, .4);
}
.tmk.late-warn .tmk-delay { display: block; background: #f59e0b; }
.tmk.late-bad .tmk-delay { display: block; background: #dc2626; }
.tmk-num {
  display: none; position: absolute; left: 33px; top: 50%; margin-top: -9px;
  height: 18px; padding: 0 6px; border-radius: 6px;
  background: var(--card); color: var(--ink); box-shadow: 0 1px 3px rgba(0, 0, 0, .3);
  font: 800 11px/18px var(--font); font-variant-numeric: tabular-nums; white-space: nowrap;
}
.show-nums .tmk-num, .tmk.is-sel .tmk-num { display: block; }

/* Phase styling. Opacity lives on inner parts because MapLibre sets the root's. */
.tmk.ph-origin .tmk-dot { width: 19px; height: 19px; opacity: .78; }
.tmk.ph-origin .tmk-rot { display: none; }
.tmk.ph-origin .tmk-delay { top: 8px; right: 8px; }
/* Unmatched (approximate position): dashed rim, colour faded to ~60%. Mixed rather than
   made transparent, so the track underneath doesn't show through and smudge the dot. */
.tmk.ph-unmatched .tmk-dot { background: color-mix(in srgb, var(--c, #475569) 60%, var(--card)); border-style: dashed; }
.tmk.ph-unmatched .tmk-rot { opacity: .6; }

.tmk.is-sel { z-index: 3; }
.tmk.is-sel .tmk-in { transform: scale(1.22); }
.tmk.is-sel .tmk-dot {
  opacity: 1;
  box-shadow: 0 0 0 4px color-mix(in srgb, var(--c, #475569) 38%, transparent), 0 2px 10px rgba(0, 0, 0, .45);
}
.tmk.is-off { display: none; }
.tmk:focus { outline: none; }
.tmk:focus-visible .tmk-dot { outline: 3px solid var(--focus); outline-offset: 3px; }

/* ---------------------------------------------------------------- sheet */
.sheet {
  position: absolute; z-index: 8; left: 0; right: 0; bottom: 0;
  height: var(--sheet-h, 88%);
  display: flex; flex-direction: column;
  background: var(--card); color: var(--ink);
  border-radius: 20px 20px 0 0;
  box-shadow: 0 -4px 28px rgba(15, 23, 42, .2);
  transform: translateY(104%);
  visibility: hidden;
  transition: transform .3s cubic-bezier(.2, .8, .2, 1), visibility 0s linear .3s;
}
.sheet.is-open {
  transform: translateY(var(--sheet-y, 0px));
  visibility: visible;
  transition: transform .3s cubic-bezier(.2, .8, .2, 1), visibility 0s;
}
.sheet.is-dragging { transition: none; }
.sheet-top { position: relative; flex: none; padding: 0 16px; touch-action: none; cursor: grab; user-select: none; -webkit-user-select: none; }
.sheet.is-dragging .sheet-top { cursor: grabbing; }
.sheet-grab { display: grid; place-items: center; height: 20px; }
.sheet-grab span { width: 40px; height: 5px; border-radius: 3px; background: var(--line-2); }
.sheet-head { padding: 0 0 10px; }
.sheet-x { position: absolute; top: 10px; right: 6px; color: var(--muted); }
.sheet-body {
  flex: 1; min-height: 0; overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch;
  padding: 0 16px calc(20px + var(--sheet-pad, 0px));
}
@media (min-width: 900px) {
  .sheet {
    top: 0; right: auto; width: var(--panel-w); height: auto;
    border-radius: 0; border-right: 1px solid var(--line);
    box-shadow: 4px 0 24px rgba(15, 23, 42, .12);
    transform: translateX(-104%);
  }
  .sheet.is-open { transform: none; }
  .sheet-grab { display: none; }
  .sheet-top { cursor: auto; padding-top: 16px; touch-action: auto; user-select: auto; -webkit-user-select: auto; }
  .sheet-x { top: 12px; }
}

/* ------------------------------------------------------- sheet contents */
.sh-title { display: flex; align-items: center; gap: 12px; min-height: 44px; padding-right: 44px; }
.sh-title .grow { flex: 1; min-width: 0; }
.sh-title h2 { font-size: 1.22rem; letter-spacing: -.01em; overflow-wrap: anywhere; }
/* The title receives focus when a sheet opens (so screen readers announce it); it is not
   interactive, so no ring — Chrome would otherwise draw one after a marker tap. */
.sh-title h2:focus, .sh-title h2:focus-visible { outline: none; }
.sh-sub { margin: 2px 0 0; color: var(--muted); font-size: .92rem; font-weight: 600; }
.sh-title .chips { margin-top: 6px; }
.sh-status { margin: 2px 0 12px; font-size: 1rem; line-height: 1.35; }
.sh-status strong { font-weight: 800; }
.sh-status .sh-next { color: var(--ink-2); }
.sh-actions { margin-bottom: 10px; }
.sh-actions .btn { min-height: 42px; padding: 0 12px; font-size: .92rem; }
.sh-actions .btn .ic { width: 18px; height: 18px; }
.sh-actions .btn[aria-pressed="true"] { color: var(--brand-2); border-color: var(--brand-2); background: var(--info-bg); }
.sh-actions-3 .btn { padding: 0 8px; font-size: .86rem; gap: 6px; }
.sh-earlier {
  display: flex; align-items: center; justify-content: center; gap: 6px; width: 100%; margin: 6px 0 0; padding: 9px 10px;
  border: 0; border-radius: 10px; background: var(--card-2); color: var(--brand-2); font-weight: 700; font-size: .86rem;
}
.sheet .tl { margin: 0 -8px; padding: 6px 0 2px; }
.sheet .tl li { padding-top: 7px; padding-bottom: 7px; }
.sheet .tl .nm { color: var(--ink); }
.sheet .tl li.passed .nm { color: var(--muted); }
.sheet .tl .tm.late { color: var(--bad); }
.sheet .notice { margin: 4px 0 10px; }
.sh-foot { margin: 14px 0 0; color: var(--muted); font-size: .75rem; }
.sh-sec { display: flex; align-items: center; justify-content: space-between; gap: 10px; margin: 6px 0 8px; }
.sh-sec h3 { font-size: .95rem; }
.sh-board .card { box-shadow: none; }
.sh-board .board-block > .card { margin-top: 0 !important; }
.sh-skel { padding: 6px 0; }
.sh-skel .skel { height: 16px; margin: 12px 0; }

/* train list */
.sh-search {
  display: flex; align-items: center; gap: 8px; height: 44px; margin: 4px 0 2px; padding: 0 12px;
  border-radius: 12px; border: 1px solid var(--line); background: var(--card-2); color: var(--muted);
}
.sh-search input { flex: 1; min-width: 0; height: 100%; border: 0; outline: 0; background: transparent; color: var(--ink); font-size: 1rem; }
.sh-search:focus-within { border-color: var(--focus); }
.lgrp { display: flex; align-items: center; gap: 8px; margin: 14px 0 2px; font-size: .82rem; font-weight: 800; color: var(--ink-2); }
.lgrp .cnt { margin-left: auto; color: var(--muted); font-weight: 700; }
.lrows { list-style: none; margin: 0 -8px; padding: 0; }
.lrow {
  display: grid; grid-template-columns: 80px minmax(0, 1fr) auto; grid-template-areas: "num dest st" "num meta meta";
  align-items: center; gap: 1px 10px; width: 100%; min-height: 56px; padding: 8px;
  border: 0; border-radius: 12px; background: transparent; color: var(--ink); text-align: left;
}
.lrow:hover { background: var(--card-2); }
.lrow .lnum { grid-area: num; display: flex; align-items: center; gap: 8px; font-weight: 800; font-variant-numeric: tabular-nums; white-space: nowrap; }
.lrow .ldot {                                                     /* echoes the map marker */
  flex: none; width: 14px; height: 14px; border-radius: 50%; background: var(--c, #475569);
  border: 2px solid #fff; box-shadow: 0 0 0 1px rgba(15, 23, 42, .18), 0 1px 3px rgba(15, 23, 42, .25);
}
.lrow .ldest { grid-area: dest; font-weight: 700; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.lrow .lst { grid-area: st; font-size: .82rem; font-weight: 750; white-space: nowrap; }
.lrow .lmeta { grid-area: meta; font-size: .82rem; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.sheet .empty { padding: 28px 12px; }

/* ------------------------------------------------------------ fallback */
.map-fallback { position: absolute; z-index: 6; inset: 0; display: grid; place-items: center; padding: 20px; }
.map-fallback[hidden] { display: none; }
.map-fallback .card { max-width: 380px; text-align: center; }
.map-fallback .ic { width: 34px; height: 34px; margin: 0 auto 10px; color: var(--muted); }
.map-fallback strong { display: block; font-size: 1.08rem; margin-bottom: 6px; }
.map-fallback p { color: var(--ink-2); font-size: .92rem; }
.map-fallback .btn-row { justify-content: center; }
@media (min-width: 900px) { .mapwrap.has-sheet .map-fallback { left: var(--panel-w); } }

/* ------------------------------------------------------ MapLibre controls */
.mapwrap .maplibregl-ctrl-attrib { font-size: 11px; }
@media (prefers-color-scheme: dark) {
  .mapwrap .maplibregl-ctrl-attrib, .mapwrap .maplibregl-ctrl-attrib.maplibregl-compact { background: rgba(19, 28, 46, .85); color: #c4cfdf; }
  .mapwrap .maplibregl-ctrl-attrib a { color: #c4cfdf; }
  .mapwrap .maplibregl-ctrl-attrib-button { filter: invert(1) hue-rotate(180deg); }
  .mapwrap .maplibregl-ctrl-group { background: var(--card); }
  .mapwrap .maplibregl-ctrl-group button + button { border-top-color: var(--line); }
  .mapwrap .maplibregl-ctrl button .maplibregl-ctrl-icon { filter: invert(1); }
}

@media (prefers-reduced-motion: reduce) {
  .sheet, .sheet.is-open, .map-top { transition: none !important; }
  .sheet .tl li.is-next .node::after, .fab[aria-busy="true"] .ic { animation: none; }
}
