strata-style 0.1.1

Pure Rust CSS engine, cascade solver, and Tailwind CSS JIT compiler
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
//! High-performance CSS keyframe animation runtime, track sampling, and iteration engine.

use std::collections::HashMap;

use crate::parser::{
    parse_angle_rad, parse_color, parse_dimension, parse_edge_insets_px, parse_transform_list,
    Declaration, KeyframeRule, StyleSheet,
};
use crate::transition::{
    apply_animatable_value, extract_animatable_value, interpolate_border_radius,
    interpolate_box_shadow, interpolate_color, interpolate_dimension, interpolate_edge_insets,
    interpolate_transform_ops, AnimatableValue,
};
use crate::types::{
    AnimationDirection, AnimationFillMode, AnimationIterationCount, AnimationPlayState,
    AnimationSpec, AnimationTimeline, ComputedStyle, Dimension, TransformOp, TransitionEasing,
    TransitionProperty,
};

/// A compiled stop in a keyframe animation timeline.
#[derive(Debug, Clone, PartialEq)]
pub struct CompiledKeyframeStop {
    /// Normalized timeline offset in [0.0, 1.0].
    pub offset: f32,
    /// Property values specified at this keyframe.
    pub properties: HashMap<TransitionProperty, AnimatableValue>,
    /// Optional segment easing starting from this keyframe.
    pub timing_function: Option<TransitionEasing>,
}

/// A compiled timeline track representing a named CSS `@keyframes` animation.
#[derive(Debug, Clone, PartialEq)]
pub struct KeyframeTrack {
    pub name: String,
    pub stops: Vec<CompiledKeyframeStop>,
}

impl KeyframeTrack {
    /// Constructs a compiled `KeyframeTrack` from a parsed `KeyframeRule`.
    pub fn from_rule(rule: &KeyframeRule) -> Self {
        let mut stops = Vec::with_capacity(rule.stops.len());

        for stop_rule in &rule.stops {
            let mut props = HashMap::new();
            for decl in &stop_rule.declarations {
                if let Some((prop, val)) = declaration_to_animatable(decl) {
                    props.insert(prop, val);
                }
            }
            stops.push(CompiledKeyframeStop {
                offset: stop_rule.offset,
                properties: props,
                timing_function: stop_rule.timing_function,
            });
        }

        stops.sort_by(|a, b| {
            a.offset
                .partial_cmp(&b.offset)
                .unwrap_or(std::cmp::Ordering::Equal)
        });

        Self {
            name: rule.name.clone(),
            stops,
        }
    }

    /// Evaluates and samples animated property values at progress fraction `progress` in [0.0, 1.0].
    pub fn sample(
        &self,
        progress: f32,
        base_style: &ComputedStyle,
    ) -> HashMap<TransitionProperty, AnimatableValue> {
        let mut result = HashMap::new();
        if self.stops.is_empty() {
            return result;
        }

        let p = progress.clamp(0.0, 1.0);
        let first = &self.stops[0];
        let last = &self.stops[self.stops.len() - 1];

        if p <= first.offset {
            return first.properties.clone();
        }
        if p >= last.offset {
            return last.properties.clone();
        }

        let mut k1 = first;
        let mut k2 = last;

        for i in 0..self.stops.len() - 1 {
            if self.stops[i].offset <= p && p <= self.stops[i + 1].offset {
                k1 = &self.stops[i];
                k2 = &self.stops[i + 1];
                break;
            }
        }

        let span = k2.offset - k1.offset;
        let mut alpha = if span > 0.0 {
            (p - k1.offset) / span
        } else {
            0.0
        };

        // Apply segment timing function if specified on the starting keyframe
        if let Some(easing) = k1.timing_function {
            alpha = easing.solve(alpha);
        }

        // Collect all distinct property keys present across k1, k2, or the base style
        let mut all_keys = std::collections::HashSet::new();
        for k in k1.properties.keys() {
            all_keys.insert(k.clone());
        }
        for k in k2.properties.keys() {
            all_keys.insert(k.clone());
        }

        for key in all_keys {
            let v1 = k1
                .properties
                .get(&key)
                .cloned()
                .or_else(|| extract_animatable_value(base_style, &key));
            let v2 = k2
                .properties
                .get(&key)
                .cloned()
                .or_else(|| extract_animatable_value(base_style, &key));

            if let (Some(val1), Some(val2)) = (v1, v2) {
                let interpolated = interpolate_animatable_value(&val1, &val2, alpha);
                result.insert(key, interpolated);
            }
        }

        result
    }
}

