:root {
  --bg: #07080c;
  --panel: rgba(22, 24, 32, .92);
  --line: rgba(255, 255, 255, .09);
  --text: #eef0f5;
  --muted: #9aa1b3;
  --accent: #e5383b;
  --accent2: #ffb703;
  --radius: 14px;
  --px: 3px;                         /* 1 pixel do GBA na tela (JS) */
  --sat: env(safe-area-inset-top, 0px);
  --sab: env(safe-area-inset-bottom, 0px);
  --sal: env(safe-area-inset-left, 0px);
  --sar: env(safe-area-inset-right, 0px);
  color-scheme: dark;
}
* { box-sizing: border-box; }
[hidden] { display: none !important; }
html, body { margin: 0; height: 100%; overflow: hidden; background: var(--bg); }
body {
  color: var(--text);
  font: 15px/1.45 system-ui, -apple-system, "Segoe UI", Roboto, Ubuntu, sans-serif;
  -webkit-tap-highlight-color: transparent; -webkit-user-select: none; user-select: none;
  touch-action: manipulation; overscroll-behavior: none;
}
button, select, input { font: inherit; color: inherit; }
a { color: var(--accent2); }
kbd {
  display: inline-block; min-width: 1.6em; padding: 0 .38em; margin: 0 .08em;
  font: 600 .76em/1.6 ui-monospace, SFMono-Regular, Menlo, monospace; text-align: center;
  color: var(--text); background: rgba(255, 255, 255, .08); border: 1px solid rgba(255, 255, 255, .16); border-bottom-width: 2px; border-radius: 6px;
}

/* ---------- fundo imersivo ---------- */
#ambient {
  position: fixed; inset: -12%; width: 124%; height: 124%; object-fit: cover; z-index: 0;
  filter: blur(56px) saturate(1.9) brightness(.62); opacity: 0; transition: opacity 1.2s ease; pointer-events: none;
}
body.ambient-on #ambient.live { opacity: 1; }
.backdrop {
  position: fixed; inset: 0; z-index: 0; pointer-events: none;
  background:
    radial-gradient(120% 80% at 50% 0%, rgba(229, 56, 59, .16), transparent 60%),
    radial-gradient(90% 70% at 50% 110%, rgba(66, 84, 255, .14), transparent 60%),
    radial-gradient(ellipse at center, transparent 40%, rgba(0, 0, 0, .55) 100%);
}

/* ---------- área do jogo ---------- */
#app { position: fixed; inset: 0; z-index: 1; display: flex; flex-direction: column; }
.stage { position: relative; flex: 1; display: grid; place-items: center; min-height: 0; padding: 24px; }
.screen {
  position: relative; line-height: 0; border-radius: 6px; overflow: hidden;
  box-shadow: 0 30px 80px rgba(0, 0, 0, .65), 0 0 0 1px rgba(255, 255, 255, .06);
}
.screen canvas {
  display: block; width: calc(240 * var(--px)); height: calc(160 * var(--px));
  image-rendering: pixelated; image-rendering: crisp-edges; background: #000;
}
.screen.gba-color canvas { filter: brightness(.88) contrast(.92) saturate(.85) sepia(.06); }
.crt-overlay { position: absolute; inset: 0; pointer-events: none; }
.pause-badge {
  position: absolute; inset: 0; margin: auto; width: max-content; height: max-content; padding: 6px 16px;
  font: 800 13px/1.6 ui-monospace, monospace; letter-spacing: .3em; background: rgba(0, 0, 0, .7); border-radius: 8px;
}

