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

:root {
  --bg-deep: #050508;
  --bg-dark: #0a0a0f;
  --bg-panel: #111118;
  --bg-card: #1a1a24;
  --bg-elevated: #22222e;
  --sega-red: #D32F2F;
  --sega-blue: #1565C0;
  --sega-green: #4CAF50;
  --text-primary: #e0e0e8;
  --text-secondary: #8888a0;
  --text-dim: #555568;
  --border: #2a2a3a;
  --glow-blue: rgba(21, 101, 192, 0.3);
  --glow-red: rgba(211, 47, 47, 0.3);
  --glow-green: rgba(76, 175, 80, 0.3);
}

body {
  font-family: 'JetBrains Mono', monospace;
  background: var(--bg-deep);
  color: var(--text-primary);
  min-height: 100vh;
  overflow-x: hidden;
  background-image: 
    linear-gradient(rgba(21, 101, 192, 0.03) 1px, transparent 1px),
    linear-gradient(90deg, rgba(21, 101, 192, 0.03) 1px, transparent 1px);
  background-size: 20px 20px;
}

#app-container {
  max-width: 1400px;
  margin: 0 auto;
  display: flex;
  flex-direction: column;
  min-height: 100vh;
}

/* Header */
#console-header {
  position: relative;
  background: linear-gradient(180deg, #141418 0%, #0e0e12 100%);
  border-bottom: 1px solid var(--border);
  overflow: hidden;
}

.header-stripe {
  height: 4px;
  background: linear-gradient(90deg, var(--sega-red), var(--sega-red) 40%, var(--sega-blue) 60%, var(--sega-blue));
}

.header-content {
  display: flex;
  align-items: center;
  gap: 16px;
  padding: 12px 24px;
}

.power-led {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #333;
  box-shadow: none;
  transition: all 0.3s;
  flex-shrink: 0;
}

.power-led.on {
  background: var(--sega-green);
  box-shadow: 0 0 8px var(--glow-green), 0 0 20px var(--glow-green);
  animation: pulse-led 2s ease-in-out infinite;
}

.power-led.paused {
  background: var(--sega-red);
  box-shadow: 0 0 8px var(--glow-red);
  animation: pulse-led 1s ease-in-out infinite;
}

@keyframes pulse-led {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.6; }
}

.header-text h1 {
  font-family: 'Press Start 2P', cursive;
  font-size: 16px;
  letter-spacing: 2px;
  background: linear-gradient(180deg, #fff 0%, #bbb 100%);
  -webkit-background-clip: text;
  -webkit-text-fill-color: transparent;
}

.subtitle {
  font-size: 9px;
  color: var(--text-dim);
  margin-top: 4px;
  letter-spacing: 1px;
}

/* Main Area */
#main-area {
  display: flex;
  flex: 1;
  gap: 0;
}

#display-section {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 20px;
}

/* CRT Display */
#crt-bezel {
  background: linear-gradient(145deg, #1a1a1e, #0d0d10);
  border-radius: 20px;
  padding: 20px;
  box-shadow: 
    0 0 0 2px #222,
    0 0 0 4px #111,
    inset 0 0 30px rgba(0,0,0,0.5),
    0 8px 32px rgba(0,0,0,0.8);
  position: relative;
}

#crt-bezel.running {
  box-shadow: 
    0 0 0 2px #222,
    0 0 0 4px #111,
    inset 0 0 30px rgba(0,0,0,0.5),
    0 8px 32px rgba(0,0,0,0.8),
    0 0 60px var(--glow-blue);
}

#crt-screen {
  position: relative;
  overflow: hidden;
  border-radius: 8px;
  background: #000;
  line-height: 0;
}

#canvas {
  width: 768px;
  height: 576px;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  display: block;
  background: #000;
}

#crt-screen.crt-mode #canvas {
  filter: brightness(1.1) contrast(1.1);
}

#crt-screen.crt-mode {
  border-radius: 16px;
}

#scanline-overlay {
  display: none;
  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;
}

#crt-screen.scanlines #scanline-overlay { display: block; }

#crt-vignette {
  display: none;
  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;
}

#crt-screen.crt-mode #crt-vignette { display: block; }

