@font-face {
  font-family: "Postal UI";
  src: url("./assets/ui-arabic.woff") format("woff");
  font-weight: 400;
  font-display: swap;
}

@font-face {
  font-family: "Postal Ink";
  src: url("./assets/print-arabic.woff") format("woff");
  font-weight: 700;
  font-display: swap;
}

:root {
  color-scheme: dark;
  --cream: #ffedda;
  --wine: #650d1e;
  --stock-light: #b5314a;
  --ease: cubic-bezier(.21, .7, .25, 1);
  --printed-type: "Postal Ink", Georgia, "Times New Roman", serif;
  --postal-imprint: url("./assets/postal-imprint.webp");
  font-family: "Postal UI", Tahoma, Arial, sans-serif;
  background: var(--wine);
  color: var(--cream);
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; width: 100%; overflow: hidden; }
body { -webkit-tap-highlight-color: transparent; }
button, input, a { font: inherit; }
button, a { touch-action: manipulation; }
a { color: inherit; }
button { color: inherit; }
button:focus-visible, a:focus-visible {
  outline: 2px solid var(--cream);
  outline-offset: 5px;
}

.scene {
  --side: 75vw;
  --mouth: 78%;
  --collapse: 0;
  --slot: 0;
  --address-top: 48%;
  --brand-top: 25%;
  position: relative;
  isolation: isolate;
  overflow: hidden;
  height: 100vh;
  height: 100dvh;
  min-height: 200px;
  user-select: none;
}

