/* ============================================================
   BitOpCalc — web edition
   A bench instrument: paper chassis, one dark display well, one
   signal accent. Rules do the dividing; nothing floats.
   ============================================================ */

:root {
  /* Ground and surfaces */
  --ground: #e7e4de;
  --paper: #f7f6f2;
  --paper-2: #fcfbf8;
  --field: #fdfcf9;
  --field-focus: #ffffff;
  --key-face: #eeece6;
  --key-face-hover: #e5e2da;
  --key-edge: #c6c1b7;
  --cell: #f1eee7;
  --cell-edge: #d3cfc5;
  --bit-off: #a8a79d;

  /* Ink */
  --ink: #1a1a18;
  --ink-2: #45453f;
  --ink-3: #6f6f67;
  --ink-4: #77766d;

  /* Rules */
  --rule: #d8d4cb;
  --rule-2: #bcb7ac;

  /* The single signal */
  --accent: #e8641e;
  --accent-text: #a8430c;
  --accent-well: #ffb45e;
  --accent-soft: rgba(232, 100, 30, 0.1);
  --alert: #b23a0e;

  /* Display well */
  --well: #1b1b19;
  --well-edge: rgba(255, 255, 255, 0.09);
  --well-ink: #f4f1e8;
  --well-ink-2: #a8a89e;
  --well-ink-3: #8d8d83;
  --on-accent: #17140f;

  --grid-line: rgba(26, 26, 24, 0.055);

  /* Type */
  --sans: "Archivo", "Helvetica Neue", "Segoe UI", sans-serif;
  --mono: "IBM Plex Mono", ui-monospace, "Cascadia Code", Consolas, monospace;

  --fs-micro: 10.5px;
  --fs-small: 12px;
  --fs-body: 13.5px;

  --r: 3px;
  --r-sm: 2px;

  --pad: 20px;
}

/* Ink is the ET66 itself: dark body, backlit light display. */
:root[data-theme="ink"] {
  --ground: #101010;
  --paper: #1c1c1a;
  --paper-2: #232320;
  --field: #232320;
  --field-focus: #2a2a26;
  --key-face: #272724;
  --key-face-hover: #2f2f2b;
  --key-edge: #3b3b34;
  --cell: #292925;
  --cell-edge: #3d3d36;
  --bit-off: #726f66;

  --ink: #f1eee6;
  --ink-2: #c9c6bd;
  --ink-3: #928f86;
  --ink-4: #8a877e;

  --rule: #2e2e2a;
  --rule-2: #45453e;

  --accent: #f07a32;
  --accent-text: #f28a4d;
  --accent-well: #b04a10;
  --accent-soft: rgba(240, 122, 50, 0.14);
  --alert: #f0784f;

  --well: #e9e6df;
  --well-edge: rgba(23, 23, 15, 0.14);
  --well-ink: #16160f;
  --well-ink-2: #4c4c43;
  --well-ink-3: #7b7b70;
  --on-accent: #17140f;

  --grid-line: rgba(255, 255, 255, 0.045);
}

* { box-sizing: border-box; }

html {
  color-scheme: light;
  -webkit-text-size-adjust: 100%;
}

:root[data-theme="ink"] { color-scheme: dark; }

body {
  margin: 0;
  min-height: 100vh;
  background: var(--ground);
  color: var(--ink);
  font-family: var(--sans);
  font-size: var(--fs-body);
  line-height: 1.5;
  -webkit-font-smoothing: antialiased;
  text-rendering: optimizeLegibility;
  text-wrap: pretty;
}

/* The bench surface: an engineering grid, the way an instrument is
   photographed. Purely positional — it never sits behind type. */
body::before {
  content: "";
  position: fixed;
  inset: 0;
  z-index: 0;
  pointer-events: none;
  background-image:
    linear-gradient(to right, var(--grid-line) 1px, transparent 1px),
    linear-gradient(to bottom, var(--grid-line) 1px, transparent 1px);
  background-size: 32px 32px;
  background-position: center top;
}

:root[data-grid="off"] body::before { display: none; }

::selection { background: var(--accent); color: var(--on-accent); }

[hidden] { display: none !important; }

button { font: inherit; color: inherit; }

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

kbd {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  min-width: 18px;
  padding: 2px 5px;
  font: 500 10px/1.2 var(--mono);
  color: var(--ink-2);
  background: var(--paper-2);
  border: 1px solid var(--rule);
  border-bottom: 2px solid var(--rule-2);
  border-radius: var(--r-sm);
}

/* ---------- bench + chassis ---------- */

.bench {
  position: relative;
  z-index: 1;
  display: flex;
  flex-direction: column;
  width: 100%;
  min-height: 100vh;
  padding: clamp(20px, 5vh, 56px) 20px;
}

/* auto margins centre the instrument on the bench, and collapse to zero
   when the window is too short — no clipped top edge. */
.chassis {
  width: min(1200px, 100%);
  margin: auto;
  background: var(--paper);
  border: 1px solid var(--rule-2);
  border-radius: var(--r);
  overflow: hidden;
}

