:root {
  --bg: #eaf4ff;
  --card: #ffffff;
  --ink: #15304d;
  --muted: #5a7390;
  --line: #d3e2f2;
  --primary: #1d6fd6;
  --primary-ink: #ffffff;
  --sun: #ffb703;
  --ok: #178a55;
  --ok-bg: #dff5e8;
  --bad: #d92d3a;
  --bad-bg: #ffe0e2;
  --prog: #1d6fd6;
  --cur-bg: #fff0b3;
  --radius: 18px;
  --u: clamp(24px, 4.1vw, 46px);   /* cỡ 1 phím */

  --f-lp: #ffd3d3; --f-lr: #ffe2b8; --f-lm: #fff3a8; --f-li: #cdf2c5;
  --f-ri: #bfe9f5; --f-rm: #d0dcff; --f-rr: #ecd2ff; --f-rp: #ffcfe6; --f-th: #e6ebf0;
  --fs-lp: #f06a6a; --fs-lr: #f2a22d; --fs-lm: #e6c600; --fs-li: #43b92f;
  --fs-ri: #1eaed1; --fs-rm: #5a7ef2; --fs-rr: #b46af0; --fs-rp: #ee5ea3; --fs-th: #7b8a99;
}

* { box-sizing: border-box; }
html { -webkit-text-size-adjust: 100%; }
body {
  margin: 0;
  background: var(--bg);
  color: var(--ink);
  font-family: "Segoe UI", "Trebuchet MS", system-ui, -apple-system, Roboto, Arial, sans-serif;
  line-height: 1.45;
  min-height: 100vh;
}
[hidden] { display: none !important; }
button { font: inherit; }

