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

html, body {
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  background: #0e0d0c;
  font-family: 'EB Garamond', serif;
  touch-action: none;
  position: fixed;
  width: 100%;
  inset: 0;
}

#stage {
  position: relative;
  width: 100%;
  height: 100%;
  height: 100dvh;
  display: flex;
  align-items: center;
  justify-content: center;
  background: radial-gradient(ellipse at center, #171513 0%, #0a0908 100%);
  overflow: hidden;
  opacity: 1;
  transition: opacity 0.5s ease;
}

#stage.pre-load {
  opacity: 0;
  pointer-events: none;
}

/* ---------- LOADING GATE ---------- */
#loadGate {
  position: fixed;
  inset: 0;
  z-index: 100;
  background: #0e0d0c;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: opacity 0.5s ease;
}

#loadGate.hide {
  opacity: 0;
  pointer-events: none;
}

.load-inner {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1rem;
}

.load-spinner {
  width: 34px;
  height: 34px;
  border-radius: 50%;
  border: 2px solid rgba(212,175,106,0.2);
  border-top-color: rgba(212,175,106,0.85);
  animation: loadSpin 0.9s linear infinite;
}

@keyframes loadSpin {
  to { transform: rotate(360deg); }
}

.load-text {
  font-family: 'Caveat', cursive;
  color: rgba(240,230,210,0.55);
  font-size: 1.2rem;
  letter-spacing: 0.5px;
}

.load-enter-btn {
  display: none;
  font-family: 'Caveat', cursive;
  font-size: 1.3rem;
  color: #1a1408;
  background: linear-gradient(160deg, #d4af6a, #a5793a);
  border: none;
  border-radius: 30px;
  padding: 0.7rem 1.8rem;
  cursor: pointer;
  letter-spacing: 0.5px;
  box-shadow: 0 6px 16px rgba(0,0,0,0.4);
  transition: transform 0.15s ease;
}

.load-enter-btn:active { transform: scale(0.95); }

/* once assets are ready, the spinner/text hide and the button takes over */
#loadGate.ready .load-spinner,
#loadGate.ready .load-text {
  display: none;
}

#loadGate.ready .load-enter-btn {
  display: block;
}

