:root {
  --paper: #f8efdd;
  --paper2: #efe0c4;
  --crust: #e0913f;
  --crust-deep: #c4762a;
  --cocoa: #4a3222;
  --cocoa2: #6b4a2f;
  --berry: #d9534f;
  --mint: #8fbf6f;
  --sky: #7fb4d9;
  --gold: #f2c94c;
  --ink: #3a2a1a;
}
* { margin: 0; padding: 0; box-sizing: border-box; }
html, body {
  height: 100%;
  overflow: hidden;
  background: var(--cocoa);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
  font-family: "Yuanti SC", "YouYuan", "PingFang SC", "Microsoft YaHei", sans-serif;
  color: var(--ink);
}
::-webkit-scrollbar { width: 10px; height: 10px; }
::-webkit-scrollbar-track { background: var(--paper2); }
::-webkit-scrollbar-thumb { background: var(--crust); border-radius: 6px; border: 2px solid var(--paper2); }
::-webkit-scrollbar-thumb:hover { background: var(--crust-deep); }

/* ---------- ambient bakery air ---------- */
#ambient { position: fixed; inset: 0; z-index: 0; overflow: hidden;
  background: radial-gradient(120% 90% at 50% 8%, #7a5636 0%, #5a3d26 46%, #3c2817 100%); }
#ambient .glow { position: absolute; left: 50%; top: 46%; width: 90vmin; height: 90vmin;
  transform: translate(-50%, -50%);
  background: radial-gradient(circle, rgba(242, 201, 76, .22) 0%, rgba(242, 201, 76, 0) 62%);
  animation: glowPulse 6s ease-in-out infinite; }
@keyframes glowPulse { 0%, 100% { opacity: .7; } 50% { opacity: 1; } }
.mote { position: absolute; bottom: -4vh; width: 7px; height: 7px; border-radius: 50%;
  background: rgba(248, 239, 221, .5); filter: blur(1.5px);
  animation: rise linear infinite; }
.mote:nth-child(2) { left: 6%; animation-duration: 17s; animation-delay: 0s; }
.mote:nth-child(3) { left: 14%; animation-duration: 21s; animation-delay: 3s; width: 5px; height: 5px; }
.mote:nth-child(4) { left: 24%; animation-duration: 15s; animation-delay: 6s; }
.mote:nth-child(5) { left: 33%; animation-duration: 23s; animation-delay: 1s; width: 4px; height: 4px; }
.mote:nth-child(6) { left: 43%; animation-duration: 18s; animation-delay: 8s; }
.mote:nth-child(7) { left: 52%; animation-duration: 20s; animation-delay: 4s; width: 5px; height: 5px; }
.mote:nth-child(8) { left: 61%; animation-duration: 16s; animation-delay: 10s; }
.mote:nth-child(9) { left: 70%; animation-duration: 22s; animation-delay: 2s; width: 4px; height: 4px; }
.mote:nth-child(10) { left: 79%; animation-duration: 19s; animation-delay: 7s; }
.mote:nth-child(11) { left: 87%; animation-duration: 14s; animation-delay: 5s; width: 6px; height: 6px; }
.mote:nth-child(12) { left: 94%; animation-duration: 24s; animation-delay: 9s; }
.mote:nth-child(13) { left: 50%; animation-duration: 26s; animation-delay: 12s; width: 4px; height: 4px; }
@keyframes rise {
  0% { transform: translateY(0) translateX(0); opacity: 0; }
  8% { opacity: .55; }
  90% { opacity: .35; }
  100% { transform: translateY(-108vh) translateX(6vw); opacity: 0; }
}

/* ---------- HUD ---------- */
#hud { position: fixed; top: 0; left: 0; right: 0; z-index: 20;
  display: flex; align-items: center; gap: 12px;
  padding: max(10px, env(safe-area-inset-top)) 16px 8px; }
#hud[hidden] { display: none; }
#hudTitle { font-size: 18px; font-weight: 800; color: var(--paper);
  letter-spacing: 3px; text-shadow: 0 2px 6px rgba(0,0,0,.4); opacity: .92; }
