:root {
  --bg: #0b0b10;
  --bezel: #17171f;
  --bezel-edge: #2a2a36;
  --text: #e8e8f0;
  --muted: #8a8aa0;
  --key: #23232e;
  --key-edge: #3a3a4a;
  --accent: #e45c10;
  --font: "Press Start 2P", ui-monospace, monospace;
}

*,
*::before,
*::after {
  box-sizing: border-box;
}

html,
body {
  height: 100%;
  margin: 0;
}

body {
  display: grid;
  place-items: center;
  overflow: hidden;
  background: radial-gradient(ellipse at 50% 30%, #1b1b28 0%, var(--bg) 70%);
  color: var(--text);
  font-family: var(--font);
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
}

.stage {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 14px;
  padding: 16px;
}

/* ── Screen with bezel & faint scanlines ───────────────────────── */

.screen {
  position: relative;
  padding: 6px;
  border-radius: 10px;
  background: var(--bezel);
  box-shadow:
    0 0 0 1px var(--bezel-edge),
    0 24px 60px rgba(0, 0, 0, 0.7);
}

.screen::after {
  content: "";
  position: absolute;
  inset: 6px;
  pointer-events: none;
  background: repeating-linear-gradient(to bottom, rgba(0, 0, 0, 0.08) 0 1px, transparent 1px 3px);
}

canvas {
  display: block;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  background: #000;
  /* Taps on the screen advance dialogs, so a double tap must not zoom the page. */
  touch-action: manipulation;
}

/* ── Keyboard legend ───────────────────────────────────────────── */

.legend {
  display: flex;
  flex-wrap: wrap;
  justify-content: center;
  gap: 8px 18px;
  margin: 0;
  padding: 0;
  list-style: none;
  color: var(--muted);
  font-size: 9px;
  line-height: 1;
}

.legend li {
  display: flex;
  align-items: center;
  gap: 4px;
}

kbd {
  padding: 4px 5px 3px;
  border: 1px solid var(--key-edge);
  border-bottom-width: 3px;
  border-radius: 4px;
  background: var(--key);
  color: var(--text);
  font-family: var(--font);
  font-size: 8px;
}

/* ── Touch controls (phones & tablets only) ───────────────────── */

.touch {
  display: none;
}

@media (pointer: coarse) {
  .legend {
    display: none;
  }

  .touch {
    display: flex;
    align-items: center;
    justify-content: space-between;
    width: min(100vw - 32px, 520px);
    padding-bottom: env(safe-area-inset-bottom);
  }
}

/* Landscape: game in the middle, D-pad left, A/B right. The stage's side padding makes room. */
@media (pointer: coarse) and (orientation: landscape) {
  .stage {
    --edge: max(12px, env(safe-area-inset-left), env(safe-area-inset-right));
    --controls: 168px;
    padding: 8px calc(var(--edge) + var(--controls));
  }

  .touch {
    position: fixed;
    inset: 0;
    width: auto;
    padding: 0 var(--edge);
    pointer-events: none;
  }

  .touch > * {
    pointer-events: auto;
  }

  .touch__btn--start {
    position: absolute;
    top: max(12px, env(safe-area-inset-top));
    right: calc(var(--edge) + (var(--controls) - 52px) / 2);
  }
}

/* Upright phones: ask the player to turn the phone sideways. */
.rotate {
  display: none;
}

@media (pointer: coarse) and (orientation: portrait) and (max-width: 600px) {
  .rotate {
    position: fixed;
    inset: 0;
    z-index: 10;
    display: grid;
    place-content: center;
    justify-items: center;
    gap: 28px;
    background: var(--bg);
    color: var(--text);
    font-size: 12px;
    text-align: center;
  }

  .rotate p {
    margin: 0;
  }

  .rotate__phone {
    width: 44px;
    height: 76px;
    border: 4px solid var(--text);
    border-radius: 8px;
    animation: rotate-hint 2.4s ease-in-out infinite;
  }
}

@keyframes rotate-hint {
  0%, 25% { transform: rotate(0deg); }
  50%, 75% { transform: rotate(-90deg); }
  100% { transform: rotate(0deg); }
}

@media (prefers-reduced-motion: reduce) {
  .rotate__phone {
    animation: none;
    transform: rotate(-90deg);
  }
}

.touch__btn {
  display: grid;
  place-items: center;
  border: 1px solid var(--key-edge);
  border-bottom-width: 4px;
  border-radius: 10px;
  background: var(--key);
  color: var(--text);
  font-family: var(--font);
  font-size: 14px;
  touch-action: none;
}

.touch__btn.is-pressed {
  border-bottom-width: 1px;
  transform: translateY(3px);
  filter: brightness(1.3);
}

.touch__dpad {
  display: grid;
  grid-template-areas:
    ". up ."
    "left . right"
    ". down .";
  grid-template-columns: repeat(3, 44px);
  grid-template-rows: repeat(3, 44px);
  gap: 4px;
}

.touch__btn--up { grid-area: up; }
.touch__btn--left { grid-area: left; }
.touch__btn--right { grid-area: right; }
.touch__btn--down { grid-area: down; }

.touch__btn--start {
  width: 52px;
  height: 28px;
  border-radius: 14px;
  font-size: 10px;
}

.touch__actions {
  display: flex;
  gap: 14px;
  transform: rotate(-12deg);
}

.touch__btn--round {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  font-size: 16px;
}

.touch__btn--a {
  margin-top: -24px;
  background: var(--accent);
  border-color: #b84a0c;
}
