:root {
  color-scheme: light;
  --bg: #fcfcfb;
  --plane: #f9f9f7;
  --panel: #ffffff;
  --ink: #0b0b0b;
  --ink-2: #52514e;
  --muted: #898781;
  --hair: #e1e0d9;
  --ring: rgba(11, 11, 11, 0.10);
  --link: #1c5cab;
  --hover: rgba(42, 120, 214, 0.10);
  --shadow: 0 4px 16px rgba(11, 11, 11, 0.12);
  --brand: #110058;
  --brand-ink: #eeffa7;

  --fill-cell-port: #d2e2f4;
  --line-cell-port: #2a78d6;
  --fill-cell-const: #f0efec;
  --line-cell-const: #898781;
  --fill-cell-bitwise: #f9ded3;
  --line-cell-bitwise: #eb6834;
  --fill-cell-arith: #cfede1;
  --line-cell-arith: #1baf7a;
  --fill-cell-mux: #f9eac9;
  --line-cell-mux: #c98500;
  --fill-cell-shape: #e1e0d9;
  --line-cell-shape: #52514e;
  --fill-cell-reg: #d8d5ea;
  --line-cell-reg: #4a3aa7;
  --fill-cell-mem: #cae4c9;
  --line-cell-mem: #008300;
  --fill-cell-hier: #f3f2ee;
  --line-cell-hier: #52514e;
  --fill-cell-bus: #f8e2ea;
  --line-cell-bus: #d55181;
  --fill-cell-lut: #f7d8d7;
  --line-cell-lut: #e34948;
  --fill-cell-formal: #cce9ed;
  --line-cell-formal: #0e9fb5;
  --fill-wiring: #52514e;
  --line-wiring: #0b0b0b;
  --fill-instance: #f3f2ee;
  --line-instance: #52514e;
  --fill-blackbox: #dcdbd5;
  --line-blackbox: #52514e;
  --fill-oversize: #fcedce;
  --line-oversize: #c98500;
  --fill-failed: #f3d5d5;
  --line-failed: #d03b3b;
  --fill-stub: #fcfcfb;
  --line-stub: #898781;
  --fill-bin: rgba(82, 81, 78, 0.55);
  --line-bin: rgba(0, 0, 0, 0);

  --edge-bit: #52514e;
  --edge-word: #2a78d6;
  --edge-memory: #008300;
  --edge-write: #eda100;
  --edge-bus: #e87ba4;
  --edge-attach: #e87ba4;

  --text-label: #0b0b0b;
  --text-number: #52514e;
  --text-name: #0b0b0b;
  --text-port: #52514e;
  --text-literal: #52514e;
  --text-width: #898781;
  --text-title: #0b0b0b;

  --dot: #0b0b0b;
  --mark: #0b0b0b;
  --select: #0b0b0b;
  --select-halo: rgba(42, 120, 214, 0.35);
  --hilite: rgba(42, 120, 214, 0.28);
  --added: #c98500;
  --added-fill: rgba(250, 178, 25, 0.28);
  --changed: #2a78d6;
  --erased: #d03b3b;
  --trace: #0e9fb5;
  --trace-halo: rgba(14, 159, 181, 0.45);
}

