:root {
  --gb-shell: #bcc0b8;
  --gb-shell-dark: #9a9e96;
  --gb-bezel: #4a4d48;
  --gb-screen-bg: #8bac0f;
  --gb-screen-border: #2d2d2d;
  --gb-dpad: #2d2d33;
  --gb-ab: #8b2252;
  --gb-ss: #555;
  --bg-dark: #0c0e14;
  --bg-mid: #141827;
  --accent: #8bac0f;
  --text: #e0e0e0;
  --text-dim: #888;
}

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

body {
  background: var(--bg-dark);
  background-image: 
    radial-gradient(ellipse at 50% 0%, #1a2035 0%, transparent 70%),
    linear-gradient(180deg, #0c0e14 0%, #111520 100%);
  min-height: 100vh;
  font-family: 'IBM Plex Mono', monospace;
  color: var(--text);
  display: flex;
  justify-content: center;
  overflow-x: hidden;
}

#app {
  width: 100%;
  max-width: 520px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  align-items: center;
}

/* HEADER */
#header {
  text-align: center;
  margin-bottom: 12px;
}
#header h1 {
  font-family: 'Press Start 2P', cursive;
  font-size: 20px;
  color: var(--accent);
  text-shadow: 0 0 20px rgba(139,172,15,0.4);
  letter-spacing: 2px;
}
.subtitle {
  font-size: 11px;
  color: var(--text-dim);
  margin-top: 4px;
}
#status-bar {
  display: flex;
  justify-content: center;
  gap: 20px;
  margin-top: 8px;
  font-size: 11px;
}
.status-idle { color: #888; }
.status-running { color: #4caf50; }
.status-paused { color: #ff9800; }
.status-error { color: #f44336; }
#fps-counter {
  color: var(--accent);
  font-family: 'Press Start 2P', cursive;
  font-size: 9px;
}

/* ERROR DISPLAY */
#error-display {
  margin-top: 8px;
  padding: 8px 12px;
  background: #2a1010;
  border: 1px solid #f44336;
  border-radius: 6px;
  color: #ff6b6b;
  font-size: 10px;
  font-family: 'IBM Plex Mono', monospace;
  word-break: break-all;
  max-width: 100%;
  text-align: left;
}
#error-display.hidden { display: none; }

/* TOOLBAR */
#toolbar {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: center;
  align-items: center;
  margin-bottom: 12px;
  padding: 8px;
  background: #1a1d28;
  border-radius: 8px;
  border: 1px solid #2a2d38;
  width: 100%;
}
#toolbar button, #toolbar select {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 11px;
  padding: 5px 10px;
  border: 1px solid #3a3d48;
  background: #22252f;
  color: var(--text);
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.15s;
}
#toolbar button:hover:not(:disabled), #toolbar select:hover {
  background: #2a2d38;
  border-color: var(--accent);
}
#toolbar button:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}
.toolbar-separator {
  width: 1px;
  height: 24px;
  background: #3a3d48;
}
.toolbar-toggle {
  font-size: 11px;
  cursor: pointer;
  display: flex;
  align-items: center;
  gap: 4px;
}
.toolbar-toggle input { cursor: pointer; }
.volume-control {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 12px;
}
.volume-control input[type="range"] {
  width: 60px;
  height: 4px;
  accent-color: var(--accent);
}

/* GAME BOY SHELL */
#gameboy-shell {
  background: linear-gradient(160deg, #d0d4cc 0%, #b8bcb0 40%, #a8aca0 100%);
  border-radius: 16px 16px 16px 60px;
  padding: 16px;
  width: 100%;
  max-width: 420px;
  box-shadow: 
    0 8px 32px rgba(0,0,0,0.6),
    inset 0 1px 0 rgba(255,255,255,0.3),
    inset 0 -2px 4px rgba(0,0,0,0.1);
  position: relative;
}
#shell-top {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  margin-bottom: 8px;
  padding-left: 40px;
}
#power-led {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #666;
  box-shadow: inset 0 1px 2px rgba(0,0,0,0.4);
  position: absolute;
  left: 24px;
  top: 20px;
}
#power-led.on {
  background: #ff3333;
  box-shadow: 0 0 8px #ff3333, inset 0 1px 2px rgba(255,255,255,0.3);
}
#gb-label {
  font-family: 'Press Start 2P', cursive;
  font-size: 14px;
  color: #2c2c6c;
  font-style: italic;
  letter-spacing: 4px;
}
#gb-label-sub {
  font-size: 8px;
  color: #2c2c6c;
  vertical-align: super;
}

