:root {
  --bg: #121418;
  --panel: #1a1d23;
  --panel-2: #20242c;
  --line: #2b313b;
  --text: #d8dce3;
  --dim: #8b94a3;
  --faint: #5b6370;
  --blue: #7aa2f7;
  --cyan: #7dcfff;
  --green: #9ece6a;
  --yellow: #e0af68;
  --orange: #ff9e64;
  --red: #f7768e;
  --purple: #bb9af7;
  --pink: #e48fc4;
  --changed: rgba(224, 175, 104, 0.28);
  --mono: ui-monospace, "SF Mono", "JetBrains Mono", Menlo, Consolas, monospace;
  --sans: -apple-system, BlinkMacSystemFont, "Segoe UI", system-ui, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; height: 100%; }
body {
  background: var(--bg);
  color: var(--text);
  font: 13px/1.4 var(--sans);
  display: grid;
  grid-template-rows: auto 1fr auto;
  overflow: hidden;
}
button, select, input { font: inherit; color: inherit; }
button {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 5px;
  padding: 4px 10px;
  cursor: pointer;
  white-space: nowrap;
}
button:hover { border-color: var(--blue); }
button, select, label, a { touch-action: manipulation; }
button:active { transform: translateY(1px); }
button.primary { background: #2d4a7c; border-color: #3d5f99; min-width: 70px; }
button.on { background: #3a3150; border-color: var(--purple); }
button:disabled { opacity: 0.4; cursor: default; }
select, input[type=text], input[type=number] {
  background: var(--panel-2);
  border: 1px solid var(--line);
  border-radius: 5px;
  padding: 3px 6px;
}
input[type=text] { font-family: var(--mono); }
label.check { display: inline-flex; align-items: center; gap: 4px; cursor: pointer; color: var(--dim); }
.mono { font-family: var(--mono); }
.dim { color: var(--dim); }
.faint { color: var(--faint); }
.small { font-size: 11px; }
.nowrap { white-space: nowrap; }

/* ---------- toolbar ---------- */
#toolbar {
  display: flex;
  align-items: center;
  gap: 14px;
  padding: 7px 12px;
  border-bottom: 1px solid var(--line);
  background: #16191e;
  flex-wrap: wrap;
}
.brand { font-weight: 700; font-size: 16px; letter-spacing: 0.5px; color: var(--green); }
.brand span { color: var(--text); font-weight: 400; margin-left: 2px; }
#toolbar .group { display: flex; gap: 5px; align-items: center; }
.status { margin-left: auto; color: var(--dim); font-family: var(--mono); font-size: 12px; text-align: right; }
.status b { color: var(--yellow); font-weight: 600; }

/* ---------- layout ---------- */
#layout {
  display: grid;
  grid-template-columns: 500px minmax(420px, 1fr) minmax(500px, 1.15fr);
  gap: 8px;
  padding: 8px;
  min-height: 0;
  overflow: hidden;
}
#layout > section { display: flex; flex-direction: column; gap: 8px; min-height: 0; min-width: 0; overflow-y: auto; }
#col-right { overflow: hidden !important; }
.panel {
  background: var(--panel);
  border: 1px solid var(--line);
  border-radius: 8px;
  padding: 8px 10px;
  min-width: 0;
}
.panel.grow { flex: 1; min-height: 0; overflow: auto; }
.panel h2 {
  margin: 0 0 6px;
  font-size: 11px;
  text-transform: uppercase;
  letter-spacing: 0.08em;
  color: var(--dim);
  font-weight: 600;
  display: flex;
  align-items: center;
  gap: 8px;
}
.panel h2 .right { margin-left: auto; display: flex; gap: 6px; align-items: center; text-transform: none; letter-spacing: 0; font-weight: 400; }
.panel h3 { margin: 12px 0 6px; font-size: 12px; color: var(--text); font-weight: 600; }
.panel h3:first-child { margin-top: 0; }
p.lead { margin: 0 0 8px; color: var(--dim); }

