:root {
  /* Wire colors — the active/inactive distinction is the core teaching signal (FR-012, SC-003) */
  --wire-inactive: #64748b; /* slate gray — low/idle */
  --wire-active: #22c55e;   /* bright green — high/energized */
  --wire-preview: #94a3b8;

  /* Logic level fills */
  --level-low: #1e293b;
  --level-high: #22c55e;

  /* Terminal accents by kind */
  --input-accent: #3b82f6;  /* blue */
  --output-accent: #a855f7; /* purple */

  --bg: #0f172a;
  --panel: #1e293b;
  --workspace-bg: #111827;
  --grid: #1f2937;
  --text: #e2e8f0;
  --text-dim: #94a3b8;
  --danger: #ef4444;
  --bus-accent: #f59e0b; /* amber — a multi-bit bus, distinct from single-bit */
}

/* ---- Bus (converters, bus ports, bus wires) ---- */
.instance--bus .instance__body {
  stroke: var(--bus-accent);
  stroke-width: 2px;
}
.terminal__port--bus {
  fill: var(--bus-accent);
  stroke: var(--bus-accent);
}
/* Higher specificity (.wire.wire--bus) so a bus wire's thicker amber stroke
   wins over the base `.wire` rule regardless of stylesheet order. */
.wire.wire--bus {
  stroke: var(--bus-accent);
  stroke-width: 6px;
}
/* Bus terminals: amber-accented body, monospace binary readout. Compound
   selectors (.terminal__*.bus__*) keep these winning over the base rules. */
.terminal__body.bus__body {
  fill: #3a2f12;
  stroke: var(--bus-accent);
  stroke-width: 2px;
}
.terminal__label.bus__value {
  fill: var(--bus-accent);
  font-family: ui-monospace, "SFMono-Regular", Menlo, monospace;
  font-size: 0.72rem;
  letter-spacing: 1px;
}
.terminal__label.bus__tag {
  fill: var(--text-dim);
  font-size: 0.68rem;
}
.bus-ctrl__bits {
  display: flex;
  gap: 5px;
  justify-content: center;
  margin-bottom: 6px;
}
.bus-ctrl__bit {
  width: 16px;
  height: 16px;
  accent-color: var(--bus-accent);
  cursor: pointer;
}

/* Transient toast (e.g. a refused bus connection). */
.toast {
  position: absolute;
  left: 50%;
  bottom: 1.5rem;
  transform: translateX(-50%);
  z-index: 20;
  background: var(--danger);
  color: #fff;
  padding: 0.5rem 0.9rem;
  border-radius: 6px;
  font-size: 0.85rem;
  box-shadow: 0 4px 14px rgba(0, 0, 0, 0.4);
  cursor: pointer;
  max-width: 80%;
}

* {
  box-sizing: border-box;
}

html,
body {
  margin: 0;
  padding: 0;
  height: 100%;
  background: var(--bg);
  color: var(--text);
  font-family: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;
}

.app {
  display: flex;
  flex-direction: column;
  height: 100vh;
}

.app__header {
  padding: 0.75rem 1.25rem;
}

.app__header h1 {
  margin: 0;
  font-size: 1.15rem;
  font-weight: 650;
}

.app__header p {
  margin: 0.2rem 0 0;
  font-size: 0.8rem;
  color: var(--text-dim);
}

/* ---- Toolbar ---- */
.toolbar {
  display: flex;
  gap: 0.5rem;
  align-items: center;
  padding: 0.5rem 1.25rem;
  background: var(--panel);
  border-top: 1px solid #334155;
  border-bottom: 1px solid #334155;
}

.toolbar button {
  cursor: pointer;
  border: 1px solid #475569;
  background: #334155;
  color: var(--text);
  padding: 0.4rem 0.8rem;
  border-radius: 6px;
  font-size: 0.85rem;
}

.toolbar button:hover {
  background: #3f4d63;
}

.toolbar .toolbar__hint {
  margin-left: auto;
  font-size: 0.78rem;
  color: var(--text-dim);
}

/* ---- Workspace / SVG ---- */
.workspace {
  flex: 1;
  position: relative;
  overflow: hidden;
  background: var(--workspace-bg);
}

