:root {
  --bg: #0c1220;
  --surface: #141e33;
  --surface-hi: #1d2b47;
  --border: rgba(148, 163, 184, 0.24);
  --text: #f1f5f9;
  --muted: #94a3b8;
  --gold: #f59e0b;
  --gold-hi: #fde047;
  --xp: #38bdf8;
  --player: #3b82f6;
  --enemy: #ef4444;
  --emerald: #22c55e;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
  -webkit-user-select: none;
}

[hidden] {
  display: none !important;
}

[data-xgh-host] .masthead-left {
  display: none !important;
}

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  height: 100dvh;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "PingFang SC", "Microsoft YaHei", sans-serif;
  touch-action: none;
}

body {
  display: flex;
  flex-direction: column;
  padding-top: env(safe-area-inset-top, 0px);
  padding-bottom: env(safe-area-inset-bottom, 0px);
}

.masthead {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 12px;
  background: #0d1424;
  border-bottom: 1px solid var(--border);
  z-index: 20;
}

.masthead h1 {
  margin: 0;
  font-size: 14px;
  letter-spacing: 0.08em;
  font-weight: 800;
}

.masthead nav {
  display: flex;
  gap: 6px;
}

.masthead nav button {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 5px 10px;
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}

.shell {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

/* Top Dual-Base Status & Live Frontline Radar */
.top-telemetry {
  flex: 0 0 auto;
  background: #0f172a;
  border-bottom: 1px solid var(--border);
  padding: 6px 10px 6px;
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.bases-strip {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  align-items: center;
  gap: 8px;
}

.base-stat {
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.base-meta {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 11px;
  gap: 4px;
}

.era-chip {
  background: rgba(59, 130, 246, 0.22);
  border: 1px solid rgba(96, 165, 250, 0.45);
  color: #bfdbfe;
  padding: 1px 6px;
  border-radius: 999px;
  font-weight: 700;
  font-size: 10px;
  white-space: nowrap;
}

.era-chip-e {
  background: rgba(239, 68, 68, 0.2);
  border-color: rgba(248, 113, 113, 0.45);
  color: #fecaca;
}

.hp-track {
  height: 7px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.1);
  overflow: hidden;
}

.hp-fill {
  height: 100%;
  width: 100%;
  transition: width 0.14s linear;
}
.hp-fill-p { background: linear-gradient(90deg, #2563eb, #38bdf8); }
.hp-fill-e { background: linear-gradient(90deg, #f97316, #ef4444); margin-left: auto; }

.mid-status {
  font-family: ui-monospace, Menlo, monospace;
  font-size: 11px;
  font-weight: 800;
  color: var(--gold-hi);
  background: rgba(245, 158, 11, 0.14);
  border: 1px solid rgba(245, 158, 11, 0.35);
  padding: 3px 8px;
  border-radius: 6px;
  white-space: nowrap;
}

.radar-wrap {
  height: 24px;
  border-radius: 6px;
  overflow: hidden;
  border: 1px solid rgba(148, 163, 184, 0.25);
  background: #080d18;
  touch-action: none;
}

#radar-canvas {
  display: block;
  width: 100%;
  height: 100%;
  cursor: pointer;
}

/* Middle 2.5D Battlefield */
.arena-wrap {
  flex: 1 1 auto;
  min-height: 190px;
  position: relative;
  overflow: hidden;
  background: #0a101d;
  touch-action: none;
}

#battle-canvas {
  display: block;
  width: 100%;
  height: 100%;
  cursor: grab;
}
#battle-canvas:active { cursor: grabbing; }

/* Bottom Portrait-First Command Deck */
.command-deck {
  flex: 0 0 auto;
  background: #10182a;
  border-top: 2px solid rgba(245, 158, 11, 0.45);
  padding: 8px 10px 10px;
  display: flex;
  flex-direction: column;
  gap: 7px;
}

.deck-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  flex-wrap: wrap;
}

.res-pills {
  display: flex;
  gap: 6px;
  align-items: center;
}

.res-pill {
  font-family: ui-monospace, Menlo, monospace;
  font-size: 12px;
  font-weight: 800;
  padding: 4px 9px;
  border-radius: 8px;
}

.res-gold {
  background: rgba(245, 158, 11, 0.18);
  border: 1px solid rgba(245, 158, 11, 0.48);
  color: var(--gold-hi);
}

.res-xp {
  background: rgba(56, 189, 248, 0.16);
  border: 1px solid rgba(56, 189, 248, 0.42);
  color: #bae6fd;
}

.deck-controls {
  display: flex;
  gap: 5px;
}

.mini-tab, .mini-tool {
  background: var(--surface);
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 5px 10px;
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
}

.mini-tab.is-active {
  background: rgba(245, 158, 11, 0.22);
  border-color: var(--gold);
  color: #fef3c7;
}

.mini-tool.is-active {
  background: rgba(56, 189, 248, 0.2);
  border-color: var(--xp);
  color: #e0f2fe;
}

.queue-strip {
  display: flex;
  align-items: center;
  gap: 6px;
  min-height: 26px;
}

.q-label {
  font-size: 11px;
  color: var(--muted);
  font-weight: 700;
  margin-right: 2px;
}

.q-slot {
  position: relative;
  height: 24px;
  min-width: 48px;
  padding: 0 7px;
  border-radius: 6px;
  background: rgba(15, 23, 42, 0.85);
  border: 1px solid var(--border);
  color: var(--text);
  font-size: 10px;
  font-weight: 700;
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  cursor: pointer;
}

.q-slot-empty {
  min-width: 24px;
  opacity: 0.35;
  cursor: default;
}

.q-prog {
  position: absolute;
  left: 0;
  bottom: 0;
  height: 3px;
  background: var(--gold-hi);
}

.deck-main {
  display: grid;
  grid-template-columns: 1fr 112px;
  gap: 8px;
  align-items: stretch;
}

.deck-cards {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 6px;
}

.deck-cards.is-turrets {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.slot-row {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr));
  gap: 5px;
}

.slot-pill {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 7px;
  padding: 5px 4px;
  color: var(--text);
  font-size: 10px;
  font-weight: 700;
  cursor: pointer;
  text-align: center;
}

.slot-pill.is-selected {
  border-color: var(--gold-hi);
  background: rgba(245, 158, 11, 0.22);
}

.slot-pill.is-locked {
  border-style: dashed;
  color: var(--gold-hi);
}

.turret-grid {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 6px;
}

.unit-btn {
  background: linear-gradient(180deg, var(--surface-hi), var(--surface));
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 8px 6px;
  color: var(--text);
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  align-items: flex-start;
  gap: 4px;
  cursor: pointer;
  text-align: left;
  min-height: 78px;
}

.unit-btn:active:not(:disabled) { transform: scale(0.97); }
.unit-btn:disabled { opacity: 0.45; cursor: not-allowed; }

.u-top {
  width: 100%;
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 4px;
}

.u-role {
  font-size: 9px;
  font-weight: 800;
  padding: 1px 5px;
  border-radius: 4px;
  background: rgba(59, 130, 246, 0.25);
  color: #bfdbfe;
  text-transform: uppercase;
}
.u-role-ranged { background: rgba(16, 185, 129, 0.25); color: #a7f3d0; }
.u-role-heavy { background: rgba(245, 158, 11, 0.25); color: #fde68a; }

.u-cost {
  font-family: ui-monospace, Menlo, monospace;
  font-size: 11px;
  font-weight: 800;
  color: var(--gold-hi);
}

.u-name {
  font-size: 12px;
  font-weight: 800;
  line-height: 1.2;
}

.u-stats {
  font-size: 10px;
  color: var(--muted);
}

.deck-specials {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.special-btn {
  flex: 1;
  border-radius: 10px;
  border: 1px solid var(--border);
  padding: 6px 8px;
  color: var(--text);
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
  text-align: center;
  gap: 2px;
  cursor: pointer;
  background: var(--surface);
}

.special-btn:disabled { opacity: 0.48; cursor: not-allowed; }
.sp-title { font-size: 12px; font-weight: 800; line-height: 1.15; }
.sp-sub { font-family: ui-monospace, Menlo, monospace; font-size: 10px; color: var(--muted); }

.special-evolve.is-ready {
  background: linear-gradient(135deg, #d97706, #eab308);
  border-color: #fef08a;
  color: #0b101b;
  box-shadow: 0 0 18px rgba(245, 158, 11, 0.55);
}
.special-evolve.is-ready .sp-sub { color: #1c1917; font-weight: 800; }

.special-ult.is-ready {
  background: linear-gradient(135deg, #dc2626, #f97316);
  border-color: #fed7aa;
  color: #ffffff;
  box-shadow: 0 0 18px rgba(239, 68, 68, 0.55);
}
.special-ult.is-ready .sp-sub { color: #fef3c7; font-weight: 800; }

/* War Room / Pause / Guide / Victory Overlays */
.overlay-root {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: grid;
  place-items: center;
  padding: 58px 14px 16px;
  background: rgba(7, 11, 20, 0.78);
}

.overlay-card {
  width: min(680px, 100%);
  max-height: calc(100dvh - 78px);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-y;
  background: #111a2e;
  border: 1px solid rgba(245, 158, 11, 0.38);
  border-top: 3px solid var(--gold);
  border-radius: 14px;
  padding: 18px;
  box-shadow: 0 24px 56px rgba(0, 0, 0, 0.6);
}

.ov-title { margin: 0; font-size: 22px; font-weight: 900; letter-spacing: 0.04em; }
.ov-sub { margin: 4px 0 14px; font-size: 13px; color: var(--muted); line-height: 1.45; }

.mode-tabs {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
  margin-bottom: 14px;
}

.mode-tab {
  background: var(--surface);
  color: var(--muted);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 8px 6px;
  font-size: 13px;
  font-weight: 800;
  cursor: pointer;
}
.mode-tab.is-active {
  background: rgba(245, 158, 11, 0.2);
  border-color: var(--gold);
  color: #fef3c7;
}

.card-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(145px, 1fr));
  gap: 8px;
  margin-bottom: 14px;
}

.pick-card {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px;
  color: var(--text);
  text-align: left;
  cursor: pointer;
}
.pick-card.is-selected {
  border-color: var(--gold-hi);
  background: rgba(245, 158, 11, 0.16);
}
.pick-card-top {
  display: flex;
  justify-content: space-between;
  font-size: 11px;
  color: var(--gold-hi);
  font-weight: 700;
  margin-bottom: 4px;
}
.pick-card-name { font-size: 14px; font-weight: 800; }
.pick-card-desc { font-size: 11px; color: var(--muted); margin-top: 4px; line-height: 1.4; }

.btn-row { display: flex; gap: 8px; flex-wrap: wrap; margin-top: 12px; }
.btn-primary {
  flex: 1;
  min-width: 140px;
  background: linear-gradient(135deg, #f59e0b, #d97706);
  color: #0b101b;
  border: none;
  border-radius: 10px;
  padding: 12px 18px;
  font-size: 14px;
  font-weight: 900;
  cursor: pointer;
}
.btn-secondary {
  background: var(--surface);
  color: var(--text);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 11px 16px;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
}

.stats-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(120px, 1fr));
  gap: 8px;
  margin: 12px 0;
}
.stat-box {
  background: var(--surface);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 10px;
}
.stat-lbl { font-size: 11px; color: var(--muted); }
.stat-val { font-size: 18px; font-weight: 800; margin-top: 4px; }

.guide-block {
  padding-bottom: 10px;
  margin-bottom: 10px;
  border-bottom: 1px solid rgba(148, 163, 184, 0.16);
}
.guide-block h3 { margin: 0 0 5px; font-size: 14px; color: var(--gold-hi); }
.guide-block p { margin: 0; font-size: 13px; line-height: 1.55; color: #cbd5e1; }

/* Desktop wide optimization */
@media (min-width: 820px) {
  .command-deck { padding: 8px 18px 10px; }
  .deck-main { grid-template-columns: 1fr 220px; }
  .deck-specials { flex-direction: row; }
  .unit-btn { min-height: 68px; padding: 8px 12px; }
}
