/* ==========================================================================
   Gate Lab styles

   1. Themes   - one block of CSS variables per theme
   2. Base     - resets and shared pieces
   3. Layout   - top bar, side panel, toolbar, canvas stage
   ========================================================================== */

/* --------------------------------------------------------------------------
   1. THEMES

   A theme is exactly one [data-theme="..."] block. Nothing else in this
   file, and nothing in the JavaScript, names a colour.

   TO ADD A THEME: copy a block, change the id in the selector, the
   --theme-name and the values. That is all - the switcher button is built
   from this stylesheet (js/themes.js) and the canvas reads these same
   variables (js/render.js).

   Variables:
     --theme-name     label on the switcher button
     --scheme         "dark" or "light": styles scrollbars and form controls
     --font-ui        font for the page and for text on the canvas

     --bg             page background
     --panel          top bar, side panel and toolbar background
     --raised         buttons, hovered rows
     --border         hairlines
     --text           main text (also switch/lamp labels on the canvas)
     --text-dim       secondary text
     --accent         highlights, focus rings, the wire being drawn
     --accent-text    text on top of --accent
     --ok             checkmarks and success messages
     --danger         errors, rejected wires

     --canvas-bg      canvas background
     --grid           grid colour
     --grid-style     "dots" or "lines"
     --gate-fill      inside of gates
     --gate-stroke    outlines of gates, switches, lamps and ports
     --gate-text      text inside parts
     --wire-off       a wire carrying 0
     --wire-on        a wire carrying 1 (also drawn thicker, with a glow)
     --wire-glow      glow around a live wire
     --on-text        text on top of --wire-on / --lamp-on surfaces
     --lamp-off       unlit lamp
     --lamp-on        lit lamp
     --lamp-glow      halo around a lit lamp
     --switch-off     track of a switch that is off
     --knob           switch knob
     --select         dashed outline of the selected part or wire
   -------------------------------------------------------------------------- */

[data-theme="midnight"] {
  --theme-name: "Midnight Purple";
  --scheme: dark;
  --font-ui: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;

  --bg: #0a0810;
  --panel: #12101b;
  --raised: #1c1829;
  --border: #2e2745;
  --text: #ece7f8;
  --text-dim: #9d94b8;
  --accent: #a855f7;
  --accent-text: #12081f;
  --ok: #4ade80;
  --danger: #fb7185;

  --canvas-bg: #07060c;
  --grid: #231d36;
  --grid-style: dots;
  --gate-fill: #1a1528;
  --gate-stroke: #b7a4e6;
  --gate-text: #e6dcfb;
  --wire-off: #665b8c;
  --wire-on: #d58bff;
  --wire-glow: #a855f7;
  --on-text: #1a0b2e;
  --lamp-off: #1a1528;
  --lamp-on: #e9b6ff;
  --lamp-glow: #c026ff;
  --switch-off: #1a1528;
  --knob: #f3ecff;
  --select: #ffffff;
}

[data-theme="terminal"] {
  --theme-name: "Terminal Green";
  --scheme: dark;
  --font-ui: ui-monospace, "SF Mono", Menlo, Consolas, "DejaVu Sans Mono", monospace;

  --bg: #020602;
  --panel: #061006;
  --raised: #0b1c0c;
  --border: #1a4a1e;
  --text: #b9f8b4;
  --text-dim: #63a862;
  --accent: #33ff66;
  --accent-text: #021a06;
  --ok: #33ff66;
  --danger: #ff7b6b;

  --canvas-bg: #010401;
  --grid: #103314;
  --grid-style: dots;
  --gate-fill: #061206;
  --gate-stroke: #3fbf55;
  --gate-text: #9af09a;
  --wire-off: #1f6a2a;
  --wire-on: #5dff3b;
  --wire-glow: #33ff66;
  --on-text: #021a06;
  --lamp-off: #061206;
  --lamp-on: #b8ff5c;
  --lamp-glow: #33ff66;
  --switch-off: #061206;
  --knob: #d6ffd0;
  --select: #eaffea;
}

