:root {
  --navy: #18232f;
  --navy-deep: #0e1014;
  --navy-soft: #232b34;
  --terracotta: #a76433;
  --rust: #682d10;
  --copper: #c88851;
  --sand: #caa883;
  --ivory: #d9cbb9;
  --olive: #736e50;
  --paper: #ead8c4;
  --paper-deep: #caa883;
  --ink: #f0e2cf;
  --charcoal: #18232f;
  --envelope-texture: url("./assets/envelope-texture.png");
}

* {
  box-sizing: border-box;
}

body {
  min-height: 100vh;
  margin: 0;
  font-family: Inter, system-ui, sans-serif;
  color: var(--ink);
  background:
    linear-gradient(rgba(24, 35, 47, 0.5), rgba(24, 35, 47, 0.28)),
    var(--envelope-texture) center / cover fixed,
    radial-gradient(circle at 74% 17%, rgba(200, 136, 81, 0.34), transparent 31%),
    linear-gradient(180deg, rgba(24, 35, 47, 0.98) 0 55%, rgba(202, 168, 131, 0.94) 55% 100%),
    repeating-linear-gradient(90deg, rgba(255, 255, 255, 0.08) 0 1px, transparent 1px 7px);
  overflow: hidden;
}

body::before {
  content: "";
  position: fixed;
  inset: 0;
  pointer-events: none;
  background:
    linear-gradient(90deg, rgba(217, 203, 185, 0.18), transparent 28%, rgba(104, 45, 16, 0.1)),
    repeating-linear-gradient(0deg, rgba(217, 203, 185, 0.07) 0 1px, transparent 1px 5px);
  mix-blend-mode: soft-light;
}

body::after {
  content: "";
  position: fixed;
  z-index: 2147483647;
  inset: 0;
  pointer-events: none;
  opacity: 0;
  background: #fff;
  transition: opacity 1.25s ease;
}

body.is-fading-to-white::after {
  opacity: 1;
}

.stage {
  display: grid;
  min-height: 100vh;
  place-items: center;
  padding: 24px;
  transition: padding 0.8s ease;
}

.card-scene {
  position: relative;
  display: grid;
  width: 100%;
  min-height: calc(100vh - 48px);
  place-items: center;
  perspective: 1400px;
  isolation: isolate;
  --scene-x: 0px;
  --scene-y: 0px;
}

.envelope {
  position: absolute;
  width: min(94vw, 760px);
  aspect-ratio: 1.62;
  padding: 0;
  border: 0;
  appearance: none;
  background: transparent;
  color: inherit;
  cursor: pointer;
  transform: translateX(0) translateY(8px) scale(0.98);
  transform-origin: center;
  transform-style: preserve-3d;
  will-change: transform, width;
}

.envelope:focus-visible {
  outline: 3px solid rgba(240, 226, 207, 0.85);
  outline-offset: 14px;
}

.envelope__inner {
  position: absolute;
  inset: 0;
  transform: scaleX(1);
  transform-style: preserve-3d;
}

.envelope__face {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  transform-style: preserve-3d;
}

.envelope__face--back {
  opacity: 1;
  transform: translateZ(1px);
}

.envelope__face--inside {
  opacity: 0;
  transform: translateZ(1px);
}

.envelope__back,
.envelope__front,
.envelope__flap,
.envelope__side {
  position: absolute;
  inset: 0;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
}

