:root {
  color-scheme: light;
  --ink: #072b49;
  --muted: #53697a;
  --line: #cfe1ec;
  --paper: #f6fbfe;
  --surface: #ffffff;
  --accent: #003865;
  --accent-2: #73bfe2;
  --accent-3: #0b5f88;
  --soft: #e7f5fb;
  --warn: #eaf6fb;
  --danger: #a62822;
  --shadow: 0 18px 40px rgba(0, 56, 101, 0.10);
}

* { box-sizing: border-box; }

html {
  overflow-x: hidden;
  scroll-padding-top: 96px;
}

body {
  margin: 0;
  overflow-x: hidden;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  background: var(--paper);
  color: var(--ink);
  padding-bottom: calc(78px + env(safe-area-inset-bottom));
}

button, input, select, textarea {
  font: inherit;
}

button, select, input {
  min-height: 44px;
}

button {
  border: 1px solid var(--accent);
  background: var(--accent);
  color: white;
  padding: 10px 14px;
  border-radius: 7px;
  cursor: pointer;
  font-weight: 700;
}

button.secondary {
  color: var(--ink);
  background: var(--surface);
  border-color: var(--line);
}

button:hover, button:focus-visible, .bottom-nav button.active {
  border-color: var(--accent);
  background: var(--soft);
  color: var(--accent);
  outline: none;
}

button:disabled {
  cursor: not-allowed;
  opacity: 0.52;
}

select, input {
  width: 100%;
  min-width: 0;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: white;
  color: var(--ink);
  padding: 9px 10px;
}

h1, h2, h3, h4, p {
  margin-top: 0;
}

h1 {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 0;
  font-size: 24px;
  line-height: 1.1;
  letter-spacing: 0;
}

h2 {
  margin-bottom: 6px;
  font-size: 22px;
  line-height: 1.15;
}

h3 {
  margin: 0 0 12px;
  font-size: 16px;
  line-height: 1.2;
}

h4 {
  margin: 0 0 6px;
  font-size: 15px;
}

label {
  display: grid;
  gap: 6px;
  margin-bottom: 12px;
  color: var(--muted);
  font-size: 13px;
  font-weight: 700;
}

.skip-link {
  position: fixed;
  left: 12px;
  top: 12px;
  z-index: 40;
  transform: translateY(-160%);
  background: var(--ink);
  color: white;
  padding: 9px 12px;
  border-radius: 7px;
}

.skip-link:focus {
  transform: translateY(0);
}

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: calc(12px + env(safe-area-inset-top)) 14px 12px;
  background: rgba(255, 255, 255, 0.96);
  border-bottom: 1px solid var(--line);
  position: sticky;
  top: 0;
  z-index: 12;
  backdrop-filter: blur(8px);
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--accent);
  font-size: 11px;
  font-weight: 800;
  text-transform: uppercase;
}

.brand-map-icon {
  width: 24px;
  height: 28px;
  flex: 0 0 auto;
}

.brand-map-path {
  fill: var(--soft);
  stroke: var(--accent);
  stroke-width: 14;
}

.secondary-menu {
  position: relative;
  flex: 0 0 auto;
}

.secondary-menu summary {
  min-height: 44px;
  display: grid;
  place-items: center;
  padding: 0 12px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: var(--surface);
  cursor: pointer;
  font-weight: 700;
}

.secondary-menu > div {
  position: absolute;
  right: 0;
  top: calc(100% + 8px);
  display: grid;
  gap: 8px;
  width: min(220px, calc(100vw - 28px));
  padding: 10px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.secondary-menu a {
  color: var(--accent);
  font-weight: 700;
  text-decoration: none;
  padding: 10px 8px;
}

main {
  width: min(100%, 1180px);
  margin: 0 auto;
  padding: 14px 14px 18px;
}

.notice {
  margin-bottom: 12px;
  padding: 12px 14px;
  background: var(--warn);
  border: 1px solid #b7dced;
  border-radius: 8px;
  color: #07365d;
}

.error {
  color: var(--danger);
  background: #fff0ef;
  border: 1px solid #edc2bf;
  border-radius: 8px;
  padding: 12px;
}

.view { display: none; }
.view.active { display: block; }

.panel, .component-card, .chart, .history-card, .group-card, .method-block, .table-card {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: 8px;
}

.panel {
  padding: 14px;
  box-shadow: var(--shadow);
}

.compact {
  box-shadow: none;
}

.section-head {
  display: grid;
  gap: 12px;
  margin: 4px 0 14px;
}

.section-head p {
  color: var(--muted);
  margin: 0;
}

.compact-head {
  align-items: end;
}

.inline-control, .scenario-control {
  margin: 0;
}

.active-filter-bar {
  position: sticky;
  top: calc(62px + env(safe-area-inset-top));
  z-index: 10;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr) auto;
  gap: 8px;
  align-items: center;
  margin-bottom: 12px;
  padding: 8px;
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(246, 251, 254, 0.96);
  backdrop-filter: blur(8px);
}

