:root {
  --bg: #0b1120;
  --panel: #111b2e;
  --panel-2: #17253d;
  --border: rgba(148, 163, 184, 0.22);
  --text: #f1f5f9;
  --muted: #94a3b8;
  --amber: #f59e0b;
  --amber-strong: #fbbf24;
  --cyan: #38bdf8;
  --red: #ef4444;
  --green: #34d399;
  --purple: #c084fc;
}

*, *::before, *::after {
  box-sizing: border-box;
}

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100dvh;
  overflow: hidden;
  background: radial-gradient(circle at 50% 0%, #16233b 0%, var(--bg) 72%);
  color: var(--text);
  font-family: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  touch-action: manipulation;
  overscroll-behavior: none;
}

body {
  display: flex;
  flex-direction: column;
}

button {
  font: inherit;
  color: inherit;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

img, a {
  -webkit-user-drag: none;
  user-drag: none;
}

.masthead {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 6px 12px;
  min-height: 46px;
  background: rgba(11, 17, 32, 0.92);
  border-bottom: 1px solid var(--border);
  z-index: 20;
}

.masthead h1 {
  margin: 0;
  font-size: 16px;
  letter-spacing: 0.08em;
  color: var(--amber-strong);
}

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

.masthead nav button {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 999px;
  padding: 5px 12px;
  font-size: 12px;
  font-weight: 700;
}

.shell {
  flex: 1 1 auto;
  min-height: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 6px 8px calc(6px + env(safe-area-inset-bottom));
  gap: 6px;
}

/* Top Battle HUD */
.hud-bar {
  width: 100%;
  max-width: 520px;
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  gap: 5px;
  padding: 6px 10px;
  background: rgba(17, 27, 46, 0.92);
  border: 1px solid var(--border);
  border-radius: 12px;
}

.hud-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.stage-pill {
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0.03em;
  color: var(--amber-strong);
  white-space: nowrap;
}

.hud-xp {
  flex: 1 1 auto;
  display: flex;
  align-items: center;
  gap: 6px;
  min-width: 0;
}

.lv-badge {
  font-size: 11px;
  font-weight: 800;
  background: rgba(56, 189, 248, 0.16);
  color: var(--cyan);
  border: 1px solid rgba(56, 189, 248, 0.45);
  border-radius: 6px;
  padding: 2px 6px;
  white-space: nowrap;
}

.bar-track {
  flex: 1 1 auto;
  height: 8px;
  background: rgba(15, 23, 42, 0.9);
  border-radius: 999px;
  overflow: hidden;
  border: 1px solid rgba(255, 255, 255, 0.08);
}

.bar-track > i {
  display: block;
  height: 100%;
  width: 0%;
  transition: width 120ms linear;
}

.xp-track > i {
  background: linear-gradient(90deg, #0ea5e9, #38bdf8);
}

.wall-track > i {
  background: linear-gradient(90deg, #10b981, #34d399);
}

.wall-track.low > i {
  background: linear-gradient(90deg, #dc2626, #f97316);
}

.speed-btn {
  flex: 0 0 auto;
  min-width: 40px;
  padding: 4px 8px;
  border-radius: 8px;
  border: 1px solid rgba(245, 158, 11, 0.5);
  background: rgba(245, 158, 11, 0.15);
  color: var(--amber-strong);
  font-size: 12px;
  font-weight: 800;
}

.speed-btn[aria-pressed="true"] {
  background: var(--amber);
  color: #0f172a;
}

.wall-row {
  font-size: 11px;
}

.wall-label {
  font-weight: 800;
  color: var(--muted);
  white-space: nowrap;
}

.wall-val {
  font-weight: 800;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  color: #e2e8f0;
}

.shield-badge {
  font-size: 11px;
  font-weight: 800;
  color: #7dd3fc;
  background: rgba(14, 165, 233, 0.18);
  padding: 1px 6px;
  border-radius: 6px;
  white-space: nowrap;
}

/* Main Responsive Battlefield Grid */
.arena-grid {
  flex: 1 1 auto;
  min-height: 0;
  width: 100%;
  max-width: 1120px;
  display: flex;
  align-items: stretch;
  justify-content: center;
  gap: 12px;
}

.battlefield-wrap {
  position: relative;
  flex: 0 1 auto;
  min-height: 0;
  min-width: 0;
  width: min(100%, calc((100dvh - 178px) * 480 / 760));
  height: auto;
  aspect-ratio: 480 / 760;
  max-height: 100%;
  max-width: 100%;
  align-self: center;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 14px;
  overflow: hidden;
  border: 1px solid rgba(148, 163, 184, 0.28);
  box-shadow: 0 14px 36px rgba(0, 0, 0, 0.55);
  background: #0f172a;
}

#battle-canvas {
  width: 100%;
  height: 100%;
  object-fit: contain;
  display: block;
  touch-action: none;
}

.aim-status {
  position: absolute;
  bottom: 8px;
  left: 50%;
  transform: translateX(-50%);
  padding: 3px 10px;
  border-radius: 999px;
  background: rgba(11, 17, 32, 0.8);
  border: 1px solid rgba(255, 255, 255, 0.14);
  font-size: 11px;
  color: #cbd5e1;
  white-space: nowrap;
  pointer-events: none;
}

.aim-status.active {
  border-color: var(--amber);
  color: var(--amber-strong);
}

/* Desktop / Wide Side Rails */
.side-rail {
  width: 248px;
  flex: 0 0 248px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  min-height: 0;
}

.rail-card {
  background: rgba(17, 27, 46, 0.9);
  border: 1px solid var(--border);
  border-radius: 12px;
  padding: 10px 12px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  overflow: hidden;
}

.rail-card h2 {
  margin: 0;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--amber-strong);
}

.gun-tags {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
}

.tag-chip {
  font-size: 11px;
  font-weight: 700;
  padding: 2px 7px;
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.07);
  border: 1px solid rgba(255, 255, 255, 0.1);
}

.rail-skill-list, .threat-list, .dps-list {
  display: flex;
  flex-direction: column;
  gap: 6px;
  font-size: 12px;
}

.rail-skill-row, .threat-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 6px;
  padding: 5px 7px;
  border-radius: 8px;
  background: rgba(15, 23, 42, 0.65);
}

