/* Colour roles follow the dataviz reference palette (light + dark, selected per mode). */
.viz-root {
  color-scheme: light;
  --page:           #f9f9f7;
  --surface-1:      #fcfcfb;
  --text-primary:   #0b0b0b;
  --text-secondary: #52514e;
  --text-muted:     #6b6a65;   /* darker than the palette's #898781 so small text clears 4.5:1 */
  --grid:           #e1e0d9;
  --axis:           #c3c2b7;
  --border:         rgba(11, 11, 11, 0.10);
  --nodata:         #e1e0d9;
  --accent:         #2a78d6;
  --focus:          #1c5cab;
}
@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) .viz-root {
    color-scheme: dark;
    --page: #0d0d0d; --surface-1: #1a1a19; --text-primary: #f0efec; --text-secondary: #c3c2b7;
    --text-muted: #a3a29b; --grid: #2c2c2a; --axis: #383835; --border: rgba(255, 255, 255, 0.10);
    --nodata: #383835; --accent: #3987e5; --focus: #86b6ef;
  }
}
:root[data-theme="dark"] .viz-root {
  color-scheme: dark;
  --page: #0d0d0d; --surface-1: #1a1a19; --text-primary: #f0efec; --text-secondary: #c3c2b7;
  --text-muted: #a3a29b; --grid: #2c2c2a; --axis: #383835; --border: rgba(255, 255, 255, 0.10);
  --nodata: #383835; --accent: #3987e5; --focus: #86b6ef;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
/* Reserve the scrollbar's space from the start: charts drawn before the page grows long
   enough to scroll would otherwise be 15 px wider than the ones drawn after. */
html { scrollbar-gutter: stable; }
body { font-family: system-ui, -apple-system, "Segoe UI", sans-serif; font-size: 16px; line-height: 1.5; }
.viz-root { background: var(--page); color: var(--text-primary); min-height: 100vh; padding: 0 clamp(12px, 3vw, 32px) 32px; }
/* Cap the content width on wide screens so charts don't stretch flat (request R12). */
.viz-root { padding-inline: max(clamp(12px, 3vw, 32px), calc((100% - 1440px) / 2)); }
h1 { font-size: 1.5rem; line-height: 1.25; margin: 0 0 4px; font-weight: 650; }
h2 { font-size: 1.05rem; margin: 0; font-weight: 600; }
.lede { margin: 0; color: var(--text-secondary); }
:focus-visible { outline: 3px solid var(--focus); outline-offset: 2px; }
.sr-only { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
.skip-link { position: absolute; left: -9999px; }
.skip-link:focus { left: 12px; top: 12px; background: var(--surface-1); color: var(--text-primary); padding: 8px 12px; z-index: 10; }

.site-header { display: flex; justify-content: space-between; align-items: flex-start; gap: 16px; padding: 24px 0 16px; }

.btn {
  font: inherit; font-size: 0.95rem; color: var(--text-primary); background: var(--surface-1);
  border: 1px solid var(--axis); border-radius: 8px; padding: 8px 14px; cursor: pointer; min-height: 40px;
}
.btn:hover { background: var(--grid); }

/* Tabs */
.tabs { display: flex; flex-wrap: wrap; gap: 4px; border-bottom: 1px solid var(--axis); }
.tab {
  font: inherit; font-size: 1rem; background: none; border: none; color: var(--text-secondary);
  padding: 10px 16px; cursor: pointer; border-bottom: 3px solid transparent; margin-bottom: -1px; min-height: 44px;
}
.tab:hover { color: var(--text-primary); }
.tab[aria-selected="true"] { color: var(--text-primary); font-weight: 600; border-bottom-color: var(--accent); }

/* Filter row: one row above everything it scopes */
.filters { display: flex; flex-wrap: wrap; align-items: flex-end; gap: 12px 20px; padding: 16px 0 8px; }
.filter { display: flex; flex-direction: column; gap: 4px; }
.filter[hidden] { display: none; }
.filter-label { font-size: 0.85rem; color: var(--text-secondary); }
select, input[type="search"] {
  font: inherit; font-size: 0.95rem; color: var(--text-primary); background: var(--surface-1);
  border: 1px solid var(--axis); border-radius: 8px; padding: 8px 10px; min-height: 40px; max-width: 100%;
}
select { min-width: 12rem; }
/* (i) button and its explanation box (R37). */
.filter-head { display: inline-flex; align-items: center; gap: 6px; }
#scale-filter { position: relative; }
.info-btn {
  font: italic 700 0.8rem/1 Georgia, serif; color: var(--text-secondary); background: var(--surface-1);
  border: 1px solid var(--text-secondary); border-radius: 50%; width: 22px; height: 22px; padding: 0; cursor: pointer;
}
.info-btn:hover, .info-btn[aria-expanded="true"] { background: var(--text-secondary); color: var(--surface-1); }
.info-pop {
  position: absolute; z-index: 20; top: calc(100% + 4px); left: 0; width: min(24rem, calc(100vw - 24px));
  background: var(--surface-1); color: var(--text-primary); border: 1px solid var(--axis); border-radius: 10px;
  padding: 4px 12px; box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12); font-size: 0.875rem; line-height: 1.45;
}
.info-pop p { margin: 8px 0; }

.picker { position: relative; }
.picker-toggle { min-width: 16rem; text-align: left; }
.picker-toggle::after { content: " ▾"; color: var(--text-muted); }
.picker-panel {
  position: absolute; z-index: 20; top: calc(100% + 4px); left: 0; width: min(22rem, 90vw);
  background: var(--surface-1); border: 1px solid var(--axis); border-radius: 10px; padding: 10px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.12);
}
.picker-panel input { width: 100%; }
.picker-clear { display: block; margin: 0 0 6px auto; } /* "Clear all", top right of the panel (R61) */
.picker-clear:disabled { color: var(--text-muted); text-decoration: none; cursor: default; }
.picker-note { font-size: 0.85rem; color: var(--text-secondary); margin: 6px 2px; min-height: 1.2em; }
.picker-list { max-height: 22rem; overflow-y: auto; }
.picker-list fieldset { border: none; margin: 0 0 8px; padding: 0; }
.picker-list legend { font-size: 0.8rem; font-weight: 600; color: var(--text-secondary); text-transform: uppercase; letter-spacing: 0.03em; padding: 4px 2px; }
.picker-list label { display: flex; align-items: center; gap: 8px; padding: 4px 4px; border-radius: 6px; cursor: pointer; }
.picker-list label:hover { background: var(--grid); }
/* Search hides non-matching entries with `hidden`, which `display: flex` above would override,
   so a group with one match showed all its entries (region search looked broken, 2026-11-10, R57). */
