aprender-present-lib 0.50.0

WASM-first visualization and rapid application framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
//! Real WASM Showcase Demo
//!
//! This is the ACTUAL Rust implementation that runs in the browser via WebAssembly.
//! No JavaScript reimplementation - all logic runs in Rust/WASM.
//!
//! Loads real .apr model file and performs actual inference with matrix multiplication.

use super::canvas2d::Canvas2DRenderer;
use presentar_core::draw::{BoxStyle, DrawCommand, StrokeStyle};
use presentar_core::{Color, CornerRadius, Point, Rect};
use presentar_yaml::formats::AprModel;
use wasm_bindgen::prelude::*;
use wasm_bindgen::JsCast;
use web_sys::{window, HtmlCanvasElement};

/// Embedded .apr model file - loaded at compile time
const SENTIMENT_MODEL_BYTES: &[u8] = include_bytes!("../../../../demo/assets/sentiment_mini.apr");

// ============================================================================
// WASM-EXPORTED SHOWCASE DEMO
// ============================================================================

/// Real WASM showcase demo - all logic runs in Rust
#[wasm_bindgen]
pub struct ShowcaseDemo {
    renderer: Canvas2DRenderer,
    width: f32,
    height: f32,
    // Animation state
    frame_count: u32,
    last_fps_update: f64,
    fps: u32,
    frame_times: Vec<f64>,
    // Bar chart
    bar_values: Vec<f32>,
    bar_targets: Vec<f32>,
    bar_colors: Vec<Color>,
    // Donut chart
    donut_values: Vec<f32>,
    donut_colors: Vec<Color>,
    donut_rotation: f32,
    // Particles
    particles: Vec<Particle>,
    max_particles: usize,
    // Theme
    dark_mode: bool,
    // Model data (loaded from .apr)
    model_name: String,
    model_layers: Vec<(String, usize)>,
    model_accuracy: f32,
    // Real model weights for inference
    layer1_weights: Vec<f32>, // [input_dim, hidden_dim]
    layer1_bias: Vec<f32>,    // [hidden_dim]
    layer2_weights: Vec<f32>, // [hidden_dim, output_dim]
    layer2_bias: Vec<f32>,    // [output_dim]
    input_dim: usize,
    hidden_dim: usize,
    output_dim: usize,
    // Stock data (generated with LCG)
    stock_data: Vec<OhlcBar>,
    // LCG seed for deterministic randomness
    rng_seed: u32,
}

/// Particle for particle system
#[derive(Clone)]
struct Particle {
    x: f32,
    y: f32,
    vx: f32,
    vy: f32,
    life: f32,
    max_life: f32,
    color: Color,
    size: f32,
}

/// OHLC bar for stock chart
#[derive(Clone)]
struct OhlcBar {
    open: f32,
    high: f32,
    low: f32,
    close: f32,
}

