:root {
  --pr-color: #2e7d52;
  --sr-color: #c1440e;
  --or-color: #2b6cb0;
  --sr-link: #d1495b;
  --or-link: #2b6cb0;
  --bg: #f7f7f5;
  --panel-bg: #ffffff;
  --border: #ddd;
  --text: #222;
  --text-muted: #777;
}

* { box-sizing: border-box; }

body {
  margin: 0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Helvetica, Arial, sans-serif;
  background: var(--bg);
  color: var(--text);
}

header {
  padding: 14px 24px;
  background: #1a1a1a;
  color: white;
  display: flex;
  align-items: flex-start;
  justify-content: space-between;
  gap: 16px;
}
header h1 { margin: 0; font-size: 20px; }
header .subtitle { margin: 4px 0 0; font-size: 13px; color: #bbb; }

.header-links {
  display: flex;
  gap: 8px;
  flex-shrink: 0;
}
.header-btn {
  border: 1px solid #555;
  background: #2a2a2a;
  color: #eee;
  border-radius: 6px;
  padding: 6px 12px;
  font-size: 12.5px;
  font-weight: 600;
  cursor: pointer;
  text-decoration: none;
  white-space: nowrap;
}
.header-btn:hover { background: #3a3a3a; }

.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.5);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.modal-overlay.hidden { display: none; }
.modal-card {
  position: relative;
  background: white;
  color: var(--text);
  border-radius: 12px;
  padding: 26px 30px;
  max-width: 560px;
  max-height: 80vh;
  overflow-y: auto;
  box-shadow: 0 10px 40px rgba(0,0,0,0.3);
}
.modal-card h2 { margin-top: 0; }
.modal-card ul { padding-left: 20px; margin: 8px 0 16px; }
.modal-card li { margin-bottom: 6px; font-size: 13.5px; line-height: 1.5; }
.modal-card p { font-size: 13.5px; line-height: 1.5; }
.modal-card .citation { font-size: 12px; color: var(--text-muted); font-style: italic; border-top: 1px solid #eee; padding-top: 12px; margin-top: 16px; }
.modal-card .popup-close { font-size: 22px; }

main {
  display: flex;
  height: calc(100vh - 66px);
}

#map-panel {
  flex: 1;
  position: relative;
  background: #fdfdfb;
  border-right: 1px solid var(--border);
  overflow: hidden;
}

#map {
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none; /* we handle pan/pinch-zoom ourselves */
  cursor: grab;
}
#map.panning { cursor: grabbing; }

#zoom-controls {
  position: absolute;
  right: 14px;
  bottom: 14px;
  display: flex;
  flex-direction: column;
  background: rgba(255,255,255,0.92);
  border: 1px solid var(--border);
  border-radius: 8px;
  overflow: hidden;
  box-shadow: 0 2px 8px rgba(0,0,0,0.1);
  z-index: 15;
}
#zoom-controls button {
  width: 34px;
  height: 34px;
  border: none;
  background: none;
  border-bottom: 1px solid var(--border);
  font-size: 17px;
  line-height: 1;
  cursor: pointer;
  color: #333;
}
#zoom-controls button:last-child { border-bottom: none; }
#zoom-controls button:hover { background: #f0f0ee; }
#zoom-reset { font-size: 14px !important; }

.parcel-shape {
  fill: #eaeaea;
  stroke: #b8b8b8;
  stroke-width: 1.2;
  pointer-events: none;
}

.ds-shape {
  fill: rgba(120, 120, 200, 0.08);
  stroke: rgba(90, 90, 160, 0.35);
  stroke-width: 1;
  stroke-dasharray: 3 2;
  pointer-events: none;
}

.region-shape {
  stroke: rgba(0,0,0,0.35);
  stroke-width: 0.6;
  pointer-events: none;
}

