azul-layout 0.0.12

Layout solver + font and image loader the Azul GUI framework
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
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
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
//! Bridge between Azul's CSS style system and the Taffy layout engine.
//!
//! This module translates Azul CSS properties into Taffy's `Style` struct and
//! implements Taffy's `TraversePartialTree`, `LayoutPartialTree`, `CacheTree`,
//! `LayoutFlexboxContainer`, and `LayoutGridContainer` traits via the
//! [`TaffyBridge`] struct. The main entry point is [`layout_taffy_subtree`],
//! which is called from `fc.rs` when a flex or grid formatting context is
//! encountered during layout.

use crate::solver3::calc::CalcResolveContext;
use crate::solver3::getters::{get_overflow_x, get_overflow_y};
use azul_core::dom::FormattingContext;
use azul_css::{
    css::CssPropertyValue,
    props::{
        basic::{
            pixel::{DEFAULT_FONT_SIZE, PT_TO_PX},
            PixelValue, SizeMetric,
        },
        layout::{
            dimensions::CalcAstItemVec,
            flex::LayoutFlexBasis,
            grid::{GridAutoTracks, GridTemplate, GridTrackSizing},
            LayoutAlignContent, LayoutAlignItems, LayoutAlignSelf, LayoutDisplay,
            LayoutFlexDirection, LayoutFlexWrap, LayoutGridAutoFlow, LayoutJustifyContent,
            LayoutPosition, LayoutWritingMode,
        },
        property::{
            LayoutAlignContentValue, LayoutAlignItemsValue, LayoutAlignSelfValue,
            LayoutDisplayValue, LayoutFlexDirectionValue, LayoutFlexWrapValue,
            LayoutGridAutoColumnsValue, LayoutGridAutoFlowValue, LayoutGridAutoRowsValue,
            LayoutGridTemplateColumnsValue, LayoutGridTemplateRowsValue, LayoutJustifyContentValue,
            LayoutPositionValue,
        },
    },
};
use taffy::style::{MaxTrackSizingFunction, MinTrackSizingFunction, TrackSizingFunction};

/// CSS reference pixels per inch (96 dpi per CSS Values spec).
const CSS_PX_PER_INCH: f32 = 96.0;

/// Convert `PixelValue` to pixels, only for absolute units (no %, and em/rem use fallback)
/// Used where proper resolution context is not available (grid tracks, etc.)
#[allow(clippy::match_same_arms)] // enum/value mapping/dispatch table: one arm per input variant (or cross-type bindings that can't merge)
fn pixel_value_to_pixels_fallback(pv: &PixelValue) -> Option<f32> {
    match pv.metric {
        SizeMetric::Px => Some(pv.number.get()),
        SizeMetric::Pt => Some(pv.number.get() * PT_TO_PX),
        SizeMetric::In => Some(pv.number.get() * CSS_PX_PER_INCH),
        SizeMetric::Cm => Some(pv.number.get() * CSS_PX_PER_INCH / 2.54),
        SizeMetric::Mm => Some(pv.number.get() * CSS_PX_PER_INCH / 25.4),
        // For em/rem, use DEFAULT_FONT_SIZE as fallback (not ideal but needed without context)
        SizeMetric::Em | SizeMetric::Rem => Some(pv.number.get() * DEFAULT_FONT_SIZE),
        SizeMetric::Percent => None, // Cannot resolve without containing block
        // Viewport units: Cannot resolve without viewport context
        SizeMetric::Vw | SizeMetric::Vh | SizeMetric::Vmin | SizeMetric::Vmax => None,
    }
}

/// Converts an Azul `grid-template-rows` value into Taffy grid template components.
fn grid_template_rows_to_taffy(
    val: LayoutGridTemplateRowsValue,
) -> Vec<GridTemplateComponent<String>> {
    let auto_tracks = val.get_property_or_default().unwrap_or_default();
    auto_tracks
        .tracks
        .iter()
        .map(|track| GridTemplateComponent::Single(translate_track(track)))
        .collect()
}

/// Converts an Azul `grid-template-columns` value into Taffy grid template components.
fn grid_template_columns_to_taffy(
    val: LayoutGridTemplateColumnsValue,
) -> Vec<GridTemplateComponent<String>> {
    let auto_tracks = val.get_property_or_default().unwrap_or_default();
    auto_tracks
        .tracks
        .iter()
        .map(|track| GridTemplateComponent::Single(translate_track(track)))
        .collect()
}

/// Converts an Azul `grid-auto-rows` value into Taffy min/max track sizing pairs.
fn grid_auto_rows_to_taffy(
    val: LayoutGridAutoRowsValue,
) -> Vec<taffy::MinMax<MinTrackSizingFunction, MaxTrackSizingFunction>> {
    let auto_tracks = val.get_property_or_default().unwrap_or_default();
    let tracks = auto_tracks.tracks;
    tracks
        .iter()
        .map(|track| taffy::MinMax {
            min: translate_track(track).min,
            max: translate_track(track).max,
        })
        .collect()
}

/// Converts an Azul `grid-auto-columns` value into Taffy track sizing functions.
fn grid_auto_columns_to_taffy(
    val: LayoutGridAutoColumnsValue,
) -> Vec<taffy::TrackSizingFunction> {
    let auto_tracks = val.get_property_or_default().unwrap_or_default();
    auto_tracks.tracks.iter().map(translate_track).collect()
}

#[allow(clippy::cast_precision_loss)] // bounded layout/render numeric cast
fn translate_track(track: &GridTrackSizing) -> taffy::TrackSizingFunction {
    // Helper to resolve PixelValue to absolute pixels (handles em, rem, but not %)
    // Grid track sizing in Taffy doesn't support % - only absolute values
    let px_to_float = |pv: PixelValue| -> f32 {
        pixel_value_to_pixels_fallback(&pv).unwrap_or(0.0)
    };

    match track {
        GridTrackSizing::MinContent => minmax(
            taffy::MinTrackSizingFunction::min_content(),
            taffy::MaxTrackSizingFunction::min_content(),
        ),
        GridTrackSizing::MaxContent => minmax(
            taffy::MinTrackSizingFunction::max_content(),
            taffy::MaxTrackSizingFunction::max_content(),
        ),
        GridTrackSizing::MinMax(minmax_box) => minmax(
            translate_track(&minmax_box.min).min,
            translate_track(&minmax_box.max).max,
        ),
        GridTrackSizing::Fixed(px) => {
            // Fixed tracks: resolve em/rem to pixels
            // Note: % is not supported in grid track sizing (CSS Grid spec)
            let pixels = px_to_float(*px);
            minmax(
                taffy::MinTrackSizingFunction::length(pixels),
                taffy::MaxTrackSizingFunction::length(pixels),
            )
        }
        GridTrackSizing::Fr(fr) => {
            // Fr units: minmax(auto, Xfr) per CSS Grid spec
            // The min is auto, max is the fractional value
            // fr is stored as i32 * 100 (e.g., 1fr = 100, 2fr = 200)
            minmax(
                taffy::MinTrackSizingFunction::auto(),
                taffy::MaxTrackSizingFunction::fr(*fr as f32 / 100.0),
            )
        }
        GridTrackSizing::Auto => minmax(
            taffy::MinTrackSizingFunction::min_content(),
            taffy::MaxTrackSizingFunction::max_content(),
        ),
        GridTrackSizing::FitContent(px) => {
            // fit-content: resolve em/rem to pixels
            let pixels = px_to_float(*px);
            minmax(
                taffy::MinTrackSizingFunction::length(pixels),
                taffy::MaxTrackSizingFunction::max_content(),
            )
        }
    }
}

const fn minmax(min: MinTrackSizingFunction, max: MaxTrackSizingFunction) -> taffy::TrackSizingFunction {
    TrackSizingFunction { min, max }
}

fn layout_display_to_taffy(val: LayoutDisplayValue) -> Display {
    match val.get_property_or_default().unwrap_or_default() {
        LayoutDisplay::None => Display::None,
        LayoutDisplay::Flex | LayoutDisplay::InlineFlex => Display::Flex,
        LayoutDisplay::Grid | LayoutDisplay::InlineGrid => Display::Grid,
        _ => Display::Block,
    }
}

// to determine their CB; Taffy's Position::Absolute handles this for both flex and grid
#[allow(clippy::match_same_arms)] // enum/value mapping/dispatch table: one arm per input variant (or cross-type bindings that can't merge)
fn layout_position_to_taffy(val: LayoutPositionValue) -> Position {
    match val.get_property_or_default().unwrap_or_default() {
        LayoutPosition::Absolute => Position::Absolute,
        LayoutPosition::Fixed => Position::Absolute, // Taffy has no Fixed variant
        LayoutPosition::Relative => Position::Relative,
        LayoutPosition::Static => Position::Relative,
        LayoutPosition::Sticky => Position::Relative, // Sticky treated as Relative
    }
}

#[allow(clippy::cast_sign_loss)] // bounded layout/render numeric cast
fn decode_compact_grid_line(v: i16) -> GridPlacement<String> {
    if v == azul_css::compact_cache::I16_AUTO || v == azul_css::compact_cache::I16_SENTINEL {
        GridPlacement::Auto
    } else if v < 0 {
        GridPlacement::<String>::from_span((-v) as u16)
    } else {
        GridPlacement::<String>::from_line_index(v)
    }
}

fn grid_auto_flow_to_taffy(val: LayoutGridAutoFlowValue) -> GridAutoFlow {
    match val.get_property_or_default().unwrap_or_default() {
        LayoutGridAutoFlow::Row => GridAutoFlow::Row,
        LayoutGridAutoFlow::Column => GridAutoFlow::Column,
        LayoutGridAutoFlow::RowDense => GridAutoFlow::RowDense,
        LayoutGridAutoFlow::ColumnDense => GridAutoFlow::ColumnDense,
    }
}

/// Convert an azul `GridLine` (single start or end) to a Taffy `GridPlacement`.
#[allow(clippy::cast_possible_truncation, clippy::cast_sign_loss)] // bounded layout/render numeric cast
fn grid_line_to_taffy(
    line: &azul_css::props::layout::grid::GridLine,
) -> GridPlacement<String> {
    use azul_css::props::layout::grid::GridLine as AzGridLine;
    use taffy::style_helpers::{TaffyGridLine, TaffyGridSpan};
    match line {
        AzGridLine::Auto => GridPlacement::Auto,
        AzGridLine::Line(n) => {
            GridPlacement::<String>::from_line_index(*n as i16)
        }
        AzGridLine::Span(n) => GridPlacement::<String>::from_span(*n as u16),
        AzGridLine::Named(named) => {
            // Named lines: use the name with optional span
            let name = named.grid_line_name.as_str().to_string();
            if named.span_count > 0 {
                GridPlacement::NamedSpan(name, named.span_count as u16)
            } else {
                GridPlacement::NamedLine(name, 0)
            }
        }
    }
}

/// Convert an azul `GridPlacement` (grid-column / grid-row) to a Taffy `Line<GridPlacement>`.
fn grid_placement_to_taffy(
    placement: &azul_css::props::layout::grid::GridPlacement,
) -> Line<GridPlacement<String>> {
    Line {
        start: grid_line_to_taffy(&placement.grid_start),
        end: grid_line_to_taffy(&placement.grid_end),
    }
}

fn layout_flex_direction_to_taffy(val: LayoutFlexDirectionValue) -> FlexDirection {
    match val.get_property_or_default().unwrap_or_default() {
        LayoutFlexDirection::Row => FlexDirection::Row,
        LayoutFlexDirection::RowReverse => FlexDirection::RowReverse,
        LayoutFlexDirection::Column => FlexDirection::Column,
        LayoutFlexDirection::ColumnReverse => FlexDirection::ColumnReverse,
    }
}

fn layout_flex_wrap_to_taffy(val: LayoutFlexWrapValue) -> FlexWrap {
    match val.get_property_or_default().unwrap_or_default() {
        LayoutFlexWrap::NoWrap => FlexWrap::NoWrap,
        LayoutFlexWrap::Wrap => FlexWrap::Wrap,
        LayoutFlexWrap::WrapReverse => FlexWrap::WrapReverse,
    }
}

fn layout_align_items_to_taffy(val: LayoutAlignItemsValue) -> AlignItems {
    match val.get_property_or_default().unwrap_or_default() {
        LayoutAlignItems::Stretch => AlignItems::Stretch,
        LayoutAlignItems::Center => AlignItems::Center,
        LayoutAlignItems::Start => AlignItems::FlexStart,
        LayoutAlignItems::End => AlignItems::FlexEnd,
        LayoutAlignItems::Baseline => AlignItems::Baseline,
    }
}

fn layout_align_self_to_taffy(val: LayoutAlignSelfValue) -> Option<AlignSelf> {
    match val.get_property_or_default().unwrap_or_default() {
        LayoutAlignSelf::Auto => None, // Auto means inherit from parent's align-items (for non-abspos; abspos auto computes to itself per spec)
        LayoutAlignSelf::Start => Some(AlignSelf::FlexStart),
        LayoutAlignSelf::End => Some(AlignSelf::FlexEnd),
        LayoutAlignSelf::Center => Some(AlignSelf::Center),
        LayoutAlignSelf::Baseline => Some(AlignSelf::Baseline),
        LayoutAlignSelf::Stretch => Some(AlignSelf::Stretch),
    }
}

fn layout_align_content_to_taffy(val: LayoutAlignContentValue) -> AlignContent {
    match val.get_property_or_default().unwrap_or_default() {
        LayoutAlignContent::Start => AlignContent::FlexStart,
        LayoutAlignContent::End => AlignContent::FlexEnd,
        LayoutAlignContent::Center => AlignContent::Center,
        LayoutAlignContent::Stretch => AlignContent::Stretch,
        LayoutAlignContent::SpaceBetween => AlignContent::SpaceBetween,
        LayoutAlignContent::SpaceAround => AlignContent::SpaceAround,
    }
}

fn layout_justify_content_to_taffy(val: LayoutJustifyContentValue) -> JustifyContent {
    match val.get_property_or_default().unwrap_or_default() {
        LayoutJustifyContent::FlexStart => JustifyContent::FlexStart,
        LayoutJustifyContent::FlexEnd => JustifyContent::FlexEnd,
        LayoutJustifyContent::Start => JustifyContent::Start,
        LayoutJustifyContent::End => JustifyContent::End,
        LayoutJustifyContent::Center => JustifyContent::Center,
        LayoutJustifyContent::SpaceBetween => JustifyContent::SpaceBetween,
        LayoutJustifyContent::SpaceAround => JustifyContent::SpaceAround,
        LayoutJustifyContent::SpaceEvenly => JustifyContent::SpaceEvenly,
    }
}

fn layout_justify_items_to_taffy(
    val: azul_css::props::property::LayoutJustifyItemsValue,
) -> AlignItems {
    use azul_css::props::layout::grid::LayoutJustifyItems;
    match val.get_property_or_default().unwrap_or_default() {
        LayoutJustifyItems::Start => AlignItems::Start,
        LayoutJustifyItems::End => AlignItems::End,
        LayoutJustifyItems::Center => AlignItems::Center,
        LayoutJustifyItems::Stretch => AlignItems::Stretch,
    }
}

// TODO: visibility, z_index still missing
// --- CSS <-> Taffy conversion functions ---

use std::{collections::{BTreeMap, HashMap}, sync::Arc};

use azul_core::{dom::NodeId, geom::LogicalSize, styled_dom::StyledDom};
use azul_css::props::{
    layout::{LayoutHeight, LayoutWidth},
    property::{CssProperty, CssPropertyType},
};
use taffy::{
    compute_cached_layout, compute_flexbox_layout, compute_grid_layout, compute_leaf_layout,
    prelude::*, CacheTree, LayoutFlexboxContainer, LayoutGridContainer, LayoutInput, LayoutOutput,
    RunMode,
};

use crate::{
    font_traits::{FontLoaderTrait, ParsedFontTrait},
    solver3::{
        fc::{
            translate_taffy_point_back, translate_taffy_size_back, FloatingContext,
            LayoutConstraints, TextAlign as FcTextAlign,
        },
        getters::{
            get_align_content, get_align_items, get_css_border_bottom_width,
            get_css_border_left_width, get_css_border_right_width,
            get_css_border_top_width, get_css_box_sizing, get_css_bottom, get_css_height, get_css_left,
            get_css_margin_bottom, get_css_margin_left, get_css_margin_right, get_css_margin_top,
            get_css_max_height, get_css_max_width, get_css_min_height, get_css_min_width,
            get_css_padding_bottom, get_css_padding_left, get_css_padding_right,
            get_css_padding_top, get_css_right, get_css_top, get_css_width, get_flex_direction,
            get_position, MultiValue,
        },
        layout_tree::{get_display_type, LayoutTree},
        sizing, LayoutContext,
    },
};

/// Shared scrollbar detection for Taffy-managed flex/grid nodes.
///
/// When Taffy lays out a flex/grid container, it may expand the container
/// beyond the CSS-specified size (Taffy doesn't know about `overflow`).
/// This function resolves the CSS-constrained container size, computes
/// content vs. container overflow, and returns the resulting `ScrollbarRequirements`
/// plus the effective content size (for `overflow_content_size`).
///
/// Returns `(scrollbar_info, effective_content_width, effective_content_height)`.
fn compute_taffy_scrollbar_info<T: ParsedFontTrait>(
    ctx: &LayoutContext<'_, T>,
    tree: &LayoutTree,
    node_idx: usize,
    result_width: f32,
    result_height: f32,
    taffy_content_width: f32,
    taffy_content_height: f32,
) -> (crate::solver3::scrollbar::ScrollbarRequirements, f32, f32) {
    use crate::solver3::scrollbar::ScrollbarRequirements;

    let node = tree.get(node_idx);
    let dom_id = node.and_then(|n| n.dom_node_id);

    let Some(dom_id) = dom_id else {
        return (ScrollbarRequirements::default(), 0.0, 0.0);
    };

    let styled_node_state = ctx
        .styled_dom
        .styled_nodes
        .as_container()
        .get(dom_id)
        .map(|s| s.styled_node_state)
        .unwrap_or_default();

    // Compute padding + border from the node's box_props
    let (padding_width, padding_height, border_width, border_height, border_left, border_top) = tree
        .get(node_idx)
        .map_or((0.0, 0.0, 0.0, 0.0, 0.0, 0.0), |node| {
            let bp = node.box_props.unpack();
            (
                bp.padding.left + bp.padding.right,
                bp.padding.top + bp.padding.bottom,
                bp.border.left + bp.border.right,
                bp.border.top + bp.border.bottom,
                bp.border.left,
                bp.border.top,
            )
        });

    // Use CSS-specified dimensions as the container constraint.
    // Taffy may have expanded the box beyond these, but the CSS spec says
    // the container clips at the specified size.
    let css_height = get_css_height(ctx.styled_dom, dom_id, &styled_node_state);
    let css_width = get_css_width(ctx.styled_dom, dom_id, &styled_node_state);

    let result_content_w = result_width - padding_width - border_width;
    let result_content_h = result_height - padding_height - border_height;

    let css_container_w = css_width
        .exact()
        .and_then(|w| css_width_to_px(&w))
        .unwrap_or(result_content_w)
        .max(0.0);

    let css_container_h = css_height
        .exact()
        .and_then(|h| css_height_to_px(&h))
        .unwrap_or(result_content_h)
        .max(0.0);

    // Content size: use Taffy's content_size if non-zero,
    // else result size minus padding/border (Taffy expanded to fit).
    //
    // IMPORTANT: Taffy's content_size is measured from (0,0) of the border-box,
    // so it includes border.left/border.top as a leading offset. The container_size
    // is in content-box coordinates (result_width - padding - border). We must
    // subtract border.left/top from content_size to align coordinate spaces,
    // otherwise we get spurious horizontal scrollbars from the border offset.
    let content_w = if taffy_content_width > 0.0 {
        (taffy_content_width - border_left).max(0.0)
    } else {
        result_content_w.max(0.0)
    };
    let content_h = if taffy_content_height > 0.0 {
        (taffy_content_height - border_top).max(0.0)
    } else {
        result_content_h.max(0.0)
    };

    let content_size = LogicalSize::new(content_w, content_h);
    let container_size = LogicalSize::new(css_container_w, css_container_h);

    let scrollbar_info =
        crate::solver3::cache::compute_scrollbar_info_core(ctx, dom_id, &styled_node_state, content_size, container_size);

    (scrollbar_info, content_w, content_h)
}

