/* Coherence Lattice — Explorable Explanation
 * Typography and layout inspired by Bret Victor's essays.
 */

/* Dark is the default. Light mode is opt-in via data-theme="light". */
:root,
:root[data-theme="dark"] {
  --bg: #18160f;
  --bg-soft: #23201a;
  --text: #e8e2d3;
  --text-soft: #b8b0a0;
  --text-faint: #847e71;
  --rule: #3a342c;
  --accent: #7db3dd;
  --accent-light: #9cc6e6;
  --alive: #e89564;
  --alive-soft: #8a6442;
  --dead: #5a554c;
  --highlight: #4a3f1a;
  --vortex: #d17a97;
  --panel: #23201a;
  --panel-border: #4a4339;
  --canvas-bg: #23201a;
  --canvas-bg-soft: #2c2822;
  --canvas-text: #e8e2d3;
  --canvas-text-soft: #b8b0a0;
  --canvas-text-faint: #847e71;
  --canvas-grid: #3a342c;
  --canvas-axis: #55503f;
  --canvas-axis-bold: #6a6450;
  /* Physics colours, brightened for dark contrast. */
  --c-blue: #6ba4d4;
  --c-green: #5ea87a;
  --c-orange: #e89564;
  --c-burgundy: #d17a97;
  --c-purple: #c38df7;
}

:root[data-theme="light"] {
  --bg: #fdfaf3;
  --bg-soft: #f5f0e4;
  --text: #2a2a2a;
  --text-soft: #555;
  --text-faint: #888;
  --rule: #d8d4c8;
  --accent: #2a5f8f;
  --accent-light: #6ba4d4;
  --alive: #d97236;
  --alive-soft: #f0c29a;
  --dead: #a8a39a;
  --highlight: #f4e4a8;
  --vortex: #7d2d4f;
  --panel: #f5f0e4;
  --panel-border: #c8c0ad;
  --canvas-bg: #f5f0e4;
  --canvas-bg-soft: #fdfaf3;
  --canvas-text: #2a2a2a;
  --canvas-text-soft: #555;
  --canvas-text-faint: #888;
  --canvas-grid: #e0dbcb;
  --canvas-axis: #c8c0ad;
  --canvas-axis-bold: #c0b9a0;
  --c-blue: #2a5f8f;
  --c-green: #2d7d4f;
  --c-orange: #d97236;
  --c-burgundy: #7d2d4f;
  --c-purple: #a855f7;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  padding: 0;
  background: var(--bg);
  color: var(--text);
  font-family: "Charter", "Iowan Old Style", "Palatino Linotype", "Book Antiqua", Georgia, serif;
  font-size: 19px;
  line-height: 1.55;
  -webkit-font-smoothing: antialiased;
}

.page {
  max-width: 720px;
  margin: 0 auto;
  padding: 96px 32px 128px;
}

.wide {
  max-width: 960px;
}

/* Breadcrumb row above the title — sits in its own horizontal band */
.breadcrumb {
  font-size: 0.85em;
  color: var(--text-faint);
  letter-spacing: 0.02em;
  margin: 0 0 3.2em;
  padding-bottom: 0.6em;
  border-bottom: 1px solid var(--rule);
  display: flex;
  align-items: baseline;
  gap: 0.8em;
}

.breadcrumb a {
  color: var(--text-soft);
  border-bottom: none;
}

.breadcrumb a:hover {
  color: var(--accent);
}

.breadcrumb .sep {
  color: var(--rule);
}

.breadcrumb .section-num {
  font-family: "SF Mono", "Menlo", "Consolas", monospace;
  font-size: 0.9em;
  color: var(--text-faint);
  letter-spacing: 0.06em;
}

h1 {
  font-size: 2.6em;
  line-height: 1.08;
  font-weight: 600;
  letter-spacing: -0.015em;
  margin: 0 0 0.4em;
  color: var(--text);
}

h1 .subtitle {
  display: block;
  font-size: 0.46em;
  font-weight: 400;
  font-style: italic;
  color: var(--text-soft);
  margin-top: 0.6em;
  letter-spacing: 0;
  line-height: 1.35;
}

/* A consistent gap between the title block and the first body paragraph */
h1 + p, h1 + .lead {
  margin-top: 1.8em;
}

