:root {
  --bg: #f4f5f7;
  --panel: #ffffff;
  --ink: #1b2733;
  --ink-soft: #5b6b7b;
  --line: #e2e6ea;
  --accent: #c8102e;       /* Canada red */
  --accent-dark: #9c0c23;
  --shadow: 0 2px 8px rgba(20, 30, 45, .10), 0 8px 24px rgba(20, 30, 45, .08);
  --radius: 10px;
  /* Classification / priority colours (shared with the map legend) */
  --c-1: #d7191c;  /* High */
  --c-2: #fdae61;  /* Medium */
  --c-3: #f7e36b;  /* Low */
  --c-N: #1a9641;  /* Not a priority */
  --c-I: #9aa6b2;  /* Insufficient info */
  --c-0: #b0bac4;  /* unknown */
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, Helvetica, Arial, sans-serif;
  color: var(--ink);
  background: var(--bg);
  -webkit-font-smoothing: antialiased;
}

#app {
  display: grid;
  grid-template-columns: 360px 1fr;
  grid-template-rows: 60px 1fr;
  grid-template-areas: "top top" "side map";
  height: 100vh;
  width: 100vw;
}

/* ---------- Top bar ---------- */
#topbar {
  grid-area: top;
  display: flex;
  align-items: center;
  gap: 18px;
  padding: 0 18px;
  background: var(--panel);
  border-bottom: 1px solid var(--line);
  z-index: 30;
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 230px; }
.brand-mark {
  width: 26px; height: 26px; border-radius: 6px;
  background: var(--accent);
  position: relative; flex: none;
}
.brand-mark::after {
  content: ""; position: absolute; inset: 6px;
  background: #fff;
  clip-path: polygon(50% 0, 61% 35%, 98% 35%, 68% 57%, 79% 100%, 50% 75%, 21% 100%, 32% 57%, 2% 35%, 39% 35%);
}
.brand-text { display: flex; flex-direction: column; line-height: 1.15; }
.brand-text strong { font-size: 15px; }
.brand-text small { color: var(--ink-soft); font-size: 12px; }

.search-wrap { flex: 1; max-width: 520px; }
#search {
  width: 100%; height: 38px; padding: 0 14px;
  border: 1px solid var(--line); border-radius: 999px;
  font-size: 14px; background: #fbfcfd; outline: none;
}
#search:focus { border-color: var(--accent); background: #fff; }

.topbar-actions { display: flex; gap: 10px; margin-left: auto; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 999px; overflow: hidden; }
.seg button {
  border: 0; background: #fff; color: var(--ink-soft);
  padding: 7px 13px; font-size: 12px; font-weight: 600; cursor: pointer;
}
.seg button.active { background: var(--ink); color: #fff; }

/* ---------- Sidebar ---------- */
#sidebar {
  grid-area: side;
  background: var(--panel);
  border-right: 1px solid var(--line);
  display: flex; flex-direction: column;
  overflow: hidden;
  z-index: 20;
}
.stats {
  display: grid; grid-template-columns: 1fr 1fr; gap: 1px;
  background: var(--line);
  border-bottom: 1px solid var(--line);
}
.stat { background: var(--panel); padding: 12px 16px; }
.stat .big { font-size: 22px; font-weight: 700; letter-spacing: -.4px; }
.stat .lbl { font-size: 11px; color: var(--ink-soft); text-transform: uppercase; letter-spacing: .04em; }

