:root {
  --bg: #05040a;
  --accent: #39ff88;
  --gold: #f5c518;
  --cyan: #00d2d3;
  --danger: #ff4757;
  --pixel-font: 'Press Start 2P', 'Courier New', monospace;
  --font: system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
  --mono: ui-monospace, "SF Mono", Menlo, Consolas, monospace;
}

/* Proteção global inegociável contra seleção e cópia para não atrapalhar o gameplay */
*,
*::before,
*::after {
  box-sizing: border-box;
  margin: 0;
  padding: 0;
  user-select: none !important;
  -webkit-user-select: none !important;
  -moz-user-select: none !important;
  -ms-user-select: none !important;
  -webkit-touch-callout: none !important;
  -webkit-tap-highlight-color: transparent !important;
  -webkit-user-drag: none !important;
}

/* Garante que elementos com atributo hidden sejam de fato ocultados contra display: flex */
[hidden],
#emulator-container[hidden],
.screen-state[hidden],
#save-modal[hidden],
#btn-save-menu[hidden],
#save-feedback[hidden] {
  display: none !important;
}

html,
body {
  width: 100%;
  height: 100%;
  overflow: hidden;
  background: var(--bg);
  font-family: var(--font);
  display: flex;
  align-items: center;
  justify-content: center;
  touch-action: none;
}

/* O gabinete tem proporção nativa de 1114 / 2344 */
.cabinet-wrapper {
  position: relative;
  height: 100dvh;
  aspect-ratio: 1114 / 2344;
  max-width: 100vw;
  max-height: 100vh;
  margin: 0 auto;
  overflow: hidden;
  box-shadow: 0 0 50px rgba(0, 0, 0, 0.95);
  background: #000;
  touch-action: none;
}

/* 1. Camada da Tela (fica atrás da moldura, dentro do recorte da tela) */
.screen-layer {
  position: absolute;
  top: 10.75%;
  left: 4.3%;
  width: 91.4%;
  height: 54.8%;
  z-index: 10;
  background: #000;
  border-radius: 36px;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
}

/* Efeito CRT Retro Arcade Minimalista */
.crt-overlay {
  position: absolute;
  inset: 0;
  pointer-events: none;
  z-index: 15;
  background:
    linear-gradient(rgba(18, 16, 16, 0) 50%, rgba(0, 0, 0, 0.4) 50%),
    linear-gradient(90deg, rgba(255, 0, 0, 0.03), rgba(0, 255, 0, 0.01), rgba(0, 0, 255, 0.03));
  background-size: 100% 3px, 6px 100%;
  box-shadow: inset 0 0 45px rgba(0, 0, 0, 0.95), inset 0 0 10px rgba(0, 255, 136, 0.15);
  border-radius: inherit;
}

/* Telas de estado dentro do recorte */
.screen-state {
  width: 100%;
  height: 100%;
  position: relative;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  text-align: center;
  padding: 1.5rem;
  color: #fff;
  font-family: var(--pixel-font);
}

