:root {
  --bg: #1c2b1f;
  --panel: #f4e4c1;
  --panel-dark: #d4b27a;
  --accent: #d36b2c;
  --accent-dark: #9e3f1f;
  --text: #2b2418;
  --grass: #6aa84f;
  --shadow: rgba(0, 0, 0, 0.25);
}

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

body {
  margin: 0;
  font-family: "Trebuchet MS", "Gill Sans", sans-serif;
  background: radial-gradient(circle at top, #2f4a2f 0%, #1b2a1d 60%, #101b12 100%);
  color: var(--text);
  min-height: 100vh;
  -webkit-touch-callout: none;
  -webkit-user-select: none;
  user-select: none;
  touch-action: manipulation;
}

#app {
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

.top-bar {
  display: flex;
  flex-direction: column;
  gap: 6px;
  padding: 8px;
  background: linear-gradient(90deg, #f0d6a2, #f5e1b8);
  border-bottom: 3px solid #b9854e;
  box-shadow: 0 4px 10px var(--shadow);
  justify-content: flex-start;
  align-items: stretch;
  position: relative;
  z-index: 30;
  pointer-events: auto;
}

.bar-row {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: nowrap;
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  padding-bottom: 2px;
}

.bar-row::-webkit-scrollbar {
  display: none;
}

.top-row {
  padding: 4px;
  background: rgba(255, 255, 255, 0.5);
  border-radius: 14px;
  border: 1px solid rgba(185, 133, 78, 0.5);
}

.bottom-row {
  padding: 4px;
  background: rgba(255, 255, 255, 0.7);
  border-radius: 14px;
  border: 1px solid rgba(185, 133, 78, 0.5);
}

.bottom-row #start-wave {
  margin-left: auto;
}

.home-btn {
  width: 38px;
  height: 38px;
  border-radius: 50%;
  background: #fff8e8;
  border: 2px solid #b9854e;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  text-decoration: none;
  box-shadow: 0 2px 4px var(--shadow);
  color: #6f4a26;
  flex: 0 0 auto;
}

.home-btn:active {
  transform: scale(0.96);
}

.home-icon {
  font-size: 18px;
  font-weight: 700;
}

.bar-title {
  font-weight: 800;
  color: #6f4a26;
  background: linear-gradient(180deg, #fff2cf, #f3d7a6);
  border: 2px solid #b9854e;
  border-radius: 12px;
  padding: 4px 10px;
  box-shadow: 0 2px 4px var(--shadow);
  flex: 0 0 auto;
}

.stat-line {
  display: inline-flex;
  align-items: center;
  gap: 6px;
  padding: 6px 10px;
  background: #fff4db;
  border: 2px solid #b9854e;
  border-radius: 999px;
  font-weight: 700;
  font-size: 13px;
  color: #6f4a26;
  white-space: nowrap;
}

.stat-line .sep {
  opacity: 0.5;
}

.controls {
  display: flex;
  flex-wrap: nowrap;
  gap: 6px;
  white-space: nowrap;
  margin-left: auto;
}

.tower-overlay {
  position: absolute;
  left: 50%;
  top: 50%;
  display: flex;
  gap: 8px;
  padding: 8px;
  background: rgba(255, 244, 219, 0.95);
  border: 2px solid #b9854e;
  border-radius: 14px;
  box-shadow: 0 4px 8px var(--shadow);
  z-index: 5;
  transform: translate(-50%, -120%);
  pointer-events: auto;
  touch-action: manipulation;
}

.tower-overlay[hidden] {
  display: none;
}

.tower-overlay::after {
  content: "";
  position: absolute;
  left: 50%;
  bottom: -6px;
  width: 12px;
  height: 12px;
  background: rgba(255, 244, 219, 0.95);
  border-right: 2px solid #b9854e;
  border-bottom: 2px solid #b9854e;
  transform: translateX(-50%) rotate(45deg);
}

.tower-overlay.below {
  transform: translate(-50%, 20%);
}

.tower-overlay.below::after {
  top: -6px;
  bottom: auto;
  transform: translateX(-50%) rotate(225deg);
}

.pill,
.primary {
  border: none;
  border-radius: 999px;
  padding: 10px 14px;
  font-size: 14px;
  font-weight: 700;
  cursor: pointer;
}

.pill {
  background: #f7e8c9;
  border: 2px solid #b9854e;
  color: #6d4a22;
  box-shadow: 0 2px 4px var(--shadow);
}

.primary {
  background: linear-gradient(180deg, var(--accent), var(--accent-dark));
  color: #fff7e6;
  border: 2px solid #6f2d12;
  box-shadow: 0 3px 6px var(--shadow);
}

.primary:active,
.pill:active {
  transform: translateY(1px);
}

.game-area {
  flex: 1;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 280px;
  gap: 12px;
  padding: 12px;
}

.canvas-wrap {
  position: relative;
  background: #132016;
  border: 4px solid #2f4a2f;
  border-radius: 18px;
  box-shadow: inset 0 0 0 3px #0c140d, 0 6px 12px var(--shadow);
  display: flex;
  align-items: center;
  justify-content: center;
  overflow: hidden;
  z-index: 1;
}

#game-canvas {
  width: 100%;
  height: auto;
  touch-action: pan-y;
  display: block;
}

.side-panel {
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.panel-section {
  background: var(--panel);
  border: 3px solid var(--panel-dark);
  border-radius: 16px;
  padding: 12px;
  box-shadow: 0 4px 8px var(--shadow);
}

.panel-title {
  font-weight: 800;
  font-size: 16px;
  margin-bottom: 8px;
  color: #6f4a26;
}

.build-bar {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 8px;
}

.build-card {
  display: flex;
  align-items: center;
  gap: 8px;
  background: #fff3da;
  border: 2px solid #b9854e;
  border-radius: 12px;
  padding: 7px 8px;
  cursor: pointer;
  box-shadow: 0 2px 4px var(--shadow);
  min-height: 52px;
  touch-action: manipulation;
}

.build-card.selected {
  outline: 3px solid #cc8f48;
  background: #ffe8bf;
}

.card-icon {
  width: 32px;
  height: 32px;
  border-radius: 8px;
  border: 2px solid #6f4a26;
  background: linear-gradient(135deg, #9b7345, #c89b64);
  position: relative;
  flex: 0 0 auto;
}

.card-icon.gun::after {
  content: "";
  position: absolute;
  width: 14px;
  height: 5px;
  background: #3b2a1a;
  top: 13px;
  left: 11px;
  border-radius: 4px;
}

.card-icon.slow {
  background: linear-gradient(135deg, #5c8ea8, #84b4c6);
}

.card-icon.slow::after {
  content: "";
  position: absolute;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 2px solid #20465a;
  top: 7px;
  left: 7px;
}

.card-icon.blast {
  background: linear-gradient(135deg, #b2603b, #d99c67);
}

.card-icon.blast::after {
  content: "";
  position: absolute;
  width: 10px;
  height: 10px;
  border-radius: 4px;
  background: #2b1c12;
  top: 9px;
  left: 9px;
}

.card-icon.lightning {
  background: linear-gradient(135deg, #c9a626, #e8d740);
}

.card-icon.lightning::after {
  content: "⚡";
  position: absolute;
  font-size: 14px;
  top: 5px;
  left: 8px;
  filter: brightness(0.7);
}

.card-icon.poison {
  background: linear-gradient(135deg, #4a8f40, #7bb86f);
}

.card-icon.poison::after {
  content: "☠";
  position: absolute;
  font-size: 12px;
  top: 7px;
  left: 9px;
  filter: brightness(0.8);
}

.card-mini {
  flex: 1;
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.card-name {
  font-weight: 800;
  color: #3c2a1a;
  font-size: 12px;
  line-height: 1.1;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.card-cost {
  margin-top: 2px;
  font-size: 11px;
  color: #7d5a32;
}

.card-effect {
  margin-top: 1px;
  font-size: 10px;
  color: #6f4a26;
  opacity: 0.9;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.effect-icon {
  width: 22px;
  height: 22px;
  border-radius: 999px;
  border: 1px solid #8a5d2f;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  font-size: 12px;
  color: #3c2a1a;
  background: #ffe4b9;
  flex: 0 0 auto;
}

.hint {
  margin-top: 8px;
  font-size: 12px;
  color: #6f4a26;
}

.tower-detail-card {
  margin-top: 10px;
  background: #fff4db;
  border: 2px solid #b9854e;
  border-radius: 12px;
  padding: 8px;
}

.tower-detail-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

#tower-detail-name {
  font-size: 14px;
  font-weight: 800;
  color: #5f3f1f;
}

.detail-close-btn {
  padding: 4px 9px;
  font-size: 11px;
}

.tower-detail-body {
  margin-top: 6px;
  font-size: 12px;
  line-height: 1.5;
  color: #6f4a26;
}

.overlay {
  position: fixed;
  inset: 0;
  background: rgba(9, 15, 9, 0.85);
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 12px;
  z-index: 200;
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-y;
}

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

.menu {
  background: #f9e7c2;
  border: 4px solid #a8753c;
  border-radius: 22px;
  padding: 20px;
  max-width: 520px;
  width: 100%;
  max-height: calc(100dvh - 24px);
  overflow-y: auto;
  -webkit-overflow-scrolling: touch;
  touch-action: pan-y;
  margin: 0 auto;
  text-align: center;
  box-shadow: 0 8px 16px var(--shadow);
}

.menu h1 {
  margin-top: 0;
  font-size: 32px;
  color: #6f4a26;
}

.subtitle {
  margin-top: -8px;
  color: #7d5a32;
}

.map-list {
  display: grid;
  grid-template-columns: repeat(2, minmax(0, 1fr));
  gap: 12px;
  margin: 16px 0;
}

.map-card {
  background: #fff4db;
  border: 2px solid #b9854e;
  border-radius: 14px;
  padding: 12px;
  cursor: pointer;
  box-shadow: 0 2px 4px var(--shadow);
}

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

.map-card h3 {
  margin: 6px 0;
}

.map-start-btn {
  margin-top: 10px;
  border: 2px solid #6f2d12;
  border-radius: 999px;
  padding: 8px 14px;
  font-size: 14px;
  font-weight: 700;
  color: #fff7e6;
  background: linear-gradient(180deg, var(--accent), var(--accent-dark));
}

.toast {
  position: fixed;
  left: 50%;
  bottom: 16px;
  transform: translateX(-50%);
  background: #2f4a2f;
  color: #fff7e6;
  padding: 10px 16px;
  border-radius: 14px;
  border: 2px solid #0d1b0e;
  box-shadow: 0 4px 8px var(--shadow);
  z-index: 12;
}

.drag-ghost {
  position: fixed;
  left: 0;
  top: 0;
  background: #f9e7c2;
  border: 2px solid #b9854e;
  border-radius: 12px;
  padding: 6px 10px;
  font-weight: 700;
  pointer-events: none;
  box-shadow: 0 4px 8px var(--shadow);
  z-index: 20;
}

@media (max-width: 900px) {
  .game-area {
    grid-template-columns: 1fr;
  }

  .side-panel {
    flex-direction: column;
  }

  .build-bar {
    grid-template-columns: repeat(3, minmax(0, 1fr));
  }
}

@media (max-width: 600px) {
  .pill,
  .primary {
    font-size: 13px;
    padding: 8px 10px;
  }

  .bar-title {
    font-size: 13px;
  }

  .stat-line {
    font-size: 12px;
  }

  .home-btn {
    width: 34px;
    height: 34px;
  }

  .build-bar {
    grid-template-columns: repeat(2, minmax(0, 1fr));
  }

  .menu {
    border-radius: 16px;
    padding: 14px;
    max-height: calc(100dvh - 16px);
  }

  .map-list {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 10px;
  }

  .map-card {
    padding: 10px;
  }
}
