/* ============================================================
   togashi's library — ink-wash theme (perf pass)
   Tokens: ink #14110f, rice #f4ecd8, vermilion #c8372d,
   sakura #f2b8c6, mist #7a9a8e, gold #c9a24b
   Phone-first. Paper/mountains are .webp images; petals are
   CSS shapes. No expensive filters or fixed overlays.
   Scroll motion: hero photo + h2 titles only,
   transform/opacity only.
   ============================================================ */

/* Self-hosted brush heading font. Large file (CJK) — swap in fast. */
@font-face {
  font-family: "Ma Shan Zheng";
  src: url("/static/fonts/ma-shan-zheng.ttf") format("truetype");
  font-weight: 400;
  font-style: normal;
  font-display: swap;
}

:root {
  --ink: #14110f;
  --rice-paper: #f4ecd8;
  --rice-deep: #e9dcc0;
  --vermilion: #c8372d;
  --vermilion-deep: #9c2a22;
  --sakura: #f2b8c6;
  --mountain-mist: #7a9a8e;
  --old-gold: #c9a24b;
  --paper-shadow: rgba(20, 17, 15, 0.18);
  --radius-ink: 3px 20px 5px 24px / 22px 5px 26px 7px;
  --font-brush: "Ma Shan Zheng", "STKaiti", "KaiTi", cursive, serif;
  --font-body: "Noto Serif", Georgia, "Times New Roman", serif;
}

* { box-sizing: border-box; }

html {
  scroll-behavior: smooth;
}

body {
  margin: 0;
  color: var(--ink);
  font-family: var(--font-body);
  line-height: 1.75;
  /* Rice paper tile (normal repeating background) over a flat wash. */
  background-color: var(--rice-paper);
  background-image: url("/static/img/paper-texture.webp");
  background-repeat: repeat;
  background-size: 480px auto;
  min-height: 100svh;
}

header, main, footer {
  position: relative;
  z-index: 1;
}

section {
  scroll-margin-top: 5rem;
}

/* ---------- header / seal nav ---------- */

header {
  max-width: 68rem;
  margin: 0 auto;
  padding: 1.25rem 1.25rem 0;
  display: flex;
  flex-wrap: wrap;
  gap: 1rem;
  align-items: center;
  justify-content: space-between;
}

.brand {
  display: flex;
  align-items: center;
  gap: 0.7rem;
  margin: 0;
}

.site-logo {
  width: 48px;
  height: 48px;
  border-radius: 6px;
  transform: rotate(-3deg);
  box-shadow: 0 4px 10px rgba(20, 17, 15, 0.3);
}

header p {
  font-family: var(--font-brush);
  font-size: 1.4rem;
  margin: 0;
  letter-spacing: 0.02em;
}

header p em {
  font-family: var(--font-body);
  font-size: 0.85rem;
  opacity: 0.65;
  display: block;
}

/* Seal stamps: vermilion squares, slightly rotated, phone = row. */
nav {
  display: flex;
  gap: 0.55rem;
  flex-wrap: wrap;
}

nav a {
  display: inline-grid;
  place-items: center;
  width: 2.6rem;
  height: 2.6rem;
  font-family: var(--font-brush);
  font-size: 1.35rem;
  line-height: 1;
  color: var(--rice-paper);
  text-decoration: none;
  background: linear-gradient(160deg, var(--vermilion), var(--vermilion-deep));
  border-radius: 6px 2px 7px 3px;
  transform: rotate(-3deg);
  box-shadow: 0 2px 0 rgba(20, 17, 15, 0.55);
  transition: transform 0.18s ease;
}

nav a:nth-child(2n) { transform: rotate(2.5deg); }
nav a:nth-child(3n) { transform: rotate(-1.5deg); }

nav a:hover {
  transform: rotate(0deg) translateY(-2px);
}

nav a:active {
  /* seal press */
  transform: rotate(0deg) scale(0.9);
}

/* ---------- layout ---------- */

main {
  max-width: 68rem;
  margin: 0 auto;
  padding: 0 1.25rem 3rem;
}