.envelope__back {
  z-index: 1;
  border-radius: 2px;
  background:
    linear-gradient(90deg, rgba(255, 255, 255, 0.22), transparent 34%, rgba(104, 45, 16, 0.08)),
    linear-gradient(135deg, #eadbc8, #d5b993);
  box-shadow: 0 32px 28px rgba(14, 16, 20, 0.28);
}

.envelope__back::after {
  content: "";
  position: absolute;
  inset: 0;
  background: repeating-linear-gradient(90deg, rgba(104, 45, 16, 0.04) 0 1px, transparent 1px 4px);
  opacity: 0.8;
}

.envelope__address {
  position: absolute;
  z-index: 10;
  left: 22%;
  top: 32%;
  display: grid;
  gap: 8px;
  margin: 0;
  min-width: min(390px, 52vw);
  text-align: left;
  color: var(--terracotta);
  transform: rotate(-1deg);
}

.envelope__from,
.envelope__to,
.envelope__name {
  margin: 0;
  font-family: "Cormorant Garamond", Georgia, serif;
  font-style: italic;
  font-weight: 700;
  line-height: 0.95;
  letter-spacing: 0;
}

.envelope__from {
  font-size: clamp(36px, 5.2vw, 64px);
}

.envelope__to {
  font-size: clamp(28px, 3.9vw, 46px);
  color: var(--terracotta);
  opacity: 0.92;
}

.envelope__name {
  max-width: min(390px, 52vw);
  margin: 1px 0 -5px;
  overflow: hidden;
  color: var(--terracotta);
  font-size: clamp(28px, 4.2vw, 50px);
  white-space: nowrap;
}

.envelope__line {
  display: block;
  width: min(320px, 46vw);
  height: 2px;
  margin-top: 2px;
  background: linear-gradient(90deg, rgba(167, 100, 51, 0.72), rgba(167, 100, 51, 0.18));
  border-radius: 999px;
}

.stamp {
  position: absolute;
  z-index: 10;
  right: 38px;
  top: 34px;
  width: 72px;
  aspect-ratio: 0.78;
  padding: 6px;
  background: #f0e2cf;
  clip-path: polygon(0 4%, 6% 0, 12% 4%, 18% 0, 24% 4%, 30% 0, 36% 4%, 42% 0, 48% 4%, 54% 0, 60% 4%, 66% 0, 72% 4%, 78% 0, 84% 4%, 90% 0, 96% 4%, 100% 10%, 96% 16%, 100% 22%, 96% 28%, 100% 34%, 96% 40%, 100% 46%, 96% 52%, 100% 58%, 96% 64%, 100% 70%, 96% 76%, 100% 82%, 96% 88%, 100% 94%, 94% 100%, 88% 96%, 82% 100%, 76% 96%, 70% 100%, 64% 96%, 58% 100%, 52% 96%, 46% 100%, 40% 96%, 34% 100%, 28% 96%, 22% 100%, 16% 96%, 10% 100%, 4% 96%, 0 90%, 4% 84%, 0 78%, 4% 72%, 0 66%, 4% 60%, 0 54%, 4% 48%, 0 42%, 4% 36%, 0 30%, 4% 24%, 0 18%, 4% 12%);
  box-shadow: 0 4px 10px rgba(14, 16, 20, 0.25);
}

.stamp__photo {
  width: 100%;
  height: 100%;
  background:
    radial-gradient(circle at 50% 34%, #f2ddd4 0 14%, transparent 15%),
    radial-gradient(circle at 62% 30%, #c88851 0 13%, transparent 14%),
    linear-gradient(135deg, var(--navy), var(--terracotta));
  filter: grayscale(1);
}

.envelope__side--left {
  z-index: 5;
  clip-path: polygon(0 0, 52% 51%, 0 100%);
  background:
    linear-gradient(90deg, #dec5a6, #eadbc8);
}

.envelope__side--right {
  z-index: 5;
  clip-path: polygon(100% 0, 48% 51%, 100% 100%);
  background:
    linear-gradient(270deg, #cfad83, #eadbc8);
}

.envelope__front {
  z-index: 7;
  clip-path: polygon(0 100%, 50% 43%, 100% 100%);
  background:
    linear-gradient(180deg, #eadbc8, #d2b58e);
  box-shadow: inset 0 2px 0 rgba(255, 255, 255, 0.55);
}

.envelope__flap {
  z-index: 8;
  transform-origin: 50% 0;
  clip-path: polygon(0 0, 100% 0, 50% 60%);
  background:
    linear-gradient(180deg, #eadbc8, #caa883);
}

.tap-cue {
  position: absolute;
  z-index: 12;
  bottom: clamp(42px, 8vh, 72px);
  left: 50%;
  display: inline-flex;
  align-items: center;
  gap: 12px;
  padding: 12px 18px;
  border: 1px solid rgba(240, 226, 207, 0.2);
  border-radius: 999px;
  color: rgba(240, 226, 207, 0.94);
  overflow: hidden;
  background: rgba(14, 16, 20, 0.42);
  box-shadow: 0 18px 38px rgba(14, 16, 20, 0.22);
  transform: translateX(-50%);
  transition:
    opacity 0.45s ease,
    transform 0.45s ease;
}

.tap-cue__icon {
  position: relative;
  width: 18px;
  height: 24px;
  border: 2px solid currentColor;
  border-radius: 10px;
  animation: tapFloat 1.55s ease-in-out infinite;
}

.tap-cue__icon::before {
  content: "";
  position: absolute;
  left: 50%;
  top: 5px;
  width: 4px;
  height: 4px;
  border-radius: 50%;
  background: currentColor;
  transform: translateX(-50%);
}

.tap-cue__icon::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  width: 34px;
  height: 34px;
  border: 1px solid currentColor;
  border-radius: 50%;
  opacity: 0;
  transform: translate(-50%, -50%) scale(0.62);
  animation: tapPulse 1.55s ease-out infinite;
}

.tap-cue__text {
  font: 700 13px/1 Inter, system-ui, sans-serif;
  text-transform: uppercase;
  white-space: nowrap;
}

.card-scene.is-open .envelope {
  animation: envelopeSettle 1.45s 2.35s cubic-bezier(0.22, 0.72, 0.18, 1) forwards;
}

.card-scene.is-open .envelope__inner {
  animation: envelopeFlip 1.55s 0.2s cubic-bezier(0.42, 0, 0.22, 1) forwards;
}

.card-scene.is-open .envelope__face--back {
  animation: hideEnvelopeBack 1.55s 0.2s linear forwards;
}

.card-scene.is-open .envelope__face--inside {
  animation: showEnvelopeInside 1.55s 0.2s linear forwards;
}

.card-scene.is-open .envelope__flap {
  animation: flapOpen 1.65s 2.6s cubic-bezier(0.24, 0.72, 0.2, 1) forwards;
}

.card-scene.is-open .tap-cue {
  opacity: 0;
  pointer-events: none;
  transform: translateX(-50%) translateY(10px);
}

.card-scene.is-replaying .envelope,
.card-scene.is-replaying .envelope__inner,
.card-scene.is-replaying .envelope__face--back,
.card-scene.is-replaying .envelope__face--inside,
.card-scene.is-replaying .envelope__flap {
  animation: none;
}

@keyframes envelopeFlip {
  0% {
    transform: scaleX(1);
  }
  49.9% {
    transform: scaleX(0.035);
  }
  50.1% {
    transform: scaleX(0.035);
  }
  100% {
    transform: scaleX(1);
  }
}

@keyframes hideEnvelopeBack {
  0%,
  49.9% {
    opacity: 1;
  }
  50%,
  100% {
    opacity: 0;
  }
}

@keyframes showEnvelopeInside {
  0%,
  49.9% {
    opacity: 0;
  }
  50%,
  100% {
    opacity: 1;
  }
}

@keyframes envelopeSettle {
  0% {
    width: min(94vw, 760px);
    transform: translateX(0) translateY(8px) scale(0.98);
  }
  100% {
    width: min(460px, 80vw);
    transform: translate(var(--scene-x), calc(96px + var(--scene-y))) scale(0.9);
  }
}

@keyframes flapOpen {
  0% {
    transform: rotateX(0deg);
    z-index: 9;
  }
  65% {
    transform: rotateX(154deg);
    z-index: 3;
  }
  100% {
    transform: rotateX(158deg);
    z-index: 3;
  }
}

@keyframes tapFloat {
  0%,
  100% {
    transform: translateY(0);
  }
  50% {
    transform: translateY(-5px);
  }
}

@keyframes tapPulse {
  0% {
    opacity: 0.65;
    transform: translate(-50%, -50%) scale(0.58);
  }
  80%,
  100% {
    opacity: 0;
    transform: translate(-50%, -50%) scale(1.35);
  }
}

@media (max-width: 760px) {
  body {
    overflow: hidden;
  }

  .stage {
    gap: 24px;
    padding: 18px;
  }

  .card-scene {
    min-height: 610px;
  }

  .envelope,
  .card-scene.is-open .envelope {
    width: min(94vw, 520px);
    transform: translateY(84px) scale(0.86);
  }

  .card-scene.is-open .envelope {
    animation: envelopeSettleMobile 1.25s 2.35s cubic-bezier(0.22, 0.72, 0.18, 1) forwards;
  }

  .envelope__address {
    left: 13%;
    top: 30.5%;
    min-width: 0;
    max-width: 62vw;
  }

  .envelope__from {
    font-size: clamp(25px, 6.8vw, 33px);
  }

  .envelope__to {
    font-size: clamp(21px, 5.5vw, 28px);
  }

  .envelope__name {
    max-width: 58vw;
    font-size: clamp(21px, 5.8vw, 29px);
  }

  .envelope__line {
    width: min(250px, 58vw);
  }

  .stamp {
    right: 18px;
    top: 18px;
    width: 48px;
  }

  .tap-cue {
    bottom: clamp(30px, 8vh, 58px);
  }

  @keyframes envelopeSettleMobile {
    0% {
      width: min(94vw, 520px);
      transform: translateY(84px) scale(0.86);
    }
    100% {
      width: min(94vw, 520px);
      transform: translate(var(--scene-x), calc(84px + var(--scene-y))) scale(0.86);
    }
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.01ms !important;
    animation-delay: 0ms !important;
    transition-duration: 0.01ms !important;
  }
}

body::after {
  transition: opacity 1.25s ease !important;
}