/// Converts a single CSS `Declaration` into an animatable property and value pair.
fn declaration_to_animatable(decl: &Declaration) -> Option<(TransitionProperty, AnimatableValue)> {
    let prop_name = decl.property.as_str();
    let val_str = decl.value.as_str();

    match prop_name {
        "opacity" => {
            let n = val_str.parse::<f32>().ok()?;
            Some((TransitionProperty::Opacity, AnimatableValue::Number(n)))
        }
        "transform" => {
            let ops = parse_transform_list(val_str);
            Some((
                TransitionProperty::Transform,
                AnimatableValue::Transform(ops),
            ))
        }
        "rotate" => {
            let rad = parse_angle_rad(val_str)?;
            Some((
                TransitionProperty::Transform,
                AnimatableValue::Transform(vec![TransformOp::Rotate(rad)]),
            ))
        }
        "scale" => {
            let parts: Vec<&str> = val_str.split_whitespace().collect();
            let first = parts.first().and_then(|s| s.parse::<f32>().ok())?;
            let second = parts
                .get(1)
                .and_then(|s| s.parse::<f32>().ok())
                .unwrap_or(first);
            Some((
                TransitionProperty::Transform,
                AnimatableValue::Transform(vec![TransformOp::Scale(first, second)]),
            ))
        }
        "translate" => {
            let parts: Vec<&str> = val_str.split_whitespace().collect();
            let dx = parts.first().and_then(|s| parse_dimension(s))?;
            let dy = parts
                .get(1)
                .and_then(|s| parse_dimension(s))
                .unwrap_or(Dimension::Px(0.0));
            Some((
                TransitionProperty::Transform,
                AnimatableValue::Transform(vec![TransformOp::Translate(dx, dy)]),
            ))
        }
        "width" => {
            let d = parse_dimension(val_str)?;
            Some((TransitionProperty::Width, AnimatableValue::Dimension(d)))
        }
        "height" => {
            let d = parse_dimension(val_str)?;
            Some((TransitionProperty::Height, AnimatableValue::Dimension(d)))
        }
        "color" => {
            let c = parse_color(val_str)?;
            Some((TransitionProperty::Color, AnimatableValue::Color(c)))
        }
        "background-color" | "background" => {
            let c = parse_color(val_str)?;
            Some((
                TransitionProperty::BackgroundColor,
                AnimatableValue::Color(c),
            ))
        }
        "border-color" => {
            let c = parse_color(val_str)?;
            Some((TransitionProperty::BorderColor, AnimatableValue::Color(c)))
        }
        "border-radius" => {
            let insets = parse_edge_insets_px(val_str)?;
            let br = crate::types::BorderRadius::new(
                insets.top,
                insets.right,
                insets.bottom,
                insets.left,
            );
            Some((
                TransitionProperty::BorderRadius,
                AnimatableValue::BorderRadius(br),
            ))
        }
        "margin" => {
            let m = parse_edge_insets_px(val_str)?;
            Some((TransitionProperty::Margin, AnimatableValue::EdgeInsets(m)))
        }
        "padding" => {
            let p = parse_edge_insets_px(val_str)?;
            Some((TransitionProperty::Padding, AnimatableValue::EdgeInsets(p)))
        }
        "clip-path" => {
            let cp = crate::parser::parse_clip_path(val_str)?;
            Some((TransitionProperty::ClipPath, AnimatableValue::ClipPath(cp)))
        }
        _ => None,
    }
}

