blinc_layout 0.4.0

Blinc layout engine - Flexbox layout powered by Taffy
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
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
//! RenderState - Dynamic render properties separate from tree structure
//!
//! This module provides a clean separation between:
//! - **RenderTree**: Stable tree structure (rebuilt only when elements are added/removed)
//! - **RenderState**: Dynamic render properties (updated every frame without tree rebuild)
//!
//! # Architecture
//!
//! ```text
//! ┌─────────────────────────────────────────────────────────────────┐
//! │  UI Thread                                                       │
//! │  Event → State Change → Tree Rebuild (only structural changes)  │
//! └─────────────────────────────────────────────────────────────────┘
//!//!//!                     RenderTree (stable)
//!//!//! ┌─────────────────────────────────────────────────────────────────┐
//! │  Render Loop (60fps)                                             │
//! │  1. Tick animations                                              │
//! │  2. Update RenderState from animations                           │
//! │  3. Render tree + state to GPU                                   │
//! └─────────────────────────────────────────────────────────────────┘
//! ```
//!
//! # What Goes Where
//!
//! | Property | RenderTree | RenderState |
//! |----------|------------|-------------|
//! | Element hierarchy | ✓ | |
//! | Layout constraints | ✓ | |
//! | Text content | ✓ | |
//! | Background color | | ✓ (animated) |
//! | Opacity | | ✓ (animated) |
//! | Transform | | ✓ (animated) |
//! | Cursor visibility | | ✓ (animated) |
//! | Scroll offset | | ✓ (animated) |
//! | Hover state | | ✓ (FSM) |
//! | Focus state | | ✓ (FSM) |

use std::collections::HashMap;
use std::sync::{Arc, LazyLock, Mutex, RwLock};

use blinc_animation::{AnimationScheduler, SchedulerHandle, Spring, SpringConfig, SpringId};
use blinc_core::context_state::MotionAnimationState;
use blinc_core::{Color, Rect, Transform};

use crate::element::{MotionAnimation, MotionKeyframe};
use crate::tree::LayoutNodeId;

/// Shared motion state for query API access
///
/// This stores a snapshot of motion animation states that can be queried
/// from outside the render loop via the query_motion API.
pub type SharedMotionStates = Arc<RwLock<HashMap<String, MotionAnimationState>>>;

/// Create a new shared motion state store
pub fn create_shared_motion_states() -> SharedMotionStates {
    Arc::new(RwLock::new(HashMap::new()))
}

// =============================================================================
// Global animation scheduler handle
// =============================================================================

/// Global storage for the animation scheduler handle
///
/// This allows components to access animations without needing explicit context.
/// Set by RenderState on initialization, used by blinc_cn components.
#[allow(clippy::incompatible_msrv)]
static GLOBAL_SCHEDULER: LazyLock<RwLock<Option<SchedulerHandle>>> =
    LazyLock::new(|| RwLock::new(None));

/// Set the global animation scheduler handle
///
/// Called by RenderState during initialization to make the scheduler
/// available to all components.
pub fn set_global_scheduler(handle: SchedulerHandle) {
    let mut storage = GLOBAL_SCHEDULER.write().unwrap();
    *storage = Some(handle);
}

/// Get the global animation scheduler handle
///
/// Returns `None` if the scheduler hasn't been set yet (before app initialization).
///
/// # Example
///
/// ```ignore
/// let scheduler = get_global_scheduler()
///     .expect("Animation scheduler not initialized");
///
/// let scale = AnimatedValue::new(scheduler, 1.0, SpringConfig::snappy());
/// ```
pub fn get_global_scheduler() -> Option<SchedulerHandle> {
    GLOBAL_SCHEDULER.read().unwrap().clone()
}

/// Check if the global scheduler is available
pub fn has_global_scheduler() -> bool {
    GLOBAL_SCHEDULER.read().unwrap().is_some()
}

// =============================================================================
// Global pending motion replay queue
// =============================================================================

/// Global queue for motion keys that should replay their animation
///
/// This allows motion elements to request replay during tree building,
/// without needing direct access to RenderState.
#[allow(clippy::incompatible_msrv)]
static PENDING_MOTION_REPLAYS: LazyLock<Mutex<Vec<String>>> =
    LazyLock::new(|| Mutex::new(Vec::new()));

/// Queue a stable motion key for replay (global version)
///
/// Call this from within motion element construction when `.replay()` is used.
/// The replay will be processed when `RenderState::process_global_motion_replays()`
/// is called after `initialize_motion_animations()`.
pub fn queue_global_motion_replay(key: String) {
    let mut queue = PENDING_MOTION_REPLAYS.lock().unwrap();
    if !queue.contains(&key) {
        queue.push(key);
    }
}

/// Take all pending global motion replays
///
/// Returns the queued keys and clears the queue.
pub fn take_global_motion_replays() -> Vec<String> {
    std::mem::take(&mut *PENDING_MOTION_REPLAYS.lock().unwrap())
}

// =============================================================================
// Global pending motion exit cancel queue
// =============================================================================

/// Global queue for motion keys that should cancel their exit animation
///
/// This allows components to request exit cancellation without needing direct
/// access to RenderState.
#[allow(clippy::incompatible_msrv)]
static PENDING_MOTION_EXIT_CANCELS: LazyLock<Mutex<Vec<String>>> =
    LazyLock::new(|| Mutex::new(Vec::new()));

/// Queue a stable motion key for exit cancellation (global version)
///
/// Call this when an overlay's close is cancelled (e.g., mouse re-enters hover card).
/// The cancellation will be processed when `RenderState::process_global_motion_exit_cancels()`
/// is called during the next frame.
pub fn queue_global_motion_exit_cancel(key: String) {
    let mut queue = PENDING_MOTION_EXIT_CANCELS.lock().unwrap();
    if !queue.contains(&key) {
        queue.push(key);
    }
}

/// Take all pending global motion exit cancels
///
/// Returns the queued keys and clears the queue.
pub fn take_global_motion_exit_cancels() -> Vec<String> {
    std::mem::take(&mut *PENDING_MOTION_EXIT_CANCELS.lock().unwrap())
}

// =============================================================================
// Global pending motion exit start queue
// =============================================================================

/// Global queue for motion keys that should start their exit animation
///
/// This allows components to explicitly trigger exit animations without needing
/// direct access to RenderState.
#[allow(clippy::incompatible_msrv)]
static PENDING_MOTION_EXIT_STARTS: LazyLock<Mutex<Vec<String>>> =
    LazyLock::new(|| Mutex::new(Vec::new()));

/// Queue a stable motion key for exit start (global version)
///
/// Call this to explicitly start the exit animation for a motion (e.g., when
/// a hover card close countdown completes).
pub fn queue_global_motion_exit_start(key: String) {
    let mut queue = PENDING_MOTION_EXIT_STARTS.lock().unwrap();
    if !queue.contains(&key) {
        queue.push(key);
    }
}

/// Take all pending global motion exit starts
///
/// Returns the queued keys and clears the queue.
pub fn take_global_motion_exit_starts() -> Vec<String> {
    std::mem::take(&mut *PENDING_MOTION_EXIT_STARTS.lock().unwrap())
}

// =============================================================================
// Global pending motion start queue (for suspended motions)
// =============================================================================

/// Global queue for motion keys that should start their enter animation from suspended state
///
/// This allows components to explicitly start suspended animations without needing
/// direct access to RenderState.
#[allow(clippy::incompatible_msrv)]
static PENDING_MOTION_STARTS: LazyLock<Mutex<Vec<String>>> =
    LazyLock::new(|| Mutex::new(Vec::new()));

