/* Layout: una sola tarjeta de papel sobre fondo nocturno; la portada se abre
   como un libro y descubre el pastel (velas que se apagan) y el mensaje.
   Colores y tipografías: tokens.css. Colores del pastel: pastel.css. */

[hidden] { display: none !important; } /* que display:flex no gane a hidden */
:root {
  /* deja libres la muesca y la barra inferior del celular */
  padding-top: env(safe-area-inset-top, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}
html, body { height: 100%; }
body {
  margin: 0;
  background:
    radial-gradient(ellipse at 50% 0%, var(--night-2), transparent 70%),
    var(--night);
  color: var(--paper);
  font-family: var(--font-body);
  font-size: 1rem;
  line-height: 1.55;
  padding-left: max(16px, env(safe-area-inset-left, 0px));
  padding-right: max(16px, env(safe-area-inset-right, 0px));
  padding-block: 32px;
  box-sizing: border-box;
  display: grid;
  place-items: center;
  overflow-x: hidden;
}

.stage {
  width: min(380px, 100%);
  perspective: 1600px;
  display: grid;
  gap: 16px;
  justify-items: center;
}

.card {
  position: relative;
  width: 100%;
  transform-style: preserve-3d;
}

/* ---------- Interior ---------- */
.inside {
  background: var(--paper);
  color: var(--ink);
  border-radius: 6px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.35);
  min-height: 540px;
  padding: 28px 24px 24px;
  box-sizing: border-box;
  display: grid;
  gap: 18px;
  align-content: start;
}

.card.open .inside { cursor: pointer; } /* un clic en el interior cierra la tarjeta */

.eyebrow {
  font-size: 0.75rem;
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--ink-soft);
  text-align: center;
  margin: 0;
}

.cake-wrap { display: grid; justify-items: center; gap: 0; }

.candles {
  display: flex;
  gap: 14px;
  justify-content: center;
  height: 64px;
  align-items: flex-end;
  position: relative;
  z-index: 1;
}

