/* ==========================================================================
   Retro Arcade Collection — layout responsive + gamepad virtuale
   - Desktop (tastiera/mouse): solo il gioco, centrato e adattato alla finestra
   - Touch orizzontale: controlli ai lati del gioco (nello spazio "vuoto")
   - Touch verticale: gioco in alto, controlli sotto (zona dei pollici)
   ========================================================================== */

:root {
  --bg: #0a0a14;
  --panel: rgba(24, 24, 52, 0.78);
  --panel-on: rgba(60, 200, 255, 0.22);
  --edge: rgba(60, 200, 255, 0.45);
  --edge-on: #3cc8ff;
  --neon2: #8c32dc;
  --txt: #dcdcff;
  --sub: #9a9ab8;
  --vh: 1vh;
  /* larghezza delle colonne laterali in orizzontale: lo spazio libero accanto
     al gioco 4:3, ma abbastanza larghe per i pollici (fino a 150px) */
  --side: clamp(min(150px, 18vw), calc((100vw - 100vh * 4 / 3) / 2), 230px);
  --mini: 64px;
}
@supports (height: 100dvh) {
  :root {
    --vh: 1dvh;
    --side: clamp(min(150px, 18vw), calc((100vw - 100dvh * 4 / 3) / 2), 230px);
  }
}

* { margin: 0; padding: 0; box-sizing: border-box; }

html, body {
  width: 100%;
  height: 100%;
  background: var(--bg);
  overflow: hidden;
  overscroll-behavior: none;
  color: var(--txt);
  font-family: ui-monospace, 'Cascadia Mono', Consolas, 'Courier New', monospace;
}

#app {
  position: fixed;
  inset: 0;
  display: grid;
  grid-template-areas: "left stage right";
  grid-template-columns: 0 minmax(0, 1fr) 0;
  grid-template-rows: minmax(0, 1fr);
  /* rispetta notch e barre di sistema (viewport-fit=cover) */
  padding: env(safe-area-inset-top) env(safe-area-inset-right)
           env(safe-area-inset-bottom) env(safe-area-inset-left);
  touch-action: none;
  user-select: none;
  -webkit-user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}

#stage { grid-area: stage; position: relative; min-width: 0; min-height: 0; }
#game  { position: absolute; inset: 0; overflow: hidden; touch-action: none; }
#game canvas { display: block; touch-action: none; image-rendering: pixelated; }

/* ─── Colonne del pad ───────────────────────────────────────────────────── */

.pad {
  display: none;
  min-width: 0;
  min-height: 0;
  flex-direction: column;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 10px 6px;
}
.pad-left  { grid-area: left; }
.pad-right { grid-area: right; }

#app.pad-full .pad,
#app.pad-mini .pad { display: flex; }

#app.pad-full { grid-template-columns: var(--side) minmax(0, 1fr) var(--side); }
#app.pad-mini { grid-template-columns: var(--mini) minmax(0, 1fr) var(--mini); }

.pad-row {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  align-items: center;
}
.pad-row:empty { display: none; }
#app.pad-mini .pad-row { flex-direction: column; }
#app.pad-mini .pad { justify-content: center; }

/* ─── Verticale: gioco sopra, controlli sotto ──────────────────────────── */

@media (orientation: portrait) {
  #app.pad-full,
  #app.pad-mini {
    grid-template-areas: "stage stage" "left right";
    grid-template-columns: minmax(0, 1fr) minmax(0, 1fr);
  }
  #app.pad-full { grid-template-rows: auto minmax(0, 1fr); }
  #app.pad-full #stage {
    width: 100%;
    aspect-ratio: 4 / 3;
    max-height: calc(var(--vh) * 58);
  }
  /* first rows (system / utility buttons) aligned on top, controls centred below */
  #app.pad-full .pad { justify-content: flex-start; padding: 12px 10px 18px; }
  #app.pad-full #pad-dpad,
  #app.pad-full #pad-actions { margin: auto 0; }

  #app.pad-mini { grid-template-rows: minmax(0, 1fr) auto; }
  #app.pad-mini .pad { flex-direction: row; padding: 10px 14px 16px; }
  #app.pad-mini .pad-row { flex-direction: row; }
  #app.pad-mini .pad-left  { justify-content: flex-start; }
  #app.pad-mini .pad-right { justify-content: flex-end; }
}

/* ─── Pulsanti ──────────────────────────────────────────────────────────── */

#app button {
  -webkit-appearance: none;
  appearance: none;
  border: 2px solid var(--edge);
  background: var(--panel);
  color: var(--txt);
  font: inherit;
  display: inline-flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  cursor: pointer;
  touch-action: none;
  outline: none;
  transition: transform 60ms ease, background-color 60ms ease, box-shadow 60ms ease;
}
#app button svg { width: 55%; height: 55%; pointer-events: none; }
#app button .lbl, #app button .txt { pointer-events: none; white-space: nowrap; }
#app button.on,
#app button:active {
  background: var(--panel-on);
  border-color: var(--edge-on);
  box-shadow: 0 0 14px rgba(60, 200, 255, 0.55), inset 0 0 10px rgba(140, 50, 220, 0.35);
  transform: scale(0.94);
}

