/* ------------------------------------------------------------------ */
/* Tokens                                                              */
/* ------------------------------------------------------------------ */

:root {
  --paper: #f5f1e8;
  --paper-deep: #ece5d6;
  --ink: #1d1b17;
  --ink-2: #57524a;
  --ink-3: #8f887c;
  --line: rgba(29, 27, 23, 0.1);
  --line-2: rgba(29, 27, 23, 0.18);

  --red: #c4442f;
  --red-soft: rgba(196, 68, 47, 0.1);
  --green: #2f7a58;
  --blue: #2b5a8a;

  --tile-face: #fbf8f1;
  --tile-face-lo: #f4eee1;
  --tile-edge: #e4dccb;
  --tile-back: #3f7c68;
  --tile-back-hi: #4a8a75;
  --tile-back-edge: #31634f;

  --serif: "Fraunces", "Iowan Old Style", "Palatino", Georgia, serif;
  --kanji: "Shippori Mincho B1", "Hiragino Mincho ProN", "Yu Mincho", "Noto Serif JP", serif;
  --sans: ui-sans-serif, system-ui, -apple-system, "Segoe UI", sans-serif;

  --ease: cubic-bezier(0.2, 0.7, 0.2, 1);
  --ease-soft: cubic-bezier(0.45, 0, 0.2, 1);

  /* one knob for every ambient motion — 0 is still, 1 is default */
  --alive: 1;

  --gutter: 16px;
}

/* the paper of the chapter you're in; drifts slowly between chapters */
@property --paper-now {
  syntax: "<color>";
  inherits: true;
  initial-value: #f5f1e8;
}

/* ------------------------------------------------------------------ */
/* Reset + type                                                        */
/* ------------------------------------------------------------------ */

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

html {
  -webkit-text-size-adjust: 100%;
  scroll-behavior: smooth;
  transition: --paper-now 2.4s var(--ease-soft);
}

body {
  margin: 0;
  background: var(--paper-now);
  color: var(--ink);
  font-family: var(--serif);
  font-size: 19px;
  line-height: 1.55;
  font-optical-sizing: auto;
  font-variation-settings: "SOFT" 50, "WONK" 0;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  overflow-x: hidden;
}

::selection { background: var(--red-soft); }

h1, h2, h3, h4, p { margin: 0; font-weight: inherit; }
button { font: inherit; color: inherit; }
a { color: inherit; }

.kanji, .jp { font-family: var(--kanji); font-weight: 700; }

.label {
  font: 500 11px/1.2 var(--sans);
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--ink-3);
}

.lead {
  font-size: clamp(27px, 4.4vw, 42px);
  line-height: 1.16;
  font-weight: 330;
  letter-spacing: -0.012em;
  font-variation-settings: "SOFT" 100, "WONK" 0;
  max-width: 19em;
  text-wrap: balance;
}

.title {
  font-size: clamp(30px, 4.6vw, 44px);
  line-height: 1.1;
  font-weight: 320;
  letter-spacing: -0.012em;
  font-variation-settings: "SOFT" 100, "WONK" 0;
}
.title .jp { font-size: 0.5em; color: var(--ink-3); margin-left: 0.5em; letter-spacing: 0.08em; vertical-align: 0.2em; }

.body {
  color: var(--ink-2);
  max-width: 31em;
  text-wrap: pretty;
}
.body + .body { margin-top: -6px; }
.body em, .lead em { font-style: italic; }
.body b, .lead b { font-weight: 560; color: var(--ink); }
.small { font-size: 15px; color: var(--ink-3); }
.term { font-style: italic; }
.term .jp { font-style: normal; font-size: 0.86em; color: var(--ink-3); margin-left: 0.15em; }

/* ------------------------------------------------------------------ */
/* Pages                                                               */
/* ------------------------------------------------------------------ */

main { position: relative; z-index: 1; }

.page {
  display: grid;
  align-content: center;
  justify-items: center;
  gap: 26px;
  padding: var(--page-pad, clamp(44px, 8vh, 88px)) var(--gutter);
  text-align: center;
}

.stack { display: grid; gap: 14px; justify-items: center; }

/* hero */
.hero { min-height: 100svh; position: relative; }
.hero-name {
  font-size: clamp(17px, 1.9vw, 21px);
  font-weight: 380;
  letter-spacing: 0.02em;
  color: var(--ink-2);
}
.hero-sub { display: block; margin-top: 3px; font-size: 0.62em; font-weight: 300; font-style: italic; opacity: 0.55; }

