:root {
  --bg: #f6f1e7;
  --ink: #1f1f1f;
  --muted: rgba(0, 0, 0, .56);
  --shadow: 0 18px 45px rgba(0, 0, 0, .16);
  --radius: 20px;

  --page-w: min(92vw, 480px);
  --page-h: min(80vh, 760px);

  --turn-dur: 520ms;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
  font-family: ui-serif, Georgia, "Times New Roman", serif;
  color: var(--ink);
  overflow: hidden;
  background: var(--bg);
}

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

.hidden {
  display: none !important;
}

.paper-bg {
  position: fixed;
  inset: 0;
  background:
    radial-gradient(900px 700px at 20% 8%, rgba(0, 0, 0, .06), transparent 60%),
    radial-gradient(900px 700px at 88% 92%, rgba(0, 0, 0, .05), transparent 60%),
    linear-gradient(180deg, rgba(255, 255, 255, .45), rgba(255, 255, 255, .08));
  pointer-events: none;
}

/* Lock */
.lock-screen {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 18px;
  z-index: 50;
}

.lock-card {
  width: min(92vw, 420px);
  background: rgba(255, 255, 255, .78);
  border: 1px solid rgba(0, 0, 0, .08);
  border-radius: 22px;
  box-shadow: var(--shadow);
  padding: 22px 18px;
  backdrop-filter: blur(7px);
  text-align: center;
}

.stamp {
  width: 54px;
  height: 54px;
  display: grid;
  place-items: center;
  margin: 0 auto 10px;
  border-radius: 14px;
  background: rgba(255, 255, 255, .9);
  border: 1px solid rgba(0, 0, 0, .08);
}

.lock-card h1 {
  margin: 6px 0 6px;
  font-size: 1.55rem;
}

.subtitle {
  margin: 0 0 14px;
  color: var(--muted);
  font-size: .98rem;
}

#secretCode {
  width: 100%;
  padding: 14px;
  border-radius: 14px;
  border: 1px solid rgba(0, 0, 0, .12);
  background: rgba(255, 255, 255, .95);
  outline: none;
  font-size: 1.05rem;
  text-align: center;
  letter-spacing: .22em;
}

.btn-primary {
  width: 100%;
  margin-top: 12px;
  padding: 12px 14px;
  border: 0;
  border-radius: 14px;
  background: rgba(0, 0, 0, .86);
  color: white;
  font-size: 1rem;
  cursor: pointer;
}

.btn-primary:active {
  transform: translateY(1px);
}

.hint {
  margin: 10px 0 0;
  color: rgba(0, 0, 0, .55);
  font-size: .92rem;
}

.error {
  margin: 8px 0 0;
  color: #8d1e1e;
  min-height: 1.2em;
  font-size: .95rem;
}

.mini-note {
  margin-top: 14px;
  display: flex;
  gap: 10px;
  align-items: center;
  justify-content: center;
  padding: 10px 12px;
  background: rgba(255, 255, 255, .7);
  border: 1px dashed rgba(0, 0, 0, .18);
  border-radius: 14px;
  color: rgba(0, 0, 0, .62);
  font-size: .92rem;
}

/* App */
.app {
  height: 100%;
  display: grid;
  grid-template-rows: auto 1fr auto;
  padding: env(safe-area-inset-top) 14px env(safe-area-inset-bottom);
}

.topbar {
  height: 74px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 14px;
  padding: 10px 6px 6px;
}

.topbar-left {
  display: flex;
  align-items: center;
  gap: 10px;
}

.badge {
  width: 40px;
  height: 40px;
  border-radius: 14px;
  display: grid;
  place-items: center;
  background: rgba(255, 255, 255, .78);
  border: 1px solid rgba(0, 0, 0, .10);
  box-shadow: 0 10px 22px rgba(0, 0, 0, .10);
  font-weight: 700;
}

.title-main {
  font-weight: 700;
  font-size: 1.02rem;
}

.title-sub {
  font-size: .86rem;
  color: var(--muted);
}

.topbar-right {
  display: flex;
  align-items: center;
  gap: 10px;
}

.icon-btn {
  width: 42px;
  height: 42px;
  border-radius: 14px;
  border: 1px solid rgba(0, 0, 0, .12);
  background: rgba(255, 255, 255, .8);
  box-shadow: 0 10px 22px rgba(0, 0, 0, .10);
  cursor: pointer;
  font-size: 1.5rem;
  line-height: 1;
}

