/* Carbon Debt Explorer — editorial aesthetic, IIASA blue palette */

:root {
  --font-display: 'Fraunces', 'Source Serif 4', Georgia, serif;
  --font-body: 'Source Sans 3', system-ui, sans-serif;
  --font-mono: 'JetBrains Mono', 'SF Mono', Consolas, monospace;

  /* IIASA canonical blue → OKLCH primary */
  --primary: oklch(42% 0.14 250);
  --primary-dim: oklch(42% 0.14 250 / 0.08);
  --primary-strong: oklch(32% 0.15 250);

  /* Warm tinted neutrals */
  --bg: oklch(98.5% 0.008 75);
  --surface: oklch(100% 0 0);
  --surface-sunk: oklch(96% 0.01 75);
  --border: oklch(88% 0.012 75);
  --border-strong: oklch(78% 0.015 75);
  --text: oklch(22% 0.015 250);
  --text-muted: oklch(45% 0.01 250);
  --text-dim: oklch(60% 0.01 250);

  /* Semantic — debt (warm) vs credit (cool-muted), not neon */
  --debt: oklch(52% 0.15 35);
  --debt-dim: oklch(52% 0.15 35 / 0.12);
  --credit: oklch(52% 0.09 190);
  --credit-dim: oklch(52% 0.09 190 / 0.12);

  /* R10 region palette — deliberately desaturated, readable */
  --r-afr: oklch(58% 0.12 35);
  --r-eas: oklch(48% 0.14 260);
  --r-eur: oklch(52% 0.12 180);
  --r-fsu: oklch(45% 0.08 310);
  --r-lam: oklch(60% 0.12 130);
  --r-mea: oklch(55% 0.13 65);
  --r-nam: oklch(42% 0.14 250);
  --r-pao: oklch(55% 0.10 340);
  --r-pas: oklch(62% 0.11 95);
  --r-sas: oklch(52% 0.14 15);

  --shadow-sm: 0 1px 2px rgb(20 30 60 / 0.04);
  --shadow-md: 0 4px 16px rgb(20 30 60 / 0.06);

  --radius: 6px;
  --radius-lg: 10px;
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: oklch(16% 0.015 250);
    --surface: oklch(20% 0.018 250);
    --surface-sunk: oklch(14% 0.015 250);
    --border: oklch(28% 0.02 250);
    --border-strong: oklch(35% 0.025 250);
    --text: oklch(95% 0.01 75);
    --text-muted: oklch(70% 0.01 75);
    --text-dim: oklch(55% 0.01 75);
    --primary: oklch(68% 0.15 250);
    --primary-dim: oklch(68% 0.15 250 / 0.15);
    --primary-strong: oklch(80% 0.12 250);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  max-width: 100%;
  overflow-x: hidden;
}

body {
  font-family: var(--font-body);
  font-size: 15px;
  line-height: 1.55;
  color: var(--text);
  background: var(--bg);
  font-feature-settings: "ss01", "cv11";
  min-height: 100vh;
}

a {
  color: var(--primary);
  text-decoration-thickness: 1px;
  text-underline-offset: 3px;
}

a:hover {
  color: var(--primary-strong);
}

h1, h2, h3, h4 {
  font-family: var(--font-display);
  font-weight: 500;
  letter-spacing: -0.01em;
  color: var(--text);
  margin: 0;
}

h1 {
  font-size: clamp(1.6rem, 2vw + 1rem, 2.4rem);
  font-weight: 450;
  line-height: 1.1;
  font-variation-settings: "opsz" 120, "SOFT" 50;
}

h2 {
  font-size: 1.25rem;
  font-weight: 500;
  font-variation-settings: "opsz" 14, "SOFT" 40;
}

h3 {
  font-size: 1rem;
  font-weight: 550;
  font-variation-settings: "opsz" 14;
}

/* ================= Layout ================= */

