:root {
  --bg: #04020b;
  --bg-soft: #120922;
  --accent: #ff4da6;
  --accent-soft: #ff9bd0;
  --text: #f8f3ff;
  --line: rgba(255, 255, 255, 0.24);
  --glow: 0 0 28px rgba(255, 77, 166, 0.46);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  width: 100%;
  min-height: 100%;
  overflow: hidden;
  background: radial-gradient(circle at 50% -8%, #2b1754 0%, #10091f 35%, var(--bg) 75%);
  color: var(--text);
}

body {
  font-family: "Montserrat", sans-serif;
  touch-action: manipulation;
}

#matrixCanvas,
#particleCanvas,
#starfield,
#sceneFlash,
.vignette {
  position: fixed;
  inset: 0;
}

#matrixCanvas {
  z-index: 1;
  opacity: 1;
  transition: opacity 700ms ease;
}

#matrixCanvas.soft {
  opacity: 0.18;
}

#particleCanvas {
  z-index: 9;
  pointer-events: none;
}

#starfield {
  z-index: 2;
  opacity: 0;
  transition: opacity 700ms ease;
  background:
    radial-gradient(circle at 12% 24%, rgba(255, 255, 255, 0.26) 0 1px, transparent 2px),
    radial-gradient(circle at 67% 18%, rgba(255, 255, 255, 0.25) 0 1px, transparent 2px),
    radial-gradient(circle at 83% 62%, rgba(255, 255, 255, 0.22) 0 1px, transparent 2px),
    radial-gradient(circle at 20% 76%, rgba(255, 255, 255, 0.2) 0 1px, transparent 2px);
  background-size: 320px 320px, 300px 300px, 360px 360px, 270px 270px;
  animation: stars-drift 27s linear infinite;
}

#starfield.active {
  opacity: 0.96;
}

#sceneFlash {
  z-index: 8;
  pointer-events: none;
  opacity: 0;
  background: radial-gradient(circle at 50% 50%, rgba(255, 255, 255, 0.86), rgba(255, 255, 255, 0.06) 57%, transparent 74%);
}

body.flash #sceneFlash {
  animation: flash-step 340ms ease;
}

.vignette {
  z-index: 7;
  pointer-events: none;
  background: radial-gradient(circle at center, transparent 43%, rgba(0, 0, 0, 0.68) 100%);
}

.music-toggle {
  position: fixed;
  top: calc(14px + env(safe-area-inset-top));
  right: calc(14px + env(safe-area-inset-right));
  z-index: 20;
  border: 1px solid var(--line);
  border-radius: 999px;
  width: 44px;
  height: 44px;
  color: #f7e9ff;
  background: rgba(9, 6, 18, 0.7);
  font-size: 16px;
  cursor: pointer;
  display: grid;
  place-items: center;
  transition: transform 180ms ease, border-color 180ms ease, box-shadow 180ms ease;
}

.music-toggle:hover {
  transform: translateY(-1px);
  border-color: rgba(255, 155, 208, 0.7);
  box-shadow: 0 6px 18px rgba(255, 77, 166, 0.25);
}

.music-toggle.disabled {
  opacity: 0.45;
  cursor: not-allowed;
  box-shadow: none;
}

.audio-notice {
  position: fixed;
  top: calc(64px + env(safe-area-inset-top));
  right: calc(14px + env(safe-area-inset-right));
  z-index: 20;
  max-width: min(82vw, 320px);
  border: 1px solid rgba(255, 255, 255, 0.24);
  border-radius: 12px;
  padding: 8px 10px;
  font-size: 11px;
  line-height: 1.3;
  letter-spacing: 0.04em;
  background: rgba(10, 6, 20, 0.82);
  color: #f4e8ff;
  backdrop-filter: blur(3px);
}

.audio-notice.hidden {
  display: none;
}

.photo-notice {
  position: fixed;
  top: calc(116px + env(safe-area-inset-top));
  right: calc(14px + env(safe-area-inset-right));
  z-index: 20;
  max-width: min(82vw, 320px);
  border: 1px solid rgba(255, 255, 255, 0.24);
  border-radius: 12px;
  padding: 8px 10px;
  font-size: 11px;
  line-height: 1.3;
  letter-spacing: 0.04em;
  background: rgba(10, 6, 20, 0.82);
  color: #f4e8ff;
  backdrop-filter: blur(3px);
}

