 :root {
  --paper: #eee2cc;
  --deep: #ddc9a8;
  --cream: #fff8ea;
  --ink: #493e36;
  --soft: #75685d;
  --rose: #c98f8d;
  --gold: #c7a263;
  --black: #090806;
  --ease: cubic-bezier(.2, .72, .2, 1);
  --top: env(safe-area-inset-top, 0px);
  --bottom: env(safe-area-inset-bottom, 0px);
}

* { box-sizing: border-box; }

html, body {
  width: 100%;
  height: 100%;
  margin: 0;
  padding: 0;
  overflow: hidden;
  background: var(--black);
  color: var(--ink);
  font-family: Georgia, "Times New Roman", serif;
  overscroll-behavior: none;
  -webkit-tap-highlight-color: transparent;
}

body { touch-action: none; }

button, video, img { -webkit-tap-highlight-color: transparent; }
button { font: inherit; }

#app {
  position: relative;
  width: 100%;
  height: 100%;
  height: 100dvh;
  min-height: 100svh;
  overflow: hidden;
  isolation: isolate;
}

.screen {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  min-height: 100dvh;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity .55s var(--ease);
  overflow: hidden;
  z-index: 1;
}

.screen.active {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
  z-index: 5;
}

/* VIDEO PRESENTATION */

.full-video {
  display: block;
  width: 100%;
  height: 100%;
  object-fit: contain;
  object-position: center;
  background: var(--black);
}

.screen--welcome,
.screen--story,
.video-screen {
  background: var(--black);
}

.hero-video {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  object-position: center center;
}

.cinematic-video {
  position: absolute;
  inset: 0;
}

.screen--welcome,
.screen--story {
  touch-action: none;
}

.cinematic-video {
  object-fit: contain;
  background: #000;
}

.gesture-cue {
  position: absolute;
  z-index: 10;

  right: max(20px, 5vw);
  bottom: calc(24px + var(--bottom));

  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;

  color: rgba(255, 248, 234, .92);

  pointer-events: none;

  opacity: 1;
  animation:
    gestureFloat 1.7s ease-in-out infinite,
    gestureFadeOut 1s ease 3.5s forwards;
}

.gesture-arrow {
  font-family: system-ui, sans-serif;
  font-size: 28px;
  line-height: 1;
  text-shadow: 0 2px 8px rgba(0,0,0,.35);
}

.gesture-text {
  font-family: system-ui, sans-serif;
  font-size: 11px;
  font-weight: 600;
  letter-spacing: .08em;
  text-transform: uppercase;
  text-shadow: 0 2px 8px rgba(0,0,0,.35);
}

@keyframes gestureFloat {
  0%, 100% {
    transform: translateY(0);
  }

  50% {
    transform: translateY(6px);
  }
}

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

.story-open-button {
  position: absolute;
  z-index: 20;
  left: 50%;
  bottom: calc(28px + var(--bottom));
  transform: translateX(-50%);
  border: 1px solid rgba(73,62,54,.35);
  border-radius: 999px;
  padding: 15px 34px;
  min-width: 150px;
  background: rgba(255,248,234,.97);
  color: #332a24;
  box-shadow: 0 12px 35px rgba(0,0,0,.38);
  font-size: clamp(17px, 4.5vw, 21px);
  font-weight: 600;
  letter-spacing: .02em;
  cursor: pointer;
  touch-action: manipulation;
}

.runtime-message {
  position: absolute;
  z-index: 80;
  left: 50%;
  bottom: calc(20px + var(--bottom));
  transform: translateX(-50%);
  width: min(88vw, 500px);
  padding: 12px 15px;
  border-radius: 12px;
  background: rgba(255,248,234,.96);
  color: var(--ink);
  box-shadow: 0 12px 30px rgba(0,0,0,.22);
  text-align: center;
  font-family: system-ui, sans-serif;
  font-size: 12px;
}

.runtime-message strong,
.runtime-message span { display: block; }

.runtime-message span { margin-top: 4px; opacity: .7; }
.runtime-error { z-index: 150; top: calc(18px + var(--top)); bottom: auto; }

/* PAPER / ENVELOPE */

