/* Layout: one screen. A close-up of the embossed red envelope fills it and
   opens with the scroll. The ivory card that comes out then settles into a
   letter lying over the couple's photographs, and the RSVP is written on it. */
:root {
  color-scheme: dark;

  --night: #140b08;      /* behind the photographs while they load */
  --ivory: #e9dcc6;      /* the card inside the envelope */
  --pearl: #f6ecd9;      /* writing over the envelope */
  --ink: #4a0a0e;        /* writing on the card */
  --ink-soft: #7d4a44;   /* labels on the card */
  --rule: #bfa684;       /* form lines */
  --red: #9b1c26;        /* the accent */

  --font-script: "Pinyon Script", "Snell Roundhand", "Apple Chancery", cursive;
  --font-serif: "Libre Caslon Text", "Big Caslon", "Hoefler Text", Georgia, serif;
  --font-text: "Hanken Grotesk", "Avenir Next", "Segoe UI", system-ui, sans-serif;

  /* On a phone the photograph is shown whole, as wide as the screen allows. */
  --shot-w: min(100vw, 66.667vh);
  --shot-w: min(100vw, 66.667svh);
  --shot-h: calc(var(--shot-w) * 1.5);

  /* Where the envelope starts (it fills a phone's screen) and where the
     letter comes to rest, over the photograph's lower part. */
  --open-l: 0px;
  --open-r: 0px;
  --rest-l: max(4vw, (100vw - 25rem) / 2);
  --rest-r: max(4vw, (100vw - 25rem) / 2);
  --rest-top: 30vh;
  --rest-top: 30svh;
  --rest-bottom: 1.5vh;
  --rest-bottom: 1.5svh;
  /* How far the photograph slides up as the letter settles, to keep the two
     of them in view above it. */
  --lift: 13vh;
  --lift: 13svh;

  /* Set by the script as the page scrolls. Without it the letter lies settled. */
  --settle: 1; /* 0 the card fills the screen and shows its front, 1 it has
                  turned over and come to rest as the letter */
  --hint: 0;   /* the "scroll to continue" line */
}

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

html {
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  background: var(--night);
}

body {
  margin: 0;
  background: var(--night);
  color: var(--ink);
  font-family: var(--font-text);
  font-size: 1rem;
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
}

h1,
p {
  margin: 0;
  padding: 0;
}

img,
canvas {
  display: block;
}

