:root {
  /* Surfaces */
  --bg: #000000;
  --panel: #000000;
  --panel-2: #1a1448;
  --well: #000000;
  --well-2: #0b0826;
  --panel-edge: #5a4da8;
  --panel-edge-dim: #2e2468;
  --pane-edge: #000000;
  --wire: #e8edf2;
  --value: #ffffff;

  /* Text */
  --ink: #e8edf2;
  --muted: #a79fe0;
  --faint: #8a83c4;
  --gold: #ffd86b;

  /* Top bar and section headers: shared with syntaxor and chewring for a consistent sibling look */
  --head-bg: #4a3d8f;
  --head-bg-mid: #6d5eb5;
  --head-bg-end: #8070c8;
  --panel-head-top: #3a2e80;
  --head-ink: #ffffff;
  --logo-ring: var(--head-ink);
  --logo-bit: var(--amber);
  --btn-bg: #2b2f36;
  --btn-edge: #98a5b9;
  --btn-ink: #ffffff;
  --btn-hover: #3b414a;
  --run-bg: #17803f;
  --run-edge: #8dffb4;
  --run-ink: #ffffff;
  --pause-bg: #a8680a;
  --pause-edge: #ffe09a;
  --tab-active-bg: #170f45;
  --thumb: #6d5eb5;

  --amber: #ffb000;
  --green: #39ff7a;
  --cyan: #19b5ff;
  --magenta: #ff4fd8;
  --red: #ff6d6d;

  --bus-address: #ffb000;
  --bus-data: #0a9dff;
  --bus-control: #ff17c6;

  --mono: "Cascadia Mono", "SF Mono", Consolas, "Liberation Mono", Menlo, "Courier New", monospace;
  --sans: "Trebuchet MS", "Segoe UI", system-ui, sans-serif;

  --f-op: #ff7a7a;
  --f-mode: #ffd86b;
  --f-rd: #7dff9b;
  --f-rn: #7df9ff;
  --f-op2: #d5a8ff;

  --chip-bg: #0b3a22;
  --chip-ink: #7dff9b;
  --flag-on-bg: #39ff7a;
  --flag-on-ink: #000000;
  --flash-bg: #6b58f0;
  --reg-active-bg: #2a2070;

  /* Editor syntax colours */
  --syn-keyword: #ffd86b;
  --syn-register: #7df9ff;
  --syn-number: #ff9ecb;
  --syn-comment: #8a84b8;
  --syn-label: #7dff9b;
  --syn-string: #ffb38a;
  --syn-operator: #c9c3ee;
  --gutter-ink: #7c74b8;
  --selection: #4a3d8f;

  /* States and feedback */
  --active-line: rgba(255, 255, 255, 0.05);
  --error-bg: rgba(255, 109, 109, 0.16);
  --error-edge: var(--red);
  --exec-bg: rgba(57, 255, 122, 0.17);
  --exec-edge: var(--green);
  --msg-error-bg: rgba(255, 109, 109, 0.12);
  --msg-error-edge: var(--red);
  --msg-error-ink: #ff8a8a;
  --msg-warn-bg: rgba(255, 176, 0, 0.1);
  --msg-warn-edge: var(--amber);
  --msg-warn-ink: #ffcc66;
  --msg-ok-bg: rgba(57, 255, 122, 0.08);
  --msg-ok-edge: var(--green);
  --msg-ok-ink: #b8ffd0;
  --phase-ink: #000000;
  --phase-stop-ink: #000000;
  --phase-idle: var(--faint);
  --phase-fetch: var(--amber);
  --phase-decode: var(--cyan);
  --phase-execute: var(--green);
  --phase-input: #ffd000;
  --phase-stop: var(--red);
  --attention: var(--phase-input);
  --attention-ink: #000000;
  --trace-wave: #c9c3ee;
  --glow: 0 0 6px rgba(57, 255, 122, 0.55);
  --focus-glow: 0 0 10px rgba(255, 79, 216, 0.5);
  --swatch-edge: rgba(255, 255, 255, 0.35);
}

/* ---------- colour themes ---------- */

/* ZX Spectrum: black with saturated primaries */
[data-theme="spectrum"] {
  --phase-input: var(--magenta);
  --panel: #000000;
  --panel-2: #000000;
  --well-2: #000000;
  --panel-edge: #00d7d7;
  --panel-edge-dim: #0000d7;
  --wire: #ffffff;
  --pane-edge: var(--panel-edge-dim);
  --ink: #ffffff;
  --muted: #d7d7d7;
  --faint: #b0b0b0;
  --gold: #ffff00;
  --head-bg: #0000d7;
  --btn-bg: #000000;
  --btn-edge: #ffffff;
  --btn-hover: #0000d7;
  --run-bg: #00d700;
  --run-edge: #00ff00;
  --run-ink: #000000;
  --pause-bg: #ffff00;
  --pause-edge: #ffffff;
  --tab-active-bg: #0000d7;
  --thumb: #00d7d7;
  --amber: #ffff00;
  --green: #00ff00;
  --cyan: #00ffff;
  --magenta: #ff00ff;
  --red: #ff0000;
  --f-op: #ff0000;
  --f-mode: #ffff00;
  --f-rd: #00ff00;
  --f-rn: #00ffff;
  --f-op2: #ff00ff;
  --chip-bg: #00d700;
  --chip-ink: #000000;
  --flag-on-bg: #00ff00;
  --flash-bg: #0000d7;
  --reg-active-bg: #0000d7;
  --syn-keyword: #ffff00;
  --syn-register: #00ffff;
  --syn-number: #ff00ff;
  --syn-comment: #b0b0b0;
  --syn-label: #00ff00;
  --syn-string: #ff6a00;
  --syn-operator: #ffffff;
  --gutter-ink: #00d7d7;
  --selection: #0000d7;
}

/* Green phosphor monitor */
[data-theme="phosphor"] {
  --phase-input: var(--magenta);
  --panel: #000000;
  --panel-2: #000000;
  --well-2: #000000;
  --panel-edge: #1fbf55;
  --panel-edge-dim: #0d5a26;
  --wire: #39ff7a;
  --pane-edge: var(--panel-edge-dim);
  --ink: #39ff7a;
  --muted: #1fbf55;
  --faint: #15913f;
  --gold: #39ff7a;
  --head-bg: #063d17;
  --head-ink: #39ff7a;
  --btn-bg: #000000;
  --btn-edge: #1fbf55;
  --btn-ink: #39ff7a;
  --btn-hover: #0d5a26;
  --run-bg: #1fbf55;
  --run-edge: #39ff7a;
  --run-ink: #000000;
  --pause-bg: #ffb000;
  --pause-edge: #ffd000;
  --tab-active-bg: #063d17;
  --thumb: #1fbf55;
  --f-op: #ff5a5a;
  --f-mode: #ffd000;
  --f-rd: #39ff7a;
  --f-rn: #19d4ff;
  --f-op2: #ff6af0;
  --chip-bg: #0d5a26;
  --chip-ink: #39ff7a;
  --flash-bg: #0d5a26;
  --reg-active-bg: #0d5a26;
  --syn-keyword: #ffffff;
  --syn-register: #19d4ff;
  --syn-number: #ffd000;
  --syn-comment: #15913f;
  --syn-label: #39ff7a;
  --syn-string: #ffb000;
  --syn-operator: #1fbf55;
  --gutter-ink: #15913f;
  --selection: #0d5a26;
}

