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

:root {
  --bg: #0D0D0D;
  --bg2: #1A1A1A;
  --red: #D32F2F;
  --red-dark: #9A0007;
  --red-light: #FF6659;
  --text: #E0E0E0;
  --text-dim: #666;
  --green: #00FF41;
  --amber: #FFB000;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Press Start 2P', monospace;
  min-height: 100vh;
  display: flex;
  flex-direction: column;
  align-items: center;
  overflow-x: hidden;
}

#header {
  text-align: center;
  padding: 16px 12px 8px;
  background: linear-gradient(180deg, var(--red-dark) 0%, var(--bg) 100%);
  width: 100%;
  border-bottom: 2px solid var(--red);
}

#header h1 {
  font-size: 14px;
  color: #fff;
  text-shadow: 0 0 10px var(--red);
  letter-spacing: 1px;
}

.tagline {
  font-size: 8px;
  color: var(--text-dim);
  margin-top: 6px;
}

#main-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  flex: 1;
  width: 100%;
  max-width: 800px;
  padding: 12px;
}

#drop-zone {
  width: 100%;
  max-width: 500px;
  margin: 40px auto;
  cursor: pointer;
}
#drop-zone.hidden { display: none; }
#drop-zone.visible { display: block; }

.cartridge-slot {
  border: 3px dashed var(--red);
  background: #111;
  border-radius: 12px;
  padding: 40px 20px;
  text-align: center;
  transition: all 0.3s;
}
.cartridge-slot:hover, .cartridge-slot.drag-over {
  border-color: var(--red-light);
  background: #1a0808;
  box-shadow: 0 0 30px rgba(211,47,47,0.3);
}
.cartridge-icon { font-size: 48px; margin-bottom: 16px; }
.cartridge-slot p { font-size: 10px; line-height: 1.8; }
.cartridge-slot .small { font-size: 8px; color: var(--text-dim); }
.cartridge-slot .hint { font-size: 7px; color: var(--text-dim); margin-top: 12px; }
#file-input { display: none; }

#screen-wrapper { width: 100%; display: flex; justify-content: center; }
#screen-wrapper.hidden { display: none; }

#crt-bezel {
  position: relative;
  background: #0a0a0a;
  border: 6px solid #222;
  border-radius: 8px;
  box-shadow:
    inset 0 0 30px rgba(0,0,0,0.8),
    0 0 20px rgba(0,0,0,0.5),
    0 4px 0 #333,
    0 -2px 0 #111;
  padding: 8px;
  margin: 8px 0;
}

#screen {
  display: block;
  width: 512px;
  max-width: calc(100vw - 60px);
  height: auto;
  image-rendering: pixelated;
  image-rendering: -moz-crisp-edges;
  image-rendering: crisp-edges;
  background: #000;
  aspect-ratio: 256 / 192;
}

#scanline-overlay {
  position: absolute;
  top: 8px; left: 8px; right: 8px; bottom: 8px;
  pointer-events: none;
  background: repeating-linear-gradient(
    0deg,
    transparent 0px,
    transparent 1px,
    rgba(0,0,0,0.15) 1px,
    rgba(0,0,0,0.15) 2px
  );
  z-index: 2;
}
#scanline-overlay.hidden { display: none; }

#controls {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  justify-content: center;
  margin: 10px 0 6px;
}
#controls.hidden { display: none; }

#controls button {
  font-family: 'Press Start 2P', monospace;
  font-size: 8px;
  padding: 8px 12px;
  background: var(--bg2);
  color: var(--text);
  border: 2px solid #333;
  border-radius: 20px;
  cursor: pointer;
  transition: all 0.2s;
  white-space: nowrap;
}
#controls button:hover {
  border-color: var(--red);
  box-shadow: 0 0 10px rgba(211,47,47,0.4);
  color: #fff;
}
#controls button.active {
  background: var(--red-dark);
  border-color: var(--red);
  color: #fff;
}

#turbo-slider-wrap {
  display: flex;
  align-items: center;
  gap: 6px;
  padding: 2px 8px;
  background: var(--bg2);
  border: 2px solid var(--red-dark);
  border-radius: 20px;
}
#turbo-slider-wrap.hidden { display: none; }
#turbo-label {
  font-family: 'Press Start 2P', monospace;
  font-size: 7px;
  color: var(--amber);
  white-space: nowrap;
}
#turbo-value {
  color: var(--red-light);
  min-width: 24px;
  display: inline-block;
  text-align: right;
}
#turbo-slider {
  -webkit-appearance: none;
  appearance: none;
  width: 100px;
  height: 6px;
  background: #333;
  border-radius: 3px;
  outline: none;
  cursor: pointer;
}
#turbo-slider::-webkit-slider-thumb {
  -webkit-appearance: none;
  appearance: none;
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--red);
  border: 2px solid var(--red-light);
  cursor: pointer;
  box-shadow: 0 0 6px rgba(211,47,47,0.5);
}
#turbo-slider::-moz-range-thumb {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  background: var(--red);
  border: 2px solid var(--red-light);
  cursor: pointer;
  box-shadow: 0 0 6px rgba(211,47,47,0.5);
}

