/*
  quirky.css: a thin layer over latex.css.

  latex.css (loaded first, from a CDN) does the typesetting: Latin Modern, spacing,
  booktabs tables, dark mode, and the .theorem / .lemma / .definition / .proof blocks
  with their numbering. This file adds two fonts (Caveat for handwriting, Courier Prime
  for typewriter), the pens below, and the few blocks and props of our own.
  The hand-drawn boxes, brackets, underlines and highlights are drawn by quirky.js.
*/

:root {
  --hand: "Caveat", "Segoe Print", cursive;
  --typewriter: "Courier Prime", "Courier New", monospace;
  --red-pen: #d23a2f;
  --blue-pen: #2851c6;
  --pencil: #7c7a82;
  --sticky: #fff2a6;
  --sticky-ink: #3b3412;
  --tape: rgba(222, 214, 184, .75);
  --highlighter: rgba(255, 221, 51, .55);
  --wobbly: 255px 15px 225px 15px / 15px 225px 15px 255px;   /* a hand-drawn border-radius */
}
:root.latex-dark {
  --red-pen: #ff7a6b;
  --blue-pen: #8fb0ff;
  --pencil: #a29d92;
  --sticky: #4a4220;
  --sticky-ink: #f6eebf;
  --tape: rgba(140, 132, 108, .6);
  --highlighter: rgba(255, 214, 10, .3);
}

/* latex.css resets its counters on <body>; ours must be listed in the same rule. */
body { counter-reset: theorem definition sidenote-counter worked exercise; }

/* ── The strip above each page: name … [date stamp] [links] [light/dark] ── */
#preamble { display: flex; align-items: center; gap: 1rem; margin-bottom: 2.5rem; }
.site-name {
  margin-right: auto;                       /* pushes everything after it to the right */
  font-family: var(--hand); font-weight: 700; font-size: 1.9rem;
  color: inherit !important; text-decoration: none;
}
.stamp {
  font-family: var(--typewriter); font-weight: 700; font-size: .78rem; letter-spacing: .12em;
  text-transform: uppercase; color: var(--red-pen);
  border: 2px solid currentColor; border-radius: 5px; padding: 0 .45rem; transform: rotate(-3deg);
}
.stamp:empty { display: none; }
.site-nav { display: flex; gap: 1.1rem; }
.site-nav a {
  font-family: var(--hand); font-size: 1.35rem; line-height: 1;
  color: var(--pencil) !important; text-decoration: none; transition: color .15s ease;
}
.site-nav a:hover, .site-nav a:focus-visible, .site-nav a[aria-current] { color: inherit !important; }
.theme-toggle {
  width: 2.2rem; height: 2rem; padding: 0; cursor: pointer;
  font-size: 1.1rem; line-height: 1; color: inherit; background: none;
  border: 1.5px solid var(--pencil); border-radius: var(--wobbly);
}
.theme-toggle:hover { border-color: currentColor; transform: rotate(-6deg); }
/* The icon shows where a click takes you: a moon in light mode, a sun in dark. */
.theme-toggle::before { content: "☾"; font-family: system-ui, sans-serif; }
.latex-dark .theme-toggle::before { content: "☀"; }

/* ── Titles ── */
.subtitle {
  font-family: var(--hand); font-size: 1.5rem; line-height: 1.25;
  color: var(--blue-pen); text-align: center; margin-top: .3rem;
}
h2::before { content: "§ "; font-family: var(--hand); color: var(--red-pen); }

/* ── Blocks. The label is ::before, so the first paragraph runs in after it. ── */
:is(.theorem, .lemma, .definition, .proof, .worked, .exercise) > p:first-child { display: inline; }
:is(.theorem, .lemma, .definition, .proof, .worked, .exercise) > p + p { margin-top: .6em; }
/* A title from #+attr_html: :title … */
.theorem[title]::before    { content: "Theorem " counter(theorem) " (" attr(title) "). "; }
.lemma[title]::before      { content: "Lemma " counter(theorem) " (" attr(title) "). "; }
.definition[title]::before { content: "Definition " counter(definition) " (" attr(title) "). "; }

/* worked: a numbered example on a red margin line. */
.worked { counter-increment: worked; margin: 1.4em 0; padding-left: 1.2rem; border-left: 2px solid var(--red-pen); }
.worked::before { content: "Example " counter(worked) ". "; font-family: var(--hand); font-size: 1.35em; color: var(--red-pen); }
.worked[title]::before { content: "Example " counter(worked) " (" attr(title) "). "; }

