*, *::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;
  --amber-dim: #886600;
  --red: #ff3333;
  --blue: #66aaff;
}

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: 8px 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-subtitle { font-family: 'VT323', monospace; font-size: 11px; color: var(--amber); letter-spacing: 1px; opacity: 0.7; margin-left: 4px; }
.header-right { display: flex; align-items: center; gap: 12px; font-size: 11px; }
.speed-label { color: #666; }
.header-cycles { font-family: 'VT323', monospace; font-size: 12px; color: var(--amber); letter-spacing: 1px; }

/* 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; }
.status-dot.bp-hit { background: var(--amber); box-shadow: 0 0 8px var(--amber); animation: pulse-amber 1s infinite; }

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

/* 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: 260px; padding: 14px; 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-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, #bp-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, #bp-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-xs { padding: 2px 6px; font-size: 9px; }

.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; }

.btn-debug-toggle {
  background: #1a1a0a !important; color: var(--amber) !important;
  border-color: var(--amber-dim) !important;
}
.btn-debug-toggle:hover { background: #2a2a1a !important; }

.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;
  padding: 12px; overflow: auto; min-width: 0;
}

.center-and-debug {
  display: flex; gap: 12px; flex: 1; min-height: 0;
  align-items: flex-start;
}

.center-column {
  flex: 1; display: flex; flex-direction: column; align-items: center;
  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: 10px;
}

@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: 8px; 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: var(--blue); }

/* =======================
   CPU DEBUG PANEL (Right)
   ======================= */
.cpu-debug {
  width: 340px; flex-shrink: 0;
  background: #080808;
  border: 1px solid var(--phosphor-dim);
  border-radius: 8px;
  display: flex; flex-direction: column;
  max-height: calc(100vh - 80px);
  overflow: hidden;
  box-shadow: 0 0 30px rgba(51, 255, 51, 0.04), inset 0 0 60px rgba(0,0,0,0.6);
}
.debug-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 8px 12px; background: #0c0c0c; border-bottom: 1px solid #1a2a1a;
  flex-shrink: 0;
}
.debug-title {
  font-family: 'VT323', monospace; font-size: 14px; color: var(--phosphor);
  letter-spacing: 3px;
}
.debug-body {
  overflow-y: auto; padding: 8px;
  flex: 1;
}
.debug-body::-webkit-scrollbar { width: 6px; }
.debug-body::-webkit-scrollbar-track { background: #0a0a0a; }
.debug-body::-webkit-scrollbar-thumb { background: #1a3a1a; border-radius: 3px; }

.debug-section {
  background: #0e0e0e; border: 1px solid #1a1a1a; border-radius: 6px;
  padding: 8px 10px; margin-bottom: 6px;
}
.debug-section-title {
  font-family: 'VT323', monospace; font-size: 10px;
  color: #555; letter-spacing: 2px; margin-bottom: 6px;
  text-transform: uppercase;
  display: flex; align-items: center; justify-content: space-between;
}
.debug-log-controls { display: flex; gap: 6px; align-items: center; }
.debug-checkbox {
  font-size: 9px; color: #666; display: flex; align-items: center; gap: 3px;
  cursor: pointer;
}
.debug-checkbox input { accent-color: var(--phosphor); }

.debug-reg-grid {
  display: grid; grid-template-columns: 1fr 1fr;
  gap: 4px;
}
.debug-reg {
  display: flex; align-items: center; gap: 4px;
}
.debug-stat {
  display: flex; align-items: center; gap: 4px;
}
.debug-label {
  font-family: 'VT323', monospace; font-size: 11px; color: #555;
  min-width: 18px;
}
.debug-val {
  font-family: 'VT323', monospace; font-size: 14px; color: var(--phosphor);
  background: #060606; padding: 1px 5px; border-radius: 3px;
  border: 1px solid #151515; min-width: 38px; text-align: center;
}
.debug-val-pc { color: var(--amber); font-size: 15px; }
.debug-val-wide { min-width: 60px; font-size: 12px; }
.debug-val-big { font-size: 16px; color: var(--amber); }
.debug-dec { font-family: 'VT323', monospace; font-size: 10px; color: #444; }

.debug-cycles-grid {
  display: grid; grid-template-columns: 1fr 1fr;
  gap: 4px;
}

/* Flags */
.debug-flags {
  display: flex; gap: 3px; justify-content: center;
}
.flag-col {
  display: flex; flex-direction: column; align-items: center; gap: 2px;
}
.flag-bit {
  font-family: 'VT323', monospace; font-size: 14px;
  width: 26px; height: 26px; display: flex; align-items: center;
  justify-content: center; border-radius: 4px;
  background: #111; color: #333; border: 1px solid #1a1a1a;
  transition: all 0.15s;
}
.flag-bit.active {
  background: #0a2a0a; color: var(--phosphor);
  border-color: var(--phosphor-dim);
  box-shadow: 0 0 8px rgba(51, 255, 51, 0.2);
}
.flag-bit.flag-unused { color: #222; opacity: 0.5; }
.flag-val {
  font-family: 'VT323', monospace; font-size: 11px; color: #333;
}
.flag-val.active { color: var(--phosphor); }

/* Current instruction */
.debug-instr {
  font-family: 'VT323', monospace; font-size: 13px;
  color: var(--amber); background: #060606;
  padding: 6px 8px; border-radius: 4px;
  border: 1px solid #1a1a0a; letter-spacing: 0.5px;
  word-break: break-all;
}

/* Execution log */
.debug-exec-log {
  font-family: 'VT323', monospace; font-size: 11px;
  color: var(--phosphor-dim); background: #050505;
  padding: 4px 6px; border-radius: 4px;
  border: 1px solid #151515;
  max-height: 200px; overflow-y: auto;
  line-height: 1.4;
}
.debug-exec-log::-webkit-scrollbar { width: 5px; }
.debug-exec-log::-webkit-scrollbar-track { background: #0a0a0a; }
.debug-exec-log::-webkit-scrollbar-thumb { background: #1a2a1a; border-radius: 3px; }

.log-line {
  white-space: nowrap; padding: 1px 0;
  border-bottom: 1px solid #0a0a0a;
}
.log-line.log-bp {
  color: var(--amber) !important;
  background: rgba(255, 170, 0, 0.06);
}
.log-line.log-brk {
  color: var(--red) !important;
  background: rgba(255, 50, 50, 0.06);
}

/* Breakpoints */
.debug-bp-input {
  display: flex; gap: 4px; align-items: center; margin-bottom: 6px;
}
.debug-bp-list { display: flex; flex-direction: column; gap: 3px; }
.bp-entry {
  display: flex; align-items: center; justify-content: space-between;
  padding: 3px 6px; background: #111; border-radius: 3px;
  border: 1px solid #1a1a0a;
}
.bp-addr {
  font-family: 'VT323', monospace; font-size: 13px; color: var(--amber);
}
.bp-remove { color: #ff6666 !important; border-color: #441111 !important; }

/* Stack */
.debug-stack {
  font-family: 'VT323', monospace; font-size: 12px;
  color: var(--phosphor-dim); background: #060606;
  padding: 6px 8px; border-radius: 4px;
  border: 1px solid #151515; line-height: 1.5;
  letter-spacing: 0.5px; word-break: break-all;
}

/* 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;
  max-width: 580px; width: 100%;
}
.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: 900px) {
  .center-and-debug { flex-direction: column; }
  .cpu-debug { width: 100% !important; max-height: 400px; }
}
@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; }
  .cpu-debug { width: 100% !important; }
}
@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; }
  .header-subtitle { display: none; }
  .header-cycles { display: none; }
}