* { 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; }

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;
}

.corruptor-badge {
  font-family: 'Press Start 2P', cursive;
  font-size: 10px;
  color: #FC5554;
  background: rgba(252,85,84,0.15);
  border: 1px solid #FC5554;
  border-radius: 4px;
  padding: 3px 8px;
  animation: corrupt-pulse 2s infinite;
  text-shadow: 0 0 6px rgba(252,85,84,0.5);
}

@keyframes corrupt-pulse {
  0%, 100% { opacity: 1; border-color: #FC5554; }
  50% { opacity: 0.7; border-color: #D4524D; }
}

.subtitle {
  font-size: 10px; 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), 0 0 60px rgba(66,235,245,0.05);
  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; }

#glitch-overlay {
  position: absolute; top: 0; left: 0; width: 100%; height: 100%; pointer-events: none;
  opacity: 0; transition: opacity 0.3s;
  background: repeating-linear-gradient(0deg, transparent 0%, rgba(252,85,84,0.02) 2%, transparent 4%);
  mix-blend-mode: screen;
}

#glitch-overlay.active {
  opacity: 1;
  animation: glitch-scan 4s linear infinite;
}

@keyframes glitch-scan {
  0% { background-position: 0 0; }
  100% { background-position: 0 524px; }
}

#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-btn {
  background: linear-gradient(180deg, #1a3a2a, #0a2a1a) !important;
  border-color: #21B03B !important; color: #5EDC78 !important; font-size: 11px !important;
}

.sram-btn:hover { background: #21B03B !important; color: #000 !important; }
.sram-status { font-size: 9px; color: #5EDC78; font-family: 'Press Start 2P', cursive; }

.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-label {
  display: flex; align-items: center; gap: 6px;
  font-family: 'Press Start 2P', cursive; font-size: 8px; color: #888;
}

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; }

#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; -webkit-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; -webkit-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; box-shadow: 0 0 15px rgba(125,118,252,0.6) !important; transform: scale(0.95); }
#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; transition: all 0.15s; user-select: none; -webkit-user-select: none; touch-action: none;
}

.pause-reset-btn:active, .pause-reset-btn.pressed { background: #D4C154; color: #000; }
#key-hints { margin-top: 10px; font-size: 9px; color: #666; font-family: 'Press Start 2P', cursive; }

#debug-panel {
  width: 440px; 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; transition: max-height 0.3s; }
#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;
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
}

.module-tag {
  font-family: 'JetBrains Mono', monospace; font-size: 7px; color: #5455ED;
  background: rgba(84, 85, 237, 0.15); border: 1px solid rgba(84, 85, 237, 0.3);
  border-radius: 3px; padding: 1px 5px; font-weight: normal;
}

