/* Circuit builder (assets/js/circuit-builder.js). Load after main.css and starter.css. */

:root {
  --cb-glow: #facc15;          /* lamp light */
  --cb-led: #f472b6;           /* LED light */
  --cb-flow: #1f4fd1;          /* moving current dots */
  --cb-flow-conv: #c2410c;     /* the dots as conventional current, where a page offers both directions */
  --cb-heat: #f97316;          /* heating element glowing */
}
@media (prefers-color-scheme: dark) {
  :root { --cb-flow: #8fb0ff; --cb-flow-conv: #fb923c; }
}

.cb { margin-top: 20px; }

/* Task strip: numbered chips, then the current task card */
.cb-task-nav {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  align-content: flex-start;   /* rows stay at the top of the room kept for them */
  gap: 8px;
  margin-bottom: 10px;
}
.cb-task-nav-label { color: var(--muted); font-weight: 600; margin-right: 4px; }
.cb-chip {
  min-width: var(--tap);
  min-height: var(--tap);
  padding: 4px 14px;
  border: var(--edge-w) solid var(--edge);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--pop-sm);
  color: var(--text);
  font: inherit;
  font-weight: 700;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
  touch-action: manipulation;
}
.cb-chip:active { transform: translate(3px, 3px); box-shadow: none; }
.cb-chip[aria-current] { border-color: var(--level-colour); box-shadow: 0 0 0 3px var(--level-colour); }
/* Done: filled, and says so in words */
.cb-chip.is-done { background: var(--good-bg); border-color: var(--good); color: var(--good); }
.cb-chip.is-done::after { content: " done"; font-weight: 600; font-size: 0.85rem; }
.cb-example { display: none; }
.show-answers .cb-example { display: inline-flex; margin-left: auto; }

/* Holds the tasks; the script fixes its height (see sizeTasks). flow-root keeps the margin inside. */
.cb-tasks { display: flow-root; }
.cb-task { margin-bottom: 12px; }
.cb-task[hidden] { display: none; }
.cb-task .q-prompt { margin-bottom: 0; }
.cb-task .cb-next { margin-left: 8px; vertical-align: middle; }

/* Builder card: palette, status bar, then the grid */
.cb-workspace {
  padding: 12px;
  background: var(--surface);
  border: var(--edge-w) solid var(--edge);
  border-radius: var(--radius);
  box-shadow: var(--pop);
}

.cb-palette {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(74px, 1fr));
  gap: 6px;
  margin: 0;
  padding: 0;
  list-style: none;
}
.cb-palette button {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  width: 100%;
  min-height: 72px;
  padding: 6px 4px;
  border: var(--edge-w) solid var(--edge);
  border-radius: var(--radius);
  background: var(--bg);
  box-shadow: var(--pop-sm);
  color: var(--text);
  font: inherit;
  font-size: 0.85rem;
  font-weight: 600;
  line-height: 1.15;
  cursor: grab;
  /* Dragging out of the palette must not scroll the page */
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
  -webkit-tap-highlight-color: transparent;
}
.cb-palette button:active { transform: translate(3px, 3px); box-shadow: none; }
.cb-palette svg { width: 54px; height: 36px; overflow: visible; }
.cb-palette .cb-letter { font-size: 16px; }

.cb-bar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
  margin: 10px 0;
}
/* The status line and any warning share this slot; the script fixes its height (see sizeMsg) */
.cb-msg {
  flex: 1 1 260px;
  display: grid;
  align-items: center;
}
.cb-msg > * { grid-area: 1 / 1; }
.cb-msg.has-warning .cb-status { visibility: hidden; }
.cb-status {
  margin: 0;
  color: var(--muted);
  font-size: 1rem;
}
.cb-bar .tool { padding: 6px 14px; }
/* "Show electron flow" with "Show conventional flow" underneath */
.cb-flow-pick { display: grid; gap: 6px; }
.cb-flow.is-conventional { stroke: var(--cb-flow-conv); }
.cb-warning {
  margin: 0;
  padding: 10px 14px;
  border-left: 5px solid var(--bad);
  border-radius: 10px;
  background: var(--bad-bg);
  font-size: 1rem;
}
.cb-warning[hidden] { display: none; }
.cb-warning span { display: block; }
.cb-warning span + span { margin-top: 6px; }

