/* Typing Turret TH — Premium Educational Sci-Fi */
:root {
  --f-pinky: #D946EF;
  --f-ring: #8B5CF6;
  --f-middle: #38BDF8;
  --f-index: #4ADE80;
  --f-thumb: #94A3B8;
  --bg-0: #050816;
  --bg-1: #09142E;
  --bg-2: #111C44;
  --blue: #38BDF8;
  --cyan: #22D3EE;
  --purple: #8B5CF6;
  --pink: #D946EF;
  --success: #4ADE80;
  --warning: #FBBF24;
  --text: #F1F5F9;
  --text-dim: #A5B4CF;
  --glass: rgba(17, 28, 68, 0.62);
  --glass-strong: rgba(9, 20, 46, 0.86);
  --glass-border: rgba(56, 189, 248, 0.35);
  --radius: 18px;
  --font: "Noto Sans Thai", "Sarabun", "Leelawadee UI", "Leelawadee", Tahoma, sans-serif;
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  background: var(--bg-0);
  color: var(--text);
  font-family: var(--font);
  -webkit-font-smoothing: antialiased;
  -webkit-tap-highlight-color: transparent;
}

#app { position: fixed; inset: 0; }

#game-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  touch-action: none;
}

.hidden { display: none !important; }

button { font-family: inherit; }

kbd {
  display: inline-block;
  min-width: 1.6em;
  padding: 0 .4em;
  border: 1px solid rgba(165, 180, 252, .45);
  border-bottom-width: 2px;
  border-radius: 6px;
  background: rgba(255, 255, 255, .06);
  font-family: inherit;
  font-size: .9em;
  text-align: center;
}

/* ---------- Glass panel ---------- */
.panel {
  position: relative;
  width: min(640px, 100%);
  max-height: calc(100vh - 32px);
  overflow-y: auto;
  padding: 32px 36px;
  border-radius: 24px;
  background: linear-gradient(160deg, rgba(17, 28, 68, .78), rgba(9, 20, 46, .82));
  border: 1px solid var(--glass-border);
  box-shadow: 0 0 0 1px rgba(139, 92, 246, .15) inset, 0 20px 60px rgba(0, 0, 0, .45), 0 0 50px rgba(34, 211, 238, .12);
  backdrop-filter: blur(14px);
  -webkit-backdrop-filter: blur(14px);
  text-align: center;
}

.panel-small { width: min(420px, 100%); }
.panel-results { width: min(820px, 100%); }

.screen {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 20;
  background: radial-gradient(ellipse at center, rgba(5, 8, 22, .25), rgba(5, 8, 22, .65));
  animation: fade-in .25s ease-out;
}

@keyframes fade-in { from { opacity: 0; } to { opacity: 1; } }

h2 { margin: 0 0 8px; font-size: 1.6rem; font-weight: 800; }

