/* KCPU workbench: a wireframe that has been marked up.
   graphite lines on cool paper, blue pencil for whatever the CPU is
   touching right now, yellow highlighter for what just changed. */

:root {
  --paper: #f4f5f7;
  --sheet: #fbfbfc;
  --grid: #e6e8ec;
  --ink: #1d2230;
  --ink-2: #4a5263;
  --pencil: #9aa1ae;
  --faint: #c9ced6;
  --blue: #2f5bea;
  --blue-wash: #e6ecfd;
  --marker: #ffe66b;
  --red: #c8352b;
  --red-wash: #fbe9e7;

  --mono: "IBM Plex Mono", ui-monospace, "SF Mono", Menlo, monospace;
  --sans: "IBM Plex Sans Condensed", "Arial Narrow", system-ui, sans-serif;

  --line: 1.5px;
  --r: 6px;
  --lh: 20px; /* editor line height, shared by gutter, highlight and textarea */
  color-scheme: light;
}

@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) {
    --paper: #15181e;
    --sheet: #1b1f27;
    --grid: #20252e;
    --ink: #e4e7ec;
    --ink-2: #aab1bd;
    --pencil: #6b7383;
    --faint: #3a414d;
    --blue: #7c9cff;
    --blue-wash: #1f2a4a;
    --marker: #6b5a10;
    --red: #ff7a6e;
    --red-wash: #3a1d1b;
    color-scheme: dark;
  }
}
:root[data-theme="dark"] {
  --paper: #15181e; --sheet: #1b1f27; --grid: #20252e; --ink: #e4e7ec; --ink-2: #aab1bd;
  --pencil: #6b7383; --faint: #3a414d; --blue: #7c9cff; --blue-wash: #1f2a4a;
  --marker: #6b5a10; --red: #ff7a6e; --red-wash: #3a1d1b; color-scheme: dark;
}

* { box-sizing: border-box; }
html, body { margin: 0; }
body {
  background-color: var(--paper);
  background-image:
    linear-gradient(var(--grid) 1px, transparent 1px),
    linear-gradient(90deg, var(--grid) 1px, transparent 1px);
  background-size: 24px 24px;
  color: var(--ink);
  font: 15px/1.45 var(--sans);
  min-height: 100vh;
}
button, input, select, textarea { font: inherit; color: inherit; }
code, .mono { font-family: var(--mono); }
h2 { font-size: 15px; font-weight: 600; margin: 0; letter-spacing: .01em; }
.visually-hidden { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); white-space: nowrap; }
:focus-visible { outline: 2px solid var(--blue); outline-offset: 2px; }

/* ---------------------------------------------------------------- top bar */
.topbar {
  display: flex; flex-wrap: wrap; gap: 12px 24px; align-items: center; justify-content: space-between;
  padding: 14px 20px; border-bottom: var(--line) solid var(--ink); background: var(--sheet);
}
.brand { display: flex; align-items: center; gap: 10px; min-width: 0; }
.chip { width: 26px; height: 26px; fill: none; stroke: var(--ink); stroke-width: 1.5; stroke-linecap: round; }
.brand-name { font: 600 18px/1 var(--mono); letter-spacing: .02em; }
.brand-sub { color: var(--ink-2); font-size: 14px; }

.controls { display: flex; flex-wrap: wrap; align-items: center; gap: 8px; }
.divider { width: 1px; height: 24px; background: var(--faint); margin: 0 4px; }

.btn {
  appearance: none; cursor: pointer; padding: 6px 14px; min-height: 34px;
  background: var(--sheet); border: var(--line) solid var(--ink); border-radius: 4px;
  font-weight: 500;
}
.btn:hover { background: var(--blue-wash); }
.btn:active { transform: translateY(1px); }
.btn:disabled { cursor: not-allowed; color: var(--pencil); border-color: var(--faint); background: transparent; }
.btn-primary { background: var(--ink); color: var(--sheet); }
.btn-primary:hover { background: var(--blue); border-color: var(--blue); }
.btn-small { min-height: 28px; padding: 2px 10px; font-size: 14px; }