/* Light, Temperature and Sound sliders (only shown when a component that senses them is on the grid) */
/* Always shown, so nothing moves: greyed out until there is something on the grid that senses them.
   Name and setting on one line, slider underneath, so all three fit side by side on an iPad. */
.cb-env {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(200px, 1fr));
  gap: 4px 24px;
  margin: -2px 0 10px;
}
.cb-env-item {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 0 10px;
  font-weight: 700;
}
.cb-env-item.is-off { opacity: 0.4; }
.cb-env-item output { grid-row: 1; grid-column: 2; color: var(--muted); font-weight: 600; text-align: right; }
.cb-env input, .cb-slide {
  width: 180px;
  height: var(--tap);
  margin: 0;
  accent-color: var(--level-colour, var(--accent));
  touch-action: manipulation;
}
.cb-env input { grid-row: 2; grid-column: 1 / -1; width: 100%; }
.cb-pick {
  min-height: var(--tap);
  padding: 4px 10px;
  border: var(--edge-w) solid var(--edge);
  border-radius: 10px;
  background: var(--bg);
  color: var(--text);
  font: inherit;
  font-weight: 700;
}

/* The grid. touch-action: none so a finger on the grid draws instead of scrolling;
   it is never taller than the screen, so there is always somewhere to scroll from. */
.cb-stage { position: relative; }
.cb-board {
  display: block;
  width: 100%;
  height: auto;
  max-height: calc(100vh - 140px);
  min-height: 260px;
  color: var(--text);
  touch-action: none;
  -webkit-user-select: none;
  user-select: none;
  -webkit-touch-callout: none;
}
.cb-paper { fill: var(--bg); }
.cb-griddot { fill: var(--muted); opacity: 0.45; }

.cb-wire { stroke: currentColor; stroke-width: 3.5; stroke-linecap: round; }
.cb-wire.is-selected { stroke: var(--accent); stroke-width: 7; }
.cb-wire.is-bad { stroke: var(--bad); stroke-dasharray: 6 8; }
.cb-flow {
  stroke: var(--cb-flow);
  stroke-width: 8;
  stroke-linecap: round;
  stroke-dasharray: 0 40;
}
.cb-spin {
  fill: none;
  stroke: var(--cb-flow);
  stroke-width: 3;
  stroke-dasharray: 6 8.45;
}
.cb-junction { fill: currentColor; }
.cb-end { fill: var(--bg); stroke: var(--accent); stroke-width: 2.5; }
.cb-pending { fill: none; stroke: var(--accent); stroke-width: 4; }
@media (prefers-reduced-motion: no-preference) {
  .cb-pending { animation: cb-pulse 1s ease-in-out infinite alternate; }
  @keyframes cb-pulse { to { stroke-width: 9; } }
}

.cb-comp use { color: currentColor; }
.cb-letter { fill: currentColor; font-size: 21px; font-weight: 700; font-family: var(--font); }
.cb-lit { fill: var(--cb-glow); }
.cb-lit-led { fill: var(--cb-led); }
.cb-sound { fill: none; stroke: var(--cb-flow); stroke-width: 3.5; stroke-linecap: round; }
.cb-comp.is-bad { color: var(--bad); opacity: 0.75; }
.cb-sel { fill: none; stroke: var(--accent); stroke-width: 3; stroke-dasharray: 7 5; }
.cb-short { fill: var(--bad-bg); stroke: var(--bad); stroke-width: 3; }
.cb-lit-heat { fill: var(--cb-heat); opacity: 0.55; }

/* Walkthrough boards: the part being talked about is ringed, the rest fade back */
.cb-focus { fill: none; stroke: var(--level-colour, var(--accent)); stroke-width: 4; }
.cb-comp.is-dim { opacity: 0.3; }
.cb-comp { transition: opacity 0.3s; }
.cb-terminal { fill: var(--bg); stroke: currentColor; stroke-width: 3; }
.cb-rail { fill: currentColor; font-size: 23px; font-weight: 700; font-family: var(--font); }
.cb-note { fill: var(--text); font-size: 21px; font-weight: 700; font-family: var(--font); }
.cb-note.is-reading { fill: var(--level-colour, var(--accent)); font-size: 23px; }
.cb-note.is-pin { fill: var(--muted); font-size: 19px; font-style: italic; }
.cb-note.is-label { fill: var(--muted); font-size: 20px; }
.cb-guided .cb-value { font-size: 20px; }

