/* ========== RESET & BASE ========== */
* { box-sizing: border-box; margin: 0; padding: 0; }

:root {
  --bg-deep: #050816;
  --bg-panel: #0d1428;
  --bg-panel-2: #131c36;
  --border-cool: #1f2c50;
  --text-main: #d8e3ff;
  --text-dim: #8fa1c7;
  --accent-cyan: #00e5ff;
  --accent-magenta: #ff2d95;
  --accent-yellow: #ffd23f;
  --accent-green: #4ade80;
  --accent-red: #f87171;
  --wire-off: #2a3a66;
  --wire-on: #00e5ff;
  --glow-cyan: 0 0 12px #00e5ff, 0 0 24px rgba(0, 229, 255, 0.4);
  --glow-magenta: 0 0 12px #ff2d95, 0 0 24px rgba(255, 45, 149, 0.4);
  --glow-yellow: 0 0 8px #ffd23f, 0 0 16px rgba(255, 210, 63, 0.4);
}

body {
  font-family: 'Segoe UI', -apple-system, BlinkMacSystemFont, sans-serif;
  background: var(--bg-deep);
  color: var(--text-main);
  min-height: 100vh;
  overflow: hidden;
  background-image:
    radial-gradient(ellipse at top, rgba(0, 229, 255, 0.08) 0%, transparent 60%),
    radial-gradient(ellipse at bottom right, rgba(255, 45, 149, 0.06) 0%, transparent 60%);
}

#app {
  display: grid;
  grid-template-rows: 70px 1fr;
  height: 100vh;
  min-height: 600px;
}

/* ========== TOPBAR ========== */
#topbar {
  display: grid;
  grid-template-columns: 220px 1fr 200px 40px;
  align-items: center;
  gap: 16px;
  padding: 0 20px;
  background: linear-gradient(180deg, var(--bg-panel), var(--bg-panel-2));
  border-bottom: 1px solid var(--border-cool);
  box-shadow: 0 4px 24px rgba(0, 229, 255, 0.05);
}

#btn-help {
  width: 36px;
  height: 36px;
  border-radius: 50%;
  background: var(--bg-deep);
  border: 1px solid var(--accent-yellow);
  color: var(--accent-yellow);
  font-size: 18px;
  font-weight: 700;
  cursor: pointer;
  transition: all 0.2s;
  display: flex;
  align-items: center;
  justify-content: center;
}

#btn-help:hover {
  background: var(--accent-yellow);
  color: var(--bg-deep);
  box-shadow: var(--glow-yellow);
  transform: scale(1.05);
}

.logo {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 20px;
  font-weight: 700;
  letter-spacing: 1px;
}

.logo-icon {
  font-size: 24px;
  filter: drop-shadow(0 0 8px var(--accent-cyan));
}

.accent {
  color: var(--accent-cyan);
  text-shadow: var(--glow-cyan);
}

.level-display {
  text-align: center;
}

.level-num {
  font-size: 11px;
  color: var(--accent-yellow);
  letter-spacing: 2px;
  font-weight: 700;
}

.level-name {
  font-size: 18px;
  font-weight: 600;
  margin: 2px 0;
}

.level-desc {
  font-size: 12px;
  color: var(--text-dim);
}

.xp-bar {
  text-align: right;
}

.xp-label {
  font-size: 11px;
  color: var(--text-dim);
  margin-bottom: 4px;
  letter-spacing: 1px;
}

.xp-label span {
  color: var(--accent-cyan);
  font-weight: 700;
  font-size: 14px;
}

.xp-track {
  height: 8px;
  background: var(--bg-deep);
  border: 1px solid var(--border-cool);
  border-radius: 4px;
  overflow: hidden;
}

.xp-fill {
  height: 100%;
  width: 0%;
  background: linear-gradient(90deg, var(--accent-cyan), var(--accent-magenta));
  box-shadow: var(--glow-cyan);
  transition: width 0.6s ease;
}

/* ========== MAIN GAME AREA ========== */
#game-area {
  display: grid;
  grid-template-columns: 190px 1fr 270px;
  gap: 12px;
  padding: 12px;
  overflow: hidden;
  min-height: 0;
}

#game-area > * {
  min-height: 0;
  min-width: 0;
}

/* ========== PALETTE (LEFT) ========== */
#palette {
  background: var(--bg-panel);
  border: 1px solid var(--border-cool);
  border-radius: 8px;
  padding: 12px;
  overflow-y: auto;
}

#palette h3 {
  font-size: 11px;
  color: var(--text-dim);
  letter-spacing: 2px;
  margin-bottom: 8px;
  margin-top: 12px;
}

#palette h3:first-child { margin-top: 0; }

#palette-items {
  display: grid;
  gap: 8px;
}

.gate-btn {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 10px;
  background: var(--bg-panel-2);
  border: 1px solid var(--border-cool);
  border-radius: 6px;
  color: var(--text-main);
  cursor: pointer;
  transition: all 0.2s;
  font-size: 13px;
  font-weight: 600;
  text-align: left;
}

