rustmotion-components 0.6.1

Component library for rustmotion (51 components)
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
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
//! Bridge from the `Component` tree to the new `BoxNode` tree.
//!
//! Each `ChildComponent` becomes one `BoxNode`. The component's
//! `style: CssStyle` is augmented with:
//! - `position: absolute` + `top` / `left` when `child.position` is set
//! - `width` / `height` from the component's `size` field (if any)
//! - `z-index` from the child's `z_index` field
//!
//! Container components (Card / Flex / Grid / Container / Positioned)
//! recursively build child boxes. Leaf components produce an empty-children
//! box that the dispatcher will paint.
//!
//! The builder also returns a flat `Vec<&Component>` indexed by NodeId so
//! the painter can resolve a node back to its component.

use std::sync::Arc;

use rustmotion_core::css::style::{AlignSelf, CssStyle, Position, Size as CSize};
use rustmotion_core::css::{apply_animated_props, LengthPercentage as CLP};
use rustmotion_core::engine::animator::{resolve_props_for_effects, AnimatedProperties};
use rustmotion_core::engine::box_tree::{BoxKind, BoxNode, NodeId};
use rustmotion_core::schema::video::{AnimationEffect, MotionBlurConfig, TrailConfig};

use crate::callout::ArrowDirection as CalloutArrowDirection;
use crate::chart::ChartType;
use crate::divider::DividerDirection;
use crate::mockup::MockupDevice;
use crate::skeleton::SkeletonVariant;
use crate::stepper::StepperOrientation;
use crate::timeline::TimelineDirection;
use crate::tooltip::TooltipArrow;
use crate::{ChildComponent, Component};

/// Frame-level context passed into the builder so animations can be resolved
/// per-node and merged into the resulting `CssStyle`. When `None`, the box
/// tree is built without any animation overrides (resting state).
#[derive(Debug, Clone, Copy)]
pub struct BuildAnimationCtx {
    pub time: f64,
    /// Seconds since the scenario started, as opposed to `time`, which
    /// restarts at every scene. Only the `audio-reactive` binding reads it:
    /// the audio analysis is indexed on the scenario's timeline, so using
    /// `time` gave a scene starting at t=73 s the analysis at 73 s *into that
    /// scene*. Every animation stays on `time`, which is what a delay, a
    /// stagger or a preset is written against.
    pub scenario_time: f64,
    pub scene_duration: f64,
    /// Frames per second of the output video. Required to convert the
    /// `shutter` fraction (in `MotionBlurConfig`) into an absolute temporal
    /// offset: `shutter_window = shutter / fps` seconds.
    pub fps: u32,
}

/// Padding allowance to keep arrow/connector heads inside the box.
const ARROW_BBOX_PADDING: f32 = 16.0;

/// Result of building a box tree from a scene description.
pub struct BuiltScene<'a> {
    /// Root box (a flex column container at viewport dimensions).
    pub root: BoxNode,
    /// Lookup table — `components[id as usize]` is the component for `id`.
    /// `None` for synthetic boxes (the root scene wrapper).
    pub components: Vec<Option<&'a ChildComponent>>,
    /// Per-node animation delay accumulated from ancestor containers'
    /// `stagger` (indexed like `components`). Consumed by the paint
    /// dispatcher so internal animations shift by the same amount as the
    /// CSS overrides resolved at build time.
    pub stagger_delays: Vec<f64>,
    /// Per-node affine time remap accumulated from ancestor containers'
    /// `time_scale`/`time_offset`. Entry `i` is `(scale, shift)` where
    /// `t_local = scale * t_global + shift`. Default `(1.0, 0.0)` = identity.
    /// Indexed like `components` and `stagger_delays`.
    pub time_params: Vec<(f64, f64)>,
}

/// Build a box tree for a flat list of scene-level children at a given
/// viewport size.
///
/// The implicit scene root is a `display: flex; flex-direction: column;
/// width/height: 100%`; children flow vertically unless they specify
/// `position: { x, y }`, in which case they become `position: absolute`.
pub fn build_scene<'a>(children: &'a [ChildComponent], viewport: (f32, f32)) -> BuiltScene<'a> {
    build_scene_with_root(children, viewport, default_root_css(viewport))
}

/// Same as [`build_scene`] but lets the caller supply the root container's
/// `CssStyle`. Width/height are forced to the viewport regardless.
pub fn build_scene_with_root<'a>(
    children: &'a [ChildComponent],
    viewport: (f32, f32),
    root_css: CssStyle,
) -> BuiltScene<'a> {
    build_scene_from_refs(children.iter(), viewport, root_css, None)
}

/// Like [`build_scene_with_root`] but resolves animations at `time` (seconds)
/// for each node and merges the result into its `CssStyle`. Use this when
/// rendering an animated frame.
pub fn build_scene_at_time<'a>(
    children: &'a [ChildComponent],
    viewport: (f32, f32),
    root_css: CssStyle,
    anim: BuildAnimationCtx,
) -> BuiltScene<'a> {
    build_scene_from_refs(children.iter(), viewport, root_css, Some(anim))
}

/// Like [`build_scene`] but with an animation context. Convenience wrapper
/// that uses the default root CSS (full-viewport flex column).
pub fn build_scene_with_anim<'a>(
    children: &'a [ChildComponent],
    viewport: (f32, f32),
    anim: BuildAnimationCtx,
) -> BuiltScene<'a> {
    build_scene_from_refs(
        children.iter(),
        viewport,
        default_root_css(viewport),
        Some(anim),
    )
}

/// Same as [`build_scene_with_root`] but accepts an iterator over
/// `&ChildComponent` references. Useful when the caller has filtered or
/// re-ordered the scene's children and doesn't want to clone.
pub fn build_scene_from_refs<'a, I>(
    children: I,
    viewport: (f32, f32),
    mut root_css: CssStyle,
    anim: Option<BuildAnimationCtx>,
) -> BuiltScene<'a>
where
    I: IntoIterator<Item = &'a ChildComponent>,
{
    let mut components: Vec<Option<&'a ChildComponent>> = vec![None];
    let mut stagger_delays: Vec<f64> = vec![0.0];
    let mut time_params: Vec<(f64, f64)> = vec![(1.0, 0.0)]; // slot 0 = root (identity)
    let mut next_id: NodeId = 1;

    let mut child_boxes = Vec::new();
    for (i, c) in children.into_iter().enumerate() {
        child_boxes.extend(build_child(
            c,
            &mut components,
            &mut stagger_delays,
            &mut time_params,
            &mut next_id,
            anim,
            format!("/children/{i}"),
            0.0,
            (1.0, 0.0),
            &root_css,
        ));
    }

    // Force the root to viewport dimensions even if the caller didn't set them.
    root_css.width = Some(CSize::Length(CLP::Px(viewport.0)));
    root_css.height = Some(CSize::Length(CLP::Px(viewport.1)));

    let root = BoxNode {
        id: 0,
        kind: BoxKind::Container,
        css: root_css,
        children: child_boxes,
        intrinsic: None,
        source_path: None,
        window: None,
    };

    BuiltScene {
        root,
        components,
        stagger_delays,
        time_params,
    }
}

fn default_root_css(viewport: (f32, f32)) -> CssStyle {
    CssStyle {
        display: Some(rustmotion_core::css::style::Display::Flex),
        flex_direction: Some(rustmotion_core::css::style::FlexDirection::Column),
        width: Some(CSize::Length(CLP::Px(viewport.0))),
        height: Some(CSize::Length(CLP::Px(viewport.1))),
        ..Default::default()
    }
}

/// Detect motion_blur and trail configs from a merged effect list.
///
/// Returns `(motion_blur, trail)`. Only the first occurrence of each type is
/// used; duplicate effects of the same kind are ignored.
fn detect_ghost_effects(
    effects: &[AnimationEffect],
) -> (Option<MotionBlurConfig>, Option<TrailConfig>) {
    let mut mb: Option<MotionBlurConfig> = None;
    let mut tr: Option<TrailConfig> = None;
    for e in effects {
        match e {
            AnimationEffect::MotionBlur(c) if mb.is_none() => mb = Some(c.clone()),
            AnimationEffect::Trail(c) if tr.is_none() => tr = Some(c.clone()),
            _ => {}
        }
    }
    (mb, tr)
}

/// Build ghost `BoxNode`s for motion-blur or trail effects.
///
/// Returns a `Vec<BoxNode>` of ghosts to be prepended (painted underneath)
/// the principal node. Each ghost gets a fresh `NodeId` and its own slot in
/// `components`/`stagger_delays`, both pointing to the same `ChildComponent`
/// as the principal (v1 approximation: internal content uses frame time, not
/// ghost time).
///
/// # Ghost CSS
///
/// For **motion_blur**: the CSS is resolved at `t_ghost = t - i * (shutter/fps)/samples`
/// and opacity is `base_opacity / (samples + 1)`. The principal's opacity is
/// kept at its base value so the static (no-motion) case stays visually full.
///
/// For **trail**: the CSS is resolved at `t_ghost = t - i * spacing` and
/// opacity is `base_opacity * falloff^i`. The principal is unchanged.
///
/// Only one of `mb` / `tr` is used at a time; if both are present, motion_blur
/// takes priority.
#[allow(clippy::too_many_arguments)]
fn build_ghosts<'a>(
    child: &'a ChildComponent,
    components: &mut Vec<Option<&'a ChildComponent>>,
    stagger_delays: &mut Vec<f64>,
    time_params: &mut Vec<(f64, f64)>,
    next_id: &mut NodeId,
    actx: BuildAnimationCtx,
    stagger_delay: f64,
    time_remap: (f64, f64),
    effects: &[AnimationEffect],
    parent_css: &CssStyle,
) -> Vec<BoxNode> {
    let (mb, tr) = detect_ghost_effects(effects);

    // Choose the ghost generation strategy.
    enum Strategy {
        MotionBlur {
            samples: u32,
            shutter_window: f64,
        },
        Trail {
            copies: u32,
            spacing: f64,
            falloff: f32,
        },
    }
    let strategy = if let Some(mc) = mb {
        let samples = mc.samples.clamp(1, 16);
        if samples <= 1 {
            // Degenerate: no ghosts needed (ghost = principal position).
            return Vec::new();
        }
        let shutter_window = mc.shutter / actx.fps.max(1) as f64;
        Strategy::MotionBlur {
            samples,
            shutter_window,
        }
    } else if let Some(tc) = tr {
        let copies = tc.copies.clamp(1, 12);
        Strategy::Trail {
            copies,
            spacing: tc.spacing,
            falloff: tc.falloff,
        }
    } else {
        return Vec::new();
    };

    let base_css_for_ghost = |ghost_time: f64, ghost_opacity_scale: f32| -> CssStyle {
        // Start from the same base CSS as the principal.
        let mut css = component_css(&child.component);
        if let Some((x, y)) = child.absolute_position() {
            css.position = Some(Position::Absolute);
            css.left = Some(CLP::Px(x));
            css.top = Some(CLP::Px(y));
        }
        if let Some(z) = child.z_index {
            css.z_index = Some(z);
        }
        // Cascade: a ghost is the same component as the principal, painted
        // at a different sampled time, so it inherits from the same parent.
        rustmotion_core::css::cascade::inherit_from(parent_css, &mut css);
        // Apply timeline style states at the ghost time.
        if let Some(animatable) = child.component.as_animatable() {
            let steps = animatable.timeline_steps();
            if steps.iter().any(|s| s.style.is_some()) {
                let skip_opacity = css.transition.is_some();
                apply_style_states(&mut css, steps, ghost_time - stagger_delay, skip_opacity);
                // Same `border-radius`/`background` smoothing as the
                // principal path in `build_child`, sampled at `ghost_time`
                // so a motion-blur/trail ghost mid-transition matches what
                // the principal will look like at that same instant.
                let overrides = resolve_transition_css_overrides(
                    child.component.as_styled().style_config(),
                    steps,
                    ghost_time - stagger_delay,
                );
                if let Some(br) = overrides.border_radius {
                    css.border_radius = Some(br);
                }
                if let Some(bg) = overrides.background {
                    css.background = Some(bg);
                }
            }
        }
        // Resolve animation props at the *ghost* time.
        let ghost_actx = BuildAnimationCtx {
            time: ghost_time,
            scenario_time: actx.scenario_time,
            scene_duration: actx.scene_duration,
            fps: actx.fps,
        };
        if let Some(ghost_effects) = effective_effects(&child.component, stagger_delay) {
            let props = resolve_props_for_effects(
                &ghost_effects,
                ghost_actx.time,
                ghost_actx.scene_duration,
            );
            if props_has_paint_overrides(&props) {
                apply_animated_props(&mut css, &props);
            }
            apply_glow_effect(&mut css, &ghost_effects);
            carry_paint_pass_effects(&mut css, &ghost_effects);
        }
        // Scale opacity: multiply the base opacity by the ghost opacity factor.
        let base_opacity = css.opacity.unwrap_or(1.0);
        css.opacity = Some((base_opacity * ghost_opacity_scale).clamp(0.0, 1.0));
        css
    };

    let mut ghosts = Vec::new();

    match strategy {
        Strategy::MotionBlur {
            samples,
            shutter_window,
        } => {
            // Ghost opacity: 1 / (samples + 1) of base opacity.
            // Principal stays at full base opacity (handled in build_child).
            let ghost_opacity_scale = 1.0 / (samples + 1) as f32;
            for i in 1..=samples {
                let ghost_time = actx.time - (i as f64 * shutter_window / samples as f64);
                let ghost_css = base_css_for_ghost(ghost_time, ghost_opacity_scale);

                let ghost_id = *next_id;
                *next_id += 1;
                // Register a slot so the dispatcher can look up the component.
                components.push(Some(child));
                stagger_delays.push(stagger_delay);
                time_params.push(time_remap);

                ghosts.push(BoxNode {
                    id: ghost_id,
                    kind: BoxKind::Ghost(Arc::new(ghost_id)),
                    css: ghost_css,
                    children: Vec::new(), // v1: no child recursion in ghosts
                    intrinsic: None,      // v1: ghosts have no layout-measured content
                    source_path: None,
                    window: None,
                });
            }
        }
        Strategy::Trail {
            copies,
            spacing,
            falloff,
        } => {
            // Ghosts painted from oldest (most-trailing) to newest (closest to principal).
            // We build them in reverse order (copies → 1) so that index i=copies
            // is the oldest ghost (lowest opacity), and we then reverse to get the
            // correct under-to-over paint order.
            let mut trail_nodes = Vec::with_capacity(copies as usize);
            for i in 1..=copies {
                let ghost_time = actx.time - i as f64 * spacing;
                let ghost_opacity_scale = falloff.powi(i as i32);
                let ghost_css = base_css_for_ghost(ghost_time, ghost_opacity_scale);

                let ghost_id = *next_id;
                *next_id += 1;
                components.push(Some(child));
                stagger_delays.push(stagger_delay);
                time_params.push(time_remap);

                trail_nodes.push(BoxNode {
                    id: ghost_id,
                    kind: BoxKind::Ghost(Arc::new(ghost_id)),
                    css: ghost_css,
                    children: Vec::new(),
                    intrinsic: None,
                    source_path: None,
                    window: None,
                });
            }
            // Oldest ghost (most-trailing) painted first → prepend in reverse.
            trail_nodes.reverse();
            ghosts = trail_nodes;
        }
    }

    ghosts
}

