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

:root {
  --bg-dark: #0d0d1a;
  --bg-panel: #1a1a2e;
  --bg-panel-border: #2a2a4a;
  --beige: #c4a882;
  --beige-dark: #a08060;
  --text: #e0d8cc;
  --text-dim: #8888aa;
  --accent-red: #cc4444;
  --accent-green: #44cc66;
  --accent-yellow: #cccc44;
  --commodore-blue: #42348b;
  --commodore-lblue: #7e70ca;
  --monitor-bezel: #2a2a2a;
  --monitor-inner: #1a1a1a;
}

body {
  background: var(--bg-dark);
  background-image:
    radial-gradient(ellipse at 20% 50%, rgba(66,52,139,0.08) 0%, transparent 60%),
    radial-gradient(ellipse at 80% 20%, rgba(133,212,220,0.04) 0%, transparent 50%);
  color: var(--text);
  font-family: 'VT323', monospace;
  font-size: 18px;
  min-height: 100vh;
  overflow-x: hidden;
}

#app {
  max-width: 1600px;
  margin: 0 auto;
  padding: 0 16px;
}

.rainbow-stripe {
  height: 6px;
  background: linear-gradient(90deg,
    #e44 0%, #e44 16.6%,
    #ea4 16.6%, #ea4 33.3%,
    #ee4 33.3%, #ee4 50%,
    #4c4 50%, #4c4 66.6%,
    #48e 66.6%, #48e 83.3%,
    #a4e 83.3%, #a4e 100%
  );
  border-radius: 3px 3px 0 0;
}

#header {
  background: var(--bg-panel);
  border: 1px solid var(--bg-panel-border);
  border-radius: 6px;
  margin: 16px 0;
  overflow: hidden;
}

.header-content {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 24px;
  flex-wrap: wrap;
  gap: 12px;
}

.logo-area {
  display: flex;
  align-items: center;
  gap: 16px;
}

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

.no-kbd-badge {
  font-family: 'Press Start 2P', monospace;
  font-size: 9px;
  color: #000;
  background: var(--accent-red);
  padding: 4px 8px;
  border-radius: 3px;
  letter-spacing: 1px;
  animation: pulse-badge 2s ease-in-out infinite;
}

.kbd-badge {
  font-family: 'Press Start 2P', monospace;
  font-size: 9px;
  color: #000;
  background: var(--accent-green);
  padding: 4px 8px;
  border-radius: 3px;
  letter-spacing: 1px;
}

@keyframes pulse-badge {
  0%, 100% { opacity: 1; }
  50% { opacity: 0.7; }
}

.res-label {
  font-family: 'VT323', monospace;
  font-size: 14px;
  color: var(--text-dim);
  background: rgba(0,0,0,0.3);
  padding: 3px 10px;
  border-radius: 3px;
  border: 1px solid var(--bg-panel-border);
}

.commodore-c {
  font-family: 'Press Start 2P', monospace;
  font-size: 32px;
  color: var(--beige);
  background: var(--commodore-blue);
  padding: 8px 12px;
  border-radius: 6px;
  text-shadow: 2px 2px 0 rgba(0,0,0,0.5);
}

h1 {
  font-family: 'Press Start 2P', monospace;
  font-size: 20px;
  color: var(--beige);
  letter-spacing: 1px;
}

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

.led {
  width: 14px;
  height: 14px;
  border-radius: 50%;
  border: 1px solid rgba(255,255,255,0.2);
  flex-shrink: 0;
}

.led-yellow { background: var(--accent-yellow); box-shadow: 0 0 8px var(--accent-yellow); }
.led-green { background: var(--accent-green); box-shadow: 0 0 8px var(--accent-green); }
.led-red { background: var(--accent-red); box-shadow: 0 0 8px var(--accent-red); }

#main {
  display: grid;
  grid-template-columns: 260px 1fr 240px;
  gap: 16px;
  margin-bottom: 16px;
}

@media (max-width: 1200px) {
  #main {
    grid-template-columns: 1fr;
  }
}

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

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

.panel-hint {
  font-size: 14px;
  color: var(--text-dim);
  margin-bottom: 12px;
}

.rom-slots {
  display: flex;
  flex-direction: column;
  gap: 10px;
  margin-bottom: 16px;
}

.rom-slot {
  display: flex;
  align-items: center;
  justify-content: space-between;
  background: rgba(0,0,0,0.3);
  border: 1px solid var(--bg-panel-border);
  border-radius: 4px;
  padding: 10px;
  gap: 8px;
  transition: border-color 0.3s, background 0.3s;
}