@media (max-width: 1500px) {
  body { overflow: auto; }
  #layout { grid-template-columns: 500px minmax(420px, 1fr); overflow: visible; }
  #col-right { grid-column: 1 / -1; height: 80vh; }
}

/* ---------- tabs ---------- */
#tabs { display: flex; flex-wrap: wrap; gap: 3px; }
#tabs button { border-radius: 6px 6px 0 0; border-bottom: none; padding: 5px 10px; color: var(--dim); background: var(--panel); }
#tabs button.active { color: var(--text); background: var(--panel-2); border-color: var(--blue); }
#col-right { gap: 0 !important; }
#tab-body { border-top-left-radius: 0; }

/* ---------- help bar ---------- */
#helpbar {
  border-top: 1px solid var(--line);
  background: #16191e;
  padding: 7px 14px;
  min-height: 44px;
  font-size: 13px;
  color: var(--text);
  display: flex;
  gap: 10px;
  align-items: baseline;
}
#helpbar .help-title { color: var(--yellow); font-weight: 600; font-family: var(--mono); white-space: nowrap; }
#helpbar .help-text { color: var(--dim); }
#drop-hint {
  position: fixed; inset: 0; display: none; align-items: center; justify-content: center;
  background: rgba(18, 20, 24, 0.85); font-size: 24px; color: var(--green); border: 3px dashed var(--green);
}
body.dragging #drop-hint { display: flex; }

/* ---------- screen ---------- */
#screen-panel { padding: 8px; }
.screen-area { display: flex; justify-content: center; }
.screen-wrap { position: relative; width: min(480px, 100%); aspect-ratio: 10 / 9; margin: 0 auto; grid-area: screen; }
.screen-wrap canvas { position: absolute; inset: 0; width: 100%; height: 100%; image-rendering: pixelated; border-radius: 3px; touch-action: manipulation; }
.screen-wrap canvas.overlay { pointer-events: none; }
.screen-controls { display: flex; flex-wrap: wrap; gap: 4px 10px; margin-top: 6px; align-items: center; }
.screen-controls select { padding: 1px 4px; }
.pixel-info { margin-top: 6px; font-family: var(--mono); font-size: 12px; min-height: 36px; color: var(--dim); }
.pixel-info b { color: var(--text); font-weight: 500; }
.joypad-hint { font-size: 11px; color: var(--faint); margin-top: 4px; }
.kbd { font-family: var(--mono); background: var(--panel-2); border: 1px solid var(--line); border-radius: 3px; padding: 0 4px; font-size: 11px; color: var(--dim); }

