/* ==========================================================================
   Happy Birthday: an envelope that opens into a letter
   ========================================================================== */

:root {
  /* Palette */
  --bg: #f1ece9;
  --bg-deep: #f7ded4;
  --ink: #3b2523;
  --ink-soft: #7a5a53;
  --accent: #d9553f;
  /* --paper: #fffaf2; */
  --paper: hsl(0, 15%, 98%);
  --paper-line: #eedfd2;

  /* --env: #e46f58;
  --env-side: #dc644d;
  --env-bottom: #ec7f67;
  --env-flap: #f08b72;
  --env-inside: #b9493a;
  --env-edge: #c4533f; */
  --env: #fffdfa;          /* body of the envelope */
  --env-side: #f1ece6;     /* side flaps, slightly in shadow */
  --env-bottom: #faf7f3;   /* bottom flap */
  --env-flap: #ffffff;     /* top flap, catches the most light */
  --env-inside: #e6dfd7;   /* inside of the envelope */
  --env-inside-deep: #d6ccc1;
  --env-lining: #eee8e1;   /* underside of the top flap */
  --env-edge: #f1e3d3;     /* crease under the bottom flap */
  --env-shadow: 80 55 40;  /* RGB channels for shadows on/around the envelope */

  /* background: radial-gradient(circle at 35% 30%, var(--gold-light), var(--gold) 45%, var(--gold-dark)); */
  --seal: #c42b2b;
  --seal-light: #e4574d;
  --seal-dark: #8e1b1f;

  --gold: #e9b949;
  --gold-light: #f9dc8c;
  --gold-dark: #b98522;

  /* Envelope size: fits narrow phones, big desktops and short landscape screens */
  --w: min(84vw, 440px, 66vh);
  --h: calc(var(--w) / 1.5);

  /* Motion */
  --ease-out: cubic-bezier(.22, 1, .36, 1);
  --ease-in-out: cubic-bezier(.65, 0, .35, 1);

  /* Type */
  --font-serif: "Fraunces", Georgia, "Times New Roman", serif;
  /* --font-hand: "Caveat", "Bradley Hand", "Segoe Print", cursive; */
  --font-hand: "Charm", "Bradley Hand", "Segoe Print", cursive;

  /* Subtle paper grain */
  --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='3' stitchTiles='stitch'/%3E%3CfeColorMatrix type='saturate' values='0'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)' opacity='.07'/%3E%3C/svg%3E");
}

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

html,
body {
  margin: 0;
}

body {
  min-height: 100vh;
  font-family: var(--font-serif);
  color: var(--ink);
  background: var(--paper);
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
}

[hidden] {
  display: none !important;
}

/* --------------------------------------------------------------------------
   Stage
   -------------------------------------------------------------------------- */