.topbar {
  border-bottom: 1px solid var(--border);
  background: var(--surface);
  padding: 14px 24px;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 12px 24px;
}

@media (max-width: 600px) {
  .topbar { padding: 12px 16px; gap: 8px 12px; }
  .brand-sub { display: none; }
  .nav { gap: 14px; font-size: 0.88rem; }
}

.brand {
  display: flex;
  align-items: baseline;
  gap: 12px;
}

.brand-mark {
  font-family: var(--font-display);
  font-weight: 500;
  font-size: 1.15rem;
  font-variation-settings: "opsz" 48, "SOFT" 30;
  color: var(--text);
  letter-spacing: -0.01em;
}

.brand-sub {
  font-size: 0.82rem;
  color: var(--text-dim);
  letter-spacing: 0.02em;
  text-transform: uppercase;
  text-decoration: none;
  transition: color 0.15s;
}

a.brand-sub:hover {
  color: var(--primary);
}

.nav {
  display: flex;
  gap: 20px;
  font-size: 0.9rem;
}

.nav a {
  color: var(--text-muted);
  text-decoration: none;
  padding: 4px 0;
  border-bottom: 2px solid transparent;
}

.nav a.active, .nav a:hover {
  color: var(--text);
  border-bottom-color: var(--primary);
}

.shell {
  display: grid;
  grid-template-columns: 320px minmax(0, 1fr);
  gap: 0;
  min-height: calc(100vh - 58px);
}

.shell > * { min-width: 0; }

@media (max-width: 900px) {
  .shell { grid-template-columns: minmax(0, 1fr); }
}

.sidebar {
  padding: 28px 24px;
  border-right: 1px solid var(--border);
  background: var(--surface-sunk);
  min-width: 0;
}

@media (max-width: 900px) {
  .sidebar {
    border-right: none;
    border-bottom: 1px solid var(--border);
    padding: 20px 20px 24px;
    position: static !important;
    max-height: none !important;
    overflow: visible !important;
  }
}

.sidebar h2 {
  font-size: 0.8rem;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  color: var(--text-dim);
  font-family: var(--font-body);
  font-weight: 600;
  margin-bottom: 14px;
}

.field {
  margin-bottom: 22px;
}

.field label {
  display: block;
  font-size: 0.82rem;
  color: var(--text-muted);
  margin-bottom: 6px;
  font-weight: 500;
}

.field select,
.field input[type="range"] {
  width: 100%;
  font: inherit;
  font-size: 0.93rem;
  color: var(--text);
}

.field select {
  background: var(--surface);
  border: 1px solid var(--border-strong);
  border-radius: var(--radius);
  padding: 8px 10px;
  line-height: 1.3;
  appearance: none;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='10' height='6' viewBox='0 0 10 6'><path fill='none' stroke='%23666' stroke-width='1.5' d='M1 1l4 4 4-4'/></svg>");
  background-repeat: no-repeat;
  background-position: right 10px center;
  padding-right: 28px;
}

.field select:focus,
.field input[type="range"]:focus {
  outline: 2px solid var(--primary);
  outline-offset: 1px;
}

.field-cite {
  margin-top: 6px;
  font-size: 0.75rem;
  color: var(--text-dim);
  line-height: 1.4;
}

.field-cite a {
  color: var(--text-muted);
  text-decoration: underline;
  text-decoration-color: var(--border-strong);
  text-underline-offset: 2px;
}

.field-cite a:hover {
  color: var(--primary);
  text-decoration-color: var(--primary);
}

.slider-row {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.slider-row input[type="range"] {
  flex: 1 1 auto;
  width: auto;
  min-width: 0;
}

.slider-row output {
  font-family: var(--font-mono);
  font-size: 0.9rem;
  color: var(--primary-strong);
  min-width: 42px;
  text-align: right;
  font-weight: 600;
  flex-shrink: 0;
}

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  height: 4px;
  background: var(--border-strong);
  border-radius: 2px;
  outline: none;
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--primary);
  cursor: pointer;
  border: 2px solid var(--surface);
  box-shadow: 0 0 0 1px var(--primary);
}

