:root {
  --bg: #f4f6fa;
  --panel: #ffffff;
  --card: #ffffff;
  --line: #d9dee6;
  --text: #1b2330;
  --muted: #5c6776;
  --accent: #0a7ea4;
  --ontime: #1a9e4b;
  --late: #d23035;
  --early: #b87514;
  --unknown: #8b97a8;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body {
  font-family: -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  height: 100vh;
  display: flex;
  flex-direction: column;
}
header {
  padding: 14px 20px 10px;
  border-bottom: 1px solid var(--line);
  background: var(--panel);
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand h1 { font-size: 20px; letter-spacing: -0.3px; }
.pulse {
  width: 12px; height: 12px; border-radius: 50%;
  background: var(--accent);
  box-shadow: 0 0 0 0 rgba(54,197,240,0.7);
  animation: pulse 2s infinite;
}
@keyframes pulse {
  0% { box-shadow: 0 0 0 0 rgba(54,197,240,0.6); }
  70% { box-shadow: 0 0 0 12px rgba(54,197,240,0); }
  100% { box-shadow: 0 0 0 0 rgba(54,197,240,0); }
}
.tag { color: var(--muted); font-size: 13px; margin-top: 4px; }

.views { display: flex; gap: 6px; margin-left: 18px; }
.view-btn {
  background: var(--card); color: var(--muted);
  border: 1px solid var(--line); border-radius: 6px;
  padding: 5px 12px; font-size: 13px; cursor: pointer;
}
.view-btn:hover { color: var(--text); }
.view-btn.active { background: var(--accent); color: #ffffff; font-weight: 600; }

.city-bar {
  display: flex; gap: 8px; padding: 10px 20px;
  background: var(--panel); border-bottom: 1px solid var(--line);
  overflow-x: auto;
}
.city-btn {
  background: var(--card); color: var(--text);
  border: 1px solid var(--line); border-radius: 999px;
  padding: 6px 14px; font-size: 13px; cursor: pointer;
  white-space: nowrap; transition: all .15s;
}
.city-btn:hover { border-color: var(--accent); }
.city-btn.active { background: var(--accent); color: #ffffff; font-weight: 600; border-color: var(--accent); }
.city-btn .country { color: var(--muted); font-size: 11px; }
.city-btn.active .country { color: rgba(255,255,255,0.75); }

main { flex: 1; display: flex; min-height: 0; }
main[hidden] { display: none; }
#map { flex: 1; background: #f4f6fa; }

#panel {
  width: 340px; background: var(--panel);
  border-left: 1px solid var(--line);
  overflow-y: auto; padding: 14px;
}
.card {
  background: var(--card); border: 1px solid var(--line);
  border-radius: 10px; padding: 14px; margin-bottom: 14px;
}
.card h2 { font-size: 17px; }
.card h3 { font-size: 13px; text-transform: uppercase; letter-spacing: .5px; color: var(--muted); margin-bottom: 10px; }
.muted { color: var(--muted); font-size: 12px; line-height: 1.5; }

#feed-health { font-size: 12px; margin-top: 6px; }
#feed-health.ok { color: var(--ontime); }
#feed-health.down { color: var(--late); }

#more-info-host { margin-top: 12px; }
.more-info-summary {
  cursor: pointer; font-size: 12px; color: var(--accent);
  font-weight: 500; padding: 4px 0; user-select: none;
}
.more-info-summary:hover { text-decoration: underline; }
.more-info-body {
  margin-top: 10px; font-size: 12px; padding: 10px 0;
  border-top: 1px solid var(--line); border-bottom: 1px solid var(--line);
}
.more-info-row { display: flex; flex-direction: column; padding: 8px 0; }
.more-info-row:first-child { padding-top: 0; }
.more-info-row:last-child { padding-bottom: 0; }
.more-info-label { font-weight: 600; color: var(--text); margin-bottom: 2px; }
.more-info-value { color: var(--muted); line-height: 1.4; word-break: break-word; }
.feed-list { list-style: none; padding: 0; margin: 4px 0 0 0; }
.feed-list li {
  display: flex; align-items: flex-start; gap: 6px; padding: 4px 0;
  font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 11px;
}
.badge {
  display: inline-block; background: var(--line); color: var(--text);
  border-radius: 3px; padding: 1px 5px; font-size: 10px;
  font-weight: 500; font-family: -apple-system, "Segoe UI", Roboto, sans-serif;
  white-space: nowrap; flex-shrink: 0;
}
.more-info-value a {
  color: var(--accent); text-decoration: none; word-break: break-all;
}
.more-info-value a:hover { text-decoration: underline; }

.now-grid { display: flex; gap: 8px; margin: 12px 0 8px; }
.now { flex: 1; text-align: center; background: var(--bg); border-radius: 8px; padding: 8px 4px; }
.big { font-size: 22px; font-weight: 700; display: block; }
.big.bad { color: var(--late); }
.now label { font-size: 10px; color: var(--muted); text-transform: uppercase; }

.windows { display: flex; flex-wrap: wrap; gap: 6px; margin-bottom: 12px; }
.win-btn {
  background: var(--bg); color: var(--muted);
  border: 1px solid var(--line); border-radius: 6px;
  padding: 4px 9px; font-size: 12px; cursor: pointer;
}
.win-btn:hover { color: var(--text); }
.win-btn.active { background: var(--accent); color: #ffffff; font-weight: 600; }

.stat-row { display: flex; justify-content: space-between; padding: 6px 0; border-bottom: 1px solid var(--line); font-size: 13px; }
.stat-row:last-child { border-bottom: none; }
.stat-row .v { font-weight: 600; }
.bar { height: 8px; border-radius: 4px; overflow: hidden; display: flex; margin: 8px 0 12px; background: var(--bg); }
.bar span { display: block; height: 100%; }
.empty-note { color: var(--early); font-size: 12px; margin-top: 8px; }

.route { font-family: ui-monospace, "SF Mono", Menlo, monospace; font-size: 12px; }

/* Clickable worst/best-routes rows (TASK E) — a <button> styled to read like a
   stat row but affording a click to highlight the route on the map. */
.route-row {
  width: 100%; display: flex; justify-content: space-between; align-items: center;
  gap: 10px; font: inherit; text-align: left; cursor: pointer;
  background: none; border: none; border-bottom: 1px solid var(--line);
  padding: 6px 4px; border-radius: 6px; transition: background .12s;
}
.route-row:last-child { border-bottom: none; }
.route-row:hover { background: color-mix(in srgb, var(--accent) 8%, transparent); }
.route-row:focus-visible { outline: 2px solid var(--accent); outline-offset: -1px; }
.route-row .v { font-weight: 600; }

/* Per-mode punctuality breakdown rows (TASK B, modes panel) — label + full
   stacked bar + legend so each mode's components visibly sum to 100%. */
.mode-row { padding: 8px 0; border-bottom: 1px solid var(--line); }
.mode-row:last-child { border-bottom: none; }
.mode-row-head { display: flex; justify-content: space-between; align-items: baseline; font-size: 13px; }
.mode-row-head .v { font-weight: 600; color: var(--accent); }
.bar.mode-bar { margin: 6px 0 4px; }
.mode-row-legend { font-size: 11px; color: var(--muted); line-height: 1.4; }

/* Compare-table four-segment stacked mini-bar (TASK B) — replaces the single
   on-time minibar so the row's bar reads as the full 100% breakdown. */
.minibar4 {
  display: flex; height: 8px; width: 100%; min-width: 60px;
  border-radius: 4px; overflow: hidden; background: var(--bg);
}
.minibar4 i { display: block; height: 100%; }

/* ─────────── compare view ─────────── */
.cmp-wrap { flex: 1; overflow-y: auto; padding: 16px; max-width: 1100px; margin: 0 auto; width: 100%; }
.chip {
  background: var(--bg); color: var(--muted);
  border: 1px solid var(--line); border-radius: 999px;
  padding: 6px 13px; font-size: 13px; cursor: pointer; transition: all .12s;
}
.chip:hover { color: var(--text); border-color: var(--accent); }
.chip.on { background: var(--accent); color: #ffffff; font-weight: 600; border-color: var(--accent); }
.chip.disabled, .chip:disabled {
  opacity: .4; cursor: not-allowed; pointer-events: none;
}
.mini-btn {
  background: var(--bg); color: var(--muted);
  border: 1px solid var(--line); border-radius: 6px;
  padding: 4px 10px; font-size: 12px; cursor: pointer;
}
.mini-btn:hover { color: var(--text); }

/* ── Compare: dual-pane transfer list ── */
.cmp-transfer {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  gap: 12px;
  align-items: stretch;
}
.cmp-pane {
  display: flex; flex-direction: column;
  border: 1px solid var(--line); border-radius: 10px;
  background: var(--bg); overflow: hidden; min-width: 0;
}
.cmp-pane-head {
  display: flex; align-items: center; justify-content: space-between;
  gap: 8px; padding: 9px 12px;
  border-bottom: 1px solid var(--line); background: var(--panel);
}
.cmp-pane-title { font-size: 12px; font-weight: 700; text-transform: uppercase; letter-spacing: .5px; color: var(--muted); }
.cmp-count { color: var(--accent); font-weight: 700; }
.cmp-link {
  background: none; border: none; cursor: pointer; padding: 0;
  font: inherit; font-size: 12px; font-weight: 600; color: var(--accent);
}
.cmp-link:hover { text-decoration: underline; }
.cmp-pane-search {
  margin: 8px 8px 0; padding: 7px 10px;
  border: 1px solid var(--line); border-radius: 6px;
  background: var(--panel); color: var(--text); font: inherit; font-size: 13px;
}
.cmp-pane-search:focus { outline: 2px solid var(--accent); outline-offset: -1px; }
.cmp-pane-search::placeholder { color: var(--muted); }
.cmp-list {
  list-style: none; margin: 8px; padding: 0;
  overflow-y: auto; height: 300px;
  display: flex; flex-direction: column; gap: 2px;
}
.cmp-list::-webkit-scrollbar { width: 8px; }
.cmp-list::-webkit-scrollbar-thumb { background: var(--line); border-radius: 4px; }
.cmp-row {
  display: flex; align-items: baseline; justify-content: space-between; gap: 8px;
  padding: 8px 11px; border-radius: 7px; cursor: pointer;
  border: 1px solid transparent; transition: background .12s, border-color .12s;
  user-select: none;
}
.cmp-row:hover { background: var(--panel); border-color: var(--line); }
.cmp-row.sel { background: color-mix(in srgb, var(--accent) 14%, transparent); border-color: var(--accent); }
.cmp-row.sel .cmp-row-name { color: var(--accent); font-weight: 600; }
.cmp-row:focus-visible { outline: 2px solid var(--accent); outline-offset: -1px; }
.cmp-row-name { font-size: 13px; color: var(--text); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.cmp-row-meta { font-size: 11px; color: var(--muted); flex-shrink: 0; }
.cmp-empty { padding: 16px 11px; font-size: 12px; color: var(--muted); text-align: center; line-height: 1.5; cursor: default; }
.cmp-transfer-mid {
  display: flex; flex-direction: column; justify-content: center; gap: 8px;
}
.cmp-move {
  width: 40px; height: 40px; border-radius: 8px; cursor: pointer;
  border: 1px solid var(--line); background: var(--panel); color: var(--accent);
  font-size: 18px; line-height: 1; transition: all .12s;
}
.cmp-move:hover { background: var(--accent); color: #fff; border-color: var(--accent); transform: scale(1.05); }
.cmp-move:active { transform: scale(.96); }
.cmp-move:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
@media (max-width: 640px) {
  .cmp-transfer { grid-template-columns: 1fr; }
  .cmp-transfer-mid { flex-direction: row; justify-content: center; }
  .cmp-move { transform: rotate(90deg); }
  .cmp-move:hover { transform: rotate(90deg) scale(1.05); }
}

.highlights { display: flex; flex-wrap: wrap; gap: 12px; margin-bottom: 14px; }
.hl {
  flex: 1; min-width: 160px; background: var(--card);
  border: 1px solid var(--line); border-radius: 10px; padding: 12px 14px;
}
.hl label { font-size: 11px; text-transform: uppercase; letter-spacing: .5px; color: var(--muted); }
.hl-v { display: block; font-size: 22px; font-weight: 700; color: var(--accent); margin: 4px 0 2px; }
.hl-c { font-size: 13px; color: var(--text); }

#cmp-output { overflow: visible; }
table.cmp { width: 100%; border-collapse: collapse; font-size: 13px; }
table.cmp thead th {
  position: sticky; top: 0; z-index: 2;
  background: var(--panel);
  /* box-shadow draws the divider because border-collapse drops a sticky cell's
     own border once it detaches and scrolls */
  box-shadow: inset 0 -1px 0 var(--line);
}
table.cmp th {
  text-align: right; padding: 8px 10px; color: var(--muted);
  font-size: 11px; text-transform: uppercase; letter-spacing: .4px;
  border-bottom: 1px solid var(--line); white-space: nowrap;
}
table.cmp th:nth-child(2) { text-align: left; }
table.cmp td { text-align: right; padding: 8px 10px; border-bottom: 1px solid var(--line); white-space: nowrap; }
table.cmp td.city { text-align: left; font-weight: 600; }
table.cmp td.v { color: var(--accent); font-weight: 700; }
table.cmp td.bad { color: var(--late); font-weight: 600; }
table.cmp tbody tr:hover { background: var(--card); }
.barcell { width: 90px; }
.minibar { display: block; height: 8px; background: var(--accent); border-radius: 4px; min-width: 2px; }
table.cmp td.cmp-nodata { text-align: left; color: var(--muted); font-style: italic; font-size: 12px; }

.legend ul { list-style: none; }
.legend li { font-size: 12px; padding: 3px 0; color: var(--muted); }
.legend-summary { font-size: 13px; font-weight: 600; cursor: pointer; color: var(--text); padding: 2px 0; list-style: none; }
.legend-summary::-webkit-details-marker { display: none; }
.legend-summary::before { content: "▸"; display: inline-block; margin-right: 6px; color: var(--muted); transition: transform .15s; }
.legend-details[open] .legend-summary::before { transform: rotate(90deg); }
.legend-body { margin-top: 8px; }
.leg-punct { display: flex; flex-wrap: wrap; gap: 4px 14px; font-size: 12px; color: var(--muted); margin: 2px 0 4px; }
.leg-modes { list-style: none; display: grid; grid-template-columns: 1fr 1fr; gap: 1px 12px; }
.leg-modes li { display: flex; align-items: center; gap: 6px; font-size: 12px; color: var(--muted); padding: 2px 0; }
.leg-glyph { width: 16px; text-align: center; flex-shrink: 0; }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 6px; vertical-align: middle; }
.dot.on_time { background: var(--ontime); }
.dot.late { background: var(--late); }
.dot.early { background: var(--early); }
.dot.unknown { background: var(--unknown); }
.line { display: inline-block; width: 18px; height: 3px; margin-right: 6px; vertical-align: middle; border-radius: 1px; }

#trend-chart { width: 100%; max-height: 200px; }

footer {
  padding: 8px 20px; font-size: 11px; color: var(--muted);
  background: var(--panel); border-top: 1px solid var(--line);
}
.leaflet-popup-content { font-size: 12px; line-height: 1.5; }

/* vehicle map markers — white disc, ring colour = punctuality, glyph = type */
.vm {
  width: 26px; height: 26px; line-height: 21px;
  background: #fff; border: 3px solid var(--unknown); border-radius: 50%;
  text-align: center; font-size: 13px;
  box-shadow: 0 1px 5px rgba(0,0,0,0.45);
}

.leg-sub {
  font-size: 11px; color: var(--muted); text-transform: uppercase;
  letter-spacing: .4px; margin: 10px 0 4px;
}
.leg-sub:first-of-type { margin-top: 0; }
.cmp-modes { display: flex; flex-wrap: wrap; gap: 7px; }

@media (max-width: 720px) {
  main { flex-direction: column; }
  #panel { width: 100%; border-left: none; border-top: 1px solid var(--line); }
  #map { min-height: 50vh; }
}

/* City-vs-network scope toggle (sidebar and compare panel). */
.scope-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: var(--muted);
  margin: 6px 0 8px;
  cursor: pointer;
}
.scope-toggle input { accent-color: var(--accent); cursor: pointer; }

/* City picker — progressive disclosure: chips + typeahead + drawer + Near-me */
.city-picker {
  display: flex; align-items: center; gap: 12px;
  padding: 10px 20px;
  background: var(--panel); border-bottom: 1px solid var(--line);
  flex-wrap: wrap;
  /* Establish a stacking context above the Leaflet map (its panes reach
     z-index ~700) so the dropdown panel paints over the map, not behind it. */
  position: relative; z-index: 1200;
}
.city-picker-label {
  font-size: 12px; text-transform: uppercase; letter-spacing: .5px;
  color: var(--muted); font-weight: 600;
}
.citysel { position: relative; min-width: 230px; }
.citysel-btn {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  width: 100%; background: var(--bg); color: var(--text);
  border: 1px solid var(--line); border-radius: 8px;
  padding: 8px 12px; font-size: 14px; font-weight: 600; font-family: inherit;
  cursor: pointer; transition: border-color .12s;
}
.citysel-btn:hover, .citysel-btn[aria-expanded="true"] { border-color: var(--accent); }
.citysel-caret { color: var(--muted); flex-shrink: 0; transition: transform .15s; }
.citysel-btn[aria-expanded="true"] .citysel-caret { transform: rotate(180deg); }
.citysel-pop {
  position: absolute; top: calc(100% + 4px); left: 0; right: 0; z-index: 1300;
  background: var(--panel); border: 1px solid var(--line); border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0,0,0,0.18); overflow: hidden;
}
.citysel-search {
  width: 100%; box-sizing: border-box; border: none;
  border-bottom: 1px solid var(--line); background: transparent; color: var(--text);
  padding: 10px 12px; font-size: 13px; font-family: inherit;
}
.citysel-search:focus { outline: none; }
.citysel-list { list-style: none; margin: 0; padding: 4px; max-height: 340px; overflow-y: auto; }
.citysel-opt {
  display: flex; align-items: baseline; justify-content: space-between; gap: 10px;
  padding: 8px 10px; border-radius: 6px; cursor: pointer; transition: background .1s;
}
.citysel-opt:hover, .citysel-opt:focus { background: var(--bg); outline: none; }
.citysel-opt.active { background: color-mix(in srgb, var(--accent) 16%, transparent); }
.citysel-opt.active .citysel-opt-name { color: var(--accent); }
.citysel-opt-name { font-size: 13px; font-weight: 600; color: var(--text); }
.citysel-opt-meta { font-size: 11px; color: var(--muted); white-space: nowrap; }
.citysel-empty { padding: 12px; font-size: 12px; color: var(--muted); text-align: center; list-style: none; }

.city-picker-count {
  font-size: 12px; color: var(--muted);
}

.citysel-btn:disabled { opacity: .55; cursor: default; }
