:root {
  --bg: #0b1020;
  --panel: #121a33;
  --panel-2: #182242;
  --border: #2b3d73;
  --text: #e8eeff;
  --muted: #93a0c7;
  --accent: #ffd166;
  --player: #4cd964;
  --enemy: #ff5a5f;
  --brick: #c46a3a;
  --steel: #8b96b0;
  --water: #2f7fd6;
  --grass: #2f9e44;
  --base: #f4c430;
  --shadow: 0 12px 40px rgba(0, 0, 0, 0.45);
  --radius: 16px;
  --side-w: 220px;
  font-family: "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
}

* { box-sizing: border-box; margin: 0; padding: 0; }

html, body {
  height: 100%;
  background:
    radial-gradient(ellipse at top, #1a2750 0%, transparent 55%),
    radial-gradient(ellipse at bottom right, #2a1430 0%, transparent 45%),
    var(--bg);
  color: var(--text);
  overflow: hidden;
}

body {
  min-height: 100%;
  min-height: 100dvh;
}

#app {
  height: 100vh;
  height: 100dvh;
  max-width: 1800px;
  margin: 0 auto;
  padding: 8px 12px 6px;
  display: flex;
  flex-direction: column;
  gap: 8px;
  overflow: hidden;
}

.top-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  flex: 0 0 auto;
  z-index: 6;
}

.brand {
  display: flex;
  align-items: baseline;
  gap: 8px;
}

.brand .logo {
  color: var(--accent);
  font-size: 22px;
  line-height: 1;
  text-shadow: 0 0 18px rgba(255, 209, 102, 0.5);
}

.brand h1 {
  font-size: 22px;
  letter-spacing: 2px;
  font-weight: 800;
}

.subtitle {
  color: var(--muted);
  font-size: 12px;
  letter-spacing: 1px;
  text-transform: uppercase;
}

.hud {
  display: flex;
  gap: 5px;
  flex-wrap: wrap;
  justify-content: flex-end;
  max-width: 100%;
}

.hud-item {
  min-width: 68px;
  background: linear-gradient(180deg, var(--panel-2), var(--panel));
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 4px 8px;
  text-align: center;
  box-shadow: var(--shadow);
}

.hud-item .label {
  display: block;
  color: var(--muted);
  font-size: 10px;
  margin-bottom: 1px;
}

.hud-item span:last-child {
  font-size: 16px;
  font-weight: 700;
  color: var(--accent);
  font-variant-numeric: tabular-nums;
}

.game-area {
  flex: 1 1 auto;
  min-height: 0;
  display: grid;
  grid-template-columns: var(--side-w) minmax(0, 1fr) var(--side-w);
  gap: 10px;
  justify-content: center;
  align-items: center;
  justify-items: center;
  overflow: hidden;
}

.side-panel {
  background: linear-gradient(180deg, rgba(24, 34, 66, 0.95), rgba(12, 18, 36, 0.95));
  border: 1px solid var(--border);
  border-radius: var(--radius);
  padding: 12px;
  box-shadow: var(--shadow);
  min-height: 0;
  max-height: 100%;
  overflow: auto;
  overscroll-behavior: contain;
  scrollbar-width: thin;
}

.side-panel h2 {
  font-size: 13px;
  letter-spacing: 1px;
  color: var(--accent);
  margin: 10px 0 8px;
  text-transform: uppercase;
}

.side-panel h2:first-child { margin-top: 0; }

.controls, .legend {
  list-style: none;
  display: grid;
  gap: 6px;
  color: var(--muted);
  font-size: 12px;
  line-height: 1.4;
}

kbd {
  display: inline-block;
  min-width: 20px;
  padding: 1px 5px;
  margin: 0 1px;
  border-radius: 6px;
  border: 1px solid #445a9a;
  background: #22315f;
  color: #dce6ff;
  font-size: 11px;
  font-family: inherit;
  text-align: center;
  box-shadow: inset 0 -1px 0 rgba(0,0,0,0.35);
}

.legend li {
  display: flex;
  align-items: center;
  gap: 8px;
}