/* ---------- masthead ---------- */

.masthead {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 20px;
  flex-wrap: wrap;
  padding: 15px var(--pad);
  border-bottom: 1px solid var(--rule);
}

.brand {
  display: flex;
  align-items: center;
  gap: 10px;
  min-width: 0;
}

.brand-mark {
  flex: 0 0 auto;
  color: var(--ink);
}

.brand-mark-accent { fill: var(--accent); }

.brand-text {
  display: flex;
  flex-direction: column;
  gap: 1px;
  min-width: 0;
}

.wordmark {
  font: 600 15px/1 var(--sans);
  letter-spacing: 0.13em;
  color: var(--ink);
  white-space: nowrap;
}

.brand-sub {
  font: 400 var(--fs-micro)/1.2 var(--sans);
  color: var(--ink-3);
  white-space: nowrap;
}

.status {
  display: inline-flex;
  align-items: center;
  gap: 5px;
  margin-left: 6px;
  padding: 3px 7px 3px 6px;
  border: 1px solid var(--rule);
  border-radius: var(--r-sm);
  font: 500 9.5px/1 var(--sans);
  letter-spacing: 0.1em;
  text-transform: uppercase;
  color: var(--ink-3);
}

.status i {
  width: 5px;
  height: 5px;
  border-radius: 50%;
  background: var(--accent);
}

.masthead-controls {
  display: flex;
  align-items: center;
  gap: 14px;
  flex-wrap: wrap;
}

/* mode switch — hard-edged, no sliding thumb */

.segmented {
  display: flex;
  padding: 2px;
  border: 1px solid var(--rule-2);
  border-radius: var(--r);
  background: var(--paper-2);
}

.seg-btn {
  min-width: 96px;
  padding: 7px 13px;
  border: 0;
  border-radius: var(--r-sm);
  background: none;
  color: var(--ink-3);
  font: 500 var(--fs-small)/1 var(--sans);
  letter-spacing: 0.01em;
  cursor: pointer;
  transition: background 0.1s linear, color 0.1s linear;
}

.seg-btn:hover { color: var(--ink); background: var(--key-face-hover); }

.seg-btn.is-active {
  background: var(--ink);
  color: var(--paper);
}

/* width switch */

.width-switch {
  display: flex;
  align-items: center;
  gap: 7px;
}

.width-label {
  font: 500 9.5px/1 var(--sans);
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--ink-4);
}

.width-options {
  display: flex;
  border: 1px solid var(--rule-2);
  border-radius: var(--r);
  overflow: hidden;
  background: var(--paper-2);
}

.width-btn {
  min-width: 34px;
  padding: 7px 7px;
  border: 0;
  border-left: 1px solid var(--rule);
  background: transparent;
  color: var(--ink-3);
  font: 500 12px/1 var(--mono);
  cursor: pointer;
  transition: background 0.1s linear, color 0.1s linear;
}

.width-btn:first-child { border-left: 0; }
.width-btn:hover { background: var(--key-face-hover); color: var(--ink); }

.width-btn.is-active {
  background: var(--accent);
  color: var(--on-accent);
}

/* Auto is a mode, not a width: it tints rather than fills, so the orange
   fill keeps meaning "the width currently in effect". */
.auto-btn {
  padding: 7px 8px;
  border: 1px solid var(--rule);
  border-radius: var(--r);
  background: var(--paper-2);
  color: var(--ink-4);
  font: 500 10px/1 var(--mono);
  letter-spacing: 0.1em;
  text-transform: uppercase;
  cursor: pointer;
  transition: color 0.1s linear, background 0.1s linear, border-color 0.1s linear;
}

.auto-btn:hover {
  color: var(--ink);
  border-color: var(--rule-2);
}

.auto-btn.is-active {
  color: var(--accent-text);
  background: var(--accent-soft);
  border-color: var(--accent);
}

.icon-btn {
  width: 30px;
  height: 30px;
  display: inline-flex;
  align-items: center;
  justify-content: center;
  border: 1px solid var(--rule-2);
  border-radius: var(--r-sm);
  background: var(--paper-2);
  color: var(--ink-3);
  font: 500 13px/1 var(--mono);
  cursor: pointer;
  transition: background 0.1s linear, color 0.1s linear, border-color 0.1s linear;
}

.icon-btn:hover {
  color: var(--on-accent);
  background: var(--accent);
  border-color: var(--accent);
}

/* ---------- display well ---------- */

.display {
  background: var(--well);
  color: var(--well-ink);
  padding: 15px var(--pad) 17px;
  box-shadow: inset 0 1px 0 var(--well-edge);
}

.display-head {
  display: flex;
  align-items: center;
  gap: 12px;
  padding-bottom: 11px;
  border-bottom: 1px solid var(--well-edge);
}

.display-tag {
  font: 600 9.5px/1 var(--sans);
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--well-ink-2);
}

.readout-mode {
  font: 500 9.5px/1 var(--mono);
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--well-ink-3);
}

