:root {
  --bg: #fff;
  --ink: #2b1a0c;
  --ink-soft: #6a4d2e;
  --sig-ink: #000;
  --paper: #f1deb0;
  --wax: #9b1b1b;
  --wax-hi: #cf3b2f;
  --p: 0;     /* 0..1 how far down the scroll you are (set by JS) */
  --roll: 0;  /* scroll offset in px, drives the roll texture (set by JS) */
  /* how far the pins reach past the paper's ends; room is kept for them at
     the sides. Keep >= KNOB_X + KNOB_R in rollers.frag. */
  --handle: 48px;
}

* { box-sizing: border-box; }
html, body { margin: 0; overflow-x: clip; }

body {
  min-height: 100vh;
  color: var(--ink);
  font: 1.15rem/1.7 "IM Fell English", Georgia, serif;
  background: var(--bg);
}

a { color: var(--wax); }

/* ---------- The scroll ---------- */

/* The page scrolls inside main, not the window (see app.js). */
html, body { height: 100%; overflow: hidden; }
main {
  position: fixed; inset: 0;
  overflow: hidden auto; overscroll-behavior: contain;
  padding: 44px max(48px, var(--handle)) 0;
}
main:focus { outline: none; }

.scroll { position: relative; max-width: 760px; margin: 0 auto; }

/* Rollers are zero-height sticky anchors; the visible roll hangs off them.
   The top one pins to the viewport top, the bottom one to the viewport floor,
   so paper appears to wind off one roll and onto the other as you scroll. */
.roller { position: sticky; z-index: 5; height: 0; }
.roller.top    { top: 0; --d: calc(22px + 28px * var(--p)); }
.roller.bottom { bottom: 0; --d: calc(22px + 28px * (1 - var(--p))); }

.roll {
  position: absolute; left: -6px; right: -6px;
  height: var(--d);
  border-radius: 7px;
  cursor: grab; touch-action: none;
  /* cylinder shading, then two layers of "paper edge" lines that slide
     with --roll so the roll looks like it's turning */
  background:
    linear-gradient(to bottom,
      rgba(60, 30, 5, .55) 0%, rgba(60, 30, 5, 0) 28%,
      rgba(255, 248, 225, .55) 40%, rgba(255, 248, 225, 0) 55%,
      rgba(60, 30, 5, .65) 100%),
    linear-gradient(rgba(120, 80, 30, .22) 0 1px, transparent 1px) 0 calc(var(--roll) * -1px) / 100% 7px,
    linear-gradient(rgba(120, 80, 30, .14) 0 1px, transparent 1px) 0 calc(var(--roll) * -1px) / 100% 11px,
    #e2c78f;
  box-shadow: 0 6px 14px rgba(0, 0, 0, .45);
}
.roll:active { cursor: grabbing; }
.roller.top .roll    { top: 0; }
.roller.bottom .roll { bottom: 0; }

