:root {
  --panel: rgba(255, 255, 255, .97);
  --ink: #17202a;
  --muted: #607080;
  --line: #dfe5ea;
  --accent: #17324d;
  --warn: #9a6700;
}

html, body {
  height: 100%;
  margin: 0;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  color: var(--ink);
}

#map { position: absolute; inset: 0; background: #edf1f3; }

/* ---- Side panel ------------------------------------------------------- */
.panel {
  position: absolute; z-index: 1000; left: 16px; top: 16px; width: 380px;
  max-height: calc(100% - 32px); overflow: auto;
  background: var(--panel); border: 1px solid rgba(23, 50, 77, .10); border-radius: 14px;
  box-shadow: 0 8px 30px rgba(20, 35, 50, .20); backdrop-filter: blur(8px);
}
.panel-head { padding: 15px 16px 11px; border-bottom: 1px solid var(--line); }
h1 { font-size: 20px; line-height: 1.14; margin: 0 0 6px; letter-spacing: -.02em; }
.sub { font-size: 12px; color: var(--muted); line-height: 1.42; }

.toolbar {
  display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 7px;
  padding: 10px 12px; border-bottom: 1px solid var(--line);
}
button, select, input { font: 600 12px/1.15 system-ui; }
button {
  border: 1px solid #c8d1d9; background: #fff; color: #243444;
  border-radius: 8px; padding: 7px 8px; cursor: pointer;
}
button:hover { background: #f3f6f8; }
button.active { background: var(--accent); border-color: var(--accent); color: #fff; }
button:disabled { opacity: .4; cursor: default; }

.status {
  font-size: 11px; line-height: 1.35; padding: 9px 12px; color: #536474;
  background: #f7f9fa; border-bottom: 1px solid var(--line);
}
.status .dirty, .dirty { color: var(--warn); font-weight: 700; }
.status .error { color: #b42318; font-weight: 700; }

/* Collapsible sections */
details.section { border-bottom: 1px solid var(--line); }
details.section > summary {
  list-style: none; cursor: pointer; user-select: none;
  font-size: 10px; font-weight: 800; letter-spacing: .09em; text-transform: uppercase; color: #71808c;
  padding: 11px 12px 8px; display: flex; align-items: center; gap: 6px;
}
details.section > summary::-webkit-details-marker { display: none; }
details.section > summary::before {
  content: ""; width: 0; height: 0; border-left: 5px solid #98a5b0; border-top: 4px solid transparent;
  border-bottom: 4px solid transparent; transition: transform .12s;
}
details.section[open] > summary::before { transform: rotate(90deg); }
details.section > summary:hover { color: var(--accent); }
.section-title {
  font-size: 10px; font-weight: 800; letter-spacing: .09em; text-transform: uppercase;
  color: #71808c; padding: 11px 12px 5px;
}
.section-body { padding: 0 12px 11px; }

/* ---- Legend ------------------------------------------------------------ */
#legend { padding: 0 0 4px; }
.region {
  display: grid; grid-template-columns: 18px 21px 1fr; gap: 7px; align-items: start;
  padding: 6px 0; border-top: 1px solid #edf0f2;
}
.region:first-child { border-top: 0; }
.swatch { width: 14px; height: 14px; margin-top: 2px; border-radius: 4px; border: 1px solid rgba(0, 0, 0, .28); }
.region label { font-size: 12px; cursor: pointer; line-height: 1.22; }
.meta { display: block; color: #64727e; font-size: 10.5px; margin-top: 2px; }
.densitybar { height: 3px; background: #e8edf1; border-radius: 3px; overflow: hidden; margin-top: 4px; }
.densitybar span { display: block; height: 100%; border-radius: 3px; }
.legend-empty { font-size: 11px; color: #64727e; line-height: 1.4; padding: 6px 0 4px; }
.mode-note { font-size: 10px; color: #6f7e8a; line-height: 1.4; padding: 6px 0 2px; }

/* ---- Cumulative card --------------------------------------------------- */
.cumulative {
  margin: 4px 0 10px; padding: 11px 12px; background: #f1f5f7;
  border: 1px solid #dce5eb; border-radius: 10px;
}
.cumulative-title {
  font-size: 10px; font-weight: 850; letter-spacing: .08em; text-transform: uppercase;
  color: #5e6e7a; margin-bottom: 7px;
}
.cum-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 7px 12px; }
.cum-item .v { font-size: 16px; font-weight: 800; line-height: 1.05; color: #17202a; }
.cum-item .k { font-size: 9.5px; color: #71808c; margin-top: 2px; }
.cum-callout {
  margin-top: 9px; padding-top: 8px; border-top: 1px solid #d7e0e6;
  font-size: 11px; line-height: 1.4; color: #3f5362;
}
.cum-callout b { color: #17324d; }
.county-note { font-size: 9px; color: #82909b; margin-top: 5px; line-height: 1.35; }

/* ---- Map labels + popups ----------------------------------------------- */
.region-label {
  background: transparent; border: none !important; box-shadow: none !important;
  text-align: center; pointer-events: none;
}
.region-label .badge {
  display: inline-block; min-width: 116px; max-width: 155px; padding: 5px 7px 4px;
  background: rgba(255, 255, 255, .91); border: 2px solid var(--c); border-radius: 8px;
  box-shadow: 0 2px 8px rgba(0, 0, 0, .16); color: #14202b; font: 700 10px/1.12 system-ui;
}
.region-label .n { color: var(--c); }
.region-label .stat { display: block; margin-top: 2px; font-weight: 600; color: #4e5d69; font-size: 9px; }
.leaflet-popup-content { margin: 11px 13px; min-width: 220px; font-size: 12px; line-height: 1.4; }
.popup-title { font-weight: 800; font-size: 13px; margin-bottom: 5px; }

/* ---- Editor -------------------------------------------------------------- */
#editorSection { background: #fbfcfd; }
.editor-grid { display: grid; grid-template-columns: 1fr auto; gap: 7px; align-items: center; }
.editor-grid select { width: 100%; border: 1px solid #c8d1d9; border-radius: 8px; padding: 7px; background: #fff; }
.editor-actions { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 6px; margin-top: 7px; }
.editor-help { font-size: 10.5px; color: #687783; line-height: 1.35; margin-top: 7px; }
.editor-stats { margin-top: 8px; padding: 8px; border-radius: 8px; background: #eef3f6; font-size: 11px; line-height: 1.45; }
.editor-stats b { font-size: 12px; }
.tract-info {
  margin-top: 7px; padding: 8px; border: 1px solid #d9e0e5; border-radius: 8px; background: #fff;
  font-size: 10.5px; line-height: 1.4; min-height: 29px;
}
.newrow { display: none; grid-template-columns: 1fr auto; gap: 6px; margin-top: 7px; }
.newrow.open { display: grid; }
.newrow input { min-width: 0; border: 1px solid #c8d1d9; border-radius: 8px; padding: 7px; }

/* ---- Study area ------------------------------------------------------------ */
.area-grid { display: grid; grid-template-columns: 1fr auto; gap: 7px; align-items: center; }
.area-grid select { width: 100%; border: 1px solid #c8d1d9; border-radius: 8px; padding: 7px; background: #fff; min-width: 0; }
.area-status { margin-top: 6px; font-size: 10px; color: #71808c; line-height: 1.4; }
.area-help { margin-top: 5px; font-size: 10px; color: #6f7e8a; line-height: 1.35; }

/* ---- Transit ------------------------------------------------------------- */
.transit-group { padding: 6px 0 7px; border-top: 1px solid #e6ebef; }
.transit-group:first-child { border-top: 0; padding-top: 0; }
.transit-group-head { display: flex; justify-content: space-between; align-items: center; gap: 8px; }
.transit-check { display: flex; align-items: center; gap: 6px; font-size: 11px; font-weight: 700; color: #334654; }
.transit-check input { margin: 0; }
.transit-stations-toggle { font-weight: 600; color: #5b6b78; font-size: 10.5px; }
.transit-stations-toggle .count { color: #98a5b0; font-weight: 500; }
.transit-lines { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 5px; }
.line-chip {
  display: inline-flex; align-items: center; gap: 4px; padding: 2px 7px 2px 5px; border-radius: 999px;
  border: 1px solid #d3dbe2; background: #fff; font-size: 10px; font-weight: 700; color: #7a8894; cursor: pointer; user-select: none;
}
.line-chip input { display: none; }
.line-chip .sw { width: 9px; height: 9px; border-radius: 50%; background: var(--c); opacity: .35; }
.line-chip.on { color: #17202a; border-color: var(--c); background: color-mix(in srgb, var(--c) 10%, #fff); }
.line-chip.on .sw { opacity: 1; }
.transit-note { margin-top: 5px; font-size: 9.5px; color: #7a8894; line-height: 1.35; }
.transit-legend { margin-top: 7px; font-size: 9.5px; color: #687783; line-height: 1.45; }
.transit-key { display: inline-flex; align-items: center; gap: 4px; margin-right: 9px; white-space: nowrap; }
.linekey { width: 19px; height: 0; border-top: 4px solid #222; border-radius: 3px; }
.linekey.future { border-top-style: dashed; }
.linekey.commuter { border-top-style: dotted; border-top-width: 3px; }
.stationkey {
  width: 8px; height: 8px; border: 2px solid #fff; box-shadow: 0 0 0 1px #222;
  background: #222; border-radius: 50%;
}
.transit-status { margin-top: 6px; font-size: 9.5px; color: #71808c; }

/* ---- Export composer ----------------------------------------------------- */
.export-composer { padding: 10px; border: 1px solid #d6dee5; border-radius: 9px; background: #f7f9fb; }
.export-composer .row { display: grid; grid-template-columns: 1fr 1fr; gap: 7px; margin: 6px 0; }
.export-composer label { font-size: 10px; color: #536474; font-weight: 700; }
.export-composer select, .export-composer input {
  width: 100%; box-sizing: border-box; margin-top: 3px; padding: 6px;
  border: 1px solid #c8d1d9; border-radius: 6px; background: white;
}
.export-composer button { padding: 6px 8px; }
.bounds-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 5px; }
.export-preview { font-size: 10px; color: #667786; line-height: 1.35; margin-top: 6px; }
.export-actions { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 6px; }
.export-note { font-size: 10px; color: #6f7e8a; line-height: 1.35; margin-top: 6px; }
.export-offscreen {
  position: fixed !important; left: -20000px !important; top: 0 !important;
  z-index: -1 !important; background: #edf1f3;
}

/* Rendered legend card (off-screen, captured by html2canvas) */
.export-card {
  position: fixed; left: -10000px; top: 0; width: 900px; background: #fff; color: #17202a;
  padding: 34px 38px; box-sizing: border-box;
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
.export-card h2 { margin: 0 0 8px; font-size: 28px; letter-spacing: -.02em; }
.export-card .subtitle { font-size: 15px; color: #607080; margin-bottom: 22px; }
.export-card .cumexport { margin: 18px 0 20px; padding: 18px 20px; border: 1px solid #dce5eb; border-radius: 12px; background: #f3f6f8; }
.export-card .cumtitle { font-size: 12px; font-weight: 850; letter-spacing: .08em; text-transform: uppercase; color: #5e6e7a; margin-bottom: 10px; }
.export-card .cumgrid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 14px; }
.export-card .cumv { font-size: 22px; font-weight: 800; }
.export-card .cumk { font-size: 11px; color: #71808c; margin-top: 2px; }
.export-card .cumline { font-size: 13px; color: #3f5362; border-top: 1px solid #d7e0e6; margin-top: 12px; padding-top: 10px; }
.export-card .erow { display: grid; grid-template-columns: 24px 1fr; gap: 10px; padding: 10px 0; border-top: 1px solid #e4e9ed; }
.export-card .eswatch { width: 18px; height: 18px; border-radius: 4px; border: 1px solid rgba(0, 0, 0, .25); margin-top: 2px; }
.export-card .ename { font-size: 17px; font-weight: 750; }
.export-card .emeta { font-size: 14px; color: #607080; margin-top: 3px; }
.export-card .efoot { font-size: 11px; color: #71808c; border-top: 1px solid #dfe5ea; margin-top: 16px; padding-top: 12px; }

.foot { font-size: 10px; color: #6f7e8a; line-height: 1.4; padding: 10px 12px 13px; }

@media (max-width: 700px) {
  .panel { left: 8px; top: 8px; width: calc(100% - 16px); max-height: 52%; border-radius: 11px; }
  h1 { font-size: 17px; }
  button { padding: 7px 4px; font-size: 10px; }
}
