* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; background: #08080f; overflow: hidden; overscroll-behavior: none; }
body { height: 100dvh; display: grid; grid-template: minmax(0, 1fr) / minmax(0, 1fr); touch-action: none;
  user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; -webkit-tap-highlight-color: transparent; }
#stage { display: flex; align-items: center; justify-content: center; min-width: 0; min-height: 0; overflow: hidden; }
#frame { position: relative; flex: none; line-height: 0; }
#screen { display: block; image-rendering: crisp-edges; image-rendering: pixelated; outline: none; touch-action: none; }
#scanlines { position: absolute; inset: 0; pointer-events: none;
  background: repeating-linear-gradient(to bottom, transparent 0, transparent calc(var(--px) * .6), rgba(0,0,0,.28) calc(var(--px) * .6), rgba(0,0,0,.28) var(--px)); }
#scanlines[hidden] { display: none; }

/* On-screen controls. Keep the first condition in step with TOUCH_UI in game.js. */
.pad { display: none; }
/* The visible button is a pixel-art canvas drawn by drawPad() in game.js. */
.pad button { position: relative; min-width: 0; padding: 0; border: 0; border-radius: 0; background: none; appearance: none; touch-action: none; }
.pad button canvas { position: absolute; display: block; pointer-events: none; image-rendering: crisp-edges; image-rendering: pixelated; }
.pad button.small { height: 48px; width: 108px; max-width: 100%; }
.pad button:focus-visible { outline: 3px solid #f8f8f8; outline-offset: 2px; }

@media (max-width: 1024px), (pointer: coarse) {
  .pad { --key: clamp(64px, 11vmin, 104px); display: flex; flex-direction: column; gap: 14px; }
  .main-keys { display: flex; gap: 10px; }
  .main-keys button { flex: 1 1 0; height: var(--key); max-width: 150px; }

  /* Portrait: controls sit below the game, one cluster per thumb. */
  body { grid-template: "stage stage" minmax(0, 1fr) "left right" auto / 1fr 1fr; }
  #stage { grid-area: stage; }
  .pad-left { grid-area: left; align-items: flex-start;
    padding: 14px 8px calc(18px + env(safe-area-inset-bottom)) calc(12px + env(safe-area-inset-left)); }
  .pad-right { grid-area: right; align-items: flex-end;
    padding: 14px calc(12px + env(safe-area-inset-right)) calc(18px + env(safe-area-inset-bottom)) 8px; }
  .main-keys { width: 100%; }
  .pad-right .main-keys { justify-content: flex-end; }
}

/* Landscape: controls flank the game so it keeps the full height. */
@media (max-width: 1024px) and (orientation: landscape), (pointer: coarse) and (orientation: landscape) {
  body { grid-template: "left stage right" minmax(0, 1fr) / auto minmax(0, 1fr) auto; }
  /* Use the space a full-height game leaves free (256/224 = 114.3%), within thumb-friendly limits. */
  .pad { width: clamp(132px + var(--edge), (100vw - 114.3dvh) / 2, 220px + var(--edge)); justify-content: space-between; }
  .pad-left { --edge: env(safe-area-inset-left, 0px);
    padding: 12px 10px calc(18px + env(safe-area-inset-bottom)) calc(10px + var(--edge)); }
  .pad-right { --edge: env(safe-area-inset-right, 0px);
    padding: 12px calc(10px + var(--edge)) calc(18px + env(safe-area-inset-bottom)) 10px; }
  .pad-right .main-keys { flex-direction: column; gap: 14px; }
  .pad-right .main-keys button { flex: none; width: 100%; max-width: none; }
}
