/* ═══════════════════════════════════════════════════════════
   ON THE PHENOMENA OF THE GRAND LINE
   A treatise in the style of @instance_11
   v3 — with grit
   ═══════════════════════════════════════════════════════════ */

/* ── Tokens ─────────────────────────────────────────────── */
:root {
  --paper: #ddd8cf;
  --paper-light: #e4dfd7;
  --paper-dark: #ccc6bb;
  --ink: #151210;
  --ink-soft: #262220;
  --mid: #767066;
  --mid-light: #a9a198;
  --accent: #6b260b;

  --font-serif: 'Cormorant Garamond', 'Georgia', 'Times New Roman', serif;
  --font-mono: 'JetBrains Mono', 'Courier New', monospace;

  --text-col: 38rem;
  --margin-col: 11rem;
  --gap: 3rem;
  --page-pad: 2.5rem;

  --space-xs: 0.25rem;
  --space-sm: 0.5rem;
  --space-md: 1rem;
  --space-lg: 2rem;
  --space-xl: 3.5rem;
  --space-2xl: 6rem;
}

/* ── Reset & Base ───────────────────────────────────────── */
*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

html {
  font-size: 17px;
  scroll-behavior: smooth;
  scroll-padding-top: 2rem;
}

body {
  font-family: var(--font-serif);
  font-size: 1rem;
  font-weight: 400;
  line-height: 1.88;
  color: var(--ink);
  background-color: var(--paper);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  position: relative;
}

/* ── Paper texture layers ───────────────────────────────── */
/* The grain canvas gets placed by JS as #grain-layer */
#grain-layer {
  position: fixed;
  inset: 0;
  z-index: 9999;
  pointer-events: none;
  opacity: 0.42;
  mix-blend-mode: multiply;
}

/* Subtle vignette */
body::after {
  content: '';
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 9998;
  background: radial-gradient(
    ellipse at 50% 50%,
    transparent 50%,
    rgba(20, 18, 14, 0.1) 100%
  );
}

/* Color variation / mottling via layered gradients */
body::before {
  content: '';
  position: fixed;
  inset: 0;
  pointer-events: none;
  z-index: 1;
  background:
    radial-gradient(ellipse at 20% 30%, rgba(190, 180, 165, 0.15), transparent 60%),
    radial-gradient(ellipse at 80% 70%, rgba(170, 165, 152, 0.12), transparent 55%),
    radial-gradient(ellipse at 50% 10%, rgba(200, 195, 185, 0.08), transparent 50%),
    radial-gradient(ellipse at 70% 90%, rgba(160, 155, 145, 0.1), transparent 50%);
  mix-blend-mode: overlay;
}

main, header, footer, .folio-nav {
  position: relative;
  z-index: 10;
}

::selection {
  background: var(--accent);
  color: var(--paper-light);
}

a {
  color: var(--ink);
  text-decoration: underline;
  text-decoration-color: var(--mid-light);
  text-underline-offset: 0.15em;
  transition: color 0.2s;
}
a:hover { color: var(--accent); text-decoration-color: var(--accent); }

em { font-style: italic; }
strong { font-weight: 600; }

/* ── Ink bleed effect on heavy text ─────────────────────── */
h1, h2, .section-num, .life__name, .drop-cap text {
  text-shadow:
    0 0 0.5px rgba(21, 18, 16, 0.3),
    0 0 1px rgba(21, 18, 16, 0.1);
}

/* ── Title Leaf ─────────────────────────────────────────── */
.title-leaf {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: var(--space-2xl) var(--page-pad);
}

.title-leaf__title {
  font-family: var(--font-serif);
  font-size: clamp(2rem, 5vw, 3.2rem);
  font-weight: 700;
  letter-spacing: 0.25em;
  text-transform: uppercase;
  line-height: 1.35;
  margin-bottom: var(--space-md);
  opacity: 0;
  animation: fadeUp 1s ease-out 0.2s forwards;
}

.title-leaf__subtitle {
  font-family: var(--font-serif);
  font-style: italic;
  font-size: clamp(0.9rem, 2vw, 1.15rem);
  font-weight: 400;
  letter-spacing: 0.1em;
  color: var(--ink-soft);
  line-height: 1.7;
  margin-bottom: var(--space-xl);
  opacity: 0;
  animation: fadeUp 1s ease-out 0.5s forwards;
}

.title-leaf > .frieze {
  opacity: 0;
  animation: fadeUp 0.8s ease-out 0.8s forwards;
}

