/* ==========================================================================
   Para ti — experiencia nocturna: lirios, carta sellada y pergamino
   --------------------------------------------------------------------------
   1. Tipografías y variables
   2. Base
   3. Escena 1 · Noche, jardín, luciérnagas
   4. Escena 2 · Sobre y sello de lacre
   5. Escena 3 · Despliegue y pergamino
   6. Movimiento reducido
   ========================================================================== */

/* 1. Tipografías y variables ------------------------------------------------ */

@font-face {
  font-family: "Sorts Mill Goudy";
  font-style: italic;
  font-weight: 400;
  font-display: swap;
  src: url("fonts/sorts-mill-goudy-latin-400-italic.woff2") format("woff2");
}
@font-face {
  font-family: "Italianno";
  font-style: normal;
  font-weight: 400;
  font-display: swap;
  src: url("fonts/italianno-latin-400-normal.woff2") format("woff2");
}
@font-face {
  font-family: "Mrs Saint Delafield";
  font-style: normal;
  font-weight: 400;
  font-display: swap;
  src: url("fonts/mrs-saint-delafield-latin-400-normal.woff2") format("woff2");
}

:root {
  color-scheme: dark;

  /* Paleta */
  --night: #080D18;
  --night-2: #111B2B;
  --ivory: #F2E9D8;
  --champagne: #D8C5A3;
  --leaf: #314C3B;
  --wine: #743D46;
  --gold: #C4A77D;
  --ink: #3B2A24;

  /* Tipografía */
  --serif: "Sorts Mill Goudy", "Iowan Old Style", "Palatino Linotype", Palatino, Georgia, serif;
  --script: "Italianno", "Snell Roundhand", "Apple Chancery", "Segoe Script", cursive;

  /* Medidas */
  --env-w: min(68vw, 18.75rem, 46vh);
  --sheet-w: min(88vw, 27rem);
  --sheet-h: min(74vh, 40rem);

  /* Curvas */
  --ease-out: cubic-bezier(.16, 1, .3, 1);
  --ease-grow: cubic-bezier(.3, .05, .25, 1);

  /* Grano de papel (ruido SVG en mosaico) y borde irregular (lo genera script.js) */
  --grain: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='220' height='220'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.85' numOctaves='2' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 .42 0 0 0 0 .3 0 0 0 0 .15 .32 0 0 0 -.06'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
  --deckle: none;
}

/* 2. Base ------------------------------------------------------------------- */

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

html, body {
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: var(--night);
  color: var(--ivory);
  -webkit-text-size-adjust: 100%;
  overscroll-behavior: none;
}

body { font-family: var(--serif); -webkit-font-smoothing: antialiased; }

::selection { background: rgba(116, 61, 70, .32); color: inherit; }

.defs { position: absolute; }

