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

:root {
  --bg: #0a0a0f;
  --bg2: #12121a;
  --amber: #ffb000;
  --green: #00ff41;
  --red: #ff3344;
  --cyan: #00e5ff;
  --panel-bg: #0d0d16;
  --panel-border: #1a1a2e;
  --text: #c0c0c0;
  --text-dim: #666;
  --bezel: #1a1008;
  --wood: #2a1a0a;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Fira Code', monospace;
  font-size: 11px;
  overflow-x: hidden;
}

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

/* Header */
header {
  text-align: center;
  padding: 15px 0 10px;
  position: relative;
}

.title {
  font-family: 'Press Start 2P', monospace;
  font-size: 22px;
  color: var(--amber);
  text-shadow: 0 0 20px rgba(255,176,0,0.4), 0 2px 0 #996600;
  animation: scanlineTitle 3s linear infinite;
}

.subtitle {
  font-family: 'Fira Code', monospace;
  font-size: 10px;
  color: var(--text-dim);
  margin-top: 6px;
  letter-spacing: 1px;
}

@keyframes scanlineTitle {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.92; }
}

/* Main Layout */
#main-layout {
  display: flex;
  gap: 10px;
  margin-top: 10px;
}

#center-column {
  flex: 1;
  min-width: 0;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
}

/* Debug Panels */
.debug-panel {
  width: 260px;
  min-width: 220px;
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: 6px;
  padding: 8px;
  max-height: 80vh;
  overflow-y: auto;
  font-size: 10px;
}

.debug-panel::-webkit-scrollbar { width: 6px; }
.debug-panel::-webkit-scrollbar-track { background: var(--bg); }
.debug-panel::-webkit-scrollbar-thumb { background: var(--panel-border); border-radius: 3px; }

.panel-title {
  font-family: 'Press Start 2P', monospace;
  font-size: 10px;
  color: var(--green);
  margin-bottom: 8px;
  text-shadow: 0 0 8px rgba(0,255,65,0.3);
}

/* CPU Panel */
.reg-row {
  display: flex;
  justify-content: space-between;
  padding: 2px 4px;
  border-bottom: 1px solid #111;
}
.reg-name { color: var(--amber); font-weight: bold; }
.reg-val { color: var(--green); }
.flag-row { display: flex; gap: 4px; margin: 6px 0; flex-wrap: wrap; }
.flag-led {
  width: 24px; height: 20px;
  display: flex; align-items: center; justify-content: center;
  border-radius: 3px;
  font-size: 9px; font-weight: bold;
  background: #1a1a1a;
  color: #444;
  border: 1px solid #222;
  transition: all 0.15s;
}
.flag-led.on {
  background: #003300;
  color: var(--green);
  border-color: var(--green);
  box-shadow: 0 0 6px rgba(0,255,65,0.4);
}
.instr-display {
  background: #000;
  border: 1px solid var(--panel-border);
  padding: 6px;
  margin: 6px 0;
  font-size: 11px;
  color: var(--cyan);
  border-radius: 3px;
}
.stack-byte { color: var(--text-dim); }
.mem-map-bar {
  display: flex; height: 12px; margin: 4px 0; border-radius: 2px; overflow: hidden;
}
.mem-map-bar div { height: 100%; }