.picker-list label[hidden], .picker-list fieldset[hidden] { display: none; }
.picker-list input { width: 18px; height: 18px; margin: 0; }
.picker-list input:disabled + span { color: var(--text-muted); }

.chips { list-style: none; margin: 0; padding: 0; display: flex; flex-wrap: wrap; gap: 6px; flex-basis: 100%; }
.chip {
  display: inline-flex; align-items: center; gap: 6px; font-size: 0.9rem; background: var(--surface-1);
  border: 1px solid var(--border); border-radius: 999px; padding: 2px 4px 2px 10px;
}
.chips:empty, .chips[hidden] { display: none; }
/* Selected commodities missing from this tab's dataset (R55). */
.chips-note { flex-basis: 100%; margin: 0; font-size: 0.85em; color: var(--text-secondary); }
.chips-note[hidden] { display: none; }
.chip.chip-off { opacity: 0.5; } /* selected, but not shown on this tab (World on trade, R33) */
.chip .key { width: 16px; height: 3px; border-radius: 2px; display: inline-block; }
.chip button { font: inherit; background: none; border: none; color: var(--text-secondary); cursor: pointer; border-radius: 999px; width: 28px; height: 28px; }
.chip button:hover { background: var(--grid); color: var(--text-primary); }

/* Chart cards */
.status { color: var(--text-secondary); margin: 8px 0; min-height: 1.5em; }
.grid { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 16px; }
.card { background: var(--surface-1); border: 1px solid var(--border); border-radius: 12px; padding: 16px; margin: 0; min-width: 0; }
.card.wide { grid-column: 1 / -1; }
.card-sub { margin: 2px 0 8px; font-size: 0.9rem; color: var(--text-secondary); }
.card-controls { display: flex; flex-wrap: wrap; gap: 8px 16px; margin: 0 0 8px; font-size: 0.9rem; color: var(--text-secondary); }
.card-controls label { display: inline-flex; align-items: center; gap: 6px; cursor: pointer; }
.card-controls input { width: 18px; height: 18px; margin: 0; }
/* Map and ranking carousels when several commodities are selected (R39, R41). */
.carousel { display: flex; flex-wrap: wrap; align-items: center; gap: 6px 10px; margin: 0 0 8px; }
.carousel-arrow { font-size: 1.3rem; line-height: 1; padding: 4px 12px; min-width: 44px; }
.carousel-pos { font-weight: 600; min-width: 11rem; text-align: center; }
.carousel-dots { display: inline-flex; gap: 2px; }
.dot { width: 28px; height: 28px; padding: 0; border: none; border-radius: 50%; background: none; cursor: pointer;
  display: inline-flex; align-items: center; justify-content: center; }