.paper-screen {
  display: grid;
  place-items: center;
  padding: calc(18px + var(--top)) 14px calc(18px + var(--bottom));
  background:
    radial-gradient(circle at 50% 18%, rgba(255,255,255,.28), transparent 42%),
    linear-gradient(160deg, var(--paper), var(--deep));
}

.envelope-stage {
  width: min(96vw, 760px);
  min-height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 8px;
  text-align: center;
}

.envelope-stage[hidden] { display: none; }

.eyebrow {
  margin: 0 0 12px;
  color: #5f5046;
  font-size: clamp(12px, 2.9vw, 16px);
  font-weight: 600;
  letter-spacing: .17em;
  text-transform: uppercase;
}

.envelope-stage h1 {
  margin: 0;
  max-width: 90vw;
  color: #3d332c;
  font-size: clamp(31px, 7.5vw, 52px);
  font-weight: 400;
  line-height: 1.05;
}

.hint {
  margin: 10px 0;
  color: #b66f70;
  font-style: italic;
  font-size: clamp(19px, 4.8vw, 24px);
  font-weight: 500;
  min-height: 1.6em;
}

.wait-note {
  margin: 8px 0 0;
  color: #5f544c;
  font-size: clamp(15px, 3.7vw, 18px);
  font-weight: 500;
  opacity: .95;
}

/* Rotating six-dot loader, matching the supplied reference idea. */
.envelope-loader {
  position: relative;
  width: 52px;
  height: 52px;
  margin: 14px 0 2px;
  animation: envelopeLoaderSpin 1.15s linear infinite;
}

.envelope-loader span {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 10px;
  height: 25px;
  margin: -12.5px 0 0 -5px;
  border-radius: 999px;
  background: var(--rose);
  transform-origin: 5px 12.5px;
  opacity: .95;
}

.envelope-loader span:nth-child(1) { transform: rotate(0deg) translateY(-20px); }
.envelope-loader span:nth-child(2) { transform: rotate(60deg) translateY(-20px); }
.envelope-loader span:nth-child(3) { transform: rotate(120deg) translateY(-20px); }
.envelope-loader span:nth-child(4) { transform: rotate(180deg) translateY(-20px); }
.envelope-loader span:nth-child(5) { transform: rotate(240deg) translateY(-20px); }
.envelope-loader span:nth-child(6) { transform: rotate(300deg) translateY(-20px); }

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

/* The actual JPEG contains white canvas. Scale the image itself
   generously so the drawn envelope occupies most of that canvas. */
.envelope-button {
  position: relative;
  width: min(98vw, 760px);
  height: min(70dvh, 640px);
  margin: 8px auto 4px;
  padding: 0;
  border: 0;
  border-radius: 20px;
  background: rgba(255,253,248,.96);
  box-shadow: 0 24px 70px rgba(55,40,30,.22);
  overflow: hidden;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  touch-action: manipulation;
}

.envelope-image {
  display: block;
  width: 100%;
  height: 100%;
  max-width: none;
  max-height: none;
  object-fit: contain;
  object-position: center;
  user-select: none;
  pointer-events: none;
  transform: scale(1.42);
}

.envelope-seal {
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  z-index: 2;
  font-size: clamp(38px, 9vw, 62px);
  color: #c58a61;
  text-shadow: 0 2px 8px rgba(0,0,0,.18);
  pointer-events: none;
}

.envelope-button:active { transform: scale(.985); }

.envelope-button.shake { animation: envelopeShake .55s var(--ease); }
.envelope-button.open { animation: envelopeOpen .72s var(--ease) forwards; }

@keyframes envelopeShake {
  0%, 100% { transform: translateX(0) rotate(0); }
  20% { transform: translateX(-12px) rotate(-3deg); }
  40% { transform: translateX(12px) rotate(3deg); }
  60% { transform: translateX(-8px) rotate(-2deg); }
  80% { transform: translateX(6px) rotate(2deg); }
}

@keyframes envelopeOpen {
  to { transform: scale(1.06); opacity: 0; }
}

.attempt-message {
  min-height: 82px;
  width: min(94vw, 620px);
  margin: 7px 0 0;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #5b4b42;
  font-size: clamp(18px, 4.8vw, 24px);
  font-weight: 500;
  line-height: 1.45;
  font-style: italic;
  white-space: pre-line;
}

