:root {
  --wood-dark: #4a2810;
  --wood-med: #6b3a1f;
  --wood-light: #8b5e3c;
  --wood-grain: repeating-linear-gradient(
    87deg,
    #4a2810 0px, #5a3218 2px, #4a2810 4px, #6b3a1f 6px, #4a2810 8px
  );
  --console-black: #1a1a1a;
  --console-ridge: repeating-linear-gradient(
    0deg,
    #1a1a1a 0px, #222 1px, #1a1a1a 2px
  );
  --chrome: linear-gradient(180deg, #ddd 0%, #999 50%, #bbb 100%);
  --bg-deep: #0d0d1a;
  --amber: #ff9f1c;
  --amber-glow: #ff9f1c80;
  --green-led: #39ff14;
  --screen-glow: #39ff1420;
  --red-fire: #e63946;
  --text-dim: #8a8a9a;
  --text-bright: #e0e0e8;
}

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

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

/* ---- HEADER ---- */
#header {
  text-align: center;
  padding: 20px 16px 12px;
  background: linear-gradient(180deg, #12121f 0%, var(--bg-deep) 100%);
  border-bottom: 2px solid #2a2a3a;
  position: relative;
}
.title {
  font-family: 'Press Start 2P', cursive;
  font-size: clamp(20px, 4vw, 36px);
  color: var(--amber);
  text-shadow: 0 0 20px var(--amber-glow), 0 2px 0 #6b3a1f;
  letter-spacing: 4px;
  /* scanline overlay on title */
  background: repeating-linear-gradient(
    0deg,
    transparent 0px, transparent 2px, rgba(0,0,0,0.15) 2px, rgba(0,0,0,0.15) 4px
  );
  -webkit-background-clip: text;
  background-clip: text;
}
.subtitle {
  font-size: 11px;
  color: var(--text-dim);
  margin-top: 6px;
  letter-spacing: 1px;
}
.header-controls {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 16px;
  margin-top: 12px;
}

/* Hardware switch */
.hw-switch {
  display: flex; align-items: center; gap: 6px; cursor: pointer; font-size: 11px;
}
.sw-label { color: var(--text-dim); font-size: 10px; text-transform: uppercase; }
.sw-track {
  display: inline-block; width: 40px; height: 20px; background: #333; border-radius: 10px;
  position: relative; transition: background 0.3s;
  border: 1px solid #555;
}
.sw-track::after {
  content: ''; position: absolute; top: 2px; left: 2px; width: 16px; height: 16px;
  background: var(--chrome); border-radius: 50%; transition: transform 0.3s;
  box-shadow: 0 1px 3px rgba(0,0,0,0.5);
}
.hw-switch input:checked + .sw-track { background: #554422; }
.hw-switch input:checked + .sw-track::after { transform: translateX(20px); }
.hw-switch input { display: none; }

.icon-btn {
  background: none; border: 1px solid #444; color: var(--text-dim);
  padding: 4px 10px; border-radius: 4px; cursor: pointer; font-size: 16px;
  transition: all 0.2s;
}
.icon-btn:hover { border-color: var(--amber); color: var(--amber); }

/* ---- CARTRIDGE SLOT / DROP ZONE ---- */
.cartridge-slot {
  max-width: 500px; margin: 40px auto; padding: 40px; text-align: center;
  background: var(--wood-grain);
  border: 3px solid var(--wood-dark);
  border-radius: 8px;
  position: relative;
  cursor: pointer;
  transition: transform 0.2s, box-shadow 0.3s;
}
.cartridge-slot:hover {
  transform: scale(1.02);
  box-shadow: 0 0 30px rgba(255,159,28,0.2);
}
.cartridge-slot.drag-over {
  box-shadow: 0 0 40px var(--amber-glow);
  border-color: var(--amber);
}
.slot-inner { position: relative; z-index: 2; }
.slot-opening {
  width: 200px; height: 8px; background: #111; margin: 0 auto 20px;
  border: 2px solid #333; border-radius: 2px;
  box-shadow: inset 0 2px 4px rgba(0,0,0,0.8);
}
.slot-text {
  font-family: 'Press Start 2P', cursive; font-size: 14px; color: var(--amber);
  margin-bottom: 8px;
}
.slot-sub { font-size: 12px; color: var(--text-dim); }
.cartridge-sprite {
  position: absolute; top: -60px; left: 50%; transform: translateX(-50%);
  width: 120px; height: 80px; background: #222; border: 2px solid #444;
  border-radius: 4px 4px 0 0; transition: top 0.5s ease-in;
  z-index: 3;
}
.cartridge-sprite.inserting { top: 0px; }
.cartridge-sprite::after {
  content: 'ROM'; position: absolute; top: 50%; left: 50%; transform: translate(-50%,-50%);
  font-family: 'Press Start 2P', cursive; font-size: 10px; color: var(--amber);
}
.hidden { display: none !important; }

/* ---- TV FRAME ---- */
#tv-container { text-align: center; margin: 20px auto; }
.tv-frame {
  display: inline-flex;
  background: var(--wood-grain);
  border: 4px solid var(--wood-dark);
  border-radius: 20px;
  padding: 20px;
  box-shadow: 0 8px 40px rgba(0,0,0,0.6), inset 0 1px 0 var(--wood-light);
  position: relative;
}
.tv-dials {
  display: flex; flex-direction: column; align-items: center; justify-content: flex-end;
  padding: 0 12px 0 0; gap: 12px;
}
.dial {
  width: 30px; height: 30px; border-radius: 50%;
  background: radial-gradient(circle at 40% 40%, #bbb, #666);
  border: 2px solid #444;
  box-shadow: 0 2px 4px rgba(0,0,0,0.5);
}
.dial-label { font-size: 8px; color: var(--wood-light); }
.tv-screen-wrap {
  position: relative;
  background: #000;
  border-radius: 12px;
  overflow: hidden;
  box-shadow: inset 0 0 30px rgba(0,0,0,0.8), 0 0 20px var(--screen-glow);
}
#screen {
  display: block;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}
.crt-overlay {
  position: absolute; top: 0; left: 0; right: 0; bottom: 0;
  pointer-events: none;
  background: repeating-linear-gradient(
    0deg,
    transparent 0px, transparent 2px, rgba(0,0,0,0.12) 2px, rgba(0,0,0,0.12) 4px
  );
  border-radius: 12px;
}
.crt-overlay::after {
  content: ''; position: absolute; top: 0; left: 0; right: 0; bottom: 0;
  background: radial-gradient(ellipse at center, transparent 60%, rgba(0,0,0,0.3) 100%);
  border-radius: 12px;
}
.tv-brand {
  position: absolute; bottom: 6px; right: 20px;
  font-size: 9px; color: var(--wood-light); opacity: 0.6;
  font-family: 'Press Start 2P', cursive;
}
.debug-overlay {
  position: absolute; top: 4px; left: 4px; font-size: 10px;
  color: var(--green-led); background: rgba(0,0,0,0.6); padding: 2px 6px;
  border-radius: 2px; pointer-events: none;
}
.rom-info {
  font-size: 11px; color: var(--text-dim); margin-top: 8px;
}

/* ---- CONSOLE PANEL ---- */
.console-panel { max-width: 700px; margin: 16px auto; }
.console-surface {
  display: flex; align-items: stretch;
  background: var(--console-black);
  border-radius: 4px;
  overflow: hidden;
  box-shadow: 0 4px 16px rgba(0,0,0,0.5);
}
.console-wood {
  width: 40px; background: var(--wood-grain);
}
.console-switches {
  flex: 1; display: flex; justify-content: center; gap: 16px;
  padding: 12px 8px;
  background: var(--console-ridge);
  flex-wrap: wrap;
}
.switch-group {
  text-align: center;
}
.switch-group label {
  display: block; font-size: 8px; color: var(--text-dim); margin-top: 4px;
  font-family: 'Press Start 2P', cursive;
}
.console-switch {
  width: 36px; height: 24px; background: var(--chrome); border: none;
  border-radius: 3px; cursor: pointer; position: relative; overflow: hidden;
  box-shadow: 0 2px 4px rgba(0,0,0,0.6);
}
.console-switch .sw-toggle {
  display: block; width: 14px; height: 20px; background: linear-gradient(180deg,#ccc,#888);
  border-radius: 2px; margin: 2px auto; transition: transform 0.15s;
  box-shadow: 0 1px 2px rgba(0,0,0,0.4);
}
.console-switch[data-active="true"] .sw-toggle { transform: translateY(-2px); }
.console-switch[data-active="false"] .sw-toggle { transform: translateY(2px); }
.console-switch:active .sw-toggle { background: linear-gradient(180deg,#aaa,#666); }

/* Power LED */
#sw-power::after {
  content: ''; position: absolute; top: -6px; left: 50%; transform: translateX(-50%);
  width: 6px; height: 6px; border-radius: 50%; background: #333;
  box-shadow: none; transition: all 0.3s;
}
#sw-power[data-active="true"]::after {
  background: var(--green-led);
  box-shadow: 0 0 8px var(--green-led);
  animation: pulse-led 2s ease-in-out infinite;
}
@keyframes pulse-led {
  0%, 100% { box-shadow: 0 0 6px var(--green-led); }
  50% { box-shadow: 0 0 14px var(--green-led); }
}

/* ---- CONTROLLER ---- */
.controller-area {
  max-width: 700px; margin: 16px auto; display: flex;
  gap: 24px; justify-content: center; align-items: center; flex-wrap: wrap;
}
.controller-panel {
  display: flex; align-items: center; gap: 30px;
  background: #1e1e2e; padding: 20px 24px; border-radius: 12px;
  border: 2px solid #333;
}
.dpad {
  display: grid; grid-template: 40px 40px 40px / 40px 40px 40px;
  gap: 2px;
}
.dpad-btn {
  background: #2a2a3a; color: var(--text-bright); border: 1px solid #444;
  font-size: 16px; cursor: pointer; display: flex; align-items: center; justify-content: center;
  transition: background 0.1s;
}
.dpad-btn:active, .dpad-btn.pressed { background: var(--amber); color: #000; }
.dpad-btn.up { grid-column: 2; grid-row: 1; border-radius: 6px 6px 0 0; }
.dpad-btn.left { grid-column: 1; grid-row: 2; border-radius: 6px 0 0 6px; }
.dpad-btn.right { grid-column: 3; grid-row: 2; border-radius: 0 6px 6px 0; }
.dpad-btn.down { grid-column: 2; grid-row: 3; border-radius: 0 0 6px 6px; }
.dpad-center { grid-column: 2; grid-row: 2; background: #333; }
.fire-btn {
  width: 70px; height: 70px; border-radius: 50%;
  background: var(--red-fire); color: white; font-family: 'Press Start 2P', cursive;
  font-size: 10px; border: 3px solid #c1121f; cursor: pointer;
  box-shadow: 0 4px 12px rgba(230,57,70,0.4);
  transition: all 0.1s;
}
.fire-btn:active, .fire-btn.pressed {
  transform: scale(0.95); box-shadow: 0 2px 6px rgba(230,57,70,0.6);
  background: #c1121f;
}
.key-map {
  font-size: 11px; color: var(--text-dim); line-height: 1.8;
}
.key-map h3 {
  font-family: 'Press Start 2P', cursive; font-size: 10px; color: var(--amber);
  margin-bottom: 6px;
}
.km-row kbd {
  background: #2a2a3a; padding: 1px 6px; border-radius: 3px;
  border: 1px solid #555; font-family: 'JetBrains Mono', monospace; font-size: 10px;
  color: var(--text-bright);
}

/* ---- EMU CONTROLS ---- */
.emu-controls {
  max-width: 700px; margin: 12px auto; display: flex; flex-wrap: wrap;
  gap: 8px; justify-content: center; align-items: center;
}
.emu-btn {
  background: #1e1e2e; color: var(--text-bright); border: 1px solid #444;
  padding: 6px 14px; border-radius: 4px; cursor: pointer;
  font-family: 'JetBrains Mono', monospace; font-size: 11px;
  transition: all 0.2s;
}
.emu-btn:hover { border-color: var(--amber); color: var(--amber); }
.perf-stats {
  font-size: 10px; color: var(--green-led); margin-left: 8px;
}

/* ---- DEBUG PANEL ---- */
.debug-panel {
  max-width: 900px; margin: 16px auto; display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 12px; padding: 0 16px;
}
.debug-section {
  background: #111118; border: 1px solid #2a2a3a; border-radius: 6px;
  padding: 10px; overflow: hidden;
}
.debug-section h3 {
  font-family: 'Press Start 2P', cursive; font-size: 8px; color: var(--amber);
  margin-bottom: 6px; padding-bottom: 4px; border-bottom: 1px solid #2a2a3a;
}
.debug-section pre {
  font-size: 10px; color: var(--green-led); line-height: 1.5;
  white-space: pre-wrap; word-break: break-all;
  max-height: 180px; overflow-y: auto;
}
.beam-bar {
  height: 8px; background: #1a1a2e; border: 1px solid #333;
  border-radius: 4px; position: relative; overflow: hidden; margin-bottom: 6px;
}
.beam-pos {
  position: absolute; top: 0; left: 0; width: 3px; height: 100%;
  background: var(--green-led); box-shadow: 0 0 6px var(--green-led);
  transition: left 0.01s linear;
}

/* ---- SETTINGS MODAL ---- */
.modal {
  position: fixed; top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.8); z-index: 1000;
  display: flex; align-items: center; justify-content: center;
}
.modal-content {
  background: #1a1a2e; border: 2px solid var(--amber); border-radius: 12px;
  padding: 24px; max-width: 400px; width: 90%;
}
.modal-content h2 {
  font-family: 'Press Start 2P', cursive; font-size: 14px; color: var(--amber);
  margin-bottom: 16px; text-align: center;
}
.setting-row {
  display: flex; justify-content: space-between; align-items: center;
  padding: 8px 0; border-bottom: 1px solid #2a2a3a;
}
.setting-row label { font-size: 12px; color: var(--text-dim); }
.setting-row input[type="range"] { width: 120px; accent-color: var(--amber); }
.setting-row input[type="checkbox"] { accent-color: var(--amber); }
.setting-row select {
  background: #111; color: var(--text-bright); border: 1px solid #444;
  padding: 4px 8px; border-radius: 4px; font-family: 'JetBrains Mono', monospace;
}
.modal-content .emu-btn { margin-top: 16px; width: 100%; }

/* ---- FOOTER ---- */
footer {
  text-align: center; padding: 20px; margin-top: 30px;
  border-top: 1px solid #2a2a3a; font-size: 11px; color: var(--text-dim);
}
footer a { color: var(--amber); text-decoration: none; }
footer a:hover { text-decoration: underline; }
footer p { margin: 4px 0; }
#cycle-counter { color: var(--green-led); font-size: 10px; }

/* ---- RESPONSIVE ---- */
@media (max-width: 600px) {
  .tv-dials { display: none; }
  .tv-frame { padding: 10px; border-radius: 12px; }
  .console-switches { gap: 8px; }
  .controller-area { flex-direction: column; }
  .debug-panel { grid-template-columns: 1fr; padding: 0 8px; }
}