html, body {
  height: 100%;
  margin: 0;
  font-family: system-ui, -apple-system, Segoe UI, Roboto, Arial, sans-serif;
}

body {
  height: 100dvh;                 /* lock to visible viewport on mobile */
  display: flex;                  /* REQUIRED */
  flex-direction: column;         /* REQUIRED */
  min-height: 0;                  /* allows children to shrink */
}

.skip-link {
  position: fixed;
  top: 8px;
  left: 8px;
  z-index: 10000;
  padding: 10px 14px;
  border: 2px solid #522d80;
  border-radius: 4px;
  background: #fff;
  color: #3f1c66;
  font-weight: 700;
  transform: translateY(calc(-100% - 12px));
}

.skip-link:focus {
  outline: 3px solid #522d80;
  outline-offset: 2px;
  transform: translateY(0);
}

.app-main {
  display: flex;
  flex: 1 1 0;
  flex-direction: column;
  min-height: 0;
}

.visually-hidden {
  position: absolute !important;
  width: 1px !important;
  height: 1px !important;
  padding: 0 !important;
  margin: -1px !important;
  overflow: hidden !important;
  clip: rect(0, 0, 0, 0) !important;
  white-space: nowrap !important;
  border: 0 !important;
}

.draft-status-banner {
  flex: 0 0 auto;
  box-sizing: border-box;
  width: 100%;
  padding: 7px 12px;
  border-bottom: 1px solid #d7aa38;
  background: #fff3cd;
  color: #5d4300;
  font-size: 13px;
  line-height: 1.35;
  text-align: center;
}

#map {
  /* let map occupy the remaining height automatically */
  width: 100%;
  flex: 1 1 0;
  min-height: 0; /* optional guard */
  height: 100%;
}

/* Capture wrapper */
#mapArea {
  position: relative;
  width: 100%;
  flex: 1 1 0;
  min-height: 0;
}

.topbar {
  min-height: 0;
  height: auto;
  border-bottom: 1px solid #ddd;
  background: #fafafa;
  position: relative;
}

.topbar-scroll {
  min-height: 0;
  height: auto;

  display: flex;
  align-items: center;
  gap: 16px;
  padding: 10px 12px;
  flex-wrap: wrap;

  /* this becomes the scroll container */
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;

  width: 100%;
  box-sizing: border-box;
}

.topbar-fade {
  position: absolute;
  left: 0;
  right: 0;
  height: 26px;
  pointer-events: none;
  opacity: 0;
  transition: opacity 180ms ease;
  z-index: 5; /* above controls inside topbar */
}

.topbar-fade.top {
  top: 0;
  background: linear-gradient(
    to bottom,
    rgba(0,0,0,0.16),
    rgba(0,0,0,0)
  );
}

.topbar-fade.bottom {
  bottom: 0;
  background: linear-gradient(
    to top,
    rgba(0,0,0,0.16),
    rgba(0,0,0,0)
  );
}

/* Only show fades when the topbar is actually scrollable */
.topbar:not(.is-scrollable) .topbar-fade { opacity: 0 !important; }

.topbar.show-fade-top .topbar-fade.top { opacity: 1; }
.topbar.show-fade-bottom .topbar-fade.bottom { opacity: 1; }

.title {
  display: flex;
  flex-direction: column;
  line-height: 1.1;
  min-width: 200px;
  align-self: flex-start;
  flex: 0 1 auto;    /* shrink if needed */
}
.title h1 {
  margin: 0;
  font-size: inherit;
  line-height: inherit;
}
.subtitle {
  font-size: 12px;
  color: #555;
}

/* Make subtitle feel clickable */
#pageSubtitleText {
  align-self: flex-start;
  max-width: 100%;
  padding: 0;
  border: 0;
  background: transparent;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
  text-decoration: underline;
  text-underline-offset: 2px;
}

#pageSubtitleText:disabled {
  cursor: default;
  text-decoration: none;
}

/* ---------- Modal ---------- */
.modal-overlay[hidden] { display: none; }

.modal-overlay {
  position: fixed;
  inset: 0;
  padding: 16px;
  background: rgba(0,0,0,0.55);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 9999;
}

.modal-card {
  width: min(720px, 100%);
  max-height: 80vh;                 /* key: constrain height */
  display: flex;                    /* NEW */
  flex-direction: column;           /* NEW */
  background: #fff;
  border-radius: 12px;
  overflow: hidden;                 /* keep header fixed */
  box-shadow: 0 12px 40px rgba(0,0,0,0.35);
  outline: none;
}

.modal-header {
  flex: 0 0 auto;                   /* NEW: fixed header */
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 14px;
  border-bottom: 1px solid rgba(0,0,0,0.1);
}

.modal-header-actions{
  display: flex;
  align-items: center;
  gap: 10px;
}

.modal-heading {
  min-width: 0;
  padding-right: 10px;
}

.dataset-info-geography,
.dataset-info-updated {
  margin-top: 3px;
  color: #667085;
  font-size: 0.82rem;
  font-weight: 400;
  line-height: 1.25;
}

.dataset-info-geography strong {
  color: #475467;
  font-weight: 600;
}

.dataset-info-tabs {
  flex: 0 0 auto;
  display: flex;
  min-width: 0;
  overflow-x: auto;
  border-bottom: 1px solid rgba(0,0,0,0.12);
  background: #f7f8fa;
  scrollbar-width: thin;
}

.dataset-info-tabs button {
  position: relative;
  flex: 0 0 auto;
  min-width: 112px;
  min-height: 42px;
  padding: 9px 15px 8px;
  border: 0;
  border-right: 1px solid rgba(0,0,0,0.08);
  border-radius: 0;
  background: transparent;
  color: #4d5662;
  font-size: 13px;
  font-weight: 650;
  line-height: 1.2;
  cursor: pointer;
}

.dataset-info-tabs button:hover {
  background: #eef0f3;
  color: #242a31;
}

.dataset-info-tabs button[aria-selected="true"] {
  background: #fff;
  color: #242a31;
}

