/* Molecular Foundry — one design system for every view.
   Light-first analyst tool: neutral surfaces, one accent, numbers lead.
   View-specific rules live in css/<view>.css, scoped by .v-<view>. */

:root {
  color-scheme: light;
  --font: "Inter", ui-sans-serif, system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;

  --bg: #f6f7f9;
  --surface: #ffffff;
  --surface-2: #f1f3f6;
  --border: #e3e6eb;
  --border-strong: #cfd4dc;
  --text: #161a20;
  --text-2: #4a5260;
  --text-3: #68707f; /* ≥ 4.5:1 on --surface and --bg (WCAG AA for small text) */
  --accent: #2457d6;
  --accent-soft: #e8eefc;
  --pos: #137a4c;
  --neg: #c03a2b;
  --warn: #9a6400;
  --focus: #2457d6;

  --series-1: #2a78d6;
  --series-2: #eb6834;
  --series-3: #1baf7a;
  --series-4: #eda100;
  --series-5: #e87ba4;
  --series-6: #008300;
  --series-7: #6250d6;
  --series-other: #a9a79f;

  --origin-ocean: #2a78d6;
  --origin-brine: #6250d6;
  --origin-air: #7fb6e8;
  --origin-crust: #b07a3c;
  --origin-water: #1baf7a;
  --origin-intermediate: #a9a79f;

  --radius: 8px;
  --gap: 16px;
  --header-h: 52px;
  --shadow: 0 1px 2px rgb(16 24 40 / 0.06);
}

:root[data-theme="dark"] {
  color-scheme: dark;
  --bg: #0f1216;
  --surface: #161a20;
  --surface-2: #1d222a;
  --border: #272d36;
  --border-strong: #353c47;
  --text: #e8ebf0;
  --text-2: #aab2bf;
  --text-3: #7d8696;
  --accent: #7aa2ff;
  --accent-soft: #1c2742;
  --pos: #4cc38a;
  --neg: #f0786b;
  --warn: #e0a83a;
  --focus: #7aa2ff;
  --series-1: #3987e5;
  --series-2: #d95926;
  --series-3: #199e70;
  --series-4: #c98500;
  --series-5: #d55181;
  --series-6: #3aa53a;
  --series-7: #9085e9;
  --series-other: #5b6b7e;
  --shadow: none;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    color-scheme: dark;
    --bg: #0f1216;
    --surface: #161a20;
    --surface-2: #1d222a;
    --border: #272d36;
    --border-strong: #353c47;
    --text: #e8ebf0;
    --text-2: #aab2bf;
    --text-3: #7d8696;
    --accent: #7aa2ff;
    --accent-soft: #1c2742;
    --pos: #4cc38a;
    --neg: #f0786b;
    --warn: #e0a83a;
    --focus: #7aa2ff;
    --series-1: #3987e5;
    --series-2: #d95926;
    --series-3: #199e70;
    --series-4: #c98500;
    --series-5: #d55181;
    --series-6: #3aa53a;
    --series-7: #9085e9;
    --series-other: #5b6b7e;
    --shadow: none;
  }
}

/* ---------------------------------------------------------------- base */
*, *::before, *::after { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 var(--font);
  font-feature-settings: "tnum" 1, "cv11" 1;
  -webkit-font-smoothing: antialiased;
}
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
h1, h2, h3, h4 { margin: 0; font-weight: 650; letter-spacing: -0.01em; }
h1 { font-size: 20px; }
h2 { font-size: 15px; }
h3 { font-size: 13px; }
p { margin: 0; }
code { font-family: var(--mono); font-size: 0.92em; }
:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; }
[hidden] { display: none !important; }

