:root {
  --cream: #fff8e6; --paper: #fffdf6; --ink: #16423c; --ink2: #4d6b5f; --leaf: #2e9d5b; --leaf2: #217a45;
  --mango: #ffc93c; --mango2: #f5a623; --orange: #f57c2b; --red: #e5484d; --sky: #bfe9ff; --line: #e8dcc0;
  font-size: clamp(12px, calc(min(2.1vh, 1.5vw) + 5px), 19px);
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--cream); color: var(--ink);
  font-family: "Sarabun", "Noto Sans Thai", "Leelawadee UI", "Thonburi", "Tahoma", system-ui, sans-serif; -webkit-tap-highlight-color: transparent; }
body { touch-action: manipulation; overscroll-behavior: none; }
button { font: inherit; color: inherit; cursor: pointer; border: 0; background: none; }
.hidden { display: none !important; }
#app { position: fixed; inset: 0; display: flex; flex-direction: column;
  background: radial-gradient(circle at 12% 0%, #fff1c2 0, transparent 40%), radial-gradient(circle at 100% 100%, #d9f3df 0, transparent 45%), var(--cream); }

/* header */
#top { flex: none; display: flex; align-items: center; gap: .6rem; padding: .35rem .7rem; background: var(--leaf); color: #fff;
  box-shadow: 0 3px 0 var(--leaf2); position: relative; z-index: 5; }
