:root {
  --bg: #fbfaf7;
  --panel: #ffffff;
  --panel-2: #f3f1ec;
  --fg: #1d232a;
  --muted: #69707a;
  --line: #e2ded6;
  --ocean: #d9e4ec;
  --land: #f1ede4;
  --land-border: #b8b0a2;
  --nounit: #e4e1da;
  --accent: #0072B2;
  --danger: #b3261e;
  --ok: #1b7f4b;
  --both: #6a51a3;
  --shadow: 0 6px 24px rgba(20, 24, 30, .14);
  --radius: 10px;
  --top-h: 56px;
  --side-w: 380px;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #12161b; --panel: #1a1f26; --panel-2: #222831; --fg: #e8eaed; --muted: #9aa3ad; --line: #2e3540;
    --ocean: #0d1821; --land: #2a3038; --land-border: #4a525d; --nounit: #22272e; --both: #9e86e0;
    --danger: #ff8a80; --ok: #6fd39a; --shadow: 0 6px 24px rgba(0, 0, 0, .5); color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #12161b; --panel: #1a1f26; --panel-2: #222831; --fg: #e8eaed; --muted: #9aa3ad; --line: #2e3540;
  --ocean: #0d1821; --land: #2a3038; --land-border: #4a525d; --nounit: #22272e; --both: #9e86e0;
  --danger: #ff8a80; --ok: #6fd39a; --shadow: 0 6px 24px rgba(0, 0, 0, .5); color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--fg);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
.muted { color: var(--muted); }
.small { font-size: 13px; }
.hidden { display: none !important; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

.btn {
  display: inline-flex; align-items: center; gap: 6px; min-height: 36px; padding: 6px 12px;
  border: 1px solid var(--line); border-radius: 8px; background: var(--panel); color: var(--fg);
  text-decoration: none; white-space: nowrap;
}
.btn:hover { background: var(--panel-2); }
.btn.primary { background: var(--fg); color: var(--bg); border-color: var(--fg); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.danger { color: var(--danger); }
.btn:disabled { opacity: .5; cursor: default; }
input[type=text], input[type=search], input:not([type]), select, textarea {
  border: 1px solid var(--line); border-radius: 8px; background: var(--panel); padding: 7px 10px; min-height: 36px;
}
textarea { width: 100%; min-height: 70px; resize: vertical; }
input:focus-visible, select:focus-visible, textarea:focus-visible, button:focus-visible, .btn:focus-visible {
  outline: 2px solid var(--accent); outline-offset: 1px;
}

/* ---------- app shell ---------- */
.app { position: fixed; inset: 0; display: grid; grid-template-rows: var(--top-h) 1fr; }
.topbar {
  display: flex; align-items: center; gap: 8px; padding: 0 10px; background: var(--panel);
  border-bottom: 1px solid var(--line); z-index: 1000; min-width: 0;
}
.brand { font-weight: 700; letter-spacing: -.01em; text-decoration: none; color: var(--fg); margin-right: 4px; }
.counts { font-variant-numeric: tabular-nums; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.counts b { color: var(--fg); }
.spacer { flex: 1; }
.dropdown { position: relative; }
.menu {
  position: absolute; top: calc(100% + 6px); left: 0; min-width: 260px; max-height: 70vh; overflow: auto;
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 6px; z-index: 2000;
}
.menu.right { left: auto; right: 0; }
.menu-item {
  display: flex; width: 100%; justify-content: space-between; gap: 12px; text-align: left; padding: 8px 10px;
  border: 0; background: none; border-radius: 6px; text-decoration: none; color: var(--fg);
}
.menu-item:hover, .menu-item[aria-selected=true] { background: var(--panel-2); }
.menu-item .n { color: var(--muted); font-variant-numeric: tabular-nums; }
.menu-sep { height: 1px; background: var(--line); margin: 6px 0; }
.menu-foot { padding: 8px 10px; font-size: 13px; color: var(--muted); }
.badge {
  display: inline-grid; place-items: center; min-width: 20px; height: 20px; padding: 0 6px; border-radius: 10px;
  background: #d97706; color: #fff; font-size: 12px; font-weight: 600;
}
.swatch { width: 14px; height: 14px; border-radius: 50%; display: inline-block; vertical-align: -2px; }

.search { position: relative; flex: 0 1 280px; min-width: 120px; }
.search input { width: 100%; }
.search .menu { width: 100%; min-width: 300px; }
.result .sub { display: block; font-size: 12px; color: var(--muted); }

.main { position: relative; overflow: hidden; }
#map { position: absolute; inset: 0; background: var(--ocean); }
.leaflet-container { background: var(--ocean); font: inherit; }
.leaflet-control-attribution { font-size: 11px; background: color-mix(in srgb, var(--panel) 85%, transparent) !important; color: var(--muted); }
.leaflet-control-attribution a { color: var(--muted); }
.leaflet-bar a { background: var(--panel); color: var(--fg); border-color: var(--line); }

/* ---------- sidebar ---------- */
.sidebar {
  position: absolute; top: 0; right: 0; bottom: 0; width: var(--side-w); background: var(--panel);
  border-left: 1px solid var(--line); z-index: 900; display: flex; flex-direction: column;
  transition: transform .2s ease;
}
.sidebar.collapsed { transform: translateX(100%); }
.side-toggle {
  position: absolute; top: 12px; right: calc(var(--side-w) + 10px); z-index: 901; transition: right .2s ease;
  background: var(--panel); box-shadow: var(--shadow);
}
.sidebar.collapsed + .side-toggle { right: 10px; }
.sheet-handle { display: none; }
.tabs { display: flex; gap: 2px; padding: 8px 8px 0; border-bottom: 1px solid var(--line); }
.tab { border: 0; background: none; padding: 8px 12px; border-bottom: 2px solid transparent; color: var(--muted); }
.tab[aria-selected=true] { color: var(--fg); border-bottom-color: var(--fg); }
.side-body { overflow: auto; padding: 14px 16px 40px; flex: 1; }
.side-body h2 { margin: 0 0 2px; font-size: 22px; letter-spacing: -.01em; }
.side-body h3 { margin: 20px 0 8px; font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); }
.chip {
  display: inline-flex; align-items: center; gap: 4px; padding: 3px 9px; border-radius: 999px; font-size: 13px;
  background: var(--panel-2); border: 1px solid var(--line); color: var(--fg);
}
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
button.chip { min-height: 32px; }
button.chip[aria-pressed=true] { background: var(--fg); color: var(--bg); border-color: var(--fg); }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 9px; overflow: hidden; }
.seg button { border: 0; background: var(--panel); padding: 8px 14px; min-height: 40px; border-right: 1px solid var(--line); }
.seg button:last-child { border-right: 0; }
.seg button[aria-pressed=true] { background: var(--fg); color: var(--bg); }
.note-box { padding: 10px 12px; border-radius: 8px; background: var(--panel-2); font-size: 14px; margin: 10px 0; }
.note-box.warn { background: color-mix(in srgb, #d97706 14%, var(--panel)); }
.rows { list-style: none; margin: 0; padding: 0; }
.rows li { display: flex; align-items: center; gap: 8px; padding: 7px 4px; border-bottom: 1px solid var(--line); }
.rows li:last-child { border-bottom: 0; }
.rows li button.link, button.link { border: 0; background: none; padding: 0; text-align: left; color: var(--fg); flex: 1; min-height: 28px; }
.rows li button.link:hover, button.link:hover { text-decoration: underline; }
.dot { width: 10px; height: 10px; border-radius: 50%; flex: none; border: 1px solid var(--land-border); background: var(--land); }
.dot.visited { background: var(--accent); border-color: var(--accent); }
.dot.transit { background: repeating-linear-gradient(45deg, var(--accent) 0 2px, transparent 2px 4px); border-color: var(--accent); }
.dot.assumed { outline: 1.5px dotted var(--fg); outline-offset: 1px; }
.progress { height: 6px; background: var(--panel-2); border-radius: 3px; overflow: hidden; }
.progress > div { height: 100%; background: var(--accent); }
.region-head { display: flex; justify-content: space-between; align-items: baseline; margin-top: 14px; font-weight: 600; }
.region-head .n { font-weight: 400; color: var(--muted); font-variant-numeric: tabular-nums; }
.saved { font-size: 12px; color: var(--muted); min-height: 18px; }
.saved.err { color: var(--danger); }
.field { margin: 8px 0; }
.inline { display: flex; gap: 8px; align-items: center; }
.inline > input { flex: 1; min-width: 0; }
.legend { display: flex; flex-wrap: wrap; gap: 10px; font-size: 12px; color: var(--muted); }
.legend span { display: inline-flex; align-items: center; gap: 5px; }
.legend i { width: 14px; height: 10px; display: inline-block; border: 1px solid var(--land-border); }
.map-legend {
  position: absolute; left: 10px; bottom: 26px; z-index: 800; background: var(--panel); padding: 8px 10px;
  border-radius: 8px; box-shadow: var(--shadow); max-width: calc(100% - 20px);
}

/* place markers */
.pm { border-radius: 50%; border: 2px solid var(--panel); box-shadow: 0 0 0 1px rgba(0, 0, 0, .35); background: var(--accent); }
.pm.transit { background: var(--panel); border-color: var(--accent); box-shadow: none; }
.pm.sel { box-shadow: 0 0 0 3px var(--fg); }
.marker-cluster div { font: 600 12px system-ui, sans-serif; }

/* context menu */
.ctx { position: absolute; z-index: 3000; min-width: 210px; }

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); z-index: 5000; background: var(--fg);
  color: var(--bg); padding: 10px 16px; border-radius: 8px; box-shadow: var(--shadow); max-width: 90vw;
}
.toast.err { background: var(--danger); color: #fff; }

/* compare */
.people-picker { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.person { display: inline-flex; align-items: center; gap: 6px; padding: 4px 10px 4px 6px; border-radius: 999px; border: 1px solid var(--line); background: var(--panel); min-height: 36px; }
.person[aria-pressed=true] { border-color: var(--fg); box-shadow: inset 0 0 0 1px var(--fg); }
#venn { width: 100%; min-height: 220px; }
#venn .venn-circle path { fill-opacity: .35; stroke-width: 2; }
#venn .venn-intersection path { cursor: pointer; }
#venn .venn-circle, #venn .venn-intersection { cursor: pointer; }
#venn text { fill: var(--fg) !important; font-size: 13px; }
.bar-row { display: grid; grid-template-columns: 110px 1fr 34px; gap: 8px; align-items: center; font-size: 13px; margin: 3px 0; }
.bar { height: 10px; border-radius: 3px; }
.stack { display: flex; height: 14px; border-radius: 3px; overflow: hidden; background: var(--panel-2); }
.stack > div { height: 100%; }

/* ---------- plain pages ---------- */
.page { max-width: 980px; margin: 0 auto; padding: 24px 16px 60px; }
.page header.nav { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; margin-bottom: 18px; }
.page h1 { font-size: 28px; letter-spacing: -.02em; margin: 8px 0 16px; }
.card { background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px; margin: 14px 0; }
table { width: 100%; border-collapse: collapse; }
th, td { text-align: left; padding: 9px 8px; border-bottom: 1px solid var(--line); vertical-align: top; }
th { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); font-weight: 600; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }
.flash { padding: 10px 14px; border-radius: 8px; background: var(--panel-2); margin: 10px 0; word-break: break-all; }
.auth { max-width: 420px; margin: 8vh auto; padding: 0 16px; }
.auth .card { padding: 24px; }
.auth label { display: block; margin: 12px 0 4px; font-weight: 600; font-size: 14px; }
.auth input { width: 100%; }
.code-input { font-size: 22px; letter-spacing: .3em; text-align: center; font-variant-numeric: tabular-nums; }
.qr { display: grid; place-items: center; background: #fff; border-radius: 8px; padding: 8px; margin: 12px 0; }
.qr svg { width: 220px; height: 220px; }
pre.log { white-space: pre-wrap; font-size: 12px; background: var(--panel-2); padding: 10px; border-radius: 8px; max-height: 360px; overflow: auto; }
footer.attrib { font-size: 12px; color: var(--muted); margin-top: 30px; }

/* ---------- mobile ---------- */
@media (max-width: 767px) {
  :root { --top-h: 52px; }
  .brand, .counts.wide { display: none; }
  .topbar { gap: 6px; padding: 0 6px; }
  .topbar .btn { padding: 6px 9px; min-height: 44px; }
  .search { flex: 1 1 auto; }
  .search input { min-height: 44px; }
  .side-toggle { display: none; }
  .sidebar {
    top: auto; left: 0; width: 100%; height: 92vh; border-left: 0; border-top: 1px solid var(--line);
    border-radius: 16px 16px 0 0; box-shadow: var(--shadow); transform: translateY(calc(92vh - 96px)); z-index: 1100;
  }
  .sidebar.collapsed { transform: translateY(calc(92vh - 96px)); }
  .sidebar[data-sheet=half] { transform: translateY(42vh); }
  .sidebar[data-sheet=full] { transform: translateY(0); }
  .sheet-handle { display: block; width: 100%; height: 26px; border: 0; background: none; position: relative; touch-action: none; }
  .sheet-handle::after { content: ""; position: absolute; left: 50%; top: 10px; width: 44px; height: 5px; margin-left: -22px; border-radius: 3px; background: var(--line); }
  .map-legend { bottom: 106px; }
  .rows li { min-height: 44px; }
  .seg button { min-height: 44px; }
  button.chip { min-height: 44px; padding: 4px 12px; }
  .menu { position: fixed; left: 8px !important; right: 8px !important; top: calc(var(--top-h) + 4px); min-width: 0; }
  .leaflet-control-attribution { display: none; }
}