/* ---------- STAGE BACKGROUND GRID CORNERS ---------- */
/* these sit on the dark stage behind the notebook, in all four screen corners */
.grid-corner {
  position: absolute;
  width: 130px;
  height: 130px;
  opacity: 0.14;
  pointer-events: none;
  z-index: 1;
  background-image:
    repeating-linear-gradient(to right, rgba(255,255,255,0.55) 0, rgba(255,255,255,0.55) 1px, transparent 1px, transparent 15px),
    repeating-linear-gradient(to bottom, rgba(255,255,255,0.55) 0, rgba(255,255,255,0.55) 1px, transparent 1px, transparent 15px);
}
.grid-corner.tl { top: 0; left: 0; -webkit-mask-image: linear-gradient(135deg, #000 35%, transparent 85%); mask-image: linear-gradient(135deg, #000 35%, transparent 85%); }
.grid-corner.tr { top: 0; right: 0; -webkit-mask-image: linear-gradient(-135deg, #000 35%, transparent 85%); mask-image: linear-gradient(-135deg, #000 35%, transparent 85%); }
.grid-corner.bl { bottom: 0; left: 0; -webkit-mask-image: linear-gradient(45deg, #000 35%, transparent 85%); mask-image: linear-gradient(45deg, #000 35%, transparent 85%); }
.grid-corner.br { bottom: 0; right: 0; -webkit-mask-image: linear-gradient(-45deg, #000 35%, transparent 85%); mask-image: linear-gradient(-45deg, #000 35%, transparent 85%); }

/* ---------- COVER ---------- */
#cover {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 30;
  transition: transform 1.1s cubic-bezier(.7,0,.2,1), opacity 1s ease;
}

#cover.opened {
  transform: translateX(-100%) rotateY(-15deg);
  opacity: 0;
  pointer-events: none;
}

.book {
  /* sized to match the cover photo's real aspect ratio (700:935) so background-size: cover
     never has to crop into the image — on narrow phone screens the old fixed width/height
     combo could end up proportionally narrower than the photo, slicing text off the edge. */
  width: min(86vw, 360px);
  height: auto;
  aspect-ratio: 700 / 935;
  max-height: 78vh;
  background-image: url('../img/cover.jpg');
  background-size: cover;
  background-position: center;
  background-repeat: no-repeat;
  border-radius: 3px 10px 10px 3px;
  box-shadow:
    0 25px 60px rgba(0,0,0,0.6),
    inset -3px 0 8px rgba(0,0,0,0.35),
    inset 0 0 0 1px rgba(255,255,255,0.03);
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 2rem;
  overflow: hidden;
}

.book::before {
  content: '';
  position: absolute;
  left: 0; top: 0; bottom: 0;
  width: 14px;
  background: linear-gradient(90deg, rgba(0,0,0,0.4), transparent);
  border-radius: 3px 0 0 3px;
  z-index: 2;
}

.lock {
  width: 62px;
  height: 62px;
  background: linear-gradient(160deg, #d4af6a, #a5793a);
  border-radius: 8px;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: 0 6px 16px rgba(0,0,0,0.55), inset 0 1px 1px rgba(255,255,255,0.4), 0 0 0 3px rgba(0,0,0,0.15);
  cursor: pointer;
  transition: transform 0.15s ease;
  border: none;
  position: relative;
  z-index: 3;
}

.lock:active { transform: scale(0.93); }

.lock svg { width: 30px; height: 30px; }

.lock-hint {
  font-family: 'Caveat', cursive;
  color: rgba(255,255,255,0.85);
  font-size: 1.05rem;
  margin-top: 1rem;
  letter-spacing: 0.3px;
  text-shadow: 0 2px 6px rgba(0,0,0,0.6);
  position: relative;
  z-index: 3;
}

/* ---------- PASSWORD PANEL ---------- */
#passPanel {
  position: absolute;
  inset: 0;
  z-index: 40;
  background: rgba(5,4,4,0.94);
  display: none;
  align-items: center;
  justify-content: center;
  flex-direction: column;
  padding: 2rem;
  opacity: 0;
  transition: opacity 0.4s ease;
}

#passPanel.show { display: flex; opacity: 1; }

.pass-card {
  width: min(88vw, 340px);
  background: #16130f;
  border: 1px solid rgba(212,175,106,0.25);
  border-radius: 14px;
  padding: 2.2rem 1.8rem;
  text-align: center;
  position: relative;
}

.pass-icon {
  width: 44px;
  height: 44px;
  margin: 0 auto 1.2rem;
  opacity: 0.7;
}

.pass-label {
  font-family: 'Caveat', cursive;
  color: rgba(255,255,255,0.55);
  font-size: 1.05rem;
  margin-bottom: 1.2rem;
}

#passInput {
  width: 100%;
  background: rgba(255,255,255,0.05);
  border: 1px solid rgba(255,255,255,0.15);
  border-radius: 8px;
  padding: 0.85rem 1rem;
  color: #f0e6d2;
  font-family: 'EB Garamond', serif;
  font-size: 1.1rem;
  letter-spacing: 2px;
  text-align: center;
  outline: none;
  transition: border-color 0.2s;
}

#passInput:focus { border-color: rgba(212,175,106,0.6); }

.pass-submit {
  margin-top: 1.1rem;
  width: 100%;
  background: linear-gradient(160deg, #d4af6a, #a5793a);
  border: none;
  border-radius: 8px;
  padding: 0.75rem;
  color: #1a1408;
  font-family: 'EB Garamond', serif;
  font-weight: 500;
  font-size: 1rem;
  cursor: pointer;
  transition: transform 0.1s;
}

.pass-submit:active { transform: scale(0.97); }

.pass-close {
  position: absolute;
  top: 12px;
  right: 14px;
  color: rgba(255,255,255,0.35);
  font-size: 1.3rem;
  cursor: pointer;
  line-height: 1;
  background: none;
  border: none;
}

/* ---------- BUBBLES ---------- */
#bubbleZone {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 50;
}

.bubble {
  position: absolute;
  max-width: 240px;
  background: #f0e6d2;
  color: #2a2118;
  padding: 0.65rem 1rem;
  border-radius: 16px;
  font-family: 'Kalam', cursive;
  font-size: 0.9rem;
  line-height: 1.4;
  box-shadow: 0 6px 18px rgba(0,0,0,0.4);
  animation: bubbleIn 0.4s cubic-bezier(.2,1.4,.4,1) forwards, bubbleOut 0.4s ease forwards;
  animation-delay: 0s, 2.6s;
  pointer-events: auto;
}

.bubble.corner-tl { top: 5%; left: 5%; border-radius: 4px 16px 16px 16px; }
.bubble.corner-tr { top: 5%; right: 5%; border-radius: 16px 4px 16px 16px; }
.bubble.corner-bl { bottom: 8%; left: 5%; border-radius: 16px 16px 16px 4px; }
.bubble.corner-br { bottom: 8%; right: 5%; border-radius: 16px 16px 4px 16px; }

.bubble.special {
  background: #1c1812;
  color: #e8d9b8;
  border: 1px solid rgba(212,175,106,0.4);
}

.bubble.playable {
  cursor: pointer;
  padding-right: 2.6rem;
}

.bubble .sun { color: #d98b3a; font-weight: 700; }
.bubble .moon { color: #9fb3d9; font-weight: 700; }
.bubble.italic-line { font-style: italic; }
.bubble .kr { font-family: 'EB Garamond', serif; letter-spacing: 1px; }

.play-icon {
  position: absolute;
  right: 0.7rem;
  top: 50%;
  transform: translateY(-50%);
  width: 22px;
  height: 22px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.play-icon svg { width: 16px; height: 16px; }

.play-icon.playing svg .bar {
  animation: eq 0.7s ease-in-out infinite alternate;
}
.play-icon.playing svg .bar:nth-child(2) { animation-delay: 0.15s; }
.play-icon.playing svg .bar:nth-child(3) { animation-delay: 0.3s; }

@keyframes eq {
  0% { transform: scaleY(0.4); }
  100% { transform: scaleY(1); }
}

@keyframes bubbleIn {
  0% { opacity: 0; transform: scale(0.8); }
  100% { opacity: 1; transform: scale(1); }
}

@keyframes bubbleOut {
  0% { opacity: 1; }
  100% { opacity: 0; }
}

.bubble.playable {
  animation: bubbleIn 0.4s cubic-bezier(.2,1.4,.4,1) forwards;
}

.shake { animation: shake 0.4s; }
@keyframes shake {
  0%,100% { transform: translateX(0); }
  20% { transform: translateX(-8px); }
  40% { transform: translateX(7px); }
  60% { transform: translateX(-5px); }
  80% { transform: translateX(4px); }
}

/* ---------- BACKDROP (behind calendar + all date pages) ---------- */
#backdrop {
  position: absolute;
  inset: 0;
  z-index: 5;
  overflow: hidden;
  display: none;
  pointer-events: none;
}
#backdrop.show { display: block; }

#backdrop .cloud-track {
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  width: 200%;
  display: flex;
  animation: driftClouds 90s linear infinite;
}

#backdrop .cloud-track img {
  height: 100%;
  width: 50%;
  object-fit: cover;
  object-position: center 40%;
  display: block;
}

@keyframes driftClouds {
  from { transform: translateX(0); }
  to { transform: translateX(-50%); }
}

/* ---------- CRUMPLE STOP-MOTION ---------- */
#crumpleScreen {
  position: absolute;
  inset: 0;
  z-index: 20;
  display: none;
  align-items: center;
  justify-content: center;
  background: transparent;
}

#crumpleScreen.show { display: flex; }

#afsosWrap {
  position: absolute;
  z-index: 2;
  display: flex;
  align-items: center;
  justify-content: center;
  width: min(80vw, 480px);
  animation: afsosFadeOut 0.7s ease forwards;
  animation-delay: 2.1s;
}

#afsosImg {
  width: 100%;
  height: auto;
  display: block;
  pointer-events: none;
  user-select: none;
  clip-path: inset(0 100% 0 0);
  animation: afsosReveal 1.4s cubic-bezier(.4,0,.2,1) forwards;
}

@keyframes afsosReveal {
  to { clip-path: inset(0 0 0 0); }
}

@keyframes afsosFadeOut {
  to { opacity: 0; visibility: hidden; }
}

#crumpleFrame {
  /* fixed size on every frame — since all frames now share one aligned canvas (1000x1300),
     locking width/height here means nothing stretches or resizes between frames, only the
     browser scales the whole fixed-aspect image down to fit small screens. Widened by ~2cm
     total (1cm each side) versus the previous size. */
  width: min(78vw, 420px);
  width: min(calc(78vw + 2cm), calc(420px + 2cm));
  height: auto;
  aspect-ratio: 1000 / 1300;
  object-fit: contain;
  pointer-events: none;
  user-select: none;
}

/* ---------- CALENDAR MENU ---------- */
/* The calendar screen uses the final (fully unfolded) stop-motion frame as its actual
   page image, with the date cards placed directly on top of that photo. */
#calendar {
  position: absolute;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  background: transparent;
  z-index: 10;
}

