/* ==========================================================================
   Belize Statistical Data Portal — design system
   Tokens follow spec 04 section 4.1. No colour is hard-coded outside section 1,
   which is what keeps the dark theme honest.
   ========================================================================== */

/* --- 1. Tokens ---------------------------------------------------------- */
/* Brand: Statistical Institute of Belize. Burgundy #800020 is SIB's primary
   (buttons, rules and links across sib.org.bz); amber #f5b042 is its accent.
   The warm neutral surfaces echo the SIB site's paper tones. Chart series come
   from the BNSS logo — its blue, green and orange — extended into a validated
   eight-slot ramp. Every value below was contrast-checked in both themes:
   text pairs clear 4.5:1, chart marks clear 3:1. */
:root {
  --bg: #f7f5f2;
  --surface-1: #ffffff;
  --surface-2: #f2efe9;
  --surface-3: #e8e4dc;
  --border: #e2ded4;
  --border-strong: #cdc7ba;

  --text-primary: #1a1a19;
  --text-secondary: #4d4d4d;
  --text-muted: #77736c;
  --text-inverse: #ffffff;

  /* SIB burgundy */
  --brand-700: #5c0017;
  --brand-600: #800020;
  --brand-500: #a01330;
  --brand-100: #f6e7ea;
  /* SIB amber */
  --accent: #f5b042;
  --accent-ink: #8a5a00;

  /* Framework accents — navigation only, never data (spec 04 section 3). */
  --domain-social: #800020;
  --domain-economic: #12888f;
  --domain-environment: #6f7d00;
  --domain-multi: #6b4a9e;

  --ok: #1b7f4b;
  --warn: #8a5a00;
  --danger: #b3261e;

  /* Categorical series — fixed slot order so an entity keeps its colour when
     filters change. Slots 1-3 are the BNSS logo's blue, orange and green. */
  --c1: #1878b8; --c2: #c87f08; --c3: #12888f; --c4: #6f7d00;
  --c5: #d1503f; --c6: #800020; --c7: #6b4a9e; --c8: #4a8a2a;

  --radius-sm: 6px; --radius-md: 10px; --radius-lg: 16px;
  --space-1: 4px; --space-2: 8px; --space-3: 12px; --space-4: 16px;
  --space-6: 24px; --space-8: 32px; --space-12: 48px;
  --shadow-card: 0 1px 2px rgba(60,40,30,.06), 0 2px 8px rgba(60,40,30,.05);
  --shadow-pop: 0 4px 12px rgba(60,40,30,.14), 0 12px 32px rgba(60,40,30,.12);

  --header-h: 64px;
  --max-w: 1440px;
  --font: "Inter", "Source Sans 3", -apple-system, BlinkMacSystemFont, "Segoe UI",
          Roboto, "Helvetica Neue", Arial, sans-serif;
}

/* Burgundy is too dark to read on a dark surface, so the dark theme carries a
   lightened rose of the same hue rather than an automatic inversion. */
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #141211; --surface-1: #1c1a19; --surface-2: #262320; --surface-3: #302c28;
    --border: #38332e; --border-strong: #4c463f;
    --text-primary: #ffffff; --text-secondary: #c9c3ba; --text-muted: #948d84;
    --brand-700: #f5c3cc; --brand-600: #e88ea0; --brand-500: #f0a6b4;
    --brand-100: #3a1620;
    --accent: #f5b042; --accent-ink: #f5b042;
    --domain-social: #e88ea0; --domain-economic: #3fb9bf;
    --domain-environment: #a8bc2e; --domain-multi: #a891e8;
    --ok: #4cc07f; --warn: #f5b042; --danger: #f5836f;
    --c1: #5ba9e0; --c2: #f0ab3a; --c3: #3fb9bf; --c4: #a8bc2e;
    --c5: #f5836f; --c6: #e88ea0; --c7: #a891e8; --c8: #78c452;
    --shadow-card: 0 1px 2px rgba(0,0,0,.4), 0 2px 8px rgba(0,0,0,.3);
    --shadow-pop: 0 4px 12px rgba(0,0,0,.5), 0 12px 32px rgba(0,0,0,.45);
  }
}

