:root {
  --bg: #f4f2ec;
  --panel: #ffffff;
  --ink: #1f2a22;
  --muted: #66705f;
  --line: #e2ded3;
  --accent: #2f6b3f;
  --accent-ink: #ffffff;
  --accent-soft: #e5f0e5;
  --alert: #c8382a;
  --flag: #d98a0b;
  --history: #4f7f8f;
  --shadow: 0 2px 10px rgba(20, 30, 20, 0.18);
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #141815;
    --panel: #1d221e;
    --ink: #e7ebe4;
    --muted: #9aa494;
    --line: #2f3630;
    --accent: #74b583;
    --accent-ink: #0f1a12;
    --accent-soft: #233226;
    --alert: #ef6b5b;
    --flag: #f0ab3a;
    --history: #7fb2c2;
    --shadow: 0 2px 12px rgba(0, 0, 0, 0.5);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #141815; --panel: #1d221e; --ink: #e7ebe4; --muted: #9aa494; --line: #2f3630;
  --accent: #74b583; --accent-ink: #0f1a12; --accent-soft: #233226;
  --alert: #ef6b5b; --flag: #f0ab3a; --history: #7fb2c2; --shadow: 0 2px 12px rgba(0, 0, 0, 0.5);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg);
  color: var(--ink);
  font: 15px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif;
  overflow: hidden;
}
button, input { font: inherit; color: inherit; }
a { color: var(--accent); }
#map { position: fixed; inset: 0; z-index: 0; background: var(--bg); }

/* Top bar */
.bar {
  position: fixed; z-index: 500;
  top: calc(env(safe-area-inset-top) + 10px); left: 12px; right: 12px;
  display: flex; align-items: center; gap: 10px; pointer-events: none;
}
.bar-text {
  pointer-events: auto; flex: 1; min-width: 0;
  background: var(--panel); border-radius: 14px; padding: 8px 14px; box-shadow: var(--shadow);
}
.bar h1 { margin: 0; font-size: 16px; }
.bar p { margin: 0; font-size: 12px; color: var(--muted); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.round {
  pointer-events: auto; width: 44px; height: 44px; border-radius: 50%; border: 0; flex: none;
  background: var(--panel); color: var(--accent); box-shadow: var(--shadow); display: grid; place-items: center; cursor: pointer;
}
.round.on { background: var(--accent); color: var(--accent-ink); }

/* Filter button */
.fab {
  position: fixed; z-index: 500; right: 12px; bottom: calc(env(safe-area-inset-bottom) + 18px);
  display: inline-flex; align-items: center; gap: 8px; padding: 12px 18px; border: 0; border-radius: 999px;
  background: var(--accent); color: var(--accent-ink); font-weight: 600; box-shadow: var(--shadow); cursor: pointer;
}
.badge { background: var(--accent-ink); color: var(--accent); border-radius: 999px; padding: 0 7px; font-size: 12px; }

/* Bottom sheet (side panel on wide screens) */
.sheet {
  position: fixed; z-index: 600; left: 0; right: 0; bottom: 0; max-height: 78vh;
  display: flex; flex-direction: column;
  background: var(--panel); border-radius: 18px 18px 0 0; box-shadow: var(--shadow);
  padding: 6px 16px calc(env(safe-area-inset-bottom) + 12px);
}
.sheet[hidden] { display: none; }
.sheet-head { display: flex; align-items: center; justify-content: space-between; padding: 8px 0 4px; }
.sheet-head::before {
  content: ""; position: absolute; top: 6px; left: 50%; width: 40px; height: 4px; margin-left: -20px;
  border-radius: 2px; background: var(--line);
}
.sheet h2 { margin: 0; font-size: 18px; }
.sheet h3 { margin: 0 0 8px; font-size: 13px; color: var(--muted); font-weight: 600; text-transform: uppercase; letter-spacing: 0.04em; }
.group { padding: 10px 0; border-top: 1px solid var(--line); }
.sheet-head + .group { border-top: 0; }
.group-head { display: flex; justify-content: space-between; align-items: baseline; }
.species-group { display: flex; flex-direction: column; min-height: 0; flex: 1; }
.link { border: 0; background: none; color: var(--accent); padding: 4px 2px; cursor: pointer; font-weight: 600; }
.links { font-size: 14px; color: var(--muted); }

.chips { display: flex; flex-wrap: wrap; gap: 8px; }
.chips button {
  display: inline-flex; align-items: center; gap: 6px; padding: 7px 12px; border-radius: 999px;
  border: 1px solid var(--line); background: transparent; cursor: pointer; opacity: 0.55;
}
.chips button[aria-pressed="true"] { opacity: 1; border-color: var(--ink); }
.chips span { color: var(--muted); font-size: 13px; }
.dot { display: inline-block; width: 10px; height: 10px; border-radius: 50%; }
.dot.alert { background: var(--alert); }
.dot.flag { background: var(--flag); }
.dot.history { background: var(--history); }

.segmented { display: grid; grid-template-columns: repeat(4, 1fr); border: 1px solid var(--line); border-radius: 10px; overflow: hidden; }
.segmented button { border: 0; background: transparent; padding: 8px 4px; cursor: pointer; font-size: 14px; }
.segmented button + button { border-left: 1px solid var(--line); }
.segmented button[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); font-weight: 600; }

