:root {
  --bg: #1a120c;
  --panel: rgba(30, 22, 16, .9);
  --line: #5a4028;
  --text: #f4ead8;
  --muted: #c0a888;
  --accent: #ffd23f;
  --red: #c0392b;
  --font: 'VT323', ui-monospace, Consolas, monospace;
  --display: 'Bungee', 'VT323', system-ui, sans-serif;
  --emoji: 'Segoe UI Emoji', 'Apple Color Emoji', 'Noto Color Emoji', sans-serif;
}
* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--bg); color: var(--text); font-family: var(--font); font-size: 20px; user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; }
button, a { font: inherit; color: inherit; }
button { cursor: pointer; }
button:focus-visible, a:focus-visible { outline: 2px solid var(--accent); outline-offset: 2px; }
#app { position: relative; height: 100vh; height: 100dvh; overflow: hidden; }
#map, #map3d { position: absolute; inset: 0; display: block; width: 100%; height: 100%; touch-action: none; cursor: crosshair; }
#map[hidden], #map3d[hidden] { display: none; }
#boxRect { position: absolute; z-index: 2; pointer-events: none; border: 2px solid #b8ff9a; background: rgba(184, 255, 154, .12); }
#boxRect[hidden] { display: none; }
body:not(.g3d) .only3d { display: none; }