/// Convert a single `ChildComponent` into one or more `BoxNode`s.
///
/// Returns a `Vec` whose elements are inserted in order into the parent's
/// `children`. The last element is the principal node; any preceding elements
/// are `BoxKind::Ghost` nodes inserted *before* (painted underneath) the
/// principal for motion-blur or trail effects.
///
/// `stagger_delay` is the animation delay accumulated from ancestor
/// containers' `stagger` fields.
/// `time_remap` is the accumulated affine time transform `(scale, shift)` where
/// `t_local = scale * t_global + shift`. Default is `(1.0, 0.0)` (identity).
#[allow(clippy::too_many_arguments)]
fn build_child<'a>(
    child: &'a ChildComponent,
    components: &mut Vec<Option<&'a ChildComponent>>,
    stagger_delays: &mut Vec<f64>,
    time_params: &mut Vec<(f64, f64)>,
    next_id: &mut NodeId,
    anim: Option<BuildAnimationCtx>,
    path: String,
    stagger_delay: f64,
    time_remap: (f64, f64),
    parent_css: &CssStyle,
) -> Vec<BoxNode> {
    // Compute the local animation context for this node — remapped by the
    // accumulated affine time transform from ancestor containers.
    // `t_local = scale * t_global + shift`
    let local_actx = anim.map(|a| {
        let (scale, shift) = time_remap;
        BuildAnimationCtx {
            time: a.time * scale + shift,
            // A container's `time_scale`/`time_offset` remaps the *animation*
            // clock of its subtree. The audio is not part of that subtree —
            // it plays at wall-clock speed regardless — so the scenario clock
            // passes through unremapped.
            scenario_time: a.scenario_time,
            scene_duration: a.scene_duration,
            fps: a.fps,
        }
    });

    // ── Ghost generation (motion_blur / trail) ───────────────────────────────
    // Must happen before allocating the principal's id so that ghost ids are
    // lower (earlier in the slot table). The principal's id is allocated below.
    let mut ghosts: Vec<BoxNode> = Vec::new();
    if let Some(actx) = local_actx {
        if let Some(effects) = effective_effects(&child.component, stagger_delay) {
            ghosts = build_ghosts(
                child,
                components,
                stagger_delays,
                time_params,
                next_id,
                actx,
                stagger_delay,
                time_remap,
                &effects,
                parent_css,
            );
        }
    }

    let id = *next_id;
    *next_id += 1;
    components.push(Some(child));
    stagger_delays.push(stagger_delay);
    time_params.push(time_remap);

    let mut css = component_css(&child.component);

    // Apply per-child position/z-index from the wrapper.
    if let Some((x, y)) = child.absolute_position() {
        css.position = Some(Position::Absolute);
        css.left = Some(CLP::Px(x));
        css.top = Some(CLP::Px(y));
    }
    if let Some(z) = child.z_index {
        css.z_index = Some(z);
    }

    // CSS cascade (round 4 audit, lot LAYOUT, constat 2): propagate
    // inheritable properties (color, font-*, text-align, white-space, ...)
    // from the parent's already-cascaded style into any of this node's own
    // unset properties — mirrors CSS's "specified value" resolution, which
    // happens before state/animation overrides compute the final value.
    // `crates/rustmotion-core/src/css/cascade.rs::inherit_from` existed but
    // nothing called it until this fix.
    rustmotion_core::css::cascade::inherit_from(parent_css, &mut css);

    // Timeline style states: merge every state whose (at + stagger) <= t
    // into the box CSS. Opacity is excluded when a `transition` smooths it
    // (the synthesized keyframes then own its whole history). States affect
    // box-model properties (layout, background, border, opacity, transform,
    // filter); painter-internal properties like text color flow through the
    // keyframes path below instead.
    if let Some(animatable) = child.component.as_animatable() {
        let steps = animatable.timeline_steps();
        if steps.iter().any(|s| s.style.is_some()) {
            let t = local_actx.map(|a| a.time).unwrap_or(0.0);
            let skip_opacity = css.transition.is_some();
            apply_style_states(&mut css, steps, t - stagger_delay, skip_opacity);
            // `border-radius`/`background` (solid colour, uniform absolute
            // px only — see `resolve_transition_css_overrides`'s doc
            // comment) smooth the same way opacity does above, but land
            // directly on `css` instead of through the generic effects
            // pipeline: no `AnimatedProperties` field for them is ever read
            // by a painter, so that pipeline is a dead end for these two.
            let overrides = resolve_transition_css_overrides(
                child.component.as_styled().style_config(),
                steps,
                t - stagger_delay,
            );
            if let Some(br) = overrides.border_radius {
                css.border_radius = Some(br);
            }
            if let Some(bg) = overrides.background {
                css.background = Some(bg);
            }
        }
    }

    // Resolve animations and apply transform/opacity/filter overrides on the
    // box's CSS. Paint-time properties (transform, opacity, filter,
    // perspective) plus the box size (`width`/`height`, which taffy needs so a
    // resize reflows its children instead of stretching pixels) flow into CSS
    // — internal animations like draw_progress or char_animation remain on the
    // `AnimatedProperties` legacy path.
    if let Some(actx) = local_actx {
        if let Some(effects) = effective_effects(&child.component, stagger_delay) {
            let props = resolve_props_for_effects(&effects, actx.time, actx.scene_duration);
            if props_has_paint_overrides(&props) {
                apply_animated_props(&mut css, &props);
            }
            apply_glow_effect(&mut css, &effects);
            carry_paint_pass_effects(&mut css, &effects);
        }
    }

    // ── Audio-reactive binding ────────────────────────────────────────────────
    // Reads the audio analysis cache and lerps the target CSS property between
    // min and max. Cache-miss → value = min (deterministic fallback).
    if let Some(ar) = css.audio_reactive.take() {
        use rustmotion_core::css::style::{AudioReactiveProperty, AudioSource, AudioSourceTag};
        use rustmotion_core::engine::renderer::audio_analysis::audio_analysis_cache;

        if let Some(actx) = local_actx {
            let cache = audio_analysis_cache();
            let analysis_opt = if let Some(ref src) = ar.track {
                cache.get(src).map(|r| r.clone())
            } else {
                cache.iter().next().map(|r| r.value().clone())
            };

            let raw = if let Some(analysis) = analysis_opt {
                match &ar.source {
                    AudioSource::Amplitude(AudioSourceTag::Amplitude) => {
                        analysis.amplitude_smoothed(actx.scenario_time, ar.smoothing_frames)
                    }
                    AudioSource::Band { band } => {
                        analysis.band_smoothed(actx.scenario_time, *band, ar.smoothing_frames)
                    }
                }
            } else {
                0.0 // cache empty → use min
            };

            let lerped = ar.min as f32 + raw * (ar.max - ar.min) as f32;

            match ar.property {
                AudioReactiveProperty::Opacity => {
                    let base = css.opacity.unwrap_or(1.0);
                    css.opacity = Some(base * lerped.clamp(0.0, 1.0));
                }
                AudioReactiveProperty::Scale => {
                    let s = lerped.max(0.0);
                    let tx = css.transform.get_or_insert_with(Vec::new);
                    tx.push(rustmotion_core::css::style::TransformFn::Scale { x: s, y: s });
                }
                AudioReactiveProperty::TranslateY => {
                    use rustmotion_core::css::units::LengthPercentage;
                    let tx = css.transform.get_or_insert_with(Vec::new);
                    tx.push(rustmotion_core::css::style::TransformFn::TranslateY {
                        y: LengthPercentage::Px(lerped),
                    });
                }
                AudioReactiveProperty::Rotation => {
                    let tx = css.transform.get_or_insert_with(Vec::new);
                    tx.push(rustmotion_core::css::style::TransformFn::Rotate { deg: lerped });
                }
            }
        }
    }

    // Visibility window (start_at/end_at) — enforced by the paint pass.
    // The stagger delay shifts the window too, so a hard-cut child appears
    // in step with its staggered siblings.
    // When there is an accumulated time remap, the window times (which are in
    // local/remapped time) must be converted back to global time so the paint
    // pass (which operates on global time) can apply them correctly.
    // If `t_local = scale * t_global + shift`, then `t_global = (t_local - shift) / scale`.
    let window = child.component.as_timed().and_then(|t| {
        let (start, end) = t.timing();
        (start.is_some() || end.is_some()).then_some({
            let (scale, shift) = time_remap;
            let to_global = |t_local: f64| -> f64 {
                if scale.abs() < 1e-10 {
                    t_local
                } else {
                    (t_local - shift) / scale
                }
            };
            rustmotion_core::engine::box_tree::PaintWindow {
                start: start.map(|s| to_global(s + stagger_delay)),
                end: end.map(|e| to_global(e + stagger_delay)),
            }
        })
    });

    let children_boxes = container_children(
        &child.component,
        components,
        stagger_delays,
        time_params,
        next_id,
        anim,
        &path,
        stagger_delay,
        time_remap,
        &css,
    );
    let intrinsic = component_intrinsic(&child.component);

    let principal = BoxNode {
        id,
        kind: BoxKind::Component(Arc::new(id)),
        css,
        children: children_boxes,
        intrinsic,
        source_path: Some(path),
        window,
    };

    // Ghosts prepended (painted underneath the principal).
    let mut result = ghosts;
    result.push(principal);
    result
}

/// The full effect list for a component at paint time: `style.animation`,
/// plus `timeline` steps shifted by their `at`, plus keyframes synthesized
/// from timeline style-state changes (`style.transition`), plus the
/// container-stagger delay applied to everything. Returns `None` when there
/// is nothing to resolve, `Some(Cow::Borrowed)` on the no-merge fast path.
pub fn effective_effects(
    component: &Component,
    extra_delay: f64,
) -> Option<std::borrow::Cow<'_, [rustmotion_core::schema::AnimationEffect]>> {
    let animatable = component.as_animatable()?;
    let effects = animatable.animation_effects();
    let steps = animatable.timeline_steps();
    // `color` keyframes drive AnimatedProperties.color, consumed by the
    // text-like painters only.
    let smooth_color = matches!(component, Component::Text(_) | Component::Counter(_));
    let synthesized =
        transition_keyframes(component.as_styled().style_config(), steps, smooth_color);
    if steps.is_empty() && synthesized.is_empty() && extra_delay == 0.0 {
        return (!effects.is_empty()).then_some(std::borrow::Cow::Borrowed(effects));
    }
    let mut merged = effects.to_vec();
    for step in steps {
        for effect in &step.animation {
            let mut e = effect.clone();
            e.shift_delay(step.at);
            merged.push(e);
        }
    }
    merged.extend(synthesized);
    if extra_delay != 0.0 {
        for e in &mut merged {
            e.shift_delay(extra_delay);
        }
    }
    (!merged.is_empty()).then_some(std::borrow::Cow::Owned(merged))
}

/// Merge every timeline style state whose `at <= t` into `css`, in `at`
/// order. Serialize-merge keeps this schema-complete; `null`s and the empty
/// `animation` array never erase existing values. `skip_opacity` leaves
/// opacity to the synthesized transition keyframes.
pub(crate) fn apply_style_states(
    css: &mut CssStyle,
    steps: &[rustmotion_core::schema::TimelineStep],
    t: f64,
    skip_opacity: bool,
) {
    let mut due: Vec<&rustmotion_core::schema::TimelineStep> = steps
        .iter()
        .filter(|s| s.style.is_some() && s.at <= t)
        .collect();
    if due.is_empty() {
        return;
    }
    due.sort_by(|a, b| a.at.total_cmp(&b.at));

    let Ok(serde_json::Value::Object(mut base)) = serde_json::to_value(&*css) else {
        return;
    };
    for step in due {
        let Some(style) = step.style.as_deref() else {
            continue;
        };
        let Ok(serde_json::Value::Object(state)) = serde_json::to_value(style) else {
            continue;
        };
        for (k, v) in state {
            if v.is_null() {
                continue;
            }
            if k == "animation" && v.as_array().is_some_and(|a| a.is_empty()) {
                continue;
            }
            if skip_opacity && k == "opacity" {
                continue;
            }
            base.insert(k, v);
        }
    }
    if let Ok(merged) = serde_json::from_value::<CssStyle>(serde_json::Value::Object(base)) {
        *css = merged;
    }
}

/// Synthesize `Keyframes` effects smoothing timeline style-state changes per
/// the component's `style.transition`. Supported: `opacity` (as ratios of the
/// base opacity — `apply_animated_props` multiplies) and, on text-like
/// components, `color` (absolute, via `AnimatedProperties.color`). Color
/// states without a transition still synthesize a near-instant ramp because
/// text painters only see color through this path.
pub(crate) fn transition_keyframes(
    base: &CssStyle,
    steps: &[rustmotion_core::schema::TimelineStep],
    smooth_color: bool,
) -> Vec<rustmotion_core::schema::AnimationEffect> {
    use rustmotion_core::schema::{
        Animation, AnimationEffect, Keyframe, KeyframeValue, KeyframesConfig,
    };

    let has_states = steps.iter().any(|s| s.style.is_some());
    if !has_states {
        return Vec::new();
    }
    let (duration, easing) = match base.transition.as_ref() {
        Some(tr) if tr.duration() > 0.0 => (tr.duration(), tr.easing()),
        // Color snaps still need the keyframes path (see doc above); a 1ms
        // ramp is visually a hard cut.
        _ => (0.001, rustmotion_core::schema::EasingType::Linear),
    };
    let smooth_opacity = base.transition.is_some();

    let mut sorted: Vec<&rustmotion_core::schema::TimelineStep> =
        steps.iter().filter(|s| s.style.is_some()).collect();
    sorted.sort_by(|a, b| a.at.total_cmp(&b.at));

    let base_opacity = base.opacity.unwrap_or(1.0) as f64;
    let mut opacity_kfs: Vec<Keyframe> = Vec::new();
    let mut color_kfs: Vec<Keyframe> = Vec::new();
    let mut prev_opacity = base_opacity;
    let mut prev_color = base.color.as_ref().map(|c| c.to_css_string());

    let kf_num = |time: f64, v: f64| Keyframe {
        time,
        value: KeyframeValue::Number(v),
        easing: None,
    };
    let kf_color = |time: f64, c: String| Keyframe {
        time,
        value: KeyframeValue::Color(c),
        easing: None,
    };
    // Keep keyframe times strictly ascending even when states overlap a
    // still-running transition (the resolver walks ordered segments).
    let push_pair = |kfs: &mut Vec<Keyframe>, at: f64, from: Keyframe, to: Keyframe| {
        let floor = kfs.last().map(|k| k.time + 1e-6).unwrap_or(f64::MIN);
        let start = at.max(floor);
        let mut from = from;
        let mut to = to;
        from.time = start;
        to.time = to.time.max(start + 1e-6);
        kfs.push(from);
        kfs.push(to);
    };

    for step in sorted {
        let style = step.style.as_deref().unwrap();
        if smooth_opacity && base_opacity > 1e-6 {
            if let Some(o) = style.opacity {
                let target = o as f64;
                if (target - prev_opacity).abs() > 1e-6 {
                    push_pair(
                        &mut opacity_kfs,
                        step.at,
                        kf_num(step.at, prev_opacity / base_opacity),
                        kf_num(step.at + duration, target / base_opacity),
                    );
                    prev_opacity = target;
                }
            }
        }
        if smooth_color {
            if let Some(c) = style.color.as_ref() {
                let target = c.to_css_string();
                if prev_color.as_ref() != Some(&target) {
                    if let Some(from) = prev_color.clone() {
                        push_pair(
                            &mut color_kfs,
                            step.at,
                            kf_color(step.at, from),
                            kf_color(step.at + duration, target.clone()),
                        );
                    }
                    prev_color = Some(target);
                }
            }
        }
    }

    let mut out = Vec::new();
    let mut push_effect = |property: &str, keyframes: Vec<Keyframe>| {
        if keyframes.is_empty() {
            return;
        }
        out.push(AnimationEffect::Keyframes(KeyframesConfig {
            keyframes: vec![Animation {
                property: property.to_string(),
                keyframes,
                easing: easing.clone(),
                spring: None,
            }],
            delay: 0.0,
            duration: 0.0,
            repeat: false,
        }));
    };
    push_effect("opacity", opacity_kfs);
    push_effect("color", color_kfs);
    out
}