.photo-notice.hidden {
  display: none;
}

.app-shell {
  position: relative;
  z-index: 10;
  width: min(860px, 100vw);
  min-height: 100dvh;
  min-height: 100svh;
  margin: 0 auto;
  padding: calc(16px + env(safe-area-inset-top)) 16px calc(16px + env(safe-area-inset-bottom));
  overflow: hidden;
}

.scene {
  position: absolute;
  inset: 0;
  display: grid;
  justify-items: center;
  align-content: center;
  text-align: center;
  opacity: 0;
  transform: translateY(14px) scale(0.98);
  filter: blur(8px);
  pointer-events: none;
  transition: opacity 420ms ease, transform 420ms ease, filter 420ms ease;
  padding: 0 12px;
}

.scene.active {
  opacity: 1;
  transform: translateY(0) scale(1);
  filter: blur(0);
  pointer-events: auto;
}

.scene.leaving {
  opacity: 0;
  transform: translateY(-10px) scale(1.03);
  filter: blur(11px);
}

.countdown {
  margin: 0;
  font-size: clamp(108px, 24vw, 240px);
  line-height: 0.9;
  font-weight: 800;
  color: var(--accent-soft);
  text-shadow: var(--glow);
}

#postcardScene::before {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  transform: translate(-50%, -50%);
  width: min(84vw, 460px);
  aspect-ratio: 1 / 1;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(255, 126, 198, 0.25), rgba(255, 126, 198, 0.05) 50%, transparent 72%);
  filter: blur(6px);
  z-index: 0;
  pointer-events: none;
}

