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

:root {
  --bg: #0d0d1a;
  --bg2: #1a1a2e;
  --accent: #e87f00;
  --accent2: #c06800;
  --text: #d4c8a0;
  --text-dim: #8a7f6a;
  --panel-bg: #16162b;
  --panel-border: #2a2a4a;
  --green: #4caf50;
  --red: #ef5350;
  --yellow: #e6b422;
  --blue: #42a5f5;
}

body {
  font-family: 'IBM Plex Mono', monospace;
  background: var(--bg);
  color: var(--text);
  min-height: 100vh;
  overflow-x: hidden;
  background-image:
    radial-gradient(ellipse at 20% 50%, rgba(232,127,0,0.03) 0%, transparent 60%),
    radial-gradient(ellipse at 80% 50%, rgba(66,165,245,0.02) 0%, transparent 60%);
}

/* Subtle noise texture */
body::before {
  content: '';
  position: fixed;
  top: 0; left: 0; right: 0; bottom: 0;
  opacity: 0.03;
  background-image: url("data:image/svg+xml,%3Csvg viewBox='0 0 256 256' xmlns='http://www.w3.org/2000/svg'%3E%3Cfilter id='n'%3E%3CfeTurbulence type='fractalNoise' baseFrequency='0.9' numOctaves='4' stitchTiles='stitch'/%3E%3C/filter%3E%3Crect width='100%25' height='100%25' filter='url(%23n)'/%3E%3C/svg%3E");
  pointer-events: none;
  z-index: 9999;
}

#app {
  max-width: 1300px;
  margin: 0 auto;
  padding: 10px;
}

/* HEADER */
#header {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 12px 20px;
  background: linear-gradient(135deg, #2a1800 0%, #1a1200 100%);
  border: 2px solid var(--accent2);
  border-radius: 8px;
  margin-bottom: 10px;
}

.logo {
  font-family: 'Press Start 2P', monospace;
  font-size: 24px;
  color: var(--accent);
  text-shadow: 0 0 20px rgba(232,127,0,0.5), 2px 2px 0 #4a2800;
  letter-spacing: 4px;
}

.subtitle {
  font-size: 11px;
  color: var(--text-dim);
  margin-top: 4px;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 8px;
  font-family: 'Press Start 2P', monospace;
  font-size: 10px;
}

.led { font-size: 18px; }
.led-running { color: #4caf50; animation: blink 1s infinite; }
.led-stopped { color: #ef5350; }
.led-paused { color: #e6b422; animation: blink 0.5s infinite; }

@keyframes blink {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.4; }
}

/* MAIN LAYOUT */
#main {
  display: grid;
  grid-template-columns: 1fr 300px;
  gap: 10px;
}

/* CRT DISPLAY */
#display-section { display: flex; flex-direction: column; gap: 6px; }

#crt-frame {
  position: relative;
  background: #000;
  border-radius: 16px;
  padding: 16px;
  border: 3px solid #333;
  box-shadow:
    inset 0 0 60px rgba(0,0,0,0.8),
    0 0 30px rgba(0,0,0,0.5),
    0 0 4px rgba(232,127,0,0.2);
  overflow: hidden;
  aspect-ratio: 4/3;
}

#screen {
  width: 100%;
  height: 100%;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  border-radius: 8px;
  display: block;
}

#scanlines {
  position: absolute;
  top: 16px; left: 16px; right: 16px; bottom: 16px;
  border-radius: 8px;
  background: repeating-linear-gradient(
    to bottom,
    transparent 0px,
    transparent 2px,
    rgba(0,0,0,0.15) 2px,
    rgba(0,0,0,0.15) 4px
  );
  pointer-events: none;
  z-index: 2;
}

#crt-glow {
  position: absolute;
  top: 16px; left: 16px; right: 16px; bottom: 16px;
  border-radius: 8px;
  box-shadow: inset 0 0 80px rgba(0,255,0,0.05);
  pointer-events: none;
  z-index: 3;
}

.glow-green #crt-glow { box-shadow: inset 0 0 80px rgba(0,255,0,0.06); }
.glow-white #crt-glow { box-shadow: inset 0 0 80px rgba(255,255,255,0.04); }
.glow-amber #crt-glow { box-shadow: inset 0 0 80px rgba(255,180,0,0.06); }