@media (prefers-color-scheme: dark) {
  :root:where(:not([data-theme="light"])) {
    color-scheme: dark;
    --bg: #1a1a19;
    --plane: #0d0d0d;
    --panel: #232322;
    --ink: #ffffff;
    --ink-2: #c3c2b7;
    --muted: #898781;
    --hair: #2c2c2a;
    --ring: rgba(255, 255, 255, 0.10);
    --link: #86b6ef;
    --hover: rgba(57, 135, 229, 0.18);
    --shadow: 0 4px 16px rgba(0, 0, 0, 0.5);

    --fill-cell-port: #233b56;
    --line-cell-port: #3987e5;
    --fill-cell-const: #383835;
    --line-cell-const: #898781;
    --fill-cell-bitwise: #532d1d;
    --line-cell-bitwise: #d95926;
    --fill-cell-arith: #1a4233;
    --line-cell-arith: #199e70;
    --fill-cell-mux: #4f3a12;
    --line-cell-mux: #c98500;
    --fill-cell-shape: #383835;
    --line-cell-shape: #c3c2b7;
    --fill-cell-reg: #3d3a57;
    --line-cell-reg: #9085e9;
    --fill-cell-mem: #123a12;
    --line-cell-mem: #0ca30c;
    --fill-cell-hier: #242423;
    --line-cell-hier: #c3c2b7;
    --fill-cell-bus: #522b38;
    --line-cell-bus: #d55181;
    --fill-cell-lut: #573130;
    --line-cell-lut: #e66767;
    --fill-cell-formal: #1b474d;
    --line-cell-formal: #1db0c6;
    --fill-wiring: #c3c2b7;
    --line-wiring: #ffffff;
    --fill-instance: #242423;
    --line-instance: #c3c2b7;
    --fill-blackbox: #383835;
    --line-blackbox: #c3c2b7;
    --fill-oversize: #5d4819;
    --line-oversize: #fab219;
    --fill-failed: #512423;
    --line-failed: #d03b3b;
    --fill-stub: #1a1a19;
    --line-stub: #898781;
    --fill-bin: rgba(195, 194, 183, 0.5);

    --edge-bit: #c3c2b7;
    --edge-word: #3987e5;
    --edge-memory: #0ca30c;
    --edge-write: #c98500;
    --edge-bus: #d55181;
    --edge-attach: #d55181;

    --text-label: #ffffff;
    --text-number: #c3c2b7;
    --text-name: #ffffff;
    --text-port: #c3c2b7;
    --text-literal: #c3c2b7;
    --text-width: #898781;
    --text-title: #ffffff;

    --dot: #ffffff;
    --mark: #ffffff;
    --select: #ffffff;
    --select-halo: rgba(57, 135, 229, 0.45);
    --hilite: rgba(57, 135, 229, 0.35);
    --added: #fab219;
    --added-fill: rgba(250, 178, 25, 0.3);
    --changed: #3987e5;
    --erased: #e66767;
    --trace: #1db0c6;
    --trace-halo: rgba(29, 176, 198, 0.45);
  }
}

* { box-sizing: border-box; }

html, body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font: 13px/1.4 system-ui, -apple-system, "Segoe UI", sans-serif;
}

body {
  display: flex;
  flex-direction: column;
  overflow: hidden;
}

button, input, select {
  font: inherit;
  color: inherit;
}

#bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  padding: 6px 12px;
  background: var(--panel);
  border-bottom: 1px solid var(--hair);
  position: relative;
  z-index: 5;
}

#crumbs {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 2px;
  flex: 1 1 240px;
  min-width: 0;
  font-family: ui-monospace, "SFMono-Regular", Menlo, Consolas, monospace;
}

#crumbs button {
  border: 0;
  background: none;
  padding: 2px 4px;
  border-radius: 4px;
  cursor: pointer;
  color: var(--link);
}

#crumbs button:hover { background: var(--hover); }
#crumbs button[aria-current="page"] { color: var(--ink); cursor: default; font-weight: 600; }
#crumbs button[aria-current="page"]:hover { background: none; }
#crumbs .sep { color: var(--muted); padding: 0 2px; }

.control {
  height: 28px;
  border: 1px solid var(--hair);
  background: var(--bg);
  border-radius: 6px;
  padding: 0 8px;
}

.control:focus-visible, button:focus-visible, a:focus-visible {
  outline: 2px solid var(--link);
  outline-offset: 1px;
}

#modules { max-width: 220px; }

#search { position: relative; }
#search input { width: 200px; font-family: ui-monospace, Menlo, Consolas, monospace; }

