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

:root {
  --shell-main: #2d1b4e;
  --shell-light: #4a2d7a;
  --shell-dark: #1a0e30;
  --bezel-bg: #1a1a2e;
  --screen-off: #5a6b4a;
  --btn-dark: #2a2a3a;
  --btn-ab: #7b2d4e;
  --btn-ab-press: #5a1e38;
  --led-off: #661111;
  --led-on: #22cc44;
  --accent: #e8a0ff;
}

body {
  background: #0d0a14;
  background-image: 
    radial-gradient(ellipse at 50% 30%, rgba(60,30,90,0.3) 0%, transparent 60%),
    radial-gradient(ellipse at 80% 80%, rgba(20,10,40,0.5) 0%, transparent 50%);
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  font-family: 'IBM Plex Mono', monospace;
  color: #ccc;
  overflow-x: hidden;
  -webkit-user-select: none;
  user-select: none;
}

#toolbar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px 16px;
  flex-wrap: wrap;
  justify-content: center;
  max-width: 700px;
  width: 100%;
}

#toolbar button, #toolbar select {
  background: #1e1530;
  border: 1px solid #3a2a5a;
  color: #d4b8f0;
  padding: 6px 12px;
  border-radius: 6px;
  cursor: pointer;
  font-family: 'IBM Plex Mono', monospace;
  font-size: 12px;
  transition: background 0.15s, border-color 0.15s;
}

#toolbar button:hover:not(:disabled), #toolbar select:hover {
  background: #2e2050;
  border-color: #6a4a9a;
}

#toolbar button:disabled {
  opacity: 0.4;
  cursor: default;
}

#toolbar select {
  appearance: none;
  padding-right: 20px;
  background-image: url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M0 0l5 6 5-6z' fill='%23d4b8f0'/%3E%3C/svg%3E");
  background-repeat: no-repeat;
  background-position: right 6px center;
}

#game-title {
  font-family: 'Press Start 2P', monospace;
  font-size: 9px;
  color: var(--accent);
  margin-left: 8px;
  max-width: 200px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

#app-container {
  display: flex;
  gap: 16px;
  align-items: flex-start;
  justify-content: center;
  padding: 10px;
  flex: 1;
}

#gameboy-shell {
  background: linear-gradient(135deg, #3d2466 0%, #2a1650 30%, #1f1040 70%, #2d1b4e 100%);
  border-radius: 20px 20px 60px 20px;
  padding: 18px 20px 30px;
  box-shadow: 
    0 8px 32px rgba(0,0,0,0.6),
    inset 0 1px 0 rgba(255,255,255,0.08),
    0 0 0 2px rgba(80,40,120,0.3);
  width: 340px;
  min-width: 300px;
  position: relative;
}

#top-section {
  display: flex;
  align-items: center;
  padding: 4px 10px 8px;
}

#power-led {
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: var(--led-off);
  box-shadow: 0 0 4px rgba(100,0,0,0.5);
  transition: all 0.3s;
}

#power-led.on {
  background: var(--led-on);
  box-shadow: 0 0 8px rgba(34,204,68,0.6), 0 0 2px rgba(34,204,68,0.8);
}

#screen-bezel {
  background: var(--bezel-bg);
  border-radius: 10px;
  padding: 12px 14px 14px;
  margin: 0 4px 16px;
  box-shadow: inset 0 2px 8px rgba(0,0,0,0.5);
}

#screen-label {
  text-align: center;
  margin-bottom: 2px;
  line-height: 1;
}

#color-text {
  font-family: 'Press Start 2P', monospace;
  font-size: 8px;
  letter-spacing: 3px;
  background: linear-gradient(90deg, #ff4444, #ff8844, #ffdd44, #44dd44, #4488ff, #8844ff);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
  background-clip: text;
}

#gb-label {
  font-family: 'Press Start 2P', monospace;
  font-size: 11px;
  text-align: center;
  color: #8888aa;
  letter-spacing: 2px;
  margin-bottom: 8px;
}

#screen-wrapper {
  position: relative;
  background: var(--screen-off);
  border-radius: 4px;
  overflow: hidden;
  box-shadow: inset 0 2px 10px rgba(0,0,0,0.6);
  aspect-ratio: 160/144;
}

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

#scanline-overlay {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  background: repeating-linear-gradient(
    0deg,
    transparent 0px,
    transparent 2px,
    rgba(0,0,0,0.06) 2px,
    rgba(0,0,0,0.06) 4px
  );
  pointer-events: none;
}