.postcard {
  width: clamp(210px, 38vw, 316px);
  aspect-ratio: 3 / 4;
  border-radius: 12px;
  position: relative;
  z-index: 1;
  cursor: pointer;
  background:
    linear-gradient(160deg, #fff6fb 0%, #ffd6ea 52%, #f7bddb 100%);
  border: 1px solid rgba(255, 255, 255, 0.92);
  box-shadow:
    0 14px 40px rgba(0, 0, 0, 0.44),
    0 0 28px rgba(255, 117, 203, 0.46);
  transition: transform 260ms ease, box-shadow 260ms ease;
  animation: postcard-nudge 1.25s ease-in-out infinite;
}

.postcard:focus-visible,
.postcard:hover {
  transform: scale(1.04);
  box-shadow: 0 14px 40px rgba(255, 77, 166, 0.23);
  outline: none;
}

.postcard.opening {
  animation: postcard-open 420ms ease forwards;
}

.postcard-front {
  width: 100%;
  height: 100%;
  position: relative;
  border-radius: 12px;
  overflow: hidden;
}

.postcard-front::before {
  content: "";
  position: absolute;
  inset: 10px;
  border-radius: 10px;
  border: 1px dashed rgba(130, 43, 92, 0.4);
  pointer-events: none;
}

.postcard-front::after {
  content: "";
  position: absolute;
  inset: 0;
  background: radial-gradient(circle at 50% 18%, rgba(255, 255, 255, 0.56), transparent 58%);
  pointer-events: none;
}

.tiny-heart {
  position: absolute;
  top: 40%;
  left: 50%;
  transform: translate(-50%, -50%);
  font-size: clamp(36px, 6.2vw, 56px);
  color: #f03f86;
  text-shadow: 0 0 16px rgba(255, 118, 194, 0.62);
}

.tiny-hands {
  position: absolute;
  left: 50%;
  bottom: 10%;
  transform: translateX(-50%);
  display: flex;
  gap: 8px;
}

.tiny-hands span {
  width: clamp(62px, 10vw, 90px);
  height: clamp(62px, 10vw, 90px);
  border-radius: 40% 40% 46% 46%;
  border: 2px solid #d1ae9f;
  background: #f1e1d0;
  position: relative;
}

.tiny-hands span::before,
.tiny-hands span::after {
  content: "";
  position: absolute;
  top: -16px;
  width: 20px;
  height: 30px;
  border: 2px solid #d1ae9f;
  border-radius: 40px;
  background: #f1e1d0;
}

.tiny-hands span::before {
  left: 10px;
}

.tiny-hands span::after {
  right: 10px;
}

.tap-note {
  margin: 14px 0 0;
  z-index: 1;
  font-family: "Cormorant Garamond", serif;
  font-size: clamp(24px, 4.9vw, 34px);
  line-height: 1.05;
  color: #f2e6fb;
}

.book-shell {
  width: min(680px, calc(100vw - 20px));
  max-width: 680px;
  min-height: 70vh;
  display: grid;
  place-items: start center;
  padding-top: clamp(20px, 7vh, 76px);
}

.book-shell.closing {
  animation: book-close 420ms ease forwards;
}

.book-card {
  width: min(620px, 100%);
}

.message-banner {
  width: 100%;
  min-height: 96px;
  border-radius: 16px;
  background: rgba(255, 255, 255, 0.95);
  border: 1px solid rgba(255, 255, 255, 0.85);
  box-shadow: 0 15px 36px rgba(0, 0, 0, 0.28);
  color: #2d1a31;
  font-family: "Cormorant Garamond", serif;
  font-size: clamp(23px, 4.9vw, 36px);
  line-height: 1.2;
  padding: 14px 18px;
  display: grid;
  place-items: center start;
  text-align: left;
  opacity: 0;
  transform: translateY(12px) scale(0.97);
  animation: panel-in 460ms ease forwards;
}

.spread-deck {
  margin-top: 12px;
  position: relative;
  width: 100%;
  min-height: clamp(250px, 42vh, 470px);
}

.spread {
  position: absolute;
  inset: 0;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 10px;
  opacity: 0;
  transform: rotateY(90deg) scale(0.98);
  transform-origin: center left;
  pointer-events: none;
}

.spread.active {
  opacity: 1;
  transform: rotateY(0deg) scale(1);
  pointer-events: auto;
  transition: transform 520ms cubic-bezier(.2, .88, .34, 1), opacity 520ms ease;
}

.spread.flipping-out {
  opacity: 0;
  transform: rotateY(-88deg) scale(0.98);
  transition: transform 460ms ease, opacity 460ms ease;
}

.spread.flipping-in {
  opacity: 1;
  transform: rotateY(0deg) scale(1);
  transition: transform 460ms ease, opacity 460ms ease;
}

.photo-tile {
  min-height: clamp(220px, 34vh, 420px);
  border-radius: 14px;
  border: 1px solid rgba(255, 255, 255, 0.62);
  display: grid;
  place-items: center;
  color: #fff;
  letter-spacing: 0.08em;
  text-shadow: 0 2px 10px rgba(0, 0, 0, 0.4);
  box-shadow: 0 12px 26px rgba(0, 0, 0, 0.28), 0 0 16px rgba(255, 77, 166, 0.2);
  overflow: hidden;
  background-position: center;
  background-repeat: no-repeat;
  background-size: contain;
}

.photo-img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  object-position: center;
  background: rgba(9, 6, 18, 0.9);
  display: block;
}

.photo-tile.fallback-photo {
  background-color: rgba(9, 6, 18, 0.95);
}

.photo-tile.p1 {
  background:
    linear-gradient(155deg, rgba(255, 200, 226, 0.24), rgba(255, 255, 255, 0.06)),
    radial-gradient(circle at 20% 18%, rgba(255, 205, 225, 0.5), rgba(243, 115, 173, 0.18)),
    #23162d;
}

.photo-tile.p2 {
  background:
    linear-gradient(150deg, rgba(192, 220, 255, 0.26), rgba(255, 255, 255, 0.07)),
    radial-gradient(circle at 74% 18%, rgba(177, 211, 255, 0.44), rgba(99, 82, 172, 0.22)),
    #1b1b35;
}

.photo-tile.p3 {
  background:
    linear-gradient(140deg, rgba(255, 215, 188, 0.24), rgba(255, 255, 255, 0.06)),
    radial-gradient(circle at 18% 80%, rgba(255, 205, 166, 0.46), rgba(198, 105, 162, 0.22)),
    #2a182c;
}

.photo-tile.p4 {
  background:
    linear-gradient(150deg, rgba(188, 255, 238, 0.22), rgba(255, 255, 255, 0.05)),
    radial-gradient(circle at 84% 22%, rgba(152, 235, 205, 0.4), rgba(93, 129, 177, 0.2)),
    #182437;
}

