:root {
  --bg: #ffffff;
  --panel: #f4f5f7;
  --border: #d8dce1;
  --ink: #1f2733;
  --muted: #5b6472;
  --accent: #2b6cb0;
  --accent-ink: #ffffff;
  --topbar-h: 52px;
  --sidebar-w: 320px;
  --treemap-h: 300px;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--ink);
  background: var(--bg);
  font-size: 14px;
}

body { display: flex; flex-direction: column; height: 100vh; overflow: hidden; }

/* ---------- top bar ---------- */
#topbar {
  height: var(--topbar-h);
  flex: 0 0 auto;
  display: flex;
  align-items: center; 
  gap: 18px;
  padding: 0 14px;
  background: var(--ink);
  color: #fff;
  overflow-x: auto;
  white-space: nowrap;
}
#topbar h1 { font-size: 15px; margin: 0; font-weight: 600; }
.topgroup { display: flex; align-items: center; gap: 8px; }
.topgroup .lbl { color: #b9c1cc; font-size: 12px; }
#topbar label { display: inline-flex; align-items: center; gap: 4px; cursor: pointer; }
.counts { color: #d5dbe3; font-size: 13px; gap: 16px; }
.counts strong { color: #fff; }

#topbar button {
  background: #3a4453;
  color: #fff;
  border: 1px solid #55606f;
  border-radius: 5px;
  padding: 5px 10px;
  cursor: pointer;
  font-size: 13px;
}
#topbar button:hover { background: #49556a; }
#topbar button.tool.active { background: var(--accent); border-color: var(--accent); }
#exportBtn { background: var(--accent); border-color: var(--accent); font-weight: 600; }
#exportBtn:hover { background: #3a7cc0; }
#addSelBtn { font-size: 15px; font-weight: 700; line-height: 1; }
#addSelBtn.on { background: var(--accent); border-color: var(--accent); }
#addSelBtn.on:hover { background: #3a7cc0; }
#infoBtn {
  width: 26px; height: 26px; padding: 0; border-radius: 50%; flex: 0 0 auto;
  font-family: Georgia, "Times New Roman", serif; font-style: italic; font-weight: 700; font-size: 14px; line-height: 1;
}

/* ---------- body ---------- */
#bodywrap { flex: 1 1 auto; display: flex; min-height: 0; }

#sidebar {
  width: var(--sidebar-w);
  flex: 0 0 var(--sidebar-w);
  background: var(--panel);
  border-right: 1px solid var(--border);
  padding: 12px;
  overflow-y: auto;
}
.filterblock { margin-bottom: 18px; }
.filterblock h2 { font-size: 13px; text-transform: uppercase; letter-spacing: .04em; color: var(--muted); margin: 0 0 8px; }
.field { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.field > span { color: var(--muted); }
.field select, #sicSearch {
  flex: 1; padding: 5px 7px; border: 1px solid var(--border); border-radius: 5px; background: #fff; font-size: 13px;
}
#sicSearch { width: 100%; margin-bottom: 8px; }