/// Interpolates two `AnimatableValue` variants given normalized progress in [0.0, 1.0].
pub fn interpolate_animatable_value(
    v1: &AnimatableValue,
    v2: &AnimatableValue,
    progress: f32,
) -> AnimatableValue {
    match (v1, v2) {
        (AnimatableValue::Number(a), AnimatableValue::Number(b)) => {
            AnimatableValue::Number(a + (b - a) * progress)
        }
        (AnimatableValue::Color(c1), AnimatableValue::Color(c2)) => {
            AnimatableValue::Color(interpolate_color(*c1, *c2, progress))
        }
        (AnimatableValue::Dimension(d1), AnimatableValue::Dimension(d2)) => {
            AnimatableValue::Dimension(interpolate_dimension(d1, d2, progress))
        }
        (AnimatableValue::EdgeInsets(e1), AnimatableValue::EdgeInsets(e2)) => {
            AnimatableValue::EdgeInsets(interpolate_edge_insets(e1, e2, progress))
        }
        (AnimatableValue::BorderRadius(r1), AnimatableValue::BorderRadius(r2)) => {
            AnimatableValue::BorderRadius(interpolate_border_radius(r1, r2, progress))
        }
        (AnimatableValue::BoxShadow(s1), AnimatableValue::BoxShadow(s2)) => {
            AnimatableValue::BoxShadow(interpolate_box_shadow(s1, s2, progress))
        }
        (AnimatableValue::Transform(t1), AnimatableValue::Transform(t2)) => {
            AnimatableValue::Transform(interpolate_transform_ops(t1, t2, progress))
        }
        (AnimatableValue::ClipPath(c1), AnimatableValue::ClipPath(c2)) => {
            AnimatableValue::ClipPath(crate::effects::interpolate_clip_path(c1, c2, progress))
        }
        _ => v2.clone(),
    }
}

/// Registry of available named `@keyframes` tracks (including default Tailwind animations).
#[derive(Debug, Clone, PartialEq)]
pub struct KeyframeRegistry {
    tracks: HashMap<String, KeyframeTrack>,
}

impl Default for KeyframeRegistry {
    fn default() -> Self {
        let mut reg = Self {
            tracks: HashMap::new(),
        };
        reg.register_builtin_tailwind_tracks();
        reg
    }
}

impl KeyframeRegistry {
    /// Creates a new `KeyframeRegistry` populated with standard Tailwind built-in animation tracks.
    pub fn new() -> Self {
        Self::default()
    }

    /// Registers the default Tailwind CSS keyframe animation tracks (`spin`, `ping`, `pulse`, `bounce`).
    fn register_builtin_tailwind_tracks(&mut self) {
        // 1. spin: from { transform: rotate(0deg); } to { transform: rotate(360deg); }
        let mut spin_0 = HashMap::new();
        spin_0.insert(
            TransitionProperty::Transform,
            AnimatableValue::Transform(vec![TransformOp::Rotate(0.0)]),
        );
        let mut spin_1 = HashMap::new();
        spin_1.insert(
            TransitionProperty::Transform,
            AnimatableValue::Transform(vec![TransformOp::Rotate(2.0 * std::f32::consts::PI)]),
        );
        self.tracks.insert(
            "spin".to_string(),
            KeyframeTrack {
                name: "spin".to_string(),
                stops: vec![
                    CompiledKeyframeStop {
                        offset: 0.0,
                        properties: spin_0,
                        timing_function: None,
                    },
                    CompiledKeyframeStop {
                        offset: 1.0,
                        properties: spin_1,
                        timing_function: None,
                    },
                ],
            },
        );

        // 2. ping: 75%, 100% { transform: scale(2); opacity: 0; } (0% defaults to scale(1), opacity: 1)
        let mut ping_0 = HashMap::new();
        ping_0.insert(
            TransitionProperty::Transform,
            AnimatableValue::Transform(vec![TransformOp::Scale(1.0, 1.0)]),
        );
        ping_0.insert(TransitionProperty::Opacity, AnimatableValue::Number(1.0));

        let mut ping_75 = HashMap::new();
        ping_75.insert(
            TransitionProperty::Transform,
            AnimatableValue::Transform(vec![TransformOp::Scale(2.0, 2.0)]),
        );
        ping_75.insert(TransitionProperty::Opacity, AnimatableValue::Number(0.0));

        self.tracks.insert(
            "ping".to_string(),
            KeyframeTrack {
                name: "ping".to_string(),
                stops: vec![
                    CompiledKeyframeStop {
                        offset: 0.0,
                        properties: ping_0,
                        timing_function: None,
                    },
                    CompiledKeyframeStop {
                        offset: 0.75,
                        properties: ping_75.clone(),
                        timing_function: None,
                    },
                    CompiledKeyframeStop {
                        offset: 1.0,
                        properties: ping_75,
                        timing_function: None,
                    },
                ],
            },
        );

        // 3. pulse: 50% { opacity: .5; }
        let mut pulse_0 = HashMap::new();
        pulse_0.insert(TransitionProperty::Opacity, AnimatableValue::Number(1.0));
        let mut pulse_50 = HashMap::new();
        pulse_50.insert(TransitionProperty::Opacity, AnimatableValue::Number(0.5));
        let mut pulse_100 = HashMap::new();
        pulse_100.insert(TransitionProperty::Opacity, AnimatableValue::Number(1.0));

        self.tracks.insert(
            "pulse".to_string(),
            KeyframeTrack {
                name: "pulse".to_string(),
                stops: vec![
                    CompiledKeyframeStop {
                        offset: 0.0,
                        properties: pulse_0,
                        timing_function: None,
                    },
                    CompiledKeyframeStop {
                        offset: 0.5,
                        properties: pulse_50,
                        timing_function: None,
                    },
                    CompiledKeyframeStop {
                        offset: 1.0,
                        properties: pulse_100,
                        timing_function: None,
                    },
                ],
            },
        );

        // 4. bounce: 0%, 100% { transform: translateY(-25%); } 50% { transform: none; }
        let mut bounce_up = HashMap::new();
        bounce_up.insert(
            TransitionProperty::Transform,
            AnimatableValue::Transform(vec![TransformOp::Translate(
                Dimension::Px(0.0),
                Dimension::Percent(-25.0),
            )]),
        );
        let mut bounce_down = HashMap::new();
        bounce_down.insert(
            TransitionProperty::Transform,
            AnimatableValue::Transform(vec![TransformOp::Translate(
                Dimension::Px(0.0),
                Dimension::Px(0.0),
            )]),
        );

        self.tracks.insert(
            "bounce".to_string(),
            KeyframeTrack {
                name: "bounce".to_string(),
                stops: vec![
                    CompiledKeyframeStop {
                        offset: 0.0,
                        properties: bounce_up.clone(),
                        timing_function: Some(TransitionEasing::CubicBezier(0.8, 0.0, 1.0, 1.0)),
                    },
                    CompiledKeyframeStop {
                        offset: 0.5,
                        properties: bounce_down,
                        timing_function: Some(TransitionEasing::CubicBezier(0.0, 0.0, 0.2, 1.0)),
                    },
                    CompiledKeyframeStop {
                        offset: 1.0,
                        properties: bounce_up,
                        timing_function: None,
                    },
                ],
            },
        );
    }

