Source file typography.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
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
(** Typography utilities for text and font styling. *)

module Css = Cascade.Css

(* Text size variables with line heights *)
let text_xs_var = Var.theme Css.Length "text-xs" ~order:(6, 0)

let text_xs_lh_var =
  Var.theme Css.Line_height "text-xs--line-height" ~order:(6, 1)

let text_sm_var = Var.theme Css.Length "text-sm" ~order:(6, 2)

let text_sm_lh_var =
  Var.theme Css.Line_height "text-sm--line-height" ~order:(6, 3)

let text_base_var = Var.theme Css.Length "text-base" ~order:(6, 4)

let text_base_lh_var =
  Var.theme Css.Line_height "text-base--line-height" ~order:(6, 5)

let text_lg_var = Var.theme Css.Length "text-lg" ~order:(6, 6)

let text_lg_lh_var =
  Var.theme Css.Line_height "text-lg--line-height" ~order:(6, 7)

let text_xl_var = Var.theme Css.Length "text-xl" ~order:(6, 8)

let text_xl_lh_var =
  Var.theme Css.Line_height "text-xl--line-height" ~order:(6, 9)

let text_2xl_var = Var.theme Css.Length "text-2xl" ~order:(6, 10)

let text_2xl_lh_var =
  Var.theme Css.Line_height "text-2xl--line-height" ~order:(6, 11)

let text_3xl_var = Var.theme Css.Length "text-3xl" ~order:(6, 12)

let text_3xl_lh_var =
  Var.theme Css.Line_height "text-3xl--line-height" ~order:(6, 13)

let text_4xl_var = Var.theme Css.Length "text-4xl" ~order:(6, 14)

let text_4xl_lh_var =
  Var.theme Css.Line_height "text-4xl--line-height" ~order:(6, 15)

let text_5xl_var = Var.theme Css.Length "text-5xl" ~order:(6, 16)

let text_5xl_lh_var =
  Var.theme Css.Line_height "text-5xl--line-height" ~order:(6, 17)

let text_6xl_var = Var.theme Css.Length "text-6xl" ~order:(6, 18)

let text_6xl_lh_var =
  Var.theme Css.Line_height "text-6xl--line-height" ~order:(6, 19)

let text_7xl_var = Var.theme Css.Length "text-7xl" ~order:(6, 20)

let text_7xl_lh_var =
  Var.theme Css.Line_height "text-7xl--line-height" ~order:(6, 21)

let text_8xl_var = Var.theme Css.Length "text-8xl" ~order:(6, 22)

let text_8xl_lh_var =
  Var.theme Css.Line_height "text-8xl--line-height" ~order:(6, 23)

let text_9xl_var = Var.theme Css.Length "text-9xl" ~order:(6, 24)

let text_9xl_lh_var =
  Var.theme Css.Line_height "text-9xl--line-height" ~order:(6, 25)

let font_weight_thin_var =
  Var.theme Css.Font_weight "font-weight-thin" ~order:(6, 30)

let font_weight_extralight_var =
  Var.theme Css.Font_weight "font-weight-extralight" ~order:(6, 31)

let font_weight_light_var =
  Var.theme Css.Font_weight "font-weight-light" ~order:(6, 32)

let font_weight_normal_var =
  Var.theme Css.Font_weight "font-weight-normal" ~order:(6, 33)

let font_weight_medium_var =
  Var.theme Css.Font_weight "font-weight-medium" ~order:(6, 34)

let font_weight_semibold_var =
  Var.theme Css.Font_weight "font-weight-semibold" ~order:(6, 35)

let font_weight_bold_var =
  Var.theme Css.Font_weight "font-weight-bold" ~order:(6, 36)

let font_weight_extrabold_var =
  Var.theme Css.Font_weight "font-weight-extrabold" ~order:(6, 37)

let font_weight_black_var =
  Var.theme Css.Font_weight "font-weight-black" ~order:(6, 38)

(* A [--font-weight-*] token the project declared names a weight the built-in
   scale has no slot for; they share the slot after the scale. *)
let font_weight_named_cache = Domain_cache.v 8

let font_weight_named_var name =
  Domain_cache.or_add font_weight_named_cache name (fun () ->
      Var.theme Css.Font_weight ("font-weight-" ^ name) ~order:(6, 38))

(* Theme variables for named tracking values *)
let tracking_tighter_var = Var.theme Css.Length "tracking-tighter" ~order:(6, 39)
let tracking_tight_var = Var.theme Css.Length "tracking-tight" ~order:(6, 40)
let tracking_normal_var = Var.theme Css.Length "tracking-normal" ~order:(6, 41)
let tracking_wide_var = Var.theme Css.Length "tracking-wide" ~order:(6, 42)
let tracking_wider_var = Var.theme Css.Length "tracking-wider" ~order:(6, 43)
let tracking_widest_var = Var.theme Css.Length "tracking-widest" ~order:(6, 44)