.data-date { font-size: 11px; color: var(--ink-soft); padding: 7px 16px; border-bottom: 1px solid var(--line); background: #fbfcfd; }
.data-date:empty { display: none; }

.sidebar-footer { padding: 10px 16px; border-top: 1px solid var(--line); background: #fbfcfd; display: flex; flex-direction: column; gap: 4px; }
.sidebar-footer a { font-size: 11px; color: var(--ink-soft); text-decoration: none; line-height: 1.4; }
.sidebar-footer a:hover { color: var(--accent); text-decoration: underline; }
.sidebar-footer .disclaimer { font-size: 10.5px; color: #8a97a4; line-height: 1.35; }
.sidebar-footer .disclaimer a { font-size: 10.5px; color: var(--accent); text-decoration: none; }
.sidebar-footer .disclaimer a:hover { text-decoration: underline; }

.tabs { display: flex; border-bottom: 1px solid var(--line); }
.tabs button {
  flex: 1; border: 0; background: none; padding: 12px;
  font-size: 13px; font-weight: 600; color: var(--ink-soft); cursor: pointer;
  border-bottom: 2px solid transparent;
}
.tabs button.active { color: var(--accent); border-bottom-color: var(--accent); }

.tab-panel { overflow-y: auto; flex: 1; }
.tab-panel.hidden { display: none; }

.filter-actions { padding: 10px 16px 0; text-align: right; }
.link { background: none; border: 0; color: var(--accent); font-size: 12px; cursor: pointer; padding: 4px; font-weight: 600; }
.link:hover { text-decoration: underline; }

.fgroup { border-bottom: 1px solid var(--line); padding: 6px 16px 12px; }
.fgroup > h4 {
  margin: 10px 0 8px; font-size: 12px; text-transform: uppercase;
  letter-spacing: .04em; color: var(--ink-soft); cursor: pointer;
  display: flex; justify-content: space-between; align-items: center;
}
.fgroup > h4 .chev { transition: transform .15s; font-size: 10px; }
.fgroup.collapsed > h4 .chev { transform: rotate(-90deg); }
.fgroup.collapsed .opts { display: none; }
.opts { display: flex; flex-direction: column; gap: 3px; max-height: 220px; overflow-y: auto; }
.opt {
  display: flex; align-items: center; gap: 8px; font-size: 13px;
  padding: 4px 6px; border-radius: 6px; cursor: pointer; user-select: none;
}
.opt:hover { background: #f5f7f9; }
.opt input { accent-color: var(--accent); }
.opt .swatch { width: 11px; height: 11px; border-radius: 3px; flex: none; }
.opt .count { margin-left: auto; color: var(--ink-soft); font-size: 11px; font-variant-numeric: tabular-nums; }
.opt.disabled { opacity: .4; }

/* ---------- Results list ---------- */
.results-head {
  display: flex; align-items: center; gap: 8px; padding: 10px 16px;
  border-bottom: 1px solid var(--line); font-size: 12px; color: var(--ink-soft);
}
.results-head select { flex: 1; padding: 5px; border: 1px solid var(--line); border-radius: 6px; font-size: 12px; }
.result-list { padding: 6px; }
.rrow {
  padding: 9px 10px; border-radius: 8px; cursor: pointer; border: 1px solid transparent;
}
.rrow:hover { background: #f5f7f9; }
.rrow.active { background: #fdeef0; border-color: #f6c9d0; }
.rrow .rtitle { font-size: 13px; font-weight: 600; display: flex; align-items: center; gap: 7px; }
.rrow .rdot { width: 9px; height: 9px; border-radius: 50%; flex: none; }
.rrow .rmeta { font-size: 11.5px; color: var(--ink-soft); margin-top: 2px; }
.rrow .rspend { font-size: 11.5px; color: var(--ink); font-weight: 600; font-variant-numeric: tabular-nums; }
.list-note { padding: 12px 16px; font-size: 12px; color: var(--ink-soft); text-align: center; }

/* ---------- Map ---------- */
#map { grid-area: map; position: relative; }
.legend {
  position: absolute; left: 12px; top: 12px; z-index: 5;
  background: rgba(255,255,255,.97); border: 1px solid var(--line);
  border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 11px 13px; font-size: 12px; width: 188px;
}
.legend h5 { margin: 0 0 7px; font-size: 11px; text-transform: uppercase; color: var(--ink-soft); letter-spacing: .04em; }
.legend-head { display: flex; align-items: center; justify-content: space-between; cursor: pointer; user-select: none; }
.legend-head .caret { font-size: 10px; transition: transform .15s; }
.legend.collapsed .legend-head { margin-bottom: 0; }
.legend.collapsed .legend-head .caret { transform: rotate(-90deg); }
.legend.collapsed .legend-rows { display: none; }
.legend-toggle { display: flex; border: 1px solid #d4dae0; border-radius: 8px; overflow: hidden; margin-bottom: 9px; }
.legend-toggle button { flex: 1; border: 0; background: #eef1f4; color: var(--ink-soft); padding: 7px 8px; font-size: 12px; font-weight: 600; cursor: pointer; transition: background .12s; }
.legend-toggle button:hover { background: #e2e6ea; }
.legend-toggle button.active { background: var(--accent); color: #fff; }
.legend .row { display: flex; align-items: center; gap: 7px; padding: 2px 0; }
.legend .dot { width: 12px; height: 12px; border-radius: 50%; border: 1px solid rgba(0,0,0,.15); }

/* ---------- Detail drawer ---------- */
.detail {
  position: absolute; top: 60px; right: 0; bottom: 0; width: 420px; max-width: 92vw;
  background: var(--panel); border-left: 1px solid var(--line);
  box-shadow: var(--shadow); z-index: 70; overflow-y: auto;
  transform: translateX(0); visibility: visible;
  transition: transform .22s ease, visibility 0s;
}
/* visibility:hidden guarantees the panel is gone even if a browser (e.g. iOS
   Safari) mis-handles the off-screen transform — fixes a blank panel on load. */
.detail.hidden { transform: translateX(105%); visibility: hidden; transition: transform .22s ease, visibility 0s .22s; }
.detail-close {
  position: sticky; top: 0; float: right; margin: 10px 12px 0;
  border: 0; background: #eef1f4; width: 30px; height: 30px; border-radius: 50%;
  font-size: 20px; line-height: 1; cursor: pointer; color: var(--ink-soft); z-index: 2;
}
#detail-body { padding: 18px 22px 60px; }
.d-status { display: inline-flex; align-items: center; gap: 6px; font-size: 12px; font-weight: 600; padding: 3px 10px; border-radius: 999px; }
.d-title { font-size: 20px; font-weight: 700; margin: 12px 0 2px; letter-spacing: -.3px; }
.d-sub { color: var(--ink-soft); font-size: 13px; margin-bottom: 14px; }
.d-section { border-top: 1px solid var(--line); padding: 14px 0; }
.d-section h4 { margin: 0 0 8px; font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--ink-soft); }
.d-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px 14px; font-size: 13px; }
.d-grid .k { color: var(--ink-soft); font-size: 11px; }
.d-grid .v { font-weight: 600; }
.chips { display: flex; flex-wrap: wrap; gap: 6px; }
.media-group { margin-bottom: 10px; }
.media-group:last-child { margin-bottom: 0; }
.media-label { font-size: 12px; font-weight: 700; color: var(--ink); margin-bottom: 5px; display: flex; align-items: center; gap: 6px; }
.media-label::before { content: ""; width: 7px; height: 7px; border-radius: 2px; background: #4e79a7; flex: none; }
.chip { background: #eef1f4; border-radius: 6px; padding: 4px 9px; font-size: 12px; }
.chip.media { background: #e7f0fb; }
.d-money { display: flex; gap: 18px; margin-bottom: 10px; }
.d-step { font-size: 13px; color: #2c3a47; background: #f5f7f9; border-left: 3px solid var(--accent); padding: 8px 12px; border-radius: 0 6px 6px 0; margin-bottom: 4px; }
.d-money .m-big { font-size: 19px; font-weight: 700; }
.d-money .m-lbl { font-size: 11px; color: var(--ink-soft); }
.d-prose { font-size: 13px; line-height: 1.5; color: #2c3a47; white-space: pre-wrap; }
.d-link { display: inline-block; margin-top: 8px; color: var(--accent); font-weight: 600; font-size: 13px; text-decoration: none; }
.d-link:hover { text-decoration: underline; }
.chart-wrap { position: relative; height: 200px; }

/* ---------- Loading / token warning ---------- */
.loading {
  position: absolute; inset: 60px 0 0 360px; display: flex; gap: 12px;
  align-items: center; justify-content: center; background: var(--bg);
  z-index: 50; font-size: 14px; color: var(--ink-soft);
}
.loading.hidden { display: none; }
.spinner { width: 22px; height: 22px; border: 3px solid var(--line); border-top-color: var(--accent); border-radius: 50%; animation: spin .8s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.token-warning {
  position: absolute; top: 72px; left: 50%; transform: translateX(-50%);
  background: #2b2f36; color: #fff; padding: 12px 18px; border-radius: 10px;
  z-index: 60; font-size: 13px; max-width: 560px; box-shadow: var(--shadow);
}
.token-warning.hidden { display: none; }
.token-warning code { background: rgba(255,255,255,.15); padding: 1px 5px; border-radius: 4px; }

/* ---------- Consent modal ---------- */
.consent-overlay {
  position: fixed; inset: 0; z-index: 200;
  background: rgba(20, 30, 45, .55); backdrop-filter: blur(2px);
  display: flex; align-items: center; justify-content: center; padding: 20px;
}
.consent-overlay.hidden { display: none; }
.consent-card {
  background: var(--panel); border-radius: 14px; box-shadow: var(--shadow);
  max-width: 440px; width: 100%; padding: 26px 28px 24px; text-align: center;
}
.consent-mark { width: 40px; height: 40px; border-radius: 9px; background: var(--accent); position: relative; margin: 0 auto 14px; }
.consent-mark::after { content: ""; position: absolute; inset: 9px; background: #fff;
  clip-path: polygon(50% 0, 61% 35%, 98% 35%, 68% 57%, 79% 100%, 50% 75%, 21% 100%, 32% 57%, 2% 35%, 39% 35%); }
.consent-card h2 { margin: 0 0 14px; font-size: 19px; letter-spacing: -.3px; }
.consent-card p { margin: 0 0 12px; font-size: 13.5px; line-height: 1.5; color: #2c3a47; }
.consent-card a { color: var(--accent); font-weight: 600; text-decoration: none; }
.consent-card a:hover { text-decoration: underline; }
.consent-card #consent-unofficial { color: var(--ink-soft); }
.consent-card #consent-verify { font-size: 12.5px; color: var(--ink-soft); background: #f5f7f9; border-radius: 8px; padding: 10px 12px; }
.consent-card #consent-date { font-size: 12px; color: var(--ink-soft); margin-bottom: 18px; }
.consent-card #consent-date:empty { display: none; }
.consent-btn {
  width: 100%; border: 0; background: var(--accent); color: #fff;
  padding: 12px; border-radius: 9px; font-size: 14px; font-weight: 600; cursor: pointer;
}
.consent-btn:hover { background: var(--accent-dark); }

.mapboxgl-popup-content { font-family: inherit; font-size: 12px; padding: 8px 12px; border-radius: 8px; }

@media (max-width: 820px) {
  /* Let the whole page scroll instead of locking to 100vh — otherwise the
     filter list gets squeezed into a tiny inner scroll area below the fold. */
  html, body { height: auto; }
  #app { height: auto; min-height: 100vh; grid-template-columns: 1fr;
         grid-template-rows: auto 46vh auto; grid-template-areas: "top" "map" "side"; }

  /* Top bar wraps: brand + toggles on row 1, full-width search on row 2. */
  #topbar { flex-wrap: wrap; height: auto; padding: 9px 12px; gap: 9px; row-gap: 9px; }
  .brand { min-width: 0; flex: 1; }
  .brand-text strong { font-size: 14px; }
  .brand-text small { display: none; }
  .topbar-actions { margin-left: 0; }
  .search-wrap { order: 3; flex-basis: 100%; max-width: none; }
  #search { height: 36px; }
  .seg button { padding: 9px 13px; }

  /* Sidebar flows with the page; filters expand fully (no nested scroll). */
  #sidebar { border-right: 0; border-top: 1px solid var(--line); overflow: visible; }
  .tab-panel { overflow-y: visible; }
  .opts { max-height: none; }

  .loading { position: fixed; inset: 0; }
  .detail { position: fixed; top: auto; width: 100%; height: 80vh; bottom: 0; border-left: 0; border-top: 1px solid var(--line); border-radius: 14px 14px 0 0; }
  .detail.hidden { transform: translateY(105%); }

  /* Smaller, collapsible legend so it doesn't swallow the short map. */
  .legend { max-width: 64vw; padding: 9px 11px; }
  .opt { padding: 8px 6px; }
  .stat { padding: 11px 14px; }
}