#heartScene {
  align-content: center;
  gap: 12px;
}

#petalLayer {
  position: absolute;
  inset: 0;
  pointer-events: none;
}

.petal {
  position: absolute;
  top: -8vh;
  font-size: 16px;
  opacity: 0.8;
  animation: petal-fall linear forwards;
}

.heart-stage {
  width: min(640px, calc(100vw - 30px));
  aspect-ratio: 1 / 1;
  position: relative;
}

.restart-btn {
  border: 1px solid var(--line);
  border-radius: 999px;
  background: rgba(7, 5, 16, 0.7);
  color: #f8ebff;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  font-size: 11px;
  font-family: inherit;
  padding: 10px 16px;
  cursor: pointer;
}

.heart-chip {
  position: absolute;
  width: clamp(42px, 8.4vw, 62px);
  aspect-ratio: 4 / 5;
  border-radius: 12px;
  border: 1px solid rgba(255, 255, 255, 0.75);
  box-shadow: 0 10px 24px rgba(0, 0, 0, 0.3), 0 0 18px rgba(255, 77, 166, 0.34);
  background: rgba(10, 7, 20, 0.92);
  color: #fff;
  font-weight: 700;
  font-size: clamp(20px, 4.5vw, 30px);
  display: grid;
  place-items: center;
  padding: 2px;
  transform: translate(-50%, -50%) scale(0.3);
  opacity: 0;
  overflow: hidden;
}

.heart-chip.fly {
  transition: transform 900ms cubic-bezier(.2, .88, .34, 1), opacity 900ms ease;
  opacity: 1;
}

.heart-chip-img {
  width: 100%;
  height: 100%;
  object-fit: contain;
  object-position: center;
  background: rgba(9, 6, 18, 0.96);
  border-radius: 11px;
  display: block;
}

.heart-chip.fallback {
  background:
    linear-gradient(132deg, rgba(255, 190, 227, 0.98), rgba(255, 83, 170, 0.8)),
    radial-gradient(circle at 25% 18%, rgba(255, 255, 255, 0.56), transparent 48%);
}

.heart-chip.fallback .heart-chip-img {
  display: none;
}

@keyframes stars-drift {
  from {
    background-position: 0 0, 0 0, 0 0, 0 0;
  }
  to {
    background-position: 0 320px, 0 300px, 0 360px, 0 270px;
  }
}

@keyframes flash-step {
  0% {
    opacity: 0;
  }
  26% {
    opacity: 0.36;
  }
  100% {
    opacity: 0;
  }
}

@keyframes postcard-nudge {
  0%,
  100% {
    transform: translateY(0) rotate(0deg);
  }
  25% {
    transform: translateY(-1px) rotate(-1.4deg);
  }
  50% {
    transform: translateY(0) rotate(1.3deg);
  }
  75% {
    transform: translateY(-1px) rotate(-1.1deg);
  }
}

@keyframes postcard-open {
  0% {
    transform: scale(1) rotateY(0deg);
    opacity: 1;
  }
  60% {
    transform: scale(1.08) rotateY(-10deg);
    opacity: 1;
  }
  100% {
    transform: scale(1.26) rotateY(-16deg);
    opacity: 0;
  }
}

