/* Rules page: the rules-01 ink scroll (docs/scenes/rules-01/HANDOFF.md).
 * Four chapters over one scroll-scene: title / machine rules beside the lit lanterns / the stone
 * tablet with the twelve rules (page flow over the held film: scroll-scene.js dwell) / summit seal.
 * No boxes: copy sits on the mist like brush writing; the rules are carved in the stone.
 * Night (data-scene-theme="night"): deep-blue film, so copy turns light and the halo turns dark. */

.rules-scene {
  --r-ink: #27303a; --r-body: #3c4651; --r-halo: var(--tc-paper); --r-paper: #f1e9d6; --r-seal: #a83c2e;
  --r-glow: 255, 196, 120; --r-fog: 242, 236, 221;
}
.rules-scene[data-scene-theme="night"] {
  --r-ink: #eee5d0; --r-body: #d9d2c2; --r-halo: #0f1a2c; --r-paper: #e9dfc8; --r-seal: #b8493a;
  --r-glow: 255, 186, 104; --r-fog: 18, 30, 50;
  background: #101a2b;
}
/* The engine's paper wash over the lower band becomes a night-blue wash. */
.rules-scene[data-scene-theme="night"] .scroll-scene-stage::before {
  background: linear-gradient(0deg, rgba(14, 24, 42, .7) 0, rgba(14, 24, 42, .38) 22%, transparent 40%);
}
.rules-scene .scroll-scene-copy { color: var(--r-ink); }
.rules-scene .scroll-scene-copy :is(p, li, h2, h3) { text-shadow: 0 0 16px var(--r-halo), 0 0 3px var(--r-halo); }
.rules-scene .scroll-scene-copy p { color: var(--r-body); }
.rules-scene :is(.scene-heading, .scene-kicker) { color: var(--r-ink); text-shadow: 0 0 22px var(--r-halo), 0 0 3px var(--r-halo); }
.rules-scene .scene-cue { color: color-mix(in srgb, var(--r-ink) 70%, transparent); }
.rules-live { position: absolute; inset: 0; width: 100%; height: 100%; z-index: 2; pointer-events: none; }

/* ---------- 卷一: title + subtitle as two vertical lines in the mist, like a hanging scroll ---------- */
.rules-scene .scroll-scene-copy h1.rules-title { color: var(--r-ink); text-shadow: 0 .02em .14em color-mix(in srgb, var(--r-halo) 70%, transparent); }
.rules-scene .scroll-scene-copy .rules-sub {
  grid-area: side / side / main / main; align-self: start; justify-self: end; max-width: none; white-space: nowrap;
  writing-mode: vertical-rl; letter-spacing: .32em; line-height: 1.6;
  margin: calc(clamp(12px, 7svh, 84px) + 1.6em) calc(clamp(4px, 10vw, 190px) + clamp(50px, 5.4vw, 90px) + 26px) 0 0;
  font: 500 calc(clamp(16px, 1.35vw, 20px) * var(--text-scale, 1)) / 1.6 var(--tc-display);
}

/* The chapters keep .rule-section (admin copy editor + custom sections), not its page skin. */
.rules-scene .rule-section { display: block; border: 0; padding: 0; background: none; box-shadow: none; min-height: 0; scroll-margin-top: 0; }
/* Before the engine's first tick only the opening beats may show (the rest would flash over the title). */
@media (scripting: enabled) {
  .rules-scene:not([data-scene-live], .is-static) [data-beat]:not([data-beat="title"], [data-beat="cue"]) { visibility: hidden; }
}

/* ---------- Writing on the mist (卷二, 卷四): no frame, no card, no hard edge. A feathered patch of
   fog behind the words; brush title (vertical on desktop, on the block's outer side), sans body. ---------- */
.rules-scene .rules-mist { position: relative; isolation: isolate; display: grid; grid-template-columns: auto minmax(0, 1fr); column-gap: clamp(18px, 2vw, 34px); align-items: start; }
.rules-mist::before { content: ""; position: absolute; z-index: -1; inset: -34% -26%; pointer-events: none;
  background: radial-gradient(closest-side, color-mix(in srgb, var(--r-halo) 84%, transparent) 58%, color-mix(in srgb, var(--r-halo) 50%, transparent) 84%, transparent); }
.rules-mist > :not(h2) { grid-column: 2; }
.rules-scene .rules-mist > .scene-heading { grid-column: 1; grid-row: 1 / span 8; margin: 0; writing-mode: vertical-rl; letter-spacing: .2em;
  font-size: calc(clamp(36px, 3.3vw, 54px) * var(--text-scale, 1)); line-height: 1.1; }
