:root {
  --bg: #FFF8EF; --card: #FFFFFF; --ink: #2C2C2A; --muted: #6B6A64; --line: #2C2C2A;
  --soft-line: #EADFCF; --primary: #1D7A6E; --on-primary: #FFFFFF; --primary-edge: #125249;
  --good: #3B6D11; --good-bg: #EAF3DE; --bad: #A32D2D; --bad-bg: #FCEBEB; --chip: #F5EDE0;
  color-scheme: light dark;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1F1E24; --card: #2B2A32; --ink: #F5F2EC; --muted: #B5B2AA; --line: #57555F;
    --soft-line: #403E48; --primary: #6FD3C4; --on-primary: #10241F; --primary-edge: #3F9C8E;
    --good: #B6DC8A; --good-bg: #2A3A1D; --bad: #F4A6A6; --bad-bg: #452424; --chip: #38363F;
  }
}

* { box-sizing: border-box; }
[hidden] { display: none !important; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 17px/1.55 system-ui, -apple-system, "PingFang SC", "Noto Sans SC", "Microsoft YaHei", "Noto Sans Thai", "Segoe UI", sans-serif;
}
[lang="th"] { font-family: "Noto Sans Thai", "Sukhumvit Set", "Thonburi", "Leelawadee UI", system-ui, sans-serif; }
#app { max-width: 520px; margin: 0 auto; min-height: 100dvh; display: flex; flex-direction: column; padding: 16px 16px calc(24px + env(safe-area-inset-bottom)); }
h1, h2, h3, p { margin: 0; }
h1 { font-size: 1.5rem; line-height: 1.3; }
h2 { font-size: 1.2rem; line-height: 1.35; }
h3 { font-size: 1rem; }
a { color: inherit; }
.muted { color: var(--muted); font-size: .92rem; }
.grow { flex: 1; }
.stack { display: flex; flex-direction: column; gap: 12px; }
.row { display: flex; gap: 10px; align-items: center; }
.row > .btn { flex: 1; }
.tight { gap: 4px; }
.center { text-align: center; }
.mt { margin-top: 12px; }

/* sticker card */
.card { background: var(--card); border: 2px solid var(--line); border-radius: 20px; padding: 16px; box-shadow: 0 3px 0 var(--line); }
.card.flat { border-color: var(--soft-line); box-shadow: none; }

