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

body {
  background: #0d0d1a;
  color: #c0c0d0;
  font-family: 'JetBrains Mono', monospace;
  min-height: 100vh;
  overflow-x: hidden;
}

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

header {
  background: linear-gradient(135deg, #0a0a1a 0%, #1a1a3a 100%);
  border-bottom: 2px solid #21C842;
  padding: 12px 20px;
  text-align: center;
}
.header-content { display: flex; flex-direction: column; align-items: center; }
.header-title-row { display: flex; align-items: center; gap: 12px; justify-content: center; flex-wrap: wrap; }

.corruption-badge, .ai-badge {
  font-family: 'Press Start 2P', cursive;
  font-size: 7px;
  border-radius: 4px;
  padding: 3px 8px;
}
.corruption-badge {
  color: #FF7978;
  background: rgba(252,85,84,0.15);
  border: 1px solid #FC5554;
  animation: badge-pulse 2s infinite;
}
.ai-badge {
  color: #42EBF5;
  background: rgba(66,235,245,0.15);
  border: 1px solid #42EBF5;
  animation: ai-badge-pulse 1.5s infinite;
}
@keyframes badge-pulse { 0%,100%{box-shadow:0 0 4px rgba(252,85,84,0.3)}50%{box-shadow:0 0 12px rgba(252,85,84,0.6)} }
@keyframes ai-badge-pulse { 0%,100%{box-shadow:0 0 4px rgba(66,235,245,0.3)}50%{box-shadow:0 0 14px rgba(66,235,245,0.7)} }

header h1 { font-family: 'Press Start 2P', cursive; font-size: 18px; color: #42EBF5; text-shadow: 0 0 10px rgba(66,235,245,0.5); letter-spacing: 2px; }
.subtitle { font-size: 8px; color: #5EDC78; margin-top: 4px; font-family: 'Press Start 2P', cursive; }

#main-layout { display: flex; flex: 1; gap: 0; }
#display-area { flex: 1; display: flex; flex-direction: column; align-items: center; padding: 20px; background: radial-gradient(ellipse at center, #1a1a2e 0%, #0d0d1a 70%); }

#crt-frame { background: linear-gradient(145deg, #2a2a3a, #1a1a25); border-radius: 20px; padding: 16px; box-shadow: 0 0 30px rgba(0,0,0,0.8), inset 0 0 20px rgba(0,0,0,0.5); border: 2px solid #333; }
#crt-screen { position: relative; border-radius: 8px; overflow: hidden; background: #000; box-shadow: inset 0 0 40px rgba(0,0,0,0.8); }
#screen { display: block; width: 684px; height: 524px; image-rendering: pixelated; image-rendering: crisp-edges; }
#scanlines { position: absolute; top: 0; left: 0; width: 100%; height: 100%; pointer-events: none; background: repeating-linear-gradient(0deg, transparent, transparent 2px, rgba(0,0,0,0.15) 2px, rgba(0,0,0,0.15) 4px); opacity: 0; transition: opacity 0.3s; }
#scanlines.active { opacity: 1; }
#corruption-flash { position: absolute; top: 0; left: 0; width: 100%; height: 100%; pointer-events: none; background: rgba(252,85,84,0); }
#corruption-flash.flash { animation: corrupt-flash 0.15s ease-out; }
@keyframes corrupt-flash { 0%{background:rgba(252,85,84,0.3)}100%{background:rgba(252,85,84,0)} }

.ai-overlay { position: absolute; top: 8px; right: 8px; z-index: 10; pointer-events: none; }
.ai-overlay.hidden { display: none; }
.ai-overlay-text {
  font-family: 'Press Start 2P', cursive; font-size: 10px;
  color: #42EBF5; background: rgba(10,10,26,0.85);
  border: 2px solid #42EBF5; border-radius: 6px; padding: 6px 12px;
  animation: ai-glow 1s infinite alternate;
}
@keyframes ai-glow { 0%{box-shadow:0 0 6px rgba(66,235,245,0.4)}100%{box-shadow:0 0 20px rgba(66,235,245,0.8)} }

#controls { margin-top: 16px; display: flex; flex-direction: column; gap: 8px; width: 100%; max-width: 684px; }
.control-row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; justify-content: center; }

.sram-row { background: rgba(33,176,59,0.08); border: 1px solid rgba(33,176,59,0.25); border-radius: 6px; padding: 6px 10px; }
.sram-auto-label { font-family: 'Press Start 2P', cursive; font-size: 8px; color: #5EDC78; }
.sram-dirty-indicator { font-family: 'JetBrains Mono', monospace; font-size: 10px; font-weight: bold; }
.sram-dirty-indicator.dirty { color: #FC5554; animation: pulse-dirty 1s infinite; }
.sram-dirty-indicator.clean { color: #21C842; }
@keyframes pulse-dirty { 0%,100%{opacity:1}50%{opacity:0.5} }

.file-btn { display: inline-flex; align-items: center; gap: 4px; background: linear-gradient(180deg, #2a4a3a, #1a3a2a); border: 2px solid #21C842; color: #5EDC78; padding: 6px 14px; border-radius: 6px; cursor: pointer; font-family: 'Press Start 2P', cursive; font-size: 9px; transition: all 0.15s; }
.file-btn:hover { background: #21C842; color: #000; }

.ctrl-btn { background: linear-gradient(180deg, #2a2a40, #1a1a30); border: 2px solid #5455ED; color: #7D76FC; padding: 6px 12px; border-radius: 6px; cursor: pointer; font-size: 14px; transition: all 0.15s; font-family: 'JetBrains Mono', monospace; }
.ctrl-btn:hover { background: #5455ED; color: #fff; }
.ctrl-btn:active { transform: translateY(1px); }
.ctrl-btn:disabled { opacity: 0.4; cursor: not-allowed; }
.tiny-btn { font-size: 8px !important; padding: 2px 6px !important; }

.ctrl-label { display: flex; align-items: center; gap: 6px; font-size: 8px; color: #888; font-family: 'Press Start 2P', cursive; }

select, input[type="range"] { background: #1a1a30; border: 1px solid #444; color: #c0c0d0; padding: 4px; border-radius: 4px; font-family: 'JetBrains Mono', monospace; }
input[type="range"] { width: 80px; accent-color: #42EBF5; }

/* AI PANEL */
.ai-panel {
  margin-top: 16px; width: 100%; max-width: 684px;
  background: rgba(66,235,245,0.04); border: 2px solid rgba(66,235,245,0.3);
  border-radius: 12px; overflow: hidden;
}
.ai-panel-header {
  background: linear-gradient(135deg, rgba(66,235,245,0.12), rgba(33,176,59,0.08));
  padding: 10px 14px; display: flex; justify-content: space-between; align-items: center;
  font-family: 'Press Start 2P', cursive; font-size: 10px; color: #42EBF5;
  border-bottom: 1px solid rgba(66,235,245,0.2);
}
.ai-status-badge {
  font-family: 'Press Start 2P', cursive; font-size: 7px;
  padding: 3px 8px; border-radius: 4px;
}
.ai-status-badge.idle { color: #666; background: rgba(100,100,100,0.2); border: 1px solid #444; }
.ai-status-badge.recording { color: #FC5554; background: rgba(252,85,84,0.2); border: 1px solid #FC5554; animation: pulse-dirty 0.5s infinite; }
.ai-status-badge.training { color: #D4C154; background: rgba(212,193,84,0.2); border: 1px solid #D4C154; animation: pulse-dirty 0.8s infinite; }
.ai-status-badge.trained { color: #21C842; background: rgba(33,176,59,0.2); border: 1px solid #21C842; }
.ai-status-badge.playing { color: #42EBF5; background: rgba(66,235,245,0.2); border: 1px solid #42EBF5; animation: ai-glow 1s infinite alternate; }

.ai-panel-content { padding: 12px; display: flex; flex-direction: column; gap: 10px; }
.ai-controls-row { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.ai-info-row { display: flex; gap: 16px; flex-wrap: wrap; }
.ai-info-text { font-size: 9px; color: #888; font-family: 'JetBrains Mono', monospace; }
.ai-training-row { display: flex; gap: 6px; flex-wrap: wrap; align-items: center; }
.ai-label { font-family: 'Press Start 2P', cursive; font-size: 7px; color: #42EBF5; display: flex; align-items: center; gap: 4px; }
.ai-input { background: #0a0a18; border: 1px solid #42EBF5; color: #42EBF5; padding: 3px 6px; border-radius: 4px; font-family: 'JetBrains Mono', monospace; font-size: 10px; width: 60px; }
.ai-viz-row { display: flex; gap: 8px; flex-wrap: wrap; }
.ai-viz-row canvas { border: 1px solid rgba(66,235,245,0.2); border-radius: 4px; background: #0a0a18; }

.ai-btn {
  font-family: 'Press Start 2P', cursive; font-size: 8px;
  padding: 5px 10px; border-radius: 6px; cursor: pointer;
  border: 2px solid #42EBF5; color: #42EBF5;
  background: linear-gradient(180deg, rgba(66,235,245,0.12), rgba(66,235,245,0.04));
  transition: all 0.15s;
}
.ai-btn:hover:not(:disabled) { background: rgba(66,235,245,0.25); }
.ai-btn:disabled { opacity: 0.35; cursor: not-allowed; }
.record-btn { border-color: #FC5554; color: #FC5554; background: linear-gradient(180deg, rgba(252,85,84,0.12), rgba(252,85,84,0.04)); }
.record-btn:hover:not(:disabled) { background: rgba(252,85,84,0.25); }
.stop-btn { border-color: #D4C154; color: #D4C154; background: linear-gradient(180deg, rgba(212,193,84,0.12), rgba(212,193,84,0.04)); }
.train-btn { border-color: #D4C154; color: #D4C154; background: linear-gradient(180deg, rgba(212,193,84,0.12), rgba(212,193,84,0.04)); }
.play-ai-btn { border-color: #21C842; color: #21C842; background: linear-gradient(180deg, rgba(33,176,59,0.12), rgba(33,176,59,0.04)); }
.play-ai-btn:hover:not(:disabled) { background: rgba(33,176,59,0.25); }
.clear-btn { border-color: #666; color: #888; background: rgba(100,100,100,0.1); }
.export-btn, .import-btn { border-color: #7D76FC; color: #7D76FC; background: rgba(125,118,252,0.1); display: inline-flex; align-items: center; }
.import-btn { cursor: pointer; }
.import-btn input { display: none; }

.ai-progress { position: relative; height: 20px; background: #0a0a18; border-radius: 4px; border: 1px solid rgba(66,235,245,0.2); overflow: hidden; }
.ai-progress.hidden { display: none; }
.ai-progress-bar { height: 100%; background: linear-gradient(90deg, #42EBF5, #21C842); transition: width 0.1s; width: 0; }
.ai-progress-text { position: absolute; top: 50%; left: 50%; transform: translate(-50%, -50%); font-size: 8px; font-family: 'Press Start 2P', cursive; color: #fff; text-shadow: 0 0 4px #000; white-space: nowrap; }

.ai-status-bar { font-size: 8px; color: #42EBF5; padding: 2px 8px; border: 1px solid rgba(66,235,245,0.3); border-radius: 3px; background: rgba(66,235,245,0.08); }

/* GAMEPADS */
#gamepads-container { margin-top: 16px; display: flex; align-items: flex-start; gap: 20px; justify-content: center; }
.gamepad-wrapper { display: flex; flex-direction: column; align-items: center; gap: 4px; }
.gamepad-label { font-family: 'Press Start 2P', cursive; font-size: 10px; color: #42EBF5; text-shadow: 0 0 6px rgba(66,235,245,0.4); padding: 2px 10px; border: 1px solid #42EBF5; border-radius: 4px; background: rgba(66,235,245,0.08); }
.gamepad-label.p2-label { color: #FF7978; text-shadow: 0 0 6px rgba(255,121,120,0.4); border-color: #FF7978; background: rgba(255,121,120,0.08); }
.gamepad-divider { width: 2px; height: 140px; background: linear-gradient(180deg, transparent, #444, transparent); align-self: center; }
#gamepad, #gamepad-p2 { display: flex; align-items: center; gap: 40px; }
#dpad, #dpad-p2 { 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: linear-gradient(180deg, #333, #222); border: 2px solid #555; color: #aaa; font-size: 16px; cursor: pointer; display: flex; align-items: center; justify-content: center; transition: all 0.1s; user-select: none; touch-action: none; }
.dpad-btn:active, .dpad-btn.pressed { background: #5455ED; color: #fff; border-color: #7D76FC; }
.dpad-center { width: 44px; height: 44px; background: #2a2a2a; border: 2px solid #444; }
#action-btns, #action-btns-p2 { display: flex; gap: 16px; }
.action-btn { width: 56px; height: 56px; border-radius: 50%; background: linear-gradient(180deg, #D4524D, #a03030); border: 3px solid #FC5554; color: #fff; font-family: 'Press Start 2P', cursive; font-size: 12px; cursor: pointer; transition: all 0.1s; user-select: none; touch-action: none; }
.action-btn:active, .action-btn.pressed { background: #FC5554; box-shadow: 0 0 15px rgba(252,85,84,0.6); transform: scale(0.95); }
.p2-dpad:active, .p2-dpad.pressed { background: #D4C154; color: #000; border-color: #E6CE80; }
.p2-action { background: linear-gradient(180deg, #5455ED, #3a3aad) !important; border-color: #7D76FC !important; }
.p2-action:active, .p2-action.pressed { background: #7D76FC !important; }
#pause-btn-container { display: flex; justify-content: center; margin-left: 8px; }
.pause-reset-btn { padding: 6px 14px; border-radius: 8px; background: linear-gradient(180deg, #4a3a1a, #2a2010); border: 2px solid #D4C154; color: #E6CE80; font-family: 'Press Start 2P', cursive; font-size: 8px; cursor: pointer; user-select: none; touch-action: none; }

#key-hints { margin-top: 10px; font-size: 9px; color: #666; font-family: 'Press Start 2P', cursive; text-align: center; }

/* DEBUG PANEL */
#debug-panel { width: 400px; background: #0f0f1f; border-left: 2px solid #333; overflow-y: auto; max-height: calc(100vh - 120px); flex-shrink: 0; }
.debug-header { background: #1a1a30; padding: 10px 14px; cursor: pointer; display: flex; justify-content: space-between; align-items: center; font-family: 'Press Start 2P', cursive; font-size: 10px; color: #42EBF5; border-bottom: 1px solid #333; position: sticky; top: 0; z-index: 2; }
#debug-content { padding: 10px; }
#debug-content.collapsed { display: none; }
.debug-section { margin-bottom: 14px; }
.debug-section h3 { font-family: 'Press Start 2P', cursive; font-size: 8px; color: #D4C154; margin-bottom: 6px; border-bottom: 1px solid #333; padding-bottom: 4px; }
.debug-section pre { font-size: 10px; line-height: 1.5; color: #c0d0c0; white-space: pre-wrap; word-break: break-all; }
#flags-display { display: flex; gap: 6px; flex-wrap: wrap; }
.flag-indicator { padding: 2px 6px; border-radius: 3px; font-size: 10px; font-weight: bold; border: 1px solid #444; background: #1a1a1a; color: #666; }
.flag-indicator.active { background: #21C842; color: #000; border-color: #5EDC78; }
.bp-row { display: flex; gap: 6px; align-items: center; margin-bottom: 4px; }
#bp-input, #mem-addr { background: #1a1a30; border: 1px solid #444; color: #42EBF5; padding: 4px 8px; border-radius: 4px; font-family: 'JetBrains Mono', monospace; font-size: 12px; width: 60px; }
#bp-status { font-size: 10px; color: #888; }
#mem-select { margin-bottom: 4px; font-size: 11px; }
#mem-display { font-size: 9px; max-height: 140px; overflow-y: auto; color: #aaa; }

/* CORRUPTION */
.corruption-section { background: rgba(252,85,84,0.04); border: 1px solid rgba(252,85,84,0.2); border-radius: 8px; padding: 10px; }
.corruption-section h3 { color: #FC5554 !important; }
.corruption-controls { display: flex; flex-direction: column; gap: 6px; }
.corruption-mode-row, .corruption-manual-row { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.corruption-label { font-size: 7px !important; color: #FF7978 !important; white-space: nowrap; }
.corruption-select { background: #1a0a0a; border: 1px solid #D4524D; color: #FF7978; padding: 3px 6px; border-radius: 4px; font-family: 'JetBrains Mono', monospace; font-size: 10px; }
.corruption-indicator { font-size: 16px; }
.corruption-indicator.off { color: #444; }
.corruption-indicator.realistic { color: #D4C154; animation: ind-pulse 2s infinite; }
.corruption-indicator.aggressive { color: #FC5554; animation: ind-pulse 1s infinite; }
.corruption-indicator.chaos { color: #FF00FF; animation: ind-chaos 0.3s infinite; }
@keyframes ind-pulse { 0%,100%{opacity:1}50%{opacity:0.4} }
@keyframes ind-chaos { 0%{color:#FF00FF}25%{color:#00FFFF}50%{color:#FF0}75%{color:#0F0}100%{color:#FF00FF} }

.corrupt-vdp-btn { background: linear-gradient(180deg, #4a1a2a, #2a0a15) !important; border-color: #FC5554 !important; color: #FF7978 !important; font-size: 10px !important; padding: 4px 10px !important; }
.corruption-stats { font-size: 9px !important; color: #FF7978 !important; line-height: 1.4 !important; }
.corruption-log-header { display: flex; justify-content: space-between; align-items: center; font-family: 'Press Start 2P', cursive; font-size: 7px; color: #888; margin-bottom: 4px; }
.corruption-log { font-size: 8px !important; color: #996 !important; max-height: 100px; overflow-y: auto; line-height: 1.3 !important; background: rgba(0,0,0,0.3); border-radius: 4px; padding: 4px; }

.rom-readonly-row { display: flex; align-items: center; margin-bottom: 6px; }
.rom-readonly-label { display: flex; align-items: center; gap: 8px; font-size: 10px; cursor: pointer; user-select: none; }
.rom-readonly-label input[type="checkbox"] { display: none; }
.rom-cb-custom { width: 18px; height: 18px; border: 2px solid #21C842; border-radius: 3px; background: #0a0a18; display: inline-flex; align-items: center; justify-content: center; }
.rom-cb-custom::after { content: ''; width: 10px; height: 10px; background: #21C842; border-radius: 1px; opacity: 0; transition: opacity 0.15s; }
.rom-readonly-label input[type="checkbox"]:checked + .rom-cb-custom::after { opacity: 1; }
.rom-readonly-status { font-size: 9px; color: #21C842; display: block; }

.corrupt-row { display: flex; gap: 8px; align-items: center; margin-bottom: 6px; }
.corrupt-label { display: flex; align-items: center; gap: 6px; font-size: 7px; color: #FC5554; font-family: 'Press Start 2P', cursive; }
#corrupt-amount { background: #1a0a0a; border: 1px solid #D4524D; color: #FF7978; padding: 4px 8px; border-radius: 4px; font-family: 'JetBrains Mono', monospace; font-size: 12px; width: 80px; }
.corrupt-btn-style { background: linear-gradient(180deg, #4a1a1a, #2a0a0a) !important; border-color: #D4524D !important; color: #FC5554 !important; font-size: 11px !important; }
.corrupt-status { font-size: 9px; color: #666; display: block; }
.corrupt-status.active { color: #FC5554; animation: glitch 0.3s ease; }
@keyframes glitch { 0%{transform:translateX(0)}20%{transform:translateX(-2px);color:#42EBF5}40%{transform:translateX(3px);color:#FF7978}60%{transform:translateX(-1px)}80%{transform:translateX(2px)}100%{transform:translateX(0);color:#FC5554} }

.dis-addr { color: #21C842; }
.dis-hex { color: #7D76FC; }
.dis-mnemonic { color: #42EBF5; }
.dis-current { background: #2a2a40; display: block; }
.dis-halt { color: #FC5554; font-weight: bold; }

#status-bar {
  background: #0a0a18; border-top: 2px solid #333;
  padding: 6px 16px; display: flex; gap: 12px;
  font-size: 9px; font-family: 'Press Start 2P', cursive;
  color: #666; flex-wrap: wrap; align-items: center;
}
#status-state { color: #FC5554; }
#status-state.running { color: #21C842; animation: pulse 1.5s infinite; }
#status-state.paused { color: #D4C154; }
@keyframes pulse { 0%,100%{opacity:1}50%{opacity:0.6} }

.corruption-status-bar { font-size: 8px; padding: 2px 8px; border-radius: 3px; border: 1px solid #333; }
.corruption-status-bar.off { color: #555; }
.corruption-status-bar.realistic { color: #D4C154; border-color: #D4C154; background: rgba(212,193,84,0.1); }
.corruption-status-bar.aggressive { color: #FC5554; border-color: #FC5554; background: rgba(252,85,84,0.1); }
.corruption-status-bar.chaos { color: #FF00FF; border-color: #FF00FF; background: rgba(255,0,255,0.1); animation: ind-chaos 0.5s infinite; }

footer {
  background: #0a0a18; padding: 10px 20px;
  text-align: center; font-size: 9px; color: #444;
  display: flex; justify-content: center; gap: 20px;
  font-family: 'Press Start 2P', cursive;
}
footer a { color: #42EBF5; text-decoration: none; }
footer a:hover { text-decoration: underline; }

@media (max-width: 1100px) {
  #main-layout { flex-direction: column; }
  #debug-panel { width: 100%; border-left: none; border-top: 2px solid #333; max-height: 400px; }
  #screen { width: 100%; max-width: 512px; height: auto; }
}
@media (max-width: 600px) {
  header h1 { font-size: 12px; }
  .subtitle { font-size: 6px; }
  #display-area { padding: 10px; }
  #crt-frame { padding: 8px; border-radius: 12px; }
  #screen { max-width: 100%; }
  #gamepads-container { gap: 10px; flex-wrap: wrap; }
  #gamepad, #gamepad-p2 { gap: 20px; }
  .gamepad-divider { height: 2px; width: 100px; }
  .dpad-btn { width: 38px; height: 38px; }
  .dpad-center { width: 38px; height: 38px; }
  .action-btn { width: 48px; height: 48px; }
  #key-hints { display: none; }
  #status-bar { font-size: 7px; gap: 8px; }
  .ai-panel { max-width: 100%; }
  .ai-viz-row canvas { width: 140px; height: 60px; }
}