.overflow-badge {
  margin-left: auto;
  padding: 3px 7px;
  border: 1px solid var(--accent-well);
  border-radius: var(--r-sm);
  color: var(--accent-well);
  font: 600 9.5px/1 var(--sans);
  letter-spacing: 0.1em;
  text-transform: uppercase;
}

.readout-grid {
  display: grid;
  grid-template-columns: minmax(0, 1.72fr) minmax(0, 1fr);
  margin-top: 13px;
}

.readout-main,
.readout-side {
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.readout-side {
  padding-left: 22px;
  border-left: 1px solid var(--well-edge);
}

.readout-cell {
  position: relative;
  min-width: 0;
  padding: 2px 0 0;
  transition: background 0.12s linear;
}

.readout-hex { padding-right: 22px; }

.readout-binary {
  margin-top: 12px;
  padding: 11px 22px 0 0;
  border-top: 1px solid var(--well-edge);
}

.readout-signed {
  margin-top: 11px;
  padding-top: 11px;
  border-top: 1px solid var(--well-edge);
}

.readout-label {
  display: block;
  margin-bottom: 5px;
  font: 500 9.5px/1 var(--sans);
  letter-spacing: 0.14em;
  text-transform: uppercase;
  color: var(--well-ink-3);
}

.readout-value {
  display: block;
  color: var(--well-ink);
  font: 500 clamp(28px, 4.2vw, 48px)/1.06 var(--mono);
  letter-spacing: 0.01em;
  font-variant-numeric: tabular-nums;
  word-break: break-all;
  transition: color 0.12s linear;
}

.readout-binary .readout-value {
  /* 64 bits + 15 separators must stay on one line in the left column. */
  font-size: clamp(10.5px, 1.3vw, 13.5px);
  line-height: 1.85;
  letter-spacing: 0.045em;
  word-break: normal;
}

.readout-cell:not(.readout-hex):not(.readout-binary) .readout-value {
  font-size: clamp(17px, 2.1vw, 26px);
}

.readout-value .one { color: var(--well-ink); }
.readout-value .zero { color: var(--well-ink-3); }
.readout-value .sp { display: inline-block; width: 0.6em; }

.readout-cell.is-empty .readout-value {
  color: var(--well-ink-3);
  opacity: 0.55;
}

.readout-hex.is-empty .readout-value { font-size: clamp(20px, 2.6vw, 30px); }

.readout-cell.is-copyable { cursor: pointer; }

.readout-cell.is-copyable:hover .readout-value { color: var(--accent-well); }

.readout-cell.flash { animation: well-flash 0.5s ease-out; }

@keyframes well-flash {
  from { background: rgba(232, 100, 30, 0.16); }
  to { background: transparent; }
}

.copied-tag {
  position: absolute;
  top: 0;
  right: 0;
  font: 600 9.5px/1 var(--sans);
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--accent-well);
  opacity: 0;
  transition: opacity 0.15s linear;
  pointer-events: none;
}

.readout-cell.is-copied .copied-tag { opacity: 1; }

/* ---------- console ---------- */

.console {
  display: grid;
  grid-template-columns: minmax(360px, 0.84fr) minmax(0, 1.16fr);
  align-items: stretch;
}

.controls {
  display: flex;
  flex-direction: column;
  padding: var(--pad);
  border-right: 1px solid var(--rule);
  min-width: 0;
}

.register {
  padding: var(--pad);
  min-width: 0;
  display: flex;
  flex-direction: column;
}

.sec-head {
  display: flex;
  align-items: baseline;
  gap: 9px;
  margin-bottom: 16px;
}

.sec-num {
  font: 500 9.5px/1 var(--mono);
  letter-spacing: 0.1em;
  color: var(--ink-4);
}

.sec-head h2 {
  margin: 0;
  font: 600 11.5px/1 var(--sans);
  letter-spacing: 0.15em;
  text-transform: uppercase;
  color: var(--ink);
}

.sec-note {
  margin-left: auto;
  font: 400 var(--fs-micro)/1.2 var(--sans);
  color: var(--ink-3);
  white-space: nowrap;
}

/* ---------- operands ---------- */

.operand-row {
  display: grid;
  grid-template-columns: minmax(0, 1fr) auto minmax(0, 1fr);
  align-items: start;
  gap: 10px;
}

.operand {
  display: flex;
  flex-direction: column;
  gap: 6px;
  min-width: 0;
}

.operand-top {
  display: flex;
  align-items: center;
  gap: 6px;
}

.operand-tag {
  width: 18px;
  height: 18px;
  display: grid;
  place-items: center;
  border-radius: var(--r-sm);
  font: 600 10px/1 var(--mono);
}

.operand-tag.a {
  background: var(--ink);
  color: var(--paper);
  border: 1px solid var(--ink);
}

.operand-tag.b {
  background: transparent;
  color: var(--ink);
  border: 1px solid var(--rule-2);
}

.operand-label {
  font: 400 var(--fs-micro)/1 var(--sans);
  color: var(--ink-3);
}