.dataset-info-tabs button[aria-selected="true"]::after {
  content: "";
  position: absolute;
  right: 12px;
  bottom: 0;
  left: 12px;
  height: 3px;
  background: #f56600;
}

.dataset-info-tabs button:focus-visible {
  z-index: 1;
  outline: 2px solid #522d80;
  outline-offset: -3px;
}

.dataset-info-panel[hidden] {
  display: none;
}

.visitor-guide {
  max-width: 68ch;
  margin: 0 auto;
  color: #30343a;
  font-size: 0.94rem;
  line-height: 1.52;
}

.visitor-guide .guide-lead {
  margin-top: 0;
  color: #404852;
  font-size: 1rem;
}

.visitor-guide h2 {
  margin: 22px 0 7px;
  color: #242a31;
  font-size: 1.08rem;
  line-height: 1.25;
}

.visitor-guide h3 {
  margin: 14px 0 4px;
  color: #343b45;
  font-size: 0.96rem;
  line-height: 1.25;
}

.visitor-guide p,
.visitor-guide ul,
.visitor-guide ol {
  margin-top: 7px;
  margin-bottom: 10px;
}

.visitor-guide li + li {
  margin-top: 5px;
}

.visitor-guide code {
  font-size: 0.9em;
}

.guide-note,
.guide-geography {
  padding: 9px 0 9px 12px;
  border-left: 4px solid #f56600;
  background: #f8f8f6;
}

.guide-geography {
  max-width: 68ch;
  margin: 0 auto 13px;
  color: #404852;
  font-size: 0.9rem;
}

.geography-identifier-guide {
  margin-top: 20px;
}

.geography-identifier-option {
  padding: 9px 0;
  border-bottom: 1px solid #e0e3e7;
}

.geography-identifier-option h3,
.geography-identifier-option p {
  margin-top: 0;
  margin-bottom: 5px;
}

.geography-identifier-option code {
  white-space: normal;
  overflow-wrap: anywhere;
}

.guide-matching-note {
  margin-top: 11px;
  color: #4d5662;
  font-size: 0.9rem;
}

.guide-example {
  max-width: 100%;
  box-sizing: border-box;
  overflow-x: auto;
  margin: 8px 0 10px;
  padding: 10px 12px;
  border: 1px solid #d8dce2;
  border-radius: 4px;
  background: #f6f7f8;
  color: #20252b;
  font-size: 0.84rem;
  line-height: 1.45;
}

.modal-brand-logo {
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 0;
  padding: 0 0 12px;
}

.modal-brand-logo[hidden] {
  display: none;
}

.modal-brand-logo img {
  display: block;
  width: auto;
  height: auto;
  max-width: min(420px, 88%);
  max-height: clamp(64px, 14vh, 120px);
  object-fit: contain;
}

.modal-body {
  flex: 1 1 auto;                   /* NEW: fills remaining space */
  padding: 14px;
  overflow-y: auto;                 /* scrolling happens here */
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
}

.about-citation {
  margin-top: 18px;
  padding-top: 12px;
  border-top: 1px solid rgba(0,0,0,0.14);
  font-size: 0.92rem;
}

.about-citation h3 {
  margin: 0 0 6px;
  font-size: 0.95rem;
}

.about-citation p {
  margin: 0;
}

.about-citation .citation-note {
  margin-top: 5px;
  color: rgba(0,0,0,0.7);
}

.modal-title {
  margin: 0;
  font-weight: 700;
  font-size: 1.05rem;
  line-height: 1.2;
}

.modal-close {
  border: 0;
  background: transparent;
  font-size: 1.6rem;
  cursor: pointer;
}

@media (max-width: 520px) {
  .dataset-info-tabs {
    overflow-x: hidden;
  }

  .dataset-info-tabs button {
    flex: 1 1 0;
    min-width: 0;
    padding-inline: 10px;
    font-size: 12px;
    white-space: normal;
  }

  .modal-brand-logo {
    padding: 0 0 10px;
  }

  .modal-brand-logo img {
    max-width: 92%;
    max-height: clamp(56px, 12vh, 96px);
  }
}

/*
 * At extreme browser zoom, the CSS viewport can become very short even on a
 * desktop display. Preserve the standard modal layout everywhere else, but
 * reclaim vertical space and keep every modal section reachable here.
 */
@media (max-height: 320px) {
  #datasetInfoOverlay.modal-overlay {
    padding: 4px;
  }

  #datasetInfoOverlay .modal-card {
    max-height: calc(100dvh - 8px);
    border-radius: 8px;
  }

  #datasetInfoOverlay .modal-header {
    padding: 6px 8px;
  }

  #datasetInfoOverlay .modal-heading {
    padding-right: 4px;
  }

  #datasetInfoOverlay .modal-title {
    font-size: 0.9rem;
    line-height: 1.1;
  }

  #datasetInfoOverlay .dataset-info-geography,
  #datasetInfoOverlay .dataset-info-updated {
    margin-top: 2px;
    font-size: 0.7rem;
    line-height: 1.1;
  }

  #datasetInfoOverlay .modal-close {
    flex: 0 0 auto;
    padding: 4px 7px;
    font-size: 1.25rem;
    line-height: 1;
  }

  #datasetInfoOverlay .dataset-info-tabs {
    overflow-x: hidden;
  }

  #datasetInfoOverlay .dataset-info-tabs button {
    flex: 1 1 0;
    min-width: 0;
    min-height: 32px;
    padding: 4px 3px;
    font-size: 0.7rem;
    line-height: 1.1;
    white-space: normal;
  }

  #datasetInfoOverlay .modal-body {
    min-height: 56px;
    padding: 8px 10px;
  }

  #datasetInfoOverlay .modal-footer {
    padding: 4px 8px;
  }

  #datasetInfoOverlay .modal-primary-action {
    min-height: 32px;
    padding: 4px 10px;
    font-size: 0.8rem;
  }
}

