:root {
  --bg: #ffffff;
  --bg-soft: #f4f6f8;
  --text: #1d2733;
  --muted: #5d6b7a;
  --border: #dde3ea;
  --accent: #1f6feb;
  --accent-text: #ffffff;
  --sidebar-w: 360px;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #161b22;
    --bg-soft: #1f2630;
    --text: #e6edf3;
    --muted: #9aa7b4;
    --border: #2f3a46;
    --accent: #4c8dff;
  }
}

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

#sidebar {
  width: var(--sidebar-w);
  flex-shrink: 0;
  height: 100%;
  display: flex;
  flex-direction: column;
  border-right: 1px solid var(--border);
  background: var(--bg);
  z-index: 1000;
}
#map { flex: 1; height: 100%; }

.brand { padding: 14px 16px 8px; }
.brand h1 { font-size: 17px; margin: 0; }
.brand p { margin: 2px 0 0; color: var(--muted); font-size: 12px; }

.tabs { display: flex; border-bottom: 1px solid var(--border); padding: 0 8px; }
.tab {
  flex: 1; background: none; border: 0; padding: 10px 4px; cursor: pointer;
  color: var(--muted); font: inherit; border-bottom: 2px solid transparent;
}
.tab.active { color: var(--text); border-bottom-color: var(--accent); font-weight: 600; }

.panel { display: none; flex: 1; overflow-y: auto; padding: 12px 16px 20px; }
.panel.active { display: block; }
.panel h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 18px 0 8px; }
.panel h2:first-child { margin-top: 4px; }

.field { display: block; margin-bottom: 10px; font-size: 12px; color: var(--muted); }
.field input, .field select {
  display: block; width: 100%; margin-top: 4px; padding: 7px 9px;
  border: 1px solid var(--border); border-radius: 6px; background: var(--bg-soft); color: var(--text); font: inherit;
}
.row { display: flex; gap: 8px; }
.row > * { flex: 1; }

.btn {
  padding: 8px 12px; border-radius: 6px; border: 1px solid var(--accent);
  background: var(--accent); color: var(--accent-text); cursor: pointer; font: inherit; font-weight: 500;
}
.btn.ghost { background: transparent; color: var(--text); border-color: var(--border); }
.btn.small { padding: 4px 8px; font-size: 12px; }
.btn.active { outline: 2px solid var(--accent); outline-offset: 2px; }

.hint { font-size: 12px; color: var(--muted); }

.search { position: relative; margin-bottom: 10px; }
.search input {
  width: 100%; padding: 8px 10px; border-radius: 6px; border: 1px solid var(--border);
  background: var(--bg-soft); color: var(--text); font: inherit;
}
#search-results {
  list-style: none; margin: 2px 0 0; padding: 0; position: absolute; left: 0; right: 0;
  background: var(--bg); border: 1px solid var(--border); border-radius: 6px; z-index: 10;
  max-height: 260px; overflow-y: auto;
}
#search-results:empty { display: none; }
#search-results li { padding: 7px 10px; cursor: pointer; font-size: 13px; }
#search-results li:hover { background: var(--bg-soft); }

.group { border: 1px solid var(--border); border-radius: 8px; margin-bottom: 10px; overflow: hidden; }
.group-head {
  display: flex; align-items: center; gap: 8px; padding: 8px 10px; background: var(--bg-soft);
  font-weight: 600; cursor: pointer; user-select: none;
}
.group-head .caret { margin-left: auto; color: var(--muted); transition: transform .15s; }
.group.collapsed .caret { transform: rotate(-90deg); }
.group.collapsed .group-body { display: none; }
.group-body { padding: 4px 10px 8px; }
.layer-item { display: flex; align-items: center; gap: 8px; padding: 5px 0; font-size: 13px; }
.layer-item input { margin: 0; }
.layer-item .swatch { width: 12px; height: 12px; border-radius: 3px; flex-shrink: 0; }
.layer-item .swatch.line { height: 3px; border-radius: 1px; }
.layer-item .swatch.dot { border-radius: 50%; }
.layer-item .lname { flex: 1; }
.layer-item .lstatus { font-size: 11px; color: var(--muted); white-space: nowrap; }
.layer-item .lstatus.err { color: #d1453b; }

.chips { display: flex; flex-wrap: wrap; gap: 5px; margin-top: 5px; }
.chip {
  display: inline-flex; align-items: center; gap: 5px; padding: 3px 8px; border-radius: 12px;
  border: 1px solid var(--border); font-size: 12px; cursor: pointer; color: var(--text); background: var(--bg);
}
.chip.off { opacity: .4; }
.chip i { width: 8px; height: 8px; border-radius: 50%; display: inline-block; }

table.summary { width: 100%; border-collapse: collapse; font-size: 12px; margin: 8px 0; }
table.summary th, table.summary td { text-align: left; padding: 5px 4px; border-bottom: 1px solid var(--border); }
table.summary td.num, table.summary th.num { text-align: right; font-variant-numeric: tabular-nums; }

.bar-row { display: flex; align-items: center; gap: 6px; font-size: 12px; margin: 3px 0; }
.bar-row .lbl { width: 78px; text-transform: capitalize; }
.bar-row .bar { height: 10px; border-radius: 2px; }
.bar-row .val { color: var(--muted); font-variant-numeric: tabular-nums; }

.result-list { list-style: none; margin: 6px 0; padding: 0; max-height: 280px; overflow-y: auto; border: 1px solid var(--border); border-radius: 6px; }
.result-list li { padding: 6px 8px; border-bottom: 1px solid var(--border); cursor: pointer; font-size: 12px; }
.result-list li:last-child { border-bottom: 0; }
.result-list li:hover { background: var(--bg-soft); }
.result-list .sub { color: var(--muted); }

.site-card { border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; margin: 8px 0; }
.site-card .cat { font-size: 11px; color: var(--muted); text-transform: uppercase; letter-spacing: .04em; }
.site-card .nm { font-weight: 600; }
.site-card .dist { float: right; font-weight: 600; font-variant-numeric: tabular-nums; }

.credits { font-size: 10.5px; color: var(--muted); padding: 8px 16px; border-top: 1px solid var(--border); }

#status {
  position: fixed; top: 12px; left: calc(var(--sidebar-w) + 60px); z-index: 1200;
  background: var(--bg); color: var(--text); border: 1px solid var(--border); border-radius: 6px;
  padding: 6px 12px; font-size: 12px; box-shadow: 0 2px 8px rgba(0,0,0,.15);
}