.rom-slot.loaded {
  border-color: var(--accent-green);
  background: rgba(68, 204, 102, 0.05);
}

.rom-info {
  display: flex;
  align-items: center;
  gap: 10px;
  flex: 1;
  min-width: 0;
}

.rom-info strong {
  font-size: 14px;
  display: block;
}

.rom-info small {
  font-size: 12px;
  color: var(--text-dim);
  word-break: break-all;
}

.rom-status {
  font-size: 18px;
  flex-shrink: 0;
  width: 24px;
  text-align: center;
}

.rom-status.ok { color: var(--accent-green); }
.rom-status.fail { color: var(--accent-red); }

.rom-upload-btn {
  font-family: 'VT323', monospace;
  font-size: 14px;
  background: var(--beige-dark);
  color: #fff;
  border: none;
  border-radius: 3px;
  padding: 4px 10px;
  cursor: pointer;
  flex-shrink: 0;
  text-align: center;
  transition: background 0.15s;
}

.rom-upload-btn:hover {
  background: var(--beige);
  color: #000;
}

.power-buttons {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
}

.btn-power, .btn-reset, .btn-poweroff {
  font-family: 'Press Start 2P', monospace;
  font-size: 10px;
  padding: 8px 12px;
  border: 2px solid;
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.15s;
}

.btn-power {
  background: #1a3a1a;
  color: var(--accent-green);
  border-color: var(--accent-green);
}

.btn-power:disabled {
  background: #1a1a1a;
  color: #444;
  border-color: #444;
  cursor: not-allowed;
}

.btn-power:not(:disabled):hover {
  background: var(--accent-green);
  color: #000;
}

.btn-reset {
  background: #3a3a1a;
  color: var(--accent-yellow);
  border-color: var(--accent-yellow);
}

.btn-reset:disabled {
  background: #1a1a1a;
  color: #444;
  border-color: #444;
  cursor: not-allowed;
}

.btn-reset:not(:disabled):hover {
  background: var(--accent-yellow);
  color: #000;
}

.btn-poweroff {
  background: #3a1a1a;
  color: var(--accent-red);
  border-color: var(--accent-red);
}

.btn-poweroff:disabled {
  background: #1a1a1a;
  color: #444;
  border-color: #444;
  cursor: not-allowed;
}

.btn-poweroff:not(:disabled):hover {
  background: var(--accent-red);
  color: #000;
}

#monitor {
  display: flex;
  flex-direction: column;
  justify-content: center;
  align-items: center;
}

.monitor-bezel {
  background: var(--monitor-bezel);
  border-radius: 16px;
  padding: 24px;
  box-shadow:
    0 4px 20px rgba(0,0,0,0.6),
    inset 0 1px 0 rgba(255,255,255,0.05);
  max-width: 100%;
  transition: width 0.2s;
}

.monitor-screen-area {
  position: relative;
  background: #000;
  border-radius: 8px;
  overflow: hidden;
  line-height: 0;
  border: 2px solid #111;
}

#canvas {
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  -ms-interpolation-mode: nearest-neighbor;
  display: block;
  outline: none;
}

#canvas:focus {
  box-shadow: 0 0 0 2px var(--accent-green);
}

#canvas.mode-bordered.scale-3 { width: 768px; height: 560px; }
#canvas.mode-bordered.scale-4 { width: 768px; height: 560px; }
#canvas.mode-bordered.scale-5 { width: 960px; height: 700px; }
#canvas.mode-bordered.scale-6 { width: 960px; height: 700px; }
#canvas.mode-borderless.scale-3 { width: 660px; height: 460px; }
#canvas.mode-borderless.scale-4 { width: 660px; height: 460px; }
#canvas.mode-borderless.scale-5 { width: 660px; height: 460px; }
#canvas.mode-borderless.scale-6 { width: 660px; height: 460px; }

@media (max-width: 1200px) {
  #canvas.mode-bordered,
  #canvas.mode-borderless {
    width: 100% !important;
    height: auto !important;
  }
}

.crt-overlay {
  position: absolute;
  top: 0;
  left: 0;
  right: 0;
  bottom: 0;
  pointer-events: none;
  background-image: repeating-linear-gradient(
    0deg,
    rgba(0,0,0,0.18) 0px,
    rgba(0,0,0,0.18) 1px,
    transparent 1px,
    transparent 5px
  );
  mix-blend-mode: multiply;
}

.crt-overlay.off {
  display: none;
}

.monitor-bottom {
  display: flex;
  align-items: center;
  justify-content: center;
  margin-top: 10px;
  gap: 16px;
}