/* Cartridge Slot */
#cartridge-slot {
  width: 100%;
  max-width: 660px;
  background: linear-gradient(180deg, #2a2a2a 0%, #1a1a1a 100%);
  border: 2px solid #333;
  border-radius: 8px 8px 4px 4px;
  padding: 8px;
  position: relative;
}
#cartridge-slot::before {
  content: '';
  position: absolute;
  top: -3px; left: 30%; right: 30%; height: 6px;
  background: #111;
  border-radius: 0 0 4px 4px;
  border: 1px solid #333;
  border-top: none;
}
#cart-slot-inner {
  background: #0a0a0a;
  border: 2px inset #222;
  border-radius: 4px;
  padding: 12px;
  text-align: center;
}
#cart-drop-zone {
  border: 2px dashed #444;
  border-radius: 4px;
  padding: 15px;
  transition: border-color 0.2s, background 0.2s;
  cursor: pointer;
}
#cart-drop-zone.dragover {
  border-color: var(--amber);
  background: rgba(255,176,0,0.05);
}
.cart-text {
  font-family: 'Press Start 2P', monospace;
  font-size: 12px;
  color: var(--amber);
  display: block;
  margin-bottom: 4px;
}
.cart-subtext {
  color: var(--text-dim);
  font-size: 9px;
  display: block;
  margin-bottom: 8px;
}
#load-rom-btn {
  font-family: 'Press Start 2P', monospace;
  font-size: 10px;
  padding: 8px 20px;
  background: linear-gradient(180deg, #444 0%, #222 100%);
  color: var(--amber);
  border: 2px outset #555;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.1s;
}
#load-rom-btn:active {
  border-style: inset;
  transform: translateY(1px);
}
#cart-label {
  background: #c4483a;
  color: white;
  font-family: 'Press Start 2P', monospace;
  font-size: 9px;
  padding: 8px 12px;
  border-radius: 3px;
  margin-top: 8px;
  word-break: break-all;
  text-shadow: 1px 1px 0 rgba(0,0,0,0.3);
}
.cart-warning {
  color: var(--red);
  font-size: 9px;
  margin-top: 4px;
}

/* TV Screen */
#tv-cabinet {
  background: linear-gradient(135deg, var(--wood) 0%, #1a0f05 100%);
  border-radius: 16px;
  padding: 16px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.6), inset 0 1px 0 rgba(255,255,255,0.05);
  width: 100%;
  max-width: 660px;
}
#tv-bezel {
  background: #111;
  border-radius: 12px;
  padding: 10px;
  box-shadow: inset 0 0 30px rgba(0,0,0,0.8);
}
#tv-screen-container {
  position: relative;
  width: 100%;
  aspect-ratio: 4/3;
  background: #000;
  border-radius: 8px;
  overflow: hidden;
}
#tv-screen {
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
  image-rendering: -moz-crisp-edges;
  display: block;
}
#scanline-overlay {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  background: repeating-linear-gradient(
    0deg,
    transparent,
    transparent 2px,
    rgba(0,0,0,0.15) 2px,
    rgba(0,0,0,0.15) 4px
  );
  pointer-events: none;
}
#crt-vignette {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  background: radial-gradient(ellipse at center, transparent 60%, rgba(0,0,0,0.5) 100%);
  pointer-events: none;
}
#no-signal {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  display: none;
  align-items: center;
  justify-content: center;
  background: #000;
  z-index: 2;
}
#no-signal.active {
  display: flex;
}
#no-signal span {
  font-family: 'Press Start 2P', monospace;
  font-size: 14px;
  color: #555;
  animation: flicker 1.5s infinite;
}
@keyframes flicker {
  0%, 100% { opacity: 0.6; }
  50% { opacity: 1; }
}
#tv-logo {
  text-align: center;
  font-family: 'Press Start 2P', monospace;
  font-size: 8px;
  color: #4a3a2a;
  margin-top: 6px;
  letter-spacing: 3px;
}
.tv-running #tv-bezel {
  box-shadow: inset 0 0 30px rgba(0,0,0,0.8), 0 0 15px rgba(0,200,255,0.1);
}