.title-leaf__proem {
  max-width: calc(var(--text-col) + var(--gap) + var(--margin-col));
  margin-top: var(--space-xl);
  text-align: left;
  opacity: 0;
  animation: fadeUp 0.8s ease-out 1.1s forwards;
}

.title-leaf__proem p {
  font-size: 0.95rem;
  line-height: 1.9;
  text-indent: 1.5em;
}

/* ── Frieze (Ornamental Band) ───────────────────────────── */
.frieze {
  width: 100%;
  max-width: calc(var(--text-col) + var(--gap) + var(--margin-col));
  margin: 0 auto;
  overflow: hidden;
}

.frieze canvas {
  display: block;
  width: 100%;
  height: auto;
  image-rendering: auto;
}

.frieze--thin canvas {
  height: 28px;
}

/* ── Section Head ───────────────────────────────────────── */
.treatise-section {
  max-width: calc(var(--text-col) + var(--gap) + var(--margin-col));
  margin: 0 auto;
  padding: var(--space-2xl) var(--page-pad) var(--space-xl);
  opacity: 0;
  transform: translateY(16px);
  transition: opacity 0.9s ease-out, transform 0.9s ease-out;
}

.treatise-section.visible {
  opacity: 1;
  transform: translateY(0);
}

.section-head {
  display: flex;
  align-items: baseline;
  gap: var(--space-md);
  margin: var(--space-lg) 0 var(--space-xl);
}

.section-num {
  font-family: var(--font-serif);
  font-size: 2.2rem;
  font-weight: 700;
  color: var(--ink);
  flex-shrink: 0;
}

.section-title {
  font-family: var(--font-serif);
  font-size: 1.5rem;
  font-weight: 600;
  letter-spacing: 0.03em;
  line-height: 1.3;
}

/* ── Manuscript Grid (Text + Margin) ────────────────────── */
.manuscript-grid {
  display: grid;
  grid-template-columns: var(--text-col) var(--margin-col);
  gap: var(--gap);
}

.text-col {
  min-width: 0;
}

.text-col > p {
  text-indent: 1.5em;
  margin-bottom: 0.4em;
}

.text-col > p:first-child,
.drop-cap-block + p,
.rule + p,
.subhead + p,
.figure + p,
.code-block + p {
  text-indent: 0;
}

.margin-col {
  position: relative;
}

.margin-note {
  display: block;
  font-family: var(--font-serif);
  font-style: italic;
  font-size: 0.72rem;
  line-height: 1.55;
  color: var(--mid);
  text-align: right;
  margin-bottom: var(--space-sm);
}

/* ── Drop Cap ───────────────────────────────────────────── */
.drop-cap-block {
  display: flex;
  gap: var(--space-md);
  align-items: flex-start;
  margin-bottom: 0.4em;
}

.drop-cap-block p {
  text-indent: 0 !important;
  flex: 1;
}

.drop-cap {
  flex-shrink: 0;
  display: block;
  margin-top: 0.15rem;
}

/* Drop cap canvas (generated by JS) */
.drop-cap canvas {
  display: block;
  width: 58px;
  height: 58px;
}

/* SVG drop cap fallback */
.drop-cap svg {
  display: block;
}

/* ── Horizontal Rule ────────────────────────────────────── */
.rule {
  border: none;
  border-top: 1px solid var(--mid-light);
  margin: var(--space-lg) 0;
  opacity: 0.6;
}

/* ── Subheads ───────────────────────────────────────────── */
.subhead {
  font-family: var(--font-serif);
  font-size: 1.05rem;
  font-weight: 600;
  font-style: italic;
  letter-spacing: 0.04em;
  margin-top: var(--space-lg);
  margin-bottom: var(--space-sm);
}

/* ── Lives (Crew Bios) ──────────────────────────────────── */
.life {
  margin-bottom: var(--space-sm);
}

.life__name {
  font-family: var(--font-serif);
  font-size: 1.15rem;
  font-weight: 600;
  letter-spacing: 0.02em;
  margin-bottom: var(--space-sm);
}

.life__epithet {
  font-weight: 400;
  font-style: italic;
  color: var(--mid);
}

.life p {
  text-indent: 1.5em;
  margin-bottom: 0.4em;
}

.life p:first-of-type {
  text-indent: 0;
}

/* ── Figures ────────────────────────────────────────────── */
.figure {
  margin: var(--space-lg) 0;
  text-align: center;
}

.figure__diagram {
  margin-bottom: var(--space-sm);
}

.figure__diagram svg {
  max-width: 100%;
  height: auto;
}

.figure__caption {
  font-family: var(--font-serif);
  font-size: 0.82rem;
  color: var(--mid);
  line-height: 1.6;
  text-align: center;
}

