:root {
  --hud-bg: rgba(4, 12, 28, 0.78);
  --hud-line: rgba(162, 203, 255, 0.42);
  --hud-text: #f2f7ff;
  --good: #88ffbf;
  --warn: #ffd57f;
  --bad: #ff8f8f;
}

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

html,
body {
  margin: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  font-family: "Lucida Console", "Courier New", monospace;
  background: #02060f;
  color: var(--hud-text);
  touch-action: manipulation;
}

.game-shell {
  position: fixed;
  inset: 0;
  width: 100%;
  height: 100%;
}

.screen-overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-items: center;
  padding: 18px;
  background: rgba(2, 7, 17, 0.78);
  backdrop-filter: blur(5px);
  z-index: 8;
}

.screen-panel {
  width: min(980px, calc(100vw - 32px));
  max-height: min(92vh, 860px);
  overflow: auto;
  display: grid;
  gap: 14px;
  padding: 18px;
  border-radius: 18px;
  border: 1px solid rgba(188, 218, 255, 0.35);
  background:
    radial-gradient(circle at top, rgba(76, 136, 255, 0.14), transparent 36%),
    linear-gradient(180deg, rgba(6, 15, 33, 0.95), rgba(4, 10, 22, 0.96));
  box-shadow: 0 16px 48px rgba(0, 0, 0, 0.42);
}

.main-menu-panel {
  align-content: start;
}

.menu-hero {
  display: grid;
  gap: 8px;
}

.menu-kicker {
  font-size: 12px;
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: #92c2ff;
}

.menu-title {
  margin: 0;
  font-size: clamp(28px, 5vw, 44px);
  line-height: 1;
  color: #f4f8ff;
}

.menu-subtitle,
.menu-section-copy {
  margin: 0;
  font-size: 14px;
  line-height: 1.45;
  color: rgba(226, 238, 255, 0.84);
}

.menu-summary,
.game-over-rewards,
.pause-actions {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
}

.menu-pill {
  min-width: 144px;
  justify-content: flex-start;
}

.menu-pill-label {
  margin-left: auto;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.06em;
  color: rgba(216, 232, 255, 0.7);
}

.primary-menu-btn,
.secondary-menu-btn {
  appearance: none;
  border-radius: 14px;
  font: inherit;
  font-weight: 700;
  cursor: pointer;
  color: var(--hud-text);
  touch-action: manipulation;
}

.primary-menu-btn,
.secondary-menu-btn {
  min-height: 54px;
  padding: 12px 18px;
  border: 1px solid rgba(188, 218, 255, 0.5);
}

.primary-menu-btn {
  background: linear-gradient(180deg, rgba(110, 225, 183, 0.98), rgba(28, 151, 117, 0.98));
  color: #031c16;
}

.secondary-menu-btn {
  background: rgba(8, 18, 37, 0.9);
  color: #eaf3ff;
}

.danger-menu-btn {
  background: linear-gradient(180deg, rgba(255, 132, 132, 0.98), rgba(183, 34, 34, 0.98));
  color: #fff6f6;
}

.menu-section {
  display: grid;
  gap: 10px;
  padding: 14px;
  border-radius: 14px;
  border: 1px solid rgba(154, 193, 255, 0.26);
  background: rgba(6, 14, 31, 0.72);
}

.menu-section-head {
  display: grid;
  gap: 4px;
}

.menu-section-title {
  margin: 0;
  font-size: 18px;
  color: #eef5ff;
}

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

.tech-tree-item {
  --afford-ratio: 0;
  --afford-fill: rgba(255, 214, 130, 0.24);
  position: relative;
  overflow: hidden;
  isolation: isolate;
  width: 100%;
  display: grid;
  gap: 6px;
  padding: 12px;
  border-radius: 12px;
  border: 1px solid rgba(172, 214, 255, 0.28);
  background: rgba(8, 18, 37, 0.9);
  color: #eef5ff;
  text-align: left;
}