#results {
  position: absolute;
  top: 32px;
  right: 0;
  width: 320px;
  max-height: 50vh;
  overflow: auto;
  background: var(--panel);
  border: 1px solid var(--hair);
  border-radius: 8px;
  box-shadow: var(--shadow);
  padding: 4px;
  margin: 0;
  list-style: none;
}

#results li button {
  display: flex;
  gap: 8px;
  width: 100%;
  text-align: left;
  border: 0;
  background: none;
  padding: 4px 6px;
  border-radius: 4px;
  cursor: pointer;
  font-family: ui-monospace, Menlo, Consolas, monospace;
}

#results li button:hover, #results li button:focus-visible { background: var(--hover); }
#results .kind { color: var(--ink-2); margin-left: auto; }
#results .note { color: var(--ink-2); padding: 4px 6px; font-size: 12px; }

.tool {
  height: 28px;
  border: 1px solid var(--hair);
  background: var(--bg);
  border-radius: 6px;
  padding: 0 10px;
  cursor: pointer;
}

.tool:hover { background: var(--hover); }
.tool[aria-pressed="true"] { background: var(--hover); border-color: var(--link); }

#status {
  color: var(--ink-2);
  font-size: 12px;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
}

#stage {
  position: relative;
  flex: 1 1 auto;
  min-height: 0;
  background: var(--bg);
}

#canvas {
  position: absolute;
  inset: 0;
}

#tip {
  position: absolute;
  z-index: 6;
  pointer-events: none;
  background: var(--panel);
  color: var(--ink);
  border: 1px solid var(--ring);
  box-shadow: var(--shadow);
  border-radius: 6px;
  padding: 4px 8px;
  font: 12px/1.35 ui-monospace, Menlo, Consolas, monospace;
  max-width: 360px;
  white-space: pre-wrap;
}

#tip .sub { color: var(--ink-2); }

#inspector {
  position: absolute;
  z-index: 4;
  top: 8px;
  right: 8px;
  bottom: 8px;
  width: min(360px, calc(100% - 16px));
  overflow: auto;
  background: var(--panel);
  border: 1px solid var(--ring);
  border-radius: 10px;
  box-shadow: var(--shadow);
  padding: 12px 14px;
}

#inspector h2 {
  margin: 0 28px 4px 0;
  font: 600 15px/1.3 ui-monospace, Menlo, Consolas, monospace;
  overflow-wrap: anywhere;
}

#inspector .close {
  position: absolute;
  top: 8px;
  right: 8px;
  width: 28px;
  height: 28px;
  border: 0;
  background: none;
  border-radius: 6px;
  cursor: pointer;
  color: var(--ink-2);
  font-size: 16px;
}

#inspector .close:hover { background: var(--hover); }
#inspector .where { color: var(--ink-2); margin-bottom: 8px; font-size: 12px; }
#inspector .actions { display: flex; flex-wrap: wrap; gap: 6px; margin: 8px 0; }
#inspector .actions .tool { white-space: nowrap; }

#inspector dl {
  display: grid;
  grid-template-columns: max-content 1fr;
  gap: 4px 10px;
  margin: 8px 0;
}

#inspector dt { color: var(--ink-2); }
#inspector dd { margin: 0; overflow-wrap: anywhere; }
#inspector h3 { font-size: 12px; color: var(--ink-2); font-weight: 600; margin: 12px 0 4px; }

#inspector table {
  border-collapse: collapse;
  width: 100%;
  font: 12px/1.4 ui-monospace, Menlo, Consolas, monospace;
}

#inspector td { padding: 2px 6px 2px 0; vertical-align: top; overflow-wrap: anywhere; }
#inspector td.role { color: var(--ink-2); }
#inspector .mono { font-family: ui-monospace, Menlo, Consolas, monospace; }
#inspector .uses { display: flex; flex-wrap: wrap; gap: 2px 8px; font: 12px/1.5 ui-monospace, Menlo, Consolas, monospace; }
#inspector .more { color: var(--ink-2); }
#inspector .lit { color: var(--ink); }

