:root {
  --bg-1: #FFF8F0;
  --bg-2: #FCE8DB;
  --bg-3: #F4D2C1;
  --ink: #3E2A22;
  --soft: #A27766;
  --paper: #FFFBF3;
  color-scheme: light;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  background: radial-gradient(130% 85% at 50% 30%, var(--bg-1) 0%, var(--bg-2) 55%, var(--bg-3) 100%) fixed, var(--bg-2);
  color: var(--ink);
  font-family: 'Caveat', 'Segoe Print', 'Bradley Hand', cursive;
  -webkit-text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

/* faint paper grain over the whole backdrop */
body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  opacity: .32;
  mix-blend-mode: multiply;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='160' height='160'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.85' numOctaves='2' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 .55 0 0 0 0 .4 0 0 0 0 .3 0 0 0 .22 0'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
}

/* ---------- cake stage: touch cuts, never scrolls ---------- */
#stage {
  position: fixed;
  inset: 0;
  z-index: 2;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  transition: opacity .9s ease, filter .9s ease;
}
#stage canvas.gl {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  opacity: 0;
  animation: fadein .9s ease .05s forwards;
}
#stage.knife-on { cursor: none; }
@keyframes fadein { to { opacity: 1; } }

body.letter-phase #stage {
  opacity: .42;
  filter: blur(3px) saturate(.9);
}

/* ---------- 2D fallback ---------- */
#stage.is-2d {
  display: grid;
  place-items: center;
}
.cake2d {
  width: min(88vw, 460px, calc((100svh - 120px) * 400 / 470));
  height: auto;
  overflow: visible;
  touch-action: none;
}
.cake2d .flame-inner {
  transform-box: fill-box;
  transform-origin: 50% 100%;
  animation: flicker 1.6s ease-in-out infinite;
}
.cake2d .glow { animation: glow 2.2s ease-in-out infinite; transform-box: fill-box; transform-origin: center; }
@keyframes flicker {
  0%, 100% { transform: scale(1, 1) skewX(0deg); }
  20% { transform: scale(.94, 1.06) skewX(-3deg); }
  45% { transform: scale(1.04, .95) skewX(2deg); }
  70% { transform: scale(.97, 1.08) skewX(-1.5deg); }
}
@keyframes glow { 50% { opacity: .72; transform: scale(.94); } }
.knife2d {
  position: fixed;
  left: 0;
  top: 0;
  z-index: 20;
  pointer-events: none;
  opacity: 0;
  filter: drop-shadow(0 6px 6px rgba(90, 50, 30, .25));
  transition: opacity .2s ease;
}

/* ---------- hint ---------- */
.hint {
  position: fixed;
  left: 0;
  right: 0;
  bottom: max(9svh, calc(env(safe-area-inset-bottom) + 28px));
  z-index: 3;
  margin: 0;
  text-align: center;
  font-size: 21px;
  font-weight: 500;
  letter-spacing: .01em;
  color: var(--soft);
  opacity: 0;
  transform: translateY(6px);
  transition: opacity .7s ease, transform .7s ease;
  pointer-events: none;
}
.hint.show { opacity: .85; transform: none; }
body.og .hint { display: none; }

/* ---------- letter ---------- */
.letter-layer {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: flex;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  overscroll-behavior: contain;
  visibility: hidden;
  pointer-events: none;
  padding: 5svh 0;
}
.letter-layer.show { visibility: visible; }
body.letter-open .letter-layer { pointer-events: auto; touch-action: pan-y; }
.letter-wrap {
  margin: auto;
  transform-origin: 50% 50%;
  will-change: transform;
}
.letter {
  position: relative;
  width: var(--W, 340px);
  height: var(--H, 510px);
  perspective: 1600px;
  filter: drop-shadow(0 22px 28px rgba(110, 58, 36, .2)) drop-shadow(0 3px 5px rgba(110, 58, 36, .14));
}
.panel {
  position: absolute;
  left: 0;
  width: 100%;
  height: var(--h3, 170px);
  transform-style: preserve-3d;
  -webkit-transform-style: preserve-3d;
}
.p-top { top: 0; transform-origin: 50% 100%; }
.p-mid { top: var(--h3, 170px); }
.p-bot { top: calc(var(--h3, 170px) * 2); transform-origin: 50% 0; }
.face {
  position: absolute;
  inset: 0;
  overflow: hidden;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  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='p'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.7' numOctaves='3' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 .6 0 0 0 0 .45 0 0 0 0 .32 0 0 0 .1 0'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23p)'/%3E%3C/svg%3E");
}
.face.back { transform: rotateX(180deg); }
.p-top .face.front { border-radius: 3px 3px 0 0; }
.p-bot .face.front { border-radius: 0 0 3px 3px; }
/* fold creases */
.p-top .face.front::after,
.p-mid .face.front::after,
.p-bot .face.front::after {
  content: "";
  position: absolute;
  inset: 0;
  pointer-events: none;
}
.p-top .face.front::after { background: linear-gradient(to top, rgba(140, 100, 70, .1), transparent 7%); }
.p-mid .face.front::after { background: linear-gradient(to bottom, rgba(255, 255, 255, .6), transparent 4%), linear-gradient(to top, rgba(140, 100, 70, .08), transparent 6%); }
.p-bot .face.front::after { background: linear-gradient(to bottom, rgba(255, 255, 255, .55), transparent 5%); }
.face.back { background-color: #FBF4E8; }
.seal {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 34%;
  max-width: 120px;
  height: auto;
  transform: translate(-50%, -50%);
}

.content {
  position: absolute;
  left: 0;
  width: var(--W, 340px);
  height: var(--H, 510px);
  padding: var(--pad, 28px);
  display: flex;
  flex-direction: column;
  justify-content: center;
  font-size: var(--fs, 26px);
  line-height: 1.3;
  font-weight: 500;
  color: var(--ink);
}
.p-top .content { top: 0; }
.p-mid .content { top: calc(var(--h3, 170px) * -1); }
.p-bot .content { top: calc(var(--h3, 170px) * -2); }
.content p { margin: 0 0 .7em; }
.content .greet { font-size: 1.12em; font-weight: 600; margin-bottom: .55em; }
.content .sign { margin: .3em 0 0; text-align: right; padding-right: .4em; font-weight: 600; }
.content.measure {
  position: absolute;
  left: -9999px;
  top: 0;
  height: auto;
  display: block;
  visibility: hidden;
  pointer-events: none;
}

.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  margin: -1px;
  padding: 0;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  border: 0;
}

@media (prefers-reduced-motion: reduce) {
  .cake2d .flame-inner { animation-duration: 4s; }
  .cake2d .glow { animation: none; }
  #stage, .hint { transition-duration: .3s; }
}
