:root {
  --green-900: #0b3d24;
  --green-800: #0f4d2e;
  --green-700: #17643c;
  --green-100: #e7f2ea;
  --ink: #15231b;
  --ink-2: #4a5a50;
  --line: #dfe7e1;
  --paper: #ffffff;
  --bg: #f3f6f4;
  --c-flood: #e5262b;
  --c-traffic: #1f6fd1;
  --c-waste: #f2c21b;
  --c-air: #7a3ea1;
  --c-green: #1d9447;
  --radius: 14px;
  --shadow: 0 1px 2px rgba(11, 61, 36, .06), 0 6px 20px rgba(11, 61, 36, .08);
  --font: "Segoe UI", system-ui, -apple-system, Roboto, "Helvetica Neue", Arial, sans-serif;
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }
/* dvh follows the phone's visible area as the browser address bar shows/hides */
body { height: 100dvh; font-family: var(--font); color: var(--ink); background: var(--bg); display: flex; flex-direction: column; overflow: hidden; }
button { font: inherit; cursor: pointer; -webkit-tap-highlight-color: transparent; }

/* ---------- top bar ---------- */
.topbar {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 18px; padding-top: max(10px, env(safe-area-inset-top));
  padding-left: max(18px, env(safe-area-inset-left)); padding-right: max(18px, env(safe-area-inset-right));
  background: var(--green-900); color: #fff;
  border-bottom: 3px solid var(--green-700); flex: 0 0 auto;
}
.brand { display: flex; align-items: center; gap: 12px; min-width: 0; }
.logo { height: 44px; width: auto; background: #fff; border-radius: 6px; padding: 4px 8px; flex: 0 0 auto; }
.brand-mark { width: 40px; height: 40px; border-radius: 50%; background: #2fa35a; display: grid; place-items: center; flex: 0 0 auto; }
.brand-mark svg { width: 24px; height: 24px; fill: none; stroke: #fff; stroke-width: 2; stroke-linecap: round; }
.titles { min-width: 0; }
.titles h1 { margin: 0; font-size: clamp(16px, 2.2vw, 24px); font-weight: 800; letter-spacing: -.01em; line-height: 1.15; }
.subtitle { margin: 2px 0 0; font-size: clamp(12px, 1.4vw, 15px); color: #a9dcb9; font-weight: 600; }
.top-actions { display: flex; align-items: center; gap: 8px; }
.lang { display: flex; background: rgba(255,255,255,.12); border-radius: 999px; padding: 3px; }
.lang button { border: 0; background: transparent; color: #d9efe0; padding: 5px 12px; border-radius: 999px; font-weight: 700; font-size: 13px; }
.lang button.on { background: #fff; color: var(--green-900); }
.top-actions { flex: 0 0 auto; }
.sheet-handle { display: none; }

/* ---------- layout ---------- */
.layout { flex: 1 1 auto; display: flex; min-height: 0; }
.sidebar {
  width: 330px; flex: 0 0 330px; overflow-y: auto; padding: 14px;
  display: flex; flex-direction: column; gap: 12px; background: var(--bg);
}
/* isolate: keeps Leaflet's z-indexed controls from drawing over the mobile sheet */
.map-wrap { position: relative; flex: 1 1 auto; min-width: 0; isolation: isolate; }
#map { position: absolute; inset: 0; background: #0e2a1d; }

.card { background: var(--paper); border-radius: var(--radius); box-shadow: var(--shadow); padding: 14px; }
.step-head { display: flex; gap: 10px; align-items: flex-start; margin-bottom: 10px; }
.step2 .step-head { margin-bottom: 0; }
.step-num {
  width: 30px; height: 30px; flex: 0 0 30px; border-radius: 50%; background: var(--green-900); color: #fff;
  display: grid; place-items: center; font-weight: 800; font-size: 15px;
}
.step-head h2 { margin: 3px 0 0; font-size: 15px; font-weight: 800; color: var(--green-900); line-height: 1.25; }
.hint { margin: 2px 0 0; font-size: 12.5px; color: var(--ink-2); line-height: 1.35; }

/* ---------- categories ---------- */
.cats { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 6px; }
.cat { display: flex; align-items: stretch; border-radius: 12px; border: 1.5px solid var(--line); transition: border-color .15s, background .15s, opacity .15s; }
.cat-pick {
  flex: 1 1 auto; display: flex; align-items: center; gap: 10px; text-align: left;
  border: 0; background: transparent; padding: 8px 6px 8px 8px; border-radius: 11px 0 0 11px; color: inherit; min-width: 0;
}
.cat:hover { border-color: #b9cfc0; }
.cat.active { border-color: var(--c); background: color-mix(in srgb, var(--c) 9%, white); box-shadow: 0 0 0 3px color-mix(in srgb, var(--c) 22%, transparent); }
.cat.hidden-cat .cat-pick { opacity: .45; }
.count {
  width: 40px; height: 40px; flex: 0 0 40px; border-radius: 50%; background: var(--c);
  color: #fff; font-weight: 800; font-size: 15px; display: grid; place-items: center;
  box-shadow: inset 0 -3px 0 rgba(0,0,0,.15); font-variant-numeric: tabular-nums; transition: transform .25s;
}
.count.dark { color: #3b2e00; }
.count.bump { transform: scale(1.18); }
.cat-icon { width: 34px; height: 34px; flex: 0 0 34px; border-radius: 9px; background: var(--c); display: grid; place-items: center; }
.cat-icon svg { width: 22px; height: 22px; fill: #fff; }
.cat-icon.dark svg { fill: #3b2e00; }
.cat-text { min-width: 0; line-height: 1.2; }
.cat-name { display: block; font-weight: 700; font-size: 13.5px; }
.cat-alt { display: block; font-size: 11.5px; color: var(--ink-2); }
.cat-eye {
  flex: 0 0 40px; border: 0; border-left: 1.5px solid var(--line); background: transparent; border-radius: 0 11px 11px 0;
  display: grid; place-items: center; color: var(--green-800);
}
.cat-eye svg { width: 19px; height: 19px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.cat-eye:hover { background: var(--green-100); }
.cat-eye[aria-pressed="false"] { color: #9aa79f; }

.filter-tools { display: flex; align-items: center; gap: 6px; margin-top: 10px; font-size: 12.5px; color: var(--ink-2); }
.ft-label { font-weight: 600; }
.link { border: 0; background: none; padding: 0; color: var(--green-700); font-weight: 700; font-size: 12.5px; }
.link:hover { text-decoration: underline; }
.sep { color: #b4c0b8; }

/* ---------- totals ---------- */
.total-row { display: flex; align-items: center; justify-content: space-between; gap: 10px; }
.total-num { font-size: 30px; font-weight: 800; color: var(--green-900); line-height: 1; font-variant-numeric: tabular-nums; }
.total-label { font-size: 12.5px; color: var(--ink-2); margin-top: 2px; }
.dist { display: flex; height: 10px; border-radius: 999px; overflow: hidden; background: var(--line); margin: 12px 0 10px; gap: 2px; }
.dist span { display: block; height: 100%; transition: flex-grow .35s; min-width: 0; }
.ranking { list-style: none; margin: 0; padding: 0; display: flex; flex-direction: column; gap: 4px; font-size: 12.5px; }
.ranking li { display: flex; align-items: center; gap: 8px; }
.ranking .sw { width: 10px; height: 10px; border-radius: 50%; flex: 0 0 10px; }
.ranking .nm { flex: 1 1 auto; color: var(--ink); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.ranking .pc { color: var(--ink-2); font-variant-numeric: tabular-nums; }
.ranking .empty { color: var(--ink-2); }

.switch { display: inline-flex; align-items: center; gap: 8px; font-size: 13px; font-weight: 600; color: var(--ink); cursor: pointer; user-select: none; }
.switch input { position: absolute; opacity: 0; pointer-events: none; }
.switch .track { width: 38px; height: 22px; border-radius: 999px; background: #c9d4cd; position: relative; transition: background .2s; }
.switch .thumb { position: absolute; top: 3px; left: 3px; width: 16px; height: 16px; border-radius: 50%; background: #fff; transition: transform .2s; box-shadow: 0 1px 2px rgba(0,0,0,.2); }
.switch input:checked + .track { background: var(--green-700); }
.switch input:checked + .track .thumb { transform: translateX(16px); }
.switch input:focus-visible + .track { outline: 2px solid var(--green-700); outline-offset: 2px; }

.side-foot { margin-top: auto; display: flex; flex-direction: column; gap: 6px; padding: 4px 4px 0; font-size: 12px; color: var(--ink-2); }
.tagline { font-weight: 700; color: var(--green-800); }
.mode { display: inline-flex; align-items: center; gap: 6px; }
.mode .dot { width: 8px; height: 8px; border-radius: 50%; background: #aaa; }
.mode.online .dot { background: #22a35a; box-shadow: 0 0 0 3px rgba(34,163,90,.2); }
.mode.offline .dot { background: #e0a100; box-shadow: 0 0 0 3px rgba(224,161,0,.2); }

/* ---------- map overlays ---------- */
.map-wrap.placing #map { cursor: crosshair; }
.banner {
  position: absolute; top: 12px; left: 50%; transform: translateX(-50%); z-index: 800;
  display: flex; align-items: center; gap: 10px; background: #fff; border-radius: 999px;
  padding: 7px 8px 7px 14px; box-shadow: 0 6px 24px rgba(0,0,0,.25); font-size: 13.5px; font-weight: 600; max-width: calc(100% - 24px);
}
.banner[hidden] { display: none; }
.banner-dot { width: 14px; height: 14px; border-radius: 50%; flex: 0 0 14px; }
.banner-x { border: 0; background: var(--green-100); color: var(--green-900); border-radius: 999px; padding: 5px 12px; font-weight: 700; font-size: 12.5px; }
.toast {
  position: absolute; bottom: 24px; left: 50%; transform: translate(-50%, 20px); z-index: 900;
  background: var(--green-900); color: #fff; padding: 9px 16px; border-radius: 10px; font-size: 13.5px;
  opacity: 0; pointer-events: none; transition: opacity .2s, transform .2s; max-width: calc(100% - 24px); text-align: center;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }
.toast.warn { background: #8a3b00; }

/* sticker markers */
.sticker { background: none; border: 0; }
.sticker .pin {
  width: 30px; height: 30px; border-radius: 50%; background: var(--c); border: 2.5px solid #fff;
  box-shadow: 0 2px 6px rgba(0,0,0,.45); display: grid; place-items: center; transition: transform .15s;
}
.sticker .pin svg { width: 16px; height: 16px; fill: #fff; }
.sticker .pin.dark svg { fill: #3b2e00; }
.sticker:hover .pin { transform: scale(1.15); }
.sticker.draft .pin { animation: pulse 1.2s infinite; }
.sticker.dim { opacity: .55; }
@keyframes pulse { 0%,100% { box-shadow: 0 0 0 0 color-mix(in srgb, var(--c) 70%, transparent), 0 2px 6px rgba(0,0,0,.45); } 50% { box-shadow: 0 0 0 10px transparent, 0 2px 6px rgba(0,0,0,.45); } }

.cluster-free {} /* placeholder for future clustering */

/* popups */
.leaflet-popup-content-wrapper { border-radius: 12px; }
.leaflet-popup-content { margin: 12px 14px; font-family: var(--font); font-size: 13px; min-width: 210px; }
.pop-head { display: flex; align-items: center; gap: 8px; font-weight: 800; color: var(--ink); margin-bottom: 6px; }
.pop-head .sw { width: 12px; height: 12px; border-radius: 50%; flex: 0 0 12px; }
.pop-comment { margin: 4px 0 6px; color: var(--ink); white-space: pre-wrap; word-break: break-word; }
.pop-meta { color: var(--ink-2); font-size: 11.5px; }
.pop-actions { display: flex; gap: 6px; margin-top: 10px; justify-content: flex-end; }
.btn { border: 0; border-radius: 8px; padding: 7px 12px; font-weight: 700; font-size: 12.5px; }
.btn-primary { background: var(--green-800); color: #fff; }
.btn-primary:hover { background: var(--green-700); }
.btn-ghost { background: var(--green-100); color: var(--green-900); }
.btn-danger { background: #fde8e8; color: #a3151a; }
.pop-form textarea {
  width: 100%; min-height: 64px; resize: vertical; border: 1.5px solid var(--line); border-radius: 8px;
  padding: 7px 8px; font: inherit; font-size: 13px;
}
.pop-form textarea:focus { outline: none; border-color: var(--green-700); }
.pop-form .chars { text-align: right; font-size: 11px; color: var(--ink-2); }

/* legend chip for boundary */
.legend-box { background: #fff; border-radius: 10px; padding: 7px 10px; box-shadow: var(--shadow); font: 600 12px var(--font); color: var(--ink); display: flex; align-items: center; gap: 8px; }
.legend-box .ln { width: 22px; height: 12px; border: 2.5px solid #e5262b; border-radius: 3px; }
.legend-box small { display: block; color: var(--ink-2); font-weight: 500; }
.locate-btn { width: 34px; height: 34px; background: #fff; border: 0; display: grid; place-items: center; }
.locate-btn svg { width: 18px; height: 18px; fill: none; stroke: var(--green-900); stroke-width: 2; }
.search-box { width: min(320px, calc(100vw - 90px)); font-family: var(--font); }
.search-form { display: flex; align-items: center; background: #fff; border-radius: 10px; box-shadow: var(--shadow); overflow: hidden; }
.search-form input { flex: 1 1 auto; min-width: 0; border: 0; outline: 0; padding: 9px 4px 9px 12px; font: 500 13.5px var(--font); color: var(--ink); background: transparent; }
.search-form input::-webkit-search-cancel-button { display: none; }
.search-form:focus-within { box-shadow: 0 0 0 2px var(--green-700), var(--shadow); }
.search-clear { border: 0; background: transparent; color: var(--ink-2); font-size: 20px; line-height: 1; padding: 0 6px; }
.search-clear[hidden] { display: none; }
.search-go { border: 0; background: var(--green-800); width: 38px; height: 38px; display: grid; place-items: center; flex: 0 0 auto; }
.search-go:hover { background: var(--green-700); }
.search-go:disabled { opacity: .6; cursor: progress; }
.search-go svg { width: 18px; height: 18px; fill: none; stroke: #fff; stroke-width: 2.2; stroke-linecap: round; }
.search-results { list-style: none; margin: 6px 0 0; padding: 4px; background: #fff; border-radius: 10px; box-shadow: var(--shadow); max-height: 50vh; overflow-y: auto; }
.search-results[hidden] { display: none; }
.search-results button { display: block; width: 100%; text-align: left; border: 0; background: transparent; padding: 7px 9px; border-radius: 7px; color: var(--ink); }
.search-results button:hover, .search-results button:focus-visible { background: var(--green-100); outline: 0; }
.search-results strong { display: block; font-size: 13px; }
.search-results small { display: block; font-size: 11.5px; color: var(--ink-2); margin-top: 1px; }
.search-tip { font: 700 12px var(--font); color: var(--green-900); border-radius: 8px; }

/* ---------- large screens ---------- */
@media (min-width: 1600px) {
  .sidebar { width: 370px; flex-basis: 370px; padding: 18px; }
}

/* ---------- tablet / small laptop ---------- */
@media (max-width: 1100px) and (min-width: 761px) {
  .sidebar { width: 290px; flex-basis: 290px; padding: 12px; gap: 10px; }
  .card { padding: 12px; }
  .cat-icon { display: none; }
  .logo { height: 38px; }
  .brand-mark { display: none; }
}

/* ---------- touch devices: bigger tap targets ---------- */
@media (pointer: coarse) {
  .lang button { padding: 8px 14px; }
  .link { padding: 6px 2px; }
  .btn { padding: 10px 14px; font-size: 13.5px; }
  .locate-btn { width: 44px; height: 44px; }
  .leaflet-touch .leaflet-bar a { width: 40px; height: 40px; line-height: 40px; }
  .search-go { width: 44px; height: 44px; }
  .search-clear { padding: 0 10px; font-size: 22px; }
  .search-results button { padding: 10px; }
  /* 16px stops iOS Safari zooming the page when a field is focused */
  .search-form input, .pop-form textarea { font-size: 16px; }
}

/* ---------- mobile: sidebar becomes bottom sheet ---------- */
@media (max-width: 760px) {
  :root { --peek: 54px; }
  .topbar { padding: 8px 12px; padding-top: max(8px, env(safe-area-inset-top)); gap: 8px; }
  .brand { gap: 8px; }
  .brand-mark { display: none; }
  .logo { height: 32px; padding: 3px 6px; }
  .titles h1 { font-size: 15px; }
  .subtitle { font-size: 11.5px; }
  .banner { top: 64px; border-radius: 14px; font-size: 13px; }   /* sit below the search box */
  .search-box { width: min(380px, calc(100vw - 92px)); }
  .layout { position: relative; }
  .map-wrap { position: absolute; inset: 0; }
  .sidebar {
    position: absolute; left: 0; right: 0; bottom: 0; width: auto; max-height: 58%; z-index: 1000;
    border-radius: 18px 18px 0 0; box-shadow: 0 -8px 30px rgba(0,0,0,.25);
    padding: 0 12px calc(14px + env(safe-area-inset-bottom)); gap: 10px;
    transition: transform .25s ease; overscroll-behavior: contain;
  }
  /* keep the handle peeking above the bottom edge so the sheet can always be reopened */
  .sidebar.collapsed { transform: translateY(calc(100% - var(--peek) - env(safe-area-inset-bottom))); overflow: hidden; }
  .sheet-handle {
    display: flex; flex-direction: column; align-items: center; gap: 6px; position: sticky; top: 0; z-index: 2;
    margin: 0 -12px; padding: 8px 16px 10px; min-height: var(--peek); border: 0; background: var(--bg);
    border-radius: 18px 18px 0 0; color: var(--green-900); touch-action: none;
  }
  .sheet-handle .grab { width: 42px; height: 5px; border-radius: 999px; background: #b9c8bf; }
  .sheet-label { display: flex; align-items: center; gap: 8px; font-weight: 800; font-size: 14px; }
  .sheet-count { background: var(--green-900); color: #fff; border-radius: 999px; padding: 1px 9px; font-size: 12px; font-variant-numeric: tabular-nums; }
  .step2, .side-foot .tagline { display: none; }
  .cats { display: grid; grid-template-columns: 1fr 1fr; }
  .cat-alt { display: none; }
  .cat-icon { display: none; }
  .count { width: 34px; height: 34px; flex-basis: 34px; font-size: 14px; }
  .cat-name { font-size: 13px; }
  .total-num { font-size: 26px; }
  /* map controls & messages stay clear of the peeking handle */
  .leaflet-bottom { margin-bottom: calc(var(--peek) + env(safe-area-inset-bottom)); }
  .toast { bottom: calc(var(--peek) + 16px + env(safe-area-inset-bottom)); }
  .leaflet-bottom.leaflet-left { display: none; }
  .sidebar.collapsed ~ .map-wrap .leaflet-bottom.leaflet-left { display: block; }
  .leaflet-popup-content { min-width: 0; width: min(260px, calc(100vw - 80px)) !important; }
  .leaflet-control-attribution { font-size: 10px; max-width: calc(100vw - 110px); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
}

/* ---------- small phones ---------- */
@media (max-width: 420px) {
  .logo { height: 26px; padding: 2px 5px; border-radius: 5px; }
  .titles h1 { font-size: 13.5px; }
  .subtitle { display: none; }
  .lang button { padding: 6px 10px; font-size: 12px; }
  .cats { grid-template-columns: 1fr; }
  .cat-pick { padding: 6px; }
}

/* ---------- phones in landscape: short screens ---------- */
@media (max-height: 500px) and (orientation: landscape) {
  .topbar { padding-block: 5px; }
  .logo { height: 26px; }
  .subtitle { display: none; }
  .titles h1 { font-size: 14px; }
}
@media (max-height: 500px) and (orientation: landscape) and (max-width: 760px) {
  :root { --peek: 44px; }
  .sidebar { max-height: 78%; }
  .sheet-handle { flex-direction: row; justify-content: center; gap: 12px; padding-block: 6px; }
  .cats { grid-template-columns: 1fr 1fr 1fr; }
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; animation: none !important; } }