.checklist {
  max-height: 260px; overflow-y: auto; border: 1px solid var(--border);
  border-radius: 5px; background: #fff; padding: 4px 6px;
}
.checklist--wrap { max-height: none; }
.checklist label {
  display: flex; align-items: baseline; gap: 6px; padding: 2px 2px; cursor: pointer; line-height: 1.3;
}
.checklist label:hover { background: #eef3fa; }
.checklist input { margin-top: 2px; }
.checklist .cl-count { margin-left: auto; color: var(--muted); font-variant-numeric: tabular-nums; font-size: 12px; padding-left: 8px; }
.checklist .cl-name { flex: 1; }

/* 3-way segmented switch (used for the % change YoY filter) */
.segmented { display: flex; border: 1px solid var(--border); border-radius: 6px; overflow: hidden; background: #fff; }
.segmented .seg {
  flex: 1; padding: 6px 4px; border: none; border-left: 1px solid var(--border);
  background: #fff; color: var(--ink); cursor: pointer; font-size: 13px; line-height: 1.1;
}
.segmented .seg:first-child { border-left: none; }
.segmented .seg:hover { background: #eef3fa; }
.segmented .seg.active { background: var(--accent); color: var(--accent-ink); }
.segmented .seg.active:hover { background: var(--accent); }

.blockfoot { margin-top: 6px; }
.linkbtn { background: none; border: none; color: var(--accent); cursor: pointer; padding: 0; font-size: 12px; }
.linkbtn:hover { text-decoration: underline; }

.slider { margin: 18px 6px 6px; }
.rangelabels { display: flex; justify-content: space-between; color: var(--muted); font-size: 12px; margin-top: 6px; }

/* Header row: block title with the "Reset all filters" button floated to its right */
.blockhead { display: flex; align-items: baseline; justify-content: space-between; gap: 8px; margin-bottom: 8px; }
.blockhead h2 { margin-bottom: 0; }
#resetFilters {
  border: 1px solid var(--border); border-radius: 4px; background: #fff; color: var(--muted);
  cursor: pointer; font-size: 11px; padding: 2px 7px; line-height: 1.4; white-space: nowrap;
}
#resetFilters:hover { background: #eef3fa; color: var(--ink); }
.note { color: var(--muted); font-size: 12px; margin: 0 0 10px; line-height: 1.35; }

/* ---------- main / map / treemap ---------- */
#main { flex: 1 1 auto; display: flex; flex-direction: column; min-width: 0; min-height: 0; position: relative; }
#mapWrap { flex: 1 1 auto; position: relative; min-height: 0; }
#map { position: absolute; inset: 0; }

/* Distribution overlay, bottom-left of the map: selection vs all-shown firms */
#distPanel {
  position: absolute; left: 10px; bottom: 10px; z-index: 500; width: 300px;
  background: rgba(255,255,255,.92); border: 1px solid var(--border); border-radius: 6px;
  box-shadow: 0 1px 6px rgba(0,0,0,.18); overflow: hidden;
}
.dist-head {
  display: flex; align-items: center; justify-content: space-between; gap: 8px;
  padding: 4px 8px; font-size: 11px; color: var(--muted);
  border-bottom: 1px solid var(--border); background: rgba(244,245,247,.9);
}
#distPanel.collapsed { width: auto; }
#distPanel.collapsed .dist-head { border-bottom: none; }
#distPanel.collapsed #distChart { display: none; }
.dist-head-right { display: flex; align-items: center; gap: 7px; }
.dist-head label { display: inline-flex; align-items: center; gap: 3px; cursor: pointer; }
#distTitle { font-weight: 600; color: var(--ink); }
#distChart { height: 120px; }

/* Colour-blind palette toggle, centred just above the map colour bar. Kept in sync
   with the bar: x:0.92 in map.js -> left:92% here (+6px = half the bar thickness to
   centre on it); the bar's len-0.6 (centred) top sits 20% down, matched by top:20%. */
#paletteToggle {
  position: absolute; left: calc(92% + 6px); top: 20%; z-index: 500;
  transform: translate(-50%, calc(-100% - 8px));
  width: 30px; height: 30px; padding: 0;
  border: 1px solid rgba(0,0,0,.2); border-radius: 4px; background: rgba(255,255,255,.92);
  box-shadow: 0 1px 4px rgba(0,0,0,.15); cursor: pointer; font-size: 16px; line-height: 1;
}
#paletteToggle:hover { background: #fff; }
#paletteToggle.on { background: var(--accent); border-color: var(--accent); }