    /// Registers all parsed `@keyframes` rules from a stylesheet into this registry.
    pub fn register_stylesheet(&mut self, sheet: &StyleSheet) {
        for rule in &sheet.keyframes {
            self.register_rule(rule);
        }
    }

    /// Registers a single `KeyframeRule` into the registry.
    pub fn register_rule(&mut self, rule: &KeyframeRule) {
        let track = KeyframeTrack::from_rule(rule);
        self.tracks.insert(rule.name.clone(), track);
    }

    /// Registers an already compiled `KeyframeTrack`.
    pub fn register_track(&mut self, track: KeyframeTrack) {
        self.tracks.insert(track.name.clone(), track);
    }

    /// Looks up a registered track by animation name.
    pub fn get(&self, name: &str) -> Option<&KeyframeTrack> {
        self.tracks.get(name)
    }
}

/// Lifecycle events emitted during CSS keyframe animation execution (W3C CSS Animations Level 1).
#[derive(Debug, Clone, PartialEq)]
pub enum AnimationLifecycleEvent {
    Start {
        node_id: u64,
        name: String,
        elapsed_time_sec: f32,
    },
    Iteration {
        node_id: u64,
        name: String,
        elapsed_time_sec: f32,
    },
    End {
        node_id: u64,
        name: String,
        elapsed_time_sec: f32,
    },
    Cancel {
        node_id: u64,
        name: String,
        elapsed_time_sec: f32,
    },
}

/// An active running or paused instance of a named keyframe animation on a DOM node.
#[derive(Debug, Clone, PartialEq)]
pub struct ActiveKeyframeAnimation {
    pub name: String,
    pub spec: AnimationSpec,
    pub track: KeyframeTrack,
    pub start_time_ms: f64,
    pub current_iteration: u32,
    pub start_emitted: bool,
    pub end_emitted: bool,
    pub is_finished: bool,
    pub current_values: HashMap<TransitionProperty, AnimatableValue>,
}

