:root {
  color-scheme: light;
  --page: #f9f9f7;
  --surface: #fcfcfb;
  --surface-2: #f2f1ed;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #898781;
  --grid: #e1e0d9;
  --axis: #c3c2b7;
  --border: rgba(11, 11, 11, 0.10);
  --accent: #2a78d6;
  --accent-ink: #ffffff;
  --series-1: #2a78d6;
  --series-2: #eb6834;
  --band: rgba(42, 120, 214, 0.14);
  --ok: #0ca30c;
  --ok-text: #006300;
  --warn: #fab219;
  --err: #d03b3b;
  --err-bg: rgba(208, 59, 59, 0.08);
  --pin-a: #2a78d6;
  --pin-b: #6250d6;
  --shadow: 0 1px 2px rgba(0, 0, 0, 0.04), 0 4px 16px rgba(0, 0, 0, 0.05);
  --radius: 12px;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --page: #0d0d0d;
    --surface: #1a1a19;
    --surface-2: #232321;
    --ink: #f0efec;
    --ink-2: #c3c2b7;
    --muted: #898781;
    --grid: #2c2c2a;
    --axis: #383835;
    --border: rgba(255, 255, 255, 0.10);
    --accent: #3987e5;
    --series-1: #3987e5;
    --series-2: #d95926;
    --band: rgba(57, 135, 229, 0.20);
    --ok-text: #0ca30c;
    --err: #e66767;
    --err-bg: rgba(230, 103, 103, 0.12);
    --pin-a: #3987e5;
    --pin-b: #9085e9;
    --shadow: none;
  }
}
:root[data-theme="dark"] {
  color-scheme: dark;
  --page: #0d0d0d; --surface: #1a1a19; --surface-2: #232321; --ink: #f0efec; --ink-2: #c3c2b7; --muted: #898781;
  --grid: #2c2c2a; --axis: #383835; --border: rgba(255, 255, 255, 0.10); --accent: #3987e5; --series-1: #3987e5;
  --series-2: #d95926; --band: rgba(57, 135, 229, 0.20); --ok-text: #0ca30c; --err: #e66767; --err-bg: rgba(230, 103, 103, 0.12);
  --pin-a: #3987e5; --pin-b: #9085e9; --shadow: none;
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; }
body { background: var(--page); color: var(--ink); font-size: 15px; line-height: 1.45; }
h1, h2, h3 { margin: 0; line-height: 1.25; }
h1 { font-size: 22px; font-weight: 650; }
h2 { font-size: 16px; font-weight: 650; }
h3 { font-size: 15px; font-weight: 650; }
a { color: var(--accent); }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.err { color: var(--err); }
.ok { color: var(--ok-text); }
.pad { padding: 8px 12px; }
input, select, button { font: inherit; color: inherit; }
input[type="text"], input[type="password"], input[type="number"], input[type="time"], input:not([type]), select {
  background: var(--surface); border: 1px solid var(--axis); border-radius: 8px; padding: 6px 9px; min-height: 34px;
}
input:focus-visible, select:focus-visible, button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }

/* ---------- buttons ---------- */
.btn {
  display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--axis); background: var(--surface);
  border-radius: 8px; padding: 7px 14px; cursor: pointer; font-weight: 550; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: 0.5; cursor: default; }
