:root {
  --ink: #f6fbef;
  --gold: #ffd84d;
  --lime: #a3e635;
  --panel: rgba(10, 30, 24, .86);
  --line: rgba(190, 242, 100, .22);
}

* { box-sizing: border-box; margin: 0; padding: 0; }
[hidden] { display: none !important; }

html, body {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  overscroll-behavior: none;
  touch-action: none;
  -webkit-touch-callout: none;
  background: #0b1a12;
  color: var(--ink);
  font-family: system-ui, -apple-system, 'Segoe UI', Roboto, sans-serif;
  user-select: none;
  -webkit-user-select: none;
  -webkit-tap-highlight-color: transparent;
}

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

/* ---------- HUD ---------- */
#hud {
  position: fixed;
  top: max(8px, env(safe-area-inset-top));
  left: max(8px, env(safe-area-inset-left));
  right: max(8px, env(safe-area-inset-right));
  display: flex;
  gap: 8px;
  align-items: center;
  pointer-events: none;
  z-index: 5;
}
.hud-box, .hud-btn {
  background: linear-gradient(180deg, rgba(24, 58, 46, .82), rgba(8, 24, 20, .82));
  border: 1px solid var(--line);
  box-shadow: 0 4px 14px rgba(0, 0, 0, .25), inset 0 1px 0 rgba(255, 255, 255, .12);
}
.hud-box {
  border-radius: 999px;
  padding: 6px 14px;
  font-size: 10px;
  font-weight: 700;
  letter-spacing: .1em;
  color: #b9d8c4;
  white-space: nowrap;
}
.hud-box b { color: var(--gold); font-size: 15px; margin-left: 5px; letter-spacing: .02em; }
.hud-box.lives b::before { content: '♥ '; color: #ff6b6b; }
#hud-p2 { border-color: rgba(96, 165, 250, .75); color: #bfdbfe; }
#hud-p2 b + b { font-size: 12px; }
#hud.coop .hud-high { display: none; }
#btn-mute { margin-left: auto; }
.hud-btn {
  flex-shrink: 0;
  position: relative;
  pointer-events: auto;
  width: 40px;
  height: 40px;
  border-radius: 13px;
  color: inherit;
  cursor: pointer;
}
.hud-btn.pause::before, .hud-btn.pause::after { content: ''; position: absolute; top: 12px; width: 4px; height: 14px; border-radius: 2px; background: var(--ink); }
.hud-btn.pause::before { left: 13px; }
.hud-btn.pause::after { right: 13px; }
.hud-btn:active { transform: translateY(1px); }

#boss-bar {
  position: fixed;
  top: calc(max(8px, env(safe-area-inset-top)) + 50px);
  left: 50%;
  transform: translateX(-50%);
  width: min(360px, 62vw);
  text-align: center;
  font-size: 11px;
  font-weight: 800;
  letter-spacing: .1em;
  text-shadow: 0 1px 3px #000;
  z-index: 5;
  pointer-events: none;
}
#boss-bar div { height: 12px; margin-top: 4px; padding: 2px; background: rgba(20, 5, 5, .72); border: 1px solid rgba(252, 165, 165, .7); border-radius: 999px; box-shadow: 0 4px 14px rgba(0, 0, 0, .35); }
#boss-bar i { display: block; height: 100%; width: 100%; border-radius: 999px; background: linear-gradient(90deg, #e11d48, #fb923c 70%, #fde047); transition: width .15s; }

/* ---------- Bảng popup ---------- */
.screen {
  position: fixed;
  inset: 0;
  display: flex;
  justify-content: center;
  align-items: center;
  background: radial-gradient(ellipse at center, rgba(4, 20, 16, .2), rgba(3, 12, 12, .68));
  padding:
    max(10px, env(safe-area-inset-top)) max(10px, env(safe-area-inset-right))
    max(10px, env(safe-area-inset-bottom)) max(10px, env(safe-area-inset-left));
  z-index: 10;
}
.panel {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 11px;
  width: min(400px, 100%);
  max-height: 100%;
  overflow-y: auto;
  overscroll-behavior: contain;
  touch-action: pan-y; /* cho cuộn trong bảng, không cuộn cả trang */
  scrollbar-width: none;
  padding: 22px 24px;
  background: linear-gradient(180deg, rgba(255, 255, 255, .07), rgba(255, 255, 255, 0) 38%), var(--panel);
  border: 1px solid var(--line);
  border-radius: 26px;
  text-align: center;
  box-shadow: 0 30px 70px rgba(0, 0, 0, .5), inset 0 1px 0 rgba(255, 255, 255, .16);
  -webkit-backdrop-filter: blur(10px);
  backdrop-filter: blur(10px);
  animation: pop .24s cubic-bezier(.2, 1.3, .4, 1);
}
.panel::-webkit-scrollbar { display: none; }
@keyframes pop { from { opacity: 0; transform: translateY(14px) scale(.95); } }
.panel > * { flex-shrink: 0; }