input[type="range"]::-moz-range-thumb {
  width: 16px;
  height: 16px;
  border-radius: 50%;
  background: var(--primary);
  cursor: pointer;
  border: 2px solid var(--surface);
  box-shadow: 0 0 0 1px var(--primary);
}

.year-legend {
  display: flex;
  justify-content: space-between;
  margin-top: 6px;
  font-family: var(--font-mono);
  font-size: 0.7rem;
  color: var(--text-dim);
  letter-spacing: 0.02em;
}

.year-legend span {
  flex: 1;
  text-align: center;
}
.year-legend span:first-child { text-align: left; }
.year-legend span:last-child { text-align: right; }

.legend-note {
  margin-top: 24px;
  padding: 12px 14px;
  border-left: 3px solid var(--primary);
  background: var(--primary-dim);
  font-size: 0.82rem;
  line-height: 1.5;
  color: var(--text-muted);
  border-radius: 0 var(--radius) var(--radius) 0;
}

.legend-note strong {
  color: var(--text);
  font-weight: 600;
}

/* ================= Main ================= */

.main {
  padding: 32px 32px 48px;
  display: flex;
  flex-direction: column;
  gap: 24px;
  min-width: 0;
  overflow-x: hidden;
}

@media (max-width: 900px) {
  .main { padding: 24px 20px 36px; }
}

@media (max-width: 600px) {
  .main { padding: 20px 14px 28px; }
}

.headline {
  max-width: 95ch;
}

.headline p {
  font-size: 0.98rem;
  color: var(--text-muted);
  margin: 10px 0 0;
  line-height: 1.6;
}

.summary-strip {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(160px, 1fr));
  gap: 1px;
  background: var(--border);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  overflow: hidden;
}

@media (max-width: 480px) {
  .summary-strip {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }
  .stat { padding: 12px 14px; }
  .stat-value { font-size: 1.25rem; }
}

.stat {
  background: var(--surface);
  padding: 16px 20px;
  min-width: 0;
}

.stat-label {
  font-size: 0.78rem;
  color: var(--text-dim);
  letter-spacing: 0.04em;
  text-transform: uppercase;
  font-weight: 600;
  margin-bottom: 4px;
}

.stat-value {
  font-family: var(--font-display);
  font-variation-settings: "opsz" 48;
  font-size: 1.55rem;
  font-weight: 500;
  color: var(--text);
  line-height: 1.1;
}

.stat-unit {
  font-size: 0.82rem;
  color: var(--text-dim);
  margin-left: 4px;
  font-family: var(--font-body);
  font-weight: 400;
}

.card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: var(--radius-lg);
  overflow: hidden;
  box-shadow: var(--shadow-sm);
  min-width: 0;
}

.card-head {
  padding: 18px 22px 10px;
  border-bottom: 1px solid var(--border);
}

.card-title {
  font-family: var(--font-display);
  font-variation-settings: "opsz" 18, "SOFT" 40;
  font-size: 1.08rem;
  font-weight: 500;
  color: var(--text);
  margin: 0 0 4px;
}

.card-sub {
  color: var(--text-muted);
  font-size: 0.87rem;
  margin: 0;
  line-height: 1.5;
}

.chart-container {
  height: 440px;
  padding: 10px 12px 16px;
  min-width: 0;
  overflow: hidden;
}

@media (max-width: 900px) {
  .chart-container { height: 400px; padding: 6px 6px 12px; }
}

@media (max-width: 600px) {
  .chart-container { height: 360px; }
}

.footer {
  margin-top: 32px;
  padding-top: 20px;
  border-top: 1px solid var(--border);
  color: var(--text-dim);
  font-size: 0.82rem;
  display: flex;
  justify-content: space-between;
  flex-wrap: wrap;
  gap: 8px;
}

