package toffee

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

Source file compute_flexbox.ml

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
(* Flexbox layout computation algorithm *)

open Geometry
open Style
open Tree

(* Flex direction helpers *)
module Flex_direction_ext = struct
  open Flex_direction

  let is_row dir =
    match dir with
    | Row | Row_reverse -> true
    | Column | Column_reverse -> false

  let _is_column dir = not (is_row dir)

  let is_reverse dir =
    match dir with
    | Row_reverse | Column_reverse -> true
    | Row | Column -> false

  let main_size size dir =
    if is_row dir then size.Size.width else size.Size.height

  let _cross_size size dir =
    if is_row dir then size.Size.height else size.Size.width

  let _with_cross_size size dir value =
    if is_row dir then Size.{ size with height = value }
    else Size.{ size with width = value }

  let main_axis_sum rect dir =
    if is_row dir then Rect.horizontal_axis_sum rect
    else Rect.vertical_axis_sum rect

  let _cross_axis_sum rect dir =
    if is_row dir then Rect.vertical_axis_sum rect
    else Rect.horizontal_axis_sum rect

  (* Rect accessors based on flex direction *)
  let main_start rect dir = if is_row dir then rect.Rect.left else rect.Rect.top

  let main_end rect dir =
    if is_row dir then rect.Rect.right else rect.Rect.bottom

  let cross_start rect dir =
    if is_row dir then rect.Rect.top else rect.Rect.left

  let cross_end rect dir =
    if is_row dir then rect.Rect.bottom else rect.Rect.right

  (* Point accessors based on flex direction *)
  let _main_axis_pt pt dir = if is_row dir then pt.Point.x else pt.Point.y
  let _cross_axis_pt pt dir = if is_row dir then pt.Point.y else pt.Point.x
end

(* The intermediate results of a flexbox calculation for a single item *)
type flex_item = {
  node : Node_id.t; (* The identifier for the associated node *)
  order : int; (* The order of the node relative to its siblings *)
  (* Size constraints *)
  size : float option size; (* The base size of this item *)
  min_size : float option size; (* The minimum allowable size of this item *)
  max_size : float option size; (* The maximum allowable size of this item *)
  align_self : align_self; (* The cross-alignment of this item *)
  (* Overflow and scrollbar *)
  overflow : overflow point; (* The overflow style of the item *)
  scrollbar_width : float; (* The width of the scrollbars (if it has any) *)
  (* Flex properties *)
  flex_shrink : float; (* The flex shrink style of the item *)
  flex_grow : float; (* The flex grow style of the item *)
  (* Resolved sizes *)
  mutable resolved_minimum_main_size : float; (* The minimum size of the item *)
  (* Positioning *)
  inset : float option rect; (* The final offset of this item *)
  mutable margin : float rect; (* The margin of this item *)
  margin_is_auto : bool rect; (* Whether each margin is an auto margin or not *)
  padding : float rect; (* The padding of this item *)
  border : float rect; (* The border of this item *)
  (* Flex basis *)
  mutable flex_basis : float; (* The default size of this item *)
  mutable inner_flex_basis : float;
  (* The default size of this item, minus padding and border *)
  (* Layout state *)
  mutable violation : float;
      (* The amount by which this item has deviated from its target size *)
  mutable frozen : bool; (* Is the size of this item locked *)
  (* Content flex fraction *)
  mutable content_flex_fraction : float;
  (* Either the max- or min- content flex fraction *)
  (* Hypothetical and target sizes (flattened) *)
  mutable hypothetical_inner_width : float;
  mutable hypothetical_inner_height : float;
  mutable hypothetical_outer_width : float;
  mutable hypothetical_outer_height : float;
  mutable target_width : float; (* The size that this item wants to be *)
  mutable target_height : float;
  mutable outer_target_width : float;
  mutable outer_target_height : float;
  (* Baseline *)
  mutable baseline : float; (* The position of the bottom edge of this item *)
  (* Offsets *)
  mutable offset_main : float; (* A temporary value for the main offset *)
  mutable offset_cross : float; (* A temporary value for the cross offset *)
}

(* A line of flex_items used for intermediate computation *)
type flex_line = {
  items : flex_item array;
      (* The slice of items to iterate over during computation of this line *)
  mutable cross_size : float; (* The dimensions of the cross-axis *)
  mutable offset_cross : float; (* The relative offset of the cross-axis *)
}