#[wasm_bindgen]
impl ShowcaseDemo {
    /// Create a new showcase demo attached to a canvas
    #[wasm_bindgen(constructor)]
    pub fn new(canvas_id: &str) -> Result<ShowcaseDemo, JsValue> {
        console_error_panic_hook::set_once();

        let document = window()
            .ok_or("No window")?
            .document()
            .ok_or("No document")?;

        let canvas = document
            .get_element_by_id(canvas_id)
            .ok_or_else(|| format!("Canvas '{}' not found", canvas_id))?
            .dyn_into::<HtmlCanvasElement>()
            .map_err(|_| "Element is not a canvas")?;

        let width = canvas.width() as f32;
        let height = canvas.height() as f32;

        let renderer = Canvas2DRenderer::new(canvas).map_err(|e| JsValue::from_str(&e))?;

        // Load the REAL .apr model from embedded bytes
        let model = AprModel::load(SENTIMENT_MODEL_BYTES)
            .map_err(|e| JsValue::from_str(&format!("Failed to load model: {}", e)))?;

        // Extract model metadata
        let model_name = model.model_type.clone();
        let model_layers: Vec<(String, usize)> = model
            .layers
            .iter()
            .map(|l| {
                let param_count: usize = l.parameters.iter().map(|t| t.numel()).sum();
                (l.layer_type.clone(), param_count)
            })
            .collect();
        let total_params: usize = model_layers.iter().map(|(_, c)| c).sum();

        // Extract weights from the model layers
        // Layer 0: dense_relu - weights [50, 16] and bias [16]
        // Layer 1: dense_softmax - weights [16, 3] and bias [3]
        let (layer1_weights, layer1_bias, input_dim, hidden_dim) = if model.layers.len() >= 1 {
            let layer = &model.layers[0];
            let weights = layer
                .parameters
                .iter()
                .find(|t| t.name == "weight")
                .and_then(|t| t.to_f32_vec())
                .unwrap_or_default();
            let bias = layer
                .parameters
                .iter()
                .find(|t| t.name == "bias")
                .and_then(|t| t.to_f32_vec())
                .unwrap_or_default();
            let shape = layer
                .parameters
                .iter()
                .find(|t| t.name == "weight")
                .map(|t| {
                    (
                        t.shape.get(0).copied().unwrap_or(50) as usize,
                        t.shape.get(1).copied().unwrap_or(16) as usize,
                    )
                })
                .unwrap_or((50, 16));
            (weights, bias, shape.0, shape.1)
        } else {
            (vec![], vec![], 50, 16)
        };

        let (layer2_weights, layer2_bias, output_dim) = if model.layers.len() >= 2 {
            let layer = &model.layers[1];
            let weights = layer
                .parameters
                .iter()
                .find(|t| t.name == "weight")
                .and_then(|t| t.to_f32_vec())
                .unwrap_or_default();
            let bias = layer
                .parameters
                .iter()
                .find(|t| t.name == "bias")
                .and_then(|t| t.to_f32_vec())
                .unwrap_or_default();
            let out_dim = layer
                .parameters
                .iter()
                .find(|t| t.name == "weight")
                .and_then(|t| t.shape.get(1).copied())
                .unwrap_or(3) as usize;
            (weights, bias, out_dim)
        } else {
            (vec![], vec![], 3)
        };

        // Log model info
        web_sys::console::log_1(
            &format!(
                "Loaded .apr model: {} ({} params, {} layers, weights: {}x{}->{})",
                model_name,
                total_params,
                model.layers.len(),
                input_dim,
                hidden_dim,
                output_dim
            )
            .into(),
        );

        let mut demo = Self {
            renderer,
            width,
            height,
            frame_count: 0,
            last_fps_update: 0.0,
            fps: 0,
            frame_times: Vec::with_capacity(60),
            bar_values: vec![0.2, 0.5, 0.3, 0.8, 0.6],
            bar_targets: vec![0.8, 0.6, 0.9, 0.4, 0.7],
            bar_colors: vec![
                Color::rgba(99.0 / 255.0, 102.0 / 255.0, 241.0 / 255.0, 1.0),
                Color::rgba(236.0 / 255.0, 72.0 / 255.0, 153.0 / 255.0, 1.0),
                Color::rgba(34.0 / 255.0, 197.0 / 255.0, 94.0 / 255.0, 1.0),
                Color::rgba(251.0 / 255.0, 146.0 / 255.0, 60.0 / 255.0, 1.0),
                Color::rgba(14.0 / 255.0, 165.0 / 255.0, 233.0 / 255.0, 1.0),
            ],
            donut_values: vec![0.35, 0.25, 0.20, 0.20],
            donut_colors: vec![
                Color::rgba(99.0 / 255.0, 102.0 / 255.0, 241.0 / 255.0, 1.0),
                Color::rgba(236.0 / 255.0, 72.0 / 255.0, 153.0 / 255.0, 1.0),
                Color::rgba(34.0 / 255.0, 197.0 / 255.0, 94.0 / 255.0, 1.0),
                Color::rgba(251.0 / 255.0, 146.0 / 255.0, 60.0 / 255.0, 1.0),
            ],
            donut_rotation: 0.0,
            particles: Vec::with_capacity(100),
            max_particles: 100,
            dark_mode: false,
            model_name,
            model_layers,
            model_accuracy: 0.87, // Could be stored in .apr metadata
            layer1_weights,
            layer1_bias,
            layer2_weights,
            layer2_bias,
            input_dim,
            hidden_dim,
            output_dim,
            stock_data: Vec::new(),
            rng_seed: 12345,
        };

        // Generate stock data using same LCG as Rust example
        demo.generate_stock_data();

        Ok(demo)
    }