impl ActiveKeyframeAnimation {
    /// Creates a new active keyframe animation starting at `now_ms`.
    pub fn new(spec: AnimationSpec, track: KeyframeTrack, now_ms: f64) -> Self {
        let name = spec.name.clone();
        Self {
            name,
            spec,
            track,
            start_time_ms: now_ms,
            current_iteration: 0,
            start_emitted: false,
            end_emitted: false,
            is_finished: false,
            current_values: HashMap::new(),
        }
    }

    /// Advances the animation to timestamp `now_ms` and computes updated animatable values.
    pub fn step(
        &mut self,
        node_id: u64,
        now_ms: f64,
        base_style: &ComputedStyle,
    ) -> Vec<AnimationLifecycleEvent> {
        let mut events = Vec::new();
        if self.is_finished {
            return events;
        }

        // If paused, maintain existing current values without advancing time
        if self.spec.play_state == AnimationPlayState::Paused {
            return events;
        }

        let elapsed_ms = (now_ms - self.start_time_ms).max(0.0) as f32;

        // 1. Delay phase
        if elapsed_ms < self.spec.delay_ms {
            if self.spec.fill_mode == AnimationFillMode::Backwards
                || self.spec.fill_mode == AnimationFillMode::Both
            {
                let start_progress = match self.spec.direction {
                    AnimationDirection::Reverse | AnimationDirection::AlternateReverse => 1.0,
                    _ => 0.0,
                };
                self.current_values = self.track.sample(start_progress, base_style);
            }
            return events;
        }

        let active_ms = elapsed_ms - self.spec.delay_ms;

        // 2. Start event
        if !self.start_emitted {
            self.start_emitted = true;
            events.push(AnimationLifecycleEvent::Start {
                node_id,
                name: self.name.clone(),
                elapsed_time_sec: 0.0,
            });
        }

        let dur = self.spec.duration_ms.max(1.0);
        let raw_iter = active_ms / dur;
        let iter_idx = raw_iter.floor() as u32;

        // 3. Completion check for finite iteration counts
        if let AnimationIterationCount::Count(max_count) = self.spec.iteration_count {
            if active_ms >= max_count * dur {
                self.is_finished = true;
                if !self.end_emitted {
                    self.end_emitted = true;
                    events.push(AnimationLifecycleEvent::End {
                        node_id,
                        name: self.name.clone(),
                        elapsed_time_sec: max_count * dur / 1000.0,
                    });
                }

                if self.spec.fill_mode == AnimationFillMode::Forwards
                    || self.spec.fill_mode == AnimationFillMode::Both
                {
                    // Compute final position taking direction into account
                    let final_iter_idx = (max_count - 0.0001).floor() as u32;
                    let final_frac = if max_count.fract() == 0.0 {
                        1.0
                    } else {
                        max_count.fract()
                    };
                    let solved_t = self.spec.timing_function.solve(final_frac);
                    let final_progress = match self.spec.direction {
                        AnimationDirection::Normal => solved_t,
                        AnimationDirection::Reverse => 1.0 - solved_t,
                        AnimationDirection::Alternate => {
                            if final_iter_idx.is_multiple_of(2) {
                                solved_t
                            } else {
                                1.0 - solved_t
                            }
                        }
                        AnimationDirection::AlternateReverse => {
                            if final_iter_idx.is_multiple_of(2) {
                                1.0 - solved_t
                            } else {
                                solved_t
                            }
                        }
                    };
                    self.current_values = self.track.sample(final_progress, base_style);
                } else {
                    self.current_values.clear();
                }

                return events;
            }
        }

        // 4. Iteration boundary event
        if iter_idx > self.current_iteration {
            self.current_iteration = iter_idx;
            events.push(AnimationLifecycleEvent::Iteration {
                node_id,
                name: self.name.clone(),
                elapsed_time_sec: (iter_idx as f32 * dur) / 1000.0,
            });
        }

        // 5. Active fractional progress calculation
        let frac = (active_ms % dur) / dur;
        let solved_t = self.spec.timing_function.solve(frac);

        let progress = match self.spec.direction {
            AnimationDirection::Normal => solved_t,
            AnimationDirection::Reverse => 1.0 - solved_t,
            AnimationDirection::Alternate => {
                if iter_idx.is_multiple_of(2) {
                    solved_t
                } else {
                    1.0 - solved_t
                }
            }
            AnimationDirection::AlternateReverse => {
                if iter_idx.is_multiple_of(2) {
                    1.0 - solved_t
                } else {
                    solved_t
                }
            }
        };

        self.current_values = self.track.sample(progress, base_style);

        events
    }