.hud-right { margin-left: auto; display: flex; gap: 10px; }
#hud button {
  display: flex; align-items: center; gap: 6px;
  border: none; cursor: pointer;
  font-family: inherit; font-size: 15px; font-weight: 700;
  color: var(--cocoa); background: var(--paper);
  padding: 9px 16px; border-radius: 24px;
  box-shadow: 0 3px 0 rgba(0,0,0,.28), inset 0 -2px 0 rgba(0,0,0,.08);
  transition: transform .12s, background .2s;
}
#hud button:active { transform: translateY(2px); box-shadow: 0 1px 0 rgba(0,0,0,.28); }
#mute { width: 46px; height: 46px; padding: 0; justify-content: center; border-radius: 50%; }
#mute .ic-off { display: none; }
#mute.muted { background: var(--berry); color: #fff; }
#mute.muted .ic-on { display: none; }
#mute.muted .ic-off { display: block; }
#auto.on { background: var(--mint); color: #fff; }

/* ---------- stage & book ---------- */
#stage { position: fixed; inset: 0; z-index: 5;
  display: flex; align-items: center; justify-content: center;
  padding: 64px 12px 58px; }
#bookWrap { position: relative; width: 100%; height: 100%;
  display: flex; align-items: center; justify-content: center;
  perspective: 2400px;
  background: radial-gradient(120% 90% at 50% 8%, #7a5636 0%, #5a3d26 46%, #3c2817 100%); }
#bookBox { position: relative; }
#book {
  position: relative;
  border-radius: 10px 14px 14px 10px;
  background: var(--paper);
  box-shadow: 0 18px 50px rgba(0,0,0,.5), 0 4px 14px rgba(0,0,0,.35);
  overflow: hidden;
  transform-style: preserve-3d;
}
#book::after { content: ""; position: absolute; inset: 0; pointer-events: none; z-index: 40;
  background:
    repeating-linear-gradient(0deg, rgba(120,90,50,.028) 0 2px, rgba(0,0,0,0) 2px 5px),
    radial-gradient(140% 110% at 50% 0%, rgba(0,0,0,0) 62%, rgba(74,50,34,.14) 100%);
  border-radius: inherit; }
#gutter { position: absolute; top: 0; bottom: 0; left: 50%; width: 46px; z-index: 30;
  transform: translateX(-50%); pointer-events: none;
  background: linear-gradient(90deg, rgba(0,0,0,0) 0%, rgba(74,50,34,.16) 42%, rgba(58,42,26,.34) 50%, rgba(74,50,34,.16) 58%, rgba(0,0,0,0) 100%); }
