:root {
  color-scheme: dark;
  --bg: #101711;
  --panel: #182118;
  --wall: #335f39;
  --wall-edge: #69a25a;
  --track: #263726;
  --text: #f4f7d8;
  --muted: #aab98f;
  --accent: #e6c04a;
  --danger: #de5b45;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  background: radial-gradient(circle at top, #26331d 0, var(--bg) 48%, #070a08 100%);
  color: var(--text);
  font-family: Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", sans-serif;
  overflow: hidden;
}

body.show-touch-controls {
  overflow: hidden;
}

button {
  border: 0;
  border-radius: 8px;
  background: var(--accent);
  color: #201806;
  cursor: pointer;
  font: inherit;
  font-weight: 800;
}

.shell {
  display: grid;
  grid-template-rows: minmax(0, 1fr) auto;
  gap: 12px;
  width: min(100vw, 1180px);
  height: 100%;
  margin: 0 auto;
  padding: 12px;
}

.game-panel {
  position: relative;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
  min-height: 0;
  overflow: hidden;
  border: 1px solid rgba(244, 247, 216, 0.14);
  border-radius: 8px;
  background: rgba(16, 23, 17, 0.86);
  box-shadow: 0 18px 60px rgba(0, 0, 0, 0.35);
}

.hud {
  display: grid;
  grid-template-columns: repeat(4, minmax(72px, max-content));
  gap: 8px 12px;
  align-items: center;
  padding: 10px 12px;
  background: rgba(7, 10, 8, 0.62);
  border-bottom: 1px solid rgba(244, 247, 216, 0.1);
}

.hud > div {
  display: grid;
  gap: 3px;
}

.label {
  color: var(--muted);
  font-size: 0.72rem;
  font-weight: 700;
  text-transform: uppercase;
}

.hud strong {
  font-variant-numeric: tabular-nums;
  line-height: 1;
}

.pollution {
  grid-column: 1 / -1;
  display: grid;
  grid-template-columns: auto minmax(80px, 1fr) auto;
  gap: 10px;
  align-items: center;
  padding-top: 4px;
}

.pollution .label {
  grid-column: auto;
}

.pollution-bar {
  display: grid;
  grid-template-columns: repeat(50, minmax(0, 1fr));
  gap: 3px;
  width: 100%;
  min-width: 0;
  height: 18px;
  padding: 3px;
  border: 1px solid rgba(244, 247, 216, 0.2);
  border-radius: 5px;
  background: rgba(7, 10, 8, 0.45);
}

.pollution-block {
  min-width: 0;
  border-radius: 2px;
  background: rgba(244, 247, 216, 0.12);
}

.pollution-block.active.safe {
  background: #58bf59;
}

.pollution-block.active.warn {
  background: #e1c34a;
}

.pollution-block.active.danger {
  background: #d84c3e;
}

canvas {
  display: block;
  width: 100%;
  height: auto;
  align-self: center;
  aspect-ratio: 29 / 19;
  min-height: 0;
  max-height: 100%;
  background: var(--track);
  image-rendering: pixelated;
  touch-action: none;
}

.overlay {
  position: absolute;
  inset: 0;
  display: grid;
  place-content: center;
  justify-items: center;
  gap: 12px;
  padding: 24px;
  text-align: center;
  background: rgba(7, 10, 8, 0.78);
}

.overlay.hidden {
  display: none;
}

.overlay h1 {
  margin: 0;
  color: var(--accent);
  font-size: clamp(2.3rem, 7vw, 5.6rem);
  line-height: 0.95;
}

.overlay p {
  max-width: 34rem;
  margin: 0;
  color: var(--text);
}

.overlay button {
  min-width: 140px;
  padding: 12px 22px;
}

.touch-pad {
  display: none;
  grid-template-columns: repeat(3, 58px);
  grid-template-rows: repeat(2, 52px);
  justify-content: center;
  gap: 8px;
  padding-bottom: max(0px, env(safe-area-inset-bottom));
}

body.show-touch-controls .touch-pad {
  display: flex;
}

body.show-touch-controls .shell {
  height: 100dvh;
}

.joystick {
  display: grid;
  place-items: center;
  width: 144px;
  height: 144px;
  touch-action: none;
  user-select: none;
}

.joystick-ring {
  position: relative;
  width: 132px;
  height: 132px;
  border: 2px solid rgba(244, 247, 216, 0.28);
  border-radius: 999px;
  background:
    radial-gradient(circle at center, rgba(230, 192, 74, 0.2) 0 28%, transparent 29%),
    rgba(7, 10, 8, 0.54);
  box-shadow: inset 0 0 24px rgba(0, 0, 0, 0.42), 0 10px 30px rgba(0, 0, 0, 0.25);
}

.joystick-ring::before,
.joystick-ring::after {
  content: "";
  position: absolute;
  left: 50%;
  top: 50%;
  background: rgba(244, 247, 216, 0.16);
  transform: translate(-50%, -50%);
}

.joystick-ring::before {
  width: 84px;
  height: 2px;
}

.joystick-ring::after {
  width: 2px;
  height: 84px;
}

.joystick-knob {
  position: absolute;
  left: 50%;
  top: 50%;
  width: 54px;
  height: 54px;
  border-radius: 999px;
  background: linear-gradient(145deg, #f3d469, #c99929);
  box-shadow: 0 8px 18px rgba(0, 0, 0, 0.35), inset 0 2px 4px rgba(255, 255, 255, 0.42);
  transform: translate(-50%, -50%);
}

.joystick-hint {
  position: absolute;
  color: rgba(244, 247, 216, 0.52);
  font-size: 0.78rem;
  font-weight: 900;
  line-height: 1;
}

.joystick-hint.up {
  left: 50%;
  top: 11px;
  transform: translateX(-50%);
}

.joystick-hint.right {
  right: 12px;
  top: 50%;
  transform: translateY(-50%);
}

.joystick-hint.down {
  bottom: 11px;
  left: 50%;
  transform: translateX(-50%);
}

.joystick-hint.left {
  left: 12px;
  top: 50%;
  transform: translateY(-50%);
}

@media (hover: none), (max-width: 760px) {
  body {
    overflow: hidden;
  }

  .shell {
    height: 100dvh;
    padding: 6px;
    gap: 6px;
  }

  .game-panel {
    min-height: 0;
  }

  .hud {
    grid-template-columns: repeat(4, 1fr);
    gap: 6px;
  }

  .pollution {
    grid-column: 1 / -1;
  }

  .touch-pad {
    display: flex;
    align-items: center;
    justify-content: center;
    min-height: 148px;
    padding-bottom: max(4px, env(safe-area-inset-bottom));
  }
}

@media (hover: none) and (orientation: portrait), (max-width: 760px) and (orientation: portrait) {
  .shell {
    grid-template-rows: minmax(0, 1fr) 154px;
  }

  .hud {
    padding: 5px 7px;
  }

  .label {
    font-size: 0.62rem;
  }

  .hud strong {
    font-size: 0.9rem;
  }

  .pollution {
    grid-template-columns: auto minmax(0, 1fr) auto;
    gap: 8px;
    padding-top: 3px;
  }

  .pollution-bar {
    height: 16px;
    gap: 2px;
  }
}