/// Convert `LayoutWidth::Px(…)` to `f32`, returning None for non-px units.
fn css_width_to_px(w: &LayoutWidth) -> Option<f32> {
    match w {
        LayoutWidth::Px(px) => pixel_value_to_pixels_fallback(px),
        _ => None,
    }
}

/// Convert `LayoutHeight::Px(…)` to `f32`, returning None for non-px units.
fn css_height_to_px(h: &LayoutHeight) -> Option<f32> {
    match h {
        LayoutHeight::Px(px) => pixel_value_to_pixels_fallback(px),
        _ => None,
    }
}

// Helper function to convert MultiValue<PixelValue> to LengthPercentageAuto
fn multi_value_to_lpa(mv: MultiValue<PixelValue>) -> LengthPercentageAuto {
    match mv {
        MultiValue::Auto | MultiValue::Initial | MultiValue::Inherit => {
            LengthPercentageAuto::auto()
        }
        MultiValue::Exact(pv) => pixel_value_to_pixels_fallback(&pv)
            .map(LengthPercentageAuto::length)
            .or_else(|| {
                pv.to_percent()
                    .map(|p| LengthPercentageAuto::percent(p.get()))
            })
            .unwrap_or_else(LengthPercentageAuto::auto),
    }
}

// Helper function to convert MultiValue<PixelValue> to LengthPercentageAuto for margins
// CSS spec: margin initial value is 0, but `auto` has special centering meaning in flexbox
fn multi_value_to_lpa_margin(mv: MultiValue<PixelValue>) -> LengthPercentageAuto {
    match mv {
        MultiValue::Auto => {
            LengthPercentageAuto::auto() // Preserve auto for flexbox centering
        }
        MultiValue::Initial | MultiValue::Inherit => {
            LengthPercentageAuto::length(0.0) // Margins' initial value is 0
        }
        MultiValue::Exact(pv) => {
            pixel_value_to_pixels_fallback(&pv)
                .map(LengthPercentageAuto::length)
                .or_else(|| {
                    pv.to_percent()
                        .map(|p| LengthPercentageAuto::percent(p.get()))
                })
                .unwrap_or_else(|| LengthPercentageAuto::length(0.0)) // Fallback to 0 for
                                                                             // margins
        }
    }
}

// Helper function to convert MultiValue<PixelValue> to LengthPercentage
fn multi_value_to_lp(mv: MultiValue<PixelValue>) -> LengthPercentage {
    match mv {
        MultiValue::Auto | MultiValue::Initial | MultiValue::Inherit => {
            LengthPercentage::ZERO
        }
        MultiValue::Exact(pv) => pixel_value_to_pixels_fallback(&pv)
            .map(LengthPercentage::length)
            .or_else(|| {
                pv.to_percent()
                    .map(|p| LengthPercentage::percent(p.get()))
            })
            .unwrap_or(LengthPercentage::ZERO),
    }
}

// Helper function to convert plain PixelValue to LengthPercentage
/// Converts Azul's CSS overflow value to Taffy's Overflow enum.
///
/// Taffy only has Visible, Clip, Hidden, Scroll (no Auto).
/// CSS `auto` behaves like `scroll` from a layout perspective —
/// it constrains the container and enables scrolling.
#[allow(clippy::match_same_arms)] // enum/value mapping/dispatch table: one arm per input variant (or cross-type bindings that can't merge)
const fn azul_overflow_to_taffy(ov: MultiValue<azul_css::props::layout::LayoutOverflow>) -> taffy::Overflow {
    use azul_css::props::layout::LayoutOverflow;
    match ov {
        MultiValue::Exact(LayoutOverflow::Visible) => taffy::Overflow::Visible,
        MultiValue::Exact(LayoutOverflow::Hidden) => taffy::Overflow::Hidden,
        MultiValue::Exact(LayoutOverflow::Scroll) => taffy::Overflow::Scroll,
        MultiValue::Exact(LayoutOverflow::Auto) => taffy::Overflow::Scroll, // Auto acts like scroll for layout
        MultiValue::Exact(LayoutOverflow::Clip) => taffy::Overflow::Clip,
        _ => taffy::Overflow::Visible, // default
    }
}

fn pixel_to_lp(pv: PixelValue) -> LengthPercentage {
    pixel_value_to_pixels_fallback(&pv)
        .map(LengthPercentage::length)
        .or_else(|| {
            pv.to_percent()
                .map(|p| LengthPercentage::percent(p.get()))
        })
        .unwrap_or(LengthPercentage::ZERO)
}

/// Slow path for flex-basis: full property cache lookup + decode.
/// Extracted to avoid duplicating the logic in the compact fast-path fallback.
#[allow(clippy::trivially_copy_pass_by_ref)] // <=8B Copy param kept by-ref intentionally (hot pixel/coord path or to avoid churning call sites for a perf-neutral change)
fn flex_basis_slow_path(
    cache: &azul_core::prop_cache::CssPropertyCache,
    node_data: &azul_core::dom::NodeData,
    id: &NodeId,
    node_state: &azul_core::styled_dom::StyledNodeState,
    taffy_style: &mut Style,
) -> Dimension {
    cache
        .get_property(node_data, id, node_state, &CssPropertyType::FlexBasis)
        .and_then(|p| {
            if let CssProperty::FlexBasis(v) = p {
                let basis = match v.get_property_or_default().unwrap_or_default() {
                    LayoutFlexBasis::Auto => Dimension::auto(),
                    LayoutFlexBasis::Exact(pv) => pixel_value_to_pixels_fallback(&pv)
                        .map(Dimension::length)
                        .or_else(|| pv.to_percent().map(|p| Dimension::percent(p.get())))
                        .unwrap_or_else(Dimension::auto),
                };
                // WORKAROUND: If flex-basis is set and not auto, clear width to let flex-basis
                // take precedence. Workaround for Taffy not properly prioritizing flex-basis over width
                if !matches!(basis, auto if auto == Dimension::auto()) {
                    taffy_style.size.width = Dimension::auto();
                }
                Some(basis)
            } else {
                None
            }
        })
        .unwrap_or_else(Dimension::auto)
}

/// The bridge struct that implements Taffy's traits.
/// It holds mutable references to the solver's data structures, allowing Taffy
/// to read styles and write layout results back into our `LayoutTree`.
struct TaffyBridge<'a, 'b, T: ParsedFontTrait> {
    ctx: &'a mut LayoutContext<'b, T>,
    tree: &'a mut LayoutTree,
    /// Raw pointer to text cache - needed because we can't have multiple &mut references
    /// SAFETY: This pointer is only valid for the lifetime of the `TaffyBridge`
    /// and must only be used within `compute_child_layout` callbacks
    text_cache: *mut crate::font_traits::TextLayoutCache,
    /// Heap-pinned `CalcResolveContext`s whose addresses are passed into taffy
    /// `Dimension::calc(ptr)`. Kept alive for the duration of the layout pass.
    /// Uses `RefCell` because `get_core_container_style` takes `&self`.
    // Box gives each CalcResolveContext a stable heap address for the `*const` handed to
    // taffy `Dimension::calc()`; a plain Vec<T> would invalidate those pointers on realloc.
    #[allow(clippy::vec_box)]
    calc_storage: std::cell::RefCell<Vec<Box<CalcResolveContext>>>,
    /// Memoised `translate_style_to_taffy` results, keyed by DOM node id
    /// (`usize` = `NodeId::index`). Taffy calls
    /// `get_core_container_style` and `should_suppress_cross_intrinsic`
    /// many times per node during a single layout pass; each call
    /// triggers ~13 `cache.get_property` cascade walks for grid/flex
    /// props. Caching the built `Style` cuts this to one build per node.
    style_memo: std::cell::RefCell<HashMap<usize, Style>>,
}

impl<'a, 'b, T: ParsedFontTrait> TaffyBridge<'a, 'b, T> {
    fn new(
        ctx: &'a mut LayoutContext<'b, T>,
        tree: &'a mut LayoutTree,
        text_cache: *mut crate::font_traits::TextLayoutCache,
    ) -> Self {
        Self {
            ctx,
            tree,
            text_cache,
            calc_storage: std::cell::RefCell::new(Vec::new()),
            style_memo: std::cell::RefCell::new(HashMap::new()),
        }
    }

    /// Cache-backed wrapper for `translate_style_to_taffy`. Returns a
    /// clone of the memoised `Style` on cache hit, builds + inserts on
    /// miss. Keyed by DOM node index (not tree index) because the
    /// result depends only on the styled DOM, not on the transient
    /// layout tree.
    fn translate_style_to_taffy_cached(&self, dom_id: Option<NodeId>) -> Style {
        let Some(id) = dom_id else {
            return Style::default();
        };
        let key = id.index();
        if let Some(style) = self.style_memo.borrow().get(&key) {
            return style.clone();
        }
        let style = self.translate_style_to_taffy(dom_id);
        self.style_memo.borrow_mut().insert(key, style.clone());
        style
    }