/// Resolved `style.transition` smoothing for `border-radius`/`background`,
/// ready to be written straight onto a `CssStyle`.
pub(crate) struct TransitionCssOverrides {
    pub border_radius: Option<rustmotion_core::css::style::BorderRadius>,
    pub background: Option<rustmotion_core::css::style::Background>,
}

/// CSS-native smoothing for `border-radius` (uniform, absolute-px only) and
/// `background` (solid colour only) timeline style-state changes.
///
/// Unlike `opacity`/`color` in `transition_keyframes` above, neither
/// property has anywhere to land in `AnimatedProperties` that any painter or
/// the CSS bridge (`css/animation.rs::apply_animated_props`) actually reads
/// (see `KNOWN_ANIMATABLE_PROPERTIES`'s doc comment in `animator.rs`) —
/// every painter reads `css.border_radius`/`css.background` straight off
/// the node's own `CssStyle` (`paint_pass.rs`, frozen, already does this for
/// the static case). So instead of synthesizing an `AnimationEffect` for the
/// generic effects pipeline (a dead end for these two), this resolves the
/// interpolated value directly and returns it for the caller to write onto
/// the box's `CssStyle` by hand — reusing `animator::resolve_keyframe_track`
/// for the actual segment/easing/spring math rather than reinventing it.
///
/// Gated on `style.transition` being set, mirroring `opacity`'s gate above
/// (not `color`'s forced near-instant ramp — nothing downstream *requires*
/// these two to smooth the way text painters require `color` to). Absent an
/// explicit `style.transition`, this returns an all-`None` result, so every
/// existing scenario without one renders byte-identical to before this
/// workstream.
///
/// **"Unités mixtes" decision** (see workstream report): resolves only when
/// *both* the origin and the target value are the exact shape
/// `BorderRadius::absolute_px`/`Background::solid_hex` can resolve without a
/// `LengthContext` — uniform absolute px, solid colour. Anything else
/// (per-corner radii, `%`/`em`/`rem`/`vw`/`vh`, gradients, image layers) is
/// refused rather than guessed: that property just falls back to
/// `apply_style_states`'s existing snap. `validate_schema.rs` calls the
/// exact same two predicates so the diagnostic and the runtime can never
/// disagree about what's interpolable.
pub(crate) fn resolve_transition_css_overrides(
    base: &CssStyle,
    steps: &[rustmotion_core::schema::TimelineStep],
    t: f64,
) -> TransitionCssOverrides {
    use rustmotion_core::css::style::{Background, BorderRadius, Color};
    use rustmotion_core::css::units::LengthPercentage as CssLP;
    use rustmotion_core::engine::animator::resolve_keyframe_track;
    use rustmotion_core::schema::{Animation, Keyframe, KeyframeValue};

    let mut out = TransitionCssOverrides {
        border_radius: None,
        background: None,
    };
    let Some(tr) = base.transition.as_ref() else {
        return out;
    };
    if tr.duration() <= 0.0 {
        return out;
    }
    let duration = tr.duration();
    let easing = tr.easing();

    let mut sorted: Vec<&rustmotion_core::schema::TimelineStep> =
        steps.iter().filter(|s| s.style.is_some()).collect();
    sorted.sort_by(|a, b| a.at.total_cmp(&b.at));

    let mut prev_radius = base
        .border_radius
        .as_ref()
        .and_then(BorderRadius::absolute_px);
    let mut prev_bg = base.background.as_ref().and_then(Background::solid_hex);
    let mut radius_kfs: Vec<Keyframe> = Vec::new();
    let mut bg_kfs: Vec<Keyframe> = Vec::new();

    // Same ascending-time bookkeeping as `transition_keyframes`'s
    // `push_pair` above (kept local — a shared closure can't easily borrow
    // two different `Vec`s across both loops below without upsetting the
    // borrow checker for no real benefit at this size).
    let push_pair = |kfs: &mut Vec<Keyframe>, at: f64, from: Keyframe, to: Keyframe| {
        let floor = kfs.last().map(|k| k.time + 1e-6).unwrap_or(f64::MIN);
        let start = at.max(floor);
        let mut from = from;
        let mut to = to;
        from.time = start;
        to.time = to.time.max(start + 1e-6);
        kfs.push(from);
        kfs.push(to);
    };

    for step in sorted {
        let style = step.style.as_deref().unwrap();
        if let Some(br) = style.border_radius.as_ref() {
            match br.absolute_px() {
                Some(target) => {
                    if prev_radius != Some(target) {
                        if let Some(from) = prev_radius {
                            push_pair(
                                &mut radius_kfs,
                                step.at,
                                Keyframe {
                                    time: step.at,
                                    value: KeyframeValue::Number(from as f64),
                                    easing: None,
                                },
                                Keyframe {
                                    time: step.at + duration,
                                    value: KeyframeValue::Number(target as f64),
                                    easing: None,
                                },
                            );
                        }
                        prev_radius = Some(target);
                    }
                }
                // Unresolvable shape (per-corner, %/em/rem/vw/vh) — lose the
                // interpolation origin for *this* transition only;
                // `validate_schema.rs` diagnoses this exact step, and a
                // later resolvable value simply resumes smoothing from
                // itself onward (see the doc comment above).
                None => prev_radius = None,
            }
        }
        if let Some(bg) = style.background.as_ref() {
            match bg.solid_hex() {
                Some(target) => {
                    if prev_bg.as_deref() != Some(target.as_str()) {
                        if let Some(from) = prev_bg.clone() {
                            push_pair(
                                &mut bg_kfs,
                                step.at,
                                Keyframe {
                                    time: step.at,
                                    value: KeyframeValue::Color(from),
                                    easing: None,
                                },
                                Keyframe {
                                    time: step.at + duration,
                                    value: KeyframeValue::Color(target.clone()),
                                    easing: None,
                                },
                            );
                        }
                        prev_bg = Some(target);
                    }
                }
                None => prev_bg = None,
            }
        }
    }

    if !radius_kfs.is_empty() {
        let anim = Animation {
            property: "border_radius".to_string(),
            keyframes: radius_kfs,
            easing: easing.clone(),
            spring: None,
        };
        if let KeyframeValue::Number(v) = resolve_keyframe_track(&anim, t) {
            out.border_radius = Some(BorderRadius::Uniform(CssLP::Px(v as f32)));
        }
    }
    if !bg_kfs.is_empty() {
        let anim = Animation {
            property: "background".to_string(),
            keyframes: bg_kfs,
            easing,
            spring: None,
        };
        if let KeyframeValue::Color(c) = resolve_keyframe_track(&anim, t) {
            out.background = Some(Background::Color(Color::String(c)));
        }
    }
    out
}

/// Quick gate: does this resolved `AnimatedProperties` carry any property
/// that we know how to translate to CSS? Avoids allocating a transform Vec
/// when there's nothing to apply.
fn props_has_paint_overrides(p: &AnimatedProperties) -> bool {
    p.translate_x != 0.0
        || p.translate_y != 0.0
        || (p.scale_x - 1.0).abs() > 1e-4
        || (p.scale_y - 1.0).abs() > 1e-4
        || p.rotation.abs() > 1e-3
        || p.rotate_x.abs() > 1e-3
        || p.rotate_y.abs() > 1e-3
        || (p.opacity - 1.0).abs() > 1e-4
        || p.blur > 0.0
        || (p.glow_radius > 0.0 && p.glow_intensity > 0.0)
        || p.perspective > 0.0
        // An animated box size is a *layout* override rather than a paint one,
        // but it travels through the same bridge, so the gate has to let it
        // through or `apply_animated_props` never runs for a scenario whose
        // only animated property is `width`/`height` (the card-resize case).
        // -1.0 is the animator's "never animated" sentinel.
        || p.width >= 0.0
        || p.height >= 0.0
}

/// Hand the effects that the *paint pass* resolves for itself down to the box
/// node, in their delay-shifted form.
///
/// Everything else on this path is resolved here and lands on `css` as a
/// finished value. `shimmer` cannot be: it composites against the pixels the
/// node paints, which do not exist until the paint pass has run. So the paint
/// pass reads it off `css.animation` — and it has to read the *shifted* copy
/// (container stagger, `timeline` step `at`) rather than the author's raw
/// `style.animation`, or a shimmer inside a staggered list would sweep in step
/// with the list's first item instead of its own.
fn carry_paint_pass_effects(
    css: &mut CssStyle,
    effects: &[rustmotion_core::schema::AnimationEffect],
) {
    use rustmotion_core::schema::AnimationEffect;
    if effects
        .iter()
        .any(|e| matches!(e, AnimationEffect::Shimmer(_)))
    {
        css.animation = effects
            .iter()
            .filter(|e| matches!(e, AnimationEffect::Shimmer(_)))
            .cloned()
            .collect();
    }
}

/// M3: apply the static `glow` animation effect (a coloured halo — not
/// time-varying, see `AnimationEffect::shift_delay`'s doc comment) as a CSS
/// `filter: drop-shadow(...)`.
///
/// This is deliberately *not* routed through `resolve_props_for_effects` /
/// `AnimatedProperties::glow_radius`+`glow_intensity` even though those
/// fields exist: `AnimatedProperties`'s public shape is frozen for this
/// workstream (can't add a `glow_color` field), and the existing bridge that
/// *does* consume those two fields — `css::animation::apply_animated_props`,
/// in a file outside this workstream's scope — hardcodes `color: None` on
/// the `DropShadow` it builds, which resolves to black. Piping the `glow`
/// effect through that path would either still render black, or — if we did
/// find a way to set the color fields too — double up into two stacked
/// drop-shadows (one colourless from the frozen bridge, one coloured from
/// here). Building the filter directly here, from the raw `GlowConfig`
/// (found via `animator::find_glow_effect`), keeps it a single, correctly
/// coloured shadow and needs no change to any frozen file.
///
/// The pre-existing `glow_radius`/`glow_intensity` numeric-property path
/// (animating those as arbitrary `keyframes` targets, unrelated to this
/// named `glow` effect) is untouched and still produces a colourless halo —
/// that is `css::animation.rs`'s DropShadow-with-`color:None` bug, out of
/// this workstream's file ownership.
fn apply_glow_effect(css: &mut CssStyle, effects: &[rustmotion_core::schema::AnimationEffect]) {
    use rustmotion_core::css::style::{Color, FilterFn};
    use rustmotion_core::css::units::Length;
    use rustmotion_core::engine::animator::find_glow_effect;

    let Some(glow) = find_glow_effect(effects) else {
        return;
    };

    let (r, g, b, a) = rustmotion_core::engine::renderer::parse_css_color(&glow.color)
        .unwrap_or((255, 255, 255, 255));
    let alpha = ((a as f32 / 255.0) * glow.intensity.max(0.0)).clamp(0.0, 1.0);
    let radius = glow.radius.max(0.0);
    if radius <= 0.0 || alpha <= 0.0 {
        return;
    }

    let shadow = FilterFn::DropShadow {
        offset_x: Length::Px(0.0),
        offset_y: Length::Px(0.0),
        blur: Some(Length::Px(radius)),
        color: Some(Color::Rgba { r, g, b, a: alpha }),
    };
    css.filter.get_or_insert_with(Vec::new).push(shadow);
}

/// Build an [`IntrinsicMeasure`] for components whose box size depends on
/// their content (text, codeblock, terminal, etc.). Returns `None` for
/// components with explicit dimensions or pure containers.
fn component_intrinsic(
    component: &Component,
) -> Option<Arc<dyn rustmotion_core::engine::box_tree::IntrinsicMeasure>> {
    use Component::*;
    match component {
        Text(t) => Some(Arc::new(crate::intrinsic::TextIntrinsic::from_text(t))),
        GradientText(t) => Some(Arc::new(
            crate::intrinsic::GradientTextIntrinsic::from_gradient_text(t),
        )),
        Caption(c) => Some(Arc::new(crate::intrinsic::CaptionIntrinsic::from_caption(
            c,
        ))),
        Kbd(k) => Some(Arc::new(crate::intrinsic::KbdIntrinsic::from_kbd(k))),
        Counter(c) => Some(Arc::new(crate::intrinsic::CounterIntrinsic::from_counter(
            c,
        ))),
        NumberWheel(w) => Some(Arc::new(
            crate::intrinsic::NumberWheelIntrinsic::from_number_wheel(w),
        )),
        Badge(b) => Some(Arc::new(crate::intrinsic::BadgeIntrinsic::from_badge(b))),
        Terminal(t) => Some(Arc::new(
            crate::intrinsic::TerminalIntrinsic::from_terminal(t),
        )),
        Table(t) => Some(Arc::new(crate::intrinsic::TableIntrinsic::from_table(t))),
        Codeblock(c) => Some(Arc::new(
            crate::intrinsic::CodeblockIntrinsic::from_codeblock(c),
        )),
        // M2: rich_text had no intrinsic measurer at all, so it laid out
        // 0×0 and rendered nothing unless the author guessed an explicit
        // width/height.
        RichText(rt) => Some(Arc::new(
            crate::intrinsic::RichTextIntrinsic::from_rich_text(rt),
        )),
        _ => None,
    }
}

