/* ============================================================
   The Bridge — shared stylesheet
   Enhanced CSS with modern typography, math cards, and refined themes.
   ============================================================ */

@import url('https://fonts.googleapis.com/css2?family=Fira+Code:wght@400;500&family=Inter:wght@400;500;600;700;800&family=Lora:ital,wght@0,400;0,500;0,600;1,400&display=swap');

:root {
  /* light theme (default) */
  --bg:            #faf9f6;
  --bg-raised:     #ffffff;
  --bg-subtle:     #f3efe6;
  --ink:           #1c1917;
  --ink-soft:      #44403c;
  --ink-faint:     #78716c;
  --rule:          #e7e5e4;
  --accent:        #c2410c;   /* rich terracotta accent */
  --accent-soft:   #ea580c;
  --accent-bg:     #fff7ed;
  --link:          #9a3412;
  --link-hover:    #7c2d12;
  --code-bg:       #f5f5f4;
  --math-bg:       #ffffff;
  --math-border:   #e2e8f0;
  --shadow-sm:     0 2px 6px rgba(0, 0, 0, 0.04);
  --shadow-md:     0 6px 16px rgba(0, 0, 0, 0.06);
  --fig-accent:    #c2410c;
  --fig-second:    #1d4ed8;
  --fig-plain:     #a8a29e;
  --max:           45rem;
  --serif: 'Lora', Georgia, 'Times New Roman', serif;
  --sans:  'Inter', system-ui, -apple-system, sans-serif;
  --mono:  'Fira Code', ui-monospace, monospace;
}

:root[data-theme="dark"] {
  --bg:            #0f172a;   /* deep slate dark theme */
  --bg-raised:     #1e293b;
  --bg-subtle:     #334155;
  --ink:           #f8fafc;
  --ink-soft:      #cbd5e1;
  --ink-faint:     #94a3b8;
  --rule:          #334155;
  --accent:        #fb923c;   /* vibrant warm amber */
  --accent-soft:   #fdba74;
  --accent-bg:     #291e19;
  --link:          #38bdf8;   /* bright sky link */
  --link-hover:    #7dd3fc;
  --code-bg:       #1e293b;
  --math-bg:       #182234;
  --math-border:   #334155;
  --shadow-sm:     0 2px 8px rgba(0, 0, 0, 0.3);
  --shadow-md:     0 8px 24px rgba(0, 0, 0, 0.4);
  --fig-accent:    #fb923c;
  --fig-second:    #38bdf8;
  --fig-plain:     #64748b;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg:            #0f172a;
    --bg-raised:     #1e293b;
    --bg-subtle:     #334155;
    --ink:           #f8fafc;
    --ink-soft:      #cbd5e1;
    --ink-faint:     #94a3b8;
    --rule:          #334155;
    --accent:        #fb923c;
    --accent-soft:   #fdba74;
    --accent-bg:     #291e19;
    --link:          #38bdf8;
    --link-hover:    #7dd3fc;
    --code-bg:       #1e293b;
    --math-bg:       #182234;
    --math-border:   #334155;
    --shadow-sm:     0 2px 8px rgba(0, 0, 0, 0.3);
    --shadow-md:     0 8px 24px rgba(0, 0, 0, 0.4);
    --fig-accent:    #fb923c;
    --fig-second:    #38bdf8;
    --fig-plain:     #64748b;
  }
}

* { box-sizing: border-box; }

html { -webkit-text-size-adjust: 100%; scroll-behavior: smooth; }

body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--serif);
  font-size: 1.125rem;
  line-height: 1.7;
  text-rendering: optimizeLegibility;
  -webkit-font-smoothing: antialiased;
  transition: background-color 0.2s ease, color 0.2s ease;
}

/* ---- layout ---- */
.wrap { max-width: var(--max); margin: 0 auto; padding: 0 1.5rem; }