h2 {
  font-size: 1.55em;
  margin: 2.6em 0 0.7em;
  font-weight: 600;
  letter-spacing: -0.005em;
}

h3 {
  font-size: 1.1em;
  margin: 2em 0 0.4em;
  font-weight: 600;
  font-style: italic;
  color: var(--text-soft);
}

p {
  margin: 0 0 1em;
}

/* Lists use a tight, consistent indent from the left column. Avoids stacking
 * with browser default padding-left when sections also declare inline margin. */
.page ul, .page ol {
  padding-left: 1.4em;
  margin-left: 0;
}
.page li { margin: 0.25em 0; }

@media (max-width: 720px) {
  .page ul, .page ol { padding-left: 1.1em; }
}

p + p {
  text-indent: 1.4em;
  margin-top: -0.2em;
}

.lead, .lead + p {
  text-indent: 0;
}

/* Lead paragraph: same size as body, visually distinguished by weight + drop-cap space */
.lead {
  font-size: 1em;
  line-height: 1.6;
  color: var(--text);
  margin-bottom: 1.4em;
}

.lead::first-letter {
  font-weight: 600;
}

a {
  color: var(--accent);
  text-decoration: none;
  border-bottom: 1px solid var(--accent-light);
}

a:hover {
  border-bottom-color: var(--accent);
}

em {
  font-style: italic;
}

strong {
  font-weight: 600;
}

code, .mono {
  font-family: "SF Mono", "Menlo", "Consolas", monospace;
  font-size: 0.92em;
  background: var(--panel);
  padding: 1px 5px;
  border-radius: 3px;
  color: var(--text);
}

hr {
  border: none;
  border-top: 1px solid var(--rule);
  margin: 3em 0;
}

/* --- Figures and interactives --- */

/* Default (dark-mode) figure treatment: bordered box with rounded corners.
 * Light-mode override below uses top/bottom bands for a cream-paper feel. */
figure {
  margin: 2em 0;
  padding: 24px 32px;
  background: var(--bg-soft);
  border: 1px solid var(--panel-border);
  border-radius: 6px;
}

:root[data-theme="light"] figure {
  margin: 2em -32px;
  background: var(--panel);
  border: none;
  border-top: 1px solid var(--panel-border);
  border-bottom: 1px solid var(--panel-border);
  border-radius: 0;
}

figure.plain,
:root[data-theme="light"] figure.plain {
  background: transparent;
  border: none;
  padding: 0;
}

figcaption {
  font-size: 0.88em;
  color: var(--text-soft);
  margin-top: 0.8em;
  font-style: italic;
}

canvas {
  display: block;
  max-width: 100%;
  margin: 0 auto;
  image-rendering: auto;
}

/* --- Inline interactive values --- */

.tune {
  display: inline-block;
  cursor: ew-resize;
  color: var(--accent);
  font-weight: 600;
  border-bottom: 1px dashed var(--accent-light);
  padding: 0 2px;
  user-select: none;
  touch-action: none;
}

.tune:hover {
  background: var(--highlight);
}

.tune.active {
  background: var(--highlight);
  border-bottom-style: solid;
}

.readout {
  display: inline-block;
  color: var(--alive);
  font-family: "SF Mono", "Menlo", "Consolas", monospace;
  font-size: 0.95em;
  font-weight: 500;
}

/* --- Controls --- */

.controls {
  display: flex;
  flex-wrap: wrap;
  gap: 18px 32px;
  margin: 18px 0;
  padding: 16px;
  background: var(--bg);
  border: 1px solid var(--panel-border);
  border-radius: 4px;
  font-size: 0.92em;
}

.control {
  display: flex;
  flex-direction: column;
  gap: 4px;
  min-width: 140px;
}

.control label {
  color: var(--text-soft);
  font-size: 0.88em;
  display: flex;
  justify-content: space-between;
  gap: 8px;
}

.control label .val {
  color: var(--alive);
  font-family: "SF Mono", "Menlo", "Consolas", monospace;
}

.control input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  width: 100%;
  height: 3px;
  background: var(--rule);
  border-radius: 2px;
  outline: none;
}

.control input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 16px;
  height: 16px;
  background: var(--accent);
  border-radius: 50%;
  cursor: pointer;
  transition: transform 0.08s;
}