/// If the component is a container, recurse into its children. Otherwise
/// return an empty Vec. A container's `stagger` adds `index * stagger`
/// to each child's inherited animation delay (cumulative across nesting).
/// `time_remap` is the accumulated affine time transform `(scale, shift)` for
/// this container node; the container's own `time_scale`/`time_offset` are
/// composed in to produce the remap for children.
#[allow(clippy::too_many_arguments)]
fn container_children<'a>(
    component: &'a Component,
    components: &mut Vec<Option<&'a ChildComponent>>,
    stagger_delays: &mut Vec<f64>,
    time_params: &mut Vec<(f64, f64)>,
    next_id: &mut NodeId,
    anim: Option<BuildAnimationCtx>,
    parent_path: &str,
    inherited_delay: f64,
    time_remap: (f64, f64),
    parent_css: &CssStyle,
) -> Vec<BoxNode> {
    let (children, stagger, child_scale, child_offset): (&[ChildComponent], Option<f32>, f64, f64) =
        match component {
            Component::Card(c) => (
                &c.children,
                c.stagger,
                c.time_scale.unwrap_or(1.0),
                c.time_offset.unwrap_or(0.0),
            ),
            Component::Flex(c) => (
                &c.children,
                c.stagger,
                c.time_scale.unwrap_or(1.0),
                c.time_offset.unwrap_or(0.0),
            ),
            Component::Grid(c) => (
                &c.children,
                c.stagger,
                c.time_scale.unwrap_or(1.0),
                c.time_offset.unwrap_or(0.0),
            ),
            Component::Container(c) => (
                &c.children,
                c.stagger,
                c.time_scale.unwrap_or(1.0),
                c.time_offset.unwrap_or(0.0),
            ),
            Component::Positioned(c) => (
                &c.children,
                None,
                c.time_scale.unwrap_or(1.0),
                c.time_offset.unwrap_or(0.0),
            ),
            _ => return Vec::new(),
        };

    // Clamp scale defensively to avoid division-by-zero downstream.
    let child_scale = child_scale.max(1e-6);

    // Compose the container's time remap with the inherited (accumulated) remap.
    // Accumulated remap: `t_parent = scale_acc * t_global + shift_acc`
    // Container formula: `t_child = (t_parent - child_offset) * child_scale`
    //   = child_scale * (scale_acc * t_global + shift_acc) - child_scale * child_offset
    //   = (child_scale * scale_acc) * t_global + child_scale * (shift_acc - child_offset)
    let (scale_acc, shift_acc) = time_remap;
    let new_scale = child_scale * scale_acc;
    let new_shift = child_scale * (shift_acc - child_offset);
    let child_remap = (new_scale, new_shift);

    // `anim` stays GLOBAL all the way down the recursion — each `build_child`
    // derives its node-local time from the accumulated `child_remap`. Passing
    // a pre-remapped ctx here would double-apply the transform.
    let step = stagger.unwrap_or(0.0) as f64;
    let mut result = Vec::new();
    for (j, c) in children.iter().enumerate() {
        result.extend(build_child(
            c,
            components,
            stagger_delays,
            time_params,
            next_id,
            anim,
            format!("{parent_path}/children/{j}"),
            inherited_delay + j as f64 * step,
            child_remap,
            parent_css,
        ));
    }
    result
}

/// Pull the component's `CssStyle`, augmented with intrinsic `width`/`height`
/// for components that carry a fixed size.
fn component_css(component: &Component) -> CssStyle {
    let mut css = component_style(component).clone();
    apply_default_display(component, &mut css);
    apply_intrinsic_overrides(component, &mut css);
    css
}

/// Set `display` from the component kind when the user didn't specify one.
/// `card` / `flex` → `flex`, `grid` → `grid`. The taffy bridge defaults to
/// `block` otherwise, which would silently ignore `flex-direction` & friends.
fn apply_default_display(component: &Component, css: &mut CssStyle) {
    use rustmotion_core::css::style::Display;
    if css.display.is_some() {
        return;
    }
    css.display = match component {
        Component::Card(_) | Component::Flex(_) | Component::Container(_) => Some(Display::Flex),
        Component::Grid(_) => Some(Display::Grid),
        _ => return,
    };
}

/// Measure a single line of text with the exact same Skia font metrics the
/// affected painters (`callout`, `tooltip`, `pill_nav`, `stepper`) already use
/// to draw it (`measure_text_with_fallback`), so a size computed here matches
/// the pixels those painters actually paint instead of guessing at an average
/// character width. Returns `0.0` if the font family can't be resolved —
/// matches those painters' own silent-return-on-font-load-failure behaviour.
fn measure_text_line_width(text: &str, font_size: f32, family: &str, bold: bool) -> f32 {
    use rustmotion_core::engine::renderer::{
        emoji_typeface, measure_text_with_fallback, typeface_with_fallback,
    };
    let style = if bold {
        skia_safe::FontStyle::bold()
    } else {
        skia_safe::FontStyle::normal()
    };
    let Ok(typeface) = typeface_with_fallback(family, style) else {
        return 0.0;
    };
    let font = skia_safe::Font::from_typeface(typeface, font_size);
    let emoji_font = emoji_typeface().map(|tf| skia_safe::Font::from_typeface(tf, font_size));
    measure_text_with_fallback(text, &font, &emoji_font, 0.0)
}