/// Queue a stable motion key to start its enter animation from suspended state
///
/// Call this to explicitly start a suspended motion's enter animation.
/// The motion must be in `Suspended` state for this to have an effect.
pub fn queue_global_motion_start(key: String) {
    let mut queue = PENDING_MOTION_STARTS.lock().unwrap();
    if !queue.contains(&key) {
        queue.push(key);
    }
}

/// Take all pending global motion starts
///
/// Returns the queued keys and clears the queue.
pub fn take_global_motion_starts() -> Vec<String> {
    std::mem::take(&mut *PENDING_MOTION_STARTS.lock().unwrap())
}

/// Buffer zone around viewport for prefetching content
/// This prevents pop-in when scrolling slowly
const VIEWPORT_BUFFER: f32 = 100.0;

/// State of a motion animation
#[derive(Clone, Debug, Default)]
pub enum MotionState {
    /// Animation is suspended (waiting for explicit start)
    /// Content is rendered with opacity 0, waiting for `MotionHandle.start()` to trigger
    Suspended,
    /// Animation hasn't started yet (waiting for delay)
    Waiting { remaining_delay_ms: f32 },
    /// Animation is playing (enter animation)
    Entering { progress: f32, duration_ms: f32 },
    /// Element is fully visible (enter complete)
    #[default]
    Visible,
    /// Animation is playing (exit animation)
    Exiting { progress: f32, duration_ms: f32 },
    /// Element should be removed (exit complete)
    Removed,
}

/// Active motion animation for a node
#[derive(Clone, Debug)]
pub struct ActiveMotion {
    /// The animation configuration
    pub config: MotionAnimation,
    /// Current state of the animation
    pub state: MotionState,
    /// Current interpolated values
    pub current: MotionKeyframe,
}

/// Active CSS keyframe animation state
///
/// Tracks a running CSS animation for an element, including the full
/// `MultiKeyframeAnimation` with all keyframes preserved.
#[derive(Clone, Debug)]
pub struct ActiveCssAnimation {
    /// The running animation with all keyframes
    pub animation: blinc_animation::MultiKeyframeAnimation,
    /// Whether the animation is currently playing
    pub is_playing: bool,
    /// Current interpolated properties (cached each tick)
    pub current_properties: blinc_animation::KeyframeProperties,
}

impl ActiveCssAnimation {
    /// Create a new active CSS animation
    pub fn new(mut animation: blinc_animation::MultiKeyframeAnimation) -> Self {
        animation.start();
        let current = animation.current_properties();
        Self {
            animation,
            is_playing: true,
            current_properties: current,
        }
    }

    /// Tick the animation and update current properties
    ///
    /// Returns true if the animation is still playing
    pub fn tick(&mut self, dt_ms: f32) -> bool {
        if self.is_playing {
            self.animation.tick(dt_ms);
            self.current_properties = self.animation.current_properties();
            if !self.animation.is_playing() {
                self.is_playing = false;
            }
        }
        self.is_playing
    }
}

/// Shared CSS animation/transition store
///
/// Wraps CSS keyframe animations and CSS transitions in a single struct
/// that can be shared between the AnimationScheduler's background thread
/// (for ticking) and the main render thread (for reading/writing).
///
/// The background thread ticks all animations at 120fps via a tick callback.
/// The main thread inserts/removes animations and reads `current_properties`
/// to apply animated values to render props.
#[derive(Default)]
pub struct CssAnimationStore {
    /// Active CSS keyframe animations (from stylesheet `animation:` property)
    pub animations: HashMap<LayoutNodeId, ActiveCssAnimation>,
    /// Active CSS transitions (from stylesheet `transition:` property)
    pub transitions: HashMap<LayoutNodeId, ActiveCssAnimation>,
}

impl CssAnimationStore {
    /// Create a new empty store
    pub fn new() -> Self {
        Self::default()
    }

    /// Tick all active CSS animations and transitions
    ///
    /// Called from the AnimationScheduler's background thread via tick callback.
    /// Returns `(animations_active, transitions_active)`.
    pub fn tick(&mut self, dt_ms: f32) -> (bool, bool) {
        // Tick CSS animations
        let mut anim_playing = false;
        for anim in self.animations.values_mut() {
            if anim.tick(dt_ms) {
                anim_playing = true;
            }
        }

        // Tick CSS transitions
        // NOTE: Completed transitions are NOT removed here — they stay in the store
        // so apply_all_css_transition_props can apply their final values. Call
        // remove_completed_transitions() after applying transition props.
        let mut trans_playing = false;
        for trans in self.transitions.values_mut() {
            if trans.tick(dt_ms) {
                trans_playing = true;
            }
        }

        (anim_playing, trans_playing)
    }

    /// Remove completed transitions from the store.
    ///
    /// Must be called AFTER `apply_all_css_transition_props()` so that the final
    /// transition values are applied to render props before removal. If transitions
    /// are removed during tick(), the final values never get applied, causing the
    /// before-snapshot to use stale intermediate values and restarting the transition.
    pub fn remove_completed_transitions(&mut self) {
        self.transitions.retain(|_, trans| trans.is_playing);
    }

    /// Check if there are any active CSS animations
    pub fn has_active_animations(&self) -> bool {
        self.animations.values().any(|a| a.is_playing)
    }

    /// Check if there are any active CSS transitions
    pub fn has_active_transitions(&self) -> bool {
        !self.transitions.is_empty()
    }
}

/// Dynamic render state for a single node
///
/// Contains all properties that can change without requiring a tree rebuild.
/// These properties are updated by animations or state machines.
#[derive(Clone, Debug)]
pub struct NodeRenderState {
    // =========================================================================
    // Animated visual properties
    // =========================================================================
    /// Current opacity (0.0 - 1.0)
    pub opacity: f32,

    /// Current background color (animated)
    pub background_color: Option<Color>,

    /// Current border color (animated)
    pub border_color: Option<Color>,

    /// Current transform (animated)
    pub transform: Option<Transform>,

    /// Current scale (animated, applied to transform)
    pub scale: f32,

    // =========================================================================
    // Animation handles (for tracking which properties are animating)
    // =========================================================================
    /// Spring ID for opacity animation
    pub opacity_spring: Option<SpringId>,

    /// Spring IDs for color animation (r, g, b, a)
    pub bg_color_springs: Option<[SpringId; 4]>,

    /// Spring IDs for transform (translate_x, translate_y, scale, rotate)
    pub transform_springs: Option<[SpringId; 4]>,

    // =========================================================================
    // Interaction state
    // =========================================================================
    /// Whether this node is currently hovered
    pub hovered: bool,

    /// Whether this node is currently focused
    pub focused: bool,

    /// Whether this node is currently pressed
    pub pressed: bool,

    // =========================================================================
    // Motion animation state
    // =========================================================================
    /// Active motion animation (enter/exit) for this node
    pub motion: Option<ActiveMotion>,

    // =========================================================================
    // CSS keyframe animation state
    // =========================================================================
    /// Active CSS keyframe animation for this node
    ///
    /// This is separate from `motion` because CSS animations can have
    /// multiple keyframes (not just enter/exit) and different lifecycle.
    pub css_animation: Option<ActiveCssAnimation>,
}

impl Default for NodeRenderState {
    fn default() -> Self {
        Self {
            opacity: 1.0,
            background_color: None,
            border_color: None,
            transform: None,
            scale: 1.0,
            opacity_spring: None,
            bg_color_springs: None,
            transform_springs: None,
            hovered: false,
            focused: false,
            pressed: false,
            motion: None,
            css_animation: None,
        }
    }
}

impl NodeRenderState {
    /// Create a new node render state with default values
    pub fn new() -> Self {
        Self::default()
    }

    /// Check if any properties are currently animating
    pub fn is_animating(&self) -> bool {
        self.opacity_spring.is_some()
            || self.bg_color_springs.is_some()
            || self.transform_springs.is_some()
            || self.has_active_motion()
            || self.has_active_css_animation()
    }

