*, *::before, *::after { box-sizing: border-box; margin: 0; padding: 0; }
html { overflow-anchor: none; }

:root {
  --bg-deep: #0d0d1a;
  --bg-panel: #1a1a2e;
  --woodgrain: linear-gradient(90deg, #5c3a1e 0%, #7a4f2e 15%, #6b4226 30%, #8b5e3c 50%, #6b4226 70%, #7a4f2e 85%, #5c3a1e 100%);
  --chrome: linear-gradient(180deg, #c0c0c0, #888, #aaa);
  --atari-red: #e74c3c;
  --atari-orange: #f39c12;
  --green-led: #39ff14;
  --amber-led: #ffbf00;
  --amber-dim: #665200;
  --crt-glow: 0 0 60px rgba(57, 255, 20, 0.15), 0 0 120px rgba(57, 255, 20, 0.05);
  --font-retro: 'Press Start 2P', monospace;
  --timing-vsync: #ff6b35;
  --timing-vblank: #4a9eff;
  --timing-visible: #39ff14;
  --timing-overscan: #b266ff;
  --timing-error: #ff2244;
}

body {
  background: var(--bg-deep);
  color: #e0e0e0;
  font-family: var(--font-retro);
  font-size: 10px;
  min-height: 100vh;
  overflow-x: hidden;
  overscroll-behavior: none;
}

.hidden { display: none !important; }
.hidden-input { position: absolute; opacity: 0; width: 0; height: 0; }

/* HEADER */
#header {
  background: var(--woodgrain);
  padding: 16px 20px;
  text-align: center;
  position: relative;
  border-bottom: 4px solid #3a2210;
  overflow: hidden;
}
.header-inner { position: relative; z-index: 2; }
.atari-logo { display: flex; align-items: center; justify-content: center; gap: 12px; }

.fuji-icon { display: flex; gap: 3px; align-items: flex-end; }
.fuji-icon .stripe { display: block; width: 6px; border-radius: 3px 3px 0 0; }
.fuji-icon .s1 { height: 20px; background: var(--atari-red); }
.fuji-icon .s2 { height: 28px; background: var(--atari-orange); }
.fuji-icon .s3 { height: 20px; background: #e67e22; }

#header h1 {
  font-size: 22px;
  color: #fff;
  text-shadow: 2px 2px 0 #000, 0 0 10px rgba(231,76,60,0.5);
  letter-spacing: 4px;
}
.subtitle { font-size: 7px; color: #ddd; margin-top: 6px; text-shadow: 1px 1px 0 #000; }

.scanline-header {
  position: absolute; top: 0; left: 0; right: 0; bottom: 0;
  background: repeating-linear-gradient(0deg, transparent, transparent 2px, rgba(0,0,0,0.1) 2px, rgba(0,0,0,0.1) 4px);
  pointer-events: none;
  animation: scanmove 8s linear infinite;
}
@keyframes scanmove { 0% { transform: translateY(0); } 100% { transform: translateY(4px); } }

/* MAIN LAYOUT */
#main {
  display: flex;
  gap: 16px;
  padding: 16px;
  max-width: 1200px;
  margin: 0 auto;
  align-items: flex-start;
}

/* TV AREA */
#tv-area { flex: 1; min-width: 0; }

#tv-frame {
  background: #111;
  border-radius: 24px;
  padding: 20px;
  box-shadow: 0 0 40px rgba(0,0,0,0.8), inset 0 0 30px rgba(0,0,0,0.5);
  border: 3px solid #333;
}

#tv-bezel {
  position: relative;
  border-radius: 12px;
  overflow: hidden;
  background: #000;
  box-shadow: var(--crt-glow);
}

#canvas {
  display: block;
  width: 100%;
  aspect-ratio: 160 / 192;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  border-radius: 8px;
}

#scanline-overlay {
  position: absolute; top: 0; left: 0; right: 0; bottom: 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;
  border-radius: 8px;
}

#crt-reflection {
  position: absolute; top: 0; left: 0; right: 0; bottom: 0;
  background: radial-gradient(ellipse at 30% 20%, rgba(255,255,255,0.06) 0%, transparent 60%);
  pointer-events: none;
  border-radius: 8px;
}