kbd {
  display: inline-block;
  font: 500 10px/1 var(--sans);
  font-style: normal;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--ink-2);
  padding: 4px 9px 5px;
  margin: 0 0.15em;
  border-radius: 6px;
  vertical-align: 0.12em;
  box-shadow: inset 0 0 0 1px var(--line-2), 0 1.5px 0 var(--line-2);
  background: rgba(255, 255, 255, 0.35);
}

/* say "space" to keyboards, "scroll" to fingers */
.touch-only { display: none; }
@media (hover: none), (pointer: coarse) {
  .keys-only { display: none; }
  .touch-only { display: inline; }
}
/* headings soften and firm up, slowly, like breathing */
.chapter-title { animation: soften 16s ease-in-out infinite alternate; }
@keyframes soften {
  from { font-variation-settings: "SOFT" 100, "WONK" 1; }
  to { font-variation-settings: "SOFT" 30, "WONK" 1; }
}

.scroll-cue {
  position: absolute;
  bottom: 5vh;
  left: 50%;
  translate: -50% 0;
  display: grid;
  justify-items: center;
  gap: 10px;
}
.scroll-cue kbd { padding: 6px 16px 7px; animation: press 4.8s var(--ease-soft) infinite; }
@keyframes press {
  0%, 82%, 100% { translate: 0 0; box-shadow: inset 0 0 0 1px var(--line-2), 0 1.5px 0 var(--line-2); }
  88% { translate: 0 1.5px; box-shadow: inset 0 0 0 1px var(--line-2), 0 0 0 var(--line-2); }
}
.scroll-cue i {
  width: 1px;
  height: 46px;
  background: linear-gradient(var(--ink-3), transparent);
  transform-origin: top;
  animation: cue 3.6s var(--ease-soft) infinite;
}
@keyframes cue {
  0% { transform: scaleY(0); opacity: 1; }
  60% { transform: scaleY(1); opacity: 1; }
  100% { transform: scaleY(1); opacity: 0; }
}

/* chapter openers */
.chapter { min-height: 100svh; gap: 10px; }
.chapter-num {
  font-family: var(--kanji);
  font-weight: 700;
  font-size: clamp(84px, 13vw, 140px);
  line-height: 1;
  color: var(--ink);
  margin-bottom: 18px;
}
.chapter-title {
  font-size: clamp(42px, 7vw, 80px);
  line-height: 1;
  font-weight: 290;
  letter-spacing: -0.025em;
  font-variation-settings: "SOFT" 100, "WONK" 1;
}
.chapter .label { margin-top: 18px; }

/* a quiet divider between ideas */
.rule { width: 28px; height: 1px; background: var(--line-2); border: 0; margin: 0 auto; }

/* ------------------------------------------------------------------ */
/* Top bar                                                             */
/* ------------------------------------------------------------------ */

.bar {
  position: fixed;
  inset: 0 0 auto 0;
  z-index: 20;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 14px var(--gutter);
  pointer-events: none;
}
.bar > * { pointer-events: auto; }
.bar::before {
  content: "";
  position: absolute;
  inset: 0 0 -26px 0;
  z-index: -1;
  pointer-events: none;
  background: linear-gradient(color-mix(in srgb, var(--paper-now) 78%, transparent) 30%, transparent);
  -webkit-backdrop-filter: blur(8px);
  backdrop-filter: blur(8px);
  -webkit-mask-image: linear-gradient(#000 45%, transparent);
  mask-image: linear-gradient(#000 45%, transparent);
}
.bar-mark {
  font-family: var(--kanji);
  font-weight: 700;
  font-size: 16px;
  letter-spacing: 0.12em;
  text-decoration: none;
}
.bar-chapter {
  position: absolute;
  left: 50%;
  translate: -50% 0;
  font: 500 11px/1 var(--sans);
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--ink-3);
  transition: opacity 0.6s var(--ease);
  white-space: nowrap;
}
.bar-chapter .jp { font-size: 13px; letter-spacing: 0; margin-right: 0.6em; color: var(--ink-2); }
.bar-right { display: flex; align-items: center; gap: 14px; }
.hint-toggle {
  border: 1px solid var(--line-2);
  background: transparent;
  border-radius: 999px;
  font: 500 10px/1 var(--sans);
  letter-spacing: 0.1em;
  padding: 6px 9px;
  color: var(--ink-3);
  cursor: pointer;
  transition: color 0.3s, border-color 0.3s, background 0.3s;
}
.hint-toggle[aria-pressed="true"] { color: var(--ink); border-color: var(--ink-3); background: rgba(255, 255, 255, 0.4); }
.lab-mark {
  font: 500 10px/1 var(--sans);
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--red);
  text-decoration: none;
  opacity: 0.7;
}
.lab-mark:hover { opacity: 1; }
.sound-toggle,
.table-link {
  display: grid;
  place-items: center;
  width: 28px;
  height: 24px;
  padding: 0;
  border: 0;
  background: none;
  color: var(--ink-3);
  cursor: pointer;
  transition: color 0.3s;
}
.sound-toggle:hover,
.table-link:hover { color: var(--ink); }
.sound-toggle svg,
.table-link svg { width: 16px; height: 16px; overflow: visible; }
/* the square: off to the table — and, at the table, filled, back to the book */
.table-link rect { fill: currentColor; fill-opacity: 0.12; stroke: currentColor; stroke-width: 1.2; transition: fill-opacity 0.3s; }
.table-link[aria-current="page"] { color: var(--ink-2); }
.table-link[aria-current="page"] rect { fill-opacity: 1; }
.sound-toggle path { fill: none; stroke: currentColor; stroke-width: 1.2; stroke-linecap: round; stroke-linejoin: round; transition: opacity 0.3s; }
.sound-toggle .spk { fill: currentColor; fill-opacity: 0.12; }
.sound-toggle .off { opacity: 0; }
.sound-toggle[aria-pressed="false"] .wave { opacity: 0; }
.sound-toggle[aria-pressed="false"] .off { opacity: 1; }
.maru-count {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  font: 500 12px/1 var(--sans);
  color: var(--ink-2);
  font-variant-numeric: tabular-nums;
}
.maru-count svg { width: 15px; height: 15px; overflow: visible; }
.maru-count circle { fill: none; stroke: var(--red); stroke-width: 1.6; }
.maru-count.bump svg { animation: bump 0.9s var(--ease); }
@keyframes bump {
  30% { transform: scale(1.35) rotate(-8deg); }
  100% { transform: none; }
}

