:root {
  --bg: #f6f7f9;
  --panel: #ffffff;
  --text: #1c2430;
  --muted: #667085;
  --border: #dde2e8;
  --accent: #2a7de1;
  --active-bg: #1f6fd1;
  --active-text: #ffffff;
  --backdrop: rgba(18, 24, 33, 0.55);
  --hi: #2e7d32;
  --warn-bg: #fff4d6;
  --warn-text: #5c4300;
  --warn-border: #f0d68a;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #11151b;
    --panel: #1a2029;
    --text: #e6eaf0;
    --muted: #94a0b0;
    --border: #2c3542;
    --accent: #5aa0ff;
    --active-bg: #5aa0ff;
    --active-text: #0b1220;
    --backdrop: rgba(0, 0, 0, 0.65);
    --hi: #56d364;
    --warn-bg: #3a2f10;
    --warn-text: #ffe29a;
    --warn-border: #5c4a17;
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  display: flex;
  flex-direction: column;
  font: 14px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
}

#app { flex: 1; display: flex; min-height: 0; }

#banner {
  display: flex;
  align-items: flex-start;
  gap: 10px;
  padding: 10px 16px;
  background: var(--warn-bg);
  color: var(--warn-text);
  border-bottom: 1px solid var(--warn-border);
  font-size: 13px;
  z-index: 1000;
}
#banner[hidden] { display: none; }
#banner .banner-text { flex: 1; }
#banner button {
  background: none; border: 0; padding: 0 4px;
  color: inherit; font-size: 18px; line-height: 1;
}
button.link {
  background: none; border: 0; padding: 0; margin-top: 4px;
  color: var(--accent); font-size: 12px;
}
button.link:hover { text-decoration: underline; }

#sidebar {
  width: 320px;
  flex-shrink: 0;
  overflow-y: auto;
  padding: 16px;
  background: var(--panel);
  border-right: 1px solid var(--border);
}
#map { flex: 1; }

h1 { font-size: 18px; margin: 0; }
h3 { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 0 0 6px; }
.sub { color: var(--muted); font-size: 12px; margin: 2px 0 0; }

#stats { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; margin: 16px 0; }
#stats > div { background: var(--bg); border-radius: 8px; padding: 8px; }
#stats > div > span { display: block; font-size: 16px; font-weight: 600; font-variant-numeric: tabular-nums; }
#stats > div > span b { font-weight: inherit; }
#stats .unit { font-size: 11px; font-weight: 400; color: var(--muted); }
#stats label { font-size: 11px; color: var(--muted); }
/* Max under the averages, for the current filters. */
#stats .range { display: flex; flex-wrap: wrap; gap: 0 6px; margin: 4px 0 0; font-size: 10.5px; font-variant-numeric: tabular-nums; }
#stats .range .hi { color: var(--hi); }

#filters { display: flex; flex-direction: column; gap: 10px; }
#filters label, #filters legend { display: flex; flex-direction: column; gap: 4px; font-size: 12px; color: var(--muted); }
#filters fieldset { border: 0; padding: 0; margin: 0; }
#filters .row { display: flex; align-items: center; gap: 6px; margin-top: 4px; }
#filters .row input { flex: 1; min-width: 0; }
input, select, button {
  font: inherit;
  color: var(--text);
  background: var(--bg);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 6px 8px;
}
button { cursor: pointer; }
button:hover { border-color: var(--accent); }
/* "Reset filters" while filters are applied. */
#reset-filters.active { background: var(--active-bg); border-color: var(--active-bg); color: var(--active-text); font-weight: 600; }
#reset-filters.active:hover { filter: brightness(1.1); }