.site-header {
  border-bottom: 1px solid var(--rule);
  background: color-mix(in srgb, var(--bg) 85%, transparent);
  position: sticky; top: 0; z-index: 100;
  backdrop-filter: saturate(1.8) blur(12px);
  -webkit-backdrop-filter: saturate(1.8) blur(12px);
}
.site-header .wrap {
  display: flex; align-items: center; gap: 1rem;
  padding-top: .85rem; padding-bottom: .85rem;
}
.brand {
  font-family: var(--sans);
  font-weight: 800; letter-spacing: .04em;
  font-size: 1rem; text-transform: uppercase;
  color: var(--ink); text-decoration: none; white-space: nowrap;
}
.brand span { color: var(--accent); }
.site-nav { margin-left: auto; display: flex; align-items: center; gap: 1.25rem; }
.site-nav a {
  font-family: var(--sans); font-size: .88rem; font-weight: 600;
  color: var(--ink-soft); text-decoration: none; transition: color .15s ease;
}
.site-nav a:hover { color: var(--accent); }

.theme-toggle {
  font-family: var(--sans); font-size: .82rem; font-weight: 600;
  background: var(--bg-raised); border: 1px solid var(--rule);
  color: var(--ink-soft); border-radius: 999px;
  padding: .35rem .85rem; cursor: pointer; line-height: 1;
  box-shadow: var(--shadow-sm); transition: all .15s ease;
}
.theme-toggle:hover { border-color: var(--accent); color: var(--ink); transform: translateY(-1px); }

main { padding: 3rem 0 5rem; }

/* ---- typography ---- */
h1, h2, h3 { font-family: var(--sans); line-height: 1.25; color: var(--ink); }
h1 { font-size: 2.25rem; font-weight: 800; letter-spacing: -.02em; margin: 0 0 .5rem; }
h2 { font-size: 1.45rem; font-weight: 700; margin: 2.8rem 0 .9rem; }
h3 { font-size: 1.15rem; font-weight: 700; margin: 2rem 0 .6rem; }

p { margin: 0 0 1.25rem; }
a { color: var(--link); text-decoration: underline; text-underline-offset: 3px; text-decoration-thickness: 1.5px; transition: color .15s ease; }
a:hover { color: var(--link-hover); }

strong { color: var(--ink); font-weight: 600; }
em { color: var(--ink-soft); }

blockquote {
  margin: 1.6rem 0; padding: .6rem 1.2rem;
  border-left: 4px solid var(--accent);
  background: var(--bg-raised); border-radius: 0 8px 8px 0;
  color: var(--ink-soft); font-style: italic; box-shadow: var(--shadow-sm);
}

code, kbd {
  font-family: var(--mono);
  font-size: .88em; background: var(--code-bg); padding: .15em .45em; border-radius: 5px;
  border: 1px solid var(--rule);
}
pre { background: var(--code-bg); padding: 1.1rem 1.3rem; border-radius: 10px; border: 1px solid var(--rule); overflow-x: auto; line-height: 1.5; box-shadow: var(--shadow-sm); }
pre code { background: none; padding: 0; border: none; font-size: .85rem; }

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

/* ---- math formula cards ---- */
.katex-display {
  overflow-x: auto; overflow-y: hidden;
  padding: 1.1rem 1.5rem;
  margin: 1.6rem 0;
  background: var(--math-bg);
  border: 1px solid var(--math-border);
  border-left: 4px solid var(--accent);
  border-radius: 12px;
  box-shadow: var(--shadow-sm);
  transition: box-shadow .2s ease;
}
.katex-display:hover {
  box-shadow: var(--shadow-md);
}
.katex {
  font-size: 1.08em;
}

/* ---- eyebrow / kicker ---- */
.kicker {
  font-family: var(--sans); font-size: .8rem; font-weight: 700;
  letter-spacing: .14em; text-transform: uppercase; color: var(--accent);
  margin: 0 0 .7rem;
}
.lede { font-size: 1.3rem; line-height: 1.55; color: var(--ink-soft); margin: 0 0 1.8rem; font-weight: 400; }

