/* ==========================================================
   มีซาน — ตาชั่งแห่งตลาด | style.css
   โทนสี: ทองเหลือง (ตาชั่ง) + สนิมเขียวคราม + ปูนขาวเทา + น้ำเงินครามกลางคืน
   ========================================================== */
:root {
  --brass: #b9862c;
  --brass-hi: #e0b45a;
  --verdi: #2e7d70;
  --madder: #a33a3a;
  --font-ui: 'IBM Plex Sans Thai', 'Noto Sans Thai', 'Sarabun', system-ui, -apple-system, 'Segoe UI', sans-serif;
  --font-read: 'Noto Serif Thai', 'Sarabun', 'Noto Sans Thai', Georgia, serif;
  --font-ar: 'Amiri', 'Scheherazade New', 'Noto Naskh Arabic', 'Traditional Arabic', serif;
  --fs: 1;
  --r: 14px;
  --gap: 16px;
}
[data-theme='day'] {
  --bg: #e4e8e0; --bg2: #d7ddd2; --panel: rgba(244, 246, 241, .93); --ink: #17271f; --ink2: #44574c;
  --line: rgba(23, 39, 31, .16); --accent: #8d6414; --accent-ink: #fff; --ok: #1f6d4d; --bad: #a33a3a; --chip: #cfd8cb;
  color-scheme: light;
}
[data-theme='night'] {
  --bg: #0d1528; --bg2: #14203a; --panel: rgba(16, 26, 48, .92); --ink: #eaeef7; --ink2: #a6b2cb;
  --line: rgba(234, 238, 247, .16); --accent: #e0b45a; --accent-ink: #1a1405; --ok: #5ccf9f; --bad: #ff8a80; --chip: #1e2d4d;
  color-scheme: dark;
}
[data-theme='eye'] {
  --bg: #2a2418; --bg2: #352d1d; --panel: rgba(46, 38, 24, .94); --ink: #e9d9b4; --ink2: #bca97f;
  --line: rgba(233, 217, 180, .16); --accent: #d9a441; --accent-ink: #2a1d05; --ok: #a6c77a; --bad: #e59a7d; --chip: #3d3322;
  color-scheme: dark;
}

* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { height: 100%; margin: 0; }
body {
  background: var(--bg); color: var(--ink); font-family: var(--font-ui);
  font-size: calc(16px * var(--fs)); line-height: 1.65; overflow: hidden;
  overscroll-behavior: none; touch-action: manipulation;
}
button { font: inherit; color: inherit; cursor: pointer; }
a { color: var(--accent); }
:focus-visible { outline: 3px solid var(--brass-hi); outline-offset: 2px; border-radius: 6px; }
.sr { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }

#app { position: fixed; inset: 0; display: flex; flex-direction: column; }
#scene { position: absolute; inset: 0; overflow: hidden; background: var(--bg2); }
#scene .layer { position: absolute; top: 0; left: 0; height: 100%; will-change: transform; pointer-events: none; }
#scene .layer svg { display: block; width: 100%; height: 100%; }
#scene .hot { position: absolute; z-index: 6; }
#scene.dim::after { content: ''; position: absolute; inset: 0; background: rgba(5, 9, 20, .38); pointer-events: none; }
#warm { position: fixed; inset: 0; pointer-events: none; z-index: 90; background: #ff9a2e; mix-blend-mode: multiply; opacity: 0; transition: opacity .4s; }

/* ---------- แถบด้านบน ---------- */
#hud {
  position: relative; z-index: 10; display: flex; align-items: center; gap: 8px;
  padding: calc(8px + env(safe-area-inset-top)) 12px 8px; background: linear-gradient(var(--panel), transparent);
}
#hud[hidden] { display: none; }
.ibtn {
  width: 44px; height: 44px; border-radius: 50%; border: 1px solid var(--line); background: var(--panel);
  display: grid; place-items: center; font-size: 1.15rem; flex: none;
}
.ibtn:active { transform: scale(.94); }
#hudTitle { flex: 1; min-width: 0; line-height: 1.25; }
#hudTitle b { display: block; font-family: var(--font-read); font-size: .98rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#hudTitle span { display: block; font-size: .78rem; color: var(--ink2); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
#miniScale { width: 92px; height: 44px; flex: none; }
#stats { display: flex; gap: 6px; font-size: .78rem; flex: none; }
.stat { background: var(--panel); border: 1px solid var(--line); border-radius: 999px; padding: 2px 9px; white-space: nowrap; }
.stat i { font-style: normal; opacity: .75; }