.gate-btn:hover {
  border-color: var(--accent-cyan);
  background: rgba(0, 229, 255, 0.08);
  transform: translateX(2px);
}

.gate-btn.active {
  border-color: var(--accent-cyan);
  background: rgba(0, 229, 255, 0.15);
  box-shadow: var(--glow-cyan);
}

.gate-btn .gate-symbol {
  width: 32px;
  height: 24px;
  display: flex;
  align-items: center;
  justify-content: center;
  background: var(--bg-deep);
  border-radius: 4px;
  border: 1px solid var(--border-cool);
  font-family: monospace;
  font-size: 11px;
  color: var(--accent-cyan);
  flex-shrink: 0;
}

.gate-btn .gate-name {
  flex: 1;
}

.gate-btn .info-icon {
  width: 20px;
  height: 20px;
  border-radius: 50%;
  background: var(--bg-deep);
  border: 1px solid var(--border-cool);
  color: var(--text-dim);
  font-size: 11px;
  display: flex;
  align-items: center;
  justify-content: center;
  cursor: pointer;
  transition: all 0.2s;
  flex-shrink: 0;
}

.gate-btn .info-icon:hover {
  background: var(--accent-yellow);
  color: var(--bg-deep);
  border-color: var(--accent-yellow);
  box-shadow: var(--glow-yellow);
  transform: scale(1.1);
}

.gate-btn:disabled,
.gate-btn.locked {
  opacity: 0.5;
}

.gate-btn.locked .gate-symbol,
.gate-btn.locked .gate-name {
  opacity: 0.5;
}

.gate-btn.locked:hover {
  border-color: var(--border-cool);
  background: var(--bg-panel-2);
  transform: none;
}

.gate-btn.locked .info-icon {
  opacity: 1;
}

.library-btn {
  width: 100%;
  margin-top: 10px;
  padding: 10px;
  background: linear-gradient(135deg, var(--bg-panel-2), var(--bg-deep));
  border: 1px solid var(--accent-magenta);
  border-radius: 6px;
  color: var(--accent-magenta);
  cursor: pointer;
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 1px;
  transition: all 0.2s;
}

.library-btn:hover {
  background: rgba(255, 45, 149, 0.15);
  box-shadow: var(--glow-magenta);
  transform: translateY(-1px);
}

.tools {
  display: flex;
  gap: 6px;
  flex-wrap: wrap;
}

.tool-btn {
  flex: 1;
  min-width: 40px;
  padding: 8px;
  background: var(--bg-panel-2);
  border: 1px solid var(--border-cool);
  border-radius: 6px;
  color: var(--text-main);
  cursor: pointer;
  font-size: 18px;
  transition: all 0.2s;
}

.tool-btn:hover {
  border-color: var(--accent-cyan);
}

.tool-btn.active {
  border-color: var(--accent-cyan);
  background: rgba(0, 229, 255, 0.15);
  box-shadow: var(--glow-cyan);
}

.hint-box {
  margin-top: 16px;
  padding: 10px;
  background: rgba(255, 210, 63, 0.05);
  border: 1px dashed rgba(255, 210, 63, 0.3);
  border-radius: 6px;
  font-size: 11px;
  color: var(--text-dim);
  line-height: 1.5;
}

.hint-box strong {
  color: var(--accent-yellow);
  display: block;
  margin-bottom: 4px;
}

/* ========== BOARD (CENTER) ========== */
#board-wrap {
  display: flex;
  flex-direction: column;
  gap: 12px;
  min-width: 0;
}

#board {
  flex: 1;
  background: var(--bg-panel);
  border: 1px solid var(--border-cool);
  border-radius: 8px;
  box-shadow: inset 0 0 60px rgba(0, 0, 0, 0.4);
  min-height: 0;
  cursor: crosshair;
}

#board.tool-select { cursor: default; }
#board.tool-delete { cursor: not-allowed; }

#board-toolbar {
  display: flex;
  gap: 8px;
  justify-content: center;
}

#board-toolbar button {
  padding: 10px 20px;
  background: var(--bg-panel);
  border: 1px solid var(--border-cool);
  border-radius: 6px;
  color: var(--text-main);
  cursor: pointer;
  font-size: 13px;
  font-weight: 600;
  letter-spacing: 1px;
  transition: all 0.2s;
}

#board-toolbar button:hover:not(:disabled) {
  border-color: var(--accent-cyan);
  transform: translateY(-1px);
}

#board-toolbar button.primary {
  background: linear-gradient(180deg, var(--accent-cyan), #00b8cc);
  color: #001520;
  border-color: var(--accent-cyan);
  box-shadow: var(--glow-cyan);
}

#board-toolbar button:disabled {
  opacity: 0.4;
  cursor: not-allowed;
}

/* ========== SVG COMPONENT STYLES ========== */
.gate-body {
  fill: var(--bg-panel-2);
  stroke: var(--accent-cyan);
  stroke-width: 2;
  transition: all 0.2s;
}

