:root {
  color-scheme: dark;
  --bg: #121417;
  --panel: #1d2329;
  --panel-2: #222a31;
  --line: #34414d;
  --text: #edf3f7;
  --muted: #98a8b4;
  --accent: #2ec4a6;
  --accent-2: #f2c14e;
  --danger: #ff6b6b;
  --good: #72df8f;
  --shadow: rgba(0, 0, 0, 0.28);
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  min-height: 100vh;
  background:
    linear-gradient(135deg, rgba(46, 196, 166, 0.08), transparent 28%),
    radial-gradient(circle at 85% 12%, rgba(242, 193, 78, 0.12), transparent 26%),
    var(--bg);
  color: var(--text);
  font-family:
    Inter, ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, "Segoe UI",
    sans-serif;
}

button {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: #26313a;
  color: var(--text);
  cursor: pointer;
  font: inherit;
  font-weight: 700;
  min-height: 40px;
  padding: 0 14px;
  transition:
    border-color 120ms ease,
    background 120ms ease,
    transform 120ms ease;
}

button:hover {
  border-color: var(--accent);
  transform: translateY(-1px);
}

button.active,
button.primary {
  background: var(--accent);
  border-color: var(--accent);
  color: #071310;
}

button.quiet {
  background: transparent;
}

.app-shell {
  display: grid;
  gap: 18px;
  min-height: 100vh;
  padding: 20px;
}

.topbar {
  align-items: center;
  display: flex;
  justify-content: space-between;
  gap: 18px;
}

.eyebrow {
  color: var(--accent-2);
  font-size: 0.78rem;
  font-weight: 800;
  letter-spacing: 0.08em;
  margin: 0 0 4px;
  text-transform: uppercase;
}

h1,
h2,
p {
  margin: 0;
}

h1 {
  font-size: clamp(2rem, 4vw, 4.3rem);
  line-height: 0.95;
}

h2 {
  font-size: 1rem;
  margin-bottom: 14px;
}

.level-actions {
  align-items: center;
  display: grid;
  grid-template-columns: 44px minmax(170px, 260px) 44px;
  gap: 10px;
}

.level-actions button {
  font-size: 1.8rem;
  padding: 0;
}

.level-title {
  border: 1px solid var(--line);
  border-radius: 8px;
  background: rgba(29, 35, 41, 0.85);
  padding: 10px 14px;
  text-align: center;
}

.level-title span {
  color: var(--muted);
  display: block;
  font-size: 0.78rem;
  font-weight: 800;
  text-transform: uppercase;
}

.game-layout {
  display: grid;
  grid-template-columns: 220px minmax(520px, 1fr) 280px;
  gap: 18px;
  min-height: 0;
}

.panel,
.board-wrap {
  background: rgba(29, 35, 41, 0.9);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 18px 45px var(--shadow);
}

.panel {
  align-self: stretch;
  padding: 16px;
}

.truth {
  max-height: calc(100vh - 130px);
  overflow: auto;
}

.gate-palette {
  display: grid;
  gap: 10px;
}

.gate-button {
  align-items: center;
  display: flex;
  justify-content: space-between;
  width: 100%;
}

.gate-button small {
  color: var(--muted);
  font-weight: 800;
}

.tool-row {
  display: grid;
  gap: 8px;
  grid-template-columns: 1fr 1fr;
  margin: 18px 0 8px;
}

.wide,
.quiet {
  width: 100%;
}

.board-wrap {
  display: grid;
  grid-template-rows: auto minmax(520px, 1fr);
  overflow: hidden;
}

.status-strip {
  align-items: center;
  border-bottom: 1px solid var(--line);
  display: flex;
  justify-content: space-between;
  gap: 16px;
  min-height: 68px;
  padding: 12px 16px;
}

.status-label {
  color: var(--muted);
  display: block;
  font-size: 0.76rem;
  font-weight: 800;
  text-transform: uppercase;
}

.run-status {
  border: 1px solid var(--line);
  border-radius: 999px;
  color: var(--muted);
  font-weight: 800;
  min-width: 120px;
  padding: 8px 12px;
  text-align: center;
}

.run-status.good {
  border-color: rgba(114, 223, 143, 0.7);
  color: var(--good);
}

.run-status.bad {
  border-color: rgba(255, 107, 107, 0.8);
  color: var(--danger);
}