.scene {
  position: fixed;
  inset: 0;
  overflow: hidden;
  overflow: clip;            /* clip: el navegador no puede desplazar la escena al enfocar la carta */
  background:
    radial-gradient(120% 62% at 78% -12%, #1B2942 0%, rgba(17, 27, 43, .55) 38%, transparent 70%),
    radial-gradient(100% 46% at 50% 108%, rgba(49, 76, 59, .34) 0%, transparent 72%),
    linear-gradient(180deg, #080D18 0%, #0B1322 58%, #080D18 100%);
}

/* 3. Escena 1 · Noche, jardín, luciérnagas ----------------------------------- */

.sky, .twinkles, .garden, .mist, .fireflies, .aura, .veil {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.sky { width: 100%; height: 100%; opacity: 0; animation: fade-in 2.4s ease-out .1s forwards; }

.twinkle {
  position: absolute;
  width: 2px; height: 2px;
  border-radius: 50%;
  background: var(--ivory);
  opacity: .15;
  animation: twinkle var(--t) ease-in-out var(--d) infinite;
}

/* Cada lirio es su propio <svg>: el vaivén se anima sobre el elemento completo
   (solo transform), así el navegador lo compone sin repintar. */
.lily, .undergrowth {
  position: absolute;
  bottom: 0;
  overflow: visible;
}
.lily {
  transform-origin: 50% 100%;
  animation: sway var(--sw) ease-in-out var(--sd) infinite alternate;
}

/* Estado por defecto = planta ya florecida. Las animaciones parten de un estado
   anterior (fill-mode: both), así sin animación todo queda visible. */
.stem {
  fill: none;
  stroke-dasharray: 1;
  stroke-dashoffset: 0;
  animation: stem-grow var(--dur) var(--ease-grow) var(--d) both;
}
.leaf   { animation: leaf-unfold var(--dur) var(--ease-out) var(--d) both; }
.blade  { animation: blade-grow var(--dur) var(--ease-out) var(--d) both; }
.bud-grow   { animation: bud-form 1.3s var(--ease-out) var(--d) both; }
.bloom-grow { animation: bud-form 1s var(--ease-out) var(--d) both; }
.petal {
  transform: rotate(var(--a));
  animation: petal-open var(--dur) cubic-bezier(.3, .6, .2, 1) var(--d) both;
}
.stamens { animation: stamen-out 1.1s var(--ease-out) var(--d) both; }
.glow    { animation: fade-in 2s ease-out var(--d) both; }

@keyframes stem-grow   { from { stroke-dashoffset: 1; } }
@keyframes leaf-unfold { from { transform: rotate(var(--fold)) scale(.02); opacity: 0; } 12% { opacity: 1; } }
@keyframes blade-grow  { from { transform: scale(.3, 0); } }
@keyframes bud-form    { from { transform: scale(0); } }
@keyframes petal-open  { from { transform: rotate(var(--a0)) scale(.46, .8); } }
@keyframes stamen-out  { from { transform: scale(.15); opacity: 0; } }
@keyframes sway        { from { transform: rotate(calc(var(--amp) * -1)); } to { transform: rotate(var(--amp)); } }
@keyframes fade-in     { from { opacity: 0; } to { opacity: 1; } }
@keyframes twinkle     { 0%, 100% { opacity: .12; } 50% { opacity: .85; } }

/* Bruma del suelo: los tallos nacen de la oscuridad */
.mist {
  background: linear-gradient(to top, #060A13 0%, rgba(6, 10, 19, .82) 5%, rgba(8, 13, 24, .35) 13%, transparent 24%);
}

.firefly {
  position: absolute;
  left: 0; top: 0;
  width: 30px; height: 30px;
  margin: -15px 0 0 -15px;
  border-radius: 50%;
  opacity: 0;
  background: radial-gradient(circle, rgba(246, 236, 180, .95) 0 5%, rgba(226, 208, 132, .4) 13%, rgba(196, 167, 125, .1) 36%, transparent 62%);
  will-change: transform, opacity;
}

/* Luz ambiental tenue alrededor de la carta */
.aura {
  opacity: 0;
  background: radial-gradient(62% 30% at 50% 45%, rgba(216, 197, 163, .1) 0%, rgba(196, 167, 125, .04) 45%, transparent 75%);
  transition: opacity 2.4s ease-out;
}
.scene.is-sealed .aura { opacity: 1; }
.scene.is-reading .aura { opacity: 0; }

/* Oscurecimiento para la lectura: el jardín sigue visible detrás */
.veil {
  opacity: 0;
  background: radial-gradient(90% 70% at 50% 50%, rgba(4, 7, 14, .5) 0%, rgba(4, 7, 14, .3) 100%);
  transition: opacity 1.4s ease-out;
}
.scene.is-reading .veil { opacity: 1; }

/* 4. Escena 2 · Sobre y sello de lacre --------------------------------------- */

.letter-stage {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: env(safe-area-inset-top, 0px) env(safe-area-inset-right, 0px) calc(env(safe-area-inset-bottom, 0px) + 7vh) env(safe-area-inset-left, 0px);
  pointer-events: none;
  opacity: 0;
  transform: translateY(28px);
  filter: blur(6px);
  transition: opacity 1.5s ease-out, transform 1.9s var(--ease-out), filter 1.5s ease-out;
}
.scene.is-sealed .letter-stage { opacity: 1; transform: none; filter: none; }

.envelope {
  appearance: none;
  display: grid;
  justify-items: center;
  gap: 1.5rem;
  margin: 0;
  padding: .75rem;
  border: 0;
  border-radius: 10px;
  background: none;
  color: inherit;
  font: inherit;
  cursor: pointer;
  pointer-events: auto;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
.envelope:disabled { cursor: default; }
.envelope:focus-visible { outline: 1px solid var(--gold); outline-offset: 6px; }

/* Flotación casi imperceptible */
.envelope-float { display: block; animation: float 7s ease-in-out infinite; }
.scene.is-opening .envelope-float { animation-play-state: paused; }
@keyframes float {
  0%, 100% { transform: translateY(0) rotate(-.35deg); }
  50%      { transform: translateY(-5px) rotate(.35deg); }
}

.envelope-art {
  position: relative;
  display: block;
  width: var(--env-w);
  aspect-ratio: 300 / 206;
  perspective: 900px;
}

.env-ground-shadow {
  position: absolute;
  left: 4%; right: 4%; top: 6%; bottom: -5%;
  border-radius: 8px;
  box-shadow: 0 26px 44px -16px rgba(0, 0, 0, .78), 0 6px 14px -4px rgba(0, 0, 0, .5);
}

.env-layer { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; }
.env-back  { z-index: 1; }
.env-front { z-index: 4; }

/* Hoja doblada: muestra el tercio superior del pergamino, a escala */
.letter {
  position: absolute;
  z-index: 3;
  left: 6%; top: 7%;
  width: 88%;
  aspect-ratio: var(--panel-ratio, 2.4);
  box-shadow: 0 2px 5px rgba(20, 12, 4, .35);
}
.letter .slice { position: absolute; left: 0; top: 0; transform-origin: 0 0; overflow: hidden; }

.env-flap {
  position: absolute;
  z-index: 5;
  left: 0; top: 0;
  width: 100%; height: 62.14%;   /* 128 / 206 */
  transform-origin: 50% 0;
  transform-style: preserve-3d;
}
.flap-face {
  position: absolute; inset: 0;
  width: 100%; height: 100%;
  overflow: visible;
  -webkit-backface-visibility: hidden;
  backface-visibility: hidden;
}
.flap-in { transform: rotateX(180deg); }

/* Sello: pequeño corazón de lacre en la punta de la solapa */
.seal {
  position: absolute;
  z-index: 6;
  left: 50%; top: 59.5%;
  width: 17.5%;
  aspect-ratio: 64 / 60;
  transform: translate(-50%, -50%);
  filter: drop-shadow(0 2px 2.5px rgba(10, 4, 6, .6));
  transition: transform .14s ease-out;
}
.seal::after {  /* anillo que invita a tocar */
  content: "";
  position: absolute;
  inset: -18%;
  border-radius: 50%;
  border: 1px solid var(--gold);
  opacity: 0;
}
.scene.is-sealed:not(.is-opening) .seal::after { animation: ping 3.4s ease-out 1.6s infinite; }
@keyframes ping {
  0%   { transform: scale(.7); opacity: 0; }
  18%  { opacity: .5; }
  70%, 100% { transform: scale(1.75); opacity: 0; }
}

.seal-half { position: absolute; inset: 0; width: 100%; height: 100%; overflow: visible; }
.seal-left  { clip-path: polygon(-20% -20%, 50% -20%, 50% 12%, 45% 30%, 54% 46%, 47% 62%, 52% 80%, 50% 120%, -20% 120%); }
.seal-right { clip-path: polygon(120% -20%, 50% -20%, 50% 12%, 45% 30%, 54% 46%, 47% 62%, 52% 80%, 50% 120%, 120% 120%); }

.seal.is-pressed { transform: translate(-50%, -50%) scale(.92); }
.seal.is-broken .seal-left  { animation: seal-fall-left 1s cubic-bezier(.5, 0, .9, .6) forwards; }
.seal.is-broken .seal-right { animation: seal-fall-right 1.05s cubic-bezier(.5, 0, .9, .6) .04s forwards; }
@keyframes seal-fall-left {
  22%  { transform: translate(-1.5px, 0) rotate(-2deg); opacity: 1; }
  100% { transform: translate(-16px, 72px) rotate(-26deg); opacity: 0; }
}
@keyframes seal-fall-right {
  22%  { transform: translate(1.5px, .5px) rotate(2deg); opacity: 1; }
  100% { transform: translate(13px, 84px) rotate(21deg); opacity: 0; }
}

.hint {
  font-family: var(--serif);
  font-style: italic;
  font-size: 1.125rem;
  letter-spacing: .16em;
  padding-left: .16em;
  color: var(--champagne);
  transition: opacity .5s ease-out;
  animation: breathe 3.2s ease-in-out infinite;
}
.scene.is-opening .hint { opacity: 0; animation: none; }
@keyframes breathe {
  0%, 100% { opacity: .62; transform: translateY(0); }
  50%      { opacity: 1; transform: translateY(-2px); }
}

/* 5. Escena 3 · Despliegue y pergamino --------------------------------------- */

/* Piel del papel: color, grano, bordes envejecidos y las dos marcas de doblez */
.sheet-skin {
  position: absolute;
  inset: 0;
  clip-path: var(--deckle);
  background:
    linear-gradient(to bottom, transparent calc(33.333% - 1px), rgba(92, 64, 30, .13) 33.333%, rgba(255, 251, 238, .4) calc(33.333% + 1px), transparent calc(33.333% + 2.5px)),
    linear-gradient(to bottom, transparent calc(66.666% - 1px), rgba(92, 64, 30, .13) 66.666%, rgba(255, 251, 238, .4) calc(66.666% + 1px), transparent calc(66.666% + 2.5px)),
    radial-gradient(125% 95% at 50% 42%, transparent 52%, rgba(150, 108, 58, .2) 100%),
    var(--grain),
    linear-gradient(165deg, #F4EAD4 0%, #EBDEC2 100%);
}
.slice > .sheet-skin { inset: auto; left: 0; top: 0; width: 100%; height: 300%; }

.fold, .parchment {
  position: absolute;
  inset: 0;
  margin: auto;
  width: var(--sheet-w);
  height: var(--sheet-h);
  visibility: hidden;
}

.fold { z-index: 8; perspective: 1500px; transform-origin: 0 0; pointer-events: none; }
.fold-panel { position: absolute; left: 0; width: 100%; height: 33.3334%; }
.fold-top { top: 0; z-index: 2; }
.fold-mid { top: 33.3333%; z-index: 1; transform-origin: 50% 0; transform-style: preserve-3d; }
.fold-bot { top: 100%; height: 100%; transform-origin: 50% 0; }
.fold-panel > .slice { position: absolute; inset: 0; overflow: hidden; }
.fold-mid > .slice > .sheet-skin { top: -100%; }
.fold-bot > .slice > .sheet-skin { top: -200%; }
.shade { position: absolute; inset: 0; background: #1A0F04; opacity: 0; }

.parchment { z-index: 9; }
.parchment-shadow {
  position: absolute;
  inset: 8px 6px 2px;
  box-shadow: 0 34px 70px -18px rgba(0, 0, 0, .85), 0 10px 22px -8px rgba(0, 0, 0, .55);
  opacity: 0;
  transition: opacity 1.2s ease-out;
}
.scene.is-unfolded .parchment-shadow { opacity: 1; }

.parchment-scroll {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  overflow-x: hidden;
  overflow-y: auto;
  overscroll-behavior: contain;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-y;
  scrollbar-width: thin;
  scrollbar-color: rgba(120, 88, 52, .35) transparent;
  -webkit-mask-image: linear-gradient(to bottom, transparent 0, #000 1.5rem, #000 calc(100% - 1.5rem), transparent 100%);
          mask-image: linear-gradient(to bottom, transparent 0, #000 1.5rem, #000 calc(100% - 1.5rem), transparent 100%);
}
.parchment-scroll::-webkit-scrollbar { width: 4px; }
.parchment-scroll::-webkit-scrollbar-thumb { background: rgba(120, 88, 52, .35); border-radius: 2px; }
.parchment-scroll:focus-visible { outline: 1px solid rgba(116, 61, 70, .5); outline-offset: -6px; }

/* margin-block:auto centra el mensaje si es corto y deja desplazarlo si es largo */
.parchment-inner {
  margin-block: auto;
  padding: 2.6rem clamp(1.6rem, 8vw, 2.75rem) 2.2rem;
}

.message {
  font-family: var(--script);
  font-size: clamp(1.95rem, 8.4vw, 2.3rem);
  line-height: 1.32;
  color: var(--ink);
  caret-color: var(--wine);
  overflow-wrap: break-word;
}
.message p { margin: 0 0 .75em; }
.message p:last-child { margin-bottom: 0; }
.message.is-brief { text-align: center; }
.message .heart {
  display: inline-block;
  width: .5em; height: .5em;
  margin-left: .08em;
  vertical-align: -.03em;
  fill: var(--wine);
}

/* Escritura: cada renglón se revela de izquierda a derecha con un borde difuso,
   como tinta que se asienta. */
.message .line {
  display: block;
  width: fit-content;
  white-space: nowrap;
  padding: .12em .45em .16em .3em;
  margin: -.12em -.45em -.16em -.3em;
  -webkit-mask: linear-gradient(90deg, #000 40%, transparent 50%) 100% 0 / 250% 100% no-repeat;
          mask: linear-gradient(90deg, #000 40%, transparent 50%) 100% 0 / 250% 100% no-repeat;
}
.message.is-brief .line { margin-inline: auto; }
.message .line.is-writing { animation: write var(--dur) cubic-bezier(.35, .1, .55, .95) forwards; }
.message .line.is-written { -webkit-mask: none; mask: none; }
@keyframes write {
  to { -webkit-mask-position: 0 0; mask-position: 0 0; }
}

/* Firma: iniciales con trazo fino de pluma, en tinta azul negra, y rúbrica debajo */
.signature {
  --pen: #1F2A44;
  position: relative;
  width: fit-content;
  margin: 1.1rem .4rem 0 auto;
  transform: rotate(-5deg);
  transform-origin: 100% 100%;
  color: var(--pen);
}
.signature:empty, .signature.is-off { display: none; }
.signature-name {
  display: block;
  font-family: "Mrs Saint Delafield", var(--script);
  font-size: clamp(3.3rem, 15vw, 4.1rem);
  line-height: 1.25;
  padding: 0 .5em 0 .35em;
  white-space: nowrap;
  -webkit-mask: linear-gradient(90deg, #000 40%, transparent 50%) 100% 0 / 250% 100% no-repeat;
          mask: linear-gradient(90deg, #000 40%, transparent 50%) 100% 0 / 250% 100% no-repeat;
}
.signature.is-signed .signature-name { animation: write 1.5s cubic-bezier(.4, 0, .3, 1) forwards; }
.signature-flourish {
  position: absolute;
  left: -4%; bottom: -.1em;
  width: 108%;
  overflow: visible;
}
.signature-flourish path {
  fill: none;
  stroke: var(--pen);
  stroke-width: 1.15;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-dasharray: 1;
  stroke-dashoffset: 1;
  transition: stroke-dashoffset 1.1s cubic-bezier(.5, 0, .2, 1) 1.35s;
}
.signature.is-signed .signature-flourish path { stroke-dashoffset: 0; }

.colophon {
  display: block;
  width: 5.25rem;
  margin: 1.9rem auto 0;
  overflow: visible;
}
.colophon path {
  fill: none;
  stroke: #A1814F;
  stroke-width: 1;
  stroke-linecap: round;
  stroke-linejoin: round;
  stroke-dasharray: 1;
  stroke-dashoffset: 1;
  transition: stroke-dashoffset 1.8s cubic-bezier(.4, 0, .2, 1);
}
.colophon path:nth-child(n+4) { transition-delay: .7s; }
.colophon.is-drawn path { stroke-dashoffset: 0; }

/* 6. Movimiento reducido ----------------------------------------------------- */
/* Sin crecimiento ni vaivén: el jardín aparece ya florecido con un fundido,
   y la carta se abre con transiciones de opacidad. */

@media (prefers-reduced-motion: reduce) {
  .lily, .lily *, .undergrowth *, .twinkle, .envelope-float, .hint, .seal::after { animation: none !important; }
  .garden { animation: fade-in 1.8s ease-out both; }
  .letter-stage { transform: none; filter: none; transition: opacity 1.2s ease-out; }
  .seal { transition: opacity .5s ease-out; }
  .seal.is-broken { opacity: 0; }
  .seal.is-broken .seal-half { animation: none; }
  .hint { opacity: .9; }
  .message .line, .signature-name { -webkit-mask: none; mask: none; animation: none !important; }
  .signature-flourish path { transition-duration: .01s; transition-delay: 0s; }
  .colophon path { transition-duration: .01s; transition-delay: 0s; }
}
