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

:root {
  --bg: #0e0e0e;
  --bg2: #1a1a1a;
  --amber: #D4A843;
  --amber-dim: #8a6e2a;
  --orange: #FF6B35;
  --green: #4CAF50;
  --red: #FF4444;
  --text: #E0E0E0;
  --text-dim: #888;
  --wood: linear-gradient(135deg, #5c3d1e 0%, #7a5230 30%, #4e2f14 60%, #6b4425 100%);
  --wood-dark: linear-gradient(135deg, #3a2410 0%, #4e3018 50%, #2e1a0a 100%);
  --plastic: #1c1c1c;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: 'IBM Plex Mono', monospace;
  min-height: 100vh;
  overflow-x: hidden;
}

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

/* HEADER */
#header {
  text-align: center;
  padding: 24px 0 12px;
  position: relative;
  overflow: hidden;
  background: linear-gradient(180deg, #0e0e0e 0%, #1a1210 100%);
  border-bottom: 2px solid var(--amber-dim);
}
#header h1 {
  font-family: 'Press Start 2P', monospace;
  font-size: clamp(14px, 3.5vw, 24px);
  color: var(--amber);
  text-shadow: 0 0 20px rgba(212,168,67,0.5), 0 0 40px rgba(212,168,67,0.2);
  letter-spacing: 2px;
  position: relative;
  z-index: 1;
}
.subtitle {
  font-family: 'Press Start 2P', monospace;
  font-size: clamp(7px, 1.5vw, 10px);
  color: var(--amber-dim);
  margin-top: 8px;
  position: relative;
  z-index: 1;
}
.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;
  z-index: 2;
  animation: scanlineShift 8s linear infinite;
}
@keyframes scanlineShift {
  0% { transform: translateY(0); }
  100% { transform: translateY(4px); }
}

/* ROM UPLOAD */
.cartridge-slot {
  margin: 20px auto;
  max-width: 500px;
}
.cartridge-shape {
  background: var(--wood-dark);
  border: 3px solid #3a2410;
  border-radius: 8px 8px 4px 4px;
  padding: 28px 20px;
  text-align: center;
  cursor: pointer;
  position: relative;
  transition: box-shadow 0.3s, border-color 0.3s;
}
.cartridge-shape:hover {
  border-color: var(--amber);
  box-shadow: 0 0 20px rgba(212,168,67,0.3), inset 0 0 30px rgba(0,0,0,0.5);
}
.cartridge-shape::before {
  content: '';
  display: block;
  width: 80%;
  height: 6px;
  background: #222;
  border-radius: 0 0 3px 3px;
  margin: 0 auto 16px;
  box-shadow: inset 0 2px 3px rgba(0,0,0,0.8);
}
.cartridge-label {
  font-family: 'Press Start 2P', monospace;
  font-size: clamp(10px, 2.2vw, 16px);
  color: var(--amber);
  margin-bottom: 8px;
}
.cart-sub {
  font-size: 11px;
  color: var(--text-dim);
  line-height: 1.5;
}
.dim { opacity: 0.6; }
#rom-input {
  position: absolute;
  top: 0; left: 0; width: 100%; height: 100%;
  opacity: 0;
  cursor: pointer;
}
.rom-info {
  background: #111;
  border: 1px solid #333;
  padding: 8px 14px;
  margin-top: 8px;
  border-radius: 4px;
  font-family: 'IBM Plex Mono', monospace;
  font-size: 12px;
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
  color: var(--green);
}
.rom-info.hidden { display: none; }

/* CRT SCREEN */
#screen-area {
  display: flex;
  justify-content: center;
  margin: 16px 0;
}
.crt-bezel {
  background: linear-gradient(145deg, #2a2a2a, #1a1a1a);
  border: 4px solid #222;
  border-radius: 18px;
  padding: 20px 24px;
  box-shadow: 0 0 40px rgba(0,0,0,0.8), inset 0 0 20px rgba(0,0,0,0.5);
  position: relative;
}
.crt-bezel::after {
  content: '';
  position: absolute;
  bottom: -16px;
  left: 10%;
  right: 10%;
  height: 16px;
  background: var(--wood);
  border-radius: 0 0 6px 6px;
}
.crt-screen-wrap {
  position: relative;
  overflow: hidden;
  border-radius: 8px;
  background: #000;
  line-height: 0;
}
#canvas {
  width: min(640px, calc(100vw - 80px));
  height: auto;
  aspect-ratio: 4 / 3;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  display: block;
}
.crt-overlay {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.3s;
}
.crt-overlay.active {
  opacity: 1;
  background:
    repeating-linear-gradient(
      0deg,
      rgba(0,0,0,0.12) 0px,
      rgba(0,0,0,0.12) 1px,
      transparent 1px,
      transparent 3px
    );
  box-shadow: inset 0 0 60px rgba(0,0,0,0.4);
}