.tech-tree-item::before,
.tech-tree-item::after {
  content: "";
  position: absolute;
  left: 0;
  top: 0;
  height: 100%;
  pointer-events: none;
}

.tech-tree-item::before {
  width: 100%;
  background: rgba(151, 165, 187, 0.12);
}

.tech-tree-item::after {
  width: calc(var(--afford-ratio, 0) * 100%);
  background: var(--afford-fill);
  transition: width 0.2s ease;
}

.tech-tree-item > * {
  position: relative;
  z-index: 1;
}

.tech-tree-item.active {
  border-color: rgba(136, 255, 191, 0.7);
}

.tech-tree-item.maxed {
  border-color: rgba(130, 255, 224, 0.82);
  box-shadow: 0 0 0 2px rgba(130, 255, 224, 0.18);
}

.tech-tree-item:disabled {
  opacity: 0.55;
}

.tech-tree-title-row,
.tech-tree-meta {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
}

.tech-tree-name {
  font-size: 14px;
  font-weight: 700;
}

.tech-tree-level,
.tech-tree-cost,
.tech-tree-branch {
  border-radius: 7px;
  font-size: 11px;
  font-weight: 700;
  line-height: 1.3;
  padding: 2px 6px;
}

.tech-tree-level {
  border: 1px solid rgba(184, 216, 255, 0.22);
  background: rgba(0, 0, 0, 0.38);
  color: #dcecff;
}

.tech-tree-cost {
  border: 1px solid rgba(62, 41, 1, 0.42);
  background: rgba(255, 220, 145, 0.92);
  color: #3c2801;
}

.tech-tree-branch {
  border: 1px solid rgba(130, 255, 224, 0.24);
  background: rgba(8, 35, 33, 0.72);
  color: #bfffee;
}

.tech-tree-desc {
  font-size: 12px;
  line-height: 1.45;
  color: rgba(226, 238, 255, 0.82);
}

#game-canvas {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  display: block;
  image-rendering: pixelated;
  touch-action: none;
  will-change: transform;
}

.hud-top {
  position: absolute;
  left: 0;
  right: 0;
  top: 0;
  padding: calc(8px + env(safe-area-inset-top)) 10px 8px;
  display: flex;
  justify-content: center;
  gap: 10px;
  pointer-events: none;
}

.hud-speed-controls {
  display: flex;
  gap: 6px;
  pointer-events: auto;
}

.hud-pill {
  min-width: 88px;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  padding: 6px 10px;
  border-radius: 10px;
  border: 1px solid var(--hud-line);
  background: var(--hud-bg);
  backdrop-filter: blur(4px);
}

.hud-speed-btn {
  min-width: 40px;
  justify-content: center;
  font: inherit;
  font-size: 20px;
  font-weight: 700;
  line-height: 1;
  cursor: pointer;
  pointer-events: auto;
  color: var(--hud-text);
}

.hud-speed-btn:disabled {
  opacity: 0.5;
}

.hud-speed-readout {
  min-width: 58px;
}

.hud-icon {
  width: 22px;
  height: 22px;
  image-rendering: pixelated;
}

.hud-value {
  font-size: clamp(16px, 2.8vw, 22px);
  line-height: 1;
  letter-spacing: 0.02em;
}

.hud-wave-btn {
  position: relative;
  appearance: none;
  font: inherit;
  border: 1px solid rgba(130, 255, 224, 0.55);
  background: rgba(5, 22, 31, 0.85);
  color: var(--hud-text);
  padding-right: 12px;
  cursor: pointer;
  pointer-events: auto;
  touch-action: manipulation;
}

.hud-wave-btn:disabled {
  opacity: 0.5;
}

.hud-wave-btn .hud-value {
  min-width: 22px;
}

.hud-wave-progress {
  min-width: 34px;
  border-radius: 7px;
  border: 1px solid rgba(130, 255, 224, 0.4);
  background: rgba(130, 255, 224, 0.14);
  color: #b7ffe8;
  font-size: 11px;
  font-weight: 700;
  line-height: 1.4;
  text-align: center;
  padding: 1px 5px;
}

