* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
html, body { margin: 0; height: 100%; background: #0b0b0d; color: #ddd; overflow: hidden;
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
  user-select: none; -webkit-user-select: none; touch-action: none; overscroll-behavior: none; }
button { font: inherit; color: inherit; }

#app { position: fixed; inset: 0; display: flex;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left); }

#screen-wrap { position: relative; display: flex; align-items: center; justify-content: center; background: #000; }
#screen { width: 100%; height: 100%; object-fit: contain; image-rendering: pixelated; display: block; }
#screen.stretch { object-fit: fill; }

#toolbar { display: flex; gap: 6px; justify-content: center; }
#toolbar button { width: 44px; height: 36px; border-radius: 10px; border: 1px solid #333; background: #1c1c22; font-size: 18px; }
#toolbar button.on { background: #2e4f8a; border-color: #4a73c0; }

/* ---------- pad ---------- */
#controls { display: flex; align-items: center; justify-content: space-between; }
.pad-side { display: flex; align-items: center; justify-content: center; }
#dpad { position: relative; width: var(--dpad, 150px); height: var(--dpad, 150px); border-radius: 50%;
  background: radial-gradient(circle, #26262e 0 60%, #1a1a20 61%); touch-action: none; }
#dpad .dp { position: absolute; background: #3a3a44; border-radius: 6px; }
#dpad .up, #dpad .down { left: 35%; width: 30%; height: 35%; }
#dpad .left, #dpad .right { top: 35%; height: 30%; width: 35%; }
#dpad .up { top: 2%; } #dpad .down { bottom: 2%; } #dpad .left { left: 2%; } #dpad .right { right: 2%; }
#dpad .dp-center { position: absolute; left: 35%; top: 35%; width: 30%; height: 30%; background: #3a3a44; }
#dpad .dp.on { background: #6a8fd8; }

#abtns { position: relative; width: var(--ab, 150px); height: var(--ab, 150px); }
.ab { position: absolute; width: 46%; height: 46%; border-radius: 50%; border: none; font-weight: 700; font-size: 20px;
  background: #8a2b3a; color: #fff; box-shadow: 0 3px 0 #4d1520; touch-action: none; }
.ab.a { right: 0; top: 8%; } .ab.b { left: 0; bottom: 8%; }
.ab.on, .pill.on { filter: brightness(1.5); transform: translateY(2px); box-shadow: none; }
#pad-mid { display: flex; gap: 10px; }
.pill { border: none; border-radius: 14px; padding: 6px 12px; font-size: 11px; letter-spacing: 1px;
  background: #33333c; color: #bbb; touch-action: none; }

/* ---------- keyboard ---------- */
#kbd { display: flex; flex-direction: column; gap: 4px; padding: 4px; background: #15151a; }
#kbd .krow { display: flex; gap: 4px; }
#kbd .k { flex: 1 1 0; min-width: 0; height: var(--kh, 38px); border: none; border-radius: 6px; background: #2b2b33;
  font-size: 13px; padding: 0; touch-action: none; }
#kbd .k.mod { background: #3a3a46; }
#kbd .k.latch { background: #2e4f8a; }
#kbd .k.down { background: #6a8fd8; }

/* ---------- portrait: screen on top, controls below ---------- */
#app.portrait { flex-direction: column; }
#app.portrait #screen-wrap { width: 100%; aspect-ratio: 4 / 3; flex: none; max-height: 55vh; }
#app.portrait #toolbar { padding: 8px 0; }
#app.portrait #controls { flex: 1; padding: 0 14px 40px; position: relative; min-height: 0; }
#app.portrait #pad-mid { position: absolute; bottom: 10px; left: 50%; transform: translateX(-50%); }
#app.portrait.kbd-on #controls { display: none; }
#app.portrait #kbd { flex: 1; justify-content: center; }

/* ---------- landscape (side): controls in bars left/right of the image ---------- */
#app.side { flex-direction: row; }
#app.side #screen-wrap { position: absolute; top: 0; bottom: 0; left: var(--sidew); right: var(--sidew); }
#app.side #controls { position: absolute; inset: 0; pointer-events: none; }
#app.side .pad-side { position: absolute; top: 0; bottom: 0; width: var(--sidew); pointer-events: auto; }
#app.side #pad-left { left: 0; } #app.side #pad-right { right: 0; }
#app.side #pad-mid { position: absolute; bottom: 10px; left: 0; width: var(--sidew); flex-direction: column; align-items: center; pointer-events: auto; }
#app.side #toolbar { position: absolute; top: 6px; right: 0; width: var(--sidew); flex-wrap: wrap; z-index: 5; }
#app.side #toolbar button { width: 38px; height: 32px; font-size: 15px; }

/* ---------- landscape (overlay): fullscreen image, translucent controls on top ---------- */
#app.overlay #screen-wrap { position: absolute; inset: 0; }
#app.overlay #controls { position: absolute; inset: 0; pointer-events: none; }
#app.overlay .pad-side { position: absolute; bottom: 12px; pointer-events: auto; opacity: .45; }
#app.overlay #pad-left { left: 16px; } #app.overlay #pad-right { right: 16px; }
#app.overlay #pad-mid { position: absolute; bottom: 8px; left: 50%; transform: translateX(-50%); pointer-events: auto; opacity: .5; }
#app.overlay #toolbar { position: absolute; top: 6px; left: 50%; transform: translateX(-50%); opacity: .55; z-index: 5; }
#app.overlay #toolbar button { width: 38px; height: 30px; font-size: 15px; }
#app.side.pad-hidden #controls, #app.overlay.pad-hidden #controls { display: none; }

#app.side #kbd, #app.overlay #kbd { position: absolute; left: 0; right: 0; bottom: 0; z-index: 4; opacity: .92; }

/* ---------- dialogs ---------- */
.dialog { position: fixed; inset: 0; background: rgba(0,0,0,.75); display: flex; align-items: center; justify-content: center; z-index: 20; }
.dialog[hidden], #kbd[hidden] { display: none !important; }
.dlg-body { background: #1b1b21; border: 1px solid #333; border-radius: 14px; padding: 14px; width: min(560px, 94vw);
  max-height: 90vh; overflow: auto; user-select: text; touch-action: auto; }
.dlg-body.center { text-align: center; }
.dlg-head { display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px; }
.dlg-head button, .dialog button, .btn { background: #2b2b33; border: 1px solid #444; border-radius: 8px; padding: 8px 14px; cursor: pointer; }
.dialog select { background: #2b2b33; color: #ddd; border: 1px solid #444; border-radius: 8px; padding: 7px; font: inherit; }
.row { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin: 10px 0; }
.hint { font-size: 13px; color: #999; }
#file-list { list-style: none; padding: 0; margin: 0; font-size: 14px; }
#file-list li { display: flex; justify-content: space-between; align-items: center; padding: 6px 4px; border-bottom: 1px solid #2a2a30; gap: 8px; }
#file-list li span { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#file-list li small { color: #777; margin-left: 6px; }
#file-list li button { padding: 3px 9px; }
#boot h1 { margin: 4px 0 8px; letter-spacing: 3px; }
#btn-start { font-size: 18px; padding: 12px 24px; background: #2e4f8a; border-color: #4a73c0; }