.network-node {
  stroke: #1b1b1b;
  stroke-width: 1.2;
  cursor: pointer;
  transition: stroke-width 0.15s ease;
}
.network-node:hover { stroke: #ff3b30; stroke-width: 2.5; }
.network-node.selected { stroke: #ff3b30; stroke-width: 3; }
.network-node.neighbor { stroke: #ff9500; stroke-width: 2.5; }

.building-shape {
  fill: #4a4a4a;
  stroke: #222;
  stroke-width: 1;
  cursor: pointer;
  transition: fill 0.15s ease, stroke-width 0.15s ease;
}
.building-shape:hover {
  stroke: #ff3b30;
  stroke-width: 2.5;
}
.building-shape.selected {
  stroke: #ff3b30;
  stroke-width: 3;
}
.building-shape.neighbor {
  stroke: #ff9500;
  stroke-width: 2.5;
}

.link-line {
  stroke-width: 1.4;
  fill: none;
  opacity: 0.65;
  pointer-events: none;
}
.link-line.sr { stroke: var(--sr-link); }
.link-line.or { stroke: var(--or-link); }
.link-line.removed { display: none; }

.link-arrowhead {
  opacity: 0.9;
  pointer-events: none;
}
.link-arrowhead.removed { display: none; }

/* Network view: links become the focal element, drawn thicker and more opaque
   so the graph structure is legible without the GIS layers competing for attention. */
#map.mode-network .link-line {
  stroke-width: 2.4;
  opacity: 0.85;
}
#map.mode-network .link-arrowhead { opacity: 1; }

.house-label {
  font-size: 10px;
  font-weight: 700;
  fill: #fff;
  stroke: #000;
  stroke-width: 2.5px;
  paint-order: stroke fill;
  pointer-events: none;
  user-select: none;
}
.house-label.hidden { display: none; }

.link-highlight {
  stroke: #ff3b30;
  stroke-width: 2.6;
  fill: none;
  opacity: 0.95;
  pointer-events: none;
}

#legend {
  position: absolute;
  left: 14px;
  bottom: 14px;
  background: rgba(255,255,255,0.92);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 12px;
  font-size: 12px;
  min-width: 160px;
  max-width: 230px;
}
#legend .legend-block + .legend-block { margin-top: 8px; padding-top: 8px; border-top: 1px solid #eee; }
#legend .legend-title { font-weight: 600; margin-bottom: 6px; }
#legend .legend-bar {
  height: 10px;
  border-radius: 3px;
  margin: 4px 0;
}
#legend .legend-range { display: flex; justify-content: space-between; color: var(--text-muted); }

#selection-popup {
  position: absolute;
  left: 14px;
  top: 14px;
  background: rgba(255,255,255,0.97);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 12px 14px;
  font-size: 12.5px;
  min-width: 220px;
  max-width: 260px;
  box-shadow: 0 4px 14px rgba(0,0,0,0.12);
  z-index: 15;
}
.popup-close {
  position: absolute;
  top: 6px;
  right: 8px;
  border: none;
  background: none;
  font-size: 18px;
  line-height: 1;
  cursor: pointer;
  color: var(--text-muted);
  padding: 2px 4px;
}
.popup-close:hover { color: #222; }

#tooltip {
  position: absolute;
  pointer-events: none;
  background: rgba(20,20,20,0.92);
  color: white;
  padding: 6px 9px;
  border-radius: 6px;
  font-size: 12px;
  line-height: 1.4;
  z-index: 20;
}
#tooltip.hidden { display: none; }

#sidebar {
  width: 340px;
  overflow-y: auto;
  padding: 14px;
  background: var(--bg);
}

.panel {
  background: var(--panel-bg);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 14px;
  margin-bottom: 14px;
}
.panel h2 {
  font-size: 14px;
  margin: 0 0 8px;
  text-transform: uppercase;
  letter-spacing: 0.03em;
  color: #444;
}
.hint { font-size: 12px; color: var(--text-muted); margin: 0 0 8px; }

.group-label, .row-label {
  font-size: 11px;
  font-weight: 600;
  color: var(--text-muted);
  margin: 8px 0 4px;
  text-transform: uppercase;
}

.pill-row {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
  margin-bottom: 4px;
}

