:root {
  --shell-color: #5ba3d9;
  --shell-dark: #3a7ab8;
  --shell-light: #7ec4f0;
  --bezel-color: #1a1a2e;
  --lcd-bg: #8b9d6b;
  --lcd-fg: #1a2010;
  --lcd-ghost: rgba(26, 32, 16, 0.15);
  --btn-color: #333;
  --btn-highlight: #555;
  --bg-primary: #0d0d1a;
  --bg-secondary: #16162a;
  --text-primary: #e0e0e0;
  --text-accent: #ffcc00;
  --danger: #e74c3c;
}

/* Themes */
.theme-wooper-blue { --shell-color: #5ba3d9; --shell-dark: #3a7ab8; --shell-light: #7ec4f0; }
.theme-chikorita-green { --shell-color: #6dbf6d; --shell-dark: #4a9a4a; --shell-light: #90d890; }
.theme-smoochum-purple { --shell-color: #9b6dcc; --shell-dark: #7a4faa; --shell-light: #b890e0; }
.theme-pikachu-yellow { --shell-color: #f0d040; --shell-dark: #d4b020; --shell-light: #ffe060; }

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

body {
  background: var(--bg-primary);
  background-image:
    radial-gradient(circle at 20% 30%, rgba(91, 163, 217, 0.05) 0%, transparent 50%),
    radial-gradient(circle at 80% 70%, rgba(240, 208, 64, 0.05) 0%, transparent 50%),
    repeating-linear-gradient(0deg, transparent, transparent 40px, rgba(255,255,255,0.01) 40px, rgba(255,255,255,0.01) 41px),
    repeating-linear-gradient(90deg, transparent, transparent 40px, rgba(255,255,255,0.01) 40px, rgba(255,255,255,0.01) 41px);
  color: var(--text-primary);
  font-family: 'JetBrains Mono', monospace;
  min-height: 100vh;
  display: flex;
  justify-content: center;
  align-items: flex-start;
  overflow-x: hidden;
}

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

/* Toolbar */
#toolbar {
  width: 100%;
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  padding: 10px 12px;
  background: var(--bg-secondary);
  border-radius: 10px;
  margin-bottom: 10px;
  border: 1px solid rgba(255,255,255,0.06);
}

.toolbar-title {
  font-family: 'Press Start 2P', cursive;
  font-size: 14px;
  color: var(--text-accent);
  text-shadow: 2px 2px 0 rgba(0,0,0,0.5);
  margin-right: auto;
}

.toolbar-buttons {
  display: flex;
  flex-wrap: wrap;
  gap: 4px;
  align-items: center;
}

.toolbar-buttons button, .toolbar-buttons select {
  font-family: 'JetBrains Mono', monospace;
  font-size: 11px;
  padding: 5px 8px;
  background: #2a2a40;
  color: #ccc;
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: 5px;
  cursor: pointer;
  transition: all 0.15s;
  white-space: nowrap;
}

.toolbar-buttons button:hover, .toolbar-buttons select:hover {
  background: #3a3a55;
  color: #fff;
}

.toolbar-buttons select {
  appearance: auto;
}

#fps-counter {
  font-size: 10px;
  color: #666;
  font-family: 'JetBrains Mono', monospace;
  min-width: 55px;
  text-align: right;
}

/* Theme Selector */
#theme-selector {
  display: flex;
  gap: 8px;
  margin-bottom: 10px;
}

.theme-btn {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  border: 2px solid rgba(255,255,255,0.2);
  cursor: pointer;
  transition: all 0.2s;
}

.theme-btn:hover { transform: scale(1.15); }
.theme-btn[data-theme="wooper-blue"] { background: #5ba3d9; }
.theme-btn[data-theme="chikorita-green"] { background: #6dbf6d; }
.theme-btn[data-theme="smoochum-purple"] { background: #9b6dcc; }
.theme-btn[data-theme="pikachu-yellow"] { background: #f0d040; }
.theme-btn.active { border-color: #fff; box-shadow: 0 0 10px rgba(255,255,255,0.3); }

/* Console Body */
#console-body {
  position: relative;
  width: 100%;
  max-width: 460px;
}

#console-shell {
  background: linear-gradient(160deg, var(--shell-light) 0%, var(--shell-color) 30%, var(--shell-dark) 100%);
  border-radius: 30px 30px 50px 50px;
  padding: 20px 24px 28px;
  box-shadow:
    0 12px 40px rgba(0,0,0,0.5),
    0 4px 12px rgba(0,0,0,0.3),
    inset 0 1px 0 rgba(255,255,255,0.3),
    inset 0 -2px 0 rgba(0,0,0,0.15);
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
  position: relative;
}

/* Console Branding */
#console-brand {
  font-family: 'Press Start 2P', cursive;
  font-size: 11px;
  color: rgba(0,0,0,0.5);
  text-shadow: 0 1px 0 rgba(255,255,255,0.3);
  letter-spacing: 2px;
  text-transform: uppercase;
}

/* Screen Bezel */
#screen-bezel {
  background: var(--bezel-color);
  border-radius: 8px;
  padding: 10px;
  box-shadow:
    inset 0 3px 8px rgba(0,0,0,0.6),
    inset 0 -1px 0 rgba(255,255,255,0.05),
    0 1px 0 rgba(255,255,255,0.1);
  position: relative;
  line-height: 0;
}

#canvas {
  width: 384px;
  height: 256px;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  background: var(--lcd-bg);
  border-radius: 3px;
  display: block;
}

#lcd-overlay {
  position: absolute;
  top: 10px; left: 10px; right: 10px; bottom: 10px;
  border-radius: 3px;
  pointer-events: none;
  background: repeating-linear-gradient(
    0deg,
    transparent 0px,
    transparent 3px,
    rgba(0,0,0,0.06) 3px,
    rgba(0,0,0,0.06) 4px
  ),
  repeating-linear-gradient(
    90deg,
    transparent 0px,
    transparent 3px,
    rgba(0,0,0,0.06) 3px,
    rgba(0,0,0,0.06) 4px
  );
  mix-blend-mode: multiply;
}