/* ---------- เนื้อหา ---------- */
#view { position: relative; z-index: 5; flex: 1; min-height: 0; overflow-y: auto; overflow-x: hidden; -webkit-overflow-scrolling: touch; }
.page { max-width: 760px; margin: 0 auto; padding: 8px var(--gap) calc(28px + env(safe-area-inset-bottom)); }
.card {
  background: var(--panel); border: 1px solid var(--line); border-radius: var(--r); padding: 14px 16px; margin: 0 0 12px;
  backdrop-filter: blur(6px); -webkit-backdrop-filter: blur(6px);
}
h1, h2, h3 { font-family: var(--font-read); line-height: 1.3; margin: 0 0 .4em; font-weight: 700; }
h2 { font-size: 1.3rem; } h3 { font-size: 1.05rem; }
p { margin: 0 0 .7em; }
.muted { color: var(--ink2); font-size: .88rem; }
.btn {
  display: inline-flex; align-items: center; justify-content: center; gap: 8px; min-height: 48px; padding: 10px 18px;
  border-radius: 12px; border: 1px solid var(--line); background: var(--panel); font-weight: 600; text-align: center;
}
.btn.pri { background: var(--accent); color: var(--accent-ink); border-color: transparent; }
.btn.ghost { background: transparent; }
.btn.block { display: flex; width: 100%; margin-bottom: 10px; }
.btn:active { transform: translateY(1px); }
.btn[disabled] { opacity: .45; pointer-events: none; }
.row { display: flex; gap: 8px; flex-wrap: wrap; align-items: center; }
.tabs { display: flex; gap: 6px; overflow-x: auto; padding: 4px 0 10px; scrollbar-width: none; }
.tabs button { flex: none; padding: 8px 14px; border-radius: 999px; border: 1px solid var(--line); background: var(--chip); font-size: .9rem; }
.tabs button[aria-selected='true'] { background: var(--accent); color: var(--accent-ink); border-color: transparent; font-weight: 700; }
.ar { font-family: var(--font-ar); direction: rtl; text-align: right; font-size: calc(1.55rem * var(--fs)); line-height: 2.15; margin: .2em 0 .3em; color: var(--ink); }
.ae { display: inline-block; width: .55em; height: .55em; margin: 0 .35em; border-radius: 50%; border: 2px solid var(--brass); vertical-align: middle; }
.ref { font-size: .8rem; color: var(--ink2); }
.ref a { word-break: break-all; }
.badge { display: inline-block; font-size: .72rem; padding: 1px 8px; border-radius: 999px; background: var(--chip); border: 1px solid var(--line); margin-inline-end: 4px; }
.ok { color: var(--ok); } .bad { color: var(--bad); }

