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

:root {
  --bg-deep: #0a0a14;
  --bg-main: #0f0f1a;
  --bg-panel: #1a1a2e;
  --bg-surface: #222240;
  --accent-orange: #ff6600;
  --accent-red: #cc0000;
  --accent-blue: #4488ff;
  --text-main: #e8e8f0;
  --text-dim: #8888aa;
  --text-green: #00ff88;
  --text-amber: #ffaa00;
  --font-pixel: 'Press Start 2P', monospace;
  --font-mono: 'JetBrains Mono', monospace;
}

html, body {
  background: var(--bg-deep);
  color: var(--text-main);
  font-family: var(--font-mono);
  min-height: 100vh;
  overflow-x: hidden;
}

#app {
  max-width: 1200px;
  margin: 0 auto;
  padding: 0 16px;
}

/* HEADER */
header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 18px 0 10px;
  border-bottom: 3px solid var(--accent-orange);
  background: linear-gradient(90deg, var(--bg-deep) 0%, var(--bg-panel) 50%, var(--bg-deep) 100%);
  position: relative;
}
header::after {
  content: '';
  position: absolute;
  bottom: -3px;
  left: 0;
  right: 0;
  height: 3px;
  background: linear-gradient(90deg, var(--accent-red), var(--accent-orange), var(--accent-red));
  animation: headerGlow 3s ease-in-out infinite;
}
@keyframes headerGlow {
  0%, 100% { opacity: 0.6; }
  50% { opacity: 1; }
}

.header-inner { flex: 1; }

.title {
  font-family: var(--font-pixel);
  font-size: clamp(16px, 4vw, 28px);
  color: var(--accent-orange);
  text-shadow: 0 0 20px rgba(255,102,0,0.5), 2px 2px 0 #000;
  letter-spacing: 2px;
  animation: titleFlicker 4s ease-in-out infinite;
}
@keyframes titleFlicker {
  0%, 100% { text-shadow: 0 0 20px rgba(255,102,0,0.5), 2px 2px 0 #000; }
  50% { text-shadow: 0 0 30px rgba(255,102,0,0.8), 0 0 60px rgba(255,102,0,0.3), 2px 2px 0 #000; }
}
.berry { animation: berryBounce 2s ease-in-out infinite; display: inline-block; }
@keyframes berryBounce {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-3px); }
}

.subtitle {
  font-family: var(--font-mono);
  font-size: clamp(8px, 1.5vw, 11px);
  color: var(--text-dim);
  margin-top: 4px;
  letter-spacing: 1px;
}

.icon-btn {
  background: var(--bg-surface);
  border: 2px solid #444;
  color: var(--text-main);
  font-size: 20px;
  padding: 8px 12px;
  cursor: pointer;
  border-radius: 4px;
  transition: all 0.2s;
}
.icon-btn:hover { border-color: var(--accent-orange); box-shadow: 0 0 12px rgba(255,102,0,0.3); }

.icon-btn-sm {
  background: none;
  border: none;
  color: var(--text-dim);
  font-size: 16px;
  cursor: pointer;
  padding: 4px 8px;
}
.icon-btn-sm:hover { color: var(--accent-orange); }

/* ROM UPLOAD */
.rom-slot {
  margin: 16px 0;
}
.slot-inner {
  border: 3px dashed #444;
  border-radius: 8px;
  padding: 24px;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s;
  background: linear-gradient(180deg, var(--bg-panel) 0%, var(--bg-main) 100%);
  position: relative;
  overflow: hidden;
}
.slot-inner::before {
  content: '';
  position: absolute;
  top: 0; left: -100%;
  width: 50%;
  height: 100%;
  background: linear-gradient(90deg, transparent, rgba(255,102,0,0.05), transparent);
  animation: slotShimmer 4s ease-in-out infinite;
}
@keyframes slotShimmer { 0% { left: -100%; } 100% { left: 200%; } }

.slot-inner:hover, .slot-inner.dragover {
  border-color: var(--accent-orange);
  box-shadow: 0 0 30px rgba(255,102,0,0.15);
}
.cartridge-icon {
  font-size: 28px;
  color: var(--accent-orange);
  letter-spacing: -2px;
  margin-bottom: 8px;
  transition: transform 0.4s;
}
.slot-inner.inserting .cartridge-icon {
  animation: cartridgeInsert 0.6s ease-out;
}
@keyframes cartridgeInsert {
  0% { transform: translateY(-40px); opacity: 0; }
  60% { transform: translateY(4px); }
  100% { transform: translateY(0); opacity: 1; }
}
.slot-title {
  font-family: var(--font-pixel);
  font-size: 14px;
  color: var(--text-main);
  margin-bottom: 6px;
}
.slot-sub {
  font-size: 11px;
  color: var(--text-dim);
}

.rom-info {
  margin-top: 8px;
  padding: 10px 16px;
  background: var(--bg-surface);
  border-radius: 4px;
  font-size: 11px;
  color: var(--text-green);
  border-left: 3px solid var(--text-green);
}