.monitor-label {
  text-align: center;
  font-family: 'Press Start 2P', monospace;
  font-size: 8px;
  color: #666;
  letter-spacing: 2px;
}

.control-group {
  margin-bottom: 12px;
}

.control-group label {
  font-size: 13px;
  color: var(--text-dim);
  display: block;
  margin-bottom: 4px;
}

.control-group select, .control-group input[type="range"] {
  width: 100%;
  font-family: 'VT323', monospace;
  font-size: 16px;
  background: rgba(0,0,0,0.4);
  color: var(--text);
  border: 1px solid var(--bg-panel-border);
  border-radius: 3px;
  padding: 4px;
}

.sound-controls {
  display: flex;
  gap: 8px;
  align-items: center;
}

.btn-small {
  font-family: 'VT323', monospace;
  font-size: 16px;
  background: rgba(0,0,0,0.4);
  color: var(--text);
  border: 1px solid var(--bg-panel-border);
  border-radius: 3px;
  padding: 4px 10px;
  cursor: pointer;
  transition: border-color 0.15s, color 0.15s;
}

.btn-small:hover { border-color: var(--beige); }

.btn-toggle.active {
  border-color: var(--accent-green);
  color: var(--accent-green);
}

.btn-upload-label {
  display: inline-block;
}

.periph-status {
  font-size: 13px;
  color: var(--text-dim);
  display: block;
  margin-top: 4px;
}

.border-toggle-row {
  display: flex;
  align-items: center;
  gap: 8px;
}

.btn-borders {
  min-width: 50px;
  text-align: center;
  font-weight: bold;
}

.btn-borders.active {
  border-color: var(--commodore-lblue);
  color: var(--commodore-lblue);
  background: rgba(66,52,139,0.2);
}

.border-res-info {
  font-family: 'VT323', monospace;
  font-size: 14px;
  color: var(--text-dim);
  background: rgba(0,0,0,0.3);
  padding: 2px 8px;
  border-radius: 3px;
  border: 1px solid var(--bg-panel-border);
}

.debug-panel {
  margin-bottom: 16px;
}

.debug-panel.hidden {
  display: none;
}

.debug-regs {
  background: rgba(0,0,0,0.4);
  padding: 10px;
  border-radius: 4px;
  margin-bottom: 10px;
}

.debug-row {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  margin-bottom: 4px;
  font-size: 16px;
}

.debug-row code {
  color: var(--accent-green);
  font-family: 'VT323', monospace;
}

.debug-controls {
  display: flex;
  gap: 8px;
  margin-bottom: 10px;
}

.debug-section {
  margin-bottom: 12px;
}

.debug-section h3 {
  font-family: 'Press Start 2P', monospace;
  font-size: 9px;
  color: var(--commodore-lblue);
  margin-bottom: 8px;
  letter-spacing: 0.5px;
}

.kbd-debug-info {
  background: rgba(0,0,0,0.4);
  padding: 8px 10px;
  border-radius: 4px;
  margin-bottom: 8px;
}

.kbd-debug-row {
  display: flex;
  flex-wrap: wrap;
  gap: 16px;
  margin-bottom: 4px;
  font-size: 15px;
}

.kbd-debug-row code {
  color: var(--accent-yellow);
  font-family: 'VT323', monospace;
}

.vic-regs-dump {
  width: 100%;
  font-family: 'VT323', monospace;
  font-size: 14px;
  color: var(--text-dim);
  background: rgba(0,0,0,0.5);
  padding: 10px;
  border-radius: 4px;
  overflow-x: auto;
  white-space: pre;
  line-height: 1.6;
  border: 1px solid var(--bg-panel-border);
}

.sound-status-dump {
  line-height: 1.4;
}

.vic-reg-entry {
  display: block;
  transition: background 0.1s;
}

.vic-reg-entry:hover {
  background: rgba(126, 112, 202, 0.12);
}

.vic-reg-addr {
  color: var(--accent-yellow);
  font-family: 'VT323', monospace;
}

.vic-reg-name {
  color: var(--text-dim);
  font-family: 'VT323', monospace;
  font-size: 13px;
}

.vic-reg-hex {
  color: var(--accent-green);
  font-family: 'VT323', monospace;
}

.vic-reg-bin {
  color: var(--commodore-lblue);
  font-family: 'VT323', monospace;
  letter-spacing: 1px;
}

.vic-reg-decoded {
  color: var(--beige);
  font-family: 'VT323', monospace;
  font-size: 13px;
}

.debug-mem {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}