#sp-search {
  width: 100%; padding: 9px 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); margin-bottom: 6px;
}
.species { list-style: none; margin: 0; padding: 0; overflow-y: auto; min-height: 120px; -webkit-overflow-scrolling: touch; }
.species label { display: flex; align-items: center; gap: 10px; padding: 8px 2px; cursor: pointer; border-bottom: 1px solid var(--line); }
.species input { width: 18px; height: 18px; accent-color: var(--accent); flex: none; }
.species .name { flex: 1; min-width: 0; }
.species .name i { display: block; font-size: 12px; color: var(--muted); }
.species .count { color: var(--muted); font-size: 13px; font-variant-numeric: tabular-nums; }
.species .bell { font-size: 12px; }
.species .empty { padding: 12px 0; color: var(--muted); }

@media (min-width: 760px) {
  .sheet {
    top: 12px; bottom: 12px; left: 12px; right: auto; width: 360px; max-height: none; border-radius: 16px;
    padding-top: 12px;
  }
  .sheet-head::before { display: none; }
  .bar { left: 384px; }
  body.sheet-closed .bar { left: 12px; }
}

/* Pins */
.pin { border-radius: 50%; border: 2px solid #fff; box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.35); }
.pin.alert { background: var(--alert); }
.pin.flag { background: var(--flag); }
.pin.history { background: var(--history); }
.home-pin { background: var(--accent); border: 3px solid #fff; border-radius: 50%; box-shadow: 0 0 0 2px var(--accent); }
.me-pin { background: #2a7de1; border: 3px solid #fff; border-radius: 50%; box-shadow: 0 0 0 6px rgba(42, 125, 225, 0.25); }

.cluster {
  display: grid; place-items: center; border-radius: 50%; color: #fff; font-weight: 700; font-size: 12px;
  background: var(--history); border: 2px solid #fff; box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.3);
}
.cluster.flag { box-shadow: 0 0 0 4px var(--flag); }
.cluster.alert { box-shadow: 0 0 0 4px var(--alert); }

/* Popups */
.leaflet-popup-content-wrapper, .leaflet-popup-tip { background: var(--panel); color: var(--ink); }
.leaflet-popup-content { margin: 12px; width: 240px !important; }
.pop img { width: 100%; height: 150px; object-fit: cover; border-radius: 8px; background: var(--line); display: block; margin-bottom: 8px; }
.pop .name { font-weight: 700; font-size: 15px; }
.pop .sci { font-style: italic; color: var(--muted); font-size: 13px; }
.pop .meta { font-size: 13px; color: var(--muted); margin-top: 6px; }
.pop .tags { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 6px; }
.pop .tag { font-size: 11px; padding: 1px 7px; border-radius: 999px; background: var(--accent-soft); }
.pop .tag.alert { background: var(--alert); color: #fff; }
.pop .tag.flag { background: var(--flag); color: #1f1400; }
.pop .actions { display: flex; gap: 12px; margin-top: 8px; font-size: 14px; font-weight: 600; }
.leaflet-container a.leaflet-popup-close-button { color: var(--muted); }

.leaflet-control-zoom { display: none; }
@media (min-width: 760px) { .leaflet-control-zoom { display: block; } }
.leaflet-control-attribution { font-size: 10px; }