/* Report modal helpers */
/* Reuse your icon-btn look; if icon-btn is already styled, you may not need this */
#reportPdfBtn{
  padding: 6px 10px;
  border: 1px solid #bbb;
  background: white;
  border-radius: 8px;
  cursor: pointer;
  font-size: 13px;
}
#reportPdfBtn:hover{ background: #f2f2f2; }
#reportPdfBtn:disabled{ opacity: 0.6; cursor: default; }

.pdf-status{
  display: none;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  opacity: 0.85;
}

/* Only show when JS removes hidden */
.pdf-status:not([hidden]){
  display: inline-flex;
}

/* spinner */
.pdf-status::before{
  content: "";
  width: 14px;
  height: 14px;
  border: 2px solid rgba(0,0,0,0.25);
  border-top-color: rgba(0,0,0,0.75);
  border-radius: 50%;
  display: inline-block;
  animation: pdfspin 0.9s linear infinite;
}

@keyframes pdfspin{
  to { transform: rotate(360deg); }
}

/* Optional: make the button look "busy" when disabled */
#reportPdfBtn[disabled]{
  opacity: 0.7;
  cursor: default;
}

#reportMapImg,
.report-screenshot {
  max-width: 100%;
  height: auto;
  display: block;
}

/* ---------------- Report modal content ---------------- */
.report-block { margin: 10px 0 16px; }

.report-intro{
  font-size: 13px;
  line-height: 1.35;
  margin-bottom: 10px;
  padding-bottom: 8px;
  border-bottom: 1px solid rgba(0,0,0,0.15);
}

.report-screenshot {
  width: 100%;
  max-width: 900px;
  border: 1px solid rgba(0,0,0,0.15);
  border-radius: 10px;
  display: block;
}

.report-kv {
  margin: 10px 0 0;
  font-size: 13px;
  line-height: 1.45;
}

.report-kv .row { margin: 2px 0; }
.report-kv .k { font-weight: 700; }

.report-table {
  width: 100%;
  border-collapse: separate;   /* <-- important for sticky <th> on mobile */
  border-spacing: 0;           /* keeps the same look as collapse */
  font-size: 13px;
  min-width: 520px;
}

.report-table-wrap{
  overflow: visible;  /* <-- no overflow container here */
  width: 100%;
}

.report-table th, .report-table td {
  padding: 8px 10px;
  border-bottom: 1px solid rgba(0,0,0,0.10);
  vertical-align: top;
}

.report-table thead th {
  position: sticky;
  top: 0;
  background: #fff;
  z-index: 2;
  text-align: left;
  border-bottom: 1px solid rgba(0,0,0,0.18);
}

.report-table td.num, .report-table th.num {
  text-align: right;
  font-variant-numeric: tabular-nums;
}

/* Report table sorting */
.report-table th.sortable {
  cursor: pointer;
  user-select: none;
}

.report-table th.sortable .sort-ind {
  display: inline-block;
  margin-left: 6px;
  opacity: 0.6;
  font-size: 11px;
}

/* Inline bars inside the Attribute Report table */
.report-bar-wrap{
  width: 140px;              /* tweak: 120–180px */
  height: 10px;
  background: rgba(0,0,0,0.10);
  border-radius: 999px;
  outline: 1px solid rgba(0,0,0,0.08);
  overflow: hidden;
}

.report-bar{
  height: 100%;
  width: 0%;
  background: rgba(0,0,0,0.55);
  border-radius: 999px;
}

/* Region report tables compare attributes within one selected region. */
.county-report-divider {
  margin: 10px 0 12px;
}

.county-report-table {
  min-width: 680px;
  table-layout: fixed;
}

.county-report-table.no-scalar {
  min-width: 520px;
}

.county-report-table .region-report-label-col {
  width: 34%;
}

.county-report-table .region-report-label-col-wide {
  width: 45%;
}

.county-report-table .region-report-value-col {
  width: 33%;
}

.county-report-table.no-scalar .region-report-value-col {
  width: 55%;
}

.county-report-table .region-report-value-cell {
  vertical-align: middle;
}

.region-report-metric {
  display: flex;
  align-items: center;
  justify-content: flex-end;
  gap: 8px;
  min-width: 0;
}

.region-report-metric-value {
  flex: 1 1 auto;
  min-width: 0;
  white-space: nowrap;
  text-align: right;
}

.region-report-bar-wrap {
  display: block;
  flex: 0 0 74px;
  width: 74px;
  height: 8px;
}

.region-report-bar-wrap .report-bar {
  display: block;
}

#reportBody:focus-visible {
  outline: 3px solid #522d80;
  outline-offset: -4px;
}

/* Reflow the report at zoom-equivalent narrow widths without changing its desktop table. */
@media (max-width: 520px) {
  #reportBody {
    overflow-x: hidden;
  }

  #reportContent,
  #reportContent .report-block,
  #reportContent .report-table-wrap {
    min-width: 0;
    max-width: 100%;
  }

  #reportContent {
    overflow-wrap: anywhere;
  }

  #reportContent .report-table,
  #reportContent .county-report-table,
  #reportContent .county-report-table.no-scalar {
    min-width: 0;
    table-layout: fixed;
  }

  #reportContent .report-table th,
  #reportContent .report-table td {
    padding: 6px 4px;
    overflow-wrap: anywhere;
  }

  #reportContent .report-bar-wrap {
    width: 100%;
    min-width: 24px;
  }

  #reportContent .region-report-metric {
    flex-wrap: wrap;
    gap: 4px;
  }

  #reportContent .region-report-metric-value {
    white-space: normal;
  }

  #reportContent .region-report-bar-wrap {
    flex: 1 1 44px;
    width: auto;
    min-width: 24px;
    max-width: 74px;
  }
}

.report-text{
  margin-top: 10px;
  padding-top: 10px;
  border-top: 1px solid rgba(0,0,0,0.12);
  font-size: 13px;
  line-height: 1.35;
}

.controls .collapse-btn {
  margin-left: 4px;
}

.collapse-btn{
  width: auto;
  margin: 0;
  padding: 6px 10px;
  cursor: pointer;
  font-size: 13px;
}

