:root {
  --bg: #f6f5f2;
  --surface: #ffffff;
  --text: #1d2125;
  --muted: #5d6670;
  --line: #e2e0da;
  --accent: #1f5f8b;
  --accent-soft: #e4eef5;
  --shadow: 0 6px 24px rgba(20, 30, 40, .14);
  --size-tiny: #7d93a8;
  --size-small: #2a8f84;
  --size-medium: #d9902c;
  --size-large: #c0412f;
  color-scheme: light;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #15181b; --surface: #1e2226; --text: #e7e9ea; --muted: #9aa3ab; --line: #30363c;
    --accent: #7fb6dc; --accent-soft: #23313c; --shadow: 0 6px 24px rgba(0,0,0,.5);
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --bg: #15181b; --surface: #1e2226; --text: #e7e9ea; --muted: #9aa3ab; --line: #30363c;
  --accent: #7fb6dc; --accent-soft: #23313c; --shadow: 0 6px 24px rgba(0,0,0,.5);
  color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg); color: var(--text);
  font: 14px/1.45 -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}
#map { position: fixed; inset: 0; }

.panel {
  position: fixed; top: 12px; left: 12px; width: 340px; max-height: calc(100% - 24px);
  display: flex; flex-direction: column;
  background: var(--surface); border: 1px solid var(--line); border-radius: 12px;
  box-shadow: var(--shadow); z-index: 2; overflow: hidden;
}
.panel-head { display: flex; align-items: flex-start; justify-content: space-between; gap: 8px; padding: 14px 16px 10px; }
h1 { font-size: 17px; margin: 0; letter-spacing: -.01em; }
.sub { margin: 2px 0 0; color: var(--muted); font-size: 12.5px; }
.panel-body { padding: 0 16px 14px; overflow-y: auto; }
.icon-btn {
  border: 1px solid var(--line); background: transparent; color: var(--text);
  border-radius: 8px; width: 32px; height: 32px; display: grid; place-items: center; cursor: pointer;
}
.icon-btn[aria-expanded="false"] svg { transform: rotate(-90deg); }

.stat { display: flex; align-items: baseline; gap: 8px; padding: 6px 0 10px; border-bottom: 1px solid var(--line); margin-bottom: 12px; }
.stat-num { font-size: 26px; font-weight: 650; font-variant-numeric: tabular-nums; letter-spacing: -.02em; }
.stat-label { color: var(--muted); }

.field { display: block; border: 0; padding: 0; margin: 0 0 14px; min-width: 0; }
.field-label { display: block; font-size: 12px; font-weight: 600; color: var(--muted); margin-bottom: 6px; text-transform: uppercase; letter-spacing: .04em; padding: 0; }
input[type="search"], select {
  width: 100%; height: 36px; padding: 0 10px; font: inherit; color: var(--text);
  background: var(--bg); border: 1px solid var(--line); border-radius: 8px;
}
input[type="search"]:focus, select:focus { outline: 2px solid var(--accent); outline-offset: 0; border-color: transparent; }

.seg { display: grid; grid-template-columns: repeat(3, 1fr); background: var(--bg); border: 1px solid var(--line); border-radius: 8px; padding: 3px; gap: 3px; }
.seg label { position: relative; }
.seg input { position: absolute; opacity: 0; inset: 0; cursor: pointer; }
.seg span { display: block; text-align: center; padding: 5px 4px; border-radius: 6px; font-size: 13px; }
.seg input:checked + span { background: var(--surface); box-shadow: 0 1px 3px rgba(0,0,0,.15); font-weight: 600; }
.seg input:focus-visible + span { outline: 2px solid var(--accent); }

.sizes { display: grid; grid-template-columns: 1fr 1fr; gap: 6px; }
.size-chip { display: flex; align-items: center; gap: 8px; padding: 6px 8px; border: 1px solid var(--line); border-radius: 8px; cursor: pointer; font-size: 13px; }
.size-chip input { margin: 0; accent-color: var(--accent); }
.dot { width: 10px; height: 10px; border-radius: 50%; flex: none; }
.size-chip small { color: var(--muted); margin-left: auto; font-variant-numeric: tabular-nums; }

.check { display: flex; gap: 8px; align-items: flex-start; margin-bottom: 12px; cursor: pointer; }
.check input { margin-top: 3px; accent-color: var(--accent); }
.check small { color: var(--muted); }

