*,
*::before,
*::after {
  box-sizing: border-box;
}

:root {
  --bg: #f3f4f6;
  --surface: #ffffff;
  --text: #111827;
  --muted: #6b7280;
  --border: #e5e7eb;
  --primary: #1d4ed8;
  --primary-hover: #1e40af;
  --danger: #b91c1c;
  --radius: 10px;
  --shadow: 0 1px 3px rgba(0, 0, 0, 0.08), 0 1px 2px rgba(0, 0, 0, 0.04);
  --shadow-lg: 0 20px 50px rgba(0, 0, 0, 0.25);
  --modal-gap: 1rem;
  --sidebar-width: 280px;
}

html,
body {
  margin: 0;
  height: 100%;
}

body {
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.5;
}

body.modal-open {
  overflow: hidden;
}

h1,
h2,
h3,
p {
  margin: 0;
}

/* ---------- Page layout ---------- */

.app {
  min-height: 100%;
  display: grid;
  grid-template-rows: auto 1fr auto;
  max-width: 960px;
  margin: 0 auto;
  padding: 2rem 1rem;
  gap: 1.5rem;
}

.app-header h1 {
  font-size: 1.75rem;
}

.subtitle {
  color: var(--muted);
  margin-top: 0.25rem;
}

.app-main {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(300px, 1fr));
  gap: 1.5rem;
  align-items: start;
}

.app-footer {
  font-size: 0.8rem;
  color: var(--muted);
  text-align: center;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.25rem;
  display: grid;
  gap: 0.75rem;
}

.card h2 {
  font-size: 1.1rem;
}

/* ---------- Form ---------- */

.zip-form {
  display: grid;
  gap: 0.5rem;
}

.field-label {
  font-weight: 600;
  font-size: 0.9rem;
}

.input-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 0.5rem;
}

#zip-input {
  font: inherit;
  font-size: 1.1rem;
  letter-spacing: 0.1em;
  padding: 0.6rem 0.75rem;
  border: 1px solid #d1d5db;
  border-radius: 8px;
  min-width: 0;
}

#zip-input:focus {
  outline: 2px solid var(--primary);
  outline-offset: 1px;
  border-color: var(--primary);
}

#zip-input.is-invalid {
  border-color: var(--danger);
}

.hint {
  font-size: 0.85rem;
  color: var(--muted);
}

.error {
  font-size: 0.9rem;
  color: var(--danger);
  font-weight: 500;
}

/* ---------- Buttons ---------- */

.btn {
  font: inherit;
  font-weight: 600;
  border-radius: 8px;
  padding: 0.6rem 1rem;
  cursor: pointer;
  border: 1px solid transparent;
  transition: background-color 0.15s, border-color 0.15s;
}

.btn:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.btn-primary {
  background: var(--primary);
  color: #fff;
}

.btn-primary:hover:not(:disabled) {
  background: var(--primary-hover);
}

.btn-secondary {
  background: var(--surface);
  color: var(--text);
  border-color: #d1d5db;
}

.btn-secondary:hover:not(:disabled) {
  background: #f9fafb;
}

.link-btn {
  font: inherit;
  background: none;
  border: none;
  padding: 0;
  color: var(--primary);
  text-decoration: underline;
  cursor: pointer;
}

.icon-btn {
  background: none;
  border: none;
  font-size: 1.75rem;
  line-height: 1;
  width: 2.25rem;
  height: 2.25rem;
  border-radius: 6px;
  cursor: pointer;
  color: var(--muted);
}

.icon-btn:hover {
  background: var(--bg);
  color: var(--text);
}

:focus-visible {
  outline: 2px solid var(--primary);
  outline-offset: 2px;
}

/* ---------- Swatches ---------- */

.swatch {
  display: inline-block;
  width: 14px;
  height: 14px;
  border-radius: 3px;
  flex-shrink: 0;
}

/* ---------- Modal ---------- */

.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 2000;
  background: rgba(17, 24, 39, 0.55);
}

/* `display` rules elsewhere would otherwise override the [hidden] attribute. */
.modal-overlay[hidden] {
  display: none;
}

.modal {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: min(960px, calc(100vw - 2 * var(--modal-gap)));
  height: min(640px, calc(100vh - 2 * var(--modal-gap)));
  height: min(640px, calc(100dvh - 2 * var(--modal-gap)));
  display: grid;
  grid-template-rows: auto minmax(0, 1fr) auto;
  background: var(--surface);
  border-radius: var(--radius);
  box-shadow: var(--shadow-lg);
  overflow: hidden;
}

.modal-header {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: start;
  gap: 1rem;
  padding: 1rem 1.25rem;
  border-bottom: 1px solid var(--border);
}

.modal-header h2 {
  font-size: 1.15rem;
}

.modal-desc {
  color: var(--muted);
  font-size: 0.9rem;
}

.modal-body {
  display: grid;
  grid-template-columns: minmax(0, 1fr) var(--sidebar-width);
  min-height: 0;
}

.map-container {
  position: relative;
  width: 100%;
  height: 100%;
  min-height: 280px;
  background: #e5e7eb;
}

.map-container.map-unavailable {
  display: grid;
  place-items: center;
  padding: 1rem;
  color: var(--muted);
  text-align: center;
}

