/* ============================================================
   style.css — สายฝนแห่งเราะห์มะฮ์
   มือถือก่อน (mobile-first) · โหมดถนอมสายตา · เคารพ prefers-reduced-motion
   ============================================================ */
:root {
  --bg0: #061622;
  --bg1: #0b2236;
  --card: rgba(10, 33, 52, .78);
  --card-solid: #0e2a42;
  --line: rgba(160, 210, 240, .22);
  --text: #eaf4fb;
  --muted: #a9c2d4;
  --gold: #f5c76b;
  --gold-2: #ffdf9e;
  --teal: #4cc3b5;
  --blue: #5bb8ee;
  --danger: #ff8d7a;
  --ok: #7ee2a8;
  --radius: 18px;
  --safe-t: env(safe-area-inset-top, 0px);
  --safe-b: env(safe-area-inset-bottom, 0px);
  --fs: 1;
  --font-th: 'Noto Sans Thai', 'Sarabun', 'Tahoma', system-ui, -apple-system, 'Segoe UI', sans-serif;
  --font-ar: 'Amiri', 'Scheherazade New', 'Noto Naskh Arabic', 'Traditional Arabic', serif;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg0);
  color: var(--text);
  font-family: var(--font-th);
  font-size: calc(17px * var(--fs));
  line-height: 1.65;
  overflow: hidden;
  overscroll-behavior: none;
  -webkit-text-size-adjust: 100%;
}
button { font: inherit; color: inherit; cursor: pointer; }
a { color: var(--blue); }
:focus-visible { outline: 3px solid var(--gold); outline-offset: 2px; }

/* ---------- ชั้นพื้นหลัง ---------- */
#scene { position: fixed; inset: 0; z-index: 0; overflow: hidden; background: linear-gradient(#0b2236, #061622); }
#scene svg { width: 100%; height: 100%; display: block; }
#rain { position: fixed; inset: 0; z-index: 1; width: 100%; height: 100%; pointer-events: none; }
#flash { position: fixed; inset: 0; z-index: 2; background: #fff; opacity: 0; pointer-events: none; }
#flash.on { animation: flash 0.9s ease-out; }
@keyframes flash { 0% { opacity: .0 } 6% { opacity: .75 } 14% { opacity: .1 } 22% { opacity: .55 } 100% { opacity: 0 } }

/* โหมดถนอมสายตา: โทนอุ่น ลดแสงสีน้ำเงิน ลดความจัดของฉาก */
#eyefilter { position: fixed; inset: 0; z-index: 90; pointer-events: none; opacity: 0; transition: opacity .4s;
  background: rgba(255, 150, 40, .20); mix-blend-mode: multiply; }