    /// Translates CSS properties from the `StyledDom` into a `taffy::Style` struct.
    /// This is the core of the integration, mapping one style system to another.
    #[allow(clippy::field_reassign_with_default)] // struct built incrementally / test setup; a struct literal is not clearer here
    #[allow(clippy::too_many_lines)] // large but cohesive: single-purpose layout/render/parse routine (one branch per case)
    fn translate_style_to_taffy(&self, dom_id: Option<NodeId>) -> Style {
        let Some(id) = dom_id else {
            return Style::default();
        };
        let styled_dom = &self.ctx.styled_dom;
        let node_data = &styled_dom.node_data.as_ref()[id.index()];
        let node_state = &styled_dom.styled_nodes.as_container()[id].styled_node_state;
        let cache = &styled_dom.css_property_cache.ptr;
        let mut taffy_style = Style::default();

        // Box Sizing — CSS default is content-box, but Taffy defaults to border-box
        taffy_style.box_sizing = match get_css_box_sizing(styled_dom, id, node_state).unwrap_or_default() {
            azul_css::props::layout::LayoutBoxSizing::BorderBox => BoxSizing::BorderBox,
            azul_css::props::layout::LayoutBoxSizing::ContentBox => BoxSizing::ContentBox,
        };

        // Display Mode
        taffy_style.display =
            layout_display_to_taffy(CssPropertyValue::Exact(get_display_type(styled_dom, id)));

        // Position
        taffy_style.position =
            from_layout_position(get_position(styled_dom, id, node_state).unwrap_or_default());

        // Inset (top, left, bottom, right)
        taffy_style.inset = Rect {
            left: multi_value_to_lpa(get_css_left(styled_dom, id, node_state)),
            right: multi_value_to_lpa(get_css_right(styled_dom, id, node_state)),
            top: multi_value_to_lpa(get_css_top(styled_dom, id, node_state)),
            bottom: multi_value_to_lpa(get_css_bottom(styled_dom, id, node_state)),
        };

        // Size
        let width = get_css_width(self.ctx.styled_dom, id, node_state);
        let height = get_css_height(self.ctx.styled_dom, id, node_state);

        // Resolve node-local font sizes for calc() em/rem resolution
        let em_size = crate::solver3::getters::get_element_font_size(styled_dom, id, node_state);
        let rem_size = {
            let root_id = NodeId::new(0);
            let root_state = &styled_dom.styled_nodes.as_container()[root_id].styled_node_state;
            crate::solver3::getters::get_element_font_size(styled_dom, root_id, root_state)
        };

        let taffy_width = from_layout_width(width.unwrap_or_default(), &self.calc_storage, em_size, rem_size);
        let taffy_height = from_layout_height(height.unwrap_or_default(), &self.calc_storage, em_size, rem_size);

        taffy_style.size = Size {
            width: taffy_width,
            height: taffy_height,
        };

        // Overflow — CRITICAL for scroll containers.
        // Without this, Taffy's flexbox algorithm uses content size as automatic
        // minimum size, causing flex containers with overflow:auto/scroll to
        // expand to fit all content instead of clipping at the explicit size.
        // With overflow: Hidden/Scroll, Taffy sets automatic min size to 0 and
        // constrains the container.
        let overflow_x = get_overflow_x(styled_dom, id, node_state);
        let overflow_y = get_overflow_y(styled_dom, id, node_state);
        taffy_style.overflow = taffy::Point {
            x: azul_overflow_to_taffy(overflow_x),
            y: azul_overflow_to_taffy(overflow_y),
        };

        // Forward CSS aspect-ratio to taffy so flex/grid items honor it (and taffy's
        // transferred min-size suggestion works). AspectRatioValue stores width and
        // height ×1000, so the preferred ratio is simply width/height.
        #[allow(clippy::cast_precision_loss)] // small integer aspect-ratio components (e.g. 2000/1000)
        {
            taffy_style.aspect_ratio = match crate::solver3::getters::get_aspect_ratio_property(
                styled_dom, id, node_state,
            ) {
                MultiValue::Exact(azul_css::props::style::effects::StyleAspectRatio::Ratio(ar))
                    if ar.height != 0 =>
                {
                    Some(ar.width as f32 / ar.height as f32)
                }
                _ => None,
            };
        }

        // Min/Max Size
        // min-size:auto enables Taffy's auto minimum size algorithm which computes the
        // content size suggestion (min-content in main axis) and transferred size suggestion
        // (cross size converted through aspect ratio, if any). NOTE: aspect_ratio is not yet
        // forwarded to Taffy, so the transferred size suggestion path is incomplete.
        // NOTE: In CSS, the default min-width/min-height for flex items is `auto`
        // (which resolves to `min-content`), preventing them from shrinking below
        // their content size. We must map Auto to Dimension::Auto, NOT to 0px.
        let min_width_css = get_css_min_width(styled_dom, id, node_state);
        let min_height_css = get_css_min_height(styled_dom, id, node_state);

        taffy_style.min_size = Size {
            width: match min_width_css {
                MultiValue::Auto | MultiValue::Initial | MultiValue::Inherit => {
                    Dimension::auto()
                }
                MultiValue::Exact(v) => pixel_to_lp(v.inner).into(),
            },
            height: match min_height_css {
                MultiValue::Auto | MultiValue::Initial | MultiValue::Inherit => {
                    Dimension::auto()
                }
                MultiValue::Exact(v) => pixel_to_lp(v.inner).into(),
            },
        };

        // For max-size, we need to handle Auto specially - it should translate to Taffy's auto, not
        // a concrete value This is CRITICAL for flexbox stretch to work: items with
        // max-height: auto CAN be stretched
        let max_width_css = get_css_max_width(styled_dom, id, node_state);
        let max_height_css = get_css_max_height(styled_dom, id, node_state);

        taffy_style.max_size = Size {
            width: match max_width_css {
                MultiValue::Auto | MultiValue::Initial | MultiValue::Inherit => {
                    Dimension::auto()
                }
                MultiValue::Exact(v) => pixel_to_lp(v.inner).into(),
            },
            height: match max_height_css {
                MultiValue::Auto | MultiValue::Initial | MultiValue::Inherit => {
                    Dimension::auto()
                }
                MultiValue::Exact(v) => pixel_to_lp(v.inner).into(),
            },
        };

        // Box Model (margin, padding, border)
        let margin_left_css = get_css_margin_left(styled_dom, id, node_state);
        let margin_right_css = get_css_margin_right(styled_dom, id, node_state);
        let margin_top_css = get_css_margin_top(styled_dom, id, node_state);
        let margin_bottom_css = get_css_margin_bottom(styled_dom, id, node_state);

        taffy_style.margin = Rect {
            left: multi_value_to_lpa_margin(margin_left_css),
            right: multi_value_to_lpa_margin(margin_right_css),
            top: multi_value_to_lpa_margin(margin_top_css),
            bottom: multi_value_to_lpa_margin(margin_bottom_css),
        };

        taffy_style.padding = Rect {
            left: multi_value_to_lp(get_css_padding_left(styled_dom, id, node_state)),
            right: multi_value_to_lp(get_css_padding_right(styled_dom, id, node_state)),
            top: multi_value_to_lp(get_css_padding_top(styled_dom, id, node_state)),
            bottom: multi_value_to_lp(get_css_padding_bottom(styled_dom, id, node_state)),
        };

        taffy_style.border = Rect {
            left: multi_value_to_lp(get_css_border_left_width(styled_dom, id, node_state)),
            right: multi_value_to_lp(get_css_border_right_width(styled_dom, id, node_state)),
            top: multi_value_to_lp(get_css_border_top_width(styled_dom, id, node_state)),
            bottom: multi_value_to_lp(get_css_border_bottom_width(styled_dom, id, node_state)),
        };

        // Grid & gap properties — COMPACT FAST PATH: row_gap/column_gap are
        // i16 px × 10 in tier2_dims. The slow-path lookup would walk the
        // cascade for every node even though the answer is already encoded.
        taffy_style.gap = cache.compact_cache.as_ref().map_or_else(|| cache
                .get_property(node_data, &id, node_state, &CssPropertyType::Gap)
                .and_then(|p| if let CssProperty::Gap(v) = p { Some(v) } else { None })
                .map_or_else(Size::zero, |v| {
                    let val = v.get_property_or_default().unwrap_or_default().inner;
                    let gap_lp = pixel_to_lp(val);
                    Size { width: gap_lp, height: gap_lp }
                }), |cc| {
            let row = cc.tier2_dims[id.index()].row_gap;
            let col = cc.tier2_dims[id.index()].column_gap;
            let decode = |raw: i16| -> LengthPercentage {
                if raw >= azul_css::compact_cache::I16_SENTINEL_THRESHOLD {
                    LengthPercentage::length(0.0)
                } else {
                    LengthPercentage::length(f32::from(raw) / 10.0)
                }
            };
            Size {
                width: decode(col),
                height: decode(row),
            }
        });

        // Skip grid properties when not in a grid context.
        // Grid container props: only if this node has display:grid.
        // Grid item props: only if parent has display:grid.
        let (self_is_grid, parent_is_grid) = cache.compact_cache.as_ref().map_or((false, false), |cc| {
            #[allow(clippy::wildcard_imports)] // widget/render module pulls in the css property/value types it builds with
            use azul_css::compact_cache::*;
            let self_t1 = cc.tier1_enums[id.index()];
            let self_display = ((self_t1 >> DISPLAY_SHIFT) & DISPLAY_MASK) as u8;
            let grid_val = layout_display_to_u8(LayoutDisplay::Grid);
            let self_grid = self_display == grid_val;

            let parent_idx = styled_dom.node_hierarchy.as_ref()[id.index()].parent_id()
                .map_or(0, |p| p.index());
            let parent_t1 = cc.tier1_enums[parent_idx];
            let parent_display = ((parent_t1 >> DISPLAY_SHIFT) & DISPLAY_MASK) as u8;
            let parent_grid = parent_display == grid_val;
            (self_grid, parent_grid)
        });

        if self_is_grid {
        taffy_style.grid_template_rows = cache
            .get_property(
                node_data,
                &id,
                node_state,
                &CssPropertyType::GridTemplateRows,
            )
            .and_then(|p| {
                if let CssProperty::GridTemplateRows(v) = p {
                    Some(v.clone())
                } else {
                    None
                }
            })
            .map(grid_template_rows_to_taffy)
            .unwrap_or_default();

        // Grid template columns - convert GridTemplate to Vec<GridTemplateComponent>
        taffy_style.grid_template_columns = cache
            .get_property(
                node_data,
                &id,
                node_state,
                &CssPropertyType::GridTemplateColumns,
            )
            .and_then(|p| {
                if let CssProperty::GridTemplateColumns(v) = p {
                    Some(v.clone())
                } else {
                    None
                }
            })
            .map(grid_template_columns_to_taffy)
            .unwrap_or_default();

        // Grid template areas - convert GridTemplateAreas to Vec<taffy::GridTemplateArea<String>>
        taffy_style.grid_template_areas = cache
            .get_property(
                node_data,
                &id,
                node_state,
                &CssPropertyType::GridTemplateAreas,
            )
            .and_then(|p| {
                if let CssProperty::GridTemplateAreas(v) = p {
                    v.get_property().cloned()
                } else {
                    None
                }
            })
            .map(|areas| {
                areas
                    .areas
                    .as_ref()
                    .iter()
                    .map(|a| taffy::GridTemplateArea {
                        name: a.name.as_str().to_string(),
                        row_start: a.row_start,
                        row_end: a.row_end,
                        column_start: a.column_start,
                        column_end: a.column_end,
                    })
                    .collect::<Vec<_>>()
            })
            .unwrap_or_default();

        taffy_style.grid_auto_rows = cache
            .get_property(node_data, &id, node_state, &CssPropertyType::GridAutoRows)
            .and_then(|p| {
                if let CssProperty::GridAutoRows(v) = p {
                    Some(v.clone())
                } else {
                    None
                }
            })
            .map(grid_auto_rows_to_taffy)
            .unwrap_or_default();

        taffy_style.grid_auto_columns = cache
            .get_property(
                node_data,
                &id,
                node_state,
                &CssPropertyType::GridAutoColumns,
            )
            .and_then(|p| {
                if let CssProperty::GridAutoColumns(v) = p {
                    Some(v.clone())
                } else {
                    None
                }
            })
            .map(grid_auto_columns_to_taffy)
            .unwrap_or_default();

        taffy_style.grid_auto_flow = cache.compact_cache.as_ref().map_or_else(|| cache
                .get_property(node_data, &id, node_state, &CssPropertyType::GridAutoFlow)
                .and_then(|p| if let CssProperty::GridAutoFlow(v) = p { Some(*v) } else { None })
                .map(grid_auto_flow_to_taffy)
                .unwrap_or_default(), |cc| {
            #[allow(clippy::wildcard_imports)] // widget/render module pulls in the css property/value types it builds with
            use azul_css::compact_cache::*;
            let bits = ((cc.tier1_enums[id.index()] >> GRID_AUTO_FLOW_SHIFT) & GRID_AUTO_FLOW_MASK) as u8;
            let val = layout_grid_auto_flow_from_u8(bits);
            grid_auto_flow_to_taffy(CssPropertyValue::Exact(val))
        });

        } // end if self_is_grid

        if parent_is_grid {
        // Grid item placement — read from compact cold cache (Auto/Line/Span)
        if let Some(cc) = cache.compact_cache.as_ref() {
            let cs = cc.tier2_cold[id.index()].grid_col_start;
            let ce = cc.tier2_cold[id.index()].grid_col_end;
            if cs != azul_css::compact_cache::I16_AUTO || ce != azul_css::compact_cache::I16_AUTO {
                taffy_style.grid_column = Line { start: decode_compact_grid_line(cs), end: decode_compact_grid_line(ce) };
            }
            let rs = cc.tier2_cold[id.index()].grid_row_start;
            let re = cc.tier2_cold[id.index()].grid_row_end;
            if rs != azul_css::compact_cache::I16_AUTO || re != azul_css::compact_cache::I16_AUTO {
                taffy_style.grid_row = Line { start: decode_compact_grid_line(rs), end: decode_compact_grid_line(re) };
            }
        } else {
            if let Some(grid_col) = cache
                .get_property(node_data, &id, node_state, &CssPropertyType::GridColumn)
                .and_then(|p| if let CssProperty::GridColumn(v) = p { v.get_property().cloned() } else { None })
            { taffy_style.grid_column = grid_placement_to_taffy(&grid_col); }
            if let Some(grid_row) = cache
                .get_property(node_data, &id, node_state, &CssPropertyType::GridRow)
                .and_then(|p| if let CssProperty::GridRow(v) = p { v.get_property().cloned() } else { None })
            { taffy_style.grid_row = grid_placement_to_taffy(&grid_row); }
        }
        } // end if parent_is_grid

        // Flexbox
        taffy_style.flex_direction = match get_flex_direction(styled_dom, id, node_state) {
            MultiValue::Exact(v) => layout_flex_direction_to_taffy(CssPropertyValue::Exact(v)),
            _ => FlexDirection::Row,
        };
        // COMPACT FAST PATH: flex_wrap is Tier 1 enum
        taffy_style.flex_wrap = {
            let compact = if node_state.is_normal() {
                cache.compact_cache.as_ref().map(|cc| {
                    layout_flex_wrap_to_taffy(CssPropertyValue::Exact(cc.get_flex_wrap(id.index())))
                })
            } else {
                None
            };
            compact.unwrap_or_else(|| {
                cache
                    .get_property(node_data, &id, node_state, &CssPropertyType::FlexWrap)
                    .and_then(|p| if let CssProperty::FlexWrap(v) = p { Some(*v) } else { None })
                    .map_or(FlexWrap::NoWrap, layout_flex_wrap_to_taffy)
            })
        };
        taffy_style.align_items = match get_align_items(styled_dom, id, node_state) {
            MultiValue::Exact(v) => Some(layout_align_items_to_taffy(CssPropertyValue::Exact(v))),
            _ => None,
        };
                // CSS spec: default align-items is "normal" which acts like "stretch"
                // for non-replaced grid/flex items. Taffy handles this internally when
                // align_items is None, so we should NOT force a default here.
        taffy_style.justify_items = cache.compact_cache.as_ref().map_or_else(|| cache
                .get_property(node_data, &id, node_state, &CssPropertyType::JustifyItems)
                .and_then(|p| if let CssProperty::JustifyItems(v) = p { Some(*v) } else { None })
                .map(layout_justify_items_to_taffy), |cc| {
            #[allow(clippy::wildcard_imports)] // widget/render module pulls in the css property/value types it builds with
            use azul_css::compact_cache::*;
            use azul_css::props::layout::grid::LayoutJustifyItems;
            let bits = ((cc.tier1_enums[id.index()] >> JUSTIFY_ITEMS_SHIFT) & JUSTIFY_ITEMS_MASK) as u8;
            let val = layout_justify_items_from_u8(bits);
            Some(match val {
                LayoutJustifyItems::Start => AlignItems::Start,
                LayoutJustifyItems::End => AlignItems::End,
                LayoutJustifyItems::Center => AlignItems::Center,
                LayoutJustifyItems::Stretch => AlignItems::Stretch,
            })
        });
        // COMPACT FAST PATH: justify-content is in tier1 bits 21-23.
        taffy_style.justify_content = cache.compact_cache.as_ref().map_or_else(|| cache
                .get_property(node_data, &id, node_state, &CssPropertyType::JustifyContent)
                .and_then(|p| if let CssProperty::JustifyContent(v) = p { Some(v) } else { None })
                .map(|v| layout_justify_content_to_taffy(*v)), |cc| {
            #[allow(clippy::wildcard_imports)] // widget/render module pulls in the css property/value types it builds with
            use azul_css::compact_cache::*;
            use azul_css::props::layout::LayoutJustifyContent;
            let bits = ((cc.tier1_enums[id.index()] >> JUSTIFY_CONTENT_SHIFT) & JUSTIFY_MASK) as u8;
            Some(match layout_justify_content_from_u8(bits) {
                LayoutJustifyContent::FlexStart => JustifyContent::FlexStart,
                LayoutJustifyContent::FlexEnd => JustifyContent::FlexEnd,
                LayoutJustifyContent::Start => JustifyContent::Start,
                LayoutJustifyContent::End => JustifyContent::End,
                LayoutJustifyContent::Center => JustifyContent::Center,
                LayoutJustifyContent::SpaceBetween => JustifyContent::SpaceBetween,
                LayoutJustifyContent::SpaceAround => JustifyContent::SpaceAround,
                LayoutJustifyContent::SpaceEvenly => JustifyContent::SpaceEvenly,
            })
        });
                // CSS spec: default justify-content is "normal". Taffy handles
                // this internally when justify_content is None.
        // COMPACT FAST PATH: flex_grow stored as u16 × 100
        taffy_style.flex_grow = {
            let compact = if node_state.is_normal() {
                cache.compact_cache.as_ref().and_then(|cc| cc.get_flex_grow(id.index()))
            } else {
                None
            };
            compact.unwrap_or_else(|| {
                cache
                    .get_property(node_data, &id, node_state, &CssPropertyType::FlexGrow)
                    .and_then(|p| if let CssProperty::FlexGrow(v) = p {
                        Some(v.get_property_or_default().unwrap_or_default().inner.get())
                    } else { None })
                    .unwrap_or(0.0)
            })
        };

        // COMPACT FAST PATH: flex_shrink stored as u16 × 100
        taffy_style.flex_shrink = {
            let compact = if node_state.is_normal() {
                cache.compact_cache.as_ref().and_then(|cc| cc.get_flex_shrink(id.index()))
            } else {
                None
            };
            compact.unwrap_or_else(|| {
                cache
                    .get_property(node_data, &id, node_state, &CssPropertyType::FlexShrink)
                    .and_then(|p| if let CssProperty::FlexShrink(v) = p {
                        Some(v.get_property_or_default().unwrap_or_default().inner.get())
                    } else { None })
                    .unwrap_or(1.0)
            })
        };
        // COMPACT FAST PATH: flex_basis stored as u32 with PixelValue encoding
        taffy_style.flex_basis = {
            let compact = if node_state.is_normal() {
                cache.compact_cache.as_ref().and_then(|cc| {
                    let raw = cc.get_flex_basis_raw(id.index());
                    match raw {
                        azul_css::compact_cache::U32_AUTO
                        | azul_css::compact_cache::U32_NONE
                        | azul_css::compact_cache::U32_INITIAL => Some(Dimension::auto()),
                        azul_css::compact_cache::U32_SENTINEL
                        | azul_css::compact_cache::U32_INHERIT => None,
                        _ => {
                            if let Some(pv) = azul_css::compact_cache::decode_pixel_value_u32(raw) {
                                let basis = pixel_value_to_pixels_fallback(&pv)
                                    .map(Dimension::length)
                                    .or_else(|| pv.to_percent().map(|p| Dimension::percent(p.get())))
                                    .unwrap_or_else(Dimension::auto);
                                if !matches!(basis, auto if auto == Dimension::auto()) {
                                    taffy_style.size.width = Dimension::auto();
                                }
                                Some(basis)
                            } else {
                                Some(Dimension::auto())
                            }
                        }
                    }
                })
            } else {
                None
            };
            compact.unwrap_or_else(|| {
                flex_basis_slow_path(cache, node_data, &id, node_state, &mut taffy_style)
            })
        };
        taffy_style.align_self = cache.compact_cache.as_ref().map_or_else(|| cache
                .get_property(node_data, &id, node_state, &CssPropertyType::AlignSelf)
                .and_then(|p| if let CssProperty::AlignSelf(v) = p { layout_align_self_to_taffy(*v) } else { None }), |cc| {
            #[allow(clippy::wildcard_imports)] // widget/render module pulls in the css property/value types it builds with
            use azul_css::compact_cache::*;
            let bits = ((cc.tier1_enums[id.index()] >> ALIGN_SELF_SHIFT) & ALIGN_SELF_MASK) as u8;
            let val = layout_align_self_from_u8(bits);
            match val {
                LayoutAlignSelf::Auto => None,
                LayoutAlignSelf::Start => Some(AlignSelf::FlexStart),
                LayoutAlignSelf::End => Some(AlignSelf::FlexEnd),
                LayoutAlignSelf::Center => Some(AlignSelf::Center),
                LayoutAlignSelf::Baseline => Some(AlignSelf::Baseline),
                LayoutAlignSelf::Stretch => Some(AlignSelf::Stretch),
            }
        });
        taffy_style.justify_self = cache.compact_cache.as_ref().map_or_else(|| cache
                .get_property(node_data, &id, node_state, &CssPropertyType::JustifySelf)
                .and_then(|p| if let CssProperty::JustifySelf(v) = p {
                    use azul_css::props::layout::grid::LayoutJustifySelf;
                    match v.get_property_or_default().unwrap_or_default() {
                        LayoutJustifySelf::Auto => None,
                        LayoutJustifySelf::Start => Some(AlignSelf::Start),
                        LayoutJustifySelf::End => Some(AlignSelf::End),
                        LayoutJustifySelf::Center => Some(AlignSelf::Center),
                        LayoutJustifySelf::Stretch => Some(AlignSelf::Stretch),
                    }
                } else { None }), |cc| {
            #[allow(clippy::wildcard_imports)] // widget/render module pulls in the css property/value types it builds with
            use azul_css::compact_cache::*;
            use azul_css::props::layout::grid::LayoutJustifySelf;
            let bits = ((cc.tier1_enums[id.index()] >> JUSTIFY_SELF_SHIFT) & JUSTIFY_SELF_MASK) as u8;
            let val = layout_justify_self_from_u8(bits);
            match val {
                LayoutJustifySelf::Auto => None,
                LayoutJustifySelf::Start => Some(AlignSelf::Start),
                LayoutJustifySelf::End => Some(AlignSelf::End),
                LayoutJustifySelf::Center => Some(AlignSelf::Center),
                LayoutJustifySelf::Stretch => Some(AlignSelf::Stretch),
            }
        });
        taffy_style.align_content = match get_align_content(styled_dom, id, node_state) {
            MultiValue::Exact(v) => Some(layout_align_content_to_taffy(CssPropertyValue::Exact(v))),
            _ => None,
        };
                // CSS spec: default align-content is "normal". Taffy handles
                // this internally when align_content is None.

        taffy_style
    }

    /// Gets or computes the Taffy style for a given node index.
    fn get_taffy_style(&self, node_idx: usize) -> Style {
        let dom_id = self.tree.get(node_idx).and_then(|n| n.dom_node_id);
        let mut style = self.translate_style_to_taffy_cached(dom_id);
        
        // CSS 2.1 § 10.3.3: Root element margin handling for Flex/Grid.
        //
        // The root element's margin is already resolved and subtracted from
        // available_size by calculate_used_size_for_node() (sizing.rs). The
        // resulting margin-adjusted size is passed to Taffy as known_dimensions.
        //
        // Taffy's layout algorithm reads margin from the style and subtracts it
        // from known_dimensions internally. If we also pass the margin through
        // the style, it gets subtracted twice:
        //   1. calculate_used_size_for_node: viewport - margin → available_size
        //   2. Taffy: known_dimensions - style.margin → content_area
        //
        // Zeroing the style margin for root nodes prevents this double-subtraction.
        // This is NOT a hack — it's the correct integration point between Azul's
        // BFC-level sizing and Taffy's Flex/Grid algorithm.
        let is_root = self.tree.get(node_idx).is_some_and(|n| n.parent.is_none());
        if is_root {
            style.margin = Rect::zero();
        }
        
        // FIX: Apply cross-axis intrinsic size suppression for stretch alignment.
        // This enables align-self: stretch to work correctly by ensuring Taffy
        // sees the cross-axis size as Auto (allowing stretch) rather than a definite value.
        let (suppress_width, suppress_height) = self.should_suppress_cross_intrinsic(node_idx, &style);

        if suppress_width {
            // Force width to Auto and set min-width to 0 to allow stretching.
            // Taffy treats Auto size + Stretch alignment as a signal to fill the container.
            style.size.width = Dimension::auto(); 
            style.min_size.width = Dimension::length(0.0);
        }

        if suppress_height {
            style.size.height = Dimension::auto();
            style.min_size.height = Dimension::length(0.0);
        }

        style
    }