.debug-section pre { font-size: 11px; 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: 10px; max-height: 200px; overflow-y: auto; color: #aaa; }

.rom-readonly-row { display: flex; align-items: center; margin-bottom: 6px; }

.rom-readonly-label {
  display: flex; align-items: center; gap: 8px; font-size: 10px; color: #c0c0d0;
  font-family: 'JetBrains Mono', monospace; 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;
  transition: all 0.2s; flex-shrink: 0;
}

.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-label input[type="checkbox"]:not(:checked) + .rom-cb-custom { border-color: #FC5554; box-shadow: 0 0 6px rgba(252, 85, 84, 0.3); }
.rom-readonly-status { font-size: 9px; color: #21C842; font-family: 'JetBrains Mono', monospace; display: block; }
.corrupt-row { display: flex; gap: 8px; align-items: center; margin-bottom: 6px; }

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

#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-btn-style:hover {
  background: linear-gradient(180deg, #6a2a2a, #4a1a1a) !important;
  border-color: #FC5554 !important; color: #FF7978 !important;
  box-shadow: 0 0 12px rgba(252, 85, 84, 0.5);
}

.corrupt-status { font-size: 9px; color: #666; font-family: 'JetBrains Mono', monospace; 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); color: #21C842; }
  80% { transform: translateX(2px); color: #D4C154; }
  100% { transform: translateX(0); color: #FC5554; }
}

/* ── Corruption Engine Styles ── */
.corruption-master-section {
  background: linear-gradient(180deg, rgba(252,85,84,0.05), rgba(252,85,84,0.02));
  border: 1px solid rgba(252,85,84,0.2);
  border-radius: 8px;
  padding: 10px;
}

.corruption-master-section h3 {
  color: #FC5554 !important;
  border-bottom-color: rgba(252,85,84,0.3) !important;
}

.corruption-master-controls {
  display: flex; flex-direction: column; gap: 8px; margin-bottom: 12px;
}

.corruption-master-row { display: flex; align-items: center; gap: 8px; }

.corruption-toggle-label {
  display: flex; align-items: center; gap: 6px; font-size: 10px; color: #c0c0d0;
  font-family: 'JetBrains Mono', monospace; cursor: pointer; user-select: none;
}

.corruption-toggle-label input[type="checkbox"] { display: none; }

.corruption-cb-custom {
  width: 16px; height: 16px; border: 2px solid #FC5554; border-radius: 3px;
  background: #0a0a18; display: inline-flex; align-items: center; justify-content: center;
  transition: all 0.2s; flex-shrink: 0;
}

.corruption-cb-custom::after { content: ''; width: 8px; height: 8px; background: #FC5554; border-radius: 1px; opacity: 0; transition: opacity 0.15s; }
.corruption-toggle-label input[type="checkbox"]:checked + .corruption-cb-custom::after { opacity: 1; }

.cb-ram { border-color: #42EBF5; }
.cb-ram::after { background: #42EBF5; }
.cb-cpu { border-color: #D4C154; }
.cb-cpu::after { background: #D4C154; }
.cb-vdp { border-color: #5EDC78; }
.cb-vdp::after { background: #5EDC78; }
.cb-psg { border-color: #C95BBA; }
.cb-psg::after { background: #C95BBA; }
.cb-io { border-color: #7D76FC; }
.cb-io::after { background: #7D76FC; }
.cb-guard { border-color: #21C842; }
.cb-guard::after { background: #21C842; }

.corruption-subsystem {
  background: rgba(255,255,255,0.02); border: 1px solid rgba(255,255,255,0.05);
  border-radius: 6px; padding: 8px; margin-top: 6px;
}

.corruption-sub-header {
  display: flex; justify-content: space-between; align-items: center; margin-bottom: 6px;
}

.corruption-counter {
  font-family: 'JetBrains Mono', monospace; font-size: 10px; color: #FC5554;
  background: rgba(252,85,84,0.1); border: 1px solid rgba(252,85,84,0.2);
  border-radius: 3px; padding: 1px 6px; min-width: 40px; text-align: right;
}

.corruption-sub-controls {
  display: flex; flex-wrap: wrap; gap: 6px; align-items: center;
}

.corruption-sub-controls select {
  font-size: 10px; padding: 2px 4px; background: #0f0f1f;
  border: 1px solid #444; color: #c0c0d0; border-radius: 3px;
  max-width: 150px;
}

.corruption-val {
  font-family: 'JetBrains Mono', monospace; font-size: 9px; color: #888;
  min-width: 30px;
}

.corruption-unit { font-size: 8px; color: #666; }

.small-toggle { font-size: 9px !important; }

.chaos-burst-btn {
  background: linear-gradient(180deg, #6a1a1a, #3a0a0a) !important;
  border-color: #FC5554 !important; color: #FF7978 !important;
  font-family: 'Press Start 2P', cursive !important; font-size: 9px !important;
  width: 100%; padding: 8px !important;
  animation: chaos-idle 2s infinite;
}

.chaos-burst-btn:hover {
  background: linear-gradient(180deg, #8a2a2a, #5a1a1a) !important;
  box-shadow: 0 0 20px rgba(252,85,84,0.6);
}

.burst-flash {
  animation: burst 0.3s ease !important;
}

@keyframes burst {
  0% { transform: scale(1); box-shadow: none; }
  50% { transform: scale(1.05); box-shadow: 0 0 30px rgba(252,85,84,0.8), 0 0 60px rgba(252,85,84,0.4); background: #FC5554 !important; color: #fff !important; }
  100% { transform: scale(1); box-shadow: none; }
}

@keyframes chaos-idle {
  0%, 100% { text-shadow: none; }
  50% { text-shadow: 0 0 8px rgba(252, 85, 84, 0.8); }
}

.corruption-reset-btn {
  background: linear-gradient(180deg, #2a2a40, #1a1a30) !important;
  font-size: 10px !important;
}

#corruption-tick-interval {
  background: #1a0a0a; border: 1px solid #D4524D; color: #FF7978; padding: 3px 6px;
  border-radius: 4px; font-family: 'JetBrains Mono', monospace; font-size: 11px; width: 60px;
}

.corruption-status-indicator {
  font-size: 8px; color: #666;
  font-family: 'Press Start 2P', cursive;
}

.corruption-status-indicator.corruption-active {
  color: #FC5554;
  animation: corrupt-blink 1s infinite;
}

@keyframes corrupt-blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.5; }
}

/* ── Status bar and footer ── */
#status-bar {
  background: #0a0a18; border-top: 2px solid #333; padding: 6px 16px;
  display: flex; gap: 20px; font-size: 10px; font-family: 'Press Start 2P', cursive;
  color: #666; flex-wrap: wrap;
}

#status-state { color: #FC5554; }
#status-state.running { color: #21C842; animation: pulse 1.5s infinite; }
#status-state.paused { color: #D4C154; }
#mem-map-info { color: #5455ED; font-size: 8px; }
@keyframes pulse { 0%, 100% { opacity: 1; } 50% { opacity: 0.6; } }

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; }

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

@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: 7px; }
  #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: 10px; }
}