.cb-reading rect { fill: var(--surface); stroke: var(--text); stroke-width: 2; }
.cb-reading text { fill: var(--text); font-size: 19px; font-weight: 700; font-family: var(--font); }
.cb-value { fill: var(--muted); font-size: 17px; font-weight: 700; font-family: var(--font); }
.cb-name { fill: var(--text); font-size: 20px; }

/* Pop-up next to the selected component or wire */
.cb-pop {
  position: absolute;
  z-index: 5;
  display: flex;
  gap: 6px;
  padding: 6px;
  border: var(--edge-w) solid var(--edge);
  border-radius: 12px;
  background: var(--surface);
  box-shadow: 0 6px 18px rgba(0, 0, 0, 0.2);
}
.cb-pop[hidden] { display: none; }
.cb-pop .tool { box-shadow: none; }
.cb-pop [hidden] { display: none; }

/* The symbol that follows a finger while it is dragged in from the palette */
.cb-ghost {
  position: fixed;
  left: -40px;
  top: -27px;
  z-index: 50;
  width: 80px;
  height: 54px;
  pointer-events: none;
  color: var(--text);
  opacity: 0.85;
}
.cb-ghost[hidden] { display: none; }
.cb-ghost svg { width: 100%; height: 100%; overflow: visible; }

.cb-help { margin: 16px 0 0; color: var(--muted); font-size: 1rem; }
.cb-help li + li { margin-top: 4px; }

/* ---------- Walkthroughs (assets/js/circuit-walkthroughs.js) ---------- */
/* Walkthroughs page (n5/circuit-walkthroughs.html) */
.wt-section { margin-top: 24px; }
.wt-intro { margin: 0 0 16px; }
.wt-picker h2 { margin: 18px 0 10px; font-size: 1.15rem; color: var(--muted); }
.wt-cards {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(230px, 1fr));
  gap: 12px;
  margin: 0;
  padding: 0;
  list-style: none;
}
.wt-card {
  display: grid;
  grid-template-columns: auto 1fr;
  gap: 2px 12px;
  height: 100%;
  padding: 14px 16px;
  border: var(--edge-w) solid var(--edge);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--pop);
  color: var(--text);
  text-decoration: none;
  -webkit-tap-highlight-color: transparent;
}
.wt-card:active { transform: translate(4px, 4px); box-shadow: none; }
.wt-card-num {
  grid-row: span 2;
  display: grid;
  place-items: center;
  width: 44px;
  height: 44px;
  border-radius: 50%;
  background: var(--level-colour, var(--accent));
  color: var(--on-level);
  font-family: var(--font-head);
  font-size: 1.25rem;
  font-weight: 700;
}
.wt-card-title { font-weight: 700; font-size: 1.1rem; line-height: 1.25; }
.wt-card-sub { color: var(--muted); font-size: 0.95rem; line-height: 1.3; }
/* Finished: says so in words as well as colour */
.wt-card.is-done { border-color: var(--good); background: var(--good-bg); }
.wt-card.is-done .wt-card-title::after { content: " (done)"; color: var(--good); font-weight: 600; font-size: 0.9rem; }

.wt-player[hidden], .wt-picker[hidden], .wt[hidden], .wt-step[hidden], .wt-name[hidden] { display: none; }
.wt-head {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 10px 14px;
  margin-bottom: 12px;
}
.wt-title { flex: 1 1 260px; margin: 0; font-family: var(--font-head); font-size: 1.4rem; }
.wt-head .tool { text-decoration: none; }
.wt-dots { display: flex; flex-wrap: wrap; gap: 6px; margin: 0; padding: 0; list-style: none; }
.wt-dot {
  min-width: var(--tap);
  min-height: var(--tap);
  border: var(--edge-w) solid var(--edge);
  border-radius: 50%;
  background: var(--surface);
  color: var(--text);
  font: inherit;
  font-weight: 700;
  cursor: pointer;
  touch-action: manipulation;
  -webkit-tap-highlight-color: transparent;
}
.wt-dot[aria-current] { background: var(--level-colour, var(--accent)); border-color: var(--level-colour, var(--accent)); color: var(--on-level); }