/* ---------- registers ---------- */
.regs { display: grid; grid-template-columns: repeat(2, 1fr); gap: 4px 10px; font-family: var(--mono); }
.reg { display: grid; grid-template-columns: 26px 44px 34px 1fr; align-items: center; gap: 4px; padding: 1px 4px; border-radius: 4px; }
.reg .name { color: var(--yellow); font-weight: 600; }
.reg .hex { color: var(--text); }
.reg .dec { color: var(--faint); font-size: 11px; text-align: right; }
.reg .bin { color: var(--dim); font-size: 11px; letter-spacing: 0.5px; }
.reg .bin .one { color: var(--cyan); }
.reg.changed, .changed { background: var(--changed); }
.reg16 { display: flex; gap: 6px; font-family: var(--mono); margin-top: 6px; flex-wrap: wrap; }
.reg16 span { background: var(--panel-2); border-radius: 4px; padding: 1px 6px; }
.reg16 span b { color: var(--yellow); font-weight: 600; margin-right: 4px; }
.flags { display: flex; gap: 6px; margin-top: 8px; flex-wrap: wrap; }
.flag { font-family: var(--mono); border: 1px solid var(--line); border-radius: 4px; padding: 1px 7px; color: var(--faint); }
.flag.set { color: #10131a; background: var(--green); border-color: var(--green); font-weight: 600; }
.flag.changed { outline: 2px solid var(--yellow); }
.cpu-state { display: flex; gap: 6px; margin-top: 8px; flex-wrap: wrap; font-family: var(--mono); font-size: 12px; }
.pill { border-radius: 10px; padding: 0 8px; background: var(--panel-2); border: 1px solid var(--line); color: var(--dim); }
.pill.on { color: var(--green); border-color: #4d6b38; }
.pill.warn { color: var(--yellow); border-color: #7a6235; }
.pill.bad { color: var(--red); border-color: #7a3545; }

/* ---------- timeline ---------- */
.timeline { display: flex; gap: 10px; align-items: flex-start; }
.timeline canvas { image-rendering: pixelated; border: 1px solid var(--line); cursor: crosshair; }
.timeline .legend { font-size: 11px; line-height: 1.6; }
.swatch { display: inline-block; width: 10px; height: 10px; border-radius: 2px; vertical-align: -1px; margin-right: 4px; }
.counters { display: grid; grid-template-columns: auto 1fr; gap: 0 10px; font-family: var(--mono); font-size: 12px; margin-top: 6px; }
.counters dt { color: var(--dim); }
.counters dd { margin: 0; }

/* ---------- code ---------- */
#code-panel { display: flex; flex-direction: column; }
.code-view { flex: 1; overflow: hidden; font-family: var(--mono); font-size: 12.5px; min-height: 200px; position: relative; }
.code-row { display: grid; grid-template-columns: 18px 64px 74px 1fr; gap: 6px; padding: 0 4px; height: 20px; align-items: center; white-space: nowrap; border-radius: 3px; }
.code-row.label-row { grid-template-columns: 18px 1fr; color: var(--orange); height: 18px; font-size: 12px; }
.code-row:hover { background: #222731; }
.code-row.past { opacity: 0.55; }
.code-row.current { background: #263452; box-shadow: inset 3px 0 0 var(--blue); opacity: 1; }
.code-row.divider { height: 14px; color: var(--faint); font-size: 10px; grid-template-columns: 1fr; text-transform: uppercase; letter-spacing: 0.1em; padding-left: 28px; }
.code-row .gutter { cursor: pointer; text-align: center; color: transparent; }
.code-row .gutter:hover { color: #6b3a45; }
.code-row .gutter.bp { color: var(--red); }
.code-row .addr { color: var(--faint); }
.code-row .bytes { color: var(--faint); font-size: 11px; }
.code-row .ins { overflow: hidden; text-overflow: ellipsis; }
.code-row .comment { color: var(--faint); margin-left: 10px; }
.code-row .comment.taken { color: var(--green); }
.code-row .comment.not-taken { color: var(--dim); }
.code-row .irq { color: var(--pink); }
.tk-mn { color: var(--blue); font-weight: 600; }
.tk-reg { color: var(--yellow); }
.tk-imm, .tk-imm16 { color: var(--green); }
.tk-addr { color: var(--cyan); cursor: pointer; }
.tk-addr:hover, .tk-imm16:hover { text-decoration: underline; }
.tk-cond { color: var(--purple); }
.tk-num { color: var(--green); }
.tk-punc, .tk-sep { color: var(--dim); }
.code-toolbar { display: flex; gap: 6px; align-items: center; margin-bottom: 6px; flex-wrap: wrap; }
.code-toolbar input { width: 120px; }
.seg { display: inline-flex; border: 1px solid var(--line); border-radius: 5px; overflow: hidden; }
.seg button { border: none; border-radius: 0; padding: 3px 9px; background: var(--panel); color: var(--dim); }
.seg button.active { background: var(--panel-2); color: var(--text); box-shadow: inset 0 -2px 0 var(--blue); }
.source-view { flex: 1; overflow: auto; font-family: var(--mono); font-size: 12px; white-space: pre; }
.source-line { display: grid; grid-template-columns: 40px 1fr; padding: 0 4px; border-radius: 3px; }
.source-line .ln { color: var(--faint); text-align: right; padding-right: 10px; user-select: none; }
.source-line.current { background: #263452; box-shadow: inset 3px 0 0 var(--blue); }
.source-line.recent { background: #1f2533; }
.src-comment { color: #6f7a8c; font-style: italic; }
.src-label { color: var(--orange); }
.src-directive { color: var(--purple); }
.src-string { color: var(--green); }

/* ---------- explain ---------- */
#explain-panel { max-height: 48vh; overflow: auto; flex-shrink: 0; }
.ex-head { display: flex; align-items: baseline; gap: 10px; flex-wrap: wrap; }
.ex-ins { font-family: var(--mono); font-size: 17px; }
.group-tag { font-size: 10px; text-transform: uppercase; letter-spacing: 0.06em; padding: 1px 6px; border-radius: 8px; background: var(--panel-2); color: var(--dim); border: 1px solid var(--line); }
.ex-desc { margin: 6px 0; color: var(--text); max-width: 70ch; }
.ex-bytes { display: flex; gap: 6px; flex-wrap: wrap; margin: 6px 0; }
.byte-box { font-family: var(--mono); border: 1px solid var(--line); border-radius: 4px; padding: 2px 6px; text-align: center; background: var(--panel-2); }
.byte-box small { display: block; color: var(--dim); font-size: 10px; font-family: var(--sans); }
.ex-grid { display: grid; grid-template-columns: auto 1fr; gap: 3px 12px; font-size: 12px; margin-top: 6px; }
.ex-grid dt { color: var(--dim); }
.ex-grid dd { margin: 0; font-family: var(--mono); }
.effects { list-style: none; padding: 0; margin: 4px 0 0; font-family: var(--mono); font-size: 12px; }
.effects li { padding: 1px 0; }
.effects .arrow { color: var(--faint); margin: 0 4px; }
.effects .k { color: var(--yellow); display: inline-block; min-width: 26px; }
.effects .rw { display: inline-block; width: 44px; color: var(--dim); }
.effects .w { color: var(--red); }
.effects .r { color: var(--green); }
.flag-table { display: inline-grid; grid-template-columns: repeat(4, auto); gap: 0 4px; font-family: var(--mono); font-size: 11px; }
.flag-table span { border: 1px solid var(--line); border-radius: 3px; padding: 0 5px; }
.flag-table span.affected { color: var(--yellow); border-color: #7a6235; }
.callout { border-left: 3px solid var(--purple); background: #211f2b; padding: 6px 10px; border-radius: 0 6px 6px 0; margin: 8px 0; font-size: 12.5px; }
.callout.irq { border-color: var(--pink); background: #2a1f28; }
.callout.halt { border-color: var(--yellow); background: #2a2519; }

/* ---------- stack ---------- */
.stack-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px; }
.stack-list { font-family: var(--mono); font-size: 12px; }
.stack-list div { display: flex; gap: 8px; padding: 0 4px; border-radius: 3px; }
.stack-list .sp { background: #263452; }
.stack-list .ret { color: var(--cyan); }
.callstack div { font-family: var(--mono); font-size: 12px; padding: 0 4px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.callstack .irq { color: var(--pink); }

/* ---------- memory ---------- */
.mem-toolbar { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; margin-bottom: 6px; }
.mem-toolbar input[type=text] { width: 140px; }
.region-bar { display: flex; height: 22px; border-radius: 4px; overflow: hidden; margin-bottom: 6px; font-size: 10px; cursor: pointer; position: relative; }
.region-bar div { display: flex; align-items: center; justify-content: center; color: rgba(255,255,255,0.85); overflow: hidden; white-space: nowrap; border-right: 1px solid rgba(0,0,0,0.3); }
.region-bar .cursor { position: absolute; top: 0; bottom: 0; border: 2px solid white; pointer-events: none; min-width: 3px; }
.hex-view { font-family: var(--mono); font-size: 12px; position: relative; overflow-y: scroll; height: calc(100% - 120px); min-height: 300px; }
.hex-row { display: flex; height: 18px; align-items: center; white-space: nowrap; }
.hex-row .ra { width: 60px; color: var(--faint); }
.hex-row .rg { width: 6px; height: 14px; margin-right: 6px; border-radius: 2px; }
.hex-row .b { width: 22px; text-align: center; cursor: pointer; border-radius: 2px; }
.hex-row .b:nth-child(10) { margin-right: 8px; }
.hex-row .b:hover { outline: 1px solid var(--blue); }
.hex-row .b.zero { color: var(--faint); }
.hex-row .b.chg { background: var(--changed); color: var(--yellow); }
.hex-row .b.sel { outline: 2px solid var(--blue); }
.hex-row .b.pc { box-shadow: inset 0 -2px 0 var(--blue); }
.hex-row .b.spm { box-shadow: inset 0 -2px 0 var(--purple); }
.hex-row .b.hl { box-shadow: inset 0 -2px 0 var(--orange); }
.hex-row .asc { margin-left: 10px; color: var(--dim); letter-spacing: 1px; }
.hex-row .tag { margin-left: 8px; color: var(--yellow); font-size: 11px; }
.mem-legend { display: flex; gap: 12px; flex-wrap: wrap; font-size: 11px; color: var(--dim); margin-top: 4px; }
.mem-legend i { display: inline-block; width: 14px; height: 3px; vertical-align: middle; margin-right: 3px; }
.sel-info { font-family: var(--mono); font-size: 12px; margin-top: 6px; color: var(--dim); min-height: 18px; }

/* ---------- heatmap ---------- */
.heat-wrap { display: flex; gap: 10px; align-items: flex-start; }
.heat-wrap canvas { image-rendering: pixelated; border: 1px solid var(--line); cursor: crosshair; }
.heat-labels { font-family: var(--mono); font-size: 11px; position: relative; height: 448px; width: 150px; }
.heat-labels div { position: absolute; left: 0; white-space: nowrap; }

/* ---------- video ---------- */
.subtabs { display: flex; gap: 4px; margin-bottom: 8px; flex-wrap: wrap; }
.vid-grid { display: grid; grid-template-columns: auto 1fr; gap: 10px 14px; align-items: start; }
canvas.pix { image-rendering: pixelated; border: 1px solid var(--line); background: #000; }
.kv { display: grid; grid-template-columns: auto 1fr; gap: 2px 10px; font-size: 12px; }
.kv dt { color: var(--dim); }
.kv dd { margin: 0; font-family: var(--mono); }
.bits { display: grid; grid-template-columns: 46px 1fr auto; gap: 1px 8px; font-size: 12px; align-items: center; }
.bits .bitno { font-family: var(--mono); color: var(--faint); white-space: nowrap; font-size: 11px; }
.bits .val { font-family: var(--mono); text-align: right; }
.bits .val.one { color: var(--green); }
.reg-card { background: var(--panel-2); border: 1px solid var(--line); border-radius: 6px; padding: 6px 8px; margin-bottom: 8px; }
.reg-card .head { display: flex; gap: 8px; align-items: baseline; margin-bottom: 4px; }
.reg-card .head b { color: var(--yellow); font-family: var(--mono); }
.reg-card .head .v { margin-left: auto; font-family: var(--mono); }
.reg-card .help { font-size: 11.5px; color: var(--dim); margin-bottom: 4px; }
.cards { columns: 2 300px; column-gap: 10px; }
.cards .reg-card { break-inside: avoid; }
.tile-detail { display: flex; gap: 12px; flex-wrap: wrap; align-items: flex-start; }
.tile-bytes { font-family: var(--mono); font-size: 11.5px; border-collapse: collapse; }
.tile-bytes td { padding: 0 4px; }
.tile-bytes td.px { width: 14px; height: 14px; padding: 0; text-align: center; font-size: 10px; color: rgba(127,127,127,0.9); }
.oam-table { width: 100%; border-collapse: collapse; font-family: var(--mono); font-size: 11.5px; }
.oam-table th { text-align: left; color: var(--dim); font-weight: 500; position: sticky; top: 0; background: var(--panel); }
.oam-table td, .oam-table th { padding: 1px 6px; }
.oam-table tr.hidden-spr td { color: var(--faint); }
.oam-table tr.on-line td:first-child { box-shadow: inset 3px 0 0 var(--green); }
.oam-table tr:hover { background: #222731; }
.oam-table canvas { image-rendering: pixelated; vertical-align: middle; background: repeating-conic-gradient(#2a2f38 0 25%, #1d2128 0 50%) 0 0/6px 6px; }
.palette-row { display: flex; gap: 4px; align-items: center; margin: 3px 0; font-size: 12px; }
.palette-row .sw { width: 34px; height: 22px; border-radius: 3px; border: 1px solid var(--line); display: flex; align-items: flex-end; justify-content: center; font-size: 10px; font-family: var(--mono); }

/* ---------- sound ---------- */
.channels { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; }
.channel { background: var(--panel-2); border: 1px solid var(--line); border-radius: 6px; padding: 6px 8px; }
.channel.off { opacity: 0.55; }
.channel .head { display: flex; gap: 6px; align-items: center; margin-bottom: 4px; }
.channel .head b { font-size: 12px; }
.channel .head .note { margin-left: auto; font-family: var(--mono); color: var(--cyan); }
.channel canvas { width: 100%; height: 60px; background: #111317; border-radius: 4px; display: block; }
.vol { height: 6px; background: #111317; border-radius: 3px; overflow: hidden; margin: 4px 0; }
.vol div { height: 100%; background: var(--green); }

/* ---------- tables, misc ---------- */
table.data { border-collapse: collapse; width: 100%; font-size: 12px; }
table.data th { text-align: left; color: var(--dim); font-weight: 500; border-bottom: 1px solid var(--line); padding: 3px 6px; }
table.data td { padding: 3px 6px; border-bottom: 1px solid #22262e; vertical-align: top; }
table.data td.mono { font-family: var(--mono); }
.ok { color: var(--green); }
.bad { color: var(--red); }
.warn { color: var(--yellow); }
.trace-view { font-family: var(--mono); font-size: 12px; height: calc(100% - 60px); min-height: 300px; overflow-y: scroll; position: relative; }
.trace-row { display: grid; grid-template-columns: 70px 34px 80px 170px 1fr; gap: 6px; height: 18px; align-items: center; white-space: nowrap; padding: 0 4px; cursor: pointer; }
.trace-row:hover { background: #222731; }
.trace-row.irq { color: var(--pink); }
.trace-row .regs-t { color: var(--dim); font-size: 11px; overflow: hidden; }
.bp-list div { display: flex; gap: 8px; align-items: center; font-family: var(--mono); padding: 2px 0; }
.form-row { display: flex; gap: 6px; align-items: center; flex-wrap: wrap; margin: 6px 0; }
.serial-out { font-family: var(--mono); white-space: pre-wrap; background: #111317; border-radius: 4px; padding: 6px; min-height: 40px; max-height: 160px; overflow: auto; }
.joy { display: flex; gap: 6px; flex-wrap: wrap; }
.joy span { font-family: var(--mono); border: 1px solid var(--line); border-radius: 4px; padding: 1px 7px; color: var(--faint); cursor: pointer; user-select: none; }
.joy span.down { background: var(--green); color: #10131a; }
.irq-table td.st { font-weight: 600; }
.progress { height: 8px; background: #111317; border-radius: 4px; overflow: hidden; margin: 3px 0; }
.progress div { height: 100%; background: var(--blue); }

/* ---------- learn ---------- */
.diagram { display: grid; grid-template-columns: repeat(4, 1fr); gap: 8px; margin: 8px 0 14px; }
.block { border: 1px solid var(--line); border-radius: 8px; padding: 8px; background: var(--panel-2); cursor: pointer; }
.block:hover { border-color: var(--blue); }
.block b { display: block; margin-bottom: 2px; }
.block .live { font-family: var(--mono); font-size: 11px; color: var(--cyan); margin-top: 4px; white-space: pre-line; }
.block.cpu { grid-column: span 2; border-color: #3d5f99; }
.block.bus { grid-column: 1 / -1; text-align: center; background: #1d2230; border-style: dashed; }
.lesson { border: 1px solid var(--line); border-radius: 8px; padding: 10px 12px; margin-bottom: 10px; background: var(--panel-2); }
.lesson h4 { margin: 0 0 4px; font-size: 13px; color: var(--green); }
.lesson p { margin: 4px 0; color: var(--dim); max-width: 80ch; }
.lesson .actions { display: flex; gap: 6px; flex-wrap: wrap; margin-top: 6px; }
.opref { display: grid; min-width: 600px; grid-template-columns: repeat(16, 1fr); gap: 1px; font-family: var(--mono); font-size: 9.5px; }
.opref div { padding: 2px; border-radius: 2px; text-align: center; cursor: default; min-height: 32px; display: flex; align-items: center; justify-content: center; line-height: 1.15; }
.opref div:hover { outline: 1px solid var(--text); }
.g-load { background: #24324d; } .g-load16 { background: #1f3a4a; } .g-alu { background: #2e3d24; } .g-alu16 { background: #34401f; }
.g-bit { background: #3a2f45; } .g-jump { background: #4a2a30; } .g-stack { background: #44381f; } .g-control { background: #2f2f2f; } .g-illegal { background: #191919; color: var(--faint); }

/* ---------- canvases that scale down to fit ---------- */
canvas.pix, .timeline canvas { max-width: 100%; height: auto; }

/* ---------- touch gamepad ---------- */
.gp-left, .gp-right, .gp-mid { display: none; }
body.show-pad .screen-area {
  display: grid;
  grid-template-columns: 1fr 1fr;
  grid-template-areas: "screen screen" "left right" "mid mid";
  align-items: center;
  gap: 12px 8px;
}
body.show-pad .gp-left { display: flex; grid-area: left; justify-content: center; }
body.show-pad .gp-right { display: flex; grid-area: right; justify-content: center; gap: 14px; align-items: center; }
body.show-pad .gp-mid { display: flex; grid-area: mid; justify-content: center; gap: 22px; margin: 0 0 6px; }
.gp-left, .gp-right, .gp-mid { user-select: none; -webkit-user-select: none; -webkit-touch-callout: none; }
.gp-dpad { position: relative; width: 138px; height: 138px; touch-action: none; }
.gp-arm, .gp-hub { position: absolute; background: #2b313b; border: 1px solid #3a414d; }
.gp-d-up { left: 46px; top: 0; width: 46px; height: 50px; border-radius: 8px 8px 0 0; border-bottom: none; }
.gp-d-down { left: 46px; bottom: 0; width: 46px; height: 50px; border-radius: 0 0 8px 8px; border-top: none; }
.gp-d-left { left: 0; top: 46px; width: 50px; height: 46px; border-radius: 8px 0 0 8px; border-right: none; }
.gp-d-right { right: 0; top: 46px; width: 50px; height: 46px; border-radius: 0 8px 8px 0; border-left: none; }
.gp-hub { left: 46px; top: 46px; width: 46px; height: 46px; border: none; }
.gp-arm.down { background: var(--blue); }
.gp-btn:focus { outline: none; }
.gp-btn { touch-action: none; user-select: none; -webkit-user-select: none; font-weight: 700; color: var(--dim); }
.gp-round { width: 62px; height: 62px; border-radius: 50%; background: #3a2f45; border-color: #57466a; font-size: 18px; }
.gp-a { transform: translateY(-16px); }
.gp-b { transform: translateY(14px); }
.gp-pill { padding: 6px 16px; border-radius: 14px; font-size: 11px; letter-spacing: 0.08em; }
.gp-btn.down { background: var(--purple); color: #10131a; }
.gp-round.down { transform: translateY(-16px) scale(0.94); }
.gp-b.down { transform: translateY(14px) scale(0.94); }

/* ---------- phones and small tablets ---------- */
@media (max-width: 900px), (max-height: 520px) {
  html, body { height: auto; }
  body { display: block; overflow: auto; padding-bottom: calc(58px + env(safe-area-inset-bottom)); }
  #toolbar { gap: 6px 8px; padding: 6px 8px; }
  .brand { font-size: 14px; }
  .status { margin-left: 0; text-align: left; width: 100%; order: 10; font-size: 11px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  .status br { display: none; }
  .status b { margin-left: 8px; }
  /* Run / step controls live in a bar at the bottom, within reach of a thumb. */
  #toolbar .transport {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 20;
    background: #16191e; border-top: 1px solid var(--line);
    padding: 7px 8px calc(7px + env(safe-area-inset-bottom));
    overflow-x: auto; scrollbar-width: none;
  }
  #toolbar .transport button { padding: 8px 12px; flex: 0 0 auto; }
  #layout { display: flex; flex-direction: column; padding: 6px; gap: 6px; overflow: visible; }
  #layout > section { overflow: visible; }
  #col-right { height: 85svh; }
  .code-view { flex: none; height: 380px; min-height: 0; }
  #explain-panel { max-height: none; }
  #tabs { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; }
  #tabs button { flex: 0 0 auto; }
  .joypad-hint { display: none; }
  /* Explanations appear as a card when you tap something; tap it to close. */
  #helpbar {
    position: fixed; left: 8px; right: 8px; bottom: calc(62px + env(safe-area-inset-bottom)); z-index: 30;
    display: none; flex-direction: column; gap: 3px;
    border: 1px solid var(--line); border-radius: 10px; box-shadow: 0 8px 30px rgba(0,0,0,0.6);
    max-height: 45vh; overflow: auto;
  }
  #helpbar.show { display: flex; }
}

/* Landscape phone: gamepad on either side of the screen. */
@media (orientation: landscape) and (max-height: 520px) {
  .brand { display: none; }
  #toolbar { flex-wrap: nowrap; padding: 4px 8px; }
  #toolbar button, #toolbar select { padding: 3px 8px; }
  .status { width: auto; flex: 1; min-width: 0; order: 0; }
  #screen-panel h2 { margin-bottom: 2px; }
  body.show-pad .screen-area { grid-template-columns: auto 1fr auto; grid-template-areas: "left screen right" "mid screen right"; }
  body.show-pad .gp-mid { gap: 10px; align-self: start; }
  .screen-wrap { width: min(100%, calc((100svh - 150px) * 10 / 9)); }
  body.show-pad .gp-right { flex-direction: row; }
}

@media (max-width: 600px) {
  .panel { padding: 8px; }
  .code-row { grid-template-columns: 18px 58px 1fr; }
  .code-row .bytes { display: none; }
  .reg { grid-template-columns: 22px 36px 1fr; }
  .reg .dec { display: none; }
  .stack-grid, .channels, .vid-grid { grid-template-columns: 1fr; }
  .diagram { grid-template-columns: 1fr 1fr; }
  .block.cpu { grid-column: 1 / -1; }
  .hex-row .tag { display: none; }
  .hex-row .ra { width: 46px; }
  .trace-row { grid-template-columns: 30px 74px 1fr; }
  .trace-row > :nth-child(1), .trace-row > :nth-child(5) { display: none; }
  .heat-labels { display: none; }
  .timeline { flex-wrap: wrap; }
  .ex-ins { font-size: 15px; }
  .mem-toolbar input[type=text] { width: 110px; }
}

/* Touch screens have no hover: don't leave buttons highlighted after a tap. */
@media (hover: none) {
  button:hover { border-color: var(--line); }
  button.primary:hover { border-color: #3d5f99; }
}
button:focus:not(:focus-visible) { outline: none; }