.icon-btn:active {
  transform: translateY(1px);
}

.page-indicator {
  font-size: .95rem;
  color: rgba(0, 0, 0, .65);
  min-width: 72px;
  text-align: center;
}

/* Stage */
.stage {
  display: grid;
  place-items: center;
  padding: 10px 0 14px;
}

.page-shell {
  width: var(--page-w);
  height: var(--page-h);
  position: relative;
  perspective: 1400px;
}

/* Cards stacked */
.page-card {
  position: absolute;
  inset: 0;
  transform-style: preserve-3d;
  transform-origin: 50% 50%;
  will-change: transform, opacity;
}

.page-card.under {
  z-index: 1;
  opacity: 0;
}

.page-card.top {
  z-index: 2;
}

.page-shell.show-under .page-card.under {
  opacity: 1;
}

.page-inner {
  position: relative;
  width: 100%;
  height: 100%;
  border-radius: var(--radius);
  overflow: hidden;
  /* keep rounded corners */
  border: 1px solid rgba(0, 0, 0, .10);
  background:
    linear-gradient(180deg, rgba(255, 255, 255, .84), rgba(255, 255, 255, .62));
  box-shadow: var(--shadow);
  padding: 18px 16px 16px;
}

/* paper texture */
.page-inner::before {
  content: "";
  position: absolute;
  inset: 0;
  background-image: url("assets/paper-texture.jpg");
  background-size: cover;
  opacity: .18;
  pointer-events: none;
}

.page-inner::after {
  content: "";
  position: absolute;
  inset: -40px;
  background: radial-gradient(closest-side, transparent 60%, rgba(0, 0, 0, .10));
  opacity: .20;
  pointer-events: none;
}

/* scroll area inside page */
.scroll {
  position: relative;
  z-index: 2;
  height: 100%;
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  padding-right: 6px;
  /* room for iOS scroll indicator */
}

.scroll::-webkit-scrollbar {
  width: 8px;
}

.scroll::-webkit-scrollbar-thumb {
  background: rgba(0, 0, 0, .12);
  border-radius: 999px;
}

/* Centered headings */
.page-head {
  margin-bottom: 10px;
  text-align: center;
}

.chapter {
  font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  font-size: .78rem;
  letter-spacing: .18em;
  color: rgba(0, 0, 0, .55);
  text-transform: uppercase;
}

.page-head h2 {
  margin: 6px 0 4px;
  font-size: 1.45rem;
  letter-spacing: .2px;
}

.date {
  font-size: .95rem;
  color: rgba(0, 0, 0, .60);
}

.story {
  margin: 12px 2px 0;
  line-height: 1.6;
  color: rgba(0, 0, 0, .80);
}

/* Photo */
.photo {
  margin: 14px auto 10px;
  width: min(96%, 390px);
  cursor: zoom-in;
  user-select: none;
}

.photo.hidden {
  display: none !important;
}

.media {
  position: relative;
}

.video {
  margin: 14px auto 10px;
  width: min(96%, 390px);
}

.video.hidden {
  display: none !important;
}

.video video {
  width: 100%;
  height: clamp(220px, 28vh, 320px);
  object-fit: cover;
  border-radius: 12px;
  display: block;
  background: rgba(0, 0, 0, .08);
}


.polaroid {
  background: rgba(255, 255, 255, .92);
  border-radius: 16px;
  padding: 12px 12px 16px;
  border: 1px solid rgba(0, 0, 0, .10);
  box-shadow: 0 16px 34px rgba(0, 0, 0, .14);
  transform: rotate(-.8deg);
}

.polaroid img {
  width: 100%;
  height: clamp(220px, 28vh, 320px);
  object-fit: cover;
  border-radius: 12px;
  display: block;
}

.polaroid figcaption {
  margin-top: 10px;
  font-size: 1rem;
  color: rgba(0, 0, 0, .66);
  text-align: center;
}

/* Note */
.note {
  margin-top: 12px;
  padding: 10px 12px;
  border-radius: 14px;
  background: rgba(255, 255, 255, .62);
  border: 1px dashed rgba(0, 0, 0, .18);
  display: flex;
  gap: 10px;
  align-items: flex-start;
  color: rgba(0, 0, 0, .68);
  font-size: .98rem;
}