/* ---------- หน้าแรก ---------- */
.home { min-height: 100%; display: flex; flex-direction: column; justify-content: center; padding-top: calc(10px + env(safe-area-inset-top)); }
.hero { text-align: center; margin: 4px 0 14px; }
.hero svg { width: min(78vw, 340px); height: auto; filter: drop-shadow(0 10px 22px rgba(0, 0, 0, .35)); }
.hero h1 { font-size: clamp(2rem, 9vw, 3rem); margin: 4px 0 2px; color: #f4e7c4; text-shadow: 0 2px 14px rgba(0, 0, 0, .55); letter-spacing: .01em; }
.hero p { color: #e9e1cc; text-shadow: 0 1px 8px rgba(0, 0, 0, .6); max-width: 30em; margin-inline: auto; }
.menu { max-width: 430px; margin: 0 auto; width: 100%; }
.menu .btn { justify-content: flex-start; padding-inline: 16px; }
.menu .btn small { margin-inline-start: auto; opacity: .7; font-weight: 400; }

/* ---------- แผ่นเรื่องเล่า (ใต้จอเกม) ---------- */
#sheet {
  position: relative; z-index: 8; margin-top: auto; max-height: 64%; overflow-y: auto;
  background: var(--panel); border-top: 1px solid var(--line); border-radius: 20px 20px 0 0;
  padding: 16px var(--gap) calc(18px + env(safe-area-inset-bottom)); box-shadow: 0 -12px 34px rgba(0, 0, 0, .28);
  backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
}
#sheet[hidden] { display: none; }
.story { font-family: var(--font-read); font-size: calc(1.04rem * var(--fs)); line-height: 1.95; max-width: 40em; margin: 0 auto; }
.story.q { font-weight: 600; }
.opt {
  display: block; width: 100%; text-align: left; padding: 12px 14px; margin: 8px 0; border-radius: 12px; font-size: .98rem;
  background: var(--chip); border: 1px solid var(--line); line-height: 1.55; min-height: 48px;
}
.opt:active { transform: scale(.99); }
.opt.right { border-color: var(--ok); background: color-mix(in srgb, var(--ok) 16%, var(--chip)); }
.opt.wrong { border-color: var(--bad); background: color-mix(in srgb, var(--bad) 14%, var(--chip)); }
.fb { margin: 10px 0 0; padding: 10px 12px; border-radius: 10px; border-inline-start: 4px solid var(--brass); background: color-mix(in srgb, var(--brass) 12%, transparent); font-size: .95rem; }
.fx { display: flex; gap: 8px; flex-wrap: wrap; margin: 8px 0 0; font-size: .82rem; }
.fx span { padding: 1px 9px; border-radius: 999px; background: var(--chip); border: 1px solid var(--line); }
.fx .up { color: var(--ok); } .fx .dn { color: var(--bad); }
.nav { display: flex; justify-content: space-between; gap: 8px; margin-top: 12px; }
.nav .btn { flex: 1; }

/* ---------- ไพ่ความรู้ ---------- */
.verse { border-inline-start: 3px solid var(--brass); padding-inline-start: 12px; margin: 10px 0 14px; }
.verse .th { font-family: var(--font-read); line-height: 1.85; }
.audiobar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; margin-top: 6px; }
.audiobar select { max-width: 100%; padding: 8px; border-radius: 10px; border: 1px solid var(--line); background: var(--chip); color: var(--ink); font: inherit; font-size: .86rem; }
.pbtn { min-height: 40px; padding: 6px 14px; border-radius: 999px; border: 1px solid var(--line); background: var(--accent); color: var(--accent-ink); font-weight: 600; font-size: .88rem; }
.pbtn.alt { background: var(--chip); color: var(--ink); }
.tbl { width: 100%; border-collapse: collapse; font-size: .9rem; }
.tbl th, .tbl td { text-align: left; padding: 8px 6px; border-bottom: 1px solid var(--line); vertical-align: top; }
.bar { height: 10px; border-radius: 6px; background: var(--chip); overflow: hidden; margin-top: 4px; }
.bar i { display: block; height: 100%; background: linear-gradient(90deg, var(--verdi), var(--brass)); }