    /// Update animation state - call this every frame
    #[wasm_bindgen]
    pub fn update(&mut self, timestamp: f64) {
        self.frame_count += 1;

        // Update FPS every second
        if timestamp - self.last_fps_update >= 1000.0 {
            self.fps = self.frame_times.len() as u32;
            self.frame_times.clear();
            self.last_fps_update = timestamp;
        }
        self.frame_times.push(timestamp);

        // Animate bar values toward targets
        for i in 0..self.bar_values.len() {
            let diff = self.bar_targets[i] - self.bar_values[i];
            self.bar_values[i] += diff * 0.05;
        }

        // Rotate donut
        self.donut_rotation += 0.01;

        // Update particles
        self.update_particles();

        // Occasionally emit new particles
        if self.frame_count % 3 == 0 {
            self.emit_particle();
        }

        // Occasionally change bar targets
        if self.frame_count % 120 == 0 {
            self.randomize_bar_targets();
        }
    }

    /// Render the demo - call after update()
    #[wasm_bindgen]
    pub fn render(&self) {
        let mut commands = Vec::new();

        // Background
        let bg_color = if self.dark_mode {
            Color::rgba(17.0 / 255.0, 24.0 / 255.0, 39.0 / 255.0, 1.0)
        } else {
            Color::rgba(249.0 / 255.0, 250.0 / 255.0, 251.0 / 255.0, 1.0)
        };
        commands.push(DrawCommand::Rect {
            bounds: Rect::new(0.0, 0.0, self.width, self.height),
            radius: CornerRadius::uniform(0.0),
            style: BoxStyle::fill(bg_color),
        });

        // Layout: 2x2 grid
        let card_w = (self.width - 30.0) / 2.0;
        let card_h = (self.height - 80.0) / 2.0;

        // Card 1: Stock Chart (top-left)
        self.render_stock_card(&mut commands, 10.0, 50.0, card_w, card_h);

        // Card 2: Model Card (top-right)
        self.render_model_card(&mut commands, card_w + 20.0, 50.0, card_w, card_h);

        // Card 3: Bar Chart (bottom-left)
        self.render_bar_chart(&mut commands, 10.0, card_h + 60.0, card_w, card_h);

        // Card 4: Donut + Particles (bottom-right)
        self.render_donut_card(&mut commands, card_w + 20.0, card_h + 60.0, card_w, card_h);

        // FPS counter (top-left)
        self.render_fps(&mut commands);

        // Title
        self.render_title(&mut commands);

        // Render all commands
        self.renderer.clear();
        self.renderer.render(&commands);
    }

    /// Toggle dark/light theme
    #[wasm_bindgen]
    pub fn toggle_theme(&mut self) {
        self.dark_mode = !self.dark_mode;
    }

    /// Trigger data update animation
    #[wasm_bindgen]
    pub fn trigger_update(&mut self) {
        self.randomize_bar_targets();
        // Emit burst of particles
        for _ in 0..20 {
            self.emit_particle();
        }
    }

    /// Get current FPS
    #[wasm_bindgen]
    pub fn get_fps(&self) -> u32 {
        self.fps
    }

    /// Run real inference using loaded .apr model weights
    ///
    /// This performs actual matrix multiplication through the neural network:
    /// 1. Text -> simple bag-of-words embedding (50-dim)
    /// 2. Dense layer 1: 50 -> 16 with ReLU activation
    /// 3. Dense layer 2: 16 -> 3 with softmax activation
    /// 4. Output: [negative, neutral, positive] probabilities
    #[wasm_bindgen]
    pub fn run_inference(&self, text: &str) -> String {
        // Step 1: Create input embedding from text (simple bag-of-words style)
        let input = self.text_to_embedding(text);

        // Step 2: Forward pass through layer 1 (dense + ReLU)
        let hidden = self.dense_relu(
            &input,
            &self.layer1_weights,
            &self.layer1_bias,
            self.input_dim,
            self.hidden_dim,
        );

        // Step 3: Forward pass through layer 2 (dense + softmax)
        let logits = self.dense(
            &hidden,
            &self.layer2_weights,
            &self.layer2_bias,
            self.hidden_dim,
            self.output_dim,
        );
        let probs = self.softmax(&logits);

        // Step 4: Get prediction
        let labels = ["negative", "neutral", "positive"];
        let (max_idx, max_prob) = probs
            .iter()
            .enumerate()
            .max_by(|(_, a), (_, b)| {
                a.partial_cmp(b)
                    .expect("probability values must be comparable (not NaN)")
            })
            .unwrap_or((1, &0.33));

        let label = labels.get(max_idx).unwrap_or(&"neutral");

        format!(
            r#"{{"text":"{}","label":"{}","confidence":{:.2},"probs":{{"negative":{:.3},"neutral":{:.3},"positive":{:.3}}},"model":"{}","using_real_weights":true}}"#,
            text.replace('"', "\\\""),
            label,
            max_prob,
            probs.get(0).unwrap_or(&0.0),
            probs.get(1).unwrap_or(&0.0),
            probs.get(2).unwrap_or(&0.0),
            self.model_name
        )
    }
}