/* Amber monochrome terminal */
[data-theme="amber"] {
  --phase-input: var(--magenta);
  --panel: #000000;
  --panel-2: #000000;
  --well-2: #000000;
  --panel-edge: #ffb000;
  --panel-edge-dim: #6b4800;
  --wire: #ffcc33;
  --pane-edge: var(--panel-edge-dim);
  --value: #ffcc33;
  --ink: #ffb000;
  --muted: #d99100;
  --faint: #a36d00;
  --gold: #ffcc33;
  --head-bg: #3d2800;
  --head-ink: #ffb000;
  --logo-bit: #ffffff;
  --btn-bg: #000000;
  --btn-edge: #ffb000;
  --btn-ink: #ffb000;
  --btn-hover: #4a3000;
  --run-bg: #ffb000;
  --run-edge: #ffcc33;
  --run-ink: #000000;
  --pause-bg: #ff6a00;
  --pause-edge: #ff9a40;
  --tab-active-bg: #3d2800;
  --thumb: #ffb000;
  --green: #ffb000;
  --f-op: #ff5a1f;
  --f-mode: #ffcc33;
  --f-rd: #ffb000;
  --f-rn: #ffd980;
  --f-op2: #ff8a3d;
  --chip-bg: #4a3000;
  --chip-ink: #ffcc33;
  --flag-on-bg: #ffb000;
  --flash-bg: #4a3000;
  --reg-active-bg: #4a3000;
  --syn-keyword: #ffffff;
  --syn-register: #ffcc33;
  --syn-number: #ff8a3d;
  --syn-comment: #b07600;
  --syn-label: #ffb000;
  --syn-string: #ff8a3d;
  --syn-operator: #d99100;
  --gutter-ink: #b07600;
  --selection: #4a3000;
}

/* Textbook: black on white with greys. High contrast, easy to photocopy or screenshot for worksheets. */
[data-theme="textbook"] {
  --bg: #ffffff;
  --panel: #ffffff;
  --panel-2: #f0f0f0;
  --well: #ffffff;
  --well-2: #ffffff;
  --panel-edge: #000000;
  --panel-edge-dim: #8c8c8c;
  --wire: #000000;
  --pane-edge: var(--panel-edge-dim);
  --ink: #000000;
  --muted: #333333;
  --faint: #595959;
  --gold: #000000;
  --value: #000000;
  --head-bg: #ffffff;
  --head-ink: #000000;
  --logo-bit: #737373;
  --btn-bg: #ffffff;
  --btn-edge: #000000;
  --btn-ink: #000000;
  --btn-hover: #dcdcdc;
  --run-bg: #000000;
  --run-edge: #000000;
  --run-ink: #ffffff;
  --pause-bg: #595959;
  --pause-edge: #000000;
  --tab-active-bg: #e6e6e6;
  --thumb: #737373;
  --amber: #000000;
  --green: #000000;
  --cyan: #0b3d91;
  --magenta: #000000;
  --red: #b3001b;
  --bus-address: #1a1a1a;
  --bus-data: #1a1a1a;
  --bus-control: #1a1a1a;
  --f-op: #a33f00;
  --f-mode: #7a5c00;
  --f-rd: #00705a;
  --f-rn: #0b4f9c;
  --f-op2: #7a2a8a;
  --chip-bg: #e0e0e0;
  --chip-ink: #000000;
  --flag-on-bg: #000000;
  --flag-on-ink: #ffffff;
  --flash-bg: #cfcfcf;
  --reg-active-bg: #d9d9d9;
  --syn-keyword: #000000;
  --syn-register: #0b3d91;
  --syn-number: #7a2e00;
  --syn-comment: #595959;
  --syn-label: #00663a;
  --syn-string: #7a2e00;
  --syn-operator: #333333;
  --gutter-ink: #595959;
  --selection: #cfcfcf;
  --active-line: rgba(0, 0, 0, 0.06);
  --error-bg: #fbe3e6;
  --error-edge: #b3001b;
  --exec-bg: #d9d9d9;
  --exec-edge: #000000;
  --msg-error-bg: #fbe3e6;
  --msg-error-edge: #b3001b;
  --msg-error-ink: #6b0010;
  --msg-warn-bg: #f2f2f2;
  --msg-warn-edge: #595959;
  --msg-warn-ink: #000000;
  --msg-ok-bg: #f2f2f2;
  --msg-ok-edge: #000000;
  --msg-ok-ink: #000000;
  --phase-ink: #000000;
  --phase-stop-ink: #ffffff;
  --phase-idle: #e0e0e0;
  --phase-fetch: #d6d6d6;
  --phase-decode: #b8b8b8;
  --phase-execute: #9c9c9c;
  --phase-input: #ffffff;
  --phase-stop: #b3001b;
  --attention: #000000;
  --attention-ink: #ffffff;
  --trace-wave: #000000;
  --glow: none;
  --focus-glow: 0 0 0 2px #000000;
  --swatch-edge: rgba(0, 0, 0, 0.6);
}

* {
  box-sizing: border-box;
}

[hidden] {
  display: none !important;
}

html,
body {
  margin: 0;
  height: 100%;
  background: var(--bg);
  color: var(--ink);
  font-family: var(--sans);
  -webkit-text-size-adjust: 100%;
}

body {
  display: flex;
  flex-direction: column;
  height: 100dvh;
  overflow: hidden;
  padding-bottom: env(safe-area-inset-bottom);
}

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

/* ---------- scrollbars ---------- */

::-webkit-scrollbar {
  width: 15px;
  height: 15px;
}

::-webkit-scrollbar-track {
  background: var(--well-2);
  box-shadow: inset 0 0 0 1px var(--panel-edge-dim);
}

::-webkit-scrollbar-thumb {
  min-width: 44px;
  min-height: 44px;
  background: var(--thumb);
  border: 3px solid var(--well-2);
  border-radius: 9px;
}

::-webkit-scrollbar-thumb:hover {
  background: var(--amber);
}

::-webkit-scrollbar-thumb:active {
  background: var(--gold);
}

::-webkit-scrollbar-corner {
  background: var(--well-2);
}

/* Firefox has no pseudo-element scrollbars, so it gets the colours only. */
@supports not selector(::-webkit-scrollbar) {
  * {
    scrollbar-color: var(--thumb) var(--well-2);
  }
}

/* ---------- top bar ---------- */

.topbar {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px 18px;
  margin: 10px 10px 0;
  padding: 8px 14px;
  padding-top: calc(8px + env(safe-area-inset-top));
  background: linear-gradient(135deg, var(--head-bg) 0%, var(--head-bg-mid) 50%, var(--head-bg-end) 100%);
  border: 1px solid var(--panel-edge);
  border-radius: 10px;
  box-shadow: 0 6px 18px rgba(25, 14, 66, 0.5);
}

.toolbar {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  justify-content: space-between;
  gap: 10px 18px;
  margin: 8px 10px;
  padding: 8px 14px;
  background: linear-gradient(180deg, var(--panel-head-top), var(--panel-edge-dim));
  border: 1px solid var(--panel-edge);
  border-radius: 10px;
}

.brand {
  display: flex;
  align-items: center;
  gap: 12px;
  margin-right: 6px;
}

.brand-logo {
  flex: none;
  display: block;
}

.logo-ring {
  fill: var(--logo-ring);
}

.logo-bit {
  fill: var(--logo-bit);
}

.brand-name {
  font-family: var(--mono);
  font-weight: 800;
  font-size: 32px;
  line-height: 1;
  letter-spacing: 0.16em;
  color: var(--head-ink);
}

.controls,
.settings {
  display: flex;
  flex-wrap: wrap;
  align-items: center;
  gap: 8px;
}

/* Shows the file last opened/saved via the File System Access API (see app.js), so Save/Save As
   behave like a desktop app's title bar. Hidden entirely below - there's no room for it on
   narrow/mobile layouts, which also don't have the File System Access API to make Save meaningful
   there anyway. */
.file-status {
  display: flex;
  align-items: center;
  gap: 6px;
  color: var(--muted);
  font-family: var(--mono);
  font-size: 13px;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  max-width: 22ch;
}