.debug-mem input {
  font-family: 'VT323', monospace;
  font-size: 16px;
  width: 60px;
  background: rgba(0,0,0,0.4);
  color: var(--accent-green);
  border: 1px solid var(--bg-panel-border);
  border-radius: 3px;
  padding: 4px;
  text-transform: uppercase;
}

.debug-mem pre {
  width: 100%;
  font-family: 'VT323', monospace;
  font-size: 14px;
  color: var(--text-dim);
  background: rgba(0,0,0,0.4);
  padding: 8px;
  border-radius: 4px;
  overflow-x: auto;
  white-space: pre;
  margin-top: 8px;
}

/* ---- Memory Corruption ---- */
.corruption-group {
  border-top: 1px solid var(--bg-panel-border);
  padding-top: 12px;
  margin-top: 4px;
}

.corruption-controls {
  display: flex;
  align-items: center;
  gap: 4px;
  margin-bottom: 8px;
}

.corruption-input {
  font-family: 'VT323', monospace;
  font-size: 16px;
  width: 80px;
  background: rgba(0,0,0,0.4);
  color: var(--accent-red);
  border: 1px solid var(--bg-panel-border);
  border-radius: 3px;
  padding: 4px 6px;
  text-align: right;
}

.corruption-input:focus {
  outline: none;
  border-color: var(--accent-red);
  box-shadow: 0 0 4px rgba(204,68,68,0.3);
}

.corruption-pct {
  font-family: 'VT323', monospace;
  font-size: 16px;
  color: var(--text-dim);
}

.btn-corrupt {
  font-family: 'Press Start 2P', monospace;
  font-size: 9px;
  width: 100%;
  padding: 8px 12px;
  background: #3a1a1a;
  color: var(--accent-red);
  border: 2px solid var(--accent-red);
  border-radius: 4px;
  cursor: pointer;
  transition: all 0.15s;
  letter-spacing: 1px;
}

.btn-corrupt:hover {
  background: var(--accent-red);
  color: #000;
  box-shadow: 0 0 12px rgba(204,68,68,0.4);
}

.btn-corrupt:active {
  transform: scale(0.96);
}

.btn-corrupt.corrupting {
  animation: corrupt-flash 0.3s ease-out;
}

