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

:root {
  --wood-dark: #3E2723;
  --wood-mid: #5D4037;
  --wood-light: #795548;
  --wood-grain: #6D4C41;
  --console-black: #1a1a1a;
  --console-ridge: #2a2a2a;
  --silver: #9E9E9E;
  --crt-green: #33ff33;
  --led-red: #ff1744;
  --bg-dark: #0d0d1a;
  --tv-bezel-color: #4E342E;
}

body {
  background: var(--bg-dark);
  background-image:
    radial-gradient(ellipse at 50% 0%, #1a1a2e 0%, var(--bg-dark) 70%),
    repeating-linear-gradient(0deg, transparent, transparent 3px, rgba(80,60,40,0.03) 3px, rgba(80,60,40,0.03) 6px);
  color: #e0d6c8;
  font-family: 'VT323', monospace;
  min-height: 100vh;
  overflow-x: hidden;
}

#app-container {
  max-width: 900px;
  margin: 0 auto;
  padding: 20px 10px;
}

/* TV Frame */
#tv-frame {
  background: linear-gradient(135deg, var(--wood-dark), var(--wood-mid), var(--wood-dark));
  border-radius: 16px 16px 8px 8px;
  padding: 20px 20px 10px;
  margin-bottom: 0;
  box-shadow: 0 8px 32px rgba(0,0,0,0.6), inset 0 1px 0 rgba(255,255,255,0.05);
  position: relative;
}

#tv-bezel {
  background: #111;
  border-radius: 12px;
  padding: 16px;
  display: flex;
  gap: 12px;
  align-items: center;
}

#tv-screen-wrap {
  flex: 1;
  position: relative;
  border-radius: 20px;
  overflow: hidden;
  background: #000;
  box-shadow: 0 0 30px rgba(100,140,255,0.08), inset 0 0 60px rgba(0,0,0,0.8);
}

#screen {
  width: 100%;
  height: auto;
  display: block;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  border-radius: 16px;
  aspect-ratio: 160 / 192;
  object-fit: contain;
}

#scanlines {
  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;
  border-radius: 16px;
}

#crt-overlay {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  background: radial-gradient(ellipse at center, transparent 60%, rgba(0,0,0,0.4) 100%);
  pointer-events: none;
  border-radius: 16px;
}

#tv-controls {
  display: flex;
  flex-direction: column;
  gap: 16px;
  padding: 10px 4px;
}

.tv-knob {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 35%, #888, #333);
  border: 2px solid #222;
  cursor: pointer;
}

#tv-brand {
  text-align: center;
  font-family: 'Press Start 2P', monospace;
  font-size: 10px;
  color: #8D6E63;
  padding: 6px;
  letter-spacing: 4px;
}

/* Status Bar */
#status-bar {
  background: var(--console-black);
  display: flex;
  justify-content: space-between;
  padding: 6px 16px;
  font-size: 14px;
  color: #aaa;
  border-left: 3px solid var(--wood-mid);
  border-right: 3px solid var(--wood-mid);
}

/* Console Body */
#console-body {
  background: var(--console-black);
  border-radius: 0 0 12px 12px;
  overflow: hidden;
  box-shadow: 0 8px 24px rgba(0,0,0,0.5);
}

#console-top {
  background: linear-gradient(180deg, #222 0%, #1a1a1a 100%);
  padding: 8px 16px;
}

#rainbow-stripe {
  display: flex;
  height: 6px;
  margin-bottom: 8px;
  border-radius: 1px;
  overflow: hidden;
}