/* CARD */

.card-shell {
  width: min(96vw, 760px);
  height: 100%;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: clamp(10px, 1.5vh, 18px);
  text-align: center;
}

.card-media {
  width: min(94vw, 680px);
  height: min(72dvh, 700px);
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}

.card-media img,
.card-media video {
  display: block;
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: 100%;
  object-fit: contain;
  border-radius: 10px;
  box-shadow: 0 18px 50px rgba(55,40,30,.2);
  background: var(--cream);
}

.card-caption {
  min-height: 1.2em;
  margin: 0;
  color: var(--soft);
  font-size: 13px;
  letter-spacing: .05em;
}

.primary-button {
  border: 1px solid rgba(73,62,54,.3);
  border-radius: 999px;
  padding: 13px 26px;
  color: #332a24;
  background: rgba(255,253,248,.97);
  box-shadow: 0 10px 28px rgba(55,40,30,.18);
  font-size: clamp(16px, 3.8vw, 19px);
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
}

.primary-button:active { transform: scale(.97); }

/* CINEMATIC TRANSITIONS */

.video-screen {
  background: #000;
  display: flex;
  align-items: center;
  justify-content: center;
}

.video-screen .cinematic-video {
  width: 100%;
  height: 100%;
  object-fit: contain;
  object-position: center;
}

/* MEMORIES */

.memories-screen {
  background: var(--paper);
  touch-action: none;
}

.memory-heading {
  position: absolute;
  z-index: 10;
  left: 6vw;
  top: calc(3vh + var(--top));
  pointer-events: none;
}

.memory-heading .eyebrow { margin-bottom: 6px; }

.memory-heading h1 {
  margin: 0;
  color: #3d332c;
  font-size: clamp(30px, 7vw, 54px);
  font-weight: 400;
  line-height: .95;
}

.memory-viewport {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding:
    calc(88px + var(--top))
    12px
    calc(20px + var(--bottom));
  touch-action: none;
}

.memory-viewport img,
.memory-viewport video {
  display: block;
  width: auto;
  height: auto;
  max-width: min(94vw, 720px);
  max-height: calc(100dvh - 150px - var(--top) - var(--bottom));
  object-fit: contain;
  border-radius: 10px;
  box-shadow: 0 18px 50px rgba(55,40,30,.18);
  background: #2b231c;
}

/* Caption deliberately overlays the memory, rather than occupying
   separate layout space. This keeps the existing memory composition intact. */
.memory-caption {
  position: absolute;
  z-index: 12;
  left: 50%;
  bottom: calc(30px + var(--bottom));
  transform: translateX(-50%);
  width: max-content;
  max-width: min(88vw, 620px);
  margin: 0;
  padding: 9px 17px;
  border-radius: 999px;
  color: #fff8ea;
  background: rgba(45, 35, 28, .66);
  border: 1px solid rgba(255,255,255,.28);
  box-shadow: 0 8px 24px rgba(0,0,0,.2);
  backdrop-filter: blur(8px);
  font-size: clamp(14px, 3.7vw, 18px);
  line-height: 1.3;
  text-align: center;
  pointer-events: none;
}

/* MESSAGE */

.letter-card {
  width: min(94vw, 720px);
  max-height: calc(100dvh - 34px - var(--top) - var(--bottom));
  overflow-y: auto;
  padding: clamp(26px, 6vw, 64px);
  background: rgba(255,253,248,.9);
  border-radius: 18px;
  box-shadow: 0 24px 70px rgba(55,40,30,.18);
  text-align: left;
   touch-action: pan-y;
  -webkit-overflow-scrolling: touch;
}

.letter-small {
  margin: 0 0 8px;
  color: var(--rose);
  letter-spacing: .17em;
  text-transform: uppercase;
  font-size: 12px;
}

.letter-card h1 {
  margin: 0;
  font-size: clamp(30px, 8vw, 52px);
  font-weight: 400;
  line-height: 1;
}

.letter-body {
  margin: 22px 0 26px;
  color: var(--ink);
  font-size: clamp(16px, 4vw, 21px);
  line-height: 1.7;
}

.letter-body pre {
  margin: 0;
  white-space: pre-wrap;
  overflow-wrap: anywhere;
  font: inherit;
  line-height: inherit;
}

