/* ============================================================
   CSS Custom Properties — light + dark themes
   ============================================================ */
:root {
  --bg:           #f8fafc;
  --surface:      #ffffff;
  --surface-2:    #f1f5f9;
  --border:       #e2e8f0;
  --text:         #1a202c;
  --text-muted:   #64748b;
  --accent:       #2563eb;
  --accent-hover: #1d4ed8;
  --geo-bg:       #dbeafe;
  --geo-text:     #1e40af;
  --county-bg:    #dcfce7;
  --county-text:  #166534;
  --shadow:       0 1px 3px rgba(0,0,0,.08), 0 1px 2px rgba(0,0,0,.05);
  --shadow-md:    0 4px 6px -1px rgba(0,0,0,.08);
  --radius:       8px;
}

[data-theme="dark"] {
  --bg:           #0f172a;
  --surface:      #1e293b;
  --surface-2:    #0f172a;
  --border:       #334155;
  --text:         #e2e8f0;
  --text-muted:   #94a3b8;
  --accent:       #60a5fa;
  --accent-hover: #93c5fd;
  --geo-bg:       #1e3a5f;
  --geo-text:     #93c5fd;
  --county-bg:    #14532d;
  --county-text:  #86efac;
  --shadow:       0 1px 3px rgba(0,0,0,.3);
  --shadow-md:    0 4px 8px rgba(0,0,0,.4);
}

/* ============================================================
   Reset & Base
   ============================================================ */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html  { scroll-behavior: smooth; -webkit-text-size-adjust: 100%; }
body  {
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  background: var(--bg);
  color: var(--text);
  line-height: 1.6;
  transition: background .2s, color .2s;
}
a { color: var(--accent); }
a:hover { color: var(--accent-hover); }

/* ============================================================
   Sticky Header
   ============================================================ */
.site-header {
  position: sticky;
  top: 0;
  z-index: 100;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
  box-shadow: var(--shadow);
  transition: box-shadow .2s;
}
.site-header.compact { box-shadow: var(--shadow-md); }

.header-inner {
  max-width: 1100px;
  margin: 0 auto;
  padding: 1rem 1.25rem .5rem;
  transition: padding .2s;
}
.site-header.compact .header-inner { padding-top: .5rem; }

.header-title-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  margin-bottom: .6rem;
}

.header-title-group {
  display: flex;
  align-items: baseline;
  gap: .5rem;
  min-width: 0;
}

.site-title {
  font-size: 1.35rem;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: font-size .2s;
}
.site-header.compact .site-title { font-size: 1rem; }

.header-county-label {
  font-size: .85rem;
  color: var(--text-muted);
  white-space: nowrap;
}

.theme-toggle {
  flex-shrink: 0;
  width: 36px;
  height: 36px;
  border-radius: 50%;
  border: 1px solid var(--border);
  background: var(--surface-2);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1rem;
  transition: background .15s;
}
.theme-toggle:hover { background: var(--border); }

.header-controls {
  display: flex;
  flex-wrap: wrap;
  gap: .75rem;
  align-items: center;
  margin-bottom: .6rem;
}

.control-group {
  display: flex;
  align-items: center;
  gap: .4rem;
}

.control-group label {
  font-size: .72rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--text-muted);
  white-space: nowrap;
}

#county-select {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text);
  border-radius: var(--radius);
  padding: .3rem .55rem;
  font-size: .875rem;
  cursor: pointer;
}

.scope-control-group {
  display: flex;
  flex-direction: column;
  gap: .35rem;
}

.scope-tabs {
  display: flex;
  gap: 0;
  border-bottom: 2px solid var(--border);
}

.scope-tab {
  background: transparent;
  border: none;
  border-bottom: 2px solid transparent;
  margin-bottom: -2px;
  color: var(--text-muted);
  padding: .3rem .75rem;
  font-size: .8rem;
  font-weight: 600;
  cursor: pointer;
  white-space: nowrap;
  transition: color .15s, border-color .15s;
}
.scope-tab:hover:not(:disabled)  { color: var(--accent); }
.scope-tab.active                 { color: var(--accent); border-bottom-color: var(--accent); }
.scope-tab.disabled,
.scope-tab:disabled               { color: var(--text-muted); opacity: .4; cursor: not-allowed; }