.filter-button, .small-action {
  min-height: 38px;
  padding: 7px 10px;
}

.chip-row {
  display: flex;
  gap: 6px;
  overflow-x: auto;
  scrollbar-width: none;
}

.chip-row::-webkit-scrollbar {
  display: none;
}

.chip {
  flex: 0 0 auto;
  min-height: 34px;
  max-width: min(280px, 70vw);
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  border-color: var(--line);
  background: var(--surface);
  color: var(--ink);
  padding: 6px 10px;
  font-size: 12px;
}

.muted-chip {
  display: inline-flex;
  align-items: center;
  color: var(--muted);
}

.summary-grid, .card-grid, .chart-grid, .history-card-grid, .group-explain-grid, .methods {
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
}

.map-summary {
  display: grid;
  gap: 12px;
  margin-bottom: 12px;
}

.map-wrap {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: linear-gradient(180deg, #ffffff, #eef8fc);
  aspect-ratio: 10 / 11;
  min-height: 0;
  padding: 12px;
  display: grid;
  place-items: center;
  overflow: hidden;
}

#countyMap {
  width: min(100%, 520px);
  height: auto;
  max-height: 100%;
}

.county {
  fill: #d7ecf6;
  stroke: #ffffff;
  stroke-width: 1.4;
  cursor: pointer;
  transition: fill .12s ease, stroke .12s ease;
}

.county:hover, .county:focus {
  fill: #b8dff0;
  outline: none;
}

.county.active {
  fill: var(--accent-2);
  stroke: var(--accent);
}

.metric, .group-card, .component-card, .chart, .history-card, .method-block {
  padding: 13px;
}

.metric strong {
  display: block;
  font-size: 24px;
  line-height: 1.1;
}

.metric span, .group-card span, .table-card span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.metric small, .group-card small {
  display: block;
  margin-top: 8px;
  color: var(--muted);
  font-size: 11px;
}

.component-head {
  display: flex;
  justify-content: space-between;
  gap: 10px;
}

.meta-line {
  margin: 12px 0 0;
  color: var(--muted);
  font-size: 11px;
  overflow-wrap: anywhere;
}

.source-note {
  margin-bottom: 12px;
}

.loading, .empty {
  color: var(--muted);
  padding: 18px;
  border: 1px dashed var(--line);
  border-radius: 8px;
  background: rgba(255, 255, 255, 0.72);
}

.mini-chart {
  display: grid;
  gap: 8px;
}

.county-row {
  display: grid;
  grid-template-columns: minmax(82px, 1fr) minmax(90px, 1.6fr) minmax(70px, auto);
  gap: 8px;
  align-items: center;
  width: 100%;
  min-height: 44px;
  border-color: var(--line);
  background: #fbfdff;
  color: var(--ink);
  text-align: left;
}

.bar-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  grid-template-areas:
    "label value"
    "track track";
  gap: 6px 10px;
  align-items: center;
  margin: 12px 0;
  font-size: 13px;
}

.bar-label {
  grid-area: label;
  min-width: 0;
  overflow-wrap: anywhere;
}

.bar-value {
  grid-area: value;
  white-space: nowrap;
  font-weight: 700;
}

.bar-track {
  grid-area: track;
  height: 12px;
  border-radius: 999px;
  background: #dcecf4;
  overflow: hidden;
}

.bar {
  display: block;
  height: 100%;
  background: linear-gradient(90deg, var(--accent), var(--accent-3));
}

.two-col {
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
  align-items: start;
}

.search-row {
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
  margin-bottom: 16px;
}

.profile-output {
  display: grid;
  gap: 14px;
}

.profile-title {
  display: grid;
  grid-template-columns: 1fr;
  gap: 14px;
  align-items: start;
}

.synthetic-label {
  display: inline-flex;
  margin: 0 0 8px;
  padding: 4px 8px;
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--accent);
  background: var(--soft);
  font-size: 12px;
  font-weight: 700;
}

