/* ============ ตัวแปรหลัก ============ */
:root {
  --cream: #f5ede1;
  --paper: #fffaf2;
  --rose: #cf9c9a;
  --rose-soft: #ead0cb;
  --wine: #7a2638;
  --wine-dark: #591a29;
  --ink: #3f2c2c;
  --muted: #7d6361;
  --shadow: 0 2px 4px rgba(78, 40, 36, .08), 0 12px 30px rgba(78, 40, 36, .14);
  --thai: "Noto Serif Thai", "Noto Serif Lao", "Thonburi", serif;
  --lao: "Noto Serif Lao", "Lao Sangam MN", "Noto Serif Thai", serif;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-r: env(safe-area-inset-right, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --safe-l: env(safe-area-inset-left, 0px);
}

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

html, body {
  height: 100%;
  overflow: hidden;
  overscroll-behavior: none;
}

body {
  font-family: var(--thai);
  color: var(--ink);
  background-color: var(--cream);
  /* ลายกระดาษเบา ๆ */
  background-image:
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='180' height='180'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='.85' numOctaves='2' stitchTiles='stitch'/%3E%3CfeColorMatrix values='0 0 0 0 .45 0 0 0 0 .28 0 0 0 0 .2 0 0 0 .09 0'/%3E%3C/filter%3E%3Crect width='180' height='180' filter='url(%23n)'/%3E%3C/svg%3E"),
    radial-gradient(ellipse at 50% 30%, #fbf5ea 0%, #f5ede1 55%, #ecdcd0 100%);
  line-height: 1.6;
  -webkit-text-size-adjust: 100%;
  text-size-adjust: 100%;
  -webkit-tap-highlight-color: transparent;
}

[lang="lo"] {
  font-family: var(--lao);
  letter-spacing: normal;   /* ห้ามเว้นช่องไฟ สระและวรรณยุกต์จะเพี้ยน */
  word-break: normal;
  overflow-wrap: break-word; /* ตัดคำตามพจนานุกรมก่อน ล้นจริงจึงตัดทั้งกลุ่มอักษร */
  line-break: auto;
}

.keep { display: inline-block; max-width: 100%; }

.sr-only {
  position: absolute;
  width: 1px; height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.fatal { padding: 3rem 1.5rem; text-align: center; }

/* ============ หน้าจอ ============ */
#app {
  position: relative;
  width: 100%;
  height: 100vh;
  height: 100dvh;
}

.screen {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  padding:
    calc(var(--safe-t) + 12px)
    calc(var(--safe-r) + 16px)
    calc(var(--safe-b) + 14px)
    calc(var(--safe-l) + 16px);
  opacity: 0;
  transition: opacity .6s ease;
}
.screen.is-active { opacity: 1; }

/* ============ ปุ่ม ============ */
button {
  font: inherit;
  color: inherit;
  background: none;
  border: 0;
  cursor: pointer;
  touch-action: manipulation;
}
button:focus-visible, .lines:focus-visible {
  outline: 3px solid var(--wine);
  outline-offset: 3px;
}

.btn {
  min-height: 48px;
  padding: 10px 22px;
  border-radius: 999px;
  border: 1.5px solid var(--wine);
  font-size: 1rem;
  font-weight: 500;
  line-height: 1.4;
  transition: transform .15s ease, background-color .2s ease, opacity .2s ease;
}
.btn:active { transform: scale(.97); }
.btn-primary { background: var(--wine); color: var(--paper); box-shadow: 0 6px 16px rgba(122, 38, 56, .25); }
.btn-primary:hover { background: var(--wine-dark); }
.btn-ghost { background: rgba(255, 250, 242, .6); color: var(--wine); }
.btn-ghost:hover { background: var(--paper); }
.btn:disabled { opacity: .35; cursor: default; }

.icon-btn {
  display: grid;
  place-items: center;
  width: 44px; height: 44px;
  border-radius: 50%;
  background: var(--paper);
  color: var(--wine);
  box-shadow: 0 2px 10px rgba(78, 40, 36, .18);
  flex: none;
}
.icon-btn svg {
  width: 22px; height: 22px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.sound-btn {
  position: fixed;
  z-index: 50;
  top: calc(var(--safe-t) + 10px);
  right: calc(var(--safe-r) + 12px);
}
.sound-btn .mute { display: none; }
.sound-btn.is-off .mute { display: inline; }
.sound-btn.is-off .wave { display: none; }

/* ============ 1) หน้าเปิด ============ */
#cover { overflow-y: auto; }
.cover-inner {
  margin: auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 18px;
  width: 100%;
  text-align: center;
}
.eyebrow { font-size: 1.15rem; font-weight: 500; color: var(--wine); }

.envelope {
  --w: min(86vw, 360px, 70vh);
  position: relative;
  display: block;
  width: var(--w);
  height: calc(var(--w) * .68);
  margin-top: calc(var(--w) * .12);
  perspective: 900px;
  border-radius: 10px;
  filter: drop-shadow(0 16px 22px rgba(78, 40, 36, .22));
  animation: float 5s ease-in-out infinite;
}
.envelope > span { position: absolute; display: block; }
.env-back { inset: 0; background: #b97f7e; border-radius: 10px; }
.env-letter {
  left: 7%; right: 7%; top: 8%; height: 80%;
  z-index: 1;
  background: var(--paper);
  border-radius: 4px;
  box-shadow: inset 0 0 0 6px var(--paper), inset 0 0 0 7px var(--rose-soft);
  transition: transform .9s cubic-bezier(.22, .8, .3, 1) .55s;
}
.env-letter svg { display: block; width: 18%; margin: 12% auto 0; fill: var(--wine); }
.env-front {
  inset: 0;
  z-index: 2;
  border-radius: 10px;
  background:
    linear-gradient(to top, #dcb0ab, #d4a4a1);
  clip-path: polygon(0 0, 50% 55%, 100% 0, 100% 100%, 0 100%);
}
.env-front::after {
  content: "";
  position: absolute;
  inset: 0;
  background: #e4bdb7;
  clip-path: polygon(0 100%, 50% 48%, 100% 100%);
}
.env-flap {
  left: 0; right: 0; top: 0; height: 60%;
  z-index: 3;
  background: linear-gradient(to bottom, #c98f8e, #d39d9b);
  clip-path: polygon(0 0, 100% 0, 52% 98%, 50% 100%, 48% 98%);
  transform-origin: top center;
  transition: transform .65s ease, z-index 0s linear .35s;
}
.env-seal {
  left: 50%; top: 56%;
  z-index: 4;
  width: 17%;
  aspect-ratio: 1;
  transform: translate(-50%, -50%);
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, #a2475a, var(--wine) 60%, var(--wine-dark));
  box-shadow: 0 3px 8px rgba(60, 10, 20, .35);
  transition: opacity .3s ease, transform .3s ease;
}
.env-seal svg { display: block; width: 52%; margin: 25% auto 0; fill: #f3d9d3; }
.env-name {
  left: 8%; right: 8%; bottom: 6%;
  z-index: 5;
  font-size: calc(var(--w) * .082);
  font-weight: 600;
  color: var(--wine-dark);
  line-height: 1.8;
  transition: opacity .3s ease;
}

.envelope.is-open { animation: none; }
.envelope.is-open .env-flap { transform: rotateX(180deg); z-index: 0; }
.envelope.is-open .env-seal { opacity: 0; transform: translate(-50%, -50%) scale(.5); }
.envelope.is-open .env-letter { transform: translateY(-48%); }

.cover-from { color: var(--muted); font-size: .98rem; }

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

/* ============ 2) หน้าข้อความ ============ */
#story { cursor: pointer; user-select: none; -webkit-user-select: none; }
.top-bar { flex: none; padding-right: 54px; min-height: 52px; }
.progress { display: flex; gap: 5px; padding-top: 6px; }
.progress span {
  height: 4px;
  border-radius: 4px;
  background: rgba(122, 38, 56, .16);
  overflow: hidden;
}
.progress i {
  display: block;
  height: 100%;
  width: 100%;
  background: var(--wine);
  transform-origin: left;
  transform: scaleX(0);
  transition: transform .4s ease;
}
.chapter-label { margin-top: 5px; font-size: .86rem; color: var(--muted); }
.chapter-label #chapterNum { color: var(--wine); font-weight: 600; margin-right: .6em; }

.story-body {
  flex: 1;
  min-height: 0;
  display: flex;
  flex-direction: column;
  gap: 10px;
  width: 100%;
  max-width: 560px;
  margin: 0 auto;
}

.photo-stage {
  position: relative;
  flex: 0 0 46%;
  min-height: 0;
  container-type: size;
}
.polaroid {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  opacity: 0;
  transition: opacity .8s ease;
}
.polaroid.is-shown { opacity: 1; }
.polaroid-card {
  position: relative;
  padding: 10px 10px 36px;
  background: #fffdf8;
  border-radius: 3px;
  box-shadow: var(--shadow);
  transform: rotate(-1.6deg);
}
.polaroid:nth-of-type(even) .polaroid-card { transform: rotate(1.4deg); }
.polaroid-card::before { /* เทปกาว */
  content: "";
  position: absolute;
  top: -9px; left: 50%;
  width: 78px; height: 21px;
  transform: translateX(-50%) rotate(-3deg);
  background: rgba(207, 156, 154, .55);
  z-index: 1;
}
.polaroid-card img {
  display: block;
  width: auto; height: auto;
  max-width: 78vw; max-height: 34vh;
  max-width: calc(100cqw - 34px);
  max-height: calc(100cqh - 66px);
}
.polaroid-card figcaption {
  position: absolute;
  left: 8px; right: 8px; bottom: 5px;
  text-align: center;
  font-size: .82rem;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.placeholder {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  padding: 8px;
  overflow: hidden;
  text-align: center;
  font-size: .85rem;
  color: var(--muted);
  background: repeating-linear-gradient(45deg, #f3e2dc 0 12px, #f6e9e2 12px 24px);
}
.placeholder svg { width: 36px; height: 36px; fill: var(--rose); flex: none; }
.placeholder small { font-size: .72rem; opacity: .75; max-width: 100%; overflow-wrap: anywhere; }
.polaroid-card .placeholder {
  width: 250px; height: 188px;
  width: min(calc(100cqw - 34px), calc((100cqh - 66px) * 4 / 3), 420px);
  height: auto;
  aspect-ratio: 4 / 3;
}

.lines {
  position: relative;
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  overscroll-behavior: contain;
  scroll-behavior: smooth;
  padding: 18px 6px;
  text-align: center;
  -webkit-mask-image: linear-gradient(to bottom, transparent, #000 18px);
  mask-image: linear-gradient(to bottom, transparent, #000 18px);
  -webkit-overflow-scrolling: touch;
}
.line {
  font-size: clamp(1.08rem, 4.6vw, 1.25rem);
  line-height: 2.05;
  margin-bottom: .35em;
  animation: rise .65s ease both;
}
.line.no-anim { animation: none; }

@keyframes rise {
  from { opacity: 0; transform: translateY(14px); }
  to { opacity: 1; transform: none; }
}

.bottom-bar {
  flex: none;
  display: flex;
  justify-content: center;
  gap: 12px;
  width: 100%;
  max-width: 560px;
  margin: 0 auto;
  padding-top: 8px;
}
.bottom-bar .btn-primary { flex: 1; max-width: 300px; }

#endHeading { font-size: 1.25rem; font-weight: 600; color: var(--wine); line-height: 1.9; }
#endHeading:focus { outline: none; }

/* ============ 3) หน้าจบ ============ */
#end { padding: 0; overflow: hidden; }
.end-scroll {
  position: relative;
  z-index: 1;
  height: 100%;
  overflow-y: auto;
  overscroll-behavior: contain;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  text-align: center;
  padding:
    calc(var(--safe-t) + 14px)
    calc(var(--safe-r) + 16px)
    calc(var(--safe-b) + 28px)
    calc(var(--safe-l) + 16px);
}
.end-scroll > * { flex: none; }
#endHeading { padding: 0 48px; }

.collage {
  display: flex;
  flex-wrap: wrap;
  align-content: center;
  justify-content: center;
  gap: 6px;
  width: 100%;
  max-width: 820px;
}
.tile {
  background: #fff;
  padding: 3px;
  box-shadow: 0 3px 10px rgba(78, 40, 36, .2);
  transform: rotate(var(--tilt, 0deg));
}
.tile img { display: block; width: 100%; height: 100%; object-fit: contain; }
.tile.placeholder { font-size: .7rem; }

.closing { width: 100%; max-width: 560px; }
.closing p {
  font-size: clamp(1.2rem, 5.4vw, 1.5rem);
  font-weight: 600;
  color: var(--wine-dark);
  line-height: 2.1;
  opacity: 0;
  transform: translateY(14px);
  transition: opacity .8s ease, transform .8s ease;
}
.closing p.is-shown { opacity: 1; transform: none; }

.end-extra { display: flex; flex-direction: column; align-items: center; gap: 6px; }
.counter { font-size: 1.1rem; }
.counter strong { font-size: 2.2rem; font-weight: 600; color: var(--wine); padding: 0 .1em; font-variant-numeric: tabular-nums; }
.since { font-size: .82rem; color: var(--muted); }
.end-actions { display: flex; flex-wrap: wrap; justify-content: center; gap: 10px; margin-top: 12px; }

.hearts { position: absolute; inset: 0; overflow: hidden; pointer-events: none; z-index: 0; }
.hearts svg {
  position: absolute;
  bottom: -40px;
  fill: var(--rose);
  opacity: 0;
  animation: drift linear infinite;
}
.hearts svg:nth-child(3n) { fill: var(--wine); }
@keyframes drift {
  0% { transform: translate(0, 0) rotate(-10deg); opacity: 0; }
  12% { opacity: .55; }
  75% { opacity: .35; }
  100% { transform: translate(var(--dx, 20px), -110vh) rotate(18deg); opacity: 0; }
}

/* ============ ข้อความแจ้งเตือน ============ */
.toast {
  position: fixed;
  z-index: 60;
  top: calc(var(--safe-t) + 62px);
  right: calc(var(--safe-r) + 12px);
  padding: 6px 14px;
  border-radius: 999px;
  background: var(--ink);
  color: var(--paper);
  font-size: .84rem;
  opacity: 0;
  pointer-events: none;
  transition: opacity .3s ease;
}
.toast.is-shown { opacity: .95; }

/* ============ จอกว้าง / คอมพิวเตอร์ ============ */
@media (min-width: 860px) {
  .story-body { flex-direction: row; align-items: stretch; max-width: 1060px; gap: 40px; }
  .photo-stage { flex: 0 0 52%; }
  .lines { display: flex; flex-direction: column; }
  .lines > .line:first-child { margin-top: auto; }
  .lines > .line:last-child { margin-bottom: auto; }
  .line { font-size: 1.3rem; }
  .polaroid-card { padding: 14px 14px 44px; }
  .polaroid-card img { max-width: calc(100cqw - 48px); max-height: calc(100cqh - 90px); }
  .polaroid-card .placeholder { width: min(calc(100cqw - 48px), calc((100cqh - 90px) * 4 / 3), 460px); }
  .polaroid-card figcaption { bottom: 9px; font-size: .95rem; }
}

/* มือถือแนวนอน */
@media (orientation: landscape) and (max-height: 560px) {
  .story-body { flex-direction: row; max-width: none; gap: 16px; }
  .photo-stage { flex: 0 0 46%; }
  .top-bar { min-height: 44px; }
  .bottom-bar { padding-top: 4px; }
  .btn { min-height: 42px; padding: 6px 20px; }
  .cover-inner { gap: 6px; }
  .envelope { --w: min(60vw, 280px, 68vh); }
}

/* ============ ลดการเคลื่อนไหว ============ */
@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after {
    animation: none !important;
    transition: none !important;
    scroll-behavior: auto !important;
  }
  .hearts { display: none; }
}