.empty-state {
  background: radial-gradient(circle at center, #0a1118 0%, #020305 100%);
  gap: 1.6rem;
}

.arcade-badge {
  font-size: 0.65rem;
  letter-spacing: 0.18em;
  color: var(--accent);
  text-shadow: 0 0 8px rgba(57, 255, 136, 0.7);
  padding: 4px 10px;
  border: 1px dashed rgba(57, 255, 136, 0.5);
  border-radius: 3px;
  text-transform: uppercase;
}

.empty-title {
  font-size: 0.85rem;
  line-height: 1.7;
  color: var(--gold);
  text-shadow: 0 0 12px rgba(245, 197, 24, 0.8);
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.empty-desc {
  font-size: 0.62rem;
  line-height: 1.8;
  color: var(--gold);
  text-shadow: 0 0 10px rgba(245, 197, 24, 0.7);
  max-width: 18rem;
  letter-spacing: 0.06em;
  text-transform: uppercase;
}

.loading-text {
  font-family: var(--pixel-font);
  font-size: 0.8rem;
  line-height: 1.8;
  color: var(--accent);
  text-shadow: 0 0 12px rgba(57, 255, 136, 0.85);
  letter-spacing: 0.1em;
  text-transform: uppercase;
}

.arcade-credit {
  font-size: 0.5rem;
  color: rgba(255, 255, 255, 0.4);
  letter-spacing: 0.12em;
  text-transform: uppercase;
  margin-top: 0.5rem;
}

.blink {
  animation: crtBlink 1.2s step-start infinite;
}

@keyframes crtBlink {
  50% {
    opacity: 0;
  }
}

/* Container do emulador - Preenchimento total do monitor */
#emulator-container {
  width: 100%;
  height: 100%;
  position: absolute;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: #000;
}

#emulator-container canvas {
  width: 100% !important;
  height: 100% !important;
  object-fit: fill !important;
  display: block;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

/* 2. Camada da Moldura Física do Gabinete (sobreposta ao canvas) */
.frame-layer {
  position: absolute;
  inset: 0;
  width: 100%;
  height: 100%;
  z-index: 20;
  pointer-events: none;
  background-image: url('assets/template1_frame.png');
  background-size: 100% 100%;
  background-repeat: no-repeat;
}

/* 3. Camada de Controles (sobrepostos à imagem e interativos) */
.controls-layer {
  position: absolute;
  inset: 0;
  z-index: 30;
  pointer-events: none;
  touch-action: none;
}

/* Botões de Ombro (L e R em Retrato): posicionados confortavelmente ACIMA dos botões */
.shoulder-buttons {
  position: absolute;
  top: 70.8%;
  left: 8%;
  right: 8%;
  display: flex;
  justify-content: space-between;
  pointer-events: auto;
}

.shoulder-btn {
  width: 26%;
  height: 2.2rem;
  background: rgba(30, 26, 48, 0.85);
  border: 1px solid rgba(255, 255, 255, 0.25);
  border-radius: 10px;
  color: #fff;
  font-family: var(--pixel-font);
  font-size: 0.75rem;
  box-shadow: 0 4px 10px rgba(0, 0, 0, 0.6);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.05s ease, background 0.1s ease;
}

.shoulder-btn:active,
.shoulder-btn.active {
  background: var(--accent);
  color: #000;
  transform: scale(0.95);
  box-shadow: 0 0 15px var(--accent);
}

/* D-Pad (direcionais na esquerda inferior): proporção equilibrada e gap amplo */
.dpad-container {
  position: absolute;
  left: 8%;
  bottom: 7%;
  width: 31%;
  aspect-ratio: 1 / 1;
  pointer-events: auto;
}

.dpad-btn {
  position: absolute;
  background: rgba(28, 25, 45, 0.85);
  border: 1px solid rgba(255, 255, 255, 0.2);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.6);
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #fff;
  font-size: 1.15rem;
  transition: background 0.05s ease;
}

.dpad-btn:active,
.dpad-btn.active {
  background: var(--accent);
  color: #000;
  box-shadow: 0 0 15px var(--accent);
}

.dpad-up {
  top: 0;
  left: 33.33%;
  width: 33.33%;
  height: 33.33%;
  border-radius: 8px 8px 0 0;
}

.dpad-down {
  bottom: 0;
  left: 33.33%;
  width: 33.33%;
  height: 33.33%;
  border-radius: 0 0 8px 8px;
}

.dpad-left {
  left: 0;
  top: 33.33%;
  width: 33.33%;
  height: 33.33%;
  border-radius: 8px 0 0 8px;
}

.dpad-right {
  right: 0;
  top: 33.33%;
  width: 33.33%;
  height: 33.33%;
  border-radius: 0 8px 8px 0;
}

.dpad-center {
  left: 33.33%;
  top: 33.33%;
  width: 33.33%;
  height: 33.33%;
  background: #121020;
}

/* Joystick Analógico Virtual (substitui D-Pad para jogos com useJoystick) */
.joystick-container {
  position: absolute;
  left: 8%;
  bottom: 7%;
  width: 31%;
  aspect-ratio: 1 / 1;
  pointer-events: auto;
  user-select: none;
  touch-action: none;
  display: flex;
  align-items: center;
  justify-content: center;
}

.joystick-container[hidden] {
  display: none !important;
}

.joystick-base {
  width: 100%;
  height: 100%;
  border-radius: 50%;
  background: radial-gradient(circle at center, rgba(32, 27, 54, 0.9) 0%, rgba(12, 10, 22, 0.95) 100%);
  border: 2px solid rgba(57, 255, 136, 0.35);
  box-shadow: 0 6px 20px rgba(0, 0, 0, 0.8), inset 0 0 20px rgba(0, 0, 0, 0.9), 0 0 10px rgba(57, 255, 136, 0.15);
  position: relative;
  touch-action: none;
  overflow: visible;
  display: flex;
  align-items: center;
  justify-content: center;
}

.joystick-guide-line {
  position: absolute;
  background: rgba(255, 255, 255, 0.1);
  pointer-events: none;
}

