:root {
  --bg: #fbf3e8;
  --bg-2: #f7e6d4;
  --paper: #fffaf3;
  --ink: #4a3a32;
  --muted: #94796a;
  --peach: #f4b393;
  --rose: #e39a9a;
  --gold: #e8b65c;
  --brown: #b9825a;
  --shadow: 0 10px 30px rgba(120, 80, 50, .14);
  --radius: 18px;
  --font: "Nunito", system-ui, sans-serif;
  --hand: "Caveat", "Comic Sans MS", cursive;
}
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --bg: #2a211d; --bg-2: #33281f; --paper: #3a2e27; --ink: #f6e9dc;
    --muted: #c4a896; --shadow: 0 10px 30px rgba(0,0,0,.35);
  }
}
:root[data-theme="dark"] {
  --bg: #2a211d; --bg-2: #33281f; --paper: #3a2e27; --ink: #f6e9dc;
  --muted: #c4a896; --shadow: 0 10px 30px rgba(0,0,0,.35);
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; scroll-behavior: smooth; }
body {
  margin: 0;
  background: var(--bg);
  background-image:
    radial-gradient(circle at 15% 10%, color-mix(in srgb, var(--peach) 25%, transparent), transparent 40%),
    radial-gradient(circle at 90% 40%, color-mix(in srgb, var(--rose) 18%, transparent), transparent 45%),
    radial-gradient(circle at 20% 85%, color-mix(in srgb, var(--gold) 18%, transparent), transparent 40%);
  background-attachment: fixed;
  color: var(--ink);
  font-family: var(--font);
  font-size: 18px;
  line-height: 1.7;
  overflow-x: hidden;
}
body.locked { overflow: hidden; height: 100dvh; }
.hand { font-family: var(--hand); }
.muted { color: var(--muted); }
h2 { font-weight: 800; font-size: 1.7rem; line-height: 1.25; margin: 0 0 .8em; }