    /// Determines if cross-axis intrinsic size should be suppressed for stretching.
    ///
    /// Per CSS Flexbox spec, align-items: stretch makes items fill the cross-axis
    /// ONLY if the item's cross-size is 'auto' AND the item has no intrinsic cross-size.
    ///
    /// Returns (`suppress_width`, `suppress_height`) booleans.
    #[allow(clippy::match_same_arms)] // enum/value mapping/dispatch table: one arm per input variant (or cross-type bindings that can't merge)
    fn should_suppress_cross_intrinsic(&self, node_idx: usize, style: &Style) -> (bool, bool) {
        let Some(node) = self.tree.get(node_idx) else {
            return (false, false);
        };

        // Check if parent is a flex or grid container
        let Some(parent_fc) = self.tree.warm(node_idx).and_then(|w| w.parent_formatting_context) else {
            return (false, false);
        };

        match parent_fc {
            FormattingContext::Flex => {
                // Get parent node to check its flex-direction and align-items
                let Some(parent_idx) = node.parent else {
                    return (false, false);
                };
                let parent_dom_id = self.tree.get(parent_idx).and_then(|n| n.dom_node_id);
                let parent_style = self.translate_style_to_taffy_cached(parent_dom_id);

                // Determine if flex container is row or column
                let is_row = matches!(
                    parent_style.flex_direction,
                    FlexDirection::Row | FlexDirection::RowReverse
                );

                // Get effective align value for this item
                // align-self overrides parent's align-items
                let align = style
                    .align_self
                    .or(parent_style.align_items)
                    .unwrap_or(AlignSelf::Stretch);

                let should_stretch = matches!(align, AlignSelf::Stretch);

                if !should_stretch {
                    return (false, false);
                }

                // Check if cross-axis size is auto
                // For row flex: cross-axis is height
                // For column flex: cross-axis is width
                let cross_size_is_auto = if is_row {
                    style.size.height == Dimension::auto()
                } else {
                    style.size.width == Dimension::auto()
                };

                if !cross_size_is_auto {
                    return (false, false);
                }

                // All conditions met: suppress intrinsic cross-size
                if is_row {
                    (false, true) // Suppress height for row flex
                } else {
                    (true, false) // Suppress width for column flex
                }
            }
            FormattingContext::Grid => {
                // TODO: Implement grid stretch detection
                // Grid is more complex because:
                // 1. Default align-items is 'start', not 'stretch'
                // 2. Items can stretch in both axes simultaneously
                // 3. Need to check grid-auto-flow and track sizing
                (false, false)
            }
            _ => (false, false),
        }
    }

    /// Helper to get children that participate in layout (i.e., not `display: none`).
    fn get_layout_children(&self, node_idx: usize) -> Vec<usize> {
        use crate::solver3::getters::{get_display_property, MultiValue};
        let Some(node) = self.tree.get(node_idx) else {
            return Vec::new();
        };

        self.tree.children(node_idx)
            .iter()
            .filter(|&&child_idx| {
                let Some(child_node) = self.tree.get(child_idx) else {
                    return false;
                };
                let Some(child_dom_id) = child_node.dom_node_id else {
                    return true;
                };

                // Check if child has display: none
                let display = get_display_property(self.ctx.styled_dom, Some(child_dom_id));
                let is_display_none = matches!(display, MultiValue::Exact(LayoutDisplay::None));

                !is_display_none
            })
            .copied()
            .collect()
    }
}

/// Main entry point for laying out a Flexbox or Grid container using Taffy.
///
/// This function now accepts a `text_cache` parameter so that IFC layout can be
/// performed inline during Taffy's measure callbacks, rather than as a post-processing step.
/// # Panics
///
/// Panics if `node_idx` is not present in the layout tree.
pub fn layout_taffy_subtree<T: ParsedFontTrait>(
    ctx: &mut LayoutContext<'_, T>,
    tree: &mut LayoutTree,
    text_cache: &mut crate::font_traits::TextLayoutCache,
    node_idx: usize,
    inputs: LayoutInput,
) -> LayoutOutput {
    let children: Vec<usize> = tree.children(node_idx).to_vec();

    // DEBUG: Log Taffy inputs
    if ctx.debug_messages.is_some() {
        ctx.debug_info_inner(format!(
            "[TAFFY INPUT] node_idx={} known_dims=({:?}, {:?}) available=({:?}, {:?}) \
             parent_size=({:?}, {:?}) children={:?}",
            node_idx,
            inputs.known_dimensions.width,
            inputs.known_dimensions.height,
            inputs.available_space.width,
            inputs.available_space.height,
            inputs.parent_size.width,
            inputs.parent_size.height,
            children
        ));
    }

    // Clear cache to force re-measure
    for &child_idx in &children {
        if let Some(child) = tree.warm_mut(child_idx) {
            child.taffy_cache.clear();
        }
    }

    // SAFETY: We pass text_cache as a raw pointer because TaffyBridge needs to call
    // layout_ifc from within compute_child_layout, but we already have &mut ctx and &mut tree.
    // The pointer is only valid for the duration of this function call.
    let text_cache_ptr = core::ptr::from_mut::<crate::font_traits::TextLayoutCache>(text_cache);

    let mut bridge = TaffyBridge::new(ctx, tree, text_cache_ptr);
    let node = bridge.tree.get(node_idx).unwrap();

    let output = match node.formatting_context {
        FormattingContext::Flex => compute_flexbox_layout(&mut bridge, node_idx.into(), inputs),
        FormattingContext::Grid => compute_grid_layout(&mut bridge, node_idx.into(), inputs),
        _ => LayoutOutput::HIDDEN,
    };

    // DEBUG: Log Taffy output
    if bridge.ctx.debug_messages.is_some() {
        bridge.ctx.debug_info_inner(format!(
            "[TAFFY OUTPUT] node_idx={} output_size=({:?}, {:?})",
            node_idx, output.size.width, output.size.height
        ));

        // Log child layout results
        for &child_idx in &children {
            if let Some(child) = bridge.tree.get(child_idx) {
                bridge.ctx.debug_info_inner(format!(
                    "[TAFFY CHILD RESULT] child_idx={} used_size={:?} relative_pos={:?}",
                    child_idx, child.used_size, bridge.tree.warm(child_idx).and_then(|w| w.relative_position)
                ));
            }
        }
    }

    output
}

// --- Trait Implementations for the Bridge ---

impl<T: ParsedFontTrait> TraversePartialTree for TaffyBridge<'_, '_, T> {
    type ChildIter<'c>
        = std::vec::IntoIter<taffy::NodeId>
    where
        Self: 'c;

    fn child_ids(&self, node_id: taffy::NodeId) -> Self::ChildIter<'_> {
        let node_idx: usize = node_id.into();
        let children = self.get_layout_children(node_idx);
        children
            .into_iter()
            .map(Into::into)
            .collect::<Vec<taffy::NodeId>>()
            .into_iter()
    }

    fn child_count(&self, node_id: taffy::NodeId) -> usize {
        let node_idx: usize = node_id.into();
        
        self.get_layout_children(node_idx).len()
    }

    fn get_child_id(&self, node_id: taffy::NodeId, index: usize) -> taffy::NodeId {
        self.get_layout_children(node_id.into())[index].into()
    }
}

impl<T: ParsedFontTrait> LayoutPartialTree for TaffyBridge<'_, '_, T> {
    type CoreContainerStyle<'c>
        = Style
    where
        Self: 'c;
    type CustomIdent = String;

    fn get_core_container_style(&self, node_id: taffy::NodeId) -> Self::CoreContainerStyle<'_> {
        let node_idx: usize = node_id.into();
        // Use get_taffy_style instead of translate_style_to_taffy to apply
        // cross-axis intrinsic suppression for stretch alignment
        self.get_taffy_style(node_idx)
    }

    fn set_unrounded_layout(&mut self, node_id: taffy::NodeId, layout: &Layout) {
        let node_idx: usize = node_id.into();

        // FIX: Retrieve parent border/padding to adjust position.
        // Taffy positions are relative to the parent's Border Box origin.
        // Azul expects positions relative to the parent's Content Box origin.
        // We must subtract the parent's border and padding from the Taffy-returned position.
        let (parent_border_left, parent_border_top, parent_padding_left, parent_padding_top) = {
            if let Some(child) = self.tree.get(node_idx) {
                if let Some(parent_idx) = child.parent {
                    self.tree.get(parent_idx).map_or((0.0, 0.0, 0.0, 0.0), |parent| {
                        let pbp = parent.box_props.unpack();
                        (
                            pbp.border.left,
                            pbp.border.top,
                            pbp.padding.left,
                            pbp.padding.top,
                        )
                    })
                } else {
                    (0.0, 0.0, 0.0, 0.0)
                }
            } else {
                (0.0, 0.0, 0.0, 0.0)
            }
        };

        if let Some(node) = self.tree.get_mut(node_idx) {
            let size = translate_taffy_size_back(layout.size);
            let mut pos = translate_taffy_point_back(layout.location);

            // DEBUG: Log Taffy's raw layout result before adjustment
            if self.ctx.debug_messages.is_some() {
                self.ctx.debug_info_inner(format!(
                    "[TAFFY set_unrounded_layout] node_idx={} taffy_size=({:.2}, {:.2}) \
                     taffy_pos=({:.2}, {:.2}) parent_border=({:.2}, {:.2}) parent_padding=({:.2}, \
                     {:.2})",
                    node_idx,
                    layout.size.width,
                    layout.size.height,
                    layout.location.x,
                    layout.location.y,
                    parent_border_left,
                    parent_border_top,
                    parent_padding_left,
                    parent_padding_top
                ));
            }

            // Subtract parent's border and padding offset to convert
            // from border-box-relative to content-box-relative position
            pos.x -= parent_border_left + parent_padding_left;
            pos.y -= parent_border_top + parent_padding_top;

            node.used_size = Some(size);
        }
        if let Some(warm) = self.tree.warm_mut(node_idx) {
            let mut pos = translate_taffy_point_back(layout.location);
            pos.x -= parent_border_left + parent_padding_left;
            pos.y -= parent_border_top + parent_padding_top;
            warm.relative_position = Some(pos);
        }
    }

    fn resolve_calc_value(&self, val: *const (), basis: f32) -> f32 {
        // SAFETY: `val` came from `store_calc_and_make_dimension` which stored
        // a `Box<CalcResolveContext>` in `self.calc_storage`. The Box is alive for
        // the lifetime of this TaffyBridge, and taffy only clears the low 3 bits.
        let ctx = unsafe { &*val.cast::<CalcResolveContext>() };
        crate::solver3::calc::evaluate_calc(ctx, basis)
    }

    fn compute_child_layout(
        &mut self,
        node_id: taffy::NodeId,
        inputs: LayoutInput,
    ) -> LayoutOutput {
        let node_idx: usize = node_id.into();

        // DEBUG: Log the style being used for this child
        if self.ctx.debug_messages.is_some() {
            let style = self.get_taffy_style(node_idx);
            self.ctx.debug_info_inner(format!(
                "[TAFFY compute_child_layout] node_idx={} flex_grow={} flex_shrink={} \
                 flex_basis={:?} size=({:?}, {:?}) inputs.known_dims=({:?}, {:?})",
                node_idx,
                style.flex_grow,
                style.flex_shrink,
                style.flex_basis,
                style.size.width,
                style.size.height,
                inputs.known_dimensions.width,
                inputs.known_dimensions.height
            ));
        }

        // Get formatting context
        let fc = self
            .tree
            .get(node_idx)
            .map(|s| s.formatting_context)
            .unwrap_or_default();

        let mut result = compute_cached_layout(self, node_id, inputs, |tree, node_id, inputs| {
            let node_idx: usize = node_id.into();
            let fc = tree
                .tree
                .get(node_idx)
                .map(|s| s.formatting_context)
                .unwrap_or_default();

            match fc {
                FormattingContext::Flex => compute_flexbox_layout(tree, node_id, inputs),
                FormattingContext::Grid => compute_grid_layout(tree, node_id, inputs),
                // For Block, Inline, Table, InlineBlock - delegate to layout_formatting_context
                // This ensures proper recursive layout of all formatting contexts
                _ => tree.compute_non_flex_layout(node_idx, inputs),
            }
        });

        // Store layout for container nodes - Taffy only calls set_unrounded_layout for leaf nodes
        if let Some(node) = self.tree.get_mut(node_idx) {
            let size = translate_taffy_size_back(result.size);
            node.used_size = Some(size);
        }

        // CRITICAL FIX: For Flex/Grid children with overflow:auto/scroll,
        // compute scrollbar_info by comparing Taffy's content_size against the
        // CSS-specified container size.
        //
        // We skip when content_size is (0,0) because that's the sizing pass
        // where Taffy hasn't determined actual content size yet. The final
        // layout pass always has non-zero content_size for nodes that need
        // scroll. This avoids 2/3 of the compute_taffy_scrollbar_info calls
        // (one sizing pass per axis) while still getting correct final values.
        if matches!(fc, FormattingContext::Flex | FormattingContext::Grid) {
            let taffy_content_width = result.content_size.width;
            let taffy_content_height = result.content_size.height;

            // Skip on sizing pass where content_size is still zero:
            // scrollbar_info computed from zero content would be wrong anyway.
            if taffy_content_width <= 0.0 && taffy_content_height <= 0.0 {
                return result;
            }

            let (scrollbar_info, eff_content_w, eff_content_h) =
                compute_taffy_scrollbar_info(
                    self.ctx,
                    self.tree,
                    node_idx,
                    result.size.width,
                    result.size.height,
                    taffy_content_width,
                    taffy_content_height,
                );

            if let Some(warm) = self.tree.warm_mut(node_idx) {
                warm.scrollbar_info = Some(scrollbar_info);
                // eff_content_w/h are already in content-box coordinates
                // (border.left/top subtracted in compute_taffy_scrollbar_info),
                // so store directly without further subtraction.
                warm.overflow_content_size = Some(LogicalSize::new(
                    eff_content_w,
                    eff_content_h,
                ));
            }
        }

        result
    }
}

