:root {
  --bg-dark: #0b0f17;
  --panel-bg: rgba(18, 24, 38, 0.88);
  --panel-border: rgba(255, 255, 255, 0.12);
  --accent-gold: #ffd700;
  --accent-red: #ff3d71;
  --accent-blue: #00d2ff;
  --accent-green: #00e096;
  --accent-purple: #9d4edd;
  --text-primary: #f8fafc;
  --text-secondary: #94a3b8;
  --font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

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

body {
  background-color: var(--bg-dark);
  color: var(--text-primary);
  font-family: var(--font-family);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  overflow-x: hidden;
  background-image: radial-gradient(circle at 50% 10%, #1a2333 0%, #0b0f17 100%);
}

.game-container {
  width: 100%;
  max-width: 1240px;
  padding: 10px;
  display: flex;
  flex-direction: column;
  gap: 10px;
}

/* Header & HUD */
.game-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  background: var(--panel-bg);
  backdrop-filter: blur(14px);
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  padding: 8px 16px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.4);
}

.title-section {
  display: flex;
  align-items: center;
  gap: 12px;
}

.title-section h1 {
  font-size: 1.3rem;
  font-weight: 800;
  background: linear-gradient(135deg, #ffd700, #ff9100);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  letter-spacing: 0.02em;
}

.map-selector-wrap select {
  background: #1e293b;
  color: #f1f5f9;
  border: 1px solid #334155;
  border-radius: 6px;
  padding: 6px 10px;
  font-size: 0.85rem;
  outline: none;
  cursor: pointer;
}

/* HUD Stats */
.hud-stats {
  display: flex;
  align-items: center;
  gap: 12px;
}

.stat-item {
  display: flex;
  align-items: center;
  gap: 5px;
  background: rgba(15, 23, 42, 0.65);
  padding: 5px 11px;
  border-radius: 20px;
  border: 1px solid rgba(255, 255, 255, 0.08);
  font-size: 0.92rem;
  font-weight: 700;
}

.stat-gold { color: #ffd700; text-shadow: 0 0 8px rgba(255, 215, 0, 0.35); }
.stat-lives { color: #ff3d71; text-shadow: 0 0 8px rgba(255, 61, 113, 0.35); }
.stat-wave { color: #00d2ff; }
.stat-score { color: #00e096; }

/* Control buttons group */
.controls-group {
  display: flex;
  align-items: center;
  gap: 6px;
}

.btn {
  background: #1e293b;
  color: var(--text-primary);
  border: 1px solid #334155;
  border-radius: 8px;
  padding: 6px 11px;
  font-size: 0.82rem;
  font-weight: 600;
  cursor: pointer;
  transition: all 0.15s ease-in-out;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 4px;
}

.btn:hover:not(:disabled) {
  background: #334155;
  border-color: #64748b;
  transform: translateY(-1px);
}

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

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

.btn-primary {
  background: linear-gradient(135deg, #e65100, #ff9800);
  border: none;
  color: #fff;
  font-weight: 700;
  box-shadow: 0 4px 12px rgba(255, 152, 0, 0.35);
}
.btn-primary:hover:not(:disabled) {
  background: linear-gradient(135deg, #f57c00, #ffa726);
  box-shadow: 0 6px 16px rgba(255, 152, 0, 0.5);
}

.btn-speed.active {
  background: #0284c7;
  border-color: #38bdf8;
  color: #fff;
}

.btn.active {
  background: #059669;
  border-color: #34d399;
}

.auto-wave-toggle {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 0.78rem;
  color: var(--text-secondary);
  cursor: pointer;
}

/* Canvas Viewport */
.canvas-wrapper {
  position: relative;
  width: 100%;
  aspect-ratio: 1200 / 720;
  background: #000;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: 0 12px 36px rgba(0, 0, 0, 0.55);
  border: 1px solid var(--panel-border);
}

#gameCanvas {
  width: 100%;
  height: 100%;
  display: block;
  cursor: crosshair;
}

#gameCanvas.damage-flash {
  animation: flashRed 0.25s ease-out;
}

@keyframes flashRed {
  0% { box-shadow: inset 0 0 100px rgba(255, 0, 0, 0.85); }
  100% { box-shadow: inset 0 0 0 rgba(255, 0, 0, 0); }
}

/* Boss Health Bar HUD */
.boss-hp-bar {
  position: absolute;
  top: 14px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(15, 23, 42, 0.85);
  backdrop-filter: blur(8px);
  border: 1px solid #ff1744;
  border-radius: 20px;
  padding: 6px 18px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  width: 320px;
  z-index: 10;
  box-shadow: 0 0 20px rgba(255, 23, 68, 0.4);
}

.boss-hp-bar.hidden {
  display: none;
}

.boss-bar-title {
  font-size: 0.82rem;
  font-weight: 800;
  color: #ff5252;
  text-transform: uppercase;
}

.boss-bar-track {
  width: 100%;
  height: 8px;
  background: rgba(0, 0, 0, 0.7);
  border-radius: 4px;
  overflow: hidden;
}

.boss-bar-fill {
  height: 100%;
  background: linear-gradient(90deg, #ff1744, #ff5252);
  width: 100%;
  transition: width 0.15s ease-out;
}

/* Boss Warning Banner */
.boss-banner {
  position: absolute;
  top: 70px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(220, 38, 38, 0.9);
  color: #fff;
  padding: 10px 28px;
  border-radius: 8px;
  font-weight: 800;
  font-size: 1.15rem;
  letter-spacing: 0.05em;
  box-shadow: 0 0 30px rgba(220, 38, 38, 0.8);
  z-index: 20;
  animation: pulseGlow 1s infinite alternate;
}

.boss-banner.hidden {
  display: none;
}

@keyframes pulseGlow {
  0% { transform: translateX(-50%) scale(0.98); opacity: 0.9; }
  100% { transform: translateX(-50%) scale(1.02); opacity: 1; }
}

/* Wave Preview Info Bar */
.wave-preview-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: rgba(18, 24, 38, 0.7);
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  padding: 6px 14px;
  font-size: 0.8rem;
  color: var(--text-secondary);
}

.preview-badge {
  background: #1e293b;
  padding: 2px 7px;
  border-radius: 6px;
  border: 1px solid #334155;
  color: #e2e8f0;
  font-weight: 600;
}

/* Bottom Bar: Cards & Inspector */
.game-bottom-bar {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 10px;
}

.cards-container {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.section-label {
  font-size: 0.73rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  color: var(--text-secondary);
}

.towers-deck, .spells-deck {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

/* Cards */
.tower-card, .spell-card {
  position: relative;
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  padding: 8px 12px;
  min-width: 105px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  cursor: pointer;
  transition: all 0.15s ease;
}

.tower-card:hover:not(.disabled), .spell-card:hover:not(.on-cooldown) {
  border-color: #38bdf8;
  background: rgba(30, 41, 59, 0.95);
  transform: translateY(-2px);
}

.tower-card.selected, .spell-card.selected {
  border-color: var(--accent-gold);
  box-shadow: 0 0 14px rgba(255, 215, 0, 0.45);
  background: rgba(45, 55, 72, 0.95);
}

.tower-card.disabled {
  opacity: 0.42;
  cursor: not-allowed;
  filter: grayscale(0.7);
}

.card-hotkey {
  position: absolute;
  top: 3px;
  left: 6px;
  font-size: 0.68rem;
  font-weight: 800;
  color: var(--text-secondary);
  background: rgba(0, 0, 0, 0.45);
  padding: 1px 4px;
  border-radius: 4px;
}

.card-icon {
  font-size: 1.45rem;
  margin-top: 4px;
}

.card-name {
  font-size: 0.78rem;
  font-weight: 700;
  text-align: center;
}

.card-cost {
  font-size: 0.74rem;
  color: var(--accent-gold);
  font-weight: 700;
}

.spell-card.on-cooldown {
  opacity: 0.6;
  cursor: not-allowed;
}

.spell-cooldown {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.68);
  border-radius: 10px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 1.1rem;
  font-weight: 800;
  color: #ff3d71;
}

/* Tower Inspector Sidebar */
.tower-inspector {
  background: var(--panel-bg);
  backdrop-filter: blur(14px);
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  padding: 12px 16px;
  width: 320px;
  display: flex;
  flex-direction: column;
  gap: 10px;
  box-shadow: 0 8px 24px rgba(0, 0, 0, 0.35);
}

.tower-inspector.hidden {
  display: none;
}

.insp-header {
  display: flex;
  align-items: center;
  gap: 10px;
  border-bottom: 1px solid rgba(255, 255, 255, 0.1);
  padding-bottom: 8px;
}

.insp-stats-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  font-size: 0.8rem;
  color: var(--text-secondary);
}

.insp-stats-grid span {
  color: var(--text-primary);
  font-weight: 700;
}

.strategy-row {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.strat-buttons {
  display: flex;
  gap: 4px;
}

.btn-strat {
  flex: 1;
  padding: 4px 6px;
  font-size: 0.72rem;
  background: #1e293b;
  color: var(--text-secondary);
  border: 1px solid #334155;
  border-radius: 4px;
  cursor: pointer;
}

.btn-strat.active {
  background: #0284c7;
  color: #fff;
  border-color: #38bdf8;
  font-weight: 700;
}

.insp-actions {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.btn-upgrade {
  background: linear-gradient(135deg, #15803d, #22c55e);
  color: #fff;
  border: none;
  font-weight: 700;
  padding: 8px;
}
.btn-upgrade:hover:not(:disabled) {
  background: linear-gradient(135deg, #16a34a, #4ade80);
}

.spec-container {
  display: flex;
  flex-direction: column;
  gap: 5px;
}

.btn-spec {
  background: #312e81;
  border: 1px solid #4338ca;
  color: #e0e7ff;
  font-size: 0.75rem;
  text-align: left;
  padding: 6px 8px;
  border-radius: 6px;
}
.btn-spec strong {
  color: #ffd700;
  font-size: 0.82rem;
}
.btn-spec small {
  color: #cbd5e1;
}
.btn-spec:hover:not(:disabled) {
  background: #3730a3;
  border-color: #6366f1;
}

.btn-sell {
  background: rgba(220, 38, 38, 0.2);
  color: #fca5a5;
  border: 1px solid rgba(220, 38, 38, 0.4);
  padding: 6px;
  font-size: 0.8rem;
}
.btn-sell:hover {
  background: rgba(220, 38, 38, 0.4);
}

/* Modals */
.modal-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0, 0, 0, 0.78);
  backdrop-filter: blur(8px);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 1000;
}

.modal-overlay.hidden {
  display: none;
}

.modal-content {
  background: #1e293b;
  border: 1px solid #475569;
  border-radius: 16px;
  padding: 28px;
  max-width: 480px;
  width: 90%;
  text-align: center;
  box-shadow: 0 16px 40px rgba(0, 0, 0, 0.6);
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.modal-content h2 {
  font-size: 1.8rem;
  font-weight: 800;
}

.modal-content p {
  color: var(--text-secondary);
  font-size: 0.92rem;
  line-height: 1.5;
}

.help-modal-content {
  max-width: 600px;
  text-align: left;
}

.help-modal-content h3 {
  color: #38bdf8;
  margin-top: 8px;
}

.help-list {
  padding-left: 20px;
  color: #cbd5e1;
  font-size: 0.88rem;
  line-height: 1.6;
}

@media (max-width: 960px) {
  .game-bottom-bar {
    grid-template-columns: 1fr;
  }
  .tower-inspector {
    width: 100%;
  }
}