.joystick-guide-line.guide-v {
  top: 10%;
  bottom: 10%;
  left: 50%;
  width: 1px;
  transform: translateX(-50%);
}

.joystick-guide-line.guide-h {
  left: 10%;
  right: 10%;
  top: 50%;
  height: 1px;
  transform: translateY(-50%);
}

.joystick-ring {
  position: absolute;
  inset: 22%;
  border-radius: 50%;
  border: 1px dashed rgba(57, 255, 136, 0.25);
  pointer-events: none;
}

.joystick-stick {
  position: absolute;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  width: 48%;
  height: 48%;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 35%, #423d68 0%, #1a172e 70%, #0d0b17 100%);
  border: 2px solid var(--accent);
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.8), 0 0 12px rgba(57, 255, 136, 0.3);
  pointer-events: none;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: transform 0.04s ease-out;
  will-change: transform;
}

.joystick-stick.active {
  box-shadow: 0 4px 18px rgba(0, 0, 0, 0.9), 0 0 20px rgba(57, 255, 136, 0.7);
  border-color: #55ff99;
}

.joystick-stick-center {
  width: 35%;
  height: 35%;
  border-radius: 50%;
  background: rgba(57, 255, 136, 0.25);
  border: 1px solid rgba(57, 255, 136, 0.5);
}

/* Botão pequeno de configurações do joystick */
.btn-joystick-settings {
  position: absolute;
  top: -0.75rem;
  right: -0.75rem;
  width: 2rem;
  height: 2rem;
  border-radius: 50%;
  background: rgba(14, 11, 26, 0.95);
  border: 1.5px solid var(--gold);
  color: #fff;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  box-shadow: 0 2px 10px rgba(0, 0, 0, 0.7), 0 0 8px rgba(245, 197, 24, 0.3);
  font-size: 0.85rem;
  pointer-events: auto;
  z-index: 40;
  transition: transform 0.1s ease, box-shadow 0.1s ease;
}

.btn-joystick-settings:active {
  transform: scale(0.9);
  box-shadow: 0 0 15px var(--gold);
}

/* Botões de Ação (A, B, X, Y na direita inferior): com espaçamento gap generoso do D-Pad */
.action-buttons {
  position: absolute;
  right: 8%;
  bottom: 7%;
  width: 31%;
  aspect-ratio: 1 / 1;
  pointer-events: auto;
}

.action-btn {
  position: absolute;
  width: 37%;
  height: 37%;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--pixel-font);
  font-size: 0.75rem;
  color: #fff;
  cursor: pointer;
  border: 2px solid rgba(255, 255, 255, 0.3);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.7);
  transition: transform 0.05s ease;
}

.action-btn:active,
.action-btn.active {
  transform: scale(0.92);
  filter: brightness(1.3);
}