    /// Advances the animation according to a normalized timeline progress fraction in [0.0, 1.0] (for scroll-driven animations).
    pub fn step_progress(
        &mut self,
        node_id: u64,
        progress: f32,
        base_style: &ComputedStyle,
    ) -> Vec<AnimationLifecycleEvent> {
        let mut events = Vec::new();
        if self.spec.play_state == AnimationPlayState::Paused {
            return events;
        }

        if !self.start_emitted {
            self.start_emitted = true;
            events.push(AnimationLifecycleEvent::Start {
                node_id,
                name: self.name.clone(),
                elapsed_time_sec: 0.0,
            });
        }

        // Apply range boundaries if configured
        let range = &self.spec.range;
        let p_clamped = progress.clamp(0.0, 1.0);
        let range_span = (range.end_offset - range.start_offset).abs();
        let effective_p = if range_span > 0.0 {
            ((p_clamped - range.start_offset) / range_span).clamp(0.0, 1.0)
        } else {
            p_clamped
        };

        let solved_t = self.spec.timing_function.solve(effective_p);
        let final_progress = match self.spec.direction {
            AnimationDirection::Normal => solved_t,
            AnimationDirection::Reverse => 1.0 - solved_t,
            AnimationDirection::Alternate | AnimationDirection::AlternateReverse => solved_t,
        };

        self.current_values = self.track.sample(final_progress, base_style);

        if progress >= 1.0 && !self.end_emitted {
            self.end_emitted = true;
            events.push(AnimationLifecycleEvent::End {
                node_id,
                name: self.name.clone(),
                elapsed_time_sec: 0.0,
            });
        }

        events
    }
}

/// Comprehensive animation coordinator managing active CSS keyframe animations across DOM nodes.
#[derive(Debug, Default, Clone)]
pub struct AnimationManager {
    registry: KeyframeRegistry,
    node_animations: HashMap<u64, Vec<ActiveKeyframeAnimation>>,
}

impl AnimationManager {
    /// Creates a new `AnimationManager` with default Tailwind animation tracks.
    pub fn new() -> Self {
        Self {
            registry: KeyframeRegistry::new(),
            node_animations: HashMap::new(),
        }
    }

    /// Returns a mutable reference to the internal `KeyframeRegistry`.
    pub fn registry_mut(&mut self) -> &mut KeyframeRegistry {
        &mut self.registry
    }

    /// Returns true if there are any active running or forward-filling keyframe animations.
    pub fn has_active_animations(&self) -> bool {
        self.node_animations
            .values()
            .any(|anims| anims.iter().any(|a| !a.is_finished))
    }

    /// Returns true when any running keyframe animation targets a property that participates in
    /// box layout, so the layout tree must be recomputed as the value changes.
    pub fn has_layout_affecting_animations(&self) -> bool {
        self.node_animations.values().any(|list| {
            list.iter().filter(|anim| !anim.is_finished).any(|anim| {
                anim.current_values
                    .keys()
                    .any(|property| property.is_layout_affecting())
            })
        })
    }

    /// Returns all node IDs currently driven by a layout-affecting keyframe animation.
    pub fn layout_affecting_node_ids(&self) -> Vec<u64> {
        self.node_animations
            .iter()
            .filter_map(|(&node_id, list)| {
                if list.iter().filter(|a| !a.is_finished).any(|a| {
                    a.current_values
                        .keys()
                        .any(|property| property.is_layout_affecting())
                }) {
                    Some(node_id)
                } else {
                    None
                }
            })
            .collect()
    }

    /// Returns true if there are any active time-based animations (requiring continuous timer ticks).
    pub fn has_time_based_animations(&self) -> bool {
        self.node_animations.values().any(|list| {
            list.iter()
                .any(|a| !a.is_finished && matches!(a.spec.timeline, AnimationTimeline::Time))
        })
    }

    /// Retains only active animations whose `node_id` satisfies `keep`.
    pub fn retain_nodes<F: FnMut(&u64) -> bool>(&mut self, mut keep: F) {
        self.node_animations.retain(|id, _| keep(id));
    }

