:root {
  --bg: #f5f2ea;
  --ink: #1d2433;
  --muted: #586174;
  --card: #ffffff;
  --accent: #0f766e;
  --accent-soft: #e2f3f0;
  --focus: #ffbf1f;
  --radius: 12px;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  color: var(--ink);
  background: var(--bg);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

body { margin: 0; }

.app { max-width: 1240px; margin: 0 auto; padding: 16px; }
.app-content { container-type: inline-size; }

h1 {
  margin: 0.5rem 0 0.25rem;
  text-align: center;
  font-size: clamp(1.45rem, 4vw, 2.4rem);
  line-height: 1.15;
}

h2 { margin: 0 0 0.5rem; font-size: 1.1rem; }

.state-caption { margin: 0 0 1rem; text-align: center; color: var(--muted); }
.state-caption a { color: inherit; }

.toolbar { display: flex; flex-wrap: wrap; justify-content: center; gap: 0.5rem; margin-bottom: 0.75rem; }

button { font: inherit; }

button:focus-visible,
.district-row:focus-visible {
  outline: 3px solid var(--focus);
  outline-offset: 2px;
}

.matchup-toggle,
.new-map-button {
  min-height: 44px;
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  padding: 0.55rem 1.15rem;
  border: 2px solid var(--ink);
  border-radius: 999px;
  background: var(--card);
  color: var(--ink);
  font-weight: 650;
  cursor: pointer;
}
.matchup-toggle::before { content: "⇄"; }
.matchup-toggle:hover,
.new-map-button:hover { background: #ece8dc; }
.new-map-button::before { content: "↻"; }

.editing-status {
  margin: 0 auto 0.75rem;
  max-width: 720px;
  padding: 0.5rem 0.9rem;
  border-radius: var(--radius);
  background: var(--accent);
  color: #fff;
  text-align: center;
  font-weight: 600;
}

.edit-message {
  margin: 0 auto 0.75rem;
  max-width: 720px;
  padding: 0.5rem 0.9rem;
  border: 2px solid #b3261e;
  border-radius: var(--radius);
  background: #fde8e8;
  color: #7a1a14;
  text-align: center;
  font-weight: 600;
}

.error-message {
  margin: 1rem auto;
  max-width: 520px;
  padding: 1rem;
  border-radius: var(--radius);
  background: #fde8e8;
  text-align: center;
}

.map-layout {
  display: grid;
  grid-template-columns: minmax(0, 1fr);
  gap: 16px;
  align-items: start;
}

@container (min-width: 760px) {
  .map-layout { grid-template-columns: minmax(0, 1fr) 260px; }
}

.map-frame,
.control-total,
.plan-status,
.legend,
.districts {
  margin: 0;
  border-radius: var(--radius);
  background: var(--card);
  box-shadow: 0 1px 3px rgb(0 0 0 / 0.12);
}

.map-frame { padding: 8px; }
.state-map { display: block; width: 100%; height: auto; }

.county {
  stroke: #ffffff;
  stroke-width: 0.8;
  cursor: pointer;
  transition: opacity 0.15s ease;
}
.county:focus { outline: none; }
.is-editing .county { cursor: copy; }
.county.dimmed { opacity: 0.4; }
/* Inside a revealed district, thin white seams separate the counties' own colors. */
.county[data-revealed="true"] { stroke: #ffffff; stroke-width: 1; }

.county-highlight { fill: none; stroke: var(--focus); stroke-width: 3.5; }
.district-border { stroke: var(--ink); stroke-width: 2.6; stroke-linecap: round; }
.district-border.selected-border { stroke: var(--accent); stroke-width: 4.5; }
.state-outline { fill: none; stroke: var(--ink); stroke-width: 3; stroke-linejoin: round; }

.badge circle { stroke: #ffffff; stroke-width: 2; }
.badge text { font: 700 12px system-ui, sans-serif; }
.badge.selected circle { stroke: var(--focus); stroke-width: 3; }

.map-side { display: flex; flex-direction: column; gap: 16px; }
.control-total,
.plan-status,
.legend { padding: 12px 14px; }

.plan-overall { margin: 0 0 0.4rem; font-weight: 700; color: #7a1a14; }
.plan-overall[data-valid="true"] { color: #1b6e2d; }
.balance-result { margin: 0 0 0.6rem; font-weight: 600; color: #7a1a14; }
.balance-result[data-pass="true"] { color: #1b6e2d; }
.plan-rows {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 4px 8px;
  margin: 0;
  font-size: 0.9rem;
}
.plan-rows dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; font-weight: 600; }

.unassigned-overlay { opacity: 1; }

.control-summary { margin: 0 0 0.6rem; font-weight: 600; }

.legend-rows {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 6px 8px;
  align-items: center;
  margin: 0;
}
.legend-rows dt > .swatch { justify-self: end; }

.control-rows {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 6px 8px;
  align-items: center;
  margin: 0;
}
.control-rows dt,
.legend-rows dt { display: contents; }
.control-rows dd,
.legend-rows dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }
.control-count { font-weight: 700; }

.legend-shades { display: flex; gap: 2px; }
.legend-shades span { width: 16px; height: 14px; border-radius: 2px; }
.legend-note { margin: 0.6rem 0 0; color: var(--muted); font-size: 0.8rem; }

.swatch {
  display: inline-block;
  flex: none;
  width: 18px;
  height: 18px;
  border: 1px solid rgb(0 0 0 / 0.2);
  border-radius: 4px;
}
.swatch.empty { background: repeating-linear-gradient(45deg, #fff 0 3px, #c8c8c8 3px 6px); }

.districts { margin-top: 16px; padding: 14px; }
.hint { margin: 0 0 0.75rem; color: var(--muted); font-size: 0.9rem; }

.district-list {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
  gap: 8px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.district-row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  border: 2px solid #e4e1d8;
  border-radius: 10px;
  background: #fff;
}
.district-row.selected { border-color: var(--accent); background: var(--accent-soft); }
.district-name { font-weight: 700; white-space: nowrap; }
.district-status { color: var(--muted); font-size: 0.85rem; }
.district-balance { order: 10; flex-basis: 100%; padding-left: 26px; font-size: 0.8rem; font-weight: 600; color: var(--muted); }
.district-balance[data-balance="ok"] { color: #1b6e2d; }
.district-balance[data-balance="over"],
.district-balance[data-balance="under"] { color: #9a4b00; }

.pen {
  display: inline-grid;
  place-items: center;
  flex: none;
  width: 34px;
  height: 34px;
  margin-left: auto;
  padding: 0;
  border: 2px solid var(--ink);
  border-radius: 8px;
  background: #fff;
  color: var(--ink);
  cursor: pointer;
}
.pen svg { width: 18px; height: 18px; }
.pen[aria-pressed="true"] { border-color: var(--accent); background: var(--accent); color: #fff; }

.notice { margin: 1rem 0; color: var(--muted); font-size: 0.85rem; text-align: center; }

.tooltip {
  position: fixed;
  z-index: 20;
  top: 0;
  left: 0;
  max-width: min(320px, calc(100vw - 16px));
  padding: 10px 12px;
  border-radius: 8px;
  background: var(--ink);
  color: #fff;
  font-size: 0.85rem;
  line-height: 1.35;
  pointer-events: none;
  box-shadow: 0 6px 18px rgb(0 0 0 / 0.25);
}
.tt-section + .tt-section { margin-top: 8px; padding-top: 8px; border-top: 1px solid rgb(255 255 255 / 0.25); }
.tt-title { font-weight: 700; }
.tt-subtitle { margin-top: 2px; color: #cfd5e2; }
.tt-rows { display: grid; grid-template-columns: auto auto; gap: 2px 12px; margin: 6px 0 0; }
.tt-rows dt { display: flex; align-items: center; gap: 6px; }
.tt-rows dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; }
.tt-rows .swatch { width: 12px; height: 12px; border-color: rgb(255 255 255 / 0.6); }

@media (max-width: 480px) {
  .app { padding: 10px; }
  .map-frame { padding: 4px; }
}
