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

:root {
  --nes-red: #E60012;
  --charcoal: #2C2C2C;
  --amber: #FFB347;
  --bg: #1a1a2e;
  --text: #E8E8E8;
  --dark-surface: #16162a;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Press Start 2P', monospace;
  min-height: 100vh;
  overflow-x: hidden;
  background-image:
    radial-gradient(ellipse at 20% 50%, rgba(230, 0, 18, 0.03) 0%, transparent 50%),
    radial-gradient(ellipse at 80% 50%, rgba(255, 179, 71, 0.03) 0%, transparent 50%);
}

/* Noise texture overlay */
body::before {
  content: '';
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='noise'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23noise)' opacity='0.03'/%3E%3C/svg%3E");
  pointer-events: none;
  z-index: 0;
}

#app {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  min-height: 100vh;
}

/* Header */
header {
  width: 100%;
  background: var(--charcoal);
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 8px 16px;
  border-bottom: 3px solid #444;
  box-shadow: 0 2px 10px rgba(0,0,0,0.5);
  z-index: 50;
}

.header-left {
  display: flex;
  align-items: center;
  gap: 12px;
}

.power-led {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: #333;
  border: 1px solid #555;
  transition: all 0.3s;
}

.power-led.on {
  background: #ff0000;
  box-shadow: 0 0 8px #ff0000, 0 0 16px rgba(255,0,0,0.4);
  animation: led-blink 2s infinite;
}

@keyframes led-blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.7; }
}

.logo {
  font-size: 14px;
  color: var(--text);
  text-shadow: 2px 2px 0 #000, -1px -1px 0 rgba(255,255,255,0.1);
  letter-spacing: 2px;
}

.header-right {
  display: flex;
  gap: 8px;
}

.toolbar-btn {
  background: #3a3a3a;
  border: 2px solid #555;
  color: var(--text);
  width: 36px;
  height: 36px;
  border-radius: 6px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: 'Press Start 2P', monospace;
  font-size: 14px;
  transition: all 0.15s;
}

.toolbar-btn:hover {
  background: #4a4a4a;
  border-color: #777;
}

.toolbar-btn.active-crt {
  border-color: var(--amber);
  background: rgba(255,179,71,0.15);
  color: var(--amber);
  box-shadow: 0 0 10px rgba(255,179,71,0.3);
}

/* Upload Area */
.upload-area {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  flex: 1;
  padding: 40px 20px;
  width: 100%;
  max-width: 600px;
}

.cartridge-slot {
  border: 3px dashed #444;
  border-radius: 12px;
  padding: 40px;
  text-align: center;
  cursor: pointer;
  transition: all 0.3s;
  background: rgba(0,0,0,0.3);
  width: 100%;
}

.cartridge-slot:hover {
  border-color: #4488ff;
  box-shadow: 0 0 20px rgba(68,136,255,0.2), inset 0 0 20px rgba(68,136,255,0.05);
}

.cartridge-slot.drag-over {
  border-color: #4488ff;
  background: rgba(68,136,255,0.1);
  box-shadow: 0 0 30px rgba(68,136,255,0.3);
}

.cartridge-icon {
  margin-bottom: 20px;
  opacity: 0.7;
}

.slot-text {
  font-size: 14px;
  margin-bottom: 8px;
  color: var(--text);
}

.slot-sub {
  font-size: 8px;
  color: #888;
  line-height: 1.6;
}

.idle-console {
  margin-top: 30px;
}

.blink-text {
  font-size: 10px;
  color: var(--amber);
  animation: bounce-text 2s ease-in-out infinite;
}

@keyframes bounce-text {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-8px); }
}

/* Game Container */
.game-container {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 16px;
  width: 100%;
  flex: 1;
}

.game-container.hidden, .hidden {
  display: none !important;
}

.tv-bezel {
  background: #222;
  border: 4px solid #333;
  border-radius: 16px;
  padding: 16px;
  box-shadow:
    0 0 0 2px #1a1a1a,
    0 8px 32px rgba(0,0,0,0.6),
    inset 0 0 30px rgba(0,0,0,0.4);
  position: relative;
  max-width: 95vw;
}

.screen-wrapper {
  position: relative;
  border-radius: 8px;
  overflow: hidden;
  background: #000;
  box-shadow: inset 0 0 20px rgba(0,0,0,0.8);
  width: 640px;
  height: 480px;
  max-width: 85vw;
  max-height: 60vh;
}

