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

:root {
  --bg-dark: #0a0a0a;
  --bg-panel: #141414;
  --bg-surface: #1a1a1a;
  --sega-blue: #0060A8;
  --sega-red: #CC0000;
  --text-primary: #e0e0e0;
  --text-dim: #666;
  --text-green: #33ff66;
  --crt-glow: rgba(0, 96, 168, 0.15);
  --led-on: #ff2020;
  --led-off: #330808;
}

html, body {
  width: 100%; height: 100%;
  background: var(--bg-dark);
  color: var(--text-primary);
  font-family: 'JetBrains Mono', monospace;
  overflow-x: hidden;
  -webkit-user-select: none; user-select: none;
  touch-action: manipulation;
}

#app {
  display: flex; flex-direction: column;
  min-height: 100vh;
  align-items: center;
}

/* HEADER */
#header {
  width: 100%; padding: 12px 20px;
  background: linear-gradient(180deg, #1a1a1a 0%, #0d0d0d 100%);
  border-bottom: 2px solid var(--sega-red);
}
.header-content {
  max-width: 900px; margin: 0 auto;
  display: flex; align-items: center; justify-content: space-between;
}
.title {
  font-family: 'Archivo Black', sans-serif;
  font-size: 22px; letter-spacing: 2px;
  color: var(--sega-blue);
  text-shadow: 0 0 20px rgba(0,96,168,0.5), 0 0 40px rgba(0,96,168,0.2);
  font-style: italic;
}
.title-accent { color: #fff; }
.subtitle {
  font-size: 10px; color: var(--text-dim);
  letter-spacing: 1px; margin-top: 2px;
  font-weight: 300;
}
.power-led {
  width: 12px; height: 12px;
  border-radius: 50%;
  background: var(--led-off);
  box-shadow: 0 0 4px var(--led-off);
  transition: all 0.3s;
}
.power-led.on {
  background: var(--led-on);
  box-shadow: 0 0 8px var(--led-on), 0 0 20px rgba(255,32,32,0.4);
  animation: led-pulse 2s ease-in-out infinite;
}
@keyframes led-pulse {
  0%, 100% { box-shadow: 0 0 8px var(--led-on), 0 0 20px rgba(255,32,32,0.4); }
  50% { box-shadow: 0 0 12px var(--led-on), 0 0 30px rgba(255,32,32,0.6); }
}

/* MAIN */
#main {
  flex: 1; display: flex; flex-direction: column;
  align-items: center; padding: 20px 10px;
  width: 100%; max-width: 900px;
}

/* CRT SCREEN */
.screen-container { width: 100%; display: flex; justify-content: center; }
.crt-bezel {
  background: linear-gradient(145deg, #2a2a2a, #1a1a1a);
  border-radius: 18px; padding: 16px;
  box-shadow: 0 4px 30px rgba(0,0,0,0.8), inset 0 1px 0 rgba(255,255,255,0.05);
  border: 1px solid #333;
}
.crt-screen {
  position: relative; overflow: hidden;
  border-radius: 8px;
  background: #000;
  box-shadow: inset 0 0 60px rgba(0,0,0,0.8), 0 0 30px var(--crt-glow);
}
#canvas {
  display: block;
  width: 640px; height: 480px;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}
.scanlines {
  position: absolute; top: 0; left: 0;
  width: 100%; height: 100%;
  background: repeating-linear-gradient(
    0deg,
    transparent,
    transparent 1px,
    rgba(0,0,0,0.15) 1px,
    rgba(0,0,0,0.15) 2px
  );
  pointer-events: none;
  z-index: 2;
}
.scanlines.hidden { display: none; }
.screen-glow {
  position: absolute; top: 0; left: 0;
  width: 100%; height: 100%;
  background: radial-gradient(ellipse at center, transparent 60%, rgba(0,0,0,0.4) 100%);
  pointer-events: none; z-index: 3;
}