.legend {
  position: fixed; right: 12px; bottom: 28px; z-index: 1100; background: var(--bg); color: var(--text);
  border: 1px solid var(--border); border-radius: 8px; padding: 8px 10px; font-size: 11.5px;
  max-height: 45vh; overflow-y: auto; box-shadow: 0 2px 8px rgba(0,0,0,.15);
}
.legend:empty { display: none; }
.legend .lg-title { font-weight: 600; margin: 4px 0 2px; }
.legend .lg-item { display: flex; align-items: center; gap: 6px; }
.legend .lg-item i { width: 10px; height: 10px; border-radius: 50%; display: inline-block; }
.legend .ramp { height: 8px; width: 140px; border-radius: 2px; background: linear-gradient(90deg, #ffffcc, #fd8d3c, #bd0026, #4a0013); }

.btn:disabled { opacity: .55; cursor: wait; }
.hint.warn { color: #b7791f; }

.crit { border: 1px solid var(--border); border-radius: 8px; padding: 7px 9px; margin-bottom: 7px; }
.crit-head { display: flex; justify-content: space-between; font-size: 12.5px; font-weight: 500; }
.crit-head .w-val { font-variant-numeric: tabular-nums; color: var(--accent); font-weight: 700; }
.crit input[type=range] { width: 100%; margin: 4px 0; accent-color: var(--accent); }
.crit-opts { display: flex; gap: 8px; align-items: center; font-size: 11.5px; color: var(--muted); }
.crit-opts select, .crit-opts input {
  padding: 2px 4px; border: 1px solid var(--border); border-radius: 4px; background: var(--bg-soft); color: var(--text); font-size: 11.5px;
}
.crit-opts input { width: 52px; }
#mca-opacity { padding: 0; accent-color: var(--accent); }

.why { font-size: 12px; background: var(--bg-soft); border-left: 3px solid var(--accent); padding: 7px 9px; border-radius: 4px; margin: 0 0 10px; }
.crit .tag { display: none; font-size: 10.5px; color: var(--muted); border: 1px solid var(--border); border-radius: 8px; padding: 0 6px; margin-left: auto; margin-right: 8px; }
.crit.unused { opacity: .5; }
.crit.unused .tag { display: inline; }
.crit.unused .crit-opts { display: none; }
.crit-opts { flex-wrap: wrap; }
.crit-opts input[data-k="lo"], .crit-opts input[data-k="hi"], .crit-opts input[data-k="min"] { width: 46px; }
.site-card.res { border-color: #f0b429; }
.legend .readout { margin-top: 6px; font-variant-numeric: tabular-nums; color: var(--muted); max-width: 200px; }

.mca-sites { padding-left: 0; list-style: none; margin: 6px 0; }
.mca-sites li { border: 1px solid var(--border); border-radius: 8px; padding: 7px 9px; margin-bottom: 6px; font-size: 12.5px; }
.mca-sites .sub { color: var(--muted); margin-left: 6px; }
.mca-sites .row { margin-top: 6px; }
.mca-overlay { image-rendering: pixelated; image-rendering: crisp-edges; }
.mca-pin span {
  display: flex; align-items: center; justify-content: center; width: 24px; height: 24px; border-radius: 50%;
  background: #1a1a1a; color: #fff; font: 700 12px system-ui, sans-serif; border: 2px solid #fff; box-shadow: 0 1px 4px rgba(0,0,0,.4);
}
.legend .ramp.mca { background: linear-gradient(90deg, rgb(215,48,39), rgb(252,141,89), rgb(254,224,139), rgb(145,207,96), rgb(26,152,80)); }
.tab { font-size: 13px; }

.leaflet-popup-content { font-size: 12px; max-height: 300px; overflow-y: auto; }
.popup-table td { padding: 1px 6px 1px 0; vertical-align: top; }
.popup-table td:first-child { color: #667; white-space: nowrap; }

@media (max-width: 760px) {
  body { flex-direction: column-reverse; }
  #sidebar { width: 100%; height: 48%; border-right: 0; border-top: 1px solid var(--border); }
  #map { height: 52%; }
  #status { left: 16px; }
  .legend { display: none; }
}