.lcd-input {
  width: 100%;
  min-width: 0;
  padding: 11px 10px;
  border: 1px solid var(--rule);
  border-bottom: 2px solid var(--rule-2);
  border-radius: var(--r-sm);
  background: var(--field);
  color: var(--ink);
  font: 500 clamp(15px, 1.2vw, 18px)/1.25 var(--mono);
  letter-spacing: 0.01em;
  outline: none;
  transition: border-color 0.1s linear, background 0.1s linear;
}

.lcd-input::placeholder { color: var(--ink-4); }

.lcd-input:focus {
  border-color: var(--rule-2);
  border-bottom-color: var(--accent);
  background: var(--field-focus);
}

.lcd-input.is-unused {
  color: var(--ink-4);
  border-style: dashed;
  border-bottom-style: solid;
}

.operand-hex {
  font: 400 var(--fs-micro)/1.4 var(--mono);
  font-variant-numeric: tabular-nums;
  color: var(--ink-3);
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
  transition: color 0.1s linear;
}

.operand-hex.is-empty { color: var(--ink-4); }
.operand-hex.is-invalid { color: var(--alert); }
.operand-hex.is-unused { color: var(--ink-4); font-style: italic; }

.op-display {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 3px;
  padding-top: 22px;
}

.op-symbol {
  font: 500 20px/1 var(--mono);
  color: var(--accent-text);
}

.op-name {
  font: 500 9px/1 var(--sans);
  letter-spacing: 0.12em;
  color: var(--ink-4);
  white-space: nowrap;
}

/* ---------- operator keypad ---------- */

.keypad {
  display: grid;
  grid-template-columns: repeat(3, minmax(0, 1fr));
  gap: 7px;
  margin-top: 18px;
  /* In the stacked layout the panel is ~900px wide; keys that wide stop
     reading as keys. */
  max-width: 470px;
}

.op-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 4px;
  padding: 12px 8px 11px;
  border: 1px solid var(--key-edge);
  border-bottom-width: 2px;
  border-radius: var(--r);
  background: var(--key-face);
  color: var(--ink);
  text-align: left;
  cursor: pointer;
  transition: background 0.09s linear, border-color 0.09s linear, color 0.09s linear;
}

.op-btn:hover { background: var(--key-face-hover); }

.op-btn:active {
  border-bottom-width: 1px;
  transform: translateY(1px);
}

.op-glyph {
  font: 500 15px/1.15 var(--mono);
  color: var(--ink);
}

.op-text {
  font: 500 9px/1 var(--sans);
  letter-spacing: 0.11em;
  color: var(--ink-3);
  white-space: nowrap;
}

.op-btn.is-active {
  background: var(--accent);
  border-color: var(--accent);
  border-bottom-color: rgba(0, 0, 0, 0.35);
}

.op-btn.is-active .op-glyph { color: var(--on-accent); }
.op-btn.is-active .op-text { color: rgba(23, 20, 15, 0.72); }

.field-hint {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  flex-wrap: wrap;
  margin-top: auto;
  padding-top: 11px;
  border-top: 1px solid var(--rule);
  color: var(--ink-3);
  font-size: var(--fs-micro);
}

.field-hint b {
  margin-left: 5px;
  color: var(--ink-2);
  font: 500 var(--fs-micro)/1 var(--mono);
}

.field-hint b:first-child { margin-left: 3px; }
.field-hint kbd { margin-right: 1px; }

/* ---------- expression ---------- */

.expr-shell {
  display: flex;
  align-items: center;
  gap: 8px;
  padding: 0 8px 0 11px;
  border: 1px solid var(--rule);
  border-bottom: 2px solid var(--rule-2);
  border-radius: var(--r-sm);
  background: var(--field);
  transition: border-color 0.1s linear, background 0.1s linear;
}

.expr-shell:focus-within {
  border-color: var(--rule-2);
  border-bottom-color: var(--accent);
  background: var(--field-focus);
}

.expr-shell.has-error { border-bottom-color: var(--alert); }

.expr-prompt {
  font: 600 13px/1 var(--mono);
  color: var(--accent-text);
}

#expr-input {
  flex: 1;
  min-width: 0;
  padding: 12px 0;
  border: 0;
  background: none;
  color: var(--ink);
  font: 500 14px/1.4 var(--mono);
  outline: none;
}

#expr-input::placeholder { color: var(--ink-4); }

.run-btn {
  flex: 0 0 auto;
  padding: 7px 11px;
  border: 1px solid var(--accent);
  border-bottom: 2px solid rgba(0, 0, 0, 0.35);
  border-radius: var(--r-sm);
  background: var(--accent);
  color: var(--on-accent);
  font: 600 10px/1 var(--sans);
  letter-spacing: 0.12em;
  text-transform: uppercase;
  cursor: pointer;
  transition: filter 0.09s linear;
}

.run-btn:hover { filter: brightness(1.07); }
.run-btn:active { transform: translateY(1px); }

.expr-error {
  min-height: 17px;
  margin: 7px 0 0;
  color: var(--alert);
  font-size: var(--fs-small);
}

.expr-chips {
  display: flex;
  flex-wrap: wrap;
  gap: 5px;
  margin-top: 3px;
}

