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

:root {
  --bg: #1a1a2e;
  --surface: #16213e;
  --surface2: #0f3460;
  --accent: #e94560;
  --gold: #f5a623;
  --text: #eaeaea;
  --text-dim: #888;
  --radius: 10px;
}

html, body {
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', sans-serif;
  overflow: hidden;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

/* ── Screens ─────────────────────────────────────────────────────────────── */
.screen {
  display: none;
  position: fixed;
  inset: 0;
  overflow-y: auto;
}
.screen.active { display: flex; flex-direction: column; }

/* ── Auth / Menu / Leaderboard ───────────────────────────────────────────── */
#auth-screen, #menu-screen, #leaderboard-screen, #gameover-screen {
  align-items: center;
  justify-content: center;
  background: linear-gradient(135deg, #1a1a2e 0%, #16213e 50%, #0f3460 100%);
}

.auth-box, .menu-box {
  background: var(--surface);
  border: 1px solid var(--surface2);
  border-radius: var(--radius);
  padding: 28px 24px;
  width: min(340px, 92vw);
  display: flex;
  flex-direction: column;
  gap: 12px;
  box-shadow: 0 8px 32px rgba(0,0,0,0.5);
}

.auth-box h1, .menu-box h1, .menu-box h2 {
  text-align: center;
  font-size: 1.6rem;
}

.tagline {
  text-align: center;
  color: var(--text-dim);
  font-size: 0.85rem;
}

#auth-tabs {
  display: flex;
  gap: 8px;
}

.tab-btn {
  flex: 1;
  padding: 8px;
  background: var(--surface2);
  border: none;
  border-radius: var(--radius);
  color: var(--text-dim);
  font-size: 0.9rem;
  cursor: pointer;
  transition: background 0.2s, color 0.2s;
}
.tab-btn.active { background: var(--accent); color: #fff; }

input[type=text], input[type=password], input[type=email] {
  width: 100%;
  padding: 10px 12px;
  background: var(--bg);
  border: 1px solid var(--surface2);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 1rem;
}
input:focus { outline: 2px solid var(--accent); }

.primary-btn {
  width: 100%;
  padding: 12px;
  background: var(--accent);
  border: none;
  border-radius: var(--radius);
  color: #fff;
  font-size: 1rem;
  font-weight: 600;
  cursor: pointer;
  transition: opacity 0.2s;
}
.primary-btn:disabled { opacity: 0.5; cursor: not-allowed; }
.primary-btn:active { opacity: 0.8; }

.secondary-btn {
  width: 100%;
  padding: 10px;
  background: transparent;
  border: 1px solid var(--surface2);
  border-radius: var(--radius);
  color: var(--text);
  font-size: 0.95rem;
  cursor: pointer;
  transition: background 0.2s;
}
.secondary-btn:active { background: var(--surface2); }

.text-btn {
  width: 100%;
  padding: 8px;
  background: transparent;
  border: none;
  color: var(--text-dim);
  font-size: 0.85rem;
  cursor: pointer;
}

.error-msg { color: #ff6b6b; font-size: 0.85rem; min-height: 1rem; }

#player-info {
  display: flex;
  justify-content: space-between;
  font-size: 0.85rem;
  color: var(--text-dim);
}

/* ── Leaderboard ─────────────────────────────────────────────────────────── */
#leaderboard-list {
  max-height: 50vh;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.lb-row {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 10px;
  background: var(--bg);
  border-radius: 6px;
  font-size: 0.9rem;
}
.lb-row.lb-top { border-left: 3px solid var(--gold); }
.lb-rank { width: 28px; text-align: center; }
.lb-name { flex: 1; font-weight: 500; }
.lb-score { color: var(--gold); font-weight: 600; }
.lb-wave { color: var(--text-dim); font-size: 0.8rem; }
.empty-msg { text-align: center; color: var(--text-dim); padding: 20px 0; }

/* ── Game Screen ─────────────────────────────────────────────────────────── */
#game-screen {
  background: var(--bg);
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  gap: 0;
  overflow: hidden;
}

/* HUD */
#hud {
  width: 100%;
  display: flex;
  justify-content: space-around;
  background: var(--surface);
  padding: 6px 8px;
  border-bottom: 1px solid var(--surface2);
  flex-shrink: 0;
}

.hud-cell {
  font-size: 0.95rem;
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 3px;
}

/* Game layout wrappers */
#game-main {
  display: flex;
  flex-direction: column;
  align-items: center;
  flex: 1;
  width: 100%;
  min-height: 0;
}

