/* ---------- theme tokens ---------- */
:root {
  --bg: #f6f7f4; --surface: #ffffff; --surface-2: #f0f2ec; --text: #17201a; --muted: #5d6b61;
  --border: #dde2d8; --accent: #1f7a3f; --accent-ink: #ffffff; --shadow: 0 6px 24px rgba(20, 35, 25, .16);
  --radius: 14px; --tap: 44px;
  --c-street: #2f9e44; --c-park: #0b7285; --c-botanic: #7048e8; --c-user: #e8590c;
  --c-conservation: #1971c2; --c-heritage: #c2255c; --c-other: #868e96;
  --safe-t: env(safe-area-inset-top, 0px); --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px); --safe-r: env(safe-area-inset-right, 0px);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #0f1411; --surface: #18201b; --surface-2: #222c25; --text: #e7eee8; --muted: #9aa89e;
    --border: #2d3830; --accent: #51cf66; --accent-ink: #0b1a10; --shadow: 0 6px 24px rgba(0, 0, 0, .5);
    --c-street: #69db7c; --c-park: #3bc9db; --c-botanic: #b197fc; --c-user: #ffa94d;
    --c-conservation: #74c0fc; --c-heritage: #f783ac; --c-other: #adb5bd;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #0f1411; --surface: #18201b; --surface-2: #222c25; --text: #e7eee8; --muted: #9aa89e;
  --border: #2d3830; --accent: #51cf66; --accent-ink: #0b1a10; --shadow: 0 6px 24px rgba(0, 0, 0, .5);
  --c-street: #69db7c; --c-park: #3bc9db; --c-botanic: #b197fc; --c-user: #ffa94d;
  --c-conservation: #74c0fc; --c-heritage: #f783ac; --c-other: #adb5bd;
  color-scheme: dark;
}

/* ---------- base ---------- */
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; height: 100dvh; background: var(--bg); color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, sans-serif; overflow: hidden;
  -webkit-text-size-adjust: 100%; overscroll-behavior: none; }
#map { position: fixed; inset: 0; }
button { font: inherit; color: inherit; cursor: pointer; touch-action: manipulation; }
a { color: var(--accent); }
.muted { color: var(--muted); }
[hidden] { display: none !important; }
:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }

/* ---------- top bar ---------- */
.topbar { position: fixed; z-index: 5; display: flex; gap: 8px; align-items: flex-start; pointer-events: none;
  top: max(12px, var(--safe-t)); left: max(12px, var(--safe-l)); right: max(12px, var(--safe-r)); }
.topbar > * { pointer-events: auto; flex-shrink: 0; }
.topbar > .search { flex-shrink: 1; }
.search { position: relative; flex: 1 1 auto; min-width: 0; max-width: 420px; background: var(--surface); border-radius: 999px;
  box-shadow: var(--shadow); display: flex; align-items: center; border: 1px solid var(--border); min-height: var(--tap); }
.search-icon { padding: 0 2px 0 14px; font-size: 14px; }
#q { flex: 1; border: 0; background: transparent; color: var(--text); font: inherit; font-size: 16px;
  padding: 11px 8px; outline: none; min-width: 0; width: 0; }
#q::-webkit-search-cancel-button { -webkit-appearance: none; appearance: none; }
.icon-btn { border: 0; background: transparent; min-width: var(--tap); min-height: var(--tap); padding: 0 10px;
  border-radius: 999px; color: var(--muted); display: inline-grid; place-items: center; }
.icon-btn:hover { background: var(--surface-2); }
.pill { border: 1px solid var(--border); background: var(--surface); box-shadow: var(--shadow); border-radius: 999px;
  min-height: var(--tap); min-width: var(--tap); padding: 0 14px; white-space: nowrap; display: inline-flex;
  align-items: center; justify-content: center; gap: 6px; }
.pill:hover { background: var(--surface-2); }
#results { position: absolute; top: calc(100% + 6px); left: 0; right: 0; margin: 0; padding: 6px; list-style: none;
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
  max-height: min(60dvh, 480px); overflow: auto; overscroll-behavior: contain; }
#results li { display: flex; align-items: center; gap: 10px; padding: 10px; min-height: var(--tap); border-radius: 10px; cursor: pointer; }
#results li[aria-selected="true"], #results li:hover { background: var(--surface-2); }
#results .r-name { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#results .r-alt { display: block; font-size: 12px; color: var(--muted); overflow: hidden; text-overflow: ellipsis; }
#results .r-count { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
#results .empty, #results .group { color: var(--muted); cursor: default; min-height: 0; }
#results .group { font-size: 11px; text-transform: uppercase; letter-spacing: .05em; padding: 8px 10px 2px; }
.badge { font-size: 11px; text-transform: uppercase; letter-spacing: .04em; padding: 2px 7px; border-radius: 999px;
  background: var(--surface-2); color: var(--muted); white-space: nowrap; }
