/* Critical Minerals viewer — light/dark, editorial, system fonts only.
 * USGS-inspired color theme: navy banner with green accent.
 * Follows DESIGN.md: serif display, tabular numerals, content-first chrome.
 */

:root {
  --font-sans: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui,
               "Helvetica Neue", Arial, sans-serif;
  --font-serif: "Charter", "Source Serif 4", "Source Serif Pro",
                "Iowan Old Style", "Apple Garamond", "Palatino", "Georgia",
                "Times New Roman", serif;
  --font-mono: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;

  /* USGS official identity (light mode):
   *   Primary navy:  #014478 (USGS Blue)
   *   Secondary:     #097945 (USGS Green)
   *   Page bg:       warm off-white for long-form readability
   */
  --usgs-navy: #014478;
  --usgs-navy-deep: #002a4d;
  --usgs-green: #097945;
  --usgs-gold: #b89000;

  --bg: #fbfaf7;
  --surface: #ffffff;
  --surface-2: #f3f1eb;
  --border: #e3dfd5;
  --text: #1c1a16;
  --text-muted: #6b6557;
  --accent: var(--usgs-navy);
  --accent-soft: #e3edf6;
  --warn: #8f5a05;
  --hover: rgba(1, 68, 120, 0.07);

  --banner-bg: var(--usgs-navy);
  --banner-bg-2: var(--usgs-navy-deep);
  --banner-text: #ffffff;
  --banner-text-muted: #c9d9e8;
  --banner-accent: var(--usgs-green);
  --banner-border: rgba(255, 255, 255, 0.15);

  --download-bar-bg: var(--usgs-green);
  --download-bar-text: #ffffff;
  --download-bar-hover: rgba(255, 255, 255, 0.18);

  font-feature-settings: "tnum" 1, "lnum" 1;
}

[data-theme="dark"] {
  --bg: #0f1419;
  --surface: #181d24;
  --surface-2: #232932;
  --border: #2e3540;
  --text: #f0eee6;
  --text-muted: #a2a8b3;
  --accent: #6fa8de;            /* lighter USGS-blue derivative for dark mode contrast */
  --accent-soft: #1a2e44;
  --warn: #d9a437;
  --hover: rgba(111, 168, 222, 0.10);

  --banner-bg: #02233f;
  --banner-bg-2: #011a30;
  --banner-text: #f0eee6;
  --banner-text-muted: #aac0d6;
  --banner-accent: #4faa7b;
  --banner-border: rgba(255, 255, 255, 0.10);

  --download-bar-bg: #0b5a36;
  --download-bar-text: #f0eee6;
  --download-bar-hover: rgba(255, 255, 255, 0.10);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }

/* Screen-reader-only utility: visually hide but keep in the AT tree so
 * decorative-glyph icons can carry real labels for assistive tech.
 * Per WebAIM's "visually-hidden" pattern.
 */
.visually-hidden {
  position: absolute !important;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: var(--font-sans);
  font-size: 15px;
  line-height: 1.5;
}

a { color: var(--accent); }
a:focus-visible, button:focus-visible, [tabindex]:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
  border-radius: 4px;
}

.skip-link {
  position: absolute;
  left: -9999px;
}
.skip-link:focus {
  left: 16px;
  top: 16px;
  background: var(--surface);
  padding: 8px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  z-index: 100;
}

/* ---------- Download bar (top of page) ---------- */
.download-bar {
  width: 100%;
  background: var(--download-bar-bg);
  color: var(--download-bar-text);
  border-bottom: 1px solid var(--banner-border);
}
.download-bar-inner {
  max-width: 1280px;
  margin: 0 auto;
  padding: 10px 32px;
  display: flex;
  align-items: center;
  justify-content: flex-end;
  flex-wrap: wrap;       /* view-switch + downloads wrap onto two rows when tight */
  gap: 10px 14px;
}
.download-bar-label {
  font-size: 11.5px;
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--download-bar-text);
  opacity: 0.92;
}

