/* Live map.
 *
 * The frame, the palette and the dark mode all come from punctuality.css; this
 * is only the map, the legend and the card that follows the pointer.
 *
 * The band colours are the same four the control room bands its punctuality
 * mix with -- cool for early, neutral for on time, two warm steps for late --
 * so a bus that is orange here is orange there. They are declared twice: once
 * as CSS variables for the legend and the card, and once as literals in
 * live_map.js, because MapLibre paint expressions are evaluated on the GPU and
 * cannot read a custom property. The two lists have to agree by hand.
 *
 * They do NOT flip with the page theme, unlike everything else here. The
 * basemap is a light raster whatever the page is set to, and a colour picked
 * to sit on a dark panel is the wrong colour on top of it.
 */

.viz-root {
  --band-early: #3987e5;
  --band-on-time: #383835;
  --band-late: #ec835a;
  --band-very-late: #d03b3b;
  --band-unknown: #898781;
}

/* ---------- The bar above the map ---------- */

.map-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px 18px;
  margin: 0 0 12px;
  font-size: 13px;
  color: var(--text-secondary);
}

.map-live {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  font-weight: 600;
  color: var(--text-primary);
}

/* A dot that beats once per refresh, so a wallboard left on a screen shows at
   a glance that it is still being fed. It stops rather than keeps beating when
   the feed goes quiet -- an animation that runs regardless is a liveness
   indicator that cannot report the one thing it exists to report. */
.map-live .pulse {
  width: 9px;
  height: 9px;
  border-radius: 50%;
  background: var(--status-good);
  flex: none;
}

.map-live.beat .pulse { animation: live-beat 1.1s ease-out; }
.map-live.stalled .pulse { background: var(--status-critical); animation: none; }

@keyframes live-beat {
  0%   { box-shadow: 0 0 0 0 rgba(12, 163, 12, 0.55); }
  100% { box-shadow: 0 0 0 9px rgba(12, 163, 12, 0); }
}

@media (prefers-reduced-motion: reduce) {
  .map-live .pulse { animation: none !important; }
}

.map-counts { color: var(--text-secondary); }
.map-counts strong { color: var(--text-primary); font-weight: 600; }

.map-toggle-label {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  margin-left: auto;
  cursor: pointer;
  user-select: none;
}

/* ---------- The controls ---------- */

.map-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: flex-end;
  gap: 10px 14px;
  margin: 0 0 12px;
  font-size: 13px;
  color: var(--text-secondary);
}

.ctl {
  display: flex;
  flex-direction: column;
  gap: 3px;
  /* Flex items default to min-width:auto, which lets a long operator name in
     the select push the whole row -- and on a phone, the whole page --
     sideways. Everything in this row is allowed to shrink instead. */
  min-width: 0;
}