h1 {
  font-size: 38px;
  line-height: 1;
  font-weight: 900;
  letter-spacing: .03em;
  color: var(--gold);
  text-shadow: 0 2px 0 #e8890c, 0 4px 0 #b45309, 0 6px 0 #7c2d12, 0 12px 22px rgba(0, 0, 0, .5);
}
h1 span {
  display: block;
  margin-top: 10px;
  font-size: 16px;
  letter-spacing: .42em;
  text-indent: .42em;
  color: #d9f99d;
  text-shadow: 0 2px 6px rgba(0, 0, 0, .6);
}
h2 { font-size: 27px; font-weight: 900; letter-spacing: .08em; text-shadow: 0 3px 0 rgba(0, 0, 0, .35); }
.bad { color: #ff7b7b; }
.good { color: var(--gold); }
.label { font-size: 11px; font-weight: 700; letter-spacing: .24em; text-transform: uppercase; color: #9fc7ae; }
.chip { display: inline-flex; align-items: center; gap: 7px; padding: 4px 14px; border-radius: 999px; background: rgba(0, 0, 0, .28); font-size: 12px; color: #cfe6d6; }
.chip b { color: var(--gold); font-size: 15px; }
.chip::before, .btn[class*='i-']::before {
  content: '';
  flex-shrink: 0;
  width: 15px;
  height: 15px;
  background: currentColor;
  -webkit-mask: var(--icon) center / contain no-repeat;
  mask: var(--icon) center / contain no-repeat;
}
.chip::before { background: var(--gold); }
.i-play::before { --icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M7 4l13 8-13 8z'/%3E%3C/svg%3E"); }
.i-users::before { --icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M8 11a4 4 0 100-8 4 4 0 000 8zm0 2c-4 0-7 2-7 5v3h14v-3c0-3-3-5-7-5zm9-2a3.5 3.5 0 100-7 3.5 3.5 0 000 7zm0 2c-.7 0-1.4.1-2 .3 1.3 1.2 2 2.800 2 4.700v3h6v-3c0-3-2.500-5-6-5z'/%3E%3C/svg%3E"); }
.i-globe::before { --icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M12 2a10 10 0 100 20 10 10 0 000-20zm7 6h-3a15 15 0 00-1.400-3.600A8 8 0 0119 8zM12 4c.9 1.100 1.600 2.500 2 4h-4c.4-1.500 1.100-2.900 2-4zM4.300 14a8 8 0 010-4h3.400a17 17 0 000 4zm.7 2h3a15 15 0 001.400 3.600A8 8 0 015 16zm3-8H5a8 8 0 014.400-3.600A15 15 0 008 8zm4 12c-.9-1.100-1.600-2.500-2-4h4c-.4 1.500-1.100 2.900-2 4zm2.300-6H9.700a15 15 0 010-4h4.600a15 15 0 010 4zm.3 5.600A15 15 0 0016 16h3a8 8 0 01-4.400 3.600zM16.300 14a17 17 0 000-4h3.400a8 8 0 010 4z'/%3E%3C/svg%3E"); }
.i-home::before { --icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M12 3l10 9h-3v9h-5v-6h-4v6H5v-9H2z'/%3E%3C/svg%3E"); }
.i-trophy::before { --icon: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 24 24'%3E%3Cpath d='M6 2h12v2h4v3c0 3-2 5-5 5.200A6 6 0 0113 16v3h4v3H7v-3h4v-3a6 6 0 01-4-3.800C4 12 2 10 2 7V4h4zm0 4H4v1c0 1.500.8 2.700 2 3zm12 0v4c1.200-.3 2-1.500 2-3V6z'/%3E%3C/svg%3E"); }
.score { display: flex; flex-direction: column; align-items: center; width: min(260px, 100%); padding: 8px; border-radius: 16px; background: rgba(0, 0, 0, .28); font-size: 11px; font-weight: 700; letter-spacing: .24em; text-transform: uppercase; color: #9fc7ae; }
.score b { font-size: 32px; letter-spacing: .04em; color: var(--gold); font-variant-numeric: tabular-nums; }
.stars { font-size: 30px; line-height: 1; letter-spacing: .2em; color: var(--gold); text-shadow: 0 3px 0 #b45309, 0 0 18px rgba(255, 216, 77, .6); }
.cheat { color: #f9a8d4; font-weight: 800; font-size: 13px; }
.cheat:not(.on) { display: none; }
.help { font-size: 11.5px; line-height: 1.8; color: #b9d3c2; }
kbd { display: inline-block; min-width: 20px; padding: 0 6px; border-radius: 5px; background: rgba(255, 255, 255, .12); box-shadow: 0 2px 0 rgba(0, 0, 0, .4); font: 700 10.5px ui-monospace, Consolas, monospace; color: var(--ink); }

.btn {
  --top: #35695a;
  --bottom: #24493f;
  --edge: #122a24;
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 8px;
  width: min(280px, 100%);
  padding: 10px 14px;
  border: 0;
  border-radius: 14px;
  background: linear-gradient(180deg, var(--top), var(--bottom));
  box-shadow: 0 4px 0 var(--edge), 0 8px 16px rgba(0, 0, 0, .28), inset 0 1px 0 rgba(255, 255, 255, .22);
  margin-bottom: 3px;
  color: var(--ink);
  font: inherit;
  font-size: 13.5px;
  font-weight: 800;
  letter-spacing: .04em;
  text-decoration: none;
  cursor: pointer;
  transition: transform .08s, box-shadow .08s, filter .15s;
}
.btn.primary { --top: #c6f45f; --bottom: #6fbf1f; --edge: #3f7212; color: #122c05; text-shadow: 0 1px 0 rgba(255, 255, 255, .4); }
.btn.blue { --top: #5cc8f5; --bottom: #2482c8; --edge: #14527f; }
.btn.ghost { --top: rgba(255, 255, 255, .11); --bottom: rgba(255, 255, 255, .05); --edge: rgba(0, 0, 0, .34); font-size: 12.5px; font-weight: 700; color: #d6e9dc; }
.btn.big { font-size: 17px; padding: 13px; letter-spacing: .1em; }
.btn:hover { filter: brightness(1.1); }
.btn:active { transform: translateY(3px); box-shadow: 0 1px 0 var(--edge), inset 0 1px 0 rgba(255, 255, 255, .22); }
.btn:focus-visible, .level:focus-visible, .hud-btn:focus-visible { outline: 3px solid var(--gold); outline-offset: 2px; }
.btn:disabled, .level:disabled { opacity: .45; cursor: default; filter: grayscale(.5); transform: none; }
.row { display: flex; gap: 9px; width: min(280px, 100%); }
.row > .btn { flex: 1 1 0; width: auto; min-width: 0; padding-inline: 6px; white-space: nowrap; }
.row:not(:has(> :not([hidden], .gone))) { display: none; }
.row > .i-users, .row > .i-globe { flex-direction: column; gap: 5px; font-size: 11.5px; }

.card { display: flex; flex-direction: column; gap: 10px; width: min(280px, 100%); padding: 12px 14px; border-radius: 16px; background: rgba(0, 0, 0, .28); }
label { display: flex; align-items: center; justify-content: space-between; gap: 10px; font-size: 12.5px; font-weight: 700; }
input[type=range] { -webkit-appearance: none; appearance: none; width: 160px; height: 22px; background: transparent; cursor: pointer; touch-action: none; }
input[type=range]::-webkit-slider-runnable-track { height: 8px; border-radius: 999px; background: linear-gradient(90deg, #6fbf1f, #c6f45f); box-shadow: inset 0 1px 3px rgba(0, 0, 0, .4); }
input[type=range]::-webkit-slider-thumb { -webkit-appearance: none; width: 20px; height: 20px; margin-top: -6px; border-radius: 50%; border: 3px solid #fff; background: #4d9418; box-shadow: 0 2px 6px rgba(0, 0, 0, .45); }
input[type=range]::-moz-range-track { height: 8px; border-radius: 999px; background: linear-gradient(90deg, #6fbf1f, #c6f45f); }
input[type=range]::-moz-range-thumb { width: 14px; height: 14px; border-radius: 50%; border: 3px solid #fff; background: #4d9418; }

/* ---------- Nút cảm ứng ---------- */
#touch { position: fixed; inset: 0; pointer-events: none; z-index: 12; }
#dpad, .act { pointer-events: auto; touch-action: none; -webkit-touch-callout: none; }
#dpad {
  position: absolute;
  left: max(14px, env(safe-area-inset-left));
  bottom: max(14px, env(safe-area-inset-bottom));
  width: 148px;
  height: 148px;
  border-radius: 50%;
  background: radial-gradient(circle, rgba(255, 255, 255, .04) 40%, rgba(6, 26, 20, .42));
  border: 2px solid rgba(255, 255, 255, .32);
  box-shadow: 0 8px 24px rgba(0, 0, 0, .25), inset 0 0 22px rgba(255, 255, 255, .08);
}
#dpad span { position: absolute; font-size: 17px; opacity: .8; pointer-events: none; text-shadow: 0 1px 3px rgba(0, 0, 0, .5); }
#dpad .up { top: 10px; left: 50%; transform: translateX(-50%); }
#dpad .down { bottom: 10px; left: 50%; transform: translateX(-50%); }
#dpad .left { left: 12px; top: 50%; transform: translateY(-50%); }
#dpad .right { right: 12px; top: 50%; transform: translateY(-50%); }
#dpad-knob {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 56px;
  height: 56px;
  margin: -28px 0 0 -28px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 30%, rgba(255, 255, 255, .75), rgba(190, 220, 205, .35));
  box-shadow: 0 5px 12px rgba(0, 0, 0, .35);
  pointer-events: none;
}
#actions {
  position: absolute;
  right: max(14px, env(safe-area-inset-right));
  bottom: max(14px, env(safe-area-inset-bottom));
  display: flex;
  gap: 14px;
  align-items: flex-end;
}
.act {
  width: 76px;
  height: 76px;
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, .55);
  box-shadow: 0 6px 0 rgba(0, 0, 0, .28), 0 10px 22px rgba(0, 0, 0, .25), inset 0 2px 8px rgba(255, 255, 255, .35);
  color: #fff;
  font: inherit;
  text-shadow: 0 1px 3px rgba(0, 0, 0, .45);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}
.act b { font-size: 25px; }
.act small { font-size: 10px; font-weight: 700; letter-spacing: .1em; }
.act.shoot { background: radial-gradient(circle at 35% 28%, rgba(255, 140, 120, .85), rgba(200, 30, 45, .68)); margin-bottom: 34px; }
.act.jump { background: radial-gradient(circle at 35% 28%, rgba(125, 200, 255, .85), rgba(30, 95, 210, .68)); }
.act.held { filter: brightness(1.3); transform: translateY(4px); box-shadow: 0 2px 0 rgba(0, 0, 0, .28), inset 0 2px 8px rgba(255, 255, 255, .35); }

body.touch .keys { display: none; }
.gone { display: none !important; }
.stack { display: flex; flex-direction: column; align-items: center; gap: 11px; width: 100%; }
a.btn { text-align: center; }

/* ---------- Chọn màn ---------- */
#screen-levels .panel { width: min(520px, 100%); }
#level-list { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; width: 100%; }
.level {
  --a: #3fae5a;
  --b: #14532d;
  position: relative;
  display: flex;
  flex-direction: column;
  justify-content: flex-end;
  gap: 3px;
  min-height: 96px;
  padding: 11px 13px;
  overflow: hidden;
  border: 1px solid rgba(255, 255, 255, .24);
  border-radius: 18px;
  background:
    linear-gradient(180deg, transparent 30%, rgba(0, 0, 0, .55)),
    linear-gradient(145deg, var(--a), var(--b));
  box-shadow: 0 5px 0 rgba(0, 0, 0, .34), inset 0 1px 0 rgba(255, 255, 255, .25);
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: pointer;
  transition: transform .12s, filter .15s;
}
.level::after { content: attr(data-value); position: absolute; right: 10px; top: -10px; font-size: 62px; font-weight: 900; color: rgba(255, 255, 255, .2); }
.level[data-value='2'] { --a: #38bdf8; --b: #155e75; }
.level[data-value='3'] { --a: #8b5cf6; --b: #1e1b4b; }
.level[data-value='4'] { --a: #a5c8ee; --b: #3b5b8c; }
.level:hover:not(:disabled) { transform: translateY(-2px); filter: brightness(1.08); }
.level:active:not(:disabled) { transform: translateY(3px); }
.level b { position: relative; z-index: 1; font-size: 13px; color: #fff; text-shadow: 0 2px 4px rgba(0, 0, 0, .6); }
.level small { position: relative; z-index: 1; font-size: 11px; color: #e8f5ec; text-shadow: 0 1px 3px rgba(0, 0, 0, .7); }

/* ---------- Online ---------- */
#room-form { display: flex; flex-direction: column; align-items: center; gap: 11px; width: 100%; }
#room-input {
  width: min(280px, 100%);
  padding: 11px;
  border-radius: 14px;
  border: 2px dashed rgba(190, 242, 100, .45);
  background: rgba(0, 0, 0, .35);
  color: var(--gold);
  font: 800 19px ui-monospace, Consolas, monospace;
  letter-spacing: .3em;
  text-align: center;
  user-select: text;
  -webkit-user-select: text;
  touch-action: manipulation;
}
#room-input:focus { outline: 0; border-style: solid; border-color: var(--gold); }
#room-code { padding: 8px 14px 8px 24px; border-radius: 16px; background: rgba(0, 0, 0, .35); border: 2px dashed rgba(255, 216, 77, .55); font: 800 36px ui-monospace, Consolas, monospace; letter-spacing: .25em; color: var(--gold); user-select: all; -webkit-user-select: all; }
#room-status { font-size: 13px; color: #cfe6d6; }
#online-error:empty, #pause-note:empty { display: none; }

#net-msg {
  position: fixed;
  left: 50%;
  top: calc(max(8px, env(safe-area-inset-top)) + 84px);
  transform: translateX(-50%);
  width: max-content;
  max-width: 90vw;
  padding: 8px 18px;
  border-radius: 999px;
  background: linear-gradient(180deg, rgba(185, 28, 28, .94), rgba(127, 29, 29, .94));
  border: 1px solid rgba(254, 202, 202, .4);
  box-shadow: 0 10px 24px rgba(0, 0, 0, .35);
  font-size: 12.5px;
  font-weight: 700;
  text-align: center;
  z-index: 15;
  pointer-events: none;
}

#rotate {
  position: fixed;
  inset: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 26px;
  padding: 30px;
  background: radial-gradient(circle at 50% 35%, #1c4a39, #08170f 70%);
  font-size: 18px;
  font-weight: 800;
  text-align: center;
  z-index: 100;
}
#rotate .phone { width: 58px; height: 100px; border: 4px solid var(--lime); border-radius: 14px; box-shadow: 0 0 30px rgba(163, 230, 53, .4); animation: turn 2s ease-in-out infinite; }
@keyframes turn { 0%, 20% { transform: rotate(0); } 60%, 100% { transform: rotate(-90deg); } }
@media (prefers-reduced-motion: reduce) { .panel, #rotate .phone { animation: none; } }

/* Màn thấp (điện thoại cầm ngang) */
@media (max-height: 520px) {
  .panel { gap: 6px; padding: 11px 16px; border-radius: 20px; }
  h1 { font-size: 24px; text-shadow: 0 2px 0 #b45309, 0 4px 0 #7c2d12; }
  h1 span { margin-top: 6px; font-size: 11px; }
  h2 { font-size: 20px; }
  .btn { padding: 7px 10px; font-size: 12.5px; border-radius: 11px; box-shadow: 0 3px 0 var(--edge), inset 0 1px 0 rgba(255, 255, 255, .22); margin-bottom: 2px; }
  .btn.big { padding: 8px; font-size: 14px; }
  .btn.ghost { font-size: 11.5px; padding-block: 6px; }
  .row { gap: 7px; }
  .row > .i-users, .row > .i-globe { flex-direction: row; font-size: 10.5px; }
  .row > .btn[class*='i-']::before { display: none; }
  .card { gap: 4px; padding: 6px 12px; }
  .score { padding: 3px; }
  .score b { font-size: 22px; }
  .stars { font-size: 19px; }
  .level { min-height: 64px; padding: 8px 10px; }
  .level::after { font-size: 44px; }
  #level-list { gap: 8px; }
  #room-code { font-size: 25px; padding-block: 3px; }
  #room-input { padding: 6px; }
  #screen-title .panel .help { font-size: 9.5px; line-height: 1.6; }
  .chip { padding: 2px 12px; font-size: 11px; }
  /* Chỉ màn chờ cần chừa chỗ cho nút cảm ứng */
  body.touch #screen-title .panel { width: min(400px, calc(100% - 360px)); min-width: 224px; }
}
@media (orientation: portrait) {
  body.touch #screen-title { align-items: flex-start; padding-top: 60px; }
  body.touch #screen-title .panel { max-height: calc(100% - 190px); }
  .hud-high { display: none; }
}
/* Màn hẹp: nhãn nằm trên giá trị, hai nút luôn nằm trong màn hình */
@media (max-width: 480px) {
  #hud { gap: 5px; }
  .hud-box { min-width: 0; overflow: hidden; font-size: 8.5px; padding: 3px 8px; border-radius: 12px; }
  .hud-box b { display: block; margin: 0; font-size: 12px; overflow: hidden; text-overflow: ellipsis; }
  .hud-btn { width: 36px; height: 36px; }
  .hud-btn.pause::before, .hud-btn.pause::after { top: 10px; }
  .hud-btn.pause::before { left: 11px; }
  .hud-btn.pause::after { right: 11px; }
  #level-list { grid-template-columns: 1fr; }
}