.candle {
  appearance: none;
  border: 0;
  padding: 0;
  width: 10px;
  height: 38px;
  background: repeating-linear-gradient(160deg, var(--mint) 0 6px, var(--paper) 6px 11px);
  border-radius: 3px 3px 0 0;
  position: relative;
  cursor: pointer;
}
.candle::before { /* mecha */
  content: "";
  position: absolute;
  left: 50%;
  top: -5px;
  width: 2px;
  height: 6px;
  background: var(--ink);
  transform: translateX(-50%);
}
.candle::after { /* llama */
  content: "";
  position: absolute;
  left: 50%;
  bottom: 100%;
  margin-bottom: 3px;
  width: 12px;
  height: 20px;
  background: radial-gradient(ellipse at 50% 70%, #fff 0 18%, var(--flame) 40%, var(--marigold) 70%, transparent 72%);
  border-radius: 50% 50% 50% 50% / 60% 60% 40% 40%;
  transform: translateX(-50%);
  transform-origin: 50% 100%;
  filter: drop-shadow(0 0 6px var(--flame));
  animation: flicker 0.9s ease-in-out infinite alternate;
  transition: opacity 0.25s, transform 0.25s;
}
.candle:nth-child(2n)::after { animation-duration: 1.1s; }
.candle:nth-child(3n)::after { animation-duration: 0.75s; }
.candle.out::after { opacity: 0; transform: translateX(-50%) scale(0.2); animation: none; }
.candle.out .smoke { animation: smoke 1.2s ease-out forwards; }
.candle:focus-visible { outline: 2px solid var(--accent); outline-offset: 4px; }

/* Velas con forma de número (campo "numero" del JSON, ej. 18): cada cifra es una vela.
   Reusan la mecha (::before), la llama (::after) y el humo de las velas normales;
   solo cambia el cuerpo: la cifra en letra display con las mismas rayas. */
.candles.numbers { height: 112px; gap: 2px; }
.candle.digit {
  width: auto;
  height: auto;
  padding: 0 3px;
  border-radius: 0;
  font-family: var(--font-display);
  font-size: 84px;
  line-height: 0.82;
  background: repeating-linear-gradient(160deg, var(--mint) 0 8px, var(--paper) 8px 15px);
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  -webkit-text-stroke: 2px var(--accent-deep);
  margin-bottom: -4px; /* la cifra se "clava" en el pastel, como las velas normales */
}

.smoke {
  position: absolute;
  left: 50%;
  bottom: 100%;
  width: 8px;
  height: 8px;
  margin-left: -4px;
  border-radius: 50%;
  background: var(--ink-soft);
  opacity: 0;
  pointer-events: none;
}

@keyframes flicker {
  from { transform: translateX(-50%) rotate(-4deg) scaleY(1); }
  to   { transform: translateX(-50%) rotate(4deg) scaleY(1.12); }
}
@keyframes smoke {
  0%   { opacity: 0.5; transform: translateY(0) scale(1); }
  100% { opacity: 0; transform: translateY(-34px) scale(2.6); }
}

/* El pastel es un SVG que dibuja pastel.js; su borde superior es la cara
   de arriba del piso alto, así las velas (encima, en el flujo) quedan clavadas en él. */
.cake {
  display: block;
  width: 260px;
  max-width: 100%;
  height: auto;
  margin-top: -12px; /* la base de las velas queda en el centro de la cara de arriba (y = 12) */
}

/* Al abrir la tarjeta, el glaseado escurre: las gotas crecen desde arriba */
.card.open .cake .drips {
  transform-box: fill-box;
  transform-origin: top;
  animation: drip 1.6s cubic-bezier(0.3, 0.7, 0.3, 1) 0.5s both;
}
@keyframes drip {
  from { transform: scaleY(0.35); }
  to   { transform: scaleY(1); }
}

/* Al apagar la última vela, el pastel da un saltito */
.cake.celebrate {
  transform-origin: 50% 100%;
  animation: hop 0.75s cubic-bezier(0.3, 1.6, 0.5, 1);
}
@keyframes hop {
  0%   { transform: translateY(0) scale(1, 1); }
  30%  { transform: translateY(-10px) scale(0.98, 1.03); }
  60%  { transform: translateY(0) scale(1.03, 0.97); }
  100% { transform: translateY(0) scale(1, 1); }
}

.wish {
  text-align: center;
  margin: 0;
  font-size: 0.9rem;
  color: var(--ink-soft);
  min-height: 1.55em;
}
.wish.done {
  color: var(--accent-deep);
  font-weight: 600;
}

.rule {
  border: 0;
  height: 0;
  border-top: 2px dotted rgba(43, 32, 48, 0.18);
  margin: 0;
}

.message { display: grid; gap: 10px; }
.message h2 {
  font-family: var(--font-display);
  font-weight: 400;
  font-size: 1.7rem;
  line-height: 1.15;
  margin: 0;
  color: var(--accent-deep);
  text-wrap: balance;
}
.message p { margin: 0; }
.signature {
  font-family: var(--font-hand);
  font-size: 1.5rem;
  line-height: 1.2;
  text-align: right;
  color: var(--ink);
  margin-top: 6px;
}

/* ---------- Portada ---------- */
/* La portada es una hoja con dos caras que gira sobre su borde izquierdo (bisagra).
   Al abrir: gira hasta -178° mostrando su cara interior y al final se desvanece.
   Al cerrar: reaparece al instante en -178° y gira de vuelta: el mismo recorrido al revés. */
.cover {
  --turn: 1.1s cubic-bezier(0.6, 0.05, 0.25, 1);
  position: absolute;
  inset: 0;
  border-radius: 6px;
  transform-origin: left center;
  transform-style: preserve-3d;
  transition: transform var(--turn), opacity 0s; /* cerrar: visible desde el primer instante */
  background: none;
  cursor: pointer;
  border: 0;
  padding: 0;
  color: var(--paper);
  font: inherit;
  text-align: center;
  z-index: 2;
}
.cover:focus-visible { outline: 3px solid var(--marigold); outline-offset: 4px; }

.cover-face {
  position: absolute;
  inset: 0;
  border-radius: 6px;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.35);
}
.cover-front {
  background:
    radial-gradient(circle, var(--marigold) 2.5px, transparent 3px) 0 0 / 34px 34px,
    radial-gradient(circle, var(--mint) 2px, transparent 2.5px) 17px 17px / 34px 34px,
    var(--accent);
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.35), inset 0 0 0 10px var(--accent), inset 0 0 0 12px var(--paper);
  display: grid;
  place-items: center;
  padding: 28px 18px;
  box-sizing: border-box;
  container-type: inline-size; /* el título se mide contra el ancho de la tarjeta */
}
.cover-back { /* cara interior de la portada: papel con sombra hacia el lomo */
  transform: rotateY(180deg);
  background:
    linear-gradient(to left, rgba(43, 32, 48, 0.16), transparent 35%),
    var(--paper);
}

