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

:root {
  --green: #00ff41;
  --green-dim: #00aa2a;
  --green-dark: #004d15;
  --amber: #ffb000;
  --red: #ff2442;
  --cyan: #00e5ff;
  --cyan-dim: #007a88;
  --pcb-green: #0d2818;
  --pcb-trace: #b8860b;
  --bg: #0a0a0a;
  --bg-panel: #111111;
  --bg-input: #1a1a1a;
  --border: #222;
  --text: #cccccc;
  --glow: 0 0 10px rgba(0, 255, 65, 0.3);
  --glow-strong: 0 0 20px rgba(0, 255, 65, 0.5), 0 0 40px rgba(0, 255, 65, 0.2);
  --glow-cyan: 0 0 10px rgba(0, 229, 255, 0.3);
  --glow-amber: 0 0 10px rgba(255, 176, 0, 0.3);
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: 'VT323', 'Courier New', monospace;
  font-size: 16px;
  min-height: 100vh;
  overflow-x: hidden;
}

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

/* Header */
header {
  text-align: center;
  padding: 15px 10px 10px;
  border-bottom: 2px solid var(--green-dark);
  margin-bottom: 20px;
  position: relative;
}

header h1 {
  font-family: 'Press Start 2P', monospace;
  font-size: clamp(10px, 2.5vw, 18px);
  color: var(--green);
  text-shadow: var(--glow);
  letter-spacing: 2px;
  line-height: 1.5;
}

header .dash {
  color: var(--amber);
}

header .subtitle {
  font-family: 'Press Start 2P', monospace;
  font-size: clamp(7px, 1.5vw, 11px);
  color: var(--amber);
  text-shadow: var(--glow-amber);
  margin-top: 4px;
  letter-spacing: 1px;
}

.badge {
  position: absolute;
  top: 10px;
  right: 10px;
  font-size: 11px;
  color: var(--green-dim);
  font-family: 'VT323', monospace;
}

/* Main Layout */
#main-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
}

/* ROM Panel */
#rom-panel {
  background: var(--bg-panel);
  border: 2px solid var(--green-dark);
  border-radius: 8px;
  padding: 24px;
  width: 100%;
  max-width: 700px;
  box-shadow: var(--glow);
}

#rom-panel h2 {
  font-family: 'Press Start 2P', monospace;
  font-size: 14px;
  color: var(--green);
  text-shadow: var(--glow);
  margin-bottom: 8px;
  text-align: center;
}

.rom-instructions {
  text-align: center;
  color: var(--green-dim);
  font-size: 14px;
  margin-bottom: 16px;
}

#rom-slots {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-bottom: 16px;
}

.rom-slot {
  background: var(--bg-input);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px;
}

.rom-label {
  font-family: 'Press Start 2P', monospace;
  font-size: 10px;
  color: var(--green);
  margin-bottom: 6px;
}

.rom-addr {
  color: var(--green-dim);
  font-size: 9px;
}

.rom-dropzone {
  border: 1px dashed var(--green-dark);
  border-radius: 4px;
  padding: 12px 8px;
  text-align: center;
  cursor: pointer;
  transition: all 0.2s;
  position: relative;
  min-height: 50px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
}

.rom-dropzone:hover,
.rom-dropzone.dragover {
  border-color: var(--green);
  background: rgba(0, 255, 65, 0.05);
  box-shadow: inset 0 0 15px rgba(0, 255, 65, 0.1);
}

.rom-dropzone.loaded {
  border-color: var(--green);
  border-style: solid;
  background: rgba(0, 255, 65, 0.08);
}

.rom-status { font-size: 18px; }
.rom-text { font-size: 12px; color: var(--green-dim); }

.rom-input {
  position: absolute;
  inset: 0;
  opacity: 0;
  cursor: pointer;
}

.rom-actions {
  display: flex;
  gap: 12px;
  justify-content: center;
  flex-wrap: wrap;
}

/* Buttons */
.btn {
  font-family: 'Press Start 2P', monospace;
  font-size: 11px;
  padding: 10px 18px;
  border: none;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.15s;
  text-transform: uppercase;
  letter-spacing: 1px;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}

.btn-primary {
  background: var(--green-dark);
  color: var(--green);
  border: 2px solid var(--green);
  box-shadow: var(--glow);
}

.btn-primary:not(:disabled):hover {
  background: var(--green);
  color: #000;
  box-shadow: var(--glow-strong);
}

.btn-primary:disabled {
  opacity: 0.3;
  cursor: not-allowed;
  box-shadow: none;
}

.btn-primary:not(:disabled) {
  animation: glow-pulse 2s ease-in-out infinite;
}