:root[data-theme="dark"] {
  --bg: #141211; --surface-1: #1c1a19; --surface-2: #262320; --surface-3: #302c28;
  --border: #38332e; --border-strong: #4c463f;
  --text-primary: #ffffff; --text-secondary: #c9c3ba; --text-muted: #948d84;
  --brand-700: #f5c3cc; --brand-600: #e88ea0; --brand-500: #f0a6b4;
  --brand-100: #3a1620;
  --accent: #f5b042; --accent-ink: #f5b042;
  --domain-social: #e88ea0; --domain-economic: #3fb9bf;
  --domain-environment: #a8bc2e; --domain-multi: #a891e8;
  --ok: #4cc07f; --warn: #f5b042; --danger: #f5836f;
  --c1: #5ba9e0; --c2: #f0ab3a; --c3: #3fb9bf; --c4: #a8bc2e;
  --c5: #f5836f; --c6: #e88ea0; --c7: #a891e8; --c8: #78c452;
  --shadow-card: 0 1px 2px rgba(0,0,0,.4), 0 2px 8px rgba(0,0,0,.3);
  --shadow-pop: 0 4px 12px rgba(0,0,0,.5), 0 12px 32px rgba(0,0,0,.45);
}

/* --- 2. Base ------------------------------------------------------------ */
*, *::before, *::after { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--text-primary);
  font: 400 16px/1.5 var(--font);
  -webkit-font-smoothing: antialiased;
}
h1, h2, h3, h4 { margin: 0 0 var(--space-3); font-weight: 600; line-height: 1.25; }
h1 { font-size: 32px; letter-spacing: -.01em; }
h2 { font-size: 24px; }
h3 { font-size: 20px; }
h4 { font-size: 16px; }
p { margin: 0 0 var(--space-4); }
a { color: var(--brand-600); text-decoration-thickness: 1px; text-underline-offset: 2px; }
a:hover { color: var(--brand-500); }
small, .small { font-size: 14px; }
.muted { color: var(--text-muted); }
.secondary { color: var(--text-secondary); }
code, pre { font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace; font-size: 13px; }
:focus-visible { outline: 3px solid var(--brand-500); outline-offset: 2px; border-radius: 3px; }

.wrap { max-width: var(--max-w); margin: 0 auto; padding: 0 var(--space-6); }
@media (max-width: 767px) { .wrap { padding: 0 var(--space-4); } }

.skip {
  position: absolute; left: -9999px; top: 0; z-index: 100;
  background: var(--brand-600); color: #fff; padding: var(--space-3) var(--space-4);
  border-radius: 0 0 var(--radius-sm) 0;
}
.skip:focus { left: 0; }

/* --- 3. Header ---------------------------------------------------------- */
.hdr {
  position: sticky; top: 0; z-index: 40; height: var(--header-h);
  background: var(--surface-1); border-bottom: 1px solid var(--border);
  display: flex; align-items: center;
}
.hdr .wrap { display: flex; align-items: center; gap: var(--space-6); width: 100%; }
.brand { display: flex; align-items: center; gap: var(--space-3); text-decoration: none; flex: none; }
.brand img.bnss { height: 38px; width: auto; display: block; }
.brand .rule { width: 1px; height: 30px; background: var(--border); flex: none; }
.brand .sib { display: flex; flex-direction: column; gap: 2px; }
.brand .sib img { height: 15px; width: auto; display: block; }
.brand .sib .owner {
  font-size: 10px; letter-spacing: .06em; text-transform: uppercase;
  color: var(--text-muted);
}
/* The SIB lockup is dark ink on transparent; lift it on a dark surface. */
:root[data-theme="dark"] .brand .sib img { filter: invert(1) hue-rotate(180deg) saturate(1.3) brightness(1.15); }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .brand .sib img {
    filter: invert(1) hue-rotate(180deg) saturate(1.3) brightness(1.15);
  }
}
@media (max-width: 900px) { .brand .rule, .brand .sib { display: none; } }