.board {
  background-image:
    linear-gradient(rgba(255, 255, 255, 0.04) 1px, transparent 1px),
    linear-gradient(90deg, rgba(255, 255, 255, 0.04) 1px, transparent 1px);
  background-size: 28px 28px;
  min-height: 520px;
  overflow: hidden;
  position: relative;
}

.wire-layer {
  inset: 0;
  pointer-events: none;
  position: absolute;
  z-index: 1;
}

.wire {
  fill: none;
  stroke: #7fb7ff;
  stroke-linecap: round;
  stroke-width: 4;
}

.wire.on {
  stroke: var(--accent-2);
  filter: drop-shadow(0 0 5px rgba(242, 193, 78, 0.65));
}

.wire.preview {
  opacity: 0.75;
  stroke-dasharray: 8 8;
}

.node {
  align-items: center;
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: 0 12px 28px var(--shadow);
  display: grid;
  min-height: 76px;
  min-width: 122px;
  padding: 14px 18px;
  place-items: center;
  position: absolute;
  touch-action: none;
  user-select: none;
  z-index: 2;
}

.node.gate {
  cursor: grab;
}

.node.gate:active {
  cursor: grabbing;
}

.node.input,
.node.output {
  background: #172027;
}

.node.output.satisfied {
  border-color: var(--good);
}

.node.output.failed {
  border-color: var(--danger);
}

.node-title {
  font-size: 1.08rem;
  font-weight: 900;
  pointer-events: none;
}

.node-value {
  color: var(--muted);
  font-size: 0.9rem;
  font-weight: 800;
  pointer-events: none;
}

.port {
  border: 2px solid #0b1115;
  border-radius: 50%;
  cursor: crosshair;
  height: 24px;
  min-height: 24px;
  padding: 0;
  position: absolute;
  top: 50%;
  transform: translateY(-50%);
  width: 24px;
  z-index: 3;
}

.port.input-port {
  background: #88a2b4;
  left: -12px;
}

.port.output-port {
  background: var(--accent);
  right: -12px;
}

.port.active {
  outline: 3px solid rgba(242, 193, 78, 0.45);
}

.port[data-index="1"] {
  top: 70%;
}

.port[data-index="0"][data-total="2"] {
  top: 30%;
}

.truth-table {
  overflow-x: auto;
}

.truth-table table {
  border-collapse: collapse;
  font-size: 0.92rem;
  table-layout: fixed;
  width: 100%;
}

.truth-table th,
.truth-table td {
  border-bottom: 1px solid rgba(255, 255, 255, 0.08);
  font-weight: 800;
  height: 34px;
  padding: 7px 8px;
  text-align: center;
}

.truth-table th {
  background: rgba(46, 196, 166, 0.12);
  color: var(--accent);
  font-size: 0.78rem;
  position: sticky;
  text-transform: uppercase;
  top: 0;
  z-index: 1;
}

.truth-table td {
  background: rgba(255, 255, 255, 0.035);
  color: var(--text);
}

.truth-table tr.pass td {
  background: rgba(114, 223, 143, 0.14);
}

.truth-table tr.fail td {
  background: rgba(255, 107, 107, 0.14);
}

.truth-table .truth-output {
  color: var(--accent-2);
}

.score-card {
  align-items: center;
  background: rgba(46, 196, 166, 0.08);
  border: 1px solid var(--line);
  border-radius: 8px;
  display: flex;
  justify-content: space-between;
  margin: 16px 0 10px;
  padding: 12px;
}

.score-card span {
  color: var(--muted);
  font-size: 0.84rem;
  font-weight: 800;
}

.score-card strong {
  font-size: 1.25rem;
}

@media (max-width: 1050px) {
  .game-layout {
    grid-template-columns: 1fr;
  }

  .palette,
  .truth {
    order: 2;
  }

  .board-wrap {
    order: 1;
  }
}

@media (max-width: 660px) {
  .app-shell {
    padding: 12px;
  }

  .topbar {
    align-items: stretch;
    flex-direction: column;
  }

  .level-actions {
    grid-template-columns: 42px 1fr 42px;
  }

  .board-wrap {
    grid-template-rows: auto 480px;
  }

  .board {
    min-height: 480px;
  }
}
