@layer soil, frame, pieces, states;

@layer soil {
  :root {
    --jx-paper: #f5f0e6;
    --jx-wash: #e7ddd0;
    --jx-ink: #19312f;
    --jx-muted: #5c6a64;
    --jx-plum: #8d425c;
    --jx-amber: #e3a25a;
    --jx-moss: #40635b;
    --jx-line: color-mix(in srgb, var(--jx-ink) 16%, transparent);
    --jx-shadow: 0 25px 60px rgb(25 49 47 / 14%);
    --jx-step-1: clamp(0.75rem, 0.7rem + 0.25vw, 1rem);
    --jx-step-2: clamp(1rem, 0.9rem + 0.55vw, 1.35rem);
    --jx-step-3: clamp(1.5rem, 1.15rem + 1.4vw, 2.65rem);
    --jx-step-4: clamp(2.35rem, 1.8rem + 3vw, 5rem);
    --jx-serif: Iowan Old Style, Palatino Linotype, Book Antiqua, Palatino, Georgia, serif;
    --jx-sans: Avenir Next, Avenir, Helvetica Neue, Noto Sans CJK SC, PingFang SC, sans-serif;
  }

  * { box-sizing: border-box; }
  html { scroll-behavior: smooth; scroll-padding-top: 5rem; }
  body { margin: 0; min-width: 320px; color: var(--jx-ink); background: var(--jx-paper); font-family: var(--jx-sans); line-height: 1.72; }
  a { color: inherit; text-decoration-thickness: 1px; text-underline-offset: 0.2em; }
  img { display: block; max-width: 100%; }
  :focus-visible { outline: 3px solid var(--jx-amber); outline-offset: 4px; }
  ::selection { color: var(--jx-paper); background: var(--jx-plum); }
}

@layer frame {
  .jx-skip { position: fixed; z-index: 20; top: 0.75rem; left: 0.75rem; padding: 0.5rem 0.85rem; color: var(--jx-paper); background: var(--jx-ink); transform: translateY(-180%); transition: transform 160ms ease; }
  .jx-skip:focus { transform: translateY(0); }

  .jx-lantern { display: flex; align-items: center; justify-content: space-between; gap: 1rem; width: min(1180px, calc(100% - 2rem)); margin: 0 auto; padding: 1.15rem 0; border-bottom: 1px solid var(--jx-line); }
  .jx-lantern__sigil { display: inline-flex; align-items: center; gap: 0.55rem; text-decoration: none; font-weight: 800; letter-spacing: -0.04em; }
  .jx-lantern__sigil svg { width: 2rem; fill: var(--jx-plum); }
  .jx-lantern__sigil span { display: grid; font-size: 1.1rem; line-height: 1; }
  .jx-lantern__sigil small { margin-top: 0.25rem; color: var(--jx-muted); font-size: 0.55rem; letter-spacing: 0.18em; text-transform: uppercase; }
  .jx-lantern__trail ol { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 0.3rem 0.95rem; margin: 0; padding: 0; list-style: none; }
  .jx-lantern__trail a { color: var(--jx-muted); font-size: 0.875rem; font-weight: 650; text-decoration: none; }
  .jx-lantern__trail a:hover, .jx-lantern__trail a[aria-current="page"] { color: var(--jx-plum); }
  .jx-lantern__trail a[aria-current="page"] { text-decoration: underline; text-underline-offset: 0.42rem; }

  .jx-folio { display: grid; grid-template-columns: minmax(0, 1fr) minmax(230px, 310px); gap: clamp(2rem, 6vw, 7rem); width: min(1180px, calc(100% - 2rem)); margin: 0 auto; padding: var(--jx-step-4) 0 5rem; }
  .jx-folio__leaf { min-width: 0; }
  .jx-folio__leaf--margin { align-self: start; position: sticky; top: 1.5rem; }
  .jx-afterglow { display: grid; grid-template-columns: 1fr auto; gap: 0.35rem 2rem; width: min(1180px, calc(100% - 2rem)); margin: 0 auto; padding: 1.75rem 0 2.25rem; border-top: 1px solid var(--jx-line); color: var(--jx-muted); font-size: 0.82rem; }
  .jx-afterglow p { margin: 0; }
  .jx-afterglow strong { color: var(--jx-ink); }
  .jx-afterglow nav { display: flex; flex-wrap: wrap; justify-content: flex-end; gap: 1rem; grid-column: 2; grid-row: 1 / span 2; }
  .jx-afterglow a:hover { color: var(--jx-plum); }
}