.controls {
  align-self: flex-start;
  flex: 1 1 0;
  min-width: 0; /* important: allows shrinking in a flex row */
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px 16px;
}

.controls label {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  user-select: none;
}

.controls input[type="range"] {
  width: 140px;
}

/* File upload input (keep compact + consistent across browsers) */
.controls input[type="file"] {
  font-size: 13px;
  line-height: 1;
  max-width: 260px;
}

/* Style the native "Choose file" button portion */
.controls input[type="file"]::file-selector-button {
  padding: 6px 10px;
  border: 1px solid #bbb;
  background: white;
  border-radius: 6px;
  cursor: pointer;
  margin-right: 8px;
}
.controls input[type="file"]::file-selector-button:hover {
  background: #f2f2f2;
}

/* Safari/older Chromium fallback */
.controls input[type="file"]::-webkit-file-upload-button {
  padding: 6px 10px;
  border: 1px solid #bbb;
  background: white;
  border-radius: 6px;
  cursor: pointer;
  margin-right: 8px;
}
.controls input[type="file"]::-webkit-file-upload-button:hover {
  background: #f2f2f2;
}

/* Source/status pill shown when a user uploads data */
.file-status {
  font-size: 12px;
  color: #444;
  padding: 4px 8px;
  border-radius: 999px;
  background: #f0f0f0;
  border: 1px solid #e0e0e0;
  white-space: nowrap;
}

.controls .val {
  display: inline-block;
  min-width: 44px;
  text-align: right;
  color: #222;
  font-variant-numeric: tabular-nums;
}

.controls button {
  padding: 6px 10px;
  border: 1px solid #bbb;
  background: white;
  border-radius: 6px;
  cursor: pointer;
}
.controls button:hover {
  background: #f2f2f2;
}

.controls .chk {
  display: flex;
  align-items: center;
  gap: 8px;
}

.controls select {
  padding: 6px 10px;
  border: 1px solid #bbb;
  background: white;
  border-radius: 6px;
  font-size: 13px;
  line-height: 1;
  min-width: 9rem;
  max-width: 16rem;
}

.controls select:focus {
  outline: none;
  box-shadow: 0 0 0 3px rgba(82, 45, 128, 0.18); /* subtle Clemson-purple focus */
}

/* Compact icon buttons (minimal real estate) */
.export-btns {
  display: inline-flex;
  gap: 6px;
  align-items: center;
}

.icon-btn {
  width: 34px;
  height: 34px;
  padding: 0;
  border-radius: 8px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.icon-btn svg {
  width: 22px;
  height: 22px;
  fill: currentColor;
}

#advancedControls{
  width: 100%;
  flex: 0 0 100%;
  display: none;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px 16px;
}

.status {
  font-size: 12px;
  color: #444;
  padding: 4px 8px;
  border-radius: 999px;
  background: #f0f0f0;
  border: 1px solid #e0e0e0;
}

/* Mode controls visibility */
.mode-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 12px 16px;
}

.mode-controls.hidden {
  display: none;
}

/* Legend for contour mode */
.legend {
  position: absolute;
  right: 12px;
  bottom: 18px;
  z-index: 1000;
  background: rgba(255,255,255,0.92);
  border: 1px solid #ddd;
  border-radius: 10px;
  padding: 10px 12px;
  box-shadow: 0 6px 18px rgba(0,0,0,0.08);
  width: 260px;
}

.legend-title {
  font-size: 13px;
  font-weight: 600;
  margin-bottom: 8px;
}

.legend-bar {
  display: flex;
  justify-content: center;
  margin-bottom: 6px;
}

.legend-labels {
  display: flex;
  justify-content: space-between;
  font-size: 12px;
  color: #333;
  font-variant-numeric: tabular-nums;
}

/* Legend for polygons mode (existing — keep as-is) */
#polygonsLegend {
  position: absolute;
  right: 12px;
  bottom: 18px;
  z-index: 1000;
  padding: 10px;
  background: rgba(255, 255, 255, 0.95);
  border: 1px solid rgba(0,0,0,0.15);
  border-radius: 6px;
  box-shadow: 0 1px 6px rgba(0,0,0,0.2);
  max-width: 240px;
  font-size: 14px;
}

/* Toggle should not show on desktop */
#polygonsLegend .legend-toggle {
  display: none;
}

.legend-item {
  display: flex;
  align-items: center;
  margin: 2px 0;
}

.legend-color {
  width: 20px;
  height: 20px;
  margin-right: 8px;
  flex-shrink: 0;
}

.legend-note {
  margin-top: 6px;
  font-style: italic;
  opacity: 0.85;
  font-size: 12px;
}

/* --- Mobile-only legend collapse/tuning (does not affect desktop) --- */
@media (max-width: 640px) {
  #polygonsLegend .legend-toggle {
    display: flex;
    width: 100%;
    align-items: center;
    justify-content: space-between;
    gap: 8px;
    padding: 6px 0;
    border: none;
    background: transparent;
    color: #1f2933;
    font-weight: 600;
    cursor: pointer;
    touch-action: manipulation;
  }

  #polygonsLegend .legend-toggle:focus-visible {
    outline: 2px solid #522d80;
    outline-offset: 2px;
  }

  #polygonsLegend .legend-chevron {
    display: inline-block;
    width: 0;
    height: 0;
    border-left: 5px solid transparent;
    border-right: 5px solid transparent;
    border-top: 7px solid currentColor;
    font-size: 0;
    line-height: 0;
    transform-origin: center;
    transition: transform 140ms ease;
  }

  #polygonsLegend:not(.is-collapsed) .legend-chevron {
    transform: rotate(180deg);
  }

  /* Collapse hides ONLY bins */
  #polygonsLegend.is-collapsed .legend-body { display: none; }

  /* Keep bins from dominating when expanded */
  #polygonsLegend .legend-body {
    max-height: 40vh;
    overflow: auto;
  }

    #polygonsLegend .legend-header {
    display: flex;
    justify-content: space-between;
    align-items: flex-start; /* keeps stack top-aligned */
    gap: 10px;
  }

  #polygonsLegend .legend-header-text {
    min-width: 0; /* allows wrapping instead of forcing width */
    flex: 1 1 auto;
  }

  #polygonsLegend .legend-toggle.legend-toggle-icon {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    padding: 0;
    border: 1px solid rgba(0, 0, 0, 0.35);
    border-radius: 4px;
    background: rgba(255, 255, 255, 0.94);
    box-shadow: 0 1px 2px rgba(0, 0, 0, 0.16);
    flex: 0 0 auto;
  }

  #polygonsLegend .legend-toggle.legend-toggle-icon:active {
    background: #eceff1;
  }

  @media (prefers-reduced-motion: reduce) {
    #polygonsLegend .legend-chevron {
      transition: none;
    }
  }

}

