* {
  box-sizing: border-box;
}

:root {
  color-scheme: dark;
  font-family: system-ui, sans-serif;
  background: #101827;
  color: #f8fafc;
}

body {
  margin: 0;
  min-height: 100vh;
  min-height: 100dvh;
  padding: max(12px, env(safe-area-inset-top)) 12px
           max(12px, env(safe-area-inset-bottom));
  overflow-x: hidden;
}

.app {
  width: 100%;
  max-width: 520px;
  margin: auto;
  overflow-x: hidden;
}

.header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  width: 100%;
  min-width: 0;
  overflow: hidden;
  /* One row only: the title shrinks and the icon buttons shrink so the
     header never wraps, even on the narrowest phones. */
  flex-wrap: nowrap;
}

.header > div {
  min-width: 0;
  flex: 1 1 auto;
  overflow: hidden;
}

h1 {
  margin: 0;
  font-size: clamp(14px, 5vw, 25px);
  flex: 0 1 auto;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.header p,
.hint {
  color: #94a3b8;
  font-size: clamp(9px, 2.4vw, 12px);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 100%;
}

.header p {
  margin: 0;
}

.stats {
  display: flex;
  gap: 4px;
  font-size: clamp(11px, 3vw, 15px);
  flex: 1 0 auto;
  min-width: 0;
  align-items: center;
  white-space: nowrap;
  overflow: hidden;
}

.stats span {
  white-space: nowrap;
  flex: 0 0 auto;
  overflow: hidden;
  text-overflow: ellipsis;
}

.icon-btn {
  width: clamp(26px, 6.5vw, 34px);
  height: clamp(26px, 6.5vw, 34px);
  flex: 0 0 auto;
  font-size: clamp(14px, 3.8vw, 18px);
}

.icon-btn[hidden] {
  display: none;
}

@media (max-width: 480px) {
  .header {
    flex-wrap: wrap;
    gap: 4px;
  }

  .header > div:first-child {
    flex: 1 0 100%;
  }

  .stats {
    flex: 1 0 100%;
    justify-content: space-between;
    gap: 2px;
    font-size: 11px;
    overflow: visible;
  }

  .stats span {
    overflow: visible;
  }

  .stats .icon-btn {
    width: 26px;
    height: 26px;
    min-height: 26px;
    padding: 0;
    font-size: 12px;
  }
}

.status {
  margin: 14px 0 8px;
  padding: 11px;
  text-align: center;
  border-radius: 10px;
  background: #1e293b;
  font-size: 14px;
}

.wave-panel {
  margin: 6px 0 8px;
  padding: 8px 10px;
  border-radius: 10px;
  background: #0f172a;
  border: 1px solid #334155;
  font-size: 12px;
  display: flex;
  flex-direction: column;
  gap: 4px;
  text-align: left;
}

.wave-info {
  font-weight: 600;
}

.wave-preview {
  display: flex;
  flex-direction: column;
  gap: 1px;
  min-width: 0;
  margin: 0;
  padding-left: 8px;
  border-left: 1px dashed #334155;
  font-size: 11px;
  line-height: 1.4;
}

/* Ensure preview lines wrap instead of overflowing/cutting off on narrow screens */
.wave-preview > span {
  display: block;
  overflow-wrap: break-word;
  word-wrap: break-word;
  max-width: 100%;
}

.preview-title {
  color: #e2e8f0;
  font-weight: 700;
}

.preview-line {
  color: #94a3b8;
}

/* Keep current-wave details and the next-wave preview side by side. */
.wave-info-row {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
  align-items: stretch;
}

.wave-info,
.wave-preview {
  display: flex;
  flex-direction: column;
  gap: 1px;
  min-width: 0;
  font-size: 11px;
  line-height: 1.25;
}

.wave-info {
  min-width: 0;
  white-space: normal;
  overflow-wrap: break-word;
  word-wrap: break-word;
  font-weight: 600;
}

.wave-info > span {
  display: block;
}

.wave-info .preview-title,
.wave-preview .preview-title {
  font-size: 12px;
}

/* ---- Upgrade panel as a context-style popup over the canvas ---- */
#panelOverlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(2, 6, 23, 0.55);
  -webkit-backdrop-filter: blur(2px);
  backdrop-filter: blur(2px);
}

#panelOverlay[hidden] {
  display: none;
}

#roundOverlay {
  position: fixed;
  inset: 0;
  z-index: 60;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
  background: rgba(2, 6, 23, 0.72);
  -webkit-backdrop-filter: blur(3px);
  backdrop-filter: blur(3px);
}