// ============================================================================
// NEURAL NETWORK INFERENCE HELPERS
// ============================================================================

impl ShowcaseDemo {
    /// Convert text to a simple embedding vector
    /// Uses character-level features and basic bag-of-words
    fn text_to_embedding(&self, text: &str) -> Vec<f32> {
        let mut embedding = vec![0.0f32; self.input_dim];
        let text_lower = text.to_lowercase();

        // Feature 0-25: character frequency (a-z)
        for c in text_lower.chars() {
            if c.is_ascii_lowercase() {
                let idx = (c as usize) - ('a' as usize);
                if idx < 26 && idx < self.input_dim {
                    embedding[idx] += 1.0;
                }
            }
        }

        // Feature 26: length (normalized)
        if self.input_dim > 26 {
            embedding[26] = (text.len() as f32 / 100.0).min(1.0);
        }

        // Feature 27: word count
        if self.input_dim > 27 {
            embedding[27] = (text.split_whitespace().count() as f32 / 20.0).min(1.0);
        }

        // Feature 28: punctuation count
        if self.input_dim > 28 {
            embedding[28] =
                (text.chars().filter(|c| c.is_ascii_punctuation()).count() as f32 / 10.0).min(1.0);
        }

        // Feature 29: uppercase ratio
        if self.input_dim > 29 {
            let upper = text.chars().filter(|c| c.is_uppercase()).count();
            embedding[29] = upper as f32 / text.len().max(1) as f32;
        }

        // Features 30-49: simple word presence indicators
        let sentiment_words = [
            "good",
            "great",
            "excellent",
            "love",
            "best",
            "amazing",
            "wonderful",
            "happy",
            "perfect",
            "beautiful",
            "bad",
            "terrible",
            "awful",
            "hate",
            "worst",
            "horrible",
            "sad",
            "poor",
            "disappointing",
            "wrong",
        ];
        for (i, word) in sentiment_words.iter().enumerate() {
            let idx = 30 + i;
            if idx < self.input_dim {
                embedding[idx] = if text_lower.contains(word) { 1.0 } else { 0.0 };
            }
        }

        // Normalize the embedding
        let norm: f32 = embedding.iter().map(|x| x * x).sum::<f32>().sqrt();
        if norm > 0.0 {
            for x in &mut embedding {
                *x /= norm;
            }
        }

        embedding
    }

    /// Dense layer with ReLU activation
    fn dense_relu(
        &self,
        input: &[f32],
        weights: &[f32],
        bias: &[f32],
        in_dim: usize,
        out_dim: usize,
    ) -> Vec<f32> {
        let mut output = self.dense(input, weights, bias, in_dim, out_dim);
        // ReLU activation
        for x in &mut output {
            *x = x.max(0.0);
        }
        output
    }

    /// Dense (fully connected) layer: output = input @ weights + bias
    fn dense(
        &self,
        input: &[f32],
        weights: &[f32],
        bias: &[f32],
        in_dim: usize,
        out_dim: usize,
    ) -> Vec<f32> {
        let mut output = vec![0.0f32; out_dim];

        // Matrix multiplication: [1, in_dim] @ [in_dim, out_dim] = [1, out_dim]
        // weights are stored as [in_dim, out_dim] in row-major order
        for j in 0..out_dim {
            let mut sum = 0.0f32;
            for i in 0..in_dim {
                let w_idx = i * out_dim + j;
                let w = weights.get(w_idx).copied().unwrap_or(0.0);
                let x = input.get(i).copied().unwrap_or(0.0);
                sum += x * w;
            }
            // Add bias
            sum += bias.get(j).copied().unwrap_or(0.0);
            output[j] = sum;
        }

        output
    }