.sidebar {
  display: grid;
  grid-template-rows: auto auto minmax(0, 1fr) auto;
  gap: 0.5rem;
  padding: 1rem;
  border-left: 1px solid var(--border);
  min-height: 0;
}

.sidebar h3 {
  font-size: 1rem;
}

.sidebar-hint {
  font-size: 0.85rem;
  color: var(--muted);
}

.district-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 0.5rem;
  align-content: start;
  overflow-y: auto;
  min-height: 0;
}

.district-btn {
  width: 100%;
  display: grid;
  grid-template-columns: auto 1fr;
  align-items: center;
  gap: 0.6rem;
  padding: 0.6rem 0.75rem;
  font: inherit;
  text-align: left;
  background: var(--surface);
  border: 2px solid var(--border);
  border-radius: 8px;
  cursor: pointer;
  transition: border-color 0.15s, background-color 0.15s;
}

.district-btn:hover,
.district-btn.is-hovered {
  border-color: #9ca3af;
  background: #f9fafb;
}

.district-btn.is-selected {
  border-color: var(--primary);
  background: #eff6ff;
}

.district-btn-text {
  display: grid;
}

.district-btn-text span {
  font-size: 0.8rem;
  color: var(--muted);
}

.selection-status {
  font-size: 0.85rem;
  font-weight: 600;
  min-height: 1.25em;
}

.modal-footer {
  display: flex;
  justify-content: flex-end;
  gap: 0.5rem;
  padding: 0.75rem 1.25rem;
  border-top: 1px solid var(--border);
}

/* ---------- Candidate popup ---------- */

/* `.modal.` raises specificity above the small-screen `.modal` height rule. */
.modal.modal-compact {
  width: min(520px, calc(100vw - 2 * var(--modal-gap)));
  height: auto;
  max-height: calc(100vh - 2 * var(--modal-gap));
  max-height: calc(100dvh - 2 * var(--modal-gap));
  display: flex;
  flex-direction: column;
}

.modal-eyebrow {
  font-size: 0.8rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  color: var(--primary);
}

.candidate-body {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  padding: 1rem 1.25rem;
}

.candidate-list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: grid;
  gap: 0.5rem;
}

.candidate-list[hidden] {
  display: none;
}

.candidate {
  display: grid;
  gap: 0.25rem;
  padding: 0.75rem;
  border: 1px solid var(--border);
  border-radius: 8px;
}

.candidate-main {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 0.5rem;
}

.candidate-section {
  margin-top: 0.5rem;
  font-size: 0.8rem;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--muted);
}

.candidate-section:first-child {
  margin-top: 0;
}

.candidate-message {
  font-size: 0.9rem;
  color: var(--muted);
}

.candidate-message.is-error {
  color: #b91c1c;
}

.candidate-name {
  font-weight: 600;
}

.candidate-office {
  font-size: 0.9rem;
}

.candidate-office-label {
  color: var(--muted);
}

.party-badge {
  font-size: 0.75rem;
  font-weight: 600;
  padding: 0.1rem 0.5rem;
  border-radius: 999px;
  background: #f3f4f6;
  color: #374151;
}

.party-badge.party-dem {
  background: #dbeafe;
  color: #1e40af;
}

.party-badge.party-rep {
  background: #fee2e2;
  color: #991b1b;
}

/* ---------- Small screens ---------- */

@media (max-width: 700px) {
  :root {
    --modal-gap: 0.5rem;
  }

  .modal {
    height: calc(100vh - 2 * var(--modal-gap));
    height: calc(100dvh - 2 * var(--modal-gap));
  }

  .modal-body {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(220px, 1fr) auto;
  }

  .sidebar {
    border-left: none;
    border-top: 1px solid var(--border);
    max-height: 40vh;
  }

  .map-container {
    min-height: 220px;
  }
}

/* Candidate Popup Component Styles */
.candidate-modal-header {
    background-color: #0071e3;
    color: white;
    padding: 20px;
    border-radius: 8px 8px 0 0;
}

.candidate-modal-header h3 {
    margin: 0 0 6px 0;
    font-size: 20px;
}

.candidate-modal-header p {
    margin: 0;
    font-size: 14px;
    opacity: 0.9;
}

.candidate-roster-list {
    padding: 20px;
    max-height: 400px;
    overflow-y: auto;
    display: flex;
    flex-direction: column;
    gap: 12px;
}

.candidate-entry-card {
    background: #f5f5f7;
    border-left: 5px solid #86868b;
    padding: 14px;
    border-radius: 4px;
    display: flex;
    justify-content: space-between;
    align-items: center;
}

.candidate-entry-card.democrat { border-left-color: #00aeef; }
.candidate-entry-card.republican { border-left-color: #e23d28; }
.candidate-entry-card.independent { border-left-color: #61b329; }

.candidate-meta h5 {
    margin: 0 0 4px 0;
    font-size: 16px;
    color: #1d1d1f;
}

.candidate-meta span {
    font-size: 13px;
    color: #86868b;
    text-transform: uppercase;
    font-weight: 600;
}

.office-tag {
    font-size: 12px;
    background: #e8e8ed;
    color: #424245;
    padding: 4px 10px;
    border-radius: 12px;
    font-weight: 500;
}