[data-theme="blueprint"] {
  --theme-name: "Blueprint";
  --scheme: dark;
  --font-ui: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;

  --bg: #0c2f66;
  --panel: #103a7a;
  --raised: #17488f;
  --border: #4f7fc4;
  --text: #f3f8ff;
  --text-dim: #b3cbee;
  --accent: #ffffff;
  --accent-text: #0c2f66;
  --ok: #8cf0b4;
  --danger: #ffb0a8;

  --canvas-bg: #134089;
  --grid: #2a5cab;
  --grid-style: lines;
  --gate-fill: #134089;
  --gate-stroke: #ffffff;
  --gate-text: #ffffff;
  --wire-off: #6f97d4;
  --wire-on: #ffd84a;
  --wire-glow: #ffb300;
  --on-text: #1a2b4d;
  --lamp-off: #134089;
  --lamp-on: #ffe066;
  --lamp-glow: #ffc21a;
  --switch-off: #134089;
  --knob: #ffffff;
  --select: #9ff0ff;
}

[data-theme="light"] {
  --theme-name: "Light";
  --scheme: light;
  --font-ui: system-ui, -apple-system, "Segoe UI", Roboto, sans-serif;

  --bg: #eef0f4;
  --panel: #ffffff;
  --raised: #f1f2f6;
  --border: #d3d8e0;
  --text: #1b1f2a;
  --text-dim: #596275;
  --accent: #5b3fd6;
  --accent-text: #ffffff;
  --ok: #15803d;
  --danger: #c81e3a;

  --canvas-bg: #fbfbfd;
  --grid: #d0d5df;
  --grid-style: dots;
  --gate-fill: #ffffff;
  --gate-stroke: #2a3040;
  --gate-text: #1b1f2a;
  --wire-off: #8b94a5;
  --wire-on: #e04a00;
  --wire-glow: #ff9d4d;
  --on-text: #2a1000;
  --lamp-off: #e9ecf1;
  --lamp-on: #ffb703;
  --lamp-glow: #ff8c00;
  --switch-off: #e9ecf1;
  --knob: #ffffff;
  --select: #5b3fd6;
}

/* --------------------------------------------------------------------------
   2. BASE
   -------------------------------------------------------------------------- */

*,
*::before,
*::after {
  box-sizing: border-box;
}

:root {
  color-scheme: var(--scheme);
  /* Scrollbars follow the theme too: thumb colour, then track colour. */
  scrollbar-color: var(--border) var(--panel);
}

html,
body {
  height: 100%;
  margin: 0;
}

body {
  display: flex;
  flex-direction: column;
  overflow: hidden;
  background: var(--bg);
  color: var(--text);
  font: 14px/1.45 var(--font-ui);
}

h1,
h2,
h3,
p {
  margin: 0;
}

button {
  font-family: inherit;
}

:focus-visible {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

.visually-hidden {
  position: absolute;
  width: 1px;
  height: 1px;
  overflow: hidden;
  clip-path: inset(50%);
  white-space: nowrap;
}

.button {
  padding: 7px 12px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--raised);
  color: var(--text);
  font-size: 13px;
  font-weight: 600;
  cursor: pointer;
}

.button:hover:not(:disabled) {
  border-color: var(--accent);
}

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

.button.primary {
  border-color: var(--accent);
  background: var(--accent);
  color: var(--accent-text);
}

/* A destructive button waiting for its confirming second click. */
.armed,
.button.armed {
  border-color: var(--danger);
  color: var(--danger);
}

.link-button {
  padding: 2px 0;
  border: 0;
  background: none;
  color: var(--text-dim);
  font-size: 12px;
  text-decoration: underline;
  text-underline-offset: 2px;
  cursor: pointer;
}

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

/* --------------------------------------------------------------------------
   3. LAYOUT
   -------------------------------------------------------------------------- */