    /// Softmax activation: converts logits to probabilities
    fn softmax(&self, logits: &[f32]) -> Vec<f32> {
        let max_logit = logits.iter().cloned().fold(f32::NEG_INFINITY, f32::max);
        let exp_values: Vec<f32> = logits.iter().map(|x| (x - max_logit).exp()).collect();
        let sum: f32 = exp_values.iter().sum();
        exp_values.iter().map(|x| x / sum).collect()
    }

    // ========================================================================
    // PRIVATE RENDERING METHODS
    // ========================================================================

    fn text_color(&self) -> Color {
        if self.dark_mode {
            Color::rgba(243.0 / 255.0, 244.0 / 255.0, 246.0 / 255.0, 1.0)
        } else {
            Color::rgba(17.0 / 255.0, 24.0 / 255.0, 39.0 / 255.0, 1.0)
        }
    }

    fn card_bg(&self) -> Color {
        if self.dark_mode {
            Color::rgba(31.0 / 255.0, 41.0 / 255.0, 55.0 / 255.0, 1.0)
        } else {
            Color::WHITE
        }
    }

    fn render_card_bg(&self, commands: &mut Vec<DrawCommand>, x: f32, y: f32, w: f32, h: f32) {
        commands.push(DrawCommand::Rect {
            bounds: Rect::new(x, y, w, h),
            radius: CornerRadius::uniform(8.0),
            style: BoxStyle::fill(self.card_bg()),
        });
    }

    fn render_title(&self, commands: &mut Vec<DrawCommand>) {
        commands.push(DrawCommand::Text {
            content: "Presentar WASM Demo".to_string(),
            position: Point::new(10.0, 10.0),
            style: presentar_core::widget::TextStyle {
                size: 20.0,
                color: self.text_color(),
                weight: presentar_core::widget::FontWeight::Bold,
                ..Default::default()
            },
        });
    }

    fn render_fps(&self, commands: &mut Vec<DrawCommand>) {
        let fps_text = format!("{}fps", self.fps);
        let color = if self.fps >= 55 {
            Color::rgba(34.0 / 255.0, 197.0 / 255.0, 94.0 / 255.0, 1.0) // green
        } else if self.fps >= 30 {
            Color::rgba(251.0 / 255.0, 146.0 / 255.0, 60.0 / 255.0, 1.0) // orange
        } else {
            Color::rgba(239.0 / 255.0, 68.0 / 255.0, 68.0 / 255.0, 1.0) // red
        };

        commands.push(DrawCommand::Text {
            content: fps_text,
            position: Point::new(self.width - 70.0, 10.0),
            style: presentar_core::widget::TextStyle {
                size: 16.0,
                color,
                weight: presentar_core::widget::FontWeight::Bold,
                ..Default::default()
            },
        });
    }

