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

:root {
  --crt-green: #33FF33;
  --crt-dim: #0a3a0a;
  --crt-bg: #001100;
  --crt-glow: #00FF00;
  --bezel-dark: #3a3028;
  --bezel-light: #6b5d4f;
  --panel-bg: #d2c4a0;
  --panel-border: #bfb494;
  --panel-shadow: #8a7d68;
  --bg-dark: #1a1612;
  --bg-wood: #2d2318;
  --key-bg: #e8e0d0;
  --key-border: #c4b89a;
  --key-shadow: #9a8e76;
  --key-text: #2a2218;
  --text-light: #e0d8c8;
  --accent-red: #cc3333;
  --accent-green: #33cc33;
  --led-off: #444;
  --led-run: #33ff33;
  --led-halt: #ff3333;
}

body {
  font-family: 'IBM Plex Mono', monospace;
  background: var(--bg-dark);
  background-image: 
    repeating-linear-gradient(
      90deg,
      transparent, transparent 2px,
      rgba(45,35,24,0.3) 2px, rgba(45,35,24,0.3) 4px
    ),
    repeating-linear-gradient(
      0deg,
      transparent, transparent 2px,
      rgba(45,35,24,0.15) 2px, rgba(45,35,24,0.15) 4px
    );
  color: var(--text-light);
  min-height: 100vh;
  overflow-x: hidden;
}

/* Top Bar */
#top-bar {
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 10px 20px;
  background: linear-gradient(180deg, #3a3028 0%, #2a2018 100%);
  border-bottom: 2px solid #5a4d3e;
  flex-wrap: wrap;
  gap: 10px;
}

#top-bar h1 {
  font-family: 'Press Start 2P', monospace;
  font-size: 16px;
  color: var(--crt-green);
  text-shadow: 0 0 10px rgba(0,255,0,0.5);
  letter-spacing: 1px;
}

.subtitle {
  font-size: 11px;
  color: #8a7d68;
  margin-top: 4px;
  font-style: italic;
}

.header-right {
  display: flex;
  align-items: center;
  gap: 16px;
}

.status-led {
  display: flex;
  align-items: center;
  gap: 6px;
}

.led {
  width: 10px;
  height: 10px;
  border-radius: 50%;
  background: var(--led-off);
  display: inline-block;
  border: 1px solid #333;
}

.led.running {
  background: var(--led-run);
  box-shadow: 0 0 8px var(--led-run), 0 0 16px rgba(0,255,0,0.3);
  animation: pulse-led 1.5s ease-in-out infinite;
}

.led.halted {
  background: var(--led-halt);
  box-shadow: 0 0 6px var(--led-halt);
}

@keyframes pulse-led {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.6; }
}

.led-label { font-size: 10px; text-transform: uppercase; }

.berry-badge {
  font-size: 10px;
  background: rgba(255,255,255,0.08);
  padding: 4px 8px;
  border-radius: 4px;
  border: 1px solid rgba(255,255,255,0.1);
}

/* Layout */
#main-layout {
  display: flex;
  gap: 12px;
  padding: 12px;
  max-width: 1400px;
  margin: 0 auto;
  align-items: flex-start;
}

/* Panels */
.panel {
  background: linear-gradient(160deg, var(--panel-bg) 0%, #c4b694 100%);
  border-radius: 8px;
  padding: 12px;
  border: 2px solid var(--panel-border);
  box-shadow: 
    2px 2px 8px rgba(0,0,0,0.4),
    inset 0 1px 0 rgba(255,255,255,0.3);
  color: #2a2218;
}

.panel-title {
  font-family: 'Press Start 2P', monospace;
  font-size: 9px;
  color: #3a3028;
  margin-bottom: 10px;
  text-transform: uppercase;
  border-bottom: 1px solid var(--panel-shadow);
  padding-bottom: 6px;
}

/* ROM Panel */
#rom-panel {
  width: 280px;
  min-width: 240px;
  flex-shrink: 0;
}

.rom-slot {
  margin-bottom: 10px;
  padding-bottom: 10px;
  border-bottom: 1px dashed #bfb494;
}

.rom-slot label {
  display: block;
  font-size: 10px;
  font-weight: 600;
  margin-bottom: 4px;
  color: #3a3028;
}

.rom-upload {
  display: flex;
  align-items: center;
  gap: 6px;
  flex-wrap: wrap;
}

.rom-upload input[type="file"] { display: none; }

.rom-btn {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 10px;
  padding: 4px 8px;
  border: 1px solid #8a7d68;
  border-radius: 3px;
  background: #e8e0d0;
  color: #2a2218;
  cursor: pointer;
  box-shadow: 1px 1px 2px rgba(0,0,0,0.2);
}