.select select {
  appearance: none; cursor: pointer; min-height: 34px; padding: 6px 30px 6px 12px;
  border: var(--line) dashed var(--pencil); border-radius: 4px; background: var(--sheet)
    url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='10' height='6'%3E%3Cpath d='M1 1l4 4 4-4' fill='none' stroke='%239aa1ae' stroke-width='1.5'/%3E%3C/svg%3E") no-repeat right 11px center;
}
.select select:hover { border-color: var(--ink); }

.speed { display: flex; align-items: center; gap: 8px; color: var(--ink-2); font-size: 14px; margin-left: 4px; }
.speed input { width: 90px; accent-color: var(--blue); }
.speed output { font-size: 14px; font-weight: 500; color: var(--ink); min-width: 48px; }

/* ---------------------------------------------------------------- layout */
.workbench {
  display: grid; grid-template-columns: minmax(340px, 440px) minmax(0, 1fr);
  gap: 16px; padding: 16px 20px 24px; align-items: start;
}
.machine {
  display: grid; gap: 16px; min-width: 0;
  grid-template-columns: minmax(300px, 380px) minmax(0, 1fr);
  grid-template-areas: "datapath datapath" "regs mem" "listing listing";
}
.panel-datapath { grid-area: datapath; }
.panel-regs { grid-area: regs; }
.panel-mem { grid-area: mem; }
.panel-listing { grid-area: listing; }

.panel {
  background: var(--sheet); border: var(--line) solid var(--ink); border-radius: var(--r);
  min-width: 0; display: flex; flex-direction: column;
}
.panel-head {
  display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 10px 14px; border-bottom: 1px dashed var(--pencil);
}
.hint { color: var(--ink-2); font-size: 14px; }

/* ---------------------------------------------------------------- editor */
.panel-source { position: sticky; top: 16px; height: calc(100vh - 100px); min-height: 460px; }
.editor { position: relative; flex: 1; display: flex; min-height: 0; overflow: hidden; font: 13px/var(--lh) var(--mono); }
.gutter {
  flex: none; width: 46px; padding: 10px 0; border-right: 1px dashed var(--faint);
  overflow: hidden; user-select: none; color: var(--pencil);
}
.gutter-inner { will-change: transform; }
.gutter button {
  display: flex; align-items: center; justify-content: flex-end; gap: 4px; width: 100%; height: var(--lh);
  padding: 0 8px 0 0; border: 0; background: none; cursor: pointer; font: inherit; color: inherit;
}
.gutter button::before {
  content: ""; width: 8px; height: 8px; border-radius: 50%; border: 1.5px solid transparent;
}
.gutter button:hover::before { border-color: var(--faint); }
.gutter button.bp { color: var(--red); }
.gutter button.bp::before { background: var(--red); border-color: var(--red); }
.gutter button.cur { color: var(--blue); font-weight: 600; }