/* Memory map bar */
#memory-map-bar {
  display: flex;
  height: 18px;
  border-radius: 4px;
  overflow: hidden;
  border: 1px solid var(--panel-border);
}

.mem-seg {
  position: relative;
  opacity: 0.8;
  transition: opacity 0.3s;
}
.mem-seg:hover { opacity: 1; }
.mem-seg::after {
  content: attr(data-label);
  position: absolute;
  top: 50%; left: 50%;
  transform: translate(-50%,-50%);
  font-size: 7px;
  font-family: 'Press Start 2P', monospace;
  color: #000;
  white-space: nowrap;
}

.mem-seg-pulse {
  animation: pulse-seg 1s infinite;
}

@keyframes pulse-seg {
  0%, 100% { opacity: 0.8; }
  50% { opacity: 1; box-shadow: 0 0 8px #e040fb; }
}

/* CONTROLS SIDEBAR */
#controls-section {
  display: flex;
  flex-direction: column;
  gap: 8px;
  max-height: calc(100vh - 120px);
  overflow-y: auto;
  scrollbar-width: thin;
  scrollbar-color: var(--accent2) var(--bg2);
}

.panel {
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: 6px;
  padding: 10px;
}

.panel h3 {
  font-family: 'Press Start 2P', monospace;
  font-size: 8px;
  color: var(--accent);
  margin-bottom: 8px;
  text-transform: uppercase;
  letter-spacing: 1px;
}

/* Buttons */
.btn {
  font-family: 'IBM Plex Mono', monospace;
  font-weight: 600;
  font-size: 12px;
  padding: 8px 12px;
  border: 2px solid var(--panel-border);
  border-radius: 4px;
  cursor: pointer;
  background: linear-gradient(180deg, #2a2a4a 0%, #1a1a2e 100%);
  color: var(--text);
  transition: all 0.15s;
  box-shadow: 0 2px 0 #111, inset 0 1px 0 rgba(255,255,255,0.05);
}

.btn:hover { background: linear-gradient(180deg, #3a3a5a 0%, #2a2a3e 100%); }
.btn:active { box-shadow: none; transform: translateY(2px); }

.btn-run { border-color: var(--green); color: var(--green); }
.btn-pause { border-color: var(--yellow); color: var(--yellow); }
.btn-reset { border-color: var(--red); color: var(--red); }
.btn-step { border-color: var(--blue); color: var(--blue); }
.btn-sm { font-size: 10px; padding: 5px 8px; }

.btn-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
}

.file-btn {
  display: block;
  text-align: center;
  padding: 10px;
  background: linear-gradient(135deg, #3a1a50 0%, #2a1040 100%);
  border: 2px dashed #e040fb;
  border-radius: 6px;
  color: #e040fb;
  cursor: pointer;
  font-family: 'Press Start 2P', monospace;
  font-size: 8px;
  line-height: 1.6;
  transition: all 0.2s;
}
.file-btn:hover { background: linear-gradient(135deg, #4a2a60 0%, #3a2050 100%); }

.checkbox-row {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 11px;
  margin-top: 6px;
  cursor: pointer;
}

input[type="range"] {
  width: 100%;
  accent-color: var(--accent);
}

select {
  width: 100%;
  background: var(--bg2);
  color: var(--text);
  border: 1px solid var(--panel-border);
  padding: 5px;
  border-radius: 4px;
  font-family: 'IBM Plex Mono', monospace;
  font-size: 11px;
  margin-bottom: 6px;
}

#file-info {
  margin-top: 8px;
  padding: 6px;
  background: var(--bg);
  border-radius: 4px;
  font-size: 10px;
  word-break: break-all;
}

/* Debug panel */
.collapsible-header {
  cursor: pointer;
  user-select: none;
}
.collapsible-header:hover { color: #ffa040; }

#registers {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 2px;
}

.reg-row {
  display: flex;
  gap: 4px;
  font-size: 11px;
}
.reg-name { color: var(--accent); font-weight: 700; min-width: 26px; }
.reg-val { color: var(--green); font-family: 'Press Start 2P', monospace; font-size: 9px; }
.flags-row { grid-column: 1 / -1; }

#disasm-view, #mem-inspector { margin-top: 8px; }
#disasm-view h4, #mem-inspector h4 {
  font-size: 10px;
  color: var(--accent);
  margin-bottom: 4px;
}

#disasm, #mem-dump {
  font-family: 'Press Start 2P', monospace;
  font-size: 7px;
  line-height: 1.6;
  color: var(--text-dim);
  max-height: 150px;
  overflow-y: auto;
  background: #0a0a15;
  padding: 4px;
  border-radius: 4px;
}

.hex-input {
  background: var(--bg);
  border: 1px solid var(--panel-border);
  color: var(--accent);
  font-family: 'Press Start 2P', monospace;
  font-size: 9px;
  width: 50px;
  padding: 2px 4px;
  border-radius: 2px;
}

#counters {
  display: flex;
  gap: 12px;
  font-size: 10px;
  color: var(--text-dim);
  margin-top: 6px;
}

#actions-panel {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

/* VIRTUAL KEYBOARD */
#keyboard-section {
  margin-top: 10px;
  padding: 10px;
  background: linear-gradient(180deg, #3a2a1a 0%, #2a1a0a 100%);
  border: 2px solid #5a3a1a;
  border-radius: 8px;
}

#virtual-keyboard {
  display: flex;
  flex-direction: column;
  gap: 4px;
  max-width: 800px;
  margin: 0 auto;
}