[hidden] {
  display: none !important;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

/* ---------- The scene ---------- */

/* With the script running, a tall track scrolls past the pinned screen; that
   distance opens the envelope. The track ends with the letter at rest. */
.is-scripted .scene {
  height: 460vh;
  height: 460svh;
}

/* A squarer screen: the envelope stands whole in the middle rather than
   being cropped to fill it. */
@media (min-aspect-ratio: 3 / 4) {
  :root {
    --open-l: max(0px, (100vw - 56.25vh) / 2);
    --open-l: max(0px, (100vw - 56.25svh) / 2);
    --open-r: var(--open-l);
  }
}

/* A wide screen: the envelope, then the letter, stands in the middle with a
   photograph filling the screen on either side of it. */
@media (min-aspect-ratio: 13 / 10) {
  :root {
    --card-w: 56.25vh;
    --card-w: 56.25svh;
    --side: calc((100vw - var(--card-w)) / 2);
    --inset: calc((var(--card-w) - min(25rem, var(--card-w))) / 2);

    --open-l: var(--side);
    --open-r: var(--side);
    --rest-l: calc(var(--side) + var(--inset));
    --rest-r: calc(var(--side) + var(--inset));
    --rest-top: 8vh;
    --rest-top: 8svh;
    --rest-bottom: 8vh;
    --rest-bottom: 8svh;
    /* The photographs are beside the card here, so nothing has to move aside. */
    --lift: 0px;
  }
}

.scene__pin {
  position: sticky;
  top: 0;
  height: 100vh;
  height: 100svh;
  overflow: hidden;
  /* Depth for the card turning over. */
  perspective: 110rem;
}

/* ---------- The photograph ---------- */

.backdrop {
  position: absolute;
  inset: 0;
  overflow: hidden;
  background: var(--night);
}

/* A dim, out-of-focus copy of the photograph fills the screen behind everything. */
.backdrop__glow {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
  filter: blur(40px) brightness(0.4);
  transform: scale(1.2);
}

/* On a phone the photograph is shown whole at the top of the screen. When the
   letter rises over its lower part it slides up a little, so the two of them
   stay in view. */
.backdrop__photo {
  position: absolute;
  top: 0;
  left: calc((100vw - var(--shot-w)) / 2);
  width: var(--shot-w);
  height: var(--shot-h);
  object-fit: cover;
  transform: translateY(calc(var(--settle) * var(--lift) * -1));
}

/* The second photograph is for wide screens only. */
.backdrop__photo--far {
  display: none;
}

/* On a wide screen a photograph fills each side, from the screen's edge to
   the envelope, framed on the two of them. */
@media (min-aspect-ratio: 13 / 10) {
  .backdrop__photo {
    left: 0;
    width: var(--side);
    height: 100%;
    object-position: 30% 42%;
  }

  .backdrop__photo--far {
    display: block;
    left: auto;
    right: 0;
    object-position: 62% 55%;
  }
}

/* ---------- The card: the film on its front, the letter on its back ---------- */

/* As it settles the card shrinks to its resting place and turns over once. */
.card {
  position: absolute;
  top: calc(var(--settle) * var(--rest-top));
  bottom: calc(var(--settle) * var(--rest-bottom));
  left: calc(var(--open-l) + var(--settle) * (var(--rest-l) - var(--open-l)));
  right: calc(var(--open-r) + var(--settle) * (var(--rest-r) - var(--open-r)));
  transform: rotateY(calc(var(--settle) * 180deg));
  transform-style: preserve-3d;
}

.card__face {
  position: absolute;
  inset: 0;
  overflow: hidden;
  backface-visibility: hidden;
  -webkit-backface-visibility: hidden;
  background: var(--ivory);
  box-shadow: 0 calc(var(--settle) * 1.5rem) calc(var(--settle) * 3.5rem) rgb(10 4 2 / 0.6);
}

.card__film img,
.card__film canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  object-fit: cover;
}

/* Without the script there is no film; the front stays plain. */
.card__film img {
  display: none;
}

.is-scripted .card__film img {
  display: block;
}

/* The letter is the card's back: the same embossed paper, without the words. */
.letter {
  transform: rotateY(180deg);
  background: var(--ivory) url("media/paper.jpg") center / cover no-repeat;
  display: flex;
  flex-direction: column;
  justify-content: center;
  padding: 3.25rem clamp(1.25rem, 7%, 2.25rem);
  overflow-y: auto;
  text-align: center;
}

.letter[inert] {
  pointer-events: none;
}

.letter__eyebrow {
  font-size: 0.625rem;
  font-weight: 600;
  letter-spacing: 0.26em;
  text-transform: uppercase;
  color: var(--ink-soft);
}

.letter__names {
  font-family: var(--font-script);
  font-weight: 400;
  font-size: clamp(2.3rem, 10.5vw, 3rem);
  line-height: 1;
  color: var(--red);
  margin-top: 0.35rem;
}

.letter__names span {
  font-size: 0.8em;
}

.letter__when {
  font-family: var(--font-serif);
  font-style: italic;
  font-size: 0.9375rem;
  line-height: 1.4;
  margin-top: 0.35rem;
}

.letter__when span {
  color: var(--red);
  font-style: normal;
}

.letter__form {
  display: grid;
  gap: 0.8rem;
  margin-top: 1.1rem;
  padding-top: 1rem;
  border-top: 1px solid var(--rule);
  text-align: left;
}

.field {
  display: grid;
  gap: 0.05rem;
  min-width: 0;
}

.field label,
.choice legend {
  font-size: 0.625rem;
  font-weight: 600;
  letter-spacing: 0.2em;
  text-transform: uppercase;
  color: var(--ink-soft);
}

.field input,
.field textarea {
  width: 100%;
  border: 0;
  border-bottom: 1px solid var(--rule);
  border-radius: 0;
  background: transparent;
  color: var(--ink);
  color-scheme: light;
  caret-color: var(--red);
  font-family: var(--font-serif);
  font-size: 1rem;
  line-height: 1.35;
  padding: 0.1rem 0 0.25rem;
}