#calendar.show { display: flex; }

.calendar-page-bg {
  position: relative;
  width: min(78vw, 420px);
  width: min(calc(78vw + 2cm), calc(420px + 2cm));
  aspect-ratio: 1000 / 1300;
  display: flex;
  align-items: center;
  justify-content: center;
}

.calendar-paper-img {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: contain;
  pointer-events: none;
  user-select: none;
  filter: drop-shadow(0 18px 40px rgba(0,0,0,0.5));
}

.calendar-content {
  position: relative;
  z-index: 2;
  width: 72%;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 1rem;
  border-radius: 10px;
  background: rgba(20,16,10,0.28);
  backdrop-filter: blur(1px);
}

.calendar-title {
  font-family: 'Caveat', cursive;
  font-weight: 700;
  font-size: 2rem;
  color: #fbf3e2;
  margin-bottom: 1.3rem;
  letter-spacing: 0.5px;
  text-shadow: 0 2px 8px rgba(0,0,0,0.7);
}

.date-grid {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 0.75rem;
  width: 100%;
}

.date-card {
  background: rgba(15,12,8,0.55);
  border: 1px solid rgba(212,175,106,0.35);
  border-radius: 8px;
  padding: 0.9rem 0.5rem;
  text-align: center;
  cursor: pointer;
  transition: transform 0.15s ease, background 0.15s ease;
  font-family: 'Patrick Hand', 'Kalam', cursive;
  backdrop-filter: blur(2px);
}