    /// Check if this node has an active motion animation
    pub fn has_active_motion(&self) -> bool {
        if let Some(ref motion) = self.motion {
            !matches!(motion.state, MotionState::Visible | MotionState::Removed)
        } else {
            false
        }
    }

    // =========================================================================
    // CSS Animation methods
    // =========================================================================

    /// Start a CSS keyframe animation for this node
    ///
    /// Replaces any existing CSS animation.
    pub fn start_css_animation(&mut self, animation: blinc_animation::MultiKeyframeAnimation) {
        self.css_animation = Some(ActiveCssAnimation::new(animation));
    }

    /// Check if this node has an active CSS animation
    pub fn has_active_css_animation(&self) -> bool {
        self.css_animation
            .as_ref()
            .map(|a| a.is_playing)
            .unwrap_or(false)
    }

    /// Tick the CSS animation and get current properties
    ///
    /// Returns Some with current properties if animation is active, None otherwise.
    pub fn tick_css_animation(
        &mut self,
        dt_ms: f32,
    ) -> Option<&blinc_animation::KeyframeProperties> {
        if let Some(ref mut active) = self.css_animation {
            active.tick(dt_ms);
            if active.is_playing {
                return Some(&active.current_properties);
            }
        }
        None
    }

    /// Stop and remove the CSS animation
    pub fn stop_css_animation(&mut self) {
        if let Some(ref mut active) = self.css_animation {
            active.is_playing = false;
        }
    }

    /// Get the current CSS animation properties (if any)
    pub fn css_animation_properties(&self) -> Option<&blinc_animation::KeyframeProperties> {
        self.css_animation
            .as_ref()
            .filter(|a| a.is_playing)
            .map(|a| &a.current_properties)
    }
}

/// Overlay type for rendering on top of the tree
#[derive(Clone, Debug)]
pub enum Overlay {
    /// Text cursor overlay
    Cursor {
        /// Position (x, y)
        position: (f32, f32),
        /// Size (width, height)
        size: (f32, f32),
        /// Color
        color: Color,
        /// Current opacity (for blinking)
        opacity: f32,
    },
    /// Text selection overlay
    Selection {
        /// Selection rectangles (multiple for multi-line)
        rects: Vec<(f32, f32, f32, f32)>,
        /// Selection color
        color: Color,
    },
    /// Focus ring overlay
    FocusRing {
        /// Position (x, y)
        position: (f32, f32),
        /// Size (width, height)
        size: (f32, f32),
        /// Corner radius
        radius: f32,
        /// Ring color
        color: Color,
        /// Ring thickness
        thickness: f32,
    },
}

/// Global render state - updated every frame independently of tree rebuilds
///
/// This holds all dynamic render properties that change frequently:
/// - Animated colors, transforms, opacity
/// - Cursor blink state
/// - Scroll positions (from physics)
/// - Hover/focus visual state
/// - Viewport for visibility culling
pub struct RenderState {
    /// Per-node animated properties
    node_states: HashMap<LayoutNodeId, NodeRenderState>,

    /// Stable-keyed motion animations (for overlays that rebuild each frame)
    /// Key is a stable string ID (e.g., overlay handle ID), value is the motion state
    stable_motions: HashMap<String, ActiveMotion>,

    /// Set of stable motion keys that were accessed this frame
    /// Used for mark-and-sweep cleanup of unused motions
    stable_motions_used: std::collections::HashSet<String>,

    /// Queue of stable motion keys that should replay their animation
    /// These are processed after initialize_motion_animations completes
    pending_motion_replays: Vec<String>,

    /// Global overlays (cursors, selections, focus rings)
    overlays: Vec<Overlay>,

    /// Animation scheduler (shared with app)
    animations: Arc<Mutex<AnimationScheduler>>,

    /// Cursor blink state (global for all text inputs)
    cursor_visible: bool,

    /// Last cursor blink toggle time
    cursor_blink_time: u64,

    /// Cursor blink interval in ms
    cursor_blink_interval: u64,

    /// Last tick time (for calculating delta time)
    last_tick_time: Option<u64>,

    /// Current viewport bounds for visibility culling
    /// Updated each frame based on window size and scroll position
    viewport: Rect,

    /// Whether viewport has been set (false = no culling)
    viewport_set: bool,

    /// Shared motion state for query API access
    /// Updated after each tick to expose motion states to components
    shared_motion_states: Option<SharedMotionStates>,
}

impl RenderState {
    /// Create a new render state with the given animation scheduler
    pub fn new(animations: Arc<Mutex<AnimationScheduler>>) -> Self {
        // Set global scheduler so components can access animations without context
        let handle = animations.lock().unwrap().handle();
        set_global_scheduler(handle);

        Self {
            node_states: HashMap::new(),
            stable_motions: HashMap::new(),
            stable_motions_used: std::collections::HashSet::new(),
            pending_motion_replays: Vec::new(),
            overlays: Vec::new(),
            animations,
            cursor_visible: true,
            cursor_blink_time: 0,
            cursor_blink_interval: 400,
            last_tick_time: None,
            viewport: Rect::new(0.0, 0.0, 0.0, 0.0),
            viewport_set: false,
            shared_motion_states: None,
        }
    }

    /// Set the shared motion states for query API access
    ///
    /// Call this after creating the RenderState to enable motion state queries.
    pub fn set_shared_motion_states(&mut self, shared: SharedMotionStates) {
        self.shared_motion_states = Some(shared);
    }

    /// Sync motion states to the shared store
    ///
    /// Call this after tick() to update the shared motion states for query API.
    pub fn sync_shared_motion_states(&self) {
        if let Some(ref shared) = self.shared_motion_states {
            let mut states = shared.write().unwrap();
            states.clear();
            for (key, motion) in &self.stable_motions {
                let state = match &motion.state {
                    MotionState::Suspended => MotionAnimationState::Suspended,
                    MotionState::Waiting { .. } => MotionAnimationState::Waiting,
                    MotionState::Entering { progress, .. } => MotionAnimationState::Entering {
                        progress: *progress,
                    },
                    MotionState::Visible => MotionAnimationState::Visible,
                    MotionState::Exiting { progress, .. } => MotionAnimationState::Exiting {
                        progress: *progress,
                    },
                    MotionState::Removed => MotionAnimationState::Removed,
                };
                states.insert(key.clone(), state);
            }
        }
    }

    /// Get animation scheduler handle for creating animations
    pub fn animation_handle(&self) -> SchedulerHandle {
        self.animations.lock().unwrap().handle()
    }