.workspace svg {
  display: block;
  width: 100%;
  height: 100%;
  touch-action: none; /* let pointer drags pan/move without browser gestures */
  background-image:
    linear-gradient(var(--grid) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid) 1px, transparent 1px);
  background-size: 32px 32px;
}

/* Zoom / fit controls overlaid on the canvas */
.canvas-controls {
  position: absolute;
  top: 0.6rem;
  right: 0.6rem;
  z-index: 5;
  display: flex;
  flex-direction: column;
  gap: 0.35rem;
}

.canvas-controls button {
  width: 2rem;
  height: 2rem;
  cursor: pointer;
  border: 1px solid #475569;
  background: #1e293b;
  color: var(--text);
  border-radius: 6px;
  font-size: 1rem;
  line-height: 1;
  display: flex;
  align-items: center;
  justify-content: center;
}

.canvas-controls button:hover {
  background: #334155;
}

/* ---- Wires ---- */
.wire {
  fill: none;
  stroke: var(--wire-inactive);
  stroke-width: 3;
  stroke-linecap: round;
  pointer-events: none; /* all interaction is handled by the fat .wire-hit band */
  transition: stroke 80ms linear, stroke-width 80ms linear;
}

.wire--active {
  stroke: var(--wire-active);
}

/* Hovering anywhere in the clickable band highlights the visible wire, so the
   highlighted area and the removable area always match. */
.wire-group:hover .wire {
  stroke-width: 6;
}

.wire-hit {
  fill: none;
  stroke: transparent;
  stroke-width: 16; /* fat invisible hit target so wires are easy to click */
  stroke-linecap: round;
  pointer-events: stroke;
  cursor: pointer;
}

.wire-preview {
  fill: none;
  stroke: var(--wire-preview);
  stroke-width: 2.5;
  stroke-dasharray: 6 5;
  pointer-events: none;
}

/* ---- Terminals ---- */
.terminal {
  cursor: default;
}

.terminal__body {
  stroke-width: 2.5;
  fill: var(--level-low);
  transition: fill 80ms linear;
}

.terminal--input .terminal__body {
  stroke: var(--input-accent);
  cursor: pointer; /* inputs are clickable to toggle */
}

.terminal--output .terminal__body {
  stroke: var(--output-accent);
}

.terminal--high .terminal__body {
  fill: var(--level-high);
}

.terminal__label {
  fill: var(--text);
  font-size: 11px;
  font-weight: 600;
  text-anchor: middle;
  dominant-baseline: middle;
  pointer-events: none;
  user-select: none;
}

/* Connection port nubs */
.terminal__port {
  fill: #0f172a;
  stroke: #94a3b8;
  stroke-width: 2;
  cursor: crosshair;
}

.terminal__port:hover {
  stroke: #e2e8f0;
}

/* Invisible, larger click target around a port nub (easy to hit; wins over an
   overlapping wire band at a connected port). */
.port-hit {
  fill: transparent;
  stroke: none;
  cursor: crosshair;
}

.terminal__port--pending {
  fill: #fde047;
  stroke: #fde047;
}

.terminal__port--high {
  fill: var(--wire-active);
  stroke: var(--wire-active);
}

/* NAND block in the playground */
.gate-block__body {
  fill: #1e293b;
  stroke: #a855f7;
  stroke-width: 2.5;
}

.gate-block__label {
  fill: var(--text);
  font-size: 13px;
  font-weight: 700;
  text-anchor: middle;
  dominant-baseline: middle;
  pointer-events: none;
  user-select: none;
}

/* ===== Step 4: components, toolbox, selection ===== */

/* Generic component instance block */
.instance__body {
  fill: #1e293b;
  stroke: #a855f7;
  stroke-width: 2.5;
}

.instance__label {
  fill: var(--text);
  font-size: 13px;
  font-weight: 700;
  text-anchor: middle;
  dominant-baseline: middle;
  pointer-events: none;
  user-select: none;
}

.instance__port-label {
  fill: var(--text-dim);
  font-size: 10px;
  dominant-baseline: middle;
  pointer-events: none;
  user-select: none;
}