a.cell, button.link {
  color: var(--link);
  text-decoration: none;
  cursor: pointer;
  background: none;
  border: 0;
  padding: 0;
  font: inherit;
}

a.cell:hover, button.link:hover { text-decoration: underline; }

#panel {
  position: absolute;
  z-index: 3;
  inset: 0;
  display: flex;
  align-items: flex-start;
  justify-content: center;
  padding: 48px 16px;
  overflow: auto;
  background: var(--plane);
}

#panel .card {
  width: min(560px, 100%);
  background: var(--panel);
  border: 1px solid var(--ring);
  border-radius: 10px;
  box-shadow: var(--shadow);
  padding: 16px 18px;
}

#panel h2 { margin: 0 0 6px; font: 600 16px/1.3 ui-monospace, Menlo, Consolas, monospace; }
#panel p { color: var(--ink-2); margin: 6px 0 12px; }
#panel ul { list-style: none; padding: 0; margin: 0; display: grid; gap: 4px; }
#panel li { display: flex; gap: 10px; align-items: center; font: 13px/1.4 ui-monospace, Menlo, Consolas, monospace; }
#panel li .dir { color: var(--ink-2); width: 44px; }
#panel li .w { color: var(--ink-2); }
#panel form { display: flex; gap: 6px; margin-top: 12px; }
#panel form input { flex: 1; font-family: ui-monospace, Menlo, Consolas, monospace; }
#panel .matches { margin-top: 8px; }

#loading {
  position: absolute;
  z-index: 2;
  top: 8px;
  left: 50%;
  transform: translateX(-50%);
  background: var(--panel);
  border: 1px solid var(--ring);
  border-radius: 999px;
  padding: 2px 10px;
  color: var(--ink-2);
  font-size: 12px;
  box-shadow: var(--shadow);
}

#fatal {
  margin: 48px auto;
  max-width: 560px;
  padding: 16px;
  color: var(--ink);
}

[hidden] { display: none !important; }

@media (max-width: 640px) {
  #search input { width: 140px; }
  #modules { max-width: 140px; }
  #status { display: none; }
  #inspector { top: auto; height: 55%; }
}

#timeline {
  background: var(--panel);
  border-top: 1px solid var(--hair);
  padding: 6px 12px;
  position: relative;
  z-index: 5;
  max-height: 40vh;
  display: flex;
  flex-direction: column;
  gap: 6px;
}

#timeline[hidden] { display: none; }

#timeline .row {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}

#timeline .moves { display: flex; gap: 4px; }
#timeline .moves .tool { min-width: 32px; padding: 0 8px; font-family: ui-monospace, Menlo, Consolas, monospace; }
#timeline .state { font-variant-numeric: tabular-nums; white-space: nowrap; font-weight: 600; }
#timeline .label { flex: 1 1 240px; min-width: 0; color: var(--ink-2); font-family: ui-monospace, Menlo, Consolas, monospace; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
#timeline .filter { display: flex; align-items: center; gap: 4px; color: var(--ink-2); white-space: nowrap; }

#t-runs {
  overflow: auto;
  min-height: 0;
  border-top: 1px solid var(--hair);
  padding-top: 4px;
  font: 12px/1.5 ui-monospace, Menlo, Consolas, monospace;
}

#t-runs ul { list-style: none; margin: 0; padding: 0 0 0 16px; }
#t-runs > ul { padding-left: 0; }
#t-runs li { white-space: nowrap; }
#t-runs button { border: 0; background: none; padding: 0 4px; border-radius: 4px; cursor: pointer; font: inherit; color: var(--link); }
#t-runs button:hover { background: var(--hover); }
#t-runs .twist { color: var(--ink-2); width: 16px; padding: 0; }
#t-runs li.step { padding-left: 16px; }
#t-runs li.more button { color: var(--ink-2); }
#t-runs li.here > .go { color: var(--ink); font-weight: 600; }
#t-runs li.step.here > .go { background: var(--hover); }