@keyframes panel-in {
  from {
    opacity: 0;
    transform: translateY(12px) scale(0.97);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

@keyframes petal-fall {
  from {
    transform: translate3d(0, -10vh, 0) rotate(0deg);
    opacity: 0;
  }
  10% {
    opacity: 0.8;
  }
  to {
    transform: translate3d(var(--drift, 44px), 112vh, 0) rotate(430deg);
    opacity: 0;
  }
}

@keyframes book-close {
  from {
    opacity: 1;
    transform: scale(1) translateY(0) rotate(0deg);
  }
  to {
    opacity: 0;
    transform: scale(0.56) translateY(24px) rotate(-6deg);
  }
}

@media (max-width: 760px) {
  .scene {
    overflow-y: auto;
    padding-bottom: calc(14px + env(safe-area-inset-bottom));
  }

  #bookScene {
    align-content: center;
    padding-top: calc(20px + env(safe-area-inset-top));
  }

  #heartScene {
    align-content: start;
    padding-top: calc(62px + env(safe-area-inset-top));
  }

  .app-shell {
    padding: calc(10px + env(safe-area-inset-top)) 10px calc(12px + env(safe-area-inset-bottom));
  }

  .music-toggle {
    top: calc(10px + env(safe-area-inset-top));
    right: calc(10px + env(safe-area-inset-right));
  }

  .audio-notice {
    top: calc(58px + env(safe-area-inset-top));
    right: calc(10px + env(safe-area-inset-right));
    max-width: min(88vw, 320px);
  }

  .photo-notice {
    top: calc(104px + env(safe-area-inset-top));
    right: calc(10px + env(safe-area-inset-right));
    max-width: min(88vw, 320px);
  }

  .postcard {
    width: min(74vw, 308px);
  }

  .tap-note {
    max-width: 88vw;
    font-size: clamp(22px, 6.5vw, 30px);
    text-wrap: balance;
  }

  .book-shell {
    width: min(680px, calc(100vw - 14px));
    min-height: auto;
    max-height: calc(100svh - 36px);
    padding-top: clamp(2px, 1.4vh, 10px);
    overflow: hidden;
  }

  .book-card {
    width: min(620px, calc(100vw - 18px));
  }

  .message-banner {
    min-height: 62px;
    padding: 8px 10px;
    place-items: center;
    text-align: center;
    font-size: clamp(18px, 5.4vw, 26px);
    line-height: 1.15;
  }

  .spread-deck {
    height: clamp(230px, 43svh, 340px);
    min-height: 0;
    max-height: 46svh;
  }

  .spread {
    grid-template-columns: 1fr 1fr;
    grid-template-rows: 1fr;
    min-height: 0;
    gap: 6px;
  }

  .photo-tile {
    min-height: 0;
    height: 100%;
    border-radius: 12px;
  }

  .heart-stage {
    width: min(92vw, 470px);
  }

  .heart-chip {
    width: clamp(42px, 10.6vw, 62px);
    border-radius: 11px;
  }
}

@media (max-width: 480px) {
  .scene {
    padding: 0 8px;
  }

  .music-toggle {
    width: 40px;
    height: 40px;
    font-size: 14px;
  }

  .audio-notice,
  .photo-notice {
    font-size: 10px;
    padding: 7px 8px;
  }

  .photo-notice {
    top: calc(98px + env(safe-area-inset-top));
  }

  .postcard {
    width: min(82vw, 304px);
  }

  .tap-note {
    font-size: clamp(20px, 7.4vw, 28px);
  }

  #bookScene {
    padding-top: calc(12px + env(safe-area-inset-top));
  }

  #heartScene {
    padding-top: calc(50px + env(safe-area-inset-top));
  }

  .book-shell {
    width: calc(100vw - 10px);
    min-height: auto;
    max-height: calc(100svh - 22px);
    padding-top: clamp(2px, 1vh, 8px);
  }

  .book-card {
    width: calc(100vw - 14px);
  }

  .message-banner {
    min-height: 56px;
    padding: 7px 9px;
    font-size: clamp(16px, 6.2vw, 20px);
  }

  .spread-deck {
    height: clamp(210px, 42svh, 320px);
    min-height: 0;
    max-height: 44svh;
  }

  .photo-tile {
    min-height: 0;
    height: 100%;
  }

  .heart-stage {
    width: min(92vw, 340px);
  }

  .restart-btn {
    margin-top: 6px;
    font-size: 10px;
    padding: 8px 13px;
  }
}

@media (max-height: 720px) and (max-width: 760px) {
  #bookScene {
    padding-top: calc(50px + env(safe-area-inset-top));
  }

  #heartScene {
    padding-top: calc(52px + env(safe-area-inset-top));
  }

  .book-shell {
    max-height: calc(100svh - 18px);
  }

  .spread-deck {
    height: clamp(190px, 35svh, 270px);
    max-height: 38svh;
  }
}

@media (max-width: 380px) {
  .message-banner {
    font-size: clamp(17px, 7.4vw, 22px);
    line-height: 1.1;
  }

  .spread-deck {
    height: clamp(180px, 34svh, 250px);
    min-height: 0;
  }

  .photo-tile {
    min-height: 0;
  }
}
