:root {
  --bg: #0f131d;
  --panel: #191f2e;
  --panel-2: #22293d;
  --border: #323c58;
  --gold: #f5c451;
  --gold-dim: #b58c34;
  --text: #eef2ff;
  --muted: #9aa7c7;
  --green: #43b56f;
  --red: #e05660;
  --blue: #52a9e0;
  --purple: #9d7cff;
}

* {
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
  user-select: none;
}

html, body {
  margin: 0;
  padding: 0;
  width: 100%;
  height: 100%;
  background: radial-gradient(circle at top, #1b2236 0%, var(--bg) 70%);
  color: var(--text);
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  overflow: hidden;
  touch-action: manipulation;
}

#app {
  width: 100%;
  height: 100%;
  padding-top: env(safe-area-inset-top);
  padding-bottom: env(safe-area-inset-bottom);
  padding-left: env(safe-area-inset-left);
  padding-right: env(safe-area-inset-right);
}

.screen {
  display: none;
  width: 100%;
  height: 100%;
}

.screen.active {
  display: flex;
  flex-direction: column;
}

.hidden {
  display: none !important;
}

/* MENU SCREEN */
#menu-screen {
  overflow-y: auto;
  padding: 18px 20px 30px;
  max-width: 1120px;
  margin: 0 auto;
}

.menu-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding-bottom: 12px;
  border-bottom: 1px solid var(--border);
}

.eyebrow {
  font-size: 11px;
  letter-spacing: 0.2em;
  color: var(--gold);
  font-weight: 700;
}

h1, h2 {
  font-family: "Palatino Linotype", "Book Antiqua", Georgia, serif;
  margin: 0;
  letter-spacing: 0.02em;
}

h1 {
  font-size: clamp(26px, 4vw, 38px);
  color: #fff6dc;
}

.menu-actions {
  display: flex;
  align-items: center;
  gap: 10px;
}

.pill {
  padding: 7px 14px;
  border-radius: 999px;
  background: var(--panel-2);
  border: 1px solid var(--border);
  font-weight: 700;
  font-size: 14px;
}

.gold-pill {
  color: var(--gold);
  border-color: var(--gold-dim);
}

.icon-btn {
  min-width: 42px;
  min-height: 42px;
  border-radius: 10px;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  font-size: 18px;
  cursor: pointer;
}

.menu-intro {
  margin: 14px 0 18px;
  color: var(--muted);
  font-size: 14px;
  line-height: 1.5;
}

.menu-intro p {
  margin: 0 0 10px;
}

.legend-pills {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}

.mini-tag {
  font-size: 12px;
  background: rgba(255,255,255,0.05);
  border: 1px solid var(--border);
  padding: 4px 10px;
  border-radius: 999px;
  color: #d6dff5;
}

.map-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(290px, 1fr));
  gap: 16px;
}

.map-card {
  background: linear-gradient(180deg, var(--panel-2) 0%, var(--panel) 100%);
  border: 1px solid var(--border);
  border-radius: 14px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  transition: transform 0.14s ease, border-color 0.14s ease;
  cursor: pointer;
  text-align: left;
  color: var(--text);
  padding: 0;
}

.map-card:hover:not(.locked) {
  transform: translateY(-2px);
  border-color: var(--gold);
}

.map-card.locked {
  opacity: 0.58;
  cursor: not-allowed;
}

.map-thumb {
  width: 100%;
  aspect-ratio: 16 / 9;
  display: block;
  background: #141925;
  border-bottom: 1px solid var(--border);
}

.map-card-body {
  padding: 12px 14px 14px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  flex: 1;
}

.map-card-top {
  display: flex;
  justify-content: space-between;
  align-items: center;
  gap: 8px;
}

.map-card-title {
  font-family: "Palatino Linotype", Georgia, serif;
  font-size: 18px;
  font-weight: 700;
  color: #fff3d1;
}

.diff-badge {
  font-size: 11px;
  font-weight: 700;
  padding: 3px 8px;
  border-radius: 999px;
  background: rgba(255,255,255,0.08);
}