/* RAM: the memory colour code is blue — a deeper blue body with a bright blue
   outline, plus a monospace binary readout of the value currently being read. */
.instance--ram .instance__body {
  fill: #15294a;
  stroke: #4d8bff;
}

.ram__tag {
  fill: #9cc2ff;
  font-size: 11px;
  letter-spacing: 1px;
}

.ram__value {
  fill: #dbe9ff;
  font-family: "SFMono-Regular", "Consolas", "Liberation Mono", monospace;
  font-size: 13px;
  letter-spacing: 1px;
}

/* Selection highlight */
.selected .terminal__body,
.selected .instance__body {
  stroke: #fde047;
  stroke-width: 3.5;
}

/* Marquee selection rectangle */
.marquee {
  fill: rgba(253, 224, 71, 0.08);
  stroke: #fde047;
  stroke-width: 1.5;
  stroke-dasharray: 5 4;
  pointer-events: none;
}

/* Toolbox palette overlay */
.toolbox {
  position: absolute;
  top: 0.6rem;
  left: 0.6rem;
  z-index: 5;
  width: 180px;
  max-height: calc(100% - 1.2rem);
  overflow-y: auto;
  background: rgba(15, 23, 42, 0.92);
  border: 1px solid #334155;
  border-radius: 8px;
  padding: 0.5rem;
}

.toolbox__title {
  font-size: 0.8rem;
  font-weight: 700;
  color: var(--text-dim);
  margin-bottom: 0.25rem;
}

.toolbox__hint {
  font-size: 0.68rem;
  color: var(--text-dim);
  opacity: 0.8;
  margin-bottom: 0.5rem;
}

.modal__section {
  font-size: 0.72rem;
  font-weight: 700;
  color: var(--text-dim);
  margin: 0.5rem 0 0.2rem;
}

.toolbox__create {
  width: 100%;
  cursor: pointer;
  border: 1px solid #475569;
  background: #334155;
  color: var(--text);
  padding: 0.35rem 0.5rem;
  border-radius: 6px;
  font-size: 0.8rem;
  margin-bottom: 0.5rem;
}

.toolbox__create:disabled {
  opacity: 0.45;
  cursor: not-allowed;
}

.toolbox__list {
  list-style: none;
  margin: 0;
  padding: 0;
  display: flex;
  flex-direction: column;
  gap: 0.3rem;
}

.toolbox__entry {
  display: flex;
  gap: 0.3rem;
}

.toolbox__chip {
  flex: 1;
  cursor: grab;
  text-align: left;
  border: 1px solid #475569;
  background: #1e293b;
  color: var(--text);
  padding: 0.35rem 0.5rem;
  border-radius: 6px;
  font-size: 0.82rem;
}

.toolbox__chip:hover {
  background: #334155;
}

.toolbox__chip.armed {
  border-color: var(--wire-active);
  box-shadow: 0 0 0 1px var(--wire-active) inset;
}

.toolbox__edit {
  cursor: pointer;
  border: 1px solid #475569;
  background: #1e293b;
  color: var(--text-dim);
  border-radius: 6px;
  font-size: 0.8rem;
  padding: 0 0.4rem;
}

.toolbox__edit:hover {
  color: var(--text);
}

.rename {
  margin: 0.3rem 0 0.1rem;
  padding: 0.3rem;
  background: #0f172a;
  border: 1px solid #334155;
  border-radius: 6px;
  display: flex;
  flex-direction: column;
  gap: 0.25rem;
}

.rename__row {
  display: flex;
  align-items: center;
  gap: 0.3rem;
}

.rename__tag {
  width: 26px;
  font-size: 0.68rem;
  color: var(--text-dim);
}

.rename__input,
.modal__input {
  flex: 1;
  width: 100%;
  background: #1e293b;
  border: 1px solid #475569;
  border-radius: 4px;
  color: var(--text);
  font-size: 0.8rem;
  padding: 0.2rem 0.35rem;
}

/* Create-component popup (opens at the click position) */
.modal {
  position: fixed;
  inset: 0;
  z-index: 20;
  background: rgba(2, 6, 23, 0.45);
}