#drop-overlay {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  background: rgba(90,107,74,0.85);
  transition: opacity 0.3s;
}

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

#drop-msg {
  font-family: 'Press Start 2P', monospace;
  font-size: 8px;
  color: #1a2a10;
  text-align: center;
  line-height: 2;
  animation: pulse 2s ease-in-out infinite;
}

@keyframes pulse {
  0%, 100% { opacity: 0.7; }
  50% { opacity: 1; }
}

#drop-overlay.dragover {
  background: rgba(60,180,80,0.5);
}

#drop-overlay.dragover #drop-msg {
  color: #fff;
}

/* D-Pad & Buttons */
#controls-section {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-rows: auto auto;
  gap: 10px;
  padding: 12px 6px 0;
}

#dpad-container {
  display: flex;
  justify-content: center;
  align-items: center;
}

#dpad {
  display: grid;
  grid-template-columns: 32px 32px 32px;
  grid-template-rows: 32px 32px 32px;
  gap: 0;
}

.dpad-btn {
  background: #2a2a3a;
  border: 1px solid #3a3a4a;
  color: #888;
  font-size: 10px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: all 0.08s;
  -webkit-tap-highlight-color: transparent;
}

.dpad-btn:active, .dpad-btn.pressed {
  background: #1a1a28;
  box-shadow: inset 0 1px 3px rgba(0,0,0,0.5);
  color: #aaa;
}

#dpad-up { grid-column: 2; grid-row: 1; border-radius: 6px 6px 0 0; }
#dpad-left { grid-column: 1; grid-row: 2; border-radius: 6px 0 0 6px; }
.dpad-center { grid-column: 2; grid-row: 2; background: #2a2a3a; border: 1px solid #3a3a4a; }
#dpad-right { grid-column: 3; grid-row: 2; border-radius: 0 6px 6px 0; }
#dpad-down { grid-column: 2; grid-row: 3; border-radius: 0 0 6px 6px; }

#ab-container {
  display: flex;
  justify-content: center;
  align-items: center;
  gap: 10px;
  transform: rotate(-20deg);
}

.ab-btn {
  width: 42px;
  height: 42px;
  border-radius: 50%;
  background: var(--btn-ab);
  border: 2px solid #5a1e3a;
  color: #e8c0d8;
  font-family: 'Press Start 2P', monospace;
  font-size: 9px;
  cursor: pointer;
  box-shadow: 0 3px 0 #4a1830, 0 4px 8px rgba(0,0,0,0.3);
  transition: all 0.08s;
  -webkit-tap-highlight-color: transparent;
}

.ab-btn:active, .ab-btn.pressed {
  box-shadow: 0 1px 0 #4a1830;
  transform: translateY(2px);
  background: var(--btn-ab-press);
}

#startsel-container {
  grid-column: 1 / 3;
  display: flex;
  justify-content: center;
  gap: 16px;
  padding-top: 8px;
}

.ss-btn {
  padding: 5px 14px;
  border-radius: 10px;
  background: #3a3a4a;
  border: 1px solid #4a4a5a;
  color: #999;
  font-family: 'Press Start 2P', monospace;
  font-size: 6px;
  letter-spacing: 1px;
  cursor: pointer;
  box-shadow: 0 2px 0 #2a2a3a;
  transition: all 0.08s;
  -webkit-tap-highlight-color: transparent;
}

.ss-btn:active, .ss-btn.pressed {
  box-shadow: 0 0 0 #2a2a3a;
  transform: translateY(2px);
  background: #2a2a3a;
}

#speaker-grille {
  display: flex;
  flex-direction: column;
  gap: 3px;
  position: absolute;
  bottom: 40px;
  right: 24px;
  transform: rotate(-30deg);
}

.grille-line {
  width: 24px;
  height: 2px;
  background: rgba(0,0,0,0.25);
  border-radius: 1px;
}

/* Debug Panel */
#debug-panel {
  background: #12101e;
  border: 1px solid #2a2050;
  border-radius: 10px;
  padding: 14px;
  width: 280px;
  max-height: 80vh;
  overflow-y: auto;
  font-size: 11px;
}

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

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

#debug-panel h4 {
  font-size: 9px;
  color: #8a7aaa;
  margin-bottom: 6px;
  text-transform: uppercase;
  letter-spacing: 1px;
}

