:root {
  --panel-bg: rgba(16, 22, 38, 0.82);
  --panel-border: rgba(120, 150, 210, 0.35);
  --accent: #4fd1c5;
  --accent-warm: #f6ad55;
  --text: #e8f0ff;
  --danger: #fc8181;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100%;
  overflow: hidden;
  font-family: "Segoe UI", "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
  background: #0b1020;
  color: var(--text);
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

#game-root,
#game-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
}

#game-canvas {
  display: block;
  touch-action: none;
}

/* ---- Top HUD ---- */
#hud {
  position: absolute;
  top: 16px;
  left: 16px;
  display: flex;
  gap: 10px;
  flex-wrap: wrap;
  pointer-events: none;
}

.hud-stat {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 8px 14px;
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: 12px;
  backdrop-filter: blur(6px);
  font-size: 17px;
  font-weight: 700;
  letter-spacing: 0.3px;
}

.hud-icon {
  font-size: 16px;
}

/* ---- Tower panel ---- */
#tower-panel {
  position: absolute;
  right: 16px;
  top: 16px;
  width: 210px;
  padding: 12px;
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: 16px;
  backdrop-filter: blur(8px);
}

.panel-title {
  font-size: 12px;
  letter-spacing: 2px;
  text-transform: uppercase;
  color: #9fb6e8;
  margin: 0 0 10px 2px;
}

.tower-btn {
  position: relative;
  width: 100%;
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  margin-bottom: 8px;
  background: rgba(255, 255, 255, 0.05);
  border: 1px solid rgba(255, 255, 255, 0.1);
  border-radius: 10px;
  color: var(--text);
  cursor: pointer;
  font-size: 14px;
  transition: background 0.15s ease, border-color 0.15s ease, transform 0.1s ease;
  touch-action: manipulation;
}

.tower-btn:hover {
  background: rgba(79, 209, 197, 0.14);
  border-color: rgba(79, 209, 197, 0.5);
}

.tower-btn.selected {
  background: rgba(79, 209, 197, 0.24);
  border-color: var(--accent);
  box-shadow: 0 0 0 2px rgba(79, 209, 197, 0.25);
}

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

.tower-name {
  flex: 1;
  text-align: left;
  font-weight: 600;
}

.tower-cost {
  color: #ffd479;
  font-weight: 700;
}

.tower-key {
  position: absolute;
  top: -6px;
  right: -6px;
  width: 20px;
  height: 20px;
  display: grid;
  place-items: center;
  font-size: 11px;
  font-weight: 800;
  color: #0b1020;
  background: var(--accent-warm);
  border-radius: 50%;
}

.panel-divider {
  height: 1px;
  background: var(--panel-border);
  margin: 10px 0;
}

.action-btn {
  width: 100%;
  padding: 11px;
  margin-bottom: 8px;
  background: linear-gradient(180deg, #38b2ac, #2c7a7b);
  border: 1px solid #4fd1c5;
  border-radius: 10px;
  color: #07120f;
  font-size: 14px;
  font-weight: 800;
  cursor: pointer;
  letter-spacing: 0.5px;
  touch-action: manipulation;
}

.action-btn:hover {
  filter: brightness(1.08);
}

.action-btn.ghost {
  background: rgba(255, 255, 255, 0.06);
  border-color: rgba(255, 255, 255, 0.14);
  color: var(--text);
}

.action-btn.danger {
  background: rgba(252, 129, 129, 0.08);
  border-color: rgba(252, 129, 129, 0.35);
  color: var(--danger);
}

/* ---- Transient message ---- */
#message {
  position: absolute;
  left: 50%;
  top: 18%;
  transform: translateX(-50%);
  padding: 8px 18px;
  background: rgba(16, 22, 38, 0.85);
  border: 1px solid var(--panel-border);
  border-radius: 999px;
  opacity: 0;
  pointer-events: none;
  transition: opacity 0.25s ease;
  font-weight: 600;
}

#message.show {
  opacity: 1;
}

/* ---- Overlay ---- */
#overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  background: rgba(5, 8, 18, 0.68);
  backdrop-filter: blur(4px);
}

#overlay.hidden {
  display: none;
}