section {
  position: relative;
  margin: 3.5rem 0;
  padding: 2.2rem 1.4rem 2.4rem;
  background: rgba(255, 252, 240, 0.72);
  border: 1px solid rgba(20, 17, 15, 0.28);
  border-radius: var(--radius-ink);
  box-shadow: 0 18px 40px -22px var(--paper-shadow);
}

/* Brush-stroke divider on top of each chapter (static SVG, no image). */
section::before {
  content: "";
  position: absolute;
  top: -14px;
  left: 4%;
  right: 4%;
  height: 22px;
  opacity: 0.9;
  background: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 600 22' preserveAspectRatio='none'%3E%3Cpath d='M2 13 C 60 6, 140 17, 210 11 S 360 5, 430 13 S 540 18, 598 9 L 598 14 C 480 20, 360 17, 240 15 S 80 18, 2 16 Z' fill='%2314110f' fill-opacity='0.85'/%3E%3C/svg%3E") no-repeat center / 100% 100%;
}

/* ---------- typography ---------- */

h1, h2, h3 {
  font-family: var(--font-brush);
  font-weight: 400;
  line-height: 1.05;
  text-wrap: balance;
}

h1 {
  font-size: clamp(3rem, 9vw, 5.5rem);
  margin: 0.2em 0 0.3em;
  letter-spacing: 0.01em;
}

h2 {
  font-size: clamp(2.2rem, 6vw, 3.4rem);
  margin: 0 0 0.4em;
  display: inline-block;
  padding-right: 3.2rem; /* room for seal accent */
  position: relative;
}

/* Vermilion seal accent after each chapter title. */
h2::after {
  content: "藏";
  position: absolute;
  right: 0;
  top: 0.1em;
  width: 2.2rem;
  height: 2.2rem;
  display: grid;
  place-items: center;
  font-size: 1.2rem;
  color: var(--rice-paper);
  background: linear-gradient(160deg, var(--vermilion), var(--vermilion-deep));
  border-radius: 5px 2px 6px 2px;
  transform: rotate(4deg);
}

#gate h2::after { content: "山"; }
#scholar h2::after { content: "書"; }
#scrolls h2::after { content: "卷"; }
#seals h2::after { content: "印"; }
#toolbox h2::after { content: "器"; }
#letter h2::after { content: "信"; }

h3 {
  font-size: clamp(1.4rem, 4vw, 1.9rem);
  margin: 0 0 0.4em;
}

p { max-width: 62ch; }

a { color: var(--vermilion-deep); }
a:hover { color: var(--vermilion); }

/* Vermilion glyph accents replacing emoji (brush font, seal red). */
.glyph {
  font-family: var(--font-brush);
  font-style: normal;
  color: var(--vermilion);
}

/* ---------- hero: mountain gate ---------- */

#gate {
  overflow: hidden;
  min-height: 88svh;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  padding-top: 14rem;
  padding-bottom: 8rem;
  background: linear-gradient(180deg, #f8f1de 0%, #efe3c8 55%, #d9e2d5 100%);
}

/* Generated hero photo as a parallax layer (transform-only motion). */
#gate .hero-photo {
  content: "";
  position: absolute;
  inset: 0;
  background-image: url("/static/img/hero-mountains.webp");
  background-size: cover;
  background-position: center top;
  background-repeat: no-repeat;
}

/* Bottom rice fade so hero text stays legible (static gradient). */
#gate::after {
  content: "";
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  height: 52%;
  background: linear-gradient(180deg, transparent 0%, rgba(244, 236, 216, 0.92) 78%);
  pointer-events: none;
}

/* Sakura branch cutout, pinned off the top-right corner like a
   seal on an album leaf. Transparency comes from the file itself:
   no mask, no fade, no box, no background. */
.hero-sakura {
  position: absolute;
  top: -5%;
  right: -12%;
  width: 70%;
  height: auto;
  opacity: 0.95;
  z-index: 2;
  transform: rotate(8deg);
}

/* Keep hero text above photo and mist. */
#gate > *:not(.hero-photo) {
  position: relative;
  z-index: 2;
}

