* {
  margin: 0;
  padding: 0;
  box-sizing: border-box;
  -webkit-tap-highlight-color: transparent;
}

html, body {
  width: 100%;
  height: 100%;
  overflow-x: hidden;
  overflow-y: auto;
  background: #0a0a1a;
  font-family: 'Press Start 2P', monospace;
}

#app-container {
  width: 100%;
  min-height: 100%;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: flex-start;
  padding-top: 10px;
  background: radial-gradient(ellipse at center, #1a1a2e 0%, #0a0a1a 70%);
}

#tv-frame {
  position: relative;
  flex-shrink: 0;
}

#tv-bezel {
  background: linear-gradient(145deg, #3a3a4a, #252535, #1e1e2e);
  border-radius: 16px;
  padding: 16px 20px 36px 20px;
  box-shadow:
    0 0 0 3px #111,
    0 8px 32px rgba(0,0,0,0.8),
    inset 0 1px 0 rgba(255,255,255,0.05);
  position: relative;
}

#screen-container {
  position: relative;
  overflow: hidden;
  border-radius: 6px;
  background: #000;
  box-shadow: inset 0 0 30px rgba(0,0,0,0.9);
}

#canvas {
  display: block;
  image-rendering: pixelated;
  image-rendering: -moz-crisp-edges;
  image-rendering: crisp-edges;
  width: 512px;
  height: 480px;
}

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

#crt-overlay {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  pointer-events: none;
  background: radial-gradient(ellipse at center, transparent 60%, rgba(0,0,0,0.4) 100%);
  z-index: 3;
}

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

#bezel-led {
  position: absolute;
  bottom: 12px;
  right: 28px;
  width: 8px;
  height: 8px;
  border-radius: 50%;
  background: #333;
  box-shadow: none;
  transition: none;
}

#bezel-led.on {
  background: #ff2222;
  box-shadow: 0 0 6px #ff2222, 0 0 12px #ff000066;
  animation: led-pulse 2s ease-in-out infinite;
}

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

#controls-bar {
  display: flex;
  gap: 8px;
  margin-top: 8px;
  align-items: center;
  flex-wrap: wrap;
  justify-content: center;
}

