/* ---------- Fuentes locales ---------- */
@font-face {
  font-family: 'Mystery Quest';
  font-weight: 400;
  font-display: swap;
  src: url('../fonts/mystery-quest.ttf') format('truetype');
}
@font-face {
  font-family: 'Quicksand';
  font-weight: 500;
  font-display: swap;
  src: url('../fonts/quicksand-500.ttf') format('truetype');
}
@font-face {
  font-family: 'Quicksand';
  font-weight: 600;
  font-display: swap;
  src: url('../fonts/quicksand-600.ttf') format('truetype');
}
@font-face {
  font-family: 'Quicksand';
  font-weight: 700;
  font-display: swap;
  src: url('../fonts/quicksand-700.ttf') format('truetype');
}
@font-face {
  font-family: 'Cormorant Garamond';
  font-style: normal;
  font-weight: 300 700;
  font-display: swap;
  src: url('../fonts/cormorant-garamond.ttf') format('truetype');
}
@font-face {
  font-family: 'Cormorant Garamond';
  font-style: italic;
  font-weight: 300 700;
  font-display: swap;
  src: url('../fonts/cormorant-garamond-italic.ttf') format('truetype');
}
@font-face {
  font-family: 'Pinyon Script';
  font-weight: 400;
  font-display: swap;
  src: url('../fonts/pinyon-script.ttf') format('truetype');
}

/* ---------- Tokens ---------- */
:root {
  --night: #070612;
  --plum: #1a1233;
  --ink: #f7f2ff;
  --muted: #bcb2e2;
  --pumpkin: #ff8a2a;
  --pumpkin-light: #ffc27a;
  --pumpkin-deep: #c2530c;
  --rose: #ff7fae;
  --glass: rgba(18, 12, 38, 0.62);
  --glass-strong: rgba(14, 10, 30, 0.86);
  --line: rgba(255, 214, 170, 0.18);
  --glow: 0 0 22px rgba(255, 138, 42, 0.55);
  --radius: 18px;
  --gutter: max(16px, env(safe-area-inset-left));
  --font-display: 'Mystery Quest', 'Quicksand', cursive;
  --font-body: 'Quicksand', system-ui, -apple-system, 'Segoe UI', sans-serif;
  --font-serif: 'Cormorant Garamond', 'Cormorant', Georgia, 'Times New Roman', serif;
  --font-script: 'Pinyon Script', 'Snell Roundhand', cursive;
  /* Papel marfil con grano sutil */
  --paper:
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='240' height='240'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.85' numOctaves='3' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 0.45 0 0 0 0 0.33 0 0 0 0 0.18 0 0 0 0.1 0'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E"),
    radial-gradient(120% 80% at 50% 0%, #fffaf0 0%, transparent 60%),
    radial-gradient(90% 70% at 100% 100%, rgba(203, 168, 116, 0.34), transparent 70%),
    #f6ecd9;
  --ease-out: cubic-bezier(0.22, 1, 0.36, 1);
  --ease-spring: cubic-bezier(0.34, 1.56, 0.64, 1);
  color-scheme: dark;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--night);
}

body {
  font-family: var(--font-body);
  font-weight: 500;
  color: var(--ink);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
}