(* A [--tracking-*] token the project declared names a letter spacing the
   built-in scale has no slot for; they share the slot after the scale, the way
   the project's font families and text sizes share theirs. *)
let tracking_named_cache = Domain_cache.v 8

let tracking_named_var name =
  Domain_cache.or_add tracking_named_cache name (fun () ->
      Var.theme Css.Length ("tracking-" ^ name) ~order:(6, 45))

(* Publish the weight and tracking scales through the theme-token registry, the
   way rule.ml publishes the breakpoints, so [tracking-[theme(--tracking-wide)]]
   resolves and [theme(static)] emits them. *)
let () =
  List.iter
    (fun (var, weight) -> Theme.register_default var weight)
    [
      (font_weight_thin_var, (Weight 100. : Css.font_weight));
      (font_weight_extralight_var, Weight 200.);
      (font_weight_light_var, Weight 300.);
      (font_weight_normal_var, Weight 400.);
      (font_weight_medium_var, Weight 500.);
      (font_weight_semibold_var, Weight 600.);
      (font_weight_bold_var, Weight 700.);
      (font_weight_extrabold_var, Weight 800.);
      (font_weight_black_var, Weight 900.);
    ];
  List.iter
    (fun (var, spacing) -> Theme.register_default var spacing)
    [
      (tracking_tighter_var, (Em (-0.05) : Css.length));
      (tracking_tight_var, Em (-0.025));
      (* Tailwind's default theme spells the normal tracking [0em], not a
         unitless zero. *)
      (tracking_normal_var, Em 0.0);
      (tracking_wide_var, Em 0.025);
      (tracking_wider_var, Em 0.05);
      (tracking_widest_var, Em 0.1);
    ]

(* Theme variables for named leading values *)
let leading_none_var = Var.theme Css.Line_height "leading-none" ~order:(6, 47)
let leading_tight_var = Var.theme Css.Line_height "leading-tight" ~order:(6, 48)
let leading_snug_var = Var.theme Css.Line_height "leading-snug" ~order:(6, 49)

let leading_normal_var =
  Var.theme Css.Line_height "leading-normal" ~order:(6, 50)

let leading_relaxed_var =
  Var.theme Css.Line_height "leading-relaxed" ~order:(6, 51)

let leading_loose_var = Var.theme Css.Line_height "leading-loose" ~order:(6, 52)

let underline_offset_auto_var =
  Var.theme Css.Length "text-underline-offset-auto" ~order:(6, 55)

let leading_var =
  (* Leading appears in @layer properties after transforms (position 11) *)
  Var.channel ~needs_property:true ~property_order:11 ~family:`Leading
    Css.Line_height "tw-leading"

let font_weight_var =
  (* Font-weight appears in @layer properties after leading (position 12) *)
  Var.channel ~needs_property:true ~property_order:12 ~family:`Font_weight
    Css.Font_weight "tw-font-weight"

let tracking_var =
  (* Tracking doesn't appear in @layer properties in standard Tailwind - only
     referenced *)
  Var.channel ~needs_property:true ~property_order:101 ~family:`Tracking
    Css.Length "tw-tracking"

let ordinal_var =
  Var.channel ~needs_property:true ~property_order:102
    Css.Font_variant_numeric_token "tw-ordinal"

let slashed_var =
  Var.channel ~needs_property:true ~property_order:103
    Css.Font_variant_numeric_token "tw-slashed-zero"

let figure_var =
  Var.channel ~needs_property:true ~property_order:104
    Css.Font_variant_numeric_token "tw-numeric-figure"

let spacing_var =
  Var.channel ~needs_property:true ~property_order:105
    Css.Font_variant_numeric_token "tw-numeric-spacing"

let fraction_var =
  Var.channel ~needs_property:true ~property_order:106
    Css.Font_variant_numeric_token "tw-numeric-fraction"

(* Theme record for line height variables *)
type line_height_theme = { leading : Css.declaration * Css.line_height Css.var }

(* Default line height theme with empty fallback for Tailwind's var(--name,)
   pattern *)
let default_line_height_theme : line_height_theme =
  let leading_decl, leading_ref =
    Var.binding leading_var ~fallback:Css.Empty (Num 1.5)
  in
  { leading = (leading_decl, leading_ref) }

(* Content variable *)
let content_var =
  Var.property_default Content ~initial:(String "") ~universal:true
    ~property_order:100 ~family:`Content "tw-content"

(* Default theme for font variant numeric variables *)
type font_variant_theme = {
  ordinal : Css.declaration * Css.font_variant_numeric_token Css.var;
  slashed : Css.declaration * Css.font_variant_numeric_token Css.var;
  figure : Css.declaration * Css.font_variant_numeric_token Css.var;
  spacing : Css.declaration * Css.font_variant_numeric_token Css.var;
  fraction : Css.declaration * Css.font_variant_numeric_token Css.var;
}

let default_font_variant_theme : font_variant_theme =
  let ordinal_decl, ordinal_ref =
    Var.binding ordinal_var ~fallback:Css.Empty Normal
  in
  let slashed_decl, slashed_ref =
    Var.binding slashed_var ~fallback:Css.Empty Normal
  in
  let figure_decl, figure_ref =
    Var.binding figure_var ~fallback:Css.Empty Normal
  in
  let spacing_decl, spacing_ref =
    Var.binding spacing_var ~fallback:Css.Empty Normal
  in
  let fraction_decl, fraction_ref =
    Var.binding fraction_var ~fallback:Css.Empty Normal
  in
  {
    ordinal = (ordinal_decl, ordinal_ref);
    slashed = (slashed_decl, slashed_ref);
    figure = (figure_decl, figure_ref);
    spacing = (spacing_decl, spacing_ref);
    fraction = (fraction_decl, fraction_ref);
  }

(* Font family theme variables. A project [@theme] can name families of its own
   ([--font-source]); order (1, 100) puts those after the three built-in
   stacks. *)
let font_named_cache = Domain_cache.v 8

let font_named_var name =
  Domain_cache.or_add font_named_cache name (fun () ->
      Var.theme Css.Font_family ("font-" ^ name) ~order:(1, 100))

(* The [--font-<name>--font-feature-settings] and [--font-variation-settings]
   tokens a project declares beside a family, each after the family. *)
let font_named_features_cache = Domain_cache.v 8
let font_named_variations_cache = Domain_cache.v 8

let font_named_features_var name =
  Domain_cache.or_add font_named_features_cache name (fun () ->
      Var.theme Css.Font_feature_settings
        ("font-" ^ name ^ "--font-feature-settings")
        ~order:(1, 101))

let font_named_variations_var name =
  Domain_cache.or_add font_named_variations_cache name (fun () ->
      Var.theme Css.Font_variation_settings
        ("font-" ^ name ^ "--font-variation-settings")
        ~order:(1, 101))

(* A [--text-*] token the project declared names a font size the built-in scale
   has no slot for; they share the slot after the scale, the way the project's
   font families share theirs. *)
let text_named_cache = Domain_cache.v 8

let text_named_var name =
  Domain_cache.or_add text_named_cache name (fun () ->
      Var.theme Css.Length ("text-" ^ name) ~order:(6, 26))

(* The [--text-<name>--line-height], [--letter-spacing] and [--font-weight]
   tokens a project declares beside a size of its own, each after the size. *)
let text_named_lh_cache = Domain_cache.v 8
let text_named_tracking_cache = Domain_cache.v 8
let text_named_weight_cache = Domain_cache.v 8

let text_named_lh_var name =
  Domain_cache.or_add text_named_lh_cache name (fun () ->
      Var.theme Css.Line_height ("text-" ^ name ^ "--line-height") ~order:(6, 27))

let text_named_tracking_var name =
  Domain_cache.or_add text_named_tracking_cache name (fun () ->
      Var.theme Css.Length ("text-" ^ name ^ "--letter-spacing") ~order:(6, 27))

let text_named_weight_var name =
  Domain_cache.or_add text_named_weight_cache name (fun () ->
      Var.theme Css.Font_weight ("text-" ^ name ^ "--font-weight") ~order:(6, 27))

(* The same for a [--leading-*] token the project declared. *)
let leading_named_cache = Domain_cache.v 8

let leading_named_var name =
  Domain_cache.or_add leading_named_cache name (fun () ->
      Var.theme Css.Line_height ("leading-" ^ name) ~order:(6, 53))

let font_sans_var = Var.theme Css.Font_family "font-sans" ~order:(1, 0)
let font_serif_var = Var.theme Css.Font_family "font-serif" ~order:(1, 1)
let font_mono_var = Var.theme Css.Font_family "font-mono" ~order:(1, 2)

(* Default font family variables that reference the base font variables *)
let default_font_family_var =
  Var.theme Css.Font_family "default-font-family" ~order:(8, 39)

let default_mono_font_family_var =
  Var.theme Css.Font_family "default-mono-font-family" ~order:(8, 42)

(* Tailwind v4.3.2 replaced the [ui-sans-serif, system-ui, ...] default with the
   platform system-font stack. *)
let default_sans_stack : Css.font_family =
  List
    [
      Name "-apple-system";
      Name "BlinkMacSystemFont";
      Segoe_ui;
      Roboto;
      Helvetica_neue;
      Noto_sans;
      Arial;
      Sans_serif;
      Apple_color_emoji;
      Segoe_ui_emoji;
      Segoe_ui_symbol;
      Noto_color_emoji;
    ]

let default_mono_stack : Css.font_family =
  List
    [
      Ui_monospace;
      SFMono_regular;
      Menlo;
      Monaco;
      Consolas;
      Liberation_mono;
      Courier_new;
      Monospace;
    ]

let default_serif_stack : Css.font_family =
  List [ Ui_serif; Georgia; Cambria; Times_new_roman; Times; Serif ]

(* Published the same way, so [font-[theme(--font-mono)]] resolves. *)
let () =
  List.iter
    (fun (var, stack) -> Theme.register_default var stack)
    [
      (font_sans_var, default_sans_stack);
      (font_serif_var, default_serif_stack);
      (font_mono_var, default_mono_stack);
    ]

(* Base font family variables for theme layer *)
let default_font_declarations =
  let sans_decl = Var.set font_sans_var default_sans_stack in
  let mono_decl = Var.set font_mono_var default_mono_stack in
  [ sans_decl; mono_decl ]

(* Default font family variables that reference the base font variables. *)
let default_font_family_declarations =
  let sans_decl, sans_ref = Var.binding font_sans_var default_sans_stack in
  let mono_decl, mono_ref = Var.binding font_mono_var default_mono_stack in
  let default_font_decl = Var.set default_font_family_var (Css.Var sans_ref) in
  let default_mono_decl =
    Var.set default_mono_font_family_var (Css.Var mono_ref)
  in
  [ sans_decl; mono_decl; default_font_decl; default_mono_decl ]

(* CSS Fonts 4 sec. 6.4: [font-feature-settings] is [normal] or a comma list of
   [<opentype-tag> [<integer> | on | off]?], the tag being a quoted
   four-character string. The docs' [<value>] placeholder is neither. *)

(** Early typography handler - comes before color utilities (priority 22) *)
let is_feature_tag_list s =
  let s = Parse.decode_underscores s in
  let entry e =
    match
      String.split_on_char ' ' (String.trim e) |> List.filter (( <> ) "")
    with
    | [] -> false
    | tag :: rest -> (
        let n = String.length tag in
        n = 6
        && (tag.[0] = '"' || tag.[0] = '\'')
        && tag.[n - 1] = tag.[0]
        &&
        match rest with
        | [] -> true
        | [ x ] -> x = "on" || x = "off" || int_of_string_opt x <> None
        | _ -> false)
  in
  String.trim s = "normal"
  || (s <> "" && List.for_all entry (String.split_on_char ',' s))

(* CSS Fonts 4 sec. 2.2: a family name is an identifier sequence or a quoted
   string, and the list is comma-separated. *)
let is_font_family_value s =
  let entry e =
    let e = String.trim e in
    let n = String.length e in
    if n = 0 then false
    else if (e.[0] = '"' || e.[0] = '\'') && n > 1 then e.[n - 1] = e.[0]
    else
      String.split_on_char ' ' e |> List.filter (fun w -> w <> "") |> fun ws ->
      ws <> [] && List.for_all Parse.is_ident ws
  in
  s <> "" && List.for_all entry (String.split_on_char ',' s)

module Typography_early = struct
  open Style
  open Css

  type t =
    | (* Text sizes *)
      Text_xs
    | Text_sm
    | Text_base
    | Text_lg
    | Text_xl
    | Text_2xl
    | Text_3xl
    | Text_4xl
    | Text_5xl
    | Text_6xl
    | Text_7xl
    | Text_8xl
    | Text_9xl
    | (* Font weights *)
      Font_thin
    | Font_extralight
    | Font_light
    | Font_normal
    | Font_medium
    | Font_semibold
    | Font_bold
    | Font_extrabold
    | Font_black
    | (* Arbitrary font values - store (bracket_content, parsed_value) *)
      Font_bracket_weight of string * int (* font-[100] *)
    | Font_bracket_weight_var of string * string
      (* font-[number:var(--x)] or font-[var(--x)] *)
    | Font_bracket_family_quoted of string * string (* font-["arial_rounded"] *)
    | Font_bracket_family_name of string * string (* font-[ui-sans-serif] *)
    | Font_bracket_family_var of string * string
      (* font-[family-name:var(--x)] or font-[generic-name:var(--x)] *)
    | (* Font feature settings *)
      Font_features_quoted of string (* font-features-["smcp"] *)
    | Font_features_raw of string * string
      (* Each of these families writes one longhand, so a bracket no reader took
         still names it and the value is forwarded verbatim - Tailwind's
         token-stream contract. *)
    | Font_features_var of string (* font-features-[var(--x)] *)
    | Font_features_bare_var of string (* font-features-(--my-var) *)
    | (* Font families. The flag records whether the theme adds feature
         settings, which gives the candidate a wider property set. *)
      Font_sans of bool
    | Font_serif of bool
    | Font_mono of bool
    | Font_named of string * bool (* font-source, from a --font-* theme token *)
    | Font_weight_theme of string (* weight from a --font-weight-* token *)
    | Leading_theme of string (* line height from a --leading-* token *)
    | (* Font styles *)
      Italic
    | Not_italic
    | (* Text alignment *)
      Text_left
    | Text_center
    | Text_right
    | Text_justify
    | Text_start
    | Text_end
    | (* Leading *)
      Leading_none
    | Leading_tight
    | Leading_snug
    | Leading_normal
    | Leading_relaxed
    | Leading_loose
    | Leading of int
    | Leading_step of float (* leading-0.5, a half-step scale value *)
    | Leading_var of string (* leading-[var(--value)] *)
    (* The author's bracket text travels with the line-height it denotes, so the
       class name is spelled exactly as it was written. *)
    | Leading_bracket of
        string * Css.line_height (* leading-[1.8], leading-[24px], etc. *)
    | Leading_raw of string * string
    | (* Font-size with explicit line-height modifier (text-sm/6) *)
      Text_named_lh of string * lh_modifier
    | (* Font size named by a project [--text-*] token (text-huge) *)
      Text_theme of string
    | Text_theme_lh of string * lh_modifier
    | (* Arbitrary font-size (text-[12px]) *)
      Text_bracket_fs of string
    | (* Arbitrary font-size with line-height (text-[12px]/6) *)
      Text_bracket_fs_lh of string * lh_modifier
    | (* A font-size hint over a value no font-size grammar reads
         (text-[length:red]). The hint chooses the longhand and the value is
         forwarded verbatim, which is Tailwind's token-stream contract; the
         browser discards the declaration. *)
      Text_bracket_fs_raw of string * string

  and lh_modifier =
    | Spacing of int (* /6 → calc(var(--spacing) * 6) *)
    | No_leading (* /none → line-height: 1 *)
    | Named of string (* /snug → var(--leading-snug) *)
    | Bracket of string * Css.line_height (* /[4px] → 4px *)
    | Var_shorthand of string (* /(--lh) → var(--lh) *)

  let name = "typography_early"

  (* Most early-typography families (text-align, font, leading) sort at priority
     24. font-style (italic) is canonically later (rank 87, after tracking/
     whitespace/text-transform), so it joins the late handler's band at priority
     26 via a suborder just above text-transform (~8360). *)
  let priority = function Italic | Not_italic -> 26 | _ -> 24
  let err_not_utility = Error (`Msg "Not an early typography utility")

  (* Lookup table for named text sizes: (name, size_var, default_rem) *)
  let text_size_data =
    [
      ("xs", text_xs_var, 0.75);
      ("sm", text_sm_var, 0.875);
      ("base", text_base_var, 1.0);
      ("lg", text_lg_var, 1.125);
      ("xl", text_xl_var, 1.25);
      ("2xl", text_2xl_var, 1.5);
      ("3xl", text_3xl_var, 1.875);
      ("4xl", text_4xl_var, 2.25);
      ("5xl", text_5xl_var, 3.0);
      ("6xl", text_6xl_var, 3.75);
      ("7xl", text_7xl_var, 4.5);
      ("8xl", text_8xl_var, 6.0);
      ("9xl", text_9xl_var, 8.0);
    ]

  (* The acceptor for [text-<size>] reads the converter's own table: the two
     have to hold the same names, and a name in one but not the other is an
     accepted class that emits no declarations at all. *)
  (* The line-height each text size carries, which its [text-*] utility and its
     [--text-*--line-height] token both spell. *)
  let text_line_heights : (string * Css.line_height) list =
    [
      ("xs", Rem 1.0);
      ("sm", Rem 1.25);
      ("base", Rem 1.5);
      ("lg", Rem 1.75);
      ("xl", Rem 1.75);
      ("2xl", Rem 2.0);
      ("3xl", Rem 2.25);
      ("4xl", Rem 2.5);
      ("5xl", Num 1.0);
      ("6xl", Num 1.0);
      ("7xl", Num 1.0);
      ("8xl", Num 1.0);
      ("9xl", Num 1.0);
    ]

  let line_height_of size = List.assoc size text_line_heights
  let named_sizes = List.map (fun (name, _, _) -> name) text_size_data
  let is_named_size s = List.mem s named_sizes

  (** Font-size keyword lookup. *)
  let font_size_keyword = function
    | "larger" -> Stdlib.Option.Some Css.Larger
    | "smaller" -> Stdlib.Option.Some Css.Smaller
    | "xx-large" -> Stdlib.Option.Some Css.Xx_large
    | "x-large" -> Stdlib.Option.Some Css.X_large
    | "large" -> Stdlib.Option.Some Css.Large
    | "medium" -> Stdlib.Option.Some Css.Medium
    | "small" -> Stdlib.Option.Some Css.Small
    | "x-small" -> Stdlib.Option.Some Css.X_small
    | "xx-small" -> Stdlib.Option.Some Css.Xx_small
    | "xxx-large" -> Stdlib.Option.Some Css.Xxx_large
    | _ -> Stdlib.Option.None

  (** A bracket font-size is any CSS length, but it has to carry a unit:
      Tailwind reads the unitless zero of [text-[0]] as a colour, not a size. *)
  let try_parse_length_value s =
    match Parse.arbitrary_length s with
    | Stdlib.Option.Some Css.Zero -> Stdlib.Option.None
    | len -> len

  (** Split a type prefix like "absolute-size:var(--my-size)" into (prefix,
      value). Finds first ':' outside parens/brackets. *)
  let split_type_prefix inner =
    let len = String.length inner in
    let rec find i depth =
      if i >= len then Stdlib.Option.None
      else
        match inner.[i] with
        | '(' | '[' -> find (i + 1) (depth + 1)
        | ')' | ']' -> find (i + 1) (depth - 1)
        | ':' when depth = 0 ->
            Stdlib.Option.Some
              (String.sub inner 0 i, String.sub inner (i + 1) (len - i - 1))
        | _ -> find (i + 1) depth
    in
    find 0 0

  (* Tailwind's [--spacing(N)] inside an arbitrary value reads the spacing
     scale: [text-[--spacing(2)]] is [font-size: calc(var(--spacing) * 2)]. *)

  (** Does [inner] look like something we can emit as a font-size? Accepts typed
      prefix (length/percentage/absolute-size/relative-size), font-size keyword
      (medium, xxx-large, larger, ...), any CSS length carrying a unit (16px,
      1rem, 1.5em, 3ch, 100%, clamp(...), calc(...)), or var(...). Rejects bare
      identifiers that aren't recognised keywords -- e.g. a bare "1A202C" is not
      a valid font-size: a length must carry a unit and a hex color must start
      with "#" (CSS Color §5.4.6). *)
  let parse_spacing_call s =
    Option.bind (Parse.call_body "--spacing" s) float_of_string_opt

  (* A data-type hint in front of an arbitrary font size says how to read the
     value written after it, so [text-[length:1rem]] is the size [1rem] and not
     a reference to a custom property called [--1rem]. *)
  let is_font_size_hint = function
    | "absolute-size" | "relative-size" | "length" | "percentage" -> true
    | _ -> false

  (* The value a bracket font size carries, told apart from the hint that
     introduces it. *)
  let bracket_font_size_value inner =
    match split_type_prefix inner with
    | Stdlib.Option.Some (prefix, value) when is_font_size_hint prefix ->
        (value, true)
    | _ -> (inner, false)

  (* A hint settles the ambiguity [try_parse_length_value] guards against, so a
     hinted [0] is the size zero where a bare one is a colour. A bare number
     after the hint goes through Tailwind's generator as written and its
     minifier reads it as pixels, which is the spelling [--diff] compares and
     the one [stroke-[1.5]] already follows; see
     docs/token-stream-contract.md. *)
  let bracket_font_size_length value ~hinted =
    if hinted then
      match Parse.arbitrary_length value with
      | Stdlib.Option.Some _ as len -> len
      | Stdlib.Option.None -> (
          match float_of_string_opt value with
          | Some f -> Stdlib.Option.Some (Css.Px f)
          | None -> Stdlib.Option.None)
    else try_parse_length_value value

  (* A [length:] hint says the bracket is a font size whatever the value turns
     out to be, so a value no font-size grammar reads is still a font size -
     forwarded verbatim under the token-stream contract. Without the hint the
     same text names no utility, because nothing says which longhand it is
     for. *)
  let hinted_font_size_token_stream inner =
    match split_type_prefix inner with
    | Stdlib.Option.Some (prefix, _) when is_font_size_hint prefix ->
        Parse.arbitrary_declaration_value inner
    | _ -> Stdlib.Option.None

  let is_valid_bracket_font_size (inner : string) : bool =
    let value, hinted = bracket_font_size_value inner in
    match font_size_keyword value with
    | Some _ -> true
    | Stdlib.Option.None ->
        bracket_font_size_length value ~hinted <> Stdlib.Option.None
        || Parse.is_var value
        || parse_spacing_call value <> Stdlib.Option.None

  (** Check if bracket content looks like a color (should go to color handler).
  *)
  let is_color_bracket inner =
    (* Hex color *)
    (String.length inner > 0 && inner.[0] = '#')
    (* color: typed prefix *)
    || String.starts_with ~prefix:"color:" inner
    (* bare var() without type prefix defaults to color *)
    || Parse.is_var inner
    (* CSS color functions like rgba(...), hsl(...), oklch(...) *)
    || Parse.is_css_color_fn inner

  (** Split on first '/' outside brackets/parens. *)
  let split_on_slash s =
    let len = String.length s in
    let rec find i depth =
      if i >= len then Stdlib.Option.None
      else
        match s.[i] with
        | '[' | '(' -> find (i + 1) (depth + 1)
        | ']' | ')' -> find (i + 1) (depth - 1)
        | '/' when depth = 0 ->
            Stdlib.Option.Some
              (String.sub s 0 i, String.sub s (i + 1) (len - i - 1))
        | _ -> find (i + 1) depth
    in
    find 0 0

  (* The one reader for an arbitrary line-height, shared by [leading-[...]] and
     by the [/leading] modifier on a text size. The string parsing is cascade's,
     so every unit, [calc()] and [var()] spelling it reads is accepted; a
     bracket it cannot read is not a line-height and the utility is refused. *)
  let parse_bracket_leading raw : Css.line_height option =
    if Parse.is_var raw then Some (Var (Var.bracket raw))
    else
      let cursor =
        Cascade.Cursor.of_string (Parse.decode_arbitrary_value raw)
      in
      match
        Cascade.Cursor.try_parse_full_err Css.Properties.read_line_height cursor
      with
      (* The reader keeps a dimension whose unit it does not know as verbatim
         text, so a stray source word reading as one would become a utility. The
         length grammar is what says whether the unit is a length; asking it
         keeps the answer out of a unit table here. *)
      | Ok (Number { repr; unit = Some unit; _ } as lh) ->
          if Css.parse_length (repr ^ unit) = None then None else Some lh
      | Ok lh -> Some lh
      | Error _ -> None

  (* [none] is not here: [parse_lh_modifier] answers it with [No_leading] before
     this list is consulted, because Tailwind writes [line-height: 1] for it
     rather than reading a [--leading-none] token it never defines. A row here
     would contradict that the moment anyone removed the short-circuit. *)
  let known_leading_names = [ "tight"; "snug"; "normal"; "relaxed"; "loose" ]

  (* The modifier names a [--leading-*] token, and one the project declared
     counts the same as a built-in one. *)
  let parse_lh_modifier theme s =
    if s = "none" then Stdlib.Option.Some No_leading
    else if Parse.is_bare_var s then
      (* The [(--name)] shorthand reads the variable as the line height, the way
         [[var(--name)]] does, and keeps its own spelling in the class. *)
      Stdlib.Option.Some (Var_shorthand (Parse.bare_var_inner s))
    else if Parse.is_bracket_value s then
      let inner = Parse.bracket_inner s in
      match parse_bracket_leading inner with
      | Stdlib.Option.Some lh -> Stdlib.Option.Some (Bracket (inner, lh))
      | Stdlib.Option.None -> Stdlib.Option.None
    else
      match Parse.decimal_int s with
      | Stdlib.Option.Some n when n >= 0 -> Stdlib.Option.Some (Spacing n)
      | _ ->
          if
            List.mem s known_leading_names
            || Scheme.theme_value (Some theme) ("leading-" ^ s) <> None
          then Stdlib.Option.Some (Named s)
          else Stdlib.Option.None

  (* A font family the project named in its [@theme]. Gated on the token being
     there so a stray source word (font-awesome) is not a utility. *)
  (* A weight or line height the project named in its [@theme]. The built-in
     scales are matched before these are consulted. *)
  let is_theme_font_weight theme n =
    Scheme.theme_value (Some theme) ("font-weight-" ^ n) <> None

  (* [leading-6] is the spacing-derived numeric form, whatever a project happens
     to have declared, so a plain number is never a token name. *)
  let is_theme_leading theme n =
    Parse.decimal_int n = None
    && Scheme.theme_value (Some theme) ("leading-" ^ n) <> None

  let is_named_font theme n =
    Scheme.theme_value (Some theme) ("font-" ^ n) <> None

  let has_font_features theme n =
    Scheme.theme_value (Some theme) ("font-" ^ n ^ "--font-feature-settings")
    <> None

  (* A font size the project named in its [@theme]. The built-in scale is
     published through the static token catalog, so exclude it: those names have
     their own constructors. [text-<name>] also spells a colour, and a project
     that declares both under one name gets the colour, as Tailwind does.
     [--text-shadow-*] is a namespace of its own, and a doubled hyphen marks a
     modifier on another token ([--text-lg--line-height]), so neither names a
     size; nor does a token whose value is not a length. *)
  let is_theme_text_size theme n =
    (not (is_named_size n))
    && (not (String.starts_with ~prefix:"shadow-" n))
    && List.for_all (fun seg -> seg <> "") (String.split_on_char '-' n)
    && Scheme.theme_value (Some theme) ("color-" ^ n) = None
    &&
    match Scheme.theme_value (Some theme) ("text-" ^ n) with
    | None -> false
    | Some raw -> Css.parse_length raw <> None

  let of_class theme class_name =
    let parts = Parse.split_class class_name in
    match parts with
    | [ "text"; "xs" ] -> Ok Text_xs
    | [ "text"; "sm" ] -> Ok Text_sm
    | [ "text"; "base" ] -> Ok Text_base
    | [ "text"; "lg" ] -> Ok Text_lg
    | [ "text"; "xl" ] -> Ok Text_xl
    | [ "text"; "2xl" ] -> Ok Text_2xl
    | [ "text"; "3xl" ] -> Ok Text_3xl
    | [ "text"; "4xl" ] -> Ok Text_4xl
    | [ "text"; "5xl" ] -> Ok Text_5xl
    | [ "text"; "6xl" ] -> Ok Text_6xl
    | [ "text"; "7xl" ] -> Ok Text_7xl
    | [ "text"; "8xl" ] -> Ok Text_8xl
    | [ "text"; "9xl" ] -> Ok Text_9xl
    | [ "font"; v ] when Parse.is_bracket_value v -> (
        let inner = Parse.bracket_inner v in
        (* A family name is a list of idents or quoted strings, so the docs'
           [<value>] placeholder is not one. [_] separates the words of a name
           and [\_] is a literal underscore inside one, so the stack is read
           decoded while the class keeps the spelling it was written with. *)
        let family raw value =
          if Parse.is_var value then Ok (Font_bracket_family_var (raw, value))
          else
            let decoded = Parse.decode_underscores value in
            if is_font_family_value decoded then
              Ok (Font_bracket_family_name (raw, decoded))
            else err_not_utility
        in
        let weight raw value =
          if Parse.is_var value then Ok (Font_bracket_weight_var (raw, value))
          else
            match Parse.decimal_int value with
            | Some n -> Ok (Font_bracket_weight (raw, n))
            | None -> err_not_utility
        in
        if String.length inner > 0 && inner.[0] = '"' then
          (* font-["arial_rounded"] → the decoded bracket text becomes the
             literal font-family value, quotes and all; Tailwind never wraps it
             in another layer of quoting. *)
          let decoded = Parse.decode_underscores inner in
          Ok (Font_bracket_family_quoted (inner, decoded))
        else
          (* A data-type hint picks which of the two readings applies; the value
             written after it is read the way the un-hinted spelling is, so only
             a var() reference names a custom property. *)
          match split_type_prefix inner with
          | Stdlib.Option.Some (("family-name" | "generic-name"), value) ->
              family inner value
          | Stdlib.Option.Some ("number", value) -> weight inner value
          | _ ->
              if Parse.decimal_int inner <> None || Parse.is_var inner then
                weight inner inner
              else family inner inner)
    | [ "font"; "features"; v ] when Parse.is_bracket_value v -> (
        let inner = Parse.bracket_inner v in
        if Parse.is_var inner then Ok (Font_features_var inner)
        else if is_feature_tag_list inner then Ok (Font_features_quoted inner)
        else
          match Parse.arbitrary_declaration_value inner with
          | Some raw -> Ok (Font_features_raw (inner, raw))
          | None -> err_not_utility)
    | [ "font"; "features"; v ] when Parse.is_bare_var v ->
        Ok (Font_features_bare_var v)
    | [ "font"; "thin" ] -> Ok Font_thin
    | [ "font"; "extralight" ] -> Ok Font_extralight
    | [ "font"; "light" ] -> Ok Font_light
    | [ "font"; "normal" ] -> Ok Font_normal
    | [ "font"; "medium" ] -> Ok Font_medium
    | [ "font"; "semibold" ] -> Ok Font_semibold
    | [ "font"; "bold" ] -> Ok Font_bold
    | [ "font"; "extrabold" ] -> Ok Font_extrabold
    | [ "font"; "black" ] -> Ok Font_black
    | [ "font"; "sans" ] -> Ok (Font_sans (has_font_features theme "sans"))
    | [ "font"; "serif" ] -> Ok (Font_serif (has_font_features theme "serif"))
    | [ "font"; "mono" ] -> Ok (Font_mono (has_font_features theme "mono"))
    | "font" :: (_ :: _ as rest)
      when is_named_font theme (String.concat "-" rest) ->
        let name = String.concat "-" rest in
        Ok (Font_named (name, has_font_features theme name))
    | "font" :: (_ :: _ as rest)
      when is_theme_font_weight theme (String.concat "-" rest) ->
        Ok (Font_weight_theme (String.concat "-" rest))
    | [ "italic" ] -> Ok Italic
    | [ "not"; "italic" ] -> Ok Not_italic
    | [ "text"; "left" ] -> Ok Text_left
    | [ "text"; "center" ] -> Ok Text_center
    | [ "text"; "right" ] -> Ok Text_right
    | [ "text"; "justify" ] -> Ok Text_justify
    | [ "text"; "start" ] -> Ok Text_start
    | [ "text"; "end" ] -> Ok Text_end
    | [ "leading"; "none" ] -> Ok Leading_none
    | [ "leading"; "tight" ] -> Ok Leading_tight
    | [ "leading"; "snug" ] -> Ok Leading_snug
    | [ "leading"; "normal" ] -> Ok Leading_normal
    | [ "leading"; "relaxed" ] -> Ok Leading_relaxed
    | [ "leading"; "loose" ] -> Ok Leading_loose
    | [ "leading"; v ] when Parse.is_bracket_value v -> (
        let inner = Parse.bracket_inner v in
        if Parse.is_var inner then Ok (Leading_var inner)
        else
          match parse_bracket_leading inner with
          | Stdlib.Option.Some lh -> Ok (Leading_bracket (inner, lh))
          | Stdlib.Option.None -> (
              match Parse.arbitrary_declaration_value inner with
              | Some value -> Ok (Leading_raw (inner, value))
              | None -> err_not_utility))
    | "leading" :: (_ :: _ as rest)
      when is_theme_leading theme (String.concat "-" rest) ->
        Ok (Leading_theme (String.concat "-" rest))
    | [ "leading"; n ] -> (
        (* Tailwind accepts any non-negative leading-N (or a quarter-step
           leading-N.M), derived from spacing. *)
        match Parse.spacing_value ~name:"leading" n with
        | Ok f when Float.is_integer f -> Ok (Leading (int_of_float f))
        | Ok f -> Ok (Leading_step f)
        | Error _ as e -> e)
    | [ "text"; part ] -> (
        match split_on_slash part with
        | Stdlib.Option.Some (base, lh_str) -> (
            match parse_lh_modifier theme lh_str with
            | Stdlib.Option.Some lh_mod ->
                if Parse.is_bracket_value base then
                  let inner = Parse.bracket_inner base in
                  if is_color_bracket inner then err_not_utility
                  else if is_valid_bracket_font_size inner then
                    Ok (Text_bracket_fs_lh (inner, lh_mod))
                  else err_not_utility
                else if is_named_size base then
                  Ok (Text_named_lh (base, lh_mod))
                else if is_theme_text_size theme base then
                  Ok (Text_theme_lh (base, lh_mod))
                else err_not_utility
            | Stdlib.Option.None -> err_not_utility)
        | Stdlib.Option.None ->
            if Parse.is_bracket_value part then
              let inner = Parse.bracket_inner part in
              if is_color_bracket inner then err_not_utility
              else if is_valid_bracket_font_size inner then
                Ok (Text_bracket_fs inner)
              else
                match hinted_font_size_token_stream inner with
                | Stdlib.Option.Some value ->
                    Ok (Text_bracket_fs_raw (inner, value))
                | Stdlib.Option.None -> err_not_utility
            else if is_theme_text_size theme part then Ok (Text_theme part)
            else err_not_utility)
    (* A project token's name may span several segments, and the line-height
       modifier still rides on the last one. *)
    | "text" :: (_ :: _ :: _ as rest) -> (
        let joined = String.concat "-" rest in
        match split_on_slash joined with
        | Stdlib.Option.Some (base, lh_str) when is_theme_text_size theme base
          -> (
            match parse_lh_modifier theme lh_str with
            | Stdlib.Option.Some lh_mod -> Ok (Text_theme_lh (base, lh_mod))
            | Stdlib.Option.None -> err_not_utility)
        | _ when is_theme_text_size theme joined -> Ok (Text_theme joined)
        | _ -> err_not_utility)
    | _ -> err_not_utility

  let lh_to_string = function
    | Spacing n -> string_of_int n
    | No_leading -> "none"
    | Named name -> name
    | Bracket (v, _) -> "[" ^ v ^ "]"
    | Var_shorthand name -> "(" ^ name ^ ")"

  let to_class = function
    | Text_xs -> "text-xs"
    | Text_sm -> "text-sm"
    | Text_base -> "text-base"
    | Text_lg -> "text-lg"
    | Text_xl -> "text-xl"
    | Text_2xl -> "text-2xl"
    | Text_3xl -> "text-3xl"
    | Text_4xl -> "text-4xl"
    | Text_5xl -> "text-5xl"
    | Text_6xl -> "text-6xl"
    | Text_7xl -> "text-7xl"
    | Text_8xl -> "text-8xl"
    | Text_9xl -> "text-9xl"
    | Font_thin -> "font-thin"
    | Font_extralight -> "font-extralight"
    | Font_light -> "font-light"
    | Font_normal -> "font-normal"
    | Font_medium -> "font-medium"
    | Font_semibold -> "font-semibold"
    | Font_bold -> "font-bold"
    | Font_extrabold -> "font-extrabold"
    | Font_black -> "font-black"
    | Font_bracket_weight (raw, _) -> "font-[" ^ raw ^ "]"
    | Font_bracket_weight_var (raw, _) -> "font-[" ^ raw ^ "]"
    | Font_bracket_family_quoted (raw, _) -> "font-[" ^ raw ^ "]"
    | Font_bracket_family_name (raw, _) -> "font-[" ^ raw ^ "]"
    | Font_bracket_family_var (raw, _) -> "font-[" ^ raw ^ "]"
    | Font_features_quoted raw | Font_features_raw (raw, _) ->
        "font-features-[" ^ raw ^ "]"
    | Font_features_var raw -> "font-features-[" ^ raw ^ "]"
    | Font_features_bare_var raw -> "font-features-" ^ raw
    | Font_named (name, _) -> "font-" ^ name
    | Font_weight_theme name -> "font-" ^ name
    | Leading_theme name -> "leading-" ^ name
    | Font_sans _ -> "font-sans"
    | Font_serif _ -> "font-serif"
    | Font_mono _ -> "font-mono"
    | Italic -> "italic"
    | Not_italic -> "not-italic"
    | Text_left -> "text-left"
    | Text_center -> "text-center"
    | Text_right -> "text-right"
    | Text_justify -> "text-justify"
    | Text_start -> "text-start"
    | Text_end -> "text-end"
    | Leading_none -> "leading-none"
    | Leading_tight -> "leading-tight"
    | Leading_snug -> "leading-snug"
    | Leading_normal -> "leading-normal"
    | Leading_relaxed -> "leading-relaxed"
    | Leading_loose -> "leading-loose"
    | Leading n -> "leading-" ^ string_of_int n
    | Leading_step f ->
        "leading-" ^ Spacing.pp_spacing_suffix (`Rem (f *. 0.25))
    | Leading_var v -> "leading-[" ^ v ^ "]"
    | Leading_bracket (v, _) -> "leading-[" ^ v ^ "]"
    | Leading_raw (v, _) -> "leading-[" ^ v ^ "]"
    | Text_named_lh (name, lh) -> "text-" ^ name ^ "/" ^ lh_to_string lh
    | Text_theme name -> "text-" ^ name
    | Text_theme_lh (name, lh) -> "text-" ^ name ^ "/" ^ lh_to_string lh
    | Text_bracket_fs raw -> "text-[" ^ raw ^ "]"
    | Text_bracket_fs_raw (raw, _) -> "text-[" ^ raw ^ "]"
    | Text_bracket_fs_lh (raw, lh) -> "text-[" ^ raw ^ "]/" ^ lh_to_string lh

  (** {1 Ordering Support} *)

  let suborder = function
    (* Text align comes first - alphabetical order *)
    | Text_center -> 1001
    | Text_end -> 1002
    | Text_justify -> 1003
    | Text_left -> 1004
    | Text_right -> 1005
    | Text_start -> 1006
    (* Font-family candidates share a slot and sort by class name. A theme
       family that also writes feature settings has a wider property set, so
       Tailwind places it in the preceding slot. *)
    | Font_bracket_family_quoted _ -> 1503
    | Font_bracket_family_var _ -> 1503
    | Font_bracket_family_name _ -> 1503
    (* Bracket font weights come before named font weights *)
    (* Font family - comes between text-align and text-size. *)
    | Font_mono true | Font_sans true | Font_serif true | Font_named (_, true)
      ->
        1502
    | Font_mono false
    | Font_sans false
    | Font_serif false
    | Font_named (_, false) ->
        1503
    | Font_weight_theme _ -> 4201
    | Leading_theme _ -> 3206
    (* Bracket font-size/line-height candidates follow 9xl in the shared slot;
       the candidate-name tiebreak keeps 9xl first. *)
    | Text_bracket_fs_lh _ -> 2008
    (* Font sizes come second - alphabetical order *)
    | Text_2xl -> 2001
    | Text_3xl -> 2002
    | Text_4xl -> 2003
    | Text_5xl -> 2004
    | Text_6xl -> 2005
    | Text_7xl -> 2006
    | Text_8xl -> 2007
    | Text_9xl -> 2008
    | Text_base -> 2009
    | Text_lg -> 2010
    | Text_sm -> 2011
    | Text_xl -> 2012
    | Text_xs -> 2013
    (* Named size with line-height modifier — same suborder as base size *)
    | Text_named_lh (name, _) -> (
        match name with
        | "2xl" -> 2001
        | "3xl" -> 2002
        | "4xl" -> 2003
        | "5xl" -> 2004
        | "6xl" -> 2005
        | "7xl" -> 2006
        | "8xl" -> 2007
        | "9xl" -> 2008
        | "base" -> 2009
        | "lg" -> 2010
        | "sm" -> 2011
        | "xl" -> 2012
        | "xs" -> 2013
        | _ -> 2000)
    (* A project's own size sorts after the whole built-in scale, the way
       Tailwind orders it. *)
    | Text_theme _ -> 2014
    | Text_theme_lh _ -> 2014
    (* Bracket font-size without modifier — after named sizes *)
    | Text_bracket_fs _ | Text_bracket_fs_raw _ -> 2100
    (* Leading comes third — numeric first, then arbitrary, then named
       (alphabetical: loose, none, normal, relaxed, snug, tight) *)
    | Leading n -> 3000 + n
    | Leading_step f -> 3000 + int_of_float (f *. 10.)
    | Leading_var _ -> 3100
    | Leading_bracket _ -> 3100
    | Leading_raw _ -> 3100
    | Leading_loose -> 3201
    | Leading_none -> 3202
    | Leading_normal -> 3203
    | Leading_relaxed -> 3204
    | Leading_snug -> 3205
    | Leading_tight -> 3206
    (* Bracket font weights come before named font weights *)
    | Font_bracket_weight _ -> 4000
    | Font_bracket_weight_var _ -> 4000
    (* Font weight comes fourth - alphabetical order *)
    | Font_black -> 4100
    | Font_bold -> 4200
    | Font_extrabold -> 4300
    | Font_extralight -> 4400
    | Font_light -> 4500
    | Font_medium -> 4600
    | Font_normal -> 4700
    | Font_semibold -> 4800
    | Font_thin -> 4900
    (* Font feature settings *)
    | Font_features_quoted _ | Font_features_raw _ -> 1600
    | Font_features_var _ -> 1600
    | Font_features_bare_var _ -> 1600
    (* Italic *)
    | Italic -> 8380
    | Not_italic -> 8381

  (* Text utilities use theme record for line height variable reference *)
  (* Tailwind v4 expresses a rem line-height as the ratio [calc(line-height /
     font-size)]; unitless line-heights stay verbatim. *)
  let text_line_height ~size_rem : Css.line_height -> Css.line_height = function
    | Rem lh_rem ->
        let num f : Css.line_height Css.calc = Num f in
        Calc (Css.Calc.div (num lh_rem) (num size_rem))
    | other -> other

  let text_size_utility (size_var : Css.length Var.theme)
      (lh_var : Css.line_height Var.theme) size_rem lh_value =
    let size_decl, size_ref = Var.binding size_var (Rem size_rem) in
    let lh_value = text_line_height ~size_rem lh_value in
    let lh_decl, lh_ref = Var.binding lh_var lh_value in
    (* Use shared theme record - no declaration, just reference *)
    let theme = default_line_height_theme in
    let leading_ref = snd theme.leading in
    let leading_with_fallback =
      Css.with_fallback leading_ref (Css.Var lh_ref)
    in
    style
      [
        size_decl;
        lh_decl;
        font_size (Css.Var size_ref);
        line_height (Var leading_with_fallback);
      ]

  let text_xs () =
    text_size_utility text_xs_var text_xs_lh_var 0.75 (line_height_of "xs")

  let text_sm () =
    text_size_utility text_sm_var text_sm_lh_var 0.875 (line_height_of "sm")

  let text_base () =
    text_size_utility text_base_var text_base_lh_var 1.0 (line_height_of "base")

  let text_lg () =
    text_size_utility text_lg_var text_lg_lh_var 1.125 (line_height_of "lg")

  let text_xl () =
    text_size_utility text_xl_var text_xl_lh_var 1.25 (line_height_of "xl")

  let text_2xl () =
    text_size_utility text_2xl_var text_2xl_lh_var 1.5 (line_height_of "2xl")

  let text_3xl () =
    text_size_utility text_3xl_var text_3xl_lh_var 1.875 (line_height_of "3xl")

  let text_4xl () =
    text_size_utility text_4xl_var text_4xl_lh_var 2.25 (line_height_of "4xl")

  let text_5xl () =
    text_size_utility text_5xl_var text_5xl_lh_var 3.0 (line_height_of "5xl")

  let text_6xl () =
    text_size_utility text_6xl_var text_6xl_lh_var 3.75 (line_height_of "6xl")

  let text_7xl () =
    text_size_utility text_7xl_var text_7xl_lh_var 4.5 (line_height_of "7xl")

  let text_8xl () =
    text_size_utility text_8xl_var text_8xl_lh_var 6.0 (line_height_of "8xl")

  let text_9xl () =
    text_size_utility text_9xl_var text_9xl_lh_var 8.0 (line_height_of "9xl")

  (* Font weight utilities set --tw-font-weight for animation but use theme var
     directly *)
  let font_weight_utility weight_var weight_value =
    let weight_theme_decl, weight_theme_ref =
      Var.binding weight_var weight_value
    in
    let weight_util_decl = Var.set font_weight_var (Css.Var weight_theme_ref) in
    (* Get @property rule for font-weight channel (needed for animations) *)
    let property_rules =
      match Var.property_rule font_weight_var with
      | None -> Css.empty
      | Some rule -> rule
    in
    style ~property_rules
      [
        weight_theme_decl;
        weight_util_decl;
        font_weight (Css.Var weight_theme_ref);
      ]

  let font_thin = font_weight_utility font_weight_thin_var (Weight 100.)

  let font_extralight =
    font_weight_utility font_weight_extralight_var (Weight 200.)

  let font_light = font_weight_utility font_weight_light_var (Weight 300.)
  let font_normal = font_weight_utility font_weight_normal_var (Weight 400.)
  let font_medium = font_weight_utility font_weight_medium_var (Weight 500.)
  let font_semibold = font_weight_utility font_weight_semibold_var (Weight 600.)
  let font_bold () = font_weight_utility font_weight_bold_var (Weight 700.)

  let font_extrabold =
    font_weight_utility font_weight_extrabold_var (Weight 800.)

  let font_black = font_weight_utility font_weight_black_var (Weight 900.)

  let font_serif =
    let serif_decl, serif_ref =
      Var.binding font_serif_var default_serif_stack
    in
    style [ serif_decl; font_family (Css.Var serif_ref) ]

  let font_mono =
    let mono_decl, mono_ref = Var.binding font_mono_var default_mono_stack in
    style [ mono_decl; font_family (Css.Var mono_ref) ]

  (* Font family utilities use the font variables directly *)
  let font_sans =
    let sans_decl, sans_ref = Var.binding font_sans_var default_sans_stack in
    style [ sans_decl; font_family (Css.Var sans_ref) ]

  (* [font-source] reads --font-source from the theme; the token has to be
     there, or a stray source word would parse as a utility.

     An [@theme inline] token has no declaration of its own, so the utility
     carries the value instead of a reference to it. A self-referential one
     ([--font-a: var(--font-a)]) is the exception: inlining it would leave the
     reference dangling, so the token keeps its declaration. *)
  (* A family the project declared also carries the [--font-<name>--font-
     feature-settings] beside it, the way Tailwind emits it. *)
  (* The feature and variation settings a project declares beside a family, as
     [--font-<name>--font-feature-settings]: each is a token of its own that
     the utility reads, or the value itself when the block is inline. *)
  let font_feature_decls theme name =
    let sub : type a.
        string ->
        (Cascade.Cursor.t -> a) ->
        a Var.theme ->
        (a -> Css.declaration) ->
        (a Css.var -> Css.declaration) ->
        Css.declaration list =
     fun suffix read var property property_of_var ->
      let token = String.concat "" [ "font-"; name; "--"; suffix ] in
      match Scheme.theme_value (Some theme) token with
      | None -> []
      | Some raw -> (
          match
            Cascade.Cursor.try_parse_full_err read
              (Cascade.Cursor.of_string raw)
          with
          | Error _ -> []
          | Ok value ->
              if Scheme.is_inline_token theme token then [ property value ]
              else
                let decl, ref_ = Var.binding var value in
                [ decl; property_of_var ref_ ])
    in
    sub "font-feature-settings" Css.Properties.read_font_feature_settings
      (font_named_features_var name) font_feature_settings (fun v ->
        font_feature_settings (Var v))
    @ sub "font-variation-settings" Css.Properties.read_font_variation_settings
        (font_named_variations_var name) font_variation_settings (fun v ->
          font_variation_settings (Var v))

  let font_weight_theme theme name =
    match Scheme.theme_value (Some theme) ("font-weight-" ^ name) with
    | None -> style []
    | Some raw -> (
        match Parse.decimal_int raw with
        | None -> style []
        | Some n ->
            font_weight_utility
              (font_weight_named_var name)
              (Css.Weight (float_of_int n)))

  let font_named ?fallback theme name =
    let token = "font-" ^ name in
    match Scheme.theme_value (Some theme) token with
    | None -> ( match fallback with Some s -> s | None -> style [])
    | Some raw -> (
        match Css.parse_font_family raw with
        | None -> ( match fallback with Some s -> s | None -> style [])
        | Some family ->
            let self_referential =
              match family with
              | Css.Var v -> Css.var_name v = token
              | _ -> false
            in
            let features = font_feature_decls theme name in
            if Scheme.is_inline_token theme token && not self_referential then
              style (font_family family :: features)
            else
              let decl, ref = Var.binding (font_named_var name) family in
              style (decl :: font_family (Css.Var ref) :: features))

  let italic = style [ font_style Italic ]
  let not_italic = style [ font_style Normal ]
  let text_left = style [ text_align Left ]
  let text_center = style [ text_align Center ]
  let text_right = style [ text_align Right ]
  let text_justify = style [ text_align Justify ]
  let text_start = style [ text_align Start ]
  let text_end = style [ text_align End ]

  let leading_with_theme_var theme_var default_value =
    let theme_decl, theme_ref = Var.binding theme_var default_value in
    let channel_decl = Var.set leading_var (Css.Var theme_ref) in
    let property_rules =
      Var.property_rule leading_var |> Option.to_list |> Css.concat
    in
    style ~property_rules
      [ theme_decl; channel_decl; line_height (Css.Var theme_ref) ]

  let leading_theme theme name =
    match Scheme.theme_value (Some theme) ("leading-" ^ name) with
    | None -> style []
    | Some raw -> (
        match parse_bracket_leading raw with
        | Stdlib.Option.None -> style []
        | Stdlib.Option.Some lh ->
            leading_with_theme_var (leading_named_var name) lh)

  let leading_none ?theme () =
    match Scheme.theme_value theme "leading-none" with
    | Some _ -> leading_with_theme_var leading_none_var (Num 1.0)
    | None ->
        (* Tailwind v4.3 ships no --leading-none token, so inline the literal
           rather than minting a var. *)
        let value : line_height = Num 1.0 in
        let channel_decl = Var.set leading_var value in
        let property_rules =
          Var.property_rule leading_var |> Option.to_list |> Css.concat
        in
        style ~property_rules [ channel_decl; line_height value ]

  let leading_tight = leading_with_theme_var leading_tight_var (Num 1.25)
  let leading_snug = leading_with_theme_var leading_snug_var (Num 1.375)
  let leading_normal = leading_with_theme_var leading_normal_var (Num 1.5)
  let leading_relaxed = leading_with_theme_var leading_relaxed_var (Num 1.625)
  let leading_loose = leading_with_theme_var leading_loose_var (Num 2.0)

  (* Shared by the int base ([leading]) and its half-step sibling ([leading']):
     [n] is the class number (spacing-scale multiplier), an integer or a
     fractional quarter-step alike. *)
  let leading_calc ?theme (n : float) =
    let name = "leading-" ^ Spacing.pp_spacing_suffix (`Rem (n *. 0.25)) in
    match Scheme.theme_value theme name with
    | Some _ ->
        (* A theme overrides --leading-N: reference it like a named leading. *)
        let theme_var = Var.theme Css.Line_height name ~order:(6, 53) in
        leading_with_theme_var theme_var (Rem (n *. 0.25))
    | None when n = 0.0 ->
        (* leading-0 is a literal 0, not calc(var(--spacing) * 0). *)
        let value : line_height = Num 0.0 in
        let channel_decl = Var.set leading_var value in
        let property_rules =
          Var.property_rule leading_var |> Option.to_list |> Css.concat
        in
        style ~property_rules [ channel_decl; line_height value ]
    | None ->
        (* Tailwind v4.3 default: derive numeric leading from the spacing scale,
           leading-1 -> var(--spacing), leading-N -> calc(var(--spacing) *
           N). *)
        let spacing_decl = Var.set Theme.spacing_var Theme.spacing_base in
        let spacing = Var.name Theme.spacing_var in
        let value : line_height =
          if n = 1.0 then Css.Var (Var.theme_ref spacing)
          else Css.Calc (Css.Calc.mul (Css.Calc.var spacing) (Css.Calc.float n))
        in
        let channel_decl = Var.set leading_var value in
        let property_rules =
          Var.property_rule leading_var |> Option.to_list |> Css.concat
        in
        style ~property_rules [ spacing_decl; channel_decl; line_height value ]

  let leading ?theme n = leading_calc ?theme (float_of_int n)
  let leading' ?theme n = leading_calc ?theme n

  (* The leading scale, published the same way. *)
  let () =
    List.iter
      (fun (name, value) ->
        Scheme.register_default_token ("leading-" ^ name)
          (Css.Pp.to_string ~minify:true Css.Properties.pp_line_height value))
      [
        ("tight", (Num 1.25 : Css.line_height));
        ("snug", Num 1.375);
        ("normal", Num 1.5);
        ("relaxed", Num 1.625);
        ("loose", Num 2.0);
      ]

  (* Publish the text scale through the theme-token registry, the way rule.ml
     publishes the breakpoints, so [theme()] in a project's CSS resolves against
     the same values the utilities use. *)
  let () =
    List.iter
      (fun (name, _var, rem) ->
        Scheme.register_default_token ("text-" ^ name)
          (Css.Pp.to_string Css.pp_length (Css.Rem rem)))
      text_size_data

  (* And each size's line-height, which [theme(static)] declares with the rest
     of the theme. *)
  let () =
    List.iter
      (fun (name, _var, rem) ->
        Scheme.register_default_token
          ("text-" ^ name ^ "--line-height")
          (Css.Pp.to_string ~minify:true Css.Properties.pp_line_height
             (text_line_height ~size_rem:rem (line_height_of name))))
      text_size_data

  (* A theme token's value is text; read it back at the type the property
     wants. *)
  let parse_theme_line_height theme token =
    match Scheme.theme_value (Some theme) token with
    | None -> Stdlib.Option.None
    | Some raw -> (
        let cursor = Cascade.Cursor.of_string raw in
        match
          Cascade.Cursor.try_parse_full_err Css.Properties.read_line_height
            cursor
        with
        | Ok lh -> Stdlib.Option.Some lh
        | Error _ -> Stdlib.Option.None)

  (* Both tables are compile-time constants: building them per call allocated
     the list and a second remapped copy once per utility rendered. *)
  let named_leading_table :
      (string * (Css.line_height Var.theme * Css.line_height)) list =
    [
      (* [none] is absent for the reason [known_leading_names] gives. *)
      ("tight", (leading_tight_var, (Num 1.25 : Css.line_height)));
      ("snug", (leading_snug_var, Num 1.375));
      ("normal", (leading_normal_var, Num 1.5));
      ("relaxed", (leading_relaxed_var, Num 1.625));
      ("loose", (leading_loose_var, Num 2.0));
    ]

  let text_size_table = List.map (fun (n, v, d) -> (n, (v, d))) text_size_data

  (** Convert a line-height modifier to (extra_declarations, line_height_value).
  *)
  let lh_modifier_to_css theme = function
    | Spacing n ->
        let spacing_decl, _spacing_ref =
          Var.binding Theme.spacing_var Theme.spacing_base
        in
        (* [--spacing] holds a length; here it is read as a line height, so the
           reference is built at that kind off the same handle. *)
        let lh_spacing_ref : Css.line_height Css.var =
          Var.bracket (Var.name Theme.spacing_var)
        in
        let lh : Css.line_height =
          Calc (Css.Calc.mul (Var lh_spacing_ref) (Num (float_of_int n)))
        in
        ([ spacing_decl ], lh)
    | No_leading -> ([], Num 1.0)
    | Named name -> (
        match List.assoc_opt name named_leading_table with
        | Stdlib.Option.Some (theme_var, default_val) ->
            let decl, ref_ = Var.binding theme_var default_val in
            ([ decl ], Var ref_)
        | Stdlib.Option.None -> (
            (* A [--leading-*] the project declared: bind it, so the theme layer
               carries the token the utility reads. *)
            match parse_theme_line_height theme ("leading-" ^ name) with
            | Stdlib.Option.Some lh ->
                let decl, ref_ = Var.binding (leading_named_var name) lh in
                ([ decl ], Css.Var ref_)
            | Stdlib.Option.None ->
                let ref_ : Css.line_height Css.var =
                  Var.bracket ("leading-" ^ name)
                in
                ([], Css.Var ref_)))
    | Bracket (_, lh) -> ([], lh)
    | Var_shorthand name -> ([], Css.Var (Var.bracket ("var(" ^ name ^ ")")))

  (* A size the project declared, with the line height, letter spacing and font
     weight the block may declare beside it as [--text-<name>--line-height] and
     its siblings. Each goes through its channel with the token as the fallback,
     as a built-in size's line height does, so [leading-*] on the same element
     still wins. A [/modifier] names the line height itself, and Tailwind then
     writes the size and that line height alone. *)
  let text_theme_decls ?(modified = false) theme name =
    let token = "text-" ^ name in
    (* The channel keeps the token as its fallback either way; an inline token
       puts its value there, as Tailwind writes [var(--tw-leading, 1.2)]. *)
    let sub : type a.
        string ->
        (Cascade.Cursor.t -> a) ->
        a Var.theme ->
        a Var.channel ->
        (a Css.var -> Css.declaration) ->
        Css.declaration list =
     fun suffix read var channel property ->
      let token = String.concat "" [ token; "--"; suffix ] in
      match Scheme.theme_value (Some theme) token with
      | None -> []
      | Some raw -> (
          match
            Cascade.Cursor.try_parse_full_err read
              (Cascade.Cursor.of_string raw)
          with
          | Error _ -> []
          | Ok value ->
              if Scheme.is_inline_token theme token then
                [ property (Var.reference_with_fallback channel value) ]
              else
                let decl = Var.set var value in
                [
                  decl;
                  property (Var.reference_with_var_fallback channel var value);
                ])
    in
    match Scheme.theme_value (Some theme) token with
    | None -> []
    | Some raw -> (
        match Css.parse_length raw with
        | None -> []
        | Some len ->
            let size =
              if Scheme.is_inline_token theme token then [ font_size len ]
              else
                let decl, ref = Var.binding (text_named_var name) len in
                [ decl; font_size (Css.Var ref) ]
            in
            if modified then size
            else
              size
              @ sub "line-height" Css.Properties.read_line_height
                  (text_named_lh_var name) leading_var (fun v ->
                    line_height (Var v))
              @ sub "letter-spacing"
                  (Css.Values.read_length ~allow_negative:true ~length_only:true)
                  (text_named_tracking_var name) tracking_var (fun v ->
                    letter_spacing (Var v))
              @ sub "font-weight" Css.Properties.read_font_weight
                  (text_named_weight_var name) font_weight_var (fun v ->
                    font_weight (Var v)))

  (** Generate font-size + line-height style for a named text size with
      modifier. *)
  let text_named_with_lh theme name lh_mod =
    match List.assoc_opt name text_size_table with
    | Stdlib.Option.Some (size_var, default_rem) ->
        let size_decl, size_ref = Var.binding size_var (Rem default_rem) in
        let lh_extra, lh_value = lh_modifier_to_css theme lh_mod in
        ( (size_decl :: lh_extra) @ [ font_size (Css.Var size_ref) ],
          [ line_height lh_value ] )
    | Stdlib.Option.None ->
        (* Fallback — should not happen for valid named sizes *)
        ([], [])

  (* font_size_keyword / try_parse_length_value / split_type_prefix are defined
     earlier in the module so of_class can validate arbitrary bracket values up
     front. *)

  (* is_valid_bracket_font_size is defined earlier (before of_class). *)

  (** Parse bracket content as font-size declarations (without line-height).
      Caller must have already passed [inner] through
      [is_valid_bracket_font_size]. *)
  let bracket_font_size_decls inner =
    let value, hinted = bracket_font_size_value inner in
    match parse_spacing_call value with
    | Stdlib.Option.Some n ->
        let decl, len = Theme.spacing_calc_float n in
        [ decl; font_size len ]
    | Stdlib.Option.None -> (
        match font_size_keyword value with
        | Stdlib.Option.Some kw -> [ Css.font_size_kw kw ]
        | Stdlib.Option.None -> (
            match bracket_font_size_length value ~hinted with
            | Stdlib.Option.Some fs_len -> [ font_size fs_len ]
            | Stdlib.Option.None ->
                if Parse.is_var value then
                  let bare = Parse.extract_var_name value in
                  [ font_size (Css.Var (Var.bracket bare)) ]
                else
                  invalid_arg
                    ("bracket_font_size_decls: not a valid font-size value: "
                   ^ inner)))

  (** Generate font-size-only style for bracket value. *)
  let bracket_font_size_style raw = style (bracket_font_size_decls raw)

  let to_style theme =
    let leading_none () = leading_none ~theme () in
    let leading n = leading ~theme n in
    let leading_step f = leading' ~theme f in
    function
    | Text_xs -> text_xs ()
    | Text_sm -> text_sm ()
    | Text_base -> text_base ()
    | Text_lg -> text_lg ()
    | Text_xl -> text_xl ()
    | Text_2xl -> text_2xl ()
    | Text_3xl -> text_3xl ()
    | Text_4xl -> text_4xl ()
    | Text_5xl -> text_5xl ()
    | Text_6xl -> text_6xl ()
    | Text_7xl -> text_7xl ()
    | Text_8xl -> text_8xl ()
    | Text_9xl -> text_9xl ()
    | Font_thin -> font_thin
    | Font_extralight -> font_extralight
    | Font_light -> font_light
    | Font_normal -> font_normal
    | Font_medium -> font_medium
    | Font_semibold -> font_semibold
    | Font_bold -> font_bold ()
    | Font_extrabold -> font_extrabold
    | Font_black -> font_black
    | Font_bracket_weight (_, n) ->
        let weight_util_decl =
          Var.set font_weight_var (Weight (float_of_int n))
        in
        let property_rules =
          match Var.property_rule font_weight_var with
          | None -> Css.empty
          | Some rule -> rule
        in
        style ~property_rules
          [ weight_util_decl; font_weight (Weight (float_of_int n)) ]
    | Font_bracket_weight_var (_, var_str) ->
        let bare_name = Parse.extract_var_name var_str in
        let var_ref : Css.font_weight Css.var = Var.bracket bare_name in
        let weight_util_decl = Var.set font_weight_var (Css.Var var_ref) in
        let property_rules =
          match Var.property_rule font_weight_var with
          | None -> Css.empty
          | Some rule -> rule
        in
        style ~property_rules [ weight_util_decl; font_weight (Var var_ref) ]
    | Font_bracket_family_quoted (_, decoded) ->
        let n = String.length decoded in
        if n >= 2 && decoded.[0] = '"' && decoded.[n - 1] = '"' then
          (* font-["arial_rounded"] → the decoded text is one quoted family
             name; strip the quotes cascade's own printer adds back. *)
          style [ font_family (Name (String.sub decoded 1 (n - 2))) ]
        else
          (* font-["liga"_0x10] → not a single quoted name. The decoded text
             already carries its own quoting (a quoted string mixed with other
             tokens); tokenize it and pass the tokens through rather than
             wrapping the whole string in one more layer of quotes. *)
          let tokens =
            Css.Values.read_invalid_value (Cascade.Cursor.of_string decoded)
          in
          style [ font_family (Invalid tokens) ]
    | Font_bracket_family_name (_, s) ->
        (* Each comma segment is its own [<family-name>] (CSS Fonts 4 sec. 2.1),
           so a generic keyword only matches a segment on its own, not the
           bracket as a whole; [font-[Papyrus,fantasy]] is the two-entry stack
           [Papyrus, fantasy], never the single literal name
           ["Papyrus,fantasy"]. A quoted segment is always a custom-ident
           literal, so it never resolves to a generic keyword either. *)
        let family_of_entry entry : Css.font_family =
          let entry = String.trim entry in
          let n = String.length entry in
          if
            n >= 2
            && (entry.[0] = '"' || entry.[0] = '\'')
            && entry.[n - 1] = entry.[0]
          then Css.Name (String.sub entry 1 (n - 2))
          else
            match entry with
            | "ui-sans-serif" -> Css.Ui_sans_serif
            | "ui-serif" -> Css.Ui_serif
            | "ui-monospace" -> Css.Ui_monospace
            | "ui-rounded" -> Css.Ui_rounded
            | "sans-serif" -> Css.Sans_serif
            | "serif" -> Css.Serif
            | "monospace" -> Css.Monospace
            | "cursive" -> Css.Cursive
            | "fantasy" -> Css.Fantasy
            | "system-ui" -> Css.System_ui
            | "emoji" -> Css.Emoji
            | "math" -> Css.Math
            | name -> Css.Name name
        in
        let family =
          match String.split_on_char ',' s with
          | [ single ] -> family_of_entry single
          | entries -> Css.List (List.map family_of_entry entries)
        in
        style [ font_family family ]
    | Font_bracket_family_var (_, var_str) ->
        let bare_name = Parse.extract_var_name var_str in
        let var_ref : Css.font_family Css.var = Var.bracket bare_name in
        style [ font_family (Var var_ref) ]
    | Font_features_raw (_, v) ->
        style
          (Option.to_list (Parse.opaque_declaration "font-feature-settings" v))
    | Font_features_quoted s -> (
        match
          Css.parse_declaration "font-feature-settings"
            (Parse.decode_underscores s)
        with
        | Some decl -> style [ decl ]
        | None -> style [])
    | Font_features_var var_str ->
        let bare_name = Parse.extract_var_name var_str in
        let var_ref : Css.font_feature_settings Css.var =
          Var.bracket bare_name
        in
        style [ font_feature_settings (Var var_ref) ]
    | Font_features_bare_var v ->
        (* bare_var_inner "(--my-features)" → "--my-features"; strip the --
           prefix since var_ref adds it *)
        let inner = Parse.bare_var_inner v in
        let bare_name = Option.value ~default:inner (Parse.bare_name inner) in
        let var_ref : Css.font_feature_settings Css.var =
          Var.bracket bare_name
        in
        style [ font_feature_settings (Var var_ref) ]
    | Font_named (name, _) -> font_named theme name
    | Font_weight_theme name -> font_weight_theme theme name
    | Leading_theme name -> leading_theme theme name
    | Font_sans _ -> font_named ~fallback:font_sans theme "sans"
    | Font_serif _ -> font_named ~fallback:font_serif theme "serif"
    | Font_mono _ -> font_named ~fallback:font_mono theme "mono"
    | Italic -> italic
    | Not_italic -> not_italic
    | Text_left -> text_left
    | Text_center -> text_center
    | Text_right -> text_right
    | Text_justify -> text_justify
    | Text_start -> text_start
    | Text_end -> text_end
    | Leading_none -> leading_none ()
    | Leading_tight -> leading_tight
    | Leading_snug -> leading_snug
    | Leading_normal -> leading_normal
    | Leading_relaxed -> leading_relaxed
    | Leading_loose -> leading_loose
    | Leading n -> leading n
    | Leading_step f -> leading_step f
    | Leading_var v ->
        let bare_name = Parse.extract_var_name v in
        let var_ref : Css.line_height Css.var = Var.bracket bare_name in
        let channel_decl = Var.set leading_var (Css.Var var_ref) in
        let property_rules =
          Var.property_rule leading_var |> Option.to_list |> Css.concat
        in
        style ~property_rules [ channel_decl; line_height (Css.Var var_ref) ]
    | Leading_bracket (_, lh) ->
        let channel_decl = Var.set leading_var lh in
        let property_rules =
          Var.property_rule leading_var |> Option.to_list |> Css.concat
        in
        style ~property_rules [ channel_decl; line_height lh ]
    | Leading_raw (_, value) ->
        let channel_decl =
          Css.custom_property ~layer:"utilities" (Var.css_name leading_var)
            value
        in
        let property_rules =
          Var.property_rule leading_var |> Option.to_list |> Css.concat
        in
        let declarations =
          channel_decl
          :: Option.to_list (Parse.opaque_declaration "line-height" value)
        in
        style
          ~metadata:[ Var.metadata leading_var ]
          ~property_rules declarations
    | Text_named_lh (name, lh_mod) ->
        let fs_decl, lh_decls = text_named_with_lh theme name lh_mod in
        style (fs_decl @ lh_decls)
    | Text_theme name -> style (text_theme_decls theme name)
    | Text_theme_lh (name, lh_mod) ->
        let lh_extra, lh_value = lh_modifier_to_css theme lh_mod in
        style
          (text_theme_decls ~modified:true theme name
          @ lh_extra
          @ [ line_height lh_value ])
    | Text_bracket_fs raw -> bracket_font_size_style raw
    | Text_bracket_fs_raw (_, value) ->
        style (Option.to_list (Parse.opaque_declaration "font-size" value))
    | Text_bracket_fs_lh (raw, lh_mod) ->
        let fs_decls = bracket_font_size_decls raw in
        let lh_extra, lh_value = lh_modifier_to_css theme lh_mod in
        style (fs_decls @ lh_extra @ [ line_height lh_value ])

  let examples =
    [
      Text_base; Font_normal; Font_sans false; Italic; Text_center; Leading_none;
    ]
end

(** Late typography handler - comes after color utilities (priority 24) *)
module Typography_late = struct
  open Style
  open Css

  (* Tailwind reads the bracket as a colour unless it is a length or a
     percentage, so a bare number lands on the colour branch whether it is
     written [2] or [1.5]. *)
  let is_bare_number str =
    let is_number_char c = (c >= '0' && c <= '9') || c = '.' || c = '-' in
    str <> ""
    && String.for_all is_number_char str
    && float_of_string_opt str <> None

  (* A data-type hint chooses which longhand a bracket lands in and says nothing
     about the value. [indent-] and [underline-offset-] each write one longhand,
     so every hint lands there and the length reader is handed what follows it.
     The bracket text kept on the constructor still carries the hint, because
     the class name is what the markup holds. *)
  let arbitrary_length_after_hint inner : Css.length option =
    Option.bind (Parse.value_after_hint inner) Parse.arbitrary_length

  (* An arbitrary decoration thickness is any CSS length. *)
  let parse_decoration_thickness inner : Css.length option =
    Parse.arbitrary_length inner

  (* Tailwind's generator writes the percentage through and its minifier folds
     it to em, 50% -> .5em, which is the spelling its own test corpus records
     and the one [--diff] compares. Reading the unminified sheet instead reports
     a difference that is the minifier's. *)
  let parse_decoration_pct inner : Css.length option =
    match Parse.arbitrary_length inner with
    | Some (Pct pct) -> Some (Em (pct /. 100.))
    | _ -> None

  type t =
    | (* Decoration color *)
      Decoration_color of Color.color * int option
    | Decoration_color_opacity of Color.color * int * Color.opacity_modifier
    | Decoration_transparent
    | Decoration_current
    | Decoration_current_opacity of Color.opacity_modifier
    | Decoration_inherit
    | Decoration_bracket_color of string * Css.color
    | Decoration_bracket_invalid_color of string
    | Decoration_bracket_color_opacity of
        string * Css.color * Color.opacity_modifier
    | Decoration_bracket_var of string
    | Decoration_bracket_var_opacity of string * Color.opacity_modifier
    | Decoration_bracket_color_var of string
    | Decoration_bracket_color_var_opacity of string * Color.opacity_modifier
    | (* Text decoration lines *)
      Underline
    | Overline
    | Line_through
    | No_underline
    | (* Decoration styles *)
      Decoration_solid
    | Decoration_double
    | Decoration_dotted
    | Decoration_dashed
    | Decoration_wavy
    | Decoration_thickness of int
    | Decoration_from_font
    | Decoration_auto
    | Decoration_bracket_thickness of string * Css.length
    | Decoration_bracket_pct of string * Css.length
    | Decoration_bracket_length_var of string
    | Decoration_bracket_pct_var of string
    | (* Tracking *)
      (* The author's bracket text travels with the length it denotes, so the
         class name is spelled exactly as it was written. *)
      Tracking_arbitrary of string * Css.length
    | Neg_tracking_arbitrary of string * Css.length
    | Tracking_raw of string * string
    | Neg_tracking_raw of string * string
    | Tracking_var of string
    | Neg_tracking_var of string
    | Tracking_tighter
    | Tracking_tight
    | Tracking_normal
    | Tracking_wide
    | Tracking_wider
    | Tracking_widest
    | (* Letter spacing named by a project [--tracking-*] token *)
      Tracking_theme of string
    | (* Text transform *)
      Uppercase
    | Lowercase
    | Capitalize
    | Normal_case
    | (* Whitespace *)
      Whitespace_normal
    | Whitespace_nowrap
    | Whitespace_pre
    | Whitespace_pre_line
    | Whitespace_pre_wrap
    | Whitespace_break_spaces
    | (* Vertical align *)
      Align_baseline
    | Align_top
    | Align_middle
    | Align_bottom
    | Align_text_top
    | Align_text_bottom
    | Align_sub
    | Align_super
    | Align_arbitrary_var of string
    | Align_raw of string * string
      (* align-[foo]: one longhand, so the value is forwarded verbatim. *)
    | (* List utilities *)
      List_none
    | List_disc
    | List_decimal
    | List_bracket_var of string
    | List_bracket of string * Css.list_style_type
    | List_inside
    | List_outside
    | List_image_none
    | List_image_bracket_var of string
    | List_image_bracket of string * Css.list_style_image
    | List_image_raw of string * string
    | List_image_url of string
    | (* Underline offset *)
      Underline_offset_auto
    | Underline_offset_0
    | Underline_offset_1
    | Underline_offset_2
    | Underline_offset_4
    | Underline_offset_8
    | Underline_offset_px of float
    | Underline_offset_arbitrary of string * Css.length
    | Underline_offset_raw of string * string
    | Underline_offset_var of string
    | Underline_offset_neg_px of float
    | Underline_offset_neg_arbitrary of string * Css.length
    | Underline_offset_neg_var of string
    | (* Antialiased *)
      Antialiased
    | Subpixel_antialiased
    | (* Text overflow *)
      Text_ellipsis
    | Overflow_ellipsis (* legacy alias for text-ellipsis, keeps its name *)
    | Text_clip
    | Truncate
    | (* Text wrap *)
      Text_wrap
    | Text_nowrap
    | Text_balance
    | Text_pretty
    | (* Break utilities *)
      Break_normal
    | Break_words
    | Break_all
    | Break_keep
    | (* Overflow wrap *)
      Overflow_wrap_normal
    | Overflow_wrap_anywhere
    | Overflow_wrap_break_word
    | (* Hyphens *)
      Hyphens_none
    | Hyphens_manual
    | Hyphens_auto
    | (* Font stretch *)
      Font_stretch_ultra_condensed
    | Font_stretch_extra_condensed
    | Font_stretch_condensed
    | Font_stretch_semi_condensed
    | Font_stretch_normal
    | Font_stretch_semi_expanded
    | Font_stretch_expanded
    | Font_stretch_extra_expanded
    | Font_stretch_ultra_expanded
    | Font_stretch_percent of int
    | (* Numeric variants *)
      Normal_nums
    | Ordinal
    | Slashed_zero
    | Lining_nums
    | Oldstyle_nums
    | Proportional_nums
    | Tabular_nums
    | Diagonal_fractions
    | Stacked_fractions
    | (* Indent and line clamp *)
      Indent of float
    | Indent_neg of float
    | Indent_px
    | Indent_neg_px
    | Indent_arbitrary of string
    | Indent_raw of string * string
    | Indent_neg_arbitrary of string
    | Line_clamp of int
    | Line_clamp_arbitrary of int
    | Line_clamp_raw of string * string
    | Line_clamp_none
    | (* Content *)
      Content_none
    | Content of string
    | Content_squote of string (* single-quoted arbitrary: content-['x'] *)
    | Content_raw of string * [ `Content of Css.content | `Raw of string ]
      (* unquoted arbitrary: content-[attr(before)]; raw class text + value *)
    | Content_named of string (* content-<token> defined in the @theme *)

  let name = "typography_late"

  (* Most late-typography families (decoration, tracking, whitespace,
     word-break) sort at priority 26. Four occupy earlier canonical slots:
     list-style (rank 48, by cursor/resize at priority 11),
     text-overflow/truncate (rank 62, just before overflow at priority 18),
     text-indent and vertical-align (right after text-align in the early handler
     at priority 24), and line-clamp (rank 26, with box-sizing at priority
     3). *)
  let priority = function
    | List_none | List_disc | List_decimal | List_inside | List_outside
    | List_image_none | List_image_url _ | List_bracket_var _
    | List_image_bracket_var _ | List_bracket _ | List_image_bracket _
    | List_image_raw _ ->
        11
    (* line-clamp (rank 26) sits between box-sizing and the display family, so
       it shares box-sizing's priority and sorts after it on suborder. *)
    | Line_clamp _ | Line_clamp_arbitrary _ | Line_clamp_raw _ | Line_clamp_none
      ->
        3
    | Truncate -> 17
    | Indent _ | Indent_neg _ | Indent_px | Indent_neg_px | Indent_arbitrary _
    | Indent_raw _ | Indent_neg_arbitrary _ | Align_baseline | Align_top
    | Align_middle | Align_bottom | Align_sub | Align_super | Align_text_top
    | Align_text_bottom | Align_arbitrary_var _ | Align_raw _ ->
        24
    | Content_none | Content _ | Content_squote _ | Content_raw _
    | Content_named _ ->
        35
    | _ -> 26

  let ( >|= ) = Parse.( >|= )
  let err_not_utility = Error (`Msg "Not a late typography utility")

  (* The length a [tracking-[...]] bracket denotes, read with cascade's own
     grammar so [calc()] and the function forms come for free. [None] is a
     bracket no length reads from, and [of_class] refuses the utility rather
     than leaving [to_style] to raise. *)
  let arbitrary_tracking inner : Css.length option =
    Css.parse_length (Parse.decode_underscores inner)

  (* A letter spacing the project named in its [@theme]. The built-in scale is
     matched before this is consulted, so those names never reach here. *)
  let is_theme_tracking theme n =
    Scheme.theme_value (Some theme) ("tracking-" ^ n) <> None

  let of_class theme class_name =
    let parts = Parse.split_class class_name in
    match parts with
    | [ "underline" ] -> Ok Underline
    | [ "overline" ] -> Ok Overline
    | [ "line"; "through" ] -> Ok Line_through
    | [ "no"; "underline" ] -> Ok No_underline
    | [ "decoration"; "solid" ] -> Ok Decoration_solid
    | [ "decoration"; "double" ] -> Ok Decoration_double
    | [ "decoration"; "dotted" ] -> Ok Decoration_dotted
    | [ "decoration"; "dashed" ] -> Ok Decoration_dashed
    | [ "decoration"; "wavy" ] -> Ok Decoration_wavy
    | [ "decoration"; "from"; "font" ] -> Ok Decoration_from_font
    | [ "decoration"; "auto" ] -> Ok Decoration_auto
    | [ "decoration"; "transparent" ] -> Ok Decoration_transparent
    | [ "decoration"; "inherit" ] -> Ok Decoration_inherit
    | [ "decoration"; n ] -> (
        (* Parse opacity modifier first *)
        let base_str, opacity = Color.parse_opacity_modifier ~theme n in
        (* Check for "current" with optional opacity *)
        match (base_str, opacity) with
        | "current", Color.No_opacity -> Ok Decoration_current
        | "current", opacity -> Ok (Decoration_current_opacity opacity)
        | _ -> (
            if
              (* Check for bracket values *)
              Parse.is_bracket_value base_str
            then
              let inner = Parse.bracket_inner base_str in
              (* [raw] is the bracket text the class was written with, hint
                 included, so the class prints back the way it was read. *)
              let bracket_color raw value =
                match Color.parse_bracket_color value with
                | Some c -> (
                    match opacity with
                    | Color.No_opacity -> Ok (Decoration_bracket_color (raw, c))
                    | _ ->
                        Ok (Decoration_bracket_color_opacity (raw, c, opacity)))
                | None -> err_not_utility
              in
              let bracket_pct raw value =
                match parse_decoration_pct value with
                | Some len -> Ok (Decoration_bracket_pct (raw, len))
                | None -> err_not_utility
              in
              let bracket_thickness raw value =
                if
                  String.length value > 0
                  && value.[String.length value - 1] = '%'
                then bracket_pct raw value
                else
                  match parse_decoration_thickness value with
                  | Some len -> Ok (Decoration_bracket_thickness (raw, len))
                  | None -> err_not_utility
              in
              (* A data-type hint says how to read the value written after it,
                 so only a var() reference there names a custom property. *)
              if String.starts_with ~prefix:"color:" inner then
                let value = String.sub inner 6 (String.length inner - 6) in
                if Parse.is_var value then
                  match opacity with
                  | Color.No_opacity -> Ok (Decoration_bracket_color_var value)
                  | _ ->
                      Ok (Decoration_bracket_color_var_opacity (value, opacity))
                else bracket_color inner value
              else if String.starts_with ~prefix:"var(" inner then
                match opacity with
                | Color.No_opacity -> Ok (Decoration_bracket_var inner)
                | _ -> Ok (Decoration_bracket_var_opacity (inner, opacity))
              else if String.starts_with ~prefix:"length:" inner then
                let value = String.sub inner 7 (String.length inner - 7) in
                if Parse.is_var value then
                  Ok (Decoration_bracket_length_var value)
                else bracket_thickness inner value
              else if String.starts_with ~prefix:"percentage:" inner then
                let value = String.sub inner 11 (String.length inner - 11) in
                if Parse.is_var value then Ok (Decoration_bracket_pct_var value)
                else bracket_pct inner value
              else
                (* Every colour spelling CSS knows, not only a [#] hex and a
                   colour function: a named colour and a keyword name a
                   decoration colour too. The colour reader runs before the
                   percentage one because [hsl(200_50%_50%)] ends in a [%]. *)
                match Color.parse_bracket_color inner with
                | Some _ -> bracket_color inner inner
                | None -> (
                    if
                      String.length inner > 0
                      && inner.[String.length inner - 1] = '%'
                    then
                      match parse_decoration_pct inner with
                      | Some len -> Ok (Decoration_bracket_pct (inner, len))
                      | None -> err_not_utility
                    else if is_bare_number inner then
                      (* Tailwind resolves the ambiguous unitless spelling
                         through the colour candidate path. Its declaration is
                         invalid CSS and browsers discard it; accepting an inert
                         candidate is therefore equivalent, while treating it as
                         px is not. *)
                      Ok (Decoration_bracket_invalid_color inner)
                    else
                      match parse_decoration_thickness inner with
                      | Some len ->
                          Ok (Decoration_bracket_thickness (inner, len))
                      | None -> err_not_utility)
            else
              (* Try parsing as number (decoration thickness) *)
              match Parse.int_any base_str with
              | Ok i when opacity = Color.No_opacity ->
                  Ok (Decoration_thickness i)
              | _ -> (
                  (* A shadeless colour (white, black, a theme name) carries its
                     opacity on the single segment, so read the segment as a
                     colour and an opacity together. *)
                  match Color.shade_and_opacity_of_strings ~theme [ n ] with
                  | Ok (c, _, Color.No_opacity) ->
                      Ok (Decoration_color (c, None))
                  | Ok (c, shade, opacity) ->
                      Ok (Decoration_color_opacity (c, shade, opacity))
                  | Error _ -> err_not_utility)))
    | [ "decoration"; color; shade ] -> (
        match Color.shade_and_opacity_of_strings ~theme [ color; shade ] with
        | Ok (c, s, Color.No_opacity) -> Ok (Decoration_color (c, Some s))
        | Ok (c, s, opacity) -> Ok (Decoration_color_opacity (c, s, opacity))
        | Error _ -> err_not_utility)
    | [ "tracking"; v ] when Parse.is_bracket_value v -> (
        let inner = Parse.bracket_inner v in
        if Parse.is_var inner then Ok (Tracking_var inner)
        else
          match arbitrary_tracking inner with
          | Some len -> Ok (Tracking_arbitrary (inner, len))
          | None -> (
              match Parse.arbitrary_declaration_value inner with
              | Some value -> Ok (Tracking_raw (inner, value))
              | None -> Error (`Msg ("Invalid tracking value: " ^ inner))))
    | "" :: "tracking" :: rest when rest <> [] -> (
        let value = String.concat "-" rest in
        if not (Parse.is_bracket_value value) then err_not_utility
        else
          let inner = Parse.bracket_inner value in
          if Parse.is_var inner then Ok (Neg_tracking_var inner)
          else
            match arbitrary_tracking inner with
            | Some len -> Ok (Neg_tracking_arbitrary (inner, len))
            | None -> (
                match Parse.arbitrary_declaration_value inner with
                | Some value -> Ok (Neg_tracking_raw (inner, value))
                | None -> Error (`Msg ("Invalid tracking value: " ^ inner))))
    | [ "tracking"; "tighter" ] -> Ok Tracking_tighter
    | [ "tracking"; "tight" ] -> Ok Tracking_tight
    | [ "tracking"; "normal" ] -> Ok Tracking_normal
    | [ "tracking"; "wide" ] -> Ok Tracking_wide
    | [ "tracking"; "wider" ] -> Ok Tracking_wider
    | [ "tracking"; "widest" ] -> Ok Tracking_widest
    | [ "tracking"; name ] when is_theme_tracking theme name ->
        Ok (Tracking_theme name)
    | "tracking" :: (_ :: _ :: _ as rest)
      when is_theme_tracking theme (String.concat "-" rest) ->
        Ok (Tracking_theme (String.concat "-" rest))
    | [ "uppercase" ] -> Ok Uppercase
    | [ "lowercase" ] -> Ok Lowercase
    | [ "capitalize" ] -> Ok Capitalize
    | [ "normal"; "case" ] -> Ok Normal_case
    | [ "whitespace"; "normal" ] -> Ok Whitespace_normal
    | [ "whitespace"; "nowrap" ] -> Ok Whitespace_nowrap
    | [ "whitespace"; "pre" ] -> Ok Whitespace_pre
    | [ "whitespace"; "pre"; "line" ] -> Ok Whitespace_pre_line
    | [ "whitespace"; "pre"; "wrap" ] -> Ok Whitespace_pre_wrap
    | [ "whitespace"; "break"; "spaces" ] -> Ok Whitespace_break_spaces
    | [ "align"; v ] when Parse.is_bracket_var v ->
        Ok (Align_arbitrary_var (Parse.bracket_inner v))
    | [ "align"; v ] when Parse.is_bracket_value v -> (
        let inner = Parse.bracket_inner v in
        match Parse.arbitrary_declaration_value inner with
        | Some raw -> Ok (Align_raw (inner, raw))
        | None -> err_not_utility)
    | [ "align"; "baseline" ] -> Ok Align_baseline
    | [ "align"; "top" ] -> Ok Align_top
    | [ "align"; "middle" ] -> Ok Align_middle
    | [ "align"; "bottom" ] -> Ok Align_bottom
    | [ "align"; "text"; "top" ] -> Ok Align_text_top
    | [ "align"; "text"; "bottom" ] -> Ok Align_text_bottom
    | [ "align"; "sub" ] -> Ok Align_sub
    | [ "align"; "super" ] -> Ok Align_super
    | [ "list"; value ] when Parse.is_bracket_var value ->
        Ok (List_bracket_var (Parse.bracket_inner value))
    | [ "list"; value ] when Parse.is_bracket_value value -> (
        let inner = Parse.bracket_inner value in
        match
          Css.parse_list_style_type (Parse.decode_arbitrary_value inner)
        with
        | Some t -> Ok (List_bracket (inner, t))
        | None -> err_not_utility)
    | [ "list"; "none" ] -> Ok List_none
    | [ "list"; "disc" ] -> Ok List_disc
    | [ "list"; "decimal" ] -> Ok List_decimal
    | [ "list"; "inside" ] -> Ok List_inside
    | [ "list"; "outside" ] -> Ok List_outside
    | [ "list"; "image"; "none" ] -> Ok List_image_none
    | [ "list"; "image"; value ] when Parse.is_bracket_var value ->
        Ok (List_image_bracket_var (Parse.bracket_inner value))
    (* The [(--x)] shorthand names a custom property, the same as
       [list-image-[var(--x)]]. *)
    | [ "list"; "image"; value ] when Parse.is_bare_var value ->
        Ok (List_image_bracket_var value)
    | [ "list"; "image"; value ] when Parse.is_bracket_value value -> (
        let inner = Parse.bracket_inner value in
        match
          Css.parse_list_style_image (Parse.decode_arbitrary_value inner)
        with
        | Some i -> Ok (List_image_bracket (inner, i))
        | None -> (
            match Parse.arbitrary_declaration_value inner with
            | Some raw -> Ok (List_image_raw (inner, raw))
            | None -> err_not_utility))
    | "list" :: "image" :: rest when rest <> [] ->
        let url = String.concat "-" rest in
        if Parse.is_valid_theme_name url then Ok (List_image_url url)
        else err_not_utility
    | [ "underline"; "offset"; "auto" ] -> Ok Underline_offset_auto
    | [ "underline"; "offset"; "0" ] -> Ok Underline_offset_0
    | [ "underline"; "offset"; "1" ] -> Ok Underline_offset_1
    | [ "underline"; "offset"; "2" ] -> Ok Underline_offset_2
    | [ "underline"; "offset"; "4" ] -> Ok Underline_offset_4
    | [ "underline"; "offset"; "8" ] -> Ok Underline_offset_8
    | [ "underline"; "offset"; n ] when Parse.is_bracket_value n -> (
        let inner = Parse.bracket_inner n in
        if Parse.is_var inner then Ok (Underline_offset_var inner)
        else
          match arbitrary_length_after_hint inner with
          | Some len -> Ok (Underline_offset_arbitrary (inner, len))
          | None -> (
              match Parse.arbitrary_declaration_value inner with
              | Some raw -> Ok (Underline_offset_raw (inner, raw))
              | None -> err_not_utility))
    | [ "underline"; "offset"; n ] -> (
        match Parse.decimal_float n with
        | Some px -> Ok (Underline_offset_px px)
        | None -> err_not_utility)
    | [ ""; "underline"; "offset"; n ] when Parse.is_bracket_value n -> (
        let inner = Parse.bracket_inner n in
        if Parse.is_var inner then Ok (Underline_offset_neg_var inner)
        else
          match arbitrary_length_after_hint inner with
          | Some len -> Ok (Underline_offset_neg_arbitrary (inner, len))
          | None -> err_not_utility)
    | [ ""; "underline"; "offset"; n ] -> (
        match Parse.decimal_float n with
        | Some px -> Ok (Underline_offset_neg_px px)
        | None -> err_not_utility)
    | [ "antialiased" ] -> Ok Antialiased
    | [ "subpixel"; "antialiased" ] -> Ok Subpixel_antialiased
    | [ "text"; "ellipsis" ] -> Ok Text_ellipsis
    | [ "overflow"; "ellipsis" ] -> Ok Overflow_ellipsis
    | [ "text"; "clip" ] -> Ok Text_clip
    | [ "truncate" ] -> Ok Truncate
    | [ "text"; "wrap" ] -> Ok Text_wrap
    | [ "text"; "nowrap" ] -> Ok Text_nowrap
    | [ "text"; "balance" ] -> Ok Text_balance
    | [ "text"; "pretty" ] -> Ok Text_pretty
    | [ "break"; "normal" ] -> Ok Break_normal
    | [ "break"; "words" ] -> Ok Break_words
    | [ "break"; "all" ] -> Ok Break_all
    | [ "break"; "keep" ] -> Ok Break_keep
    | [ "overflow"; "wrap"; "normal" ] -> Ok Overflow_wrap_normal
    | [ "overflow"; "wrap"; "anywhere" ] -> Ok Overflow_wrap_anywhere
    | [ "overflow"; "wrap"; "break"; "word" ] -> Ok Overflow_wrap_break_word
    | [ "hyphens"; "none" ] -> Ok Hyphens_none
    | [ "hyphens"; "manual" ] -> Ok Hyphens_manual
    | [ "hyphens"; "auto" ] -> Ok Hyphens_auto
    | [ "font"; "stretch"; "ultra"; "condensed" ] ->
        Ok Font_stretch_ultra_condensed
    | [ "font"; "stretch"; "extra"; "condensed" ] ->
        Ok Font_stretch_extra_condensed
    | [ "font"; "stretch"; "condensed" ] -> Ok Font_stretch_condensed
    | [ "font"; "stretch"; "semi"; "condensed" ] ->
        Ok Font_stretch_semi_condensed
    | [ "font"; "stretch"; "normal" ] -> Ok Font_stretch_normal
    | [ "font"; "stretch"; "semi"; "expanded" ] -> Ok Font_stretch_semi_expanded
    | [ "font"; "stretch"; "expanded" ] -> Ok Font_stretch_expanded
    | [ "font"; "stretch"; "extra"; "expanded" ] ->
        Ok Font_stretch_extra_expanded
    | [ "font"; "stretch"; "ultra"; "expanded" ] ->
        Ok Font_stretch_ultra_expanded
    | [ "font"; "stretch"; n ] when String.ends_with ~suffix:"%" n ->
        let n = String.sub n 0 (String.length n - 1) in
        Parse.int_bounded ~name:"font-stretch" ~min:50 ~max:200 n >|= fun i ->
        Font_stretch_percent i
    | [ "normal"; "nums" ] -> Ok Normal_nums
    | [ "ordinal" ] -> Ok Ordinal
    | [ "slashed"; "zero" ] -> Ok Slashed_zero
    | [ "lining"; "nums" ] -> Ok Lining_nums
    | [ "oldstyle"; "nums" ] -> Ok Oldstyle_nums
    | [ "proportional"; "nums" ] -> Ok Proportional_nums
    | [ "tabular"; "nums" ] -> Ok Tabular_nums
    | [ "diagonal"; "fractions" ] -> Ok Diagonal_fractions
    | [ "stacked"; "fractions" ] -> Ok Stacked_fractions
    | [ "indent"; "px" ] -> Ok Indent_px
    | [ ""; "indent"; "px" ] -> Ok Indent_neg_px
    | [ "indent"; n ] when Parse.is_bracket_value n -> (
        let inner = Parse.bracket_inner n in
        match arbitrary_length_after_hint inner with
        | Some _ -> Ok (Indent_arbitrary inner)
        | None -> (
            match Parse.arbitrary_declaration_value inner with
            | Some raw -> Ok (Indent_raw (inner, raw))
            | None -> err_not_utility))
    | [ ""; "indent"; n ] when Parse.is_bracket_value n ->
        let inner = Parse.bracket_inner n in
        if arbitrary_length_after_hint inner = None then err_not_utility
        else Ok (Indent_neg_arbitrary inner)
    | [ "indent"; n ] -> (
        match Parse.spacing_value ~name:"indent" n with
        | Ok f when Theme.has_spacing_step ~theme f -> Ok (Indent f)
        | Ok _ | Error _ -> err_not_utility)
    | [ ""; "indent"; n ] -> (
        match Parse.spacing_value ~name:"indent" n with
        | Ok f when Theme.has_spacing_step ~theme f -> Ok (Indent_neg f)
        | Ok _ | Error _ -> err_not_utility)
    | [ "line"; "clamp"; "none" ] -> Ok Line_clamp_none
    | [ "line"; "clamp"; n ] when Parse.is_bracket_value n -> (
        let inner = Parse.bracket_inner n in
        match Parse.decimal_int inner with
        | Some i -> Ok (Line_clamp_arbitrary i)
        | None -> (
            match Parse.arbitrary_declaration_value inner with
            | Some value -> Ok (Line_clamp_raw (inner, value))
            | None -> err_not_utility))
    | [ "line"; "clamp"; n ] ->
        Parse.int_bounded ~name:"line-clamp" ~min:0 ~max:999 n >|= fun i ->
        Line_clamp i
    | [ "content"; "none" ] -> Ok Content_none
    | "content" :: rest ->
        let joined = String.concat "-" rest in
        (* Parse arbitrary value syntax: content-["value"] -> value *)
        let bracket_quoted q =
          String.length joined >= 4
          && String.get joined 0 = '['
          && String.get joined 1 = q
          && String.get joined (String.length joined - 1) = ']'
          && String.get joined (String.length joined - 2) = q
        in
        (* v4 has content-none, arbitrary content-[...], and content-<token>
           when the @theme defines --content-<token>. A bare word like
           content-wrapper with no such token is not a utility (it used to be
           wrongly accepted). An unquoted bracket (content-[attr(before)])
           carries a function value, parsed as a content list. *)
        if bracket_quoted '"' then
          let value = String.sub joined 2 (String.length joined - 4) in
          Ok (Content value)
        else if bracket_quoted '\'' then
          let value = String.sub joined 2 (String.length joined - 4) in
          Ok (Content_squote value)
        else if Scheme.theme_value (Some theme) ("content-" ^ joined) <> None
        then Ok (Content_named joined)
        else if
          String.length joined >= 2
          && String.get joined 0 = '['
          && String.get joined (String.length joined - 1) = ']'
        then begin
          let inner = String.sub joined 1 (String.length joined - 2) in
          let spaced = Parse.decode_underscores inner in
          match
            Cascade.Cursor.try_parse_full_err Css.Properties.read_content
              (Cascade.Cursor.of_string spaced)
          with
          | Ok value -> Ok (Content_raw (inner, `Content value))
          | Error _ -> (
              match Parse.arbitrary_declaration_value inner with
              | Some value -> Ok (Content_raw (inner, `Raw value))
              | None -> err_not_utility)
        end
        else err_not_utility
    | _ -> err_not_utility

  let to_class = function
    | Decoration_color (color, None) -> "decoration-" ^ Color.pp color
    | Decoration_color (color, Some shade) ->
        "decoration-" ^ Color.pp color ^ "-" ^ string_of_int shade
    | Decoration_color_opacity (color, shade, opacity) ->
        let base =
          if Color.is_shadeless color then "decoration-" ^ Color.pp color
          else "decoration-" ^ Color.pp color ^ "-" ^ string_of_int shade
        in
        base ^ "/" ^ Color.pp_opacity opacity
    | Decoration_transparent -> "decoration-transparent"
    | Decoration_current -> "decoration-current"
    | Decoration_current_opacity opacity ->
        "decoration-current/" ^ Color.pp_opacity opacity
    | Decoration_inherit -> "decoration-inherit"
    | Decoration_bracket_color (v, _) -> "decoration-[" ^ v ^ "]"
    | Decoration_bracket_invalid_color v -> "decoration-[" ^ v ^ "]"
    | Decoration_bracket_color_opacity (v, _, opacity) ->
        "decoration-[" ^ v ^ "]/" ^ Color.pp_opacity opacity
    | Decoration_bracket_var v -> "decoration-[" ^ v ^ "]"
    | Decoration_bracket_var_opacity (v, opacity) ->
        "decoration-[" ^ v ^ "]/" ^ Color.pp_opacity opacity
    | Decoration_bracket_color_var v -> "decoration-[color:" ^ v ^ "]"
    | Decoration_bracket_color_var_opacity (v, opacity) ->
        "decoration-[color:" ^ v ^ "]/" ^ Color.pp_opacity opacity
    | Underline -> "underline"
    | Overline -> "overline"
    | Line_through -> "line-through"
    | No_underline -> "no-underline"
    | Decoration_solid -> "decoration-solid"
    | Decoration_double -> "decoration-double"
    | Decoration_dotted -> "decoration-dotted"
    | Decoration_dashed -> "decoration-dashed"
    | Decoration_wavy -> "decoration-wavy"
    | Decoration_thickness n -> "decoration-" ^ string_of_int n
    | Decoration_from_font -> "decoration-from-font"
    | Decoration_auto -> "decoration-auto"
    | Decoration_bracket_thickness (v, _) -> "decoration-[" ^ v ^ "]"
    | Decoration_bracket_pct (v, _) -> "decoration-[" ^ v ^ "]"
    | Decoration_bracket_length_var v -> "decoration-[length:" ^ v ^ "]"
    | Decoration_bracket_pct_var v -> "decoration-[percentage:" ^ v ^ "]"
    | Tracking_arbitrary (v, _) -> "tracking-[" ^ v ^ "]"
    | Neg_tracking_arbitrary (v, _) -> "-tracking-[" ^ v ^ "]"
    | Tracking_raw (v, _) -> "tracking-[" ^ v ^ "]"
    | Neg_tracking_raw (v, _) -> "-tracking-[" ^ v ^ "]"
    | Tracking_var v -> "tracking-[" ^ v ^ "]"
    | Neg_tracking_var v -> "-tracking-[" ^ v ^ "]"
    | Tracking_tighter -> "tracking-tighter"
    | Tracking_tight -> "tracking-tight"
    | Tracking_normal -> "tracking-normal"
    | Tracking_wide -> "tracking-wide"
    | Tracking_wider -> "tracking-wider"
    | Tracking_widest -> "tracking-widest"
    | Tracking_theme name -> "tracking-" ^ name
    | Uppercase -> "uppercase"
    | Lowercase -> "lowercase"
    | Capitalize -> "capitalize"
    | Normal_case -> "normal-case"
    | Whitespace_normal -> "whitespace-normal"
    | Whitespace_nowrap -> "whitespace-nowrap"
    | Whitespace_pre -> "whitespace-pre"
    | Whitespace_pre_line -> "whitespace-pre-line"
    | Whitespace_pre_wrap -> "whitespace-pre-wrap"
    | Whitespace_break_spaces -> "whitespace-break-spaces"
    | Align_arbitrary_var s -> "align-[" ^ s ^ "]"
    | Align_raw (s, _) -> "align-[" ^ s ^ "]"
    | Align_baseline -> "align-baseline"
    | Align_top -> "align-top"
    | Align_middle -> "align-middle"
    | Align_bottom -> "align-bottom"
    | Align_text_top -> "align-text-top"
    | Align_text_bottom -> "align-text-bottom"
    | Align_sub -> "align-sub"
    | Align_super -> "align-super"
    | List_none -> "list-none"
    | List_disc -> "list-disc"
    | List_decimal -> "list-decimal"
    | List_bracket_var s -> "list-[" ^ s ^ "]"
    | List_bracket (raw, _) -> "list-[" ^ raw ^ "]"
    | List_inside -> "list-inside"
    | List_outside -> "list-outside"
    | List_image_none -> "list-image-none"
    | List_image_bracket_var s ->
        if Parse.is_bare_var s then "list-image-" ^ s
        else "list-image-[" ^ s ^ "]"
    | List_image_bracket (raw, _) | List_image_raw (raw, _) ->
        "list-image-[" ^ raw ^ "]"
    | List_image_url url -> "list-image-" ^ url
    | Underline_offset_auto -> "underline-offset-auto"
    | Underline_offset_0 -> "underline-offset-0"
    | Underline_offset_1 -> "underline-offset-1"
    | Underline_offset_2 -> "underline-offset-2"
    | Underline_offset_4 -> "underline-offset-4"
    | Underline_offset_8 -> "underline-offset-8"
    | Underline_offset_px px ->
        let s = string_of_float px in
        let s =
          if String.ends_with ~suffix:"." s then
            String.sub s 0 (String.length s - 1)
          else s
        in
        "underline-offset-" ^ s
    | Underline_offset_arbitrary (raw, _) | Underline_offset_raw (raw, _) ->
        "underline-offset-[" ^ raw ^ "]"
    | Underline_offset_var v -> "underline-offset-[" ^ v ^ "]"
    | Underline_offset_neg_px px ->
        let s = string_of_float px in
        let s =
          if String.ends_with ~suffix:"." s then
            String.sub s 0 (String.length s - 1)
          else s
        in
        "-underline-offset-" ^ s
    | Underline_offset_neg_arbitrary (raw, _) ->
        "-underline-offset-[" ^ raw ^ "]"
    | Underline_offset_neg_var v -> "-underline-offset-[" ^ v ^ "]"
    | Antialiased -> "antialiased"
    | Subpixel_antialiased -> "subpixel-antialiased"
    | Text_ellipsis -> "text-ellipsis"
    | Overflow_ellipsis -> "overflow-ellipsis"
    | Text_clip -> "text-clip"
    | Truncate -> "truncate"
    | Text_wrap -> "text-wrap"
    | Text_nowrap -> "text-nowrap"
    | Text_balance -> "text-balance"
    | Text_pretty -> "text-pretty"
    | Break_normal -> "break-normal"
    | Break_words -> "break-words"
    | Break_all -> "break-all"
    | Break_keep -> "break-keep"
    | Overflow_wrap_normal -> "overflow-wrap-normal"
    | Overflow_wrap_anywhere -> "overflow-wrap-anywhere"
    | Overflow_wrap_break_word -> "overflow-wrap-break-word"
    | Hyphens_none -> "hyphens-none"
    | Hyphens_manual -> "hyphens-manual"
    | Hyphens_auto -> "hyphens-auto"
    | Font_stretch_ultra_condensed -> "font-stretch-ultra-condensed"
    | Font_stretch_extra_condensed -> "font-stretch-extra-condensed"
    | Font_stretch_condensed -> "font-stretch-condensed"
    | Font_stretch_semi_condensed -> "font-stretch-semi-condensed"
    | Font_stretch_normal -> "font-stretch-normal"
    | Font_stretch_semi_expanded -> "font-stretch-semi-expanded"
    | Font_stretch_expanded -> "font-stretch-expanded"
    | Font_stretch_extra_expanded -> "font-stretch-extra-expanded"
    | Font_stretch_ultra_expanded -> "font-stretch-ultra-expanded"
    | Font_stretch_percent n -> "font-stretch-" ^ string_of_int n ^ "%"
    | Normal_nums -> "normal-nums"
    | Ordinal -> "ordinal"
    | Slashed_zero -> "slashed-zero"
    | Lining_nums -> "lining-nums"
    | Oldstyle_nums -> "oldstyle-nums"
    | Proportional_nums -> "proportional-nums"
    | Tabular_nums -> "tabular-nums"
    | Diagonal_fractions -> "diagonal-fractions"
    | Stacked_fractions -> "stacked-fractions"
    | Indent n -> "indent-" ^ Spacing.pp_spacing_suffix (`Rem (n *. 0.25))
    | Indent_neg n -> "-indent-" ^ Spacing.pp_spacing_suffix (`Rem (n *. 0.25))
    | Indent_px -> "indent-px"
    | Indent_neg_px -> "-indent-px"
    | Indent_arbitrary s -> "indent-[" ^ s ^ "]"
    | Indent_raw (s, _) -> "indent-[" ^ s ^ "]"
    | Indent_neg_arbitrary s -> "-indent-[" ^ s ^ "]"
    | Line_clamp n -> "line-clamp-" ^ string_of_int n
    | Line_clamp_arbitrary n -> "line-clamp-[" ^ string_of_int n ^ "]"
    | Line_clamp_raw (spelling, _) -> "line-clamp-[" ^ spelling ^ "]"
    | Line_clamp_none -> "line-clamp-none"
    | Content_none -> "content-none"
    | Content s -> "content-[\"" ^ s ^ "\"]"
    | Content_squote s -> "content-['" ^ s ^ "']"
    | Content_raw (s, _) -> "content-[" ^ s ^ "]"
    | Content_named s -> "content-" ^ s

  (** {1 Ordering Support} *)

  let suborder = function
    (* Text decoration lines - suborder >= 8000 (alphabetical) *)
    | Line_through -> 8400
    | No_underline -> 8401
    | Overline -> 8402
    | Underline -> 8403
    (* The decoration properties follow the line, in Tailwind's own order:
       colour, then style, then thickness. All the colour variants share a
       suborder so the tie is broken alphabetically by class name. *)
    | Decoration_bracket_color _ -> 8410
    | Decoration_bracket_invalid_color _ -> 8410
    | Decoration_bracket_color_opacity _ -> 8410
    | Decoration_bracket_color_var _ -> 8411
    | Decoration_bracket_color_var_opacity _ -> 8411
    | Decoration_bracket_var _ -> 8412
    | Decoration_bracket_var_opacity _ -> 8412
    | Decoration_color _ -> 8413
    | Decoration_color_opacity _ -> 8413
    | Decoration_transparent -> 8413
    | Decoration_current -> 8413
    | Decoration_current_opacity _ -> 8413
    | Decoration_inherit -> 8413
    (* Decoration styles - same suborder, sorted by class name *)
    | Decoration_solid -> 8420
    | Decoration_double -> 8420
    | Decoration_dotted -> 8420
    | Decoration_dashed -> 8420
    | Decoration_wavy -> 8420
    (* Decoration thickness - numeric values by n, then brackets, then
       auto/from-font. The numeric band is open-ended (the class carries any
       integer), so the brackets sit far enough above it that no thickness a
       stylesheet would ask for reaches them, and below the underline offset. *)
    | Decoration_thickness n -> 8430 + n
    | Decoration_bracket_thickness _ -> 30000
    | Decoration_bracket_pct _ -> 30000
    | Decoration_bracket_length_var _ -> 30000
    | Decoration_bracket_pct_var _ -> 30000
    | Decoration_auto -> 30010
    | Decoration_from_font -> 30010
    (* Tracking — negative first, then arbitrary, then named *)
    | Neg_tracking_arbitrary _ -> 8245
    | Neg_tracking_raw _ -> 8245
    | Neg_tracking_var _ -> 8250
    | Tracking_arbitrary _ -> 8255
    | Tracking_raw _ -> 8255
    | Tracking_var _ -> 8260
    (* Alphabetical by class name: normal, tight, tighter, wide, wider,
       widest *)
    | Tracking_normal -> 8300
    | Tracking_tight -> 8301
    | Tracking_tighter -> 8302
    | Tracking_wide -> 8303
    | Tracking_wider -> 8304
    | Tracking_widest -> 8305
    | Tracking_theme _ -> 8306
    (* Text transform - alphabetical order *)
    | Capitalize -> 8360
    | Lowercase -> 8361
    | Normal_case -> 8362
    | Uppercase -> 8363
    (* Whitespace - alphabetical order *)
    | Whitespace_break_spaces -> 8330
    | Whitespace_normal -> 8331
    | Whitespace_nowrap -> 8332
    | Whitespace_pre -> 8333
    | Whitespace_pre_line -> 8334
    | Whitespace_pre_wrap -> 8335
    (* Vertical align (priority 24) - between text-align (~1005) and font-family
       (~1501) in the early handler's suborder space. Alphabetical. *)
    | Align_arbitrary_var _ | Align_raw _ -> 1200
    | Align_baseline -> 1200
    | Align_bottom -> 1201
    | Align_middle -> 1202
    | Align_sub -> 1203
    | Align_super -> 1204
    | Align_text_bottom -> 1205
    | Align_text_top -> 1206
    | Align_top -> 1207
    (* List utilities (priority 11) - after resize (~1M), before appearance
       (~3M). Position, type, and image form separate property bands; candidate
       names settle ties inside each band. *)
    | List_inside | List_outside -> 2_000_000
    | List_bracket_var _ | List_bracket _ | List_decimal | List_disc | List_none
      ->
        2_010_000
    | List_image_bracket_var _ | List_image_bracket _ | List_image_raw _
    | List_image_none | List_image_url _ ->
        2_020_000
    (* Underline offset — negatives first, then positives, then auto *)
    | Underline_offset_neg_px px -> 50000 + int_of_float px
    | Underline_offset_neg_arbitrary _ -> 59989
    | Underline_offset_neg_var _ -> 59990
    | Underline_offset_0 -> 60000
    | Underline_offset_1 -> 60001
    | Underline_offset_2 -> 60002
    | Underline_offset_4 -> 60003
    | Underline_offset_8 -> 60004
    | Underline_offset_px px -> 60005 + int_of_float px
    | Underline_offset_arbitrary _ | Underline_offset_raw _ -> 69989
    | Underline_offset_var _ -> 69990
    | Underline_offset_auto -> 69999
    (* Both simple smoothing rules lead the placeholder pseudo-element band.
       Keep them in their own slot: variant rules use their selector to settle
       ties, while base rules retain handler order. *)
    | Antialiased -> 79999
    | Subpixel_antialiased -> 79999
    (* The legacy overflow-ellipsis alias leads the canonical text-overflow
       candidates. Truncate sorts before overflow (priority 18) via a suborder
       above alignment/gap's range. Tailwind ranks [text-overflow] at 293,
       between the word-wrapping families (291/292) below and hyphens (294)
       above - it already lands there without adjustment. *)
    | Overflow_ellipsis -> 8319
    | Text_clip -> 8320
    | Text_ellipsis -> 8321
    | Truncate -> 9_000_000 (* priority 17, after alignment/gap (max ~2100) *)
    (* Text wrap - Tailwind ranks [text-wrap] at 290, right after letter-spacing
       (289, Tracking's suborder tops out at 8306) and before overflow-wrap
       (291, Break_normal below). Every variant here writes only [text-wrap], so
       they tie and the class name breaks it alphabetically: balance, nowrap,
       pretty, wrap.

       Not [Property_order.last 290]: that mechanism suits a family whose
       neighbours already use it (backgrounds, masks), where every rank in the
       shared priority is on the same 1000-wide scale. Here Tracking,
       Text_clip/Ellipsis and Whitespace stay on this file's local bare-integer
       scale (82xx-83xx) and are not being touched by this fix, so a suborder on
       the real-rank scale (290 * 1000 and up) would sort after all of them
       regardless of true rank. Every family below that needs to interleave with
       an unconverted neighbour stays on this same local scale for the same
       reason. *)
    | Text_balance -> 8307
    | Text_nowrap -> 8307
    | Text_pretty -> 8307
    | Text_wrap -> 8307
    (* Break utilities - Tailwind ranks [overflow-wrap] at 291 and [word-break]
       at 292. [break-normal] writes both, so it leads the 291-292 stretch;
       lib/overflow_wrap.ml's wrap-anywhere/wrap-break-word/ wrap-normal write
       only [overflow-wrap], same as [break-words], so all four tie with it at
       8309 - a bare constant matching this file's local scale (see the
       [Text_wrap] comment above). [break-all] and [break-keep] write only
       [word-break] and tie at 8310. *)
    | Break_normal -> 8308
    | Break_words -> 8309
    | Break_all -> 8310
    | Break_keep -> 8310
    (* Overflow wrap - not real Tailwind utility names ([overflow-wrap-normal]
       etc. rather than [wrap-normal]); real tailwindcss generates nothing for
       them, so there is no oracle order to match. Left where they were. *)
    | Overflow_wrap_normal -> 9300
    | Overflow_wrap_anywhere -> 9301
    | Overflow_wrap_break_word -> 9302
    (* Hyphens - Tailwind ranks [hyphens] at 294, between text-overflow (293,
       Text_ellipsis above) and white-space (295, Whitespace_* below); the
       [-webkit-hyphens] declaration every variant also writes carries no rank
       of its own, so all three tie on [hyphens] alone and the class name breaks
       it alphabetically: auto, manual, none. *)
    | Hyphens_auto -> 8325
    | Hyphens_manual -> 8325
    | Hyphens_none -> 8325
    (* Font stretch shares one property slot after font style. *)
    | Font_stretch_percent _ | Font_stretch_condensed | Font_stretch_expanded
    | Font_stretch_extra_condensed | Font_stretch_extra_expanded
    | Font_stretch_normal | Font_stretch_semi_condensed
    | Font_stretch_semi_expanded | Font_stretch_ultra_condensed
    | Font_stretch_ultra_expanded ->
        8382
    (* Numeric variants - alphabetical order with normal-nums last *)
    | Diagonal_fractions -> 8390
    | Lining_nums -> 8391
    | Oldstyle_nums -> 8392
    | Ordinal -> 8393
    | Proportional_nums -> 8394
    | Slashed_zero -> 8395
    | Stacked_fractions -> 8396
    | Tabular_nums -> 8397
    | Normal_nums -> 8398
    (* Indent - Tailwind ranks [text-indent] at 282, between [text-align] (281)
       and [vertical-align] (283), so the family sits at priority 24 with both:
       [Text_align] holds 1001-1006 in the early handler, [Align_*] below holds
       1200-1207, and one slot in between puts every spelling of [indent-*]
       where Tailwind puts it. Bare rather than [Property_order.slot 282], for
       the same reason as [Text_wrap] above: the neighbours it sorts against are
       on this file's local scale.

       The whole family shares that slot and [Sort.natural_compare] on the class
       name separates it, which is the order Tailwind emits: negatives lead on
       the [-], digit runs compare numerically so [indent-8] precedes
       [indent-10], and the bracket and [px] spellings follow the bare steps.
       One slot also keeps an [indent-<n>] of any size inside it, where an
       offset derived from [n] would eventually reach [Align_*]. *)
    | Indent _ | Indent_neg _ | Indent_px | Indent_neg_px | Indent_arbitrary _
    | Indent_raw _ | Indent_neg_arbitrary _ ->
        1100
    | Line_clamp n -> 10000 + n
    | Line_clamp_arbitrary _ | Line_clamp_raw _ -> 11000
    | Line_clamp_none -> 12000
    (* Content *)
    | Content_none -> 13000
    | Content _ -> 10001
    | Content_squote _ -> 10001
    | Content_raw _ -> 10001
    | Content_named _ -> 10002

  (* Shared utility implementations *)
  let underline = style [ text_decoration_line Underline ]
  let overline = style [ text_decoration_line Overline ]
  let line_through = style [ text_decoration_line Line_through ]
  let no_underline = style [ text_decoration_line None ]
  let decoration_solid = style [ text_decoration_style Solid ]
  let decoration_double = style [ text_decoration_style Double ]
  let decoration_dotted = style [ text_decoration_style Dotted ]
  let decoration_dashed = style [ text_decoration_style Dashed ]
  let decoration_wavy = style [ text_decoration_style Wavy ]

  let decoration_thickness n =
    style [ text_decoration_thickness (Px (float_of_int n)) ]

  let decoration_from_font = style [ text_decoration_thickness From_font ]
  let decoration_auto = style [ text_decoration_thickness Auto ]

  let decoration_bracket_thickness (len : Css.length) =
    style [ text_decoration_thickness len ]

  let decoration_bracket_pct (len : Css.length) =
    style [ text_decoration_thickness len ]

  let decoration_bracket_length_var v =
    let bare_name = Parse.extract_var_name v in
    let var_ref : Css.length Css.var = Var.bracket bare_name in
    style [ text_decoration_thickness (Var var_ref) ]

  let decoration_bracket_pct_var = decoration_bracket_length_var

  let decoration_color ?theme ?(shade = 500) (color : Color.color) =
    if Color.is_custom_color color then
      let css_color = Color.to_css color shade in
      style
        [
          webkit_text_decoration_color css_color;
          text_decoration_color css_color;
        ]
    else
      let color_var =
        Color.property_color_var ?theme ~property_prefix:"text-decoration-color"
          color shade
      in
      let color_value =
        Color.property_color_value ?theme
          ~property_prefix:"text-decoration-color" color shade
      in
      let decls, color = Color.bound ?theme color_var color_value in
      style
        (decls
        @ [ webkit_text_decoration_color color; text_decoration_color color ])

  let decoration_color_with_opacity ?theme (color : Color.color) shade opacity =
    let percent = Color.opacity_to_percent opacity in
    let scheme = Scheme.or_default theme in
    let color_name = Color.scheme_color_name color shade in
    match Color.opacity_keyword color with
    | Some keyword ->
        let color = Color.apply_alpha opacity keyword in
        style
          [ webkit_text_decoration_color color; text_decoration_color color ]
    | None -> (
        match Scheme.hex_color scheme color_name with
        | Some hex_value ->
            (* Scheme has hex: fallback is hex+alpha, @supports has color-mix
               with webkit *)
            let hex_with_alpha = Color.hex_with_alpha hex_value percent in
            let fallback_decl =
              text_decoration_color (Css.hex hex_with_alpha)
            in
            let decls, color =
              Color.bound ?theme
                (Color.color_var color shade)
                (Css.hex hex_value)
            in
            let oklab_color = Color.mix_alpha opacity color in
            let webkit_decl = webkit_text_decoration_color oklab_color in
            let oklab_decl = text_decoration_color oklab_color in
            let supports_block =
              Color.color_mix_supports [ webkit_decl; oklab_decl ]
            in
            style ~rules:(Some [ supports_block ]) (decls @ [ fallback_decl ])
        | None ->
            (* No scheme hex: use property-scoped variable *)
            let color_var =
              Color.property_color_var ?theme
                ~property_prefix:"text-decoration-color" color shade
            in
            let color_value =
              Color.property_color_value ?theme
                ~property_prefix:"text-decoration-color" color shade
            in
            (* A project token may be any CSS colour, not necessarily a palette
               colour that can be converted to a compile-time hex fallback, so
               the modifier's alpha rides on an sRGB mix of the value itself. An
               alpha read from a var has no percentage to mix in, leaving the
               colour at full opacity. *)
            let fallback_color =
              if Color.opacity_var_bare_of opacity <> None then color_value
              else Color.opacity_fallback ~percent color shade color_value
            in
            let fallback_decl = text_decoration_color fallback_color in
            let decls, color = Color.bound ?theme color_var color_value in
            let oklab_color = Color.mix_alpha opacity color in
            let webkit_decl = webkit_text_decoration_color oklab_color in
            let oklab_decl = text_decoration_color oklab_color in
            let supports_block =
              Color.color_mix_supports [ webkit_decl; oklab_decl ]
            in
            style ~rules:(Some [ supports_block ]) (decls @ [ fallback_decl ]))

  let decoration_transparent = style [ text_decoration_color (Css.hex "#0000") ]
  let decoration_current = style [ text_decoration_color Current ]

  let decoration_inherit =
    style
      [ webkit_text_decoration_color Inherit; text_decoration_color Inherit ]

  let decoration_current_with_opacity opacity =
    let fallback_decl = text_decoration_color Current in
    let oklab_color = Color.mix_alpha opacity Css.Current in
    let webkit_decl = webkit_text_decoration_color oklab_color in
    let oklab_decl = text_decoration_color oklab_color in
    let supports_block = Color.color_mix_supports [ webkit_decl; oklab_decl ] in
    style ~rules:(Some [ supports_block ]) [ fallback_decl ]

  let decoration_bracket_color_style ~theme c =
    let enhanced = Color.resolve_bracket_css_color c in
    match Color.pre_color_mix_fallback theme enhanced with
    | None -> style ~merge_key:"decoration-" [ text_decoration_color enhanced ]
    | Some fallback ->
        let supports_block =
          Color.color_mix_supports
            [
              webkit_text_decoration_color enhanced;
              text_decoration_color enhanced;
            ]
        in
        style ~merge_key:"decoration-" ~rules:(Some [ supports_block ])
          [ text_decoration_color fallback ]

  (* An opacity modifier applies to the colour the bracket was parsed into. The
     modifier read the bracket text back as a hex and answered black whenever
     that failed, and it wrote the mix in place of the colour for every spelling
     that is not a [#] hex, where Tailwind folds the alpha in. *)
  let decoration_bracket_color_with_opacity ~theme c opacity =
    match Color.bracket_color_opacity ~theme c opacity with
    | Color.Folded value ->
        style ~merge_key:"decoration-" [ text_decoration_color value ]
    | Color.Guarded { fallback; mixed } ->
        let supports_block =
          Color.color_mix_supports
            [ webkit_text_decoration_color mixed; text_decoration_color mixed ]
        in
        style ~merge_key:"decoration-" ~rules:(Some [ supports_block ])
          [ text_decoration_color fallback ]

  let decoration_bracket_var_style v =
    let bare_name = Parse.extract_var_name v in
    let var_color : Css.color = Css.Var (Var.bracket bare_name) in
    style ~merge_key:"decoration-"
      [
        webkit_text_decoration_color var_color; text_decoration_color var_color;
      ]

  let decoration_bracket_var_with_opacity v opacity =
    let bare_name = Parse.extract_var_name v in
    let var_color : Css.color = Css.Var (Var.bracket bare_name) in
    let fallback_webkit = webkit_text_decoration_color var_color in
    let fallback_decl = text_decoration_color var_color in
    let oklab_color = Color.mix_alpha opacity var_color in
    let webkit_decl = webkit_text_decoration_color oklab_color in
    let oklab_decl = text_decoration_color oklab_color in
    let supports_block = Color.color_mix_supports [ webkit_decl; oklab_decl ] in
    style ~merge_key:"decoration-" ~rules:(Some [ supports_block ])
      [ fallback_webkit; fallback_decl ]

  let whitespace_normal = style [ white_space Normal ]
  let whitespace_nowrap = style [ white_space Nowrap ]
  let whitespace_pre = style [ white_space Pre ]
  let whitespace_pre_line = style [ white_space Pre_line ]
  let whitespace_pre_wrap = style [ white_space Pre_wrap ]
  let whitespace_break_spaces = style [ white_space Break_spaces ]

  let tracking_tighter =
    let theme_decl, theme_ref = Var.binding tracking_tighter_var (Em (-0.05)) in
    let channel_decl = Var.set tracking_var (Css.Var theme_ref) in
    let property_rules =
      Var.property_rule tracking_var |> Option.to_list |> Css.concat
    in
    style ~property_rules
      [ theme_decl; channel_decl; letter_spacing (Css.Var theme_ref) ]

  let tracking_tight =
    (* Theme var: --tracking-tight: -0.025em *)
    let theme_decl, theme_ref = Var.binding tracking_tight_var (Em (-0.025)) in
    (* Channel var: --tw-tracking: var(--tracking-tight) *)
    let channel_decl = Var.set tracking_var (Css.Var theme_ref) in
    (* Property: letter-spacing: var(--tracking-tight) *)
    let property_rules =
      Var.property_rule tracking_var |> Option.to_list |> Css.concat
    in
    style ~property_rules
      [ theme_decl; channel_decl; letter_spacing (Css.Var theme_ref) ]

  let tracking_normal () =
    (* Tailwind's default theme defines --tracking-normal as 0em, not a unitless
       0, so keep the explicit em unit. *)
    let theme_decl, theme_ref = Var.binding tracking_normal_var (Em 0.0) in
    let channel_decl = Var.set tracking_var (Css.Var theme_ref) in
    let property_rules =
      Var.property_rule tracking_var |> Option.to_list |> Css.concat
    in
    style ~property_rules
      [ theme_decl; channel_decl; letter_spacing (Css.Var theme_ref) ]

  let tracking_wide =
    let theme_decl, theme_ref = Var.binding tracking_wide_var (Em 0.025) in
    let channel_decl = Var.set tracking_var (Css.Var theme_ref) in
    let property_rules =
      Var.property_rule tracking_var |> Option.to_list |> Css.concat
    in
    style ~property_rules
      [ theme_decl; channel_decl; letter_spacing (Css.Var theme_ref) ]

  let tracking_theme theme name =
    let token = "tracking-" ^ name in
    match Scheme.theme_value (Some theme) token with
    | Stdlib.Option.None -> style []
    | Stdlib.Option.Some raw -> (
        match Css.parse_length raw with
        | Stdlib.Option.None -> style []
        | Stdlib.Option.Some len ->
            let theme_decl, theme_ref =
              Var.binding (tracking_named_var name) len
            in
            let channel_decl = Var.set tracking_var (Css.Var theme_ref) in
            let property_rules =
              Var.property_rule tracking_var |> Option.to_list |> Css.concat
            in
            style ~property_rules
              [ theme_decl; channel_decl; letter_spacing (Css.Var theme_ref) ])

  let tracking_wider =
    let theme_decl, theme_ref = Var.binding tracking_wider_var (Em 0.05) in
    let channel_decl = Var.set tracking_var (Css.Var theme_ref) in
    let property_rules =
      Var.property_rule tracking_var |> Option.to_list |> Css.concat
    in
    style ~property_rules
      [ theme_decl; channel_decl; letter_spacing (Css.Var theme_ref) ]

  let tracking_widest =
    let theme_decl, theme_ref = Var.binding tracking_widest_var (Em 0.1) in
    let channel_decl = Var.set tracking_var (Css.Var theme_ref) in
    let property_rules =
      Var.property_rule tracking_var |> Option.to_list |> Css.concat
    in
    style ~property_rules
      [ theme_decl; channel_decl; letter_spacing (Css.Var theme_ref) ]

  let uppercase = style [ text_transform (Case Uppercase) ]
  let lowercase = style [ text_transform (Case Lowercase) ]
  let capitalize = style [ text_transform (Case Capitalize) ]
  let normal_case = style [ text_transform None ]

  let tracking_arbitrary len =
    let channel_decl = Var.set tracking_var len in
    let property_rules =
      Var.property_rule tracking_var |> Option.to_list |> Css.concat
    in
    style ~property_rules [ channel_decl; letter_spacing len ]

  let tracking_raw ?(negative = false) value =
    let value = if negative then "calc(" ^ value ^ " * -1)" else value in
    let channel_decl =
      Css.custom_property ~layer:"utilities" (Var.css_name tracking_var) value
    in
    let property_rules =
      Var.property_rule tracking_var |> Option.to_list |> Css.concat
    in
    let declarations =
      channel_decl
      :: Option.to_list (Parse.opaque_declaration "letter-spacing" value)
    in
    style ~metadata:[ Var.metadata tracking_var ] ~property_rules declarations

  let negate_length : Css.length -> Css.length = function
    | Px n -> Px (-.n)
    | Em n -> Em (-.n)
    | Rem n -> Rem (-.n)
    | Cm n -> Cm (-.n)
    | Mm n -> Mm (-.n)
    | Pt n -> Pt (-.n)
    | Pc n -> Pc (-.n)
    | Ex n -> Ex (-.n)
    | Ch n -> Ch (-.n)
    | Lh n -> Lh (-.n)
    | Vw n -> Vw (-.n)
    | Vh n -> Vh (-.n)
    | Pct n -> Pct (-.n)
    | len -> Calc (Calc.mul (Calc.length len) (Calc.float (-1.)))

  let neg_tracking_arbitrary len =
    (* --tw-tracking uses calc() form, letter-spacing uses direct negation *)
    let calc_neg : Css.length =
      Calc (Calc.mul (Calc.length len) (Calc.float (-1.)))
    in
    let direct_neg = negate_length len in
    let channel_decl = Var.set tracking_var calc_neg in
    let property_rules =
      Var.property_rule tracking_var |> Option.to_list |> Css.concat
    in
    style ~property_rules [ channel_decl; letter_spacing direct_neg ]

  let underline_offset_auto ?theme () =
    match Scheme.theme_value theme "text-underline-offset-auto" with
    | Some _ ->
        let decl, ref_ =
          Var.binding underline_offset_auto_var (Auto : Css.length)
        in
        style [ decl; text_underline_offset (Var ref_) ]
    | None -> style [ text_underline_offset (Auto : Css.length) ]

  let underline_offset_0 = style [ text_underline_offset Zero ]
  let underline_offset_1 = style [ text_underline_offset (Px 1.) ]
  let underline_offset_2 = style [ text_underline_offset (Px 2.) ]
  let underline_offset_4 = style [ text_underline_offset (Px 4.) ]
  let underline_offset_8 = style [ text_underline_offset (Px 8.) ]

  let antialiased =
    style
      [ webkit_font_smoothing Antialiased; moz_osx_font_smoothing Grayscale ]

  let subpixel_antialiased =
    style [ webkit_font_smoothing Auto; moz_osx_font_smoothing Auto ]

  let list_image_url url = style [ list_style_image (Image (Url url)) ]

  let text_indent_length length =
    text_indent (Indent { length; hanging = false; each_line = false })

  let indent n =
    let spacing_decl, spacing_ref =
      Var.binding Theme.spacing_var Theme.spacing_base
    in
    let base : Css.length_percentage = Length (Css.Var spacing_ref) in
    let calc : Css.length_percentage Css.calc = Expr (Val base, Mul, Num n) in
    let length : Css.length_percentage = Calc calc in
    style [ spacing_decl; text_indent_length length ]

  let indent_arbitrary s =
    match arbitrary_length_after_hint s with
    | Some len -> style [ text_indent_length (Length len) ]
    | None -> style [ text_indent_length (Length (Px 0.)) ]

  (* A negated arbitrary length is [calc(<value> * -1)], the spelling Tailwind
     writes whatever the unit. [negate_length] still folds the sign for
     [-indent-px], where the CLI writes [-1px] itself. *)
  let indent_neg_arbitrary s =
    match arbitrary_length_after_hint s with
    | Some len ->
        style
          [
            text_indent_length
              (Length (Calc (Calc.mul (Calc.length len) (Calc.float (-1.)))));
          ]
    | None -> style [ text_indent_length (Length (Px 0.)) ]

  let line_clamp n =
    style
      [
        webkit_line_clamp (Lines n);
        webkit_box_orient Vertical;
        display Webkit_box;
        overflow Hidden;
      ]

  let line_clamp_raw value =
    let clamp =
      Option.to_list (Parse.opaque_declaration "-webkit-line-clamp" value)
    in
    style
      (clamp
      @ [ webkit_box_orient Vertical; display Webkit_box; overflow Hidden ])

  (* A project [@theme] token is a namespace key, not a value Tailwind type-
     checks at build time: any override of [--line-clamp-none], decimal or not,
     switches the utility to the variable-driven form. Reading it with
     [int_of_string_opt] let an OCaml-only spelling like [0x3] through by
     accident and rejected a spelling like [banana] that Tailwind still honours,
     so the branch no longer parses the value at all. *)
  let line_clamp_none_style ?theme () =
    match Scheme.theme_value theme "line-clamp-none" with
    | Some value_str ->
        let decl =
          Css.custom_property ~layer:"theme" "--line-clamp-none" value_str
        in
        let ref : Css.webkit_line_clamp Css.var =
          Var.theme_ref "line-clamp-none"
            ~default:(Css.Unset : Css.webkit_line_clamp)
            ~default_css:"unset"
        in
        style
          [
            decl;
            webkit_line_clamp (Var ref);
            webkit_box_orient Vertical;
            display Webkit_box;
            overflow Hidden;
          ]
    | None ->
        style
          [
            webkit_line_clamp Unset;
            webkit_box_orient Horizontal;
            display Block;
            overflow Visible;
          ]

  (* content-none sets --tw-content: none and uses a literal [content: none], so
     it never references var(--tw-content) and Tailwind emits no @property rule
     for it (unlike the value/theme variants below, which do reference the var).
     The set-based auto-collection is suppressed for --tw-content in
     build.ml. *)
  let content_none =
    let content_decl, _content_ref = Var.binding content_var None in
    style [ content_decl; content None ]

  let content s =
    let value = Parse.decode_underscores s in
    let content_decl, content_ref = Var.binding content_var (String value) in
    let property_rules = Var.property_rules content_var in
    style ~property_rules [ content_decl; content (Css.Var content_ref) ]

  (* content-<token> referencing a --content-<token> theme value (of_class only
     builds this when the token exists in the theme). *)
  let content_named ?theme name =
    let var_name = "content-" ^ name in
    let content_decl, content_ref =
      match Scheme.theme_value theme var_name with
      | Some _ ->
          let tv = Var.theme Css.Content var_name ~order:(6, 60) in
          let theme_decl, theme_ref = Var.binding tv (String "") in
          let cd, cr = Var.binding content_var (Var theme_ref) in
          ([ theme_decl; cd ], cr)
      | None ->
          let theme_ref : Css.content Css.var =
            Var.theme_ref var_name
              ~default:(String "" : Css.content)
              ~default_css:"\"\""
          in
          let cd, cr = Var.binding content_var (Var theme_ref) in
          ([ cd ], cr)
    in
    let property_rules = Var.property_rules content_var in
    let c : Css.content = Css.Var content_ref in
    style ~property_rules (content_decl @ [ Css.content c ])

  let content_squote s =
    (* Single-quoted arbitrary content (content-['x']) keeps the single quote.
       [_] is a space and [\_] a literal underscore. *)
    let value = Parse.decode_underscores s in
    let quoted : Css.content =
      Css.Quoted { value; quote = '\''; repr = None }
    in
    let content_decl, content_ref = Var.binding content_var quoted in
    let property_rules = Var.property_rules content_var in
    let c : Css.content = Css.Var content_ref in
    style ~property_rules [ content_decl; Css.content c ]

  (* Unquoted arbitrary content (content-[attr(before)]) binds the parsed value
     verbatim. *)
  let content_raw (value : Css.content) =
    let content_decl, content_ref = Var.binding content_var value in
    let property_rules = Var.property_rules content_var in
    style ~property_rules [ content_decl; Css.content (Css.Var content_ref) ]

  let content_raw_value value =
    let content_decl =
      Css.custom_property ~layer:"utilities" (Var.css_name content_var) value
    in
    let content_ref = Var.reference content_var in
    let property_rules = Var.property_rules content_var in
    style
      ~metadata:[ Var.metadata content_var ]
      ~property_rules
      [ content_decl; Css.content (Css.Var content_ref) ]

  let align_baseline = style [ vertical_align Baseline ]
  let align_top = style [ vertical_align Top ]
  let align_middle = style [ vertical_align Middle ]
  let align_bottom = style [ vertical_align Bottom ]
  let align_text_top = style [ vertical_align Text_top ]
  let align_text_bottom = style [ vertical_align Text_bottom ]
  let align_sub = style [ vertical_align Sub ]
  let align_super = style [ vertical_align Super ]

  let list_none ?theme () =
    let var_name = "list-style-type-none" in
    let ref =
      Var.theme_ref var_name
        ~default:(None : Css.list_style_type)
        ~default_css:"none"
    in
    match Scheme.theme_value theme var_name with
    | Some value ->
        let theme_decl =
          Css.custom_property ~layer:"theme" ("--" ^ var_name) value
        in
        style [ theme_decl; list_style_type (Var ref) ]
    (* Without a theme override Tailwind writes the keyword, not a reference to
       a token nothing declares. *)
    | None -> style [ list_style_type None ]

  let list_bracket_var s =
    let inner = Parse.extract_var_name s in
    let ref : Css.list_style_type Css.var = Var.bracket inner in
    style [ list_style_type (Var ref) ]

  let list_disc = style [ list_style_type Disc ]
  let list_decimal = style [ list_style_type Decimal ]
  let list_inside = style [ list_style_position Inside ]
  let list_outside = style [ list_style_position Outside ]

  let list_image_bracket_var s =
    (* Written either as [[var(--x)]] or as the [(--x)] shorthand. *)
    let inner =
      if Parse.is_bare_var s then
        let bare = Parse.bare_var_inner s in
        Option.value ~default:bare (Parse.bare_name bare)
      else Parse.extract_var_name s
    in
    let ref : Css.list_style_image Css.var = Var.bracket inner in
    style [ list_style_image (Var ref) ]

  let list_image_none ?theme () =
    let var_name = "list-style-image-none" in
    let ref =
      Var.theme_ref var_name
        ~default:(None : Css.list_style_image)
        ~default_css:"none"
    in
    match Scheme.theme_value theme var_name with
    | Some value ->
        let theme_decl =
          Css.custom_property ~layer:"theme" ("--" ^ var_name) value
        in
        style [ theme_decl; list_style_image (Var ref) ]
    | None -> style [ list_style_image None ]

  let text_ellipsis = style [ text_overflow Ellipsis ]
  let text_clip = style [ text_overflow Clip ]

  let truncate =
    style [ text_overflow Ellipsis; white_space Nowrap; overflow Hidden ]

  let text_wrap = style [ Css.text_wrap Wrap ]
  let text_nowrap = style [ Css.text_wrap No_wrap ]
  let text_balance = style [ Css.text_wrap Balance ]
  let text_pretty = style [ Css.text_wrap Pretty ]
  let break_normal = style [ overflow_wrap Normal; word_break Normal ]
  let break_words = style [ overflow_wrap Break_word ]
  let break_all = style [ word_break Break_all ]
  let break_keep = style [ word_break Keep_all ]
  let overflow_wrap_normal = style [ overflow_wrap Normal ]
  let overflow_wrap_anywhere = style [ overflow_wrap Anywhere ]
  let overflow_wrap_break_word = style [ overflow_wrap Break_word ]
  let hyphens_none = style [ webkit_hyphens None; hyphens None ]
  let hyphens_manual = style [ webkit_hyphens Manual; hyphens Manual ]
  let hyphens_auto = style [ webkit_hyphens Auto; hyphens Auto ]
  let font_stretch_ultra_condensed = style [ font_stretch Ultra_condensed ]
  let font_stretch_extra_condensed = style [ font_stretch Extra_condensed ]
  let font_stretch_condensed = style [ font_stretch Condensed ]
  let font_stretch_semi_condensed = style [ font_stretch Semi_condensed ]
  let font_stretch_normal = style [ font_stretch Normal ]
  let font_stretch_semi_expanded = style [ font_stretch Semi_expanded ]
  let font_stretch_expanded = style [ font_stretch Expanded ]
  let font_stretch_extra_expanded = style [ font_stretch Extra_expanded ]
  let font_stretch_ultra_expanded = style [ font_stretch Ultra_expanded ]
  let font_stretch_percent n = style [ font_stretch (Pct (float_of_int n)) ]
  let normal_nums = style [ font_variant_numeric (Tokens [ Normal ]) ]

  let font_variant_numeric_utility var_to_set value =
    (* Use the shared default theme *)
    let theme = default_font_variant_theme in

    (* Override the specific variable being set - with EMPTY fallback to match
       Tailwind *)
    let updated_theme =
      match var_to_set with
      | `Ordinal ->
          let new_decl, new_ref =
            Var.binding ordinal_var ~fallback:Css.Empty value
          in
          { theme with ordinal = (new_decl, new_ref) }
      | `Slashed ->
          let new_decl, new_ref =
            Var.binding slashed_var ~fallback:Css.Empty value
          in
          { theme with slashed = (new_decl, new_ref) }
      | `Figure ->
          let new_decl, new_ref =
            Var.binding figure_var ~fallback:Css.Empty value
          in
          { theme with figure = (new_decl, new_ref) }
      | `Spacing ->
          let new_decl, new_ref =
            Var.binding spacing_var ~fallback:Css.Empty value
          in
          { theme with spacing = (new_decl, new_ref) }
      | `Fraction ->
          let new_decl, new_ref =
            Var.binding fraction_var ~fallback:Css.Empty value
          in
          { theme with fraction = (new_decl, new_ref) }
    in

    (* Extract only the active declaration (the one being set) *)
    let active_decl =
      match var_to_set with
      | `Ordinal -> fst updated_theme.ordinal
      | `Slashed -> fst updated_theme.slashed
      | `Figure -> fst updated_theme.figure
      | `Spacing -> fst updated_theme.spacing
      | `Fraction -> fst updated_theme.fraction
    in

    (* Compose the font-variant-numeric value using all var references *)
    let composed_value =
      Composed
        {
          ordinal = Some (Css.Var (snd updated_theme.ordinal));
          slashed_zero = Some (Css.Var (snd updated_theme.slashed));
          numeric_figure = Some (Css.Var (snd updated_theme.figure));
          numeric_spacing = Some (Css.Var (snd updated_theme.spacing));
          numeric_fraction = Some (Css.Var (snd updated_theme.fraction));
        }
    in

    (* Get property rules for animation support *)
    let property_rules =
      List.filter_map
        (fun x -> x)
        [
          Var.property_rule ordinal_var;
          Var.property_rule slashed_var;
          Var.property_rule figure_var;
          Var.property_rule spacing_var;
          Var.property_rule fraction_var;
        ]
    in

    style
      ~property_rules:(Css.concat property_rules)
      [ active_decl; font_variant_numeric composed_value ]

  let ordinal = font_variant_numeric_utility `Ordinal Ordinal
  let slashed_zero = font_variant_numeric_utility `Slashed Slashed_zero
  let lining_nums = font_variant_numeric_utility `Figure Lining_nums
  let oldstyle_nums = font_variant_numeric_utility `Figure Oldstyle_nums

  let proportional_nums =
    font_variant_numeric_utility `Spacing Proportional_nums

  let tabular_nums = font_variant_numeric_utility `Spacing Tabular_nums

  let diagonal_fractions =
    font_variant_numeric_utility `Fraction Diagonal_fractions

  let stacked_fractions =
    font_variant_numeric_utility `Fraction Stacked_fractions

  let to_style theme =
    let decoration_color ?shade color = decoration_color ~theme ?shade color in
    let decoration_color_with_opacity color shade opacity =
      decoration_color_with_opacity ~theme color shade opacity
    in
    let underline_offset_auto () = underline_offset_auto ~theme () in
    let line_clamp_none_style () = line_clamp_none_style ~theme () in
    let content_named name = content_named ~theme name in
    let list_none () = list_none ~theme () in
    let list_image_none () = list_image_none ~theme () in
    function
    | Decoration_color (color, None) -> decoration_color color
    | Decoration_color (color, Some shade) -> decoration_color ~shade color
    | Decoration_color_opacity (color, shade, opacity) ->
        decoration_color_with_opacity color shade opacity
    | Decoration_transparent -> decoration_transparent
    | Decoration_current -> decoration_current
    | Decoration_current_opacity opacity ->
        decoration_current_with_opacity opacity
    | Decoration_inherit -> decoration_inherit
    | Decoration_bracket_color (_, c) -> decoration_bracket_color_style ~theme c
    | Decoration_bracket_invalid_color _ -> style []
    | Decoration_bracket_color_opacity (_, c, opacity) ->
        decoration_bracket_color_with_opacity ~theme c opacity
    | Decoration_bracket_var v -> decoration_bracket_var_style v
    | Decoration_bracket_var_opacity (v, opacity) ->
        decoration_bracket_var_with_opacity v opacity
    | Decoration_bracket_color_var v -> decoration_bracket_var_style v
    | Decoration_bracket_color_var_opacity (v, opacity) ->
        decoration_bracket_var_with_opacity v opacity
    | Underline -> underline
    | Overline -> overline
    | Line_through -> line_through
    | No_underline -> no_underline
    | Decoration_solid -> decoration_solid
    | Decoration_double -> decoration_double
    | Decoration_dotted -> decoration_dotted
    | Decoration_dashed -> decoration_dashed
    | Decoration_wavy -> decoration_wavy
    | Decoration_thickness n -> decoration_thickness n
    | Decoration_from_font -> decoration_from_font
    | Decoration_auto -> decoration_auto
    | Decoration_bracket_thickness (_, len) -> decoration_bracket_thickness len
    | Decoration_bracket_pct (_, len) -> decoration_bracket_pct len
    | Decoration_bracket_length_var v -> decoration_bracket_length_var v
    | Decoration_bracket_pct_var v -> decoration_bracket_pct_var v
    | Tracking_arbitrary (_, len) -> tracking_arbitrary len
    | Neg_tracking_arbitrary (_, len) -> neg_tracking_arbitrary len
    | Tracking_raw (_, value) -> tracking_raw value
    | Neg_tracking_raw (_, value) -> tracking_raw ~negative:true value
    | Tracking_var v ->
        let bare_name = Parse.extract_var_name v in
        let var_ref : Css.length Css.var = Var.bracket bare_name in
        let channel_decl = Var.set tracking_var (Css.Var var_ref) in
        let property_rules =
          Var.property_rule tracking_var |> Option.to_list |> Css.concat
        in
        style ~property_rules [ channel_decl; letter_spacing (Css.Var var_ref) ]
    | Neg_tracking_var v ->
        let bare_name = Parse.extract_var_name v in
        let neg_len : Css.length =
          Calc (Calc.mul (Calc.var bare_name) (Calc.float (-1.)))
        in
        let channel_decl = Var.set tracking_var neg_len in
        let property_rules =
          Var.property_rule tracking_var |> Option.to_list |> Css.concat
        in
        style ~property_rules [ channel_decl; letter_spacing neg_len ]
    | Tracking_tighter -> tracking_tighter
    | Tracking_tight -> tracking_tight
    | Tracking_normal -> tracking_normal ()
    | Tracking_wide -> tracking_wide
    | Tracking_wider -> tracking_wider
    | Tracking_widest -> tracking_widest
    | Tracking_theme name -> tracking_theme theme name
    | Uppercase -> uppercase
    | Lowercase -> lowercase
    | Capitalize -> capitalize
    | Normal_case -> normal_case
    | Whitespace_normal -> whitespace_normal
    | Whitespace_nowrap -> whitespace_nowrap
    | Whitespace_pre -> whitespace_pre
    | Whitespace_pre_line -> whitespace_pre_line
    | Whitespace_pre_wrap -> whitespace_pre_wrap
    | Whitespace_break_spaces -> whitespace_break_spaces
    | Align_arbitrary_var var_str ->
        let bare_name = Parse.extract_var_name var_str in
        style [ vertical_align (Var (Var.bracket bare_name)) ]
    | Align_raw (_, v) ->
        style (Option.to_list (Parse.opaque_declaration "vertical-align" v))
    | Align_baseline -> align_baseline
    | Align_top -> align_top
    | Align_middle -> align_middle
    | Align_bottom -> align_bottom
    | Align_text_top -> align_text_top
    | Align_text_bottom -> align_text_bottom
    | Align_sub -> align_sub
    | Align_super -> align_super
    | List_none -> list_none ()
    | List_disc -> list_disc
    | List_decimal -> list_decimal
    | List_bracket_var s -> list_bracket_var s
    | List_bracket (_, t) -> style [ list_style_type t ]
    | List_inside -> list_inside
    | List_outside -> list_outside
    | List_image_none -> list_image_none ()
    | List_image_bracket_var s -> list_image_bracket_var s
    | List_image_bracket (_, i) -> style [ list_style_image i ]
    | List_image_raw (_, v) ->
        style (Option.to_list (Parse.opaque_declaration "list-style-image" v))
    | List_image_url url -> list_image_url url
    | Underline_offset_auto -> underline_offset_auto ()
    | Underline_offset_0 -> underline_offset_0
    | Underline_offset_1 -> underline_offset_1
    | Underline_offset_2 -> underline_offset_2
    | Underline_offset_4 -> underline_offset_4
    | Underline_offset_8 -> underline_offset_8
    | Underline_offset_px px -> style [ text_underline_offset (Px px) ]
    | Underline_offset_arbitrary (_, len) -> style [ text_underline_offset len ]
    | Underline_offset_raw (_, v) ->
        style
          (Option.to_list (Parse.opaque_declaration "text-underline-offset" v))
    | Underline_offset_var v ->
        let bare_name = Parse.extract_var_name v in
        let var_ref : Css.length Css.var = Var.bracket bare_name in
        style [ text_underline_offset (Var var_ref) ]
    | Underline_offset_neg_px px ->
        style
          [
            text_underline_offset
              (Calc (Calc.mul (Calc.length (Px px)) (Calc.float (-1.))));
          ]
    | Underline_offset_neg_arbitrary (_, len) ->
        style
          [
            text_underline_offset
              (Calc (Calc.mul (Calc.length len) (Calc.float (-1.))));
          ]
    | Underline_offset_neg_var v ->
        let bare_name = Parse.extract_var_name v in
        style
          [
            text_underline_offset
              (Calc (Calc.mul (Calc.var bare_name) (Calc.float (-1.))));
          ]
    | Antialiased -> antialiased
    | Subpixel_antialiased -> subpixel_antialiased
    | Text_ellipsis -> text_ellipsis
    | Overflow_ellipsis -> text_ellipsis
    | Text_clip -> text_clip
    | Truncate -> truncate
    | Text_wrap -> text_wrap
    | Text_nowrap -> text_nowrap
    | Text_balance -> text_balance
    | Text_pretty -> text_pretty
    | Break_normal -> break_normal
    | Break_words -> break_words
    | Break_all -> break_all
    | Break_keep -> break_keep
    | Overflow_wrap_normal -> overflow_wrap_normal
    | Overflow_wrap_anywhere -> overflow_wrap_anywhere
    | Overflow_wrap_break_word -> overflow_wrap_break_word
    | Hyphens_none -> hyphens_none
    | Hyphens_manual -> hyphens_manual
    | Hyphens_auto -> hyphens_auto
    | Font_stretch_ultra_condensed -> font_stretch_ultra_condensed
    | Font_stretch_extra_condensed -> font_stretch_extra_condensed
    | Font_stretch_condensed -> font_stretch_condensed
    | Font_stretch_semi_condensed -> font_stretch_semi_condensed
    | Font_stretch_normal -> font_stretch_normal
    | Font_stretch_semi_expanded -> font_stretch_semi_expanded
    | Font_stretch_expanded -> font_stretch_expanded
    | Font_stretch_extra_expanded -> font_stretch_extra_expanded
    | Font_stretch_ultra_expanded -> font_stretch_ultra_expanded
    | Font_stretch_percent n -> font_stretch_percent n
    | Normal_nums -> normal_nums
    | Ordinal -> ordinal
    | Slashed_zero -> slashed_zero
    | Lining_nums -> lining_nums
    | Oldstyle_nums -> oldstyle_nums
    | Proportional_nums -> proportional_nums
    | Tabular_nums -> tabular_nums
    | Diagonal_fractions -> diagonal_fractions
    | Stacked_fractions -> stacked_fractions
    | Indent n -> indent n
    | Indent_neg n -> indent (-.n)
    | Indent_px -> style [ text_indent_length (Length (Px 1.)) ]
    | Indent_neg_px -> style [ text_indent_length (Length (Px (-1.))) ]
    | Indent_arbitrary s -> indent_arbitrary s
    | Indent_raw (_, v) ->
        style (Option.to_list (Parse.opaque_declaration "text-indent" v))
    | Indent_neg_arbitrary s -> indent_neg_arbitrary s
    | Line_clamp n -> line_clamp n
    | Line_clamp_arbitrary n -> line_clamp n
    | Line_clamp_raw (_, value) -> line_clamp_raw value
    | Line_clamp_none -> line_clamp_none_style ()
    | Content_none -> content_none
    | Content s -> content s
    | Content_raw (_, `Content c) -> content_raw c
    | Content_raw (_, `Raw value) -> content_raw_value value
    | Content_squote s -> content_squote s
    | Content_named name -> content_named name

  let examples =
    [
      Align_baseline;
      Antialiased;
      Break_normal;
      Capitalize;
      Content_none;
      Decoration_transparent;
      Decoration_solid;
      Decoration_auto;
      Font_stretch_normal;
      Hyphens_none;
      Indent_px;
      Line_clamp_none;
      Line_through;
      List_none;
      List_image_none;
      List_inside;
      Normal_nums;
      Overflow_wrap_normal;
      Text_balance;
      Text_ellipsis;
      Text_nowrap;
      Tracking_normal;
      Underline_offset_auto;
      Whitespace_normal;
    ]
end

module Early_utility = Utility.Make (Typography_early)
module Late_utility = Utility.Make (Typography_late)

(* Public API - using appropriate handler for each utility *)

(* Early typography utilities - priority 22 *)
let utility_early = Early_utility.v
let text_xs = utility_early Typography_early.Text_xs
let text_sm = utility_early Typography_early.Text_sm
let text_base = utility_early Typography_early.Text_base
let text_lg = utility_early Typography_early.Text_lg
let text_xl = utility_early Typography_early.Text_xl
let text_2xl = utility_early Typography_early.Text_2xl
let text_3xl = utility_early Typography_early.Text_3xl
let text_4xl = utility_early Typography_early.Text_4xl
let text_5xl = utility_early Typography_early.Text_5xl
let text_6xl = utility_early Typography_early.Text_6xl
let text_7xl = utility_early Typography_early.Text_7xl
let text_8xl = utility_early Typography_early.Text_8xl
let text_9xl = utility_early Typography_early.Text_9xl
let font_medium = utility_early Typography_early.Font_medium
let font_thin = utility_early Typography_early.Font_thin
let font_extralight = utility_early Typography_early.Font_extralight
let font_light = utility_early Typography_early.Font_light
let font_normal = utility_early Typography_early.Font_normal
let font_semibold = utility_early Typography_early.Font_semibold
let font_bold = utility_early Typography_early.Font_bold
let font_extrabold = utility_early Typography_early.Font_extrabold
let font_black = utility_early Typography_early.Font_black
let font_mono = utility_early (Typography_early.Font_mono false)
let font_sans = utility_early (Typography_early.Font_sans false)
let font_serif = utility_early (Typography_early.Font_serif false)
let italic = utility_early Typography_early.Italic
let not_italic = utility_early Typography_early.Not_italic
let text_left = utility_early Typography_early.Text_left
let text_center = utility_early Typography_early.Text_center
let text_right = utility_early Typography_early.Text_right
let text_justify = utility_early Typography_early.Text_justify
let text_start = utility_early Typography_early.Text_start
let text_end = utility_early Typography_early.Text_end
let leading_none = utility_early Typography_early.Leading_none
let leading_tight = utility_early Typography_early.Leading_tight
let leading_snug = utility_early Typography_early.Leading_snug
let leading_normal = utility_early Typography_early.Leading_normal
let leading_relaxed = utility_early Typography_early.Leading_relaxed
let leading_loose = utility_early Typography_early.Leading_loose
let leading n = utility_early (Typography_early.Leading n)
let leading' n = utility_early (Typography_early.Leading_step n)

(* Late typography utilities - priority 24 *)
let utility_late = Late_utility.v

let decoration_color ?(shade = 500) color =
  Color.check_shade ~utility:"decoration_color" color shade;
  utility_late (Typography_late.Decoration_color (color, Some shade))

let underline = utility_late Typography_late.Underline
let overline = utility_late Typography_late.Overline
let line_through = utility_late Typography_late.Line_through
let no_underline = utility_late Typography_late.No_underline
let decoration_solid = utility_late Typography_late.Decoration_solid
let decoration_double = utility_late Typography_late.Decoration_double
let decoration_dotted = utility_late Typography_late.Decoration_dotted
let decoration_dashed = utility_late Typography_late.Decoration_dashed
let decoration_wavy = utility_late Typography_late.Decoration_wavy

let decoration_thickness n =
  utility_late (Typography_late.Decoration_thickness n)

let decoration_from_font = utility_late Typography_late.Decoration_from_font
let whitespace_normal = utility_late Typography_late.Whitespace_normal
let whitespace_nowrap = utility_late Typography_late.Whitespace_nowrap
let whitespace_pre = utility_late Typography_late.Whitespace_pre
let whitespace_pre_line = utility_late Typography_late.Whitespace_pre_line
let whitespace_pre_wrap = utility_late Typography_late.Whitespace_pre_wrap

let whitespace_break_spaces =
  utility_late Typography_late.Whitespace_break_spaces

let tracking_tighter = utility_late Typography_late.Tracking_tighter
let tracking_tight = utility_late Typography_late.Tracking_tight
let tracking_normal = utility_late Typography_late.Tracking_normal
let tracking_wide = utility_late Typography_late.Tracking_wide
let tracking_wider = utility_late Typography_late.Tracking_wider
let tracking_widest = utility_late Typography_late.Tracking_widest
let uppercase = utility_late Typography_late.Uppercase
let lowercase = utility_late Typography_late.Lowercase
let capitalize = utility_late Typography_late.Capitalize
let normal_case = utility_late Typography_late.Normal_case

let underline_offset n =
  utility_late (Typography_late.Underline_offset_px (float_of_int n))

let underline_offset_auto = utility_late Typography_late.Underline_offset_auto
let underline_offset_0 = utility_late Typography_late.Underline_offset_0
let underline_offset_1 = utility_late Typography_late.Underline_offset_1
let underline_offset_2 = utility_late Typography_late.Underline_offset_2
let underline_offset_4 = utility_late Typography_late.Underline_offset_4
let underline_offset_8 = utility_late Typography_late.Underline_offset_8
let antialiased = utility_late Typography_late.Antialiased
let subpixel_antialiased = utility_late Typography_late.Subpixel_antialiased
let align_baseline = utility_late Typography_late.Align_baseline
let align_top = utility_late Typography_late.Align_top
let align_middle = utility_late Typography_late.Align_middle
let align_bottom = utility_late Typography_late.Align_bottom
let align_text_top = utility_late Typography_late.Align_text_top
let align_text_bottom = utility_late Typography_late.Align_text_bottom
let align_sub = utility_late Typography_late.Align_sub
let align_super = utility_late Typography_late.Align_super
let list_none = utility_late Typography_late.List_none
let list_disc = utility_late Typography_late.List_disc
let list_decimal = utility_late Typography_late.List_decimal
let list_inside = utility_late Typography_late.List_inside
let list_outside = utility_late Typography_late.List_outside
let list_image_none = utility_late Typography_late.List_image_none
let list_image_url url = utility_late (Typography_late.List_image_url url)
let indent' n = utility_late (Typography_late.Indent n)
let indent n = indent' (float_of_int n)
let line_clamp n = utility_late (Typography_late.Line_clamp n)
let content_none = utility_late Typography_late.Content_none
let content s = utility_late (Typography_late.Content s)
let text_ellipsis = utility_late Typography_late.Text_ellipsis
let text_clip = utility_late Typography_late.Text_clip
let text_wrap = utility_late Typography_late.Text_wrap
let text_nowrap = utility_late Typography_late.Text_nowrap
let text_balance = utility_late Typography_late.Text_balance
let text_pretty = utility_late Typography_late.Text_pretty
let break_normal = utility_late Typography_late.Break_normal
let break_words = utility_late Typography_late.Break_words
let break_all = utility_late Typography_late.Break_all
let break_keep = utility_late Typography_late.Break_keep
let overflow_wrap_normal = utility_late Typography_late.Overflow_wrap_normal
let overflow_wrap_anywhere = utility_late Typography_late.Overflow_wrap_anywhere

let overflow_wrap_break_word =
  utility_late Typography_late.Overflow_wrap_break_word

let hyphens_none = utility_late Typography_late.Hyphens_none
let hyphens_manual = utility_late Typography_late.Hyphens_manual
let hyphens_auto = utility_late Typography_late.Hyphens_auto

let font_stretch_ultra_condensed =
  utility_late Typography_late.Font_stretch_ultra_condensed

let font_stretch_extra_condensed =
  utility_late Typography_late.Font_stretch_extra_condensed

let font_stretch_condensed = utility_late Typography_late.Font_stretch_condensed

let font_stretch_semi_condensed =
  utility_late Typography_late.Font_stretch_semi_condensed

let font_stretch_normal = utility_late Typography_late.Font_stretch_normal

let font_stretch_semi_expanded =
  utility_late Typography_late.Font_stretch_semi_expanded

let font_stretch_expanded = utility_late Typography_late.Font_stretch_expanded

let font_stretch_extra_expanded =
  utility_late Typography_late.Font_stretch_extra_expanded

let font_stretch_ultra_expanded =
  utility_late Typography_late.Font_stretch_ultra_expanded

let font_stretch_percent n =
  utility_late (Typography_late.Font_stretch_percent n)

(** {1 Numeric Variants} *)

let normal_nums = utility_late Typography_late.Normal_nums
let ordinal = utility_late Typography_late.Ordinal
let slashed_zero = utility_late Typography_late.Slashed_zero
let lining_nums = utility_late Typography_late.Lining_nums
let oldstyle_nums = utility_late Typography_late.Oldstyle_nums
let proportional_nums = utility_late Typography_late.Proportional_nums
let tabular_nums = utility_late Typography_late.Tabular_nums
let diagonal_fractions = utility_late Typography_late.Diagonal_fractions
let stacked_fractions = utility_late Typography_late.Stacked_fractions