.chip {
  min-width: 30px;
  padding: 6px 8px;
  border: 1px solid var(--key-edge);
  border-bottom-width: 2px;
  border-radius: var(--r-sm);
  background: var(--key-face);
  color: var(--ink-2);
  font: 500 var(--fs-small)/1 var(--mono);
  cursor: pointer;
  transition: background 0.09s linear, color 0.09s linear, border-color 0.09s linear;
}

.chip:hover {
  background: var(--key-face-hover);
  color: var(--ink);
  border-color: var(--accent);
}

.chip:active { transform: translateY(1px); }

.precedence {
  display: flex;
  align-items: center;
  gap: 8px;
  flex-wrap: wrap;
  margin-top: 15px;
  padding-top: 12px;
  border-top: 1px solid var(--rule);
}

.prec-label {
  font: 500 9.5px/1 var(--sans);
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--ink-4);
}

.prec-seq {
  display: inline-flex;
  align-items: center;
  gap: 5px;
}

.prec-seq code {
  padding: 2px 5px;
  border: 1px solid var(--rule);
  border-radius: var(--r-sm);
  background: var(--paper-2);
  color: var(--ink-2);
  font: 500 var(--fs-small) var(--mono);
}

.prec-seq i {
  font-style: normal;
  color: var(--ink-4);
  font-size: var(--fs-micro);
}

.prec-low {
  margin-left: auto;
  color: var(--ink-4);
  font-size: var(--fs-micro);
}

.expr-tips {
  margin-top: auto;
  padding-top: 12px;
  display: grid;
  gap: 3px;
  color: var(--ink-3);
  font-size: var(--fs-micro);
}

.expr-tips p { margin: 0; }

.expr-tips b {
  color: var(--ink-2);
  font-weight: 500;
}

/* ---------- register ---------- */

.bitfield {
  display: flex;
  flex-direction: column;
}

.bitfield-scroll {
  container-type: inline-size;
  display: flex;
  flex-direction: column;
  overflow-x: auto;
  padding: 2px 2px 8px;
  margin: 0 -2px;
  scrollbar-width: thin;
  scrollbar-color: var(--rule-2) transparent;

  --bit-gap: 1px;
  --bit-w: 12px;
  --byte-gap: 10px;
  --bit-font: 10px;
}

/* Cells scale with the panel so all w bits fit without a scrollbar:
   row = label 26 + w*bit-w + (w - bytes) gaps + (bytes - 1)*byte-gap. */
.bitfield.w8 .bitfield-scroll {
  --byte-gap: 16px;
  --bit-w: clamp(20px, calc((100cqw - 50px) / 8), 72px);
  --bit-font: clamp(11px, calc(var(--bit-w) * 0.4), 24px);
}

.bitfield.w16 .bitfield-scroll {
  --byte-gap: 12px;
  --bit-w: clamp(12px, calc((100cqw - 66px) / 16), 38px);
  --bit-font: clamp(9px, calc(var(--bit-w) * 0.5), 13px);
}

.bitfield.w32 .bitfield-scroll {
  --byte-gap: 10px;
  --bit-w: clamp(7px, calc((100cqw - 96px) / 32), 26px);
  --bit-font: clamp(7px, calc(var(--bit-w) * 0.66), 12px);
}

.bitfield.w64 .bitfield-scroll {
  --byte-gap: 6px;
  --bit-w: clamp(6px, calc((100cqw - 132px) / 64), 18px);
  --bit-font: clamp(6px, calc(var(--bit-w) * 0.84), 11px);
}

.bitfield-scroll::-webkit-scrollbar { height: 6px; }
.bitfield-scroll::-webkit-scrollbar-track { background: transparent; }
.bitfield-scroll::-webkit-scrollbar-thumb {
  background: var(--rule-2);
  border-radius: 3px;
}

.bit-ruler,
.bit-row {
  display: flex;
  align-items: center;
  gap: var(--byte-gap);
  width: max-content;
}

.bit-ruler { padding-bottom: 5px; }

.ruler-spacer,
.bit-label {
  flex: 0 0 26px;
  width: 26px;
}

.ruler-group {
  width: calc(8 * var(--bit-w) + 7 * var(--bit-gap));
  text-align: center;
  font: 400 9.5px/1 var(--mono);
  letter-spacing: 0.02em;
  white-space: nowrap;
  color: var(--ink-4);
}

.ruler-group b {
  font-weight: 500;
  color: var(--ink-3);
}

.ruler-group { color: var(--ink-4); }

.ruler-group i {
  font-style: normal;
  color: var(--ink-4);
}

.bitfield.w64 .ruler-group i { display: none; }

/* 64-bit cells are ~8px wide: the digits stop being legible long before
   this, so the register reads as a pure bit pattern. */
.bitfield.w64 .bit { font-size: 0; }

/* At 8px per cell the diff marker degenerates into noise; the popcount
   line still reports the count. */
.bitfield.w64 .bit.is-diff::after { display: none; }
.bitfield.w64 .legend-item.diff-legend { display: none; }

.bit-row { padding: 4px 0; }