/// Apply per-component CSS overrides for things that the legacy
/// `Widget::measure` derived from constraints (e.g. divider stretching to its
/// parent, line bounding box from its endpoints).
fn apply_intrinsic_overrides(component: &Component, css: &mut CssStyle) {
    use Component::*;
    match component {
        Text(t) => {
            // M1: `white-space: nowrap|pre` must style the *content*, not
            // silently resize the *box*. Without this, CSS's "automatic
            // minimum size" (min-width: auto + the default overflow:
            // visible) lets a nowrap text's own auto-width box grow to its
            // full natural width inside a flex container — which can push
            // or resize flex siblings the author never touched, a
            // surprising failure mode for a tool whose whole model is
            // authors declaring explicit positions/sizes. Forcing
            // `min-width: 0` (only when the author hasn't set their own)
            // keeps the box within whatever space its container gives it;
            // the painter (`text.rs`) still draws the full unwrapped line
            // regardless of that box width, so the text visibly bleeds past
            // it exactly as `white-space: nowrap` should — it's the box
            // that stays put, not the content.
            let nowrap = matches!(
                t.style.white_space,
                Some(
                    rustmotion_core::css::style::WhiteSpace::Nowrap
                        | rustmotion_core::css::style::WhiteSpace::Pre
                )
            );
            if nowrap && css.min_width.is_none() {
                css.min_width = Some(CSize::Length(CLP::Px(0.0)));
            }
        }
        // M1 follow-up: same reasoning as the `Text` arm above — now that
        // `gradient_text.rs` and `caption.rs` word-wrap and respect
        // `white-space: nowrap|pre` too (see `intrinsic.rs`'s
        // `GradientTextIntrinsic`/`CaptionIntrinsic`), their auto-width
        // boxes can hit the same CSS automatic-minimum-size growth when
        // nowrap/pre is set with no explicit width.
        GradientText(t) => {
            let nowrap = matches!(
                t.style.white_space,
                Some(
                    rustmotion_core::css::style::WhiteSpace::Nowrap
                        | rustmotion_core::css::style::WhiteSpace::Pre
                )
            );
            if nowrap && css.min_width.is_none() {
                css.min_width = Some(CSize::Length(CLP::Px(0.0)));
            }
        }
        Caption(c) => {
            let nowrap = matches!(
                c.style.white_space,
                Some(
                    rustmotion_core::css::style::WhiteSpace::Nowrap
                        | rustmotion_core::css::style::WhiteSpace::Pre
                )
            );
            if nowrap && css.min_width.is_none() {
                css.min_width = Some(CSize::Length(CLP::Px(0.0)));
            }
        }
        Divider(d) => match d.direction {
            DividerDirection::Horizontal => {
                // Stretch horizontally in flex row/column parents (cross-axis
                // for column = horizontal). Width stays auto.
                if css.height.is_none() {
                    css.height = Some(CSize::Length(CLP::Px(d.thickness)));
                }
                if css.width.is_none() {
                    css.width = match d.length {
                        Some(l) => Some(CSize::Length(CLP::Px(l))),
                        None => Some(CSize::Length(CLP::String("100%".into()))),
                    };
                }
                if css.align_self.is_none() {
                    css.align_self = Some(AlignSelf::Stretch);
                }
            }
            DividerDirection::Vertical => {
                if css.width.is_none() {
                    css.width = Some(CSize::Length(CLP::Px(d.thickness)));
                }
                if css.height.is_none() {
                    css.height = match d.length {
                        Some(l) => Some(CSize::Length(CLP::Px(l))),
                        None => Some(CSize::Length(CLP::String("100%".into()))),
                    };
                }
            }
        },
        Line(l) => {
            // Line draws inside its bounding box at (x1,y1)→(x2,y2). Use the
            // bounding box as the intrinsic size so taffy reserves enough room.
            let w = (l.x2 - l.x1).abs().max(1.0);
            let h = (l.y2 - l.y1).abs().max(1.0);
            if css.width.is_none() {
                css.width = Some(CSize::Length(CLP::Px(w)));
            }
            if css.height.is_none() {
                css.height = Some(CSize::Length(CLP::Px(h)));
            }
        }
        Arrow(a) => {
            // Endpoint bounding box + padding for the arrowhead/curve overshoot.
            let pad = ARROW_BBOX_PADDING + a.arrow_size.max(0.0);
            let w = (a.x2 - a.x1).abs().max(1.0) + pad;
            let h = (a.y2 - a.y1).abs().max(1.0) + pad;
            if css.width.is_none() {
                css.width = Some(CSize::Length(CLP::Px(w)));
            }
            if css.height.is_none() {
                css.height = Some(CSize::Length(CLP::Px(h)));
            }
        }
        Connector(c) => {
            let pad = ARROW_BBOX_PADDING + c.arrow_size.max(0.0);
            let w = (c.to.x - c.from.x).abs().max(1.0) + pad;
            let h = (c.to.y - c.from.y).abs().max(1.0) + pad;
            if css.width.is_none() {
                css.width = Some(CSize::Length(CLP::Px(w)));
            }
            if css.height.is_none() {
                css.height = Some(CSize::Length(CLP::Px(h)));
            }
        }
        Cursor(cur) => {
            // Fixed-size pointer; legacy measure returns (width, height).
            if css.width.is_none() {
                css.width = Some(CSize::Length(CLP::Px(cur.width)));
            }
            if css.height.is_none() {
                css.height = Some(CSize::Length(CLP::Px(cur.height)));
            }
        }
        SuccessCheck(c) => {
            // The halo is the box: the entrance scales *within* it (0.72→1),
            // so the mark never needs more room than its own diameter.
            apply_default_size(css, c.size, c.size);
        }
        Pointer(p) => {
            // The box is the arrow glyph, not the area it travels over: the
            // waypoints translate the glyph away from this box, and the
            // geometry checker exempts `pointer` for exactly that reason.
            // Sizing the box to the travel instead would make the pointer
            // shove its flex siblings around.
            if css.width.is_none() {
                css.width = Some(CSize::Length(CLP::Px(p.size * 0.6)));
            }
            if css.height.is_none() {
                css.height = Some(CSize::Length(CLP::Px(p.size)));
            }
        }
        Particle(_) => {
            // Particles fill their parent (legacy returned the max constraints).
            if css.width.is_none() {
                css.width = Some(CSize::Length(CLP::String("100%".into())));
            }
            if css.height.is_none() {
                css.height = Some(CSize::Length(CLP::String("100%".into())));
            }
        }
        Switch(c) => {
            if css.width.is_none() {
                css.width = Some(CSize::Length(CLP::Px(c.width)));
            }
            if css.height.is_none() {
                css.height = Some(CSize::Length(CLP::Px(c.height)));
            }
        }
        Slider(c) => {
            if css.width.is_none() {
                css.width = Some(CSize::Length(CLP::Px(c.width)));
            }
            if css.height.is_none() {
                css.height = Some(CSize::Length(CLP::Px(c.height)));
            }
        }
        Progress(c) => {
            if css.width.is_none() {
                css.width = Some(CSize::Length(CLP::Px(c.width)));
            }
            if css.height.is_none() {
                css.height = Some(CSize::Length(CLP::Px(c.height)));
            }
        }
        List(c) => {
            if css.width.is_none() {
                css.width = Some(CSize::Length(CLP::Px(c.width)));
            }
            if css.height.is_none() {
                let font_size = c.style.font_size_px_or(16.0);
                let line_height = font_size * 1.3;
                let n = c.items.len() as f32;
                let h = n * line_height + (n - 1.0).max(0.0) * c.gap;
                css.height = Some(CSize::Length(CLP::Px(h)));
            }
        }
        Timeline(c) => {
            if css.width.is_none() {
                css.width = Some(CSize::Length(CLP::Px(c.width)));
            }
            if css.height.is_none() {
                let r = c.node_radius;
                let h = match c.direction {
                    TimelineDirection::Horizontal => r * 2.0 + c.font_size * 2.5 + 24.0,
                    TimelineDirection::Vertical => {
                        let n = c.steps.len().max(1) as f32;
                        n * (r * 2.0 + 64.0)
                    }
                };
                css.height = Some(CSize::Length(CLP::Px(h)));
            }
        }
        Notification(c) => {
            if css.width.is_none() {
                css.width = Some(CSize::Length(CLP::Px(c.width)));
            }
            if css.height.is_none() {
                let h = if c.message.is_some() { 96.0 } else { 64.0 };
                css.height = Some(CSize::Length(CLP::Px(h)));
            }
        }
        Rating(c) => {
            if css.width.is_none() {
                let count = c.max as f32;
                let w = count * c.size + (count - 1.0).max(0.0) * c.gap;
                css.width = Some(CSize::Length(CLP::Px(w)));
            }
            if css.height.is_none() {
                css.height = Some(CSize::Length(CLP::Px(c.size)));
            }
        }
        Avatar(c) => {
            if css.width.is_none() {
                css.width = Some(CSize::Length(CLP::Px(c.size)));
            }
            if css.height.is_none() {
                css.height = Some(CSize::Length(CLP::Px(c.size)));
            }
        }
        AvatarGroup(c) => {
            if css.width.is_none() {
                let visible = c.visible_count() as f32;
                let extra = if c.overflow_count() > 0 { 1.0 } else { 0.0 };
                let total = visible + extra;
                let step = (c.size - c.overlap).max(0.0);
                let w = if total <= 0.0 {
                    0.0
                } else {
                    c.size + (total - 1.0) * step
                };
                css.width = Some(CSize::Length(CLP::Px(w)));
            }
            if css.height.is_none() {
                css.height = Some(CSize::Length(CLP::Px(c.size)));
            }
        }
        QrCode(c) => {
            if css.width.is_none() {
                css.width = Some(CSize::Length(CLP::Px(c.size)));
            }
            if css.height.is_none() {
                css.height = Some(CSize::Length(CLP::Px(c.size)));
            }
        }
        Countdown(c) if (css.width.is_none() || css.height.is_none()) => {
            let visible = [c.show_hours, c.show_minutes, c.show_seconds]
                .iter()
                .filter(|v| **v)
                .count() as f32;
            let box_w = c.digit_size * 0.75;
            let box_h = c.digit_size * 1.2;
            let w = (visible * 2.0 * box_w) + ((visible - 1.0).max(0.0) * c.gap);
            if css.width.is_none() {
                css.width = Some(CSize::Length(CLP::Px(w)));
            }
            if css.height.is_none() {
                css.height = Some(CSize::Length(CLP::Px(box_h)));
            }
        }
        AudioSpectrum(_) => {
            if css.width.is_none() {
                css.width = Some(CSize::Length(CLP::Px(400.0)));
            }
            if css.height.is_none() {
                css.height = Some(CSize::Length(CLP::Px(120.0)));
            }
        }
        Waveform(_) => {
            if css.width.is_none() {
                css.width = Some(CSize::Length(CLP::Px(400.0)));
            }
            if css.height.is_none() {
                css.height = Some(CSize::Length(CLP::Px(80.0)));
            }
        }

        // ── Round 4 audit, lot LAYOUT, constat 4: the 23-components block
        // below (`Callout` through `Lottie`) now routes every default size
        // through `apply_default_size`, which honours an explicit
        // `aspect-ratio` (see its own doc comment) instead of the two
        // guards below reaching separate, aspect-ratio-blind defaults —
        // `width: 400` + `aspect-ratio: 16/9` used to still get the
        // component's unrelated hardcoded default height (e.g. `shape`'s
        // 80px) instead of the 225px the ratio implies.
        // ── #126 / W3: the 23 components with no size source ─────────────
        //
        // A card's default flex column gives every child its width via
        // `align-items: stretch`, but height stays at the CSS auto-height
        // default (0 for a leaf with no intrinsic measurer) — these 23 paint
        // nothing. Like the arms above, every default here fires only when
        // the author hasn't set the corresponding `style` property, so a
        // component that already declares `width`/`height` keeps rendering
        // exactly as before. Both width *and* height are always set (not
        // just height) so the same defaults also work in a flex *row* (e.g.
        // `stat` cards side by side), where width — not height — is the one
        // that would otherwise collapse to 0.
        //
        // Text-bearing "bubble" components (callout/tooltip) and label-flow
        // components (pill_nav/stepper) are measured with the exact same
        // Skia metrics their own painters use (`measure_text_line_width`),
        // so the box matches the ink. Everything else uses either a formula
        // derived from the component's own fields (heatmap/skeleton/gauge/
        // marquee) or a fixed size justified by a documented convention
        // already established in this project's own skill docs
        // (`.claude/skills/rustmotion/rules/*.md`) or its own example
        // scenarios (`examples/*.json`) — never a number picked by feel.
        Callout(t) => {
            // Mirrors callout.rs's own `paint()`: 12px text padding, a
            // `font_size * 1.4` line height, and the arrow eating into
            // whichever axis it points along (width for Left/Right, height
            // for Top/Bottom/default). Sized to a single line — since the
            // box is fit exactly to the unwrapped text width, the painter's
            // own `wrap_text(text, font, Some(text_area_w))` never has a
            // reason to wrap, so painted output matches this box exactly.
            let font_size = t.style.font_size_px_or(16.0);
            let family = t.style.font_family_or("Inter");
            let text_w = measure_text_line_width(&t.text, font_size, family, false);
            let h_pad = 12.0; // callout.rs's own `let padding = 12.0;`
            let v_pad = 16.0; // breathing room around the line, same order of magnitude as h_pad
            let line_h = font_size * 1.4; // callout.rs's own `line_height = font_size * 1.4`
            let (extra_w, extra_h) = match t.arrow_direction {
                CalloutArrowDirection::Left | CalloutArrowDirection::Right => (t.arrow_size, 0.0),
                CalloutArrowDirection::Top | CalloutArrowDirection::Bottom => (0.0, t.arrow_size),
            };
            apply_default_size(
                css,
                text_w + h_pad * 2.0 + extra_w,
                line_h + v_pad + extra_h,
            );
        }
        Tooltip(t) => {
            // Same shape as Callout above; padding value borrowed from
            // callout.rs since tooltip.rs's own paint() centers text in the
            // body with no defined constant of its own.
            let font_size = t.style.font_size_px_or(t.font_size);
            let family = t.style.font_family_or("Inter");
            let text_w = measure_text_line_width(&t.text, font_size, family, false);
            let h_pad = 12.0;
            let v_pad = 16.0;
            let line_h = font_size * 1.4;
            let (extra_w, extra_h) = match t.arrow {
                TooltipArrow::Left | TooltipArrow::Right => (t.arrow_size, 0.0),
                TooltipArrow::Top | TooltipArrow::Bottom | TooltipArrow::None => {
                    (0.0, t.arrow_size)
                }
            };
            apply_default_size(
                css,
                text_w + h_pad * 2.0 + extra_w,
                line_h + v_pad + extra_h,
            );
        }
        PillNav(p) => {
            // `height` is already a declared field on the component (like
            // Progress/Switch/Slider above) — just promote it to CSS. Width
            // replicates pill_nav.rs's own private `compute_tab_layout()`
            // formula (h_pad = font_size*1.2 per side, `gap` before/after/
            // between every pill) using the same public fields and the same
            // `measure_text_with_fallback` call it makes internally.
            let font_size = p.style.font_size_px_or(14.0);
            let family = p.style.font_family_or("Inter");
            let h_pad = font_size * 1.2;
            let n = p.items.len() as f32;
            let labels_w: f32 = p
                .items
                .iter()
                .map(|label| measure_text_line_width(label, font_size, family, false) + h_pad * 2.0)
                .sum();
            let total_w = labels_w + p.gap * (n + 1.0).max(1.0);
            apply_default_size(css, total_w, p.height);
        }
        Marquee(m) => {
            // Marquee's whole purpose is to scroll unbounded content, so
            // there's no natural content width. A percentage (mirroring
            // `Particle`'s "fills its parent" default above) would be the
            // obvious choice, but it resolves to 0 against an indefinite
            // parent (a card that itself has no explicit width) — the same
            // "card with height: auto" case this issue asks to fix, so a
            // percentage default would still paint nothing in that case. A
            // fixed width sidesteps that: 800px matches this project's own
            // marquee usage (examples/mega-showcase.json and SKILL.md's own
            // example both use `style.width: 800`, or scale up from there —
            // mega-showcase's 1700px is that same scene's marquee spanning a
            // much wider bleed banner). Height follows the font-size-to-
            // height ratio both of those same real usages share:
            // `font_size: 24` paired with `style.height: 48`, i.e.
            // `2 × font_size`.
            let font_size = m.style.font_size_px_or(m.font_size);
            apply_default_size(css, 800.0, font_size * 2.0);
        }
        Stepper(s) => {
            // Same shape as `Timeline`'s formula above (r*2 + label metrics),
            // adapted to stepper.rs's own layout constants: `cy = r + 4.0`,
            // label offset `r + 12.0`, description offset
            // `label_font_size + 4.0`, and its hardcoded label/description
            // font sizes (14px / 11px — not fields, copied from paint()).
            let n = (s.steps.len().max(1)) as f32;
            let has_desc = s.steps.iter().any(|st| st.description.is_some());
            const LABEL_FS: f32 = 14.0;
            const DESC_FS: f32 = 11.0;
            let max_label_w = s
                .steps
                .iter()
                .map(|st| measure_text_line_width(&st.label, LABEL_FS, "Inter", false))
                .fold(0.0_f32, f32::max);
            let max_desc_w = s
                .steps
                .iter()
                .filter_map(|st| st.description.as_deref())
                .map(|d| measure_text_line_width(d, DESC_FS, "Inter", false))
                .fold(0.0_f32, f32::max);
            match s.orientation {
                StepperOrientation::Horizontal => {
                    // Per-step allocation: the node needs ~3 diameters of
                    // breathing room (a common stepper-UI spacing
                    // convention), or enough for its longest label/desc,
                    // whichever is larger.
                    let per_step = (s.node_size * 3.0).max(max_label_w.max(max_desc_w) + 24.0);
                    let label_h = LABEL_FS * 1.3;
                    let desc_h = if has_desc { DESC_FS * 1.3 + 4.0 } else { 0.0 };
                    let h = s.node_size + 4.0 + 12.0 + label_h + desc_h;
                    apply_default_size(css, per_step * n, h);
                }
                StepperOrientation::Vertical => {
                    let label_w = max_label_w.max(max_desc_w);
                    let w = s.node_size + 12.0 + label_w + 24.0;
                    let label_block = if has_desc {
                        LABEL_FS * 1.3 + DESC_FS * 1.3 + 8.0
                    } else {
                        LABEL_FS * 1.3 + 8.0
                    };
                    let per_step = (s.node_size * 2.0).max(label_block);
                    apply_default_size(css, w, per_step * n);
                }
            }
        }
        TagCloud(tc) => {
            // Replicates tag_cloud.rs's own per-tag metrics (bold Inter,
            // weight-normalized font size between min/max_font_size, its
            // hardcoded h_gap=12/v_gap=8) to sum a single-line content width,
            // then wraps that at a conventional card-content width (matching
            // this project's own tag_cloud usage in
            // examples/mega-showcase.json) to estimate a line count and
            // hence a height — an approximation of the real flow-wrap
            // algorithm, not a re-implementation of it.
            let n = tc.tags.len();
            if n > 0 {
                let min_w = tc.tags.iter().map(|t| t.weight).fold(f64::MAX, f64::min);
                let max_w = tc.tags.iter().map(|t| t.weight).fold(f64::MIN, f64::max);
                let range = (max_w - min_w).max(0.001);
                const H_GAP: f32 = 12.0;
                const V_GAP: f32 = 8.0;
                let total_w: f32 = tc
                    .tags
                    .iter()
                    .map(|t| {
                        let normalized = ((t.weight - min_w) / range) as f32;
                        let fs =
                            tc.min_font_size + normalized * (tc.max_font_size - tc.min_font_size);
                        measure_text_line_width(&t.text, fs, "Inter", true) + H_GAP
                    })
                    .sum();
                const CAP_W: f32 = 600.0;
                let box_w = total_w.clamp(CAP_W * 0.3, CAP_W);
                let lines = (total_w / box_w).ceil().max(1.0);
                let line_h = tc.max_font_size * 1.3;
                let box_h = lines * line_h + (lines - 1.0).max(0.0) * V_GAP;
                apply_default_size(css, box_w, box_h);
            }
        }
        Heatmap(h) => {
            // Fully content-derived from heatmap.rs's own paint() formula:
            // `step = cell_size + cell_gap`, cell (col,row) drawn at
            // `(col*step, row*step)` sized `cell_size` — so the painted
            // extent is exactly `(cols-1)*step + cell_size` per axis.
            let rows = h.data.len();
            let cols = h.data.iter().map(|r| r.len()).max().unwrap_or(0);
            let step = h.cell_size + h.cell_gap;
            let w = (cols.max(1) as f32 - 1.0).max(0.0) * step + h.cell_size;
            let hh = (rows.max(1) as f32 - 1.0).max(0.0) * step + h.cell_size;
            apply_default_size(css, w, hh);
        }
        Sparkline(_) => {
            // "Sparkline: no axes, no labels, compact (120x40 default),
            // inline use" — documented in
            // .claude/skills/rustmotion/rules/data-viz-components.md.
            apply_default_size(css, 120.0, 40.0);
        }
        Stat(_) => {
            // Documented default from
            // .claude/skills/rustmotion/rules/stat-cards.md's own "GOOD"
            // example: `style: { width: 280, height: 180 }`. This is also
            // the fix for the issue's second bug: three `stat`s in a flex
            // row with no explicit size rendered zero pixels because width
            // (not just height) collapsed to 0 in a row context.
            apply_default_size(css, 280.0, 180.0);
        }
        Gauge(g) => {
            // Square — gauge.rs's own paint() derives its ring radius from
            // `min(w, h)/2 - track_width/2 - 4`. Solved backwards for a
            // target radius of 88px (chosen so the value-text font-size —
            // this same file's own `radius * 0.45` — comes out to ~40px,
            // comfortably legible), so the box scales with the component's
            // own `track_width` field rather than a size picked independent
            // of it. At the default `track_width` (16px) this lands on
            // exactly 200×200, which also matches the midpoint of the
            // documented "hero icon" desktop range (160–200px) in
            // icon-sizing-hierarchy.md.
            const TARGET_RADIUS: f32 = 88.0;
            let size = 2.0 * (TARGET_RADIUS + g.track_width / 2.0 + 4.0);
            apply_default_size(css, size, size);
        }
        DotMap(_) => {
            // 2:1 — the standard aspect ratio for an equirectangular world
            // map (360° longitude : 180° latitude), the projection
            // dot_map.rs's own `geo_to_screen` implements. dot_map.rs always
            // paints a full-box background rect first, so any positive size
            // shows ink even with zero points.
            apply_default_size(css, 640.0, 320.0);
        }
        Comparison(_) => {
            // No natural intrinsic size (the painter just splits whatever
            // box it's given at the divider) — matches this project's own
            // reference usage in examples/mega-showcase.json's `comparison`
            // block.
            apply_default_size(css, 520.0, 280.0);
        }
        Treemap(_) => {
            // Slice-and-dice treemap fills whatever box it's given — matches
            // this project's own reference usage in
            // examples/mega-showcase.json's `treemap` block (near-square,
            // the conventional treemap aspect since its rectangles are area-
            // proportional in both axes).
            apply_default_size(css, 416.0, 368.0);
        }
        Chart(c) => {
            // Pie/donut/radar/radial_bar are inherently circular — a square
            // box avoids wasting space on one axis or clipping into an
            // ellipse. The other 8 chart types (bar/line/area/scatter/
            // funnel/waterfall/stacked_bar/horizontal_bar) read axis labels
            // best in a landscape 4:3, per data-viz-components.md's guidance
            // that charts are "larger, standalone" than a sparkline.
            let round = matches!(
                c.chart_type,
                ChartType::Pie | ChartType::Donut | ChartType::Radar | ChartType::RadialBar
            );
            let (dw, dh) = if round {
                (320.0, 320.0)
            } else {
                (400.0, 300.0)
            };
            apply_default_size(css, dw, dh);
        }
        Skeleton(s) => {
            // `rectangle`: documented default from data-viz-components.md's
            // own "GOOD" example (`{ "width": 400, "height": 200 }`).
            // `circle`: matches this file's own Icon/Avatar-adjacent 64px
            // convention (skeleton circles most commonly stand in for an
            // avatar). `text`: fully derived from the component's own
            // `lines`/`line_height`/`line_gap` fields, mirroring `List`'s
            // formula above — matches skeleton.rs's own per-line paint loop
            // (`y = i * (line_height + line_gap)`) exactly.
            match s.variant {
                SkeletonVariant::Rectangle => apply_default_size(css, 400.0, 200.0),
                SkeletonVariant::Circle => apply_default_size(css, 64.0, 64.0),
                SkeletonVariant::Text => {
                    let n = s.lines.max(1) as f32;
                    let h = n * s.line_height + (n - 1.0).max(0.0) * s.line_gap;
                    apply_default_size(css, 240.0, h);
                }
            }
        }
        Mockup(m) => {
            // Per-device aspect matches each device's real-world screen
            // proportions: phones (iPhone/Android) ~9:19.5 (modern
            // flagship aspect), laptop 16:10 (the common MacBook/ultrabook
            // ratio), browser 16:9 (the standard desktop viewport ratio).
            let (dw, dh) = match m.device {
                MockupDevice::Iphone | MockupDevice::Android => (320.0, 690.0),
                MockupDevice::Laptop => (640.0, 400.0),
                MockupDevice::Browser => (640.0, 360.0),
            };
            apply_default_size(css, dw, dh);
        }
        Icon(_) => {
            // 64×64 — the midpoint of the documented "card / feature icon"
            // role across all three device classes in
            // icon-sizing-hierarchy.md (desktop 40–56px, mobile 72–96px,
            // square 60–80px), and a size icon asset systems near-universally
            // ship as a default export (24/32/48/64 being the common family).
            apply_default_size(css, 64.0, 64.0);
        }
        Svg(_) => {
            // 200×200 — square, since an arbitrary vector graphic (icon,
            // diagram, or logo) has no single natural aspect; matches the
            // common equal-aspect SVG viewBox convention and sits above
            // Icon's 64px "card icon" role for the more elaborate content
            // `svg` typically carries (illustrations/diagrams, not glyphs).
            apply_default_size(css, 200.0, 200.0);
        }
        Shape(_) => {
            // 80×80 — matches the median of this project's own decorative
            // (non full-bleed-background, non-divider-line) shape usages in
            // examples/*.json, which cluster at 44–70px for accent shapes
            // (26, 36, 44, 60, 70, 140 — median ~55, rounded up for
            // visibility as a standalone default rather than a same-scene
            // accent tuned against neighbours).
            apply_default_size(css, 80.0, 80.0);
        }
        Image(_) => {
            // 4:3 (400×300) — the traditional default photo aspect ratio,
            // distinct from Video/Gif's 16:9 below so a generic still image
            // doesn't presume widescreen framing.
            apply_default_size(css, 400.0, 300.0);
        }
        Video(_) | Gif(_) => {
            // 16:9 (400×225) — the industry-standard video aspect ratio
            // (matches every render resolution this project documents:
            // 1920×1080, 1280×720), scaled down to a card-sized default.
            apply_default_size(css, 400.0, 225.0);
        }
        Lottie(_) => {
            // 300×300 — square, matching the aspect the vast majority of
            // Lottie animation assets ship at (LottieFiles' own marketplace
            // preview convention is a 1:1 canvas).
            apply_default_size(css, 300.0, 300.0);
        }

        _ => {}
    }
}