.diff-Easy { color: #7ae582; }
.diff-Normal { color: #ffd166; }
.diff-Moderate { color: #6bd4ff; }
.diff-Hard { color: #ff8c52; }
.diff-Brutal { color: #ff5c6c; }

.map-card-desc {
  font-size: 12.5px;
  color: var(--muted);
  line-height: 1.4;
}

.map-twist {
  font-size: 11.5px;
  color: #d6c2ff;
  background: rgba(157, 124, 255, 0.12);
  border: 1px solid rgba(157, 124, 255, 0.32);
  padding: 4px 8px;
  border-radius: 6px;
}

.map-card-foot {
  margin-top: auto;
  padding-top: 8px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 13px;
}

.stars-text {
  color: var(--gold);
  letter-spacing: 2px;
  font-size: 15px;
}

.play-cta {
  font-weight: 700;
  color: var(--gold);
}

/* GAME SCREEN */
#game-screen {
  height: 100%;
  justify-content: space-between;
}

.hud-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 12px;
  background: rgba(18, 22, 34, 0.96);
  border-bottom: 1px solid var(--border);
  flex-shrink: 0;
  flex-wrap: wrap;
}

.hud-left, .hud-stats, .hud-controls {
  display: flex;
  align-items: center;
  gap: 7px;
}

.map-title-hud {
  font-family: "Palatino Linotype", Georgia, serif;
  font-weight: 700;
  font-size: 15px;
  color: #f7e6b7;
}

.stat-chip {
  background: var(--panel-2);
  border: 1px solid var(--border);
  border-radius: 9px;
  padding: 5px 10px;
  font-weight: 700;
  font-size: 14px;
  white-space: nowrap;
}

.stat-chip.gold { color: var(--gold); }
.stat-chip.lives { color: #ff7a85; }
.stat-chip.wave { color: #9ecbff; }

.hud-btn {
  min-height: 36px;
  padding: 6px 12px;
  border-radius: 9px;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--text);
  font-weight: 700;
  font-size: 13px;
  cursor: pointer;
  white-space: nowrap;
}

.hud-btn:hover {
  border-color: #60709c;
}

.hud-btn.primary {
  background: linear-gradient(180deg, #3c9d62 0%, #2a7848 100%);
  border-color: #54c780;
  color: #fff;
}

.hud-btn.early {
  background: linear-gradient(180deg, #c98f2b 0%, #9b6a19 100%);
  border-color: #ffd166;
  color: #fff;
}

.hud-btn.active {
  border-color: var(--gold);
  color: var(--gold);
}

.hud-btn:disabled {
  opacity: 0.45;
  cursor: default;
}

.rotate-hint {
  display: none;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 5px 12px;
  background: rgba(245, 196, 81, 0.14);
  border-bottom: 1px solid rgba(245, 196, 81, 0.3);
  color: #ffe299;
  font-size: 12px;
  flex-shrink: 0;
}

@media (max-width: 640px) and (orientation: portrait) {
  .rotate-hint:not(.dismissed) {
    display: flex;
  }
}

.dismiss-btn {
  background: none;
  border: none;
  color: #ffe299;
  font-size: 14px;
  cursor: pointer;
  padding: 2px 6px;
}

/* CANVAS STAGE */
.stage-wrap {
  flex: 1;
  min-height: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 6px;
  position: relative;
}

.canvas-frame {
  position: relative;
  width: 100%;
  height: 100%;
  display: flex;
  align-items: center;
  justify-content: center;
}

#game-canvas {
  aspect-ratio: 16 / 9;
  width: auto;
  height: auto;
  max-width: 100%;
  max-height: 100%;
  border-radius: 10px;
  border: 2px solid var(--border);
  box-shadow: 0 12px 32px rgba(0, 0, 0, 0.55);
  background: #141925;
  touch-action: none;
  display: block;
}

.wave-toast {
  position: absolute;
  top: 14px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(18, 22, 34, 0.9);
  border: 1px solid var(--gold);
  color: #fff3d1;
  padding: 7px 16px;
  border-radius: 999px;
  font-weight: 700;
  font-size: 13px;
  pointer-events: none;
  box-shadow: 0 6px 18px rgba(0,0,0,0.5);
}

.wave-toast.boss {
  border-color: #ff574d;
  color: #ffd3cf;
}

.pause-overlay {
  position: absolute;
  inset: 0;
  background: rgba(10, 13, 21, 0.72);
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 10px;
  z-index: 10;
}

.pause-card {
  background: var(--panel);
  border: 1px solid var(--gold-dim);
  border-radius: 14px;
  padding: 20px 24px;
  text-align: center;
  max-width: 320px;
}

/* BOTTOM DECK */
.bottom-deck {
  background: rgba(18, 22, 34, 0.97);
  border-top: 1px solid var(--border);
  padding: 8px 10px;
  flex-shrink: 0;
}

.build-dock {
  display: flex;
  gap: 8px;
  justify-content: safe center;
  overflow-x: auto;
  padding-bottom: 2px;
}

.tower-card {
  min-width: 104px;
  flex: 1 1 0;
  max-width: 148px;
  background: var(--panel-2);
  border: 1.5px solid var(--border);
  border-radius: 10px;
  padding: 7px 8px;
  color: var(--text);
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 3px;
  cursor: pointer;
  text-align: left;
  position: relative;
}

.tower-card.selected {
  border-color: var(--gold);
  background: #2b3550;
  box-shadow: 0 0 0 1px var(--gold);
}

.tower-card.unaffordable {
  opacity: 0.48;
}

.tower-card-head {
  display: flex;
  width: 100%;
  justify-content: space-between;
  align-items: center;
  font-weight: 700;
  font-size: 13px;
}

.tower-cost {
  color: var(--gold);
  font-size: 12.5px;
}

.tower-role {
  font-size: 11px;
  color: var(--muted);
}

.air-badge {
  font-size: 9.5px;
  font-weight: 800;
  padding: 1px 5px;
  border-radius: 4px;
  background: rgba(82, 169, 224, 0.2);
  color: #8ee0ff;
  border: 1px solid rgba(82, 169, 224, 0.4);
}

/* INSPECTOR PANEL */
.inspector-panel {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
  max-width: 980px;
  margin: 0 auto;
}

.insp-info {
  display: flex;
  flex-direction: column;
  gap: 3px;
  min-width: 165px;
}

.insp-title {
  font-weight: 700;
  font-size: 14px;
  color: #fff3d1;
}

.insp-sub {
  font-size: 11.5px;
  color: var(--muted);
}

.insp-target {
  display: flex;
  gap: 4px;
  align-items: center;
}

.pill-btn {
  min-height: 32px;
  padding: 4px 9px;
  border-radius: 7px;
  border: 1px solid var(--border);
  background: var(--panel-2);
  color: var(--muted);
  font-size: 12px;
  font-weight: 700;
  cursor: pointer;
}

.pill-btn.active {
  background: #334061;
  border-color: var(--gold);
  color: var(--gold);
}

.insp-upgrades {
  display: flex;
  gap: 8px;
  flex: 1;
  justify-content: center;
  flex-wrap: wrap;
}

.up-btn {
  min-height: 44px;
  padding: 6px 12px;
  border-radius: 9px;
  border: 1.5px solid #4c648f;
  background: #23304d;
  color: var(--text);
  display: flex;
  flex-direction: column;
  align-items: flex-start;
  gap: 2px;
  cursor: pointer;
  min-width: 155px;
  max-width: 260px;
}

.up-btn.branch-a { border-color: #c99c3c; }
.up-btn.branch-b { border-color: #46a8c7; }

.up-btn:disabled {
  opacity: 0.45;
  cursor: default;
}

.up-btn-top {
  display: flex;
  width: 100%;
  justify-content: space-between;
  gap: 8px;
  font-weight: 700;
  font-size: 12.5px;
}

.up-btn-cost { color: var(--gold); }
.up-btn-desc { font-size: 10.5px; color: #b7c4e3; text-align: left; }

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

.sell-btn {
  min-height: 38px;
  padding: 6px 11px;
  border-radius: 9px;
  border: 1px solid #96454d;
  background: #402026;
  color: #ffb8be;
  font-weight: 700;
  font-size: 12.5px;
  cursor: pointer;
}

/* RESULT MODAL */
.modal {
  position: fixed;
  inset: 0;
  background: rgba(9, 12, 20, 0.8);
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  z-index: 50;
}

.modal-card {
  background: linear-gradient(180deg, #21283b 0%, #171c2b 100%);
  border: 1.5px solid var(--gold-dim);
  border-radius: 16px;
  padding: 24px 26px;
  max-width: 390px;
  width: 100%;
  text-align: center;
  box-shadow: 0 20px 50px rgba(0,0,0,0.6);
}

.result-stars {
  font-size: 36px;
  color: var(--gold);
  letter-spacing: 6px;
  margin-bottom: 6px;
}

.result-stats {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 8px;
  margin: 14px 0 18px;
}

.r-stat {
  background: rgba(255,255,255,0.04);
  border: 1px solid var(--border);
  border-radius: 8px;
  padding: 8px 4px;
  font-size: 12px;
  color: var(--muted);
}

.r-stat strong {
  display: block;
  font-size: 16px;
  color: var(--text);
}

.modal-actions {
  display: flex;
  gap: 8px;
  justify-content: center;
  flex-wrap: wrap;
}

/* MOBILE COMPACT LANDSCAPE & PORTRAIT TWEAKS */
@media (max-width: 700px) {
  .map-title-hud { display: none; }
  .hud-bar { padding: 6px 8px; gap: 5px; }
  .stat-chip { padding: 4px 8px; font-size: 12.5px; }
  .hud-btn { min-height: 34px; padding: 5px 9px; font-size: 12px; }
  .tower-card { min-width: 90px; padding: 6px 7px; }
  .tower-card-head { font-size: 12px; }
  .tower-role { font-size: 10px; }
}

@media (max-height: 460px) {
  .hud-bar { padding: 4px 8px; }
  .bottom-deck { padding: 4px 8px; }
  .tower-card { padding: 4px 7px; }
  .tower-role { display: none; }
  .stage-wrap { padding: 3px; }
}

@media (max-width: 640px) and (orientation: portrait) {
  .build-dock {
    display: grid;
    grid-template-columns: repeat(3, 1fr);
    gap: 6px;
    overflow-x: visible;
  }
  .tower-card {
    min-width: 0;
    max-width: none;
    padding: 6px 7px;
  }
}
