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

:root {
  --bg: #0e0e1a;
  --bg2: #151528;
  --nes-red: #E60012;
  --nes-white: #fcfcfc;
  --nes-gray: #7c7c7c;
  --nes-dark: #1a1a2e;
  --nes-gold: #f8b800;
  --text: #bcbcbc;
  --border: #3c3c5a;
}

body {
  font-family: 'Press Start 2P', monospace;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  overflow-x: hidden;
  position: relative;
}

#scanline-bg {
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  pointer-events: none;
  z-index: 0;
  background: repeating-linear-gradient(
    0deg,
    transparent,
    transparent 2px,
    rgba(0,0,0,0.08) 2px,
    rgba(0,0,0,0.08) 4px
  );
  animation: scanmove 8s linear infinite;
}

@keyframes scanmove {
  0% { background-position: 0 0; }
  100% { background-position: 0 100px; }
}

header {
  position: relative;
  text-align: center;
  padding: 32px 16px 24px;
  background: linear-gradient(180deg, #1a0a2e 0%, var(--bg) 100%);
  border-bottom: 4px solid var(--nes-red);
  overflow: hidden;
  z-index: 1;
}

header .header-scanlines {
  position: absolute;
  inset: 0;
  background: repeating-linear-gradient(
    0deg,
    transparent,
    transparent 1px,
    rgba(255,255,255,0.02) 1px,
    rgba(255,255,255,0.02) 2px
  );
  pointer-events: none;
  animation: scanmove 4s linear infinite;
}

header h1 {
  font-size: clamp(20px, 5vw, 40px);
  color: var(--nes-white);
  text-shadow: 
    3px 3px 0 var(--nes-red),
    -1px -1px 0 #000,
    6px 6px 0 rgba(0,0,0,0.4);
  letter-spacing: 2px;
  position: relative;
  z-index: 1;
}

.subtitle {
  font-size: clamp(7px, 1.5vw, 10px);
  color: var(--nes-gray);
  margin-top: 12px;
  position: relative;
  z-index: 1;
}

main {
  max-width: 1100px;
  margin: 0 auto;
  padding: 24px 16px;
  position: relative;
  z-index: 1;
}

/* Upload Zone */
.upload-zone {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 20px;
  padding: 40px 20px;
  border: 4px dashed var(--border);
  background: var(--bg2);
  transition: border-color 0.2s, background 0.2s;
  cursor: pointer;
}

.upload-zone.dragover {
  border-color: var(--nes-red);
  background: rgba(230,0,18,0.06);
}

.cartridge-slot {
  width: 240px;
  height: 100px;
  background: #222;
  border: 4px solid #444;
  display: flex;
  align-items: center;
  justify-content: center;
  position: relative;
}

.cartridge-slot::before {
  content: '';
  position: absolute;
  top: -12px;
  left: 30px;
  right: 30px;
  height: 12px;
  background: #333;
  border: 3px solid #444;
  border-bottom: none;
}

.slot-inner {
  text-align: center;
}

.slot-label {
  font-size: 9px;
  color: var(--nes-white);
  letter-spacing: 1px;
}

.slot-sublabel {
  font-size: 7px;
  color: var(--nes-gray);
  margin-top: 8px;
}

.load-btn {
  font-family: 'Press Start 2P', monospace;
  font-size: 11px;
  padding: 12px 28px;
  background: var(--nes-red);
  color: var(--nes-white);
  border: none;
  cursor: pointer;
  letter-spacing: 1px;
  transition: transform 0.1s, box-shadow 0.1s;
  box-shadow: 4px 4px 0 #000;
}

.load-btn:hover {
  transform: translate(2px, 2px);
  box-shadow: 2px 2px 0 #000;
}

.load-btn:active {
  transform: translate(4px, 4px);
  box-shadow: none;
}

/* Controls */
.controls-panel {
  margin-top: 24px;
  background: var(--bg2);
  border: 3px solid var(--border);
  padding: 16px;
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

@media (max-width: 640px) {
  .controls-panel {
    grid-template-columns: 1fr;
  }
}

.control-row {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.control-row label {
  font-size: 7px;
  color: var(--nes-gold);
  letter-spacing: 1px;
}

.control-inner {
  display: flex;
  align-items: center;
  gap: 8px;
}

.val-display {
  font-size: 8px;
  color: var(--nes-white);
  min-width: 52px;
  text-align: right;
}

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  flex: 1;
  height: 8px;
  background: #222;
  border: 2px solid var(--border);
  outline: none;
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 16px;
  height: 16px;
  background: var(--nes-red);
  border: 2px solid var(--nes-white);
  cursor: pointer;
}

input[type="range"]::-moz-range-thumb {
  width: 16px;
  height: 16px;
  background: var(--nes-red);
  border: 2px solid var(--nes-white);
  cursor: pointer;
  border-radius: 0;
}

select {
  font-family: 'Press Start 2P', monospace;
  font-size: 8px;
  padding: 6px 8px;
  background: #222;
  color: var(--text);
  border: 2px solid var(--border);
  outline: none;
  flex: 1;
}

select:focus {
  border-color: var(--nes-gold);
}

.toggle-btn {
  font-family: 'Press Start 2P', monospace;
  font-size: 8px;
  padding: 6px 16px;
  border: 2px solid var(--border);
  cursor: pointer;
  letter-spacing: 1px;
  transition: all 0.15s;
}

.toggle-btn.off {
  background: #222;
  color: var(--nes-gray);
}

.toggle-btn.on {
  background: var(--nes-red);
  color: var(--nes-white);
  border-color: var(--nes-red);
}

/* Canvas Area */
.canvas-area {
  margin-top: 24px;
  display: flex;
  gap: 20px;
  justify-content: center;
  flex-wrap: wrap;
}

.canvas-wrapper {
  flex: 1;
  min-width: 280px;
  max-width: 520px;
}

.tv-label {
  font-size: 8px;
  color: var(--nes-gold);
  text-align: center;
  margin-bottom: 8px;
  letter-spacing: 2px;
}

.tv-bezel {
  background: #111;
  border: 6px solid #2a2a2a;
  box-shadow: 
    inset 0 0 30px rgba(0,0,0,0.8),
    0 0 20px rgba(0,0,0,0.5);
  padding: 8px;
  position: relative;
  overflow: hidden;
}

.tv-bezel.crt {
  border-radius: 8px;
  box-shadow: 
    inset 0 0 60px rgba(0,0,0,0.6),
    0 0 30px rgba(230,0,18,0.1),
    0 0 80px rgba(230,0,18,0.05);
}

.tv-bezel canvas {
  width: 100%;
  height: auto;
  display: block;
  image-rendering: pixelated;
  image-rendering: -moz-crisp-edges;
  image-rendering: crisp-edges;
}

.crt-scanlines {
  position: absolute;
  inset: 0;
  background: repeating-linear-gradient(
    0deg,
    transparent,
    transparent 1px,
    rgba(0,0,0,0.25) 1px,
    rgba(0,0,0,0.25) 2px
  );
  pointer-events: none;
  z-index: 2;
}

/* Palette display */
.palette-display {
  margin-top: 20px;
  text-align: center;
}

.palette-title {
  font-size: 8px;
  color: var(--nes-gray);
  margin-bottom: 10px;
  letter-spacing: 1px;
}

.palette-swatches {
  display: flex;
  flex-wrap: wrap;
  gap: 3px;
  justify-content: center;
}

.palette-swatches .swatch {
  width: 16px;
  height: 16px;
  border: 1px solid rgba(255,255,255,0.2);
}

/* Export */
.export-panel {
  margin-top: 20px;
  background: var(--bg2);
  border: 3px solid var(--border);
  padding: 16px;
  display: flex;
  align-items: flex-end;
  gap: 16px;
  flex-wrap: wrap;
}

.save-btn {
  font-family: 'Press Start 2P', monospace;
  font-size: 11px;
  padding: 12px 24px;
  background: #28a428;
  color: var(--nes-white);
  border: none;
  cursor: pointer;
  letter-spacing: 1px;
  box-shadow: 4px 4px 0 #000;
  transition: transform 0.1s, box-shadow 0.1s;
  white-space: nowrap;
}

.save-btn:hover {
  transform: translate(2px, 2px);
  box-shadow: 2px 2px 0 #000;
}

.save-btn:active {
  transform: translate(4px, 4px);
  box-shadow: none;
}

/* Loading overlay */
.loading-overlay {
  position: fixed;
  inset: 0;
  background: rgba(0,0,0,0.92);
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

.loading-text {
  text-align: center;
}

.loading-text .blink {
  font-size: 12px;
  color: var(--nes-white);
  animation: blinker 0.6s step-end infinite;
}

@keyframes blinker {
  50% { opacity: 0; }
}

.loading-bar {
  width: 260px;
  height: 16px;
  border: 3px solid var(--nes-white);
  margin-top: 20px;
  background: #111;
}

.loading-fill {
  height: 100%;
  width: 0%;
  background: var(--nes-red);
  transition: width 0.1s linear;
}

/* Easter egg */
.easter-egg {
  position: fixed;
  inset: 0;
  background: #000;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 101;
  cursor: pointer;
}

.easter-egg p {
  font-size: clamp(10px, 3vw, 16px);
  color: var(--nes-white);
  text-align: center;
  line-height: 2.5;
  animation: blinker 1.2s step-end infinite;
}

/* Footer */
footer {
  text-align: center;
  padding: 32px 16px;
  border-top: 4px solid var(--border);
  margin-top: 40px;
  position: relative;
  z-index: 1;
}

.footer-pixels {
  display: flex;
  justify-content: center;
  gap: 6px;
  margin-bottom: 16px;
}

.px {
  width: 10px;
  height: 10px;
  display: inline-block;
}
.p1 { background: #E60012; }
.p2 { background: #f8b800; }
.p3 { background: #28a428; }
.p4 { background: #0058f8; }
.p5 { background: #a800b0; }
.p6 { background: #00a8a8; }
.p7 { background: #f87858; }
.p8 { background: #fcfcfc; }

footer p {
  font-size: 7px;
  color: var(--nes-gray);
  margin-top: 8px;
  line-height: 2;
}

footer a {
  color: var(--nes-red);
  text-decoration: none;
}

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

.hidden {
  display: none !important;
}

/* Responsive */
@media (max-width: 700px) {
  .canvas-area {
    flex-direction: column;
    align-items: center;
  }
  .canvas-wrapper {
    max-width: 100%;
  }
  .export-panel {
    flex-direction: column;
    align-items: stretch;
  }
}