.top {
  display: flex; align-items: center; justify-content: space-between; gap: 16px;
  padding: 12px 20px; background: var(--card); border-bottom: 2px solid var(--line);
  position: sticky; top: 0; z-index: 5;
}
.brand { display: flex; align-items: center; gap: 10px; color: var(--ink); text-decoration: none; font-weight: 800; font-size: 1.25rem; }
.who { display: flex; align-items: center; gap: 8px; font-weight: 600; color: var(--muted); }
.who input {
  font: inherit; color: var(--ink); padding: 8px 12px; border: 2px solid var(--line); border-radius: 12px; width: min(220px, 46vw);
}
.who input:focus { outline: 3px solid #a9ccf7; border-color: var(--primary); }

main { max-width: 1080px; margin: 0 auto; padding: 22px 16px 60px; }
h1 { font-size: clamp(1.5rem, 3.2vw, 2.2rem); margin: 0 0 6px; }
h2 { margin: 0; }
.muted { color: var(--muted); margin: 0; }
.card { background: var(--card); border: 2px solid var(--line); border-radius: var(--radius); padding: 16px 18px; }
.warn { color: var(--bad); font-weight: 700; }

.hero { margin: 8px 0 18px; }
.hero p { margin: 0; font-size: 1.05rem; color: var(--muted); max-width: 62ch; }

.settings { display: grid; grid-template-columns: 1.6fr 1fr; gap: 18px; margin-bottom: 20px; }
fieldset { border: 0; margin: 0; padding: 0; display: grid; gap: 8px; }
legend { font-weight: 800; padding: 0; margin-bottom: 6px; }
.opt { display: flex; gap: 10px; align-items: flex-start; padding: 8px 10px; border: 2px solid var(--line); border-radius: 12px; cursor: pointer; }
.opt:has(input:checked) { border-color: var(--primary); background: #eef5ff; }
.opt small { display: block; color: var(--muted); }
.opt input { margin-top: 5px; width: 18px; height: 18px; accent-color: var(--primary); }
.toggles { display: grid; gap: 10px; align-content: start; font-weight: 600; }
.toggles input { width: 18px; height: 18px; accent-color: var(--primary); margin-right: 6px; }

/* Lưới level */
.level-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 14px; }
.lv-card {
  position: relative; text-align: left; background: var(--card); border: 2px solid var(--line); border-radius: var(--radius);
  padding: 14px 14px 12px; cursor: pointer; display: grid; gap: 6px; color: inherit;
  transition: transform .12s ease, border-color .12s ease, box-shadow .12s ease;
}
.lv-card:hover:not(:disabled) { transform: translateY(-3px); border-color: var(--primary); box-shadow: 0 8px 18px rgba(29,111,214,.14); }
.lv-card:focus-visible { outline: 3px solid #a9ccf7; }
.lv-card:disabled { cursor: not-allowed; background: #f4f7fb; color: #8ea1b6; }
.lv-num {
  width: 44px; height: 44px; border-radius: 14px; display: grid; place-items: center;
  background: var(--sun); color: #3a2a00; font-weight: 900; font-size: 1.3rem;
}
.lv-card:disabled .lv-num { background: #dbe4ee; color: #8ea1b6; }
.lv-title { font-weight: 800; }
.lv-sub { font-size: .88rem; color: var(--muted); min-height: 2.4em; }
.lv-prog { display: flex; align-items: center; justify-content: space-between; font-size: .85rem; color: var(--muted); font-weight: 600; }
.star { color: #cfd9e4; font-size: 1.1rem; letter-spacing: 1px; }
.star.on { color: var(--sun); }
.lock-tag { font-size: .8rem; font-weight: 700; color: #7b8fa6; }
.foot-note { color: var(--muted); font-size: .9rem; margin-top: 18px; }

/* Level */
.link-btn { background: none; border: 0; color: var(--primary); font-weight: 700; cursor: pointer; padding: 6px 4px; border-radius: 8px; }
.link-btn:hover { text-decoration: underline; }
.link-btn.center { display: block; margin: 10px auto 0; }
.level-head { display: flex; gap: 14px; align-items: center; margin: 8px 0 12px; }
.level-num { width: 62px; height: 62px; border-radius: 18px; background: var(--sun); display: grid; place-items: center; font-size: 2rem; font-weight: 900; color: #3a2a00; }
.tip { background: #fff8dc; border: 2px solid #ffe08a; border-radius: 14px; padding: 12px 14px; margin-bottom: 16px; }
.lesson-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(260px, 1fr)); gap: 14px; }
.ls-card { background: var(--card); border: 2px solid var(--line); border-radius: var(--radius); padding: 14px; display: grid; gap: 8px; }
.ls-top { display: flex; justify-content: space-between; align-items: center; font-weight: 800; }
.ls-preview { font-size: .95rem; color: var(--muted); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.ls-best { font-size: .85rem; color: var(--muted); min-height: 1.3em; }

.btn {
  background: var(--primary); color: var(--primary-ink); border: 0; border-radius: 14px; padding: 11px 20px;
  font-weight: 800; cursor: pointer; box-shadow: 0 3px 0 #124f9c; transition: transform .08s ease, box-shadow .08s ease;
}
.btn:hover { filter: brightness(1.06); }
.btn:active { transform: translateY(2px); box-shadow: 0 1px 0 #124f9c; }
.btn:focus-visible { outline: 3px solid #a9ccf7; outline-offset: 2px; }
.btn.ghost { background: #fff; color: var(--primary); border: 2px solid var(--primary); box-shadow: 0 3px 0 #b8d3f5; }

/* Luyện gõ */
.play-bar { display: flex; align-items: center; gap: 14px; flex-wrap: wrap; margin-bottom: 12px; }
.play-title { font-weight: 800; flex: 1; min-width: 160px; }
.stats { display: flex; gap: 10px; }
.stat { background: var(--card); border: 2px solid var(--line); border-radius: 14px; padding: 6px 14px; text-align: center; min-width: 84px; }
.stat-v { display: block; font-size: 1.35rem; font-weight: 900; font-variant-numeric: tabular-nums; }
.stat-l { font-size: .78rem; color: var(--muted); font-weight: 600; }

.type-panel {
  position: relative; background: var(--card); border: 3px solid var(--line); border-radius: 22px;
  padding: clamp(16px, 3vw, 28px); min-height: 150px; cursor: text;
}
.type-panel.focus { border-color: var(--primary); box-shadow: 0 0 0 5px rgba(29,111,214,.15); }
.target {
  font-size: clamp(1.5rem, 3.4vw, 2.4rem); font-weight: 700; line-height: 1.9; letter-spacing: .02em;
  word-break: break-word; font-variant-ligatures: none;
}
.ch { display: inline-block; min-width: .55em; text-align: center; border-bottom: 3px solid transparent; border-radius: 6px; color: #7c93ac; white-space: pre; }
.ch.sp { min-width: .5em; }
.ch.ok { color: var(--ok); }
.ch.bad { color: var(--bad); background: var(--bad-bg); border-bottom-color: var(--bad); }
.ch.prog { color: var(--prog); border-bottom-color: #9bc2f4; }
.ch.cur { color: var(--ink); background: var(--cur-bg); border-bottom-color: var(--sun); }
.ch.sp.bad::before, .ch.sp.cur::before { content: "·"; }
.ch.shake { animation: shake .22s linear; }
@keyframes shake { 25% { transform: translateX(-3px); } 75% { transform: translateX(3px); } }

.cap { position: absolute; inset: 0; width: 100%; height: 100%; opacity: 0; cursor: text; border: 0; background: transparent; font-size: 16px; }
.blur-hint {
  position: absolute; inset: 0; border-radius: 19px; display: grid; place-items: center; font-weight: 800; font-size: 1.15rem;
  background: rgba(234,244,255,.86); color: var(--primary); opacity: 0; pointer-events: none; transition: opacity .15s ease;
}
.type-panel.blurred .blur-hint { opacity: 1; }

.hint {
  margin: 14px 0 6px; font-size: 1.1rem; font-weight: 700; min-height: 2.6em;
  display: flex; flex-wrap: wrap; gap: 8px; align-items: center;
}
.hint .kcap {
  display: inline-grid; place-items: center; min-width: 2em; padding: 2px 10px; border-radius: 10px; font-weight: 900;
  background: #fff; border: 2px solid var(--ink); box-shadow: 0 3px 0 var(--ink);
}
.hint .finger { color: var(--muted); font-weight: 600; }
.hint .bad { color: var(--bad); }
.warn-box { background: #fff1e6; border: 2px solid #ffc38a; color: #7a3d00; border-radius: 12px; padding: 10px 14px; margin: 6px 0; font-weight: 600; }

/* Bàn phím */
.kb-wrap { margin-top: 14px; overflow-x: auto; padding-bottom: 6px; }
.kb { display: grid; gap: 5px; width: max-content; margin: 0 auto; padding: 12px; background: #dde9f6; border-radius: 18px; }
.krow { display: flex; gap: 5px; }
.key {
  height: var(--u); min-width: var(--u); border-radius: 9px; display: grid; place-items: center;
  font-weight: 800; font-size: calc(var(--u) * .4); color: #37506b; background: var(--f-th);
  border-bottom: 3px solid rgba(0,0,0,.18); position: relative; user-select: none;
}
.key small { position: absolute; bottom: 2px; left: 50%; transform: translateX(-50%); width: 40%; height: 3px; background: rgba(0,0,0,.35); border-radius: 2px; }
.key.f-lp { background: var(--f-lp); } .key.f-lr { background: var(--f-lr); } .key.f-lm { background: var(--f-lm); } .key.f-li { background: var(--f-li); }
.key.f-ri { background: var(--f-ri); } .key.f-rm { background: var(--f-rm); } .key.f-rr { background: var(--f-rr); } .key.f-rp { background: var(--f-rp); }
.key.f-th { background: var(--f-th); }
.key.next { color: #fff; transform: translateY(-3px) scale(1.06); box-shadow: 0 0 0 3px var(--ink), 0 8px 14px rgba(0,0,0,.25); z-index: 1; animation: pulse 1s ease-in-out infinite; }
.key.next.f-lp { background: var(--fs-lp); } .key.next.f-lr { background: var(--fs-lr); } .key.next.f-lm { background: var(--fs-lm); color: #3a3000; }
.key.next.f-li { background: var(--fs-li); } .key.next.f-ri { background: var(--fs-ri); } .key.next.f-rm { background: var(--fs-rm); }
.key.next.f-rr { background: var(--fs-rr); } .key.next.f-rp { background: var(--fs-rp); } .key.next.f-th { background: var(--fs-th); }
.key.press { filter: brightness(.88); transform: translateY(2px); }
@keyframes pulse { 50% { transform: translateY(-3px) scale(1.12); } }

.legend { display: flex; flex-wrap: wrap; gap: 6px 12px; justify-content: center; margin-top: 10px; font-size: .82rem; font-weight: 600; color: var(--muted); }
.legend span { display: inline-flex; align-items: center; gap: 5px; }
.legend i { width: 14px; height: 14px; border-radius: 5px; border: 1px solid rgba(0,0,0,.2); display: inline-block; }
.play-actions { margin-top: 16px; text-align: center; }

/* Kết quả */
.modal { position: fixed; inset: 0; background: rgba(21,48,77,.55); display: grid; place-items: center; padding: 16px; z-index: 20; }
.modal-card { background: #fff; border-radius: 26px; padding: 26px 24px 18px; width: min(460px, 100%); text-align: center; animation: pop .25s ease; }
@keyframes pop { from { transform: scale(.9); opacity: 0; } }
.stars { font-size: 3rem; letter-spacing: 6px; line-height: 1; margin-bottom: 4px; }
.stars .star { font-size: 3rem; }
.res-grid { display: grid; grid-template-columns: repeat(3, 1fr); gap: 10px; margin: 18px 0; }
.res-grid div { background: #f1f7fe; border-radius: 14px; padding: 10px 6px; }
.res-grid b { display: block; font-size: 1.5rem; }
.res-grid span { font-size: .8rem; color: var(--muted); font-weight: 600; }
.modal-actions { display: flex; gap: 10px; justify-content: center; flex-wrap: wrap; }

@media (max-width: 760px) {
  .settings { grid-template-columns: 1fr; }
  .brand-text { font-size: 1.05rem; }
  .who span { display: none; }
}
@media (prefers-reduced-motion: reduce) {
  .key.next, .ch.shake, .modal-card, .lv-card { animation: none; transition: none; }
}

/* Ảnh bìa đầu trang */
.cover {
  position: relative; max-width: 1080px; margin: 16px auto 0; aspect-ratio: 8 / 3; overflow: hidden;
  border-radius: 26px; border: 3px solid #fff; box-shadow: 0 10px 26px rgba(21,48,77,.18);
  background: linear-gradient(135deg, #ffb703 0%, #fb8500 45%, #1d6fd6 100%);
}
.cover img { width: 100%; height: 100%; object-fit: cover; object-position: 70% 40%; display: block; }
.cover.no-img img { display: none; }
.cover::after { content: ""; position: absolute; inset: 0; background: linear-gradient(90deg, rgba(10,24,44,.55) 0%, rgba(10,24,44,0) 55%); pointer-events: none; }
.cover-text { position: absolute; left: clamp(14px, 3.5vw, 36px); bottom: clamp(12px, 3vw, 32px); z-index: 1; display: grid; gap: 4px; color: #fff; text-shadow: 0 2px 10px rgba(0,0,0,.45); }
.cover-kicker { font-weight: 800; letter-spacing: .08em; text-transform: uppercase; font-size: clamp(.7rem, 1.6vw, .95rem); color: #ffe08a; }
.cover-title { font-weight: 900; font-size: clamp(1.6rem, 5vw, 3.2rem); line-height: 1.05; }
body.playing .cover { display: none; }
@media (max-width: 760px) { .cover { margin: 10px 12px 0; border-radius: 18px; } }

/* Chào mừng level: ảnh bay ra */
.intro { position: fixed; inset: 0; z-index: 30; display: grid; place-items: center; background: radial-gradient(circle at 50% 45%, rgba(255,183,3,.55), rgba(21,48,77,.88) 70%); animation: fadeIn .2s ease; overflow: hidden; }
@keyframes fadeIn { from { opacity: 0; } }
.intro.out { animation: fadeOut .35s ease forwards; }
@keyframes fadeOut { to { opacity: 0; } }
.intro-card { position: relative; width: min(300px, 62vw); animation: flyIn 1.05s cubic-bezier(.2,.9,.25,1.1) both; }
.intro-card img { width: 100%; aspect-ratio: 2 / 3; object-fit: cover; border-radius: 22px; border: 6px solid #fff; box-shadow: 0 18px 40px rgba(0,0,0,.45); display: block; background: #fff3c4; }
.intro-card.no-img img { display: none; }
.intro-card.no-img { width: min(300px, 70vw); }
.intro-ribbon {
  position: absolute; left: 50%; bottom: -26px; transform: translateX(-50%); width: 118%; text-align: center;
  background: #ffb703; color: #3a2a00; border-radius: 16px; padding: 8px 10px 10px; box-shadow: 0 8px 18px rgba(0,0,0,.35);
  display: grid; gap: 0; border: 3px solid #fff;
}
.intro-small { font-weight: 700; font-size: .95rem; }
.intro-level { font-weight: 900; font-size: clamp(2rem, 7vw, 3rem); line-height: 1.05; }
.intro-title { font-weight: 800; font-size: 1rem; }
.intro-skip { position: absolute; bottom: 18px; left: 50%; transform: translateX(-50%); background: rgba(255,255,255,.92); border: 0; border-radius: 999px; padding: 8px 18px; font-weight: 800; color: var(--ink); cursor: pointer; }
@keyframes flyIn {
  0%   { transform: translate(70vw, 60vh) rotate(28deg) scale(.1); opacity: 0; }
  45%  { opacity: 1; }
  70%  { transform: translate(-1%, -3%) rotate(-7deg) scale(1.1); }
  85%  { transform: translate(0, 0) rotate(3deg) scale(.97); }
  100% { transform: translate(0, 0) rotate(0) scale(1); }
}
.confetti { position: absolute; inset: 0; pointer-events: none; }
.confetti i { position: absolute; top: -20px; width: 10px; height: 16px; border-radius: 3px; animation: fall linear forwards; }
@keyframes fall { to { transform: translateY(110vh) rotate(720deg); } }

.res-avatar { width: 92px; height: 92px; border-radius: 50%; object-fit: cover; object-position: 50% 25%; border: 4px solid var(--sun); margin: -62px auto 6px; display: block; background: #fff3c4; box-shadow: 0 6px 14px rgba(0,0,0,.2); }
.res-avatar.no-img { display: none; }
@media (prefers-reduced-motion: reduce) { .intro-card, .confetti i { animation: none; } }