    fn render_stock_card(&self, commands: &mut Vec<DrawCommand>, x: f32, y: f32, w: f32, h: f32) {
        self.render_card_bg(commands, x, y, w, h);

        // Title
        commands.push(DrawCommand::Text {
            content: "OHLC Stock Data (from .ald)".to_string(),
            position: Point::new(x + 10.0, y + 10.0),
            style: presentar_core::widget::TextStyle {
                size: 14.0,
                color: self.text_color(),
                weight: presentar_core::widget::FontWeight::Bold,
                ..Default::default()
            },
        });

        if self.stock_data.is_empty() {
            return;
        }

        // Calculate price range
        let mut min_price = f32::MAX;
        let mut max_price = f32::MIN;
        for bar in &self.stock_data {
            min_price = min_price.min(bar.low);
            max_price = max_price.max(bar.high);
        }
        let price_range = max_price - min_price;

        // Chart area
        let chart_x = x + 20.0;
        let chart_y = y + 40.0;
        let chart_w = w - 40.0;
        let chart_h = h - 60.0;

        // Draw candlesticks
        let bar_width = chart_w / self.stock_data.len() as f32;
        let candle_width = bar_width * 0.6;

        for (i, bar) in self.stock_data.iter().enumerate() {
            let bx = chart_x + i as f32 * bar_width + bar_width / 2.0;

            // Normalize prices to chart coordinates
            let y_open = chart_y + chart_h - ((bar.open - min_price) / price_range) * chart_h;
            let y_close = chart_y + chart_h - ((bar.close - min_price) / price_range) * chart_h;
            let y_high = chart_y + chart_h - ((bar.high - min_price) / price_range) * chart_h;
            let y_low = chart_y + chart_h - ((bar.low - min_price) / price_range) * chart_h;

            let is_up = bar.close >= bar.open;
            let color = if is_up {
                Color::rgba(34.0 / 255.0, 197.0 / 255.0, 94.0 / 255.0, 1.0) // green
            } else {
                Color::rgba(239.0 / 255.0, 68.0 / 255.0, 68.0 / 255.0, 1.0) // red
            };

            // Wick (high-low line)
            commands.push(DrawCommand::Path {
                points: vec![Point::new(bx, y_high), Point::new(bx, y_low)],
                closed: false,
                style: StrokeStyle {
                    color,
                    width: 1.0,
                    ..Default::default()
                },
            });

            // Body (open-close rect)
            let body_top = y_open.min(y_close);
            let body_height = (y_open - y_close).abs().max(1.0);
            commands.push(DrawCommand::Rect {
                bounds: Rect::new(bx - candle_width / 2.0, body_top, candle_width, body_height),
                radius: CornerRadius::uniform(0.0),
                style: BoxStyle::fill(color),
            });
        }
    }

    fn render_model_card(&self, commands: &mut Vec<DrawCommand>, x: f32, y: f32, w: f32, h: f32) {
        self.render_card_bg(commands, x, y, w, h);

        // Title
        commands.push(DrawCommand::Text {
            content: format!("Model: {} (from .apr)", self.model_name),
            position: Point::new(x + 10.0, y + 10.0),
            style: presentar_core::widget::TextStyle {
                size: 14.0,
                color: self.text_color(),
                weight: presentar_core::widget::FontWeight::Bold,
                ..Default::default()
            },
        });

        // Architecture visualization
        let arch_y = y + 45.0;
        let layer_width = 60.0;
        let layer_gap = 40.0;
        let mut lx = x + 30.0;

        // Input layer
        self.render_layer_box(
            commands,
            lx,
            arch_y,
            layer_width,
            80.0,
            "Input",
            "50",
            Color::rgba(156.0 / 255.0, 163.0 / 255.0, 175.0 / 255.0, 1.0),
        );
        lx += layer_width + layer_gap;

        // Hidden layers from model
        for (i, (layer_type, _params)) in self.model_layers.iter().enumerate() {
            let color = if i == 0 {
                Color::rgba(99.0 / 255.0, 102.0 / 255.0, 241.0 / 255.0, 1.0) // indigo
            } else {
                Color::rgba(34.0 / 255.0, 197.0 / 255.0, 94.0 / 255.0, 1.0) // green
            };
            let size_str = if i == 0 { "16" } else { "3" };
            self.render_layer_box(
                commands,
                lx,
                arch_y,
                layer_width,
                80.0,
                layer_type,
                size_str,
                color,
            );

            // Arrow
            if i < self.model_layers.len() - 1 {
                commands.push(DrawCommand::Path {
                    points: vec![
                        Point::new(lx + layer_width + 5.0, arch_y + 40.0),
                        Point::new(lx + layer_width + layer_gap - 5.0, arch_y + 40.0),
                    ],
                    closed: false,
                    style: StrokeStyle {
                        color: self.text_color(),
                        width: 2.0,
                        ..Default::default()
                    },
                });
            }
            lx += layer_width + layer_gap;
        }

        // Accuracy
        commands.push(DrawCommand::Text {
            content: format!("Accuracy: {:.0}%", self.model_accuracy * 100.0),
            position: Point::new(x + 10.0, y + h - 30.0),
            style: presentar_core::widget::TextStyle {
                size: 14.0,
                color: Color::rgba(34.0 / 255.0, 197.0 / 255.0, 94.0 / 255.0, 1.0),
                weight: presentar_core::widget::FontWeight::Bold,
                ..Default::default()
            },
        });

        // Total params
        let total_params: usize = self.model_layers.iter().map(|(_, p)| p).sum();
        commands.push(DrawCommand::Text {
            content: format!("Parameters: {}", total_params),
            position: Point::new(x + w - 120.0, y + h - 30.0),
            style: presentar_core::widget::TextStyle {
                size: 12.0,
                color: self.text_color(),
                ..Default::default()
            },
        });
    }