.control input[type="range"]::-webkit-slider-thumb:hover {
  transform: scale(1.15);
}

.control input[type="range"]::-moz-range-thumb {
  width: 16px;
  height: 16px;
  background: var(--accent);
  border: none;
  border-radius: 50%;
  cursor: pointer;
}

button {
  font-family: inherit;
  font-size: 0.92em;
  padding: 6px 16px;
  background: var(--bg);
  color: var(--text);
  border: 1px solid var(--panel-border);
  border-radius: 3px;
  cursor: pointer;
  transition: all 0.1s;
}

button:hover {
  background: var(--highlight);
  border-color: var(--accent-light);
}

button.primary {
  background: var(--accent);
  color: white;
  border-color: var(--accent);
}

button.primary:hover {
  background: #1f4870;
}

/* --- Navigation --- */

nav.toc {
  margin: 3em 0;
}

nav.toc ol {
  list-style: none;
  padding: 0;
  counter-reset: section;
}

nav.toc li {
  counter-increment: section;
  margin: 0.4em 0;
  padding: 0;
  display: flex;
  align-items: baseline;
  gap: 0.6em;
}

nav.toc li::before {
  content: counter(section, decimal-leading-zero);
  color: var(--text-faint);
  font-family: "SF Mono", "Menlo", "Consolas", monospace;
  font-size: 0.85em;
  min-width: 2em;
}

nav.toc a {
  border-bottom: none;
  flex: 1;
}

nav.toc a:hover {
  color: var(--alive);
}

nav.toc .status {
  font-size: 0.8em;
  color: var(--text-faint);
  font-style: italic;
}

.nav-links {
  display: flex;
  justify-content: space-between;
  margin: 4em 0 0;
  padding-top: 1.5em;
  border-top: 1px solid var(--rule);
  font-size: 0.95em;
}

.nav-links a {
  border-bottom: none;
}

.nav-links .prev::before { content: "← "; }
.nav-links .next::after { content: " →"; }
.nav-links .up { color: var(--text-soft); }

/* On mobile, stack the three links vertically so each has a comfortable
 * tap target and long chapter titles don't overflow. */
@media (max-width: 620px) {
  .nav-links {
    flex-direction: column;
    gap: 14px;
    padding-top: 1.2em;
    text-align: center;
  }
  .nav-links a {
    padding: 6px 10px;
    line-height: 1.35;
  }
  .nav-links .prev { text-align: left; }
  .nav-links .next { text-align: right; }
  .nav-links .up  { font-weight: 600; }
}

/* --- Callouts --- */

.aside {
  margin: 2em 0;
  padding: 16px 20px;
  background: var(--bg);
  border-left: 3px solid var(--accent-light);
  color: var(--text-soft);
  font-size: 0.95em;
}

.aside .label {
  font-size: 0.78em;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--accent);
  font-weight: 600;
  margin-right: 0.6em;
}

/* --- Math --- */

.katex { font-size: 1.05em; }
.katex-display { margin: 1.2em 0; }

/* --- Interactive equations --- */

.equation-block {
  margin: 2em 0;
  background: var(--bg);
  border: 1px solid var(--panel-border);
  border-radius: 4px;
  padding: 18px 24px 20px;
}

.eq-display {
  text-align: center;
  font-size: 1.4em;
  font-family: "Latin Modern Math", "STIX Two Math", "Cambria Math", serif;
  font-style: italic;
  padding: 0.4em 0 0.8em;
  line-height: 1.7;
  color: var(--text);
}

.eq-display sub, .eq-display sup { font-style: normal; font-size: 0.75em; }

.eq-symbol {
  display: inline-block;
  cursor: pointer;
  padding: 2px 4px;
  border-radius: 3px;
  transition: background 0.12s, box-shadow 0.12s;
  user-select: none;
}

.eq-symbol:hover {
  background: rgba(244, 228, 168, 0.55);
}

.eq-symbol.active {
  background: rgba(244, 228, 168, 0.9);
  box-shadow: 0 0 0 1px var(--accent-light);
}

