/* =========================================================
   Back to School — Prototype styles
   ธีมชั่วคราว (รอดีไซน์จริง): สีหลักเดียว = เหลืองอำพันจากปากเป็ด
   เปลี่ยนสีทั้งเว็บได้ที่ตัวแปรใน :root ด้านล่าง
   ========================================================= */

/* ---------- ฟอนต์ Kanit (โหลดจากในเว็บเอง ไม่พึ่ง Google Fonts) ---------- */
@font-face { font-family: 'Kanit'; font-style: normal; font-display: swap; font-weight: 300; src: url(../assets/fonts/kanit-thai-300-normal.woff2) format('woff2'); unicode-range: U+02D7,U+0303,U+0331,U+0E01-0E5B,U+200C-200D,U+25CC; }
@font-face { font-family: 'Kanit'; font-style: normal; font-display: swap; font-weight: 400; src: url(../assets/fonts/kanit-thai-400-normal.woff2) format('woff2'); unicode-range: U+02D7,U+0303,U+0331,U+0E01-0E5B,U+200C-200D,U+25CC; }
@font-face { font-family: 'Kanit'; font-style: normal; font-display: swap; font-weight: 500; src: url(../assets/fonts/kanit-thai-500-normal.woff2) format('woff2'); unicode-range: U+02D7,U+0303,U+0331,U+0E01-0E5B,U+200C-200D,U+25CC; }
@font-face { font-family: 'Kanit'; font-style: normal; font-display: swap; font-weight: 700; src: url(../assets/fonts/kanit-thai-700-normal.woff2) format('woff2'); unicode-range: U+02D7,U+0303,U+0331,U+0E01-0E5B,U+200C-200D,U+25CC; }
@font-face { font-family: 'Kanit'; font-style: normal; font-display: swap; font-weight: 300; src: url(../assets/fonts/kanit-latin-300-normal.woff2) format('woff2'); unicode-range: U+0000-00FF,U+0131,U+0152-0153,U+02BB-02BC,U+02C6,U+02DA,U+02DC,U+0304,U+0308,U+0329,U+2000-206F,U+20AC,U+2122,U+2191,U+2193,U+2212,U+2215,U+FEFF,U+FFFD; }
@font-face { font-family: 'Kanit'; font-style: normal; font-display: swap; font-weight: 400; src: url(../assets/fonts/kanit-latin-400-normal.woff2) format('woff2'); unicode-range: U+0000-00FF,U+0131,U+0152-0153,U+02BB-02BC,U+02C6,U+02DA,U+02DC,U+0304,U+0308,U+0329,U+2000-206F,U+20AC,U+2122,U+2191,U+2193,U+2212,U+2215,U+FEFF,U+FFFD; }
@font-face { font-family: 'Kanit'; font-style: normal; font-display: swap; font-weight: 500; src: url(../assets/fonts/kanit-latin-500-normal.woff2) format('woff2'); unicode-range: U+0000-00FF,U+0131,U+0152-0153,U+02BB-02BC,U+02C6,U+02DA,U+02DC,U+0304,U+0308,U+0329,U+2000-206F,U+20AC,U+2122,U+2191,U+2193,U+2212,U+2215,U+FEFF,U+FFFD; }
@font-face { font-family: 'Kanit'; font-style: normal; font-display: swap; font-weight: 700; src: url(../assets/fonts/kanit-latin-700-normal.woff2) format('woff2'); unicode-range: U+0000-00FF,U+0131,U+0152-0153,U+02BB-02BC,U+02C6,U+02DA,U+02DC,U+0304,U+0308,U+0329,U+2000-206F,U+20AC,U+2122,U+2191,U+2193,U+2212,U+2215,U+FEFF,U+FFFD; }