/* Console Controls */
#console-controls {
  display: flex;
  gap: 12px;
  align-items: center;
  flex-wrap: wrap;
  justify-content: center;
  background: linear-gradient(180deg, #2a2a2a, #1a1a1a);
  padding: 10px 16px;
  border-radius: 6px;
  border: 1px solid #333;
  width: 100%;
  max-width: 660px;
}
.control-group {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}
.control-group label {
  font-family: 'Press Start 2P', monospace;
  font-size: 7px;
  color: var(--text-dim);
}
.switch {
  width: 50px; height: 24px;
  background: #1a1a1a;
  border: 2px solid #444;
  border-radius: 12px;
  cursor: pointer;
  position: relative;
  transition: all 0.2s;
}
.switch-handle {
  width: 18px; height: 18px;
  background: linear-gradient(180deg, #666, #444);
  border-radius: 50%;
  position: absolute;
  top: 1px; left: 2px;
  transition: left 0.2s;
  box-shadow: 0 1px 3px rgba(0,0,0,0.5);
}
.switch.on .switch-handle {
  left: 26px;
}
.switch.on {
  border-color: var(--green);
  box-shadow: 0 0 8px rgba(0,255,65,0.2);
}
.console-btn {
  font-family: 'Press Start 2P', monospace;
  font-size: 8px;
  padding: 6px 12px;
  background: linear-gradient(180deg, #555, #333);
  color: #ccc;
  border: 2px outset #666;
  border-radius: 3px;
  cursor: pointer;
}
.console-btn:active {
  border-style: inset;
  transform: translateY(1px);
}

/* Speed Controls */
#speed-controls {
  display: flex;
  gap: 6px;
  align-items: center;
  flex-wrap: wrap;
  justify-content: center;
  width: 100%;
  max-width: 660px;
}
.emu-btn {
  font-family: 'Fira Code', monospace;
  font-size: 10px;
  padding: 5px 10px;
  background: var(--panel-bg);
  color: var(--amber);
  border: 1px solid var(--panel-border);
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.15s;
}
.emu-btn:hover {
  border-color: var(--amber);
  box-shadow: 0 0 6px rgba(255,176,0,0.2);
}
.speed-slider-wrap {
  display: flex; align-items: center; gap: 6px;
  color: var(--text-dim);
  font-size: 10px;
}
#speed-slider {
  width: 80px;
  accent-color: var(--amber);
}

/* Virtual Joystick */
#input-section {
  display: flex;
  gap: 20px;
  align-items: center;
  justify-content: center;
  width: 100%;
  max-width: 660px;
}
#virtual-joystick {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}
.joy-row {
  display: flex;
  gap: 2px;
  align-items: center;
}
.joy-btn {
  width: 40px; height: 40px;
  background: linear-gradient(180deg, #444, #222);
  color: #aaa;
  border: 2px outset #555;
  border-radius: 6px;
  font-size: 16px;
  cursor: pointer;
  display: flex; align-items: center; justify-content: center;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
}
.joy-btn:active, .joy-btn.pressed {
  border-style: inset;
  background: linear-gradient(180deg, #333, #111);
  color: var(--amber);
}
.joy-center {
  width: 40px; height: 40px;
  background: radial-gradient(circle, #333, #1a1a1a);
  border-radius: 50%;
  border: 2px solid #444;
}
.fire-btn {
  font-family: 'Press Start 2P', monospace;
  font-size: 9px;
  width: 80px; height: 36px;
  margin-top: 6px;
  background: linear-gradient(180deg, #a03030, #601818);
  color: white;
  border: 2px outset #c04040;
  border-radius: 6px;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
}
.fire-btn:active, .fire-btn.pressed {
  border-style: inset;
  background: linear-gradient(180deg, #801010, #400808);
}

#key-map {
  color: var(--text-dim);
  font-size: 10px;
  line-height: 1.8;
}
#key-map h3 {
  color: var(--amber);
  font-family: 'Press Start 2P', monospace;
  font-size: 9px;
  margin-bottom: 4px;
}

/* Scanline Viz */
#scanline-viz {
  width: 100%;
  max-width: 660px;
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: 4px;
  padding: 8px;
}
#scanline-viz h3 {
  font-size: 9px;
  color: var(--amber);
  font-family: 'Press Start 2P', monospace;
  margin-bottom: 4px;
}
#beam-bar {
  height: 10px;
  background: #111;
  border-radius: 2px;
  overflow: hidden;
  position: relative;
}
#beam-pos {
  height: 100%;
  width: 2px;
  background: var(--green);
  box-shadow: 0 0 6px var(--green);
  position: absolute;
  left: 0;
  transition: left 0.05s;
}
#beam-info {
  color: var(--text-dim);
  font-size: 9px;
  margin-top: 3px;
  text-align: center;
}

