:root {
  --bg: #14161c;
  --panel: #1f232c;
  --line: #2e3442;
  --text: #e9ecf2;
  --muted: #8d95a6;
  --accent: #2fbf71;
  --warn: #e0a030;
  --bad: #e05050;
  --key: #2a2f3b;
  --key-down: #46506a;
  color-scheme: dark;
}
* { box-sizing: border-box; -webkit-tap-highlight-color: transparent; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; background: var(--bg); color: var(--text); font: 15px/1.4 system-ui, -apple-system, sans-serif; }
body {
  display: flex; flex-direction: column; overflow: hidden;
  padding: env(safe-area-inset-top) env(safe-area-inset-right) env(safe-area-inset-bottom) env(safe-area-inset-left);
  user-select: none; -webkit-user-select: none; touch-action: none;
}
input, textarea { user-select: text; -webkit-user-select: text; }

.bar { display: flex; align-items: center; gap: 10px; padding: 6px 12px; min-height: 44px; }
.title { flex: 1; font-weight: 600; color: var(--muted); overflow: hidden; white-space: nowrap; text-overflow: ellipsis; }
.pill { font-size: 12px; padding: 3px 10px; border-radius: 999px; background: var(--panel); border: 1px solid var(--line); white-space: nowrap; }
.pill::before { content: ""; display: inline-block; width: 8px; height: 8px; border-radius: 50%; margin-right: 6px; background: var(--muted); vertical-align: 0; }
.pill.on::before { background: var(--accent); }
.pill.wait::before { background: var(--warn); }
.pill.off::before { background: var(--bad); }
.icon { background: none; border: 0; color: var(--text); font-size: 22px; width: 40px; height: 40px; }

#stage { position: relative; display: flex; align-items: center; justify-content: center; padding: 0 8px; }
#screen-wrap { position: relative; width: 100%; max-width: calc((100vh - 330px) * 1.5); min-width: 240px; aspect-ratio: 3 / 2; background: #000; border-radius: 6px; overflow: hidden; }
#screen { width: 100%; height: 100%; display: block; image-rendering: pixelated; image-rendering: crisp-edges; }
#fx { position: absolute; inset: 0; width: 100%; height: 100%; display: block; pointer-events: none; }
#fx[hidden] { display: none; }
select { width: 100%; min-height: 40px; margin-top: 4px; border-radius: 8px; border: 1px solid var(--line); background: var(--key); color: var(--text); font: inherit; padding: 0 10px; }
#splash { position: absolute; inset: 0; display: flex; flex-direction: column; align-items: center; justify-content: center; gap: 10px; padding: 16px; text-align: center; background: #000c; }
#splash[hidden] { display: none; }
#splash p { margin: 0; color: var(--muted); }
#rom-cache { display: flex; flex-direction: column; gap: 6px; }
.error { color: var(--bad) !important; }
#banner { position: absolute; left: 16px; right: 16px; top: 8px; padding: 10px 12px; border-radius: 8px; background: #3a2c10ee; border: 1px solid var(--warn); font-size: 13px; z-index: 2; }
#banner[hidden] { display: none; }