/* Leaflet-style +/- zoom control, overlaid on the top-left of the map */
#mapZoom {
  position: absolute; top: 10px; left: 10px; z-index: 500;
  display: flex; flex-direction: column;
  border: 2px solid rgba(0,0,0,.2); border-radius: 4px; background-clip: padding-box;
  box-shadow: 0 1px 4px rgba(0,0,0,.15);
}
#mapZoom button {
  width: 30px; height: 30px; padding: 0; border: none; background: #fff; color: #333;
  font-size: 18px; line-height: 28px; font-weight: 600; cursor: pointer;
}
#mapZoom button:first-child { border-radius: 2px 2px 0 0; border-bottom: 1px solid #ccc; }
#mapZoom button:last-child { border-radius: 0 0 2px 2px; }
#mapZoom button:hover { background: #f4f4f4; }

#treemapPanel {
  flex: 0 0 var(--treemap-h);
  border-top: 1px solid var(--border);
  display: flex; flex-direction: column; min-height: 0;
  transition: flex-basis .15s ease;
}
#treemapPanel.collapsed { flex-basis: 34px; }
#treemapPanel.collapsed #treemap { display: none; }
.panel-head {
  flex: 0 0 auto; display: flex; align-items: center; justify-content: space-between;
  padding: 6px 12px; background: var(--panel); border-bottom: 1px solid var(--border);
  font-size: 12px; color: var(--muted);
}
.panel-head-right { display: flex; align-items: center; gap: 8px; }
.panel-head label { display: inline-flex; align-items: center; gap: 3px; cursor: pointer; }
#treemap { flex: 1 1 auto; min-height: 0; }

/* ---------- firm popup ---------- */
#firmPopup {
  position: absolute; z-index: 1000; max-width: 320px;
  background: #fff; border: 1px solid var(--border); border-radius: 8px;
  box-shadow: 0 6px 24px rgba(0,0,0,.18); padding: 12px 14px; font-size: 13px; line-height: 1.45;
}
#firmPopup[hidden] { display: none; }
#firmPopupClose {
  position: absolute; top: 6px; right: 8px; border: none; background: none; cursor: pointer;
  font-size: 14px; color: var(--muted);
}
#firmPopupBody h3 { margin: 0 8px 8px 0; font-size: 14px; padding-right: 16px; }
#firmPopupBody .kv { display: flex; justify-content: space-between; gap: 12px; }
#firmPopupBody .kv span:first-child { color: var(--muted); }
#firmPopupBody .pos { color: #1a7f37; font-weight: 600; }
#firmPopupBody .neg { color: #c62828; font-weight: 600; }
#firmPopupBody a { color: var(--accent); }

/* ---------- info / about modal ---------- */
#infoModal[hidden] { display: none; }
#infoModal {
  position: fixed; inset: 0; z-index: 3000;
  display: flex; align-items: center; justify-content: center;
}
#infoBackdrop { position: absolute; inset: 0; background: rgba(0,0,0,.4); }
#infoBox {
  position: relative; z-index: 1; max-width: 480px; width: calc(100% - 40px);
  background: #fff; border: 1px solid var(--border); border-radius: 10px;
  box-shadow: 0 10px 40px rgba(0,0,0,.25); padding: 22px 24px; font-size: 14px; line-height: 1.5;
}
#infoClose {
  position: absolute; top: 8px; right: 10px; border: none; background: none; cursor: pointer;
  font-size: 16px; color: var(--muted);
}
#infoBody { color: var(--ink); }
#infoBody :first-child { margin-top: 0; }
#infoBody p { margin: 10px 0 0; }
#infoBody a { color: var(--accent); }

/* ---------- loading overlay ---------- */
#loading {
  position: fixed; inset: 0; z-index: 2000; background: rgba(255,255,255,.92);
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 14px;
}
#loading[hidden] { display: none; }
#loading p { color: var(--muted); }
.spinner {
  width: 42px; height: 42px; border: 4px solid #d8dce1; border-top-color: var(--accent);
  border-radius: 50%; animation: spin 0.9s linear infinite;
}
@keyframes spin { to { transform: rotate(360deg); } }