/* ---------- ตัวแปรสีและจังหวะ ---------- */
:root {
  --accent: #F2B544;          /* เหลืองอำพัน (ปากเป็ด) — สีหลักของทั้งเว็บ */
  --accent-strong: #E09A22;   /* อำพันเข้ม ใช้ตอนกด/hover */
  --accent-ink: #2A1A12;      /* ตัวอักษรบนพื้นอำพัน */
  --accent-soft: rgba(242, 181, 68, 0.18);

  --text: #FFF8EC;            /* ตัวอักษรบนภาพพื้นหลัง */
  --text-dim: rgba(255, 248, 236, 0.72);
  --warn: #FFB4A2;

  --paper: #FBF3E6;           /* พื้นการ์ดสมุดพก */
  --paper-line: #E9DCC6;
  --ink: #2A1A12;             /* ตัวอักษรบนการ์ด */
  --ink-soft: #6B5446;
  --night: #1C2447;           /* น้ำเงินกลางคืนจากฉากฝน/ดวงดาว */

  --overlay: rgba(12, 10, 22, 0.50);        /* ชั้นดำโปร่งทับภาพพื้นหลัง */
  --overlay-strong: rgba(12, 10, 22, 0.66); /* หน้าผลลัพธ์ */

  --glass: rgba(20, 16, 30, 0.42);
  --glass-line: rgba(255, 248, 236, 0.55);

  /* จังหวะ (ค่าที่ต้องตรงกับ CFG ใน js/app.js มีหมายเหตุกำกับ) */
  --black-fade: 1000ms;         /* เปลี่ยนฉาก: จางดำออก/เข้า (≈ CFG.blackOut / blackIn) */
  --dim-fade: 1600ms;          /* ชั้นดำโปร่งค่อย ๆ จางเข้าหลังเปลี่ยนฉาก */
  --text-in: 1800ms;           /* ข้อความแต่ละบรรทัดจางเข้า (1–2 วิ) */
  --text-out: 700ms;           /* ข้อความเก่าจางออก (= CFG.textOut) */
  --stagger: 220ms;            /* บรรทัดถัดไปเริ่มช้ากว่ากันเท่านี้ */
  --bg-fade: 900ms;            /* เปลี่ยนเบลอ/ไม่เบลอในภาพเดิม */

  --radius: 18px;
  --app-w: 450px;
  --app-h: 900px;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  background: #0b0a10;
  color: var(--text);
  font-family: 'Kanit', system-ui, sans-serif;
  font-weight: 400;
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

body { display: flex; align-items: center; justify-content: center; overflow: hidden; }

button, input { font: inherit; color: inherit; touch-action: manipulation; }
button { cursor: pointer; border: none; background: none; }
[hidden] { display: none !important; }

/* ---------- กรอบแอป: แนวตั้งเสมอ ---------- */
#app {
  position: relative;
  width: 100%;
  max-width: var(--app-w);
  height: 100vh;
  height: 100dvh;
  max-height: var(--app-h);
  overflow: hidden;
  background: var(--night);
  box-shadow: 0 0 40px rgba(0,0,0,.6);
  isolation: isolate;
}
@media (min-width: 500px) and (min-height: 920px) { #app { border-radius: 24px; } }

/* ---------- ภาพพื้นหลัง ---------- */
.bg-layer {
  position: absolute; inset: 0;
  background: center / cover no-repeat;
  transition: filter var(--bg-fade) ease, transform var(--bg-fade) ease;
  z-index: 0;
}
.bg-layer.no-anim { transition: none; }
.bg-layer.blur { filter: blur(9px) saturate(1.05); transform: scale(1.08); }

.bg-overlay {
  position: absolute; inset: 0; z-index: 1; pointer-events: none;
  background:
    linear-gradient(to bottom, rgba(12,10,22,.45) 0%, rgba(12,10,22,.15) 30%, transparent 45%, transparent 70%, rgba(12,10,22,.55) 100%),
    var(--overlay);
  transition: opacity var(--dim-fade) ease, background-color var(--bg-fade) ease;
}
.bg-overlay.strong { background-color: var(--overlay-strong); }
.bg-overlay.off { opacity: 0; transition: none; }   /* ภาพใหม่ยังไม่ถูกย้อมดำ */

/* ชั้นดำทึบสำหรับเปลี่ยนฉาก (ปุ่มล่างจอ/ธง/เสียง อยู่เหนือชั้นนี้ จึงไม่จางตาม) */
.blackout { position: absolute; inset: 0; z-index: 20; background: #000; opacity: 0; pointer-events: none; transition: opacity var(--black-fade) ease; }
.blackout.on { opacity: 1; }

/* ---------- ปุ่มธงเปลี่ยนภาษา ---------- */
.lang-switch {
  position: absolute; top: calc(14px + env(safe-area-inset-top, 0)); left: 14px; z-index: 40;
  display: flex; gap: 6px; padding: 5px; border-radius: 999px;
  background: var(--glass); border: 1px solid rgba(255,248,236,.28);
  backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px);
}
.lang-switch button {
  width: 34px; height: 34px; border-radius: 50%; overflow: hidden; padding: 0;
  opacity: .45; filter: grayscale(.4); border: 2px solid transparent;
  transition: opacity .2s ease, filter .2s ease, border-color .2s ease, transform .15s ease;
}
.lang-switch button svg { width: 100%; height: 100%; display: block; transform: scale(1.5); }
.lang-switch button[aria-pressed="true"] { opacity: 1; filter: none; border-color: var(--accent); }
.lang-switch button:active { transform: scale(.92); }

/* ---------- แถบความคืบหน้า ---------- */
.progress {
  position: absolute; top: env(safe-area-inset-top, 0); left: 0; right: 0; height: 4px;
  background: rgba(255,248,236,.16); z-index: 30;
}
.progress-fill {
  height: 100%; width: 0; background: var(--accent);
  border-radius: 0 4px 4px 0;
  transition: width 700ms cubic-bezier(.3,.7,.3,1);
}

/* ---------- ปุ่มเสียง ---------- */
.audio-ctrl { position: absolute; top: calc(14px + env(safe-area-inset-top, 0)); right: 14px; z-index: 40; display: flex; flex-direction: column; align-items: flex-end; gap: 8px; }
.audio-btn {
  width: 44px; height: 44px; border-radius: 50%;
  background: var(--glass); border: 1.5px solid var(--glass-line);
  display: grid; place-items: center; backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px);
}
.audio-btn svg { width: 22px; height: 22px; fill: none; stroke: var(--text); stroke-width: 1.8; stroke-linecap: round; stroke-linejoin: round; }
.audio-btn .spk { fill: var(--text); }
.audio-btn .cross { display: none; }
.audio-ctrl.muted .wave { display: none; }
.audio-ctrl.muted .cross { display: block; }
.audio-ctrl.muted .audio-btn { border-color: var(--accent); }
.audio-panel {
  width: 190px; padding: 14px; border-radius: 14px;
  background: rgba(20,16,30,.82); border: 1px solid var(--glass-line);
  backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
  display: flex; flex-direction: column; gap: 10px;
}
.audio-label { font-size: .85rem; color: var(--text-dim); }
.audio-mute { font-size: .9rem; padding: 6px 10px; border-radius: 999px; border: 1px solid var(--glass-line); }
.audio-mute[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
input[type="range"] { -webkit-appearance: none; appearance: none; width: 100%; height: 28px; background: transparent; }
input[type="range"]::-webkit-slider-runnable-track { height: 6px; border-radius: 6px; background: linear-gradient(to right, var(--accent) var(--fill, 60%), rgba(255,248,236,.25) var(--fill, 60%)); }
input[type="range"]::-webkit-slider-thumb { -webkit-appearance: none; width: 22px; height: 22px; margin-top: -8px; border-radius: 50%; background: var(--text); border: 3px solid var(--accent); }
input[type="range"]::-moz-range-track { height: 6px; border-radius: 6px; background: rgba(255,248,236,.25); }
input[type="range"]::-moz-range-progress { height: 6px; border-radius: 6px; background: var(--accent); }
input[type="range"]::-moz-range-thumb { width: 18px; height: 18px; border-radius: 50%; background: var(--text); border: 3px solid var(--accent); }

/* ---------- เวที + หน้า ---------- */
.stage { position: absolute; inset: 0; z-index: 10; }
.page {
  position: absolute; inset: 0;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: calc(70px + env(safe-area-inset-top, 0)) 28px calc(96px + env(safe-area-inset-bottom, 0));
  text-align: center;
}
.page.leaving { pointer-events: none; }
/* หน้าบรรยาย: ข้อความชิดด้านบน เปิดพื้นที่กลางจอให้เห็นฉาก */
.page.top { justify-content: flex-start; padding-top: calc(84px + env(safe-area-inset-top, 0)); }
.page.scroll { justify-content: flex-start; overflow-y: auto; overscroll-behavior: contain; -webkit-overflow-scrolling: touch; padding-bottom: calc(40px + env(safe-area-inset-bottom, 0)); }

.content { width: 100%; display: flex; flex-direction: column; align-items: center; }
/* ข้อความทุกบรรทัดค่อย ๆ จางเข้าทีละบรรทัด */
.content > :not(.result-wrap), .result-wrap > * {
  opacity: 0;                                   /* จางเข้าอย่างเดียว ไม่เลื่อนตำแหน่ง */
  transition: opacity var(--text-in) ease;
  transition-delay: calc(var(--i, 0) * var(--stagger));
}
.content.visible > :not(.result-wrap), .content.visible .result-wrap > * { opacity: 1; }
.page.leaving .content > *, .page.leaving .result-wrap > * { opacity: 0 !important; transition: opacity var(--text-out) ease; transition-delay: 0s; }
.page.swap .content > :not(.result-wrap), .page.swap .result-wrap > * { transition-duration: .45s; transition-delay: 0s; }

/* ข้อความเรื่องเล่า */
.story-text { font-size: 1.28rem; line-height: 1.7; font-weight: 400; text-wrap: balance; text-shadow: 0 2px 12px rgba(0,0,0,.55); }
.story-text + .story-text { margin-top: 18px; }
.announce {
  font-size: 1rem; line-height: 1.6; padding: 12px 18px; margin-bottom: 22px;
  border-radius: 14px; background: var(--glass); border: 1px solid var(--accent);
  backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px);
}
.announce strong, .loved strong { color: var(--accent); font-weight: 500; }
.loved { font-weight: 500; font-size: 1.2rem; }