.ctl-label {
  font-size: 11.5px;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.map-controls select,
.map-controls input[type="search"] {
  font: inherit;
  font-size: 13px;
  padding: 5px 8px;
  max-width: 100%;
  border: 1px solid var(--border);
  border-radius: 7px;
  background: var(--surface-1);
  color: var(--text-primary);
}

.map-controls select { max-width: 260px; }
.map-controls input[type="search"] { width: 150px; }

.map-controls select:disabled { color: var(--text-muted); }

.map-controls select:focus-visible,
.map-controls input:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.showing {
  margin-left: auto;
  padding-bottom: 6px;
  font-variant-numeric: tabular-nums;
  color: var(--text-muted);
}

/* ---------- Finding one bus ---------- */

.ctl.find { position: relative; }

.find-results {
  position: absolute;
  z-index: 5;
  top: 100%;
  left: 0;
  min-width: 264px;
  max-height: 268px;
  overflow-y: auto;
  margin-top: 4px;
  border: 1px solid var(--border);
  border-radius: 9px;
  background: var(--surface-1);
  box-shadow: 0 8px 26px rgba(0, 0, 0, 0.18);
}

.find-results .hit {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0 10px;
  width: 100%;
  padding: 7px 10px;
  border: 0;
  border-bottom: 1px solid var(--border);
  background: none;
  font: inherit;
  text-align: left;
  cursor: pointer;
  color: var(--text-secondary);
}

.find-results .hit:last-child { border-bottom: 0; }
.find-results .hit:hover,
.find-results .hit:focus-visible { background: var(--accent-soft); }

/* A grid column will not shrink below its content unless told to, which is
   what lets one long fleet id ("1003_-_BV74_NEN" is a real one) stack four
   lines high and squeeze the state out of the row. */
.find-results .hit > * { min-width: 0; }

.hit-fleet {
  font-weight: 600;
  color: var(--text-primary);
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* The operator, always, and never optional. Fleet numbers are only unique
   within one company -- 268 of 7,513 across northern England are shared by
   two -- so a result that showed the number alone would be offering a choice
   between two real buses without saying so. */
.hit-where {
  grid-column: 1;
  font-size: 11.5px;
  color: var(--text-muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.hit-state {
  grid-row: 1 / span 2;
  grid-column: 2;
  align-self: center;
  font-size: 11.5px;
  font-weight: 600;
}

.hit-state.early { color: var(--band-early); }
.hit-state.on-time { color: var(--status-good); }
.hit-state.late { color: var(--band-late); }
.hit-state.very-late { color: var(--band-very-late); }
.hit-state.unknown { color: var(--text-muted); font-weight: 400; }

.find-results .no-hits {
  margin: 0;
  padding: 9px 11px;
  font-size: 12.5px;
  color: var(--text-muted);
}

/* The chip naming the bus being followed. Sits on the map, because the halo
   it refers to is on the map. */
.follow-chip {
  position: absolute;
  z-index: 4;
  top: 10px;
  left: 10px;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 5px 9px 5px 11px;
  border: 1px solid var(--accent);
  border-radius: 999px;
  background: var(--surface-1);
  color: var(--text-secondary);
  font: inherit;
  font-size: 12.5px;
  cursor: pointer;
  box-shadow: 0 3px 12px rgba(0, 0, 0, 0.14);
}

/* `[hidden]` is only a user-agent rule setting display:none, and any author
   rule that sets a display beats it on specificity -- so this button, which
   is display:flex, stayed on screen for the whole session however many times
   the script set .hidden = true. It is the same trap the legend would have
   fallen into. Anything here toggled by the attribute and given a display by
   a class needs this line; .bus-card and .find-results set no display at all,
   so the attribute works on them unaided. */
.follow-chip[hidden] { display: none; }

.follow-chip strong { color: var(--text-primary); }
.follow-chip .chip-x {
  font-size: 16px;
  line-height: 1;
  color: var(--text-muted);
  padding: 0 2px;
}
.follow-chip .chip-x:hover { color: var(--fault); }

/* ---------- The map ---------- */

.map-holder {
  position: relative;
  border: 1px solid var(--border);
  border-radius: var(--radius, 14px);
  overflow: hidden;
  background: var(--surface-1);
}

#map {
  /* Tall enough to be worth opening, short enough that the legend and the
     note under it are still on the screen on a laptop. */
  height: clamp(420px, 68vh, 760px);
}

.map-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 6px 16px;
  padding: 9px 12px;
  border-top: 1px solid var(--border);
  background: var(--surface-1);
  font-size: 12px;
  color: var(--text-secondary);
}

.map-legend .k { display: inline-flex; align-items: center; gap: 6px; }

.map-legend i {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  flex: none;
}

/* Each swatch sits on a ring of the basemap's own paper colour, so it is shown
   against the surface it is actually drawn on. Without it the near-black "on
   time" dot disappears into the legend bar when the page is in dark mode --
   and lightening the dot to fix that would be showing a colour the map does
   not use. The basemap is a light raster whatever the page theme is, so this
   ring is the same in both. */
.map-legend i { box-shadow: 0 0 0 2px #f2efe9; }

.map-legend .early i { background: var(--band-early); }
.map-legend .on-time i { background: var(--band-on-time); }
.map-legend .late i { background: var(--band-late); }
.map-legend .very-late i { background: var(--band-very-late); }
.map-legend .unknown i { background: var(--band-unknown); opacity: 0.45; }
/* Stale is drawn as a ring rather than a fill, which is what the layer does. */
.map-legend .stale i {
  background: transparent;
  border: 2px solid var(--band-unknown);
}

/* ---------- The card under the pointer ---------- */

.bus-card {
  position: absolute;
  z-index: 3;
  min-width: 194px;
  max-width: 260px;
  padding: 9px 11px 10px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--surface-1);
  box-shadow: 0 6px 22px rgba(0, 0, 0, 0.16);
  font-size: 12.5px;
  line-height: 1.45;
  color: var(--text-secondary);
  /* It follows the pointer, so it must never be under it: a card that can be
     hovered steals the mouseleave from the bus and sticks on the screen. */
  pointer-events: none;
}

.bus-card .row-line {
  display: flex;
  align-items: baseline;
  gap: 7px;
  margin-bottom: 2px;
}

.bus-card .line {
  font-weight: 700;
  font-size: 15px;
  color: var(--text-primary);
}

.bus-card .operator { font-size: 11.5px; color: var(--text-muted); }

.bus-card .dest {
  color: var(--text-primary);
  margin-bottom: 5px;
}

.bus-card .state {
  font-weight: 600;
  display: block;
  margin-bottom: 4px;
}

.bus-card .state.early { color: var(--band-early); }
.bus-card .state.on-time { color: var(--status-good); }
.bus-card .state.late { color: var(--band-late); }
.bus-card .state.very-late { color: var(--band-very-late); }
.bus-card .state.unknown { color: var(--text-muted); font-weight: 400; }

.bus-card dl {
  margin: 0;
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 1px 9px;
}

.bus-card dt { color: var(--text-muted); }
.bus-card dd { margin: 0; color: var(--text-primary); }

/* The next stop and its time arrive a beat after the rest -- they are a
   separate query, and pretending otherwise by leaving a blank gap reads as a
   fault. It says it is fetching. */
.bus-card .pending { color: var(--text-muted); font-style: italic; }

.bus-card .stale-note {
  display: block;
  margin-top: 5px;
  padding-top: 5px;
  border-top: 1px solid var(--border);
  color: var(--status-critical);
}

/* ---------- Narrow screens ---------- */

@media (max-width: 720px) {
  #map { height: clamp(340px, 58vh, 520px); }
  .map-toggle-label { margin-left: 0; }
  .showing { margin-left: 0; padding-bottom: 0; }
  /* Two controls per row rather than one long line of four. */
  .ctl { flex: 1 1 44%; }
  /* Its own row: the results hang off this element, and half a phone is not
     enough width for an operator name. */
  .ctl.find { flex: 1 1 100%; }
  .map-controls select,
  .map-controls input[type="search"] { width: 100%; max-width: none; }
  .find-results { min-width: 0; width: 100%; }
  /* There is no pointer to follow on a touch screen, so the card is pinned to
     the bottom of the map and opened by tapping a bus instead. */
  .bus-card {
    left: 8px !important;
    right: 8px;
    bottom: 8px;
    top: auto !important;
    max-width: none;
  }
}