.wt-body { display: grid; gap: 16px; }
.wt-board {
  padding: 10px;
  border: var(--edge-w) solid var(--edge);
  border-radius: var(--radius);
  background: var(--surface);
  box-shadow: var(--pop);
}
.cb.cb-guided { margin: 0; }
/* Nothing to draw on a walkthrough board, so a finger on it scrolls the page as normal */
.cb-guided .cb-board { touch-action: manipulation; min-height: 0; max-height: 60vh; }
.cb-guided .cb-paper { fill: var(--surface); }
.wt-tapinfo {
  min-height: 3.2em;
  margin: 8px 0 0;
  padding: 8px 12px;
  border-left: 5px solid var(--level-colour, var(--accent));
  border-radius: 10px;
  background: var(--bg);
  font-size: 1rem;
}
.wt-tapinfo strong { margin-right: 4px; }
@media (min-width: 900px) {
  .wt-body { grid-template-columns: minmax(0, 1.55fr) minmax(0, 1fr); align-items: start; }
  .wt-board { position: sticky; top: 10px; }
}
/* Longer questions: one column, with a smaller circuit above */
.wt-player.is-wide .wt-body { grid-template-columns: minmax(0, 1fr); }
.wt-player.is-wide .wt-board { position: static; }
.wt-player.is-wide .cb-guided .cb-board { max-height: 34vh; }
/* iPad portrait: the circuit stays in view at the top while the steps scroll underneath */
@media (max-width: 899px) and (min-height: 700px) {
  .wt-board { position: sticky; top: 0; z-index: 4; }
  .cb-guided .cb-board { max-height: 38vh; }
}

.wt-step h3 { margin: 0 0 8px; font-family: var(--font-head); font-size: 1.25rem; }
.wt-step > p { margin: 0 0 12px; }
.wt-step .q { margin-top: 14px; padding: 16px 18px; }
.wt-step .q-prompt { font-size: 1.15rem; }
/* The panel is narrow beside the circuit, so longer answers go one per row */
.wt-step .options:not(.is-row):not(.is-pair) { grid-template-columns: minmax(0, 1fr); }
.wt-step .option { min-height: 48px; padding: 10px 14px; font-size: 1.05rem; }
.wt-tip {
  margin: 12px 0;
  padding: 12px 16px;
  border: 2px dashed var(--level-colour, var(--accent));
  border-radius: 12px;
}
.wt-tip p { margin: 0; }
.wt-tip p + p { margin-top: 6px; }
.wt-step mark {
  padding: 0 3px;
  border-radius: 4px;
  background: rgba(250, 204, 21, 0.45);
  color: inherit;
}

.wt-live { margin-top: 14px; }
.wt-controls { display: grid; gap: 10px; }
.wt-controls.is-locked { opacity: 0.5; }
.wt-controls .tool { justify-self: start; }
.wt-slider {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  align-items: center;
  gap: 0 12px;
  padding: 8px 14px;
  border: 2px solid var(--border);
  border-radius: 12px;
  background: var(--bg);
}
.wt-slider-name { grid-row: 1; grid-column: 1; font-weight: 700; }
.wt-slider input { grid-column: 1 / -1; grid-row: 2; width: 100%; height: var(--tap); margin: 0; accent-color: var(--level-colour, var(--accent)); touch-action: manipulation; }
.wt-slider output { grid-row: 1; grid-column: 2; font-weight: 700; color: var(--level-colour, var(--accent)); text-align: right; }
.wt-slider-ends { grid-column: 1 / -1; display: flex; justify-content: space-between; margin-top: -6px; color: var(--muted); font-size: 0.85rem; }
.wt-lock { margin: 6px 0 0; color: var(--muted); font-weight: 600; }
.wt-lock[hidden] { display: none; }

.wt-graph svg { display: block; width: 100%; max-width: 340px; margin: 10px auto 0; }
.wt-axis { fill: none; stroke: var(--text); stroke-width: 2; }
.wt-curve { fill: none; stroke: var(--level-colour, var(--accent)); stroke-width: 3.5; }
.wt-axis-text { fill: var(--muted); font-size: 13px; font-family: var(--font); }
.wt-axis-title { fill: var(--text); font-size: 14px; font-weight: 700; font-family: var(--font); }
.wt-dot-now { fill: var(--cb-heat); stroke: var(--text); stroke-width: 2; }