/* Popup styling */
.popup-scroll {
  max-height: 150px;
  overflow-y: auto;
  overflow-x: hidden;
  padding-right: 6px;
}

/* Popup rows for county data */
.popup-row {
  margin: 2px 0;
}

.popup-row .k {
  font-weight: 600;
}

.popup-row .v {
  font-variant-numeric: tabular-nums;
}

/* Visible separator inside Leaflet popup content */
.popup-divider{
  height: 1px;
  background: rgba(0,0,0,0.25);
  margin: 6px 0;
}

/* Popup action button (County report) */
.popup-actions{
  margin: 0 0 6px 0;
}

.popup-btn{
  display: inline-block;
  width: 100%;
  padding: 6px 10px;
  font-size: 12px;
  line-height: 1.2;
  border-radius: 8px;
  border: 1px solid rgba(0,0,0,0.25);
  background: #fff;
  cursor: pointer;
}

.popup-btn:hover{
  background: rgba(0,0,0,0.04);
}

/* County report uses the same key/value row styling as the popup */
.county-report-rows .popup-row{
  margin: 4px 0;
}

/* Permanent county value labels */
.leaflet-tooltip.data-label {
  background: rgba(255,255,255,0.75);
  color: #000;
  border: 1px solid rgba(0,0,0,0.25);
  border-radius: 4px;
  padding: 2px 5px;
  box-shadow: none;
  font-size: 12px;
  font-weight: 600;
  white-space: nowrap;
  pointer-events: none; /* doesn’t block clicks on counties */
}

.leaflet-tooltip.data-label::before {
  display: none; /* remove tooltip “pointer” */
}

/* Legend overlay on the map */
#map .map-legend {
  background: rgba(255, 255, 255, 0.95);
  border: 1px solid rgba(0,0,0,0.15);
  border-radius: 6px;
  padding: 10px;
  box-shadow: 0 1px 6px rgba(0,0,0,0.2);
  max-width: 240px;
}

/* Keep the Regions legend's intrinsic width when Leaflet attribution collapses. */
#map .leaflet-bottom.leaflet-right > #polygonsLegend.map-legend {
  width: max-content;
}

.leaflet-marker-icon.map-logo-icon {
  pointer-events: auto;   /* allow clicks */
  cursor: pointer;        /* visual cue */
}

/* Optional: keep legend from getting huge on small screens */
@media (max-width: 480px) {
  #map .map-legend {
    max-width: 180px;
    font-size: 12px;
  }
  
  .legend {
    width: 200px;
    font-size: 11px;
  }
}

/* Medium + small screens: controls drop under title, stay left-justified, stay compact */
@media (max-width: 900px) {
  
  /* Lock the overall page to the viewport without weird body scroll states */
  html, body {
    height: 100%;
    overflow: hidden;
    width: 100%;
  }

  /* Only the topbar scrolls */
  .topbar {
    max-height: 30dvh;  /* your setting */
    overflow-y: auto; /* ONLY topbar scrolls */
    -webkit-overflow-scrolling: touch;
    overscroll-behavior: contain;
  }

  .topbar-scroll {
    max-height: 30dvh;  /* your setting */
  }

  /* Map flexes to fill the remaining space and is allowed to shrink */
  #map {
    min-height: 0;
    flex: 1 1 0;
  }

  .controls {
    flex: 0 0 100%;
    width: 100%;
    justify-content: flex-start; /* left, not centered */
    align-items: center;
  }

  /* Keep Mode + Attribute side-by-side as long as they fit */
  .controls label {
    display: inline-flex;
    align-items: center;
    flex-wrap: nowrap;  /* don't wrap unless forced */
    gap: 6px 8px;
  }

  /* Compact selects, but don't let them overflow the viewport */
  .controls select {
    width: auto;
    max-width: 16rem;   /* tweak: 14–18rem */
    min-width: 9rem;    /* tweak: 8–10rem */
  }

  /* Keep file inputs from forcing horizontal overflow */
  .controls input[type="file"] {
    max-width: 100%;
  }

  .controls .collapse-btn {
    width: auto;
  }
}

/* Very small screens only: stack each label + select */
@media (max-width: 520px) {
  .controls label {
    flex: 0 0 100%;
    width: 100%;
    flex-wrap: wrap;        /* allow select to drop */
    justify-content: flex-start;
  }

  .controls select {
    max-width: 100%;
  }
}

/* ========================================================================== */
/* v2 compact application toolbar                                             */
/* ========================================================================== */

:root {
  --toolbar-bg: #f7f8fa;
  --toolbar-border: #d8dce2;
  --toolbar-text: #242a31;
  --toolbar-muted: #59616d;
  --toolbar-control-height: 32px;
  --toolbar-gap: 8px;
}

.topbar {
  flex: 0 0 auto;
  max-height: none;
  overflow: visible;
  border-bottom-color: var(--toolbar-border);
  background: var(--toolbar-bg);
  color: var(--toolbar-text);
}

.topbar-scroll {
  display: grid;
  grid-template-columns: var(--toolbar-control-height) minmax(210px, 320px) minmax(0, 1fr);
  grid-template-areas:
    "navigation title primary"
    "advanced advanced advanced";
  align-items: end;
  column-gap: 10px;
  row-gap: 0;
  width: 100%;
  max-height: min(42dvh, 360px);
  padding: 7px 10px 8px;
  overflow-x: hidden;
  overflow-y: auto;
}

