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

:root {
  --phosphor: #33FF33;
  --phosphor-dim: #1a8a1a;
  --screen-bg: #001100;
  --bezel: #C4B59A;
  --bezel-dark: #a89a7e;
  --panel-bg: #1A1A1A;
  --panel-border: #333;
  --keycap: #D4CAB0;
  --keycap-pressed: #b8a88e;
  --keycap-text: #2A2A2A;
  --accent: #FF6600;
  --error: #FF3333;
  --success: #33FF33;
  --body-bg: #0a0a0a;
  --matrix-active: #FF6600;
  --matrix-pressed: #33FF33;
}

body {
  background: var(--body-bg);
  background-image: 
    repeating-linear-gradient(90deg, transparent, transparent 49px, rgba(60,40,20,0.03) 49px, rgba(60,40,20,0.03) 50px),
    repeating-linear-gradient(0deg, transparent, transparent 49px, rgba(60,40,20,0.03) 49px, rgba(60,40,20,0.03) 50px);
  color: #ccc;
  font-family: 'IBM Plex Mono', monospace;
  min-height: 100vh;
  overflow-x: hidden;
}

.app-container {
  max-width: 1200px;
  margin: 0 auto;
  padding: 10px;
}

/* TOP BAR */
.top-bar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 12px 20px;
  background: linear-gradient(180deg, #2a2a2a 0%, #1a1a1a 100%);
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  margin-bottom: 10px;
  flex-wrap: wrap;
  gap: 10px;
}

.top-bar-title {
  font-family: 'VT323', monospace;
  font-size: 28px;
  color: var(--accent);
  text-shadow: 0 0 10px rgba(255,102,0,0.5);
  line-height: 1;
}

.top-bar-subtitle {
  font-family: 'VT323', monospace;
  font-size: 15px;
  color: #888;
}

.top-bar-controls {
  display: flex;
  align-items: center;
  gap: 10px;
}