/// Apply a component's natural default size (`dw` × `dh`) to `css`, honouring
/// an explicit `aspect-ratio` instead of always falling back to `dw`/`dh`
/// independently (round 4 audit, lot LAYOUT, constat 4 — the previous code
/// guarded each axis with its own `is_none()` check and never looked at
/// `aspect-ratio`, so `width: 400` + `aspect-ratio: 16/9` still got the
/// component's unrelated hardcoded default height instead of 225).
///
/// - Both axes already set: untouched (the author fully specified the box).
/// - One axis set to a fixed pixel length, `aspect-ratio` present: the other
///   axis is derived from it (`h = w / ratio` or `w = h * ratio`) — the CSS
///   replaced-element sizing rule for a single definite axis plus a
///   preferred aspect ratio.
/// - Neither axis set: the natural default width is kept (there is no
///   author-declared axis to derive from), and height is derived from
///   `aspect-ratio` when present, the natural default height otherwise.
///
/// `min-*`/`max-*` need no equivalent guard here: taffy clamps the final
/// used size against them at layout time regardless of what `size` resolves
/// to (`style.min_size`/`max_size` in `taffy_bridge::to_taffy_style`), so a
/// default below `min-width` is corrected downstream, not silently wrong.
fn apply_default_size(css: &mut CssStyle, dw: f32, dh: f32) {
    let ratio = css.aspect_ratio.filter(|r| *r > 0.0);
    match (css.width.is_some(), css.height.is_some()) {
        (true, true) => {}
        (true, false) => {
            let h = fixed_px(css.width.as_ref())
                .zip(ratio)
                .map(|(w, r)| w / r)
                .unwrap_or(dh);
            css.height = Some(CSize::Length(CLP::Px(h)));
        }
        (false, true) => {
            let w = fixed_px(css.height.as_ref())
                .zip(ratio)
                .map(|(h, r)| h * r)
                .unwrap_or(dw);
            css.width = Some(CSize::Length(CLP::Px(w)));
        }
        (false, false) => {
            css.width = Some(CSize::Length(CLP::Px(dw)));
            let h = ratio.map(|r| dw / r).unwrap_or(dh);
            css.height = Some(CSize::Length(CLP::Px(h)));
        }
    }
}

/// Extract a fixed pixel value from a `Size`, if it resolves to one without a
/// `LengthContext` (only `Size::Length(LengthPercentage::Px(_))` — a bare
/// number or `"NNpx"`). `%`/`vw`/`vh`/`em`/`rem` and `auto` return `None`:
/// `apply_default_size` can't derive a ratio from a length it can't resolve
/// at build time, so it falls back to the component's hardcoded default.
fn fixed_px(size: Option<&CSize>) -> Option<f32> {
    match size? {
        CSize::Length(lp) => match lp.try_parse()? {
            rustmotion_core::css::units::ParsedLength::Px(v) => Some(v),
            _ => None,
        },
        _ => None,
    }
}

/// Borrow the `CssStyle` from any component.
fn component_style(c: &Component) -> &CssStyle {
    use Component::*;
    match c {
        Text(c) => &c.style,
        Shape(c) => &c.style,
        Image(c) => &c.style,
        Icon(c) => &c.style,
        Svg(c) => &c.style,
        Video(c) => &c.style,
        Gif(c) => &c.style,
        Counter(c) => &c.style,
        Cursor(c) => &c.style,
        Caption(c) => &c.style,
        Codeblock(c) => &c.style,
        Connector(c) => &c.style,
        Avatar(c) => &c.style,
        AvatarGroup(c) => &c.style,
        Arrow(c) => &c.style,
        Badge(c) => &c.style,
        Callout(c) => &c.style,
        Chart(c) => &c.style,
        Comparison(c) => &c.style,
        Countdown(c) => &c.style,
        Divider(c) => &c.style,
        DotMap(c) => &c.style,
        Gauge(c) => &c.style,
        GradientText(c) => &c.style,
        Heatmap(c) => &c.style,
        Kbd(c) => &c.style,
        Line(c) => &c.style,
        List(c) => &c.style,
        Lottie(c) => &c.style,
        Marquee(c) => &c.style,
        Mockup(c) => &c.style,
        Notification(c) => &c.style,
        Particle(c) => &c.style,
        PillNav(c) => &c.style,
        Progress(c) => &c.style,
        QrCode(c) => &c.style,
        NumberWheel(c) => &c.style,
        SuccessCheck(c) => &c.style,
        Pointer(c) => &c.style,
        Rating(c) => &c.style,
        Skeleton(c) => &c.style,
        Slider(c) => &c.style,
        Sparkline(c) => &c.style,
        Stat(c) => &c.style,
        Stepper(c) => &c.style,
        Switch(c) => &c.style,
        RichText(c) => &c.style,
        Table(c) => &c.style,
        TagCloud(c) => &c.style,
        Terminal(c) => &c.style,
        Timeline(c) => &c.style,
        Tooltip(c) => &c.style,
        Treemap(c) => &c.style,
        Positioned(c) => &c.style,
        Flex(c) => &c.style,
        Grid(c) => &c.style,
        Card(c) => &c.style,
        Container(c) => &c.style,
        AudioSpectrum(c) => &c.style,
        Waveform(c) => &c.style,
    }
}