.btn {
  appearance: none; font: inherit; font-weight: 600; color: var(--ink); background: var(--card);
  border: 2px solid var(--line); border-radius: 16px; box-shadow: 0 3px 0 var(--line);
  min-height: 52px; padding: 10px 16px; display: flex; align-items: center; justify-content: center; gap: 8px;
  cursor: pointer; text-decoration: none; text-align: center; width: 100%;
  touch-action: manipulation; transition: transform .08s, box-shadow .08s;
}
.btn:active { transform: translateY(3px); box-shadow: 0 0 0 var(--line); }
.btn:focus-visible, .ghost:focus-visible, .seg:focus-visible, .pal:focus-visible, .tile:focus-visible, .dayrow:focus-visible, summary:focus-visible { outline: 3px solid #378ADD; outline-offset: 2px; }
.btn[disabled] { opacity: .5; cursor: default; }
.btn.good[disabled], .btn.bad[disabled] { opacity: 1; }
.btn.primary { background: var(--primary); color: var(--on-primary); border-color: var(--primary-edge); box-shadow: 0 3px 0 var(--primary-edge); }
.btn.rec { background: var(--bad-bg); color: var(--bad); }
.btn.good { background: var(--good-bg); color: var(--good); border-color: var(--good); box-shadow: none; }
.btn.bad { background: var(--bad-bg); color: var(--bad); border-color: var(--bad); box-shadow: none; }
.btn svg { width: 22px; height: 22px; flex: none; fill: currentColor; }
.ghost { appearance: none; background: none; border: 0; font: inherit; font-weight: 600; color: var(--ink); min-width: 48px; min-height: 48px; display: inline-flex; align-items: center; justify-content: center; border-radius: 12px; cursor: pointer; text-decoration: none; }
.ghost svg { width: 24px; height: 24px; fill: currentColor; }
.link { min-height: 48px; display: inline-flex; align-items: center; font-weight: 600; }

/* check sheet */
.flags { display: grid; grid-template-columns: repeat(2, 1fr); gap: 8px; }
.flags .btn { min-height: 48px; padding: 6px; font-size: .92rem; box-shadow: none; }
.flags .btn[aria-pressed="true"] { background: var(--bad-bg); color: var(--bad); border-color: var(--bad); }
.note-in, #notes { width: 100%; font: inherit; color: var(--ink); background: var(--card); border: 2px solid var(--soft-line); border-radius: 12px; padding: 10px; min-height: 48px; }
#asks { padding-left: 32px; display: flex; flex-direction: column; gap: 6px; }
details > summary { min-height: 48px; display: flex; align-items: center; cursor: pointer; }

/* mascot */
.avatar { display: inline-flex; align-items: center; justify-content: center; flex: none; border-radius: 50%; background: var(--soft, #E6F1FB); padding: 4px; }
.avatar svg { width: 100%; height: 100%; }
.av-chaang { --soft: #DCE9F7; } .av-nok { --soft: #DDF3D6; } .av-non { --soft: #F2F0C8; } .av-plaa { --soft: #FBE3C8; } .av-som { --soft: #FFE1BF; }
.av-s { width: 48px; height: 48px; } .av-m { width: 72px; height: 72px; } .av-l { width: 96px; height: 96px; } .av-xl { width: 170px; height: 170px; }
.mascot { display: block; overflow: visible; }
.mood-yay svg { animation: bounce .7s ease 2; }
.mood-oops svg { animation: wobble .5s ease 1; }
.hop svg { animation: bounce .5s ease 1; }
@keyframes bounce { 0%, 100% { transform: translateY(0); } 40% { transform: translateY(-14px) rotate(-3deg); } 70% { transform: translateY(-3px) rotate(2deg); } }
@keyframes wobble { 0%, 100% { transform: rotate(0); } 25% { transform: rotate(-7deg); } 75% { transform: rotate(7deg); } }
@media (prefers-reduced-motion: reduce) { .mood-yay svg, .mood-oops svg, .hop svg { animation: none; } .btn { transition: none; } }

.host { display: flex; align-items: flex-end; gap: 10px; }
.bubble { position: relative; background: var(--card); border: 2px solid var(--line); border-radius: 16px; padding: 10px 14px; font-weight: 600; flex: 1; margin-bottom: 8px; min-width: 0; }
.bubble::before { content: ""; position: absolute; left: -9px; bottom: 14px; width: 14px; height: 14px; background: var(--card); border-left: 2px solid var(--line); border-bottom: 2px solid var(--line); transform: rotate(45deg); }

/* home */
.home-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 8px; gap: 8px; }
.brand { font-weight: 700; font-size: 1.05rem; }
.stat { background: var(--chip); border-radius: 999px; padding: 2px 12px; font-size: .85rem; font-weight: 600; white-space: nowrap; }
.today { margin-top: 14px; }
.today .btn { margin-top: 12px; }
.cast { display: flex; gap: 4px; justify-content: space-between; margin: 18px 0 4px; }
.pal { appearance: none; background: none; border: 0; padding: 4px 0; font: inherit; color: inherit; flex: 1; min-width: 0; display: flex; flex-direction: column; align-items: center; gap: 2px; cursor: pointer; border-radius: 12px; }
.pal b { font-size: .85rem; }
.pal small { font-size: .72rem; color: var(--muted); }
.unit { margin-top: 14px; border: 2px solid var(--soft-line); border-radius: 18px; background: var(--card); padding: 0 12px; }
.unit > summary { cursor: pointer; padding: 12px 0; min-height: 48px; font-weight: 700; display: flex; justify-content: space-between; align-items: center; gap: 8px; list-style: none; }
.unit > summary::-webkit-details-marker, .wk > summary::-webkit-details-marker { display: none; }
.unit > summary::after, .wk > summary::after { content: "▾"; color: var(--muted); }
.unit[open] > summary::after, .wk[open] > summary::after { content: "▴"; }
.unit > summary span:first-child, .wk > summary span:first-child { flex: 1; }
.wk { border-top: 2px solid var(--soft-line); }
.wk > summary { cursor: pointer; padding: 10px 0; min-height: 48px; font-weight: 600; display: flex; justify-content: space-between; align-items: center; gap: 8px; list-style: none; }
.wk-off { border-top: 2px solid var(--soft-line); padding: 12px 0; color: var(--muted); }
.dayrow { display: flex; align-items: center; gap: 10px; min-height: 52px; padding: 8px 10px; margin-bottom: 8px; border: 2px solid var(--soft-line); border-radius: 14px; text-decoration: none; color: inherit; line-height: 1.3; }
.dayrow.next { border-color: var(--line); box-shadow: 0 3px 0 var(--line); }
.dayrow .t { flex: 1; min-width: 0; }
.tick { color: var(--good); font-weight: 700; font-size: .85rem; white-space: nowrap; }

/* day */
.seg { display: flex; align-items: center; gap: 12px; padding: 8px 12px 8px 8px; min-height: 68px; border: 2px solid var(--line); border-radius: 16px; background: var(--card); text-decoration: none; color: inherit; box-shadow: 0 2px 0 var(--line); }
.seg.done { border-color: var(--soft-line); box-shadow: none; }
.seg .t { flex: 1; font-weight: 600; line-height: 1.3; min-width: 0; }
.seg .t small { display: block; font-weight: 400; color: var(--muted); font-size: .85rem; }

/* lesson */
.topbar { display: flex; align-items: center; gap: 6px; margin: -6px -8px 10px; }
progress { flex: 1; width: 100%; height: 12px; appearance: none; border: 0; background: var(--chip); border-radius: 99px; overflow: hidden; }
progress::-webkit-progress-bar { background: var(--chip); }
progress::-webkit-progress-value { background: var(--primary); }
progress::-moz-progress-bar { background: var(--primary); }
.foot { position: sticky; bottom: 0; padding: 12px 0 calc(4px + env(safe-area-inset-bottom)); background: var(--bg); margin-top: auto; display: flex; flex-direction: column; gap: 10px; }
.body { display: flex; flex-direction: column; gap: 14px; padding-bottom: 14px; }
.big { font-size: 1.9rem; font-weight: 700; line-height: 1.25; text-align: center; overflow-wrap: anywhere; }
.big-th { font-size: 2.3rem; font-weight: 700; line-height: 1.5; text-align: center; }
.mean { font-size: 1.1rem; text-align: center; }
.note { font-size: .92rem; color: var(--muted); }
.play-big { width: 104px; height: 104px; align-self: center; border-radius: 50%; padding: 0; flex: none; }
.play-big svg { width: 44px; height: 44px; }
.ws { display: flex; flex-wrap: wrap; justify-content: center; gap: 2px 6px; }
.tile .ws, .line .ws { justify-content: flex-start; }
.w { display: inline-flex; flex-direction: column; align-items: center; padding: 2px 4px; border-radius: 8px; cursor: pointer; min-height: 48px; justify-content: center; }
.w:hover, .w:focus-visible { background: var(--chip); }
.w .h { font-size: 1em; font-weight: 600; line-height: 1.4; }
.w .r { font-size: .72em; font-weight: 400; color: var(--muted); line-height: 1.3; min-height: 1em; }
.big .w .r { font-size: .5em; }
.gloss { position: fixed; z-index: 20; max-width: min(300px, calc(100vw - 16px)); background: var(--card); border: 2px solid var(--primary); border-radius: 12px; padding: 8px 12px; line-height: 1.4; box-shadow: 0 4px 14px rgba(0, 0, 0, .3); }
.tiles { display: flex; flex-direction: column; gap: 10px; }
.tile { appearance: none; font: inherit; color: inherit; text-align: left; background: var(--card); border: 2px solid var(--line); border-radius: 16px; padding: 10px 14px; min-height: 60px; display: flex; align-items: center; gap: 12px; cursor: pointer; width: 100%; box-shadow: 0 2px 0 var(--line); touch-action: manipulation; }
.tile.now { border-color: var(--primary); background: var(--chip); }
.tile > svg { width: 24px; height: 24px; fill: var(--primary); flex: none; }
.tile b { display: block; font-size: 1.15rem; line-height: 1.3; font-weight: 600; }
.tile .m { font-size: .92rem; color: var(--muted); display: block; line-height: 1.35; }
.tile .x { min-width: 0; flex: 1; }
.who { flex: none; width: 28px; height: 28px; border-radius: 50%; background: var(--chip); display: inline-flex; align-items: center; justify-content: center; font-weight: 700; font-size: .85rem; }
.tones { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
.tones .btn { flex-direction: column; min-height: 80px; gap: 2px; padding: 8px 4px; font-size: .92rem; }
.tones svg { width: 48px; height: 32px; fill: none; stroke: currentColor; stroke-width: 4; stroke-linecap: round; stroke-linejoin: round; }
.opt-th { display: flex; flex-direction: column; line-height: 1.3; }
.opt-th small { font-weight: 400; color: var(--muted); }
.feedback { display: flex; gap: 10px; align-items: center; border-radius: 16px; padding: 8px 12px; font-weight: 600; }
.feedback.ok { background: var(--good-bg); color: var(--good); }
.feedback.no { background: var(--bad-bg); color: var(--bad); }
.tray { display: flex; flex-wrap: wrap; gap: 8px; justify-content: center; min-height: 64px; padding: 8px; border-radius: 14px; }
.tray.answer { border: 2px dashed var(--soft-line); }
.chip { appearance: none; font: inherit; color: var(--ink); background: var(--card); border: 2px solid var(--line); border-radius: 12px; min-height: 48px; min-width: 48px; padding: 4px 10px; cursor: pointer; display: inline-flex; flex-direction: column; align-items: center; line-height: 1.25; font-weight: 600; }
.chip small { font-weight: 400; color: var(--muted); font-size: .72rem; }
.fact { border-radius: 14px; padding: 10px 14px; background: var(--chip); font-size: .95rem; }
.fact h3 { font-size: .8rem; color: var(--muted); font-weight: 700; }

/* tone chart */
.chart { width: 100%; height: auto; display: block; background: var(--card); border: 2px solid var(--soft-line); border-radius: 16px; }
.chart .ideal { fill: none; stroke: var(--muted); stroke-width: 3; stroke-dasharray: 6 6; stroke-linecap: round; }
.chart .you { fill: none; stroke: var(--primary); stroke-width: 4; stroke-linecap: round; stroke-linejoin: round; }
.chart text { fill: var(--muted); font-size: 12px; text-anchor: middle; }
.chart .grid { stroke: var(--soft-line); stroke-width: 1; }
.legend { display: flex; flex-wrap: wrap; gap: 4px 14px; font-size: .85rem; color: var(--muted); }
.legend i { display: inline-block; width: 22px; height: 0; border-top: 3px dashed var(--muted); vertical-align: middle; margin-right: 6px; }
.legend i.y { border-top: 4px solid var(--primary); }
.player { width: 100%; }

/* romanization key */
.key { width: 100%; border-collapse: collapse; font-size: .95rem; }
.key td { padding: 6px 6px; border-top: 1px solid var(--soft-line); vertical-align: middle; }
.key td:first-child { font-weight: 700; white-space: nowrap; width: 4.5em; }
.key td:nth-child(2) { white-space: nowrap; width: 5.5em; }

/* checklist */
.check { display: flex; gap: 12px; align-items: flex-start; padding: 12px; min-height: 52px; border: 2px solid var(--soft-line); border-radius: 14px; background: var(--card); cursor: pointer; }
.check input { width: 24px; height: 24px; margin: 1px 0 0; accent-color: var(--primary); flex: none; }
.check:has(input:checked) { border-color: var(--good); background: var(--good-bg); }

/* settings */
.head { display: flex; align-items: center; gap: 6px; margin: -6px -8px 12px; }
.setting { display: flex; gap: 12px; justify-content: space-between; align-items: center; min-height: 52px; }
.setting input { width: 26px; height: 26px; accent-color: var(--primary); flex: none; }
.status { font-weight: 600; font-size: .92rem; }
.status.ok { color: var(--good); }
.status.no { color: var(--bad); }
.finish { justify-content: center; align-items: center; text-align: center; }

#toast { position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%); background: var(--ink); color: var(--bg); padding: 10px 16px; border-radius: 14px; max-width: calc(100vw - 32px); opacity: 0; pointer-events: none; transition: opacity .2s; font-size: .95rem; text-align: center; }
#toast.show { opacity: 1; }
