:root {
  --bg-deep: #0d0d1a;
  --bg-panel: #1a1a2e;
  --bg-panel-alt: #16213e;
  --amber: #ffb000;
  --amber-dim: #cc8800;
  --amber-glow: #ffcc44;
  --green: #33ff66;
  --green-dim: #22aa44;
  --red: #ff3355;
  --wood-light: #8b6914;
  --wood-dark: #5c4a1e;
  --wood-grain: #6b5520;
  --plastic-dark: #222;
  --plastic-mid: #333;
  --text-dim: #667;
  --text-bright: #dde;
  --bezel-radius: 18px;
}

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

body {
  background: var(--bg-deep);
  background-image:
    radial-gradient(ellipse at 20% 50%, rgba(255,176,0,0.03) 0%, transparent 60%),
    radial-gradient(ellipse at 80% 30%, rgba(51,255,102,0.02) 0%, transparent 50%);
  color: var(--text-bright);
  font-family: 'IBM Plex Mono', monospace;
  min-height: 100vh;
  overflow-x: hidden;
}

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

/* HEADER */
header {
  text-align: center;
  padding: 20px 0 12px;
  position: relative;
}

header::after {
  content: '';
  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;
}

.title {
  font-family: 'Press Start 2P', monospace;
  font-size: clamp(18px, 4vw, 36px);
  color: var(--amber);
  text-shadow: 0 0 20px rgba(255,176,0,0.5), 0 2px 0 #664400;
  letter-spacing: 2px;
  position: relative;
  z-index: 1;
}

.tagline {
  font-family: 'Press Start 2P', monospace;
  font-size: clamp(7px, 1.2vw, 10px);
  color: var(--amber-dim);
  margin-top: 8px;
  opacity: 0.7;
  position: relative;
  z-index: 1;
}

/* LAYOUT */
#main-layout {
  display: flex;
  gap: 16px;
  margin-top: 16px;
}

#left-panel { flex: 1; min-width: 0; max-width: 800px; }
#right-panel { width: 380px; flex-shrink: 0; }

/* PANELS */
.panel {
  background: var(--bg-panel);
  border: 1px solid rgba(255,176,0,0.15);
  border-radius: 8px;
  padding: 12px;
  margin-bottom: 12px;
}

.panel-title {
  font-family: 'Press Start 2P', monospace;
  font-size: 10px;
  color: var(--amber);
  cursor: pointer;
  user-select: none;
  padding: 4px 0;
}

/* ROM UPLOAD */
#drop-zone {
  border: 2px dashed var(--wood-light);
  border-radius: 12px;
  padding: 24px;
  text-align: center;
  background: linear-gradient(135deg, rgba(139,105,20,0.08), rgba(92,74,30,0.08));
  cursor: pointer;
  transition: all 0.3s;
  position: relative;
}

#drop-zone:hover, #drop-zone.drag-over {
  border-color: var(--amber);
  background: linear-gradient(135deg, rgba(139,105,20,0.15), rgba(92,74,30,0.15));
  box-shadow: 0 0 20px rgba(255,176,0,0.1);
}

.cartridge-slot {
  display: flex;
  flex-direction: column;
  gap: 6px;
  margin-bottom: 12px;
}

.slot-label {
  font-family: 'Press Start 2P', monospace;
  font-size: 14px;
  color: var(--amber);
}

.slot-sub {
  font-size: 11px;
  color: var(--text-dim);
}

.rom-badge {
  font-family: 'Press Start 2P', monospace;
  font-size: 10px;
  color: var(--green);
  margin-bottom: 8px;
  text-shadow: 0 0 8px rgba(51,255,102,0.5);
}

#rom-details {
  font-size: 11px;
  color: var(--text-dim);
  font-family: 'IBM Plex Mono', monospace;
}

#rom-details .hex-preview {
  margin-top: 6px;
  background: rgba(0,0,0,0.3);
  padding: 6px 8px;
  border-radius: 4px;
  word-break: break-all;
  color: var(--green-dim);
}