@keyframes glow-pulse {
  0%, 100% { box-shadow: 0 0 10px rgba(0,255,65,0.3); }
  50% { box-shadow: 0 0 25px rgba(0,255,65,0.6), 0 0 50px rgba(0,255,65,0.3); }
}

.btn-secondary {
  background: var(--bg-input);
  color: var(--green);
  border: 1px solid var(--green-dark);
  cursor: pointer;
}

.btn-secondary:hover { background: var(--green-dark); }

.btn-coin {
  background: #332200;
  color: var(--amber);
  border: 2px solid var(--amber);
  font-size: 12px;
  padding: 12px 20px;
}

.btn-coin:active { transform: translateY(2px); box-shadow: none; }

.btn-start-game {
  background: #220011;
  color: var(--red);
  border: 2px solid var(--red);
  font-size: 11px;
  padding: 12px 16px;
}

.btn-start-game:active { transform: translateY(2px); }

.btn-move, .btn-fire {
  background: var(--bg-input);
  color: var(--green);
  border: 2px solid var(--green-dark);
  font-size: 13px;
  padding: 14px 22px;
  border-bottom-width: 4px;
}

.btn-fire { border-color: var(--red); color: var(--red); }
.btn-move:active, .btn-fire:active { transform: translateY(2px); border-bottom-width: 2px; }

/* Screen */
#screen-container {
  width: 100%;
  max-width: 560px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

#screen-bezel {
  background: #000;
  border: 4px solid #1a1a1a;
  border-radius: 12px;
  padding: 16px;
  box-shadow: inset 0 0 60px rgba(0,0,0,0.8), 0 0 30px rgba(0,255,65,0.1);
  width: 100%;
  display: flex;
  justify-content: center;
}

#crt-wrapper {
  position: relative;
  width: 100%;
  aspect-ratio: 224 / 256;
  max-width: 448px;
}

#canvas {
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
  image-rendering: -moz-crisp-edges;
  image-rendering: crisp-edges;
  display: block;
  background: #000;
}

#scanlines {
  position: absolute;
  inset: 0;
  pointer-events: none;
  background: repeating-linear-gradient(
    0deg,
    transparent 0px, transparent 1px,
    rgba(0, 0, 0, 0.15) 1px, rgba(0, 0, 0, 0.15) 2px
  );
  mix-blend-mode: multiply;
}

#scanlines.disabled { display: none; }

#crt-wrapper.crt-effect {
  filter: contrast(1.1) brightness(1.05);
  border-radius: 8px;
  overflow: hidden;
  box-shadow: inset 0 0 80px rgba(0,0,0,0.4);
}

#stats-bar {
  display: flex;
  gap: 16px;
  padding: 8px;
  font-size: 13px;
  color: var(--green-dim);
  width: 100%;
  justify-content: center;
  flex-wrap: wrap;
}

#audio-display { color: var(--cyan-dim); }

/* Controls */
#controls-panel {
  width: 100%;
  max-width: 560px;
}

#game-controls {
  display: flex;
  flex-direction: column;
  gap: 10px;
  align-items: center;
}

.control-row {
  display: flex;
  gap: 10px;
  justify-content: center;
  flex-wrap: wrap;
}

#keyboard-map {
  margin-top: 16px;
  padding: 12px;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 6px;
}

#keyboard-map h3 {
  font-family: 'Press Start 2P', monospace;
  font-size: 10px;
  color: var(--green-dim);
  margin-bottom: 8px;
  text-align: center;
}

.key-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 4px 16px;
  font-size: 13px;
}

kbd {
  background: var(--bg-input);
  border: 1px solid var(--green-dark);
  border-radius: 3px;
  padding: 1px 5px;
  font-family: 'VT323', monospace;
  color: var(--green);
  font-size: 14px;
}

/* Settings */
#settings-panel {
  width: 100%;
  max-width: 560px;
}

.settings-group {
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 14px;
}

.settings-group h3 {
  font-family: 'Press Start 2P', monospace;
  font-size: 10px;
  color: var(--green-dim);
  margin-bottom: 10px;
}

.setting-row {
  display: flex;
  align-items: center;
  gap: 10px;
  margin-bottom: 8px;
  font-size: 14px;
}

.setting-row label {
  min-width: 90px;
  color: var(--green-dim);
}

.setting-row select {
  background: var(--bg-input);
  color: var(--green);
  border: 1px solid var(--green-dark);
  padding: 4px 8px;
  font-family: 'VT323', monospace;
  font-size: 14px;
  border-radius: 3px;
}

.setting-row input[type="checkbox"] {
  accent-color: var(--green);
  width: 18px;
  height: 18px;
}

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

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

#volume-value {
  color: var(--green);
  font-size: 13px;
  min-width: 35px;
}

