:root {
  --paper: #f3ead8;
  --board: #33483a;
  --board-edge: #26362b;
  --ink: #4b3a2b;
  --accent: #a3202f;
  --gold: #c9a45a;
  --hand: "BookHand", "Kaiti TC", "Kaiti SC", "STKaiti", "KaiTi", "BiauKai", "DFKai-SB", serif;
  --script: "BookScript", "Snell Roundhand", "Apple Chancery", cursive;
  --foil: linear-gradient(100deg, #8a6424 0%, #f6e2a3 22%, #b98a3a 40%, #fff3c9 55%, #a67a30 72%, #ecd08a 88%, #8a6424 100%);
}

* { box-sizing: border-box; margin: 0; padding: 0; -webkit-tap-highlight-color: transparent; }

html, body {
  height: 100%;
  overflow: hidden;
  background: #050302;
  color: #f4ead8;
  font-family: var(--hand);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  overscroll-behavior: none;
}

/* Everything is laid out in landscape inside #root. On a phone held upright the engine turns
   #root a quarter turn, so the reader only has to turn the phone. */
#root {
  position: fixed;
  left: 0;
  top: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  transform-origin: 0 0;
  background: #050302;
}

/* ---------- 3D scene ---------- */

#stage {
  position: absolute;
  inset: 0;
  overflow: hidden;
  perspective: 2200px;
  perspective-origin: 50% 42%;
}

#world {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 0;
  height: 0;
  transform-style: preserve-3d;
  will-change: transform;
}

#desk {
  position: absolute;
  width: 2600px;
  height: 2200px;
  left: -1300px;
  top: -1450px;
  object-fit: cover;
  background: #3a2418;
  pointer-events: none;
}

#bookshadow {
  position: absolute;
  background: rgba(0, 0, 0, .62);
  border-radius: 10px;
  box-shadow: 0 0 30px 20px rgba(0, 0, 0, .62);
  transform: translateZ(.1px);
  will-change: transform, opacity;
}

#book {
  position: absolute;
  left: 0;
  top: 0;
  width: 0;
  height: 0;
  transform-style: preserve-3d;
  will-change: transform;
}

.leaf {
  position: absolute;
  transform-origin: 0 50%;
  transform-style: preserve-3d;
  will-change: transform;
}

.face {
  position: absolute;
  inset: 0;
  transform-style: preserve-3d;
}

.sheet {
  position: absolute;
  inset: 0;
  overflow: hidden;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  transform: translateZ(0);
  background: var(--paper);
}
.leaf.board .sheet { background: var(--board); border-radius: 0 3px 3px 0; }