/* EmulatorJS container */
#emulator-game {
  width: 100%;
  height: 100%;
  position: relative;
  z-index: 1;
}

/* Force EmulatorJS elements to fit our container */
#emulator-game > div {
  width: 100% !important;
  height: 100% !important;
}

/* Hide EmulatorJS built-in top bar / overlay controls so they don't cover the screen */
#emulator-game [class*="top-bar"],
#emulator-game [class*="topBar"],
#emulator-game .ejs_menu_bar,
#emulator-game .ejs_menu_bar_top,
#emulator-game [class*="menu_bar"],
#emulator-game [class*="menu-bar"],
#emulator-game .ejs--top-bar,
#emulator-game > div > div:not(canvas):not([class*="game"]) > div[style*="position: absolute"][style*="top: 0"],
#emulator-game .ejs_cheat_modal,
#emulator-game .ejs_menu_container {
  display: none !important;
  opacity: 0 !important;
  pointer-events: none !important;
  height: 0 !important;
  overflow: hidden !important;
}

/* Additional specificity: hide anything absolutely positioned at the top of the emulator */
#emulator-game > div > div > div[style*="z-index"] {
  display: none !important;
  pointer-events: none !important;
}

/* Keep the actual game canvas visible */
#emulator-game canvas {
  display: block !important;
  opacity: 1 !important;
  pointer-events: auto !important;
}

/* CRT overlay on top of emulator */
.crt-overlay-div {
  position: absolute;
  top: 0;
  left: 0;
  width: 100%;
  height: 100%;
  pointer-events: none;
  z-index: 10;
  display: none;
}

.crt-overlay-div.active {
  display: block;
}

/* CRT scanlines */
.crt-overlay-div.active::before {
  content: '';
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  background: repeating-linear-gradient(
    0deg,
    rgba(0,0,0,0) 0px,
    rgba(0,0,0,0) 1px,
    rgba(0,0,0,var(--crt-scanline-alpha, 0.1)) 1px,
    rgba(0,0,0,var(--crt-scanline-alpha, 0.1)) 2px
  );
  pointer-events: none;
  z-index: 11;
}

/* CRT vignette */
.crt-overlay-div.active::after {
  content: '';
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  background: radial-gradient(
    ellipse at center,
    rgba(0,0,0,0) 50%,
    rgba(0,0,0,var(--crt-vignette-alpha, 0.3)) 100%
  );
  pointer-events: none;
  z-index: 12;
}

/* CRT screen curvature effect */
.crt-active .screen-wrapper {
  border-radius: 12px;
}

.crt-active #emulator-game canvas {
  filter: contrast(1.1) brightness(1.05);
}

.tv-label {
  text-align: center;
  font-size: 8px;
  color: #555;
  margin-top: 8px;
  letter-spacing: 3px;
}

.rom-label {
  font-size: 8px;
  color: var(--amber);
  margin-top: 8px;
  padding: 4px 12px;
  background: rgba(255,179,71,0.1);
  border: 1px solid rgba(255,179,71,0.3);
  border-radius: 4px;
  max-width: 300px;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* Action Bar */
.action-bar {
  display: flex;
  gap: 8px;
  margin-top: 12px;
  align-items: center;
  flex-wrap: wrap;
  justify-content: center;
}

.action-btn {
  background: #2a2a3a;
  border: 2px solid #444;
  color: var(--text);
  height: 40px;
  padding: 0 14px;
  border-radius: 8px;
  cursor: pointer;
  font-size: 12px;
  font-family: 'Press Start 2P', monospace;
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 6px;
  transition: all 0.15s;
  white-space: nowrap;
}

.action-btn.icon-only {
  width: 40px;
  padding: 0;
  font-size: 16px;
}

.action-btn .btn-label {
  font-size: 7px;
  letter-spacing: 0.5px;
}

.action-btn:hover {
  background: #3a3a4a;
  border-color: #666;
  transform: translateY(-1px);
}

.action-btn:active {
  transform: translateY(1px);
}

/* Toast */
.toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--charcoal);
  border: 2px solid #555;
  color: var(--text);
  font-family: 'Press Start 2P', monospace;
  font-size: 10px;
  padding: 12px 24px;
  border-radius: 8px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.5);
  z-index: 1000;
  animation: toast-in 0.3s ease-out;
}

@keyframes toast-in {
  from { transform: translateX(-50%) translateY(20px); opacity: 0; }
  to { transform: translateX(-50%) translateY(0); opacity: 1; }
}