.badge.addr { background: color-mix(in srgb, var(--accent) 15%, var(--surface)); color: var(--accent); }

/* ---------- panel (side panel on desktop, bottom sheet on phones) ---------- */
.panel { position: fixed; z-index: 4; left: 12px; top: 74px; bottom: 12px; width: 390px; background: var(--surface);
  border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); overflow: auto;
  overscroll-behavior: contain; padding: 18px 18px 24px; }
.panel-close { position: absolute; top: 6px; right: 6px; }
.sheet-handle { display: none; }
.panel h2 { margin: 2px 40px 2px 0; font-size: 22px; line-height: 1.2; }
.panel h3 { margin: 22px 0 8px; font-size: 13px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); }
.panel .sub { color: var(--muted); margin: 0 0 12px; }
.sci { font-style: italic; }
.stats { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; margin: 14px 0; }
.stat { background: var(--surface-2); border-radius: 10px; padding: 10px 12px; }
.stat b { display: block; font-size: 20px; font-variant-numeric: tabular-nums; }
.stat span { font-size: 12px; color: var(--muted); }
.bars { display: flex; flex-direction: column; gap: 2px; }
.bar { display: grid; grid-template-columns: 1fr auto; gap: 2px 8px; align-items: center; padding: 8px 6px;
  border-radius: 8px; border: 0; background: transparent; text-align: left; width: 100%; min-height: var(--tap); }
button.bar:hover, button.bar.on { background: var(--surface-2); }
.bar .track { grid-column: 1 / -1; height: 6px; background: var(--surface-2); border-radius: 3px; overflow: hidden; }
.bar .fill { height: 100%; background: var(--accent); border-radius: 3px; }
.bar .n { font-size: 12px; color: var(--muted); font-variant-numeric: tabular-nums; }
.stack { display: flex; height: 10px; border-radius: 5px; overflow: hidden; margin: 6px 0; }
.stack-legend { display: flex; flex-wrap: wrap; gap: 4px 12px; font-size: 12px; color: var(--muted); }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 5px; vertical-align: -1px; }
.kv { display: grid; grid-template-columns: max-content 1fr; gap: 8px 14px; margin: 0; align-items: baseline; }
.kv dt { color: var(--muted); }
.kv dd { margin: 0; }
.linkish { border: 0; background: none; padding: 4px 0; color: var(--accent); text-decoration: underline; text-align: left; }
.list { list-style: none; margin: 0; padding: 0; }
.list li button { display: flex; width: 100%; gap: 8px; justify-content: space-between; align-items: center; border: 0;
  background: none; text-align: left; padding: 10px 6px; min-height: var(--tap); border-bottom: 1px solid var(--border); }
.list li button:hover { background: var(--surface-2); }
.list .meta { font-size: 12px; color: var(--muted); white-space: nowrap; }
.actions { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 14px; }
.btn { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--border); background: var(--surface);
  border-radius: 999px; padding: 0 14px; min-height: 40px; text-decoration: none; color: var(--text); font-size: 14px; }
.btn:hover { background: var(--surface-2); }
.callout { background: color-mix(in srgb, var(--c-heritage) 12%, var(--surface)); border-radius: 10px; padding: 10px 12px; margin-top: 12px; }
.callout.warn { background: color-mix(in srgb, var(--c-user) 14%, var(--surface)); }
.note { font-size: 12px; color: var(--muted); margin-top: 6px; }
.chip { display: inline-flex; align-items: center; gap: 6px; padding: 2px 9px; border-radius: 999px; font-size: 12px;
  background: var(--surface-2); }
.prose p { margin: 0 0 10px; }
.prose ul { margin: 0 0 10px; padding-left: 18px; }

/* ---------- legend ---------- */
.legend { position: fixed; z-index: 3; right: max(12px, var(--safe-r)); top: max(12px, var(--safe-t));
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 8px 12px; font-size: 13px; max-width: 230px; }
.legend-title { font-weight: 600; display: flex; align-items: center; min-height: 32px; gap: 4px; border: 0;
  background: none; padding: 0; width: 100%; text-align: left; }
.legend label { display: flex; align-items: center; gap: 8px; min-height: 32px; cursor: pointer; }
.legend input { accent-color: var(--accent); margin: 0; width: 18px; height: 18px; }
.legend .n { margin-left: auto; color: var(--muted); font-variant-numeric: tabular-nums; font-size: 12px; }
.legend-foot { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-top: 6px;
  padding-top: 6px; border-top: 1px solid var(--border); font-size: 12px; color: var(--muted); }