svg {
  width: 1.15em;
  height: 1.15em;
  flex: none;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

button {
  font: inherit;
  color: inherit;
}

:focus-visible {
  outline: 2px solid var(--pumpkin-light);
  outline-offset: 3px;
}

/* ---------- Escena ---------- */
.scene {
  position: fixed;
  inset: 0;
  touch-action: none;
  background: radial-gradient(120% 90% at 60% 20%, #2b1d55 0%, #110c26 45%, var(--night) 100%);
}
.scene canvas {
  display: block;
  width: 100%;
  height: 100%;
}

.vignette {
  position: fixed;
  inset: 0;
  pointer-events: none;
  background:
    radial-gradient(130% 100% at 50% 45%, transparent 55%, rgba(3, 2, 10, 0.75) 100%),
    linear-gradient(to bottom, rgba(5, 4, 14, 0.35), transparent 22%, transparent 70%, rgba(5, 4, 14, 0.55));
  transition: background 1.2s;
}
body[data-step='gate'] .vignette {
  background-color: rgba(6, 4, 16, 0.5);
}

/* ---------- Pantallas ---------- */
.screen {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding:
    max(24px, env(safe-area-inset-top)) var(--gutter)
    max(24px, env(safe-area-inset-bottom));
  text-align: center;
  pointer-events: none;
  opacity: 0;
  visibility: hidden;
  transform: translateY(14px);
  transition: opacity 0.8s var(--ease-out), transform 0.8s var(--ease-out), visibility 0s linear 0.8s;
}
.screen.is-visible {
  opacity: 1;
  visibility: visible;
  transform: none;
  transition: opacity 0.9s var(--ease-out), transform 0.9s var(--ease-out), visibility 0s;
}
.screen.is-visible > * {
  pointer-events: auto;
}

.display {
  margin: 0;
  font-family: var(--font-display);
  font-weight: 400;
  line-height: 1.04;
  color: var(--pumpkin-light);
  text-wrap: balance;
  text-shadow:
    0 0 24px rgba(255, 128, 32, 0.6),
    0 0 2px rgba(90, 30, 0, 0.9),
    0 4px 14px rgba(0, 0, 0, 0.7);
}

.eyebrow {
  margin: 0 0 14px;
  font-size: 0.78rem;
  font-weight: 700;
  letter-spacing: 0.28em;
  text-transform: uppercase;
  color: var(--muted);
}

.lede {
  margin: 18px 0 0;
  max-width: 28em;
  font-size: clamp(1rem, 2.6vw, 1.15rem);
  color: #e4dcff;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.8);
}

/* ---------- Botones ---------- */
.btn {
  position: relative;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.55em;
  min-height: 48px;
  padding: 0.75em 1.6em;
  border: 0;
  border-radius: 999px;
  font-weight: 700;
  font-size: 1rem;
  letter-spacing: 0.02em;
  white-space: nowrap;
  cursor: pointer;
  transition: transform 0.35s var(--ease-spring), box-shadow 0.35s, background-color 0.3s, opacity 0.3s;
}
.btn:active {
  transform: scale(0.96);
}
.btn--primary {
  color: #2a0f00;
  background: linear-gradient(180deg, var(--pumpkin-light), var(--pumpkin));
  box-shadow: var(--glow), inset 0 -3px 0 rgba(140, 50, 0, 0.35), inset 0 1px 0 rgba(255, 255, 255, 0.45);
}
.btn--primary:hover {
  box-shadow: 0 0 34px rgba(255, 150, 60, 0.85), inset 0 -3px 0 rgba(140, 50, 0, 0.35), inset 0 1px 0 rgba(255, 255, 255, 0.45);
}
.btn--ghost {
  color: var(--ink);
  background: var(--glass);
  border: 1px solid rgba(220, 205, 255, 0.28);
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
}
.btn--ghost:hover {
  background: rgba(40, 28, 80, 0.7);
}
.btn--lg {
  margin-top: 34px;
  min-height: 56px;
  padding: 0.85em 2.2em;
  font-size: 1.12rem;
}
.btn--xl {
  min-width: 132px;
  min-height: 58px;
  font-size: 1.25rem;
}
.btn svg {
  transition: transform 0.3s var(--ease-out);
}
.btn:hover svg {
  transform: translateX(3px);
}

.icon-btn {
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  padding: 0;
  border-radius: 50%;
  border: 1px solid rgba(220, 205, 255, 0.22);
  background: var(--glass);
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
  cursor: pointer;
  transition: background-color 0.25s, transform 0.25s;
}
.icon-btn:hover {
  background: rgba(40, 28, 80, 0.75);
}

.sr-only-focusable:not(:focus) {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip: rect(0 0 0 0);
  white-space: nowrap;
  padding: 0;
  border: 0;
}

/* ---------- Carga ---------- */
.loader {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 18px;
  background: radial-gradient(circle at 50% 42%, #22163f 0, #0d0a1d 55%, #05040c 100%);
  transition: opacity 0.9s var(--ease-out), visibility 0s linear 0.9s;
}
.loader:not(.is-visible) {
  opacity: 0;
  visibility: hidden;
}
.loader__ghost {
  width: 72px;
  height: 72px;
  fill: #f2f3ff;
  stroke: none;
  filter: drop-shadow(0 0 16px rgba(180, 195, 255, 0.6));
  animation: float 2.4s ease-in-out infinite;
}
.loader__eye {
  fill: #140d22;
}
.loader__text {
  margin: 0;
  font-family: var(--font-display);
  font-size: 1.6rem;
  color: var(--pumpkin-light);
  text-shadow: var(--glow);
}
.loader__bar {
  width: 140px;
  height: 3px;
  overflow: hidden;
  border-radius: 3px;
  background: rgba(255, 255, 255, 0.1);
}
.loader__bar span {
  display: block;
  width: 40%;
  height: 100%;
  border-radius: inherit;
  background: linear-gradient(90deg, transparent, var(--pumpkin), transparent);
  animation: shimmer 1.3s ease-in-out infinite;
}

/* ---------- Portada ---------- */
.gate {
  justify-content: center;
}
/* Halo cálido detrás del título para separarlo del cielo. */
.gate::before {
  content: '';
  position: absolute;
  left: 50%;
  top: 50%;
  z-index: -1;
  width: min(120vw, 900px);
  height: min(80vh, 560px);
  transform: translate(-50%, -55%);
  background: radial-gradient(closest-side, rgba(10, 6, 24, 0.7), rgba(10, 6, 24, 0.35) 55%, transparent);
  pointer-events: none;
}
.gate .eyebrow {
  margin-bottom: 22px;
  font-size: clamp(0.8rem, 2.4vw, 0.95rem);
  letter-spacing: 0.34em;
  color: #e9dcff;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.9);
}
.gate__title {
  font-size: clamp(3.4rem, 15vw, 6.4rem);
  line-height: 0.95;
  color: var(--pumpkin-light);
  /* Contorno oscuro por fuera de la letra (el trazo se pinta debajo del relleno). */
  -webkit-text-stroke: 0.09em #120a06;
  paint-order: stroke fill;
  text-shadow:
    0 0 30px rgba(255, 128, 32, 0.75),
    0 0 70px rgba(255, 110, 30, 0.4),
    0 0 2px rgba(90, 30, 0, 0.9),
    0 5px 18px rgba(0, 0, 0, 0.75);
  animation: title-glow 3.2s ease-in-out infinite;
}
#gate-name {
  color: #ffd29a;
}
.gate .lede {
  margin-top: 22px;
  font-size: clamp(1.15rem, 3.6vw, 1.4rem);
  font-weight: 600;
  color: #ffffff;
  text-wrap: balance;
  text-shadow: 0 0 18px rgba(0, 0, 0, 0.9), 0 2px 6px rgba(0, 0, 0, 0.95);
}

/* ---------- Capítulo ---------- */
.chapter {
  padding-top: max(7vh, calc(env(safe-area-inset-top) + 24px));
}
.chapter__title {
  font-size: clamp(2.1rem, 7vw, 3.6rem);
}
.chapter__text {
  margin: 10px 0 0;
  font-weight: 600;
  letter-spacing: 0.06em;
  color: #e2d8ff;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.85);
}
.progress {
  display: flex;
  gap: 12px;
  margin: 20px 0 0;
  padding: 0;
  list-style: none;
}
.progress li {
  width: 12px;
  height: 12px;
  border-radius: 50%;
  border: 1.5px solid rgba(255, 194, 122, 0.6);
  transition: background-color 0.5s, box-shadow 0.5s, transform 0.5s var(--ease-spring);
}
.progress li.is-lit {
  background: var(--pumpkin-light);
  border-color: var(--pumpkin-light);
  box-shadow: 0 0 14px 2px rgba(255, 150, 50, 0.8);
  transform: scale(1.2);
}
#btn-light {
  margin-top: 20px;
}

/* ---------- Pregunta ---------- */
.question {
  justify-content: space-between;
  padding-top: max(8vh, calc(env(safe-area-inset-top) + 24px));
  padding-bottom: max(9vh, calc(env(safe-area-inset-bottom) + 28px));
}
.question__text {
  max-width: 13em;
  font-size: clamp(2.5rem, 9vw, 5rem);
  color: #fff4e4;
  text-shadow:
    0 0 28px rgba(255, 150, 60, 0.75),
    0 0 60px rgba(255, 120, 40, 0.35),
    0 4px 14px rgba(0, 0, 0, 0.7);
}
.question.is-visible .question__text {
  animation: rise 1.2s var(--ease-out) both;
}
/* Con escena 3D la pregunta la dibujan las luces; el texto queda para lectores de pantalla. */
body.has-world .question__text {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}
.question__actions {
  margin-top: auto;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 18px;
}
.btn--yes {
  transform: scale(var(--grow, 1));
  animation: heartbeat 1.8s ease-in-out infinite;
}
.btn--yes:active {
  transform: scale(calc(var(--grow, 1) * 0.96));
}
.btn--no {
  z-index: 40;
  transition: left 0.42s var(--ease-spring), top 0.42s var(--ease-spring), opacity 0.4s, transform 0.4s;
}
.btn--no.is-loose {
  position: fixed;
  margin: 0;
}
.btn--no.is-gone {
  opacity: 0;
  transform: scale(0.4) rotate(-12deg);
  pointer-events: none;
}

/* ---------- Final: sobre y carta ---------- */
.finale {
  justify-content: center;
}
body.is-reading .vignette {
  background-color: rgba(5, 3, 14, 0.5);
}
.mail {
  display: grid;
  place-items: center;
  width: min(100%, 540px);
  max-height: 100%;
}
.mail > * {
  grid-area: 1 / 1;
}
.mail__closed {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 30px;
  transition: opacity 0.7s var(--ease-out), transform 0.8s var(--ease-out);
}
.mail.is-read .mail__closed {
  opacity: 0;
  transform: translateY(60px) scale(0.92);
  pointer-events: none;
}
.mail__hint {
  display: grid;
  gap: 8px;
  margin: 0;
  transition: opacity 0.4s;
}
.mail__title {
  font-family: var(--font-serif);
  font-style: italic;
  font-weight: 500;
  font-size: 2rem;
  line-height: 1;
  color: #f6e7c8;
  text-shadow: 0 2px 16px rgba(0, 0, 0, 0.6);
}
.mail__cta {
  font-family: var(--font-serif);
  font-weight: 600;
  font-size: 0.8rem;
  letter-spacing: 0.3em;
  text-transform: uppercase;
  color: rgba(246, 231, 200, 0.7);
}
.mail.is-opening .mail__hint {
  opacity: 0;
}

/* Sobre: piezas en SVG para que los pliegues sean nítidos a cualquier tamaño. */
.envelope {
  --gold: #e8c98a;
  position: relative;
  width: min(84vw, 400px);
  aspect-ratio: 8 / 5;
  padding: 0;
  border: 0;
  background: none;
  cursor: pointer;
  perspective: 1400px;
  transition: transform 0.4s var(--ease-out);
}
.finale.is-visible .envelope {
  animation: envelope-in 1.2s var(--ease-out) both, bob 4s ease-in-out 1.2s infinite;
}
.envelope:hover {
  transform: translateY(-4px);
}
.mail.is-opening .envelope {
  animation: none;
  cursor: default;
  transform: none;
}
.envelope > span,
.envelope > svg {
  position: absolute;
}
/* Las piezas del sobre no heredan el trazo de los iconos de la interfaz. */
.envelope svg {
  stroke: none;
}
.envelope__shadow {
  left: 6%;
  right: 6%;
  bottom: -14%;
  height: 22%;
  border-radius: 50%;
  background: radial-gradient(closest-side, rgba(0, 0, 0, 0.55), transparent);
  filter: blur(6px);
}
.envelope__back {
  inset: 0;
  z-index: 0;
  overflow: hidden;
  border-radius: 6px;
  background: linear-gradient(180deg, #2c1838, #24132e);
  box-shadow: 0 18px 40px rgba(0, 0, 0, 0.45);
}
/* Forro dorado del interior, visible al abrir la solapa */
.envelope__back::before {
  content: '';
  position: absolute;
  inset: 0 0 auto;
  height: 60%;
  background: linear-gradient(135deg, #b98a3e, #f1d9a0 45%, #a87a32);
  clip-path: polygon(0 0, 100% 0, 50% 100%);
  opacity: 0.9;
}
.envelope__paper {
  left: 6%;
  right: 6%;
  top: 7%;
  height: 86%;
  z-index: 2;
  border-radius: 2px;
  background: var(--paper);
  box-shadow: 0 0 0 1px rgba(176, 138, 72, 0.25), 0 -6px 14px rgba(0, 0, 0, 0.2);
  transition: transform 1s var(--ease-out) 0.75s;
}
.envelope__paper::before {
  content: '';
  position: absolute;
  inset: 10px;
  border: 1px solid rgba(176, 138, 72, 0.5);
}
.envelope__front {
  inset: 0;
  z-index: 3;
  width: 100%;
  height: 100%;
  border-radius: 6px;
  overflow: hidden;
}
.envelope__front svg {
  display: block;
  width: 100%;
  height: 100%;
}
.envelope__address {
  left: 0;
  right: 0;
  top: 80%;
  z-index: 4;
  display: grid;
  justify-items: center;
  gap: 0;
  pointer-events: none;
}
.envelope__address small {
  font-family: var(--font-serif);
  font-weight: 600;
  font-size: 0.68rem;
  letter-spacing: 0.34em;
  text-transform: uppercase;
  color: rgba(232, 201, 138, 0.75);
}
.envelope__to {
  font-family: var(--font-script);
  font-size: clamp(1.35rem, 5.2vw, 1.7rem);
  line-height: 1.15;
  color: var(--gold);
  text-shadow: 0 1px 0 rgba(0, 0, 0, 0.35);
}
.envelope__flap {
  left: 0;
  right: 0;
  top: 0;
  height: 60%;
  z-index: 5;
  transform-origin: 50% 0;
  transform-style: preserve-3d;
  transition: transform 0.85s var(--ease-out) 0.25s, z-index 0s linear 0.65s;
}
.envelope__flap svg {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
}
.envelope__flap-face {
  filter: drop-shadow(0 3px 4px rgba(0, 0, 0, 0.35));
}
.envelope__flap-liner {
  transform: rotateY(180deg);
}
.envelope__seal {
  left: 50%;
  top: 60%;
  z-index: 6;
  /* Escala con el sobre para mantener la distancia con el texto de abajo. */
  width: clamp(54px, 18.5%, 74px);
  aspect-ratio: 1;
  translate: -50% -60%;
  filter: drop-shadow(0 4px 6px rgba(40, 0, 10, 0.55));
  transition: transform 0.45s var(--ease-out), opacity 0.35s;
}
.envelope__seal svg {
  width: 100%;
  height: 100%;
}
.envelope__seal::after {
  content: '';
  position: absolute;
  inset: -3px;
  border-radius: 50%;
  border: 1px solid rgba(232, 201, 138, 0.6);
  animation: seal-ring 2.4s ease-out infinite;
}
.envelope:hover .envelope__seal {
  transform: scale(1.06) rotate(-4deg);
}
.mail.is-opening .envelope__seal {
  transform: scale(1.35) rotate(12deg);
  opacity: 0;
}
.mail.is-opening .envelope__seal::after {
  animation: none;
  opacity: 0;
}
.mail.is-opening .envelope__flap {
  transform: rotateX(180deg);
  z-index: 1;
}
.mail.is-opening .envelope__paper {
  transform: translateY(-46%);
}

/* Carta */
.letter {
  --letter-ink: #3a2414;
  --letter-soft: #4b3626;
  --letter-gold: #a3541a;
  --letter-script: #6b1d2a;
  /* Siempre centrada en la pantalla, sin importar dónde quedó el sobre. */
  position: fixed;
  inset: max(28px, env(safe-area-inset-top)) var(--gutter) max(28px, env(safe-area-inset-bottom));
  z-index: 10;
  margin: auto;
  display: flex;
  flex-direction: column;
  gap: 20px;
  width: min(540px, calc(100vw - 2 * var(--gutter)));
  height: fit-content;
  max-height: calc(100dvh - max(28px, env(safe-area-inset-top)) - max(28px, env(safe-area-inset-bottom)));
  opacity: 0;
  visibility: hidden;
}
/* La carta recibe el foco para los lectores de pantalla, pero no es interactiva: sin contorno. */
.letter:focus {
  outline: none;
}
.mail.is-read .letter {
  opacity: 1;
  visibility: visible;
}
.letter__close {
  position: absolute;
  top: 8px;
  right: 8px;
  z-index: 20;
  display: grid;
  place-items: center;
  width: 42px;
  height: 42px;
  padding: 0;
  border: 1px solid rgba(160, 124, 62, 0.55);
  border-radius: 50%;
  color: var(--letter-ink);
  background: rgba(255, 252, 244, 0.96);
  box-shadow: 0 3px 12px rgba(60, 35, 10, 0.18);
  font: 700 1.7rem/1 system-ui, sans-serif;
  cursor: pointer;
  opacity: 0;
  transform: scale(0.85);
  transition: opacity 0.3s var(--ease-out), transform 0.3s var(--ease-out), background-color 0.2s;
}
.mail.is-read .letter__close {
  opacity: 1;
  transform: scale(1);
  transition-delay: 0.2s;
}
.letter__close:hover {
  background: #fff;
  transform: scale(1.05);
}
.letter__close:focus-visible {
  outline: 3px solid rgba(160, 124, 62, 0.75);
  outline-offset: 3px;
}
/* El contenido aparece cuando la hoja ya llegó al centro (ver Interface.showLetter). */
.letter__scroll > * {
  opacity: 0;
}
.mail.is-read .letter__scroll > * {
  animation: ink 0.7s var(--ease-out) both;
  animation-delay: 0.95s;
}
.letter__sheet {
  position: relative;
  display: flex;
  flex-direction: column;
  flex: 0 1 auto;
  min-height: 0;
  overflow: hidden;
  border-radius: 3px;
  background: var(--paper);
  box-shadow:
    inset 0 1px 0 rgba(255, 255, 255, 0.7),
    0 2px 6px rgba(0, 0, 0, 0.25),
    0 40px 90px rgba(0, 0, 0, 0.55);
}
/* Solo el texto se desplaza: el marco de la hoja se queda quieto. */
.letter__scroll {
  flex: 1 1 auto;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  padding: clamp(58px, 8vw, 72px) clamp(26px, 6vw, 54px) clamp(30px, 5vw, 44px);
  color: var(--letter-soft);
  text-align: left;
  scrollbar-width: thin;
  scrollbar-color: rgba(160, 124, 62, 0.45) transparent;
  user-select: text;
  -webkit-user-select: text;
}
.letter__scroll::-webkit-scrollbar {
  width: 6px;
}
.letter__scroll::-webkit-scrollbar-thumb {
  border-radius: 3px;
  background: rgba(160, 124, 62, 0.45);
}
/* Marco fino doble, como en la papelería impresa */
.letter__sheet::before,
.letter__sheet::after {
  content: '';
  position: absolute;
  z-index: 2;
  pointer-events: none;
}
.letter__sheet::before {
  inset: 12px;
  border: 1px solid rgba(160, 124, 62, 0.55);
}
.letter__sheet::after {
  inset: 16px;
  border: 1px solid rgba(160, 124, 62, 0.25);
}
.letter__title {
  margin: 0;
  font-family: var(--font-serif);
  font-style: italic;
  font-weight: 500;
  font-size: clamp(1.9rem, 5.4vw, 2.5rem);
  line-height: 1.12;
  text-align: center;
  text-wrap: balance;
  color: var(--letter-ink);
}
.letter__rule {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 12px;
  margin: 22px auto 26px;
  width: min(220px, 60%);
}
.letter__rule::before,
.letter__rule::after {
  content: '';
  flex: 1;
  height: 1px;
  background: linear-gradient(90deg, transparent, rgba(160, 124, 62, 0.7));
}
.letter__rule::after {
  background: linear-gradient(270deg, transparent, rgba(160, 124, 62, 0.7));
}
.letter__rule span {
  width: 6px;
  height: 6px;
  border: 1px solid var(--letter-gold);
  transform: rotate(45deg);
}
.letter__body p {
  margin: 0 0 0.85em;
  font-family: var(--font-serif);
  font-weight: 500;
  font-size: 1.22rem;
  line-height: 1.6;
}
.letter__body .word {
  opacity: 0;
}
.mail.is-read .letter__body .word {
  animation: ink 0.6s var(--ease-out) both;
  animation-delay: calc(var(--i) * 40ms + 0.9s);
}
/* Firma manuscrita */
.letter__sign {
  margin: 10px 0 0;
  text-align: right;
  font-family: var(--font-script);
  font-size: clamp(1.9rem, 7vw, 2.4rem);
  line-height: 1.2;
  color: var(--letter-script);
}
/* Fotos pegadas en la carta, como polaroids */
.letter__gallery {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 18px 14px;
  margin: 24px auto 4px;
}
.polaroid {
  --r: 0deg;
  position: relative;
  width: clamp(128px, 42%, 200px);
  padding: 8px 8px 26px;
  border: 1px solid rgba(160, 124, 62, 0.45);
  background: #fffdf8;
  box-shadow: 0 6px 18px rgba(60, 35, 10, 0.22), 0 1px 3px rgba(60, 35, 10, 0.2);
  cursor: zoom-in;
  transform: rotate(var(--r));
  transition: transform 0.4s var(--ease-out), box-shadow 0.4s;
}
.polaroid:hover {
  transform: rotate(0deg) scale(1.05);
  box-shadow: 0 12px 28px rgba(60, 35, 10, 0.32);
  z-index: 2;
}
.polaroid img {
  display: block;
  width: 100%;
  aspect-ratio: 5 / 4;
  object-fit: cover;
  background: rgba(160, 124, 62, 0.12);
}
/* Cinta adhesiva en la esquina superior */
.polaroid::after {
  content: '';
  position: absolute;
  left: 50%;
  top: -8px;
  width: 52px;
  height: 16px;
  translate: -50% 0;
  background: rgba(232, 201, 138, 0.5);
  box-shadow: 0 1px 2px rgba(60, 35, 10, 0.18);
  transform: rotate(-3deg);
}
.letter__plan {
  display: grid;
  justify-items: center;
  gap: 4px;
  margin: 26px auto 0;
  padding: 14px 8px;
  max-width: 340px;
  border-top: 1px solid rgba(160, 124, 62, 0.45);
  border-bottom: 1px solid rgba(160, 124, 62, 0.45);
  text-align: center;
}
.letter__plan-label {
  font-family: var(--font-serif);
  font-weight: 700;
  font-size: 0.7rem;
  letter-spacing: 0.34em;
  text-transform: uppercase;
  color: var(--letter-gold);
}
.letter__plan-value {
  font-family: var(--font-serif);
  font-style: italic;
  font-weight: 600;
  font-size: 1.3rem;
  color: var(--letter-ink);
}
.letter__sign,
.letter__gallery,
.letter__plan,
.letter__actions {
  opacity: 0;
}
/* La firma, las fotos, el plan y el botón aparecen cuando el texto termina de escribirse. */
.mail.is-read .letter__sign,
.mail.is-read .letter__gallery,
.mail.is-read .letter__plan,
.mail.is-read .letter__actions {
  animation: ink 0.8s var(--ease-out) both;
  animation-delay: var(--after, 1.5s);
}
.letter__actions {
  display: flex;
  justify-content: center;
  gap: 12px;
  flex: none;
}

/* En vertical el sobre baja para dejar ver el corazón en el cielo; la carta, al abrirse, se centra. */
@media (max-aspect-ratio: 4/5) {
  .finale {
    justify-content: flex-end;
  }
}

/* Pantallas bajas: la carta se compacta para que la firma quepa sin desplazarse. */
@media (max-height: 820px) {
  .letter__scroll {
    padding-top: 58px;
    padding-bottom: 26px;
  }
  .letter__title {
    margin-top: 10px;
    font-size: clamp(1.7rem, 5vw, 2.2rem);
  }
  .letter__rule {
    margin: 14px auto 16px;
  }
  .letter__body p {
    font-size: 1.12rem;
    line-height: 1.5;
  }
  .letter__gallery {
    margin-top: 18px;
  }
  .polaroid {
    width: clamp(118px, 40%, 170px);
  }
  .letter__plan {
    margin-top: 14px;
    padding: 10px 8px;
  }
}

/* En móvil la carta deja libre la esquina del botón de sonido. */
@media (max-width: 600px) {
  .finale {
    padding-top: calc(max(14px, env(safe-area-inset-top)) + 58px);
  }
  .letter {
    top: calc(max(14px, env(safe-area-inset-top)) + 58px);
    bottom: max(24px, env(safe-area-inset-bottom));
    max-height: calc(100dvh - max(14px, env(safe-area-inset-top)) - 58px - max(24px, env(safe-area-inset-bottom)));
  }
}

@media (min-aspect-ratio: 5/4) and (min-width: 820px) {
  .finale {
    align-items: flex-start;
    padding-left: max(5vw, 32px);
  }
}

/* ---------- Visor de fotos ---------- */
.lightbox {
  position: fixed;
  inset: 0;
  z-index: 100;
  display: grid;
  place-items: center;
  padding: 22px;
  background: rgba(8, 3, 14, 0.92);
  -webkit-backdrop-filter: blur(6px);
  backdrop-filter: blur(6px);
  cursor: zoom-out;
  animation: fade 0.3s ease;
}
.lightbox img {
  max-width: 100%;
  max-height: 100%;
  border-radius: 3px;
  box-shadow: 0 24px 70px rgba(0, 0, 0, 0.85);
}

/* ---------- Controles ---------- */
.music-control {
  position: fixed;
  top: 20px;
  right: 20px;
  z-index: 1000;
  display: flex;
  flex-direction: column;
  align-items: center;
  transition: opacity 0.6s, visibility 0.6s;
}
/* El botón de música aparece después de pulsar "Entrar". */
body[data-step='loading'] .music-control,
body[data-step='gate'] .music-control {
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
}

.music-btn {
  width: 50px;
  height: 50px;
  background: linear-gradient(180deg, var(--pumpkin-light), var(--pumpkin));
  border: none;
  border-radius: 50%;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--glow), inset 0 -3px 0 rgba(140, 50, 0, 0.35), inset 0 1px 0 rgba(255, 255, 255, 0.45);
  transition: all 0.3s ease;
}

.music-btn:hover {
  transform: scale(1.1);
  box-shadow: 0 0 34px rgba(255, 150, 60, 0.85), inset 0 -3px 0 rgba(140, 50, 0, 0.35), inset 0 1px 0 rgba(255, 255, 255, 0.45);
}

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

.music-btn:disabled {
  filter: grayscale(0.7);
  cursor: default;
}

.music-btn svg {
  width: 24px;
  height: 24px;
  fill: #2a0f00;
  stroke: none;
}

.volume-control {
  width: 120px;
  background: var(--glass-strong);
  padding: 10px;
  border-radius: 10px;
  box-shadow: 0 5px 15px rgba(0, 0, 0, 0.4);
  border: 1px solid rgba(255, 138, 42, 0.55);
  margin-top: 10px;
  display: none;
  opacity: 0;
  transform: translateY(-10px);
  transition: opacity 0.3s ease, transform 0.3s ease;
}

.volume-control.visible {
  display: block;
  opacity: 1;
  transform: translateY(0);
}

.volume-control input {
  width: 100%;
  height: 8px;
  background: rgba(255, 194, 122, 0.25);
  border-radius: 4px;
  outline: none;
  cursor: pointer;
  -webkit-appearance: none;
  appearance: none;
}

.volume-control input::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 16px;
  height: 16px;
  background: var(--pumpkin);
  border-radius: 50%;
  box-shadow: 0 0 8px rgba(255, 138, 42, 0.8);
}

.volume-control input::-moz-range-thumb {
  width: 16px;
  height: 16px;
  background: var(--pumpkin);
  border-radius: 50%;
  box-shadow: 0 0 8px rgba(255, 138, 42, 0.8);
  border: none;
}

@media (max-width: 768px) {
  .music-control {
    right: 15px;
  }

  .music-btn {
    width: 35px;
    height: 35px;
  }

  .music-btn svg {
    width: 22px;
    height: 22px;
  }

  .volume-control {
    width: 100px;
    margin-top: 8px;
  }

  .volume-control input {
    height: 10px;
  }

  .volume-control input::-webkit-slider-thumb {
    width: 18px;
    height: 18px;
  }

  .volume-control input::-moz-range-thumb {
    width: 18px;
    height: 18px;
  }
}

.fullscreen-control {
  position: fixed;
  top: 18px;
  left: 18px;
  z-index: 22;
}
body[data-step='loading'] .fullscreen-control {
  opacity: 0;
  pointer-events: none;
}

.fullscreen-btn {
  width: 50px;
  height: 50px;
  padding: 0;
  border: none;
  border-radius: 999px;
  background: linear-gradient(180deg, var(--pumpkin-light), var(--pumpkin));
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: var(--glow), inset 0 -3px 0 rgba(140, 50, 0, 0.35), inset 0 1px 0 rgba(255, 255, 255, 0.45);
  transition: transform 0.26s ease, box-shadow 0.26s ease, filter 0.26s ease;
}

.fullscreen-btn:hover {
  transform: scale(1.08);
  box-shadow: 0 0 34px rgba(255, 150, 60, 0.85), inset 0 -3px 0 rgba(140, 50, 0, 0.35), inset 0 1px 0 rgba(255, 255, 255, 0.45);
}

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

.fullscreen-btn svg {
  width: 28px;
  height: 28px;
  fill: #2a0f00;
  stroke: none;
}

#fullscreenExitIcon {
  width: 24px;
  height: 24px;
}