.field textarea {
  resize: none;
  min-height: 3rem;
}

.field input:focus-visible,
.field textarea:focus-visible {
  outline: 0;
  border-bottom-color: var(--red);
  box-shadow: 0 1px 0 0 var(--red);
}

/* Browser autofill paints its own background; keep the paper ivory. */
.field input:-webkit-autofill,
.field textarea:-webkit-autofill {
  -webkit-text-fill-color: var(--ink);
  box-shadow: 0 0 0 100rem var(--ivory) inset;
}

.field--trap {
  position: absolute;
  left: -200vw;
}

.choice {
  border: 0;
  margin: 0;
  padding: 0;
  min-width: 0;
  display: flex;
  flex-wrap: wrap;
  gap: 0 1.75rem;
}

.choice legend {
  padding: 0;
}

/* A short phone leaves less room, so the letter rises further up the photograph. */
@media (max-aspect-ratio: 13 / 10) and (max-height: 46rem) {
  :root {
    --rest-top: 17vh;
    --rest-top: 17svh;
    --lift: 22vh;
    --lift: 22svh;
  }
}

/* Yes and No are written out in script; the one chosen turns red and is underlined. */
.choice__option {
  position: relative;
  cursor: pointer;
}

.choice__option input {
  position: absolute;
  opacity: 0;
  width: 100%;
  height: 100%;
  margin: 0;
  cursor: pointer;
}

.choice__option span {
  display: inline-block;
  font-family: var(--font-script);
  font-size: 2rem;
  line-height: 1.1;
  color: var(--ink-soft);
  border-bottom: 1px solid transparent;
  padding-inline: 0.1em 0.2em;
  transition: color 0.2s ease, border-color 0.2s ease;
}

.choice__option:hover span {
  color: var(--ink);
}

.choice__option input:checked + span {
  color: var(--red);
  border-bottom-color: var(--red);
}

.choice__option input:focus-visible + span {
  outline: 2px solid var(--red);
  outline-offset: 4px;
}

.letter__send {
  display: grid;
  justify-items: center;
  gap: 0.4rem;
  margin-top: 0.25rem;
}

.letter__send button {
  appearance: none;
  border: 0;
  border-radius: 0;
  background: var(--red);
  color: var(--ivory);
  font-family: var(--font-text);
  font-size: 0.75rem;
  font-weight: 600;
  letter-spacing: 0.22em;
  text-transform: uppercase;
  padding: 0.8rem 2.5rem;
  cursor: pointer;
  transition: background 0.2s ease;
}

.letter__send button:hover {
  background: var(--ink);
}

.letter__send button:focus-visible {
  outline: 2px solid var(--ink);
  outline-offset: 3px;
}

.letter__send button:disabled {
  background: var(--ink-soft);
  cursor: default;
}

.letter__status {
  min-width: 0;
  font-size: 0.875rem;
  color: var(--red);
  text-align: center;
}

.letter__thanks {
  font-family: var(--font-serif);
  font-style: italic;
  font-size: 1.25rem;
  line-height: 1.45;
  text-wrap: balance;
  /* Keeps the break before the closing line, "Formal invitation to follow." */
  white-space: pre-line;
  margin-top: 1.75rem;
}

.letter__thanks:focus {
  outline: 0;
}

/* ---------- "Scroll to continue", at the foot of the screen ---------- */

.scene__hint {
  display: none;
}

/* It stays until the letter is in place. The dark pill keeps it readable
   over the red envelope and the ivory card alike. */
.is-scripted .scene__hint {
  display: block;
  position: absolute;
  left: 50%;
  bottom: max(1.5rem, env(safe-area-inset-bottom, 0px));
  transform: translateX(-50%);
  padding: 0.55rem 1.1rem 0.5rem;
  border-radius: 999px;
  background: rgb(20 8 6 / 0.62);
  white-space: nowrap;
  font-size: 0.6875rem;
  font-weight: 600;
  letter-spacing: 0.26em;
  text-transform: uppercase;
  color: var(--pearl);
  opacity: var(--hint);
  pointer-events: none;
}
