* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; height: 100%; }
body {
  background: #0e1116;
  color: #d8e0e8;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 100vh;
  overflow: hidden;
}

#wrap {
  position: relative;
  width: 1136px;
  height: 640px;
  display: grid;
  grid-template-columns: 220px 900px;
  grid-template-rows: 60px 580px;
  gap: 0 16px;
}

canvas {
  grid-column: 2;
  grid-row: 1 / span 2;
  background: #2d4a2b;
  border-radius: 6px;
  box-shadow: 0 6px 30px rgba(0,0,0,0.4);
  cursor: crosshair;
  align-self: end;
}

#hud {
  grid-column: 2;
  grid-row: 1;
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 0 12px;
}
.stat {
  display: flex;
  flex-direction: column;
  font-size: 11px;
  line-height: 1.1;
  background: rgba(30,40,50,0.6);
  border: 1px solid rgba(120,160,200,0.15);
  padding: 6px 10px;
  border-radius: 6px;
  min-width: 62px;
}
.stat .lbl { color: #8aa; font-size: 10px; }
.stat span:last-child, .stat b { font-size: 16px; font-weight: 600; color: #fff; }

#hud button {
  margin-left: auto;
  padding: 8px 16px;
  background: linear-gradient(180deg, #4a8a4a, #2e5a2e);
  color: #fff;
  border: 1px solid rgba(255,255,255,0.15);
  border-radius: 5px;
  font-weight: 600;
  cursor: pointer;
  font-size: 13px;
}
#hud button:hover { filter: brightness(1.15); }
#hud button:disabled { opacity: 0.4; cursor: default; }
#btn-speed {
  margin-left: 0;
  background: linear-gradient(180deg, #4a6a8a, #2e4a6a);
  min-width: 50px;
}
#btn-mute {
  margin-left: 0;
  background: linear-gradient(180deg, #555, #333);
  min-width: 44px;
  font-size: 16px;
  padding: 6px 8px;
}

#shop {
  grid-column: 1;
  grid-row: 1 / span 2;
  background: #161c24;
  border-radius: 6px;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  border: 1px solid rgba(120,160,200,0.1);
  overflow-y: auto;
}
.shop-title {
  font-weight: 700;
  font-size: 13px;
  letter-spacing: 0.1em;
  color: #8aa;
  text-transform: uppercase;
  padding-bottom: 4px;
  border-bottom: 1px solid rgba(120,160,200,0.15);
}
.tower-btn {
  background: rgba(40,50,65,0.5);
  border: 1px solid rgba(120,160,200,0.15);
  border-radius: 5px;
  padding: 6px 9px;
  cursor: pointer;
  user-select: none;
  transition: background 0.1s;
  position: relative;
}
.tower-btn::after {
  /* Cooldown squish overlay — translucent white sheet that compresses down
     to nothing over the cooldown when the .cooling class is applied. */
  content: '';
  position: absolute;
  left: 0; right: 0; top: 0; bottom: 0;
  background: rgba(255, 255, 255, 0.32);
  transform: scaleY(0);
  transform-origin: bottom;
  pointer-events: none;
}
.tower-btn.cooling::after {
  animation: cooldown-squish 3s linear forwards;
}
.tower-btn.cooling { filter: saturate(0.7); }
@keyframes cooldown-squish {
  from { transform: scaleY(1); }
  to   { transform: scaleY(0); }
}
.tower-btn:hover { background: rgba(60,80,110,0.7); }
.tower-btn.selected {
  background: rgba(80,140,80,0.45);
  border-color: rgba(140,220,140,0.6);
}
.tower-btn.disabled { opacity: 0.45; cursor: default; }
.tower-btn.wall-btn {
  background: rgba(80,40,30,0.5);
  border-color: rgba(180,90,70,0.35);
}
.tower-btn.wall-btn:hover { background: rgba(110,55,40,0.7); }
.tower-btn.wall-btn.selected {
  background: rgba(168,80,64,0.55);
  border-color: rgba(220,140,110,0.7);
}
.tower-btn.upgrade-btn {
  background: rgba(35,55,80,0.5);
  border-color: rgba(120,170,210,0.35);
}
.tower-btn.upgrade-btn:hover { background: rgba(55,80,110,0.7); }
.tower-btn.upgrade-btn.selected {
  background: rgba(80,140,200,0.55);
  border-color: rgba(160,210,255,0.7);
}
.tower-btn.mine-btn {
  background: rgba(60,30,30,0.5);
  border-color: rgba(170,90,80,0.35);
}
.tower-btn.mine-btn:hover { background: rgba(90,45,40,0.7); }
.tower-btn.mine-btn.selected {
  background: rgba(180,80,70,0.55);
  border-color: rgba(255,140,120,0.7);
}
.tower-btn.press-btn {
  background: rgba(20,20,20,0.7);
  border-color: rgba(80,80,80,0.4);
}
.tower-btn.press-btn:hover { background: rgba(40,40,40,0.85); }
.tower-btn.press-btn.selected {
  background: rgba(60,60,60,0.9);
  border-color: rgba(180,180,180,0.7);
}
.tower-btn.laser-btn {
  background: rgba(70,20,20,0.5);
  border-color: rgba(220,80,80,0.4);
}
.tower-btn.laser-btn:hover { background: rgba(100,30,30,0.7); }
.tower-btn.laser-btn.selected {
  background: rgba(170,40,40,0.6);
  border-color: rgba(255,140,140,0.8);
}

.mobile-toggle {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 13px;
  color: #aac;
  margin-bottom: 14px;
  cursor: pointer;
  user-select: none;
}
.mobile-toggle input { transform: scale(1.2); }

/* ─── MOBILE MODE ─── stacked layout, full-width canvas, touch-sized UI */
body.mobile {
  display: block;
  align-items: initial;
  justify-content: initial;
  overflow: auto;
}
body.mobile #wrap {
  width: 100%;
  height: auto;
  display: block;
  padding: 6px;
  gap: 0;
}
body.mobile canvas {
  width: 100%;
  height: auto;
  display: block;
  margin: 0 0 8px 0;
  touch-action: none;
}
body.mobile #hud {
  flex-wrap: wrap;
  gap: 8px;
  padding: 8px;
  background: #161c24;
  border-radius: 6px;
  margin-bottom: 8px;
}
body.mobile #hud button { padding: 12px 16px; font-size: 15px; min-height: 44px; }
body.mobile #shop {
  display: flex;
  flex-direction: row;
  flex-wrap: wrap;
  gap: 6px;
  padding: 8px;
  height: auto;
  overflow: visible;
}
body.mobile #shop .shop-title { width: 100%; }
body.mobile .tower-btn {
  flex: 1 1 calc(50% - 6px);
  min-height: 60px;
  padding: 10px 12px;
}
body.mobile .tname { font-size: 14px; }
body.mobile .tdesc { font-size: 11px; }
body.mobile #inspector {
  position: static;
  width: 100%;
  margin-top: 8px;
}
body.mobile #inspector button { padding: 12px; font-size: 13px; min-height: 44px; }
body.mobile #back { position: static; padding: 12px; text-align: center; }
.tname { font-weight: 600; font-size: 13px; color: #fff; }
.tcost { font-size: 11px; color: #d4c460; margin-top: 1px; }
.tdesc { font-size: 11px; color: #8a9aaa; margin-top: 3px; line-height: 1.35; white-space: normal; }

#shop .hint {
  font-size: 10px;
  color: #6a7a8a;
  margin-top: auto;
  line-height: 1.4;
}

#inspector {
  position: absolute;
  right: 12px;
  top: 80px;
  width: 200px;
  background: rgba(22,28,36,0.92);
  border: 1px solid rgba(120,160,200,0.2);
  border-radius: 6px;
  padding: 12px;
  display: none;
  backdrop-filter: blur(4px);
  z-index: 5;
}
#inspector.show { display: block; }
.ins-title {
  font-weight: 700;
  font-size: 14px;
  margin-bottom: 8px;
  color: #fff;
}
.ins-stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px 8px;
  font-size: 11px;
  margin-bottom: 10px;
}
.ins-stats > div { display: flex; justify-content: space-between; }
.ins-stats span { color: #8aa; }
.ins-stats b { color: #fff; }
#inspector button {
  width: 100%;
  padding: 6px;
  margin-bottom: 4px;
  background: rgba(60,80,110,0.6);
  border: 1px solid rgba(120,160,200,0.2);
  color: #d8e0e8;
  border-radius: 4px;
  cursor: pointer;
  font-size: 11px;
  text-align: left;
  display: flex;
  justify-content: space-between;
}
#inspector button:hover:not(:disabled) { background: rgba(80,110,150,0.8); }
#inspector button:disabled { opacity: 0.4; cursor: default; }
#inspector button.sell {
  background: rgba(120,60,60,0.5);
  margin-top: 6px;
}