.note .dot {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: rgba(0, 0, 0, .55);
  margin-top: 6px;
}

/* Letter */
.letter {
  margin-top: 10px;
}

.handwritten {
  font-family: "Comic Sans MS", "Bradley Hand", "Segoe Script", "Apple Chancery", cursive;
  font-size: 1.12rem;
  line-height: 1.65;
  color: rgba(0, 0, 0, .80);
}

.handwritten .sign {
  margin-top: 14px;
}

.handwritten .signature {
  font-weight: 700;
}

.handwritten .date-sign {
  margin-top: 6px;
  color: rgba(0, 0, 0, .62);
}

.handwritten .ps {
  margin-top: 14px;
  padding-top: 10px;
  border-top: 1px dashed rgba(0, 0, 0, .22);
  color: rgba(0, 0, 0, .74);
}

.seal {
  margin-top: 14px;
  display: flex;
  gap: 10px;
  align-items: center;
  padding: 10px 12px;
  border-radius: 16px;
  background: rgba(255, 255, 255, .66);
  border: 1px solid rgba(0, 0, 0, .10);
  color: rgba(0, 0, 0, .72);
}

/* Tape */
.tape {
  position: absolute;
  width: 120px;
  height: 44px;
  background-image: url("assets/tape.png");
  background-size: contain;
  background-repeat: no-repeat;
  opacity: .70;
  pointer-events: none;
  z-index: 4;
}

.tape-a {
  top: 8px;
  left: 10px;
  transform: rotate(-8deg);
}

.tape-b {
  top: 8px;
  right: 10px;
  transform: rotate(10deg);
}

/* Paper-like turn: top card peels away with a moving crease + shadow (no snap) */
.page-shell {
  perspective: 1600px;
}

.page-card.top {
  transform: translateZ(0);
}

/* “Paper sheet” feel: slight thickness + edge highlight */
.page-card.top .page-inner {
  box-shadow:
    0 18px 45px rgba(0, 0, 0, .16),
    0 1px 0 rgba(255, 255, 255, .55) inset;
}

/* Dynamic curl lighting during turn (overlay lives on the card, not inside scroll) */
.page-card.top::before,
.page-card.top::after {
  content: "";
  position: absolute;
  inset: -2px;
  border-radius: var(--radius);
  pointer-events: none;
  opacity: 0;
  transition: opacity 120ms linear;
  z-index: 5;
}

/* Crease highlight (a narrow bright gradient that moves) */
.page-card.top::before {
  background:
    linear-gradient(90deg,
      rgba(255, 255, 255, 0) 0%,
      rgba(255, 255, 255, .28) 40%,
      rgba(255, 255, 255, .55) 50%,
      rgba(255, 255, 255, .22) 60%,
      rgba(255, 255, 255, 0) 100%);
  filter: blur(.2px);
  mix-blend-mode: screen;
}

/* Shadow band (gives the “curl” depth) */
.page-card.top::after {
  background:
    linear-gradient(90deg,
      rgba(0, 0, 0, 0) 0%,
      rgba(0, 0, 0, .10) 38%,
      rgba(0, 0, 0, .20) 50%,
      rgba(0, 0, 0, .08) 62%,
      rgba(0, 0, 0, 0) 100%);
  filter: blur(.3px);
}

/* Turning states */
.page-card.turning-next {
  transform-origin: left center;
  animation: peelNext var(--turn-dur) cubic-bezier(.2, .8, .2, 1) forwards;
}

.page-card.turning-prev {
  transform-origin: right center;
  animation: peelPrev var(--turn-dur) cubic-bezier(.2, .8, .2, 1) forwards;
}

.page-card.turning-next::before,
.page-card.turning-next::after,
.page-card.turning-prev::before,
.page-card.turning-prev::after {
  opacity: 1;
  animation-duration: var(--turn-dur);
  animation-timing-function: cubic-bezier(.2, .8, .2, 1);
  animation-fill-mode: forwards;
}

/* Crease + shadow movement */
.page-card.turning-next::before {
  animation-name: creaseNext;
}

.page-card.turning-next::after {
  animation-name: shadowNext;
}

