/* ============================================================================
 * app.css — สไตล์ทั้งแอป (รวมจาก style.css + journeystyle.css + resultstyle.css เดิม)
 *
 * หลักการ: เวทีล็อกสัดส่วน 9:16 (มือถือ = เต็มจอ / จอใหญ่ = อยู่กลางจอ)
 *          ขนาดตัวอักษร/ระยะทั้งหมดเป็น rem และสเกลตามความกว้างเวที (ดีไซน์อ้างอิงกว้าง 390px = 16px)
 * ========================================================================== */
:root {
  --app-h: 100vh;
  --w: min(100vw, calc(var(--app-h) * 9 / 16));
  font-size: clamp(12px, calc(var(--w) * 16 / 390), 19px);

  --padX: 1rem;
  --padY: 1.25rem;
  --text: #fff;
  --muted: #d1d1d1;
  --chip: #ddd;
  --btn-bg: #fff;
  --btn-fg: #111;
  --btn-radius: 16px;
  --font: 'Kanit', system-ui, -apple-system, 'Segoe UI', 'Noto Sans Thai', sans-serif;
}
@supports (height: 100dvh) { :root { --app-h: 100dvh; } }

*, *::before, *::after { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }

html, body {
  margin: 0; height: 100%; background: #000; overflow: hidden;
  overscroll-behavior: none; touch-action: manipulation;
  -webkit-text-size-adjust: 100%; text-size-adjust: 100%;
}
body {
  position: fixed; inset: 0;
  color: var(--text); font-family: var(--font); font-size: 1rem;
  -webkit-user-select: none; user-select: none; -webkit-touch-callout: none;
  -webkit-font-smoothing: antialiased;
}
button { font-family: inherit; }
p { margin: 0; line-height: 1.6; }

#app { position: fixed; inset: 0; display: flex; justify-content: center; background: #000; }
#frame {
  position: relative; width: var(--w); height: var(--app-h);
  overflow: hidden; background: #000; isolation: isolate;
}

/* ---------------------------------------------------------------- เอฟเฟกต์โผล่ข้อความ/ปุ่ม (เดิม: .hidden + .fade-in 2s) */
@keyframes fadeIn { from { opacity: 0; } to { opacity: 1; } }
@keyframes fadeOutKf { from { opacity: 1; } to { opacity: 0; } }
.rv { opacity: 0; visibility: hidden; }
.rv.on { opacity: 1; visibility: visible; animation: fadeIn var(--fd, 1.6s) ease forwards; }
.scene.rv.on { --fd: 1.6s; }
#bg.rv.on { --fd: 1.2s; }                                              /* ภาพเฟดเข้าจากสีดำ */
.q.rv.on, .nextbtn.rv.on { --fd: 1.1s; }                              /* ตัวเลือก/ปุ่ม: เร็วขึ้นเล็กน้อยให้กดได้ไว */
.fx { animation: fadeIn 1.6s ease forwards; }
html.fast .rv.on, html.fast .fx { animation: none; }
/* เฟดออกตอนเปลี่ยนฉาก */
#view.leaving { opacity: 0; transition: opacity .5s ease; pointer-events: none; }
#bg.out { opacity: 0; transition: opacity .55s ease; }

/* ---------------------------------------------------------------- ชั้นภาพ */
#bg { position: absolute; inset: 0; z-index: 1; background: #000; }
.bgl { position: absolute; inset: 0; background-position: center; background-repeat: no-repeat; background-size: cover; }
#ov { position: absolute; inset: 0; z-index: 2; background: #000; opacity: 0; pointer-events: none; }   /* โอเวอร์เลย์ดำ (ชั้นเดียว ปรับ opacity ตามฉาก) */
#view { position: absolute; inset: 0; z-index: 3; }