#mapper-bar {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  align-items: center;
  justify-content: center;
  padding: 8px 12px;
  background: #111;
  border: 1px solid #2a2a2a;
  border-radius: 8px;
  margin: 6px 0;
  width: 100%;
  max-width: 600px;
}
#mapper-bar.hidden { display: none; }

.mapper-label {
  font-size: 7px;
  color: var(--amber);
  letter-spacing: 1px;
}

#mapper-detected {
  font-size: 8px;
  color: var(--green);
  margin-left: 4px;
}

#system-info {
  display: flex;
  gap: 12px;
  align-items: center;
  padding: 4px 8px;
  background: #0a1a0a;
  border: 1px solid #1a3a1a;
  border-radius: 6px;
}

.system-label, .region-label {
  font-size: 7px;
  color: var(--text-dim);
  letter-spacing: 0.5px;
}

.system-value, .region-value {
  color: var(--green);
  font-weight: bold;
}

#keyboard-map { font-size: 7px; color: var(--text-dim); margin: 4px 0; text-align: center; }
#keyboard-map.hidden { display: none; }
.key {
  display: inline-block;
  background: #222;
  border: 1px solid #444;
  border-radius: 3px;
  padding: 2px 5px;
  font-size: 7px;
  color: var(--text);
}

#gamepad {
  display: flex;
  justify-content: center;
  align-items: center;
  padding: 12px 8px 4px;
  z-index: 100;
  width: 100%;
  max-width: 800px;
  margin: 0 auto;
}
#gamepad.hidden { display: none; }

#controller-body {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  max-width: 580px;
  background: linear-gradient(180deg, #1a1a1a 0%, #111 40%, #0a0a0a 100%);
  border: 3px solid #333;
  border-bottom-color: #1a1a1a;
  border-top-color: #444;
  border-radius: 16px 16px 40px 40px;
  padding: 18px 24px 24px;
  box-shadow:
    0 6px 20px rgba(0,0,0,0.7),
    inset 0 1px 0 rgba(255,255,255,0.06),
    inset 0 -2px 4px rgba(0,0,0,0.5);
  position: relative;
}

#controller-body::before {
  content: 'SEGA';
  position: absolute;
  top: -1px;
  left: 50%;
  transform: translateX(-50%);
  font-family: 'Press Start 2P', monospace;
  font-size: 6px;
  color: #555;
  letter-spacing: 4px;
  padding: 0 8px;
}

#controller-left {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
}