/* RETRO BUTTONS */
.retro-btn {
  font-family: 'Press Start 2P', monospace;
  font-size: 10px;
  padding: 10px 16px;
  border: none;
  border-radius: 6px;
  cursor: pointer;
  background: linear-gradient(180deg, #555 0%, #333 100%);
  color: var(--text-bright);
  box-shadow:
    0 3px 0 #111,
    0 4px 8px rgba(0,0,0,0.4),
    inset 0 1px 0 rgba(255,255,255,0.1);
  transition: all 0.1s;
  position: relative;
  top: 0;
}

.retro-btn:hover {
  background: linear-gradient(180deg, #666 0%, #444 100%);
}

.retro-btn:active {
  top: 2px;
  box-shadow:
    0 1px 0 #111,
    0 2px 4px rgba(0,0,0,0.4),
    inset 0 1px 0 rgba(255,255,255,0.05);
}

.retro-btn.sm { font-size: 8px; padding: 8px 10px; }

/* CRT FRAME */
#crt-frame {
  margin-bottom: 12px;
}

.crt-bezel {
  background: linear-gradient(145deg, #3a3a3a 0%, #1a1a1a 50%, #2a2a2a 100%);
  border-radius: var(--bezel-radius);
  padding: 20px;
  box-shadow:
    0 8px 32px rgba(0,0,0,0.6),
    inset 0 1px 0 rgba(255,255,255,0.08),
    inset 0 -1px 0 rgba(0,0,0,0.3);
  position: relative;
  border: 3px solid #2a2a2a;
}

.crt-bezel::before, .crt-bezel::after {
  content: '';
  position: absolute;
  top: 8px;
  bottom: 8px;
  width: 28px;
  border-radius: 4px;
}

.crt-bezel::before {
  left: -8px;
  background: linear-gradient(180deg, var(--wood-light), var(--wood-dark), var(--wood-grain), var(--wood-dark));
  border-radius: var(--bezel-radius) 0 0 var(--bezel-radius);
}

.crt-bezel::after {
  right: -8px;
  background: linear-gradient(180deg, var(--wood-dark), var(--wood-light), var(--wood-dark), var(--wood-grain));
  border-radius: 0 var(--bezel-radius) var(--bezel-radius) 0;
}

.crt-screen-wrap {
  position: relative;
  background: #000;
  border-radius: 8px;
  overflow: hidden;
  aspect-ratio: 256 / 192;
}

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

#crt-overlay {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  pointer-events: none;
  z-index: 2;
}

#crt-overlay.crt-effects {
  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);
}

#crt-overlay.crt-effects.curvature {
  border-radius: 12px;
  box-shadow: inset 0 0 80px rgba(0,0,0,0.5);
}

#crt-overlay.crt-effects.glow {
  filter: brightness(1.02);
}

.no-cart {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: 'Press Start 2P', monospace;
  font-size: clamp(12px, 3vw, 24px);
  color: var(--amber);
  z-index: 1;
  text-shadow: 0 0 15px rgba(255,176,0,0.6);
}

.no-cart.hidden { display: none; }

/* STATUS BAR */
#status-bar {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
  font-size: 10px;
  font-family: 'IBM Plex Mono', monospace;
  color: var(--green-dim);
  padding: 8px 12px;
}

/* CONTROLS */
#controls {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: center;
}

.ctrl-btn { flex: 1; min-width: 80px; text-align: center; }

/* CONTROLLER VISUAL */
#controller-visual {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
  justify-content: center;
}

.ctrl-section {
  text-align: center;
}

.ctrl-label {
  font-family: 'Press Start 2P', monospace;
  font-size: 8px;
  color: var(--amber-dim);
  margin-bottom: 8px;
}

.key-hint {
  font-size: 9px;
  color: var(--text-dim);
  margin-top: 6px;
}

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

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