/* ---------- มินิเกม / โมดัล ---------- */
.overlay { position: fixed; inset: 0; z-index: 60; background: rgba(4, 8, 18, .72); display: flex; align-items: flex-end; justify-content: center; }
.overlay[hidden] { display: none; }
.dlg {
  width: min(680px, 100%); max-height: 94dvh; overflow-y: auto; background: var(--bg); color: var(--ink);
  border-radius: 20px 20px 0 0; padding: 16px var(--gap) calc(20px + env(safe-area-inset-bottom)); border: 1px solid var(--line);
}
@media (min-width: 720px) { .overlay { align-items: center; } .dlg { border-radius: 20px; } }
.dlg header { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
.dlg header h2 { flex: 1; margin: 0; font-size: 1.1rem; }
canvas.stage { width: 100%; height: auto; display: block; border-radius: 14px; border: 1px solid var(--line); background: #0d1528; touch-action: none; }
.hold { user-select: none; -webkit-user-select: none; touch-action: none; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; margin: 8px 0; }
.chip { padding: 8px 14px; border-radius: 999px; border: 1px solid var(--line); background: var(--chip); font-weight: 600; min-height: 42px; }
.chip.on { background: var(--accent); color: var(--accent-ink); }
.res { margin-top: 10px; padding: 12px; border-radius: 12px; background: var(--chip); border: 1px solid var(--line); }
input[type='range'] { width: 100%; accent-color: var(--accent); height: 36px; }
input[type='text'], input[type='password'], input[type='number'], select.f {
  width: 100%; padding: 12px; border-radius: 10px; border: 1px solid var(--line); background: var(--chip); color: var(--ink); font: inherit; min-height: 48px;
}
label.lbl { display: block; margin: 10px 0 4px; font-weight: 600; font-size: .9rem; }
.sw { display: flex; align-items: center; justify-content: space-between; gap: 12px; padding: 10px 0; border-bottom: 1px solid var(--line); }
.sw input[type='checkbox'] { width: 24px; height: 24px; accent-color: var(--accent); }
.spot svg { width: 100%; height: auto; display: block; border-radius: 14px; background: #0d1528; }
.spot .it { cursor: pointer; }
.spot .it .ring { fill: none; stroke: var(--brass-hi); stroke-width: 4; stroke-dasharray: 8 6; opacity: 0; }
.spot .it.mark .ring { opacity: 1; }
.spot .it.hit .ring { stroke: #5ccf9f; opacity: 1; stroke-dasharray: none; }
.spot .it.miss .ring { stroke: #ff8a80; opacity: 1; stroke-dasharray: none; }
.spot .it.fp .ring { stroke: #ffb74d; opacity: 1; stroke-dasharray: none; }
.hs {
  position: absolute; width: 48px; height: 48px; margin: -24px 0 0 -24px; border-radius: 50%; border: 2px solid var(--brass-hi);
  background: rgba(15, 24, 48, .72); color: #fff; display: grid; place-items: center; font-size: 1.3rem; z-index: 7;
  animation: pulse 2.6s ease-out infinite; padding: 0;
}
.hs.seen { border-color: #5ccf9f; animation: none; opacity: .85; }
@keyframes pulse { 0% { box-shadow: 0 0 0 0 rgba(224, 180, 90, .55); } 70% { box-shadow: 0 0 0 16px rgba(224, 180, 90, 0); } 100% { box-shadow: 0 0 0 0 rgba(224, 180, 90, 0); } }
.pan { position: absolute; bottom: 12px; z-index: 9; }
.toast { position: fixed; left: 50%; top: calc(112px + env(safe-area-inset-top)); transform: translateX(-50%); z-index: 120; max-width: 90vw; background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 999px; font-size: .9rem; box-shadow: 0 8px 24px rgba(0, 0, 0, .35); }
.toast[hidden] { display: none; }
.rest { text-align: center; }
.rest .big { font-size: 3rem; font-family: var(--font-read); }

@media (prefers-reduced-motion: reduce) { *, *::before, *::after { animation: none !important; transition: none !important; } }
html.nomotion *, html.nomotion *::before, html.nomotion *::after { animation: none !important; transition: none !important; }
@media (max-width: 560px) { #hud { flex-wrap: wrap; row-gap: 4px; } #stats { order: 3; flex-basis: 100%; justify-content: center; } }
@media (max-width: 340px) { #miniScale { display: none; } }
@media (min-width: 900px) { #sheet { max-width: 760px; margin-inline: auto; width: 100%; border-radius: 20px 20px 0 0; } }