/* CONTROL PANEL */
.control-panel {
  width: 100%; margin-top: 16px;
  background: var(--bg-panel);
  border-radius: 10px; overflow: hidden;
  border: 1px solid #222;
}
.red-stripe {
  height: 4px;
  background: linear-gradient(90deg, var(--sega-red), #ff3333, var(--sega-red));
}
.controls-inner {
  display: flex; align-items: center;
  justify-content: space-between;
  padding: 12px 16px; gap: 12px;
  flex-wrap: wrap;
}
.rom-section { display: flex; align-items: center; gap: 10px; }
.btn {
  border: none; cursor: pointer;
  font-family: 'JetBrains Mono', monospace;
  transition: all 0.1s;
  outline: none;
}
.btn:active { transform: scale(0.95); }
.cart-btn {
  background: linear-gradient(180deg, #333, #222);
  color: var(--text-primary);
  padding: 8px 16px; border-radius: 4px;
  font-size: 11px; font-weight: 700;
  letter-spacing: 1px;
  border: 2px solid #444;
  box-shadow: inset 0 -2px 0 #111, 0 2px 4px rgba(0,0,0,0.5);
}
.cart-btn:active { box-shadow: inset 0 2px 4px rgba(0,0,0,0.5); }
.rom-display {
  background: #0a0f0a; color: var(--text-green);
  font-size: 10px; padding: 6px 12px;
  border-radius: 3px; min-width: 120px;
  border: 1px solid #1a3a1a;
  text-transform: uppercase; letter-spacing: 1px;
  max-width: 200px; overflow: hidden;
  white-space: nowrap; text-overflow: ellipsis;
}
.emu-controls { display: flex; gap: 8px; align-items: center; }
.power-btn {
  width: 36px; height: 36px; border-radius: 50%;
  background: radial-gradient(circle, #cc0000, #990000);
  color: #fff; font-size: 16px;
  box-shadow: 0 2px 8px rgba(204,0,0,0.4);
}
.power-btn.active {
  background: radial-gradient(circle, #00cc00, #009900);
  box-shadow: 0 2px 8px rgba(0,204,0,0.4);
}
.pause-btn, .reset-btn, .mute-btn, .scan-btn, .debug-btn {
  background: linear-gradient(180deg, #2a2a2a, #1a1a1a);
  color: var(--text-dim); padding: 6px 10px;
  border-radius: 4px; font-size: 14px;
  border: 1px solid #333;
}
.pause-btn:hover, .reset-btn:hover, .mute-btn:hover, .scan-btn:hover, .debug-btn:hover {
  color: var(--text-primary); border-color: #555;
}
.fps-display {
  font-size: 11px; color: var(--text-green);
  background: #0a0f0a; padding: 4px 10px;
  border-radius: 3px; border: 1px solid #1a3a1a;
  min-width: 70px; text-align: center;
}

/* DEBUG PANEL */
.debug-panel {
  position: fixed; top: 0; right: -360px;
  width: 350px; height: 100vh;
  background: var(--bg-surface);
  border-left: 2px solid var(--sega-blue);
  z-index: 100; transition: right 0.3s ease;
  overflow-y: auto;
}
.debug-panel.open { right: 0; }
.debug-header {
  display: flex; justify-content: space-between;
  align-items: center; padding: 12px 16px;
  background: linear-gradient(90deg, var(--sega-blue), #004080);
  color: #fff; font-size: 12px; font-weight: 700;
  letter-spacing: 2px;
}
.debug-close {
  background: none; border: none; color: #fff;
  font-size: 18px; cursor: pointer;
}
.debug-content { padding: 12px; font-size: 10px; }
.debug-section {
  margin-bottom: 16px;
  border: 1px solid #333;
  border-radius: 6px; overflow: hidden;
}
.debug-section-title {
  background: #222; padding: 6px 10px;
  font-weight: 700; letter-spacing: 1px;
  color: var(--sega-blue); font-size: 11px;
}
.debug-section-body { padding: 8px 10px; }
.reg-grid {
  display: grid; grid-template-columns: repeat(4, 1fr);
  gap: 4px;
}
.reg-item { text-align: center; }
.reg-label { color: var(--text-dim); font-size: 9px; }
.reg-value { color: var(--text-green); font-size: 11px; font-weight: 700; }
.flag-row { display: flex; gap: 6px; margin-top: 6px; justify-content: center; }
.flag-item {
  width: 22px; height: 22px;
  border-radius: 50%; display: flex;
  align-items: center; justify-content: center;
  font-size: 9px; font-weight: 700;
}
.flag-on { background: var(--sega-blue); color: #fff; }
.flag-off { background: #222; color: #555; border: 1px solid #333; }
.vu-container { display: flex; gap: 8px; align-items: flex-end; height: 50px; justify-content: center; }
.vu-bar-wrap {
  display: flex; flex-direction: column;
  align-items: center; gap: 2px;
}
.vu-bar {
  width: 20px; height: 40px;
  background: #111; border: 1px solid #333;
  border-radius: 2px; position: relative;
  overflow: hidden;
}
.vu-fill {
  position: absolute; bottom: 0; left: 0;
  width: 100%;
  background: linear-gradient(0deg, #33ff66, #ffff00, #ff3333);
  transition: height 0.1s;
}
.vu-label { font-size: 8px; color: var(--text-dim); }
.disasm-line { color: #888; white-space: pre; }
.disasm-line.current { color: var(--text-green); font-weight: 700; }

/* VIRTUAL GAMEPAD */
.virtual-gamepad {
  display: none;
  width: 100%; max-width: 500px;
  padding: 20px;
  justify-content: space-between;
  align-items: center; margin-top: 10px;
}
.dpad-container {
  display: grid;
  grid-template-columns: 44px 44px 44px;
  grid-template-rows: 44px 44px 44px;
  gap: 2px;
}
.dpad-btn {
  background: #222; color: #888;
  border: 1px solid #333; border-radius: 4px;
  font-size: 16px; cursor: pointer;
  display: flex; align-items: center;
  justify-content: center;
  -webkit-tap-highlight-color: transparent;
}
.dpad-btn:active { background: #444; color: #fff; }
.dpad-up { grid-column: 2; grid-row: 1; }
.dpad-left { grid-column: 1; grid-row: 2; }
.dpad-center {
  grid-column: 2; grid-row: 2;
  background: #1a1a1a; border-radius: 50%;
  border: 1px solid #333;
}
.dpad-right { grid-column: 3; grid-row: 2; }
.dpad-down { grid-column: 2; grid-row: 3; }
.action-container { display: flex; gap: 12px; }
.action-btn {
  width: 52px; height: 52px;
  border-radius: 50%;
  background: linear-gradient(145deg, #333, #1a1a1a);
  border: 2px solid #444;
  color: #aaa; font-size: 14px;
  font-weight: 700; cursor: pointer;
  font-family: 'Archivo Black', sans-serif;
  -webkit-tap-highlight-color: transparent;
}
.action-btn:active { background: #555; color: #fff; }
.action-pause { width: 40px; height: 28px; border-radius: 4px; font-size: 10px; }

/* KEY OVERLAY */
.key-overlay {
  display: none; position: fixed;
  top: 0; left: 0; width: 100%; height: 100%;
  background: rgba(0,0,0,0.85);
  z-index: 200; align-items: center; justify-content: center;
}
.key-overlay.show { display: flex; }
.key-overlay-inner {
  background: var(--bg-surface);
  border: 2px solid var(--sega-blue);
  border-radius: 12px; padding: 30px;
  max-width: 320px; text-align: center;
}
.key-overlay-inner h3 {
  color: var(--sega-blue); margin-bottom: 20px;
  font-size: 14px; letter-spacing: 2px;
}
.key-row {
  display: flex; justify-content: space-between;
  padding: 6px 0; font-size: 12px; color: var(--text-dim);
}
.key {
  background: #333; padding: 2px 8px;
  border-radius: 3px; color: #fff;
  font-size: 11px; border: 1px solid #555;
}
.key-dismiss { color: #555; margin-top: 16px; font-size: 10px; }

/* FOOTER */
#footer {
  width: 100%; padding: 12px;
  text-align: center; font-size: 11px;
  color: var(--text-dim);
  border-top: 1px solid #1a1a1a;
}
#footer a {
  color: var(--sega-blue); text-decoration: none;
}
#footer a:hover { text-decoration: underline; }
.sep { margin: 0 8px; }

/* RESPONSIVE */
@media (max-width: 700px) {
  .title { font-size: 15px; }
  .subtitle { font-size: 8px; }
  #canvas { width: 100%; height: auto; }
  .crt-bezel { padding: 8px; border-radius: 10px; }
  .controls-inner { flex-direction: column; gap: 8px; }
  .rom-section { flex-wrap: wrap; justify-content: center; }
  .virtual-gamepad { display: flex; }
  .debug-panel { width: 280px; }
}

@media (hover: none) and (pointer: coarse) {
  .virtual-gamepad { display: flex; }
}