/* filtros */
.screen[data-filter="smooth"] canvas { image-rendering: auto; }
.screen[data-filter="lcd"] .crt-overlay {
  background:
    linear-gradient(to right, rgba(0, 0, 0, .28) 0 1px, transparent 1px) 0 0 / var(--px) var(--px),
    linear-gradient(to bottom, rgba(0, 0, 0, .28) 0 1px, transparent 1px) 0 0 / var(--px) var(--px);
}
.screen[data-filter="scanlines"] .crt-overlay {
  background: linear-gradient(to bottom, transparent 55%, rgba(0, 0, 0, .45) 55%) 0 0 / 100% var(--px);
}
.screen[data-filter="crt"] canvas, .screen[data-filter="crt-curve"] canvas { filter: contrast(1.12) saturate(1.2) brightness(1.08); }
.screen[data-filter="crt"] .crt-overlay, .screen[data-filter="crt-curve"] .crt-overlay {
  background:
    radial-gradient(ellipse at center, transparent 58%, rgba(0, 0, 0, .55) 100%),
    linear-gradient(to bottom, transparent 50%, rgba(0, 0, 0, .38) 50%) 0 0 / 100% var(--px),
    linear-gradient(to right, rgba(255, 40, 40, .09) 0 33%, rgba(40, 255, 40, .09) 33% 66%, rgba(60, 60, 255, .09) 66%) 0 0 / var(--px) 100%;
  box-shadow: inset 0 0 calc(var(--px) * 8) rgba(0, 0, 0, .55);
}
.screen[data-filter="crt"] { box-shadow: 0 0 calc(var(--px) * 10) rgba(140, 170, 255, .18), 0 30px 80px rgba(0, 0, 0, .65); }
.screen[data-filter="crt-curve"] { border-radius: 7% / 10%; transform: scale(.97); }
.screen[data-filter="crt-curve"]::after {
  content: ""; position: absolute; inset: 0; pointer-events: none; border-radius: inherit;
  background: radial-gradient(ellipse at 30% 20%, rgba(255, 255, 255, .10), transparent 45%);
  box-shadow: inset 0 0 0 calc(var(--px) * 2) rgba(0, 0, 0, .6), inset 0 0 calc(var(--px) * 16) rgba(0, 0, 0, .7);
}

/* ---------- botões de canto (discretos) ---------- */
.corner {
  position: fixed; z-index: 5; top: calc(12px + var(--sat)); right: calc(12px + var(--sar));
  display: flex; gap: 8px; opacity: .55; transition: opacity .2s;
}
.corner:hover, .corner:focus-within { opacity: 1; }
.icon-btn {
  display: grid; place-items: center; width: 40px; height: 40px; padding: 0; cursor: pointer;
  color: #fff; background: rgba(10, 11, 16, .45); border: 1px solid rgba(255, 255, 255, .14); border-radius: 12px;
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px);
}
.icon-btn svg { width: 20px; height: 20px; }
.icon-btn:hover { background: rgba(30, 33, 45, .7); }
.icon-btn.small { width: 34px; height: 34px; border-radius: 10px; }

/* dica de teclado (desktop) */
.keyhint {
  position: fixed; z-index: 4; left: 50%; bottom: calc(14px + var(--sab)); transform: translateX(-50%);
  display: flex; flex-wrap: wrap; justify-content: center; gap: 6px 14px; padding: 8px 14px; max-width: calc(100vw - 32px);
  font-size: .78rem; color: rgba(255, 255, 255, .78); background: rgba(10, 11, 16, .5); border: 1px solid var(--line); border-radius: 12px;
  backdrop-filter: blur(10px); -webkit-backdrop-filter: blur(10px); transition: opacity .8s; pointer-events: none;
}
.keyhint i { font-style: normal; color: var(--muted); }
.keyhint.gone { opacity: 0; }
body.pad-on .keyhint { display: none; }