impl<T: ParsedFontTrait> TaffyBridge<'_, '_, T> {
    /// Compute layout for non-flex/grid nodes by delegating to `layout_formatting_context`.
    /// This handles Block, Inline, Table, `InlineBlock` formatting contexts recursively.
    #[allow(clippy::match_same_arms)] // enum/value mapping/dispatch table: one arm per input variant (or cross-type bindings that can't merge)
    #[allow(clippy::too_many_lines)] // large but cohesive: single-purpose layout/render/parse routine (one branch per case)
    fn compute_non_flex_layout(&mut self, node_idx: usize, inputs: LayoutInput) -> LayoutOutput {
        // Taffy's known_dimensions are BORDER-BOX sizes (the child's outer size
        // as determined by the parent flex/grid algorithm, e.g. via stretch alignment).
        // Our BFC/IFC layout expects the available_size to be the CONTENT-BOX width
        // (i.e. the space available for the child's own content, excluding the child's
        // own padding and border).
        //
        // Get padding/border early so we can convert border-box → content-box.
        let (node_padding_width, node_padding_height, node_border_width, node_border_height) = self
            .tree
            .get(node_idx)
            .map_or((0.0, 0.0, 0.0, 0.0), |node| {
                let bp = node.box_props.unpack();
                (
                    bp.padding.left + bp.padding.right,
                    bp.padding.top + bp.padding.bottom,
                    bp.border.left + bp.border.right,
                    bp.border.top + bp.border.bottom,
                )
            });

        // Determine available size from Taffy's inputs.
        // When known_dimensions is set (e.g. flex stretch), subtract the child's own
        // padding+border to convert from border-box to content-box available space.
        // For MinContent/MaxContent, use INFINITY and let the text layout calculate
        // its actual intrinsic width.
        let available_width = inputs
            .known_dimensions
            .width
            .map(|kw| (kw - node_padding_width - node_border_width).max(0.0))
            .or(match inputs.available_space.width {
                AvailableSpace::Definite(w) => Some(w),
                AvailableSpace::MinContent => None, // Use infinity, return intrinsic min-content
                AvailableSpace::MaxContent => None, // Use infinity for max-content
            })
            .unwrap_or(f32::INFINITY);

        let available_height = inputs
            .known_dimensions
            .height
            .map(|kh| (kh - node_padding_height - node_border_height).max(0.0))
            .or(match inputs.available_space.height {
                AvailableSpace::Definite(h) => Some(h),
                AvailableSpace::MinContent => None, // Use infinity, return intrinsic min-content
                AvailableSpace::MaxContent => None,
            })
            .unwrap_or(f32::INFINITY);

        let mut available_size = LogicalSize {
            width: available_width,
            height: available_height,
        };

        // NOTE: Scrollbar reservation is handled inside layout_bfc() where it subtracts
        // scrollbar width from children_containing_block_size. We do NOT subtract here
        // to avoid double-subtraction when compute_non_flex_layout delegates to
        // layout_formatting_context → layout_bfc.

        // Convert Taffy's AvailableSpace to our Text3AvailableSpace for caching.
        // When the child has known_dimensions.width (from flex/grid layout), use that
        // instead of the parent's available_space — otherwise text centers/wraps in
        // the wrong width (e.g., 404px parent instead of 120px child).
        let available_width_type = if inputs.known_dimensions.width.is_some() {
            crate::text3::cache::AvailableSpace::Definite(available_width)
        } else {
            match inputs.available_space.width {
                AvailableSpace::Definite(w) => crate::text3::cache::AvailableSpace::Definite(w),
                AvailableSpace::MinContent => crate::text3::cache::AvailableSpace::MinContent,
                AvailableSpace::MaxContent => crate::text3::cache::AvailableSpace::MaxContent,
            }
        };

        // Get text-align from CSS for this node (important for centering content in flex items)
        let text_align = self
            .tree
            .get(node_idx)
            .and_then(|node| node.dom_node_id)
            .map(|dom_id| {
                let node_state =
                    &self.ctx.styled_dom.styled_nodes.as_container()[dom_id].styled_node_state;
                crate::solver3::getters::get_text_align(self.ctx.styled_dom, dom_id, node_state)
                    .unwrap_or_default()
            })
            .unwrap_or_default();

        // Convert CSS text-align to our internal TextAlign enum
        let fc_text_align = match text_align {
            azul_css::props::style::StyleTextAlign::Left => FcTextAlign::Start,
            azul_css::props::style::StyleTextAlign::Right => FcTextAlign::End,
            azul_css::props::style::StyleTextAlign::Center => FcTextAlign::Center,
            azul_css::props::style::StyleTextAlign::Justify => FcTextAlign::Justify,
            azul_css::props::style::StyleTextAlign::Start => FcTextAlign::Start,
            azul_css::props::style::StyleTextAlign::End => FcTextAlign::End,
        };

        // SAFETY: `self.text_cache` was derived from `&mut TextLayoutCache` in
        // `layout_taffy_subtree` and no other reference to it exists at this point.
        // The raw pointer is necessary because we already hold `&mut self` (which
        // borrows `ctx` and `tree`), and Rust's borrow checker cannot express the
        // disjointness of text_cache from ctx/tree.
        let text_cache = unsafe { &mut *self.text_cache };

        let constraints = LayoutConstraints {
            available_size,
            writing_mode: LayoutWritingMode::HorizontalTb,
            writing_mode_ctx: super::geometry::WritingModeContext::default(),
            bfc_state: None,
            text_align: fc_text_align,
            containing_block_size: available_size,
            available_width_type,
        };

        // A prior Taffy measurement pass (e.g. the min-content pass Taffy runs to
        // find a flex item's intrinsic width) stores its result in `node.used_size`
        // at the end of this function. layout_bfc then reads `used_size` as the
        // children's containing-block width. When the subsequent definite-width
        // cross-sizing pass re-enters here, that STALE min-content width (not this
        // pass's `known_dimensions.width`) drives child wrapping — so a flex item
        // with long text wraps at min-content and reports an over-tall cross size,
        // over-sizing the container (invoice `.head` measured 125px for ~45px of
        // content). Reset `used_size` to the border-box dims Taffy fixed for THIS
        // measure. When width is unknown (an intrinsic pass), clear it so layout_bfc
        // falls back to `constraints.available_size` (INFINITY → true intrinsic).
        if let Some(n) = self.tree.get_mut(node_idx) {
            n.used_size = inputs.known_dimensions.width.map(|w| LogicalSize {
                width: w,
                height: inputs.known_dimensions.height.unwrap_or_else(|| {
                    if available_height.is_finite() {
                        available_height + node_padding_height + node_border_height
                    } else {
                        0.0
                    }
                }),
            });
        }

        // Use a temporary float cache for this subtree
        let mut float_cache = HashMap::new();

        // Call layout_formatting_context - this handles ALL formatting context types
        // including nested flex/grid, tables, BFC, and IFC
        let fc_result = crate::solver3::fc::layout_formatting_context(
            self.ctx,
            self.tree,
            text_cache,
            node_idx,
            &constraints,
            &mut float_cache,
        );

        match fc_result {
            Ok(bfc_result) => {
                let output = bfc_result.output;
                let content_width = output.overflow_size.width;
                let content_height = output.overflow_size.height;

                // Padding/border already computed at start of function
                let padding_width = node_padding_width;
                let padding_height = node_padding_height;
                let border_width = node_border_width;
                let border_height = node_border_height;

                // Get intrinsic sizes for min/max-content queries
                let intrinsic = self
                    .tree
                    .warm(node_idx)
                    .and_then(|w| w.intrinsic_sizes)
                    .unwrap_or_default();

                // min-content size in the main axis; for items with a preferred aspect ratio, it
                // should be clamped by definite min/max cross sizes converted through the ratio.
                // For MinContent/MaxContent queries, use intrinsic sizes instead of layout result.
                // HOWEVER: If intrinsic sizes are 0 but content_width is non-zero, use content_width.
                // This happens for FormattingContext::Inline nodes that are measured by their
                // parent IFC root and don't have their own intrinsic sizes stored.
                //
                // CRITICAL FIX: For InlineBlock elements with width: auto (known_dimensions.width = None),
                // we must use intrinsic max-content width instead of content_width from BFC layout.
                // The BFC layout was done with the full container width, but InlineBlock should
                // shrink-to-fit its content. This is per CSS 2.1 § 10.3.9: "shrink-to-fit width".
                let fc = self
                    .tree
                    .get(node_idx)
                    .map(|s| s.formatting_context)
                    .unwrap_or_default();
                
                let is_shrink_to_fit = matches!(fc, FormattingContext::InlineBlock)
                    && inputs.known_dimensions.width.is_none();
                
                let effective_content_width = match inputs.available_space.width {
                    AvailableSpace::MinContent => {
                        if intrinsic.min_content_width > 0.0 {
                            intrinsic.min_content_width
                        } else {
                            content_width
                        }
                    }
                    AvailableSpace::MaxContent => {
                        if intrinsic.max_content_width > 0.0 {
                            intrinsic.max_content_width
                        } else {
                            content_width
                        }
                    }
                    AvailableSpace::Definite(_) => {
                        // For shrink-to-fit elements (InlineBlock with auto width),
                        // use intrinsic max-content width clamped by available space.
                        // CSS 2.1 § 10.3.9: shrink-to-fit = min(max(preferred minimum, available), preferred)
                        if is_shrink_to_fit && intrinsic.max_content_width > 0.0 {
                            // Use max-content (preferred width) - already clamped by min/max-width in sizing
                            intrinsic.max_content_width
                        } else {
                            content_width
                        }
                    }
                };

                // Replaced elements (image / VirtualView) have NO flow content, so the
                // BFC content_height above is 0 (and shrink-to-fit width may be wrong).
                // Their content size is the CSS/intrinsic-resolved size from
                // calculate_used_size_for_node (border-box) — strip padding+border back
                // to content-box. Fixes blank / 0-height images as flex/grid items.
                let (effective_content_width, content_height) = {
                    let dom_id = self.tree.get(node_idx).and_then(|n| n.dom_node_id);
                    let is_replaced = dom_id
                        .is_some_and(|id| {
                            let nd = &self.ctx.styled_dom.node_data.as_container()[id];
                            matches!(nd.get_node_type(), azul_core::dom::NodeType::Image(_))
                                || nd.is_virtual_view_node()
                        });
                    match (is_replaced, dom_id) {
                        (true, Some(id)) => {
                            let bp = self.tree.get(node_idx).unwrap().box_props.unpack();
                            crate::solver3::sizing::calculate_used_size_for_node(
                                self.ctx.styled_dom,
                                Some(id),
                                &constraints.containing_block_size,
                                intrinsic,
                                &bp,
                                &self.ctx.viewport_size,
                            ).map_or((effective_content_width, content_height), |sz| (
                                    (sz.width - padding_width - border_width).max(0.0),
                                    (sz.height - padding_height - border_height).max(0.0),
                                ))
                        }
                        _ => (effective_content_width, content_height),
                    }
                };

                // Convert content-box size to border-box size (for when we compute our own size)
                let border_box_width = effective_content_width + padding_width + border_width;
                let border_box_height = content_height + padding_height + border_height;

                // CRITICAL: Taffy's known_dimensions is BORDER-BOX (the child's
                // outer size as set by the parent flex/grid algorithm). Our BFC/IFC
                // layout computes content-box sizes, but Taffy expects the returned
                // `size` to be BORDER-BOX for correct positioning of subsequent items.
                //
                // When known_dimensions is set: use it directly (it's already border-box).
                // When it's None: add padding+border to our content-box result.
                let final_width = inputs.known_dimensions.width.map_or(border_box_width, |border_box_w| border_box_w);

                // For grid items: if known_dimensions.height is None but available_space.height
                // is definite, use the available space. This ensures empty grid items stretch
                // to fill their grid cell, per CSS Grid spec behavior.
                let final_height = if let Some(border_box_h) = inputs.known_dimensions.height { border_box_h } else {
                    // Check if parent is a grid container and available_space is definite
                    let parent_is_grid = self
                        .tree
                        .get(node_idx)
                        .and_then(|n| n.parent)
                        .and_then(|p| self.tree.get(p))
                        .is_some_and(|p| matches!(p.formatting_context, FormattingContext::Grid));

                    if parent_is_grid {
                        // For grid items, use available space if content is smaller
                        match inputs.available_space.height {
                            AvailableSpace::Definite(h) => {
                                // Grid items stretch to fill their cell by default
                                // Use the larger of content size or available space
                                h.max(border_box_height)
                            }
                            _ => border_box_height,
                        }
                    } else {
                        border_box_height
                    }
                };

                // CRITICAL: Transfer positions from layout_formatting_context to child nodes.
                // Without this, children of flex items won't have their relative_position set,
                // causing them to all render at (0,0) relative to their parent.
                for (child_idx, child_pos) in &output.positions {
                    if let Some(child_warm) = self.tree.warm_mut(*child_idx) {
                        child_warm.relative_position = Some(*child_pos);
                    }
                }

                // Compute scrollbar_info for this node (it's a child of a Flex/Grid container,
                // so calculate_layout_for_subtree won't be called for it).
                // Uses the unified compute_scrollbar_info_core path.
                let (scrollbar_info, _, _) = compute_taffy_scrollbar_info(
                    self.ctx,
                    self.tree,
                    node_idx,
                    final_width,
                    final_height,
                    content_width,
                    content_height,
                );

                // Store the border-box size and scrollbar_info on the node for display list generation
                if let Some(node) = self.tree.get_mut(node_idx) {
                    node.used_size = Some(LogicalSize {
                        width: final_width,
                        height: final_height,
                    });
                }
                if let Some(warm) = self.tree.warm_mut(node_idx) {
                    warm.scrollbar_info = Some(scrollbar_info);
                    // Store the actual content size for scroll calculations
                    warm.overflow_content_size = Some(LogicalSize {
                        width: content_width,
                        height: content_height,
                    });
                }

                // Return the same size to Taffy for correct positioning
                LayoutOutput {
                    size: Size {
                        width: final_width,
                        height: final_height,
                    },
                    content_size: Size {
                        width: content_width,
                        height: content_height,
                    },
                    first_baselines: taffy::Point {
                        x: None,
                        y: output.baseline,
                    },
                    top_margin: taffy::CollapsibleMarginSet::ZERO,
                    bottom_margin: taffy::CollapsibleMarginSet::ZERO,
                    margins_can_collapse_through: false,
                }
            }
            Err(_e) => {
                // Fallback to intrinsic sizes if layout fails
                let intrinsic = self.tree.warm(node_idx).and_then(|w| w.intrinsic_sizes).unwrap_or_default();

                let width = inputs
                    .known_dimensions
                    .width
                    .unwrap_or(intrinsic.max_content_width);
                let height = inputs
                    .known_dimensions
                    .height
                    .unwrap_or(intrinsic.max_content_height);

                LayoutOutput {
                    size: Size { width, height },
                    content_size: Size { width, height },
                    first_baselines: taffy::Point { x: None, y: None },
                    top_margin: taffy::CollapsibleMarginSet::ZERO,
                    bottom_margin: taffy::CollapsibleMarginSet::ZERO,
                    margins_can_collapse_through: false,
                }
            }
        }
    }
}

impl<T: ParsedFontTrait> CacheTree for TaffyBridge<'_, '_, T> {
    fn cache_get(
        &self,
        node_id: taffy::NodeId,
        input: &LayoutInput,
    ) -> Option<LayoutOutput> {
        let node_idx: usize = node_id.into();
        self.tree
            .warm(node_idx)?
            .taffy_cache
            .get(input)
    }

    fn cache_store(
        &mut self,
        node_id: taffy::NodeId,
        input: &LayoutInput,
        layout_output: LayoutOutput,
    ) {
        let node_idx: usize = node_id.into();
        if let Some(warm) = self.tree.warm_mut(node_idx) {
            warm.taffy_cache
                .store(input, layout_output);
        }
    }

    fn cache_clear(&mut self, node_id: taffy::NodeId) {
        let node_idx: usize = node_id.into();
        if let Some(warm) = self.tree.warm_mut(node_idx) {
            warm.taffy_cache.clear();
        }
    }
}

impl<T: ParsedFontTrait> LayoutFlexboxContainer for TaffyBridge<'_, '_, T> {
    type FlexboxContainerStyle<'c>
        = Style
    where
        Self: 'c;
    type FlexboxItemStyle<'c>
        = Style
    where
        Self: 'c;

    fn get_flexbox_container_style(
        &self,
        node_id: taffy::NodeId,
    ) -> Self::FlexboxContainerStyle<'_> {
        self.get_core_container_style(node_id)
    }

    fn get_flexbox_child_style(&self, child_node_id: taffy::NodeId) -> Self::FlexboxItemStyle<'_> {
        self.get_core_container_style(child_node_id)
    }
}

impl<T: ParsedFontTrait> LayoutGridContainer for TaffyBridge<'_, '_, T> {
    type GridContainerStyle<'c>
        = Style
    where
        Self: 'c;
    type GridItemStyle<'c>
        = Style
    where
        Self: 'c;

    fn get_grid_container_style(&self, node_id: taffy::NodeId) -> Self::GridContainerStyle<'_> {
        self.get_core_container_style(node_id)
    }

    fn get_grid_child_style(&self, child_node_id: taffy::NodeId) -> Self::GridItemStyle<'_> {
        self.get_core_container_style(child_node_id)
    }
}

// --- Conversion Functions ---

#[allow(clippy::match_same_arms)] // enum/value mapping/dispatch table: one arm per input variant (or cross-type bindings that can't merge)
#[allow(clippy::vec_box)] // calc_storage Box gives stable addresses for taffy calc() pointers
fn from_layout_width(
    val: LayoutWidth,
    calc_storage: &std::cell::RefCell<Vec<Box<CalcResolveContext>>>,
    em_size: f32,
    rem_size: f32,
) -> Dimension {
    match val {
        LayoutWidth::Auto => Dimension::auto(),
        LayoutWidth::Px(px) => pixel_value_to_pixels_fallback(&px).map_or_else(
            || px.to_percent().map_or_else(Dimension::auto, |p| Dimension::percent(p.get())),
            Dimension::length,
        ),
        LayoutWidth::MinContent | LayoutWidth::MaxContent | LayoutWidth::FitContent(_) => Dimension::auto(),
        LayoutWidth::Calc(items) => store_calc_and_make_dimension(items, calc_storage, em_size, rem_size),
    }
}

#[allow(clippy::match_same_arms)] // enum/value mapping/dispatch table: one arm per input variant (or cross-type bindings that can't merge)
#[allow(clippy::vec_box)] // calc_storage Box gives stable addresses for taffy calc() pointers
fn from_layout_height(
    val: LayoutHeight,
    calc_storage: &std::cell::RefCell<Vec<Box<CalcResolveContext>>>,
    em_size: f32,
    rem_size: f32,
) -> Dimension {
    match val {
        LayoutHeight::Auto => Dimension::auto(),
        LayoutHeight::Px(px) => pixel_value_to_pixels_fallback(&px).map_or_else(
            || px.to_percent().map_or_else(Dimension::auto, |p| Dimension::percent(p.get())),
            Dimension::length,
        ),
        LayoutHeight::MinContent | LayoutHeight::MaxContent | LayoutHeight::FitContent(_) => Dimension::auto(),
        LayoutHeight::Calc(items) => store_calc_and_make_dimension(items, calc_storage, em_size, rem_size),
    }
}

/// Stores the calc AST + font-size context in heap-pinned storage and returns
/// a `Dimension::calc(ptr)` with a stable pointer to the `CalcResolveContext`.
///
/// The `Box` ensures the address doesn't move when the outer `Vec` reallocates.
/// The `RefCell<Vec<…>>` keeps all boxes alive for the layout pass duration.
#[allow(clippy::vec_box)] // calc_storage Box gives stable addresses for taffy calc() pointers
fn store_calc_and_make_dimension(
    items: CalcAstItemVec,
    storage: &std::cell::RefCell<Vec<Box<CalcResolveContext>>>,
    em_size: f32,
    rem_size: f32,
) -> Dimension {
    let boxed = Box::new(CalcResolveContext { items, em_size, rem_size });
    let ptr: *const CalcResolveContext = &raw const *boxed;
    storage.borrow_mut().push(boxed);
    // SAFETY: Box gives ≥8-byte-aligned heap pointer; taffy masks low 3 bits.
    Dimension::calc(ptr.cast::<()>())
}

#[allow(clippy::match_same_arms)] // enum/value mapping/dispatch table: one arm per input variant (or cross-type bindings that can't merge)
const fn from_layout_position(val: LayoutPosition) -> Position {
    match val {
        LayoutPosition::Static => Position::Relative, // Taffy treats Static as Relative
        LayoutPosition::Relative => Position::Relative,
        LayoutPosition::Absolute => Position::Absolute,
        LayoutPosition::Fixed => Position::Absolute, // Taffy doesn't distinguish Fixed
        LayoutPosition::Sticky => Position::Relative, // Sticky = Relative for Taffy
    }
}

#[cfg(test)]
#[allow(
    clippy::float_cmp,
    clippy::too_many_lines,
    clippy::cast_precision_loss,
    clippy::cast_possible_truncation
)]
mod autotest_generated {
    use azul_css::props::layout::{
        dimensions::CalcAstItem,
        grid::{
            GridLine as AzGridLine, GridMinMax, GridPlacement as AzGridPlacement,
            GridTrackSizingVec, LayoutJustifyItems, NamedGridLine,
        },
        LayoutOverflow,
    };

    use super::*;

    // ==================================================================
    // Fixtures
    // ==================================================================

    fn track_vec(v: Vec<GridTrackSizing>) -> GridTrackSizingVec {
        GridTrackSizingVec::from_vec(v)
    }

    fn template(v: Vec<GridTrackSizing>) -> GridTemplate {
        GridTemplate { tracks: track_vec(v) }
    }

    fn auto_tracks(v: Vec<GridTrackSizing>) -> GridAutoTracks {
        GridAutoTracks { tracks: track_vec(v) }
    }

    /// The absolute metrics `pixel_value_to_pixels_fallback` can resolve.
    const ABSOLUTE_METRICS: [SizeMetric; 7] = [
        SizeMetric::Px,
        SizeMetric::Pt,
        SizeMetric::In,
        SizeMetric::Cm,
        SizeMetric::Mm,
        SizeMetric::Em,
        SizeMetric::Rem,
    ];

    /// The metrics that need a resolution context this function does not have.
    const CONTEXTUAL_METRICS: [SizeMetric; 5] = [
        SizeMetric::Percent,
        SizeMetric::Vw,
        SizeMetric::Vh,
        SizeMetric::Vmin,
        SizeMetric::Vmax,
    ];

    fn approx(actual: f32, expected: f32) {
        assert!(
            (actual - expected).abs() < 1e-3,
            "expected ~{expected}, got {actual}"
        );
    }

    // ==================================================================
    // pixel_value_to_pixels_fallback — numeric
    // ==================================================================

    #[test]
    fn pixel_value_to_pixels_fallback_is_zero_at_zero_for_every_absolute_metric() {
        for m in ABSOLUTE_METRICS {
            assert_eq!(
                pixel_value_to_pixels_fallback(&PixelValue::from_metric(m, 0.0)),
                Some(0.0),
                "{m:?}"
            );
        }
    }

    #[test]
    fn pixel_value_to_pixels_fallback_converts_each_absolute_unit_to_css_px() {
        assert_eq!(
            pixel_value_to_pixels_fallback(&PixelValue::px(10.5)),
            Some(10.5)
        );
        assert_eq!(
            pixel_value_to_pixels_fallback(&PixelValue::inch(1.0)),
            Some(96.0)
        );
        assert_eq!(
            pixel_value_to_pixels_fallback(&PixelValue::em(2.0)),
            Some(2.0 * DEFAULT_FONT_SIZE)
        );
        assert_eq!(
            pixel_value_to_pixels_fallback(&PixelValue::rem(2.0)),
            Some(2.0 * DEFAULT_FONT_SIZE)
        );
        approx(
            pixel_value_to_pixels_fallback(&PixelValue::pt(72.0)).unwrap(),
            96.0,
        );
        approx(
            pixel_value_to_pixels_fallback(&PixelValue::cm(2.54)).unwrap(),
            96.0,
        );
        approx(
            pixel_value_to_pixels_fallback(&PixelValue::mm(25.4)).unwrap(),
            96.0,
        );
        // PT_TO_PX is the documented factor — 1pt = 96/72 px.
        approx(
            pixel_value_to_pixels_fallback(&PixelValue::pt(1.0)).unwrap(),
            PT_TO_PX,
        );
    }

