:root {
  --bg: #0f1b2d; --panel: rgba(16, 28, 48, 0.92); --text: #f4f7fb; --muted: #a9b7cc;
  --accent: #ffcc33; --good: #3ccf74; --bad: #ff5a5a; --blue: #3d8bfd;
  --font: "Noto Sans JP", system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font-family: var(--font); }
body.game { overflow: hidden; touch-action: none; -webkit-user-select: none; user-select: none; -webkit-touch-callout: none; }
button { font-family: inherit; }
.jp { font-family: "Noto Sans JP", sans-serif; }
ruby rt { font-size: 0.55em; color: #ffe9a8; }
[hidden] { display: none !important; }

#game-canvas { position: fixed; inset: 0; width: 100vw; height: 100vh; display: block; }

/* ---------- Màn hình bắt đầu / kết quả ---------- */
.screen { position: fixed; inset: 0; display: flex; align-items: center; justify-content: center; padding: 16px;
  background: radial-gradient(circle at 30% 20%, #23406b, #0b1424 70%); z-index: 50; overflow: auto; }
.screen.translucent { background: rgba(5, 12, 24, 0.72); }
.card { background: var(--panel); border-radius: 18px; padding: 22px; width: min(560px, 100%); box-shadow: 0 10px 40px rgba(0,0,0,.45); }
.card h1 { margin: 0 0 4px; font-size: 1.6rem; }
.card h2 { margin: 0 0 12px; font-size: 1.25rem; }
.card p { color: var(--muted); margin: 0 0 14px; line-height: 1.5; }
.field { display: flex; flex-direction: column; gap: 6px; margin-bottom: 12px; }
.field label { font-size: .9rem; color: var(--muted); }
.field input[type=text], .field select { font: inherit; padding: 10px 12px; border-radius: 10px; border: 1px solid #34507a; background: #0d1a2e; color: var(--text); }
.check { display: flex; align-items: center; gap: 8px; margin-bottom: 12px; }
.btn { display: inline-flex; align-items: center; justify-content: center; gap: 6px; font-size: 1.05rem; font-weight: 700;
  padding: 12px 18px; border-radius: 12px; border: 0; cursor: pointer; background: var(--accent); color: #1b1b1b; text-decoration: none; }
.btn.secondary { background: #2b456d; color: var(--text); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn-row { display: flex; gap: 10px; flex-wrap: wrap; margin-top: 8px; }
.help { font-size: .85rem; color: var(--muted); line-height: 1.6; margin-top: 14px; }
kbd { background: #2b456d; border-radius: 5px; padding: 1px 6px; font-family: inherit; font-size: .85em; }

/* ---------- HUD ---------- */
#hud { position: fixed; inset: 0; pointer-events: none; z-index: 10; }
.hud-top { position: absolute; top: max(10px, env(safe-area-inset-top)); left: 10px; right: 10px; display: flex; gap: 10px; align-items: center; }
.hud-box { background: rgba(0,0,0,.45); border-radius: 12px; padding: 6px 12px; font-weight: 800; font-size: 1.3rem; min-width: 64px; text-align: center; }
.hud-rank small { font-size: .8rem; color: var(--muted); }
.hud-time.low { color: var(--bad); }
.hud-progress { flex: 1; }
.hud-track { position: relative; height: 10px; border-radius: 6px; background: rgba(255,255,255,.25); }
.hud-finish { position: absolute; right: -2px; top: -5px; width: 4px; height: 20px; background: repeating-linear-gradient(#fff 0 4px, #000 4px 8px); }
.hud-dot { position: absolute; top: 50%; width: 12px; height: 12px; border-radius: 50%; border: 2px solid #fff; transform: translate(-50%, -50%); transition: left .2s linear; }
.hud-dot.me { width: 18px; height: 18px; z-index: 2; box-shadow: 0 0 0 3px rgba(255,204,51,.7); }
.hud-status { position: absolute; top: 64px; left: 50%; transform: translateX(-50%); background: rgba(0,0,0,.55); padding: 6px 14px; border-radius: 10px; font-weight: 700; white-space: nowrap; }
.hud-countdown { position: absolute; top: 32%; left: 50%; transform: translate(-50%, -50%); font-size: min(26vw, 160px); font-weight: 900; color: var(--accent); text-shadow: 0 6px 0 #a3570b, 0 10px 30px rgba(0,0,0,.6); }
.hud-countdown.pop { animation: pop .5s ease-out; }
@keyframes pop { from { transform: translate(-50%, -50%) scale(1.8); opacity: .2; } to { transform: translate(-50%, -50%) scale(1); opacity: 1; } }
.hud-toasts { position: absolute; top: 100px; left: 50%; transform: translateX(-50%); display: flex; flex-direction: column; gap: 6px; align-items: center; }
.hud-toast { background: rgba(0,0,0,.6); padding: 6px 14px; border-radius: 10px; animation: fade 2.6s forwards; white-space: nowrap; }
.hud-toast.good { background: rgba(30,140,70,.85); font-weight: 700; }
@keyframes fade { 0% { opacity: 0; transform: translateY(-6px); } 10%, 80% { opacity: 1; transform: none; } 100% { opacity: 0; } }
.hud-item { position: absolute; right: 12px; top: 64px; width: 54px; height: 54px; border-radius: 14px; background: rgba(0,0,0,.45); border: 2px dashed rgba(255,255,255,.35); display: flex; align-items: center; justify-content: center; font-size: 1.6rem; }
.hud-gear { position: fixed; left: 12px; top: 64px; z-index: 12; pointer-events: auto; background: rgba(0,0,0,.45); color: #fff; border: 0; border-radius: 12px; width: 44px; height: 44px; font-size: 1.3rem; cursor: pointer; }
.hud-fps { position: fixed; left: 12px; bottom: 12px; z-index: 12; font: 12px monospace; background: rgba(0,0,0,.5); padding: 2px 6px; border-radius: 6px; }

/* ---------- Điều khiển cảm ứng ---------- */
#touch { position: fixed; inset: 0; z-index: 11; pointer-events: none; }
.joy-zone { position: absolute; left: 0; bottom: 0; width: 50%; height: 60%; pointer-events: auto; touch-action: none; }
.joy-base { position: absolute; left: 90px; top: calc(100% - 110px); width: 120px; height: 120px; margin: -60px 0 0 -60px; border-radius: 50%;
  background: rgba(255,255,255,.12); border: 2px solid rgba(255,255,255,.35); }
.joy-knob { position: absolute; left: 50%; top: 50%; width: 56px; height: 56px; margin: -28px 0 0 -28px; border-radius: 50%; background: rgba(255,255,255,.55); }
.joy-zone.active .joy-base { background: rgba(255,255,255,.2); }
.touch-btn { position: absolute; pointer-events: auto; touch-action: none; border-radius: 50%; border: 3px solid rgba(255,255,255,.6);
  color: #fff; font-weight: 800; font-family: inherit; display: flex; align-items: center; justify-content: center; text-align: center; }
.touch-btn.pressed { transform: scale(.92); filter: brightness(1.3); }
.btn-jump { right: max(20px, env(safe-area-inset-right)); bottom: max(28px, env(safe-area-inset-bottom)); width: 96px; height: 96px; background: rgba(61,139,253,.6); font-size: 1rem; }
.btn-item { right: max(128px, calc(env(safe-area-inset-right) + 108px)); bottom: max(40px, env(safe-area-inset-bottom)); width: 72px; height: 72px; background: rgba(255,170,40,.55); font-size: .8rem; }

/* ---------- Câu hỏi ---------- */
#quiz { position: fixed; inset: 0; z-index: 30; display: flex; align-items: center; justify-content: center; padding: 12px; background: rgba(5,10,20,.45); pointer-events: auto; }
.quiz-card { background: var(--panel); border-radius: 18px; padding: 16px; width: min(640px, 100%); max-height: 100%; overflow: auto; box-shadow: 0 10px 40px rgba(0,0,0,.5); border: 2px solid #ffcc33; }
.quiz-head { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.quiz-title { font-weight: 800; color: var(--accent); flex: 1; }
.quiz-type { font-size: .8rem; background: #2b456d; border-radius: 8px; padding: 2px 8px; }
.quiz-timer { font-weight: 900; font-size: 1.5rem; min-width: 2ch; text-align: right; }
.quiz-bar { height: 6px; background: rgba(255,255,255,.15); border-radius: 4px; overflow: hidden; margin-bottom: 12px; }
.quiz-bar-fill { height: 100%; width: 100%; background: linear-gradient(90deg, #ff5a5a, #ffcc33, #3ccf74); }
.quiz-passage { background: rgba(255,255,255,.06); border-radius: 10px; padding: 10px 12px; line-height: 2; margin-bottom: 10px; }
.quiz-stem { font-size: 1.25rem; line-height: 2.1; margin-bottom: 12px; }
.quiz-choices { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
@media (max-width: 520px) { .quiz-choices { grid-template-columns: 1fr; } .quiz-stem { font-size: 1.1rem; } }
.quiz-choice { display: flex; align-items: center; gap: 10px; text-align: left; font-size: 1.1rem; padding: 12px; border-radius: 12px; border: 2px solid #34507a; background: #13243d; color: var(--text); cursor: pointer; line-height: 1.8; }
.quiz-choice:hover:not(:disabled) { border-color: var(--accent); }
.quiz-choice .num { flex: none; width: 26px; height: 26px; border-radius: 50%; background: #2b456d; display: inline-flex; align-items: center; justify-content: center; font-size: .85rem; font-weight: 800; }
.quiz-choice.correct { border-color: var(--good); background: rgba(60,207,116,.25); }
.quiz-choice.wrong { border-color: var(--bad); background: rgba(255,90,90,.25); }
.quiz-choice:disabled { cursor: default; }
.quiz-feedback { margin-top: 12px; padding: 10px 12px; border-radius: 10px; font-weight: 700; line-height: 1.6; }
.quiz-feedback.ok { background: rgba(60,207,116,.2); }
.quiz-feedback.bad { background: rgba(255,90,90,.18); }
.quiz-expl { font-weight: 400; margin-top: 4px; color: #e5ecf6; }
.quiz-stun { margin-top: 6px; color: var(--accent); }

/* ---------- Kết quả ---------- */
.results-table { width: 100%; border-collapse: collapse; font-size: .92rem; margin: 8px 0 12px; }
.results-table th, .results-table td { padding: 6px 4px; text-align: center; border-bottom: 1px solid rgba(255,255,255,.1); }
.results-table th { color: var(--muted); font-weight: 600; font-size: .8rem; }
.results-table td.name { text-align: left; white-space: nowrap; }
.results-table tr.me { background: rgba(255,204,51,.12); font-weight: 700; }
.swatch { display: inline-block; width: 12px; height: 12px; border-radius: 50%; margin-right: 6px; vertical-align: -1px; border: 1px solid #fff; }
.table-wrap { overflow-x: auto; }

/* ---------- Trang chủ ---------- */
.home-links { display: grid; gap: 10px; }
.home-links .btn { justify-content: flex-start; }
.badge-soon { font-size: .7rem; background: #555; color: #ddd; padding: 2px 6px; border-radius: 6px; margin-left: auto; }

/* ---------- Phòng / sảnh ---------- */
.pin-input { font-size: 1.8rem !important; letter-spacing: .3em; text-align: center; font-weight: 800; }
.pin-badge { background: var(--accent); color: #1b1b1b; border-radius: 10px; padding: 2px 10px; letter-spacing: .12em; }
.form-error { background: rgba(255,90,90,.18); border: 1px solid var(--bad); border-radius: 10px; padding: 8px 12px; margin-bottom: 10px; }
.player-list { list-style: none; padding: 0; margin: 0 0 12px; display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 8px; }
.player-list li { display: flex; align-items: center; gap: 8px; background: #13243d; border-radius: 10px; padding: 8px 10px; min-width: 0; }
.player-list li .pname { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.player-list li.me { outline: 2px solid var(--accent); }
.player-list li.off { opacity: .5; }
.player-list .kick { border: 0; background: #5a2330; color: #fff; border-radius: 8px; padding: 2px 8px; cursor: pointer; }
.waiting { color: var(--accent); font-weight: 700; animation: blink 1.6s infinite; }
@keyframes blink { 50% { opacity: .5; } }
.hud-item.has { border: 2px solid var(--accent); background: rgba(255,204,51,.25); animation: pulse 1s infinite; }
@keyframes pulse { 50% { transform: scale(1.08); } }
.touch-btn .item-emoji { font-size: 2rem; }
.touch-btn.btn-item.has { background: rgba(255,170,40,.85); border-color: #fff; }

/* ---------- Thiên tai ---------- */
#disaster { position: fixed; inset: 0; z-index: 32; display: flex; align-items: center; justify-content: center; padding: 12px; background: rgba(20,40,80,.55); pointer-events: auto; }
.dis-card { background: var(--panel); border-radius: 18px; padding: 16px; width: min(760px, 100%); max-height: 100%; overflow: auto; border: 3px solid #7fd3ff; box-shadow: 0 0 40px rgba(127,211,255,.4); }
.dis-card.listening { border-color: #c38bff; box-shadow: 0 0 40px rgba(195,139,255,.4); }
.dis-head { display: flex; align-items: center; justify-content: space-between; gap: 8px; }
.dis-title { font-weight: 900; font-size: 1.15rem; color: #9fe3ff; }
.listening .dis-title { color: #d9b8ff; }
.dis-timer { font-weight: 900; font-size: 1.6rem; font-variant-numeric: tabular-nums; }
.dis-help { color: var(--muted); font-size: .9rem; margin: 6px 0 10px; }
.dis-lock { color: var(--accent); font-weight: 700; margin: -4px 0 10px; }
.dis-listen { display: flex; align-items: center; gap: 12px; background: rgba(255,255,255,.06); border-radius: 12px; padding: 12px; margin-bottom: 10px; font-weight: 700; }
.dis-speaker { font-size: 2rem; animation: pulse 0.8s infinite; }
.dis-speaker.done { animation: none; opacity: .5; }
.hud-banner { position: absolute; top: 34%; left: 50%; transform: translate(-50%, -50%); background: rgba(200,40,40,.9); color: #fff; padding: 14px 22px; border-radius: 14px; font-size: clamp(1.1rem, 4vw, 1.8rem); font-weight: 900; text-align: center; animation: pop .4s ease-out; white-space: nowrap; }
.speaker-row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; margin: 8px 0 12px; }
.speaker-row .result { font-size: .9rem; }
.speaker-row .result.bad { color: #ffb4b4; }
.speaker-row .result.ok { color: #9ff0bd; }
.conn-error { background: rgba(255,90,90,.12); border: 1px solid rgba(255,90,90,.5); border-radius: 10px; padding: 10px 12px; color: #ffd6d6 !important; line-height: 1.7; }
.conn-error code { background: rgba(0,0,0,.35); padding: 1px 6px; border-radius: 5px; color: #fff; }
.conn-error a { color: var(--accent); }
