* {
  box-sizing: border-box;
}

html,
body {
  width: 100%;
  height: 100%;
  margin: 0;
  overflow: hidden;
  background: #070909;
  color: #f5f1df;
  font-family: "Courier New", Consolas, monospace;
  cursor: default;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  touch-action: none;
}

button {
  font: inherit;
}

#gameCanvas {
  position: fixed;
  inset: 0;
  width: 100vw;
  height: 100vh;
  display: block;
  image-rendering: pixelated;
  background: #171a18;
  cursor: none;
}

#uiRoot {
  position: fixed;
  inset: 0;
  pointer-events: none;
}

.touch-controls {
  position: fixed;
  inset: 0;
  z-index: 5;
  display: none;
  pointer-events: none;
}

.touch-stick,
.touch-action {
  pointer-events: auto;
  touch-action: none;
}

.touch-stick {
  position: absolute;
  left: max(18px, env(safe-area-inset-left));
  bottom: max(18px, env(safe-area-inset-bottom));
  width: 136px;
  height: 136px;
  display: grid;
  place-items: center;
}

.touch-stick-base {
  width: 116px;
  height: 116px;
  border: 3px solid rgba(245, 241, 223, 0.28);
  border-radius: 50%;
  background: rgba(7, 10, 10, 0.42);
  box-shadow: inset 0 0 0 3px rgba(0, 0, 0, 0.35), 0 8px 26px rgba(0, 0, 0, 0.45);
}