#banner {
  position: absolute;
  top: 80px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(0,0,0,0.7);
  color: #fff;
  padding: 10px 20px;
  border-radius: 4px;
  font-weight: 600;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.3s;
  font-size: 14px;
}
#banner.show { opacity: 1; }

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(8,12,16,0.92);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 100;
}
.overlay.hidden { display: none; }
.overlay h1 {
  font-size: 48px;
  margin: 0 0 10px;
  color: #fff;
  letter-spacing: 0.05em;
}
.overlay p {
  font-size: 16px;
  color: #8aa;
  margin: 0 0 24px;
}
.overlay button {
  padding: 12px 32px;
  font-size: 16px;
  background: linear-gradient(180deg, #4a8a4a, #2e5a2e);
  color: #fff;
  border: 1px solid rgba(255,255,255,0.2);
  border-radius: 6px;
  cursor: pointer;
  font-weight: 600;
}
.overlay button:hover { filter: brightness(1.15); }

.mode-row {
  display: flex;
  gap: 8px;
  margin-bottom: 12px;
  flex-wrap: wrap;
  justify-content: center;
  max-width: 700px;
}
.overlay .mode-btn.attacker {
  background: rgba(120, 30, 30, 0.7);
  border-color: rgba(220, 80, 80, 0.5);
}
.overlay .mode-btn.attacker.selected {
  background: rgba(180, 50, 50, 0.7);
  border-color: rgba(255, 140, 140, 0.7);
}
.overlay .mode-btn {
  padding: 10px 16px;
  font-size: 13px;
  background: rgba(40, 50, 65, 0.7);
  border: 1px solid rgba(120, 160, 200, 0.25);
  color: #d8e0e8;
  font-weight: 500;
}
.overlay .mode-btn:hover { background: rgba(60, 80, 110, 0.8); }
.overlay .mode-btn.selected {
  background: rgba(80, 120, 180, 0.6);
  border-color: rgba(160, 210, 255, 0.7);
  color: #fff;
}

.diff-row { display: flex; gap: 12px; margin-bottom: 14px; }
.overlay .diff {
  padding: 14px 26px;
  font-size: 17px;
  letter-spacing: 0.04em;
}
.overlay .diff.easy {
  background: linear-gradient(180deg, #4a8a4a, #2e5a2e);
}
.overlay .diff.medium {
  background: linear-gradient(180deg, #b88a3a, #6a4a18);
}
.overlay .diff.nightmare {
  background: linear-gradient(180deg, #b03a3a, #5a1010);
  border-color: rgba(255,80,80,0.5);
  text-shadow: 0 0 8px rgba(255,80,80,0.6);
}
.diff-hint {
  font-size: 12px !important;
  color: #6a7a8a !important;
  max-width: 540px;
  text-align: center;
  margin-top: 6px !important;
}

#back {
  position: fixed;
  bottom: 8px;
  left: 12px;
  font-size: 12px;
}

/* ── Attacker mode UI ─────────────────────────────────────────── */
#attacker-base {
  position: fixed;
  inset: 0;
  background: linear-gradient(180deg, #1d1018 0%, #0a0608 100%);
  z-index: 50;
  padding: 28px 36px;
  display: none;
  flex-direction: column;
  gap: 14px;
  transition: transform 0.5s cubic-bezier(0.5, 0, 0.4, 1);
  color: #e8d8d0;
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
}
body.mode-attacker #attacker-base { display: flex; }
body.mode-attacker.view-battle #attacker-base { transform: translateX(-105%); }
body.mode-attacker #shop { display: none; }
body.mode-attacker #inspector { display: none; }
body.mode-attacker #btn-wave { display: none; }

.ab-header {
  font-size: 22px;
  font-weight: 700;
  letter-spacing: 0.08em;
  color: #ff8080;
  text-shadow: 0 0 18px rgba(255, 80, 80, 0.5);
}
.ab-stats {
  display: flex;
  gap: 14px;
  flex-wrap: wrap;
}
.ab-stat {
  flex: 0 0 auto;
  background: rgba(40, 20, 25, 0.7);
  border: 1px solid rgba(180, 90, 80, 0.3);
  padding: 8px 14px;
  border-radius: 6px;
  display: flex;
  flex-direction: column;
  gap: 3px;
  min-width: 100px;
}
.ab-stat .ab-lbl { color: #c0a0a0; font-size: 11px; }
.ab-stat b { color: #fff; font-size: 18px; font-weight: 600; }

.ab-cols {
  flex: 1;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 24px;
  min-height: 0;
}
.ab-units, .ab-queue {
  background: rgba(20, 10, 14, 0.6);
  border: 1px solid rgba(120, 60, 60, 0.3);
  border-radius: 8px;
  padding: 18px;
  display: flex;
  flex-direction: column;
  min-height: 0;
}
.ab-units h4, .ab-queue h4 {
  margin: 0 0 12px;
  color: #ffae80;
  font-size: 13px;
  letter-spacing: 0.1em;
  text-transform: uppercase;
}

.unit-btn {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: rgba(60, 30, 30, 0.6);
  border: 1px solid rgba(180, 100, 100, 0.4);
  color: #fff;
  padding: 12px 16px;
  margin-bottom: 8px;
  cursor: pointer;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 500;
  transition: background 0.1s, transform 0.05s;
}
.unit-btn:hover { background: rgba(90, 45, 40, 0.85); }
.unit-btn:active { transform: scale(0.98); }
.unit-btn:disabled { opacity: 0.4; cursor: default; transform: none; }
.unit-btn .u-cost { color: #ffd24a; font-weight: 600; }

.ab-hint {
  margin-top: auto;
  padding-top: 12px;
  font-size: 11px;
  color: #a08080;
  line-height: 1.4;
}

#ab-map {
  width: 100%;
  max-width: 420px;
  height: auto;
  border-radius: 6px;
  background: #2d4a2b;
  border: 1px solid rgba(180,100,100,0.3);
  display: block;
  cursor: pointer;
}
.ab-map-hint {
  font-size: 11px;
  color: #a0908a;
  margin-top: 6px;
  text-align: center;
}
#ab-clear {
  margin-top: 10px;
  background: rgba(80, 40, 40, 0.6);
  border: 1px solid rgba(150, 80, 80, 0.4);
  color: #e8d8d0;
  padding: 8px 14px;
  border-radius: 5px;
  cursor: pointer;
  font-size: 12px;
}
#ab-clear:hover { background: rgba(110, 50, 50, 0.8); }

#ab-send {
  padding: 16px;
  background: linear-gradient(180deg, #d04040 0%, #6a1010 100%);
  color: #fff;
  border: 1px solid rgba(255, 180, 180, 0.4);
  border-radius: 8px;
  font-size: 17px;
  font-weight: 700;
  cursor: pointer;
  letter-spacing: 0.1em;
  text-shadow: 0 1px 2px rgba(0, 0, 0, 0.4);
}
#ab-send:hover:not(:disabled) {
  background: linear-gradient(180deg, #e85050 0%, #802020 100%);
}
#ab-send:disabled { opacity: 0.45; cursor: default; }

#return-to-base {
  position: fixed;
  top: 14px;
  left: 240px;
  display: none;
  z-index: 40;
  background: linear-gradient(180deg, #4a6a8a, #2a4a6a);
  color: #fff;
  border: 1px solid rgba(255, 255, 255, 0.3);
  border-radius: 6px;
  padding: 10px 18px;
  cursor: pointer;
  font-weight: 600;
  font-size: 14px;
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.4);
}
body.mode-attacker.view-battle #return-to-base { display: block; }
#return-to-base:hover { background: linear-gradient(180deg, #5a7a9a, #3a5a7a); }
#back a { color: #6a8aaa; text-decoration: none; }
#back a:hover { color: #aac; }