.hud-bottom {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 0;
  padding: 8px 8px calc(10px + env(safe-area-inset-bottom));
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  gap: 6px;
  pointer-events: none;
}

.hud-builds {
  position: relative;
  flex: 1;
  min-width: 0;
  pointer-events: auto;
}

.hud-builds-scroll {
  display: flex;
  gap: 6px;
}

.hud-actions {
  position: relative;
  pointer-events: auto;
}

.control-btn {
  --afford-ratio: 0;
  --afford-fill: rgba(255, 214, 130, 0.44);
  position: relative;
  width: clamp(50px, 11vw, 78px);
  height: clamp(50px, 11vw, 78px);
  border-radius: 12px;
  border: 1px solid rgba(188, 218, 255, 0.44);
  background: rgba(7, 16, 35, 0.84);
  box-shadow: 0 2px 12px rgba(0, 0, 0, 0.34);
  padding: 0;
  display: grid;
  place-items: center;
  touch-action: manipulation;
  overflow: hidden;
  isolation: isolate;
}

.control-btn::before,
.control-btn::after {
  content: "";
  position: absolute;
  top: 0;
  left: 0;
  height: 100%;
  pointer-events: none;
}

.control-btn::before {
  width: 100%;
  background: rgba(151, 165, 187, 0.28);
  z-index: 0;
}

.control-btn::after {
  width: calc(var(--afford-ratio, 0) * 100%);
  background: var(--afford-fill);
  z-index: 0;
  transition: width 0.2s ease;
}

.control-btn > * {
  position: relative;
  z-index: 1;
}

.control-btn .btn-icon {
  width: 66%;
  height: 66%;
  image-rendering: pixelated;
  pointer-events: none;
}

.control-btn .btn-level {
  position: absolute;
  left: 3px;
  top: 3px;
  min-width: 18px;
  height: 16px;
  border-radius: 7px;
  border: 1px solid rgba(255, 255, 255, 0.2);
  background: rgba(0, 0, 0, 0.45);
  color: #d9e9ff;
  font-size: 10px;
  font-weight: 700;
  line-height: 14px;
  text-align: center;
  padding: 0 3px;
}

.control-btn .btn-level:empty {
  display: none;
}

.control-btn .btn-badge {
  position: absolute;
  right: 3px;
  bottom: 3px;
  min-width: 18px;
  height: 16px;
  border-radius: 7px;
  border: 1px solid rgba(0, 0, 0, 0.28);
  background: rgba(255, 214, 130, 0.92);
  color: #2a1a00;
  font-size: 10px;
  font-weight: 700;
  line-height: 14px;
  text-align: center;
  padding: 0 3px;
}

.build-btn .btn-badge {
  border: 1px solid rgba(0, 0, 0, 0.28);
  background: rgba(255, 214, 130, 0.92);
  color: #2a1a00;
}

.build-btn {
  --afford-fill: rgba(255, 196, 76, 0.48);
}

.upgrade-menu-toggle .btn-badge {
  border: 1px solid rgba(1, 20, 18, 0.4);
  background: rgba(130, 255, 224, 0.9);
  color: #04231a;
}

.upgrade-menu-toggle {
  --afford-fill: rgba(130, 255, 224, 0.44);
}


.build-btn.active {
  border-color: rgba(136, 255, 191, 0.9);
  box-shadow: 0 0 0 2px rgba(136, 255, 191, 0.35), 0 3px 12px rgba(0, 0, 0, 0.35);
}

.upgrade-menu-toggle.active {
  border-color: rgba(130, 255, 224, 0.92);
  box-shadow: 0 0 0 2px rgba(130, 255, 224, 0.3), 0 3px 12px rgba(0, 0, 0, 0.35);
}

.control-btn.cannot-afford {
  border-color: rgba(168, 180, 197, 0.45);
}