#gutter.off { display: none; }
.page-edges { position: absolute; top: 6px; bottom: 6px; width: 12px; z-index: -1;
  background: repeating-linear-gradient(90deg, #efe0c4 0 2px, #d9c6a2 2px 3px);
  border-radius: 3px; box-shadow: 0 8px 24px rgba(0,0,0,.4); }
.page-edges.left { left: -10px; }
.page-edges.right { right: -10px; }

/* ---------- pages ---------- */
.page { position: absolute; inset: 0; overflow: hidden; }
.page-bg { position: absolute; inset: 0; }
.page-bg::after { content: ""; position: absolute; inset: 0;
  background: radial-gradient(120% 80% at 50% 108%, rgba(74,50,34,.10) 0%, rgba(0,0,0,0) 55%); }
img.layer { position: absolute; will-change: transform; pointer-events: auto; cursor: pointer; }
.art { position: absolute; inset: 0; }
.art svg { width: 100%; height: 100%; display: block; }
.art g { transform-box: fill-box; will-change: transform; }
img.layer.boing { animation: boing .5s ease; }
@keyframes boing {
  0% { transform: scale(1,1); } 30% { transform: scale(1.14,.86); }
  60% { transform: scale(.92,1.08); } 100% { transform: scale(1,1); }
}

/* ---------- page flip ---------- */
.half { position: absolute; top: 0; bottom: 0; width: 50%; overflow: hidden; z-index: 20; }
.half-inner { position: absolute; top: 0; height: 100%; width: 200%; }
.flipper { position: absolute; top: 0; bottom: 0; width: 50%; z-index: 25;
  transform-style: preserve-3d; }
.flipper.next { left: 50%; transform-origin: left center; }
.flipper.prev { left: 0; transform-origin: right center; }
.flipper .face { position: absolute; inset: 0; backface-visibility: hidden; -webkit-backface-visibility: hidden; overflow: hidden; }
.flipper .face.back { transform: rotateY(180deg); }
.flip-shade { position: absolute; inset: 0; z-index: 26; pointer-events: none; opacity: 0;
  background: linear-gradient(90deg, rgba(40,26,14,.5), rgba(40,26,14,0) 70%); }
.flipper.next .flip-shade { background: linear-gradient(270deg, rgba(40,26,14,.5), rgba(40,26,14,0) 70%); }
.flipper.go-next { animation: flipNext .78s cubic-bezier(.35,.1,.25,1) forwards; }
.flipper.go-prev { animation: flipPrev .78s cubic-bezier(.35,.1,.25,1) forwards; }
.flipper.go-next .flip-shade, .flipper.go-prev .flip-shade { animation: shade .78s ease forwards; }
@keyframes flipNext { from { transform: rotateY(0); } to { transform: rotateY(-180deg); } }
@keyframes flipPrev { from { transform: rotateY(0); } to { transform: rotateY(180deg); } }
@keyframes shade { 0% { opacity: 0; } 45% { opacity: 1; } 100% { opacity: 0; } }
#castShadow { position: absolute; top: 0; bottom: 0; width: 30%; z-index: 22; pointer-events: none; opacity: 0; }
#castShadow.next { left: 50%; background: linear-gradient(90deg, rgba(30,20,10,.4), rgba(0,0,0,0)); animation: cast .78s ease forwards; }
#castShadow.prev { right: 50%; background: linear-gradient(270deg, rgba(30,20,10,.4), rgba(0,0,0,0)); animation: cast .78s ease forwards; }
@keyframes cast { 0% { opacity: 0; } 40% { opacity: 1; } 100% { opacity: 0; } }

/* ---------- footer ---------- */
#foot { position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
  display: flex; flex-direction: column; align-items: center; gap: 5px;
  padding: 6px 12px max(10px, env(safe-area-inset-bottom)); }
#foot[hidden] { display: none; }
#dots { display: flex; gap: 9px; }
#dots span { width: 11px; height: 11px; border-radius: 50%; cursor: pointer;
  background: rgba(248,239,221,.32); transition: transform .2s, background .2s; }
#dots span.on { background: var(--gold); transform: scale(1.4); box-shadow: 0 0 10px rgba(242,201,76,.6); }
#pageLabel { font-size: 13px; font-weight: 700; color: rgba(248,239,221,.75); letter-spacing: 2px; }