#gate > h1 {
  background: rgba(244, 236, 216, 0.9);
  border: 1px solid rgba(20, 17, 15, 0.25);
  border-radius: var(--radius-ink);
  padding: 0.6rem 1.2rem;
  text-shadow: 1px 1px 0 var(--rice-paper);
}

#gate > p,
#gate > div:not(.hero-photo) {
  background: rgba(244, 236, 216, 0.92);
  border: 1px solid rgba(20, 17, 15, 0.25);
  border-radius: var(--radius-ink);
  padding: 0.9rem 1.1rem;
}

/* Scholar: faint bamboo stalk to avoid a flat card. */
#scholar {
  background:
    linear-gradient(90deg, transparent 0 26px, rgba(122, 154, 142, 0.35) 26px 30px, transparent 30px),
    rgba(255, 252, 240, 0.72);
}

/* ---------- buttons: ink + ripple ---------- */

button {
  position: relative;
  overflow: hidden;
  font-family: var(--font-brush);
  font-size: 1.15rem;
  letter-spacing: 0.02em;
  color: var(--rice-paper);
  background: linear-gradient(165deg, #241f1a, var(--ink));
  border: 1px solid var(--ink);
  border-radius: 10px 2px 12px 3px;
  padding: 0.6rem 1.1rem;
  cursor: pointer;
  transition: transform 0.16s ease;
}

button:hover {
  transform: translateY(-2px);
}

button:active {
  transform: translateY(0) scale(0.97);
}

/* ---------- scroll cards (projects) ---------- */

#scrolls article {
  position: relative;
  margin: 3rem 0; /* room for the knobs protruding top/bottom */
  padding: 1.6rem 2.7rem 1.6rem 2.9rem;
  border: none;
  border-radius: 0;
  outline: none;
  /* Paper sheet runs 10px under each rod (rods sit at the edges);
     silk-mount strips top/bottom with a soft inner shade. */
  /* Paper sheet + silk strips stop at each rod's centre line
     (rods sit 2px in from the article edge, 48px wide zones). */
  background:
    linear-gradient(180deg, rgba(80, 55, 20, 0.35), transparent) center top / calc(100% - 52px) 12px no-repeat,
    linear-gradient(180deg, #c9ab72, #d8c49a) center top / calc(100% - 52px) 16px no-repeat,
    linear-gradient(0deg, rgba(80, 55, 20, 0.35), transparent) center bottom / calc(100% - 52px) 12px no-repeat,
    linear-gradient(0deg, #c9ab72, #d8c49a) center bottom / calc(100% - 52px) 16px no-repeat,
    linear-gradient(180deg, #fbf5e3, #f1e5c8) center / calc(100% - 52px) 100% no-repeat;
  transition: transform 0.18s ease;
}

/* Handscroll rollers: sliced bamboo-rod photos stacked as
   backgrounds — knob cap pinned top, mirrored knob bottom,
   seamless body slice tiling between (repeat-y). The right
   roller is flipped horizontally so the light mirrors.
   Rods outlive the paper by one cap height each end, so the
   knobs stick out above and below the sheet. */
#scrolls article::before,
#scrolls article::after {
  content: "";
  position: absolute;
  top: -48px;
  bottom: -48px;
  width: 48px;
  /* Mid tiling lives in the content box between the caps (cap
     height 48 minus a 3px overlap so knob meets body cleanly). */
  padding-top: 45px;
  padding-bottom: 45px;
}

/* Left roller (contact shadow is baked into the slices). */
#scrolls article::before {
  left: 2px;
  background:
    url("/static/img/rod-cap-top.webp") left top / 48px auto no-repeat,
    url("/static/img/rod-cap-bottom.webp") left bottom / 48px auto no-repeat,
    url("/static/img/rod-mid.webp") left top / 48px auto repeat-y;
  background-origin: border-box, border-box, content-box;
  background-clip: border-box, border-box, content-box;
}

/* Right roller: flipped light so the highlight mirrors. */
#scrolls article::after {
  right: 2px;
  transform: scaleX(-1);
  background:
    url("/static/img/rod-cap-top.webp") left top / 48px auto no-repeat,
    url("/static/img/rod-cap-bottom.webp") left bottom / 48px auto no-repeat,
    url("/static/img/rod-mid.webp") left top / 48px auto repeat-y;
  background-origin: border-box, border-box, content-box;
  background-clip: border-box, border-box, content-box;
}