.nav { display: flex; gap: var(--space-1); margin-left: auto; align-items: center; }
.nav a, .nav button {
  font: inherit; font-size: 14px; color: var(--text-secondary);
  padding: 6px 10px; border-radius: var(--radius-sm); text-decoration: none;
  background: none; border: 0; cursor: pointer; white-space: nowrap;
}
.nav a:hover, .nav button:hover { background: var(--surface-2); color: var(--text-primary); }
.nav a[aria-current="page"] { color: var(--brand-600); background: var(--brand-100); font-weight: 600; }
.nav .sep { width: 1px; height: 20px; background: var(--border); margin: 0 var(--space-2); }

.navtoggle { display: none; }
@media (max-width: 900px) {
  .navtoggle { display: inline-flex; }
  .nav { display: none; }
  .nav.open {
    display: flex; position: absolute; top: var(--header-h); left: 0; right: 0;
    flex-direction: column; align-items: stretch; gap: 0;
    background: var(--surface-1); border-bottom: 1px solid var(--border);
    padding: var(--space-2); box-shadow: var(--shadow-card);
  }
  .nav.open a, .nav.open button { padding: 10px 12px; text-align: left; }
  .nav .sep { display: none; }
}

/* --- 4. Layout ---------------------------------------------------------- */
main { padding: var(--space-8) 0 var(--space-12); min-height: 60vh; }
.page-head { margin-bottom: var(--space-6); }
.crumbs { font-size: 13px; color: var(--text-muted); margin-bottom: var(--space-2); }
.crumbs a { color: var(--text-secondary); text-decoration: none; }
.crumbs a:hover { text-decoration: underline; }
.crumbs span + span::before { content: "›"; margin: 0 6px; color: var(--border-strong); }

.cols { display: grid; grid-template-columns: 280px minmax(0, 1fr); gap: var(--space-8); }
@media (max-width: 1199px) { .cols { grid-template-columns: 1fr; gap: var(--space-6); } }

.card {
  background: var(--surface-1); border: 1px solid var(--border);
  border-radius: var(--radius-md); padding: var(--space-6);
}
.card + .card { margin-top: var(--space-4); }
.card-tight { padding: var(--space-4); }

.grid { display: grid; gap: var(--space-4); }
.grid-2 { grid-template-columns: repeat(auto-fit, minmax(280px, 1fr)); }
.grid-3 { grid-template-columns: repeat(auto-fit, minmax(240px, 1fr)); }
.grid-4 { grid-template-columns: repeat(auto-fit, minmax(190px, 1fr)); }