/* ---------------------------------------------------------------- หน้าแรก (เดิม: style.css) */
.screen { position: absolute; inset: 0; z-index: 10; background: #000; }
.layer { position: absolute; inset: 0; background-position: center; background-repeat: no-repeat; background-size: cover; pointer-events: none; }
.layer--bg { background-image: url('../assets/img/cover.jpg'); z-index: 0; opacity: 0; transition: opacity .9s ease; }
.cover-ready .layer--bg { opacity: 1; }
.layer--overlay { background-color: #000; opacity: .65; z-index: 1; }
#start-content {
  position: relative; z-index: 2; width: 100%; height: 100%;
  display: grid; place-items: center; padding: var(--padY) var(--padX);
  overflow-y: auto; overflow-x: hidden; scrollbar-width: none;
}
#start-content::-webkit-scrollbar { display: none; }
.center { text-align: center; }
.stack > * + * { margin-top: 14px; }
#start .center.stack { width: 100%; }
h1 { font-size: 1.25rem; margin: 0; line-height: 1.4; }
.tiny { font-size: .75rem; opacity: .9; }
.text-muted { color: var(--muted); }
.field { width: min(92%, 26rem); margin: 10px auto 0; }
label { display: block; text-align: left; }
input[type="text"] {
  width: 100%; margin-top: 6px; padding: 12px 14px; border-radius: 12px;
  border: 1px solid rgba(255,255,255,.15); background: rgba(0,0,0,.35); color: #fff; outline: none;
  font-family: inherit; font-size: 16px; line-height: 1.3;     /* ต้องเป็น ≥16px เสมอ ไม่งั้น iOS ซูมจอตอนโฟกัส */
  touch-action: manipulation; transform: translateZ(0);
  -webkit-user-select: text; user-select: text; -webkit-appearance: none; appearance: none;
}
input::placeholder { color: rgba(255,255,255,.6); }
input:focus { border-color: rgba(255,255,255,.4); }
.btn {
  width: 100%; padding: 12px 16px; border: 0; background: var(--btn-bg); color: var(--btn-fg);
  border-radius: var(--btn-radius); cursor: pointer; font-size: 1rem;
}
.btn:active { transform: translateY(1px); }
.error { min-height: 1.4em; color: #ffb3b3; font-size: .9rem; }
.fi-start { animation: fadeInUp .8s ease both; }
.fi-slow { animation: fadeInUp 1.4s ease both; }
@keyframes fadeInUp { from { opacity: 0; transform: translateY(6px); } to { opacity: 1; transform: translateY(0); } }
.kanit-light { font-weight: 300; }
.kanit-medium { font-weight: 500; }
.kanit-semibold { font-weight: 700; }

/* ---------------------------------------------------------------- Loader (Phase 1 / 2) */
.loadingscene {
  position: absolute; inset: 0; z-index: 20; background: #15193B; color: #fff; text-align: center;
  display: flex; justify-content: center; align-items: center;
}
.loader-wrap { display: flex; flex-direction: column; align-items: center; gap: 14px; width: min(78%, 26rem); }
.loader-title { font-weight: 700; font-size: 1.62rem; }
.progress { width: 100%; height: 10px; border-radius: 999px; background: rgba(255,255,255,.2); overflow: hidden; }
.progress .bar { height: 100%; width: 0; background: #fff; border-radius: 999px 0 0 999px; transition: width .25s ease; }
.percent { font-size: 1.125rem; font-weight: 700; letter-spacing: .5px; }
.loadingscene .small { font-size: 1.25rem; line-height: 1.6; }
.loadingscene.glow .loader-title, .loadingscene.glow .percent { text-shadow: 0 0 15px #fff, 0 0 20px #fff; }
.ld-warn { min-height: 1.2em; font-size: .8rem; color: #ffd9a0; }
.loadbtn {
  display: inline-block; padding: 12px 24px; border: 0; border-radius: 999px; font-weight: 600;
  text-decoration: none; font-size: 1rem; line-height: 1.2; background: #888; color: #fff; cursor: pointer;
  min-width: 35%; opacity: .5; pointer-events: none; transition: all .3s ease;
}
.loadbtn.ready { background: #fff; color: #000; opacity: 1; pointer-events: auto; }

/* ---------------------------------------------------------------- ฉากเนื้อเรื่อง (เดิม: journeystyle.css / resultstyle.css) */
.scene { position: absolute; inset: 0; color: #fff; text-align: center; }
.scroller {
  position: absolute; inset: 0; z-index: 1; overflow-x: hidden; overflow-y: auto;
  scrollbar-width: none; -webkit-overflow-scrolling: touch; overscroll-behavior: contain;
}
.scroller::-webkit-scrollbar { display: none; }
.overlay { position: absolute; inset: 0; pointer-events: none; }
.overlay.dim-25 { background: rgba(0,0,0,.25); }
.overlay.dim-50 { background: rgba(0,0,0,.5); }
.content { position: relative; z-index: 1; padding: 1.5rem 1rem; max-width: 94%; margin-inline: auto; display: flex; flex-direction: column; align-items: center; }
.small { font-size: 1.15rem; }   /* Kanit กว้างกว่าฟอนต์ระบบเดิม — 1.15rem ทำให้ทุกบรรทัดตามที่ผู้เขียนตัดบรรทัดไว้พอดีจอ 390px */
.message { padding-top: calc(var(--top, 25) * 1%); }
.theword { color: #fff; text-shadow: 0 0 15px #fff, 0 0 20px #fff; }
.speaker { opacity: .5; }

.btnlayer { position: absolute; inset: 0; z-index: 6; pointer-events: none; }
.buttonbelow {
  position: absolute; left: 50%; transform: translateX(-50%);
  bottom: calc(var(--btm, 60) * 0.0625rem + env(safe-area-inset-bottom, 0px));
  pointer-events: auto;
}
.nextbtn {
  display: inline-block; min-width: 35%; padding: 12px 24px; border: 0; border-radius: 999px;
  font-weight: 600; font-size: 1rem; line-height: 1.2; white-space: nowrap;
  background: #fff; color: #000; cursor: pointer; text-decoration: none;
}
.nextbtn:active { filter: brightness(.9); }

/* ปุ่มคำตอบ — แต่ละชุดคำถามมีสีของตัวเอง (คงของเดิม) */
.q { position: relative; z-index: 2; width: 100%; display: flex; flex-direction: column; align-items: center; padding-bottom: 3rem; }
.q button {
  font-weight: 500; color: #fff; border: 2px solid #fff; border-radius: 100px;
  min-height: 3.125rem; padding: .4rem 1rem; margin-top: 1.25rem; line-height: 1.3;
  display: grid; place-items: center; text-align: center; cursor: pointer;
  -webkit-appearance: none; appearance: none; max-width: calc(100% - 2rem);
}
.q button:active { transform: scale(.98); filter: brightness(1.2); }
.windanswer  { font-size: 1.5rem;  background: rgba(62,51,120,.75);  width: min(80%, 20rem); }
.windanswer2 { font-size: 1.1rem;  background: rgba(62,51,120,.75);  width: min(120%, 20rem); }
.rockanswer  { font-size: 1.1rem;  background: rgba(40,34,100,.50);  width: min(100%, 21.875rem); }
.thornanswer { font-size: 1.1rem;  background: rgba(56,63,43,.50);   width: min(100%, 21.875rem); }
.mananswer   { font-size: 1.1rem;  background: rgba(104,126,180,.50); width: min(100%, 21.875rem); }
.lastanswer  { font-size: 1.25rem; background: rgba(23,75,164,.50);  width: min(100%, 20rem); }
.scn-s18B .lastanswer, .scn-s18C .lastanswer, .scn-s18D .lastanswer { font-size: 1.15rem; }
button:focus-visible { outline: 2px solid #fff; outline-offset: 3px; }

/* ---------------------------------------------------------------- หน้าผลลัพธ์ (ดีไซน์เดิม) */
.result-scene { display: block; }
.result-wrap {
  position: relative; width: 100%; min-height: 100%;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  padding: var(--padY) var(--padX);
}
.poster {
  position: relative; aspect-ratio: 9 / 16; overflow: hidden; background: #111; box-shadow: 0 6px 24px rgba(0,0,0,.45);
  width: min(92%, 40rem, calc((var(--app-h) - 10.5rem) * 9 / 16));      /* พอดีจอเสมอ ปุ่มด้านล่างไม่โดนตัด */
}
.poster > img {
  position: absolute; inset: 0; width: 100%; height: 100%; object-fit: cover; display: block;
  -webkit-touch-callout: default; -webkit-user-select: auto; user-select: auto;
}
.song-hotspot {
  position: absolute; right: clamp(10px, 3vmin, 18px); bottom: clamp(10px, 3vmin, 18px);
  width: clamp(84px, 28%, 160px); aspect-ratio: 1 / 1; cursor: pointer; background: rgba(255,255,255,.01);
}
.hint { color: #fff; font-size: .8125rem; text-align: center; margin-top: .75rem; font-weight: 300; }
.btn-row { display: flex; gap: 12px; width: min(92%, 40rem); justify-content: center; flex-wrap: wrap; margin-top: 6px; }
.btn--secondary {
  background: #fff; border: 1px solid var(--chip); color: #202020; border-radius: 14px;
  padding: .9em 1.2em; font-weight: 700; cursor: pointer; font-size: .875rem; margin-top: .5rem;
}
.btn--secondary:active { transform: translateY(1px); }

/* ---------------------------------------------------------------- HUD: แท็บความคืบหน้า + ปุ่มเสียง */
#hud { position: absolute; top: 0; left: 0; right: 0; z-index: 40; pointer-events: none; padding-top: env(safe-area-inset-top, 0px); }
.hud-row { display: flex; justify-content: space-between; align-items: center; padding: .7rem .8rem 0; }
.hud-tab {
  position: relative; width: 4.6rem; height: .42rem; border-radius: 999px; overflow: hidden;
  background: rgba(255,255,255,.2); backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px);
}
.hud-tab i { position: absolute; left: 0; top: 0; bottom: 0; width: 0; border-radius: inherit; background: rgba(255,255,255,.92); transition: width .6s ease; }
#muteBtn {
  pointer-events: auto; width: 2rem; height: 2rem; border-radius: 50%; border: 0; padding: 0; cursor: pointer;
  display: grid; place-items: center; color: #fff; background: rgba(0,0,0,.35);
  backdrop-filter: blur(4px); -webkit-backdrop-filter: blur(4px);
}
#muteBtn .off { display: none; }
#muteBtn.off .on { display: none; }
#muteBtn.off .off { display: inline; }

/* ---------------------------------------------------------------- Toast / Modal */
#toast {
  position: absolute; left: 50%; bottom: calc(2rem + env(safe-area-inset-bottom, 0px)); transform: translate(-50%, 8px);
  z-index: 60; max-width: 86%; padding: .6rem 1rem; border-radius: 999px; background: rgba(255,255,255,.95); color: #111;
  font-size: .85rem; opacity: 0; pointer-events: none; transition: opacity .25s ease, transform .25s ease; text-align: center;
}
#toast.on { opacity: 1; transform: translate(-50%, 0); }
#modal { position: absolute; inset: 0; z-index: 70; background: rgba(0,0,0,.88); display: grid; place-items: center; padding: 1rem; }
#modal .box { text-align: center; max-height: 100%; display: flex; flex-direction: column; align-items: center; gap: .75rem; }
#modal p { font-size: .95rem; } #modal p span { opacity: .6; font-size: .8rem; }
#modal img { max-width: 100%; max-height: calc(var(--app-h) - 8rem); object-fit: contain; -webkit-touch-callout: default; -webkit-user-select: auto; user-select: auto; }

/* ---------------------------------------------------------------- debug (?debug=1) */
#dbg { position: absolute; left: 4px; bottom: 4px; z-index: 90; font: 11px/1.3 monospace; color: #0f0; background: rgba(0,0,0,.72); padding: 4px 6px; border-radius: 6px; max-width: 96%; }
#dbg .row { display: flex; gap: 4px; margin: 3px 0; } #dbg select, #dbg button { font-size: 11px; }
#dbg pre { margin: 2px 0 0; white-space: pre-wrap; word-break: break-all; max-height: 7rem; overflow: auto; }

@media (prefers-reduced-motion: reduce) {
  .fi-start, .fi-slow { animation: none; }
}

/* ---------------------------------------------------------------- Footer (ข้อความอยู่ใน APP_CONFIG.footer) */
#footer {
  position: absolute; left: 0; right: 0; z-index: 35; pointer-events: none; text-align: center;
  bottom: calc(.55rem + env(safe-area-inset-bottom, 0px)); padding: 0 1rem;
  font-size: .62rem; letter-spacing: .3px; color: #fff; opacity: .45; font-weight: 300;
}

/* ---------------------------------------------------------------- ช่องเว้นว่างที่เฉลย (ฉันอ่อนแอ______ → เมื่อใด) : ขีดล่างจางหาย พร้อมคำตอบจางเข้ามาแทนที่ตำแหน่งเดิม */
.blank { display: inline-grid; vertical-align: baseline; }
.blank > span { grid-area: 1 / 1; text-align: left; white-space: nowrap; }
.blank .bl-old { contain: inline-size; overflow: hidden; }      /* ความกว้างช่อง = ความกว้างคำตอบ → บรรทัดไม่ขยับตอนเฉลย */
@supports not (contain: inline-size) { .blank .bl-old { width: 0; overflow: visible; } }
.blank .bl-new { opacity: 0; }
.blank.on .bl-old { animation: fadeOutKf 1.8s ease forwards; }
.blank.on .bl-new { animation: fadeIn 2.2s ease .3s forwards; }
html.fast .blank.on .bl-old { animation: none; opacity: 0; }
html.fast .blank.on .bl-new { animation: none; opacity: 1; }