.bit-label {
  height: 22px;
  display: grid;
  place-items: center;
  border: 1px solid var(--rule-2);
  border-radius: var(--r-sm);
  background: var(--paper-2);
  color: var(--ink-2);
  font: 600 9.5px/1 var(--mono);
  letter-spacing: 0.04em;
}

.row-a .bit-label {
  background: var(--ink);
  border-color: var(--ink);
  color: var(--paper);
}

.row-b .bit-label {
  background: transparent;
  color: var(--ink);
}

.row-r .bit-label {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--on-accent);
}

.byte-group {
  display: flex;
  gap: var(--bit-gap);
}

.bit {
  position: relative;
  width: var(--bit-w);
  height: calc(var(--bit-w) + 10px);
  display: grid;
  place-items: center;
  padding: 0;
  border: 1px solid var(--cell-edge);
  border-radius: 1px;
  background: var(--cell);
  color: var(--bit-off);
  font: 500 var(--bit-font)/1 var(--mono);
  font-variant-numeric: tabular-nums;
  cursor: default;
  transition: background 0.09s linear, color 0.09s linear, border-color 0.09s linear;
}

button.bit { cursor: pointer; }

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

.bit:disabled { opacity: 0.4; cursor: not-allowed; }

/* Set bits carry the fill weight of their row: A solid ink, B mid grey,
   R the signal. Weight, not hue, is what separates the rows. */
.row-a .bit.is-set {
  background: var(--ink);
  border-color: var(--ink);
  color: var(--paper);
}

.row-b .bit.is-set {
  background: var(--ink-3);
  border-color: var(--ink-3);
  color: var(--paper);
}

.row-r .bit.is-set {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--on-accent);
}

.row-a .bit.is-set:hover,
.row-b .bit.is-set:hover { border-color: var(--accent); }

.bit.is-diff::after {
  content: "";
  position: absolute;
  left: 1px;
  right: 1px;
  bottom: 1px;
  height: 2px;
  background: var(--accent);
}

.row-b.is-unused { opacity: 0.42; }

.bit-separator {
  height: 1px;
  margin: 4px 0 4px calc(28px + var(--byte-gap));
  background-image: repeating-linear-gradient(90deg, var(--rule-2) 0 4px, transparent 4px 9px);
}

.bitfield.is-idle .bit-row { opacity: 0.55; }
.bitfield.is-idle .row-r { opacity: 0.65; }

.bit-legend {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 7px 16px;
  margin-top: auto;
  padding-top: 12px;
  border-top: 1px solid var(--rule);
  color: var(--ink-3);
  font-size: var(--fs-micro);
}

.legend-item {
  display: inline-flex;
  align-items: center;
  gap: 6px;
}

.legend-swatch {
  width: 9px;
  height: 9px;
  border-radius: 1px;
}

.legend-swatch.set {
  background: var(--ink);
}

.legend-swatch.unset {
  border: 1px solid var(--rule-2);
}

.legend-swatch.diff {
  height: 3px;
  background: var(--accent);
}

.legend-note {
  margin-left: auto;
  color: var(--ink-4);
  font-size: var(--fs-micro);
}

/* ---------- byte ledger + stats ---------- */

.byte-ledger {
  margin-top: 15px;
  padding-top: 12px;
  border-top: 1px solid var(--rule);
}

.ledger-head {
  display: flex;
  align-items: baseline;
  gap: 10px;
  margin-bottom: 8px;
}

.ledger-title {
  font: 500 9.5px/1 var(--sans);
  letter-spacing: 0.13em;
  text-transform: uppercase;
  color: var(--ink-3);
}

.ledger-note {
  margin-left: auto;
  font-size: var(--fs-micro);
  color: var(--ink-4);
}

.ledger-cells {
  display: grid;
  grid-template-columns: repeat(var(--bytes, 4), minmax(0, 1fr));
  max-width: calc(var(--bytes, 4) * 168px);
}

.ledger-cell {
  min-width: 0;
  padding: 7px 12px 6px;
  border-left: 1px solid var(--rule);
}

.ledger-cell:first-child {
  border-left: 0;
  padding-left: 0;
}

.ledger-cell:last-child { padding-right: 0; }

.ledger-idx {
  display: block;
  margin-bottom: 2px;
  font: 400 9.5px/1.2 var(--mono);
  letter-spacing: 0.04em;
  color: var(--ink-4);
}

.ledger-val {
  display: block;
  font: 500 14px/1.15 var(--mono);
  font-variant-numeric: tabular-nums;
  color: var(--ink-3);
  transition: color 0.1s linear;
}

.ledger-cell.is-active .ledger-val { color: var(--ink); }

.ledger-bin {
  display: block;
  margin-top: 2px;
  font: 400 9px/1.2 var(--mono);
  letter-spacing: 0.02em;
  font-variant-numeric: tabular-nums;
  color: var(--ink-4);
}

.ledger-cell.is-zero .ledger-bin { opacity: 0.7; }

