/* =========================================================
   kp-quest.css — รูปแบบ "ภารกิจฟิสิกส์" (Physics Quest)
   เล่นก่อน → รู้สึกเอง → เข้าใจ → สูตรมาทีหลัง
   ใช้คู่กับ kp.css (ใช้ design tokens เดียวกัน)
   ========================================================= */

.quest { max-width: 1180px; margin: 0 auto; padding: 16px 16px 40px; }
.q-screen[hidden] { display: none !important; }

/* ---------- หัวภารกิจ + แถบแท็บ ---------- */
.q-cover { text-align: center; padding: 10px 0 4px; }
.q-cover h1 { margin: 8px 0 0; font-size: clamp(1.5rem, 4vw, 2.2rem); }
.q-cover .credit { font-family: var(--font-head); color: var(--muted); font-size: .9rem; }
.q-cover .q-sub { max-width: 640px; margin: 4px auto 0; font-size: .95rem; }

.q-tabs {
  position: sticky; top: 60px; z-index: 30; display: flex; gap: 8px; overflow-x: auto; scrollbar-width: none;
  margin: 14px -16px 16px; padding: 10px 16px; background: var(--bg); border-bottom: 1px solid var(--line);
  scroll-snap-type: x proximity;
}
.q-tabs::-webkit-scrollbar { display: none; }
.q-tab {
  flex: none; display: flex; align-items: center; gap: 8px; scroll-snap-align: center;
  border: 2px solid var(--line); background: var(--surface); color: var(--ink); border-radius: 16px;
  padding: 5px 10px 5px 5px; min-height: 52px; cursor: pointer; font: inherit; text-align: left; transition: border-color .2s, transform .15s;
}
.q-tab:hover:not(.locked) { border-color: var(--primary); }
.q-tab .t-ic { width: 34px; height: 34px; border-radius: 11px; display: grid; place-items: center; font-size: 1.15rem; background: var(--bg-soft); flex: none; }
.q-tab .t-txt { font: 500 .86rem var(--font-head); line-height: 1.2; white-space: nowrap; }
.q-tab .t-txt small { display: block; font-size: .75rem; color: var(--muted); font-weight: 400; }
.q-tab .t-st { font-size: .75rem; letter-spacing: -1px; }
.q-tab.done .t-ic { background: var(--good-soft); }
.q-tab.active { border-color: var(--primary); background: var(--primary-soft); box-shadow: 0 4px 14px color-mix(in srgb, var(--primary) 25%, transparent); }
.q-tab.next { border-color: var(--accent); }
.q-tab.next .t-ic { animation: q-bob 1.6s ease-in-out infinite; }
/* ล็อกอยู่: ไม่จางทั้งแท็บ (ตัวหนังสือจะตก AA) ใช้เส้นประ + พื้นเทา + ตัวหนังสือสีรอง แทน */
.q-tab.locked { cursor: not-allowed; border-style: dashed; background: var(--surface-2); color: var(--muted); }
.q-tab.locked .t-ic { opacity: .55; }
.q-tab.summary { margin-left: auto; border-style: dashed; }
.q-tab.summary.active { border-style: solid; }
@keyframes q-bob { 0%, 100% { transform: translateY(0); } 50% { transform: translateY(-4px); } }

.q-mission { display: flex; justify-content: space-between; align-items: flex-end; gap: 10px 20px; flex-wrap: wrap; margin-bottom: 12px; }
.q-mission h2 { margin: 0; font-size: clamp(1.25rem, 3.2vw, 1.6rem); }
.q-mnum { font: 500 .8rem var(--font-head); color: var(--muted); }
.q-goal { background: var(--warn-soft); border: 1px solid color-mix(in srgb, var(--warn) 40%, transparent); border-radius: 12px; padding: 6px 12px; font-size: .95rem; }

/* ---------- หน้าเล่น ---------- */