/* ---- table of contents (landing) ---- */
.part { margin: 2.8rem 0; }
.part-head { font-family: var(--sans); font-size: .85rem; font-weight: 800; letter-spacing: .14em; text-transform: uppercase; color: var(--accent); border-bottom: 2px solid var(--accent); padding-bottom: .4rem; margin-bottom: .4rem; display: inline-block; }
.part-intro { font-family: var(--sans); font-size: .95rem; line-height: 1.55; color: var(--ink-soft); margin: .6rem 0 1.2rem; max-width: 44rem; }
.toc { list-style: none; margin: 0; padding: 0; }
.toc li { border-bottom: 1px solid var(--rule); }
.toc a:hover { background: var(--bg-raised); box-shadow: var(--shadow-sm); }
.toc .n { font-family: var(--sans); font-size: .85rem; font-weight: 700; color: var(--accent); margin-right: .75rem; }

/* ---- prev/next footer nav ---- */
.chapter-nav {
  display: flex; justify-content: space-between; gap: 1rem;
  margin-top: 3.5rem; padding-top: 1.6rem; border-top: 1px solid var(--rule);
  font-family: var(--sans); font-size: .92rem;
}
.chapter-nav a { text-decoration: none; color: var(--ink-soft); max-width: 45%; font-weight: 600; padding: .5rem .8rem; border-radius: 8px; border: 1px solid transparent; transition: all .15s ease; }
.chapter-nav a:hover { color: var(--accent); background: var(--bg-raised); border-color: var(--rule); box-shadow: var(--shadow-sm); }
.chapter-nav .dir { display: block; font-size: .72rem; letter-spacing: .1em; text-transform: uppercase; color: var(--ink-faint); margin-bottom: .2rem; }
.chapter-nav .next { text-align: right; margin-left: auto; }

.site-footer { border-top: 1px solid var(--rule); color: var(--ink-faint); font-family: var(--sans); font-size: .85rem; }
.site-footer .wrap { padding: 2rem 1.5rem 3rem; }

img, svg, table { max-width: 100%; }

/* ---- responsive ---- */
@media (max-width: 34rem) {
  body { font-size: 1.05rem; }
  h1 { font-size: 1.8rem; }
  .lede { font-size: 1.15rem; }
}

