:root {
  color-scheme: light;
  --bg: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f0efec;
  --text: #0b0b0b;
  --text-2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.12);
  --accent: #2a78d6;
  --accent-ink: #1c5cab;
  --hover: rgba(11, 11, 11, 0.05);

  /* Stop categories (validated; shape + label are the secondary encoding) */
  --c-ok: #0ca30c;
  --c-warning: #eda100;
  --c-untagged: #2a78d6;
  --c-placeholder: #2a78d6;
  --c-missing: #d03b3b;
  --c-hail_ride: #898781;
  --c-inactive_tagged: #d55181;
  --c-inactive: #b5b3ab;
  --c-osm_unknown: #4a3aa7;
  --c-osm_only: #4a3aa7;
  --good-ink: #006300;
  --bad-ink: #b3261e;

  --radius: 10px;
  --font: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #242422;
    --text: #ffffff;
    --text-2: #c3c2b7;
    --muted: #898781;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.12);
    --accent: #3987e5;
    --accent-ink: #86b6ef;
    --hover: rgba(255, 255, 255, 0.06);
    --c-ok: #0ca30c;
    --c-warning: #fab219;
    --c-untagged: #3987e5;
    --c-missing: #e66767;
    --c-placeholder: #3987e5;
    --c-inactive_tagged: #d55181;
    --c-inactive: #6b6a65;
    --c-osm_unknown: #9085e9;
    --c-osm_only: #9085e9;
    --good-ink: #0ca30c;
    --bad-ink: #e66767;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0d0d0d;
  --surface: #1a1a19;
  --surface-2: #242422;
  --text: #ffffff;
  --text-2: #c3c2b7;
  --muted: #898781;
  --grid: #2c2c2a;
  --axis: #383835;
  --border: rgba(255, 255, 255, 0.12);
  --accent: #3987e5;
  --accent-ink: #86b6ef;
  --hover: rgba(255, 255, 255, 0.06);
  --c-warning: #fab219;
  --c-untagged: #3987e5;
  --c-missing: #e66767;
  --c-placeholder: #3987e5;
  --c-inactive: #6b6a65;
  --c-osm_unknown: #9085e9;
  --c-osm_only: #9085e9;
  --good-ink: #0ca30c;
  --bad-ink: #e66767;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  font: 14px/1.45 var(--font);
  background: var(--bg);
  color: var(--text);
  display: flex;
  flex-direction: column;
}
a { color: var(--accent-ink); }
button, select, input { font: inherit; color: inherit; }
body:not(.is-admin) .admin-only { display: none !important; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
.num { font-variant-numeric: tabular-nums; }
.muted { color: var(--muted); }
.small { font-size: 12px; }

/* ---------- Top bar ---------- */
.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 8px 16px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  flex-wrap: wrap;
  z-index: 1000;
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 650; color: var(--text); text-decoration: none; }
.region-picker select, .input, .btn {
  border: 1px solid var(--border);
  background: var(--surface);
  border-radius: 8px;
  padding: 6px 10px;
}
.tabs { display: flex; gap: 2px; background: var(--surface-2); padding: 3px; border-radius: 9px; }
.tabs a { padding: 4px 12px; border-radius: 7px; color: var(--text-2); text-decoration: none; }
.tabs a.active { background: var(--surface); color: var(--text); box-shadow: 0 1px 2px rgba(0,0,0,.12); }
.job-status { margin-left: auto; display: flex; align-items: center; gap: 8px; color: var(--text-2); font-size: 13px; }
.spinner { width: 14px; height: 14px; border: 2px solid var(--grid); border-top-color: var(--accent); border-radius: 50%; animation: spin 0.8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.btn { cursor: pointer; display: inline-flex; align-items: center; gap: 6px; text-decoration: none; color: var(--text); line-height: 1.3; }
.btn:hover { background: var(--hover); }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.small { padding: 3px 8px; font-size: 12px; }
.btn:disabled { opacity: .5; cursor: default; }
.link-btn { background: none; border: 0; padding: 0; color: var(--accent-ink); cursor: pointer; }

main { flex: 1; min-height: 0; display: flex; }
.view { flex: 1; min-height: 0; }
.view[hidden] { display: none !important; }

/* ---------- Map view ---------- */
#view-map { display: flex; }
.sidebar {
  width: 380px;
  flex-shrink: 0;
  background: var(--surface);
  border-right: 1px solid var(--border);
  overflow-y: auto;
}
.map-wrap { flex: 1; position: relative; min-width: 0; }
#map { position: absolute; inset: 0; background: var(--surface-2); }
.sb-section { padding: 14px 16px; border-bottom: 1px solid var(--border); }
.sb-section h2 { margin: 0 0 2px; font-size: 18px; }
.sb-section h3 { margin: 0 0 8px; font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--text-2); }
.meta-line { color: var(--text-2); font-size: 12px; }
.row { display: flex; align-items: center; gap: 8px; }
.row.between { justify-content: space-between; }
.wrap { flex-wrap: wrap; }