/* ---------- thanh trên ---------- */
#top {
  position: absolute; top: 0; left: 0; right: 0; z-index: 3; display: flex; align-items: center; gap: 6px;
  padding: max(6px, env(safe-area-inset-top)) max(8px, env(safe-area-inset-right)) 6px max(8px, env(safe-area-inset-left));
  pointer-events: none;
}
#top > * { pointer-events: auto; }
.spacer { flex: 1; pointer-events: none !important; }
.ibtn { display: inline-flex; align-items: center; justify-content: center; width: 38px; height: 34px; padding: 0; font-family: var(--emoji); font-size: 18px; text-decoration: none; background: var(--panel); border: 2px solid var(--line); }
.ibtn:hover { border-color: var(--accent); }
.ibtn.on { border-color: var(--accent); background: #4a3a16; }
.res { display: flex; align-items: center; gap: 4px; height: 34px; padding: 0 8px; font-family: var(--emoji); background: var(--panel); border: 2px solid var(--line); }
.res b { font-family: var(--display); font-weight: 400; font-size: 15px; color: var(--accent); }
.res.bad b { color: #ff7a6a; }

#mini { position: absolute; right: max(8px, env(safe-area-inset-right)); top: 48px; z-index: 3; width: 180px; height: 92px; background: rgba(0, 0, 0, .4); border: 2px solid var(--line); cursor: pointer; touch-action: none; }

#hint {
  position: absolute; left: 50%; top: 52px; transform: translateX(-50%); z-index: 4; max-width: min(560px, 70%);
  padding: 6px 14px; font-size: 20px; line-height: 1.1; text-align: center; pointer-events: none;
  background: rgba(60, 40, 10, .92); border: 2px solid var(--accent); box-shadow: 3px 3px 0 rgba(0, 0, 0, .5);
}
#hint[hidden] { display: none; }
#toast { position: absolute; left: 50%; bottom: 150px; transform: translateX(-50%); z-index: 4; max-width: 90%; padding: 4px 14px; font-size: 20px; text-align: center; pointer-events: none; background: rgba(20, 14, 10, .88); border: 2px solid var(--line); opacity: 0; transition: opacity .25s; }
#toast.show { opacity: 1; }
#banner { position: absolute; left: 0; right: 0; top: 26%; z-index: 4; text-align: center; pointer-events: none; font-family: var(--display); font-size: clamp(20px, 4vw, 38px); color: #fff; text-shadow: 3px 3px 0 #000; opacity: 0; padding: 0 12px; }
#banner.show { animation: banner 2.4s ease-out forwards; }
#banner.foe { color: #ff9a8a; }
#banner.ally { color: var(--accent); }
@keyframes banner { 0% { opacity: 0; transform: scale(1.3); } 12% { opacity: 1; transform: scale(1); } 80% { opacity: 1; } 100% { opacity: 0; } }

/* ---------- bảng dưới ---------- */
#panel {
  position: absolute; left: 0; right: 0; bottom: 0; z-index: 3; display: flex; gap: 6px; align-items: stretch; overflow-x: auto;
  padding: 6px max(8px, env(safe-area-inset-right)) max(6px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left));
  background: linear-gradient(rgba(26, 18, 12, .55), rgba(26, 18, 12, .95)); border-top: 2px solid var(--line);
}
.sec { flex: none; display: flex; flex-direction: column; gap: 3px; padding: 2px 6px; background: rgba(0, 0, 0, .25); border: 1px solid rgba(255, 255, 255, .08); }
.sec.grow { flex: 1; min-width: 150px; }
.lbl { font-family: var(--display); font-size: 10px; color: var(--muted); white-space: nowrap; }
.lbl small { font-family: var(--font); font-size: 13px; }
.btns { display: flex; gap: 4px; flex-wrap: wrap; align-items: center; }
.rbtn { position: relative; width: 74px; height: 70px; padding: 2px; display: flex; flex-direction: column; align-items: center; background: #3a2a1a; border: 2px solid #7a5a36; }
.rbtn canvas { width: 40px; height: 30px; }
.rbtn .nm { font-size: 14px; line-height: 1; }
.rbtn .ct { font-family: var(--emoji); font-size: 11px; color: var(--accent); line-height: 1.1; white-space: nowrap; }
.rbtn .q { position: absolute; top: -6px; right: -6px; min-width: 18px; height: 18px; padding: 0 4px; font-family: var(--display); font-size: 10px; line-height: 16px; color: #111; background: var(--accent); border: 1px solid #000; border-radius: 9px; }
.rbtn .q:empty { display: none; }
.rbtn .pg { position: absolute; left: 2px; right: 2px; bottom: 2px; height: 3px; background: #111; }
.rbtn .pg i { display: block; height: 100%; width: 0; background: #b8f2a6; }
.rbtn.poor { opacity: .5; }
.rbtn:hover:not(.poor) { border-color: var(--accent); }
.unit { position: relative; width: 44px; height: 46px; padding: 0; display: flex; flex-direction: column; align-items: center; background: #2a1e14; border: 2px solid #5a4028; }
.unit canvas { width: 36px; height: 30px; }
.unit .hp { position: absolute; left: 3px; right: 3px; bottom: 3px; height: 4px; background: #300; }
.unit .hp i { display: block; height: 100%; background: #6be06b; }
.unit.hero { border-color: var(--accent); }
.unit .more { font-family: var(--display); font-size: 12px; margin-top: 12px; }
.cmd { width: 38px; height: 34px; padding: 0; font-size: 20px; background: #3a2a1a; border: 2px solid #7a5a36; }
.cmd:hover { border-color: var(--accent); }
.cmd.sel, .cmd.on { border-color: var(--accent); background: #5a4416; }
.cmd:disabled { opacity: .35; cursor: default; }
.skill { position: relative; height: 34px; padding: 0 8px; overflow: hidden; font-family: var(--display); font-size: 10px; color: #111; background: var(--accent); border: 2px solid #000; white-space: nowrap; }
.skill i { position: absolute; left: 0; top: 0; bottom: 0; background: rgba(0, 0, 0, .55); pointer-events: none; }
.skill.cool { color: #333; }
.skill.aim { background: #ff8a3a; animation: aim .6s infinite alternate; }
@keyframes aim { to { background: #ffd23f; } }

/* ---------- lớp phủ ---------- */
.overlay { position: fixed; inset: 0; z-index: 10; display: none; align-items: center; justify-content: center; padding: 14px; background: rgba(10, 6, 4, .78); backdrop-filter: blur(2px); }
.overlay.show { display: flex; }
#title { background: radial-gradient(ellipse at 50% 0%, #5a2a14 0%, #1a120c 70%); }
.panel { width: 100%; max-width: 520px; max-height: 100%; overflow-y: auto; padding: 18px 20px; text-align: center; background: #241810; border: 2px solid #000; box-shadow: 0 0 0 2px var(--line), 8px 8px 0 rgba(0, 0, 0, .5); }
.panel.wide { max-width: 760px; }
.panel.small { max-width: 360px; }
.panel.help { max-width: 680px; text-align: left; }
.panel h1 { margin: 6px 0 2px; font-family: var(--display); font-weight: 400; font-size: clamp(28px, 6vw, 46px); line-height: 1; color: var(--accent); text-shadow: 3px 3px 0 #000; }
.panel h1 span { display: block; font-size: .8em; color: #ff6a4a; }
.panel h2 { margin: 0 0 10px; font-family: var(--display); font-weight: 400; font-size: 26px; color: var(--accent); text-align: center; }
.sub { margin: 4px 0 10px; color: var(--muted); }
.section { margin: 14px 0 6px; font-family: var(--display); font-size: 12px; color: var(--muted); letter-spacing: 1px; text-align: left; }
.home { float: left; }
.battles { display: grid; grid-template-columns: repeat(auto-fit, minmax(160px, 1fr)); gap: 8px; }
.battle { padding: 8px; text-align: left; background: #3a2616; border: 2px solid #7a5a36; }
.battle:hover:not(:disabled) { border-color: var(--accent); }
.battle b { display: block; font-family: var(--display); font-weight: 400; font-size: 14px; color: var(--accent); }
.battle small { display: block; color: var(--muted); font-size: 16px; }
.battle .st { font-family: var(--emoji); font-size: 15px; letter-spacing: 2px; }
.battle:disabled { opacity: .45; cursor: default; }
.skirmish { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; justify-content: center; }
.seg { display: inline-flex; border: 2px solid #7a5a36; }
.seg button { padding: 4px 12px; background: #3a2616; border: 0; border-right: 1px solid #7a5a36; }
.seg button:last-child { border-right: 0; }
.seg button.sel { color: #111; background: var(--accent); }
.big { padding: 10px 24px; font-family: var(--display); font-size: 17px; color: #111; background: var(--accent); border: 2px solid #000; box-shadow: 3px 3px 0 #000; }
.ghost { display: inline-block; padding: 7px 14px; font-family: var(--display); font-size: 13px; text-decoration: none; background: none; border: 2px solid var(--line); }
.ghost.small { padding: 3px 10px; font-size: 11px; }
.ghost:hover { border-color: var(--muted); }
.row { display: flex; justify-content: center; gap: 10px; flex-wrap: wrap; margin-top: 14px; }
.col { display: flex; flex-direction: column; align-items: center; gap: 10px; }
.col .ghost { min-width: 220px; }
.save-note { margin: 12px 0 0; font-size: 16px; color: var(--muted); }
.year { font-family: var(--display); font-size: 12px; color: var(--muted); }
.story { margin: 4px 0 8px; text-align: left; line-height: 1.15; }
.heroes { display: grid; gap: 6px; text-align: left; }
.heroes .h { display: flex; gap: 8px; align-items: center; padding: 4px 8px; background: #3a2616; }
.heroes canvas { flex: none; width: 48px; height: 48px; }
.heroes b { color: var(--accent); font-weight: 400; }
.heroes small { display: block; color: var(--muted); font-size: 16px; line-height: 1; }
.foe { margin: 0; text-align: left; color: #ffb0a0; }
.stars { font-family: var(--emoji); font-size: 40px; letter-spacing: 6px; }
.panel ul { margin: 0 0 12px; padding: 0; list-style: none; }
.panel li { margin-bottom: 8px; line-height: 1.1; }
.panel li b { color: var(--accent); font-weight: 400; }

/* ---------- màn thấp / hẹp ---------- */
body.short #top { padding-top: max(3px, env(safe-area-inset-top)); gap: 4px; }
body.short .ibtn, body.short .res { height: 28px; }
body.short #mini { top: 36px; width: 140px; height: 72px; }
body.short #panel { padding-top: 3px; gap: 4px; }
body.short .lbl { display: none; }
body.short .rbtn { width: 66px; height: 54px; }
body.short .rbtn canvas { width: 32px; height: 22px; }
body.short .rbtn .nm { font-size: 13px; white-space: nowrap; }
body.short .rbtn .ct { font-size: 10px; }
body.short .unit { width: 38px; height: 40px; }
body.short .cmd { width: 34px; height: 30px; font-size: 18px; }
body.short .skill { height: 30px; }
body.short #toast { bottom: 76px; }
body.short #hint { top: 36px; font-size: 18px; }
/* điện thoại dựng đứng: thanh trên gọn lại, bảng dưới xuống hai hàng cho khỏi phải cuộn ngang */
@media (max-width: 600px) {
  #top { gap: 4px; }
  .ibtn { width: 32px; }
  .res { padding: 0 4px; gap: 2px; }
  .res b { font-size: 11px; }
  .res.time { display: none; }
  #mini { top: 46px; width: 120px; height: 62px; }
  #hint { top: 116px; max-width: 86%; font-size: 17px; }
  #panel { flex-wrap: wrap; overflow-x: hidden; }
  .sec { flex: 1 1 auto; }
  .sec.grow { min-width: 0; }
  #toast { bottom: 200px; }
}