/* SVG text classes */
.svg-label {
  font-family: var(--font-mono);
  font-size: 7px;
  font-weight: 400;
  letter-spacing: 0.06em;
  fill: var(--ink);
}
.svg-label-xs {
  font-family: var(--font-mono);
  font-size: 5.5px;
  font-weight: 300;
  fill: var(--mid);
}
.svg-label-blue {
  font-family: var(--font-serif);
  font-size: 10px;
  font-weight: 600;
  font-style: italic;
  fill: var(--ink);
  opacity: 0.35;
}

/* ── Code Block ─────────────────────────────────────────── */
.code-block {
  margin: var(--space-lg) 0;
}

.code {
  font-family: var(--font-mono);
  font-size: 0.78rem;
  font-weight: 300;
  line-height: 1.75;
  color: var(--ink-soft);
  padding: var(--space-md) 0;
  overflow-x: auto;
  white-space: pre;
}

.line-num {
  display: inline-block;
  width: 2.2em;
  text-align: right;
  color: var(--mid-light);
  user-select: none;
  margin-right: 1em;
}

.comment {
  color: var(--mid);
  font-style: italic;
}

.kw {
  color: var(--ink);
  font-weight: 400;
}

/* ── Colophon ───────────────────────────────────────────── */
.colophon {
  max-width: calc(var(--text-col) + var(--gap) + var(--margin-col));
  margin: 0 auto;
  padding: var(--space-2xl) var(--page-pad) var(--space-xl);
  text-align: center;
}

.colophon__mark {
  margin: var(--space-xl) auto var(--space-lg);
  opacity: 0.6;
}

.colophon__text {
  font-size: 0.82rem;
  color: var(--mid);
  line-height: 2;
  font-style: italic;
}

.colophon__finis {
  font-family: var(--font-serif);
  font-size: 0.8rem;
  font-weight: 600;
  letter-spacing: 0.3em;
  color: var(--mid);
  margin-top: var(--space-xl);
}

/* ── Folio Nav ──────────────────────────────────────────── */
.folio-nav {
  position: fixed;
  right: 1rem;
  top: 50%;
  transform: translateY(-50%);
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
  z-index: 100;
}

.folio-nav__link {
  font-family: var(--font-serif);
  font-size: 0.7rem;
  color: var(--mid-light);
  text-decoration: none;
  padding: 0.2rem 0.4rem;
  text-align: center;
  transition: color 0.2s;
  min-width: 1.5rem;
}

.folio-nav__link:hover { color: var(--ink); }
.folio-nav__link.active { color: var(--accent); font-weight: 600; }

/* ── Animations ─────────────────────────────────────────── */
@keyframes fadeUp {
  from { opacity: 0; transform: translateY(12px); }
  to { opacity: 1; transform: translateY(0); }
}

/* ── Responsive ─────────────────────────────────────────── */
@media (max-width: 960px) {
  :root {
    --text-col: 1fr;
    --margin-col: 0;
    --gap: 0;
    --page-pad: 1.5rem;
  }
  .manuscript-grid { grid-template-columns: 1fr; }
  .margin-col { display: none; }
  .section-head { flex-direction: column; gap: var(--space-xs); }
  .section-num { font-size: 1.8rem; }
  .section-title { font-size: 1.25rem; }
  .folio-nav {
    position: fixed; bottom: 0; left: 0; right: 0;
    top: auto; transform: none;
    flex-direction: row; justify-content: center;
    background: var(--paper); border-top: 1px solid var(--mid-light);
    padding: 0.5rem; gap: 1rem;
  }
}

@media (max-width: 600px) {
  html { font-size: 15px; }
  :root { --page-pad: 1rem; }
  .title-leaf__title { letter-spacing: 0.12em; }
  .drop-cap-block { flex-direction: column; gap: var(--space-sm); }
}

/* ── Print ──────────────────────────────────────────────── */
@media print {
  .folio-nav, #grain-layer, body::before, body::after { display: none; }
  body { background: white; }
  .treatise-section { opacity: 1; transform: none; page-break-inside: avoid; }
  .title-leaf { min-height: auto; page-break-after: always; }
  .title-leaf__title, .title-leaf__subtitle,
  .title-leaf > .frieze, .title-leaf__proem { opacity: 1; animation: none; }
  .margin-note { font-size: 0.65rem; }
}

/* ── Reduced Motion ─────────────────────────────────────── */
@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  .treatise-section { opacity: 1; transform: none; transition: none; }
  .title-leaf__title, .title-leaf__subtitle,
  .title-leaf > .frieze, .title-leaf__proem { opacity: 1; animation: none; }
}
