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

:root {
  --bg: #0d0d1a;
  --panel: #141428;
  --bezel: #1a1a2e;
  --accent: #D700D7;
  --text: #c8c8d0;
  --green: #00D700;
  --cyan: #00D7D7;
  --yellow: #D7D700;
  --key-bg: #2a2a3a;
  --key-hover: #3a3a5a;
  --key-active: #4a4a6a;
}

body {
  background: var(--bg);
  color: var(--text);
  font-family: 'Share Tech Mono', monospace;
  min-height: 100vh;
  min-height: 100dvh;
  display: flex;
  flex-direction: column;
  align-items: center;
  overflow-x: hidden;
}

#app {
  width: 100%;
  max-width: 960px;
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 10px;
}

header {
  text-align: center;
  margin-bottom: 12px;
  width: 100%;
}

header h1 {
  font-family: 'VT323', monospace;
  font-size: 2.4rem;
  letter-spacing: 2px;
  color: #fff;
  text-shadow: 0 0 20px rgba(215,0,215,0.4);
}

header .model {
  color: var(--cyan);
}

.rainbow-bar {
  display: flex;
  height: 4px;
  margin: 6px auto;
  max-width: 400px;
  border-radius: 2px;
  overflow: hidden;
}

.rainbow-bar span {
  flex: 1;
}

.status-bar {
  display: flex;
  justify-content: center;
  gap: 20px;
  font-size: 0.8rem;
  color: #888;
  margin-top: 6px;
}

.status-bar span {
  padding: 2px 8px;
  background: rgba(255,255,255,0.05);
  border-radius: 3px;
}

#screen-wrapper {
  position: relative;
  margin-bottom: 12px;
}

#crt-bezel {
  position: relative;
  background: #0a0a0a;
  border: 3px solid #2a2a3a;
  border-radius: 12px;
  padding: 8px;
  box-shadow:
    inset 0 0 30px rgba(0,0,0,0.8),
    0 0 40px rgba(215,0,215,0.1),
    0 4px 20px rgba(0,0,0,0.6);
  overflow: hidden;
}

#canvas {
  display: block;
  image-rendering: pixelated;
  image-rendering: crisp-edges;
  width: 640px;
  height: 512px;
  border-radius: 4px;
}

#scanlines {
  position: absolute;
  top: 8px;
  left: 8px;
  right: 8px;
  bottom: 8px;
  pointer-events: none;
  border-radius: 4px;
  opacity: 0;
  transition: opacity 0.3s;
  background: repeating-linear-gradient(
    0deg,
    transparent,
    transparent 1px,
    rgba(0,0,0,0.15) 1px,
    rgba(0,0,0,0.15) 2px
  );
}

#scanlines.active {
  opacity: 1;
}

#controls {
  display: flex;
  flex-wrap: wrap;
  gap: 8px;
  justify-content: center;
  margin-bottom: 12px;
  padding: 0 4px;
}

.ctrl-btn {
  font-family: 'VT323', monospace;
  font-size: 1rem;
  padding: 8px 14px;
  background: var(--key-bg);
  color: var(--text);
  border: 1px solid #444;
  border-radius: 6px;
  cursor: pointer;
  transition: all 0.15s;
  user-select: none;
  -webkit-user-select: none;
  text-shadow: 0 1px 2px rgba(0,0,0,0.5);
  box-shadow: 0 2px 0 #111, 0 3px 6px rgba(0,0,0,0.3);
  touch-action: manipulation;
  min-height: 44px;
}

.ctrl-btn:hover {
  background: var(--key-hover);
  transform: translateY(-1px);
}

.ctrl-btn:active {
  background: var(--key-active);
  transform: translateY(1px);
  box-shadow: 0 0 0 #111;
}

.ctrl-btn.active {
  border-color: var(--cyan);
  color: var(--cyan);
}

.ctrl-btn.running {
  border-color: var(--green);
  color: var(--green);
}

#keyboard-panel {
  width: 100%;
  max-width: 700px;
  margin-bottom: 12px;
  padding: 0 2px;
}

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

.kb-key {
  font-family: 'VT323', monospace;
  font-size: 0.95rem;
  min-width: 42px;
  height: 44px;
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  background: #2a2a35;
  color: #e0e0e0;
  border: 1px solid #3a3a4a;
  border-radius: 5px;
  cursor: pointer;
  user-select: none;
  -webkit-user-select: none;
  transition: all 0.1s;
  box-shadow: 0 2px 0 #111;
  position: relative;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}

.kb-key .sub {
  font-size: 0.55rem;
  color: #D70000;
  position: absolute;
  bottom: 2px;
  line-height: 1;
}

