:root {
  --cw: min(88vw, 370px);
  --ch: min(calc(var(--cw) * 1.12), 60vh);
  --ew: min(78vw, 320px);
  --eh: calc(var(--ew) * 0.68);
  --font: 'Quicksand', 'Baloo 2', ui-rounded, 'Hiragino Maru Gothic ProN', system-ui, sans-serif;
}
@supports (height: 1dvh) {
  :root { --ch: min(calc(var(--cw) * 1.12), 60dvh); }
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body {
  margin: 0; height: 100%; overflow: hidden;
  overscroll-behavior: none; touch-action: manipulation;
}
body {
  background-color: var(--bg);
  background-image: radial-gradient(var(--bg-dot) 2.5px, transparent 3px);
  background-size: 24px 24px;
  color: var(--ink);
  font-family: var(--font);
  -webkit-user-select: none; user-select: none;
  -webkit-touch-callout: none;
}
img { -webkit-user-drag: none; -webkit-touch-callout: none; display: block; }
button { font-family: inherit; color: inherit; }

#app { position: fixed; inset: 0; height: 100vh; height: 100dvh; overflow: hidden; }

/* ---------- nút tắt tiếng ---------- */
#mute {
  position: absolute; z-index: 30;
  top: calc(env(safe-area-inset-top) + 10px); right: 14px;
  width: 40px; height: 40px; border-radius: 50%;
  border: 2px solid var(--accent); background: var(--card);
  font-size: 18px; line-height: 1; padding: 0;
}

/* ---------- phong bì ---------- */
#envelope {
  position: absolute; left: 50%; top: 50%;
  width: var(--ew); height: var(--eh);
  transform: translate(-50%, -50%);
  cursor: pointer; outline: none;
}
.env-back {
  position: absolute; inset: 0; z-index: 0; border-radius: 10px;
  background: var(--envelope); filter: brightness(0.93);
  box-shadow: 0 10px 22px rgba(0, 0, 0, 0.12);
}
/* Hình SVG thay cho clip-path: Chrome bỏ qua clip-path của lớp này khi nắp đang chạy animation 3D,
   làm cả mặt trước phong bì phủ kín phần bên trong. */
.env-pocket {
  position: absolute; inset: 0; z-index: 2; width: 100%; height: 100%;
}
.env-pocket path { fill: var(--envelope); }
.flap {
  position: absolute; left: 0; top: 0; width: 100%; height: 58%; z-index: 3;
  transform-origin: top; transform-style: preserve-3d;
  transform: perspective(700px) rotateX(0deg);
  transition: transform 0.6s ease-in-out, z-index 0s 0s, opacity 0.6s;
}
.flap-f, .flap-b {
  position: absolute; inset: 0;
  clip-path: polygon(0 0, 100% 0, 50% 100%);
  -webkit-backface-visibility: hidden; backface-visibility: hidden;
}
.flap-f { background: var(--flap); }
.flap-b { background: var(--flap); filter: brightness(0.9); transform: rotateX(180deg); clip-path: polygon(50% 0, 100% 100%, 0 100%); }
.seal {
  position: absolute; z-index: 4; left: 50%; top: 58%;
  width: 46px; height: 46px; margin: -23px 0 0 -23px; border-radius: 50%;
  background: var(--seal); color: #fff; font-size: 22px;
  display: flex; align-items: center; justify-content: center;
  box-shadow: 0 3px 6px rgba(0, 0, 0, 0.2);
  transition: opacity 0.3s, transform 0.3s;
}
.p-flap .flap { transform: perspective(700px) rotateX(180deg); z-index: 0; transition: transform 0.6s ease-in-out, z-index 0s 0.3s, opacity 0.6s; }
.p-flap .seal { opacity: 0; transform: scale(0.3); }
.env-back, .env-pocket { transition: opacity 0.6s; }
.p-full #envelope { pointer-events: none; }
.p-full .env-back, .p-full .env-pocket, .p-full .flap, .p-full .seal { opacity: 0; }

/* ---------- thiệp ---------- */
.card {
  position: absolute; left: 50%; top: 50%; z-index: 1;
  width: var(--cw); height: var(--ch);
  margin: calc(var(--ch) / -2) 0 0 calc(var(--cw) / -2);
  transform: scale(0.5);
  transition: transform 0.6s cubic-bezier(0.4, 0, 0.2, 1), z-index 0s 0.6s;
}
.p-out .card { transform: translateY(calc(var(--eh) * -0.75)) scale(0.5); }
.p-full .card { z-index: 6; transform: none; transition: transform 0.6s cubic-bezier(0.4, 0, 0.2, 1), z-index 0s 0s; }

.page {
  position: absolute; inset: 0; border-radius: 16px;
  background: var(--card); border: 3px solid var(--accent);
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.15);
  display: flex; flex-direction: column; overflow: hidden;
}
.page-content {
  display: flex; flex-direction: column; flex: 1; min-height: 0;
  padding: 26px 24px 22px; opacity: 0; transition: opacity 0.5s 0s;
}
.p-book .page-content { opacity: 1; transition: opacity 0.6s 0.5s; }
.recipient { margin: 0 0 14px; font-size: 21px; font-weight: 700; color: var(--ink); }
.message {
  margin: 0; flex: 1; min-height: 0; overflow-y: auto; -webkit-overflow-scrolling: touch;
  touch-action: pan-y; font-size: 17px; line-height: 1.6; font-weight: 500;
  white-space: pre-line; -webkit-user-select: text; user-select: text;
}

