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

:root {
  --bg: #0f1419;
  --panel: #1a2332;
  --border: #2d3a4f;
  --text: #e8edf5;
  --muted: #8b9cb3;
  --accent: #4fc3f7;
  --gold: #ffd54f;
  --danger: #ef5350;
  --success: #66bb6a;
}

body {
  font-family: "Segoe UI", system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  overscroll-behavior: none;
}

#app {
  max-width: 1400px;
  margin: 0 auto;
  padding: 12px;
}

header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin-bottom: 12px;
  padding: 8px 12px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 8px;
}

header h1 {
  font-size: 1.4rem;
  letter-spacing: 0.04em;
}

.stats {
  display: flex;
  gap: 20px;
  font-weight: 600;
}

#stat-gold { color: var(--gold); }
#stat-lives { color: var(--danger); }

.tip-bar {
  font-size: 0.75rem;
  color: var(--muted);
  text-align: center;
  line-height: 1.4;
  margin-bottom: 10px;
  padding: 0 8px;
}

main {
  display: flex;
  justify-content: center;
}

.upgrade-buttons {
  display: grid;
  gap: 4px;
  margin: 8px 0;
}

#turret-popup .upgrade-buttons {
  margin: 6px 0;
}

#turret-popup button {
  padding: 5px 8px;
  font-size: 0.78rem;
}

#turret-popup #btn-sell {
  margin-bottom: 4px;
}

#turret-popup #btn-combo-hint {
  font-size: 0.72rem;
}

.shop-key {
  display: inline-block;
  margin-left: 4px;
  padding: 0 5px;
  font-size: 0.65rem;
  font-weight: 700;
  color: var(--muted);
  background: #0f1722;
  border: 1px solid var(--border);
  border-radius: 3px;
  vertical-align: middle;
}

button {
  font: inherit;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #243044;
  color: var(--text);
  cursor: pointer;
  transition: background 0.15s;
}

button:hover:not(:disabled) {
  background: #2f3f58;
}

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

button .cost {
  color: var(--gold);
  font-size: 0.8rem;
}

#btn-sell {
  width: 100%;
  margin-bottom: 6px;
  background: #3d2020;
  border-color: #5c3030;
}

#btn-combo-hint {
  width: 100%;
  font-size: 0.85rem;
}

.game-column {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  width: 100%;
  max-width: 960px;
  margin: 0 auto;
}

#canvas-wrap {
  position: relative;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: #0a0e14;
  line-height: 0;
  width: 100%;
}

/* Controls bar sits BELOW the canvas so nothing ever covers the game
   itself — important in portrait mode where screen space is tight.
   Layout: [Combos] [turret circles] [Start Wave] on wide screens;
   the turret row moves to its own line above the two action buttons
   once the bar gets too narrow to fit in one row (see media query). */
#controls-bar {
  display: grid;
  grid-template-columns: 1fr auto 1fr;
  grid-template-areas: "combo hud wave";
  align-items: center;
  gap: 12px;
  width: 100%;
  padding: 10px 14px;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
}

.control-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  padding: 10px 14px;
  font-size: 0.85rem;
  font-weight: 600;
  background: #243044;
  border: 1px solid rgba(79, 195, 247, 0.55);
  border-radius: 999px;
  touch-action: manipulation;
  min-height: 44px;
}

.control-btn:hover:not(:disabled) {
  background: #2f3f58;
}

.control-combo {
  grid-area: combo;
  justify-self: start;
  border-color: rgba(255, 213, 79, 0.55);
}

.control-wave {
  grid-area: wave;
  justify-self: end;
}

/* Bottom circular turret picker — big, thumb-friendly targets, equally
   usable with a mouse on desktop. */
#turret-hud {
  grid-area: hud;
  justify-self: center;
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: center;
}