.stripe { flex: 1; }
.s1 { background: #f44336; }
.s2 { background: #ff9800; }
.s3 { background: #ffeb3b; }
.s4 { background: #4caf50; }
.s5 { background: #2196f3; }
.s6 { background: #9c27b0; }

#switches-row {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  justify-content: center;
  padding: 8px 0;
}

.switch-group {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}

.switch-group label {
  font-family: 'Press Start 2P', monospace;
  font-size: 7px;
  color: #888;
  text-transform: uppercase;
}

.switch-toggle {
  width: 40px;
  height: 20px;
  background: #333;
  border-radius: 10px;
  cursor: pointer;
  position: relative;
  border: 1px solid #555;
  transition: background 0.2s;
}

.switch-toggle[data-state="on"] {
  background: #555;
}

.switch-knob {
  width: 18px;
  height: 18px;
  background: linear-gradient(135deg, #ccc, #888);
  border-radius: 50%;
  position: absolute;
  top: 0;
  left: 0;
  transition: left 0.2s;
}

.switch-toggle[data-state="on"] .switch-knob {
  left: 20px;
}

#power-led {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #333;
  transition: all 0.3s;
}

#power-led.on {
  background: var(--led-red);
  box-shadow: 0 0 8px var(--led-red), 0 0 16px rgba(255,23,68,0.4);
}

.console-btn {
  font-family: 'Press Start 2P', monospace;
  font-size: 8px;
  padding: 6px 10px;
  background: linear-gradient(180deg, #555, #333);
  color: #ccc;
  border: 1px solid #666;
  border-radius: 3px;
  cursor: pointer;
  transition: all 0.1s;
}

.console-btn:active {
  transform: scale(0.95);
  background: linear-gradient(180deg, #444, #222);
}

#console-wood {
  background:
    repeating-linear-gradient(90deg, transparent, transparent 8px, rgba(0,0,0,0.05) 8px, rgba(0,0,0,0.05) 9px),
    linear-gradient(180deg, #6D4C41, #5D4037, #4E342E);
  padding: 16px;
  min-height: 80px;
  display: flex;
  justify-content: center;
  align-items: center;
}

#cartridge-area {
  outline: none;
}

#cartridge-slot {
  width: 200px;
  height: 60px;
  background: #111;
  border: 2px solid #333;
  border-radius: 4px;
  display: flex;
  justify-content: center;
  align-items: center;
  cursor: pointer;
  position: relative;
  overflow: hidden;
  transition: border-color 0.3s;
}

#cartridge-slot:hover {
  border-color: #888;
}

#cartridge-slot.drag-over {
  border-color: #ff9800;
  box-shadow: 0 0 12px rgba(255,152,0,0.3);
}

#slot-text {
  font-family: 'Press Start 2P', monospace;
  font-size: 8px;
  color: #555;
  text-align: center;
  line-height: 1.6;
}

#cartridge {
  position: absolute;
  width: 160px;
  height: 50px;
  background: linear-gradient(180deg, #333, #222);
  border-radius: 4px 4px 0 0;
  display: flex;
  justify-content: center;
  align-items: center;
  top: 60px;
  transition: top 0.6s cubic-bezier(0.34, 1.56, 0.64, 1);
  z-index: 2;
}

#cartridge.inserted {
  top: 5px;
}

#cartridge.hidden {
  display: none;
}

#cart-label {
  background: linear-gradient(135deg, #e8d5b7, #d4c4a8);
  color: #333;
  font-family: 'Press Start 2P', monospace;
  font-size: 6px;
  padding: 4px 8px;
  border-radius: 2px;
  max-width: 140px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  text-align: center;
}

/* Controls Row */
#controls-row {
  display: flex;
  gap: 16px;
  margin-top: 16px;
  flex-wrap: wrap;
  justify-content: center;
  align-items: flex-start;
}

#joystick-panel {
  background: #1a1a1a;
  border-radius: 12px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  border: 2px solid #333;
}

#dpad {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
}

.dpad-mid {
  display: flex;
  gap: 2px;
  align-items: center;
}

.dpad-btn {
  width: 44px;
  height: 44px;
  background: linear-gradient(180deg, #444, #2a2a2a);
  border: 1px solid #555;
  border-radius: 6px;
  color: #aaa;
  font-size: 16px;
  cursor: pointer;
  display: flex;
  justify-content: center;
  align-items: center;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}

.dpad-btn:active, .dpad-btn.pressed {
  background: linear-gradient(180deg, #555, #333);
  transform: scale(0.95);
}

.dpad-center {
  width: 44px;
  height: 44px;
  background: #222;
  border-radius: 6px;
}

#fire-btn {
  width: 60px;
  height: 60px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 35%, #e53935, #b71c1c);
  border: 3px solid #c62828;
  color: white;
  font-family: 'Press Start 2P', monospace;
  font-size: 8px;
  cursor: pointer;
  box-shadow: 0 4px 12px rgba(229,57,53,0.3);
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}

#fire-btn:active, #fire-btn.pressed {
  transform: scale(0.93);
  box-shadow: 0 2px 6px rgba(229,57,53,0.2);
}

