:root {
  --bg: #F7F5F0; --card: #FFFFFF; --line: #E6E2D8; --line2: #EDE9E0;
  --ink: #1C1B19; --muted: #6B665C; --faint: #8C877C;
  --red: #B8321F; --red-bg: #F9E9E6; --green: #1F7A5C; --green-bg: #E7F3EE; --amber: #8A5A00; --amber-bg: #FFF4E5; --blue: #1F6FB2;
  --t1: #1F6FB2; --t2: #1F7A5C; --t3: #8A5A00; --t4: #B8321F; --t5: #6B665C;
  --han: 'Noto Sans SC', 'PingFang SC', 'Heiti SC', 'Microsoft YaHei', sans-serif;
  --sat: env(safe-area-inset-top, 0px); --sab: env(safe-area-inset-bottom, 0px);
}
* { box-sizing: border-box; }
html, body { margin: 0; background: var(--bg); color: var(--ink); font-family: 'Noto Sans Thai', 'Noto Sans SC', -apple-system, system-ui, sans-serif; font-size: 16px; -webkit-text-size-adjust: 100%; }
body { min-height: 100vh; padding-top: calc(52px + var(--sat)); padding-bottom: calc(76px + var(--sab)); -webkit-tap-highlight-color: transparent; }
body.no-tabs { padding-bottom: 24px; }
a { color: var(--red); text-decoration: none; }
button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
h1, h2, h3 { margin: 0; }
.han { font-family: var(--han); }
.muted { color: var(--muted); }
.small { font-size: 13px; }
[hidden] { display: none !important; }