.dpad-btn {
  width: 36px;
  height: 36px;
  background: #333;
  border: 1px solid #555;
  border-radius: 4px;
  color: var(--text-bright);
  font-size: 14px;
  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-center {
  width: 36px;
  height: 36px;
  background: #222;
  border-radius: 4px;
}

.fire-btns {
  display: flex;
  gap: 8px;
}

.fire-btn {
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: radial-gradient(circle, #cc2244, #881133);
  border: 2px solid #ff3355;
  color: white;
  font-family: 'Press Start 2P', monospace;
  font-size: 10px;
  cursor: pointer;
  box-shadow: 0 3px 0 #550011;
  transition: all 0.1s;
}

.fire-btn:active, .fire-btn.pressed {
  box-shadow: 0 1px 0 #550011;
  transform: translateY(2px);
  background: radial-gradient(circle, #ff3366, #cc2244);
}

.keypad-grid {
  display: grid;
  grid-template-columns: repeat(3, 32px);
  gap: 4px;
  justify-content: center;
}

.kp-btn {
  width: 32px;
  height: 28px;
  background: #444;
  border: 1px solid #666;
  border-radius: 3px;
  color: var(--text-bright);
  font-family: 'Press Start 2P', monospace;
  font-size: 8px;
  cursor: pointer;
  transition: background 0.1s;
}

.kp-btn:active, .kp-btn.pressed {
  background: var(--amber);
  color: #000;
}

/* DEBUG PANEL */
.debug-section {
  margin-bottom: 12px;
}

.debug-section h4 {
  font-family: 'Press Start 2P', monospace;
  font-size: 8px;
  color: var(--amber-dim);
  margin-bottom: 6px;
  border-bottom: 1px solid rgba(255,176,0,0.1);
  padding-bottom: 4px;
}

.mono-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(90px, 1fr));
  gap: 4px;
  font-size: 11px;
  color: var(--green);
  font-family: 'IBM Plex Mono', monospace;
}

.mono-block {
  font-size: 10px;
  font-family: 'IBM Plex Mono', monospace;
  color: var(--green-dim);
  background: rgba(0,0,0,0.3);
  padding: 8px;
  border-radius: 4px;
  max-height: 160px;
  overflow-y: auto;
  white-space: pre;
  line-height: 1.5;
}

#vram-canvas {
  width: 100%;
  max-width: 256px;
  image-rendering: pixelated;
  border: 1px solid rgba(255,176,0,0.1);
  border-radius: 4px;
}

/* SETTINGS */
.setting-row {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 8px;
  font-size: 11px;
  color: var(--text-dim);
}

.setting-row input[type="checkbox"] {
  accent-color: var(--amber);
}

.setting-row input[type="range"] {
  flex: 1;
  accent-color: var(--amber);
}

.setting-row select {
  background: var(--bg-deep);
  color: var(--text-bright);
  border: 1px solid var(--amber-dim);
  border-radius: 4px;
  padding: 4px;
  font-size: 10px;
  font-family: 'IBM Plex Mono', monospace;
}

.btns-row {
  flex-wrap: wrap;
  gap: 6px;
  margin-top: 8px;
}

/* TOAST */
#toast-container {
  position: fixed;
  top: 20px;
  right: 20px;
  z-index: 1000;
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.toast {
  font-family: 'Press Start 2P', monospace;
  font-size: 9px;
  padding: 10px 16px;
  border-radius: 6px;
  background: var(--bg-panel);
  border: 1px solid var(--amber);
  color: var(--amber);
  box-shadow: 0 4px 16px rgba(0,0,0,0.5);
  animation: toastIn 0.3s ease, toastOut 0.3s ease 2.7s forwards;
}

.toast.error {
  border-color: var(--red);
  color: var(--red);
}

.toast.success {
  border-color: var(--green);
  color: var(--green);
}

@keyframes toastIn {
  from { opacity: 0; transform: translateX(40px); }
  to { opacity: 1; transform: translateX(0); }
}

@keyframes toastOut {
  from { opacity: 1; }
  to { opacity: 0; transform: translateY(-10px); }
}

/* FOOTER */
footer {
  text-align: center;
  padding: 20px;
  font-size: 10px;
  color: var(--text-dim);
  border-top: 1px solid rgba(255,176,0,0.08);
  margin-top: 20px;
}

footer a {
  color: var(--amber);
  text-decoration: none;
}

footer a:hover {
  text-decoration: underline;
  text-shadow: 0 0 8px rgba(255,176,0,0.4);
}

.hidden { display: none !important; }

/* Bounce animation for no-cart screen */
@keyframes bounce {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-10px); }
}

.no-cart span {
  animation: bounce 1.5s ease-in-out infinite;
}

/* Scrollbar styling */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: var(--bg-deep); }
::-webkit-scrollbar-thumb { background: var(--wood-dark); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--wood-light); }

/* RESPONSIVE */
@media (max-width: 900px) {
  #main-layout {
    flex-direction: column;
  }
  #right-panel {
    width: 100%;
  }
  #left-panel {
    max-width: 100%;
  }
}

@media (max-width: 500px) {
  #app { padding: 8px; }
  .crt-bezel { padding: 12px; }
  .crt-bezel::before, .crt-bezel::after { display: none; }
  #controls { gap: 4px; }
  .ctrl-btn { font-size: 8px; padding: 8px 6px; min-width: 60px; }
  #controller-visual { gap: 8px; }
  .dpad-btn { width: 30px; height: 30px; }
  .dpad-center { width: 30px; height: 30px; }
}