.dot::before { content: ""; width: 10px; height: 10px; border-radius: 50%; background: var(--axis); }
.dot:hover::before { background: var(--text-muted); }
.dot[aria-current="true"]::before { background: var(--accent); }
.chart { width: 100%; height: 360px; }
.chart.map { height: 420px; }
/* Map legend below the map on phones (R27). */
.map-legend { list-style: none; display: flex; flex-wrap: wrap; gap: 4px 14px; margin: 6px 0 0; padding: 0; font-size: 0.85rem; color: var(--text-secondary); }
.map-legend li { display: inline-flex; align-items: center; gap: 6px; }
.map-legend .map-legend-title { flex-basis: 100%; }
.map-legend .swatch { width: 14px; height: 14px; border-radius: 3px; flex: none; }
.chart-msg { font-size: 0.9rem; color: var(--text-secondary); margin: 4px 0 0; }
.map-caption { font-size: 0.8rem; font-style: italic; color: var(--text-secondary); margin: 6px 0 0; } /* map disclaimer, R50 */
.chart-empty { display: flex; align-items: center; justify-content: center; text-align: center; color: var(--text-secondary); height: 200px; padding: 16px; border: 1px dashed var(--axis); border-radius: 8px; }
.card-tools { display: flex; flex-wrap: wrap; align-items: center; gap: 8px 16px; margin-top: 8px; }
.card-tools[hidden] { display: none; } /* empty cards (e.g. violins with countries only, R44) have no table or CSV */
.card-tools details { flex-basis: 100%; }
.card-tools summary { cursor: pointer; color: var(--text-secondary); font-size: 0.9rem; padding: 4px 0; }
.link-btn { font: inherit; font-size: 0.9rem; background: none; border: none; padding: 4px 0; color: var(--text-secondary); text-decoration: underline; cursor: pointer; }
.table-wrap { overflow-x: auto; max-height: 24rem; overflow-y: auto; margin-top: 6px; }
table { border-collapse: collapse; font-size: 0.875rem; width: 100%; }
th, td { padding: 4px 8px; border-bottom: 1px solid var(--grid); text-align: right; white-space: nowrap; }
th:first-child, td:first-child { text-align: left; }
th { position: sticky; top: 0; background: var(--surface-1); color: var(--text-secondary); font-weight: 600; }
td { font-variant-numeric: tabular-nums; }

.site-footer { margin-top: 32px; padding-top: 16px; border-top: 1px solid var(--axis); color: var(--text-secondary); font-size: 0.9rem; }
.site-footer ul { padding-left: 1.2em; }
.attribution { font-size: 0.8rem; color: var(--text-muted); }

/* Paired charts stay side by side down to tablet width (request R12). */
@media (max-width: 700px) {
  .grid { grid-template-columns: minmax(0, 1fr); }
  .site-header { flex-direction: column; }
  .chart.map { height: 250px; } /* legend now sits below the map (R27) */
}
@media (prefers-reduced-motion: reduce) { * { transition: none !important; } }