.date-card:active { transform: scale(0.95); background: rgba(15,12,8,0.75); }

.date-card.visited {
  background: rgba(165, 121, 58, 0.28);
  border-color: rgba(212,175,106,0.7);
}

.date-card.visited .date-num,
.date-card.visited .date-label {
  color: #f0e0b8;
}

.date-card .date-num {
  display: block;
  font-size: 1.1rem;
  color: #f5ead2;
  font-weight: 700;
  letter-spacing: 0.5px;
  text-shadow: 0 1px 4px rgba(0,0,0,0.6);
}

.date-card .date-label {
  display: block;
  font-size: 0.8rem;
  color: #d9cbb0;
  margin-top: 0.3rem;
  font-style: italic;
  text-shadow: 0 1px 4px rgba(0,0,0,0.6);
}

/* ---------- LETTER PROGRESS LOCK ---------- */
/* ---------- AMBIENCE VISUALIZER ---------- */
#ambienceViz {
  position: fixed;
  bottom: 3vh;
  left: 5vw;
  z-index: 60;
  display: none;
  align-items: flex-end;
  gap: 3px;
  height: 18px;
  opacity: 0.55;
  pointer-events: none;
}

#ambienceViz.show { display: flex; }

.viz-bar {
  width: 3px;
  height: 4px;
  background: rgba(212,175,106,0.85);
  border-radius: 2px;
  transform-origin: bottom;
  transition: height 0.1s ease;
}