.title { font-size: 2.3rem; font-weight: 700; line-height: 1.2; margin-bottom: 10px; letter-spacing: .01em; text-shadow: 0 3px 16px rgba(0,0,0,.5); }
.title .accent { color: var(--accent); }
.lead { font-size: 1.05rem; color: var(--text); margin-bottom: 22px; text-shadow: 0 2px 10px rgba(0,0,0,.5); }
.small { font-size: .85rem; line-height: 1.55; color: var(--text-dim); margin-top: 14px; }
.small.warn { color: var(--warn); font-weight: 500; }
.hint { font-size: .92rem; color: var(--text-dim); margin: 6px 0 20px; }

/* ---------- ปุ่ม ---------- */
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px;
  min-height: 52px; padding: 12px 34px; border-radius: 999px;
  font-size: 1.1rem; font-weight: 500;
  transition: transform .15s ease, background-color .2s ease, opacity .3s ease, box-shadow .2s ease;
}
.btn:active { transform: scale(.97); }
.btn-primary { background: var(--accent); color: var(--accent-ink); box-shadow: 0 6px 20px rgba(242,181,68,.28); min-width: 70%; margin-top: 22px; }
.btn-primary:hover { background: var(--accent-strong); }
.btn-ghost { border: 1.5px solid var(--glass-line); background: var(--glass); color: var(--text); backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px); }
.btn[disabled], .btn.is-loading { opacity: .38; pointer-events: none; box-shadow: none; }