.modal__box {
  position: absolute;
  /* --mx/--my are the click position (set inline); clamp within the viewport */
  left: min(var(--mx, 50%), calc(100vw - 372px));
  top: min(var(--my, 20%), calc(100vh - 252px));
  background: var(--panel);
  border: 1px solid #475569;
  border-radius: 10px;
  padding: 1.1rem 1.2rem;
  width: 360px;
  max-width: calc(100vw - 1.5rem);
  min-height: 240px;
  max-height: 80vh;
  overflow-y: auto;
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.45);
}

.modal__title {
  font-size: 0.9rem;
  font-weight: 650;
  margin-bottom: 0.6rem;
}

.modal__actions {
  display: flex;
  gap: 0.5rem;
  justify-content: flex-end;
  margin-top: 0.7rem;
}

.modal__actions button {
  cursor: pointer;
  border: 1px solid #475569;
  background: #334155;
  color: var(--text);
  padding: 0.35rem 0.8rem;
  border-radius: 6px;
  font-size: 0.82rem;
}

/* Right-click context menu */
.ctx-backdrop {
  position: fixed;
  inset: 0;
  z-index: 25;
}

.ctx-menu {
  position: absolute;
  left: min(var(--mx, 50%), calc(100vw - 240px));
  top: min(var(--my, 20%), calc(100vh - 110px));
  min-width: 200px;
  max-height: 70vh;
  overflow-y: auto;
  background: var(--panel);
  border: 1px solid #475569;
  border-radius: 8px;
  padding: 0.3rem;
  box-shadow: 0 12px 30px rgba(0, 0, 0, 0.45);
  display: flex;
  flex-direction: column;
  gap: 0.15rem;
}

.ctx-heading {
  font-size: 0.68rem;
  font-weight: 700;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  color: var(--text-dim);
  padding: 0.3rem 0.6rem 0.1rem;
}

.ctx-sep {
  height: 1px;
  background: #334155;
  margin: 0.25rem 0.3rem;
}

.ctx-item {
  cursor: pointer;
  text-align: left;
  border: none;
  background: transparent;
  color: var(--text);
  padding: 0.45rem 0.6rem;
  border-radius: 6px;
  font-size: 0.85rem;
}

.ctx-item:hover {
  background: #334155;
}

.ctx-item--danger {
  color: #fca5a5;
}

.ctx-item--danger:hover {
  background: #7f1d1d;
  color: #fff;
}

/* Toolbox delete-component affordance */
.rename__delete {
  margin-top: 0.35rem;
  cursor: pointer;
  border: 1px solid #7f1d1d;
  background: #3f1d1d;
  color: #fca5a5;
  border-radius: 6px;
  font-size: 0.78rem;
  padding: 0.3rem 0.5rem;
}

.rename__delete:hover {
  background: #7f1d1d;
  color: #fff;
}

.rename__msg {
  margin-top: 0.25rem;
  font-size: 0.7rem;
  color: #fca5a5;
}

/* Delete affordance */
.terminal__delete {
  fill: var(--danger);
  cursor: pointer;
  opacity: 0.85;
}

.terminal__delete:hover {
  opacity: 1;
}

.terminal__delete-x {
  fill: #fff;
  font-size: 12px;
  font-weight: 700;
  text-anchor: middle;
  dominant-baseline: central;
  pointer-events: none;
  user-select: none;
}

.workspace__empty {
  fill: var(--text-dim);
  font-size: 14px;
  text-anchor: middle;
}

/* ===== Step 2: NAND gate view ===== */

/* Scene selector (segmented control in the header) */
.scenes {
  display: flex;
  gap: 0.4rem;
  margin-top: 0.5rem;
}

.scenes button {
  cursor: pointer;
  border: 1px solid #475569;
  background: #1e293b;
  color: var(--text-dim);
  padding: 0.3rem 0.75rem;
  border-radius: 6px;
  font-size: 0.82rem;
}

.scenes button:hover {
  background: #334155;
  color: var(--text);
}

.scenes button.scenes__active {
  background: var(--input-accent);
  border-color: var(--input-accent);
  color: #fff;
}

/* Power rails */
.rail {
  stroke-width: 5;
  stroke-linecap: round;
}