/* MAIN AREA */
#main-area {
  display: flex;
  gap: 16px;
  margin: 12px 0;
  align-items: flex-start;
}

#screen-wrapper {
  flex: 1;
  display: flex;
  justify-content: center;
}

#crt-bezel {
  position: relative;
  background: #111;
  border: 4px solid #333;
  border-radius: 12px;
  box-shadow: inset 0 0 30px rgba(0,0,0,0.8), 0 0 40px rgba(0,0,0,0.5), 0 4px 20px rgba(0,0,0,0.6);
  padding: 8px;
  overflow: hidden;
}
#crt-bezel.crt-curve {
  border-radius: 20px;
}
#crt-bezel.crt-curve #canvas {
  border-radius: 12px;
}

#canvas {
  display: block;
  width: 512px;
  height: 480px;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  background: #000;
  border-radius: 4px;
}

.scanlines {
  position: absolute;
  top: 8px; left: 8px; right: 8px; bottom: 8px;
  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;
  border-radius: 4px;
}
.scanlines.off { display: none; }

.fps {
  position: absolute;
  top: 14px;
  right: 14px;
  font-family: var(--font-pixel);
  font-size: 8px;
  color: var(--text-green);
  background: rgba(0,0,0,0.7);
  padding: 2px 6px;
  border-radius: 2px;
  pointer-events: none;
}

/* CONTROLS */
#controls {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 0;
  flex-wrap: wrap;
  justify-content: center;
}
.ctrl-btn {
  font-family: var(--font-pixel);
  font-size: 12px;
  background: var(--bg-surface);
  color: var(--text-main);
  border: 2px solid #444;
  padding: 8px 14px;
  cursor: pointer;
  border-radius: 4px;
  transition: all 0.2s;
  white-space: nowrap;
}
.ctrl-btn:hover {
  border-color: var(--accent-orange);
  color: var(--accent-orange);
  box-shadow: 0 0 10px rgba(255,102,0,0.25);
}
.ctrl-btn.active {
  background: var(--accent-orange);
  color: #000;
  border-color: var(--accent-orange);
}
.ctrl-btn.pulse {
  animation: btnPulse 2s ease-in-out infinite;
}
@keyframes btnPulse {
  0%, 100% { box-shadow: 0 0 0 rgba(255,102,0,0); }
  50% { box-shadow: 0 0 15px rgba(255,102,0,0.4); }
}

.separator { color: #333; font-size: 18px; user-select: none; }

.vol-label { font-size: 14px; }
.vol-slider {
  width: 70px;
  accent-color: var(--accent-orange);
  cursor: pointer;
}
.speed-select {
  font-family: var(--font-pixel);
  font-size: 10px;
  background: var(--bg-surface);
  color: var(--text-main);
  border: 2px solid #444;
  padding: 6px 8px;
  border-radius: 4px;
  cursor: pointer;
}
.speed-select:hover { border-color: var(--accent-orange); }

/* GAMEPAD */
#gamepad {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 10px;
  max-width: 500px;
  margin: 0 auto;
  gap: 10px;
}
.dpad {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0;
}
.dpad-mid {
  display: flex;
  align-items: center;
  gap: 0;
}
.dpad-btn {
  width: 44px;
  height: 44px;
  background: #2a2a3a;
  border: 2px solid #444;
  color: var(--text-dim);
  font-size: 14px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.1s;
  user-select: none;
  -webkit-user-select: none;
}
.dpad-btn:active, .dpad-btn.pressed {
  background: #444;
  color: var(--text-main);
  transform: scale(0.95);
}
.dpad-up { border-radius: 6px 6px 0 0; }
.dpad-down { border-radius: 0 0 6px 6px; }
.dpad-left { border-radius: 6px 0 0 6px; }
.dpad-right { border-radius: 0 6px 6px 0; }
.dpad-center { width: 44px; height: 44px; background: #2a2a3a; border: 2px solid #444; }

.gamepad-center {
  display: flex;
  gap: 10px;
  align-items: center;
}
.meta-btn {
  font-family: var(--font-pixel);
  font-size: 8px;
  background: #333;
  color: var(--text-dim);
  border: 2px solid #555;
  padding: 6px 12px;
  border-radius: 12px;
  cursor: pointer;
  transition: all 0.1s;
  user-select: none;
  -webkit-user-select: none;
}
.meta-btn:active, .meta-btn.pressed { background: #555; color: var(--text-main); }

.gamepad-right {
  display: flex;
  gap: 12px;
  align-items: center;
}
.action-btn {
  width: 52px;
  height: 52px;
  border-radius: 50%;
  font-family: var(--font-pixel);
  font-size: 12px;
  cursor: pointer;
  border: 3px solid;
  transition: all 0.1s;
  user-select: none;
  -webkit-user-select: none;
}
.btn-i {
  background: var(--accent-red);
  border-color: #ff2222;
  color: white;
}
.btn-ii {
  background: #992200;
  border-color: var(--accent-red);
  color: white;
}
.action-btn:active, .action-btn.pressed {
  transform: scale(0.9);
  filter: brightness(1.3);
}

/* DEBUG PANEL */
.debug-panel {
  width: 340px;
  min-width: 280px;
  max-height: 80vh;
  overflow-y: auto;
  background: #0d0d0d;
  border: 2px solid #333;
  border-radius: 6px;
  font-size: 11px;
}
.debug-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 12px;
  background: var(--bg-panel);
  font-family: var(--font-pixel);
  font-size: 10px;
  color: var(--text-amber);
  border-bottom: 1px solid #333;
  position: sticky;
  top: 0;
  z-index: 1;
}
.debug-content { padding: 8px; }
.debug-section {
  margin-bottom: 12px;
  border-bottom: 1px solid #1a1a1a;
  padding-bottom: 8px;
}
.debug-section h3 {
  font-family: var(--font-pixel);
  font-size: 8px;
  color: var(--accent-orange);
  margin-bottom: 6px;
  text-transform: uppercase;
}
.debug-section pre {
  font-family: var(--font-mono);
  font-size: 10px;
  color: var(--text-green);
  white-space: pre-wrap;
  line-height: 1.5;
}

.palette-grid {
  display: flex;
  flex-wrap: wrap;
  gap: 1px;
  margin: 4px 0;
}
.pal-swatch {
  width: 8px;
  height: 8px;
  border-radius: 1px;
}
.psg-channels {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px;
}
.psg-ch {
  background: #111;
  padding: 4px 6px;
  border-radius: 3px;
  font-size: 9px;
  color: var(--text-amber);
  font-family: var(--font-mono);
}
.psg-bar {
  height: 4px;
  background: var(--accent-orange);
  border-radius: 2px;
  margin-top: 2px;
  transition: width 0.1s;
}
.disasm {
  font-size: 9px !important;
  color: var(--text-amber) !important;
}
.memory-view {
  font-size: 9px !important;
  max-height: 120px;
  overflow-y: auto;
}
#mem-region {
  font-size: 10px;
  background: var(--bg-surface);
  color: var(--text-main);
  border: 1px solid #444;
  padding: 2px 6px;
  border-radius: 3px;
  margin-bottom: 4px;
}

