* {
  box-sizing: border-box;
}

:root {
  --ink: #070907;
  --panel: #182014;
  --panel-2: #28351e;
  --cream: #f3e7bd;
  --muted: #b9c99c;
  --gold: #ffd272;
  --red: #d84328;
  --green: #91bd59;
}

html,
body {
  margin: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: #000;
  color: #fff;
  font-family: "Courier New", "Noto Sans Mono CJK SC", monospace;
  touch-action: none;
  user-select: none;
}

body {
  display: block;
}

button {
  min-width: 120px;
  min-height: 44px;
  padding: 12px 24px;
  border: 3px solid var(--gold);
  background: #9b2d20;
  box-shadow: 0 5px 0 #3b110d, 0 0 0 2px #000;
  color: #fff;
  font: 900 16px/1 "Courier New", monospace;
  text-shadow: 2px 2px #000;
  cursor: pointer;
}

button:focus-visible {
  outline: 3px solid #fff;
  outline-offset: 3px;
}

button:disabled {
  filter: grayscale(1);
  opacity: .48;
  cursor: wait;
}

button:active:not(:disabled) {
  transform: translateY(3px);
  box-shadow: 0 2px 0 #3b110d;
}

.hidden {
  display: none !important;
}

#shell {
  position: fixed;
  inset: 0;
  display: block;
  padding: 0;
  background: #000;
}

#game-frame {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  overflow: hidden;
  border: 0;
  background: #162514;
  box-shadow: none;
}

#game-canvas {
  display: block;
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
}

#flash {
  position: absolute;
  inset: 0;
  pointer-events: none;
  opacity: 0;
  background: #fff;
  mix-blend-mode: screen;
}

.flash-bomb {
  animation: bombFlash .25s steps(3);
}

@keyframes bombFlash {
  0% { opacity: .9; background: #fff; }
  45% { opacity: .65; background: #ff6a00; }
  100% { opacity: 0; }
}

.screen {
  position: absolute;
  inset: 0;
  z-index: 20;
  display: grid;
  place-items: center;
  overflow: hidden;
  background:
    linear-gradient(90deg, transparent 49.5%, rgba(255, 210, 114, .04) 50%, transparent 50.5%),
    radial-gradient(circle at center, rgba(20, 45, 13, .2), rgba(0, 0, 0, .88));
  pointer-events: auto;
}

.panel {
  max-width: 94%;
  padding: clamp(10px, 1.7vw, 18px);
  border: 4px double #e9db99;
  background: linear-gradient(135deg, rgba(36, 47, 27, .98), rgba(12, 16, 10, .98));
  box-shadow: 0 0 0 3px #000, 9px 9px 0 rgba(0, 0, 0, .55);
  text-align: center;
}

.title-panel {
  width: min(900px, 94%);
  height: min(504px, 94%);
  display: grid;
  grid-template-rows: auto auto 1fr auto;
  gap: 8px;
  padding: 12px 18px;
}

.title-lockup {
  display: grid;
  grid-template-columns: 1fr auto;
  align-items: end;
  gap: 0 16px;
  text-align: left;
}

.eyebrow {
  margin: 0 0 4px;
  color: var(--gold);
  font-size: clamp(8px, 1.25vw, 12px);
  letter-spacing: .18em;
}

.title-lockup .eyebrow {
  grid-column: 1 / -1;
}

h1,
h2 {
  margin: 0;
  line-height: .9;
  text-shadow: 4px 4px #111, 0 0 12px #d34124;
}

h1 {
  font-size: clamp(24px, 4.8vw, 48px);
  color: #f5efe0;
  letter-spacing: -.05em;
}

h1 span {
  color: #e04a2d;
}

h2 {
  margin-bottom: 12px;
  font-size: clamp(26px, 5vw, 52px);
}

#loading-text {
  margin: 0 0 4px;
  color: #c5d8a9;
  font-size: clamp(8px, 1.25vw, 12px);
  text-align: right;
  white-space: nowrap;
}

#stage-select {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 6px;
}