/* ---------- USGS banner (page header) ---------- */
.page-header.usgs-banner {
  display: flex;
  align-items: center;
  gap: 20px;
  padding: 20px 32px;
  background:
    linear-gradient(180deg, var(--banner-bg) 0%, var(--banner-bg-2) 100%);
  color: var(--banner-text);
  border-bottom: 4px solid var(--banner-accent);
  position: relative;
}
.usgs-banner-mark {
  flex: 0 0 auto;
  width: 80px;
  height: 28px;
  display: flex;
  align-items: center;
  color: var(--banner-accent);
}
.usgs-banner-mark svg {
  width: 100%;
  height: 100%;
  display: block;
}
.usgs-banner .brand {
  flex: 1 1 auto;
  min-width: 0;
}
.usgs-banner .brand h1 {
  font-family: var(--font-serif);
  font-weight: 600;
  font-size: 26px;
  letter-spacing: -0.01em;
  margin: 0 0 4px;
  color: var(--banner-text);
}
.usgs-banner .subtitle {
  margin: 0;
  color: var(--banner-text-muted);
  font-size: 13px;
}
.usgs-banner .header-meta {
  display: flex;
  align-items: center;
  gap: 10px;
}

.pill {
  display: inline-block;
  padding: 1px 8px;
  border-radius: 999px;
  font-size: 10.5px;
  font-weight: 600;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  background: var(--accent-soft);
  color: var(--accent);
}
.pill.outline {
  background: transparent;
  border: 1px solid var(--border);
  color: var(--text-muted);
}

.icon-btn {
  width: 36px;
  height: 36px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  border: 1px solid var(--banner-border);
  background: rgba(255, 255, 255, 0.08);
  color: var(--banner-text);
  font-size: 16px;
  cursor: pointer;
  transition: background 120ms ease;
}
.icon-btn:hover { background: rgba(255, 255, 255, 0.18); }

/* ---------- Export button group ---------- */
.export-group {
  display: inline-flex;
  border: 1px solid var(--banner-border);
  border-radius: 8px;
  overflow: hidden;
  background: rgba(255, 255, 255, 0.08);
}
.export-btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 36px;
  padding: 0 14px;
  font-family: var(--font-sans);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--download-bar-text);
  background: transparent;
  border: 0;
  border-right: 1px solid var(--banner-border);
  cursor: pointer;
  text-decoration: none;
  transition: background 120ms ease;
}
.export-btn:last-child { border-right: 0; }
.export-btn:hover { background: var(--download-bar-hover); }
.export-btn:active { background: rgba(255, 255, 255, 0.30); }
.export-btn--primary {
  /* Darker overlay (not lighter) so the cream text-on-green hits ≥4.5:1
   * in dark mode. A 25% black overlay deepens the green; the secondary
   * buttons keep the lighter look so the visual hierarchy still reads
   * "primary first, then alts." */
  background: rgba(0, 0, 0, 0.25);
}

/* ---------- Main ---------- */
main {
  max-width: 1280px;
  margin: 0 auto;
  padding: 28px 32px 48px;
}

.eyebrow {
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--text-muted);
  margin: 0 0 8px;
}

.units-note {
  font-family: var(--font-serif);
  font-style: italic;
  color: var(--text-muted);
  margin: 0 0 16px;
}

.footnote-note {
  /* Above the table now; bottom margin instead of top. Bumped font-size +
   * weight so the 5:1 contrast on muted text still feels readable at glance. */
  margin: 4px 0 16px;
  color: var(--text-muted);
  font-size: 13px;
  max-width: 80ch;
}