/* ---------------------------------------------------------------- shell */
.app-header {
  position: sticky; top: 0; z-index: 20;
  height: var(--header-h);
  display: flex; align-items: center; gap: 24px;
  padding: 0 20px;
  background: var(--surface);
  border-bottom: 1px solid var(--border);
}
.brand { font-weight: 700; font-size: 15px; color: var(--text); white-space: nowrap; }
.brand:hover { text-decoration: none; }
.app-nav { display: flex; gap: 2px; height: 100%; }
.app-nav a {
  display: flex; align-items: center; padding: 0 12px; white-space: nowrap;
  color: var(--text-2); font-weight: 550;
  border-bottom: 2px solid transparent;
}
.app-nav a:hover { color: var(--text); text-decoration: none; }
.app-nav a[aria-current="page"] { color: var(--text); border-bottom-color: var(--accent); }
.header-spacer { flex: 1; }
.sim-status { font-size: 12px; color: var(--text-3); white-space: nowrap; }
.sim-status.is-busy { color: var(--warn); }
.icon-button {
  border: 1px solid var(--border); background: var(--surface); color: var(--text-2);
  border-radius: 6px; height: 30px; padding: 0 10px; font: inherit; font-size: 12px; cursor: pointer;
}
.icon-button:hover { color: var(--text); border-color: var(--border-strong); }

.app-main { max-width: 1440px; margin: 0 auto; padding: 20px 20px 48px; }
.view-title { display: flex; align-items: baseline; gap: 12px; flex-wrap: wrap; margin-bottom: 16px; }
.view-title p { color: var(--text-2); }
.crumbs { font-size: 12px; color: var(--text-3); margin-bottom: 6px; }

/* ---------------------------------------------------------------- layout primitives */
.grid { display: grid; gap: var(--gap); }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.grid-sidebar { grid-template-columns: 280px minmax(0, 1fr); align-items: start; }
.stack { display: flex; flex-direction: column; gap: var(--gap); }
.row { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
.card {
  background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius);
  box-shadow: var(--shadow); padding: 16px; min-width: 0;
}
.card-head { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; margin-bottom: 10px; flex-wrap: wrap; }
.card-head p, .muted { color: var(--text-3); font-size: 12px; }
.sticky { position: sticky; top: calc(var(--header-h) + 16px); }

@media (max-width: 1000px) {
  .grid-sidebar { grid-template-columns: minmax(0, 1fr); }
  .sticky { position: static; }
}
@media (max-width: 680px) {
  .app-main { padding: 14px 16px 40px; }
  .grid-2 { grid-template-columns: minmax(0, 1fr); }
}

/* ---------------------------------------------------------------- KPIs */
/* Dividers are drawn per tile (right + bottom edge, clipped by the frame), so a short last row leaves no grey cells. */
.kpis { display: grid; grid-template-columns: repeat(auto-fit, minmax(150px, 1fr)); background: var(--surface); border: 1px solid var(--border); border-radius: var(--radius); overflow: hidden; }
.kpi { background: var(--surface); padding: 12px 14px; min-width: 0; box-shadow: 1px 0 0 var(--border), 0 1px 0 var(--border); }
.kpi-label { font-size: 11px; color: var(--text-3); text-transform: uppercase; letter-spacing: 0.04em; font-weight: 600; }
.kpi-value { font-size: 22px; font-weight: 650; letter-spacing: -0.02em; margin-top: 2px; white-space: nowrap; }
.kpi-sub { font-size: 12px; color: var(--text-3); margin-top: 1px; }

