/* The effect stays in the recipient stacking context, behind the message. */
#recipient .message-magic-canvas {
  position: fixed;
  inset: 0;
  z-index: 6;
  display: block;
  width: 100vw;
  height: 100dvh;
  pointer-events: none;
}

#recipient {
  --message-glow: #f4c478;
  --message-glow-soft: #ffd69047;
  --message-glow-faint: #ffcf781a;
}
body[data-theme="arctic"] #recipient {
  --message-glow: #e8e9ca;
  --message-glow-soft: #b9eee647;
  --message-glow-faint: #b8e9f51a;
}
body[data-theme="neon"] #recipient {
  --message-glow-soft: #e5c5ee3d;
  --message-glow-faint: #c2daef1a;
}

#recipient.opening-revealed .after-card { animation: none; }

/* A slow breath of light travels out of the parcel, with no full-screen flash. */
#recipient.gift-magic-opening::after {
  inset: 0;
  z-index: 1;
  background:
    radial-gradient(ellipse at 50% var(--magic-source-y, 53%), var(--message-glow-soft), transparent 54%),
    radial-gradient(ellipse at 50% 29%, var(--message-glow-faint), transparent 55%);
  mix-blend-mode: screen;
  opacity: 0;
  transform-origin: 50% var(--magic-source-y, 53%);
  animation: giftMagicBreath 2.8s ease-out both;
}

#recipient.message-arriving::after {
  inset: 0;
  z-index: 1;
  background:
    radial-gradient(ellipse at 50% 36%, var(--message-glow-soft), transparent 56%),
    radial-gradient(ellipse at 50% 62%, var(--message-glow-faint), transparent 58%);
  mix-blend-mode: screen;
  opacity: 0;
  animation: messageMagicAfterglow 3.1s ease-out both;
}

#recipient.message-arriving #gift-card {
  z-index: 7;
  transform-origin: 50% 78%;
  animation: messageMagicArrival 1.12s cubic-bezier(.18, .72, .24, 1) both;
}

/* A masked highlight catches only the paper's rim, leaving the ink clear. */
#recipient.message-arriving #gift-card::before {
  inset: -2px;
  padding: 2px;
  border: 0;
  border-radius: inherit;
  background: linear-gradient(112deg, transparent 23%, #f4cd8899 38%, #fff5cf 47%, #f9dc9dbb 52%, transparent 68%);
  background-size: 260% 100%;
  -webkit-mask: linear-gradient(#fff 0 0) content-box, linear-gradient(#fff 0 0);
  -webkit-mask-composite: xor;
  mask: linear-gradient(#fff 0 0) content-box, linear-gradient(#fff 0 0);
  mask-composite: exclude;
  opacity: 0;
  animation: messageMagicRim 2.45s .24s ease-out both;
}

#recipient.message-arriving #gift-card::after {
  color: #d9ab62;
  text-shadow: 0 0 9px #f0c46e99;
  animation: messageMagicStar 2.3s .3s ease-out both;
}

#recipient.message-arriving #card-to {
  animation: messageMagicInk .52s .16s ease-out both;
}
#recipient.message-arriving #card-message {
  animation: messageMagicInk .7s .3s ease-out both;
}
#recipient.message-arriving #card-from {
  animation: messageMagicInk .56s .52s ease-out both;
}
#recipient.message-arriving #ticket {
  animation: messageMagicInk .56s .65s ease-out both;
}
#recipient.message-arriving .after-card {
  z-index: 8;
  animation: messageMagicActions .5s .9s ease-out both;
}

@keyframes giftMagicBreath {
  0% { opacity: 0; transform: scale(.76); }
  24% { opacity: .73; }
  60% { opacity: .4; transform: scale(1.07); }
  100% { opacity: 0; transform: scale(1.2); }
}
@keyframes messageMagicAfterglow {
  0% { opacity: .3; transform: scale(.9); }
  24% { opacity: .7; }
  65% { opacity: .31; }
  100% { opacity: 0; transform: scale(1.12); }
}
@keyframes messageMagicArrival {
  0% {
    opacity: 0;
    transform: perspective(800px) translateY(var(--card-lift, 50px)) scale(.91) rotateX(9deg) rotate(2.4deg);
    box-shadow: 0 3px 0 #a17a4f, 0 15px 32px #09131f75, 0 0 44px var(--message-glow-soft);
  }
  28% { opacity: 1; }
  72% {
    transform: perspective(800px) translateY(-3px) scale(1.008) rotateX(0) rotate(-.7deg);
    box-shadow: 0 2px 0 #a17a4f, 0 13px 30px #09131f80, 0 0 25px var(--message-glow-faint);
  }
  100% {
    opacity: 1;
    transform: perspective(800px) translateY(0) scale(1) rotateX(0) rotate(-.45deg);
    box-shadow: 0 2px 0 #a17a4f, 0 12px 28px #09131f80;
  }
}
@keyframes messageMagicRim {
  0% { opacity: 0; background-position: 140% 0; }
  16% { opacity: .88; }
  69% { opacity: .56; }
  100% { opacity: 0; background-position: -80% 0; }
}
@keyframes messageMagicStar {
  0% { opacity: .25; transform: scale(.7) rotate(-12deg); }
  28% { opacity: 1; transform: scale(1.15) rotate(4deg); }
  62% { opacity: .85; }
  100% { opacity: 1; transform: scale(1) rotate(0); }
}
@keyframes messageMagicInk {
  0% { opacity: 0; transform: translateY(6px); clip-path: inset(0 0 64% 0); }
  100% { opacity: 1; transform: translateY(0); clip-path: inset(0); }
}
@keyframes messageMagicActions {
  0% { opacity: 0; transform: translateY(9px); }
  100% { opacity: 1; transform: translateY(0); }
}

@media (prefers-reduced-motion: reduce) {
  #recipient .message-magic-canvas { display: none; }
  #recipient.gift-magic-opening::after,
  #recipient.message-arriving::after { animation: none; opacity: 0; transform: none; }
  #recipient.message-arriving #gift-card {
    animation: none;
    opacity: 1;
    transform: none;
    box-shadow: 0 2px 0 #a17a4f, 0 12px 28px #09131f80, 0 0 16px var(--message-glow-faint);
  }
  #recipient.message-arriving #gift-card::before { animation: none; opacity: 0; }
  #recipient.message-arriving #gift-card::after { animation: none; transform: none; }
  #recipient.message-arriving :is(#card-to, #card-message, #card-from, #ticket, .after-card) {
    animation: none;
    opacity: 1;
    transform: none;
    clip-path: none;
  }
}