.cover {
  position: absolute; inset: 0; transform-origin: left center;
  transform-style: preserve-3d;
  transform: perspective(1400px) rotateY(0deg);
  transition: transform 0.8s ease-in-out;
}
.p-book .cover { transform: perspective(1400px) rotateY(-180deg); }
.cover-front, .cover-back {
  position: absolute; inset: 0; border-radius: 16px;
  -webkit-backface-visibility: hidden; backface-visibility: hidden;
}
.cover-front {
  background: var(--accent); border: 3px solid var(--card);
  outline: 3px solid var(--accent); outline-offset: -3px;
  display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 12px;
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.15);
}
.cover-mark { font-size: 96px; line-height: 1; color: var(--card); }
.cover-text { font-size: 24px; font-weight: 700; color: var(--ink); background: var(--card); padding: 4px 16px; border-radius: 999px; }
.cover-back {
  background: var(--card); border: 3px solid var(--accent); transform: rotateY(180deg);
  background-image: repeating-linear-gradient(transparent 0 31px, var(--bg-dot) 31px 33px);
}

/* ---------- gợi ý, nút đóng ---------- */
#hint {
  position: absolute; z-index: 5; left: 50%; margin: 0;
  top: calc(50% + var(--eh) / 2 + 28px); transform: translateX(-50%);
  font-size: 18px; font-weight: 700; white-space: nowrap;
  animation: blink 1.6s ease-in-out infinite; transition: opacity 0.3s;
}
.p-flap #hint, .p-open #hint { opacity: 0; animation: none; pointer-events: none; }
#close {
  position: absolute; z-index: 20; left: 50%;
  bottom: calc(env(safe-area-inset-bottom) + 22px);
  transform: translateX(-50%);
  padding: 10px 26px; font-size: 18px; font-weight: 700;
  background: var(--card); border: 3px solid var(--accent); border-radius: 999px;
  opacity: 0; pointer-events: none; transition: opacity 0.4s;
}
.p-open #close { opacity: 1; pointer-events: auto; }
@keyframes blink { 0%, 100% { opacity: 1; } 50% { opacity: 0.35; } }

/* ---------- sticker ---------- */
#stickers, #particles { position: absolute; inset: 0; z-index: 10; pointer-events: none; }
#particles { z-index: 25; }
.slot {
  position: absolute; width: var(--size); height: var(--size);
  transform: translate(-50%, -50%) rotate(var(--rot));
  pointer-events: auto;
}
.pop {
  width: 100%; height: 100%;
  opacity: 0; transform: scale(0) rotate(-20deg);
  transition: transform 0.4s cubic-bezier(0.34, 1.56, 0.64, 1), opacity 0.2s;
}
.pop.in { opacity: 1; transform: none; }
.pop.out { opacity: 0; transform: scale(0.3) rotate(15deg); transition: transform 0.25s ease-in, opacity 0.25s; pointer-events: none; }
.pop.hit { animation: hit 0.4s; }
.bob { width: 100%; height: 100%; animation: bob 3s ease-in-out infinite alternate; }
.bob img { width: 100%; height: 100%; object-fit: contain; }
@keyframes bob {
  from { transform: translateY(0) rotate(-2.5deg); }
  to { transform: translateY(-7px) rotate(2.5deg); }
}
@keyframes hit { 40% { transform: scale(1.3) rotate(8deg); } }

.particle {
  position: absolute; font-size: 22px; line-height: 1;
  animation: rise 1.5s ease-out forwards;
}
@keyframes rise {
  from { opacity: 1; transform: translate(0, 0) scale(0.6); }
  to { opacity: 0; transform: translate(var(--dx), -140px) scale(1.2); }
}

/* ---------- đổi theme ---------- */
#wipe {
  position: absolute; inset: 0; z-index: 0; pointer-events: none;
  opacity: 0; clip-path: circle(0 at 50% 50%);
}
#wipe.on { opacity: 1; clip-path: circle(75vmax at 50% 50%); transition: clip-path 0.6s ease-out; }
#wipe.on.fade { opacity: 0; transition: opacity 0.3s; }
.p-switch #hint { opacity: 0; animation: none; }
.p-switch #envelope { pointer-events: none; transition: transform 0.6s cubic-bezier(0.34, 1.56, 0.64, 1); }
.p-leave #envelope { transform: translate(-50%, calc(-50% - 110vh)) rotate(-12deg) scale(0.85); transition: transform 0.5s cubic-bezier(0.55, 0, 0.75, 0); }
.p-enter #envelope { transform: translate(-50%, calc(-50% - 110vh)) rotate(8deg); transition: none; }

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation: none !important; }
  .flap, .cover, .card, .pop { transition-property: opacity !important; transition-duration: 0.4s !important; }
  .cover { opacity: 1; }
  .p-book .cover { opacity: 0; }
  #wipe { clip-path: none; }
  #wipe.on { transition: opacity 0.4s; }
  .p-leave #envelope, .p-enter #envelope { transform: translate(-50%, -50%); }
}