/* toast */
.toast {
  position: fixed; z-index: 20; left: 50%; top: calc(14px + var(--sat)); transform: translateX(-50%);
  max-width: calc(100vw - 120px); padding: 8px 14px; font-size: .85rem; text-align: center;
  background: rgba(14, 15, 21, .85); border: 1px solid var(--line); border-radius: 12px; backdrop-filter: blur(10px);
  -webkit-backdrop-filter: blur(10px); pointer-events: none; animation: toastIn .25s ease;
}
.toast.err { border-color: rgba(229, 56, 59, .6); color: #ffb3b4; }
@keyframes toastIn { from { opacity: 0; transform: translate(-50%, -6px); } }

/* carregando / erro */
.boot, .error { position: fixed; inset: 0; z-index: 30; display: grid; place-content: center; justify-items: center; gap: 14px; text-align: center; padding: 24px; }
.boot { background: radial-gradient(ellipse at center, #141621, var(--bg)); color: var(--muted); }
.spinner { width: 42px; height: 42px; border-radius: 50%; border: 3px solid rgba(255, 255, 255, .12); border-top-color: var(--accent); animation: spin .9s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }
.error { background: rgba(7, 8, 12, .92); }
.error .card { max-width: 460px; padding: 22px; background: var(--panel); border: 1px solid var(--line); border-radius: var(--radius); text-align: left; }
.error h2 { margin: 0 0 6px; }
.error p { color: var(--muted); margin: 0 0 12px; }
.error pre { max-height: 160px; overflow: auto; font-size: 12px; white-space: pre-wrap; color: #ffb3b4; -webkit-user-select: text; user-select: text; }
.error .row { display: flex; gap: 8px; margin-top: 12px; }

/* ---------- controle na tela ---------- */
.pad { display: none; }
body.pad-on .pad { display: block; }
.key {
  appearance: none; border: 0; padding: 0; cursor: pointer; touch-action: none; color: rgba(255, 255, 255, .92);
  font: 800 1rem/1 system-ui, sans-serif; -webkit-user-select: none; user-select: none;
  background: rgba(255, 255, 255, .10); box-shadow: inset 0 1px 0 rgba(255, 255, 255, .14), 0 4px 14px rgba(0, 0, 0, .35);
  backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px); transition: transform .05s, background .08s;
}
.key.pressed { transform: scale(.94); background: rgba(255, 255, 255, .24); }
.key.round { width: 74px; height: 74px; border-radius: 50%; font-size: 1.35rem; }
.key.a { background: linear-gradient(160deg, rgba(229, 56, 59, .95), rgba(170, 30, 34, .95)); }
.key.b { background: linear-gradient(160deg, rgba(110, 82, 235, .95), rgba(70, 48, 170, .95)); }
.key.round.pressed { filter: brightness(1.3); }
.key.sh { width: 92px; height: 38px; border-radius: 12px; font-size: .9rem; }
.key.pill { padding: 9px 14px; border-radius: 999px; font-size: .68rem; letter-spacing: .1em; color: rgba(255, 255, 255, .7); }
.key.menu { width: 42px; height: 34px; border-radius: 12px; display: grid; place-items: center; }
.key.menu svg { width: 20px; height: 20px; }
.dpad { position: relative; width: 156px; height: 156px; touch-action: none; }
.dpad .arm, .dpad .hub {
  position: absolute; background: rgba(255, 255, 255, .11); box-shadow: inset 0 1px 0 rgba(255, 255, 255, .14);
  backdrop-filter: blur(8px); -webkit-backdrop-filter: blur(8px);
}
.dpad .arm.up, .dpad .arm.down { left: 52px; width: 52px; height: 56px; }
.dpad .arm.left, .dpad .arm.right { top: 52px; width: 56px; height: 52px; }
.dpad .arm.up { top: 0; border-radius: 12px 12px 0 0; }
.dpad .arm.down { bottom: 0; border-radius: 0 0 12px 12px; }
.dpad .arm.left { left: 0; border-radius: 12px 0 0 12px; }
.dpad .arm.right { right: 0; border-radius: 0 12px 12px 0; }
.dpad .hub { left: 52px; top: 52px; width: 52px; height: 52px; background: rgba(255, 255, 255, .08); box-shadow: none; }
.dpad .arm::after { content: ""; position: absolute; inset: 0; margin: auto; width: 0; height: 0; border: 8px solid transparent; opacity: .55; }
.dpad .up::after { border-bottom-color: #fff; border-top-width: 0; }
.dpad .down::after { border-top-color: #fff; border-bottom-width: 0; }
.dpad .left::after { border-right-color: #fff; border-left-width: 0; }
.dpad .right::after { border-left-color: #fff; border-right-width: 0; }
.dpad .arm.on { background: rgba(255, 255, 255, .3); }
.ab { display: flex; gap: 18px; align-items: center; transform: rotate(-22deg); }
.ab .a { margin-bottom: 46px; }
.mid { display: flex; justify-content: center; align-items: center; gap: 14px; }

/* ===== celular em pé (estilo Delta): tela de ponta a ponta + controle embaixo ===== */
body.portrait.pad-on .stage { flex: 0 0 auto; padding: var(--sat) 0 0; }
body.portrait.pad-on .screen { border-radius: 0; box-shadow: 0 18px 40px rgba(0, 0, 0, .55); }
body.portrait.pad-on .pad {
  position: relative; flex: 1; min-height: 0;
  display: grid; grid-template-columns: 1fr 1fr; grid-template-rows: auto 1fr auto;
  grid-template-areas: "l r" "dpad ab" "mid mid";
  padding: 14px calc(16px + var(--sar)) calc(14px + var(--sab)) calc(16px + var(--sal));
}
body.portrait.pad-on .key.l { grid-area: l; justify-self: start; }
body.portrait.pad-on .key.r { grid-area: r; justify-self: end; }
body.portrait.pad-on .dpad { grid-area: dpad; align-self: center; justify-self: start; }
body.portrait.pad-on .ab { grid-area: ab; align-self: center; justify-self: end; margin-right: 8px; }
body.portrait.pad-on .mid { grid-area: mid; }
body.portrait.pad-on .corner { display: none; }        /* no celular o menu fica no controle */
@media (max-height: 700px) {
  body.portrait.pad-on .key.round { width: 62px; height: 62px; }
  body.portrait.pad-on .dpad { transform: scale(.86); transform-origin: left center; }
  body.portrait.pad-on .ab .a { margin-bottom: 34px; }
}

/* ===== celular deitado: tela na altura toda, botões por cima ===== */
body.landscape.pad-on .stage { padding: 0; }
body.landscape.pad-on .screen { border-radius: 0; }
body.landscape.pad-on .pad { position: fixed; inset: 0; z-index: 3; pointer-events: none; }
body.landscape.pad-on .pad > * { pointer-events: auto; position: absolute; opacity: .78; }
body.landscape.pad-on .key.l { top: calc(12px + var(--sat)); left: calc(14px + var(--sal)); }
body.landscape.pad-on .key.r { top: calc(12px + var(--sat)); right: calc(14px + var(--sar)); }
body.landscape.pad-on .dpad { left: calc(18px + var(--sal)); bottom: calc(22px + var(--sab)); }
body.landscape.pad-on .ab { right: calc(26px + var(--sar)); bottom: calc(30px + var(--sab)); }
body.landscape.pad-on .mid { left: 50%; bottom: calc(8px + var(--sab)); transform: translateX(-50%); }
body.landscape.pad-on .corner { display: none; }
body.landscape.pad-on .dpad { transform: scale(.78); transform-origin: left bottom; }
body.landscape.pad-on .key.round { width: 58px; height: 58px; font-size: 1.1rem; }
body.landscape.pad-on .ab .a { margin-bottom: 34px; }
body.landscape.pad-on .key.sh { width: 76px; height: 32px; }
body.landscape.pad-on .toast { font-size: .78rem; }
body.portrait.pad-on .toast { top: auto; bottom: calc(96px + var(--sab)); }

/* desktop com controle na tela ligado */
body.desktop.pad-on .pad {
  position: relative; flex: 0 0 auto; display: grid; grid-template-columns: auto 1fr auto; grid-template-rows: auto auto;
  grid-template-areas: "l mid r" "dpad mid ab"; align-items: center; gap: 8px 24px;
  width: min(760px, 100%); margin: 0 auto 18px; padding: 12px 22px;
}
body.desktop.pad-on .key.l { grid-area: l; }
body.desktop.pad-on .key.r { grid-area: r; justify-self: end; }
body.desktop.pad-on .dpad { grid-area: dpad; transform: scale(.8); transform-origin: left center; }
body.desktop.pad-on .ab { grid-area: ab; }
body.desktop.pad-on .mid { grid-area: mid; }

/* ---------- configurações ---------- */
.sheet {
  width: min(520px, calc(100vw - 24px)); max-height: min(86vh, 760px); padding: 0; margin: auto;
  color: var(--text); background: var(--panel); border: 1px solid var(--line); border-radius: 18px;
  box-shadow: 0 40px 120px rgba(0, 0, 0, .7); backdrop-filter: blur(18px); -webkit-backdrop-filter: blur(18px);
  overflow: hidden; flex-direction: column;
}
.sheet[open] { display: flex; }
.sheet::backdrop { background: rgba(0, 0, 0, .45); backdrop-filter: blur(3px); }
@media (max-width: 600px) {
  .sheet { width: 100vw; max-width: 100vw; max-height: 88vh; margin: auto 0 0; border-radius: 20px 20px 0 0; border-bottom: 0; padding-bottom: var(--sab); }
}
.sheet-head { display: flex; align-items: center; gap: 10px; padding: 14px 14px 10px 18px; }
.sheet-head h2 { margin: 0; flex: 1; font-size: 1.05rem; letter-spacing: .02em; }
.sheet-head h2 span { color: var(--accent); }
.sheet-head select { width: auto; padding: 6px 8px; }
.tabs { display: flex; padding: 0 8px; border-bottom: 1px solid var(--line); overflow-x: auto; scrollbar-width: none; flex: 0 0 auto; }
.tabs button {
  flex: 1; padding: 10px 8px; background: none; border: 0; border-bottom: 2px solid transparent;
  color: var(--muted); font-weight: 600; font-size: .86rem; cursor: pointer; white-space: nowrap;
}
.tabs button[aria-selected="true"] { color: var(--text); border-bottom-color: var(--accent); }
.panel { padding: 14px 18px 20px; display: flex; flex-direction: column; gap: 10px; overflow-y: auto; }
.panel h3 { margin: 8px 0 0; font-size: .74rem; text-transform: uppercase; letter-spacing: .09em; color: var(--muted); }
.note { margin: 0; color: var(--muted); font-size: .84rem; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.btn {
  appearance: none; display: inline-flex; align-items: center; justify-content: center; gap: 6px;
  padding: 10px 12px; font-weight: 600; font-size: .9rem; color: var(--text); cursor: pointer;
  background: rgba(255, 255, 255, .06); border: 1px solid rgba(255, 255, 255, .12); border-radius: 11px;
}
.btn:hover { background: rgba(255, 255, 255, .1); }
.btn.primary { background: var(--accent); border-color: #ff6164; color: #fff; }
.btn.on { background: rgba(88, 114, 176, .35); border-color: #5872b0; }
.btn.small { padding: 8px 10px; }
.btn.file { position: relative; overflow: hidden; }
.btn.file input { position: absolute; inset: 0; opacity: 0; cursor: pointer; }
.slider-row { display: flex; align-items: center; gap: 10px; }
.slider-row input { flex: 1; accent-color: var(--accent); }
select { width: 100%; padding: 9px; background: rgba(255, 255, 255, .06); border: 1px solid rgba(255, 255, 255, .14); border-radius: 10px; }
select option { background: #1a1c25; }
.slots { display: grid; grid-template-columns: repeat(4, 1fr); gap: 6px; }
.slots label { text-align: center; padding: 7px; border: 1px solid var(--line); border-radius: 10px; cursor: pointer; }
.slots label:has(input:checked) { border-color: var(--accent); background: rgba(229, 56, 59, .14); }
.slots input, .seg input { display: none; }
.choices { display: flex; flex-direction: column; gap: 6px; }
.choices label {
  display: grid; grid-template-columns: auto auto 1fr; align-items: baseline; gap: 8px;
  padding: 9px 11px; border: 1px solid var(--line); border-radius: 10px; cursor: pointer;
}
.choices.compact label { display: flex; align-items: center; }
.choices label:has(input:checked) { border-color: rgba(229, 56, 59, .7); background: rgba(229, 56, 59, .09); }
.choices span { color: var(--muted); font-size: .8rem; text-align: right; }
.choices.compact span { color: var(--text); font-size: .88rem; text-align: left; }
.choices input { accent-color: var(--accent); }
.seg { display: grid; grid-template-columns: repeat(3, 1fr); padding: 3px; background: rgba(255, 255, 255, .05); border-radius: 11px; }
.seg label { text-align: center; padding: 7px; border-radius: 9px; cursor: pointer; font-size: .88rem; }
.seg label:has(input:checked) { background: rgba(255, 255, 255, .14); }
.toggles { display: flex; flex-direction: column; gap: 8px; }
.toggle { display: grid; grid-template-columns: 1fr auto; align-items: center; gap: 10px; padding: 10px 12px; border: 1px solid var(--line); border-radius: 11px; cursor: pointer; }
.toggle b { display: block; font-size: .92rem; }
.toggle small { color: var(--muted); }
.switch { position: relative; width: 42px; height: 24px; }
.switch input { position: absolute; opacity: 0; inset: 0; margin: 0; cursor: pointer; }
.switch i { position: absolute; inset: 0; background: rgba(255, 255, 255, .16); border-radius: 99px; transition: background .15s; }
.switch i::after { content: ""; position: absolute; top: 3px; left: 3px; width: 18px; height: 18px; border-radius: 50%; background: #fff; transition: transform .15s; }
.switch input:checked + i { background: var(--accent); }
.switch input:checked + i::after { transform: translateX(18px); }
.console {
  margin: 0; max-height: 200px; overflow: auto; padding: 10px; background: #0a0b0e; border: 1px solid var(--line);
  border-radius: 10px; font: 12px/1.45 ui-monospace, Menlo, monospace; color: #b8f2c9; white-space: pre-wrap; -webkit-user-select: text; user-select: text;
}
.keys { width: 100%; border-collapse: collapse; font-size: .88rem; }
.keys td { padding: 6px 4px; border-bottom: 1px solid var(--line); }
.keys td:first-child { white-space: nowrap; width: 46%; }
.keys tr.sep td { border-top: 2px solid var(--line); }
.fixes { margin: 0; padding-left: 18px; color: var(--muted); font-size: .86rem; display: flex; flex-direction: column; gap: 6px; }
.fixes b { color: var(--text); }
.version { font-size: .75rem; opacity: .7; }
