/* รีหละตุลอิลม์ — เส้นทางแห่งปัญญา : สไตล์หลัก (มือถือเป็นหลัก) */
@font-face { font-family: 'Amiri'; src: local('Amiri'), url(../fonts/Amiri-Regular.woff2) format('woff2'); font-display: swap; }
@font-face { font-family: 'Noto Sans Thai'; src: local('Noto Sans Thai'), url(../fonts/NotoSansThai.woff2) format('woff2'); font-display: swap; }

:root {
  --font: 'Noto Sans Thai', 'Sarabun', 'Prompt', 'Leelawadee UI', 'Tahoma', 'Loma', system-ui, sans-serif;
  --font-ar: 'Amiri', 'Noto Naskh Arabic', 'Scheherazade New', 'Traditional Arabic', 'Geeza Pro', 'Segoe UI', serif;
  --fs: 17px;
  --gold: #c9a227; --gold2: #8a6a12;
  --emerald: #0f766e; --emerald2: #0b5a54;
  --danger: #b4232c; --ok: #138a4b;
  --radius: 24px;
  --warm: 0;
}
html[data-theme='day'] {
  --card: rgba(255, 249, 232, .93); --card2: rgba(247, 236, 207, .95); --ink: #2b1d0c; --muted: #6a5636; --line: rgba(138, 106, 18, .35);
  --btn-bg: linear-gradient(180deg, #14948a, #0b5f58); --btn-ink: #fffbea; --chip: #f3e6bf; --bar: rgba(255, 248, 228, .85); --bg: #e9d9b2;
}
html[data-theme='eye'] {
  --card: rgba(238, 224, 190, .94); --card2: rgba(228, 212, 172, .95); --ink: #3a2e1a; --muted: #6e5e40; --line: rgba(120, 92, 30, .35);
  --btn-bg: linear-gradient(180deg, #2f8077, #1f5e58); --btn-ink: #f6efd8; --chip: #e3d3a4; --bar: rgba(236, 222, 186, .88); --bg: #d9c89a;
}
html[data-theme='night'] {
  --card: rgba(18, 27, 52, .90); --card2: rgba(26, 38, 70, .92); --ink: #efe6cc; --muted: #b9ad8c; --line: rgba(233, 196, 90, .35);
  --btn-bg: linear-gradient(180deg, #1b8f85, #0e5a55); --btn-ink: #fff8e2; --chip: #26365f; --bar: rgba(10, 16, 34, .85); --bg: #070d1e;
  --gold: #e8c24d;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html { font-size: var(--fs); }
html, body { margin: 0; height: 100%; background: var(--bg); }
body { font-family: var(--font); color: var(--ink); line-height: 1.6; overscroll-behavior: none; -webkit-text-size-adjust: 100%; }
button { font: inherit; color: inherit; cursor: pointer; }
.ic { display: inline-block; vertical-align: -0.2em; flex: none; }

#bg { position: fixed; inset: 0; z-index: 0; overflow: hidden; background: var(--bg); }
#bg .layer { position: absolute; inset: 0; opacity: 0; transition: opacity .9s ease; }
#bg .layer.on { opacity: 1; }
#bg svg { width: 100%; height: 100%; display: block; }
#warm { position: fixed; inset: 0; z-index: 90; pointer-events: none; background: #ff9a2e; mix-blend-mode: multiply; opacity: calc(var(--warm) * 0.5); }
#veil { position: fixed; inset: 0; z-index: 1; pointer-events: none; background: linear-gradient(180deg, rgba(0,0,0,0) 0%, rgba(0,0,0,.12) 100%); }
html[data-theme='eye'] #bg { filter: sepia(.35) saturate(.8) brightness(.93); }

#app { position: relative; z-index: 2; min-height: 100dvh; display: flex; flex-direction: column; padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left); }
.topbar { background: linear-gradient(180deg, rgba(5,10,30,.55), rgba(5,10,30,0)); display: flex; align-items: center; gap: 10px; padding: 10px 14px; position: sticky; top: 0; z-index: 5; }
.topbar .brand { font-family: var(--font-ar); font-size: 1.35rem; color: #fff; text-shadow: 0 2px 8px rgba(0,0,0,.6); flex: 1; line-height: 1.2; }
.pill { display: inline-flex; align-items: center; gap: 6px; background: var(--bar); border: 1px solid var(--line); border-radius: 999px; padding: 5px 12px; font-size: .88rem; color: var(--ink); backdrop-filter: blur(8px); }
.iconbtn { width: 44px; height: 44px; border-radius: 50%; border: 1px solid var(--line); background: var(--bar); color: var(--ink); display: grid; place-items: center; backdrop-filter: blur(8px); }
.iconbtn:active { transform: scale(.94); }

.screen { flex: 1; width: 100%; max-width: 760px; margin: 0 auto; padding: 8px 16px 40px; display: flex; flex-direction: column; gap: 14px; animation: rise .45s ease both; }
@keyframes rise { from { opacity: 0; transform: translateY(14px); } to { opacity: 1; transform: none; } }

.card { position: relative; background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 18px 18px 20px; box-shadow: 0 10px 34px rgba(0,0,0,.28); backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px); }
.card::before { content: ''; position: absolute; left: 22px; right: 22px; top: 6px; height: 2px; background: linear-gradient(90deg, transparent, var(--gold), transparent); opacity: .8; }
.card h2, .card h3 { margin: 0 0 6px; font-weight: 700; }
.card h2 { font-size: 1.25rem; } .card h3 { font-size: 1.05rem; }
.muted { color: var(--muted); }
.small { font-size: .85rem; }
.center { text-align: center; }
.row { display: flex; gap: 10px; align-items: center; }
.wrap { flex-wrap: wrap; }
.grow { flex: 1; }
.stack { display: flex; flex-direction: column; gap: 12px; }
.ar { font-family: var(--font-ar); direction: rtl; text-align: center; font-size: 1.7rem; line-height: 2.2; }
.ar.sm { font-size: 1.25rem; line-height: 1.9; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: 48px; padding: 10px 22px; border-radius: 16px; border: 1px solid var(--gold); background: var(--btn-bg); color: var(--btn-ink); font-weight: 700; font-size: 1rem; box-shadow: 0 4px 0 var(--gold2), 0 8px 18px rgba(0,0,0,.25); transition: transform .08s, box-shadow .08s, filter .2s; text-decoration: none; }
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--gold2), 0 3px 8px rgba(0,0,0,.25); }
.btn.big { min-height: 58px; font-size: 1.12rem; }
.btn.ghost { background: var(--chip); color: var(--ink); box-shadow: 0 3px 0 var(--line); border-color: var(--line); }
.btn.gold { background: linear-gradient(180deg, #e9c552, #b8901c); color: #3a2a05; }
.btn.danger { background: linear-gradient(180deg, #c5303a, #8f1b23); }
.btn[disabled] { filter: grayscale(.8); opacity: .6; pointer-events: none; }
.btn.block { width: 100%; }
:focus-visible { outline: 3px solid #ffd54a; outline-offset: 2px; }

/* หน้าแรก */
.hero { text-align: center; padding: 6vh 0 0; }
.hero .arch { font-family: var(--font-ar); font-size: clamp(3rem, 14vw, 5.2rem); color: #fff3c9; text-shadow: 0 0 24px rgba(255, 210, 110, .75), 0 4px 10px rgba(0,0,0,.6); line-height: 1.3; }
.hero h1 { margin: 2px 0 4px; color: #fff; font-size: clamp(1.35rem, 5.5vw, 2rem); text-shadow: 0 2px 10px rgba(0,0,0,.7); }
.hero p { color: #ffeccb; margin: 0 auto; max-width: 28em; text-shadow: 0 1px 6px rgba(0,0,0,.7); }
.menu { display: grid; gap: 12px; margin-top: 8px; }
.verse { margin-top: 6px; }

/* แผนที่ด่าน */
.path { position: relative; display: flex; flex-direction: column; gap: 6px; padding: 6px 0 20px; }
.node { position: relative; display: flex; align-items: center; gap: 12px; padding: 10px 14px; border-radius: 22px; background: var(--card); border: 1px solid var(--line); backdrop-filter: blur(8px); text-align: left; width: 86%; transition: transform .15s; }
.node:nth-child(even) { align-self: flex-end; flex-direction: row-reverse; text-align: right; }
.node:active { transform: scale(.98); }
.node .med { position: relative; flex: none; }
.node .med .medal { filter: drop-shadow(0 3px 5px rgba(0,0,0,.35)); }
.node.locked { filter: grayscale(.9); opacity: .65; }
.node .lockmark { position: absolute; right: -4px; bottom: -4px; background: var(--card2); border-radius: 50%; width: 26px; height: 26px; display: grid; place-items: center; border: 1px solid var(--line); }
.node .ttl { font-weight: 700; }
.node .sub { font-size: .8rem; color: var(--muted); }
.stars { color: var(--gold); letter-spacing: 2px; font-size: .95rem; }
.stars .off { opacity: .28; }
.tag { display: inline-block; font-size: .72rem; padding: 1px 9px; border-radius: 999px; color: #fff; margin-top: 2px; }
.node::after { content: ''; position: absolute; left: 50%; bottom: -8px; width: 2px; height: 8px; background: repeating-linear-gradient(#e8c24d 0 3px, transparent 3px 6px); }
.node:last-child::after { display: none; }

/* บทสนทนา */
.dlg { display: flex; gap: 12px; align-items: flex-start; }
.dlg.me { flex-direction: row-reverse; }
.bubble { background: var(--card2); border: 1px solid var(--line); border-radius: 18px; padding: 12px 14px; flex: 1; font-size: 1rem; animation: pop .3s ease both; }
.bubble .who { font-weight: 700; font-size: .82rem; color: var(--gold2); margin-bottom: 2px; }
html[data-theme='night'] .bubble .who { color: var(--gold); }
@keyframes pop { from { opacity: 0; transform: scale(.96) translateY(6px); } to { opacity: 1; transform: none; } }
.narr { font-style: italic; color: var(--muted); text-align: center; padding: 6px 10px; animation: pop .3s ease both; }

.steps { display: flex; gap: 6px; justify-content: center; margin: 4px 0 2px; }
.steps i { width: 26px; height: 6px; border-radius: 4px; background: rgba(255,255,255,.35); display: block; }
.steps i.on { background: var(--gold); }
.steps i.cur { background: #fff; box-shadow: 0 0 8px #fff; }
.chead { display: flex; align-items: center; gap: 12px; }
.chead .medal { flex: none; }
.chead .t { flex: 1; min-width: 0; }
.chead .t b { display: block; font-size: 1.1rem; }

/* คำถาม */
.opts { display: grid; gap: 10px; margin-top: 10px; }
.opt { text-align: left; padding: 13px 16px; border-radius: 16px; background: var(--card2); border: 2px solid var(--line); font-size: 1rem; min-height: 52px; transition: transform .1s, border-color .2s, background .2s; display: flex; gap: 10px; align-items: center; }
.opt:active { transform: scale(.985); }
.opt .k { width: 28px; height: 28px; border-radius: 50%; background: var(--chip); display: grid; place-items: center; font-weight: 700; flex: none; font-size: .85rem; }
.opt.right { border-color: var(--ok); background: rgba(19, 138, 75, .22); }
.opt.wrong { border-color: var(--danger); background: rgba(180, 35, 44, .2); animation: shake .35s; }
.opt.faded { opacity: .35; }
@keyframes shake { 20%, 60% { transform: translateX(-6px); } 40%, 80% { transform: translateX(6px); } }
.expl { margin-top: 10px; padding: 10px 14px; border-radius: 14px; background: var(--chip); border: 1px dashed var(--line); animation: pop .3s both; }
.timer { height: 8px; background: var(--chip); border-radius: 6px; overflow: hidden; }
.timer > i { display: block; height: 100%; width: 100%; background: linear-gradient(90deg, #14948a, #e9c552); transform-origin: left; }

/* มินิเกม */
.game { touch-action: manipulation; user-select: none; -webkit-user-select: none; }
.hint { font-size: .92rem; color: var(--muted); text-align: center; }
.pipegrid { display: grid; gap: 3px; margin: 8px auto; width: min(100%, 420px); aspect-ratio: 1; padding: 6px; background: #3b2a16; border-radius: 16px; }
.pipecell { background: #e7d7ae; border-radius: 8px; padding: 0; border: 0; position: relative; overflow: hidden; }
.pipecell svg { width: 100%; height: 100%; display: block; transition: transform .22s; }
.pipecell .p { stroke: #8a6a3c; stroke-width: 16; stroke-linecap: butt; fill: none; }
.pipecell.wet .p { stroke: #1fa2d0; }
.pipecell.src, .pipecell.dst { background: #cfe9f0; }
.scale-wrap { position: relative; height: 190px; margin: 4px 0; }
.scale-beam { position: absolute; left: 6%; right: 6%; top: 40px; height: 10px; background: linear-gradient(#c9a227, #8a6a12); border-radius: 6px; transform-origin: center; transition: transform .5s cubic-bezier(.3, 1.6, .5, 1); }
.scale-post { position: absolute; left: 50%; top: 40px; width: 12px; bottom: 6px; margin-left: -6px; background: #7a5a12; border-radius: 6px; }
.scale-base { position: absolute; left: 30%; right: 30%; bottom: 0; height: 10px; background: #7a5a12; border-radius: 6px; }
.pan { position: absolute; top: 50px; width: 40%; text-align: center; transition: transform .5s cubic-bezier(.3, 1.6, .5, 1); }
.pan .tray { margin: 38px auto 0; min-width: 80px; max-width: 94%; padding: 10px 6px; background: #f4e3b0; border: 2px solid #8a6a12; border-radius: 0 0 40px 40px; font-weight: 700; font-size: 1.2rem; color: #3a2a05; }
.pan::before { content: ''; position: absolute; left: 50%; top: -4px; height: 42px; border-left: 2px solid #8a6a12; }
.tilt-l .scale-beam { transform: rotate(-9deg); } .tilt-l .pan.l { transform: translateY(24px); } .tilt-l .pan.r { transform: translateY(-24px); }
.tilt-r .scale-beam { transform: rotate(9deg); } .tilt-r .pan.r { transform: translateY(24px); } .tilt-r .pan.l { transform: translateY(-24px); }
.seqlist { display: grid; gap: 8px; margin: 8px 0; }
.slot { display: flex; align-items: center; gap: 10px; padding: 10px 12px; border: 2px dashed var(--line); border-radius: 14px; min-height: 48px; background: rgba(255,255,255,.12); }
.slot.filled { border-style: solid; border-color: var(--ok); background: rgba(19, 138, 75, .18); }
.slot .n { width: 28px; height: 28px; border-radius: 50%; background: var(--gold); color: #3a2a05; font-weight: 700; display: grid; place-items: center; flex: none; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; margin-top: 6px; }
.chip { padding: 10px 14px; border-radius: 14px; background: var(--card2); border: 2px solid var(--line); font-size: .95rem; display: inline-flex; align-items: center; gap: 8px; min-height: 46px; }
.chip.sel { border-color: #ffd54a; box-shadow: 0 0 0 3px rgba(255, 213, 74, .4); }
.chip.done { opacity: .35; pointer-events: none; }
.chip.bad { animation: shake .35s; border-color: var(--danger); }
.matchcols { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.matchcols .chip { width: 100%; justify-content: center; text-align: center; }
.chip.m0 { background: #cbe7ff; } .chip.m1 { background: #ffd9c9; } .chip.m2 { background: #d6f2cf; } .chip.m3 { background: #ecd6ff; } .chip.m4 { background: #fff2b8; }
html[data-theme='night'] .chip.m0, html[data-theme='night'] .chip.m1, html[data-theme='night'] .chip.m2, html[data-theme='night'] .chip.m3, html[data-theme='night'] .chip.m4 { color: #111; }
.clue { padding: 10px 12px; border-radius: 12px; border: 1px solid var(--line); background: var(--card2); text-align: left; width: 100%; }
.clue.open { background: var(--chip); }
.range { width: 100%; accent-color: var(--emerald); height: 36px; }
.meter { display: flex; align-items: center; gap: 8px; }
.meter .bar { flex: 1; height: 14px; background: var(--chip); border-radius: 8px; overflow: hidden; border: 1px solid var(--line); }
.meter .bar i { display: block; height: 100%; background: linear-gradient(90deg, #14948a, #e9c552); transition: width .5s; }
.meter .bar i.low { background: linear-gradient(90deg, #c5303a, #e98a52); }
.hazard-scene { position: relative; width: 100%; aspect-ratio: 4 / 3; border-radius: 18px; overflow: hidden; border: 2px solid var(--line); background: linear-gradient(#f9e2b0, #c99d5a); }
.hz { position: absolute; transform: translate(-50%, -50%); width: 52px; height: 52px; border-radius: 50%; border: 2px dashed rgba(60, 40, 10, .35); background: rgba(255, 255, 255, .55); font-size: 1.6rem; display: grid; place-items: center; padding: 0; }
.hz.found { border: 3px solid var(--ok); background: rgba(19, 138, 75, .35); }
.hz.fp { border: 3px solid var(--danger); background: rgba(180, 35, 44, .3); }
.cardstack { position: relative; min-height: 200px; }
.rcard { background: var(--card2); border: 2px solid var(--gold); border-radius: 20px; padding: 18px; text-align: center; font-size: 1.1rem; animation: pop .35s both; }
.qsvg { width: 100%; height: auto; border-radius: 16px; border: 2px solid var(--line); background: #e8d6a8; touch-action: none; }

.toast { position: fixed; left: 50%; bottom: 28px; transform: translateX(-50%); background: rgba(20, 20, 20, .9); color: #fff; padding: 10px 18px; border-radius: 14px; z-index: 100; max-width: 88vw; font-size: .92rem; animation: pop .3s both; }
.modal { position: fixed; inset: 0; z-index: 80; background: rgba(0, 0, 0, .55); display: grid; place-items: center; padding: 16px; }
.modal .card { width: min(520px, 100%); max-height: 88dvh; overflow: auto; }
.badge { display: flex; gap: 10px; align-items: center; padding: 8px 10px; border-radius: 14px; background: var(--card2); border: 1px solid var(--line); }
.badge.off { opacity: .4; filter: grayscale(1); }
.input { width: 100%; padding: 12px 14px; border-radius: 14px; border: 2px solid var(--line); background: #fffdf5; color: #2b1d0c; font: inherit; font-size: 1.05rem; min-height: 48px; }
html[data-theme='night'] .input { background: #0f1830; color: #efe6cc; }
.avatars { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; }
.avatar { padding: 10px 4px; border-radius: 16px; background: var(--card2); border: 2px solid var(--line); text-align: center; font-size: .82rem; }
.avatar.sel { border-color: var(--gold); box-shadow: 0 0 0 3px rgba(201, 162, 39, .45); }
.seg { display: flex; background: var(--chip); border-radius: 14px; padding: 3px; gap: 3px; }
.seg button { flex: 1; border: 0; background: transparent; padding: 10px 6px; border-radius: 11px; font-weight: 600; min-height: 44px; }
.seg button.on { background: var(--btn-bg); color: var(--btn-ink); }
.switch { display: flex; align-items: center; justify-content: space-between; gap: 10px; min-height: 44px; }
.switch input { width: 48px; height: 28px; accent-color: var(--emerald); }
table.sqlres { border-collapse: collapse; width: 100%; font-size: .8rem; }
table.sqlres th, table.sqlres td { border: 1px solid var(--line); padding: 4px 6px; text-align: left; }
textarea.input { font-family: ui-monospace, Consolas, monospace; font-size: .85rem; min-height: 90px; }
.codebox { font-family: ui-monospace, Consolas, monospace; font-size: 1.8rem; letter-spacing: .3em; text-align: center; background: var(--chip); border-radius: 14px; padding: 8px; }
.plist { display: grid; gap: 8px; }
.pl { display: flex; align-items: center; gap: 10px; padding: 8px 12px; background: var(--card2); border-radius: 14px; border: 1px solid var(--line); }
.pl.me { border-color: var(--gold); }
.pl .sc { margin-left: auto; font-weight: 700; }
.rankn { width: 30px; height: 30px; border-radius: 50%; background: var(--gold); color: #3a2a05; display: grid; place-items: center; font-weight: 800; flex: none; }
.progressline { height: 12px; border-radius: 8px; background: var(--chip); overflow: hidden; }
.progressline i { display: block; height: 100%; background: linear-gradient(90deg, #14948a, #e9c552); }

/* แอนิเมชันฉาก */
.tw { animation: tw 3.2s ease-in-out infinite; } @keyframes tw { 0%, 100% { opacity: .25; } 50% { opacity: 1; } }
.fl { animation: fl 4s ease-in-out infinite; } @keyframes fl { 0%, 100% { opacity: .95; } 50% { opacity: .55; } }
.drift { animation: drift 90s linear infinite; } @keyframes drift { from { transform: translateX(-160px); } to { transform: translateX(1300px); } }
.spin { animation: spin 22s linear infinite; transform-box: fill-box; transform-origin: center; } .spin.slow { animation-duration: 50s; transform-box: view-box; } .spin.rev { animation-direction: reverse; }
@keyframes spin { to { transform: rotate(360deg); } }
.sway { animation: sway 6s ease-in-out infinite; } @keyframes sway { 0%, 100% { transform: rotate(-1.2deg); } 50% { transform: rotate(1.2deg); } }
.swing { animation: swing 5s ease-in-out infinite; } @keyframes swing { 0%, 100% { transform: rotate(-3deg); } 50% { transform: rotate(3deg); } }
.flow { animation: flow 3s ease-in-out infinite; } @keyframes flow { 0%, 100% { transform: translateX(-6px); } 50% { transform: translateX(6px); } }
.jet { stroke-dasharray: 8 6; animation: jet 1.2s linear infinite; } @keyframes jet { to { stroke-dashoffset: -28; } }
html[data-motion='off'] *, html[data-motion='off'] *::before { animation: none !important; transition: none !important; }
@media (prefers-reduced-motion: reduce) { html[data-motion='auto'] *, html[data-motion='auto'] *::before { animation-duration: .001s !important; animation-iteration-count: 1 !important; } }
@media (min-width: 760px) { :root { --fs: 18px; } .node { width: 70%; } }