/* SCREEN */
#screen-bezel {
  background: linear-gradient(135deg, #4a4a5c 0%, #3a3a48 100%);
  border-radius: 8px;
  padding: 20px 16px 16px 16px;
  margin: 0 8px;
  box-shadow: 
    inset 0 4px 12px rgba(0,0,0,0.6),
    0 1px 0 rgba(255,255,255,0.1);
}
#screen-inner {
  position: relative;
  background: #0f380f;
  border-radius: 4px;
  overflow: hidden;
  aspect-ratio: 160/144;
}
#canvas {
  width: 100%;
  height: 100%;
  display: block;
  image-rendering: pixelated;
  image-rendering: -moz-crisp-edges;
  image-rendering: crisp-edges;
}
#scanline-overlay {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  pointer-events: none;
  opacity: 0;
  background: repeating-linear-gradient(
    0deg,
    transparent 0px,
    transparent 2px,
    rgba(0,0,0,0.15) 2px,
    rgba(0,0,0,0.15) 4px
  );
  transition: opacity 0.3s;
}
#scanline-overlay.active { opacity: 1; }

/* DROP ZONE */
#drop-zone {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  display: none;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: rgba(15,56,15,0.95);
  cursor: pointer;
  z-index: 10;
}
#drop-zone.active { display: flex; }
#drop-zone.dragover {
  background: rgba(48,98,48,0.95);
}
.cartridge-icon {
  font-size: 36px;
  margin-bottom: 8px;
  animation: bob 2s ease-in-out infinite;
}
@keyframes bob {
  0%,100% { transform: translateY(0); }
  50% { transform: translateY(-6px); }
}
.drop-title {
  font-family: 'Press Start 2P', cursive;
  font-size: 11px;
  color: #9bbc0f;
  margin-bottom: 8px;
}
.drop-sub {
  font-size: 9px;
  color: #8bac0f;
  margin-bottom: 2px;
}

/* CART INFO */
#cart-info {
  display: flex;
  justify-content: space-between;
  padding: 4px 12px;
  font-size: 9px;
  color: #555;
}
#cart-info.hidden { display: none; }

/* CONTROLS */
#controls-area {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: auto auto;
  gap: 8px;
  padding: 16px 8px 8px;
  align-items: center;
}
#dpad-container {
  display: flex;
  justify-content: center;
}
#dpad {
  display: grid;
  grid-template-columns: 40px 40px 40px;
  grid-template-rows: 40px 40px 40px;
  gap: 0;
}
.dpad-btn {
  background: var(--gb-dpad);
  border: none;
  color: #888;
  font-size: 14px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
  transition: background 0.1s;
}
.dpad-btn:active, .dpad-btn.pressed {
  background: #1a1a20;
  color: #aaa;
}
.dpad-up { border-radius: 6px 6px 0 0; }
.dpad-down { border-radius: 0 0 6px 6px; }
.dpad-left { border-radius: 6px 0 0 6px; }
.dpad-right { border-radius: 0 6px 6px 0; }
.dpad-center {
  background: var(--gb-dpad);
  cursor: default;
  box-shadow: inset 0 0 8px rgba(0,0,0,0.3);
}

#ab-container {
  display: flex;
  justify-content: center;
  gap: 16px;
  transform: rotate(-25deg);
}
.ab-btn {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: linear-gradient(135deg, #9b2856 0%, #7a1e44 100%);
  border: 2px solid #5a1530;
  color: #ddd;
  font-family: 'Press Start 2P', cursive;
  font-size: 10px;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
  box-shadow: 0 3px 6px rgba(0,0,0,0.3), inset 0 1px 0 rgba(255,255,255,0.15);
  transition: all 0.1s;
}
.ab-btn:active, .ab-btn.pressed {
  box-shadow: 0 1px 2px rgba(0,0,0,0.3), inset 0 2px 4px rgba(0,0,0,0.3);
  transform: translateY(2px);
}

#start-select-container {
  grid-column: 1 / -1;
  display: flex;
  justify-content: center;
  gap: 24px;
  transform: rotate(-25deg);
  margin-top: 4px;
}
.ss-btn {
  padding: 6px 16px;
  border-radius: 20px;
  background: linear-gradient(135deg, #666 0%, #555 100%);
  border: 1px solid #444;
  color: #ccc;
  font-family: 'Press Start 2P', cursive;
  font-size: 7px;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
  box-shadow: 0 2px 4px rgba(0,0,0,0.3);
  transition: all 0.1s;
}
.ss-btn:active, .ss-btn.pressed {
  box-shadow: 0 1px 1px rgba(0,0,0,0.3);
  transform: translateY(1px);
}

/* BATTERY LABEL */
#battery-label {
  font-family: 'Press Start 2P', cursive;
  font-size: 7px;
  color: #2c2c6c;
  letter-spacing: 2px;
  position: absolute;
  right: 24px;
  top: 22px;
  opacity: 0.5;
}

