:root {
  --reader-font-scale: 1;
  --paper-bg: rgba(255, 250, 244, 0.88);
  --text-main: #3f2a22;
  --text-soft: #7c5b4b;
  --text-faint: #9b7462;
  --accent: #7d4f3a;
  --accent-strong: #5d392b;
  --accent-soft: #d99583;
  --rose: #b94f66;
  --mint: #6d9080;
  --border: rgba(90, 54, 35, 0.16);
  --shadow: 0 24px 80px rgba(80, 45, 20, 0.18);
}

* {
  box-sizing: border-box;
}

html {
  min-height: 100%;
  -webkit-text-size-adjust: 100%;
  scroll-behavior: smooth;
}

body {
  min-height: 100vh;
  margin: 0;
  color: var(--text-main);
  font-family: -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
  background-color: #fff3ed;
  background-image:
    radial-gradient(circle at 14% 18%, rgba(255, 255, 255, 0.48), transparent 24rem),
    linear-gradient(rgba(255, 246, 238, 0.68), rgba(255, 246, 238, 0.68)),
    url("assets/bg/letter-bg.webp"),
    url("assets/placeholders/letter-bg.svg");
  background-size: auto, auto, cover, cover;
  background-position: center, center, center, center;
  background-attachment: fixed;
}

body.modal-open {
  overflow: hidden;
}

button {
  font: inherit;
}

button:focus-visible {
  outline: 3px solid rgba(185, 79, 102, 0.38);
  outline-offset: 3px;
}

[hidden] {
  display: none !important;
}

.cover-screen {
  min-height: 100vh;
  display: grid;
  place-items: center;
  padding: max(24px, env(safe-area-inset-top)) 24px max(24px, env(safe-area-inset-bottom));
}

.cover-card {
  width: min(92vw, 520px);
  padding: clamp(34px, 8vw, 54px) clamp(24px, 6vw, 38px);
  border: 1px solid rgba(255, 255, 255, 0.6);
  border-radius: 28px;
  text-align: center;
  background: rgba(255, 250, 244, 0.88);
  box-shadow: 0 28px 90px rgba(75, 42, 28, 0.2);
  backdrop-filter: blur(10px);
  animation: cardArrive 680ms ease both;
}