.rom-btn:hover { background: #f0e8d8; }
.rom-btn:active { box-shadow: inset 1px 1px 2px rgba(0,0,0,0.2); }

.rom-status { font-size: 10px; }
.rom-status.loaded { color: #2a8a2a; }
.rom-status.error { color: #cc3333; }

.rom-clear {
  font-size: 9px;
  background: none;
  border: 1px solid #aa8888;
  color: #884444;
  padding: 2px 6px;
  border-radius: 2px;
  cursor: pointer;
}

.rom-info { font-size: 9px; color: #6b5d4f; margin-top: 2px; }

.rom-master-status {
  font-size: 10px;
  padding: 6px;
  background: rgba(0,0,0,0.08);
  border-radius: 4px;
  text-align: center;
  font-weight: 600;
  margin-bottom: 12px;
}

.rom-master-status.ready { color: #2a8a2a; }

/* Cassette */
.cassette-section, .audio-section { margin-top: 12px; }

.cassette-controls {
  display: flex;
  flex-direction: column;
  gap: 6px;
}

.cassette-load { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.cassette-load input[type="file"] { display: none; }
.cassette-info { font-size: 9px; color: #6b5d4f; }

.cassette-status {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 10px;
}

.tape-icon {
  font-size: 14px;
  color: #5a4d3e;
}

.tape-icon.playing {
  animation: spin-reels 0.5s linear infinite;
}

@keyframes spin-reels {
  to { transform: rotate(360deg); }
}

.baud-rate { font-size: 9px; color: #8a7d68; margin-left: auto; }

.tape-progress {
  height: 4px;
  background: #bfb494;
  border-radius: 2px;
  overflow: hidden;
}

.tape-progress-bar {
  height: 100%;
  width: 0%;
  background: var(--accent-green);
  transition: width 0.3s;
}

/* Audio controls */
.audio-controls {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 10px;
}

.audio-controls input[type="range"] {
  flex: 1;
  height: 4px;
  -webkit-appearance: none;
  appearance: none;
  background: #8a7d68;
  border-radius: 2px;
  outline: none;
}

.audio-controls input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none;
  width: 12px;
  height: 12px;
  border-radius: 50%;
  background: #5a4d3e;
  cursor: pointer;
}

/* Center Area */
#center-area {
  flex: 1;
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

/* CRT */
#crt-container {
  width: 100%;
  max-width: 560px;
}

#crt-bezel {
  background: linear-gradient(145deg, #4a4038 0%, #2a2218 40%, #3a3028 100%);
  border-radius: 24px;
  padding: 24px;
  box-shadow:
    4px 4px 16px rgba(0,0,0,0.6),
    inset 0 1px 0 rgba(255,255,255,0.08),
    inset 0 -1px 0 rgba(0,0,0,0.3);
  position: relative;
}

#crt-screen-wrap {
  position: relative;
  border-radius: 12px;
  overflow: hidden;
  background: var(--crt-bg);
  box-shadow:
    0 0 30px rgba(0,255,0,0.08),
    0 0 60px rgba(0,255,0,0.04),
    inset 0 0 40px rgba(0,0,0,0.5);
  border: 3px solid #1a1612;
}

#crt-canvas {
  display: block;
  width: 100%;
  height: auto;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
}

#scanlines {
  position: absolute;
  top: 0; left: 0; right: 0; bottom: 0;
  background: repeating-linear-gradient(
    0deg,
    transparent 0px,
    transparent 2px,
    rgba(0,0,0,0.15) 2px,
    rgba(0,0,0,0.15) 4px
  );
  pointer-events: none;
}

#screen-glare {
  position: absolute;
  top: 5%; left: 5%;
  width: 40%;
  height: 30%;
  background: radial-gradient(ellipse, rgba(255,255,255,0.04) 0%, transparent 70%);
  pointer-events: none;
}

#bezel-label {
  text-align: center;
  font-family: 'Press Start 2P', monospace;
  font-size: 8px;
  color: #6b5d4f;
  margin-top: 10px;
  letter-spacing: 3px;
}

/* Control Panel */
#control-panel {
  display: flex;
  align-items: center;
  gap: 10px;
  flex-wrap: wrap;
  justify-content: center;
  width: 100%;
  max-width: 560px;
}

.ctrl-btn {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 11px;
  font-weight: 700;
  padding: 8px 14px;
  border: 2px solid;
  border-radius: 6px;
  cursor: pointer;
  text-transform: uppercase;
  letter-spacing: 1px;
  box-shadow: 2px 2px 4px rgba(0,0,0,0.3);
  transition: all 0.1s;
}

.ctrl-btn:active {
  transform: translateY(1px);
  box-shadow: 1px 1px 2px rgba(0,0,0,0.3);
}

