:root {
  --bg: #f3efe2;
  --paper: rgba(255, 252, 244, 0.78);
  --ink: #1a1a17;
  --muted: #6a675d;
  --line: rgba(43, 40, 32, 0.12);
  --accent: #0e6b5c;
  --goal: #f1a208;
  --agent: #2a7de1;
  --wall: #00a6a6;
  --noise: #c84c8a;
  --total: #131515;
  --left-flow: #d1495b;
  --right-flow: #00798c;
  --shadow: 0 24px 70px rgba(37, 31, 15, 0.12);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  padding: 12px 16px;
  min-height: 100vh;
  min-height: 100svh;
  overflow: hidden;
  font-family: Georgia, "Times New Roman", serif;
  color: var(--ink);
  background:
    radial-gradient(circle at top left, rgba(255, 255, 255, 0.8), transparent 28%),
    linear-gradient(135deg, #efe8d3 0%, #f8f4ea 44%, #dfebea 100%);
}

.layout {
  width: min(1400px, 100%);
  height: calc(100vh - 24px);
  height: calc(100svh - 24px);
  margin: 0 auto;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  gap: 18px;
}

.content-grid {
  display: grid;
  grid-template-columns: minmax(320px, 0.95fr) minmax(0, 2fr);
  gap: 18px;
  align-items: stretch;
  min-height: 0;
}

.hero,
.panel {
  backdrop-filter: blur(14px);
  background: var(--paper);
  border: 1px solid var(--line);
  border-radius: 24px;
  box-shadow: var(--shadow);
}

.hero {
  padding: 10px 14px;
  display: block;
}

.eyebrow {
  margin: 0 0 2px;
  text-transform: uppercase;
  letter-spacing: 0.16em;
  font-size: 0.68rem;
  color: var(--accent);
}

h1 {
  margin: 0;
  font-size: clamp(1.15rem, 1.8vw, 1.75rem);
  line-height: 0.92;
}

.lede {
  margin: 4px 0 0;
  max-width: 60ch;
  color: var(--muted);
  font-size: 0.84rem;
  line-height: 1.22;
}

.stats {
  display: grid;
  grid-template-columns: repeat(3, minmax(100px, 1fr));
  gap: 8px;
  align-content: start;
}

.stat {
  padding: 8px 10px;
  border-radius: 14px;
  background: rgba(255, 255, 255, 0.55);
  border: 1px solid rgba(0, 0, 0, 0.05);
}

.label {
  display: block;
  color: var(--muted);
  font-size: 0.74rem;
}

.stat strong {
  font-size: 1rem;
}

.side-panel {
  padding: 10px;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.tab-header {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  margin-bottom: 10px;
  flex-shrink: 0;
}

.tab-button {
  border: 1px solid rgba(43, 40, 32, 0.12);
  border-radius: 14px;
  padding: 9px 12px;
  font: inherit;
  color: var(--muted);
  background: rgba(255, 255, 255, 0.5);
  cursor: pointer;
}

.tab-button.is-active {
  color: #fffdf6;
  border-color: transparent;
  background: linear-gradient(135deg, #0f6a5c, #174c62);
}

.tab-panel {
  min-height: 0;
  flex: 1;
}

.controls-panel {
  overflow: auto;
}

.tab-panel.is-hidden {
  display: none;
}

.model-option.is-hidden {
  display: none;
}

.controls {
  padding: 6px 8px;
  display: grid;
  grid-template-columns: 1fr;
  gap: 12px;
  align-content: start;
}

.controls label {
  display: grid;
  gap: 8px;
  font-size: 0.95rem;
}

.controls input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
}

.controls select {
  width: 100%;
  border: 1px solid rgba(43, 40, 32, 0.12);
  border-radius: 14px;
  padding: 10px 12px;
  background: rgba(255, 255, 255, 0.72);
  color: var(--ink);
  font: inherit;
}

.toggle {
  grid-template-columns: auto 1fr;
  align-items: center;
  gap: 10px;
}

.toggle input {
  width: 18px;
  height: 18px;
  accent-color: var(--accent);
}

.actions {
  display: flex;
  gap: 10px;
  align-self: end;
  flex-wrap: wrap;
}

.actions button {
  border: 0;
  border-radius: 999px;
  padding: 10px 16px;
  font: inherit;
  cursor: pointer;
  color: #fffdf6;
  background: linear-gradient(135deg, #0f6a5c, #174c62);
}

.actions button:last-child {
  background: linear-gradient(135deg, #8d4e2f, #b76d2a);
}

.viz {
  padding: 16px;
  display: flex;
  flex-direction: column;
  min-height: 0;
}

.explainer {
  padding: 8px 10px;
  min-height: 0;
  overflow: auto;
}

.explainer h2 {
  margin: 0 0 10px;
  font-size: 1.75rem;
}

.explain-copy {
  margin: 0 0 14px;
  color: var(--muted);
  line-height: 1.5;
}

.compact {
  margin-bottom: 0;
}

.formula-card {
  padding: 12px 14px;
  border-radius: 18px;
  background: rgba(255, 255, 255, 0.55);
  border: 1px solid rgba(0, 0, 0, 0.05);
  margin-top: 12px;
}

.formula-label {
  margin-bottom: 8px;
  color: var(--muted);
  font-size: 0.82rem;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

.formula {
  display: block;
  white-space: pre-wrap;
  font-size: 0.95rem;
  line-height: 1.55;
}

.references-copy {
  color: var(--muted);
  font-size: 0.88rem;
  line-height: 1.4;
}

.references-copy p {
  margin: 0;
}

.references-copy p + p {
  margin-top: 10px;
}

.formula mjx-container {
  overflow-x: auto;
  overflow-y: hidden;
}

.legend {
  display: flex;
  flex-wrap: wrap;
  gap: 8px 16px;
  margin: 0 0 10px;
  color: var(--muted);
  font-size: 0.9rem;
}

.swatch {
  display: inline-block;
  width: 16px;
  height: 3px;
  margin-right: 8px;
  vertical-align: middle;
  border-radius: 999px;
}

.goal { background: var(--goal); }
.agent { background: var(--agent); }
.agent-base { background: rgba(42, 125, 225, 0.35); }
.wall { background: var(--wall); }
.noise { background: var(--noise); }
.total { background: var(--total); }
.utility { background: linear-gradient(90deg, #f1a208, #00798c); }
.pred-ped {
  height: 0;
  border-top: 3px dashed rgba(14, 107, 92, 0.85);
  background: transparent;
}
.pred-wall {
  height: 0;
  border-top: 3px dashed rgba(0, 166, 166, 0.85);
  background: transparent;
}

.canvas-frame {
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

canvas {
  width: 100%;
  max-width: 100%;
  max-height: 100%;
  height: auto;
  display: block;
  border-radius: 18px;
  background:
    linear-gradient(180deg, rgba(250, 247, 239, 0.94), rgba(233, 227, 212, 0.96)),
    repeating-linear-gradient(
      90deg,
      rgba(27, 36, 41, 0.02) 0,
      rgba(27, 36, 41, 0.02) 24px,
      transparent 24px,
      transparent 48px
    );
}

.hint {
  margin: 10px 2px 0;
  color: var(--muted);
  flex-shrink: 0;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

@media (min-width: 981px) {
  .hero {
    padding: 8px 12px;
  }

  .lede {
    display: -webkit-box;
    -webkit-box-orient: vertical;
    -webkit-line-clamp: 1;
    overflow: hidden;
    font-size: 0.78rem;
  }

  .side-panel {
    padding: 8px;
  }

  .tab-header {
    margin-bottom: 8px;
  }

  .tab-button {
    padding: 8px 10px;
    font-size: 0.9rem;
  }

  .controls {
    padding: 4px 6px;
  }

  .controls label {
    gap: 6px;
    font-size: 0.9rem;
  }

  .controls select,
  .actions button {
    padding-top: 8px;
    padding-bottom: 8px;
  }

  .explainer {
    padding: 6px 8px;
  }

  .explain-copy {
    line-height: 1.4;
  }

  .formula-card {
    margin-top: 10px;
  }

  .viz {
    padding: 14px;
  }

  .legend {
    gap: 6px 14px;
    margin-bottom: 8px;
    font-size: 0.85rem;
  }

  .hint {
    margin-top: 8px;
    font-size: 0.9rem;
  }
}

@media (max-width: 980px) {
  body {
    padding: 0;
    overflow-y: auto;
  }

  .hero {
    flex-direction: column;
  }

  .content-grid,
  .stats {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .content-grid {
    grid-template-columns: 1fr;
  }

  .layout {
    width: min(1400px, calc(100vw - 32px));
    height: auto;
    min-height: auto;
    margin: 18px auto 24px;
    grid-template-rows: auto;
  }

  .explainer,
  .viz,
  .content-grid,
  .canvas-frame,
  .side-panel,
  .tab-panel {
    min-height: auto;
    overflow: visible;
  }

  .tab-header {
    margin-bottom: 12px;
  }

  .controls {
    padding: 0;
  }

  canvas {
    width: 100%;
    max-height: none;
  }
}

@media (max-width: 640px) {
  .layout {
    width: min(100vw - 18px, 1400px);
    margin: 10px auto 20px;
  }

  .hero,
  .panel {
    border-radius: 18px;
  }

  .hero,
  .side-panel,
  .explainer,
  .viz {
    padding: 16px;
  }

  .content-grid,
  .stats,
  .controls {
    grid-template-columns: 1fr;
  }
}