.btn.small { padding: 5px 11px; font-size: 13px; }
.btn.primary { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.btn.primary:hover { filter: brightness(1.08); }
.btn.ghost { background: transparent; border-color: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.link { background: none; border: none; color: var(--accent); padding: 0; text-decoration: underline; }
.btn.danger { color: var(--err); }
.icon-btn { background: none; border: none; font-size: 18px; cursor: pointer; color: var(--muted); padding: 4px 8px; border-radius: 6px; }
.icon-btn:hover { background: var(--surface-2); }
.btn-row, .row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.file-btn { cursor: pointer; }

/* ---------- top bar ---------- */
.topbar {
  position: sticky; top: 0; z-index: 1000; display: flex; align-items: center; gap: 16px; padding: 10px 16px;
  background: var(--surface); border-bottom: 1px solid var(--border);
}
.brand { display: flex; align-items: center; gap: 8px; font-weight: 700; font-size: 16px; }
.collector { display: flex; align-items: center; gap: 10px; flex: 1; min-width: 0; }
.top-actions { display: flex; align-items: center; gap: 6px; }
.pill { display: inline-flex; align-items: center; gap: 7px; padding: 3px 10px; border-radius: 999px; font-size: 13px; font-weight: 600; background: var(--surface-2); white-space: nowrap; }
.pill i { width: 8px; height: 8px; border-radius: 50%; background: var(--muted); }
.pill-ok i { background: var(--ok); animation: pulse 2s infinite; }
.pill-paused i, .pill-follower i { background: var(--warn); }
.pill-error, .pill-nokey { background: var(--err-bg); color: var(--err); }
.pill-error i, .pill-nokey i { background: var(--err); }
@keyframes pulse { 50% { opacity: 0.35; } }

/* ---------- layout ---------- */
.layout { display: grid; grid-template-columns: 270px 1fr; height: calc(100vh - 53px); }
.sidebar { border-right: 1px solid var(--border); background: var(--surface); overflow-y: auto; display: flex; flex-direction: column; }
.side-head { display: flex; justify-content: space-between; align-items: center; padding: 14px 14px 8px; }
.side-foot { margin-top: auto; padding: 12px 14px; border-top: 1px solid var(--border); }
.route-list { list-style: none; margin: 0; padding: 4px 8px; }
.route-item {
  width: 100%; text-align: left; background: none; border: 1px solid transparent; border-radius: 10px; padding: 9px 10px;
  cursor: pointer; display: flex; flex-direction: column; gap: 2px; margin-bottom: 2px;
}
.route-item:hover { background: var(--surface-2); }
.route-item.active { background: var(--surface-2); border-color: var(--border); }
.ri-name { font-weight: 600; }
.ri-row { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.ri-latest { font-size: 18px; font-weight: 650; }
.spark polyline { fill: none; stroke: var(--series-1); stroke-width: 1.5; stroke-linejoin: round; stroke-linecap: round; }
.spark circle { fill: var(--series-1); }
.main { overflow-y: auto; padding: 22px 28px 60px; }

/* ---------- route header ---------- */
.route-head { display: flex; justify-content: space-between; gap: 16px; align-items: flex-start; flex-wrap: wrap; }
.route-head .ab { margin: 6px 0 2px; display: flex; align-items: center; gap: 6px; flex-wrap: wrap; color: var(--ink-2); }
.arrow { color: var(--muted); }
.route-actions { display: flex; gap: 6px; }
.pin-dot { display: inline-grid; place-items: center; width: 20px; height: 20px; border-radius: 50%; color: #fff; font-size: 11px; font-weight: 700; flex: none; }
.pin-dot.pin-a { background: var(--pin-a); }
.pin-dot.pin-b { background: var(--pin-b); }
.latest { display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; margin: 14px 0 4px; padding: 10px 14px; background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); }
.latest-label { font-size: 12px; text-transform: uppercase; letter-spacing: 0.06em; color: var(--muted); font-weight: 600; }
.latest-value { font-size: 22px; font-weight: 650; }
.banner { margin: 10px 0; padding: 9px 14px; border-radius: 10px; font-size: 14px; }
.banner.err { background: var(--err-bg); }

/* ---------- tabs ---------- */
.tabs { display: flex; gap: 2px; box-shadow: inset 0 -1px 0 var(--border); margin: 18px 0 16px; overflow-x: auto; overflow-y: hidden; scrollbar-width: none; }
.tab { background: none; border: none; padding: 9px 14px; cursor: pointer; color: var(--ink-2); font-weight: 550; border-bottom: 2px solid transparent; white-space: nowrap; }
.tab:hover { color: var(--ink); }
.tab.active { color: var(--ink); border-bottom-color: var(--accent); }

/* ---------- cards & kpis ---------- */
.card { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 16px 18px; margin-bottom: 16px; box-shadow: var(--shadow); min-width: 0; }
.card > h3 + p { margin-top: 4px; }
.card-head { display: flex; justify-content: space-between; align-items: center; gap: 12px; flex-wrap: wrap; margin-bottom: 6px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 16px; }
.filter-bar { display: flex; align-items: center; gap: 12px; margin-bottom: 14px; flex-wrap: wrap; }
.filter-bar label { display: flex; align-items: center; gap: 6px; }
.kpis { display: grid; grid-template-columns: repeat(auto-fill, minmax(165px, 1fr)); gap: 12px; margin-bottom: 16px; }
.kpi { background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); padding: 12px 14px; box-shadow: var(--shadow); }
.kpi-label { font-size: 13px; color: var(--ink-2); }
.kpi-value { font-size: 26px; font-weight: 650; margin: 2px 0; }
.kpi-sub { font-size: 12px; color: var(--muted); }
.empty-state { text-align: left; }
.empty-state h3 { margin-bottom: 6px; }
.empty { color: var(--muted); padding: 20px 0; }

/* ---------- plan ---------- */
.plan-controls { display: flex; gap: 8px; flex-wrap: wrap; margin: 10px 0 12px; }
.seg { display: inline-flex; border: 1px solid var(--axis); border-radius: 8px; overflow: hidden; }
.seg button { border: none; background: var(--surface); padding: 6px 12px; cursor: pointer; }
.seg button.on { background: var(--accent); color: var(--accent-ink); }
.plan-big { font-size: 22px; font-weight: 650; }
.plan-big .muted { font-size: 15px; font-weight: 400; }
.plan-list { margin: 8px 0; padding-left: 20px; color: var(--ink-2); }
.plan-list li { margin: 3px 0; }

/* ---------- charts ---------- */
.chart-wrap { width: 100%; min-height: 120px; }
svg.chart { display: block; overflow: visible; touch-action: pan-y; }
.chart .grid { stroke: var(--grid); stroke-width: 1; }
.chart .axis-base { stroke: var(--axis); stroke-width: 1; }
.chart .tick { fill: var(--muted); font-size: 11px; font-variant-numeric: tabular-nums; }
.chart .line { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart .line.thin { stroke-width: 1.5; }
.chart .s1 { stroke: var(--series-1); }
.chart .s2 { stroke: var(--series-2); }
.chart .dot { stroke: var(--surface); stroke-width: 2; }
.chart .dot.s1 { fill: var(--series-1); }
.chart .dot.s2 { fill: var(--series-2); }
.chart .band { fill: var(--band); }
.chart .ref-line { stroke: var(--muted); stroke-width: 1; }
.chart .ref-label { fill: var(--muted); font-size: 11px; }
.chart .crosshair { stroke: var(--axis); stroke-width: 1; }
.legend { display: flex; gap: 16px; flex-wrap: wrap; font-size: 13px; color: var(--ink-2); margin: 6px 0 10px; }
.legend span { display: inline-flex; align-items: center; gap: 6px; }
.key-line { display: inline-block; width: 16px; height: 3px; border-radius: 2px; }
.key-line.s1 { background: var(--series-1); }
.key-line.s2 { background: var(--series-2); }
.key-band { display: inline-block; width: 16px; height: 10px; border-radius: 2px; background: var(--band); }
.key-ref { display: inline-block; width: 16px; height: 1px; background: var(--muted); }
.chart-tip {
  position: fixed; z-index: 3000; display: none; pointer-events: none; background: var(--surface); color: var(--ink);
  border: 1px solid var(--border); border-radius: 10px; padding: 8px 10px; font-size: 13px; box-shadow: 0 6px 24px rgba(0, 0, 0, 0.18); min-width: 180px;
}
.tip-title { font-weight: 650; margin-bottom: 4px; }
.tip-row { display: grid; grid-template-columns: 12px 1fr auto; gap: 8px; align-items: center; color: var(--ink-2); }
.tip-row b { color: var(--ink); font-variant-numeric: tabular-nums; }
.tip-foot { color: var(--muted); font-size: 12px; margin-top: 4px; }
.sw { width: 10px; height: 10px; border-radius: 2px; display: inline-block; }
.sw.s1 { background: var(--series-1); }
.sw.s2 { background: var(--series-2); }
.sw.band { background: var(--band); }

/* heatmap */
.heat { display: grid; gap: 2px; margin-top: 10px; }
.heat-x { font-size: 11px; color: var(--muted); white-space: nowrap; overflow: visible; height: 16px; font-variant-numeric: tabular-nums; }
.heat-y { font-size: 12px; color: var(--ink-2); display: flex; align-items: center; }
.heat-cell { height: 28px; border-radius: 3px; cursor: default; }
.heat-cell.empty-cell { background: var(--surface-2); }
.heat-legend { display: flex; align-items: center; gap: 8px; margin-top: 10px; font-size: 12px; color: var(--ink-2); flex-wrap: wrap; }
.heat-ramp { width: 140px; height: 10px; border-radius: 3px; }

/* lists */
.times { margin: 10px 0 0; padding: 0; list-style: none; }
.times li { display: grid; grid-template-columns: 7.5em 1fr; gap: 10px; padding: 6px 0; border-bottom: 1px solid var(--grid); }
.times li:last-child { border-bottom: none; }
.times b { font-variant-numeric: tabular-nums; }
.tbl-wrap { overflow-x: auto; max-height: 520px; overflow-y: auto; }
.tbl { width: 100%; border-collapse: collapse; font-size: 14px; margin-top: 8px; }
.tbl th { text-align: left; font-weight: 600; color: var(--ink-2); border-bottom: 1px solid var(--axis); padding: 6px 8px; position: sticky; top: 0; background: var(--surface); }
.tbl td { padding: 6px 8px; border-bottom: 1px solid var(--grid); }
.tbl .num { text-align: right; font-variant-numeric: tabular-nums; }

/* hotspots */
.filter-bar.wrap { row-gap: 8px; }
.chips { display: inline-flex; gap: 6px; flex-wrap: wrap; }
.chip { border: 1px solid var(--axis); background: var(--surface); border-radius: 999px; padding: 4px 11px; font-size: 13px; cursor: pointer; }
.chip.on { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.hot-layout { display: grid; grid-template-columns: minmax(0, 3fr) minmax(280px, 2fr); gap: 16px; align-items: start; }
.map-card { padding: 0; overflow: hidden; }
.hot-map { height: 480px; }
.map-legend { padding: 10px 14px; margin: 0; }
.stretches { list-style: none; padding: 0; margin: 8px 0 0; }
.stretches li { display: flex; gap: 10px; padding: 9px 6px; border-bottom: 1px solid var(--grid); cursor: pointer; border-radius: 8px; }
.stretches li:hover { background: var(--surface-2); }
.stretches .num { flex: none; width: 24px; height: 24px; border-radius: 50%; background: var(--ink); color: var(--surface); display: grid; place-items: center; font-size: 12px; font-weight: 700; }
.st-name { font-weight: 600; }
.st-meta { font-size: 13px; }

/* map pins */
.pin span { display: grid; place-items: center; width: 100%; height: 100%; border-radius: 50%; color: #fff; font-weight: 700; font-size: 13px; border: 2px solid #fff; box-shadow: 0 1px 4px rgba(0, 0, 0, 0.35); }
.pin-a span { background: var(--pin-a); }
.pin-b span { background: var(--pin-b); }
.pin-hot span { background: #0b0b0b; font-size: 11px; }
.leaflet-container { font: inherit; background: var(--surface-2); }

/* switch */
.switch { display: inline-flex; align-items: center; gap: 8px; cursor: pointer; font-size: 14px; }
.check { display: flex; align-items: center; gap: 8px; margin: 6px 0; cursor: pointer; }

/* ---------- dialogs ---------- */
.dlg { border: none; border-radius: 16px; padding: 0; background: var(--surface); color: var(--ink); width: min(560px, calc(100vw - 32px)); max-height: calc(100vh - 32px); box-shadow: 0 20px 60px rgba(0, 0, 0, 0.3); }
.dlg.wide { width: min(1080px, calc(100vw - 32px)); }
.dlg::backdrop { background: rgba(0, 0, 0, 0.45); }
.dlg form { display: flex; flex-direction: column; max-height: calc(100vh - 32px); }
.dlg-head { display: flex; justify-content: space-between; align-items: center; padding: 14px 18px; border-bottom: 1px solid var(--border); }
.dlg-body { padding: 16px 18px; overflow-y: auto; }
.dlg-foot { display: flex; justify-content: flex-end; gap: 8px; padding: 12px 18px; border-top: 1px solid var(--border); }
.field { display: flex; flex-direction: column; gap: 4px; font-size: 14px; font-weight: 550; margin-bottom: 12px; }
.inline-field { flex-direction: row; align-items: center; gap: 8px; font-weight: 400; }
.route-editor { display: grid; grid-template-columns: 360px 1fr; gap: 18px; }
.editor-map { min-height: 460px; border-radius: 12px; border: 1px solid var(--border); }
.place { margin-bottom: 12px; }
.place-label { display: flex; align-items: center; gap: 8px; font-weight: 550; font-size: 14px; margin-bottom: 4px; }
.search-row { display: flex; gap: 6px; }
.search-row input { flex: 1; min-width: 0; }
.results { list-style: none; margin: 4px 0 0; padding: 0; }
.results:empty { display: none; }
.results li button { width: 100%; text-align: left; background: var(--surface-2); border: none; padding: 6px 9px; border-radius: 6px; margin-top: 3px; cursor: pointer; font-size: 13px; }
.results li button:hover { background: var(--grid); }
.chosen { margin-top: 5px; }
.chosen.set { color: var(--ok-text); }
.hint { color: var(--ink-2); }
label.inline { display: inline-flex; align-items: center; gap: 3px; margin: 0 4px; }
.schedule { border: 1px solid var(--border); border-radius: 10px; padding: 10px 14px; margin: 8px 0; }
.schedule legend { font-weight: 600; font-size: 14px; padding: 0 4px; }
.schedule .field { margin-bottom: 8px; }
.days { display: flex; gap: 4px; flex-wrap: wrap; margin: 4px 0 8px; }
.day input { position: absolute; opacity: 0; pointer-events: none; }
.day span { display: inline-block; padding: 4px 9px; border: 1px solid var(--axis); border-radius: 999px; font-size: 13px; cursor: pointer; user-select: none; }
.day input:checked + span { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.day input:focus-visible + span { outline: 2px solid var(--accent); outline-offset: 1px; }
.estimate { margin: 6px 0 0; color: var(--ink-2); }
.set-section { margin-bottom: 18px; }
.set-section h3 { margin-bottom: 4px; }
.set-section p { margin: 4px 0 8px; }

/* ---------- welcome ---------- */
.welcome { max-width: 680px; padding-top: 20px; }
.welcome h1 { font-size: 28px; margin-bottom: 10px; }
.welcome > p { color: var(--ink-2); font-size: 16px; }
.steps { padding-left: 22px; margin: 22px 0; }
.steps li { margin-bottom: 16px; }
.steps li .btn { margin-left: 6px; }
.steps li.done { color: var(--muted); }

/* ---------- toasts ---------- */
.toasts { position: fixed; bottom: 16px; right: 16px; z-index: 4000; display: flex; flex-direction: column; gap: 8px; align-items: flex-end; }
.toast { background: var(--ink); color: var(--surface); padding: 10px 14px; border-radius: 10px; font-size: 14px; max-width: 360px; box-shadow: 0 6px 24px rgba(0, 0, 0, 0.2); transition: opacity 0.4s, transform 0.4s; }
.toast-error { background: var(--err); color: #fff; }
.toast.out { opacity: 0; transform: translateY(8px); }

/* ---------- small screens ---------- */
@media (max-width: 900px) {
  .hot-layout, .grid2 { grid-template-columns: 1fr; }
  .route-editor { grid-template-columns: 1fr; }
  .editor-map { min-height: 300px; }
}
@media (max-width: 720px) {
  .topbar { flex-wrap: wrap; gap: 8px; padding: 8px 16px; }
  .collector { order: 3; flex-basis: 100%; }
  .top-actions { margin-left: auto; }
  #usage { display: none; }
  .brand { cursor: pointer; }
  .brand::after { content: "▾ routes"; font-size: 12px; font-weight: 500; color: var(--muted); }
  .layout { display: block; height: auto; }
  .sidebar { display: none; border-right: none; border-bottom: 1px solid var(--border); }
  body.show-sidebar .sidebar { display: flex; }
  .main { padding: 16px 16px 60px; overflow: visible; }
  .kpis { grid-template-columns: 1fr 1fr; }
  .kpi-value { font-size: 22px; }
  .hot-map { height: 360px; }
}