.stage-card {
  position: relative;
  min-width: 0;
  min-height: 55px;
  padding: 7px 9px 6px 54px;
  overflow: hidden;
  border: 2px solid #697852;
  background: linear-gradient(135deg, #2a351f, #182013);
  box-shadow: inset 0 0 0 1px #10140c, 0 3px 0 #090b08;
  color: var(--cream);
  text-align: left;
  text-shadow: 1px 1px #000;
}

.stage-card::after {
  content: "";
  position: absolute;
  inset: 0;
  border-left: 4px solid transparent;
  pointer-events: none;
}

.stage-card:hover {
  border-color: #b7c97f;
  background: linear-gradient(135deg, #354527, #1f2918);
}

.stage-card:disabled {
  cursor: not-allowed;
  border-color: #3c4333;
  background: linear-gradient(135deg, #171b15, #0c0f0b);
  box-shadow: inset 0 0 0 1px #090b08, 0 3px 0 #050605;
  color: #7b826f;
  filter: saturate(.35);
  opacity: .72;
}

.stage-card:disabled .stage-order,
.stage-card:disabled strong,
.stage-card:disabled small {
  color: #7b826f;
}

.stage-card:disabled::before {
  content: "LOCK";
  position: absolute;
  right: 6px;
  top: 5px;
  color: #75685b;
  font-size: 7px;
  letter-spacing: .08em;
}

.stage-card.selected {
  border-color: var(--gold);
  background: linear-gradient(135deg, #6d2d1e, #2d2417 58%, #192112);
  box-shadow: inset 0 0 14px rgba(255, 202, 91, .16), 0 3px 0 #2c100b, 0 0 0 1px #000;
}

.stage-card.selected::after {
  border-left-color: var(--red);
}

.stage-order {
  position: absolute;
  left: 8px;
  top: 50%;
  transform: translateY(-50%);
  color: var(--gold);
  font-size: 12px;
  letter-spacing: .05em;
}

.stage-card strong,
.stage-card small {
  display: block;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.stage-card strong {
  font-size: clamp(10px, 1.45vw, 14px);
  color: #fff7da;
}

.stage-card small {
  margin-top: 3px;
  color: #aeba99;
  font-size: clamp(8px, 1.05vw, 10px);
}

.stage-card.selected small {
  color: #f0d89e;
}

#stage-summary {
  min-height: 0;
  padding: 8px 10px;
  border: 1px solid #65734c;
  border-left: 4px solid var(--red);
  background: rgba(6, 9, 5, .64);
  text-align: left;
}

.summary-heading {
  display: flex;
  align-items: baseline;
  gap: 10px;
  margin-bottom: 4px;
}

#selected-stage-checkpoint {
  flex: 1;
  color: var(--gold);
  font-size: clamp(10px, 1.4vw, 13px);
  font-weight: 900;
  text-transform: uppercase;
}

#selected-stage-name {
  color: #fff;
  font-size: clamp(10px, 1.4vw, 13px);
  text-transform: uppercase;
}

#selected-stage-briefing {
  margin: 0 0 5px;
  color: #d6dfc5;
  font-size: clamp(8px, 1.16vw, 11px);
  line-height: 1.28;
}

.summary-orders {
  display: grid;
  grid-template-columns: 66px 1fr;
  gap: 8px;
  font-size: clamp(8px, 1.08vw, 10px);
  line-height: 1.28;
}

.summary-orders span {
  color: var(--green);
  font-weight: 900;
}

.summary-orders b {
  min-width: 0;
  color: #fff0c5;
}

.boss-order {
  margin-top: 2px;
}

.boss-order span {
  color: #ff8065;
}

.deploy-row {
  display: grid;
  grid-template-columns: minmax(190px, 1fr) auto;
  align-items: center;
  gap: 18px;
}

#start-button {
  width: 100%;
  min-height: 50px;
  font-size: 20px;
  letter-spacing: .18em;
}

.controls-copy {
  margin: 0;
  color: #aebb9a;
  font-size: clamp(7px, 1.02vw, 9px);
  line-height: 1.35;
  text-align: right;
}