.btn-y {
  left: 0;
  top: 31.5%;
  background: radial-gradient(circle, #2ed573, #1e90ff);
}

.btn-x {
  left: 31.5%;
  top: 0;
  background: radial-gradient(circle, #3742fa, #2f3542);
}

.btn-b {
  left: 31.5%;
  bottom: 0;
  background: radial-gradient(circle, #ffa502, #ff6348);
}

.btn-a {
  right: 0;
  top: 31.5%;
  background: radial-gradient(circle, #ff4757, #c0392b);
}

/* Start e Select no centro inferior com espaçamento ideal */
.menu-buttons {
  position: absolute;
  left: 50%;
  transform: translateX(-50%);
  bottom: 2.5%;
  width: 22%;
  display: flex;
  justify-content: space-between;
  pointer-events: auto;
}

.menu-btn {
  width: 44%;
  height: 0.85rem;
  background: #2a2c38;
  border: 1px solid #4f5367;
  border-radius: 10px;
  transform: rotate(-25deg);
  cursor: pointer;
  position: relative;
}

.menu-btn::after {
  content: attr(data-label);
  position: absolute;
  top: 1.05rem;
  left: 50%;
  transform: translateX(-50%) rotate(25deg);
  font-size: 0.45rem;
  font-family: var(--pixel-font);
  font-weight: 700;
  color: #929bb0;
  letter-spacing: 0.05em;
}

.menu-btn:active,
.menu-btn.active {
  background: var(--accent);
}

/* Layout específico para NES (Apenas B e A) */
[data-console="nes"] .action-buttons {
  width: 32%;
}

[data-console="nes"] .btn-b {
  left: 0;
  bottom: 15%;
}

[data-console="nes"] .btn-a {
  right: 0;
  top: 15%;
}

/* 4. MODO PAISAGEM (T22) */
@media (orientation: landscape) {
  .cabinet-wrapper {
    width: 100vw !important;
    height: 100vh !important;
    max-width: 100vw !important;
    max-height: 100vh !important;
    aspect-ratio: unset !important;
    background: #000;
  }

  .screen-layer {
    position: fixed;
    inset: 0;
    width: 100vw;
    height: 100vh;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #000;
    z-index: 10;
  }

  #emulator-container {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    height: 100%;
  }

  #emulator-container canvas {
    width: auto !important;
    height: 100vh !important;
    aspect-ratio: 4 / 3;
    object-fit: contain;
  }

  /* Moldura vertical de retrato oculta em paisagem */
  .frame-layer {
    display: none !important;
  }

  /* HUD translúcido sobreposto nas laterais */
  .controls-layer {
    position: fixed;
    inset: 0;
    pointer-events: none;
    z-index: 30;
  }

  .dpad-container,
  .joystick-container {
    left: 2.5rem;
    bottom: 2.5rem;
    width: 10rem;
    height: 10rem;
    pointer-events: auto;
  }

  .dpad-btn {
    background: rgba(255, 255, 255, 0.15);
    backdrop-filter: blur(8px);
    border: 1px solid rgba(255, 255, 255, 0.25);
  }

  .action-buttons {
    right: 2.5rem;
    bottom: 2.5rem;
    width: 10rem;
    height: 10rem;
    pointer-events: auto;
  }

  .action-btn {
    background: rgba(255, 255, 255, 0.18);
    backdrop-filter: blur(8px);
    border: 1px solid rgba(255, 255, 255, 0.3);
  }

  .shoulder-buttons {
    position: fixed;
    top: 1.5rem;
    left: 0;
    right: 0;
    padding: 0 2.5rem;
    pointer-events: auto;
  }

  .shoulder-btn {
    width: 5.5rem;
    height: 2.5rem;
    background: rgba(255, 255, 255, 0.15);
    backdrop-filter: blur(8px);
  }

  .menu-buttons {
    position: fixed;
    bottom: 1.5rem;
    left: 50%;
    transform: translateX(-50%);
    width: 9rem;
    pointer-events: auto;
  }

  .save-menu-trigger {
    position: fixed;
    top: 1rem;
    right: 1.5rem;
    z-index: 45;
  }
}

/* --- Botão Discreto de Opções / Save Retrô --- */
.save-menu-trigger {
  position: absolute;
  top: 3.2%;
  right: 5%;
  z-index: 35;
  display: flex;
  align-items: center;
  gap: 0.35rem;
  padding: 0.3rem 0.65rem;
  background: rgba(14, 11, 26, 0.9);
  border: 1.5px solid var(--accent);
  border-radius: 6px;
  color: var(--accent);
  font-family: var(--pixel-font);
  font-size: 0.55rem;
  letter-spacing: 0.05em;
  cursor: pointer;
  box-shadow: 0 0 12px rgba(57, 255, 136, 0.3), inset 0 0 6px rgba(57, 255, 136, 0.15);
  transition: transform 0.08s ease, background 0.15s ease, box-shadow 0.15s ease;
  pointer-events: auto;
}

.save-menu-trigger:active {
  transform: scale(0.92);
  background: var(--accent);
  color: #000;
  box-shadow: 0 0 18px var(--accent);
}

.save-menu-trigger .save-icon {
  font-size: 0.75rem;
}

/* --- Modal / Tela Retrô de Save (Memory Card) --- */
.save-modal-overlay {
  position: absolute;
  inset: 0;
  z-index: 60;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 1.25rem;
  pointer-events: auto;
}

.save-modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(0, 0, 0, 0.82);
  backdrop-filter: blur(6px);
}

.save-modal-card {
  position: relative;
  width: 100%;
  max-width: 22rem;
  background: linear-gradient(180deg, #130f28 0%, #070512 100%);
  border: 2px solid var(--accent);
  border-radius: 14px;
  box-shadow: 0 0 30px rgba(57, 255, 136, 0.35), inset 0 0 15px rgba(0, 0, 0, 0.85);
  overflow: hidden;
  z-index: 2;
  font-family: var(--pixel-font);
  color: #fff;
}

.save-modal-crt {
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: linear-gradient(rgba(18, 16, 16, 0) 50%, rgba(0, 0, 0, 0.35) 50%);
  background-size: 100% 3px;
  opacity: 0.7;
}

.save-modal-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 0.85rem 1rem;
  background: rgba(255, 255, 255, 0.04);
  border-bottom: 1.5px solid rgba(57, 255, 136, 0.25);
}

.save-modal-header-title {
  display: flex;
  align-items: center;
  gap: 0.5rem;
}

.save-modal-header-title h2 {
  font-size: 0.65rem;
  color: var(--gold);
  letter-spacing: 0.05em;
  margin: 0;
}

.save-modal-close {
  background: transparent;
  border: none;
  color: #a4b0be;
  font-size: 0.9rem;
  cursor: pointer;
  padding: 0.2rem 0.4rem;
  border-radius: 4px;
  transition: color 0.1s ease;
}

.save-modal-close:hover,
.save-modal-close:active {
  color: var(--danger);
}

.save-modal-body {
  padding: 1rem;
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
}

.save-slot-card {
  background: rgba(0, 0, 0, 0.55);
  border: 1px solid rgba(255, 255, 255, 0.12);
  border-radius: 8px;
  padding: 0.75rem;
  display: flex;
  flex-direction: column;
  gap: 0.4rem;
}

.save-slot-top {
  display: flex;
  align-items: center;
  justify-content: space-between;
}

.save-slot-badge {
  font-size: 0.55rem;
  background: rgba(57, 255, 136, 0.15);
  color: var(--accent);
  border: 1px solid rgba(57, 255, 136, 0.35);
  padding: 0.2rem 0.4rem;
  border-radius: 4px;
}

.save-slot-time {
  font-size: 0.5rem;
  color: #929bb0;
}

.save-slot-title {
  font-size: 0.65rem;
  color: #fff;
  letter-spacing: 0.05em;
  margin-top: 0.2rem;
}

.save-slot-desc {
  font-family: var(--font);
  font-size: 0.65rem;
  color: #8b87a0;
  line-height: 1.3;
}

.save-feedback {
  font-size: 0.55rem;
  text-align: center;
  padding: 0.5rem;
  border-radius: 6px;
  letter-spacing: 0.05em;
  animation: crtBlink 1.5s infinite;
}

.save-feedback.success {
  background: rgba(57, 255, 136, 0.15);
  border: 1px solid var(--accent);
  color: var(--accent);
}

.save-feedback.error {
  background: rgba(255, 71, 87, 0.15);
  border: 1px solid var(--danger);
  color: var(--danger);
}

.save-modal-actions {
  display: flex;
  flex-direction: column;
  gap: 0.5rem;
  margin-top: 0.2rem;
}

.save-btn {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 0.5rem;
  padding: 0.65rem;
  border-radius: 8px;
  font-family: var(--pixel-font);
  font-size: 0.55rem;
  letter-spacing: 0.05em;
  cursor: pointer;
  border: none;
  transition: transform 0.06s ease, filter 0.1s ease;
  width: 100%;
}

.save-btn:active:not(:disabled) {
  transform: scale(0.97);
}

.save-btn:disabled {
  opacity: 0.35;
  cursor: not-allowed;
  filter: grayscale(0.8);
}

.btn-primary {
  background: linear-gradient(135deg, #10ac84 0%, #1dd1a1 100%);
  color: #000;
  font-weight: bold;
  box-shadow: 0 2px 8px rgba(29, 209, 161, 0.4);
}

.btn-secondary {
  background: linear-gradient(135deg, #2e86de 0%, #54a0ff 100%);
  color: #fff;
  box-shadow: 0 2px 8px rgba(84, 160, 255, 0.35);
}

.btn-warning {
  background: rgba(255, 71, 87, 0.15);
  border: 1px solid var(--danger);
  color: var(--danger);
}

.btn-muted {
  background: rgba(255, 255, 255, 0.08);
  border: 1px solid rgba(255, 255, 255, 0.2);
  color: #dcdde1;
}

/* --- Controles do Modal de Calibração do Joystick --- */
.joystick-settings-controls {
  display: flex;
  flex-direction: column;
  gap: 0.85rem;
  margin: 0.8rem 0;
  padding: 0.75rem;
  background: rgba(0, 0, 0, 0.45);
  border: 1px solid rgba(255, 255, 255, 0.08);
  border-radius: 8px;
}

.joystick-slider-group {
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.joystick-slider-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-family: var(--pixel-font);
  font-size: 0.52rem;
  color: #dcdde1;
  letter-spacing: 0.04em;
}

.joystick-slider-val {
  font-family: var(--pixel-font);
  font-size: 0.6rem;
  color: var(--accent);
  text-shadow: 0 0 6px rgba(57, 255, 136, 0.5);
}

.retro-slider {
  width: 100%;
  height: 6px;
  border-radius: 3px;
  background: #25203f;
  outline: none;
  cursor: pointer;
  accent-color: var(--accent);
}