html, body {
  margin: 0;
  height: 100%;
  background: #0a1030;
  overflow: hidden;
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
}
#wrap {
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
}
canvas {
  width: min(100vw, calc(100vh * 4 / 3));
  height: min(100vh, calc(100vw * 3 / 4));
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  background: #000;
  box-shadow: 0 0 40px rgba(160, 210, 255, 0.35);
}

#touch { display: none; }
@media (pointer: coarse) {
  #touch { display: block; }
}
#touch button {
  position: fixed;
  width: 64px;
  height: 64px;
  border-radius: 14px;
  border: 3px solid rgba(255, 255, 255, 0.7);
  background: rgba(20, 30, 60, 0.55);
  color: #fff;
  font-size: 24px;
  font-family: "Press Start 2P", monospace;
  touch-action: none;
}
#touch button.on { background: rgba(200, 32, 42, 0.7); }
.dpad .up    { left: 84px;  bottom: 156px; }
.dpad .left  { left: 16px;  bottom: 88px; }
.dpad .right { left: 152px; bottom: 88px; }
.dpad .down  { left: 84px;  bottom: 20px; }
.ab .a { right: 20px;  bottom: 70px; width: 80px; height: 80px; border-radius: 50%; background: rgba(46, 139, 62, 0.7); }
.ab .b { right: 116px; bottom: 30px; border-radius: 50%; }

/* ---------- Phones / tablets only ---------- */
@media (pointer: coarse) {
  html, body {
    /* Use the real visible height (iOS 100vh includes hidden areas). */
    height: 100dvh;
    position: fixed;
    inset: 0;
  }
  :root {
    --sat: env(safe-area-inset-top, 0px);
    --sab: env(safe-area-inset-bottom, 0px);
    --sal: env(safe-area-inset-left, 0px);
    --sar: env(safe-area-inset-right, 0px);
    --btn: 60px;
    --gap: 6px;
  }
  #wrap {
    /* Keep the game inside the notch / home-indicator safe area. */
    top: var(--sat);
    bottom: var(--sab);
    left: var(--sal);
    right: var(--sar);
  }
  canvas {
    --aw: calc(100vw - var(--sal) - var(--sar));
    --ah: calc(100dvh - var(--sat) - var(--sab));
    width: min(var(--aw), calc(var(--ah) * 4 / 3));
    height: min(var(--ah), calc(var(--aw) * 3 / 4));
    box-shadow: none;
  }

  /* Buttons: more see-through, brighten while pressed. */
  #touch button {
    width: var(--btn);
    height: var(--btn);
    font-size: 20px;
    border: 2px solid rgba(255, 255, 255, 0.35);
    background: rgba(20, 30, 60, 0.22);
    color: rgba(255, 255, 255, 0.6);
  }
  #touch button.on { background: rgba(200, 32, 42, 0.55); color: #fff; }
  .ab .a { width: calc(var(--btn) + 12px); height: calc(var(--btn) + 12px); background: rgba(46, 139, 62, 0.3); }

  .dpad .up    { left: calc(var(--sal) + 12px + var(--btn) + var(--gap));         bottom: calc(var(--sab) + 12px + 2 * (var(--btn) + var(--gap))); }
  .dpad .left  { left: calc(var(--sal) + 12px);                                    bottom: calc(var(--sab) + 12px + var(--btn) + var(--gap)); }
  .dpad .right { left: calc(var(--sal) + 12px + 2 * (var(--btn) + var(--gap)));   bottom: calc(var(--sab) + 12px + var(--btn) + var(--gap)); }
  .dpad .down  { left: calc(var(--sal) + 12px + var(--btn) + var(--gap));         bottom: calc(var(--sab) + 12px); }
  .ab .a { right: calc(var(--sar) + 16px); bottom: calc(var(--sab) + 12px + var(--btn) + 4px); }
  .ab .b { right: calc(var(--sar) + 16px + var(--btn) + 20px); bottom: calc(var(--sab) + 12px); }
}

/* Portrait phone: game sits at the top, controls go in the empty space below it. */
@media (pointer: coarse) and (orientation: portrait) {
  #wrap { align-items: flex-start; padding-top: 8px; }
  :root { --btn: 64px; }
}

/* Landscape phone: game uses the full height; controls may overlap its edges,
   so they're extra see-through until pressed. */
@media (pointer: coarse) and (orientation: landscape) {
  :root { --btn: 50px; --gap: 4px; }
  #touch button {
    font-size: 16px;
    border-color: rgba(255, 255, 255, 0.2);
    background: rgba(20, 30, 60, 0.1);
    color: rgba(255, 255, 255, 0.35);
  }
  #touch button.on { background: rgba(200, 32, 42, 0.5); color: #fff; }
}