/* ROM Label */
#rom-label {
  font-family: 'JetBrains Mono', monospace;
  font-size: 9px;
  color: rgba(0,0,0,0.5);
  background: rgba(255,255,255,0.15);
  padding: 3px 12px;
  border-radius: 3px;
  max-width: 250px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Contrast */
#contrast-area, #volume-area {
  display: flex;
  align-items: center;
  gap: 6px;
  width: 100%;
}

#contrast-area label, #volume-area label {
  font-family: 'Press Start 2P', cursive;
  font-size: 7px;
  color: rgba(0,0,0,0.4);
  min-width: 60px;
}

input[type="range"] {
  flex: 1;
  height: 4px;
  -webkit-appearance: none;
  appearance: none;
  background: rgba(0,0,0,0.2);
  border-radius: 2px;
  outline: none;
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 14px;
  height: 14px;
  background: rgba(0,0,0,0.4);
  border-radius: 50%;
  cursor: pointer;
  border: 2px solid rgba(255,255,255,0.2);
}

/* Controls Layout */
#controls-layout {
  display: flex;
  justify-content: space-between;
  align-items: center;
  width: 100%;
  padding: 10px 0;
}

/* D-Pad */
#dpad {
  display: grid;
  grid-template-columns: 40px 40px 40px;
  grid-template-rows: 40px 40px 40px;
  gap: 0;
  position: relative;
}

.dpad-btn {
  width: 40px;
  height: 40px;
  background: #2a2a2a;
  color: rgba(255,255,255,0.5);
  border: none;
  cursor: pointer;
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.08s;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}

.dpad-btn:active, .dpad-btn.pressed {
  transform: scale(0.92);
  background: #1a1a1a;
}

.dpad-up { grid-column: 2; grid-row: 1; border-radius: 8px 8px 0 0; }
.dpad-left { grid-column: 1; grid-row: 2; border-radius: 8px 0 0 8px; }
.dpad-right { grid-column: 3; grid-row: 2; border-radius: 0 8px 8px 0; }
.dpad-down { grid-column: 2; grid-row: 3; border-radius: 0 0 8px 8px; }

.dpad-center {
  grid-column: 2;
  grid-row: 2;
  background: #222;
  width: 40px;
  height: 40px;
}

/* Action Buttons */
#action-buttons {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  position: relative;
}

.action-btn {
  width: 46px;
  height: 46px;
  border-radius: 50%;
  border: none;
  cursor: pointer;
  font-family: 'Press Start 2P', cursive;
  font-size: 10px;
  color: rgba(255,255,255,0.7);
  transition: all 0.08s;
  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);
}

.action-btn:active, .action-btn.pressed {
  transform: scale(0.9) translateY(2px);
  box-shadow: 0 1px 3px rgba(0,0,0,0.3);
}

.btn-a { background: #c0392b; position: relative; left: 20px; }
.btn-b { background: #2980b9; position: relative; right: 10px; }
.btn-c { background: #27ae60; font-size: 8px; width: 36px; height: 36px; position: relative; left: 30px; top: -5px; }

/* Bottom Buttons */
#bottom-buttons {
  display: flex;
  justify-content: center;
  gap: 20px;
  width: 100%;
}

.small-btn {
  font-family: 'Press Start 2P', cursive;
  font-size: 6px;
  padding: 6px 14px;
  background: rgba(0,0,0,0.3);
  color: rgba(255,255,255,0.4);
  border: none;
  border-radius: 12px;
  cursor: pointer;
  transition: all 0.1s;
  user-select: none;
  touch-action: manipulation;
}

.small-btn:active, .small-btn.pressed {
  transform: scale(0.95);
  background: rgba(0,0,0,0.5);
}

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

.keymap-content {
  background: var(--bg-secondary);
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: 12px;
  padding: 24px;
  max-width: 350px;
  width: 90%;
}

.keymap-content h3 {
  font-family: 'Press Start 2P', cursive;
  font-size: 12px;
  color: var(--text-accent);
  margin-bottom: 16px;
}

.keymap-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
  font-size: 12px;
  margin-bottom: 16px;
}

.keymap-grid span:nth-child(odd) {
  color: var(--text-accent);
  font-weight: bold;
}

#close-keymap {
  width: 100%;
  padding: 8px;
  background: #2a2a40;
  color: #ccc;
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: 6px;
  cursor: pointer;
  font-family: 'JetBrains Mono', monospace;
}

