:root {
  color-scheme: light;
  --bg: #eef1f5;
  --panel: #f7f8fa;
  --panel-2: #ffffff;
  --panel-border: #cfd6df;
  --text: #18212b;
  --muted: #6c7785;
  --accent: #1d4ed8;
  --accent-soft: #dbeafe;
  --page: #fffdf8;
  --page-margin: #f6efe1;
  --function: #fff1d9;
  --function-border: #cc8d1a;
  --fb: #e4f0ff;
  --fb-border: #4274c8;
  --io: #e8f7df;
  --io-border: #5f9f43;
  --wire: #2d3648;
  --wire-bool: #2946d1;
  --diff-position: #d97706;
  --diff-added: #15803d;
  --diff-deleted: #b91c1c;
  --diff-changed: #a21caf;
}

* {
  box-sizing: border-box;
}

body {
  margin: 0;
  font-family: "Segoe UI", Tahoma, sans-serif;
  background: linear-gradient(180deg, #f6f8fb 0, #edf1f6 100%);
  color: var(--text);
}

.busy-overlay {
  position: fixed;
  inset: 0;
  z-index: 9999;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgba(30, 36, 48, 0.28);
  backdrop-filter: blur(6px);
}

.busy-overlay[hidden] {
  display: none !important;
}

.busy-card {
  width: min(460px, 100%);
  padding: 24px;
  border: 1px solid rgba(255, 255, 255, 0.5);
  border-radius: 22px;
  background: linear-gradient(180deg, rgba(255, 251, 244, 0.98) 0, rgba(248, 243, 233, 0.98) 100%);
  box-shadow: 0 28px 70px rgba(34, 26, 17, 0.18);
}

.busy-title {
  margin: 0;
  font-size: 26px;
  font-weight: 700;
  color: var(--text);
}

.busy-detail {
  margin: 8px 0 0;
  color: var(--muted);
  line-height: 1.5;
}

.busy-progress {
  margin-top: 18px;
  height: 14px;
  overflow: hidden;
  border-radius: 999px;
  background: #e9deca;
  box-shadow: inset 0 1px 2px rgba(42, 35, 26, 0.08);
}

.busy-progress-bar {
  width: 0;
  height: 100%;
  border-radius: inherit;
  background:
    linear-gradient(90deg, rgba(255,255,255,0.16), rgba(255,255,255,0) 42%),
    linear-gradient(90deg, #3b82f6 0, #1d4ed8 100%);
  transition: width 240ms ease;
}

.shell {
  display: grid;
  grid-template-columns: 360px 1fr;
  min-height: 100vh;
  gap: 18px;
  padding: 18px;
}

.sidebar {
  border: 1px solid var(--panel-border);
  border-radius: 16px;
  overflow: hidden;
  background: var(--panel);
  display: grid;
  grid-template-rows: auto 1fr;
}

.topbar h1,
.viewer-header h2,
.panel h2 {
  margin: 0;
  font-weight: 700;
}

.topbar p,
.viewer-header p,
.hint {
  color: var(--muted);
}

.topbar {
  padding: 18px;
  background: linear-gradient(180deg, #ffffff 0, #f4f6f9 100%);
  border-bottom: 1px solid var(--panel-border);
}

.tabs {
  display: flex;
  gap: 8px;
  margin-top: 10px;
}

.tab {
  padding: 7px 12px;
  border: 1px solid var(--panel-border);
  border-radius: 999px;
  font-size: 13px;
  color: var(--muted);
  background: #fff;
}

.tab.active {
  color: #0f172a;
  border-color: #93c5fd;
  background: #eff6ff;
}

.sidebar-sections {
  min-height: 0;
  overflow: auto;
  padding: 14px;
}

.panel {
  margin-top: 14px;
  padding: 16px;
  background: var(--panel);
  border: 1px solid var(--panel-border);
  border-radius: 14px;
}

button,
.extract-form input,
.extract-form select {
  font: inherit;
}

button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-height: 42px;
  padding: 0 14px;
  border-radius: 12px;
  border: 1px solid #1d4ed8;
  background: linear-gradient(180deg, #3b82f6 0, var(--accent) 100%);
  color: white;
  cursor: pointer;
}

button:disabled {
  opacity: 0.7;
  cursor: progress;
}

.secondary {
  border: 1px solid var(--panel-border);
  background: white;
  color: var(--text);
}

.extract-form {
  display: grid;
  gap: 12px;
  margin-top: 12px;
}

.extract-form label {
  display: grid;
  gap: 6px;
}

.extract-form span {
  font-size: 13px;
  color: var(--muted);
}

.extract-form input,
.extract-form select {
  min-height: 42px;
  padding: 10px 12px;
  border-radius: 10px;
  border: 1px solid var(--panel-border);
  background: white;
  color: var(--text);
}

.inline-actions {
  display: flex;
  gap: 10px;
  align-items: end;
}

.inline-actions > button {
  flex: 0 0 auto;
}

.grow {
  flex: 1;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  padding: 0;
  margin: -1px;
  overflow: hidden;
  clip: rect(0, 0, 0, 0);
  white-space: nowrap;
  border: 0;
}

.meta {
  margin: 12px 0 0;
}

.meta-row {
  display: grid;
  grid-template-columns: 1fr auto;
  gap: 8px;
  padding: 8px 0;
  border-bottom: 1px dashed #d7dce4;
}

.meta-row:last-child {
  border-bottom: 0;
}

.meta-row dt {
  color: var(--muted);
}

.legend {
  list-style: none;
  margin: 12px 0 0;
  padding: 0;
  display: grid;
  gap: 10px;
}

.legend li {
  display: flex;
  align-items: center;
  gap: 10px;
}

.swatch {
  width: 20px;
  height: 20px;
  border-radius: 6px;
}

.swatch.function {
  background: var(--function);
  border: 1px solid var(--function-border);
}

.swatch.fb {
  background: var(--fb);
  border: 1px solid var(--fb-border);
}

.swatch.io {
  background: var(--io);
  border: 1px solid var(--io-border);
}

.swatch.wire {
  width: 28px;
  height: 4px;
  border-radius: 999px;
  background: var(--wire);
  border: 0;
}

.swatch.diff-position {
  background: color-mix(in srgb, var(--diff-position) 18%, white);
  border: 1px solid var(--diff-position);
}

.swatch.diff-added {
  background: color-mix(in srgb, var(--diff-added) 18%, white);
  border: 1px solid var(--diff-added);
}

.swatch.diff-deleted {
  background: color-mix(in srgb, var(--diff-deleted) 14%, white);
  border: 1px solid var(--diff-deleted);
}

.swatch.diff-changed {
  background: color-mix(in srgb, var(--diff-changed) 14%, white);
  border: 1px solid var(--diff-changed);
}

.project-tree {
  margin-top: 14px;
  max-height: 340px;
  overflow: auto;
  padding: 10px;
  border: 1px solid var(--panel-border);
  border-radius: 14px;
  background: linear-gradient(180deg, #fbfcfd 0, #f6f8fb 100%);
}

.project-tree.empty {
  color: var(--muted);
}

.tree-search {
  margin-top: 12px;
}

.tree-search input {
  width: 100%;
  padding: 10px 12px;
  border: 1px solid var(--panel-border);
  border-radius: 10px;
  font: inherit;
  background: #fff;
}

.tree-root,
.tree-children {
  margin: 0;
  display: grid;
  gap: 4px;
}

.tree-children {
  margin-left: 14px;
  padding-left: 12px;
  border-left: 1px solid #dde3eb;
}

.tree-node {
  margin: 0;
}

.tree-node.tree-collapsed > .tree-children {
  display: none;
}

.tree-row {
  display: grid;
  grid-template-columns: 22px 1fr;
  align-items: center;
  gap: 8px;
  min-height: 32px;
  padding: 4px 8px;
  border-radius: 10px;
}

.tree-row:hover {
  background: #eef3fa;
}

.tree-row.selected {
  background: var(--accent-soft);
  box-shadow: inset 0 0 0 1px #93c5fd;
}

.tree-toggle {
  min-height: 18px;
  width: 18px;
  padding: 0;
  border-radius: 6px;
  border: 1px solid #d7dce4;
  background: #fff;
  color: #475569;
  cursor: pointer;
  font-size: 11px;
}

.tree-toggle:disabled {
  opacity: 1;
  cursor: default;
  background: #f8fafc;
}

.tree-icon {
  font-size: 11px;
  line-height: 1;
}

.tree-labels {
  min-width: 0;
  display: grid;
  gap: 1px;
}

.tree-select {
  justify-content: flex-start;
  min-height: 22px;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--text);
  font-size: 14px;
  font-weight: 600;
}

.tree-select:hover {
  background: transparent;
}

.tree-path {
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.tree-leaf .tree-icon {
  color: #94a3b8;
}

.viewer {
  display: grid;
  grid-template-rows: auto 1fr;
  min-width: 0;
  border: 1px solid var(--panel-border);
  border-radius: 16px;
  overflow: hidden;
  background: #fff;
}

.viewer-header {
  padding: 24px 28px 10px;
  background: #fff;
  border-bottom: 1px solid var(--panel-border);
  display: flex;
  align-items: start;
  justify-content: space-between;
  gap: 16px;
  flex-wrap: wrap;
}

.compare-summary {
  display: flex;
  gap: 8px;
  flex-wrap: wrap;
  justify-content: flex-end;
}

.viewer-header-actions {
  display: grid;
  gap: 10px;
  justify-items: end;
}

.compare-view-switch {
  display: inline-flex;
  padding: 4px;
  gap: 4px;
  border: 1px solid var(--panel-border);
  border-radius: 999px;
  background: #f8fafc;
}

.compare-view-switch button {
  min-height: 34px;
  padding: 0 12px;
  border: 0;
  background: transparent;
  color: var(--muted);
}

.compare-view-switch button.active {
  background: white;
  color: var(--text);
  box-shadow: 0 1px 3px rgba(15, 23, 42, 0.12);
}

.compare-pill {
  display: inline-flex;
  align-items: center;
  min-height: 30px;
  padding: 0 10px;
  border-radius: 999px;
  border: 1px solid var(--panel-border);
  background: #fff;
  color: var(--text);
  font-size: 13px;
  font-weight: 600;
}

.canvas-wrap {
  padding: 18px 24px 24px;
  min-width: 0;
  overflow: hidden;
  background: linear-gradient(180deg, #ffffff 0, #fafbfc 100%);
}

.canvas-grid {
  display: grid;
  gap: 18px;
}

.canvas-grid.split-mode {
  grid-template-columns: minmax(420px, 1fr) minmax(420px, 1fr);
  align-items: start;
}

.canvas-pane {
  min-width: 0;
  display: grid;
  grid-template-rows: auto minmax(0, 1fr);
}

.canvas-pane-header {
  margin-bottom: 10px;
  font-size: 13px;
  font-weight: 700;
  color: var(--muted);
  text-transform: uppercase;
  letter-spacing: 0.04em;
}

.canvas-scroller {
  min-width: 0;
  overflow-x: auto;
  overflow-y: hidden;
  padding-bottom: 8px;
  scrollbar-gutter: stable both-edges;
}

.canvas {
  display: block;
  width: auto;
  height: auto;
  min-height: 0;
  max-width: none;
  background:
    linear-gradient(90deg, rgba(0, 0, 0, 0.04) 1px, transparent 1px) 0 0 / 16px 16px,
    linear-gradient(rgba(0, 0, 0, 0.04) 1px, transparent 1px) 0 0 / 16px 16px,
    #ffffff;
  border: 1px solid #cfcfcf;
}

.page-surface {
  fill: none;
  stroke: #d7d7d7;
  stroke-width: 0.8;
}

.node-body {
  stroke-width: 0.8;
  shape-rendering: crispEdges;
}

.node.fb .node-body {
  fill: #e9eff8;
  stroke: #707780;
}

.node.variable .node-body {
  fill: #eef4ea;
  stroke: #7b8a70;
}

.node.connection-mark .node-body {
  fill: #eef4ea;
  stroke: #7b8a70;
}

.node.block .node-body {
  fill: #f1f1f1;
  stroke: #707070;
}

.node-label {
  font-size: 5.1px;
  font-weight: 600;
  fill: #141414;
  dominant-baseline: middle;
}

.node-label.compact {
  font-size: 4.8px;
  font-weight: 600;
}

.pin {
  stroke: #3e4650;
  stroke-width: 0.8;
  shape-rendering: crispEdges;
}

.pin-label {
  font-size: 4.4px;
  fill: #2b2b2b;
  dominant-baseline: middle;
}

.annotation {
  font-size: 4px;
  fill: var(--muted);
}

.wire {
  fill: none;
  stroke: #3f4650;
  stroke-width: 0.8;
  stroke-linejoin: miter;
  stroke-linecap: square;
  shape-rendering: crispEdges;
}

.node.diff-position .node-body,
.wire.diff-position {
  stroke: var(--diff-position);
}

.node.diff-added .node-body,
.wire.diff-added {
  stroke: var(--diff-added);
}

.node.diff-deleted .node-body,
.wire.diff-deleted {
  stroke: var(--diff-deleted);
}

.node.diff-changed .node-body,
.wire.diff-changed {
  stroke: var(--diff-changed);
}

.node.diff-position .node-body {
  fill: color-mix(in srgb, var(--diff-position) 16%, white);
  stroke-width: 1.2;
}

.node.diff-added .node-body {
  fill: color-mix(in srgb, var(--diff-added) 14%, white);
  stroke-width: 1.2;
}

.node.diff-deleted .node-body {
  fill: color-mix(in srgb, var(--diff-deleted) 10%, white);
  stroke-width: 1.2;
  stroke-dasharray: 4 2;
}

.node.diff-changed .node-body {
  fill: color-mix(in srgb, var(--diff-changed) 12%, white);
  stroke-width: 1.2;
}

.node.ghost,
.wire.ghost {
  opacity: 0.55;
}

.ghost-outline {
  fill: none;
  stroke: var(--diff-position);
  stroke-width: 0.8;
  stroke-dasharray: 3 2;
  pointer-events: none;
}

.wire.diff-position,
.wire.diff-added,
.wire.diff-deleted,
.wire.diff-changed {
  stroke-width: 1.2;
}

.wire.diff-deleted,
.wire.ghost {
  stroke-dasharray: 4 2;
}

.instance-name {
  font-size: 4.6px;
  fill: #2b2b2b;
}

.execution-order {
  font-size: 4.2px;
  fill: #5f6778;
}

@media (max-width: 1100px) {
  .shell {
    grid-template-columns: 1fr;
    padding: 12px;
    gap: 12px;
  }

  .sidebar {
    border-right: 1px solid var(--panel-border);
  }

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

  .canvas-grid.split-mode {
    grid-template-columns: 1fr;
  }

}