.kb-row {
  display: flex;
  gap: 3px;
  justify-content: center;
}

.kb-key {
  font-family: 'Press Start 2P', monospace;
  font-size: 7px;
  min-width: 32px;
  height: 30px;
  background: linear-gradient(180deg, #6a4a2a 0%, #4a3018 100%);
  border: 1px solid #8a6a3a;
  border-bottom: 3px solid #2a1808;
  border-radius: 3px;
  color: #f0e0c0;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  user-select: none;
  transition: all 0.1s;
  padding: 0 4px;
}

.kb-key:hover { background: linear-gradient(180deg, #7a5a3a 0%, #5a4028 100%); }
.kb-key:active, .kb-key.active {
  background: linear-gradient(180deg, #4a3018 0%, #6a4a2a 100%);
  border-bottom-width: 1px;
  transform: translateY(2px);
}

.kb-key.wide { min-width: 52px; }
.kb-key.wider { min-width: 72px; }
.kb-key.space { min-width: 200px; }
.kb-key.special { background: linear-gradient(180deg, #8a4a2a 0%, #6a3018 100%); border-color: #aa6a3a; }

/* STATUS BAR */
#status-bar {
  display: flex;
  gap: 20px;
  padding: 6px 12px;
  margin-top: 8px;
  font-size: 10px;
  color: var(--text-dim);
  background: var(--panel-bg);
  border: 1px solid var(--panel-border);
  border-radius: 4px;
}

/* FOOTER */
footer {
  text-align: center;
  padding: 12px;
  margin-top: 10px;
}

footer a {
  color: var(--accent);
  text-decoration: none;
  font-family: 'Press Start 2P', monospace;
  font-size: 9px;
  opacity: 0.7;
  transition: opacity 0.2s;
}
footer a:hover { opacity: 1; }

/* TOAST */
#toast {
  position: fixed;
  bottom: 30px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--accent);
  color: #000;
  padding: 10px 20px;
  border-radius: 6px;
  font-family: 'IBM Plex Mono', monospace;
  font-weight: 600;
  font-size: 12px;
  z-index: 10000;
  transition: opacity 0.3s;
}

.hidden { display: none !important; }

/* RESPONSIVE */
@media (max-width: 900px) {
  #main {
    grid-template-columns: 1fr;
  }
  #controls-section {
    max-height: none;
  }
  .logo { font-size: 16px; }
}

@media (max-width: 600px) {
  .logo { font-size: 12px; letter-spacing: 2px; }
  .subtitle { font-size: 8px; }
  #crt-frame { padding: 8px; }
  .kb-key { min-width: 24px; height: 26px; font-size: 6px; }
  .kb-key.space { min-width: 120px; }
}