body.eye #eyefilter { opacity: 1; }
body.eye { --text: #f3e9d8; --muted: #cdbfa6; --card: rgba(30, 24, 14, .82); --card-solid: #2a2113; --line: rgba(240, 210, 150, .22); --gold: #e9b866; --blue: #d9b27a; --teal: #b9c98a; }
body.eye #scene { filter: sepia(.55) saturate(.8) brightness(.78); }
body.eye #rain { opacity: .6; }
body.eye .btn.primary { background: linear-gradient(#d9a24f, #b27b2b); color: #1f1608; }

body.contrast { --muted: #d6e6f2; --line: rgba(220, 240, 255, .5); }

/* ---------- แถบบน ---------- */
#bar { position: fixed; z-index: 20; top: 0; left: 0; right: 0; display: flex; align-items: center; gap: 6px;
  padding: calc(8px + var(--safe-t)) 10px 8px; background: linear-gradient(rgba(5, 16, 26, .92), rgba(5, 16, 26, 0)); }
.bar-title { flex: 1; text-align: center; font-weight: 700; font-size: .95rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; text-shadow: 0 1px 6px #000; }
.icon-btn { width: 44px; height: 44px; border-radius: 14px; border: 1px solid var(--line); background: rgba(8, 28, 44, .7);
  backdrop-filter: blur(6px); font-size: 1.15rem; display: grid; place-items: center; }
.icon-btn[aria-pressed="true"] { border-color: var(--gold); box-shadow: 0 0 0 2px rgba(245, 199, 107, .25); }

/* ---------- พื้นที่เนื้อหา ---------- */
#view { position: fixed; z-index: 10; inset: 0; overflow-y: auto; overflow-x: hidden; -webkit-overflow-scrolling: touch;
  padding: calc(64px + var(--safe-t)) 14px calc(28px + var(--safe-b)); scroll-behavior: smooth; outline: none; }
.wrap { max-width: 640px; margin: 0 auto; display: flex; flex-direction: column; gap: 14px; }

.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); padding: 16px 16px;
  backdrop-filter: blur(10px); box-shadow: 0 8px 28px rgba(0, 0, 0, .28); animation: rise .45s ease both; }
@keyframes rise { from { opacity: 0; transform: translateY(10px) } to { opacity: 1; transform: none } }
.card h2, .card h3 { margin: 0 0 6px; line-height: 1.35; }
.card h2 { font-size: 1.25rem; color: var(--gold-2); }
.card h3 { font-size: 1.05rem; color: var(--gold); }
.card p { margin: .35em 0; }
.muted { color: var(--muted); font-size: .9em; }
.tag { display: inline-block; font-size: .75rem; padding: 2px 10px; border-radius: 99px; border: 1px solid var(--line); color: var(--muted); margin-right: 6px; }
.tag.gold { color: var(--gold); border-color: rgba(245, 199, 107, .5); }

/* ---------- ปุ่ม ---------- */
.btn { min-height: 48px; padding: 10px 18px; border-radius: 14px; border: 1px solid var(--line); background: rgba(14, 42, 66, .85);
  font-weight: 500; display: inline-flex; align-items: center; justify-content: center; gap: 8px; text-decoration: none; color: var(--text); transition: transform .12s, background .2s; }
.btn:active { transform: scale(.97); }
.btn.primary { background: linear-gradient(#f7d58b, #e0a940); color: #2a1c05; font-weight: 700; border-color: transparent; }
.btn.teal { background: linear-gradient(#5fd6c8, #2f9d92); color: #04211e; font-weight: 700; border-color: transparent; }
.btn.ghost { background: transparent; }
.btn.block { width: 100%; }
.btn[disabled] { opacity: .45; pointer-events: none; }
.row { display: flex; gap: 10px; flex-wrap: wrap; }
.row > .btn { flex: 1 1 140px; }

/* ---------- หน้าแรก ---------- */
.hero { text-align: center; padding: 8px 4px 0; }
.hero .logo { font-size: 3.2rem; filter: drop-shadow(0 6px 14px rgba(0, 0, 0, .5)); animation: float 4s ease-in-out infinite; }
@keyframes float { 50% { transform: translateY(-6px) } }
.hero h1 { margin: 4px 0 2px; font-size: 1.7rem; line-height: 1.25; color: var(--gold-2); text-shadow: 0 3px 16px rgba(0, 0, 0, .65); }
.hero .ar { font-family: var(--font-ar); font-size: 1.6rem; color: var(--text); text-shadow: 0 2px 10px #000; direction: rtl; }
.hero p { margin: 4px auto 0; max-width: 34ch; color: var(--text); text-shadow: 0 1px 8px #000; }
.menu { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.menu .btn { flex-direction: column; min-height: 92px; font-size: .98rem; text-align: center; line-height: 1.3; }
.menu .btn .em { font-size: 1.9rem; }
.menu .btn.wide { grid-column: 1 / -1; min-height: 68px; flex-direction: row; font-size: 1.1rem; }
.statline { display: flex; justify-content: center; gap: 14px; flex-wrap: wrap; font-weight: 700; }
.statline span { background: rgba(0, 0, 0, .35); padding: 4px 12px; border-radius: 99px; border: 1px solid var(--line); }

/* ---------- แผนที่บท ---------- */
.chapters { display: flex; flex-direction: column; gap: 10px; position: relative; }
.chap { display: grid; grid-template-columns: 54px 1fr auto; gap: 12px; align-items: center; text-align: left; width: 100%;
  padding: 12px 14px; border-radius: 16px; border: 1px solid var(--line); background: var(--card); backdrop-filter: blur(8px); }
.chap .no { width: 54px; height: 54px; border-radius: 50%; display: grid; place-items: center; font-size: 1.6rem;
  background: radial-gradient(circle at 30% 25%, #3a7aa6, #123552); border: 2px solid rgba(255, 255, 255, .25); }
.chap.done .no { background: radial-gradient(circle at 30% 25%, #5fe0b0, #1f7a5f); }
.chap.locked { opacity: .72; filter: saturate(.6); }
.chap .t { font-weight: 700; line-height: 1.3; }
.chap .s { font-size: .82rem; color: var(--muted); line-height: 1.35; }
.chap .stars { letter-spacing: 2px; color: var(--gold); font-size: .95rem; white-space: nowrap; }

/* ---------- ฉากเล่าเรื่อง ---------- */
.progress { height: 6px; border-radius: 99px; background: rgba(255, 255, 255, .15); overflow: hidden; }
.progress > i { display: block; height: 100%; width: 0; background: linear-gradient(90deg, var(--teal), var(--gold)); transition: width .4s; }
.story { font-size: 1.05rem; }
.story .who { display: inline-block; font-size: .78rem; font-weight: 700; color: #04211e; background: var(--teal); padding: 1px 10px; border-radius: 99px; margin-bottom: 4px; }
.science { border-left: 4px solid var(--blue); }
.science ul { margin: .4em 0 .2em 1.1em; padding: 0; }
.science li { margin: .25em 0; }
.callout { border-left: 4px solid var(--gold); padding-left: 12px; font-size: .95rem; color: var(--gold-2); }

.verse { text-align: center; border: 1px solid rgba(245, 199, 107, .45); background:
  radial-gradient(circle at 50% 0, rgba(245, 199, 107, .12), transparent 60%), var(--card); position: relative; }
.verse::before, .verse::after { content: '❖'; position: absolute; top: 6px; color: var(--gold); opacity: .7; font-size: .9rem; }
.verse::before { left: 12px; } .verse::after { right: 12px; }
.ar-line { font-family: var(--font-ar); font-size: calc(1.75rem * var(--ar-scale, 1)); line-height: 2.1; direction: rtl; text-align: center; margin: .15em 0; color: #fff9e8; }
.ar-num { font-size: .7em; color: var(--gold); margin: 0 .25em; }
.verse .ref { color: var(--gold); font-weight: 700; margin-top: 6px; }
.verse .th { text-align: left; margin-top: 8px; font-size: .98rem; }
.verse .note { text-align: left; font-size: .86rem; color: var(--muted); border-top: 1px dashed var(--line); margin-top: 8px; padding-top: 8px; }
.hadith { border-left: 4px solid var(--teal); }
.hadith .ar-line { font-size: calc(1.45rem * var(--ar-scale, 1)); }
.hadith .src { font-size: .82rem; color: var(--muted); }
.mini-actions { display: flex; gap: 8px; flex-wrap: wrap; justify-content: center; margin-top: 8px; }
.mini-actions .btn { min-height: 40px; padding: 6px 14px; font-size: .9rem; }
.th-left { text-align: left; }

/* ---------- มินิเกม ---------- */
.game { padding: 12px; }
.game-stage { position: relative; width: 100%; aspect-ratio: 4 / 5; max-height: 62vh; border-radius: 14px; overflow: hidden;
  background: linear-gradient(#123a58, #0a2236); border: 1px solid var(--line); touch-action: none; user-select: none; -webkit-user-select: none; }
.game-stage canvas, .game-stage svg { width: 100%; height: 100%; display: block; }
.hud { display: flex; justify-content: space-between; gap: 8px; font-weight: 700; margin-bottom: 8px; }
.hud span { background: rgba(0, 0, 0, .35); border: 1px solid var(--line); padding: 2px 12px; border-radius: 99px; }
.hint { font-size: .88rem; color: var(--muted); text-align: center; margin: 8px 0 0; }
.slider { width: 100%; accent-color: var(--gold); height: 40px; }
.chip { display: flex; align-items: center; gap: 10px; padding: 12px 14px; border-radius: 14px; border: 1px solid var(--line); background: rgba(14, 42, 66, .8); width: 100%; text-align: left; min-height: 52px; }
.chip.sel { border-color: var(--gold); background: rgba(245, 199, 107, .14); }
.chip.ok { border-color: var(--ok); background: rgba(126, 226, 168, .16); }
.chip.bad { border-color: var(--danger); background: rgba(255, 141, 122, .14); }
.chip .n { min-width: 28px; height: 28px; border-radius: 50%; background: var(--gold); color: #201402; display: grid; place-items: center; font-weight: 700; font-size: .85rem; }
.stack { display: flex; flex-direction: column; gap: 8px; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.meter { display: flex; align-items: center; gap: 8px; font-size: .85rem; }
.meter b { min-width: 74px; }
.meter .bar { flex: 1; height: 10px; border-radius: 99px; background: rgba(255, 255, 255, .14); overflow: hidden; }
.meter .bar i { display: block; height: 100%; transition: width .5s; background: linear-gradient(90deg, var(--teal), var(--gold)); }
.meter .bar i.low { background: linear-gradient(90deg, #ff6a5a, #ffa05a); }
.feedback { margin-top: 10px; padding: 10px 12px; border-radius: 12px; font-size: .95rem; }
.feedback.ok { background: rgba(126, 226, 168, .14); border: 1px solid var(--ok); }
.feedback.bad { background: rgba(255, 141, 122, .12); border: 1px solid var(--danger); }
.feedback.info { background: rgba(91, 184, 238, .12); border: 1px solid var(--blue); }
.bigstars { font-size: 2.2rem; letter-spacing: 6px; text-align: center; color: var(--gold); }
.cert { text-align: center; border: 2px solid var(--gold); background: radial-gradient(circle at 50% 0, rgba(245, 199, 107, .18), transparent 70%), var(--card); }
.cert h2 { font-size: 1.5rem; }
.cert .nm { font-size: 1.5rem; font-weight: 700; margin: 8px 0; border-bottom: 1px dashed var(--gold); display: inline-block; padding: 0 14px 2px; }

/* ---------- ห้องรับฝน ---------- */
.rainroom label { display: block; margin: 12px 0 2px; font-weight: 700; }
.seg { display: flex; flex-wrap: wrap; gap: 8px; }
.seg .btn { flex: 1 1 30%; min-height: 44px; font-size: .9rem; padding: 6px 8px; }
.seg .btn.on { background: linear-gradient(#5fd6c8, #2f9d92); color: #04211e; font-weight: 700; border-color: transparent; }

/* ---------- ตั้งค่า / คลัง ---------- */
.setting { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.setting:last-child { border-bottom: 0; }
.setting .l { flex: 1; } .setting .l small { display: block; color: var(--muted); line-height: 1.3; }
.switch { position: relative; width: 56px; height: 32px; flex: none; }
.switch input { appearance: none; -webkit-appearance: none; position: absolute; inset: 0; margin: 0; width: 100%; height: 100%; border-radius: 99px; background: rgba(255, 255, 255, .2); border: 1px solid var(--line); cursor: pointer; transition: background .2s; }
.switch input::after { content: ''; position: absolute; top: 3px; left: 3px; width: 24px; height: 24px; border-radius: 50%; background: #fff; transition: transform .2s; }
.switch input:checked { background: var(--teal); }
.switch input:checked::after { transform: translateX(24px); }
.tabs { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 4px; }
.tabs .btn { flex: none; min-height: 40px; padding: 4px 14px; font-size: .9rem; }
.tabs .btn.on { background: var(--gold); color: #2a1c05; font-weight: 700; }
.ref-item { padding: 10px 0; border-bottom: 1px solid var(--line); font-size: .92rem; }
.ref-item:last-child { border-bottom: 0; }
.ref-item b { color: var(--gold-2); }
.lb-row { display: flex; justify-content: space-between; padding: 8px 4px; border-bottom: 1px solid var(--line); }

/* ---------- Modal / Toast ---------- */
#modal-root:empty { display: none; }
.modal-bg { position: fixed; inset: 0; z-index: 100; background: rgba(2, 8, 14, .72); display: grid; place-items: center; padding: 18px; backdrop-filter: blur(4px); }
.modal { width: min(460px, 100%); max-height: 86vh; overflow: auto; background: var(--card-solid); border: 1px solid var(--line); border-radius: 20px; padding: 20px; animation: rise .3s ease both; }
.modal h2 { margin: 0 0 8px; color: var(--gold-2); }
#toast { position: fixed; z-index: 120; left: 50%; bottom: calc(24px + var(--safe-b)); transform: translate(-50%, 30px); opacity: 0;
  background: rgba(10, 28, 44, .96); border: 1px solid var(--gold); padding: 10px 18px; border-radius: 99px; font-size: .92rem; transition: .3s; max-width: 90vw; text-align: center; pointer-events: none; }
#toast.on { opacity: 1; transform: translate(-50%, 0); }

/* ---------- ฉาก SVG แอนิเมชัน ---------- */
.cloud-drift { animation: drift 60s linear infinite; }
.cloud-drift.slow { animation-duration: 110s; }
@keyframes drift { from { transform: translateX(-12%) } to { transform: translateX(112%) } }
.sway { transform-origin: 50% 100%; animation: sway 5s ease-in-out infinite; }
@keyframes sway { 50% { transform: rotate(2.2deg) } }
.twinkle { animation: tw 3s ease-in-out infinite; }
@keyframes tw { 50% { opacity: .25 } }
.ripple { transform-box: fill-box; transform-origin: center; animation: ripple 2.8s ease-out infinite; }
@keyframes ripple { from { transform: scale(.2); opacity: .8 } to { transform: scale(1.6); opacity: 0 } }
.shimmer { animation: shimmer 4s ease-in-out infinite; }
@keyframes shimmer { 50% { opacity: .55 } }
.rise-vapor { animation: vap 5s ease-in infinite; transform-box: fill-box; }
@keyframes vap { from { transform: translateY(0); opacity: .0 } 20% { opacity: .7 } to { transform: translateY(-70px); opacity: 0 } }

@media (prefers-reduced-motion: reduce), body.calm {
  *, *::before, *::after { animation-duration: .001s !important; animation-iteration-count: 1 !important; transition-duration: .001s !important; scroll-behavior: auto !important; }
  .cloud-drift, .sway, .twinkle, .rise-vapor, .shimmer, .ripple { animation: none !important; }
}
@media (min-width: 700px) {
  body { font-size: calc(18px * var(--fs)); }
  .menu { grid-template-columns: repeat(3, 1fr); }
}

.card, .chap { scroll-margin-top: 72px; }
@media print {
  body { overflow: visible; background: #fff; }
  #scene, #rain, #flash, #bar, #eyefilter, #toast, .no-print { display: none !important; }
  #view { position: static; overflow: visible; padding: 0; }
  .cert { color: #000; background: #fff; border: 4px double #b8860b; box-shadow: none; }
  .cert * { color: #000 !important; }
}