.eq-symbol.state { color: #2a5f8f; }     /* dynamical variables */
.eq-symbol.param { color: #2d7d4f; }     /* parameters/constants */
.eq-symbol.struct { color: #d97236; }    /* sums, indices, topology */
.eq-symbol.op { color: #7d2d4f; }        /* functions, operators */

.eq-prompt {
  text-align: center;
  color: var(--text-faint);
  font-style: italic;
  font-size: 0.9em;
  margin: 0.4em 0 0;
}

.eq-detail {
  margin-top: 1.2em;
  padding: 14px 18px;
  background: var(--bg);
  border: 1px solid var(--panel-border);
  border-radius: 3px;
  min-height: 72px;
  font-size: 0.96em;
  display: none;
}

.eq-detail.visible { display: block; }

.eq-detail .name {
  font-weight: 600;
  font-size: 1.05em;
  margin-bottom: 2px;
  color: var(--text);
}

.eq-detail .pronounce {
  color: var(--text-soft);
  font-style: italic;
  font-size: 0.88em;
  margin-bottom: 8px;
}

.eq-detail .description {
  color: var(--text);
  line-height: 1.5;
}

.eq-detail .description em {
  color: var(--alive);
  font-style: italic;
}

.eq-howitworks {
  margin-top: 1.4em;
  padding-top: 1em;
  border-top: 1px dashed var(--panel-border);
  font-size: 0.98em;
  color: var(--text);
  line-height: 1.55;
}

.eq-howitworks .label {
  font-size: 0.72em;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--accent);
  font-weight: 700;
  margin-right: 0.5em;
}

/* --- Theme toggle --- */

.theme-toggle {
  position: fixed;
  top: 18px;
  right: 18px;
  z-index: 100;
  width: 38px;
  height: 38px;
  padding: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 18px;
  line-height: 1;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 50%;
  color: var(--text-soft);
  cursor: pointer;
  transition: transform 0.15s, background 0.15s, color 0.15s;
}
.theme-toggle:hover {
  background: var(--highlight);
  color: var(--text);
  transform: translateY(-1px);
}
.theme-toggle:focus-visible {
  outline: 2px solid var(--accent-light);
  outline-offset: 2px;
}

@media (max-width: 720px) {
  .theme-toggle { top: 12px; right: 12px; width: 34px; height: 34px; font-size: 16px; }
}

/* --- Responsive --- */

@media (max-width: 720px) {
  .page { padding: 32px 20px 96px; }
  figure { margin-left: -20px; margin-right: -20px; padding-left: 20px; padding-right: 20px; }
  h1 { font-size: 1.9em; }
  h2 { font-size: 1.35em; }
}

/* --- Theme-specific aesthetic tweaks ---
 * Dark is the default — these rules apply unless [data-theme="light"] overrides.
 */

/* eq-symbol hover/active tint: yellow highlight reads differently on each bg. */
.eq-symbol:hover {
  background: rgba(244, 228, 168, 0.18);
}
.eq-symbol.active {
  background: rgba(244, 228, 168, 0.28);
  box-shadow: 0 0 0 1px var(--accent-light);
}
:root[data-theme="light"] .eq-symbol:hover {
  background: rgba(244, 228, 168, 0.55);
}
:root[data-theme="light"] .eq-symbol.active {
  background: rgba(244, 228, 168, 0.9);
  box-shadow: 0 0 0 1px var(--accent-light);
}

/* Primary buttons: dark-mode accent is light, so flip the text colour. */
button.primary { color: #18160f; }
button.primary:hover { background: var(--accent-light); border-color: var(--accent-light); }
:root[data-theme="light"] button.primary { color: white; }
:root[data-theme="light"] button.primary:hover { background: #1f4870; border-color: #1f4870; }

/* Physics-coloured eq-symbol classes — brighten for dark bg by default. */
.eq-symbol.state  { color: #7fb0d9; }
.eq-symbol.param  { color: #6dc48f; }
.eq-symbol.struct { color: #efa57a; }
.eq-symbol.op     { color: #e091ac; }
:root[data-theme="light"] .eq-symbol.state  { color: #2a5f8f; }
:root[data-theme="light"] .eq-symbol.param  { color: #2d7d4f; }
:root[data-theme="light"] .eq-symbol.struct { color: #d97236; }
:root[data-theme="light"] .eq-symbol.op     { color: #7d2d4f; }