/* Settings Panel */
.settings-panel {
  position: fixed;
  top: 0; right: 0;
  width: 320px;
  max-width: 90vw;
  height: 100vh;
  background: #1e1e32;
  border-left: 3px solid #333;
  z-index: 100;
  overflow-y: auto;
  box-shadow: -4px 0 20px rgba(0,0,0,0.5);
  transform: translateX(0);
  transition: transform 0.3s;
}

.settings-panel.hidden {
  display: block !important;
  transform: translateX(100%);
  pointer-events: none;
}

.settings-content {
  padding: 24px 16px;
  position: relative;
}

.settings-content h2 {
  font-size: 12px;
  margin-bottom: 24px;
  color: var(--amber);
}

.settings-close {
  position: absolute;
  top: 16px;
  right: 16px;
  background: none;
  border: none;
  color: var(--text);
  font-size: 18px;
  cursor: pointer;
}

.setting-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 0;
  border-bottom: 1px solid #2a2a3a;
}

.setting-row label {
  font-size: 8px;
  color: #aaa;
  line-height: 1.6;
}

.setting-row input[type="range"] {
  width: 100px;
  accent-color: var(--amber);
}

.setting-row input[type="checkbox"] {
  accent-color: var(--nes-red);
  width: 18px;
  height: 18px;
}

/* Modal */
.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: 200;
}

.modal-content {
  background: #1e1e32;
  border: 3px solid #444;
  border-radius: 12px;
  padding: 24px;
  max-width: 400px;
  width: 90%;
  position: relative;
}

.modal-content h2 {
  font-size: 12px;
  margin-bottom: 20px;
  color: var(--amber);
}

.modal-close {
  position: absolute;
  top: 16px; right: 16px;
  background: none;
  border: none;
  color: var(--text);
  font-size: 18px;
  cursor: pointer;
}

.controls-grid {
  display: flex;
  flex-direction: column;
  gap: 8px;
}

.control-item {
  display: flex;
  justify-content: space-between;
  align-items: center;
  font-size: 8px;
  padding: 8px;
  background: rgba(0,0,0,0.3);
  border-radius: 4px;
}

.key {
  background: #333;
  padding: 4px 8px;
  border-radius: 4px;
  border: 1px solid #555;
  color: var(--amber);
}

/* Recent ROMs */
.recent-roms {
  width: 100%;
  max-width: 500px;
  padding: 0 16px;
  margin-top: 12px;
}

.recent-roms h3 {
  font-size: 8px;
  color: #666;
  margin-bottom: 8px;
}

.recent-rom-item {
  font-size: 7px;
  color: #888;
  padding: 6px 10px;
  background: rgba(0,0,0,0.2);
  border: 1px solid #2a2a3a;
  border-radius: 4px;
  margin-bottom: 4px;
  cursor: pointer;
  transition: all 0.15s;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.recent-rom-item:hover {
  border-color: #444;
  color: var(--text);
}

/* Footer */
.footer {
  width: 100%;
  text-align: center;
  padding: 16px;
  font-size: 7px;
  color: #555;
  margin-top: auto;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.footer a {
  color: #666;
  text-decoration: none;
}

.footer a:hover {
  color: var(--amber);
}

.berry-egg {
  cursor: pointer;
  transition: transform 0.2s;
}

.berry-egg:hover {
  transform: scale(1.3);
}

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

.cartridge-inserted {
  animation: cartridge-insert 0.5s ease-out;
}

/* Loading overlay */
.loading-overlay {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.85);
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  z-index: 20;
  gap: 16px;
}

.loading-overlay .spinner {
  width: 40px;
  height: 40px;
  border: 4px solid #333;
  border-top-color: var(--nes-red);
  border-radius: 50%;
  animation: spin 0.8s linear infinite;
}

@keyframes spin {
  to { transform: rotate(360deg); }
}

.loading-overlay .loading-text {
  font-size: 10px;
  color: var(--amber);
  animation: bounce-text 2s ease-in-out infinite;
}

/* Responsive */
@media (max-width: 700px) {
  .logo { font-size: 10px; }
  .tv-bezel { padding: 8px; border-radius: 10px; }
  .screen-wrapper {
    width: 90vw;
    height: calc(90vw * 0.75);
    max-height: 55vh;
  }
  .action-bar { flex-wrap: wrap; justify-content: center; }
}

@media (max-width: 400px) {
  .screen-wrapper {
    width: 95vw;
    height: calc(95vw * 0.75);
  }
}