/* --- 5. Buttons and chips ----------------------------------------------- */
.btn {
  display: inline-flex; align-items: center; gap: 6px; font: inherit; font-size: 14px;
  padding: 8px 14px; border-radius: var(--radius-sm); cursor: pointer;
  border: 1px solid var(--border-strong); background: var(--surface-1);
  color: var(--text-primary); text-decoration: none; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn-primary { background: var(--brand-600); border-color: var(--brand-600); color: #fff; }
.btn-primary:hover { background: var(--brand-500); border-color: var(--brand-500); color: #fff; }
.btn-sm { padding: 5px 10px; font-size: 13px; }

.chip {
  display: inline-flex; align-items: center; gap: 6px; font-size: 13px;
  padding: 4px 10px; border-radius: 999px; background: var(--surface-2);
  color: var(--text-secondary); border: 1px solid var(--border); text-decoration: none;
}
.chip-on { background: var(--brand-100); color: var(--brand-700); border-color: transparent; font-weight: 600; }
.chip-code { font-family: ui-monospace, monospace; font-size: 12px; }
.chip-warn { background: color-mix(in srgb, var(--warn) 14%, transparent); color: var(--warn); border-color: transparent; }

.dot { width: 8px; height: 8px; border-radius: 50%; flex: none; display: inline-block; }
.dot-social { background: var(--domain-social); }
.dot-economic { background: var(--domain-economic); }
.dot-environment { background: var(--domain-environment); }
.dot-multi { background: var(--domain-multi); }

/* --- 6. Forms ----------------------------------------------------------- */
input[type="search"], input[type="text"], input[type="password"], input[type="number"],
select, textarea {
  font: inherit; font-size: 15px; color: var(--text-primary);
  background: var(--surface-1); border: 1px solid var(--border-strong);
  border-radius: var(--radius-sm); padding: 9px 12px; width: 100%;
}
select { cursor: pointer; }
label { font-size: 14px; font-weight: 600; display: block; margin-bottom: 4px; }
.field + .field { margin-top: var(--space-4); }
.searchbig { display: flex; gap: var(--space-2); }
.searchbig input { font-size: 17px; padding: 14px 16px; }

/* --- 7. Tables ---------------------------------------------------------- */
.tablewrap { overflow-x: auto; border: 1px solid var(--border); border-radius: var(--radius-md); }
table { border-collapse: collapse; width: 100%; font-size: 14px; }
th, td { text-align: left; padding: 10px 14px; border-bottom: 1px solid var(--border); }
thead th {
  background: var(--surface-2); font-weight: 600; font-size: 13px;
  color: var(--text-secondary); position: sticky; top: 0; z-index: 1;
  white-space: nowrap;
}
tbody tr:last-child td { border-bottom: 0; }
tbody tr:hover { background: var(--surface-2); }
.num { text-align: right; font-variant-numeric: tabular-nums; }
.sticky-col { position: sticky; left: 0; background: var(--surface-1); }
tbody tr:hover .sticky-col { background: var(--surface-2); }

/* --- 8. KPI ------------------------------------------------------------- */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(170px, 1fr)); gap: var(--space-3); }
.kpi {
  background: var(--surface-1); border: 1px solid var(--border);
  border-radius: var(--radius-md); padding: var(--space-4);
}
.kpi .k-label { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--text-muted); }
.kpi .k-value { font-size: 28px; font-weight: 700; font-variant-numeric: tabular-nums; margin-top: 2px; }
.kpi .k-note { font-size: 13px; color: var(--text-secondary); margin-top: 2px; }
.k-up { color: var(--ok); } .k-down { color: var(--danger); } .k-flat { color: var(--text-muted); }

/* --- 9. Thematic tree --------------------------------------------------- */
.tree { font-size: 14px; }
.tree details { margin-bottom: 2px; }
.tree summary {
  cursor: pointer; padding: 6px 8px; border-radius: var(--radius-sm);
  display: flex; align-items: center; gap: 8px; list-style: none;
}
.tree summary::-webkit-details-marker { display: none; }
.tree summary::before {
  content: "▸"; color: var(--text-muted); font-size: 11px; transition: transform .15s;
}
.tree details[open] > summary::before { transform: rotate(90deg); }
.tree summary:hover { background: var(--surface-2); }
.tree .t-name { flex: 1; min-width: 0; }
.tree .t-count { font-size: 12px; color: var(--text-muted); font-variant-numeric: tabular-nums; }
.tree .kids { padding-left: var(--space-4); border-left: 1px solid var(--border); margin-left: 12px; }
.tree a.leaf {
  display: block; padding: 5px 8px; border-radius: var(--radius-sm);
  color: var(--text-secondary); text-decoration: none; font-size: 13px;
}
.tree a.leaf:hover { background: var(--surface-2); color: var(--text-primary); }
.tree a.leaf[aria-current="true"] { background: var(--brand-100); color: var(--brand-700); font-weight: 600; }

/* --- 10. Explorer ------------------------------------------------------- */
.filterbar {
  display: flex; flex-wrap: wrap; gap: var(--space-3); align-items: flex-end;
  padding: var(--space-4); background: var(--surface-1);
  border: 1px solid var(--border); border-radius: var(--radius-md);
}
.filterbar .f { min-width: 170px; flex: 1 1 170px; }
.filterbar label { font-size: 12px; text-transform: uppercase; letter-spacing: .04em; color: var(--text-muted); }
.filterbar select[multiple] { height: auto; min-height: 96px; }

.tabs { display: flex; gap: 2px; border-bottom: 1px solid var(--border); margin-bottom: var(--space-4); }
.tabs a {
  padding: 9px 16px; font-size: 14px; font-weight: 600; text-decoration: none;
  color: var(--text-secondary); border-bottom: 2px solid transparent; margin-bottom: -1px;
}
.tabs a:hover { color: var(--text-primary); }
.tabs a[aria-selected="true"] { color: var(--brand-600); border-bottom-color: var(--brand-600); }