/* หน้าเลือกภาษา */
.lang-buttons { display: flex; gap: 14px; width: 100%; justify-content: center; margin: 26px 0 18px; }
.lang-buttons .btn { flex: 1; max-width: 170px; margin: 0; min-width: 0; }
.loadbar { width: 70%; height: 4px; border-radius: 4px; background: rgba(255,248,236,.18); overflow: hidden; }
.loadbar i { display: block; height: 100%; width: 0; background: var(--accent); transition: width .3s ease; }
.load-text { font-size: .85rem; color: var(--text-dim); margin-top: 10px; line-height: 1.5; min-height: 2.6em; }

/* หน้าใส่ชื่อ */
.field {
  width: 100%; padding: 14px 18px; border-radius: 14px;
  background: rgba(255,248,236,.94); color: var(--ink);
  border: 2px solid transparent; outline: none;
  font-size: 1.1rem; text-align: center;
}
.field::placeholder { color: #9a8a7e; }
.field:focus { border-color: var(--accent); box-shadow: 0 0 0 4px var(--accent-soft); }
.choice-row { display: flex; gap: 12px; width: 100%; margin-top: 14px; }
.choice {
  flex: 1; min-height: 48px; border-radius: 999px;
  border: 1.5px solid var(--glass-line); background: var(--glass);
  backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px);
  font-size: 1rem; transition: all .2s ease;
}
.choice[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 500; }
.lang-chip { margin-top: 16px; font-size: .88rem; color: var(--text-dim); text-decoration: underline; text-underline-offset: 3px; }

/* หน้าคำถาม */
.question { font-size: 1.3rem; line-height: 1.55; font-weight: 500; text-shadow: 0 2px 10px rgba(0,0,0,.5); }
.options { width: 100%; display: flex; flex-direction: column; gap: 10px; }
.option {
  width: 100%; text-align: left; padding: 14px 18px; border-radius: 16px;
  background: var(--glass); border: 1.5px solid var(--glass-line); line-height: 1.5; font-size: .98rem;
  backdrop-filter: blur(5px); -webkit-backdrop-filter: blur(5px);
  transition: all .2s ease;
}
.option[aria-pressed="true"] { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); font-weight: 500; }

