:root { color-scheme: dark; font-family: system-ui, sans-serif; color: #f1f3f5; background: #000; --accent: #a8e4d2; }
* { box-sizing: border-box; }
[hidden] { display: none !important; }
body { margin: 0; width: 100%; height: 100dvh; display: flex; flex-direction: column; background: #000; overflow: hidden; }
main { flex: 1; min-width: 0; min-height: 0; display: grid; place-items: center; position: relative; overflow: hidden; touch-action: none; }
canvas { display: block; object-fit: contain; image-rendering: pixelated; touch-action: none; outline: none; cursor: none; }
.visually-hidden { position: absolute; width: 1px; height: 1px; padding: 0; margin: -1px; overflow: hidden; clip-path: inset(50%); white-space: nowrap; }
button { font: inherit; color: inherit; background: #222; border: 1px solid #444; border-radius: 5px; touch-action: manipulation; cursor: pointer; }
@media (hover: hover) { button:hover { background: #333; } }
button:active, button[aria-pressed="true"], button[aria-expanded="true"], .key.held { color: #112c24; background: var(--accent); border-color: var(--accent); }
button:focus-visible { outline: 2px solid var(--accent); outline-offset: -3px; }
#touch-controls { flex-shrink: 0; background: #000; padding: 6px max(8px, env(safe-area-inset-right)) max(6px, env(safe-area-inset-bottom)) max(8px, env(safe-area-inset-left)); user-select: none; -webkit-user-select: none; }
.control-bar, .compact-controls, #quick-keys { display: flex; gap: 6px; max-width: 920px; margin: auto; }
.control-bar button { flex: 1; min-width: 0; min-height: 44px; padding: 6px; font-size: 13px; }
[data-mouse] { touch-action: none; }
.compact-controls { align-items: center; margin-top: 6px; }
.quick-controls { flex: 1; min-width: 0; }
#touch-hint { color: #b8b8b8; font-size: 12px; text-align: center; margin: 0 0 6px; }
#quick-keys .key { min-height: 44px; font-size: 13px; }
#keyboard { margin-top: 8px; max-height: min(48dvh, 260px); overflow-y: auto; overscroll-behavior: contain; }
#joystick-controls { display: flex; align-items: center; gap: 8px; }
.joystick-pads { display: flex; gap: 12px; }
.dual-joysticks .compact-controls { display: grid; grid-template-columns: auto minmax(0, 1fr) auto; }
.dual-joysticks #joystick-controls, .dual-joysticks .joystick-pads { display: contents; }
.dual-joysticks .joystick-control:first-child { grid-column: 1; grid-row: 1 / span 2; }
.dual-joysticks #mouse-joystick-control { grid-column: 3; grid-row: 1 / span 2; }
.dual-joysticks #joystick-mapping { grid-column: 2; grid-row: 1; justify-self: center; }
.dual-joysticks .quick-controls { grid-column: 2; grid-row: 2; }
.dual-joysticks #touch-hint { display: none; }
.joystick-control { display: grid; justify-items: center; gap: 4px; font-size: 12px; color: #b8b8b8; }
.joystick { width: 112px; height: 112px; flex-shrink: 0; border: 1px solid #555; border-radius: 50%; position: relative; background: #171717; touch-action: none; }
.joystick::before, .joystick::after { content: ''; position: absolute; background: #444; }
.joystick::before { width: 1px; height: 80px; left: 55px; top: 15px; }
.joystick::after { height: 1px; width: 80px; top: 55px; left: 15px; }
.joystick-knob { position: absolute; width: 44px; height: 44px; top: 33px; left: 33px; background: var(--accent); border-radius: 50%; }
#joystick-mapping { width: 150px; min-height: 44px; font: inherit; font-size: 13px; color: inherit; background: #222; border: 1px solid #444; border-radius: 5px; }
@media (max-width: 600px) {
  #joystick-controls { flex-direction: column; gap: 6px; }
  .dual-joysticks .compact-controls { grid-template-columns: 150px minmax(0, 1fr); }
  .dual-joysticks .joystick-pads { display: flex; justify-content: space-between; grid-column: 1 / -1; grid-row: 1; }
  .dual-joysticks #joystick-mapping { grid-column: 1; grid-row: 2; }
  .dual-joysticks .quick-controls { grid-column: 2; grid-row: 2; }
  .dual-joysticks #quick-keys .key { flex: 1; }
}
@media (max-width: 400px) { #joystick-controls:not([hidden]) + .quick-controls #quick-keys { flex-direction: column; } }
.dual-joysticks #joystick-controls:not([hidden]) + .quick-controls #quick-keys { flex-direction: row; }
#keys { max-width: 920px; margin: auto; display: grid; gap: 5px; }
.key-row { display: flex; gap: 4px; justify-content: center; }
.key { padding: 4px 0; min-width: 0; flex: 1; min-height: 38px; font-size: 13px; touch-action: none; }
.key.wide { flex: 1.5; font-size: 11px; }
.key.space { flex: 4; }
#error { position: absolute; max-width: min(40ch, 90%); padding: 24px; background: #151515; text-align: center; line-height: 1.5; }
#error button { padding: 10px 20px; }
::selection { color: #112c24; background: var(--accent); }
@media (max-height: 500px) and (pointer: coarse) { #keys .key { min-height: 32px; } .joystick { width: 96px; height: 96px; } .joystick-knob { top: 25px; left: 25px; } .joystick::before { left: 47px; top: 7px; } .joystick::after { top: 47px; left: 7px; } }
@media (pointer: coarse) and (orientation: landscape) {
  body { flex-direction: row; }
  #controls-toggle { align-self: center; flex: 0 0 44px; width: 44px; min-height: 120px; writing-mode: vertical-rl; padding: 12px 8px; font-size: 13px; }
  #touch-controls { width: clamp(280px, 38vw, 360px); height: 100%; overflow-y: auto; overscroll-behavior: contain; }
  .control-bar { display: grid; grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .compact-controls { flex-direction: column; align-items: stretch; gap: 8px; }
  #joystick-controls { flex-direction: column; gap: 6px; }
  #joystick-controls:not([hidden]) + .quick-controls #quick-keys { flex-direction: row; }
  .dual-joysticks .compact-controls { grid-template-columns: minmax(0, 1fr); }
  .dual-joysticks .joystick-pads { display: flex; justify-content: space-between; grid-column: 1; grid-row: 1; }
  .dual-joysticks #joystick-mapping { grid-column: 1; grid-row: 2; width: 100%; }
  .dual-joysticks .quick-controls { grid-column: 1; grid-row: 3; }
  #keyboard { max-height: none; overflow: visible; }
}
@media (pointer: coarse) and (orientation: landscape) and (max-height: 360px) {
  .joystick { width: 84px; height: 84px; }
  .joystick-knob { top: 19px; left: 19px; }
  .joystick::before { left: 41px; top: 1px; }
  .joystick::after { top: 41px; left: 1px; }
}