#hud {
  position: absolute;
  inset: 0;
  z-index: 8;
  color: #fff;
  font-size: clamp(8px, 1.6vw, 15px);
  font-weight: 900;
  text-shadow: 2px 2px #000, -1px -1px #000;
  pointer-events: none;
}

#hud-left {
  position: absolute;
  left: 1.4%;
  top: 2.2%;
  line-height: 1.35;
}

.jeep-icon {
  display: inline-block;
  color: #75b84b;
  transform: rotate(-90deg);
  text-shadow: 1px 1px #000;
}

#timer {
  position: absolute;
  left: 50%;
  top: 1.5%;
  transform: translateX(-50%);
  font-size: clamp(14px, 3vw, 26px);
  letter-spacing: .08em;
}

#hud-mission {
  position: absolute;
  left: 50%;
  top: 8%;
  width: min(56%, 500px);
  transform: translateX(-50%);
  padding: 3px 8px 4px;
  border-top: 1px solid rgba(255, 210, 114, .6);
  background: linear-gradient(90deg, transparent, rgba(0, 0, 0, .6) 18%, rgba(0, 0, 0, .6) 82%, transparent);
  text-align: center;
}

#hud-stage {
  color: var(--gold);
  font-size: clamp(8px, 1.25vw, 12px);
  letter-spacing: .08em;
}

#hud-objective {
  overflow: hidden;
  color: #fff;
  font-size: clamp(7px, 1.12vw, 11px);
  text-overflow: ellipsis;
  white-space: nowrap;
}

#hud-objective-label {
  color: #dce9c6;
}

#hud-objective-progress {
  margin-left: 5px;
  color: #ffda72;
}

#hud-objective-detail {
  overflow: hidden;
  color: #aecd9c;
  font-size: clamp(6px, .98vw, 9px);
  text-overflow: ellipsis;
  white-space: nowrap;
}

#score {
  position: absolute;
  right: 1.5%;
  top: 2%;
  color: #ffefbe;
  text-align: right;
}

#score b {
  color: #fff;
}

#stats {
  position: absolute;
  left: 1.4%;
  top: 25%;
  font-size: clamp(7px, 1.25vw, 12px);
  line-height: 1.4;
}

#stats b {
  color: #d6ef9e;
}

#bomb-count {
  position: absolute;
  left: 50%;
  bottom: 2%;
  transform: translateX(-50%);
  color: var(--gold);
  font-size: clamp(13px, 2.4vw, 22px);
}

#boss-warning {
  position: absolute;
  left: 50%;
  top: 22%;
  max-width: 86%;
  transform: translateX(-50%);
  overflow: hidden;
  padding: 5px 10px;
  border: 2px solid #ffcc66;
  background: rgba(80, 20, 13, .86);
  color: #ffb33d;
  text-align: center;
  text-overflow: ellipsis;
  white-space: nowrap;
  animation: warning .45s steps(2) infinite;
}

@keyframes warning {
  50% { color: #fff; background: #8b2015; }
}

.upgrade-panel {
  width: 92%;
}

#upgrade-cards {
  display: grid;
  grid-template-columns: repeat(3, 1fr);
  gap: clamp(5px, 1.5vw, 14px);
}

.upgrade-card {
  min-width: 0;
  min-height: 100px;
  padding: clamp(7px, 1.5vw, 15px);
  border-color: #a9cc65;
  background: var(--panel-2);
  font-size: clamp(9px, 1.5vw, 14px);
}

.upgrade-card strong,
.upgrade-card span {
  display: block;
}

.upgrade-card strong {
  margin-bottom: 7px;
  color: #ffdc79;
}

.upgrade-card span {
  font-size: .82em;
  line-height: 1.2;
}

.result-panel {
  width: min(560px, 90%);
}

#result-stats {
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1.5fr);
  gap: 5px 14px;
  max-height: 285px;
  margin: 12px auto 16px;
  overflow: hidden;
  color: #dbe8c1;
  font-size: clamp(9px, 1.45vw, 13px);
  text-align: left;
}

#result-stats b {
  overflow: hidden;
  color: #fff;
  text-align: right;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#result-actions {
  display: grid;
  grid-template-columns: 1.35fr 1fr;
  gap: 12px;
}