/* ---- แถบบน / แท็บล่าง ---- */
.topbar { position: fixed; top: 0; left: 0; right: 0; height: calc(52px + var(--sat)); padding-top: var(--sat); background: var(--card); border-bottom: 1px solid var(--line); display: flex; align-items: center; gap: 8px; padding-left: 12px; padding-right: 12px; z-index: 10; }
.brand { font-weight: 700; font-size: 17px; flex: 1; text-align: center; }
.back-btn { width: 44px; height: 44px; border: none; background: none; font-size: 30px; line-height: 1; color: var(--ink); border-radius: 12px; }
.top-right { min-width: 44px; display: flex; justify-content: flex-end; font-size: 13px; color: var(--muted); }
.tabs { position: fixed; bottom: 0; left: 0; right: 0; background: var(--card); border-top: 1px solid var(--line); display: flex; padding: 6px 4px calc(10px + var(--sab)); z-index: 10; }
.tabs a { flex: 1; display: flex; flex-direction: column; align-items: center; gap: 3px; padding: 6px 0; font-size: 11px; color: var(--muted); border-radius: 10px; }
.tabs a svg { width: 24px; height: 24px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.tabs a.active { color: var(--red); font-weight: 700; }

main { max-width: 520px; margin: 0 auto; padding: 16px 16px 8px; }
main.wide { max-width: 1100px; }

/* ---- องค์ประกอบทั่วไป ---- */
.card { background: var(--card); border: 1px solid var(--line); border-radius: 16px; padding: 16px; margin-bottom: 12px; }
.card.dark { background: var(--ink); color: #fff; border-color: var(--ink); }
.card.dark .muted { color: #CFC9BC; }
.card.locked { background: var(--line2); color: var(--muted); }
.card.link { display: block; color: inherit; }
.card.link:active { background: #FBFAF7; }
.row { display: flex; align-items: center; gap: 12px; }
.row.between { justify-content: space-between; }
.col { display: flex; flex-direction: column; gap: 4px; }
.grow { flex: 1; min-width: 0; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.grid3 { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; }
.grid4 { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; }
.stat { font-size: 28px; font-weight: 700; line-height: 1.1; }
.stat small { font-size: 14px; color: var(--muted); font-weight: 400; }
.pill { display: inline-flex; align-items: center; gap: 4px; padding: 4px 10px; border-radius: 999px; font-size: 12px; background: var(--line2); color: #4A463F; white-space: nowrap; }
.pill.red { background: var(--red); color: #fff; }
.pill.green { background: var(--green-bg); color: var(--green); }
.pill.amber { background: var(--amber-bg); color: var(--amber); }
.pill.redbg { background: var(--red-bg); color: var(--red); }
.bar { height: 8px; border-radius: 4px; background: var(--line2); overflow: hidden; margin: 8px 0 6px; }
.bar > i { display: block; height: 100%; background: var(--red); }
.bar > i.green { background: var(--green); }
.title { font-size: 20px; font-weight: 700; margin: 4px 0 12px; }
.h { font-size: 16px; font-weight: 700; margin: 16px 0 8px; }
.note { font-size: 13px; color: #4A463F; line-height: 1.5; padding: 12px 14px; border-radius: 12px; background: var(--amber-bg); border: 1px solid #F3DDB8; margin-bottom: 12px; }
.note.red { background: var(--red-bg); border-color: #F0C9C2; }
.note.green { background: var(--green-bg); border-color: #BFE0D2; }
.center { text-align: center; }

.btn { display: flex; align-items: center; justify-content: center; gap: 8px; width: 100%; min-height: 50px; padding: 0 16px; border-radius: 14px; border: 1px solid var(--line); background: var(--card); color: var(--ink); font-size: 16px; font-weight: 500; }
.btn.primary { background: var(--red); color: #fff; border-color: var(--red); font-weight: 700; }
.btn.dark { background: var(--ink); color: #fff; border-color: var(--ink); font-weight: 700; }
.btn.ghost { background: transparent; }
.btn.sm { min-height: 40px; font-size: 14px; width: auto; padding: 0 14px; border-radius: 10px; }
.btn.icon { width: 52px; min-width: 52px; padding: 0; }
.btn:disabled { opacity: .5; }
.btn:active { filter: brightness(.95); }
.btnrow { display: flex; gap: 10px; margin-top: 12px; }
.btnrow .btn { flex: 1; }
.btn svg, .ib svg { width: 20px; height: 20px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.ib { display: inline-flex; align-items: center; justify-content: center; width: 40px; height: 40px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); color: var(--red); }

label.f { display: block; font-size: 13px; font-weight: 500; color: #4A463F; margin: 10px 0 6px; }
input.f, select.f, textarea.f { width: 100%; height: 48px; border: 1px solid #D9D4C8; border-radius: 12px; padding: 0 14px; background: var(--card); font-size: 16px; }
textarea.f { height: auto; padding: 10px 14px; min-height: 90px; }
.chk { display: flex; align-items: center; gap: 8px; margin: 12px 0; }
.chk input { width: 20px; height: 20px; }

.toast { position: fixed; left: 50%; bottom: calc(96px + var(--sab)); transform: translateX(-50%); background: var(--ink); color: #fff; padding: 10px 16px; border-radius: 12px; font-size: 14px; z-index: 50; max-width: 90%; }

/* ---- เสียงวรรณยุกต์ ---- */
.t1 { color: var(--t1); } .t2 { color: var(--t2); } .t3 { color: var(--t3); } .t4 { color: var(--t4); } .t5 { color: var(--t5); }
.py { font-family: var(--han); font-weight: 500; }

/* ---- หน้าเข้าสู่ระบบ ---- */
.logo { width: 88px; height: 88px; border-radius: 24px; background: var(--red); color: #fff; display: flex; align-items: center; justify-content: center; font-family: var(--han); font-size: 52px; font-weight: 700; margin: 24px auto 12px; }

/* ---- ควิซ ---- */
.qhead { display: flex; align-items: center; justify-content: space-between; gap: 8px; margin-bottom: 8px; font-size: 13px; color: var(--muted); }
.prompt { background: var(--card); border: 1px solid var(--line); border-radius: 20px; padding: 24px 16px; text-align: center; margin-bottom: 14px; }
.prompt .big { font-family: var(--han); font-size: 80px; line-height: 1.15; font-weight: 500; }
.prompt .big.small { font-size: 56px; }
.prompt .trad { font-family: var(--han); font-size: 28px; color: var(--muted); margin-top: 4px; }
.prompt .pyq { font-family: var(--han); font-size: 40px; font-weight: 500; }
.playbtn { width: 96px; height: 96px; border-radius: 48px; border: none; background: var(--red); color: #fff; display: inline-flex; align-items: center; justify-content: center; margin: 6px 0; }
.playbtn svg { width: 44px; height: 44px; fill: none; stroke: currentColor; stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; }
.opts { display: flex; flex-direction: column; gap: 10px; }
.opts.grid { display: grid; grid-template-columns: 1fr 1fr; }
.opt { min-height: 58px; border-radius: 14px; border: 1px solid var(--line); background: var(--card); color: var(--ink); font-size: 22px; font-family: var(--han); display: flex; align-items: center; justify-content: center; gap: 6px; padding: 8px 12px; text-align: center; }
.opts.grid .opt { min-height: 96px; font-size: 38px; flex-direction: column; }
.opt .trad { font-size: 15px; color: var(--muted); }
.opt.ok { border: 2px solid var(--green); background: var(--green-bg); color: var(--green); font-weight: 700; }
.opt.bad { border: 2px solid var(--red); background: var(--red-bg); color: var(--red); }
.opt.dim { color: var(--faint); }
.opt:disabled { cursor: default; }
.info { background: var(--card); border: 1px solid var(--line); border-radius: 14px; padding: 12px 14px; margin-top: 12px; font-size: 14px; }
.info .han { font-size: 18px; }
.chips { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 6px; }
.chip { font-family: var(--han); font-size: 15px; padding: 6px 10px; border-radius: 10px; background: var(--bg); border: 1px solid var(--line); }
.chip b { color: var(--red); }
button.chip { cursor: pointer; display: inline-flex; align-items: center; gap: 6px; color: var(--ink); }
button.chip:active { background: var(--line2); }
.spk { width: 16px; height: 16px; fill: none; stroke: var(--red); stroke-width: 2; stroke-linecap: round; stroke-linejoin: round; flex-shrink: 0; }

/* ---- แฟลชการ์ด ---- */
.flash { background: var(--card); border: 1px solid var(--line); border-radius: 24px; padding: 28px 20px; min-height: 380px; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px; text-align: center; cursor: pointer; }
.flash .big { font-family: var(--han); font-size: 88px; line-height: 1.15; font-weight: 500; }
.flash .big.small { font-size: 60px; }
.flash .trad { font-family: var(--han); font-size: 24px; color: #4A463F; }
.flash .pyb { font-family: var(--han); font-size: 30px; font-weight: 500; }
.grade { display: grid; grid-template-columns: 1fr 1fr 1fr; gap: 10px; margin-top: 12px; }
.grade button { display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 2px; min-height: 62px; border-radius: 14px; border: 1px solid var(--line); background: var(--card); font-size: 16px; font-weight: 700; }
.grade small { font-size: 11px; color: var(--muted); font-weight: 400; }
.grade .g0 { color: var(--red); } .grade .g1 { color: var(--amber); } .grade .g2 { color: var(--green); }

/* ---- พินอิน ---- */
.seg { display: flex; gap: 6px; overflow-x: auto; padding-bottom: 6px; margin-bottom: 10px; -webkit-overflow-scrolling: touch; }
.seg button { flex: 0 0 auto; padding: 8px 14px; border-radius: 999px; border: 1px solid var(--line); background: var(--card); font-size: 14px; font-family: var(--han); }
.seg button.active { background: var(--ink); color: #fff; border-color: var(--ink); }
.syl { display: flex; align-items: center; gap: 8px; padding: 10px 12px; border-bottom: 1px solid var(--line2); }
.syl:last-child { border-bottom: none; }
.syl .base { font-family: var(--han); font-size: 20px; font-weight: 700; width: 64px; }
.syl .tones { display: flex; gap: 6px; flex: 1; flex-wrap: wrap; }
.tone { display: flex; flex-direction: column; align-items: center; min-width: 56px; padding: 6px 8px; border-radius: 10px; border: 1px solid var(--line); background: var(--bg); font-family: var(--han); }
.tone b { font-size: 17px; }
.tone span { font-size: 15px; color: var(--ink); }

/* ---- รายการ / ตาราง ---- */
.list .item { display: flex; align-items: center; gap: 12px; padding: 12px 14px; background: var(--card); border: 1px solid var(--line); border-bottom: none; }
.list .item:first-child { border-radius: 14px 14px 0 0; }
.list .item:last-child { border-radius: 0 0 14px 14px; border-bottom: 1px solid var(--line); }
.list .item:only-child { border-radius: 14px; }
.list .item .han { font-size: 26px; width: 64px; flex-shrink: 0; }
.tablebox { overflow-x: auto; background: var(--card); border: 1px solid var(--line); border-radius: 14px; }
table { border-collapse: collapse; width: 100%; }
th { font-size: 12px; font-weight: 500; color: var(--muted); text-align: left; padding: 10px 12px; border-bottom: 1px solid var(--line); white-space: nowrap; }
td { font-size: 14px; padding: 10px 12px; border-bottom: 1px solid var(--line2); white-space: nowrap; vertical-align: middle; }
tr:last-child td { border-bottom: none; }
td input.f { height: 36px; min-width: 160px; font-size: 14px; }
.subnav { display: flex; gap: 6px; overflow-x: auto; margin-bottom: 12px; }
.subnav a { flex: 0 0 auto; padding: 8px 14px; border-radius: 999px; border: 1px solid var(--line); background: var(--card); color: var(--ink); font-size: 14px; }
.subnav a.active { background: var(--red); color: #fff; border-color: var(--red); }

.modal { position: fixed; inset: 0; background: rgba(28, 27, 25, .5); display: flex; align-items: flex-end; justify-content: center; z-index: 40; }
.modal .box { background: var(--card); width: 100%; max-width: 520px; max-height: 92vh; overflow: auto; border-radius: 20px 20px 0 0; padding: 20px 16px calc(20px + var(--sab)); }
@media (min-width: 700px) { .modal { align-items: center; } .modal .box { border-radius: 20px; } }
.days { display: flex; gap: 4px; align-items: flex-end; height: 80px; }
.days div { flex: 1; background: var(--red); border-radius: 4px 4px 0 0; min-height: 2px; }