/* ---------------------------------------------------------------- tables */
.table-scroll { overflow-x: auto; }
.data-table { width: 100%; border-collapse: collapse; font-size: 13px; }
.data-table th, .data-table td { padding: 7px 10px; border-bottom: 1px solid var(--border); text-align: left; vertical-align: top; }
.data-table thead th { font-size: 11px; color: var(--text-3); text-transform: uppercase; letter-spacing: 0.04em; font-weight: 600; white-space: nowrap; background: var(--surface); position: sticky; top: 0; }
.data-table th[data-sort] { cursor: pointer; user-select: none; }
.data-table th[data-sort]:hover { color: var(--text); }
.data-table th[aria-sort="ascending"]::after { content: " ▲"; }
.data-table th[aria-sort="descending"]::after { content: " ▼"; }
.data-table tbody tr:hover { background: var(--surface-2); }
.data-table tbody tr[data-href] { cursor: pointer; }
.data-table .num { text-align: right; white-space: nowrap; }
.data-table.compact th, .data-table.compact td { padding: 4px 8px; }
.cell-sub { display: block; font-size: 12px; color: var(--text-3); }
.is-pos { color: var(--pos); }
.is-neg { color: var(--neg); }
.is-dim { opacity: 0.5; }

/* ---------------------------------------------------------------- tags, origins */
.tag { display: inline-flex; align-items: center; gap: 4px; font-size: 11px; font-weight: 600; padding: 1px 7px; border-radius: 999px; background: var(--surface-2); color: var(--text-2); border: 1px solid var(--border); white-space: nowrap; }
.tag.is-cited { color: var(--pos); }
.tag.is-screening { color: var(--warn); }
.tag.is-accent { background: var(--accent-soft); color: var(--accent); border-color: transparent; }
.origin-dot { display: inline-block; width: 8px; height: 8px; border-radius: 50%; background: var(--origin-intermediate); }
.origin-dot[data-origin="ocean"] { background: var(--origin-ocean); }
.origin-dot[data-origin="brine"] { background: var(--origin-brine); }
.origin-dot[data-origin="air"] { background: var(--origin-air); }
.origin-dot[data-origin="crust"] { background: var(--origin-crust); }
.origin-dot[data-origin="water"] { background: var(--origin-water); }