.cover-inner {
  display: grid;
  gap: 14px;
  justify-items: center;
  background: var(--accent);
  padding: 26px 12px;
  border-radius: 4px;
  width: 100%;
  box-sizing: border-box;
  min-width: 0;
}
.cover-title {
  font-family: var(--font-display);
  font-size: clamp(1.7rem, 11cqi, 2.6rem);
  line-height: 1.02;
  margin: 0;
  text-wrap: balance;
  text-shadow: 3px 3px 0 var(--accent-deep);
}
.cover-to {
  font-family: var(--font-hand);
  font-size: 1.6rem;
  margin: 0;
}
.seal {
  margin-top: 10px;
  width: 76px;
  height: 76px;
  border-radius: 50%;
  background: var(--marigold);
  color: var(--ink);
  display: grid;
  place-items: center;
  font-weight: 600;
  font-size: 0.8rem;
  letter-spacing: 0.1em;
  text-transform: uppercase;
  box-shadow: 0 0 0 4px var(--accent), 0 0 0 6px var(--marigold);
  animation: pulse 2.2s ease-in-out infinite;
}
@keyframes pulse {
  0%, 100% { transform: scale(1); }
  50% { transform: scale(1.07); }
}

.card.open .cover {
  transform: rotateY(-178deg);
  opacity: 0;
  pointer-events: none;
  transition: transform var(--turn), opacity 0.4s 0.9s; /* abrir: se desvanece al terminar el giro */
}

/* ---------- Controles bajo la tarjeta ---------- */
.actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  justify-content: center;
}
.btn {
  appearance: none;
  font: inherit;
  font-size: 0.9rem;
  font-weight: 600;
  padding: 10px 16px;
  border-radius: 999px;
  border: 2px solid var(--paper);
  background: transparent;
  color: var(--paper);
  cursor: pointer;
}
.btn.primary { background: var(--marigold); border-color: var(--marigold); color: var(--ink); }
.btn:hover { filter: brightness(1.08); }
.btn:focus-visible { outline: 3px solid var(--mint); outline-offset: 3px; }

#confetti, #fireworks {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  pointer-events: none; /* delante de la tarjeta, pero los toques la atraviesan */
  z-index: 5;
}

/* ---------- Final "fuegos": cielo estrellado detrás de la tarjeta ---------- */
.stage { position: relative; z-index: 1; } /* la tarjeta queda por encima del cielo */
#nightsky {
  position: fixed;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  opacity: 0;
  transition: opacity 1.4s ease;
  background: radial-gradient(ellipse at 50% 110%, var(--sky-glow), var(--sky-deep) 65%);
}
#nightsky canvas { width: 100%; height: 100%; display: block; }
body.fiesta #nightsky { opacity: 1; }

@media (prefers-reduced-motion: reduce) {
  .cover, .card.open .cover { transition: opacity 0.2s; }
  .card.open .cover { transform: none; }
  .candle::after, .seal { animation: none; }
  .card.open .cake .drips, .cake.celebrate { animation: none; }
}