.gate-body.selected {
  stroke: var(--accent-yellow);
  filter: url(#strong-glow);
}

.gate-label {
  fill: var(--accent-cyan);
  font-family: monospace;
  font-size: 12px;
  font-weight: 700;
  text-anchor: middle;
  pointer-events: none;
  user-select: none;
}

.port {
  fill: var(--bg-panel);
  stroke: var(--text-dim);
  stroke-width: 2;
  transition: all 0.15s;
}

.port-group { cursor: pointer; }

.port-group:hover .port {
  fill: var(--accent-cyan);
  stroke: var(--accent-cyan);
  r: 11;
}

.port-group:active .port {
  r: 13;
}

.port.on {
  fill: var(--accent-cyan);
  stroke: var(--accent-cyan);
  filter: url(#glow);
}

.port.wire-source {
  fill: var(--accent-yellow);
  stroke: var(--accent-yellow);
  filter: url(#glow);
}

.io-node {
  fill: var(--bg-panel-2);
  stroke: var(--accent-magenta);
  stroke-width: 2;
  cursor: pointer;
  transition: all 0.2s;
}

.io-node.on {
  fill: var(--accent-magenta);
  filter: url(#glow);
}

.io-label {
  fill: var(--accent-magenta);
  font-family: monospace;
  font-size: 14px;
  font-weight: 700;
  text-anchor: middle;
  pointer-events: none;
  user-select: none;
}

.io-value {
  font-family: monospace;
  font-size: 22px;
  font-weight: 800;
  pointer-events: none;
  user-select: none;
  transition: fill 0.2s, filter 0.2s;
}

.io-value.on {
  fill: var(--accent-cyan);
  filter: url(#glow);
}

.io-value.off {
  fill: #3a4a72;
}

.wire {
  fill: none;
  stroke: var(--wire-off);
  stroke-width: 3;
  stroke-linecap: round;
  pointer-events: stroke;
  cursor: pointer;
  transition: stroke 0.3s;
}

.wire:hover {
  stroke: var(--accent-red);
}

.wire.on {
  stroke: var(--wire-on);
  filter: url(#glow);
  stroke-dasharray: 10 4;
  animation: wire-flow 0.6s linear infinite;
}

@keyframes wire-flow {
  to { stroke-dashoffset: -14; }
}

@keyframes wire-flow-fast {
  to { stroke-dashoffset: -14; }
}

.wire-preview {
  fill: none;
  stroke: var(--accent-yellow);
  stroke-width: 2;
  stroke-dasharray: 4 4;
  pointer-events: none;
  opacity: 0.6;
}

/* ========== RIGHT PANEL ========== */
#right-panel {
  display: flex;
  flex-direction: column;
  gap: 10px;
  overflow-y: auto;
  overflow-x: hidden;
  padding-right: 4px;
  max-height: 100%;
}

#truth-section, #neo-section {
  background: var(--bg-panel);
  border: 1px solid var(--border-cool);
  border-radius: 8px;
  padding: 10px;
  flex-shrink: 0;
}

#right-panel h3 {
  font-size: 11px;
  color: var(--text-dim);
  letter-spacing: 2px;
  margin-bottom: 10px;
}

#truth-table {
  width: 100%;
  font-family: monospace;
  font-size: 12px;
}

#truth-table table {
  width: 100%;
  border-collapse: collapse;
}

#truth-table th, #truth-table td {
  padding: 6px;
  text-align: center;
  border-bottom: 1px solid var(--border-cool);
}

#truth-table th {
  color: var(--accent-magenta);
  font-size: 11px;
  letter-spacing: 1px;
}

#truth-table td.input-val { color: var(--text-dim); }
#truth-table td.output-val { color: var(--accent-cyan); font-weight: 700; }
#truth-table tr.passed td { background: rgba(74, 222, 128, 0.08); }
#truth-table tr.failed td { background: rgba(248, 113, 113, 0.08); }
#truth-table td .check { color: var(--accent-green); margin-left: 4px; }
#truth-table td .cross { color: var(--accent-red); margin-left: 4px; }

/* ========== NEO COMPANION ========== */
#neo-stage {
  position: relative;
  height: 160px;
  display: flex;
  align-items: center;
  justify-content: center;
  margin-bottom: 10px;
  background: radial-gradient(ellipse at center, rgba(0, 229, 255, 0.08) 0%, transparent 70%);
  border-radius: 6px;
  transition: background 0.3s;
}

#neo-stage:hover {
  background: radial-gradient(ellipse at center, rgba(0, 229, 255, 0.15) 0%, transparent 70%);
}

#neo-stage.neo-happy {
  animation: neo-bounce 0.45s ease-in-out 2;
  background: radial-gradient(ellipse at center, rgba(74, 222, 128, 0.25) 0%, transparent 70%);
}

#neo-stage.neo-sad {
  animation: neo-shake 0.25s ease-in-out 3;
  background: radial-gradient(ellipse at center, rgba(248, 113, 113, 0.2) 0%, transparent 70%);
}

@keyframes neo-bounce {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-8px); }
}