.kb-key:hover {
  background: #3a3a4a;
}

.kb-key.pressed {
  background: #5a5a7a;
  transform: translateY(1px);
  box-shadow: none;
  border-color: var(--cyan);
}

.kb-key.wide {
  min-width: 70px;
}

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

#debug-panel {
  width: 100%;
  max-width: 700px;
  background: #0a0a12;
  border: 1px solid #1a1a2a;
  border-radius: 8px;
  padding: 12px;
  margin-bottom: 12px;
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 12px;
  font-size: 0.75rem;
}

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

.debug-section h3 {
  color: var(--green);
  font-family: 'VT323', monospace;
  font-size: 1rem;
  margin-bottom: 6px;
  border-bottom: 1px solid #1a2a1a;
  padding-bottom: 3px;
}

.debug-section pre {
  font-family: 'Share Tech Mono', monospace;
  color: #00cc00;
  font-size: 0.72rem;
  line-height: 1.5;
  white-space: pre;
  overflow-x: auto;
}

#debug-flags {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.flag-ind {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  font-size: 0.65rem;
  font-weight: bold;
  border: 1px solid #333;
  background: #1a1a1a;
  color: #555;
}

.flag-ind.on {
  background: var(--green);
  color: #000;
  border-color: var(--green);
  box-shadow: 0 0 6px var(--green);
}

footer {
  text-align: center;
  padding: 16px;
  color: #555;
  font-size: 0.75rem;
  width: 100%;
}

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

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

/* CRT power-on animation */
@keyframes crt-on {
  0% { filter: brightness(0) blur(10px); transform: scaleY(0.01); }
  40% { filter: brightness(1.5) blur(2px); transform: scaleY(1.05); }
  60% { filter: brightness(0.8) blur(0); transform: scaleY(0.98); }
  100% { filter: brightness(1) blur(0); transform: scaleY(1); }
}

#canvas {
  animation: crt-on 0.8s ease-out forwards;
}

/* ========== RESPONSIVE ========== */

/* Tablet / medium screens */
@media (max-width: 720px) {
  #canvas {
    width: calc(100vw - 48px);
    height: auto;
    aspect-ratio: 320 / 256;
    max-width: 640px;
    max-height: 512px;
  }

  #crt-bezel {
    padding: 6px;
  }

  #scanlines {
    top: 6px; left: 6px; right: 6px; bottom: 6px;
  }

  #debug-panel {
    grid-template-columns: 1fr;
  }

  .kb-key {
    min-width: 32px;
    height: 38px;
    font-size: 0.8rem;
  }

  .kb-key.wide { min-width: 52px; }
  .kb-key.space { min-width: 140px; }

  header h1 { font-size: 1.8rem; }

  .ctrl-btn {
    font-size: 0.85rem;
    padding: 6px 10px;
    min-height: 40px;
  }
}

/* Phone screens */
@media (max-width: 420px) {
  #app { padding: 6px; }

  header h1 { font-size: 1.5rem; }
  header { margin-bottom: 8px; }

  .rainbow-bar { max-width: 250px; }

  .status-bar {
    gap: 8px;
    font-size: 0.7rem;
  }

  #screen-wrapper { margin-bottom: 8px; }

  #canvas {
    width: calc(100vw - 36px);
    height: auto;
    aspect-ratio: 320 / 256;
  }

  #crt-bezel {
    padding: 4px;
    border-radius: 8px;
    border-width: 2px;
  }

  #scanlines {
    top: 4px; left: 4px; right: 4px; bottom: 4px;
  }

  #controls {
    gap: 4px;
    margin-bottom: 8px;
  }

  .ctrl-btn {
    font-size: 0.75rem;
    padding: 5px 8px;
    min-height: 38px;
    border-radius: 4px;
  }

  .kb-row { gap: 2px; margin-bottom: 2px; }

  .kb-key {
    min-width: 26px;
    height: 34px;
    font-size: 0.7rem;
    border-radius: 3px;
    flex: 1;
    max-width: 40px;
  }

  .kb-key .sub {
    font-size: 0.45rem;
    bottom: 1px;
  }

  .kb-key.wide { 
    min-width: 38px; 
    max-width: 50px;
    flex: 1.4;
  }
  .kb-key.space { 
    min-width: 80px;
    max-width: 200px;
    flex: 4;
  }

  footer { padding: 10px; font-size: 0.65rem; }
}

/* Very small phones */
@media (max-width: 340px) {
  .kb-key {
    min-width: 22px;
    height: 30px;
    font-size: 0.6rem;
  }

  .kb-key .sub { display: none; }

  .ctrl-btn {
    font-size: 0.65rem;
    padding: 4px 6px;
  }
}