/* ---------- Table ---------- */
.table-wrap {
  /* Sticky `thead` only sticks within its nearest scroll container, so we
   * give `.table-wrap` its own vertical scroll. The 75vh cap keeps the table
   * tall enough to show ~15-20 rows at a glance while leaving room for the
   * detail panel below; on mobile we drop the cap so the page scrolls
   * naturally instead. */
  overflow: auto;
  max-height: 75vh;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--surface);
}
.data-table {
  width: 100%;
  /* `separate` (not `collapse`) is required for `position: sticky` to engage
   * on <th> cells — collapsed borders fold the thead into the body's box
   * and sticky positioning silently no-ops in Chrome/Safari. */
  border-collapse: separate;
  border-spacing: 0;
  font-variant-numeric: tabular-nums;
  font-size: 14px;
}
.data-table th, .data-table td {
  padding: 10px 12px;
  text-align: left;
  border-bottom: 1px solid var(--border);
  vertical-align: top;
}
.data-table th {
  font-size: 11px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-muted);
  background: var(--surface-2);
  font-weight: 600;
  white-space: nowrap;
  /* Sticky header — column names stay visible while scrolling the 50+ row
   * overview. The sticky position is relative to .table-wrap's scroll
   * context for horizontal overflow, and relative to the page for vertical.
   */
  position: sticky;
  top: 0;
  z-index: 2;
  /* Render a clean bottom edge that doesn't bleed under the next row's
   * border-bottom when the header is stuck. */
  box-shadow: inset 0 -1px 0 var(--border);
}
.data-table th.num, .data-table td.num {
  text-align: right;
}
.data-table tbody tr {
  transition: background 90ms ease;
  cursor: pointer;
}
.data-table tbody tr:hover {
  background: var(--hover);
}
.data-table tbody tr[aria-selected="true"] {
  background: var(--accent-soft);
}
.data-table tbody tr:last-child td {
  border-bottom: 0;
}

.element-cell {
  font-family: var(--font-serif);
  font-weight: 600;
  font-size: 16px;
  white-space: nowrap;
}
.element-symbol {
  display: inline-block;
  font-family: var(--font-mono);
  font-size: 11px;
  color: var(--text-muted);
  margin-left: 6px;
  font-weight: 400;
}

.muted-cell {
  font-style: italic;
  color: var(--text-muted);
}

.sentinel {
  font-family: var(--font-mono);
  font-size: 12px;
  padding: 1px 6px;
  border-radius: 4px;
  background: var(--surface-2);
  color: var(--warn);
  font-weight: 600;
  letter-spacing: 0.04em;
}

.cat-heading {
  font-size: 11.5px;
  font-weight: 700;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text);
  margin: 14px 0 4px;
}
.cat-heading:first-of-type {
  margin-top: 0;
}

.row-label {
  max-width: 360px;
}

.source-link {
  font-size: 12.5px;
  color: var(--accent);
  white-space: nowrap;
}

/* ---------- Detail block ---------- */
.detail-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-left: 4px solid var(--usgs-green);
  border-radius: 12px;
  padding: 20px 24px;
  margin-top: 16px;
}
[data-theme="dark"] .detail-card {
  border-left-color: #4faa7b;
}
.detail-card h3 {
  font-family: var(--font-serif);
  margin: 0 0 4px;
  font-size: 22px;
}
.detail-card .source-row {
  font-size: 12.5px;
  color: var(--text-muted);
  margin: 0 0 16px;
}
.detail-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
  gap: 24px;
}

/* ---------- Tab strip inside the detail card ----------
 * The detail card was a single 5-viewport-tall scroll (stats → imports →
 * world prod → 5 prose blocks → footnotes → 2 huge PNG screenshots). It's
 * now a tab strip: header / units / price-basis stay always-visible at the
 * top, then a single tab's content renders below. Keyboard ARIA APG:
 * Arrow keys move focus between tabs and switch the active panel.
 */