.legend.collapsed #legend-items, .legend.collapsed .legend-foot { display: none; }
.legend-title .chev { margin-left: auto; padding-left: 10px; color: var(--muted); transition: transform .15s ease; }
.legend.collapsed .legend-title .chev { transform: rotate(-90deg); }
.legend-foot button, .legend-foot a { border: 0; background: none; color: var(--accent); padding: 6px 0; font-size: 12px; text-decoration: underline; }
.foot-links { display: inline-flex; gap: 12px; }

.hover { position: fixed; z-index: 6; pointer-events: none; background: var(--surface); border: 1px solid var(--border);
  border-radius: 10px; padding: 6px 10px; box-shadow: var(--shadow); font-size: 13px; max-width: 260px; }
.toast { position: fixed; z-index: 7; left: 50%; bottom: calc(24px + var(--safe-b)); transform: translateX(-50%);
  background: var(--text); color: var(--bg); padding: 10px 16px; border-radius: 999px; font-size: 14px; max-width: 90vw; text-align: center; }

/* ---------- map controls ---------- */
.maplibregl-ctrl-attrib { font-size: 11px; }
.maplibregl-ctrl-group { background: var(--surface); }
.maplibregl-ctrl-group button { width: 36px; height: 36px; }
.maplibregl-ctrl-group button + button { border-top-color: var(--border); }
.maplibregl-ctrl-attrib-button { width: 28px; height: 28px; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .maplibregl-ctrl-group button span,
  :root:not([data-theme="light"]) .maplibregl-ctrl-attrib-button { filter: invert(1); } }
:root[data-theme="dark"] .maplibregl-ctrl-group button span, :root[data-theme="dark"] .maplibregl-ctrl-attrib-button { filter: invert(1); }
:root[data-theme="dark"] .maplibregl-ctrl-attrib, :root:not([data-theme="light"]) .maplibregl-ctrl-attrib { color: var(--text); }
/* ---------- dev preview banner (only on dev.treenerd.fyi) ---------- */
html.is-dev { --safe-t: calc(env(safe-area-inset-top, 0px) + 34px); }
.dev-banner { position: fixed; z-index: 20; top: 0; left: 0; right: 0; padding: env(safe-area-inset-top, 0px) 12px 0;
  height: calc(26px + env(safe-area-inset-top, 0px)); box-sizing: border-box; line-height: 26px; text-align: center;
  font: 700 12px/26px system-ui, sans-serif; letter-spacing: .02em; color: #1a1300; white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
  background: repeating-linear-gradient(-45deg, #f59f00 0 14px, #fcc419 14px 28px); box-shadow: 0 2px 8px rgba(0, 0, 0, .25); }
.dev-banner a { color: inherit; text-decoration: underline; }
@media (min-width: 721px) { html.is-dev .panel { top: 108px; } }

.maplibregl-ctrl-attrib.maplibregl-compact, .maplibregl-ctrl-attrib { background: var(--surface); color: var(--text); }
.maplibregl-ctrl-attrib a { color: var(--accent); }
.maplibregl-ctrl-bottom-right { right: var(--safe-r); bottom: var(--safe-b); }

/* ---------- phones ---------- */
@media (max-width: 720px) {
  .search { max-width: none; }
  .pill span { display: none; }
  .pill { padding: 0; }
  .legend { top: calc(max(12px, var(--safe-t)) + 52px); padding: 4px 12px; }
  body.panel-open .legend { display: none; }

  /* bottom sheet: height is set from JS (peek / half / full) and draggable via the handle */
  .panel { left: 0; right: 0; bottom: 0; top: auto; width: auto; border-radius: 18px 18px 0 0; border-bottom: 0;
    height: var(--sheet-h, 50dvh); max-height: calc(100dvh - var(--safe-t) - 64px);
    padding: 0 18px calc(24px + var(--safe-b)); transition: height .22s ease; }
  .panel.dragging { transition: none; }
  .sheet-handle { display: block; position: sticky; top: 0; z-index: 2; margin: 0 -18px 4px; height: 28px;
    background: var(--surface); touch-action: none; cursor: grab; }
  .sheet-handle::after { content: ""; position: absolute; left: 50%; top: 10px; width: 40px; height: 5px;
    margin-left: -20px; border-radius: 3px; background: var(--border); }
  .panel-close { top: 22px; z-index: 3; }
  .btn { min-height: var(--tap); }
  .toast { bottom: calc(var(--sheet-offset, 0px) + 16px + var(--safe-b)); }
  .maplibregl-ctrl-group button { width: var(--tap); height: var(--tap); }
}