/* ---- Top bar ---- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 12px;
  padding: 8px 14px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}

.brand {
  display: flex;
  align-items: center;
  gap: 8px;
  font-size: 17px;
  font-weight: 700;
  letter-spacing: 0.01em;
  white-space: nowrap;
}

.brand svg {
  width: 30px;
  height: 22px;
  fill: none;
  stroke: var(--accent);
  stroke-width: 2.2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.theme-switcher {
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: 8px;
}

/* Each button carries its own data-theme attribute, so the variables used
   here resolve to that theme's values: every button previews itself. */
.theme-button {
  display: inline-flex;
  align-items: center;
  gap: 7px;
  padding: 5px 11px 5px 6px;
  border: 1px solid var(--border);
  border-radius: 999px;
  background: var(--bg);
  color: var(--text);
  font: 600 12px var(--font-ui);
  cursor: pointer;
}

.theme-swatch {
  width: 16px;
  height: 16px;
  border: 2px solid var(--gate-stroke);
  border-radius: 50%;
  background: var(--wire-on);
}

.theme-button[aria-pressed="true"] {
  outline: 2px solid var(--accent);
  outline-offset: 2px;
}

/* ---- Side panel ---- */

.layout {
  display: grid;
  flex: 1;
  grid-template-columns: 310px minmax(0, 1fr);
  min-height: 0;
}

.panel {
  padding: 14px;
  overflow-y: auto;
  border-right: 1px solid var(--border);
  background: var(--panel);
}

.panel h2 {
  margin-bottom: 8px;
  color: var(--text-dim);
  font-size: 11px;
  font-weight: 700;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.lesson-list {
  display: grid;
  gap: 3px;
  margin: 0;
  padding: 0;
  list-style: none;
}

.lesson-item {
  display: flex;
  align-items: center;
  gap: 10px;
  width: 100%;
  padding: 7px 9px;
  border: 1px solid transparent;
  border-radius: 8px;
  background: none;
  color: var(--text);
  font-size: 14px;
  text-align: left;
  cursor: pointer;
}

.lesson-item:hover {
  background: var(--raised);
}

.lesson-item.active {
  border-color: var(--accent);
  background: var(--raised);
  font-weight: 600;
}

.lesson-mark {
  display: grid;
  flex: none;
  place-items: center;
  width: 24px;
  height: 24px;
  border: 1.5px solid var(--border);
  border-radius: 50%;
  color: var(--text-dim);
  font-size: 12px;
  font-weight: 700;
}

.lesson-item.done .lesson-mark {
  border-color: var(--ok);
  background: var(--ok);
  color: var(--panel);
}

.progress-row {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 8px;
  margin: 10px 2px 0;
  color: var(--text-dim);
  font-size: 12px;
  white-space: nowrap;
}

.detail {
  display: grid;
  gap: 10px;
  margin-top: 14px;
  padding-top: 14px;
  border-top: 1px solid var(--border);
}

.detail h3 {
  margin-bottom: 6px;
  font-size: 16px;
}

.detail-live {
  display: grid;
  gap: 8px;
  justify-items: start;
}

.hint {
  margin-top: 8px;
  color: var(--text-dim);
  font-size: 13px;
}

.hint summary {
  width: fit-content;
  cursor: pointer;
}

.hint p {
  margin-top: 4px;
}

.truth-table {
  width: 100%;
  border-collapse: collapse;
  font-variant-numeric: tabular-nums;
  text-align: center;
}

.truth-table caption {
  padding-bottom: 6px;
  color: var(--text-dim);
  font-size: 12px;
  text-align: left;
}

.truth-table th {
  padding: 3px 6px;
  font-size: 12px;
  font-weight: 700;
}

.truth-table th.group {
  color: var(--text-dim);
  font-size: 10px;
  letter-spacing: 0.08em;
  text-transform: uppercase;
}

.truth-table thead tr:last-child th {
  border-bottom: 1px solid var(--border);
}

.truth-table td {
  padding: 5px 6px;
}

/* Many columns (the bigger lessons): tighter cells so the table fits. */
.truth-table.wide th,
.truth-table.wide td {
  padding: 3px 2px;
  font-size: 11px;
}

.truth-table.wide .mark {
  width: 18px;
}

/* First column of each group (after the first) gets a dividing line. */
.truth-table .group-start:not(:first-child),
.truth-table th.group:not(:first-child) {
  border-left: 1px solid var(--border);
}

.truth-table .bit.on {
  font-weight: 700;
}

.truth-table .bit.off {
  color: var(--text-dim);
}

/* The row that matches the switches right now. */
.truth-table tr.live td {
  background: var(--raised);
}

.truth-table tr.live td:first-child {
  box-shadow: inset 3px 0 0 var(--accent);
}

.truth-table .mark {
  width: 28px;
  font-weight: 700;
}

.truth-table .mark.ok {
  color: var(--ok);
}

.truth-table .mark.bad {
  color: var(--danger);
}

.lesson-status {
  font-weight: 600;
}

.lesson-status.solved {
  color: var(--ok);
}

.lesson-issue {
  color: var(--danger);
  font-size: 13px;
}

.lesson-note {
  color: var(--text-dim);
  font-size: 13px;
}

/* ---- Toolbar ---- */

.workspace {
  display: flex;
  flex-direction: column;
  min-width: 0;
  min-height: 0;
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 8px 16px;
  padding: 8px 12px;
  border-bottom: 1px solid var(--border);
  background: var(--panel);
}

.palette,
.actions {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
}

.palette-divider {
  align-self: stretch;
  width: 1px;
  margin: 4px 4px;
  background: var(--border);
}

.part {
  display: grid;
  justify-items: center;
  gap: 1px;
  width: 62px;
  padding: 5px 4px 4px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--raised);
  color: var(--text);
  font-size: 11px;
  font-weight: 600;
  cursor: grab;
  /* Dragging a part out of the toolbar must not scroll or select text. */
  touch-action: none;
  user-select: none;
}