.dps-row {
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.dps-head {
  display: flex;
  justify-content: space-between;
  font-size: 11px;
}

.dps-bar {
  height: 5px;
  background: rgba(15, 23, 42, 0.8);
  border-radius: 999px;
  overflow: hidden;
}

.dps-bar > i {
  display: block;
  height: 100%;
  background: var(--amber);
}

/* Bottom Skill Cooldown Dock */
.skill-dock {
  width: 100%;
  max-width: 520px;
  flex: 0 0 auto;
}

.dock-slots {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
}

.dock-slot {
  position: relative;
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 6px 8px;
  border-radius: 10px;
  background: rgba(17, 27, 46, 0.92);
  border: 1px solid var(--border);
  overflow: hidden;
  min-height: 40px;
}

.dock-slot.empty {
  opacity: 0.45;
  justify-content: center;
  font-size: 11px;
  color: var(--muted);
}

.dock-slot .cd-fill {
  position: absolute;
  inset: 0;
  background: rgba(56, 189, 248, 0.14);
  transform-origin: left center;
  pointer-events: none;
}

.dock-slot .sk-ico {
  font-size: 16px;
  z-index: 1;
}

.dock-slot .sk-meta {
  display: flex;
  flex-direction: column;
  min-width: 0;
  z-index: 1;
}

.dock-slot .sk-name {
  font-size: 11px;
  font-weight: 700;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.dock-slot .sk-cd {
  font-size: 10px;
  color: var(--muted);
  font-variant-numeric: tabular-nums;
}

/* Overlays & Native Scrollable Modals */
.overlay-root {
  position: fixed;
  inset: 0;
  z-index: 40;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: calc(54px + env(safe-area-inset-top)) 12px calc(14px + env(safe-area-inset-bottom));
  background: rgba(6, 10, 20, 0.78);
  backdrop-filter: blur(6px);
}

.overlay-root[hidden] {
  display: none;
}

.overlay-card {
  width: 100%;
  max-width: 560px;
  max-height: 100%;
  background: linear-gradient(180deg, #132038 0%, #0e1729 100%);
  border: 1px solid rgba(148, 163, 184, 0.3);
  border-radius: 16px;
  padding: 16px;
  box-shadow: 0 24px 60px rgba(0, 0, 0, 0.65);
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.scrollable-panel {
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-y;
  overscroll-behavior: contain;
}

.modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.modal-header h2 {
  margin: 0;
  font-size: 18px;
  letter-spacing: 0.03em;
  color: #f8fafc;
}

.sub-line {
  margin: 0;
  font-size: 12px;
  color: var(--muted);
}

.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 16px;
  border-radius: 11px;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  font-size: 14px;
  font-weight: 800;
  transition: transform 90ms ease, background 120ms ease;
}

.btn:active {
  transform: scale(0.97);
}

.btn.primary {
  background: linear-gradient(135deg, #f59e0b, #ea580c);
  border-color: rgba(254, 240, 138, 0.45);
  color: #0b1120;
}

.btn.cyan {
  background: linear-gradient(135deg, #0284c7, #2563eb);
  border-color: rgba(125, 211, 252, 0.45);
  color: #ffffff;
}

.btn.danger {
  background: rgba(220, 38, 38, 0.2);
  border-color: rgba(239, 68, 68, 0.45);
  color: #fecaca;
}

.btn.sm {
  padding: 6px 11px;
  font-size: 12px;
  border-radius: 8px;
}

.btn:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.hero-banner {
  padding: 14px;
  border-radius: 12px;
  background: radial-gradient(circle at 80% 15%, rgba(245, 158, 11, 0.22), rgba(15, 23, 42, 0.9) 70%);
  border: 1px solid rgba(245, 158, 11, 0.35);
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.hero-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  flex-wrap: wrap;
}

.hero-title {
  margin: 0;
  font-size: 22px;
  font-weight: 900;
  letter-spacing: 0.05em;
  color: var(--amber-strong);
}

.resource-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.res-pill {
  font-size: 12px;
  font-weight: 800;
  padding: 4px 10px;
  border-radius: 999px;
  background: rgba(15, 23, 42, 0.85);
  border: 1px solid var(--border);
}

.checkpoint-box {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 10px 12px;
  border-radius: 10px;
  background: rgba(14, 165, 233, 0.14);
  border: 1px solid rgba(56, 189, 248, 0.45);
  flex-wrap: wrap;
}

.base-actions {
  display: grid;
  grid-template-columns: repeat(2, 1fr);
  gap: 8px;
}

.nav-tabs {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
}

.tab-btn {
  padding: 8px 6px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: rgba(15, 23, 42, 0.72);
  font-size: 12px;
  font-weight: 800;
  text-align: center;
}

.tab-btn.active {
  background: rgba(245, 158, 11, 0.2);
  border-color: var(--amber);
  color: var(--amber-strong);
}

/* Campaign Stage Grid */
.chapter-block {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 10px;
  border-radius: 10px;
  background: rgba(15, 23, 42, 0.55);
  border: 1px solid var(--border);
}

.chapter-head {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 13px;
  font-weight: 800;
}

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

.stage-tile {
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  padding: 8px;
  border-radius: 9px;
  border: 1px solid var(--border);
  background: var(--panel-2);
  text-align: left;
}

.stage-tile.selected {
  border-color: var(--amber);
  box-shadow: 0 0 0 1px var(--amber);
}

.stage-tile.boss {
  border-color: rgba(239, 68, 68, 0.55);
}

.stage-tile:disabled {
  opacity: 0.4;
}

.stage-num {
  font-size: 11px;
  font-weight: 900;
  color: var(--amber-strong);
}

.stage-name {
  font-size: 11px;
  font-weight: 700;
  color: #e2e8f0;
  line-height: 1.25;
}

.stage-stars {
  font-size: 11px;
  color: #facc15;
}

/* Roguelike Upgrade 3-Card Picker */
.upgrade-grid {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.upgrade-card {
  display: flex;
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 12px;
  border-radius: 12px;
  border: 1px solid var(--border);
  background: linear-gradient(135deg, rgba(23, 37, 61, 0.96), rgba(15, 23, 42, 0.96));
  text-align: left;
}

.upgrade-card:hover {
  border-color: var(--amber);
}

.upgrade-card.synergy {
  border-color: rgba(192, 132, 252, 0.7);
  background: linear-gradient(135deg, rgba(64, 29, 97, 0.75), rgba(15, 23, 42, 0.95));
}

.upgrade-card.evo {
  border-color: rgba(250, 204, 21, 0.85);
  background: linear-gradient(135deg, rgba(120, 53, 15, 0.78), rgba(15, 23, 42, 0.95));
}

.up-icon {
  flex: 0 0 44px;
  height: 44px;
  border-radius: 11px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 22px;
  background: rgba(15, 23, 42, 0.8);
  border: 1px solid rgba(255, 255, 255, 0.14);
}

.up-body {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.up-title-row {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

.up-name {
  font-size: 14px;
  font-weight: 900;
}

.up-badge {
  font-size: 10px;
  font-weight: 900;
  padding: 1px 6px;
  border-radius: 999px;
  background: rgba(56, 189, 248, 0.2);
  color: var(--cyan);
}

.up-badge.synergy {
  background: rgba(192, 132, 252, 0.25);
  color: #e9d5ff;
}

.up-badge.evo {
  background: rgba(245, 158, 11, 0.28);
  color: #fef08a;
}

.up-desc {
  font-size: 12px;
  color: #cbd5e1;
  line-height: 1.35;
}

.up-key {
  flex: 0 0 24px;
  height: 24px;
  border-radius: 6px;
  background: rgba(255, 255, 255, 0.08);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 11px;
  font-weight: 800;
  color: var(--muted);
}

/* Armory, Chips & Codex Lists */
.item-list {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.list-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 12px;
  border-radius: 10px;
  background: rgba(15, 23, 42, 0.72);
  border: 1px solid var(--border);
}

.list-row.equipped {
  border-color: var(--green);
}

.row-main {
  flex: 1 1 auto;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 2px;
}

.row-title {
  font-size: 13px;
  font-weight: 800;
}

.row-desc {
  font-size: 11px;
  color: var(--muted);
  line-height: 1.35;
}

.slot-pills {
  display: flex;
  gap: 5px;
  flex-wrap: wrap;
}

.slot-pill {
  padding: 5px 9px;
  border-radius: 999px;
  border: 1px solid var(--border);
  background: rgba(15, 23, 42, 0.75);
  font-size: 11px;
  font-weight: 800;
}

.slot-pill.active {
  border-color: var(--cyan);
  background: rgba(56, 189, 248, 0.18);
  color: #bae6fd;
}

.tier-1 { color: #e2e8f0; }
.tier-2 { color: #38bdf8; }
.tier-3 { color: #c084fc; }
.tier-4 { color: #facc15; }

.summary-stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
}

.stat-box {
  padding: 8px;
  border-radius: 10px;
  background: rgba(15, 23, 42, 0.75);
  border: 1px solid var(--border);
  text-align: center;
}

.stat-box b {
  display: block;
  font-size: 16px;
  color: var(--amber-strong);
}

.stat-box span {
  font-size: 11px;
  color: var(--muted);
}

/* Responsive Breakpoints */
@media (max-width: 980px) {
  .side-rail {
    display: none !important;
  }
}

@media (max-height: 540px) and (orientation: landscape) {
  .hud-bar {
    padding: 4px 8px;
    gap: 3px;
  }
  .dock-slot {
    min-height: 32px;
    padding: 4px 6px;
  }
  .aim-status {
    display: none !important;
  }
}
