:root {
  --bg: #f4f0ea;
  --paper: rgba(255, 255, 255, 0.92);
  --ink: #1f2721;
  --muted: #595951;
  --line: rgba(31, 39, 33, 0.12);
  --cool: #275f78;
  --warm: #b6642d;
  --bad: #9e382d;
  --shadow: 0 18px 36px rgba(59, 48, 27, 0.12);
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  height: 100vh;
  min-height: 100vh;
  overflow: hidden;
  font-family: "Trebuchet MS", "Segoe UI", sans-serif;
  color: var(--ink);
  background:
    radial-gradient(circle at top left, rgba(182, 100, 45, 0.12), transparent 24%),
    radial-gradient(circle at bottom right, rgba(39, 95, 120, 0.12), transparent 24%),
    linear-gradient(180deg, #fbf8f3 0%, #f0e7dc 100%);
}

body {
  padding: 16px;
}

.simulator-shell {
  width: 100%;
  height: calc(100vh - 32px);
  min-height: calc(100vh - 32px);
}

.simulator-layout {
  display: grid;
  gap: 18px;
  height: calc(100vh - 32px);
  min-height: calc(100vh - 32px);
}

.bank-panel,
.canvas-panel {
  display: flex;
  flex-direction: column;
  min-width: 0;
  padding: 16px;
  border-radius: 24px;
  border: 1px solid var(--line);
  background: var(--paper);
  box-shadow: var(--shadow);
}

.bank-label,
.mini-kicker {
  margin: 0 0 12px;
  text-transform: uppercase;
  letter-spacing: 0.12em;
  font-size: 0.72rem;
  font-weight: 800;
  color: var(--muted);
}

.canvas-key {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
  margin-bottom: 12px;
}

.button {
  appearance: none;
  border: 1px solid var(--line);
  border-radius: 999px;
  padding: 10px 16px;
  background: rgba(255, 255, 255, 0.88);
  color: var(--ink);
  font: inherit;
  font-weight: 800;
  cursor: pointer;
}

.button:disabled {
  background: rgba(120, 125, 128, 0.18);
  color: rgba(31, 39, 33, 0.42);
  border-color: rgba(31, 39, 33, 0.08);
  cursor: not-allowed;
}

.gate-palette {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(150px, 1fr));
  gap: 12px;
}

.palette-gate {
  display: grid;
  grid-template-columns: 56px 1fr;
  gap: 10px;
  align-items: center;
  width: 100%;
  padding: 12px;
  border: 1px solid rgba(31, 39, 33, 0.14);
  border-radius: 18px;
  background: #fff;
  color: inherit;
  font: inherit;
  text-align: left;
  cursor: grab;
}

.palette-gate strong,
.palette-gate span {
  display: block;
}

.palette-gate strong {
  margin-bottom: 2px;
}

.palette-gate span span {
  color: var(--muted);
  font-size: 0.88rem;
}

.gate-svg {
  width: 100%;
  height: auto;
}