.chartbox { min-height: 420px; width: 100%; }
@media (max-width: 767px) { .chartbox { min-height: 320px; } }
.chart-note { font-size: 13px; color: var(--text-muted); margin-top: var(--space-3); }

.legend { display: flex; flex-wrap: wrap; gap: var(--space-3); margin-top: var(--space-3); font-size: 13px; }
.legend .li { display: flex; align-items: center; gap: 6px; color: var(--text-secondary); }
.legend .sw { width: 12px; height: 12px; border-radius: 3px; }

.empty {
  padding: var(--space-12) var(--space-6); text-align: center; color: var(--text-muted);
  border: 1px dashed var(--border-strong); border-radius: var(--radius-md);
}

/* --- 11. Menus ---------------------------------------------------------- */
.menu { position: relative; display: inline-block; }
.menu-panel {
  position: absolute; right: 0; top: calc(100% + 6px); min-width: 250px; z-index: 30;
  background: var(--surface-1); border: 1px solid var(--border);
  border-radius: var(--radius-md); box-shadow: var(--shadow-pop); padding: 6px;
}
.menu-panel[hidden] { display: none; }
.menu-panel a, .menu-panel button {
  display: block; width: 100%; text-align: left; font: inherit; font-size: 14px;
  padding: 8px 10px; border-radius: var(--radius-sm); color: var(--text-primary);
  text-decoration: none; background: none; border: 0; cursor: pointer;
}
.menu-panel a:hover, .menu-panel button:hover { background: var(--surface-2); }
.menu-panel hr { border: 0; border-top: 1px solid var(--border); margin: 6px 0; }

/* --- 12. Metadata panel ------------------------------------------------- */
.meta-tabs { display: flex; flex-wrap: wrap; gap: 2px; border-bottom: 1px solid var(--border); }
.meta-tabs button {
  font: inherit; font-size: 14px; padding: 8px 14px; background: none; border: 0;
  border-bottom: 2px solid transparent; margin-bottom: -1px; cursor: pointer;
  color: var(--text-secondary); font-weight: 600;
}
.meta-tabs button[aria-selected="true"] { color: var(--brand-600); border-bottom-color: var(--brand-600); }
.meta-panel { padding-top: var(--space-4); }
.meta-panel[hidden] { display: none; }
.meta-item + .meta-item { margin-top: var(--space-4); padding-top: var(--space-4); border-top: 1px solid var(--border); }
.meta-item dt { font-size: 13px; font-weight: 600; color: var(--text-secondary); margin-bottom: 4px; }
.meta-item dd { margin: 0; white-space: pre-line; }

/* --- 13. Footer --------------------------------------------------------- */
footer {
  border-top: 1px solid var(--border); background: var(--surface-1);
  padding: var(--space-8) 0; font-size: 14px; color: var(--text-secondary);
}
footer .cols2 { display: flex; flex-wrap: wrap; gap: var(--space-8); justify-content: space-between; }
.notice {
  background: color-mix(in srgb, var(--warn) 12%, transparent);
  border: 1px solid color-mix(in srgb, var(--warn) 35%, transparent);
  color: var(--text-primary);
  border-radius: var(--radius-sm); padding: var(--space-3) var(--space-4);
  font-size: 13px; margin-bottom: var(--space-6);
}

/* --- 14. Hero ----------------------------------------------------------- */
.hero { padding: var(--space-12) 0 var(--space-8); }
.hero h1 { font-size: 40px; max-width: 18ch; }
@media (max-width: 767px) { .hero h1 { font-size: 30px; } }
.hero p.lead { font-size: 18px; color: var(--text-secondary); max-width: 60ch; }