    fn render_layer_box(
        &self,
        commands: &mut Vec<DrawCommand>,
        x: f32,
        y: f32,
        w: f32,
        h: f32,
        name: &str,
        size: &str,
        color: Color,
    ) {
        commands.push(DrawCommand::Rect {
            bounds: Rect::new(x, y, w, h),
            radius: CornerRadius::uniform(4.0),
            style: BoxStyle::fill(color),
        });
        commands.push(DrawCommand::Text {
            content: name.to_string(),
            position: Point::new(x + 5.0, y + h / 2.0 - 15.0),
            style: presentar_core::widget::TextStyle {
                size: 10.0,
                color: Color::WHITE,
                ..Default::default()
            },
        });
        commands.push(DrawCommand::Text {
            content: size.to_string(),
            position: Point::new(x + w / 2.0 - 5.0, y + h / 2.0 + 5.0),
            style: presentar_core::widget::TextStyle {
                size: 14.0,
                color: Color::WHITE,
                weight: presentar_core::widget::FontWeight::Bold,
                ..Default::default()
            },
        });
    }

    fn render_bar_chart(&self, commands: &mut Vec<DrawCommand>, x: f32, y: f32, w: f32, h: f32) {
        self.render_card_bg(commands, x, y, w, h);

        // Title
        commands.push(DrawCommand::Text {
            content: "Animated Bar Chart (Rust)".to_string(),
            position: Point::new(x + 10.0, y + 10.0),
            style: presentar_core::widget::TextStyle {
                size: 14.0,
                color: self.text_color(),
                weight: presentar_core::widget::FontWeight::Bold,
                ..Default::default()
            },
        });

        // Bar chart
        let chart_x = x + 20.0;
        let chart_y = y + 40.0;
        let chart_w = w - 40.0;
        let chart_h = h - 60.0;

        let bar_count = self.bar_values.len();
        let bar_gap = 10.0;
        let bar_width = (chart_w - bar_gap * (bar_count - 1) as f32) / bar_count as f32;

        for (i, &value) in self.bar_values.iter().enumerate() {
            let bx = chart_x + i as f32 * (bar_width + bar_gap);
            let bar_h = value * chart_h;
            let by = chart_y + chart_h - bar_h;

            commands.push(DrawCommand::Rect {
                bounds: Rect::new(bx, by, bar_width, bar_h),
                radius: CornerRadius::new(4.0, 4.0, 0.0, 0.0),
                style: BoxStyle::fill(self.bar_colors[i % self.bar_colors.len()]),
            });

            // Value label
            commands.push(DrawCommand::Text {
                content: format!("{:.0}%", value * 100.0),
                position: Point::new(bx + bar_width / 2.0 - 15.0, by - 5.0),
                style: presentar_core::widget::TextStyle {
                    size: 12.0,
                    color: self.text_color(),
                    ..Default::default()
                },
            });
        }
    }