.turret-btn {
  position: relative;
  flex: 0 0 auto;
  width: clamp(46px, 12vw, 58px);
  height: clamp(46px, 12vw, 58px);
  border-radius: 50%;
  border: 2px solid rgba(255, 255, 255, 0.25);
  background:
    radial-gradient(circle at 35% 30%, rgba(255, 255, 255, 0.4), rgba(255, 255, 255, 0) 55%),
    var(--tc, #37474f);
  color: #10151d;
  font-weight: 800;
  font-size: 1.05rem;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  padding: 0;
  transition: transform 0.12s ease, border-color 0.12s ease, opacity 0.12s ease, box-shadow 0.12s ease;
  touch-action: manipulation;
}

.turret-btn:hover:not(.disabled) {
  transform: translateY(-2px);
}

.turret-btn:active {
  transform: scale(0.92);
}

.turret-btn.selected {
  border-color: var(--accent);
  box-shadow: 0 0 0 3px rgba(79, 195, 247, 0.4);
  transform: scale(1.08);
}

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

.turret-btn-letter {
  text-shadow: 0 1px 1px rgba(0, 0, 0, 0.35);
  pointer-events: none;
}

.turret-btn-cost {
  position: absolute;
  bottom: -8px;
  left: 50%;
  transform: translateX(-50%);
  background: #10151d;
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: var(--gold);
  font-size: 0.6rem;
  font-weight: 700;
  padding: 1px 5px;
  border-radius: 8px;
  white-space: nowrap;
  pointer-events: none;
}

#turret-popup {
  position: absolute;
  z-index: 5;
  width: 168px;
  padding: 8px;
  background: rgba(26, 35, 50, 0.5);
  border: 1px solid rgba(79, 195, 247, 0.55);
  border-radius: 6px;
  line-height: normal;
  pointer-events: auto;
  backdrop-filter: blur(4px);
}

#turret-popup[hidden] {
  display: none !important;
}

#turret-popup-info {
  font-size: 0.75rem;
  line-height: 1.45;
  margin-bottom: 2px;
}

#turret-popup-info .popup-pos {
  color: var(--muted);
}

#turret-popup-info .combo-ready {
  color: var(--gold);
}

#turret-popup-info .combo-hint {
  color: var(--muted);
}

#turret-popup #btn-sell {
  width: 100%;
  background: #3d2020;
  border-color: #5c3030;
}

#turret-popup #btn-combo-hint {
  width: 100%;
}

canvas {
  display: block;
  width: 960px;
  max-width: 100%;
  height: auto;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

#combo-list {
  list-style: none;
  font-size: 0.72rem;
  line-height: 1.5;
  max-height: 220px;
  overflow-y: auto;
  color: var(--muted);
}

#combo-list li {
  margin-bottom: 4px;
  padding-left: 8px;
  border-left: 3px solid var(--border);
}

#combo-list .combo-name {
  color: var(--accent);
  font-weight: 600;
}

/* Combo Reference modal — richer per-combo entries */
.combo-entry {
  border-left: 3px solid var(--combo-color, var(--border)) !important;
  padding: 6px 0 6px 10px !important;
  margin-bottom: 8px !important;
  display: flex;
  flex-direction: column;
  gap: 3px;
}

.combo-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.combo-header .combo-name {
  color: var(--combo-color, var(--accent));
  font-weight: 700;
  font-size: 0.85rem;
}

.combo-cat {
  flex: 0 0 auto;
  font-size: 0.6rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.05em;
  padding: 2px 6px;
  border-radius: 999px;
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid var(--combo-color, var(--border));
  color: var(--combo-color, var(--muted));
  white-space: nowrap;
}

.combo-recipe {
  font-size: 0.68rem;
  color: var(--muted);
}

.combo-desc {
  font-size: 0.74rem;
  line-height: 1.4;
  color: var(--text);
}

.combo-stats {
  font-size: 0.68rem;
  color: var(--gold);
  font-weight: 600;
}

#overlay {
  position: absolute;
  inset: 0;
  z-index: 10;
  background: rgba(0, 0, 0, 0.72);
  display: flex;
  align-items: center;
  justify-content: center;
}

#overlay[hidden] {
  display: none !important;
}

#overlay-box {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 24px 32px;
  text-align: center;
  width: min(360px, 100%);
  max-height: 90%;
  overflow-y: auto;
  /* #canvas-wrap sets line-height: 0 (to remove the whitespace gap under the
     <canvas> element), which is inherited by every descendant. Without this
     reset, every line of text in here collapses onto the same baseline and
     overlaps. */
  line-height: normal;
  display: flex;
  flex-direction: column;
  gap: 14px;
}

#overlay-box h2 {
  line-height: 1.3;
}

#overlay-box p {
  color: var(--muted);
  line-height: 1.4;
}

.hs-form {
  display: flex;
  flex-direction: column;
  gap: 6px;
  text-align: left;
}

.hs-form label {
  font-size: 0.75rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
}