/* MODAL */
.modal {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.8);
  z-index: 100;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 20px;
}
.modal.hidden { display: none; }
.modal-content {
  background: var(--bg-panel);
  border: 2px solid #444;
  border-radius: 8px;
  max-width: 500px;
  width: 100%;
  max-height: 80vh;
  overflow-y: auto;
}
.modal-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  border-bottom: 2px solid var(--accent-orange);
}
.modal-header h2 {
  font-family: var(--font-pixel);
  font-size: 12px;
  color: var(--accent-orange);
}
.modal-body { padding: 16px; }
.settings-group {
  margin-bottom: 16px;
}
.settings-group h3 {
  font-family: var(--font-pixel);
  font-size: 9px;
  color: var(--text-amber);
  margin-bottom: 8px;
}
.settings-group label {
  display: block;
  font-size: 12px;
  color: var(--text-main);
  margin-bottom: 6px;
  cursor: pointer;
}
.settings-group input[type="checkbox"] {
  accent-color: var(--accent-orange);
  margin-right: 6px;
}
.settings-group input[type="range"] {
  accent-color: var(--accent-orange);
  width: 120px;
  margin-left: 8px;
}
.ch-mutes {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
}
.ch-mutes label { font-size: 10px; display: inline-flex; align-items: center; }
.key-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px;
  font-size: 10px;
}
.key-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 4px 8px;
  background: var(--bg-surface);
  border-radius: 3px;
}
.key-item span { color: var(--text-dim); }
.key-item kbd {
  background: #333;
  padding: 2px 6px;
  border-radius: 3px;
  color: var(--accent-orange);
  font-family: var(--font-mono);
  font-size: 10px;
}

.hidden { display: none !important; }

/* FOOTER */
footer {
  text-align: center;
  padding: 20px 0;
  border-top: 1px solid #222;
  margin-top: 20px;
}
footer p {
  font-size: 11px;
  color: var(--text-dim);
  margin: 4px 0;
}
footer a {
  color: var(--accent-orange);
  text-decoration: none;
}
footer a:hover { text-decoration: underline; }
.disclaimer { font-size: 9px !important; color: #555 !important; }

/* RESPONSIVE */
@media (max-width: 768px) {
  #main-area { flex-direction: column; }
  .debug-panel { width: 100%; max-height: 400px; }
  #canvas { width: 100%; height: auto; }
  #controls { gap: 4px; }
  .ctrl-btn { padding: 6px 10px; font-size: 10px; }
  .separator { display: none; }
}

/* Scrollbar */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: #111; }
::-webkit-scrollbar-thumb { background: #444; border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--accent-orange); }