    #[test]
    fn pixel_value_to_pixels_fallback_keeps_the_sign_of_negative_lengths() {
        for m in ABSOLUTE_METRICS {
            let out = pixel_value_to_pixels_fallback(&PixelValue::from_metric(m, -4.0))
                .expect("absolute metric resolves");
            assert!(out < 0.0, "{m:?} produced {out} for -4");
        }
    }

    #[test]
    fn pixel_value_to_pixels_fallback_returns_none_for_context_dependent_metrics() {
        for m in CONTEXTUAL_METRICS {
            assert_eq!(
                pixel_value_to_pixels_fallback(&PixelValue::from_metric(m, 50.0)),
                None,
                "{m:?} must not be resolved without a containing block / viewport"
            );
        }
    }

    #[test]
    fn pixel_value_to_pixels_fallback_sanitises_nan_and_infinite_inputs() {
        // `FloatValue` stores `f32 * 1000` in an `isize` via an `as` cast, which
        // saturates: NaN → 0, ±inf → isize::MIN/MAX. So no NaN and no infinity can
        // reach the layout through a PixelValue, whatever the caller passes in.
        assert_eq!(
            pixel_value_to_pixels_fallback(&PixelValue::px(f32::NAN)),
            Some(0.0),
            "NaN must be flattened to 0, not propagated"
        );

        let pos = pixel_value_to_pixels_fallback(&PixelValue::px(f32::INFINITY))
            .expect("px resolves");
        assert!(pos.is_finite() && pos > 0.0, "+inf px became {pos}");

        let neg = pixel_value_to_pixels_fallback(&PixelValue::px(f32::NEG_INFINITY))
            .expect("px resolves");
        assert!(neg.is_finite() && neg < 0.0, "-inf px became {neg}");
    }

    #[test]
    fn pixel_value_to_pixels_fallback_stays_finite_at_the_f32_extremes() {
        for v in [f32::MAX, f32::MIN, f32::MIN_POSITIVE, -f32::MIN_POSITIVE] {
            for m in ABSOLUTE_METRICS {
                let out = pixel_value_to_pixels_fallback(&PixelValue::from_metric(m, v))
                    .expect("absolute metric resolves");
                assert!(out.is_finite(), "{m:?} at {v} overflowed to {out}");
            }
        }
    }

    // ==================================================================
    // minmax + translate_track
    // ==================================================================

    #[test]
    fn minmax_puts_the_arguments_where_it_says_it_does() {
        let t = minmax(
            MinTrackSizingFunction::length(1.0),
            MaxTrackSizingFunction::fr(2.0),
        );
        assert_eq!(t.min, MinTrackSizingFunction::length(1.0));
        assert_eq!(t.max, MaxTrackSizingFunction::fr(2.0));
    }

    #[test]
    fn translate_track_maps_the_intrinsic_keywords() {
        assert_eq!(
            translate_track(&GridTrackSizing::MinContent),
            minmax(
                MinTrackSizingFunction::min_content(),
                MaxTrackSizingFunction::min_content()
            )
        );
        assert_eq!(
            translate_track(&GridTrackSizing::MaxContent),
            minmax(
                MinTrackSizingFunction::max_content(),
                MaxTrackSizingFunction::max_content()
            )
        );
        // `auto` is minmax(min-content, max-content) per CSS Grid §7.2.
        assert_eq!(
            translate_track(&GridTrackSizing::Auto),
            minmax(
                MinTrackSizingFunction::min_content(),
                MaxTrackSizingFunction::max_content()
            )
        );
    }

    #[test]
    fn translate_track_resolves_fixed_tracks_through_the_absolute_unit_table() {
        assert_eq!(
            translate_track(&GridTrackSizing::Fixed(PixelValue::px(120.0))),
            minmax(
                MinTrackSizingFunction::length(120.0),
                MaxTrackSizingFunction::length(120.0)
            )
        );
        assert_eq!(
            translate_track(&GridTrackSizing::Fixed(PixelValue::em(2.0))),
            minmax(
                MinTrackSizingFunction::length(32.0),
                MaxTrackSizingFunction::length(32.0)
            )
        );
        assert_eq!(
            translate_track(&GridTrackSizing::FitContent(PixelValue::px(50.0))),
            minmax(
                MinTrackSizingFunction::length(50.0),
                MaxTrackSizingFunction::max_content()
            )
        );
    }

    #[test]
    fn translate_track_collapses_unresolvable_track_units_to_zero_px() {
        // % / vw / vh cannot be expressed as a taffy track sizing fn here, and the
        // `.unwrap_or(0.0)` inside translate_track turns them into a 0px track
        // rather than dropping the track. Locking the (lossy) behaviour in.
        for m in CONTEXTUAL_METRICS {
            let pv = PixelValue::from_metric(m, 50.0);
            assert_eq!(
                translate_track(&GridTrackSizing::Fixed(pv)),
                minmax(
                    MinTrackSizingFunction::length(0.0),
                    MaxTrackSizingFunction::length(0.0)
                ),
                "{m:?}"
            );
            assert_eq!(
                translate_track(&GridTrackSizing::FitContent(pv)),
                minmax(
                    MinTrackSizingFunction::length(0.0),
                    MaxTrackSizingFunction::max_content()
                ),
                "{m:?}"
            );
        }
    }

    #[test]
    fn translate_track_divides_fr_by_the_hundredfold_scaling_factor() {
        assert_eq!(
            translate_track(&GridTrackSizing::Fr(100)),
            minmax(
                MinTrackSizingFunction::auto(),
                MaxTrackSizingFunction::fr(1.0)
            )
        );
        assert_eq!(
            translate_track(&GridTrackSizing::Fr(50)),
            minmax(
                MinTrackSizingFunction::auto(),
                MaxTrackSizingFunction::fr(0.5)
            )
        );
        assert_eq!(
            translate_track(&GridTrackSizing::Fr(0)),
            minmax(
                MinTrackSizingFunction::auto(),
                MaxTrackSizingFunction::fr(0.0)
            )
        );
    }

    #[test]
    fn translate_track_does_not_overflow_at_the_fr_integer_bounds() {
        for fr in [i32::MIN, i32::MIN + 1, -100, i32::MAX, i32::MAX - 1] {
            let t = translate_track(&GridTrackSizing::Fr(fr));
            let v = t.max.into_raw().value();
            assert!(v.is_finite(), "Fr({fr}) produced a non-finite fr: {v}");
            assert_eq!(v, fr as f32 / 100.0, "Fr({fr})");
        }
    }

    #[test]
    fn translate_track_minmax_takes_the_min_of_the_min_and_the_max_of_the_max() {
        let t = GridTrackSizing::MinMax(GridMinMax {
            min: Box::new(GridTrackSizing::Fixed(PixelValue::px(10.0))),
            max: Box::new(GridTrackSizing::Fr(200)),
        });
        assert_eq!(
            translate_track(&t),
            minmax(
                MinTrackSizingFunction::length(10.0),
                MaxTrackSizingFunction::fr(2.0)
            )
        );

        // The *other* halves are discarded: only minmax_box.min.min and
        // minmax_box.max.max survive the translation.
        let t = GridTrackSizing::MinMax(GridMinMax {
            min: Box::new(GridTrackSizing::MaxContent),
            max: Box::new(GridTrackSizing::MinContent),
        });
        assert_eq!(
            translate_track(&t),
            minmax(
                MinTrackSizingFunction::max_content(),
                MaxTrackSizingFunction::min_content()
            )
        );
    }

    #[test]
    fn translate_track_terminates_on_a_left_nested_minmax_chain() {
        // Nesting only on the `min` side keeps the recursion linear.
        let mut t = GridTrackSizing::Fixed(PixelValue::px(7.0));
        for _ in 0..64 {
            t = GridTrackSizing::MinMax(GridMinMax {
                min: Box::new(t),
                max: Box::new(GridTrackSizing::MaxContent),
            });
        }
        assert_eq!(
            translate_track(&t),
            minmax(
                MinTrackSizingFunction::length(7.0),
                MaxTrackSizingFunction::max_content()
            )
        );
    }

    #[test]
    fn translate_track_terminates_on_a_doubly_nested_minmax_chain() {
        // NOTE: translate_track calls itself on BOTH halves of a MinMax, so a
        // minmax nested on both sides costs O(2^depth). CSS grammar forbids
        // minmax() inside minmax(), so the parser can't reach this — but the type
        // can express it. Depth 10 = ~1k calls; it must still terminate and pick
        // the leaf on each side.
        let mut t = GridTrackSizing::Fixed(PixelValue::px(3.0));
        for _ in 0..10 {
            t = GridTrackSizing::MinMax(GridMinMax {
                min: Box::new(t.clone()),
                max: Box::new(t),
            });
        }
        assert_eq!(
            translate_track(&t),
            minmax(
                MinTrackSizingFunction::length(3.0),
                MaxTrackSizingFunction::length(3.0)
            )
        );
    }

    // ==================================================================
    // grid-template-* / grid-auto-* → taffy
    // ==================================================================

    #[test]
    fn grid_templates_are_empty_for_every_non_exact_css_value() {
        for v in [
            CssPropertyValue::None,
            CssPropertyValue::Inherit,
            CssPropertyValue::Revert,
            CssPropertyValue::Unset,
            CssPropertyValue::Auto,
            CssPropertyValue::Initial,
        ] {
            assert!(grid_template_rows_to_taffy(v.clone()).is_empty(), "{v:?}");
            assert!(grid_template_columns_to_taffy(v).is_empty());
        }
        assert!(grid_template_rows_to_taffy(CssPropertyValue::Exact(template(Vec::new()))).is_empty());
    }

    #[test]
    fn grid_auto_tracks_are_empty_for_every_non_exact_css_value() {
        for v in [
            CssPropertyValue::None,
            CssPropertyValue::Inherit,
            CssPropertyValue::Revert,
            CssPropertyValue::Unset,
            CssPropertyValue::Auto,
            CssPropertyValue::Initial,
        ] {
            assert!(grid_auto_rows_to_taffy(v.clone()).is_empty(), "{v:?}");
            assert!(grid_auto_columns_to_taffy(v).is_empty());
        }
    }

    #[test]
    fn grid_template_rows_and_columns_translate_each_track_in_order() {
        let t = template(vec![
            GridTrackSizing::Fr(100),
            GridTrackSizing::Fixed(PixelValue::px(20.0)),
            GridTrackSizing::Auto,
        ]);
        let rows = grid_template_rows_to_taffy(CssPropertyValue::Exact(t.clone()));
        let cols = grid_template_columns_to_taffy(CssPropertyValue::Exact(t));

        assert_eq!(rows.len(), 3);
        assert_eq!(rows, cols, "rows and columns share one translation path");
        assert_eq!(
            rows[0],
            GridTemplateComponent::Single(minmax(
                MinTrackSizingFunction::auto(),
                MaxTrackSizingFunction::fr(1.0)
            ))
        );
        assert_eq!(
            rows[1],
            GridTemplateComponent::Single(minmax(
                MinTrackSizingFunction::length(20.0),
                MaxTrackSizingFunction::length(20.0)
            ))
        );
        assert_eq!(
            rows[2],
            GridTemplateComponent::Single(minmax(
                MinTrackSizingFunction::min_content(),
                MaxTrackSizingFunction::max_content()
            ))
        );
    }

    #[test]
    fn grid_auto_rows_and_columns_agree_on_every_track() {
        let tracks = vec![
            GridTrackSizing::Fr(250),
            GridTrackSizing::MinContent,
            GridTrackSizing::FitContent(PixelValue::px(9.0)),
            GridTrackSizing::MinMax(GridMinMax {
                min: Box::new(GridTrackSizing::Fixed(PixelValue::px(1.0))),
                max: Box::new(GridTrackSizing::MaxContent),
            }),
        ];
        let rows = grid_auto_rows_to_taffy(CssPropertyValue::Exact(auto_tracks(tracks.clone())));
        let cols = grid_auto_columns_to_taffy(CssPropertyValue::Exact(auto_tracks(tracks.clone())));

        assert_eq!(rows.len(), tracks.len());
        // grid_auto_rows_to_taffy rebuilds the MinMax by calling translate_track
        // twice; it must land on exactly the same value as the single-call path.
        assert_eq!(rows, cols);
        for (i, track) in tracks.iter().enumerate() {
            assert_eq!(rows[i], translate_track(track), "track #{i}");
        }
    }

    #[test]
    fn grid_templates_handle_a_very_long_track_list() {
        let tracks: Vec<GridTrackSizing> = (0..2048).map(GridTrackSizing::Fr).collect();
        let out = grid_template_columns_to_taffy(CssPropertyValue::Exact(template(tracks)));
        assert_eq!(out.len(), 2048);
        assert_eq!(
            out[2047],
            GridTemplateComponent::Single(minmax(
                MinTrackSizingFunction::auto(),
                MaxTrackSizingFunction::fr(20.47)
            ))
        );
    }

    // ==================================================================
    // decode_compact_grid_line — numeric
    // ==================================================================

    #[test]
    fn decode_compact_grid_line_maps_both_sentinels_to_auto() {
        assert_eq!(
            decode_compact_grid_line(azul_css::compact_cache::I16_AUTO),
            GridPlacement::<String>::Auto
        );
        assert_eq!(
            decode_compact_grid_line(azul_css::compact_cache::I16_SENTINEL),
            GridPlacement::<String>::Auto
        );
        // i16::MAX *is* the sentinel, so the top of the range is Auto, not a line.
        assert_eq!(
            decode_compact_grid_line(i16::MAX),
            GridPlacement::<String>::Auto
        );
    }

    #[test]
    fn decode_compact_grid_line_zero_is_line_zero_not_auto() {
        assert_eq!(
            decode_compact_grid_line(0),
            GridPlacement::<String>::from_line_index(0)
        );
    }

    #[test]
    fn decode_compact_grid_line_positive_is_a_line_and_negative_is_a_span() {
        assert_eq!(
            decode_compact_grid_line(3),
            GridPlacement::<String>::from_line_index(3)
        );
        assert_eq!(
            decode_compact_grid_line(32_765),
            GridPlacement::<String>::from_line_index(32_765),
            "the largest value below I16_SENTINEL_THRESHOLD is still a line"
        );
        assert_eq!(
            decode_compact_grid_line(-1),
            GridPlacement::<String>::from_span(1)
        );
        assert_eq!(
            decode_compact_grid_line(-4),
            GridPlacement::<String>::from_span(4)
        );
        assert_eq!(
            decode_compact_grid_line(-32_767),
            GridPlacement::<String>::from_span(32_767)
        );
    }

    #[test]
    fn decode_compact_grid_line_at_i16_min_overflows_the_negation() {
        // `(-v) as u16` on i16::MIN: -(-32768) is not representable in i16.
        // Debug builds panic ("attempt to negate with overflow"); release wraps
        // back to i16::MIN, whose bit pattern as u16 is 32768. Neither is a
        // sensible span. Accept both so the test is profile-independent, and see
        // the report: this input should be rejected (or the negation widened to
        // i32) inside decode_compact_grid_line.
        let decoded = std::panic::catch_unwind(|| decode_compact_grid_line(i16::MIN));
        match decoded {
            Err(_) => { /* debug: overflow panic */ }
            Ok(p) => assert_eq!(
                p,
                GridPlacement::<String>::from_span(32_768),
                "release build: the negation wraps"
            ),
        }
    }

    // ==================================================================
    // grid_line_to_taffy / grid_placement_to_taffy
    // ==================================================================

    #[test]
    fn grid_line_to_taffy_maps_auto_lines_and_spans() {
        assert_eq!(
            grid_line_to_taffy(&AzGridLine::Auto),
            GridPlacement::<String>::Auto
        );
        assert_eq!(
            grid_line_to_taffy(&AzGridLine::Line(0)),
            GridPlacement::<String>::from_line_index(0)
        );
        assert_eq!(
            grid_line_to_taffy(&AzGridLine::Line(4)),
            GridPlacement::<String>::from_line_index(4)
        );
        assert_eq!(
            grid_line_to_taffy(&AzGridLine::Line(-2)),
            GridPlacement::<String>::from_line_index(-2),
            "negative lines count from the end of the explicit grid"
        );
        assert_eq!(
            grid_line_to_taffy(&AzGridLine::Span(3)),
            GridPlacement::<String>::from_span(3)
        );
        assert_eq!(
            grid_line_to_taffy(&AzGridLine::Span(0)),
            GridPlacement::<String>::from_span(0)
        );
    }

    #[test]
    fn grid_line_to_taffy_truncates_out_of_range_line_numbers_instead_of_clamping() {
        // azul stores grid lines as i32, taffy as i16 — the `as i16` cast wraps.
        // A `grid-column: 70000` therefore silently becomes line 4464 rather than
        // being clamped to i16::MAX. Documented here; see the report.
        assert_eq!(
            grid_line_to_taffy(&AzGridLine::Line(70_000)),
            GridPlacement::<String>::from_line_index(70_000_i32 as i16)
        );
        assert_eq!(
            grid_line_to_taffy(&AzGridLine::Line(i32::MAX)),
            GridPlacement::<String>::from_line_index(-1),
            "i32::MAX wraps to line -1 — the far end of the grid"
        );
        assert_eq!(
            grid_line_to_taffy(&AzGridLine::Line(i32::MIN)),
            GridPlacement::<String>::from_line_index(0)
        );
    }

    #[test]
    fn grid_line_to_taffy_wraps_out_of_range_and_negative_spans() {
        // `span -1` is not valid CSS, but the i32 → u16 cast turns it into the
        // largest possible span rather than rejecting it.
        assert_eq!(
            grid_line_to_taffy(&AzGridLine::Span(-1)),
            GridPlacement::<String>::from_span(u16::MAX)
        );
        // `span 65536` wraps to `span 0`.
        assert_eq!(
            grid_line_to_taffy(&AzGridLine::Span(65_536)),
            GridPlacement::<String>::from_span(0)
        );
        assert_eq!(
            grid_line_to_taffy(&AzGridLine::Span(i32::MIN)),
            GridPlacement::<String>::from_span(0)
        );
    }