.tab-strip {
  display: flex;
  flex-wrap: wrap;
  gap: 2px;
  margin: 12px 0 16px;
  border-bottom: 1px solid var(--border);
  /* Keep tabs in view as the panel grows tall. */
  position: sticky;
  top: 0;
  background: var(--surface);
  z-index: 1;
}
.tab-btn {
  appearance: none;
  border: 0;
  background: transparent;
  color: var(--text-muted);
  font-family: var(--font-sans);
  font-size: 12.5px;
  font-weight: 600;
  letter-spacing: 0.04em;
  padding: 8px 14px;
  border-bottom: 2px solid transparent;
  cursor: pointer;
  /* iOS/Android: ≥44px touch target. */
  min-height: 44px;
  transition: color 120ms ease, border-color 120ms ease, background 120ms ease;
}
.tab-btn:hover {
  color: var(--text);
  background: var(--surface-2);
}
.tab-btn[aria-selected="true"] {
  color: var(--accent);
  border-bottom-color: var(--accent);
}
.tab-panel {
  /* Each panel can host the existing .detail-grid; that grid wasn't being
   * used anymore since panels render one section apiece, so any direct
   * children that need columns can opt into `.detail-grid`. */
  min-height: 100px;
}

.kv {
  display: grid;
  grid-template-columns: 200px 1fr;
  row-gap: 6px;
  column-gap: 12px;
  margin: 0;
}
.kv dt {
  font-size: 11px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-muted);
  font-weight: 600;
}
.kv dd {
  margin: 0;
  font-variant-numeric: tabular-nums;
}

.mini-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 13px;
  font-variant-numeric: tabular-nums;
}
.mini-table th, .mini-table td {
  padding: 6px 8px;
  border-bottom: 1px solid var(--border);
  text-align: left;
}
.mini-table th {
  font-size: 10.5px;
  letter-spacing: 0.06em;
  text-transform: uppercase;
  color: var(--text-muted);
  font-weight: 600;
}
.mini-table th.num, .mini-table td.num {
  text-align: right;
}
.mini-table tr:last-child td {
  border-bottom: 0;
  font-weight: 600;
  background: var(--surface-2);
}

.prose {
  font-family: var(--font-serif);
  font-size: 14.5px;
  line-height: 1.55;
  color: var(--text);
  margin: 0 0 8px;
  max-width: 64ch;
}

.screenshots {
  margin-top: 12px;
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
}
.screenshots figure {
  margin: 0;
  flex: 1 1 280px;
  max-width: 420px;
}
.screenshots figcaption {
  font-size: 11.5px;
  color: var(--text-muted);
  margin-bottom: 6px;
}
.screenshots img {
  width: 100%;
  height: auto;
  /* Hard cap to keep the Source-PDF tab compact even when a sheet has
   * 4+ pages. Click to open the original PNG full-size in a new tab. */
  max-height: 640px;
  object-fit: contain;
  object-position: top;
  border: 1px solid var(--border);
  border-radius: 8px;
  display: block;
  cursor: zoom-in;
}

.footnotes {
  margin: 0;
  padding-left: 0;
  list-style: none;
  font-size: 12.5px;
}
.footnotes li {
  margin-bottom: 4px;
}
.footnotes .marker {
  font-family: var(--font-mono);
  font-weight: 700;
  color: var(--accent);
  margin-right: 6px;
}

/* ---------- Footer ---------- */
.page-footer {
  padding: 16px 32px 32px;
  font-size: 12.5px;
  color: var(--text-muted);
  border-top: 1px solid var(--border);
  background: var(--surface);
}

/* ---------- Responsive ---------- */
@media (max-width: 720px) {
  .download-bar-inner { padding: 10px 16px; }
  .page-header.usgs-banner {
    padding: 16px;
    flex-wrap: wrap;
  }
  .usgs-banner .brand { flex-basis: 100%; order: 2; }
  .usgs-banner-mark { order: 1; }
  .usgs-banner .header-meta { order: 1; margin-left: auto; }
  main { padding: 20px 16px 32px; }
  .usgs-banner .brand h1 { font-size: 20px; }
  .kv { grid-template-columns: 110px 1fr; }
  .download-bar-label { display: none; }
  /* On phones we don't want a nested scroll container — the page already
   * scrolls and a 75vh-capped table fights touch ergonomics. Drop the cap
   * and let the table grow; the sticky header still sticks within the
   * page's viewport. */
  .table-wrap { max-height: none; overflow: visible; overflow-x: auto; }
}

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