.pill {
  border: 1.5px solid var(--pill-color, #888);
  color: var(--pill-color, #444);
  background: white;
  border-radius: 999px;
  padding: 5px 12px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.12s ease;
}
.pill:hover { background: color-mix(in srgb, var(--pill-color, #888) 12%, white); }
.pill.active {
  background: var(--pill-color, #888);
  color: white;
}
.pill-none {
  margin-top: 6px;
  --pill-color: #888;
}

.toggle {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
  padding: 4px 0;
  cursor: pointer;
}
.swatch {
  display: inline-block;
  width: 10px;
  height: 10px;
  border-radius: 50%;
}

.sel-id { font-weight: 700; font-size: 15px; margin-bottom: 8px; }
.stat-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12.5px;
  margin-bottom: 10px;
}
.stat-table th, .stat-table td {
  padding: 4px 6px;
  text-align: right;
  border-bottom: 1px solid #eee;
}
.stat-table th:first-child, .stat-table td:first-child { text-align: left; }
.pr-label { color: var(--pr-color); font-weight: 700; }
.sr-label { color: var(--sr-color); font-weight: 700; }
.or-label { color: var(--or-color); font-weight: 700; }

.conn-summary { font-size: 12.5px; display: flex; flex-direction: column; gap: 4px; margin-bottom: 10px; }

#sim-play, #sim-reset, .clear-btn {
  border: 1px solid var(--border);
  background: #f0f0ee;
  border-radius: 6px;
  padding: 6px 10px;
  font-size: 12.5px;
  cursor: pointer;
}
#sim-play:hover, #sim-reset:hover, .clear-btn:hover { background: #e6e6e2; }
.clear-btn { width: 100%; margin-top: 4px; }

.slider-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin: 10px 0 8px;
}
.slider-row input[type=range] { flex: 1; }
#sim-slider-label { font-size: 12.5px; font-weight: 600; width: 42px; text-align: right; }

.sim-buttons { display: flex; gap: 8px; margin-bottom: 10px; }

.sim-stats {
  font-size: 12.5px;
  display: flex;
  flex-direction: column;
  gap: 3px;
  background: #f6f6f3;
  border-radius: 6px;
  padding: 8px 10px;
}

.hidden { display: none !important; }

/* ------------------------------------------------------------------
   Mobile: stack the map above the controls instead of a side-by-side
   layout, give the map a fixed viewport-relative height so it doesn't
   collapse, and let the page itself scroll rather than clipping the
   sidebar. Popup/legend/modal shrink to fit a narrow screen.
   ------------------------------------------------------------------ */
@media (max-width: 820px) {
  header {
    flex-direction: column;
    align-items: flex-start;
    gap: 8px;
    padding: 12px 16px;
  }
  header h1 { font-size: 17px; }
  .header-links { align-self: stretch; }
  .header-btn { flex: 1; text-align: center; }

  main {
    flex-direction: column;
    height: auto;
  }

  #map-panel {
    flex: none;
    width: 100%;
    height: 58vh;
    min-height: 320px;
    border-right: none;
    border-bottom: 1px solid var(--border);
  }

  #sidebar {
    width: 100%;
    max-height: none;
    overflow-y: visible;
    padding: 12px;
  }

  #selection-popup {
    left: 6px;
    top: 6px;
    right: auto;
    min-width: 0;
    width: 45vw;
    max-width: 190px;
    max-height: 46vh;
    overflow-y: auto;
    padding: 8px 9px 6px;
    font-size: 10.5px;
  }
  #selection-popup .sel-id { font-size: 12px; margin-bottom: 5px; }
  #selection-popup .stat-table { font-size: 10px; margin-bottom: 6px; }
  #selection-popup .stat-table th, #selection-popup .stat-table td { padding: 2px 3px; }
  #selection-popup .conn-summary { font-size: 10px; gap: 2px; margin-bottom: 6px; }
  #selection-popup .clear-btn { padding: 4px 8px; font-size: 10.5px; }
  #selection-popup .popup-close { font-size: 15px; top: 3px; right: 4px; }

  #legend {
    left: 6px;
    bottom: 6px;
    width: 34vw;
    max-width: 130px;
    max-height: 30vh;
    overflow-y: auto;
    padding: 6px 8px;
    font-size: 9.5px;
  }
  #legend .legend-block + .legend-block { margin-top: 5px; padding-top: 5px; }
  #legend .legend-title { margin-bottom: 3px; line-height: 1.25; }
  #legend .legend-bar { height: 7px; margin: 2px 0; }

  #zoom-controls { right: 8px; bottom: 6px; }
  #zoom-controls button { width: 28px; height: 28px; font-size: 15px; }

  .modal-card {
    max-width: 92vw;
    max-height: 85vh;
    padding: 20px 18px;
  }

  /* Slightly larger tap targets */
  .pill { padding: 8px 13px; }
  .toggle { padding: 7px 0; font-size: 14px; }
  .toggle input[type="checkbox"] { width: 18px; height: 18px; }
}