.file-status-icon {
  width: 15px;
  height: 15px;
  flex: none;
  fill: none;
  stroke: currentColor;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.file-status-name {
  overflow: hidden;
  text-overflow: ellipsis;
}

.file-status.is-unsaved {
  color: var(--head-ink);
}

.settings {
  margin-left: auto;
  gap: 8px 14px;
}

.btn,
.mini-btn,
.keypad button {
  cursor: pointer;
  border: 1px solid var(--btn-edge);
  border-radius: 6px;
  background: var(--btn-bg);
  color: var(--btn-ink);
  -webkit-tap-highlight-color: transparent;
}

.btn {
  min-height: 38px;
  padding: 0 14px;
  font-weight: 700;
  letter-spacing: 0.02em;
}

.btn:hover:not(:disabled),
.mini-btn:hover,
.keypad button:hover {
  background: var(--btn-hover);
}

.btn:active:not(:disabled),
.mini-btn:active,
.keypad button:active {
  transform: translateY(1px);
}

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

.btn-small {
  min-height: 34px;
  padding: 0 12px;
  font-size: 13px;
}

.btn[aria-pressed="true"],
.btn-primary.is-running {
  background: var(--pause-bg);
  border-color: var(--pause-edge);
  color: var(--run-ink);
}

.btn-primary {
  background: var(--run-bg);
  border-color: var(--run-edge);
  color: var(--run-ink);
  min-width: 96px;
}

.btn-primary:hover:not(:disabled) {
  background: var(--run-bg);
  filter: brightness(1.15);
}

.btn-primary.is-running:hover:not(:disabled) {
  background: var(--pause-bg);
}

.btn.symbol-btn {
  min-width: 44px;
  width: 44px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.symbol-btn svg {
  width: 22px;
  height: 22px;
  display: block;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.symbol-btn svg.solid-icon,
.symbol-btn svg .solid-icon-shape {
  fill: currentColor;
  stroke: none;
}

.mini-btn {
  min-height: 24px;
  padding: 0 8px;
  font-size: 12px;
  border-radius: 5px;
}

.mini-btn.symbol-btn {
  width: 26px;
  min-width: 26px;
  padding: 0;
  display: inline-flex;
  align-items: center;
  justify-content: center;
}

.mini-btn.symbol-btn svg {
  width: 15px;
  height: 15px;
  stroke-width: 1.8;
}

.clock-lock-icon {
  width: 13px;
  height: 13px;
  flex: none;
  fill: none;
  stroke: var(--amber);
  stroke-width: 2.2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

.clock-lock-icon.is-hidden {
  display: none;
}

.setting {
  display: flex;
  align-items: center;
  gap: 6px;
  font-size: 13px;
}

.setting-label {
  color: var(--head-ink);
  font-weight: 700;
  font-size: 12px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
}

/* ---------- hamburger menu ---------- */

.hamburger-btn {
  flex: none;
  display: flex;
  flex-direction: column;
  justify-content: center;
  gap: 5px;
  width: 44px;
  height: 44px;
  padding: 0 11px;
  cursor: pointer;
  background: var(--btn-bg);
  border: 1px solid var(--btn-edge);
  border-radius: 6px;
  -webkit-tap-highlight-color: transparent;
}

.hamburger-btn:hover {
  background: var(--btn-hover);
}

.hamburger-btn span {
  display: block;
  height: 3px;
  border-radius: 2px;
  background: var(--btn-ink);
}

.hamburger-btn:focus-visible,
.menu-concertina-trigger:focus-visible,
.menu-item-btn:focus-visible,
.menu-choices button:focus-visible {
  outline: 3px solid var(--ink);
  outline-offset: 2px;
}

.app-menu {
  position: fixed;
  top: 0;
  right: -340px;
  bottom: 0;
  z-index: 30;
  display: flex;
  flex-direction: column;
  width: min(320px, 88vw);
  overflow-y: auto;
  background: var(--panel);
  border-left: 2px solid var(--panel-edge);
  box-shadow: -8px 0 24px rgba(0, 0, 0, 0.45);
  visibility: hidden;
  transition: right 220ms ease, visibility 0ms 220ms;
}

.app-menu.is-open {
  right: 0;
  visibility: visible;
  transition: right 220ms ease, visibility 0ms 0ms;
}

.menu-header {
  flex: none;
  display: flex;
  align-items: center;
  justify-content: space-between;
  padding: 10px 14px;
  border-bottom: 1px solid var(--panel-edge-dim);
}

.menu-title {
  font-family: var(--mono);
  font-weight: 800;
  font-size: 20px;
  letter-spacing: 0.2em;
  color: var(--gold);
}

.menu-close-btn {
  display: inline-grid;
  place-items: center;
  width: 34px;
  min-width: 34px;
  height: 34px;
  min-height: 34px;
  padding: 0;
  cursor: pointer;
  border: 1px solid #8f1c1c;
  border-radius: 10px;
  background: linear-gradient(180deg, #f15454, #b32020);
  color: #ffffff;
}

.menu-close-btn:hover:not(:disabled) {
  background: linear-gradient(180deg, #ff6666, #c32727);
  border-color: #a52323;
  box-shadow: 0 5px 12px rgba(207, 42, 42, 0.45);
  transform: translateY(-1px);
}

.menu-close-btn:focus-visible {
  outline: 2px solid #fff3a6;
  outline-offset: 1px;
}

.menu-close-btn svg {
  width: 16px;
  height: 16px;
  display: block;
  fill: none;
  stroke: currentColor;
  stroke-width: 2.4;
  stroke-linecap: round;
}

/* Modal close-X buttons reuse .menu-close-btn but are smaller and flat red, matching the
   rule-del-btn style used for the same purpose in syntaxor and chewring. */
.menu-close-btn.modal-close-x {
  width: 28px;
  min-width: 28px;
  height: 28px;
  min-height: 28px;
  border-radius: 8px;
  background: #cf2a2a;
}

.menu-close-btn.modal-close-x:hover:not(:disabled) {
  background: #e13c3c;
}

.menu-inner {
  display: flex;
  flex-direction: column;
  gap: 4px;
  padding: 12px 14px 24px;
}

.menu-concertina-trigger,
.menu-item-btn {
  display: flex;
  align-items: center;
  justify-content: space-between;
  width: 100%;
  min-height: 44px;
  padding: 8px 12px;
  background: none;
  border: 0;
  border-radius: 6px;
  color: var(--ink);
  font-size: 16px;
  text-align: left;
  cursor: pointer;
}

.menu-concertina-trigger {
  font-weight: 700;
  border: 1px solid var(--panel-edge-dim);
}

.menu-current {
  color: var(--gold);
}

.menu-concertina-trigger:hover,
.menu-item-btn:hover {
  background: var(--btn-hover);
}

.concertina-arrow {
  transition: transform 160ms ease;
}

.menu-concertina-trigger.is-open .concertina-arrow {
  transform: rotate(90deg);
}

.menu-concertina-list {
  margin: 0 0 8px;
  padding: 0 0 0 6px;
  list-style: none;
}

.menu-concertina-list[hidden] {
  display: none;
}

.menu-choices {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 6px;
  padding: 0 0 0 6px;
}

.menu-choices-list {
  grid-template-columns: 1fr;
}

.menu-choices button {
  min-height: 44px;
  padding: 0 10px;
  cursor: pointer;
  background: var(--btn-bg);
  border: 1px solid var(--btn-edge);
  border-radius: 6px;
  color: var(--btn-ink);
  font-size: 15px;
  font-weight: 700;
  text-align: left;
}

.menu-choices button:hover {
  background: var(--btn-hover);
}

/* The selected choice is drawn inverted so it is obvious in every theme, with or without colour. */
.menu-choices button.is-selected {
  background: var(--ink);
  border-color: var(--ink);
  color: var(--well);
}

.menu-choices button.is-selected::after {
  content: " \2713";
}

.menu-about {
  margin-top: 10px;
  border-top: 1px solid var(--panel-edge-dim);
  border-radius: 0 0 6px 6px;
}

/* ---------- welcome / about modal ---------- */

.modal-backdrop {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: flex;
  align-items: center;
  justify-content: center;
  padding: 16px;
  background: rgba(0, 0, 0, 0.72);
}

.modal-backdrop[hidden] {
  display: none;
}

.modal {
  position: relative;
  width: min(480px, 100%);
  max-height: 100%;
  overflow-y: auto;
  padding: 26px 26px 20px;
  background: var(--panel);
  color: var(--ink);
  border: 2px solid var(--panel-edge);
  border-radius: 10px;
  box-shadow: 0 12px 40px rgba(0, 0, 0, 0.6);
  text-align: center;
}

/* The dialog container itself is only ever focused programmatically (as a neutral landing spot
   when a modal opens with no prior user gesture), never via Tab, so it shows no ring of its own. */
.modal:focus {
  outline: none;
}

.modal-close-x {
  position: absolute;
  top: 8px;
  right: 10px;
}

.modal-head {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 14px;
  margin: 0 36px 16px;
}

.modal-head h2 {
  margin: 0;
  font-family: var(--mono);
  font-size: 22px;
  letter-spacing: 0.04em;
  color: var(--gold);
}

.welcome-logo {
  flex: none;
}

.welcome-logo .logo-ring {
  fill: var(--ink);
}

.modal-lead {
  margin: 0 0 14px;
  font-size: 16px;
  line-height: 1.45;
}

.modal-points {
  margin: 0 0 14px;
  padding: 0;
  list-style: none;
  text-align: left;
  font-size: 15px;
  line-height: 1.4;
}

.modal-points li {
  margin: 0 0 8px;
  padding: 8px 12px;
  border: 1px solid var(--panel-edge-dim);
  border-radius: 6px;
}

.modal-points b {
  color: var(--gold);
}

.modal-points code {
  font-family: var(--mono);
  color: var(--syn-keyword);
}

.menu-glyph {
  display: inline-flex;
  flex-direction: column;
  gap: 2px;
  width: 16px;
  margin: 0 3px;
  vertical-align: middle;
}

.menu-glyph span {
  display: block;
  height: 2px;
  background: var(--ink);
  border-radius: 1px;
}

.modal-version {
  margin: 0 0 14px;
  font-size: 13px;
  color: var(--muted);
}

.modal-start {
  min-width: 160px;
  min-height: 44px;
  font-size: 16px;
}

.modal-check {
  display: flex;
  align-items: center;
  justify-content: center;
  gap: 8px;
  margin: 16px 0 14px;
  font-size: 15px;
  cursor: pointer;
}

.modal-check input {
  width: 18px;
  height: 18px;
  cursor: pointer;
  accent-color: var(--amber);
}

.modal-credit {
  margin: 0;
  font-size: 13px;
  color: var(--muted);
}

.modal-credit a {
  color: var(--gold);
  font-weight: 700;
  text-decoration: none;
}

.modal-credit a:hover {
  text-decoration: underline;
}

.modal-start:focus-visible,
.modal-check input:focus-visible,
.modal-credit a:focus-visible {
  outline: 3px solid var(--ink);
  outline-offset: 2px;
}

/* ---------- help modal ---------- */

.help-modal {
  display: flex;
  flex-direction: column;
  width: min(1100px, 100%);
  height: min(900px, 100%);
  overflow: hidden;
  text-align: left;
}

.help-title {
  flex: none;
  margin: 0 48px 12px 0;
  font-family: var(--mono);
  font-size: 20px;
  letter-spacing: 0.04em;
  color: var(--gold);
}

.help-tabs {
  flex: none;
  display: flex;
  flex-wrap: wrap;
  gap: 6px;
  margin-bottom: 12px;
}

.help-tabs button {
  min-height: 38px;
  padding: 0 12px;
  cursor: pointer;
  background: var(--btn-bg);
  border: 1px solid var(--btn-edge);
  border-radius: 6px;
  color: var(--btn-ink);
  font-size: 14px;
  font-weight: 700;
}

.help-tabs button:hover {
  background: var(--btn-hover);
}

/* The selected tab is drawn inverted so it is obvious in every theme, with or without colour. */
.help-tabs button[aria-selected="true"] {
  background: var(--ink);
  border-color: var(--ink);
  color: var(--well);
}

.help-tabs button:focus-visible {
  outline: 3px solid var(--ink);
  outline-offset: 2px;
}

.help-panels {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding-right: 6px;
  font-size: 15px;
  line-height: 1.5;
}

.help-panel[hidden] {
  display: none;
}

.help-panel > :first-child {
  margin-top: 0;
}

.help-panel h3 {
  margin: 20px 0 8px;
  font-size: 16px;
  color: var(--gold);
}

.help-panel h3:first-child {
  margin-top: 0;
}

.help-panel p {
  margin: 0 0 10px;
}

.help-panel code,
.help-code {
  font-family: var(--mono);
}

.help-panel code {
  padding: 0;
  font-size: 0.93em;
  color: var(--syn-keyword);
  white-space: nowrap;
}

.help-panel .help-flag {
  font-family: var(--mono);
  font-weight: 700;
  color: var(--green);
}

.help-scroll {
  margin: 0 0 12px;
  overflow-x: auto;
}

.help-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 14px;
}

.help-table th,
.help-table td {
  padding: 6px 10px;
  border: 1px solid var(--panel-edge-dim);
  vertical-align: top;
}

.help-table th {
  text-align: left;
  color: var(--gold);
  background: var(--well-2);
}

.help-ops th:nth-child(n + 3),
.help-ops td:nth-child(n + 3) {
  text-align: center;
}

.help-list {
  margin: 0 0 12px;
  padding-left: 20px;
}

.help-list li {
  margin-bottom: 6px;
}

.help-list b,
.help-panel li b {
  color: var(--gold);
}

.help-code {
  margin: 0 0 12px;
  padding: 10px 12px;
  overflow-x: auto;
  font-size: 13px;
  line-height: 1.45;
  background: var(--well);
  color: var(--ink);
  border: 1px solid var(--panel-edge-dim);
  border-radius: 6px;
  white-space: pre;
}

.field-diagram {
  display: grid;
  grid-template-columns: 5fr 3fr 4fr 4fr 17fr;
  gap: 3px;
  margin: 0 0 12px;
  font-size: 13px;
  text-align: center;
}

.field-diagram .field {
  display: flex;
  flex-direction: column;
  justify-content: center;
  min-width: 0;
  padding: 6px 2px;
  background: var(--well);
  border: 2px solid currentColor;
  border-radius: 4px;
  line-height: 1.25;
}

.field-diagram .field b {
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.field-diagram .field span {
  color: var(--ink);
  font-size: 11px;
}

.field-diagram .field i {
  color: var(--muted);
  font-size: 10px;
  font-style: normal;
}

/* ---------- challenges modal ---------- */

.challenges-modal {
  display: flex;
  flex-direction: column;
  width: min(640px, 100%);
  height: min(780px, 100%);
  overflow: hidden;
  text-align: left;
}

.challenges-lead {
  flex: none;
  margin: 0 48px 12px 0;
  font-size: 14px;
  color: var(--muted);
}

.challenges-actions {
  flex: none;
  display: flex;
  align-items: center;
  gap: 8px;
  margin-bottom: 12px;
}

.challenges-position {
  margin-right: auto;
  font-family: var(--mono);
  font-size: 14px;
  color: var(--muted);
}

.challenges-list {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  padding-right: 6px;
}

.challenge-card-title {
  margin: 0 0 10px;
  font-size: 17px;
  color: var(--gold);
}

.challenge-card-description {
  margin: 0 0 14px;
  font-size: 15px;
  line-height: 1.5;
}

.challenge-tests-label {
  margin: 0 0 6px;
  font-size: 13px;
  font-weight: 700;
  color: var(--muted);
}

.challenge-tests-list {
  margin: 0;
  padding: 0;
  list-style: none;
  font-family: var(--mono);
  font-size: 13px;
}

.challenge-test-item {
  display: flex;
  flex-wrap: wrap;
  gap: 4px 12px;
  margin-bottom: 6px;
  padding: 6px 10px;
  background: var(--well);
  border: 1px solid var(--panel-edge-dim);
  border-radius: 6px;
}

.challenge-test-output {
  color: var(--syn-keyword);
}

.tasks-modal-shortcut {
  flex: none;
  display: block;
  margin: 8px 0 0;
  text-align: right;
  color: var(--muted);
  font-size: 13px;
  line-height: 1.2;
}

@media (max-width: 480px), (max-height: 700px) {
  .modal {
    padding: 16px 16px 14px;
  }

  .modal-head {
    margin-bottom: 10px;
  }

  .modal-head h2 {
    font-size: 19px;
  }

  .modal-lead {
    margin-bottom: 10px;
    font-size: 15px;
  }

  .modal-points {
    margin-bottom: 8px;
    font-size: 14px;
  }

  .modal-points li {
    margin-bottom: 6px;
    padding: 6px 10px;
  }

  .modal-version {
    margin-bottom: 8px;
  }

  .modal-check {
    margin: 10px 0 8px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .app-menu,
  .app-menu.is-open,
  .concertina-arrow {
    transition: none;
  }
}

.setting select {
  flex: none;
  font-family: var(--mono);
  font-weight: 700;
  font-variant-numeric: tabular-nums;
  white-space: nowrap;
  color: var(--amber);
  background: var(--well);
  border: 1px solid var(--panel-edge);
  border-radius: 5px;
  padding: 4px 6px;
  cursor: pointer;
}

/* ---------- tabs and layout ---------- */

.tabs {
  display: none;
  background: var(--well-2);
  border-bottom: 1px solid var(--panel-edge-dim);
}

.tab {
  flex: 1;
  min-height: 42px;
  border: 0;
  border-bottom: 3px solid transparent;
  background: transparent;
  color: var(--muted);
  font-weight: 700;
  cursor: pointer;
}

.tab.is-active {
  color: var(--ink);
  border-bottom-color: var(--amber);
  background: var(--tab-active-bg);
}

.layout {
  flex: 1;
  min-height: 0;
  display: grid;
  grid-template-columns: minmax(250px, 1.05fr) minmax(700px, 2.25fr) minmax(230px, 0.8fr);
  gap: 10px;
  padding: 10px;
  padding-top: 0; /* .toolbar's own bottom margin already supplies this gap; avoid doubling it up */
}

.pane {
  min-height: 0;
  min-width: 0;
  display: flex;
  flex-direction: column;
  gap: 8px;
  overflow-y: auto;
  overscroll-behavior: contain;
  background: var(--panel);
  border: 1px solid var(--pane-edge);
  border-radius: 10px;
  padding: 10px;
}

.pane-code {
  overflow: hidden;
  border: 2px solid var(--panel-edge);
}

.pane-code > .pane-head:first-child {
  border-radius: 8px 8px 0 0;
}

.pane-io {
  border: 2px solid var(--panel-edge);
}

.pane-io > .pane-head:first-child {
  border-radius: 8px 8px 0 0;
}

.pane-machine {
  container-type: inline-size;
}

.pane-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  flex: none;
  margin: 0 -10px;
  padding: 8px 10px;
  background: linear-gradient(180deg, var(--panel-head-top), var(--panel-edge-dim));
  border-bottom: 1px solid var(--panel-edge);
}

.pane > .pane-head:first-child {
  margin-top: -10px;
  border-radius: 9px 9px 0 0;
}

.pane-head.sub-head {
  margin: 0;
  padding: 4px 0;
  background: none;
  border-bottom: none;
}

.pane-title {
  font-family: var(--mono);
  font-weight: 800;
  font-size: 13px;
  letter-spacing: 0.3em;
  text-transform: uppercase;
  color: var(--head-ink);
}

.pane-hint {
  font-size: 12px;
  color: var(--muted);
}

.pane-tools {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

/* ---------- editor ---------- */

.editor-wrap {
  flex: 1;
  min-height: 160px;
  border: 1px solid var(--panel-edge);
  border-radius: 8px;
  overflow: hidden;
  background: var(--well);
}

.editor-wrap .cm-editor {
  height: 100%;
  background: var(--well);
  color: var(--ink);
  font-family: var(--mono);
  font-size: 14px;
}

.editor-wrap .cm-editor.cm-focused {
  outline: none;
}

.editor-wrap .cm-scroller {
  font-family: inherit;
  line-height: 1.55;
  overflow: auto;
}

.editor-wrap .cm-content {
  caret-color: var(--ink);
  padding: 6px 0;
}

.editor-wrap .cm-line {
  padding: 0 8px;
}

.editor-wrap .cm-gutters {
  background: var(--well);
  color: var(--gutter-ink);
  border-right: 1px solid var(--panel-edge-dim);
}

.editor-wrap .cm-asm-fold-gutter .cm-gutterElement {
  padding: 0 2px;
}

.editor-wrap .cm-asm-fold-button {
  display: block;
  width: 14px;
  padding: 0;
  border: 0;
  background: transparent;
  color: var(--gutter-ink);
  font: inherit;
  line-height: inherit;
  cursor: pointer;
}

.editor-wrap .cm-asm-fold-button:hover {
  color: var(--ink);
}

.editor-wrap .cm-asm-fold-button:focus-visible {
  outline: 2px solid var(--ink);
  outline-offset: -2px;
  border-radius: 3px;
}

.editor-wrap .cm-asm-fold-placeholder {
  padding: 0 3px;
  border: 1px solid var(--panel-edge);
  border-radius: 3px;
  color: var(--gutter-ink);
}

.editor-wrap .cm-lineNumbers .cm-gutterElement {
  padding: 0 8px 0 10px;
  min-width: 30px;
}

.editor-wrap .cm-activeLine {
  background: var(--active-line);
}

.editor-wrap .cm-cursor,
.editor-wrap .cm-dropCursor {
  border-left: 2px solid var(--ink);
}

.editor-wrap .cm-selectionBackground,
.editor-wrap .cm-editor.cm-focused > .cm-scroller > .cm-selectionLayer .cm-selectionBackground {
  background: var(--selection);
}

.cm-asm-marks {
  width: 16px;
}

.cm-asm-error {
  background: var(--error-bg);
  box-shadow: inset 3px 0 var(--error-edge);
}

.cm-asm-exec {
  background: var(--exec-bg);
  box-shadow: inset 3px 0 var(--exec-edge);
}

.cm-asm-error-mark {
  color: var(--red);
  font-weight: 800;
}

.cm-asm-exec-mark {
  color: var(--green);
  font-size: 10px;
}

.messages {
  list-style: none;
  margin: 0;
  padding: 0;
  max-height: 150px;
  overflow-y: auto;
  flex: none;
  font-size: 13px;
}

.messages:empty {
  display: none;
}

.messages li {
  padding: 4px 2px;
  margin-top: 2px;
}

.messages li[tabindex] {
  cursor: pointer;
}

.messages li[tabindex]:hover {
  text-decoration: underline;
}

.msg-error {
  color: var(--msg-error-ink);
}

.msg-warn {
  color: var(--msg-warn-ink);
}

.msg-ok {
  color: var(--msg-ok-ink);
}

/* ---------- cycle strip ---------- */

.cycle-strip {
  display: grid;
  grid-template-columns: minmax(0, 1.5fr) minmax(120px, 1fr) auto;
  align-items: center;
  gap: 10px;
  flex: none;
  padding: 6px 8px;
  background: var(--well);
  border: 0;
  border-radius: 8px;
}

.phase-box {
  display: flex;
  align-items: center;
  gap: 8px;
  min-width: 0;
}

.phase-label {
  flex: none;
  min-width: 82px;
  text-align: center;
  padding: 3px 8px;
  border-radius: 4px;
  font-family: var(--mono);
  font-size: 12px;
  font-weight: 800;
  letter-spacing: 0.1em;
  color: var(--phase-ink);
  background: var(--phase-idle);
}

.phase-label[data-kind="fetch"] {
  background: var(--phase-fetch);
}

.phase-label[data-kind="decode"] {
  background: var(--phase-decode);
}

.phase-label[data-kind="execute"] {
  background: var(--phase-execute);
}

.phase-label[data-kind="error"],
.phase-label[data-kind="halted"] {
  background: var(--phase-stop);
  color: var(--phase-stop-ink);
}

.phase-label[data-kind="input"] {
  background: var(--phase-input);
}

.phase-text {
  font-family: var(--mono);
  font-size: 13px;
  color: var(--ink);
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

.clock-trace {
  width: 100%;
  height: 30px;
}

.clock-trace.is-hidden {
  visibility: hidden;
}

.counters {
  display: flex;
  gap: 12px;
  font-family: var(--mono);
  font-size: 12px;
  color: var(--muted);
  white-space: nowrap;
}

.counters b {
  color: var(--amber);
}

/* ---------- machine diagram ---------- */

.machine {
  --scale: 1;
  position: relative;
  display: grid;
  grid-template-columns: minmax(0, 1fr) 5.8em 23em;
  grid-template-rows: 1fr auto;
  align-items: stretch;
  flex: 1;
  min-height: calc(520px * var(--scale));
  font-size: calc(13px * var(--scale));
  margin: -10px 0 0 -10px;
}

.machine.base-bin {
  grid-template-columns: minmax(0, 1fr) 5.8em 27em;
  min-height: calc(660px * var(--scale));
}

.cpu {
  grid-column: 1;
  grid-row: 1 / span 2;
  display: flex;
  flex-direction: column;
  gap: 0.8em;
  padding: 0.8em;
  background: var(--well);
  border: 2px solid var(--panel-edge);
  border-radius: 10px;
}

.cpu-title {
  display: flex;
  align-items: center;
  justify-content: space-between;
  margin: calc(-0.8 * 13px * var(--scale)) calc(-0.8 * 13px * var(--scale)) 0;
  padding: calc(0.5 * 13px * var(--scale)) calc(0.8 * 13px * var(--scale));
  background: linear-gradient(180deg, var(--panel-head-top), var(--panel-edge-dim));
  border-bottom: 1px solid var(--panel-edge);
  border-radius: 8px 8px 0 0;
  font-family: var(--mono);
  font-weight: 800;
  letter-spacing: 0.3em;
  color: var(--head-ink);
  font-size: 13px;
}

.cpu-title .legend {
  display: inline-flex;
  align-items: center;
  gap: 0.5em;
  font-weight: 400;
  letter-spacing: 0.02em;
  font-size: 0.85em;
  color: var(--faint);
}

.lg {
  display: inline-block;
  width: 1.7em;
  height: 3px;
  background: var(--wire);
}

.cpu-cols {
  display: grid;
  grid-template-columns: minmax(0, 1fr) 2.3em minmax(0, 1fr);
  gap: 0;
  flex: 1;
}

.col {
  display: flex;
  flex-direction: column;
  justify-content: space-between;
  gap: 0.7em;
  min-width: 0;
}

.spine {
  position: relative;
}

.node,
.regfile,
.io-ctrl {
  background: var(--panel-2);
  border: 1px solid var(--panel-edge);
  border-radius: 0.6em;
  padding: 0.4em 0.6em 0.5em;
  transition: border-color 0.12s, box-shadow 0.12s;
}

.node-pc {
  position: relative;
}

.node-head {
  display: flex;
  flex-wrap: wrap;
  align-items: baseline;
  gap: 0 0.55em;
  min-width: 0;
}

.node-name {
  font-family: var(--mono);
  font-weight: 800;
  font-size: 1.08em;
  color: var(--gold);
}

.node-sub {
  font-size: 0.8em;
  color: var(--faint);
}

.node-val {
  font-family: var(--mono);
  font-size: 1.08em;
  color: var(--value);
  min-height: 1.4em;
  white-space: nowrap;
  overflow: hidden;
  text-overflow: clip;
}

.base-bin .node-val,
.base-bin .reg-val {
  font-size: 0.88em;
}

.node-inc {
  position: absolute;
  right: 0.6em;
  bottom: 0.5em;
}

.inc-label {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  padding: 0.12em 0.45em;
  border: 1px solid var(--panel-edge-dim);
  border-radius: 0.35em;
  font-family: var(--mono);
  font-weight: 800;
  font-size: 0.78em;
  color: var(--faint);
  background: var(--well-2);
  transition: color 0.12s, border-color 0.12s, background 0.12s;
}

/* PC's dedicated +1 incrementer lights up only for the self-increment transfer (PC <- PC + 1),
   never for a branch/MOV writing a different value into PC - see isIncrement() in cpuview.js. */
.inc-label.active {
  color: var(--flag-on-ink);
  background: var(--flag-on-bg);
  border-color: var(--flag-on-bg);
}

.base-bin .node-val {
  white-space: normal;
  line-height: 1.2;
}

.fields {
  font-family: var(--mono);
  font-size: 0.8em;
  display: flex;
  flex-wrap: wrap;
  gap: 0 0.4em;
  min-height: 1.2em;
}

.field-legend {
  display: flex;
  flex-wrap: wrap;
  gap: 0 0.6em;
  font-size: 0.7em;
  text-transform: uppercase;
  letter-spacing: 0.04em;
  opacity: 0.9;
}

.f-op {
  color: var(--f-op);
}

.f-mode {
  color: var(--f-mode);
}

.f-rd {
  color: var(--f-rd);
}

.f-rn {
  color: var(--f-rn);
}

.f-op2 {
  color: var(--f-op2);
}

.flags {
  display: grid;
  grid-template-columns: repeat(auto-fit, minmax(2.3em, 1fr));
  gap: 0.35em;
  margin-top: 0.25em;
}

.flag {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: 0.3em;
  min-width: 0;
  padding: 0.15em 0.2em;
  border: 1px solid var(--panel-edge-dim);
  border-radius: 0.3em;
  font-family: var(--mono);
  font-weight: 700;
  font-size: 1em;
  color: var(--faint);
  background: var(--well-2);
}

.flag b {
  font-weight: 800;
}

.flag.on {
  color: var(--flag-on-ink);
  background: var(--flag-on-bg);
  border-color: var(--flag-on-bg);
}

.cu-decode {
  font-family: var(--mono);
  font-weight: 800;
  font-size: 1.25em;
  color: var(--green);
  min-height: 1.3em;
}

.cu-mode {
  font-size: 0.85em;
  color: var(--muted);
  min-height: 1.2em;
}

.cu-signal {
  font-family: var(--mono);
  font-size: 0.85em;
  font-weight: 800;
  color: var(--bus-control);
  min-height: 1.2em;
}

.regfile {
  flex: none;
  display: flex;
  flex-direction: column;
  gap: 0.45em;
}

.regfile-title {
  font-size: 0.85em;
  text-transform: uppercase;
  letter-spacing: 0.1em;
  color: var(--faint);
}

.reg-grid {
  --reg-min: 9.8em;
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(var(--reg-min), 1fr));
  gap: 0.4em;
}

.base-bin .reg-grid {
  --reg-min: 20.5em;
}

.reg {
  display: flex;
  align-items: baseline;
  justify-content: space-between;
  gap: 0.45em;
  padding: 0.25em 0.5em;
  background: var(--well-2);
  border: 1px solid var(--panel-edge-dim);
  border-radius: 0.4em;
  font-family: var(--mono);
  transition: border-color 0.12s, background 0.12s;
}

.reg-name {
  font-weight: 800;
  font-size: 0.92em;
  color: var(--gold);
}

.reg-val {
  font-size: 1em;
  color: var(--value);
  white-space: nowrap;
}

.node.active,
.regfile.active,
.reg.active,
.mem-panel.active,
.io-ctrl.active {
  border-color: var(--ink);
  box-shadow: 0 0 0 1px var(--ink);
}

.reg.active {
  background: var(--reg-active-bg);
}

.flash {
  animation: flash 0.45s ease-out;
}

@keyframes flash {
  0% {
    background-color: var(--flash-bg);
  }
}

.lanes {
  grid-column: 2;
  grid-row: 1 / span 2;
}

.mem-panel {
  grid-column: 3;
  grid-row: 1;
  display: flex;
  flex-direction: column;
  min-height: 0;
  background: var(--well);
  border: 2px solid var(--panel-edge);
  border-radius: 10px;
  overflow: hidden;
}

.mem-head {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 8px;
  padding: 8px 10px;
  background: linear-gradient(180deg, var(--panel-head-top), var(--panel-edge-dim));
  border-bottom: 1px solid var(--panel-edge);
  border-radius: 8px 8px 0 0;
}

.mem-title {
  font-family: var(--mono);
  font-weight: 800;
  font-size: 13px;
  letter-spacing: 0.3em;
  text-transform: uppercase;
  color: var(--head-ink);
  white-space: nowrap;
}

.mem-title small {
  font-weight: 400;
  color: var(--faint);
  letter-spacing: 0;
}

.mem-base {
  display: flex;
  flex-wrap: nowrap;
  gap: 0.25em;
  min-width: 0;
}

.mem-base button {
  /* Fixed px, not em: .mem-panel inherits the diagram's scaled font-size (up to 2x via
     --scale on wide screens), and these are real controls, not diagram labels, so they
     should stay the same size as the rest of the UI chrome (compare .mini-btn) instead of
     ballooning past the header buttons as the CPU diagram zooms in. */
  min-height: 24px;
  padding: 0 8px;
  font-size: 12px;
  font-weight: 700;
  white-space: nowrap;
  cursor: pointer;
  background: var(--btn-bg);
  border: 1px solid var(--btn-edge);
  border-radius: 5px;
  color: var(--btn-ink);
  -webkit-tap-highlight-color: transparent;
}

.mem-base button:hover {
  background: var(--btn-hover);
}

/* The selected format is drawn inverted so it is obvious in every theme, with or without colour. */
.mem-base button.is-selected {
  background: var(--ink);
  border-color: var(--ink);
  color: var(--well);
}

.mem-base button:focus-visible {
  outline: 2px solid var(--ink);
  outline-offset: 1px;
}

.mem-scroll {
  flex: 1;
  min-height: 0;
  overflow-y: auto;
  position: relative;
  outline: none;
}

.mem-space {
  position: relative;
  width: 100%;
}

.mem-rows {
  position: absolute;
  inset: 0;
}

.mem-row {
  position: absolute;
  left: 0;
  right: 0;
  display: flex;
  align-items: center;
  gap: 0.6em;
  padding: 0 0.6em;
  font-family: var(--mono);
  /* .mem-row inherits the diagram's scaled font-size (up to 2x via --scale on wide
     screens), which otherwise makes the memory list noticeably bigger than the fixed
     14px program editor text. Capping with min() keeps it proportional to the rest of
     the CPU diagram while never growing far past the editor's size. */
  font-size: min(0.92em, 15px);
  border-bottom: 1px solid rgba(128, 128, 128, 0.18);
  white-space: nowrap;
}

.base-bin .mem-row {
  flex-wrap: wrap;
  align-content: center;
  gap: 0 0.6em;
  font-size: min(0.85em, 14px);
}

.base-bin .mem-note {
  flex-basis: 100%;
  padding-left: 0;
}

.mem-addr {
  flex: none;
  min-width: 4ch;
  color: var(--faint);
}

.machine[data-base="hex"] .mem-addr {
  min-width: 6ch;
}

.base-bin .mem-addr {
  min-width: 13ch;
}

.mem-val {
  flex: none;
  width: 10ch;
  color: var(--value);
}

/* Signed decimal can be 11 characters wide (a minus sign plus 10 digits). */
.machine[data-base="dec"] .mem-val {
  width: 11ch;
  text-align: right;
}

.machine[data-base="udec"] .mem-val {
  text-align: right;
}

.base-bin .mem-val {
  width: auto;
}

.mem-note {
  flex: 0 1 auto;
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  color: var(--green);
}

.mem-label {
  flex: 0 8 auto;
  min-width: 0;
  margin-left: auto;
  padding: 0 0.4em;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
  border-radius: 3px;
  background: var(--chip-bg);
  color: var(--chip-ink);
  font-size: 0.85em;
}

.mem-note .mem-label {
  display: inline-block;
  margin: 0 0.5em 0 0;
}

.mem-row.is-data .mem-note {
  color: var(--cyan);
}

.mem-row.is-video {
  background: rgba(255, 79, 216, 0.06);
}

.mem-row.is-video .mem-note {
  color: var(--muted);
}

.mem-row.is-stack {
  background: color-mix(in srgb, var(--green) 6%, transparent);
}

.mem-row.is-stack-live {
  background: color-mix(in srgb, var(--green) 18%, transparent);
}

.mem-row.is-sp {
  box-shadow: inset 0 2px 0 var(--green);
}

.mem-row.is-sp-empty {
  box-shadow: inset 0 -2px 0 var(--green);
}

.sp-chip {
  display: inline-block;
  padding: 0 0.4em;
  border: 1px solid var(--green);
  border-radius: 3px;
  color: var(--green);
  font-size: 0.85em;
  font-weight: 700;
}

.reg-sp .reg-name {
  color: var(--green);
}

.mem-row.is-pc {
  outline: 2px solid var(--amber);
  outline-offset: -2px;
}

.mem-row.is-mar {
  outline: 2px solid var(--cyan);
  outline-offset: -2px;
}

.swatch {
  display: inline-block;
  width: 0.9em;
  height: 0.9em;
  margin-right: 0.3em;
  vertical-align: -0.1em;
  border: 1px solid var(--swatch-edge);
}

.io-ctrl {
  grid-column: 3;
  grid-row: 2;
  margin-top: 1.2em;
  display: flex;
  align-items: baseline;
  gap: 0.6em;
  padding: 0.6em 0.8em;
  border-width: 2px;
  border-radius: 0.8em;
  background: var(--well);
}

.bus-layer {
  position: absolute;
  inset: 0;
  pointer-events: none;
  overflow: visible;
}

.wire {
  stroke: var(--wire);
  stroke-width: calc(3px * var(--scale));
}

.bus-line {
  fill: none;
  stroke-width: calc(6px * var(--scale));
  stroke-linejoin: round;
  opacity: 0.85;
  transition: opacity 0.12s, stroke-width 0.12s;
}

.bus-line.active {
  opacity: 1;
  stroke-width: calc(8px * var(--scale));
  filter: drop-shadow(0 0 5px currentColor);
}

.bus-address {
  stroke: var(--bus-address);
  color: var(--bus-address);
}

.bus-data {
  stroke: var(--bus-data);
  color: var(--bus-data);
}

.bus-control {
  stroke: var(--bus-control);
  color: var(--bus-control);
}

.bus-label {
  font-family: var(--mono);
  font-size: calc(9px * var(--scale));
  font-weight: 800;
  letter-spacing: 0.02em;
  fill: var(--faint);
}

.bus-label-address {
  fill: var(--bus-address);
}

.bus-label-data {
  fill: var(--bus-data);
}

.bus-label-control {
  fill: var(--bus-control);
}

.dot-core {
  fill: #fff;
  stroke: #000;
  stroke-width: 1.5;
}

.dot-badge {
  fill: #000;
  stroke: #fff;
  stroke-width: 1;
}

.dot-text {
  font-family: var(--mono);
  font-size: calc(10.5px * var(--scale));
  font-weight: 700;
  fill: #fff;
}

.dot-address .dot-core,
.dot-address .dot-badge {
  fill: var(--bus-address);
}

.dot-address .dot-text {
  fill: #000;
}

.dot-data .dot-core,
.dot-data .dot-badge {
  fill: var(--bus-data);
}

.dot-data .dot-text {
  fill: #000;
}

.dot-control .dot-core,
.dot-control .dot-badge {
  fill: var(--bus-control);
}

.dot-control .dot-text {
  fill: #000;
}

.dot-internal .dot-badge {
  fill: #fff;
}

.dot-internal .dot-text {
  fill: #000;
}

@container (max-width: 670px) {
  /* When the machine stacks and the pane scrolls, keep the status bar where it can always be seen. */
  .cycle-strip {
    order: -1;
  }

  .machine {
    --scale: 1;
    min-height: 0;
    grid-template-columns: minmax(0, 1fr);
    grid-template-rows: auto;
    padding-right: 24px;
    gap: 14px;
  }

  .cpu,
  .lanes,
  .mem-panel,
  .io-ctrl {
    grid-column: 1;
    grid-row: auto;
  }

  .cpu-cols {
    flex: none;
  }

  .col {
    justify-content: flex-start;
  }

  .lanes,
  .io-ctrl {
    display: none;
  }

  .mem-panel {
    height: 380px;
  }

  .cycle-strip {
    grid-template-columns: minmax(0, 1fr) auto;
  }

  .clock-trace {
    display: none;
  }
}

/* ---------- screen, console, keyboard ---------- */

.screen-bezel {
  flex: none;
  position: relative;
  padding: 10px;
  background: var(--well-2);
  border: 2px solid var(--panel-edge);
  border-radius: 14px;
}

.screen-bezel canvas {
  display: block;
  /* Capping width (not just letting it fill 100%) keeps the canvas's 4:3 shape from making the
     console scroll out of view on very wide windows (e.g. dual-monitor setups) in any panel
     layout - not just .layout.hide-machine, which used to carry this rule alone. 430px is the
     rest of the I/O pane's vertical chrome: the topbar/toolbar above it, its own headers and
     gaps, and the console's own min-height (see .console below) - so the console always keeps a
     few lines visible without scrolling. min() with 100% means this only shrinks the screen when
     that height budget is actually the tighter constraint. */
  width: min(100%, calc((100vh - 430px) * 4 / 3));
  margin: 0 auto;
  aspect-ratio: 4 / 3;
  image-rendering: pixelated;
  background: #000;
  border-radius: 6px;
}

.screen-bezel::after {
  content: "";
  position: absolute;
  inset: 10px;
  border-radius: 6px;
  pointer-events: none;
  background: repeating-linear-gradient(0deg, rgba(0, 0, 0, 0.14) 0 1px, transparent 1px 3px);
}

.console {
  flex: 1;
  min-height: 110px;
  margin: 0;
  padding: 8px 10px;
  overflow: auto;
  background: var(--well);
  border: 1px solid var(--panel-edge-dim);
  border-radius: 8px;
  color: var(--green);
  font-family: var(--mono);
  font-size: 16px;
  line-height: 1.4;
  white-space: pre-wrap;
  word-break: break-all;
  text-shadow: var(--glow);
}

.console .echo {
  color: var(--faint);
  text-shadow: none;
}

.input-area {
  flex: none;
  display: none;
  flex-direction: column;
  gap: 6px;
}

.input-area.waiting {
  display: flex;
}

.input-banner {
  padding: 8px 10px;
  border-radius: 6px;
  background: var(--attention);
  color: var(--attention-ink);
  font-family: var(--mono);
  font-size: 14px;
  font-weight: 800;
  letter-spacing: 0.04em;
  text-align: center;
}

.input-row {
  display: flex;
  gap: 8px;
  flex: none;
}

.input-row input {
  flex: 1;
  min-width: 0;
  min-height: 46px;
  padding: 0 12px;
  font-family: var(--mono);
  font-size: 18px;
  color: var(--ink);
  background: var(--well);
  border: 1px solid var(--panel-edge);
  border-radius: 6px;
}

.input-row .btn {
  min-height: 46px;
  padding: 0 18px;
  font-size: 16px;
}

/* The input area only appears while the program is waiting for INP. */
.input-area.waiting .input-row input {
  min-height: 60px;
  font-size: 28px;
  font-weight: 700;
  border: 3px solid var(--attention);
  box-shadow: 0 0 0 4px var(--attention);
}

.input-area.waiting .input-row .btn {
  min-height: 60px;
  padding: 0 22px;
  font-size: 18px;
  background: var(--attention);
  border-color: var(--attention);
  color: var(--attention-ink);
}

.input-row input.bad,
.input-area.waiting .input-row input.bad {
  border-color: var(--red);
  box-shadow: 0 0 0 4px var(--red);
  animation: shake 0.3s;
}

@keyframes shake {
  25% {
    transform: translateX(-4px);
  }
  75% {
    transform: translateX(4px);
  }
}

.keypad {
  flex: none;
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 6px;
}

.keypad button {
  min-height: 40px;
  font-size: 18px;
  touch-action: manipulation;
}

.keypad .wide {
  grid-column: span 2;
  font-size: 14px;
  font-weight: 700;
}

/* The on-screen keys are only needed without a physical keyboard, so hide them for mouse users. */
@media (hover: hover) and (pointer: fine) {
  .keyboard-head,
  .keypad {
    display: none;
  }
}

/* ---------- responsive ---------- */

/* Hiding a panel lets the others use its space. Only applies on wide screens (narrow ones use tabs). */
@media (min-width: 1181px) {
  .layout.hide-code {
    grid-template-columns: minmax(700px, 2.6fr) minmax(250px, 0.9fr);
  }

  .layout.hide-code .pane-code {
    display: none;
  }

  .layout.hide-machine {
    grid-template-columns: minmax(320px, 1.5fr) minmax(320px, 1fr);
  }

  .layout.hide-machine .pane-machine {
    display: none;
  }
}

/* On large wide screens the CPU/memory diagram's growth is capped (MAX_SCALE in cpuview.js), so
   beyond this width extra space given to it mostly sits unused. Hand some of that width back to
   the I/O pane instead, so the screen is bigger. Lower specificity than .layout.hide-code /
   .layout.hide-machine above, so it never overrides those. */
@media (min-width: 1600px) {
  .layout {
    grid-template-columns: minmax(250px, 1fr) minmax(700px, 1.7fr) minmax(260px, 1.1fr);
  }

  /* .layout.hide-code's own ratio above predates the wide-screen rule just above, and gave the
     I/O pane a smaller share (0.9 of 3.5) than the three-pane "All" view now does (1.1 of 3.8) -
     so hiding the code pane could actually shrink the I/O pane instead of growing it. Redistribute
     the code pane's wide-screen share (1fr) between machine and I/O in their existing 1.7:1.1
     proportion so I/O only ever gets bigger as panels are hidden, never smaller.
     (.layout.hide-code has higher specificity than the plain .layout rule above, so this wins.) */
  .layout.hide-code {
    grid-template-columns: minmax(700px, 2.3fr) minmax(260px, 1.5fr);
  }
}

@media (max-width: 1180px) {
  .focus-btn {
    display: none;
  }

  .tabs {
    display: flex;
  }

  .layout {
    display: block;
    padding: 8px;
  }

  .pane {
    display: none;
    height: 100%;
  }

  .pane.is-active {
    display: flex;
  }

  .editor-wrap {
    min-height: 280px;
  }
}

@media (max-width: 900px) {
  .topbar,
  .toolbar {
    gap: 8px 10px;
  }

  .clock-setting .setting-label {
    display: none;
  }

  /* No room for it once things start getting tight, and narrow/mobile browsers generally lack
     the File System Access API that makes Save (rather than always Save As) meaningful anyway. */
  .file-status {
    display: none;
  }
}

@media (max-width: 640px) {
  .topbar {
    gap: 6px 10px;
    padding: 6px 8px;
  }

  .brand-name {
    font-size: 24px;
  }

  .toolbar {
    gap: 8px 10px;
    padding: 6px 8px;
  }

  .btn {
    padding: 0 4px;
    font-size: 13px;
    min-width: 0;
  }

  .setting-label {
    display: inline;
  }
}

@media (pointer: coarse) {
  .editor-wrap .cm-editor {
    font-size: 16px;
  }
}

@media (prefers-reduced-motion: reduce) {
  .flash {
    animation: none;
  }
}

/* ---------- Textbook theme: meaning carried by line style and weight, not only by colour ---------- */

/* The three buses stay distinguishable in greyscale and for colour-blind users:
   address is solid, data is dashed, control is dotted. A bus in use turns solid black and thicker. */
[data-theme="textbook"] .bus-line {
  stroke: #6b6b6b;
  opacity: 1;
}

[data-theme="textbook"] .bus-line.active {
  stroke: #000000;
  filter: none;
}

[data-theme="textbook"] .bus-data {
  stroke-dasharray: calc(14px * var(--scale)) calc(7px * var(--scale));
}

[data-theme="textbook"] .bus-control {
  stroke-dasharray: calc(3px * var(--scale)) calc(6px * var(--scale));
}

[data-theme="textbook"] .bus-label {
  fill: #000000;
}

[data-theme="textbook"] .dot-badge {
  fill: #ffffff;
  stroke: #000000;
}

[data-theme="textbook"] .dot-text {
  fill: #000000;
}

[data-theme="textbook"] .dot-core {
  fill: #000000;
  stroke: #ffffff;
}

[data-theme="textbook"] .phase-label {
  border: 1px solid #000000;
}

[data-theme="textbook"] .mem-row.is-mar {
  outline-style: dashed;
}

[data-theme="textbook"] .mem-row.is-video {
  background: rgba(0, 0, 0, 0.04);
}

[data-theme="textbook"] .screen-bezel {
  background: #e6e6e6;
  border-color: #000000;
}