.geo-filter-group {
  display: flex;
  gap: .25rem;
  flex-wrap: wrap;
}

.geo-btn {
  background: var(--surface-2);
  border: 1px solid var(--border);
  color: var(--text-muted);
  border-radius: 999px;
  padding: .22rem .6rem;
  font-size: .75rem;
  font-weight: 600;
  cursor: pointer;
  transition: border-color .15s, color .15s, background .15s;
  white-space: nowrap;
}
.geo-btn:hover  { border-color: var(--accent); color: var(--accent); }
.geo-btn.active { background: var(--accent); border-color: var(--accent); color: #fff; }

.section-nav {
  display: flex;
  gap: .15rem;
  overflow-x: auto;
  padding-bottom: .4rem;
  scrollbar-width: thin;
  -webkit-overflow-scrolling: touch;
}

.nav-link {
  color: var(--text-muted);
  text-decoration: none;
  font-size: .78rem;
  font-weight: 500;
  white-space: nowrap;
  padding: .2rem .5rem;
  border-radius: 4px;
  transition: color .12s, background .12s;
}
.nav-link:hover { color: var(--accent); background: var(--surface-2); }

/* ============================================================
   Main Content
   ============================================================ */
#main-content {
  max-width: 1100px;
  margin: 0 auto;
  padding: 1.5rem 1.25rem 3rem;
}

.page-intro {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  padding: 1.25rem 1.5rem;
  margin-bottom: 1.75rem;
}

.data-note {
  margin-top: .4rem;
  font-size: .8rem;
  color: var(--text-muted);
}

/* ============================================================
   Chart Sections
   ============================================================ */
.chart-section {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  box-shadow: var(--shadow);
  margin-bottom: 1.5rem;
  /* No `overflow: hidden` — would break position:sticky on table headers
     inside .table-scroll. Rounded corners are kept by giving the inner
     header/footer their own border-radii where the corners meet. */
  scroll-margin-top: 130px;  /* fallback; charts.js measures live header height */
}
.chart-section .section-header { border-top-left-radius: var(--radius); border-top-right-radius: var(--radius); }
.chart-section .section-footer { border-bottom-left-radius: var(--radius); border-bottom-right-radius: var(--radius); }

.section-header {
  padding: 1.1rem 1.5rem .75rem;
  border-bottom: 1px solid var(--border);
}

.section-meta {
  display: flex;
  gap: .35rem;
  flex-wrap: wrap;
  margin-bottom: .35rem;
}

.geo-tag, .county-tag {
  font-size: .68rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .05em;
  padding: .12rem .45rem;
  border-radius: 999px;
}
.geo-tag    { background: var(--geo-bg);    color: var(--geo-text); }
.county-tag { background: var(--county-bg); color: var(--county-text); }

.section-title {
  font-size: 1.05rem;
  font-weight: 700;
  margin-bottom: .2rem;
}

.section-description {
  font-size: .85rem;
  color: var(--text-muted);
}

.chart-wrapper {
  padding: 1.25rem 1.5rem;
  /* Slot lock: reserve full chart height before the chart paints so the
     section card is the same size whether it shows the loading placeholder
     or a fully-rendered chart. Eliminates the layout shift that was making
     county switching feel disruptive. 430 = 380 (canvas max) + 50 (padding
     and breathing room). */
  min-height: 430px;
}

.chart-wrapper canvas { max-height: 380px; }

.chart-loading {
  text-align: center;
  color: var(--text-muted);
  font-size: .875rem;
  padding: 2rem 0;
}

.section-footer {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: .55rem 1.5rem;
  background: var(--surface-2);
  border-top: 1px solid var(--border);
}

.updated-label { font-size: .72rem; color: var(--text-muted); }
.data-link     { font-size: .72rem; text-decoration: none; }
.data-link:hover { text-decoration: underline; }

/* ============================================================
   Data Table
   ============================================================ */
.table-scroll {
  /* Slot lock: fixed height (max == min) so the table section is the same
     size whether it shows the loading placeholder, an 8-row small-county
     table, or a 1,200-row Cuyahoga precinct table. Internal scroll lets the
     user reach every row, page geometry stays stable across counties, and
     chart-to-chart anchoring math stays accurate. */
  height: 60vh;
  max-height: 60vh;
  min-height: 60vh;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
}

