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

:root {
  --bg: #0e0e0e;
  --bg-card: #1a1a1a;
  --atari-orange: #E87000;
  --atari-amber: #FFA630;
  --wood-brown: #8B5E3C;
  --wood-dark: #5C3A1E;
  --wood-light: #A67B5B;
  --text: #d4c8b0;
  --text-dim: #7a7060;
  --crt-green: #33ff66;
  --screen-bg: #0a0a0a;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Courier New', monospace;
  min-height: 100vh;
  overflow-x: hidden;
}

/* ── Header ── */
#header {
  position: relative;
  text-align: center;
  padding: 32px 16px 24px;
  background: linear-gradient(180deg, #1e1510 0%, var(--bg) 100%);
  border-bottom: 3px solid var(--wood-brown);
  overflow: hidden;
}

.header-content { position: relative; z-index: 2; }

.title {
  font-family: 'Press Start 2P', monospace;
  font-size: clamp(14px, 4vw, 28px);
  color: var(--atari-orange);
  text-shadow:
    0 0 10px rgba(232,112,0,0.6),
    0 0 30px rgba(232,112,0,0.2),
    2px 2px 0 #3a2000;
  letter-spacing: 2px;
  margin-bottom: 12px;
}

.subtitle {
  font-size: clamp(10px, 2vw, 14px);
  color: var(--text-dim);
  font-style: italic;
}

.scanline-overlay {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  background: repeating-linear-gradient(
    0deg,
    transparent,
    transparent 2px,
    rgba(0,0,0,0.15) 2px,
    rgba(0,0,0,0.15) 4px
  );
  pointer-events: none;
  z-index: 1;
  animation: scanFlicker 8s ease-in-out infinite;
}

@keyframes scanFlicker {
  0%, 100% { opacity: 0.4; }
  50% { opacity: 0.6; }
  93% { opacity: 0.4; }
  94% { opacity: 0.7; }
  95% { opacity: 0.4; }
}

/* ── Main ── */
#main {
  max-width: 1100px;
  margin: 0 auto;
  padding: 24px 16px;
}

/* ── Upload Zone ── */
.upload-zone {
  border: 4px solid var(--wood-brown);
  border-radius: 8px;
  padding: 6px;
  background:
    repeating-linear-gradient(
      90deg,
      var(--wood-dark) 0px,
      var(--wood-brown) 4px,
      var(--wood-light) 8px,
      var(--wood-brown) 12px,
      var(--wood-dark) 16px
    );
  cursor: pointer;
  transition: all 0.3s;
}

.upload-zone:hover, .upload-zone.dragover {
  border-color: var(--atari-orange);
  box-shadow: 0 0 20px rgba(232,112,0,0.3);
}

.upload-inner {
  border: 3px dashed rgba(255,255,255,0.2);
  border-radius: 4px;
  background: rgba(0,0,0,0.7);
  padding: 48px 24px;
  text-align: center;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 12px;
}

.cartridge-icon {
  font-size: 48px;
  color: var(--atari-orange);
  animation: bob 2s ease-in-out infinite;
}

@keyframes bob {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-6px); }
}

.upload-text {
  font-family: 'Press Start 2P', monospace;
  font-size: clamp(10px, 2.5vw, 16px);
  color: var(--atari-amber);
}

.upload-subtext {
  font-size: 12px;
  color: var(--text-dim);
}