/* ---------- ปุ่มล่างจอ ---------- */
.footer-nav {
  position: absolute; left: 0; right: 0; bottom: calc(16px + env(safe-area-inset-bottom, 0));
  padding: 0 18px; display: flex; justify-content: space-between; z-index: 35; pointer-events: none;
}
.nav-btn {
  pointer-events: auto; min-height: 44px; padding: 8px 16px; border-radius: 999px;
  font-size: 1rem; font-weight: 500; color: var(--text);
  background: var(--glass); border: 1px solid rgba(255,248,236,.28);
  backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px);
}
.nav-btn::before { content: '‹ '; }
.nav-next { margin-left: auto; background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
.nav-next::before { content: none; }
.nav-next::after { content: ' ›'; }
.nav-btn.wait { opacity: .35; }

/* ---------- หน้าผลลัพธ์ ---------- */
.result-wrap { width: 100%; display: flex; flex-direction: column; align-items: center; gap: 16px; }
.result-image-box {
  width: 100%; aspect-ratio: 1080 / 1920; border-radius: var(--radius); overflow: hidden;
  background: var(--paper); box-shadow: 0 12px 40px rgba(0,0,0,.45);
  display: grid; place-items: center; color: var(--ink-soft);
}
.result-image-box img { width: 100%; height: 100%; display: block; -webkit-touch-callout: default; user-select: auto; }
.result-actions { width: 100%; display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.result-actions .btn { margin: 0; min-width: 0; padding: 12px 16px; font-size: 1rem; }
.result-actions .btn-primary { grid-column: 1 / -1; }
.scroll-hint { font-size: .95rem; color: var(--text); margin: 14px 0 4px; }

.report-card {
  position: relative; width: 100%; text-align: left;
  background: var(--paper); color: var(--ink);
  border-radius: var(--radius); padding: 18px 18px 16px;
  box-shadow: 0 8px 26px rgba(0,0,0,.35);
  overflow: hidden;
}
.report-card::before { content: ''; position: absolute; left: 0; top: 0; bottom: 0; width: 6px; background: var(--accent); }
.report-card.grow::before { background: var(--night); }
.rc-top { display: flex; justify-content: space-between; align-items: center; gap: 10px; }
.badge { font-size: .78rem; font-weight: 500; padding: 4px 10px; border-radius: 999px; background: var(--accent); color: var(--accent-ink); }
.report-card.grow .badge { background: var(--night); color: var(--text); }
.rc-code { font-size: .85rem; color: var(--ink-soft); font-weight: 500; letter-spacing: .03em; }
.rc-subject { font-size: 1.25rem; font-weight: 700; margin: 10px 0 8px; line-height: 1.3; }
.rc-comment { font-size: .98rem; line-height: 1.65; color: var(--ink); border-top: 1.5px dashed var(--paper-line); padding-top: 10px; }
.rc-teacher { display: flex; align-items: flex-end; justify-content: flex-end; gap: 10px; margin-top: 10px; text-align: right; }
.rc-teacher img { width: 92px; height: auto; margin-bottom: -16px; filter: drop-shadow(0 4px 6px rgba(0,0,0,.15)); }
.rc-teacher strong { display: block; font-size: .98rem; font-weight: 700; }
.rc-teacher span { display: block; font-size: .8rem; color: var(--ink-soft); padding-bottom: 6px; }
.play-again { margin-top: 10px; }

/* ---------- toast + overlay ---------- */
.toast {
  position: absolute; left: 50%; bottom: calc(84px + env(safe-area-inset-bottom, 0)); transform: translateX(-50%);
  z-index: 60; max-width: 86%; padding: 10px 18px; border-radius: 999px;
  background: rgba(20,16,30,.92); border: 1px solid var(--accent); font-size: .92rem; text-align: center;
  animation: toast-in .25s ease;
}
@keyframes toast-in { from { opacity: 0; transform: translate(-50%, 8px); } }

.overlay { position: absolute; inset: 0; z-index: 70; background: rgba(8,6,14,.92); display: grid; place-items: center; padding: 24px; }
.overlay-inner { position: relative; width: 100%; display: flex; flex-direction: column; align-items: center; gap: 14px; }
.overlay-inner img { width: 82%; border-radius: 14px; -webkit-touch-callout: default; }
.overlay-inner p { font-size: 1rem; text-align: center; }
.overlay-inner .btn { position: absolute; top: -10px; right: 0; min-height: 44px; padding: 0 16px; }

@media (prefers-reduced-motion: reduce) {
  :root { --text-in: 300ms; --stagger: 0ms; --bg-fade: 1ms; }
}