.letter-lock {
  position: absolute;
  bottom: 3vh;
  left: 50%;
  transform: translateX(-50%);
  z-index: 12;
  display: flex;
  align-items: center;
  gap: 0.55rem;
  background: rgba(10,9,8,0.72);
  border: 1px solid rgba(212,175,106,0.3);
  border-radius: 30px;
  padding: 0.55rem 1.1rem 0.55rem 0.85rem;
  backdrop-filter: blur(4px);
  cursor: pointer;
  transition: transform 0.15s ease, border-color 0.3s ease, background 0.3s ease;
}

.letter-lock:active { transform: translateX(-50%) scale(0.96); }

.letter-lock-icon {
  width: 19px;
  height: 19px;
  color: #a5793a;
  transition: color 0.3s ease;
}

.letter-lock-text {
  font-family: 'Caveat', cursive;
  font-weight: 700;
  font-size: 1.15rem;
  color: rgba(240,230,210,0.85);
  letter-spacing: 0.5px;
}

.letter-lock-progress {
  font-family: 'EB Garamond', serif;
  font-size: 0.8rem;
  color: rgba(240,230,210,0.5);
  border-left: 1px solid rgba(255,255,255,0.15);
  padding-left: 0.55rem;
}

/* unlocked state: gold glow, open-lock icon swap handled via JS (different path), text brightens */
.letter-lock.unlocked {
  border-color: rgba(212,175,106,0.75);
  background: rgba(30,22,10,0.82);
  box-shadow: 0 0 18px rgba(212,175,106,0.25);
}

.letter-lock.unlocked .letter-lock-icon { color: #d4af6a; }
.letter-lock.unlocked .letter-lock-text { color: #f0e0b8; }

.letter-lock.unlocked .letter-lock-progress {
  color: rgba(212,175,106,0.7);
}

.letter-lock.shake {
  animation: letterLockShake 0.4s;
}

@keyframes letterLockShake {
  0%,100% { transform: translateX(-50%); }
  20% { transform: translateX(calc(-50% - 8px)); }
  40% { transform: translateX(calc(-50% + 7px)); }
  60% { transform: translateX(calc(-50% - 5px)); }
  80% { transform: translateX(calc(-50% + 4px)); }
}

/* ---------- SINGLE DATE PAGE VIEWER ---------- */
#dateViewer {
  position: absolute;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  background: transparent;
  z-index: 15;
}

#dateViewer.show { display: flex; }

#letterViewer {
  position: absolute;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  background: transparent;
  z-index: 25;
}

#letterViewer.show { display: flex; }

.back-arrow {
  position: absolute;
  top: 4vh;
  left: 5vw;
  z-index: 30;
  width: 42px;
  height: 42px;
  border-radius: 50%;
  background: rgba(255,255,255,0.08);
  border: 1px solid rgba(255,255,255,0.18);
  color: rgba(255,255,255,0.75);
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: background 0.15s ease, transform 0.15s ease;
}

.back-arrow:active { transform: scale(0.9); background: rgba(255,255,255,0.16); }
.back-arrow svg { width: 20px; height: 20px; }

/* A4 aspect ratio is 1:1.414. Sized to leave visible backdrop margin around it on phones,
   while staying comfortably readable. Height-driven since phones are taller than wide. */
.page-stack {
  position: relative;
  height: min(74vh, 780px);
  width: min(calc(min(74vh, 780px) / 1.414), 84vw);
}