.touch-stick-knob {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 54px;
  height: 54px;
  margin: -27px 0 0 -27px;
  border: 3px solid #0a0c0c;
  border-radius: 50%;
  background: linear-gradient(#6f7b74, #252c2b);
  box-shadow: inset 0 4px 0 rgba(255, 255, 255, 0.16), inset 0 -5px 0 rgba(0, 0, 0, 0.35);
}

.touch-action {
  position: absolute;
  min-width: 72px;
  min-height: 58px;
  padding: 8px 12px;
  color: #fff8d8;
  font: 700 15px "Courier New", Consolas, monospace;
  text-shadow: 2px 2px 0 #070707;
  border: 3px solid #090a0a;
  border-radius: 8px;
  background: linear-gradient(#4f5955 0 14%, #2b3331 14% 72%, #121615 72%);
  box-shadow: inset 0 3px 0 rgba(255, 255, 255, 0.18), inset 0 -4px 0 rgba(0, 0, 0, 0.42), 4px 4px 0 rgba(0, 0, 0, 0.42);
}

.touch-action:active,
.touch-action.is-held {
  transform: translate(2px, 2px);
  filter: brightness(1.18);
}

.touch-fire {
  right: max(20px, env(safe-area-inset-right));
  bottom: max(24px, env(safe-area-inset-bottom));
  width: 92px;
  height: 92px;
  border-radius: 50%;
  font-size: 17px;
  background: linear-gradient(#f0b43a 0 14%, #bd7516 14% 72%, #71340a 72%);
}

.touch-reload {
  right: max(28px, env(safe-area-inset-right));
  bottom: calc(max(24px, env(safe-area-inset-bottom)) + 104px);
}

.touch-dash {
  left: calc(max(18px, env(safe-area-inset-left)) + 138px);
  bottom: max(32px, env(safe-area-inset-bottom));
}

body.game-active .touch-controls {
  display: block;
}

.screen {
  position: absolute;
  inset: 0;
  display: none;
  align-items: center;
  justify-content: center;
  padding: clamp(16px, 3vw, 42px);
  background: rgba(5, 7, 7, 0.42);
  pointer-events: auto;
  cursor: default;
}

.screen.active {
  display: flex;
}

.city-silhouette {
  position: absolute;
  inset: 0;
  background:
    linear-gradient(rgba(5, 8, 9, 0.12), rgba(5, 8, 9, 0.72)),
    linear-gradient(90deg, transparent 0 7%, rgba(7, 11, 12, 0.95) 7% 12%, transparent 12% 18%, rgba(9, 13, 15, 0.9) 18% 27%, transparent 27% 35%, rgba(7, 10, 11, 0.94) 35% 44%, transparent 44% 54%, rgba(8, 12, 13, 0.92) 54% 63%, transparent 63% 75%, rgba(7, 10, 11, 0.94) 75% 84%, transparent 84%),
    radial-gradient(circle at 50% 100%, rgba(119, 21, 17, 0.18), transparent 44%);
  opacity: 0.9;
}

.menu-panel,
.panel {
  position: relative;
  width: min(460px, 92vw);
  padding: 24px;
  border: 4px solid #2c3130;
  outline: 2px solid #090b0b;
  background:
    linear-gradient(180deg, rgba(28, 34, 33, 0.97), rgba(7, 10, 10, 0.98)),
    repeating-linear-gradient(0deg, rgba(255,255,255,0.035) 0 1px, transparent 1px 5px),
    radial-gradient(circle at 18% 12%, rgba(139, 36, 24, 0.16), transparent 34%);
  box-shadow:
    inset 0 3px 0 rgba(255, 255, 255, 0.08),
    inset 0 -5px 0 rgba(0, 0, 0, 0.42),
    inset 0 0 0 2px rgba(255, 255, 255, 0.05),
    0 0 0 2px rgba(0, 0, 0, 0.8),
    0 22px 70px rgba(0, 0, 0, 0.72);
}

.wide {
  width: min(820px, 94vw);
  max-height: 88vh;
  overflow: auto;
}

.game-title {
  margin: 0 0 12px;
  text-align: center;
  font-size: clamp(42px, 9vw, 82px);
  line-height: 0.82;
  letter-spacing: 0;
  text-shadow:
    4px 4px 0 #060606,
    -2px 0 0 #222,
    0 4px 18px rgba(153, 19, 14, 0.45);
}

.game-title span {
  display: block;
}

.game-title span:nth-child(1) {
  color: #f0f0e7;
}

.game-title span:nth-child(2) {
  color: #7ac343;
}

.game-title span:nth-child(3) {
  color: #d33a28;
}

.subtitle {
  margin: 0 0 22px;
  text-align: center;
  color: #f3c65b;
  font-weight: 700;
  text-shadow: 2px 2px 0 #060606;
}

.pixel-btn {
  display: block;
  width: 100%;
  min-height: 46px;
  margin: 10px 0;
  padding: 8px 16px;
  color: #f5f1df;
  background:
    linear-gradient(#4a504e 0 12%, #303635 12% 72%, #171a1a 72%),
    repeating-linear-gradient(90deg, rgba(255,255,255,0.04) 0 2px, transparent 2px 8px);
  border: 3px solid #111;
  outline: 2px solid #575d59;
  box-shadow:
    inset 0 3px 0 rgba(255, 255, 255, 0.18),
    inset 0 -4px 0 rgba(0, 0, 0, 0.42),
    4px 4px 0 rgba(0, 0, 0, 0.45);
  text-shadow: 2px 2px 0 #070707;
  cursor: pointer;
}

.pixel-btn:hover,
.pixel-btn:focus-visible {
  transform: translate(-1px, -1px);
  filter: brightness(1.16);
}

.pixel-btn:active {
  transform: translate(2px, 2px);
  box-shadow:
    inset 0 2px 0 rgba(255, 255, 255, 0.12),
    inset 0 -2px 0 rgba(0, 0, 0, 0.45),
    1px 1px 0 rgba(0, 0, 0, 0.45);
}

.pixel-btn.primary {
  color: #fff8d8;
  background:
    linear-gradient(#f0b43a 0 14%, #bd7516 14% 72%, #71340a 72%),
    repeating-linear-gradient(90deg, rgba(255,255,255,0.08) 0 2px, transparent 2px 8px);
  outline-color: #f2bd45;
}

.pixel-btn:disabled {
  color: #8a8d87;
  background: linear-gradient(#252828, #171919);
  outline-color: #343837;
  filter: grayscale(0.7);
}

.record-box,
.controls {
  margin-top: 16px;
  padding: 12px;
  border: 2px solid #343837;
  background:
    linear-gradient(180deg, rgba(14, 18, 18, 0.82), rgba(0, 0, 0, 0.54)),
    repeating-linear-gradient(0deg, rgba(255,255,255,0.025) 0 1px, transparent 1px 4px);
  box-shadow: inset 0 2px 0 rgba(255,255,255,0.06), inset 0 -3px 0 rgba(0,0,0,0.38);
  line-height: 1.5;
}

.record-box p,
.controls p {
  margin: 4px 0;
}

.record-box strong,
.coin-total strong,
.stats-grid strong {
  color: #ffd359;
}

.panel-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  margin-bottom: 14px;
  border-bottom: 2px solid #303534;
}

.panel h2 {
  margin: 0 0 12px;
  color: #f3c65b;
  text-shadow: 2px 2px 0 #060606;
}

.upgrade-list {
  display: grid;
  gap: 12px;
}

.upgrade-card {
  display: grid;
  grid-template-columns: 54px 1fr 116px;
  gap: 12px;
  align-items: center;
  padding: 12px;
  border: 2px solid #353a38;
  background:
    linear-gradient(180deg, rgba(20, 25, 24, 0.9), rgba(6, 8, 8, 0.9)),
    repeating-linear-gradient(90deg, rgba(255,255,255,0.025) 0 1px, transparent 1px 6px);
  box-shadow: inset 0 2px 0 rgba(255,255,255,0.06), 4px 4px 0 rgba(0,0,0,0.34);
}

.upgrade-icon {
  width: 46px;
  height: 46px;
  display: grid;
  place-items: center;
  border: 3px solid #101211;
  outline: 2px solid #4c544e;
  background: linear-gradient(#303936, #121616);
  color: #f2c64d;
  font-size: 24px;
}

.mag-icon {
  position: relative;
  width: 30px;
  height: 28px;
  display: flex;
  align-items: end;
  justify-content: center;
  gap: 3px;
}

.mag-icon::before {
  content: "";
  position: absolute;
  left: 2px;
  right: 2px;
  bottom: 1px;
  height: 7px;
  background: #151a1a;
  border: 2px solid #070808;
  box-shadow: inset 0 2px 0 rgba(255,255,255,0.12);
}

.mag-icon i {
  width: 6px;
  height: 22px;
  display: block;
  background: linear-gradient(#ffe07a 0 24%, #c9821f 24% 100%);
  border: 2px solid #070808;
  box-shadow: inset 1px 0 0 rgba(255,255,255,0.28), inset -1px 0 0 rgba(0,0,0,0.38);
}

.upgrade-info h3 {
  margin: 0 0 6px;
  color: #f2f0dc;
}

.upgrade-info p {
  margin: 2px 0;
  color: #b9c5b5;
  font-size: 14px;
}

.upgrade-buy {
  margin: 0;
  min-height: 40px;
}

.upgrade-buy.insufficient {
  color: #ffb6aa;
  outline-color: #843329;
  background: linear-gradient(#4a2724, #1b1110);
}

.gameover-panel {
  width: min(560px, 94vw);
  text-align: center;
}

.gameover-title {
  color: #d83a2a !important;
  font-size: clamp(42px, 9vw, 76px);
  line-height: 0.95;
  margin-bottom: 18px !important;
}

.stats-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px 18px;
  width: min(360px, 100%);
  margin: 0 auto 20px;
  text-align: left;
  font-size: 18px;
}

.stats-grid span {
  color: #d6dccb;
}

.button-row {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: 10px;
}

.button-row .pixel-btn {
  margin: 0;
}

@media (max-width: 640px) {
  .screen {
    align-items: flex-start;
    overflow-y: auto;
    padding: 12px;
  }

  .menu-panel,
  .panel {
    width: min(420px, 94vw);
    padding: 16px;
  }

  .game-title {
    font-size: clamp(38px, 15vw, 62px);
  }

  .pixel-btn {
    min-height: 52px;
  }

  .upgrade-card {
    grid-template-columns: 44px 1fr;
  }

  .upgrade-buy {
    grid-column: 1 / -1;
  }

  .button-row {
    grid-template-columns: 1fr;
  }
}

@media (max-width: 520px), (max-height: 520px) {
  .touch-stick {
    width: 118px;
    height: 118px;
  }

  .touch-stick-base {
    width: 98px;
    height: 98px;
  }

  .touch-stick-knob {
    width: 48px;
    height: 48px;
    margin: -24px 0 0 -24px;
  }

  .touch-fire {
    width: 82px;
    height: 82px;
  }

  .touch-reload {
    bottom: calc(max(20px, env(safe-area-inset-bottom)) + 92px);
  }

  .touch-dash {
    left: max(18px, env(safe-area-inset-left));
    bottom: calc(max(20px, env(safe-area-inset-bottom)) + 122px);
  }
}

@media (hover: hover) and (pointer: fine) {
  body.game-active .touch-controls {
    display: none;
  }
}