/* ---------------------------------------------------------------- forms */
.field { display: flex; flex-direction: column; gap: 3px; min-width: 0; }
.field-label { display: flex; justify-content: space-between; gap: 6px; font-size: 12px; color: var(--text-2); font-weight: 550; }
.field-label small { color: var(--text-3); font-weight: 500; }
.field input[type="number"], .field select, select.control, input.control {
  height: 30px; padding: 0 8px; border: 1px solid var(--border-strong); border-radius: 6px;
  background: var(--surface); color: var(--text); font: inherit; font-size: 13px; width: 100%;
}
.field input[type="range"] { width: 100%; accent-color: var(--accent); }
.field-hint { font-size: 11px; color: var(--text-3); }
.field-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 12px; }
.check { display: flex; align-items: flex-start; gap: 8px; font-size: 13px; cursor: pointer; }
.check input { accent-color: var(--accent); margin-top: 2px; }
.check small { display: block; color: var(--text-3); font-size: 11px; }
.form-section { border-top: 1px solid var(--border); padding-top: 12px; margin-top: 12px; }
.form-section > h3 { font-size: 11px; color: var(--text-3); text-transform: uppercase; letter-spacing: 0.05em; margin-bottom: 10px; }
.button {
  min-height: 30px; padding: 5px 12px; line-height: 1.3; border-radius: 6px; font: inherit; font-size: 13px; font-weight: 550; cursor: pointer;
  border: 1px solid var(--border-strong); background: var(--surface); color: var(--text);
}
.button.primary { background: var(--accent); border-color: var(--accent); color: #fff; }
.segmented { display: inline-flex; border: 1px solid var(--border-strong); border-radius: 6px; overflow: hidden; }
.segmented button { border: 0; background: var(--surface); color: var(--text-2); font: inherit; font-size: 12px; padding: 4px 10px; cursor: pointer; }
.segmented button[aria-pressed="true"] { background: var(--accent-soft); color: var(--accent); font-weight: 600; }

/* ---------------------------------------------------------------- charts (js/ui/charts.js) */
.chart-legend { display: flex; flex-wrap: wrap; gap: 4px 12px; font-size: 12px; color: var(--text-2); margin-bottom: 6px; }
.legend-item { display: inline-flex; align-items: center; gap: 5px; }
.legend-swatch { width: 10px; height: 10px; border-radius: 2px; background: var(--swatch); }
.legend-swatch.is-line { height: 2px; border-radius: 1px; }
.chart-body { width: 100%; min-height: 120px; }
.chart-svg { display: block; width: 100%; height: auto; overflow: visible; touch-action: pan-y; }
.chart-svg:focus { outline: none; }
.chart-svg:focus-visible { outline: 2px solid var(--focus); outline-offset: 4px; border-radius: 4px; }
.chart-grid-lines line { stroke: var(--border); stroke-width: 1; }
.chart-grid-lines .chart-baseline { stroke: var(--border-strong); }
.chart-tick { fill: var(--text-3); font-size: 11px; }
.chart-line { fill: none; stroke-width: 2; stroke-linejoin: round; stroke-linecap: round; }
.chart-line.is-dashed { stroke-dasharray: 4 3; }
.chart-band { stroke: var(--surface); stroke-width: 0.5; }
.chart-wash { opacity: 0.12; }
.chart-ref { stroke: var(--text-3); stroke-dasharray: 3 3; }
.chart-ref-label { fill: var(--text-3); font-size: 11px; }
.chart-end-label { fill: var(--text-2); font-size: 11px; }
.chart-cross { stroke: var(--text-3); stroke-width: 1; }
.chart-dot { stroke: var(--surface); stroke-width: 2; }
.chart-empty { color: var(--text-3); font-size: 13px; padding: 24px 0; text-align: center; }
.chart-tooltip {
  position: fixed; left: 0; top: 0; z-index: 50; pointer-events: none;
  background: var(--surface); border: 1px solid var(--border-strong); border-radius: 6px;
  box-shadow: 0 4px 16px rgb(0 0 0 / 0.12); padding: 8px 10px; font-size: 12px; min-width: 160px;
}
.tip-head { font-weight: 650; margin-bottom: 4px; }
.tip-row { display: grid; grid-template-columns: 10px auto 1fr; gap: 6px; align-items: center; }
.tip-row strong { font-weight: 650; text-align: right; }
.tip-row span:last-child { color: var(--text-2); }
.tip-key { width: 8px; height: 8px; border-radius: 2px; }
.tip-key.is-empty { background: none; }
.tip-total { border-top: 1px solid var(--border); margin-top: 4px; padding-top: 4px; }
details.chart-data { margin-top: 6px; font-size: 12px; }
details.chart-data summary { color: var(--text-3); cursor: pointer; }

/* ---------------------------------------------------------------- misc */
.empty { color: var(--text-3); padding: 32px; text-align: center; }
.list-plain { margin: 0; padding-left: 18px; color: var(--text-2); font-size: 13px; }
.list-plain li + li { margin-top: 4px; }
.meter-cell { display: inline-flex; flex-direction: column; align-items: flex-end; gap: 3px; }
.meter { display: block; width: 64px; height: 4px; border-radius: 2px; background: var(--surface-2); overflow: hidden; }
.meter > span { display: block; height: 100%; background: var(--accent); border-radius: 2px; }
.facts { display: grid; grid-template-columns: max-content minmax(0, 1fr); gap: 6px 14px; margin: 0; font-size: 13px; }
.facts dt { color: var(--text-3); }
.facts dd { margin: 0; min-width: 0; overflow-wrap: anywhere; }
@media (max-width: 680px) {
  .facts { grid-template-columns: 1fr; gap: 2px; }
  .facts dd { margin-bottom: 6px; }
}

/* A field whose explanation is in its tooltip: dotted underline + help cursor so the hover is discoverable. */
.field.has-hint .field-label > span:first-child { text-decoration: underline dotted var(--text-3); text-underline-offset: 3px; cursor: help; }

/* Column headers that explain themselves on hover; clickable rows highlight. */
.data-table thead th[title] { text-decoration: underline dotted var(--text-3); text-underline-offset: 3px; }
.data-table tbody tr[data-href]:hover td { background: var(--surface-2); }
.table-more { font-size: 13px; }

/* Results from the company simulation fade while a newer run is in flight (js/app.js). */
.sim-result { transition: opacity 0.15s; }
.is-simulating .sim-result { opacity: 0.4; }

/* Comparison bar (js/app.js): under the header while a scenario is pinned. */
.compare-bar {
  position: sticky; top: var(--header-h); z-index: 19;
  display: flex; flex-wrap: wrap; align-items: baseline; gap: 4px 12px;
  padding: 8px 20px; font-size: 13px;
  background: var(--accent-soft); color: var(--text); border-bottom: 1px solid var(--border);
}
.compare-bar[hidden] { display: none; }
.compare-bar .compare-diff { color: var(--text-2); }
#pinBtn[aria-pressed="true"] { color: var(--accent); border-color: var(--accent); }
@media (max-width: 680px) {
  .compare-bar { position: static; padding: 8px 16px; }
}
/* Text-style button (Show all rows, Swap, Unpin, Download CSV…), usable in any view. */
.link-button { border: 0; padding: 0; background: none; color: var(--accent); font: inherit; font-size: 13px; cursor: pointer; }
.link-button:hover { text-decoration: underline; }
.link-button:disabled { color: var(--text-3); cursor: default; text-decoration: none; }
.table-foot { display: flex; flex-wrap: wrap; justify-content: space-between; gap: 8px 16px; margin-top: 8px; }
.table-foot .table-csv { margin-left: auto; font-size: 12px; }
#pinBtn .label-short { display: none; }
@media (max-width: 680px) {
  #pinBtn .label-long { display: none; }
  #pinBtn .label-short { display: inline; }
}
.app-nav a.nav-external { color: var(--text-3); }
.app-nav a.nav-external:hover { color: var(--text); }
/* Sort buttons fill their header and look like it (js/ui/kit.js sortableTable). */
.data-table th .th-sort { all: unset; cursor: pointer; display: inline; }
.data-table th .th-sort:focus-visible { outline: 2px solid var(--focus); outline-offset: 2px; border-radius: 2px; }

/* Header on narrower screens: tighter spacing below 1180 px; and two rows (brand, status and
   buttons on top, every tab below, wrapping if needed) whenever one row does not fit. Whether it
   fits depends on the fonts the visitor has, so js/app.js measures and sets html.header-wrap,
   rather than guessing a breakpoint. */
@media (max-width: 1180px) {
  .app-header { gap: 12px; padding: 0 14px; }
  .app-nav a { padding: 0 8px; }
  .sim-status { max-width: 130px; white-space: normal; line-height: 1.2; font-size: 11px; }
}
html.header-wrap .app-header { height: auto; flex-wrap: wrap; gap: 4px 10px; padding: 8px 12px 0; }
html.header-wrap .app-nav { order: 10; width: 100%; height: auto; flex-wrap: wrap; gap: 0; margin: 0 -8px; }
html.header-wrap .app-nav a { padding: 8px; font-size: 14px; }
html.header-wrap .sim-status { order: 11; width: 100%; max-width: none; font-size: 11px; white-space: normal; }
html.header-wrap .sim-status:empty { display: none; }
html.header-wrap .header-spacer { flex: 1 0 0; }
html.header-wrap .icon-button { padding: 0 8px; }
html.header-wrap .compare-bar { position: static; }
/* Search box in a card's header (Abundance and Markets tables). */
.table-search { width: min(280px, 100%); }
/* Skip link: off-screen until focused by keyboard, then shown over the header. */
.skip-link { position: absolute; left: 8px; top: -60px; z-index: 100; padding: 8px 12px; border-radius: 6px;
  background: var(--accent); color: #fff; font-weight: 600; text-decoration: none; }
.skip-link:focus { top: 8px; }
#view:focus { outline: none; }