#roundOverlay[hidden] {
  display: none;
}

.round-dialog {
  width: min(360px, 100%);
  padding: 20px;
  text-align: center;
  border: 1px solid #475569;
  border-radius: 16px;
  background: linear-gradient(150deg, #1e293b, #111827 75%);
  box-shadow: 0 20px 60px rgba(0, 0, 0, 0.65);
}

.campaign-complete .round-dialog {
  border-color: #fbbf24;
  background:
    radial-gradient(ellipse at 50% 0%, rgba(245, 158, 11, 0.22), transparent 68%),
    linear-gradient(150deg, #292315, #111827 78%);
  box-shadow: 0 0 0 1px rgba(251, 191, 36, 0.18), 0 22px 70px rgba(245, 158, 11, 0.22);
}

.round-dialog h2 {
  margin: 0 0 10px;
  color: #fde68a;
  font-size: 22px;
}

.campaign-complete .round-dialog h2 {
  color: #fcd34d;
  text-shadow: 0 0 18px rgba(251, 191, 36, 0.45);
}

.round-dialog p {
  margin: 0 0 16px;
  color: #cbd5e1;
  font-size: 14px;
  line-height: 1.5;
}

.upgrade-panel[hidden] {
  display: none;
}

#panelBackdrop {
  position: absolute;
  inset: 0;
}

.upgrade-panel {
  position: relative;
  z-index: 1;
  width: min(340px, 92vw);
  max-height: min(78vh, 560px);
  overflow-y: auto;
  margin: 16px;
  padding: 12px;
  border: 1px solid #334155;
  border-radius: 14px;
  background: #111827;
  box-shadow: 0 18px 50px rgba(0, 0, 0, 0.55);
}

.build-panel {
  display: block;
}

.build-panel .tower-picker {
  margin: 10px 0;
}

.build-panel .tower-picker button {
  min-height: 52px;
}

.panel-close {
  min-height: auto;
  min-width: auto;
  width: 30px;
  height: 30px;
  padding: 0;
  font-size: 14px;
  border-radius: 8px;
  background: #1e293b;
  color: #e2e8f0;
  border: 1px solid #334155;
  cursor: pointer;
  touch-action: manipulation;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}

.panel-close:active {
  transform: scale(0.93);
}

canvas {
  display: block;
  width: 100%;
  height: auto;
  aspect-ratio: 5 / 6;
  background: #173c35;
  border: 1px solid #365b50;
  border-radius: 14px;
  touch-action: none;
}

.controls {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin-top: 12px;
}

.tower-picker {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 6px;
  margin-bottom: 10px;
}

.tower-picker button {
  min-height: 40px;
  padding: 6px 4px;
  font-size: 11px;
  border-radius: 10px;
  background: #1e293b;
  color: #e2e8f0;
  border: 1px solid #334155;
  cursor: pointer;
  touch-action: manipulation;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
}

.tower-picker button .tname {
  font-weight: 700;
}

.tower-picker button .tcost {
  font-size: 10px;
  color: #fbbf24;
}

.tower-picker button .tfeature {
  font-size: 9px;
  color: #c4b5fd;
}

.tower-picker button.active {
  background: #2563eb;
  border-color: #60a5fa;
  color: white;
}

.tower-picker button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

button {
  min-height: 48px;
  padding: 10px 6px;
  border: 0;
  border-radius: 12px;
  background: #2563eb;
  color: white;
  font: inherit;
  font-size: 13px;
  font-weight: 700;
  cursor: pointer;
  touch-action: manipulation;
  width: 100%;
}

#startBtn {
  background: #15803d;
}

button.secondary {
  background: #334155;
}

button:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