.app-navigation {
  position: relative;
  grid-area: navigation;
  align-self: center;
}

.app-navigation-trigger {
  display: inline-grid;
  width: var(--toolbar-control-height);
  height: var(--toolbar-control-height);
  min-height: var(--toolbar-control-height);
  place-content: center;
  gap: 4px;
  box-sizing: border-box;
  padding: 0;
  border: 1px solid #aeb5bf;
  border-radius: 5px;
  background: #fff;
  color: var(--toolbar-text);
  cursor: pointer;
}

.app-navigation-trigger:hover,
.app-navigation-trigger[aria-expanded="true"] {
  background: #f2f2f2;
}

.app-navigation-trigger span {
  display: block;
  width: 17px;
  height: 2px;
  border-radius: 2px;
  background: currentColor;
}

.app-navigation-menu {
  position: fixed;
  z-index: 1700;
  display: grid;
  width: min(270px, calc(100vw - 16px));
  max-height: calc(100dvh - 16px);
  box-sizing: border-box;
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: 6px;
  border: 1px solid #b8bec7;
  border-radius: 6px;
  background: #fff;
  color: #242a31;
  box-shadow: 0 12px 28px rgba(25, 30, 36, 0.22);
}

.app-navigation-menu[hidden] {
  display: none;
}

.app-navigation-menu > a,
.app-navigation-menu > button {
  display: flex;
  width: 100%;
  min-height: 39px;
  align-items: center;
  box-sizing: border-box;
  margin: 0;
  padding: 8px 10px;
  border: 0;
  border-radius: 4px;
  background: #fff;
  color: #242a31;
  font: 600 13px/1.25 system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  text-align: left;
  text-decoration: none;
  cursor: pointer;
}

.app-navigation-menu > a:hover,
.app-navigation-menu > button:hover {
  background: #f1f3f5;
}

.app-navigation-menu > a:focus-visible,
.app-navigation-menu > button:focus-visible {
  position: relative;
  z-index: 1;
  outline: 2px solid #522d80;
  outline-offset: -2px;
}

.app-navigation-menu > hr {
  width: 100%;
  margin: 5px 0;
  border: 0;
  border-top: 1px solid var(--toolbar-border);
}

.title {
  grid-area: title;
  align-self: center;
  min-width: 0;
  gap: 2px;
  line-height: 1.12;
}

.title strong {
  display: block;
  overflow: visible;
  color: var(--toolbar-text);
  font-size: 17px;
  line-height: 1.15;
  overflow-wrap: anywhere;
  text-overflow: clip;
  white-space: normal;
}

.subtitle {
  width: max-content;
  max-width: 100%;
  overflow: hidden;
  color: var(--toolbar-muted);
  font-size: 11.5px;
  line-height: 1.2;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#pageSubtitleText {
  display: inline-flex;
  align-items: center;
  padding-block: 4px;
  margin-block: -4px;
}

.controls {
  grid-area: primary;
  align-self: end;
  display: flex;
  flex: 1 1 auto;
  flex-wrap: nowrap;
  align-items: flex-end;
  gap: var(--toolbar-gap);
  width: 100%;
  min-width: 0;
}

.controls label {
  display: flex;
  flex-direction: column;
  flex-wrap: nowrap;
  align-items: stretch;
  justify-content: flex-end;
  gap: 3px;
  width: auto;
  min-width: 0;
  color: var(--toolbar-muted);
  font-size: 11px;
  font-weight: 650;
  line-height: 1.05;
}

.controls label > span {
  display: block;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  line-height: 1.2;
  white-space: nowrap;
}

#uploadWrap {
  flex: 0 1 170px;
  max-width: 170px;
}

#modeSelectWrap {
  flex: 0 1 105px;
  max-width: 105px;
}

#sheetPickerWrap {
  flex: 0 1 130px;
  max-width: 130px;
}

.controls > label:not(#uploadWrap):not(#modeSelectWrap):not(#sheetPickerWrap) {
  flex: 0 1 185px;
  max-width: 185px;
}

.controls select,
.controls input[type="file"] {
  width: 100%;
  min-width: 0;
  max-width: none;
  height: var(--toolbar-control-height);
  box-sizing: border-box;
  border-color: #aeb5bf;
  border-radius: 5px;
  background: #fff;
  color: var(--toolbar-text);
  font-size: 12px;
  line-height: 1.15;
}

.controls select {
  padding: 5px 26px 5px 8px;
  -webkit-appearance: none;
  appearance: none;
  background-color: #fff;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath d='M1 1.5 6 6.5 11 1.5' fill='none' stroke='%23242a31' stroke-width='2' stroke-linecap='round' stroke-linejoin='round'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 8px center;
  background-size: 12px 8px;
}

.controls input[type="file"] {
  padding: 0 7px 0 0;
}

#uploadWrap .file-picker-shell {
  position: relative;
  display: grid;
  grid-template-columns: auto minmax(0, 1fr);
  align-items: center;
  width: 100%;
  height: var(--toolbar-control-height);
  box-sizing: border-box;
  overflow: hidden;
  border: 1px solid #aeb5bf;
  border-radius: 5px;
  background: #fff;
  color: var(--toolbar-text);
}

#uploadWrap .file-picker-shell:hover {
  background: #f5f6f8;
}

#uploadWrap .file-picker-shell:focus-within {
  outline: 2px solid #522d80;
  outline-offset: 1px;
}

#uploadWrap .file-picker-shell input[type="file"] {
  position: absolute;
  inset: 0;
  z-index: 2;
  width: 100%;
  max-width: none;
  height: 100%;
  margin: 0;
  padding: 0;
  opacity: 0;
  cursor: pointer;
}

.file-picker-trigger {
  align-self: stretch;
  display: inline-flex;
  align-items: center;
  padding: 0 9px;
  border-right: 1px solid #b8bec7;
  background: #f8f9fa;
  color: var(--toolbar-text);
  font-size: 12px;
  font-weight: 600;
}

