:root {
  --bg0: #0a0c1b;
  --bg1: #141833;
  --panel: rgba(28, 34, 66, 0.66);
  --panel-br: rgba(120, 140, 220, 0.18);
  --ink: #e7ecff;
  --muted: #97a0c8;
  --accent: #56d6ff;
  --accent2: #36e0a0;
  --danger: #ff556b;
  --gold: #ffd45e;
  --shadow: 0 10px 30px rgba(0, 0, 0, 0.45);
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
}

body {
  font-family: "Segoe UI", system-ui, -apple-system, "Helvetica Neue", Arial, sans-serif;
  color: var(--ink);
  background:
    radial-gradient(1200px 600px at 50% -10%, #243066 0%, transparent 60%),
    radial-gradient(800px 500px at 90% 110%, #2a1c4a 0%, transparent 55%),
    linear-gradient(160deg, var(--bg0), var(--bg1));
  min-height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 18px 14px 36px;
  -webkit-user-select: none;
  user-select: none;
}

.app { width: 100%; max-width: 540px; }

header { text-align: center; margin-bottom: 12px; }
header h1 {
  margin: 0;
  font-size: 26px;
  letter-spacing: 3px;
  font-weight: 800;
  background: linear-gradient(90deg, var(--accent), var(--accent2));
  -webkit-background-clip: text;
  background-clip: text;
  color: transparent;
  text-shadow: 0 2px 18px rgba(86, 214, 255, 0.25);
}
header .sub { margin-top: 3px; font-size: 12.5px; color: var(--muted); letter-spacing: .5px; }

.topbar {
  display: flex; align-items: center; gap: 8px;
  margin-bottom: 10px;
}
.lvl-name {
  flex: 1; min-width: 0;
  font-weight: 700; font-size: 15px;
  white-space: nowrap; overflow: hidden; text-overflow: ellipsis;
}
.lvl-name b { color: var(--accent); margin-right: 6px; font-variant-numeric: tabular-nums; }
.pills { display: flex; gap: 6px; flex-shrink: 0; }
.pill {
  display: inline-flex; align-items: center; gap: 5px;
  background: var(--panel); border: 1px solid var(--panel-br);
  border-radius: 999px; padding: 5px 10px; font-size: 12.5px;
  color: var(--ink); font-variant-numeric: tabular-nums;
}
.pill .ic { font-size: 13px; line-height: 1; }
.pill.gem .ic { color: var(--accent); }
.pill.mv .ic { color: var(--muted); }
.pill.snd {
  cursor: pointer; font-size: 14px; line-height: 1; padding: 5px 9px;
  font-family: inherit; transition: transform .08s ease, border-color .15s ease, background .15s ease;
}
.pill.snd:hover { border-color: var(--accent); background: var(--panel-2, #222a58); }
.pill.snd:active { transform: scale(0.9); }
.pill.snd.off { opacity: 0.55; }

.stage {
  position: relative;
  background: var(--panel);
  border: 1px solid var(--panel-br);
  border-radius: 18px;
  padding: 12px;
  box-shadow: var(--shadow);
  display: flex; justify-content: center;
}
canvas { display: block; border-radius: 10px; touch-action: none; }

.hint {
  margin: 10px 2px 0;
  font-size: 12.5px; line-height: 1.5;
  color: var(--muted); min-height: 34px;
}
.hint b { color: var(--ink); }

.controls {
  margin-top: 12px;
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center; gap: 14px;
}
.actions { display: flex; flex-direction: column; gap: 8px; }
.actions.right { align-items: flex-end; }

.dpad {
  display: grid;
  grid-template-columns: repeat(3, 52px);
  grid-template-rows: repeat(3, 52px);
  gap: 6px;
  justify-content: center;
}
.dpad button { font-size: 22px; }
.dpad .up { grid-area: 1 / 2; }
.dpad .left { grid-area: 2 / 1; }
.dpad .right { grid-area: 2 / 3; }
.dpad .down { grid-area: 3 / 2; }
.dpad .mid {
  grid-area: 2 / 2;
  border: none; background: transparent; box-shadow: none;
  pointer-events: none;
}

button {
  font-family: inherit; color: var(--ink);
  background: linear-gradient(180deg, rgba(70, 84, 140, 0.55), rgba(40, 48, 92, 0.55));
  border: 1px solid var(--panel-br);
  border-radius: 12px;
  padding: 9px 12px; font-size: 13px; font-weight: 600;
  cursor: pointer; transition: transform .06s ease, filter .15s ease, background .15s ease;
  box-shadow: 0 4px 12px rgba(0,0,0,.25);
}
button:hover { filter: brightness(1.12); }
button:active { transform: translateY(1px) scale(.97); }
button:disabled { opacity: .4; cursor: not-allowed; filter: none; }
button.ghost { background: var(--panel); }
.btn-wide { width: 100%; }

.levelbar {
  margin-top: 12px; display: flex; gap: 8px; align-items: center;
}
.levelbar .spacer { flex: 1; }

/* overlay (win / death) */
.overlay {
  position: absolute; inset: 0;
  display: none; align-items: center; justify-content: center;
  background: rgba(8, 10, 24, 0.72);
  backdrop-filter: blur(3px);
  border-radius: 18px; z-index: 5;
}
.overlay.show { display: flex; animation: fade .2s ease; }
@keyframes fade { from { opacity: 0; } to { opacity: 1; } }
.card {
  background: linear-gradient(180deg, #1b2247, #131836);
  border: 1px solid var(--panel-br);
  border-radius: 16px; padding: 22px 26px; text-align: center;
  box-shadow: var(--shadow); max-width: 88%;
  transform: translateY(6px) scale(.98); animation: pop .25s ease forwards;
}
@keyframes pop { to { transform: translateY(0) scale(1); } }
.card h2 { margin: 0 0 6px; font-size: 22px; }
.card .stars { font-size: 34px; letter-spacing: 6px; margin: 6px 0 4px; }
.card .stars .on { color: var(--gold); text-shadow: 0 0 16px rgba(255,212,94,.6); }
.card .stars .off { color: rgba(255,255,255,.18); }
.card .meta { color: var(--muted); font-size: 13px; margin-bottom: 16px; }
.card .meta b { color: var(--ink); }
.card .row { display: flex; gap: 10px; justify-content: center; }

/* level select modal */
.modal {
  position: fixed; inset: 0; z-index: 20;
  display: none; align-items: center; justify-content: center;
  background: rgba(6, 8, 20, 0.7); backdrop-filter: blur(4px);
  padding: 20px;
}
.modal.show { display: flex; }
.modal .box {
  background: linear-gradient(180deg, #1b2247, #131836);
  border: 1px solid var(--panel-br); border-radius: 18px;
  padding: 18px; width: 100%; max-width: 440px; box-shadow: var(--shadow);
  max-height: 86vh; display: flex; flex-direction: column;
}
.modal h3 { margin: 2px 4px 12px; font-size: 17px; flex: 0 0 auto; }
.lvl-grid {
  display: grid; grid-template-columns: repeat(5, 1fr); gap: 8px;
  overflow-y: auto; flex: 1 1 auto; min-height: 0;
  padding: 2px 8px 6px 2px; /* chừa chỗ cho thanh cuộn */
}
.lvl-grid::-webkit-scrollbar { width: 8px; }
.lvl-grid::-webkit-scrollbar-thumb { background: rgba(120,140,210,.4); border-radius: 8px; }
.lvl-cell {
  aspect-ratio: 1 / 1; border-radius: 11px;
  display: flex; flex-direction: column; align-items: center; justify-content: center;
  gap: 2px; font-weight: 700; font-size: 15px; position: relative;
}
.lvl-cell .s { font-size: 10px; letter-spacing: 1px; color: var(--gold); height: 12px; }
.lvl-cell.locked { opacity: .45; }
.lvl-cell.current { outline: 2px solid var(--accent); }
.modal .close { margin-top: 14px; }

.toast {
  position: absolute; left: 50%; top: 14px; transform: translateX(-50%);
  background: rgba(255, 85, 107, .92); color: #fff; font-weight: 700;
  padding: 7px 14px; border-radius: 999px; font-size: 13px;
  opacity: 0; pointer-events: none; transition: opacity .15s ease; z-index: 8;
}
.toast.show { opacity: 1; }

@media (max-width: 430px) {
  .dpad { grid-template-columns: repeat(3, 46px); grid-template-rows: repeat(3, 46px); }
  .pill { padding: 5px 8px; font-size: 12px; }
}