/* ---------------- envelope ---------------- */
.envelope-screen {
  position: fixed; inset: 0; z-index: 50;
  display: grid; place-items: center;
  background: var(--bg);
  cursor: pointer;
  transition: opacity .9s ease, visibility .9s;
  -webkit-tap-highlight-color: transparent;
}
.envelope-screen.gone { opacity: 0; visibility: hidden; }
.envelope-wrap { display: flex; flex-direction: column; align-items: center; padding: 16px; }
.envelope {
  position: relative; width: min(320px, 82vw); aspect-ratio: 3 / 2;
  background: #f2c9a8; border-radius: 10px; box-shadow: var(--shadow);
  perspective: 800px;
  animation: float 3.5s ease-in-out infinite;
}
.env-front {
  position: absolute; inset: 0; border-radius: 10px; z-index: 3;
  background: #f6d4b6;
  clip-path: polygon(0 0, 50% 58%, 100% 0, 100% 100%, 0 100%);
}
.env-label { position: absolute; left: 0; right: 0; bottom: 9%; z-index: 5; text-align: center;
  font-family: var(--hand); font-size: 1.6rem; color: #9a6a48; transition: opacity .3s; }
.envelope-screen.open .env-label { opacity: 0; }
.env-flap {
  position: absolute; top: 0; left: 0; right: 0; height: 60%; z-index: 4;
  background: #eebc95;
  clip-path: polygon(0 0, 100% 0, 50% 100%);
  transform-origin: top; transition: transform .7s ease, z-index 0s .35s;
  border-radius: 10px 10px 0 0;
}
.env-letter {
  position: absolute; left: 7%; right: 7%; top: 10%; bottom: 8%; z-index: 2;
  background: var(--paper); border-radius: 6px; padding: 14px 16px; text-align: center;
  color: var(--ink); transition: transform .8s cubic-bezier(.2,.8,.2,1) .35s;
  box-shadow: 0 2px 8px rgba(0,0,0,.08);
}
.env-hello { margin: .3em 0 0; font-weight: 800; font-size: 1.35rem; }
.env-sub { margin: 0; font-size: 1.5rem; color: var(--brown); line-height: 1.2; }
.env-seal {
  position: absolute; left: 50%; top: 56%; transform: translate(-50%, -50%); z-index: 5;
  width: 42px; height: 42px; border-radius: 50%; background: var(--rose); color: #fff;
  display: grid; place-items: center; font-size: 1.1rem; box-shadow: 0 3px 8px rgba(0,0,0,.15);
  transition: opacity .3s;
}
.envelope-screen.open .env-flap { transform: rotateX(180deg); z-index: 1; }
.envelope-screen.open .env-seal { opacity: 0; }
.envelope-screen.open .env-letter { transform: translateY(-55%); }
.envelope-screen.open .envelope { animation: none; }
.tap-hint { margin-top: 26px; font-size: 1.5rem; color: var(--muted); animation: pulse 1.8s ease-in-out infinite; }
.pig-on-envelope { width: 110px; margin-bottom: -18px; position: relative; z-index: 6; }

/* ---------------- letter ---------------- */
main { max-width: 640px; margin: 0 auto; padding: 0 16px 40px; }
.hero { min-height: 100dvh; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; }
.hero-to { font-size: 1.6rem; margin: 0; color: var(--muted); }
.hero-name { font-size: clamp(3.4rem, 18vw, 5.5rem); margin: 0; line-height: 1; font-weight: 800;
  background: linear-gradient(120deg, var(--peach), var(--rose), var(--gold)); -webkit-background-clip: text; background-clip: text; color: transparent; }
.hero-sub { font-size: 1.8rem; margin: .2em 0 0; color: var(--brown); }
.pig-hero { width: 150px; margin: 24px 0 10px; }
.scroll-hint { color: var(--muted); font-size: .9rem; animation: bob 2s ease-in-out infinite; }

.letter-section, .card-section {
  background: var(--paper); border-radius: var(--radius); box-shadow: var(--shadow);
  padding: 28px 24px; margin: 0 0 48px; position: relative;
}
.letter-section p, .card-section p { margin: 0 0 1em; }
.letter-section p:last-child, .card-section p:last-child { margin-bottom: 0; }
.placeholder { background: color-mix(in srgb, var(--gold) 30%, transparent); border-radius: 6px; padding: 0 4px; font-style: italic; }
.peek { position: absolute; width: 64px; top: -40px; right: 18px; }
.peek.left { right: auto; left: 18px; }

/* ---------------- memories ---------------- */
.memories { margin: 0 0 56px; text-align: center; }
.memories > .muted { margin: -.8em 0 1.2em; font-size: 1.3rem; }
.year { font-family: var(--hand); font-size: 2.4rem; color: var(--brown); margin: 1em 0 .3em; position: relative; }
.year::before, .year::after { content: ""; display: inline-block; width: 40px; height: 2px; background: var(--peach); vertical-align: middle; margin: 0 12px; border-radius: 2px; }
.grid { display: grid; grid-template-columns: repeat(2, 1fr); gap: 22px 16px; padding: 6px 4px; }
@media (min-width: 560px) { .grid { grid-template-columns: repeat(3, 1fr); } }
.polaroid {
  background: #fff; padding: 8px 8px 0; border-radius: 4px; box-shadow: var(--shadow);
  transform: rotate(var(--tilt, 0deg)); transition: transform .3s ease; cursor: pointer;
  border: 0; font: inherit; color: #4a3a32; text-align: center; width: 100%; display: block;
  -webkit-tap-highlight-color: transparent;
}
.polaroid:hover, .polaroid:focus-visible { transform: rotate(0deg) scale(1.04); outline: none; }
.polaroid .ph { position: relative; aspect-ratio: 1; overflow: hidden; background: #efe4d8; border-radius: 2px; }
.polaroid img, .polaroid video { width: 100%; height: 100%; object-fit: cover; display: block; }
.polaroid .cap { font-family: var(--hand); font-size: 1.15rem; line-height: 1.15; padding: 8px 2px 10px; min-height: 2.3em; display: flex; align-items: center; justify-content: center; }
.badge { position: absolute; right: 6px; bottom: 6px; background: rgba(0,0,0,.5); color: #fff; font-size: .75rem; border-radius: 999px; padding: 2px 8px; font-weight: 600; }
.badge.play { left: 50%; top: 50%; right: auto; bottom: auto; transform: translate(-50%,-50%); width: 46px; height: 46px; padding: 0; display: grid; place-items: center; font-size: 1.1rem; }
.tape { position: absolute; top: -10px; left: 50%; width: 56px; height: 18px; transform: translateX(-50%) rotate(-3deg); background: color-mix(in srgb, var(--gold) 55%, transparent); opacity: .8; z-index: 1; }
.polaroid-wrap { position: relative; }
.polaroid-wrap.tall { grid-row: span 2; align-self: center; }
.polaroid-wrap.tall .ph { aspect-ratio: 9 / 16; }

/* ---------------- wishes ---------------- */
.wishes { margin: 0 0 64px; text-align: center; }
#wishes { list-style: none; padding: 0; margin: 0; display: flex; flex-wrap: wrap; gap: 12px; justify-content: center; }
#wishes li {
  background: var(--paper); border-radius: 14px; padding: 10px 16px; box-shadow: var(--shadow);
  font-size: 1rem; max-width: 100%;
  opacity: 0; transform: translateY(14px) rotate(var(--tilt, 0deg));
  transition: opacity .6s ease, transform .6s ease; transition-delay: var(--d, 0s);
}
.wishes.in #wishes li { opacity: 1; transform: translateY(0) rotate(var(--tilt, 0deg)); }
#wishes li:nth-child(3n+1) { border-bottom: 3px solid var(--peach); }
#wishes li:nth-child(3n+2) { border-bottom: 3px solid var(--rose); }
#wishes li:nth-child(3n) { border-bottom: 3px solid var(--gold); }

/* ---------------- closing ---------------- */
.closing { min-height: 90dvh; display: flex; flex-direction: column; align-items: center; justify-content: center; text-align: center; padding: 40px 0; }
.closing-line { font-size: clamp(1.4rem, 6vw, 2rem); font-weight: 600; margin: 0 0 .4em; line-height: 1.35;
  opacity: 0; transform: translateY(10px); filter: blur(4px); transition: opacity 1.4s ease, transform 1.4s ease, filter 1.4s ease; }
.closing-line.on { opacity: 1; transform: none; filter: none; }
.closing-line:last-child { color: var(--brown); }
.signature { font-size: 1.9rem; margin: 1.2em 0 0; }
.pig-wave { width: 120px; margin-top: 16px; }
.goodbye { font-size: 1.7rem; margin: .2em 0 0; color: var(--muted); }
.reveal-late { opacity: 0; transition: opacity 1.2s ease; }
.reveal-late.on { opacity: 1; }
.foot { text-align: center; font-size: .8rem; color: var(--muted); padding: 20px 0 80px; }

/* ---------------- pdf ---------------- */
.pdf-box { text-align: center; margin: 10px 0 0; }
.pdf-box .hand { font-size: 1.5rem; margin: 0 0 8px; color: var(--muted); }
.pdf-btn { border: 0; border-radius: 999px; padding: 14px 24px; font: 700 1rem var(--font); cursor: pointer;
  background: linear-gradient(120deg, var(--peach), var(--rose)); color: #fff; box-shadow: var(--shadow); }
.pdf-btn:disabled { opacity: .7; cursor: progress; }
.pdf-status { font-size: .85rem; min-height: 1.4em; margin: 10px 0 0; }

/* ---------------- reveal ---------------- */
.reveal { opacity: 0; transform: translateY(28px); transition: opacity .9s ease, transform .9s ease; }
.reveal.in { opacity: 1; transform: none; }

/* ---------------- music ---------------- */
.music-btn {
  position: fixed; right: 16px; bottom: calc(16px + env(safe-area-inset-bottom)); z-index: 40;
  display: flex; align-items: center; gap: 8px; max-width: calc(100vw - 32px);
  background: var(--paper); color: var(--ink); border: 0; border-radius: 999px;
  padding: 10px 16px 10px 12px; box-shadow: var(--shadow); font: 600 .85rem var(--font); cursor: pointer;
}
.music-icon { width: 28px; height: 28px; border-radius: 50%; background: var(--peach); color: #fff; display: grid; place-items: center; font-size: 1rem; }
.music-btn.playing .music-icon { animation: spin 3s linear infinite; }
.music-label { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#yt-holder { position: fixed; width: 1px; height: 1px; left: -10px; bottom: 0; opacity: 0; pointer-events: none; overflow: hidden; }

/* ---------------- lightbox ---------------- */
.lightbox { position: fixed; inset: 0; z-index: 60; background: rgba(30, 20, 15, .92); display: flex; align-items: center; justify-content: center; padding: 16px; }
.lightbox[hidden] { display: none; }
.lb-figure { margin: 0; max-width: 100%; max-height: 100%; display: flex; flex-direction: column; align-items: center; }
.lb-media img, .lb-media video { max-width: min(92vw, 900px); max-height: 78dvh; border-radius: 6px; display: block; background: #000; }
.lb-figure figcaption { color: #fbeee0; font-size: 1.5rem; margin-top: 10px; text-align: center; }
.lb-close { position: absolute; top: calc(8px + env(safe-area-inset-top)); right: 12px; background: none; border: 0; color: #fff; font-size: 2.4rem; cursor: pointer; line-height: 1; padding: 8px; }
.lb-nav { position: absolute; top: 50%; transform: translateY(-50%); background: rgba(255,255,255,.12); border: 0; color: #fff; font-size: 2rem; width: 44px; height: 44px; border-radius: 50%; cursor: pointer; }
.lb-prev { left: 8px; } .lb-next { right: 8px; }

/* ---------------- pig ---------------- */
.pig-slot svg { width: 100%; height: auto; display: block; overflow: visible; }
.pig-hero svg { animation: float 3s ease-in-out infinite; }
.pig-wave .paw { transform-origin: 70px 92px; animation: wave 1s ease-in-out infinite; }
.pig .blink { animation: blink 4.5s infinite; transform-origin: center; transform-box: fill-box; }

@keyframes float { 0%,100% { transform: translateY(0); } 50% { transform: translateY(-8px); } }
@keyframes bob { 0%,100% { transform: translateY(0); } 50% { transform: translateY(5px); } }
@keyframes pulse { 0%,100% { opacity: .55; } 50% { opacity: 1; } }
@keyframes spin { to { transform: rotate(360deg); } }
@keyframes wave { 0%,100% { transform: rotate(0); } 50% { transform: rotate(-25deg); } }
@keyframes blink { 0%,92%,100% { transform: scaleY(1); } 95% { transform: scaleY(.1); } }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; transition-duration: .01ms !important; transition-delay: 0s !important; }
  .reveal, .closing-line, .reveal-late, #wishes li { opacity: 1 !important; transform: none !important; filter: none !important; }
}
