:root {
  color-scheme: dark;
  --ink: #f8fbff;
  --muted: #aebcd0;
  --panel: #151923;
  --edge: #323848;
  --accent: #55e7ff;
  --danger: #ff5978;
}

* {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
}

body {
  margin: 0;
  background: linear-gradient(135deg, #090b10, #151923 54%, #10131b);
  color: var(--ink);
  font-family: "Courier New", monospace;
  overflow: hidden;
}

button {
  font: inherit;
  touch-action: manipulation;
}

input {
  font: inherit;
}

.shell {
  min-height: 100%;
  display: grid;
  place-items: center;
  padding: 18px;
}

.game-panel {
  width: min(100%, 1080px, calc((100vh - 128px) * 16 / 9));
  display: grid;
  gap: 10px;
}

.hud {
  display: grid;
  grid-template-columns: repeat(4, minmax(0, 1fr)) auto;
  gap: 8px;
  align-items: stretch;
}

.hud > div,
.hud button {
  border: 2px solid var(--edge);
  background: rgba(21, 25, 35, .92);
  border-radius: 5px;
  box-shadow: inset 0 0 0 2px rgba(255, 255, 255, .04);
}

.hud > div {
  min-width: 0;
  padding: 8px 10px;
}

.hud span {
  display: block;
  color: var(--muted);
  font-size: clamp(10px, 1.6vw, 14px);
  line-height: 1.1;
  text-transform: uppercase;
}

.hud strong {
  display: block;
  margin-top: 2px;
  font-size: clamp(17px, 3.3vw, 28px);
  line-height: 1;
  letter-spacing: 0;
  white-space: nowrap;
}

.hud button {
  color: var(--ink);
  min-width: 116px;
  padding: 8px 12px;
  cursor: pointer;
}

#game {
  display: block;
  width: 100%;
  aspect-ratio: 16 / 9;
  image-rendering: pixelated;
  border: 2px solid #596274;
  border-radius: 5px;
  background: #080a10;
  box-shadow: 0 18px 38px rgba(0, 0, 0, .38);
}

.touch-controls {
  display: none;
  gap: 10px;
  user-select: none;
  -webkit-user-select: none;
}

.touch-menu,
.touch-dpad,
.touch-actions {
  display: grid;
  gap: 8px;
}

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

.touch-dpad {
  grid-template-columns: repeat(2, minmax(0, 1fr));
  flex: 1 1 auto;
}

.touch-actions {
  grid-template-columns: repeat(2, minmax(0, 1fr));
  flex: 1 1 auto;
}

.touch-controls button {
  min-height: 58px;
  color: var(--ink);
  background: rgba(21, 25, 35, .94);
  border: 2px solid var(--edge);
  border-radius: 5px;
  box-shadow: inset 0 0 0 2px rgba(255, 255, 255, .05);
  text-transform: uppercase;
}

.touch-controls button:active,
.touch-controls button.is-down {
  border-color: var(--accent);
  background: rgba(85, 231, 255, .18);
}

@media (max-width: 640px) {
  body {
    overflow: hidden;
  }

  .shell {
    min-height: 100dvh;
    align-content: start;
    padding: 6px;
  }

  .game-panel {
    width: 100%;
    min-height: calc(100dvh - 12px);
    align-content: start;
    grid-template-rows: auto auto auto;
    gap: 6px;
  }

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

  .hud > div {
    padding: 5px 6px;
  }

  .hud span {
    font-size: 9px;
  }

  .hud strong {
    font-size: clamp(14px, 4.6vw, 20px);
  }

  .hud button {
    grid-column: 1 / -1;
    min-height: 30px;
    padding: 4px 8px;
  }

  .touch-controls {
    display: grid;
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
    grid-template-areas:
      "menu menu"
      "dpad actions";
    gap: 8px;
    padding-bottom: env(safe-area-inset-bottom);
  }

  .touch-menu {
    grid-area: menu;
  }

  .touch-dpad {
    grid-area: dpad;
  }

  .touch-actions {
    grid-area: actions;
  }

  .touch-menu button {
    min-height: 34px;
    font-size: 13px;
  }

  .touch-dpad button,
  .touch-actions button {
    min-height: clamp(62px, 17vw, 84px);
    font-size: clamp(14px, 4vw, 18px);
  }
}

@media (pointer: coarse) and (min-width: 641px) {
  .touch-controls {
    display: grid;
    grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
    align-items: end;
  }

  .touch-menu {
    min-width: 220px;
  }
}

@media (max-width: 920px) and (orientation: landscape) {
  body {
    overflow: hidden;
  }

  .shell {
    min-height: 100dvh;
    padding: 4px;
  }

  .game-panel {
    width: min(100%, calc((100dvh - 58px) * 16 / 9));
    min-height: auto;
    gap: 4px;
  }

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

  .hud > div {
    padding: 3px 5px;
  }

  .hud span {
    font-size: 8px;
  }

  .hud strong {
    font-size: 15px;
  }

  .hud button {
    grid-column: auto;
    min-width: 82px;
    min-height: 28px;
    padding: 3px 6px;
    font-size: 12px;
  }

  #game {
    box-shadow: 0 10px 24px rgba(0, 0, 0, .34);
  }

  .touch-controls {
    position: fixed;
    inset: auto 8px calc(8px + env(safe-area-inset-bottom)) 8px;
    display: grid;
    grid-template-columns: 168px 1fr 168px;
    grid-template-areas: "dpad . actions";
    align-items: end;
    pointer-events: none;
    z-index: 5;
  }

  .touch-menu {
    position: fixed;
    top: calc(8px + env(safe-area-inset-top));
    left: 8px;
    width: 108px;
    grid-template-columns: 1fr;
  }

  .touch-dpad {
    grid-area: dpad;
  }

  .touch-actions {
    grid-area: actions;
  }

  .touch-controls button {
    pointer-events: auto;
    background: rgba(21, 25, 35, .68);
  }

  .touch-menu button {
    min-height: 30px;
    font-size: 11px;
  }

  .touch-dpad button,
  .touch-actions button {
    min-height: 56px;
    font-size: 13px;
  }
}