/* Red silk cord tied around the left roller: knot band plus two
   tassel strands with beads hanging below it. */
#scrolls article::before {
  background:
    radial-gradient(circle, #7c1f18 0 3px, transparent 4px) left 8px top 36px / 32px 12px no-repeat,
    linear-gradient(180deg, #7c1f18, #c8372d 50%, #7c1f18) left 8px top 34px / 32px 12px no-repeat,
    radial-gradient(circle, #c8372d 0 2.5px, #7c1f18 3px, transparent 3.5px) left 14px top 84px / 7px 7px no-repeat,
    radial-gradient(circle, #c8372d 0 2.5px, #7c1f18 3px, transparent 3.5px) right 14px top 90px / 7px 7px no-repeat,
    linear-gradient(#a93226, #7c1f18) left 16px top 46px / 2px 40px no-repeat,
    linear-gradient(#a93226, #7c1f18) right 16px top 46px / 2px 46px no-repeat,
    url("/static/img/rod-cap-top.webp") left top / 48px auto no-repeat,
    url("/static/img/rod-cap-bottom.webp") left bottom / 48px auto no-repeat,
    url("/static/img/rod-mid.webp") left top / 48px auto repeat-y;
  background-origin: border-box, border-box, border-box, border-box, border-box, border-box, border-box, border-box, content-box;
  background-clip: border-box, border-box, border-box, border-box, border-box, border-box, border-box, border-box, content-box;
}

/* Unrolled detail: plain content inside the SAME paper sheet —
   no second scroll, no extra rods. The sheet grows and the
   rod-mid tiling stretches with it. Animation hook only. */
.unrolled {
  margin-top: 0.8rem;
}

#scrolls article:hover {
  transform: translateY(-3px);
}

#scrolls article h3 {
  margin-top: 0;
}

/* Featured legendary scroll: vermilion silk band instead of a border. */
#scrolls article:first-of-type {
  background:
    linear-gradient(180deg, rgba(80, 55, 20, 0.35), transparent) center top / calc(100% - 52px) 12px no-repeat,
    linear-gradient(180deg, rgba(200, 55, 45, 0.5), rgba(200, 55, 45, 0.15)) center top / calc(100% - 52px) 16px no-repeat,
    linear-gradient(0deg, rgba(80, 55, 20, 0.35), transparent) center bottom / calc(100% - 52px) 12px no-repeat,
    linear-gradient(0deg, rgba(200, 55, 45, 0.5), rgba(200, 55, 45, 0.15)) center bottom / calc(100% - 52px) 16px no-repeat,
    linear-gradient(180deg, #fff9e8, #f3e6c6) center / calc(100% - 52px) 100% no-repeat;
}

/* Narrow phones: slimmer rods and tighter insets so text keeps
   a readable column (≈200px+ at 360px viewport). */
@media (max-width: 480px) {
  #scrolls article {
    padding-left: 2.2rem;
    padding-right: 2.1rem;
    background-size: calc(100% - 40px) 12px, calc(100% - 40px) 16px, calc(100% - 40px) 12px, calc(100% - 40px) 16px, calc(100% - 40px) 100%;
  }

  #scrolls article:first-of-type {
    background-size: calc(100% - 40px) 12px, calc(100% - 40px) 16px, calc(100% - 40px) 12px, calc(100% - 40px) 16px, calc(100% - 40px) 100%;
  }

  #scrolls article::after {
    width: 36px;
    top: -36px;
    bottom: -36px;
    padding-top: 33px;
    padding-bottom: 33px;
    background-size: 36px auto, 36px auto, 36px auto;
  }

  #scrolls article::before {
    width: 36px;
    top: -36px;
    bottom: -36px;
    padding-top: 33px;
    padding-bottom: 33px;
    background-size: 24px 12px, 24px 12px, 7px 7px, 7px 7px, 2px 40px, 2px 46px, 36px auto, 36px auto, 36px auto;
  }
}