#legend { margin-top: 18px; }
#legend .item {
  display: flex; align-items: center; gap: 8px;
  font: inherit; font-size: 12px; color: inherit; text-align: left;
  background: none; border: 1px solid transparent; border-radius: 4px; padding: 2px 4px; margin: 0 -4px;
  width: calc(100% + 8px);
}
#legend button.item:hover { background: var(--bg); border-color: var(--border); }
#legend button.item.selected { border-color: var(--accent); }
#legend i { width: 10px; height: 10px; border-radius: 50%; flex-shrink: 0; }
#legend span { margin-left: auto; color: var(--muted); font-variant-numeric: tabular-nums; }
#engine { margin-top: 16px; }
.check { display: flex; align-items: center; gap: 6px; margin: -6px 0 14px; font-size: 12px; color: var(--muted); cursor: pointer; }
.check input { margin: 0; accent-color: var(--accent); }
.footer-row { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; margin-top: 4px; }
.footer-row > * { white-space: nowrap; }
.footer-row button.link { margin-top: 0; flex-shrink: 0; }
.footer-row time { margin: 0; font-size: 11px; overflow: hidden; text-overflow: ellipsis; }
#sidebar.busy #stats { opacity: .5; transition: opacity .2s; }
.points-layer { pointer-events: none; }
.leaflet-container { cursor: pointer; }
.map-button a { display: grid; place-items: center; color: #333; }
.map-button a.leaflet-disabled { color: #bbb; cursor: default; }

.popup table { border-collapse: collapse; margin: 6px 0; font-size: 12px; }
.popup th { text-align: left; color: #667085; font-weight: 500; padding: 1px 10px 1px 0; }

/* loading modal */
.modal-backdrop {
  position: fixed; inset: 0; z-index: 2000;
  display: grid; place-items: center;
  background: var(--backdrop);
  transition: opacity .3s;
}
.modal-backdrop.hidden { opacity: 0; pointer-events: none; }
.modal {
  width: min(360px, calc(100vw - 32px));
  background: var(--panel);
  border-radius: 12px;
  padding: 28px 24px 20px;
  text-align: center;
  box-shadow: 0 20px 50px rgba(0, 0, 0, .3);
}
.modal h2 { font-size: 16px; margin: 14px 0 4px; }
.modal p { margin: 4px 0; }
.spinner {
  width: 40px; height: 40px; margin: 0 auto;
  border: 4px solid var(--border);
  border-top-color: var(--accent);
  border-radius: 50%;
  animation: spin .8s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
.progress { height: 6px; background: var(--border); border-radius: 3px; overflow: hidden; margin: 12px 0 6px; }
#loading-bar { height: 100%; width: 0; background: var(--accent); transition: width .15s; }
.modal-backdrop.error .spinner { animation: none; border-color: #d64545; }

/* Data notice shown at page load. */
.modal.notice { width: min(440px, calc(100vw - 32px)); text-align: left; }
.modal.notice h2 { margin-top: 0; }
.modal.notice p { margin: 10px 0; font-size: 13px; line-height: 1.5; }
.modal.notice a { color: var(--accent); }
#notice-close {
  display: block; margin: 16px 0 0 auto; padding: 7px 18px;
  background: var(--active-bg); border-color: var(--active-bg); color: var(--active-text); font-weight: 600;
}

#sidebar > header { display: flex; align-items: flex-start; justify-content: space-between; gap: 8px; }
#drawer-close, #drawer-backdrop, .menu-toggle { display: none; }

/* Mobile: full-screen map; the sidebar becomes a slide-out drawer opened from a
   hamburger button that replaces Leaflet's zoom buttons (pinch-zoom still works). */
@media (max-width: 720px) {
  #sidebar {
    position: fixed; top: 0; bottom: 0; left: 0; z-index: 1500;
    width: min(320px, 85vw);
    transform: translateX(-100%); visibility: hidden;
    transition: transform .25s ease, visibility 0s linear .25s;
    box-shadow: 0 0 24px rgba(0, 0, 0, .35);
  }
  body.drawer-open #sidebar { transform: none; visibility: visible; transition: transform .25s ease; }
  #drawer-backdrop {
    display: block; position: fixed; inset: 0; z-index: 1400;
    background: var(--backdrop); opacity: 0; pointer-events: none; transition: opacity .25s;
  }
  body.drawer-open #drawer-backdrop { opacity: 1; pointer-events: auto; }
  #drawer-close {
    display: block; flex-shrink: 0; width: 30px; height: 30px; padding: 0;
    font-size: 20px; line-height: 1; background: none; border: 0; color: var(--muted);
  }
  .leaflet-control-zoom { display: none; }
  .menu-toggle { display: block; }
}