@keyframes neo-shake {
  0%, 100% { transform: translateX(0); }
  25% { transform: translateX(-5px); }
  75% { transform: translateX(5px); }
}

#neo-svg {
  width: 130px;
  height: 160px;
}

#neo-bubble {
  position: absolute;
  top: 10px;
  right: 5px;
  background: var(--accent-cyan);
  color: var(--bg-deep);
  padding: 6px 12px;
  border-radius: 16px 16px 16px 4px;
  font-size: 13px;
  font-weight: 600;
  box-shadow: var(--glow-cyan);
  animation: bubble-in 0.4s ease;
}

@keyframes bubble-in {
  from { opacity: 0; transform: translateY(-10px) scale(0.8); }
  to { opacity: 1; transform: translateY(0) scale(1); }
}

#neo-abilities {
  display: grid;
  grid-template-columns: 1fr 1fr 1fr;
  gap: 4px;
  margin-top: 6px;
}

.ability-row {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 2px;
  padding: 4px 2px;
  background: var(--bg-panel-2);
  border-radius: 4px;
  font-size: 10px;
  text-align: center;
  opacity: 0.4;
  transition: all 0.4s;
}

.ability-row.unlocked {
  opacity: 1;
  background: rgba(0, 229, 255, 0.1);
  box-shadow: inset 0 0 8px rgba(0, 229, 255, 0.2);
}

.ability-icon {
  font-size: 14px;
  filter: grayscale(1);
  transition: filter 0.4s;
}

.ability-row.unlocked .ability-icon {
  filter: grayscale(0);
}

#neo-playground {
  margin-top: 12px;
  padding: 10px;
  background: var(--bg-deep);
  border: 1px solid var(--accent-magenta);
  border-radius: 6px;
}

#neo-playground h4 {
  color: var(--accent-magenta);
  font-size: 12px;
  margin-bottom: 8px;
}

/* ========== MODAL ========== */
#modal {
  position: fixed;
  inset: 0;
  display: flex;
  align-items: center;
  justify-content: center;
  z-index: 100;
}

#modal.hidden { display: none; }

.modal-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(5, 8, 22, 0.85);
  backdrop-filter: blur(8px);
}

.modal-card {
  position: relative;
  background: linear-gradient(135deg, var(--bg-panel), var(--bg-panel-2));
  border: 2px solid var(--accent-cyan);
  border-radius: 12px;
  padding: 32px 40px;
  max-width: 500px;
  text-align: center;
  box-shadow: 0 0 60px rgba(0, 229, 255, 0.4);
  animation: modal-in 0.5s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes modal-in {
  from { transform: scale(0.7); opacity: 0; }
  to { transform: scale(1); opacity: 1; }
}

.modal-card h2 {
  font-size: 28px;
  color: var(--accent-cyan);
  text-shadow: var(--glow-cyan);
  margin-bottom: 12px;
}

#modal-body {
  font-size: 14px;
  line-height: 1.6;
  color: var(--text-main);
  margin-bottom: 20px;
}

#modal-body .reward-line {
  font-size: 16px;
  color: var(--accent-yellow);
  margin: 12px 0;
  font-weight: 600;
}

.modal-actions button {
  padding: 12px 32px;
  background: linear-gradient(180deg, var(--accent-cyan), #00b8cc);
  color: var(--bg-deep);
  border: none;
  border-radius: 6px;
  font-size: 14px;
  font-weight: 700;
  letter-spacing: 1px;
  cursor: pointer;
  box-shadow: var(--glow-cyan);
}

.modal-confetti {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: hidden;
}

.confetti-piece {
  position: absolute;
  width: 8px;
  height: 8px;
  animation: confetti-fall 2s ease-out forwards;
}

@keyframes confetti-fall {
  0% {
    transform: translateY(-20px) rotate(0deg);
    opacity: 1;
  }
  100% {
    transform: translateY(500px) rotate(720deg);
    opacity: 0;
  }
}

/* ========== TOAST ========== */
#toast {
  position: fixed;
  bottom: 24px;
  left: 50%;
  transform: translateX(-50%);
  padding: 12px 24px;
  background: var(--bg-panel);
  border: 1px solid var(--accent-cyan);
  border-radius: 8px;
  font-size: 14px;
  z-index: 50;
  box-shadow: var(--glow-cyan);
  animation: toast-in 0.3s ease;
}

#toast.hidden { display: none; }
#toast.error { border-color: var(--accent-red); box-shadow: 0 0 12px var(--accent-red); }

@keyframes toast-in {
  from { transform: translate(-50%, 20px); opacity: 0; }
  to { transform: translate(-50%, 0); opacity: 1; }
}

/* ========== UTILITY ========== */
.hidden { display: none; }

/* ========== WELCOME MODAL ========== */
#welcome-modal {
  position: fixed;
  inset: 0;
  z-index: 300;
  display: flex;
  align-items: center;
  justify-content: center;
}

#welcome-modal.hidden { display: none; }

.welcome-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(5, 8, 22, 0.92);
  backdrop-filter: blur(12px);
}