/* exercise: numbered; quirky.js brackets it in pencil. */
.exercise { counter-increment: exercise; margin: 1.4em 0; }
.exercise::before { content: "Exercise " counter(exercise) ". "; font-family: var(--hand);
                    font-size: 1.35em; font-weight: 700; color: var(--red-pen); }

/* note and warning: a handwritten label; quirky.js adds a bracket or a box. */
.note, .warning { margin: 1.6em 0; padding: .2rem 1rem; }
.note::before, .warning::before { display: block; font-family: var(--hand); font-size: 1.4em; font-weight: 700; }
.note::before { content: "Note"; color: var(--blue-pen); }
.warning::before { content: "Careful!"; color: var(--red-pen); }

/* tip: a sticky note, taped on. */
.tip {
  position: relative; margin: 2em 0; padding: 1.1rem 1.3rem .9rem; max-width: 34rem;
  background: var(--sticky); color: var(--sticky-ink);
  box-shadow: 0 6px 14px -6px rgba(0, 0, 0, .35); transform: rotate(-.6deg);
}
.tip::before { content: "Tip"; display: block; font-family: var(--hand); font-size: 1.4em; font-weight: 700; }
.tip::after {
  content: ""; position: absolute; top: -.6rem; left: 50%; width: 6rem; height: 1.3rem;
  margin-left: -3rem; background: var(--tape); transform: rotate(-3deg);
}
.tip a { color: inherit; }
:is(.note, .warning, .tip)[title]::before { content: attr(title); }

/* aside: a margin note on wide screens, a pencilled line in the text otherwise. */
aside {
  font-family: var(--hand); font-size: 1.25rem; line-height: 1.3; color: var(--pencil);
  margin: 1em 0; padding-left: .8rem; border-left: 2px solid var(--sticky);
}
aside p { margin: 0; }
@media (min-width: 72rem) {
  aside { float: right; clear: right; width: 13rem; margin: 0 -16rem 1em 0; }
}

/* details/summary: folds (solutions, the shelf's "want to" lists). */
details { margin: 1.2em 0; padding: .5rem 1rem; border: 1.5px dashed var(--pencil); border-radius: 6px; }
details > summary { cursor: pointer; font-family: var(--hand); font-size: 1.35em; color: var(--red-pen); }
details > summary p { display: inline; margin: 0; }
details[open] > summary { margin-bottom: .5rem; }
details ul { columns: 2 14rem; column-gap: 2rem; margin: .4rem 0 .2rem; }
details li { break-inside: avoid; }

blockquote.epigraph { margin-left: auto; max-width: 26rem; font-style: italic; }

/* The post list (the sitemap): dates in red handwriting. */
.content li i { font-family: var(--hand); font-style: normal; font-size: 1.2em; color: var(--red-pen); margin-left: .4em; }

/* ── Tables and maths ── */
/* latex.css numbers tables and figures itself ("Table 1."), so hide Org's own numbers. */
.table-number, .figure-number, .listing-number { display: none; }
/* A short caption may run wider than its table instead of wrapping. */
caption { width: max-content; max-width: min(36rem, calc(100vw - 2.5rem)); }
table.payoff { margin-inline: auto; }
table.payoff td, table.payoff th { border: 1.36px solid var(--table-border-color); text-align: center; }
table.payoff td:first-child { font-family: var(--hand); font-size: 1.3em; color: var(--red-pen); }
/* Wide display maths scrolls sideways instead of widening the page. */
mjx-container[display="true"] { overflow-x: auto; overflow-y: hidden; }

/* ── Code: typewriter on graph paper, comments handwritten ── */
pre, code, kbd { font-family: var(--typewriter); }
pre.src {
  padding: 1rem 1.2rem; border-radius: 6px; line-height: 1.5rem;
  background-color: var(--pre-bg-color);
  background-image: linear-gradient(rgba(40, 81, 198, .08) 1px, transparent 1px),
                    linear-gradient(90deg, rgba(40, 81, 198, .08) 1px, transparent 1px);
  background-size: 1.5rem 1.5rem;
}
/* At text size, so comments don't widen lines on their own. */
.hljs-comment { font-family: var(--hand); font-size: 1.15em; line-height: 1; font-style: normal; color: var(--blue-pen); }
.org-src-name { font-family: var(--hand); font-size: 1.25rem; color: var(--blue-pen); }
/* The "widen" button (quirky.js): the block breaks out of the text column, up to 120
   characters or the window's width. Narrow screens have nothing to widen. */