.debug-section {
  margin-bottom: 12px;
  padding-bottom: 10px;
  border-bottom: 1px solid #1e1838;
}

.mono { font-family: 'IBM Plex Mono', monospace; }

#reg-display {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 3px;
  font-size: 11px;
  color: #aaa;
}

#reg-display b {
  color: #e0c0ff;
  margin-left: 4px;
}

#flag-display {
  display: flex;
  gap: 8px;
}

.flag {
  display: inline-block;
  width: 24px;
  height: 24px;
  line-height: 24px;
  text-align: center;
  border-radius: 4px;
  background: #1a1828;
  color: #555;
  font-family: 'IBM Plex Mono', monospace;
  font-weight: 600;
  font-size: 10px;
  border: 1px solid #2a2040;
}

.flag.set {
  background: #2a1848;
  color: #e0c0ff;
  border-color: #5a3a8a;
  box-shadow: 0 0 6px rgba(160,100,255,0.3);
}

#cur-op { color: #b8a0d8; font-size: 11px; }
#perf-display { color: #8a8a9a; font-size: 10px; }

#vram-canvas {
  background: #0a0a14;
  border: 1px solid #2a2040;
  border-radius: 4px;
  image-rendering: pixelated;
  width: 100%;
  max-width: 256px;
}

.debug-controls {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
  border-bottom: none;
}

.debug-controls button {
  background: #1e1530;
  border: 1px solid #3a2a5a;
  color: #d4b8f0;
  padding: 4px 10px;
  border-radius: 4px;
  cursor: pointer;
  font-size: 10px;
  font-family: 'IBM Plex Mono', monospace;
}

.debug-controls button:hover { background: #2e2050; }

#btn-step {
  background: #1a2818;
  border-color: #3a5a2a;
  color: #b8f0b4;
}

#btn-step:hover:not(:disabled) {
  background: #2a4020;
  border-color: #5a8a4a;
}

.debug-controls label {
  font-size: 10px;
  color: #888;
  display: flex;
  align-items: center;
  gap: 4px;
}

.debug-controls input {
  background: #1a1828;
  border: 1px solid #2a2040;
  color: #e0c0ff;
  padding: 2px 6px;
  border-radius: 3px;
  width: 70px;
  font-family: 'IBM Plex Mono', monospace;
  font-size: 10px;
}

/* Modals */
#controls-modal, #about-modal {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.7);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

#controls-modal.hidden, #about-modal.hidden { display: none; }

.modal-content {
  background: #1a1530;
  border: 1px solid #3a2a5a;
  border-radius: 12px;
  padding: 24px;
  max-width: 360px;
  width: 90%;
}

.modal-content h3 {
  font-family: 'Press Start 2P', monospace;
  font-size: 11px;
  color: var(--accent);
  margin-bottom: 16px;
}

.modal-content table {
  width: 100%;
  border-collapse: collapse;
  margin-bottom: 16px;
}

.modal-content th, .modal-content td {
  padding: 6px 10px;
  text-align: left;
  font-size: 12px;
  border-bottom: 1px solid #2a2040;
}

.modal-content th { color: #8a7aaa; }
.modal-content td { color: #ccc; }

.modal-content p {
  font-size: 12px;
  margin-bottom: 10px;
  line-height: 1.5;
}

.modal-content .fun {
  color: #b8a0d8;
  font-style: italic;
}

.modal-content button {
  background: var(--shell-light);
  border: none;
  color: #fff;
  padding: 8px 20px;
  border-radius: 6px;
  cursor: pointer;
  font-family: 'IBM Plex Mono', monospace;
}

footer {
  padding: 16px;
  text-align: center;
  font-size: 11px;
  color: #555;
  display: flex;
  gap: 8px;
  align-items: center;
  justify-content: center;
  flex-wrap: wrap;
}

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

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

.legal { font-size: 9px; color: #444; }

/* Scrollbar */
::-webkit-scrollbar { width: 6px; }
::-webkit-scrollbar-track { background: transparent; }
::-webkit-scrollbar-thumb { background: #2a2050; border-radius: 3px; }

@media (max-width: 720px) {
  #app-container { flex-direction: column; align-items: center; }
  #debug-panel { width: 340px; max-width: 95vw; }
  #gameboy-shell { width: 95vw; max-width: 380px; min-width: unset; }
  #toolbar { font-size: 10px; }
  #toolbar button, #toolbar select { padding: 5px 8px; font-size: 10px; }
}