.gate-canvas {
  flex: 1;
  position: relative;
  width: 100%;
  min-height: 680px;
  overflow: hidden;
  border-radius: 22px;
  border: 1px solid rgba(39, 95, 120, 0.14);
  background:
    radial-gradient(circle at 20% 15%, rgba(255, 255, 255, 0.14), transparent 22%),
    linear-gradient(180deg, #dcebf5 0%, #c6dde9 100%);
  touch-action: none;
  contain: layout paint;
  cursor: grab;
}

.gate-canvas.is-scrollable {
  overflow: auto;
}

.gate-canvas.is-panning {
  cursor: grabbing;
}

.gate-scene-sizer {
  position: relative;
  width: 100%;
  min-width: 100%;
  min-height: 100%;
  overflow: hidden;
}

.gate-scene {
  position: absolute;
  inset: 0 auto auto 0;
  transform-origin: top left;
}

.canvas-connections {
  position: absolute;
  inset: 0;
  overflow: visible;
  pointer-events: none;
}

.selection-marquee {
  position: absolute;
  border: 2px dashed rgba(39, 95, 120, 0.7);
  background: rgba(39, 95, 120, 0.12);
  pointer-events: none;
  z-index: 3;
}

.canvas-link {
  fill: none;
  stroke: #000;
  stroke-width: 3;
  stroke-linecap: round;
  stroke-linejoin: round;
  pointer-events: stroke;
  cursor: pointer;
}

.placed-gate {
  position: absolute;
  width: 168px;
  height: 120px;
}

.placed-gate-body {
  position: relative;
  display: grid;
  place-items: center;
  width: 100%;
  height: 100%;
  padding: 18px 18px 28px;
  border-radius: 24px;
  border: 1px solid rgba(31, 39, 33, 0.14);
  background: rgba(255, 255, 255, 0.92);
  box-shadow: 0 10px 24px rgba(59, 48, 27, 0.12);
}

.placed-gate.is-selected .placed-gate-body {
  outline: 4px solid rgba(39, 95, 120, 0.78);
  outline-offset: 2px;
  box-shadow:
    0 0 0 6px rgba(39, 95, 120, 0.18),
    0 14px 30px rgba(39, 95, 120, 0.18);
}

.placed-gate.is-targeted .placed-gate-body {
  outline: 3px solid rgba(182, 100, 45, 0.3);
}

.placed-gate-delete {
  position: absolute;
  top: -8px;
  right: -8px;
  z-index: 3;
  width: 24px;
  height: 24px;
  border: 0;
  border-radius: 999px;
  background: rgba(158, 56, 45, 0.94);
  color: #fff;
  font: inherit;
  font-weight: 800;
  cursor: pointer;
}

.placed-gate-title {
  position: absolute;
  left: 0;
  right: 0;
  bottom: 8px;
  text-align: center;
  font-size: 0.78rem;
  font-weight: 800;
  letter-spacing: 0.08em;
}

.gate-connector {
  position: absolute;
  top: var(--connector-top);
  width: 18px;
  height: 18px;
  margin-top: -9px;
  border: 2px solid #000;
  border-radius: 999px;
  background: #fff;
  cursor: pointer;
}

.gate-connector[data-side="left"] {
  left: -9px;
}

.gate-connector[data-side="right"] {
  right: -9px;
}

.gate-connector.is-on,
.gate-connector.is-output-on {
  background: #000;
}

.gate-connector.is-disabled {
  cursor: default;
}

.gate-connector-label {
  position: absolute;
  top: var(--connector-top);
  margin-top: -2px;
  font-size: 0.8rem;
  font-weight: 800;
  line-height: 20px;
  color: #000;
}

.gate-connector-label.is-on {
  color: #d60f0f;
}

.gate-connector-label.is-off {
  color: #000;
}

.gate-connector-label.input {
  left: 24px;
}

.gate-connector-label.output {
  right: 16px;
}

.input-gate-figure {
  position: relative;
  display: grid;
  place-items: center;
  width: 120px;
}

.input-gate-badge {
  position: absolute;
  left: 14px;
  top: 16px;
  width: 66px;
  height: 48px;
  display: grid;
  place-items: center;
  border: 0;
  background: transparent;
  color: #000;
  text-align: center;
  font: inherit;
  font-size: 1rem;
  font-weight: 800;
}

.output-gate-badge {
  left: 34px;
  top: 16px;
  width: 40px;
  height: 48px;
  color: #000;
}

.output-gate-badge.is-on {
  color: #d60f0f;
}

.truth-popup {
  position: absolute;
  top: 18px;
  right: 18px;
  z-index: 4;
  min-width: 280px;
  min-height: 180px;
  max-height: calc(100% - 36px);
  overflow: auto;
  border: 1px solid rgba(31, 39, 33, 0.14);
  border-radius: 20px;
  background: rgba(255, 255, 255, 0.96);
  box-shadow: 0 18px 36px rgba(59, 48, 27, 0.16);
  resize: both;
}

.truth-popup__head {
  display: flex;
  justify-content: space-between;
  gap: 12px;
  align-items: start;
  padding: 14px 16px 8px;
  border-bottom: 1px solid rgba(31, 39, 33, 0.08);
  cursor: grab;
  user-select: none;
}

.truth-popup.is-dragging .truth-popup__head {
  cursor: grabbing;
}

.truth-popup__head h2 {
  margin: 0;
  font-size: 1.1rem;
}

.truth-popup__close {
  border: 0;
  background: rgba(158, 56, 45, 0.08);
  color: var(--bad);
  width: 32px;
  height: 32px;
  border-radius: 999px;
  font: inherit;
  font-size: 1.1rem;
  font-weight: 800;
  cursor: pointer;
}

.truth-popup__body {
  padding: 12px 16px 16px;
}

.truth-popup__section + .truth-popup__section {
  margin-top: 14px;
}

.truth-popup__body table {
  width: max-content;
  border-collapse: collapse;
  font-size: 0.92rem;
}

.truth-popup__body th,
.truth-popup__body td {
  border: 1px solid rgba(31, 39, 33, 0.12);
  padding: 8px 10px;
  text-align: center;
  white-space: nowrap;
}

.truth-popup__body th {
  background: #ef7f2d;
  color: #fff;
  font-weight: 800;
}

@media (max-width: 720px) {
  body {
    padding: 10px;
  }

  .gate-canvas {
    min-height: 600px;
  }

  .gate-scene-sizer {
    min-height: 100%;
  }
}