.org-src-container { position: relative; }
.org-src-container.wide { width: min(calc(100vw - 2.5rem), 120ch); left: 50%; transform: translateX(-50%); }
.code-toggle {
  display: block; margin: 0 0 .25rem auto; padding: .05rem .5rem;
  font-family: var(--hand); font-size: 1.05rem; line-height: 1.2;
  color: var(--blue-pen); background: none; cursor: pointer; opacity: .75;
  border: 1.5px solid currentColor; border-radius: var(--wobbly);
}
.code-toggle:hover, .code-toggle:focus-visible { opacity: 1; }
@media (max-width: 52rem) { .code-toggle { display: none; } }

/* ── The shelf (content/shelf.org, written by shelf.el): covers pinned up a little crooked.
   Every card lifts on hover; linked titles also get a red underline and ↗. ── */
ul.shelf {                                   /* ul.x, to beat latex.css's ul[class] { margin: 0 } */
  list-style: none; padding: 0; margin: 1.5rem 0 2.2rem;
  display: grid; grid-template-columns: repeat(auto-fill, minmax(8rem, 1fr)); gap: 2rem 1.4rem;
}
.shelf > li { margin: 0; display: flex; flex-direction: column; font-size: .92rem; line-height: 1.3; }
.shelf .cover {
  position: relative; display: block; aspect-ratio: 2 / 3; margin-bottom: .6rem;
  color: inherit; text-decoration: none; rotate: -1.2deg;
  box-shadow: 0 1px 2px rgba(0,0,0,.18), 0 4px 10px rgba(0,0,0,.12);
  transition: rotate .2s ease, translate .2s ease, box-shadow .2s ease;
}
.shelf.square .cover { aspect-ratio: 1; }
.shelf > li:nth-child(3n+2) .cover { rotate: .9deg; }
.shelf > li:nth-child(3n) .cover { rotate: -.4deg; }
.shelf > li:is(:hover, :focus-within) .cover {
  rotate: 0deg; translate: 0 -4px;
  box-shadow: 0 2px 4px rgba(0,0,0,.18), 0 10px 22px rgba(0,0,0,.2);
}
.shelf .cover img { display: block; width: 100%; height: 100%; object-fit: cover; }
.shelf .blank {                              /* no cover image: a hand-labelled card */
  display: flex; align-items: center; justify-content: center; padding: .7rem; text-align: center;
  background: var(--sticky); color: var(--sticky-ink);
  font-family: var(--hand); font-size: 1.3rem; line-height: 1.1;
}
.shelf .kind {                               /* "book", "film"… where kinds mix */
  position: absolute; top: -.55rem; left: -.45rem; padding: 0 .4rem;
  font-family: var(--hand); font-size: 1rem; line-height: 1.35;
  background: var(--tape); transform: rotate(-5deg);
}
.shelf .name { font-size: 1rem; line-height: 1.25; }
.shelf .name a {
  color: inherit; text-decoration: underline transparent; text-decoration-thickness: 1.5px;
  text-underline-offset: 2px; transition: text-decoration-color .2s ease;
}
.shelf .name a::after { content: "\202F↗"; font-size: .78em; color: var(--pencil); }
.shelf > li:hover .name a, .shelf .name a:focus-visible { text-decoration-color: var(--red-pen); }
.shelf .by, .shelf .meta {
  display: block; margin-top: .2rem; font-family: var(--typewriter); font-size: .74rem; color: var(--pencil);
}
.shelf .by { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.shelf .stars { color: var(--red-pen); letter-spacing: .03em; margin-right: .4rem; }
.shelf .stars, .shelf .when { white-space: nowrap; }
.shelf .said { display: block; margin-top: .35rem; font-family: var(--hand); font-size: 1.12rem; line-height: 1.15; color: var(--blue-pen); }
.shelf .said::before { content: "“"; }
.shelf .said::after { content: "”"; }
@media (max-width: 30rem) { ul.shelf { grid-template-columns: repeat(2, 1fr); gap: 1.6rem 1rem; } }

/* ── Motion and print ── */
@media (prefers-reduced-motion: reduce) { .shelf .cover, .shelf .name a, .site-nav a { transition: none; } }
@media print {
  #preamble { display: none; }
  .tip { transform: none; box-shadow: none; }
  aside { float: none; width: auto; margin: 1em 0; }
}