.welcome-card {
  position: relative;
  width: 92vw;
  max-width: 720px;
  max-height: 92vh;
  background: linear-gradient(135deg, var(--bg-panel), var(--bg-panel-2));
  border: 2px solid var(--accent-cyan);
  border-radius: 16px;
  box-shadow:
    0 0 60px rgba(0, 229, 255, 0.4),
    0 0 120px rgba(255, 45, 149, 0.15);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  animation: welcome-in 0.5s cubic-bezier(0.34, 1.56, 0.64, 1);
}

@keyframes welcome-in {
  from { opacity: 0; transform: scale(0.8) translateY(20px); }
  to { opacity: 1; transform: scale(1) translateY(0); }
}

.welcome-close {
  position: absolute;
  top: 12px;
  right: 12px;
  width: 32px;
  height: 32px;
  border-radius: 50%;
  background: rgba(5, 8, 22, 0.6);
  border: 1px solid var(--border-cool);
  color: var(--text-dim);
  font-size: 14px;
  cursor: pointer;
  z-index: 10;
  transition: all 0.2s;
}

.welcome-close:hover {
  background: var(--accent-red);
  color: white;
  border-color: var(--accent-red);
}

.welcome-hero {
  text-align: center;
  padding: 28px 24px 20px;
  background:
    radial-gradient(ellipse at top, rgba(0, 229, 255, 0.15), transparent 70%),
    linear-gradient(180deg, transparent, rgba(255, 45, 149, 0.05));
  border-bottom: 1px solid var(--border-cool);
}

.welcome-icon {
  font-size: 48px;
  margin-bottom: 8px;
  filter: drop-shadow(0 0 12px var(--accent-cyan));
  animation: hero-float 3s ease-in-out infinite;
}

@keyframes hero-float {
  0%, 100% { transform: translateY(0); }
  50% { transform: translateY(-5px); }
}

.welcome-title {
  font-size: 32px;
  font-weight: 800;
  letter-spacing: 4px;
  margin-bottom: 4px;
}

.welcome-accent {
  color: var(--accent-cyan);
  text-shadow: var(--glow-cyan);
}

.welcome-tagline {
  font-size: 13px;
  color: var(--text-dim);
  letter-spacing: 1px;
}

.welcome-body {
  flex: 1;
  overflow-y: auto;
  padding: 20px 28px;
}

.welcome-section {
  margin-bottom: 22px;
}

.welcome-section h3 {
  font-size: 12px;
  color: var(--accent-yellow);
  letter-spacing: 2px;
  margin-bottom: 10px;
  font-weight: 700;
  text-shadow: 0 0 8px rgba(255, 210, 63, 0.4);
}

.welcome-section p {
  font-size: 13.5px;
  line-height: 1.7;
  color: var(--text-main);
}

.welcome-goals {
  list-style: none;
  padding: 0;
}

.welcome-goals li {
  font-size: 13.5px;
  line-height: 1.7;
  padding: 4px 0 4px 24px;
  position: relative;
}

.welcome-goals li::before {
  content: '✓';
  position: absolute;
  left: 0;
  color: var(--accent-green);
  font-weight: 700;
}

.welcome-steps {
  list-style: none;
  padding: 0;
  counter-reset: step;
  display: grid;
  gap: 8px;
}

.welcome-steps li {
  display: grid;
  grid-template-columns: 32px 1fr;
  align-items: start;
  gap: 12px;
  padding: 10px 12px;
  background: var(--bg-deep);
  border: 1px solid var(--border-cool);
  border-radius: 8px;
  transition: all 0.2s;
}

.welcome-steps li:hover {
  border-color: var(--accent-cyan);
  transform: translateX(2px);
}

.step-num {
  width: 28px;
  height: 28px;
  border-radius: 50%;
  background: var(--accent-cyan);
  color: var(--bg-deep);
  font-weight: 800;
  font-size: 14px;
  display: flex;
  align-items: center;
  justify-content: center;
  flex-shrink: 0;
  box-shadow: var(--glow-cyan);
}

.step-content {
  font-size: 13px;
  line-height: 1.6;
  padding-top: 4px;
}

.dot-out {
  color: var(--accent-yellow);
  font-size: 16px;
  text-shadow: 0 0 6px var(--accent-yellow);
}

.dot-in {
  color: var(--accent-cyan);
  font-size: 16px;
  text-shadow: 0 0 6px var(--accent-cyan);
}

.welcome-tips {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px;
}

.tip {
  display: flex;
  gap: 10px;
  align-items: start;
  padding: 10px;
  background: rgba(255, 210, 63, 0.05);
  border: 1px dashed rgba(255, 210, 63, 0.3);
  border-radius: 6px;
  font-size: 12px;
  line-height: 1.5;
}

.tip-icon {
  font-size: 18px;
  flex-shrink: 0;
}

.tip strong {
  color: var(--accent-yellow);
}

.welcome-actions {
  padding: 16px 28px 24px;
  text-align: center;
  border-top: 1px solid var(--border-cool);
  background: rgba(0, 0, 0, 0.2);
}