/* ผู้ช่วยนำทาง (mascot) + กล่องคำพูด */
.q-guide { display: flex; gap: 12px; align-items: flex-end; margin-bottom: 12px; min-height: 64px; }
.q-avatar {
  flex: none; width: 58px; height: 58px; border-radius: 50%; display: grid; place-items: center; font-size: 1.9rem;
  background: radial-gradient(circle at 35% 30%, #fff, var(--accent)); box-shadow: var(--shadow);
}
.q-avatar.talk { animation: q-talk .5s ease 2; }
@keyframes q-talk { 50% { transform: rotate(-8deg) scale(1.06); } }
.q-bubble {
  position: relative; background: var(--surface); border: 2px solid var(--line); border-radius: 18px 18px 18px 4px;
  padding: 10px 16px; font-size: 1.08rem; line-height: 1.55; box-shadow: var(--shadow); max-width: 760px;
}
.q-bubble.good { border-color: var(--good); background: var(--good-soft); }
.q-bubble.bad { border-color: var(--bad); background: var(--bad-soft); }
.q-bubble.think { border-color: var(--primary); background: var(--primary-soft); }

.q-stage { position: relative; background: var(--sim-bg); border: 1px solid var(--line); border-radius: 22px; overflow: hidden; box-shadow: var(--shadow); }
.q-stage canvas { display: block; width: 100%; touch-action: none; cursor: crosshair; }
.q-stage .q-hud { position: absolute; left: 12px; top: 10px; display: flex; gap: 6px; flex-wrap: wrap; pointer-events: none; }
.q-stage .q-hud span { background: color-mix(in srgb, var(--surface) 85%, transparent); border: 1px solid var(--line); border-radius: 999px; padding: 2px 10px; font: 500 .85rem var(--font-head); }

.q-panels { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 10px; }
@media (max-width: 640px) { .q-panels { grid-template-columns: 1fr; } }
.q-panel { appearance: none; border: 3px solid var(--line); border-radius: 18px; overflow: hidden; background: var(--sim-bg); padding: 0; cursor: pointer; position: relative; font: inherit; color: var(--ink); }
.q-panel:hover:not([disabled]) { border-color: var(--primary); }
.q-panel canvas { display: block; width: 100%; }
.q-panel .tag { position: absolute; left: 8px; top: 6px; font: 600 .95rem var(--font-head); background: var(--surface); border-radius: 10px; padding: 0 10px; }
.q-panel.wrong { border-color: var(--bad); opacity: .65; }
.q-panel.right { border-color: var(--good); box-shadow: 0 0 0 4px color-mix(in srgb, var(--good) 25%, transparent); }

.q-actions { display: flex; gap: 10px; flex-wrap: wrap; justify-content: center; align-items: center; margin-top: 14px; min-height: 56px; }
.q-actions .btn { font-size: 1.05rem; min-height: 52px; padding: 12px 22px; border-radius: 16px; }
.q-actions .grow { flex: 1 1 260px; max-width: 420px; }
.q-actions .ctrl { flex: 1 1 260px; max-width: 420px; }
.q-actions .ctrl label { display: flex; justify-content: space-between; font: 500 1rem var(--font-head); }

/* ---------- ผลลัพธ์ + การ์ด Aha ---------- */
.q-result { position: fixed; inset: 0; z-index: 420; background: rgba(10, 12, 30, .6); display: none; place-items: center; padding: 16px; }
.q-result.open { display: grid; }
.q-result .box { background: var(--surface); border-radius: 26px; padding: 22px; width: min(520px, 100%); max-height: 92vh; overflow: auto; text-align: center; box-shadow: var(--shadow-lg); animation: q-pop .35s ease; }
@keyframes q-pop { from { transform: scale(.9); opacity: 0; } to { transform: none; opacity: 1; } }
.q-bigstars { font-size: 2.6rem; letter-spacing: 6px; }
.q-bigstars i { font-style: normal; display: inline-block; filter: grayscale(1); opacity: .3; }
.q-bigstars i.on { filter: none; opacity: 1; animation: q-star .45s ease backwards; }
.q-bigstars i.on:nth-child(2) { animation-delay: .15s; }
.q-bigstars i.on:nth-child(3) { animation-delay: .3s; }
@keyframes q-star { from { transform: scale(0) rotate(-40deg); } to { transform: none; } }

.aha {
  text-align: left; border-radius: 20px; padding: 16px 18px; margin: 14px 0 4px; color: var(--ink);
  background: linear-gradient(135deg, var(--primary-soft), var(--surface-2)); border: 2px solid var(--primary);
  position: relative; overflow: hidden;
}
.aha::before { content: "💡 การ์ด Aha!"; display: block; font: 600 .8rem var(--font-head); color: var(--primary); margin-bottom: 4px; }
.aha h3 { margin: 0 0 6px; }
.aha .formula, .q-chapcard .formula { margin-top: 10px; padding: 8px 12px; background: var(--surface); border-radius: 12px; border: 1px dashed var(--line); text-align: center; font-size: 1.1rem; overflow-x: auto; }
.aha .num { position: absolute; right: 14px; top: 10px; font: 700 1.6rem var(--font-head); color: var(--primary); opacity: .25; }

/* ---------- ประเมินความเข้าใจ ---------- */
.q-steps { display: flex; gap: 6px; justify-content: center; margin-bottom: 8px; }
.q-steps i { width: 28px; height: 5px; border-radius: 9px; background: var(--line); }
.q-steps i.on { background: var(--primary); }
.q-ul { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
.q-uopt {
  appearance: none; border: 2px solid var(--line); background: var(--surface); color: var(--ink); border-radius: 16px;
  padding: 12px 10px; cursor: pointer; font: inherit; display: grid; justify-items: center; gap: 2px; min-height: 104px; transition: border-color .2s, transform .15s;
}
.q-uopt .e { font-size: 2rem; line-height: 1.1; }
.q-uopt b { font-family: var(--font-head); }
.q-uopt small { color: var(--muted); font-size: .78rem; line-height: 1.3; }
.q-uopt:hover { border-color: var(--primary); transform: translateY(-2px); }
.q-uopt[aria-pressed="true"] { border-color: var(--primary); background: var(--primary-soft); }
.q-ufb .callout { margin-top: 12px; }
.q-ufb .alt { background: var(--surface); border-radius: 10px; padding: 8px 12px; margin: 8px 0; }
.q-ufb .ans { background: var(--good-soft); border-radius: 10px; padding: 8px 12px; margin-top: 8px; }
.q-ul.tally { grid-template-columns: 1fr; gap: 6px; }
.q-trow { display: grid; grid-template-columns: 32px 108px 1fr 40px 32px 40px; gap: 6px; align-items: center; text-align: left; }
.q-trow .e { font-size: 1.5rem; }
.q-trow .lbl { font: 500 .88rem var(--font-head); }
.q-trow .bar { height: 12px; background: var(--bg-soft); border-radius: 9px; overflow: hidden; }
.q-trow .bar i { display: block; height: 100%; background: linear-gradient(90deg, var(--primary), var(--accent)); transition: width .3s; }
.q-trow b { text-align: center; font-family: var(--font-head); }
@media (max-width: 420px) { .q-trow { grid-template-columns: 28px 1fr 40px 28px 40px; } .q-trow .bar { display: none; } }

/* ---------- คำถามท้าทายแบบตัวเลือก + จับเวลา ---------- */
.ch { position: relative; margin-top: 8px; }
.ch-q { background: var(--surface); border-radius: 10px; padding: 8px 12px; margin-bottom: 8px; }
.ch-timer { position: relative; height: 26px; border-radius: 999px; background: var(--bg-soft); overflow: hidden; margin-bottom: 10px; }
.ch-timer i { position: absolute; inset: 0 auto 0 0; width: 100%; background: linear-gradient(90deg, var(--good), var(--primary)); transition: width .1s linear; }
.ch-timer.low i { background: var(--bad); }
.ch-timer b { position: relative; display: block; text-align: center; font: 600 .9rem/26px var(--font-head); color: var(--ink); }
.ch-timer b::after { content: " วินาที"; font-weight: 400; }
.ch .choices { grid-template-columns: 1fr 1fr; }
@media (max-width: 520px) { .ch .choices { grid-template-columns: 1fr; } }
.ch-cover { position: absolute; inset: 40px 0 0; display: grid; place-items: center; background: color-mix(in srgb, var(--surface) 70%, transparent); backdrop-filter: blur(3px); border-radius: 12px; }
@media (prefers-reduced-motion: reduce) { .ch-timer i { transition: none; } }

/* ---------- แท็บสรุป ---------- */
.q-stats { margin: 10px 0 14px; }
.q-stats .stat { text-align: center; }
.q-stats .stat b { display: block; font: 700 1.9rem var(--font-head); color: var(--primary); }
.q-sumlist { display: grid; gap: 10px; }
.q-sumrow { display: grid; grid-template-columns: 48px 1fr auto auto auto; gap: 12px; align-items: center; padding: 12px 14px; }
.q-sumrow .ic { font-size: 1.8rem; text-align: center; }
.q-sumrow .info b { display: block; font-family: var(--font-head); }
.q-sumrow .st { white-space: nowrap; }
.q-sumrow select { border: 1.5px solid var(--line); border-radius: 10px; padding: 8px; background: var(--surface); color: var(--ink); font: inherit; min-height: 44px; }
@media (max-width: 640px) {
  .q-sumrow { grid-template-columns: 40px 1fr auto; }
  .q-sumrow .u, .q-sumrow .btn { grid-column: 2 / -1; }
}

/* ---------- เครื่องเล่นภาพประกอบ + เสียงบรรยาย (kp-explain.js) ---------- */
.xp [hidden] { display: none !important; }
.xp { margin: 10px 0 6px; background: var(--surface); border: 1px solid var(--line); border-radius: 16px; padding: 8px; text-align: left; }
.xp-stage { position: relative; border-radius: 12px; overflow: hidden; background: var(--sim-bg); }
.xp-stage canvas { display: block; width: 100%; }
.xp-cap { margin: 8px 4px 2px; font-size: 1rem; line-height: 1.6; min-height: 3.2em; }
.xp-cap .said { color: var(--ink); background: linear-gradient(transparent 62%, color-mix(in srgb, var(--warn) 35%, transparent) 62%); }
.xp-cap .rest { color: var(--muted); }
.xp-bar { display: flex; align-items: center; gap: 8px; margin-top: 6px; flex-wrap: wrap; }
.xp-bar .xp-scrub { flex: 1 1 140px; }
.xp-time { font: 500 .8rem var(--font-head); color: var(--muted); font-variant-numeric: tabular-nums; white-space: nowrap; }
.xp-chips { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 6px; }
.xp-chips button { border: 1px solid var(--line); background: var(--surface-2); color: var(--muted); border-radius: 999px; padding: 4px 10px; font: 500 .78rem var(--font-head); cursor: pointer; min-height: 34px; }
.xp-chips button.on { border-color: var(--primary); background: var(--primary-soft); color: var(--ink); }
.xp-modes { margin-top: 8px; }
.xp-ctrls { display: grid; gap: 10px; margin-top: 8px; }
.q-result .xp { margin-top: 12px; }
/* ---------- แท็บเรียนเนื้อหา (cfg.course) ---------- */
.q-tab.course .t-ic { background: var(--primary-soft); }
.q-tab.course.done .t-ic { background: var(--good-soft); }
.q-chap { display: flex; gap: 8px; flex-wrap: wrap; margin: 12px 0; }
.q-chap button { display: inline-flex; align-items: center; gap: 8px; min-height: 44px; padding: 6px 14px 6px 6px; border: 1px solid var(--line); border-radius: 999px; background: var(--surface); color: var(--ink); font: 500 .9rem var(--font-head); cursor: pointer; text-align: left; }
.q-chap button .n { width: 30px; height: 30px; flex: none; border-radius: 50%; display: grid; place-items: center; background: var(--bg-soft); font-size: .85rem; }
.q-chap button.seen .n { background: var(--good-soft); color: var(--ink); }
.q-chap button.on { border-color: var(--primary); background: var(--primary-soft); }
.q-chapcard h3 { margin: 2px 0 8px; }
.q-points { margin-top: 12px; }
.q-points ul { margin: 6px 0 0; padding-left: 1.3em; }
.q-points li { margin: 4px 0; }
.q-chapnav { justify-content: space-between; margin-top: 14px; }

/* ---------- สคริปต์อ่านตาม (kp-explain: transcript) ---------- */
.xp-rate { font-variant-numeric: tabular-nums; }
/* ชี้ปุ่มเล่นเมื่อเบราว์เซอร์ไม่ยอมเล่นเสียงเอง: วงแหวนขยาย 2 ครั้งแล้วหยุด (ไม่กะพริบ) */
.xp-play.xp-nudge { animation: xp-nudge 1.1s ease-out 2; }
@keyframes xp-nudge { 0% { box-shadow: 0 0 0 0 var(--primary); } 100% { box-shadow: 0 0 0 12px transparent; } }
@media (prefers-reduced-motion: reduce) { .xp-play.xp-nudge { animation: none; outline: 3px solid var(--primary); outline-offset: 2px; } }
.xp-script { margin-top: 8px; border: 1px solid var(--line); border-radius: 12px; background: var(--surface-2); padding: 8px 10px; }
.xp-script-head { font-size: .85rem; margin-bottom: 4px; }
.xp-script-head .muted { font-size: .8rem; }
.xp-lines { position: relative; max-height: 13.5em; overflow-y: auto; overscroll-behavior: contain; scroll-behavior: smooth; }
.xp-line { padding: 5px 8px; margin: 2px 0; border-radius: 10px; border-left: 4px solid transparent; font-size: 1.0625rem; line-height: 1.75; color: var(--muted); cursor: pointer; }
.xp-line:hover, .xp-line:focus-visible { background: var(--surface); outline: none; }
.xp-line:focus-visible { box-shadow: 0 0 0 2px var(--primary); }
.xp-line.said { color: var(--ink); }
.xp-line.now { color: var(--muted); background: var(--surface); border-left-color: var(--primary); }
.xp-line.now .w.on { color: var(--ink); background: color-mix(in srgb, var(--warn) 35%, transparent); }
.xp-script.reading .xp-line.now { color: var(--ink); }
.xp-line.ev { border-left-color: var(--accent); box-shadow: 0 0 0 2px var(--accent); background: var(--surface); color: var(--ink); }
.xp-term { font: inherit; color: inherit; background: none; border: 0; border-bottom: 2px dashed var(--primary); padding: 0 1px; margin: 0; cursor: help; border-radius: 0; }
.xp-term:hover, .xp-term:focus-visible { background: var(--primary-soft); outline: none; }
.xp-gloss { margin-top: 6px; padding: 8px 40px 8px 12px; position: relative; border-radius: 10px; background: var(--primary-soft); color: var(--ink); font-size: 1rem; line-height: 1.6; }
.xp-gloss-x { position: absolute; top: 2px; right: 2px; width: 36px; height: 36px; border: 0; background: none; color: var(--ink); font-size: 1rem; cursor: pointer; border-radius: 8px; }
.xp-readnav { margin-top: 8px; justify-content: space-between; align-items: center; }
.xp-readpos { font: 500 .85rem var(--font-head); }
@media (prefers-reduced-motion: reduce) { .xp-lines { scroll-behavior: auto; } }

/* ---------- อ่านแล้วตอบ (ท้ายแต่ละตอนของแท็บเรียนเนื้อหา) ---------- */
.q-readq { margin-top: 14px; padding: 12px; border-radius: 14px; background: var(--bg-soft); }
.q-readq h4 { margin: 0 0 8px; font: 600 1.05rem var(--font-head); }
.q-readq .rq { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; margin-top: 8px; }
.q-readq .rq-q { margin-bottom: 8px; line-height: 1.7; }
.q-readq .rq-q .pill { font-size: .78rem; }
.q-readq .feedback .btn { margin-left: 4px; }

@media print { .xp-bar, .xp-chips, .xp-modes, .xp-ctrls { display: none !important; } }

/* ---------- การ์ดความรู้แบบอ่านเองได้ ---------- */
.q-study { display: grid; gap: 14px; margin-top: 12px; max-width: 820px; }
.aha.study { padding: 18px 20px; }
.aha.study::before { content: "📖 การ์ดความรู้"; }
.study-top { display: flex; justify-content: space-between; align-items: center; gap: 8px; margin-bottom: 4px; }
.aha.study .big-idea { font-size: 1.12rem; line-height: 1.7; margin-bottom: 10px; }
.aha.study details { background: var(--surface); border: 1px solid var(--line); border-radius: 12px; padding: 0 12px; margin-top: 8px; }
.aha.study details[open] { padding-bottom: 10px; }
.aha.study summary { cursor: pointer; font: 500 .95rem var(--font-head); padding: 10px 0; min-height: 44px; display: flex; align-items: center; }
.aha.study summary::-webkit-details-marker { display: none; }
.aha.study summary::after { content: "▸"; margin-left: auto; transition: transform .2s; }
.aha.study details[open] summary::after { transform: rotate(90deg); }
.aha.study .legend { width: 100%; border-collapse: collapse; margin-top: 8px; font-size: .93rem; }
.aha.study .legend td { border-top: 1px dashed var(--line); padding: 6px 4px; vertical-align: top; }
.aha.study .legend td:first-child { width: 64px; text-align: center; font-size: 1.05rem; }
.aha.study .ans { background: var(--good-soft); border-radius: 10px; padding: 8px 12px; margin-top: 8px; }
.study-self { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; margin-top: 12px; }
.study-self button { appearance: none; border: 2px solid var(--line); background: var(--surface); border-radius: 12px; font-size: 1.3rem; width: 46px; height: 46px; cursor: pointer; }
.study-self button.on { border-color: var(--primary); background: var(--primary-soft); }
@media print { .aha.study details { break-inside: avoid; } .study-top .btn, .study-self button:not(.on) { display: none; } }

.q-book { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 14px; margin-top: 16px; }
.q-book .aha.locked { background: var(--bg-soft); border-style: dashed; border-color: var(--line); color: var(--muted); }
.q-book .aha.locked::before { content: "🔒 ยังไม่ได้ปลดล็อก"; color: var(--muted); }

@media (prefers-reduced-motion: reduce) {
  .q-tab.next .t-ic, .q-avatar.talk, .q-result .box, .q-bigstars i.on { animation: none; }
}
@media (max-width: 520px) {
  .q-bubble { font-size: 1rem; }
  .q-avatar { width: 48px; height: 48px; font-size: 1.5rem; }
}

@media print {
  .q-tabs, .q-result, .q-screen[data-s="play"], .q-screen[data-s="course"] { display: none !important; }
  .q-sumrow { break-inside: avoid; }
}