.file-picker-name {
  min-width: 0;
  overflow: hidden;
  padding: 0 8px;
  color: var(--toolbar-muted);
  font-size: 12px;
  font-weight: 400;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.controls input[type="file"]::file-selector-button,
.controls input[type="file"]::-webkit-file-upload-button {
  height: 30px;
  margin-right: 7px;
  padding: 5px 9px;
  border-width: 0 1px 0 0;
  border-color: #b8bec7;
  border-radius: 4px 0 0 4px;
  background: #fff;
  font-size: 12px;
}

.controls .collapse-btn,
.controls button {
  min-height: var(--toolbar-control-height);
  padding: 5px 9px;
  border-color: #aeb5bf;
  border-radius: 5px;
  font-size: 12px;
  line-height: 1.15;
}

.controls .collapse-btn {
  width: auto;
  margin-left: 0;
  white-space: nowrap;
}

.export-btns {
  align-self: flex-end;
  flex: 0 0 auto;
  gap: 4px;
}

.toolbar-actions {
  align-self: flex-end;
  display: inline-flex;
  align-items: center;
  flex: 0 0 auto;
  gap: 6px;
}

.controls .toolbar-home-link {
  box-sizing: border-box;
  border: 1px solid #aeb5bf;
  background: #fff;
  color: var(--toolbar-text);
  text-decoration: none;
}

.controls .toolbar-home-link:hover {
  background: #f2f2f2;
}

.controls .toolbar-home-link:focus-visible {
  outline: 2px solid #522d80;
  outline-offset: 2px;
}

.toolbar-share {
  align-self: flex-end;
  flex: 0 0 auto;
}

.controls .toolbar-share-trigger {
  border-color: #aeb5bf;
  background: #fff;
  color: var(--toolbar-text);
}

.controls .toolbar-share-trigger:hover,
.controls .toolbar-share-trigger[aria-expanded="true"] {
  background: #f2f2f2;
}

.controls .toolbar-share-trigger:focus-visible {
  outline: 2px solid #522d80;
  outline-offset: 2px;
}

.controls .toolbar-share-trigger svg {
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.toolbar-share-menu {
  position: fixed;
  z-index: 1500;
  display: grid;
  width: min(210px, calc(100vw - 16px));
  max-height: calc(100dvh - 16px);
  box-sizing: border-box;
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  border: 1px solid #b8bec7;
  border-radius: 6px;
  background: #fff;
  color: #242a31;
  box-shadow: 0 12px 28px rgba(25, 30, 36, 0.22);
}

.toolbar-share-menu[hidden],
.toolbar-share-menu [hidden] {
  display: none;
}

.toolbar-share-menu a,
.toolbar-share-menu button {
  display: flex;
  width: 100%;
  min-height: 39px;
  align-items: center;
  box-sizing: border-box;
  margin: 0;
  padding: 8px 12px;
  border: 0;
  border-radius: 0;
  background: #fff;
  color: #242a31;
  font: 500 13px/1.2 system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  text-align: left;
  text-decoration: none;
  cursor: pointer;
}

.toolbar-share-menu a:hover,
.toolbar-share-menu button:hover {
  background: #f1f3f5;
}

.toolbar-share-menu a:focus-visible,
.toolbar-share-menu button:focus-visible {
  position: relative;
  z-index: 1;
  outline: 2px solid #522d80;
  outline-offset: -2px;
}

.toolbar-share-status {
  display: block;
  min-height: 0;
  color: #3f2853;
  font-size: 12px;
  font-weight: 650;
  line-height: 1.3;
}

.toolbar-share-status:not(:empty) {
  min-height: 32px;
  padding: 8px 12px;
  background: #f1edf5;
}

.controls .icon-btn {
  flex: 0 0 auto;
  width: var(--toolbar-control-height);
  height: var(--toolbar-control-height);
  min-height: var(--toolbar-control-height);
  padding: 0;
  border-radius: 5px;
}

.controls .icon-btn svg {
  width: 19px;
  height: 19px;
}

#toggleAdvancedControls {
  align-self: flex-end;
  flex: 0 0 auto;
  height: var(--toolbar-control-height);
  width: 98px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  border-color: #c3c9d1;
  background: #eef1f4;
  color: #343b45;
  font-size: 0;
}

#toggleAdvancedControls::before {
  content: "Advanced";
  font-size: 12px;
}

#toggleAdvancedControls::after {
  content: "+";
  font-size: 16px;
  font-weight: 650;
  line-height: 1;
}

#toggleAdvancedControls[aria-expanded="true"]::after {
  content: "-";
}

#toggleAdvancedControls:hover {
  border-color: #aeb5bf;
  background: #e4e8ed;
}

#toggleAdvancedControls:focus-visible {
  outline: 2px solid #522d80;
  outline-offset: 2px;
}

#advancedControls {
  grid-area: advanced;
  width: 100%;
  flex: initial;
  margin-top: 7px;
  padding-top: 7px;
  border-top: 1px solid var(--toolbar-border);
  gap: 8px 12px;
}

.mode-controls {
  width: 100%;
  align-items: flex-end;
  gap: 7px 12px;
}

#advancedControls label {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  justify-content: flex-end;
  gap: 3px;
  min-width: 0;
  color: var(--toolbar-muted);
  font-size: 11px;
  font-weight: 650;
  line-height: 1.05;
}

#advancedControls label.chk,
#advancedControls label.ctrl,
#advancedControls label.control-row,
#advancedControls label[style*="white-space"] {
  flex-direction: row;
  align-items: center;
  min-height: var(--toolbar-control-height);
  color: var(--toolbar-text);
  font-size: 12px;
  font-weight: 500;
  white-space: nowrap;
}

#advancedControls select,
#advancedControls button {
  min-width: 125px;
  max-width: 190px;
  height: var(--toolbar-control-height);
  box-sizing: border-box;
  padding: 5px 8px;
  border: 1px solid #aeb5bf;
  border-radius: 5px;
  background: #fff;
  color: var(--toolbar-text);
  font-size: 12px;
}

