:root {
  --bg: #f6f7f4;
  --surface: #ffffff;
  --text: #1c2326;
  --muted: #5c686e;
  --line: #d9dedb;
  --accent: #1f6f78;
  --shadow: 0 2px 10px rgba(20, 30, 35, .18);
  --lv1: #2a9d8f;
  --lv2: #e9c46a;
  --lv3: #f4a261;
  --lv4: #e76f51;
  --lv5: #8b1e3f;
  --gutter: 16px;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #141a1c; --surface: #1d2528; --text: #e8eeee; --muted: #9bacb0;
    --line: #2e3a3e; --accent: #5ec2cc; --shadow: 0 2px 12px rgba(0,0,0,.5);
  }
}

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

.topbar {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px var(--gutter);
  padding-top: max(10px, env(safe-area-inset-top));
  background: var(--surface);
  border-bottom: 1px solid var(--line);
  z-index: 1200;
}
.brand h1 { margin: 0; font-size: 18px; line-height: 1.2; }
.tagline { margin: 2px 0 0; color: var(--muted); font-size: 12.5px; }

.layers { display: flex; flex-wrap: wrap; gap: 8px; margin-left: auto; }
.chip {
  display: inline-flex; align-items: center; gap: 8px;
  min-height: 40px; padding: 6px 12px;
  border: 1.5px solid var(--line); border-radius: 999px;
  background: var(--surface); color: var(--text);
  font: inherit; font-size: 14px; cursor: pointer;
}
.chip[aria-pressed="true"] { border-color: var(--accent); background: color-mix(in srgb, var(--accent) 12%, var(--surface)); }
.chip:focus-visible, .legend-toggle:focus-visible, .detail-close:focus-visible { outline: 3px solid var(--accent); outline-offset: 2px; }
.chip .count { color: var(--muted); font-size: 12px; }
.chip .swatch { width: 14px; height: 14px; display: inline-block; background: var(--muted); }
.chip[aria-pressed="false"] .swatch { opacity: .35; }

.stage { position: relative; flex: 1; min-height: 0; }
#map { position: absolute; inset: 0; background: #aad3df; }
.leaflet-container { font: inherit; }

/* Layer-specific marker shapes: shape + colour, so severity never relies on colour alone */
.pin { display: block; width: var(--s); height: var(--s); border: 2px solid #fff; box-shadow: 0 0 0 1px rgba(0,0,0,.35), 0 1px 4px rgba(0,0,0,.35); }
.pin.lv1 { background: var(--lv1); } .pin.lv2 { background: var(--lv2); } .pin.lv3 { background: var(--lv3); }
.pin.lv4 { background: var(--lv4); } .pin.lv5 { background: var(--lv5); }
.pin-air { border-radius: 50%; }
.pin-water { border-radius: 4px; }
.pin-noise { transform: rotate(45deg); border-radius: 2px; }
.pin-plastic { clip-path: polygon(50% 4%, 96% 94%, 4% 94%); border: 0; box-shadow: none; }
.marker-wrap { background: none; border: 0; }
.marker-wrap:focus-visible .pin { outline: 3px solid var(--accent); outline-offset: 2px; }
.swatch.pin-plastic { box-shadow: none; }
.swatch.pin-air { border-radius: 50%; } .swatch.pin-water { border-radius: 3px; }
.swatch.pin-noise { transform: rotate(45deg) scale(.8); }

/* Legend */
.legend {
  position: absolute; left: var(--gutter); bottom: 28px; z-index: 1000;
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px;
  box-shadow: var(--shadow); max-width: 260px;
}
.legend-toggle {
  display: block; width: 100%; text-align: left; font: inherit; font-weight: 600;
  padding: 10px 14px; border: 0; background: none; color: var(--text); cursor: pointer; border-radius: 12px;
}
.legend-body { padding: 0 14px 12px; }
.legend[data-collapsed="true"] .legend-body { display: none; }
.legend h2 { font-size: 12px; text-transform: uppercase; letter-spacing: .05em; color: var(--muted); margin: 6px 0 6px; }
.scale { display: grid; grid-template-columns: 18px 1fr; gap: 4px 8px; align-items: center; font-size: 13px; }
.scale i { width: 16px; height: 16px; border-radius: 3px; display: block; }
.shapes { display: grid; gap: 5px; font-size: 13px; margin-top: 4px; }
.shapes div { display: flex; align-items: center; gap: 8px; }
.shapes .swatch { width: 14px; height: 14px; background: var(--muted); display: inline-block; }
.legend .note { margin: 10px 0 0; color: var(--muted); font-size: 11.5px; line-height: 1.35; }

/* Detail panel */
.detail {
  position: absolute; top: var(--gutter); right: var(--gutter); z-index: 1100;
  width: min(360px, calc(100% - 2 * var(--gutter)));
  max-height: calc(100% - 2 * var(--gutter)); overflow: auto;
  background: var(--surface); border: 1px solid var(--line); border-radius: 14px;
  box-shadow: var(--shadow); padding: 16px;
}
.detail[hidden] { display: none; }
.detail-close {
  position: absolute; top: 6px; right: 6px; width: 40px; height: 40px;
  border: 0; background: none; font-size: 26px; line-height: 1; color: var(--muted); cursor: pointer; border-radius: 8px;
}
.detail h2 { margin: 0 40px 2px 0; font-size: 18px; }
.detail .where { margin: 0 0 10px; color: var(--muted); font-size: 13px; }
.badge { display: inline-flex; align-items: center; gap: 8px; padding: 4px 10px; border-radius: 999px; font-weight: 600; font-size: 13px; color: #fff; }
.badge.dark-text { color: #1c2326; }
.detail dl { display: grid; grid-template-columns: auto 1fr; gap: 4px 14px; margin: 12px 0 0; font-size: 14px; }
.detail dt { color: var(--muted); }
.detail dd { margin: 0; }
.detail p { margin: 10px 0 0; font-size: 14px; }
.detail .src { color: var(--muted); font-size: 12px; }

.status {
  position: absolute; left: 50%; transform: translateX(-50%); bottom: 8px; z-index: 900;
  background: color-mix(in srgb, var(--surface) 92%, transparent); border: 1px solid var(--line);
  border-radius: 999px; padding: 3px 12px; font-size: 12px; color: var(--muted); max-width: calc(100% - 32px);
  text-align: center; pointer-events: none;
}
.status:empty { display: none; }
.noscript { position: absolute; inset: 40% 0 auto; text-align: center; z-index: 2000; }

/* Mobile: compact top bar, scrollable chips, details as bottom sheet */
@media (max-width: 720px) {
  .topbar { flex-direction: column; align-items: stretch; gap: 8px; }
  .tagline { display: none; }
  .layers { margin-left: 0; flex-wrap: nowrap; overflow-x: auto; padding-bottom: 2px; -webkit-overflow-scrolling: touch; }
  .chip { flex: 0 0 auto; }
  .legend { bottom: auto; top: 10px; left: 56px; max-width: min(260px, calc(100% - 66px)); }
  .legend[data-collapsed="false"] { z-index: 1000; }
  .detail {
    left: 0; right: 0; top: auto; bottom: 0; width: auto; max-height: 55%;
    border-radius: 16px 16px 0 0; padding-bottom: max(16px, env(safe-area-inset-bottom));
  }
  .status { bottom: auto; top: 56px; }
}
@media (prefers-reduced-motion: reduce) { * { scroll-behavior: auto !important; transition: none !important; animation: none !important; } }