.progress {
  position: fixed;
  inset: 0 0 auto 0;
  height: 1px;
  z-index: 21;
  pointer-events: none;
}
.progress i {
  display: block;
  height: 100%;
  background: var(--ink);
  opacity: 0.22;
  transform-origin: left;
  transform: scaleX(var(--progress, 0));
}

/* ------------------------------------------------------------------ */
/* Reveal                                                              */
/* ------------------------------------------------------------------ */

.js [data-reveal] {
  opacity: 0;
  translate: 0 14px;
  filter: blur(3px);
  transition:
    opacity 1.1s var(--ease),
    translate 1.1s var(--ease),
    filter 1.1s var(--ease);
  transition-delay: calc(var(--i, 0) * 110ms);
}
.js [data-reveal].in { opacity: 1; translate: 0 0; filter: none; }

/* ------------------------------------------------------------------ */
/* Contents — a column of ticks on the left                            */
/* ------------------------------------------------------------------ */

.toc {
  position: fixed;
  left: 22px;
  top: 50%;
  translate: -6px -50%;
  z-index: 19;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.9s var(--ease), translate 0.9s var(--ease);
  font: 500 10px/1 var(--sans);
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--ink-3);
}
.toc.on { opacity: 1; translate: 0 -50%; pointer-events: auto; }
.toc ol { list-style: none; margin: 0; padding: 0; }
.toc > ol { display: grid; gap: 18px; }
.toc li ol { display: grid; gap: 1px; margin-top: 6px; }
.toc a {
  display: flex;
  align-items: center;
  gap: 10px;
  height: 15px;
  color: inherit;
  text-decoration: none;
  transition: color 0.4s;
}
.toc a i {
  flex: none;
  width: 7px;
  height: 1px;
  background: currentColor;
  opacity: 0.55;
  transition: width 0.5s var(--ease), opacity 0.5s;
}
.toc a span {
  white-space: nowrap;
  opacity: 0;
  translate: -3px 0;
  transition: opacity 0.4s var(--ease), translate 0.4s var(--ease);
}
.toc .toc-head { height: 18px; gap: 9px; }
.toc .toc-head b { font-size: 12px; font-weight: 700; color: var(--ink-3); width: 12px; text-align: center; letter-spacing: 0; transition: color 0.4s; }
.toc .cur .toc-head b { color: var(--ink); }
.toc a:hover, .toc a.on { color: var(--ink); }
.toc a.on i { width: 16px; opacity: 1; }
.toc a.on span, .toc:hover a span { opacity: 1; translate: 0 0; }
.toc li:not(.cur) a:not(:hover) span { color: var(--ink-3); }
/* a soft wash behind the labels while they're all showing */
.toc::before {
  content: "";
  position: absolute;
  inset: -40px -60px -40px -40px;
  z-index: -1;
  background: radial-gradient(closest-side, color-mix(in srgb, var(--paper-now) 92%, transparent), transparent);
  opacity: 0;
  transition: opacity 0.4s;
  pointer-events: none;
}
.toc:hover::before { opacity: 1; }
/* when the page is tight, or the table is big, just the ticks */
@media (max-width: 1180px) { .toc a.on span { opacity: 0; } .toc:hover a span { opacity: 1; } }
.toc-quiet .toc:not(:hover) a span { opacity: 0; }
@media (max-width: 860px), (max-height: 620px) { .toc { display: none; } }

