:root {
  color-scheme: dark;
  --bg-editorial: #EAE6DC;
  --text-editorial: #171A1B;
  --bg-canvas: #171B20;
  --bg-panel: #20262C;
  --border-divider: #363D43;
  --text-muted: #B9C1C6;
  --text-main: #EAE6DC;
  --quantum: #62B6CF;
  --metro: #F08A73;
  --distress: #B9362D;
  --structural: #7A8B99;
  
  --font-editorial: 'Playfair Display', Georgia, serif;
  --font-ui: 'Inter', system-ui, -apple-system, sans-serif;
  --font-mono: 'IBM Plex Mono', ui-monospace, SFMono-Regular, monospace;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

body {
  font-family: var(--font-ui);
  background-color: var(--bg-canvas);
  color: var(--text-main);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

/* App Header */
.app-header {
  background-color: var(--bg-panel);
  border-bottom: 1px solid var(--border-divider);
  padding: 0.5rem 0;
  font-size: 0.85rem;
}

.header-container {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 2rem;
}

.brand {
  font-weight: 600;
  color: var(--text-muted);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  text-decoration: none;
}

.brand:hover { color: var(--text-main); }

.header-nav button.ghost {
  font-family: var(--font-mono);
  font-size: 0.75rem;
  color: var(--text-muted);
  border-color: var(--border-divider);
  background: transparent;
  text-transform: uppercase;
  letter-spacing: 0.05em;
}

.header-nav button.ghost:hover:not(:disabled) {
  background-color: var(--text-main);
  color: var(--bg-panel);
}

.container { max-width: 96%; margin: 0 auto; padding: 0 1.5rem; }

/* Editorial Opening */
.editorial {
  background-color: var(--bg-editorial);
  color: var(--text-editorial);
  padding: 1.5rem 0 2rem;
  border-bottom: 1px solid var(--border-divider);
}

.editorial h1 {
  font-family: var(--font-editorial);
  font-size: 1.8rem;
  font-weight: 600;
  font-style: italic;
  line-height: 1.2;
  margin-bottom: 0.25rem;
  letter-spacing: -0.02em;
}

.editorial .subtitle {
  font-family: var(--font-ui);
  font-size: 1rem;
  font-weight: 500;
  color: #4A5155;
  margin-bottom: 1rem;
}

.editorial .intro-text {
  font-size: 0.9rem;
  max-width: 900px;
  margin-bottom: 1.5rem;
  color: #33383B;
  border-left: 2px solid #33383B;
  padding-left: 1rem;
}

.run-controls {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 1rem;
  background-color: rgba(255, 255, 255, 0.5);
  padding: 0.75rem 1rem;
  border: 1px solid rgba(0,0,0,0.15);
  border-radius: 4px;
}

.run-status {
  display: flex;
  align-items: center;
  gap: 0.5rem;
  font-family: var(--font-mono);
  font-size: 0.85rem;
}

.status-label {
  color: #666;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  font-size: 0.75rem;
}

#status {
  font-weight: 500;
  color: var(--text-editorial);
}

.actions {
  display: flex;
  align-items: center;
  gap: 0.75rem;
  flex-wrap: wrap;
}

button {
  font-family: var(--font-ui);
  background-color: transparent;
  color: var(--text-editorial);
  border: 1px solid #171A1B;
  padding: 0.4rem 0.8rem;
  font-size: 0.85rem;
  font-weight: 500;
  cursor: pointer;
  border-radius: 3px;
  transition: all 0.15s ease;
}

button:hover:not(:disabled) {
  background-color: var(--text-editorial);
  color: var(--bg-editorial);
}

button:disabled { opacity: 0.4; cursor: not-allowed; }

label {
  font-family: var(--font-mono);
  font-size: 0.75rem;
  display: inline-flex;
  align-items: center;
  gap: 0.5rem;
  color: #4A5155;
  text-transform: uppercase;
}

input[type="number"], select {
  font-family: var(--font-mono);
  background-color: #fff;
  color: var(--text-editorial);
  border: 1px solid #ccc;
  padding: 0.3rem 0.5rem;
  border-radius: 3px;
  font-size: 0.85rem;
  width: 70px;
}

/* ------------------------------- Landing ------------------------------- */

.landing-hero { padding: 3rem 0 3.5rem; }
/* Center the reading column instead of hugging the left edge of a
   96%-wide container: the hero text caps at ~78ch, so the container
   should too, with margin auto centering it like an editorial page. */
.landing-hero .container { max-width: 78ch; }
.landing-hero h1 { font-size: 2.6rem; max-width: 20ch; margin-bottom: 0.75rem; }
.landing-hero .subtitle { max-width: 70ch; font-size: 1.05rem; margin-bottom: 2.5rem; }

/* Theory essay on the landing page */
.theory { max-width: 78ch; }

.theory h2 {
  font-family: var(--font-mono);
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: #6A7177;
  border-bottom: 1px solid rgba(0, 0, 0, 0.18);
  padding-bottom: 0.4rem;
  margin: 2rem 0 0.85rem;
}

.theory h2:first-child { margin-top: 0; }

.theory p { font-size: 0.95rem; color: #33383B; margin-bottom: 0.9rem; }
.theory strong, .claim-box strong { font-weight: 600; color: var(--text-editorial); }

.theory .eq {
  margin: 1.2rem 0 1.4rem;
  padding: 0.9rem 1.25rem;
  background-color: rgba(255, 255, 255, 0.55);
  border: 1px solid rgba(0, 0, 0, 0.12);
  border-radius: 4px;
  overflow-x: auto;
}

.theory .eq .katex-display { margin: 0.35rem 0; }

.theory .katex { font-size: 1.02em; }

/* "what each term means" mapping table + shock encodings */
.mapping-block, .shock-block { margin-bottom: 2rem; max-width: 95ch; }

.mapping-block h2, .shock-block h2 {
  font-family: var(--font-mono);
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: #6A7177;
  border-bottom: 1px solid rgba(0, 0, 0, 0.18);
  padding-bottom: 0.4rem;
  margin-bottom: 0.85rem;
}

table.mapping {
  width: 100%;
  border-collapse: collapse;
  font-size: 0.87rem;
  color: #33383B;
}

table.mapping th {
  text-align: left;
  font-family: var(--font-mono);
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: #6A7177;
  font-weight: 500;
  padding-bottom: 0.4rem;
}

table.mapping td { padding: 0.45rem 1rem 0.45rem 0; border-top: 1px solid rgba(0, 0, 0, 0.12); vertical-align: top; }
table.mapping td:first-child { font-family: var(--font-mono); font-size: 0.82rem; color: var(--text-editorial); white-space: nowrap; }
table.mapping td:last-child { color: #4A5155; padding-right: 0; }

.shock-block p { font-size: 0.92rem; color: #33383B; }
.shock-list { margin: 0.85rem 0; padding-left: 1.2rem; display: grid; gap: 0.45rem; font-size: 0.9rem; color: #33383B; }
.shock-list strong { font-weight: 600; }
.shock-note { border-left: 2px solid #33383B; padding-left: 0.9rem; margin-top: 0.85rem; }

/* Domain grid: 4 x 2 on desktop, collapsing naturally on narrow screens.
   Capped and centered so the 4-up grid doesn't stretch edge to edge
   on wide monitors while the essay above sits in a narrow column. */
.domains { padding-top: 2.5rem; max-width: 1200px; margin: 0 auto; }

.domain-grid {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 1rem;
}

@media (max-width: 1200px) {
  .domain-grid { grid-template-columns: repeat(2, 1fr); }
}

@media (max-width: 640px) {
  .domain-grid { grid-template-columns: 1fr; }
}

.domain {
  display: flex;
  flex-direction: column;
  gap: 0.45rem;
  text-align: left;
  background-color: var(--bg-panel);
  border: 1px solid var(--border-divider);
  border-radius: 4px;
  padding: 1rem 1.1rem;
  color: var(--text-main);
  font-family: var(--font-ui);
}

.domain.built { cursor: pointer; border-color: rgba(98, 182, 207, 0.4); }
.domain.built:hover { background-color: var(--bg-panel); color: var(--text-main); border-color: var(--quantum); }

.domain-head { display: flex; align-items: baseline; justify-content: space-between; gap: 0.5rem; }
.domain h3 { font-size: 0.95rem; font-weight: 600; }
.built-tag { color: var(--quantum); border-color: rgba(98, 182, 207, 0.45); }

.domain-coupling { font-size: 0.8rem; color: var(--text-muted); }
.domain-coupling .domain-k { font-family: var(--font-mono); font-size: 0.75rem; color: var(--text-main); }

.domain-tail { font-size: 0.8rem; color: var(--text-muted); margin-top: auto; }
.domain-tail::before {
  content: 'tail question: ';
  font-family: var(--font-mono);
  font-size: 0.68rem;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: #8B9399;
}

.claim-box {
  background-color: rgba(255, 255, 255, 0.5);
  border: 1px solid rgba(0, 0, 0, 0.15);
  border-left: 3px solid var(--text-editorial);
  border-radius: 4px;
  padding: 1rem 1.25rem;
  max-width: 95ch;
}

.claim-box h2 {
  font-family: var(--font-mono);
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: #4A5155;
  margin-bottom: 0.5rem;
}

.claim-box p { font-size: 0.92rem; color: #33383B; }

/* Scenario chooser */
.chooser { padding-top: 2.5rem; padding-bottom: 4rem; max-width: 1200px; margin: 0 auto; }

.chooser-title {
  font-family: var(--font-editorial);
  font-size: 1.6rem;
  font-style: italic;
  font-weight: 600;
  margin-bottom: 0.4rem;
}

.chooser-sub { color: var(--text-muted); max-width: 80ch; margin-bottom: 1.75rem; font-size: 0.92rem; }
.chooser-foot { color: var(--text-muted); font-size: 0.8rem; font-family: var(--font-mono); margin-top: 1.5rem; }

.scenario-cards {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(320px, 1fr));
  gap: 1.25rem;
}

.scenario-card {
  display: flex;
  flex-direction: column;
  gap: 0.75rem;
  text-align: left;
  background-color: var(--bg-panel);
  border: 1px solid var(--border-divider);
  border-radius: 4px;
  padding: 1.25rem;
  color: var(--text-main);
  cursor: pointer;
  transition: border-color 0.15s ease, transform 0.15s ease;
}

.scenario-card:hover:not(:disabled) {
  background-color: var(--bg-panel);
  color: var(--text-main);
  border-color: var(--quantum);
  transform: translateY(-2px);
}

.card-tags { display: flex; gap: 0.5rem; align-items: center; flex-wrap: wrap; }

.tag {
  font-family: var(--font-mono);
  font-size: 0.65rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  padding: 0.15rem 0.45rem;
  border-radius: 2px;
  border: 1px solid var(--border-divider);
  color: var(--text-muted);
}

.tag.finance { color: var(--quantum); border-color: rgba(98, 182, 207, 0.45); }
.tag.traffic { color: #8FBF7A; border-color: rgba(143, 191, 122, 0.45); }

.scenario-card h3 {
  font-family: var(--font-editorial);
  font-style: italic;
  font-size: 1.3rem;
  font-weight: 600;
  line-height: 1.2;
}

.card-story { font-size: 0.87rem; color: var(--text-muted); flex: 1; }

.card-headline {
  font-family: var(--font-mono);
  font-size: 0.78rem;
  border-top: 1px solid var(--border-divider);
  padding-top: 0.7rem;
  display: grid;
  gap: 0.3rem;
}

.card-headline .row { display: flex; justify-content: space-between; gap: 1rem; }
.card-headline .q { color: var(--quantum); }
.card-headline .m { color: var(--metro); }
.card-headline .gap { color: var(--text-main); }
.card-cta { font-family: var(--font-mono); font-size: 0.72rem; color: var(--text-muted); text-transform: uppercase; letter-spacing: 0.06em; }
.scenario-card:hover .card-cta { color: var(--quantum); }

/* Scenario switcher inside the dashboard */
.scenario-switcher {
  display: flex;
  flex-wrap: wrap;
  gap: 0.5rem;
  align-items: center;
  margin-bottom: 1.25rem;
}

.scenario-switcher .switch-label {
  font-family: var(--font-mono);
  font-size: 0.7rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: #6A7177;
  margin-right: 0.25rem;
}

.scenario-switcher button {
  font-family: var(--font-mono);
  font-size: 0.75rem;
  padding: 0.3rem 0.65rem;
  border-color: rgba(0, 0, 0, 0.3);
}

.scenario-switcher button[aria-current="true"] {
  background-color: var(--text-editorial);
  color: var(--bg-editorial);
}

/* Scientific Canvas */
.scientific-canvas { padding-top: 1.5rem; }

.main-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(450px, 1fr));
  grid-auto-flow: dense;
  gap: 1.5rem;
  margin-bottom: 1.5rem;
}

/* A full-width panel (the ranking chart) spans every column of the row. */
.panel-wide { grid-column: 1 / -1; }

.panel {
  background-color: var(--bg-panel);
  border: 1px solid var(--border-divider);
  display: flex;
  flex-direction: column;
  height: 100%;
}

.panel-header {
  padding: 1rem 1.2rem;
  border-bottom: 1px solid var(--border-divider);
}

.panel h2 {
  font-family: var(--font-mono);
  font-size: 0.85rem;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-main);
  margin-bottom: 0.4rem;
}

.panel .hint { font-size: 0.85rem; color: var(--text-muted); }

/* Canvas boxes get DETERMINISTIC heights from CSS, never from flex.
   A canvas is a replaced element: its width/height attributes are its
   intrinsic size, so sizing the box with `flex: 1` makes layout depend on the
   backing store that the renderer is simultaneously computing from the box.
   That circularity is what made the page jitter on some machines. */
.network-container { display: flex; flex-direction: column; flex: 1; }
.network {
  width: 100%;
  height: clamp(300px, 34vh, 440px);
  background-color: var(--bg-canvas);
  cursor: crosshair;
  display: block;
}
/* Network legend: says what every visual channel means. */
.net-legend {
  border-top: 1px solid var(--border-divider);
  background-color: #1A2025;
  padding: 0.85rem 1.1rem 1rem;
}

.net-legend ul {
  list-style: none;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(230px, 1fr));
  gap: 0.5rem 1.25rem;
}

.net-legend li { display: flex; align-items: flex-start; gap: 0.6rem; }

.net-legend .leg-text { display: flex; flex-direction: column; font-size: 0.74rem; line-height: 1.35; }
.net-legend .leg-text b { font-family: var(--font-mono); font-size: 0.7rem; color: var(--text-main); font-weight: 500; }
.net-legend .leg-text span { color: var(--text-muted); }

.net-legend .sw { flex: 0 0 auto; width: 34px; display: flex; align-items: center; gap: 2px; margin-top: 0.15rem; }
.sw-ramp { height: 10px; border-radius: 2px; }
.sw-size i { display: block; background: #55707E; border-radius: 50%; }
.sw-size { justify-content: center; }

.sw-dash { flex-direction: column; gap: 2px; }
.sw-dash i { display: block; width: 100%; height: 0; border-top: 1.5px solid rgba(234, 230, 220, 0.5); }
.sw-dash i.d1 { border-top-style: dashed; }
.sw-dash i.d2 { border-top-style: dotted; }

.sw-edge { flex-direction: column; gap: 3px; }
.sw-edge i { display: block; width: 100%; height: 0; }
.sw-edge i.ferro { border-top: 2px solid rgba(122, 139, 153, 0.85); }
.sw-edge i.frus { border-top: 2px dashed var(--metro); }

.sw-mark { font-family: var(--font-mono); font-weight: 700; font-size: 0.8rem; color: var(--quantum); justify-content: center; }

.sw-seed {
  width: 14px; height: 14px; margin-left: 10px;
  border: 1.5px solid var(--distress); border-radius: 50%;
}

.leg-callout {
  margin-top: 0.8rem;
  padding-top: 0.7rem;
  border-top: 1px solid var(--border-divider);
  font-size: 0.78rem;
  color: var(--text-main);
  font-family: var(--font-mono);
  line-height: 1.5;
}

.charts { padding: 0.75rem; display: flex; flex-direction: column; flex: 1; }
canvas { display: block; width: 100%; height: 210px; margin-bottom: 0.75rem; }
/* Full-width ranking panel: 12-16 bar groups read badly in 210px. */
#chart-ranking { height: 320px; margin-bottom: 0; }

.stat-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(130px, 1fr));
  border-top: 1px solid var(--border-divider);
  background-color: #1A2025;
}

.stat {
  padding: 0.8rem 1rem;
  font-family: var(--font-mono);
  font-size: 0.7rem;
  color: var(--text-muted);
  border-right: 1px solid var(--border-divider);
}

.stat:last-child { border-right: none; }
.stat b { display: block; font-size: 1.1rem; color: var(--text-main); margin-top: 0.2rem; font-weight: 400; }
.stat.quantum b { color: var(--quantum); }
.stat.metro b { color: var(--metro); }

/* Solution Panel */
.solution-panel { margin-bottom: 2rem; }
.solution-content { padding: 1.5rem; font-size: 0.95rem; color: var(--text-main); display: grid; gap: 1rem; }
.solution-item { display: flex; flex-direction: column; gap: 0.25rem; }
.solution-item strong { color: var(--text-muted); font-family: var(--font-mono); font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.05em; }

/* Methodology */
.methodology {
  padding: 2rem 0 4rem;
  border-top: 1px solid var(--border-divider);
  color: var(--text-muted);
  font-size: 0.9rem;
  max-width: 800px;
}

.methodology h2 {
  font-family: var(--font-editorial);
  font-size: 1.5rem;
  font-weight: 600;
  font-style: italic;
  color: var(--text-main);
  margin-bottom: 1rem;
}

.methodology p { margin-bottom: 1rem; }
.references { margin: 1.5rem 0; padding-left: 1.5rem; font-family: var(--font-mono); font-size: 0.8rem; }
.references li { margin-bottom: 0.5rem; }
.attribution { margin-top: 2rem; font-family: var(--font-mono); font-size: 0.75rem; text-transform: uppercase; letter-spacing: 0.05em; opacity: 0.7; }

/* Responsive */
@media (max-width: 1024px) {
  .main-grid { grid-template-columns: 1fr; }
}

@media (max-width: 700px) {
  .landing-hero h1 { font-size: 1.9rem; }

  /* Three narrow columns cannot hold a sentence each; stack every row into a
     labelled block instead of letting the last column run off-screen. */
  table.mapping thead { display: none; }
  table.mapping, table.mapping tbody, table.mapping tr, table.mapping td { display: block; width: 100%; }
  table.mapping tr { border-top: 1px solid rgba(0, 0, 0, 0.12); padding: 0.6rem 0; }
  table.mapping td { border: 0; padding: 0; }
  table.mapping td:first-child { white-space: normal; margin-bottom: 0.15rem; }
  table.mapping td:nth-child(2)::before { content: 'means: '; color: #6A7177; }
  table.mapping td:last-child::before { content: 'so: '; color: #6A7177; }

  .domain-coupling, .domain-tail { font-size: 0.78rem; }
}

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