    #[test]
    fn grid_line_to_taffy_named_lines_keep_their_name_and_split_on_the_span_count() {
        let named = |name: &str, span: i32| {
            AzGridLine::Named(NamedGridLine {
                grid_line_name: name.into(),
                span_count: span,
            })
        };

        assert_eq!(
            grid_line_to_taffy(&named("sidebar", 0)),
            GridPlacement::NamedLine("sidebar".to_string(), 0)
        );
        assert_eq!(
            grid_line_to_taffy(&named("sidebar", 2)),
            GridPlacement::NamedSpan("sidebar".to_string(), 2)
        );
        // A negative span_count is not > 0, so it falls back to a named *line*.
        assert_eq!(
            grid_line_to_taffy(&named("sidebar", -5)),
            GridPlacement::NamedLine("sidebar".to_string(), 0)
        );
        // Empty and non-ASCII names must survive the AzString → String hop.
        assert_eq!(
            grid_line_to_taffy(&named("", 0)),
            GridPlacement::NamedLine(String::new(), 0)
        );
        assert_eq!(
            grid_line_to_taffy(&named("行 🎉 col", 1)),
            GridPlacement::NamedSpan("行 🎉 col".to_string(), 1)
        );
    }

    #[test]
    fn grid_line_to_taffy_named_span_count_wraps_at_u16() {
        assert_eq!(
            grid_line_to_taffy(&AzGridLine::Named(NamedGridLine {
                grid_line_name: "x".into(),
                span_count: 65_536,
            })),
            GridPlacement::NamedSpan("x".to_string(), 0),
            "span_count 65536 wraps to a 0-track named span"
        );
    }

    #[test]
    fn grid_placement_to_taffy_does_not_swap_start_and_end() {
        let p = AzGridPlacement {
            grid_start: AzGridLine::Line(2),
            grid_end: AzGridLine::Span(3),
        };
        let out = grid_placement_to_taffy(&p);
        assert_eq!(out.start, GridPlacement::<String>::from_line_index(2));
        assert_eq!(out.end, GridPlacement::<String>::from_span(3));

        let both_auto = AzGridPlacement {
            grid_start: AzGridLine::Auto,
            grid_end: AzGridLine::Auto,
        };
        let out = grid_placement_to_taffy(&both_auto);
        assert_eq!(out.start, GridPlacement::<String>::Auto);
        assert_eq!(out.end, GridPlacement::<String>::Auto);
    }

    // ==================================================================
    // enum → taffy mapping tables
    // ==================================================================

    #[test]
    fn layout_display_to_taffy_maps_flex_and_grid_and_folds_the_rest_into_block() {
        assert_eq!(
            layout_display_to_taffy(CssPropertyValue::Exact(LayoutDisplay::None)),
            Display::None
        );
        for d in [LayoutDisplay::Flex, LayoutDisplay::InlineFlex] {
            assert_eq!(
                layout_display_to_taffy(CssPropertyValue::Exact(d)),
                Display::Flex,
                "{d:?}"
            );
        }
        for d in [LayoutDisplay::Grid, LayoutDisplay::InlineGrid] {
            assert_eq!(
                layout_display_to_taffy(CssPropertyValue::Exact(d)),
                Display::Grid,
                "{d:?}"
            );
        }
        // Everything else — including `contents`, `table*` and `list-item` — is
        // handed to taffy as a plain block box.
        for d in [
            LayoutDisplay::Block,
            LayoutDisplay::Inline,
            LayoutDisplay::InlineBlock,
            LayoutDisplay::Table,
            LayoutDisplay::TableCell,
            LayoutDisplay::TableRow,
            LayoutDisplay::FlowRoot,
            LayoutDisplay::ListItem,
            LayoutDisplay::Contents,
        ] {
            assert_eq!(
                layout_display_to_taffy(CssPropertyValue::Exact(d)),
                Display::Block,
                "{d:?}"
            );
        }
    }

    #[test]
    fn layout_display_to_taffy_distinguishes_css_wide_none_from_display_none() {
        // `CssPropertyValue::None` means "the property is absent", NOT `display: none`.
        // It must fall back to the initial value (block), or nothing would render.
        assert_eq!(
            layout_display_to_taffy(CssPropertyValue::None),
            Display::Block
        );
        assert_eq!(
            layout_display_to_taffy(CssPropertyValue::Inherit),
            Display::Block
        );
        assert_eq!(
            layout_display_to_taffy(CssPropertyValue::Auto),
            Display::Block
        );
        assert_eq!(
            layout_display_to_taffy(CssPropertyValue::Exact(LayoutDisplay::None)),
            Display::None,
            "…but an explicit `display: none` still means none"
        );
    }

    #[test]
    fn layout_position_to_taffy_and_from_layout_position_agree_on_every_variant() {
        let all = [
            LayoutPosition::Static,
            LayoutPosition::Relative,
            LayoutPosition::Absolute,
            LayoutPosition::Fixed,
            LayoutPosition::Sticky,
        ];
        for p in all {
            assert_eq!(
                layout_position_to_taffy(CssPropertyValue::Exact(p)),
                from_layout_position(p),
                "the two position paths disagree on {p:?}"
            );
        }
        assert_eq!(from_layout_position(LayoutPosition::Static), Position::Relative);
        assert_eq!(from_layout_position(LayoutPosition::Relative), Position::Relative);
        assert_eq!(from_layout_position(LayoutPosition::Sticky), Position::Relative);
        assert_eq!(from_layout_position(LayoutPosition::Absolute), Position::Absolute);
        assert_eq!(from_layout_position(LayoutPosition::Fixed), Position::Absolute);
        // Absent property → `static` → relative.
        assert_eq!(layout_position_to_taffy(CssPropertyValue::None), Position::Relative);
        assert_eq!(layout_position_to_taffy(CssPropertyValue::Inherit), Position::Relative);
    }

    #[test]
    fn grid_auto_flow_to_taffy_maps_all_four_variants_and_defaults_to_row() {
        assert_eq!(
            grid_auto_flow_to_taffy(CssPropertyValue::Exact(LayoutGridAutoFlow::Row)),
            GridAutoFlow::Row
        );
        assert_eq!(
            grid_auto_flow_to_taffy(CssPropertyValue::Exact(LayoutGridAutoFlow::Column)),
            GridAutoFlow::Column
        );
        assert_eq!(
            grid_auto_flow_to_taffy(CssPropertyValue::Exact(LayoutGridAutoFlow::RowDense)),
            GridAutoFlow::RowDense
        );
        assert_eq!(
            grid_auto_flow_to_taffy(CssPropertyValue::Exact(LayoutGridAutoFlow::ColumnDense)),
            GridAutoFlow::ColumnDense
        );
        for v in [
            CssPropertyValue::None,
            CssPropertyValue::Inherit,
            CssPropertyValue::Revert,
            CssPropertyValue::Unset,
            CssPropertyValue::Auto,
            CssPropertyValue::Initial,
        ] {
            assert_eq!(grid_auto_flow_to_taffy(v), GridAutoFlow::Row, "{v:?}");
        }
    }

    #[test]
    fn layout_flex_direction_to_taffy_maps_all_four_variants_and_defaults_to_row() {
        assert_eq!(
            layout_flex_direction_to_taffy(CssPropertyValue::Exact(LayoutFlexDirection::Row)),
            FlexDirection::Row
        );
        assert_eq!(
            layout_flex_direction_to_taffy(CssPropertyValue::Exact(LayoutFlexDirection::RowReverse)),
            FlexDirection::RowReverse
        );
        assert_eq!(
            layout_flex_direction_to_taffy(CssPropertyValue::Exact(LayoutFlexDirection::Column)),
            FlexDirection::Column
        );
        assert_eq!(
            layout_flex_direction_to_taffy(CssPropertyValue::Exact(
                LayoutFlexDirection::ColumnReverse
            )),
            FlexDirection::ColumnReverse
        );
        assert_eq!(
            layout_flex_direction_to_taffy(CssPropertyValue::Inherit),
            FlexDirection::Row
        );
    }

    #[test]
    fn layout_flex_wrap_to_taffy_maps_all_three_variants_and_defaults_to_nowrap() {
        assert_eq!(
            layout_flex_wrap_to_taffy(CssPropertyValue::Exact(LayoutFlexWrap::NoWrap)),
            FlexWrap::NoWrap
        );
        assert_eq!(
            layout_flex_wrap_to_taffy(CssPropertyValue::Exact(LayoutFlexWrap::Wrap)),
            FlexWrap::Wrap
        );
        assert_eq!(
            layout_flex_wrap_to_taffy(CssPropertyValue::Exact(LayoutFlexWrap::WrapReverse)),
            FlexWrap::WrapReverse
        );
        assert_eq!(
            layout_flex_wrap_to_taffy(CssPropertyValue::Inherit),
            FlexWrap::NoWrap
        );
    }

    #[test]
    fn layout_align_items_to_taffy_maps_start_and_end_onto_the_flex_variants() {
        assert_eq!(
            layout_align_items_to_taffy(CssPropertyValue::Exact(LayoutAlignItems::Stretch)),
            AlignItems::Stretch
        );
        assert_eq!(
            layout_align_items_to_taffy(CssPropertyValue::Exact(LayoutAlignItems::Center)),
            AlignItems::Center
        );
        assert_eq!(
            layout_align_items_to_taffy(CssPropertyValue::Exact(LayoutAlignItems::Start)),
            AlignItems::FlexStart
        );
        assert_eq!(
            layout_align_items_to_taffy(CssPropertyValue::Exact(LayoutAlignItems::End)),
            AlignItems::FlexEnd
        );
        assert_eq!(
            layout_align_items_to_taffy(CssPropertyValue::Exact(LayoutAlignItems::Baseline)),
            AlignItems::Baseline
        );
        // Absent → the CSS initial value, `stretch`.
        assert_eq!(
            layout_align_items_to_taffy(CssPropertyValue::Inherit),
            AlignItems::Stretch
        );
    }

    #[test]
    fn layout_align_self_to_taffy_returns_none_only_for_auto() {
        assert_eq!(
            layout_align_self_to_taffy(CssPropertyValue::Exact(LayoutAlignSelf::Auto)),
            None
        );
        // `auto` is the initial value, so an absent property is None too — that is
        // what lets taffy inherit the parent's align-items.
        assert_eq!(layout_align_self_to_taffy(CssPropertyValue::Inherit), None);
        assert_eq!(layout_align_self_to_taffy(CssPropertyValue::Initial), None);

        assert_eq!(
            layout_align_self_to_taffy(CssPropertyValue::Exact(LayoutAlignSelf::Start)),
            Some(AlignSelf::FlexStart)
        );
        assert_eq!(
            layout_align_self_to_taffy(CssPropertyValue::Exact(LayoutAlignSelf::End)),
            Some(AlignSelf::FlexEnd)
        );
        assert_eq!(
            layout_align_self_to_taffy(CssPropertyValue::Exact(LayoutAlignSelf::Center)),
            Some(AlignSelf::Center)
        );
        assert_eq!(
            layout_align_self_to_taffy(CssPropertyValue::Exact(LayoutAlignSelf::Baseline)),
            Some(AlignSelf::Baseline)
        );
        assert_eq!(
            layout_align_self_to_taffy(CssPropertyValue::Exact(LayoutAlignSelf::Stretch)),
            Some(AlignSelf::Stretch)
        );
    }

    #[test]
    fn layout_align_content_to_taffy_maps_every_variant_and_defaults_to_stretch() {
        assert_eq!(
            layout_align_content_to_taffy(CssPropertyValue::Exact(LayoutAlignContent::Start)),
            AlignContent::FlexStart
        );
        assert_eq!(
            layout_align_content_to_taffy(CssPropertyValue::Exact(LayoutAlignContent::End)),
            AlignContent::FlexEnd
        );
        assert_eq!(
            layout_align_content_to_taffy(CssPropertyValue::Exact(LayoutAlignContent::Center)),
            AlignContent::Center
        );
        assert_eq!(
            layout_align_content_to_taffy(CssPropertyValue::Exact(LayoutAlignContent::Stretch)),
            AlignContent::Stretch
        );
        assert_eq!(
            layout_align_content_to_taffy(CssPropertyValue::Exact(
                LayoutAlignContent::SpaceBetween
            )),
            AlignContent::SpaceBetween
        );
        assert_eq!(
            layout_align_content_to_taffy(CssPropertyValue::Exact(
                LayoutAlignContent::SpaceAround
            )),
            AlignContent::SpaceAround
        );
        assert_eq!(
            layout_align_content_to_taffy(CssPropertyValue::Inherit),
            AlignContent::Stretch
        );
    }

    #[test]
    fn layout_justify_content_to_taffy_keeps_start_distinct_from_flex_start() {
        assert_eq!(
            layout_justify_content_to_taffy(CssPropertyValue::Exact(
                LayoutJustifyContent::FlexStart
            )),
            JustifyContent::FlexStart
        );
        assert_eq!(
            layout_justify_content_to_taffy(CssPropertyValue::Exact(LayoutJustifyContent::Start)),
            JustifyContent::Start
        );
        assert_ne!(JustifyContent::Start, JustifyContent::FlexStart);
        assert_eq!(
            layout_justify_content_to_taffy(CssPropertyValue::Exact(
                LayoutJustifyContent::FlexEnd
            )),
            JustifyContent::FlexEnd
        );
        assert_eq!(
            layout_justify_content_to_taffy(CssPropertyValue::Exact(LayoutJustifyContent::End)),
            JustifyContent::End
        );
        assert_eq!(
            layout_justify_content_to_taffy(CssPropertyValue::Exact(LayoutJustifyContent::Center)),
            JustifyContent::Center
        );
        assert_eq!(
            layout_justify_content_to_taffy(CssPropertyValue::Exact(
                LayoutJustifyContent::SpaceBetween
            )),
            JustifyContent::SpaceBetween
        );
        assert_eq!(
            layout_justify_content_to_taffy(CssPropertyValue::Exact(
                LayoutJustifyContent::SpaceAround
            )),
            JustifyContent::SpaceAround
        );
        assert_eq!(
            layout_justify_content_to_taffy(CssPropertyValue::Exact(
                LayoutJustifyContent::SpaceEvenly
            )),
            JustifyContent::SpaceEvenly
        );
        // Initial value of justify-content in this codebase is `start`.
        assert_eq!(
            layout_justify_content_to_taffy(CssPropertyValue::Inherit),
            JustifyContent::Start
        );
    }

    #[test]
    fn layout_justify_items_to_taffy_maps_all_four_variants_and_defaults_to_stretch() {
        assert_eq!(
            layout_justify_items_to_taffy(CssPropertyValue::Exact(LayoutJustifyItems::Start)),
            AlignItems::Start
        );
        assert_eq!(
            layout_justify_items_to_taffy(CssPropertyValue::Exact(LayoutJustifyItems::End)),
            AlignItems::End
        );
        assert_eq!(
            layout_justify_items_to_taffy(CssPropertyValue::Exact(LayoutJustifyItems::Center)),
            AlignItems::Center
        );
        assert_eq!(
            layout_justify_items_to_taffy(CssPropertyValue::Exact(LayoutJustifyItems::Stretch)),
            AlignItems::Stretch
        );
        assert_eq!(
            layout_justify_items_to_taffy(CssPropertyValue::Inherit),
            AlignItems::Stretch
        );
        // justify-items uses the *logical* Start/End, unlike align-items, which is
        // mapped onto FlexStart/FlexEnd. Guard the asymmetry.
        assert_ne!(
            layout_justify_items_to_taffy(CssPropertyValue::Exact(LayoutJustifyItems::Start)),
            layout_align_items_to_taffy(CssPropertyValue::Exact(LayoutAlignItems::Start))
        );
    }

    #[test]
    fn azul_overflow_to_taffy_treats_auto_as_scroll_and_everything_unset_as_visible() {
        assert_eq!(
            azul_overflow_to_taffy(MultiValue::Exact(LayoutOverflow::Visible)),
            taffy::Overflow::Visible
        );
        assert_eq!(
            azul_overflow_to_taffy(MultiValue::Exact(LayoutOverflow::Hidden)),
            taffy::Overflow::Hidden
        );
        assert_eq!(
            azul_overflow_to_taffy(MultiValue::Exact(LayoutOverflow::Scroll)),
            taffy::Overflow::Scroll
        );
        assert_eq!(
            azul_overflow_to_taffy(MultiValue::Exact(LayoutOverflow::Clip)),
            taffy::Overflow::Clip
        );
        // Taffy has no `auto`; `auto` constrains the box exactly like `scroll`.
        assert_eq!(
            azul_overflow_to_taffy(MultiValue::Exact(LayoutOverflow::Auto)),
            taffy::Overflow::Scroll
        );
        for v in [MultiValue::Auto, MultiValue::Initial, MultiValue::Inherit] {
            assert_eq!(azul_overflow_to_taffy(v), taffy::Overflow::Visible);
        }
    }

    // ==================================================================
    // css_width_to_px / css_height_to_px
    // ==================================================================

    #[test]
    fn css_width_and_height_to_px_only_resolve_absolute_px_lengths() {
        assert_eq!(
            css_width_to_px(&LayoutWidth::Px(PixelValue::px(120.0))),
            Some(120.0)
        );
        assert_eq!(
            css_height_to_px(&LayoutHeight::Px(PixelValue::px(120.0))),
            Some(120.0)
        );
        // em/rem go through the 16px fallback rather than returning None.
        assert_eq!(
            css_width_to_px(&LayoutWidth::Px(PixelValue::em(2.0))),
            Some(32.0)
        );
        assert_eq!(
            css_height_to_px(&LayoutHeight::Px(PixelValue::rem(2.0))),
            Some(32.0)
        );
        // …but a percentage width has no containing block here.
        assert_eq!(
            css_width_to_px(&LayoutWidth::Px(PixelValue::percent(50.0))),
            None
        );
        assert_eq!(
            css_height_to_px(&LayoutHeight::Px(PixelValue::percent(50.0))),
            None
        );
    }

    #[test]
    fn css_width_and_height_to_px_return_none_for_every_non_px_variant() {
        let widths = [
            LayoutWidth::Auto,
            LayoutWidth::MinContent,
            LayoutWidth::MaxContent,
            LayoutWidth::FitContent(PixelValue::px(10.0)),
            LayoutWidth::Calc(CalcAstItemVec::from_vec(vec![CalcAstItem::Value(
                PixelValue::px(10.0),
            )])),
        ];
        for w in &widths {
            assert_eq!(css_width_to_px(w), None, "{w:?}");
        }

        let heights = [
            LayoutHeight::Auto,
            LayoutHeight::MinContent,
            LayoutHeight::MaxContent,
            LayoutHeight::FitContent(PixelValue::px(10.0)),
            LayoutHeight::Calc(CalcAstItemVec::from_vec(Vec::new())),
        ];
        for h in &heights {
            assert_eq!(css_height_to_px(h), None, "{h:?}");
        }
    }

