:root {
  --bg: #f5f6f8;
  --card: #ffffff;
  --text: #1d2330;
  --muted: #5d6675;
  --accent: #2f6fdb;
  --border: #d9dde5;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #141820;
    --card: #1d2330;
    --text: #e8ebf1;
    --muted: #9aa3b2;
    --accent: #6a9cf0;
    --border: #2d3544;
  }
}

:root[data-theme="dark"] {
  --bg: #141820;
  --card: #1d2330;
  --text: #e8ebf1;
  --muted: #9aa3b2;
  --accent: #6a9cf0;
  --border: #2d3544;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  padding: 0 16px 32px;
  font-family: system-ui, -apple-system, "Segoe UI", sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.4;
}

header { padding: 20px 0 8px; }
h1 { margin: 0; font-size: 1.6rem; }
h2 { margin: 0 0 8px; font-size: 1.05rem; }
.sub, .note, footer, small { color: var(--muted); font-size: 0.9rem; }
.note { margin: 0 0 8px; }

.filters {
  display: flex;
  flex-wrap: wrap;
  gap: 12px;
  align-items: flex-end;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px;
  margin-bottom: 12px;
}
.filters label { display: flex; flex-direction: column; gap: 4px; font-size: 0.9rem; }
.filters label.wide { min-width: 220px; }
.radius {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px;
}
.radius legend { font-size: 0.9rem; padding: 0 4px; }
.radius input[type="number"] { width: 70px; }

select, input, button {
  font: inherit;
  padding: 6px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--card);
  color: var(--text);
}
button { cursor: pointer; background: var(--accent); color: #fff; border: none; }

.kpis {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 12px;
  margin-bottom: 12px;
}
.kpi {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px;
}
.kpi .value { font-size: 1.5rem; font-weight: 600; }
.kpi .label { color: var(--muted); font-size: 0.85rem; }

.map-wrap { margin-bottom: 12px; }
#map {
  height: 420px;
  width: 100%;
  border-radius: 10px;
  border: 1px solid var(--border);
}

.grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: 12px;
  margin-bottom: 12px;
}
.card {
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px;
  min-width: 0;
}
.card.wide { grid-column: 1 / -1; }
canvas { max-height: 320px; }

.table-card { margin-bottom: 12px; }
.table-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
}
.table-scroll { max-height: 420px; overflow: auto; margin-top: 8px; }
table { width: 100%; border-collapse: collapse; font-size: 0.9rem; }
th, td { text-align: left; padding: 6px 8px; border-bottom: 1px solid var(--border); }
th { position: sticky; top: 0; background: var(--card); }
.num { text-align: right; }

footer { margin-top: 16px; }

@media (max-width: 600px) {
  h1 { font-size: 1.3rem; }
  #map { height: 320px; }
  .filters label.wide { min-width: 0; width: 100%; }
}

[hidden] { display: none !important; }
.area-panel { scroll-margin-top: 12px; margin-bottom: 12px; }
.area-controls { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; }
.pill {
  display: inline-flex;
  gap: 6px;
  align-items: center;
  padding: 4px 8px;
  border: 1px solid var(--border);
  border-radius: 999px;
  font-size: 0.9rem;
}
.pill button { padding: 2px 8px; font-size: 0.85rem; }
.id { font-family: ui-monospace, Menlo, monospace; font-size: 0.8rem; word-break: break-all; }
#locationRows tr[data-lsoa] { cursor: pointer; }
#locationRows tr[data-lsoa]:hover { background: var(--bg); }
.area-head th { cursor: pointer; user-select: none; }
.id { font-size: 0.75rem; color: var(--muted); white-space: nowrap; }
.id-col { text-align: right; }

.back { margin: 0 0 4px; font-size: 0.9rem; }
.search-wrap { display: flex; flex-direction: column; gap: 6px; max-width: 420px; margin-bottom: 16px; }
#councilSearch { font-size: 1rem; }
.council-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: 12px;
}
.council-card {
  display: block;
  background: var(--card);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px;
  color: var(--text);
  text-decoration: none;
}
.council-card:hover { border-color: var(--accent); }
.council-card h2 { font-size: 1.1rem; }
.council-card p { margin: 4px 0; }
.muted { color: var(--muted); font-size: 0.9rem; }
.map-box { height: 460px; width: 100%; border-radius: 10px; border: 1px solid var(--border); margin-bottom: 16px; }
.postcode-search { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; margin-bottom: 12px; }
.postcode-search input { width: 180px; text-transform: uppercase; }
.map-legend {
  background: var(--card);
  color: var(--text);
  padding: 6px 8px;
  border-radius: 6px;
  border: 1px solid var(--border);
  font-size: 0.8rem;
  max-height: 260px;
  overflow: auto;
}
.map-legend .swatch { display: inline-block; width: 10px; height: 10px; border-radius: 50%; margin-right: 6px; vertical-align: middle; }

.chat-card { margin-bottom: 16px; }
.chat-form { display: flex; gap: 8px; margin: 8px 0 12px; }
.chat-form input { flex: 1; font-size: 1rem; }
.chat-question { font-weight: 600; margin: 12px 0 4px; }
.chat-answer { border-left: 3px solid var(--accent); padding-left: 10px; margin-bottom: 12px; }
.chat-sql { background: var(--bg); border: 1px solid var(--border); border-radius: 6px; padding: 8px; overflow-x: auto; font-size: 0.85rem; }
.chat-error { color: #c0392b; }