    /// Tick all animations and update render state
    ///
    /// Returns true if any animations are active (need another frame)
    pub fn tick(&mut self, current_time_ms: u64) -> bool {
        // Calculate delta time
        let dt_ms = if let Some(last_time) = self.last_tick_time {
            (current_time_ms.saturating_sub(last_time)) as f32
        } else {
            16.0 // Assume ~60fps for first frame
        };
        self.last_tick_time = Some(current_time_ms);

        // Tick the animation scheduler
        let animations_active = self.animations.lock().unwrap().tick();

        // Update cursor blink
        if current_time_ms >= self.cursor_blink_time + self.cursor_blink_interval {
            self.cursor_visible = !self.cursor_visible;
            self.cursor_blink_time = current_time_ms;
        }

        // Track if any motion animations are active
        let mut motion_active = false;

        // Update node states from their animation springs and motion animations
        {
            let scheduler = self.animations.lock().unwrap();
            for state in self.node_states.values_mut() {
                // Update opacity from spring
                if let Some(spring_id) = state.opacity_spring {
                    if let Some(value) = scheduler.get_spring_value(spring_id) {
                        state.opacity = value.clamp(0.0, 1.0);
                    }
                }

                // Update background color from springs
                if let Some(springs) = state.bg_color_springs {
                    let r = scheduler.get_spring_value(springs[0]).unwrap_or(0.0);
                    let g = scheduler.get_spring_value(springs[1]).unwrap_or(0.0);
                    let b = scheduler.get_spring_value(springs[2]).unwrap_or(0.0);
                    let a = scheduler.get_spring_value(springs[3]).unwrap_or(1.0);
                    state.background_color = Some(Color::rgba(r, g, b, a));
                }

                // Update transform from springs
                // Note: For now, we only support translation. Scale/rotation would need
                // matrix composition which Transform doesn't expose directly.
                if let Some(springs) = state.transform_springs {
                    let tx = scheduler.get_spring_value(springs[0]).unwrap_or(0.0);
                    let ty = scheduler.get_spring_value(springs[1]).unwrap_or(0.0);
                    let scale = scheduler.get_spring_value(springs[2]).unwrap_or(1.0);
                    let _rotate = scheduler.get_spring_value(springs[3]).unwrap_or(0.0);
                    // TODO: Support scale/rotation when Transform supports composition
                    state.transform = Some(Transform::translate(tx, ty));
                    state.scale = scale;
                }

                // Update motion animation
                if let Some(ref mut motion) = state.motion {
                    if Self::tick_motion(motion, dt_ms) {
                        motion_active = true;
                    }
                }
            }
        } // Drop scheduler lock

        // Tick stable-keyed motions (for overlays)
        self.tick_stable_motions(dt_ms);

        // Update cursor overlays with blink state
        for overlay in &mut self.overlays {
            if let Overlay::Cursor { opacity, .. } = overlay {
                *opacity = if self.cursor_visible { 1.0 } else { 0.0 };
            }
        }

        animations_active || motion_active || self.has_active_motions() || self.has_overlays()
    }

    /// Tick a motion animation, returns true if still active
    fn tick_motion(motion: &mut ActiveMotion, dt_ms: f32) -> bool {
        match &mut motion.state {
            MotionState::Waiting { remaining_delay_ms } => {
                *remaining_delay_ms -= dt_ms;
                if *remaining_delay_ms <= 0.0 {
                    // Start enter animation
                    if motion.config.enter_from.is_some() && motion.config.enter_duration_ms > 0 {
                        tracing::debug!(
                            "Motion: Starting enter animation, duration={}ms",
                            motion.config.enter_duration_ms
                        );
                        motion.state = MotionState::Entering {
                            progress: 0.0,
                            duration_ms: motion.config.enter_duration_ms as f32,
                        };
                        // Initialize current to the "from" state
                        motion.current = motion.config.enter_from.clone().unwrap_or_default();
                    } else {
                        motion.state = MotionState::Visible;
                        motion.current = MotionKeyframe::default(); // Fully visible
                    }
                }
                true // Still animating
            }
            MotionState::Entering {
                progress,
                duration_ms,
            } => {
                *progress += dt_ms / *duration_ms;
                if *progress >= 1.0 {
                    motion.state = MotionState::Visible;
                    motion.current = MotionKeyframe::default(); // Fully visible (opacity=1, scale=1, etc.)
                    false // Done animating
                } else {
                    // Interpolate from enter_from to visible (default)
                    let from = motion
                        .config
                        .enter_from
                        .as_ref()
                        .cloned()
                        .unwrap_or_default();
                    let to = MotionKeyframe::default();
                    // Apply ease-in-out for enter animation
                    // This provides a smooth start that doesn't feel "sudden"
                    // when items appear in sequence (stagger animations)
                    let eased = ease_in_out_cubic(*progress);
                    motion.current = from.lerp(&to, eased);
                    true // Still animating
                }
            }
            MotionState::Suspended => true, // Still animating (waiting for start)
            MotionState::Visible => false,  // Not animating
            MotionState::Exiting {
                progress,
                duration_ms,
            } => {
                *progress += dt_ms / *duration_ms;
                if *progress >= 1.0 {
                    motion.state = MotionState::Removed;
                    motion.current = motion.config.exit_to.clone().unwrap_or_default();
                    false // Done animating
                } else {
                    // Interpolate from visible to exit_to
                    let from = MotionKeyframe::default();
                    let to = motion.config.exit_to.as_ref().cloned().unwrap_or_default();
                    // Apply ease-in for exit animation
                    let eased = ease_in_cubic(*progress);
                    motion.current = from.lerp(&to, eased);
                    true // Still animating
                }
            }
            MotionState::Removed => false, // Not animating
        }
    }

    /// Reset cursor blink (call when focus changes or user types)
    pub fn reset_cursor_blink(&mut self, current_time_ms: u64) {
        self.cursor_visible = true;
        self.cursor_blink_time = current_time_ms;
    }

    /// Set cursor blink interval
    pub fn set_cursor_blink_interval(&mut self, interval_ms: u64) {
        self.cursor_blink_interval = interval_ms;
    }

    /// Check if cursor is currently visible
    pub fn cursor_visible(&self) -> bool {
        self.cursor_visible
    }

    // =========================================================================
    // Node State Management
    // =========================================================================

    /// Get or create render state for a node
    pub fn get_or_create(&mut self, node_id: LayoutNodeId) -> &mut NodeRenderState {
        self.node_states.entry(node_id).or_default()
    }

    /// Get render state for a node (if exists)
    pub fn get(&self, node_id: LayoutNodeId) -> Option<&NodeRenderState> {
        self.node_states.get(&node_id)
    }

    /// Get mutable render state for a node (if exists)
    pub fn get_mut(&mut self, node_id: LayoutNodeId) -> Option<&mut NodeRenderState> {
        self.node_states.get_mut(&node_id)
    }

    /// Remove render state for a node
    pub fn remove(&mut self, node_id: LayoutNodeId) {
        self.node_states.remove(&node_id);
    }

    /// Clear all node states (call when tree is completely rebuilt)
    pub fn clear_nodes(&mut self) {
        self.node_states.clear();
    }

    // =========================================================================
    // Animation Control
    // =========================================================================

    /// Animate opacity for a node
    pub fn animate_opacity(&mut self, node_id: LayoutNodeId, target: f32, config: SpringConfig) {
        // Get current values first
        let (current, old_spring) = {
            let state = self.node_states.entry(node_id).or_default();
            (state.opacity, state.opacity_spring.take())
        };

        // Remove existing spring if any
        if let Some(old_id) = old_spring {
            self.animations.lock().unwrap().remove_spring(old_id);
        }

        // Create new spring
        let mut spring = Spring::new(config, current);
        spring.set_target(target);
        let spring_id = self.animations.lock().unwrap().add_spring(spring);

        // Store the new spring id
        if let Some(state) = self.node_states.get_mut(&node_id) {
            state.opacity_spring = Some(spring_id);
        }
    }

    /// Animate background color for a node
    pub fn animate_background(
        &mut self,
        node_id: LayoutNodeId,
        target: Color,
        config: SpringConfig,
    ) {
        // Get current values first
        let (current, old_springs) = {
            let state = self.node_states.entry(node_id).or_default();
            let current = state.background_color.unwrap_or(Color::TRANSPARENT);
            (current, state.bg_color_springs.take())
        };

        // Remove existing springs if any
        if let Some(old_ids) = old_springs {
            let mut scheduler = self.animations.lock().unwrap();
            for id in old_ids {
                scheduler.remove_spring(id);
            }
        }

        // Create springs for r, g, b, a
        let springs = {
            let mut scheduler = self.animations.lock().unwrap();
            [
                {
                    let mut s = Spring::new(config, current.r);
                    s.set_target(target.r);
                    scheduler.add_spring(s)
                },
                {
                    let mut s = Spring::new(config, current.g);
                    s.set_target(target.g);
                    scheduler.add_spring(s)
                },
                {
                    let mut s = Spring::new(config, current.b);
                    s.set_target(target.b);
                    scheduler.add_spring(s)
                },
                {
                    let mut s = Spring::new(config, current.a);
                    s.set_target(target.a);
                    scheduler.add_spring(s)
                },
            ]
        };

        // Store the new spring ids
        if let Some(state) = self.node_states.get_mut(&node_id) {
            state.bg_color_springs = Some(springs);
        }
    }