.id-strip {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 12px;
}

.id-strip span {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.id-strip code {
  max-width: 100%;
  overflow-wrap: anywhere;
  padding: 6px 8px;
  border: 1px solid var(--line);
  border-radius: 7px;
  background: #fbfdff;
}

.copy-button {
  min-height: 34px;
  padding: 6px 10px;
}

.scenario-note {
  border-left: 3px solid var(--accent);
  padding-left: 12px;
  color: var(--muted);
}

.scenario-note strong {
  display: block;
  color: var(--ink);
  margin-bottom: 6px;
}

.profile-section {
  border-top: 1px solid var(--line);
  padding-top: 14px;
}

.status-line {
  margin-bottom: 12px;
  color: var(--muted);
  font-size: 13px;
}

.ballot-line {
  display: grid;
  grid-template-columns: minmax(76px, auto) minmax(0, 1fr);
  gap: 10px;
  margin: 8px 0;
  font-size: 13px;
}

.ballot-line span {
  color: var(--muted);
}

.method-details {
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 12px 14px;
  background: #fbfdff;
}

.method-details summary {
  min-height: 34px;
  cursor: pointer;
  font-weight: 700;
}

.kv {
  display: grid;
  grid-template-columns: 1fr;
  gap: 10px;
}

.details-kv {
  margin-top: 12px;
}

.kv div {
  min-width: 0;
  padding: 10px;
  border-radius: 8px;
  border: 1px solid var(--line);
  background: #fbfdff;
}

.kv dt {
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
}

.kv dd {
  margin: 4px 0 0;
  overflow-wrap: anywhere;
}

.mini-kv {
  display: grid;
  gap: 6px;
  margin: 10px 0 0;
  font-size: 12px;
}

.mini-kv div {
  display: grid;
  grid-template-columns: 108px minmax(0, 1fr);
  gap: 8px;
}

.mini-kv dt {
  color: var(--muted);
  font-weight: 700;
}

.mini-kv dd {
  margin: 0;
  overflow-wrap: anywhere;
}

.table-cards {
  display: grid;
  gap: 10px;
}

.table-card {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
  padding: 12px;
}

.table-card div {
  display: grid;
  gap: 2px;
  min-width: 0;
}

.table-card strong {
  overflow-wrap: anywhere;
}

.method-block pre {
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  font: inherit;
  color: var(--muted);
  max-height: 430px;
  overflow: auto;
}

.ask-explorer {
  margin-top: 14px;
}

.ask-explorer details {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: var(--surface);
  box-shadow: var(--shadow);
}

.ask-explorer summary {
  min-height: 48px;
  display: flex;
  align-items: center;
  padding: 0 14px;
  cursor: pointer;
  font-weight: 800;
}

.assistant-mode, .assistant-output {
  margin: 0 14px 12px;
  color: var(--muted);
}

.suggested-actions {
  display: grid;
  grid-template-columns: 1fr;
  gap: 8px;
  padding: 0 14px 12px;
}

.sheet-backdrop {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: rgba(0, 56, 101, 0.38);
}

.filter-sheet {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 21;
  max-height: min(86dvh, 720px);
  overflow-y: auto;
  padding: 14px 14px calc(18px + env(safe-area-inset-bottom));
  border: 1px solid var(--line);
  border-radius: 16px 16px 0 0;
  background: var(--surface);
  box-shadow: 0 -18px 44px rgba(0, 56, 101, 0.18);
  transform: translateY(110%);
  transition: transform .18s ease;
}

.filter-sheet[aria-hidden="false"] {
  transform: translateY(0);
}

.sheet-open {
  overflow: hidden;
}

.sheet-head {
  position: sticky;
  top: -14px;
  z-index: 1;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  margin: -14px -14px 12px;
  padding: 14px;
  background: var(--surface);
  border-bottom: 1px solid var(--line);
}

.split {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
}

.sheet-actions {
  position: sticky;
  bottom: calc(-18px - env(safe-area-inset-bottom));
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 8px;
  margin: 10px -14px calc(-18px - env(safe-area-inset-bottom));
  padding: 12px 14px calc(18px + env(safe-area-inset-bottom));
  background: var(--surface);
  border-top: 1px solid var(--line);
}

.bottom-nav {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 15;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
  padding: 8px 8px calc(8px + env(safe-area-inset-bottom));
  background: rgba(255, 255, 255, 0.96);
  border-top: 1px solid var(--line);
  backdrop-filter: blur(8px);
}

.bottom-nav button {
  min-width: 0;
  min-height: 48px;
  border-color: var(--line);
  background: var(--surface);
  color: var(--ink);
  padding: 8px 6px;
}

@media (min-width: 560px) {
  h1 {
    font-size: 28px;
  }

  .summary-grid, .card-grid, .history-card-grid, .group-explain-grid, .suggested-actions {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .kv, .table-card {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .search-row {
    grid-template-columns: minmax(0, 1fr) auto auto;
  }
}

@media (min-width: 780px) {
  main {
    padding: 22px 28px 28px;
  }

  .topbar {
    padding-left: 28px;
    padding-right: 28px;
  }

  .section-head {
    grid-template-columns: minmax(0, 1fr) minmax(220px, 280px);
    align-items: start;
  }

  .map-summary {
    grid-template-columns: minmax(280px, 42%) minmax(0, 1fr);
    align-items: start;
  }

  .chart-grid, .methods {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .profile-title {
    grid-template-columns: minmax(0, 1fr) minmax(220px, 320px);
  }

  .two-col {
    grid-template-columns: minmax(0, 1fr) 330px;
  }

  .filter-sheet {
    left: 50%;
    right: auto;
    width: min(720px, calc(100vw - 40px));
    transform: translate(-50%, 110%);
    border-radius: 16px 16px 0 0;
  }

  .filter-sheet[aria-hidden="false"] {
    transform: translate(-50%, 0);
  }

  .bottom-nav {
    left: 50%;
    right: auto;
    width: min(520px, calc(100vw - 24px));
    transform: translateX(-50%);
    border: 1px solid var(--line);
    border-bottom: 0;
    border-radius: 14px 14px 0 0;
  }
}

@media (min-width: 1060px) {
  .summary-grid {
    grid-template-columns: repeat(5, minmax(0, 1fr));
  }

  .kv {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}

/* Research assistant chat */
.chat-panel .panel-head { display:flex; justify-content:space-between; gap:12px; align-items:flex-start; flex-wrap:wrap; }
.chat-log { border:1px solid var(--line); border-radius:8px; background:var(--paper); padding:12px; min-height:220px; max-height:55vh; overflow-y:auto; display:flex; flex-direction:column; gap:10px; margin-top:12px; }
.chat-msg { padding:8px 10px; border-radius:8px; max-width:100%; }
.chat-user { background:var(--surface); border:1px solid var(--line); align-self:flex-end; }
.chat-assistant { background:#fff; border:1px solid var(--line); }
.chat-system { background:#f3f6f9; border:1px dashed var(--line); font-size:13px; }
.chat-role { font-size:11px; letter-spacing:.04em; text-transform:uppercase; color:var(--muted); margin-bottom:4px; }
.chat-body p { margin:0 0 6px; } .chat-body ul { margin:0 0 6px 18px; } .chat-body code { font-size:12px; }
.chat-meta { font-size:11px; color:var(--muted); margin-top:4px; }
.chat-proposals { margin-top:12px; padding:12px; border:1px solid #b7dcc0; background:#e8f4ea; border-radius:8px; }
.chat-proposals h4 { margin:0 0 8px; }
.proposal-card { background:#fff; border:1px solid var(--line); border-radius:6px; padding:10px; margin-bottom:8px; }
.proposal-detail { font-size:13px; margin-top:6px; }
.chat-form { margin-top:12px; } .chat-form textarea { width:100%; box-sizing:border-box; font:inherit; padding:8px; border:1px solid var(--line); border-radius:6px; }

/* Synthetic surveys */
.survey-blueprint { margin-top:12px; }
.survey-items { padding-left:20px; }
.survey-items li { margin-bottom:8px; }
.survey-items .survey-section { list-style:none; margin-left:-20px; margin-top:10px; }
.bottom-nav button { font-size:12px; }

/* Bring-your-own API key panel */
.key-panel { margin:12px 0; padding:12px; border:1px solid var(--line); border-radius:8px; background:var(--paper); }
.key-panel-head { display:flex; flex-direction:column; gap:4px; margin-bottom:8px; }
.key-panel-row { display:flex; flex-wrap:wrap; gap:8px; align-items:center; }
.key-panel-row input { flex:1 1 260px; font:inherit; padding:8px; border:1px solid var(--line); border-radius:6px; }
.key-help ol, .key-help ul { padding-left:20px; } .key-help li { margin-bottom:6px; } .key-help h4 { margin:14px 0 6px; }