.cover-eyebrow {
  margin: 0;
  color: var(--rose);
  font-size: 13px;
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

.cover-card h1 {
  margin: 14px 0 30px;
  font-size: clamp(28px, 6vw, 42px);
  line-height: 1.28;
  letter-spacing: 0;
}

.primary-button,
.music-resume,
.video-manual {
  border: 0;
  border-radius: 999px;
  cursor: pointer;
  transition: transform 180ms ease, box-shadow 180ms ease, background 180ms ease;
}

.primary-button {
  padding: 14px 30px;
  color: #fffaf5;
  background: linear-gradient(135deg, var(--accent), var(--rose));
  box-shadow: 0 14px 32px rgba(125, 79, 58, 0.28);
}

.primary-button:hover,
.music-resume:hover,
.video-manual:hover {
  transform: translateY(-2px);
}

.letter-screen {
  min-height: 100vh;
  padding: clamp(22px, 5vw, 64px) 0;
}

.reader-tools {
  position: sticky;
  top: 0;
  z-index: 30;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 8px;
  margin: 0 auto 24px;
  padding: max(10px, env(safe-area-inset-top)) 12px 14px;
  background: linear-gradient(
    180deg,
    rgba(255, 243, 237, 0.96) 0%,
    rgba(255, 243, 237, 0.88) 72%,
    rgba(255, 243, 237, 0) 100%
  );
}

.reader-tools button,
.font-scale-value {
  min-height: 38px;
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 8px 13px;
  color: var(--text-main);
  background: rgba(255, 250, 244, 0.86);
  box-shadow: 0 8px 22px rgba(80, 45, 20, 0.08);
  backdrop-filter: blur(8px);
}

.reader-tools button {
  cursor: pointer;
}

.font-scale-value {
  min-width: 58px;
  text-align: center;
  color: var(--text-soft);
  font-size: 14px;
}

.letter-card {
  width: min(92vw, 780px);
  margin: 0 auto;
  padding: clamp(24px, 5vw, 56px);
  border: 1px solid rgba(255, 255, 255, 0.62);
  border-radius: 28px;
  background: var(--paper-bg);
  box-shadow: var(--shadow);
  backdrop-filter: blur(7px);
}

.letter-title-block {
  margin-bottom: clamp(26px, 5vw, 38px);
  text-align: center;
}

.letter-title-block h2 {
  margin: 0;
  font-size: clamp(26px, 5vw, 38px);
  line-height: 1.24;
  letter-spacing: 0;
}

.letter-date {
  margin: 0 0 8px;
  color: var(--text-soft);
}

.letter-body {
  font-size: calc(clamp(17px, 2.1vw, 21px) * var(--reader-font-scale));
  line-height: 1.9;
  letter-spacing: 0.03em;
}

.letter-body p {
  margin: 0 0 1.15em;
  text-indent: 2em;
}

/* 称呼顶格，正文每段首行缩进，像一封手写信 */
.letter-body .letter-salutation {
  text-indent: 0;
}

.letter-body code {
  border-radius: 5px;
  padding: 0.05em 0.25em;
  background: rgba(125, 79, 58, 0.1);
  font-family: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  font-size: 0.86em;
}

.character-section {
  position: relative;
  margin-top: clamp(42px, 8vw, 68px);
  text-align: center;
}

.character-hint {
  margin: 0 0 14px;
  color: var(--text-soft);
}

.pet-stage {
  position: relative;
  display: inline-flex;
  flex-direction: column;
  align-items: center;
}

/* speech bubble — the pet "chiming in" */
.pet-bubble {
  position: absolute;
  bottom: calc(100% - 4px);
  left: 50%;
  max-width: min(78vw, 320px);
  width: max-content;
  padding: 10px 15px;
  border-radius: 18px 18px 18px 6px;
  border: 1px solid rgba(125, 79, 58, 0.12);
  background: rgba(255, 252, 248, 0.97);
  color: var(--text-main);
  font-size: 15px;
  line-height: 1.5;
  text-align: center;
  box-shadow: 0 12px 30px rgba(80, 45, 20, 0.16);
  opacity: 0;
  pointer-events: none;
  transform: translate(-50%, 6px);
  transition: opacity 300ms ease, transform 300ms ease;
  z-index: 4;
}

.pet-bubble::after {
  content: "";
  position: absolute;
  left: 30px;
  top: 100%;
  border: 8px solid transparent;
  border-top-color: rgba(255, 252, 248, 0.97);
}

.pet-bubble.is-visible {
  opacity: 1;
  transform: translate(-50%, 0);
}

.character-button {
  position: relative;
  display: inline-grid;
  place-items: center;
  border: 0;
  padding: 0;
  background: transparent;
  cursor: pointer;
  transform-origin: center bottom;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.pet-floater {
  display: inline-grid;
  place-items: center;
  animation: pet-float 3.6s ease-in-out infinite;
}

.pet-gaze {
  position: relative;
  display: inline-grid;
  place-items: center;
  transform: translate(var(--gaze-x, 0px), var(--gaze-y, 0px));
  transition: transform 200ms ease;
}

/* the 一二 panda, driven as a CSS sprite (8 cols x 9 rows, 192x208 / frame) */
.pet {
  --fw: clamp(140px, 40vw, 178px);
  --fh: calc(var(--fw) * 208 / 192);
  position: relative;
  z-index: 1;
  width: var(--fw);
  height: var(--fh);
  background-image: url("assets/character/yi-er-sprite.webp");
  background-repeat: no-repeat;
  background-size: calc(var(--fw) * 8) calc(var(--fh) * 9);
  background-position: 0 0;
  transition: transform 140ms ease;
}

.pet-fallback {
  position: absolute;
  z-index: 1;
  width: clamp(140px, 40vw, 178px);
  height: auto;
  user-select: none;
  -webkit-user-drag: none;
}

.character-glow {
  position: absolute;
  inset: -4% 6%;
  z-index: 0;
  border-radius: 999px;
  background:
    radial-gradient(circle, rgba(255, 255, 255, 0.72), transparent 46%),
    radial-gradient(circle, rgba(217, 149, 131, 0.4), transparent 68%);
  filter: blur(5px);
  opacity: 0.8;
}

/* sprite states: row selects the animation, steps() walks its frames */
.pet[data-state="idle"] {
  background-position-y: 0;
  animation: pet-idle 2.6s steps(6) infinite;
}

.pet[data-state="wave"] {
  background-position-y: calc(var(--fh) * -3);
  animation: pet-wave 0.66s steps(4) infinite;
}

.pet[data-state="excited"] {
  background-position-y: calc(var(--fh) * -1);
  animation: pet-excited 0.72s steps(8) infinite;
}

/* single-click reaction: bashful, paws up by the blushing cheeks (row 7) */
.pet[data-state="shy"] {
  background-position-y: calc(var(--fh) * -7);
  animation: pet-shy 1.1s steps(6) infinite;
}

/* double-click entrance: a wink (row 6, frame 4), paired with flying kiss hearts */
.pet[data-state="kiss"] {
  background-position: calc(var(--fw) * -4) calc(var(--fh) * -6);
  animation: none;
}

.pet[data-state="tap"] {
  background-position: 0 calc(var(--fh) * -4);
  animation: none;
}

@keyframes pet-idle {
  from { background-position-x: 0; }
  to { background-position-x: calc(var(--fw) * -6); }
}

@keyframes pet-wave {
  from { background-position-x: 0; }
  to { background-position-x: calc(var(--fw) * -4); }
}

@keyframes pet-excited {
  from { background-position-x: 0; }
  to { background-position-x: calc(var(--fw) * -8); }
}

@keyframes pet-shy {
  from { background-position-x: 0; }
  to { background-position-x: calc(var(--fw) * -6); }
}

@keyframes pet-float {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-7px); }
}