/* ---- print ---- */
@media print {
  :root, :root[data-theme], :root:not([data-theme="light"]) { --bg:#fff; --bg-raised:#fff; --ink:#000; --ink-soft:#222; --ink-faint:#555; --rule:#ccc; --code-bg:#f5f5f5; --link:#000; --math-bg:#fff; --math-border:#ccc; }
  .site-header, .theme-toggle, .site-nav, .chapter-nav, .site-footer { display: none !important; }
  body { font-size: 11pt; color: #000; background: #fff; }
  main { padding: 0; }
  a { color: #000; text-decoration: none; }
}

/* Reading, worked answers, and keyboard navigation. */
h2[id] { scroll-margin-top: 6rem; }
.skip-link { position: absolute; top: -5rem; left: 1rem; padding: .6rem 1rem; background: var(--bg-raised); z-index: 20; }
.skip-link:focus { top: .5rem; }
:focus-visible { outline: 3px solid var(--accent); outline-offset: 4px; }
.prerequisites { font-family: var(--sans); font-size: .9rem; padding: 1rem; background: var(--code-bg); }
.on-this-page { margin: 1.8rem 0 2.2rem; border-block: 1px solid var(--rule); padding: 1rem 0; font-family: var(--sans); font-size: .9rem; }
.on-this-page p { margin-bottom: .4rem; font-weight: 700; }
.on-this-page ol { margin: 0; padding-left: 1.4rem; }
.on-this-page li { margin: .25rem 0; }
.chapter-description { display: block; margin: .35rem 0 0 2rem; font-size: .95rem; color: var(--ink-soft); }
.start-link { display: inline-block; font-family: var(--sans); font-weight: 650; margin: .3rem 0; }
details { border: 1px solid var(--rule); border-radius: 5px; padding: .8rem 1rem; margin: -.3rem 0 1.5rem; background: var(--bg-raised); }
summary { font-family: var(--sans); color: var(--link); cursor: pointer; font-size: .95rem; }
details[open] summary { margin-bottom: .8rem; }
details p:last-child { margin-bottom: 0; }
.table-scroll { overflow-x: auto; margin: 1.3rem 0; }
table { width: 100%; border-collapse: collapse; font-size: .94rem; }
caption { text-align: left; font-family: var(--sans); font-weight: 650; margin-bottom: .5rem; }
th, td { padding: .6rem .7rem; text-align: left; border-bottom: 1px solid var(--rule); vertical-align: top; }
th { font-family: var(--sans); font-size: .86rem; }
.references { border-top: 1px solid var(--rule); margin-top: 2rem; font-size: .95rem; color: var(--ink-soft); }
@media (max-width: 34rem) {
  .site-header .wrap { flex-wrap: wrap; gap: .6rem; }
  .site-nav { gap: .7rem; }
  .chapter-description { margin-left: 0; }
}
@media print {
  .on-this-page, .skip-link { display: none; }
  details { break-inside: avoid; }
  details > * { display: block !important; }
  .katex-display { overflow: visible; }
}

/* Keep wide derivations aligned at their readable starting edge. */
.katex-display > .katex { text-align: left; }
.skip-link { z-index: 200; }
@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after { transition: none !important; }
}
@media print {
  .katex-display { box-shadow: none; padding: .5rem; border-left-width: 1px; }
}


/* ---- figures (generated by scripts/figures/) and optional derivations ---- */
figure { margin: 2rem 0; }
.fig-scroll { overflow-x: auto; background: var(--bg-raised); border: 1px solid var(--rule); border-radius: 10px; padding: .9rem; }
.fig-svg { display: block; width: 100%; height: auto; }
figcaption { font-family: var(--sans); font-size: .9rem; line-height: 1.55; color: var(--ink-soft); margin-top: .7rem; }
.fig-svg text { font-family: var(--sans); font-size: 13px; fill: var(--ink); }
.fig-svg .fig-head { font-weight: 700; font-size: 13px; }
.fig-svg .fig-strong { font-weight: 700; }
.fig-svg .fig-soft { fill: var(--ink-soft); }
.fig-svg .fig-italic { font-style: italic; font-family: var(--serif); }
/* Kets need the angle bracket, which the sans face lacks; KaTeX's text face has it. */
.fig-svg .fig-math { font-family: KaTeX_Main, 'Times New Roman', serif; font-size: 15px; font-weight: 400; }
.fig-rule { stroke: var(--rule); stroke-width: 1; }
.fig-axis { stroke: var(--ink-faint); stroke-width: 1; }
.fig-outline { fill: none; stroke: var(--ink-faint); stroke-width: 1.2; }
.fig-line { fill: none; stroke: var(--ink-faint); stroke-width: 1; }
.fig-dashed { stroke-dasharray: 4 4; }
.fig-vector { stroke: var(--fig-accent); stroke-width: 2; }
.fig-vector-plain { stroke: var(--ink-soft); stroke-width: 1.6; }
.fig-branch { stroke: var(--ink-soft); stroke-width: 1.4; }
.fig-node { fill: var(--bg-raised); stroke: var(--ink-soft); stroke-width: 1.2; }
.fig-svg .fig-accent-text { fill: var(--fig-accent); font-weight: 700; }
/* Narrow figures are drawn to fit a phone column, so they never need sideways scrolling. */
figure.fig-narrow .fig-scroll { max-width: 30rem; margin: 0 auto; }
.fig-chord { stroke: var(--ink-soft); stroke-width: 1.4; }
.fig-chord-alt { stroke: var(--fig-second); stroke-dasharray: 5 3; }
.fig-dot.fig-dot-mixed { fill: var(--bg-raised); stroke: var(--fig-accent); stroke-width: 2.5; }
.fig-wire { stroke: var(--ink-soft); stroke-width: 1.5; fill: none; }
.fig-gate { fill: var(--bg-raised); stroke: var(--ink); stroke-width: 1.5; }
.fig-control { fill: var(--ink); }
.fig-target { fill: none; stroke: var(--ink-soft); stroke-width: 1.5; }
.fig-trace { fill: none; stroke: var(--fig-accent); stroke-width: 2.2; }
.fig-trace-alt { stroke: var(--fig-second); stroke-dasharray: 6 4; }
.fig-faint { stroke-opacity: .5; }
.fig-dot.fig-dot-alt { fill: var(--fig-second); }
.fig-svg .fig-alt-text { fill: var(--fig-second); }
.fig-bar-alt { fill: var(--fig-second); }
.fig-cell { fill: var(--bg-raised); stroke: var(--rule); stroke-width: 1; }
.fig-cell-on { fill: var(--accent-bg); stroke: var(--fig-accent); }
.fig-vector-alt { stroke: var(--fig-second); }
.fig-band { fill: var(--fig-plain); fill-opacity: .18; }
.fig-zone { fill: var(--accent-bg); stroke: none; }
.fig-zone-alt { fill: var(--bg-subtle); fill-opacity: .6; }
.fig-bell { fill: none; stroke: var(--fig-second); stroke-width: 1.6; stroke-dasharray: 4 3; }
.fig-classical { stroke: var(--ink-soft); stroke-width: 1.2; fill: none; }
.fig-image { fill: var(--fig-accent); fill-opacity: .12; stroke: var(--fig-accent); stroke-width: 1.8; }
.fig-image-alt { fill: var(--fig-second); stroke: var(--fig-second); stroke-dasharray: 5 3; }
.fig-bar-outline { fill: none; stroke: var(--fig-accent); stroke-width: 1.6; }
.fig-bar { fill: var(--fig-accent); }
.fig-bar-plain { fill: var(--fig-plain); }
.fig-dot { fill: var(--ink); stroke: var(--bg-raised); stroke-width: 1.5; }
.fig-dot-back { fill: var(--bg-raised); stroke: var(--ink-faint); }
.fig-dot-accent { fill: var(--fig-accent); }
details.more { border-style: dashed; margin: .2rem 0 1.5rem; }
details.more summary::after { content: " (optional)"; color: var(--ink-faint); }
@media (max-width: 40rem) {
  .fig-svg { min-width: 620px; }
  figure.fig-narrow .fig-svg { min-width: 0; }
  .fig-scroll::after { content: "Scroll sideways to see the whole figure."; display: block; font-family: var(--sans); font-size: .78rem; color: var(--ink-faint); margin-top: .4rem; position: sticky; left: 0; }
  figure.fig-narrow .fig-scroll::after { display: none; }
}
@media print {
  :root, :root[data-theme], :root:not([data-theme="light"]) { --fig-accent:#b45309; --fig-second:#1d4ed8; --fig-plain:#999; }
  figure { break-inside: avoid; }
  .fig-scroll { overflow: visible; border-color: #ccc; }
  .fig-scroll::after { display: none; }
  details::details-content { content-visibility: visible; }
}
/* Contents entries: the whole entry is a link, so underline only the title. */
.toc a { display: block; text-decoration: none; color: var(--ink); padding: .7rem .3rem; border-radius: 6px; }
.toc a strong { color: var(--link); text-decoration: underline; text-underline-offset: 3px; text-decoration-thickness: 1.5px; }
.toc a .chapter-description { color: var(--ink-soft); }