#brand { display: flex; align-items: center; gap: .35rem; font-weight: 800; font-size: 1.25rem; white-space: nowrap; }
#brand img { width: 2.2rem; height: 2.2rem; object-fit: contain; filter: drop-shadow(0 2px 0 rgba(0,0,0,.15)); }
#brand small { display: block; font-size: .6rem; font-weight: 600; opacity: .85; letter-spacing: .05em; line-height: 1; }
#brand span { line-height: 1.05; }
#tabs { display: flex; gap: .25rem; background: rgba(0,0,0,.15); padding: .2rem; border-radius: 999px; margin: 0 auto; }
#tabs button { padding: .3rem .9rem; border-radius: 999px; font-weight: 700; color: #eafff0; }
#tabs button.on { background: var(--mango); color: var(--ink); box-shadow: 0 2px 0 var(--mango2); }
#tools { display: flex; align-items: center; gap: .35rem; }
.chip { display: flex; align-items: center; gap: .35rem; padding: .25rem .6rem; border-radius: 999px; background: rgba(0,0,0,.15); font-size: .8rem; font-weight: 600; white-space: nowrap; }
.chip .dot { width: .75rem; height: .75rem; border-radius: 50%; background: #c6d4cc; box-shadow: inset 0 0 0 2px rgba(0,0,0,.15); }
.chip.on .dot { background: var(--mango); }
.ico { width: 2.1rem; height: 2.1rem; border-radius: 50%; background: rgba(0,0,0,.15); background-repeat: no-repeat; background-position: center; background-size: 60%; flex: none; }
#mute { background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath fill='%23fff' d='M3 9h4l5-4v14l-5-4H3z'/%3E%3Cpath d='M16 8a5 5 0 0 1 0 8M18.5 5.5a8.5 8.5 0 0 1 0 13' stroke='%23fff' stroke-width='2' fill='none' stroke-linecap='round'/%3E%3C/svg%3E"); }
#mute.off { background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath fill='%23fff' d='M3 9h4l5-4v14l-5-4H3z'/%3E%3Cpath d='M16 9l6 6M22 9l-6 6' stroke='%23fff' stroke-width='2' stroke-linecap='round'/%3E%3C/svg%3E"); }
#music { background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath fill='%23fff' d='M9 17.5A2.5 2.5 0 1 1 7 15V4l12-2v12.5A2.5 2.5 0 1 1 17 12V6.3L9 7.6z'/%3E%3C/svg%3E"); }
#music.off { opacity: .45; }

/* views */
#main { flex: 1; min-height: 0; position: relative; }
.view { position: absolute; inset: 0; display: none; flex-direction: column; align-items: center; padding: .5rem .7rem .4rem; gap: .4rem; }
.view.on { display: flex; }
.bar { flex: none; width: 100%; max-width: 62rem; display: flex; align-items: center; gap: .6rem; }
.stat { display: flex; flex-direction: column; align-items: center; background: var(--paper); border: 2px solid var(--line); border-radius: .8rem; padding: .15rem .8rem; min-width: 5rem; }
.stat b { font-size: 1.35rem; line-height: 1.1; }
.stat span { font-size: .7rem; color: var(--ink2); }
#tStats .btn { margin-left: auto; }

.btn { display: inline-flex; align-items: center; justify-content: center; gap: .4rem; padding: .55rem 1.3rem; border-radius: 1rem; font-weight: 800;
  background: var(--orange); color: #fff; box-shadow: 0 4px 0 #c55a14; font-size: 1.05rem; transition: transform .08s; white-space: nowrap; }
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 #c55a14; }
.btn.big { font-size: 1.35rem; padding: .7rem 2.2rem; }
.btn.small { font-size: .85rem; padding: .3rem .8rem; box-shadow: 0 3px 0 #c55a14; }
.btn.ghost { background: var(--paper); color: var(--ink); box-shadow: 0 4px 0 var(--line); border: 2px solid var(--line); }
.btn.small.ghost { box-shadow: 0 3px 0 var(--line); }
.kc { display: inline-block; min-width: 1.6em; padding: .05em .35em; margin: 0 .1em; border-radius: .35em; background: #fff; border: 2px solid var(--line);
  border-bottom-width: 4px; font-weight: 700; text-align: center; line-height: 1.3; color: var(--ink); }
.hint { margin: 0; font-size: .85rem; color: var(--ink2); flex: none; text-align: center; }

/* test text */
#tBox { flex: none; width: 100%; max-width: 62rem; height: calc(2 * 2.6rem + 1rem); overflow: hidden; background: var(--paper); border: 3px solid var(--line);
  border-radius: 1.1rem; padding: .5rem .9rem; position: relative; box-shadow: 0 4px 0 var(--line); }
#tWords { display: flex; flex-wrap: wrap; gap: 0 .9rem; font-size: 1.6rem; line-height: 2.6rem; transition: transform .2s; }
.w { position: relative; border-radius: .4rem; padding: 0 .15rem; white-space: nowrap; color: #6b7f77; }
.w.cur { background: #fff0b8; color: var(--ink); box-shadow: inset 0 -3px 0 var(--mango); }
.w.ok { color: var(--leaf); }
.w.bad { color: var(--red); text-decoration: line-through; text-decoration-thickness: 2px; }
.w .c.ok { color: var(--leaf); }
.w .c.bad { color: #fff; background: var(--red); border-radius: .2rem; }
.w .c.part { color: var(--orange); }
.inrow { flex: none; width: 100%; max-width: 62rem; display: flex; gap: .5rem; align-items: center; }
.tin { flex: 1; min-width: 0; font: inherit; font-size: 1.4rem; padding: .35rem .9rem; border-radius: .9rem; border: 3px solid var(--leaf); background: #fff; color: var(--ink);
  outline: none; box-shadow: 0 4px 0 #bfe3cb; }
.tin.err { border-color: var(--red); animation: shake .25s; }
@keyframes shake { 25% { transform: translateX(-5px); } 75% { transform: translateX(5px); } }
.ghostin { position: absolute; left: 0; top: 0; width: 1px; height: 1px; opacity: 0; font-size: 16px; pointer-events: none; }

/* keyboard */
.kbwrap { flex: 1 1 auto; min-height: 0; width: 100%; display: flex; align-items: center; justify-content: center; }
.kb { --u: 40px; --kh: 40px; display: flex; flex-direction: column; gap: calc(var(--u) * .08); padding: calc(var(--u) * .12); background: #2f5b4f; border-radius: calc(var(--u) * .3);
  box-shadow: 0 calc(var(--u) * .1) 0 #1d3d34; user-select: none; -webkit-user-select: none; touch-action: none; }
.kr { display: flex; gap: calc(var(--u) * .08); }
.kb { gap: calc(var(--kh) * .08); }
.k { position: relative; width: calc(var(--u) * .92); height: calc(var(--kh) * .86); border-radius: calc(var(--u) * .16); background: #fdf9ee; color: var(--ink);
  box-shadow: 0 calc(var(--u) * .08) 0 #c9bfa3, inset 0 calc(var(--u) * -.07) 0 var(--fc, transparent); display: flex; align-items: center; justify-content: center;
  font-size: min(calc(var(--u) * .5), calc(var(--kh) * .44)); font-weight: 700; line-height: 1; }
.k .s { position: absolute; left: calc(var(--u) * .1); top: calc(var(--u) * .05); font-size: calc(var(--u) * .25); color: #8a9a92; font-weight: 600; }
.k.w15 { width: calc(var(--u) * 1.42); } .k.w175 { width: calc(var(--u) * 1.67); } .k.w2 { width: calc(var(--u) * 1.92); }
.k.w225 { width: calc(var(--u) * 2.17); } .k.w275 { width: calc(var(--u) * 2.67); } .k.sp { width: calc(var(--u) * 7.2); }
.k.fn { font-size: calc(var(--u) * .24); color: #5d6f68; background: #ece4cf; }
.k.home::after { content: ""; position: absolute; bottom: calc(var(--u) * .14); width: calc(var(--u) * .22); height: 2px; border-radius: 2px; background: #9aa59f; }
.k.next { background: var(--fc); color: #fff; transform: translateY(-2px); box-shadow: 0 calc(var(--u) * .08) 0 rgba(0,0,0,.25), 0 0 0 3px #fff; animation: pulse 1s infinite; }
.k.next .s { color: rgba(255,255,255,.85); }
.k.shiftnext { background: var(--fc); color: #fff; }
.k.down { transform: translateY(2px); filter: brightness(.9); }
.k.wrong { background: var(--red) !important; color: #fff; }
.k.latched { background: var(--mango); }
@keyframes pulse { 50% { box-shadow: 0 calc(var(--u) * .08) 0 rgba(0,0,0,.25), 0 0 0 6px rgba(255,255,255,.5); } }
.kb.fcol .k:not(.fn) { background: color-mix(in srgb, var(--fc) 22%, #fdf9ee); }
.kb.fcol .k.next { background: var(--fc); }

/* overlays / panels */
.overlay { position: absolute; inset: 0; display: none; align-items: center; justify-content: center; padding: .6rem; background: rgba(22,66,60,.35); z-index: 10; }
.overlay.on { display: flex; }
.panel { position: relative; background: var(--paper); border: 3px solid var(--line); border-radius: 1.4rem; box-shadow: 0 6px 0 var(--line), 0 12px 30px rgba(0,0,0,.18);
  padding: 1rem 1.3rem; max-width: 46rem; width: 100%; max-height: 100%; overflow: auto; display: flex; flex-direction: column; gap: .5rem; }
.panel.start { flex-direction: row; align-items: center; gap: 1rem; max-width: 44rem; }
.panel.small { max-width: 30rem; align-items: center; text-align: center; }
.puk { width: 9rem; height: auto; flex: none; animation: bob 2.4s ease-in-out infinite; }
@keyframes bob { 50% { transform: translateY(-6px) rotate(-2deg); } }
.stxt { display: flex; flex-direction: column; gap: .45rem; min-width: 0; }
h1, h2 { margin: 0; line-height: 1.15; color: var(--leaf2); }
h1 { font-size: 1.9rem; } h2 { font-size: 1.5rem; }
h1 small, h2 small { display: block; font-size: .55em; color: var(--orange); }
.intro { margin: 0; font-size: .95rem; line-height: 1.45; }
.intro.small { font-size: .85rem; }
.opts { display: flex; flex-wrap: wrap; gap: .5rem; }
.seg { display: inline-flex; background: #efe6cf; border-radius: 999px; padding: .2rem; gap: .15rem; }
.seg button { padding: .3rem .85rem; border-radius: 999px; font-weight: 700; font-size: .9rem; color: var(--ink2); }
.seg button.on { background: var(--leaf); color: #fff; box-shadow: 0 2px 0 var(--leaf2); }
.btns { display: flex; gap: .6rem; flex-wrap: wrap; justify-content: center; }
.stxt .btns { justify-content: flex-start; }
.btns.sticky { position: sticky; bottom: -1rem; background: var(--paper); padding: .4rem 0 .2rem; margin-top: auto; }
.best { margin: 0; font-size: .85rem; color: var(--ink2); min-height: 1em; }
.keys { margin: 0; font-size: .8rem; color: var(--ink2); }

.result { max-width: 40rem; }
.rtop { display: flex; align-items: center; justify-content: center; gap: 1.2rem; }
.big { display: flex; flex-direction: column; align-items: center; }
.big b { font-size: 3.6rem; line-height: 1; color: var(--orange); }
.big span { font-weight: 700; }
.grade { font-size: 1.4rem; font-weight: 800; padding: .3rem 1rem; border-radius: 1rem; background: var(--mango); box-shadow: 0 3px 0 var(--mango2); }
.newbest { margin-top: .3rem; text-align: center; font-weight: 800; color: var(--leaf); }
.rstats { display: grid; grid-template-columns: repeat(4, 1fr); gap: .4rem; width: 100%; }
.rstats.three { grid-template-columns: repeat(3, 1fr); }
.rstats div { background: #f6efdc; border-radius: .8rem; padding: .3rem; display: flex; flex-direction: column; align-items: center; }
.rstats b { font-size: 1.3rem; } .rstats span { font-size: .72rem; color: var(--ink2); }
.ladder { display: flex; width: 100%; border-radius: .8rem; overflow: hidden; font-size: .7rem; font-weight: 700; position: relative; }
.ladder div { flex: 1; padding: .3rem .2rem; text-align: center; color: var(--ink); line-height: 1.2; }
.ladder div.me { outline: 3px solid var(--ink); outline-offset: -3px; }
.ladder div small { display: block; font-weight: 600; opacity: .75; }
.note { margin: 0; font-size: .82rem; color: var(--ink2); text-align: center; line-height: 1.4; }

/* lessons */
.picker { max-width: 52rem; }
.phead { display: flex; align-items: center; justify-content: space-between; gap: .6rem; flex-wrap: wrap; }
#lGrid { display: grid; grid-template-columns: repeat(auto-fill, minmax(10.5rem, 1fr)); gap: .45rem; }
.lc { text-align: left; background: #fff; border: 2px solid var(--line); border-radius: .9rem; padding: .4rem .6rem; box-shadow: 0 3px 0 var(--line); display: flex; flex-direction: column; gap: .1rem; }
.lc:hover { border-color: var(--leaf); }
.lc .n { font-size: .7rem; color: var(--ink2); font-weight: 700; }
.lc .t { font-weight: 800; font-size: .92rem; line-height: 1.2; }
.lc .st { color: var(--mango2); font-size: .9rem; letter-spacing: .05em; }
.lc .st i { color: #ddd3bb; font-style: normal; }
.lname { font-weight: 800; font-size: 1rem; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 40%; }
.prog { flex: 1; height: .7rem; background: #efe6cf; border-radius: 999px; overflow: hidden; }
.prog i { display: block; height: 100%; width: 0; background: var(--leaf); transition: width .2s; }
.lline { flex: none; width: 100%; max-width: 62rem; background: var(--paper); border: 3px solid var(--line); border-radius: 1.1rem; box-shadow: 0 4px 0 var(--line);
  padding: .4rem .8rem; font-size: 2.1rem; line-height: 3rem; text-align: center; white-space: nowrap; overflow: hidden; letter-spacing: .04em; }
.lline .c { padding: 0 .02em; border-radius: .25rem; }
.lline .c.done { color: var(--leaf); }
.lline .c.cur { background: #fff0b8; box-shadow: inset 0 -4px 0 var(--mango); }
.lline .c.todo { color: #8f9c96; }
.lline .c.sp { display: inline-block; min-width: .5em; }
.lline .c.sp.cur { min-width: .7em; }
.stars { font-size: 2.6rem; color: var(--mango); letter-spacing: .1em; text-shadow: 0 3px 0 var(--mango2); }
.stars i { color: #e5dcc4; font-style: normal; text-shadow: none; }

/* game */
#gStage { position: relative; flex: 1 1 auto; min-height: 0; width: 100%; max-width: 62rem; border-radius: 1.1rem; overflow: hidden; border: 3px solid var(--line); background: #bfe9ff; }
#cv { position: absolute; inset: 0; width: 100%; height: 100%; display: block; }
#gHud { position: absolute; left: .5rem; right: .5rem; top: .4rem; display: flex; gap: .5rem; align-items: center; pointer-events: none; font-weight: 800; }
#gHud span { background: rgba(255,253,246,.9); border-radius: 999px; padding: .1rem .7rem; box-shadow: 0 2px 0 rgba(0,0,0,.12); }
#gScore { margin-left: auto; color: var(--orange); }
#gHearts { color: var(--red); letter-spacing: .05em; }
.inrow.game .tin { font-size: 1.25rem; }
#vGame .kbwrap { flex: 0 1 auto; }

/* short frames */
@media (max-height: 560px) {
  #top { padding: .2rem .6rem; }
  #brand img { width: 1.8rem; height: 1.8rem; }
  .view { padding: .35rem .6rem .3rem; gap: .3rem; }
  .ico { width: 1.8rem; height: 1.8rem; }
  .stat { padding: 0 .6rem; flex-direction: row; gap: .3rem; align-items: baseline; }
  .stat b { font-size: 1.1rem; }
  #tBox { height: calc(2 * 2.1rem + .7rem); padding: .3rem .8rem; }
  #tWords { font-size: 1.3rem; line-height: 2.1rem; }
  .tin { font-size: 1.15rem; padding: .2rem .8rem; }
  .lline { font-size: 1.7rem; line-height: 2.4rem; padding: .2rem .8rem; }
  .puk { width: 7rem; }
  h1 { font-size: 1.5rem; } h2 { font-size: 1.25rem; }
  .panel { padding: .7rem 1rem; gap: .35rem; }
  .big b { font-size: 2.8rem; }
  .btn.big { font-size: 1.15rem; padding: .5rem 1.6rem; }
}
@media (max-height: 420px) {
  .hint { display: none; }
  .intro { font-size: .85rem; line-height: 1.3; }
  .puk { width: 5.5rem; }
  .keys { display: none; }
  .ladder div small { display: none; }
  .note { font-size: .75rem; }
}
@media (max-width: 600px) {
  #top { flex-wrap: wrap; justify-content: space-between; row-gap: .25rem; }
  #tabs { order: 3; width: 100%; justify-content: center; margin: 0; }
  #tabs button { flex: 1; }
  .chip .lbl { font-size: .72rem; }
  .panel.start { flex-direction: column; text-align: center; }
  .stxt .btns { justify-content: center; }
  .opts { justify-content: center; }
  .puk { width: 6rem; }
  .stat { min-width: 0; padding: .1rem .5rem; }
  .rstats { grid-template-columns: repeat(2, 1fr); }
  .lname { max-width: 30%; font-size: .85rem; }
  #tWords { font-size: 1.35rem; }
}
@media (max-width: 400px) {
  #brand small { display: none; }
  .chip .lbl { display: none; }
}
#vGame .kbwrap { flex: none; height: 34%; }
@media (max-width: 600px) and (min-height: 640px) {
  #tBox { height: calc(4 * 2.6rem + 1rem); }
  .kbwrap { align-items: flex-end; padding-bottom: .4rem; }
  .lline { font-size: 2.4rem; line-height: 3.6rem; }
}
@media (min-height: 900px) and (min-width: 1300px) {
  #tBox { height: calc(3 * 2.6rem + 1rem); }
  #tWords { font-size: 1.8rem; }
}