#drop-zone-overlay {
  display: none;
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(21, 101, 192, 0.3);
  border: 3px dashed var(--sega-blue);
  color: white;
  font-family: 'Press Start 2P', cursive;
  font-size: 14px;
  justify-content: center;
  align-items: center;
  z-index: 10;
}

#drop-zone-overlay.active { display: flex; }

/* Control Panel */
#control-panel {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  margin-top: 16px;
  justify-content: center;
}

.ctl-btn {
  font-family: 'Press Start 2P', cursive;
  font-size: 8px;
  padding: 8px 14px;
  background: linear-gradient(180deg, #2a2a34, #1e1e26);
  color: var(--text-secondary);
  border: 1px solid var(--border);
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.15s;
  display: flex;
  align-items: center;
  gap: 6px;
  user-select: none;
}

.ctl-btn:hover {
  background: linear-gradient(180deg, #333340, #28283a);
  color: var(--text-primary);
  border-color: var(--sega-blue);
}

.ctl-btn:active {
  transform: scale(0.95);
  background: #1a1a24;
}

.ctl-btn.active {
  border-color: var(--sega-green);
  color: var(--sega-green);
  box-shadow: 0 0 8px var(--glow-green);
}

.load-btn {
  cursor: pointer;
  border-color: var(--sega-blue);
  color: var(--sega-blue);
}

.btn-icon { font-size: 12px; }

/* Virtual Gamepad */
#virtual-gamepad {
  display: flex;
  align-items: center;
  gap: 60px;
  margin-top: 20px;
  padding: 20px 30px;
  background: linear-gradient(180deg, #1a1a20, #141418);
  border-radius: 30px 30px 12px 12px;
  border: 1px solid var(--border);
  box-shadow: inset 0 2px 4px rgba(255,255,255,0.03);
}

.dpad {
  display: grid;
  grid-template-columns: 40px 40px 40px;
  grid-template-rows: 40px 40px 40px;
  gap: 2px;
}

.dpad-btn {
  background: #222230;
  border: 1px solid #333;
  color: var(--text-dim);
  font-size: 14px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  border-radius: 4px;
  transition: all 0.1s;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
}

.dpad-btn:active, .dpad-btn.pressed {
  background: #333345;
  color: white;
  transform: scale(0.92);
}

.dpad-center {
  background: #1a1a24;
  border-radius: 50%;
  cursor: default;
}

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

#action-buttons {
  display: flex;
  gap: 20px;
}

.action-btn-wrap { text-align: center; }

.action-btn {
  width: 52px;
  height: 52px;
  border-radius: 50%;
  border: 2px solid #444;
  font-family: 'Press Start 2P', cursive;
  font-size: 12px;
  cursor: pointer;
  transition: all 0.1s;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
}

.btn-1 {
  background: linear-gradient(145deg, #c62828, #b71c1c);
  color: white;
  box-shadow: 0 3px 8px rgba(198,40,40,0.4);
}

.btn-2 {
  background: linear-gradient(145deg, #c62828, #b71c1c);
  color: white;
  box-shadow: 0 3px 8px rgba(198,40,40,0.4);
}

.action-btn:active, .action-btn.pressed {
  transform: scale(0.9);
  box-shadow: 0 1px 4px rgba(198,40,40,0.3);
}

/* Debug Panel */
#debug-panel {
  width: 340px;
  background: var(--bg-panel);
  border-left: 1px solid var(--border);
  overflow-y: auto;
  transition: width 0.3s, padding 0.3s;
  flex-shrink: 0;
}

#debug-panel.collapsed {
  width: 0;
  padding: 0;
  border: none;
  overflow: hidden;
}

.debug-header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  position: sticky;
  top: 0;
  background: var(--bg-panel);
  z-index: 5;
}

.debug-header h2 {
  font-family: 'Press Start 2P', cursive;
  font-size: 10px;
  color: var(--sega-blue);
}

#debug-close {
  background: none;
  border: none;
  color: var(--text-secondary);
  font-size: 18px;
  cursor: pointer;
}

.debug-content {
  padding: 12px 16px;
}

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

.debug-section h3 {
  font-family: 'Press Start 2P', cursive;
  font-size: 8px;
  color: var(--sega-red);
  margin-bottom: 8px;
}