.control-btn:disabled {
  opacity: 0.46;
}

.upgrade-menu {
  position: absolute;
  right: 0;
  bottom: calc(100% + 8px);
  width: min(340px, 84vw);
  display: grid;
  gap: 6px;
  padding: 8px;
  border-radius: 12px;
  border: 1px solid rgba(130, 255, 224, 0.34);
  background: rgba(5, 14, 30, 0.95);
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.4);
  backdrop-filter: blur(6px);
}

.type-upgrade-menu {
  left: 0;
  right: auto;
  width: min(300px, 82vw);
  border-color: rgba(171, 209, 255, 0.34);
  background: rgba(9, 16, 33, 0.95);
}

.upgrade-subhead,
.unit-menu-title {
  font-size: 11px;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: #9bc2ff;
  padding: 2px 4px;
}

.unit-menu {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  bottom: calc(100% + 8px);
  width: min(300px, 82vw);
  display: grid;
  gap: 6px;
  padding: 8px;
  border-radius: 12px;
  border: 1px solid rgba(255, 201, 112, 0.34);
  background: rgba(28, 17, 7, 0.95);
  box-shadow: 0 6px 24px rgba(0, 0, 0, 0.4);
  backdrop-filter: blur(6px);
  pointer-events: auto;
  z-index: 4;
}

.unit-element-panel {
  display: grid;
  gap: 6px;
}

.unit-element-title {
  font-size: 11px;
  letter-spacing: 0.04em;
  text-transform: uppercase;
  color: #b5caf1;
  padding: 2px 4px 0;
}

.unit-element-actions {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}

.unit-action-btn {
  --afford-fill: rgba(255, 196, 76, 0.32);
}

.unit-element-btn {
  --afford-ratio: 1;
  grid-template-columns: 20px 1fr;
  gap: 6px;
  padding: 7px 8px;
}

.unit-element-btn .upgrade-item-level,
.unit-element-btn .upgrade-item-cost {
  min-width: auto;
  padding: 1px 4px;
}

.unit-element-btn .upgrade-item-level {
  grid-column: 1 / -1;
  justify-self: start;
}

.unit-element-btn .upgrade-item-cost {
  grid-column: 1 / -1;
  justify-self: end;
}

.unit-element-icon {
  display: grid;
  place-items: center;
  font-size: 18px;
  line-height: 1;
}

.unit-element-ice {
  --afford-fill: rgba(111, 196, 255, 0.2);
}

.unit-element-fire {
  --afford-fill: rgba(255, 148, 92, 0.22);
}

.unit-element-speed {
  --afford-fill: rgba(129, 255, 193, 0.22);
}

.unit-element-power {
  --afford-fill: rgba(255, 210, 109, 0.24);
}

.unit-element-freeze-spread {
  --afford-fill: rgba(121, 204, 255, 0.22);
}

.unit-element-freeze-duration {
  --afford-fill: rgba(174, 151, 255, 0.22);
}

.unit-element-ice.active {
  border-color: rgba(125, 212, 255, 0.9);
  box-shadow: 0 0 0 2px rgba(125, 212, 255, 0.24);
}

.unit-element-fire.active {
  border-color: rgba(255, 159, 96, 0.92);
  box-shadow: 0 0 0 2px rgba(255, 159, 96, 0.24);
}

.unit-element-speed.active {
  border-color: rgba(129, 255, 193, 0.92);
  box-shadow: 0 0 0 2px rgba(129, 255, 193, 0.22);
}

.unit-element-power.active {
  border-color: rgba(255, 210, 109, 0.92);
  box-shadow: 0 0 0 2px rgba(255, 210, 109, 0.24);
}

.unit-element-freeze-spread.active {
  border-color: rgba(121, 204, 255, 0.92);
  box-shadow: 0 0 0 2px rgba(121, 204, 255, 0.22);
}