/* The chain: each link lights up when it happens */
.wt-chain { display: grid; gap: 8px; margin: 14px 0 0; padding: 0; list-style: none; }
.wt-chain.is-arrows { gap: 18px; }
.wt-link {
  position: relative;
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto;
  gap: 2px 10px;
  align-items: baseline;
  padding: 7px 12px;
  border: var(--edge-w) solid var(--border);
  border-radius: 12px;
  background: var(--bg);
  transition: background 0.3s, border-color 0.3s;
}
.wt-link-label { font-weight: 600; }
.wt-link-value { font-weight: 700; font-size: 1.15rem; font-variant-numeric: tabular-nums; text-align: right; }
.wt-trend { color: var(--muted); font-size: 0.85rem; font-weight: 700; white-space: nowrap; }
.wt-link.is-on { border-color: var(--level-colour, var(--accent)); background: var(--surface); box-shadow: 0 0 0 3px var(--level-colour, var(--accent)); }
.wt-link.is-on .wt-link-value { color: var(--level-colour, var(--accent)); }
/* A short arrow down to the next link (drawn, not a character) */
.wt-chain.is-arrows .wt-link + .wt-link::before {
  content: "";
  position: absolute;
  left: 28px;
  top: -17px;
  width: 3px;
  height: 10px;
  background: var(--muted);
}
.wt-chain.is-arrows .wt-link + .wt-link::after {
  content: "";
  position: absolute;
  left: 23.5px;
  top: -8px;
  border: 6px solid transparent;
  border-top: 7px solid var(--muted);
}
.wt-meter {
  position: relative;
  grid-column: 1 / -1;
  height: 16px;
  margin: 4px 0 20px;
  border: 2px solid var(--text);
  border-radius: 8px;
  background: var(--surface);
}
.wt-meter-fill { position: absolute; top: 0; bottom: 0; left: 0; border-radius: 6px; background: var(--level-colour, var(--accent)); transition: width 0.15s; }
.wt-meter-mark { position: absolute; top: -6px; bottom: -6px; width: 3px; margin-left: -1.5px; background: var(--bad); }
.wt-meter-text {
  position: absolute;
  top: 18px;
  transform: translateX(-50%);
  white-space: nowrap;
  color: var(--bad);
  font-size: 0.85rem;
  font-weight: 700;
}
.wt-meter-mark[hidden], .wt-meter-text[hidden] { display: none; }
.wt-extra { margin: 12px 0 0; padding: 10px 14px; border-radius: 10px; background: var(--bg); }
.wt-extra:empty { display: none; }

.wt-nav {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  margin-top: 18px;
  padding-top: 14px;
  border-top: 2px solid var(--border);
}
.wt-count { color: var(--muted); font-weight: 700; }
.wt-nav .tool:disabled { opacity: 0.4; }
.wt-next { background: var(--level-colour, var(--accent)); border-color: var(--level-colour, var(--accent)); color: var(--on-level); }

/* On the builder page: a pointer to the walkthroughs */
.cb-see-also { margin: 0 0 16px; }

/* Messages about a wrong circuit have bold words inside the sentence: only the first one is a title */
.cb-task .q-feedback strong ~ strong { margin-right: 0; }

/* Virtual experiments (bge/current-in-parallel.html): the results table to copy, and the conclusion */
.exp-setup { margin: 0 0 20px; }
.exp-setup h2, .exp-heading { margin: 0 0 6px; font-size: 1.3rem; }
.exp-setup p, .exp-lead { margin: 0 0 12px; }
.exp-setup .data-table td { min-width: 5em; height: 2.4em; }
.exp-heading { margin-top: 32px; }

/* Tasks answered in boxes (.cb-answers): a row of small labelled number boxes and a Check button */
.cb-answers { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 16px; margin: 12px 0 4px; font-size: 1.15rem; }
.cb-answers label { display: inline-flex; align-items: center; gap: 6px; font-weight: 700; white-space: nowrap; }
.cb-answers .answer { width: 3.6em; min-height: 46px; padding: 6px 8px; font-size: 1.2rem; text-align: center; }
.cb-answers .answer:disabled { opacity: 1; -webkit-text-fill-color: currentColor; }