    /// Set background color immediately (no animation)
    pub fn set_background(&mut self, node_id: LayoutNodeId, color: Color) {
        // Get old springs first
        let old_springs = {
            let state = self.node_states.entry(node_id).or_default();
            state.bg_color_springs.take()
        };

        // Remove any active animation
        if let Some(old_ids) = old_springs {
            let mut scheduler = self.animations.lock().unwrap();
            for id in old_ids {
                scheduler.remove_spring(id);
            }
        }

        // Set the color
        if let Some(state) = self.node_states.get_mut(&node_id) {
            state.background_color = Some(color);
        }
    }

    /// Set opacity immediately (no animation)
    pub fn set_opacity(&mut self, node_id: LayoutNodeId, opacity: f32) {
        // Get old spring first
        let old_spring = {
            let state = self.node_states.entry(node_id).or_default();
            state.opacity_spring.take()
        };

        // Remove any active animation
        if let Some(old_id) = old_spring {
            self.animations.lock().unwrap().remove_spring(old_id);
        }

        // Set the opacity
        if let Some(state) = self.node_states.get_mut(&node_id) {
            state.opacity = opacity;
        }
    }

    // =========================================================================
    // Overlay Management
    // =========================================================================

    /// Add a cursor overlay
    pub fn add_cursor(&mut self, x: f32, y: f32, width: f32, height: f32, color: Color) {
        self.overlays.push(Overlay::Cursor {
            position: (x, y),
            size: (width, height),
            color,
            opacity: if self.cursor_visible { 1.0 } else { 0.0 },
        });
    }

    /// Add a selection overlay
    pub fn add_selection(&mut self, rects: Vec<(f32, f32, f32, f32)>, color: Color) {
        self.overlays.push(Overlay::Selection { rects, color });
    }

    /// Add a focus ring overlay
    #[allow(clippy::too_many_arguments)]
    pub fn add_focus_ring(
        &mut self,
        x: f32,
        y: f32,
        width: f32,
        height: f32,
        radius: f32,
        color: Color,
        thickness: f32,
    ) {
        self.overlays.push(Overlay::FocusRing {
            position: (x, y),
            size: (width, height),
            radius,
            color,
            thickness,
        });
    }

    /// Clear all overlays (call before each frame's overlay collection)
    pub fn clear_overlays(&mut self) {
        self.overlays.clear();
    }

    /// Get all overlays for rendering
    pub fn overlays(&self) -> &[Overlay] {
        &self.overlays
    }

    /// Check if there are any overlays
    pub fn has_overlays(&self) -> bool {
        !self.overlays.is_empty()
    }

    // =========================================================================
    // Interaction State
    // =========================================================================

    /// Set hover state for a node
    pub fn set_hovered(&mut self, node_id: LayoutNodeId, hovered: bool) {
        self.get_or_create(node_id).hovered = hovered;
    }

    /// Set focus state for a node
    pub fn set_focused(&mut self, node_id: LayoutNodeId, focused: bool) {
        self.get_or_create(node_id).focused = focused;
    }

    /// Set pressed state for a node
    pub fn set_pressed(&mut self, node_id: LayoutNodeId, pressed: bool) {
        self.get_or_create(node_id).pressed = pressed;
    }

    /// Check if a node is hovered
    pub fn is_hovered(&self, node_id: LayoutNodeId) -> bool {
        self.get(node_id).map(|s| s.hovered).unwrap_or(false)
    }

    /// Check if a node is focused
    pub fn is_focused(&self, node_id: LayoutNodeId) -> bool {
        self.get(node_id).map(|s| s.focused).unwrap_or(false)
    }

    /// Check if a node is pressed
    pub fn is_pressed(&self, node_id: LayoutNodeId) -> bool {
        self.get(node_id).map(|s| s.pressed).unwrap_or(false)
    }

    // =========================================================================
    // Motion Animation Control
    // =========================================================================

    /// Start an enter motion animation for a node
    ///
    /// This is called when a node with motion config first appears in the tree.
    pub fn start_enter_motion(&mut self, node_id: LayoutNodeId, config: MotionAnimation) {
        let state = self.get_or_create(node_id);

        // Determine initial state based on delay
        let initial_state = if config.enter_delay_ms > 0 {
            MotionState::Waiting {
                remaining_delay_ms: config.enter_delay_ms as f32,
            }
        } else if config.enter_from.is_some() && config.enter_duration_ms > 0 {
            MotionState::Entering {
                progress: 0.0,
                duration_ms: config.enter_duration_ms as f32,
            }
        } else {
            MotionState::Visible
        };

        // Initial values come from enter_from (the starting state)
        let current = if matches!(initial_state, MotionState::Visible) {
            MotionKeyframe::default() // Already fully visible
        } else {
            config.enter_from.clone().unwrap_or_default()
        };

        state.motion = Some(ActiveMotion {
            config,
            state: initial_state,
            current,
        });
    }

    /// Start an exit motion animation for a node
    ///
    /// This is called when a node with motion config is about to be removed.
    pub fn start_exit_motion(&mut self, node_id: LayoutNodeId) {
        if let Some(state) = self.node_states.get_mut(&node_id) {
            if let Some(ref mut motion) = state.motion {
                if motion.config.exit_to.is_some() && motion.config.exit_duration_ms > 0 {
                    motion.state = MotionState::Exiting {
                        progress: 0.0,
                        duration_ms: motion.config.exit_duration_ms as f32,
                    };
                    motion.current = MotionKeyframe::default(); // Start from visible
                } else {
                    motion.state = MotionState::Removed;
                }
            }
        }
    }

    /// Get the current motion values for a node
    ///
    /// Returns the interpolated keyframe values if the node has an active motion.
    pub fn get_motion_values(&self, node_id: LayoutNodeId) -> Option<&MotionKeyframe> {
        self.get(node_id)
            .and_then(|s| s.motion.as_ref())
            .map(|m| &m.current)
    }

    /// Check if a node's motion animation is complete and should be removed
    pub fn is_motion_removed(&self, node_id: LayoutNodeId) -> bool {
        self.get(node_id)
            .and_then(|s| s.motion.as_ref())
            .map(|m| matches!(m.state, MotionState::Removed))
            .unwrap_or(false)
    }

    /// Check if any nodes have active motion animations
    pub fn has_active_motions(&self) -> bool {
        self.node_states.values().any(|s| s.has_active_motion())
            || self
                .stable_motions
                .values()
                .any(|m| !matches!(m.state, MotionState::Visible | MotionState::Removed))
    }

    // =========================================================================
    // Stable-Keyed Motion Animations (for overlays)
    // =========================================================================

