*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg: #0f0f1a;
  --panel: #1a1a2e;
  --panel2: #2d2d44;
  --accent: #4a9eff;
  --red: #ff4a6a;
  --text: #e0e0e0;
  --muted: #666;
  --debug-bg: #0a0a14;
  --debug-text: #33ff88;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: 'JetBrains Mono', monospace;
  min-height: 100vh;
  overflow-x: hidden;
}

#app {
  display: flex;
  flex-direction: column;
  align-items: center;
  min-height: 100vh;
}

#header {
  width: 100%;
  background: linear-gradient(180deg, #16162b 0%, #0f0f1a 100%);
  padding: 12px 0;
  text-align: center;
  border-bottom: 1px solid #2a2a40;
  position: relative;
}

#header::after {
  content: '';
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  background: repeating-linear-gradient(
    0deg,
    transparent,
    transparent 2px,
    rgba(0,0,0,0.08) 2px,
    rgba(0,0,0,0.08) 4px
  );
  pointer-events: none;
}

#header h1 {
  font-family: 'Press Start 2P', monospace;
  font-size: 18px;
  color: var(--accent);
  text-shadow: 0 0 10px rgba(74,158,255,0.4);
}

.subtitle {
  font-size: 10px;
  color: var(--muted);
  margin-top: 4px;
  font-family: 'Press Start 2P', monospace;
}

#main {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 16px;
  gap: 12px;
  width: 100%;
  max-width: 900px;
}

.upload-zone {
  border: 2px dashed var(--panel2);
  border-radius: 12px;
  padding: 40px 60px;
  text-align: center;
  cursor: pointer;
  transition: all 0.2s;
  background: rgba(26,26,46,0.5);
}

.upload-zone:hover, .upload-zone.drag-over {
  border-color: var(--accent);
  background: rgba(74,158,255,0.05);
}

.cartridge-icon { margin-bottom: 12px; }

.upload-text {
  font-size: 14px;
  margin-bottom: 6px;
}

.upload-hint {
  font-size: 11px;
  color: var(--muted);
}

.rom-info {
  font-size: 12px;
  color: var(--accent);
  padding: 6px 16px;
  background: var(--panel);
  border-radius: 6px;
}

.rom-info span + span::before {
  content: ' • ';
  color: var(--muted);
}

.hidden { display: none !important; }

.screen-wrapper {
  display: flex;
  justify-content: center;
  width: 100%;
}

.crt-bezel {
  position: relative;
  background: linear-gradient(145deg, #2a2a2a 0%, #111 50%, #000 100%);
  padding: 16px;
  border-radius: 16px;
  box-shadow:
    0 0 30px rgba(0,0,0,0.8),
    inset 0 0 20px rgba(0,0,0,0.5),
    0 0 60px rgba(74,158,255,0.08);
  display: inline-block;
}

.crt-bezel.crt-curve {
  border-radius: 24px;
}

.crt-bezel.crt-curve #screen {
  border-radius: 8px;
}

#screen {
  display: block;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  background: #000;
  width: 768px;
  height: 576px;
}

#screen.smooth {
  image-rendering: auto;
}

.scanline-overlay {
  position: absolute;
  top: 16px;
  left: 16px;
  right: 16px;
  bottom: 16px;
  background: repeating-linear-gradient(
    0deg,
    transparent,
    transparent 1px,
    rgba(0,0,0,0.15) 1px,
    rgba(0,0,0,0.15) 2px
  );
  pointer-events: none;
  z-index: 2;
}

.scanline-overlay.off { display: none; }

.controls {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: center;
}

.ctrl-btn {
  background: var(--panel2);
  border: 1px solid rgba(255,255,255,0.08);
  color: var(--text);
  font-family: 'JetBrains Mono', monospace;
  font-size: 14px;
  padding: 8px 14px;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.1s;
  user-select: none;
}