/* ---------- cover ---------- */
#cover { position: fixed; inset: 0; z-index: 50; overflow: hidden;
  display: flex; flex-direction: column; align-items: center;
  background:
    radial-gradient(90% 60% at 50% 108%, rgba(242,201,76,.30) 0%, rgba(242,201,76,0) 60%),
    radial-gradient(120% 80% at 50% -10%, #8a6038 0%, rgba(0,0,0,0) 55%),
    linear-gradient(180deg, #6b4a2f 0%, #5a3d26 48%, #40291a 100%);
  transform-origin: left center; }
#cover[hidden] { display: none; }
#cover.opening { animation: openCover .9s cubic-bezier(.4,.1,.3,1) forwards; pointer-events: none; }
@keyframes openCover {
  0% { transform: perspective(2000px) rotateY(0); opacity: 1; }
  100% { transform: perspective(2000px) rotateY(-105deg); opacity: 0; }
}
.cov-awning { width: 100%; height: 62px; flex: none;
  background: repeating-linear-gradient(90deg, var(--berry) 0 64px, #fff8ef 64px 128px);
  box-shadow: 0 6px 18px rgba(0,0,0,.35);
  -webkit-mask-image: linear-gradient(#000 0 0), radial-gradient(circle 32px at 32px 32px, #000 98%, transparent);
  -webkit-mask-size: 100% calc(100% - 30px), 64px 62px;
  -webkit-mask-position: 0 0, 0 100%;
  -webkit-mask-repeat: no-repeat, repeat-x;
  mask-image: linear-gradient(#000 0 0), radial-gradient(circle 32px at 32px 32px, #000 98%, transparent);
  mask-size: 100% calc(100% - 30px), 64px 62px;
  mask-position: 0 0, 0 100%;
  mask-repeat: no-repeat, repeat-x; }
.cov-head { text-align: center; padding: 22px 16px 4px; z-index: 2; }
.cover-series { font-size: 13px; letter-spacing: 5px; color: rgba(248,239,221,.75); margin-bottom: 10px; }
.cover-title { display: flex; justify-content: center; gap: 4px;
  font-size: clamp(44px, 9vw, 88px); font-weight: 800; line-height: 1.1; }
.cover-title span { color: var(--gold); -webkit-text-stroke: 2px var(--cocoa);
  text-shadow: 0 4px 0 rgba(0,0,0,.35), 0 10px 26px rgba(0,0,0,.45);
  animation: titleBob 2.6s ease-in-out infinite; }
.cover-title span:nth-child(odd) { transform: rotate(-3deg); }
.cover-title span:nth-child(even) { transform: rotate(3deg); animation-delay: .3s; }
.cover-title span:nth-child(3) { animation-delay: .6s; }
.cover-title span:nth-child(4) { animation-delay: .9s; }
.cover-title span:nth-child(5) { animation-delay: 1.2s; }
@keyframes titleBob { 0%,100% { translate: 0 0; } 50% { translate: 0 -7px; } }
.cover-sub { margin-top: 10px; font-size: 15px; letter-spacing: 3px; color: rgba(248,239,221,.9); }
#coverArt { position: relative; flex: 1; width: min(94vw, 780px); min-height: 0; margin: 4px auto; }
#coverArt .art { position: absolute; inset: 0; }
#coverArt svg { width: 100%; height: 100%; }
#start { flex: none; margin: 6px 0 8px; border: none; cursor: pointer; font-family: inherit;
  font-size: 24px; font-weight: 800; letter-spacing: 5px; color: var(--cocoa);
  background: linear-gradient(180deg, var(--gold), var(--crust));
  padding: 16px 56px; border-radius: 44px;
  box-shadow: 0 6px 0 var(--crust-deep), 0 16px 40px rgba(0,0,0,.5), inset 0 2px 0 rgba(255,255,255,.5);
  animation: pulse 1.8s ease-in-out infinite; }
#start:active { transform: translateY(4px); box-shadow: 0 2px 0 var(--crust-deep); }
.cov-foot { flex: none; font-size: 12px; letter-spacing: 4px; color: rgba(248,239,221,.6);
  padding-bottom: max(14px, env(safe-area-inset-bottom)); }
@keyframes pulse { 0%, 100% { transform: scale(1); } 50% { transform: scale(1.05); } }

/* ---------- end ---------- */
#endCard { position: fixed; inset: 0; z-index: 60; overflow: hidden;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 16px;
  background: rgba(58,42,26,.9); }
#endCard[hidden] { display: none; }
.end-title { font-size: clamp(48px, 10vw, 84px); font-weight: 800; color: var(--gold);
  letter-spacing: 8px; text-shadow: 0 4px 0 rgba(0,0,0,.35);
  animation: pulse 1.5s ease-in-out infinite; }
.end-sub { font-size: 18px; letter-spacing: 3px; color: rgba(248,239,221,.85); }
#again { border: none; cursor: pointer; font-family: inherit; font-size: 22px; font-weight: 800;
  letter-spacing: 3px; color: var(--cocoa); background: var(--paper);
  padding: 14px 44px; border-radius: 36px; margin-top: 10px;
  box-shadow: 0 5px 0 rgba(0,0,0,.3); }
#again:active { transform: translateY(4px); box-shadow: 0 1px 0 rgba(0,0,0,.3); }
#confetti { position: absolute; inset: 0; pointer-events: none; }
#confetti i { position: absolute; top: -6vh; width: 12px; height: 16px; border-radius: 3px;
  animation: fall linear forwards; }
@keyframes fall {
  0% { transform: translateY(0) rotate(0); opacity: 1; }
  100% { transform: translateY(112vh) rotate(720deg); opacity: .6; }
}

@media (max-width: 640px) {
  #hudTitle { display: none; }
  #hud button { font-size: 13px; padding: 8px 12px; }
  #stage { padding: 56px 6px 52px; }
}