.letter-body p { margin: 0 0 14px; }

/* ENDING */

.ending-screen {
  background:
    radial-gradient(circle at 50% 20%, rgba(255,255,255,.25), transparent 45%),
    linear-gradient(160deg, var(--paper), var(--deep));
}

.ending-shell {
  width: 100%;
  height: 100%;
  padding: calc(14px + var(--top)) 14px calc(14px + var(--bottom));
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: clamp(10px, 2vh, 18px);
  text-align: center;
}

.ending-video {
  display: block;
  width: min(94vw, 720px);
  height: min(58dvh, 600px);
  max-width: 100%;
  max-height: 58dvh;
  object-fit: contain;
  background: transparent;
}

.ending-copy { width: min(94vw, 620px); }

.ending-copy h1 {
  margin: 4px 0 8px;
  font-size: clamp(32px, 9vw, 58px);
  font-weight: 400;
  line-height: .98;
}

.ending-copy p:not(.eyebrow) {
  margin: 8px 0 18px;
  color: var(--soft);
}

/* REVISIT MENU */

.revisit-menu {
  width: min(94vw, 620px);
  margin-top: 10px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

.revisit-label {
  margin: 0;
  color: var(--soft);
  font-size: 13px;
  letter-spacing: .08em;
  opacity: .85;
}

.revisit-links {
  display: flex;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  gap: 9px;
}

.revisit-button {
  border: 1px solid rgba(73,62,54,.22);
  border-radius: 999px;
  padding: 9px 16px;
  color: #493e36;
  background: rgba(255,253,248,.72);
  box-shadow: 0 6px 18px rgba(55,40,30,.10);
  font-size: 14px;
  font-weight: 600;
  cursor: pointer;
  touch-action: manipulation;
  transition:
    transform .2s var(--ease),
    background .2s var(--ease),
    box-shadow .2s var(--ease);
}

.revisit-button:active {
  transform: scale(.95);
}

@media (hover: hover) {
  .revisit-button:hover {
    background: rgba(255,253,248,.95);
    box-shadow: 0 8px 22px rgba(55,40,30,.14);
    transform: translateY(-1px);
  }
}

/* SOUND */

.sound-toggle {
  position: fixed;
  z-index: 110;
  right: max(14px, 4vw);
  top: calc(14px + var(--top));
  width: 48px;
  height: 48px;
  border: 1px solid rgba(73,62,54,.28);
  border-radius: 50%;
  background: rgba(255,253,248,.95);
  color: var(--ink);
  box-shadow: 0 10px 30px rgba(0,0,0,.18);
  display: grid;
  place-items: center;
  font-size: 19px;
  cursor: pointer;
  backdrop-filter: blur(8px);
}

/* RESPONSIVE TUNING */

@media (max-width: 600px) {
  .gesture-cue { bottom: calc(16px + var(--bottom)); }

  .envelope-button {
    width: 98vw;
    height: min(65dvh, 560px);
  }

  .envelope-image { transform: scale(1.50); }

  .card-media { height: min(70dvh, 650px); }

  .memory-viewport { padding-top: calc(76px + var(--top)); }

  .memory-viewport img,
  .memory-viewport video {
    max-height: calc(100dvh - 125px - var(--top) - var(--bottom));
    max-width: 96vw;
  }

  .memory-caption {
    bottom: calc(24px + var(--bottom));
    max-width: 88vw;
  }

  .ending-video {
    width: 96vw;
    height: min(56dvh, 560px);
    max-height: 56dvh;
  }
}

@media (orientation: landscape) and (max-height: 600px) {
  .envelope-button { height: 72dvh; }
  .card-media { height: 66dvh; }

  .memory-viewport { padding-top: 12px; }

  .memory-heading {
    left: 3vw;
    top: calc(8px + var(--top));
  }

  .memory-viewport img,
  .memory-viewport video {
    max-height: calc(100dvh - 20px);
  }

  .memory-caption {
    bottom: calc(12px + var(--bottom));
  }

  .ending-video { height: 60dvh; }
}

@media (prefers-reduced-motion: reduce) {
  .gesture-cue,
  .envelope-loader {
    animation: none;
  }

  * { transition-duration: .01ms !important; }
}
