:root {
  --bg: #0b0e1a; --panel: #141a2e; --card: #1b2340; --line: #2a3459;
  --text: #e8ecf8; --muted: #8b96b8; --gold: #feca57; --blue: #48dbfb; --red: #ff4757; --green: #2ed573;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font: 14px/1.4 system-ui, 'Segoe UI', Roboto, sans-serif; }
button { font: inherit; color: var(--text); background: var(--card); border: 1px solid var(--line); border-radius: 8px; padding: 6px 12px; cursor: pointer; }
button:hover { border-color: var(--blue); }
button.primary { background: linear-gradient(135deg, #ff9f43, #ee5253); border: 0; font-weight: 700; padding: 12px 20px; }
.hidden { display: none !important; }
.muted { color: var(--muted); }
.small { font-size: 12px; }
kbd { background: #000; border: 1px solid var(--line); border-radius: 4px; padding: 0 5px; font-size: 12px; }

#app { display: flex; flex-direction: column; height: 100vh; }
#topbar { display: flex; align-items: center; gap: 20px; padding: 8px 16px; background: var(--panel); border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.logo { font-size: 22px; font-weight: 800; color: var(--gold); }
.logo span { font-size: 16px; color: var(--text); margin-left: 4px; }
.bars { display: flex; gap: 16px; flex: 1; min-width: 260px; }
.bar-wrap { display: flex; align-items: center; gap: 8px; flex: 1; }
.bar-wrap label { font-weight: 700; font-size: 12px; color: var(--muted); width: 48px; }
.bar { position: relative; flex: 1; height: 18px; background: #000; border-radius: 9px; overflow: hidden; border: 1px solid var(--line); }
.bar > div { height: 100%; transition: width .15s; }
.bar.hp > div { background: linear-gradient(90deg, #ff4757, #ff6b81); }
.bar.ck > div { background: linear-gradient(90deg, #0abde3, #48dbfb); }
.bar.ck.charging { box-shadow: 0 0 14px var(--blue); animation: pulse .6s infinite alternate; }
.bar span { position: absolute; inset: 0; text-align: center; font-size: 11px; font-weight: 700; line-height: 16px; text-shadow: 0 1px 2px #000; }
nav { display: flex; gap: 6px; flex-wrap: wrap; }

main { flex: 1; display: grid; grid-template-columns: minmax(0, 1fr) 380px; gap: 12px; padding: 12px; min-height: 0; }
#stage-wrap { position: relative; align-self: start; border-radius: 12px; overflow: hidden; border: 1px solid var(--line); aspect-ratio: 16 / 9; background: #000; }
#stage { width: 100%; height: 100%; display: block; }
#video { display: none; }
.pill { position: absolute; top: 12px; right: 12px; padding: 4px 12px; border-radius: 99px; font-weight: 700; font-size: 12px; background: rgba(0,0,0,.6); border: 1px solid var(--line); }
.pill.touch { color: var(--gold); border-color: var(--gold); }
.pill.clasp { color: var(--blue); border-color: var(--blue); }
#toast { position: absolute; bottom: 12px; left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; align-items: center; gap: 6px; pointer-events: none; width: 90%; }
.toast { background: rgba(10,14,30,.9); border: 1px solid var(--line); padding: 6px 14px; border-radius: 8px; transition: opacity .4s; font-weight: 600; }
.toast.ok { border-color: var(--green); } .toast.warn { border-color: var(--gold); color: var(--gold); }
.toast.error { border-color: var(--red); color: #ff8a95; } .toast.out { opacity: 0; }
#debug { position: absolute; top: 80px; left: 10px; max-width: 560px; background: rgba(0,0,0,.8); border: 1px solid var(--line); border-radius: 8px; padding: 8px 10px; font: 11px/1.5 ui-monospace, monospace; }
#debug .bar { display: inline-block; width: 50px; height: 6px; vertical-align: middle; border: 0; background: #333; flex: none; }
#debug .bar span { position: static; display: block; height: 100%; background: var(--blue); }
#debug .fingers, #debug .scores { display: flex; flex-wrap: wrap; gap: 4px 12px; }
#debug .win { color: var(--gold); font-weight: 700; }

#panel { display: flex; flex-direction: column; gap: 10px; overflow-y: auto; min-height: 0; max-height: calc(100vh - 90px); }
.card { background: var(--card); border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px; }
.card-title { font-size: 11px; font-weight: 800; letter-spacing: .1em; color: var(--muted); }
.preview { transition: border-color .15s, box-shadow .15s; }
.preview.touching { border-color: var(--gold); box-shadow: 0 0 18px rgba(254,202,87,.25); }
.preview.clasp { border-color: var(--blue); box-shadow: 0 0 18px rgba(72,219,251,.35); }
.preview.bad { border-color: var(--red); box-shadow: 0 0 18px rgba(255,71,87,.35); }
.pv-top { display: flex; justify-content: space-between; }
#pv-hold { font: 12px ui-monospace, monospace; color: var(--muted); }
.pv-main { display: flex; align-items: center; gap: 14px; margin: 6px 0; }
#pv-kanji { font-size: 64px; font-weight: 800; width: 80px; text-align: center; line-height: 1; }
#pv-name { font-size: 22px; font-weight: 700; }
#pv-name small { font-size: 13px; color: var(--muted); font-weight: 400; }
.pv-conf { height: 4px; background: #000; border-radius: 2px; overflow: hidden; }
.pv-conf > div { height: 100%; background: var(--gold); transition: width .1s; }
#pv-state { margin-top: 6px; font-size: 13px; color: var(--muted); min-height: 18px; }
.preview.bad #pv-state { color: #ff8a95; font-weight: 700; }

#seq-chips { display: flex; align-items: center; flex-wrap: wrap; gap: 4px; min-height: 44px; margin: 6px 0; }
.arrow { color: var(--muted); font-weight: 800; }
.seq-timer { height: 3px; background: #000; }
.seq-timer > div { height: 100%; background: var(--gold); transform-origin: left; }

.chip { --c: #888; display: inline-flex; align-items: center; gap: 4px; padding: 2px 7px; border-radius: 6px; border: 1px solid color-mix(in srgb, var(--c) 60%, transparent); background: color-mix(in srgb, var(--c) 15%, transparent); font-size: 12px; }
.chip b { font-size: 15px; color: var(--c); } .chip i { font-style: normal; }
.chip.big { font-size: 14px; padding: 4px 10px; } .chip.big b { font-size: 22px; }
.chip.done { border-color: var(--green); background: rgba(46,213,115,.15); } .chip.done b { color: var(--green); }
.chip.next { box-shadow: 0 0 0 2px var(--c); animation: pulse .8s infinite alternate; }
.chip.todo { opacity: .45; }
.chip.ghost { border-style: dashed; opacity: .7; animation: pulse .8s infinite alternate; }

.banner { padding: 10px 12px; border-radius: 10px; font-weight: 700; text-align: center; }
.banner.ready { background: linear-gradient(135deg, rgba(72,219,251,.25), rgba(254,202,87,.25)); border: 2px solid var(--gold); color: #fff; animation: glow 1s infinite alternate; font-size: 15px; }
.banner.blocked { background: rgba(255,255,255,.05); border: 1px dashed var(--muted); color: var(--muted); }

#suggest h3 { margin: 0 0 8px; font-size: 12px; letter-spacing: .1em; color: var(--muted); text-transform: uppercase; }
.branch { border: 1px solid var(--line); border-radius: 10px; padding: 8px; margin-bottom: 8px; transition: all .15s; }
.branch.hover { border-color: var(--c); box-shadow: 0 0 16px color-mix(in srgb, var(--c) 50%, transparent); background: color-mix(in srgb, var(--c) 8%, transparent); }
.branch-head { display: flex; justify-content: space-between; align-items: center; }
.branch-count { font-size: 11px; color: var(--muted); }
.branch ul, .help-list { list-style: none; margin: 6px 0 0; padding: 0; }
.branch li { padding: 6px 4px; border-top: 1px solid var(--line); }
.branch li.dim { opacity: .45; }
.j-name { font-weight: 700; } .j-icon { margin-right: 4px; }
.finish { font-size: 11px; color: var(--gold); font-style: normal; border: 1px solid var(--gold); border-radius: 4px; padding: 0 4px; margin-left: 4px; }
.j-seq { display: flex; gap: 3px; margin: 4px 0; flex-wrap: wrap; }
.j-meta { display: flex; gap: 10px; font-size: 12px; color: var(--muted); }
.st.ok { color: var(--green); } .st.cd { color: var(--gold); } .st.ck { color: var(--blue); }

.error-flash { animation: errflash .5s 2; }
.cast-flash { animation: castflash .6s; }
@keyframes errflash { 50% { box-shadow: 0 0 0 3px var(--red), 0 0 30px var(--red); background-color: rgba(255,71,87,.2); } }
@keyframes castflash { 30% { box-shadow: 0 0 0 3px var(--cast), 0 0 40px var(--cast); } }
@keyframes pulse { from { opacity: .7; } to { opacity: 1; } }
@keyframes glow { from { box-shadow: 0 0 6px var(--gold); } to { box-shadow: 0 0 22px var(--gold); } }

.overlay { position: fixed; inset: 0; background: rgba(5,7,15,.8); backdrop-filter: blur(4px); display: grid; place-items: center; z-index: 10; padding: 16px; }
.modal { position: relative; background: var(--panel); border: 1px solid var(--line); border-radius: 16px; padding: 24px; max-width: 560px; width: 100%; max-height: 90vh; overflow: auto; }
.modal.wide { max-width: 1000px; }
.modal h1 { margin-top: 0; color: var(--gold); }
.modal .actions { display: flex; gap: 10px; flex-wrap: wrap; margin: 16px 0; }
.close { position: absolute; top: 12px; right: 12px; }
.cols { display: grid; grid-template-columns: 1fr 1fr; gap: 20px; }
.help-list li { display: flex; gap: 10px; align-items: flex-start; padding: 8px 0; border-bottom: 1px solid var(--line); flex-wrap: wrap; }
.help-list p { margin: 2px 0 0; color: var(--muted); }
.rules li { margin-bottom: 4px; }
.slider { display: grid; grid-template-columns: 170px 1fr 50px; gap: 8px; align-items: center; margin: 8px 0; }
.slider small { grid-column: 2 / 4; color: var(--muted); font-size: 11px; margin-top: -6px; }
.slider output { font: 12px ui-monospace, monospace; }
.check { display: block; margin: 12px 0; }

@media (max-width: 900px) {
  main { grid-template-columns: 1fr; }
  #panel { max-height: none; }
  .cols { grid-template-columns: 1fr; }
}

/* ---- Bảng huấn luyện ML ---- */
.train-grid { display: grid; grid-template-columns: 260px 1fr; gap: 20px; }
#train-preview { width: 224px; height: 224px; background: #000; border-radius: 8px; border: 1px solid var(--line); display: block; }
#train-rec { min-height: 20px; margin-top: 6px; font-weight: 700; color: var(--gold); }
#train-rec.on { color: var(--red); animation: pulse .5s infinite alternate; }
.train-bar { margin: 8px 0 4px; height: 6px; }
.train-io { display: flex; gap: 6px; flex-wrap: wrap; margin: 10px 0; }
.btn { display: inline-block; background: var(--card); border: 1px solid var(--line); border-radius: 8px; padding: 6px 12px; cursor: pointer; }
.train-classes { list-style: none; margin: 0; padding: 0; }
.train-classes li { display: grid; grid-template-columns: 120px 1fr auto; gap: 10px; align-items: center; padding: 8px 0; border-bottom: 1px solid var(--line); }
.train-classes li.rec { background: rgba(255,71,87,.12); }
.t-count { font-weight: 700; color: var(--gold); } .t-count.ok { color: var(--green); }
.t-act { display: flex; gap: 4px; }
button:disabled { opacity: .4; cursor: not-allowed; }
@media (max-width: 900px) { .train-grid { grid-template-columns: 1fr; } }