/* Unrolled detail spacing. */
#scrolls div[id^="detail-"] {
  margin-top: 0.8rem;
}

#fortune-result, #contact-result, #secret {
  margin-top: 1rem;
  padding: 0.9rem 1.1rem;
  background: rgba(122, 154, 142, 0.14);
  border-left: 6px solid var(--vermilion);
  border-radius: 2px 14px 4px 16px;
}

/* Result bars stay hidden until HTMX fills them (seal shows on success). */
#contact-result:empty,
#secret:empty {
  display: none;
}

/* Contact + secret seal prints. */
.contact-seal, .secret-seal {
  display: block;
  margin-top: 0.8rem;
  border-radius: 6px;
}

/* ---------- seals (contributions) ---------- */

#seals ul {
  list-style: none;
  margin: 1rem 0 0;
  padding: 0;
  display: grid;
  gap: 1rem;
}

#seals li {
  position: relative;
  padding: 1rem 1.1rem 1rem 4rem;
  background: rgba(255, 252, 240, 0.7);
  border: 1px solid rgba(20, 17, 15, 0.3);
  border-radius: var(--radius-ink);
  transition: transform 0.16s ease;
}

#seals li::before {
  content: "印";
  position: absolute;
  left: 0.9rem;
  top: 0.9rem;
  width: 2.2rem;
  height: 2.2rem;
  display: grid;
  place-items: center;
  font-family: var(--font-brush);
  color: var(--rice-paper);
  background: linear-gradient(160deg, var(--vermilion), var(--vermilion-deep));
  border-radius: 5px 2px 6px 2px;
  transform: rotate(-5deg);
}

#seals li:hover {
  transform: translateY(-2px);
}

/* ---------- toolbox ---------- */

#toolbox ul {
  list-style: none;
  margin: 1rem 0 0;
  padding: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 0.7rem;
}

#toolbox li {
  padding: 0.5rem 0.9rem;
  background: var(--ink);
  color: var(--rice-paper);
  font-family: var(--font-brush);
  font-size: 1.1rem;
  border-radius: 12px 3px 14px 4px;
  transform: rotate(-0.6deg);
}

#toolbox li:nth-child(2n) {
  transform: rotate(0.8deg);
  background: #1a2420;
  border: 1px solid rgba(122, 154, 142, 0.6);
}

#toolbox li:nth-child(3n) {
  background: linear-gradient(165deg, var(--vermilion), var(--vermilion-deep));
}

/* ---------- letter (contact) ---------- */

form {
  display: grid;
  gap: 0.9rem;
  max-width: 34rem;
}

label {
  display: grid;
  gap: 0.3rem;
  font-family: var(--font-brush);
  font-size: 1.2rem;
}

input, textarea {
  font-family: var(--font-body);
  font-size: 1rem;
  color: var(--ink);
  background: rgba(255, 252, 240, 0.9);
  border: 1px solid rgba(20, 17, 15, 0.5);
  border-radius: 3px 14px 4px 16px;
  padding: 0.65rem 0.85rem;
}

input:focus-visible, textarea:focus-visible, button:focus-visible, a:focus-visible {
  outline: 3px solid rgba(200, 55, 45, 0.6);
  outline-offset: 2px;
}

/* ---------- footer ---------- */

footer {
  max-width: 68rem;
  margin: 0 auto;
  padding: 1rem 1.25rem 3rem;
  text-align: center;
  opacity: 0.95;
}

footer button {
  font-size: 1rem;
}

/* ---------- motion (cheap properties only) ---------- */

/* 1. Falling sakura petals: CSS shapes, 6 total, transform/opacity only. */
.petals {
  position: absolute;
  inset: 0;
  z-index: 2;
  pointer-events: none;
  overflow: hidden;
}

