:root {
  --bg: #FFF8EF; --card: #FFFFFF; --ink: #2C2C2A; --muted: #6B6A64; --line: #2C2C2A;
  --soft-line: #EADFCF; --primary: #D4537E; --on-primary: #FFFFFF; --primary-edge: #993556;
  --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: #ED93B1; --on-primary: #2C1520; --primary-edge: #B65E80;
    --good: #B6DC8A; --good-bg: #2A3A1D; --bad: #F4A6A6; --bad-bg: #452424; --chip: #38363F;
  }
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0; background: var(--bg); color: var(--ink);
  font: 17px/1.55 system-ui, -apple-system, "Noto Sans Thai", "Segoe 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; }

/* 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, .lesson:focus-visible, .pal:focus-visible, .tile:focus-visible { outline: 3px solid #378ADD; outline-offset: 2px; }
.btn[disabled] { opacity: .5; cursor: default; }
.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; 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; }

/* mascot */
.avatar { display: inline-flex; align-items: center; justify-content: center; flex: none; border-radius: 50%; background: var(--soft, #E6F1FB); }
.mascot { display: block; overflow: visible; }
.mood-yay { animation: bounce .7s ease 2; }
.mood-oops { animation: wobble .5s ease 1; }
.hop { 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, .mood-oops, .hop { 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; }
.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; }
.brand { font-weight: 700; font-size: 1.05rem; }
.stat { background: var(--chip); border-radius: 999px; padding: 2px 12px; font-size: .88rem; font-weight: 600; }
.today { margin-top: 14px; }
.today .btn { margin-top: 12px; }
.cast { display: flex; gap: 6px; 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 small { font-size: .72rem; color: var(--muted); }
.week { margin-top: 20px; }
.week h2 { font-size: 1rem; display: inline; }
.week summary { cursor: pointer; padding: 12px 0; }
.lesson { display: flex; align-items: center; gap: 12px; padding: 8px 12px 8px 8px; min-height: 60px; border: 2px solid var(--soft-line); border-radius: 16px; background: var(--card); text-decoration: none; color: inherit; margin-bottom: 8px; }
.lesson.next { border-color: var(--line); box-shadow: 0 3px 0 var(--line); }
.lesson .t { flex: 1; font-weight: 600; line-height: 1.3; }
.lesson .tick { color: var(--good); font-weight: 700; font-size: .85rem; white-space: nowrap; }

/* lesson */
.topbar { display: flex; align-items: center; gap: 6px; margin: -6px -8px 10px; }
.progress { flex: 1; height: 12px; background: var(--chip); border-radius: 99px; overflow: hidden; }
.progress i { display: block; height: 100%; background: var(--primary); border-radius: 99px; transition: width .25s; }
.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: 2.1rem; font-weight: 700; line-height: 1.25; text-align: center; overflow-wrap: anywhere; }
.center { text-align: center; }
.th { font-size: 1.1rem; text-align: center; }
.said { text-align: center; font-size: .92rem; background: var(--chip); padding: 4px 12px; border-radius: 99px; align-self: center; }
.note { font-size: .92rem; color: var(--muted); }
.play-big { width: 104px; height: 104px; align-self: center; border-radius: 50%; padding: 0; }
.play-big svg { width: 44px; height: 44px; }
.ws { display: flex; flex-wrap: wrap; justify-content: center; gap: 2px 6px; }
.tile .ws { justify-content: flex-start; }
.w { display: inline-flex; flex-direction: column; align-items: center; padding: 2px 4px; border-radius: 8px; cursor: pointer; }
.w:hover, .w:focus-visible { background: var(--chip); }
.w .h { font-size: .9em; font-weight: 500; }
.gloss { position: fixed; z-index: 20; max-width: min(280px, 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:active { transform: translateY(2px); box-shadow: none; }
.tile svg { width: 24px; height: 24px; fill: var(--primary); flex: none; }
.tile b { display: block; font-size: 1.2rem; line-height: 1.3; }
.tile span.m { font-size: .92rem; color: var(--muted); display: block; line-height: 1.35; }
.tones { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.tones .btn { flex-direction: column; min-height: 88px; gap: 2px; }
.tones svg { width: 48px; height: 32px; fill: none; stroke: currentColor; stroke-width: 4; stroke-linecap: round; stroke-linejoin: round; }
.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); }

/* 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: 13px; 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); }

/* character */
.writer { width: 260px; height: 260px; align-self: center; background: #fff; border: 2px solid var(--line); border-radius: 16px; touch-action: none;
  background-image: linear-gradient(#E5DCCB 0 0), linear-gradient(#E5DCCB 0 0); background-size: 100% 1px, 1px 100%; background-position: center; background-repeat: no-repeat; }
.fact { border-radius: 14px; padding: 10px 14px; background: var(--chip); }
.fact h3 { font-size: .8rem; color: var(--muted); font-weight: 700; }

/* 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; }
.setting input { width: 26px; height: 26px; accent-color: var(--primary); flex: none; }
.res { display: block; text-decoration: none; }
.res b { display: block; }
.tag { display: inline-block; font-size: .78rem; font-weight: 600; background: var(--chip); border-radius: 99px; padding: 0 10px; margin-top: 4px; }
.status { font-weight: 600; font-size: .92rem; }
.status.ok { color: var(--good); }
.status.no { color: var(--bad); }

#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; }