.stage {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  overflow: hidden;
  background:
    radial-gradient(55% 45% at 50% 46%, #fff4ec 0%, transparent 100%),
    radial-gradient(90% 60% at 50% 115%, var(--bg-deep) 0%, transparent 100%),
    var(--bg);
}

.stage.is-resetting,
.stage.is-resetting * {
  transition: none !important;
}

.stage.is-returning .envelope-wrap {
  animation: return-in .7s var(--ease-out) both;
}

@keyframes return-in {
  from { opacity: 0; transform: translateY(24px) scale(.96); }
}

/* --------------------------------------------------------------------------
   Envelope
   -------------------------------------------------------------------------- */

.envelope-wrap {
  position: relative;
  width: var(--w);
  height: var(--h);
  transition:
    transform 1s var(--ease-in-out),
    opacity .5s ease;
}

.envelope-float {
  width: 100%;
  height: 100%;
}

.envelope-shadow {
  position: absolute;
  left: 6%;
  right: 6%;
  bottom: -13%;
  height: 14%;
  border-radius: 50%;
  background: radial-gradient(closest-side, rgb(120 40 25 / .28), transparent);
  animation: float-shadow 5s ease-in-out infinite;
  pointer-events: none;
}

@keyframes float-shadow {
  0%, 100% { transform: scale(1);   opacity: 1; }
  50%      { transform: scale(.9);  opacity: .75; }
}

.envelope {
  /* reset button */
  appearance: none;
  margin: 0;
  padding: 0;
  border: 0;
  background: none;
  font: inherit;
  color: inherit;
  -webkit-tap-highlight-color: transparent;

  position: relative;
  display: block;
  width: 100%;
  height: 100%;
  border-radius: 6px;
  cursor: pointer;
  perspective: 1400px;
  transition: transform .4s var(--ease-out);
}

.envelope:hover {
  transform: translateY(-5px) rotate(-1.5deg);
}

.envelope:active {
  transform: translateY(-2px) rotate(-1deg) scale(.985);
}

.envelope:focus-visible {
  outline: 3px solid var(--accent);
  outline-offset: 12px;
}

.envelope > span {
  position: absolute;
  display: block;
}

/* Inside back of the envelope */
.envelope__back {
  inset: 0;
  z-index: 0;
  border-radius: 6px;
  background: linear-gradient(to bottom, var(--env-inside-deep), var(--env-inside) 40%);
  box-shadow:
    0 0 0 1px rgb(var(--env-shadow) / .06),
    0 1px 2px rgb(var(--env-shadow) / .16),
    0 22px 40px -18px rgb(var(--env-shadow) / .4);
}

/* The letter while it's still inside */
.envelope__letter {
  left: 5%;
  right: 5%;
  top: 6%;
  height: 88%;
  z-index: 2;
  overflow: hidden;
  border-radius: 3px;
  background: var(--grain), var(--paper);
  box-shadow: 0 1px 3px rgb(var(--env-shadow) / .2);
  transition: transform 1s var(--ease-in-out);
}

/* Shared between the mini letter and its copy in the full-page letter */
.mini {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: calc(var(--w) * .028);
  width: 100%;
  height: 100%;
  padding-top: calc(var(--w) * .06);
}

.mini__title {
  font-family: var(--font-hand);
  font-weight: 700;
  font-size: calc(var(--w) * .085);
  line-height: 1;
  color: var(--accent);
  margin-bottom: calc(var(--w) * .015);
  white-space: nowrap;
}

.mini__line {
  display: block;
  height: calc(var(--w) * .011);
  border-radius: 99px;
  background: var(--paper-line);
}

.mini__line:nth-child(2) { width: calc(var(--w) * .62); }
.mini__line:nth-child(3) { width: calc(var(--w) * .68); }
.mini__line:nth-child(4) { width: calc(var(--w) * .58); }
.mini__line:nth-child(5) { width: calc(var(--w) * .36); }

/* Front pocket: left & right flaps, with the bottom flap layered on top */
.envelope__pocket {
  inset: 0;
  z-index: 3;
  border-radius: 6px;
  background: linear-gradient(90deg, var(--env-side), var(--env) 50%, var(--env-side));
  clip-path: polygon(0 0, 50% 52%, 100% 0, 100% 100%, 0 100%);
}

.envelope__pocket::before,
.envelope__pocket::after {
  content: "";
  position: absolute;
  inset: 0;
}

/* a fine crease along the edges of the bottom flap */
.envelope__pocket::before {
  background: var(--env-edge);
  clip-path: polygon(0 100%, 0 calc(100% - 1.5px), 50% calc(57% - 1.5px), 100% calc(100% - 1.5px), 100% 100%);
}

.envelope__pocket::after {
  background: linear-gradient(to top, var(--env), var(--env-bottom));
  clip-path: polygon(0 100%, 50% 57%, 100% 100%);
}

.envelope__to {
  left: 0;
  right: 0;
  bottom: 9%;
  z-index: 3;
  font-family: var(--font-hand);
  font-weight: 500;
  font-size: calc(var(--w) * .05);
  line-height: 1;
  text-align: center;
  /* color: #fff3ea; */
  color: var(--ink-soft);
  transform: rotate(-4deg);
}

/* Top flap (rotates open) */
.envelope__flap {
  left: 0;
  right: 0;
  top: 0;
  height: 58%;
  z-index: 4;
  transform-origin: 50% 0;
  filter: drop-shadow(0 2px 3px rgb(var(--env-shadow) / .2));
  transition:
    transform .7s var(--ease-in-out) .15s,
    filter .25s ease .15s,
    z-index 0s linear .5s; /* drop behind the letter as it passes edge-on */
}

.envelope__flap::before,
.envelope__flap::after {
  content: "";
  position: absolute;
  inset: 0;
  border-radius: 6px 6px 0 0;
  clip-path: polygon(0 0, 100% 0, 53.5% 94%, 50% 100%, 46.5% 94%);
}

.envelope__flap::before {
  background: linear-gradient(to bottom, var(--env-flap), var(--env) 90%);
}

/* the inside of the flap, shown once it has flipped over */
.envelope__flap::after {
  background: linear-gradient(to top, var(--env-lining), var(--env-inside));
  opacity: 0;
  transition: opacity 0s linear .5s;
}

/* Wax seal */
.envelope > .envelope__seal {
  left: 50%;
  top: 57%;
  z-index: 5;
  display: grid;
  place-items: center;
  width: calc(var(--w) * .15);
  aspect-ratio: 1;
  translate: -50% -50%;
  border-radius: 48% 52% 50% 50% / 52% 48% 52% 48%;
  background: radial-gradient(circle at 35% 30%, var(--seal-light), var(--seal) 45%, var(--seal-dark));
  box-shadow:
    0 2px 5px rgb(90 20 20 / .35),
    inset 0 0 0 calc(var(--w) * .012) rgb(255 255 255 / .12),
    inset 0 -2px 4px rgb(60 0 10 / .3);
  transition:
    transform .35s var(--ease-in-out),
    opacity .3s ease;
}

.envelope__seal svg {
  width: 46%;
  fill: var(--seal-dark);
  filter: drop-shadow(0 1px 0 rgb(255 190 180 / .45));
}

/* Hint under the envelope */
.hint {
  position: absolute;
  left: 50%;
  top: calc(100% + var(--h) * .28);
  translate: -50% 0;
  margin: 0;
  font-size: .78rem;
  font-weight: 500;
  letter-spacing: .24em;
  text-transform: uppercase;
  white-space: nowrap;
  color: var(--ink-soft);
  opacity: .55;
}

.hint__mouse { display: none; }

@media (hover: hover) and (pointer: fine) {
  .hint__touch { display: none; }
  .hint__mouse { display: inline; }
}

/* --------------------------------------------------------------------------
   Opening sequence (classes added by script.js)
   1. .is-open     seal pops, flap swings open
   2. .is-pulling  letter slides up and out, envelope drops to make room
   3. .is-leaving  letter takes over the page, envelope falls away
   -------------------------------------------------------------------------- */

.is-open .envelope,
.is-open .envelope:hover {
  transform: none;
  cursor: default;
}

.is-open .envelope-float,
.is-open .envelope-shadow {
  animation-play-state: paused;
}

.is-open .hint {
  opacity: 0;
}

.is-open .envelope__seal {
  transform: scale(.9) rotate(-25deg);
}

.is-open .envelope__flap {
  transform: rotateX(180deg);
  filter: none;
  z-index: 1;
}

.is-open .envelope__flap::after {
  opacity: 1;
}

.is-pulling .envelope-wrap {
  transform: translateY(38%);
}

.is-pulling .envelope__letter {
  transform: translateY(-100%);
}

.is-leaving .envelope-wrap {
  transform: translateY(75%) scale(.92);
  opacity: 0;
  transition:
    transform .9s var(--ease-in-out),
    opacity .45s ease .1s;
}

/* --------------------------------------------------------------------------
   The full-page letter
   -------------------------------------------------------------------------- */

.letter {
  position: fixed;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  z-index: 10;
  overflow: hidden;
  background:
    radial-gradient(130% 100% at 50% 0%, transparent 55%, rgb(210 160 120 / .07)),
    var(--grain),
    var(--paper);
}

.letter:focus {
  outline: none;
}

.letter.is-expanding {
  box-shadow: 0 30px 80px -20px rgb(90 30 20 / .45);
}

/* Once it has filled the screen it joins the normal page flow, so it scrolls like any page */
.letter.is-settled {
  position: relative;
  height: auto;
  min-height: 100vh;
  min-height: 100svh;
  overflow: visible;
}

.letter.is-closing {
  opacity: 0;
  transition: opacity .45s ease;
}

.letter__ghost {
  display: none;
  position: absolute;
  top: 0;
  left: 50%;
  width: calc(var(--w) * .9);
  height: calc(var(--h) * .88);
  translate: -50% 0;
  pointer-events: none;
}

.letter.is-expanding .letter__ghost {
  display: block;
}

.letter__content {
  position: relative;
  max-width: 37rem;
  margin: 0 auto;
  padding:
    clamp(3.5rem, 11vh, 7.5rem)
    max(1.5rem, env(safe-area-inset-right))
    clamp(4rem, 12vh, 7rem)
    max(1.5rem, env(safe-area-inset-left));
  font-size: clamp(1.1rem, 1rem + .35vw, 1.28rem);
  line-height: 1.7;
}

.letter__content > * {
  opacity: 0;
}

.letter.is-revealed .letter__content > * {
  animation: rise .9s var(--ease-out) both;
  animation-delay: calc(var(--i, 0) * 85ms + 60ms);
}

@keyframes rise {
  from { opacity: 0; transform: translateY(18px); }
  to   { opacity: 1; transform: none; }
}

.letter__content p {
  margin: 0 0 1.15em;
  text-wrap: pretty;
}

.letter__content .title {
  margin: 0 0 0.5em;
  font-size: clamp(3.4rem, 16vw, 4rem);
  font-weight: 400;
  font-style: italic;
  line-height: .92;
  color: var(--accent);
  letter-spacing: -.025em;
  text-wrap: balance;
}

.letter__content .salutation {
  margin-bottom: .5em;
  font-family: var(--font-hand);
  font-size: 2em;
  font-weight: 500;
  line-height: 1.2;
}

.letter__content .signoff {
  margin: 2.2em 0 0;
  font-style: italic;
}

.letter__content .signature {
  margin: 0;
  font-family: var(--font-hand);
  font-size: 2.3em;
  font-weight: 700;
  line-height: 1.2;
  color: var(--accent);
  transform-origin: left center;
  rotate: -3deg;
}

.nb {
  /* no-break */
  white-space: nowrap;
}

.aq {
  width: 1em;
  height: auto;
}

.present {
  font-size: 1.8em;
  text-align: center;
}

.present a {
  text-decoration: none;
  display: flex;
  flex-direction: column;
  margin: 1.5em 0;
  color: var(--seal);
}

.present__icon {
  font-size: 3em;
  line-height: 1;
  perspective: 1400px;
  transition: transform .4s var(--ease-out);
}

.present:hover .present__icon,
.present:active .present__icon {
  transform: translateY(-5px) rotate(-1.5deg);
}

.present__text {
  font-style: italic;
}

.letter__content .ps {
  margin: 3em 0 0;
  font-size: .88em;
  font-style: italic;
  color: var(--ink-soft);
}

.footer {
  display: grid;
  grid-template-columns: auto min-content;
}

.footer img {
  display: block;
  width: min(40vw, 240px);
  height: auto;
  /* Fade the photo itself out towards the edges, so the real paper shows through */
  -webkit-mask-image: radial-gradient(closest-side, #000 80%, rgb(0 0 0 / .6) 90%, transparent);
          mask-image: radial-gradient(closest-side, #000 80%, rgb(0 0 0 / .6) 90%, transparent);
}

/* --------------------------------------------------------------------------
   Confetti
   -------------------------------------------------------------------------- */

.confetti {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 20;
  pointer-events: none;
}

/* --------------------------------------------------------------------------
   Reduced motion: skip the theatrics, keep the letter
   -------------------------------------------------------------------------- */

@media (prefers-reduced-motion: reduce) {
  .envelope-float,
  .envelope-shadow,
  .hint span {
    animation: none;
  }

  .envelope:hover,
  .envelope:active {
    transform: none;
  }

  .letter.is-revealed .letter__content > * {
    animation: fade .4s ease both;
    animation-delay: 0s;
  }

  @keyframes fade {
    from { opacity: 0; }
    to   { opacity: 1; }
  }
}
