Skip to main content

logicaffeine_web/ui/components/
debug_drawer.rs

1//! The Studio bottom **debug drawer** — an IDE-style debugger for imperative
2//! (Code-mode) LOGOS, driven by the zero-cost bytecode debugger in
3//! `logicaffeine_compile::debug`. A self-contained, additive panel: it docks below
4//! the editor/output and slides away on Stop, leaving the rest of the Studio
5//! untouched.
6//!
7//! Anatomy (classic IDE Debug panel): a step toolbar (Step Into / Over / Out /
8//! Continue / **Step Back** / Restart / Stop), a status line (state · pc · current
9//! op · step counter), and Variables / Call Stack / Breakpoints / Bytecode tabs.
10//! Breakpoints are set on the bytecode tape (this is a bytecode-level debugger —
11//! you watch the actual VM). Step Back is true time-travel, free from the
12//! debugger's snapshot history.
13
14use dioxus::prelude::*;
15use logicaffeine_compile::debug::{CausalNode, DebugSnapshot, Debugger, ProofVerdict, VarTimeline};
16use logicaffeine_compile::interpreter::InterpreterResult;
17
18#[derive(Clone, Copy, PartialEq)]
19enum DebugTab {
20    Variables,
21    Stack,
22    Heap,
23    Timeline,
24    Prove,
25    CallStack,
26    Breakpoints,
27    Bytecode,
28}
29
30/// The bottom debug drawer. `source` is the Code-mode program; `output` receives what the
31/// program has printed at the cursor, for the Studio's Output column; `on_close` stops the
32/// session (the Studio hides the drawer).
33#[component]
34pub fn DebugDrawer(
35    source: String,
36    height: Signal<f64>,
37    output: Signal<InterpreterResult>,
38    on_close: EventHandler<()>,
39) -> Element {
40    // Dragging the grip on the drawer's top edge paints each frame straight onto the drawer's
41    // height and commits it once, on release (see ui::drag). The panel row above gives way.
42    let start_grip_drag = move |evt: PointerEvent| {
43        #[cfg(target_arch = "wasm32")]
44        {
45            use crate::ui::pages::studio_layout::drawer_dragged;
46            use dioxus::html::input_data::MouseButton;
47            use wasm_bindgen::JsCast;
48            let mut height = height;
49            if evt
50                .data()
51                .trigger_button()
52                .is_some_and(|b| b != MouseButton::Primary)
53            {
54                return;
55            }
56            evt.prevent_default();
57            let Some(document) = web_sys::window().and_then(|w| w.document()) else {
58                return;
59            };
60            let find = |selector: &str| document.query_selector(selector).ok().flatten();
61            let (Some(grip), Some(drawer), Some(panels)) = (
62                find(".dbg-grip"),
63                find(".dbg-drawer").and_then(|el| el.dyn_into::<web_sys::HtmlElement>().ok()),
64                find(".studio-content"),
65            ) else {
66                return;
67            };
68            let start = drawer.get_bounding_client_rect().height();
69            let room = start + panels.get_bounding_client_rect().height();
70            let point = evt.data().client_coordinates();
71            let _ = grip.class_list().add_1("active");
72            let released = grip.clone();
73            crate::ui::drag::begin(
74                grip,
75                evt.data().pointer_id(),
76                (point.x, point.y),
77                crate::ui::drag::Axis::Rows,
78                move |_, dy| {
79                    let _ = drawer
80                        .style()
81                        .set_property("height", &format!("{}px", drawer_dragged(start, dy, room)));
82                },
83                move |_, dy| {
84                    let _ = released.class_list().remove_1("active");
85                    height.set(drawer_dragged(start, dy, room));
86                },
87            );
88        }
89        #[cfg(not(target_arch = "wasm32"))]
90        let _ = (evt, height);
91    };
92    let drawer_style = format!("height: {}px;", height());
93
94    // The debugger is built once from the source the session opened with; `armed_source`
95    // remembers that source so we can tell when the editor/file has since changed.
96    let mut dbg = use_signal(|| Debugger::from_source(&source));
97    let mut armed_source = use_signal(|| source.clone());
98    // What the program has printed at the cursor shows in the Studio's Output column, and a
99    // program that cannot be debugged says why there too.
100    use_effect(move || {
101        let printed = match &*dbg.read() {
102            Ok(d) => InterpreterResult {
103                lines: d.output().to_vec(),
104                error: d.error().map(str::to_string),
105            },
106            Err(message) => InterpreterResult {
107                lines: Vec::new(),
108                error: Some(message.clone()),
109            },
110        };
111        let mut output = output;
112        output.set(printed);
113    });
114    let tab = use_signal(|| DebugTab::Variables);
115    // The variable whose causal provenance is being traced (click a value to set it).
116    let mut traced = use_signal(|| None::<u16>);
117    // The live-proof query typed into the Prove tab.
118    let mut prove_query = use_signal(String::new);
119    // The "virtual hardware" easter egg — off by default, bundle-light.
120    let mut hw_open = use_signal(|| false);
121    // Auto-play: a timer steps the program so you can watch it run.
122    let mut playing = use_signal(|| false);
123    let speed = use_signal(|| 650u32);
124    use_future(move || async move {
125        loop {
126            #[cfg(target_arch = "wasm32")]
127            {
128                gloo_timers::future::TimeoutFuture::new(speed().max(60)).await;
129                if playing() {
130                    let mut still = false;
131                    if let Ok(d) = dbg.write().as_mut() {
132                        if d.is_running() {
133                            d.step();
134                            still = d.is_running();
135                        }
136                    }
137                    if !still {
138                        playing.set(false);
139                    }
140                }
141            }
142            #[cfg(not(target_arch = "wasm32"))]
143            {
144                let _ = (speed, playing, dbg);
145                break;
146            }
147        }
148    });
149
150    // The timeline is wider than the drawer: keep its playhead in view as the cursor moves.
151    use_effect(move || {
152        let _ = dbg.read();
153        if tab() == DebugTab::Timeline {
154            #[cfg(target_arch = "wasm32")]
155            keep_playhead_in_view();
156        }
157    });
158
159    // Compile error → a minimal error drawer (nothing to step).
160    if let Err(msg) = &*dbg.read() {
161        let msg = msg.clone();
162        return rsx! {
163            style { "{DEBUG_DRAWER_STYLE}" }
164            div { class: "dbg-drawer", style: "{drawer_style}",
165                div { class: "dbg-grip", onpointerdown: move |evt| start_grip_drag(evt) }
166                div { class: "dbg-bar",
167                    span { class: "dbg-title", "Debugger" }
168                    button { class: "dbg-btn dbg-stop", onclick: move |_| on_close.call(()), "Close" }
169                }
170                div { class: "dbg-error", "Cannot debug: {msg}" }
171            }
172        };
173    }
174
175    // Pull a fresh snapshot + disassembly for this render.
176    let snap = dbg.read().as_ref().ok().map(|d| d.snapshot());
177    let snap = match snap {
178        Some(s) => s,
179        None => return rsx! {},
180    };
181    let disasm = dbg
182        .read()
183        .as_ref()
184        .ok()
185        .map(|d| {
186            d.disassembly()
187                .iter()
188                .map(|l| (l.pc, l.text.clone()))
189                .collect::<Vec<_>>()
190        })
191        .unwrap_or_default();
192    let bps: std::collections::BTreeSet<usize> = dbg
193        .read()
194        .as_ref()
195        .ok()
196        .map(|d| d.breakpoints().into_iter().collect())
197        .unwrap_or_default();
198
199    let paused = snap.state == "paused";
200    let at_start = snap.step == 0;
201    let cur_tab = *tab.read();
202
203    // ── step toolbar handlers (each mutates the debugger in place) ─────────────
204    let act = move |f: fn(&mut Debugger)| {
205        move |_| {
206            if let Ok(d) = dbg.write().as_mut() {
207                f(d);
208            }
209        }
210    };
211
212    // The editor/file changed since we armed this session (a different program).
213    let stale = armed_source() != source;
214    let reload_source = source.clone();
215
216    rsx! {
217        style { "{DEBUG_DRAWER_STYLE}" }
218        div { class: "dbg-drawer", style: "{drawer_style}",
219            div { class: "dbg-grip", onpointerdown: move |evt| start_grip_drag(evt) }
220            // Toolbar + status line
221            div { class: "dbg-bar",
222                span { class: "dbg-title",
223                    span { class: "dbg-bug", dangerous_inner_html: IC_BUG }
224                    "Debug"
225                }
226                div { class: "dbg-controls",
227                    button { class: "dbg-btn dbg-rev", disabled: at_start, title: "Reverse continue (back to the previous breakpoint)",
228                        onclick: act(Debugger::reverse_resume), dangerous_inner_html: IC_REVERSE }
229                    button { class: "dbg-btn dbg-rev", disabled: at_start, title: "Step back (time-travel one op)",
230                        onclick: act(Debugger::step_back), dangerous_inner_html: IC_STEP_BACK }
231                    span { class: "dbg-divider" }
232                    button { class: if playing() { "dbg-btn dbg-play on" } else { "dbg-btn dbg-play" },
233                        disabled: !paused && !playing(),
234                        title: if playing() { "Pause" } else { "Auto-play: watch it run" },
235                        onclick: move |_| { let p = playing(); playing.set(!p); },
236                        dangerous_inner_html: if playing() { IC_PAUSE } else { IC_PLAY } }
237                    button { class: "dbg-btn dbg-go", disabled: !paused, title: "Continue",
238                        onclick: act(Debugger::resume), dangerous_inner_html: IC_CONTINUE }
239                    button { class: "dbg-btn", disabled: !paused, title: "Step over",
240                        onclick: act(Debugger::step_over), dangerous_inner_html: IC_STEP_OVER }
241                    button { class: "dbg-btn", disabled: !paused, title: "Step into",
242                        onclick: act(Debugger::step), dangerous_inner_html: IC_STEP_INTO }
243                    button { class: "dbg-btn", disabled: !paused, title: "Step out",
244                        onclick: act(Debugger::step_out), dangerous_inner_html: IC_STEP_OUT }
245                    span { class: "dbg-divider" }
246                    button { class: "dbg-btn", title: "Restart (rewind to the entry)",
247                        onclick: act(Debugger::restart), dangerous_inner_html: IC_RESTART }
248                    button { class: if hw_open() { "dbg-btn dbg-toggle on" } else { "dbg-btn dbg-toggle" },
249                        title: "Virtual hardware view",
250                        onclick: move |_| { let v = hw_open(); hw_open.set(!v); }, dangerous_inner_html: IC_GEAR }
251                }
252                div { class: "dbg-status",
253                    if playing() {
254                        span { class: "dbg-state dbg-state-playing", "playing" }
255                    } else {
256                        span { class: "dbg-state dbg-state-{snap.state}", "{snap.state}" }
257                    }
258                    span { class: "dbg-sep", "\u{00B7}" }
259                    span { "pc {snap.pc}/{snap.total_ops}" }
260                    if !snap.op_text.is_empty() {
261                        span { class: "dbg-sep", "\u{00B7}" }
262                        span { class: "dbg-op", "{snap.op_text}" }
263                    }
264                }
265                button { class: "dbg-close", title: "Close the debugger",
266                    onclick: move |_| on_close.call(()), dangerous_inner_html: IC_CLOSE }
267            }
268
269            // The editor changed since this session armed (a file switch or an edit) —
270            // non-destructive: keep debugging the old program, or reload the new one.
271            if stale {
272                div { class: "dbg-reload",
273                    span { class: "dbg-reload-msg", "\u{26A0} The code changed. This session is the previous version." }
274                    button { class: "dbg-reload-btn",
275                        onclick: move |_| {
276                            dbg.set(Debugger::from_source(&reload_source));
277                            armed_source.set(reload_source.clone());
278                            playing.set(false);
279                        },
280                        "Reload" }
281                }
282            }
283
284            // Time-travel scrubber — drag through every op you have run.
285            div { class: "dbg-scrub",
286                span { class: "dbg-scrub-cap", dangerous_inner_html: IC_CLOCK }
287                input {
288                    class: "dbg-scrub-range",
289                    r#type: "range",
290                    min: "0",
291                    max: "{snap.total_steps}",
292                    value: "{snap.step}",
293                    oninput: move |e| {
294                        if let Ok(v) = e.value().parse::<usize>() {
295                            if let Ok(d) = dbg.write().as_mut() { d.seek(v); }
296                        }
297                    },
298                }
299                span { class: "dbg-scrub-label", "{snap.step} / {snap.total_steps}" }
300            }
301
302            // Teaching line: a question to predict the step when the engine has one, otherwise
303            // what the step does (the op itself where there are no plainer words), with the
304            // step's first-order-logic meaning alongside (sum = x + y, t ⟺ (i < n)). One row,
305            // always present, so stepping never moves the panel below it.
306            {
307                let ask = !snap.socratic.is_empty();
308                let text = if ask {
309                    snap.socratic.clone()
310                } else if !snap.narration.is_empty() {
311                    snap.narration.clone()
312                } else {
313                    snap.op_text.clone()
314                };
315                rsx! {
316                    div { class: if ask { "dbg-narr dbg-narr-socratic".to_string() } else { format!("dbg-narr dbg-narr-{}", snap.state) },
317                        span { class: "dbg-narr-ico", dangerous_inner_html: if ask { IC_SOCRATIC } else { IC_BULB } }
318                        span { class: "dbg-narr-text", title: "{text}", "{text}" }
319                        if !snap.fol.is_empty() {
320                            span { class: "dbg-fol", title: "first-order-logic meaning of this step",
321                                span { class: "dbg-fol-tag", "\u{22A8}" }
322                                span { class: "dbg-fol-text", "{snap.fol}" }
323                            }
324                        }
325                    }
326                }
327            }
328
329            // The "virtual hardware" easter egg — an animated datapath of the live VM.
330            if hw_open() {
331                div { class: "dbg-hw", dangerous_inner_html: "{datapath_svg(&snap)}" }
332            }
333
334            // Tabs
335            div { class: "dbg-tabs",
336                DebugTabBtn { label: "Variables", this: DebugTab::Variables, cur: cur_tab, tab }
337                DebugTabBtn { label: "Stack", this: DebugTab::Stack, cur: cur_tab, tab }
338                DebugTabBtn { label: "Heap", this: DebugTab::Heap, cur: cur_tab, tab }
339                DebugTabBtn { label: "Timeline", this: DebugTab::Timeline, cur: cur_tab, tab }
340                DebugTabBtn { label: "Prove", this: DebugTab::Prove, cur: cur_tab, tab }
341                DebugTabBtn { label: "Call Stack", this: DebugTab::CallStack, cur: cur_tab, tab }
342                DebugTabBtn { label: "Breakpoints", this: DebugTab::Breakpoints, cur: cur_tab, tab }
343                DebugTabBtn { label: "Bytecode", this: DebugTab::Bytecode, cur: cur_tab, tab }
344            }
345
346            div { class: "dbg-body",
347                match cur_tab {
348                    DebugTab::Variables => rsx! {
349                        if let Some(frame) = snap.frames.last() {
350                            div { class: "dbg-vars",
351                                if !frame.registers.iter().any(|r| r.name.is_some()) {
352                                    div { class: "dbg-empty", "no variables in this frame yet. The Stack tab shows every register" }
353                                }
354                                for reg in frame.registers.iter().filter(|r| r.name.is_some()) {
355                                    {
356                                        let idx = reg.index;
357                                        let on = traced() == Some(idx);
358                                        rsx! {
359                                            div {
360                                                class: if on { "dbg-var traced" } else if reg.changed { "dbg-var changed" } else { "dbg-var" },
361                                                title: "Trace where this value came from",
362                                                onclick: move |_| {
363                                                    if traced() == Some(idx) { traced.set(None); } else { traced.set(Some(idx)); }
364                                                },
365                                                span { class: "dbg-var-name",
366                                                    { reg.name.clone().unwrap_or_else(|| format!("R{}", reg.index)) }
367                                                }
368                                                if !reg.kind.is_empty() {
369                                                    span { class: "dbg-var-type", "{reg.kind}" }
370                                                }
371                                                span { class: "dbg-var-eq", "=" }
372                                                span { class: "dbg-var-val", "{reg.value}" }
373                                                span { class: "dbg-why", dangerous_inner_html: IC_TRACE }
374                                            }
375                                        }
376                                    }
377                                }
378                            }
379                            // Causal provenance — "why is this value here?" — the exact data-flow
380                            // lineage, possible only because execution is deterministically recorded.
381                            if let Some(node) = traced().and_then(|r| dbg.read().as_ref().ok().and_then(|d| d.provenance(r))) {
382                                div { class: "dbg-prov",
383                                    div { class: "dbg-prov-h",
384                                        span { class: "dbg-prov-title",
385                                            span { class: "dbg-prov-ico", dangerous_inner_html: IC_TRACE }
386                                            "why \u{2192} causal lineage"
387                                        }
388                                        button { class: "dbg-prov-x", onclick: move |_| traced.set(None),
389                                            dangerous_inner_html: IC_CLOSE }
390                                    }
391                                    div { class: "dbg-prov-tree", dangerous_inner_html: "{causal_html(&node)}" }
392                                }
393                            }
394                            if !snap.globals.is_empty() {
395                                div { class: "dbg-globals-h", "globals" }
396                                for (name, val) in snap.globals.iter() {
397                                    div { class: "dbg-var",
398                                        span { class: "dbg-var-name", "{name}" }
399                                        span { class: "dbg-var-eq", "=" }
400                                        span { class: "dbg-var-val", "{val}" }
401                                    }
402                                }
403                            }
404                        }
405                    },
406                    DebugTab::Stack => rsx! {
407                        div { class: "dbg-stackmem",
408                            div { class: "dbg-mem-hint", "The register file, the VM's stack memory. Each frame's slots at their addresses (current frame on top)." }
409                            for frame in snap.frames.iter().rev() {
410                                div { class: "dbg-sframe",
411                                    div { class: "dbg-sframe-h",
412                                        span { class: "dbg-sframe-fn", { frame.function.clone().unwrap_or_else(|| "Main".to_string()) } }
413                                        span { class: "dbg-sframe-base", "base @ {frame.base}" }
414                                    }
415                                    for reg in frame.registers.iter() {
416                                        {
417                                            let addr = frame.base + reg.index as usize;
418                                            let nm = reg.name.clone().unwrap_or_else(|| format!("R{}", reg.index));
419                                            rsx! {
420                                                div { class: if reg.changed { "dbg-slot changed" } else { "dbg-slot" },
421                                                    span { class: "dbg-slot-addr", "{addr:04}" }
422                                                    span { class: "dbg-slot-name", "{nm}" }
423                                                    if !reg.kind.is_empty() {
424                                                        span { class: "dbg-slot-type", "{reg.kind}" }
425                                                    }
426                                                    span { class: "dbg-slot-val", "{reg.value}" }
427                                                }
428                                            }
429                                        }
430                                    }
431                                }
432                            }
433                        }
434                    },
435                    DebugTab::Heap => rsx! {
436                        div { class: "dbg-heap",
437                            div { class: "dbg-mem-hint", "Heap objects (lists, maps, structs, text). A box shared by two variables is an alias: one allocation, two names." }
438                            if snap.heap.is_empty() {
439                                div { class: "dbg-empty", "no heap objects, only scalar values in scope" }
440                            }
441                            for obj in snap.heap.iter() {
442                                div { class: if obj.shared { "dbg-hobj shared" } else { "dbg-hobj" },
443                                    div { class: "dbg-hobj-h",
444                                        span { class: "dbg-hobj-id", "{obj.id}" }
445                                        span { class: "dbg-hobj-kind", "{obj.kind}" }
446                                        span { class: "dbg-hobj-store", title: "memory layout", "{obj.storage}" }
447                                        if obj.rc > 1 {
448                                            span { class: "dbg-hobj-rc", title: "reference count", "rc {obj.rc}" }
449                                        }
450                                        if obj.shared {
451                                            span { class: "dbg-hobj-alias", "aliased" }
452                                        }
453                                    }
454                                    div { class: "dbg-hobj-val", "{obj.summary}" }
455                                    div { class: "dbg-hobj-refs",
456                                        span { class: "dbg-refs-label", "\u{2190}" }
457                                        for r in obj.referenced_by.iter() {
458                                            span { class: "dbg-ref", "{r}" }
459                                        }
460                                    }
461                                }
462                            }
463                        }
464                    },
465                    DebugTab::Timeline => {
466                        let tl = dbg.read().as_ref().ok().map(|d| d.variable_timeline());
467                        let insights = dbg.read().as_ref().ok().map(|d| d.observed_invariants()).unwrap_or_default();
468                        let proven = dbg.read().as_ref().ok().map(|d| d.proven_invariants()).unwrap_or_default();
469                        match tl {
470                            Some(tl) if !tl.vars.is_empty() => rsx! {
471                                div { class: "dbg-osc-wrap",
472                                    div { class: "dbg-mem-hint", "Every variable's value across the whole run: a logic-analyzer for your program. The pink playhead is your time-travel cursor. Drag the scrubber to move through time." }
473                                    if tl.truncated {
474                                        div { class: "dbg-mem-hint dim", "showing the most recent {tl.steps} steps" }
475                                    }
476                                    {
477                                        let (names, lanes) = timeline_svg(&tl);
478                                        rsx! {
479                                            div { class: "dbg-osc",
480                                                div { class: "dbg-osc-names", dangerous_inner_html: "{names}" }
481                                                div { class: "dbg-osc-scroll", dangerous_inner_html: "{lanes}" }
482                                            }
483                                        }
484                                    }
485                                    // PROVEN invariants — the Oracle's static guarantees (every run).
486                                    if !proven.is_empty() {
487                                        div { class: "dbg-insights",
488                                            div { class: "dbg-insights-h proven",
489                                                span { class: "dbg-proven-ico", dangerous_inner_html: IC_SHIELD }
490                                                "proven: holds on every run"
491                                            }
492                                            for p in proven.iter() {
493                                                div { class: "dbg-insight",
494                                                    span { class: "dbg-insight-name", "{p.name}" }
495                                                    for f in p.facts.iter() {
496                                                        span { class: "dbg-insight-fact proven", "{f}" }
497                                                    }
498                                                }
499                                            }
500                                        }
501                                    }
502                                    // Daikon-style observed invariants — what held over THIS run.
503                                    if !insights.is_empty() {
504                                        div { class: "dbg-insights",
505                                            div { class: "dbg-insights-h", "observed this run" }
506                                            for ins in insights.iter() {
507                                                div { class: "dbg-insight",
508                                                    span { class: "dbg-insight-name", "{ins.name}" }
509                                                    for f in ins.facts.iter() {
510                                                        span { class: "dbg-insight-fact", "{f}" }
511                                                    }
512                                                }
513                                            }
514                                        }
515                                    }
516                                }
517                            },
518                            _ => rsx! {
519                                div { class: "dbg-empty", "no variables to plot yet. Step the program to record a timeline" }
520                            },
521                        }
522                    },
523                    DebugTab::Prove => {
524                        let q = prove_query();
525                        let result = if q.trim().is_empty() {
526                            None
527                        } else {
528                            dbg.read().as_ref().ok().map(|d| d.assert_at_cursor(&q))
529                        };
530                        rsx! {
531                            div { class: "dbg-prove",
532                                div { class: "dbg-mem-hint", "Assert a property of the variables. You get both lenses: whether it holds NOW (live values) and whether it's PROVEN for every run (the Oracle's static facts, with no Z3)." }
533                                input {
534                                    class: "dbg-prove-input",
535                                    r#type: "text",
536                                    placeholder: "e.g.  x < y    or    sum >= 0",
537                                    value: "{q}",
538                                    oninput: move |e| prove_query.set(e.value()),
539                                }
540                                if let Some(r) = result {
541                                    if r.parsed {
542                                        div { class: "dbg-prove-result",
543                                            div { class: "dbg-prove-row",
544                                                span { class: "dbg-prove-label", "now" }
545                                                match r.now {
546                                                    Some(true) => rsx! { span { class: "dbg-verdict yes", "true" } },
547                                                    Some(false) => rsx! { span { class: "dbg-verdict no", "false" } },
548                                                    None => rsx! { span { class: "dbg-verdict idk", "not a live value" } },
549                                                }
550                                                if !r.now_detail.is_empty() {
551                                                    span { class: "dbg-prove-detail", "{r.now_detail}" }
552                                                }
553                                            }
554                                            div { class: "dbg-prove-row",
555                                                span { class: "dbg-prove-label", "every run" }
556                                                {
557                                                    let (cls, txt) = match r.verdict {
558                                                        ProofVerdict::ProvenTrue => ("yes", "proven"),
559                                                        ProofVerdict::ProvenFalse => ("no", "refuted"),
560                                                        ProofVerdict::Unknown => ("idk", "unproven"),
561                                                    };
562                                                    rsx! { span { class: "dbg-verdict {cls}", "{txt}" } }
563                                                }
564                                                if !r.verdict_detail.is_empty() {
565                                                    span { class: "dbg-prove-detail", "{r.verdict_detail}" }
566                                                }
567                                            }
568                                        }
569                                    } else {
570                                        div { class: "dbg-prove-err", "{r.now_detail}" }
571                                    }
572                                }
573                            }
574                        }
575                    },
576                    DebugTab::CallStack => rsx! {
577                        div { class: "dbg-stack",
578                            for (i, frame) in snap.frames.iter().enumerate().rev() {
579                                div { class: if i + 1 == snap.frames.len() { "dbg-frame current" } else { "dbg-frame" },
580                                    span { class: "dbg-frame-fn",
581                                        { frame.function.clone().unwrap_or_else(|| "Main".to_string()) }
582                                    }
583                                }
584                            }
585                        }
586                    },
587                    DebugTab::Breakpoints => rsx! {
588                        div { class: "dbg-bps",
589                            if bps.is_empty() {
590                                div { class: "dbg-empty", "no breakpoints. Click a line in the Bytecode tab" }
591                            }
592                            for pc in bps.iter().copied() {
593                                {
594                                    let label = disasm.iter().find(|(p, _)| *p == pc)
595                                        .map(|(_, t)| t.clone()).unwrap_or_default();
596                                    rsx! {
597                                        div { class: "dbg-bp",
598                                            onclick: move |_| { if let Ok(d) = dbg.write().as_mut() { d.clear_breakpoint(pc); } },
599                                            span { class: "dbg-bp-dot", "\u{25CF}" }
600                                            span { class: "dbg-bp-pc", "{pc}" }
601                                            span { class: "dbg-bp-op", "{label}" }
602                                        }
603                                    }
604                                }
605                            }
606                        }
607                    },
608                    DebugTab::Bytecode => rsx! {
609                        div { class: "dbg-tape",
610                            for (pc, text) in disasm.iter() {
611                                {
612                                    let pc = *pc;
613                                    let is_cur = pc == snap.pc && snap.state != "done";
614                                    let has_bp = bps.contains(&pc);
615                                    let row = if is_cur { "dbg-row cur" } else { "dbg-row" };
616                                    rsx! {
617                                        div { class: "{row}",
618                                            onclick: move |_| { if let Ok(d) = dbg.write().as_mut() { d.toggle_breakpoint(pc); } },
619                                            span { class: if has_bp { "dbg-gutter on" } else { "dbg-gutter" },
620                                                if has_bp { "\u{25CF}" } else { "" }
621                                            }
622                                            span { class: "dbg-pc", "{pc}" }
623                                            span { class: "dbg-text", "{text}" }
624                                        }
625                                    }
626                                }
627                            }
628                        }
629                    },
630                }
631            }
632        }
633    }
634}
635
636#[component]
637fn DebugTabBtn(label: String, this: DebugTab, cur: DebugTab, tab: Signal<DebugTab>) -> Element {
638    let active = this == cur;
639    rsx! {
640        button {
641            class: if active { "dbg-tab active" } else { "dbg-tab" },
642            onclick: move |_| tab.set(this),
643            "{label}"
644        }
645    }
646}
647
648/// XML-escape a value for embedding in the datapath SVG.
649fn esc(s: &str) -> String {
650    s.replace('&', "&amp;")
651        .replace('<', "&lt;")
652        .replace('>', "&gt;")
653        .replace('"', "&quot;")
654}
655
656/// `s` cut to at most `max` characters, the last one an ellipsis when anything is cut.
657fn clip(s: &str, max: usize) -> String {
658    if s.chars().count() > max {
659        format!("{}\u{2026}", s.chars().take(max - 1).collect::<String>())
660    } else {
661        s.to_string()
662    }
663}
664
665/// Scroll the timeline so its playhead sits in view, once the next frame has laid it out.
666#[cfg(target_arch = "wasm32")]
667fn keep_playhead_in_view() {
668    use wasm_bindgen::JsCast;
669    let follow = wasm_bindgen::closure::Closure::once_into_js(move || {
670        let Some(document) = web_sys::window().and_then(|w| w.document()) else {
671            return;
672        };
673        let find = |selector: &str| document.query_selector(selector).ok().flatten();
674        let (Some(view), Some(head)) = (find(".dbg-osc-scroll"), find(".dbg-osc-scroll .tlph"))
675        else {
676            return;
677        };
678        let (v, h) = (
679            view.get_bounding_client_rect(),
680            head.get_bounding_client_rect(),
681        );
682        let margin = v.width() * 0.15;
683        if h.left() < v.left() + margin || h.right() > v.right() - margin {
684            let target = view.scroll_left() as f64 + (h.left() - v.left()) - v.width() / 2.0;
685            view.set_scroll_left(target.max(0.0) as i32);
686        }
687    });
688    if let Some(window) = web_sys::window() {
689        let _ = window.request_animation_frame(follow.unchecked_ref());
690    }
691}
692
693/// The "virtual hardware" easter egg: an animated datapath of the paused VM — the
694/// register file wired into the engine/ALU, the current op lit up, changed registers
695/// pulsing. Pure string SVG + CSS keyframes (no deps; nothing renders until opened).
696fn datapath_svg(snap: &DebugSnapshot) -> String {
697    let regs: &[_] = match snap.frames.last() {
698        Some(f) => &f.registers,
699        None => &[],
700    };
701    let cw = 92.0_f64;
702    let gap = 8.0_f64;
703    // Wide enough for every register — and never narrower than the original 800, so a
704    // small frame is not blown up past its designed size.
705    let width = (24.0 + regs.len() as f64 * (cw + gap) - gap).max(800.0);
706    let engine_x = width / 2.0;
707    let engine_y = 104.0_f64;
708    let mut s = format!(
709        "<svg viewBox='0 0 {width:.0} 168' width='100%' preserveAspectRatio='xMidYMid meet' xmlns='http://www.w3.org/2000/svg'>",
710    );
711    s.push_str(DATAPATH_CSS);
712    let reads: std::collections::BTreeSet<u16> = snap.op_reads.iter().copied().collect();
713    let write = snap.op_writes;
714    let cell_cx = |pos: usize| 12.0 + pos as f64 * (cw + gap) + cw / 2.0;
715    // Operand wires: each SOURCE register of this op flows down into the engine.
716    for (pos, r) in regs.iter().enumerate() {
717        if reads.contains(&r.index) {
718            let sx = cell_cx(pos);
719            s.push_str(&format!(
720                "<path class='wire live' d='M{sx:.0},44 C{sx:.0},80 {engine_x:.0},80 {engine_x:.0},{ey:.0}'/>",
721                ey = engine_y - 4.0,
722            ));
723        }
724    }
725    // Result wire: the engine writes its result back up to the DESTINATION register.
726    if let Some(w) = write {
727        if let Some(pos) = regs.iter().position(|r| r.index == w) {
728            let dx = cell_cx(pos);
729            s.push_str(&format!(
730                "<path class='wire out' d='M{engine_x:.0},{eb:.0} C{engine_x:.0},{lo:.0} {dx:.0},{lo:.0} {dx:.0},44'/>",
731                eb = engine_y + 46.0,
732                lo = engine_y + 72.0,
733            ));
734        }
735    }
736    // Register cells — sources glow cyan, the destination glows green.
737    for (pos, r) in regs.iter().enumerate() {
738        let x = 12.0 + pos as f64 * (cw + gap);
739        let name = r.name.clone().unwrap_or_else(|| format!("R{}", r.index));
740        let cls = if Some(r.index) == write {
741            "rcell dst"
742        } else if reads.contains(&r.index) {
743            "rcell src"
744        } else if r.changed {
745            "rcell hot"
746        } else {
747            "rcell"
748        };
749        s.push_str(&format!(
750            "<g class='{cls}'><rect x='{x:.0}' y='14' width='{cw:.0}' height='30' rx='6'/>\
751             <text x='{tx:.0}' y='33' text-anchor='middle'>{n} = {v}</text></g>",
752            tx = x + cw / 2.0,
753            n = esc(&name),
754            v = esc(&clip(&r.value, 9)),
755        ));
756    }
757    // Engine box — the live operation, narrated in plain English.
758    let engine_cls = if snap.state == "paused" {
759        "engine live"
760    } else {
761        "engine"
762    };
763    let label = if !snap.narration.is_empty() {
764        &snap.narration
765    } else {
766        &snap.op_text
767    };
768    let label = clip(label, 48);
769    s.push_str(&format!(
770        "<g class='{engine_cls}'><rect x='{ex:.0}' y='{ey:.0}' width='340' height='46' rx='8'/>\
771         <text class='lbl' x='{tx:.0}' y='{ly:.0}' text-anchor='middle'>ALU \u{00B7} pc {pc}/{tot}</text>\
772         <text class='op' x='{tx:.0}' y='{oy:.0}' text-anchor='middle'>{op}</text></g>",
773        ex = engine_x - 170.0,
774        ey = engine_y,
775        tx = engine_x,
776        ly = engine_y + 18.0,
777        oy = engine_y + 37.0,
778        pc = snap.pc,
779        tot = snap.total_ops,
780        op = esc(&label),
781    ));
782    s.push_str("</svg>");
783    s
784}
785
786/// The **variable oscilloscope** — a logic-analyzer waveform of every variable's
787/// value across the whole recorded run, runs of equal value merged into held "bus"
788/// boxes, with a pink playhead pinned at the time-travel cursor. Two SVGs drawn side by
789/// side: the lane names, which stay put, and the lanes, which scroll beneath a long run. Reuses the studio's
790/// `waveform_svg` idiom; the program's entire observable past on one scope.
791fn timeline_svg(tl: &VarTimeline) -> (String, String) {
792    let gutter = 96.0_f64;
793    let pad = 4.0_f64;
794    let col_w = 34.0_f64;
795    let row_h = 30.0_f64;
796    let top = 22.0_f64;
797    let n = tl.steps.max(1);
798    let lanes = tl.vars.len().max(1);
799    let width = pad + n as f64 * col_w + 14.0;
800    let height = top + lanes as f64 * row_h + 16.0;
801    let bottom = top + lanes as f64 * row_h;
802    let col_x = |step: usize| pad + step as f64 * col_w;
803
804    // The lane names: their own narrow SVG, so they stay put while the lanes scroll.
805    let mut names = format!(
806        "<svg viewBox='0 0 {gutter:.0} {height:.0}' width='{gutter:.0}' height='{height:.0}' xmlns='http://www.w3.org/2000/svg'>"
807    );
808    names.push_str(TIMELINE_CSS);
809    for (ri, v) in tl.vars.iter().enumerate() {
810        let y = top + ri as f64 * row_h;
811        let label = if v.kind.is_empty() {
812            v.name.clone()
813        } else {
814            format!("{} : {}", v.name, v.kind)
815        };
816        names.push_str(&format!(
817            "<text class='tln' x='{:.0}' y='{:.0}'>{}</text>",
818            gutter - 9.0,
819            y + row_h * 0.62,
820            esc(&label)
821        ));
822    }
823    names.push_str("</svg>");
824
825    let mut s = format!(
826        "<svg viewBox='0 0 {width:.0} {height:.0}' width='{width:.0}' height='{height:.0}' xmlns='http://www.w3.org/2000/svg'>"
827    );
828    s.push_str(TIMELINE_CSS);
829
830    // Time grid + step labels (thinned so they stay readable on long runs).
831    let label_every = (n / 14).max(1);
832    for step in 0..=n {
833        let x = col_x(step);
834        s.push_str(&format!(
835            "<line class='tlg' x1='{x:.0}' y1='{top:.0}' x2='{x:.0}' y2='{bottom:.0}'/>"
836        ));
837        if step < n && step % label_every == 0 {
838            s.push_str(&format!(
839                "<text class='tltl' x='{:.0}' y='14'>{}</text>",
840                x + col_w / 2.0,
841                tl.start + step
842            ));
843        }
844    }
845
846    // Playhead at the cursor column.
847    let px = col_x(tl.cursor.saturating_sub(tl.start));
848    s.push_str(&format!(
849        "<rect class='tlph-bg' x='{px:.0}' y='{top:.0}' width='{col_w:.0}' height='{:.0}'/>",
850        bottom - top
851    ));
852    s.push_str(&format!(
853        "<line class='tlph' x1='{px:.0}' y1='{:.0}' x2='{px:.0}' y2='{:.0}'/>",
854        top - 6.0,
855        bottom + 4.0
856    ));
857
858    for (ri, v) in tl.vars.iter().enumerate() {
859        let y = top + ri as f64 * row_h;
860        // Merge consecutive equal samples into one held box (the waveform "bus value").
861        let mut i = 0usize;
862        while i < v.points.len() {
863            let p = &v.points[i];
864            let mut j = i + 1;
865            while j < v.points.len()
866                && v.points[j].present == p.present
867                && v.points[j].value == p.value
868            {
869                j += 1;
870            }
871            if p.present {
872                let x = col_x(i);
873                let span = (j - i) as f64 * col_w;
874                let cls = if p.changed { "tlbox edge" } else { "tlbox" };
875                s.push_str(&format!(
876                    "<rect class='{cls}' x='{:.0}' y='{:.0}' width='{:.0}' height='{:.0}' rx='5'/>",
877                    x + 1.5,
878                    y + 4.0,
879                    (span - 3.0).max(2.0),
880                    row_h - 9.0
881                ));
882                let shown: String = p.value.chars().take((span / 7.0) as usize + 1).collect();
883                s.push_str(&format!(
884                    "<text class='tlv' x='{:.0}' y='{:.0}'>{}</text>",
885                    x + span / 2.0,
886                    y + row_h * 0.62,
887                    esc(&shown)
888                ));
889            }
890            i = j;
891        }
892    }
893    s.push_str("</svg>");
894    (names, s)
895}
896
897/// Render a [`CausalNode`] provenance tree to indented HTML — each value shown with
898/// the op that produced it and the step it happened, its inputs nested below.
899fn causal_html(node: &CausalNode) -> String {
900    let mut s = String::new();
901    causal_html_rec(node, 0, &mut s);
902    s
903}
904
905fn causal_html_rec(node: &CausalNode, depth: usize, out: &mut String) {
906    let name = node
907        .name
908        .clone()
909        .unwrap_or_else(|| format!("R{}", node.reg));
910    let how = if !node.narration.is_empty() {
911        node.narration.clone()
912    } else if !node.op_text.is_empty() {
913        node.op_text.clone()
914    } else {
915        "initial value".to_string()
916    };
917    out.push_str(&format!(
918        "<div class='cz-row' style='padding-left:{}px'>",
919        depth * 18
920    ));
921    if depth > 0 {
922        out.push_str("<span class='cz-arm'>\u{2514}\u{2500}</span>");
923    }
924    out.push_str(&format!(
925        "<span class='cz-val'>{} = {}</span>",
926        esc(&name),
927        esc(&node.value)
928    ));
929    if !node.kind.is_empty() {
930        out.push_str(&format!("<span class='cz-kind'>{}</span>", esc(&node.kind)));
931    }
932    out.push_str(&format!("<span class='cz-how'>{}</span>", esc(&how)));
933    if node.step > 0 {
934        out.push_str(&format!("<span class='cz-step'>step {}</span>", node.step));
935    }
936    out.push_str("</div>");
937    for inp in &node.inputs {
938        causal_html_rec(inp, depth + 1, out);
939    }
940}
941
942const TIMELINE_CSS: &str = "<style>\
943.tlg{stroke:rgba(255,255,255,0.05);stroke-width:1}\
944.tltl{fill:rgba(255,255,255,0.4);font:9px ui-monospace,monospace;text-anchor:middle}\
945.tln{fill:#a5b4fc;font:11px ui-monospace,monospace;text-anchor:end}\
946.tlbox{fill:rgba(34,211,238,0.10);stroke:#22d3ee;stroke-width:1}\
947.tlbox.edge{fill:rgba(129,140,248,0.20);stroke:#818cf8;stroke-width:1.5}\
948.tlv{fill:#dbeafe;font:10px ui-monospace,monospace;text-anchor:middle}\
949.tlph{stroke:#f472b6;stroke-width:1.5;opacity:0.9}\
950.tlph-bg{fill:rgba(244,114,182,0.08)}\
951</style>";
952
953const IC_TRACE: &str = "<svg viewBox='0 0 16 16' width='12' height='12' fill='none' stroke='currentColor' stroke-width='1.3' stroke-linecap='round' stroke-linejoin='round'><circle cx='3.4' cy='3.4' r='1.6'/><circle cx='12.6' cy='3.4' r='1.6'/><circle cx='8' cy='12.6' r='1.6'/><path d='M3.4 5v2.2a2 2 0 0 0 2 2h5.2a2 2 0 0 0 2-2V5'/><path d='M8 9.4v1.6'/></svg>";
954
955const IC_SHIELD: &str = "<svg viewBox='0 0 16 16' width='12' height='12' fill='none' stroke='currentColor' stroke-width='1.3' stroke-linecap='round' stroke-linejoin='round'><path d='M8 1.6l5 1.8v4c0 3.2-2.2 5.4-5 6.4-2.8-1-5-3.2-5-6.4v-4z'/><path d='M5.8 8l1.5 1.5L10.4 6'/></svg>";
956
957const IC_SOCRATIC: &str = "<svg viewBox='0 0 16 16' width='14' height='14' fill='none' stroke='currentColor' stroke-width='1.3' stroke-linecap='round' stroke-linejoin='round'><path d='M3 2.8h10a1 1 0 0 1 1 1v6a1 1 0 0 1-1 1H7l-3 2.4V10.8H3a1 1 0 0 1-1-1v-6a1 1 0 0 1 1-1z'/><path d='M6.5 5.6a1.6 1.6 0 0 1 3 .5c0 1-1.5 1.3-1.5 2.2'/><path d='M8 9.9h.01'/></svg>";
958
959const DATAPATH_CSS: &str = "<style>\
960.rcell rect{fill:#161b22;stroke:rgba(255,255,255,0.12);stroke-width:1}\
961.rcell text{fill:#cbd5e1;font:11px ui-monospace,monospace}\
962.rcell.hot rect{stroke:#667eea;fill:#1e2540;animation:dpglow 1s ease-in-out infinite}\
963.rcell.hot text{fill:#a5b4fc}\
964.rcell.src rect{stroke:#22d3ee;fill:#0e2a33;animation:dpglow 1s ease-in-out infinite}\
965.rcell.src text{fill:#67e8f9}\
966.rcell.dst rect{stroke:#4ade80;fill:#0e2a1c;animation:dpglow 1s ease-in-out infinite}\
967.rcell.dst text{fill:#86efac}\
968@keyframes dpglow{0%,100%{stroke-opacity:.45}50%{stroke-opacity:1}}\
969.wire{fill:none;stroke:rgba(255,255,255,0.08);stroke-width:1.5}\
970.wire.live{stroke:#22d3ee;stroke-dasharray:5 4;animation:dpflow .7s linear infinite}\
971.wire.out{fill:none;stroke:#4ade80;stroke-width:1.5;stroke-dasharray:5 4;animation:dpflow .7s linear infinite}\
972@keyframes dpflow{to{stroke-dashoffset:-18}}\
973.engine rect{fill:#12161c;stroke:rgba(255,255,255,0.18);stroke-width:1.5}\
974.engine.live rect{stroke:#4ade80;animation:dpglow 1.4s ease-in-out infinite}\
975.engine .lbl{fill:#9ca3af;font:bold 11px ui-monospace,monospace}\
976.engine .op{fill:#e8eaed;font:12px ui-monospace,monospace}\
977</style>";
978
979// ── Debug-control icons — inline SVG (font-independent, crisp, `currentColor` so
980//    they inherit each button's colour). No tofu, unlike obscure Unicode glyphs.
981const IC_CONTINUE: &str = "<svg viewBox='0 0 16 16' width='15' height='15' fill='currentColor'><path d='M3.5 3v10l7-5z'/><rect x='11' y='3' width='1.8' height='10' rx='.6'/></svg>";
982const IC_REVERSE: &str = "<svg viewBox='0 0 16 16' width='15' height='15' fill='currentColor'><rect x='3.2' y='3' width='1.8' height='10' rx='.6'/><path d='M12.5 3v10l-7-5z'/></svg>";
983const IC_STEP_BACK: &str = "<svg viewBox='0 0 16 16' width='15' height='15' fill='currentColor'><rect x='3' y='3.2' width='1.7' height='9.6' rx='.6'/><path d='M12.5 3.2v9.6l-6.6-4.8z'/></svg>";
984const IC_STEP_INTO: &str = "<svg viewBox='0 0 16 16' width='15' height='15' fill='none' stroke='currentColor' stroke-width='1.4' stroke-linecap='round' stroke-linejoin='round'><path d='M8 2.5v5'/><path d='M5.4 5L8 7.6 10.6 5'/><circle cx='8' cy='12.2' r='1.5' fill='currentColor' stroke='none'/></svg>";
985const IC_STEP_OUT: &str = "<svg viewBox='0 0 16 16' width='15' height='15' fill='none' stroke='currentColor' stroke-width='1.4' stroke-linecap='round' stroke-linejoin='round'><path d='M8 8.1V3'/><path d='M5.4 5.6L8 3l2.6 2.6'/><circle cx='8' cy='12.2' r='1.5' fill='currentColor' stroke='none'/></svg>";
986const IC_STEP_OVER: &str = "<svg viewBox='0 0 16 16' width='15' height='15' fill='none' stroke='currentColor' stroke-width='1.4' stroke-linecap='round' stroke-linejoin='round'><path d='M3 8.6c.4-4 9.6-4 10 .4'/><path d='M11 6.7l2 1.1-1.7 1.8'/><circle cx='8' cy='12.2' r='1.5' fill='currentColor' stroke='none'/></svg>";
987const IC_RESTART: &str = "<svg viewBox='0 0 16 16' width='15' height='15' fill='none' stroke='currentColor' stroke-width='1.4' stroke-linecap='round' stroke-linejoin='round'><path d='M12.6 8a4.6 4.6 0 1 1-1.5-3.4'/><path d='M12.9 2.3l-.2 2.6-2.6-.4'/></svg>";
988const IC_CLOCK: &str = "<svg viewBox='0 0 16 16' width='13' height='13' fill='none' stroke='currentColor' stroke-width='1.3' stroke-linecap='round' stroke-linejoin='round'><circle cx='8' cy='8.6' r='4.8'/><path d='M8 5.6v3l2 1.4'/><path d='M6 1.6h4M8 1.6v2'/></svg>";
989const IC_CLOSE: &str = "<svg viewBox='0 0 16 16' width='15' height='15' fill='none' stroke='currentColor' stroke-width='1.7' stroke-linecap='round'><path d='M4 4l8 8M12 4l-8 8'/></svg>";
990const IC_BULB: &str = "<svg viewBox='0 0 16 16' width='14' height='14' fill='none' stroke='currentColor' stroke-width='1.3' stroke-linecap='round' stroke-linejoin='round'><path d='M6 12h4M6.4 13.6h3.2'/><path d='M5 7.4a3 3 0 1 1 6 0c0 1.3-.8 2-1.3 2.7-.2.3-.2.6-.2 1.4H6.5c0-.8 0-1.1-.2-1.4C5.8 9.4 5 8.7 5 7.4z'/></svg>";
991const IC_PLAY: &str = "<svg viewBox='0 0 16 16' width='15' height='15' fill='currentColor'><path d='M4.5 3v10l8-5z'/></svg>";
992const IC_PAUSE: &str = "<svg viewBox='0 0 16 16' width='15' height='15' fill='currentColor'><rect x='4' y='3.5' width='2.7' height='9' rx='.8'/><rect x='9.3' y='3.5' width='2.7' height='9' rx='.8'/></svg>";
993const IC_GEAR: &str = "<svg viewBox='0 0 16 16' width='15' height='15' fill='none' stroke='currentColor' stroke-width='1.2' stroke-linecap='round' stroke-linejoin='round'><circle cx='8' cy='8' r='2.1'/><path d='M8 1.7v1.6M8 12.7v1.6M14.3 8h-1.6M3.3 8H1.7M12.45 3.55l-1.13 1.13M4.68 11.32l-1.13 1.13M12.45 12.45l-1.13-1.13M4.68 4.68L3.55 3.55'/></svg>";
994
995/// A ladybug glyph as inline SVG, for the Debug button + drawer title (shared with
996/// the Studio toolbar). Explicitly sized so it needs no extra CSS.
997pub const IC_BUG: &str = "<svg viewBox='0 0 16 16' width='14' height='14' fill='none' stroke='currentColor' stroke-width='1.2' stroke-linecap='round' stroke-linejoin='round'><ellipse cx='8' cy='9' rx='2.9' ry='3.5'/><path d='M8 5.6v6.8'/><path d='M5.1 9H2.7M10.9 9h2.4M5.3 6.5L3.6 5.2M10.7 6.5l1.7-1.3M5.3 11.6L3.6 12.9M10.7 11.6l1.7 1.3'/><path d='M6.4 4.4a1.6 1.6 0 0 1 3.2 0'/></svg>";
998
999const DEBUG_DRAWER_STYLE: &str = r#"
1000.dbg-drawer { display:flex; flex-direction:column; background:var(--studio-panel-bg,#12161c);
1001  border-top:1px solid var(--studio-border,rgba(255,255,255,0.08)); color:var(--studio-text,#e8eaed);
1002  font-size:13px; flex:0 1 auto; min-height:44px; overflow:hidden; }
1003/* The grab bars are the column bars turned on their side: solid 8px, in their grey. */
1004.dbg-grip { flex:none; height:8px; background:var(--studio-resizer,#22272b); cursor:row-resize; touch-action:none; transition:background 0.2s ease; }
1005.dbg-grip:hover, .dbg-grip.active { background:var(--studio-accent,#667eea); }
1006/* Under a finger the grip takes a finger: its grab area reaches 18px past what it draws. */
1007@media (pointer: coarse) {
1008.dbg-grip { position:relative; }
1009.dbg-grip::before { content:""; position:absolute; left:0; right:0; top:-18px; bottom:-18px; z-index:1; }
1010}
1011.dbg-bar { display:flex; align-items:center; gap:12px; padding:6px 12px; flex-wrap:wrap;
1012  border-bottom:1px solid var(--studio-border,rgba(255,255,255,0.08)); }
1013.dbg-title { font-weight:600; }
1014.dbg-controls { display:flex; gap:4px; }
1015.dbg-btn { background:rgba(255,255,255,0.05); border:1px solid var(--studio-border,rgba(255,255,255,0.08));
1016  color:var(--studio-text,#e8eaed); border-radius:6px; padding:3px 8px; cursor:pointer; font-size:13px;
1017  line-height:1; transition:all .12s ease; }
1018.dbg-btn:hover:not(:disabled) { background:rgba(255,255,255,0.1); }
1019.dbg-btn:disabled { opacity:0.35; cursor:default; }
1020.dbg-go { color:#4ade80; }
1021.dbg-stop { color:#f87171; }
1022.dbg-toggle.on { background:rgba(102,126,234,0.3); border-color:var(--studio-accent,#667eea); }
1023.dbg-status { display:flex; align-items:center; gap:6px; margin-left:auto; color:var(--studio-text-secondary,#9ca3af);
1024  font-family:ui-monospace,monospace; font-size:12px; }
1025.dbg-sep { opacity:0.4; }
1026.dbg-op { color:var(--studio-text,#e8eaed); }
1027.dbg-state { text-transform:uppercase; font-size:11px; letter-spacing:0.5px; padding:1px 6px; border-radius:4px; }
1028.dbg-state-paused { background:rgba(102,126,234,0.25); color:#a5b4fc; }
1029.dbg-state-done { background:rgba(74,222,128,0.2); color:#4ade80; }
1030.dbg-state-blocked { background:rgba(251,191,36,0.2); color:#fbbf24; }
1031.dbg-state-error { background:rgba(248,113,113,0.2); color:#f87171; }
1032.dbg-state-playing { background:rgba(34,211,238,0.18); color:#22d3ee; }
1033.dbg-tabs { display:flex; gap:2px; padding:4px 8px 0; border-bottom:1px solid var(--studio-border,rgba(255,255,255,0.08)); }
1034.dbg-tab { background:transparent; border:none; color:var(--studio-text-muted,#6b7280); padding:5px 12px;
1035  cursor:pointer; border-radius:5px 5px 0 0; font-size:12px; }
1036.dbg-tab.active { color:var(--studio-text,#e8eaed); background:rgba(255,255,255,0.05); }
1037.dbg-body { flex:1; min-height:0; overflow:auto; padding:8px 12px; font-family:ui-monospace,monospace; font-size:12px; }
1038.dbg-empty { color:var(--studio-text-muted,#6b7280); font-style:italic; }
1039.dbg-var { display:flex; gap:6px; padding:2px 4px; border-radius:4px; }
1040.dbg-var.changed { background:rgba(102,126,234,0.18); }
1041.dbg-var-name { color:#a5b4fc; min-width:48px; }
1042.dbg-var-type { color:#f0abfc; font-size:11px; opacity:0.85; background:rgba(240,171,252,0.1); border-radius:4px; padding:0 5px; align-self:center; }
1043.dbg-var-eq { opacity:0.4; }
1044.dbg-var-val { color:#4ade80; }
1045.dbg-var { cursor:pointer; border-radius:5px; }
1046.dbg-var:hover { background:rgba(255,255,255,0.04); }
1047.dbg-var:hover .dbg-why { opacity:0.7; }
1048.dbg-var.traced { background:rgba(34,211,238,0.12); box-shadow:inset 2px 0 0 #22d3ee; }
1049.dbg-var.traced .dbg-why { opacity:1; color:#22d3ee; }
1050.dbg-why { margin-left:auto; display:inline-flex; align-items:center; color:#6b7280; opacity:0; transition:opacity 0.12s; }
1051.dbg-prov { margin:8px 6px 4px; border:1px solid rgba(34,211,238,0.25); border-radius:8px; background:rgba(34,211,238,0.04); overflow:hidden; }
1052.dbg-prov-h { display:flex; align-items:center; justify-content:space-between; padding:5px 10px; background:rgba(34,211,238,0.08); border-bottom:1px solid rgba(34,211,238,0.15); }
1053.dbg-prov-title { display:flex; align-items:center; gap:6px; color:#67e8f9; font-size:11px; text-transform:uppercase; letter-spacing:0.5px; }
1054.dbg-prov-ico { display:inline-flex; align-items:center; color:#22d3ee; }
1055.dbg-prov-x { background:transparent; border:none; color:#6b7280; cursor:pointer; display:inline-flex; padding:2px; border-radius:4px; }
1056.dbg-prov-x:hover { background:rgba(248,113,113,0.15); color:#f87171; }
1057.dbg-prov-tree { padding:8px 10px; font-family:ui-monospace,monospace; font-size:12px; }
1058.cz-row { display:flex; align-items:center; gap:8px; padding:2px 0; white-space:nowrap; }
1059.cz-arm { color:#475569; }
1060.cz-val { color:#4ade80; font-weight:600; }
1061.cz-kind { color:#f0abfc; font-size:10px; opacity:0.8; }
1062.cz-how { color:#cbd5e1; opacity:0.85; }
1063.cz-step { color:#6b7280; font-size:10px; margin-left:auto; }
1064.dbg-osc-wrap { display:flex; flex-direction:column; gap:4px; }
1065.dbg-osc { display:flex; padding:4px 2px 8px; }
1066.dbg-osc-names { flex:none; }
1067.dbg-osc-scroll { flex:1 1 auto; min-width:0; overflow-x:auto; }
1068.dbg-mem-hint.dim { opacity:0.6; font-size:11px; }
1069.dbg-fol { margin-left:auto; display:inline-flex; align-items:center; gap:8px; min-width:0; flex-shrink:1; font-family:ui-monospace,monospace; }
1070.dbg-fol-tag { color:#a78bfa; font-size:13px; }
1071.dbg-fol-text { color:#ddd6fe; font-size:12.5px; white-space:nowrap; overflow:hidden; text-overflow:ellipsis; }
1072.dbg-prove { display:flex; flex-direction:column; gap:8px; padding:2px 4px; }
1073.dbg-prove-input { background:var(--studio-panel-bg,#12161c); border:1px solid var(--studio-border,rgba(255,255,255,0.12)); border-radius:6px; color:#e8eaed; font-family:ui-monospace,monospace; font-size:13px; padding:7px 10px; outline:none; }
1074.dbg-prove-input:focus { border-color:#a78bfa; }
1075.dbg-prove-result { display:flex; flex-direction:column; gap:6px; padding:4px 2px; }
1076.dbg-prove-row { display:flex; align-items:center; gap:9px; flex-wrap:wrap; }
1077.dbg-prove-label { color:#6b7280; font-size:10px; text-transform:uppercase; letter-spacing:0.6px; min-width:62px; }
1078.dbg-verdict { font-weight:600; font-size:12px; border-radius:10px; padding:1px 9px; }
1079.dbg-verdict.yes { color:#86efac; background:rgba(74,222,128,0.14); border:1px solid rgba(74,222,128,0.3); }
1080.dbg-verdict.no { color:#fca5a5; background:rgba(248,113,113,0.14); border:1px solid rgba(248,113,113,0.3); }
1081.dbg-verdict.idk { color:#9ca3af; background:rgba(156,163,175,0.12); border:1px solid rgba(156,163,175,0.25); }
1082.dbg-prove-detail { color:#94a3b8; font-family:ui-monospace,monospace; font-size:11px; }
1083.dbg-prove-err { color:#fbbf24; font-size:12px; padding:4px 2px; }
1084.dbg-insights { margin:2px 6px 6px; }
1085.dbg-insights-h { color:#67e8f9; font-size:10px; text-transform:uppercase; letter-spacing:0.6px; margin-bottom:5px; }
1086.dbg-insights-h.proven { color:#c4b5fd; display:flex; align-items:center; gap:5px; }
1087.dbg-proven-ico { display:inline-flex; align-items:center; color:#a78bfa; }
1088.dbg-insight { display:flex; align-items:center; gap:7px; flex-wrap:wrap; padding:2px 0; }
1089.dbg-insight-name { color:#a5b4fc; font-family:ui-monospace,monospace; font-size:12px; min-width:48px; }
1090.dbg-insight-fact { color:#86efac; background:rgba(74,222,128,0.1); border:1px solid rgba(74,222,128,0.2); border-radius:10px; padding:0 8px; font-size:11px; }
1091.dbg-insight-fact.proven { color:#ddd6fe; background:rgba(167,139,250,0.12); border-color:rgba(167,139,250,0.3); font-family:ui-monospace,monospace; }
1092.dbg-globals-h { color:var(--studio-text-muted,#6b7280); margin-top:8px; text-transform:uppercase; font-size:10px; letter-spacing:0.5px; }
1093.dbg-frame { padding:3px 6px; border-radius:4px; }
1094.dbg-frame.current { background:rgba(102,126,234,0.18); }
1095.dbg-frame-fn { color:#a5b4fc; }
1096.dbg-bp, .dbg-row { display:flex; gap:8px; align-items:center; padding:2px 4px; cursor:pointer; border-radius:3px; }
1097.dbg-bp:hover, .dbg-row:hover { background:rgba(255,255,255,0.05); }
1098.dbg-row.cur { background:rgba(102,126,234,0.22); }
1099.dbg-gutter { width:12px; color:#f87171; text-align:center; }
1100.dbg-pc, .dbg-bp-pc { color:var(--studio-text-muted,#6b7280); min-width:28px; text-align:right; }
1101.dbg-text, .dbg-bp-op { color:var(--studio-text,#e8eaed); }
1102.dbg-bp-dot { color:#f87171; }
1103.dbg-error { padding:12px; color:#f87171; }
1104.dbg-hw { padding:10px 12px; border-bottom:1px solid var(--studio-border,rgba(255,255,255,0.08)); color:var(--studio-text-secondary,#9ca3af); font-style:italic; }
1105.dbg-btn { display:inline-flex; align-items:center; justify-content:center; }
1106.dbg-btn svg { display:block; }
1107.dbg-title { display:inline-flex; align-items:center; gap:6px; }
1108.dbg-bug { display:inline-flex; color:#f87171; }
1109.dbg-divider { width:1px; height:18px; background:var(--studio-border,rgba(255,255,255,0.12)); margin:0 3px; align-self:center; }
1110.dbg-rev { color:#c084fc; }
1111.dbg-scrub { display:flex; align-items:center; gap:10px; padding:5px 12px; border-bottom:1px solid var(--studio-border,rgba(255,255,255,0.08)); }
1112.dbg-scrub-cap { color:var(--studio-text-muted,#6b7280); font-size:13px; }
1113.dbg-scrub-range { flex:1; accent-color:var(--studio-accent,#667eea); height:4px; cursor:pointer; }
1114.dbg-scrub-label { color:var(--studio-text-secondary,#9ca3af); font-family:ui-monospace,monospace; font-size:11px; min-width:62px; text-align:right; }
1115.dbg-narr { display:flex; align-items:center; gap:9px; padding:8px 14px; background:linear-gradient(90deg,rgba(102,126,234,0.12),rgba(102,126,234,0.03)); border-bottom:1px solid var(--studio-border,rgba(255,255,255,0.08)); }
1116.dbg-narr-ico { display:inline-flex; color:#fbbf24; flex-shrink:0; }
1117.dbg-narr-text { color:var(--studio-text,#e8eaed); font-size:13.5px; flex:0 1 auto; min-width:0; white-space:nowrap; overflow:hidden; text-overflow:ellipsis; }
1118.dbg-narr-done .dbg-narr-text, .dbg-narr-error .dbg-narr-text { font-style:italic; color:var(--studio-text-secondary,#9ca3af); }
1119.dbg-narr-error .dbg-narr-ico { color:#f87171; }
1120.dbg-narr-socratic { background:linear-gradient(90deg,rgba(34,211,238,0.13),rgba(34,211,238,0.03)); }
1121.dbg-narr-socratic .dbg-narr-ico { color:#22d3ee; }
1122.dbg-narr-socratic .dbg-narr-text { color:#cffafe; }
1123.dbg-play { color:#4ade80; }
1124.dbg-mem-hint { color:var(--studio-text-muted,#6b7280); font-size:11px; margin-bottom:8px; }
1125.dbg-sframe { margin-bottom:8px; border:1px solid var(--studio-border,rgba(255,255,255,0.08)); border-radius:6px; overflow:hidden; }
1126.dbg-sframe-h { display:flex; justify-content:space-between; padding:4px 8px; background:rgba(255,255,255,0.04); }
1127.dbg-sframe-fn { color:#a5b4fc; font-weight:600; }
1128.dbg-sframe-base { color:var(--studio-text-muted,#6b7280); font-family:ui-monospace,monospace; font-size:11px; }
1129.dbg-slot { display:flex; gap:10px; padding:1px 8px; }
1130.dbg-slot.changed { background:rgba(102,126,234,0.18); }
1131.dbg-slot-addr { color:#6b7280; font-family:ui-monospace,monospace; min-width:36px; }
1132.dbg-slot-name { color:#cbd5e1; min-width:50px; }
1133.dbg-slot-type { color:#f0abfc; font-size:10px; opacity:0.75; min-width:42px; }
1134.dbg-slot-val { color:#4ade80; }
1135.dbg-heap { display:flex; flex-direction:column; gap:8px; }
1136.dbg-hobj { border:1px solid var(--studio-border,rgba(255,255,255,0.12)); border-radius:7px; padding:6px 9px; background:rgba(255,255,255,0.02); }
1137.dbg-hobj.shared { border-color:#fbbf24; background:rgba(251,191,36,0.06); }
1138.dbg-hobj-h { display:flex; align-items:center; gap:8px; }
1139.dbg-hobj-id { color:#22d3ee; font-family:ui-monospace,monospace; font-weight:600; }
1140.dbg-hobj-kind { color:var(--studio-text-secondary,#9ca3af); text-transform:uppercase; font-size:10px; letter-spacing:0.5px; }
1141.dbg-hobj-store { color:#22d3ee; font-family:ui-monospace,monospace; font-size:10px; background:rgba(34,211,238,0.1); padding:0 5px; border-radius:3px; }
1142.dbg-hobj-rc { color:#9ca3af; font-size:10px; }
1143.dbg-hobj-alias { color:#fbbf24; font-size:10px; text-transform:uppercase; letter-spacing:0.5px; }
1144.dbg-hobj-val { color:#86efac; margin:3px 0; word-break:break-all; }
1145.dbg-hobj-refs { display:flex; align-items:center; gap:5px; flex-wrap:wrap; }
1146.dbg-refs-label { color:#6b7280; }
1147.dbg-ref { background:rgba(102,126,234,0.2); color:#a5b4fc; padding:0 6px; border-radius:4px; font-size:11px; }
1148.dbg-close { display:inline-flex; align-items:center; justify-content:center; background:transparent; border:none; color:var(--studio-text-muted,#6b7280); cursor:pointer; padding:4px 5px; border-radius:5px; margin-left:8px; }
1149.dbg-close:hover { background:rgba(248,113,113,0.15); color:#f87171; }
1150.dbg-reload { display:flex; align-items:center; gap:12px; padding:7px 14px; background:rgba(251,191,36,0.1); border-bottom:1px solid var(--studio-border,rgba(255,255,255,0.08)); }
1151.dbg-reload-msg { color:#fbbf24; font-size:12.5px; flex:1; }
1152.dbg-reload-btn { background:#fbbf24; color:#1a1f27; border:none; border-radius:5px; padding:3px 12px; font-weight:600; cursor:pointer; font-size:12px; }
1153.dbg-reload-btn:hover { filter:brightness(1.1); }
1154"#;
1155
1156#[cfg(test)]
1157#[path = "../../../../../crates/logicaffeine_tests/unit/logicaffeine_web/ui/components/debug_drawer/tests.rs"]
1158mod tests;