.power-led {
  width: 10px; height: 10px; border-radius: 50%; border: 1px solid #555;
  transition: all 0.3s;
}
.power-led.off { background: #550000; }
.power-led.on { background: var(--success); box-shadow: 0 0 8px var(--success); }

.hw-button {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 12px; font-weight: 700;
  padding: 6px 14px; border: 2px solid #555;
  border-radius: 4px; cursor: pointer;
  text-transform: uppercase; transition: all 0.1s; letter-spacing: 1px;
}
.hw-button:active { transform: translateY(1px); }
.hw-button:disabled { opacity: 0.4; cursor: not-allowed; }
.hw-button.power { background: #333; color: var(--success); border-color: #555; }
.hw-button.power.on { background: #1a3a1a; border-color: var(--success); box-shadow: 0 0 6px rgba(51,255,51,0.3); }
.hw-button.reset { background: #553333; color: #ff9999; border-color: #884444; }
.hw-button.break-btn { background: #554433; color: #ffaa66; border-color: #886644; }

.screenshot-btn, .paste-btn {
  font-size: 11px; padding: 4px 8px;
  background: #2a2a2a; border: 1px solid #444;
  border-radius: 4px; color: #aaa; cursor: pointer;
}
.screenshot-btn:hover, .paste-btn:hover { border-color: var(--accent); color: var(--accent); }

/* MAIN LAYOUT */
.main-layout {
  display: grid;
  grid-template-columns: 260px 1fr 240px;
  gap: 10px;
  margin-bottom: 10px;
}
@media (max-width: 1024px) {
  .main-layout { grid-template-columns: 1fr; }
}

/* PANELS */
.panel {
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: 8px;
  overflow: hidden;
}
.panel-header {
  display: flex; align-items: center; justify-content: space-between;
  padding: 10px 14px;
  background: linear-gradient(180deg, #2a2a2a, #222);
  border-bottom: 1px solid var(--panel-border);
  cursor: pointer; user-select: none;
}
.panel-header h3 { font-family: 'VT323', monospace; font-size: 18px; color: var(--accent); }
.panel-header .toggle { font-size: 12px; color: #666; }
.panel-body { padding: 12px; }
.panel-body.collapsed { display: none; }

/* ROM UPLOAD */
.rom-zone {
  border: 2px dashed #444; border-radius: 6px; padding: 12px;
  margin-bottom: 10px; text-align: center; transition: all 0.2s; cursor: pointer;
}
.rom-zone:hover { border-color: var(--accent); background: rgba(255,102,0,0.05); }
.rom-zone.loaded { border-color: var(--success); border-style: solid; background: rgba(51,255,51,0.05); }
.rom-zone.error { border-color: var(--error); border-style: solid; background: rgba(255,51,51,0.05); }
.rom-label { font-family: 'VT323', monospace; font-size: 16px; color: #aaa; margin-bottom: 4px; }
.rom-addr { font-size: 11px; color: #666; font-family: 'IBM Plex Mono', monospace; }
.rom-status { font-size: 12px; margin-top: 6px; font-family: 'VT323', monospace; }
.rom-status.loaded { color: var(--success); }
.rom-status.error { color: var(--error); }
.rom-status.empty { color: #666; }

/* MEMORY MAP */
.mem-map {
  display: flex; flex-direction: column; gap: 2px; margin-top: 10px;
  font-size: 10px; font-family: 'IBM Plex Mono', monospace;
}
.mem-block { display: flex; align-items: center; gap: 6px; padding: 2px 4px; border-radius: 2px; }
.mem-block .bar { width: 8px; height: 8px; border-radius: 2px; border: 1px solid #555; }
.mem-block .bar.active { border-color: var(--success); }
.mem-block.ram .bar.active { background: #2266aa; }
.mem-block.rom .bar.active { background: var(--success); }
.mem-block.io .bar.active { background: var(--accent); }
.mem-block.video .bar.active { background: #aa22aa; }

/* BOOT BUTTON */
.boot-button {
  width: 100%; padding: 10px;
  font-family: 'VT323', monospace; font-size: 20px;
  background: linear-gradient(180deg, #2a5a2a, #1a3a1a);
  color: var(--success); border: 2px solid var(--success);
  border-radius: 6px; cursor: pointer; margin-top: 10px;
  text-transform: uppercase; letter-spacing: 2px; transition: all 0.2s;
}
.boot-button:disabled { opacity: 0.3; cursor: not-allowed; border-color: #555; color: #555; background: #1a1a1a; }
.boot-button:not(:disabled):hover { background: linear-gradient(180deg, #3a7a3a, #2a5a2a); box-shadow: 0 0 15px rgba(51,255,51,0.3); }

/* CRT MONITOR */
.monitor-frame {
  background: linear-gradient(145deg, #d4c4a4, #b4a484);
  border-radius: 16px; padding: 20px;
  box-shadow: 0 4px 20px rgba(0,0,0,0.5), inset 0 1px 0 rgba(255,255,255,0.2);
}
.monitor-inner {
  background: #111; border-radius: 12px; padding: 12px; overflow: hidden;
}
.crt-container {
  position: relative; border-radius: 8px; overflow: hidden; background: var(--screen-bg);
}
.crt-canvas {
  display: block; width: 100%;
  image-rendering: pixelated; image-rendering: crisp-edges;
}
.crt-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; border-radius: 8px;
}
.crt-glow {
  position: absolute; top: -2px; left: -2px; right: -2px; bottom: -2px;
  border-radius: 10px; box-shadow: inset 0 0 60px rgba(51,255,51,0.08); pointer-events: none;
}
.monitor-label {
  text-align: center; margin-top: 8px;
  font-family: 'IBM Plex Mono', monospace; font-size: 10px; color: #665;
  letter-spacing: 3px; text-transform: uppercase;
}

/* MATRIX OVERLAY TOGGLE */
.matrix-overlay-toggle {
  font-family: 'IBM Plex Mono', monospace; font-size: 11px; color: #888;
  cursor: pointer; user-select: none; display: flex; align-items: center; gap: 4px;
}
.matrix-overlay-toggle input { accent-color: var(--accent); }

/* VIRTUAL KEYBOARD */
.keyboard-container {
  background: linear-gradient(180deg, #c8b898, #b8a888);
  border-radius: 10px; padding: 14px;
  box-shadow: 0 4px 15px rgba(0,0,0,0.4);
}
.keyboard-row {
  display: flex; justify-content: center; gap: 4px; margin-bottom: 4px;
}
.key {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 11px; font-weight: 700;
  color: var(--keycap-text);
  background: linear-gradient(180deg, #e8dcc8, var(--keycap));
  border: 1px solid #a09080; border-bottom: 3px solid #908070;
  border-radius: 4px; padding: 6px 8px; min-width: 34px;
  text-align: center; cursor: pointer; user-select: none;
  transition: all 0.05s; white-space: nowrap;
  position: relative;
}
.key:active, .key.pressed {
  background: var(--keycap-pressed);
  border-bottom-width: 1px; transform: translateY(2px);
}
.key.wide { min-width: 60px; }
.key.wider { min-width: 80px; }
.key.space { flex: 1; max-width: 400px; }
.key.shift-active {
  background: linear-gradient(180deg, #ffe0a0, #eec880);
  border-color: var(--accent);
}
.key.row-scanned {
  box-shadow: 0 0 6px rgba(255, 102, 0, 0.6), inset 0 0 4px rgba(255,102,0,0.2);
  border-color: var(--accent);
}

/* Key content with matrix overlay */
.key-content {
  display: flex; flex-direction: column; align-items: center; gap: 1px;
}
.key-label { font-size: 10px; line-height: 1; }
.key-matrix-pos {
  font-size: 8px; color: #888; font-weight: 400;
  background: rgba(0,0,0,0.1); border-radius: 2px; padding: 0 3px;
}

/* CASSETTE */
.cassette-transport {
  display: flex; gap: 6px; margin: 10px 0; justify-content: center;
}
.transport-btn {
  font-size: 16px; padding: 6px 12px;
  background: #2a2a2a; border: 1px solid #555;
  border-radius: 4px; color: #aaa; cursor: pointer; transition: all 0.1s;
}
.transport-btn:hover { background: #3a3a3a; color: #fff; }
.transport-btn.active { background: #3a2a1a; color: var(--accent); border-color: var(--accent); }
.transport-btn.record { color: #ff4444; }
.transport-btn.record.active { background: #3a1a1a; border-color: #ff4444; animation: record-pulse 1s infinite; }
@keyframes record-pulse { 0%, 100% { box-shadow: 0 0 5px rgba(255,0,0,0.3); } 50% { box-shadow: 0 0 15px rgba(255,0,0,0.6); } }

.tape-counter {
  font-family: 'VT323', monospace; font-size: 20px; color: var(--accent);
  text-align: center; background: #111; padding: 4px 12px;
  border-radius: 4px; border: 1px solid #333; letter-spacing: 3px;
}
.tape-reels { display: flex; justify-content: center; gap: 30px; margin: 8px 0; }
.tape-reel {
  width: 40px; height: 40px; border: 3px solid #555; border-radius: 50%;
  position: relative; background: #222;
}
.tape-reel::after {
  content: ''; position: absolute; top: 50%; left: 50%;
  width: 12px; height: 12px; background: #444; border-radius: 50%;
  transform: translate(-50%, -50%); border: 2px solid #333;
}
.tape-reel.spinning { animation: spin 1s linear infinite; }
@keyframes spin { to { transform: rotate(360deg); } }

.acia-status { display: grid; grid-template-columns: 1fr 1fr; gap: 4px; font-size: 11px; font-family: 'IBM Plex Mono', monospace; }
.acia-flag { display: flex; align-items: center; gap: 4px; padding: 2px 4px; }
.acia-dot { width: 6px; height: 6px; border-radius: 50%; background: #550000; }
.acia-dot.on { background: var(--success); box-shadow: 0 0 4px var(--success); }

.cassette-upload {
  border: 2px dashed #444; border-radius: 6px; padding: 10px;
  text-align: center; cursor: pointer; transition: all 0.2s; margin-bottom: 8px;
}
.cassette-upload:hover { border-color: var(--accent); }
.cassette-upload.loaded { border-color: var(--success); border-style: solid; }

.volume-control { display: flex; align-items: center; gap: 6px; margin-top: 8px; font-size: 11px; color: #888; }
.volume-control input { flex: 1; accent-color: var(--accent); }

/* ============================================
   KEYBOARD MATRIX MONITOR PANEL
   ============================================ */
.matrix-monitor {
  display: flex; flex-direction: column; gap: 12px;
}

.matrix-registers {
  display: grid; grid-template-columns: 1fr 1fr auto; gap: 10px;
}
@media (max-width: 900px) {
  .matrix-registers { grid-template-columns: 1fr; }
}

.matrix-reg {
  background: #111; border: 1px solid #333; border-radius: 6px; padding: 8px 10px;
}
.matrix-reg-label {
  font-size: 10px; color: #666; text-transform: uppercase; letter-spacing: 1px;
  font-family: 'IBM Plex Mono', monospace; margin-bottom: 4px;
}
.matrix-reg-value {
  font-family: 'VT323', monospace; font-size: 18px; color: var(--phosphor);
}
.matrix-reg-detail {
  font-size: 10px; color: #555; margin-top: 4px; font-family: 'IBM Plex Mono', monospace;
}
.matrix-reg.scan-rate {
  display: flex; flex-direction: column; justify-content: center; align-items: center;
}
.scan-value {
  font-size: 22px !important; color: var(--accent) !important;
}

.matrix-bits {
  font-family: 'VT323', monospace; font-size: 20px; letter-spacing: 2px;
}
.matrix-bit {
  display: inline-block; width: 16px; text-align: center;
  color: #555; transition: all 0.1s;
}
.matrix-bit.active-row {
  color: var(--accent); text-shadow: 0 0 6px var(--accent);
  font-weight: bold;
}
.matrix-bit.key-pressed {
  color: var(--success); text-shadow: 0 0 6px var(--success);
  font-weight: bold;
}

/* 8x8 Matrix Grid */
.matrix-grid-container {
  overflow-x: auto;
}
.matrix-grid {
  border-collapse: collapse; font-family: 'VT323', monospace; font-size: 13px;
  margin: 0 auto;
}
.matrix-grid th, .matrix-grid td {
  border: 1px solid #333; padding: 4px 6px; text-align: center; min-width: 38px;
}
.matrix-grid th {
  background: #222; color: #888; font-size: 11px;
}
.matrix-col-header { color: #6688aa !important; }
.matrix-row-header { background: #222 !important; color: #888; font-size: 11px; }
.matrix-row-header.selected {
  background: rgba(255,102,0,0.2) !important;
  color: var(--accent) !important;
  text-shadow: 0 0 4px var(--accent);
}
.matrix-cell {
  background: #0a0a0a; color: #555; transition: all 0.1s; font-size: 11px;
}
.matrix-cell.row-active {
  background: rgba(255,102,0,0.06);
  border-color: rgba(255,102,0,0.3);
}
.matrix-cell.switch-closed {
  background: rgba(51,255,51,0.15) !important;
  color: var(--success) !important;
  text-shadow: 0 0 4px var(--success);
  font-weight: bold;
}
.matrix-cell.switch-closed.row-active {
  background: rgba(51,255,51,0.25) !important;
  box-shadow: inset 0 0 8px rgba(51,255,51,0.3);
}
.matrix-cell.unused { color: #222; }

.matrix-row-selected td { border-color: rgba(255,102,0,0.4); }

/* Hardware explanation */
.matrix-explanation {
  display: flex; align-items: center; gap: 6px; flex-wrap: wrap;
  font-size: 11px; color: #555; padding: 6px 10px;
  background: #111; border-radius: 4px; border: 1px solid #222;
}
.hw-chip {
  display: inline-block; background: #222; color: var(--accent);
  padding: 2px 6px; border-radius: 3px; font-family: 'VT323', monospace;
  font-size: 12px; border: 1px solid #444;
}

/* WELCOME 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; padding: 20px;
}
.modal-content {
  background: var(--panel-bg); border: 1px solid var(--panel-border);
  border-radius: 12px; padding: 30px; max-width: 520px; width: 100%;
  max-height: 85vh; overflow-y: auto;
}
.modal-content h2 { font-family: 'VT323', monospace; font-size: 28px; color: var(--accent); margin-bottom: 8px; }
.modal-badge {
  display: inline-block; background: rgba(255,102,0,0.15); color: var(--accent);
  padding: 3px 10px; border-radius: 4px; font-size: 11px; font-family: 'VT323', monospace;
  letter-spacing: 1px; margin-bottom: 16px; border: 1px solid rgba(255,102,0,0.3);
}
.modal-content p { font-size: 13px; color: #aaa; margin-bottom: 10px; line-height: 1.6; }
.modal-close {
  padding: 10px 30px; font-family: 'VT323', monospace; font-size: 18px;
  background: var(--accent); color: #000; border: none; border-radius: 6px;
  cursor: pointer; margin-top: 10px;
}

/* DEBUG PANEL */
.debug-grid {
  display: grid; grid-template-columns: repeat(auto-fit, minmax(110px, 1fr));
  gap: 8px; margin-bottom: 10px;
}
.debug-reg {
  background: #111; border: 1px solid #333; border-radius: 4px;
  padding: 6px 8px; text-align: center;
}
.debug-reg-label { font-size: 10px; color: #666; text-transform: uppercase; letter-spacing: 1px; }
.debug-reg-value { font-family: 'VT323', monospace; font-size: 20px; color: var(--phosphor); }

.speed-slider-container { display: flex; align-items: center; gap: 10px; margin: 8px 0; }
.speed-slider { flex: 1; accent-color: var(--accent); }
.speed-label { font-family: 'VT323', monospace; font-size: 16px; color: var(--accent); min-width: 50px; }

.hex-viewer {
  font-family: 'IBM Plex Mono', monospace; font-size: 11px;
  background: #0a0a0a; border: 1px solid #333; border-radius: 4px;
  padding: 8px; max-height: 120px; overflow-y: auto;
  white-space: pre; color: var(--phosphor-dim); line-height: 1.4;
}
.hex-input {
  font-family: 'IBM Plex Mono', monospace; font-size: 12px;
  background: #111; border: 1px solid #444; border-radius: 4px;
  padding: 4px 8px; color: var(--phosphor); width: 80px;
}
.disasm-view {
  font-family: 'IBM Plex Mono', monospace; font-size: 11px;
  background: #0a0a0a; border: 1px solid #333; border-radius: 4px;
  padding: 8px; max-height: 100px; overflow-y: auto; color: #888; line-height: 1.5;
}
.disasm-current { color: var(--phosphor); font-weight: bold; }

/* FOOTER */
.app-footer {
  text-align: center; padding: 16px; font-size: 12px; color: #555;
  border-top: 1px solid #222; margin-top: 10px;
}
.app-footer a { color: var(--accent); text-decoration: none; }
.app-footer a:hover { text-decoration: underline; }

@media (max-width: 768px) {
  .top-bar { flex-direction: column; align-items: flex-start; }
  .key { font-size: 9px; padding: 4px 5px; min-width: 26px; }
  .key.wide { min-width: 45px; }
  .key.wider { min-width: 58px; }
  .monitor-frame { padding: 10px; border-radius: 10px; }
  .matrix-registers { grid-template-columns: 1fr; }
}