.page-card.turning-prev::before {
  animation-name: creasePrev;
}

.page-card.turning-prev::after {
  animation-name: shadowPrev;
}

/* Peel motion: rotate + tiny bend + tilt (feels like paper) */
@keyframes peelNext {
  0% {
    transform: rotateY(0deg) rotateZ(0deg) translateX(0);
    opacity: 1;
  }

  35% {
    transform: rotateY(-42deg) rotateZ(-.4deg) translateX(-2px);
    opacity: .96;
  }

  70% {
    transform: rotateY(-98deg) rotateZ(-.9deg) translateX(-6px);
    opacity: .55;
  }

  100% {
    transform: rotateY(-132deg) rotateZ(-1.2deg) translateX(-10px);
    opacity: 0;
  }
}

@keyframes peelPrev {
  0% {
    transform: rotateY(0deg) rotateZ(0deg) translateX(0);
    opacity: 1;
  }

  35% {
    transform: rotateY(42deg) rotateZ(.4deg) translateX(2px);
    opacity: .96;
  }

  70% {
    transform: rotateY(98deg) rotateZ(.9deg) translateX(6px);
    opacity: .55;
  }

  100% {
    transform: rotateY(132deg) rotateZ(1.2deg) translateX(10px);
    opacity: 0;
  }
}

/* The highlight line travels toward the “hinge”, like a crease */
@keyframes creaseNext {
  0% {
    transform: translateX(22%);
  }

  40% {
    transform: translateX(12%);
  }

  70% {
    transform: translateX(6%);
  }

  100% {
    transform: translateX(2%);
  }
}

@keyframes shadowNext {
  0% {
    transform: translateX(18%);
    opacity: .0;
  }

  25% {
    opacity: .65;
  }

  70% {
    transform: translateX(7%);
    opacity: .85;
  }

  100% {
    transform: translateX(2%);
    opacity: .15;
  }
}

@keyframes creasePrev {
  0% {
    transform: translateX(-22%);
  }

  40% {
    transform: translateX(-12%);
  }

  70% {
    transform: translateX(-6%);
  }

  100% {
    transform: translateX(-2%);
  }
}

@keyframes shadowPrev {
  0% {
    transform: translateX(-18%);
    opacity: .0;
  }

  25% {
    opacity: .65;
  }

  70% {
    transform: translateX(-7%);
    opacity: .85;
  }

  100% {
    transform: translateX(-2%);
    opacity: .15;
  }
}

/* Bottom bar */

.bottombar {
  height: 56px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 10px;
  padding-bottom: 8px;
}

.hint-chip {
  font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  font-size: .86rem;
  color: rgba(0, 0, 0, .65);
  background: rgba(255, 255, 255, .68);
  border: 1px solid rgba(0, 0, 0, .10);
  border-radius: 999px;
  padding: 8px 12px;
}

/* Modal */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, .80);
  z-index: 80;
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  padding: env(safe-area-inset-top) 14px env(safe-area-inset-bottom);
}

.modal-close {
  position: absolute;
  top: calc(env(safe-area-inset-top) + 14px);
  right: 14px;
  width: 44px;
  height: 44px;
  border-radius: 14px;
  border: 1px solid rgba(255, 255, 255, .18);
  background: rgba(255, 255, 255, .12);
  color: rgba(255, 255, 255, .92);
  font-size: 1.25rem;
  cursor: pointer;
}

.modal-viewport {
  width: min(94vw, 860px);
  height: min(78vh, 860px);
  overflow: hidden;
  border-radius: 18px;
  border: 1px solid rgba(255, 255, 255, .16);
  background: rgba(0, 0, 0, .35);
  display: grid;
  place-items: center;
  touch-action: none;
}

.modal-viewport img {
  max-width: none;
  width: auto;
  height: auto;
  transform-origin: center center;
  user-select: none;
  -webkit-user-drag: none;
}

.modal-help {
  margin-top: 10px;
  color: rgba(255, 255, 255, .78);
  font-family: ui-sans-serif, system-ui, -apple-system, Segoe UI, Roboto, sans-serif;
  font-size: .88rem;
}

@media (max-width: 360px) {
  .page-head h2 {
    font-size: 1.35rem;
  }

  .story {
    font-size: 1.03rem;
  }
}