#dpad {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 0;
  background: #0d0d0d;
  border-radius: 50%;
  padding: 4px;
  box-shadow:
    inset 0 2px 6px rgba(0,0,0,0.8),
    0 1px 2px rgba(255,255,255,0.05);
}
.dpad-row { display: flex; gap: 0; align-items: center; }
.dpad-center {
  width: 36px; height: 36px;
  background: #222;
  border-radius: 50%;
  display: flex;
  align-items: center;
  justify-content: center;
  color: #444;
  font-size: 18px;
  font-weight: bold;
  box-shadow: inset 0 1px 3px rgba(0,0,0,0.6);
}
.dpad-btn {
  width: 36px; height: 36px;
  background: #1f1f1f;
  color: #888;
  border: 1px solid #2a2a2a;
  border-radius: 4px;
  font-size: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.1s;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  user-select: none;
}
.dpad-btn:hover { background: #2a2a2a; color: #ccc; }
.dpad-btn:active { background: #333; color: #fff; box-shadow: inset 0 1px 3px rgba(0,0,0,0.5); }

#controller-center {
  flex: 0 0 auto;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 8px;
}

.controller-label {
  font-family: 'Press Start 2P', monospace;
  font-size: 5px;
  color: #444;
  letter-spacing: 2px;
  text-transform: uppercase;
}

.start-btn {
  font-family: 'Press Start 2P', monospace;
  font-size: 7px;
  padding: 6px 18px;
  background: #1a1a1a;
  color: #aaa;
  border: 2px solid #333;
  border-radius: 12px;
  cursor: pointer;
  transition: all 0.15s;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  user-select: none;
  box-shadow: 0 2px 4px rgba(0,0,0,0.4);
  letter-spacing: 1px;
}
.start-btn:hover { border-color: var(--red); color: #fff; box-shadow: 0 0 8px rgba(211,47,47,0.3); }
.start-btn:active { background: var(--red-dark); border-color: var(--red); color: #fff; transform: scale(0.95); }

#controller-right {
  flex: 0 0 auto;
  display: flex;
  align-items: center;
  justify-content: center;
}

#action-buttons { display: flex; gap: 14px; align-items: center; }
.action-btn {
  width: 52px; height: 52px;
  border-radius: 50%;
  background: var(--red);
  color: #fff;
  border: 3px solid #b71c1c;
  font-family: 'Press Start 2P', monospace;
  font-size: 12px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.1s;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
  user-select: none;
  box-shadow:
    0 4px 8px rgba(0,0,0,0.5),
    inset 0 1px 0 rgba(255,255,255,0.15),
    inset 0 -2px 0 rgba(0,0,0,0.2);
  text-shadow: 0 1px 2px rgba(0,0,0,0.5);
}
.action-btn:hover {
  background: var(--red-light);
  border-color: var(--red);
  box-shadow: 0 4px 12px rgba(211,47,47,0.4), inset 0 1px 0 rgba(255,255,255,0.2), inset 0 -2px 0 rgba(0,0,0,0.2);
}
.action-btn:active {
  background: var(--red-dark);
  border-color: #7f0000;
  transform: scale(0.93);
  box-shadow: 0 2px 4px rgba(0,0,0,0.5), inset 0 2px 4px rgba(0,0,0,0.3);
}

#debug-panel {
  position: fixed;
  top: 0; right: 0;
  width: 380px;
  max-width: 90vw;
  height: 100vh;
  background: rgba(0,0,0,0.95);
  border-left: 2px solid var(--green);
  padding: 12px;
  overflow-y: auto;
  z-index: 200;
  transition: transform 0.3s;
  font-family: monospace;
  font-size: 11px;
  color: var(--green);
}
#debug-panel.hidden { transform: translateX(100%); }
#debug-panel h3 {
  font-family: 'Press Start 2P', monospace;
  font-size: 10px;
  color: var(--amber);
  margin-bottom: 10px;
  border-bottom: 1px solid #333;
  padding-bottom: 6px;
}
#debug-content { line-height: 1.6; white-space: pre; }

.sram-indicator {
  text-align: center;
  font-family: 'Press Start 2P', monospace;
  font-size: 8px;
  color: var(--green);
  padding: 4px 10px;
  opacity: 0;
  transition: opacity 0.3s ease;
  height: 0;
  overflow: hidden;
}
.sram-indicator.visible {
  opacity: 1;
  height: auto;
  padding: 6px 10px;
}

#btn-sram { position: relative; }
#btn-sram.active {
  background: linear-gradient(180deg, #2E7D32, #1B5E20) !important;
  border-color: #4CAF50 !important;
  box-shadow: 0 0 8px rgba(76, 175, 80, 0.5);
}

#status-bar {
  width: 100%;
  background: #111;
  border-top: 1px solid #222;
  padding: 6px 12px;
  display: flex;
  gap: 16px;
  font-size: 7px;
  color: var(--text-dim);
  flex-wrap: wrap;
  justify-content: center;
}

#status-system {
  color: var(--green);
  font-weight: bold;
}

footer {
  padding: 10px;
  text-align: center;
}
footer a {
  color: var(--text-dim);
  font-size: 8px;
  font-family: 'Press Start 2P', monospace;
  text-decoration: none;
}
footer a:hover { color: var(--red); }

@media (max-width: 768px) {
  #keyboard-map { display: none !important; }
  #screen { max-width: calc(100vw - 40px); }
  #controls button { font-size: 7px; padding: 6px 8px; }
  body { padding-bottom: 20px; }
  #controller-body { padding: 14px 16px 18px; max-width: 100%; }
  .dpad-btn { width: 32px; height: 32px; font-size: 10px; }
  .dpad-center { width: 30px; height: 30px; font-size: 14px; }
  .action-btn { width: 44px; height: 44px; font-size: 10px; }
  #action-buttons { gap: 10px; }
  .start-btn { font-size: 6px; padding: 5px 12px; }
  #mapper-bar { flex-direction: column; gap: 4px; }
}