    /// Start or get a stable-keyed motion animation
    ///
    /// Unlike node-based motions, these persist across tree rebuilds using a
    /// stable string key (e.g., overlay handle ID).
    ///
    /// If the motion already exists and is still animating (Waiting, Entering),
    /// we leave it alone. If it's in Visible or Exiting state, we also leave it
    /// alone. Only when in Removed state do we restart (overlay was closed and
    /// reopened).
    ///
    /// If `replay` is true, the animation restarts from the beginning even if
    /// it already exists (useful for tab transitions where content changes).
    ///
    /// Motion exit is now triggered explicitly via `MotionHandle.exit()` /
    /// `start_stable_motion_exit()` instead of the old `is_exiting` flag.
    pub fn start_stable_motion(&mut self, key: &str, config: MotionAnimation, replay: bool) {
        // Mark this key as used this frame (for garbage collection)
        self.stable_motions_used.insert(key.to_string());

        // Check if motion already exists
        if let Some(existing) = self.stable_motions.get_mut(key) {
            // NOTE: replay flag is intentionally ignored here for existing motions.
            // The replay mechanism via this flag doesn't work correctly because
            // initialize_motion_animations is called for ALL motions in the tree,
            // not just the ones that changed. Use `replay_stable_motion(key)` instead,
            // called from an on_ready callback when the motion is first mounted.

            // If already animating, suspended, or visible, leave it alone
            match existing.state {
                MotionState::Suspended
                | MotionState::Waiting { .. }
                | MotionState::Entering { .. }
                | MotionState::Visible => {
                    // Don't restart - animation is either suspended, in progress, or completed
                    return;
                }
                MotionState::Exiting { .. } => {
                    // Motion is exiting - do NOT cancel it automatically.
                    // Exit animations should only be cancelled by an explicit cancel_exit() call.
                    // This ensures exit animations play fully even when the tree is rebuilt
                    // (e.g., during overlay close animation where content is still rendered).
                    tracing::debug!(
                        "Motion '{}': Exiting, continuing exit animation (use cancel_exit() to interrupt)",
                        key
                    );
                    return;
                }
                // Motion was removed (exit animation completed) - do NOT restart!
                // The motion should only restart when it's been fully cleaned up from stable_motions
                // (via end_stable_motion_frame) and then created fresh. This prevents the enter
                // animation from replaying immediately after exit completes while the overlay
                // content is still being rendered during the Closing state.
                MotionState::Removed => {
                    tracing::debug!(
                        "Motion '{}': Removed state, NOT restarting (wait for cleanup)",
                        key
                    );
                    return;
                }
            }
        }

        // Create new motion
        tracing::debug!(
            "Motion '{}': Creating new motion (enter_duration={}ms)",
            key,
            config.enter_duration_ms
        );
        let initial_state = if config.enter_delay_ms > 0 {
            MotionState::Waiting {
                remaining_delay_ms: config.enter_delay_ms as f32,
            }
        } else if config.enter_from.is_some() && config.enter_duration_ms > 0 {
            MotionState::Entering {
                progress: 0.0,
                duration_ms: config.enter_duration_ms as f32,
            }
        } else {
            MotionState::Visible
        };

        // Initial values come from enter_from (the starting state)
        let current = if matches!(initial_state, MotionState::Visible) {
            MotionKeyframe::default() // Already fully visible
        } else {
            config.enter_from.clone().unwrap_or_default()
        };

        self.stable_motions.insert(
            key.to_string(),
            ActiveMotion {
                config,
                state: initial_state,
                current,
            },
        );
    }

    /// Start exit animation for a stable-keyed motion
    pub fn start_stable_motion_exit(&mut self, key: &str) {
        if let Some(motion) = self.stable_motions.get_mut(key) {
            if motion.config.exit_to.is_some() && motion.config.exit_duration_ms > 0 {
                motion.state = MotionState::Exiting {
                    progress: 0.0,
                    duration_ms: motion.config.exit_duration_ms as f32,
                };
                motion.current = MotionKeyframe::default(); // Start from visible
            } else {
                motion.state = MotionState::Removed;
            }
        }
    }

    /// Cancel a stable motion's exit animation and return to Visible state
    ///
    /// Used when an overlay's close is cancelled (e.g., mouse re-enters hover card).
    /// This interrupts the exit animation and immediately sets the motion to fully visible.
    pub fn cancel_stable_motion_exit(&mut self, key: &str) {
        if let Some(motion) = self.stable_motions.get_mut(key) {
            if matches!(motion.state, MotionState::Exiting { .. }) {
                // Return to fully visible state
                motion.state = MotionState::Visible;
                motion.current = MotionKeyframe::default(); // Reset to default (fully visible)
            }
        }
    }

    /// Start a suspended stable-keyed motion
    ///
    /// Unlike `start_stable_motion`, this creates the motion in `Suspended` state.
    /// The motion renders with opacity 0 and waits for an explicit `start()` call
    /// via `MotionHandle.start()` to begin the enter animation.
    ///
    /// This is useful for tab transitions and other cases where you want to:
    /// 1. Mount the content invisibly
    /// 2. Perform any setup/measurement
    /// 3. Then trigger the animation manually
    ///
    /// **Important**: Unlike regular motions, suspended motions will reset to
    /// `Suspended` state when this is called again, even if the motion is already
    /// `Visible`. This enables tab-like behavior where each time a tab becomes
    /// active, it resets to suspended and waits for explicit `start()`.
    ///
    /// # Parameters
    ///
    /// * `key` - Stable string key for this motion
    /// * `config` - Animation configuration (enter/exit animations)
    ///
    /// Returns `true` if a new motion was created or an existing one was reset
    /// (meaning the on_ready callback should be re-registered).
    pub fn start_stable_motion_suspended(&mut self, key: &str, config: MotionAnimation) -> bool {
        // Mark this key as used this frame (for garbage collection)
        self.stable_motions_used.insert(key.to_string());

        // Check if motion already exists
        if let Some(existing) = self.stable_motions.get_mut(key) {
            match existing.state {
                // Already suspended or animating - leave it alone
                MotionState::Suspended
                | MotionState::Waiting { .. }
                | MotionState::Entering { .. } => {
                    return false;
                }
                // Motion is visible - leave it visible, don't reset
                // The suspended animation is only for first appearance
                // Re-entry animations should use .replay() instead
                MotionState::Visible => {
                    return false;
                }
                // Exiting - let it finish, don't interrupt
                MotionState::Exiting { .. } => {
                    return false;
                }
                MotionState::Removed => {
                    tracing::debug!(
                        "Motion '{}': Removed state, NOT restarting suspended (wait for cleanup)",
                        key
                    );
                    return false;
                }
            }
        }

        // Create new suspended motion
        tracing::debug!(
            "Motion '{}': Creating new SUSPENDED motion (will wait for start())",
            key
        );

        // Initial keyframe has opacity 0 for suspended state
        let mut current = config.enter_from.clone().unwrap_or_default();
        current.opacity = Some(0.0);

        self.stable_motions.insert(
            key.to_string(),
            ActiveMotion {
                config,
                state: MotionState::Suspended,
                current,
            },
        );

        // New motion created, on_ready callback should be registered
        true
    }

    /// Start the enter animation for a suspended motion
    ///
    /// Transitions a motion from `Suspended` → `Waiting` or `Entering` state.
    /// No-op if the motion is not in `Suspended` state.
    pub fn start_suspended_motion(&mut self, key: &str) {
        if let Some(motion) = self.stable_motions.get_mut(key) {
            if matches!(motion.state, MotionState::Suspended) {
                let config = &motion.config;
                tracing::debug!(
                    "Motion '{}': Starting from suspended (enter_duration={}ms)",
                    key,
                    config.enter_duration_ms
                );

                // Transition to the appropriate next state
                motion.state = if config.enter_delay_ms > 0 {
                    MotionState::Waiting {
                        remaining_delay_ms: config.enter_delay_ms as f32,
                    }
                } else if config.enter_from.is_some() && config.enter_duration_ms > 0 {
                    MotionState::Entering {
                        progress: 0.0,
                        duration_ms: config.enter_duration_ms as f32,
                    }
                } else {
                    MotionState::Visible
                };

                // Reset current values to enter_from state
                motion.current = if matches!(motion.state, MotionState::Visible) {
                    MotionKeyframe::default()
                } else {
                    config.enter_from.clone().unwrap_or_default()
                };
            }
        }
    }