.ctrl-btn:hover { background: #3a3a58; }
.ctrl-btn:active { transform: scale(0.95); }
.ctrl-btn.active { background: var(--accent); color: #000; }

.volume-slider {
  width: 80px;
  accent-color: var(--accent);
  cursor: pointer;
}

/* Virtual Gamepad */
.virtual-gamepad {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  max-width: 500px;
  padding: 16px;
}

.dpad {
  display: grid;
  grid-template-columns: 44px 44px 44px;
  grid-template-rows: 44px 44px 44px;
  gap: 2px;
}

.dpad-btn {
  background: rgba(45,45,68,0.8);
  border: 1px solid rgba(255,255,255,0.1);
  color: var(--text);
  font-size: 16px;
  border-radius: 6px;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}

.dpad-center { background: rgba(30,30,50,0.5); border: none; }
.dpad-up { grid-column: 2; grid-row: 1; }
.dpad-left { grid-column: 1; grid-row: 2; }
.dpad-center { grid-column: 2; grid-row: 2; }
.dpad-right { grid-column: 3; grid-row: 2; }
.dpad-down { grid-column: 2; grid-row: 3; }

.action-buttons {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

.ab-buttons {
  display: flex;
  gap: 16px;
}

.action-btn {
  background: rgba(45,45,68,0.8);
  border: 1px solid rgba(255,255,255,0.1);
  color: var(--text);
  font-family: 'Press Start 2P', monospace;
  font-size: 10px;
  padding: 12px 16px;
  border-radius: 50%;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
  min-width: 48px;
  min-height: 48px;
}

.action-btn[data-btn="start"] {
  border-radius: 20px;
  padding: 8px 20px;
}

.btn-1 { background: rgba(74,158,255,0.3); }
.btn-2 { background: rgba(255,74,106,0.3); }

/* Debug Panel */
.debug-panel {
  width: 100%;
  max-width: 900px;
  background: var(--debug-bg);
  border: 1px solid #1a2a1a;
  border-radius: 8px;
  padding: 12px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  font-size: 11px;
  color: var(--debug-text);
  max-height: 400px;
  overflow-y: auto;
}

.debug-section {
  background: rgba(0,20,0,0.3);
  padding: 8px;
  border-radius: 4px;
  border: 1px solid #1a2a1a;
}

.debug-section h3 {
  color: var(--accent);
  font-size: 10px;
  margin-bottom: 6px;
  text-transform: uppercase;
  letter-spacing: 1px;
}

.debug-regs, .debug-vdp, .debug-psg, .debug-perf {
  font-family: 'JetBrains Mono', monospace;
  white-space: pre;
  line-height: 1.5;
}

.debug-flags {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.flag-indicator {
  padding: 2px 6px;
  border-radius: 3px;
  font-size: 10px;
  font-weight: bold;
}

.flag-set { background: #1a4a1a; color: #33ff88; }
.flag-reset { background: #1a1a1a; color: #444; }

.debug-disasm {
  font-family: 'JetBrains Mono', monospace;
  white-space: pre;
  line-height: 1.4;
  font-size: 10px;
}

#debug-palette {
  image-rendering: pixelated;
  width: 100%;
  height: 32px;
  border: 1px solid #1a2a1a;
}

/* Settings */
.settings-overlay {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.settings-modal {
  background: var(--panel);
  border-radius: 12px;
  padding: 24px;
  max-width: 400px;
  width: 90%;
  max-height: 80vh;
  overflow-y: auto;
}

.settings-modal h2 {
  color: var(--accent);
  margin-bottom: 16px;
  font-family: 'Press Start 2P', monospace;
  font-size: 14px;
}

.settings-group {
  margin-bottom: 16px;
}

.settings-group h3 {
  font-size: 12px;
  color: var(--muted);
  margin-bottom: 8px;
  text-transform: uppercase;
  letter-spacing: 1px;
}

.settings-group label {
  display: block;
  padding: 4px 0;
  cursor: pointer;
  font-size: 13px;
}

.key-map label {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 4px 0;
}

.key-input {
  background: var(--debug-bg);
  border: 1px solid var(--panel2);
  color: var(--accent);
  font-family: 'JetBrains Mono', monospace;
  font-size: 12px;
  padding: 4px 8px;
  border-radius: 4px;
  width: 120px;
  text-align: center;
  cursor: pointer;
}

.key-input.listening {
  border-color: var(--red);
  animation: pulse 0.5s infinite alternate;
}

@keyframes pulse {
  from { box-shadow: 0 0 0 rgba(255,74,106,0); }
  to { box-shadow: 0 0 8px rgba(255,74,106,0.5); }
}

.status-bar {
  width: 100%;
  background: var(--panel);
  padding: 6px 16px;
  font-size: 11px;
  color: var(--muted);
  display: flex;
  justify-content: space-between;
  gap: 16px;
  border-top: 1px solid #2a2a40;
}

.footer {
  padding: 12px;
  text-align: center;
}

.footer a {
  color: var(--muted);
  text-decoration: none;
  font-size: 11px;
}

.footer a:hover {
  color: var(--accent);
}

@media (max-width: 840px) {
  #screen {
    width: 512px;
    height: 384px;
  }
}

@media (max-width: 560px) {
  #screen {
    width: 256px;
    height: 192px;
  }
  .crt-bezel { padding: 8px; }
  .debug-panel { grid-template-columns: 1fr; }
  .upload-zone { padding: 24px; }
}