.stock, .stock::before, .stock::after { position: absolute; inset: 0; pointer-events: none; }
.stock {
  z-index: 0;
  background:
    radial-gradient(ellipse at 38% 41%, #87182e 0%, #6f0e22 44%, transparent 79%),
    radial-gradient(ellipse at 76% 11%, #720c21, #3f0714 100%);
}

.stock::before {
  content: "";
  background:
    linear-gradient(45deg, transparent 51%, #ef6d6739 57%, #ffad854f 62%, #c34b4f2c 70%, transparent 77%, transparent 84%, #f7836530 90%, transparent 98%);
  -webkit-mask-image: radial-gradient(ellipse at 4% 0, #000 4%, #000c 26%, transparent 73%);
  mask-image: radial-gradient(ellipse at 4% 0, #000 4%, #000c 26%, transparent 73%);
}

.stock::after {
  content: "";
  background: url("./assets/paper.webp") repeat;
  opacity: .28;
  mix-blend-mode: soft-light;
}

.imprint { position: absolute; pointer-events: none; }
.imprint::before, .imprint::after {
  content: "";
  position: absolute;
  inset: 0;
  -webkit-mask: var(--impression) center / 100% 100% no-repeat;
  mask: var(--impression) center / 100% 100% no-repeat;
}
.imprint::before {
  background: var(--stock-light);
  opacity: .19;
  transform: translate(-.65px, .85px);
}
.imprint::after { background: #3e0210; opacity: .48; }
.imprint-waves {
  --impression: var(--postal-imprint);
  top: 8%;
  right: max(-17vw, calc((100vw - 670px) / 2));
  width: clamp(230px, 56vw, 435px);
  aspect-ratio: 520 / 222;
  transform: rotate(-10deg);
  opacity: .46;
}
.imprint-fort {
  --impression: url("./assets/fort-engraving.webp");
  right: 50%;
  bottom: -3%;
  width: clamp(400px, 109vw, 775px);
  aspect-ratio: 760 / 317;
  transform: translateX(50%);
  -webkit-mask-image: linear-gradient(#000 5%, #000b 65%, transparent);
  mask-image: linear-gradient(#000 5%, #000b 65%, transparent);
  opacity: .47;
}

.brand {
  position: absolute;
  z-index: 2;
  top: var(--brand-top);
  left: 50%;
  width: clamp(176px, 49vw, 242px);
  max-width: 65%;
  max-height: 44%;
  object-fit: contain;
  filter: drop-shadow(0 7px 13px #28030e45);
  transform: translate(-50%, -50%) scale(.92);
  opacity: 1;
  transition: top 760ms var(--ease), transform 760ms var(--ease), opacity 300ms ease;
  pointer-events: none;
}
.scene[data-state="opening"] .brand {
  top: 45%;
  transform: translate(-50%, -50%) scale(1.16);
}
.scene[data-keyboard] .brand {
  max-width: min(38%, 138px);
  max-height: var(--logo-room, 110px);
}
.scene:is([data-state="entry"], [data-state="ready"], [data-state="mailing"], [data-state="done"]) .brand {
  transform: translate(-50%, -64%) scale(.53);
  opacity: 0;
}

.address {
  position: absolute;
  z-index: 7;
  top: var(--address-top);
  left: 50%;
  width: min(79%, 390px);
  opacity: 0;
  visibility: hidden;
  transform: translate(-50%, calc(-50% + 12px));
  pointer-events: none;
  transition: transform 460ms var(--ease), opacity 245ms ease;
}
.scene[data-state="address"] .address {
  visibility: visible;
  pointer-events: auto;
  opacity: 1;
  transform: translate(-50%, -50%);
}
.address-line {
  display: flex;
  align-items: baseline;
  gap: .86em;
  font-size: clamp(19px, 5.5vw, 25px);
}
.address-line label { color: #ffdfbe; cursor: text; flex: none; }
.address-field {
  position: relative;
  flex: 1;
  min-width: 0;
  border-bottom: 1px solid #fce3cdb2;
  box-shadow: 0 2px 0 #4208176b;
}
.address-field:focus-within {
  border-color: #ffe6d0;
  box-shadow: 0 1px 0 #f5c9b230, 0 2px 0 #4208176b;
}
.address-field input {
  display: block;
  width: 100%;
  height: 2.15em;
  border: 0;
  border-radius: 0;
  color: #fff1df;
  background: transparent;
  text-align: right;
  padding: .03em .19em 0 2.55em;
  outline: none;
  caret-color: #ffdfbe;
  user-select: text;
}
.address-field input::placeholder { color: #fbd8c5a8; }
.address-field button {
  position: absolute;
  inset-inline-end: 0;
  bottom: 0;
  display: grid;
  place-items: center;
  min-width: 44px;
  min-height: 44px;
  padding: 0 .45rem;
  background: transparent;
  border: 0;
  font-size: .65em;
  cursor: pointer;
}
.error {
  position: absolute;
  inset-inline: 0;
  top: 100%;
  margin: .55rem 0 0;
  color: #ffdfbe;
  font-size: .82rem;
  text-align: center;
}

.paper-stage {
  position: absolute;
  z-index: 3;
  inset: 0;
  pointer-events: none;
}
.scene[data-posting] .paper-stage {
  bottom: auto;
  height: calc(var(--mouth) + 4px);
  overflow: hidden;
}
.rig {
  position: absolute;
  left: var(--card-left, 0px);
  top: var(--card-top, 0px);
  width: var(--side);
  height: var(--side);
  direction: ltr;
  transform-origin: center;
  pointer-events: none;
}
.paper {
  isolation: isolate;
  border-radius: 1px;
  background: #fcf9ef;
  box-shadow: 0 1px 0 #d4b9a8, 0 2px 3px #33000965, 0 14px 26px #31030eb2, 0 29px 54px #38051164;
}
.paper canvas {
  display: block;
  width: 100%;
  height: 100%;
  border-radius: inherit;
}
.paper-light {
  position: absolute;
  inset: 0;
  border-radius: inherit;
  background: linear-gradient(139deg, #fffaf1 0%, transparent 48%, #5c0b2315 100%);
  opacity: var(--paper-light, .04);
  pointer-events: none;
}
.hand {
  z-index: 4;
}
.hand canvas {
  position: absolute;
  display: block;
  transform: rotate(var(--flex, 0deg));
}
.scene:is([data-state="opening"], [data-state="address"], [data-state="preparing"], [data-state="done"]) :is(.paper-stage, .hand) {
  visibility: hidden;
}
.scene:not([data-state="mailing"]) :is(.parcel, .hand-left) { visibility: hidden; }

.parcel {
  --fold: 168deg;
  --open-flap: 1;
  --closed-flap: 0;
  --envelope-paper: #f8f5ee;
  --envelope-fold: #e8e3d9;
  --stripe-red-end: calc(var(--side) * .039);
  --stripe-green-start: calc(var(--side) * .061);
  --stripe-green-end: calc(var(--side) * .100);
  --stripe-repeat: calc(var(--side) * .122);
  --airmail-ink:
    #d01832 0 var(--stripe-red-end),
    transparent var(--stripe-red-end) var(--stripe-green-start),
    #138743 var(--stripe-green-start) var(--stripe-green-end),
    transparent var(--stripe-green-end) var(--stripe-repeat);
  --airmail: repeating-linear-gradient(133deg, var(--airmail-ink));
  --trim-width: max(2px, calc(var(--side) * .022));
}
.envelope {
  position: absolute;
  inset: -3.5% -7%;
  border-radius: 2px;
}
.parcel-pocket {
  background: linear-gradient(180deg, #6a514e, #b0a49a 32%, #cec5ba);
  box-shadow: inset 0 2px 0 #e6ded3, 0 2px 3px #41091a8c, 0 18px 37px #31040f94;
}
.parcel-face {
  position: absolute;
  inset: 0;
  overflow: hidden;
  border-radius: inherit;
  background:
    linear-gradient(136deg, #fffdf8e8, #eeebe3e4 91%),
    url("./assets/paper.webp") var(--envelope-paper);
  box-shadow: inset 0 1px 0 #fffcf7, inset 0 -1px 0 #8b777252, 0 -2px 8px #69293630;
}
.parcel-face::before {
  content: "";
  pointer-events: none;
  position: absolute;
  inset: 0;
  background: linear-gradient(118deg, #3b282816, #ffffff00 45%, #5142400e);
  clip-path: polygon(0 37%, 51% 82%, 100% 37%, 100% 100%, 0 100%);
}
.airmail-border {
  position: absolute;
  z-index: 1;
  inset: 2.5%;
  border-radius: 1px;
  padding: var(--trim-width);
  background: var(--airmail);
  -webkit-mask: linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0);
  -webkit-mask-composite: xor;
  mask: linear-gradient(#000 0 0) content-box, linear-gradient(#000 0 0);
  mask-composite: exclude;
  clip-path: inset(calc(var(--closed-flap) * var(--trim-width) + var(--closed-flap) * 1px) 0 0);
  pointer-events: none;
}
.parcel-stamp {
  position: absolute;
  z-index: 1;
  left: 12%;
  bottom: 13.5%;
  width: 26%;
  max-height: 25%;
  object-fit: contain;
  transform: rotate(-3deg);
}
.parcel-postmark {
  position: absolute;
  z-index: 1;
  right: 11%;
  bottom: 15.5%;
  width: 35%;
  aspect-ratio: 520 / 222;
  background: #790d25;
  -webkit-mask: var(--postal-imprint) center / contain no-repeat;
  mask: var(--postal-imprint) center / contain no-repeat;
  opacity: .75;
  transform: rotate(-5deg);
}
.parcel-address {
  position: absolute;
  z-index: 1;
  top: 47.6%;
  inset-inline: 13%;
  color: #7e1028;
  text-align: center;
}
.parcel-address span {
  display: block;
  font: 700 clamp(.82rem, calc(var(--side) * .049), 1.27rem) / 1.12 var(--printed-type);
  opacity: .77;
}
.parcel-address bdi {
  display: block;
  margin-top: .1em;
  font: 700 var(--recipient-size, 1.45rem) / 1.12 var(--printed-type);
  overflow-wrap: anywhere;
}
.flap {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  height: 42%;
  transform: perspective(960px) rotateX(var(--fold));
  transform-origin: center top;
  filter: drop-shadow(0 2px 1.5px #5517275c);
}
.flap::before, .flap::after {
  content: "";
  position: absolute;
  inset: 0;
}
.flap::before {
  clip-path: polygon(0 0, 100% 0, 64% 83%, 55% 97%, 50% 100%, 45% 97%, 36% 83%);
  background:
    var(--airmail) left top / 50% 100% no-repeat,
    repeating-linear-gradient(47deg, var(--airmail-ink)) right top / 50% 100% no-repeat,
    var(--envelope-paper);
}
.flap::after {
  clip-path: polygon(4.7% 4%, 95.3% 4%, 62.1% 78%, 54.1% 91%, 50% 94%, 45.9% 91%, 37.9% 78%);
  background: linear-gradient(143deg, var(--envelope-paper), var(--envelope-fold));
}
.flap-back { opacity: var(--open-flap); }
.flap-back::before { background-color: #d8cfc5; }
.flap-back::after { background: linear-gradient(#c3b6ad, var(--envelope-fold) 93%); }
.flap-front { opacity: var(--closed-flap); }

.card-touch {
  z-index: 6;
  padding: 0;
  border: 0;
  border-radius: 2px;
  background: transparent;
  cursor: grab;
  touch-action: none;
  pointer-events: auto;
}
.card-touch:active { cursor: grabbing; }

.slot, .lip, .mail-controls {
  position: absolute;
  left: 50%;
  width: var(--slot-width, calc(var(--side) + 12px));
  max-width: calc(100% - 24px);
  top: var(--mouth);
  transform: translate(-50%, -50%);
}
.slot, .lip {
  pointer-events: none;
  opacity: var(--slot);
}
.slot {
  z-index: 1;
  height: 19px;
  border-radius: 9px;
  background: linear-gradient(#4e0815 1%, #22030b 29%, #180308 66%, #5c1426 87%, #87253a);
  box-shadow: 0 1px 0 #eb897365, 0 -1px 1px #35040e90, 0 6px 11px #38000d5b, inset 0 2px 5px #220009;
}
.lip {
  z-index: 5;
  height: 6px;
  margin-top: 7px;
  border-radius: 50% 50% 8px 8px / 80% 80% 8px 8px;
  background: linear-gradient(180deg, #da8c7955 0%, #b4525e 13%, #731c30 32%, #72192b 76%, #4e0719);
  box-shadow: 0 1.5px 2px #260308ad;
}
.scene[data-state="done"] :is(.slot, .lip) {
  animation: slot-rest 460ms ease-out both;
}
@keyframes slot-rest {
  from { opacity: var(--slot); }
  to { opacity: calc(var(--slot) * .28); }
}

.mail-controls {
  z-index: 6;
  pointer-events: none;
  opacity: 0;
  visibility: hidden;
  transition: opacity 240ms ease;
}
.scene:is([data-state="ready"], [data-state="mailing"]) .mail-controls {
  opacity: 1;
  visibility: visible;
}
.save {
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
  width: 100%;
  height: 53px;
  border: 0;
  padding: 0;
  border-radius: 12px;
  background: linear-gradient(#fff2e3, #f5dec1);
  box-shadow: inset 0 1px 0 #fff8eb, 0 2px 0 #d5a980, 0 6px 10px #3c001766;
  color: #740e22;
  text-decoration: none;
  font: 700 clamp(21px, 5.4vw, 26px) / 1.03 var(--printed-type);
  opacity: calc(1 - var(--collapse));
  transform: scaleY(calc(1 - var(--collapse) * .76));
}
.save span { opacity: calc(1 - var(--collapse)); translate: 0 1px; }
.save:focus-visible { outline-offset: 5px; }
.scene[data-state="ready"] .save { cursor: pointer; pointer-events: auto; }
.scene[data-state="ready"] .save:active {
  background: #f3d9bd;
  translate: 0 2px;
}
.scene[data-state="mailing"] .save { pointer-events: none; }
.save[aria-disabled="true"] { cursor: progress; }

.completion {
  position: absolute;
  z-index: 7;
  left: 50%;
  top: calc(var(--mouth) + 27px);
  width: min(91%, 475px);
  transform: translateX(-50%);
  text-align: center;
  pointer-events: none;
}
.scene[data-state="done"] .completion {
  top: 46%;
  transform: translate(-50%, -50%);
}
.completion-logo {
  display: none;
  width: min(65%, 232px, 33vh);
  width: min(65%, 232px, 33dvh);
  height: auto;
  margin: 0 auto;
  filter: drop-shadow(0 5px 12px #37041348);
}
.scene[data-state="done"] .completion-logo {
  display: block;
  animation: completion-logo-in 460ms var(--ease) both;
}
.scene[data-state="done"] .imprint-waves {
  opacity: .26;
  transition: opacity 440ms ease-out;
}
@keyframes completion-logo-in {
  from { opacity: 0; transform: translateY(7px) scale(.96); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}
.mail-status {
  margin: .75rem 0 .30rem;
  color: #fff4e9;
  font: 700 clamp(21px, 5.7vw, 30px) / 1.16 var(--printed-type);
  animation: completion-label 400ms 70ms var(--ease) both;
}
.mail-status:empty { display: none; }
@keyframes completion-label {
  from { opacity: 0; transform: translateY(6px); }
  to { opacity: 1; transform: translateY(0); }
}

.secondary {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 1.1rem;
  color: #f5cfbc;
  opacity: 0;
  visibility: hidden;
  pointer-events: none;
  transition: opacity 220ms ease;
}
.secondary a, .secondary button {
  display: grid;
  place-items: center;
  min-height: 44px;
  padding: 0 .45em;
  font: 700 .95rem / 1.15 var(--printed-type);
  background: transparent;
  border: 0;
  text-decoration: none;
  cursor: pointer;
}
.scene:is([data-state="ready"], [data-state="done"]) .secondary {
  opacity: 1;
  visibility: visible;
  pointer-events: auto;
}
.secondary :is(a, button):is(:hover, :focus-visible) { color: #fff2e6; text-decoration: underline; }

.no-script {
  position: absolute;
  inset: auto 10% 10%;
  color: var(--cream);
  text-align: center;
}
.sr-only {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

@media (min-width: 740px) {
  .stock::before {
    -webkit-mask-image: radial-gradient(ellipse at 24% 0%, #000 4%, #000c 21%, transparent 69%);
    mask-image: radial-gradient(ellipse at 24% 0%, #000 4%, #000c 21%, transparent 69%);
  }
}

@media (min-width: 560px) and (min-aspect-ratio: 6/5) {
  body {
    min-height: 100vh;
    min-height: 100dvh;
    display: grid;
    place-items: center;
    background: radial-gradient(ellipse at 42% 18%, #5a1024, #2a0510 86%);
  }
  .scene {
    width: min(1120px, calc(100vw - 48px), calc(128dvh - 26px));
    height: min(892px, calc(100dvh - 36px));
    border-radius: 18px;
    outline: 1px solid #e99b913b;
    box-shadow: 0 16px 94px #1e030da6, 0 2px 9px #2a0210;
  }
  .stock::before {
    -webkit-mask-image: radial-gradient(ellipse at 11% 1%, #000, #000b 25%, transparent 71%);
    mask-image: radial-gradient(ellipse at 11% 1%, #000, #000b 25%, transparent 71%);
  }
  .imprint-waves {
    top: 10%;
    right: -3%;
    width: 44%;
    opacity: .64;
  }
  .imprint-fort {
    right: auto;
    left: -10%;
    bottom: -1%;
    width: 75%;
    transform: none;
    opacity: .52;
  }
  .brand { width: clamp(205px, 25vw, 274px); }
}

@media (max-height: 510px) and (orientation: landscape) {
  .completion { top: calc(var(--mouth) + 14px); }
  .scene[data-state="done"] .completion { top: 44%; }
  .completion-logo { width: min(62%, 170px, 31dvh); }
  .mail-status { margin: .35rem 0 0; font-size: 19px; }
  .save { height: 43px; }
  .brand { width: 138px; }
}

@media (min-width: 560px) and (max-height: 410px) and (min-aspect-ratio: 6/5) {
  .scene { width: min(calc(100vw - 48px), calc(120dvh - 44px)); }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: 1ms !important; transition-duration: 1ms !important; }
}