    #[test]
    fn css_width_to_px_never_returns_nan_for_a_nan_pixel_value() {
        let w = css_width_to_px(&LayoutWidth::Px(PixelValue::px(f32::NAN)));
        assert_eq!(w, Some(0.0));
        let h = css_height_to_px(&LayoutHeight::Px(PixelValue::px(f32::INFINITY)));
        assert!(h.expect("px resolves").is_finite());
    }

    // ==================================================================
    // MultiValue<PixelValue> → taffy lengths
    // ==================================================================

    #[test]
    fn multi_value_to_lpa_maps_the_css_wide_keywords_to_auto() {
        for mv in [MultiValue::Auto, MultiValue::Initial, MultiValue::Inherit] {
            assert!(
                multi_value_to_lpa(mv).is_auto(),
                "inset keywords must stay auto"
            );
        }
    }

    #[test]
    fn multi_value_to_lpa_resolves_lengths_percentages_and_falls_back_to_auto() {
        assert_eq!(
            multi_value_to_lpa(MultiValue::Exact(PixelValue::px(0.0))),
            LengthPercentageAuto::length(0.0)
        );
        assert_eq!(
            multi_value_to_lpa(MultiValue::Exact(PixelValue::px(-12.5))),
            LengthPercentageAuto::length(-12.5),
            "negative insets are legal and must not be clamped"
        );
        assert_eq!(
            multi_value_to_lpa(MultiValue::Exact(PixelValue::percent(50.0))),
            LengthPercentageAuto::percent(0.5),
            "taffy percentages are 0..1, azul's are 0..100"
        );
        assert_eq!(
            multi_value_to_lpa(MultiValue::Exact(PixelValue::em(2.0))),
            LengthPercentageAuto::length(32.0)
        );
        // Viewport units resolve to neither a length nor a percent → auto.
        for m in [SizeMetric::Vw, SizeMetric::Vh, SizeMetric::Vmin, SizeMetric::Vmax] {
            assert!(
                multi_value_to_lpa(MultiValue::Exact(PixelValue::from_metric(m, 10.0))).is_auto(),
                "{m:?} is silently dropped to auto"
            );
        }
    }

    #[test]
    fn multi_value_to_lpa_margin_keeps_auto_but_zeroes_the_other_keywords() {
        // The whole point of the margin variant: `auto` survives (flex centering),
        // `initial`/`inherit` become 0 (the CSS initial margin).
        assert!(multi_value_to_lpa_margin(MultiValue::Auto).is_auto());
        assert_eq!(
            multi_value_to_lpa_margin(MultiValue::Initial),
            LengthPercentageAuto::length(0.0)
        );
        assert_eq!(
            multi_value_to_lpa_margin(MultiValue::Inherit),
            LengthPercentageAuto::length(0.0)
        );
        // …which is exactly where it differs from the inset variant.
        assert!(multi_value_to_lpa(MultiValue::Initial).is_auto());
    }

    #[test]
    fn multi_value_to_lpa_margin_falls_back_to_zero_not_auto_for_unresolvable_units() {
        assert_eq!(
            multi_value_to_lpa_margin(MultiValue::Exact(PixelValue::from_metric(
                SizeMetric::Vw,
                10.0
            ))),
            LengthPercentageAuto::length(0.0),
            "an unresolvable margin must not turn into `margin: auto` (it would centre the item)"
        );
        assert_eq!(
            multi_value_to_lpa_margin(MultiValue::Exact(PixelValue::percent(25.0))),
            LengthPercentageAuto::percent(0.25)
        );
        assert_eq!(
            multi_value_to_lpa_margin(MultiValue::Exact(PixelValue::px(-8.0))),
            LengthPercentageAuto::length(-8.0),
            "negative margins are legal CSS"
        );
        assert_eq!(
            multi_value_to_lpa_margin(MultiValue::Exact(PixelValue::px(f32::NAN))),
            LengthPercentageAuto::length(0.0)
        );
    }

    #[test]
    fn multi_value_to_lp_maps_every_keyword_and_unresolvable_unit_to_zero() {
        for mv in [MultiValue::Auto, MultiValue::Initial, MultiValue::Inherit] {
            assert_eq!(multi_value_to_lp(mv), LengthPercentage::ZERO);
        }
        for m in CONTEXTUAL_METRICS.iter().filter(|m| **m != SizeMetric::Percent) {
            assert_eq!(
                multi_value_to_lp(MultiValue::Exact(PixelValue::from_metric(*m, 10.0))),
                LengthPercentage::ZERO,
                "{m:?}"
            );
        }
        assert_eq!(
            multi_value_to_lp(MultiValue::Exact(PixelValue::px(4.0))),
            LengthPercentage::length(4.0)
        );
        assert_eq!(
            multi_value_to_lp(MultiValue::Exact(PixelValue::percent(10.0))),
            LengthPercentage::percent(0.1)
        );
    }

    #[test]
    fn pixel_to_lp_agrees_with_multi_value_to_lp_on_every_exact_value() {
        let values = [
            PixelValue::px(0.0),
            PixelValue::px(12.0),
            PixelValue::px(-12.0),
            PixelValue::px(f32::NAN),
            PixelValue::px(f32::INFINITY),
            PixelValue::em(1.5),
            PixelValue::rem(1.5),
            PixelValue::pt(12.0),
            PixelValue::percent(33.0),
            PixelValue::from_metric(SizeMetric::Vw, 100.0),
            PixelValue::from_metric(SizeMetric::Vmax, 100.0),
        ];
        for pv in values {
            assert_eq!(
                pixel_to_lp(pv),
                multi_value_to_lp(MultiValue::Exact(pv)),
                "{pv:?}"
            );
        }
        assert_eq!(
            pixel_to_lp(PixelValue::from_metric(SizeMetric::Vw, 100.0)),
            LengthPercentage::ZERO
        );
    }

    // ==================================================================
    // from_layout_width / from_layout_height / store_calc_and_make_dimension
    // ==================================================================

    #[allow(clippy::vec_box)] // return type must mirror the production calc_storage (Box = stable element addresses)
    fn empty_calc_storage() -> std::cell::RefCell<Vec<Box<CalcResolveContext>>> {
        std::cell::RefCell::new(Vec::new())
    }

    #[test]
    fn from_layout_width_and_height_agree_on_every_shared_variant() {
        let storage = empty_calc_storage();
        let pairs: [(LayoutWidth, LayoutHeight); 6] = [
            (LayoutWidth::Auto, LayoutHeight::Auto),
            (
                LayoutWidth::Px(PixelValue::px(100.0)),
                LayoutHeight::Px(PixelValue::px(100.0)),
            ),
            (
                LayoutWidth::Px(PixelValue::percent(50.0)),
                LayoutHeight::Px(PixelValue::percent(50.0)),
            ),
            (LayoutWidth::MinContent, LayoutHeight::MinContent),
            (LayoutWidth::MaxContent, LayoutHeight::MaxContent),
            (
                LayoutWidth::FitContent(PixelValue::px(10.0)),
                LayoutHeight::FitContent(PixelValue::px(10.0)),
            ),
        ];
        for (w, h) in pairs {
            assert_eq!(
                from_layout_width(w.clone(), &storage, 16.0, 16.0),
                from_layout_height(h, &storage, 16.0, 16.0),
                "{w:?}"
            );
        }
        assert!(storage.borrow().is_empty(), "no calc() → no storage growth");
    }

    #[test]
    fn from_layout_width_maps_the_intrinsic_keywords_to_auto() {
        let storage = empty_calc_storage();
        for v in [
            LayoutWidth::Auto,
            LayoutWidth::MinContent,
            LayoutWidth::MaxContent,
            LayoutWidth::FitContent(PixelValue::px(10.0)),
        ] {
            assert_eq!(
                from_layout_width(v.clone(), &storage, 16.0, 16.0),
                Dimension::auto(),
                "{v:?} is not forwarded to taffy — it becomes auto"
            );
        }
    }

    #[test]
    fn from_layout_width_resolves_lengths_and_percentages_and_defaults_to_auto() {
        let storage = empty_calc_storage();
        assert_eq!(
            from_layout_width(LayoutWidth::Px(PixelValue::px(0.0)), &storage, 16.0, 16.0),
            Dimension::length(0.0)
        );
        assert_eq!(
            from_layout_width(LayoutWidth::Px(PixelValue::px(-50.0)), &storage, 16.0, 16.0),
            Dimension::length(-50.0),
            "a negative width is nonsense CSS, but the bridge passes it straight through"
        );
        assert_eq!(
            from_layout_width(
                LayoutWidth::Px(PixelValue::percent(100.0)),
                &storage,
                16.0,
                16.0
            ),
            Dimension::percent(1.0)
        );
        assert_eq!(
            from_layout_height(LayoutHeight::Px(PixelValue::em(3.0)), &storage, 16.0, 16.0),
            Dimension::length(48.0),
            "em uses the 16px fallback, NOT the em_size argument"
        );
        // Viewport units cannot be resolved here → auto.
        assert_eq!(
            from_layout_width(
                LayoutWidth::Px(PixelValue::from_metric(SizeMetric::Vw, 100.0)),
                &storage,
                16.0,
                16.0
            ),
            Dimension::auto()
        );
        // NaN is already flattened to 0 by PixelValue itself.
        assert_eq!(
            from_layout_width(LayoutWidth::Px(PixelValue::px(f32::NAN)), &storage, 16.0, 16.0),
            Dimension::length(0.0)
        );
        let huge = from_layout_height(
            LayoutHeight::Px(PixelValue::px(f32::INFINITY)),
            &storage,
            16.0,
            16.0,
        );
        assert!(huge.value().is_finite(), "an infinite height reached taffy");
    }

    #[test]
    fn from_layout_width_ignores_the_font_sizes_for_non_calc_values() {
        // em_size/rem_size are only wired into the calc() context; the Px path uses
        // the hard-coded 16px fallback. Passing NaN font sizes must therefore not
        // corrupt a plain `width: 2em`.
        let storage = empty_calc_storage();
        assert_eq!(
            from_layout_width(
                LayoutWidth::Px(PixelValue::em(2.0)),
                &storage,
                f32::NAN,
                f32::INFINITY
            ),
            Dimension::length(32.0)
        );
    }

    #[test]
    fn from_layout_width_calc_produces_a_calc_dimension_and_pins_the_context() {
        let storage = empty_calc_storage();
        let items = CalcAstItemVec::from_vec(vec![
            CalcAstItem::Value(PixelValue::percent(100.0)),
            CalcAstItem::Sub,
            CalcAstItem::Value(PixelValue::px(20.0)),
        ]);
        let d = from_layout_width(LayoutWidth::Calc(items), &storage, 20.0, 10.0);
        let raw = d.into_raw();
        assert!(raw.is_calc(), "calc() must reach taffy as a calc Dimension");
        assert_eq!(storage.borrow().len(), 1);

        // SAFETY: the Box is still owned by `storage`, exactly as during a layout pass.
        let ctx = unsafe { &*raw.calc_value().cast::<CalcResolveContext>() };
        assert_eq!(ctx.em_size, 20.0);
        assert_eq!(ctx.rem_size, 10.0);
        assert_eq!(ctx.items.as_ref().len(), 3);
    }

    #[test]
    fn store_calc_and_make_dimension_keeps_every_pointer_valid_across_vec_growth() {
        // The whole reason for Box<CalcResolveContext>: the outer Vec reallocates
        // many times while taffy is still holding raw pointers into it.
        let storage = empty_calc_storage();
        let dims: Vec<Dimension> = (0..256usize)
            .map(|i| {
                let items = CalcAstItemVec::from_vec(vec![CalcAstItem::Value(PixelValue::px(
                    i as f32,
                ))]);
                store_calc_and_make_dimension(items, &storage, i as f32, 16.0)
            })
            .collect();

        assert_eq!(storage.borrow().len(), 256);
        for (i, d) in dims.iter().enumerate() {
            let raw = d.into_raw();
            assert!(raw.is_calc(), "#{i} is not a calc dimension");
            // SAFETY: every Box is still alive in `storage`.
            let ctx = unsafe { &*raw.calc_value().cast::<CalcResolveContext>() };
            assert_eq!(
                ctx.em_size, i as f32,
                "context #{i} moved when the Vec reallocated"
            );
            assert_eq!(ctx.rem_size, 16.0);
            assert_eq!(ctx.items.as_ref().len(), 1);
        }
    }

    #[test]
    fn store_calc_and_make_dimension_accepts_an_empty_ast_and_nan_font_sizes() {
        let storage = empty_calc_storage();
        let d = store_calc_and_make_dimension(
            CalcAstItemVec::from_vec(Vec::new()),
            &storage,
            f32::NAN,
            f32::INFINITY,
        );
        let raw = d.into_raw();
        assert!(raw.is_calc());
        assert_eq!(storage.borrow().len(), 1);
        // SAFETY: the Box is still owned by `storage`.
        let ctx = unsafe { &*raw.calc_value().cast::<CalcResolveContext>() };
        assert!(ctx.items.as_ref().is_empty());
        assert!(
            ctx.em_size.is_nan() && ctx.rem_size.is_infinite(),
            "the font sizes are stored verbatim — evaluate_calc has to cope"
        );
    }

    #[test]
    fn store_calc_and_make_dimension_hands_out_a_distinct_pointer_per_call() {
        let storage = empty_calc_storage();
        let a = store_calc_and_make_dimension(
            CalcAstItemVec::from_vec(Vec::new()),
            &storage,
            1.0,
            1.0,
        );
        let b = store_calc_and_make_dimension(
            CalcAstItemVec::from_vec(Vec::new()),
            &storage,
            2.0,
            2.0,
        );
        assert_ne!(
            a.into_raw().calc_value(),
            b.into_raw().calc_value(),
            "two calc() values must not alias the same context"
        );
        assert_ne!(a, b);
    }

    // ==================================================================
    // compute_taffy_scrollbar_info — needs a real StyledDom + LayoutTree
    // ==================================================================

    #[cfg(all(feature = "text_layout", feature = "font_loading"))]
    mod with_layout_context {
        use azul_core::{
            dom::{Dom, DomId},
            selection::TextSelection,
        };
        use azul_css::{props::basic::FontRef, LayoutDebugMessage};

        use super::*;
        use crate::{
            font_traits::FontManager,
            solver3::{cache, layout_tree::generate_layout_tree},
        };

        /// Owns everything a `LayoutContext` borrows.
        struct Env {
            styled_dom: StyledDom,
            font_manager: FontManager<FontRef>,
            text_selections: BTreeMap<DomId, TextSelection>,
            counters: HashMap<(usize, String), i32>,
            image_cache: azul_core::resources::ImageCache,
            debug_messages: Option<Vec<LayoutDebugMessage>>,
        }

        impl Env {
            fn new() -> Self {
                let mut dom = Dom::create_body();
                let (css, _warnings) = azul_css::parser2::new_from_str("");
                Self {
                    styled_dom: StyledDom::create(&mut dom, css),
                    font_manager: FontManager::new(rust_fontconfig::FcFontCache::default())
                        .expect("FontManager over an empty font cache"),
                    text_selections: BTreeMap::new(),
                    counters: HashMap::new(),
                    image_cache: azul_core::resources::ImageCache::default(),
                    debug_messages: None,
                }
            }

            fn ctx(&mut self) -> LayoutContext<'_, FontRef> {
                LayoutContext {
                    scrollbar_style_cache: core::cell::RefCell::new(HashMap::new()),
                    styled_dom: &self.styled_dom,
                    font_manager: &self.font_manager,
                    text_selections: &self.text_selections,
                    debug_messages: &mut self.debug_messages,
                    counters: &mut self.counters,
                    viewport_size: LogicalSize::new(800.0, 600.0),
                    fragmentation_context: None,
                    cursor_is_visible: true,
                    cursor_locations: Vec::new(),
                    preedit_text: None,
                    dirty_text_overrides: BTreeMap::new(),
                    cache_map: cache::LayoutCacheMap::default(),
                    image_cache: &self.image_cache,
                    system_style: None,
                    get_system_time_fn: azul_core::task::GetSystemTimeCallback {
                        cb: azul_core::task::get_system_time_libstd,
                    },
                }
            }
        }

        #[test]
        fn compute_taffy_scrollbar_info_returns_defaults_for_an_out_of_range_node() {
            let mut env = Env::new();
            let mut ctx = env.ctx();
            let tree = generate_layout_tree(&mut ctx).expect("a plain body dom builds");

            for idx in [usize::MAX, tree.nodes.len(), tree.nodes.len() + 1] {
                let (info, w, h) =
                    compute_taffy_scrollbar_info(&ctx, &tree, idx, 100.0, 100.0, 500.0, 500.0);
                assert!(!info.needs_horizontal, "#{idx}");
                assert!(!info.needs_vertical, "#{idx}");
                assert_eq!(w, 0.0);
                assert_eq!(h, 0.0);
            }
        }

        #[test]
        fn compute_taffy_scrollbar_info_never_reports_a_negative_or_nan_content_size() {
            let mut env = Env::new();
            let mut ctx = env.ctx();
            let tree = generate_layout_tree(&mut ctx).expect("a plain body dom builds");
            let root = tree.root;

            let extremes = [
                0.0f32,
                -1.0,
                f32::NAN,
                f32::INFINITY,
                f32::NEG_INFINITY,
                f32::MAX,
                f32::MIN,
            ];
            for v in extremes {
                for c in extremes {
                    let (_info, w, h) =
                        compute_taffy_scrollbar_info(&ctx, &tree, root, v, v, c, c);
                    assert!(
                        !w.is_nan() && w >= 0.0,
                        "result={v} content={c} → content width {w}"
                    );
                    assert!(
                        !h.is_nan() && h >= 0.0,
                        "result={v} content={c} → content height {h}"
                    );
                }
            }
        }

        #[test]
        fn compute_taffy_scrollbar_info_needs_no_scrollbars_for_an_overflow_visible_body() {
            let mut env = Env::new();
            let mut ctx = env.ctx();
            let tree = generate_layout_tree(&mut ctx).expect("a plain body dom builds");
            let root = tree.root;

            // Content far larger than the box: `overflow: visible` still must not
            // ask for scrollbars (only `auto`/`scroll` do).
            let (info, w, h) =
                compute_taffy_scrollbar_info(&ctx, &tree, root, 100.0, 100.0, 10_000.0, 10_000.0);
            assert!(!info.needs_horizontal);
            assert!(!info.needs_vertical);
            assert_eq!(info.scrollbar_width, 0.0);
            assert_eq!(info.scrollbar_height, 0.0);
            assert!(w > 0.0 && h > 0.0, "the taffy content size is passed back");
        }
    }
}