.mono-data {
  font-size: 10px;
  line-height: 1.6;
  color: var(--text-secondary);
  white-space: pre;
  overflow-x: auto;
}

.flag-indicators {
  display: flex;
  gap: 6px;
  margin: 6px 0;
  flex-wrap: wrap;
}

.flag-indicators span {
  font-size: 9px;
  padding: 2px 6px;
  border-radius: 3px;
  background: #1a1a24;
  border: 1px solid #2a2a3a;
  color: var(--text-dim);
}

.flag-indicators span.set {
  background: #1b3a1b;
  border-color: var(--sega-green);
  color: var(--sega-green);
}

.disasm { color: #7ab; }

.hex-viewer {
  font-size: 9px;
  line-height: 1.5;
  max-height: 200px;
  overflow-y: auto;
  color: #8a8;
}

.volume-control {
  display: flex;
  align-items: center;
  gap: 8px;
  margin-top: 8px;
  font-size: 9px;
  color: var(--text-dim);
}

.volume-control input[type="range"] {
  flex: 1;
  accent-color: var(--sega-blue);
}

.dbg-btn {
  font-family: 'JetBrains Mono', monospace;
  font-size: 9px;
  padding: 4px 10px;
  background: #1a1a2e;
  color: var(--sega-blue);
  border: 1px solid var(--sega-blue);
  border-radius: 4px;
  cursor: pointer;
  margin: 2px;
}

.dbg-btn:hover { background: #222240; }

.bp-input {
  display: flex;
  gap: 4px;
  margin-top: 8px;
}

.bp-input input {
  flex: 1;
  font-family: 'JetBrains Mono', monospace;
  font-size: 9px;
  padding: 4px 8px;
  background: #0e0e18;
  color: var(--text-primary);
  border: 1px solid var(--border);
  border-radius: 4px;
  outline: none;
}

.bp-input input:focus { border-color: var(--sega-blue); }

#psg-channels {
  display: flex;
  flex-direction: column;
  gap: 4px;
}

.psg-ch {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 9px;
  color: var(--text-secondary);
}

.psg-bar {
  width: 60px;
  height: 8px;
  background: #1a1a24;
  border-radius: 4px;
  overflow: hidden;
}

.psg-bar-fill {
  height: 100%;
  background: var(--sega-green);
  transition: width 0.05s;
  border-radius: 4px;
}

/* Status Bar */
#status-bar {
  display: flex;
  gap: 20px;
  padding: 6px 24px;
  background: var(--bg-panel);
  border-top: 1px solid var(--border);
  font-size: 9px;
  color: var(--text-dim);
  flex-wrap: wrap;
}

#status-bar span { white-space: nowrap; }

#app-footer {
  text-align: center;
  padding: 12px;
  font-size: 9px;
  color: var(--text-dim);
  border-top: 1px solid #1a1a2a;
}

#app-footer a {
  color: var(--sega-blue);
  text-decoration: none;
  margin-left: 8px;
}

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

/* Responsive */
@media (max-width: 900px) {
  #canvas {
    width: 100%;
    height: auto;
    max-width: 512px;
  }
  
  #crt-bezel { padding: 10px; border-radius: 12px; }
  
  .header-text h1 { font-size: 11px; }
  .subtitle { font-size: 7px; }
  
  #debug-panel {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    width: 100% !important;
    max-height: 50vh;
    border-left: none;
    border-top: 2px solid var(--sega-blue);
    z-index: 100;
  }
  
  #debug-panel.collapsed { max-height: 0; }
  
  #control-panel { gap: 4px; }
  .ctl-btn { padding: 6px 8px; font-size: 7px; }
  
  #virtual-gamepad { gap: 40px; padding: 16px 20px; }
  
  #display-section { padding: 10px; }
}

@media (max-width: 500px) {
  #canvas {
    width: 100%;
    max-width: 320px;
  }
  
  .header-content { padding: 8px 12px; }
  .header-text h1 { font-size: 9px; }
  
  #virtual-gamepad { gap: 20px; }
  .dpad { grid-template-columns: 36px 36px 36px; grid-template-rows: 36px 36px 36px; }
  .action-btn { width: 44px; height: 44px; font-size: 10px; }
}