:root {
  --lacquer: #7d0b14;
  --lacquer-deep: #3f040a;
  --lacquer-glow: #b3261c;
  --gold: #e9b949;
  --gold-hi: #ffd978;
  --gold-dim: #a97a22;
  --cream: #fbe6cc;
  --cream-soft: #f0c9a8;
  --ink: #22100a;
  --ink-red: #9a0c16;
  --ballpoint: #2140a8;
  --paper: #f6b8c8;
  --paper-shade: #e897ad;

  --f-display: 'Chonburi', 'Pridi', serif;
  --f-text: 'Pridi', 'Leelawadee UI', 'Thonburi', serif;
  --f-hand: 'Sriracha', 'Pridi', cursive;

  --gutter: clamp(16px, 4vw, 56px);
  color-scheme: dark;
}

*, *::before, *::after { box-sizing: border-box; }

html, body { height: 100%; }

body {
  margin: 0;
  min-height: 100svh;
  overflow: hidden;
  font-family: var(--f-text);
  color: var(--cream);
  background:
    radial-gradient(ellipse 34% 40% at 68% 60%, rgba(255, 150, 60, 0.12), transparent 70%),
    linear-gradient(180deg, #5c0710 0%, var(--lacquer-deep) 100%);
  background-color: var(--lacquer);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

::selection { background: var(--gold); color: var(--lacquer-deep); }

:focus-visible {
  outline: 3px solid var(--gold-hi);
  outline-offset: 4px;
}

button { font: inherit; color: inherit; cursor: pointer; }

/* ควันธูป */
.haze { position: fixed; inset: 0; pointer-events: none; z-index: 0; overflow: hidden; }
.haze span {
  position: absolute;
  width: 70vmax; height: 70vmax;
  border-radius: 50%;
  background: radial-gradient(closest-side, rgba(255, 214, 190, 0.10), transparent);
  filter: blur(30px);
  animation: drift 38s ease-in-out infinite alternate;
}
.haze span:nth-child(1) { left: -20vmax; top: 10vh; }
.haze span:nth-child(2) { right: -25vmax; top: -20vh; animation-duration: 46s; animation-delay: -12s; }
.haze span:nth-child(3) { left: 20vw; bottom: -45vmax; animation-duration: 52s; animation-delay: -20s; }
@keyframes drift {
  from { transform: translate3d(0, 0, 0) scale(1); }
  to { transform: translate3d(6vmax, -8vmax, 0) scale(1.15); }
}

/* หัวเว็บ */
.masthead {
  position: fixed;
  z-index: 3;
  top: max(var(--gutter), env(safe-area-inset-top));
  left: var(--gutter);
  max-width: min(30rem, 42vw);
  pointer-events: none;
}
.masthead h1 {
  margin: 0;
  font-family: var(--f-display);
  font-weight: 400;
  font-size: clamp(2.4rem, 5.4vw, 5.25rem);
  line-height: 1;
  letter-spacing: -0.02em;
  color: var(--gold);
}
.soon {
  margin: 0.55rem 0 0;
  font-family: var(--f-display);
  font-size: clamp(1.15rem, 2vw, 1.7rem);
  line-height: 1.3;
  color: var(--cream);
}
.soon span {
  font-family: var(--f-hand);
  color: var(--gold-hi);
  display: inline-block;
  transform: rotate(-4deg) translateY(-2px);
}
.lede {
  margin: 1rem 0 0;
  font-size: clamp(1rem, 1.25vw, 1.15rem);
  line-height: 1.7;
  color: var(--cream-soft);
  max-width: 26ch;
  text-wrap: pretty;
}

/* เวที three.js */
.stage { position: fixed; inset: 0; z-index: 1; }
#scene {
  display: block;
  width: 100%; height: 100%;
  touch-action: none;
  cursor: grab;
}
#scene.holding { cursor: grabbing; }
.status {
  position: absolute;
  left: 50%;
  bottom: calc(9.5rem + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  margin: 0;
  min-height: 1.6em;
  width: max-content;
  max-width: calc(100vw - 2 * var(--gutter));
  text-align: center;
  font-family: var(--f-hand);
  font-size: clamp(1.05rem, 1.6vw, 1.3rem);
  color: var(--gold-hi);
  pointer-events: none;
}

/* ปุ่มเขย่า */
.controls {
  position: fixed;
  z-index: 3;
  left: 50%;
  bottom: calc(3.4rem + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  display: grid;
  justify-items: center;
  gap: 0.6rem;
  width: max-content;
  max-width: calc(100vw - 2 * var(--gutter));
}
.shake {
  position: relative;
  padding: 0.85rem 2.4rem 0.95rem;
  border: 0;
  border-radius: 3px;
  font-family: var(--f-display);
  font-size: clamp(1.15rem, 1.8vw, 1.4rem);
  color: var(--lacquer-deep);
  background: var(--gold);
  box-shadow: 0 8px 22px rgba(20, 0, 3, 0.45);
  transition: transform 160ms cubic-bezier(0.16, 1, 0.3, 1), filter 160ms;
}
.shake::after {
  content: '';
  position: absolute;
  inset: 4px;
  border: 1px solid rgba(63, 4, 10, 0.55);
  border-radius: 1px;
  pointer-events: none;
}
.shake:hover { filter: brightness(1.08); transform: translateY(-1px); }
.shake:active { transform: translateY(2px); filter: brightness(0.96); }
.shake:disabled { cursor: progress; filter: saturate(0.7) brightness(0.9); }
.hint {
  margin: 0;
  font-size: 0.95rem;
  color: var(--cream-soft);
  text-align: center;
}
.motion {
  border: 0;
  background: none;
  padding: 0.2rem 0.4rem;
  font-size: 0.95rem;
  color: var(--gold-hi);
  text-decoration: underline;
  text-underline-offset: 0.3em;
  text-decoration-thickness: 1px;
}

@media (min-width: 761px) {
  .controls, .status { transition: left 900ms cubic-bezier(0.16, 1, 0.3, 1); }
  body.reading .controls, body.reading .status { left: 47%; }
}

/* ท้ายเว็บ */
.foot {
  position: fixed;
  z-index: 3;
  left: var(--gutter);
  right: var(--gutter);
  bottom: max(0.9rem, env(safe-area-inset-bottom));
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 1rem;
  font-size: 0.8rem;
  line-height: 1.5;
  color: var(--cream-soft);
}
.foot p { margin: 0; }
.count { font-variant-numeric: tabular-nums; }
.fine { opacity: 0.85; text-align: center; }
.sound {
  display: inline-flex;
  align-items: center;
  gap: 0.4rem;
  border: 0;
  background: none;
  padding: 0.3rem 0;
  font-size: 0.8rem;
  color: var(--cream-soft);
  white-space: nowrap;
}
.sound svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 1.6; stroke-linecap: round; stroke-linejoin: round; }
.sound .spk { fill: currentColor; }
.sound .mute { display: none; }
.sound[aria-pressed="false"] .wave { display: none; }
.sound[aria-pressed="false"] .mute { display: inline; }
.sound:hover { color: var(--gold-hi); }

/* ใบเซียมซี */
.slip {
  position: fixed;
  z-index: 5;
  top: 1rem;
  bottom: 1rem;
  height: fit-content;
  margin-block: auto;
  right: clamp(16px, 5vw, 88px);
  width: min(27rem, 40vw);
  max-height: calc(100svh - 2rem);
  overflow: auto;
  transform-origin: 50% 0;
  color: var(--ink);
  background-color: var(--paper);
  background-image:
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='180' height='180'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='2' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 0.35 0 0 0 0 0.1 0 0 0 0 0.1 0 0 0 0.18 0'/%3E%3C/filter%3E%3Crect width='180' height='180' filter='url(%23n)'/%3E%3C/svg%3E"),
    linear-gradient(180deg, rgba(255, 255, 255, 0.18), transparent 22%, transparent 78%, rgba(120, 20, 30, 0.08));
  border-radius: 2px;
  box-shadow: 0 2px 4px rgba(20, 0, 2, 0.35), 0 26px 60px rgba(20, 0, 2, 0.55);
  scrollbar-width: thin;
  scrollbar-color: var(--ink-red) transparent;
  perspective: 900px;
}
.slip[hidden] { display: none; }
.slip:focus { outline: none; }
.slip ::selection { background: var(--ink-red); color: var(--paper); }
.slip.paper-saffron { --paper: #f5c451; --paper-shade: #d9a632; }
.slip.paper-leaf { --paper: #b9db92; --paper-shade: #95bd6c; }

.slip-frame {
  position: relative;
  margin: 0.8rem;
  padding: 1.4rem 1.4rem 1rem;
  border: 2px solid var(--ink-red);
  outline: 1px solid var(--ink-red);
  outline-offset: 4px;
}
.slip-close {
  position: absolute;
  top: 0.4rem; right: 0.4rem;
  width: 2.5rem; height: 2.5rem;
  display: grid; place-items: center;
  border: 0;
  border-radius: 50%;
  background: transparent;
  color: var(--ink-red);
}
.slip-close svg { width: 20px; height: 20px; stroke: currentColor; stroke-width: 2.2; stroke-linecap: round; }
.slip-close:hover { background: rgba(176, 16, 26, 0.1); }
.slip :focus-visible { outline-color: var(--ink-red); outline-offset: 2px; }

.slip-head {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 1rem;
  padding-bottom: 0.9rem;
  border-bottom: 1px solid var(--ink-red);
}
.slip-head h2 {
  margin: 0;
  font-family: var(--f-display);
  font-weight: 400;
  font-size: clamp(2rem, 3.4vw, 2.6rem);
  line-height: 1.1;
  color: var(--ink-red);
}
.stamp {
  margin: 0;
  padding: 0.25rem 0.7rem 0.35rem;
  border: 2px solid var(--ink-red);
  border-radius: 999px;
  box-shadow: inset 0 0 0 2px var(--paper), inset 0 0 0 3px var(--ink-red);
  font-family: var(--f-display);
  font-size: 0.95rem;
  color: var(--ink-red);
  transform: rotate(-8deg);
  mix-blend-mode: multiply;
  white-space: nowrap;
}

.verse {
  margin: 1rem 0 0;
  text-align: center;
  font-size: 1.02rem;
  line-height: 1.85;
}
.verse p { margin: 0; }
.verse p:nth-child(2n) { padding-left: 1.6em; }

.oracle {
  margin: 1.1rem 0 0;
  padding: 0.85rem 0.9rem 0.95rem;
  background: rgba(176, 16, 26, 0.09);
  border-top: 1px solid var(--ink-red);
  border-bottom: 1px solid var(--ink-red);
  text-align: center;
}
.oracle-lead { font-weight: 400; color: var(--ink-red); }
.oracle-text {
  margin: 0;
  font-weight: 600;
  font-size: clamp(1.15rem, 1.7vw, 1.35rem);
  line-height: 1.45;
  color: var(--ink);
  text-wrap: balance;
  word-break: keep-all;
  overflow-wrap: anywhere;
}

.cats {
  margin: 1rem 0 0;
  display: grid;
  grid-template-columns: auto 1fr;
  column-gap: 0.9rem;
  font-size: 0.93rem;
  line-height: 1.55;
}
.cats dt, .cats dd {
  margin: 0;
  padding: 0.42rem 0;
  border-bottom: 1px dashed rgba(176, 16, 26, 0.55);
}
.cats dt { font-weight: 600; color: var(--ink-red); white-space: nowrap; }
.cats dt:last-of-type, .cats dd:last-of-type { border-bottom: 0; }

.note {
  margin: 1rem 0 0;
  font-family: var(--f-hand);
  font-size: 1.1rem;
  line-height: 1.45;
  color: var(--ballpoint);
  transform: rotate(-1.6deg);
}
.note .sig { white-space: nowrap; font-size: 0.95rem; }

.slip-actions {
  margin-top: 1.1rem;
  display: flex;
  flex-wrap: wrap;
  gap: 0.6rem 1rem;
  align-items: center;
}
.again {
  border: 0;
  border-radius: 4px;
  padding: 0.6rem 1.2rem 0.7rem;
  font-family: var(--f-display);
  font-size: 1rem;
  color: #fff3e6;
  background: var(--ink-red);
  box-shadow: inset 0 0 0 1px rgba(255, 255, 255, 0.25), 0 3px 8px rgba(80, 0, 8, 0.3);
}
.again:hover { background: #c4121e; }
.copy {
  border: 0;
  background: none;
  padding: 0.4rem 0;
  font-size: 0.95rem;
  color: var(--ink-red);
  text-decoration: underline;
  text-underline-offset: 0.3em;
  text-decoration-thickness: 1px;
}
.imprint {
  margin: 1rem 0 0;
  padding-top: 0.6rem;
  border-top: 1px solid var(--ink-red);
  font-size: 0.8rem;
  text-align: center;
  color: #6c1a16;
}

/* จอแคบ */
@media (max-width: 760px) {
  .masthead {
    left: var(--gutter);
    right: var(--gutter);
    max-width: none;
    text-align: center;
  }
  .lede { margin: 0.6rem auto 0; max-width: 30ch; font-size: 0.95rem; line-height: 1.6; }
  .soon { margin-top: 0.35rem; }
  .controls { bottom: calc(4.6rem + env(safe-area-inset-bottom)); }
  .status { bottom: calc(10.6rem + env(safe-area-inset-bottom)); }
  .foot {
    flex-direction: column;
    gap: 0.25rem;
    font-size: 0.8rem;
    text-align: center;
  }
  .fine { order: 3; }
  .sound { position: fixed; top: 0.6rem; right: 0.6rem; padding: 0.5rem; }
  .sound span { position: absolute; width: 1px; height: 1px; overflow: hidden; clip-path: inset(50%); }

  .slip {
    top: auto;
    bottom: 0;
    left: 0; right: 0;
    width: auto;
    max-height: 86svh;
    margin: 0;
    border-radius: 6px 6px 0 0;
  }
  .slip-frame { padding: 1.2rem 1rem 0.9rem; }
  .slip { max-height: 64svh; }
  .masthead { transition: opacity 400ms; }
  body.reading .masthead { opacity: 0; }
}

@media (max-height: 640px) and (min-width: 761px) {
  .lede { display: none; }
}

@media (prefers-reduced-motion: reduce) {
  .haze span { animation: none; }
  .shake { transition: none; }
}