/* ---------- View switcher (By element / By country) — lives in the download bar ---------- */
.view-switch {
  display: inline-flex;
  gap: 4px;
  margin-right: auto;   /* sit on the left; downloads stay right-aligned */
}
.view-tab {
  appearance: none;
  border: 1px solid var(--banner-border);
  background: rgba(255, 255, 255, 0.08);
  color: var(--download-bar-text);
  font-family: var(--font-sans);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.04em;
  padding: 0 14px;
  min-height: 36px;     /* matches the export buttons + theme toggle in this bar */
  border-radius: 999px;
  cursor: pointer;
  transition: background 120ms ease, color 120ms ease;
}
.view-tab:hover { background: var(--download-bar-hover); }
.view-tab[aria-selected="true"] {
  background: #ffffff;
  color: #0b5a36;       /* dark green — ≥7:1 on the white pill, in both themes */
  border-color: #ffffff;
}

/* ---------- By-country view ---------- */
.country-picker-row {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  margin: 12px 0 20px;
}
.country-picker-row label {
  font-size: 11px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  font-weight: 700;
  color: var(--text-muted);
}
#country-select {
  appearance: none;
  -webkit-appearance: none;
  font: inherit;
  font-size: 15px;
  font-weight: 600;
  color: var(--text);
  background-color: var(--surface);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 10px 38px 10px 12px;
  min-height: 44px;
  min-width: min(280px, 100%);
  cursor: pointer;
  /* caret (theme-neutral gray) */
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'%3E%3Cpath fill='%23888' d='M1 1l5 5 5-5'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 13px center;
}
#country-body h3 {
  margin: 6px 0 2px;
  font-size: 22px;
}
#country-body h4.eyebrow {
  margin-top: 22px;
}
.count-badge {
  display: inline-block;
  font-size: 11px;
  font-weight: 700;
  color: var(--text-muted);
  background: var(--surface-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 1px 8px;
  margin-left: 4px;
  vertical-align: middle;
  letter-spacing: 0;
}

/* ---------- Print (Export PDF) ---------- */
@media print {
  :root { --bg: #fff; --surface: #fff; --surface-2: #f5f5f5; --border: #bbb;
          --text: #000; --text-muted: #444; --accent: #014478; --hover: transparent;
          --banner-bg: #014478; --banner-bg-2: #014478; --banner-text: #fff;
          --banner-text-muted: #c9d9e8; --banner-accent: #097945;
          --banner-border: rgba(255,255,255,0.4);
          --download-bar-bg: #fff; --download-bar-text: #000; }
  body { font-size: 11px; }
  .page-footer { background: #fff; border-color: #bbb; }
  /* Chrome we don't want in a printed report */
  .download-bar, #theme-toggle, .skip-link, #overview-footnote { display: none !important; }
  .page-header.usgs-banner { border-bottom-width: 2px; }
  /* Make the detail card flow naturally onto subsequent pages */
  .detail-card { border: 0; border-left: 2px solid #097945; padding: 0 0 0 8px; box-shadow: none; page-break-inside: auto; }
  .screenshots { page-break-before: auto; }
  .screenshots figure { break-inside: avoid; flex: 0 0 100%; max-width: 100%; }
  .data-table, .mini-table { font-size: 10px; }
  .data-table tbody tr { cursor: default; }
  .data-table tbody tr[aria-selected="true"] { background: transparent; }
  a { color: inherit; text-decoration: none; }
  a[href]:after { content: ""; } /* don't print URL after links */
  @page { margin: 16mm 14mm; }
}