/* Debug Panel */
#debug-panel {
  width: 100%;
  max-width: 460px;
  background: var(--bg-secondary);
  border: 1px solid rgba(255,255,255,0.06);
  border-radius: 10px;
  padding: 14px;
  margin-top: 10px;
  font-size: 11px;
}

#debug-panel h3 {
  font-family: 'Press Start 2P', cursive;
  font-size: 10px;
  color: var(--text-accent);
  margin-bottom: 10px;
}

#debug-registers {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(100px, 1fr));
  gap: 4px;
  margin-bottom: 10px;
}

.reg-row {
  display: flex;
  justify-content: space-between;
  background: rgba(0,0,0,0.3);
  padding: 3px 6px;
  border-radius: 3px;
}

.reg-row span:first-child { color: #888; }
.reg-row span:last-child { color: #0f0; font-family: 'JetBrains Mono', monospace; }

#debug-info {
  display: flex;
  flex-wrap: wrap;
  gap: 10px;
  margin-bottom: 10px;
}

#debug-info div {
  background: rgba(0,0,0,0.3);
  padding: 3px 8px;
  border-radius: 3px;
}

#debug-info span:first-child { color: #888; margin-right: 6px; }
#debug-info span:last-child { color: #0f0; }

#debug-memory {
  margin-bottom: 10px;
}

#debug-memory label {
  font-size: 10px;
  color: #888;
  display: flex;
  align-items: center;
  gap: 6px;
  margin-bottom: 4px;
}

#debug-memory input {
  background: rgba(0,0,0,0.4);
  border: 1px solid rgba(255,255,255,0.1);
  color: #0f0;
  font-family: 'JetBrains Mono', monospace;
  font-size: 10px;
  padding: 2px 6px;
  border-radius: 3px;
}

#mem-dump {
  background: rgba(0,0,0,0.4);
  padding: 6px;
  border-radius: 4px;
  font-size: 10px;
  color: #0f0;
  overflow-x: auto;
  line-height: 1.6;
}

#debug-breakpoint {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 10px;
}

#debug-breakpoint label { color: #888; }

#debug-breakpoint input {
  background: rgba(0,0,0,0.4);
  border: 1px solid rgba(255,255,255,0.1);
  color: #ff0;
  font-family: 'JetBrains Mono', monospace;
  font-size: 10px;
  padding: 2px 6px;
  border-radius: 3px;
}

#debug-breakpoint button {
  font-size: 9px;
  padding: 3px 8px;
  background: #2a2a40;
  color: #ccc;
  border: 1px solid rgba(255,255,255,0.1);
  border-radius: 4px;
  cursor: pointer;
}

#bp-status { color: #f80; }

/* Drop Overlay */
#drop-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: 2000;
}

.drop-content {
  text-align: center;
}

.cartridge-anim {
  font-size: 80px;
  animation: cartridge-bounce 0.6s ease-in-out infinite alternate;
}

@keyframes cartridge-bounce {
  from { transform: translateY(-20px); }
  to { transform: translateY(10px); }
}

.drop-content p {
  font-family: 'Press Start 2P', cursive;
  font-size: 14px;
  color: var(--text-accent);
  margin-top: 16px;
}

/* Hidden */
.hidden { display: none !important; }

/* Footer */
#app-footer {
  margin-top: 20px;
  margin-bottom: 20px;
  text-align: center;
  font-size: 10px;
  color: #444;
}

#app-footer a {
  color: #666;
  text-decoration: none;
  transition: color 0.2s;
}

#app-footer a:hover { color: var(--text-accent); }

.footer-disclaimer {
  display: block;
  margin-top: 4px;
  font-size: 8px;
  color: #333;
}

/* Responsive */
@media (max-width: 500px) {
  #console-shell { padding: 14px 14px 20px; border-radius: 20px 20px 30px 30px; }
  #canvas { width: 288px; height: 192px; }
  .toolbar-title { font-size: 10px; }
  .toolbar-buttons button { font-size: 9px; padding: 4px 6px; }
  .dpad-btn { width: 36px; height: 36px; font-size: 12px; }
  .dpad-center { width: 36px; height: 36px; }
  .action-btn { width: 40px; height: 40px; font-size: 9px; }
  .btn-c { width: 32px; height: 32px; }
  #screen-bezel { padding: 6px; }
}

@media (max-width: 360px) {
  #canvas { width: 240px; height: 160px; }
}

/* Loading animation */
@keyframes blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.2; }
}

.blink { animation: blink 1s ease-in-out infinite; }

/* Cartridge insert animation */
@keyframes insert-cartridge {
  0% { transform: translateY(-30px); opacity: 0; }
  60% { transform: translateY(3px); opacity: 1; }
  100% { transform: translateY(0); opacity: 1; }
}

.cart-insert { animation: insert-cartridge 0.4s ease-out; }