/* AGING CART EFFECT */
#gameboy-shell.aged {
  background: linear-gradient(160deg, #c8c4a8 0%, #b0a890 40%, #a09878 100%);
  box-shadow: 
    0 8px 32px rgba(0,0,0,0.6),
    inset 0 1px 0 rgba(255,255,255,0.15),
    inset 0 -2px 4px rgba(0,0,0,0.15);
}
#gameboy-shell.aged #screen-bezel {
  background: linear-gradient(135deg, #3a3a32 0%, #2e2e28 100%);
}
#gameboy-shell.aged #screen-inner {
  background: #0d3008;
}
#gameboy-shell.aged .dpad-btn {
  background: #252520;
}
#gameboy-shell.aged .ab-btn {
  background: linear-gradient(135deg, #7a2040 0%, #601830 100%);
  border-color: #481020;
}
#gameboy-shell.aged .ss-btn {
  background: linear-gradient(135deg, #504830 0%, #403828 100%);
}
#gameboy-shell.aged .grille-line {
  background: #887860;
}
#gameboy-shell.aged #gb-label {
  color: #28284a;
  opacity: 0.7;
}
#gameboy-shell.aged #battery-label {
  opacity: 0.3;
}
#gameboy-shell.aged #power-led.on {
  background: #cc3030;
  box-shadow: 0 0 4px #cc3030, inset 0 1px 2px rgba(255,255,255,0.2);
  opacity: 0.7;
}

.aging-label {
  color: #c8a060 !important;
}

/* CPU INFO PANEL */
#cpu-info-panel {
  margin-top: 12px;
  width: 100%;
  max-width: 420px;
  background: #12141c;
  border: 1px solid #2a2d38;
  border-radius: 6px;
  padding: 12px;
}
.cpu-info-title {
  font-family: 'Press Start 2P', cursive;
  font-size: 9px;
  color: var(--accent);
  margin-bottom: 10px;
  text-align: center;
  letter-spacing: 1px;
}
.cpu-info-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}
.cpu-info-item {
  display: flex;
  flex-direction: column;
  padding: 4px 6px;
  background: #0a0c12;
  border-radius: 4px;
  border: 1px solid #1a1d28;
}
.cpu-label {
  font-size: 8px;
  color: #666;
  text-transform: uppercase;
  letter-spacing: 1px;
  margin-bottom: 2px;
}
.cpu-value {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 10px;
  color: var(--accent);
}

/* SPEAKER */
#speaker-grille {
  display: flex;
  justify-content: flex-end;
  padding: 12px 20px 4px;
  gap: 4px;
  transform: rotate(-25deg);
  transform-origin: right center;
}
.grille-line {
  width: 3px;
  height: 32px;
  background: #999;
  border-radius: 2px;
  box-shadow: inset 0 0 4px rgba(0,0,0,0.4);
}

/* KEYBOARD HELP */
#keyboard-help {
  margin-top: 12px;
  width: 100%;
  max-width: 420px;
}
#toggle-help {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 11px;
  background: none;
  border: 1px solid #3a3d48;
  color: var(--text-dim);
  padding: 6px 12px;
  border-radius: 4px;
  cursor: pointer;
  width: 100%;
}
#toggle-help:hover { border-color: var(--accent); color: var(--text); }
#help-panel {
  background: #1a1d28;
  border: 1px solid #2a2d38;
  border-top: none;
  border-radius: 0 0 4px 4px;
  padding: 10px 14px;
  font-size: 11px;
}
#help-panel.hidden { display: none; }
.key-row { margin: 4px 0; color: var(--text-dim); }
kbd {
  background: #2a2d38;
  border: 1px solid #3a3d48;
  padding: 1px 6px;
  border-radius: 3px;
  font-family: 'IBM Plex Mono', monospace;
  font-size: 10px;
  color: var(--accent);
}

/* DEBUG */
#debug-panel {
  margin-top: 8px;
  width: 100%;
  max-width: 420px;
  font-size: 11px;
  color: var(--text-dim);
}
#debug-panel summary {
  cursor: pointer;
  padding: 6px 0;
}
#debug-content {
  background: #0c0e14;
  border: 1px solid #2a2d38;
  border-radius: 4px;
  padding: 10px;
}
#debug-text {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 10px;
  color: var(--accent);
  white-space: pre;
  line-height: 1.5;
}

/* FOOTER */
footer {
  margin-top: 20px;
  text-align: center;
  font-size: 10px;
  color: var(--text-dim);
  padding-bottom: 24px;
}
footer a {
  color: var(--accent);
  text-decoration: none;
}
footer a:hover { text-decoration: underline; }
.disclaimer {
  margin-top: 6px;
  font-size: 9px;
  color: #555;
}
footer p:nth-child(2) {
  font-size: 9px;
  color: #556;
  margin-top: 4px;
  letter-spacing: 0.5px;
}

/* RESPONSIVE */
@media (max-width: 480px) {
  #app { padding: 8px; }
  #header h1 { font-size: 14px; }
  #toolbar { font-size: 10px; padding: 6px; }
  #toolbar button, #toolbar select { padding: 4px 6px; font-size: 10px; }
  #gameboy-shell { padding: 10px; border-radius: 12px 12px 12px 40px; }
  .dpad-btn { width: 36px; height: 36px; }
  #dpad { grid-template-columns: 36px 36px 36px; grid-template-rows: 36px 36px 36px; }
  .ab-btn { width: 44px; height: 44px; }
}