    /// Process all pending motion starts from the global queue
    ///
    /// Call this during the render loop to start any suspended motions
    /// that were queued via `queue_global_motion_start()`.
    pub fn process_global_motion_starts(&mut self) {
        let keys = take_global_motion_starts();
        for key in keys {
            self.start_suspended_motion(&key);
        }
    }

    /// Queue a stable motion key for replay
    ///
    /// The replay will be processed after `initialize_motion_animations` completes.
    /// This allows motion elements to request replay during tree building without
    /// affecting other motions.
    ///
    /// Call `process_pending_motion_replays()` after `initialize_motion_animations()`
    /// to actually perform the replays.
    pub fn queue_motion_replay(&mut self, key: String) {
        if !self.pending_motion_replays.contains(&key) {
            self.pending_motion_replays.push(key);
        }
    }

    /// Process all pending motion replays (from local queue)
    ///
    /// Call this after `initialize_motion_animations()` to replay any motions
    /// that requested it via `queue_motion_replay()`.
    pub fn process_pending_motion_replays(&mut self) {
        let keys = std::mem::take(&mut self.pending_motion_replays);
        for key in keys {
            self.replay_stable_motion(&key);
        }
    }

    /// Process all pending motion replays from the global queue
    ///
    /// Call this after `initialize_motion_animations()` to replay any motions
    /// that were queued via `queue_global_motion_replay()` during tree building.
    pub fn process_global_motion_replays(&mut self) {
        let keys = take_global_motion_replays();
        for key in keys {
            self.replay_stable_motion(&key);
        }
    }

    /// Process all pending motion exit cancels from the global queue
    ///
    /// Call this during the render loop to cancel any exit animations
    /// that were queued via `queue_global_motion_exit_cancel()`.
    pub fn process_global_motion_exit_cancels(&mut self) {
        let keys = take_global_motion_exit_cancels();
        for key in keys {
            self.cancel_stable_motion_exit(&key);
        }
    }

    /// Process all pending motion exit starts from the global queue
    ///
    /// Call this during the render loop to start any exit animations
    /// that were queued via `queue_global_motion_exit_start()`.
    pub fn process_global_motion_exit_starts(&mut self) {
        let keys = take_global_motion_exit_starts();
        for key in keys {
            self.start_stable_motion_exit(&key);
        }
    }

    /// Replay a stable-keyed motion animation from the beginning
    ///
    /// This restarts the animation if it's in Visible state.
    /// Prefer using `queue_motion_replay()` during tree building, and
    /// `process_pending_motion_replays()` after initialization.
    pub fn replay_stable_motion(&mut self, key: &str) {
        if let Some(motion) = self.stable_motions.get_mut(key) {
            // Only replay if animation is complete (Visible state)
            if matches!(motion.state, MotionState::Visible) {
                let config = motion.config.clone();
                motion.state = if config.enter_delay_ms > 0 {
                    MotionState::Waiting {
                        remaining_delay_ms: config.enter_delay_ms as f32,
                    }
                } else if config.enter_from.is_some() && config.enter_duration_ms > 0 {
                    MotionState::Entering {
                        progress: 0.0,
                        duration_ms: config.enter_duration_ms as f32,
                    }
                } else {
                    MotionState::Visible
                };
                motion.current = if matches!(motion.state, MotionState::Visible) {
                    MotionKeyframe::default()
                } else {
                    config.enter_from.clone().unwrap_or_default()
                };
            }
        }
    }

    /// Get the current motion values for a stable-keyed animation
    pub fn get_stable_motion_values(&self, key: &str) -> Option<&MotionKeyframe> {
        self.stable_motions.get(key).map(|m| &m.current)
    }

    /// Get the animation state for a stable-keyed motion
    ///
    /// Returns the current state of the motion animation as `MotionAnimationState`.
    /// This is used by the query API to expose animation state to components.
    pub fn get_stable_motion_state(
        &self,
        key: &str,
    ) -> blinc_core::context_state::MotionAnimationState {
        use blinc_core::context_state::MotionAnimationState;

        match self.stable_motions.get(key) {
            Some(motion) => match &motion.state {
                MotionState::Suspended => MotionAnimationState::Suspended,
                MotionState::Waiting { .. } => MotionAnimationState::Waiting,
                MotionState::Entering { progress, .. } => MotionAnimationState::Entering {
                    progress: *progress,
                },
                MotionState::Visible => MotionAnimationState::Visible,
                MotionState::Exiting { progress, .. } => MotionAnimationState::Exiting {
                    progress: *progress,
                },
                MotionState::Removed => MotionAnimationState::Removed,
            },
            None => MotionAnimationState::NotFound,
        }
    }

    /// Check if a stable-keyed motion is complete and should be removed
    pub fn is_stable_motion_removed(&self, key: &str) -> bool {
        self.stable_motions
            .get(key)
            .map(|m| matches!(m.state, MotionState::Removed))
            .unwrap_or(false)
    }

    /// Reset all stable motions to replay on next frame
    ///
    /// Call this before a full UI rebuild to ensure all motion animations
    /// replay when the UI is reconstructed. This resets motions in `Visible`
    /// state back to their initial `Waiting` or `Entering` state.
    ///
    /// Motions that are currently animating (Entering/Exiting) or already
    /// Removed are left alone.
    pub fn reset_stable_motions_for_rebuild(&mut self) {
        for motion in self.stable_motions.values_mut() {
            if matches!(motion.state, MotionState::Visible) {
                let config = &motion.config;
                motion.state = if config.enter_delay_ms > 0 {
                    MotionState::Waiting {
                        remaining_delay_ms: config.enter_delay_ms as f32,
                    }
                } else if config.enter_from.is_some() && config.enter_duration_ms > 0 {
                    MotionState::Entering {
                        progress: 0.0,
                        duration_ms: config.enter_duration_ms as f32,
                    }
                } else {
                    MotionState::Visible
                };
                motion.current = if matches!(motion.state, MotionState::Visible) {
                    MotionKeyframe::default()
                } else {
                    motion.config.enter_from.clone().unwrap_or_default()
                };
            }
        }
    }

    /// Clear all stable motions
    ///
    /// Use this for a complete reset, e.g., when navigating to a completely
    /// different view. For normal full rebuilds, prefer `reset_stable_motions_for_rebuild()`
    /// which preserves motion configs but replays animations.
    pub fn clear_stable_motions(&mut self) {
        self.stable_motions.clear();
        self.stable_motions_used.clear();
    }

    /// Remove a stable-keyed motion (after exit animation completes)
    pub fn remove_stable_motion(&mut self, key: &str) {
        self.stable_motions.remove(key);
    }

    /// Tick stable-keyed motions (called from tick())
    fn tick_stable_motions(&mut self, dt_ms: f32) {
        for motion in self.stable_motions.values_mut() {
            Self::tick_single_motion(motion, dt_ms);
        }
    }