.stats-strip {
  display: flex;
  flex-wrap: wrap;
  gap: 7px 22px;
  margin-top: 13px;
  padding-top: 11px;
  border-top: 1px solid var(--rule);
}

.stat {
  display: inline-flex;
  align-items: baseline;
  gap: 7px;
}

.stat-key {
  font: 500 9.5px/1 var(--sans);
  letter-spacing: 0.13em;
  text-transform: uppercase;
  color: var(--ink-4);
}

.stat-pair {
  display: inline-flex;
  align-items: baseline;
  gap: 3px;
}

.stat-pair b {
  font: 500 13px/1 var(--mono);
  font-variant-numeric: tabular-nums;
  color: var(--ink);
}

.stat-pair i {
  font-style: normal;
  font-size: 9.5px;
  color: var(--ink-4);
}

.stat-pair.is-b b { color: var(--ink-3); }
.stat-pair.is-r b { color: var(--accent-text); }
.stat-pair.is-diff b { color: var(--ink-2); }

.stat-pair.is-empty b { color: var(--ink-4); }

/* ---------- spec band ---------- */

.spec-band {
  display: flex;
  flex-wrap: wrap;
  gap: 7px 30px;
  padding: 11px var(--pad);
  border-top: 1px solid var(--rule);
  background: var(--paper);
}

.spec {
  display: inline-flex;
  align-items: baseline;
  gap: 7px;
}

.spec i {
  font-style: normal;
  font: 500 9.5px/1 var(--sans);
  letter-spacing: 0.12em;
  text-transform: uppercase;
  color: var(--ink-4);
}

.spec b {
  font: 500 11.5px/1 var(--mono);
  font-variant-numeric: tabular-nums;
  color: var(--ink-2);
}

/* ---------- colophon ---------- */

.colophon {
  display: flex;
  align-items: center;
  flex-wrap: wrap;
  gap: 8px 16px;
  padding: 11px var(--pad) 12px;
  border-top: 1px solid var(--rule);
  background: var(--paper-2);
  color: var(--ink-3);
  font-size: var(--fs-micro);
}

.hint {
  display: inline-flex;
  align-items: center;
  gap: 4px;
}



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

.help-overlay {
  position: fixed;
  inset: 0;
  z-index: 50;
  display: grid;
  place-items: center;
  padding: 24px;
  background: rgba(20, 20, 18, 0.62);
  animation: fade-in 0.12s linear;
}

@keyframes fade-in { from { opacity: 0; } }

.help-card {
  width: min(540px, 100%);
  max-height: calc(100vh - 48px);
  overflow-y: auto;
  padding: 18px 20px 18px;
  border: 1px solid var(--ink);
  border-radius: var(--r);
  background: var(--paper);
  animation: rise 0.14s ease-out;
}

@keyframes rise { from { opacity: 0; transform: translateY(6px); } }

.help-head {
  display: flex;
  align-items: center;
  gap: 10px;
  padding-bottom: 13px;
  border-bottom: 1px solid var(--rule);
}

.help-head h2 {
  margin: 0;
  font: 600 11.5px/1 var(--sans);
  letter-spacing: 0.15em;
  text-transform: uppercase;
  color: var(--ink);
}

.help-head .icon-btn { margin-left: auto; }

.help-grid { display: grid; }

.help-row {
  display: grid;
  grid-template-columns: 160px minmax(0, 1fr);
  align-items: center;
  gap: 14px;
  padding: 8px 2px;
  border-bottom: 1px solid var(--rule);
  color: var(--ink-3);
  font-size: var(--fs-small);
}

.help-keys {
  display: inline-flex;
  align-items: center;
  gap: 3px;
  flex-wrap: wrap;
  color: var(--ink-2);
}

.help-alt {
  color: var(--ink-4);
  font-size: var(--fs-micro);
}

.help-section {
  margin-top: 15px;
  padding-top: 13px;
  border-top: 1px solid var(--rule);
}

.help-section h3 {
  margin: 0 0 7px;
  font: 600 10.5px/1 var(--sans);
  letter-spacing: 0.13em;
  text-transform: uppercase;
  color: var(--ink);
}

.help-section p {
  margin: 0;
  color: var(--ink-3);
  font-size: var(--fs-small);
  line-height: 1.7;
}

.help-section code {
  padding: 1px 4px;
  border: 1px solid var(--rule);
  border-radius: var(--r-sm);
  background: var(--paper-2);
  color: var(--ink-2);
  font-family: var(--mono);
  font-size: 11px;
}

.help-foot {
  margin: 14px 0 0;
  color: var(--ink-4);
  font-size: var(--fs-micro);
  line-height: 1.7;
}

body.help-open { overflow: hidden; }

/* ---------- tweaks ---------- */

.tweaks {
  position: fixed;
  right: 18px;
  bottom: 18px;
  z-index: 60;
  display: flex;
  flex-direction: column;
  align-items: flex-end;
  gap: 8px;
}