.data-table {
  width: 100%;
  border-collapse: separate;   /* needed so sticky thead's border doesn't disappear when scrolling */
  border-spacing: 0;
  font-size: .85rem;
}

.data-table th {
  position: sticky;
  top: 0;
  z-index: 2;
  background: var(--surface-2);
  color: var(--text);
  font-weight: 700;
  text-align: left;
  padding: .55rem .85rem;
  border-bottom: 2px solid var(--border);
  white-space: nowrap;
  box-shadow: inset 0 -2px 0 var(--border);
}

.data-table td {
  padding: .45rem .85rem;
  border-bottom: 1px solid var(--border);
  color: var(--text);
  white-space: nowrap;
}

.row-even td { background: var(--surface); }
.row-odd  td { background: var(--surface-2); }

.data-table tr:last-child td { border-bottom: none; }

/* ============================================================
   Table Pagination
   ============================================================ */
.table-pagination {
  display: flex;
  align-items: center;
  gap: .75rem;
  padding: .55rem .85rem;
  border-top: 1px solid var(--border);
  background: var(--surface-2);
  font-size: .8rem;
}

.table-pagination button {
  padding: .3rem .75rem;
  border: 1px solid var(--border);
  border-radius: 4px;
  background: var(--surface);
  color: var(--text);
  cursor: pointer;
  font-size: .8rem;
  transition: background .15s;
}

