html, body {
  margin: 0;
  height: 100%;
  background: #0a0a14;
  font-family: system-ui, sans-serif;
  -webkit-user-select: none;
  user-select: none;
  -webkit-tap-highlight-color: transparent;
  overscroll-behavior: none;
}

body {
  min-height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

#frame {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

#screen {
  width: 768px;
  max-width: 96vw;
  aspect-ratio: 8 / 7;   /* 256:224 */
  height: auto;
  max-height: 84vh;
  image-rendering: pixelated;
  border: 3px solid #303050;
  border-radius: 6px;
  background: #000;
}

#controls {
  color: #707090;
  font-size: 13px;
  letter-spacing: 1px;
  text-align: center;
}

/* ===== タッチ仮想パッド（既定は非表示。body.touch で表示） ===== */
#touch-controls, #sys-controls { display: none; }

body.touch { justify-content: flex-start; }

body.touch #controls { display: none; }   /* キーボード説明はスマホでは隠す */

body.touch #screen {
  max-height: none;
  margin-top: calc(env(safe-area-inset-top, 0) + 34px); /* 上部ボタンの分だけ下げる */
}

.btn {
  -webkit-appearance: none;
  appearance: none;
  color: #e8e8f4;
  background: rgba(60, 64, 96, 0.78);
  border: 2px solid rgba(150, 160, 210, 0.55);
  font-family: system-ui, sans-serif;
  font-weight: 700;
  touch-action: none;
  user-select: none;
  display: flex;
  align-items: center;
  justify-content: center;
}
.btn.pressed { background: rgba(110, 120, 180, 0.95); transform: scale(0.94); }

/* 上部システムボタン（メニュー / おと）を画面上に固定オーバーレイ */
body.touch #sys-controls {
  display: flex;
  justify-content: space-between;
  position: fixed;
  top: env(safe-area-inset-top, 0);
  left: 0; right: 0;
  padding: 6px 10px;
  pointer-events: none;
  z-index: 10;
}
body.touch #sys-controls .sys { pointer-events: auto; }
.sys {
  height: 34px;
  min-width: 92px;
  border-radius: 17px;
  font-size: 14px;
  padding: 0 14px;
  background: rgba(40, 44, 70, 0.86);
}

/* 下段：十字キー（左）＋A/B（右） */
body.touch #touch-controls {
  display: flex;
  justify-content: space-between;
  align-items: flex-end;
  width: 100%;
  box-sizing: border-box;
  padding: 12px 16px calc(16px + env(safe-area-inset-bottom, 0));
  margin-top: auto;            /* 画面下に寄せる */
  touch-action: none;
}

/* 十字キー（3x3グリッド） */
#dpad {
  display: grid;
  grid-template-columns: repeat(3, 48px);
  grid-template-rows: repeat(3, 48px);
  gap: 4px;
}
.dir { font-size: 20px; border-radius: 11px; }
.dir.up    { grid-area: 1 / 2; }
.dir.left  { grid-area: 2 / 1; }
.dir.right { grid-area: 2 / 3; }
.dir.down  { grid-area: 3 / 2; }

/* A / B ボタン */
#action-pad {
  display: flex;
  align-items: center;
  gap: 14px;
}
.act {
  width: 64px;
  height: 64px;
  border-radius: 50%;
  font-size: 24px;
}
.act.a { background: rgba(70, 120, 90, 0.82); border-color: rgba(120, 220, 150, 0.6); margin-bottom: 6px; }
.act.b { background: rgba(130, 70, 80, 0.82); border-color: rgba(220, 130, 140, 0.6); margin-top: 16px; }

/* 横向き（landscape）はパッドを画面の左右に重ねて広く使う */
@media (orientation: landscape) and (pointer: coarse) {
  body.touch { justify-content: center; }
  body.touch #screen { max-height: 96vh; max-width: 74vw; margin-top: 0; }
  body.touch #touch-controls {
    position: fixed;
    inset: 0;
    padding: 0 16px calc(12px + env(safe-area-inset-bottom, 0));
    align-items: center;
    pointer-events: none;        /* 隙間はタップ透過、ボタンのみ反応 */
  }
  body.touch #dpad, body.touch #action-pad { pointer-events: auto; }
}
