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

:root {
  --phosphor: #33ff33;
  --phosphor-dim: #1a8c1a;
  --phosphor-glow: #33ff3366;
  --bg-dark: #0a0a0a;
  --bg-panel: #111;
  --bg-panel-border: #222;
  --keycap: #f0ead6;
  --keycap-dark: #d4cdb8;
  --keycap-text: #333;
  --keycap-shadow: #8a8475;
  --amber: #ffaa00;
  --red: #ff3333;
}

html, body {
  background: var(--bg-dark);
  color: #ccc;
  font-family: 'IBM Plex Mono', monospace;
  height: 100%;
  overflow-x: hidden;
}

#app { min-height: 100vh; display: flex; flex-direction: column; }

/* Header */
#header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 16px; border-bottom: 1px solid #1a1a1a; background: #0d0d0d;
  flex-shrink: 0;
}
.header-left { display: flex; align-items: center; gap: 10px; }
.apple-icon { font-size: 20px; }
.header-title { font-family: 'VT323', monospace; font-size: 22px; color: var(--phosphor); letter-spacing: 2px; }
.header-right { display: flex; align-items: center; gap: 14px; font-size: 11px; }
.speed-label { color: #666; }

/* Status dots */
.status-dot {
  display: inline-block; width: 8px; height: 8px;
  border-radius: 50%; margin-right: 6px; vertical-align: middle;
}
.status-dot.running { background: var(--phosphor); box-shadow: 0 0 6px var(--phosphor); }
.status-dot.halted { background: var(--red); box-shadow: 0 0 6px var(--red); }
.status-dot.idle { background: #666; }

/* Main layout */
#main-layout { display: flex; flex: 1; min-height: 0; }

/* ROM Panel */
.rom-panel {
  width: 0; overflow: hidden; padding: 0;
  border-right: 1px solid #1a1a1a; background: #0d0d0d;
  flex-shrink: 0; transition: width 0.3s, padding 0.3s;
}
.rom-panel.open { width: 280px; padding: 16px; overflow-y: auto; }
.panel-header { font-family: 'VT323', monospace; font-size: 16px; color: var(--phosphor); margin-bottom: 12px; }

/* Upload slots */
.upload-slot {
  display: flex; align-items: center; gap: 10px;
  padding: 10px; background: #1a1a1a; border: 1px dashed #333;
  border-radius: 6px; margin-bottom: 8px; cursor: pointer;
  transition: border-color 0.2s;
}
.upload-slot:hover { border-color: #555; }
.upload-slot.loaded { border-color: var(--phosphor-dim); border-style: solid; }
.upload-slot input[type="file"] { display: none; }
.slot-info { flex: 1; }
.slot-label { font-size: 12px; font-weight: 600; color: #ccc; }
.slot-desc { font-size: 10px; color: #777; margin-top: 2px; }
.slot-status { font-size: 10px; color: var(--phosphor); margin-top: 4px; }
.slot-icon { font-size: 20px; color: #444; }
.slot-icon.loaded { color: var(--phosphor); }

/* Load program */
.load-program-section { margin-top: 16px; }
.section-label { font-size: 11px; color: #888; margin-bottom: 6px; }
.addr-row { display: flex; gap: 6px; align-items: center; margin-bottom: 8px; }
.dollar { font-size: 11px; color: #666; }
#load-addr {
  background: #1a1a1a; border: 1px solid #333; border-radius: 4px;
  color: var(--phosphor); font-family: 'VT323', monospace; font-size: 14px;
  padding: 4px 8px; width: 60px; outline: none;
}
#load-addr:focus { border-color: var(--phosphor-dim); }

/* Buttons */
.btn {
  font-family: 'IBM Plex Mono', monospace; font-size: 12px; font-weight: 600;
  padding: 8px 16px; border: 1px solid #444; border-radius: 4px;
  cursor: pointer; transition: all 0.15s; background: #222; color: #ccc;
  outline: none;
}
.btn:hover { background: #333; border-color: #666; }
.btn:disabled { opacity: 0.4; cursor: not-allowed; }
.btn-sm { padding: 4px 10px; font-size: 10px; }

.btn-boot {
  width: 100%; margin-top: 16px;
  background: linear-gradient(180deg, #2a6a2a 0%, #1a4a1a 100%);
  color: var(--phosphor); border-color: var(--phosphor-dim);
  font-size: 14px; padding: 10px 24px; text-transform: uppercase; letter-spacing: 2px;
}
.btn-boot:hover:not(:disabled) {
  background: linear-gradient(180deg, #3a8a3a 0%, #2a6a2a 100%);
  box-shadow: 0 0 12px var(--phosphor-glow);
}
.btn-danger { border-color: #662222; color: #ff6666; }
.btn-danger:hover { background: #331111; }

/* State section */
.state-section { margin-top: 16px; border-top: 1px solid #222; padding-top: 12px; }
.state-btns { display: flex; gap: 6px; }
.state-btns .btn { flex: 1; }

/* Center content */
#center-content {
  flex: 1; display: flex; flex-direction: column; align-items: center;
  padding: 16px 12px; overflow: auto; min-width: 0;
}

/* CRT Screen */
.crt-screen {
  position: relative; background: #000; border: 3px solid #222;
  border-radius: 12px; overflow: hidden;
  box-shadow: 0 0 40px rgba(51, 255, 51, 0.08), inset 0 0 80px rgba(0,0,0,0.6);
  width: 100%; max-width: 580px; aspect-ratio: 580 / 480;
  margin-bottom: 12px;
}

@keyframes flicker {
  0% { opacity: 1; } 3% { opacity: 0.97; } 6% { opacity: 1; }
  50% { opacity: 0.99; } 53% { opacity: 0.96; } 56% { opacity: 1; }
}
.crt-screen { animation: flicker 8s infinite; }

.scanline-overlay {
  position: absolute; top: 0; left: 0; right: 0; bottom: 0;
  background: repeating-linear-gradient(0deg, rgba(0,0,0,0.15) 0px, rgba(0,0,0,0.15) 1px, transparent 1px, transparent 3px);
  pointer-events: none; z-index: 10;
}
.vignette-overlay {
  position: absolute; top: 0; left: 0; right: 0; bottom: 0;
  background: radial-gradient(ellipse at center, transparent 60%, rgba(0,0,0,0.4) 100%);
  pointer-events: none; z-index: 11;
}

.terminal-canvas {
  display: block; image-rendering: pixelated; width: 100%; height: 100%;
}

/* CPU Bar */
.cpu-bar {
  display: flex; gap: 12px; margin-bottom: 10px; flex-wrap: wrap;
  justify-content: center; font-family: 'VT323', monospace;
  font-size: 13px; color: var(--phosphor-dim);
}
.cpu-bar-sep { color: #333; }
.reg-cycles { color: var(--amber); }
.reg-mhz { color: #66aaff; }

/* CPU Debug Panel */
.cpu-debug {
  width: 100%; max-width: 580px; margin-bottom: 10px;
  background: #0c0c0c; border: 1px solid var(--phosphor-dim);
  border-radius: 8px; overflow: hidden;
  box-shadow: 0 0 20px rgba(51, 255, 51, 0.05), inset 0 0 40px rgba(0,0,0,0.5);
}
.debug-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 12px; background: #111; border-bottom: 1px solid #222;
}
.debug-title {
  font-family: 'VT323', monospace; font-size: 14px; color: var(--phosphor);
  letter-spacing: 3px;
}
.debug-grid {
  display: grid; grid-template-columns: 1fr 1fr;
  gap: 1px; padding: 10px; background: transparent;
}
.debug-section {
  background: #111; border: 1px solid #1a1a1a; border-radius: 6px;
  padding: 10px; margin: 2px;
}
.debug-section-wide { grid-column: 1 / -1; }
.debug-section-title {
  font-family: 'VT323', monospace; font-size: 10px;
  color: #555; letter-spacing: 2px; margin-bottom: 8px;
  text-transform: uppercase;
}
.debug-reg-grid {
  display: grid; grid-template-columns: 1fr 1fr 1fr;
  gap: 4px;
}
.debug-reg, .debug-stat {
  display: flex; align-items: center; gap: 6px;
}
.debug-label {
  font-family: 'VT323', monospace; font-size: 12px; color: #666;
  min-width: 22px;
}
.debug-val {
  font-family: 'VT323', monospace; font-size: 14px; color: var(--phosphor);
  background: #0a0a0a; padding: 2px 6px; border-radius: 3px;
  border: 1px solid #1a1a1a; min-width: 42px; text-align: center;
}
.debug-val-wide { min-width: 70px; font-size: 13px; }
.debug-cycles-grid {
  display: grid; grid-template-columns: 1fr 1fr;
  gap: 6px;
}
.debug-flags {
  display: flex; gap: 4px; justify-content: center;
}
.flag-bit {
  font-family: 'VT323', monospace; font-size: 16px;
  width: 24px; height: 24px; display: flex; align-items: center;
  justify-content: center; border-radius: 4px;
  background: #1a1a1a; color: #444; border: 1px solid #222;
  transition: all 0.15s;
}
.flag-bit.active {
  background: var(--phosphor-dim); color: #000;
  border-color: var(--phosphor);
  box-shadow: 0 0 8px rgba(51, 255, 51, 0.3);
  font-weight: bold;
}
.flag-bit.flag-unused { color: #333; opacity: 0.5; }
.debug-instr {
  font-family: 'VT323', monospace; font-size: 15px;
  color: var(--amber); background: #0a0a0a;
  padding: 6px 10px; border-radius: 4px;
  border: 1px solid #1a1a1a; letter-spacing: 1px;
}
.debug-stack {
  font-family: 'VT323', monospace; font-size: 13px;
  color: var(--phosphor-dim); background: #0a0a0a;
  padding: 6px 10px; border-radius: 4px;
  border: 1px solid #1a1a1a; line-height: 1.5;
  letter-spacing: 1px;
}

/* Keyboard */
.keyboard {
  width: 100%; max-width: 580px; background: #1a1a1a;
  border: 1px solid #222; border-radius: 10px; padding: 10px 8px;
}
.kb-row { display: flex; justify-content: center; gap: 3px; margin-bottom: 3px; }
.kb-row:last-child { margin-bottom: 0; }

.key-btn {
  font-family: 'IBM Plex Mono', monospace; font-size: 11px; font-weight: 600;
  background: linear-gradient(180deg, var(--keycap) 0%, var(--keycap-dark) 100%);
  color: var(--keycap-text); border: 1px solid #b8b2a0; border-radius: 5px;
  padding: 6px 4px; min-width: 32px; min-height: 34px; cursor: pointer;
  box-shadow: 0 3px 0 var(--keycap-shadow), 0 4px 8px rgba(0,0,0,0.4);
  transition: all 0.05s ease; user-select: none; -webkit-user-select: none;
  text-align: center; line-height: 1.1; outline: none;
}
.key-btn:hover { background: linear-gradient(180deg, #f7f2e2 0%, #e0d9c4 100%); }
.key-btn:active { transform: translateY(2px); box-shadow: 0 1px 0 var(--keycap-shadow), 0 2px 4px rgba(0,0,0,0.3); }
.key-btn.locked {
  background: linear-gradient(180deg, #ffe4a0 0%, #ddc070 100%);
  border-color: var(--amber);
  box-shadow: 0 3px 0 #a08030, 0 4px 8px rgba(0,0,0,0.4), 0 0 8px rgba(255,170,0,0.3);
}
.key-btn.wide { min-width: 70px; font-size: 9px; }
.key-btn.space { flex: 1; min-width: 180px; }
.key-btn.cr-key { min-width: 56px; background: linear-gradient(180deg, #ffe8d0 0%, #e8c8a0 100%); border-color: #c0a070; }

/* Control bar */
.control-bar {
  display: flex; gap: 8px; margin-top: 10px; flex-wrap: wrap;
  justify-content: center; align-items: center;
}
.speed-control { display: flex; align-items: center; gap: 6px; }
.speed-min, .speed-max { font-size: 10px; color: #666; }
#speed-slider { width: 80px; accent-color: var(--phosphor); }

/* Memory Inspector */
.mem-inspector {
  margin-top: 10px; width: 100%; max-width: 580px;
  background: #141414; border: 1px solid #222; border-radius: 8px; padding: 16px;
}
.mem-header { display: flex; align-items: center; gap: 10px; margin-bottom: 8px; }
#mem-region {
  background: #1a1a1a; border: 1px solid #333; border-radius: 4px;
  color: var(--phosphor); font-family: 'VT323', monospace; font-size: 13px;
  padding: 3px 8px; outline: none;
}
.mem-dump {
  font-family: 'VT323', monospace; font-size: 14px; color: var(--phosphor-dim);
  line-height: 1.4; max-height: 200px; overflow-y: auto; margin: 0;
}

/* Modal */
.modal-overlay {
  position: fixed; top: 0; left: 0; right: 0; bottom: 0;
  background: rgba(0,0,0,0.85); display: flex;
  align-items: center; justify-content: center; z-index: 1000;
}
.modal-content {
  background: #141414; border: 1px solid #333; border-radius: 12px;
  padding: 24px; max-width: 500px; width: 90%; max-height: 80vh;
  overflow-y: auto; color: #ccc;
}
.modal-content h2 { color: var(--phosphor); margin-bottom: 12px; font-family: 'VT323', monospace; font-size: 24px; }
.modal-content code { color: var(--phosphor); background: #1a1a1a; padding: 2px 6px; border-radius: 3px; font-size: 13px; }
.modal-content p { margin-bottom: 10px; font-size: 13px; line-height: 1.5; }
.modal-content hr { border-color: #333; margin: 12px 0; }

/* Footer */
.footer {
  text-align: center; padding: 16px; font-size: 11px; color: #555;
  border-top: 1px solid #1a1a1a; margin-top: 20px; flex-shrink: 0;
}
.footer a { color: var(--phosphor-dim); text-decoration: none; }
.footer a:hover { color: var(--phosphor); }
.footer-sep { margin: 0 8px; color: #333; }

/* Responsive */
@media (max-width: 768px) {
  .rom-panel.open { width: 240px; padding: 12px; }
  .key-btn { font-size: 9px; min-width: 26px; min-height: 28px; padding: 4px 2px; }
  .key-btn.wide { min-width: 50px; }
  .key-btn.space { min-width: 100px; }
  #center-content { padding: 8px 6px; }
}
@media (max-width: 500px) {
  .rom-panel.open { position: fixed; top: 0; left: 0; bottom: 0; z-index: 100; width: 260px; }
  .header-title { font-size: 16px; }
}