.tweaks-toggle {
  padding: 7px 12px;
  border: 1px solid var(--rule-2);
  border-bottom-width: 2px;
  border-radius: var(--r);
  background: var(--paper);
  color: var(--ink-3);
  font: 600 9.5px/1 var(--sans);
  letter-spacing: 0.14em;
  text-transform: uppercase;
  cursor: pointer;
  transition: color 0.1s linear, border-color 0.1s linear;
}

.tweaks-toggle:hover {
  color: var(--ink);
  border-color: var(--accent);
}

.tweaks-toggle[aria-expanded="true"] {
  background: var(--accent);
  border-color: var(--accent);
  color: var(--on-accent);
}

.tweaks-panel {
  width: 218px;
  padding: 13px 13px 14px;
  border: 1px solid var(--rule-2);
  border-radius: var(--r);
  background: var(--paper);
}

.tweaks-title {
  margin: 0 0 10px;
  padding-bottom: 8px;
  border-bottom: 1px solid var(--rule);
  font: 600 9.5px/1 var(--sans);
  letter-spacing: 0.16em;
  text-transform: uppercase;
  color: var(--ink);
}

.tweaks-row {
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 4px 0;
}

.tweaks-label {
  font: 400 var(--fs-micro)/1 var(--sans);
  color: var(--ink-3);
}

.tweaks-seg {
  display: flex;
  border: 1px solid var(--rule);
  border-radius: var(--r-sm);
  overflow: hidden;
}

.tweaks-seg button {
  padding: 4px 7px;
  border: 0;
  border-left: 1px solid var(--rule);
  background: var(--paper-2);
  color: var(--ink-3);
  font: 500 10px/1 var(--mono);
  cursor: pointer;
  transition: background 0.1s linear, color 0.1s linear;
}

.tweaks-seg button:first-child { border-left: 0; }

.tweaks-seg button:hover { color: var(--ink); }

.tweaks-seg button.is-active {
  background: var(--ink);
  color: var(--paper);
}

/* ---------- density ---------- */

:root[data-density="compact"] {
  --pad: 14px;
  --fs-micro: 10px;
}

:root[data-density="compact"] .masthead { padding-top: 11px; padding-bottom: 11px; }
:root[data-density="compact"] .display { padding-top: 12px; padding-bottom: 13px; }
:root[data-density="compact"] .keypad { gap: 5px; margin-top: 13px; }
:root[data-density="compact"] .op-btn { padding: 7px 9px 6px; }
:root[data-density="compact"] .bit-row { padding: 2px 0; }
:root[data-density="compact"] .ledger-cell { padding: 5px 7px 4px; }
:root[data-density="compact"] .stats-strip { margin-top: 10px; padding-top: 9px; }

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

@media (max-width: 1060px) {
  .console { grid-template-columns: minmax(0, 1fr); }
  .controls { border-right: 0; border-bottom: 1px solid var(--rule); }
  /* With the console stacked the panel is wide: run the operators as one
     bank of keys instead of a narrow 3-wide block. */
  .keypad {
    grid-template-columns: repeat(6, minmax(0, 1fr));
    max-width: none;
  }
}

@media (max-width: 700px) {
  .keypad { grid-template-columns: repeat(3, minmax(0, 1fr)); }
}

@media (max-width: 900px) {
  .readout-grid { grid-template-columns: minmax(0, 1fr); }

  .readout-hex { padding-right: 0; }
  .readout-binary { padding-right: 0; }

  .readout-side {
    margin-top: 12px;
    padding: 11px 0 0;
    border-left: 0;
    border-top: 1px solid var(--well-edge);
  }
}

@media (max-width: 780px) {
  .bench { padding: 14px 12px; }
  .masthead { gap: 12px; padding-top: 12px; padding-bottom: 12px; }
  .brand { width: 100%; }
  .masthead-controls { width: 100%; justify-content: space-between; gap: 10px; }
  .segmented { flex: 1 1 100%; }
  .seg-btn { flex: 1; min-width: 0; }
  .width-switch { flex: 1 1 auto; }
  .operand-row { grid-template-columns: minmax(0, 1fr); gap: 10px; }
  .op-display {
    flex-direction: row;
    align-items: baseline;
    justify-content: flex-start;
    gap: 7px;
    padding-top: 0;
  }
  .op-symbol { font-size: 18px; }
  .help-row { grid-template-columns: 1fr; gap: 4px; }
  .legend-note { margin-left: 0; }
  .tweaks { right: 8px; bottom: 8px; }
  .tweaks-toggle { padding: 5px 9px; font-size: 9px; letter-spacing: 0.1em; }
}

@media (max-width: 460px) {
  .brand-sub, .status { display: none; }
  .width-label { display: none; }
  .seg-btn { padding: 7px 9px; font-size: 11.5px; }
  .keypad { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .ledger-cell { padding: 6px 5px 5px; }
  .ledger-bin { display: none; }
  .spec-band { gap: 6px 18px; }
}

/* ---------- reduced motion ---------- */

@media (prefers-reduced-motion: reduce) {
  *,
  *::before,
  *::after {
    animation-duration: 0.001ms !important;
    animation-iteration-count: 1 !important;
    transition-duration: 0.001ms !important;
    scroll-behavior: auto !important;
  }
}