.overlay-card {
  min-width: 300px;
  text-align: center;
  padding: 30px 34px;
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: 20px;
}

.overlay-card h1 {
  margin: 0 0 10px;
  font-size: 30px;
}

.overlay-card p {
  margin: 0 0 20px;
  color: #b9c7ea;
}

#overlay-btn {
  padding: 11px 30px;
  background: var(--accent-warm);
  border: none;
  border-radius: 10px;
  color: #201407;
  font-size: 15px;
  font-weight: 800;
  cursor: pointer;
}

.hidden {
  display: none !important;
}

#level-buttons {
  display: flex;
  flex-direction: column;
  gap: 8px;
  margin: 0 0 18px;
}

.level-btn {
  padding: 12px 14px;
  background: rgba(255, 255, 255, 0.06);
  border: 1px solid rgba(120, 150, 210, 0.25);
  border-radius: 10px;
  color: var(--text);
  cursor: pointer;
  text-align: left;
}

.level-btn:hover {
  background: rgba(79, 209, 197, 0.14);
  border-color: var(--accent);
}

.level-btn .level-name {
  display: block;
  font-size: 15px;
  font-weight: 800;
  margin-bottom: 3px;
}

.level-btn .level-sub {
  display: block;
  font-size: 12px;
  color: #9fb6e8;
}

.level-btn.locked {
  opacity: 0.5;
  cursor: not-allowed;
}

/* ---- Hint ---- */
#controls-hint {
  position: absolute;
  left: 16px;
  bottom: 16px;
  padding: 7px 12px;
  font-size: 12px;
  color: #8fa2cf;
  background: rgba(16, 22, 38, 0.6);
  border: 1px solid rgba(120, 150, 210, 0.2);
  border-radius: 999px;
  pointer-events: none;
}

/* ---- Legend ---- */
#legend {
  position: absolute;
  left: 16px;
  bottom: 64px;
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 10px 14px;
  font-size: 12px;
  color: #c6d3f2;
  background: rgba(16, 22, 38, 0.68);
  border: 1px solid rgba(120, 150, 210, 0.22);
  border-radius: 12px;
  backdrop-filter: blur(6px);
  pointer-events: none;
}

#legend span {
  display: flex;
  align-items: center;
  gap: 8px;
}

.dot {
  width: 13px;
  height: 13px;
  border-radius: 3px;
  flex: none;
}

.dot.road {
  background: #8a6b3f;
}

.dot.build {
  background: #2e4b6b;
  box-shadow: inset 0 0 0 1px rgba(150, 180, 220, 0.4);
}

.dot.range {
  background: transparent;
  border: 2px solid #4fd1c5;
  border-radius: 50%;
}

@media (max-width: 900px), (pointer: coarse) {
  #hud {
    top: 8px;
    left: 8px;
    gap: 6px;
  }

  .hud-stat {
    padding: 5px 10px;
    font-size: 13px;
    border-radius: 9px;
    gap: 5px;
  }

  .hud-icon {
    font-size: 12px;
  }

  #tower-panel {
    top: auto;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100%;
    display: flex;
    flex-wrap: wrap;
    align-items: center;
    gap: 6px;
    padding: 8px 8px calc(8px + env(safe-area-inset-bottom));
    border-radius: 14px 14px 0 0;
  }

  .panel-title,
  .panel-divider {
    display: none;
  }

  .tower-btn {
    width: calc(25% - 5px);
    margin-bottom: 0;
    padding: 7px 4px;
    flex-direction: column;
    gap: 2px;
    font-size: 12px;
    border-radius: 9px;
  }

  .tower-key {
    top: -5px;
    right: -5px;
    width: 15px;
    height: 15px;
    font-size: 9px;
  }

  .action-btn {
    flex: 1 1 45%;
    margin-bottom: 0;
    padding: 9px 4px;
    font-size: 12px;
    border-radius: 9px;
  }

  #message {
    top: 15%;
  }

  .overlay-card {
    width: min(90vw, 340px);
    min-width: 0;
  }

  #controls-hint,
  #legend {
    display: none;
  }

  html,
  body {
    touch-action: none;
  }
}