.rules-scene .scroll-scene-copy .rules-mist :is(p, li, span) { font: 500 calc(17px * var(--text-scale, 1)) / 1.88 var(--tc-sans); color: var(--r-body); }

/* ---------- 卷二 and 卷四 are their own copy layers after the stage in the page (so the page reads in
   order: title, machine rules, tablet, summit). On the film each pins like the stage and shows with
   its beat. Phones and screens under 1200 px (no room beside the lanterns): 卷二 is a dwell instead
   (scroll-scene.js, same media query in rules-01.json), scrolling past the held lantern frame. ---------- */
.rules-scene :is(.rules-machine-copy, .rules-summit-copy) { position: sticky; top: 0; z-index: 3; pointer-events: none; }
.rules-scene :is(.rules-machine-copy, .rules-summit-copy) > * { pointer-events: auto; }

/* ---------- 卷二: the machine rules, on the mist up right of the lantern pair (the lamps are lit by
   rules-live.js as the beat comes in; nothing to press). ---------- */
.rules-scene .rules-machine { grid-area: side / side / main / main; align-self: start; justify-self: end; width: min(640px, 44vw); max-width: none;
  grid-template-columns: minmax(0, 1fr) auto; margin-top: clamp(0px, 2svh, 24px); }
.rules-machine > :not(h2) { grid-column: 1; }
.rules-scene .rules-machine > .scene-heading { grid-column: 2; }
.rules-scene .rules-machine .rule-alert { margin: 0 0 8px; }
.rules-scene .scroll-scene-copy .rules-machine .rule-alert b { font: 700 calc(20px * var(--text-scale, 1)) / 1.5 var(--tc-display); color: var(--r-seal); }
.rules-scene[data-scene-theme="night"] .rules-machine .rule-alert b { color: #f09a82 !important; }
.rules-scene .rules-machine > p { margin: 0 0 8px; max-width: none; }
.rules-scene .rules-machine .avoid-grid { list-style: none; margin: 4px 0 10px; padding: 0; display: grid; grid-template-columns: 1fr 1fr; gap: 0 22px; }
.rules-scene .rules-machine .avoid-grid li { position: relative; padding-left: 16px; line-height: 1.7; }
.rules-scene .rules-machine .avoid-grid li::before { content: ""; position: absolute; left: 1px; top: .78em; width: 6px; height: 6px; border-radius: 50%; background: var(--r-seal); opacity: .8; }
.rules-scene .scroll-scene-copy .rules-machine .tip { margin: 0; font-size: calc(15px * var(--text-scale, 1)); }

/* ---------- 卷三: the stone tablet (assets/scenes/rules-01/tablet). Normal page flow over the pinned
   stage: scroll-scene.js holds the film while it passes and fogs the picture under it. The stone is
   three pieces at their own ratio (--u = one art pixel of the 2400 px wide art): top (roof +
   forehead), mid (seamless, repeat-y, never stretched), base (frame end + plinth). Mid starts under
   the top's last row, so the transparent roof never shows it; the base fades in over it. ---------- */
.rules-scene .rules-tablet {
  --tw: min(1040px, 88vw); --u: calc(var(--tw) / 2400);
  --top: url(/assets/scenes/rules-01/tablet/rules-tablet-top.webp); --mid: url(/assets/scenes/rules-01/tablet/rules-tablet-mid.webp);
  --base: url(/assets/scenes/rules-01/tablet/rules-tablet-base.webp); --carve: #353b43;
  position: relative; z-index: 3; isolation: isolate; box-sizing: border-box; width: var(--tw); margin: 0 auto;
  padding: calc(var(--u) * 720) calc(var(--u) * 440) calc(var(--u) * 430); color: var(--carve);
}
.rules-tablet::before, .rules-tablet::after { content: ""; position: absolute; left: 0; right: 0; pointer-events: none; }
.rules-tablet::before { z-index: -2; top: 0; bottom: calc(var(--u) * 1140); box-sizing: border-box; padding-top: calc(var(--u) * 815);
  background: var(--top) 0 0 / 100% auto no-repeat border-box, var(--mid) 0 0 / 100% auto repeat-y content-box;
  background-clip: border-box, content-box; }
.rules-tablet::after { z-index: -1; bottom: 0; height: calc(var(--u) * 1280); background: var(--base) 0 100% / 100% auto no-repeat;
  -webkit-mask: linear-gradient(transparent, #000 calc(var(--u) * 140)); mask: linear-gradient(transparent, #000 calc(var(--u) * 140)); }
/* Night: moonlit stone (the carving stays dark ink, >= 4.5:1 on it). */
.rules-scene[data-scene-theme="night"] .rules-tablet { --carve: #1e2329; }
.rules-scene[data-scene-theme="night"] .rules-tablet::before, .rules-scene[data-scene-theme="night"] .rules-tablet::after { filter: sepia(.3) hue-rotate(180deg) saturate(.7) brightness(.74); }
/* Carved: darker than the stone, a faint shadow on the upper lip and light on the lower one. */
.rules-scene .rules-tablet :is(h2, b, span) { color: var(--carve); text-shadow: 0 1px 0 rgba(255, 255, 255, .42), 0 -1px 0 rgba(0, 0, 0, .2); }
.rules-scene .rules-tablet > .scene-heading {      /* in the forehead panel (top art x 830-1566, y 340-555) */
  position: absolute; left: calc(var(--u) * 830); top: calc(var(--u) * 340); width: calc(var(--u) * 736); height: calc(var(--u) * 215);
  display: grid; place-items: center; margin: 0; padding: 0; font-size: calc(var(--u) * 140); line-height: 1; letter-spacing: .5em; text-indent: .5em;
}
.rules-scene .rules-tablet .rule-list { counter-reset: rule; list-style: none; margin: 0; padding: 0; display: block; columns: 2; column-gap: calc(var(--u) * 120); }
.rules-scene .rules-tablet .rule-list > li { counter-increment: rule; display: block; break-inside: avoid; margin: 0; padding: 0 0 22px; border: 0;
  scroll-margin-top: calc(var(--tc-topbar, 72px) + 24px); }
.rules-scene .rules-tablet .rule-list > li:nth-child(7) { break-before: column; }
.rules-scene .rules-tablet .rule-list > li::before { content: none; }
.rules-scene .rules-tablet .rule-list b { display: block; margin: 0 0 4px; font: 400 calc(clamp(24px, 2.1vw, 29px) * var(--text-scale, 1)) / 1.35 var(--tc-brush); letter-spacing: .05em; color: var(--carve) !important; }
/* 壹 … 拾貳 (trad-chinese-formal would write 壹拾 for ten). */
@counter-style rules-formal { system: fixed; symbols: "壹" "貳" "參" "肆" "伍" "陸" "柒" "捌" "玖" "拾" "拾壹" "拾貳"; fallback: trad-chinese-formal; }
.rules-scene .rules-tablet .rule-list b::before { content: counter(rule, rules-formal) "、"; }
.rules-scene .rules-tablet .rule-list span { display: block; font: 500 calc(17px * var(--text-scale, 1)) / 1.85 var(--tc-sans); }
/* While the tablet passes, the held picture behind it sinks into fog (blur + paper or night mist). */
.rules-scene .scroll-scene-stage::after { content: ""; position: absolute; inset: 0; z-index: 4; pointer-events: none; opacity: 0; background: rgba(var(--r-fog), .62); }
.rules-scene[data-scene-dwell="basic"] .scroll-scene-stage::after { opacity: var(--scene-dwell, 0); -webkit-backdrop-filter: blur(6px); backdrop-filter: blur(6px); }

/* ---------- 卷四: summit. Words for new residents on the sky, then the seal. ---------- */
.rules-scene .rules-summit { grid-area: side / side / main / main; align-self: center; justify-self: start; max-width: min(36em, 50vw); }
.rules-scene .rules-summit p { margin: 0 0 10px; }
.rules-scene .scroll-scene-copy .rules-summit .resident-welcome { color: var(--r-ink) !important; font-weight: 600; font-size: calc(17px * var(--text-scale, 1)) !important; margin-top: 0; }   /* styles.css forces 20px !important */
.rules-scene[data-scene-theme="night"] .rules-seal::before { border-color: #e07a62; }
.rules-scene[data-scene-theme="night"] .rules-seal-label { color: #f09a82; }
.rules-pass { display: flex; align-items: center; gap: 18px; margin: 22px 0 14px; }
.rules-pass-side { display: grid; gap: 10px; }
/* The seal: an empty red frame until stamped, then the red "已讀". */
.rules-seal {
  position: relative; flex: none; width: 70px; height: 70px; padding: 0; border: 0; background: transparent; cursor: pointer;
  touch-action: manipulation; -webkit-tap-highlight-color: transparent;
}
.rules-seal::before {
  content: ""; position: absolute; inset: 4px; border: 2px dashed color-mix(in srgb, var(--r-seal) 75%, transparent); border-radius: 6px; rotate: -5deg;
  transition: rotate .3s ease, scale .3s ease;
}
.rules-seal:is(:hover, :focus-visible)::before { rotate: 0deg; scale: 1.05; }
/* The empty frame breathes until it is stamped. */
.rules-seal:not([aria-pressed="true"], :hover, :focus-visible)::before { animation: rules-hint-seal 2.4s ease-in-out infinite; }
@keyframes rules-hint-seal { 0%, 100% { scale: 1; box-shadow: 0 0 0 0 transparent; } 50% { scale: 1.08; box-shadow: 0 0 18px 2px color-mix(in srgb, var(--r-seal) 40%, transparent); } }
.rules-seal:focus-visible { outline: 3px solid var(--r-seal); outline-offset: 4px; border-radius: 8px; }
.rules-seal-ink {
  position: absolute; inset: 4px; display: grid; place-items: center; border-radius: 6px; rotate: -5deg;
  writing-mode: vertical-rl; letter-spacing: .1em; color: #f6efe0; background: var(--r-seal);
  font: 400 calc(23px * var(--text-scale, 1)) / 1 var(--tc-brush);
  /* ink bleeding into the paper at the edge */
  box-shadow: 0 0 0 1px color-mix(in srgb, var(--r-seal) 55%, transparent), 0 0 6px 1px color-mix(in srgb, var(--r-seal) 38%, transparent);
  opacity: 0; scale: 1.6;
}
.rules-seal-label { position: absolute; inset: 0; display: grid; place-items: center; font: 600 calc(13px * var(--text-scale, 1)) / 1 var(--tc-display); letter-spacing: .3em; text-indent: .3em; color: var(--r-seal); }
.rules-seal[aria-pressed="true"] .rules-seal-label { opacity: 0; }
.rules-seal[aria-pressed="true"] .rules-seal-ink { opacity: 1; scale: 1; }
.rules-seal.is-stamping .rules-seal-ink { animation: rules-stamp .48s cubic-bezier(.3, 1.5, .5, 1) both; }
.rules-seal.is-stamping::after {   /* the paper flinches, ink spreads */
  content: ""; position: absolute; inset: -6px; border-radius: 10px; pointer-events: none;
  background: radial-gradient(closest-side, color-mix(in srgb, var(--r-seal) 30%, transparent), transparent);
  animation: rules-bleed .7s ease-out both;
}
@keyframes rules-stamp { 0% { opacity: 0; scale: 1.9; rotate: -16deg; } 60% { opacity: 1; scale: .9; rotate: -4deg; } 100% { opacity: 1; scale: 1; rotate: -5deg; } }
@keyframes rules-bleed { 0% { opacity: 0; scale: .6; } 40% { opacity: 1; } 100% { opacity: 0; scale: 1.25; } }
.rules-pass-actions { display: flex; flex-wrap: wrap; gap: 10px; }
.rules-scene[data-rules-js] .rules-pass-actions { visibility: hidden; opacity: 0; translate: -8px 0; transition: opacity .5s var(--tc-ease) .15s, translate .5s var(--tc-ease) .15s, visibility 0s linear .65s; }
.rules-scene[data-rules-js] .rules-pass.is-sealed .rules-pass-actions { visibility: visible; opacity: 1; translate: 0 0; transition-delay: .2s, .2s, 0s; }
.rules-btn {
  display: inline-flex; align-items: center; min-height: 44px; padding: 0 18px; border-radius: 6px; border: 1.5px solid var(--r-ink);
  background: var(--r-ink); color: var(--r-halo); text-decoration: none; font: 700 calc(15px * var(--text-scale, 1)) / 1 var(--tc-display); letter-spacing: .1em;
  transition: transform .2s ease, background-color .2s ease, color .2s ease;
}
.rules-btn.ghost { background: color-mix(in srgb, var(--r-halo) 55%, transparent); color: var(--r-ink); }
.rules-btn:hover { transform: translateY(-2px); }
.rules-btn:focus-visible { outline: 3px solid var(--r-seal); outline-offset: 3px; }


/* ---------- Static (reduced motion, Save-Data, film error, no JS): an ordinary readable page ---------- */
.rules-scene.is-static .scroll-scene-copy { justify-content: start; padding-top: calc(var(--tc-topbar) + 32px); gap: 40px; max-width: 900px; margin: 0 auto; }
.rules-scene.is-static :is(.rules-machine-copy, .rules-summit-copy) { min-height: 0; padding-top: 48px; }
.rules-scene.is-static .scroll-scene-stage { background: var(--r-paper); }
.rules-scene.is-static .scroll-scene-poster { position: relative; height: 46svh; inset: auto; }
.rules-scene.is-static .rules-sub { writing-mode: horizontal-tb; margin: 0; white-space: normal; }
.rules-scene.is-static :is(.rules-machine, .rules-summit) { width: auto; max-width: none; text-align: left; align-self: stretch; }
.rules-scene.is-static .scroll-scene-copy > :is(h1, .rules-sub) { align-self: start; }
.rules-scene.is-static .rules-tablet { margin-top: 24px; }
/* Plain typography, never a tinted box with a coloured bar down its left edge. */
.rules-scene :is(.rule-alert, .tip) { display: block; margin: 12px 0 0; padding: 0; border: 0; background: none; box-shadow: none; }
.rules-scene .rule-alert > :is(b, span) { display: block; }

@media (max-aspect-ratio: 3/4), (max-width: 1199px) {
  .rules-scene:not(.is-static) .rules-machine-copy { position: relative; height: auto; padding-top: 32px; padding-bottom: 64px; }
  .rules-scene .rules-machine { width: min(640px, 100%); margin-top: 0; }
}

/* ---------- Phones (portrait films) ---------- */
@media (max-aspect-ratio: 3/4) {
  .rules-scene .scroll-scene-copy h1.rules-title { justify-self: start; margin: clamp(8px, 4svh, 40px) 0 0 4vw; }
  .rules-scene .scroll-scene-copy .rules-sub { justify-self: start; margin: calc(clamp(8px, 4svh, 40px) + 1.4em) 0 0 calc(4vw + clamp(44px, 13vw, 64px) + 18px); font-size: calc(15px * var(--text-scale, 1)); }
  .rules-scene .rules-mist { display: block; }
  .rules-scene .rules-mist > .scene-heading { writing-mode: horizontal-tb; margin: 0 0 8px; font-size: calc(clamp(30px, 9vw, 40px) * var(--text-scale, 1)); letter-spacing: .1em; }
  .rules-scene .scroll-scene-copy .rules-mist :is(p, li, span) { font-size: calc(15px * var(--text-scale, 1)); line-height: 1.75; }
  .rules-scene .rules-machine { justify-self: stretch; width: auto; }
  .rules-scene .rules-machine .avoid-grid { grid-template-columns: 1fr; }
  .rules-scene .scroll-scene-copy .rules-machine .rule-alert b { font-size: calc(17px * var(--text-scale, 1)); }
  .rules-scene .rules-tablet { --tw: calc(100vw - 24px); --top: url(/assets/scenes/rules-01/tablet/rules-tablet-top-1200.webp);
    --mid: url(/assets/scenes/rules-01/tablet/rules-tablet-mid-1200.webp); --base: url(/assets/scenes/rules-01/tablet/rules-tablet-base-1200.webp);
    padding-inline: calc(var(--u) * 400); }
  .rules-scene .rules-tablet .rule-list { columns: 1; }
  .rules-scene .rules-tablet .rule-list > li:nth-child(7) { break-before: auto; }
  .rules-scene .rules-tablet .rule-list > li { padding-bottom: 16px; }
  .rules-scene .rules-tablet .rule-list b { font-size: calc(22px * var(--text-scale, 1)); }
  .rules-scene .rules-tablet .rule-list span { font-size: calc(15px * var(--text-scale, 1)); }
  .rules-scene .rules-summit { align-self: start; max-width: none; }
  .rules-scene .rules-summit p { margin-bottom: 6px; }
  .rules-pass { margin: 12px 0 8px; gap: 14px; }
  .rules-seal { width: 62px; height: 62px; }
}
@media (max-aspect-ratio: 3/4) and (max-height: 700px) {
  .rules-scene .scroll-scene-copy .rules-summit p { font-size: calc(13.5px * var(--text-scale, 1)); line-height: 1.62; margin-bottom: 4px; }
}
@media (prefers-reduced-motion: reduce) {
  .rules-seal::before, .rules-seal.is-stamping .rules-seal-ink, .rules-seal.is-stamping::after { animation: none !important; }
}

/* The static page sits on light paper whatever the hour: day palette. */
.rules-scene.is-static { --r-ink: #27303a; --r-body: #3c4651; --r-halo: var(--tc-paper); background: var(--tc-paper); }