.page {
  position: absolute;
  inset: 0;
  background:
    repeating-linear-gradient(
      to bottom,
      transparent, transparent 36px,
      rgba(120,140,170,0.35) 36px, rgba(120,140,170,0.35) 37px
    ),
    linear-gradient(135deg, #f4ecd8 0%, #ede2c8 50%, #f0e8d4 100%);
  border-radius: 4px;
  box-shadow: 0 20px 50px rgba(0,0,0,0.55);
  padding: 4.5vh 6% 3vh 9%;
  overflow: hidden;
  transform-origin: left center;
  transition: transform 0.55s cubic-bezier(.5,0,.2,1), opacity 0.5s ease;
  display: flex;
  flex-direction: column;
  clip-path: polygon(
    0.6% 1.2%, 8% 0%, 19% 0.8%, 33% 0%, 47% 0.6%, 61% 0%, 76% 0.9%, 89% 0%, 99.4% 1.4%,
    100% 9%, 99.3% 22%, 100% 36%, 99.5% 51%, 100% 65%, 99.4% 79%, 100% 91%, 99.2% 98.8%,
    91% 100%, 78% 99.3%, 64% 100%, 50% 99.4%, 36% 100%, 22% 99.3%, 9% 100%, 0.4% 98.7%,
    1% 88%, 0.3% 74%, 1.1% 60%, 0% 46%, 0.8% 32%, 0% 18%
  );
}

.page::before {
  content: '';
  position: absolute;
  left: 7%;
  top: 0;
  bottom: 0;
  width: 1px;
  background: rgba(200,80,80,0.25);
}

.page::after {
  content: '';
  position: absolute;
  inset: 0;
  background:
    radial-gradient(ellipse at top left, rgba(255,255,255,0.15), transparent 60%),
    radial-gradient(circle at 12% 8%, rgba(140,110,60,0.16) 0%, transparent 14%),
    radial-gradient(circle at 88% 5%, rgba(140,110,60,0.13) 0%, transparent 12%),
    radial-gradient(circle at 6% 92%, rgba(140,110,60,0.15) 0%, transparent 13%),
    radial-gradient(circle at 93% 95%, rgba(140,110,60,0.12) 0%, transparent 11%);
  pointer-events: none;
  mix-blend-mode: multiply;
}

.tear-drop {
  position: absolute;
  pointer-events: none;
  background: radial-gradient(circle at 38% 28%, rgba(255,255,255,0.3), rgba(140,120,80,0.22) 45%, rgba(105,88,55,0.34) 75%, rgba(90,74,46,0.4) 100%);
  box-shadow: inset 0 0 8px rgba(80,64,38,0.35);
  mix-blend-mode: multiply;
  filter: blur(0.3px);
}

.tear-streak {
  position: absolute;
  width: 6px;
  pointer-events: none;
  background: linear-gradient(to bottom, rgba(130,110,70,0.26), rgba(130,110,70,0.06));
  mix-blend-mode: multiply;
  filter: blur(0.4px);
}

.wrinkle-fold {
  position: absolute;
  pointer-events: none;
  background: linear-gradient(115deg, transparent 46%, rgba(90,75,50,0.1) 49%, rgba(255,255,255,0.25) 51%, transparent 54%);
  mix-blend-mode: multiply;
}

.page-number {
  position: absolute;
  bottom: 2vh;
  right: 5%;
  font-family: 'Caveat', cursive;
  color: rgba(60,50,40,0.4);
  font-size: 1.15rem;
}

.page-heading {
  font-family: 'Caveat', cursive;
  font-weight: 700;
  font-size: 1.6rem;
  color: #6e1c1c;
  margin-bottom: 0.6rem;
  letter-spacing: 0.5px;
}

.page-text {
  font-family: 'Patrick Hand', 'Kalam', cursive;
  font-weight: 400;
  color: #2f2618;
  font-size: 1.32rem;
  line-height: 36px;
  white-space: pre-wrap;
  overflow: hidden;
  letter-spacing: 0.3px;
}

.page-text .word {
  display: inline-block;
}

.page-text .ch {
  display: inline-block;
  transform-origin: bottom center;
}

.page.current { z-index: 5; transform: rotateY(0deg) translateX(0); opacity: 1; }