/* SN76477 Panel */
#sn76477-panel {
  margin-top: 12px;
  background: var(--pcb-green);
  border: 1px solid var(--pcb-trace);
  border-radius: 6px;
  padding: 10px;
  box-shadow: 0 0 10px rgba(184, 134, 11, 0.1);
}

#sn76477-panel > summary {
  font-family: 'Press Start 2P', monospace;
  font-size: 10px;
  color: var(--amber);
  cursor: pointer;
  user-select: none;
  text-shadow: var(--glow-amber);
  padding: 4px;
}

#sn76477-content {
  margin-top: 10px;
}

/* Chip Block Diagram */
#chip-diagram {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 2px;
  flex-wrap: wrap;
  padding: 8px 4px;
  background: rgba(0,0,0,0.3);
  border: 1px solid rgba(184, 134, 11, 0.3);
  border-radius: 4px;
  margin-bottom: 10px;
  position: relative;
}

.chip-block {
  background: rgba(0, 30, 10, 0.8);
  border: 1px solid var(--green-dark);
  border-radius: 3px;
  padding: 4px 6px;
  text-align: center;
  font-family: 'Press Start 2P', monospace;
  font-size: 7px;
  color: var(--green-dark);
  min-width: 38px;
  position: relative;
  transition: all 0.15s;
}

.chip-block.active {
  border-color: var(--green);
  color: var(--green);
  box-shadow: 0 0 8px rgba(0, 255, 65, 0.3);
  background: rgba(0, 60, 20, 0.8);
}

.chip-block-bottom {
  position: absolute;
  bottom: -2px;
  left: 50%;
  transform: translate(-50%, 100%);
}

.block-led {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: #002200;
  margin: 0 auto 2px;
  transition: all 0.15s;
}

.chip-block.active .block-led {
  background: var(--green);
  box-shadow: 0 0 6px var(--green);
}

.block-value {
  font-family: 'VT323', monospace;
  font-size: 9px;
  color: var(--cyan-dim);
  margin-top: 1px;
}

.chip-block.active .block-value { color: var(--cyan); }

.chip-arrow {
  color: var(--pcb-trace);
  font-size: 10px;
  font-weight: bold;
}

/* Sound Channels */
#sound-channels {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  gap: 4px;
  margin-bottom: 10px;
}

.sound-channel {
  background: rgba(0,0,0,0.4);
  border: 1px solid #1a1a1a;
  border-radius: 3px;
  padding: 3px 4px;
  text-align: center;
  font-family: 'Press Start 2P', monospace;
  font-size: 6px;
  color: #333;
  transition: all 0.1s;
}

.sound-channel.active {
  color: var(--cyan);
  border-color: var(--cyan-dim);
  background: rgba(0, 229, 255, 0.08);
}

.ch-led {
  font-size: 8px;
  color: #220000;
  transition: all 0.1s;
}

.sound-channel.active .ch-led {
  color: #ff3333;
  text-shadow: 0 0 6px #ff0000;
}

/* Oscilloscope */
#oscilloscope-container {
  background: rgba(0,0,0,0.5);
  border: 1px solid var(--green-dark);
  border-radius: 4px;
  padding: 4px;
  margin-bottom: 8px;
}

#oscilloscope {
  width: 100%;
  height: 64px;
  display: block;
  image-rendering: auto;
}

/* Tuning Panel */
#tuning-panel {
  margin-top: 8px;
  background: rgba(0,0,0,0.2);
  border: 1px solid rgba(184, 134, 11, 0.2);
  border-radius: 4px;
  padding: 6px;
}

#tuning-panel > summary {
  font-family: 'Press Start 2P', monospace;
  font-size: 8px;
  color: var(--cyan-dim);
  cursor: pointer;
  user-select: none;
}

.tuning-controls { margin-top: 8px; }

.tuning-row {
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 5px;
  font-size: 12px;
}

.tuning-row label {
  min-width: 80px;
  color: var(--cyan-dim);
  font-size: 11px;
}

.tuning-row span {
  color: var(--cyan);
  font-size: 11px;
  min-width: 40px;
  text-align: right;
}

.preset-buttons {
  display: flex;
  gap: 4px;
  margin-top: 8px;
  flex-wrap: wrap;
}

.btn-preset {
  font-family: 'Press Start 2P', monospace;
  font-size: 6px;
  padding: 5px 8px;
  background: rgba(0,0,0,0.4);
  color: var(--cyan-dim);
  border: 1px solid var(--cyan-dim);
  border-radius: 3px;
  cursor: pointer;
  transition: all 0.15s;
}

.btn-preset:hover { border-color: var(--cyan); color: var(--cyan); }