.power-btn {
  background: linear-gradient(180deg, #44cc44, #228822);
  border-color: #1a6a1a;
  color: #fff;
  text-shadow: 0 1px 2px rgba(0,0,0,0.3);
}

.power-btn.on {
  background: linear-gradient(180deg, #cc4444, #882222);
  border-color: #6a1a1a;
}

.reset-btn {
  background: linear-gradient(180deg, #ccaa44, #887722);
  border-color: #6a5a1a;
  color: #fff;
}

.break-btn {
  background: linear-gradient(180deg, #cc4444, #882222);
  border-color: #6a1a1a;
  color: #fff;
}

.debug-btn {
  background: linear-gradient(180deg, #666, #444);
  border-color: #333;
  color: #ddd;
  font-size: 10px;
}

.speed-display {
  font-family: 'Press Start 2P', monospace;
  font-size: 9px;
  color: var(--crt-green);
  background: #111;
  padding: 6px 10px;
  border-radius: 4px;
  border: 1px solid #333;
  text-shadow: 0 0 6px rgba(0,255,0,0.4);
}

/* Keyboard */
#keyboard-panel {
  width: 100%;
  max-width: 680px;
}

#virtual-keyboard {
  display: flex;
  flex-direction: column;
  gap: 4px;
  align-items: center;
}

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

.kb-key {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 10px;
  font-weight: 600;
  min-width: 32px;
  height: 32px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: linear-gradient(180deg, #f0e8d8, #d8d0c0);
  border: 1px solid var(--key-border);
  border-radius: 4px;
  color: var(--key-text);
  cursor: pointer;
  box-shadow:
    0 2px 0 var(--key-shadow),
    0 3px 4px rgba(0,0,0,0.2);
  transition: all 0.05s;
  user-select: none;
  padding: 0 4px;
  white-space: nowrap;
}

.kb-key:hover { background: linear-gradient(180deg, #fff, #e8e0d0); }

.kb-key:active, .kb-key.pressed {
  transform: translateY(2px);
  box-shadow: 0 0 0 var(--key-shadow);
  background: linear-gradient(180deg, #c8c0b0, #b8b0a0);
}

.kb-key.special {
  background: linear-gradient(180deg, #bab2a0, #a8a090);
  font-size: 8px;
  min-width: 48px;
}

.kb-key.break-key {
  background: linear-gradient(180deg, #dd6666, #bb4444);
  color: #fff;
  border-color: #884444;
}

.kb-key.space-key {
  min-width: 260px;
}

.kb-key.shift-key { min-width: 56px; }
.kb-key.return-key { min-width: 52px; }

/* Debug Panel */
#debug-panel {
  width: 240px;
  min-width: 200px;
  flex-shrink: 0;
  font-size: 11px;
  max-height: 80vh;
  overflow-y: auto;
}

.reg-row {
  display: flex;
  justify-content: space-between;
  padding: 2px 0;
  border-bottom: 1px dotted #bfb494;
}

.reg-label { font-weight: 700; }

.reg-val {
  font-family: 'Press Start 2P', monospace;
  font-size: 10px;
  color: #3a3028;
}

#debug-flags {
  display: flex;
  gap: 4px;
  margin: 8px 0;
  justify-content: center;
}

.flag {
  width: 20px;
  height: 20px;
  display: flex;
  align-items: center;
  justify-content: center;
  font-size: 9px;
  font-weight: 700;
  border-radius: 3px;
  background: #bfb494;
  color: #6b5d4f;
  border: 1px solid #8a7d68;
}

.flag.active {
  background: var(--accent-green);
  color: #fff;
  border-color: #228822;
}

.flag-dash { opacity: 0.3; }

#debug-disasm {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 9px;
  background: #111;
  color: var(--crt-green);
  padding: 6px;
  border-radius: 4px;
  margin: 6px 0;
  min-height: 60px;
  max-height: 120px;
  overflow-y: auto;
  white-space: pre;
  line-height: 1.4;
}

.debug-controls {
  display: flex;
  gap: 4px;
  margin: 6px 0;
  flex-wrap: wrap;
  align-items: center;
}

.dbg-btn {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 9px;
  padding: 3px 8px;
  border: 1px solid #8a7d68;
  border-radius: 3px;
  background: #e8e0d0;
  cursor: pointer;
}

.debug-mem {
  display: flex;
  align-items: center;
  gap: 4px;
  font-size: 10px;
  margin: 6px 0;
}

.debug-mem input {
  width: 30px;
  font-family: 'IBM Plex Mono', monospace;
  font-size: 10px;
  text-align: center;
  border: 1px solid #8a7d68;
  border-radius: 2px;
  background: #f0e8d8;
  padding: 2px;
  text-transform: uppercase;
}

.mem-dump {
  font-family: 'IBM Plex Mono', monospace;
  font-size: 8px;
  background: #111;
  color: var(--crt-green);
  padding: 4px;
  border-radius: 4px;
  white-space: pre;
  overflow-x: auto;
  max-height: 200px;
  overflow-y: auto;
  line-height: 1.3;
}

/* Footer */
footer {
  text-align: center;
  padding: 12px;
  font-size: 11px;
}

footer a {
  color: var(--crt-green);
  text-decoration: none;
}

footer a:hover { text-decoration: underline; }

/* Responsive */
@media (max-width: 960px) {
  #main-layout {
    flex-direction: column;
    align-items: center;
  }
  #rom-panel, #debug-panel {
    width: 100%;
    max-width: 560px;
  }
  #debug-panel { order: 10; }
}

@media (max-width: 700px) {
  #top-bar h1 { font-size: 11px; }
  .kb-key { min-width: 24px; height: 26px; font-size: 8px; }
  .kb-key.special { min-width: 36px; font-size: 7px; }
  .kb-key.space-key { min-width: 180px; }
  .kb-key.shift-key { min-width: 40px; }
  #crt-bezel { padding: 12px; border-radius: 16px; }
}