:root {
  color-scheme: light;
  --wine: #691f2b;
  --ink: #422621;
  --cream: #f9e8bc;
  --paper: radial-gradient(ellipse at 44% 35%, #fff4d6, transparent 72%),
    repeating-linear-gradient(107deg, #996d3008 0 1px, transparent 1px 5px),
    linear-gradient(141deg, #f5dcab, var(--cream) 42%, #f3dca8);
  --serif: 'Palatino Linotype', 'Book Antiqua', Palatino, Georgia, serif;
}

*, *::before, *::after { box-sizing: border-box; }
[hidden] { display: none !important; }

body {
  min-width: 0;
  margin: 0;
  background: #7fa8e6;
  color: var(--ink);
  font-family: var(--serif);
}

body::before {
  content: '';
  position: fixed;
  inset: 0;
  pointer-events: none;
  background: url('assets/paisagem.png') center / cover no-repeat;
}

button { font: inherit; color: inherit; }

button:focus-visible, .letter:focus-visible {
  outline: 2px solid var(--wine);
  outline-offset: 5px;
}

.experience { position: relative; min-height: 100vh; min-height: 100svh; }

.music-toggle {
  position: fixed;
  z-index: 7;
  top: max(12px, env(safe-area-inset-top));
  right: max(12px, env(safe-area-inset-right));
  min-height: 44px;
  padding: 6px 14px;
  border: 1px solid #691f2b66;
  border-radius: 999px;
  background: #fff4db;
  color: var(--wine);
  font-size: 14px;
  cursor: pointer;
}

.intro {
  min-height: 100vh;
  min-height: 100svh;
  padding: 22px 0;
  display: grid;
  place-items: center;
  overflow: clip;
}

.open-letter {
  display: block;
  width: min(calc(100% - 36px), 460px);
  padding: 0;
  border: 0;
  border-radius: 6px;
  background: transparent;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.envelope {
  /* Recorta as margens transparentes e alinha a dobra das duas fotos. */
  --closed-position: 49% 51% / 109.5% 111.7% no-repeat;
  --unsealed: url('assets/envelope-sem-selo.png') var(--closed-position);
  --open-body: url('assets/envelope-aberto.png') 50% 97% / 126.9% 198.4% no-repeat;
  --opening-duration: 1500ms;
  position: relative;
  isolation: isolate;
  display: block;
  width: 100%;
  aspect-ratio: 1423 / 903;
  perspective: 1000px;
}

.envelope-back, .envelope-pocket, .flap { position: absolute; inset: 0; }

.envelope-back {
  border-radius: 3px;
  background: var(--open-body);
  box-shadow: 0 15px 30px -10px #47212d99;
}

.envelope-back::before {
  content: '';
  position: absolute;
  inset: auto 0 100%;
  height: 93%;
  background: url('assets/envelope-aberto.png') 50% 2.6% / 126.9% 213.6% no-repeat;
  transform-origin: bottom center;
  transform: scaleY(0);
}

.envelope-closed {
  position: absolute;
  z-index: 5;
  inset: 0;
  background: url('assets/envelope.png') var(--closed-position);
  pointer-events: none;
}

.envelope-pocket {
  z-index: 2;
  background: var(--open-body);
  clip-path: polygon(0 0, 11% 5%, 50% 62%, 89% 5%, 100% 0, 100% 100%, 0 100%);
}

.flap {
  z-index: 3;
  transform-origin: top center;
  background: var(--unsealed);
  clip-path: polygon(0 0, 100% 0, 98% 12.7%, 48.6% 72.4%, 2.8% 12.7%);
}

.envelope-seal {
  position: absolute;
  z-index: 6;
  top: 67.5%;
  left: 47.9%;
  width: 16.5%;
  height: auto;
  opacity: 0;
  transform: translate(-50%, -50%);
  filter: drop-shadow(0 4px 4px #41201b65);
  pointer-events: none;
}

.folded-sheet {
  position: absolute;
  z-index: 1;
  top: 14%;
  left: 9%;
  width: 82%;
  height: 46%;
  visibility: hidden;
  transform-origin: top left;
  perspective: 800px;
}

.sheet-upper, .sheet-lower {
  position: absolute;
  inset: 0;
  background: var(--paper);
  border: 1px solid #b08a5160;
  box-shadow: 0 2px 6px #794e4033;
}

.sheet-lower {
  top: 100%;
  bottom: -100%;
  transform: rotateX(-180deg);
  transform-origin: top;
  background: linear-gradient(#aa773658, transparent 16%), var(--paper);
}

.experience[data-state='closed'] .letter-view { display: none; }

.experience[data-state='opening'] .letter-view {
  position: absolute;
  inset: 0;
  overflow: hidden;
  visibility: hidden;
}

.experience[data-state='opening'] .envelope-closed {
  background: var(--unsealed);
  animation: uncover-envelope 150ms step-end both;
}

.experience[data-state='opening'] .envelope-seal { animation: drop-seal 850ms ease-in both; }
.experience[data-state='opening'] .flap { animation: open-flap var(--opening-duration) ease-in-out both; }
.experience[data-state='opening'] .envelope-back::before { animation: reveal-open-flap var(--opening-duration) ease-in-out both; }
.experience[data-state='opening'] .envelope-back,
.experience[data-state='opening'] .envelope-pocket { animation: leave-envelope var(--opening-duration) ease-in both; }
.experience[data-state='opening'] .folded-sheet { animation: lift-sheet var(--opening-duration) ease-in-out both; }
.experience[data-state='opening'] .sheet-lower { animation: unfold var(--opening-duration) ease-in-out both; }

@keyframes uncover-envelope { to { visibility: hidden; } }

@keyframes drop-seal {
  0% { opacity: 1; transform: translate(-50%, -50%); }
  18% { opacity: 1; transform: translate(-28%, -41%) rotate(9deg); }
  72% { opacity: 1; }
  100% { opacity: 0; transform: translate(98%, calc(-50% + 52vh)) rotate(64deg); }
}

@keyframes open-flap {
  0%, 10% { transform: rotateX(0); opacity: 1; }
  21% { transform: rotateX(88deg); opacity: 1; }
  22%, 100% { transform: rotateX(90deg); opacity: 0; }
}

@keyframes reveal-open-flap {
  0%, 21% { transform: scaleY(0); }
  32%, 100% { transform: scaleY(1); }
}

@keyframes lift-sheet {
  0%, 24% { visibility: hidden; transform: translateY(0); z-index: 1; }
  25% { visibility: visible; }
  51%, 53% { transform: translateY(-105%); z-index: 1; }
  54% { z-index: 4; }
  69% { transform: translate(var(--sheet-x), var(--sheet-y)) scale(var(--sheet-scale-x), 1); }
  100% {
    visibility: visible;
    z-index: 4;
    transform: translate(var(--sheet-x), var(--sheet-y)) scale(var(--sheet-scale-x), var(--sheet-scale-y));
  }
}

@keyframes unfold {
  0%, 53% { transform: rotateX(-180deg); }
  86%, 100% { transform: rotateX(0); }
}

@keyframes leave-envelope { 67% { opacity: 1; } 94%, 100% { opacity: 0; } }

.letter-view {
  padding: clamp(80px, 13svh, 111px) clamp(8px, 2.5vw, 30px) clamp(38px, 11svh, 110px);
}

.letter {
  position: relative;
  width: min(100%, 680px);
  margin-inline: auto;
  padding: clamp(35px, 8vw, 61px) clamp(18px, 5vw, 53px) clamp(33px, 8vw, 62px);
  border: 1px solid #a3753e55;
  background: var(--paper);
  box-shadow: 0 18px 33px -14px #7737398c, 0 3px 8px #77373928;
  text-align: center;
  overflow-wrap: anywhere;
}

.letter::before {
  content: '';
  position: absolute;
  inset: 8px;
  border: 3px double #9b683f9e;
  pointer-events: none;
}

.letter-dedication {
  color: var(--wine);
  font-size: clamp(36px, 10vw, 64px);
  font-style: italic;
  line-height: 1.2;
  margin: 0 auto clamp(18px, 5vw, 29px);
}

.letter h1 {
  display: grid;
  grid-template-columns: repeat(3, auto);
  align-items: center;
  justify-content: center;
  column-gap: clamp(5px, 1.5vw, 10px);
  margin: 0 0 clamp(24px, 6vw, 37px);
  color: var(--wine);
  font-family: 'Baskerville', 'Baskerville Old Face', Georgia, serif;
  font-size: clamp(29px, 7.4vw, 39px);
  font-style: italic;
  font-weight: 400;
  line-height: 1;
}

.letter h1::after {
  content: '';
  grid-column: 1 / -1;
  justify-self: center;
  width: 49px;
  height: 1px;
  margin-top: 3px;
  background: #9a4b4c;
}

.letter-flower {
  font-family: var(--serif);
  font-size: clamp(23px, 6.5vw, 31px);
  font-style: normal;
  line-height: 1;
}

.poem, .signature { font-size: clamp(18px, 4.8vw, 22px); line-height: 1.58; }

.poem { text-align: start; }
.stanza + .stanza { margin-block-start: 1.85em; }

.verse {
  margin: 0;
  padding-inline-start: 0.96em;
  text-indent: -0.96em;
}

.verse + .verse { margin-block-start: 0.65em; }

.verse::first-letter {
  color: var(--wine);
  font-weight: 700;
}

.signature { margin: 1.5em 0 0; font-style: italic; }

.letter-end {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 8px;
  margin-top: clamp(25px, 7vw, 40px);
  color: var(--wine);
  font-size: 24px;
  line-height: 1;
}

.letter-end::before, .letter-end::after {
  content: '';
  width: 28px;
  height: 1px;
  background: #9a4b4c;
}

.reading-hearts {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 1;
  overflow: hidden;
  pointer-events: none;
}

.experience[data-state='reading'] .reading-hearts {
  display: block;
}

.reading-heart {
  position: absolute;
  bottom: 0;
  left: var(--heart-x);
  width: var(--heart-size);
  aspect-ratio: 1;
  background: url('assets/coracao.png') center / contain no-repeat;
  opacity: 0;
  animation: rise-heart var(--heart-duration) var(--heart-delay) linear infinite;
}

.letter > * {
  position: relative;
  z-index: 2;
}

@keyframes rise-heart {
  0% { opacity: 0; transform: translate(-50%, 100%) rotate(-12deg); }
  12%, 77% { opacity: var(--heart-opacity); }
  50% { transform: translate(calc(-50% + var(--heart-drift)), -50vh) rotate(9deg); }
  100% { opacity: 0; transform: translate(-50%, -100vh) rotate(-6deg); }
}

@media (orientation: landscape) and (max-height: 530px) {
  .open-letter { width: min(calc(100% - 36px), 82vh, 460px); }
}

@media (prefers-reduced-motion: reduce) {
  .experience *, .experience *::before { animation: none !important; }
}