/* TIMING LED */
.timing-led {
  position: absolute;
  top: 8px;
  right: 10px;
  font-size: 16px;
  z-index: 10;
  transition: color 0.2s, text-shadow 0.2s;
  pointer-events: none;
}
.timing-led.good { color: #39ff14; text-shadow: 0 0 8px #39ff14, 0 0 16px #39ff14; }
.timing-led.warn { color: #ffbf00; text-shadow: 0 0 8px #ffbf00, 0 0 16px #ffbf00; }
.timing-led.bad { color: #ff2244; text-shadow: 0 0 8px #ff2244, 0 0 16px #ff2244; animation: ledpulse 0.5s ease-in-out 3; }
.timing-led.off { color: #333; text-shadow: none; }
@keyframes ledpulse {
  0%,100% { opacity: 1; }
  50% { opacity: 0.3; }
}

/* EMULATOR BAR */
#emulator-bar {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 10px;
  margin-top: 8px;
  background: #111;
  border-radius: 8px;
  flex-wrap: wrap;
}

.emu-btn {
  font-family: var(--font-retro);
  font-size: 10px;
  padding: 6px 10px;
  background: #2a2a40;
  color: #e0e0e0;
  border: 1px solid #444;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.1s;
  -webkit-tap-highlight-color: transparent;
}
.emu-btn:hover { background: #3a3a55; }
.emu-btn:active { transform: scale(0.95); }
.emu-btn.active, .speed-btn.active { background: var(--atari-red); color: #fff; }

.strict-timing-group { display: flex; align-items: center; gap: 4px; margin-left: auto; }
.strict-label { font-size: 6px; color: var(--amber-led); }
.strict-btn.on { background: var(--amber-led); color: #000; border-color: #cc9900; }
.strict-btn.off { background: #333; color: #666; }

.speed-group, .volume-group { display: flex; align-items: center; gap: 4px; }
.volume-group label { font-size: 14px; }
#volume-slider { width: 60px; accent-color: var(--atari-orange); }

.fps-display {
  font-size: 9px;
  color: var(--green-led);
  background: #0a0a0a;
  padding: 4px 8px;
  border-radius: 3px;
  border: 1px solid #1a3a1a;
  min-width: 70px;
  text-align: center;
  text-shadow: 0 0 6px var(--green-led);
}

/* SIDE PANEL */
#side-panel {
  width: 300px;
  flex-shrink: 0;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.panel {
  background: var(--bg-panel);
  border: 1px solid #2a2a40;
  border-radius: 8px;
  padding: 12px;
}
.panel h3 {
  font-size: 9px;
  color: var(--atari-orange);
  margin-bottom: 10px;
  text-align: center;
  letter-spacing: 2px;
}

/* CARTRIDGE SLOT */
.cart-slot {
  border: 2px dashed #444;
  border-radius: 8px;
  padding: 16px;
  text-align: center;
  position: relative;
  transition: border-color 0.3s;
}
.cart-slot.dragover { border-color: var(--atari-orange); background: rgba(243,156,18,0.05); }

.slot-opening {
  width: 80%;
  height: 6px;
  background: #0a0a0a;
  margin: 0 auto 12px;
  border-radius: 2px;
  box-shadow: inset 0 2px 4px rgba(0,0,0,0.8);
}

.cart-visual {
  width: 60%;
  margin: 0 auto;
  transition: transform 0.5s cubic-bezier(0.34, 1.56, 0.64, 1);
}
.cart-visual.inserting { animation: insertCart 0.6s ease-out forwards; }
.cart-body {
  height: 40px;
  background: var(--woodgrain);
  border-radius: 4px 4px 0 0;
  border: 2px solid #3a2210;
  position: relative;
}
.cart-body::after {
  content: '';
  position: absolute;
  bottom: 0; left: 20%; right: 20%;
  height: 8px;
  background: #222;
  border-radius: 0 0 2px 2px;
}

@keyframes insertCart {
  0% { transform: translateY(-30px); opacity: 0; }
  60% { transform: translateY(4px); opacity: 1; }
  100% { transform: translateY(0); opacity: 1; }
}

.cart-btn {
  font-family: var(--font-retro);
  font-size: 10px;
  padding: 10px 20px;
  background: var(--woodgrain);
  color: #fff;
  border: 2px solid #3a2210;
  border-radius: 6px;
  cursor: pointer;
  text-shadow: 1px 1px 0 #000;
  transition: transform 0.1s;
}
.cart-btn:hover { transform: scale(1.03); }
.cart-btn:active { transform: scale(0.97); }

.drop-hint { font-size: 7px; color: #666; margin-top: 8px; }

.rom-info { margin-top: 10px; }
.info-row {
  display: flex;
  justify-content: space-between;
  font-size: 8px;
  padding: 3px 0;
  border-bottom: 1px solid #222;
}
.info-row span:first-child { color: #888; }
.info-row span:last-child { color: var(--green-led); text-shadow: 0 0 4px var(--green-led); }

.test-rom-btn {
  font-family: var(--font-retro);
  font-size: 8px;
  width: 100%;
  padding: 8px;
  margin-top: 8px;
  background: #1a3a1a;
  color: var(--green-led);
  border: 1px solid #2a5a2a;
  border-radius: 4px;
  cursor: pointer;
}
.test-rom-btn:hover { background: #2a4a2a; }

/* TIMING REFERENCE */
.timing-ref-text {
  font-family: 'Courier New', monospace;
  font-size: 8px;
  color: var(--amber-led);
  text-shadow: 0 0 4px rgba(255,191,0,0.2);
  line-height: 1.6;
  white-space: pre;
}

/* SWITCHES */
.switches-row {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: center;
}
.switch-group {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 4px;
}
.switch-group label { font-size: 6px; color: #888; }

.toggle-switch {
  font-family: var(--font-retro);
  font-size: 7px;
  padding: 4px 8px;
  background: var(--chrome);
  color: #333;
  border: 1px solid #666;
  border-radius: 3px;
  cursor: pointer;
  min-width: 36px;
  transition: all 0.15s;
  -webkit-tap-highlight-color: transparent;
}
.toggle-switch.on { background: #ddd; box-shadow: 0 0 4px rgba(255,255,255,0.3); }

/* JOYSTICK / DPAD */
.joystick-area {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 24px;
}

.dpad {
  display: grid;
  grid-template-columns: 32px 32px 32px;
  grid-template-rows: 32px 32px 32px;
  gap: 2px;
}
.dpad-btn {
  background: #222;
  color: #ccc;
  border: 1px solid #444;
  border-radius: 4px;
  font-size: 12px;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
}
.dpad-btn:active, .dpad-btn.pressed { background: #555; }
.dpad-btn.center { background: #1a1a1a; border-color: #333; cursor: default; }

.fire-btn {
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: radial-gradient(circle at 35% 35%, #ff6b35, #cc4400);
  color: #fff;
  font-family: var(--font-retro);
  font-size: 8px;
  border: 3px solid #aa3300;
  cursor: pointer;
  box-shadow: 0 4px 8px rgba(0,0,0,0.4);
  transition: transform 0.1s;
  user-select: none;
  -webkit-user-select: none;
  touch-action: none;
  -webkit-tap-highlight-color: transparent;
}
.fire-btn:active, .fire-btn.pressed { transform: scale(0.9); box-shadow: 0 2px 4px rgba(0,0,0,0.4); }

.key-map { margin-top: 10px; text-align: center; }
.key-map p { font-size: 6px; color: #666; line-height: 1.6; }

/* ====== TIMING DIAGNOSTICS PANEL ====== */
#timing-toggle-bar { text-align: center; padding: 8px; }
#btn-timing-toggle {
  font-family: var(--font-retro);
  font-size: 8px;
  padding: 6px 16px;
  background: #1a1200;
  color: var(--amber-led);
  border: 1px solid var(--amber-dim);
  border-radius: 4px;
  cursor: pointer;
}
#btn-timing-toggle:hover { background: #2a2000; }

#timing-panel {
  max-width: 1200px;
  margin: 0 auto;
  padding: 12px 16px;
}

.timing-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
}

.timing-section {
  background: #0a0800;
  border: 1px solid var(--amber-dim);
  border-radius: 6px;
  padding: 10px;
}
.timing-section h4 {
  font-size: 7px;
  color: var(--amber-led);
  margin-bottom: 8px;
  letter-spacing: 1px;
}

/* Timing bar chart */
.timing-bar-chart { display: flex; flex-direction: column; gap: 6px; }
.timing-bar-row { display: flex; align-items: center; gap: 6px; }
.bar-label { font-size: 7px; color: #888; width: 50px; text-align: right; }
.bar-track { flex: 1; height: 14px; background: #111; border-radius: 3px; overflow: hidden; border: 1px solid #222; }
.bar-fill { height: 100%; border-radius: 2px; transition: width 0.15s ease; }
.vsync-bar { background: var(--timing-vsync); }
.vblank-bar { background: var(--timing-vblank); }
.visible-bar { background: var(--timing-visible); }
.overscan-bar { background: var(--timing-overscan); }
.total-bar { background: linear-gradient(90deg, var(--timing-vsync), var(--timing-vblank), var(--timing-visible), var(--timing-overscan)); }
.bar-value { font-size: 7px; color: var(--amber-led); width: 50px; text-shadow: 0 0 3px rgba(255,191,0,0.3); }
.total-row { margin-top: 4px; padding-top: 4px; border-top: 1px solid #222; }

/* State machine */
.state-machine {
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 10px;
}
.state-node {
  font-size: 7px;
  padding: 4px 8px;
  background: #111;
  border: 1px solid #333;
  border-radius: 4px;
  color: #555;
  transition: all 0.2s;
}
.state-node.active {
  color: var(--amber-led);
  border-color: var(--amber-led);
  background: rgba(255,191,0,0.1);
  box-shadow: 0 0 8px rgba(255,191,0,0.3);
}

.timing-stats { display: flex; flex-direction: column; gap: 3px; }
.stat-row {
  display: flex;
  justify-content: space-between;
  font-size: 7px;
}
.stat-row span:first-child { color: #666; }
.stat-row span:last-child { color: var(--amber-led); text-shadow: 0 0 3px rgba(255,191,0,0.3); }

/* Timing log */
.timing-log {
  font-family: 'Courier New', monospace;
  font-size: 8px;
  color: var(--amber-led);
  background: #050400;
  border: 1px solid #1a1200;
  border-radius: 4px;
  padding: 6px;
  height: 160px;
  overflow-y: auto;
  line-height: 1.5;
  text-shadow: 0 0 3px rgba(255,191,0,0.2);
}
.timing-log .log-error { color: var(--timing-error); }
.timing-log .log-warn { color: var(--atari-orange); }
.timing-log .log-vsync { color: var(--timing-vsync); }
.timing-log .log-vblank { color: var(--timing-vblank); }

/* Timing warnings */
.timing-warnings {
  font-size: 8px;
  min-height: 60px;
}
.no-warnings { color: var(--green-led); text-shadow: 0 0 4px rgba(57,255,20,0.3); }
.warning-item {
  color: var(--timing-error);
  padding: 4px 6px;
  margin-bottom: 4px;
  background: rgba(255,34,68,0.08);
  border-left: 2px solid var(--timing-error);
  border-radius: 2px;
  animation: warnflash 0.5s ease-out;
}
@keyframes warnflash {
  0% { background: rgba(255,34,68,0.3); }
  100% { background: rgba(255,34,68,0.08); }
}

/* DEBUG PANEL */
#debug-toggle-bar { text-align: center; padding: 8px; }
#btn-debug-toggle {
  font-family: var(--font-retro);
  font-size: 8px;
  padding: 6px 16px;
  background: #001a0a;
  color: var(--green-led);
  border: 1px solid #1a3a1a;
  border-radius: 4px;
  cursor: pointer;
}
#btn-debug-toggle:hover { background: #002a14; }

#debug-panel {
  max-width: 1200px;
  margin: 0 auto;
  padding: 12px 16px;
}

.debug-grid {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(250px, 1fr));
  gap: 12px;
}

.debug-section {
  background: #0a0a0a;
  border: 1px solid #1a3a1a;
  border-radius: 6px;
  padding: 10px;
}
.debug-section h4 {
  font-size: 8px;
  color: var(--green-led);
  margin-bottom: 6px;
}
.debug-section pre {
  font-family: 'Courier New', monospace;
  font-size: 9px;
  color: var(--green-led);
  text-shadow: 0 0 4px rgba(57,255,20,0.3);
  line-height: 1.5;
  white-space: pre-wrap;
  word-break: break-all;
}
.debug-section pre .hl-vsync { color: var(--amber-led); text-shadow: 0 0 4px rgba(255,191,0,0.3); }

.debug-buttons {
  display: flex;
  gap: 8px;
  align-items: center;
  margin-top: 12px;
  flex-wrap: wrap;
}
.vsync-step-btn { color: var(--timing-vsync) !important; border-color: var(--timing-vsync) !important; }
.vblank-step-btn { color: var(--timing-vblank) !important; border-color: var(--timing-vblank) !important; }

.scanline-break-group {
  display: flex;
  align-items: center;
  gap: 4px;
  margin-left: auto;
}
.scanline-break-group label { font-size: 7px; color: #888; }
.scanline-break-group input {
  font-family: var(--font-retro);
  font-size: 8px;
  width: 50px;
  padding: 4px;
  background: #0a0a0a;
  color: var(--green-led);
  border: 1px solid #1a3a1a;
  border-radius: 3px;
  text-align: center;
}

/* FOOTER */
#footer {
  text-align: center;
  padding: 20px;
  border-top: 2px solid #1a1a2e;
  font-size: 7px;
  color: #555;
}
#footer p { margin: 4px 0; }
#footer a { color: var(--atari-orange); text-decoration: none; }
#footer a:hover { text-decoration: underline; }
.disclaimer { font-size: 6px; color: #444; }

/* RESPONSIVE */
@media (max-width: 768px) {
  #main { flex-direction: column; padding: 8px; }
  #side-panel { width: 100%; }
  #header h1 { font-size: 16px; }
  .subtitle { font-size: 6px; }
  #emulator-bar { justify-content: center; }
  .timing-grid { grid-template-columns: 1fr; }
  .debug-grid { grid-template-columns: 1fr; }
  .joystick-area { gap: 32px; }
  .fire-btn { width: 64px; height: 64px; }
  .debug-buttons { flex-direction: column; align-items: stretch; }
  .scanline-break-group { margin-left: 0; }
}