@media (max-width: 720px) {
  .fullscreen-control {
    top: 14px;
    left: 14px;
  }

  .fullscreen-btn {
    width: 38px;
    height: 38px;
  }

  .fullscreen-btn svg {
    width: 24px;
    height: 24px;
  }

  #fullscreenExitIcon {
    width: 21px;
    height: 21px;
  }
}

@media (max-width: 480px) {
  .fullscreen-control {
    top: 12px;
    left: 12px;
  }
}

.noscript {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 24px;
  text-align: center;
  background: var(--night);
}

/* ---------- Animaciones ---------- */
@keyframes title-glow {
  50% {
    text-shadow:
      0 0 40px rgba(255, 140, 40, 0.95),
      0 0 90px rgba(255, 110, 30, 0.55),
      0 0 2px rgba(90, 30, 0, 0.9),
      0 5px 18px rgba(0, 0, 0, 0.75);
  }
}
@keyframes float {
  50% {
    transform: translateY(-10px);
  }
}
@keyframes shimmer {
  from {
    transform: translateX(-100%);
  }
  to {
    transform: translateX(260%);
  }
}
@keyframes heartbeat {
  0%, 100% {
    box-shadow: var(--glow), inset 0 -3px 0 rgba(140, 50, 0, 0.35), inset 0 1px 0 rgba(255, 255, 255, 0.45);
  }
  50% {
    box-shadow: 0 0 42px rgba(255, 150, 60, 0.95), inset 0 -3px 0 rgba(140, 50, 0, 0.35), inset 0 1px 0 rgba(255, 255, 255, 0.45);
  }
}
@keyframes rise {
  from {
    opacity: 0;
    filter: blur(8px);
    transform: translateY(18px) scale(0.96);
  }
}
@keyframes envelope-in {
  from {
    opacity: 0;
    transform: translateY(70px) rotate(-4deg) scale(0.9);
  }
}
@keyframes bob {
  50% {
    transform: translateY(-8px) rotate(0.6deg);
  }
}
@keyframes seal-ring {
  0% {
    transform: scale(0.85);
    opacity: 0;
  }
  30% {
    opacity: 1;
  }
  100% {
    transform: scale(1.18);
    opacity: 0;
  }
}
@keyframes ink {
  from {
    opacity: 0;
    filter: blur(3px);
    transform: translateY(4px);
  }
  to {
    opacity: 1;
    filter: none;
    transform: none;
  }
}
@keyframes fade {
  from {
    opacity: 0;
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.2s !important;
  }
}