/* the cut edges of a leaf: a card for the inner leaves, board plus a block of pages for the covers */
.edge { position: absolute; background: linear-gradient(180deg, #efe3ca, #cdbd9a); }
.edge.side { background: linear-gradient(90deg, #efe3ca, #cdbd9a); }
.edge.base {
  background:
    repeating-linear-gradient(180deg, rgba(110, 86, 52, .5) 0 .7px, rgba(110, 86, 52, 0) .7px 3px) 0 0 / 100% var(--bl) no-repeat,
    linear-gradient(180deg, #e6d9bd 0, #cdbd9a var(--bl), var(--board-edge) var(--bl));
}
.edge.base.side {
  background:
    repeating-linear-gradient(90deg, rgba(110, 86, 52, .5) 0 .7px, rgba(110, 86, 52, 0) .7px 3px) 0 0 / var(--bl) 100% no-repeat,
    linear-gradient(90deg, #e6d9bd 0, #cdbd9a var(--bl), var(--board-edge) var(--bl));
}
.edge.lid {
  background:
    repeating-linear-gradient(180deg, rgba(110, 86, 52, .5) 0 .7px, rgba(110, 86, 52, 0) .7px 3px) 0 var(--bd) / 100% var(--bl) no-repeat,
    linear-gradient(180deg, var(--board-edge) 0, var(--board-edge) var(--bd), #e6d9bd var(--bd), #cdbd9a 100%);
}
.edge.lid.side {
  background:
    repeating-linear-gradient(90deg, rgba(110, 86, 52, .5) 0 .7px, rgba(110, 86, 52, 0) .7px 3px) var(--bd) 0 / var(--bl) 100% no-repeat,
    linear-gradient(90deg, var(--board-edge) 0, var(--board-edge) var(--bd), #e6d9bd var(--bd), #cdbd9a 100%);
}

.paper {
  position: absolute;
  background: var(--paper) center / cover no-repeat;
  color: var(--ink);
  overflow: hidden;
}

/* the valley where the two pages meet */
.gutter {
  position: absolute;
  top: 0;
  bottom: 0;
  width: 13%;
  pointer-events: none;
}
.gutter.left { right: 0; background: linear-gradient(270deg, rgba(60, 38, 16, .5), rgba(74, 48, 22, .14) 30%, rgba(74, 48, 22, 0)); }
.gutter.right { left: 0; background: linear-gradient(90deg, rgba(60, 38, 16, .5), rgba(74, 48, 22, .14) 30%, rgba(74, 48, 22, 0)); }

.shade {
  position: absolute;
  inset: 0;
  background: #0d0703;
  opacity: 0;
  pointer-events: none;
}

.pops {
  position: absolute;
  transform-style: preserve-3d;
  pointer-events: none;
}

/* ---------- cover ---------- */

.cover {
  position: absolute;
  inset: 0;
  background: var(--board) center / cover no-repeat;
}
.cover::after {
  content: "";
  position: absolute;
  inset: 0;
  background: linear-gradient(115deg, rgba(255, 255, 255, .10), rgba(255, 255, 255, 0) 38%, rgba(0, 0, 0, .18));
  pointer-events: none;
}
.ribbon {
  position: absolute;
  background:
    repeating-linear-gradient(0deg, rgba(255, 255, 255, .05) 0 1px, rgba(0, 0, 0, .05) 1px 2px),
    linear-gradient(90deg, #4f0d18, #8d1a2b 16%, #b23043 34%, #84162a 54%, #9c2134 76%, #4f0d18);
  box-shadow: 0 1px 4px rgba(0, 0, 0, .55), 0 0 1px rgba(0, 0, 0, .6);
  transition: transform .75s cubic-bezier(.6, 0, .85, .4), opacity .5s .3s;
}
.ribbon.v { left: 50%; top: -2%; bottom: -2%; width: 7.6%; margin-left: -3.8%; }
.bow {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 44%;
  transform: translate(-50%, -50%);
  filter: drop-shadow(0 3px 4px rgba(0, 0, 0, .5));
  transition: transform .6s cubic-bezier(.5, -0.4, .7, .6), opacity .35s .3s;
  pointer-events: none;
}
.knot {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 10%;
  aspect-ratio: 1;
  transform: translate(-50%, -50%) rotate(45deg);
  border-radius: 22%;
  background: linear-gradient(135deg, #cf4252, #7d1422);
  box-shadow: 0 2px 4px rgba(0, 0, 0, .5);
  transition: transform .5s, opacity .35s .2s;
}
.tagwrap {
  position: absolute;
  left: 57%;
  top: 54%;
  width: 21%;
  transform-origin: 20% 6%;
  transform: rotate(14deg);
  filter: drop-shadow(0 2px 3px rgba(0, 0, 0, .45));
  transition: transform .9s cubic-bezier(.4, 0, .7, 1), opacity .5s .5s;
}
.tagwrap img { display: block; width: 100%; }
.tagwrap span {
  position: absolute;
  left: 14%;
  right: 22%;
  top: 36%;
  text-align: center;
  color: var(--ink);
  font-size: calc(var(--W) * .042);
  line-height: 1.3;
  white-space: pre-line;
}
.cover.untied .ribbon.v { transform: translateY(125%); opacity: 0; }
.cover.untied .bow { transform: translate(-50%, -50%) scale(1.5) rotate(-18deg); opacity: 0; }
.cover.untied .knot { transform: translate(-50%, -50%) rotate(45deg) scale(.2); opacity: 0; }
.cover.untied .tagwrap { transform: rotate(48deg) translate(60%, 130%); opacity: 0; }

/* ---------- flat things printed or lying on a page ---------- */

.flat {
  position: absolute;
  transform-origin: 50% 50%;
  white-space: pre-line;
  text-align: center;
  line-height: 1.55;
}
.flat img { display: block; width: 100%; height: auto; }
.flat.decal { mix-blend-mode: multiply; }
.flat.script { font-family: var(--script); line-height: 1.15; }
.foil {
  background: var(--foil);
  background-size: 220% 100%;
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  filter: drop-shadow(0 .6px 0 rgba(84, 56, 12, .55));
  animation: foil 7s ease-in-out infinite alternate;
}
@keyframes foil { from { background-position: 0% 0; } to { background-position: 100% 0; } }

.ink .line {
  display: block;
  clip-path: inset(-12% 100% -12% 0);
  transition: clip-path 1.25s cubic-bezier(.45, .05, .55, .95);
}
.ink.on .line { clip-path: inset(-12% -2% -12% 0); }

/* ---------- pop-up pieces ---------- */

.pop {
  position: absolute;
  width: 0;
  height: 0;
  transform-style: preserve-3d;
}

.sh {
  position: absolute;
  transform-origin: 50% 100%;
  filter: brightness(0) blur(3.5px);
  opacity: 0;
  pointer-events: none;
  will-change: transform, opacity;
}

.contact {
  position: absolute;
  border-radius: 50%;
  background: radial-gradient(closest-side, rgba(24, 12, 2, .7), rgba(24, 12, 2, 0));
  opacity: 0;
  pointer-events: none;
}

.stand {
  position: absolute;
  transform-origin: 50% 100%;
  transform-style: preserve-3d;
  will-change: transform;
}
.stand .front {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
}
.stand .bk {
  position: absolute;
  left: 0;
  top: 0;
  transform: rotateY(180deg);
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  background: linear-gradient(180deg, #f1e7d2, #e2d4b8);
  -webkit-mask: center / 100% 100% no-repeat;
  mask: center / 100% 100% no-repeat;
}
.stand .body {
  position: absolute;
  left: 0;
  top: 0;
  transform-origin: 50% 100%;
}
.stand .body > img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  pointer-events: none;
}
.stand .stick {
  position: absolute;
  width: 5px;
  margin-left: -2.5px;
  bottom: 0;
  background: linear-gradient(90deg, #dccfb2, #fbf4e3 45%, #cfc09f);
  border-radius: 1px;
}

.prop {
  position: absolute;
  pointer-events: none;
  filter: drop-shadow(2px 4px 3px rgba(0, 0, 0, .6));
}

/* ---------- overlays ---------- */

#dark, #glow, #fx {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
}
#glow { mix-blend-mode: screen; }
[data-lite] #glow { mix-blend-mode: normal; opacity: .85; }
[data-lite="2"] .sh { filter: brightness(0) opacity(.8); }

#ui {
  position: absolute;
  inset: 0;
  pointer-events: none;
}
#caption {
  position: absolute;
  left: 12%;
  right: 12%;
  bottom: 26px;
  text-align: center;
  font-size: 17px;
  line-height: 1.45;
  letter-spacing: .08em;
  color: #f6ead0;
  text-shadow: 0 1px 10px rgba(0, 0, 0, .95), 0 0 3px rgba(0, 0, 0, .9);
  white-space: pre-line;
}
#caption span { opacity: 0; display: inline-block; transform: translateY(6px); animation: capin .7s cubic-bezier(.2, .7, .3, 1) forwards; }
#caption.out { opacity: 0; transition: opacity .45s; }
@keyframes capin { to { opacity: 1; transform: none; } }

#hint {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 7px;
  text-align: center;
  font-size: 11.5px;
  letter-spacing: .3em;
  text-indent: .3em;
  color: rgba(246, 234, 208, .62);
  text-shadow: 0 1px 6px rgba(0, 0, 0, .9);
  opacity: 0;
  transition: opacity .8s;
}
#hint.on { opacity: 1; animation: breathe 2.8s ease-in-out infinite; }
@keyframes breathe { 0%, 100% { opacity: .42; } 50% { opacity: .95; } }

.corner, #mute {
  width: 38px;
  height: 38px;
  border: 0;
  border-radius: 50%;
  background: rgba(0, 0, 0, .3);
  color: rgba(246, 234, 208, .85);
  pointer-events: auto;
  display: flex;
  align-items: center;
  justify-content: center;
}
#mute {
  position: absolute;
  right: calc(env(safe-area-inset-right, 0px) + 12px);
  top: 12px;
  gap: 3px;
}
#mute[hidden] { display: none; }
#mute i { width: 2.5px; height: 12px; background: rgba(246, 234, 208, .8); border-radius: 2px; animation: eq 1s ease-in-out infinite alternate; }
#mute i:nth-child(2) { animation-delay: -.4s; }
#mute i:nth-child(3) { animation-delay: -.7s; }
#mute.off i { animation: none; height: 3px; }
@keyframes eq { from { height: 4px; } to { height: 14px; } }

#corner {
  position: absolute;
  left: calc(env(safe-area-inset-left, 0px) + 12px);
  top: 12px;
  display: flex;
  gap: 10px;
}
.corner { opacity: 0; transform: scale(.6); transition: opacity .6s, transform .6s cubic-bezier(.2, 1.6, .4, 1); pointer-events: none; }
.corner.on { opacity: 1; transform: none; pointer-events: auto; }
.corner svg { width: 21px; height: 21px; display: block; }
.corner.pulse::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: 50%;
  border: 1.5px solid rgba(246, 234, 208, .7);
  animation: ring 2.4s ease-out infinite;
}
.corner { position: relative; }
@keyframes ring { from { transform: scale(1); opacity: .9; } to { transform: scale(1.7); opacity: 0; } }
.turned #mute { right: 12px; }
.turned #corner { left: 12px; }

/* ---------- the dark screen before the lamp goes on ---------- */

#veil {
  position: fixed;
  inset: 0;
  background: #050302;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 18px;
  z-index: 20;
  transition: opacity 1.4s ease;
}
#veil.gone { opacity: 0; pointer-events: none; }
#veil-msg {
  font-size: 15px;
  letter-spacing: .5em;
  text-indent: .5em;
  color: rgba(246, 234, 208, .78);
}
#veil.ready #veil-msg { animation: breathe 2.6s ease-in-out infinite; }
#veil-bar { width: 96px; height: 1px; background: rgba(246, 234, 208, .16); overflow: hidden; }
#veil-bar i { display: block; width: 100%; height: 100%; background: var(--gold); transform: scaleX(0); transform-origin: 0 50%; transition: transform .3s; }
#veil.ready #veil-bar { opacity: 0; transition: opacity .5s; }

/* shown only while the phone is upright: turn it on its side */
#veil-turn { display: none; flex-direction: column; align-items: center; gap: 14px; margin-bottom: 14px; }
.turned #veil-turn { display: flex; }
#veil-turn svg { width: 54px; height: 54px; color: rgba(246, 234, 208, .8); animation: turnphone 2.6s cubic-bezier(.6, 0, .3, 1) infinite; }
#veil-turn span { font-size: 16px; letter-spacing: .34em; text-indent: .34em; color: rgba(246, 234, 208, .9); }
@keyframes turnphone { 0%, 18% { transform: rotate(0); } 52%, 82% { transform: rotate(-90deg); } 100% { transform: rotate(0); } }

/* ---------- modal ---------- */

#modal {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(8, 5, 3, .72);
  backdrop-filter: blur(3px);
  -webkit-backdrop-filter: blur(3px);
  z-index: 10;
  opacity: 0;
  transition: opacity .35s;
}
#modal[hidden] { display: none; }
#modal.show { opacity: 1; }

.polaroid {
  width: min(calc(var(--vh, 400px) * .66), 320px);
  padding: 11px 11px 14px;
  background: linear-gradient(180deg, #fdfaf3, #f3ecdd);
  border-radius: 2px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, .7), 0 2px 6px rgba(0, 0, 0, .45);
  transform: rotate(-2deg);
  transition: transform .55s cubic-bezier(.2, 1.15, .3, 1);
}
.polaroid .pic {
  width: 100%;
  aspect-ratio: 1;
  background: #cfc3ae center / cover no-repeat;
  box-shadow: inset 0 0 0 1px rgba(0, 0, 0, .08), inset 0 0 18px rgba(0, 0, 0, .18);
}
.polaroid p {
  margin-top: 10px;
  min-height: 1.5em;
  text-align: center;
  color: var(--ink);
  font-size: 16px;
  line-height: 1.45;
}