.cat-filters { display: grid; gap: 2px; }
.cat-filter {
  display: flex; align-items: center; gap: 10px;
  padding: 5px 8px; border-radius: 7px; cursor: pointer; user-select: none;
  border: 0; background: none; text-align: left; width: 100%;
}
.cat-filter:hover { background: var(--hover); }
.cat-filter .count { margin-left: auto; font-variant-numeric: tabular-nums; color: var(--text-2); }
.cat-filter.off { opacity: .45; }
.cat-filter.off .swatch { filter: grayscale(1); }

.swatch { width: 12px; height: 12px; border-radius: 50%; flex-shrink: 0; display: inline-block; }
.swatch.fill { background: var(--c); box-shadow: 0 0 0 1px rgba(0,0,0,.25) inset; }
.swatch.ring { border: 3px solid var(--c); background: transparent; }

.subtabs { display: flex; gap: 14px; padding: 0 16px; border-bottom: 1px solid var(--border); position: sticky; top: 0; background: var(--surface); z-index: 2; }
.subtabs button { background: none; border: 0; padding: 10px 0; color: var(--text-2); cursor: pointer; border-bottom: 2px solid transparent; }
.subtabs button.active { color: var(--text); border-bottom-color: var(--accent); }

.search { width: 100%; }
.list { list-style: none; margin: 0; padding: 4px 8px 16px; }
.list li { }
.list-item {
  display: flex; gap: 10px; align-items: flex-start; width: 100%;
  padding: 7px 8px; border-radius: 7px; border: 0; background: none; text-align: left; cursor: pointer;
}
.list-item:hover { background: var(--hover); }
.list-item .swatch { margin-top: 4px; }
.list-item .title { font-weight: 550; }
.list-item .sub { font-size: 12px; color: var(--text-2); }
.list-more { padding: 8px 16px; color: var(--muted); font-size: 12px; }

.route-badge {
  display: inline-flex; align-items: center; justify-content: center;
  min-width: 34px; padding: 1px 6px; border-radius: 5px; font-weight: 650; font-size: 12px;
  background: var(--surface-2); color: var(--text); border: 1px solid var(--border);
  font-variant-numeric: tabular-nums; white-space: nowrap;
}
.route-badge .dot { width: 8px; height: 8px; border-radius: 2px; margin-right: 5px; }