#advancedControls button {
  min-width: auto;
  cursor: pointer;
}

.advanced-mode-actions {
  display: inline-flex;
  align-items: flex-end;
  gap: 4px;
}

#advancedControls .close-advanced-controls {
  display: none;
}

#advancedControls input[type="range"] {
  width: 118px;
  margin: 0;
}

#advancedControls .val {
  min-width: 38px;
  color: var(--toolbar-text);
  font-size: 11px;
  font-variant-numeric: tabular-nums;
  text-align: left;
}

#advancedControls label.range-control {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  grid-template-areas:
    "label value"
    "slider slider";
  align-items: center;
  gap: 3px 6px;
}

#advancedControls .range-control .control-label {
  grid-area: label;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#advancedControls .range-control input[type="range"] {
  grid-area: slider;
}

#advancedControls .range-control .val {
  grid-area: value;
  min-width: 0;
  text-align: right;
}

@media (min-width: 1121px) {
  .export-btns {
    padding-left: 8px;
    border-left: 1px solid var(--toolbar-border);
  }
}

@media (min-width: 641px) {
  .toolbar-actions {
    margin-left: auto;
  }
}

@media (max-width: 1120px) {
  .topbar-scroll {
    grid-template-columns: var(--toolbar-control-height) minmax(0, 1fr);
    grid-template-areas:
      "navigation title"
      "primary primary"
      "advanced advanced";
    column-gap: 8px;
    row-gap: 0;
  }

  .title {
    flex-direction: column;
    align-items: flex-start;
    gap: 2px;
  }

  .title strong {
    flex: 0 1 auto;
  }

  .subtitle {
    flex: 0 1 auto;
  }

  .controls {
    margin-top: 6px;
  }
}

@media (max-width: 900px) {
  .topbar {
    max-height: none;
    overflow: visible;
  }

  .topbar-scroll {
    max-height: 40dvh;
  }

  .controls {
    flex: initial;
    flex-wrap: wrap;
    align-items: flex-end;
  }

}

@media (max-width: 640px) {
  .topbar-scroll {
    display: grid;
    max-height: 44dvh;
    padding: 7px 8px 8px;
  }

  .title {
    gap: 2px;
  }

  .title strong {
    font-size: 15px;
  }

  .subtitle {
    font-size: 11px;
  }

  .controls {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 7px;
    width: 100%;
  }

  .controls label {
    width: auto;
    max-width: none;
    flex: initial;
  }

  #uploadWrap {
    grid-column: 1 / -1;
    max-width: none;
  }

  #modeSelectWrap,
  #sheetPickerWrap {
    max-width: none;
  }

  .controls > label:not(#uploadWrap):not(#modeSelectWrap):not(#sheetPickerWrap) {
    grid-column: 1 / -1;
    max-width: none;
  }

  .toolbar-actions {
    grid-column: 1;
  }

  #toggleAdvancedControls {
    grid-column: 2;
    width: 100%;
  }

  .controls .icon-btn,
  #toggleAdvancedControls {
    height: 38px;
    min-height: 38px;
  }

  .controls .icon-btn {
    width: 38px;
  }

  .mode-controls {
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    align-items: end;
    gap: 8px;
  }

  /* app.v3.js shows the active mode with inline display:flex. Preserve the
     inactive state while allowing the visible mobile controls to use the grid. */
  #advancedControls .mode-controls {
    display: grid !important;
  }

  #advancedControls .mode-controls[style*="display: none"] {
    display: none !important;
  }

  #advancedControls label,
  #advancedControls select,
  #advancedControls button {
    width: 100%;
    min-width: 0;
    max-width: none;
  }

  #advancedControls input[type="range"] {
    width: 100%;
  }

  #advancedControls label.chk,
  #advancedControls label.ctrl,
  #advancedControls label.control-row,
  #advancedControls label[style*="white-space"] {
    width: auto;
  }

  .advanced-mode-actions {
    position: sticky;
    bottom: -8px;
    z-index: 3;
    grid-column: 1 / -1;
    display: grid;
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 8px;
    width: 100%;
    box-sizing: border-box;
    padding: 7px 0 8px;
    border-top: 1px solid var(--toolbar-border);
    background: var(--toolbar-bg);
  }

  #advancedControls .advanced-mode-actions button,
  #advancedControls .close-advanced-controls {
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    min-height: 38px;
  }
}

@media (min-width: 360px) and (max-width: 640px) {
  .controls {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }

  #uploadWrap {
    grid-column: span 2;
  }

  .controls > label:not(#uploadWrap):not(#modeSelectWrap):not(#sheetPickerWrap) {
    grid-column: span 2;
  }

  .toolbar-actions {
    grid-column: span 2;
  }

  #toggleAdvancedControls {
    grid-column: 3;
  }

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

  .advanced-mode-actions {
    grid-column: 1 / -1;
  }
}

@media (max-width: 380px) {
  .topbar-scroll {
    max-height: 46dvh;
  }

  .controls {
    gap: 6px;
  }

  .controls select,
  .controls input[type="file"],
  .controls .collapse-btn,
  .controls button {
    font-size: 11.5px;
  }
}

@media (max-width: 350px) {
  .toolbar-actions,
  #toggleAdvancedControls {
    grid-column: 1 / -1;
  }
}

/* Shared keyboard focus treatment. It appears only for keyboard-style focus. */
:where(a[href], button, input, select, textarea, summary, [tabindex]:not([tabindex="-1"])):focus-visible {
  outline: 3px solid #522d80;
  outline-offset: 2px;
}

.controls select:focus-visible {
  outline: 3px solid #522d80;
  outline-offset: 2px;
  box-shadow: none;
}

.modal-card:focus-visible {
  outline: 3px solid #522d80;
  outline-offset: -5px;
}

#map:focus {
  outline: none;
}

#map:focus::after {
  content: "";
  position: absolute;
  inset: 0;
  z-index: 1001;
  border: 3px solid #fff;
  box-shadow: inset 0 0 0 6px #522d80;
  pointer-events: none;
}