    /// Steps active scroll-driven animations on `node_id` at progress `progress` in [0.0, 1.0].
    pub fn step_node_scroll_progress(
        &mut self,
        node_id: u64,
        progress: f32,
        base_style: &ComputedStyle,
    ) -> Vec<AnimationLifecycleEvent> {
        let mut events = Vec::new();
        if let Some(list) = self.node_animations.get_mut(&node_id) {
            for anim in list.iter_mut() {
                if matches!(
                    anim.spec.timeline,
                    AnimationTimeline::Scroll(..) | AnimationTimeline::View(..)
                ) {
                    events.extend(anim.step_progress(node_id, progress, base_style));
                }
            }
        }
        events
    }

    /// Updates animation instances for `node_id` matching the new `style.animations` list.
    pub fn update_styles(
        &mut self,
        node_id: u64,
        style: &ComputedStyle,
        now_ms: f64,
    ) -> Vec<AnimationLifecycleEvent> {
        let mut events = Vec::new();

        if style.animations.is_empty() {
            if let Some(existing_anims) = self.node_animations.remove(&node_id) {
                for anim in existing_anims {
                    if !anim.end_emitted {
                        events.push(AnimationLifecycleEvent::Cancel {
                            node_id,
                            name: anim.name,
                            elapsed_time_sec: ((now_ms - anim.start_time_ms).max(0.0) / 1000.0)
                                as f32,
                        });
                    }
                }
            }
            return events;
        }

        let active_list = self.node_animations.entry(node_id).or_default();

        // 1. Check for removed animations
        let mut i = 0;
        while i < active_list.len() {
            let active_name = &active_list[i].name;
            if !style
                .animations
                .iter()
                .any(|spec| &spec.name == active_name)
            {
                let removed = active_list.remove(i);
                if !removed.end_emitted {
                    events.push(AnimationLifecycleEvent::Cancel {
                        node_id,
                        name: removed.name,
                        elapsed_time_sec: ((now_ms - removed.start_time_ms).max(0.0) / 1000.0)
                            as f32,
                    });
                }
            } else {
                i += 1;
            }
        }

        // 2. Add or update animations from style.animations
        for spec in &style.animations {
            if spec.name.is_empty() || spec.name == "none" {
                continue;
            }

            if let Some(existing) = active_list.iter_mut().find(|a| a.name == spec.name) {
                // Update play state or iteration count if mutated dynamically
                existing.spec.play_state = spec.play_state;
                existing.spec.iteration_count = spec.iteration_count;
            } else if let Some(track) = self.registry.get(&spec.name).cloned() {
                let mut new_anim = ActiveKeyframeAnimation::new(spec.clone(), track, now_ms);
                events.extend(new_anim.step(node_id, now_ms, style));
                active_list.push(new_anim);
            }
        }

        if active_list.is_empty() {
            self.node_animations.remove(&node_id);
        }

        events
    }

    /// Advances all running keyframe animations to timestamp `now_ms`.
    pub fn advance(
        &mut self,
        now_ms: f64,
        base_styles: &HashMap<u64, ComputedStyle>,
    ) -> Vec<AnimationLifecycleEvent> {
        let mut events = Vec::new();

        for (&node_id, anims) in self.node_animations.iter_mut() {
            let default_style = ComputedStyle::default();
            let base_style = base_styles.get(&node_id).unwrap_or(&default_style);

            for anim in anims.iter_mut() {
                if !anim.is_finished {
                    events.extend(anim.step(node_id, now_ms, base_style));
                }
            }
        }

        // Remove animations that have completed and do not fill forwards
        for anims in self.node_animations.values_mut() {
            anims.retain(|a| {
                !a.is_finished
                    || a.spec.fill_mode == AnimationFillMode::Forwards
                    || a.spec.fill_mode == AnimationFillMode::Both
            });
        }

        self.node_animations.retain(|_, v| !v.is_empty());

        events
    }

    /// Applies all active animation property overrides onto `style` for `node_id`.
    pub fn apply_to_style(&self, node_id: u64, style: &mut ComputedStyle) {
        if let Some(anims) = self.node_animations.get(&node_id) {
            for anim in anims {
                for (prop, val) in &anim.current_values {
                    apply_animatable_value(style, prop, val);
                }
            }
        }
    }

    /// Clears all active animations across all nodes.
    pub fn clear(&mut self) {
        self.node_animations.clear();
    }
}