/* sistema: pausa / riavvia / menu / schermo intero */
#app button.sys {
  width: 46px;
  height: 46px;
  border-radius: 12px;
  border-color: rgba(140, 50, 220, 0.55);
}
#app button.sys svg { width: 20px; height: 20px; }
#app button.sys .lbl { font-size: 8px; letter-spacing: 0.02em; color: var(--sub); }
/* utility buttons (full screen / hide pad): icon only, the name stays in aria-label/title */
#app button.util .lbl { display: none; }
#app button.util svg { width: 22px; height: 22px; }

/* azioni: ruota, fuoco, drop… */
#app button.btn {
  --s: min(66px, calc(var(--vh) * 17));
  width: var(--s);
  height: var(--s);
  border-radius: 50%;
}
#app button.btn.big { --s: min(84px, calc(var(--vh) * 22)); }
#app button.btn .txt { font-size: 13px; font-weight: 700; letter-spacing: 0.04em; }
#app button.btn .lbl { font-size: 9px; color: var(--sub); }
#app button.btn svg { width: 46%; height: 46%; }

@media (orientation: portrait) {
  #app button.btn { --s: min(76px, 19vw); }
  #app button.btn.big { --s: min(92px, 23vw); }
}

/* ─── Croce direzionale ─────────────────────────────────────────────────── */

.dpad {
  position: relative;
  /* NB: no percentage here: the parent is sized by its content (circular size → d-pad collapsed) */
  width: min(150px, calc(var(--vh) * 36), calc(var(--side) - 14px));
  aspect-ratio: 1;
  border-radius: 50%;
  background:
    radial-gradient(circle at center, rgba(140, 50, 220, 0.18) 0 22%, transparent 23%),
    var(--panel);
  border: 2px solid var(--edge);
  touch-action: none;
  flex-shrink: 0;
}
.dpad.axis-x { aspect-ratio: 2.1; border-radius: 999px; }
.dpad.axis-y { aspect-ratio: 0.48; border-radius: 999px; }
@media (orientation: portrait) {
  .dpad { width: min(190px, 44vw, calc(var(--vh) * 30)); }
}

.dpad .arr {
  position: absolute;
  width: 36%;
  height: 36%;
  display: grid;
  place-items: center;
  color: var(--txt);
  opacity: 0.85;
  pointer-events: none;
  transition: color 60ms ease, filter 60ms ease;
}
.dpad .arr svg { width: 62%; height: 62%; }
.dpad .arr.up    { top: 3%;    left: 32%; }
.dpad .arr.down  { bottom: 3%; left: 32%; }
.dpad .arr.left  { left: 3%;   top: 32%; }
.dpad .arr.right { right: 3%;  top: 32%; }
.dpad.axis-x .arr { top: 0; height: 100%; width: 40%; }
.dpad.axis-x .arr.left  { left: 4%; }
.dpad.axis-x .arr.right { right: 4%; }
.dpad .arr.on {
  color: var(--edge-on);
  opacity: 1;
  filter: drop-shadow(0 0 6px var(--edge-on));
}

/* ─── Pulsanti flottanti (pad nascosto) ─────────────────────────────────── */

#fab {
  position: fixed;
  top: calc(env(safe-area-inset-top) + 8px);
  right: calc(env(safe-area-inset-right) + 8px);
  display: flex;
  gap: 6px;
  z-index: 10;
  opacity: 0;
  pointer-events: none;
  transition: opacity 300ms ease;
}
#fab.awake,
#fab.pinned { opacity: 0.6; pointer-events: auto; }
#fab:hover { opacity: 1; }
#fab[hidden] { display: none; }
#fab button.sys { width: 38px; height: 38px; }
#fab button.sys .lbl { display: none; }

/* ─── Messaggi di avvio ─────────────────────────────────────────────────── */

#boot,
#file-warning {
  position: fixed;
  inset: 0;
  display: grid;
  place-items: center;
  text-align: center;
  padding: 24px;
  font-size: 15px;
  line-height: 1.6;
  color: var(--txt);
  pointer-events: none;
}
#boot span { color: #32e6ff; letter-spacing: 0.15em; animation: blink 1s steps(2) infinite; }
#file-warning { background: var(--bg); pointer-events: auto; z-index: 100; }
#file-warning[hidden] { display: none; }
#file-warning code { color: #78f078; }
@keyframes blink { 50% { opacity: 0.25; } }

@media (prefers-reduced-motion: reduce) {
  #boot span { animation: none; }
  #app button { transition: none; }
}