.swatch {
  width: 14px;
  height: 14px;
  border-radius: 4px;
  border: 1px solid rgba(255,255,255,0.15);
  flex: 0 0 auto;
}

.swatch.player { background: var(--player); }
.swatch.player2 { background: #4dabf7; }
.swatch.enemy { background: var(--enemy); }
.swatch.brick { background: var(--brick); }
.swatch.steel { background: linear-gradient(135deg, #cfd6e6, #6d778f); }
.swatch.water { background: var(--water); }
.swatch.grass { background: var(--grass); }
.swatch.base { background: var(--base); }

.stats { display: grid; gap: 6px; }

.stat-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 6px 8px;
  border-radius: 10px;
  background: rgba(255,255,255,0.03);
  border: 1px solid rgba(255,255,255,0.06);
  color: var(--muted);
  font-size: 12px;
}

.stat-row strong {
  color: var(--text);
  font-size: 14px;
}

.tips {
  color: var(--muted);
  font-size: 12px;
  line-height: 1.5;
}

.enemy-icons {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 5px;
  margin-top: 12px;
}

.enemy-icons i {
  width: 100%;
  aspect-ratio: 1;
  border-radius: 4px;
  background: #3a1a1d;
  border: 1px solid #6a2a30;
  position: relative;
}

.enemy-icons i::after {
  content: "";
  position: absolute;
  inset: 22%;
  background: var(--enemy);
  border-radius: 2px;
  clip-path: polygon(20% 0, 80% 0, 100% 35%, 70% 35%, 70% 100%, 30% 100%, 30% 35%, 0 35%);
}

.enemy-icons i.dead {
  opacity: 0.25;
  filter: grayscale(1);
}

/* Desktop board size */
.canvas-wrap {
  position: relative;
  justify-self: center;
  align-self: center;
  --board: min(
    832px,
    100%,
    calc(100dvh - 90px),
    calc(100vh - 90px),
    calc(100vw - 480px)
  );
  width: var(--board);
  height: var(--board);
  max-width: 100%;
  max-height: 100%;
  border-radius: 14px;
  overflow: hidden;
  border: 2px solid #3d56a0;
  box-shadow:
    0 0 0 1px rgba(255,255,255,0.05) inset,
    0 12px 40px rgba(0,0,0,0.55),
    0 0 30px rgba(70, 110, 255, 0.12);
  background: #000;
  flex: 0 0 auto;
}

#game {
  display: block;
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
  background: #000;
}

.overlay {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(4, 8, 20, 0.72);
  backdrop-filter: blur(4px);
  z-index: 5;
  transition: opacity 0.2s ease;
}

.overlay.hidden {
  opacity: 0;
  pointer-events: none;
}

.overlay-card {
  width: min(400px, 90%);
  text-align: center;
  padding: 22px 18px;
  border-radius: 16px;
  border: 1px solid #4a63b0;
  background: linear-gradient(180deg, rgba(36, 52, 98, 0.95), rgba(14, 20, 42, 0.96));
  box-shadow: var(--shadow);
}

.overlay-card h2 {
  font-size: clamp(22px, 4vw, 30px);
  margin-bottom: 8px;
  letter-spacing: 2px;
  color: var(--accent);
  text-shadow: 0 0 20px rgba(255, 209, 102, 0.35);
}

.overlay-card p {
  color: var(--muted);
  line-height: 1.55;
  margin-bottom: 14px;
  font-size: 14px;
  white-space: pre-line;
}

.btn {
  appearance: none;
  border: none;
  cursor: pointer;
  border-radius: 999px;
  padding: 10px 24px;
  font-size: 15px;
  font-weight: 700;
  letter-spacing: 1px;
  transition: transform 0.15s ease, box-shadow 0.15s ease, background 0.15s ease;
}

.btn.primary {
  color: #1a1400;
  background: linear-gradient(180deg, #ffe08a, #f0b429);
  box-shadow: 0 8px 24px rgba(240, 180, 41, 0.35);
}

.btn.primary:hover {
  transform: translateY(-1px);
  box-shadow: 0 12px 28px rgba(240, 180, 41, 0.45);
}

.btn.primary:active { transform: translateY(1px); }

.btn.secondary {
  color: #e8eeff;
  background: linear-gradient(180deg, #3a5299, #2a3c78);
  box-shadow: 0 8px 24px rgba(40, 70, 160, 0.35);
  border: 1px solid #5a78c8;
}

.btn.secondary:hover {
  transform: translateY(-1px);
  box-shadow: 0 12px 28px rgba(40, 70, 160, 0.45);
}

.btn.secondary:active { transform: translateY(1px); }

.hint {
  margin-top: 10px !important;
  margin-bottom: 0 !important;
  font-size: 12px !important;
  opacity: 0.8;
}

.mode-select {
  display: flex;
  flex-direction: column;
  gap: 10px;
  align-items: stretch;
  margin-bottom: 4px;
}

.mode-select.hidden,
#start-btn.hidden,
[hidden] {
  display: none !important;
}

.overlay-card .mode-select .btn { width: 100%; }

.bottom-bar {
  flex: 0 0 auto;
  text-align: center;
  min-height: 0;
}

.credit {
  color: var(--muted);
  font-size: 11px;
  opacity: 0.7;
  line-height: 1.2;
}

/* Dual player UI */
.p1-text { color: #4cd964; }
.p2-text { color: #4dabf7; }
.hud-item.p1 span:last-child { color: #4cd964; }
.hud-item.p2 span:last-child { color: #4dabf7; }
.controls strong { font-weight: 700; }

/* ===== Overlay touch controls ===== */
.mobile-controls {
  display: none;
  position: fixed;
  inset: 0;
  z-index: 30;
  pointer-events: none;
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
}

.mobile-controls .stick-zone,
.mobile-controls .fire-btn {
  pointer-events: auto;
}

.stick-zone {
  position: absolute;
  left: max(6px, env(safe-area-inset-left));
  bottom: max(10px, env(safe-area-inset-bottom));
  width: 250px;
  height: 250px;
  display: flex;
  align-items: center;
  justify-content: center;
}

.stick-base {
  position: relative;
  width: 220px;
  height: 220px;
  border-radius: 50%;
  background: radial-gradient(circle at 40% 35%, rgba(80, 110, 180, 0.36), rgba(12, 18, 36, 0.5));
  border: 3px solid rgba(120, 150, 220, 0.55);
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.38);
}

.stick-knob {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 100px;
  height: 100px;
  margin-left: -50px;
  margin-top: -50px;
  border-radius: 50%;
  background: linear-gradient(180deg, #6d8fd8, #3a5299);
  border: 3px solid rgba(220, 230, 255, 0.65);
  box-shadow: 0 8px 18px rgba(0, 0, 0, 0.48);
  will-change: transform;
}

.stick-knob.active {
  background: linear-gradient(180deg, #8eaff0, #4a6bb8);
}

.fire-btn {
  position: absolute;
  right: max(10px, env(safe-area-inset-right));
  bottom: max(16px, env(safe-area-inset-bottom));
  width: 124px;
  height: 124px;
  border-radius: 50%;
  border: 3px solid #ff9a9e;
  background: linear-gradient(180deg, #ff7a7f, #d9363e);
  color: #fff;
  font-size: 20px;
  font-weight: 800;
  letter-spacing: 1px;
  box-shadow: 0 12px 32px rgba(217, 54, 62, 0.5);
}

.fire-btn:active,
.fire-btn.pressed {
  transform: scale(0.94);
  filter: brightness(1.12);
}

/* ===== Responsive: maximize board ===== */

/* No side panels when space is limited */
@media (max-width: 1180px), (max-height: 900px) {
  .game-area {
    grid-template-columns: 1fr;
    grid-template-rows: minmax(0, 1fr);
  }
  .side-panel { display: none; }
  .canvas-wrap {
    --board: min(832px, calc(100vw - 16px), calc(100dvh - 84px), calc(100vh - 84px));
  }
}

/* Touch / phone: full-bleed board, floating HUD */
@media (max-width: 900px), ((pointer: coarse) and (hover: none)) {
  #app {
    padding: 0;
    gap: 0;
    max-width: none;
  }

  .top-bar {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    padding: 6px 8px;
    background: linear-gradient(180deg, rgba(8, 12, 28, 0.82), rgba(8, 12, 28, 0));
    pointer-events: none;
  }

  .top-bar .hud { pointer-events: auto; }
  .brand { display: none; }
  .credit { display: none; }
  .bottom-bar {
    height: 0;
    min-height: 0;
    margin: 0;
    padding: 0;
    overflow: visible;
  }

  .game-area {
    flex: 1;
    min-height: 0;
    height: 100%;
  }

  .canvas-wrap {
    justify-self: center;
    align-self: center;
    border-radius: 0;
    border-width: 0;
    box-shadow: none;
    /* Fill the smaller viewport dimension almost completely */
    --board: min(100vw, 100dvh, 100vh);
    width: var(--board);
    height: var(--board);
  }

  .mobile-controls { display: block; }

  .hud-item {
    min-width: 46px;
    padding: 2px 6px;
    background: rgba(18, 26, 51, 0.75);
    backdrop-filter: blur(4px);
  }
  .hud-item .label { font-size: 9px; }
  .hud-item span:last-child { font-size: 13px; }
}

/* Portrait phone */
@media (max-width: 720px) and (orientation: portrait) {
  .canvas-wrap {
    --board: min(100vw, 100dvh, 100vh);
  }

  .stick-zone {
    width: 240px;
    height: 240px;
    bottom: max(6px, env(safe-area-inset-bottom));
    left: max(4px, env(safe-area-inset-left));
  }
  .stick-base { width: 212px; height: 212px; }
  .stick-knob { width: 96px; height: 96px; margin-left: -48px; margin-top: -48px; }
  .fire-btn {
    width: 118px;
    height: 118px;
    bottom: max(14px, env(safe-area-inset-bottom));
    font-size: 19px;
  }
}

/* Landscape phone: use full height, board as large as possible */
@media (orientation: landscape) and (max-height: 600px) {
  #app {
    padding: 0;
    gap: 0;
    max-width: none;
  }

  .top-bar {
    position: absolute;
    top: 0;
    left: 0;
    right: 0;
    padding: 4px 8px;
    background: linear-gradient(180deg, rgba(8, 12, 28, 0.75), transparent);
    pointer-events: none;
    z-index: 25;
  }

  .top-bar .hud { pointer-events: auto; }
  .brand { display: none; }
  .subtitle { display: none; }
  .side-panel { display: none; }
  .credit { display: none; }
  .bottom-bar { height: 0; margin: 0; padding: 0; overflow: visible; }

  .game-area {
    grid-template-columns: 1fr;
    height: 100%;
  }

  .canvas-wrap {
    border-radius: 0;
    border: none;
    box-shadow: none;
    /* height-limited: almost full screen height */
    --board: min(100dvh, 100vh, 100vw);
    width: var(--board);
    height: var(--board);
  }

  .mobile-controls { display: block; }

  .stick-zone {
    left: max(4px, env(safe-area-inset-left));
    bottom: 50%;
    transform: translateY(50%);
    width: 210px;
    height: 210px;
  }
  .stick-base { width: 186px; height: 186px; }
  .stick-knob { width: 86px; height: 86px; margin-left: -43px; margin-top: -43px; }

  .fire-btn {
    right: max(6px, env(safe-area-inset-right));
    bottom: 50%;
    transform: translateY(50%);
    width: 110px;
    height: 110px;
    font-size: 18px;
  }

  .fire-btn:active,
  .fire-btn.pressed {
    transform: translateY(50%) scale(0.94);
  }

  .hud-item {
    min-width: 42px;
    padding: 1px 5px;
  }
  .hud-item span:last-child { font-size: 12px; }
}

/* Very short landscape: compact HUD, keep kills visible */
@media (orientation: landscape) and (max-height: 400px) {
  .top-bar {
    display: flex;
    padding: 2px 6px;
  }
  .hud-item {
    min-width: 36px;
    padding: 1px 4px;
  }
  .hud-item .label { font-size: 8px; }
  .hud-item span:last-child { font-size: 11px; }
  .canvas-wrap {
    --board: min(100dvh, 100vh, 100vw);
  }
}