.nes-btn {
  font-family: 'Press Start 2P', monospace;
  font-size: 8px;
  background: #c41a1a;
  color: #fff;
  border: 2px solid #8b0e0e;
  padding: 8px 14px;
  cursor: pointer;
  text-transform: uppercase;
  letter-spacing: 1px;
  box-shadow: 0 3px 0 #600808;
  transition: none;
}
.nes-btn:hover { background: #d42a2a; }
.nes-btn:active { transform: translateY(2px); box-shadow: 0 1px 0 #600808; }
.nes-btn.small { font-size: 7px; padding: 6px 10px; }
.nes-btn.tiny { font-size: 6px; padding: 4px 8px; background: #2a2a4a; border-color: #3a3a5a; box-shadow: 0 2px 0 #1a1a2a; }
.nes-btn.tiny:hover { background: #3a3a5a; }

#status-bar {
  display: flex;
  gap: 12px;
  margin-top: 6px;
  font-size: 7px;
  color: #888;
  letter-spacing: 1px;
  flex-wrap: wrap;
  justify-content: center;
}
#status-bar span { display: inline-block; }
#emu-status { color: #0f0; }
#fps-display { color: #ff0; }
#rom-name { color: #aaa; max-width: 160px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#mapper-info { color: #6cf; }
#bank-info { color: #f90; }

#error-display {
  margin-top: 8px;
  font-size: 7px;
  color: #f44;
  background: #1a0000;
  border: 1px solid #f44;
  padding: 8px 12px;
  max-width: 520px;
  text-align: center;
  white-space: pre-line;
}

#settings-panel {
  position: fixed;
  top: 50%;
  left: 50%;
  transform: translate(-50%, -50%);
  background: #111128;
  border: 2px solid #444;
  padding: 16px;
  z-index: 100;
  min-width: 260px;
}
.settings-title {
  font-size: 10px;
  color: #ff0;
  margin-bottom: 12px;
  text-align: center;
}
.setting-row {
  display: flex;
  justify-content: space-between;
  align-items: center;
  margin-bottom: 8px;
  font-size: 7px;
  color: #ccc;
}

#debug-panel {
  position: fixed;
  top: 10px;
  right: 10px;
  background: #0c0c18;
  border: 1px solid #2a2a4a;
  padding: 0;
  z-index: 90;
  font-size: 7px;
  color: #0f0;
  width: 340px;
  max-height: 85vh;
  overflow: hidden;
  display: flex;
  flex-direction: column;
  box-shadow: 0 4px 24px rgba(0,0,0,0.7), inset 0 0 1px rgba(0,255,0,0.05);
}
#debug-tabs {
  display: flex;
  border-bottom: 1px solid #2a2a4a;
  background: #111128;
  flex-shrink: 0;
}
.debug-tab {
  flex: 1;
  font-family: 'Press Start 2P', monospace;
  font-size: 6px;
  color: #556;
  background: transparent;
  border: none;
  padding: 6px 2px;
  cursor: pointer;
  border-bottom: 2px solid transparent;
  transition: color 0.15s, border-color 0.15s;
}
.debug-tab:hover { color: #8f8; }
.debug-tab.active { color: #0f0; border-bottom-color: #0f0; }
.debug-tab-content { display: none; padding: 8px; overflow-y: auto; flex: 1; }
.debug-tab-content.active { display: block; }
#debug-controls {
  display: flex;
  gap: 4px;
  padding: 6px 8px;
  border-bottom: 1px solid #1a1a3a;
  background: #0e0e20;
  align-items: center;
  flex-shrink: 0;
}
.dbg-btn {
  font-family: 'Press Start 2P', monospace;
  font-size: 6px;
  background: #1a1a3a;
  color: #8f8;
  border: 1px solid #2a2a5a;
  padding: 3px 6px;
  cursor: pointer;
}
.dbg-btn:hover { background: #2a2a4a; color: #0f0; }
.dbg-btn:active { background: #0f0; color: #000; }
.dbg-state { color: #ff0; font-size: 6px; margin-left: auto; }
.dbg-bp-row {
  display: flex;
  align-items: center;
  gap: 4px;
  margin: 4px 0;
  color: #0f0;
}
.dbg-input {
  font-family: 'Press Start 2P', monospace;
  font-size: 7px;
  background: #111;
  color: #0f0;
  border: 1px solid #2a2a4a;
  padding: 2px 4px;
  width: 42px;
  text-transform: uppercase;
}
.dbg-select {
  font-family: 'Press Start 2P', monospace;
  font-size: 6px;
  background: #111;
  color: #0f0;
  border: 1px solid #2a2a4a;
  padding: 2px 4px;
}
.dbg-bp-info { color: #f66; font-size: 6px; margin-left: 4px; }
.debug-title { color: #ff0; margin-bottom: 4px; margin-top: 6px; font-size: 7px; letter-spacing: 1px; }
.debug-title:first-child { margin-top: 0; }
#debug-panel pre {
  margin: 0;
  white-space: pre;
  word-break: break-all;
  line-height: 1.7;
  font-size: 6.5px;
  color: #0d0;
  overflow-x: auto;
}
#debug-panel pre .hl { color: #fff; background: #1a3a1a; }
#debug-panel pre .dim { color: #454; }
#debug-panel pre .acc { color: #ff0; }
#debug-panel pre .brk { color: #f44; background: #2a0a0a; }
#debug-disasm .current-line { color: #fff; background: #0a2a0a; display: inline-block; width: 100%; }
#debug-palette-display {
  display: flex;
  gap: 1px;
  flex-wrap: wrap;
  margin: 4px 0;
}
.pal-swatch {
  width: 12px;
  height: 12px;
  border: 1px solid #333;
}
.pal-swatch:nth-child(4n+1) { margin-left: 4px; }
.pal-swatch:nth-child(1) { margin-left: 0; }
@media (max-width: 600px) {
  #debug-panel { width: 100vw; right: 0; top: auto; bottom: 0; max-height: 50vh; border-radius: 0; }
}

#mobile-controls {
  width: 100%;
  max-width: 520px;
  padding: 10px 15px;
  display: flex;
  justify-content: space-between;
  align-items: center;
  flex-shrink: 0;
}

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

.dpad-btn {
  background: #2a2a3a;
  border: 1px solid #3a3a4a;
  color: #888;
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}
.dpad-btn:active { background: #4a4a5a; color: #fff; }
#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: #333344; cursor: default; }
#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; }

#action-buttons { display: flex; flex-direction: column; align-items: center; gap: 10px; }
#meta-buttons { display: flex; gap: 12px; }
.meta-btn {
  background: #444;
  border: none;
  color: #aaa;
  font-family: 'Press Start 2P', monospace;
  font-size: 6px;
  padding: 6px 10px;
  border-radius: 10px;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
}
.meta-btn:active { background: #666; color: #fff; }
#ab-buttons { display: flex; gap: 16px; }
.action-btn {
  width: 48px;
  height: 48px;
  border-radius: 50%;
  background: #8b1a1a;
  border: 2px solid #6b0e0e;
  color: #ddd;
  font-family: 'Press Start 2P', monospace;
  font-size: 12px;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  touch-action: manipulation;
  box-shadow: 0 3px 0 #500a0a;
}
.action-btn:active { background: #aa2a2a; transform: translateY(2px); box-shadow: 0 1px 0 #500a0a; }

#footer {
  color: #444;
  font-size: 7px;
  padding: 8px;
  text-align: center;
  letter-spacing: 1px;
  flex-shrink: 0;
  margin-top: auto;
}
#footer a { color: #666; text-decoration: none; margin-left: 12px; }
#footer a:hover { color: #999; }

#screen-container.drag-over {
  outline: 3px dashed #0f0;
  outline-offset: -3px;
}

@media (max-width: 600px) {
  #canvas { width: 100vw; height: calc(100vw * 240 / 256); }
  #tv-bezel { border-radius: 0; padding: 4px 4px 20px 4px; }
  #bezel-label { font-size: 5px; margin-top: 4px; }
  #bezel-led { bottom: 6px; right: 10px; width: 6px; height: 6px; }
  #mobile-controls { display: flex !important; }
  #status-bar { font-size: 6px; gap: 8px; }
}