/* CONTROL PANEL */
#control-panel {
  margin: 24px 0 12px;
}
.console-switches {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  margin-bottom: 12px;
}
.switch-btn {
  font-family: 'Press Start 2P', monospace;
  font-size: 8px;
  background: linear-gradient(180deg, #444, #2a2a2a);
  border: 2px solid #555;
  border-radius: 4px;
  color: var(--text);
  padding: 10px 12px;
  cursor: pointer;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
  transition: all 0.15s;
  min-width: 72px;
}
.switch-btn:hover { border-color: var(--amber); background: linear-gradient(180deg, #555, #333); }
.switch-btn:active { transform: scale(0.95); }
.switch-btn .sw-label { font-size: 6px; color: var(--text-dim); text-transform: uppercase; }
.switch-btn .sw-val { font-size: 8px; color: var(--amber); }
.power-btn[data-state="on"] { border-color: var(--green); box-shadow: 0 0 10px rgba(76,175,80,0.4); }
.power-btn[data-state="on"] .sw-label { color: var(--green); }

.playback-controls {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: center;
  margin-bottom: 10px;
}
.ctrl-btn {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 11px;
  background: #222;
  border: 1px solid #444;
  color: var(--text);
  padding: 6px 10px;
  border-radius: 3px;
  cursor: pointer;
  transition: all 0.15s;
}
.ctrl-btn:hover { background: #333; border-color: var(--amber); }
.ctrl-btn:active { transform: scale(0.95); }
.speed-group { display: flex; gap: 4px; }
.speed-btn.active { background: var(--amber-dim); color: #fff; border-color: var(--amber); }

.crt-toggle-row {
  display: flex;
  gap: 16px;
  justify-content: center;
  font-size: 11px;
  color: var(--text-dim);
}
.crt-toggle-row label { cursor: pointer; display: flex; align-items: center; gap: 4px; }
.crt-toggle-row input { accent-color: var(--amber); }

/* JOYSTICK DISPLAY */
#joystick-display {
  margin: 16px auto;
  max-width: 320px;
  text-align: center;
}
.joy-title {
  font-family: 'Press Start 2P', monospace;
  font-size: 9px;
  color: var(--amber-dim);
  margin-bottom: 8px;
}
.joy-grid {
  display: grid;
  grid-template-columns: 36px 36px 36px;
  gap: 3px;
  justify-content: center;
  margin-bottom: 8px;
}
.joy-cell {
  width: 36px; height: 36px;
  display: flex; align-items: center; justify-content: center;
  background: #1a1a1a;
  border: 1px solid #333;
  border-radius: 4px;
  font-size: 14px;
  color: #555;
  transition: all 0.1s;
}
.joy-cell.active { background: var(--orange); color: #fff; border-color: var(--orange); box-shadow: 0 0 8px rgba(255,107,53,0.5); }
.joy-center { background: #333; border-radius: 50%; }
.joy-fire {
  font-family: 'Press Start 2P', monospace;
  font-size: 9px;
  background: #333;
  border: 2px solid #555;
  color: #888;
  border-radius: 50%;
  width: 56px; height: 56px;
  display: flex; align-items: center; justify-content: center;
  margin: 8px auto;
  transition: all 0.1s;
}
.joy-fire.active { background: var(--red); color: #fff; border-color: var(--red); box-shadow: 0 0 12px rgba(255,68,68,0.6); }
.key-help {
  font-size: 9px;
  color: var(--text-dim);
  display: flex;
  flex-direction: column;
  gap: 2px;
  margin-top: 8px;
}

/* DEBUG */
#debug-toggle-bar {
  text-align: center;
  margin: 8px 0;
}
#btn-debug-toggle {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 11px;
  background: none;
  border: 1px solid #333;
  color: var(--amber-dim);
  padding: 6px 20px;
  border-radius: 3px;
  cursor: pointer;
}
#btn-debug-toggle:hover { border-color: var(--amber); color: var(--amber); }

#debug-panel {
  background: #111;
  border: 1px solid #2a2a2a;
  border-radius: 6px;
  padding: 14px;
  margin: 8px 0 16px;
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(240px, 1fr));
  gap: 12px;
}
#debug-panel.hidden { display: none; }
.debug-section {
  background: #0a0a0a;
  border: 1px solid #222;
  border-radius: 4px;
  padding: 10px;
}
.debug-section h3 {
  font-family: 'Press Start 2P', monospace;
  font-size: 8px;
  color: var(--amber);
  margin-bottom: 8px;
  border-bottom: 1px solid #222;
  padding-bottom: 4px;
}
.mono-grid {
  display: flex;
  flex-direction: column;
  gap: 3px;
  font-size: 11px;
  font-family: 'IBM Plex Mono', monospace;
}
.mono-grid b { color: var(--green); }
.swatch {
  display: inline-block;
  width: 10px; height: 10px;
  border: 1px solid #444;
  vertical-align: middle;
}
#beam-canvas {
  width: 100%;
  height: auto;
  background: #000;
  border: 1px solid #222;
  border-radius: 2px;
}
.ram-dump, .disasm {
  font-size: 9px;
  font-family: 'IBM Plex Mono', monospace;
  color: var(--text-dim);
  line-height: 1.5;
  max-height: 150px;
  overflow-y: auto;
  white-space: pre;
}

/* STATUS BAR */
#status-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  justify-content: center;
  font-size: 10px;
  color: var(--text-dim);
  padding: 8px;
  border-top: 1px solid #222;
  margin-bottom: 8px;
}

/* FOOTER */
footer {
  text-align: center;
  padding: 20px;
  border-top: 1px solid #222;
  font-size: 11px;
  color: var(--text-dim);
}
footer a {
  color: var(--amber);
  text-decoration: none;
  font-family: 'Press Start 2P', monospace;
  font-size: 10px;
}
footer a:hover { text-decoration: underline; }
footer p { margin-top: 6px; }

/* SCROLLBAR */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: #111; }
::-webkit-scrollbar-thumb { background: #333; border-radius: 3px; }

/* Responsive */
@media (max-width: 600px) {
  #canvas { width: calc(100vw - 64px); }
  .console-switches { gap: 4px; }
  .switch-btn { min-width: 56px; padding: 8px 6px; }
  .switch-btn .sw-label { font-size: 5px; }
  #debug-panel { grid-template-columns: 1fr; }
}