:root {
  --bg: #0b0f19; --panel: #121826; --panel2: #182033; --line: #2a3350; --text: #dbe3f5; --dim: #8a96b5;
  --hi: #34d399; --lo: #3b4458; --accent: #6366f1; --amber: #fbbf24; --red: #f87171;
}
* { box-sizing: border-box; }
html, body { height: 100%; overflow: hidden; }
body { display: flex; flex-direction: column; margin: 0; background: var(--bg); color: var(--text); font: 14px/1.5 system-ui, -apple-system, "Segoe UI", sans-serif; }
header { display: flex; align-items: center; gap: 16px; padding: 10px 18px; border-bottom: 1px solid var(--line); background: var(--panel); }
header h1 { font-size: 18px; margin: 0; }
#crumb { color: var(--dim); flex: 1; font-size: 13px; }
button { background: var(--panel2); color: var(--text); border: 1px solid var(--line); border-radius: 6px; padding: 6px 12px; cursor: pointer; font: inherit; }
button:hover { border-color: var(--accent); }
button.ghost { background: transparent; }
button.accent { background: var(--accent); border-color: var(--accent); color: #fff; }
label { color: var(--dim); display: inline-flex; align-items: center; gap: 5px; font-size: 13px; }
input[type=range] { width: 90px; }

#app { flex: 1; min-height: 0; display: grid; grid-template-columns: 168px minmax(0, 1fr); gap: 0;  }
nav { overflow-y: auto; border-right: 1px solid var(--line); padding: 8px 6px; background: var(--panel); }
nav h4 { margin: 12px 4px 3px; font-size: 10px; letter-spacing: .08em; text-transform: uppercase; color: var(--dim); }
nav h4:first-child { margin-top: 2px; }
nav button { display: block; width: 100%; text-align: left; background: transparent; border: 0; border-radius: 5px; padding: 3px 7px; font-size: 12px; line-height: 1.35; color: var(--text); }
nav button:hover { background: var(--panel2); }
nav button.act { background: var(--accent); color: #fff; }
main { overflow-y: auto; padding: 12px 20px 40px; min-width: 0; }

.titlebar { display: flex; flex-wrap: wrap; align-items: baseline; gap: 14px; }
.titlebar h2 { margin: 0; font-size: 22px; }
code#expr { color: var(--amber); font-size: 14px; }
#toolbar { display: flex; flex-wrap: wrap; align-items: center; gap: 10px; margin: 10px 0; }
#stage { position: relative; background: #070a12; border: 1px solid var(--line); border-radius: 10px; height: 52vh; min-height: 320px; overflow: hidden; }
#svg { width: 100%; height: 100%; display: block; cursor: grab; touch-action: none; }
#svg:active { cursor: grabbing; }
#status { position: absolute; left: 10px; bottom: 6px; font-size: 12px; color: var(--dim); pointer-events: none; text-shadow: 0 0 4px #000; }

/* diagram */
.wire { fill: none; stroke: var(--lo); stroke-width: 2.4; stroke-linejoin: round; transition: stroke .15s; }
.wire.hi { stroke: var(--hi); }
.wire.hl { stroke-width: 4.5; }
.flow { fill: none; stroke: #eafff6; stroke-width: 3; stroke-linecap: round; stroke-dasharray: 2 14; opacity: 0; pointer-events: none; }
.flow.hi { opacity: .95; animation: flow calc(0.9s * var(--spd, 1)) linear infinite; }
@keyframes flow { to { stroke-dashoffset: -16; } }
.pin { fill: var(--lo); } .pin.hi { fill: var(--hi); }
.body, .sbody { fill: #101729; stroke: #8a96b5; stroke-width: 2; transition: fill .15s; }
.body.hi { fill: #0f3d2e; stroke: var(--hi); }
.xtra, .stubl { fill: none; stroke: #8a96b5; stroke-width: 2; }
.bubble { fill: #070a12; stroke: #8a96b5; stroke-width: 2; }
.gl { fill: #55618a; font-size: 10px; font-weight: 600; pointer-events: none; }
.sg { fill: #5b6788; font-size: 10px; font-family: monospace; }
.nm { fill: var(--text); font-size: 13px; font-family: monospace; font-weight: 600; }
.ctag { fill: var(--amber); font-size: 10px; font-family: monospace; }
.gate, .subbox { cursor: pointer; }
.gate:hover .body, .subbox:hover .sbody { stroke: var(--accent); }
.node.sel .body, .node.sel .sbody { stroke: var(--amber); stroke-width: 3; }
.sw { cursor: pointer; }
.swbox { fill: #1b2338; stroke: #4a5578; stroke-width: 2; }
.sw.hi .swbox { fill: #0f3d2e; stroke: var(--hi); }
.knob { fill: #8a96b5; transition: cx .12s; } .knob.hi { fill: var(--hi); }
.swval { fill: #fff; font-size: 11px; font-family: monospace; text-anchor: middle; pointer-events: none; }
.led { fill: #2a1313; stroke: #5a2a2a; stroke-width: 2; transition: fill .15s; }
.led.hi { fill: var(--hi); stroke: #b5ffe0; filter: drop-shadow(0 0 7px var(--hi)); }
.st { fill: var(--text); font-size: 13px; font-weight: 600; pointer-events: none; }
.ss { fill: #55618a; font-size: 9px; pointer-events: none; }
.pl { fill: var(--dim); font-size: 10px; font-family: monospace; pointer-events: none; }
.sbody { fill: #141d35; }

/* cards */
.card { background: var(--panel2); border: 1px solid var(--line); border-radius: 10px; padding: 10px 14px; margin-bottom: 12px; }
.card h3 { margin: 0 0 6px; font-size: 12px; text-transform: uppercase; letter-spacing: .08em; color: var(--dim); }
.grid2 { display: grid; grid-template-columns: minmax(0, 1fr); gap: 12px; margin-top: 12px; }
#theory, #hw, #hwdyn { font-size: 13.5px; }
#hwdyn { margin-top: 10px; border-top: 1px dashed var(--line); padding-top: 8px; }
#tryit { margin: 0; padding-left: 18px; font-size: 13.5px; }
.cmos { width: 100%; background: #0b0f19; border-radius: 8px; border: 1px solid var(--line); margin: 6px 0; }
.small { font-size: 12px; color: var(--dim); }
.note { margin: 4px 0; font-size: 12.5px; }

/* live strip */
#live { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 10px; align-items: center; }
.chip { font-family: monospace; font-size: 12px; padding: 2px 8px; border-radius: 99px; border: 1px solid var(--line); background: var(--panel); color: var(--dim); }
.chip.hi { background: #0f3d2e; border-color: var(--hi); color: #b5ffe0; }
.chip.in { cursor: pointer; }
.chip.in:hover { border-color: var(--accent); }
.sep { color: var(--dim); }

/* tables */
#truth { overflow: visible; }
table { border-collapse: collapse; width: 100%; font-family: monospace; font-size: 13px; }
th, td { padding: 3px 8px; text-align: center; border-bottom: 1px solid var(--line); }
th { position: sticky; top: 0; background: var(--panel2); color: var(--dim); }
th.out, td.out { border-left: 2px solid var(--line); }
tr.cur td { background: #1f2f55; color: #fff; }
tr.pick { cursor: pointer; } tr.pick:hover td { background: #1a2547; }
td.one { color: var(--hi); font-weight: 700; }
#buses { display: grid; gap: 4px; font-family: monospace; font-size: 13px; }
#buses div b { color: var(--amber); }
#widget { margin-top: 8px; }
#timing { width: 100%; background: #070a12; border: 1px solid var(--line); border-radius: 6px; }
#timingWrap { margin-top: 12px; }
#timingWrap h3 { margin-bottom: 4px; }
.ok { color: var(--hi); } .bad { color: var(--red); }
.seg { fill: #1d2335; } .seg.hi { fill: #ff4d4d; filter: drop-shadow(0 0 4px #f00); }
.ram { border-collapse: separate; border-spacing: 4px; width: auto; }
.ram td { border: 1px solid var(--line); border-radius: 4px; width: 34px; height: 30px; background: #0e1424; color: var(--dim); }
.ram td.one { background: #0f3d2e; color: #b5ffe0; border-color: var(--hi); }
.ram tr.cur td { box-shadow: 0 0 0 2px var(--amber); }
.ram th { background: transparent; }
dialog { background: var(--panel); color: var(--text); border: 1px solid var(--line); border-radius: 12px; max-width: 560px; }
dialog::backdrop { background: #000a; }
dialog li { margin-bottom: 6px; }

@media (max-width: 720px) {
  html, body { overflow: auto; height: auto; }
  #app { grid-template-columns: 1fr; height: auto; }
  nav { border-right: 0; border-bottom: 1px solid var(--line); max-height: 150px; }
  main { overflow: visible; }
  .grid2, .desc { grid-template-columns: 1fr; }
}

.grow { flex: 1; }
.desc { display: grid; grid-template-columns: repeat(3, minmax(0, 1fr)); gap: 12px; margin-top: 12px; }
.desc .card { margin: 0; }
#hwCard { margin-top: 12px; }
#hwCard h3 span { color: var(--amber); text-transform: none; letter-spacing: 0; font-size: 13px; }
.hwrow { display: grid; grid-template-columns: minmax(0, 1.4fr) minmax(0, 1fr); gap: 16px; align-items: start; }
.hwin { display: flex; flex-wrap: wrap; gap: 8px; margin-bottom: 8px; }
.tgl { display: inline-flex; align-items: center; gap: 6px; padding: 3px 10px 3px 4px; border-radius: 99px; border: 1px solid var(--line); background: #1b2338; font: 12px monospace; color: var(--text); cursor: pointer; }
.tgl i { width: 26px; height: 14px; border-radius: 7px; background: #3b4458; position: relative; }
.tgl i::after { content: ""; position: absolute; top: 2px; left: 2px; width: 10px; height: 10px; border-radius: 50%; background: #8a96b5; transition: left .12s; }
.tgl.hi { border-color: var(--hi); background: #0f3d2e; }
.tgl.hi i { background: #1c7a5a; } .tgl.hi i::after { left: 14px; background: var(--hi); }
.tgl.lock { opacity: .55; cursor: not-allowed; }
.cf { fill: none; stroke: #fff; stroke-width: 2.2; stroke-linecap: round; stroke-dasharray: 2 11; pointer-events: none; animation: flow calc(0.9s * var(--spd, 1)) linear infinite; }
.cmos { max-height: 300px; margin: 0; }
#status:empty { display: none; }
#status { background: #000a; padding: 1px 8px; border-radius: 4px; }


* { scrollbar-width: thin; scrollbar-color: #2a3350 transparent; }
::-webkit-scrollbar { width: 7px; height: 7px; }
::-webkit-scrollbar-thumb { background: #2a3350; border-radius: 4px; }
::-webkit-scrollbar-track { background: transparent; }

table.tt { width: 100%; border: 1px solid var(--line); border-collapse: collapse; table-layout: fixed; }
table.tt th, table.tt td { border: 1px solid var(--line); padding: 5px 8px; }
table.tt th.grp { font-size: 12px; letter-spacing: .1em; text-transform: uppercase; padding: 6px; }
table.tt th.grp.in, table.tt th.in { background: #1b2a4a; color: #9dbcff; }
table.tt th.grp.out, table.tt th.out { background: #3a2a14; color: var(--amber); }
table.tt td.out.first, table.tt th.out.first { border-left: 3px solid var(--amber); }
table.tt tr.pick td.in { background: #101830; }
table.tt tr.cur td { background: #243a6e; color: #fff; }
table.tt tr.cur td.one { background: #14573f; }
table.tt th { position: static; }

.dim { color: var(--dim); }
.now { border-color: #3b4a7a; background: #16203a; }
#now { font-size: 14.5px; line-height: 1.6; }
#now b.one, #trace b.one { color: var(--hi); }
#now .bad, .bad { color: var(--red); }
details summary { cursor: pointer; color: var(--dim); font-size: 12.5px; margin-top: 8px; }
#trace { font: 12.5px/1.7 monospace; margin-top: 6px; }
#groups { display: flex; flex-wrap: wrap; gap: 6px; margin-top: 8px; }
#groups:empty { display: none; }
.gchip { font-size: 11.5px; padding: 1px 8px 1px 6px; border-radius: 99px; background: var(--panel); border: 1px solid var(--line); color: var(--dim); display: inline-flex; align-items: center; gap: 5px; }
.gchip i { width: 9px; height: 9px; border-radius: 50%; display: inline-block; }
.gchip.lbl { border: 0; background: none; }
#log div { padding: 4px 0; border-bottom: 1px solid var(--line); font-size: 13.5px; }
#log div:first-child { background: #1a2547; padding-left: 6px; border-radius: 4px; }
table.sg { width: 100%; font-family: inherit; font-size: 13px; }
table.sg td, table.sg th { text-align: left; padding: 4px 8px; }
table.sg td.nm { font-family: monospace; color: var(--amber); white-space: nowrap; }
table.sg td.kd { color: var(--dim); font-size: 12px; }
table.sg td.val { text-align: center; font-family: monospace; width: 44px; }
table.sg td.val.hi { color: #b5ffe0; background: #0f3d2e; }
table.tt tr.live td { background: #243a6e; color: #fff; font-weight: 700; }
table.tt tr.live td.one { background: #14573f; }
.mini { width: 100%; max-height: 260px; background: #0b0f19; border: 1px solid var(--line); border-radius: 8px; margin: 6px 0; }
.mini .sw { cursor: default; }