.rail--vdd {
  stroke: var(--wire-active); /* VDD is the high rail */
}

.rail--gnd {
  stroke: var(--wire-inactive); /* GND is the low rail */
}

.rail-label {
  font-size: 13px;
  font-weight: 700;
  fill: var(--text-dim);
  dominant-baseline: middle;
  user-select: none;
}

/* Conductors inside the gate (transistor leads + buses) carry a logic level */
.conductor {
  fill: none;
  stroke: var(--wire-inactive);
  stroke-width: 3.5;
  stroke-linecap: round;
  transition: stroke 80ms linear;
}

.conductor--high {
  stroke: var(--wire-active);
}

/* The live conducting path from the driving rail to the output */
.conductor--path {
  stroke-width: 5.5;
}

.node-dot {
  fill: var(--text);
}

/* Control (gate) lines from inputs to transistor gates */
.ctrl-line {
  fill: none;
  stroke: var(--wire-inactive);
  stroke-width: 2.5;
  stroke-dasharray: 5 4;
  transition: stroke 80ms linear;
}

.ctrl-line--high {
  stroke: var(--wire-active);
}

/* Transistor symbols */
.transistor__body {
  stroke-width: 2.5;
  rx: 6;
  fill: #0f172a;
  transition: fill 80ms linear, stroke 80ms linear;
}

.transistor--nmos .transistor__body {
  stroke: #38bdf8;
}

.transistor--pmos .transistor__body {
  stroke: #f472b6;
}

/* Conducting (closed) transistor: filled/energized; open: dim */
.transistor--on .transistor__body {
  fill: #14532d;
  stroke: var(--wire-active);
}

.transistor--off .transistor__body {
  opacity: 0.55;
}

.transistor__label {
  font-size: 12px;
  font-weight: 700;
  fill: var(--text);
  text-anchor: middle;
  dominant-baseline: central;
  user-select: none;
}

.transistor__state {
  font-size: 10px;
  fill: var(--text-dim);
  text-anchor: middle;
  user-select: none;
}

.gate-caption {
  fill: var(--text-dim);
  font-size: 12px;
  text-anchor: middle;
}

.gate-caption--strong {
  fill: var(--text);
  font-size: 14px;
  font-weight: 650;
}

/* Edit-schematic action in the toolbox rename panel */
.rename__edit-schematic {
    margin-top: 10px;
    width: 100%;
    padding: 6px;
    border: 1px solid var(--accent);
    border-radius: 6px;
    background: transparent;
    color: var(--accent);
    cursor: pointer;
}

.rename__edit-schematic:hover {
    background: var(--hover);
}

/* "Editing a component schematic" banner (shown during an edit session) */
.edit-banner {
    position: absolute;
    top: 12px;
    left: 50%;
    transform: translateX(-50%);
    z-index: 6;
    display: flex;
    align-items: center;
    gap: 16px;
    padding: 8px 14px;
    border: 1px solid var(--accent);
    border-radius: 10px;
    background: var(--panel);
    color: var(--ink);
    box-shadow: 0 4px 16px rgba(0, 0, 0, 0.4);
}

.edit-banner__label {
    font-size: 14px;
}

.edit-banner__actions {
    display: flex;
    gap: 8px;
}

.edit-banner__save,
.edit-banner__cancel {
    padding: 5px 14px;
    border-radius: 6px;
    border: 1px solid var(--line);
    cursor: pointer;
    font-size: 13px;
}

.edit-banner__save {
    background: var(--accent);
    color: var(--accent-ink);
    border-color: var(--accent);
}

.edit-banner__cancel {
    background: transparent;
    color: var(--ink);
}

.edit-banner__save:hover,
.edit-banner__cancel:hover {
    filter: brightness(1.1);
}

/* ===== Step 11: clock signal ===== */
.terminal--clock .clock__body {
    stroke: #f59e0b; /* amber — distinct from input(blue)/output(purple) */
}

.terminal--clock.terminal--high .clock__body {
    fill: var(--level-high);
}

.clock__tag {
    fill: #f59e0b;
    font-weight: 700;
}