(* Values that can be cached during the flexbox algorithm *)
type algo_constants = {
  (* Direction *)
  dir : flex_direction;
      (* The direction of the current segment being laid out *)
  is_row : bool; (* Is this segment a row *)
  is_wrap : bool; (* Is wrapping enabled (in either direction) *)
  is_wrap_reverse : bool; (* Is the wrap direction inverted *)
  (* Size constraints *)
  min_size : float option size; (* The item's min_size style *)
  max_size : float option size; (* The item's max_size style *)
  (* Box model *)
  border : float rect; (* The border of this section *)
  content_box_inset : float rect;
      (* The space between the content box and the border box *)
  scrollbar_gutter : float point;
  (* The size reserved for scrollbar gutters in each axis *)
  (* Layout properties *)
  mutable gap : float size; (* The gap of this section *)
  align_items : align_items; (* The align_items property of this node *)
  align_content : align_content; (* The align_content property of this node *)
  justify_content : justify_content option;
  (* The justify_content property of this node *)
  (* Computed sizes *)
  mutable node_outer_size : float option size;
      (* The border-box size of the node being laid out (if known) *)
  mutable node_inner_size : float option size;
      (* The content-box size of the node being laid out (if known) *)
  mutable container_size : float size;
      (* The size of the virtual container containing the flex items *)
  mutable inner_container_size : float size;
      (* The size of the internal container *)
}

(* Helper functions *)
let is_scroll_container (item : flex_item) : bool =
  Overflow.is_container item.overflow.x || Overflow.is_container item.overflow.y

(* Compute algorithm constants from style *)
let compute_constants (type t)
    (module Tree : Tree.LAYOUT_PARTIAL_TREE with type t = t) (tree : t)
    (style : Style.t) (known_dimensions : float option size)
    (parent_size : float option size) : algo_constants =
  let dir = Style.flex_direction style in
  let is_row = Flex_direction_ext.is_row dir in
  let is_wrap =
    match Style.flex_wrap style with
    | Wrap | Wrap_reverse -> true
    | No_wrap -> false
  in
  let is_wrap_reverse = Style.flex_wrap style = Wrap_reverse in
  let aspect_ratio = Style.aspect_ratio style in
  let calc = Tree.resolve_calc_value tree in
  let padding =
    Style.padding style
    |> Rect.map (fun lp ->
        Length_percentage.resolve_or_zero lp parent_size.width calc)
  in
  let border =
    Style.border style
    |> Rect.map (fun lp ->
        Length_percentage.resolve_or_zero lp parent_size.width calc)
  in

  let padding_border_sum = Rect.sum_axes (Rect.add padding border) in
  let box_sizing_adjustment =
    if Style.box_sizing style = Box_sizing.Content_box then padding_border_sum
    else Size.zero
  in

  (* Handle overflow and scrollbar gutters *)
  let overflow = Style.overflow style in
  let scrollbar_gutter =
    Point.
      {
        x =
          (if overflow.y = Overflow.Scroll then Style.scrollbar_width style
           else 0.0);
        y =
          (if overflow.x = Overflow.Scroll then Style.scrollbar_width style
           else 0.0);
      }
  in

  (* Content box inset = padding + border + scrollbar gutter *)
  let content_box_inset =
    Rect.add (Rect.add padding border)
      Rect.
        {
          top = 0.0;
          left = 0.0;
          right = scrollbar_gutter.x;
          bottom = scrollbar_gutter.y;
        }
  in

  let node_outer_size = known_dimensions in
  let node_inner_size =
    Size.sub_option node_outer_size
      (Size.map Option.some (Rect.sum_axes content_box_inset))
  in

  (* Resolve gap *)
  let gap_base =
    match node_inner_size with
    | { width = Some w; height = Some h } -> Size.{ width = w; height = h }
    | _ -> Size.zero
  in
  let gap =
    Style.gap style |> fun gap_size ->
    Size.
      {
        width =
          Length_percentage.resolve_or_zero (Size.get Inline gap_size)
            (Some gap_base.width) calc;
        height =
          Length_percentage.resolve_or_zero (Size.get Block gap_size)
            (Some gap_base.height) calc;
      }
  in

  (* Resolve min/max sizes *)
  let min_size =
    Style.min_size style |> fun dims ->
    Size.
      {
        width =
          Dimension.maybe_resolve (Size.get Inline dims) parent_size.width calc;
        height =
          Dimension.maybe_resolve (Size.get Block dims) parent_size.height calc;
      }
    |> Size.apply_aspect_ratio aspect_ratio
    |> Size.maybe_add box_sizing_adjustment
  in

  let max_size =
    Style.max_size style |> fun dims ->
    Size.
      {
        width =
          Dimension.maybe_resolve (Size.get Inline dims) parent_size.width calc;
        height =
          Dimension.maybe_resolve (Size.get Block dims) parent_size.height calc;
      }
    |> Size.apply_aspect_ratio aspect_ratio
    |> Size.maybe_add box_sizing_adjustment
  in

  {
    dir;
    is_row;
    is_wrap;
    is_wrap_reverse;
    min_size;
    max_size;
    border;
    content_box_inset;
    scrollbar_gutter;
    gap;
    align_items =
      Option.value (Style.align_items style) ~default:Align_items.Stretch;
    align_content =
      Option.value (Style.align_content style) ~default:Align_content.Stretch;
    justify_content = Style.justify_content style;
    node_outer_size;
    node_inner_size;
    container_size = Size.zero;
    inner_container_size = Size.zero;
  }

(* Generate anonymous flex items *)
let generate_anonymous_flex_items (type t)
    (module Tree : Tree.LAYOUT_PARTIAL_TREE with type t = t) (tree : t)
    (node : Node_id.t) (constants : algo_constants) : flex_item array =
  let calc = Tree.resolve_calc_value tree in
  let keep_child style =
    Style.position style <> Position.Absolute
    && Style.box_generation_mode style <> Box_generation_mode.None
  in
  let child_count = Tree.child_count tree node in
  (* First pass: count kept children *)
  let kept = ref 0 in
  for i = 0 to child_count - 1 do
    let child = Tree.get_child_id tree node i in
    let style = Tree.get_core_container_style tree child in
    if keep_child style then incr kept
  done;
  let items = if !kept = 0 then [||] else Array.make !kept (Obj.magic 0) in
  let insert_at = ref 0 in
  for index = 0 to child_count - 1 do
    let child_node = Tree.get_child_id tree node index in
    let child_style = Tree.get_core_container_style tree child_node in
    if keep_child child_style then (
      let aspect_ratio = Style.aspect_ratio child_style in
      let padding =
        Style.padding child_style
        |> Rect.map (fun lp ->
            Length_percentage.resolve_or_zero lp constants.node_inner_size.width
              calc)
      in
      let border =
        Style.border child_style
        |> Rect.map (fun lp ->
            Length_percentage.resolve_or_zero lp constants.node_inner_size.width
              calc)
      in
      let pb_sum = Rect.sum_axes (Rect.add padding border) in
      let box_sizing_adjustment =
        if Style.box_sizing child_style = Box_sizing.Content_box then pb_sum
        else Size.zero
      in

      let size =
        Style.size child_style |> fun dims ->
        Size.
          {
            width =
              Dimension.maybe_resolve (Size.get Inline dims)
                constants.node_inner_size.width calc;
            height =
              Dimension.maybe_resolve (Size.get Block dims)
                constants.node_inner_size.height calc;
          }
        |> Size.apply_aspect_ratio aspect_ratio
        |> Size.maybe_add box_sizing_adjustment
      in

      let min_size =
        Style.min_size child_style |> fun dims ->
        Size.
          {
            width =
              Dimension.maybe_resolve (Size.get Inline dims)
                constants.node_inner_size.width calc;
            height =
              Dimension.maybe_resolve (Size.get Block dims)
                constants.node_inner_size.height calc;
          }
        |> Size.apply_aspect_ratio aspect_ratio
        |> Size.maybe_add box_sizing_adjustment
      in

      let max_size =
        Style.max_size child_style |> fun dims ->
        Size.
          {
            width =
              Dimension.maybe_resolve (Size.get Inline dims)
                constants.node_inner_size.width calc;
            height =
              Dimension.maybe_resolve (Size.get Block dims)
                constants.node_inner_size.height calc;
          }
        |> Size.apply_aspect_ratio aspect_ratio
        |> Size.maybe_add box_sizing_adjustment
      in

      let inset =
        Style.inset child_style
        |> Rect.zip_size constants.node_inner_size (fun p s ->
            Length_percentage_auto.maybe_resolve p s calc)
      in

      let margin =
        Style.margin child_style
        |> Rect.map (fun lpa ->
            Length_percentage_auto.resolve_or_zero lpa
              constants.node_inner_size.width calc)
      in

      let margin_is_auto =
        Style.margin child_style |> Rect.map Length_percentage_auto.is_auto
      in

      let align_self =
        match Style.align_self child_style with
        | Some align -> align
        | None -> constants.align_items
      in

      let item =
        {
          node = child_node;
          order = index;
          size;
          min_size;
          max_size;
          align_self;
          overflow = Style.overflow child_style;
          scrollbar_width = Style.scrollbar_width child_style;
          flex_grow = Style.flex_grow child_style;
          flex_shrink = Style.flex_shrink child_style;
          flex_basis = 0.0;
          inner_flex_basis = 0.0;
          violation = 0.0;
          frozen = false;
          resolved_minimum_main_size = 0.0;
          inset;
          margin;
          margin_is_auto;
          padding;
          border;
          hypothetical_inner_width = 0.0;
          hypothetical_inner_height = 0.0;
          hypothetical_outer_width = 0.0;
          hypothetical_outer_height = 0.0;
          target_width = 0.0;
          target_height = 0.0;
          outer_target_width = 0.0;
          outer_target_height = 0.0;
          content_flex_fraction = 0.0;
          baseline = 0.0;
          offset_main = 0.0;
          offset_cross = 0.0;
        }
      in
      items.(!insert_at) <- item;
      incr insert_at)
  done;
  items

(* Determine available space *)
let determine_available_space (known_dimensions : float option size)
    (available_space : Available_space.t size) (constants : algo_constants) :
    Available_space.t size =
  (* Note: min/max/preferred size styles have already been applied to
     known_dimensions in the compute function above *)
  let width =
    match known_dimensions.width with
    | Some node_width ->
        Available_space.of_float
          (node_width -. Rect.horizontal_axis_sum constants.content_box_inset)
    | None ->
        Available_space.sub available_space.width
          (Rect.horizontal_axis_sum constants.content_box_inset)
  in

  let height =
    match known_dimensions.height with
    | Some node_height ->
        Available_space.of_float
          (node_height -. Rect.vertical_axis_sum constants.content_box_inset)
    | None ->
        Available_space.sub available_space.height
          (Rect.vertical_axis_sum constants.content_box_inset)
  in

  Size.{ width; height }

(* Determine flex base size *)
let determine_flex_base_size (type t)
    (module Tree : Tree.LAYOUT_PARTIAL_TREE with type t = t) (tree : t)
    (constants : algo_constants) (available_space : Available_space.t size)
    (flex_items : flex_item array) : unit =
  let calc = Tree.resolve_calc_value tree in

  Array.iter
    (fun child ->
      let child_style = Tree.get_core_container_style tree child.node in

      (* Parent size for child sizing *)
      let cross_axis_parent_size =
        if constants.is_row then constants.node_inner_size.height
        else constants.node_inner_size.width
      in
      let child_parent_size =
        if constants.is_row then
          Size.{ width = None; height = cross_axis_parent_size }
        else Size.{ width = cross_axis_parent_size; height = None }
      in

      (* Available space for child sizing *)
      let cross_axis_margin_sum =
        if constants.is_row then child.margin.top +. child.margin.bottom
        else child.margin.left +. child.margin.right
      in

      let child_min_cross =
        (if constants.is_row then child.min_size.height
         else child.min_size.width)
        |> Option.map (fun v -> v +. cross_axis_margin_sum)
      in
      let child_max_cross =
        (if constants.is_row then child.max_size.height
         else child.max_size.width)
        |> Option.map (fun v -> v +. cross_axis_margin_sum)
      in

      (* Clamp available space by min- and max- size *)
      let cross_axis_available_space =
        let available_cross =
          if constants.is_row then available_space.height
          else available_space.width
        in
        match available_cross with
        | Available_space.Definite val_ ->
            let clamped =
              Option.value cross_axis_parent_size ~default:val_ |> fun v ->
              match (child_min_cross, child_max_cross) with
              | Some min, Some max -> Float.max min (Float.min v max)
              | Some min, None -> Float.max min v
              | None, Some max -> Float.min v max
              | None, None -> v
            in
            Available_space.of_float clamped
        | Available_space.Min_content -> (
            match child_min_cross with
            | Some min -> Available_space.of_float min
            | None -> Available_space.min_content)
        | Available_space.Max_content -> (
            match child_max_cross with
            | Some max -> Available_space.of_float max
            | None -> Available_space.max_content)
      in

      (* Known dimensions for child sizing *)
      let child_known_dimensions =
        let base_size =
          if constants.is_row then
            { Size.width = None; height = child.size.height }
          else { Size.width = child.size.width; height = None }
        in
        (* Handle stretch alignment *)
        let cross_dim =
          if constants.is_row then base_size.height else base_size.width
        in
        if child.align_self = Stretch && cross_dim = None then
          let cross_size =
            Available_space.to_option cross_axis_available_space
            |> Option.map (fun v -> v -. cross_axis_margin_sum)
          in
          if constants.is_row then { base_size with height = cross_size }
          else { base_size with width = cross_size }
        else base_size
      in

      (* Get container width for resolving flex basis *)
      let container_width =
        if constants.is_row then constants.node_inner_size.width
        else constants.node_inner_size.height
      in

      (* Compute box sizing adjustment *)
      let box_sizing_adjustment =
        if Style.box_sizing child_style = Box_sizing.Content_box then
          let padding_main =
            if constants.is_row then child.padding.left +. child.padding.right
            else child.padding.top +. child.padding.bottom
          in
          let border_main =
            if constants.is_row then child.border.left +. child.border.right
            else child.border.top +. child.border.bottom
          in
          padding_main +. border_main
        else 0.0
      in

      (* Resolve flex basis *)
      let flex_basis_style =
        Style.flex_basis child_style |> fun fb ->
        Dimension.maybe_resolve fb container_width calc
        |> Option.map (fun v -> v +. box_sizing_adjustment)
      in

      (* Compute flex basis according to spec *)
      child.flex_basis <-
        (* A. If the item has a definite used flex basis, that's the flex base
           size. *)
        (let main_size =
           if constants.is_row then child.size.width else child.size.height
         in
         match (flex_basis_style, main_size) with
         | Some fb, _ -> fb
         | None, Some ms -> ms
         | None, None ->
             (* E. Otherwise, size the item into the available space *)
             let child_available_space =
               let main_available =
                 match
                   if constants.is_row then available_space.width
                   else available_space.height
                 with
                 | Available_space.Min_content -> Available_space.min_content
                 | _ -> Available_space.max_content
               in
               if constants.is_row then
                 Size.
                   {
                     width = main_available;
                     height = cross_axis_available_space;
                   }
               else
                 Size.
                   {
                     width = cross_axis_available_space;
                     height = main_available;
                   }
             in

             let layout_output =
               Tree.compute_child_layout tree child.node
                 (Layout_input.make ~run_mode:Run_mode.Compute_size
                    ~sizing_mode:Sizing_mode.Content_size
                    ~axis:
                      (if constants.is_row then Requested_axis.Horizontal
                       else Requested_axis.Vertical)
                    ~known_dimensions:child_known_dimensions
                    ~parent_size:child_parent_size
                    ~available_space:child_available_space
                    ~vertical_margins_are_collapsible:Line.both_false)
             in

             let measured_size = Layout_output.size layout_output in
             if constants.is_row then measured_size.width
             else measured_size.height);

      (* Floor flex-basis by the padding_border_sum *)
      let padding_border_sum =
        if constants.is_row then
          child.padding.left +. child.padding.right +. child.border.left
          +. child.border.right
        else
          child.padding.top +. child.padding.bottom +. child.border.top
          +. child.border.bottom
      in
      child.flex_basis <- Float.max child.flex_basis padding_border_sum;

      (* Compute inner flex basis *)
      child.inner_flex_basis <- child.flex_basis -. padding_border_sum;

      (* Compute resolved minimum main size *)
      let style_min_main_size =
        let min_from_style =
          if constants.is_row then child.min_size.width
          else child.min_size.height
        in
        let auto_min =
          let overflow_main =
            if constants.is_row then child.overflow.x else child.overflow.y
          in
          match Overflow.to_automatic_min_size overflow_main with
          | dim when Dimension.is_auto dim -> None
          | dim -> Dimension.to_option dim
        in
        match (min_from_style, auto_min) with
        | Some v, _ -> Some v
        | None, Some v -> Some v
        | None, None -> None
      in

      child.resolved_minimum_main_size <-
        (match style_min_main_size with
        | Some v -> v
        | None ->
            (* Compute min-content size *)
            let child_available_space =
              let cross_available =
                if constants.is_row then
                  Size.
                    {
                      width = Available_space.min_content;
                      height = cross_axis_available_space;
                    }
                else
                  Size.
                    {
                      width = cross_axis_available_space;
                      height = Available_space.min_content;
                    }
              in
              cross_available
            in

            let layout_output =
              Tree.compute_child_layout tree child.node
                (Layout_input.make ~run_mode:Run_mode.Compute_size
                   ~sizing_mode:Sizing_mode.Content_size
                   ~axis:
                     (if constants.is_row then Requested_axis.Horizontal
                      else Requested_axis.Vertical)
                   ~known_dimensions:child_known_dimensions
                   ~parent_size:child_parent_size
                   ~available_space:child_available_space
                   ~vertical_margins_are_collapsible:Line.both_false)
            in

            let min_content_main_size =
              let measured_size = Layout_output.size layout_output in
              if constants.is_row then measured_size.width
              else measured_size.height
            in

            (* Apply automatic minimum size clamping *)
            let clamped_min =
              min_content_main_size |> fun v ->
              (match
                 if constants.is_row then child.size.width
                 else child.size.height
               with
                | Some size -> Float.min v size
                | None -> v)
              |> fun v ->
              match
                if constants.is_row then child.max_size.width
                else child.max_size.height
              with
              | Some max -> Float.min v max
              | None -> v
            in
            Float.max clamped_min padding_border_sum);

      (* Compute hypothetical sizes *)
      let hypothetical_inner_min_main =
        Float.max child.resolved_minimum_main_size padding_border_sum
      in
      let hypothetical_inner_width =
        let max_main =
          if constants.is_row then child.max_size.width
          else child.max_size.height
        in
        match max_main with
        | Some max ->
            Float.min child.flex_basis
              (Float.max hypothetical_inner_min_main max)
        | None -> Float.max child.flex_basis hypothetical_inner_min_main
      in
      let hypothetical_outer_width =
        hypothetical_inner_width
        +.
        if constants.is_row then child.margin.left +. child.margin.right
        else child.margin.top +. child.margin.bottom
      in

      if constants.is_row then (
        child.hypothetical_inner_width <- hypothetical_inner_width;
        child.hypothetical_outer_width <- hypothetical_outer_width)
      else (
        child.hypothetical_inner_height <- hypothetical_inner_width;
        child.hypothetical_outer_height <- hypothetical_outer_width))
    flex_items

(* Collect flex items into flex lines *)
let collect_flex_lines (constants : algo_constants)
    (available_space : Available_space.t size) (flex_items : flex_item array) :
    flex_line list =
  if not constants.is_wrap then
    (* Single line - all items go in one line *)
    [
      {
        items = Array.init (Array.length flex_items) (Array.get flex_items);
        cross_size = 0.0;
        offset_cross = 0.0;
      };
    ]
  else
    (* Multi-line wrapping *)
    let main_axis_available_space =
      let max_main =
        if constants.is_row then constants.max_size.width
        else constants.max_size.height
      in
      match max_main with
      | Some max_size ->
          let available_main =
            if constants.is_row then available_space.width
            else available_space.height
          in
          let resolved =
            Available_space.to_option available_main
            |> Option.value ~default:max_size
            |> fun v ->
            let min_main =
              if constants.is_row then constants.min_size.width
              else constants.min_size.height
            in
            match min_main with Some min -> Float.max v min | None -> v
          in
          Available_space.of_float resolved
      | None ->
          if constants.is_row then available_space.width
          else available_space.height
    in

    match main_axis_available_space with
    | Available_space.Max_content ->
        (* Max content - items never wrap *)
        [
          {
            items = Array.init (Array.length flex_items) (Array.get flex_items);
            cross_size = 0.0;
            offset_cross = 0.0;
          };
        ]
    | Available_space.Min_content ->
        (* Min content - each item gets its own line *)
        let lines = ref [] in
        for i = Array.length flex_items - 1 downto 0 do
          let item = flex_items.(i) in
          lines :=
            { items = [| item |]; cross_size = 0.0; offset_cross = 0.0 }
            :: !lines
        done;
        !lines
    | Available_space.Definite main_axis_available_space ->
        (* Definite size - wrap based on available space *)
        let main_axis_gap =
          if constants.is_row then constants.gap.width else constants.gap.height
        in

        (* Process flex items into lines *)
        let rec collect_lines start_index lines =
          if start_index >= Array.length flex_items then List.rev lines
          else
            let line_length = ref 0.0 in
            let rec find_split idx =
              if idx >= Array.length flex_items then idx
              else
                let item = flex_items.(idx) in
                let gap_contribution =
                  if idx = start_index then 0.0 else main_axis_gap
                in
                let item_size =
                  if constants.is_row then item.hypothetical_outer_width
                  else item.hypothetical_outer_height
                in
                let new_length =
                  !line_length +. item_size +. gap_contribution
                in

                if new_length > main_axis_available_space && idx <> start_index
                then idx
                else (
                  line_length := new_length;
                  find_split (idx + 1))
            in

            let split_idx = find_split start_index in
            let split_idx =
              if split_idx = start_index then start_index + 1 else split_idx
            in
            let line_items_len = split_idx - start_index in
            let line_items = Array.sub flex_items start_index line_items_len in

            let line =
              { items = line_items; cross_size = 0.0; offset_cross = 0.0 }
            in

            collect_lines split_idx (line :: lines)
        in

        collect_lines 0 []

(* Helper to sum axis gaps *)
let sum_axis_gaps gap num_items =
  (* Gaps only exist between items *)
  if num_items <= 1 then 0.0 else gap *. float_of_int (num_items - 1)

(* Determine container main size *)
let determine_container_main_size (type t)
    (module Tree : Tree.LAYOUT_PARTIAL_TREE with type t = t) (tree : t)
    (available_space : Available_space.t size) (flex_lines : flex_line list)
    (constants : algo_constants) : unit =
  let main_content_box_inset =
    if constants.is_row then
      Rect.horizontal_axis_sum constants.content_box_inset
    else Rect.vertical_axis_sum constants.content_box_inset
  in

  let outer_main_size =
    let current_main_size =
      if constants.is_row then constants.node_outer_size.width
      else constants.node_outer_size.height
    in
    match current_main_size with
    | Some size -> size
    | None -> (
        let available_main =
          if constants.is_row then available_space.width
          else available_space.height
        in
        match available_main with
        | Available_space.Definite main_axis_available_space ->
            (* Calculate longest line length *)
            let longest_line_length =
              List.fold_left
                (fun max_length line ->
                  let line_main_axis_gap =
                    sum_axis_gaps
                      (if constants.is_row then constants.gap.width
                       else constants.gap.height)
                      (Array.length line.items)
                  in
                  let total_target_size =
                    Array.fold_left
                      (fun sum child ->
                        let padding_border_sum =
                          if constants.is_row then
                            child.padding.left +. child.padding.right
                            +. child.border.left +. child.border.right
                          else
                            child.padding.top +. child.padding.bottom
                            +. child.border.top +. child.border.bottom
                        in
                        let min_main =
                          if constants.is_row then child.min_size.width
                          else child.min_size.height
                        in
                        let base_size =
                          match min_main with
                          | Some min -> Float.max child.flex_basis min
                          | None -> child.flex_basis
                        in
                        let margin_sum =
                          if constants.is_row then
                            child.margin.left +. child.margin.right
                          else child.margin.top +. child.margin.bottom
                        in
                        sum
                        +. Float.max (base_size +. margin_sum)
                             padding_border_sum)
                      0.0 line.items
                  in
                  Float.max max_length (total_target_size +. line_main_axis_gap))
                0.0 flex_lines
            in
            let size = longest_line_length +. main_content_box_inset in
            if List.length flex_lines > 1 then
              Float.max size main_axis_available_space
            else size
        | Available_space.Min_content when constants.is_wrap ->
            (* Min content with wrapping *)
            let longest_line_length =
              List.fold_left
                (fun max_length line ->
                  let line_main_axis_gap =
                    sum_axis_gaps
                      (if constants.is_row then constants.gap.width
                       else constants.gap.height)
                      (Array.length line.items)
                  in
                  let total_target_size =
                    Array.fold_left
                      (fun sum child ->
                        let padding_border_sum =
                          if constants.is_row then
                            child.padding.left +. child.padding.right
                            +. child.border.left +. child.border.right
                          else
                            child.padding.top +. child.padding.bottom
                            +. child.border.top +. child.border.bottom
                        in
                        let min_main =
                          if constants.is_row then child.min_size.width
                          else child.min_size.height
                        in
                        let base_size =
                          match min_main with
                          | Some min -> Float.max child.flex_basis min
                          | None -> child.flex_basis
                        in
                        let margin_sum =
                          if constants.is_row then
                            child.margin.left +. child.margin.right
                          else child.margin.top +. child.margin.bottom
                        in
                        sum
                        +. Float.max (base_size +. margin_sum)
                             padding_border_sum)
                      0.0 line.items
                  in
                  Float.max max_length (total_target_size +. line_main_axis_gap))
                0.0 flex_lines
            in
            longest_line_length +. main_content_box_inset
        | Available_space.Min_content | Available_space.Max_content ->
            (* Content-based sizing *)
            let main_size = ref 0.0 in

            List.iter
              (fun line ->
                (* Process each item in the line *)
                Array.iter
                  (fun (flex_item : flex_item) ->
                    let style_min =
                      if constants.is_row then flex_item.min_size.width
                      else flex_item.min_size.height
                    in
                    let style_preferred =
                      if constants.is_row then flex_item.size.width
                      else flex_item.size.height
                    in
                    let style_max =
                      if constants.is_row then flex_item.max_size.width
                      else flex_item.max_size.height
                    in

                    (* Compute clamping basis *)
                    let clamping_basis =
                      match style_preferred with
                      | Some pref -> Some (Float.max flex_item.flex_basis pref)
                      | None -> Some flex_item.flex_basis
                    in

                    let flex_basis_min =
                      if flex_item.flex_shrink = 0.0 then clamping_basis
                      else None
                    in
                    let flex_basis_max =
                      if flex_item.flex_grow = 0.0 then clamping_basis else None
                    in

                    let min_main_size =
                      let base_min =
                        match (style_min, flex_basis_min) with
                        | Some s, Some f -> Some (Float.max s f)
                        | Some s, None -> Some s
                        | None, Some f -> Some f
                        | None, None -> None
                      in
                      match base_min with
                      | Some v ->
                          Float.max v flex_item.resolved_minimum_main_size
                      | None -> flex_item.resolved_minimum_main_size
                    in

                    let max_main_size =
                      match (style_max, flex_basis_max) with
                      | Some s, Some f -> Float.min s f
                      | Some s, None -> s
                      | None, Some f -> f
                      | None, None -> Float.infinity
                    in

                    let margin_sum =
                      if constants.is_row then
                        flex_item.margin.left +. flex_item.margin.right
                      else flex_item.margin.top +. flex_item.margin.bottom
                    in

                    let content_contribution =
                      match (style_min, style_preferred, max_main_size) with
                      (* If clamping values are such that max <= min, avoid
                         computing content size *)
                      | _, Some pref, max
                        when max <= min_main_size || max <= pref ->
                          let clamped =
                            Float.min pref max |> Float.max min_main_size
                          in
                          clamped +. margin_sum
                      | _, _, max when max <= min_main_size ->
                          min_main_size +. margin_sum
                      | _ when is_scroll_container flex_item ->
                          (* Scroll containers use their flex basis but still
                             honour min/max and padding/border insets *)
                          let content_main_size =
                            flex_item.flex_basis +. margin_sum
                          in
                          if constants.is_row then
                            let clamped =
                              match (style_min, style_max) with
                              | Some min, Some max ->
                                  Float.max min
                                    (Float.min content_main_size max)
                              | Some min, None ->
                                  Float.max min content_main_size
                              | None, Some max ->
                                  Float.min content_main_size max
                              | None, None -> content_main_size
                            in
                            Float.max clamped main_content_box_inset
                          else
                            let with_flex_basis =
                              Float.max content_main_size flex_item.flex_basis
                            in
                            let clamped =
                              match (style_min, style_max) with
                              | Some min, Some max ->
                                  Float.max min (Float.min with_flex_basis max)
                              | Some min, None -> Float.max min with_flex_basis
                              | None, Some max -> Float.min with_flex_basis max
                              | None, None -> with_flex_basis
                            in
                            Float.max clamped main_content_box_inset
                      | _ ->
                          (* Need to compute content size *)
                          let cross_axis_parent_size =
                            if constants.is_row then
                              constants.node_inner_size.height
                            else constants.node_inner_size.width
                          in

                          let cross_axis_margin_sum =
                            if constants.is_row then
                              flex_item.margin.top +. flex_item.margin.bottom
                            else flex_item.margin.left +. flex_item.margin.right
                          in

                          let child_min_cross =
                            (if constants.is_row then flex_item.min_size.height
                             else flex_item.min_size.width)
                            |> Option.map (fun v -> v +. cross_axis_margin_sum)
                          in
                          let child_max_cross =
                            (if constants.is_row then flex_item.max_size.height
                             else flex_item.max_size.width)
                            |> Option.map (fun v -> v +. cross_axis_margin_sum)
                          in

                          let cross_axis_available_space =
                            let available_cross =
                              if constants.is_row then available_space.height
                              else available_space.width
                            in
                            match available_cross with
                            | Available_space.Definite val_ ->
                                let clamped =
                                  Option.value cross_axis_parent_size
                                    ~default:val_
                                  |> fun v ->
                                  match (child_min_cross, child_max_cross) with
                                  | Some min, Some max ->
                                      Float.max min (Float.min v max)
                                  | Some min, None -> Float.max min v
                                  | None, Some max -> Float.min v max
                                  | None, None -> v
                                in
                                Available_space.of_float clamped
                            | _ -> available_cross
                          in

                          let child_available_space =
                            if constants.is_row then
                              Size.
                                {
                                  width = available_space.width;
                                  height = cross_axis_available_space;
                                }
                            else
                              Size.
                                {
                                  width = cross_axis_available_space;
                                  height = available_space.height;
                                }
                          in

                          (* Known dimensions for child sizing *)
                          let child_known_dimensions =
                            let base_size =
                              if constants.is_row then
                                {
                                  Size.width = None;
                                  height = flex_item.size.height;
                                }
                              else
                                {
                                  Size.width = flex_item.size.width;
                                  height = None;
                                }
                            in
                            let cross_dim =
                              if constants.is_row then base_size.height
                              else base_size.width
                            in
                            if
                              flex_item.align_self = Stretch && cross_dim = None
                            then
                              let cross_size =
                                Available_space.to_option
                                  cross_axis_available_space
                                |> Option.map (fun v ->
                                    v
                                    -.
                                    if constants.is_row then
                                      flex_item.margin.top
                                      +. flex_item.margin.bottom
                                    else
                                      flex_item.margin.left
                                      +. flex_item.margin.right)
                              in
                              if constants.is_row then
                                { base_size with height = cross_size }
                              else { base_size with width = cross_size }
                            else base_size
                          in

                          let layout_output =
                            Tree.compute_child_layout tree flex_item.node
                              (Layout_input.make ~run_mode:Run_mode.Compute_size
                                 ~sizing_mode:Sizing_mode.Inherent_size
                                 ~axis:
                                   (if constants.is_row then
                                      Requested_axis.Horizontal
                                    else Requested_axis.Vertical)
                                 ~known_dimensions:child_known_dimensions
                                 ~parent_size:constants.node_inner_size
                                 ~available_space:child_available_space
                                 ~vertical_margins_are_collapsible:
                                   Line.both_false)
                          in

                          let content_main_size =
                            let measured_size =
                              Layout_output.size layout_output
                            in
                            (if constants.is_row then measured_size.width
                             else measured_size.height)
                            +. margin_sum
                          in

                          (* Apply different clamping based on row vs column *)
                          if constants.is_row then
                            let clamped =
                              match (style_min, style_max) with
                              | Some min, Some max ->
                                  Float.max min
                                    (Float.min content_main_size max)
                              | Some min, None ->
                                  Float.max min content_main_size
                              | None, Some max ->
                                  Float.min content_main_size max
                              | None, None -> content_main_size
                            in
                            Float.max clamped main_content_box_inset
                          else
                            let with_flex_basis =
                              Float.max content_main_size flex_item.flex_basis
                            in
                            let clamped =
                              match (style_min, style_max) with
                              | Some min, Some max ->
                                  Float.max min (Float.min with_flex_basis max)
                              | Some min, None -> Float.max min with_flex_basis
                              | None, Some max -> Float.min with_flex_basis max
                              | None, None -> with_flex_basis
                            in
                            Float.max clamped main_content_box_inset
                    in

                    (* Compute content flex fraction *)
                    flex_item.content_flex_fraction <-
                      (let diff =
                         content_contribution -. flex_item.flex_basis
                       in
                       if diff > 0.0 then
                         diff /. Float.max 1.0 flex_item.flex_grow
                       else if diff < 0.0 then
                         let scaled_shrink_factor =
                           Float.max 1.0
                             (flex_item.flex_shrink
                            *. flex_item.inner_flex_basis)
                         in
                         diff /. scaled_shrink_factor
                       else 0.0))
                  line.items;

                (* Calculate line contribution *)
                let item_main_size_sum =
                  Array.fold_left
                    (fun sum item ->
                      let flex_fraction = item.content_flex_fraction in
                      let flex_contribution =
                        if item.content_flex_fraction > 0.0 then
                          Float.max 1.0 item.flex_grow *. flex_fraction
                        else if item.content_flex_fraction < 0.0 then
                          let scaled_shrink_factor =
                            Float.max 1.0 item.flex_shrink
                            *. item.inner_flex_basis
                          in
                          scaled_shrink_factor *. flex_fraction
                        else 0.0
                      in
                      let size = item.flex_basis +. flex_contribution in
                      if constants.is_row then (
                        item.outer_target_width <- size;
                        item.target_width <- size)
                      else (
                        item.outer_target_height <- size;
                        item.target_height <- size);
                      sum +. size)
                    0.0 line.items
                in

                let gap_sum =
                  sum_axis_gaps
                    (if constants.is_row then constants.gap.width
                     else constants.gap.height)
                    (Array.length line.items)
                in
                main_size := Float.max !main_size (item_main_size_sum +. gap_sum))
              flex_lines;

            !main_size +. main_content_box_inset)
  in

  (* Apply size constraints *)
  let min_main =
    if constants.is_row then constants.min_size.width
    else constants.min_size.height
  in
  let max_main =
    if constants.is_row then constants.max_size.width
    else constants.max_size.height
  in
  let scrollbar_main =
    if constants.is_row then constants.scrollbar_gutter.x
    else constants.scrollbar_gutter.y
  in

  let outer_main_size =
    let clamped =
      match (min_main, max_main) with
      | Some min, Some max -> Float.max min (Float.min outer_main_size max)
      | Some min, None -> Float.max min outer_main_size
      | None, Some max -> Float.min outer_main_size max
      | None, None -> outer_main_size
    in
    Float.max clamped (main_content_box_inset -. scrollbar_main)
  in

  let inner_main_size =
    Float.max (outer_main_size -. main_content_box_inset) 0.0
  in

  (* Update constants *)
  if constants.is_row then (
    constants.container_size <-
      { constants.container_size with width = outer_main_size };
    constants.inner_container_size <-
      { constants.inner_container_size with width = inner_main_size };
    constants.node_inner_size <-
      { constants.node_inner_size with width = Some inner_main_size })
  else (
    constants.container_size <-
      { constants.container_size with height = outer_main_size };
    constants.inner_container_size <-
      { constants.inner_container_size with height = inner_main_size };
    constants.node_inner_size <-
      { constants.node_inner_size with height = Some inner_main_size })

(* Resolve flexible lengths *)
let resolve_flexible_lengths (line : flex_line) (constants : algo_constants) :
    unit =
  let total_main_axis_gap =
    sum_axis_gaps
      (if constants.is_row then constants.gap.width else constants.gap.height)
      (Array.length line.items)
  in

  (* 1. Determine the used flex factor *)
  let total_hypothetical_outer_main_size =
    Array.fold_left
      (fun sum child ->
        sum
        +.
        if constants.is_row then child.hypothetical_outer_width
        else child.hypothetical_outer_height)
      0.0 line.items
  in
  let used_flex_factor =
    total_main_axis_gap +. total_hypothetical_outer_main_size
  in
  let inner_main_size =
    if constants.is_row then
      Option.value constants.node_inner_size.width ~default:0.0
    else Option.value constants.node_inner_size.height ~default:0.0
  in
  let growing = used_flex_factor < inner_main_size in
  let shrinking = used_flex_factor > inner_main_size in
  let exactly_sized = (not growing) && not shrinking in

  (* 2. Size inflexible items *)
  Array.iter
    (fun child ->
      let inner_target_size =
        if constants.is_row then child.hypothetical_inner_width
        else child.hypothetical_inner_height
      in
      if constants.is_row then child.target_width <- inner_target_size
      else child.target_height <- inner_target_size;

      if
        exactly_sized
        || (child.flex_grow = 0.0 && child.flex_shrink = 0.0)
        || (growing && child.flex_basis > inner_target_size)
        || (shrinking && child.flex_basis < inner_target_size)
      then (
        child.frozen <- true;
        let outer_target_size =
          inner_target_size
          +.
          if constants.is_row then child.margin.left +. child.margin.right
          else child.margin.top +. child.margin.bottom
        in
        if constants.is_row then child.outer_target_width <- outer_target_size
        else child.outer_target_height <- outer_target_size))
    line.items;

  if exactly_sized then ()
  else
    (* 3. Calculate initial free space *)
    let calc_used_space () =
      total_main_axis_gap
      +. Array.fold_left
           (fun sum child ->
             if child.frozen then
               sum
               +.
               if constants.is_row then child.outer_target_width
               else child.outer_target_height
             else
               sum +. child.flex_basis
               +.
               if constants.is_row then child.margin.left +. child.margin.right
               else child.margin.top +. child.margin.bottom)
           0.0 line.items
    in

    let initial_free_space = inner_main_size -. calc_used_space () in

    (* 4. Loop *)
    let rec flex_loop () =
      (* a. Check for flexible items *)
      if Array.for_all (fun child -> child.frozen) line.items then ()
      else
        (* b. Calculate the remaining free space *)
        let used_space = calc_used_space () in

        let sum_flex_grow, sum_flex_shrink =
          Array.fold_left
            (fun (grow, shrink) child ->
              if child.frozen then (grow, shrink)
              else (grow +. child.flex_grow, shrink +. child.flex_shrink))
            (0.0, 0.0) line.items
        in

        let free_space =
          if growing && sum_flex_grow < 1.0 then
            Float.min
              ((initial_free_space *. sum_flex_grow) -. total_main_axis_gap)
              (inner_main_size -. used_space)
          else if shrinking && sum_flex_shrink < 1.0 then
            Float.max
              ((initial_free_space *. sum_flex_shrink) -. total_main_axis_gap)
              (inner_main_size -. used_space)
          else inner_main_size -. used_space
        in

        (* c. Distribute free space proportional to the flex factors *)
        (if Float.is_finite free_space && free_space <> 0.0 then
           if growing && sum_flex_grow > 0.0 then
             Array.iter
               (fun child ->
                 if not child.frozen then
                   let new_size =
                     child.flex_basis
                     +. (free_space *. (child.flex_grow /. sum_flex_grow))
                   in
                   if constants.is_row then child.target_width <- new_size
                   else child.target_height <- new_size)
               line.items
           else if shrinking && sum_flex_shrink > 0.0 then
             let sum_scaled_shrink_factor =
               Array.fold_left
                 (fun sum child ->
                   if child.frozen then sum
                   else sum +. (child.inner_flex_basis *. child.flex_shrink))
                 0.0 line.items
             in
             if sum_scaled_shrink_factor > 0.0 then
               Array.iter
                 (fun child ->
                   if not child.frozen then
                     let scaled_shrink_factor =
                       child.inner_flex_basis *. child.flex_shrink
                     in
                     let new_size =
                       child.flex_basis
                       +. free_space
                          *. (scaled_shrink_factor /. sum_scaled_shrink_factor)
                     in
                     if constants.is_row then child.target_width <- new_size
                     else child.target_height <- new_size)
                 line.items);

        (* d. Fix min/max violations *)
        let total_violation =
          Array.fold_left
            (fun acc child ->
              if child.frozen then acc
              else
                let current_size =
                  if constants.is_row then child.target_width
                  else child.target_height
                in
                let resolved_min = Some child.resolved_minimum_main_size in
                let max_main =
                  if constants.is_row then child.max_size.width
                  else child.max_size.height
                in
                let clamped =
                  let v =
                    match (resolved_min, max_main) with
                    | Some min, Some max ->
                        Float.max min (Float.min current_size max)
                    | Some min, None -> Float.max min current_size
                    | None, Some max -> Float.min current_size max
                    | None, None -> current_size
                  in
                  Float.max 0.0 v
                in
                child.violation <- clamped -. current_size;
                if constants.is_row then (
                  child.target_width <- clamped;
                  child.outer_target_width <-
                    clamped +. child.margin.left +. child.margin.right)
                else (
                  child.target_height <- clamped;
                  child.outer_target_height <-
                    clamped +. child.margin.top +. child.margin.bottom);

                acc +. child.violation)
            0.0 line.items
        in

        (* e. Freeze over-flexed items *)
        Array.iter
          (fun child ->
            if not child.frozen then
              child.frozen <-
                (if total_violation > 0.0 then child.violation > 0.0
                 else if total_violation < 0.0 then child.violation < 0.0
                 else true))
          line.items;

        (* f. Return to the start of this loop *)
        flex_loop ()
    in

    flex_loop ()

(* Determine hypothetical cross size *)
let determine_hypothetical_cross_size (type t)
    (module Tree : Tree.LAYOUT_PARTIAL_TREE with type t = t) (tree : t)
    (line : flex_line) (constants : algo_constants)
    (available_space : Available_space.t size) : unit =
  Array.iter
    (fun child ->
      let padding_border_sum =
        if constants.is_row then
          child.padding.top +. child.padding.bottom +. child.border.top
          +. child.border.bottom
        else
          child.padding.left +. child.padding.right +. child.border.left
          +. child.border.right
      in

      let child_known_main =
        if constants.is_row then constants.container_size.width
        else constants.container_size.height
      in

      let child_cross =
        let cross_size =
          if constants.is_row then child.size.height else child.size.width
        in
        let min_cross =
          if constants.is_row then child.min_size.height
          else child.min_size.width
        in
        let max_cross =
          if constants.is_row then child.max_size.height
          else child.max_size.width
        in
        match (cross_size, min_cross, max_cross) with
        | Some size, Some min, Some max ->
            Some
              (Float.max padding_border_sum
                 (Float.max min (Float.min size max)))
        | Some size, Some min, None ->
            Some (Float.max padding_border_sum (Float.max min size))
        | Some size, None, Some max ->
            Some (Float.max padding_border_sum (Float.min size max))
        | Some size, None, None -> Some (Float.max padding_border_sum size)
        | None, _, _ -> None
      in

      let child_available_cross =
        let available_cross =
          if constants.is_row then available_space.height
          else available_space.width
        in
        let min_cross =
          if constants.is_row then child.min_size.height
          else child.min_size.width
        in
        let max_cross =
          if constants.is_row then child.max_size.height
          else child.max_size.width
        in
        match available_cross with
        | Available_space.Definite value ->
            let clamped =
              match (min_cross, max_cross) with
              | Some min, Some max -> Float.max min (Float.min value max)
              | Some min, None -> Float.max min value
              | None, Some max -> Float.min value max
              | None, None -> value
            in
            Available_space.of_float (Float.max padding_border_sum clamped)
        | _ -> available_cross
      in

      let child_inner_cross =
        match child_cross with
        | Some cross -> cross
        | None ->
            (* Need to measure the child *)
            let child_known_dimensions =
              if constants.is_row then
                { Size.width = Some child.target_width; height = child_cross }
              else
                { Size.width = child_cross; height = Some child.target_height }
            in
            let child_available_space =
              if constants.is_row then
                {
                  Size.width = Available_space.of_float child_known_main;
                  height = child_available_cross;
                }
              else
                {
                  Size.width = child_available_cross;
                  height = Available_space.of_float child_known_main;
                }
            in
            let layout_output =
              Tree.compute_child_layout tree child.node
                (Layout_input.make ~run_mode:Run_mode.Compute_size
                   ~sizing_mode:Sizing_mode.Content_size
                   ~axis:
                     (if constants.is_row then Requested_axis.Vertical
                      else Requested_axis.Horizontal)
                   ~known_dimensions:child_known_dimensions
                   ~parent_size:constants.node_inner_size
                   ~available_space:child_available_space
                   ~vertical_margins_are_collapsible:Line.both_false)
            in
            let measured_size =
              let size = Layout_output.size layout_output in
              if constants.is_row then size.height else size.width
            in
            let min_cross =
              if constants.is_row then child.min_size.height
              else child.min_size.width
            in
            let max_cross =
              if constants.is_row then child.max_size.height
              else child.max_size.width
            in
            let clamped =
              match (min_cross, max_cross) with
              | Some min, Some max ->
                  Float.max min (Float.min measured_size max)
              | Some min, None -> Float.max min measured_size
              | None, Some max -> Float.min measured_size max
              | None, None -> measured_size
            in
            Float.max padding_border_sum clamped
      in

      let child_outer_cross =
        child_inner_cross
        +.
        if constants.is_row then child.margin.top +. child.margin.bottom
        else child.margin.left +. child.margin.right
      in

      if constants.is_row then (
        child.hypothetical_inner_height <- child_inner_cross;
        child.hypothetical_outer_height <- child_outer_cross)
      else (
        child.hypothetical_inner_width <- child_inner_cross;
        child.hypothetical_outer_width <- child_outer_cross))
    line.items

(* Calculate children baselines *)
let calculate_children_base_lines (type t)
    (module Tree : Tree.LAYOUT_PARTIAL_TREE with type t = t) (tree : t)
    (known_dimensions : float option size)
    (available_space : Available_space.t size) (flex_lines : flex_line list)
    (constants : algo_constants) : unit =
  (* Only compute baselines for flex rows because we only support baseline
     alignment in the cross axis where that axis is also the inline axis *)
  if not constants.is_row then ()
  else
    List.iter
      (fun line ->
        (* If a flex line has one or zero items participating in baseline
           alignment then baseline alignment is a no-op so we skip *)
        let line_baseline_child_count =
          Array.fold_left
            (fun count child ->
              if child.align_self = Baseline then count + 1 else count)
            0 line.items
        in
        if line_baseline_child_count <= 1 then ()
        else
          Array.iter
            (fun child ->
              (* Only calculate baselines for children participating in baseline
                 alignment *)
              if child.align_self = Baseline then
                let measured_size_and_baselines =
                  Tree.compute_child_layout tree child.node
                    (Layout_input.make ~run_mode:Run_mode.Perform_layout
                       ~sizing_mode:Sizing_mode.Content_size
                       ~axis:Requested_axis.Both
                       ~known_dimensions:
                         (if constants.is_row then
                            {
                              Size.width = Some child.target_width;
                              height = Some child.hypothetical_inner_height;
                            }
                          else
                            {
                              Size.width = Some child.hypothetical_inner_width;
                              height = Some child.target_height;
                            })
                       ~parent_size:constants.node_inner_size
                       ~available_space:
                         (if constants.is_row then
                            {
                              Size.width =
                                Available_space.of_float
                                  constants.container_size.width;
                              height =
                                Available_space.set_or_self
                                  available_space.height known_dimensions.height;
                            }
                          else
                            {
                              Size.width =
                                Available_space.set_or_self
                                  available_space.width known_dimensions.width;
                              height =
                                Available_space.of_float
                                  constants.container_size.height;
                            })
                       ~vertical_margins_are_collapsible:Line.both_false)
                in

                let baseline =
                  (Layout_output.first_baselines measured_size_and_baselines).y
                in
                let height =
                  (Layout_output.size measured_size_and_baselines).height
                in

                child.baseline <-
                  Option.value baseline ~default:height +. child.margin.top)
            line.items)
      flex_lines

(* Calculate cross size *)
let calculate_cross_size (flex_lines : flex_line list)
    (known_dimensions : float option size) (constants : algo_constants) : unit =
  (* If the flex container is single-line and has a definite cross size, the
     cross size of the flex line is the flex container's inner cross size. *)
  if
    (not constants.is_wrap)
    && (if constants.is_row then known_dimensions.height
        else known_dimensions.width)
       <> None
  then
    let cross_axis_padding_border =
      if constants.is_row then
        Rect.vertical_axis_sum constants.content_box_inset
      else Rect.horizontal_axis_sum constants.content_box_inset
    in
    let cross_min_size =
      if constants.is_row then constants.min_size.height
      else constants.min_size.width
    in
    let cross_max_size =
      if constants.is_row then constants.max_size.height
      else constants.max_size.width
    in
    let node_cross_size =
      if constants.is_row then known_dimensions.height
      else known_dimensions.width
    in

    (* Apply constraints and calculate cross size *)
    let cross_size =
      match node_cross_size with
      | Some size ->
          let clamped =
            match (cross_min_size, cross_max_size) with
            | Some min, Some max -> Float.max min (Float.min size max)
            | Some min, None -> Float.max min size
            | None, Some max -> Float.min size max
            | None, None -> size
          in
          Float.max 0.0 (clamped -. cross_axis_padding_border)
      | None -> 0.0
    in

    (* Set the cross size for the single line *)
    match flex_lines with
    | line :: _ -> line.cross_size <- cross_size
    | [] -> ()
  else
    (* Otherwise, for each flex line, calculate based on items *)
    List.iter
      (fun line ->
        (* Find the maximum baseline *)
        let max_baseline =
          Array.fold_left
            (fun acc child -> Float.max acc child.baseline)
            0.0 line.items
        in

        (* Calculate cross size based on items *)
        let cross_size =
          Array.fold_left
            (fun acc child ->
              let child_cross_size =
                if
                  child.align_self = Baseline
                  && (not
                        (if constants.is_row then child.margin_is_auto.top
                         else child.margin_is_auto.left))
                  && not
                       (if constants.is_row then child.margin_is_auto.bottom
                        else child.margin_is_auto.right)
                then
                  (* Baseline aligned items with non-auto margins *)
                  max_baseline -. child.baseline
                  +.
                  if constants.is_row then child.hypothetical_outer_height
                  else child.hypothetical_outer_width
                else if
                  (* Other items *)
                  constants.is_row
                then child.hypothetical_outer_height
                else child.hypothetical_outer_width
              in
              Float.max acc child_cross_size)
            0.0 line.items
        in

        line.cross_size <- cross_size)
      flex_lines;

  (* If single-line, clamp the line's cross-size to container's min/max *)
  if not constants.is_wrap then
    match flex_lines with
    | line :: _ ->
        let cross_axis_padding_border =
          if constants.is_row then
            Rect.vertical_axis_sum constants.content_box_inset
          else Rect.horizontal_axis_sum constants.content_box_inset
        in
        let cross_min_size =
          if constants.is_row then constants.min_size.height
          else constants.min_size.width
        in
        let cross_max_size =
          if constants.is_row then constants.max_size.height
          else constants.max_size.width
        in

        let min_constraint =
          Option.map (fun v -> v -. cross_axis_padding_border) cross_min_size
        in
        let max_constraint =
          Option.map (fun v -> v -. cross_axis_padding_border) cross_max_size
        in

        line.cross_size <-
          (match (min_constraint, max_constraint) with
          | Some min, Some max -> Float.max min (Float.min line.cross_size max)
          | Some min, None -> Float.max min line.cross_size
          | None, Some max -> Float.min line.cross_size max
          | None, None -> line.cross_size)
    | [] -> ()

(* Handle align-content: stretch *)
let handle_align_content_stretch (flex_lines : flex_line list)
    (known_dimensions : float option size) (constants : algo_constants) : unit =
  match constants.align_content with
  | Style.Align_content.Stretch ->
      let cross_axis_padding_border =
        if constants.is_row then
          Rect.vertical_axis_sum constants.content_box_inset
        else Rect.horizontal_axis_sum constants.content_box_inset
      in

      let cross_min_size =
        if constants.is_row then constants.min_size.height
        else constants.min_size.width
      in

      let cross_max_size =
        if constants.is_row then constants.max_size.height
        else constants.max_size.width
      in

      let cross_dimension =
        if constants.is_row then known_dimensions.height
        else known_dimensions.width
      in

      let container_min_inner_cross =
        let size_or_min =
          match (cross_dimension, cross_min_size) with
          | Some size, _ -> Some size
          | None, min -> min
        in
        let clamped =
          match (size_or_min, cross_min_size, cross_max_size) with
          | Some size, Some min, Some max ->
              Some (Float.max min (Float.min size max))
          | Some size, Some min, None -> Some (Float.max min size)
          | Some size, None, Some max -> Some (Float.min size max)
          | Some size, None, None -> Some size
          | None, _, _ -> None
        in
        match clamped with
        | Some v -> Float.max 0.0 (v -. cross_axis_padding_border)
        | None -> 0.0
      in

      let cross_gap =
        if constants.is_row then constants.gap.height else constants.gap.width
      in
      let total_cross_axis_gap =
        if List.length flex_lines > 0 then
          cross_gap *. float_of_int (List.length flex_lines - 1)
        else 0.0
      in

      let lines_total_cross =
        List.fold_left (fun acc line -> acc +. line.cross_size) 0.0 flex_lines
        +. total_cross_axis_gap
      in

      if lines_total_cross < container_min_inner_cross then
        let remaining = container_min_inner_cross -. lines_total_cross in
        let addition = remaining /. float_of_int (List.length flex_lines) in
        List.iter
          (fun line -> line.cross_size <- line.cross_size +. addition)
          flex_lines
  | _ -> ()

(* Determine used cross size *)
let determine_used_cross_size (type t)
    (module Tree : Tree.LAYOUT_PARTIAL_TREE with type t = t) (tree : t)
    (flex_lines : flex_line list) (constants : algo_constants) : unit =
  List.iter
    (fun line ->
      let line_cross_size = line.cross_size in
      Array.iter
        (fun child ->
          let child_style = Tree.get_core_container_style tree child.node in

          let cross_target_size =
            if
              child.align_self = Style.Align_items.Stretch
              && (not
                    (if constants.is_row then child.margin_is_auto.top
                     else child.margin_is_auto.left))
              && (not
                    (if constants.is_row then child.margin_is_auto.bottom
                     else child.margin_is_auto.right))
              &&
              let size = Style.size child_style in
              if constants.is_row then Style.Dimension.is_auto size.height
              else Style.Dimension.is_auto size.width
            then
              (* Special case for stretch alignment *)
              (* For some reason this particular usage of max_width is an exception to the rule that max_width's transfer
             using the aspect_ratio (if set). Both Chrome and Firefox agree on this. And reading the spec, it seems like
             a reasonable interpretation. Although it seems to me that the spec *should* apply aspect_ratio here. *)
              let calc = Tree.resolve_calc_value tree in

              let padding =
                Style.padding child_style
                |> Rect.map (fun lp ->
                    Length_percentage.resolve_or_zero lp
                      constants.node_inner_size.width calc)
              in
              let border =
                Style.border child_style
                |> Rect.map (fun lp ->
                    Length_percentage.resolve_or_zero lp
                      constants.node_inner_size.width calc)
              in
              let pb_sum = Rect.sum_axes (Rect.add padding border) in
              let box_sizing_adjustment =
                if Style.box_sizing child_style = Style.Box_sizing.Content_box
                then pb_sum
                else Size.zero
              in
              let max_size_ignoring_aspect_ratio =
                Style.max_size child_style |> fun dims ->
                Size.
                  {
                    width =
                      Dimension.maybe_resolve (Size.get Inline dims)
                        constants.node_inner_size.width calc;
                    height =
                      Dimension.maybe_resolve (Size.get Block dims)
                        constants.node_inner_size.height calc;
                  }
                |> Size.maybe_add box_sizing_adjustment
              in

              let cross_margin_sum =
                if constants.is_row then child.margin.top +. child.margin.bottom
                else child.margin.left +. child.margin.right
              in

              let stretched_size = line_cross_size -. cross_margin_sum in
              let min_size =
                if constants.is_row then child.min_size.height
                else child.min_size.width
              in
              let max_size =
                if constants.is_row then max_size_ignoring_aspect_ratio.height
                else max_size_ignoring_aspect_ratio.width
              in

              match (min_size, max_size) with
              | Some min, Some max ->
                  Float.max min (Float.min stretched_size max)
              | Some min, None -> Float.max min stretched_size
              | None, Some max -> Float.min stretched_size max
              | None, None -> stretched_size
            else if
              (* Use hypothetical size *)
              constants.is_row
            then child.hypothetical_inner_height
            else child.hypothetical_inner_width
          in

          (* Set target size *)
          if constants.is_row then child.target_height <- cross_target_size
          else child.target_width <- cross_target_size;

          (* Set outer target size *)
          let cross_margin_sum =
            if constants.is_row then child.margin.top +. child.margin.bottom
            else child.margin.left +. child.margin.right
          in

          if constants.is_row then
            child.outer_target_height <- cross_target_size +. cross_margin_sum
          else child.outer_target_width <- cross_target_size +. cross_margin_sum)
        line.items)
    flex_lines

(* Helper function to sum axis gaps *)
let sum_axis_gaps (gap : float) (num_items : int) : float =
  (* Gaps only exist between items, so... *)
  if num_items <= 1 then
    (* ...if there are less than 2 items then there are no gaps *)
    0.0
  else
    (* ...otherwise there are (num_items - 1) gaps *)
    gap *. float_of_int (num_items - 1)

(* Helper function to apply alignment fallback *)
let apply_alignment_fallback (free_space : float) (num_items : int)
    (alignment_mode : align_content) (is_safe : bool) : align_content =
  (* Fallback occurs in two cases: *)

  (* 1. If there is only a single item being aligned and alignment is a
     distributed alignment keyword
     https://www.w3.org/TR/css-align-3/#distribution-values *)
  let alignment_mode, is_safe =
    if num_items <= 1 || free_space <= 0.0 then
      match alignment_mode with
      | Align_content.Stretch -> (Align_content.Flex_start, true)
      | Align_content.Space_between -> (Align_content.Flex_start, true)
      | Align_content.Space_around -> (Center, true)
      | Align_content.Space_evenly -> (Center, true)
      | _ -> (alignment_mode, is_safe)
    else (alignment_mode, is_safe)
  in

  (* 2. If free space is negative the "safe" alignment variants all fallback to
     Start alignment *)
  let alignment_mode =
    if free_space <= 0.0 && is_safe then Align_content.Start else alignment_mode
  in

  alignment_mode

(* Helper function to compute alignment offset *)
let compute_alignment_offset (free_space : float) (num_items : int)
    (gap : float) (alignment_mode : align_content)
    (layout_is_flex_reversed : bool) (is_first : bool) : float =
  if is_first then
    match alignment_mode with
    | Align_content.Start -> 0.0
    | Flex_start -> if layout_is_flex_reversed then free_space else 0.0
    | End -> free_space
    | Flex_end -> if layout_is_flex_reversed then 0.0 else free_space
    | Center -> free_space /. 2.0
    | Stretch -> 0.0
    | Space_between -> 0.0
    | Space_around ->
        if free_space >= 0.0 then free_space /. float_of_int (num_items * 2)
        else free_space /. 2.0
    | Space_evenly ->
        if free_space >= 0.0 then free_space /. float_of_int (num_items + 1)
        else free_space /. 2.0
  else
    (* For non-first items *)
    match alignment_mode with
    | Align_content.Space_between ->
        gap
        +.
        if num_items > 1 then free_space /. float_of_int (num_items - 1)
        else 0.0
    | Space_around ->
        gap
        +.
        if free_space >= 0.0 then free_space /. float_of_int num_items else 0.0
    | Space_evenly ->
        gap
        +.
        if free_space >= 0.0 then free_space /. float_of_int (num_items + 1)
        else 0.0
    | _ -> gap

(* Distribute remaining free space *)
let distribute_remaining_free_space (flex_lines : flex_line list)
    (constants : algo_constants) : unit =
  List.iter
    (fun line ->
      let main_gap =
        if constants.is_row then constants.gap.width else constants.gap.height
      in
      let total_main_axis_gap =
        sum_axis_gaps main_gap (Array.length line.items)
      in
      let used_space =
        total_main_axis_gap
        +. Array.fold_left
             (fun acc child ->
               acc
               +.
               if constants.is_row then child.outer_target_width
               else child.outer_target_height)
             0.0 line.items
      in
      let free_space =
        (if constants.is_row then constants.inner_container_size.width
         else constants.inner_container_size.height)
        -. used_space
      in

      (* Count auto margins *)
      let num_auto_margins = ref 0 in
      Array.iter
        (fun child ->
          if constants.is_row then (
            if child.margin_is_auto.left then incr num_auto_margins;
            if child.margin_is_auto.right then incr num_auto_margins)
          else (
            if child.margin_is_auto.top then incr num_auto_margins;
            if child.margin_is_auto.bottom then incr num_auto_margins))
        line.items;

      if free_space > 0.0 && !num_auto_margins > 0 then
        (* Distribute free space to auto margins *)
        let margin = free_space /. float_of_int !num_auto_margins in
        Array.iter
          (fun child ->
            if constants.is_row then (
              if child.margin_is_auto.left then
                child.margin <- { child.margin with left = margin };
              if child.margin_is_auto.right then
                child.margin <- { child.margin with right = margin })
            else (
              if child.margin_is_auto.top then
                child.margin <- { child.margin with top = margin };
              if child.margin_is_auto.bottom then
                child.margin <- { child.margin with bottom = margin }))
          line.items
      else
        (* Apply justify-content alignment *)
        let num_items = Array.length line.items in
        let layout_reverse = Flex_direction_ext.is_reverse constants.dir in
        let is_safe = false in
        (* TODO: Implement safe alignment *)
        let raw_justify_content_mode =
          match constants.justify_content with
          | Some jc -> jc
          | None -> Flex_start
        in
        let justify_content_mode =
          apply_alignment_fallback free_space num_items raw_justify_content_mode
            is_safe
        in

        let justify_item i child =
          child.offset_main <-
            compute_alignment_offset free_space num_items main_gap
              justify_content_mode layout_reverse (i = 0)
        in

        if layout_reverse then
          for i = Array.length line.items - 1 downto 0 do
            let rev_i = Array.length line.items - 1 - i in
            justify_item rev_i line.items.(i)
          done
        else Array.iteri justify_item line.items)
    flex_lines

(* Helper function to align flex items along cross axis *)
let align_flex_items_along_cross_axis (child : flex_item) (free_space : float)
    (max_baseline : float) (constants : algo_constants) : float =
  match child.align_self with
  | Style.Align_items.Start -> 0.0
  | Style.Align_items.Flex_start ->
      if constants.is_wrap_reverse then free_space else 0.0
  | Style.Align_items.End -> free_space
  | Style.Align_items.Flex_end ->
      if constants.is_wrap_reverse then 0.0 else free_space
  | Style.Align_items.Center -> free_space /. 2.0
  | Style.Align_items.Baseline ->
      if constants.is_row then max_baseline -. child.baseline
      else if
        (* Until we support vertical writing modes, baseline alignment only
           makes sense if the direction is row, so we treat it as flex-start
           alignment in columns. *)
        constants.is_wrap_reverse
      then free_space
      else 0.0
  | Style.Align_items.Stretch ->
      if constants.is_wrap_reverse then free_space else 0.0

(* Resolve cross-axis auto margins *)
let resolve_cross_axis_auto_margins (flex_lines : flex_line list)
    (constants : algo_constants) : unit =
  List.iter
    (fun line ->
      let line_cross_size = line.cross_size in
      let max_baseline =
        Array.fold_left
          (fun acc child -> Float.max acc child.baseline)
          0.0 line.items
      in

      Array.iter
        (fun child ->
          let free_space =
            line_cross_size
            -.
            if constants.is_row then child.outer_target_height
            else child.outer_target_width
          in

          let cross_start_auto =
            if constants.is_row then child.margin_is_auto.top
            else child.margin_is_auto.left
          in
          let cross_end_auto =
            if constants.is_row then child.margin_is_auto.bottom
            else child.margin_is_auto.right
          in

          if cross_start_auto && cross_end_auto then
            (* Both margins are auto - distribute free space equally *)
            if constants.is_row then (
              child.margin <- { child.margin with top = free_space /. 2.0 };
              child.margin <- { child.margin with bottom = free_space /. 2.0 })
            else (
              child.margin <- { child.margin with left = free_space /. 2.0 };
              child.margin <- { child.margin with right = free_space /. 2.0 })
          else if cross_start_auto then
            (* Only start margin is auto *)
            if constants.is_row then
              child.margin <- { child.margin with top = free_space }
            else child.margin <- { child.margin with left = free_space }
          else if cross_end_auto then
            (* Only end margin is auto *)
            if constants.is_row then
              child.margin <- { child.margin with bottom = free_space }
            else child.margin <- { child.margin with right = free_space }
          else
            (* No auto margins - align according to align-self *)
            child.offset_cross <-
              align_flex_items_along_cross_axis child free_space max_baseline
                constants)
        line.items)
    flex_lines

(* Determine container cross size *)
let determine_container_cross_size (flex_lines : flex_line list)
    (known_dimensions : float option size) (constants : algo_constants) : float
    =
  let cross_gap =
    if constants.is_row then constants.gap.height else constants.gap.width
  in
  let total_cross_axis_gap = sum_axis_gaps cross_gap (List.length flex_lines) in
  let total_line_cross_size =
    List.fold_left (fun acc line -> acc +. line.cross_size) 0.0 flex_lines
  in
  let padding_border_sum =
    if constants.is_row then Rect.vertical_axis_sum constants.content_box_inset
    else Rect.horizontal_axis_sum constants.content_box_inset
  in
  let cross_scrollbar_gutter =
    if constants.is_row then constants.scrollbar_gutter.y
    else constants.scrollbar_gutter.x
  in
  let min_cross_size =
    if constants.is_row then constants.min_size.height
    else constants.min_size.width
  in
  let max_cross_size =
    if constants.is_row then constants.max_size.height
    else constants.max_size.width
  in

  let cross_node_size =
    if constants.is_row then known_dimensions.height else known_dimensions.width
  in

  let outer_container_size =
    let base_size =
      match cross_node_size with
      | Some size -> size
      | None ->
          total_line_cross_size +. total_cross_axis_gap +. padding_border_sum
    in
    let clamped_size =
      match (min_cross_size, max_cross_size) with
      | Some min, Some max -> Float.max min (Float.min base_size max)
      | Some min, None -> Float.max min base_size
      | None, Some max -> Float.min base_size max
      | None, None -> base_size
    in
    Float.max clamped_size (padding_border_sum -. cross_scrollbar_gutter)
  in

  let inner_container_size =
    Float.max 0.0 (outer_container_size -. padding_border_sum)
  in

  (* Update constants *)
  if constants.is_row then (
    constants.container_size <-
      { constants.container_size with height = outer_container_size };
    constants.inner_container_size <-
      { constants.inner_container_size with height = inner_container_size })
  else (
    constants.container_size <-
      { constants.container_size with width = outer_container_size };
    constants.inner_container_size <-
      { constants.inner_container_size with width = inner_container_size });

  total_line_cross_size

(* Align flex lines per align-content *)
let align_flex_lines_per_align_content (flex_lines : flex_line list)
    (constants : algo_constants) (total_line_cross_size : float) : unit =
  let num_lines = List.length flex_lines in
  let gap =
    if constants.is_row then constants.gap.height else constants.gap.width
  in
  let total_cross_axis_gap = sum_axis_gaps gap num_lines in
  let free_space =
    (if constants.is_row then constants.inner_container_size.height
     else constants.inner_container_size.width)
    -. total_line_cross_size -. total_cross_axis_gap
  in
  let is_safe = false in
  (* TODO: Implement safe alignment *)
  let align_content_mode =
    apply_alignment_fallback free_space num_lines constants.align_content
      is_safe
  in

  let align_line i line =
    line.offset_cross <-
      compute_alignment_offset free_space num_lines gap align_content_mode
        constants.is_wrap_reverse (i = 0)
  in

  if constants.is_wrap_reverse then
    (* Process lines in reverse order for wrap-reverse *)
    List.rev flex_lines |> List.iteri align_line
  else List.iteri align_line flex_lines

(* Final layout pass *)
let final_layout_pass (type t)
    (module Tree : Tree.LAYOUT_PARTIAL_TREE with type t = t) (tree : t)
    (flex_lines : flex_line list) (constants : algo_constants) : float size =
  (* Helper function to calculate flex item layout *)
  let calculate_flex_item item total_offset_main total_offset_cross
      line_offset_cross content_size container_size node_inner_size direction =
    (* Perform child layout *)
    let layout_output =
      Tree.compute_child_layout tree item.node
        (Layout_input.make ~run_mode:Run_mode.Perform_layout
           ~sizing_mode:Sizing_mode.Content_size ~axis:Requested_axis.Both
           ~known_dimensions:
             Size.
               {
                 width = Some item.target_width;
                 height = Some item.target_height;
               }
           ~parent_size:node_inner_size
           ~available_space:(Size.map Available_space.of_float container_size)
           ~vertical_margins_are_collapsible:Line.both_false)
    in

    let size = Layout_output.size layout_output in
    let content_size_output = Layout_output.content_size layout_output in

    (* Calculate main axis offset *)
    let offset_main =
      !total_offset_main +. item.offset_main
      +. Flex_direction_ext.main_start item.margin direction
      +.
      match Flex_direction_ext.main_start item.inset direction with
      | Some pos -> pos
      | None -> (
          match Flex_direction_ext.main_end item.inset direction with
          | Some pos -> -.pos
          | None -> 0.0)
    in

    (* Calculate cross axis offset *)
    let offset_cross =
      total_offset_cross +. item.offset_cross +. line_offset_cross
      +. Flex_direction_ext.cross_start item.margin direction
      +.
      match Flex_direction_ext.cross_start item.inset direction with
      | Some pos -> pos
      | None -> (
          match Flex_direction_ext.cross_end item.inset direction with
          | Some pos -> -.pos
          | None -> 0.0)
    in

    (* Update baseline *)
    (if Flex_direction_ext.is_row direction then
       let baseline_offset_cross =
         total_offset_cross +. item.offset_cross
         +. Flex_direction_ext.cross_start item.margin direction
       in
       let inner_baseline =
         Option.value (Layout_output.first_baselines layout_output).y
           ~default:size.height
       in
       item.baseline <- baseline_offset_cross +. inner_baseline
     else
       let baseline_offset_main =
         !total_offset_main +. item.offset_main
         +. Flex_direction_ext.main_start item.margin direction
       in
       let inner_baseline =
         Option.value (Layout_output.first_baselines layout_output).y
           ~default:size.height
       in
       item.baseline <- baseline_offset_main +. inner_baseline);

    (* Calculate location *)
    let location =
      if Flex_direction_ext.is_row direction then
        Point.{ x = offset_main; y = offset_cross }
      else Point.{ x = offset_cross; y = offset_main }
    in

    (* Calculate scrollbar size *)
    let scrollbar_size =
      Size.
        {
          width =
            (if item.overflow.y = Overflow.Scroll then item.scrollbar_width
             else 0.0);
          height =
            (if item.overflow.x = Overflow.Scroll then item.scrollbar_width
             else 0.0);
        }
    in

    (* Set layout on tree *)
    Tree.set_unrounded_layout tree item.node
      (Layout.make ~order:item.order ~location ~size
         ~content_size:content_size_output ~scrollbar_size ~border:item.border
         ~padding:item.padding ~margin:item.margin);

    (* Update main offset *)
    total_offset_main :=
      !total_offset_main +. item.offset_main
      +. Flex_direction_ext.main_axis_sum item.margin direction
      +. Flex_direction_ext.main_size size direction;

    (* Update content size *)
    let contribution_width =
      match item.overflow.x with
      | Overflow.Visible -> max size.width content_size_output.width
      | _ -> size.width
    in
    let contribution_height =
      match item.overflow.y with
      | Overflow.Visible -> max size.height content_size_output.height
      | _ -> size.height
    in
    if contribution_width > 0.0 || contribution_height > 0.0 then
      let content_contribution =
        Size.
          {
            width = location.x +. contribution_width;
            height = location.y +. contribution_height;
          }
      in
      let current_size = !content_size in
      let new_width =
        max current_size.Size.width content_contribution.Size.width
      in
      let new_height =
        max current_size.Size.height content_contribution.Size.height
      in
      content_size := Size.{ width = new_width; height = new_height }
  in

  (* Helper function to calculate layout for a line *)
  let calculate_layout_line line total_offset_cross content_size container_size
      node_inner_size padding_border direction =
    let total_offset_main =
      ref
        (if constants.is_row then padding_border.Rect.left
         else padding_border.Rect.top)
    in
    let line_offset_cross = line.offset_cross in

    if Flex_direction_ext.is_reverse direction then
      for i = Array.length line.items - 1 downto 0 do
        let item = line.items.(i) in
        calculate_flex_item item total_offset_main !total_offset_cross
          line_offset_cross content_size container_size node_inner_size
          direction
      done
    else
      Array.iter
        (fun item ->
          calculate_flex_item item total_offset_main !total_offset_cross
            line_offset_cross content_size container_size node_inner_size
            direction)
        line.items;

    total_offset_cross :=
      !total_offset_cross +. line_offset_cross +. line.cross_size
  in

  (* Main implementation *)
  let total_offset_cross =
    ref
      (if constants.is_row then constants.content_box_inset.top
       else constants.content_box_inset.left)
  in
  let content_size = ref Size.zero in

  let lines =
    if constants.is_wrap_reverse then List.rev flex_lines else flex_lines
  in

  List.iter
    (fun line ->
      calculate_layout_line line total_offset_cross content_size
        constants.container_size constants.node_inner_size
        constants.content_box_inset constants.dir)
    lines;

  (* Add final adjustments to content size *)
  Size.
    {
      width =
        !content_size.width +. constants.content_box_inset.right
        -. constants.border.right -. constants.scrollbar_gutter.x;
      height =
        !content_size.height +. constants.content_box_inset.bottom
        -. constants.border.bottom -. constants.scrollbar_gutter.y;
    }

(* Perform absolute layout on absolutely positioned children *)
let perform_absolute_layout_on_absolute_children (type t)
    (module Tree : Tree.LAYOUT_PARTIAL_TREE with type t = t) (tree : t)
    (node : Node_id.t) (constants : algo_constants) : float size =
  let container_width = constants.container_size.width in
  let container_height = constants.container_size.height in
  let inset_relative_size =
    Size.sub constants.container_size
      (Size.add
         (Rect.sum_axes constants.border)
         Size.
           {
             width = constants.scrollbar_gutter.x;
             height = constants.scrollbar_gutter.y;
           })
  in

  let content_size = ref Size.zero in
  let calc = Tree.resolve_calc_value tree in

  for order = 0 to Tree.child_count tree node - 1 do
    let child = Tree.get_child_id tree node order in
    let child_style = Tree.get_core_container_style tree child in

    (* Skip items that are display:none or are not position:absolute *)
    if
      Style.box_generation_mode child_style = Box_generation_mode.None
      || Style.position child_style <> Position.Absolute
    then ()
    else
      let overflow = Style.overflow child_style in
      let scrollbar_width = Style.scrollbar_width child_style in
      let aspect_ratio = Style.aspect_ratio child_style in
      let align_self =
        Option.value
          (Style.align_self child_style)
          ~default:constants.align_items
      in

      (* Resolve margin *)
      let margin =
        Style.margin child_style
        |> Rect.map (fun m ->
            Length_percentage_auto.resolve_to_option_with_calc m
              inset_relative_size.width calc)
      in

      (* Resolve padding *)
      let padding =
        Style.padding child_style
        |> Rect.map (fun p ->
            Length_percentage.resolve_or_zero p (Some inset_relative_size.width)
              calc)
      in

      (* Resolve border *)
      let border =
        Style.border child_style
        |> Rect.map (fun b ->
            Length_percentage.resolve_or_zero b (Some inset_relative_size.width)
              calc)
      in

      let padding_border_sum = Rect.add padding border |> Rect.sum_axes in
      let box_sizing_adjustment =
        if Style.box_sizing child_style = Box_sizing.Content_box then
          padding_border_sum
        else Size.zero
      in

      (* Resolve inset *)
      let left =
        Style.inset child_style |> fun inset ->
        Length_percentage_auto.maybe_resolve inset.left
          (Some inset_relative_size.width) calc
      in
      let right =
        Style.inset child_style |> fun inset ->
        Length_percentage_auto.maybe_resolve inset.right
          (Some inset_relative_size.width) calc
      in
      let top =
        Style.inset child_style |> fun inset ->
        Length_percentage_auto.maybe_resolve inset.top
          (Some inset_relative_size.height) calc
      in
      let bottom =
        Style.inset child_style |> fun inset ->
        Length_percentage_auto.maybe_resolve inset.bottom
          (Some inset_relative_size.height) calc
      in

      (* Compute known dimensions from min/max/inherent size styles *)
      let style_dims = Style.size child_style in
      let style_width =
        Dimension.maybe_resolve style_dims.width
          (Some inset_relative_size.width) calc
      in
      let style_height =
        Dimension.maybe_resolve style_dims.height
          (Some inset_relative_size.height) calc
      in
      let style_width, style_height =
        match (aspect_ratio, style_width, style_height) with
        | Some ratio, Some w, None -> (Some w, Some (w /. ratio))
        | Some ratio, None, Some h -> (Some (h *. ratio), Some h)
        | _ -> (style_width, style_height)
      in
      let style_width, style_height =
        match (box_sizing_adjustment.width, box_sizing_adjustment.height) with
        | 0.0, 0.0 -> (style_width, style_height)
        | bw, bh ->
            ( Option.map (fun w -> w +. bw) style_width,
              Option.map (fun h -> h +. bh) style_height )
      in

      let min_dims = Style.min_size child_style in
      let min_width =
        Dimension.maybe_resolve min_dims.width (Some inset_relative_size.width)
          calc
      in
      let min_height =
        Dimension.maybe_resolve min_dims.height
          (Some inset_relative_size.height) calc
      in
      let min_width, min_height =
        match (aspect_ratio, min_width, min_height) with
        | Some ratio, Some w, None -> (Some w, Some (w /. ratio))
        | Some ratio, None, Some h -> (Some (h *. ratio), Some h)
        | _ -> (min_width, min_height)
      in
      let min_width, min_height =
        match (box_sizing_adjustment.width, box_sizing_adjustment.height) with
        | 0.0, 0.0 -> (min_width, min_height)
        | bw, bh ->
            ( Option.map (fun w -> w +. bw) min_width,
              Option.map (fun h -> h +. bh) min_height )
      in
      let min_width =
        Some
          (Float.max
             (Option.value min_width ~default:padding_border_sum.width)
             padding_border_sum.width)
      in
      let min_height =
        Some
          (Float.max
             (Option.value min_height ~default:padding_border_sum.height)
             padding_border_sum.height)
      in

      let max_dims = Style.max_size child_style in
      let max_width =
        Dimension.maybe_resolve max_dims.width (Some inset_relative_size.width)
          calc
      in
      let max_height =
        Dimension.maybe_resolve max_dims.height
          (Some inset_relative_size.height) calc
      in
      let max_width, max_height =
        match (aspect_ratio, max_width, max_height) with
        | Some ratio, Some w, None -> (Some w, Some (w /. ratio))
        | Some ratio, None, Some h -> (Some (h *. ratio), Some h)
        | _ -> (max_width, max_height)
      in
      let max_width, max_height =
        match (box_sizing_adjustment.width, box_sizing_adjustment.height) with
        | 0.0, 0.0 -> (max_width, max_height)
        | bw, bh ->
            ( Option.map (fun w -> w +. bw) max_width,
              Option.map (fun h -> h +. bh) max_height )
      in

      (* Helper to clamp a value between optional min and max *)
      let clamp_opt value min_val max_val =
        match (value, min_val, max_val) with
        | Some v, Some min_v, Some max_v ->
            Some (Float.max min_v (Float.min v max_v))
        | Some v, Some min_v, None -> Some (Float.max min_v v)
        | Some v, None, Some max_v -> Some (Float.min v max_v)
        | v, _, _ -> v
      in

      (* Clamp style_size between min and max *)
      let known_dimensions =
        {
          Size.width = clamp_opt style_width min_width max_width;
          height = clamp_opt style_height min_height max_height;
        }
      in

      (* Fill in width from left/right and reapply aspect ratio if: - Width is
         not already known - Item has both left and right inset properties
         set *)
      let known_dimensions =
        match (known_dimensions.width, left, right) with
        | None, Some l, Some r ->
            let new_width_raw =
              inset_relative_size.width
              -. Option.value margin.left ~default:0.0
              -. Option.value margin.right ~default:0.0
              -. l -. r
            in
            let width = Some (Float.max new_width_raw 0.0) in
            let width, height =
              match (aspect_ratio, width, known_dimensions.height) with
              | Some ratio, Some w, None -> (Some w, Some (w /. ratio))
              | Some ratio, None, Some h -> (Some (h *. ratio), Some h)
              | _ -> (width, known_dimensions.height)
            in
            {
              Size.width = clamp_opt width min_width max_width;
              height = clamp_opt height min_height max_height;
            }
        | _ -> known_dimensions
      in

      (* Fill in height from top/bottom and reapply aspect ratio if: - Height is
         not already known - Item has both top and bottom inset properties
         set *)
      let known_dimensions =
        match (known_dimensions.height, top, bottom) with
        | None, Some t, Some b ->
            let new_height_raw =
              inset_relative_size.height
              -. Option.value margin.top ~default:0.0
              -. Option.value margin.bottom ~default:0.0
              -. t -. b
            in
            let height = Some (Float.max new_height_raw 0.0) in
            let width, height =
              match (aspect_ratio, known_dimensions.width, height) with
              | Some ratio, Some w, None -> (Some w, Some (w /. ratio))
              | Some ratio, None, Some h -> (Some (h *. ratio), Some h)
              | _ -> (known_dimensions.width, height)
            in
            {
              Size.width = clamp_opt width min_width max_width;
              height = clamp_opt height min_height max_height;
            }
        | _ -> known_dimensions
      in

      let available_space =
        Size.
          {
            width =
              Available_space.of_float
                (Option.value
                   (Option.map
                      (fun w -> Float.min w (Option.value max_width ~default:w))
                      (Option.map
                         (fun w ->
                           Float.max w (Option.value min_width ~default:w))
                         (Some container_width)))
                   ~default:container_width);
            height =
              Available_space.of_float
                (Option.value
                   (Option.map
                      (fun h ->
                        Float.min h (Option.value max_height ~default:h))
                      (Option.map
                         (fun h ->
                           Float.max h (Option.value min_height ~default:h))
                         (Some container_height)))
                   ~default:container_height);
          }
      in

      (* Measure child to determine its inherent size *)
      let measure_output =
        Tree.compute_child_layout tree child
          (Layout_input.make ~run_mode:Run_mode.Compute_size
             ~sizing_mode:Sizing_mode.Inherent_size ~axis:Requested_axis.Both
             ~known_dimensions ~parent_size:constants.node_inner_size
             ~available_space ~vertical_margins_are_collapsible:Line.both_false)
      in

      let measured_size = Layout_output.size measure_output in
      let final_size = Size.unwrap_or measured_size known_dimensions in
      (* Clamp final size between min and max - min wins over max when they
         conflict *)
      let final_size =
        Size.
          {
            width =
              (match (final_size.width, min_width, max_width) with
              | w, Some min_w, Some max_w -> Float.max min_w (Float.min w max_w)
              | w, Some min_w, None -> Float.max min_w w
              | w, None, Some max_w -> Float.min w max_w
              | w, _, _ -> w);
            height =
              (match (final_size.height, min_height, max_height) with
              | h, Some min_h, Some max_h -> Float.max min_h (Float.min h max_h)
              | h, Some min_h, None -> Float.max min_h h
              | h, None, Some max_h -> Float.min h max_h
              | h, _, _ -> h);
          }
      in

      (* Perform child layout with resolved size *)
      let layout_output =
        Tree.compute_child_layout tree child
          (Layout_input.make ~run_mode:Run_mode.Perform_layout
             ~sizing_mode:Sizing_mode.Inherent_size ~axis:Requested_axis.Both
             ~known_dimensions:(Size.map Option.some final_size)
             ~parent_size:constants.node_inner_size ~available_space
             ~vertical_margins_are_collapsible:Line.both_false)
      in

      (* Calculate non-auto margins *)
      let non_auto_margin =
        Rect.map (fun m -> Option.value m ~default:0.0) margin
      in

      (* Calculate free space *)
      let free_space =
        Size.
          {
            width =
              constants.container_size.width -. final_size.width
              -. (non_auto_margin.left +. non_auto_margin.right);
            height =
              constants.container_size.height -. final_size.height
              -. (non_auto_margin.top +. non_auto_margin.bottom);
          }
      in
      let free_space =
        Size.
          {
            width = Float.max free_space.width 0.0;
            height = Float.max free_space.height 0.0;
          }
      in

      (* Expand auto margins to fill available space *)
      let resolved_margin =
        let auto_margin_count_width =
          (if Option.is_none margin.left then 1 else 0)
          + if Option.is_none margin.right then 1 else 0
        in
        let auto_margin_count_height =
          (if Option.is_none margin.top then 1 else 0)
          + if Option.is_none margin.bottom then 1 else 0
        in
        let auto_margin_size =
          Size.
            {
              width =
                (if auto_margin_count_width > 0 then
                   free_space.width /. float_of_int auto_margin_count_width
                 else 0.0);
              height =
                (if auto_margin_count_height > 0 then
                   free_space.height /. float_of_int auto_margin_count_height
                 else 0.0);
            }
        in
        Rect.
          {
            left = Option.value margin.left ~default:auto_margin_size.width;
            right = Option.value margin.right ~default:auto_margin_size.width;
            top = Option.value margin.top ~default:auto_margin_size.height;
            bottom = Option.value margin.bottom ~default:auto_margin_size.height;
          }
      in

      (* Determine flex-relative insets *)
      let start_main, end_main =
        if constants.is_row then (left, right) else (top, bottom)
      in
      let start_cross, end_cross =
        if constants.is_row then (top, bottom) else (left, right)
      in

      (* Apply main-axis alignment *)
      let offset_main =
        match start_main with
        | Some start ->
            start
            +. (if constants.is_row then constants.border.left
                else constants.border.top)
            +.
            if constants.is_row then resolved_margin.left
            else resolved_margin.top
        | None -> (
            match end_main with
            | Some end_val ->
                (if constants.is_row then constants.container_size.width
                 else constants.container_size.height)
                -. (if constants.is_row then constants.border.right
                    else constants.border.bottom)
                -. (if constants.is_row then constants.scrollbar_gutter.x
                    else constants.scrollbar_gutter.y)
                -. (if constants.is_row then final_size.width
                    else final_size.height)
                -. end_val
                -.
                if constants.is_row then resolved_margin.right
                else resolved_margin.bottom
            | None -> (
                (* Handle justify_content alignment *)
                match
                  ( Option.value constants.justify_content ~default:Start,
                    constants.is_wrap_reverse )
                with
                | Space_between, _
                | Start, _
                | Stretch, false
                | Flex_start, false
                | Flex_end, true ->
                    (if constants.is_row then constants.content_box_inset.left
                     else constants.content_box_inset.top)
                    +.
                    if constants.is_row then resolved_margin.left
                    else resolved_margin.top
                | End, _ | Flex_end, false | Flex_start, true | Stretch, true ->
                    (if constants.is_row then constants.container_size.width
                     else constants.container_size.height)
                    -. (if constants.is_row then
                          constants.content_box_inset.right
                        else constants.content_box_inset.bottom)
                    -. (if constants.is_row then final_size.width
                        else final_size.height)
                    -.
                    if constants.is_row then resolved_margin.right
                    else resolved_margin.bottom
                | Space_evenly, _ | Space_around, _ | Center, _ ->
                    ((if constants.is_row then constants.container_size.width
                      else constants.container_size.height)
                    +. (if constants.is_row then
                          constants.content_box_inset.left
                        else constants.content_box_inset.top)
                    -. (if constants.is_row then
                          constants.content_box_inset.right
                        else constants.content_box_inset.bottom)
                    -. (if constants.is_row then final_size.width
                        else final_size.height)
                    +. (if constants.is_row then resolved_margin.left
                        else resolved_margin.top)
                    -.
                    if constants.is_row then resolved_margin.right
                    else resolved_margin.bottom)
                    /. 2.0))
      in

      (* Apply cross-axis alignment *)
      let offset_cross =
        match start_cross with
        | Some start ->
            start
            +. (if constants.is_row then constants.border.top
                else constants.border.left)
            +.
            if constants.is_row then resolved_margin.top
            else resolved_margin.left
        | None -> (
            match end_cross with
            | Some end_val ->
                (if constants.is_row then constants.container_size.height
                 else constants.container_size.width)
                -. (if constants.is_row then constants.border.bottom
                    else constants.border.right)
                -. (if constants.is_row then constants.scrollbar_gutter.y
                    else constants.scrollbar_gutter.x)
                -. (if constants.is_row then final_size.height
                    else final_size.width)
                -. end_val
                -.
                if constants.is_row then resolved_margin.bottom
                else resolved_margin.right
            | None -> (
                match (align_self, constants.is_wrap_reverse) with
                (* Stretch alignment does not apply to absolutely positioned
                   items *)
                | Start, _
                | Baseline, false
                | Stretch, false
                | Flex_start, false
                | Flex_end, true ->
                    (if constants.is_row then constants.content_box_inset.top
                     else constants.content_box_inset.left)
                    +.
                    if constants.is_row then resolved_margin.top
                    else resolved_margin.left
                | End, _
                | Baseline, true
                | Stretch, true
                | Flex_start, true
                | Flex_end, false ->
                    (if constants.is_row then constants.container_size.height
                     else constants.container_size.width)
                    -. (if constants.is_row then
                          constants.content_box_inset.bottom
                        else constants.content_box_inset.right)
                    -. (if constants.is_row then final_size.height
                        else final_size.width)
                    -.
                    if constants.is_row then resolved_margin.bottom
                    else resolved_margin.right
                | Center, _ ->
                    ((if constants.is_row then constants.container_size.height
                      else constants.container_size.width)
                    +. (if constants.is_row then constants.content_box_inset.top
                        else constants.content_box_inset.left)
                    -. (if constants.is_row then
                          constants.content_box_inset.bottom
                        else constants.content_box_inset.right)
                    -. (if constants.is_row then final_size.height
                        else final_size.width)
                    +. (if constants.is_row then resolved_margin.top
                        else resolved_margin.left)
                    -.
                    if constants.is_row then resolved_margin.bottom
                    else resolved_margin.right)
                    /. 2.0))
      in

      let location =
        if constants.is_row then Point.{ x = offset_main; y = offset_cross }
        else Point.{ x = offset_cross; y = offset_main }
      in

      let scrollbar_size =
        Size.
          {
            width =
              (if overflow.y = Overflow.Scroll then scrollbar_width else 0.0);
            height =
              (if overflow.x = Overflow.Scroll then scrollbar_width else 0.0);
          }
      in

      (* Set layout on tree *)
      Tree.set_unrounded_layout tree child
        (Layout.make ~order ~location ~size:final_size
           ~content_size:(Layout_output.content_size layout_output)
           ~scrollbar_size ~border ~padding ~margin:resolved_margin);

      (* Update content size *)
      let content_size_output = Layout_output.content_size layout_output in
      let size_content_size_contribution =
        Size.
          {
            width =
              (match overflow.x with
              | Overflow.Visible ->
                  Float.max final_size.width content_size_output.width
              | _ -> final_size.width);
            height =
              (match overflow.y with
              | Overflow.Visible ->
                  Float.max final_size.height content_size_output.height
              | _ -> final_size.height);
          }
      in

      if
        size_content_size_contribution.width > 0.0
        || size_content_size_contribution.height > 0.0
      then
        let content_size_contribution =
          Size.
            {
              width = location.x +. size_content_size_contribution.width;
              height = location.y +. size_content_size_contribution.height;
            }
        in
        let current_size = !content_size in
        content_size :=
          Size.
            {
              width =
                Float.max current_size.width content_size_contribution.width;
              height =
                Float.max current_size.height content_size_contribution.height;
            }
  done;

  !content_size

(* Compute a preliminary size for an item *)
let compute_preliminary (type t)
    (module Tree : Tree.LAYOUT_PARTIAL_TREE with type t = t) (tree : t)
    (node : Node_id.t) (inputs : Layout_input.t) : Layout_output.t =
  let known_dimensions = Layout_input.known_dimensions inputs in
  let parent_size = Layout_input.parent_size inputs in
  let available_space = Layout_input.available_space inputs in
  let run_mode = Layout_input.run_mode inputs in
  let _sizing_mode = Layout_input.sizing_mode inputs in

  (* Define some general constants we will need for the remainder of the
     algorithm *)
  let constants =
    compute_constants
      (module Tree)
      tree
      (Tree.get_core_container_style tree node)
      known_dimensions parent_size
  in

  (* 9. Flex Layout Algorithm *)

  (* 9.1. Initial Setup *)

  (* 1. Generate anonymous flex items as described in §4 Flex Items *)
  let flex_items =
    generate_anonymous_flex_items (module Tree) tree node constants
  in

  (* 9.2. Line Length Determination *)

  (* 2. Determine the available main and cross space for the flex items *)
  let available_space =
    determine_available_space known_dimensions available_space constants
  in

  (* 3. Determine the flex base size and hypothetical main size of each item *)
  determine_flex_base_size
    (module Tree)
    tree constants available_space flex_items;

  (* 4. Determine the main size of the flex container *)
  (* This is done as part of compute_constants and collection of flex lines *)

  (* 9.3. Main Size Determination *)

  (* 5. Collect flex items into flex lines *)
  let flex_lines = collect_flex_lines constants available_space flex_items in

  (* If container size is undefined, determine the container's main size *)
  (match
     Flex_direction_ext.main_size constants.node_inner_size constants.dir
   with
  | Some inner_main_size ->
      let outer_main_size =
        inner_main_size
        +. Flex_direction_ext.main_axis_sum constants.content_box_inset
             constants.dir
      in
      if constants.is_row then (
        constants.inner_container_size <-
          { constants.inner_container_size with width = inner_main_size };
        constants.container_size <-
          { constants.container_size with width = outer_main_size })
      else (
        constants.inner_container_size <-
          { constants.inner_container_size with height = inner_main_size };
        constants.container_size <-
          { constants.container_size with height = outer_main_size })
  | None ->
      (* Sets constants.container_size and constants.inner_container_size *)
      determine_container_main_size
        (module Tree)
        tree available_space flex_lines constants;
      let inner_main =
        Flex_direction_ext.main_size constants.inner_container_size
          constants.dir
      in
      let outer_main =
        Flex_direction_ext.main_size constants.container_size constants.dir
      in
      if constants.is_row then (
        constants.node_inner_size <-
          { constants.node_inner_size with width = Some inner_main };
        constants.node_outer_size <-
          { constants.node_outer_size with width = Some outer_main })
      else (
        constants.node_inner_size <-
          { constants.node_inner_size with height = Some inner_main };
        constants.node_outer_size <-
          { constants.node_outer_size with height = Some outer_main });

      (* Re-resolve percentage gaps *)
      let style = Tree.get_core_container_style tree node in
      let inner_container_size =
        Flex_direction_ext.main_size constants.inner_container_size
          constants.dir
      in
      let calc = Tree.resolve_calc_value tree in
      let new_gap =
        Style.gap style |> fun gap_size ->
        match constants.dir with
        | Row | Row_reverse ->
            Length_percentage.maybe_resolve (Size.get Inline gap_size)
              (Some inner_container_size) calc
            |> Option.value ~default:0.0
        | Column | Column_reverse ->
            Length_percentage.maybe_resolve (Size.get Block gap_size)
              (Some inner_container_size) calc
            |> Option.value ~default:0.0
      in
      if constants.is_row then
        constants.gap <- { constants.gap with width = new_gap }
      else constants.gap <- { constants.gap with height = new_gap });

  (* 6. Resolve the flexible lengths of all the flex items to find their used
     main size *)
  List.iter (fun line -> resolve_flexible_lengths line constants) flex_lines;

  (* 9.4. Cross Size Determination *)

  (* 7. Determine the hypothetical cross size of each item *)
  List.iter
    (fun line ->
      determine_hypothetical_cross_size
        (module Tree)
        tree line constants available_space)
    flex_lines;

  (* Calculate child baselines *)
  calculate_children_base_lines
    (module Tree)
    tree known_dimensions available_space flex_lines constants;

  (* 8. Calculate the cross size of each flex line *)
  calculate_cross_size flex_lines known_dimensions constants;

  (* 9. Handle 'align-content: stretch' *)
  handle_align_content_stretch flex_lines known_dimensions constants;

  (* 10. Collapse visibility:collapse items - TODO: Not implemented *)

  (* 11. Determine the used cross size of each flex item *)
  determine_used_cross_size (module Tree) tree flex_lines constants;

  (* 9.5. Main-Axis Alignment *)

  (* 12. Distribute any remaining free space *)
  distribute_remaining_free_space flex_lines constants;

  (* 9.6. Cross-Axis Alignment *)

  (* 13. Resolve cross-axis auto margins *)
  resolve_cross_axis_auto_margins flex_lines constants;

  (* 15. Determine the flex container's used cross size *)
  let total_line_cross_size =
    determine_container_cross_size flex_lines known_dimensions constants
  in

  (* We have the container size. If our caller does not care about performing
     layout we are done now *)
  if run_mode = Run_mode.Compute_size then
    Layout_output.from_outer_size constants.container_size
  else (
    (* 16. Align all flex lines per align-content *)
    align_flex_lines_per_align_content flex_lines constants
      total_line_cross_size;

    (* Do a final layout pass and gather the resulting layouts *)
    let inflow_content_size =
      final_layout_pass (module Tree) tree flex_lines constants
    in

    (* Before returning we perform absolute layout on all absolutely positioned
       children *)
    let absolute_content_size =
      perform_absolute_layout_on_absolute_children
        (module Tree)
        tree node constants
    in

    (* Perform hidden layout on hidden children *)
    let len = Tree.child_count tree node in
    for order = 0 to len - 1 do
      let child = Tree.get_child_id tree node order in
      if
        Style.box_generation_mode (Tree.get_core_container_style tree child)
        = Box_generation_mode.None
      then (
        Tree.set_unrounded_layout tree child (Layout.with_order order);
        Tree.compute_child_layout tree child
          (Layout_input.make ~run_mode:Run_mode.Perform_layout
             ~sizing_mode:Sizing_mode.Inherent_size ~axis:Requested_axis.Both
             ~known_dimensions:Size.none ~parent_size:Size.none
             ~available_space:
               Size.
                 {
                   width = Available_space.max_content;
                   height = Available_space.max_content;
                 }
             ~vertical_margins_are_collapsible:Line.both_false)
        |> ignore)
    done;

    (* Calculate flex container baselines *)
    let first_vertical_baseline =
      match flex_lines with
      | [] -> None
      | first_line :: _ -> (
          (* For row containers: find item with baseline alignment or fallback
             to first item For column containers: always use first item *)
          let items = first_line.items in
          let len = Array.length items in
          let baseline_item =
            if Flex_direction_ext.is_row constants.dir then
              let rec find i =
                if i >= len then None
                else
                  let item = items.(i) in
                  if item.align_self = Baseline then Some item else find (i + 1)
              in
              find 0
            else None
          in
          let item_opt =
            match baseline_item with
            | Some item -> Some item
            | None -> if len > 0 then Some items.(0) else None
          in
          match item_opt with
          | None -> None
          | Some item ->
              let offset_cross =
                if Flex_direction_ext.is_row constants.dir then
                  item.offset_cross
                else item.offset_main
              in
              Some (offset_cross +. item.baseline))
    in

    (* Return final layout output *)
    let content_size = Size.max inflow_content_size absolute_content_size in
    Layout_output.make ~size:constants.container_size ~content_size
      ~first_baselines:Point.{ x = None; y = first_vertical_baseline }
      ~top_margin:Collapsible_margin_set.zero
      ~bottom_margin:Collapsible_margin_set.zero
      ~margins_can_collapse_through:false)

(* Main entry point for flexbox layout computation *)
let compute_flexbox_layout (type t)
    (module Tree : Tree.LAYOUT_PARTIAL_TREE with type t = t) (tree : t)
    (node : Node_id.t) (inputs : Layout_input.t) : Layout_output.t =
  let known_dimensions = Layout_input.known_dimensions inputs in
  let parent_size = Layout_input.parent_size inputs in
  let run_mode = Layout_input.run_mode inputs in
  let sizing_mode = Layout_input.sizing_mode inputs in

  let style = Tree.get_core_container_style tree node in
  let calc = Tree.resolve_calc_value tree in

  (* Pull these out earlier to avoid borrowing issues *)
  let aspect_ratio = Style.aspect_ratio style in
  let padding =
    Style.padding style
    |> Rect.map (fun lp ->
        Length_percentage.resolve_or_zero lp parent_size.width calc)
  in
  let border =
    Style.border style
    |> Rect.map (fun lp ->
        Length_percentage.resolve_or_zero lp parent_size.width calc)
  in
  let padding_border_sum = Rect.sum_axes (Rect.add padding border) in
  let box_sizing_adjustment =
    if Style.box_sizing style = Box_sizing.Content_box then padding_border_sum
    else Size.zero
  in

  let min_size =
    Style.min_size style |> fun dims ->
    Size.
      {
        width =
          Dimension.maybe_resolve (Size.get Inline dims) parent_size.width calc;
        height =
          Dimension.maybe_resolve (Size.get Block dims) parent_size.height calc;
      }
    |> Size.apply_aspect_ratio aspect_ratio
    |> Size.maybe_add box_sizing_adjustment
  in

  let max_size =
    Style.max_size style |> fun dims ->
    Size.
      {
        width =
          Dimension.maybe_resolve (Size.get Inline dims) parent_size.width calc;
        height =
          Dimension.maybe_resolve (Size.get Block dims) parent_size.height calc;
      }
    |> Size.apply_aspect_ratio aspect_ratio
    |> Size.maybe_add box_sizing_adjustment
  in

  let clamped_style_size =
    if sizing_mode = Sizing_mode.Inherent_size then
      Style.size style |> fun dims ->
      Size.
        {
          width =
            Dimension.maybe_resolve (Size.get Inline dims) parent_size.width
              calc;
          height =
            Dimension.maybe_resolve (Size.get Block dims) parent_size.height
              calc;
        }
      |> Size.apply_aspect_ratio aspect_ratio
      |> Size.maybe_add box_sizing_adjustment
      |> Size.clamp_option min_size max_size
    else Size.none
  in

  (* If both min and max in a given axis are set and max <= min then this
     determines the size in that axis *)
  let min_max_definite_size =
    Size.map2
      (fun min max ->
        match (min, max) with
        | Some min_v, Some max_v when max_v <= min_v -> Some min_v
        | _ -> None)
      min_size max_size
  in

  (* The size of the container should be floored by the padding and border *)
  let styled_based_known_dimensions =
    known_dimensions
    |> Size.choose_first min_max_definite_size
    |> Size.choose_first clamped_style_size
    |> Size.maybe_max padding_border_sum
  in

  (* Short-circuit layout if the container's size is fully determined and we're
     in ComputeSize mode *)
  if run_mode = Run_mode.Compute_size then
    match styled_based_known_dimensions with
    | { width = Some width; height = Some height } ->
        Layout_output.from_outer_size Size.{ width; height }
    | _ ->
        (* Continue with compute_preliminary *)
        compute_preliminary
          (module Tree)
          tree node
          (Layout_input.make ~run_mode ~sizing_mode
             ~axis:(Layout_input.axis inputs)
             ~known_dimensions:styled_based_known_dimensions ~parent_size
             ~available_space:(Layout_input.available_space inputs)
             ~vertical_margins_are_collapsible:
               (Layout_input.vertical_margins_are_collapsible inputs))
  else
    compute_preliminary
      (module Tree)
      tree node
      (Layout_input.make ~run_mode ~sizing_mode ~axis:(Layout_input.axis inputs)
         ~known_dimensions:styled_based_known_dimensions ~parent_size
         ~available_space:(Layout_input.available_space inputs)
         ~vertical_margins_are_collapsible:
           (Layout_input.vertical_margins_are_collapsible inputs))