    /// Helper to tick a single motion animation
    fn tick_single_motion(motion: &mut ActiveMotion, dt_ms: f32) {
        match &mut motion.state {
            MotionState::Waiting { remaining_delay_ms } => {
                *remaining_delay_ms -= dt_ms;
                if *remaining_delay_ms <= 0.0 {
                    if motion.config.enter_from.is_some() && motion.config.enter_duration_ms > 0 {
                        motion.state = MotionState::Entering {
                            progress: 0.0,
                            duration_ms: motion.config.enter_duration_ms as f32,
                        };
                    } else {
                        motion.state = MotionState::Visible;
                    }
                }
            }
            MotionState::Entering {
                progress,
                duration_ms,
            } => {
                *progress += dt_ms / *duration_ms;
                if *progress >= 1.0 {
                    motion.state = MotionState::Visible;
                    motion.current = MotionKeyframe::default();
                } else {
                    // Interpolate from enter_from to default (fully visible)
                    if let Some(ref from) = motion.config.enter_from {
                        motion.current = from.lerp(&MotionKeyframe::default(), *progress);
                    }
                }
            }
            MotionState::Exiting {
                progress,
                duration_ms,
            } => {
                *progress += dt_ms / *duration_ms;
                if *progress >= 1.0 {
                    motion.state = MotionState::Removed;
                    if let Some(ref to) = motion.config.exit_to {
                        motion.current = to.clone();
                    }
                } else {
                    // Interpolate from default (fully visible) to exit_to
                    if let Some(ref to) = motion.config.exit_to {
                        motion.current = MotionKeyframe::default().lerp(to, *progress);
                    }
                }
            }
            MotionState::Suspended | MotionState::Visible | MotionState::Removed => {
                // Suspended: waiting for explicit start() call - no tick needed
                // Visible/Removed: animation complete - nothing to do
            }
        }
    }

    /// Begin a new frame for stable motion tracking
    ///
    /// Call this before rendering overlay trees to reset the "used" tracking.
    /// Stable motions that aren't accessed during the frame will be marked as
    /// removed when `end_stable_motion_frame()` is called.
    pub fn begin_stable_motion_frame(&mut self) {
        self.stable_motions_used.clear();
    }

    /// End the frame for stable motion tracking
    ///
    /// For motions that weren't accessed this frame (content removed from tree):
    /// - If they have an exit animation, transition to Exiting state
    /// - If already Exiting and complete (Removed), actually remove them
    /// - This enables CSS-like behavior: mount = enter, unmount = exit
    pub fn end_stable_motion_frame(&mut self) {
        // Collect keys to remove (motions that completed exit)
        let mut to_remove = Vec::new();

        for (key, motion) in self.stable_motions.iter_mut() {
            if !self.stable_motions_used.contains(key) {
                // This motion's content was not in the tree this frame
                match &motion.state {
                    MotionState::Removed => {
                        // Exit complete, safe to remove
                        tracing::debug!("Motion '{}': Removed state, cleaning up", key);
                        to_remove.push(key.clone());
                    }
                    MotionState::Exiting { .. } => {
                        // Already exiting, let it continue
                    }
                    _ => {
                        // Not exiting yet - start exit animation
                        if motion.config.exit_to.is_some() && motion.config.exit_duration_ms > 0 {
                            tracing::debug!(
                                "Motion '{}': Starting exit animation ({}ms)",
                                key,
                                motion.config.exit_duration_ms
                            );
                            motion.state = MotionState::Exiting {
                                progress: 0.0,
                                duration_ms: motion.config.exit_duration_ms as f32,
                            };
                            motion.current = MotionKeyframe::default(); // Start from visible
                        } else {
                            // No exit animation configured, remove immediately
                            tracing::debug!(
                                "Motion '{}': No exit config, removing immediately",
                                key
                            );
                            to_remove.push(key.clone());
                        }
                    }
                }
            }
        }

        // Remove completed motions
        for key in to_remove {
            self.stable_motions.remove(&key);
        }
    }

    // =========================================================================
    // Viewport / Visibility Culling
    // =========================================================================

    /// Set the current viewport bounds
    ///
    /// Call this each frame with the visible area (window size).
    /// Used for visibility culling of emoji and lazy-loaded images.
    pub fn set_viewport(&mut self, x: f32, y: f32, width: f32, height: f32) {
        self.viewport = Rect::new(x, y, width, height);
        self.viewport_set = true;
    }

    /// Set the viewport from window dimensions (assumes origin at 0,0)
    pub fn set_viewport_size(&mut self, width: f32, height: f32) {
        self.set_viewport(0.0, 0.0, width, height);
    }

    /// Get the current viewport bounds
    pub fn viewport(&self) -> Rect {
        self.viewport
    }

    /// Get the viewport expanded by buffer zone for prefetching
    ///
    /// Content within this area should be loaded to prevent pop-in during scroll.
    pub fn viewport_with_buffer(&self) -> Rect {
        Rect::new(
            self.viewport.x() - VIEWPORT_BUFFER,
            self.viewport.y() - VIEWPORT_BUFFER,
            self.viewport.width() + 2.0 * VIEWPORT_BUFFER,
            self.viewport.height() + 2.0 * VIEWPORT_BUFFER,
        )
    }

    /// Check if a rect is visible in the current viewport
    ///
    /// Returns true if the rect intersects with the viewport.
    /// If viewport hasn't been set, always returns true (no culling).
    pub fn is_visible(&self, bounds: &Rect) -> bool {
        if !self.viewport_set {
            return true; // No culling if viewport not set
        }
        self.viewport.intersects(bounds)
    }

    /// Check if a rect is visible with buffer zone (for prefetching)
    ///
    /// Returns true if the rect intersects with the expanded viewport.
    /// Use this for deciding what to load ahead of time.
    pub fn is_visible_with_buffer(&self, bounds: &Rect) -> bool {
        if !self.viewport_set {
            return true; // No culling if viewport not set
        }
        self.viewport_with_buffer().intersects(bounds)
    }

    /// Check if a rect is fully clipped (completely outside viewport)
    ///
    /// Returns true if the rect does not intersect with the viewport at all.
    pub fn is_clipped(&self, bounds: &Rect) -> bool {
        if !self.viewport_set {
            return false; // Nothing clipped if viewport not set
        }
        !self.viewport.intersects(bounds)
    }

    /// Check if viewport has been set
    pub fn has_viewport(&self) -> bool {
        self.viewport_set
    }
}

// ============================================================================
// Easing helper functions
// ============================================================================

/// Cubic ease-in-out (slow start, slow end) - good for stagger enter animations
/// This prevents the "sudden" appearance when items animate in sequence
fn ease_in_out_cubic(t: f32) -> f32 {
    if t < 0.5 {
        4.0 * t * t * t
    } else {
        1.0 - (-2.0 * t + 2.0).powi(3) / 2.0
    }
}

/// Cubic ease-in (slow start, fast end) - good for exit animations
fn ease_in_cubic(t: f32) -> f32 {
    t * t * t
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_render_state_creation() {
        let scheduler = Arc::new(Mutex::new(AnimationScheduler::new()));
        let state = RenderState::new(scheduler);

        assert!(state.cursor_visible());
        assert!(!state.has_overlays());
    }

    #[test]
    fn test_node_render_state() {
        let scheduler = Arc::new(Mutex::new(AnimationScheduler::new()));
        let mut state = RenderState::new(scheduler);

        let node_id = LayoutNodeId::default();

        // Should auto-create on access
        state.set_hovered(node_id, true);
        assert!(state.is_hovered(node_id));

        state.set_opacity(node_id, 0.5);
        assert_eq!(state.get(node_id).unwrap().opacity, 0.5);
    }

    #[test]
    fn test_overlays() {
        let scheduler = Arc::new(Mutex::new(AnimationScheduler::new()));
        let mut state = RenderState::new(scheduler);

        state.add_cursor(10.0, 20.0, 2.0, 16.0, Color::WHITE);
        assert!(state.has_overlays());
        assert_eq!(state.overlays().len(), 1);

        state.clear_overlays();
        assert!(!state.has_overlays());
    }

    #[test]
    fn test_cursor_blink() {
        let scheduler = Arc::new(Mutex::new(AnimationScheduler::new()));
        let mut state = RenderState::new(scheduler);
        state.set_cursor_blink_interval(100);

        assert!(state.cursor_visible());

        // Tick past the blink interval
        state.tick(150);
        assert!(!state.cursor_visible());

        // Tick again
        state.tick(300);
        assert!(state.cursor_visible());
    }
}