/* Per-clock controls (run/pause, step, pace) rendered in a foreignObject */
.clock-ctrl {
    display: flex;
    flex-direction: column;
    gap: 4px;
    font-size: 11px;
    color: var(--text);
}

.clock-ctrl__row {
    display: flex;
    align-items: center;
    gap: 4px;
}

.clock-ctrl__btn {
    padding: 1px 6px;
    border: 1px solid var(--line, #475569);
    border-radius: 4px;
    background: var(--panel);
    color: var(--text);
    cursor: pointer;
    line-height: 1.2;
}

.clock-ctrl__btn:disabled {
    opacity: 0.4;
    cursor: default;
}

.clock-ctrl__hz {
    margin-left: auto;
    color: var(--text-dim);
}

.clock-ctrl__slider {
    width: 100%;
}

/* Clock name field (in the per-clock controls panel) */
.clock-ctrl__name {
    width: 100%;
    padding: 2px 5px;
    border: 1px solid var(--line, #475569);
    border-radius: 4px;
    background: var(--bg, #0f172a);
    color: var(--text);
    font-size: 11px;
}

/* ===== Step 12: seven-segment display ===== */
.instance--display .instance__body {
    fill: #1a1f2e;
    stroke: #f43f5e; /* rose — distinct device accent */
    stroke-width: 2.5;
}

/* Each segment is a thick rounded line; off = dim, on = bright red glow */
.seg {
    stroke-width: 8;
    stroke-linecap: round;
    pointer-events: none;
}

.seg--off {
    stroke: #2a2f3e; /* barely-there, like an unlit LED segment */
}

.seg--on {
    stroke: #ff3b3b; /* lit red LED */
}

/* ===== Step 16: workspace tabs ===== */
.ws-tabs {
  display: flex;
  align-items: flex-end;
  gap: 2px;
  padding: 0.4rem 1.25rem 0;
  background: var(--panel);
  border-bottom: 1px solid #334155;
  overflow-x: auto;
}

/* A tab: rounded top, sits on the bottom border; the active one "connects" to
   the canvas by matching its background and dropping the bottom edge. */
.ws-tab {
  display: inline-flex;
  align-items: center;
  gap: 0.3rem;
  padding: 0.3rem 0.4rem 0.35rem 0.7rem;
  background: #172033;
  border: 1px solid #334155;
  border-bottom: none;
  border-radius: 8px 8px 0 0;
  color: var(--text-dim);
  font-size: 0.82rem;
  white-space: nowrap;
  position: relative;
  top: 1px;
}

.ws-tab--active {
  background: var(--workspace-bg);
  color: var(--text);
  border-top: 2px solid var(--input-accent);
  padding-top: calc(0.3rem - 1px);
}

.ws-tab__name {
  background: none;
  border: none;
  color: inherit;
  font: inherit;
  cursor: pointer;
  padding: 0;
  max-width: 14rem;
  overflow: hidden;
  text-overflow: ellipsis;
}

.ws-tab__rename {
  background: #0f172a;
  border: 1px solid var(--input-accent);
  border-radius: 4px;
  color: var(--text);
  font: inherit;
  width: 9rem;
  padding: 0.05rem 0.3rem;
}

.ws-tab__close {
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 0.95rem;
  line-height: 1;
  padding: 0 0.2rem;
  border-radius: 4px;
}

.ws-tab__close:hover {
  background: var(--danger);
  color: #fff;
}

/* "New workspace" affordances, styled as quiet pills at the end of the row. */
.ws-tab__new {
  align-self: center;
  margin-left: 0.3rem;
  background: none;
  border: 1px solid #334155;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 0.82rem;
  line-height: 1.2;
  padding: 0.25rem 0.55rem;
  border-radius: 6px;
}

.ws-tab__new:hover {
  background: #334155;
  color: var(--text);
}

/* Per-tab export action, matching the close button's quiet styling. */
.ws-tab__export {
  background: none;
  border: none;
  color: var(--text-dim);
  cursor: pointer;
  font-size: 0.9rem;
  line-height: 1;
  padding: 0 0.2rem;
  border-radius: 4px;
}
.ws-tab__export:hover {
  background: var(--input-accent);
  color: #fff;
}