.unit-element-freeze-duration.active {
  border-color: rgba(174, 151, 255, 0.92);
  box-shadow: 0 0 0 2px rgba(174, 151, 255, 0.22);
}

.unit-element-ice .upgrade-item-cost {
  border-color: rgba(17, 49, 82, 0.5);
  background: rgba(140, 221, 255, 0.92);
  color: #03253b;
}

.unit-element-fire .upgrade-item-cost {
  border-color: rgba(80, 29, 8, 0.5);
  background: rgba(255, 177, 121, 0.94);
  color: #3b1804;
}

.unit-element-speed .upgrade-item-cost {
  border-color: rgba(14, 74, 45, 0.48);
  background: rgba(154, 255, 203, 0.94);
  color: #06301f;
}

.unit-element-power .upgrade-item-cost {
  border-color: rgba(85, 60, 7, 0.48);
  background: rgba(255, 223, 135, 0.94);
  color: #3e2a03;
}

.unit-element-freeze-spread .upgrade-item-cost {
  border-color: rgba(18, 50, 87, 0.48);
  background: rgba(157, 221, 255, 0.94);
  color: #04253d;
}

.unit-element-freeze-duration .upgrade-item-cost {
  border-color: rgba(54, 36, 109, 0.48);
  background: rgba(197, 182, 255, 0.94);
  color: #22124c;
}

.unit-sell-btn {
  --afford-ratio: 1;
  --afford-fill: rgba(255, 209, 126, 0.22);
}

.unit-sell-btn .upgrade-item-cost {
  border-color: rgba(62, 41, 1, 0.42);
  background: rgba(255, 220, 145, 0.92);
  color: #3c2801;
}

.upgrade-item {
  --afford-ratio: 0;
  --afford-fill: rgba(130, 255, 224, 0.36);
  position: relative;
  overflow: hidden;
  isolation: isolate;
  width: 100%;
  border: 1px solid rgba(172, 214, 255, 0.28);
  border-radius: 10px;
  background: rgba(8, 18, 37, 0.9);
  color: #eaf3ff;
  padding: 6px 8px;
  display: grid;
  grid-template-columns: 28px 1fr auto auto;
  gap: 8px;
  align-items: center;
  text-align: left;
  touch-action: manipulation;
}

.upgrade-item::before,
.upgrade-item::after {
  content: "";
  position: absolute;
  left: 0;
  top: 0;
  height: 100%;
  pointer-events: none;
}

.upgrade-item::before {
  width: 100%;
  background: rgba(151, 165, 187, 0.2);
  z-index: 0;
}

.upgrade-item::after {
  width: calc(var(--afford-ratio, 0) * 100%);
  background: var(--afford-fill);
  z-index: 0;
  transition: width 0.2s ease;
}

.upgrade-item > * {
  position: relative;
  z-index: 1;
}

.upgrade-item:disabled {
  opacity: 0.45;
}

.upgrade-item-icon {
  width: 24px;
  height: 24px;
  image-rendering: pixelated;
}

.upgrade-item-name {
  font-size: clamp(11px, 2vw, 13px);
  line-height: 1.2;
}

.upgrade-item-level {
  min-width: 40px;
  border-radius: 7px;
  border: 1px solid rgba(184, 216, 255, 0.22);
  background: rgba(0, 0, 0, 0.4);
  color: #dcecff;
  font-size: 11px;
  font-weight: 700;
  line-height: 1.4;
  text-align: center;
  padding: 1px 5px;
}

.upgrade-item-cost {
  min-width: 28px;
  border-radius: 7px;
  border: 1px solid rgba(1, 20, 18, 0.4);
  background: rgba(130, 255, 224, 0.9);
  color: #04231a;
  font-size: 11px;
  font-weight: 700;
  line-height: 1.4;
  text-align: center;
  padding: 1px 6px;
}

.upgrade-item.locked .upgrade-item-cost {
  border-color: rgba(45, 5, 5, 0.4);
  background: rgba(255, 145, 145, 0.92);
  color: #2d0505;
}