#result-actions button {
  min-width: 0;
  padding-inline: 10px;
  font-size: clamp(10px, 1.55vw, 15px);
}

#continue-button {
  border-color: #b8cf82;
  background: #304522;
  box-shadow: 0 5px 0 #15200f, 0 0 0 2px #000;
}

#touch-controls {
  position: absolute;
  left: max(8px, env(safe-area-inset-left));
  right: max(8px, env(safe-area-inset-right));
  bottom: max(8px, env(safe-area-inset-bottom));
  z-index: 30;
  height: 96px;
  display: flex;
  align-items: flex-end;
  justify-content: space-between;
  pointer-events: none;
}

#joystick,
#bomb-button {
  pointer-events: auto;
}

#joystick {
  position: relative;
  width: 88px;
  height: 88px;
  border: 3px solid rgba(183, 199, 160, .5);
  border-radius: 50%;
  background: rgba(52, 69, 45, .44);
  box-shadow: inset 0 0 18px #000;
}

#joystick-knob {
  position: absolute;
  left: 20px;
  top: 20px;
  width: 42px;
  height: 42px;
  border: 3px solid #e5edcf;
  border-radius: 50%;
  background: rgba(106, 138, 82, .8);
  box-shadow: 0 3px 7px #000;
}

#bomb-button {
  width: 78px;
  height: 78px;
  min-width: 78px;
  min-height: 78px;
  padding: 0;
  border-radius: 50%;
  background: #9e271b;
  font-size: 14px;
}

#bomb-button span {
  color: var(--gold);
  font-size: 24px;
}

@media (pointer: fine) {
  #touch-controls { display: none; }
}

@media (max-width: 620px) {
  .title-panel {
    gap: 3px;
    padding: 6px 8px;
  }

  .title-lockup {
    gap: 2px 6px;
  }

  h1 {
    font-size: clamp(20px, 8vw, 30px);
  }

  #stage-select {
    grid-template-columns: repeat(2, minmax(0, 1fr));
    gap: 3px;
  }

  .stage-card {
    min-height: 44px;
    padding: 4px 5px 4px 39px;
  }

  .stage-order {
    left: 5px;
    font-size: 9px;
  }

  .stage-card strong {
    font-size: 9px;
  }

  .stage-card small {
    margin-top: 2px;
    font-size: 7px;
  }

  #stage-summary {
    padding: 4px 6px;
  }

  .summary-heading {
    margin-bottom: 2px;
  }

  #selected-stage-briefing {
    margin-bottom: 2px;
    line-height: 1.12;
  }

  .summary-orders {
    grid-template-columns: 50px 1fr;
    gap: 5px;
    line-height: 1.12;
  }

  .deploy-row {
    grid-template-columns: 1fr auto;
    gap: 7px;
  }

  #start-button {
    min-height: 40px;
    padding-block: 8px;
    font-size: 16px;
  }

  .controls-copy {
    font-size: 6px;
  }
}

@media (max-height: 480px) {
  .title-panel {
    height: 96%;
    gap: 4px;
    padding-block: 7px;
  }

  h1 { font-size: clamp(20px, 4.2vw, 38px); }
  .stage-card { min-height: 46px; }
  #selected-stage-briefing { line-height: 1.12; }
  #start-button { min-height: 40px; padding-block: 8px; }
  #result-stats { gap: 3px 12px; margin-block: 7px 10px; }
  #touch-controls { height: 72px; }
  #joystick { width: 66px; height: 66px; }
  #joystick-knob { width: 34px; height: 34px; left: 13px; top: 13px; }
  #bomb-button { width: 62px; height: 62px; min-width: 62px; min-height: 62px; font-size: 11px; }
  #bomb-button span { font-size: 17px; }
}

@media (orientation: landscape) and (max-height: 600px) {
  #touch-controls {
    height: 100%;
    align-items: center;
  }
}

@media (orientation: portrait) {
  .screen {
    align-items: center;
  }

  .title-panel {
    height: min(96%, 520px);
  }
}