.part svg {
  width: 44px;
  height: 28px;
  fill: none;
  stroke: currentColor;
  stroke-width: 1.8;
  stroke-linecap: round;
  stroke-linejoin: round;
  pointer-events: none;
}

.part:hover:not(:disabled) {
  border-color: var(--accent);
}

.part:disabled {
  opacity: 0.38;
  cursor: not-allowed;
}

/* ---- Custom chips in the toolbar ---- */

.chip-palette {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 6px;
  margin-left: 4px;
  padding-left: 10px;
  border-left: 1px solid var(--border);
}

.chip-palette[hidden] {
  display: none;
}

.chip-slot {
  position: relative;
  display: inline-flex;
}

.chip-part {
  width: auto;
  min-width: 62px;
  padding-right: 8px;
  padding-left: 8px;
  border-color: var(--accent);
}

/* Small "x" on the corner of a chip button. */
.chip-delete {
  position: absolute;
  top: -6px;
  right: -6px;
  display: grid;
  place-items: center;
  width: 18px;
  height: 18px;
  padding: 0;
  border: 1px solid var(--border);
  border-radius: 50%;
  background: var(--panel);
  color: var(--text-dim);
  font-size: 13px;
  line-height: 1;
  cursor: pointer;
}

.chip-delete:hover {
  border-color: var(--danger);
  color: var(--danger);
}

/* ---- Dialog ("Save as chip") ---- */

.dialog {
  width: min(380px, calc(100vw - 32px));
  padding: 18px;
  border: 1px solid var(--accent);
  border-radius: 12px;
  background: var(--panel);
  color: var(--text);
}

.dialog::backdrop {
  background: var(--bg);
  opacity: 0.7;
}

.dialog form {
  display: grid;
  gap: 10px;
}

.dialog h2 {
  font-size: 17px;
}

.dialog p {
  color: var(--text-dim);
  font-size: 13px;
}