.code { position: relative; flex: 1; min-width: 0; }
.hl, #source {
  position: absolute; inset: 0; margin: 0; padding: 10px 12px; border: 0;
  font: inherit; line-height: var(--lh); tab-size: 4; white-space: pre; overflow: auto;
}
.hl { pointer-events: none; overflow: hidden; color: var(--ink); }
.hl .l { min-height: var(--lh); padding: 0 12px; margin: 0 -12px; }
.hl .l.cur { background: var(--blue-wash); box-shadow: inset 3px 0 0 var(--blue); }
.hl .l.err { background: var(--red-wash); }
.hl .l.err .e { text-decoration: underline wavy var(--red); text-underline-offset: 4px; }
#source {
  resize: none; background: transparent; color: transparent; caret-color: var(--ink); outline: none;
}
#source::selection { background: color-mix(in srgb, var(--blue) 28%, transparent); color: transparent; }
.t-op { font-weight: 600; }
.t-reg { color: var(--blue); }
.t-num { color: #8a5a00; }
.t-str { color: #3d7a3a; }
.t-dir { color: #8f3fa8; }
.t-lbl { text-decoration: underline; text-decoration-color: var(--pencil); text-underline-offset: 3px; }
.t-com { color: var(--pencil); font-style: italic; }
@media (prefers-color-scheme: dark) {
  :root:not([data-theme="light"]) .t-num { color: #e8b75a; }
  :root:not([data-theme="light"]) .t-str { color: #8fce8a; }
  :root:not([data-theme="light"]) .t-dir { color: #d39ae6; }
}

.status {
  border-top: 1px dashed var(--pencil); padding: 10px 14px; font-size: 14px; min-height: 44px;
  display: flex; gap: 8px; align-items: baseline;
}
.status.is-error { color: var(--red); background: var(--red-wash); border-top: var(--line) solid var(--red); border-radius: 0 0 var(--r) var(--r); }
.status strong { font-weight: 600; white-space: nowrap; }

/* ---------------------------------------------------------------- datapath */
.pipeline {
  list-style: none; margin: 0; padding: 18px 14px 6px;
  display: grid; grid-template-columns: .7fr 1.1fr 1.2fr 1.3fr 1fr; gap: 22px;
}
.stage {
  position: relative; border: var(--line) dashed var(--pencil); border-radius: var(--r);
  padding: 10px 10px 12px; min-height: 96px; display: flex; flex-direction: column; gap: 6px;
  transition: border-color .2s, background-color .2s;
}
.stage.lit .stage-body { animation: stage-in .22s ease-out; }
@keyframes stage-in { from { opacity: 0; transform: translateY(3px); } to { opacity: 1; transform: none; } }
/* the wire between two stages */
.stage + .stage::before {
  content: ""; position: absolute; left: -23px; top: 50%; width: 22px; height: 0;
  border-top: var(--line) solid var(--pencil);
}
.stage + .stage::after {
  content: ""; position: absolute; left: -7px; top: calc(50% - 4px);
  border: 4px solid transparent; border-left: 6px solid var(--pencil); border-right: 0;
}
.stage-name { font-size: 14px; color: var(--ink-2); }
.stage-body { font: 12.5px/1.5 var(--mono); color: var(--ink); overflow-wrap: anywhere; }
.stage-body .k { color: var(--ink-2); }
.stage-body .v { color: var(--blue); font-weight: 500; }
.stage-body .bytes { color: var(--ink-2); font-size: 11.5px; }
.stage.filled { border-style: solid; border-color: var(--ink); }
.stage.lit { border-color: var(--blue); background: var(--blue-wash); }
.stage.lit .stage-name { color: var(--blue); }
.stage.lit + .stage::before, .stage.lit + .stage::after { border-top-color: var(--blue); border-left-color: var(--blue); }
.stage.halt { border-color: var(--ink); background: repeating-linear-gradient(-45deg, transparent 0 6px, var(--grid) 6px 7px); }
.stage.fault { border-color: var(--red); background: var(--red-wash); }

.next-line { margin: 4px 14px 14px; color: var(--ink-2); font-size: 14px; }
.next-line code { color: var(--ink); font-size: 13px; }

/* ---------------------------------------------------------------- registers */
.regs { display: grid; grid-template-columns: repeat(4, minmax(0, 1fr)); gap: 8px; padding: 14px; flex: 1; align-content: start; }
.reg {
  border: 1px solid var(--faint); border-radius: 4px; padding: 8px 8px 9px;
  display: flex; flex-direction: column; gap: 2px; min-width: 0;
  transition: background-color .4s;
}
.reg-name { font-size: 13px; color: var(--ink-2); }
.reg-val { font: 500 13px var(--mono); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.reg-val .lz { color: var(--faint); }
.reg-alt { font: 11px var(--mono); color: var(--pencil); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.reg.zero .reg-val { color: var(--pencil); }
.reg.wrote .lz { color: color-mix(in srgb, var(--ink) 35%, transparent); }
.reg.wrote .reg-alt { color: var(--ink-2); }
.reg.read { border-color: var(--blue); box-shadow: inset 0 0 0 1px var(--blue); }
.reg.wrote { background: var(--marker); border-color: var(--ink); }
.reg.wrote .reg-val { color: var(--ink); }

.special { display: flex; flex-wrap: wrap; align-items: center; gap: 10px 16px; padding: 0 14px 14px; }
.sreg { display: flex; align-items: baseline; gap: 8px; }
.sreg-name { font-size: 13px; color: var(--ink-2); }
.sreg-val { font: 500 13px var(--mono); }
.flags { display: flex; gap: 6px; margin-left: auto; }
.flag {
  width: 30px; height: 30px; display: grid; place-items: center; border-radius: 50%;
  border: var(--line) dashed var(--pencil); color: var(--pencil); font: 600 13px var(--mono); cursor: help;
}
.flag.on { border-style: solid; border-color: var(--ink); background: var(--ink); color: var(--sheet); }
.flag.changed { box-shadow: 0 0 0 3px var(--marker); }

.seg { display: inline-flex; border: 1px solid var(--pencil); border-radius: 4px; overflow: hidden; }
.seg-btn { border: 0; background: none; padding: 3px 9px; font-size: 13px; cursor: pointer; color: var(--ink-2); }
.seg-btn + .seg-btn { border-left: 1px solid var(--pencil); }
.seg-btn.is-on { background: var(--ink); color: var(--sheet); }

/* ---------------------------------------------------------------- memory */
.check { display: flex; align-items: center; gap: 6px; font-size: 14px; color: var(--ink-2); cursor: pointer; }
.check input { accent-color: var(--blue); }
.memmap { display: flex; gap: 3px; height: 26px; margin: 14px 14px 4px; }
.mm-seg {
  position: relative; flex-basis: 0; min-width: 64px; padding: 0; cursor: pointer; overflow: hidden;
  border: var(--line) solid var(--ink); border-radius: 3px; background: var(--sheet);
}
.mm-seg:hover { background: var(--blue-wash); }
.mm-seg.is-on { box-shadow: 0 0 0 2px var(--blue); }
.mm-stack { background: repeating-linear-gradient(-45deg, transparent 0 4px, var(--grid) 4px 5px); }
.mm-fill { position: absolute; left: 0; top: 0; bottom: 0; width: 0; background: var(--ink-2); opacity: .35; }
.mm-pc { position: absolute; top: -2px; bottom: -2px; width: 2px; background: var(--blue); left: 0; }
.memmap-labels { display: flex; gap: 3px; margin: 0 14px; font-size: 12.5px; color: var(--ink-2); }
.memmap-labels span { flex-basis: 0; min-width: 64px; white-space: nowrap; }

.memnav { display: flex; align-items: center; gap: 8px; padding: 12px 14px 8px; }
.goto { display: flex; align-items: center; gap: 6px; font-size: 14px; color: var(--ink-2); margin: 0 auto; }
.goto input {
  width: 92px; padding: 3px 8px; border: 1px solid var(--pencil); border-radius: 4px;
  background: var(--paper); font: 13px var(--mono); color: var(--ink);
}

.hexdump { padding: 0 14px 14px; font: 12px/1.65 var(--mono); overflow-x: auto; }
.hx-row { display: flex; gap: 14px; white-space: nowrap; }
.hx-addr { color: var(--pencil); width: 4.5ch; flex: none; }
.hx-bytes { display: flex; }
.hx-b { width: 2.6ch; text-align: center; border-radius: 2px; }
.hx-b:nth-child(4n) { margin-right: .9ch; }
.hx-b.z { color: var(--faint); }
.hx-b.code { color: var(--ink); }
.hx-b.data { color: #8a5a00; }
.hx-b.cur { background: var(--blue-wash); color: var(--blue); box-shadow: inset 0 -2px 0 var(--blue); }
.hx-b.op { font-weight: 600; }
.hx-ascii { color: var(--ink-2); letter-spacing: .05em; }
@media (prefers-color-scheme: dark) { :root:not([data-theme="light"]) .hx-b.data { color: #e8b75a; } }

.machine.is-stale > .panel { opacity: .5; transition: opacity .2s; }

/* ---------------------------------------------------------------- tabs */
.tabs { display: flex; gap: 2px; padding: 8px 10px 0; border-bottom: 1px dashed var(--pencil); }
.tab {
  border: 0; background: none; padding: 6px 12px 8px; cursor: pointer; font-size: 15px; font-weight: 500;
  color: var(--ink-2); border-bottom: 2px solid transparent; margin-bottom: -1px;
}
.tab.is-on { color: var(--ink); border-bottom-color: var(--ink); }
.tabpane { padding: 8px 4px 12px; max-height: 300px; overflow: auto; }
.tabpane[hidden] { display: none; }

table.grid { width: 100%; border-collapse: collapse; font: 12.5px/1.6 var(--mono); }
table.grid th { text-align: left; font: 500 13px var(--sans); color: var(--ink-2); padding: 4px 10px; border-bottom: 1px solid var(--faint); }
table.grid td { padding: 3px 10px; vertical-align: top; white-space: nowrap; }
table.grid tr.cur td { background: var(--blue-wash); }
table.grid tr.cur td:first-child { box-shadow: inset 3px 0 0 var(--blue); }
table.grid tr.done td { color: var(--ink-2); }
table.grid td.muted { color: var(--pencil); }
table.grid td.wide { white-space: normal; width: 100%; }
table.grid tbody tr { cursor: default; }
table.grid tbody tr:hover td { background: color-mix(in srgb, var(--blue-wash) 60%, transparent); }

.empty {
  margin: 12px; padding: 18px; border: var(--line) dashed var(--faint); border-radius: var(--r);
  color: var(--ink-2); font-size: 14px; text-align: center;
}

/* ---------------------------------------------------------------- loading / fatal */
.fatal { margin: 40px auto; max-width: 560px; padding: 20px; background: var(--sheet); border: var(--line) solid var(--red); border-radius: var(--r); }
.fatal code { background: var(--paper); padding: 2px 6px; border-radius: 3px; }

/* ---------------------------------------------------------------- responsive */
@media (max-width: 1180px) {
  .machine { grid-template-columns: minmax(0, 1fr); grid-template-areas: "datapath" "regs" "mem" "listing"; }
}
@media (max-width: 900px) {
  .workbench { grid-template-columns: minmax(0, 1fr); padding: 12px 16px 20px; }
  .panel-source { position: static; height: 420px; min-height: 0; }
  .pipeline { grid-template-columns: minmax(0, 1fr); gap: 18px; }
  .stage { min-height: 0; display: grid; grid-template-columns: 84px minmax(0, 1fr); align-items: baseline; }
  .stage + .stage::before { left: 50%; top: -19px; width: 0; height: 18px; border-top: 0; border-left: var(--line) solid var(--pencil); }
  .stage + .stage::after { left: calc(50% - 4px); top: -7px; border: 4px solid transparent; border-top: 6px solid var(--pencil); border-bottom: 0; }
  .stage.lit + .stage::before { border-left-color: var(--blue); }
  .stage.lit + .stage::after { border-top-color: var(--blue); }
  .brand-sub { display: none; }
}
@media (max-width: 520px) {
  .topbar { padding: 12px 16px; }
  .regs { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .speed input { width: 70px; }
}

@media (prefers-reduced-motion: reduce) {
  *, *::before, *::after { transition: none !important; animation: none !important; }
}