/* ------------------------------------------------------------------ */
/* For readers who scroll: new things fade up into the page            */
/* ------------------------------------------------------------------ */

.edge-fade {
  position: fixed;
  inset: auto 0 0 0;
  height: min(200px, 24vh);
  z-index: 15;
  pointer-events: none;
  background: linear-gradient(
    to top,
    color-mix(in srgb, var(--paper-now) 40%, transparent),
    color-mix(in srgb, var(--paper-now) 16%, transparent) 50%,
    transparent
  );
  opacity: 0;
  transition: opacity 0.8s var(--ease-soft);
}
.input-scroll body:not(.loading) .edge-fade:not(.off) { opacity: 1; }

/* ------------------------------------------------------------------ */
/* Paper grain — a whisper of texture                                  */
/* ------------------------------------------------------------------ */

.grain {
  position: fixed;
  inset: -50%;
  z-index: 30;
  pointer-events: none;
  opacity: calc(0.045 * var(--alive));
  mix-blend-mode: multiply;
  background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='220' height='220'><filter id='n'><feTurbulence type='fractalNoise' baseFrequency='.85' numOctaves='3' stitchTiles='stitch'/><feColorMatrix values='0 0 0 0 .2  0 0 0 0 .17  0 0 0 0 .12  0 0 0 1.4 0'/></filter><rect width='100%' height='100%' filter='url(%23n)'/></svg>");
}

/* ------------------------------------------------------------------ */
/* Footer                                                              */
/* ------------------------------------------------------------------ */

.foot {
  padding: 10vh var(--gutter) 8vh;
  text-align: center;
  display: grid;
  gap: 10px;
  justify-items: center;
}

/* ------------------------------------------------------------------ */
/* Loader shell (the WebGL lives inside)                               */
/* ------------------------------------------------------------------ */

.loader {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: var(--paper);
  display: grid;
  place-items: end center;
  transition: opacity 1s var(--ease-soft), visibility 0s linear 1s;
}
.loader canvas { position: absolute; inset: 0; width: 100%; height: 100%; display: block; opacity: 0; transition: opacity 1.4s var(--ease-soft); }
.loader canvas.on { opacity: 1; }
.loader-caption {
  position: relative;
  z-index: 1;
  margin-bottom: max(7vh, 40px);
  display: grid;
  justify-items: center;
  gap: 10px;
  padding: 14px 22px;
  border-radius: 999px;
  background: color-mix(in srgb, var(--paper) 80%, transparent);
  backdrop-filter: blur(6px);
  -webkit-backdrop-filter: blur(6px);
}
.loader-caption .kanji { font-size: 20px; letter-spacing: 0.3em; margin-right: -0.3em; }
.loader-caption .label { font-variant-numeric: tabular-nums; }
.loader.done { opacity: 0; visibility: hidden; pointer-events: none; }
.loader-fallback { position: absolute; inset: 0; display: grid; place-items: center; }
.loader-fallback .row { --tw: 44px; display: flex; gap: 14px; }
.loader-fallback .tile { animation: cssflip 2.4s var(--ease-soft) infinite; animation-delay: calc(var(--k) * 0.18s); }
@keyframes cssflip {
  0%, 30% { transform: perspective(300px) rotateX(0); }
  60%, 100% { transform: perspective(300px) rotateX(360deg); }
}

.js body.loading main,
.js body.loading .bar { opacity: 0; }
main, .bar { transition: opacity 1.2s var(--ease-soft); }

/* ------------------------------------------------------------------ */
/* Motion preferences                                                  */
/* ------------------------------------------------------------------ */

@media (prefers-reduced-motion: reduce) {
  :root { --alive: 0; }
  html { scroll-behavior: auto; }
  *, *::before, *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.01ms !important;
  }
  .js [data-reveal] { filter: none; translate: none; }
}

@media (max-width: 560px) {
  body { font-size: 17.5px; }
  .bar-chapter { font-size: 10px; letter-spacing: 0.12em; }
  .page { gap: 22px; --page-pad: clamp(40px, 7vh, 64px); }
}