@media (max-height: 900px) {
  body {
    padding-top: max(8px, env(safe-area-inset-top));
    padding-bottom: max(8px, env(safe-area-inset-bottom));
  }

  h1 {
    font-size: 22px;
  }

  .header p {
    margin-top: 2px;
    font-size: 9px;
    letter-spacing: -0.02em;
    white-space: nowrap;
  }

  .stats {
    flex-shrink: 0;
    gap: 8px;
    font-size: 12px;
  }

  .status {
    margin: 8px 0 6px;
    padding: 5px 9px;
    font-size: 12px;
  }

  .wave-panel {
    margin: 4px 0 4px;
    padding: 4px 8px;
  }

  .wave-info-row {
    gap: 6px;
  }

  .wave-info,
  .wave-preview {
    padding-left: 6px;
    font-size: 10px;
    line-height: 1.1;
  }

  .wave-info {
    padding-left: 0;
  }

  .wave-info .preview-title,
  .wave-preview .preview-title {
    font-size: 11px;
  }

  canvas {
    width: auto;
    height: min(440px, max(180px, calc(102dvh - 295px)));
    max-width: 100%;
    margin-inline: auto;
  }

  .tower-picker {
    gap: 4px;
    margin-bottom: 6px;
  }

  .tower-picker button {
    min-height: 36px;
    padding: 3px 4px;
  }

  .controls {
    gap: 6px;
    margin-top: 8px;
  }

  .controls button {
    min-height: 40px;
    padding: 6px 4px;
    font-size: 12px;
  }

  .hint {
    margin: 6px 0 0;
    font-size: 10px;
    line-height: 1.3;
  }
}

@media (max-height: 640px) {
  .wave-info,
  .wave-preview {
    font-size: 9px;
    line-height: 1.05;
  }

  .wave-info .preview-title,
  .wave-preview .preview-title {
    font-size: 10px;
  }
}

.hint {
  text-align: center;
  line-height: 1.6;
}

.icon-btn {
  background: #1e293b;
  color: #e2e8f0;
  border: 1px solid #334155;
  border-radius: 8px;
  width: 36px;
  height: 36px;
  font-size: 16px;
  cursor: pointer;
  touch-action: manipulation;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}

.icon-btn:active {
  transform: scale(0.93);
}

.settings-panel {
  max-height: min(80vh, 600px);
}

.settings-body {
  display: flex;
  flex-direction: column;
  gap: 10px;
  padding: 4px 0;
}

.setting-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 8px 0;
  border-top: 1px solid #1f2937;
  font-size: 13px;
}

.setting-row input[type="checkbox"] {
  width: 20px;
  height: 20px;
  cursor: pointer;
}

.setting-stats h3,
.setting-achievements h3 {
  margin: 8px 0 4px;
  font-size: 12px;
  color: #94a3b8;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.stat-line,
.ach-line {
  font-size: 12px;
  padding: 3px 0;
  color: #cbd5e1;
}

.ach-line.locked {
  color: #64748b;
}

.ach-line.unlocked {
  color: #fde68a;
}

.reset-confirm {
  margin: 8px 0;
  font-size: 12px;
  color: #fca5a5;
  text-align: center;
}

.danger {
  background: #7f1d1d;
  border-color: #991b1b;
}

.danger:hover {
  background: #991b1b;
}

/* ---- v2.3.1: PWA install banner ---- */
#installBanner[hidden] {
  display: none;
}

#installBanner {
  position: fixed;
  bottom: max(12px, env(safe-area-inset-bottom));
  left: 50%;
  right: auto;
  z-index: 55;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 12px 14px;
  border-radius: 14px;
  background: linear-gradient(150deg, #1e293b, #111827 80%);
  border: 1px solid #334155;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.55);
  max-width: 520px;
  width: calc(100% - 24px);
  transform: translateX(-50%);
}

.install-banner-content {
  display: flex;
  align-items: center;
  gap: 12px;
  min-width: 0;
}

.install-icon {
  width: 40px;
  height: 40px;
  flex: 0 0 40px;
  border-radius: 12px;
  background: rgba(251, 191, 36, 0.12);
  border: 1px solid rgba(251, 191, 36, 0.35);
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 22px;
}

.install-text {
  min-width: 0;
}

.install-text h3 {
  margin: 0 0 2px;
  font-size: 14px;
  color: #fde68a;
}

.install-text p {
  margin: 0;
  font-size: 12px;
  color: #cbd5e1;
  line-height: 1.4;
}

.install-actions {
  display: flex;
  gap: 8px;
  flex: 0 0 auto;
}

#installBanner .primary {
  background: #2563eb;
  border-color: #3b82f6;
}

#installBanner .primary:hover {
  background: #3b82f6;
}

@media (max-width: 420px) {
  #installBanner {
    flex-direction: column;
    align-items: stretch;
    gap: 10px;
  }
  .install-banner-content { width: 100%; }
  .install-actions { width: 100%; }
  #installBanner .primary,
  #installBanner .secondary { width: 100%; }
}

/* ---- v2.1.0: Campaign selection menu ---- */
.campaign-panel {
  max-height: min(80vh, 600px);
  overflow-y: auto;
}