.welcome-start {
  padding: 14px 48px;
  background: linear-gradient(135deg, var(--accent-cyan), var(--accent-magenta));
  color: var(--bg-deep);
  border: none;
  border-radius: 8px;
  font-size: 15px;
  font-weight: 800;
  letter-spacing: 2px;
  cursor: pointer;
  box-shadow:
    var(--glow-cyan),
    0 4px 20px rgba(255, 45, 149, 0.3);
  transition: all 0.2s;
}

.welcome-start:hover {
  transform: translateY(-2px) scale(1.02);
  box-shadow:
    0 0 24px var(--accent-cyan),
    0 6px 30px rgba(255, 45, 149, 0.5);
}

.welcome-reset {
  display: block;
  margin: 12px auto 0;
  padding: 6px 18px;
  background: transparent;
  color: var(--text-dim);
  border: 1px solid var(--border-cool);
  border-radius: 6px;
  font-size: 11px;
  cursor: pointer;
  transition: all 0.2s;
}

.welcome-reset:hover {
  border-color: var(--accent-red);
  color: var(--accent-red);
  background: rgba(248, 113, 113, 0.08);
}

@media (max-width: 600px) {
  .welcome-tips { grid-template-columns: 1fr; }
  .welcome-title { font-size: 24px; }
  .welcome-hero { padding: 20px 16px 16px; }
  .welcome-body { padding: 16px; }
}

/* ========== LIBRARY MODAL ========== */
#library-modal {
  position: fixed;
  inset: 0;
  z-index: 200;
  display: flex;
  align-items: center;
  justify-content: center;
}

#library-modal.hidden { display: none; }

.library-backdrop {
  position: absolute;
  inset: 0;
  background: rgba(5, 8, 22, 0.92);
  backdrop-filter: blur(10px);
}

.library-card {
  position: relative;
  width: 90vw;
  max-width: 900px;
  height: 85vh;
  max-height: 700px;
  background: linear-gradient(135deg, var(--bg-panel), var(--bg-panel-2));
  border: 2px solid var(--accent-magenta);
  border-radius: 12px;
  box-shadow: 0 0 60px rgba(255, 45, 149, 0.4);
  display: flex;
  flex-direction: column;
  overflow: hidden;
  animation: modal-in 0.4s cubic-bezier(0.34, 1.56, 0.64, 1);
}

.library-header {
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 16px 20px;
  border-bottom: 1px solid var(--border-cool);
  background: var(--bg-deep);
  gap: 12px;
}

.library-header h2 {
  font-size: 18px;
  color: var(--accent-magenta);
  text-shadow: var(--glow-magenta);
  flex: 1;
  text-align: center;
}

.lib-back-btn, .lib-close-btn {
  background: var(--bg-panel);
  border: 1px solid var(--border-cool);
  color: var(--text-main);
  padding: 6px 12px;
  border-radius: 6px;
  cursor: pointer;
  font-size: 13px;
  transition: all 0.2s;
}

.lib-back-btn:hover, .lib-close-btn:hover {
  border-color: var(--accent-cyan);
  color: var(--accent-cyan);
}

.lib-close-btn {
  width: 36px;
  height: 36px;
  font-size: 16px;
  padding: 0;
}

.lib-back-btn.hidden { display: none; }

#lib-content {
  flex: 1;
  overflow-y: auto;
  padding: 20px;
}

/* ===== Library grid view ===== */
.lib-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(240px, 1fr));
  gap: 14px;
}

.lib-section-title {
  font-size: 12px;
  color: var(--text-dim);
  letter-spacing: 2px;
  margin: 18px 0 10px;
  padding-bottom: 4px;
  border-bottom: 1px solid var(--border-cool);
}

.lib-section-title:first-child { margin-top: 0; }

.lib-gate-card {
  background: var(--bg-panel-2);
  border: 1px solid var(--border-cool);
  border-radius: 8px;
  padding: 16px;
  cursor: pointer;
  transition: all 0.2s;
}

.lib-gate-card:hover {
  border-color: var(--accent-cyan);
  transform: translateY(-2px);
  box-shadow: 0 8px 24px rgba(0, 229, 255, 0.15);
}

.lib-gate-card-head {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-bottom: 10px;
}

.lib-gate-card-symbol {
  width: 44px;
  height: 44px;
  border-radius: 8px;
  background: var(--bg-deep);
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: monospace;
  font-size: 20px;
  font-weight: 700;
  border: 2px solid;
}

.lib-gate-card-title {
  flex: 1;
}

.lib-gate-card-name {
  font-size: 16px;
  font-weight: 700;
}

.lib-gate-card-namevi {
  font-size: 12px;
  color: var(--text-dim);
}

.lib-gate-card-desc {
  font-size: 12px;
  line-height: 1.5;
  color: var(--text-main);
}

.lib-gate-card-meta {
  margin-top: 10px;
  font-size: 11px;
  color: var(--text-dim);
}

/* ===== Detail view ===== */
.lib-detail {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 24px;
  height: 100%;
}