@keyframes corrupt-flash {
  0% { background: #ff2222; color: #fff; box-shadow: 0 0 30px rgba(255,34,34,0.8); }
  100% { background: #3a1a1a; color: var(--accent-red); box-shadow: none; }
}

/* ---- Virtual Keyboard ---- */
.virtual-keyboard {
  background: linear-gradient(180deg, #3a3530 0%, #2a2520 40%, #1a1510 100%);
  border-radius: 0 0 12px 12px;
  padding: 14px 16px 12px 16px;
  margin-top: 0;
  border: 1px solid #4a4035;
  border-top: 2px solid #5a5045;
  box-shadow:
    0 6px 20px rgba(0,0,0,0.5),
    inset 0 1px 0 rgba(255,255,255,0.05);
  user-select: none;
  -webkit-user-select: none;
  max-width: 100%;
  overflow-x: auto;
}

.vk-row {
  display: flex;
  gap: 3px;
  margin-bottom: 3px;
  justify-content: center;
}

.vk-row:last-child {
  margin-bottom: 0;
}

.vk-row-space {
  margin-top: 2px;
}

.vk-row-fkeys {
  margin-top: 6px;
  padding-top: 6px;
  border-top: 1px solid rgba(255,255,255,0.06);
}

.vk-key {
  font-family: 'Press Start 2P', monospace;
  font-size: 8px;
  color: #ddd;
  background: linear-gradient(180deg, #5a5550 0%, #3a3530 50%, #2a2520 100%);
  border: 1px solid #666;
  border-bottom: 2px solid #222;
  border-radius: 4px;
  padding: 0;
  cursor: pointer;
  display: flex;
  align-items: center;
  justify-content: center;
  height: 32px;
  transition: all 0.05s;
  text-shadow: 0 1px 1px rgba(0,0,0,0.5);
  box-shadow:
    0 1px 2px rgba(0,0,0,0.3),
    inset 0 1px 0 rgba(255,255,255,0.08);
}

.vk-key:hover {
  background: linear-gradient(180deg, #6a6560 0%, #4a4540 50%, #3a3530 100%);
  border-color: #888;
  color: #fff;
}

.vk-key:active, .vk-key.vk-active {
  background: linear-gradient(180deg, #2a2520 0%, #1a1510 50%, #0a0500 100%);
  border-bottom: 1px solid #222;
  border-top: 2px solid #111;
  color: var(--accent-green);
  box-shadow:
    inset 0 2px 4px rgba(0,0,0,0.5);
  transform: translateY(1px);
  text-shadow: 0 0 6px rgba(68,204,102,0.4);
}

.vk-1u {
  width: 38px;
  min-width: 38px;
}

.vk-1_5u {
  width: 54px;
  min-width: 54px;
  font-size: 7px;
}

.vk-2u {
  width: 72px;
  min-width: 72px;
  font-size: 7px;
}

.vk-space {
  width: 320px;
  min-width: 200px;
  flex: 0 1 320px;
  font-size: 7px;
  letter-spacing: 3px;
}

.vk-return {
  background: linear-gradient(180deg, #4a5540 0%, #2a3520 50%, #1a2510 100%);
  border-color: #5a6;
  color: var(--accent-green);
  font-size: 7px;
}

.vk-return:hover {
  background: linear-gradient(180deg, #5a6650 0%, #3a4530 50%, #2a3520 100%);
  border-color: #7b8;
}

.vk-shift {
  background: linear-gradient(180deg, #554a40 0%, #3a3025 50%, #2a2015 100%);
  border-color: #886;
  color: var(--accent-yellow);
  font-size: 7px;
}

.vk-shift:hover {
  background: linear-gradient(180deg, #665a50 0%, #4a4035 50%, #3a3025 100%);
}

.vk-shift:active, .vk-shift.vk-active {
  color: #fff;
  background: linear-gradient(180deg, #2a2515 0%, #1a1510 50%, #0a0500 100%);
  text-shadow: 0 0 8px rgba(204,204,68,0.6);
}

.vk-ctrl, .vk-cbm {
  background: linear-gradient(180deg, #4a4055 0%, #2a2035 50%, #1a1025 100%);
  border-color: #869;
  color: var(--commodore-lblue);
  font-size: 7px;
}

.vk-ctrl:hover, .vk-cbm:hover {
  background: linear-gradient(180deg, #5a5065 0%, #3a3045 50%, #2a2035 100%);
}

.vk-ctrl:active, .vk-ctrl.vk-active,
.vk-cbm:active, .vk-cbm.vk-active {
  color: #fff;
  text-shadow: 0 0 8px rgba(126,112,202,0.6);
}

.vk-special {
  background: linear-gradient(180deg, #554040 0%, #3a2020 50%, #2a1010 100%);
  border-color: #866;
  color: var(--accent-red);
  font-size: 7px;
}

.vk-special:hover {
  background: linear-gradient(180deg, #665050 0%, #4a3030 50%, #3a2020 100%);
}

.vk-cursor {
  font-size: 10px;
  color: var(--accent-yellow);
}

.vk-fkey {
  width: 56px;
  min-width: 56px;
  font-size: 8px;
  color: #e9b287;
  background: linear-gradient(180deg, #4a4530 0%, #2a2520 50%, #1a1510 100%);
  border-color: #886;
}

.vk-fkey:hover {
  background: linear-gradient(180deg, #5a5540 0%, #3a3530 50%, #2a2520 100%);
  color: #fff;
}

.vk-shift-lock {
  width: 32px;
  min-width: 32px;
  font-size: 6px;
  background: linear-gradient(180deg, #554a40 0%, #3a3025 50%, #2a2015 100%);
  border-color: #775;
  color: #aa9;
}

@media (max-width: 900px) {
  .vk-key { height: 28px; font-size: 7px; }
  .vk-1u { width: 30px; min-width: 28px; }
  .vk-1_5u { width: 44px; min-width: 40px; }
  .vk-2u { width: 58px; min-width: 54px; }
  .vk-space { width: 220px; min-width: 140px; }
  .vk-fkey { width: 46px; min-width: 40px; }
  .virtual-keyboard { padding: 8px 6px; }
  .vk-row { gap: 2px; margin-bottom: 2px; }
}

@media (max-width: 600px) {
  .vk-key { height: 24px; font-size: 6px; }
  .vk-1u { width: 22px; min-width: 20px; }
  .vk-1_5u { width: 32px; min-width: 28px; }
  .vk-2u { width: 42px; min-width: 38px; }
  .vk-space { width: 160px; min-width: 100px; }
  .vk-fkey { width: 36px; min-width: 32px; }
}

footer {
  text-align: center;
  padding: 16px;
  font-size: 14px;
  color: var(--text-dim);
  border-top: 1px solid var(--bg-panel-border);
  margin-bottom: 24px;
}

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

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

footer p + p {
  margin-top: 6px;
}