.table-pagination button:hover:not(:disabled) {
  background: var(--accent, #4a90d9);
  color: #fff;
  border-color: var(--accent, #4a90d9);
}

.table-pagination button:disabled {
  opacity: .4;
  cursor: default;
}

.pagination-label {
  flex: 1;
  text-align: center;
  color: var(--text-muted);
}
  white-space: nowrap;
  box-shadow: inset 0 -2px 0 var(--border);   /* preserve underline when border collapses with sticky */
}

.data-table td {
  padding: .45rem .85rem;
  border-bottom: 1px solid var(--border);
}

.row-even { background: var(--surface); }
.row-odd  { background: var(--surface-2); }

/* ============================================================
   Error State
   ============================================================ */
.error {
  padding: 1rem 1.25rem;
  border-radius: var(--radius);
  font-size: .875rem;
  color: #dc2626;
  background: #fef2f2;
  border: 1px solid #fecaca;
}
[data-theme="dark"] .error {
  background: #450a0a;
  color: #fca5a5;
  border-color: #7f1d1d;
}

/* ============================================================
   Footer
   ============================================================ */
.site-footer {
  margin-top: 3rem;
  padding: 1.5rem;
  text-align: center;
  font-size: .78rem;
  color: var(--text-muted);
  border-top: 1px solid var(--border);
}

/* ============================================================
   Responsive — mobile
   ============================================================ */
@media (max-width: 600px) {
  .header-inner   { padding: .75rem .85rem .4rem; }
  .site-title     { font-size: 1.05rem; }
  #main-content   { padding: 1rem .85rem 2rem; }
  .section-header { padding: .9rem 1rem .65rem; }
  .chart-wrapper  { padding: 1rem; min-height: 320px; }
  .section-footer { padding: .5rem 1rem; }
  .chart-wrapper canvas { max-height: 280px; }
  .table-scroll   { height: 50vh; max-height: 50vh; min-height: 50vh; }
}

/* ============================================================
   Custom doughnut legend + export button
   ============================================================ */

/* Flex shell: legend left, canvas right (reverses on mobile) */
.chart-legend-flex {
  display: flex;
  flex-direction: row;
  align-items: flex-start;
  gap: 20px;
  width: 100%;
}

.chart-canvas-holder {
  flex: 1 1 auto;
  min-width: 0;
}

.chart-canvas-holder canvas {
  width: 100% !important;
  height: auto !important;
}

/* 3-column legend grid */
.chart-custom-legend {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px 14px;
  flex: 0 0 auto;
  width: 360px;
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  font-size: .78rem;
  color: var(--text);
  align-self: flex-start;
  padding: 6px 0;
}

.cl-col {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.cl-heading {
  font-size: .68rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: .05em;
  color: var(--text-muted);
}

.cl-divider {
  height: 1px;
  background: var(--border);
  margin: 1px 0 3px;
}

.cl-item {
  display: flex;
  align-items: flex-start;
  gap: 6px;
  line-height: 1.35;
}

.cl-swatch {
  display: inline-block;
  width: 11px;
  height: 11px;
  border-radius: 2px;
  flex-shrink: 0;
  margin-top: 2px;
}

.cl-text {
  font-size: .73rem;
  color: var(--text);
}

/* Export button */
.chart-export-btn {
  display: block;
  margin: 10px 0 0 auto;
  padding: 3px 10px;
  font-size: .72rem;
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  background: transparent;
  color: var(--text-muted);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  cursor: pointer;
  transition: color .15s, border-color .15s;
}
.chart-export-btn:hover {
  color: var(--accent);
  border-color: var(--accent);
}

/* Mobile: stack vertically, legend below chart */
@media (max-width: 767px) {
  .chart-legend-flex {
    flex-direction: column;
  }
  .chart-custom-legend {
    width: 100%;
    grid-template-columns: repeat(3, 1fr);
    font-size: .7rem;
  }
  .cl-text {
    font-size: .66rem;
  }
}

/* ============================================================
   Jurisdiction — data-gap note + unavailability panel
   ============================================================ */

/* Persistent info banner shown whenever type = Cities */
#jur-data-note {
  margin-bottom: 1.25rem;
}

.jur-data-note-inner {
  background: #eff6ff;
  border: 1px solid #bfdbfe;
  border-left: 4px solid var(--accent);
  border-radius: var(--radius);
  padding: .85rem 1.1rem;
  font-size: .875rem;
  color: var(--text);
  line-height: 1.55;
}

.jur-data-note-inner code {
  font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
  font-size: .82em;
  background: #dbeafe;
  color: #1e40af;
  padding: .1em .35em;
  border-radius: 3px;
}

.jur-data-note-inner details {
  margin-top: .5rem;
}

.jur-data-note-inner summary {
  cursor: pointer;
  font-size: .8rem;
  color: var(--accent);
  font-weight: 600;
  user-select: none;
}

.jur-data-note-inner details[open] summary {
  margin-bottom: .35rem;
}

.jur-data-note-inner details span {
  display: block;
  font-size: .82rem;
  color: var(--text-muted);
  padding-left: .75rem;
  border-left: 2px solid var(--border);
  line-height: 1.7;
}

/* Dark mode overrides for the info note */
[data-theme="dark"] .jur-data-note-inner {
  background: #0f2040;
  border-color: #1e3a5f;
  border-left-color: var(--accent);
  color: var(--text);
}
[data-theme="dark"] .jur-data-note-inner code {
  background: #1e3a5f;
  color: var(--accent);
}

/* Panel rendered when a gap-county entry is selected from the dropdown */
.jur-unavailable-panel {
  background: #fffbeb;
  border: 1px solid #fde68a;
  border-left: 4px solid #f59e0b;
  border-radius: var(--radius);
  padding: 1.5rem 1.75rem;
  margin-top: .5rem;
}

.jur-unavailable-panel h3 {
  font-size: 1.05rem;
  font-weight: 700;
  margin-bottom: .65rem;
  color: var(--text);
}

.jur-unavailable-panel p {
  font-size: .875rem;
  color: var(--text);
  line-height: 1.6;
  margin-bottom: .55rem;
}

.jur-unavailable-panel p:last-child {
  margin-bottom: 0;
}

.jur-unavailable-panel code {
  font-family: 'SFMono-Regular', Consolas, 'Liberation Mono', Menlo, monospace;
  font-size: .82em;
  background: #fde68a;
  color: #92400e;
  padding: .1em .35em;
  border-radius: 3px;
}

.jur-unavailable-note {
  font-size: .8rem !important;
  color: var(--text-muted) !important;
  font-style: italic;
}

/* Dark mode overrides for the warning panel */
[data-theme="dark"] .jur-unavailable-panel {
  background: #1c1407;
  border-color: #78350f;
  border-left-color: #f59e0b;
}
[data-theme="dark"] .jur-unavailable-panel h3,
[data-theme="dark"] .jur-unavailable-panel p {
  color: var(--text);
}
[data-theme="dark"] .jur-unavailable-panel code {
  background: #451a03;
  color: #fbbf24;
}