.character-button:hover .pet {
  transform: scale(1.04);
}

.character-button:active .pet {
  transform: scale(0.93);
}

/* a bashful little wobble on single click */
.character-button.is-shy {
  animation: petShy 1s ease;
}

@keyframes petShy {
  0%, 100% { transform: rotate(0deg) translateY(0); }
  25% { transform: rotate(-4deg) translateY(-2px); }
  60% { transform: rotate(3.5deg) translateY(0); }
}

/* flying kiss hearts for the double-click wink (眨眼飞吻) */
.pet-heart {
  position: absolute;
  left: 50%;
  bottom: 58%;
  z-index: 5;
  font-size: 30px;
  line-height: 1;
  color: var(--rose);
  pointer-events: none;
  text-shadow: 0 2px 6px rgba(185, 79, 102, 0.32);
  animation: heartFly 1150ms ease-out forwards;
}

@keyframes heartFly {
  0% { opacity: 0; transform: translate(-50%, 0) scale(0.4); }
  18% { opacity: 1; }
  100% { opacity: 0; transform: translate(-50%, -130px) scale(1.15) rotate(10deg); }
}

.character-button.pet-arrive {
  animation: petArrive 720ms cubic-bezier(0.16, 1, 0.3, 1) both;
}

@keyframes petArrive {
  from {
    opacity: 0;
    transform: translateY(16px) scale(0.86);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

.video-entrance-active .character-button {
  animation: characterJump 850ms ease both;
}

.video-entrance-active .character-glow {
  animation: glowBloom 850ms ease both;
}

.music-resume {
  position: fixed;
  right: 16px;
  bottom: max(16px, env(safe-area-inset-bottom));
  z-index: 20;
  padding: 10px 16px;
  color: var(--text-main);
  background: rgba(255, 250, 244, 0.94);
  box-shadow: 0 10px 30px rgba(80, 45, 20, 0.18);
}

.video-backdrop {
  position: fixed;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  padding: max(18px, env(safe-area-inset-top)) 16px max(18px, env(safe-area-inset-bottom));
  background:
    radial-gradient(circle at 50% 42%, rgba(185, 79, 102, 0.22), transparent 22rem),
    rgba(18, 13, 10, 0.78);
  z-index: 50;
}

.video-backdrop.is-open {
  display: flex;
  animation: backdropFadeIn 420ms ease both;
}

.video-dialog {
  position: relative;
  width: min(94vw, 900px);
  animation: dialogEnter 560ms cubic-bezier(0.16, 1, 0.3, 1) both;
}

.video-dialog video {
  width: 100%;
  max-height: 82vh;
  display: block;
  border-radius: 22px;
  background: #000;
  box-shadow: 0 28px 110px rgba(0, 0, 0, 0.46);
}

.video-close {
  position: absolute;
  right: 10px;
  top: -48px;
  border: 0;
  border-radius: 999px;
  padding: 9px 15px;
  color: #2d211d;
  background: rgba(255, 255, 255, 0.9);
  cursor: pointer;
}

/* 扫码领取完整视频的弹窗卡片 */
.gift-dialog {
  position: relative;
  width: min(92vw, 420px);
  padding: clamp(26px, 6vw, 40px) clamp(20px, 5vw, 32px);
  border: 1px solid rgba(255, 255, 255, 0.6);
  border-radius: 26px;
  text-align: center;
  background: rgba(255, 250, 244, 0.97);
  box-shadow: 0 28px 110px rgba(0, 0, 0, 0.4);
  animation: dialogEnter 560ms cubic-bezier(0.16, 1, 0.3, 1) both;
}

.gift-eyebrow {
  margin: 0;
  color: var(--rose);
  font-size: 12px;
  font-weight: 700;
  letter-spacing: 0.12em;
  text-transform: uppercase;
}

.gift-title {
  margin: 10px 0 18px;
  font-size: clamp(20px, 5vw, 26px);
  color: var(--text-main);
}

.gift-qr {
  display: inline-block;
  padding: 12px;
  border-radius: 16px;
  background: #fff;
  box-shadow: 0 10px 30px rgba(80, 45, 20, 0.14);
}

.gift-qr img,
.gift-qr canvas {
  display: block;
  width: 180px;
  height: 180px;
}

.gift-tip {
  margin: 16px 0 0;
  color: var(--text-soft);
  font-size: 14px;
}

.gift-tip a {
  color: var(--rose);
  font-weight: 600;
}

.video-manual {
  display: block;
  margin: 16px auto 0;
  padding: 10px 18px;
  color: #fffaf5;
  background: linear-gradient(135deg, var(--accent), var(--mint));
}

.video-lock-hint {
  margin: 14px 0 0;
  text-align: center;
  color: rgba(255, 250, 244, 0.92);
}

@keyframes cardArrive {
  from {
    opacity: 0;
    transform: translateY(14px) scale(0.98);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

@keyframes characterJump {
  0% { transform: translateY(0) scale(1); }
  38% { transform: translateY(-28px) scale(1.08); }
  70% { transform: translateY(0) scale(1.02); }
  100% { transform: translateY(0) scale(1); }
}

@keyframes glowBloom {
  0% {
    opacity: 0.78;
    transform: scale(1);
  }
  60% {
    opacity: 1;
    transform: scale(1.45);
  }
  100% {
    opacity: 0.82;
    transform: scale(1);
  }
}

@keyframes backdropFadeIn {
  from { opacity: 0; }
  to { opacity: 1; }
}

@keyframes dialogEnter {
  from {
    opacity: 0;
    transform: translateY(18px) scale(0.94);
  }
  to {
    opacity: 1;
    transform: translateY(0) scale(1);
  }
}

@media (max-width: 600px) {
  body {
    background-attachment: scroll;
  }

  .cover-card {
    border-radius: 24px;
  }

  .letter-card {
    width: 93vw;
    padding: clamp(20px, 6vw, 34px);
    border-radius: 22px;
  }

  .letter-body {
    font-size: calc(clamp(16px, 4.3vw, 20px) * var(--reader-font-scale));
    line-height: 1.92;
  }

  .reader-tools {
    flex-wrap: wrap;
  }

  .video-close {
    top: -44px;
  }
}

/* tablet portrait: keep a tighter, letter-like reading column */
@media (min-width: 601px) and (max-width: 899px) {
  .letter-card {
    width: min(88vw, 640px);
  }

  .letter-body {
    font-size: calc(clamp(17px, 2.4vw, 19px) * var(--reader-font-scale));
  }
}

@media (min-width: 900px) {
  .letter-card {
    width: min(76vw, 820px);
  }

  .letter-body {
    font-size: calc(clamp(18px, 1.35vw, 22px) * var(--reader-font-scale));
  }
}

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 1ms !important;
    transition-duration: 1ms !important;
    scroll-behavior: auto !important;
  }

  .pet-floater,
  .pet[data-state],
  .character-button.is-shy,
  .pet-heart {
    animation: none !important;
  }

  /* hold a single calm frame instead of flicking through the sheet */
  .pet[data-state="idle"],
  .pet[data-state="wave"],
  .pet[data-state="excited"] {
    background-position: 0 0;
  }

  .pet[data-state="shy"] {
    background-position: 0 calc(var(--fh) * -7);
  }

  .pet-gaze {
    transform: none !important;
  }
}