.campaign-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 10px;
}

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

.campaign-card {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  padding: 10px 6px;
  background: #1e293b;
  border: 1px solid #334155;
  border-radius: 8px;
  color: #e2e8f0;
  cursor: pointer;
  font-size: 11px;
  transition: border-color 0.15s, background 0.15s;
}

.campaign-card:not(:disabled):hover {
  border-color: #60a5fa;
  background: #1e3a5f;
}

.campaign-card:disabled {
  opacity: 0.5;
  cursor: not-allowed;
}

.campaign-card.current {
  border-color: #fde68a;
  background: #3b2f1e;
}

.campaign-card.completed {
  border-color: #4ade80;
}

.campaign-num {
  font-weight: 700;
  font-size: 12px;
}

.campaign-name {
  font-size: 9px;
  line-height: 1.2;
}

.campaign-status {
  font-size: 12px;
}

.reduced-motion *,
.reduced-motion *::before,
.reduced-motion *::after {
  scroll-behavior: auto !important;
  animation-duration: 0.01ms !important;
  animation-iteration-count: 1 !important;
  transition-duration: 0.01ms !important;
}

.reduced-motion button:active,
.reduced-motion .tmode-btn:active,
.reduced-motion .icon-btn:active {
  transform: none;
}

.upgrade-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  margin-bottom: 6px;
}

.upgrade-header h2 {
  margin: 0;
  font-size: 14px;
}

.level-pill {
  padding: 2px 8px;
  border-radius: 999px;
  background: #1e3a8a;
  color: #e0e7ff;
  font-size: 11px;
  font-weight: 700;
}

.upgrade-stats {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px 10px;
  margin-bottom: 8px;
  font-size: 12px;
}

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

.stat .label {
  color: #94a3b8;
}

.stat .value {
  font-weight: 600;
}

.upgrade-msg {
  margin-top: 6px;
  font-size: 11px;
  color: #a5b4fc;
  min-height: 14px;
}

.targeting-row {
  display: flex;
  flex-direction: column;
  gap: 4px;
  margin: 6px 0 2px;
}
.tlabel {
  font-size: 10px;
  color: #94a3b8;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}
.targeting-picker {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 4px;
}
.tmode-btn {
  min-height: 34px;
  padding: 4px 2px;
  font-size: 10px;
  border-radius: 8px;
  background: #1e293b;
  color: #e2e8f0;
  border: 1px solid #334155;
  cursor: pointer;
  touch-action: manipulation;
  width: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 1px;
  line-height: 1.1;
}
.tmode-btn:active {
  transform: scale(0.96);
}
.tmode-btn.active {
  background: #2563eb;
  border-color: #60a5fa;
  color: white;
}
@media (max-width: 360px) {
  .targeting-picker {
    grid-template-columns: repeat(2, 1fr);
  }
  .upgrade-stats {
    grid-template-columns: 1fr;
  }
}

/* ---- v2.2.0 Settings panel, game over overlay, icon button ---- */
.icon-btn {
  background: #1e293b;
  color: #e2e8f0;
  border: 1px solid #334155;
  border-radius: 8px;
  width: 36px;
  height: 36px;
  font-size: 16px;
  cursor: pointer;
  touch-action: manipulation;
  display: flex;
  align-items: center;
  justify-content: center;
  line-height: 1;
}

.icon-btn:active {
  transform: scale(0.93);
}

.settings-panel {
  max-height: min(80vh, 600px);
  overflow-y: auto;
}

.settings-body {
  display: flex;
  flex-direction: column;
  gap: 8px;
  padding: 4px 0;
}

.setting-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 8px 0;
  border-top: 1px solid #1f2937;
  font-size: 13px;
}

.setting-row input[type="checkbox"] {
  width: 20px;
  height: 20px;
  cursor: pointer;
}

.setting-stats h3,
.setting-achievements h3 {
  margin: 10px 0 4px;
  font-size: 12px;
  color: #94a3b8;
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.stat-line,
.ach-line {
  font-size: 12px;
  padding: 3px 0;
  color: #cbd5e1;
}

.ach-line.locked {
  color: #64748b;
}

.ach-line.unlocked {
  color: #fde68a;
}

.reset-confirm {
  margin: 8px 0;
  font-size: 12px;
  color: #fca5a5;
  text-align: center;
}

.danger {
  background: #7f1d1d;
  border-color: #991b1b;
}

.danger:hover {
  background: #991b1b;
}