/* wooden handles sticking out each end */
.roll::before, .roll::after {
  content: ""; position: absolute; top: 50%;
  width: 34px; height: 24px; transform: translateY(-50%);
  background:
    radial-gradient(circle at 40% 35%, #e0ae6c, #8a5424 50%, #3a200c 100%) var(--knob-x) 50% / 22px 24px no-repeat,
    linear-gradient(#2e1808, #b07a43 40%, #6b3e1a 75%, #2e1808) 50% 50% / 100% 12px no-repeat;
  filter: drop-shadow(0 3px 3px rgba(0, 0, 0, .5));
}
.roll::before { left: -30px;  --knob-x: 0; }
.roll::after  { right: -30px; --knob-x: 100%; }

/* soft shadow where paper curls into each roll */
.roller::after {
  content: ""; position: absolute; left: 0; right: 0; height: 30px;
  pointer-events: none;
}
.roller.top::after {
  top: var(--d);
  background: linear-gradient(rgba(90, 55, 15, .35), transparent);
}
.roller.bottom::after {
  bottom: var(--d);
  background: linear-gradient(transparent, rgba(90, 55, 15, .35));
}

.parchment {
  position: relative;
  background-color: var(--paper);
  background-image:
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='220' height='220'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.8' numOctaves='3' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 .42 0 0 0 0 .27 0 0 0 0 .08 0 0 0 .3 0'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E"),
    radial-gradient(ellipse 60% 40% at 25% 30%, rgba(255, 250, 230, .45), transparent 70%),
    radial-gradient(ellipse 50% 35% at 75% 75%, rgba(165, 115, 45, .16), transparent 70%);
  background-size: 220px 220px, 100% 1100px, 100% 1100px;
  box-shadow:
    inset 24px 0 30px -22px rgba(115, 72, 20, .6),
    inset -24px 0 30px -22px rgba(115, 72, 20, .6);
}
.parchment.rolled { max-height: 84px; overflow: clip; }
.parchment.clip   { overflow: clip; }

.sheet { padding: 80px clamp(22px, 8vw, 84px) 104px; min-height: 70vh; }

/* ---------- Typography ---------- */

h1, .title-input {
  margin: 0; text-align: center; font-weight: 400;
  font: 400 clamp(2.1rem, 6vw, 3.3rem)/1.12 "UnifrakturMaguntia", serif;
  overflow-wrap: anywhere;
}
.flourish {
  display: flex; align-items: center; gap: 14px;
  margin: .5em auto 1.2em; max-width: 280px;
  color: var(--ink-soft); font-size: 1.5rem;
}
.flourish::before, .flourish::after {
  content: ""; flex: 1; height: 1px;
  background: linear-gradient(90deg, transparent, var(--ink-soft), transparent);
}
.decree, .decree-edit { white-space: pre-wrap; overflow-wrap: anywhere; }
/* the drop cap; while writing it's a real element (.cap), see app.js */
.decree::first-letter, .decree-edit > .cap {
  float: left; font: 4.4em/.78 "UnifrakturMaguntia", serif;
  padding: .08em .1em 0 0; color: var(--wax);
}
.proclaimed { text-align: right; font-style: italic; color: var(--ink-soft); margin-top: 2em; }

/* ---------- Create form ---------- */

.title-input {
  display: block; width: 100%; padding: .1em 0 .15em;
  color: var(--ink); background: transparent; outline: none; border: 0;
}
/* the decree being written, drop cap and all */
.body-input { position: relative; min-height: 45vh; line-height: 1.7; outline: 0; cursor: text; }
/* the body's placeholder (it's not an input, so app.js marks it .empty) */
.body-input.empty::before {
  content: attr(data-placeholder);
  position: absolute; left: 0; top: 0; pointer-events: none;
}
::placeholder, .body-input.empty::before { color: rgba(43, 26, 12, .38); font-style: italic; opacity: 1; }
.actions { display: flex; flex-direction: column; align-items: center; gap: .7em; margin-top: 2.2em; }
.actions.row { flex-direction: row; justify-content: space-between; flex-wrap: wrap; margin-top: 1em; }


/* ---------- Sign form ---------- */

.sign {
  margin: 3.4em 0 3.2em; padding: 1.3em 1.4em 1.4em;
  border: 1px dashed rgba(43, 26, 12, .35); border-radius: 4px;
  background: rgba(255, 248, 225, .28);
  scroll-margin: 30vh;
}
/* The ✗ sits out to the left on its own; the line runs only under the name. */
.sig-line { display: flex; align-items: baseline; gap: .7em; margin-left: -.5em; }
.sig-line .x { flex: none; font-size: 1.6rem; color: var(--ink-soft); }
.sig-line input {
  /* Inputs clip their text to the box, and Pinyon's swashes reach well past
     each letter (capitals especially), so leave room inside: indent the
     start, pad the end, tall line box. */
  flex: 1; min-width: 0; padding: .1em .4em 0 0; text-indent: .6em;
  font: 2.6rem/1.6 "Pinyon Script", cursive; color: var(--sig-ink);
  background: transparent; border: 0; outline: 0;
  border-bottom: 1.5px solid var(--ink);
}

/* ---------- Signatures ---------- */

.witness {
  margin: 0 0 1.2em; text-align: center; font-weight: 400;
  font: 1.3rem "IM Fell English SC", serif; letter-spacing: .08em;
}
.witness:empty { display: none; }
.sigs {
  list-style: none; margin: 0; padding: 0;
  display: grid; grid-template-columns: repeat(auto-fill, minmax(200px, 1fr)); gap: 1.3em 2.2em;
}
.sig { position: relative; padding-bottom: .15em; }
/* Layout only counts each letter's advance width; Pinyon's swashes (big
   capitals especially) paint up to ~.5em beyond it. Nothing here clips, but
   pad by that much so they don't run past the paper's edge. */
.sig .name {
  display: block; overflow-wrap: anywhere; padding: 0 .25em 0 .5em;
  font: 2rem/1.25 "Pinyon Script", cursive; color: var(--sig-ink);
}
.sig time { display: block; padding-left: 1rem; font-size: .8rem; font-style: italic; color: var(--ink-soft); }
/* backwards, not both: no clip-path left over to trim swashes afterwards */
.sig.fresh .name { animation: ink 1.5s cubic-bezier(.45, .1, .3, 1) backwards; }
@keyframes ink   { from { clip-path: inset(-.5em 100% -.5em -.5em); } to { clip-path: inset(-.5em -.5em -.5em -.5em); } }

.more { margin: 0; padding-top: 2em; text-align: center; font-style: italic; color: var(--ink-soft); }
.lost { text-align: center; }

/* ---------- Floating bits ---------- */

.sign-here {
  position: fixed; right: 0; top: 50%; z-index: 15;
  padding: .75em 1.1em .75em 1.7em;
  font: 1rem "IM Fell English SC", serif; letter-spacing: .06em;
  color: #3a2600; background: #f6c343; border: 0; cursor: pointer;
  clip-path: polygon(16px 0, 100% 0, 100% 100%, 16px 100%, 0 50%);
  transform: translate(110%, -50%);
  transition: transform .4s cubic-bezier(.2, .9, .3, 1.2);
}
.sign-here.show { transform: translate(0, -50%); }
.sign-here .arrow { display: inline-block; font-size: .75em; transition: transform .3s; }
.sign-here[data-dir="up"] .arrow { transform: rotate(180deg); }
.sign-here.show { animation: nudge 2.4s 1s ease-in-out infinite; }
@keyframes nudge {
  0%, 80%, 100% { transform: translate(0, -50%); }
  88% { transform: translate(-8px, -50%); }
}

.toast {
  position: fixed; left: 50%; bottom: 76px; z-index: 30;
  max-width: calc(100vw - 32px); padding: .6em 1.1em;
  color: var(--paper); background: #130c07;
  border: 1px solid rgba(199, 154, 69, .35); border-radius: 8px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, .5);
  opacity: 0; transform: translate(-50%, 16px); pointer-events: none;
  transition: opacity .25s, transform .25s;
}
.toast.show { opacity: 1; transform: translate(-50%, 0); }

@media (max-width: 560px) {
  main { padding: 28px var(--handle) 0; }
  .roll::before, .roll::after { width: 26px; }
  .roll::before { left: -22px; }
  .roll::after  { right: -22px; }
  .actions.row { flex-direction: column-reverse; align-items: stretch; text-align: center; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition: none !important; }
}

/* ---------- WebGL paper (shaders.js + parchment.frag) ---------- */

/* ~2 screens tall, scrolls natively with the paper; shaders.js moves it and
   re-renders only when you near an edge. Hidden until the shader compiles,
   so the CSS background above is the fallback. */
.parchment { overflow: clip; }
/* With WebGL the canvas is the paper, ragged edges and all (RAGGED in
   paper.glsl). The CSS paper only stays behind the middle, as a backstop if
   the canvas lags a fast scroll, clear of where the edges wander. */
.parchment.gl {
  background: linear-gradient(to right, transparent 0 24px, var(--paper) 24px calc(100% - 24px), transparent calc(100% - 24px));
  box-shadow: none;
}
/* ...and the rolls' shadows on the paper fade out where the edges do */
.roller.gl::after {
  -webkit-mask-image: linear-gradient(to right, transparent, #000 24px, #000 calc(100% - 24px), transparent);
          mask-image: linear-gradient(to right, transparent, #000 24px, #000 calc(100% - 24px), transparent);
}
#paper-gl {
  position: absolute; top: 0; left: 0;
  display: block; width: 100%; height: 200vh;
  pointer-events: none; visibility: hidden;
  will-change: transform;
}
#paper-gl.ready { visibility: visible; }
.sheet { position: relative; }

.shader-error {
  position: fixed; left: 12px; bottom: 12px; z-index: 50;
  max-width: min(640px, calc(100vw - 24px)); max-height: 40vh; overflow: auto;
  margin: 0; padding: 10px 12px; border-radius: 6px;
  font: 12px/1.45 ui-monospace, Menlo, monospace; white-space: pre-wrap;
  color: #ffd6d6; background: rgba(40, 0, 0, .92); border: 1px solid #a33;
}

/* ---------- WebGL rolls (shaders.js + rollers.frag) ---------- */

/* Centered on the roll's axis and wide enough for the handles. The plain
   .roll span underneath stays as the drag target; its CSS look is dropped
   once the shader is running. */
.roll-gl {
  /* wide enough for the pins at both ends (plus a few px to anti-alias) */
  position: absolute; left: calc(-4px - var(--handle)); width: calc(100% + 8px + 2 * var(--handle)); height: 74px;
  pointer-events: none; visibility: hidden;
}
.roller.top .roll-gl    { top: calc(var(--d) / 2 - 37px); }
.roller.bottom .roll-gl { bottom: calc(var(--d) / 2 - 37px); }
.roll-gl.ready { visibility: visible; }
.roller.gl .roll { background: none; box-shadow: none; }
.roller.gl .roll::before, .roller.gl .roll::after { display: none; }

/* ---------- Keep paper inside the rolls ---------- */

/* An opaque panel beyond each roll, so paper never shows past it (e.g.
   during overscroll bounce). The bottom one doubles as the page's end
   padding, hence main's padding-bottom: 0. */
.roller::before {
  content: ""; position: absolute; left: -64px; right: -64px; height: 160px;
  background: var(--bg); pointer-events: none;
}
.roller.top::before    { bottom: 0; }
.roller.bottom::before { top: 0; }

/* ---------- Wax seal buttons (seal.js + seal.frag) ---------- */

.actions.row { justify-content: center; }

.seal {
  position: relative; display: block; width: 132px; height: 132px; padding: 0;
  align-self: center; flex: none;
  border: 0; background: none; cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
.seal canvas {
  /* 16% bigger than the button (ZOOM in seal.frag) so the shadow fits */
  position: absolute; inset: -8%; width: 116%; height: 116%; visibility: hidden;
  filter: drop-shadow(0 2px 2px rgba(60, 10, 10, .35)) drop-shadow(0 6px 8px rgba(60, 10, 10, .18));
}
.seal canvas.ready { visibility: visible; }
/* poured wax (wax.js): it hangs below the button too, for drips (its height
   comes from its aspect ratio), and lets clicks through to what's under it */
.seal canvas.wax { bottom: auto; height: auto; pointer-events: none; }
.seal.waxy:not(.poured) .seal-text { visibility: hidden; }
.seal.poured .seal-text { animation: label-in .4s .3s backwards; transition: translate .5s ease-out; }
@keyframes label-in { from { opacity: 0; } }
/* clearing it for the next signer (seal.js FADE_MS) */
.seal.fading canvas.wax, .seal.fading .seal-text { opacity: 0; transition: opacity .6s ease-in; }
.seal-text {
  position: absolute; inset: 0; display: grid; place-items: center;
  font: 1.35rem "IM Fell English SC", serif; letter-spacing: .06em;
  color: #fbe9d0; text-shadow: 0 1px 1px rgba(60, 0, 0, .6);
  pointer-events: none;
}
.seal:disabled { cursor: progress; }
.seal:focus-visible { outline: 2px dashed var(--ink-soft); outline-offset: 2px; border-radius: 50%; }
/* without WebGL: a flat CSS blob */
.seal.no-gl { width: 96px; height: 96px; border-radius: 50%; background: radial-gradient(circle at 35% 30%, var(--wax-hi), var(--wax) 55%, #5c0e0e); }
.seal.no-gl.pressed { box-shadow: inset 0 0 0 10px rgba(0, 0, 0, .12), inset 0 3px 8px rgba(0, 0, 0, .5); }
.seal.no-gl.pressed .seal-text { color: rgba(40, 0, 0, .55); text-shadow: 0 1px 0 rgba(255, 120, 100, .35); }

/* the wax seal on each signature (app.js; an image rendered by seal.js) */
.sig { padding-right: 46px; }
.sig-seal {
  position: absolute; right: -10px; bottom: -4px; width: 62px; height: 62px;
  pointer-events: none;
  filter: drop-shadow(0 1px 1px rgba(60, 10, 10, .4)) drop-shadow(0 3px 4px rgba(60, 10, 10, .2));
}