.upgrade-item.cannot-afford {
  border-color: rgba(158, 172, 190, 0.32);
}

.start-game-btn {
  position: absolute;
  left: 50%;
  bottom: calc(96px + env(safe-area-inset-bottom));
  transform: translateX(-50%);
  min-width: min(360px, calc(100vw - 32px));
  max-width: calc(100vw - 32px);
  min-height: 72px;
  padding: 16px 28px;
  border: 2px solid rgba(255, 235, 235, 0.72);
  border-radius: 20px;
  background: linear-gradient(180deg, rgba(255, 110, 110, 0.98), rgba(191, 20, 20, 0.98));
  box-shadow: 0 10px 28px rgba(0, 0, 0, 0.42), 0 0 0 4px rgba(255, 72, 72, 0.22);
  color: #fff7f7;
  font: inherit;
  font-size: clamp(24px, 4.8vw, 34px);
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
  text-align: center;
  cursor: pointer;
  pointer-events: auto;
  touch-action: manipulation;
  z-index: 5;
}

.start-game-btn:active {
  transform: translateX(-50%) translateY(1px);
}

.corner-btn {
  position: absolute;
  width: 42px;
  height: 42px;
  border-radius: 999px;
  border: 1px solid rgba(188, 218, 255, 0.55);
  background: rgba(6, 14, 32, 0.86);
  color: #ecf5ff;
  font: inherit;
  font-size: 24px;
  line-height: 1;
  display: grid;
  place-items: center;
  text-decoration: none;
  touch-action: manipulation;
}

.restart-btn {
  top: calc(10px + env(safe-area-inset-top));
  right: 10px;
}

.home-btn {
  top: calc(10px + env(safe-area-inset-top));
  left: 10px;
}

.game-over {
  z-index: 9;
}

.hidden {
  display: none;
}

.game-over-panel {
  display: grid;
  justify-items: center;
  text-align: center;
}

.pause-panel {
  width: min(460px, calc(100vw - 32px));
}

.game-over-title {
  font-size: clamp(24px, 4.2vw, 36px);
}

.game-over-icon {
  width: 48px;
  height: 48px;
  image-rendering: pixelated;
}

.game-over-stats {
  display: flex;
  gap: 8px;
}

.overlay-restart-btn {
  position: static;
}

@media (max-width: 720px) {
  .screen-overlay {
    padding: 12px;
  }

  .screen-panel {
    width: calc(100vw - 24px);
    max-height: 94vh;
    padding: 14px;
  }

  .hud-top {
    gap: 6px;
    flex-wrap: wrap;
  }

  .hud-bottom {
    gap: 8px;
    justify-content: flex-start;
  }

  .hud-builds {
    overflow-x: auto;
    overflow-y: hidden;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    touch-action: pan-x;
    padding-bottom: 2px;
  }

  .hud-builds::-webkit-scrollbar {
    display: none;
  }

  .hud-builds-scroll {
    width: max-content;
    min-width: 100%;
    padding-right: 4px;
  }

  .upgrade-menu {
    width: min(360px, 92vw);
    right: 0;
  }

  .type-upgrade-menu {
    width: min(330px, 92vw);
    left: 0;
    right: auto;
  }

  .unit-menu {
    width: min(330px, 92vw);
  }

  .unit-element-actions {
    grid-template-columns: 1fr;
  }

  .tech-tree-list {
    grid-template-columns: 1fr;
  }

  .upgrade-item {
    grid-template-columns: 26px 1fr auto auto;
    gap: 6px;
    padding: 6px 7px;
  }

  .control-btn {
    width: 44px;
    height: 44px;
    border-radius: 10px;
  }

  .start-game-btn {
    bottom: calc(104px + env(safe-area-inset-bottom));
    min-height: 66px;
    padding: 14px 22px;
    letter-spacing: 0.05em;
  }

  .corner-btn {
    width: 38px;
    height: 38px;
    font-size: 21px;
  }
}