/* TIA Tabs */
#tia-tabs {
  display: flex;
  flex-wrap: wrap;
  gap: 2px;
  margin-bottom: 6px;
}
.tia-tab {
  font-family: 'Fira Code', monospace;
  font-size: 8px;
  padding: 3px 6px;
  background: #111;
  color: var(--text-dim);
  border: 1px solid #222;
  border-radius: 3px;
  cursor: pointer;
}
.tia-tab.active {
  background: #1a1a2e;
  color: var(--green);
  border-color: var(--green);
}
#tia-content {
  font-size: 9px;
}
.tia-reg-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 2px 4px;
  border-bottom: 1px solid #0d0d0d;
}
.tia-reg-addr { color: var(--text-dim); min-width: 28px; }
.tia-reg-name { color: var(--amber); min-width: 50px; }
.tia-reg-val { color: var(--green); min-width: 24px; }
.tia-reg-bin { color: var(--text-dim); font-size: 8px; }
.color-swatch {
  display: inline-block;
  width: 14px; height: 10px;
  border: 1px solid #333;
  border-radius: 1px;
  vertical-align: middle;
}
.sprite-preview {
  display: inline-flex; gap: 0;
}
.sprite-px {
  width: 4px; height: 8px;
  display: inline-block;
}
.pf-preview {
  display: inline-flex; gap: 0;
}
.pf-px {
  width: 3px; height: 6px;
  display: inline-block;
}
.collision-indicator {
  width: 8px; height: 8px;
  border-radius: 50%;
  display: inline-block;
}
.collision-indicator.active { background: var(--green); box-shadow: 0 0 4px var(--green); }
.collision-indicator.inactive { background: #222; }

/* Bottom Bar */
#bottom-bar {
  display: flex;
  gap: 6px;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
  margin-top: 10px;
  padding: 8px;
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: 4px;
}
#fps-display {
  color: var(--green);
  font-size: 10px;
  margin-left: auto;
}

/* Memory Viewer */
#memory-viewer, #palette-viewer {
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: 4px;
  padding: 10px;
  margin-top: 6px;
}
#memory-viewer h3, #palette-viewer h3 {
  font-family: 'Press Start 2P', monospace;
  font-size: 9px;
  color: var(--amber);
  margin-bottom: 6px;
}
#ram-hex {
  font-family: 'Fira Code', monospace;
  font-size: 10px;
  line-height: 1.6;
  color: var(--green);
  word-break: break-all;
}
.ram-byte { padding: 0 2px; }
.ram-byte.changed { color: #ffff00; background: rgba(255,255,0,0.1); }

/* Palette */
#palette-grid {
  display: grid;
  grid-template-columns: repeat(16, 1fr);
  gap: 2px;
}
.palette-cell {
  aspect-ratio: 1;
  border-radius: 2px;
  cursor: pointer;
  position: relative;
  border: 1px solid transparent;
}
.palette-cell:hover {
  border-color: white;
  transform: scale(1.3);
  z-index: 1;
}
.palette-tooltip {
  display: none;
  position: absolute;
  bottom: 100%;
  left: 50%;
  transform: translateX(-50%);
  background: #000;
  color: white;
  padding: 2px 4px;
  font-size: 8px;
  border-radius: 2px;
  white-space: nowrap;
  z-index: 10;
}
.palette-cell:hover .palette-tooltip { display: block; }

/* Footer */
footer {
  text-align: center;
  padding: 16px;
  margin-top: 10px;
}
footer a {
  color: var(--text-dim);
  text-decoration: none;
  font-size: 10px;
}
footer a:hover { color: var(--amber); }

/* Responsive */
@media (max-width: 1100px) {
  #main-layout { flex-direction: column; align-items: center; }
  .debug-panel { width: 100%; max-width: 660px; max-height: 300px; }
  #cpu-panel { order: 2; }
  #center-column { order: 1; }
  #tia-panel { order: 3; }
}
@media (max-width: 600px) {
  .title { font-size: 14px; }
  #console-controls { gap: 6px; }
  #input-section { flex-direction: column; }
  .debug-panel { font-size: 9px; }
}