.petals span {
  position: absolute;
  top: -4vh;
  width: 12px;
  height: 14px;
  opacity: 0;
  will-change: transform;
  background: radial-gradient(circle at 35% 30%, #fff 0 18%, var(--sakura) 40%, #df8fa5 100%);
  border-radius: 100% 4% 100% 4%;
  animation: petal-fall linear infinite;
}

.petals span:nth-child(1) { left: 8%; animation-duration: 12s; animation-delay: 0s; }
.petals span:nth-child(2) { left: 24%; animation-duration: 15s; animation-delay: -5s; width: 9px; height: 11px; }
.petals span:nth-child(3) { left: 41%; animation-duration: 11s; animation-delay: -8s; }
.petals span:nth-child(4) { left: 62%; animation-duration: 14s; animation-delay: -3s; width: 10px; height: 12px; }
.petals span:nth-child(5) { left: 79%; animation-duration: 13s; animation-delay: -10s; }
.petals span:nth-child(6) { left: 92%; animation-duration: 16s; animation-delay: -6s; width: 9px; height: 11px; }

@keyframes petal-fall {
  0% { opacity: 0; transform: translate3d(0, -4vh, 0) rotate(0deg); }
  8% { opacity: 0.95; }
  50% { transform: translate3d(3vw, 52vh, 0) rotate(190deg); }
  100% { opacity: 0; transform: translate3d(-3vw, 106vh, 0) rotate(360deg); }
}

/* 2. Hero photo parallax on scroll (transform only, enhancement only). */
@supports (animation-timeline: scroll()) {
  #gate .hero-photo {
    animation: mountain-parallax linear both;
    animation-timeline: scroll();
    animation-range: 0 60vh;
  }
}

@keyframes mountain-parallax {
  from { transform: translateY(0); }
  to { transform: translateY(9vh); }
}

/* 3. Section titles fade-rise on entry (transform + opacity only). */
@supports (animation-timeline: view()) {
  main section h2 {
    animation: title-rise linear both;
    animation-timeline: view(block);
    animation-range: entry 0% entry 40%;
  }
}

@keyframes title-rise {
  from { opacity: 0; transform: translateY(22px); }
  to { opacity: 1; transform: translateY(0); }
}

/* 4. Scroll-unroll for HTMX inserts: one-shot, transform + opacity only. */
.unrolled,
div[id^="detail-"] > div,
div[id^="detail-"] > p,
#fortune-result > p,
#contact-result > p,
#secret > p {
  transform-origin: top center;
  animation: scroll-unroll 0.45s ease both;
}

@keyframes scroll-unroll {
  from { opacity: 0; transform: translateY(-10px) scaleY(0.7); }
  to { opacity: 1; transform: translateY(0) scaleY(1); }
}

/* 5. Lantern glow: static glow + opacity-only halo pulse (no filters). */
.lantern-btn {
  position: relative;
  border-color: #6b4a1f;
  box-shadow: 0 0 12px rgba(201, 162, 75, 0.55);
}

.lantern-btn::before {
  content: "";
  position: absolute;
  inset: -10px;
  border-radius: inherit;
  background: radial-gradient(circle, rgba(201, 162, 75, 0.35), transparent 70%);
  opacity: 0.7;
  pointer-events: none;
  animation: lantern-halo 3.2s ease-in-out infinite;
}

@keyframes lantern-halo {
  0%, 100% { opacity: 0.45; }
  50% { opacity: 0.9; }
}

/* ---------- desktop: side seal rail ---------- */

@media (min-width: 900px) {
  header {
    position: sticky;
    top: 0;
    z-index: 20;
    background: rgba(244, 236, 216, 0.96);
    border-bottom: 1px solid rgba(20, 17, 15, 0.2);
  }

  nav {
    position: fixed;
    right: 1rem;
    top: 50%;
    transform: translateY(-50%);
    flex-direction: column;
    z-index: 30;
  }

  main {
    margin-right: 5rem;
  }

  #seals ul {
    grid-template-columns: 1fr 1fr;
  }

  #gate {
    padding-top: 16rem;
  }

  .hero-sakura {
    width: 70%;
    top: -7%;
    right: -14%;
  }
}

@media (min-width: 1200px) {
  section {
    padding: 2.8rem 2.4rem;
  }
}

/* ---------- motion safety ---------- */

@media (prefers-reduced-motion: reduce) {
  html { scroll-behavior: auto; }
  *, *::before, *::after {
    animation: none !important;
    transition: none !important;
  }
  .petals { display: none; }
}