.btn { display: inline-flex; align-items: center; justify-content: center; min-height: 40px; padding: 0 14px; border-radius: 8px; border: 1px solid var(--line); background: var(--key); color: var(--text); font: inherit; cursor: pointer; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: #06210f; font-weight: 600; }

/* Controls: each one absolutely placed in #pad by controls.js (left/top = its center) */
#pad { position: relative; flex: 1; width: 100%; max-width: 560px; margin: 0 auto; min-height: 270px; }
#pad [data-ctl] { position: absolute; left: 50%; top: 50%; transform: translate(-50%, -50%); transform-origin: center; }
.key { background: var(--key); color: var(--text); border: 1px solid var(--line); font: 600 15px system-ui, sans-serif; touch-action: none; }
.key.pressed, .d.pressed { background: var(--key-down); color: var(--text); }
.shoulder { width: 96px; height: 40px; border-radius: 10px 10px 4px 4px; }
.round { width: 68px; height: 68px; border-radius: 50%; font-size: 20px; }
.pillkey { height: 30px; padding: 0 16px; border-radius: 15px; font-size: 11px; letter-spacing: 1px; }
#dpad { width: 150px; height: 150px; touch-action: none; }
.d { position: absolute; width: 50px; height: 50px; display: flex; align-items: center; justify-content: center; background: var(--key); border: 1px solid var(--line); color: var(--muted); font-size: 14px; }
.d.up { left: 50px; top: 0; border-radius: 8px 8px 0 0; }
.d.down { left: 50px; top: 100px; border-radius: 0 0 8px 8px; }
.d.left { left: 0; top: 50px; border-radius: 8px 0 0 8px; }
.d.right { left: 100px; top: 50px; border-radius: 0 8px 8px 0; }
#dpad::after { content: ""; position: absolute; left: 50px; top: 50px; width: 50px; height: 50px; background: var(--key); }

/* Edit mode: controls show an outline, the toolbar floats on top */
body.editing #pad [data-ctl] { outline: 2px dashed #ffffff55; outline-offset: 4px; cursor: grab; }
body.editing #pad [data-ctl].selected { outline-color: var(--accent); }
#edit-bar { position: fixed; z-index: 10; left: 50%; top: max(8px, env(safe-area-inset-top)); transform: translateX(-50%); width: min(420px, calc(100vw - 24px)); display: flex; flex-direction: column; gap: 6px; padding: 10px 12px; border-radius: 12px; background: #1f232cf0; border: 1px solid var(--line); }
#edit-bar[hidden] { display: none; }
#edit-bar label { display: flex; align-items: center; gap: 10px; font-size: 13px; color: var(--muted); }
#edit-bar input[type=range] { flex: 1; }
#edit-bar .buttons { justify-content: flex-end; }

/* Landscape phones: the screen fills the height, the controls float over it */
@media (orientation: landscape) and (max-height: 560px) {
  .bar { position: absolute; top: 0; left: 0; right: 0; min-height: 36px; padding: 2px 10px; z-index: 3; pointer-events: none; }
  .bar > * { pointer-events: auto; }
  .title { visibility: hidden; }
  #stage { position: absolute; inset: 0; padding: 0; }
  #screen-wrap { width: auto; height: 100%; max-width: 100%; border-radius: 0; }
  #pad { position: absolute; inset: 0; max-width: none; min-height: 0; pointer-events: none; }
  #pad [data-ctl] { pointer-events: auto; }
  .key, .d, #dpad::after { background: #2a2f3bcc; }
  #edit-bar { top: 4px; padding: 6px 10px; gap: 4px; }
}

dialog { border: 1px solid var(--line); border-radius: 12px; background: var(--panel); color: var(--text); padding: 0; width: min(420px, calc(100vw - 32px)); max-height: calc(100vh - 32px); }
dialog::backdrop { background: #0009; }
.sheet { display: flex; flex-direction: column; gap: 10px; padding: 16px; }
.sheet h2, .sheet h3 { margin: 4px 0 0; font-size: 16px; }
.sheet h3 { color: var(--muted); font-size: 13px; text-transform: uppercase; letter-spacing: 1px; margin-top: 8px; }
.sheet label:not(.btn) { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.sheet input { min-height: 40px; padding: 0 10px; border-radius: 8px; border: 1px solid var(--line); background: var(--bg); color: var(--text); font: inherit; font-size: 16px; }
.buttons { display: flex; flex-wrap: wrap; gap: 8px; }
.hint { margin: 0; font-size: 12px; color: var(--muted); }

/* Portal (index.html): login and game grid */
body.portal { overflow: auto; touch-action: auto; user-select: auto; -webkit-user-select: auto; }
.brand { color: var(--text); }
.who { color: var(--muted); font-size: 14px; }
.btn.small { min-height: 32px; padding: 0 10px; font-size: 13px; }
a.btn { text-decoration: none; }
.page { width: 100%; max-width: 880px; margin: 0 auto; padding: 8px 16px 32px; }
.page h1 { font-size: 22px; margin: 8px 0 16px; }
.card-form { display: flex; flex-direction: column; gap: 12px; max-width: 360px; margin: 6vh auto 0; padding: 20px; border-radius: 14px; background: var(--panel); border: 1px solid var(--line); }
.card-form h1 { margin: 0; }
.card-form label { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.card-form input { min-height: 44px; padding: 0 12px; border-radius: 8px; border: 1px solid var(--line); background: var(--bg); color: var(--text); font: inherit; font-size: 16px; }
.card-form .buttons .btn { flex: 1; min-height: 44px; }
.card-form .error { margin: 0; font-size: 14px; }
.grid { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 16px; }
.game-card { display: flex; flex-direction: column; gap: 8px; color: var(--text); text-decoration: none; font-weight: 600; font-size: 14px; }
.game-card img { width: 100%; aspect-ratio: 3 / 4; border-radius: 10px; border: 1px solid var(--line); background: var(--panel); box-shadow: 0 6px 18px #0006; transition: transform .15s; }
.game-card:hover img, .game-card:focus-visible img { transform: translateY(-3px); }
.game-card.off { opacity: .45; }

/* Typing with the device's keyboard over the game's naming screen */
#typer { position: fixed; left: 50%; top: max(12px, env(safe-area-inset-top)); transform: translateX(-50%); z-index: 20;
         width: min(420px, calc(100vw - 24px)); display: flex; flex-direction: column; gap: 8px; padding: 12px;
         border-radius: 12px; background: var(--panel); border: 1px solid var(--accent); box-shadow: 0 10px 40px #000c; }
#typer[hidden] { display: none; }
#typer label { font-size: 13px; color: var(--muted); }
.typer-row { display: flex; align-items: center; gap: 8px; }
#typer-input { flex: 1; min-width: 0; min-height: 44px; padding: 0 12px; border-radius: 8px; border: 1px solid var(--line);
               background: var(--bg); color: var(--text); font: inherit; font-size: 18px; }
#typer-count { font-size: 12px; color: var(--muted); min-width: 3.5em; text-align: right; }
.typer-buttons { display: flex; gap: 8px; justify-content: flex-end; }
#typer-reopen { position: fixed; right: 12px; top: max(12px, env(safe-area-inset-top)); z-index: 20; }
#typer-reopen[hidden] { display: none; }

/* PWA: install / update popups */
.pwa-popup { position: fixed; inset: 0; z-index: 30; display: flex; align-items: flex-end; justify-content: center;
             padding: 16px; padding-bottom: max(16px, env(safe-area-inset-bottom)); background: #0008; }
@media (min-width: 640px) { .pwa-popup { align-items: center; } }
.pwa-card { width: min(440px, 100%); background: var(--panel); border: 1px solid var(--accent); border-radius: 14px;
            padding: 16px; box-shadow: 0 16px 50px #000c; }
.pwa-card h2 { margin: 0 0 8px; font-size: 17px; }
.pwa-body { color: var(--muted); font-size: 14px; }
.pwa-body p { margin: 0 0 8px; }
.pwa-body ol { margin: 4px 0 8px; padding-left: 20px; }
.pwa-buttons { display: flex; gap: 8px; justify-content: flex-end; margin-top: 12px; }
.btn.update { border-color: var(--accent); color: var(--accent); }