.btn-preset.active {
  background: rgba(0, 229, 255, 0.15);
  border-color: var(--cyan);
  color: var(--cyan);
  box-shadow: var(--glow-cyan);
}

/* Debug */
#debug-panel {
  margin-top: 12px;
  background: var(--bg-panel);
  border: 1px solid var(--border);
  border-radius: 6px;
  padding: 10px;
}

#debug-panel summary {
  font-family: 'Press Start 2P', monospace;
  font-size: 9px;
  color: var(--amber);
  cursor: pointer;
  user-select: none;
}

#debug-content { margin-top: 10px; }

#cpu-state {
  font-family: 'VT323', monospace;
  font-size: 13px;
  color: var(--green);
  background: #000;
  padding: 8px;
  border-radius: 4px;
  border: 1px solid var(--border);
  overflow-x: auto;
  white-space: pre;
  line-height: 1.5;
}

/* Memory Corruption Panel */
#corruption-panel {
  margin-top: 12px;
  background: var(--bg-panel);
  border: 1px solid #442200;
  border-radius: 6px;
  padding: 14px;
  box-shadow: 0 0 10px rgba(255, 100, 0, 0.08);
}

#corruption-panel h3 {
  font-family: 'Press Start 2P', monospace;
  font-size: 10px;
  color: var(--amber);
  margin-bottom: 4px;
  text-shadow: 0 0 8px rgba(255, 176, 0, 0.4);
}

.corruption-desc { font-size: 12px; color: #886622; margin-bottom: 10px; }

.corruption-controls { display: flex; flex-direction: column; gap: 8px; }

.corruption-slider-row,
.corruption-region-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 14px;
}

.corruption-slider-row label,
.corruption-region-row label {
  min-width: 65px;
  color: var(--amber);
  font-size: 13px;
}

#corruption-amount { flex: 1; accent-color: var(--amber); cursor: pointer; height: 6px; }

.corruption-val {
  min-width: 70px;
  text-align: right;
  color: var(--amber);
  font-size: 13px;
  font-family: 'VT323', monospace;
}

#corruption-region {
  background: var(--bg-input);
  color: var(--amber);
  border: 1px solid #442200;
  padding: 4px 8px;
  font-family: 'VT323', monospace;
  font-size: 14px;
  border-radius: 3px;
  flex: 1;
}

.btn-corrupt {
  background: #331100;
  color: var(--amber);
  border: 2px solid #ff6600;
  font-family: 'Press Start 2P', monospace;
  font-size: 11px;
  padding: 10px 18px;
  cursor: pointer;
  border-radius: 4px;
  transition: all 0.15s;
  text-transform: uppercase;
  letter-spacing: 1px;
  margin-top: 4px;
  text-shadow: 0 0 6px rgba(255, 102, 0, 0.5);
  box-shadow: 0 0 10px rgba(255, 102, 0, 0.15);
}

.btn-corrupt:hover {
  background: #552200;
  box-shadow: 0 0 20px rgba(255, 102, 0, 0.4), 0 0 40px rgba(255, 102, 0, 0.15);
}

.btn-corrupt:active { transform: translateY(2px); background: #ff6600; color: #000; }

.btn-corrupt.corrupting { animation: corrupt-flash 0.3s ease-out; }

@keyframes corrupt-flash {
  0% { box-shadow: 0 0 30px rgba(255, 50, 0, 0.8); background: #ff3300; color: #fff; }
  100% { box-shadow: 0 0 10px rgba(255, 102, 0, 0.15); background: #331100; color: var(--amber); }
}

/* Footer */
footer {
  text-align: center;
  padding: 20px 10px;
  margin-top: 30px;
  border-top: 1px solid var(--green-dark);
}

footer a {
  color: var(--green-dim);
  text-decoration: none;
  font-family: 'Press Start 2P', monospace;
  font-size: 10px;
  letter-spacing: 1px;
}

footer a:hover { color: var(--green); text-shadow: var(--glow); }

.hidden { display: none !important; }

/* Responsive */
@media (max-width: 600px) {
  #rom-slots { grid-template-columns: 1fr; }
  .key-grid { grid-template-columns: 1fr; }
  .badge { position: static; margin-top: 4px; }
  .btn { font-size: 10px; padding: 8px 12px; }
  #sound-channels { grid-template-columns: repeat(3, 1fr); }
}

@media (min-width: 900px) {
  #main-container {
    display: grid;
    grid-template-columns: 1fr auto;
    grid-template-rows: auto auto;
    gap: 20px;
    align-items: start;
  }
  #rom-panel { grid-column: 1 / -1; }
  #screen-container { grid-column: 1; max-width: 100%; }
  #controls-panel { grid-column: 1; }
  #settings-panel { grid-column: 2; grid-row: 2 / 4; max-width: 300px; }
}