.dialog .dialog-ports {
  color: var(--text);
  font-weight: 600;
}

.dialog label {
  font-size: 12px;
  font-weight: 600;
}

.dialog input {
  padding: 7px 9px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--bg);
  color: var(--text);
  font: 600 14px var(--font-ui);
}

.dialog .dialog-error {
  min-height: 1.4em;
  color: var(--danger);
  font-weight: 600;
}

.dialog-buttons {
  display: flex;
  justify-content: flex-end;
  gap: 8px;
}

/* Label that follows the pointer while a part is dragged off the toolbar. */
.drag-chip {
  position: fixed;
  z-index: 10;
  padding: 3px 8px;
  border-radius: 6px;
  background: var(--accent);
  color: var(--accent-text);
  font-size: 12px;
  font-weight: 600;
  pointer-events: none;
  transform: translate(12px, 12px);
}

/* ---- Canvas stage ---- */

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

#canvas {
  position: absolute;
  inset: 0;
  display: block;
  width: 100%;
  height: 100%;
  cursor: grab;
  /* The canvas handles its own panning; stop the browser scrolling instead. */
  touch-action: none;
}

#canvas:focus-visible {
  outline-offset: -2px;
}

.status {
  position: absolute;
  bottom: 10px;
  left: 10px;
  max-width: calc(100% - 20px);
  padding: 5px 10px;
  border: 1px solid var(--border);
  border-radius: 8px;
  background: var(--panel);
  color: var(--text-dim);
  font-size: 12px;
  pointer-events: none;
}

.status[data-kind="info"] {
  color: var(--text);
}

.status[data-kind="error"] {
  border-color: var(--danger);
  color: var(--danger);
  font-weight: 600;
}

.status[data-kind="success"] {
  border-color: var(--ok);
  color: var(--ok);
  font-weight: 600;
}

/* Text field that appears over a switch's or lamp's label to rename it. */
.label-editor {
  position: absolute;
  z-index: 4;
  width: 92px;
  padding: 2px 4px;
  border: 1px solid var(--accent);
  border-radius: 6px;
  background: var(--panel);
  color: var(--text);
  font: 600 12px var(--font-ui);
  text-align: center;
  transform: translate(-50%, -50%);
}

/* ---- Toasts: brief messages over the top of the canvas ---- */

.toasts {
  position: absolute;
  top: 12px;
  left: 50%;
  z-index: 5;
  display: grid;
  gap: 6px;
  justify-items: center;
  width: max-content;
  max-width: calc(100% - 24px);
  pointer-events: none;
  transform: translateX(-50%);
}

.toast {
  display: flex;
  align-items: center;
  gap: 10px;
  padding: 7px 12px;
  border: 1px solid var(--accent);
  border-radius: 8px;
  background: var(--panel);
  color: var(--text);
  font-size: 13px;
  font-weight: 600;
}

.toast[data-kind="error"] {
  border-color: var(--danger);
  color: var(--danger);
}

.toast[data-kind="success"] {
  border-color: var(--ok);
  color: var(--ok);
}

/* Toasts let clicks through to the canvas beneath; only a button on one
   (such as "Undo") can be pressed. */
.toast-action {
  pointer-events: auto;
  padding: 2px 8px;
  border: 1px solid var(--border);
  border-radius: 6px;
  background: var(--raised);
  color: var(--text);
  font-size: 12px;
  font-weight: 600;
  cursor: pointer;
}

.toast-action:hover {
  border-color: var(--accent);
}

/* ---- Narrow screens: panel above the canvas ---- */

@media (max-width: 760px) {
  .layout {
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: minmax(0, 40%) minmax(0, 1fr);
  }

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

  .theme-name {
    display: none;
  }

  .theme-button {
    padding: 5px;
  }

  /* Keep all six parts on one row, and give the small canvas its space. */
  .part {
    width: 52px;
  }

  .status[data-kind="hint"] {
    display: none;
  }
}