@layer pieces {
  .jx-note--opening { position: relative; display: grid; gap: var(--jx-step-2); margin-bottom: var(--jx-step-3); padding: clamp(1.4rem, 3vw, 2.2rem); overflow: hidden; color: #f8f3e9; background: var(--jx-ink); box-shadow: var(--jx-shadow); }
  .jx-note--opening::after { position: absolute; right: -2.6rem; bottom: -3.4rem; width: 12rem; aspect-ratio: 1; border: 1px solid rgb(245 240 230 / 35%); border-radius: 50%; box-shadow: 0 0 0 1.35rem rgb(245 240 230 / 12%), 0 0 0 2.7rem rgb(245 240 230 / 7%); content: ""; pointer-events: none; }
  .jx-note__eyebrow { margin: 0; color: var(--jx-amber); font-size: 0.72rem; font-weight: 800; letter-spacing: 0.16em; text-transform: uppercase; }
  .jx-note__lead { max-width: 38rem; margin: 0; font-family: var(--jx-serif); font-size: clamp(1.35rem, 1.05rem + 1vw, 2rem); line-height: 1.3; }
  .jx-note__actions { position: relative; z-index: 1; display: flex; flex-wrap: wrap; gap: 0.7rem; }
  .jx-stamp { position: relative; z-index: 1; display: inline-flex; align-items: center; gap: 0.75rem; width: fit-content; padding: 0.7rem 1rem; color: var(--jx-ink); background: var(--jx-amber); font-size: 0.84rem; font-weight: 850; text-decoration: none; transition: transform 180ms ease, box-shadow 180ms ease; }
  .jx-stamp:hover { box-shadow: 5px 5px 0 var(--jx-plum); transform: translate(-2px, -2px); }
  .jx-stamp--plum { color: #fff8f0; background: var(--jx-plum); }
  .jx-stamp span { font-size: 1.15rem; line-height: 1; }

  .jx-frame { display: block; overflow: hidden; background: var(--jx-wash); }
  .jx-frame img { width: 100%; height: 100%; object-fit: cover; transition: transform 700ms cubic-bezier(.2,.75,.2,1); }
  .jx-frame--lead { aspect-ratio: 16 / 9; margin: 0 0 var(--jx-step-3); box-shadow: var(--jx-shadow); }
  .jx-frame--lead img { animation: jx-arrive 900ms cubic-bezier(.2,.75,.2,1) both; }
  .jx-frame--tile { flex: 0 0 min(78vw, 360px); aspect-ratio: 4 / 5; scroll-snap-align: start; }
  .jx-frame--tile:hover img { transform: scale(1.06); }

  .jx-prose { max-width: 49rem; }
  .jx-prose h1, .jx-prose h2, .jx-prose h3 { scroll-margin-top: 6rem; color: var(--jx-ink); }
  .jx-prose h1 { margin: 0 0 1.1rem; font-family: var(--jx-serif); font-size: clamp(2.45rem, 1.8rem + 3vw, 4.5rem); font-weight: 600; letter-spacing: -0.055em; line-height: 0.98; }
  .jx-prose h2 { margin: var(--jx-step-4) 0 1rem; padding-top: 0.35rem; font-family: var(--jx-serif); font-size: clamp(1.8rem, 1.45rem + 1.55vw, 3rem); font-weight: 600; letter-spacing: -0.04em; line-height: 1.12; }
  .jx-prose h3 { margin: 2rem 0 0.5rem; color: var(--jx-plum); font-size: 1.05rem; line-height: 1.35; }
  .jx-prose h4 { margin: 1.5rem 0 0.4rem; font-size: 0.98rem; }
  .jx-prose p, .jx-prose li { font-size: 1rem; }
  .jx-prose p { margin: 0 0 1rem; }
  .jx-prose ul, .jx-prose ol { margin: 0.8rem 0 1.2rem; padding-left: 1.2rem; }
  .jx-prose li + li { margin-top: 0.35rem; }
  .jx-prose a { color: var(--jx-plum); font-weight: 700; }
  .jx-prose a:hover { color: var(--jx-ink); }
  .jx-prose hr { height: 1px; margin: var(--jx-step-3) 0; border: 0; background: var(--jx-line); }
  .jx-prose strong { color: var(--jx-plum); }

  .jx-gallery { margin-top: var(--jx-step-4); padding-top: var(--jx-step-3); border-top: 1px solid var(--jx-line); }
  .jx-gallery__head { display: flex; align-items: baseline; justify-content: space-between; gap: 1rem; }
  .jx-gallery h2 { margin: 0 0 1.15rem; font-family: var(--jx-serif); font-size: clamp(1.5rem, 1.2rem + 1vw, 2.2rem); font-weight: 600; letter-spacing: -0.04em; }
  .jx-gallery__rail { display: flex; gap: 1rem; overflow-x: auto; padding: 0.3rem 0 0.8rem; scroll-snap-type: x mandatory; scrollbar-color: var(--jx-plum) transparent; }

  .jx-margin { padding: 1.35rem; border-top: 0.3rem solid var(--jx-amber); background: #ebe1d3; }
  .jx-margin ol { display: grid; gap: 1rem; margin: 1.1rem 0 0; padding: 0; counter-reset: jx-note; list-style: none; }
  .jx-margin li { position: relative; padding-left: 2rem; color: var(--jx-muted); font-family: var(--jx-serif); font-size: 1.06rem; line-height: 1.45; }
  .jx-margin li::before { position: absolute; left: 0; color: var(--jx-plum); font-family: var(--jx-sans); font-size: 0.72rem; font-weight: 900; counter-increment: jx-note; content: "0" counter(jx-note); }
  .jx-fold { margin-top: 1rem; padding: 0.85rem 1rem; border: 1px solid var(--jx-line); color: var(--jx-muted); font-size: 0.84rem; }
  .jx-fold summary { cursor: pointer; color: var(--jx-ink); font-weight: 800; }
  .jx-fold p { margin: 0.8rem 0 0; }
}

@layer states {
  @keyframes jx-arrive { from { opacity: 0; transform: scale(1.025); } to { opacity: 1; transform: scale(1); } }
  @media (max-width: 820px) {
    .jx-lantern { align-items: flex-start; }
    .jx-lantern__trail ol { justify-content: flex-start; }
    .jx-folio { grid-template-columns: 1fr; padding-top: var(--jx-step-3); }
    .jx-folio__leaf--margin { position: static; display: grid; grid-template-columns: 1fr 1fr; gap: 1rem; }
    .jx-fold { margin-top: 0; }
  }
  @media (max-width: 560px) {
    .jx-lantern { flex-direction: column; }
    .jx-lantern__trail ol { justify-content: flex-start; }
    .jx-folio { width: min(100% - 1.25rem, 1180px); }
    .jx-note--opening { margin-inline: -0.15rem; }
    .jx-folio__leaf--margin, .jx-afterglow { display: block; }
    .jx-afterglow nav { justify-content: flex-start; margin-top: 0.75rem; }
    .jx-fold { margin-top: 1rem; }
  }
  @media (prefers-reduced-motion: reduce) {
    html { scroll-behavior: auto; }
    *, *::before, *::after { animation-duration: 0.01ms !important; animation-iteration-count: 1 !important; scroll-behavior: auto !important; transition-duration: 0.01ms !important; }
  }
}