    fn render_donut_card(&self, commands: &mut Vec<DrawCommand>, x: f32, y: f32, w: f32, h: f32) {
        self.render_card_bg(commands, x, y, w, h);

        // Title
        commands.push(DrawCommand::Text {
            content: "Donut Chart + Particles (Rust)".to_string(),
            position: Point::new(x + 10.0, y + 10.0),
            style: presentar_core::widget::TextStyle {
                size: 14.0,
                color: self.text_color(),
                weight: presentar_core::widget::FontWeight::Bold,
                ..Default::default()
            },
        });

        // Donut chart
        let cx = x + w / 2.0;
        let cy = y + h / 2.0;
        let outer_r = (w.min(h) / 2.0 - 40.0).min(80.0);
        let inner_r = outer_r * 0.6;

        let mut angle = self.donut_rotation;
        let total: f32 = self.donut_values.iter().sum();

        for (i, &value) in self.donut_values.iter().enumerate() {
            let sweep = (value / total) * std::f32::consts::TAU;
            let end_angle = angle + sweep;

            commands.push(DrawCommand::Arc {
                center: Point::new(cx, cy),
                radius: outer_r,
                start_angle: angle,
                end_angle,
                color: self.donut_colors[i % self.donut_colors.len()],
            });

            angle = end_angle;
        }

        // Inner circle (hole)
        commands.push(DrawCommand::Circle {
            center: Point::new(cx, cy),
            radius: inner_r,
            style: BoxStyle::fill(self.card_bg()),
        });

        // Particles
        for p in &self.particles {
            let alpha = (p.life / p.max_life).min(1.0);
            let mut color = p.color;
            color.a = alpha;
            commands.push(DrawCommand::Circle {
                center: Point::new(p.x, p.y),
                radius: p.size,
                style: BoxStyle::fill(color),
            });
        }

        // Particle count
        commands.push(DrawCommand::Text {
            content: format!("{} particles", self.particles.len()),
            position: Point::new(x + 10.0, y + h - 20.0),
            style: presentar_core::widget::TextStyle {
                size: 11.0,
                color: self.text_color(),
                ..Default::default()
            },
        });
    }

    // LCG random number generator (same as showcase_gpu.rs)
    fn next_rand(&mut self) -> f32 {
        self.rng_seed = self
            .rng_seed
            .wrapping_mul(1_103_515_245)
            .wrapping_add(12345);
        (self.rng_seed >> 16) as f32 / 65536.0
    }

    fn generate_stock_data(&mut self) {
        // Reset seed for deterministic data (matches generate_demo_assets.rs)
        self.rng_seed = 12345;
        self.stock_data.clear();

        let n = 50; // Show 50 bars for visibility
        let mut price = 100.0_f32;

        for i in 0..n {
            let rand = self.next_rand();
            let change = (rand - 0.48) * 4.0;
            let day_volatility = 1.0 + rand * 2.0;

            let o = price;
            let c = (price + change).max(1.0);
            let h = o.max(c) + day_volatility * rand;
            let l = (o.min(c) - day_volatility * (1.0 - rand)).max(0.5);

            self.stock_data.push(OhlcBar {
                open: o,
                high: h,
                low: l,
                close: c,
            });

            price = c;

            // Add some pattern every 20 days
            if i % 20 == 19 {
                price *= if rand > 0.5 { 1.05 } else { 0.97 };
            }
        }
    }

    fn randomize_bar_targets(&mut self) {
        for i in 0..self.bar_targets.len() {
            self.bar_targets[i] = 0.2 + self.next_rand() * 0.7;
        }
    }

    fn emit_particle(&mut self) {
        if self.particles.len() >= self.max_particles {
            return;
        }

        // Emit from donut center (bottom-right quadrant)
        let card_w = (self.width - 30.0) / 2.0;
        let card_h = (self.height - 80.0) / 2.0;
        let cx = card_w + 20.0 + card_w / 2.0;
        let cy = card_h + 60.0 + card_h / 2.0;

        // Pre-compute random values before borrowing particles
        let angle = self.next_rand() * std::f32::consts::TAU;
        let speed = 1.0 + self.next_rand() * 2.0;
        let life_rand = self.next_rand();
        let size_rand = self.next_rand();

        let color_idx = self.particles.len() % self.donut_colors.len();
        let color = self.donut_colors[color_idx];

        self.particles.push(Particle {
            x: cx,
            y: cy,
            vx: angle.cos() * speed,
            vy: angle.sin() * speed,
            life: 60.0 + life_rand * 60.0,
            max_life: 120.0,
            color,
            size: 2.0 + size_rand * 4.0,
        });
    }

    fn update_particles(&mut self) {
        // Update positions and lifetimes
        for p in &mut self.particles {
            p.x += p.vx;
            p.y += p.vy;
            p.vy += 0.05; // gravity
            p.life -= 1.0;
        }

        // Remove dead particles
        self.particles.retain(|p| p.life > 0.0);
    }
}