.results ol { list-style: none; margin: 0 0 12px; padding: 0; border: 1px solid var(--line); border-radius: 8px; max-height: 240px; overflow-y: auto; }
.results li { padding: 7px 10px; border-bottom: 1px solid var(--line); cursor: pointer; }
.results li:last-child { border-bottom: 0; }
.results li:hover, .results li:focus { background: var(--accent-soft); outline: none; }
.results .r-name { font-weight: 600; display: block; }
.results .r-biz { display: block; font-size: 12.5px; }
.results .r-meta { color: var(--muted); font-size: 12.5px; }
.results-head { margin-bottom: 6px; }
.results-head .field-label { margin-bottom: 1px; }
.results-note { display: block; color: var(--muted); font-size: 12px; }
.results ol { max-height: 340px; }

.foot { color: var(--muted); font-size: 12px; border-top: 1px solid var(--line); padding-top: 10px; }
.foot a { color: var(--accent); }

.loading {
  position: fixed; inset: 0; display: grid; place-content: center; justify-items: center; gap: 12px;
  background: color-mix(in srgb, var(--bg) 80%, transparent); z-index: 5; color: var(--muted);
}
.loading[hidden] { display: none; }
.spinner { width: 34px; height: 34px; border-radius: 50%; border: 3px solid var(--line); border-top-color: var(--accent); animation: spin 0.9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

/* Popup */
.maplibregl-popup-content { background: var(--surface); color: var(--text); border-radius: 10px; padding: 12px 14px; box-shadow: var(--shadow); max-width: 340px; max-height: 55vh; overflow-y: auto; }
.maplibregl-popup-tip { border-top-color: var(--surface) !important; border-bottom-color: var(--surface) !important; }
.maplibregl-popup-close-button { color: var(--muted); font-size: 18px; padding: 2px 6px; }
.pop h2 { font-size: 15px; margin: 0 22px 2px 0; }
.pop .legal { color: var(--muted); font-size: 12.5px; margin: 0 0 8px; }
.pop .badges { display: flex; flex-wrap: wrap; gap: 4px; margin-bottom: 8px; }
.badge { font-size: 11.5px; padding: 1px 7px; border-radius: 999px; background: var(--accent-soft); color: var(--text); }
.pop dl { display: grid; grid-template-columns: auto 1fr; gap: 3px 10px; margin: 0 0 8px; font-size: 13px; }
.pop dt { color: var(--muted); }
.pop dd { margin: 0; font-variant-numeric: tabular-nums; }
.pop table { width: 100%; border-collapse: collapse; font-size: 12.5px; font-variant-numeric: tabular-nums; margin-bottom: 8px; }
.pop th, .pop td { text-align: right; padding: 2px 4px; border-bottom: 1px solid var(--line); }
.pop th:first-child, .pop td:first-child { text-align: left; }
.pop th { color: var(--muted); font-weight: 600; }
.pop h3 { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 8px 0 3px; }
.pop ul { margin: 0; padding-left: 16px; font-size: 12.5px; max-height: 110px; overflow-y: auto; }
.pop .note { color: var(--muted); font-size: 12px; }
.pop-list { max-height: 260px; overflow-y: auto; margin: 0; padding: 0; list-style: none; }
.pop-list li { padding: 5px 0; border-bottom: 1px solid var(--line); cursor: pointer; font-size: 13px; }
.pop-list li:hover { color: var(--accent); }

/* Level toggle (Nation / State / County) */
.view-wrap { padding: 0 16px 12px; border-bottom: 1px solid var(--line); }
.view-toggle { display: grid; grid-template-columns: repeat(3, 1fr); gap: 4px; padding: 4px;
  background: var(--bg); border: 1px solid var(--line); border-radius: 10px; }
.view-toggle button { height: 40px; border: 0; border-radius: 7px; background: transparent; color: var(--text);
  font: 600 15px/1 inherit; font-family: inherit; cursor: pointer; }
.view-toggle button:hover:not(:disabled) { background: var(--accent-soft); }
.view-toggle button[aria-checked="true"] { background: var(--accent); color: #fff; box-shadow: 0 1px 3px rgba(0,0,0,.2); }
.view-toggle button:disabled { color: var(--muted); opacity: .55; cursor: not-allowed; }
.view-toggle button:focus-visible { outline: 2px solid var(--accent); outline-offset: 1px; }
.place-row { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; margin-top: 10px; }
.place { font-weight: 650; font-size: 15px; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.link-btn { border: 0; background: none; padding: 0; color: var(--accent); font: inherit; font-size: 13px; cursor: pointer; text-decoration: underline; flex: none; }
.hint { color: var(--muted); font-size: 12.5px; margin: 4px 0 0; }
.panel-body { padding-top: 12px; }
body.busy, body.busy .bubble { cursor: progress; }

/* Advanced filters */
.advanced { border: 1px solid var(--line); border-radius: 8px; margin-bottom: 12px; }
.advanced summary { display: flex; align-items: baseline; gap: 8px; padding: 8px 10px; cursor: pointer; list-style: none; }
.advanced summary::-webkit-details-marker { display: none; }
.advanced summary::before { content: "▸"; color: var(--muted); font-size: 11px; transition: transform .15s; }
.advanced[open] summary::before { transform: rotate(90deg); }
.adv-title { font-weight: 600; font-size: 13px; }
.adv-note { color: var(--muted); font-size: 12px; margin-left: auto; text-align: right; }
.adv-body { padding: 4px 10px 2px; border-top: 1px solid var(--line); }
.adv-body .field:first-child { margin-top: 10px; }

/* Bubbles: one per state (abbreviation and count) or per county (count) */
.bubble {
  display: grid; place-content: center; justify-items: center; padding: 0; border-radius: 50%; cursor: pointer;
  background: rgba(31, 95, 139, .86); color: #fff; line-height: 1.05;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Arial, sans-serif; font-variant-numeric: tabular-nums;
  border: 1.5px solid rgba(255, 255, 255, .92); box-shadow: 0 1px 4px rgba(0, 0, 0, .25);
}
.bubble:hover, .bubble:focus-visible { background: #164a6d; outline: none; box-shadow: 0 0 0 3px rgba(31, 95, 139, .35); }
.bubble[hidden] { display: none; }
.bubble-state b { font-size: 12px; font-weight: 700; letter-spacing: .02em; }
.bubble-state span { font-size: 10.5px; font-weight: 500; opacity: .92; }
.bubble-county span { font-size: 10.5px; font-weight: 650; }
@media (max-width: 700px) {
  .bubble-state b { font-size: 10px; }
  .bubble-state span, .bubble-county span { font-size: 8.5px; }
}
.muted { color: var(--muted); }


@media (max-width: 640px) {
  .panel { top: auto; bottom: 0; left: 0; right: 0; width: auto; max-height: 62%; border-radius: 14px 14px 0 0; }
  .panel-head { padding: 12px 16px 8px; }
  .maplibregl-ctrl-top-right { top: 0; }
  .maplibregl-popup-content { max-width: calc(100vw - 32px); }
}

/* Right panel: firm analysis (compare firms in, then filters, search, largest firms) */
.builder {
  position: fixed; top: 12px; right: 12px; width: 380px; max-height: calc(100% - 24px);
  display: flex; flex-direction: column; background: var(--surface); border: 1px solid var(--line);
  border-radius: 12px; box-shadow: var(--shadow); z-index: 2; overflow: hidden;
}
.builder[hidden] { display: none; }
.has-builder .maplibregl-ctrl-top-right { right: 392px; }
.b-head { display: flex; align-items: center; justify-content: space-between; padding: 14px 16px 6px; }
.b-head h2 { font-size: 17px; margin: 0; letter-spacing: -.01em; }
.b-collapse { display: none; }
.b-body { padding: 0 16px; overflow-y: auto; flex: 1 1 auto; min-height: 0; }
.b-step { padding: 8px 0 10px; border-bottom: 1px solid var(--line); }
.b-compare { padding-top: 4px; margin-bottom: 4px; }
.b-body .stat { margin-bottom: 10px; }
.b-body .results ol { max-height: 300px; }
.b-step .field-label small { text-transform: none; letter-spacing: 0; font-weight: 400; }
.b-mode-body { margin-top: 8px; }
.seg-small span { padding: 4px 4px; font-size: 12.5px; }
.b-hint { color: var(--muted); font-size: 12.5px; margin: 6px 0 4px; }
.b-btn { width: 100%; height: 36px; border: 1px solid var(--accent); border-radius: 8px; background: var(--accent-soft);
  color: var(--text); font: 600 13.5px/1 inherit; font-family: inherit; cursor: pointer; }
.b-btn:hover { background: var(--accent); color: #fff; }
.drawing .b-btn { background: var(--accent); color: #fff; }
.b-clear { margin-top: 6px; }
.chips { display: flex; flex-wrap: wrap; gap: 5px; margin-top: 6px; }
.chip { display: inline-flex; align-items: center; gap: 4px; padding: 2px 4px 2px 9px; border-radius: 999px;
  background: var(--accent-soft); border: 1px solid color-mix(in srgb, var(--accent) 40%, transparent); font-size: 12.5px; max-width: 100%; }
.chip button { border: 0; background: none; color: var(--muted); cursor: pointer; font-size: 15px; line-height: 1; padding: 0 4px; }
.chip button:hover { color: var(--text); }
.b-res { margin: 10px 0 0; }
.b-res .field-label { margin-bottom: 4px; }
.b-final { padding: 10px 16px 12px; border-bottom: 0; border-top: 1px solid var(--line); }
.b-summary { min-height: 40px; }
.b-sumname { font-weight: 650; margin: 0 0 4px; font-size: 13.5px; }
.b-sum { display: grid; grid-template-columns: 1fr auto; gap: 2px 10px; margin: 0 0 10px; font-size: 13px; }
.b-sum dt { color: var(--muted); }
.b-sum dd { margin: 0; text-align: right; font-variant-numeric: tabular-nums; font-weight: 600; }
.b-sum .muted { font-weight: 400; font-size: 12px; }
.b-empty { color: var(--muted); font-size: 12.5px; margin: 0 0 10px; }
.b-warn { color: #a8432f; font-size: 12.5px; margin: 0 0 10px; }
.b-go { display: block; text-align: center; padding: 12px 10px; border-radius: 10px; background: var(--accent); color: #fff;
  font-weight: 700; font-size: 15px; text-decoration: none; box-shadow: 0 1px 3px rgba(0,0,0,.2); }
.b-go:hover { filter: brightness(1.08); }
.b-go.disabled { background: var(--line); color: var(--muted); box-shadow: none; cursor: not-allowed; pointer-events: none; }
.results li { position: relative; }
.results li:has(.r-checkwrap) { padding-left: 36px; }
.r-checkwrap { position: absolute; left: 10px; top: 8px; margin: 0; }
.r-check { width: 16px; height: 16px; accent-color: var(--accent); cursor: pointer; }
.drawing #map canvas { cursor: crosshair !important; }

@media (max-width: 640px) {
  .builder { top: 8px; right: 8px; left: 8px; width: auto; max-height: 48%; z-index: 3; }
  .has-builder .maplibregl-ctrl-top-right { right: 0; top: 120px; }
  .b-collapse { display: grid; }
  .b-head { padding: 10px 14px 4px; }
  .b-head h2 { font-size: 15px; }
  .builder .b-body { display: none; }
  .builder-open .builder .b-body { display: block; }
  .b-final { padding: 6px 14px 10px; border-top: 0; }
  .b-sum { grid-template-columns: auto auto auto auto; font-size: 12px; margin-bottom: 6px; }
  .b-go { padding: 9px 8px; font-size: 14px; }
}

/* Left panel: area summary */
.area-sum { min-height: 120px; }
.a-tiles { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-bottom: 12px; }
.a-tile { background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 9px 10px; display: flex; flex-direction: column; min-width: 0; }
.a-n { font-size: 21px; font-weight: 650; letter-spacing: -.02em; font-variant-numeric: tabular-nums; line-height: 1.15; }
.a-n.up { color: #2a7a52; }
.a-n.down { color: #b2452f; }
.a-l { font-size: 12px; font-weight: 600; line-height: 1.3; margin-top: 2px; }
.a-s { font-size: 11.5px; color: var(--muted); line-height: 1.3; margin-top: 2px; }
.a-go { margin-bottom: 4px; }
.a-go-note { color: var(--muted); font-size: 12px; text-align: center; margin: 0 0 12px; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .a-n.up { color: #6fc497; }
  :root:not([data-theme="light"]) .a-n.down { color: #ec8a72; }
}