.lib-detail-left h3 {
  font-size: 22px;
  color: var(--accent-cyan);
  margin-bottom: 4px;
}

.lib-detail-namevi {
  font-size: 13px;
  color: var(--text-dim);
  margin-bottom: 12px;
}

.lib-detail-section {
  margin-bottom: 16px;
}

.lib-detail-section h4 {
  font-size: 11px;
  color: var(--accent-magenta);
  letter-spacing: 2px;
  margin-bottom: 6px;
}

.lib-detail-section p {
  font-size: 13px;
  line-height: 1.6;
  color: var(--text-main);
}

.lib-detail-section ul {
  font-size: 13px;
  line-height: 1.7;
  color: var(--text-main);
  padding-left: 18px;
}

.lib-analogy {
  padding: 12px;
  background: rgba(255, 210, 63, 0.05);
  border-left: 3px solid var(--accent-yellow);
  border-radius: 4px;
  font-size: 13px;
  line-height: 1.6;
}

/* Interactive demo */
.lib-demo {
  background: var(--bg-deep);
  border: 1px solid var(--border-cool);
  border-radius: 8px;
  padding: 16px;
  display: flex;
  flex-direction: column;
  gap: 12px;
}

.lib-demo h4 {
  font-size: 11px;
  color: var(--accent-yellow);
  letter-spacing: 2px;
}

.lib-demo-stage {
  background: var(--bg-panel);
  border-radius: 6px;
  padding: 16px;
  display: flex;
  align-items: center;
  justify-content: center;
  min-height: 180px;
}

.lib-demo-stage svg {
  width: 100%;
  height: 180px;
}

.lib-demo-hint {
  font-size: 11px;
  color: var(--text-dim);
  text-align: center;
  font-style: italic;
}

.lib-truth {
  background: var(--bg-panel);
  border-radius: 6px;
  padding: 12px;
}

.lib-truth table {
  width: 100%;
  border-collapse: collapse;
  font-family: monospace;
  font-size: 13px;
}

.lib-truth th, .lib-truth td {
  padding: 6px 10px;
  text-align: center;
  border-bottom: 1px solid var(--border-cool);
}

.lib-truth th {
  color: var(--accent-magenta);
  font-size: 11px;
  letter-spacing: 1px;
}

.lib-truth td.input-val { color: var(--text-dim); }
.lib-truth td.output-val { color: var(--accent-cyan); font-weight: 700; }
.lib-truth tr.active td {
  background: rgba(255, 210, 63, 0.12);
  box-shadow: inset 3px 0 0 var(--accent-yellow);
}

/* Demo SVG interactive bits */
.demo-bit {
  cursor: pointer;
  transition: all 0.2s;
}
.demo-bit.on .demo-bit-bg { fill: var(--accent-cyan); filter: url(#libGlow); }
.demo-bit.off .demo-bit-bg { fill: var(--bg-panel-2); }
.demo-bit-label {
  fill: var(--text-main);
  font-family: monospace;
  font-weight: 700;
  font-size: 16px;
  text-anchor: middle;
  pointer-events: none;
  user-select: none;
}
.demo-bit.on .demo-bit-label { fill: var(--bg-deep); }

.demo-wire {
  fill: none;
  stroke: var(--wire-off);
  stroke-width: 3;
  stroke-linecap: round;
  transition: stroke 0.3s;
}
.demo-wire.on {
  stroke: var(--accent-cyan);
  filter: url(#libGlow);
  stroke-dasharray: 10 4;
  animation: wire-flow 0.6s linear infinite;
}

.demo-gate {
  fill: var(--bg-panel-2);
  stroke-width: 2;
}

.demo-gate-label {
  fill: var(--accent-cyan);
  font-family: monospace;
  font-weight: 700;
  font-size: 14px;
  text-anchor: middle;
  pointer-events: none;
}

@media (max-width: 700px) {
  .lib-detail { grid-template-columns: 1fr; }
}

/* ========== SCROLLBAR ========== */
::-webkit-scrollbar { width: 6px; height: 6px; }
::-webkit-scrollbar-track { background: var(--bg-deep); }
::-webkit-scrollbar-thumb { background: var(--border-cool); border-radius: 3px; }
::-webkit-scrollbar-thumb:hover { background: var(--accent-cyan); }

/* ========== RESPONSIVE: TABLET (≤900px) ========== */
@media (max-width: 900px) {
  #game-area {
    grid-template-columns: 170px 1fr;
    grid-template-rows: 1fr auto;
  }

  #right-panel {
    grid-column: 1 / -1;
    grid-row: 2;
    flex-direction: row;
    gap: 10px;
    overflow-x: auto;
    overflow-y: hidden;
    max-height: 220px;
  }

  #truth-section, #neo-section {
    flex-shrink: 0;
    width: 280px;
  }

  #neo-stage { height: 130px; }
  #neo-svg { width: 110px; height: 130px; }

  .level-desc { font-size: 11px; }
}