.hs-form input {
  font: inherit;
  padding: 8px 10px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: #0f1722;
  color: var(--text);
  line-height: normal;
}

.hs-form input:focus {
  outline: 2px solid var(--accent);
  outline-offset: 1px;
}

.hs-form button {
  background: #1e3a2e;
  border-color: #2e5c46;
}

.hs-status {
  min-height: 1em;
  font-size: 0.78rem;
  line-height: 1.3;
  color: var(--gold);
}

#highscore-board {
  text-align: left;
}

#highscore-board h3 {
  font-size: 0.8rem;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: var(--muted);
  margin-bottom: 6px;
}

#highscore-list {
  list-style: none;
  max-height: 200px;
  overflow-y: auto;
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.hs-row {
  display: flex;
  align-items: baseline;
  gap: 8px;
  padding: 5px 8px;
  border-radius: 4px;
  background: #121a26;
  font-size: 0.82rem;
  line-height: 1.3;
}

.hs-rank {
  flex: 0 0 auto;
  width: 20px;
  color: var(--muted);
  font-weight: 700;
}

.hs-name {
  flex: 1 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.hs-meta {
  flex: 0 0 auto;
  color: var(--muted);
  font-size: 0.72rem;
  white-space: nowrap;
}

.hs-score {
  flex: 0 0 auto;
  min-width: 2.5em;
  text-align: right;
  color: var(--gold);
  font-weight: 700;
}

.hs-empty {
  color: var(--muted);
  font-size: 0.8rem;
  padding: 4px 6px;
}

/* Combo reference modal */
.modal-overlay {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: rgba(0, 0, 0, 0.72);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
}

.modal-overlay[hidden] {
  display: none !important;
}

.modal-box {
  width: 100%;
  max-width: 460px;
  max-height: 80vh;
  display: flex;
  flex-direction: column;
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: 10px;
  padding: 16px;
}

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

.modal-header h2 {
  font-size: 1rem;
  letter-spacing: 0.04em;
}

.modal-close {
  padding: 4px 10px;
  font-size: 1.1rem;
  line-height: 1;
  background: #243044;
}

#combo-modal #combo-list {
  overflow-y: auto;
  max-height: none;
  font-size: 0.8rem;
}

/* Touch-friendly sizing on small screens (portrait phones/tablets) */
@media (max-width: 700px) {
  #app {
    padding: 8px;
  }

  header {
    padding: 8px;
    flex-wrap: wrap;
    gap: 6px;
  }

  header h1 {
    font-size: 1.1rem;
  }

  .stats {
    gap: 10px;
    font-size: 0.8rem;
  }

  .tip-bar {
    display: none;
  }

  button {
    padding: 10px 12px;
  }

  /* Turret circles get their own full-width row above the two action
     buttons once there isn't room for a single tidy row. */
  #controls-bar {
    grid-template-columns: 1fr 1fr;
    grid-template-areas:
      "hud hud"
      "combo wave";
    gap: 8px;
    padding: 8px 10px;
  }

  .control-btn {
    justify-self: stretch;
    padding: 10px;
    font-size: 0.78rem;
  }

  #turret-hud {
    gap: 8px;
  }

  #turret-popup {
    width: 148px;
  }

  #overlay-box {
    padding: 16px 18px;
    max-height: 94%;
  }

  #turret-popup button {
    padding: 8px 8px;
  }
}

/* Short/landscape viewports (phones rotated sideways): shrink chrome and
   cap the canvas by available height (960x640 is a 3:2 box, plus the
   controls bar below it) so the whole game fits without scrolling. */
@media (orientation: landscape) and (max-height: 520px) {
  #app {
    padding: 6px;
  }

  header {
    padding: 4px 10px;
    margin-bottom: 6px;
  }

  header h1 {
    display: none;
  }

  .tip-bar {
    display: none;
  }

  .game-column {
    gap: 6px;
  }

  #canvas-wrap {
    max-width: min(100%, calc((100vh - 130px) * 1.5));
    margin: 0 auto;
  }

  #controls-bar {
    padding: 6px 10px;
    gap: 8px;
  }

  .control-btn {
    padding: 6px 10px;
    font-size: 0.72rem;
    min-height: 34px;
  }

  #turret-hud {
    gap: 6px;
  }

  .turret-btn {
    width: clamp(38px, 8vh, 50px);
    height: clamp(38px, 8vh, 50px);
    font-size: 0.9rem;
  }
}