.upload-btn {
  display: inline-block;
  font-family: 'Press Start 2P', monospace;
  font-size: 11px;
  color: #fff;
  background: linear-gradient(180deg, #666 0%, #333 50%, #222 100%);
  border: 3px outset #888;
  border-radius: 4px;
  padding: 12px 24px;
  cursor: pointer;
  text-transform: uppercase;
  transition: all 0.1s;
  margin-top: 8px;
}

.upload-btn:hover { background: linear-gradient(180deg, #777 0%, #444 50%, #333 100%); }
.upload-btn:active {
  border-style: inset;
  transform: translateY(1px);
}

/* ── Preview ── */
.preview-section { margin-top: 24px; }
.preview-section.hidden { display: none; }

.preview-container {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 20px;
  margin-bottom: 24px;
}

@media (max-width: 700px) {
  .preview-container { grid-template-columns: 1fr; }
}

.crt-frame {
  background: #111;
  border-radius: 16px;
  padding: 12px;
  box-shadow:
    inset 0 0 30px rgba(0,0,0,0.8),
    0 4px 20px rgba(0,0,0,0.5),
    0 0 0 3px #2a2a2a;
}

.crt-label {
  font-family: 'Press Start 2P', monospace;
  font-size: 9px;
  color: var(--text-dim);
  text-align: center;
  margin-bottom: 8px;
  letter-spacing: 3px;
}

.crt-screen {
  position: relative;
  background: var(--screen-bg);
  border-radius: 10px;
  overflow: hidden;
  aspect-ratio: 4/3;
  display: flex;
  align-items: center;
  justify-content: center;
  box-shadow: inset 0 0 60px rgba(0,0,0,0.5);
}

.crt-screen canvas {
  max-width: 100%;
  max-height: 100%;
  display: block;
}

.atari-screen canvas {
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

.scanline-layer {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  background: repeating-linear-gradient(
    0deg,
    transparent,
    transparent 1px,
    rgba(0,0,0,0.3) 1px,
    rgba(0,0,0,0.3) 2px
  );
  pointer-events: none;
  mix-blend-mode: multiply;
}

.scanline-layer.off { display: none; }

/* ── Controls ── */
.controls-panel {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(220px, 1fr));
  gap: 16px;
  background: var(--bg-card);
  border: 2px solid #2a2a2a;
  border-radius: 8px;
  padding: 20px;
  margin-bottom: 20px;
}

.control-group label {
  font-family: 'Press Start 2P', monospace;
  font-size: 8px;
  color: var(--atari-amber);
  letter-spacing: 1px;
  display: block;
  margin-bottom: 8px;
}

.slider-row {
  display: flex;
  align-items: center;
  gap: 10px;
}

.ctrl-value {
  font-family: 'Press Start 2P', monospace;
  font-size: 8px;
  color: var(--atari-orange);
  min-width: 60px;
  text-align: right;
}

input[type="range"] {
  -webkit-appearance: none;
  appearance: none;
  flex: 1;
  height: 6px;
  background: #333;
  border-radius: 3px;
  outline: none;
}

input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 18px;
  height: 18px;
  background: var(--atari-orange);
  border: 2px solid #fff;
  border-radius: 2px;
  cursor: pointer;
}

input[type="range"]::-moz-range-thumb {
  width: 18px;
  height: 18px;
  background: var(--atari-orange);
  border: 2px solid #fff;
  border-radius: 2px;
  cursor: pointer;
}

select {
  width: 100%;
  font-family: 'Courier New', monospace;
  font-size: 13px;
  background: #222;
  color: var(--text);
  border: 2px solid #444;
  border-radius: 4px;
  padding: 8px;
  outline: none;
}

select:focus { border-color: var(--atari-orange); }

.toggles-row {
  display: flex;
  gap: 16px;
  flex-wrap: wrap;
  align-items: flex-start;
}

.toggles-row > label:first-child { margin-bottom: 0; }

.toggle-label {
  display: flex !important;
  align-items: center;
  gap: 6px;
  cursor: pointer;
  font-family: 'Courier New', monospace !important;
  font-size: 12px !important;
  color: var(--text) !important;
  letter-spacing: 0 !important;
}

.toggle-label input[type="checkbox"] {
  -webkit-appearance: none;
  appearance: none;
  width: 16px;
  height: 16px;
  border: 2px solid #555;
  border-radius: 2px;
  background: #222;
  cursor: pointer;
  position: relative;
}

.toggle-label input[type="checkbox"]:checked {
  background: var(--atari-orange);
  border-color: var(--atari-amber);
}

.toggle-label input[type="checkbox"]:checked::after {
  content: '';
  position: absolute;
  top: 1px; left: 4px;
  width: 4px; height: 8px;
  border: solid #fff;
  border-width: 0 2px 2px 0;
  transform: rotate(45deg);
}

/* ── Export ── */
.export-bar {
  display: flex;
  gap: 12px;
  flex-wrap: wrap;
  justify-content: center;
}

.action-btn {
  font-family: 'Press Start 2P', monospace;
  font-size: 10px;
  padding: 14px 24px;
  border: 3px outset #888;
  border-radius: 4px;
  cursor: pointer;
  text-transform: uppercase;
  color: #fff;
  background: linear-gradient(180deg, #555 0%, #333 50%, #222 100%);
  transition: all 0.1s;
}

.action-btn:hover { background: linear-gradient(180deg, #666 0%, #444 50%, #333 100%); }
.action-btn:active {
  border-style: inset;
  transform: translateY(1px);
}

.action-btn.secondary {
  background: linear-gradient(180deg, #444 0%, #2a2a2a 50%, #1a1a1a 100%);
}

.action-btn.accent {
  background: linear-gradient(180deg, #c06000 0%, var(--atari-orange) 50%, #a05000 100%);
  border-color: var(--atari-amber);
}

.size-compare {
  text-align: center;
  margin-top: 16px;
  font-family: 'Press Start 2P', monospace;
  font-size: 8px;
  color: var(--text-dim);
  min-height: 20px;
}

/* ── Footer ── */
footer {
  text-align: center;
  padding: 24px 16px;
  border-top: 2px solid #222;
  margin-top: 40px;
  font-size: 12px;
  color: var(--text-dim);
  display: flex;
  gap: 8px;
  justify-content: center;
  align-items: center;
  flex-wrap: wrap;
}

footer a {
  color: var(--atari-orange);
  text-decoration: none;
}

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

.footer-joystick {
  font-family: monospace;
  font-size: 14px;
}

/* ── Utility ── */
.hidden { display: none !important; }