/* ---------- Start screen ---------- */
.logo-mark {
  width: 84px;
  height: 84px;
  margin: 0 auto 10px;
  border-radius: 50%;
  position: relative;
  background: radial-gradient(circle at 50% 40%, #fff 0 8%, var(--cyan) 18%, rgba(34, 211, 238, .25) 45%, transparent 70%);
  box-shadow: 0 0 40px rgba(34, 211, 238, .45);
  animation: core-pulse 2.4s ease-in-out infinite;
}

.logo-mark::before,
.logo-mark::after {
  content: "";
  position: absolute;
  inset: 6px;
  border-radius: 50%;
  border: 2px solid rgba(139, 92, 246, .8);
  border-left-color: transparent;
  border-right-color: transparent;
  animation: spin 6s linear infinite;
}

.logo-mark::after {
  inset: -4px;
  border-color: rgba(34, 211, 238, .6);
  border-top-color: transparent;
  border-bottom-color: transparent;
  animation-duration: 9s;
  animation-direction: reverse;
}

.logo-mark span {
  position: absolute;
  left: 50%;
  top: -14px;
  width: 6px;
  height: 34px;
  margin-left: -3px;
  border-radius: 3px;
  background: linear-gradient(var(--cyan), transparent);
}

@keyframes spin { to { transform: rotate(360deg); } }
@keyframes core-pulse { 50% { box-shadow: 0 0 60px rgba(34, 211, 238, .65); } }

.title {
  margin: 0;
  font-size: clamp(2rem, 5vw, 3.1rem);
  font-weight: 800;
  letter-spacing: .5px;
  line-height: 1.15;
  background: linear-gradient(90deg, #E0F2FE, var(--cyan) 40%, var(--purple) 80%, var(--pink));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
}

.title-th { font-weight: 800; }

.subtitle {
  margin: 6px 0 18px;
  font-size: 1.15rem;
  font-weight: 600;
  color: var(--text-dim);
}

.level-select {
  display: inline-flex;
  gap: 4px;
  margin: 0 auto 10px;
  padding: 4px;
  border-radius: 999px;
  background: rgba(5, 8, 22, .55);
  border: 1px solid rgba(139, 92, 246, .35);
}

.level-tab {
  min-height: 36px;
  padding: 4px 18px;
  border-radius: 999px;
  border: 0;
  background: transparent;
  color: var(--text-dim);
  font-size: .95rem;
  font-weight: 700;
  cursor: pointer;
}

.level-tab:hover { color: var(--text); }
.level-tab:focus-visible { outline: 3px solid var(--warning); outline-offset: 2px; }
.level-tab.is-active { background: linear-gradient(90deg, var(--purple), var(--pink)); color: #fff; }

/* หน้าเลือกด่าน */
.panel-levels { width: min(860px, 100%); }

.level-option-tag {
  padding: 1px 10px;
  border-radius: 999px;
  background: rgba(74, 222, 128, .15);
  border: 1px solid rgba(74, 222, 128, .55);
  color: #BBF7D0;
  font-size: .78rem;
  font-weight: 700;
}

.level-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 12px;
  margin-top: 6px;
}

.level-option {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 18px 12px 16px;
  border-radius: var(--radius);
  border: 1px solid rgba(139, 92, 246, .4);
  background: rgba(5, 8, 22, .6);
  color: var(--text);
  font-family: inherit;
  cursor: pointer;
  transition: transform .12s ease, border-color .2s ease, box-shadow .2s ease;
}

.level-option:hover, .level-option:focus-visible {
  transform: translateY(-3px);
  border-color: var(--cyan);
  box-shadow: 0 0 24px rgba(34, 211, 238, .3);
  outline: none;
}

.level-option.is-active { border-color: var(--pink); box-shadow: 0 0 18px rgba(217, 70, 239, .3); }

.level-option-badge {
  padding: 2px 14px;
  border-radius: 999px;
  background: linear-gradient(90deg, var(--purple), var(--pink));
  font-weight: 800;
}

.level-option-title { font-size: 1.1rem; font-weight: 800; line-height: 1.35; min-height: 2.7em; display: flex; align-items: center; }
.level-option-speed { font-size: 1.2rem; letter-spacing: 2px; }
.level-option-meta { font-size: .82rem; color: var(--text-dim); }
.level-option-hs { font-size: .85rem; font-weight: 700; color: var(--warning); min-height: 1.2em; }

.level-card {
  margin: 0 auto 16px;
  padding: 16px 18px 18px;
  border-radius: var(--radius);
  background: rgba(5, 8, 22, .55);
  border: 1px solid rgba(139, 92, 246, .4);
}

.level-badge {
  display: inline-block;
  padding: 2px 14px;
  border-radius: 999px;
  background: linear-gradient(90deg, var(--purple), var(--pink));
  font-weight: 800;
  font-size: .95rem;
}

.level-title { margin: 6px 0 10px; font-size: 1.35rem; font-weight: 800; }

.char-row {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px;
}

.char-chip {
  width: 52px;
  height: 56px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 12px;
  background: rgba(9, 20, 46, .9);
  border: 2px solid rgba(34, 211, 238, .55);
  font-size: 1.9rem;
  font-weight: 800;
  color: #fff;
  box-shadow: 0 0 14px rgba(34, 211, 238, .18);
}

.howto { margin: 0 0 18px; color: var(--text-dim); font-size: 1rem; }

/* ---------- Finger guide: แป้นพิมพ์เกษมณี + มือโปร่งใส ---------- */
.finger-guide {
  width: min(100%, 470px);
  margin: 0 auto;
}

.finger-guide.kb-large { width: min(92vw, 780px); }

.kb-stage { position: relative; }
.kb-stage.kb-has-hands { padding-bottom: 18%; overflow: hidden; }

/* u = 1/15 ของความกว้างแป้นพิมพ์ (container query unit) */
.kb { container-type: inline-size; display: flex; flex-direction: column; }
.kb-row { display: flex; }

.kb-key, .kb-gap {
  --u: calc(100cqw / 15);
  --gap: calc(var(--u) * .1);
  flex: none;
  height: calc(var(--u) - var(--gap));
  margin: 0 var(--gap) var(--gap) 0;
}

.kb-key {
  position: relative;
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  border-radius: calc(var(--u) * .14);
  background: rgba(9, 20, 46, .9);
  border: 1px solid color-mix(in srgb, var(--fg-color, #64748B) 35%, transparent);
  box-shadow: 0 2px 0 rgba(0, 0, 0, .45);
  color: rgba(226, 232, 240, .55);
  line-height: 1;
}

.kb-key b { font-size: calc(var(--u) * .46); font-weight: 700; }
.kb-key small { font-size: calc(var(--u) * .2); font-weight: 600; color: var(--text-dim); margin-top: 1px; }
.kb-special { font-size: calc(var(--u) * .24); font-weight: 600; color: rgba(165, 180, 207, .55); }

.kb-key.kb-active {
  background: color-mix(in srgb, var(--fg-color) 22%, #09142E);
  border: 2px solid var(--fg-color);
  box-shadow: 0 2px 0 rgba(0, 0, 0, .45), 0 0 12px color-mix(in srgb, var(--fg-color) 45%, transparent);
  color: #fff;
}

.kb-key.kb-active b { font-size: calc(var(--u) * .52); font-weight: 800; }
.kb-has-hands .kb-key.kb-active { justify-content: flex-start; padding-top: calc(var(--u) * .06); }
.kb-has-hands .kb-key.kb-active b { position: relative; z-index: 2; text-shadow: 0 0 4px #050816, 0 0 8px #050816; }

/* ตัวที่ต้องกด Shift: ตัวเล็กมุมซ้ายบนของแป้น */
.kb-shift-char {
  position: absolute;
  top: 6%;
  left: 9%;
  font-style: normal;
  font-size: calc(var(--u) * .36);
  font-weight: 800;
  line-height: 1;
  color: #FDE68A;
}

.kb-has-hands .kb-shift-char { z-index: 2; text-shadow: 0 0 4px #050816, 0 0 8px #050816; }

/* แป้นที่มีตัว Shift: ตัว Shift มุมซ้ายบน · ตัวหลักชิดขวาบน · ชื่อแป้นชิดขวา — ไม่ทับกัน */
.kb-key.kb-has-shift { align-items: flex-end; justify-content: flex-start; padding: calc(var(--u) * .06) calc(var(--u) * .1) 0 0; }
.kb-key.kb-active.kb-has-shift b { font-size: calc(var(--u) * .44); margin-top: calc(var(--u) * .06); }
.kb-key.kb-has-shift .kb-shift-char { top: 8%; left: 10%; }
/* ปุ่ม Shift สว่างเป็นสีส้ม เฉพาะตอนที่ตัวถัดไปต้องกด Shift */
.kb-key.kb-special.kb-shift-lit {
  color: #fff;
  border: 2px solid #FB923C;
  background: rgba(249, 115, 22, .55);
  box-shadow: 0 0 18px rgba(251, 146, 60, .85), 0 2px 0 rgba(0, 0, 0, .45);
  animation: shift-pulse 1s ease-in-out infinite;
}

@keyframes shift-pulse { 50% { box-shadow: 0 0 6px rgba(251, 146, 60, .5), 0 2px 0 rgba(0, 0, 0, .45); } }

.kb-bump::after {
  content: "";
  position: absolute;
  bottom: 8%;
  width: 30%;
  height: 2px;
  border-radius: 2px;
  background: rgba(255, 255, 255, .75);
}

.kb-bump small { margin-bottom: 4px; }

.kb-hand {
  position: absolute;
  pointer-events: none;
  filter: drop-shadow(0 0 6px rgba(34, 211, 238, .55));
}

.kb-hand-left { transform: scaleX(-1); }

.fg-pinky { --fg-color: var(--f-pinky); }
.fg-ring { --fg-color: var(--f-ring); }
.fg-middle { --fg-color: var(--f-middle); }
.fg-index { --fg-color: var(--f-index); }
.fg-thumb { --fg-color: var(--f-thumb); }

.fg-legend {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 4px 12px;
  margin-top: 8px;
  font-size: .8rem;
  color: var(--text-dim);
}

.fg-dot { display: inline-flex; align-items: center; gap: 5px; font-weight: 700; }
.fg-dot::before { content: ""; width: 10px; height: 10px; border-radius: 50%; background: var(--fg-color); }
.fg-note { flex-basis: 100%; text-align: center; }

.countdown .finger-guide {
  padding: 14px 16px 10px;
  border-radius: 18px;
  background: var(--glass-strong);
  border: 1px solid var(--glass-border);
}

.start-actions {
  display: flex;
  flex-direction: column;
  gap: 12px;
  align-items: stretch;
  max-width: 420px;
  margin: 0 auto;
}

.highscore { margin: 14px 0 0; color: var(--warning); font-weight: 700; min-height: 1em; }

.start-footer {
  margin-top: 14px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
}

.kb-hint { color: var(--text-dim); font-size: .85rem; text-align: left; flex: 1 1 260px; }
.settings-row { display: inline-flex; gap: 6px; }

/* ---------- Buttons ---------- */
.btn {
  min-height: 52px;
  padding: 10px 22px;
  border-radius: 14px;
  border: 1px solid transparent;
  font-size: 1.08rem;
  font-weight: 700;
  color: var(--text);
  cursor: pointer;
  transition: transform .12s ease, box-shadow .2s ease, background .2s ease;
}

.btn:hover { transform: translateY(-1px); }
.btn:active { transform: translateY(1px); }
.btn:focus-visible, .icon-btn:focus-visible, .field:focus-visible { outline: 3px solid var(--warning); outline-offset: 2px; }

.btn-primary {
  background: linear-gradient(90deg, #0EA5E9, var(--cyan));
  color: #04111F;
  box-shadow: 0 0 22px rgba(34, 211, 238, .35);
}

.btn-primary:hover { box-shadow: 0 0 32px rgba(34, 211, 238, .55); }

.btn-secondary {
  background: linear-gradient(90deg, rgba(139, 92, 246, .9), rgba(217, 70, 239, .85));
  box-shadow: 0 0 22px rgba(139, 92, 246, .3);
}

.btn-ghost {
  background: rgba(255, 255, 255, .06);
  border-color: rgba(165, 180, 252, .35);
}

.icon-btn {
  width: 38px;
  height: 38px;
  border-radius: 10px;
  border: 1px solid rgba(56, 189, 248, .35);
  background: rgba(5, 8, 22, .55);
  color: var(--text);
  font-size: 1rem;
  cursor: pointer;
  line-height: 1;
}

.icon-btn.is-off { opacity: .45; }

/* ---------- Form ---------- */
.panel-form { text-align: left; width: min(480px, 100%); }
.panel-form h2 { text-align: center; }
.form-note { margin: 0 0 18px; color: var(--text-dim); text-align: center; }

.field-label { display: block; margin: 12px 0 6px; font-weight: 700; }

.field {
  width: 100%;
  min-height: 50px;
  padding: 10px 14px;
  border-radius: 12px;
  border: 1px solid rgba(56, 189, 248, .45);
  background: rgba(5, 8, 22, .7);
  color: var(--text);
  font-family: inherit;
  font-size: 1.15rem;
}

#input-student-code { letter-spacing: 1px; text-transform: uppercase; }
select.field option { background: var(--bg-1); }

.checkbox {
  display: flex;
  gap: 10px;
  align-items: flex-start;
  margin: 18px 0 4px;
  color: var(--text-dim);
  cursor: pointer;
}

.checkbox input { width: 22px; height: 22px; margin: 2px 0 0; accent-color: var(--cyan); flex: none; }
.form-error { min-height: 1.4em; margin: 10px 0 0; color: var(--warning); font-weight: 600; }

.form-actions {
  display: flex;
  justify-content: center;
  flex-wrap: wrap;
  gap: 12px;
  margin-top: 18px;
}

.form-actions .btn { flex: 1 1 140px; max-width: 240px; }

/* ---------- HUD ---------- */
.hud {
  position: absolute;
  top: 10px;
  left: 50%;
  transform: translateX(-50%);
  width: min(1180px, calc(100% - 20px));
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 6px 14px;
  padding: 8px 14px;
  border-radius: 16px;
  background: var(--glass);
  border: 1px solid var(--glass-border);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
  box-shadow: 0 6px 24px rgba(0, 0, 0, .3);
  z-index: 10;
}

.hud-item { display: flex; align-items: center; gap: 6px; white-space: nowrap; }
.hud-label { font-size: .8rem; color: var(--text-dim); font-weight: 600; }
.hud-value { font-size: 1.2rem; font-weight: 800; font-variant-numeric: tabular-nums; }
.hud-small { font-size: 1rem; }

.hud-level .hud-value {
  padding: 0 10px;
  border-radius: 999px;
  background: linear-gradient(90deg, var(--purple), var(--pink));
}

/* แถวแป้นเหย้าใน HUD: ใส เรียบ มีขีดสีตามนิ้ว */
.hud-chars { gap: 3px; }

.hud-key {
  position: relative;
  min-width: 30px;
  height: 32px;
  padding: 0 3px 3px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 8px;
  background: rgba(255, 255, 255, .04);
  border: 1px solid rgba(255, 255, 255, .12);
  font-size: 1.15rem;
  font-weight: 800;
  color: #fff;
}

.hud-key::after {
  content: "";
  position: absolute;
  left: 22%;
  right: 22%;
  bottom: 3px;
  height: 3px;
  border-radius: 3px;
  background: var(--fg-color, transparent);
  opacity: .9;
}

.hud-key-off { min-width: 14px; width: 14px; background: none; border-color: rgba(255, 255, 255, .06); }
.hud-key-off::after { display: none; }

.hud-key.chip-hit { animation: chip-hit .35s ease-out; }

/* ด่าน 11–20: ตัวบน (Shift) มุมซ้ายบนของแป้นใน HUD */
.hud-key-has-shift { min-width: 36px; height: 38px; align-items: flex-end; padding-bottom: 6px; }
.hud-shift {
  position: absolute;
  top: 1px;
  left: 4px;
  font-style: normal;
  font-size: .78rem;
  font-weight: 800;
  color: #FDBA74;
}

/* หัวข้อกลุ่มด่านในหน้าเลือกด่าน */
.level-group-title {
  grid-column: 1 / -1;
  margin: 10px 0 0;
  font-size: 1rem;
  font-weight: 800;
  color: var(--muted, #A5B4FC);
  text-align: left;
}
.level-group-title:first-child { margin-top: 0; }

@keyframes chip-hit {
  0% { background: rgba(74, 222, 128, .6); transform: scale(1.18); border-color: var(--success); }
  100% { background: rgba(255, 255, 255, .04); transform: scale(1); }
}

.hud-combo.combo-hot { color: var(--warning); }

.hud-progress { flex: 1 1 160px; }

.progress {
  flex: 1;
  min-width: 70px;
  height: 10px;
  border-radius: 999px;
  background: rgba(5, 8, 22, .8);
  border: 1px solid rgba(56, 189, 248, .3);
  overflow: hidden;
}

.progress-fill {
  height: 100%;
  width: 0;
  border-radius: 999px;
  background: linear-gradient(90deg, var(--cyan), var(--success));
  transition: width .25s ease-out;
}

.hud-lives { display: inline-flex; gap: 4px; }

.life {
  width: 18px;
  height: 20px;
  background: linear-gradient(var(--cyan), var(--blue));
  clip-path: polygon(50% 0, 100% 18%, 92% 62%, 50% 100%, 8% 62%, 0 18%);
  box-shadow: 0 0 8px var(--cyan);
}

.life.life-lost { background: rgba(165, 180, 207, .25); box-shadow: none; }

.hud-buttons { display: flex; gap: 6px; margin-left: auto; }

.hud.hud-wrong { animation: hud-wrong .3s ease-out; }
.hud.hud-danger { animation: hud-danger .5s ease-out; }

@keyframes hud-wrong {
  0% { border-color: var(--pink); box-shadow: 0 0 18px rgba(217, 70, 239, .55); }
  100% { border-color: var(--glass-border); }
}

@keyframes hud-danger {
  0%, 50% { border-color: var(--warning); box-shadow: 0 0 22px rgba(251, 191, 36, .6); }
  100% { border-color: var(--glass-border); }
}

/* ---------- Feedback ---------- */
.praise {
  position: absolute;
  left: 50%;
  top: 34%;
  transform: translate(-50%, -50%);
  font-size: clamp(2rem, 5vw, 3.2rem);
  font-weight: 800;
  color: var(--success);
  text-shadow: 0 0 18px rgba(74, 222, 128, .55), 0 2px 0 rgba(5, 8, 22, .9);
  pointer-events: none;
  opacity: 0;
  z-index: 15;
  white-space: nowrap;
}

.praise.show { animation: praise 1.1s ease-out forwards; }

@keyframes praise {
  0% { opacity: 0; transform: translate(-50%, -30%) scale(.8); }
  15% { opacity: 1; transform: translate(-50%, -50%) scale(1.05); }
  70% { opacity: 1; transform: translate(-50%, -55%) scale(1); }
  100% { opacity: 0; transform: translate(-50%, -70%) scale(1); }
}

.wrong-text {
  position: absolute;
  left: 50%;
  bottom: 24%;
  transform: translateX(-50%);
  padding: 4px 16px;
  border-radius: 999px;
  background: rgba(217, 70, 239, .18);
  border: 1px solid rgba(217, 70, 239, .5);
  font-size: 1.1rem;
  font-weight: 700;
  color: #F5D0FE;
  pointer-events: none;
  opacity: 0;
  z-index: 15;
}

.wrong-text.show { animation: wrong-pop .9s ease-out forwards; }

@keyframes wrong-pop {
  0% { opacity: 0; transform: translate(-50%, 6px); }
  15% { opacity: 1; transform: translate(-50%, 0); }
  75% { opacity: 1; }
  100% { opacity: 0; }
}

.toast {
  position: absolute;
  left: 50%;
  bottom: 18px;
  transform: translateX(-50%);
  max-width: calc(100% - 32px);
  padding: 12px 20px;
  border-radius: 14px;
  background: var(--glass-strong);
  border: 1px solid rgba(251, 191, 36, .6);
  color: #FEF3C7;
  font-weight: 700;
  text-align: center;
  z-index: 30;
  animation: fade-in .2s ease-out;
}

.countdown {
  position: absolute;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 14px;
  pointer-events: none;
  z-index: 18;
}

.countdown-number {
  font-size: clamp(5rem, 16vw, 9rem);
  font-weight: 800;
  color: #fff;
  text-shadow: 0 0 30px rgba(34, 211, 238, .8);
}

.countdown-number.pop { animation: pop .5s ease-out; }

@keyframes pop {
  0% { transform: scale(1.6); opacity: 0; }
  100% { transform: scale(1); opacity: 1; }
}

.countdown-hint {
  padding: 8px 18px;
  border-radius: 999px;
  background: var(--glass-strong);
  border: 1px solid var(--glass-border);
  color: var(--text-dim);
  text-align: center;
}

/* ---------- Results ---------- */
.results-subtitle { margin: 0 0 18px; color: var(--text-dim); }

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(140px, 1fr));
  gap: 10px;
}

.stat {
  padding: 12px 10px;
  border-radius: 14px;
  background: rgba(5, 8, 22, .55);
  border: 1px solid rgba(56, 189, 248, .25);
}

.stat-accent { border-color: rgba(74, 222, 128, .55); }
.stat-value { font-size: 1.45rem; font-weight: 800; font-variant-numeric: tabular-nums; }
.stat-accent .stat-value { color: var(--success); }
.stat-label { font-size: .85rem; color: var(--text-dim); }

.section-title { margin: 22px 0 10px; font-size: 1.15rem; }

.table-wrap { overflow-x: auto; border-radius: 14px; border: 1px solid rgba(56, 189, 248, .25); }

.char-table { width: 100%; border-collapse: collapse; font-variant-numeric: tabular-nums; }
.char-table th, .char-table td { padding: 8px 10px; border-bottom: 1px solid rgba(56, 189, 248, .14); text-align: center; white-space: nowrap; }
.char-table th { font-size: .85rem; color: var(--text-dim); background: rgba(5, 8, 22, .5); }
.char-table tr:last-child td { border-bottom: none; }
.td-char { font-size: 1.6rem; font-weight: 800; }
.td-char small { display: block; font-size: .72rem; font-weight: 600; color: var(--text-dim); }
.td-good { color: var(--success); font-weight: 700; }
.td-bad { color: #F0ABFC; font-weight: 700; }

.save-status {
  margin-top: 16px;
  padding: 10px 14px;
  border-radius: 12px;
  font-weight: 700;
}

.test-prompt {
  margin-top: 16px;
  padding: 14px;
  border-radius: 14px;
  background: rgba(139, 92, 246, .14);
  border: 1px solid rgba(217, 70, 239, .55);
}

.test-prompt p { margin: 0 0 10px; font-weight: 700; }
#t-handoff { margin: 0 0 12px; }

.save-status.is-saving { background: rgba(56, 189, 248, .12); border: 1px solid rgba(56, 189, 248, .5); color: #BAE6FD; }
.save-status.is-ok { background: rgba(74, 222, 128, .12); border: 1px solid rgba(74, 222, 128, .55); color: #BBF7D0; }
.save-status.is-error { background: rgba(251, 191, 36, .1); border: 1px solid rgba(251, 191, 36, .5); color: #FEF3C7; }

/* ---------- Touch keys (tablet) ---------- */
.touch-keys {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  display: flex;
  justify-content: center;
  gap: 6px;
  padding: 8px 8px calc(8px + env(safe-area-inset-bottom));
  background: rgba(5, 8, 22, .75);
  border-top: 1px solid var(--glass-border);
  z-index: 12;
}

.touch-key {
  flex: 1 1 0;
  max-width: 84px;
  height: 58px;
  border-radius: 12px;
  border: 1px solid rgba(34, 211, 238, .55);
  background: rgba(17, 28, 68, .9);
  color: #fff;
  font-size: 1.7rem;
  font-weight: 800;
  touch-action: manipulation;
}

.touch-key:active { background: rgba(34, 211, 238, .4); }

/* ---------- Debug ---------- */
.debug-panel {
  position: absolute;
  left: 10px;
  bottom: 10px;
  margin: 0;
  padding: 8px 10px;
  border-radius: 8px;
  background: rgba(0, 0, 0, .7);
  color: #86EFAC;
  font: 12px/1.45 Consolas, monospace;
  z-index: 40;
  pointer-events: none;
  white-space: pre;
}

.noscript {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-0);
  font-size: 1.3rem;
}

/* ---------- Key test page ---------- */
.keytest-body { background: radial-gradient(ellipse at top, var(--bg-2), var(--bg-0) 70%); }
.keytest-screen { background: none; }
.char-chip.kt-ok { border-color: var(--success); background: rgba(74, 222, 128, .22); box-shadow: 0 0 16px rgba(74, 222, 128, .45); }
.kt-link { display: inline-flex; align-items: center; justify-content: center; text-decoration: none; }

/* ---------- Responsive ---------- */
@media (max-width: 900px) {
  .hud { gap: 4px 10px; padding: 6px 10px; }
  .hud-chars { order: 10; width: 100%; justify-content: center; }
  .hud-value { font-size: 1.05rem; }
}

@media (max-width: 700px) {
  .level-grid { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

@media (max-width: 560px) {
  .panel { padding: 22px 18px; }
  .char-chip { width: 44px; height: 48px; font-size: 1.6rem; }
  .hud-label { font-size: .72rem; }
}

@media (max-height: 640px) {
  .panel { padding: 18px 22px; }
  .logo-mark { width: 60px; height: 60px; }
  .subtitle { margin-bottom: 10px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { animation-duration: .01ms !important; animation-iteration-count: 1 !important; transition: none !important; }
}

/* =========================================================
   แบบทดสอบ (test.html)
   ========================================================= */
html.test-page, html.test-page body { overflow: auto; height: auto; min-height: 100%; }

.test-body {
  background: #050816 url("../assets/images/space-background.webp") center / cover fixed no-repeat;
}

.test-app {
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.test-panel { width: min(1040px, 100%); max-height: none; overflow: visible; }

.test-head { display: flex; align-items: center; justify-content: center; gap: 14px; margin-bottom: 12px; text-align: left; }
.logo-small { width: 56px; height: 56px; margin: 0; flex: none; }
.test-title { margin: 0; font-size: clamp(1.4rem, 3.4vw, 2rem); font-weight: 800; line-height: 1.25; }
.test-subtitle { margin: 2px 0 0; font-size: 1rem; }

.phase-select {
  display: inline-flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 4px;
  margin: 0 auto 16px;
  padding: 4px;
  border-radius: 999px;
  background: rgba(5, 8, 22, .55);
  border: 1px solid rgba(139, 92, 246, .35);
}

.phase-badge {
  display: inline-block;
  margin: 0 auto 16px;
  padding: 6px 18px;
  border-radius: 999px;
  background: linear-gradient(90deg, var(--purple), var(--pink));
  font-weight: 800;
}

/* หน้าเริ่มแบบทดสอบ: แป้นพิมพ์เล็ก + วิธีทำ (ซ้าย) · ฟอร์มเลือกชื่อ (ขวา) — แป้นพิมพ์ใหญ่อยู่ในหน้าทำแบบทดสอบ */
.test-intro-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.35fr) minmax(0, 1fr);
  grid-template-areas: "kb form" "howto form";
  gap: 6px 24px;
  align-items: start;
  text-align: left;
}

.test-guide { grid-area: kb; }
.test-howto-wrap { grid-area: howto; }
.test-intro-grid .test-form { grid-area: form; }
.test-guide .finger-guide { width: 100%; }

.test-howto {
  margin: 14px 0 0;
  padding-left: 1.4em;
  color: var(--text-dim);
  line-height: 1.8;
}

.test-howto strong { color: var(--text); }

.test-form {
  padding: 18px;
  border-radius: var(--radius);
  background: rgba(5, 8, 22, .5);
  border: 1px solid rgba(56, 189, 248, .25);
}

.test-form .field-label:first-child { margin-top: 0; }
.btn-block { width: 100%; margin-top: 10px; }
.btn-small { min-height: 38px; padding: 4px 14px; font-size: .95rem; }
.btn-link { display: inline-flex; align-items: center; justify-content: center; text-decoration: none; }
.test-link { display: block; margin-top: 14px; text-align: center; color: var(--text-dim); font-size: .9rem; }
.test-link:hover { color: var(--text); }

/* ระหว่างทำแบบทดสอบ */
.test-run {
  width: min(960px, 100%);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 16px;
}

.test-topbar {
  width: 100%;
  display: flex;
  align-items: center;
  gap: 12px;
  padding: 8px 14px;
  border-radius: 16px;
  background: var(--glass);
  border: 1px solid var(--glass-border);
  backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px);
}

.test-chip {
  padding: 2px 12px;
  border-radius: 999px;
  background: linear-gradient(90deg, var(--purple), var(--pink));
  font-size: .9rem;
  font-weight: 800;
  white-space: nowrap;
}

.test-progress-text { white-space: nowrap; color: var(--text-dim); font-weight: 600; }
.test-progress-text strong { color: var(--text); font-size: 1.15rem; }
.test-progress { flex: 1; }

.word-card {
  position: relative;
  width: 100%;
  padding: 26px 20px 18px;
  border-radius: 24px;
  text-align: center;
  background: linear-gradient(160deg, rgba(17, 28, 68, .8), rgba(9, 20, 46, .85));
  border: 1px solid var(--glass-border);
  box-shadow: 0 0 40px rgba(34, 211, 238, .12);
  backdrop-filter: blur(12px);
  -webkit-backdrop-filter: blur(12px);
  overflow: hidden;
  transition: border-color .2s ease, box-shadow .2s ease;
}

.word-card.is-done { border-color: var(--success); box-shadow: 0 0 40px rgba(74, 222, 128, .3); }
.word-card.is-timeout { border-color: var(--warning); }
.word-card.shake { animation: card-shake .25s ease-out; }

@keyframes card-shake {
  25% { transform: translateX(-5px); }
  50% { transform: translateX(4px); }
  75% { transform: translateX(-2px); }
}

.word-timer { position: absolute; left: 0; right: 0; top: 0; height: 5px; background: rgba(255, 255, 255, .06); }
.word-timer-fill { height: 100%; width: 100%; background: linear-gradient(90deg, var(--cyan), var(--blue)); }
.word-timer-fill.is-low { background: var(--warning); }

.word-label { color: var(--text-dim); font-weight: 600; }

.word-target {
  margin: 4px 0 8px;
  font-size: clamp(4rem, 13vw, 7.5rem);
  font-weight: 800;
  line-height: 1.3;
  color: #fff;
  text-shadow: 0 0 24px rgba(34, 211, 238, .45);
}

.word-target.word-in { animation: pop .35s ease-out; }

.word-keys { display: flex; justify-content: center; gap: 8px; min-height: 52px; }

.word-key {
  min-width: 46px;
  height: 52px;
  padding: 0 6px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border-radius: 12px;
  font-size: 1.6rem;
  font-weight: 800;
  border: 2px solid rgba(255, 255, 255, .12);
  background: rgba(255, 255, 255, .04);
  color: rgba(226, 232, 240, .45);
  transition: all .15s ease;
}

.word-key.is-done { border-color: var(--success); background: rgba(74, 222, 128, .18); color: #fff; }
.word-key.is-current { border-color: var(--cyan); color: #fff; box-shadow: 0 0 14px rgba(34, 211, 238, .45); transform: translateY(-2px); }

.word-feedback { min-height: 1.6em; margin-top: 10px; font-size: 1.15rem; font-weight: 700; color: var(--success); }
.word-card.shake .word-feedback, .word-card.is-timeout .word-feedback { color: #F5D0FE; }

.test-run .finger-guide.kb-large { width: min(100%, 860px); }

.kb-key.kb-next { animation: key-next 1s ease-in-out infinite; }

@keyframes key-next {
  50% { box-shadow: 0 0 0 3px #fff, 0 0 18px var(--fg-color, var(--cyan)); }
}

.test-panel .stats-grid { grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); }

@media (max-width: 820px) {
  .test-intro-grid { grid-template-columns: 1fr; grid-template-areas: "kb" "howto" "form"; }
  .test-topbar { flex-wrap: wrap; }
  .test-progress { flex-basis: 100%; order: 10; }
}