#extra-buttons {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.action-btn {
  font-family: 'VT323', monospace;
  font-size: 16px;
  padding: 8px 14px;
  background: linear-gradient(180deg, #3E2723, #2a1a10);
  color: #e0d6c8;
  border: 1px solid #5D4037;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.2s;
  text-align: left;
}

.action-btn:hover {
  background: linear-gradient(180deg, #5D4037, #3E2723);
  border-color: #8D6E63;
}

#speed-control {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 16px;
}

#speed-select {
  font-family: 'VT323', monospace;
  font-size: 16px;
  background: #222;
  color: #e0d6c8;
  border: 1px solid #5D4037;
  border-radius: 4px;
  padding: 4px;
}

/* Keyboard Overlay */
#keyboard-overlay {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.85);
  display: flex;
  justify-content: center;
  align-items: center;
  z-index: 100;
}

#keyboard-overlay.hidden { display: none; }

.key-map {
  background: #1a1a1a;
  border: 2px solid var(--crt-green);
  border-radius: 12px;
  padding: 24px;
  font-family: 'VT323', monospace;
  font-size: 20px;
  color: var(--crt-green);
  text-shadow: 0 0 8px rgba(51,255,51,0.4);
  line-height: 1.8;
}

.key-map h3 {
  font-family: 'Press Start 2P', monospace;
  font-size: 14px;
  margin-bottom: 12px;
}

/* Debug Panel */
#debug-panel {
  position: fixed;
  right: 0;
  top: 0;
  bottom: 0;
  width: 340px;
  background: rgba(0,10,0,0.95);
  border-left: 2px solid var(--crt-green);
  padding: 12px;
  overflow-y: auto;
  z-index: 50;
  color: var(--crt-green);
  font-family: 'JetBrains Mono', monospace;
  font-size: 12px;
  text-shadow: 0 0 4px rgba(51,255,51,0.3);
}

#debug-panel.hidden { display: none; }

#debug-panel h3, #debug-panel h4 {
  font-family: 'Press Start 2P', monospace;
  font-size: 10px;
  margin: 8px 0 4px;
  color: #66ff66;
}

#debug-panel pre {
  white-space: pre-wrap;
  word-break: break-all;
  margin: 4px 0;
  line-height: 1.4;
}

#debug-controls {
  display: flex;
  gap: 6px;
  margin: 8px 0;
}

#debug-controls button {
  font-family: 'JetBrains Mono', monospace;
  font-size: 11px;
  padding: 4px 8px;
  background: #002200;
  color: var(--crt-green);
  border: 1px solid var(--crt-green);
  border-radius: 3px;
  cursor: pointer;
}

#debug-controls button:hover {
  background: #003300;
}

#debug-stats {
  margin-top: 8px;
  font-size: 11px;
  color: #44cc44;
}

/* Toast */
#toast {
  position: fixed;
  bottom: 80px;
  left: 50%;
  transform: translateX(-50%);
  background: rgba(255,152,0,0.95);
  color: #1a1a1a;
  font-family: 'VT323', monospace;
  font-size: 18px;
  padding: 10px 20px;
  border-radius: 8px;
  z-index: 200;
  transition: opacity 0.5s;
  white-space: nowrap;
}

#toast.hidden { opacity: 0; pointer-events: none; }

/* Disclaimer & Footer */
#disclaimer {
  text-align: center;
  font-family: 'VT323', monospace;
  font-size: 15px;
  color: #666;
  padding: 12px;
  font-style: italic;
}

footer {
  text-align: center;
  padding: 16px;
  font-family: 'VT323', monospace;
  font-size: 14px;
  color: #555;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

footer a {
  color: #ff9800;
  text-decoration: none;
}

footer a:hover {
  text-decoration: underline;
}

/* Responsive */
@media (max-width: 600px) {
  #app-container { padding: 8px 4px; }
  #tv-bezel { padding: 8px; }
  #tv-controls { display: none; }
  #switches-row { gap: 6px; }
  .switch-group label { font-size: 6px; }
  #cartridge-slot { width: 160px; height: 50px; }
  #cartridge { width: 130px; height: 40px; }
  #debug-panel { width: 100%; font-size: 10px; }
  #controls-row { flex-direction: column; align-items: center; }
  #status-bar { font-size: 12px; flex-wrap: wrap; gap: 4px; }
}