/* Stop detail */
.detail-head { display: flex; gap: 10px; align-items: flex-start; }
.badge {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 2px 8px; border-radius: 99px; font-size: 12px; background: var(--surface-2); color: var(--text);
}
.issues { margin: 10px 0 0; padding: 0; list-style: none; display: grid; gap: 4px; }
.issues li { display: flex; gap: 8px; font-size: 13px; }
.issues .icon { flex-shrink: 0; width: 18px; height: 18px; border-radius: 50%; display: inline-grid; place-items: center; font-size: 11px; font-weight: 700; color: #fff; }
.issues .icon.warn { background: #b87d00; }
.issues .icon.note { background: var(--muted); }
.compare { width: 100%; border-collapse: collapse; font-size: 13px; table-layout: fixed; }
.compare th, .compare td { text-align: left; padding: 5px 6px; border-bottom: 1px solid var(--grid); vertical-align: top; overflow-wrap: anywhere; }
.compare th { color: var(--text-2); font-weight: 500; width: 92px; }
.compare thead th { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.compare td.diff { background: color-mix(in srgb, var(--c-warning) 16%, transparent); }
.actions { display: flex; gap: 6px; flex-wrap: wrap; }
pre.tags {
  margin: 8px 0 0; padding: 8px 10px; background: var(--surface-2); border-radius: 8px;
  font: 12px/1.5 ui-monospace, SFMono-Regular, Menlo, monospace; white-space: pre-wrap; overflow-wrap: anywhere;
}
details summary { cursor: pointer; color: var(--text-2); }
.route-list { list-style: none; margin: 0; padding: 0; display: grid; gap: 2px; }
.route-item { display: flex; gap: 10px; align-items: flex-start; width: 100%; padding: 6px; border-radius: 7px; border: 0; background: none; text-align: left; cursor: pointer; }
.route-item:hover, .route-item.active { background: var(--hover); }
.route-item .sub { font-size: 12px; color: var(--text-2); }

.route-banner {
  position: absolute; top: 12px; left: 50%; transform: translateX(-50%); z-index: 800;
  background: var(--surface); border: 1px solid var(--border); border-radius: 10px;
  padding: 8px 12px; box-shadow: 0 4px 16px rgba(0,0,0,.18); display: flex; gap: 10px; align-items: center;
  max-width: calc(100% - 32px);
}
.map-legend {
  background: var(--surface); color: var(--text); padding: 8px 10px; border-radius: 8px;
  border: 1px solid var(--border); font-size: 12px; line-height: 1.7;
}
.leaflet-container { font: inherit; }
.leaflet-tooltip { font: 12px/1.35 var(--font); }

/* ---------- Pages (stats, regions) ---------- */
.page { overflow-y: auto; padding: 24px 16px 48px; }
.page-inner { max-width: 1180px; margin: 0 auto; }
.page h1 { font-size: 22px; margin: 0 0 4px; }
.page h2 { font-size: 16px; margin: 28px 0 10px; }
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px; }
.grid-tiles { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 12px; }
.tile .label { color: var(--text-2); font-size: 12px; }
.tile .value { font-size: 26px; font-weight: 650; margin-top: 2px; }
.tile .foot { font-size: 12px; color: var(--muted); }

.table-wrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface); }
table.data { border-collapse: collapse; width: 100%; font-size: 13px; }
table.data th, table.data td { padding: 8px 10px; text-align: right; border-bottom: 1px solid var(--grid); white-space: nowrap; font-variant-numeric: tabular-nums; }
table.data th:first-child, table.data td:first-child { text-align: left; }
table.data th { font-weight: 550; color: var(--text-2); cursor: pointer; user-select: none; position: sticky; top: 0; background: var(--surface); }
table.data th.sorted::after { content: " ▾"; }
table.data th.sorted.asc::after { content: " ▴"; }
table.data tbody tr { cursor: pointer; }
table.data tbody tr:hover { background: var(--hover); }
table.data tbody tr.selected { background: color-mix(in srgb, var(--accent) 10%, transparent); }
table.data td.bar-cell { width: 180px; min-width: 140px; }

.stackbar { display: flex; gap: 2px; height: 10px; width: 100%; }
.stackbar > span { height: 100%; background: var(--c); min-width: 0; }
/* Import placeholders share the blue family with "no code"; stripes tell them apart in bars. */
.stackbar > span.seg-placeholder { background: repeating-linear-gradient(135deg, var(--c) 0 3px, transparent 3px 6px); box-shadow: inset 0 0 0 1px var(--c); }
.stackbar > span:first-child { border-radius: 4px 0 0 4px; }
.stackbar > span:last-child { border-radius: 0 4px 4px 0; }
.stackbar > span:only-child { border-radius: 4px; }
.stackbar.big { height: 22px; }
.legend { display: flex; flex-wrap: wrap; gap: 6px 16px; font-size: 12px; color: var(--text-2); margin-top: 10px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }

.two-col { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }

/* Stats overview: full width; the areas table sits beside the map and scrolls on its own */
.page-wide { padding: 0 8px; }
.grid-tiles.compact .tile { padding: 12px 14px; }
.grid-tiles.compact .tile .value { font-size: 22px; }
.overview-grid { display: grid; grid-template-columns: minmax(360px, 5fr) minmax(0, 7fr); gap: 16px; margin-top: 16px; align-items: start; }
.overview-left { display: grid; gap: 16px; }
.overview-map { height: max(420px, calc(100vh - 370px)); margin-top: 10px; border-radius: 8px; overflow: hidden; background: var(--surface-2); }
.overview-areas { position: sticky; top: 0; min-width: 0; }
.areas-scroll { margin-top: 10px; max-height: max(420px, calc(100vh - 345px)); overflow: auto; }
.areas-scroll table.data th { z-index: 1; }
@media (max-width: 1100px) {
  .overview-grid { grid-template-columns: minmax(0, 1fr); }
  .overview-areas { position: static; }
  .overview-map { height: 440px; }
}
.chart { position: relative; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart .tip {
  position: absolute; pointer-events: none; background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; padding: 6px 8px; font-size: 12px; box-shadow: 0 4px 12px rgba(0,0,0,.15); white-space: nowrap;
}
.bars { display: grid; gap: 6px; }
.bars .bar-row { display: grid; grid-template-columns: minmax(90px, 180px) 1fr 64px; gap: 10px; align-items: center; font-size: 13px; }
.bars .bar-row .track { height: 10px; }
.bars .bar-row .fill { height: 100%; border-radius: 0 4px 4px 0; background: var(--accent); }
.bars .bar-row .name { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.bars .bar-row .v { text-align: right; font-variant-numeric: tabular-nums; color: var(--text-2); }
.bars .bar-row.clickable { cursor: pointer; border-radius: 6px; }
.bars .bar-row.clickable:hover { background: var(--hover); }

.area-groups { columns: 3 260px; column-gap: 24px; }
.area-group { break-inside: avoid; margin-bottom: 18px; }
.area-group h3 { font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--text-2); margin: 0 0 6px; }
.area-row { display: flex; align-items: center; gap: 8px; padding: 3px 6px; border-radius: 6px; }
.area-row:hover { background: var(--hover); }
.area-row .code { color: var(--muted); font-size: 12px; font-variant-numeric: tabular-nums; }
.area-row button { margin-left: auto; }
.error-text { color: var(--bad-ink); }
.empty { padding: 40px 16px; text-align: center; color: var(--text-2); }

#login-dialog { width: min(360px, calc(100% - 32px)); color: var(--text); padding: 20px; }
#login-dialog::backdrop { background: rgba(0, 0, 0, 0.35); }
.toast {
  position: fixed; bottom: 20px; left: 50%; transform: translateX(-50%); z-index: 2000;
  background: var(--text); color: var(--bg); padding: 8px 14px; border-radius: 8px; font-size: 13px;
  max-width: calc(100% - 32px);
}

@media (max-width: 800px) {
  #view-map { flex-direction: column-reverse; }
  .sidebar { width: auto; border-right: 0; border-top: 1px solid var(--border); flex: 1; min-height: 0; }
  .map-wrap { flex: 0 0 45vh; }
  .two-col { grid-template-columns: 1fr; }
  .topbar { gap: 8px 12px; }
  .job-status { margin-left: 0; width: 100%; }
}
.job-status[hidden] + #login-btn { margin-left: auto; }
.version { text-decoration: none; font-variant-numeric: tabular-nums; }
.version:hover { color: var(--text-2); }
.sources { display: grid; gap: 16px; margin-top: 16px; }
.source h2 a { color: var(--text); text-decoration: none; }
.source h2 a:hover { text-decoration: underline; }
.source .compare th { width: 110px; }
@media (max-width: 1000px) { .version { display: none; } }