/* --- 15. Print ---------------------------------------------------------- */
@media print {
  .hdr, .nav, .tabs, .filterbar, .menu, footer { display: none !important; }
  body { background: #fff; color: #000; }
  .card { border: 1px solid #ccc; break-inside: avoid; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}

/* --- 16. Brand lockups --------------------------------------------------- */
.footer-brand { display: flex; align-items: center; gap: var(--space-4); margin-bottom: var(--space-3); }
.footer-brand img.bnss { height: 40px; width: auto; }
.footer-brand img.sib { height: 18px; width: auto; }
:root[data-theme="dark"] .footer-brand img.sib { filter: invert(1) hue-rotate(180deg) saturate(1.3) brightness(1.15); }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .footer-brand img.sib { filter: invert(1) hue-rotate(180deg) saturate(1.3) brightness(1.15); }
}

/* A thin SIB-brand rule under the header ties the page to the institution. */
.hdr { border-bottom: 3px solid var(--brand-600); }
.hero .hero-mark { height: 58px; width: auto; margin-bottom: var(--space-4); }
@media (max-width: 767px) { .hero .hero-mark { height: 44px; } }

.adm-bar-logo { height: 20px; width: auto; filter: brightness(0) invert(1); opacity: .95; }

/* --- 17. Reviewer feedback widget (TEMPORARY) ---------------------------- */
/* In place for the SIB review period only. Delete this block together with
   templates/_feedback.html, app/feedback.py and the include in base.html. */
.fb { position: fixed; right: 20px; bottom: 20px; z-index: 60; }
@media (max-width: 767px) { .fb { right: 12px; bottom: 12px; } }

.fb-launch {
  display: inline-flex; align-items: center; gap: 8px;
  font: inherit; font-size: 14px; font-weight: 600; cursor: pointer;
  padding: 11px 18px; border-radius: 999px; border: 0;
  background: var(--brand-600); color: #fff;
  box-shadow: var(--shadow-pop);
}
:root[data-theme="dark"] .fb-launch { background: #9c1030; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .fb-launch { background: #9c1030; }
}
.fb-launch:hover { background: var(--brand-500); }
:root[data-theme="dark"] .fb-launch:hover { background: #b4183c; }
.fb-launch:focus-visible { outline: 3px solid var(--accent); outline-offset: 3px; }

.fb-panel {
  position: absolute; right: 0; bottom: calc(100% + 10px);
  width: min(380px, calc(100vw - 32px));
  background: var(--surface-1); color: var(--text-primary);
  border: 1px solid var(--border); border-radius: var(--radius-md);
  box-shadow: var(--shadow-pop); padding: var(--space-4);
  max-height: min(78vh, 640px); overflow-y: auto;
}
.fb-panel[hidden] { display: none; }
.fb-head { display: flex; align-items: flex-start; gap: var(--space-3); margin-bottom: var(--space-3); }
.fb-head strong { font-size: 15px; }
.fb-sub { font-size: 12px; color: var(--text-muted); margin-top: 2px; }
.fb-close {
  margin-left: auto; background: none; border: 0; cursor: pointer;
  font-size: 22px; line-height: 1; color: var(--text-muted); padding: 0 4px;
}
.fb-close:hover { color: var(--text-primary); }

.fb-panel label {
  font-size: 12px; text-transform: uppercase; letter-spacing: .04em;
  color: var(--text-muted); margin: var(--space-3) 0 4px; display: block;
}
.fb-panel label:first-of-type { margin-top: 0; }
.fb-panel textarea { resize: vertical; min-height: 96px; }
.fb-two { display: grid; grid-template-columns: 1fr 1fr; gap: var(--space-2); }
@media (max-width: 480px) { .fb-two { grid-template-columns: 1fr; } }

.fb-context {
  font-size: 12px; color: var(--text-muted); margin: var(--space-3) 0 0;
  background: var(--surface-2); border-radius: var(--radius-sm);
  padding: 8px 10px; word-break: break-word;
}
.fb-status { font-size: 13px; margin: var(--space-2) 0 0; min-height: 1.2em; }
.fb-status.ok { color: var(--ok); }
.fb-status.err { color: var(--danger); }
.fb-actions { display: flex; align-items: center; gap: var(--space-3); margin-top: var(--space-3); }
.fb-note { font-size: 11px; color: var(--text-muted); }

@media print { .fb { display: none !important; } }
