/* Notes pages: a reading column with a sticky table of contents, and
   "figures" — interactive widgets with a debugger-style trace. Loaded after
   site.css; uses the same tokens. */

:root, :root[data-theme="night"] { --pos: #7fd1b9; --neg: #f28b82; --c3: #b39ddb; --c4: #9cc2ff; --c5: #e6c07b; }
:root[data-theme="day"]      { --pos: #1d8466; --neg: #c0392b; --c3: #6b4fbb; --c4: #1d58b8; --c5: #9a6a00; }
:root[data-theme="sepia"]    { --pos: #2f7a5f; --neg: #a8392b; --c3: #6e4a8a; --c4: #2d5a8a; --c5: #8a5a10; }
:root[data-theme="contrast"] { --pos: #00e6a0; --neg: #ff6b6b; --c3: #d0a8ff; --c4: #7cc4ff; --c5: #ffd60a; }

/* Reading progress */
.progress { position: fixed; top: 0; left: 0; height: 2px; width: 100%; z-index: 60; background: var(--accent); transform-origin: 0 50%; transform: scaleX(0); }

/* Layout */
.note { max-width: 76rem; margin: 0 auto; padding: 0 clamp(16px, 4vw, 56px); display: grid; gap: var(--space-5); }
.note-main { min-width: 0; }
@media (min-width: 1100px) {
  .note { grid-template-columns: 13rem minmax(0, 46rem); justify-content: center; gap: clamp(2.5rem, 5vw, 5rem); }
}

.note-head { padding: var(--space-6) 0 var(--space-4); }
.note-head h1 { font-size: clamp(2.1rem, 1.3rem + 3vw, 3.4rem); line-height: 1.08; margin-bottom: var(--space-3); }
.note-meta { font-family: var(--font-mono); font-size: .75rem; color: var(--muted); display: flex; flex-wrap: wrap; gap: .4rem 1.2rem; margin-bottom: var(--space-4); }
.lede { font-size: 1.15rem; color: var(--muted); max-width: var(--measure); }

/* Table of contents */
.toc { font-size: .85rem; }
.toc ol { list-style: none; margin: 0; padding: 0; counter-reset: toc; }
.toc li { counter-increment: toc; }
.toc a { display: grid; grid-template-columns: 1.8rem 1fr; padding: .3rem 0; color: var(--muted); text-decoration: none; line-height: 1.35; }
.toc a::before { content: counter(toc, decimal-leading-zero); font-family: var(--font-mono); font-size: .7rem; padding-top: .15rem; }
.toc a:hover { color: var(--text); }
.toc a.is-active { color: var(--accent); }
.toc .label { display: block; margin-bottom: var(--space-2); }
@media (min-width: 1100px) {
  .toc { position: sticky; top: 5rem; align-self: start; max-height: calc(100vh - 6rem); overflow-y: auto; padding-top: var(--space-6); }
}
@media (max-width: 1099px) {
  .toc { border: 1px solid var(--border); border-radius: 10px; padding: var(--space-3); background: color-mix(in srgb, var(--surface) 85%, transparent); }
  .toc ol { columns: 2; column-gap: 1.5rem; }
}
@media (max-width: 560px) { .toc ol { columns: 1; } }

/* Prose */
.note-main section { padding-top: var(--space-5); scroll-margin-top: 4.5rem; }
.note-main h2 { font-size: clamp(1.5rem, 1.2rem + 1vw, 2rem); margin: 0 0 var(--space-3); }
.note-main h2 .n { font-family: var(--font-mono); font-size: .8rem; color: var(--accent); display: block; margin-bottom: .3rem; letter-spacing: .04em; }
.note-main h3 { font-size: 1.1rem; margin: var(--space-4) 0 var(--space-2); }
.note-main p, .note-main ul, .note-main ol:not(.toc ol) { max-width: var(--measure); }
.note-main li { margin-bottom: .35rem; }
.note-main strong { font-weight: 650; }
.math { font-family: var(--font-serif); font-style: italic; white-space: nowrap; }
.eq { font-family: var(--font-mono); font-size: .88rem; background: var(--code-bg); border: 1px solid var(--border); border-radius: 8px; padding: .8rem 1rem; margin: 0 0 var(--space-3); overflow-x: auto; white-space: pre; line-height: 1.6; max-width: var(--measure); }

.callout { border-left: 2px solid var(--accent); padding: .3rem 0 .3rem 1rem; margin: var(--space-4) 0; max-width: var(--measure); }
.callout .label { display: block; margin-bottom: .2rem; }
.callout p { margin: 0; }
.aside { font-size: .92rem; color: var(--muted); }

/* Flow diagrams made of HTML boxes */
.flow { display: flex; flex-wrap: wrap; align-items: stretch; gap: .5rem; margin: var(--space-3) 0 var(--space-4); }
.flow > .box { flex: 1 1 8rem; border: 1px solid var(--border); border-radius: 8px; padding: .6rem .75rem; background: color-mix(in srgb, var(--surface) 85%, transparent); font-size: .88rem; line-height: 1.4; }
.flow > .box b { display: block; font-size: .95rem; }
.flow > .box small { color: var(--muted); }
.flow > .arrow { align-self: center; color: var(--accent); font-family: var(--font-mono); }
@media (max-width: 640px) { .flow { flex-direction: column; } .flow > .arrow { transform: rotate(90deg); align-self: center; } }

/* Figures (widgets) */
.fig { margin: var(--space-4) 0 var(--space-5); border: 1px solid var(--border); border-radius: 12px; background: color-mix(in srgb, var(--surface) 92%, transparent); overflow: hidden; }
.fig-head { display: flex; justify-content: space-between; align-items: baseline; gap: 1rem; padding: .6rem 1rem; border-bottom: 1px solid var(--border); font-family: var(--font-mono); font-size: .72rem; color: var(--muted); }
.fig-head b { color: var(--text); font-weight: 600; }
.fig-head .tag { color: var(--accent); white-space: nowrap; }
.fig-body { padding: 1rem; }
.fig-cap { padding: .6rem 1rem .8rem; font-size: .85rem; color: var(--muted); border-top: 1px solid var(--border); }
.fig noscript, .fig .nojs { display: block; padding: 1rem; color: var(--muted); font-size: .9rem; }
.fig svg { display: block; width: 100%; height: auto; overflow: visible; }
.fig svg text { font-family: var(--font-mono); }
.fig svg text:not([fill]) { fill: var(--text); }
.fig svg .muted { fill: var(--muted); }
.fig svg .axis { stroke: var(--border); stroke-width: 1; }
.fig svg .grid { stroke: var(--border); stroke-width: 1; opacity: .5; }

.split2 { display: grid; gap: 1rem; }
@media (min-width: 720px) { .split2 { grid-template-columns: 1.3fr 1fr; align-items: center; } }

/* Controls */
.controls { display: grid; grid-template-columns: repeat(auto-fill, minmax(11rem, 1fr)); gap: .5rem 1.2rem; margin-top: .8rem; }
.ctl { display: grid; grid-template-columns: auto 1fr auto; align-items: center; gap: .5rem; font-family: var(--font-mono); font-size: .75rem; color: var(--muted); }
.ctl output { color: var(--text); min-width: 3.2rem; text-align: right; }
.ctl input[type="range"] { width: 100%; accent-color: var(--accent); }
.row { display: flex; flex-wrap: wrap; gap: .4rem; align-items: center; margin-top: .8rem; }
.btn, .chip {
  font: 500 .75rem/1 var(--font-mono); color: var(--text); background: var(--bg);
  border: 1px solid var(--border); border-radius: 999px; padding: .5rem .8rem; cursor: pointer;
}
.btn:hover, .chip:hover { border-color: var(--muted); }
.btn.primary { border-color: var(--accent); color: var(--accent); }
.chip[aria-pressed="true"] { border-color: currentColor; background: color-mix(in srgb, currentColor 12%, var(--bg)); }
.btn:disabled { opacity: .45; cursor: default; }
.field { font: .9rem var(--font-mono); color: var(--text); background: var(--bg); border: 1px solid var(--border); border-radius: 8px; padding: .5rem .7rem; width: 100%; }

/* Debugger-style trace */
.trace {
  font-family: var(--font-mono); font-size: .76rem; line-height: 1.7;
  background: var(--code-bg); border-top: 1px solid var(--border);
  padding: .8rem 1rem; white-space: pre-wrap; overflow-x: auto; max-height: 17rem; overflow-y: auto; color: var(--text);
}
.trace .k { color: var(--muted); }
.trace .v { color: var(--accent); }
.trace .p { color: var(--pos); }
.trace .n { color: var(--neg); }

/* Stat readouts */
.stats { display: flex; flex-wrap: wrap; gap: .4rem 1.4rem; font-family: var(--font-mono); font-size: .75rem; color: var(--muted); margin-top: .6rem; }
.stats b { color: var(--text); font-weight: 600; }

/* Token chips and heat cells */
.tokens { display: flex; flex-wrap: wrap; gap: .35rem; }
.tok { font-family: var(--font-mono); font-size: .8rem; border: 1px solid var(--border); border-radius: 6px; padding: .3rem .5rem; background: var(--bg); cursor: pointer; color: var(--text); }
.tok small { color: var(--muted); margin-left: .35rem; }
.tok.is-on { border-color: var(--accent); color: var(--accent); }
.tok.dim { opacity: .4; }
.heat { display: grid; gap: 2px; font-family: var(--font-mono); font-size: .62rem; }
.heat > span { display: grid; place-items: center; min-height: 1.6rem; border-radius: 3px; color: var(--text); background: var(--surface-2); }
.heat .lbl { background: none; color: var(--muted); justify-items: end; padding-right: .3rem; }
.bars { display: grid; gap: .3rem; font-family: var(--font-mono); font-size: .75rem; }
.bar { display: grid; grid-template-columns: 7rem 1fr 3.5rem; align-items: center; gap: .6rem; }
.bar i { display: block; height: .7rem; border-radius: 3px; background: var(--accent); transition: width .35s cubic-bezier(.16, 1, .3, 1); }
.bar.good i { background: var(--pos); }
.bar.is-true span:first-child { color: var(--pos); }
.bar span:last-child { text-align: right; color: var(--muted); }

/* Glossary */
.gloss { display: grid; grid-template-columns: max-content 1fr; gap: .45rem 1.2rem; max-width: var(--measure); margin: var(--space-3) 0; }
.gloss dt { font-weight: 650; }
.gloss dd { margin: 0; color: var(--muted); }
@media (max-width: 560px) { .gloss { grid-template-columns: 1fr; } .gloss dd { margin-bottom: .5rem; } }

.note-foot { border-top: 1px solid var(--border); margin-top: var(--space-6); padding-top: var(--space-4); font-size: .9rem; color: var(--muted); max-width: var(--measure); }

/* Notes index */
.shelf-course { margin-top: var(--space-5); }
.shelf-course h2 { margin-top: 0; }

.js .fig .nojs { display: none; }
.note-head .flow > .box { flex: 1 1 4.5rem; text-align: center; padding: .45rem .5rem; }
.note-head .flow { gap: .35rem; }
@media (max-width: 640px) { .note-head .flow { flex-direction: row; } .note-head .flow > .arrow { transform: none; } .note-head .flow > .box { flex-basis: 30%; } }
mark { background: color-mix(in srgb, var(--accent) 30%, transparent); color: inherit; padding: 0 .15em; border-radius: 3px; }
@media (max-width: 900px) { .note .toc { order: 2; margin-top: var(--space-4); } }