#game-left {
  display: flex;
  flex-direction: column;
  align-items: center;
  width: 100%;
  max-width: 512px;
}

#game-right {
  display: flex;
  flex-direction: column;
  width: 100%;
  max-width: 512px;
}

/* Canvas */
#game-canvas {
  display: block;
  width: 100%;
  max-width: 512px;
  height: auto;
  touch-action: none;
  cursor: crosshair;
}

/* Wave controls */
#wave-controls {
  display: flex;
  gap: 8px;
  padding: 6px 12px;
  width: 100%;
}

.wave-btn {
  flex: 1;
  padding: 8px;
  background: var(--surface2);
  border: none;
  border-radius: var(--radius);
  color: var(--text);
  font-size: 0.9rem;
  font-weight: 600;
  cursor: pointer;
  transition: background 0.2s, opacity 0.2s;
}
.wave-btn:disabled { opacity: 0.4; cursor: not-allowed; }
.wave-btn:not(:disabled):active { background: var(--accent); }

/* Next wave panel */
#next-wave-panel {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  padding: 5px 12px;
  background: var(--surface);
  border-top: 1px solid var(--surface2);
}

.panel-label {
  font-size: 0.68rem;
  color: var(--text-dim);
  text-transform: uppercase;
  letter-spacing: 0.05em;
  white-space: nowrap;
}

#next-wave-enemies {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  align-items: center;
}

.nw-enemy {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 1px;
}

.nw-emoji { font-size: 1.15rem; line-height: 1; }
.nw-count { font-size: 0.58rem; color: var(--text-dim); }

/* Tower panel */
#tower-panel {
  width: 100%;
  background: var(--surface);
  border-top: 1px solid var(--surface2);
  padding: 8px;
  flex-shrink: 0;
}

#tower-grid {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 5px;
  margin-bottom: 6px;
}

.tower-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: var(--bg);
  border: 2px solid transparent;
  border-radius: 8px;
  padding: 4px 2px;
  cursor: pointer;
  transition: border-color 0.15s, background 0.15s;
  gap: 2px;
}
.tower-btn.selected {
  border-color: var(--gold);
  background: #2a2a1a;
}
.tower-btn:active { background: var(--surface2); }

.t-emoji { font-size: 1.4rem; line-height: 1; }
.t-cost  { font-size: 0.6rem; color: var(--gold); }

#tower-info-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  font-size: 0.78rem;
  color: var(--text-dim);
  min-height: 24px;
  padding: 0 4px;
}

#sell-btn {
  padding: 4px 10px;
  background: #2a6a2a;
  border: none;
  border-radius: 6px;
  color: #aaffaa;
  font-size: 0.75rem;
  cursor: pointer;
  white-space: nowrap;
}
#sell-btn:active { background: #3a8a3a; }

/* ── Desktop layout (≥960px) ─────────────────────────────────────────────── */
@media (min-width: 960px) {
  #game-screen { overflow-y: auto; }

  #game-main {
    flex-direction: row;
    align-items: flex-start;
    justify-content: center;
  }

  #game-left { flex: none; }

  #game-right {
    width: 210px;
    max-width: 210px;
    flex-shrink: 0;
    border-left: 1px solid var(--surface2);
    align-self: stretch;
  }

  #next-wave-panel {
    flex-direction: column;
    align-items: flex-start;
    border-top: none;
    border-bottom: 1px solid var(--surface2);
    padding: 10px;
    gap: 6px;
  }

  #next-wave-enemies { gap: 8px; }
  .nw-emoji { font-size: 1.4rem; }

  #tower-panel { border-top: none; }

  #tower-grid { grid-template-columns: repeat(4, 1fr); }

  .t-emoji { font-size: 1.6rem; }
  .t-cost  { font-size: 0.65rem; }
}

/* ── Game over ───────────────────────────────────────────────────────────── */
#gameover-stats {
  text-align: center;
  padding: 8px 0;
  display: flex;
  flex-direction: column;
  gap: 6px;
}
#gameover-stats strong { color: var(--gold); }
#go-highscore-msg { color: #88ff88; font-size: 0.9rem; }

/* ── Responsive: taller phones ───────────────────────────────────────────── */
@media (min-height: 700px) {
  .auth-box, .menu-box { gap: 16px; }
  .hud-cell { font-size: 1rem; }
  .t-emoji { font-size: 1.5rem; }
}
