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

:root {
  --bg-dark: #0a0a14;
  --bg-panel: #14142a;
  --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: #222;
  --monitor-inner: #111;
  --kb-bg: #c8b494;
  --kb-key-bg: #d4c4a0;
  --kb-key-border: #a89878;
  --kb-key-active: #8b7355;
  --kb-key-text: #2a2015;
  --kb-func-bg: #a89070;
  --kb-special-bg: #baa888;
}

body {
  background: var(--bg-dark);
  background-image:
    radial-gradient(ellipse at 50% 0%, rgba(66,52,139,0.12) 0%, transparent 60%),
    radial-gradient(ellipse at 80% 100%, rgba(139,52,66,0.06) 0%, transparent 50%);
  color: var(--text);
  font-family: 'VT323', monospace;
  font-size: 18px;
  min-height: 100vh;
  overflow-x: hidden;
  -webkit-user-select: none;
  user-select: none;
}

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

.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 220px;
  gap: 16px;
  margin-bottom: 16px;
}

@media (max-width: 1400px) {
  #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;
}

.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;
}
.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 */
#monitor {
  display: flex;
  justify-content: center;
  align-items: flex-start;
}

.monitor-bezel {
  background: linear-gradient(175deg, #333 0%, #1a1a1a 40%, #222 100%);
  border-radius: 20px;
  padding: 26px;
  box-shadow:
    0 6px 30px rgba(0,0,0,0.7),
    0 2px 8px rgba(0,0,0,0.5),
    inset 0 1px 0 rgba(255,255,255,0.06),
    inset 0 -1px 0 rgba(0,0,0,0.3);
  max-width: 100%;
  transition: width 0.2s;
  border: 1px solid #3a3a3a;
}

.monitor-screen-area {
  position: relative;
  background: #000;
  border-radius: 10px;
  overflow: hidden;
  line-height: 0;
  box-shadow:
    inset 0 0 30px rgba(0,0,0,0.8),
    inset 0 0 4px rgba(0,0,0,1),
    0 0 2px rgba(66,52,139,0.3);
}

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

#canvas-post {
  position: absolute;
  top: 0;
  left: 0;
  pointer-events: none;
  display: block;
}

#canvas-main.mode-bordered,
#canvas-post.mode-bordered {
  width: 1152px;
  height: 840px;
}

#canvas-main.mode-borderless,
#canvas-post.mode-borderless {
  width: 1056px;
  height: 736px;
}

@media (max-width: 1400px) {
  #canvas-main.mode-bordered,
  #canvas-post.mode-bordered,
  #canvas-main.mode-borderless,
  #canvas-post.mode-borderless {
    width: 100%;
    height: auto;
  }
}

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

.monitor-label {
  text-align: center;
  font-family: 'Press Start 2P', monospace;
  font-size: 7px;
  color: #555;
  letter-spacing: 3px;
  text-transform: uppercase;
}

/* Controls */
.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;
}
.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);
}

/* KEYBOARD */
#keyboard-container {
  max-width: 900px;
  margin: 0 auto 16px auto;
  padding: 0 8px;
}

#vic20-keyboard {
  background: var(--kb-bg);
  border-radius: 10px;
  padding: 12px 10px;
  box-shadow:
    0 4px 16px rgba(0,0,0,0.5),
    inset 0 1px 0 rgba(255,255,255,0.3),
    inset 0 -2px 0 rgba(0,0,0,0.15);
  border: 2px solid #a89070;
}

.kb-row {
  display: flex;
  gap: 3px;
  margin-bottom: 3px;
  justify-content: center;
  align-items: stretch;
}
.kb-row:last-child { margin-bottom: 0; }

.kb-key {
  font-family: 'Press Start 2P', monospace;
  font-size: 7px;
  line-height: 1.3;
  color: var(--kb-key-text);
  background: var(--kb-key-bg);
  border: 1px solid var(--kb-key-border);
  border-bottom: 3px solid #8a7a5a;
  border-right: 2px solid #9a8a6a;
  border-radius: 4px;
  padding: 6px 5px;
  min-width: 38px;
  min-height: 34px;
  cursor: pointer;
  text-align: center;
  display: flex;
  align-items: center;
  justify-content: center;
  transition: background 0.05s, border-bottom 0.05s, transform 0.05s;
  touch-action: manipulation;
  white-space: nowrap;
}
.kb-key:hover { background: #c8b488; }
.kb-key.kb-active, .kb-key:active {
  background: var(--kb-key-active) !important;
  color: #fff;
  border-bottom: 1px solid #6a5a3a;
  transform: translateY(2px);
}

.kb-wide { min-width: 56px; padding: 6px 4px; font-size: 6px; }
.kb-return { min-width: 68px; background: #bfa878; }
.kb-shift { min-width: 52px; }
.kb-func {
  background: var(--kb-func-bg);
  color: #1a1000;
  min-width: 36px;
  font-size: 7px;
  border-color: #8a7a5a;
  margin-left: 6px;
}
.kb-func:hover { background: #988060; }
.kb-func.kb-active, .kb-func:active { background: #6a5a3a !important; color: #fff; }
.kb-space { flex: 1; max-width: 420px; min-width: 200px; min-height: 36px; font-size: 8px; }
.kb-cursor { min-width: 44px; font-size: 6px; }
.kb-special { background: var(--kb-special-bg); min-width: 52px; font-size: 6px; border-color: #9a8a6a; }
.kb-special:hover { background: #a89878; }
.kb-cbm { background: #8a7a5a; color: #e8d8b8; font-size: 9px; font-weight: bold; }
.kb-cbm:hover { background: #7a6a4a; }
.kb-cbm.kb-active, .kb-cbm:active { background: #5a4a2a !important; color: #fff; }
.kb-row-space { justify-content: center; gap: 6px; }

@media (max-width: 700px) {
  #vic20-keyboard { padding: 8px 4px; }
  .kb-key { font-size: 5px; min-width: 24px; min-height: 26px; padding: 3px 2px; }
  .kb-wide { min-width: 36px; font-size: 4.5px; }
  .kb-return { min-width: 44px; }
  .kb-func { min-width: 24px; margin-left: 3px; }
  .kb-space { min-width: 120px; }
  .kb-cursor { min-width: 28px; font-size: 4.5px; }
  .kb-row { gap: 2px; margin-bottom: 2px; }
}

@media (max-width: 480px) {
  .kb-key { font-size: 4px; min-width: 18px; min-height: 22px; padding: 2px 1px; border-radius: 3px; }
  .kb-wide { min-width: 26px; font-size: 3.5px; }
  .kb-return { min-width: 32px; }
  .kb-func { min-width: 18px; margin-left: 2px; font-size: 4px; }
  .kb-space { min-width: 80px; font-size: 5px; }
  .kb-cursor { min-width: 22px; font-size: 3.5px; }
}

/* Debug */
.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-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;
}

.vic-mode-label {
  font-family: 'Press Start 2P', monospace;
  font-size: 9px;
  color: var(--commodore-lblue);
  background: rgba(66,52,139,0.2);
  padding: 4px 10px;
  border-radius: 3px;
  border: 1px solid var(--commodore-blue);
  letter-spacing: 0.5px;
  white-space: nowrap;
}

.vic-modes-info {
  font-size: 12px;
  color: var(--text-dim);
  margin-top: 4px;
  opacity: 0.7;
}

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