/* About page — shares the dashboard shell, wider readable prose column */

.about-main {
  padding: 40px 48px 60px;
}

@media (max-width: 900px) {
  .about-main { padding: 28px 20px 40px; }
}

@media (max-width: 600px) {
  .about-main { padding: 22px 16px 32px; }
}

.prose {
  font-size: 1rem;
  line-height: 1.7;
  color: var(--text-muted);
  max-width: 95ch;
  width: 100%;
  word-wrap: break-word;
}

.prose h1 {
  margin-bottom: 4px;
}

.prose h2 {
  margin-top: 40px;
  margin-bottom: 12px;
  padding-bottom: 6px;
  border-bottom: 1px solid var(--border);
  color: var(--text);
  scroll-margin-top: 72px;
}

.prose h1 + .lede {
  font-size: 1.08rem;
  color: var(--text);
  font-family: var(--font-display);
  font-variation-settings: "opsz" 14, "SOFT" 40;
  line-height: 1.55;
  margin-top: 14px;
}

.prose p { margin: 0 0 14px; }

.prose strong, .prose b { color: var(--text); font-weight: 600; }

.prose em { color: var(--text); }

.prose ul {
  padding-left: 20px;
  margin: 0 0 18px;
}

.prose li {
  margin-bottom: 6px;
}

.prose code {
  font-family: var(--font-mono);
  font-size: 0.88em;
  background: var(--surface-sunk);
  border: 1px solid var(--border);
  border-radius: 4px;
  padding: 0.5px 5px;
  color: var(--primary-strong);
}

/* Sticky TOC in the sidebar on the About page */
.sidebar .toc {
  display: flex;
  flex-direction: column;
  gap: 4px;
  font-size: 0.9rem;
  margin-top: 4px;
}

.sidebar .toc a {
  color: var(--text-muted);
  text-decoration: none;
  padding: 4px 8px;
  border-left: 2px solid transparent;
  border-radius: 0 3px 3px 0;
  transition: color 0.15s, border-color 0.15s, background 0.15s;
  white-space: nowrap;
}

.sidebar .toc a:hover {
  color: var(--text);
  border-left-color: var(--primary);
  background: var(--primary-dim);
}

@media (max-width: 900px) {
  .sidebar .toc {
    flex-direction: row;
    overflow-x: auto;
    gap: 0;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: thin;
    padding-bottom: 4px;
    margin: 0 -20px;
    padding-left: 20px;
    padding-right: 20px;
  }
  .sidebar .toc a {
    padding: 4px 10px;
    border-left: none;
    border-bottom: 2px solid transparent;
    border-radius: 3px 3px 0 0;
    flex-shrink: 0;
  }
  .sidebar .toc a:hover {
    border-left: none;
    border-bottom-color: var(--primary);
  }
}

@media (min-width: 901px) {
  .sidebar {
    position: sticky;
    top: 58px;
    max-height: calc(100vh - 58px);
    overflow-y: auto;
  }
}

.callout {
  background: var(--primary-dim);
  border-left: 3px solid var(--primary);
  padding: 14px 18px;
  margin: 18px 0;
  font-size: 0.95rem;
  color: var(--text);
  border-radius: 0 var(--radius) var(--radius) 0;
}

/* Loader */
.loader {
  color: var(--text-dim);
  font-size: 0.9rem;
  padding: 80px 20px;
  text-align: center;
}

/* Fade-in on load */
.card, .summary-strip, .headline {
  animation: fadeUp 0.4s ease-out backwards;
}
.summary-strip { animation-delay: 0.05s; }
.card:nth-of-type(1) { animation-delay: 0.1s; }
.card:nth-of-type(2) { animation-delay: 0.18s; }

@keyframes fadeUp {
  from { opacity: 0; transform: translateY(8px); }
  to { opacity: 1; transform: none; }
}

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