/// Short kind label for a component (for studio selection / inspector display).
pub fn component_kind(c: &Component) -> &'static str {
    use Component::*;
    match c {
        Text(_) => "text",
        Shape(_) => "shape",
        Image(_) => "image",
        Icon(_) => "icon",
        Svg(_) => "svg",
        Video(_) => "video",
        Gif(_) => "gif",
        Counter(_) => "counter",
        Cursor(_) => "cursor",
        Caption(_) => "caption",
        Codeblock(_) => "codeblock",
        Connector(_) => "connector",
        Avatar(_) => "avatar",
        AvatarGroup(_) => "avatar_group",
        Arrow(_) => "arrow",
        Badge(_) => "badge",
        Callout(_) => "callout",
        Chart(_) => "chart",
        Comparison(_) => "comparison",
        Countdown(_) => "countdown",
        Divider(_) => "divider",
        DotMap(_) => "dot_map",
        Gauge(_) => "gauge",
        GradientText(_) => "gradient_text",
        Heatmap(_) => "heatmap",
        Kbd(_) => "kbd",
        Line(_) => "line",
        List(_) => "list",
        Lottie(_) => "lottie",
        Marquee(_) => "marquee",
        Mockup(_) => "mockup",
        Notification(_) => "notification",
        Particle(_) => "particle",
        PillNav(_) => "pill_nav",
        Progress(_) => "progress",
        QrCode(_) => "qrcode",
        NumberWheel(_) => "number_wheel",
        SuccessCheck(_) => "success_check",
        Pointer(_) => "pointer",
        Rating(_) => "rating",
        Skeleton(_) => "skeleton",
        Slider(_) => "slider",
        Sparkline(_) => "sparkline",
        Stat(_) => "stat",
        Stepper(_) => "stepper",
        Switch(_) => "switch",
        RichText(_) => "rich_text",
        Table(_) => "table",
        TagCloud(_) => "tag_cloud",
        Terminal(_) => "terminal",
        Timeline(_) => "timeline",
        Tooltip(_) => "tooltip",
        Treemap(_) => "treemap",
        Positioned(_) => "positioned",
        Flex(_) => "flex",
        Grid(_) => "grid",
        Card(_) => "card",
        Container(_) => "container",
        AudioSpectrum(_) => "audio_spectrum",
        Waveform(_) => "waveform",
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use rustmotion_core::css::style::{
        CssStyle, Display, Edges, FlexDirection, Gap, Size as CSize,
    };
    use rustmotion_core::css::taffy_bridge::ConversionContext;
    use rustmotion_core::css::units::LengthPercentage;
    use rustmotion_core::css::units::LengthPercentage as CLP;
    use rustmotion_core::engine::layout_pass::run_layout;
    use serde_json::json;

    fn make_card(children: Vec<ChildComponent>, style: CssStyle) -> Component {
        Component::Card(crate::card::Card {
            children,
            timing: Default::default(),
            style,
            timeline: Vec::new(),
            stagger: None,
            time_scale: None,
            time_offset: None,
        })
    }

    fn make_shape(width: f32, height: f32) -> ChildComponent {
        ChildComponent {
            component: Component::Shape(crate::shape::Shape {
                shape: rustmotion_core::schema::ShapeType::Rect,
                text: None,
                timing: Default::default(),
                style: CssStyle {
                    width: Some(CSize::Length(CLP::Px(width))),
                    height: Some(CSize::Length(CLP::Px(height))),
                    ..Default::default()
                },
                timeline: Vec::new(),
                stagger: None,
                fill: None,
                stroke: None,
            }),
            position: None,
            x: None,
            y: None,
            z_index: None,
            bleed: false,
        }
    }

    fn make_text(content: &str, style: CssStyle) -> ChildComponent {
        ChildComponent {
            component: Component::Text(crate::text::Text {
                content: content.to_string(),
                max_width: None,
                timing: Default::default(),
                style,
                timeline: Vec::new(),
                stagger: None,
                text_shadow: None,
                stroke: None,
                text_background: None,
                caret: None,
                states: Vec::new(),
                swap: None,
            }),
            position: None,
            x: None,
            y: None,
            z_index: None,
            bleed: false,
        }
    }

    #[test]
    fn empty_scene_has_only_root() {
        let built = build_scene(&[], (1920.0, 1080.0));
        assert_eq!(built.root.children.len(), 0);
        assert_eq!(built.components.len(), 1); // synthetic root slot
    }

    #[test]
    fn component_kind_labels() {
        assert_eq!(component_kind(&make_shape(100.0, 50.0).component), "shape");
    }

    #[test]
    fn build_child_records_source_path() {
        let card = make_card(
            vec![make_shape(10.0, 10.0), make_shape(10.0, 10.0)],
            CssStyle::default(),
        );
        let scene = vec![ChildComponent {
            component: card,
            position: Some(crate::PositionMode::Absolute { x: 0.0, y: 0.0 }),
            x: None,
            y: None,
            z_index: None,
            bleed: false,
        }];
        let built = build_scene(&scene, (800.0, 600.0));
        let card_box = &built.root.children[0];
        assert_eq!(card_box.source_path.as_deref(), Some("/children/0"));
        assert_eq!(
            card_box.children[1].source_path.as_deref(),
            Some("/children/0/children/1")
        );
    }

    #[test]
    fn flex_card_with_two_shapes_lays_out_vertically() {
        let card = make_card(
            vec![make_shape(200.0, 50.0), make_shape(200.0, 50.0)],
            CssStyle {
                display: Some(Display::Flex),
                flex_direction: Some(FlexDirection::Column),
                gap: Some(Gap::Uniform(LengthPercentage::Px(10.0))),
                padding: Some(Edges::Uniform(LengthPercentage::Px(20.0))),
                width: Some(CSize::Length(CLP::Px(300.0))),
                height: Some(CSize::Length(CLP::Px(200.0))),
                ..Default::default()
            },
        );
        let scene = vec![ChildComponent {
            component: card,
            position: Some(crate::PositionMode::Absolute { x: 0.0, y: 0.0 }),
            x: None,
            y: None,
            z_index: None,
            bleed: false,
        }];
        let built = build_scene(&scene, (1920.0, 1080.0));
        assert_eq!(built.root.children.len(), 1);
        let card_box = &built.root.children[0];
        assert_eq!(card_box.css.display, Some(Display::Flex));
        assert_eq!(card_box.css.flex_direction, Some(FlexDirection::Column));
        assert_eq!(card_box.children.len(), 2);

        let layout = run_layout(&built.root, (1920.0, 1080.0), &ConversionContext::default());
        let card_layout = layout.get(card_box.id).expect("card laid out");
        assert_eq!(card_layout.x, 0.0);
        assert_eq!(card_layout.y, 0.0);
        assert_eq!(card_layout.width, 300.0);
        assert_eq!(card_layout.height, 200.0);

        let c1 = layout
            .get(card_box.children[0].id)
            .expect("shape 1 laid out");
        let c2 = layout
            .get(card_box.children[1].id)
            .expect("shape 2 laid out");
        // Padding 20 from top, then first shape 50 high, gap 10 → 80.
        assert_eq!(c1.x, 20.0);
        assert_eq!(c1.y, 20.0);
        assert_eq!(c2.x, 20.0);
        assert_eq!(c2.y, 80.0);
    }

    #[test]
    fn absolute_child_uses_top_left() {
        let scene = vec![ChildComponent {
            component: Component::Shape(crate::shape::Shape {
                shape: rustmotion_core::schema::ShapeType::Rect,
                text: None,
                timing: Default::default(),
                style: CssStyle {
                    width: Some(CSize::Length(CLP::Px(100.0))),
                    height: Some(CSize::Length(CLP::Px(80.0))),
                    ..Default::default()
                },
                timeline: Vec::new(),
                stagger: None,
                fill: None,
                stroke: None,
            }),
            position: Some(crate::PositionMode::Absolute { x: 40.0, y: 30.0 }),
            x: None,
            y: None,
            z_index: None,
            bleed: false,
        }];
        let built = build_scene(&scene, (400.0, 400.0));
        let layout = run_layout(&built.root, (400.0, 400.0), &ConversionContext::default());
        let shape_id = built.root.children[0].id;
        let l = layout.get(shape_id).expect("shape laid out");
        assert_eq!(l.x, 40.0);
        assert_eq!(l.y, 30.0);
        assert_eq!(l.width, 100.0);
        assert_eq!(l.height, 80.0);
    }

    #[test]
    fn horizontal_divider_stretches_to_parent_width() {
        let divider = ChildComponent {
            component: Component::Divider(crate::divider::Divider {
                direction: DividerDirection::Horizontal,
                thickness: 4.0,
                line_style: Default::default(),
                length: None,
                timing: Default::default(),
                style: CssStyle::default(),
                timeline: Vec::new(),
                stagger: None,
            }),
            position: None,
            x: None,
            y: None,
            z_index: None,
            bleed: false,
        };
        let scene = vec![divider];
        let built = build_scene(&scene, (800.0, 600.0));
        let layout = run_layout(&built.root, (800.0, 600.0), &ConversionContext::default());
        let id = built.root.children[0].id;
        let l = layout.get(id).expect("divider laid out");
        assert_eq!(l.height, 4.0);
        assert_eq!(l.width, 800.0);
    }

    #[test]
    fn text_child_in_flex_card_gets_cosmic_intrinsic_size() {
        // A flex column card with no fixed size — its children's intrinsic
        // sizes should determine the card's width/height. The text child
        // must be measured via cosmic-text, not collapse to 0×0.
        use crate::card::Card;
        use crate::text::Text;

        use rustmotion_core::css::units::Length;

        let text = ChildComponent {
            component: Component::Text(Text {
                content: "Hello World".into(),
                max_width: None,
                timing: Default::default(),
                style: CssStyle {
                    font_size: Some(Length::Px(40.0)),
                    ..Default::default()
                },
                timeline: Vec::new(),
                stagger: None,
                text_shadow: None,
                stroke: None,
                text_background: None,
                caret: None,
                states: Vec::new(),
                swap: None,
            }),
            position: None,
            x: None,
            y: None,
            z_index: None,
            bleed: false,
        };

        let card = ChildComponent {
            component: Component::Card(Card {
                children: vec![text],
                timing: Default::default(),
                style: CssStyle {
                    display: Some(Display::Flex),
                    flex_direction: Some(FlexDirection::Column),
                    padding: Some(Edges::Uniform(LengthPercentage::Px(20.0))),
                    ..Default::default()
                },
                timeline: Vec::new(),
                stagger: None,
                time_scale: None,
                time_offset: None,
            }),
            position: Some(crate::PositionMode::Absolute { x: 0.0, y: 0.0 }),
            x: None,
            y: None,
            z_index: None,
            bleed: false,
        };

        let scene = vec![card];
        let built = build_scene(&scene, (1920.0, 1080.0));
        let layout = run_layout(&built.root, (1920.0, 1080.0), &ConversionContext::default());

        let card_id = built.root.children[0].id;
        let text_id = built.root.children[0].children[0].id;
        let text_layout = layout.get(text_id).expect("text laid out");

        assert!(
            text_layout.width > 0.0,
            "text width should be > 0, got {}",
            text_layout.width
        );
        assert!(
            text_layout.height >= 40.0,
            "text height should be at least one line tall, got {}",
            text_layout.height
        );

        // Card height should hug the text + 2×padding(20) = ~text_h + 40.
        let card_layout = layout.get(card_id).expect("card laid out");
        assert!(
            card_layout.height >= text_layout.height + 40.0 - 1.0,
            "card height ({}) should fit text + padding ({}+40)",
            card_layout.height,
            text_layout.height,
        );
    }

    #[test]
    fn arrow_intrinsic_size_uses_endpoint_bbox_plus_arrowhead() {
        let arrow = ChildComponent {
            component: Component::Arrow(crate::arrow::Arrow {
                x1: 10.0,
                y1: 20.0,
                x2: 110.0,
                y2: 80.0,
                cp: None,
                cp1: None,
                cp2: None,
                curve: None,
                width: 4.0,
                color: "#fff".into(),
                arrow_end: true,
                arrow_start: false,
                arrow_size: 12.0,
                dashed: None,
                timing: Default::default(),
                style: CssStyle::default(),
                timeline: Vec::new(),
                stagger: None,
            }),
            position: Some(crate::PositionMode::Absolute { x: 0.0, y: 0.0 }),
            x: None,
            y: None,
            z_index: None,
            bleed: false,
        };
        let scene = vec![arrow];
        let built = build_scene(&scene, (800.0, 600.0));
        let layout = run_layout(&built.root, (800.0, 600.0), &ConversionContext::default());
        let l = layout
            .get(built.root.children[0].id)
            .expect("arrow laid out");
        // bbox 100×60 + (16 padding + 12 arrow_size) = 128×88.
        assert_eq!(l.width, 128.0);
        assert_eq!(l.height, 88.0);
    }

    #[test]
    fn connector_intrinsic_size_uses_endpoint_bbox_plus_arrowhead() {
        let conn = ChildComponent {
            component: Component::Connector(crate::connector::Connector {
                from: crate::connector::ConnectorPoint { x: 50.0, y: 0.0 },
                to: crate::connector::ConnectorPoint { x: 150.0, y: 50.0 },
                routing: Default::default(),
                curvature: 0.4,
                width: 2.0,
                color: "#fff".into(),
                arrow_end: true,
                arrow_start: false,
                arrow_size: 10.0,
                dashed: None,
                timing: Default::default(),
                style: CssStyle::default(),
                timeline: Vec::new(),
                stagger: None,
            }),
            position: Some(crate::PositionMode::Absolute { x: 0.0, y: 0.0 }),
            x: None,
            y: None,
            z_index: None,
            bleed: false,
        };
        let scene = vec![conn];
        let built = build_scene(&scene, (800.0, 600.0));
        let layout = run_layout(&built.root, (800.0, 600.0), &ConversionContext::default());
        let l = layout
            .get(built.root.children[0].id)
            .expect("connector laid out");
        // bbox 100×50 + (16 + 10) = 126×76.
        assert_eq!(l.width, 126.0);
        assert_eq!(l.height, 76.0);
    }

    #[test]
    fn counter_intrinsic_size_reserves_space_for_max_value() {
        // 1234 → 1234 → format with 0 decimals → measure largest absolute value.
        // Expectation: width > 0 (cosmic-text didn't fail), height ≈ font_size × line_height.
        use crate::counter::Counter;

        use rustmotion_core::css::units::Length;
        let counter = ChildComponent {
            component: Component::Counter(Counter {
                duration: None,
                from: 0.0,
                to: 1234.0,
                decimals: 0,
                separator: None,
                prefix: None,
                suffix: None,
                easing: Default::default(),
                timing: Default::default(),
                style: CssStyle {
                    font_size: Some(Length::Px(64.0)),
                    ..Default::default()
                },
                timeline: Vec::new(),
                stagger: None,
                text_shadow: None,
                stroke: None,
            }),
            position: Some(crate::PositionMode::Absolute { x: 10.0, y: 20.0 }),
            x: None,
            y: None,
            z_index: None,
            bleed: false,
        };
        let scene = vec![counter];
        let built = build_scene(&scene, (800.0, 600.0));
        let layout = run_layout(&built.root, (800.0, 600.0), &ConversionContext::default());
        let id = built.root.children[0].id;
        let l = layout.get(id).expect("counter laid out");
        assert!(
            l.width > 0.0,
            "counter width should be > 0, got {}",
            l.width
        );
        // line_height defaults to font_size × 1.3 = 83.2. Allow some slack.
        assert!(
            l.height >= 60.0,
            "counter height should be ≥ ~one line ({}), got {}",
            64.0,
            l.height
        );
    }

    #[test]
    fn badge_intrinsic_size_includes_padding_and_text() {
        // Default size = Md → font_size 14, h_pad 12, v_pad 6, icon 18.
        // Without an icon, height ≈ 6×2 + 14×1.3 ≈ 30.2.
        use crate::badge::{Badge, BadgeSize, BadgeVariant};

        let badge = ChildComponent {
            component: Component::Badge(Badge {
                text: "New".into(),
                icon: None,
                variant: BadgeVariant::Solid,
                badge_size: BadgeSize::Md,
                dot: false,
                dot_color: None,
                pulse: false,
                count: None,
                timing: Default::default(),
                style: CssStyle::default(),
                timeline: Vec::new(),
                stagger: None,
            }),
            position: Some(crate::PositionMode::Absolute { x: 0.0, y: 0.0 }),
            x: None,
            y: None,
            z_index: None,
            bleed: false,
        };
        let scene = vec![badge];
        let built = build_scene(&scene, (400.0, 200.0));
        let layout = run_layout(&built.root, (400.0, 200.0), &ConversionContext::default());
        let id = built.root.children[0].id;
        let l = layout.get(id).expect("badge laid out");
        // h_pad×2 = 24 alone, plus the text width.
        assert!(
            l.width > 24.0,
            "badge width should exceed padding alone, got {}",
            l.width
        );
        assert!(
            (l.height - 30.2).abs() < 2.0,
            "badge height should be ~30.2, got {}",
            l.height
        );
    }

    #[test]
    fn line_intrinsic_size_matches_endpoint_bounding_box() {
        let line = ChildComponent {
            component: Component::Line(crate::line::Line {
                x1: 10.0,
                y1: 20.0,
                x2: 110.0,
                y2: 80.0,
                width: 2.0,
                color: "#fff".into(),
                dashed: None,
                timing: Default::default(),
                style: CssStyle::default(),
                timeline: Vec::new(),
                stagger: None,
            }),
            position: Some(crate::PositionMode::Absolute { x: 0.0, y: 0.0 }),
            x: None,
            y: None,
            z_index: None,
            bleed: false,
        };
        let scene = vec![line];
        let built = build_scene(&scene, (800.0, 600.0));
        let layout = run_layout(&built.root, (800.0, 600.0), &ConversionContext::default());
        let id = built.root.children[0].id;
        let l = layout.get(id).expect("line laid out");
        assert_eq!(l.width, 100.0);
        assert_eq!(l.height, 60.0);
    }

    // ─── M2: rich_text intrinsic wired into component_intrinsic ─────────────

    #[test]
    fn rich_text_child_gets_a_non_zero_intrinsic_size() {
        // Before the fix: rich_text had no `component_intrinsic` entry, so
        // an auto-sized rich_text laid out at 0×0 and was invisible.
        use crate::rich_text::{RichText, RichTextSpan};
        use rustmotion_core::css::units::Length;

        let rich_text = ChildComponent {
            component: Component::RichText(RichText {
                spans: vec![
                    RichTextSpan {
                        text: "Save ".into(),
                        color: None,
                        font_size: None,
                        font_weight: None,
                        font_family: None,
                        font_style: None,
                        letter_spacing: None,
                    },
                    RichTextSpan {
                        text: "40%".into(),
                        color: Some("#5C39EE".into()),
                        font_size: None,
                        font_weight: None,
                        font_family: None,
                        font_style: None,
                        letter_spacing: None,
                    },
                ],
                max_width: None,
                timing: Default::default(),
                style: CssStyle {
                    font_size: Some(Length::Px(40.0)),
                    ..Default::default()
                },
                timeline: Vec::new(),
                stagger: None,
            }),
            position: Some(crate::PositionMode::Absolute { x: 0.0, y: 0.0 }),
            x: None,
            y: None,
            z_index: None,
            bleed: false,
        };
        let scene = vec![rich_text];
        let built = build_scene(&scene, (800.0, 600.0));
        let layout = run_layout(&built.root, (800.0, 600.0), &ConversionContext::default());
        let id = built.root.children[0].id;
        let l = layout.get(id).expect("rich_text laid out");
        assert!(
            l.width > 0.0,
            "rich_text width should be > 0, got {}",
            l.width
        );
        assert!(
            l.height > 0.0,
            "rich_text height should be > 0, got {}",
            l.height
        );
    }

    // ─── M3: the `glow` effect renders as a coloured drop-shadow ────────────

    #[test]
    fn glow_effect_adds_a_coloured_drop_shadow_filter() {
        use rustmotion_core::css::style::{Color, FilterFn};
        use rustmotion_core::css::units::Length as CLength;
        use rustmotion_core::schema::{AnimationEffect, GlowConfig};

        let mut shape = make_shape(100.0, 100.0);
        if let Component::Shape(ref mut s) = shape.component {
            s.style.animation = vec![AnimationEffect::Glow(GlowConfig {
                color: "#5C39EE".to_string(),
                radius: 12.0,
                intensity: 1.0,
            })];
        }
        let anim = BuildAnimationCtx {
            time: 0.0,
            scenario_time: 0.0,
            scene_duration: 1.0,
            fps: 30,
        };
        let scene = [shape];
        let built = build_scene_with_anim(&scene, (400.0, 400.0), anim);
        let filters = built.root.children[0]
            .css
            .filter
            .as_ref()
            .expect("glow must add a filter list");
        let shadow = filters
            .iter()
            .find(|f| matches!(f, FilterFn::DropShadow { .. }))
            .expect("a DropShadow filter must be present");
        let FilterFn::DropShadow { blur, color, .. } = shadow else {
            unreachable!()
        };
        match blur {
            Some(CLength::Px(px)) => {
                assert_eq!(*px, 12.0, "blur radius must match GlowConfig.radius")
            }
            other => panic!("expected Some(Length::Px(12.0)) blur, got {:?}", other),
        }
        // The defect this fixes: `color: None` resolves to black downstream
        // (see `paint_pass.rs`'s `unwrap_or(SColor::BLACK)`). The colour must
        // be `Some` and must match the configured glow colour, not black.
        match color {
            Some(Color::Rgba { r, g, b, a }) => {
                assert_eq!(*r, 0x5C);
                assert_eq!(*g, 0x39);
                assert_eq!(*b, 0xEE);
                assert!(*a > 0.0, "alpha must be > 0 for the glow to be visible");
            }
            other => panic!("expected Some(Color::Rgba{{..}}), got {:?}", other),
        }
    }

    #[test]
    fn glow_effect_scales_alpha_by_intensity() {
        use rustmotion_core::css::style::{Color, FilterFn};
        use rustmotion_core::schema::{AnimationEffect, GlowConfig};

        let mut shape = make_shape(100.0, 100.0);
        if let Component::Shape(ref mut s) = shape.component {
            s.style.animation = vec![AnimationEffect::Glow(GlowConfig {
                color: "#FFFFFFFF".to_string(), // opaque white
                radius: 10.0,
                intensity: 0.5,
            })];
        }
        let anim = BuildAnimationCtx {
            time: 0.0,
            scenario_time: 0.0,
            scene_duration: 1.0,
            fps: 30,
        };
        let scene = [shape];
        let built = build_scene_with_anim(&scene, (400.0, 400.0), anim);
        let filters = built.root.children[0].css.filter.as_ref().unwrap();
        let FilterFn::DropShadow { color, .. } = filters
            .iter()
            .find(|f| matches!(f, FilterFn::DropShadow { .. }))
            .unwrap()
        else {
            unreachable!()
        };
        let Some(Color::Rgba { a, .. }) = color else {
            panic!("expected Rgba color");
        };
        assert!(
            (*a - 0.5).abs() < 1e-4,
            "intensity 0.5 on an opaque colour must scale alpha to ~0.5, got {}",
            a
        );
    }

    #[test]
    fn no_glow_effect_leaves_filter_untouched() {
        let shape = make_shape(100.0, 100.0);
        let anim = BuildAnimationCtx {
            time: 0.0,
            scenario_time: 0.0,
            scene_duration: 1.0,
            fps: 30,
        };
        let scene = [shape];
        let built = build_scene_with_anim(&scene, (400.0, 400.0), anim);
        assert!(built.root.children[0].css.filter.is_none());
    }

    // ─── #126 / W3: the 23 components with no size source ───────────────────

    /// Build a `ChildComponent` from a JSON literal (matches the `type`-tagged
    /// `Component` enum's own `Deserialize` impl) — much less error-prone than
    /// a full struct literal for components with a dozen+ fields.
    fn child_from_json(json: serde_json::Value) -> ChildComponent {
        let component: Component =
            serde_json::from_value(json.clone()).unwrap_or_else(|e| panic!("{e}\n{json:#}"));
        ChildComponent {
            component,
            position: None,
            x: None,
            y: None,
            z_index: None,
            bleed: false,
        }
    }

    /// Lay out a single unsized child inside a card and return its final
    /// `(width, height)`. The card itself has no explicit width or height
    /// either, so this also exercises acceptance criterion #3 ("a card with
    /// height: auto sizes to the component instead of collapsing to
    /// padding").
    fn layout_in_auto_card(child_json: serde_json::Value) -> (f32, f32) {
        let card = make_card(vec![child_from_json(child_json)], CssStyle::default());
        let scene = vec![ChildComponent {
            component: card,
            position: Some(crate::PositionMode::Absolute { x: 0.0, y: 0.0 }),
            x: None,
            y: None,
            z_index: None,
            bleed: false,
        }];
        let built = build_scene(&scene, (1920.0, 1080.0));
        let layout = run_layout(&built.root, (1920.0, 1080.0), &ConversionContext::default());
        let child_id = built.root.children[0].children[0].id;
        let l = layout.get(child_id).expect("child laid out");
        (l.width, l.height)
    }

    /// Every one of the 23 components #126 lists gets a positive width *and*
    /// height with no `style` at all — before this file's fix they all laid
    /// out at 0×0 inside a card (proven by rendering: see the workstream's
    /// scratch `ink-measure` harness, which shows every one of these going
    /// from 0 painted pixels to a positive count under the identical fixture).
    /// A positive layout box is the necessary precondition for any of that
    /// painted ink — this test is the fast, render-free regression guard.
    #[test]
    fn all_23_unsized_components_get_a_positive_box_in_a_card() {
        let cases: &[(&str, serde_json::Value)] = &[
            ("callout", json!({"type":"callout","text":"Hello"})),
            (
                "chart",
                json!({"type":"chart","chart_type":"bar","data":[{"value":10}]}),
            ),
            ("comparison", json!({"type":"comparison"})),
            (
                "dot_map",
                json!({"type":"dot_map","points":[{"lat":10.0,"lng":10.0}]}),
            ),
            ("gauge", json!({"type":"gauge","value":50})),
            ("gif", json!({"type":"gif","src":"a.gif"})),
            ("heatmap", json!({"type":"heatmap","data":[[1.0,2.0]]})),
            ("icon", json!({"type":"icon","icon":"lucide:home"})),
            ("image", json!({"type":"image","src":"a.png"})),
            ("lottie", json!({"type":"lottie","data":"{}"})),
            ("marquee", json!({"type":"marquee","content":"hi"})),
            (
                "mockup",
                json!({"type":"mockup","device":"iphone","src":"a.png"}),
            ),
            ("pill_nav", json!({"type":"pill_nav","items":["A","B"]})),
            ("shape", json!({"type":"shape","shape":"rect"})),
            ("skeleton", json!({"type":"skeleton"})),
            (
                "skeleton_text",
                json!({"type":"skeleton","variant":"text","lines":3}),
            ),
            (
                "sparkline",
                json!({"type":"sparkline","data":[1.0,2.0,3.0]}),
            ),
            ("stat", json!({"type":"stat","value":"42"})),
            (
                "stepper",
                json!({"type":"stepper","steps":[{"label":"A"},{"label":"B"}]}),
            ),
            ("svg", json!({"type":"svg","data":"<svg></svg>"})),
            (
                "tag_cloud",
                json!({"type":"tag_cloud","tags":[{"text":"rust","weight":1.0}]}),
            ),
            ("tooltip", json!({"type":"tooltip","text":"hi"})),
            ("treemap", json!({"type":"treemap","data":[{"value":10.0}]})),
            ("video", json!({"type":"video","src":"a.mp4"})),
        ];
        for (name, json) in cases {
            let (w, h) = layout_in_auto_card(json.clone());
            assert!(w > 0.0, "{name}: width should be > 0, got {w}");
            assert!(h > 0.0, "{name}: height should be > 0, got {h}");
        }
    }

    #[test]
    fn heatmap_intrinsic_size_matches_cell_grid_formula() {
        // 2 rows x 3 cols, default cell_size=14, cell_gap=3.
        // width = (3-1)*(14+3) + 14 = 48, height = (2-1)*17 + 14 = 31.
        let (w, h) =
            layout_in_auto_card(json!({"type":"heatmap","data":[[1.0,2.0,3.0],[4.0,5.0,6.0]]}));
        assert_eq!(w, 48.0);
        assert_eq!(h, 31.0);
    }

    #[test]
    fn sparkline_gets_the_documented_120x40_default() {
        // .claude/skills/rustmotion/rules/data-viz-components.md: "Sparkline:
        // ... compact (120x40 default), inline use."
        let (w, h) = layout_in_auto_card(json!({"type":"sparkline","data":[1.0,2.0,3.0]}));
        assert_eq!(w, 120.0);
        assert_eq!(h, 40.0);
    }

    #[test]
    fn stat_gets_the_documented_280x180_default() {
        // .claude/skills/rustmotion/rules/stat-cards.md's own "GOOD" example.
        let (w, h) = layout_in_auto_card(json!({"type":"stat","value":"42"}));
        assert_eq!(w, 280.0);
        assert_eq!(h, 180.0);
    }

    #[test]
    fn gauge_default_size_is_square() {
        let (w, h) = layout_in_auto_card(json!({"type":"gauge","value":50}));
        assert_eq!(w, h);
        assert!(w > 0.0);
    }

    #[test]
    fn pill_nav_height_promotes_its_own_declared_field() {
        // `height` is already a field on PillNav (default 44.0) — the fix
        // just promotes it to CSS, like Progress/Switch/Slider above.
        let (_, h) = layout_in_auto_card(json!({"type":"pill_nav","items":["Overview"]}));
        assert_eq!(h, 44.0);
    }

    #[test]
    fn callout_width_grows_with_its_own_text_content() {
        // A longer text must produce a wider box (content-derived, not a
        // fixed constant) — proves the measured-text path is actually wired
        // up, not just a padding-only fallback.
        let (short_w, _) = layout_in_auto_card(json!({"type":"callout","text":"Hi"}));
        let (long_w, _) = layout_in_auto_card(
            json!({"type":"callout","text":"This is a much longer callout message"}),
        );
        assert!(
            long_w > short_w,
            "longer text should measure a wider box: short={short_w}, long={long_w}"
        );
    }

    #[test]
    fn three_stats_in_a_flex_row_all_get_a_positive_width() {
        // #126's second bug: 3 `stat`s in a flex-row card with no explicit
        // size rendered zero pixels because *width* (not height) collapsed
        // to 0 — align-items:stretch only helps the cross axis, which is
        // height in a row.
        let stats = vec![
            child_from_json(json!({"type":"stat","value":"45.2K","label":"Users"})),
            child_from_json(json!({"type":"stat","value":"12%","label":"Growth"})),
            child_from_json(json!({"type":"stat","value":"$8.1M","label":"Revenue"})),
        ];
        let card = make_card(
            stats,
            CssStyle {
                display: Some(Display::Flex),
                flex_direction: Some(FlexDirection::Row),
                gap: Some(Gap::Uniform(LengthPercentage::Px(16.0))),
                ..Default::default()
            },
        );
        let scene = vec![ChildComponent {
            component: card,
            position: Some(crate::PositionMode::Absolute { x: 0.0, y: 0.0 }),
            x: None,
            y: None,
            z_index: None,
            bleed: false,
        }];
        let built = build_scene(&scene, (1920.0, 1080.0));
        let layout = run_layout(&built.root, (1920.0, 1080.0), &ConversionContext::default());
        for (i, child) in built.root.children[0].children.iter().enumerate() {
            let l = layout.get(child.id).expect("stat laid out");
            assert!(
                l.width > 0.0,
                "stat #{i}: width should be > 0, got {}",
                l.width
            );
            assert!(
                l.height > 0.0,
                "stat #{i}: height should be > 0, got {}",
                l.height
            );
        }
    }

    // ── Round 4 audit, lot LAYOUT, constat 2: the CSS cascade is wired ──────
    // `crates/rustmotion-core/src/css/cascade.rs::inherit_from` existed but
    // nothing called it — `color`/`font-*` set on a container never reached
    // children lacking their own value.

    #[test]
    fn card_color_cascades_to_text_child_with_no_color_of_its_own() {
        use rustmotion_core::css::style::Color;

        let card = make_card(
            vec![make_text("hello", CssStyle::default())],
            CssStyle {
                color: Some(Color::String("#ff0000".into())),
                ..Default::default()
            },
        );
        let scene = vec![ChildComponent {
            component: card,
            position: Some(crate::PositionMode::Absolute { x: 0.0, y: 0.0 }),
            x: None,
            y: None,
            z_index: None,
            bleed: false,
        }];
        let built = build_scene(&scene, (800.0, 600.0));
        let text_box = &built.root.children[0].children[0];
        assert_eq!(
            text_box.css.color,
            Some(Color::String("#ff0000".into())),
            "text child declares no color of its own — it should inherit the card's"
        );
    }

    #[test]
    fn text_own_color_wins_over_inherited_card_color() {
        use rustmotion_core::css::style::Color;

        let card = make_card(
            vec![make_text(
                "hello",
                CssStyle {
                    color: Some(Color::String("#00ff00".into())),
                    ..Default::default()
                },
            )],
            CssStyle {
                color: Some(Color::String("#ff0000".into())),
                ..Default::default()
            },
        );
        let scene = vec![ChildComponent {
            component: card,
            position: Some(crate::PositionMode::Absolute { x: 0.0, y: 0.0 }),
            x: None,
            y: None,
            z_index: None,
            bleed: false,
        }];
        let built = build_scene(&scene, (800.0, 600.0));
        let text_box = &built.root.children[0].children[0];
        assert_eq!(
            text_box.css.color,
            Some(Color::String("#00ff00".into())),
            "text child's own explicit color must win over the inherited card color"
        );
    }

    #[test]
    fn card_display_does_not_cascade_to_text_child() {
        // `display` is not an inheritable CSS property — only the documented
        // inheritable list (color, font-*, text-align, white-space, ...)
        // should propagate.
        let card = make_card(
            vec![make_text("hello", CssStyle::default())],
            CssStyle {
                display: Some(Display::Flex),
                ..Default::default()
            },
        );
        let scene = vec![ChildComponent {
            component: card,
            position: Some(crate::PositionMode::Absolute { x: 0.0, y: 0.0 }),
            x: None,
            y: None,
            z_index: None,
            bleed: false,
        }];
        let built = build_scene(&scene, (800.0, 600.0));
        let text_box = &built.root.children[0].children[0];
        assert_eq!(text_box.css.display, None);
    }

    // ── Round 4 audit, lot LAYOUT, constat 4: `apply_intrinsic_overrides`'s
    // default size ignored an explicit `aspect-ratio`. ─────────────────────

    fn make_aspect_shape(width: f32, aspect_ratio: f32) -> ChildComponent {
        ChildComponent {
            component: Component::Shape(crate::shape::Shape {
                shape: rustmotion_core::schema::ShapeType::Rect,
                text: None,
                timing: Default::default(),
                style: CssStyle {
                    width: Some(CSize::Length(CLP::Px(width))),
                    aspect_ratio: Some(aspect_ratio),
                    ..Default::default()
                },
                timeline: Vec::new(),
                stagger: None,
                fill: None,
                stroke: None,
            }),
            position: Some(crate::PositionMode::Absolute { x: 0.0, y: 0.0 }),
            x: None,
            y: None,
            z_index: None,
            bleed: false,
        }
    }

    #[test]
    fn explicit_width_with_aspect_ratio_derives_height_instead_of_the_hardcoded_default() {
        // `shape`'s hardcoded default is 80×80 (see `apply_intrinsic_overrides`).
        // `width: 400` + `aspect-ratio: 16/9` should derive height = 225, not
        // fall back to the unrelated 80px default.
        let scene = vec![make_aspect_shape(400.0, 16.0 / 9.0)];
        let built = build_scene(&scene, (1920.0, 1080.0));
        let layout = run_layout(&built.root, (1920.0, 1080.0), &ConversionContext::default());
        let l = layout
            .get(built.root.children[0].id)
            .expect("shape laid out");
        assert!(
            (l.width - 400.0).abs() < 1.0,
            "width should stay the author's explicit 400, got {}",
            l.width
        );
        assert!(
            (l.height - 225.0).abs() < 1.0,
            "height should derive from width/aspect-ratio (400/1.778=225), got {}",
            l.height
        );
    }

    #[test]
    fn neither_axis_set_with_aspect_ratio_derives_height_from_the_default_width() {
        // No width/height at all: the natural default width (80 for shape)
        // is kept, but height should come from the aspect-ratio, not the
        // unrelated 80px default.
        let scene = vec![make_aspect_shape_no_width(2.0)];
        let built = build_scene(&scene, (1920.0, 1080.0));
        let layout = run_layout(&built.root, (1920.0, 1080.0), &ConversionContext::default());
        let l = layout
            .get(built.root.children[0].id)
            .expect("shape laid out");
        assert!(
            (l.width - 80.0).abs() < 1.0,
            "width should keep the natural default (80), got {}",
            l.width
        );
        assert!(
            (l.height - 40.0).abs() < 1.0,
            "height should derive from the default width/aspect-ratio (80/2=40), got {}",
            l.height
        );
    }

    fn make_aspect_shape_no_width(aspect_ratio: f32) -> ChildComponent {
        ChildComponent {
            component: Component::Shape(crate::shape::Shape {
                shape: rustmotion_core::schema::ShapeType::Rect,
                text: None,
                timing: Default::default(),
                style: CssStyle {
                    aspect_ratio: Some(aspect_ratio),
                    ..Default::default()
                },
                timeline: Vec::new(),
                stagger: None,
                fill: None,
                stroke: None,
            }),
            position: Some(crate::PositionMode::Absolute { x: 0.0, y: 0.0 }),
            x: None,
            y: None,
            z_index: None,
            bleed: false,
        }
    }
}