* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; overflow: hidden; background: #5fb4ff;
  font-family: "Segoe UI", "Nunito", system-ui, -apple-system, Roboto, sans-serif; color: #23324a;
  user-select: none; -webkit-user-select: none; overscroll-behavior: none; }
#scene { position: fixed; inset: 0; width: 100vw; height: 100dvh; display: block; }
.hidden { display: none !important; }

/* màn hình phủ */
.screen { position: fixed; inset: 0; display: flex; align-items: center; justify-content: center;
  padding: max(12px, env(safe-area-inset-top)) 12px max(12px, env(safe-area-inset-bottom));
  background: rgba(20, 50, 90, .25); backdrop-filter: blur(2px); z-index: 20; overflow-y: auto; }
#menu { background: transparent; backdrop-filter: none; }
.panel { background: rgba(255,255,255,.94); border-radius: 26px; padding: 22px 24px; width: min(420px, 100%);
  box-shadow: 0 10px 0 rgba(0,0,0,.12), 0 20px 50px rgba(0,0,0,.25); text-align: center;
  display: flex; flex-direction: column; gap: 10px; margin: auto; animation: pop .25s ease-out; }
.panel.wide { width: min(680px, 100%); }
@keyframes pop { from { transform: scale(.9); opacity: 0; } to { transform: none; opacity: 1; } }
h1 { margin: 0; font-size: 44px; color: #2f7de1; text-shadow: 0 3px 0 #ffd23f; letter-spacing: 1px; }
h2 { margin: 0 0 4px; font-size: 24px; }
.subtitle { margin: -4px 0 8px; font-size: 18px; font-weight: 700; color: #e0453a; }
.logo { font-size: 64px; line-height: 1; animation: bob 2s ease-in-out infinite; }
.logo.spin { animation: bob 1s ease-in-out infinite; }
@keyframes bob { 50% { transform: translateY(-6px) rotate(-4deg); } }

button { font: inherit; border: 0; cursor: pointer; border-radius: 16px; padding: 12px 16px; font-weight: 700;
  font-size: 17px; background: #ffd23f; color: #3a2a00; box-shadow: 0 4px 0 #c99a00; transition: transform .06s; touch-action: manipulation; }
button:active { transform: translateY(3px); box-shadow: 0 1px 0 #c99a00; }
button.big { font-size: 21px; padding: 15px; background: linear-gradient(#4fb3ff, #1f86ff); color: white; box-shadow: 0 5px 0 #1162c9, 0 0 18px rgba(79,179,255,.55);
  text-shadow: 0 2px 0 rgba(0,0,0,.25); }
button.big:active { box-shadow: 0 2px 0 #1162c9; }
/* nút menu chính: mỗi nút một màu tươi */
.title-panel [data-go=levels] { background: linear-gradient(#ffb84d, #ff8a00); box-shadow: 0 5px 0 #c96a00, 0 0 18px rgba(255,170,60,.55); }
.title-panel [data-go=free] { background: linear-gradient(#5be37d, #1fbf4f); box-shadow: 0 5px 0 #14903a, 0 0 18px rgba(80,220,120,.5); }
.title-panel [data-go=help] { background: linear-gradient(#7fd6ff, #33b5f5); color: #fff; box-shadow: 0 5px 0 #1d8cc4; text-shadow: 0 2px 0 rgba(0,0,0,.2); }
button.secondary { background: #fff; color: #1f6fd1; border: 3px solid #8cc8ff; box-shadow: 0 4px 0 #8cc8ff; }
button.choice { flex: 1; background: #dff1ff; box-shadow: 0 4px 0 #9fd0ff; color: #1f5fa8; }
button.choice.selected { background: linear-gradient(#4fb3ff, #1f86ff); color: #fff; box-shadow: 0 4px 0 #1162c9, 0 0 14px rgba(79,179,255,.6); }
.row { display: flex; gap: 8px; }
.label { margin: 6px 0 0; font-weight: 700; text-align: left; }
.hint, .tip { color: #5a6b85; font-size: 15px; margin: 4px 0; }
.tip::before { content: "💡 "; }
.map-name { font-size: 20px; font-weight: 800; margin: 0; color: #2f7de1; }

/* chọn màn */
.level-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(180px, 1fr)); gap: 10px; }
.level-card { background: #e3f3ff; border: 3px solid #8cc8ff; border-radius: 18px; padding: 12px; text-align: left; box-shadow: 0 4px 0 #8cc8ff; color: #1d3b66; }
.level-card .num { font-size: 13px; color: #6b7c96; font-weight: 700; }
.level-card .name { font-size: 17px; font-weight: 800; margin: 2px 0 6px; }
.level-card .stars { font-size: 20px; letter-spacing: 2px; }
.level-card .units { font-size: 18px; }
.level-card.locked { opacity: .75; background: #eef1f5; border-color: #cfd8e3; box-shadow: 0 4px 0 #cfd8e3; }
.level-card.done { background: #fff3c4; border-color: #ffc93c; box-shadow: 0 4px 0 #ffc93c; }

/* cách chơi */
.help p { text-align: left; margin: 4px 0; font-size: 16px; }
.unit-help { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; text-align: left; }
.unit-help div { background: #eef3fa; border-radius: 12px; padding: 8px 10px; font-size: 14px; }
.unit-help b { font-size: 15px; }

/* câu đố */
.quiz-head { font-size: 20px; font-weight: 800; color: #8a6d3b; }
.quiz-q { font-size: 19px; font-weight: 700; margin: 4px 0 8px; }
.quiz-a { display: flex; flex-direction: column; gap: 8px; }
.quiz-a button { background: #fff; border: 3px solid #8cc8ff; box-shadow: 0 4px 0 #8cc8ff; color: #1d3b66; }
.quiz-a button.right { background: #4cd964; color: #fff; box-shadow: 0 4px 0 #2a9c40; }
.quiz-a button.wrong { background: #ff6b5e; color: #fff; box-shadow: 0 4px 0 #c43d32; }
.quiz-why { background: #fff6d1; border-radius: 14px; padding: 10px; margin: 4px 0; font-size: 16px; }

/* hội thoại */
#dialog { position: fixed; left: 0; right: 0; bottom: 0; z-index: 30; padding: 12px 12px max(14px, env(safe-area-inset-bottom));
  display: flex; flex-direction: column; align-items: center; gap: 8px; }
.dialog-box { width: min(760px, 100%); background: rgba(255,255,255,.97); border-radius: 22px; padding: 14px 16px;
  display: flex; gap: 14px; align-items: center; box-shadow: 0 8px 0 rgba(0,0,0,.12), 0 16px 40px rgba(0,0,0,.25); cursor: pointer; animation: pop .2s ease-out; }
.portrait { font-size: 46px; width: 70px; height: 70px; flex: none; border-radius: 50%; display: grid; place-items: center; background: #eef3fa; border: 4px solid #2f7de1; }
.dlg-name { font-weight: 800; font-size: 17px; }
.dlg-text { font-size: 19px; line-height: 1.4; min-height: 2.8em; }
.dlg-body { flex: 1; }
.dlg-next { font-size: 20px; color: #2f7de1; animation: blink 1s infinite; }
@keyframes blink { 50% { opacity: .2; } }
.skip { position: fixed; top: max(12px, env(safe-area-inset-top)); right: 12px; font-size: 15px; padding: 8px 12px; background: #fff; border: 3px solid #8cc8ff; box-shadow: 0 3px 0 #8cc8ff; color: #1f6fd1; }

/* HUD */
#hud { position: fixed; inset: 0; pointer-events: none; z-index: 10; }
#hud button, #hud .cards { pointer-events: auto; }
.topbar { position: absolute; top: max(8px, env(safe-area-inset-top)); left: 50%; transform: translateX(-50%); width: min(720px, calc(100% - 120px));
  display: flex; gap: 10px; align-items: center; }
.hp { flex: 1; display: flex; align-items: center; gap: 6px; font-weight: 800; color: #fff; text-shadow: 0 2px 0 rgba(0,0,0,.35); font-size: 15px; }
.hp-track { flex: 1; height: 18px; background: rgba(0,0,0,.3); border-radius: 10px; overflow: hidden; border: 2px solid rgba(255,255,255,.8); }
.hp-fill { height: 100%; width: 100%; transition: width .2s; }
.hp.blue .hp-fill { background: linear-gradient(#6fb0ff, #2f7de1); }
.hp.red .hp-fill { background: linear-gradient(#ff8a80, #e0453a); margin-left: auto; }
.hp.red .hp-track { display: flex; justify-content: flex-end; }
.timer { background: rgba(255,255,255,.92); border-radius: 12px; padding: 4px 10px; font-weight: 800; font-size: 18px; min-width: 64px; text-align: center; }
.timer.overtime { background: #ffd23f; animation: blink 1s infinite; }
.top-buttons { position: absolute; top: max(8px, env(safe-area-inset-top)); right: 8px; display: flex; gap: 6px; }
button.icon { width: 46px; height: 46px; padding: 0; font-size: 21px; background: linear-gradient(#ffe066, #ffc21a); border: 3px solid #fff; box-shadow: 0 3px 0 #d69a00, 0 0 12px rgba(255,200,40,.6); }
.bubble { position: absolute; top: calc(max(8px, env(safe-area-inset-top)) + 46px); left: 50%; transform: translateX(-50%);
  background: #fff; border-radius: 18px; padding: 8px 14px; font-size: 16px; font-weight: 600; max-width: min(520px, 92%);
  box-shadow: 0 6px 20px rgba(0,0,0,.2); display: flex; gap: 8px; align-items: center; animation: pop .2s ease-out; }
.bubble span:first-child { font-size: 26px; }
.toast { position: absolute; bottom: 150px; left: 50%; transform: translateX(-50%); background: rgba(30,30,40,.85); color: #fff;
  padding: 8px 14px; border-radius: 12px; font-size: 15px; font-weight: 600; }
.bottombar { position: absolute; left: 0; right: 0; bottom: 0; padding: 8px 8px max(10px, env(safe-area-inset-bottom));
  display: flex; flex-direction: column; align-items: center; gap: 6px; }
.gold { display: flex; align-items: center; gap: 6px; width: min(460px, 96%); font-size: 20px; }
.gold b { color: #fff; text-shadow: 0 2px 0 rgba(0,0,0,.4); font-size: 20px; min-width: 26px; }
.gold-track { flex: 1; height: 16px; background: rgba(0,0,0,.3); border-radius: 10px; overflow: hidden; border: 2px solid rgba(255,255,255,.8); }
.gold-fill { height: 100%; background: linear-gradient(#ffe57a, #f5b700); width: 50%; }
.cards { display: flex; gap: 8px; }
.card { width: 84px; padding: 20px 4px 8px; border-radius: 16px; background: linear-gradient(#ffffff, #e6f4ff); border: 3px solid #8cc8ff; box-shadow: 0 5px 0 #5aa9f2, 0 0 12px rgba(140,200,255,.6); color: #1d3b66;
  display: flex; flex-direction: column; align-items: center; gap: 1px; position: relative; overflow: hidden; }
.card .ic { font-size: 30px; line-height: 1.1; }
.card .nm { font-size: 12px; font-weight: 700; line-height: 1.1; }
.card .cost { position: absolute; top: 3px; left: 5px; background: #ffd23f; border-radius: 10px; font-size: 13px; padding: 0 6px; }
.card .key { position: absolute; top: 3px; right: 6px; font-size: 11px; color: #3d7fd0; font-weight: 800; }
.card.poor { opacity: .82; box-shadow: 0 5px 0 #9fb7cf; border-color: #c4d6e8; }
.card.poor .cost { background: #ffb3a8; }
.card.selected { background: linear-gradient(#4fb3ff, #1f86ff); color: #fff; border-color: #fff36b; box-shadow: 0 5px 0 #1162c9, 0 0 18px rgba(255,240,100,.9); transform: translateY(-6px); }
.card.selected .key { color: #fff36b; }
.card .charge { position: absolute; left: 0; bottom: 0; height: 4px; background: #f5b700; }

/* kết quả */
.res-stars { font-size: 48px; letter-spacing: 6px; }
.res-stars span { display: inline-block; animation: pop .4s ease-out backwards; }
.res-goals { list-style: none; padding: 0; margin: 0; text-align: left; }
.res-goals li { padding: 4px 0; font-size: 16px; }

@media (max-width: 520px) {
  h1 { font-size: 36px; }
  .cards { gap: 6px; flex-wrap: wrap; justify-content: center; }
  .card { width: min(80px, calc((100vw - 44px) / 4)); }
  .toast { bottom: 215px; }
  .card .nm { font-size: 11px; }
  .card .ic { font-size: 26px; }
  .topbar { width: calc(100% - 112px); left: 8px; transform: none; }
  .hp-name { display: none; }
  .dlg-text { font-size: 17px; }
  .portrait { width: 56px; height: 56px; font-size: 36px; }
  .toast { bottom: 130px; }
}
@media (max-height: 460px) { /* điện thoại xoay ngang */
  .panel { padding: 12px 16px; gap: 6px; }
  .logo { font-size: 40px; }
  h1 { font-size: 30px; }
  button.big { padding: 10px; font-size: 18px; }
  .card { width: 66px; padding: 18px 3px 4px; }
  .card .ic { font-size: 22px; }
  .card .nm { display: none; }
}

/* ---------- kim cương & kho vũ khí ---------- */
.gem { display: inline-block; width: 18px; height: 20px; vertical-align: -4px; margin: 0 2px;
  clip-path: polygon(50% 0, 100% 35%, 50% 100%, 0 35%); background: linear-gradient(135deg, #fff 0 18%, var(--gem) 19% 70%, rgba(0,0,0,.25) 71%); }
.gem.xanh { --gem: #3aa0ff; } .gem.tim { --gem: #a05cff; } .gem.vang { --gem: #ffc21a; } .gem.do { --gem: #ff3b5c; }
.gem-bar { display: flex; justify-content: center; flex-wrap: wrap; gap: 8px; font-weight: 800; font-size: 18px; }
.gem-bar span.cnt { background: #fff; border: 3px solid #8cc8ff; border-radius: 14px; padding: 3px 10px; }
.shop-grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(190px, 1fr)); gap: 10px; text-align: left; }
.shop-card { background: linear-gradient(#ffffff, #eef7ff); border: 3px solid #8cc8ff; border-radius: 18px; padding: 10px 12px; box-shadow: 0 4px 0 #8cc8ff; }
.shop-card.max { border-color: #ffd23f; box-shadow: 0 4px 0 #ffd23f, 0 0 14px rgba(255,210,63,.6); }
.shop-card .top { display: flex; align-items: center; gap: 8px; }
.shop-card .ic { font-size: 30px; }
.shop-card .nm { font-weight: 800; font-size: 16px; }
.shop-card .wp { font-size: 14px; color: #1f6fd1; font-weight: 700; }
.shop-card .tiers { font-size: 16px; letter-spacing: 2px; margin: 4px 0; }
.shop-card .stat { font-size: 13px; color: #4a5a75; }
.shop-card button { width: 100%; margin-top: 6px; padding: 9px; font-size: 15px; }
.shop-card button[disabled] { opacity: .55; cursor: default; filter: grayscale(.4); }
.shop-msg { background: #fff3c4; border-radius: 12px; padding: 8px; font-weight: 700; margin: 0; }
.res-reward { background: #fff3c4; border: 3px solid #ffc93c; border-radius: 16px; padding: 8px 10px; font-weight: 800; font-size: 18px; }
.card .tier { position: absolute; bottom: 6px; right: 5px; font-size: 13px; }

/* dòng bản quyền */
.copyright { margin: 4px 0 0; font-size: 13px; color: #5a6b85; font-weight: 600; }