/* ========== RESPONSIVE: MOBILE (≤700px) ========== */
@media (max-width: 700px) {
  /* Allow vertical page scroll instead of fixed viewport */
  body { overflow-y: auto; overflow-x: hidden; }
  #app {
    height: auto;
    min-height: 100vh;
    width: 100%;
    max-width: 100vw;
    grid-template-rows: auto 1fr;
    overflow-x: hidden;
  }
  #app > * {
    min-width: 0;
    max-width: 100%;
  }
  #topbar { overflow: hidden; }

  /* ----- Topbar: condense to one row ----- */
  #topbar {
    grid-template-columns: auto minmax(0, 1fr) auto auto;
    grid-template-rows: auto;
    height: auto;
    min-height: 56px;
    padding: 8px 10px;
    gap: 8px;
  }
  .logo { font-size: 14px; gap: 6px; }
  .logo-icon { font-size: 18px; }
  .level-display {
    text-align: left;
    min-width: 0;
    overflow: hidden;
  }
  .level-num { font-size: 9px; display: inline-block; margin-right: 6px; }
  .level-name {
    font-size: 13px;
    display: block;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    min-width: 0;
  }
  .level-desc { display: none; }
  .xp-bar { text-align: right; min-width: 0; }
  .xp-label { font-size: 9px; margin-bottom: 2px; }
  .xp-track { width: 70px; height: 6px; }
  #btn-help { width: 32px; height: 32px; font-size: 15px; }

  /* ----- Game area: single column stack ----- */
  #game-area {
    display: flex;
    flex-direction: column;
    gap: 8px;
    padding: 8px;
    overflow: visible;
    height: auto;
    width: 100%;
    max-width: 100%;
  }
  #game-area > * {
    width: 100%;
    max-width: 100%;
    min-width: 0;
  }

  /* ----- Palette: gates wrap to 2 columns ----- */
  #palette {
    order: 1;
    padding: 10px;
    overflow: hidden;
  }
  #palette h3 { font-size: 10px; margin: 0 0 6px 0; }
  #palette h3:nth-of-type(2) { margin-top: 10px; }

  #palette-items {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 6px;
  }
  .gate-btn {
    min-width: 0;
    padding: 8px;
    font-size: 12px;
  }
  .gate-btn .gate-name { font-size: 12px; }
  .gate-btn .info-icon { width: 18px; height: 18px; font-size: 10px; }

  .library-btn { margin-top: 8px; padding: 8px; font-size: 11px; }

  .tools { gap: 4px; }
  .tool-btn { padding: 6px; font-size: 16px; }
  .hint-box { display: none; }

  /* ----- Board: takes large vertical space ----- */
  #board-wrap { order: 2; }
  #board {
    min-height: 55vh;
    height: 55vh;
  }

  #board-toolbar {
    flex-wrap: wrap;
    gap: 6px;
  }
  #board-toolbar button {
    padding: 8px 12px;
    font-size: 11px;
    flex: 1 1 auto;
    min-width: 70px;
    letter-spacing: 0.5px;
  }

  /* ----- Right panel: stack at bottom, full width ----- */
  #right-panel {
    order: 3;
    flex-direction: column;
    overflow: visible;
    max-height: none;
    padding-right: 0;
  }
  #truth-section, #neo-section {
    width: auto;
    flex-shrink: 0;
    padding: 10px;
  }

  /* ----- Truth table: compact font ----- */
  #truth-table { font-size: 11px; }
  #truth-table th, #truth-table td { padding: 4px; font-size: 11px; }

  /* ----- NEO: more compact in mobile ----- */
  #neo-stage { height: 140px; }
  #neo-svg { width: 120px; height: 140px; }
  #neo-abilities {
    grid-template-columns: repeat(4, 1fr);
    gap: 4px;
  }
  .ability-row { font-size: 9px; padding: 3px 2px; }

  /* ----- Modals adapt ----- */
  .welcome-card { width: 96vw; max-height: 92vh; }
  .welcome-title { font-size: 22px; letter-spacing: 2px; }
  .welcome-icon { font-size: 36px; }
  .welcome-hero { padding: 16px 12px 12px; }
  .welcome-body { padding: 14px; }
  .welcome-steps li { padding: 8px 10px; gap: 8px; }
  .step-num { width: 24px; height: 24px; font-size: 12px; }
  .step-content { font-size: 12px; }

  .library-card { width: 96vw; height: 92vh; }
  .library-header h2 { font-size: 14px; }
  .library-header { padding: 10px 14px; }
  #lib-content { padding: 14px; }
  .lib-grid { grid-template-columns: 1fr; }
  .lib-detail { grid-template-columns: 1fr; gap: 14px; }

  .modal-card { padding: 20px 18px; max-width: 92vw; }
  .modal-card h2 { font-size: 20px; }
}

/* ========== RESPONSIVE: VERY SMALL (≤400px) ========== */
@media (max-width: 400px) {
  .logo-text { display: none; }
  .level-name { font-size: 12px; }
  #neo-abilities { grid-template-columns: repeat(3, 1fr); }
  .gate-btn { min-width: 84px; }
  #board { min-height: 50vh; height: 50vh; }
}
