badness-formatter 0.13.0

Deterministic, rule-based formatter for LaTeX and BibTeX
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
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
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
4995
4996
4997
4998
4999
5000
5001
5002
5003
5004
5005
5006
5007
5008
5009
5010
5011
5012
5013
5014
5015
5016
5017
5018
5019
5020
5021
5022
5023
5024
5025
5026
5027
5028
5029
5030
5031
5032
5033
5034
5035
5036
5037
5038
5039
5040
5041
5042
5043
5044
5045
5046
5047
5048
5049
5050
5051
5052
5053
5054
5055
5056
5057
5058
5059
5060
5061
5062
5063
5064
5065
5066
5067
5068
5069
5070
5071
5072
5073
5074
5075
5076
5077
5078
5079
5080
5081
5082
5083
5084
5085
5086
5087
5088
5089
5090
5091
5092
5093
5094
5095
5096
5097
5098
5099
5100
5101
5102
5103
5104
5105
5106
5107
5108
5109
5110
5111
5112
5113
5114
5115
5116
5117
5118
5119
5120
5121
5122
5123
5124
5125
5126
5127
5128
5129
5130
5131
5132
5133
5134
5135
5136
5137
5138
5139
5140
5141
5142
5143
5144
5145
5146
5147
5148
5149
5150
5151
5152
5153
5154
5155
5156
5157
5158
5159
5160
5161
5162
5163
5164
5165
5166
5167
5168
5169
5170
5171
5172
5173
5174
5175
5176
5177
5178
5179
5180
5181
5182
5183
5184
5185
5186
5187
5188
5189
5190
5191
5192
5193
5194
5195
5196
5197
5198
5199
5200
5201
5202
5203
5204
5205
5206
5207
5208
5209
5210
5211
5212
5213
5214
5215
5216
5217
5218
5219
5220
5221
5222
5223
5224
5225
5226
5227
5228
5229
5230
5231
5232
5233
5234
5235
5236
5237
5238
5239
5240
5241
5242
5243
5244
5245
5246
5247
5248
5249
5250
5251
5252
5253
5254
5255
5256
5257
5258
5259
5260
5261
5262
5263
5264
5265
5266
5267
5268
5269
5270
5271
5272
5273
5274
5275
5276
5277
5278
5279
5280
5281
5282
5283
5284
5285
5286
5287
5288
5289
5290
5291
5292
5293
5294
5295
5296
5297
5298
5299
5300
5301
5302
5303
5304
5305
5306
5307
5308
5309
5310
5311
5312
5313
5314
5315
5316
5317
5318
5319
5320
5321
5322
5323
5324
5325
5326
5327
5328
5329
5330
5331
5332
5333
5334
5335
5336
5337
5338
5339
5340
5341
5342
5343
5344
5345
5346
5347
5348
5349
5350
5351
5352
5353
5354
5355
5356
5357
5358
5359
5360
5361
5362
5363
5364
5365
5366
5367
5368
5369
5370
5371
5372
5373
5374
5375
5376
5377
5378
5379
5380
5381
5382
5383
5384
5385
5386
5387
5388
5389
5390
5391
5392
5393
5394
5395
5396
5397
5398
5399
5400
5401
5402
5403
5404
5405
5406
5407
5408
5409
5410
5411
5412
5413
5414
5415
5416
5417
5418
5419
5420
5421
5422
5423
5424
5425
5426
5427
5428
5429
5430
5431
5432
5433
5434
5435
5436
5437
5438
5439
5440
5441
5442
5443
5444
5445
5446
5447
5448
5449
5450
5451
5452
5453
5454
5455
5456
5457
5458
5459
5460
5461
5462
5463
5464
5465
5466
5467
5468
5469
5470
5471
5472
5473
5474
5475
5476
5477
5478
5479
5480
5481
5482
5483
5484
5485
5486
5487
5488
5489
5490
5491
5492
5493
5494
5495
5496
5497
5498
5499
5500
5501
5502
5503
5504
5505
5506
5507
5508
5509
5510
5511
5512
5513
5514
5515
5516
5517
5518
5519
5520
5521
5522
5523
5524
5525
5526
5527
5528
5529
5530
5531
5532
5533
5534
5535
5536
5537
5538
5539
5540
5541
5542
5543
5544
5545
5546
5547
5548
5549
5550
5551
5552
5553
5554
5555
5556
5557
5558
5559
5560
5561
5562
5563
5564
5565
5566
5567
5568
5569
5570
5571
5572
5573
5574
5575
5576
5577
5578
5579
5580
5581
5582
5583
5584
5585
5586
5587
5588
5589
5590
5591
5592
5593
5594
5595
5596
5597
5598
5599
5600
5601
5602
5603
5604
5605
5606
5607
5608
5609
5610
5611
5612
5613
5614
5615
5616
5617
5618
5619
5620
5621
5622
5623
5624
5625
5626
5627
5628
5629
5630
5631
5632
5633
5634
5635
5636
5637
5638
5639
5640
5641
5642
5643
5644
5645
5646
5647
5648
5649
5650
5651
5652
5653
5654
5655
5656
5657
5658
5659
5660
5661
5662
5663
5664
5665
5666
5667
5668
5669
5670
5671
5672
5673
5674
5675
5676
5677
5678
5679
5680
5681
5682
5683
5684
5685
5686
5687
5688
5689
5690
5691
5692
5693
5694
5695
5696
5697
5698
5699
5700
5701
5702
5703
5704
5705
5706
5707
5708
5709
5710
5711
5712
5713
5714
5715
5716
5717
5718
5719
5720
5721
5722
5723
5724
5725
5726
5727
5728
5729
5730
5731
5732
5733
5734
5735
5736
5737
5738
5739
5740
5741
5742
5743
5744
5745
5746
5747
5748
5749
5750
5751
5752
5753
5754
5755
5756
5757
5758
5759
5760
5761
5762
5763
5764
5765
5766
5767
5768
5769
5770
5771
5772
5773
5774
5775
5776
5777
5778
5779
5780
5781
5782
5783
5784
5785
5786
5787
5788
5789
5790
5791
5792
5793
5794
5795
5796
5797
5798
5799
5800
5801
5802
5803
5804
5805
5806
5807
5808
5809
5810
5811
5812
5813
5814
5815
5816
5817
5818
5819
5820
5821
5822
5823
5824
5825
5826
5827
5828
5829
5830
5831
5832
5833
5834
5835
5836
5837
5838
5839
5840
5841
5842
5843
5844
5845
5846
5847
5848
5849
5850
5851
5852
5853
5854
5855
5856
5857
5858
5859
5860
5861
5862
5863
5864
5865
5866
5867
5868
5869
5870
5871
5872
5873
5874
5875
5876
5877
5878
5879
5880
5881
5882
5883
5884
5885
5886
5887
5888
5889
5890
5891
5892
5893
5894
5895
5896
5897
5898
5899
5900
5901
5902
5903
5904
5905
5906
5907
5908
5909
5910
5911
5912
5913
5914
5915
5916
5917
5918
5919
5920
5921
5922
5923
5924
5925
5926
5927
5928
5929
5930
5931
5932
5933
5934
5935
5936
5937
5938
5939
5940
5941
5942
5943
5944
5945
5946
5947
5948
5949
5950
5951
5952
5953
5954
5955
5956
5957
5958
5959
5960
5961
5962
5963
5964
5965
5966
5967
5968
5969
5970
5971
5972
5973
5974
5975
5976
5977
5978
5979
5980
5981
5982
5983
5984
5985
5986
5987
5988
5989
5990
5991
5992
5993
5994
5995
5996
5997
5998
5999
6000
6001
6002
6003
6004
6005
6006
6007
6008
6009
6010
6011
6012
6013
6014
6015
6016
6017
6018
6019
6020
6021
6022
6023
6024
6025
6026
6027
6028
6029
6030
6031
6032
6033
6034
6035
6036
6037
6038
6039
6040
6041
6042
6043
6044
6045
6046
6047
6048
6049
6050
6051
6052
6053
6054
6055
6056
6057
6058
6059
6060
6061
6062
6063
6064
6065
6066
6067
6068
6069
6070
6071
6072
6073
6074
6075
6076
6077
6078
6079
6080
6081
6082
6083
6084
6085
6086
6087
6088
6089
6090
6091
6092
6093
6094
6095
6096
6097
6098
6099
6100
6101
6102
6103
6104
6105
6106
6107
6108
6109
6110
6111
6112
6113
6114
6115
6116
6117
6118
6119
6120
6121
6122
6123
6124
6125
6126
6127
6128
6129
6130
6131
6132
6133
6134
6135
6136
6137
6138
6139
6140
6141
6142
6143
6144
6145
6146
6147
6148
6149
6150
6151
6152
6153
6154
6155
6156
6157
6158
6159
6160
6161
6162
6163
6164
6165
6166
6167
6168
6169
6170
6171
6172
6173
6174
6175
6176
6177
6178
6179
6180
6181
6182
6183
6184
6185
6186
6187
6188
6189
6190
6191
6192
6193
6194
6195
6196
6197
6198
6199
6200
6201
6202
6203
6204
6205
6206
6207
6208
6209
6210
6211
6212
6213
6214
6215
6216
6217
6218
6219
6220
6221
6222
6223
6224
6225
6226
6227
6228
6229
6230
6231
6232
6233
6234
6235
6236
6237
6238
6239
6240
6241
6242
6243
6244
6245
6246
6247
6248
6249
6250
6251
6252
6253
6254
6255
6256
6257
6258
6259
6260
6261
6262
6263
6264
6265
6266
6267
6268
6269
6270
6271
6272
6273
6274
6275
6276
6277
6278
6279
6280
6281
6282
6283
6284
6285
6286
6287
6288
6289
6290
6291
6292
6293
6294
6295
6296
6297
6298
6299
6300
6301
6302
6303
6304
6305
6306
6307
6308
6309
6310
6311
6312
6313
6314
6315
6316
6317
6318
6319
6320
6321
6322
6323
6324
6325
6326
6327
6328
6329
6330
6331
6332
6333
6334
6335
6336
6337
6338
6339
6340
6341
6342
6343
6344
6345
6346
6347
6348
6349
6350
6351
6352
6353
6354
6355
6356
6357
6358
6359
6360
6361
6362
6363
6364
6365
6366
6367
6368
6369
6370
6371
6372
6373
6374
6375
6376
6377
6378
6379
6380
6381
6382
6383
6384
6385
6386
6387
6388
6389
6390
6391
6392
6393
6394
6395
6396
6397
6398
6399
6400
6401
6402
6403
6404
6405
6406
6407
6408
6409
6410
6411
6412
6413
6414
6415
6416
6417
6418
6419
6420
6421
6422
6423
6424
6425
6426
6427
6428
6429
6430
6431
6432
6433
6434
6435
6436
6437
6438
6439
6440
6441
6442
6443
6444
6445
6446
6447
6448
6449
6450
6451
6452
6453
6454
6455
6456
6457
6458
6459
6460
6461
6462
6463
6464
6465
6466
6467
6468
6469
6470
6471
6472
6473
6474
6475
6476
6477
6478
6479
6480
6481
6482
6483
6484
6485
6486
6487
6488
6489
6490
6491
6492
6493
6494
6495
6496
6497
6498
6499
6500
6501
6502
6503
6504
6505
6506
6507
6508
6509
6510
6511
6512
6513
6514
6515
6516
6517
6518
6519
6520
6521
6522
6523
6524
6525
6526
6527
6528
6529
6530
6531
6532
6533
6534
6535
6536
6537
6538
6539
6540
6541
6542
6543
6544
6545
6546
6547
6548
6549
6550
6551
6552
6553
6554
6555
6556
6557
6558
6559
6560
6561
6562
6563
6564
6565
6566
6567
6568
6569
6570
6571
6572
6573
6574
6575
6576
6577
6578
6579
6580
6581
6582
6583
6584
6585
6586
6587
6588
6589
6590
6591
6592
6593
6594
6595
6596
6597
6598
6599
6600
6601
6602
6603
6604
6605
6606
6607
6608
6609
6610
6611
6612
6613
6614
6615
6616
6617
6618
6619
6620
6621
6622
6623
6624
6625
6626
6627
6628
6629
6630
6631
6632
6633
6634
6635
6636
6637
6638
6639
6640
6641
6642
6643
6644
6645
6646
6647
6648
6649
6650
6651
6652
6653
6654
6655
6656
6657
6658
6659
6660
6661
6662
6663
6664
6665
6666
6667
6668
6669
6670
6671
6672
6673
6674
6675
6676
6677
6678
6679
6680
6681
6682
6683
6684
6685
6686
6687
6688
6689
6690
6691
6692
6693
6694
6695
6696
6697
6698
6699
6700
6701
6702
6703
6704
6705
6706
6707
6708
6709
6710
6711
6712
6713
6714
6715
6716
6717
6718
6719
6720
6721
6722
6723
6724
6725
6726
6727
6728
6729
6730
6731
6732
6733
6734
6735
6736
6737
6738
6739
6740
6741
6742
6743
6744
6745
6746
6747
6748
6749
6750
6751
6752
6753
6754
6755
6756
6757
6758
6759
6760
6761
6762
6763
6764
6765
6766
6767
6768
6769
6770
6771
6772
6773
6774
6775
6776
6777
6778
6779
6780
6781
6782
6783
6784
6785
6786
6787
6788
6789
6790
6791
6792
6793
6794
6795
6796
6797
6798
6799
6800
6801
6802
6803
6804
6805
6806
6807
6808
6809
6810
6811
6812
6813
6814
6815
6816
6817
6818
6819
6820
6821
6822
6823
6824
6825
6826
6827
6828
6829
6830
6831
6832
6833
6834
6835
6836
6837
6838
6839
6840
6841
6842
6843
6844
6845
6846
6847
6848
6849
6850
6851
6852
6853
6854
6855
6856
6857
6858
6859
6860
6861
6862
6863
6864
6865
6866
6867
6868
6869
6870
6871
6872
6873
6874
6875
6876
6877
6878
6879
6880
6881
6882
6883
6884
6885
6886
6887
6888
6889
6890
6891
6892
6893
6894
6895
6896
6897
6898
6899
6900
6901
6902
6903
6904
6905
6906
6907
6908
6909
6910
6911
6912
6913
6914
6915
6916
6917
6918
6919
6920
6921
6922
6923
6924
6925
6926
6927
6928
6929
6930
6931
6932
6933
6934
6935
6936
6937
6938
6939
6940
6941
6942
6943
6944
6945
6946
6947
6948
6949
6950
6951
6952
6953
6954
6955
6956
6957
6958
6959
6960
6961
6962
6963
6964
6965
6966
6967
6968
6969
6970
6971
6972
6973
6974
6975
6976
6977
6978
6979
6980
6981
6982
6983
6984
6985
6986
6987
6988
6989
6990
6991
6992
6993
6994
6995
6996
6997
6998
6999
7000
7001
7002
7003
7004
7005
7006
7007
7008
7009
7010
7011
7012
7013
7014
7015
7016
7017
7018
7019
7020
7021
7022
7023
7024
7025
7026
7027
7028
7029
7030
7031
7032
7033
7034
7035
7036
7037
7038
7039
7040
7041
7042
7043
7044
7045
7046
7047
7048
7049
7050
7051
7052
7053
7054
7055
7056
7057
7058
7059
7060
7061
7062
7063
7064
7065
7066
7067
7068
7069
7070
7071
7072
7073
7074
7075
7076
7077
7078
7079
7080
7081
7082
7083
7084
7085
7086
7087
7088
7089
7090
7091
7092
7093
7094
7095
7096
7097
7098
7099
7100
7101
7102
7103
7104
7105
7106
7107
7108
7109
7110
7111
7112
7113
7114
7115
7116
7117
7118
7119
7120
7121
7122
7123
7124
7125
7126
7127
7128
7129
7130
7131
7132
7133
7134
7135
7136
7137
7138
7139
7140
7141
7142
7143
7144
7145
7146
7147
7148
7149
7150
7151
7152
7153
7154
7155
7156
7157
7158
7159
7160
7161
7162
7163
7164
7165
7166
7167
7168
7169
7170
7171
7172
7173
7174
7175
7176
7177
7178
7179
7180
7181
7182
7183
7184
7185
7186
7187
7188
7189
7190
7191
7192
7193
7194
7195
7196
7197
7198
7199
7200
7201
7202
7203
7204
7205
7206
7207
7208
7209
7210
7211
7212
7213
7214
7215
7216
7217
7218
7219
7220
7221
7222
7223
7224
7225
7226
7227
7228
7229
7230
7231
7232
7233
7234
7235
7236
7237
7238
7239
7240
7241
7242
7243
7244
7245
7246
7247
7248
7249
7250
7251
7252
7253
7254
7255
7256
7257
7258
7259
7260
7261
7262
7263
7264
7265
7266
7267
7268
7269
7270
7271
7272
7273
7274
7275
7276
7277
7278
7279
7280
7281
7282
7283
7284
7285
7286
7287
7288
7289
7290
7291
7292
7293
7294
7295
7296
7297
7298
7299
7300
7301
7302
7303
7304
7305
7306
7307
7308
7309
7310
7311
7312
7313
7314
7315
7316
7317
7318
7319
7320
7321
7322
7323
7324
7325
7326
7327
7328
7329
7330
7331
7332
7333
7334
7335
7336
7337
7338
7339
7340
7341
7342
7343
7344
7345
7346
7347
7348
7349
7350
7351
7352
7353
7354
7355
7356
7357
7358
7359
7360
7361
7362
7363
7364
7365
7366
7367
7368
7369
7370
7371
7372
7373
7374
7375
7376
7377
7378
7379
7380
7381
7382
7383
7384
7385
7386
7387
7388
7389
7390
7391
7392
7393
7394
7395
7396
7397
7398
7399
7400
7401
7402
7403
7404
7405
7406
7407
7408
7409
7410
7411
7412
7413
7414
7415
7416
7417
7418
7419
7420
7421
7422
7423
7424
7425
7426
7427
7428
7429
7430
7431
7432
7433
7434
7435
7436
7437
7438
7439
7440
7441
7442
7443
7444
7445
7446
7447
7448
7449
7450
7451
7452
7453
7454
7455
7456
7457
7458
7459
7460
7461
7462
7463
7464
7465
7466
7467
7468
7469
7470
7471
7472
7473
7474
7475
7476
7477
7478
7479
7480
7481
7482
7483
7484
7485
7486
7487
7488
7489
7490
7491
7492
7493
7494
7495
7496
7497
7498
7499
7500
7501
7502
7503
7504
7505
7506
7507
7508
7509
7510
7511
7512
7513
7514
7515
7516
7517
7518
7519
7520
7521
7522
7523
7524
7525
7526
7527
7528
7529
7530
7531
7532
7533
7534
7535
7536
7537
7538
7539
7540
7541
7542
7543
7544
7545
7546
7547
7548
7549
7550
7551
7552
7553
7554
7555
7556
7557
7558
7559
7560
7561
7562
7563
7564
7565
7566
7567
7568
7569
7570
7571
7572
7573
7574
7575
7576
7577
7578
7579
7580
7581
7582
7583
7584
7585
7586
7587
7588
7589
7590
7591
7592
7593
7594
7595
7596
7597
7598
7599
7600
7601
7602
7603
7604
7605
7606
7607
7608
7609
7610
7611
7612
7613
7614
7615
7616
7617
7618
7619
7620
7621
7622
7623
7624
7625
7626
7627
7628
7629
7630
7631
7632
7633
7634
7635
7636
7637
7638
7639
7640
7641
7642
7643
7644
7645
7646
7647
7648
7649
7650
7651
7652
7653
7654
7655
7656
7657
7658
7659
7660
7661
7662
7663
7664
7665
7666
7667
7668
7669
7670
7671
7672
7673
7674
7675
7676
7677
7678
7679
7680
7681
7682
7683
7684
7685
7686
7687
7688
7689
7690
7691
7692
7693
7694
7695
7696
7697
7698
7699
7700
7701
7702
7703
7704
7705
7706
7707
7708
7709
7710
7711
7712
7713
7714
7715
7716
7717
7718
7719
7720
7721
7722
7723
7724
7725
7726
7727
7728
7729
7730
7731
7732
7733
7734
7735
7736
7737
7738
7739
7740
7741
7742
7743
7744
7745
7746
7747
7748
7749
7750
7751
7752
7753
7754
7755
7756
7757
7758
7759
7760
7761
7762
7763
7764
7765
7766
7767
7768
7769
7770
7771
7772
7773
7774
7775
7776
7777
7778
7779
7780
7781
7782
7783
7784
7785
7786
7787
7788
7789
7790
7791
7792
7793
7794
7795
7796
7797
7798
7799
7800
7801
7802
7803
7804
7805
7806
7807
7808
7809
7810
7811
7812
7813
7814
7815
7816
7817
7818
7819
7820
7821
7822
7823
7824
7825
7826
7827
7828
7829
7830
7831
7832
7833
7834
7835
7836
7837
7838
7839
7840
7841
7842
7843
7844
7845
7846
7847
7848
7849
7850
7851
7852
7853
7854
7855
7856
7857
7858
7859
7860
7861
7862
7863
7864
7865
7866
7867
7868
7869
7870
7871
7872
7873
7874
7875
7876
7877
7878
7879
7880
7881
7882
7883
7884
7885
7886
7887
7888
7889
7890
7891
7892
7893
7894
7895
7896
7897
7898
7899
7900
7901
7902
7903
7904
7905
7906
7907
7908
7909
7910
7911
7912
7913
7914
7915
7916
7917
7918
7919
7920
7921
7922
7923
7924
7925
7926
7927
7928
7929
7930
7931
7932
7933
7934
7935
7936
7937
7938
7939
7940
7941
7942
7943
7944
7945
7946
7947
7948
7949
7950
7951
7952
7953
7954
7955
7956
7957
7958
7959
7960
7961
7962
7963
7964
7965
7966
7967
7968
7969
7970
7971
7972
7973
7974
7975
7976
7977
7978
7979
7980
7981
7982
7983
7984
7985
7986
7987
7988
7989
7990
7991
7992
7993
7994
7995
7996
7997
7998
7999
8000
8001
8002
8003
8004
8005
8006
8007
8008
8009
8010
8011
8012
8013
8014
8015
8016
8017
8018
8019
8020
8021
8022
8023
8024
8025
8026
8027
8028
8029
8030
8031
8032
8033
8034
8035
8036
8037
8038
8039
8040
8041
8042
8043
8044
8045
8046
8047
8048
8049
8050
8051
8052
8053
8054
8055
8056
8057
8058
8059
8060
8061
8062
8063
8064
8065
8066
8067
8068
8069
8070
8071
8072
8073
8074
8075
8076
8077
8078
8079
8080
8081
8082
8083
8084
8085
8086
8087
8088
8089
8090
8091
8092
8093
8094
8095
8096
8097
8098
8099
8100
8101
8102
8103
8104
8105
8106
8107
8108
8109
8110
8111
8112
8113
8114
8115
8116
8117
8118
8119
8120
8121
8122
8123
8124
8125
8126
8127
8128
8129
8130
8131
8132
8133
8134
8135
8136
8137
8138
8139
8140
8141
8142
8143
8144
8145
8146
8147
8148
8149
8150
8151
8152
8153
8154
8155
8156
8157
8158
8159
8160
8161
8162
8163
8164
8165
8166
8167
8168
8169
8170
8171
8172
8173
8174
8175
8176
8177
8178
8179
8180
8181
8182
8183
8184
8185
8186
8187
8188
8189
8190
8191
8192
8193
8194
8195
8196
8197
8198
8199
8200
8201
8202
8203
8204
8205
8206
8207
8208
8209
8210
8211
8212
8213
8214
8215
8216
8217
8218
8219
8220
8221
8222
8223
8224
8225
8226
8227
8228
8229
8230
8231
8232
8233
8234
8235
8236
8237
8238
8239
8240
8241
8242
8243
8244
8245
8246
8247
8248
8249
8250
8251
8252
8253
8254
8255
8256
8257
8258
8259
8260
8261
8262
8263
8264
8265
8266
8267
8268
8269
8270
8271
8272
8273
8274
8275
8276
8277
8278
8279
8280
8281
8282
8283
8284
8285
8286
8287
8288
8289
8290
8291
8292
8293
8294
8295
8296
8297
8298
8299
8300
8301
8302
8303
8304
8305
8306
8307
8308
8309
8310
8311
8312
8313
8314
8315
8316
8317
8318
8319
8320
8321
8322
8323
8324
8325
8326
8327
8328
8329
8330
8331
8332
8333
8334
8335
8336
8337
8338
8339
8340
8341
8342
8343
8344
8345
8346
8347
8348
8349
8350
8351
8352
8353
8354
8355
8356
8357
8358
8359
8360
8361
8362
8363
8364
8365
8366
8367
8368
8369
8370
8371
8372
8373
8374
8375
8376
8377
8378
8379
8380
8381
8382
8383
8384
8385
8386
8387
8388
8389
8390
8391
8392
8393
8394
8395
8396
8397
8398
8399
8400
8401
8402
8403
8404
8405
8406
8407
8408
8409
8410
8411
8412
8413
8414
8415
8416
8417
8418
8419
8420
8421
8422
8423
8424
8425
8426
8427
8428
8429
8430
8431
8432
8433
8434
8435
8436
8437
8438
8439
8440
8441
8442
8443
8444
8445
8446
8447
8448
8449
8450
8451
8452
8453
8454
8455
8456
8457
8458
8459
8460
8461
8462
8463
8464
8465
8466
8467
8468
8469
8470
8471
8472
8473
8474
8475
8476
8477
8478
8479
8480
8481
8482
8483
8484
8485
8486
8487
8488
8489
8490
8491
8492
8493
8494
8495
8496
8497
8498
8499
8500
8501
8502
8503
8504
8505
8506
8507
8508
8509
8510
8511
8512
8513
8514
8515
8516
8517
8518
8519
8520
8521
8522
8523
8524
8525
8526
8527
8528
8529
8530
8531
8532
8533
8534
8535
8536
8537
8538
8539
8540
8541
8542
8543
8544
8545
8546
8547
8548
8549
8550
8551
8552
8553
8554
8555
8556
8557
8558
8559
8560
8561
8562
8563
8564
8565
8566
8567
8568
8569
8570
8571
8572
8573
8574
8575
8576
8577
8578
8579
8580
8581
8582
8583
8584
8585
8586
8587
8588
8589
8590
8591
8592
8593
8594
8595
8596
8597
8598
8599
8600
8601
8602
8603
8604
8605
8606
8607
8608
8609
8610
8611
8612
8613
8614
8615
8616
8617
8618
8619
8620
8621
8622
8623
8624
8625
8626
8627
8628
8629
8630
8631
8632
8633
8634
8635
8636
8637
8638
8639
8640
8641
8642
8643
8644
8645
8646
8647
8648
8649
8650
8651
8652
8653
8654
8655
8656
8657
8658
8659
8660
8661
8662
8663
8664
8665
8666
8667
8668
8669
8670
8671
8672
8673
8674
8675
8676
8677
8678
8679
8680
8681
8682
8683
8684
8685
8686
8687
8688
8689
8690
8691
8692
8693
8694
8695
8696
8697
8698
8699
8700
8701
8702
8703
8704
8705
8706
8707
8708
8709
8710
8711
8712
8713
8714
8715
8716
8717
8718
8719
8720
8721
8722
8723
8724
8725
8726
8727
8728
8729
8730
8731
8732
8733
8734
8735
8736
8737
8738
8739
8740
8741
8742
8743
8744
8745
8746
8747
8748
8749
8750
8751
8752
8753
8754
8755
8756
8757
8758
8759
8760
8761
8762
8763
8764
8765
8766
8767
8768
8769
8770
8771
8772
8773
8774
8775
8776
8777
8778
8779
8780
8781
8782
8783
8784
8785
8786
8787
8788
8789
8790
8791
8792
8793
8794
8795
8796
8797
8798
8799
8800
8801
8802
8803
8804
8805
8806
8807
8808
8809
8810
8811
8812
8813
8814
8815
8816
8817
8818
8819
8820
8821
8822
8823
8824
8825
8826
8827
8828
8829
8830
8831
8832
8833
8834
8835
8836
8837
8838
8839
8840
8841
8842
8843
8844
8845
8846
8847
8848
8849
8850
8851
8852
8853
8854
8855
8856
8857
8858
8859
8860
8861
8862
8863
8864
8865
8866
8867
8868
8869
8870
8871
8872
8873
8874
8875
8876
8877
8878
8879
8880
8881
8882
8883
8884
8885
8886
8887
8888
8889
8890
8891
8892
8893
8894
8895
8896
8897
8898
8899
8900
8901
8902
8903
8904
8905
8906
8907
8908
8909
8910
8911
8912
8913
8914
8915
8916
8917
8918
8919
8920
8921
8922
8923
8924
8925
8926
8927
8928
8929
8930
8931
8932
8933
8934
8935
8936
8937
8938
8939
8940
8941
8942
8943
8944
8945
8946
8947
8948
8949
8950
8951
8952
8953
8954
8955
8956
8957
8958
8959
8960
8961
8962
8963
8964
8965
8966
8967
8968
8969
8970
8971
8972
8973
8974
8975
8976
8977
8978
8979
8980
8981
8982
8983
8984
8985
8986
8987
8988
8989
8990
8991
8992
8993
8994
8995
8996
8997
8998
8999
9000
9001
9002
9003
9004
9005
9006
9007
9008
9009
9010
9011
9012
9013
9014
9015
9016
9017
9018
9019
9020
9021
9022
9023
9024
9025
9026
9027
9028
9029
9030
9031
9032
9033
9034
9035
9036
9037
9038
9039
9040
9041
9042
9043
9044
9045
9046
9047
9048
9049
9050
9051
9052
9053
9054
9055
9056
9057
9058
9059
9060
9061
9062
9063
9064
9065
9066
9067
9068
9069
9070
9071
9072
9073
9074
9075
9076
9077
9078
9079
9080
9081
9082
9083
9084
9085
9086
9087
9088
9089
9090
9091
9092
9093
9094
9095
9096
9097
9098
9099
9100
9101
9102
9103
9104
9105
9106
9107
9108
9109
9110
9111
9112
9113
9114
9115
9116
9117
9118
9119
9120
9121
9122
9123
9124
9125
9126
9127
9128
9129
9130
9131
9132
9133
9134
9135
9136
9137
9138
9139
9140
9141
9142
9143
9144
9145
9146
9147
9148
9149
9150
9151
9152
9153
9154
9155
9156
9157
9158
9159
9160
9161
9162
9163
9164
9165
9166
9167
9168
9169
9170
9171
9172
9173
9174
9175
9176
9177
9178
9179
9180
9181
9182
9183
9184
9185
9186
9187
9188
9189
9190
9191
9192
9193
9194
9195
9196
9197
9198
9199
9200
9201
9202
9203
9204
9205
9206
9207
9208
9209
9210
9211
9212
9213
9214
9215
9216
9217
9218
9219
9220
9221
9222
9223
9224
9225
9226
9227
9228
9229
9230
9231
9232
9233
9234
9235
9236
9237
9238
9239
9240
9241
9242
9243
9244
9245
9246
9247
9248
9249
9250
9251
9252
9253
9254
9255
9256
9257
9258
9259
9260
9261
9262
9263
9264
9265
9266
9267
9268
9269
9270
9271
9272
9273
9274
9275
9276
9277
9278
9279
9280
9281
9282
9283
9284
9285
9286
9287
9288
9289
9290
9291
9292
9293
9294
9295
9296
9297
9298
9299
9300
9301
9302
9303
9304
9305
9306
9307
9308
9309
9310
9311
9312
9313
9314
9315
9316
9317
9318
9319
9320
9321
9322
9323
9324
9325
9326
9327
9328
9329
9330
9331
9332
9333
9334
9335
9336
9337
9338
9339
9340
9341
9342
9343
9344
9345
9346
9347
9348
9349
9350
9351
9352
9353
9354
9355
9356
9357
9358
9359
9360
9361
9362
9363
9364
9365
9366
9367
9368
9369
9370
9371
9372
9373
9374
9375
9376
9377
9378
9379
9380
9381
9382
9383
9384
9385
9386
9387
9388
9389
9390
9391
9392
9393
9394
9395
9396
9397
9398
9399
9400
9401
9402
9403
9404
9405
9406
9407
9408
9409
9410
9411
9412
9413
9414
9415
9416
9417
9418
9419
9420
9421
9422
9423
9424
9425
9426
9427
9428
9429
9430
9431
9432
9433
9434
9435
9436
9437
9438
9439
9440
9441
9442
9443
9444
9445
9446
9447
9448
9449
9450
9451
9452
9453
9454
9455
9456
9457
9458
9459
9460
9461
9462
9463
9464
9465
9466
9467
9468
9469
9470
9471
9472
9473
9474
9475
9476
9477
9478
9479
9480
9481
9482
9483
9484
9485
9486
9487
9488
9489
9490
9491
9492
9493
9494
9495
9496
9497
9498
9499
9500
9501
9502
9503
9504
9505
9506
9507
9508
9509
9510
9511
9512
9513
9514
9515
9516
9517
9518
9519
9520
9521
9522
9523
9524
9525
9526
9527
9528
9529
9530
9531
9532
9533
9534
9535
9536
9537
9538
9539
9540
9541
9542
9543
9544
9545
9546
9547
9548
9549
9550
9551
9552
9553
9554
9555
9556
9557
9558
9559
9560
9561
9562
9563
9564
9565
9566
9567
9568
9569
9570
9571
9572
9573
9574
9575
9576
9577
9578
9579
9580
9581
9582
9583
9584
9585
9586
9587
9588
9589
9590
9591
9592
9593
9594
9595
9596
9597
9598
9599
9600
9601
9602
9603
9604
9605
9606
9607
9608
9609
9610
9611
9612
9613
9614
9615
9616
9617
9618
9619
9620
9621
9622
9623
9624
9625
9626
9627
9628
9629
9630
9631
9632
9633
9634
9635
9636
9637
9638
9639
9640
9641
9642
9643
9644
9645
9646
9647
9648
9649
9650
9651
9652
9653
9654
9655
9656
9657
9658
9659
9660
9661
9662
9663
9664
9665
9666
9667
9668
9669
9670
9671
9672
9673
9674
9675
9676
9677
9678
9679
9680
9681
9682
9683
9684
9685
9686
9687
9688
9689
9690
9691
9692
9693
9694
9695
9696
9697
9698
9699
9700
9701
9702
9703
9704
9705
9706
9707
9708
9709
9710
9711
9712
9713
9714
9715
9716
9717
9718
9719
9720
9721
9722
9723
9724
9725
9726
9727
9728
9729
9730
9731
9732
9733
9734
9735
9736
9737
9738
9739
9740
9741
9742
9743
9744
9745
9746
9747
9748
9749
9750
9751
9752
9753
9754
9755
9756
9757
9758
9759
9760
9761
9762
9763
9764
9765
9766
9767
9768
9769
9770
9771
9772
9773
9774
9775
9776
9777
9778
9779
9780
9781
9782
9783
9784
9785
9786
9787
9788
9789
9790
9791
9792
9793
9794
9795
9796
9797
9798
9799
9800
9801
9802
9803
9804
9805
9806
9807
9808
9809
9810
9811
9812
9813
9814
9815
9816
9817
9818
9819
9820
9821
9822
9823
9824
9825
9826
9827
9828
9829
9830
9831
9832
9833
9834
9835
9836
9837
9838
9839
9840
9841
9842
9843
9844
9845
9846
9847
9848
9849
9850
9851
9852
9853
9854
9855
9856
9857
9858
9859
9860
9861
9862
9863
9864
9865
9866
9867
9868
9869
9870
9871
9872
9873
9874
9875
9876
9877
9878
9879
9880
9881
9882
9883
9884
9885
9886
9887
9888
9889
9890
9891
9892
9893
9894
9895
9896
9897
9898
9899
9900
9901
9902
9903
9904
9905
9906
9907
9908
9909
9910
9911
9912
9913
9914
9915
9916
9917
9918
9919
9920
9921
9922
9923
9924
9925
9926
9927
9928
9929
9930
9931
9932
9933
9934
9935
9936
9937
9938
9939
9940
9941
9942
9943
9944
9945
9946
9947
9948
9949
9950
9951
9952
9953
9954
9955
9956
9957
9958
9959
9960
9961
9962
9963
9964
9965
9966
9967
9968
9969
9970
9971
9972
9973
9974
9975
9976
9977
9978
9979
9980
9981
9982
9983
9984
9985
9986
9987
9988
9989
9990
9991
9992
9993
9994
9995
9996
9997
9998
9999
10000
10001
10002
10003
10004
10005
10006
10007
10008
10009
10010
10011
10012
10013
10014
10015
10016
10017
10018
10019
10020
10021
10022
10023
10024
10025
10026
10027
10028
10029
10030
10031
10032
10033
10034
10035
10036
10037
10038
10039
10040
10041
10042
10043
10044
10045
10046
10047
10048
10049
10050
10051
10052
10053
10054
10055
10056
10057
10058
10059
10060
10061
10062
10063
10064
10065
10066
10067
10068
10069
10070
10071
10072
10073
10074
10075
10076
10077
10078
10079
10080
10081
10082
10083
10084
10085
10086
10087
10088
10089
10090
10091
10092
10093
10094
10095
10096
10097
10098
10099
10100
10101
10102
10103
10104
10105
10106
10107
10108
10109
10110
10111
10112
10113
10114
10115
10116
10117
10118
10119
10120
10121
10122
10123
10124
10125
10126
10127
10128
10129
10130
10131
10132
10133
10134
10135
10136
10137
10138
10139
10140
10141
10142
10143
10144
10145
10146
10147
10148
10149
10150
10151
10152
10153
10154
10155
10156
10157
10158
10159
10160
10161
10162
10163
10164
10165
10166
10167
10168
10169
10170
10171
10172
10173
10174
10175
10176
10177
10178
10179
10180
10181
10182
10183
10184
10185
10186
10187
10188
10189
10190
10191
10192
10193
10194
10195
10196
10197
10198
10199
10200
10201
10202
10203
10204
10205
10206
10207
10208
10209
10210
10211
10212
10213
10214
10215
10216
10217
10218
10219
10220
10221
10222
10223
10224
10225
10226
10227
10228
10229
10230
10231
10232
10233
10234
10235
10236
10237
10238
10239
10240
10241
10242
10243
10244
10245
10246
10247
10248
10249
10250
10251
10252
10253
10254
10255
10256
10257
10258
10259
10260
10261
10262
10263
10264
10265
10266
10267
10268
10269
10270
10271
10272
10273
10274
10275
10276
10277
10278
10279
10280
10281
10282
10283
10284
10285
10286
10287
10288
10289
10290
10291
10292
10293
10294
10295
10296
10297
10298
10299
10300
10301
10302
10303
10304
10305
10306
10307
10308
10309
10310
10311
10312
10313
10314
10315
10316
10317
10318
10319
10320
10321
10322
10323
10324
10325
10326
10327
10328
10329
10330
10331
10332
10333
10334
10335
10336
10337
10338
10339
10340
10341
10342
10343
10344
10345
10346
10347
10348
10349
10350
10351
10352
10353
10354
10355
10356
10357
10358
10359
10360
10361
10362
10363
10364
10365
10366
10367
10368
10369
10370
10371
10372
10373
10374
10375
10376
10377
10378
10379
10380
10381
10382
10383
10384
10385
10386
10387
10388
10389
10390
10391
10392
10393
10394
10395
10396
10397
10398
10399
10400
10401
10402
10403
10404
10405
10406
10407
10408
10409
10410
10411
10412
10413
10414
10415
10416
10417
10418
10419
10420
10421
10422
10423
10424
10425
10426
10427
10428
10429
10430
10431
10432
10433
10434
10435
10436
10437
10438
10439
10440
10441
10442
10443
10444
10445
10446
10447
10448
10449
10450
10451
10452
10453
10454
10455
10456
10457
10458
10459
10460
10461
10462
10463
10464
10465
10466
10467
10468
10469
10470
10471
10472
10473
10474
10475
10476
10477
10478
10479
10480
10481
10482
10483
10484
10485
10486
10487
10488
10489
10490
10491
10492
10493
10494
10495
10496
10497
10498
10499
10500
10501
10502
10503
10504
10505
10506
10507
10508
10509
10510
10511
10512
10513
10514
10515
10516
10517
10518
10519
10520
10521
10522
10523
10524
10525
10526
10527
10528
10529
10530
10531
10532
10533
10534
10535
10536
10537
10538
10539
10540
10541
10542
10543
10544
10545
10546
10547
10548
10549
10550
10551
10552
10553
10554
10555
10556
10557
10558
10559
10560
10561
10562
10563
10564
10565
10566
10567
10568
10569
10570
10571
10572
10573
10574
10575
10576
10577
10578
10579
10580
10581
10582
10583
10584
10585
10586
10587
10588
10589
10590
10591
10592
10593
10594
10595
10596
10597
10598
10599
10600
10601
10602
10603
10604
10605
10606
10607
10608
10609
10610
10611
10612
10613
10614
10615
10616
10617
10618
10619
10620
10621
10622
10623
10624
10625
10626
10627
10628
10629
10630
10631
10632
10633
10634
10635
10636
10637
10638
10639
10640
10641
10642
10643
10644
10645
10646
10647
10648
10649
10650
10651
10652
10653
10654
10655
10656
10657
10658
10659
10660
10661
10662
10663
10664
10665
10666
10667
10668
10669
10670
10671
10672
10673
10674
10675
10676
10677
10678
10679
10680
10681
10682
10683
10684
10685
10686
10687
10688
10689
10690
10691
10692
10693
10694
10695
10696
10697
10698
10699
10700
10701
10702
10703
10704
10705
10706
10707
10708
10709
10710
10711
10712
10713
10714
10715
10716
10717
10718
10719
10720
10721
10722
10723
10724
10725
10726
10727
10728
10729
10730
10731
10732
10733
10734
10735
10736
10737
10738
10739
10740
10741
10742
10743
10744
10745
10746
10747
10748
10749
10750
10751
10752
10753
10754
10755
10756
10757
10758
10759
10760
10761
10762
10763
10764
10765
10766
10767
10768
10769
10770
10771
10772
10773
10774
10775
10776
10777
10778
10779
10780
10781
10782
10783
10784
10785
10786
10787
10788
10789
10790
10791
10792
10793
10794
10795
10796
10797
10798
10799
10800
10801
10802
10803
10804
10805
10806
10807
10808
10809
10810
10811
10812
10813
10814
10815
10816
10817
10818
10819
10820
10821
10822
10823
10824
10825
10826
10827
10828
10829
10830
10831
10832
10833
10834
10835
10836
10837
10838
10839
10840
10841
10842
10843
10844
10845
10846
10847
10848
10849
10850
10851
10852
10853
10854
10855
10856
10857
10858
10859
10860
10861
10862
10863
10864
10865
10866
10867
10868
10869
10870
10871
10872
10873
10874
10875
10876
10877
10878
10879
10880
10881
10882
10883
10884
10885
10886
10887
10888
10889
10890
10891
10892
10893
10894
10895
10896
10897
10898
10899
10900
10901
10902
10903
10904
10905
10906
10907
10908
10909
10910
10911
10912
10913
10914
10915
10916
10917
10918
10919
10920
10921
10922
10923
10924
10925
10926
10927
10928
10929
10930
10931
10932
10933
10934
10935
10936
10937
10938
10939
10940
10941
10942
10943
10944
10945
10946
10947
10948
10949
10950
10951
10952
10953
10954
10955
10956
10957
10958
10959
10960
10961
10962
10963
10964
10965
10966
10967
10968
10969
10970
10971
10972
10973
10974
10975
10976
10977
10978
10979
10980
10981
10982
10983
10984
10985
10986
10987
10988
10989
10990
10991
10992
10993
10994
10995
10996
10997
10998
10999
11000
11001
11002
11003
11004
11005
11006
11007
11008
11009
11010
11011
11012
11013
11014
11015
11016
11017
11018
11019
11020
11021
11022
11023
11024
11025
11026
11027
11028
11029
11030
11031
11032
11033
11034
11035
11036
11037
11038
11039
11040
11041
11042
11043
11044
11045
11046
11047
11048
11049
11050
11051
11052
11053
11054
11055
11056
11057
11058
11059
11060
11061
11062
11063
11064
11065
11066
11067
11068
11069
11070
11071
11072
11073
11074
11075
11076
11077
11078
11079
11080
11081
11082
11083
11084
11085
11086
11087
11088
11089
11090
11091
11092
11093
11094
11095
11096
11097
11098
11099
11100
11101
11102
11103
11104
11105
11106
11107
11108
11109
11110
11111
11112
11113
11114
11115
11116
11117
11118
11119
11120
11121
11122
11123
11124
11125
11126
11127
11128
11129
11130
11131
11132
11133
11134
11135
11136
11137
11138
11139
11140
11141
11142
11143
11144
11145
11146
11147
11148
11149
11150
11151
11152
11153
11154
11155
11156
11157
11158
11159
11160
11161
11162
11163
11164
11165
11166
11167
11168
11169
11170
11171
11172
11173
11174
11175
11176
11177
11178
11179
11180
11181
11182
11183
11184
11185
11186
11187
11188
11189
11190
11191
11192
11193
11194
11195
11196
11197
11198
11199
11200
11201
11202
11203
11204
11205
11206
11207
11208
11209
11210
11211
11212
11213
11214
11215
11216
11217
11218
11219
11220
11221
11222
11223
11224
11225
11226
11227
11228
11229
11230
11231
11232
11233
11234
11235
11236
11237
11238
11239
11240
11241
11242
11243
11244
11245
11246
11247
11248
11249
11250
11251
11252
11253
11254
11255
11256
11257
11258
11259
11260
11261
11262
11263
11264
11265
11266
11267
11268
11269
11270
11271
11272
11273
11274
11275
11276
11277
11278
11279
11280
11281
11282
11283
11284
11285
11286
11287
11288
11289
11290
11291
11292
11293
11294
11295
11296
11297
11298
11299
11300
11301
11302
11303
11304
11305
11306
11307
11308
11309
11310
11311
11312
11313
11314
11315
11316
11317
11318
11319
11320
11321
11322
11323
11324
11325
11326
11327
11328
11329
11330
11331
11332
11333
11334
11335
11336
11337
11338
11339
11340
11341
11342
11343
11344
11345
11346
11347
11348
11349
11350
11351
11352
11353
11354
11355
11356
11357
11358
11359
11360
11361
11362
11363
11364
11365
11366
11367
11368
11369
11370
11371
11372
11373
11374
11375
11376
11377
11378
11379
11380
11381
11382
11383
11384
11385
11386
11387
11388
11389
11390
11391
11392
11393
11394
11395
11396
11397
11398
11399
11400
11401
11402
11403
11404
11405
11406
11407
11408
11409
11410
11411
11412
11413
11414
11415
11416
11417
11418
11419
11420
11421
11422
11423
11424
11425
11426
11427
11428
11429
11430
11431
11432
11433
11434
11435
11436
11437
11438
11439
11440
11441
11442
11443
11444
11445
11446
11447
11448
11449
11450
11451
11452
11453
11454
11455
11456
11457
11458
11459
11460
11461
11462
11463
11464
11465
11466
11467
11468
11469
11470
11471
11472
11473
11474
11475
11476
11477
11478
11479
11480
11481
11482
11483
11484
11485
11486
11487
11488
11489
11490
11491
11492
11493
11494
11495
11496
11497
11498
11499
11500
11501
11502
11503
11504
11505
11506
11507
11508
11509
11510
11511
11512
11513
11514
11515
11516
11517
11518
11519
11520
11521
11522
11523
11524
11525
11526
11527
11528
11529
11530
11531
11532
11533
11534
11535
11536
11537
11538
11539
11540
11541
11542
11543
11544
11545
11546
11547
11548
11549
11550
11551
11552
11553
11554
11555
11556
11557
11558
11559
11560
11561
11562
11563
11564
11565
11566
11567
11568
11569
11570
11571
11572
11573
11574
11575
11576
11577
11578
11579
11580
11581
11582
11583
11584
11585
11586
11587
11588
11589
11590
11591
11592
11593
11594
11595
11596
11597
11598
11599
11600
11601
11602
11603
11604
11605
11606
11607
11608
11609
11610
11611
11612
11613
11614
11615
11616
11617
11618
11619
11620
11621
11622
11623
11624
11625
11626
11627
11628
11629
11630
11631
11632
11633
11634
11635
11636
11637
11638
11639
11640
11641
11642
11643
11644
11645
11646
11647
11648
11649
11650
11651
11652
11653
11654
11655
11656
11657
11658
11659
11660
11661
11662
11663
11664
11665
11666
11667
11668
11669
11670
11671
11672
11673
11674
11675
11676
11677
11678
11679
11680
11681
11682
11683
11684
11685
11686
11687
11688
11689
11690
11691
11692
11693
11694
11695
11696
11697
11698
11699
11700
11701
11702
11703
11704
11705
11706
11707
11708
11709
11710
11711
11712
11713
11714
11715
11716
11717
11718
11719
11720
11721
11722
11723
11724
11725
11726
11727
11728
11729
11730
11731
11732
11733
11734
11735
11736
11737
11738
11739
11740
11741
11742
11743
11744
11745
11746
11747
11748
11749
11750
11751
11752
11753
11754
11755
11756
11757
11758
11759
11760
11761
11762
11763
11764
11765
11766
11767
11768
11769
11770
11771
11772
11773
11774
11775
11776
11777
11778
11779
11780
11781
11782
11783
11784
11785
11786
11787
11788
11789
11790
11791
11792
11793
11794
11795
11796
11797
11798
11799
11800
11801
11802
11803
11804
11805
11806
11807
11808
11809
11810
11811
11812
11813
11814
11815
11816
11817
11818
11819
11820
11821
11822
11823
11824
11825
11826
11827
11828
11829
11830
11831
11832
11833
11834
11835
11836
11837
11838
11839
11840
11841
11842
11843
11844
11845
11846
11847
11848
11849
11850
11851
11852
11853
11854
11855
11856
11857
11858
11859
11860
11861
11862
11863
11864
11865
11866
11867
11868
11869
11870
11871
11872
11873
11874
11875
11876
11877
11878
11879
11880
11881
11882
11883
11884
11885
11886
11887
11888
11889
11890
11891
11892
11893
11894
11895
11896
11897
11898
11899
11900
11901
11902
11903
11904
11905
11906
11907
11908
11909
11910
11911
11912
11913
11914
11915
11916
11917
11918
11919
11920
11921
11922
11923
11924
11925
11926
11927
11928
11929
11930
11931
11932
11933
11934
11935
11936
11937
11938
11939
11940
11941
11942
11943
11944
11945
11946
11947
11948
11949
11950
11951
11952
11953
11954
11955
11956
11957
11958
11959
11960
11961
11962
11963
11964
11965
11966
11967
11968
11969
11970
11971
11972
11973
11974
11975
11976
11977
11978
11979
11980
11981
11982
11983
11984
11985
11986
11987
11988
11989
11990
11991
11992
11993
11994
11995
11996
11997
11998
11999
12000
12001
12002
12003
12004
12005
12006
12007
12008
12009
12010
12011
12012
12013
12014
12015
12016
12017
12018
12019
12020
12021
12022
12023
12024
12025
12026
12027
12028
12029
12030
12031
12032
12033
12034
//! The formatter entry points and the CST → [`Ir`] lowering.
//!
//! Lowering normalizes whitespace and indentation, reflows prose arguments under
//! [`WrapMode::Reflow`], formats structured math, and owns layout inside expl3
//! regions. Protected regions remain verbatim.
//!
//! [`lower_node`] contains the LaTeX-specific lowering. The surrounding format
//! entry points and the Wadler-style [`Ir`] printer are language-independent.

use std::cell::RefCell;
use std::collections::HashMap;
use std::iter::Peekable;

use rowan::{TextRange, TextSize};

use super::colspec::{self, ColAlign};
use crate::ast::{AstNode, Command, Environment, Group, command_name};
use crate::declarations::ResolvedDeclarations;
use crate::directives;
use crate::parser::is_def_prefix_command;
use crate::parser::lexer::{ExplToggle, expl_toggle};
use crate::parser::{LatexFlavor, parse_with_declarations, parse_with_flavor};
use crate::semantic::expl3::{StatementMap, segment_expl_statements};
use crate::semantic::signature::document_class_signatures;
use crate::semantic::tikz::statement_glue;
use crate::semantic::{
    ArgKind, ArgSpec, ArgumentDomain, CitationPlacement, ContentKind, DelimiterRole, MathClass,
    SignatureDb, Signatures, expl3, match_arg_slot, match_verbatim_arg_slot, math_atoms,
    scan_definitions,
};
use crate::syntax::{
    SyntaxElement, SyntaxKind, SyntaxNode, SyntaxToken, is_collapsible_trivia, is_param_digit,
    is_trivia,
};

use super::context::FormatContext;
use super::ir::Ir;
use super::printer::Printer;
use super::sentence::{ResolvedProfile, SentenceOptions, is_sentence_boundary_text};
use super::style::{
    FormatStyle, ItemIndent, LineEnding, MathWrap, WrapMode, apply_line_ending, detect_line_ending,
};

/// Why a document could not be formatted. The formatter only operates on a clean
/// parse: anything the parser flagged, or any `ERROR` token, is refused rather
/// than silently reshaped.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum FormatError {
    /// The input parsed with `count` syntax error(s); the formatter only
    /// supports input the parser accepts without diagnostics.
    ParseErrors { count: usize },
    /// The CST contains an `ERROR` token the lowering does not handle.
    UnsupportedConstruct { kind: SyntaxKind, snippet: String },
}

impl std::fmt::Display for FormatError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::ParseErrors { count } => write!(
                f,
                "input contains {count} parser diagnostic(s); formatter only supports parseable input"
            ),
            Self::UnsupportedConstruct { kind, snippet } => {
                write!(
                    f,
                    "unsupported construct for formatter: {kind:?} near {snippet:?}"
                )
            }
        }
    }
}

impl std::error::Error for FormatError {}

/// Format `input` with the default [`FormatStyle`].
pub fn format(input: &str) -> Result<String, FormatError> {
    format_with_style(input, FormatStyle::default())
}

/// Format `input` under `style`. Returns [`FormatError`] if the input does not
/// parse cleanly. Note: badness's [`crate::parser::Parse`] carries `errors` +
/// `syntax()`. Uses the
/// [`Document`](LatexFlavor::Document) flavor; [`format_with_style_flavored`] is
/// the entry for `.sty`/`.cls`.
pub fn format_with_style(input: &str, style: FormatStyle) -> Result<String, FormatError> {
    format_with_style_flavored(input, style, LatexFlavor::Document)
}

/// Like [`format_with_style`] but parses `input` under an explicit
/// [`LexConfig`](crate::parser::LexConfig), so a [`Package`](LatexFlavor::Package) flavor (`.sty`/`.cls`)
/// lexes with `@` as a letter (the implicit `\makeatletter`) and a `.dtx` runs
/// the docstrip mode. A bare [`LatexFlavor`] coerces in. The wrap mode is a
/// `style` concern, decided by the caller and independent of the flavor; it
/// defaults to [`WrapMode::Reflow`] for every file kind.
pub fn format_with_style_flavored(
    input: &str,
    style: FormatStyle,
    config: impl Into<crate::parser::LexConfig>,
) -> Result<String, FormatError> {
    format_with_style_flavored_with_signatures(input, style, config, &SignatureDb::default())
}

/// Like [`format_with_style_flavored`] but with explicit
/// [`SentenceOptions`](crate::formatter::SentenceOptions) for the
/// `sentence`/`semantic` wrap modes (the document language and user no-break
/// abbreviations). The CLI resolves these from `badness.toml`; other wrap modes
/// ignore them.
pub fn format_with_style_flavored_sentence(
    input: &str,
    style: FormatStyle,
    config: impl Into<crate::parser::LexConfig>,
    sentence: SentenceOptions<'_>,
) -> Result<String, FormatError> {
    let parsed = parse_with_flavor(input, config);
    if !parsed.errors.is_empty() {
        return Err(FormatError::ParseErrors {
            count: parsed.errors.len(),
        });
    }
    format_node_with_signatures_sentence(&parsed.syntax(), style, &SignatureDb::default(), sentence)
}

/// Like [`format_with_style_flavored_sentence`] but under a project's
/// [declarations](crate::declarations), and with no other signature scope.
///
/// This is the entry for content with no path to anchor local `.sty`/`.cls`
/// resolution against: the CLI's **stdin**, the language server's
/// cache-miss/cancellation fallback, and the dprint plugin, which is sandboxed
/// with no filesystem at all. Each of those would otherwise reach for
/// [`format_with_style_flavored_sentence`], which parses declaration-blind — and
/// a formatter honoring `[environments.…]` for `badness format file.tex` but not
/// for `badness format < file.tex` (nor for the one editor request that races an
/// edit) would be a trap.
///
/// The declarations reach **both** the parse and the signature scope, exactly as
/// they do on the path-bearing entries in the `badness` crate
/// (`formatter::format_file_with_packages_sentence`), so this differs from the
/// full path only by the package tiers it cannot reach — never by precedence,
/// and never by which constructs it recognizes.
pub fn format_with_declarations_sentence(
    input: &str,
    style: FormatStyle,
    config: impl Into<crate::parser::LexConfig>,
    sentence: SentenceOptions<'_>,
    declared: &ResolvedDeclarations,
) -> Result<String, FormatError> {
    let parsed = parse_with_declarations(input, config, declared);
    if !parsed.errors.is_empty() {
        return Err(FormatError::ParseErrors {
            count: parsed.errors.len(),
        });
    }
    format_node_with_signatures_sentence(
        &parsed.syntax(),
        style,
        &declared_scope(declared),
        sentence,
    )
}

/// The signature scope a project's declarations alone make up: the top tier of
/// the disk- and salsa-backed scopes (`semantic::collect_package_signatures`,
/// `incremental::scope_signatures`, both in the `badness` crate), with nothing
/// under it.
pub fn declared_scope(declared: &ResolvedDeclarations) -> SignatureDb {
    let mut scope = SignatureDb::default();
    scope.merge_declarations(declared);
    scope
}

/// Like [`format_with_style_flavored`] but additionally folds an `external`
/// signature scope — the merged definitions of the document's loaded local
/// packages (`semantic::load::collect_package_signatures` and the salsa-cached
/// `incremental::scope_signatures`, both in the `badness` crate) — into the lowering, so calls to
/// package-defined macros are shaped by their real arity/verbatim-ness. The
/// document's own definitions always win over `external`. The CLI uses this for a
/// real file path; passing an empty DB recovers [`format_with_style_flavored`].
pub fn format_with_style_flavored_with_signatures(
    input: &str,
    style: FormatStyle,
    config: impl Into<crate::parser::LexConfig>,
    external: &SignatureDb,
) -> Result<String, FormatError> {
    let parsed = parse_with_flavor(input, config);
    if !parsed.errors.is_empty() {
        return Err(FormatError::ParseErrors {
            count: parsed.errors.len(),
        });
    }

    format_node_with_signatures(&parsed.syntax(), style, external)
}

/// Format an already-parsed CST `root` under `style`. This is the
/// reparse-free entry: the language server hands it the salsa-cached tree
/// (`db.parsed_tree`) instead of re-running the parser. The caller owns the
/// `ParseErrors` guard — this entry assumes the parse was clean and only
/// enforces the `ERROR`-token invariant ([`validate_supported_tokens`]).
/// [`format_with_style`] is the parse-then-format convenience wrapper.
pub fn format_node(root: &SyntaxNode, style: FormatStyle) -> Result<String, FormatError> {
    format_node_with_signatures(root, style, &SignatureDb::default())
}

/// Like [`format_node`] but folds an `external` signature scope (loaded local
/// packages' merged definitions) into the lowering. The language server passes the
/// salsa-cached `incremental::scope_signatures` (in the `badness` crate) here; the document's own
/// definitions always win over `external`. An empty DB recovers [`format_node`].
pub fn format_node_with_signatures(
    root: &SyntaxNode,
    style: FormatStyle,
    external: &SignatureDb,
) -> Result<String, FormatError> {
    format_node_with_signatures_sentence(root, style, external, SentenceOptions::default())
}

/// Like [`format_node_with_signatures`] but with explicit
/// [`SentenceOptions`](crate::formatter::SentenceOptions) for the
/// `sentence`/`semantic` wrap modes.
pub fn format_node_with_signatures_sentence(
    root: &SyntaxNode,
    style: FormatStyle,
    external: &SignatureDb,
    sentence: SentenceOptions<'_>,
) -> Result<String, FormatError> {
    validate_supported_tokens(root)?;

    let ctx = FormatContext::with_sentence(style, sentence);
    let mut formatted = format_root(root, ctx, external, None);
    // Normalize the document's trailing edge: drop any trailing blank lines and
    // per-line trailing whitespace at EOF, then guarantee exactly one final
    // newline. Empty output stays empty. Only ASCII whitespace/newlines are
    // trimmed, so trailing Unicode content (e.g. a non-breaking space) survives.
    let trimmed_len = formatted.trim_end_matches([' ', '\t', '\n', '\r']).len();
    formatted.truncate(trimmed_len);
    if !formatted.is_empty() {
        formatted.push('\n');
    }
    apply_line_ending(&mut formatted, resolve_line_ending(root, style));
    Ok(formatted)
}

/// Range formatting: lay out only the document-level blocks overlapping `range`,
/// returning the formatted text for the `[first block start, last block end]`
/// span. The caller (the `badness` crate's LSP) expands the editor selection to whole
/// document-level-block boundaries before calling, so `range` is already
/// block-aligned. Direct children of the canonical no-indent `document` environment
/// count as document-level blocks alongside children of `ROOT`.
///
/// The whole document is still scanned for `\newcommand` signatures and expl3
/// regions ([`format_root`]), so a selected block depending on an earlier
/// definition or sitting inside an ancestor `\ExplSyntaxOn` is laid out exactly as
/// in a full format; only *emission* is filtered (see [`LowerCtx::range`]). Unlike
/// [`format_node_with_signatures`], the document-level trailing-edge normalization
/// is **not** applied — this is a mid-document fragment, so no final newline is
/// forced. Trailing whitespace is trimmed (the slice it replaces ends at a block
/// boundary), keeping the diff against the original slice clean.
pub fn format_node_range_with_signatures(
    root: &SyntaxNode,
    style: FormatStyle,
    external: &SignatureDb,
    range: TextRange,
) -> Result<String, FormatError> {
    format_node_range_with_signatures_sentence(
        root,
        style,
        external,
        range,
        SentenceOptions::default(),
    )
}

/// Like [`format_node_range_with_signatures`] but with explicit
/// [`SentenceOptions`](crate::formatter::SentenceOptions) for the
/// `sentence`/`semantic` wrap modes.
pub fn format_node_range_with_signatures_sentence(
    root: &SyntaxNode,
    style: FormatStyle,
    external: &SignatureDb,
    range: TextRange,
    sentence: SentenceOptions<'_>,
) -> Result<String, FormatError> {
    validate_supported_tokens(root)?;

    let ctx = FormatContext::with_sentence(style, sentence);
    let mut formatted = format_root(root, ctx, external, Some(range));
    let trimmed_len = formatted.trim_end_matches([' ', '\t', '\n', '\r']).len();
    formatted.truncate(trimmed_len);
    // Detected from the whole document, not the fragment: the replacement has to
    // match the endings of the text it splices into, and a block that happens to
    // hold no line break of its own would otherwise answer `Lf`.
    apply_line_ending(&mut formatted, resolve_line_ending(root, style));
    Ok(formatted)
}

/// The concrete ending `style` calls for on this document ([`LineEnding::Auto`]
/// resolved against what the source used).
fn resolve_line_ending(root: &SyntaxNode, style: FormatStyle) -> LineEnding {
    if style.line_ending == LineEnding::Auto {
        style.line_ending.resolve(detect_line_ending(&root.text()))
    } else {
        style.line_ending.resolve(LineEnding::Lf)
    }
}

/// Refuse any `ERROR` token. A clean parse should contain none, but the parser
/// can emit them on recovery; the formatter never reshapes around them.
fn validate_supported_tokens(root: &SyntaxNode) -> Result<(), FormatError> {
    for element in root.descendants_with_tokens() {
        let Some(token) = element.into_token() else {
            continue;
        };
        if token.kind() == SyntaxKind::ERROR {
            return Err(FormatError::UnsupportedConstruct {
                kind: token.kind(),
                snippet: token.text().to_string(),
            });
        }
    }
    Ok(())
}

fn format_root(
    root: &SyntaxNode,
    ctx: FormatContext,
    external: &SignatureDb,
    range: Option<TextRange>,
) -> String {
    // Class meanings depend only on the document's literal declaration. Loaded
    // local packages and document redefinitions override these curated defaults.
    let mut user = document_class_signatures(root).cloned().unwrap_or_default();
    user.merge_from(external, None);
    // Scan the document's own `\newcommand`/`\newenvironment`/xparse definitions
    // once, so the lowering resolves a locally-defined construct's arity (not just
    // the built-in DB's). They are overlaid on top of `external` — the merged
    // signatures of any loaded local packages — so a document redefinition wins
    // over a package. Held by value for the whole lowering.
    user.merge_from(&scan_definitions(root), None);
    // The expl3 source regions, recomputed read-only from the same toggle set the
    // lexer uses ([`expl_toggle`]). Inside them source whitespace is catcode-9
    // (ignored) and `~` is catcode-10 (a literal space), so the formatter fully owns
    // layout. Held by value for the whole lowering, like `user`.
    let regions = expl3_regions(root);
    // The spans the author turned layout off over, resolved from this file's own
    // comment directives (see [`crate::directives`]). A pure function of the
    // tree, held by value for the whole lowering like `regions`. Empty for the
    // overwhelming majority of documents, in which case every query is free.
    let suppressed = directives::Suppressions::build(root);
    // The sentence-boundary profile for the `sentence`/`semantic` wrap modes,
    // resolved from the run's [`SentenceOptions`]. `Copy`, borrowing the merged
    // no-break slice `ctx` still owns for the whole call, so it rides `LowerCtx`
    // like the bare `wrap` mode. Never consulted under `reflow`/`preserve`.
    let profile = ctx.sentence().resolved();
    // The `.dtx` doc-paragraph reflow-safety memo (see [`DtxReflowCache`]),
    // owned here for the whole lowering like `user` and `regions`.
    let dtx_reflow_cache = DtxReflowCache::default();
    let algorithm2e_cache = RefCell::new(HashMap::new());
    let cx = LowerCtx {
        wrap: ctx.style().wrap,
        item_indent: ctx.style().item_indent,
        indent_width: ctx.style().indent_width,
        // Resolved here (never `Auto` past this point), so library callers get the
        // derivation from `wrap` for free.
        math_wrap: ctx.style().math_wrap.resolve(ctx.style().wrap),
        stable_target: ctx.style().stable_wrap_target(),
        signatures: Signatures::new(&user),
        expl3_regions: &regions,
        suppressed: suppressed.format_ranges(),
        profile,
        range,
        dtx_reflow_cache: &dtx_reflow_cache,
        algorithm2e_cache: &algorithm2e_cache,
        dtx_margin_probe: false,
        preserve_dtx_nested_layout: false,
        in_dtx_doc_region: false,
        in_alignment_cell: false,
        absorbed_control_newline: None,
        omitted_leading_comment: None,
        is_dtx: root
            .descendants_with_tokens()
            .filter_map(|e| e.into_token())
            .any(|t| matches!(t.kind(), SyntaxKind::DOC_MARGIN | SyntaxKind::GUARD)),
    };
    // Saturate `Group::expand` at the lowering->printer seam: after this, the
    // flag is the single representation of "forced open" the printer trusts.
    // Lowering-time `contains_forced_break` queries use the immutable summary
    // stored at each group boundary; this final pass still marks every nested
    // group for the printer in one bottom-up walk.
    let ir = lower_node(root, cx).propagate_breaks();
    Printer::new(ctx.style()).print(&ir)
}

/// Whether two byte ranges overlap (share at least one byte). Half-open, so ranges
/// that merely touch at a boundary (`a.end == b.start`) do not overlap — used by
/// the range-formatting emission filter to keep a top-level block's leading/trailing
/// trivia (which abuts but does not overlap the block-aligned range) out of the
/// fragment.
fn ranges_overlap(a: TextRange, b: TextRange) -> bool {
    a.start() < b.end() && b.start() < a.end()
}

/// Whether `range` lies wholly inside the body of the canonical `document`
/// environment. Its body is the formatter's canonical no-indent case, so exposing
/// direct body blocks does not discard any ancestor indentation context.
fn document_body_contains(node: &SyntaxNode, range: TextRange) -> bool {
    let Some(environment) = Environment::cast(node.clone()) else {
        return false;
    };
    if environment.name().as_deref() != Some("document") {
        return false;
    }
    let (Some(begin), Some(end)) = (environment.begin(), environment.end()) else {
        return false;
    };
    range.start() >= begin.syntax().text_range().end()
        && range.end() <= end.syntax().text_range().start()
}

/// The nearest preceding sibling *element* of `node`, skipping `WHITESPACE`/`NEWLINE`
/// trivia. Used by the definee gate to find the command a toggle would be the
/// definee of.
fn prev_nontrivia_element(node: &SyntaxNode) -> Option<SyntaxElement> {
    let mut prev = node.prev_sibling_or_token();
    while let Some(el) = prev {
        if let SyntaxElement::Token(t) = &el
            && is_collapsible_trivia(t.kind())
        {
            prev = el.prev_sibling_or_token();
            continue;
        }
        return Some(el);
    }
    None
}

/// Whether an expl3 toggle `CONTROL_WORD` sits at *top-level statement* position, so
/// TeX actually executes it and switches catcodes at load. Two shapes are rejected
/// (issue #69), both false positives of the name-only model:
///
/// - **Definee position:** the toggle command's immediately-preceding non-trivia
///   sibling is a `\def`/`\let`-family primitive, so the toggle is the control
///   sequence being defined (`\protected\def\ProvidesExplPackage{…}`), never run.
/// - **Nested in a group / definition body:** an ancestor of the toggle's command is
///   a `GROUP` or `OPTIONAL`, so the toggle is tokenized into a replacement text and
///   executed — if ever — only when that macro runs, not at load.
///
/// The lexer's letter mode keeps the naive name-only model: mis-lexing a name only
/// splits CST tokens (lossless, cosmetic); only mis-*owning* layout rewrites meaning.
fn toggle_is_top_level(token: &SyntaxToken) -> bool {
    let Some(command) = token.parent() else {
        return true;
    };
    if command.kind() != SyntaxKind::COMMAND {
        // Not the head of a command node — leave the naive model in charge.
        return true;
    }
    // Rule 1: nested inside an attached group or definition body.
    for ancestor in command.ancestors().skip(1) {
        match ancestor.kind() {
            SyntaxKind::GROUP | SyntaxKind::OPTIONAL => return false,
            SyntaxKind::ROOT => break,
            _ => {}
        }
    }
    // Rule 2: definee of a `\def`/`\let`-family command. `command_name` strips the
    // leading `\`, so reconstruct it for the shared curated set.
    if let Some(SyntaxElement::Node(prev)) = prev_nontrivia_element(&command)
        && prev.kind() == SyntaxKind::COMMAND
        && let Some(name) = command_name(&prev)
        && (is_def_prefix_command(&format!("\\{name}"))
            || matches!(name.as_str(), "let" | "futurelet"))
    {
        return false;
    }
    true
}

/// The byte ranges of the document's expl3 regions, in document order. A region runs
/// from an opener (`\ExplSyntaxOn`, or a `\ProvidesExpl*` declaration, which opens
/// expl3 for the rest of the file) through the matching `\ExplSyntaxOff` (inclusive
/// of both toggle commands), or to end of input when unclosed. The toggle *name set*
/// is read from [`expl_toggle`] — the same fixed set the lexer flips its `expl_syntax`
/// flag on — but the formatter additionally applies a *positional* gate
/// ([`toggle_is_top_level`]): only a top-level toggle opens a formatter-owned region.
/// The name set stays shared so the two never drift; positional layout ownership
/// remains formatter-specific.
///
/// Matches only [`SyntaxKind::CONTROL_WORD`] tokens, so a `\ExplSyntaxOn` written
/// inside `\verb`/a comment (a `VERB`/`COMMENT` token, never a `CONTROL_WORD`) is
/// not a toggle, exactly as in the lexer. The CST is untouched.
///
/// `pub` so the linter (in the `badness` crate) shares the *same* region
/// computation (the `unclosed-math-delimiter` rule suppresses inside expl3
/// code), keeping the formatter and linter from drifting on what counts as an
/// expl3 region.
pub fn expl3_regions(root: &SyntaxNode) -> Vec<TextRange> {
    let mut regions: Vec<TextRange> = Vec::new();
    let mut open: Option<TextSize> = None;
    for token in root
        .descendants_with_tokens()
        .filter_map(|e| e.into_token())
        .filter(|t| t.kind() == SyntaxKind::CONTROL_WORD)
    {
        // Positional gate: a gated-out toggle is skipped entirely (`On` and `Off`
        // alike), so a stored or definee toggle neither opens nor closes a region.
        let toggle = match expl_toggle(token.text()) {
            Some(t) if toggle_is_top_level(&token) => t,
            _ => continue,
        };
        match toggle {
            // A redundant inner `\ExplSyntaxOn` does not restart the region (the
            // lexer's flag is an idempotent set-true).
            ExplToggle::On if open.is_none() => open = Some(token.text_range().start()),
            ExplToggle::On => {}
            ExplToggle::Off => {
                if let Some(start) = open.take() {
                    regions.push(TextRange::new(start, token.text_range().end()));
                }
                // A stray `\ExplSyntaxOff` with no open region is ignored (toggling
                // an already-false flag is a no-op), matching the lexer.
            }
        }
    }
    if let Some(start) = open.take() {
        // An unclosed region runs to end of input (the lexer's flag simply stays
        // true to EOF).
        regions.push(TextRange::new(start, root.text_range().end()));
    }
    let regions = intersect_macrocode_bodies(root, regions);
    let regions = subtract_doc_margin_lines(root, regions);
    subtract_guarded_line_runs(root, regions)
}

/// In a `.dtx` (any `DOC_MARGIN` token present), restrict the expl3 regions to
/// `macrocode`/`macrocode*` chunk *bodies* — the only lines docstrip extracts as
/// code. The margin subtraction below removes doc lines that carry their `%` in
/// column 0, but the doc part is not obliged to margin every line (a stray
/// `␣%` comment, issue #58): any non-chunk line is documentation regardless of
/// its first column, so relayout must never own it. A no-op for non-`.dtx`
/// documents, where an unmargined `\begin{macrocode}` is just an ordinary
/// user environment.
fn intersect_macrocode_bodies(root: &SyntaxNode, regions: Vec<TextRange>) -> Vec<TextRange> {
    if regions.is_empty()
        || !root
            .descendants_with_tokens()
            .filter_map(|e| e.into_token())
            .any(|t| t.kind() == SyntaxKind::DOC_MARGIN)
    {
        return regions;
    }
    // `macrocode` never nests, so the bodies are disjoint and in document order.
    // A body runs from the end of the `\begin` frame to the start of the `\end`
    // frame's `\end` (or the chunk end when the frame is missing at EOF); the
    // frame line's own margin/whitespace inside that span is removed by the
    // margin subtraction pass.
    let bodies: Vec<TextRange> = root
        .descendants()
        .filter(|n| n.kind() == SyntaxKind::ENVIRONMENT)
        .filter_map(Environment::cast)
        .filter(|e| matches!(e.name().as_deref(), Some("macrocode" | "macrocode*")))
        .filter_map(|e| {
            let start = e.begin()?.syntax().text_range().end();
            let end = e
                .end()
                .map(|end| end.syntax().text_range().start())
                .unwrap_or_else(|| e.syntax().text_range().end());
            (start < end).then_some(TextRange::new(start, end))
        })
        .collect();
    let mut out = Vec::with_capacity(regions.len());
    let mut b = bodies.iter().peekable();
    for region in regions {
        while let Some(&&body) = b.peek() {
            if body.end() <= region.start() {
                b.next();
                continue;
            }
            if body.start() >= region.end() {
                break;
            }
            let start = region.start().max(body.start());
            let end = region.end().min(body.end());
            if start < end {
                out.push(TextRange::new(start, end));
            }
            if body.end() >= region.end() {
                break;
            }
            b.next();
        }
    }
    out
}

/// Remove every `.dtx` documentation line from the expl3 regions. In a `.dtx`,
/// an `\ExplSyntaxOn` in one `macrocode` chunk is regularly matched by the
/// `\ExplSyntaxOff` several chunks later, so the lexical region spans the
/// documentation in between — margined doc lines and the `%    \end{macrocode}`
/// frame lines themselves. Those lines are *not* expl3 code (at package-load
/// time they are `%` comments; the margin must stay in column 0), so relayout
/// must never own them: subtract each doc-margined line (its `DOC_MARGIN` opens
/// the line by construction — the lexer emits margins at line start only)
/// through its terminating newline. Code lines inside chunk bodies carry no
/// margin and stay in-region. A no-op for non-`.dtx` documents (no `DOC_MARGIN`
/// tokens, and the common all-code case short-circuits on the first hole scan).
fn subtract_doc_margin_lines(root: &SyntaxNode, regions: Vec<TextRange>) -> Vec<TextRange> {
    if regions.is_empty() {
        return regions;
    }
    // One pass over the leaves: a DOC_MARGIN opens a hole, the next NEWLINE
    // (inclusive) closes it.
    let mut holes: Vec<TextRange> = Vec::new();
    let mut hole_start: Option<TextSize> = None;
    for token in root
        .descendants_with_tokens()
        .filter_map(|e| e.into_token())
    {
        match token.kind() {
            SyntaxKind::DOC_MARGIN => {
                hole_start.get_or_insert(token.text_range().start());
            }
            SyntaxKind::NEWLINE => {
                if let Some(start) = hole_start.take() {
                    holes.push(TextRange::new(start, token.text_range().end()));
                }
            }
            _ => {}
        }
    }
    if let Some(start) = hole_start.take() {
        holes.push(TextRange::new(start, root.text_range().end()));
    }
    subtract_holes(regions, &holes)
}

/// Remove every maximal run of two-or-more consecutive docstrip-guarded lines
/// (`%<…>…`) from the expl3 regions. A fully-guarded chunk (a docstrip release
/// block, `%<latexrelease>…%<latexrelease>\EndIncludeInRelease`, or any run of
/// `%<*name>`/`%<name>` lines) pins **every** line to column 0 by its guard, so
/// the expl3 block layout cannot own it: reflowing indents a delimiter or wraps a
/// long line off its guard, stranding code onto an *unguarded* line — a docstrip
/// meaning change (the code leaves its guard's scope) that also re-parses
/// differently, so the layout never reaches a fixed point (issue #72, latex2e
/// `ltcmdhooks.dtx`). Handing such a run to the generic (non-expl3) lowering
/// preserves it verbatim, which is idempotent and meaning-preserving.
///
/// The two-line threshold keeps an *isolated* guarded line (a lone `%<trace> …`
/// statement amid unguarded code, or a lone `%<*name>` block marker) in-region,
/// where it lays out on its own line as before — such a line's content stays on
/// its line and never strands. A no-op for non-`.dtx` documents (no `GUARD`
/// tokens).
fn subtract_guarded_line_runs(root: &SyntaxNode, regions: Vec<TextRange>) -> Vec<TextRange> {
    if regions.is_empty() {
        return regions;
    }
    // One pass over the leaves grouping guard-led source lines into runs: a line
    // is guard-led when its first token is a `GUARD`. A run of two or more
    // adjacent guard-led lines becomes one hole spanning them (each line reaches
    // through its terminating newline).
    let mut holes: Vec<TextRange> = Vec::new();
    let mut at_line_start = true;
    let mut line_is_guard = false;
    let mut cur_line_start = TextSize::new(0);
    // The run of consecutive guard-led lines in progress: (start, end, line count).
    let mut run: Option<(TextSize, TextSize, usize)> = None;
    fn flush(run: &mut Option<(TextSize, TextSize, usize)>, holes: &mut Vec<TextRange>) {
        if let Some((start, end, count)) = run.take()
            && count >= 2
        {
            holes.push(TextRange::new(start, end));
        }
    }
    for token in root
        .descendants_with_tokens()
        .filter_map(|e| e.into_token())
    {
        if at_line_start {
            cur_line_start = token.text_range().start();
            line_is_guard = token.kind() == SyntaxKind::GUARD;
            at_line_start = false;
        }
        if token.kind() == SyntaxKind::NEWLINE {
            let line_end = token.text_range().end();
            if line_is_guard {
                match run {
                    // Adjacent to the run in progress (its end abuts this line's
                    // start): extend it.
                    Some((start, end, count)) if end == cur_line_start => {
                        run = Some((start, line_end, count + 1));
                    }
                    // A gap (a non-guarded line intervened) or the first guarded
                    // line: close any prior run and open a fresh one.
                    _ => {
                        flush(&mut run, &mut holes);
                        run = Some((cur_line_start, line_end, 1));
                    }
                }
            } else {
                flush(&mut run, &mut holes);
            }
            at_line_start = true;
        }
    }
    flush(&mut run, &mut holes);
    subtract_holes(regions, &holes)
}

/// Interval subtraction of `holes` from `regions`. Both lists must be sorted and
/// pairwise disjoint; the output is too (the region binary-search lookups rely on
/// that). Shared by the two region-subtraction passes above.
fn subtract_holes(regions: Vec<TextRange>, holes: &[TextRange]) -> Vec<TextRange> {
    if holes.is_empty() {
        return regions;
    }
    let mut out = Vec::with_capacity(regions.len());
    let mut h = holes.iter().peekable();
    for region in regions {
        let mut cursor = region.start();
        while let Some(&&hole) = h.peek() {
            if hole.end() <= cursor {
                h.next();
                continue;
            }
            if hole.start() >= region.end() {
                break;
            }
            if hole.start() > cursor {
                out.push(TextRange::new(cursor, hole.start()));
            }
            cursor = cursor.max(hole.end());
            if cursor >= region.end() {
                break;
            }
            h.next();
        }
        if cursor < region.end() {
            out.push(TextRange::new(cursor, region.end()));
        }
    }
    out
}

/// The state threaded through every lowering call: the active [`WrapMode`] plus the
/// per-document [`Signatures`] overlay (scanned definitions over the built-in DB)
/// that [`lower_begin`] consults for environment arity. `Copy`, so it passes by
/// value like the bare `wrap` mode it replaced.
#[derive(Clone, Copy)]
struct LowerCtx<'a> {
    wrap: WrapMode,
    /// How far list-item continuation lines sit from the `\item` column.
    item_indent: ItemIndent,
    /// One structural indentation step, used by [`ItemIndent::Indent`].
    indent_width: usize,
    /// Display-math break policy, pre-resolved against `wrap` in
    /// [`format_root`] — never [`MathWrap::Auto`] here.
    math_wrap: MathWrap,
    /// Soft equilibrium line target for [`WrapMode::Stable`]. Already derived and
    /// clamped against the hard width by [`FormatStyle::stable_wrap_target`].
    stable_target: usize,
    signatures: Signatures<'a>,
    /// Sorted, non-overlapping byte ranges of the document's expl3 regions (see
    /// [`expl3_regions`]). Inside these, source whitespace is catcode-9 (ignored)
    /// and `~` is catcode-10 (a literal space), so the formatter lays out the code
    /// itself — regardless of [`WrapMode`]. Borrowed from a `Vec` owned by
    /// [`format_root`], exactly like `signatures`.
    expl3_regions: &'a [TextRange],
    /// Sorted, non-overlapping byte ranges the author turned layout off over
    /// (`% badness-format off`/`skip`/`skip-file` and the combined `% badness`
    /// family — see [`crate::directives`]). Content overlapping one of these is
    /// reproduced byte-for-byte instead of laid out. Borrowed from a `Vec` owned
    /// by [`format_root`], exactly like `expl3_regions`.
    suppressed: &'a [TextRange],
    /// The sentence-boundary profile (built-in language plus user no-break
    /// abbreviations) for the [`WrapMode::Sentence`]/[`WrapMode::Semantic`] modes.
    /// `Copy`, borrowing the merged slice owned by [`format_root`]. Never consulted
    /// under [`WrapMode::Reflow`]/[`WrapMode::Stable`]/[`WrapMode::Preserve`], so an
    /// English default (see [`SentenceOptions::default`]) is harmless there.
    profile: ResolvedProfile<'a>,
    /// Range-formatting emission filter. When `Some`, only document-level blocks
    /// overlapping this byte range are lowered; the rest are skipped and never
    /// produce IR (see [`lower_node`]). These are children of [`SyntaxKind::ROOT`]
    /// or direct body children of the canonical no-indent `document` environment.
    /// `None` (the default) lowers the whole document. Every selected block still
    /// lowers in full, at its real indent-0 context, so the formatter stays the sole
    /// authority on layout.
    range: Option<TextRange>,
    /// Memo for [`dtx_doc_paragraph_reflows_safely`]. The answer is needed twice
    /// per `.dtx` doc paragraph — once by the paragraph's own lowering, once by
    /// [`margin_floats_into_paragraph`] deciding whether the floated leading `%`
    /// may be dropped — and computing it means lowering the paragraph, so without
    /// the memo a nested document pays for it repeatedly. Borrowed from
    /// [`format_root`] like `expl3_regions`.
    dtx_reflow_cache: &'a DtxReflowCache,
    /// Cache the control-flow proof for each candidate algorithm environment;
    /// every paragraph and managed command can ask for the same body policy.
    algorithm2e_cache: &'a RefCell<HashMap<SyntaxNode, bool>>,
    /// Set while *probing* whether a `.dtx` doc paragraph reflows safely. The probe
    /// lowers the paragraph, and that lowering must not consult
    /// [`margin_floats_into_paragraph`] again — the two would recurse into each
    /// other, once per nesting level, until the stack runs out. Dropping the
    /// floated margin is an emission detail that cannot change whether the reflow
    /// escapes the margin, so the probe simply keeps it.
    dtx_margin_probe: bool,
    /// A structured `.dtx` documentation paragraph may normalize its margin
    /// frames, but nested inline constructs must not synthesize unmargined lines.
    preserve_dtx_nested_layout: bool,
    /// The current node is being lowered as virtual LaTeX from a fully margined
    /// `.dtx` documentation region. Its physical `DOC_MARGIN` tokens are omitted;
    /// one canonical margin is re-applied by the enclosing IR.
    in_dtx_doc_region: bool,
    /// The current node is part of a non-math alignment cell that must collapse
    /// to one source line. Lone trivia newlines soften to spaces even when parser
    /// attachment nests them inside a command; blank lines and structural block
    /// breaks remain forced and make the grid decline.
    in_alignment_cell: bool,
    /// The exact trailing `\\<newline>` control symbol whose newline is supplied
    /// by an enclosing block's closing frame. Keeping the token's backslash here
    /// while letting the existing structural [`Ir::hard_line`] spell its newline
    /// prevents a second, blank line before the closer (issue #141).
    absorbed_control_newline: Option<TextRange>,
    /// A statement-leading [`SyntaxKind::DOC_COMMENT`] already emitted outside
    /// the statement's hanging indent. Descending lowerers omit exactly this
    /// node while retaining their ordinary command or conditional layout.
    omitted_leading_comment: Option<TextRange>,
    /// Whether the document carries any `.dtx` documentation margin at all — the
    /// cheap short-circuit for the no-`.dtx` majority, so gates that would
    /// otherwise walk back to the start of a physical line
    /// ([`doc_margin_opens_line`]) cost nothing in an ordinary `.tex` file.
    /// Computed once in [`format_root`].
    is_dtx: bool,
}

/// Memoized [`dtx_doc_paragraph_reflows_safely`] answers, keyed by paragraph node.
type DtxReflowCache = RefCell<HashMap<SyntaxNode, bool>>;

impl<'a> LowerCtx<'a> {
    /// Whether the active wrap mode lays out prose paragraphs at all (as opposed to
    /// [`WrapMode::Preserve`], which leaves authored breaks untouched). Reflow,
    /// sentence, and semantic all route prose through [`reflow_elements`]; the mode
    /// then decides how a completed run is rendered (width fill vs. sentences).
    fn wraps_prose(self) -> bool {
        matches!(
            self.wrap,
            WrapMode::Reflow | WrapMode::Stable | WrapMode::Sentence | WrapMode::Semantic
        )
    }

    /// Mark a body-final `\\<newline>` for absorption into its closing frame.
    /// If this body has no such token, preserve an outer body's marker while
    /// recursively lowering its children.
    fn absorbing_trailing_control_newline(self, body: &[SyntaxElement]) -> Self {
        let Some(token) = trailing_control_newline(body) else {
            return self;
        };
        Self {
            absorbed_control_newline: Some(token.text_range()),
            ..self
        }
    }

    fn absorbs_control_newline(self, token: &SyntaxToken) -> bool {
        self.absorbed_control_newline == Some(token.text_range())
    }

    /// Whether the document has any expl3 region at all — the cheap short-circuit
    /// for the no-expl3 majority (the slice is empty, so every query is free).
    fn any_expl3(self) -> bool {
        !self.expl3_regions.is_empty()
    }

    /// Whether byte offset `at` falls inside some expl3 region. O(log n) over the
    /// sorted, disjoint range list.
    fn in_expl3_region(self, at: TextSize) -> bool {
        self.expl3_regions
            .binary_search_by(|r| {
                use std::cmp::Ordering;
                if at < r.start() {
                    Ordering::Greater
                } else if at >= r.end() {
                    Ordering::Less
                } else {
                    Ordering::Equal
                }
            })
            .is_ok()
    }

    /// Whether `range` intersects some expl3 region (used to route a paragraph that
    /// is wholly or partly in-region).
    fn overlaps_expl3(self, range: TextRange) -> bool {
        self.expl3_regions
            .iter()
            .any(|r| r.start() < range.end() && range.start() < r.end())
    }

    /// Whether `range` lies wholly inside a directive-suppressed span, so
    /// whatever occupies it must be reproduced byte-for-byte instead of laid out.
    ///
    /// **Containment, not overlap**, and the difference is the whole granularity
    /// story. An `off`/`on` region is delimited by comments the author placed,
    /// not by CST boundaries, so it can begin halfway through a construct — and
    /// every construct it begins inside is an *ancestor* of the content it means
    /// to cover. Overlap would therefore suppress the outermost such ancestor:
    /// one directive anywhere in a document body suppresses the whole
    /// `document` environment, and with it the entire file. Containment picks
    /// the outermost node that fits *within* the region instead, so an ancestor
    /// merely straddling the boundary keeps descending and only the blocks the
    /// author actually enclosed are reproduced.
    ///
    /// A node straddling the boundary is laid out normally while its wholly
    /// enclosed children are still reproduced. That is a finer granularity than
    /// ruff's statement level, and it stays sound in the direction that matters:
    /// every byte the author enclosed is preserved.
    fn suppressed(self, range: TextRange) -> bool {
        self.suppressed.iter().any(|r| r.contains_range(range))
    }
}

/// Lower a CST node to IR. Most nodes lower generically (see
/// [`lower_element_stream`]); an [`SyntaxKind::ENVIRONMENT`] is special-cased to
/// indent its body (see [`lower_environment`]), and under each prose-wrapping mode a
/// [`SyntaxKind::PARAGRAPH`] is routed through its line policy (see
/// [`lower_paragraph_reflow`]). The [`LowerCtx`] (wrap mode + signature overlay) is
/// threaded through so it reaches every nested paragraph (including environment and
/// group bodies).
fn lower_node(node: &SyntaxNode, cx: LowerCtx<'_>) -> Ir {
    // Directive-suppressed content is reproduced, never laid out. Checked first,
    // above every routing decision, so no arm below can claim a node the author
    // turned the formatter off over.
    //
    // `ROOT` is excluded because a whole-file `skip-file` covers it: suppressing
    // there would emit the document as one opaque blob, which is the right bytes
    // by luck but skips the emission filter a range format depends on. Excluded,
    // the same directive reaches every *child* instead, and both the full and the
    // ranged path fall out of the one mechanism.
    if node.kind() != SyntaxKind::ROOT && cx.suppressed(node.text_range()) {
        return Ir::verbatim(node.text().to_string());
    }
    if node.kind() == SyntaxKind::DOC_COMMENT
        && cx.omitted_leading_comment == Some(node.text_range())
    {
        return Ir::Nil;
    }
    if cx.is_dtx
        && node.kind() != SyntaxKind::ROOT
        && !inside_macrocode(node)
        && contains_indented_dtx_comment(node)
    {
        let padding = leading_indented_dtx_comment_padding(node)
            .map(Ir::verbatim)
            .unwrap_or(Ir::Nil);
        return Ir::concat([padding, Ir::verbatim(node.text().to_string())]);
    }
    if dtx_doc_region(node, cx) {
        let virtual_doc = LowerCtx {
            in_dtx_doc_region: true,
            preserve_dtx_nested_layout: false,
            ..cx
        };
        return Ir::doc_margin(lower_node(node, virtual_doc));
    }
    if cx.is_dtx
        && node.kind() == SyntaxKind::ENVIRONMENT
        && doc_margin_opens_line(node, cx)
        && (environment_begin_has_newline(node) || !is_margin_framed(node))
    {
        return Ir::verbatim(node.text().to_string());
    }
    if cx.preserve_dtx_nested_layout
        && matches!(
            node.kind(),
            SyntaxKind::COMMAND
                | SyntaxKind::ENVIRONMENT
                | SyntaxKind::GROUP
                | SyntaxKind::OPTIONAL
                | SyntaxKind::INLINE_MATH
                | SyntaxKind::DISPLAY_MATH
                | SyntaxKind::MATH
        )
    {
        return Ir::verbatim(node.text().to_string());
    }
    // A `.dtx` command that opens on a docstrip guard and absorbs later guard
    // tokens is one fully guarded physical-line construct.  Its first guard is
    // a sibling (the command range starts at the control sequence), while the
    // continuation guards are children attached by the parser.  Relaying the
    // node would therefore join those child guards to the preceding tokens and
    // strand the continuations on unguarded lines.  Preserve the command as one
    // opaque slice; an unguarded command with guarded arguments still takes the
    // ordinary expl3 path and keeps its width-driven layout.
    if node.kind() == SyntaxKind::COMMAND
        && doc_margin_opens_line(node, cx)
        && node
            .descendants_with_tokens()
            .filter_map(|e| e.into_token())
            .any(|t| t.kind() == SyntaxKind::GUARD)
    {
        return Ir::verbatim(node.text().to_string());
    }
    // A guard-led paragraph can carry later guards as children while the first
    // guard remains a sibling. Reflowing that mixed shape treats the first
    // command as ordinary prose, then emits the next column-zero guard without
    // first closing the line, joining two docstrip variants. Preserve the whole
    // paragraph whenever its opening line and a continuation are guarded.
    if node.kind() == SyntaxKind::PARAGRAPH
        && doc_margin_opens_line(node, cx)
        && node
            .first_token()
            .is_some_and(|token| token.kind() == SyntaxKind::CONTROL_WORD)
        && node
            .descendants_with_tokens()
            .filter_map(|e| e.into_token())
            .any(|t| t.kind() == SyntaxKind::GUARD)
    {
        return Ir::verbatim(node.text().to_string());
    }
    // A fully docstrip-guarded paragraph is the whole hole cut out of an
    // expl3 region. With no byte of the paragraph left in-region it never
    // reaches `lower_expl_paragraph`; the generic Preserve paragraph path
    // would otherwise normalize its whitespace and move guards off column 0.
    if node.kind() == SyntaxKind::PARAGRAPH {
        let text = node.text().to_string();
        if text_is_fully_guarded(&text) {
            // The surrounding environment owns the body-leading break; keeping
            // the paragraph's leading newline inside an opaque IR would give
            // the printer two competing boundary breaks and float the first
            // guard onto the frame line.
            return Ir::verbatim(text.trim_start_matches(['\r', '\n']));
        }
    }
    // Range-formatting emission filter: at the document root, lower only the
    // children (top-level blocks plus the trivia between them) overlapping the
    // requested range; skip the rest entirely. A canonical `document` environment
    // is transparent when the range lies wholly in its body: that body's layout is
    // deliberately flush with the root, so its direct children are equally safe
    // independent blocks. Other environments still lower in full. A `None` range
    // (the whole-document default) never reaches here.
    if let Some(range) = cx.range
        && node.kind() == SyntaxKind::ROOT
    {
        let mut filtered = Vec::new();
        for element in node
            .children_with_tokens()
            .filter(|el| ranges_overlap(range, el.text_range()))
        {
            if let SyntaxElement::Node(child) = &element
                && document_body_contains(child, range)
            {
                filtered.extend(
                    child
                        .children_with_tokens()
                        .filter(|el| ranges_overlap(range, el.text_range())),
                );
            } else {
                filtered.push(element);
            }
        }
        return Ir::concat(lower_element_stream(filtered.into_iter(), cx));
    }
    // expl3 code layout (catcode-9 whitespace / catcode-10 `~`) applies regardless
    // of `WrapMode`, so it is checked before the wrap-gated arms below. A paragraph
    // overlapping a region is split at the toggles; a brace/optional group inside a
    // region lays out its body as expl3 code.
    if cx.any_expl3() {
        match node.kind() {
            SyntaxKind::PARAGRAPH if cx.overlaps_expl3(node.text_range()) => {
                return lower_expl_paragraph(node, cx);
            }
            SyntaxKind::GROUP if cx.in_expl3_region(node.text_range().start()) => {
                return lower_expl_group(node, SyntaxKind::L_BRACE, SyntaxKind::R_BRACE, cx);
            }
            SyntaxKind::OPTIONAL if cx.in_expl3_region(node.text_range().start()) => {
                return lower_expl_group(node, SyntaxKind::L_BRACKET, SyntaxKind::R_BRACKET, cx);
            }
            // A command and its greedily-attached `{…}`/`[…]` arguments lay out as a
            // fill so the arguments break independently (only an over-long one
            // detonates) rather than the generic concat breaking every group.
            SyntaxKind::COMMAND if cx.in_expl3_region(node.text_range().start()) => {
                // `Statements::Ignore`: within one command's attached arguments a
                // source newline is just catcode-9 whitespace, not a statement
                // boundary — the width fill alone decides the breaks. Otherwise a
                // fill-broken argument would read as a new statement on the next
                // pass and the layout would never reach a fixed point.
                let fill = lower_expl_code(node.children_with_tokens(), cx, Statements::Ignore);
                // A recognized conditional renders **all-or-nothing**: flat when the
                // whole call fits, else the R4/R5 explosion. Left to the fill above,
                // the branch groups are independent atoms, so an overflow hangs only
                // the last one and splits the branch list across two indents —
                // `\…:nTF {c} {T}` / `{F}`, which reads as a continuation of the
                // enclosing statement rather than as the false branch.
                //
                // Attached to the *node*, not to statement position. The two
                // position-keyed paths in `lower_expl_code` (statement-leading,
                // trailing) additionally join the head, but both are gated off inside
                // a *fallback* statement — deliberately, since whether a conditional
                // name sits trailing there depends on where the line's junk ends,
                // which is not pass-invariant (xtemplate's spliced
                // `cs_ \str_if_eq:nnT … set:Npn` name assembly). The node is the node
                // on every pass, so this carries no such question and covers the
                // fallback case the other two decline.
                if let Some(exploded) = command_name(node)
                    .and_then(|name| expl3::conditional_branches(&name))
                    .and_then(|n| lower_expl_conditional(node, cx, n))
                {
                    return Ir::group(Ir::if_break(fill, exploded));
                }
                return fill;
            }
            _ => {}
        }
    }
    match node.kind() {
        // A `.dtx` documentation-layer prose paragraph (its first content token is
        // a `DOC_MARGIN`): reflow the bare prose and re-emit a `% ` margin on each
        // wrapped line. Checked before the generic paragraph reflow so the margin
        // is stripped and re-synthesized rather than glued into the fill.
        SyntaxKind::PARAGRAPH
            if !cx.in_dtx_doc_region && cx.wraps_prose() && is_dtx_doc_paragraph(node) =>
        {
            return lower_dtx_doc_paragraph(node, cx);
        }
        SyntaxKind::PARAGRAPH
            if cx.wraps_prose() || in_algorithm2e_env(node, cx) && !is_dtx_doc_paragraph(node) =>
        {
            return lower_paragraph_reflow(node, cx);
        }
        // Under `Preserve` a (non-`.dtx`) prose paragraph keeps its authored line
        // breaks, but inter-word spacing on each line still normalizes to a single
        // space (see [`lower_prose_stream`]) — `Preserve` governs line breaks only.
        // Inline-prose command bodies (`\emph{…}`) are flattened in so their text
        // collapses too, matching every wrapping mode; opaque argument bodies (a
        // `\newcommand` definition) recurse through [`lower_node`] and stay verbatim.
        // A `.dtx` doc paragraph is excluded (the arm above requires `wraps_prose`),
        // so it falls through to the generic stream and keeps its `%` margins.
        SyntaxKind::PARAGRAPH if cx.wrap == WrapMode::Preserve && !is_dtx_doc_paragraph(node) => {
            let flat = flatten_inline_prose(
                flatten_statements(node.children_with_tokens().collect()),
                cx,
                false,
            );
            return Ir::concat(lower_prose_stream(flat.into_iter(), cx));
        }
        // A `.dtx` docstrip frame (`%␣␣␣␣\begin{macrocode}`, a documentation-layer
        // `% \begin{itemize}`): the body is never indented and the closing frame is
        // kept whole at column 0. Routed before the alignment/list lowerers so a
        // margin-framed environment never reaches a layout that would reindent its
        // frame margins. A *math* environment is excluded: a `bmatrix`/`array` whose
        // `\begin` happens to open a `%␣␣␣␣` line inside a `% \[…\]` doc-math block
        // (l3backend-draw.dtx, l3ldb.dtx) is doc-comment prose, not a docstrip frame —
        // margin-framing it re-breaks `\end{bmatrix}` off its `%` margin and leaves the
        // block's `\]` unparseable on pass 2. It falls through to the generic stream,
        // which keeps the authored margins verbatim (same margin rule as the math /
        // group / optional arms below).
        SyntaxKind::ENVIRONMENT
            if !cx.in_dtx_doc_region
                && !has_verbatim_body(node)
                && is_margin_framed(node)
                && !is_math_env(node, cx) =>
        {
            return lower_margin_framed_environment(node, cx);
        }
        // A named math environment (`equation`, `align`, `gather`, matrix, …) — its
        // body is a `MATH` node (the parser entered math mode). Checked before the
        // generic alignment arm so a math *grid* (`align`/`pmatrix`, both `math` and
        // `align`) takes the math-aware path; a non-math grid (`tabular`,
        // `align` but not `math`) still falls through to `lower_aligned_environment`.
        // The `contains_doc_margin` gate is the same margin rule as the generic
        // arm below: a `bmatrix` nested in a `% \[…\]` doc-math block (l3backend-draw.dtx)
        // must keep its `%` margins verbatim, or the re-broken `\end{bmatrix}` drops
        // off column 0 and the block's closing `\]` is unparseable on pass 2.
        SyntaxKind::ENVIRONMENT
            if !has_verbatim_body(node)
                && !cx.in_dtx_doc_region
                && is_math_env(node, cx)
                && !contains_doc_margin(node, cx) =>
        {
            return lower_math_environment(node, cx);
        }
        // A grid inside a fully owned virtual `.dtx` documentation region lays
        // out after its physical margins have been stripped. The enclosing
        // `Ir::doc_margin` then restores the prefix at column zero, outside the
        // grid's padding. Other margin-carrying grids still decline through
        // `contains_doc_margin`.
        SyntaxKind::ENVIRONMENT
            if !has_verbatim_body(node)
                && is_alignment_env(node, cx)
                && !contains_doc_margin(node, cx) =>
        {
            return lower_aligned_environment(node, cx);
        }
        // A list environment (`itemize`/`enumerate`/`description`): its `\item`s get
        // their continuation lines hanging-indented under the marker. Under a
        // prose-wrapping mode the body is reflowed; under `Preserve` the authored
        // breaks and inner spacing are kept byte-faithful and only the continuation
        // *indentation* is re-hung (see [`lower_item_chunks`]). A doc-margined list
        // under `Preserve` is excluded — like the generic arm below, its `%` margins
        // must stay pinned at column 0 — so it falls through to the generic stream.
        SyntaxKind::ENVIRONMENT
            if !has_verbatim_body(node)
                && is_list_env(node, cx)
                && (cx.wraps_prose() || !contains_doc_margin(node, cx)) =>
        {
            return lower_list_environment(node, cx);
        }
        // A user-defined or otherwise unclassified environment whose body carries a
        // top-level `&` reads as an alignment (`myaligned`, issue #84): `&` at
        // catcode 4 is a column tab, a static CST-shape fact. Known align/math/list
        // environments were routed by the arms above; this generalizes `&`-column
        // layout to the environments the signature DB cannot name, exactly as the
        // environment group-boundary gate generalizes the curated definition-body set
        // Whitespace-only (the grid renderer reflows only trivia) and
        // self-correcting: any shape the grid cannot lay out falls back to
        // [`lower_environment`]. Doc-margined bodies are excluded (same margin rule as
        // the arms above and below), except inside a fully owned virtual region,
        // where `contains_doc_margin` is false and framing is stripped before grid
        // layout.
        SyntaxKind::ENVIRONMENT
            if !has_verbatim_body(node)
                && !contains_doc_margin(node, cx)
                && body_has_top_level_ampersand(node) =>
        {
            return lower_aligned_environment(node, cx);
        }
        // Same margin rule as the math/group/optional arms below: an environment
        // continuing across `.dtx` doc-margined lines is never re-laid. A
        // *margin-framed* environment (its `\begin`/`\end` on `%` frame lines) took
        // the `is_margin_framed` arm above; what reaches here is an environment
        // merely *nested* in doc-margined prose — an `array` inside a `% \[…\]`
        // display-math block (l3color.dtx). Re-breaking its `\begin`/body/`\end`
        // onto fresh lines would push `\end{array}` off its `%` margin, a meaning
        // change (a column-0 line stops being a comment at package-load time) that
        // leaves the orphaned `\]` unparseable on pass 2. The generic stream keeps
        // the authored margins verbatim.
        SyntaxKind::ENVIRONMENT if !has_verbatim_body(node) && !contains_doc_margin(node, cx) => {
            return lower_environment(node, cx);
        }
        // Same margin rule as the environment arm: a conditional spanning `.dtx`
        // doc-margined lines is never re-laid, since moving a divider off its `%`
        // margin is a meaning change. The generic stream keeps margins pinned.
        SyntaxKind::CONDITIONAL if !contains_doc_margin(node, cx) => {
            return lower_conditional(node, cx);
        }
        // Same margin rule as the environment/math/group/optional arms: a command
        // whose argument continues across `.dtx` doc-margined lines
        // (`% \title{^^A\n%   …}`) is never re-laid. Reflowing a managed argument
        // breaks its body onto fresh lines, which drops the `%` margin — and on an
        // unmargined line a `^^A` doc comment re-lexes as content, so the layout
        // stops being whitespace-only and pass 2 no longer parses. The generic
        // stream keeps the authored margins verbatim.
        SyntaxKind::COMMAND
            if (command_has_math_arg(node, cx)
                || (cx.wraps_prose() || in_algorithm2e_env(node, cx))
                    && command_has_managed_arg(node, cx))
                && !contains_doc_margin(node, cx) =>
        {
            return lower_command(node, cx);
        }
        // Like the multi-line group below, math continuing across ordinary `.dtx`
        // doc-margined lines is never re-laid: math relayout would move a `%`
        // margin off column 0. A virtual doc region is different—its shared CST
        // view strips physical framing before specialized math lowering, and the
        // region wrapper regenerates the margins afterward.
        SyntaxKind::INLINE_MATH if !contains_doc_margin(node, cx) => {
            return lower_math(node, cx);
        }
        SyntaxKind::DISPLAY_MATH if !contains_doc_margin(node, cx) => {
            return lower_display_math(node, cx);
        }
        // A bare `MATH` node inside a virtual environment may be a grid cell;
        // its enclosing grid owns separators and row boundaries. Complete
        // inline/display nodes enter their math lowerers through the two arms
        // above and do not need this fallback.
        SyntaxKind::MATH if !cx.in_dtx_doc_region && !contains_doc_margin(node, cx) => {
            return lower_math_body(node, cx);
        }
        // A `.dtx` doc-layer group continuing across margined lines
        // (`\changes{…}{…\n%  …}`) is excluded: re-laying it out would move
        // content off its `%` margin — a meaning change (the line stops being a
        // comment at package-load time). Such a group falls through to the
        // generic stream, which keeps the authored margins verbatim.
        SyntaxKind::GROUP if !contains_doc_margin(node, cx) => {
            if opaque_group_has_glued_environment_sibling(node) {
                return Ir::verbatim(node.text().to_string());
            }
            if super::beamer::is_body_group(node) {
                return lower_beamer_body(node, cx);
            }
            // Width-driven Opaque layout under the default mode: block-vs-inline
            // is decided by width, content, and preserved predicates — never by
            // whether the author happened to break the line. A group *opening
            // on* a doc-margined line holds no margin token of its own, so it
            // stays on the residue path below (a width break would land content
            // off its margin — the same gate `lower_optional` carries).
            if matches!(cx.wrap, WrapMode::Reflow) && !doc_margin_opens_line(node, cx) {
                return lower_opaque_group(node, cx);
            }
            // Tier-2 residue (the non-`Reflow` modes and the margined-line
            // corner): the pre-existing behaviour, byte for byte — block form
            // when the author broke the line, the generic inline stream below
            // otherwise. Fixed-point argument on [`spans_multiple_lines`].
            if spans_multiple_lines(node) {
                return lower_bracketed(node, SyntaxKind::L_BRACE, SyntaxKind::R_BRACE, cx, false);
            }
        }
        // Same margin rule as the group above: a `[…]` continuing across
        // doc-margined lines keeps its authored margins.
        // No signature context on the generic path, so no keyval proof: only gaps
        // the author already wrote are break opportunities.
        SyntaxKind::OPTIONAL if !contains_doc_margin(node, cx) => {
            if let Some(ir) = lower_optional(node, cx, false) {
                return ir;
            }
        }
        SyntaxKind::ROOT => return lower_root(node, cx),
        _ => {}
    }
    Ir::concat(lower_element_stream(node.children_with_tokens(), cx))
}

/// Lower a complete document, discarding indentation before its first top-level
/// block. Indentation after a newline is normally absorbed into that newline's
/// [`Gap`], but the first physical line has no preceding boundary to own it, so
/// its padding otherwise survives as verbatim text. Only direct `ROOT` trivia is
/// removed—leading whitespace inside a paragraph remains under that paragraph's
/// wrap policy—and a formatter-suppressed prefix stays byte-exact.
fn lower_root(node: &SyntaxNode, cx: LowerCtx<'_>) -> Ir {
    let mut elements = node.children_with_tokens().peekable();
    while let Some(SyntaxElement::Token(token)) = elements.peek() {
        if token.kind() != SyntaxKind::WHITESPACE || cx.suppressed(token.text_range()) {
            break;
        }
        elements.next();
    }
    Ir::concat(lower_element_stream(elements, cx))
}

/// Lower a [`SyntaxKind::PARAGRAPH`] under [`WrapMode::Reflow`]: greedily wrap its
/// prose to the line width. Maximal runs of *adjacent* non-whitespace elements
/// glue into one unbreakable *atom* (so `Hello,` and `\emph{x}` never split);
/// inter-word whitespace — or a lone newline, since a paragraph holds no blank
/// lines — is a break opportunity. The run lowers to an [`Ir::fill`], which the
/// printer wraps word-by-word.
///
/// Three things end a line rather than flow into the fill: an explicit `\\` line
/// break (a [`SyntaxKind::LINE_BREAK`] node — the parser groups `\\` with its
/// `*` / `[len]` so the whole unit stays on one line), a `%` comment (which must
/// terminate its line), and a nested *block* (an environment or multi-line group
/// whose IR carries a forced break). Each emits the run-so-far as a fill, then
/// the line breaks; a fresh run continues after. The paragraph's lines are joined
/// by [`Ir::hard_line`].
///
/// A paragraph in a `statementBody` environment is *not* prose and takes
/// [`ReflowKind::Statement`] instead — see [`in_statement_body_env`].
fn lower_paragraph_reflow(node: &SyntaxNode, cx: LowerCtx<'_>) -> Ir {
    reflow_elements(
        node.children_with_tokens(),
        cx,
        paragraph_reflow_kind(node, cx),
    )
}

/// Choose the reflow policy from the paragraph’s enclosing environment.
/// Shared by [`lower_paragraph_reflow`] and the
/// `\begin`-tail splice in [`lower_env_body`], so a header the greedy parser
/// over-attached lays out under the same rule as the body it is spliced into.
fn paragraph_reflow_kind(node: &SyntaxNode, cx: LowerCtx<'_>) -> ReflowKind {
    if in_algorithm2e_env(node, cx) {
        ReflowKind::Algorithm2e
    } else if in_statement_body_env(node, cx) {
        ReflowKind::Statement
    } else if node.parent().is_some_and(|parent| {
        parent.kind() == SyntaxKind::ENVIRONMENT
            && (is_alignment_env(&parent, cx) || body_has_top_level_ampersand(&parent))
    }) {
        ReflowKind::AlignmentBody
    } else {
        ReflowKind::Prose
    }
}

/// Whether `node` is a `.dtx` documentation-layer paragraph. A pure CST-shape
/// fact, like [`is_margin_framed`]: `DOC_MARGIN` exists only under the `.dtx` lexer
/// config, so this is unambiguous and always false elsewhere, and it needs no
/// signature lookup. Two shapes count:
/// - The first content token (skipping leading `WHITESPACE`/`NEWLINE` trivia) is a
///   `DOC_MARGIN` — the margin sits inside the paragraph (the first line of a doc
///   block, or a `% \item` body line opening after the `\begin{…}` break).
/// - The margin *floated out*: when a doc paragraph follows a `%` blank line, its
///   leading `%` is attached as inter-paragraph trivia, so the nearest preceding
///   token (skipping inline whitespace on the same line) is a `DOC_MARGIN`. This is
///   the common multi-paragraph case (see [`margin_floats_into_paragraph`], which
///   drops the floated margin so the reflow re-emits a canonical one).
///
/// A guard-led line (`%<…>`, a `GUARD` token) is *not* doc prose, so guards keep
/// their column-0 pin untouched.
fn is_dtx_doc_paragraph(node: &SyntaxNode) -> bool {
    // The paragraph's first content token, descending into child nodes: a
    // paragraph that *opens* with a command (`%<package>\def\x{1}` after a guard)
    // is not doc prose just because a later line carries a margin. Walking only
    // direct child tokens would skip the opening `COMMAND` and read that later
    // margin, wrapping guarded code in a `% ` margin that comments it out.
    let margin_inside = node
        .children_with_tokens()
        .filter_map(|e| e.into_token())
        .find(|t| !is_collapsible_trivia(t.kind()))
        .is_some_and(|t| t.kind() == SyntaxKind::DOC_MARGIN);
    margin_inside
        || node
            .first_token()
            .is_some_and(|t| margin_precedes_on_line(&t))
}

/// Whether a `.dtx` doc paragraph's *first* content token sits on a margined line —
/// either it is the `DOC_MARGIN` itself, or one precedes it on its line.
///
/// [`is_dtx_doc_paragraph`] is deliberately looser: it accepts a paragraph whose
/// margin appears on any later line, because such a paragraph *is* documentation
/// and its margins must still be respected. But the `DtxProse` reflow re-emits a
/// canonical `% ` on *every* line it produces, so a paragraph whose first line is
/// unmargined would gain a `%` it never had — turning code into a comment
/// (`%<package>\def\x{1}`, where the paragraph opens after a guard). Such a
/// paragraph takes the byte-faithful stream instead.
fn dtx_paragraph_starts_margined(node: &SyntaxNode) -> bool {
    node.descendants_with_tokens()
        .filter_map(|e| e.into_token())
        .find(|t| !is_collapsible_trivia(t.kind()))
        .is_some_and(|t| t.kind() == SyntaxKind::DOC_MARGIN || margin_precedes_on_line(&t))
}

/// Whether `node` is a complete, fully margined `.dtx` documentation block that
/// can be formatted as ordinary virtual LaTeX. The physical `%` prefixes are
/// trivia in the CST, but every generated line must regain one; admitting only a
/// line-owning block makes that prefix scope exact.
fn dtx_doc_region(node: &SyntaxNode, cx: LowerCtx<'_>) -> bool {
    if !cx.is_dtx
        || cx.in_dtx_doc_region
        || node.kind() != SyntaxKind::ENVIRONMENT
        || has_verbatim_body(node)
        || node
            .descendants()
            .filter_map(Environment::cast)
            .any(|env| matches!(env.name().as_deref(), Some("verbatim" | "verbatim*")))
        || node.text().to_string().contains("\\begin{verbatim")
        || node
            .descendants()
            .filter(|child| child.kind() == SyntaxKind::BEGIN)
            .any(|begin| {
                begin.descendants_with_tokens().any(|element| {
                    element
                        .into_token()
                        .is_some_and(|token| token.kind() == SyntaxKind::NEWLINE)
                })
            })
        || node.descendants_with_tokens().any(|element| {
            element.into_token().is_some_and(|token| {
                matches!(token.kind(), SyntaxKind::GUARD | SyntaxKind::COMMENT)
            })
        })
        || Environment::cast(node.clone()).is_some_and(|environment| {
            matches!(
                environment.name().as_deref(),
                Some("macrocode" | "macrocode*")
            )
        })
    {
        return false;
    }

    let Some(first) = node.first_token() else {
        return false;
    };
    let first_is_margined =
        first.kind() == SyntaxKind::DOC_MARGIN || line_prefix_is_doc_margin(first.prev_token());
    if !first_is_margined {
        return false;
    }

    // The node must own the rest of its closing line. Otherwise a break inside
    // the region could leave following documentation outside the prefix scope.
    let mut next = node.last_token().and_then(|token| token.next_token());
    while let Some(token) = next {
        match token.kind() {
            SyntaxKind::WHITESPACE => next = token.next_token(),
            SyntaxKind::NEWLINE => break,
            _ => return false,
        }
    }

    // Every continuation line must carry its own physical margin in the source.
    // This excludes mixed doc/code constructs and macrocode bodies without
    // needing a semantic guess about where their layers change.
    let tokens: Vec<SyntaxToken> = node
        .descendants_with_tokens()
        .filter_map(SyntaxElement::into_token)
        .collect();
    tokens.iter().enumerate().all(|(index, token)| {
        token.kind() != SyntaxKind::NEWLINE
            || tokens
                .get(index + 1)
                .is_some_and(|next| next.kind() == SyntaxKind::DOC_MARGIN)
    })
}

fn environment_begin_has_newline(node: &SyntaxNode) -> bool {
    Environment::cast(node.clone())
        .and_then(|environment| environment.begin())
        .is_some_and(|begin| {
            begin.syntax().descendants_with_tokens().any(|element| {
                element
                    .into_token()
                    .is_some_and(|token| token.kind() == SyntaxKind::NEWLINE)
            })
        })
}

/// Whether the start of the current physical line, walking backward over only
/// source padding, is a documentation margin.
fn line_prefix_is_doc_margin(mut token: Option<SyntaxToken>) -> bool {
    while let Some(current) = token {
        match current.kind() {
            SyntaxKind::WHITESPACE => token = current.prev_token(),
            SyntaxKind::DOC_MARGIN => return true,
            _ => return false,
        }
    }
    false
}

/// Whether `margin` is the prefix immediately preceding a virtual documentation
/// region. The region IR re-emits the canonical margin, so this source prefix and
/// its padding must be omitted.
fn margin_starts_dtx_doc_region(margin: &SyntaxToken, cx: LowerCtx<'_>) -> bool {
    if margin.kind() != SyntaxKind::DOC_MARGIN || cx.in_dtx_doc_region {
        return false;
    }
    let mut next = margin.next_sibling_or_token();
    while let Some(SyntaxElement::Token(token)) = &next {
        if token.kind() == SyntaxKind::WHITESPACE {
            next = token.next_sibling_or_token();
        } else {
            break;
        }
    }
    let Some(SyntaxElement::Node(node)) = next else {
        return false;
    };
    if dtx_doc_region(&node, cx) {
        return true;
    }
    // A blank doc line ends the preceding paragraph, so the margin can be a
    // root sibling of a paragraph that starts with the virtual environment.
    // The same paragraph may continue with prose after `\end{...}`; that later
    // content does not change ownership of the opener's physical margin.
    if node.kind() != SyntaxKind::PARAGRAPH {
        return false;
    }
    node.children_with_tokens()
        .find(|element| {
            !matches!(
                element,
                SyntaxElement::Token(token) if is_collapsible_trivia(token.kind())
            )
        })
        .and_then(SyntaxElement::into_node)
        .is_some_and(|child| dtx_doc_region(&child, cx))
}

/// Whether `comment` is an ordinary own-line comment kept out of column zero by
/// authored indentation. In `.dtx`, removing that indentation changes even a
/// bare `%` from `COMMENT` to `DOC_MARGIN`, so the enclosing layout unit is
/// opaque.
fn is_indented_dtx_comment(comment: &SyntaxToken) -> bool {
    comment.kind() == SyntaxKind::COMMENT
        && comment
            .prev_token()
            .filter(|previous| previous.kind() == SyntaxKind::WHITESPACE)
            .and_then(|whitespace| whitespace.prev_token())
            .is_some_and(|previous| previous.kind() == SyntaxKind::NEWLINE)
}

fn contains_indented_dtx_comment(node: &SyntaxNode) -> bool {
    node.descendants_with_tokens()
        .filter_map(SyntaxElement::into_token)
        .any(|comment| is_indented_dtx_comment(&comment))
}

/// Recover the indentation token that sits just outside an opaque node beginning
/// with an indented `.dtx` comment. Generic gap lowering owns that token and would
/// otherwise erase it before the node's verbatim bytes are emitted.
fn leading_indented_dtx_comment_padding(node: &SyntaxNode) -> Option<String> {
    let comment = node.first_token()?;
    if !is_indented_dtx_comment(&comment) {
        return None;
    }
    comment
        .prev_token()
        .filter(|previous| previous.kind() == SyntaxKind::WHITESPACE)
        .map(|whitespace| whitespace.text().to_string())
}

fn inside_macrocode(node: &SyntaxNode) -> bool {
    node.ancestors()
        .filter_map(Environment::cast)
        .any(|environment| {
            matches!(
                environment.name().as_deref(),
                Some("macrocode" | "macrocode*")
            )
        })
}

/// Whether the physical line `node` starts on opens with a `.dtx` documentation
/// margin or docstrip guard — i.e. everything on it is documentation (or guarded
/// code) that docstrip anchors at column 0.
///
/// Broader than [`margin_precedes_on_line`], which only accepts a margin
/// *immediately* before its token: here the margin may be arbitrarily far back
/// (`% \begin{function}[EXP, pTF]{…}`). A construct that introduces its own line
/// break must consult this, because a break it emits lands on a line the doc layer
/// never margined — turning documentation into live code. [`contains_doc_margin`]
/// cannot see this: the margin sits *outside* the node, before it on the line.
fn doc_margin_opens_line(node: &SyntaxNode, cx: LowerCtx<'_>) -> bool {
    if !cx.is_dtx || cx.in_dtx_doc_region {
        return false;
    }
    let mut prev = node.first_token().and_then(|t| t.prev_token());
    while let Some(t) = prev {
        match t.kind() {
            SyntaxKind::NEWLINE => return false,
            SyntaxKind::DOC_MARGIN | SyntaxKind::GUARD => return true,
            _ => prev = t.prev_token(),
        }
    }
    false
}

/// Whether the nearest token before `token`, skipping inline `WHITESPACE` on the
/// same line (stopping at any `NEWLINE` or other token), is a `DOC_MARGIN`: the
/// floated leading margin of a doc paragraph. Mirrors [`is_margin_framed`]'s
/// backward walk.
fn margin_precedes_on_line(token: &SyntaxToken) -> bool {
    let mut prev = token.prev_token();
    while let Some(t) = prev {
        match t.kind() {
            SyntaxKind::WHITESPACE => prev = t.prev_token(),
            SyntaxKind::DOC_MARGIN => return true,
            _ => return false,
        }
    }
    false
}

/// Whether `margin` is the floated leading `%` of a reflowable `.dtx` doc
/// paragraph: scanning forward over inline `WHITESPACE` (not a `NEWLINE`), the next
/// sibling is a `PARAGRAPH` that reflows. Such a margin is dropped during reflow
/// because the paragraph's own [`Ir::margin_prefix`] re-emits a canonical `% ` on
/// every line. A `%`-only blank line fails this (its margin is followed by a
/// newline), so it stays a column-0 separator.
fn margin_floats_into_paragraph(margin: &SyntaxToken, cx: LowerCtx<'_>) -> bool {
    let mut next = margin.next_sibling_or_token();
    while let Some(SyntaxElement::Token(t)) = &next {
        if t.kind() == SyntaxKind::WHITESPACE {
            next = t.next_sibling_or_token();
        } else {
            break;
        }
    }
    matches!(
        next,
        Some(SyntaxElement::Node(n))
            if n.kind() == SyntaxKind::PARAGRAPH
                && is_dtx_doc_paragraph(&n)
                && dtx_doc_paragraph_reflows_safely(&n, cx)
    )
}

/// Lower a `.dtx` documentation paragraph under [`WrapMode::Reflow`]. When the
/// paragraph is pure running prose ([`dtx_paragraph_reflows`]) the bare prose is
/// reflowed to the line width via [`reflow_elements`] in [`ReflowKind::DtxProse`]
/// mode, which drops each line's `%` margin and re-emits a canonical `% ` margin
/// on every reflowed line (see [`Ir::margin_prefix`]). A complete virtual
/// documentation environment may participate as a self-margin-owning block, so
/// prose on either side keeps reflowing; other paragraphs that contain or sit
/// inside an environment (a `macrocode` block or a `macro`/`environment` doc block)
/// are lowered *preserve-style* so frame margins and item lines round-trip
/// byte-for-byte.
///
/// [`dtx_paragraph_reflows`] is a cheap up-front gate; the exact one is the reflow
/// itself. A forced-break block whose interior lines ride their own margins is
/// committed raw under a canonical first-line margin
/// ([`LineBuilder::push_margined_block`]), and a clean guard line becomes its own
/// column-0 segment ([`collect_guard_line`]), so both reflow with the prose around
/// them. A paragraph whose reflow still commits content *outside* the `% ` margin
/// (a block with an unmargined interior line, a guard line that cannot be
/// isolated — see [`LineBuilder::margin_escaped`]) is re-lowered on the preserve
/// path instead: on an unmargined line a `.dtx` doc comment re-lexes as content,
/// so keeping that layout would break the whitespace-only invariant. The gate
/// reads content only, never [`LowerCtx::wrap`], so `--wrap reflow` on a `.dtx`
/// is exactly as safe as any other mode.
fn lower_dtx_doc_paragraph(node: &SyntaxNode, cx: LowerCtx<'_>) -> Ir {
    if dtx_doc_paragraph_reflows_safely(node, cx) {
        reflow_elements(node.children_with_tokens(), cx, ReflowKind::DtxProse)
    } else {
        // Margin frames still normalize, but nested inline constructs stay
        // opaque: a width break inside one would create an unmargined line.
        let preserve = LowerCtx {
            preserve_dtx_nested_layout: true,
            ..cx
        };
        Ir::concat(lower_element_stream(node.children_with_tokens(), preserve))
    }
}

/// Whether a `.dtx` documentation paragraph may be reflowed: it is unstructured
/// ([`dtx_paragraph_reflows`]) *and* the reflow keeps every line under the `% `
/// margin ([`LineBuilder::margin_escaped`]). The second half is exact rather than
/// syntactic, so it is answered by running the reflow and throwing the layout away.
///
/// [`margin_floats_into_paragraph`] needs the same answer — a floated leading `%`
/// may only be dropped when the paragraph really does re-emit a canonical margin —
/// so both go through here, memoized per node, and always under the *probing*
/// context so the two callers cannot disagree.
fn dtx_doc_paragraph_reflows_safely(node: &SyntaxNode, cx: LowerCtx<'_>) -> bool {
    if !dtx_paragraph_reflows(node, cx) || !dtx_paragraph_starts_margined(node) {
        return false;
    }
    if let Some(&answer) = cx.dtx_reflow_cache.borrow().get(node) {
        return answer;
    }
    // Answer every *nested* doc paragraph first, innermost-first (reverse
    // pre-order visits a node before its ancestors). The probe below lowers this
    // paragraph in full, so an unanswered nested paragraph would start a probe
    // inside a probe — doubling stack depth and duplicating work at every nesting
    // level. Warmed bottom-up, each nested probe is a cache hit instead.
    let nested: Vec<SyntaxNode> = node
        .descendants()
        .filter(|d| d != node && d.kind() == SyntaxKind::PARAGRAPH && is_dtx_doc_paragraph(d))
        .collect();
    for descendant in nested.into_iter().rev() {
        dtx_doc_paragraph_reflows_safely(&descendant, cx);
    }
    let probe = LowerCtx {
        dtx_margin_probe: true,
        ..cx
    };
    let (_, margin_escaped) =
        reflow_elements_checked(node.children_with_tokens(), probe, ReflowKind::DtxProse);
    let answer = !margin_escaped;
    cx.dtx_reflow_cache
        .borrow_mut()
        .insert(node.clone(), answer);
    answer
}

/// Whether a `.dtx` documentation paragraph has only structures that can reflow
/// under its canonical margin. A direct, fully margin-owned environment composes
/// as a self-owning block; an environment hidden inside another child, an unsafe
/// direct environment, or an enclosing environment keeps the paragraph on the
/// byte-faithful preserve path.
fn dtx_paragraph_reflows(node: &SyntaxNode, cx: LowerCtx<'_>) -> bool {
    !node
        .ancestors()
        .any(|ancestor| ancestor.kind() == SyntaxKind::ENVIRONMENT)
        && node.children().all(|child| {
            if child.kind() == SyntaxKind::ENVIRONMENT {
                dtx_doc_region(&child, cx)
            } else {
                !child
                    .descendants()
                    .any(|descendant| descendant.kind() == SyntaxKind::ENVIRONMENT)
            }
        })
}

/// Greedily reflow a stream of inline elements to the line width, the shared core
/// of paragraph reflow ([`lower_paragraph_reflow`]) and prose-argument reflow
/// ([`lower_prose_group`]). Maximal runs of *adjacent* non-whitespace elements glue
/// into one unbreakable *atom* (so `Hello,` and `\emph{x}` never split); inter-word
/// whitespace or a lone newline is a break opportunity. A run of atoms lowers to an
/// [`Ir::fill`], which the printer wraps word-by-word.
///
/// Three things end a fill line rather than flow into it: an explicit `\\` line
/// break (a [`SyntaxKind::LINE_BREAK`] node), a `%` comment (which must terminate
/// its line), and a nested *block* (an environment or multi-line group whose IR
/// carries a forced break). Each commits the run-so-far as a fill, then a fresh run
/// continues after. Ordinary blocks are joined by [`Ir::hard_line`]; a sectioning
/// command that is a direct child of a prose paragraph uses [`Ir::empty_line`] on
/// both sides. Adjacent `\label` commands remain attached below that heading, with
/// the trailing empty line deferred until after the label run.
///
/// A lone newline is normally a break opportunity the fill rejoins, *except* when a
/// physical line is made up solely of command(s) (a `\usepackage{…}` line, a
/// `\section{…}` header — see [`line_is_command_only`]): the break on either side of
/// such a line is preserved, keeping it on its own line. Prose lines around it still
/// reflow.
///
/// Unlike a `PARAGRAPH` (which holds no blank lines by construction), an argument
/// *group* body may contain blank-line paragraph breaks; a blank-line trivia run
/// ends the current line and separates the next with an [`Ir::empty_line`].
///
/// [`ReflowKind`] selects how a *lone* source newline is treated (see that type).
/// `Prose` rejoins it into the surrounding fill (paragraphs, prose arguments);
/// `Statement` preserves it, so a code-like brace-group body keeps one logical line
/// per source line and only an *over-long* line wraps — never collapsing the author's
/// statement-per-line structure (`\draw …;` / `\draw …;`) into a single run.
#[derive(Clone, Copy, PartialEq, Eq)]
enum ReflowKind {
    /// Running prose: a lone newline is a break opportunity the fill rejoins.
    Prose,
    /// A grid fallback has structural row breaks. A row that happens to put
    /// commands on their own physical lines must not acquire extra breaks.
    AlignmentBody,
    /// A signature-proven prose *argument* body ([`lower_prose_group`]): like
    /// [`Self::Prose`], but the command-only-line preservation does not apply —
    /// width alone owns the layout. Preserving a command-only line here turns a
    /// width break of pass 1 into a *forced* break on pass 2, and that force
    /// bit leaks upward through every `contains_forced_break` reader (the
    /// opaque-group and optional declines, `collapse_arg_group`,
    /// `finish_cell`), flipping the enclosing construct between its inline and
    /// block forms across passes (pgf's `\emph{… \href{…} …}` table headers).
    /// The residue's own fixed-point argument covers the *interior* refill,
    /// not the propagated bit, so inside a width-owned argument the rule must
    /// not fire at all.
    ProseArg,
    /// Text statements separated by algorithm2e's text-mode `\;`. Argument and
    /// math nodes remain nested, so their semicolons cannot terminate this run.
    Algorithm2e,
    /// Code-like statements (a `\newcommand` definition body, a picture body's
    /// fallback content): a lone newline ends the line, so each source line stays
    /// its own logical line; only width forces a wrap. Flush continuation keeps
    /// the wrap idempotent (a wrapped tail re-parses as a line already at the
    /// body indent). In a curated `statementBody` *environment* body under
    /// [`WrapMode::Reflow`], `STATEMENT` nodes are lowered structurally instead
    /// ([`lower_statement`]: boundaries from the node, continuations hung) and
    /// this authored-line contract governs only the interleaved content no `;`
    /// terminates.
    Statement,
    /// The interior of one structural `STATEMENT` ([`lower_statement`]): like
    /// [`Self::ProseArg`] — width owns the layout, a lone newline is a plain
    /// atom boundary, and the command-only residue is off — but the gaps
    /// additionally consult the TikZ unit model
    /// (`semantic::tikz::statement_glue`): a unit-internal gap (`-- (1,1)`,
    /// `at (2,3)`, `circle (3)`) renders as a single space and never breaks,
    /// so a width wrap lands only at unit boundaries. The verdicts read
    /// non-trivia token text only, so this stays Tier 1: a wrap re-derives
    /// the same units on every pass.
    StatementInterior,
    /// A `.dtx` documentation-layer prose paragraph: behaves like [`Self::Prose`]
    /// (a lone newline rejoins), but the per-line `%` documentation margin
    /// (`DOC_MARGIN`) is *dropped* from each line and each fill segment is wrapped
    /// in an [`Ir::margin_prefix`] so a canonical `% ` margin is re-emitted at
    /// column 0 on every reflowed line.
    DtxProse,
}

/// The canonical `.dtx` documentation margin re-emitted on each reflowed prose
/// line under [`ReflowKind::DtxProse`]: a `%` plus one space.
const DTX_DOC_MARGIN: &str = "% ";

/// One committed atom of a logical-line run: the printed [`Ir`] plus the atom's
/// source `text`, retained so the [`WrapMode::Sentence`]/[`WrapMode::Semantic`]
/// renderer can run sentence-boundary detection over the words. Under
/// [`WrapMode::Reflow`]/[`WrapMode::Stable`] only the layout fields are used.
#[derive(Clone, Copy)]
struct CitationSuffix {
    start: usize,
    placement: CitationPlacement,
}

struct RunAtom {
    ir: Ir,
    text: String,
    /// Byte offset at which a suffix of structurally recognized inline citation
    /// commands begins. `Some(0)` means the whole atom is a citation chain;
    /// `Some(n)` means the citations directly abut preceding prose. Ordinary
    /// content appended after the chain clears the marker.
    trailing_citation: Option<CitationSuffix>,
    /// Whether the gap immediately before this atom was a source newline. False
    /// for the first atom and for ordinary inter-word whitespace.
    preferred_break_before: bool,
}

/// How a completed logical-line run is rendered into a single segment.
#[derive(Clone, Copy)]
enum RunRender<'a> {
    /// Greedy width fill (reflow): one [`Ir::fill`] over the run's atoms, the
    /// printer breaking word-by-word at the line width.
    Fill,
    /// Source-break-aware optimal fill used by [`WrapMode::Stable`].
    Stable { target: usize },
    /// One sentence per line, regardless of width.
    Sentence(ResolvedProfile<'a>),
    /// Sentence boundaries remain hard breaks while each sentence fills to width.
    Semantic(ResolvedProfile<'a>),
}

/// Split a logical-line run into sentences, optionally filling each to width.
/// Adjacent atoms within a run are always whitespace-separated (a glued
/// no-whitespace span is a single atom), so either a space or a width-driven
/// line break preserves the token boundaries. The boundary detector always
/// sees `has_whitespace_after = true`; the final atom closes the last sentence
/// regardless.
fn render_sentences(run: Vec<RunAtom>, profile: ResolvedProfile<'_>, fill: bool) -> Ir {
    let n = run.len();
    // Break decisions for the n-1 internal gaps, read before the run is consumed.
    let mut break_after = vec![false; n];
    for i in 0..n.saturating_sub(1) {
        let boundary_at_end = is_sentence_boundary_text(
            &run[i].text,
            Some(run[i + 1].text.as_str()),
            true,
            false,
            profile,
        );
        let boundary_before_glued_citation = run[i]
            .trailing_citation
            .filter(|suffix| suffix.start > 0)
            .is_some_and(|suffix| {
                // A TeX tie is an explicit nonbreaking attachment to the
                // citation, not part of the word whose terminal punctuation
                // determines the sentence boundary.
                let before_citation = run[i].text[..suffix.start]
                    .strip_suffix('~')
                    .unwrap_or(&run[i].text[..suffix.start]);
                is_sentence_boundary_text(
                    before_citation,
                    Some(&run[i].text[suffix.start..]),
                    true,
                    false,
                    profile,
                )
            });
        if !(boundary_at_end || boundary_before_glued_citation) {
            continue;
        }

        // A postpositive citation belongs to the preceding sentence even when an
        // earlier formatter stranded it on the next line. An ambiguous citation
        // follows the author's source layout; textual citations never carry a
        // suffix marker and therefore remain on the following sentence.
        let mut sentence_end = i;
        while run.get(sentence_end + 1).is_some_and(|atom| {
            atom.trailing_citation.is_some_and(|suffix| {
                suffix.start == 0
                    && match suffix.placement {
                        CitationPlacement::Postpositive => true,
                        CitationPlacement::Ambiguous => !atom.preferred_break_before,
                        CitationPlacement::Textual => false,
                    }
            })
        }) {
            sentence_end += 1;
        }
        break_after[sentence_end] = sentence_end + 1 < n;
    }

    let mut sentences: Vec<Ir> = Vec::new();
    let mut current: Vec<Ir> = Vec::new();
    let render_sentence = |atoms: Vec<Ir>| {
        if fill {
            Ir::fill(atoms)
        } else {
            Ir::join(Ir::text(" "), atoms)
        }
    };
    for (i, atom) in run.into_iter().enumerate() {
        current.push(atom.ir);
        if break_after[i] {
            sentences.push(render_sentence(std::mem::take(&mut current)));
        }
    }
    if !current.is_empty() {
        sentences.push(render_sentence(current));
    }
    // Semantic sentence boundaries reparse as authored newlines, so both
    // passes must preserve them without promoting the run to a block.
    Ir::join(
        if fill {
            Ir::preserved_line()
        } else {
            Ir::hard_line()
        },
        sentences,
    )
}

/// Accumulator for [`reflow_elements`]: glues atom pieces, collects them into the
/// current logical-line run, and commits completed lines (with their preceding
/// separators). Bundles the state the former nested `flush_atom`/`end_line`/
/// `push_segment` helpers shared so the retained atom text and the render policy
/// ride along without threading extra parameters through every call site.
struct LineBuilder<'a> {
    /// Glued pieces of the atom in progress (its [`Ir`] and its source text).
    atom: Vec<Ir>,
    atom_text: String,
    atom_trailing_citation: Option<CitationSuffix>,
    /// Source-newline preference to attach to the next committed atom.
    preferred_break_before_next: bool,
    /// Atoms of the current run (the current logical line).
    run: Vec<RunAtom>,
    /// Completed lines (fills/sentences and blocks), interleaved with `seps` at the
    /// end.
    lines: Vec<Ir>,
    /// Pieces riding the *last* committed line (see [`Self::append_to_last_line`]),
    /// held flat until the line is sealed. Folding each one into the line as it
    /// arrives would nest a `Concat` per piece, and a whole document collapsed onto
    /// one physical line (the trivia oracle's `all-newlines-to-spaces` variant) then
    /// recurses one frame per piece and overflows the stack.
    line_tail: Vec<Ir>,
    /// The separator *preceding* each committed line (`seps[0]` is unused). A blank
    /// line in the source promotes the next separator to an [`Ir::empty_line`].
    seps: Vec<Ir>,
    /// The separator to record before the next committed line. Default: one break.
    pending_sep: Ir,
    /// Under `.dtx` prose reflow, the `% ` margin re-emitted on each line; `None`
    /// otherwise.
    margin: Option<&'static str>,
    /// Whether some content was committed *outside* the [`Self::margin`] — a
    /// forced-break block whose interior lines do not all ride their own
    /// margins ([`block_rides_own_margins`]), one opening on an unmargined
    /// line, or a column-0 `GUARD` whose line could not be isolated
    /// ([`collect_guard_line`]). Such content lands on a line with no leading
    /// `%`, where a `.dtx` doc comment (`^^A`, a `%` run) re-lexes as content:
    /// the layout would no longer be whitespace-only.
    /// [`lower_dtx_doc_paragraph`] reads this and falls back to the
    /// byte-faithful preserve path. Always `false` when `margin` is `None`.
    margin_escaped: bool,
    /// How a completed run is turned into a segment (fill vs. sentences).
    render: RunRender<'a>,
}

impl<'a> LineBuilder<'a> {
    fn new(margin: Option<&'static str>, render: RunRender<'a>) -> Self {
        Self {
            atom: Vec::new(),
            atom_text: String::new(),
            atom_trailing_citation: None,
            preferred_break_before_next: false,
            run: Vec::new(),
            lines: Vec::new(),
            line_tail: Vec::new(),
            seps: Vec::new(),
            pending_sep: Ir::hard_line(),
            margin,
            margin_escaped: false,
            render,
        }
    }

    /// Record that content is about to be committed outside the `% ` margin (see
    /// [`Self::margin_escaped`]). A no-op when no margin is in force.
    fn note_margin_escape(&mut self) {
        if self.margin.is_some() {
            self.margin_escaped = true;
        }
    }

    /// Glue one piece (its [`Ir`] and source `text`) onto the atom in progress.
    fn push_atom_piece(&mut self, ir: Ir, text: &str) {
        self.atom.push(ir);
        self.atom_text.push_str(text);
        self.atom_trailing_citation = None;
    }

    /// Append a structurally recognized inline citation while retaining the
    /// start of a trailing citation chain. Unlike an ordinary atom piece, another
    /// directly abutting citation extends the same chain.
    fn push_trailing_citation(&mut self, ir: Ir, text: &str, placement: CitationPlacement) {
        self.atom_trailing_citation.get_or_insert(CitationSuffix {
            start: self.atom_text.len(),
            placement,
        });
        self.atom.push(ir);
        self.atom_text.push_str(text);
    }

    /// Commit the atom in progress (if any) as one atom of the current run.
    fn flush_atom(&mut self) {
        if !self.atom.is_empty() {
            let text = std::mem::take(&mut self.atom_text);
            // DTX prose owns wrapping only between atoms. Letting a nested group
            // break inside one can split macro-like documentation at an arbitrary
            // brace and synthesize margins that change the next pass's lowering.
            let ir = if self.margin.is_some() {
                self.atom.clear();
                Ir::verbatim(text.clone())
            } else {
                Ir::concat(self.atom.drain(..))
            };
            self.run.push(RunAtom {
                ir,
                text,
                trailing_citation: self.atom_trailing_citation.take(),
                preferred_break_before: std::mem::take(&mut self.preferred_break_before_next),
            });
        }
    }

    /// Mark the next inter-atom gap as an authored line break.
    fn prefer_next_break(&mut self) {
        if !self.run.is_empty() {
            self.preferred_break_before_next = true;
        }
    }

    /// Commit `content` as the next logical line, recording the separator before it
    /// and resetting `pending_sep` to a single break.
    fn push_segment(&mut self, content: Ir) {
        self.seal_last_line();
        self.seps
            .push(std::mem::replace(&mut self.pending_sep, Ir::hard_line()));
        self.lines.push(content);
    }

    /// Separate a paragraph-level sectioning command from adjacent prose. A
    /// `.dtx` documentation paragraph needs a bare `%` on the empty physical
    /// line so the separator remains inside the documentation layer.
    fn separate_section(&mut self) {
        self.pending_sep = if self.margin.is_some() {
            Ir::concat([Ir::hard_line(), Ir::column_zero("%"), Ir::hard_line()])
        } else {
            Ir::empty_line()
        };
    }

    /// Commit a forced-break block as its own segment under the `.dtx` margin:
    /// the canonical `% ` re-attached for its first line (the source margin was
    /// dropped by the `DOC_MARGIN` arm, or floated out of the paragraph), then
    /// the block raw — its interior lines carry their own column-0 margins
    /// byte-faithfully ([`block_rides_own_margins`]), so no [`Ir::margin_prefix`]
    /// wrap is needed (or safe: the printer's re-emitted prefix would collide
    /// with the block's own `Ir::column_zero` margins).
    fn push_margined_block(&mut self, ir: Ir) {
        let margin = self.margin.expect("only called under `.dtx` prose reflow");
        self.push_segment(Ir::concat([Ir::column_zero(margin), ir]));
    }

    /// Glue a trailing comment onto the run so it rides the end of its line: onto
    /// the atom in progress when one is open (a directly-glued `word%…`, whose
    /// missing space is the space-suppression idiom), else onto the last committed
    /// atom with the single separating space restored (`word %…`). The comment must
    /// never become a fill atom of its own — a width break before it would commit
    /// it to the next line, where the own-line `%` re-binds as the next command's
    /// doc comment on reparse and breaks idempotence.
    fn append_trailing_comment(&mut self, text: &str) {
        if !self.atom.is_empty() {
            self.push_atom_piece(Ir::verbatim(text), text);
            return;
        }
        if let Some(last) = self.run.last_mut() {
            let prev = std::mem::replace(&mut last.ir, Ir::Nil);
            last.ir = Ir::concat([prev, Ir::verbatim(" "), Ir::verbatim(text)]);
            last.text.push(' ');
            last.text.push_str(text);
            return;
        }
        // Empty run (the caller guards with `line_has_content`, so this is a
        // safety net): the comment becomes the line's only atom.
        self.push_atom_piece(Ir::verbatim(text), text);
    }

    /// Glue `ir` onto the end of the last committed line (a trailing comment, or
    /// content still on a block's last physical line). No-op when no line has been
    /// committed. Buffered in [`Self::line_tail`] and folded in by
    /// [`Self::seal_last_line`], so N riders cost one `Concat`, not N nested ones.
    fn append_to_last_line(&mut self, ir: Ir) {
        if !self.lines.is_empty() {
            self.line_tail.push(ir);
        }
    }

    /// Fold any buffered riders into the last committed line.
    fn seal_last_line(&mut self) {
        if self.line_tail.is_empty() {
            return;
        }
        let tail = std::mem::take(&mut self.line_tail);
        if let Some(last) = self.lines.last_mut() {
            let prev = std::mem::replace(last, Ir::Nil);
            *last = Ir::concat(std::iter::once(prev).chain(tail));
        }
    }

    /// End the current logical line: flush the atom and, when the run is non-empty,
    /// render it (a fill under reflow, sentences under sentence/semantic) and commit
    /// it. Under `.dtx` prose reflow (`margin` set) the segment is wrapped in an
    /// [`Ir::margin_prefix`] so a `% ` margin is re-emitted on every line.
    fn end_line(&mut self) {
        self.flush_atom();
        if self.run.is_empty() {
            return;
        }
        let run = std::mem::take(&mut self.run);
        let body = self.render_run(run);
        let segment = match self.margin {
            Some(m) => Ir::margin_prefix(m, body),
            None => body,
        };
        self.push_segment(segment);
    }

    /// Commit a reflow run as a hugging fill. A final inline construct whose IR
    /// contains hard breaks can then keep its fitting first line beside the
    /// preceding prose; the construct's remaining lines still break internally.
    fn end_hug_line(&mut self) {
        self.flush_atom();
        if self.run.is_empty() {
            return;
        }
        let atoms: Vec<Ir> = std::mem::take(&mut self.run)
            .into_iter()
            .map(|atom| atom.ir)
            .collect();
        let body = if atoms.len() == 1 {
            atoms.into_iter().next().unwrap()
        } else {
            let mut parts = Vec::with_capacity(atoms.len() * 2 - 1);
            for (index, atom) in atoms.into_iter().enumerate() {
                if index > 0 {
                    parts.push(Ir::Line);
                }
                parts.push(atom);
            }
            Ir::HugFill(parts.into())
        };
        let segment = match self.margin {
            Some(margin) => Ir::margin_prefix(margin, body),
            None => body,
        };
        self.push_segment(segment);
    }

    fn render_run(&self, run: Vec<RunAtom>) -> Ir {
        match self.render {
            RunRender::Fill => Ir::fill(run.into_iter().map(|a| a.ir)),
            RunRender::Stable { target } => {
                let preferred: Vec<bool> = run
                    .iter()
                    .skip(1)
                    .map(|atom| atom.preferred_break_before)
                    .collect();
                Ir::preferred_fill(run.into_iter().map(|a| a.ir), preferred, target)
            }
            RunRender::Sentence(profile) => render_sentences(run, profile, false),
            RunRender::Semantic(profile) => render_sentences(run, profile, true),
        }
    }

    /// Emit the accumulated lines, interleaving the recorded separators.
    fn finish(mut self) -> Ir {
        self.end_line();
        self.seal_last_line();
        let mut result: Vec<Ir> = Vec::with_capacity(self.lines.len().saturating_mul(2));
        for (i, line) in self.lines.into_iter().enumerate() {
            if i > 0 {
                result.push(self.seps[i].clone());
            }
            result.push(line);
        }
        Ir::concat(result)
    }
}

fn reflow_elements(
    elements: impl Iterator<Item = SyntaxElement>,
    cx: LowerCtx<'_>,
    kind: ReflowKind,
) -> Ir {
    reflow_elements_checked(elements, cx, kind).0
}

/// [`reflow_elements`], additionally reporting whether the result committed any
/// content outside the `% ` documentation margin (see
/// [`LineBuilder::margin_escaped`]). Only meaningful under
/// [`ReflowKind::DtxProse`]; every other kind always reports `false`.
fn reflow_elements_checked(
    elements: impl Iterator<Item = SyntaxElement>,
    cx: LowerCtx<'_>,
    kind: ReflowKind,
) -> (Ir, bool) {
    // Collected up front so the single-newline arm can look ahead at the next
    // physical line ([`line_is_command_only`]). Inline prose commands (`\footnote`,
    // `\emph`, …) are flattened into the stream so their bodies reflow as running
    // text rather than block-breaking their braces (see [`flatten_inline_prose`]);
    // `STATEMENT` wrappers are spliced out the same way (see
    // [`flatten_statements`]) so their contents reflow as the sibling stream
    // they wrap.
    // Under `Reflow` a statement-body run is lowered *structurally*: `STATEMENT`
    // nodes stay whole and take their own arm below (boundaries from the node,
    // continuations hung — see [`lower_statement`]). Every other path splices
    // the wrappers out and keeps the line-stream behavior.
    let structural = kind == ReflowKind::Statement && cx.wrap == WrapMode::Reflow;
    let elements: Vec<SyntaxElement> = elements.collect();
    let elements = if structural {
        elements
    } else {
        flatten_statements(elements)
    };
    let glue_matched_args = !cx.in_dtx_doc_region
        && !run_carries_doc_margin(&elements, cx)
        && matches!(
            kind,
            ReflowKind::Prose | ReflowKind::ProseArg | ReflowKind::AlignmentBody
        );
    // Algorithm terminators belong to this body's direct stream. Flattening a
    // text argument would promote its private `\;` into a statement boundary.
    let elements: Vec<SyntaxElement> = if kind == ReflowKind::Algorithm2e {
        elements
    } else {
        flatten_inline_prose(elements, cx, glue_matched_args)
    };

    // Inside one structural statement, gaps consult the TikZ unit model: a
    // unit-internal gap renders as a single space instead of a break
    // opportunity (see [`ReflowKind::StatementInterior`]). Computed over the
    // *flattened* stream so the verdict indices match the loop's.
    let unit_glue: Option<Vec<bool>> =
        (kind == ReflowKind::StatementInterior).then(|| statement_glue(&elements));

    // Under `.dtx` prose reflow each segment is wrapped in a `% ` margin prefix and
    // the per-line `DOC_MARGIN` tokens are dropped; `None` otherwise.
    let margin: Option<&'static str> = (kind == ReflowKind::DtxProse).then_some(DTX_DOC_MARGIN);

    // Sentence/semantic segmentation applies to *prose* runs; a `Statement` run is
    // code (a `\newcommand` body), so it keeps the width fill regardless of mode.
    let render = match cx.wrap {
        _ if kind == ReflowKind::Algorithm2e => RunRender::Fill,
        WrapMode::Stable if kind != ReflowKind::Statement => RunRender::Stable {
            target: cx.stable_target,
        },
        WrapMode::Sentence if kind != ReflowKind::Statement => RunRender::Sentence(cx.profile),
        WrapMode::Semantic if kind != ReflowKind::Statement => RunRender::Semantic(cx.profile),
        _ => RunRender::Fill,
    };

    let mut b = LineBuilder::new(margin, render);
    // Whether the current *physical* source line so far consists solely of
    // command(s) (and inline whitespace). Such a line is kept on its own line
    // rather than reflowed into its neighbours (see the single-newline arm). Both
    // reset at every physical-line boundary. This is the *residual* command-line
    // rule: curated block-level commands are intercepted upstream (the
    // block-statement arm below) and never depend on it, so what it decides for
    // is un-signatured and scanned-definition commands — whose block-ness no
    // positive signature property can know — plus block commands glued to
    // adjacent content. That residue reads the lone-newline predicate as
    // sanctioned Tier 2: preservation-only, with the fixed-point argument
    // written on [`line_is_command_only`]. It reaches the count through
    // [`consume_widened_gap_slice`], the widened boundary (see [`WideGap`]).
    let mut line_all_commands = true;
    let mut line_has_content = false;
    // Whether the current physical source line rides a `% ` documentation margin.
    // Only meaningful under `DtxProse`. Initialized `true`: both `DtxProse`
    // callers gate on a margined first line ([`dtx_paragraph_starts_margined`],
    // [`dtx_run_starts_margined`]) — a contract that covers the floated-margin
    // paragraph, whose leading `%` sits *outside* the element stream. Cleared at
    // every newline, re-established by the line's `DOC_MARGIN`.
    let mut line_margined = true;
    // Whether the previous element was a forced-break node committed via
    // `push_segment` (a doc-commented command, an environment, …). A `COMMENT`
    // on the same physical line as such a block — glued directly
    // (`\end{center}%`) or after inline whitespace (`\newcommand{…}{…} % note`)
    // — must ride the block's last line: committing it as its own line changes
    // spacing semantics in the glued case and, because an own-line `%` binds
    // forward as a doc comment on reparse, breaks idempotence (issue #38).
    // `block_gap` records that inline whitespace separated the two, so the
    // riding comment keeps a single space before it.
    let mut prev_was_block = false;
    let mut block_gap = false;
    // Set alongside `prev_was_block` when the committed block *closes* its line: a
    // non-inline environment, sectioning command, or curated block command. A
    // trailing `%` still rides (it must never be relocated), but content starts a
    // fresh line. Other forced blocks, such as a doc-commented `\input`, leave
    // their last line open so content from the same source line can ride it.
    let mut prev_block_closes_line = false;

    let mut idx = 0;
    while idx < elements.len() {
        let after_block = std::mem::take(&mut prev_was_block);
        let after_block_gap = std::mem::take(&mut block_gap);
        let after_block_closed = std::mem::take(&mut prev_block_closes_line);
        match &elements[idx] {
            // Whitespace / newline run: a physical-line and atom boundary.
            SyntaxElement::Token(token) if is_collapsible_trivia(token.kind()) => {
                let newlines = consume_widened_gap_slice(&elements, &mut idx);
                // A unit-internal gap (the TikZ unit model, statement interiors
                // only): glue the neighbors into one atom with a single space —
                // no break opportunity, no line boundary. Content-derived, so a
                // wrap re-reads to the same units (Tier 1); the model never
                // glues across a comment or a blank line.
                if newlines < 2
                    && let Some(glue) = &unit_glue
                    && glue.get(idx).copied().unwrap_or(false)
                {
                    if after_block {
                        // The unit is riding a block segment's last line (a
                        // doc-commented statement head): keep riding — the next
                        // element's `ride_after_block` gap form restores the
                        // single space, and riding appends flat, so the
                        // no-break promise holds there too.
                        prev_was_block = true;
                        block_gap = true;
                        prev_block_closes_line = after_block_closed;
                    } else {
                        b.push_atom_piece(Ir::verbatim(" "), " ");
                    }
                    continue;
                }
                if newlines >= 2 {
                    // A blank line ends the line and promotes the next separator.
                    b.end_line();
                    b.pending_sep = Ir::empty_line();
                    line_all_commands = true;
                    line_has_content = false;
                    line_margined = false;
                } else if newlines == 1 {
                    // A single source newline. Under `Statement` reflow every source
                    // line is its own logical line, so the break always ends the line
                    // (structural `STATEMENT` nodes never reach this arm — they
                    // commit through their own arm below — so this authored-line
                    // read governs only the fallback content no `;` terminates).
                    // Under `Semantic` an authored soft break is likewise preserved
                    // (sembr keeps the writer's clause breaks). Under `Prose`/`Sentence`
                    // it is normally just an atom boundary the run rejoins, except a
                    // line that is *only* command(s) — on either side of the break — is
                    // kept on its own line: end the line so the break survives instead
                    // of collapsing to a space. This is the residual rule for commands
                    // no positive signature property covers (see `line_all_commands`
                    // above); curated block commands never reach it.
                    // The command-only residue is skipped under `ProseArg` and
                    // `StatementInterior` and in grid fallbacks: a width-owned
                    // body must not mint
                    // forced breaks pass 2 can see and pass 1 could not (see
                    // [`ReflowKind`]).
                    let residue_applies = !matches!(
                        kind,
                        ReflowKind::ProseArg
                            | ReflowKind::StatementInterior
                            | ReflowKind::Algorithm2e
                            | ReflowKind::AlignmentBody
                    );
                    let prev_is_command = residue_applies && line_has_content && line_all_commands;
                    let next_is_command =
                        residue_applies && line_is_command_only(&elements, idx, cx);
                    if kind == ReflowKind::Statement
                        || cx.wrap == WrapMode::Preserve
                        || cx.wrap == WrapMode::Semantic
                        || prev_is_command
                        || next_is_command
                    {
                        b.end_line();
                        if cx.wrap == WrapMode::Semantic
                            && kind != ReflowKind::Statement
                            && line_has_content
                        {
                            // A width break from the previous pass must stay a
                            // line break without becoming a structural block
                            // that changes an enclosing argument's lowering.
                            b.pending_sep = Ir::preserved_line();
                        }
                    } else {
                        b.flush_atom();
                        // Stable uses this as a soft layout preference; sentence
                        // mode uses it to distinguish a citation intentionally
                        // starting the next source line from one following the
                        // preceding sentence on the same line. The resulting
                        // boundary reproduces itself on the next pass.
                        b.prefer_next_break();
                    }
                    line_all_commands = true;
                    line_has_content = false;
                    line_margined = false;
                } else {
                    // Pure inline whitespace: an atom boundary within the line.
                    // It stays on the block's physical line, so a comment next
                    // keeps riding the block (with the space restored).
                    b.flush_atom();
                    if after_block {
                        prev_was_block = true;
                        block_gap = true;
                        prev_block_closes_line = after_block_closed;
                    }
                }
                continue;
            }
            SyntaxElement::Token(token)
                if kind == ReflowKind::Algorithm2e
                    && token.kind() == SyntaxKind::CONTROL_SYMBOL
                    && token.text() == "\\;" =>
            {
                if after_block && !after_block_closed {
                    b.append_to_last_line(ride_after_block(lower_loose_token(token, cx), after_block_gap));
                } else {
                    b.push_atom_piece(lower_loose_token(token, cx), token.text());
                }
                b.end_line();
                line_all_commands = true;
                line_has_content = false;
                // Keep a trailing comment on the terminated statement's line.
                prev_was_block = true;
                prev_block_closes_line = true;
            }
            // A comment trailing content rides the end of that line, then forces a
            // break. But a comment that *begins* its own physical line stays on its
            // own line: end the current line first so the preceding prose run commits
            // separately, instead of reflowing the bare `%` up into that run.
            SyntaxElement::Token(token) if token.kind() == SyntaxKind::COMMENT => {
                if after_block {
                    // On the block segment's last physical line: ride it instead
                    // of starting a line of its own. A directly-glued comment
                    // (`\end{center}%`) stays glued — the `%` is the
                    // space-suppression idiom — while one separated by inline
                    // whitespace keeps a single space.
                    let comment = if after_block_gap {
                        Ir::concat([Ir::verbatim(" "), Ir::verbatim(token.text())])
                    } else {
                        Ir::verbatim(token.text())
                    };
                    b.append_to_last_line(comment);
                } else if line_has_content {
                    // Trailing content on this physical line: ride its end (never
                    // a fill atom of its own — see `append_trailing_comment`).
                    b.append_trailing_comment(token.text());
                    b.end_line();
                } else {
                    b.end_line();
                    b.push_atom_piece(Ir::verbatim(token.text()), token.text());
                    b.end_line();
                }
                line_all_commands = true;
                line_has_content = false;
            }
            // A `\`-at-end-of-line control symbol (`\` + newline) carries its own
            // newline but nothing after it — kept verbatim for losslessness, it
            // ends the line: emit the part before the break as a flat atom and let
            // the line break supply the newline, so the result reparses to the same
            // token (idempotent) instead of leaving an unbreakable multi-line atom
            // inside the run. Restricted to control symbols: a multi-line `VERB`
            // token (a brace-verbatim argument spanning lines) has real content
            // after its newline and must be emitted whole by the arm below.
            SyntaxElement::Token(token)
                if token.kind() == SyntaxKind::CONTROL_SYMBOL && token.text().contains('\n') =>
            {
                let before = token.text().split_once('\n').map(|(b, _)| b).unwrap_or("");
                if !before.is_empty() {
                    b.push_atom_piece(Ir::verbatim(before), before);
                }
                if !cx.absorbs_control_newline(token) {
                    b.end_line();
                }
                line_all_commands = true;
                line_has_content = false;
            }
            // Under `.dtx` prose reflow, a per-line `%` documentation margin
            // (`DOC_MARGIN`) is dropped: the canonical `% ` margin is re-emitted on
            // every reflowed line by the enclosing [`Ir::margin_prefix`] (see
            // `end_line`), so gluing the source `%` into the run would double it.
            // The single space following it is inter-word whitespace the run
            // re-derives. A `GUARD` is *not* dropped (guards keep their column-0 pin).
            SyntaxElement::Token(token)
                if margin.is_some() && token.kind() == SyntaxKind::DOC_MARGIN =>
            {
                line_margined = true;
            }
            // The enclosing virtual-document region re-emits one canonical
            // margin per generated line. Discard the physical marker and its
            // authored padding before ordinary structural lowering continues.
            SyntaxElement::Token(token)
                if cx.in_dtx_doc_region && token.kind() == SyntaxKind::DOC_MARGIN =>
            {
                while elements.get(idx + 1).is_some_and(|element| {
                    matches!(element, SyntaxElement::Token(next) if next.kind() == SyntaxKind::WHITESPACE)
                }) {
                    idx += 1;
                }
            }
            // A `GUARD` (`%<…>`) pins its whole physical line to column 0. Commit
            // that line as one segment under every reflow kind; otherwise two
            // adjacent guarded commands can join, and the second `%<…>` becomes a
            // trailing comment that swallows its command on the next parse. Under
            // a `.dtx` prose margin the isolated segment is deliberately
            // unmargined—the margin and guard would collide.
            SyntaxElement::Token(token) if token.kind() == SyntaxKind::GUARD => {
                if let Some(line) = collect_guard_line(&elements, &mut idx, cx) {
                    b.end_line();
                    b.push_segment(line);
                    line_all_commands = true;
                    line_has_content = false;
                    continue;
                }
                if margin.is_some() {
                    b.note_margin_escape();
                }
                b.push_atom_piece(lower_loose_token(token, cx), token.text());
                line_has_content = true;
                line_all_commands = false;
            }
            // Any other token (WORD, `~`, `&`, `#`, `^`, `_`, brackets, `\verb`,
            // a bare control symbol) glues onto the current atom — prose content,
            // so this physical line is no longer command-only. A `.dtx` margin/guard
            // (only under the dtx config) pins to column 0 instead of reflowing.
            SyntaxElement::Token(token) => {
                if after_block && !after_block_closed {
                    // Content on the block segment's last physical line
                    // (`\input docstrip.tex`, where the doc comment bound to
                    // `\input` made it a block): ride that line instead of
                    // starting one of its own. Same rule as the `COMMENT` arm
                    // above, and the chain continues so the rest of the line
                    // rides too. A block that *closes* its line (a heading) is
                    // excluded — content after it starts a fresh line.
                    b.append_to_last_line(ride_after_block(
                        lower_loose_token(token, cx),
                        after_block_gap,
                    ));
                    prev_was_block = true;
                } else {
                    b.push_atom_piece(lower_loose_token(token, cx), token.text());
                }
                line_has_content = true;
                line_all_commands = false;
            }
            // A structural statement (`\draw …;` in a curated `statementBody`
            // body): its own logical line, boundaries from the node — never from
            // the trivia around it. Only present when `structural` (every other
            // path spliced the wrapper out up front).
            //
            // A statement opens its own line even when the author *glued* it to
            // what precedes (`…;\draw …`). This is the one sanctioned breach of
            // the glued-divider principle, licensed the way `ContentKind::Keyval`
            // licenses the glued comma split: the curated `statementBody` flag
            // asserts that whitespace between a picture body's statements is
            // insignificant to the package that consumes them, so the inserted
            // break is not a typeset change. The claim is held to the curated
            // standard (`task typeset:check` carries a case), and it is
            // empirically idle: glued statement seams are unattested in the
            // pgf and user corpora (~6000 statements).
            SyntaxElement::Node(child) if child.kind() == SyntaxKind::STATEMENT => {
                let ir = lower_statement(child, cx);
                b.end_line();
                b.push_segment(ir);
                line_all_commands = true;
                line_has_content = false;
                // One statement per line: following content starts a fresh line
                // (`prev_block_closes_line`), while a trailing `%` still rides
                // (the `COMMENT` arm's `after_block` path).
                prev_was_block = true;
                prev_block_closes_line = true;
            }
            // An explicit `\\` line break (with its `*` / `[len]`, grouped by the
            // parser into one node) rides the end of the current line, then breaks.
            SyntaxElement::Node(child) if child.kind() == SyntaxKind::LINE_BREAK => {
                b.push_atom_piece(lower_node(child, cx), &child.text().to_string());
                b.end_line();
                line_all_commands = true;
                line_has_content = false;
            }
            // An inline citation list participates in the surrounding paragraph
            // fill at its top-level commas, just as an inline prose argument
            // participates at its inter-word gaps. Keeping the entries at this
            // altitude lets the first key share the preceding prose line and the
            // closing brace share the final key's line with following prose.
            SyntaxElement::Node(child)
                if matches!(cx.wrap, WrapMode::Reflow | WrapMode::Stable)
                    && margin.is_none()
                    && !after_block
                    && child.kind() == SyntaxKind::COMMAND
                    && command_is_inline(child, cx)
                    && inline_token_list_atoms(child, cx).is_some() =>
            {
                let atoms = inline_token_list_atoms(child, cx)
                    .expect("match guard proved the inline token list");
                for (index, atom) in atoms.into_iter().enumerate() {
                    if index > 0 {
                        b.flush_atom();
                    }
                    b.push_atom_piece(atom, "");
                }
                line_has_content = true;
                line_all_commands = false;
                idx += 1;
                continue;
            }
            SyntaxElement::Node(child) => {
                let (ir, source) = if let Some(last) = super::beamer::wrapper_end(&elements, idx)
                    && !run_carries_doc_margin(&elements[idx..=last], cx)
                {
                    let wrapper = &elements[idx..=last];
                    let ir = Ir::concat(lower_element_stream(wrapper.iter().cloned(), cx));
                    let source = wrapper.iter().map(ToString::to_string).collect::<String>();
                    idx = last;
                    (ir, source)
                } else {
                    (lower_node(child, cx), child.text().to_string())
                };
                // A block-level command — sectioning (`\part` … `\subparagraph`) or
                // curated block (`\usepackage`, `\newcommand`, …) — is a block-level
                // statement: it opens a line and closes one, whatever trivia the
                // author wrote around it. At paragraph level, sectioning additionally
                // receives an empty line on both sides, making the structural
                // boundary visible. The old behavior kept the break only when
                // the source had a newline there (via `line_is_command_only` below),
                // which is exactly the lone-newline predicate trivia-invariant layout
                // forbids — `\subsection{X}\nprose` and `\subsection{X} prose` are the
                // same bytes to the next parse, so both must lay out alike. Reading
                // `CommandSig::sectioning`/`CommandSig::block` keeps the rule in the
                // semantic layer instead of a formatter-owned name list.
                //
                // Two gates scope the statement treatment:
                // - Not under `ReflowKind::Statement`, whose Tier-2 contract is the
                //   authored line: a brace-group body (`\AtBeginDocument{\setcounter
                //   {page}{1}}`, a one-line `\newcommand` body) must not be forced
                //   open. Block-statement synthesis lives at prose altitude.
                // - A *block* (unlike a sectioning) command must be trivia-isolated
                //   on both sides: breaking where the author glued
                //   (`\ProcessOptions\relax`) materializes a space token TeX
                //   typesets. A heading splits even glued — its own `\par` discards
                //   the materialized glue — so sectioning keeps the unconditional
                //   form. Gluedness is a predicate the formatter preserves, so both
                //   reads are Tier-safe.
                //
                // Forced-break lowerings (a comment inside the title) fall through to
                // the block path below, which already opens and closes a line — and
                // does so through the `.dtx` margin-aware routes this arm's plain
                // `end_line` pair would bypass.
                let is_sectioning =
                    child.kind() == SyntaxKind::COMMAND && command_is_sectioning(child, cx);
                // A blank line is a real `\par`, so synthesize one only where the
                // parser already identified a top-level prose paragraph. Nested
                // headings still get the block command's hard-line boundaries;
                // inserting `\par` inside a macro argument can make a non-`long`
                // macro invalid, and inside a conditional it can reshape the
                // wrapper between formatting passes.
                let is_section_boundary = is_sectioning
                    && child
                        .parent()
                        .is_some_and(|parent| parent.kind() == SyntaxKind::PARAGRAPH);
                let is_section_label = child.kind() == SyntaxKind::COMMAND
                    && command_is_label(child)
                    && label_follows_sectioning_run(&elements, idx, cx);
                let section_label_closes_line =
                    is_section_label && next_is_separated(&elements, idx);
                // Algorithm control-flow blocks end a TeX paragraph themselves,
                // so a following statement can start a line even when glued.
                let algorithm_block = kind == ReflowKind::Algorithm2e
                    && cx.signatures.command_at(child).is_some_and(|sig| {
                        sig.args.iter().any(|arg| arg.content == ContentKind::Algorithm2e)
                    });
                let is_block_stmt = kind != ReflowKind::Statement
                    && child.kind() == SyntaxKind::COMMAND
                    && (is_sectioning
                        || (command_is_block(child, cx)
                            && b.atom.is_empty()
                            && (next_is_separated(&elements, idx) || algorithm_block)));
                if is_section_label && !ir.contains_forced_break() {
                    let glued_to_previous_label = idx > 0
                        && matches!(
                            &elements[idx - 1],
                            SyntaxElement::Node(previous)
                                if previous.kind() == SyntaxKind::COMMAND
                                    && command_is_label(previous)
                        );
                    if !glued_to_previous_label {
                        b.end_line();
                    }
                    b.push_atom_piece(ir, &source);
                    if section_label_closes_line {
                        b.end_line();
                        if next_nontrivia_is_label(&elements, idx, cx) {
                            b.pending_sep = Ir::hard_line();
                        } else {
                            b.separate_section();
                        }
                        line_has_content = false;
                        prev_was_block = true;
                        prev_block_closes_line = true;
                    } else {
                        line_has_content = true;
                    }
                    line_all_commands = true;
                    idx += 1;
                    continue;
                }
                if is_block_stmt && !ir.contains_forced_break() {
                    b.end_line();
                    if is_section_boundary {
                        b.separate_section();
                    }
                    b.push_atom_piece(ir, &source);
                    b.end_line();
                    if is_section_boundary && !next_nontrivia_is_label(&elements, idx, cx) {
                        b.separate_section();
                    }
                    line_all_commands = true;
                    line_has_content = false;
                    // Committed as a block, so a `%` still on the heading's physical
                    // line rides it (the `COMMENT` arm's `after_block` path) instead
                    // of being stranded on a line of its own, where it would rebind as
                    // the next construct's `DOC_COMMENT` at the next parse. Content
                    // does *not* ride: closing the line is the whole rule.
                    prev_was_block = true;
                    prev_block_closes_line = true;
                    idx += 1;
                    continue;
                }
                if ir.contains_forced_break() {
                    if is_section_boundary {
                        // A documentation comment belongs to the heading it
                        // introduces, so separate the whole forced-break command
                        // from preceding prose rather than splitting the comment
                        // from its command.
                        b.end_line();
                        b.separate_section();
                    }
                    // A virtual documentation environment already owns every
                    // physical margin through its `Ir::doc_margin` wrapper. It is
                    // therefore a complete segment in `DtxProse`: surrounding
                    // prose may keep reflowing, but the accumulator must neither
                    // add another `% ` nor record the block as a margin escape.
                    let owns_dtx_margin =
                        margin.is_some() && dtx_doc_region(child, cx);
                    // A margin-framed `macrocode` chunk opening its own margined
                    // source line, reachable only through a reflowing expl3 run
                    // ([`dtx_run_reflows_safely`]): committed raw behind its
                    // byte-exact source frame lead, never the canonical `% ` —
                    // docstrip matches the `%    \begin{macrocode}` line literally.
                    let frame_lead = (margin.is_some()
                        && line_margined
                        && !line_has_content
                        && is_margin_framed_macrocode(child))
                    .then(|| dtx_env_line_lead(child))
                    .flatten();
                    let hugs_preceding_prose = margin.is_none()
                        && child.kind() == SyntaxKind::INLINE_MATH
                        && line_has_content
                        && matches!(kind, ReflowKind::Prose | ReflowKind::ProseArg | ReflowKind::AlignmentBody)
                        && matches!(b.render, RunRender::Fill);
                    let rides_preceding_block = after_block
                        && !after_block_gap
                        && !after_block_closed
                        && !is_sectioning;
                    if rides_preceding_block {
                        // A forced-break node can itself be glued to the forced
                        // block before it (`{a%\n}{b%\n}`). Keep their shared
                        // boundary on one physical line just as the token and
                        // ordinary-node arms do below. Splitting it would create
                        // a TeX space token where the source had no gap.
                        b.append_to_last_line(ir);
                    } else if margin.is_none() && (!b.atom.is_empty() || hugs_preceding_prose) {
                        // A directly glued block (`\newcommand\cls@hook{%`) had no
                        // source break opportunity, so it extends the unbreakable
                        // atom in progress. The one spaced admission is inline math
                        // in running prose, whose opening fragment remains inline
                        // through the explicit hugging-fill rule below. Both paths
                        // are skipped under a `.dtx` margin, where a generated line
                        // also needs physical framing.
                        b.push_atom_piece(ir, &source);
                        if hugs_preceding_prose {
                            // Inline math remains inline at its opening edge even
                            // when protected comments force later lines. A hugging
                            // fill measures only the math node's first line here,
                            // moving it to the next line only when that prefix does
                            // not fit beside the preceding prose.
                            b.end_hug_line();
                        } else {
                            b.end_line();
                        }
                    } else if let Some(lead) = frame_lead {
                        b.end_line();
                        b.push_segment(Ir::concat([lead, ir]));
                    } else if owns_dtx_margin {
                        b.end_line();
                        b.push_segment(ir);
                    } else if margin.is_some() && line_margined && block_rides_own_margins(child) {
                        // A block amid `.dtx` doc prose whose interior lines all
                        // carry their own column-0 margins, opening on a margined
                        // line: commit it raw on a fresh line with the canonical
                        // margin re-attached for its first line. Its interior
                        // bytes are untouched, so the layout stays within the
                        // `% ` margin and the surrounding prose keeps reflowing.
                        b.end_line();
                        b.push_margined_block(ir);
                    } else {
                        // A block amid prose: end the current line, then place the
                        // block on its own line(s); a fresh run continues after.
                        // `push_segment` applies no margin, so under `.dtx` prose
                        // reflow a block that does not ride its own interior
                        // margins (or opens on an unmargined line) escapes the
                        // `% ` margin — recorded so the caller abandons the
                        // reflow for this paragraph.
                        b.end_line();
                        b.note_margin_escape();
                        b.push_segment(ir);
                    }
                    line_all_commands = true;
                    line_has_content = false;
                    prev_was_block = true;
                    // Non-inline environments and display math are complete structural
                    // blocks, so prose after their closers starts a fresh line whether
                    // the source gap was a space or newline. An inline environment
                    // leaves its line open: breaking a glued suffix would materialize
                    // a TeX space token (issue #209). Restrict display math to
                    // genuine prose: the `Statement` path also lowers opaque group
                    // bodies, where breaking a glued suffix would add a meaningful
                    // space token. A block-level statement whose lowering forced a
                    // break (a `%` bound to it as a `DOC_COMMENT`, a comment inside a
                    // title, a multi-line `\title` body) closes its line for the same
                    // reason. Everything else that lands here — an un-signatured
                    // command, a glued block command, `\input`'s bare filename shape
                    // — leaves the line open so following content can ride it (the
                    // `after_block` paths).
                    let display_math_closes_prose = child.kind() == SyntaxKind::DISPLAY_MATH
                        && matches!(kind, ReflowKind::Prose | ReflowKind::ProseArg | ReflowKind::AlignmentBody);
                    let environment_closes_line = child.kind() == SyntaxKind::ENVIRONMENT
                        && !cx.signatures.environment_at(child).is_some_and(|sig| sig.inline);
                    prev_block_closes_line = is_block_stmt
                        || section_label_closes_line
                        || environment_closes_line
                        || display_math_closes_prose;
                    if is_section_boundary && !next_nontrivia_is_label(&elements, idx, cx) {
                        b.separate_section();
                    } else if section_label_closes_line {
                        if next_nontrivia_is_label(&elements, idx, cx) {
                            b.pending_sep = Ir::hard_line();
                        } else {
                            b.separate_section();
                        }
                    }
                } else {
                    // A block-level `COMMAND` keeps the line command-only; an inline
                    // command (`\citep`, `\ref`, …) is running-text content, as is any
                    // other inline node (math, an inline group), and disqualifies it.
                    if after_block && !after_block_closed {
                        // Same as the token arm: content still on the previous
                        // block's last physical line rides it.
                        b.append_to_last_line(ride_after_block(ir, after_block_gap));
                        prev_was_block = true;
                    } else if child.kind() == SyntaxKind::COMMAND
                        && let Some(placement) = command_citation_placement(child, cx)
                        && placement != CitationPlacement::Textual
                    {
                        b.push_trailing_citation(ir, &source, placement);
                    } else {
                        b.push_atom_piece(ir, &source);
                    }
                    line_has_content = true;
                    line_all_commands &=
                        child.kind() == SyntaxKind::COMMAND && !command_is_inline(child, cx);
                }
            }
        }
        idx += 1;
    }
    let escaped = b.margin_escaped;
    (b.finish(), escaped)
}

/// Collect the guard-led physical line starting at `elements[*idx]` (a `GUARD`,
/// always at column 0) as one byte-faithful, single-line segment, advancing
/// `*idx` past the line's content; the terminating newline run is left for the
/// caller's trivia arm. Trailing inline whitespace before that newline is
/// skipped (the formatter never emits trailing whitespace — a trivia-only
/// change). Returns `None` with `*idx` untouched when the line cannot be
/// isolated — an element on it spans a newline, or its lowering carries a
/// forced break — in which case the caller records a margin escape.
fn collect_guard_line(elements: &[SyntaxElement], idx: &mut usize, cx: LowerCtx<'_>) -> Option<Ir> {
    let mut end = *idx;
    while let Some(element) = elements.get(end) {
        let spans_lines = match element {
            SyntaxElement::Token(t) => t.text().contains('\n'),
            SyntaxElement::Node(n) => n.text().contains_char('\n'),
        };
        if spans_lines {
            if matches!(element, SyntaxElement::Token(t) if is_collapsible_trivia(t.kind())) {
                break;
            }
            return None;
        }
        end += 1;
    }
    let mut last = end;
    while last > *idx
        && matches!(&elements[last - 1], SyntaxElement::Token(t) if is_collapsible_trivia(t.kind()))
    {
        last -= 1;
    }
    let ir = Ir::concat(lower_element_stream(
        elements[*idx..last].iter().cloned(),
        cx,
    ));
    if ir.contains_forced_break() {
        return None;
    }
    *idx = end;
    Some(ir)
}

/// Lower a `PARAGRAPH` that overlaps an expl3 region. The paragraph is split at the
/// `\ExplSyntaxOn`/`Off` toggles into maximal in-region and out-of-region runs;
/// each in-region run lays out as expl3 code ([`lower_expl_code`]), each out-of-region
/// run keeps the ordinary prose/stream treatment. The common case — a whole
/// paragraph inside a region (a `.sty`/`.dtx` body, or a blank-line-separated
/// `\ExplSyntaxOn…Off` block) — is a single in-region run. Runs are joined by a hard
/// line break (a region boundary always begins a fresh line).
/// Prefix `ir` with the single space that separated it from the block segment it
/// rides, when the source had one. Directly-glued content (`\end{center}%`) stays
/// glued — the missing space is the space-suppression idiom.
fn ride_after_block(ir: Ir, gap: bool) -> Ir {
    if gap {
        Ir::concat([Ir::verbatim(" "), ir])
    } else {
        ir
    }
}

/// Whether an element run carries a `.dtx` documentation margin or docstrip guard
/// anywhere inside it. Both pin to column 0, so a run containing one is never
/// *generic*-prose-reflowed (see [`lower_expl_paragraph`]); a margined run may
/// still reflow under the `% ` margin when [`dtx_run_reflows_safely`] holds.
/// Always false outside the `.dtx` lexer config, where neither token kind exists.
fn run_carries_doc_margin(run: &[SyntaxElement], cx: LowerCtx<'_>) -> bool {
    if !cx.is_dtx {
        return false;
    }
    run.iter().any(|element| match element {
        SyntaxElement::Token(t) => {
            matches!(t.kind(), SyntaxKind::DOC_MARGIN | SyntaxKind::GUARD)
        }
        SyntaxElement::Node(n) => contains_doc_margin(n, cx),
    })
}

/// Slice analogue of [`dtx_paragraph_starts_margined`] for an out-of-region expl3
/// run: the run's first non-trivia token (descending into nodes) must be a
/// `DOC_MARGIN`. Deliberately without the [`margin_precedes_on_line`] backward
/// walk — `prev_token` would cross the run boundary into the previous run's
/// byte-faithful output, and a margin owned there must not count (the reflow
/// would re-emit a second `% ` onto the same physical line).
fn dtx_run_starts_margined(run: &[SyntaxElement]) -> bool {
    for element in run {
        let first = match element {
            SyntaxElement::Token(t) if is_collapsible_trivia(t.kind()) => continue,
            SyntaxElement::Token(t) => Some(t.clone()),
            SyntaxElement::Node(n) => n
                .descendants_with_tokens()
                .filter_map(|e| e.into_token())
                .find(|t| !is_collapsible_trivia(t.kind())),
        };
        if let Some(token) = first {
            return token.kind() == SyntaxKind::DOC_MARGIN;
        }
    }
    false
}

/// Run analogue of [`dtx_doc_paragraph_reflows_safely`] for a doc-margined
/// out-of-region expl3 run: opening on a margined line
/// ([`dtx_run_starts_margined`]), unstructured, and the speculative reflow keeps
/// every line under the `% ` margin. Three deliberate differences from the
/// paragraph gate. "Unstructured" *admits* a margin-framed `macrocode` chunk as
/// a direct run element — such a run is exactly how a doc-layer paragraph
/// overlaps an expl3 region (the chunk bodies are the region; the doc lines
/// around them are the out-of-region rest), and the chunk commits raw behind
/// its byte-exact source frame lead ([`dtx_env_line_lead`]), so docstrip's
/// literal `%    \begin{macrocode}` match survives. Any other environment in
/// the run — or around it, like [`dtx_paragraph_reflows`]'s ancestor half
/// (prose inside a `% \begin{macro}` doc block keeps its authored `%    `
/// margins; reflowing structured doc content stays out of scope) — still
/// declines. And unmemoized: nothing asks twice for a run
/// ([`margin_floats_into_paragraph`] consults only `PARAGRAPH` nodes), so each
/// run is probed at most once.
fn dtx_run_reflows_safely(run: &[SyntaxElement], cx: LowerCtx<'_>) -> bool {
    if !dtx_run_starts_margined(run) {
        return false;
    }
    let structured = run.iter().any(|element| match element {
        SyntaxElement::Node(n) if is_margin_framed_macrocode(n) => false,
        SyntaxElement::Node(n) => n.descendants().any(|d| d.kind() == SyntaxKind::ENVIRONMENT),
        SyntaxElement::Token(_) => false,
    }) || run
        .first()
        .and_then(SyntaxElement::parent)
        .is_some_and(|p| p.ancestors().any(|a| a.kind() == SyntaxKind::ENVIRONMENT));
    if structured {
        return false;
    }
    let probe = LowerCtx {
        dtx_margin_probe: true,
        ..cx
    };
    !reflow_elements_checked(run.iter().cloned(), probe, ReflowKind::DtxProse).1
}

/// Whether `node` is a margin-framed `macrocode`/`macrocode*` chunk — the one
/// environment [`dtx_run_reflows_safely`] admits inside a reflowing run. The
/// curated name set matches [`intersect_macrocode_bodies`]: these are the chunks
/// whose bodies *are* the expl3 regions, so a doc paragraph cannot overlap a
/// region without containing one.
fn is_margin_framed_macrocode(node: &SyntaxNode) -> bool {
    node.kind() == SyntaxKind::ENVIRONMENT
        && Environment::cast(node.clone())
            .is_some_and(|e| matches!(e.name().as_deref(), Some("macrocode" | "macrocode*")))
        && is_margin_framed(node)
}

/// Whether an element is the explicit toggle that closes an expl3 region.
fn is_expl_syntax_off_command(element: &SyntaxElement) -> bool {
    let SyntaxElement::Node(command) = element else {
        return false;
    };
    command.kind() == SyntaxKind::COMMAND
        && command.first_token().is_some_and(|token| {
            token.kind() == SyntaxKind::CONTROL_WORD
                && expl_toggle(token.text()) == Some(ExplToggle::Off)
        })
}

/// The byte-exact line lead of a margin-framed block opening a fresh source
/// line: its preceding siblings walked backward are optional inline
/// `WHITESPACE` then the line's `DOC_MARGIN`. Returns the lead re-lowered as
/// column-0 IR (`%` pinned, whitespace verbatim), or `None` when the block is
/// not led by a directly-preceding margin. Used to commit a `macrocode` chunk
/// raw during `DtxProse` reflow with its source frame margin — docstrip
/// recognizes the literal `%    \begin{macrocode}` line, so the canonical `% `
/// re-emitted for prose must never replace a frame lead.
fn dtx_env_line_lead(node: &SyntaxNode) -> Option<Ir> {
    let mut ws: Option<String> = None;
    let mut prev = node.prev_sibling_or_token();
    if let Some(SyntaxElement::Token(t)) = &prev
        && t.kind() == SyntaxKind::WHITESPACE
        && !t.text().contains('\n')
    {
        ws = Some(t.text().to_string());
        prev = t.prev_sibling_or_token();
    }
    match prev {
        Some(SyntaxElement::Token(t)) if t.kind() == SyntaxKind::DOC_MARGIN => {
            let margin = Ir::column_zero(t.text());
            Some(match ws {
                Some(ws) => Ir::concat([margin, Ir::verbatim(ws)]),
                None => margin,
            })
        }
        _ => None,
    }
}

fn lower_expl_paragraph(node: &SyntaxNode, cx: LowerCtx<'_>) -> Ir {
    let elements: Vec<SyntaxElement> = node.children_with_tokens().collect();
    let mut segments: Vec<Ir> = Vec::new();
    let mut seps: Vec<Ir> = Vec::new();
    let mut i = 0;
    while i < elements.len() {
        // Trivia straddling a run boundary feeds the join separator (one break,
        // or a preserved blank line), never the run itself: the separator
        // already begins the fresh line, so a leading newline inside the run
        // would double it — and each pass would grow a blank line.
        let mut boundary_newlines = 0;
        if !segments.is_empty() {
            while i < elements.len() {
                let SyntaxElement::Token(t) = &elements[i] else {
                    break;
                };
                if !is_collapsible_trivia(t.kind()) {
                    break;
                }
                boundary_newlines += t.text().matches('\n').count();
                i += 1;
            }
            if i >= elements.len() {
                break;
            }
        }
        let in_region = cx.in_expl3_region(elements[i].text_range().start());
        let start = i;
        while i < elements.len()
            && cx.in_expl3_region(elements[i].text_range().start()) == in_region
        {
            i += 1;
        }
        // The region ends at the `\ExplSyntaxOff` token, but a comment after
        // inline whitespace still belongs to that physical line. Lend the
        // comment to the expl3 run for layout only; the parser's region remains
        // catcode-exact, while `lower_expl_code` keeps the comment trailing and
        // prevents it from rebinding to the next command on the following pass.
        if in_region
            && elements[start..i]
                .iter()
                .rev()
                .find(|element| !is_collapsible_trivia_element(element))
                .is_some_and(is_expl_syntax_off_command)
        {
            let mut comment = i;
            while let Some(SyntaxElement::Token(token)) = elements.get(comment)
                && token.kind() == SyntaxKind::WHITESPACE
                && !token.text().contains(['\r', '\n'])
            {
                comment += 1;
            }
            if matches!(
                elements.get(comment),
                Some(SyntaxElement::Token(token)) if token.kind() == SyntaxKind::COMMENT
            ) {
                i = comment + 1;
            }
        }
        // The trailing half of the boundary-trivia rule above: trivia at the end
        // of a run also feeds the separator. Left inside the run, a preserved
        // run-final newline would stack with the hard-line separator into a blank
        // line the next pass keeps growing. (Skipped for an all-trivia run, so
        // `i` always advances.)
        if i < elements.len() {
            let mut end = i;
            while end > start && is_collapsible_trivia_element(&elements[end - 1]) {
                end -= 1;
            }
            if end > start {
                i = end;
            }
        }
        let run = &elements[start..i];
        let guarded_text = (!in_region).then(|| {
            let text = run.iter().map(ToString::to_string).collect::<String>();
            text_is_fully_guarded(&text).then_some(text)
        });
        let ir = if let Some(text) = guarded_text.flatten() {
            // Region subtraction deliberately hands fully guarded `.dtx` lines
            // to the non-expl3 side. They remain byte-faithful there: generic
            // lowering would still collapse gaps when a guard is nested inside
            // a command/group rather than exposed as a direct paragraph token.
            // The leading boundary newline belongs to the separator that opened
            // this run, so reproduce from the first guard-bearing element.
            Ir::verbatim(text.trim_start_matches(['\r', '\n']))
        } else if in_region {
            lower_expl_code(run.iter().cloned(), cx, Statements::Structural)
        } else if cx.wraps_prose() && !run_carries_doc_margin(run, cx) {
            reflow_elements(run.iter().cloned(), cx, ReflowKind::Prose)
        } else if cx.wraps_prose() && dtx_run_reflows_safely(run, cx) {
            // `.dtx` documentation prose between expl3 regions, riding `% `
            // margins: reflow it under the margin like a doc paragraph. Gated
            // exactly like [`lower_dtx_doc_paragraph`] — margined first line, no
            // environments, and the speculative escape probe — so a run the
            // reflow cannot keep under the margin still falls through below.
            reflow_elements(run.iter().cloned(), cx, ReflowKind::DtxProse)
        } else {
            // Either a non-wrapping mode, or `.dtx` documentation-layer text
            // between expl3 regions that the gate above declined: it rides
            // margin-framed `macrocode` frames or unmargined lines that generic
            // prose reflow would relocate off column 0 — a meaning change that
            // leaves the next pass unparseable — so it takes the byte-faithful
            // stream in every wrap mode.
            Ir::concat(lower_element_stream(run.iter().cloned(), cx))
        };
        if !matches!(ir, Ir::Nil) {
            if !segments.is_empty() {
                seps.push(if boundary_newlines >= 2 {
                    Ir::empty_line()
                } else {
                    Ir::hard_line()
                });
            }
            segments.push(ir);
        }
    }
    let mut result = Vec::with_capacity(segments.len().saturating_mul(2));
    for (n, seg) in segments.into_iter().enumerate() {
        if n > 0 {
            result.push(seps[n - 1].clone());
        }
        result.push(seg);
    }
    Ir::concat(result)
}

/// Whether every nonempty physical line in `text` begins with a docstrip guard.
/// Guards are recognized only at column zero, so the spelling is the structural
/// fact the `.dtx` lexer exposes as `GUARD` before any formatter pass can move it.
fn text_is_fully_guarded(text: &str) -> bool {
    let mut content_lines = text.lines().filter(|line| !line.is_empty());
    content_lines
        .next()
        .is_some_and(|line| line.starts_with("%<"))
        && content_lines.all(|line| line.starts_with("%<"))
}

/// How [`lower_expl_code`] finds statement boundaries: **structurally**, from
/// the argspec-arity segmentation ([`segment_expl_statements`] — a call unit
/// per logical line, owned by the formatter, with the authored physical line
/// as the per-statement fallback for underivable heads), or not at all
/// ([`Statements::Ignore`]: within one command's attached arguments, where a
/// newline is inert catcode-9 whitespace and the width fill owns the breaks).
#[derive(Clone, Copy, PartialEq, Eq)]
enum Statements {
    Structural,
    Ignore,
}

/// Lay out a stream of elements known to be inside an expl3 region as expl3 code.
///
/// In an expl3 region TeX catcodes change: source spaces/tabs are **ignored**
/// (catcode 9) and `~` is a **literal space** (catcode 10). Source whitespace is
/// therefore insignificant and the formatter owns layout:
/// - **Statements** are structural *call units*: the boundary map from
///   [`segment_expl_statements`] — a head command plus the arguments its
///   argspec arity consumes — decides where logical lines commit, so
///   `\cs_new:Npn \foo:n #1 {…}` is one statement even though it is several
///   sibling CST nodes, regardless of where the author's newlines fell (one
///   call per line, formatter-owned: same-line calls split, mid-call newlines
///   join). A head with no derivable arity falls back to the authored physical
///   line for that statement (Tier 2; see [`crate::semantic::expl3`]). The exception
///   is *within one command's attached arguments* ([`Statements::Ignore`]),
///   where a newline is inert whitespace and only the width fill breaks —
///   see the `COMMAND` arm in [`lower_node`].
/// - **Inter-token spacing** collapses to a single space (any catcode-9 run is
///   inert, and one space keeps the token boundary so re-lexing never merges two
///   tokens).
/// - **`~`** renders verbatim and introduces a *soft* break (flat: nothing; broken:
///   newline) — a line carrying a tie is wrapped in a group so its ties break
///   together only when the line overflows. A following newline is catcode-9
///   ignored, so the break preserves meaning.
/// - **Brace/optional groups** recurse through [`lower_node`] → [`lower_expl_group`]
///   (an inner block indents; a fitting one stays inline). A multi-line block lands
///   on its own line(s) (Allman), like the block-amid-prose rule in
///   [`reflow_elements`].
fn lower_expl_code(
    elements: impl Iterator<Item = SyntaxElement>,
    cx: LowerCtx<'_>,
    statements: Statements,
) -> Ir {
    let elements: Vec<SyntaxElement> = elements.collect();
    // The statement-boundary map (structural mode only): computed over the very
    // element vector lowered below, so indices align by construction.
    let map = (statements == Statements::Structural).then(|| segment_expl_statements(&elements));
    let mut lines: Vec<Ir> = Vec::new();
    let mut seps: Vec<Ir> = Vec::new();
    let mut pending_sep = Ir::hard_line();

    // The current logical line is built as a Wadler *fill*: `atom` accumulates the
    // glued pieces of the atom in progress; `parts` is the alternating
    // `[atom, sep, atom, …]` the printer fills greedily. `sep_before_next` is the
    // separator to emit before the next atom — `Line` for an inter-token space
    // (flat: one space, keeping the token boundary), `SoftLine` after a `~` (flat:
    // nothing, since the `~` is itself the space).
    let mut atom: Vec<Ir> = Vec::new();
    let mut parts: Vec<Ir> = Vec::new();
    let mut sep_before_next: Option<Ir> = None;

    /// Commit the glued atom in progress as one fill atom, prefixing the pending
    /// separator when it is not the first atom of the line.
    fn flush_atom(atom: &mut Vec<Ir>, parts: &mut Vec<Ir>, sep_before_next: &mut Option<Ir>) {
        if atom.is_empty() {
            return;
        }
        if !parts.is_empty() {
            parts.push(sep_before_next.take().unwrap_or(Ir::Line));
        }
        parts.push(Ir::concat(atom.drain(..)));
        *sep_before_next = None;
    }

    /// The fill a line's `[atom, sep, atom, …]` parts commit as: sticky for a
    /// structural statement, hugging for a fallback (or junk-glued) one — see
    /// `commit_line`. Every early line commit must build its head with this,
    /// not a bare [`Ir::Fill`]: a statement that ends one atom earlier on the
    /// next pass hands the same atoms to a different arm, and the two arms have
    /// to measure them the same way (`xo-place.dtx`).
    fn line_fill(mut parts: Vec<Ir>, sticky: bool) -> Ir {
        if parts.len() == 1 {
            return parts.drain(..).next().unwrap();
        }
        if sticky {
            Ir::StickyFill(parts.into())
        } else {
            Ir::HugFill(parts.into())
        }
    }

    /// Commit the in-progress line (if any) as the next logical line, recording the
    /// pending line separator before it and resetting line state (`sticky` resets
    /// to `true`, the structural default).
    fn commit_line(
        atom: &mut Vec<Ir>,
        parts: &mut Vec<Ir>,
        sep_before_next: &mut Option<Ir>,
        lines: &mut Vec<Ir>,
        seps: &mut Vec<Ir>,
        pending_sep: &mut Ir,
        sticky: &mut bool,
    ) {
        flush_atom(atom, parts, sep_before_next);
        if !parts.is_empty() {
            // A multi-atom *structural* line is a *sticky* fill: greedy, but
            // once a hanging brace argument detonates onto its own line every
            // later argument follows, rather than an empty false-branch gluing
            // back onto the block's short closing `}` line — a glue that is
            // not pass-stable, since where the block's own body breaks is not
            // pass-invariant (issue #94). A *fallback* (or junk-glued) line
            // instead commits as a plain greedy fill: greedy packing is
            // self-fulfilling (each printed line re-segments to a fallback
            // statement that re-fills to itself), while a sticky cascade
            // forces atoms that would fit onto broken lines — a shape the next
            // pass's shorter per-line statements do not reproduce. It is a
            // *hugging* greedy fill ([`Ir::HugFill`]): an atom that detonates
            // is measured by its first line, so it stays glued to the head
            // before it (`\vbox to \Gin@req@height{%`) instead of dropping to
            // a line of its own — a placement that would otherwise be keyed on
            // forced-ness, which is exactly what is not pass-invariant here.
            let line = line_fill(std::mem::take(parts), *sticky);
            seps.push(std::mem::replace(pending_sep, Ir::hard_line()));
            lines.push(line);
        }
        parts.clear();
        *sep_before_next = None;
        *sticky = true;
    }

    // True right after a multi-line block was pushed as its own line, surviving
    // an inline (newline-free) whitespace run: a trailing comment there rides
    // the block's closing line (`}%`, the macro-code continuation idiom).
    // Stranding it would mint a fresh *own-line* comment, which the next parse
    // binds leading into the following command — a different
    // shape, so a different layout: idempotence would break.
    let mut after_block = false;
    // Whether the line in progress commits as a sticky fill (structural
    // statements) or a plain greedy fill (fallback/junk-glued statements) —
    // see `commit_line`. Any fallback-marked element makes its line greedy.
    let mut line_sticky = true;
    let mut idx = 0;
    while idx < elements.len() {
        if let Some(m) = map.as_ref()
            && (m.is_fallback(idx) || m.is_glued(idx))
        {
            line_sticky = false;
        }
        match &elements[idx] {
            // Insignificant whitespace: a gap the boundary map marks ends the
            // logical line, a blank line promotes the next line separator, and
            // any other run is a single (breakable) space before the next atom.
            // The run's newline count is read only for the blank-line promotion
            // and the `after_block` clear — both preserved predicates — never
            // for the boundary itself (trivia-invariant layout).
            SyntaxElement::Token(token) if is_collapsible_trivia(token.kind()) => {
                let run_start = idx;
                let newlines = consume_widened_gap_slice(&elements, &mut idx);
                let boundary = map
                    .as_ref()
                    .is_some_and(|m| run_start > 0 && m.boundary_after(run_start - 1));
                if boundary {
                    after_block = false;
                    commit_line(
                        &mut atom,
                        &mut parts,
                        &mut sep_before_next,
                        &mut lines,
                        &mut seps,
                        &mut pending_sep,
                        &mut line_sticky,
                    );
                    if newlines >= 2 {
                        pending_sep = Ir::empty_line();
                    }
                } else {
                    // A trailing comment glues only to a directly-abutting `}`,
                    // never across a line break (own-line-ness is preserved).
                    if newlines >= 1 {
                        after_block = false;
                    }
                    flush_atom(&mut atom, &mut parts, &mut sep_before_next);
                    // Keep a tie's soft break if one is already pending. A gap
                    // before a recognized head mid-way through a fallback
                    // statement, or any top-level gap of a junk-bearing glued
                    // statement, is an unbreakable literal space: a width wrap
                    // there would move material across a printed line boundary
                    // the next pass segments differently (see
                    // [`StatementMap::glue_before`] and
                    // [`StatementMap::is_glued`]) — the line overflows instead.
                    if sep_before_next.is_none() {
                        // Inside a command node's own stream (`Ignore` — no
                        // boundary map), a gap before a *bare* argument (a
                        // consumed `#`-parameter, relation, or bare command;
                        // anything but a `{…}`/`[…]`) is unbreakable:
                        // `glue_before`'s rationale one level down, and the
                        // house style's — a call breaks before a braced
                        // argument, never between its single-token operands.
                        // A width wrap here would emit a newline inside the
                        // node before a non-group child, the shape the
                        // fallback segmentation reads as a statement boundary
                        // (`semantic::expl3::fallback_line`'s bare-line-break
                        // rule); hardening the gap is what makes that rule's
                        // fixed-point argument total: the formatter can only
                        // ever break a command node before a braced argument.
                        let bare_arg_glue = map.is_none() && elements.get(idx).is_some_and(|el| {
                            !matches!(
                                el,
                                SyntaxElement::Node(n)
                                    if matches!(n.kind(), SyntaxKind::GROUP | SyntaxKind::OPTIONAL)
                            ) && !matches!(
                                el,
                                SyntaxElement::Token(t) if t.kind() == SyntaxKind::COMMENT
                            )
                        });
                        sep_before_next = Some(
                            if bare_arg_glue
                                || map
                                    .as_ref()
                                    .is_some_and(|m| m.glue_before(idx) || m.is_glued(idx))
                            {
                                Ir::verbatim(" ")
                            } else {
                                Ir::Line
                            },
                        );
                    }
                }
                continue;
            }
            // `~`: a literal space. Glue it to the end of the current atom, then
            // close the atom with a soft break (flat: nothing; broken: newline).
            // A tie directly before a recognized head mid-fallback-statement
            // must not break either (`xo-or.dtx`'s `=~ \exp_not:c {…}` trace
            // lines), so that gap renders as nothing (`Nil`, the soft break's
            // flat form).
            SyntaxElement::Token(token) if token.kind() == SyntaxKind::TILDE => {
                after_block = false;
                atom.push(Ir::verbatim(token.text()));
                flush_atom(&mut atom, &mut parts, &mut sep_before_next);
                let next = elements[idx + 1..]
                    .iter()
                    .position(|el| {
                        !matches!(el, SyntaxElement::Token(t) if is_collapsible_trivia(t.kind()))
                    })
                    .map(|off| idx + 1 + off);
                sep_before_next = Some(
                    if next.is_some_and(|n| {
                        map.as_ref()
                            .is_some_and(|m| m.glue_before(n) || m.is_glued(n))
                    }) {
                        Ir::Nil
                    } else {
                        Ir::SoftLine
                    },
                );
            }
            // A comment ends its line (it must terminate the source line). One
            // trailing a multi-line block glues onto the block's closing line
            // (see `after_block` above), spaced when the source spaced it. One
            // trailing code rides the committed line *outside* its width fill
            // — zero-width, rustfmt-style: the line may overflow, but prose
            // length never re-breaks code, and relocating the comment would
            // rebind it as the next statement's leading doc comment on the
            // second pass, changing its attachment. An own-line
            // comment stays its own line.
            SyntaxElement::Token(token) if token.kind() == SyntaxKind::COMMENT => {
                if after_block {
                    let block = lines.pop().expect("after_block implies a pushed line");
                    let spaced = sep_before_next.take().is_some();
                    lines.push(Ir::concat(if spaced {
                        vec![block, Ir::verbatim(" "), Ir::verbatim(token.text())]
                    } else {
                        vec![block, Ir::verbatim(token.text())]
                    }));
                    after_block = false;
                } else if atom.is_empty() && parts.is_empty() {
                    atom.push(Ir::verbatim(token.text()));
                    commit_line(
                        &mut atom,
                        &mut parts,
                        &mut sep_before_next,
                        &mut lines,
                        &mut seps,
                        &mut pending_sep,
                        &mut line_sticky,
                    );
                } else {
                    // A non-empty `atom` means the comment directly abuts it
                    // (any trivia would have flushed the atom); an empty one
                    // means source whitespace preceded, kept as one space.
                    let spaced = atom.is_empty();
                    commit_line(
                        &mut atom,
                        &mut parts,
                        &mut sep_before_next,
                        &mut lines,
                        &mut seps,
                        &mut pending_sep,
                        &mut line_sticky,
                    );
                    let line = lines
                        .pop()
                        .expect("trailing comment follows committed code");
                    let comment = if spaced {
                        format!(" {}", token.text())
                    } else {
                        token.text().to_string()
                    };
                    lines.push(Ir::concat(vec![line, Ir::zero_width(comment)]));
                }
            }
            // A docstrip guard (`%<…>`) is recognized only at column 0, so it must
            // lead its output line. Under `Statements::Ignore` (a command's attached
            // arguments) source newlines are catcode-9 whitespace, so without this a
            // guard between two arguments packs onto the previous line as a trailing
            // `%<…>` comment — losing its guard meaning and re-lexing on the next
            // parse as an ordinary comment that swallows the following argument's
            // braces (issue #78, l3backend-basics.dtx's per-backend `.def` list).
            // Commit the line in progress so the guard opens a fresh one, where
            // `lower_loose_token` pins it to column 0; the following code stays on
            // the guard's line via the ordinary inter-token fill.
            SyntaxElement::Token(token) if token.kind() == SyntaxKind::GUARD => {
                after_block = false;
                commit_line(
                    &mut atom,
                    &mut parts,
                    &mut sep_before_next,
                    &mut lines,
                    &mut seps,
                    &mut pending_sep,
                    &mut line_sticky,
                );
                atom.push(lower_loose_token(token, cx));
            }
            SyntaxElement::Token(token) => {
                after_block = false;
                atom.push(lower_loose_token(token, cx));
            }
            // A command with a bound leading `DOC_COMMENT` (an
            // own-line comment binds forward). Rendered as an opaque block it
            // would strand a blank line after the comment (the comment's own
            // newline stacking with the block separator) and split the
            // statement — a shape the next parse reads differently, breaking
            // idempotence. Instead each comment line commits as its own line
            // and the command's remaining children continue the statement.
            SyntaxElement::Node(child)
                if child.kind() == SyntaxKind::COMMAND
                    && child
                        .first_child()
                        .is_some_and(|c| c.kind() == SyntaxKind::DOC_COMMENT) =>
            {
                after_block = false;
                commit_line(
                    &mut atom,
                    &mut parts,
                    &mut sep_before_next,
                    &mut lines,
                    &mut seps,
                    &mut pending_sep,
                    &mut line_sticky,
                );
                let mut rest: Vec<SyntaxElement> = Vec::new();
                for el in child.children_with_tokens() {
                    match &el {
                        SyntaxElement::Node(n) if n.kind() == SyntaxKind::DOC_COMMENT => {
                            for t in n.children_with_tokens() {
                                if let SyntaxElement::Token(t) = t
                                    && t.kind() == SyntaxKind::COMMENT
                                {
                                    seps.push(std::mem::replace(&mut pending_sep, Ir::hard_line()));
                                    lines.push(Ir::verbatim(t.text()));
                                }
                            }
                        }
                        _ => rest.push(el.clone()),
                    }
                }
                let ir = lower_expl_code(rest.into_iter(), cx, Statements::Ignore);
                if !matches!(ir, Ir::Nil) {
                    atom.push(ir);
                }
            }
            SyntaxElement::Node(child) => {
                after_block = false;
                // A junk-bearing *glued* statement bypasses every layout
                // special: its nodes accumulate as plain atoms joined by the
                // hard separators the trivia arm supplies, so the authored
                // line shape survives verbatim (see [`StatementMap::is_glued`]
                // — a conditional explosion or head-hug here would commit a
                // line mid-statement and strand the junk on a fresh line the
                // next pass segments differently).
                let in_glued = map.as_ref().is_some_and(|m| m.is_glued(idx));
                // R2 ("everything divided up using spaces"): an expl3 function's
                // brace argument written flush against its head
                // (`\clist_count:n{#1}`) gets the house style's space. Synthesized
                // *here*, by flushing the atom exactly as a source gap would, so
                // every branch below sees the state the spaced spelling produces —
                // hang, conditional explosion, trailing-hang candidates — with no
                // second spelling to special-case. Inserting the space is a trivia
                // edit and catcode-safe: in-region source spaces are catcode 9, so
                // the token stream is unchanged (a real space is `~`, catcode 10).
                // Junk-glued statements are exempt: their authored line shape is
                // load-bearing (see above).
                if !in_glued
                    && !atom.is_empty()
                    && child.kind() == SyntaxKind::GROUP
                    && expl_arg_takes_leading_space(child)
                {
                    flush_atom(&mut atom, &mut parts, &mut sep_before_next);
                    sep_before_next = Some(Ir::Line);
                }
                // A *statement-leading* expl3 conditional (`\…:nTF {c} {T} {F}`,
                // nothing on the logical line before it) explodes structurally: the
                // head on its own line, then each `T`/`F` branch on its own line at
                // +6 (R4/R5), regardless of whether it would fit inline. Keyed on the
                // command name's argspec suffix ([`expl3::conditional_branches`]); a
                // conditional used mid-line as a value (`,key = \…:nTF …`, atom or
                // parts non-empty) is not statement-leading and stays on the
                // width-driven head-hug path (issue #71).
                //
                // [`expl_conditional_at`] covers the branches wherever greedy
                // attachment put them — on the head, or on a sibling an `N`/`V` slot
                // handed them to — so the explosion does not depend on an accident of
                // the surrounding tokens, and the unit may span several siblings
                // (hence the `last`-driven resume rather than `idx += 1`).
                if !in_glued
                    && parts.is_empty()
                    && atom.is_empty()
                    && child.kind() == SyntaxKind::COMMAND
                    && let Some((cond_ir, last)) = expl_conditional_at(&elements, idx, cx)
                {
                    seps.push(std::mem::replace(&mut pending_sep, Ir::hard_line()));
                    lines.push(cond_ir);
                    after_block = true;
                    idx = last + 1;
                    continue;
                }
                // A *trailing* expl3 conditional — one used mid-line as a value, with
                // head atoms before it on the line and only trivia after it in the
                // statement — is width-conditional. It stays flat on the line when the
                // whole statement fits (issue #71's `,key = \…:nTF {c} {T} {F}` shape),
                // but when head and conditional together overflow, the head drops to
                // its own line, which makes the conditional *statement-leading* on the
                // next parse so it then explodes unconditionally (R4). Committing head
                // and conditional together as one `group(IfBreak { flat, broken })`
                // — measured by the group's *flat* width (head included, so neither
                // fooled by a branch that detonates internally nor evaluated apart
                // from its head) — makes the passes agree: it fits => `head cond` on
                // one line; it overflows => head on its own line then the R4
                // explosion, which re-parses statement-leading and re-explodes to the
                // identical bytes (idempotency failure, `lthooks.dtx`, issue #96). The
                // `!(…)` guard leaves the statement-leading position to the block above.
                // Suppressed in a *fallback* statement too: a conditional name
                // mid-way through an unrecognized line is data being spliced
                // (`xtemplate`'s `cs_ \str_if_eq:nnT {#1} { global } { g }
                // set:Npn` name assembly), not a call — and whether it sits
                // trailing depends on where the line's junk ends, which is not
                // pass-invariant.
                //
                // **Head-attached branches only** — deliberately, unlike the
                // statement-leading arm above. Mid-statement the conditional is not
                // the head of its own unit; the enclosing segmentation already
                // decided it is an *argument* being passed as a token
                // (`\@@_patch_check:NNnn \cs_if_exist:NTF #1 { undef } {…}`, where
                // `{ undef }` is `\@@_patch_check:NNnn`'s third argument, and
                // `\exp_not:N \…:nTF`). Re-scanning it as a head would resolve
                // "branches" belonging to the outer call and explode them — a misread
                // in every one of the eight latex2e/latex3 sites it reached.
                // [`lower_expl_conditional`] reads only the node's own greedily
                // attached children, so it cannot make that mistake.
                let in_fallback = map.as_ref().is_some_and(|m| m.is_fallback(idx));
                let statement_leading = parts.is_empty() && atom.is_empty();
                if !in_glued
                    && !in_fallback
                    && !statement_leading
                    && child.kind() == SyntaxKind::COMMAND
                    && is_trailing_in_statement(&elements, idx, map.as_ref())
                    && let Some(exploded) = command_name(child)
                        .and_then(|name| expl3::conditional_branches(&name))
                        .and_then(|n| lower_expl_conditional(child, cx, n))
                {
                    // The head↔conditional separator: a space when trivia flushed the
                    // atom (`… \…:nTF`), nothing when the conditional directly abuts
                    // the atom in progress (`…\…:nTF`, no space). `flush_atom`'s own
                    // `sep_before_next` handles the *internal* head joins.
                    let sep = if atom.is_empty() {
                        sep_before_next.take().unwrap_or(Ir::Line)
                    } else {
                        Ir::Nil
                    };
                    flush_atom(&mut atom, &mut parts, &mut sep_before_next);
                    let head = if parts.len() == 1 {
                        parts.drain(..).next().unwrap()
                    } else {
                        Ir::StickyFill(std::mem::take(&mut parts).into())
                    };
                    let flat = Ir::concat(vec![head.clone(), sep, lower_node(child, cx)]);
                    let broken = Ir::concat(vec![head, Ir::hard_line(), exploded]);
                    seps.push(std::mem::replace(&mut pending_sep, Ir::hard_line()));
                    lines.push(Ir::group(Ir::if_break(flat, broken)));
                    after_block = true;
                    idx += 1;
                    continue;
                }
                // A *trailing* greedily-hung `{body}` — a brace group with a head
                // before it on the line and only trivia after it in the statement —
                // whose body is a *multi-command* fill (`\int_gset:Nn \g…_int {…}`)
                // flips K&R->Allman across passes. When the body is authored on one
                // source line and overflows, the ordinary hang path hangs the `{`
                // off the head and lets the fill wrap (K&R) on pass 1; but those
                // wrapped lines re-parse as several statements (source newlines are
                // statement boundaries inside braces), so the body then carries a
                // forced break and detonates Allman on pass 2 — the two passes
                // disagree (idempotency failure, `tagpdf.sty` line 1007,
                // `latex-lab-testphase-bookmark.sty` line 298).
                //
                // Decide instead with a three-candidate all-lines-fit group over the
                // *same* body IR — flat (whole statement on one line), Allman-inline
                // (head on its own line, `{ body }` inline), Allman-broken (`{` on
                // its own line, body wrapped) — keyed on the body's *real* one-line
                // fit rather than its authored line count, so a body that fits picks
                // the same form on every pass and one that wraps picks Allman-broken
                // on every pass. The narrow guards below keep this off the shapes the
                // existing hang path already lays out stably: a single-command or
                // bare-value body (no top-level wrap), a forced-break body
                // (comment/guard/margin, or already multi-statement — plain Allman),
                // and the multi-argument/conditional-branch shapes (a preceding
                // trailing group, or a grouped earlier command) whose head this
                // branch cannot measure as one unit from inside a single argument.
                if child.kind() == SyntaxKind::GROUP
                    && atom.is_empty()
                    && !parts.is_empty()
                    && !expl_group_forces_break(child)
                    && expl_group_body_is_multi_atom(child)
                    && is_trailing_in_statement(&elements, idx, map.as_ref())
                    && !statement_has_preceding_group(&elements, idx, map.as_ref())
                    && !head_command_has_grouped_sibling_arg(child)
                    && let ExplGroupPieces::Pieces {
                        open_ir,
                        body,
                        close_ir,
                        spaced,
                        forced: false,
                    } = expl_group_pieces(child, SyntaxKind::L_BRACE, SyntaxKind::R_BRACE, cx)
                    // A body that already carries a *forced* break (a nested group
                    // detonated on width, or the body holds several statements) can
                    // never be a one-liner: the flat/K&R candidates would render a
                    // multi-line body as a `flat` pick and freeze a hybrid the next
                    // parse re-breaks. Such a body wants the plain Allman block, which
                    // the ordinary hang path already emits stably, so fall through.
                    // This read of the non-pass-invariant forced-break predicate is
                    // safe *because* of that fall-through: the ordinary hang path's
                    // plain Allman is byte-identical to `c_allman_broken`, the
                    // candidate the three-way itself picks for a body that wraps, so
                    // a body flipping soft->forced across passes lands on the same
                    // bytes either way. (`expl_group_body_is_multi_atom` needs two
                    // top-level commands, and two *recognized* commands always mint a
                    // structural boundary, so this arm only ever sees fallback bodies
                    // — gating it on `!in_fallback` would disable it entirely.)
                    && !body.contains_forced_break()
                {
                    let sep = sep_before_next.take().unwrap_or(Ir::Line);
                    flush_atom(&mut atom, &mut parts, &mut sep_before_next);
                    let head = if parts.len() == 1 {
                        parts.drain(..).next().unwrap()
                    } else {
                        Ir::StickyFill(std::mem::take(&mut parts).into())
                    };
                    let space = if spaced { Ir::verbatim(" ") } else { Ir::Nil };
                    // Three candidates, chosen by all-lines-fit — every one measured
                    // with its nested brace groups forced *flat*, so a candidate is
                    // accepted only when its content genuinely lays out that way (no
                    // inner group silently detonating to keep each printed line short,
                    // the K&R hybrid). The bodies are therefore raw (no soft group),
                    // and the structural `HardLine`s alone shape each candidate.
                    //
                    // 1. Flat: the whole statement on one line, `{ body }` glued.
                    // 2. Allman-inline: the head on its own line, `{ body }` inline one
                    //    step under it — taken when the body fits one line there.
                    // 3. Allman-broken: `{` on its own line, the body a further step,
                    //    `}` back — the fallback for a body that wraps.
                    //
                    // Both Allman forms re-parse to a head statement followed by a
                    // statement-leading `{body}`, which the continuation hang branch
                    // below re-emits identically (the inline/broken split is then the
                    // soft group's own width choice), so each is a fixed point. Keying
                    // the K&R↔Allman decision on the body's real one-line fit — not on
                    // how many source lines the body happened to occupy — is what
                    // removes the flip (`tagpdf.sty`,
                    // `latex-lab-testphase-bookmark.sty`).
                    let c_flat = Ir::concat(vec![
                        head.clone(),
                        sep,
                        open_ir.clone(),
                        space.clone(),
                        body.clone(),
                        space.clone(),
                        close_ir.clone(),
                    ]);
                    let c_allman_inline = Ir::concat(vec![
                        head.clone(),
                        Ir::indent(Ir::concat(vec![
                            Ir::hard_line(),
                            open_ir.clone(),
                            space.clone(),
                            body.clone(),
                            space,
                            close_ir.clone(),
                        ])),
                    ]);
                    let c_allman_broken = Ir::concat(vec![
                        head,
                        Ir::indent(Ir::concat(vec![
                            Ir::hard_line(),
                            open_ir,
                            Ir::indent(Ir::concat(vec![Ir::hard_line(), body])),
                            Ir::hard_line(),
                            close_ir,
                        ])),
                    ]);
                    seps.push(std::mem::replace(&mut pending_sep, Ir::hard_line()));
                    lines.push(Ir::conditional_group_all_lines(vec![
                        c_flat,
                        c_allman_inline,
                        c_allman_broken,
                    ]));
                    after_block = true;
                    idx += 1;
                    continue;
                }
                // A brace group that *starts a fresh atom* (nothing glued before
                // it — any trivia flushed the atom) is a *continuation*: it indents
                // one step under its head statement, the l3 house style
                // (`\cs_new:Npn \foo:n #1` / `  { body }`, or `\bool_if:nTF {cond}`
                // / `  { true }`). The step is carried by an `Indent` folded around
                // the break *and the group alone* — never the rest of the line,
                // whose atoms the reparse reads as ordinary base-indent statements
                // when a width break separates them (the group's own internal lines
                // must sit at the deeper level either way, so break and body travel
                // together). This holds identically whether statement boundaries
                // are structural (`Structural`) or absent within one command's
                // attached arguments (`Ignore`), so the rule keys only on the
                // group shape, not the statement mode.
                let hang_group = child.kind() == SyntaxKind::GROUP && atom.is_empty();
                let starts_line = parts.is_empty();
                // Each brace argument breaks on its own merits: the l3styleguide's
                // own example keeps a short branch inline (`{ \module_foo_aux:n
                // { X #2 } }`) beside a multi-line sibling, and l3kernel does the
                // same throughout. A sibling's forced break is not this group's
                // business.
                let ir = lower_node(child, cx);
                // No arm of the forced-break dispatch below fires inside a
                // *fallback* statement: there, forced-ness is not
                // pass-invariant. A fallback statement's extent is the authored
                // physical line (Tier 2), so a width wrap inside the group's own
                // *body* prints newlines the reparse re-segments into several
                // fallback statements — the body's IR gains a `HardLine` and a
                // soft group flips forced on pass 2 (l3kernel `expl3.sty`'s
                // backend `.choices:nn` value; latex2e `lipsum.sty`'s
                // `\int_do_until:nNnn` loop).
                //
                // The forced arm's only effect over the soft path is to *commit
                // the line*, i.e. to force every later atom onto its own line —
                // which is exactly what a `StickyFill` does anyway, since a
                // forced atom's `flat_width` is `None` and
                // `printer::step_fill`'s `remainder_broken` then fires
                // unconditionally. Structural statements and
                // [`Statements::Ignore`] streams are both sticky, so the two
                // paths agree there; a *fallback* line commits as a greedy fill
                // with no cascade, so they disagree, and the sibling after
                // the group glued onto the closing `}` on pass 1 and dropped to
                // its own line on pass 2. Committing mid-statement also falsifies
                // the plain fill's own fixed-point argument (each printed line
                // re-segments to a fallback statement that re-fills to itself,
                // [`StatementMap::is_fallback`]) and silently drops the
                // unbreakable `glue_before` space, since `flush_atom` emits a
                // pending separator only when `parts` is non-empty.
                //
                // Nothing is lost by falling through to the soft branches below.
                // The *hanging group* keeps its own break either way: its
                // `flat_width` is `None`, so its leading gap breaks on every pass
                // at every width — only the *sibling* gap after it is left to the
                // fill, which is the point. The other three arms (head-hug, the
                // abutting-atom glue, the no-head-to-hug commit) differ from the
                // fill only in *committing the line*, and a fallback line's fill
                // hugs ([`Ir::HugFill`], `commit_line`): a detonating atom is
                // measured by its first line, so it stays on the head's line
                // exactly as `group_hug` would have put it, and the atoms after it
                // are left to the fill instead of being stranded — which is what
                // re-glues an authored abutment (`}\@ehc`, `}.`, `}{`) the
                // no-head-to-hug commit used to split.
                debug_assert!(
                    !in_fallback || !line_sticky,
                    "a fallback statement must commit as a hugging fill"
                );
                let forced_dispatch = !in_fallback && ir.contains_forced_break();
                // A junk-bearing glued statement: plain atom accumulation, hard
                // separators, no line commits until the boundary (see above).
                if in_glued {
                    atom.push(ir);
                }
                // A trailing command carrying a block argument, with a head
                // before it on the statement line: glue the head to the command
                // with an unbreakable space and let the command's own internal
                // hang absorb any overflow, so the head stays joined and only
                // the block breaks below. Deliberately checked *before* the
                // forced-break dispatch and applied to soft and forced bodies
                // alike: whether such a body is soft or forced is not
                // pass-invariant (a width wrap inside fallback content mints a
                // statement boundary the reparse reads as a hard break —
                // `expl_forced_block_body_mode`'s `\fp_eval:n` closing paren,
                // l3doc's `\string \indexentry {…} {…}` write bodies), so any
                // dispatch split on it — sticky fill when soft, `group_hug`
                // when forced — renders different bytes on the two passes.
                // Gluing renders identically either way: flat when everything
                // fits, joined head with the block hanging otherwise.
                // Structural streams only — under [`Statements::Ignore`] the
                // nested hang branches already treat soft and forced uniformly.
                else if map.is_some()
                    && child.kind() == SyntaxKind::COMMAND
                    && atom.is_empty()
                    && !parts.is_empty()
                    && is_trailing_in_statement(&elements, idx, map.as_ref())
                    && child.children().any(|c| c.kind() == SyntaxKind::GROUP)
                {
                    let head = line_fill(std::mem::take(&mut parts), line_sticky);
                    // The head↔command separator is the pending gap's *flat*
                    // form: a space for an ordinary inter-token gap, nothing
                    // after a tie (`plus ~\__char_show_code:n {…}` must not
                    // grow a space the next parse does not have).
                    let sep = match sep_before_next.take() {
                        Some(Ir::SoftLine) | Some(Ir::Nil) => Ir::Nil,
                        _ => Ir::verbatim(" "),
                    };
                    seps.push(std::mem::replace(&mut pending_sep, Ir::hard_line()));
                    lines.push(Ir::concat(vec![head, sep, ir]));
                    after_block = true;
                } else if forced_dispatch {
                    if !atom.is_empty() {
                        // A block hanging off a *directly-abutting* atom stays
                        // glued (`\cs_if_exist:NF\tag_if_active:T { … }` with a
                        // multi-line body): committing the atom alone would split
                        // the abutting pair — but on the pass before, when the
                        // body still fit softly, they rendered glued, so the two
                        // passes would never agree. Gluing is the fixed point:
                        // the pair abuts identically on every pass.
                        atom.push(ir);
                        commit_line(
                            &mut atom,
                            &mut parts,
                            &mut sep_before_next,
                            &mut lines,
                            &mut seps,
                            &mut pending_sep,
                            &mut line_sticky,
                        );
                    } else if hang_group {
                        // A multi-line brace group separated from its head by a
                        // space (Allman): end the current line — flushing any head
                        // (`\__kernel…` before its `{T}`) as its own line — then
                        // place the group on its own line(s) hung one step, folding
                        // the line separator into the `Indent` (the seps slot gets
                        // `Nil` so the two stay paired). The run's first line has no
                        // separator to fold and stays at the current level.
                        commit_line(
                            &mut atom,
                            &mut parts,
                            &mut sep_before_next,
                            &mut lines,
                            &mut seps,
                            &mut pending_sep,
                            &mut line_sticky,
                        );
                        if !lines.is_empty() {
                            let sep = std::mem::replace(&mut pending_sep, Ir::hard_line());
                            seps.push(Ir::Nil);
                            lines.push(Ir::indent(Ir::concat(vec![sep, ir])));
                        } else {
                            seps.push(std::mem::replace(&mut pending_sep, Ir::hard_line()));
                            lines.push(ir);
                        }
                    } else if !parts.is_empty() {
                        // Head-hug: a detonating *non-group* child (a command
                        // subtree whose first line is a head atom, e.g. the N-arg
                        // `\__kernel…{T}{F}` of `\cs_if_exist:NTF`) follows a head
                        // on this line, separated by a space. Keep them on one line
                        // when the prefix up to the block's first forced break fits,
                        // letting the block body break below — a rest-aware
                        // `group_hug`, so pass-stable (never the `step_fill` local
                        // cascade that would split a short head off a detonating
                        // trailing block).
                        let head = line_fill(std::mem::take(&mut parts), line_sticky);
                        let sep = sep_before_next.take().unwrap_or(Ir::Line);
                        seps.push(std::mem::replace(&mut pending_sep, Ir::hard_line()));
                        lines.push(Ir::group_hug(Ir::concat(vec![head, sep, ir])));
                    } else {
                        // A multi-line block with no head to hug and no group to
                        // hang: place it on its own line(s) at the current level.
                        commit_line(
                            &mut atom,
                            &mut parts,
                            &mut sep_before_next,
                            &mut lines,
                            &mut seps,
                            &mut pending_sep,
                            &mut line_sticky,
                        );
                        seps.push(std::mem::replace(&mut pending_sep, Ir::hard_line()));
                        lines.push(ir);
                    }
                    after_block = true;
                } else if hang_group && starts_line && !lines.is_empty() {
                    // Line-initial: fold the statement separator in; the
                    // `commit_line` seps slot then carries `Nil`.
                    let sep = std::mem::replace(&mut pending_sep, Ir::Nil);
                    atom.push(Ir::indent(Ir::concat(vec![sep, ir])));
                } else if hang_group && !starts_line {
                    // Mid-statement: if the width fill breaks at this gap, the
                    // group starts a continuation line hung one step. Flat, the
                    // leading `Line` is the single inter-token space (the `Nil`
                    // separator adds nothing). A `{`-led continuation line is
                    // safe in a fallback statement too: the reparse reads it as
                    // a statement-leading group and the continuation-hang fold
                    // below re-indents it identically. (A glued statement never
                    // reaches here — the `in_glued` arm above owns its nodes.)
                    // In a fallback statement this is also the *only* path a
                    // hanging group takes, forced or soft (`forced_dispatch`
                    // above): a forced body's `flat_width` is `None`, so
                    // `step_fill` dispatches this atom `Mode::Break` on every
                    // pass at every width and the leading `Line` breaks — the
                    // same bytes the forced arm would have emitted, minus its
                    // line commit.
                    sep_before_next = Some(Ir::Nil);
                    atom.push(Ir::indent(Ir::concat(vec![Ir::Line, ir])));
                } else {
                    atom.push(ir);
                }
            }
        }
        // A structural boundary whose gap holds no trivia (`\foo:\bar:`
        // abutting, or a statement split out of an authored same-line pair):
        // the trivia arm never sees a run there, so commit here. Re-committing
        // an already-committed line is a no-op.
        if map.as_ref().is_some_and(|m| m.boundary_after(idx)) {
            commit_line(
                &mut atom,
                &mut parts,
                &mut sep_before_next,
                &mut lines,
                &mut seps,
                &mut pending_sep,
                &mut line_sticky,
            );
        }
        idx += 1;
    }
    commit_line(
        &mut atom,
        &mut parts,
        &mut sep_before_next,
        &mut lines,
        &mut seps,
        &mut pending_sep,
        &mut line_sticky,
    );

    let mut result: Vec<Ir> = Vec::with_capacity(lines.len().saturating_mul(2));
    for (i, line) in lines.into_iter().enumerate() {
        if i > 0 {
            result.push(seps[i].clone());
        }
        result.push(line);
    }
    Ir::concat(result)
}

/// Whether an expl3-region brace group *forces* a broken (multi-line) block —
/// independent of width — because its body holds a docstrip guard, a comment, or a
/// `.dtx` margin, the same tokens that drive [`lower_expl_group`]'s block form. Used
/// as the forced-break *self* test in the trailing-hang guards of
/// [`lower_expl_code`] (a body that already forces a break wants the plain Allman
/// block, not the three-candidate hang). Keyed on this width-independent trigger
/// only, so the decision is a pass-stable function of the group's own content —
/// never of width, per the no-sibling-coupling rule (issue #101).
fn expl_group_forces_break(node: &SyntaxNode) -> bool {
    node.descendants_with_tokens()
        .filter_map(SyntaxElement::into_token)
        .any(|t| {
            matches!(
                t.kind(),
                SyntaxKind::COMMENT | SyntaxKind::GUARD | SyntaxKind::DOC_MARGIN
            )
        })
}

/// Whether an expl3 brace group's body holds **two or more** top-level `COMMAND`
/// children — the shape whose K&R hang flips to an Allman block across passes. Two
/// top-level commands (`\int_gset:Nn \g…_int {…}`, `\cmd {x} ~ \cmd {y}`) form a
/// width fill, so when the body overflows it wraps onto several physical lines, which
/// the *next* parse reads as several statements — turning a pass-1 K&R hang (a soft
/// body fill) into a pass-2 Allman block (a forced break), so the two passes disagree
/// (`tagpdf.sty`, `latex-lab-testphase-bookmark.sty`).
///
/// A body with at most one top-level command (`{ \tl_put_right:Ne #3 {…} }` — the
/// command plus its loose `#3`/group arguments, or a bare value) stays one statement:
/// it fits, or it wraps only *inside* that one command's group, never at the top
/// level, so its hang is already pass-stable and is left on the ordinary hang path.
/// Reads only CST shape (top-level `COMMAND` count), no meaning.
fn expl_group_body_is_multi_atom(node: &SyntaxNode) -> bool {
    node.children()
        .filter(|n| n.kind() == SyntaxKind::COMMAND)
        .count()
        >= 2
}

/// Whether an expl3 brace group's body is a *simple run of parameters* — `{#1}`,
/// `{#1#2}`, `{##1}` — the l3styleguide's explicit exception to the
/// divide-with-spaces rule ("With the exception of simple runs of parameter
/// (`{#1}`, `#1#2`, etc.), everything should be divided up using spaces"). Such a
/// run stays tight even inside an expl3-named command's arguments. Outer padding
/// is ignored, so `{ #1 }` normalizes to tight `{#1}`; but any whitespace
/// *between* the parameters, or any non-parameter token, disqualifies the run, so
/// `{ #1 #2 }` and `{ X #2 }` keep the canonical inner spaces (matching the
/// l3styleguide's own worked example). Reads only token kinds and digit text — no
/// meaning, no signature lookup.
fn is_simple_param_run(node: &SyntaxNode) -> bool {
    // Body tokens with the delimiters dropped; a non-token child (a nested group
    // or command) is never a bare parameter run.
    let mut body: Vec<SyntaxToken> = Vec::new();
    for element in node.children_with_tokens() {
        match element {
            SyntaxElement::Token(t) => match t.kind() {
                SyntaxKind::L_BRACE
                | SyntaxKind::R_BRACE
                | SyntaxKind::L_BRACKET
                | SyntaxKind::R_BRACKET => {}
                _ => body.push(t),
            },
            SyntaxElement::Node(_) => return false,
        }
    }
    // Trim the padding whitespace we may be about to remove; any *interior*
    // whitespace survives and disqualifies the run below.
    let is_space =
        |t: &SyntaxToken| matches!(t.kind(), SyntaxKind::WHITESPACE | SyntaxKind::NEWLINE);
    while body.first().is_some_and(is_space) {
        body.remove(0);
    }
    while body.last().is_some_and(is_space) {
        body.pop();
    }
    // A run is `#`s and single-digit indices, adjacent, each digit preceded by a
    // `#` (so `##1` counts, a stray `{1}` or `{ #1 #2 }` does not).
    let mut saw_hash = false;
    let mut prev_hash = false;
    for t in &body {
        match t.kind() {
            SyntaxKind::HASH => {
                saw_hash = true;
                prev_hash = true;
            }
            SyntaxKind::WORD if prev_hash && is_param_digit(t) => {
                prev_hash = false;
            }
            _ => return false,
        }
    }
    saw_hash
}

/// Whether the element at `idx` is the last *meaningful* element of its statement —
/// the boundary map ends the statement at it, or (under [`Statements::Ignore`],
/// where `map` is `None`) only collapsible trivia follows it in the stream. Used
/// to gate the trailing-conditional width-conditional lowering in
/// [`lower_expl_code`]: a conditional with content after it on the same statement
/// is not a clean trailing value, so it stays on the ordinary fill path. A `~`
/// (`TILDE`) or comment is not collapsible trivia, so it counts as following
/// content.
fn is_trailing_in_statement(
    elements: &[SyntaxElement],
    idx: usize,
    map: Option<&StatementMap>,
) -> bool {
    if let Some(m) = map
        && m.boundary_after(idx)
    {
        return true;
    }
    for element in &elements[idx + 1..] {
        match element {
            SyntaxElement::Token(t) if is_collapsible_trivia(t.kind()) => {}
            _ => return false,
        }
    }
    true
}

/// Whether an *earlier argument* of `child`'s owning command is a brace or
/// bracket group — the mark of a multi-argument call
/// (`\prop_get:NnNTF \g…_prop {#2} \l…_tl {branch}`) rather than a plain
/// `\cmd \target {body}` hang. The trailing-hang three-way sees only the
/// owning command's [`Statements::Ignore`] stream from `child` onward, so it
/// cannot tell the two apart without this look at the earlier children.
///
/// A recognized call owns every argument its argspec consumes, so earlier
/// grouped material appears among `child`'s preceding siblings. A node's
/// children form one statement.
fn head_command_has_grouped_sibling_arg(child: &SyntaxNode) -> bool {
    let Some(owner) = child.parent() else {
        return false;
    };
    // Only an *attached* argument has a head with sibling arguments: a
    // stream-level group's parent is the container itself, which is not a
    // call whose earlier slots could have consumed a group.
    if owner.kind() != SyntaxKind::COMMAND {
        return false;
    }
    let child_start = child.text_range().start();
    owner
        .children_with_tokens()
        .take_while(|el| el.text_range().start() < child_start)
        .any(|el| matches!(el.kind(), SyntaxKind::GROUP | SyntaxKind::OPTIONAL))
}

/// Whether a `GROUP`/`OPTIONAL` sibling precedes the element at `idx` within its
/// statement (back to the previous boundary in the map, or to the stream start
/// under [`Statements::Ignore`], where `map` is `None`). Used to keep the
/// trailing-hang three-way off a group that is *one of several* trailing brace
/// groups — the branch list of a conditional call whose N/V argument broke greedy
/// attachment (`\prop_get:NnNTF \g…_prop {#2} \l…_tl {T} {F}`), or any
/// multi-argument shape — where the existing hang path already lays the branches
/// out stably. A lone trailing group (`\l…_tl {body}`, `\bool_if:NF\…_bool
/// {body}`) has no preceding group and still qualifies.
fn statement_has_preceding_group(
    elements: &[SyntaxElement],
    idx: usize,
    map: Option<&StatementMap>,
) -> bool {
    for j in (0..idx).rev() {
        // A boundary after `j` puts `j` in the previous statement.
        if let Some(m) = map
            && m.boundary_after(j)
        {
            return false;
        }
        match &elements[j] {
            SyntaxElement::Node(n)
                if matches!(n.kind(), SyntaxKind::GROUP | SyntaxKind::OPTIONAL) =>
            {
                return true;
            }
            _ => {}
        }
    }
    false
}

/// Whether an expl3-region group's *flat* form carries the l3 house style's
/// canonical inner spaces (`{ value }`, per the l3styleguide) or stays tight
/// (`{parbox/after}`). Spaced when the group is the attached argument of an
/// expl3-*named* command — the name contains `_` or `:`, a purely lexical fact
/// (no signature or meaning lookup) — or a bare code block (an expl3 function
/// body). Tight when it belongs to an embedded LaTeX2e-named command
/// (`\UseTaggingSocket`, `\@parboxto`), whose authors write tight braces; the
/// house style governs expl3 functions, not 2e code that happens to sit inside
/// a region. Tight, too, for a *simple run of parameters* ([`is_simple_param_run`]),
/// the l3styleguide's own exception (`{#1}`, `#1#2`), regardless of the command.
fn expl_group_is_spaced(node: &SyntaxNode) -> bool {
    if is_simple_param_run(node) {
        return false;
    }
    let Some(parent) = node.parent() else {
        return true;
    };
    if parent.kind() != SyntaxKind::COMMAND {
        return true;
    }
    match command_name(&parent) {
        Some(name) => name.contains('_') || name.contains(':'),
        None => true,
    }
}

/// Whether an expl3-region brace group takes the l3 house style's space *before*
/// its opening brace, so a flush-written `\clist_count:n{#1}` is respaced to
/// `\clist_count:n {#1}`. True for the attached argument of an expl3-*named*
/// command — the name contains `_` or `:`, the same purely lexical fact
/// [`expl_group_is_spaced`] reads (no signature or meaning lookup).
///
/// Deliberately **not** subject to the *simple run of parameter* exception: that
/// exception governs a group's *inner* padding (`{#1}`, never `{ #1 }`), not the
/// gap before it. l3kernel writes the space either way — a sweep of its `.dtx`
/// sources counts 2883 spaced against 9 glued for parameter-run arguments alone,
/// and 8447 against 14 over all expl3-named heads.
///
/// False for an embedded LaTeX2e-named command (`\eqref{#1}`,
/// `\ProvidesExplPackage{demo}{…}`), whose authors write flush braces and whom the
/// house style does not govern; upstream is genuinely mixed there, so the authored
/// gap stands.
fn expl_arg_takes_leading_space(node: &SyntaxNode) -> bool {
    node.parent()
        .filter(|parent| parent.kind() == SyntaxKind::COMMAND)
        .and_then(|parent| command_name(&parent))
        .is_some_and(|name| name.contains('_') || name.contains(':'))
}

/// Lower a brace `{…}` or optional `[…]` group inside an expl3 region as a code
/// block: the body lays out as expl3 code ([`lower_expl_code`]) indented one step,
/// the whole wrapped in a soft [`Ir::group`] so it stays inline when it fits —
/// `{ body }` with canonical inner spaces for an expl3 function's argument or a
/// bare code block, tight `{body}` for an embedded 2e-named command's argument
/// ([`expl_group_is_spaced`]) — and detonates to an indented block when the body
/// spans lines or overflows. The inline-vs-block decision is width/structure
/// driven (never source newlines), keeping reformatting idempotent. Mirrors
/// [`lower_prose_group`] but recurses into expl3 code.
///
/// A group breaks only on its own body: a *sibling* argument's forced break is none
/// of its business. The l3styleguide's own worked example keeps a short true-branch
/// inline (`{ \module_foo_aux:n { X #2 } }`) beside a multi-line false-branch block,
/// and l3kernel follows that throughout.
fn lower_expl_group(
    node: &SyntaxNode,
    open: SyntaxKind,
    close: SyntaxKind,
    cx: LowerCtx<'_>,
) -> Ir {
    let (open_ir, body, close_ir, spaced, has_comment) =
        match expl_group_pieces(node, open, close, cx) {
            ExplGroupPieces::Assembled(ir) => return ir,
            ExplGroupPieces::Pieces {
                open_ir,
                body,
                close_ir,
                spaced,
                forced,
            } => (open_ir, body, close_ir, spaced, forced),
        };
    // A forced block gets hard boundary separators *in-shape*, so
    // `propagate_breaks` marks the group `expand` and the printer lays the
    // body out in break mode — never the K&R hybrid of a flat-dispatched
    // concat (issue #97, `l3auxdata.dtx`). Otherwise the flat boundary is a
    // space (l3 house style) or nothing (tight); both break identically.
    let boundary = if has_comment {
        Ir::hard_line()
    } else if spaced {
        Ir::Line
    } else {
        Ir::SoftLine
    };
    Ir::group(Ir::concat([
        open_ir,
        Ir::indent(Ir::concat([boundary.clone(), body])),
        boundary,
        close_ir,
    ]))
}

/// The result of decomposing an expl3 brace group into its layout pieces; see
/// [`expl_group_pieces`].
enum ExplGroupPieces {
    /// A special-cased shape ([`expl_group_pieces`] resolved it fully): an empty
    /// body, with or without a glued lead comment. The caller uses the assembled
    /// `Ir` verbatim and does *not* get the three-candidate hang treatment.
    Assembled(Ir),
    /// A body-bearing group split into its head/body pieces so the caller can
    /// reassemble in flat, K&R, or Allman form.
    Pieces {
        /// The opening bracket, plus any glued lead comment.
        open_ir: Ir,
        /// The lowered body (leading/trailing breaks trimmed).
        body: Ir,
        /// The closing bracket.
        close_ir: Ir,
        /// Whether the flat boundary is a space (l3 house style) or tight.
        spaced: bool,
        /// Whether a comment, guard, or `.dtx` margin forces the broken form
        /// regardless of width.
        forced: bool,
    },
}

/// Decompose an expl3 brace `{…}` (or optional `[…]`) group into the pieces
/// [`lower_expl_group`] and the trailing-hang branch in [`lower_expl_code`] share.
/// The empty-body and glued-lead-comment shapes have bespoke, already-stable
/// assembly, so they are returned pre-assembled as [`ExplGroupPieces::Assembled`];
/// every body-bearing group is returned as [`ExplGroupPieces::Pieces`] for the
/// caller to lay out flat, K&R, or Allman.
fn expl_group_pieces(
    node: &SyntaxNode,
    open: SyntaxKind,
    close: SyntaxKind,
    cx: LowerCtx<'_>,
) -> ExplGroupPieces {
    let mut open_ir = Ir::Nil;
    let mut close_ir = Ir::Nil;
    let mut body_elements: Vec<SyntaxElement> = Vec::new();
    for element in node.children_with_tokens() {
        match &element {
            SyntaxElement::Token(t) if t.kind() == open && matches!(open_ir, Ir::Nil) => {
                open_ir = Ir::verbatim(t.text());
            }
            SyntaxElement::Token(t) if t.kind() == close => {
                close_ir = Ir::verbatim(t.text());
            }
            _ => body_elements.push(element),
        }
    }
    // A comment opening the body (`{%`, the macro-code continuation idiom, with
    // inline whitespace tolerated) rides the opening bracket's line — it never
    // gets a line of its own. Inside a region the masked newline is inert
    // catcode-9 whitespace either way; this just keeps the authored shape.
    let mut lead_comment = Ir::Nil;
    {
        let mut i = 0;
        let mut spaced = false;
        while let Some(SyntaxElement::Token(t)) = body_elements.get(i) {
            match t.kind() {
                SyntaxKind::WHITESPACE => {
                    spaced = true;
                    i += 1;
                }
                SyntaxKind::COMMENT => {
                    lead_comment = if spaced {
                        Ir::concat([Ir::verbatim(" "), Ir::verbatim(t.text())])
                    } else {
                        Ir::verbatim(t.text())
                    };
                    body_elements.drain(..=i);
                    break;
                }
                _ => break,
            }
        }
    }
    // A body holding a `%` comment can never flatten: inline, everything after
    // the comment on the line — the closing bracket included — would be
    // swallowed into the comment on the next parse (`{ …% }`). The lead comment
    // glued to the opening bracket forces the broken form the same way.
    //
    // A docstrip guard (`%<…>`) or `.dtx` margin (`%`) is line-oriented the same
    // way, and worse: it is only recognized at line start, so flattening it into
    // `{ %<trace> … }` re-lexes it as an *ordinary* `%` comment that swallows the
    // rest of the line — braces included — unbalancing the enclosing group on the
    // next parse (issue #61). Force the broken form so each rides its own line,
    // where `lower_loose_token` pins it to column 0 and it stays a guard/margin.
    let has_lead_comment = !matches!(lead_comment, Ir::Nil);
    let has_comment = has_lead_comment
        || node
            .descendants_with_tokens()
            .filter_map(|e| e.into_token())
            .any(|t| {
                matches!(
                    t.kind(),
                    SyntaxKind::COMMENT | SyntaxKind::GUARD | SyntaxKind::DOC_MARGIN
                )
            });
    let open_ir = Ir::concat([open_ir, lead_comment]);
    let body = trim_trailing_break(trim_leading_break(lower_expl_code(
        body_elements.into_iter(),
        cx,
        Statements::Structural,
    )));
    if matches!(body, Ir::Nil) {
        // A glued lead comment owns the rest of its line, so an empty body
        // still breaks before the closing bracket (`{%…` + `}` on its own
        // line), never `{%…}` with the bracket swallowed.
        return ExplGroupPieces::Assembled(if has_lead_comment {
            Ir::concat([open_ir, Ir::hard_line(), close_ir])
        } else {
            Ir::concat([open_ir, close_ir])
        });
    }
    // A glued lead comment on a body-bearing group still forces the broken form
    // (it owns the rest of the opening line) and, more to the point, means the
    // three-candidate trailing-hang treatment must not apply — the flat/K&R
    // candidates would put content after the comment on the same line. Route it
    // through the forced-break path (`forced` covers both the lead comment and
    // any interior comment/guard/margin).
    ExplGroupPieces::Pieces {
        open_ir,
        body,
        close_ir,
        spaced: expl_group_is_spaced(node),
        forced: has_comment,
    }
}

/// Lower a statement-leading expl3 conditional (`\…:nTF {c} {T} {F}`, its branch
/// count recognized by [`expl3::conditional_branches`]) to the l3styleguide's
/// exploded shape (R4/R5): the head and any leading arguments on one line, then
/// each of the `n` trailing brace branches on its own line hung one indent step
/// (+2 relative to the head, so +6 inside a +4 body; a multi-line branch nests its
/// interior +8). The break is **unconditional** — width-independent, so it is
/// pass-stable: the exploded output re-parses to the same greedy `COMMAND` (brace
/// arguments attach across the inserted newlines) in statement position and
/// re-explodes identically. Each branch is a *soft* [`lower_expl_group`], so a
/// short branch stays `{ … }` inline on its line and a long one breaks internally.
///
/// An annotated branch keeps its comment: a trailing `%` rides the branch's own
/// line, an own-line one stays on its own line between branches.
///
/// Returns `None` unless the command's own last `n` argument children are `GROUP`s
/// with only trivia and comments beyond them — i.e. the branches actually attach to
/// the conditional. An `:NTF`/`:nNnTF` whose single-token (`N`/`V`/operator)
/// argument breaks greedy attachment leaves the branch groups on a following
/// sibling, which this node-local scan cannot see;
/// [`lower_expl_conditional_unit`] picks those up from the resolved call unit, and
/// [`expl_conditional_at`] tries the two in order.
fn lower_expl_conditional(cmd: &SyntaxNode, cx: LowerCtx<'_>, n: usize) -> Option<Ir> {
    let children: Vec<SyntaxElement> = cmd.children_with_tokens().collect();
    let group_positions: Vec<usize> = children
        .iter()
        .enumerate()
        .filter(|(_, e)| e.as_node().is_some_and(|nd| nd.kind() == SyntaxKind::GROUP))
        .map(|(i, _)| i)
        .collect();
    if group_positions.len() < n {
        return None;
    }
    // The last `n` groups are the branches; everything before the first of them is
    // the head (control word, leading brace/operator args, and their trivia).
    let first_branch = group_positions[group_positions.len() - n];
    // The branches must be the *trailing* arguments: nothing but groups, trivia, and
    // comments may sit from the first branch onward, else this is not a clean
    // conditional call and the width path is safer. Comments are admitted (rather
    // than bailing to the width path) because annotated branches are ordinary l3
    // style — `{ \exp_not:n { equations~ } } % You might prefer \nobreakspace to ~`
    // — and the bail cost the whole exploded shape for one `%` (issue #101).
    for element in &children[first_branch..] {
        match element {
            SyntaxElement::Node(nd) if nd.kind() == SyntaxKind::GROUP => {}
            SyntaxElement::Token(t)
                if is_collapsible_trivia(t.kind()) || t.kind() == SyntaxKind::COMMENT => {}
            _ => return None,
        }
    }
    let branch_lines = expl_branch_lines(&children[first_branch..], cx)?;
    let head = trim_trailing_break(lower_expl_code(
        children[..first_branch].iter().cloned(),
        cx,
        Statements::Ignore,
    ));
    Some(Ir::concat(std::iter::once(head).chain(branch_lines)))
}

/// The exploded branch lines of a conditional: each `GROUP` in `tail` on its own
/// line hung one indent step, as a *soft* [`lower_expl_group`] so a short branch
/// stays `{ … }` inline on its line and a long one breaks internally.
///
/// `None` when `tail` holds anything but groups, collapsible trivia, and comments
/// — the branch list is then not clean and the width-driven path is safer. Shared
/// by [`lower_expl_conditional`] (branches attached to the head node) and
/// [`lower_expl_conditional_unit`] (branches greedy attachment gave to a sibling),
/// so the two spellings of the same layout cannot drift.
fn expl_branch_lines(tail: &[SyntaxElement], cx: LowerCtx<'_>) -> Option<Vec<Ir>> {
    // Comments are admitted (rather than bailing to the width path) because
    // annotated branches are ordinary l3 style — `{ \exp_not:n { equations~ } } %
    // You might prefer \nobreakspace to ~` — and the bail cost the whole exploded
    // shape for one `%` (issue #101).
    for element in tail {
        match element {
            SyntaxElement::Node(nd) if nd.kind() == SyntaxKind::GROUP => {}
            SyntaxElement::Token(t)
                if is_collapsible_trivia(t.kind()) || t.kind() == SyntaxKind::COMMENT => {}
            _ => return None,
        }
    }
    let mut parts = Vec::new();
    // Trivia seen since the last emitted branch or comment: `gap` renders as the one
    // space before a trailing comment, `own_line` (a newline in that run) keeps an
    // own-line comment on its own line. Own-line-ness is a *preserved* predicate, so
    // reading it is trivia-invariant and stable in both directions — a trailing
    // comment re-parses trailing, an own-line one re-parses own-line. Relocating
    // either way would change its attachment.
    let mut gap = false;
    let mut own_line = false;
    for element in tail {
        match element {
            SyntaxElement::Node(nd) => {
                let group = lower_expl_group(nd, SyntaxKind::L_BRACE, SyntaxKind::R_BRACE, cx);
                parts.push(Ir::indent(Ir::concat([Ir::hard_line(), group])));
                (gap, own_line) = (false, false);
            }
            SyntaxElement::Token(t) if t.kind() == SyntaxKind::COMMENT => {
                let comment = Ir::verbatim(t.text());
                parts.push(if own_line {
                    Ir::indent(Ir::concat([Ir::hard_line(), comment]))
                } else if gap {
                    Ir::concat([Ir::verbatim(" "), comment])
                } else {
                    comment
                });
                (gap, own_line) = (false, false);
            }
            SyntaxElement::Token(t) => {
                gap = true;
                own_line |= t.kind() == SyntaxKind::NEWLINE;
            }
        }
    }
    Some(parts)
}

/// The exploded form of the expl3 conditional headed by `elements[idx]`, plus
/// the index of the unit's last element (the caller resumes at `last + 1`).
///
/// Node-local: arity attachment gives a recognized conditional its branches as
/// the head's own trailing groups, so [`lower_expl_conditional`] covers every
/// resolvable shape — including a head whose *arity* is underivable while its
/// branch count is not (`:wTF`), when greed happened to attach the branches.
/// Returns `None` to leave the call on the width-driven path.
fn expl_conditional_at(
    elements: &[SyntaxElement],
    idx: usize,
    cx: LowerCtx<'_>,
) -> Option<(Ir, usize)> {
    let node = elements.get(idx)?.as_node()?;
    if node.kind() != SyntaxKind::COMMAND {
        return None;
    }
    let n = expl3::conditional_branches(&command_name(node)?)?;
    let ir = lower_expl_conditional(node, cx, n)?;
    Some((ir, idx))
}

/// Lower a single loose token (one not collapsed into a trivia run) to inline IR.
/// A `.dtx` documentation margin (`DOC_MARGIN`) or docstrip guard (`GUARD`) pins
/// to column 0 via [`Ir::column_zero`] so docstrip's left-margin anchor survives
/// any surrounding LaTeX nesting; every other token splices verbatim. These tokens
/// only exist under the `.dtx` lexer config, so non-`.dtx` lowering is unaffected.
fn lower_loose_token(token: &SyntaxToken, cx: LowerCtx<'_>) -> Ir {
    if cx.in_dtx_doc_region && token.kind() == SyntaxKind::DOC_MARGIN {
        return Ir::Nil;
    }
    if matches!(token.kind(), SyntaxKind::DOC_MARGIN | SyntaxKind::GUARD) {
        Ir::column_zero(token.text())
    } else if cx.absorbs_control_newline(token) {
        let before = token
            .text()
            .strip_suffix('\n')
            .expect("absorbed control symbol must end in a newline");
        Ir::verbatim(before)
    } else {
        Ir::verbatim(token.text())
    }
}

/// Lower a stream of elements: child nodes recurse, non-trivia tokens (and the
/// protected `\verb`/verbatim/comment tokens) are emitted verbatim, and maximal
/// runs of `WHITESPACE`/`NEWLINE` trivia are collapsed into a single break
/// primitive by [`classify_trivia`]. Comments deliberately *break* a trivia run
/// (they are content, never collapsed away), so the run on either side is
/// classified independently.
fn lower_element_stream(
    elements: impl Iterator<Item = SyntaxElement>,
    cx: LowerCtx<'_>,
) -> Vec<Ir> {
    let mut out = Vec::new();
    let mut iter = elements.peekable();
    while let Some(element) = iter.next() {
        match element {
            SyntaxElement::Node(child) => out.push(lower_node(&child, cx)),
            // A virtual documentation region owns its physical margins. Drop
            // each one together with the source padding after it; ordinary
            // lowering will regenerate indentation in virtual coordinates.
            SyntaxElement::Token(token)
                if cx.in_dtx_doc_region && token.kind() == SyntaxKind::DOC_MARGIN =>
            {
                while let Some(SyntaxElement::Token(next)) = iter.peek() {
                    if next.kind() == SyntaxKind::WHITESPACE {
                        iter.next();
                    } else {
                        break;
                    }
                }
            }
            // The floated leading `%` of a virtual `.dtx` documentation region
            // belongs to the region wrapper. Drop it and its padding before the
            // generic trivia arm can consume it as an ordinary gap; otherwise the
            // wrapper emits a second margin and comments out the region's `\begin`.
            SyntaxElement::Token(token) if margin_starts_dtx_doc_region(&token, cx) => {
                while let Some(SyntaxElement::Token(t)) = iter.peek() {
                    if t.kind() == SyntaxKind::WHITESPACE {
                        iter.next();
                    } else {
                        break;
                    }
                }
            }
            // Trivia inside a suppressed span is reproduced too, one token at a
            // time rather than collapsed into a `Gap`. Without this the *gaps
            // between* two suppressed siblings would still normalize, so an
            // `off`/`on` region spanning several top-level blocks would keep
            // every block byte-exact and quietly rewrite the seams between them.
            SyntaxElement::Token(token)
                if is_collapsible_trivia(token.kind()) && cx.suppressed(token.text_range()) =>
            {
                out.push(Ir::verbatim(token.text().to_string()));
            }
            SyntaxElement::Token(token) if is_collapsible_trivia(token.kind()) => {
                out.push(classify_trivia(
                    consume_gap_widened(&token, &mut iter),
                    cx.in_alignment_cell,
                ));
            }
            SyntaxElement::Token(token)
                if cx.wraps_prose()
                    && !cx.dtx_margin_probe
                    && token.kind() == SyntaxKind::DOC_MARGIN
                    && margin_floats_into_paragraph(&token, cx) =>
            {
                while let Some(SyntaxElement::Token(t)) = iter.peek() {
                    if t.kind() == SyntaxKind::WHITESPACE {
                        iter.next();
                    } else {
                        break;
                    }
                }
            }
            SyntaxElement::Token(token) => out.push(lower_loose_token(&token, cx)),
        }
    }
    out
}

/// Lower a *prose* element stream under [`WrapMode::Preserve`]: like
/// [`lower_element_stream`], but a newline-free whitespace run — inter-word spacing
/// on a single line — collapses to a single space instead of surviving verbatim.
/// `Preserve` governs *line breaks* only, so intra-line spacing normalizes exactly
/// as it does in every wrapping mode (runs of spaces/tabs are catcode-10 equivalent
/// to one space, so the collapse is meaning-preserving); a run carrying a newline
/// stays a break and the printer still owns the following indentation.
///
/// Reached only for genuine prose — a non-`.dtx` `PARAGRAPH` (see [`lower_node`]) or
/// a list item body (see [`preserve_chunks`]) — after [`flatten_inline_prose`] has
/// spliced inline-prose command bodies (`\emph{…}`) into the run. A child *node*
/// still recurses through [`lower_node`], so an *opaque* brace body (a `\newcommand`
/// definition, any non-inline argument group) keeps its inner spacing byte-for-byte,
/// exactly as under every other mode.
fn lower_prose_stream(elements: impl Iterator<Item = SyntaxElement>, cx: LowerCtx<'_>) -> Vec<Ir> {
    let mut out = Vec::new();
    let mut iter = elements.peekable();
    while let Some(element) = iter.next() {
        match element {
            SyntaxElement::Node(child) => out.push(lower_node(&child, cx)),
            // Tier 2, shared with [`classify_trivia`]: `Preserve` promises the
            // authored line structure survives, so this boundary reads the newline
            // count and reproduces it. Preservation-only, hence its own fixed point.
            SyntaxElement::Token(token) if is_collapsible_trivia(token.kind()) => {
                out.push(match consume_gap_widened(&token, &mut iter).newlines {
                    0 => Ir::verbatim(" "),
                    1 => Ir::hard_line(),
                    _ => Ir::empty_line(),
                });
            }
            SyntaxElement::Token(token) => out.push(lower_loose_token(&token, cx)),
        }
    }
    out
}

/// Lower an `\begin{…} … \end{…}` environment, indenting its body one step. A
/// clean-parse environment is `[BEGIN, body…, END]`: the framing nodes are
/// lowered directly, and the body between them is wrapped in [`Ir::indent`] with
/// a leading [`Ir::hard_line`] (so it starts on its own indented line) and a
/// trailing `hard_line` at the *outer* indent (so `\end` sits flush with
/// `\begin`). All indentation is owned by the printer, so the body's own leading
/// and trailing breaks are trimmed before wrapping — this is what makes
/// re-indentation idempotent. A blank line the author placed against `\begin`/
/// `\end` is preserved as a single blank line (the leading/trailing `hard_line`
/// becomes an [`Ir::empty_line`]); the empty-body case keeps a single break.
///
/// Verbatim-like environments never reach here (their opaque `VERBATIM_BODY`
/// token would be corrupted by reflow); [`lower_node`] routes them to the
/// generic path, which emits the body verbatim.
/// The leading comment-bind run (an own-line `%` run the parser attached as
/// leading children *before* the `BEGIN` node). It is not body: it lowers to its
/// own line(s) above `\begin`, at the environment's own indentation. Returns
/// [`Ir::Nil`] when there is no such run. Shared by every environment lowerer so
/// the bound comment is rendered the same way regardless of body shape.
fn lower_environment_leading(node: &SyntaxNode, cx: LowerCtx<'_>) -> Ir {
    let mut leading: Vec<SyntaxElement> = Vec::new();
    for element in node.children_with_tokens() {
        if matches!(&element, SyntaxElement::Node(c) if c.kind() == SyntaxKind::BEGIN) {
            break;
        }
        leading.push(element);
    }
    if leading.is_empty() {
        Ir::Nil
    } else {
        Ir::concat(lower_element_stream(leading.into_iter(), cx))
    }
}

/// The decomposition every environment lowering starts from: the elements before
/// `\begin` (lowered), the lowered `\begin` header, the raw body elements, and
/// the lowered `\end`. A `%` that trails the `\begin{…}` header on the same
/// source line belongs to that line (the space-suppression idiom), not the body,
/// so it is lifted onto `begin` *here* — once, for every layout path — rather
/// than in each lowering, where a path that forgot the lift would relocate the
/// comment onto its own body line and change its meaning (issue #38). The lifted
/// token still sits (nested) in `body`; consumers drop it by identity — the
/// stream path via [`lower_body_dropping_leading_comment`], the flattening paths
/// via [`is_lifted_comment`].
struct EnvParts {
    leading: Ir,
    begin: Ir,
    body: Vec<SyntaxElement>,
    end: Ir,
    lifted: Option<SyntaxToken>,
    /// How many of `body`'s leading elements came from [`lower_begin`]'s tail —
    /// content the greedy parser attached to `BEGIN` past the end of the header.
    /// They are *in* `body` so no consumer can drop them; the count is what lets
    /// [`lower_env_body`] splice them into the body's first paragraph.
    tail_len: usize,
    /// A shape-proved, unbraced argument that the grammar represents as the
    /// first body token. At present this is the parenthesized size tuple of the
    /// standard `picture` environment.
    body_header_token: Option<SyntaxToken>,
}

/// The body-final `\\<newline>` control symbol, ignoring only indentation on
/// the closer's line. The newline is part of this non-trivia token, not a
/// separate [`SyntaxKind::NEWLINE`], so an environment that unconditionally
/// adds its own closing break would otherwise create a blank paragraph.
fn trailing_control_newline(body: &[SyntaxElement]) -> Option<SyntaxToken> {
    let first = body.first()?.text_range().start();
    let last = body.last()?;
    let mut token = match last {
        SyntaxElement::Node(node) => node.last_token(),
        SyntaxElement::Token(token) => Some(token.clone()),
    }?;

    loop {
        if token.text_range().start() < first {
            return None;
        }
        match token.kind() {
            SyntaxKind::WHITESPACE => token = token.prev_token()?,
            SyntaxKind::CONTROL_SYMBOL if token.text().ends_with('\n') => return Some(token),
            _ => return None,
        }
    }
}

fn split_environment(node: &SyntaxNode, cx: LowerCtx<'_>) -> EnvParts {
    let leading = lower_environment_leading(node, cx);
    let mut begin = Ir::Nil;
    let mut end = Ir::Nil;
    let mut body: Vec<SyntaxElement> = Vec::new();
    let mut tail_len = 0usize;
    let mut seen_begin = false;
    for element in node.children_with_tokens() {
        match &element {
            SyntaxElement::Node(child) if child.kind() == SyntaxKind::BEGIN => {
                seen_begin = true;
                // Content the greedy parser attached past the end of the header
                // leads the body (see [`lower_begin`]). `body` is still empty here
                // — everything before `BEGIN` is `leading` — so extending it now
                // keeps the tail in source order ahead of the real body.
                let parts = lower_begin(child, cx);
                begin = parts.header;
                tail_len = parts.tail.len();
                body.extend(parts.tail);
            }
            SyntaxElement::Node(child) if child.kind() == SyntaxKind::END => {
                end = lower_node(child, cx);
            }
            _ if !seen_begin => {}
            _ => body.push(element),
        }
    }
    let lifted = leading_inline_comment(&body);
    if let Some(comment) = &lifted {
        // Environment layout already ends the header line. A trailing comment
        // must not consume its width and split otherwise fitting arguments.
        begin = Ir::concat([begin, Ir::zero_width(comment.text())]);
    }
    let body_header_token = picture_header_token(node, &body);
    if let Some(token) = &body_header_token {
        begin = Ir::concat([begin, lower_loose_token(token, cx)]);
    }
    EnvParts {
        leading,
        begin,
        body,
        end,
        lifted,
        tail_len,
        body_header_token,
    }
}

/// Return the standard `picture` environment's glued `(width,height)` token.
/// TeX gives this environment an unbraced begin argument, so the generic grammar
/// necessarily places it in the body. The name, adjacency, and tuple shape make
/// the relocation text-falsifiable without claiming that arbitrary body text is
/// an environment argument.
fn picture_header_token(node: &SyntaxNode, body: &[SyntaxElement]) -> Option<SyntaxToken> {
    let environment = Environment::cast(node.clone())?;
    if environment.name().as_deref() != Some("picture") {
        return None;
    }
    let begin = environment.begin()?;
    let paragraph = body.first()?.as_node()?;
    if paragraph.kind() != SyntaxKind::PARAGRAPH {
        return None;
    }
    let token = paragraph.first_token()?;
    (token.kind() == SyntaxKind::WORD
        && begin.syntax().text_range().end() == token.text_range().start()
        && is_picture_tuple(token.text()))
    .then_some(token)
}

fn is_picture_tuple(text: &str) -> bool {
    let mut chars = text.chars().peekable();
    let mut groups = 0usize;
    while chars.peek().is_some() {
        if chars.next() != Some('(') {
            return false;
        }
        let mut comma = false;
        let mut content = false;
        loop {
            match chars.next() {
                Some(',') if !comma && content => {
                    comma = true;
                    content = false;
                }
                Some(')') if comma && content => break,
                Some('(' | ')') | None => return false,
                Some(_) => content = true,
            }
        }
        groups += 1;
    }
    matches!(groups, 1 | 2)
}

/// Whether `el` is the [`EnvParts::lifted`] `\begin`-line comment, compared by
/// token identity so the flattening body consumers (list, alignment grid, math
/// formula) drop exactly the token that was lifted and nothing else.
fn is_lifted_comment(el: &SyntaxElement, lifted: Option<&SyntaxToken>) -> bool {
    lifted.is_some_and(|l| el.as_token() == Some(l))
}

/// Whether `node` (a `PARAGRAPH`) takes the plain prose reflow in [`lower_node`] —
/// the one body path a spliced [`EnvParts::tail_len`] run can join. A `.dtx` doc
/// paragraph re-synthesizes its own `% ` margin per line and an expl3-overlapping
/// one segments into statements; neither admits foreign leading elements, so both
/// keep the concatenated form. Mirrors `lower_node`'s `PARAGRAPH` arms — keep the
/// two in step.
fn paragraph_reflows_as_prose(node: &SyntaxNode, cx: LowerCtx<'_>) -> bool {
    cx.wraps_prose()
        && !is_dtx_doc_paragraph(node)
        && !(cx.any_expl3() && cx.overlaps_expl3(node.text_range()))
}

/// Lower [`EnvParts::body`] through the generic element stream, dropping the
/// lifted `\begin`-line comment when one was taken.
///
/// The leading `tail_len` elements are content greedy attachment gave to `BEGIN`
/// past the end of its header (see [`lower_begin`]); where the body opens with a
/// prose paragraph they are *spliced into its reflow* rather than concatenated
/// ahead of it. That is what makes the relocation invisible to the layout: the
/// source lays out identically whether or not the parser happened to pull the
/// group into `BEGIN` — `\begin{center}\n{\bfseries A}\nmore` reflows onto one
/// line, exactly as it does with a word ahead of the group to keep it in the
/// paragraph.
///
/// Concatenating instead would abut the two with *no separator at all*: the
/// paragraph's own leading newline lives inside the node and its reflow trims it,
/// so `{\bfseries A}` and `more` would run together. That is a space TeX typesets,
/// silently deleted — and invisible to every CST oracle, since whitespace is trivia
/// to them and content to TeX.
fn lower_env_body(
    body: Vec<SyntaxElement>,
    tail_len: usize,
    lifted: bool,
    body_header_token: Option<&SyntaxToken>,
    cx: LowerCtx<'_>,
) -> Ir {
    if let Some(header_token) = body_header_token
        && let Some(SyntaxElement::Node(paragraph)) = body.first()
    {
        let elements = paragraph
            .children_with_tokens()
            .filter(|element| element.as_token() != Some(header_token));
        let first = if cx.wraps_prose() {
            reflow_elements(elements, cx, paragraph_reflow_kind(paragraph, cx))
        } else {
            Ir::concat(lower_element_stream(elements, cx))
        };
        return Ir::concat(
            std::iter::once(first).chain(lower_element_stream(body[1..].iter().cloned(), cx)),
        );
    }
    if tail_len > 0
        && let Some(SyntaxElement::Node(para)) = body.get(tail_len)
        && para.kind() == SyntaxKind::PARAGRAPH
        && paragraph_reflows_as_prose(para, cx)
    {
        let spliced = reflow_elements(
            body[..tail_len]
                .iter()
                .cloned()
                .chain(para.children_with_tokens()),
            cx,
            paragraph_reflow_kind(para, cx),
        );
        let rest = lower_element_stream(body[tail_len + 1..].iter().cloned(), cx);
        return Ir::concat(std::iter::once(spliced).chain(rest));
    }
    // A named math environment can likewise begin with content greedy attachment
    // left inside `BEGIN`, followed by the parser's ordinary `MATH` body wrapper.
    // The math-grid path flattens those siblings together, but a grid containing a
    // non-final multiline cell must fall back here. Keep the recovered prefix in
    // math mode on that fallback: lowering a comment-bearing group generically
    // would reset binary-operator context after the comment and indent its body a
    // full step rather than hanging it one column past `{`.
    if tail_len > 0
        && body
            .get(tail_len)
            .and_then(SyntaxElement::as_node)
            .is_some_and(|node| node.kind() == SyntaxKind::MATH)
    {
        let prefix = lower_math_seq(
            body[..tail_len].iter().cloned(),
            cx,
            MathSpacing::Normal,
            false,
        );
        let rest = lower_element_stream(body[tail_len..].iter().cloned(), cx);
        return Ir::concat(std::iter::once(prefix).chain(rest));
    }
    if lifted {
        lower_body_dropping_leading_comment(body, cx)
    } else {
        Ir::concat(lower_element_stream(body.into_iter(), cx))
    }
}

fn lower_environment(node: &SyntaxNode, cx: LowerCtx<'_>) -> Ir {
    let EnvParts {
        leading,
        begin,
        body,
        end,
        lifted,
        tail_len,
        body_header_token,
    } = split_environment(node, cx);
    let body_cx = cx.absorbing_trailing_control_newline(&body);
    let body = lower_env_body(
        body,
        tail_len,
        lifted.is_some(),
        body_header_token.as_ref(),
        body_cx,
    );
    // Trim the body's own edge breaks (the indenter re-supplies them), but if the
    // author left a blank line touching `\begin`/`\end`, preserve it as a single
    // blank line — LaTeX blank lines are deliberate visual spacing, so we keep one
    // rather than collapse to zero (interior runs already collapse to one).
    let (lead_blank, body) = peel_leading_break(body);
    let (trail_blank, body) = peel_trailing_break(body);
    let lead = if lead_blank {
        Ir::empty_line()
    } else {
        Ir::hard_line()
    };
    let trail = if trail_blank {
        Ir::empty_line()
    } else {
        Ir::hard_line()
    };

    let env = if matches!(body, Ir::Nil) {
        // Empty body: keep `\begin` and `\end` on their own lines (no edge blank).
        Ir::concat([begin, Ir::hard_line(), end])
    } else if environment_no_indent(node, cx) {
        // `document` and friends: lay the body on its own lines, but flush against
        // the surrounding indentation rather than nesting it.
        Ir::concat([begin, lead, body, trail, end])
    } else {
        Ir::concat([begin, Ir::indent(Ir::concat([lead, body])), trail, end])
    };
    Ir::concat([leading, env])
}

/// Split a `CONDITIONAL_BRANCH`'s elements from the trailing collapsible-trivia
/// run that carries the gap to whatever follows it, classifying that gap as a
/// [`Gap`].
///
/// All inter-segment trivia belongs to the *preceding* branch — the grammar's
/// branch loop consumes it before it reaches the divider — so this is the only
/// place a boundary gap can live.
///
/// Only [`Gap::Glued`] is discriminated by the layout: it is the one case that is
/// not a break opportunity. [`Gap::Comment`] is distinguished purely so that a
/// comment-terminated boundary never lands in `Glued` — the `%` itself is not
/// collapsible trivia, so nothing is peeled behind it, and without its own variant
/// a branch ending `… % note` would read as glued and send the whole construct
/// down the byte-faithful path. It lays out exactly like [`Gap::Space`]; the flat
/// candidate is separately refused by [`collapse_conditional_elements`], which sees
/// the same `%`. Whether the peeled run held a newline is, as everywhere,
/// invisible here.
fn split_branch_gap(node: &SyntaxNode) -> (Vec<SyntaxElement>, Gap) {
    let mut elements: Vec<SyntaxElement> = node.children_with_tokens().collect();
    let mut peeled = false;
    while let Some(SyntaxElement::Token(t)) = elements.last() {
        if !is_collapsible_trivia(t.kind()) {
            break;
        }
        peeled = true;
        elements.pop();
    }
    let gap = match elements.last() {
        Some(SyntaxElement::Token(t)) if t.kind() == SyntaxKind::COMMENT => Gap::Comment,
        _ if peeled => Gap::space(),
        _ => Gap::Glued,
    };
    (elements, gap)
}

/// Whether a `CONDITIONAL`'s branch interiors should be reflowed as prose, or
/// `None` when the construct must not be relaid at all.
///
/// Read off the *enclosing* context, since a branch carries no `PARAGRAPH` of its
/// own to answer with (the gate keeps a conditional inside one paragraph, so one
/// never nests in a branch). A conditional whose nearest non-conditional ancestor
/// is a `PARAGRAPH` sits in running text and reflows like it; one inside a `GROUP`
/// or `ARGUMENT` is macro code — a `\def` body — where the enclosing group emits
/// the byte-faithful stream, so the branches do too. Nested conditionals inherit
/// the answer by walking past their parent branch.
///
/// `None` for a `.dtx` documentation paragraph: the broken candidate commits hard
/// lines, and a line committed inside a doc paragraph lands outside its `% `
/// margin. The `contains_doc_margin` guard on the dispatch arm only catches a
/// conditional carrying a margin *itself*, not one riding a margined line whose
/// `DOC_MARGIN` sits before the opener.
fn conditional_interior_reflows(node: &SyntaxNode) -> Option<bool> {
    let mut ancestor = node.parent();
    while let Some(parent) = ancestor {
        match parent.kind() {
            SyntaxKind::CONDITIONAL | SyntaxKind::CONDITIONAL_BRANCH => ancestor = parent.parent(),
            SyntaxKind::PARAGRAPH => return (!is_dtx_doc_paragraph(&parent)).then_some(true),
            _ => return Some(false),
        }
    }
    Some(false)
}

/// A `CONDITIONAL`'s children, decomposed for [`lower_conditional`]: the elements
/// before the first branch, the branches, and the closing `\fi`.
///
/// The leading run is **not** always empty, which is the whole reason this exists.
/// An own-line `%` run immediately before the opener binds forward as a
/// `DOC_COMMENT` and the grammar reparents it *inside* the `CONDITIONAL`
/// (`Parser::conditional`, and `parse_block` for a top-level one), so it is a
/// sibling of the branches. A lowering that walked only `CONDITIONAL_BRANCH`
/// children would drop it on the floor — and the non-trivia-content oracle cannot
/// see that, because a comment is trivia to the CST. Hence
/// [`comments_survive_formatting`] in `tests/format.rs`.
///
/// `None` when the node is not in the shape the all-or-nothing layout assumes (no
/// closer, no branch, or a stray element between two branches); the caller then
/// takes the byte-faithful stream, which emits every child by construction.
struct ConditionalParts {
    leading: Vec<SyntaxElement>,
    branches: Vec<SyntaxNode>,
    closer: SyntaxNode,
}

fn split_conditional(node: &SyntaxNode) -> Option<ConditionalParts> {
    let mut leading: Vec<SyntaxElement> = Vec::new();
    let mut branches: Vec<SyntaxNode> = Vec::new();
    let mut closer: Option<SyntaxNode> = None;
    for element in node.children_with_tokens() {
        match &element {
            SyntaxElement::Node(child) if child.kind() == SyntaxKind::CONDITIONAL_BRANCH => {
                // The closer is last, positionally (`Conditional::closer`); a branch
                // after one means the walk stopped somewhere this layout cannot model.
                if closer.is_some() {
                    return None;
                }
                branches.push(child.clone());
            }
            SyntaxElement::Node(child)
                if child.kind() == SyntaxKind::COMMAND && !branches.is_empty() =>
            {
                if closer.replace(child.clone()).is_some() {
                    return None;
                }
            }
            _ if branches.is_empty() => leading.push(element),
            // Anything else between the branches would be dropped by the branch
            // walk, so decline the whole construct rather than lose it.
            _ => return None,
        }
    }
    Some(ConditionalParts {
        leading,
        branches,
        closer: closer?,
    })
}

/// Lower an `\if… … \else … \or … \fi` conditional **all-or-nothing**: flat when
/// the whole construct fits, else *every* divider opens a line.
///
/// The construct's extent is what makes this decidable, and it is why the node
/// exists. A per-divider rule at this layer has no coherent form: fired only
/// across a gap the author already wrote it *is* the lone-newline read; fired
/// unconditionally it manufactures a space token at the ~22% of glued sites, which
/// TeX contributes to the horizontal list; fired only where the author broke, it is
/// lopsided — one divider broken and its sibling not, decided by where the author
/// happened to glue.
///
/// Two things the node deliberately does **not** buy. There is no body indent and
/// no head/body split: the `\if` *test*'s extent is not statically resolvable
/// (`\ifnum\radius>5` scans ⟨number⟩⟨rel⟩⟨number⟩ by TeX's own scanner), so the
/// environment-shaped layout the corpus files are written in is out of reach even
/// with the node. And a construct with **any glued divider** takes the byte-faithful
/// path instead of the group: breaking one divider but not its glued sibling is the
/// lopsided form, and breaking the glued one is the typeset change, so the only
/// coherent option left is to relayout none of them. Those keep their authored
/// line structure, which is a fixed point (a hard line re-reads as a newline, glue
/// re-reads as glue) and never materializes a space.
///
/// The decision is offered to the printer as two whole candidates
/// ([`Ir::conditional_group_all_lines`]) rather than as one [`Ir::group`] of
/// `Ir::Line`s, and that is load-bearing. A group's break state is saturated from
/// whatever forced breaks its subtree carries, and a branch *interior* carries one
/// for every authored line the command-only-line rule
/// ([`line_is_command_only`]) keeps — so a group would decide the dividers from
/// the interior's authored newlines, which is precisely the predicate that must
/// not decide them. The flat candidate is collapsed from *content* alone
/// ([`collapse_conditional_elements`]), so its width — and therefore the choice
/// between the two — is a function of non-trivia content and the config only.
/// When no flat candidate exists (a `%` comment, a nested block), the broken form
/// is unconditional, which is a content fact and fair to read.
///
/// A branch *interior* is lowered the way the construct's **enclosing context**
/// would lower the same elements ([`conditional_interior_reflows`]). That is what
/// "as anywhere else" has to mean, and it cannot be read off the branch itself: the
/// gate keeps a `CONDITIONAL` inside one paragraph, so no `PARAGRAPH` node ever
/// nests in a branch to carry the prose lowering the way an environment body's
/// does. In prose the branch therefore reflows — its words wrap and its inter-word
/// spacing normalizes, exactly as they would outside the construct — while in a
/// `\def` body it takes the byte-faithful stream, because that is what the
/// enclosing `GROUP` does. Feeding macro code to the prose reflow is not merely
/// cosmetic: `pagesel.sty`'s `\ifx\\#2\\%` has the parser's `LINE_BREAK` node in an
/// `\ifx` operand slot, and the reflow's "a `\\` ends its line" rule oscillates on
/// it pass over pass.
///
/// The whole relayout is confined to the modes that lay prose out at all
/// ([`LowerCtx::wraps_prose`]). [`WrapMode::Preserve`] promises authored line
/// breaks are untouched, and the all-or-nothing choice would rejoin a conditional
/// the author spread over lines — so that mode takes the byte-faithful stream.
/// The other three rebuild every prose line from runs already, so the choice is
/// theirs to make.
fn lower_conditional(node: &SyntaxNode, cx: LowerCtx<'_>) -> Ir {
    let generic = || Ir::concat(lower_element_stream(node.children_with_tokens(), cx));
    if !cx.wraps_prose() {
        return generic();
    }
    let Some(reflow_interior) = conditional_interior_reflows(node) else {
        return generic();
    };
    let Some(ConditionalParts {
        leading,
        branches,
        closer,
    }) = split_conditional(node)
    else {
        return generic();
    };

    let split: Vec<(Vec<SyntaxElement>, Gap)> = branches.iter().map(split_branch_gap).collect();
    if split.iter().any(|(_, gap)| matches!(gap, Gap::Glued)) {
        return generic();
    }

    // The bound `DOC_COMMENT` run, if any. Lowered outside the candidates (as
    // `lower_environment` does with its own leading run): it is not part of the
    // construct's width, and it must survive whichever candidate the printer picks.
    let leading = if leading.is_empty() {
        Ir::Nil
    } else {
        Ir::concat(lower_element_stream(leading.into_iter(), cx))
    };

    let closer_ir = lower_node(&closer, cx);
    let mut broken = Vec::with_capacity(split.len() * 2 + 1);
    for (elements, _) in &split {
        broken.push(if reflow_interior {
            reflow_elements(elements.iter().cloned(), cx, ReflowKind::Prose)
        } else {
            Ir::concat(lower_element_stream(elements.iter().cloned(), cx))
        });
        broken.push(Ir::hard_line());
    }
    broken.push(closer_ir.clone());
    let broken = Ir::concat(broken);

    let group = match collapse_conditional(&split, &closer_ir, cx) {
        Some(flat) => Ir::conditional_group_all_lines([flat, broken]),
        None => broken,
    };
    Ir::concat([leading, group])
}

/// The one-line candidate for [`lower_conditional`]: every branch collapsed to a
/// single line, dividers separated by one space.
///
/// `None` — no flat form exists, so the construct is unconditionally broken — when
/// any branch holds a `%` comment (which must end its line) or force-break content
/// (a nested environment, display math, `\\`). Both are *content* facts, so keying
/// the layout on them is sound; an authored newline is not, and collapses to a
/// space here exactly as it does in [`collapse_arg_group`].
fn collapse_conditional(
    split: &[(Vec<SyntaxElement>, Gap)],
    closer: &Ir,
    cx: LowerCtx<'_>,
) -> Option<Ir> {
    if closer.contains_forced_break() {
        return None;
    }
    let mut parts = Vec::new();
    for (elements, _) in split {
        parts.extend(collapse_conditional_elements(elements, cx)?);
        parts.push(Ir::verbatim(" "));
    }
    parts.push(closer.clone());
    Some(Ir::concat(parts))
}

/// Collapse one branch's elements to a single line, or `None` if it cannot be
/// collapsed. Mirrors [`collapse_arg_group`]'s body loop, without the delimiter
/// handling a branch has no equivalent of.
fn collapse_conditional_elements(elements: &[SyntaxElement], cx: LowerCtx<'_>) -> Option<Vec<Ir>> {
    let mut out = Vec::new();
    let mut iter = elements.iter().cloned().peekable();
    while let Some(element) = iter.next() {
        match element {
            SyntaxElement::Token(t) if is_collapsible_trivia(t.kind()) => {
                let gap = consume_gap(&t, &mut iter);
                if gap == Gap::Blank {
                    return None; // a blank-line `\par` (the gate should preclude it)
                }
                out.push(Ir::verbatim(gap.flat()));
            }
            // A `%` comment must terminate its line, so there is no flat form.
            SyntaxElement::Token(t) if t.kind() == SyntaxKind::COMMENT => return None,
            SyntaxElement::Token(t) => out.push(Ir::verbatim(t.text())),
            SyntaxElement::Node(child) => {
                let ir = lower_node(&child, cx);
                if ir.contains_forced_break() {
                    return None; // nested block content: keep the broken form
                }
                out.push(ir);
            }
        }
    }
    Some(out)
}

/// Whether the environment is *margin-framed*: a `.dtx` documentation margin
/// (`DOC_MARGIN`) or docstrip guard (`GUARD`) sits immediately before its `\begin`
/// on the same physical line — `%␣␣␣␣\begin{macrocode}`, a documentation-layer
/// `% \begin{itemize}`. The `\begin`/`\end` are docstrip *frame lines* anchored at
/// column 0, so the body must not be indented (indenting would push the frame
/// margins off column 0 and split the closing `%␣␣␣␣\end{…}` frame — the corruption
/// this fixes). A pure CST-shape fact: it walks back over inline whitespace from
/// `\begin` and asks only "is the previous token a margin/guard on this line", with
/// no signature lookup, and covers `macrocode` and prose-layer environments
/// uniformly. `DOC_MARGIN`/
/// `GUARD` exist only under the `.dtx` config, so this is always false elsewhere.
fn is_margin_framed(node: &SyntaxNode) -> bool {
    let Some(begin) = Environment::cast(node.clone()).and_then(|e| e.begin()) else {
        return false;
    };
    // A frame header occupies its physical line. If body content follows the
    // `\begin` inline, the generic stream must keep it behind the existing `%`;
    // the framed layout would insert a break and turn it into live package code.
    if begin
        .syntax()
        .last_token()
        .and_then(|token| token.next_token())
        .is_some_and(|token| token.kind() != SyntaxKind::NEWLINE)
    {
        return false;
    }
    let mut tok = begin.syntax().first_token().and_then(|t| t.prev_token());
    while let Some(t) = tok {
        match t.kind() {
            SyntaxKind::WHITESPACE => tok = t.prev_token(),
            SyntaxKind::DOC_MARGIN | SyntaxKind::GUARD => return true,
            _ => return false,
        }
    }
    false
}

/// Split a trailing closing-frame margin run off `body`, returning it (the docstrip
/// `\end` frame's `%␣␣␣␣` prefix) so the caller can ride it onto the `\end` line at
/// column 0 instead of leaving it as body tail with a break before `\end` (which
/// would split the frame). The frame is the maximal trailing run of inline
/// `WHITESPACE` / `DOC_MARGIN` / `GUARD` tokens, and only counts as a frame when it
/// actually contains a margin/guard; the `NEWLINE` before it stays in `body` as the
/// trailing break that becomes the frame line's leading break. Returns `None` when
/// `\end` has no preceding margin on its own line (e.g. a prose-layer `\end{…}`
/// authored flush against content), so the caller falls back to the plain
/// no-indent shape.
fn split_closing_frame(body: &mut Vec<SyntaxElement>) -> Option<Vec<SyntaxElement>> {
    let mut boundary = body.len();
    let mut has_margin = false;
    while boundary > 0 {
        match &body[boundary - 1] {
            SyntaxElement::Token(t)
                if matches!(t.kind(), SyntaxKind::DOC_MARGIN | SyntaxKind::GUARD) =>
            {
                has_margin = true;
                boundary -= 1;
            }
            SyntaxElement::Token(t) if t.kind() == SyntaxKind::WHITESPACE => boundary -= 1,
            _ => break,
        }
    }
    has_margin.then(|| body.split_off(boundary))
}

/// Lower a *margin-framed* environment (see [`is_margin_framed`]): a `.dtx`
/// docstrip frame whose `\begin`/`\end` sit on column-0 margin lines. Unlike
/// [`lower_environment`] this never indents the body (the frames are not a real
/// indentation scope) and it pulls the closing `%␣␣␣␣` frame back onto the `\end`
/// line so the terminator stays a single byte-faithful frame line. The body is
/// still lowered as ordinary content — for `macrocode` that is real code whose
/// interior groups/environments indent relative to their column-0 base; for a
/// prose-layer environment it is margin lines, each pinned to column 0 by
/// [`Ir::column_zero`].
fn lower_margin_framed_environment(node: &SyntaxNode, cx: LowerCtx<'_>) -> Ir {
    let EnvParts {
        leading,
        begin,
        mut body,
        end,
        lifted,
        tail_len,
        body_header_token: _,
    } = split_environment(node, cx);

    // Pull the `%␣␣␣␣` that frames `\end` onto the `\end` line; what remains is the
    // real body.
    let frame = split_closing_frame(&mut body);
    let frame_ir = frame
        .map(|f| Ir::concat(lower_element_stream(f.into_iter(), cx)))
        .filter(|ir| !matches!(ir, Ir::Nil));

    let body_cx = cx.absorbing_trailing_control_newline(&body);
    let body = lower_env_body(body, tail_len, lifted.is_some(), None, body_cx);
    let (lead_blank, body) = peel_leading_break(body);
    let (trail_blank, body) = peel_trailing_break(body);
    let lead = if lead_blank {
        Ir::empty_line()
    } else {
        Ir::hard_line()
    };
    // The break that separates the body (or `\begin`, for an empty body) from the
    // `\end` frame line.
    let close_break = if trail_blank {
        Ir::empty_line()
    } else {
        Ir::hard_line()
    };

    let env = match (matches!(body, Ir::Nil), frame_ir) {
        // Empty body, framed close: `\begin` then the `%␣␣␣␣\end` frame line.
        (true, Some(frame_ir)) => Ir::concat([begin, close_break, frame_ir, end]),
        // Empty body, no frame: `\begin` and `\end` on their own lines.
        (true, None) => Ir::concat([begin, Ir::hard_line(), end]),
        // Body then the `%␣␣␣␣\end` frame line at column 0.
        (false, Some(frame_ir)) => Ir::concat([begin, lead, body, close_break, frame_ir, end]),
        // Body but no closing margin: behave like a no-indent environment.
        (false, None) => Ir::concat([begin, lead, body, close_break, end]),
    };
    Ir::concat([leading, env])
}

/// The `%` comment that trails the `\begin{…}` header on the *same* source line —
/// only inline whitespace, never a newline, separates the header from it. Such a
/// comment is the space-suppression idiom and belongs on the header line; a
/// comment the author placed on its own line (a newline intervenes) returns
/// `None` and stays in the body. Scans the body in source order, descending into
/// the first node (the body's leading paragraph holds the comment as its first
/// token): inline whitespace is skipped, a comment matches, and anything else —
/// a newline or real content — ends the scan.
fn leading_inline_comment(body_elements: &[SyntaxElement]) -> Option<SyntaxToken> {
    for element in body_elements {
        match element {
            SyntaxElement::Token(token) => match token.kind() {
                SyntaxKind::WHITESPACE => continue,
                SyntaxKind::COMMENT => return Some(token.clone()),
                _ => return None,
            },
            SyntaxElement::Node(node) => {
                for token in node
                    .descendants_with_tokens()
                    .filter_map(|e| e.into_token())
                {
                    match token.kind() {
                        SyntaxKind::WHITESPACE => continue,
                        SyntaxKind::COMMENT => return Some(token),
                        _ => return None,
                    }
                }
            }
        }
    }
    None
}

/// Lower an environment body whose leading inline comment has already been lifted
/// onto the `\begin` header by [`lower_environment`]. The comment is dropped from
/// the body to avoid emitting it twice: a bare comment token is skipped outright,
/// and the leading paragraph is re-lowered with its leading whitespace-and-comment
/// run stripped (see [`lower_node_dropping_leading_comment`]). Everything after
/// the comment lowers through the normal stream path.
fn lower_body_dropping_leading_comment(body_elements: Vec<SyntaxElement>, cx: LowerCtx<'_>) -> Ir {
    let mut out: Vec<Ir> = Vec::new();
    let mut iter = body_elements.into_iter();
    for element in iter.by_ref() {
        match element {
            SyntaxElement::Token(token) if token.kind() == SyntaxKind::WHITESPACE => continue,
            SyntaxElement::Token(token) if token.kind() == SyntaxKind::COMMENT => break,
            SyntaxElement::Node(node) => {
                out.push(lower_node_dropping_leading_comment(&node, cx));
                break;
            }
            // Unreachable given `leading_inline_comment` matched, but stay lossless.
            SyntaxElement::Token(token) => {
                out.push(lower_loose_token(&token, cx));
                break;
            }
        }
    }
    out.extend(lower_element_stream(iter, cx));
    Ir::concat(out)
}

/// Re-lower `node` with its leading whitespace-and-comment run dropped, using the
/// same dispatch [`lower_node`] would (reflow for a `PARAGRAPH` under
/// [`WrapMode::Reflow`], the generic stream otherwise). Used by
/// [`lower_body_dropping_leading_comment`] to strip a comment lifted onto the
/// `\begin` header.
fn lower_node_dropping_leading_comment(node: &SyntaxNode, cx: LowerCtx<'_>) -> Ir {
    let mut children: Vec<SyntaxElement> = node.children_with_tokens().collect();
    let mut i = 0;
    while matches!(
        children.get(i).and_then(|c| c.as_token()).map(|t| t.kind()),
        Some(SyntaxKind::WHITESPACE)
    ) {
        i += 1;
    }
    if matches!(
        children.get(i).and_then(|c| c.as_token()).map(|t| t.kind()),
        Some(SyntaxKind::COMMENT)
    ) {
        children.drain(..=i);
    }
    if node.kind() == SyntaxKind::PARAGRAPH && cx.wraps_prose() {
        reflow_elements(children.into_iter(), cx, ReflowKind::Prose)
    } else {
        Ir::concat(lower_element_stream(children.into_iter(), cx))
    }
}

/// Whether the environment's body should be left at the surrounding indentation
/// level rather than nested one step in (the `noIndent` signature flag — see
/// [`crate::semantic::signature::EnvironmentSig::no_indent`]). The canonical case
/// is `document`, whose body conventionally sits flush against the margin.
fn environment_no_indent(node: &SyntaxNode, cx: LowerCtx<'_>) -> bool {
    cx.signatures
        .environment_at(node)
        .is_some_and(|sig| sig.no_indent)
}

/// Whether `node` — a `PARAGRAPH`, or an environment body about to be reflowed as
/// one — sits in the body of an environment the signature DB marks
/// `statementBody`: the TikZ/pgf picture family, whose content is a sequence of
/// `;`-terminated path statements rather than running prose
/// ([`crate::semantic::signature::EnvironmentSig::statement_body`]).
///
/// A prose fill is wrong for such a body in a way width alone cannot express: it
/// runs `\draw …;` and `\node …;` onto one line, and it breaks a `\foreach`
/// header away from its loop variables (issue #114). [`ReflowKind::Statement`]
/// routes the body to the statement layout: under [`WrapMode::Reflow`] the
/// parser's `STATEMENT` nodes are lowered structurally with hung continuations
/// ([`lower_statement`], Tier 1), and content no `;` terminates keeps the
/// authored-line fallback — the same posture a code-like brace-group body takes,
/// with the Tier-2 flush-continuation fixed point carrying over unchanged.
///
/// The **nearest** environment ancestor decides, never any of them. An `itemize`
/// or a `tabular` inside a `\node`'s label holds ordinary prose and must still
/// reflow, though a `tikzpicture` encloses it.
fn in_statement_body_env(node: &SyntaxNode, cx: LowerCtx<'_>) -> bool {
    node.ancestors()
        .skip(1)
        .find(|ancestor| ancestor.kind() == SyntaxKind::ENVIRONMENT)
        .and_then(|env| cx.signatures.environment_at(&env))
        .is_some_and(|sig| sig.statement_body)
}

fn in_algorithm2e_env(node: &SyntaxNode, cx: LowerCtx<'_>) -> bool {
    let Some(env) = node
        .ancestors()
        .skip(1)
        .find(|ancestor| ancestor.kind() == SyntaxKind::ENVIRONMENT)
        .filter(|env| {
            cx.signatures
                .environment_at(env)
                .is_some_and(|sig| sig.algorithm2e)
        })
    else {
        return false;
    };
    if let Some(&proved) = cx.algorithm2e_cache.borrow().get(&env) {
        return proved;
    }
    // The algorithm package uses the same float name, but keeps `\;` as text
    // spacing. A complete algorithm2e control-flow call in the direct body
    // establishes the dialect; calls inside other environments or definitions
    // cannot grant that meaning to the enclosing float.
    let proved = env
        .children()
        .filter(|child| child.kind() == SyntaxKind::PARAGRAPH)
        .flat_map(|paragraph| paragraph.children())
        .any(|command| {
            cx.signatures.command_at(&command).is_some_and(|sig| {
                sig.args
                    .iter()
                    .any(|arg| arg.content == ContentKind::Algorithm2e)
            })
        });
    cx.algorithm2e_cache.borrow_mut().insert(env, proved);
    proved
}

/// A `\begin{…}` header, split from the content the greedy parser attached past
/// the end of that header. See [`lower_begin`].
struct BeginParts {
    header: Ir,
    /// Elements [`split_environment`] prepends to the environment's body, so they
    /// indent and reflow with it instead of riding the header line.
    tail: Vec<SyntaxElement>,
}

/// Lower a `\begin{name}` node into the header line and the body content the
/// greedy parser over-attached to it.
///
/// **The header ends at the last element glued to it.** Two rules decide where
/// that is. Groups matching the environment's *declared* argument slots are glued
/// to `\begin{name}` whatever the author wrote between them, so
/// `\begin{tabular}\n{cc}` renders as a single `\begin{tabular}{cc}` header.
/// Positional matching skips omitted optional slots. Once the supplied groups
/// exhaust the signature — and from the `{name}` group onwards for an environment
/// with no declared arguments — the header continues only while each boundary is
/// [`Gap::Glued`]; at the first gap it stops, and everything from there is body.
///
/// The slots come from the [`Signatures`] overlay (`cx.signatures`): a document's
/// own `\newenvironment{thm}[1]…` is honored just like a built-in `tabular`, with
/// the scanned definition shadowing a built-in of the same name. A delimiter
/// mismatch against a pending required slot invalidates the positional claim and
/// demotes the rest of the header to ordinary glue boundaries; this keeps an
/// incomplete curated signature from reclassifying source on the next parse.
///
/// Attachment past the declared slots is not an argument claim, so it must not be
/// rendered as one: leaving it in the header
/// stranded it at the `\begin` column, one level short of the body it belongs to
/// (`\begin{center}\n{\bfseries A heading}`). Gluing it up instead would dress body
/// content as an argument, so body is the honest destination.
///
/// Trivia-invariant: only `Glued`-versus-not is read, which the normalized [`Gap`]
/// boundary preserves — a lone newline, a space, and a blank line all fall in the
/// same bucket, so the unsafe predicate never reaches this decision. It is a fixed
/// point in both directions: a glued tail re-parses glued, and a tail sent to the
/// body re-parses separated.
///
/// A `%` that trailed the header on its own source line stays on it (own-line-ness
/// is a preserved predicate, and relocating a trailing comment rebinds it as the
/// next construct's `DOC_COMMENT`); one the author gave its own
/// line travels to the body with the rest of the tail, which is where it already
/// was. But a header comment *with* a declared arity keeps every argument in the
/// header, because both available moves are wrong there: gluing an argument across
/// the `%` would comment it out, and sending it to the body would take a `tabular`'s
/// colspec away from the grid. A mandatory argument after a trailing comment is
/// emitted on an indented continuation line; an optional must retain the generic
/// path, since inserting whitespace before `[…]` can change whether TeX recognizes
/// it. A `.dtx` doc margin or guard is likewise preserved wholesale — both must
/// open their own line.
///
/// Each declared argument is also matched to its signature slot ([`match_arg_slot`],
/// mirroring [`lower_command`]) so a [`ContentKind::Keyval`] argument reaches the
/// delimiter-appropriate segmented layout: `[…]` for `axis`/`tikzpicture`, and
/// `{…}` for tabularray's inner specification. The argument is nested one step
/// beneath the environment header, so a multiline argument's closing delimiter
/// aligns with the surrounding environment body and its content sits one level
/// deeper. Its first line stays attached to `\begin{…}`. Opaque braced values
/// keep their top-level words together instead of treating them as prose.
fn lower_begin(begin: &SyntaxNode, cx: LowerCtx<'_>) -> BeginParts {
    let sig = cx.signatures.environment_at(begin);
    let mut has_comment = false;
    let mut has_margin = false;
    for token in begin
        .children_with_tokens()
        .filter_map(|element| element.into_token())
    {
        match token.kind() {
            SyntaxKind::COMMENT => has_comment = true,
            SyntaxKind::DOC_MARGIN if !cx.in_dtx_doc_region => has_margin = true,
            SyntaxKind::GUARD => has_margin = true,
            _ => {}
        }
    }
    if has_margin {
        return BeginParts {
            header: lower_node(begin, cx),
            tail: Vec::new(),
        };
    }

    let args = sig.as_ref().map(|sig| &*sig.args).unwrap_or(&[]);
    if has_comment && !args.is_empty() {
        return lower_commented_begin(begin, cx, args);
    }
    let elements: Vec<SyntaxElement> = begin.children_with_tokens().collect();
    let mut head: Vec<Ir> = Vec::new();
    let mut slot = 0usize;
    let mut signature_matches = true;
    let mut i = 0usize;
    while let Some(element) = elements.get(i) {
        match element {
            SyntaxElement::Token(token)
                if cx.in_dtx_doc_region && token.kind() == SyntaxKind::DOC_MARGIN =>
            {
                // The region wrapper owns the canonical margin. Its source
                // padding is not a gap in the virtual LaTeX header.
                i += 1;
                while matches!(
                    elements.get(i),
                    Some(SyntaxElement::Token(next)) if next.kind() == SyntaxKind::WHITESPACE
                ) {
                    i += 1;
                }
            }
            SyntaxElement::Token(token) if is_collapsible_trivia(token.kind()) => {
                // Measure the run in place rather than consuming it: when it turns
                // out to be the split point it must travel to the body, so that
                // `leading_inline_comment` sees trivia — not a bare `%` — first and
                // declines to lift an own-line comment back onto the header.
                let (mut end, mut newlines, mut flat) = (i, 0usize, String::new());
                while let Some(SyntaxElement::Token(token)) = elements.get(end) {
                    if !is_collapsible_trivia(token.kind()) {
                        break;
                    }
                    newlines += usize::from(token.kind() == SyntaxKind::NEWLINE);
                    flat.push_str(token.text());
                    end += 1;
                }
                // A following group that matches the next signature slot glues
                // to `\begin{name}`, so the run is dropped. Match on a copy:
                // the group arm commits the slot only once it consumes the node.
                let mut next_slot = slot;
                let declared_arg_follows = signature_matches
                    && elements
                        .get(end)
                        .and_then(attached_arg_kind)
                        .and_then(|kind| match_arg_slot(args, &mut next_slot, kind))
                        .is_some();
                if declared_arg_follows {
                    i = end;
                    continue;
                }
                // A `%` authored on the header line rides it.
                let trails_comment = newlines == 0
                    && matches!(
                        elements.get(end),
                        Some(SyntaxElement::Token(token)) if token.kind() == SyntaxKind::COMMENT
                    );
                if trails_comment {
                    head.push(Ir::verbatim(flat));
                    i = end;
                    continue;
                }
                // Not glued: the header ends here and the rest is body.
                return BeginParts {
                    header: Ir::concat(head),
                    tail: elements[i..].to_vec(),
                };
            }
            SyntaxElement::Node(child)
                if matches!(child.kind(), SyntaxKind::GROUP | SyntaxKind::OPTIONAL) =>
            {
                let kind = attached_arg_kind(element).expect("group or optional argument");
                let spec = signature_matches
                    .then(|| match_arg_slot(args, &mut slot, kind))
                    .flatten();
                // A delimiter mismatch against a pending required slot means the
                // signature is incomplete for this source shape. Demote the rest
                // of the header to ordinary glue boundaries instead of making a
                // later group look declared by skipping over the mismatch.
                if spec.is_none() && slot < args.len() {
                    signature_matches = false;
                }
                let keyval = spec.is_some_and(|spec| spec.content == ContentKind::Keyval);
                let colspec = !keyval
                    && child.kind() == SyntaxKind::GROUP
                    && spec.is_some()
                    && slot == args.len()
                    && sig.as_ref().is_some_and(|sig| sig.align);
                let segmented = if keyval {
                    Some(match child.kind() {
                        SyntaxKind::OPTIONAL => lower_optional(child, cx, true),
                        SyntaxKind::GROUP => lower_segmented_group(
                            child,
                            SyntaxKind::L_BRACE,
                            SyntaxKind::R_BRACE,
                            cx,
                            true,
                        ),
                        _ => unreachable!("argument kind checked above"),
                    })
                } else if colspec {
                    Some(lower_column_spec_group(child, cx))
                } else {
                    None
                };
                let argument = segmented.flatten().unwrap_or_else(|| {
                    lower_header_argument(child, spec.map(|spec| spec.content), cx)
                });
                head.push(if spec.is_some() {
                    Ir::indent(argument)
                } else {
                    argument
                });
                i += 1;
            }
            // The `\begin` control word, the `{name}` group, and anything the
            // author glued past the declared arity stay on the header line.
            SyntaxElement::Node(child) => {
                head.push(lower_node(child, cx));
                i += 1;
            }
            SyntaxElement::Token(token) => {
                head.push(lower_loose_token(token, cx));
                i += 1;
            }
        }
    }
    BeginParts {
        header: Ir::concat(head),
        tail: Vec::new(),
    }
}

/// The positional signature delimiter represented by an attached syntax node.
fn attached_arg_kind(element: &SyntaxElement) -> Option<ArgKind> {
    match element.as_node()?.kind() {
        SyntaxKind::GROUP => Some(ArgKind::Brace),
        SyntaxKind::OPTIONAL => Some(ArgKind::Bracket),
        _ => None,
    }
}

/// A declared opaque header value is an argument unit, not a prose body. Its
/// spaces must not compete with earlier option-list separators for a width
/// break. Comments, paragraphs, and nested blocks retain their own layout.
fn lower_header_argument(node: &SyntaxNode, content: Option<ContentKind>, cx: LowerCtx<'_>) -> Ir {
    if content == Some(ContentKind::Opaque)
        && node.kind() == SyntaxKind::GROUP
        && matches!(cx.wrap, WrapMode::Reflow)
        && !cx.suppressed(node.text_range())
        && !cx.preserve_dtx_nested_layout
        && !contains_doc_margin(node, cx)
        && !doc_margin_opens_line(node, cx)
        && let Some(argument) =
            collapse_arg_group(node, SyntaxKind::L_BRACE, SyntaxKind::R_BRACE, cx)
    {
        return argument;
    }
    lower_node(node, cx)
}

/// Lower a commented header slice after removing collapsible gaps before declared
/// arguments, as the ordinary [`lower_begin`] path does. A gap after a comment is
/// never removed because the comment consumes the rest of its line; suppressed
/// gaps likewise remain byte-exact. Virtual `.dtx` margin streams preserve every
/// boundary because an inner rewrite can otherwise change the enclosing margin
/// region's parse shape on the next pass. Declared argument nodes receive the
/// same header-relative nesting as the ordinary [`lower_begin`] path.
fn lower_commented_header_stream(
    elements: &[SyntaxElement],
    declared: &[Option<ContentKind>],
    mut previous_was_comment: bool,
    cx: LowerCtx<'_>,
) -> Ir {
    if cx.in_dtx_doc_region {
        return Ir::concat(lower_element_stream(elements.iter().cloned(), cx));
    }

    let mut filtered = Vec::with_capacity(elements.len());
    let mut i = 0usize;
    while i < elements.len() {
        let SyntaxElement::Token(token) = &elements[i] else {
            previous_was_comment = false;
            filtered.push((elements[i].clone(), declared[i]));
            i += 1;
            continue;
        };
        if !is_collapsible_trivia(token.kind()) {
            previous_was_comment = token.kind() == SyntaxKind::COMMENT;
            filtered.push((elements[i].clone(), declared[i]));
            i += 1;
            continue;
        }

        let start = i;
        while matches!(
            elements.get(i),
            Some(SyntaxElement::Token(token)) if is_collapsible_trivia(token.kind())
        ) {
            i += 1;
        }
        let followed_by_declared = declared.get(i).is_some_and(Option::is_some);
        let suppressed = elements[start..i].iter().any(|element| {
            element
                .as_token()
                .is_some_and(|token| cx.suppressed(token.text_range()))
        });
        if followed_by_declared && !previous_was_comment && !suppressed {
            continue;
        }
        filtered.extend(
            elements[start..i]
                .iter()
                .cloned()
                .zip(declared[start..i].iter().copied()),
        );
    }

    let mut lowered = Vec::new();
    let mut generic = Vec::new();
    for (element, content) in filtered {
        if content.is_none() {
            generic.push(element);
            continue;
        }

        lowered.extend(lower_element_stream(generic.drain(..), cx));
        let node = element
            .into_node()
            .expect("declared environment argument must be a syntax node");
        lowered.push(Ir::indent(lower_header_argument(&node, content, cx)));
    }
    lowered.extend(lower_element_stream(generic.into_iter(), cx));
    Ir::concat(lowered)
}

/// Lower a declared `\begin` header containing a comment without letting the
/// comment detach or consume a later argument. Argument matching mirrors the
/// ordinary [`lower_begin`] path: omitted optional slots are skipped, a pending
/// required-slot mismatch demotes the remaining groups, and content past the
/// completed header becomes [`BeginParts::tail`] for ordinary body reflow.
///
/// When a comment trails one argument and the next matched argument is mandatory,
/// that group is a structural header continuation and receives one indent. The
/// brace gate matters: TeX skips whitespace while scanning a mandatory argument,
/// whereas inserting indentation before an optional `[…]` can change argument
/// recognition.
fn lower_commented_begin(begin: &SyntaxNode, cx: LowerCtx<'_>, args: &[ArgSpec]) -> BeginParts {
    let elements: Vec<SyntaxElement> = begin.children_with_tokens().collect();
    let mut declared = vec![None; elements.len()];
    let mut required_brace = vec![false; elements.len()];
    let mut slot = 0usize;
    let mut signature_matches = true;

    for (i, element) in elements.iter().enumerate() {
        let Some(kind) = attached_arg_kind(element) else {
            continue;
        };
        let spec = signature_matches
            .then(|| match_arg_slot(args, &mut slot, kind))
            .flatten();
        if spec.is_none() && slot < args.len() {
            signature_matches = false;
        }
        if let Some(spec) = spec {
            declared[i] = Some(spec.content);
            required_brace[i] = spec.required && kind == ArgKind::Brace;
        }
    }

    let split = elements
        .iter()
        .enumerate()
        .find_map(|(i, element)| {
            let SyntaxElement::Token(token) = element else {
                return None;
            };
            if !is_collapsible_trivia(token.kind()) {
                return None;
            }

            let mut end = i;
            let mut newlines = 0usize;
            while let Some(SyntaxElement::Token(token)) = elements.get(end) {
                if !is_collapsible_trivia(token.kind()) {
                    break;
                }
                newlines += usize::from(token.kind() == SyntaxKind::NEWLINE);
                end += 1;
            }
            if declared.get(end).is_some_and(Option::is_some)
                || (newlines == 0
                    && matches!(
                        elements.get(end),
                        Some(SyntaxElement::Token(token)) if token.kind() == SyntaxKind::COMMENT
                    ))
            {
                None
            } else {
                Some(i)
            }
        })
        .unwrap_or(elements.len());

    let header_elements = &elements[..split];
    let header = header_elements
        .iter()
        .enumerate()
        .find_map(|(i, element)| {
            let SyntaxElement::Token(comment) = element else {
                return None;
            };
            if comment.kind() != SyntaxKind::COMMENT {
                return None;
            }

            let trails_argument =
                header_elements[..i]
                    .iter()
                    .enumerate()
                    .rev()
                    .find_map(|(index, previous)| match previous {
                        SyntaxElement::Token(token) if token.kind() == SyntaxKind::WHITESPACE => {
                            None
                        }
                        SyntaxElement::Node(_) => Some(declared[index].is_some()),
                        _ => Some(false),
                    })
                    == Some(true);
            if !trails_argument {
                return None;
            }

            let mut next = i + 1;
            let mut has_newline = false;
            while let Some(SyntaxElement::Token(token)) = header_elements.get(next) {
                if !is_collapsible_trivia(token.kind()) {
                    break;
                }
                has_newline |= token.kind() == SyntaxKind::NEWLINE;
                next += 1;
            }
            if !has_newline
                || !declared.get(next).is_some_and(Option::is_some)
                || !required_brace[next..split].contains(&true)
            {
                return None;
            }

            let prefix =
                lower_commented_header_stream(&header_elements[..=i], &declared[..=i], false, cx);
            let continuation = lower_commented_header_stream(
                &header_elements[i + 1..],
                &declared[i + 1..split],
                true,
                cx,
            );
            Some(Ir::concat([prefix, Ir::indent(continuation)]))
        })
        .unwrap_or_else(|| {
            lower_commented_header_stream(header_elements, &declared[..split], false, cx)
        });

    BeginParts {
        header,
        tail: elements[split..].to_vec(),
    }
}

/// True if `node` (an `ENVIRONMENT`) names a list environment the signature DB
/// marks `list` — `itemize`/`enumerate`/`description`, whose `\item`s the
/// formatter lays out one per line with a hanging indent (see
/// [`lower_list_environment`]).
fn is_list_env(node: &SyntaxNode, cx: LowerCtx<'_>) -> bool {
    cx.signatures
        .environment_at(node)
        .is_some_and(|sig| sig.list)
}

/// One `\item` of a list environment: the rendered marker (`\item`, an optional
/// `[label]`, and a bounded Beamer `<overlay>` suffix), the width to hang
/// continuation lines at (the rendered width of the control word plus a space —
/// `\item `, *not* the label or overlay, so a wide marker does not push the body's
/// left edge around), and the item's body split into paragraph *chunks* (a blank
/// line in the source starts a new chunk). `blank_before` records whether a blank
/// line separated this item from the previous one, so it is reproduced.
struct ListItem {
    /// The comment lines of a `DOC_COMMENT` bound leading into the `\item`
    /// (`% note` on its own line directly above), rendered one per line above
    /// the marker at the item indent.
    doc_lines: Vec<String>,
    marker: String,
    hang: usize,
    glue_body: bool,
    chunks: Vec<Vec<SyntaxElement>>,
    blank_before: bool,
}

/// A flattened list-body element: either a real CST element or an explicit
/// paragraph boundary (a blank line), which [`flatten_list_body`] reifies because
/// item collection spans paragraph breaks but the trivia carrying them lives
/// *between* the body's `PARAGRAPH` nodes.
enum FlatItem {
    El(SyntaxElement),
    Blank,
}

/// Lower a list environment (`itemize`/`enumerate`/`description`): each `\item`
/// starts its own line at the body indent and its body is reflowed with the
/// configured [`ItemIndent`]. Under the default [`ItemIndent::Hang`], a
/// `description` item's wide `[label]` trails on the first line but does not deepen
/// the body indent. The framing (`\begin`/`\end`, the indented body with
/// leading/trailing `hard_line`) matches [`lower_environment`].
///
/// Under [`WrapMode::Preserve`] the body is *not* reflowed: the author's line breaks
/// and inner spacing are kept byte-faithful (see [`lower_item_chunks`]) and only the
/// continuation-line indentation is re-hung under the marker. Falls back to the plain
/// [`lower_environment`] when the body has no `\item` to anchor on, so an unusual
/// shape degrades to today's indented body rather than misformatting.
fn lower_list_environment(node: &SyntaxNode, cx: LowerCtx<'_>) -> Ir {
    let EnvParts {
        leading,
        begin,
        body,
        end,
        lifted,
        // The `BEGIN` tail (see [`lower_begin`]) rides `body` as ordinary leading
        // elements: this path flattens the body itself, so it needs no splice.
        tail_len: _,
        body_header_token: _,
    } = split_environment(node, cx);

    let body_cx = cx.absorbing_trailing_control_newline(&body);
    let Some(body) = lower_list_body(&body, body_cx, lifted.as_ref()) else {
        return lower_environment(node, cx);
    };
    Ir::concat([
        leading,
        begin,
        Ir::indent(Ir::concat([Ir::hard_line(), body])),
        Ir::hard_line(),
        end,
    ])
}

/// Build the body IR of a list environment: split into items at each top-level
/// `\item`, collect a bounded Beamer overlay suffix into the marker, and render a
/// hanging-indented reflow of the content. Returns `None` (caller falls back) when
/// the body carries no `\item`.
fn lower_list_body(
    body_elements: &[SyntaxElement],
    cx: LowerCtx<'_>,
    lifted: Option<&SyntaxToken>,
) -> Option<Ir> {
    let flat = flatten_list_body(body_elements, lifted);

    // Content before the first `\item` (usually just trivia); kept as its own
    // leading segment so nothing is dropped.
    let mut preamble: Vec<Vec<SyntaxElement>> = vec![Vec::new()];
    let mut items: Vec<ListItem> = Vec::new();
    let mut blank_pending = false;
    let mut index = 0;
    while index < flat.len() {
        match &flat[index] {
            FlatItem::Blank => {
                // A paragraph boundary: it separates items (recorded on the next
                // item) and, within an item, starts a fresh content chunk.
                blank_pending = true;
                match items.last_mut() {
                    Some(item) => item.chunks.push(Vec::new()),
                    None => preamble.push(Vec::new()),
                }
                index += 1;
            }
            FlatItem::El(el) if is_item_command(el) => {
                let mut item = split_item_marker(el, cx);
                index += 1;
                if let Some((suffix, end)) = item_overlay_marker_suffix(&flat, index) {
                    item.marker.push_str(&suffix);
                    item.glue_body = flat.get(end).is_some_and(
                        |next| matches!(next, FlatItem::El(el) if el.kind() == SyntaxKind::COMMENT),
                    );
                    index = end;
                }
                item.blank_before = blank_pending;
                items.push(item);
                blank_pending = false;
            }
            FlatItem::El(el) => {
                match items.last_mut() {
                    Some(item) => item.chunks.last_mut().unwrap().push(el.clone()),
                    None => preamble.last_mut().unwrap().push(el.clone()),
                }
                blank_pending = false;
                index += 1;
            }
        }
    }

    if items.is_empty() {
        return None;
    }

    let mut segments: Vec<Ir> = Vec::new();
    let mut seps: Vec<Ir> = Vec::new();
    let preamble_ir = lower_item_chunks(&preamble, cx);
    if !matches!(preamble_ir, Ir::Nil) {
        seps.push(Ir::hard_line()); // unused (segment 0 has no preceding separator)
        segments.push(preamble_ir);
    }
    for item in &items {
        seps.push(if item.blank_before {
            Ir::empty_line()
        } else {
            Ir::hard_line()
        });
        segments.push(render_list_item(item, cx));
    }

    let mut result: Vec<Ir> = Vec::with_capacity(segments.len().saturating_mul(2));
    for (i, segment) in segments.into_iter().enumerate() {
        if i > 0 {
            result.push(seps[i].clone());
        }
        result.push(segment);
    }
    Some(Ir::concat(result))
}

/// Render one [`ListItem`]: any bound doc-comment lines on their own lines above,
/// then the marker, then a space and the item's body reflowed inside an
/// [`Ir::align`] whose width comes from [`LowerCtx::item_indent`]. The `hang`
/// width is the control word plus its separating space (`\item `), deliberately
/// excluding a label or overlay. A comment glued to an overlay receives no
/// separating space; an empty item (marker with no body) renders as the bare
/// marker.
fn render_list_item(item: &ListItem, cx: LowerCtx<'_>) -> Ir {
    let content = lower_item_chunks(&item.chunks, cx);
    let marker = Ir::verbatim(item.marker.clone());
    let body = if matches!(content, Ir::Nil) {
        marker
    } else {
        let separator = if item.glue_body {
            Ir::Nil
        } else {
            Ir::verbatim(" ")
        };
        let continuation_indent = match cx.item_indent {
            ItemIndent::Hang => item.hang,
            ItemIndent::Indent => cx.indent_width,
            ItemIndent::None => 0,
        };
        Ir::concat([marker, separator, Ir::align(continuation_indent, content)])
    };
    if item.doc_lines.is_empty() {
        return body;
    }
    let doc = item.doc_lines.iter().map(|line| Ir::verbatim(line.clone()));
    Ir::concat([Ir::join(Ir::hard_line(), doc), Ir::hard_line(), body])
}

/// Lower an item body's paragraph chunks, dispatching on the wrap mode: a
/// prose-wrapping mode reflows each chunk to width ([`reflow_chunks`]), while
/// [`WrapMode::Preserve`] keeps the author's breaks and inner spacing byte-faithful
/// ([`preserve_chunks`]). Either way the result sits inside the item's hanging
/// [`Ir::align`], so continuation lines indent under the marker.
fn lower_item_chunks(chunks: &[Vec<SyntaxElement>], cx: LowerCtx<'_>) -> Ir {
    if cx.wraps_prose() {
        reflow_chunks(chunks, cx)
    } else {
        preserve_chunks(chunks, cx)
    }
}

/// Preserve-mode analogue of [`reflow_chunks`]: lower each paragraph chunk as prose
/// under [`WrapMode::Preserve`] ([`lower_prose_stream`]) so the author's line breaks
/// become `hard_line`s while inter-word spacing collapses to a single space, then
/// join the (non-empty) chunks with an [`Ir::empty_line`]. Inline-prose command
/// bodies are flattened in first (matching the paragraph path), so an `\emph{…}`
/// body collapses too while an opaque argument group stays verbatim. Each chunk's
/// own edge breaks are trimmed — the leading whitespace after `\item ` and any
/// trailing break — so the first line glues after the marker and no blank line
/// leaks; the interior newlines survive as `hard_line`s that hang under the marker
/// via the enclosing [`Ir::align`].
fn preserve_chunks(chunks: &[Vec<SyntaxElement>], cx: LowerCtx<'_>) -> Ir {
    let parts = chunks
        .iter()
        .map(|chunk| {
            let flat = flatten_inline_prose(chunk.clone(), cx, false);
            let ir = Ir::concat(lower_prose_stream(flat.into_iter(), cx));
            let (_, ir) = peel_leading_break(ir);
            let (_, ir) = peel_trailing_break(ir);
            ir
        })
        .filter(|ir| !matches!(ir, Ir::Nil));
    Ir::join(Ir::empty_line(), parts)
}

/// Reflow each paragraph chunk of an item body and join the (non-empty) results
/// with an [`Ir::empty_line`], so a blank line inside an item becomes a blank line
/// between its paragraphs (still under the hanging indent).
fn reflow_chunks(chunks: &[Vec<SyntaxElement>], cx: LowerCtx<'_>) -> Ir {
    let parts = chunks
        .iter()
        .map(|chunk| reflow_elements(chunk.iter().cloned(), cx, ReflowKind::Prose))
        .filter(|ir| !matches!(ir, Ir::Nil));
    Ir::join(Ir::empty_line(), parts)
}

/// Flatten a list-environment body into a stream of inline elements, reifying each
/// paragraph boundary as a [`FlatItem::Blank`]. Body-level trivia is classified by
/// its newline count: a blank-line run (`≥2` newlines) becomes the `Blank` (its
/// tokens are dropped — the boundary carries them), while a single-newline run is
/// kept in the stream so [`reflow_elements`] still sees the line break — dropping
/// it would glue a body-level own-line `%` onto the preceding content or a nested
/// `\end{…}` (issue #48). Leading and trailing runs (against `\begin`/`\end`) are
/// dropped either way; the list framing re-supplies those breaks.
fn flatten_list_body(
    body_elements: &[SyntaxElement],
    lifted: Option<&SyntaxToken>,
) -> Vec<FlatItem> {
    let mut out: Vec<FlatItem> = Vec::new();
    let mut started = false;
    // Pending body-level trivia run: its tokens and how many newlines it spans.
    let mut run: Vec<SyntaxElement> = Vec::new();
    let mut run_newlines = 0usize;
    for element in body_elements {
        match element {
            SyntaxElement::Token(t) if is_collapsible_trivia(t.kind()) => {
                if t.kind() == SyntaxKind::NEWLINE {
                    run_newlines += 1;
                }
                run.push(element.clone());
                continue;
            }
            other if is_lifted_comment(other, lifted) => continue,
            _ => {}
        }
        if started {
            if run_newlines >= 2 {
                out.push(FlatItem::Blank);
            } else {
                out.extend(run.drain(..).map(FlatItem::El));
            }
        }
        run.clear();
        run_newlines = 0;
        match element {
            SyntaxElement::Node(p) if p.kind() == SyntaxKind::PARAGRAPH => {
                out.extend(
                    p.children_with_tokens()
                        .filter(|e| !is_lifted_comment(e, lifted))
                        .map(FlatItem::El),
                );
            }
            other => out.push(FlatItem::El(other.clone())),
        }
        started = true;
    }
    out
}

/// Whether `el` is a `\item` command node — the marker that starts a new list
/// item.
fn is_item_command(el: &SyntaxElement) -> bool {
    el.as_node().is_some_and(|node| {
        node.kind() == SyntaxKind::COMMAND && command_name(node).as_deref() == Some("item")
    })
}

/// Read Beamer's bounded `\item<overlay>[label]<overlay>` suffix from the flat
/// list stream. The parser deliberately leaves angle-delimited syntax generic,
/// so list lowering recognizes only the complete, immediately following shape;
/// incomplete angle text remains ordinary item content.
fn item_overlay_marker_suffix(flat: &[FlatItem], start: usize) -> Option<(String, usize)> {
    let (mut suffix, mut end) = angle_suffix(flat, start)?;
    if let Some((label, label_end)) = bracket_suffix(flat, end) {
        suffix.push_str(&label);
        end = label_end;
        if let Some((overlay, overlay_end)) = angle_suffix(flat, end) {
            suffix.push_str(&overlay);
            end = overlay_end;
        }
    }
    Some((suffix, end))
}

fn angle_suffix(flat: &[FlatItem], start: usize) -> Option<(String, usize)> {
    let mut index = skip_flat_trivia(flat, start);
    let first = flat_element(flat.get(index)?)?;
    let first_text = element_source_text(first);
    if !first_text.starts_with('<') {
        return None;
    }

    let mut suffix = String::new();
    loop {
        let element = flat_element(flat.get(index)?)?;
        if element.kind() == SyntaxKind::COMMENT {
            return None;
        }
        if is_collapsible_trivia(element.kind()) {
            if !suffix.ends_with(' ') {
                suffix.push(' ');
            }
        } else {
            let text = element_source_text(element);
            suffix.push_str(&text);
            if text.ends_with('>') {
                return Some((suffix, index + 1));
            }
        }
        index += 1;
    }
}

fn bracket_suffix(flat: &[FlatItem], start: usize) -> Option<(String, usize)> {
    let mut index = skip_flat_trivia(flat, start);
    let first = flat_element(flat.get(index)?)?;
    if first.kind() != SyntaxKind::L_BRACKET {
        return None;
    }

    let mut depth = 0usize;
    let mut suffix = String::new();
    loop {
        let element = flat_element(flat.get(index)?)?;
        match element.kind() {
            SyntaxKind::L_BRACKET => depth += 1,
            SyntaxKind::R_BRACKET => depth = depth.checked_sub(1)?,
            SyntaxKind::COMMENT => return None,
            _ => {}
        }
        if is_collapsible_trivia(element.kind()) {
            if !suffix.ends_with(' ') {
                suffix.push(' ');
            }
        } else {
            suffix.push_str(&element_source_text(element));
        }
        index += 1;
        if depth == 0 {
            return Some((suffix, index));
        }
    }
}

fn skip_flat_trivia(flat: &[FlatItem], mut index: usize) -> usize {
    while let Some(FlatItem::El(element)) = flat.get(index) {
        if !is_collapsible_trivia(element.kind()) {
            break;
        }
        index += 1;
    }
    index
}

fn flat_element(item: &FlatItem) -> Option<&SyntaxElement> {
    match item {
        FlatItem::El(element) => Some(element),
        FlatItem::Blank => None,
    }
}

fn element_source_text(element: &SyntaxElement) -> String {
    match element {
        SyntaxElement::Node(node) => node.text().to_string(),
        SyntaxElement::Token(token) => token.text().to_string(),
    }
}

/// Split a `\item` command node into a [`ListItem`] (`blank_before` is the
/// caller's to set): the rendered marker string (the control word plus any leading
/// optional `[label]`, the only argument an item marker takes), the *hang* width
/// for continuation lines (the control word's rendered width plus one for the
/// separating space — deliberately excluding the `[label]` so a wide `description`
/// label does not deepen the body indent), and the trailing elements that are
/// really body content — a `{…}` group the greedy parser over-attached, which
/// belongs to the item body, not the marker. A `DOC_COMMENT` bound leading into
/// the `\item` yields the item's `doc_lines`, never marker or content.
fn split_item_marker(el: &SyntaxElement, cx: LowerCtx<'_>) -> ListItem {
    let node = el.as_node().expect("item command is a node");
    let mut doc_lines: Vec<String> = Vec::new();
    let mut marker_parts: Vec<Ir> = Vec::new();
    let mut content: Vec<SyntaxElement> = Vec::new();
    let mut hang = 1; // the space separating the marker from the body
    let mut in_content = false;
    for child in node.children_with_tokens() {
        if in_content {
            content.push(child);
            continue;
        }
        match &child {
            SyntaxElement::Node(n) if n.kind() == SyntaxKind::DOC_COMMENT => {
                doc_lines.extend(
                    n.children_with_tokens()
                        .filter_map(|e| e.into_token())
                        .filter(|t| t.kind() == SyntaxKind::COMMENT)
                        .map(|t| t.text().to_string()),
                );
            }
            SyntaxElement::Token(t) if t.kind() == SyntaxKind::CONTROL_WORD => {
                hang += t.text().chars().count();
                marker_parts.push(Ir::verbatim(t.text()));
            }
            // Trivia between the control word and an optional label is not part of
            // the marker.
            SyntaxElement::Token(t) if is_collapsible_trivia(t.kind()) => {}
            SyntaxElement::Node(n) if n.kind() == SyntaxKind::OPTIONAL => {
                marker_parts.push(lower_node(n, cx));
            }
            // A brace group (or anything else) is body content, not the marker.
            other => {
                in_content = true;
                content.push(other.clone());
            }
        }
    }
    let marker = Printer::new(FormatStyle::default()).print_flat(&Ir::concat(marker_parts));
    ListItem {
        doc_lines,
        marker,
        hang,
        glue_body: false,
        chunks: vec![content],
        blank_before: false,
    }
}

/// True if `node` (an `ENVIRONMENT`) names an environment the signature DB marks
/// `align` — an `align`/matrix-family environment whose `&` columns the formatter
/// lays out into a grid (see [`lower_aligned_environment`]).
fn is_alignment_env(node: &SyntaxNode, cx: LowerCtx<'_>) -> bool {
    cx.signatures
        .environment_at(node)
        .is_some_and(|sig| sig.align)
}

/// True if `node` (an `ENVIRONMENT`) carries a **top-level `&`** — an alignment tab
/// that is a direct child of the body (or of its single wrapping `PARAGRAPH`), not
/// nested inside a group or sub-environment. A `&` at catcode 4 reading as a column
/// tab is a static CST-shape fact, so an environment the signature DB cannot name
/// (`myaligned`, issue #84) still routes to the `&`-column grid when it is shaped
/// like one. It mirrors the cell boundary [`build_alignment_grid`]/`flatten_alignment_body`
/// use, so the routing decision and the grid it enables agree on what a top-level
/// `&` is; a nested `&` lives in a child node and is correctly invisible. Deliberately
/// keyed on `&` alone (not `\\`): a `\\`-only body is a line stack, not a column
/// alignment, and gridding an arbitrary `\begin{center}a \\ b\end{center}` would
/// reflow it.
fn body_has_top_level_ampersand(node: &SyntaxNode) -> bool {
    node.children_with_tokens().any(|el| match el {
        SyntaxElement::Token(t) => t.kind() == SyntaxKind::AMPERSAND,
        SyntaxElement::Node(p) if p.kind() == SyntaxKind::PARAGRAPH => p
            .children_with_tokens()
            .any(|g| g.kind() == SyntaxKind::AMPERSAND),
        _ => false,
    })
}

/// True if `node` (an `ENVIRONMENT`) names an environment the signature DB marks
/// `math` — `equation`, `align`, `gather`, matrix, … The parser wraps such a body
/// in a `MATH` node (it entered math mode); [`lower_math_environment`] lays it out
/// with the math-aware paths.
fn is_math_env(node: &SyntaxNode, cx: LowerCtx<'_>) -> bool {
    cx.signatures
        .environment_at(node)
        .is_some_and(|sig| sig.math)
}

/// One rendered grid cell: its flat, trimmed text, the number of columns it spans
/// (`1` for an ordinary cell, `n` for `\multicolumn{n}{…}{…}`), and an optional
/// alignment override (a `\multicolumn`'s own `{spec}`; `None` means "use the
/// column's declared alignment").
///
/// A *block* cell (`block` is `Some`, math grids only) is a cell that cannot
/// collapse to one line because it holds a nested multi-line construct — a block
/// environment (`\begin{aligned}…`, `\begin{cases}…`, a matrix), possibly inside
/// a `\left…\right` pair or a group. Its IR replaces `text` (which stays empty):
/// the first line continues the row and every later line hangs at the breaking
/// node's start column ([`Ir::Align`]), so a bare nested environment gets its
/// `\end{…}` directly under its `\begin{…}` and the body one indent step deeper.
/// A block cell is only ever the last cell of its row, never defines a column
/// width, and simply overflows — the same posture as a spanning cell.
struct Cell {
    text: String,
    span: usize,
    // Only a standalone caption row is independent of the column grid.
    caption: bool,
    align: Option<ColAlign>,
    block: Option<BlockCell>,
}

/// The rendered IR of a block cell (see [`Cell`]) plus its hang offset: the flat
/// width of the cell content *before* the breaking node (`= ` in
/// `= \begin{aligned}…`), which the renderer adds to the cell's start column so
/// the hanging lines anchor at the node itself.
struct BlockCell {
    hang: usize,
    ir: Ir,
}

/// One row of an alignment grid: its rendered cells, the flat text of the `\\` that
/// terminated the row (`None` for a final row written without a trailing line
/// break), and an optional end-of-line comment that trails the row (rendered
/// *after* the `\\`, so the break is never commented out).
struct AlignRow {
    cells: Vec<Cell>,
    line_break: Option<String>,
    trailing_comment: Option<String>,
}

/// One item in an alignment grid: a row, intertext, or a passthrough
/// line (comments, horizontal rules, or a leading math label). Only rows
/// contribute to column widths.
enum GridItem {
    Row(AlignRow),
    Passthrough(String),
    Intertext(Ir),
}

/// Lower an `align`/matrix-family environment, laying out its `&` columns into a
/// grid so the ampersands line up. The framing (`\begin`/`\end`, the indented
/// body with leading/trailing `hard_line`) is identical to [`lower_environment`];
/// only the body differs — it is the rendered grid rather than a generic element
/// stream.
///
/// Falls back to [`lower_environment`] whenever the body is not a clean
/// single-paragraph grid (see [`build_alignment_grid`]): a blank-line break, or a
/// cell that cannot collapse to one aligned line (a mid-row comment or a nested
/// block). Comment-only and rule-only lines (`\hline`, `\midrule`, …) are *not* a
/// reason to fall back — they are kept as passthrough lines between rows. The
/// fallback is always available, so an unhandled shape degrades to today's plain
/// indented body, never a panic or corruption.
fn lower_aligned_environment(node: &SyntaxNode, cx: LowerCtx<'_>) -> Ir {
    let EnvParts {
        leading,
        begin,
        body,
        end,
        lifted,
        // The `BEGIN` tail (see [`lower_begin`]) rides `body` as ordinary leading
        // elements: this path flattens the body itself, so it needs no splice.
        tail_len: _,
        body_header_token: _,
    } = split_environment(node, cx);

    let body_cx = cx.absorbing_trailing_control_newline(&body);
    let soften_nested_newlines = cx.in_dtx_doc_region && !is_math_env(node, cx);
    let Some(items) = build_alignment_grid(
        &body,
        body_cx,
        false,
        soften_nested_newlines,
        lifted.as_ref(),
    ) else {
        return lower_environment(node, cx);
    };
    if !items.iter().any(|item| matches!(item, GridItem::Row(_))) {
        // A body with no actual rows (empty, `\\`-only, or comment-only) has no
        // grid; let the generic path render it.
        return lower_environment(node, cx);
    }

    let aligns = column_alignments(node, cx).unwrap_or_default();
    let body = render_alignment_rows(&items, &aligns);
    Ir::concat([
        leading,
        begin,
        Ir::indent(Ir::concat([Ir::hard_line(), body])),
        Ir::hard_line(),
        end,
    ])
}

/// Lower a named **math** environment (`equation`, `align`, `gather`, matrix, …),
/// whose body the parser wrapped in a `MATH` node. Two layouts, chosen by the body's
/// shape:
///
/// - **Grid** (a top-level `&`, `\\`, or intertext): `align`/matrix column-and-row
///   grids, and `gather`/`multline` row stacks (a single column). Reuses
///   [`build_alignment_grid`] in `math` mode, so cells get role-aware math spacing.
/// - **Single formula** (none of those): `equation`/`displaymath`. Routes the
///   `MATH` body through [`lower_display_math_body`], the relation-aware
///   amsmath-style breaker, so a too-long formula breaks at its top-level
///   relations/operators.
///
/// Framing (leading, `\begin` header, indented body, `\end`) mirrors
/// [`lower_display_math`] and [`lower_aligned_environment`]. If the body is not a
/// `MATH` node — which only happens if the formatter's `math` signature view diverges
/// from the parser's built-in one — it falls back to [`lower_environment`] rather than
/// mislaying the body.
fn lower_math_environment(node: &SyntaxNode, cx: LowerCtx<'_>) -> Ir {
    let EnvParts {
        leading,
        begin,
        body: body_elements,
        end,
        lifted,
        // The `BEGIN` tail (see [`lower_begin`]) rides `body` as ordinary leading
        // elements: this path flattens the body itself, so it needs no splice.
        tail_len: _,
        body_header_token: _,
    } = split_environment(node, cx);
    let body_cx = cx.absorbing_trailing_control_newline(&body_elements);

    let Some(math_node) = body_elements
        .iter()
        .filter_map(|e| e.as_node())
        .find(|n| n.kind() == SyntaxKind::MATH)
    else {
        // The parser did not enter math mode for this environment (its built-in
        // signature is not `math`); render it generically.
        return lower_environment(node, cx);
    };

    // Intertext also separates equation rows when no explicit `\\` is present.
    let is_grid = math_node.children_with_tokens().any(|e| {
        matches!(e.kind(), SyntaxKind::AMPERSAND | SyntaxKind::LINE_BREAK)
            || grid_intertext(&e, body_cx).is_some()
    });

    let body = if let Some(body) =
        lower_tikzcd_arrow_body(node, &body_elements, lifted.as_ref(), body_cx)
    {
        body
    } else if is_grid {
        match build_alignment_grid(&body_elements, body_cx, true, false, lifted.as_ref()) {
            Some(items) if items.iter().any(|item| matches!(item, GridItem::Row(_))) => {
                let aligns = column_alignments(node, cx).unwrap_or_default();
                render_alignment_rows(&items, &aligns)
            }
            // A grid we cannot lay out on aligned rows (a mid-row comment, a blank
            // line, a nested block that is not the last cell of its row): fall back
            // to the generic environment lowering, exactly as
            // [`lower_aligned_environment`] does. A *trailing* nested block keeps
            // the grid — it renders as a hanging block cell (see [`Cell::block`]).
            _ => return lower_environment(node, cx),
        }
    } else {
        // Flatten the whole body, not only the parser's `MATH` child. Greedy
        // `\begin` attachment can leave a detached leading group in `body`
        // before that node (`\begin{equation}\n{\foo} : T`); selecting only
        // `math_node` would silently delete the group. The siblings are math
        // content too, and the display-formula lowerer handles them directly.
        let mut elements: Vec<SyntaxElement> = Vec::new();
        for element in &body_elements {
            if element.as_node() == Some(math_node) {
                elements.extend(math_node.children_with_tokens());
            } else {
                elements.push(element.clone());
            }
        }
        // Drop the lifted `\begin`-line comment (the `MATH` node's first token)
        // before the formula lowering, which would otherwise re-emit it.
        elements.retain(|e| !is_lifted_comment(e, lifted.as_ref()));
        if elements.iter().all(|e| {
            e.as_token()
                .is_some_and(|t| is_collapsible_trivia(t.kind()))
        }) {
            // Empty body (possibly only after the lift): `\begin` and `\end` on
            // adjacent lines, as [`lower_environment`]'s empty-body branch does,
            // rather than framing an empty indented line.
            return Ir::concat([leading, begin, Ir::hard_line(), end]);
        }
        trim_trailing_break(lower_display_formula_elements(&elements, body_cx))
    };

    Ir::concat([
        leading,
        begin,
        Ir::indent(Ir::concat([Ir::hard_line(), body])),
        Ir::hard_line(),
        end,
    ])
}

/// Explicit endpoints distinguish a diagram's trailing arrow declarations from
/// its cell-relative arrows. The suffix is content-derived, so inline and
/// multiline spellings produce the same layout without moving any row breaks.
fn lower_tikzcd_arrow_body(
    node: &SyntaxNode,
    body: &[SyntaxElement],
    lifted: Option<&SyntaxToken>,
    cx: LowerCtx<'_>,
) -> Option<Ir> {
    if Environment::cast(node.clone())?.name().as_deref() != Some("tikzcd") {
        return None;
    }
    let mut inline = flatten_alignment_body(body, cx, true)?;
    inline.retain(|element| !is_lifted_comment(element, lifted));
    let mut start = None;
    for (idx, element) in inline.iter().enumerate().rev() {
        if is_collapsible_trivia_element(element) || element.kind() == SyntaxKind::COMMENT {
            continue;
        }
        let Some(command) = element
            .as_node()
            .and_then(|node| Command::cast(node.clone()))
        else {
            break;
        };
        if !matches!(command.name().as_deref(), Some("arrow" | "ar"))
            || cx.suppressed(command.syntax().text_range())
            || command.groups().next().is_some()
        {
            break;
        }
        let mut optionals = command.optionals();
        let Some(optional) = optionals.next() else {
            break;
        };
        if optionals.next().is_some() || !tikzcd_arrow_has_endpoints(optional.syntax()) {
            break;
        }
        start = Some(idx);
    }
    let start = start?;
    let items = build_alignment_grid_inline(inline[..start].to_vec(), cx, true, false)?;
    let aligns = column_alignments(node, cx).unwrap_or_default();
    let mut parts = Vec::new();
    if !items.is_empty() {
        parts.extend([render_alignment_rows(&items, &aligns), Ir::hard_line()]);
    }
    let mut own_line = false;
    for element in &inline[start..] {
        match element {
            SyntaxElement::Token(token) if is_collapsible_trivia(token.kind()) => {
                own_line |= token.kind() == SyntaxKind::NEWLINE;
            }
            SyntaxElement::Token(token) if token.kind() == SyntaxKind::COMMENT => {
                parts.push(if own_line {
                    Ir::hard_line()
                } else {
                    Ir::text(" ")
                });
                parts.push(Ir::verbatim(token.text().trim_end()));
                own_line = false;
            }
            SyntaxElement::Node(command) => {
                if element != &inline[start] {
                    parts.push(Ir::hard_line());
                }
                for child in command.children_with_tokens() {
                    if let Some(comment) = child
                        .as_node()
                        .filter(|n| n.kind() == SyntaxKind::DOC_COMMENT)
                    {
                        parts.push(Ir::join(
                            Ir::hard_line(),
                            comment
                                .children_with_tokens()
                                .filter_map(SyntaxElement::into_token)
                                .filter(|token| token.kind() == SyntaxKind::COMMENT)
                                .map(|token| Ir::verbatim(token.text().trim_end())),
                        ));
                        parts.push(Ir::hard_line());
                    } else {
                        // Arrow options can contain quoted labels and nested TeX.
                        // Keep their bytes rather than applying math spacing or
                        // assuming a generic key-value argument's semantics.
                        parts.push(Ir::verbatim(child.to_string()));
                    }
                }
                own_line = false;
            }
            _ => unreachable!("the suffix contains only arrows, comments, and trivia"),
        }
    }
    Some(Ir::concat(parts))
}

/// Read only literal top-level keys. Braced values and commands remain opaque,
/// and commas inside quoted labels cannot introduce endpoint keys.
fn tikzcd_arrow_has_endpoints(optional: &SyntaxNode) -> bool {
    let children: Vec<_> = optional.children_with_tokens().collect();
    let mut entry = String::new();
    let mut quoted = false;
    let mut from = false;
    let mut to = false;
    let mut finish_entry = |entry: &str| {
        if let Some((key, value)) = entry.split_once('=') {
            match key.trim() {
                "from" => from = !value.trim().is_empty(),
                "to" => to = !value.trim().is_empty(),
                _ => {}
            }
        }
    };
    for child in &children[1..children.len() - 1] {
        match child {
            SyntaxElement::Token(token) if token.kind() == SyntaxKind::WORD => {
                for ch in token.text().chars() {
                    if ch == '"' {
                        quoted = !quoted;
                    }
                    if ch == ',' && !quoted {
                        finish_entry(&entry);
                        entry.clear();
                    } else {
                        entry.push(ch);
                    }
                }
            }
            SyntaxElement::Token(token) if is_collapsible_trivia(token.kind()) => entry.push(' '),
            SyntaxElement::Token(token)
                if matches!(
                    token.kind(),
                    SyntaxKind::COMMENT | SyntaxKind::L_BRACKET | SyntaxKind::R_BRACKET
                ) =>
            {
                return false;
            }
            _ => entry.push('\0'),
        }
    }
    finish_entry(&entry);
    !quoted && from && to
}

/// Split an alignment environment body into a sequence of grid items (rows and
/// passthrough lines), or `None` to signal the caller should fall back to the
/// generic environment lowering.
///
/// Rows are delimited by *top-level* `\\` ([`SyntaxKind::LINE_BREAK`]) nodes or
/// intertext commands in math, and cells by top-level `&` ([`SyntaxKind::AMPERSAND`])
/// tokens; a `&` nested inside a
/// group or sub-environment lives in a child node, never a direct body child, so
/// it is correctly invisible here. Each cell's elements lower through the generic
/// [`lower_element_stream`] and render *flat* (so inline math/groups normalize as
/// they do elsewhere), trimmed of surrounding space.
///
/// **Comments and rule lines.** A physical line between rows that is made up solely
/// of comments and/or horizontal-rule commands (`\hline`, `\midrule`, …) is kept as
/// a [`GridItem::Passthrough`] line, not a cell. A comment at the end of a row's
/// last physical line — directly after the row's `\\`, or trailing the final row —
/// is attached as the row's `trailing_comment`; comment-only lines after it are
/// passthrough lines like any other. A comment in the *middle* of a row (with more
/// cells after it) cannot sit on an aligned line — its text runs to end of line,
/// commenting out the rest — so it returns `None` and falls back.
///
/// Returns `None` when [`flatten_alignment_body`] rejects the body (a blank-line
/// break), when a cell carries a forced break that cannot collapse to one line (a
/// nested block, or a blank line inside the cell — a lone continuation newline is
/// joined, not a fallback), or on a mid-row comment. Exception: in a *math* grid, a
/// cell whose forced break comes from a nested block environment (`aligned`,
/// `cases`, a matrix) becomes a multi-line *block* cell ([`Cell::block`]) instead of
/// a fallback, provided it is the last cell of its row — the grid survives and the
/// nested environment's lines hang at its `\begin{…}` column.
///
/// `math` is `true` for math grids (`align`, `pmatrix`, …) whose body the parser
/// wrapped in a `MATH` node: the flattener descends that node and each cell lowers
/// through the role-aware math sequencer ([`lower_math_seq`]) so operator spacing
/// and tight scripts apply. It is `false` for a non-math grid (`tabular`),
/// where the body is a prose block and cells lower through [`lower_element_stream`]
/// exactly as before. `soften_nested_newlines` is restricted to non-math cells in
/// fully owned virtual `.dtx` regions: it makes a margin-framed continuation inside
/// a parser-attached command behave like the same continuation after margin
/// normalization, while ordinary tables and math-grid fallbacks retain their
/// existing forced-break gates.
fn build_alignment_grid(
    body_elements: &[SyntaxElement],
    cx: LowerCtx<'_>,
    math: bool,
    soften_nested_newlines: bool,
    lifted: Option<&SyntaxToken>,
) -> Option<Vec<GridItem>> {
    let mut inline = flatten_alignment_body(body_elements, cx, math)?;
    // The `\begin`-line trailing comment was lifted onto the header
    // ([`split_environment`]); drop it here so it is not re-emitted as a
    // passthrough line of its own.
    inline.retain(|e| !is_lifted_comment(e, lifted));
    build_alignment_grid_inline(inline, cx, math, soften_nested_newlines)
}

fn build_alignment_grid_inline(
    mut inline: Vec<SyntaxElement>,
    cx: LowerCtx<'_>,
    math: bool,
    soften_nested_newlines: bool,
) -> Option<Vec<GridItem>> {
    let printer = Printer::new(FormatStyle::default());
    let cell_cx = LowerCtx {
        in_alignment_cell: soften_nested_newlines,
        ..cx
    };

    /// Render the accumulated cell elements flat and trimmed, pushing the result
    /// onto `cells`. Returns `None` on a cell that cannot collapse to one line.
    ///
    /// Collapsible trivia at the cell's edges is dropped first: the structural
    /// newline after `\begin`/each `\\` and the indentation before the next cell
    /// are *boundary* whitespace, not cell content; left in, the leading newline
    /// would lower to a forced break (an [`Ir::hard_line`]) and spuriously trip the
    /// fallback. A lone newline *inside* a cell is a continuation line; it lowers to
    /// a top-level [`Ir::HardLine`], which we collapse to a space so the cell stays
    /// on one aligned row. A blank line (`\par`, an [`Ir::EmptyLine`]) in a cell, or
    /// a forced break nested inside a child block (`\begin{cases}…`), is *not*
    /// collapsed and still (correctly) falls back — it cannot sit on one aligned row.
    fn finish_cell(
        cell: &mut Vec<SyntaxElement>,
        cells: &mut Vec<Cell>,
        printer: &Printer,
        cx: LowerCtx<'_>,
        math: bool,
    ) -> Option<()> {
        let is_edge_trivia = |e: &SyntaxElement| {
            e.as_token()
                .is_some_and(|t| is_collapsible_trivia(t.kind()))
        };
        while cell.first().is_some_and(&is_edge_trivia) {
            cell.remove(0);
        }
        while cell.last().is_some_and(&is_edge_trivia) {
            cell.pop();
        }
        // Read the `\multicolumn` span/alignment before the cell is drained below.
        let (span, align) = detect_multicolumn(cell);
        let mut content = cell
            .iter()
            .filter(|element| !is_collapsible_trivia_element(element));
        let caption = !math
            && content
                .next()
                .and_then(SyntaxElement::as_node)
                .is_some_and(|node| {
                    node.kind() == SyntaxKind::COMMAND
                        && matches!(
                            command_name(node).as_deref(),
                            Some("caption" | "caption*" | "captionof" | "captionof*")
                        )
                })
            && content.next().is_none();
        // A comment in a cell is handled by the caller (passthrough / trailing /
        // fallback) and never reaches here in a handled case; this guard keeps the
        // fallback safe if one ever slips through an unmodeled path.
        if cell.iter().any(|e| {
            e.as_token()
                .is_some_and(|t| t.kind() == SyntaxKind::COMMENT)
        }) {
            return None;
        }
        // A lone interior newline classifies to a top-level `Ir::HardLine`
        // (`classify_trivia`); collapse it to a space so a continuation line joins
        // onto its aligned row. A blank line inside a cell is an `Ir::EmptyLine`
        // (untouched here), and a nested block's breaks live inside a child `Ir`, so
        // both keep tripping `contains_forced_break` below and fall back.
        //
        // A math cell lowers through the role-aware sequencer, which already collapses
        // an interior whitespace/newline run to a single space (so a continuation line
        // joins) and applies operator spacing; a blank line still surfaces as a forced
        // break and (correctly) falls back below, but a nested block environment's
        // break yields a *block* cell instead (see [`Cell::block`]) so the grid
        // survives a `\begin{aligned}…`/`\begin{cases}…`/matrix cell.
        //
        // Block eligibility is read off the elements before they are drained. The
        // anchor is the first *node* child whose own lowering cannot stay flat —
        // a nested block environment, possibly wrapped in `\left…\right` or a
        // group. Only node children can carry a forced break here (comments
        // bailed above, a `\\` never lands inside a cell), so any break in the
        // full cell IR below is that node's structured layout, safe to hang at
        // its column. A verbatim-bodied environment anywhere in the cell's
        // subtree, or a blank line of the cell's own, still falls back.
        let first_block = if math {
            cell.iter().position(|e| {
                e.as_node().is_some()
                    && lower_math_element(e.clone(), cx, MathSpacing::Normal)
                        .contains_forced_break()
            })
        } else {
            None
        };
        let block_eligible = first_block.is_some()
            && !cell.iter().filter_map(|e| e.as_node()).any(|n| {
                n.descendants()
                    .any(|d| d.kind() == SyntaxKind::ENVIRONMENT && has_verbatim_body(&d))
            })
            && !cell_has_blank_line(cell);
        // The hang offset anchors a block cell's continuation lines at the
        // breaking node's start column: the flat width of the cell content before
        // it, plus the one joining space the sequencer places before it (a
        // relation/operator prefix like `= ` always gets one; the tight operand
        // juxtaposition `2\begin{…}` would not, costing one cosmetic column in
        // that unwritten shape). Computed before the drain below.
        let hang = match first_block.filter(|_| block_eligible) {
            None | Some(0) => 0,
            Some(i) => {
                let prefix =
                    lower_math_seq(cell[..i].iter().cloned(), cx, MathSpacing::Normal, false);
                let width = printer.print_flat(&prefix).trim().chars().count();
                if width == 0 { 0 } else { width + 1 }
            }
        };
        let ir = if math {
            lower_math_seq(cell.drain(..), cx, MathSpacing::Normal, false)
        } else {
            let joined = lower_element_stream(cell.drain(..), cx)
                .into_iter()
                .map(|ir| {
                    if matches!(ir, Ir::HardLine) {
                        Ir::line()
                    } else {
                        ir
                    }
                })
                .collect::<Vec<_>>();
            Ir::concat(joined)
        };
        if ir.contains_forced_break() {
            if !block_eligible {
                return None;
            }
            cells.push(Cell {
                text: String::new(),
                span,
                caption,
                align,
                block: Some(BlockCell { hang, ir }),
            });
            return Some(());
        }
        // Preserved breaks are nonstructural, but still emit newlines in flat
        // mode. Embedding them in a row's `Ir::Text` would bypass margin prefixes.
        printer.flat_width(&ir)?;
        cells.push(Cell {
            text: printer.print_flat(&ir).trim().to_string(),
            span,
            caption,
            align,
            block: None,
        });
        Some(())
    }

    /// Inspect a not-yet-drained cell for a lone `\multicolumn{n}{spec}{body}`,
    /// returning its column span and the alignment from its `{spec}` (`(1, None)`
    /// for any ordinary cell). The greedy parser attaches all three `{…}` groups to
    /// the `\multicolumn` `COMMAND`, so the cell is a single command node; a
    /// non-integer span or unparsable spec degrades to span 1 / no override.
    fn detect_multicolumn(cell: &[SyntaxElement]) -> (usize, Option<ColAlign>) {
        let mut content = cell.iter().filter(|e| {
            !e.as_token()
                .is_some_and(|t| is_collapsible_trivia(t.kind()))
        });
        let Some(first) = content.next() else {
            return (1, None);
        };
        if content.next().is_some() {
            return (1, None);
        }
        let Some(node) = first.as_node() else {
            return (1, None);
        };
        if node.kind() != SyntaxKind::COMMAND
            || command_name(node).as_deref() != Some("multicolumn")
        {
            return (1, None);
        }
        let span = crate::ast::nth_group_text(node, 0)
            .and_then(|t| t.trim().parse::<usize>().ok())
            .filter(|&n| n >= 1);
        let align = crate::ast::nth_group(node, 1)
            .map(|g| crate::ast::group_inner_source(&g))
            .and_then(|s| colspec::parse_column_spec(&s))
            .and_then(|v| v.first().copied());
        (span.unwrap_or(1), align)
    }

    let mut items: Vec<GridItem> = Vec::new();
    let mut cells: Vec<Cell> = Vec::new();
    let mut cell: Vec<SyntaxElement> = Vec::new();

    let mut idx = 0;
    let mut leading = math;
    while idx < inline.len() {
        // A row boundary: no committed cells and the current cell holds only
        // boundary trivia. Only here can a non-row (passthrough / trailing-comment)
        // line begin.
        let at_boundary = cells.is_empty() && cell_is_blank(&cell);
        if math && let Some((mut text, tail)) = grid_intertext(&inline[idx], cell_cx) {
            // Intertext ends the preceding equation even without an explicit
            // `\\`, and its text must not contribute to formula column widths.
            if !at_boundary {
                finish_cell(&mut cell, &mut cells, &printer, cell_cx, math)?;
                items.push(GridItem::Row(AlignRow {
                    cells: std::mem::take(&mut cells),
                    line_break: None,
                    trailing_comment: None,
                }));
            }
            cell.clear();
            inline.splice(idx + 1..idx + 1, tail);
            idx += 1;
            let mut comment_idx = idx;
            while inline
                .get(comment_idx)
                .is_some_and(|element| element.kind() == SyntaxKind::WHITESPACE)
            {
                comment_idx += 1;
            }
            if let Some(token) = inline.get(comment_idx).and_then(SyntaxElement::as_token)
                && token.kind() == SyntaxKind::COMMENT
            {
                text = Ir::concat([text, Ir::text(" "), Ir::verbatim(token.text().trim_end())]);
                idx = comment_idx + 1;
            }
            items.push(GridItem::Intertext(text));
            leading = false;
            continue;
        }
        if at_boundary
            && is_comment_or_rule_start(&inline[idx], cx)
            && let Some(line) = non_row_line(&inline, idx, &printer, cx)
        {
            // A comment on its own line (a newline separates it from the previous
            // grid token), or any non-row line with no row yet before it, is a
            // passthrough between rows.
            let own_line = cell_has_newline(&cell);
            let prev_is_row = matches!(items.last(), Some(GridItem::Row(_)));
            if own_line || !prev_is_row {
                items.push(GridItem::Passthrough(line.text));
                cell.clear();
                idx = line.next;
                continue;
            }
            // Not on its own line: it directly follows the previous row's `\\`.
            if line.has_rule {
                // The `\\ \hline` form — a rule sharing the physical line with the
                // preceding row's `\\`. Normalize it onto its own passthrough line
                // (idempotent: on re-parse it reads as an own-line rule).
                items.push(GridItem::Passthrough(line.text));
            } else if let Some(GridItem::Row(row)) = items.last_mut() {
                // A pure comment there trails that row.
                row.trailing_comment = Some(line.text);
            }
            cell.clear();
            idx = line.next;
            continue;
        }

        if leading
            && at_boundary
            && let Some((text, next)) = leading_grid_label(&inline, idx, &printer, cell_cx)
        {
            // Equation bookkeeping must not widen the first formula column.
            items.push(GridItem::Passthrough(text));
            cell.clear();
            idx = next;
            continue;
        }
        if !is_collapsible_trivia_element(&inline[idx]) {
            leading = false;
        }

        match &inline[idx] {
            SyntaxElement::Token(token) if token.kind() == SyntaxKind::AMPERSAND => {
                finish_cell(&mut cell, &mut cells, &printer, cell_cx, math)?;
                // A block cell may only end its row: a `&` after one would need
                // the next cell to align past the block's last line, which the
                // grid cannot lay out — fall back.
                if cells.last().is_some_and(|c| c.block.is_some()) {
                    return None;
                }
            }
            SyntaxElement::Node(child) if child.kind() == SyntaxKind::LINE_BREAK => {
                finish_cell(&mut cell, &mut cells, &printer, cell_cx, math)?;
                let line_break = printer
                    .print_flat(&lower_node(child, cx))
                    .trim()
                    .to_string();
                items.push(GridItem::Row(AlignRow {
                    cells: std::mem::take(&mut cells),
                    line_break: Some(line_break),
                    trailing_comment: None,
                }));
            }
            SyntaxElement::Token(token) if token.kind() == SyntaxKind::COMMENT => {
                let before_intertext = math
                    && inline[idx + 1..]
                        .iter()
                        .find(|element| {
                            !is_collapsible_trivia_element(element)
                                && element.kind() != SyntaxKind::COMMENT
                        })
                        .is_some_and(|element| grid_intertext(element, cell_cx).is_some());
                if before_intertext {
                    // Intertext proves the equation ends here, so a comment
                    // cannot hide later cells when this row is flattened.
                    let own_line = cell
                        .iter()
                        .rev()
                        .take_while(|element| is_collapsible_trivia_element(element))
                        .any(|element| element.kind() == SyntaxKind::NEWLINE);
                    let text = token.text().trim_end().to_string();
                    finish_cell(&mut cell, &mut cells, &printer, cell_cx, math)?;
                    items.push(GridItem::Row(AlignRow {
                        cells: std::mem::take(&mut cells),
                        line_break: None,
                        trailing_comment: (!own_line).then(|| text.clone()),
                    }));
                    if own_line {
                        items.push(GridItem::Passthrough(text));
                    }
                    idx += 1;
                    continue;
                }
                // A comment that is *not* at a boundary trails cell content. It is
                // clean only when nothing more of the row can follow: trivia and
                // own-line comments (each a passthrough line, handled by the
                // boundary branch above on later iterations) may remain; anything
                // else would be commented out by joining onto this line — fall
                // back.
                if !rest_is_trivia_and_comment_lines(&inline, idx + 1) {
                    return None;
                }
                let text = token.text().trim_end().to_string();
                finish_cell(&mut cell, &mut cells, &printer, cell_cx, math)?;
                items.push(GridItem::Row(AlignRow {
                    cells: std::mem::take(&mut cells),
                    line_break: None,
                    trailing_comment: Some(text),
                }));
            }
            _ => cell.push(inline[idx].clone()),
        }
        idx += 1;
    }

    // The final segment (content after the last `\\` or trailing comment). Drop it
    // when it is a single empty cell — the "body ended in `\\`" (or in a
    // trailing-comment row) case — so the trailing break stays on the prior row
    // without adding a blank line; otherwise it is a real last row.
    finish_cell(&mut cell, &mut cells, &printer, cell_cx, math)?;
    let final_is_empty = cells.len() == 1
        && cells[0].text.is_empty()
        && cells[0].block.is_none()
        && cells[0].span == 1;
    if !final_is_empty {
        items.push(GridItem::Row(AlignRow {
            cells,
            line_break: None,
            trailing_comment: None,
        }));
    }

    Some(items)
}

/// Split off the single text argument of an intertext command. The greedy
/// parser may attach the following formula's leading groups, so return those
/// original elements to the grid instead of including them in the text line.
fn grid_intertext(element: &SyntaxElement, cx: LowerCtx<'_>) -> Option<(Ir, Vec<SyntaxElement>)> {
    let command = Command::cast(element.as_node()?.clone())?;
    if !matches!(command.name()?.as_str(), "intertext" | "shortintertext")
        || cx.suppressed(command.syntax().text_range())
    {
        return None;
    }
    // A redefinition with different arity no longer proves a text-row boundary.
    let signature = cx.signatures.command_at(command.syntax())?;
    if signature.args.len() != 1
        || !signature.args[0].required
        || signature.args[0].kind != ArgKind::Brace
    {
        return None;
    }
    let children: Vec<_> = command.syntax().children_with_tokens().collect();
    let argument = children
        .iter()
        .position(|child| matches!(child.kind(), SyntaxKind::GROUP | SyntaxKind::OPTIONAL))?;
    if children[argument].kind() != SyntaxKind::GROUP
        || children[..argument].iter().any(|child| {
            !matches!(
                child.kind(),
                SyntaxKind::CONTROL_WORD | SyntaxKind::DOC_COMMENT
            ) && !is_collapsible_trivia_element(child)
        })
    {
        return None;
    }
    let mut text = lower_element_stream(
        children[..argument]
            .iter()
            .filter(|child| !is_collapsible_trivia_element(child))
            .cloned(),
        cx,
    );
    text.push(lower_node(children[argument].as_node()?, cx));
    Some((Ir::concat(text), children[argument + 1..].to_vec()))
}

/// Give a leading math label its own line without moving formula content that
/// the greedy parser attached as extra arguments. A trailing comment stays with
/// the label, while a bound own-line comment stays above it.
fn leading_grid_label(
    inline: &[SyntaxElement],
    start: usize,
    printer: &Printer,
    cx: LowerCtx<'_>,
) -> Option<(String, usize)> {
    let (label, rest) = split_leading_label(&inline[start..])?;
    let command = Command::cast(label.as_node()?.clone())?;
    let node = command.syntax();
    if command.groups().count() != 1
        || command.optionals().next().is_some()
        || cx.suppressed(node.text_range())
    {
        return None;
    }
    let comment = node
        .children()
        .find(|child| child.kind() == SyntaxKind::DOC_COMMENT);
    let label_cx = LowerCtx {
        omitted_leading_comment: comment.as_ref().map(SyntaxNode::text_range),
        ..cx
    };
    let ir = lower_math_element(label, label_cx, MathSpacing::Normal);
    printer.flat_width(&ir)?;
    let mut text = comment
        .map(|comment| printer.print_flat(&lower_node(&comment, cx)))
        .unwrap_or_default();
    text.push_str(printer.print_flat(&ir).trim());

    let mut next = inline.len() - rest.len();
    let mut comment_idx = next;
    while inline.get(comment_idx).is_some_and(|element| {
        element
            .as_token()
            .is_some_and(|token| token.kind() == SyntaxKind::WHITESPACE)
    }) {
        comment_idx += 1;
    }
    if let Some(token) = inline.get(comment_idx).and_then(SyntaxElement::as_token)
        && token.kind() == SyntaxKind::COMMENT
    {
        text.push(' ');
        text.push_str(token.text().trim_end());
        next = comment_idx + 1;
    }
    Some((text, next))
}

/// A non-row line recognized at a grid boundary: its rendered text and the index
/// at which the body resumes (past the line's terminating newline).
struct NonRowLine {
    text: String,
    next: usize,
    has_rule: bool,
}

/// Try to read a *non-row* line — one made up solely of comments, horizontal-rule
/// commands (`\hline`, `\midrule`, …), and inline whitespace — starting at `start`
/// (which the caller guarantees is a comment or rule command). Returns `None` when
/// the line contains anything else (a cell, a `&`, a `\\`), so the caller treats it
/// as ordinary cell content. The rendered text is the line flattened and trimmed
/// (comments verbatim), exactly as cells and `\\` are rendered.
fn non_row_line(
    inline: &[SyntaxElement],
    start: usize,
    printer: &Printer,
    cx: LowerCtx<'_>,
) -> Option<NonRowLine> {
    let mut i = start;
    let mut content_end = start;
    let mut has_rule = false;
    let mut has_comment = false;
    while i < inline.len() {
        match &inline[i] {
            SyntaxElement::Token(t) if t.kind() == SyntaxKind::NEWLINE => break,
            SyntaxElement::Token(t) if t.kind() == SyntaxKind::WHITESPACE => {}
            SyntaxElement::Token(t) if t.kind() == SyntaxKind::COMMENT => {
                // A comment runs to end of line, so it is the line's last content.
                has_comment = true;
                i += 1;
                content_end = i;
                break;
            }
            SyntaxElement::Node(n) if n.kind() == SyntaxKind::COMMAND && is_rule_command(n, cx) => {
                has_rule = true;
                i += 1;
                content_end = i;
                continue;
            }
            // A bare rule control word — the peeled prefix of an over-attaching rule
            // command ([`rule_overattaches_cell`]), whose trailing `{…}` cell now
            // follows as its own element.
            SyntaxElement::Token(t) if token_is_rule_word(t, cx) => {
                has_rule = true;
                i += 1;
                content_end = i;
                continue;
            }
            // The booktabs `\cmidrule(lr){2-3}` paren trim spec. `(lr)` is generic
            // catcode-12 text (a `WORD`) that breaks the greedy argument attach, so
            // it and the following detached `{2-3}` range group arrive as loose
            // siblings after the rule command. Recognizing them as part of the rule
            // line is a layout decision (the rule-line concept is the formatter's).
            SyntaxElement::Token(t) if has_rule && is_paren_trim_word(t) => {
                i += 1;
                content_end = i;
                continue;
            }
            SyntaxElement::Node(n) if has_rule && n.kind() == SyntaxKind::GROUP => {
                i += 1;
                content_end = i;
                continue;
            }
            _ => return None,
        }
        i += 1;
    }
    if !(has_rule || has_comment) {
        return None;
    }
    // Resume past the line's terminating newline (and any trailing whitespace).
    let mut next = content_end;
    while next < inline.len() {
        match &inline[next] {
            SyntaxElement::Token(t) if t.kind() == SyntaxKind::WHITESPACE => next += 1,
            SyntaxElement::Token(t) if t.kind() == SyntaxKind::NEWLINE => {
                next += 1;
                break;
            }
            _ => break,
        }
    }
    let ir = Ir::concat(lower_element_stream(
        inline[start..content_end].iter().cloned(),
        cx,
    ));
    let text = printer.print_flat(&ir).trim().to_string();
    Some(NonRowLine {
        text,
        next,
        has_rule,
    })
}

/// Whether `element` begins a candidate non-row line — a comment, or a command the
/// signature DB flags as a horizontal rule (`\hline`, `\midrule`, …).
fn is_comment_or_rule_start(element: &SyntaxElement, cx: LowerCtx<'_>) -> bool {
    match element {
        SyntaxElement::Token(t) => t.kind() == SyntaxKind::COMMENT || token_is_rule_word(t, cx),
        SyntaxElement::Node(n) => n.kind() == SyntaxKind::COMMAND && is_rule_command(n, cx),
    }
}

/// Whether `token` is a bare control word naming a horizontal-rule command per the
/// signature DB — the peeled prefix of an over-attaching rule command (see
/// [`rule_overattaches_cell`]), recognized as a rule the same way an intact rule
/// `COMMAND` node is ([`is_rule_command`]).
fn token_is_rule_word(token: &SyntaxToken, cx: LowerCtx<'_>) -> bool {
    token.kind() == SyntaxKind::CONTROL_WORD
        && cx
            .signatures
            .command(token.text().trim_start_matches('\\'))
            .is_some_and(|sig| sig.rule)
}

/// Whether `token` is a booktabs `\cmidrule` paren trim spec — a `WORD` of the form
/// `(l)`, `(r)`, `(lr)`, or `(rl)` (catcode-12 text the lexer globs into one token).
/// `pub` because the linter's rule-span gate (`in_rule_span_argument`, in the
/// `badness` crate) recognizes the same shape; single-sourced so the two never
/// drift.
pub fn is_paren_trim_word(token: &SyntaxToken) -> bool {
    if token.kind() != SyntaxKind::WORD {
        return false;
    }
    let t = token.text();
    t.len() >= 3
        && t.starts_with('(')
        && t.ends_with(')')
        && t[1..t.len() - 1].chars().all(|c| c == 'l' || c == 'r')
}

/// Whether `node` (a `COMMAND`) is a horizontal-rule command per the signature DB.
fn is_rule_command(node: &SyntaxNode, cx: LowerCtx<'_>) -> bool {
    cx.signatures.command_at(node).is_some_and(|sig| sig.rule)
}

/// Whether the accumulated cell holds only collapsible trivia (no real content) —
/// i.e. the parser is at a grid boundary.
fn cell_is_blank(cell: &[SyntaxElement]) -> bool {
    cell.iter().all(|e| {
        e.as_token()
            .is_some_and(|t| is_collapsible_trivia(t.kind()))
    })
}

/// Whether the cell's own top-level trivia contains a blank line (two newlines
/// with only inline whitespace between them, the `\par` boundary). A blank line
/// inside a *child* node (a nested environment's body) is that node's own
/// business and is not seen here.
fn cell_has_blank_line(cell: &[SyntaxElement]) -> bool {
    let mut run_newlines = 0;
    for e in cell {
        match e.as_token().map(SyntaxToken::kind) {
            Some(SyntaxKind::NEWLINE) => {
                run_newlines += 1;
                if run_newlines >= 2 {
                    return true;
                }
            }
            Some(SyntaxKind::WHITESPACE) => {}
            _ => run_newlines = 0,
        }
    }
    false
}

/// Whether the boundary trivia accumulated since the last grid token includes a
/// newline — i.e. a following comment sits on its *own* physical line rather than
/// trailing the previous row's `\\`.
fn cell_has_newline(cell: &[SyntaxElement]) -> bool {
    cell.iter().any(|e| {
        e.as_token()
            .is_some_and(|t| t.kind() == SyntaxKind::NEWLINE)
    })
}

/// Whether everything from `from` onward is collapsible trivia or `%` comments
/// that each start their own physical line — nothing more of the *row* remains,
/// so a comment at the current position is a clean trailing comment, and any
/// following comment-only lines render as passthrough lines. A comment that does
/// not sit on its own line, or any non-trivia element, disqualifies the rest
/// (joining it onto the row's line would comment it out).
fn rest_is_trivia_and_comment_lines(inline: &[SyntaxElement], from: usize) -> bool {
    let mut own_line = false;
    for element in &inline[from..] {
        let Some(token) = element.as_token() else {
            return false;
        };
        match token.kind() {
            SyntaxKind::NEWLINE => own_line = true,
            SyntaxKind::WHITESPACE => {}
            SyntaxKind::COMMENT if own_line => own_line = false,
            _ => return false,
        }
    }
    true
}

/// Flatten an alignment environment's body into a single stream of inline
/// elements, descending one level into the lone body wrapper node (where the `&`
/// and `\\` separators live) — a `PARAGRAPH` for a prose grid (`tabular`), or a
/// `MATH` node for a `math` grid (`align`/matrix, parsed in math mode). Trivia
/// outside the wrapper is dropped (it is just the body's own leading/trailing break,
/// which the indenter re-supplies).
///
/// Returns `None` when the body holds more than one wrapper node — a blank-line
/// break, which the single grid does not model — so the caller falls back.
///
/// A rule command that the greedy parser saddled with the next line's first cell
/// as a bogus `{…}` argument ([`rule_overattaches_cell`]) is expanded into its own
/// children, so the rule lands on its own passthrough line and the `{…}` is handed
/// back to the grid as cell content.
fn flatten_alignment_body(
    body_elements: &[SyntaxElement],
    cx: LowerCtx<'_>,
    math: bool,
) -> Option<Vec<SyntaxElement>> {
    let wrapper = if math {
        SyntaxKind::MATH
    } else {
        SyntaxKind::PARAGRAPH
    };
    let mut inline: Vec<SyntaxElement> = Vec::new();
    let mut paragraphs = 0;
    for element in strip_virtual_dtx_framing(body_elements.iter().cloned(), cx) {
        match element {
            SyntaxElement::Node(child) if child.kind() == wrapper => {
                paragraphs += 1;
                if paragraphs > 1 {
                    return None;
                }
                extend_alignment_elements(&mut inline, child.children_with_tokens(), cx);
            }
            SyntaxElement::Token(token) if is_collapsible_trivia(token.kind()) => {}
            other => push_alignment_element(&mut inline, other, cx),
        }
    }
    Some(inline)
}

/// Extend a flattened grid stream through the virtual-document framing adapter.
/// The adapter is applied at every level the grid flattener descends, so a source
/// margin and its padding cannot become cell content merely because the parser
/// attached that physical line inside a wrapper or an over-attaching rule node.
fn extend_alignment_elements(
    inline: &mut Vec<SyntaxElement>,
    elements: impl IntoIterator<Item = SyntaxElement>,
    cx: LowerCtx<'_>,
) {
    for element in strip_virtual_dtx_framing(elements, cx) {
        push_alignment_element(inline, element, cx);
    }
}

/// Push one flattened-body element, expanding an over-attaching rule command
/// ([`rule_overattaches_cell`]) into its children so its trailing `{…}` cell is
/// exposed to the grid rather than glued to the rule.
fn push_alignment_element(
    inline: &mut Vec<SyntaxElement>,
    element: SyntaxElement,
    cx: LowerCtx<'_>,
) {
    if let SyntaxElement::Node(node) = &element
        && rule_overattaches_cell(node, cx)
    {
        extend_alignment_elements(inline, node.children_with_tokens(), cx);
    } else {
        inline.push(element);
    }
}

/// Whether `node` is a horizontal-rule `COMMAND` (`\midrule`, `\toprule`, …) onto
/// which the greedy parser attached the next line's first
/// cell as a spurious `{…}` argument. Booktabs rules take at most an optional
/// `[width]`, never a mandatory brace argument, so a leading `{…}` is never a real
/// argument — it is cell content the arity refinement peels back off.
///
/// Restricted to a *leading* `{…}` (no real argument consumed first): the rare
/// `\toprule[2pt]{…}` shape keeps the generic fallback rather than being split.
fn rule_overattaches_cell(node: &SyntaxNode, cx: LowerCtx<'_>) -> bool {
    if node.kind() != SyntaxKind::COMMAND || !is_rule_command(node, cx) {
        return false;
    }
    let Some(first_arg) = node
        .children()
        .find(|child| matches!(child.kind(), SyntaxKind::GROUP | SyntaxKind::OPTIONAL))
    else {
        return false;
    };
    if first_arg.kind() != SyntaxKind::GROUP {
        return false;
    }
    // A leading `{…}` is a real argument only when the signature's first slot is a
    // mandatory brace argument (`\cline{2-3}`, `\specialrule{…}`).
    let first_slot_is_brace = cx
        .signatures
        .command_at(node)
        .and_then(|sig| sig.args.first())
        .is_some_and(|arg| arg.kind == ArgKind::Brace);
    !first_slot_is_brace
}

/// The per-column alignments declared by a `tabular`/`array` environment's column
/// specification (`\begin{tabular}{lcr}` → `[Left, Center, Right]`), or `None` to
/// signal the caller to fall back to all-left.
///
/// Only environments the signature DB marks `align` carry a user column spec; a math
/// grid's `\begin` has no argument `GROUP` at all (the environment name is a separate
/// `NAME_GROUP`), so those return `None` and fall through. The spec is the *last*
/// `{…}` `GROUP` on the `\begin` — uniform across `tabular`'s `{spec}`, `array`'s
/// `{spec}`, and `tabular*`'s `{width}[pos]{spec}` (the `[pos]` is an `OPTIONAL`, not
/// a `GROUP`). The raw inner source is read via [`Group::inner_source`] rather than
/// `nth_group_text`, which would bail on the nested `{…}` of a `p{3cm}` column.
fn column_alignments(env: &SyntaxNode, cx: LowerCtx<'_>) -> Option<Vec<ColAlign>> {
    let begin = Environment::cast(env.clone())?.begin()?;
    if !cx
        .signatures
        .environment_at(begin.syntax())
        .is_some_and(|sig| sig.align)
    {
        return None;
    }
    let spec = crate::ast::children::<Group>(begin.syntax()).last()?;
    colspec::parse_column_spec(&spec.inner_source())
}

/// Render the grid to IR: align each cell within its column to the column's declared
/// alignment (`aligns`, empty = all-left), join cells with `" & "`, append the row's
/// `\\` and any trailing comment, and join all items with [`Ir::hard_line`]. A row is
/// one [`Ir::text`] (no newline; cells are flat), which the caller indents one step.
/// Rows terminated by `\\` pad to the full grid width, aligning their terminators;
/// unterminated rows omit that final padding so they never carry trailing whitespace.
/// [`GridItem::Passthrough`] lines (comments, `\hline`/`\midrule`, …) are emitted
/// verbatim between rows and never counted toward column widths.
///
/// A `\multicolumn{n}{…}{…}` cell spans `n` columns: it never defines a single
/// column's width, and its rendered field is the sum of the spanned column widths
/// plus the `" & "` separators it absorbs. The spanned columns keep their
/// data-derived widths (the `\multicolumn`'s *source* markup is usually wider than a
/// few narrow columns; growing them to fit would balloon the data rows), so when the
/// markup exceeds its span it simply overflows that one row — matching how such a row
/// is written by hand. When the span is instead *wider* than the markup, the cell is
/// aligned within it per the `\multicolumn`'s own `{spec}`.
fn render_alignment_rows(items: &[GridItem], aligns: &[ColAlign]) -> Ir {
    const SEP: &str = " & ";

    // Column width = the max char-count over every *span-1* cell in that column
    // (including last cells, so a long final cell still widens the column above it).
    // Char count matches the printer's own column metric. Spanning cells and
    // passthrough lines do not participate here.
    let mut col_widths: Vec<usize> = Vec::new();
    let mut widen = |c: usize, width: usize| {
        while c >= col_widths.len() {
            col_widths.push(0);
        }
        if width > col_widths[c] {
            col_widths[c] = width;
        }
    };
    for item in items {
        let GridItem::Row(row) = item else { continue };
        // A standalone caption describes the table rather than its first column.
        if row.cells.len() == 1 && row.cells[0].caption {
            continue;
        }
        let mut c = 0;
        for cell in &row.cells {
            if cell.span == 1 && cell.block.is_none() {
                widen(c, cell.text.chars().count());
            }
            c += cell.span;
        }
    }

    // The combined field width of the `span` columns starting at `c`: their widths
    // plus the `" & "` separators the spanning cell absorbs.
    let field_width = |c: usize, span: usize| -> usize {
        let sum: usize = (c..c + span)
            .map(|i| col_widths.get(i).copied().unwrap_or(0))
            .sum();
        sum + SEP.len() * (span - 1)
    };
    let grid_width =
        col_widths.iter().sum::<usize>() + SEP.len() * col_widths.len().saturating_sub(1);
    let grid_width = grid_width.saturating_sub(usize::from(
        col_widths.len() > 1 && col_widths.first() == Some(&0),
    ));

    let lines = items.iter().map(|item| {
        let row = match item {
            GridItem::Intertext(ir) => return ir.clone(),
            GridItem::Passthrough(text) => {
                // A passthrough spans physical lines when a comment-only line
                // binds into a rule command as its `DOC_COMMENT` (issue #49):
                // flat-printing keeps the doc comment's break as a raw newline,
                // which a single `Ir::text` would emit without re-applying the
                // grid indent. Split so every physical line is indented.
                return Ir::join(
                    Ir::hard_line(),
                    text.lines().map(|line| Ir::text(line.trim().to_string())),
                );
            }
            GridItem::Row(row) => row,
        };
        let caption = row.cells.len() == 1 && row.cells[0].caption;
        let mut line = String::new();
        // A block cell (always the row's last, see [`Cell::block`]): its later
        // lines hang at the nested environment's start column, so its IR goes
        // inside an [`Ir::align`] whose width is the flat prefix already on the
        // line plus the cell's own hang offset. It takes no padding and no
        // width — like a spanning cell, it overflows.
        let mut block: Option<Ir> = None;
        let last = row.cells.len().saturating_sub(1);
        let mut c = 0;
        for (idx, cell) in row.cells.iter().enumerate() {
            if idx > 0 {
                // A row never opens with the separator's leading space: when
                // everything before this `&` is empty (an `aligned`/`split` body
                // whose rows all start at `&`, so the leading column's width is
                // 0), there is nothing to separate and the `&` is the line's
                // first character. A *padded* empty cell (its column is nonzero
                // elsewhere) has already pushed its pad, keeping the `&` aligned.
                line.push_str(if line.is_empty() { "& " } else { SEP });
            }
            if let Some(block_cell) = &cell.block {
                block = Some(Ir::align(
                    line.chars().count() + block_cell.hang,
                    block_cell.ir.clone(),
                ));
                break;
            }
            let field = if caption {
                0
            } else {
                field_width(c, cell.span)
            };
            let text_width = cell.text.chars().count();
            let pad = field.saturating_sub(text_width);
            // A `\multicolumn`'s own `{spec}` overrides the column alignment.
            let align = cell
                .align
                .unwrap_or_else(|| aligns.get(c).copied().unwrap_or(ColAlign::Left));
            let (leading, trailing) = match align {
                ColAlign::Left => (0, pad),
                ColAlign::Right => (pad, 0),
                ColAlign::Center => (pad / 2, pad - pad / 2),
            };
            // The last cell never carries trailing whitespace (leading pad is fine).
            let trailing = if idx == last { 0 } else { trailing };
            line.push_str(&" ".repeat(leading));
            line.push_str(&cell.text);
            line.push_str(&" ".repeat(trailing));
            c += cell.span;
        }
        let mut tail = String::new();
        if let Some(line_break) = &row.line_break {
            if block.is_none() && !caption {
                line.push_str(&" ".repeat(grid_width.saturating_sub(line.chars().count())));
            }
            tail.push(' ');
            tail.push_str(line_break);
        }
        // The trailing comment always follows the `\\` so the break is never
        // commented out.
        if let Some(comment) = &row.trailing_comment {
            tail.push(' ');
            tail.push_str(comment);
        }
        match block {
            Some(block) => Ir::concat([Ir::text(line), block, Ir::text(tail)]),
            None => {
                line.push_str(&tail);
                Ir::text(line)
            }
        }
    });
    Ir::join(Ir::hard_line(), lines)
}

/// Lower a delimited group — a brace group `{…}` (`open`/`close` =
/// `L_BRACE`/`R_BRACE`) or an optional-argument group `[…]`
/// (`L_BRACKET`/`R_BRACKET`) — indenting its body one step, exactly like
/// [`lower_environment`] but with token delimiters instead of `BEGIN`/`END`
/// nodes. Under the Tier-2 wrap modes it is called for multi-line groups only
/// (see [`spans_multiple_lines`]); under [`WrapMode::Reflow`] it is the block
/// form a group or optional falls back to when the width-driven paths
/// ([`lower_opaque_group`], [`lower_optional`]) decline — a blank line, a
/// comment, nested block content — where the node may also be single-line
/// (`\baz[{c% x\nd}]`).
///
/// A blank line at an optional body's edge remains a blank line. Unlike opaque
/// brace groups, long optionals can now reach this path structurally, and
/// erasing the edge break would both discard their `\par` and make the next pass
/// select the inline layout.
///
/// Inside a group the parser emits body tokens directly (no `PARAGRAPH`
/// wrapping), so the only `open` token is the first child and the only `close`
/// token is the last — but an `OPTIONAL` body may contain a stray `[` (TeX does
/// not nest `[`), so the opener is captured only once (`open_ir` still `Nil`).
fn lower_bracketed(
    node: &SyntaxNode,
    open: SyntaxKind,
    close: SyntaxKind,
    cx: LowerCtx<'_>,
    keyval: bool,
) -> Ir {
    let mut open_ir = Ir::Nil;
    let mut close_ir = Ir::Nil;
    let mut body_elements: Vec<SyntaxElement> = Vec::new();
    for element in strip_virtual_dtx_framing(node.children_with_tokens(), cx) {
        match &element {
            SyntaxElement::Token(t) if t.kind() == open && matches!(open_ir, Ir::Nil) => {
                open_ir = Ir::verbatim(t.text());
            }
            SyntaxElement::Token(t) if t.kind() == close => {
                close_ir = Ir::verbatim(t.text());
            }
            _ => body_elements.push(element),
        }
    }
    let preserve_blank_edges = open == SyntaxKind::L_BRACKET;
    let leading_blank = preserve_blank_edges
        && body_elements
            .iter()
            .take_while(|element| is_collapsible_trivia_element(element))
            .filter(|element| element.kind() == SyntaxKind::NEWLINE)
            .count()
            >= 2;
    let trailing_blank = preserve_blank_edges
        && body_elements
            .iter()
            .rev()
            .take_while(|element| is_collapsible_trivia_element(element))
            .filter(|element| element.kind() == SyntaxKind::NEWLINE)
            .count()
            >= 2;

    // A comment glued to the open delimiter (`{%`, with no newline between them)
    // must ride on the open-delimiter line. Pushing it to its own indented line
    // would turn the newline the formatter inserts after `{` into real whitespace
    // inside the group, changing `\cmd{%\n}` (an empty group — the `%` eats the
    // source newline) into `\cmd{ }` (a group holding a space). The parser emits
    // leading whitespace/newlines as their own trivia tokens, so the first body
    // element is the comment iff it was glued to the opener.
    let has_leading_comment = body_elements
        .first()
        .and_then(SyntaxElement::as_token)
        .is_some_and(|t| t.kind() == SyntaxKind::COMMENT);
    let open_ir = if has_leading_comment {
        let comment = body_elements.remove(0);
        Ir::concat([open_ir, Ir::verbatim(comment.as_token().unwrap().text())])
    } else {
        open_ir
    };

    // Whether the source glued the open delimiter directly to the first body
    // token, with no whitespace between them (`{\let…`). In normal catcodes an
    // end-of-line after a non-control-word token reads as a *space* (TeX state M
    // holds right after a `{`), so a break the formatter synthesizes here would
    // inject a space token the author never wrote — a silent meaning change
    // wherever space tokens matter (horizontal mode). We therefore only break
    // after the opener when the source already had whitespace there, where
    // whitespace ↔ newline is TeX-identical. The parser emits leading
    // whitespace/newlines as their own trivia tokens, so a whitespace boundary
    // shows up as a leading `WHITESPACE`/`NEWLINE` element; anything else (real
    // content, a nested node) means the opener was glued. This path is never
    // reached inside an expl3 region (routed to `lower_expl_group` earlier),
    // where source whitespace is catcode-9 and the synthesized break is sound.
    // Only a proven keyval body lifts the guard: its processor strips spaces
    // around entries. Textual optionals observe edge spaces just like braces.
    let open_glued = !keyval
        && body_elements
            .first()
            .and_then(SyntaxElement::as_token)
            .is_none_or(|t| !is_collapsible_trivia(t.kind()));

    // Mirror the opener rule at the other edge. If the final body element was
    // glued to a meaningful closer, inserting a line break before that closer
    // would add a space token to the group. This is observable in ordinary text
    // and in a `\def` replacement body. Proven keyval processors are exempt: the
    // signature guarantees that surrounding entry whitespace is insignificant.
    let close_glued = !keyval
        && body_elements.last().is_some_and(|element| {
            element
                .as_token()
                .is_none_or(|token| !is_collapsible_trivia(token.kind()))
        });

    // A brace-group body under reflow is laid out as code-like statements: each
    // source line stays its own logical line, but an over-long one wraps to the
    // width instead of forcing the printer to break the innermost nested prose
    // group (the only soft break a rigid `lower_element_stream` body would expose).
    // Optional `[…]` bodies and the non-reflow modes keep the generic stream.
    let body =
        if matches!(cx.wrap, WrapMode::Reflow | WrapMode::Stable) && open == SyntaxKind::L_BRACE {
            reflow_elements(body_elements.into_iter(), cx, ReflowKind::Statement)
        } else {
            Ir::concat(lower_element_stream(body_elements.into_iter(), cx))
        };
    let body = trim_trailing_break(trim_leading_break(body));

    if matches!(body, Ir::Nil) {
        if leading_blank || trailing_blank {
            // One normalized blank line preserves an otherwise empty long
            // optional's `\par` and keeps this path stable on reparse.
            Ir::concat([open_ir, Ir::empty_line(), close_ir])
        } else if has_leading_comment {
            // `{%\n}`: the comment already rode the open delimiter, so the close
            // must still drop to its own line — collapsing to `{%}` would comment
            // out the closing brace.
            Ir::concat([open_ir, Ir::hard_line(), close_ir])
        } else {
            // Empty multi-line body collapses to the bare delimiters, e.g. `{\n}` → `{}`.
            Ir::concat([open_ir, close_ir])
        }
    } else {
        // A glued opener keeps the first body line on the opener's line; the
        // `Ir::indent` still indents the body's *interior* breaks one step, so
        // only the first line rides the opener (`{\aaa` / `␣␣\bbb`).
        let lead = if leading_blank {
            Ir::empty_line()
        } else if open_glued {
            Ir::Nil
        } else {
            Ir::hard_line()
        };
        let trail = if trailing_blank {
            Ir::empty_line()
        } else if close_glued {
            Ir::Nil
        } else {
            Ir::hard_line()
        };
        Ir::concat([
            open_ir,
            Ir::indent(Ir::concat([lead, body])),
            trail,
            close_ir,
        ])
    }
}

/// Whether an opaque brace group directly contains an environment glued to
/// sibling content.
///
/// With no signature proving the group's whitespace semantics, the glued seam
/// says that partially expanding the environment is unsafe: it would insert a
/// space where the source had none and leave the rest of the argument half
/// formatted. Preserve the whole group instead. A comment or any authored gap
/// already supplies a safe boundary and therefore does not trigger this gate.
/// The predicate reads only CST adjacency, so the verbatim result is its own
/// fixed point.
fn opaque_group_has_glued_environment_sibling(node: &SyntaxNode) -> bool {
    let elements: Vec<SyntaxElement> = node.children_with_tokens().collect();
    elements.iter().enumerate().any(|(index, element)| {
        let SyntaxElement::Node(child) = element else {
            return false;
        };
        if child.kind() != SyntaxKind::ENVIRONMENT {
            return false;
        }

        let left = index
            .checked_sub(1)
            .and_then(|previous| elements.get(previous));
        glued_group_content(left, SyntaxKind::L_BRACE)
            || glued_group_content(elements.get(index + 1), SyntaxKind::R_BRACE)
    })
}

fn glued_group_content(element: Option<&SyntaxElement>, delimiter: SyntaxKind) -> bool {
    match element {
        Some(SyntaxElement::Node(_)) => true,
        Some(SyntaxElement::Token(token)) => token.kind() != delimiter && !is_trivia(token.kind()),
        None => false,
    }
}

/// Lower a brace [`SyntaxKind::GROUP`] under [`WrapMode::Reflow`]: a
/// width-driven fill over its body, so block-vs-inline is decided by width,
/// content, and preserved predicates — never by whether the author happened to
/// break the line ([`spans_multiple_lines`], the unsafe lone-newline
/// predicate; see the trivia-invariant-layout section of `formatter.md`).
///
/// The flat rendering is byte-identical to the generic inline path except that
/// a lone-newline run renders as one space — the newline ↔ space exchange that
/// is TeX-identical. Break opportunities are exactly the perturbation-eligible
/// gaps ([`crate::formatter::perturb`]): a lone-newline run and a single-space
/// gap, both of which render `" "` flat, so `fmt(perturbed) == fmt(original)`
/// holds by construction. Any other gap spelling (`a␣␣b`, a tab) glues
/// verbatim into its atom, and a glued junction never gains a break — breaking
/// where the author glued would inject a space token TeX typesets (the same
/// rationale as [`lower_bracketed`]'s `open_glued`). Edge padding rides the
/// flat rendering and vanishes broken, where the delimiter's own newline
/// supplies the space token; an empty body keeps its padding flat (`{ }` and
/// `{\n}` both render `{ }` — deleting it would delete a space token).
/// One Tier-2 boundary is preserved rather than width-driven: an authored newline
/// immediately after a [`SyntaxKind::LINE_BREAK`] remains a hard source line in a
/// structurally plain, command-only text group. A same-line space remains available
/// to the fill, and inline, command/parameter-like, or glued successors remain
/// untouched; the narrow shape avoids treating `\\` in opaque macro code as a
/// semantic row break.
///
/// A body the fill cannot own takes today's indented block form
/// ([`lower_bracketed`]) instead, keyed on preserved predicates and content
/// only: an *interior* blank line, a direct `%` comment (which must end its
/// line; the glued `{%` case included), a token embedding a newline (a
/// multi-line brace `\verb`), or a child whose IR carries a forced break —
/// nested block content, itself decided by preserved predicates and content
/// under this policy, so the read stays Tier-1-clean. A blank run at the
/// body's *edge* does not decline: the block form trims it away
/// ([`trim_leading_break`]/[`trim_trailing_break`]), so edge-blank presence is
/// not a predicate the block form preserves — it erases to padding here,
/// matching the deletion the block form already performed.
///
/// Known residuals, argued safe by catcode rather than by oracle: a
/// multi-space run stays authored (`{a  b}` and `{a  \nb}` differ, but neither
/// is an eligible perturbation and TeX collapses a catcode-10 run to one space
/// token); a lone newline beside a `\verb` is erased though the oracle
/// excludes VERB-adjacent gaps (a complete `VERB` token carries its
/// delimiters); and an `\obeylines` body joins — unresolvable macro meaning is
/// out of scope, as it is for paragraph reflow generally.
fn lower_opaque_group(node: &SyntaxNode, cx: LowerCtx<'_>) -> Ir {
    /// Resolve the gap read but not yet committed: a `" "` flat gap is a break
    /// opportunity (the fill's own separator renders it), anything else glues
    /// verbatim into the atom in progress.
    fn commit_gap(atoms: &mut Vec<Ir>, atom: &mut Vec<Ir>, pending: &mut Option<(String, bool)>) {
        if let Some((gap, _)) = pending.take() {
            if gap == " " {
                if !atom.is_empty() {
                    atoms.push(Ir::concat(std::mem::take(atom)));
                }
            } else {
                atom.push(Ir::verbatim(gap));
            }
        }
    }
    let block = || lower_bracketed(node, SyntaxKind::L_BRACE, SyntaxKind::R_BRACE, cx, false);
    if plain_opaque_block_has_authored_rows(node, cx) {
        return block();
    }
    let mut open = Ir::Nil;
    let mut close = Ir::Nil;
    let mut lead: Option<String> = None;
    let mut atoms: Vec<Ir> = Vec::new();
    let mut atom: Vec<Ir> = Vec::new();
    // The flat spelling of the gap read but not yet committed, and whether it
    // was a blank-line run. Only an *interior* blank line declines — the block
    // form trims a blank at the body's edge away (`trim_leading_break` /
    // `trim_trailing_break`), so declining on one would key on a predicate the
    // emitter then destroys: pass 2 would see no blank and flatten (the
    // latexindent `poly-switch-blank-line` family). An edge blank erases to
    // padding, exactly the deletion the block form already performed.
    let mut pending: Option<(String, bool)> = None;
    let mut iter = alignment_cell_elements(node.children_with_tokens(), cx)
        .into_iter()
        .peekable();
    while let Some(element) = iter.next() {
        match element {
            SyntaxElement::Token(t)
                if t.kind() == SyntaxKind::L_BRACE && matches!(open, Ir::Nil) =>
            {
                open = Ir::verbatim(t.text());
            }
            SyntaxElement::Token(t) if t.kind() == SyntaxKind::R_BRACE => {
                close = Ir::verbatim(t.text());
            }
            SyntaxElement::Token(t)
                if cx.in_dtx_doc_region && t.kind() == SyntaxKind::DOC_MARGIN =>
            {
                // The virtual region owns both the physical margin and its source
                // padding. Retaining the padding makes a wrapped group add its
                // indentation again on every pass.
                while matches!(
                    iter.peek(),
                    Some(SyntaxElement::Token(next)) if next.kind() == SyntaxKind::WHITESPACE
                ) {
                    iter.next();
                }
            }
            SyntaxElement::Token(t) if is_collapsible_trivia(t.kind()) => {
                let gap = consume_gap(&t, &mut iter);
                let blank = gap == Gap::Blank;
                let flat = gap.flat().to_string();
                if atoms.is_empty() && atom.is_empty() {
                    lead = Some(flat);
                } else {
                    pending = Some((flat, blank));
                }
            }
            SyntaxElement::Token(t) if t.kind() == SyntaxKind::COMMENT => return block(),
            SyntaxElement::Token(t) if t.text().contains('\n') => return block(),
            SyntaxElement::Token(t) => {
                if matches!(pending, Some((_, true))) {
                    return block(); // an interior blank line: preserved predicate
                }
                commit_gap(&mut atoms, &mut atom, &mut pending);
                atom.push(lower_loose_token(&t, cx));
            }
            SyntaxElement::Node(child) => {
                let ir = lower_node(&child, cx);
                if ir.contains_forced_break() {
                    return block(); // nested block content
                }
                if matches!(pending, Some((_, true))) {
                    return block(); // an interior blank line: preserved predicate
                }
                commit_gap(&mut atoms, &mut atom, &mut pending);
                atom.push(ir);
            }
        }
    }
    let trail: Option<String> = pending.take().map(|(gap, _)| gap);
    if !atom.is_empty() {
        atoms.push(Ir::concat(atom));
    }
    if atoms.is_empty() {
        // `{}` / `{ }` / `{\n}`: nothing to lay out; the padding survives flat.
        let lead = lead.map(Ir::verbatim).unwrap_or(Ir::Nil);
        return Ir::concat([open, lead, close]);
    }
    // An edge gap joins the vanish-when-broken protocol only when its flat
    // spelling is `" "` — the one spelling a break reproduces (the broken
    // form's newline re-reads as a lone-newline gap, whose flat is `" "`), and
    // the same criterion the interior break opportunities use. Any other
    // spelling (`{0    }`) must ride verbatim and never break: vanishing it
    // would hand pass 2 a `" "` gap where pass 1 measured four spaces, and the
    // layout oscillates (pgf's `\pgfpoint@oncoil{0    }` coil tables).
    let mut parts: Vec<Ir> = vec![open];
    let mut inner: Vec<Ir> = Vec::new();
    match lead.as_deref() {
        None => {}
        Some(" ") => {
            inner.push(Ir::soft_line());
            inner.push(Ir::if_break(Ir::verbatim(" "), Ir::Nil));
        }
        Some(other) => parts.push(Ir::verbatim(other)),
    }
    inner.push(Ir::fill(atoms));
    parts.push(Ir::indent(Ir::concat(inner)));
    match trail.as_deref() {
        None => {}
        Some(" ") => {
            parts.push(Ir::if_break(Ir::verbatim(" "), Ir::Nil));
            parts.push(Ir::soft_line());
        }
        Some(other) => parts.push(Ir::verbatim(other)),
    }
    parts.push(close);
    Ir::group(Ir::concat(parts))
}

/// Overlay bodies often arrange slide content rather than running prose. Keep
/// their authored breaks even under reflow, while normalizing indentation and
/// formatting nested constructs with the ordinary preservation policy.
///
/// This is Tier 2: each direct gap reproduces its newline count (normalizing
/// blank runs), and inline gaps stay inline regardless of width. The closing
/// gap lives outside the body's indent, so the next pass reads the same gaps
/// and reproduces the same framing, including empty and comment-only bodies.
fn lower_beamer_body(node: &SyntaxNode, cx: LowerCtx<'_>) -> Ir {
    let cx = LowerCtx {
        wrap: WrapMode::Preserve,
        ..cx
    };
    let mut elements: Vec<_> = node.children_with_tokens().collect();
    let close = elements.pop().expect("a parsed group has a closer");
    let open = elements.remove(0);
    let trailing = elements
        .iter()
        .rposition(|element| !is_collapsible_trivia_element(element))
        .map_or(0, |index| index + 1);
    let closing_gap = elements.split_off(trailing);
    Ir::concat([
        Ir::verbatim(open.to_string()),
        Ir::indent(Ir::concat(lower_element_stream(elements.into_iter(), cx))),
        Ir::concat(lower_element_stream(closing_gap.into_iter(), cx)),
        Ir::verbatim(close.to_string()),
    ])
}

/// Whether an opaque group is the plain text argument of a command that alone
/// occupies its paragraph, with every `\\` followed by an authored newline and
/// flanked by words. This is the conservative shape in which source rows are
/// useful structure rather than plausible macro parameter text.
fn plain_opaque_block_has_authored_rows(group: &SyntaxNode, cx: LowerCtx<'_>) -> bool {
    if cx.in_dtx_doc_region {
        return false;
    }
    let Some(command) = group
        .parent()
        .filter(|node| node.kind() == SyntaxKind::COMMAND)
    else {
        return false;
    };
    let Some(paragraph) = command
        .parent()
        .filter(|node| node.kind() == SyntaxKind::PARAGRAPH)
    else {
        return false;
    };
    if is_dtx_doc_paragraph(&paragraph) {
        return false;
    }
    let mut content = paragraph
        .children_with_tokens()
        .filter(|element| !is_collapsible_trivia_element(element));
    if !content
        .next()
        .is_some_and(|element| element.as_node() == Some(&command))
        || content.next().is_some()
    {
        return false;
    }
    let elements: Vec<SyntaxElement> = group.children_with_tokens().collect();
    if !elements.iter().all(|element| match element {
        SyntaxElement::Token(token) => matches!(
            token.kind(),
            SyntaxKind::L_BRACE
                | SyntaxKind::R_BRACE
                | SyntaxKind::WORD
                | SyntaxKind::WHITESPACE
                | SyntaxKind::NEWLINE
        ),
        SyntaxElement::Node(node) => node.kind() == SyntaxKind::LINE_BREAK,
    }) {
        return false;
    }
    let mut saw_line_break = false;
    for (index, element) in elements.iter().enumerate() {
        if !matches!(element, SyntaxElement::Node(node) if node.kind() == SyntaxKind::LINE_BREAK) {
            continue;
        }
        saw_line_break = true;
        let left_is_word = elements[..index]
            .iter()
            .rev()
            .find(|element| !is_collapsible_trivia_element(element))
            .is_some_and(
                |element| matches!(element, SyntaxElement::Token(t) if t.kind() == SyntaxKind::WORD),
            );
        let mut newlines = 0usize;
        let right = elements[index + 1..].iter().find(|element| {
            if let SyntaxElement::Token(token) = element
                && is_collapsible_trivia(token.kind())
            {
                newlines += usize::from(token.kind() == SyntaxKind::NEWLINE);
                false
            } else {
                true
            }
        });
        let right_is_word = right.is_some_and(
            |element| matches!(element, SyntaxElement::Token(t) if t.kind() == SyntaxKind::WORD),
        );
        if !left_is_word || newlines != 1 || !right_is_word {
            return false;
        }
    }
    saw_line_break
}

/// Lower a [`SyntaxKind::OPTIONAL`] argument group, or `None` to leave it on the
/// generic inline path. The bracket entry point to [`lower_segmented_group`].
fn lower_optional(node: &SyntaxNode, cx: LowerCtx<'_>, keyval: bool) -> Option<Ir> {
    lower_segmented_group(
        node,
        SyntaxKind::L_BRACKET,
        SyntaxKind::R_BRACKET,
        cx,
        keyval,
    )
}

/// Lower a grid environment's column preamble as one all-or-nothing group.
///
/// The final declared brace argument of an environment marked `align` is the
/// column preamble. Whitespace between its top-level syntax elements separates
/// independently readable specifications (`l`, `S[…]`, `p{…}`), while brackets
/// and brace groups stay sealed inside their owning spec. If the flat preamble
/// overflows, every such boundary breaks; a partial fill is harder to scan than
/// either the compact or fully exploded form.
///
/// A comment, blank line, or nested forced break declines this layout and leaves
/// the generic group path in charge. The decision reads normalized [`Gap`]s, so a
/// source space and source newline converge on the same width-driven shape.
fn lower_column_spec_group(node: &SyntaxNode, cx: LowerCtx<'_>) -> Option<Ir> {
    if !cx.wraps_prose()
        || (!cx.in_dtx_doc_region
            && (contains_doc_margin(node, cx) || doc_margin_opens_line(node, cx)))
    {
        return None;
    }

    let mut open = Ir::Nil;
    let mut close = Ir::Nil;
    let mut entries: Vec<Ir> = Vec::new();
    let mut current: Vec<Ir> = Vec::new();
    let mut pending_gap: Option<Gap> = None;
    let mut bracket_depth = 0usize;
    let mut iter = strip_virtual_dtx_framing(node.children_with_tokens(), cx)
        .into_iter()
        .peekable();

    while let Some(element) = iter.next() {
        match element {
            SyntaxElement::Token(token)
                if token.kind() == SyntaxKind::L_BRACE && matches!(open, Ir::Nil) =>
            {
                open = Ir::verbatim(token.text());
            }
            SyntaxElement::Token(token) if token.kind() == SyntaxKind::R_BRACE => {
                close = Ir::verbatim(token.text());
            }
            SyntaxElement::Token(token) if token.kind() == SyntaxKind::L_BRACKET => {
                if let Some(gap) = pending_gap.take() {
                    current.push(Ir::verbatim(gap.flat()));
                }
                bracket_depth += 1;
                current.push(lower_loose_token(&token, cx));
            }
            SyntaxElement::Token(token) if token.kind() == SyntaxKind::R_BRACKET => {
                bracket_depth = bracket_depth.saturating_sub(1);
                current.push(lower_loose_token(&token, cx));
            }
            SyntaxElement::Token(token) if is_collapsible_trivia(token.kind()) => {
                let gap = consume_gap(&token, &mut iter);
                if gap == Gap::Blank {
                    return None;
                }
                if bracket_depth == 0 {
                    pending_gap = Some(gap);
                } else {
                    current.push(Ir::verbatim(gap.flat()));
                }
            }
            SyntaxElement::Token(token) if token.kind() == SyntaxKind::COMMENT => return None,
            SyntaxElement::Token(token) => {
                if let Some(gap) = pending_gap.take()
                    && !current.is_empty()
                {
                    entries.push(Ir::concat(std::mem::take(&mut current)));
                    entries.push(gap.separator());
                }
                current.push(lower_loose_token(&token, cx));
            }
            SyntaxElement::Node(child) => {
                let ir = lower_node(&child, cx);
                if ir.contains_forced_break() {
                    return None;
                }
                if let Some(gap) = pending_gap.take() {
                    current.push(Ir::verbatim(gap.flat()));
                }
                current.push(ir);
            }
        }
    }
    entries.push(Ir::concat(current));
    while entries.last().is_some_and(is_segment_separator) {
        entries.pop();
    }
    let splits = entries.iter().filter(|ir| is_segment_separator(ir)).count();
    if splits == 0 {
        return None;
    }

    Some(Ir::group(Ir::concat([
        open,
        Ir::indent(Ir::concat([Ir::soft_line(), Ir::concat(entries)])),
        Ir::soft_line(),
        close,
    ])))
}

/// Lower a delimited argument group as a comma-separated layout, or `None` to
/// leave it on the generic inline path.
///
/// A proven keyval body uses a Wadler group over its top-level comma-separated
/// entries: flat when it fits, one key per line otherwise. A textual optional
/// instead fills each line at existing comma-space boundaries, keeping glued
/// delimiters attached to their content. Its collapsed gaps retain spaces, so
/// `\foo[a=1,\nb=2]` formats as `\foo[a=1, b=2]`. A proven keyval canonicalizes
/// every flat separator without a space (`a=1,b=2`). Both layouts measure width
/// rather than authored line breaks, and include trailing same-line content
/// (`]{c}`) in their fit decisions.
///
/// `keyval` reports that the signature DB proved this argument a `key=value` list
/// (see [`ContentKind::Keyval`]), which additionally licenses splitting a comma the
/// author *glued*. Without it only gaps the author already wrote are break
/// opportunities, so a textual optional (`\item[red,green]`, a `\newcommand`
/// default) can never gain a space that would be typeset.
///
/// `{…}` reaches here only *through* that proof. A mandatory group is the ordinary
/// home of typeset text, so the generic opaque lowering owns it by default; the
/// keyval-family setters (`\pgfkeys`, `\tikzset`, `\lstset`, …) opt in through the
/// curated signature DB, and there segmenting at commas is the whole point — the
/// alternative is reflowing a key list as prose, which wraps mid-key.
///
/// A body that is not safely segmentable — a blank line, a `%` comment, nested
/// block content — takes the indented block form ([`lower_bracketed`])
/// unconditionally, so both spellings of the same content land on it (the
/// choice reads content and preserved predicates, never a lone newline). With
/// no split point at all the group collapses to one atom rather than
/// uselessly detonating into `[\n!htb\n]`. Inert under [`WrapMode::Preserve`]
/// and the other non-prose-wrapping modes, which keep the pre-existing block
/// layout.
fn lower_segmented_group(
    node: &SyntaxNode,
    open_kind: SyntaxKind,
    close_kind: SyntaxKind,
    cx: LowerCtx<'_>,
    keyval: bool,
) -> Option<Ir> {
    // A captured xparse `v` argument is same-line by construction. Breaking a
    // preceding optional makes the next parse lose the VERB capture and exposes
    // raw name bytes as ordinary LaTeX syntax.
    if node.next_sibling().is_some_and(|sibling| {
        sibling.kind() == SyntaxKind::GROUP
            && sibling
                .descendants_with_tokens()
                .filter_map(SyntaxElement::into_token)
                .any(|token| token.kind() == SyntaxKind::VERB)
    }) {
        return None;
    }
    // A `[…]` continuing across `.dtx` doc-margined lines keeps its authored
    // margins: `lower_node` already gates on this, but the signature-aware callers
    // reach here directly, and relaying such a body would move content off its `%`.
    //
    // The second gate is the one that bites: a bracket *on* a doc line
    // (`% \begin{function}[EXP, pTF]{…}`) holds no margin token of its own, so
    // `contains_doc_margin` says nothing, yet every line a break here creates would
    // land unmargined — silently promoting documentation to live code. The old
    // lowering was safe by accident (it only ever broke an already-multi-line
    // bracket); a width-driven group has to say so.
    if !cx.wraps_prose()
        || (!cx.in_dtx_doc_region
            && (contains_doc_margin(node, cx) || doc_margin_opens_line(node, cx)))
    {
        // Tier-2 residue: under a mode that does not wrap prose (or on a `.dtx`
        // doc line) the pre-existing behaviour is kept byte for byte — block
        // form when the author broke the line, generic inline path otherwise.
        // Fixed-point argument on [`spans_multiple_lines`].
        return spans_multiple_lines(node)
            .then(|| lower_bracketed(node, open_kind, close_kind, cx, keyval));
    }
    let Some(segments) = segment_delimited_body(node, open_kind, close_kind, cx, keyval) else {
        // Not safely segmentable: blank line, comment, or a child carrying a
        // forced break. The first two put a NEWLINE directly in the node, but
        // the third occurs in single-line spellings too (`\baz[{c\n\nd}]`), so
        // the block form applies unconditionally — both spellings of the same
        // content take it, keyed on content and preserved predicates alone.
        return Some(lower_bracketed(node, open_kind, close_kind, cx, keyval));
    };
    let GroupSegments {
        open,
        mut parts,
        close,
        splits,
    } = segments;
    // Keyval processors discard entry padding. Textual optionals must retain it
    // even when there is no comma at which the body can break.
    let lead = peel_padding(&mut parts, Edge::Leading);
    let trail = peel_padding(&mut parts, Edge::Trailing);
    if splits == 0 {
        // Nothing to break at: emit the collapsed atom and let it overflow. A
        // breakable group here would push `[!htb]` onto three lines to no gain.
        return Some(if keyval {
            Ir::concat([open, Ir::concat(parts), close])
        } else {
            Ir::concat([open, lead, Ir::concat(parts), trail, close])
        });
    }
    if !keyval {
        // Only a single-space edge can become a newline and reconstruct the
        // same normalized gap. Glued and wider edges retain their exact spelling.
        let edge = |padding: Ir| match &padding {
            Ir::Verbatim { text, .. } if text.as_ref() == " " => Ir::line(),
            _ => padding,
        };
        let atoms = parts
            .split(is_segment_separator)
            .map(|entry| Ir::concat(entry.iter().cloned()));
        return Some(Ir::group(Ir::concat([
            open,
            Ir::indent(Ir::concat([edge(lead), Ir::fill(atoms)])),
            edge(trail),
            close,
        ])));
    }
    let body = Ir::concat(parts);
    Some(Ir::group(Ir::concat([
        open,
        Ir::indent(Ir::concat([Ir::soft_line(), body])),
        Ir::soft_line(),
        close,
    ])))
}

/// Which end of an `[…]` body [`peel_padding`] works on.
#[derive(Clone, Copy, PartialEq, Eq)]
enum Edge {
    Leading,
    Trailing,
}

/// Remove and return the whitespace padding at one end of a segmented `[…]` body
/// (see [`is_trimmable_break`]). A [`Ir::Nil`] result means there was none.
fn peel_padding(parts: &mut Vec<Ir>, edge: Edge) -> Ir {
    let mut padding = Ir::Nil;
    loop {
        let at = match edge {
            Edge::Leading => 0,
            Edge::Trailing => parts.len().saturating_sub(1),
        };
        match parts.get(at) {
            Some(ir) if is_trimmable_break(ir) => {
                let taken = parts.remove(at);
                if matches!(padding, Ir::Nil) {
                    padding = taken;
                }
            }
            _ => return padding,
        }
    }
}

/// A delimited body cut into its top-level entries: the delimiters, the entry IR
/// with [`Gap::separator`] split points already interleaved, and how many there are.
struct GroupSegments {
    open: Ir,
    parts: Vec<Ir>,
    close: Ir,
    splits: usize,
}

/// Segment an `OPTIONAL` or `GROUP` body at its top-level commas, or `None` when the
/// body is not safely segmentable — the same three bail conditions as
/// [`collapse_arg_group`]: a blank-line `\par`, a `%` comment (which must end its
/// line), or nested content carrying a forced break.
///
/// A comma is a split point only at bracket depth 0. The parser closes an
/// `OPTIONAL` at its first `]`, so a stray `[` inside the body (TeX does not nest
/// `[`) opens a region that never closes — everything after it stays glued, which
/// is the conservative reading: `\foo[a=[1,2]` must not break at the `1,2`. A `{…}`
/// body needs no matching rule for braces: the parser gives every nested brace group
/// its own `GROUP` node, so the only `L_BRACE`/`R_BRACE` *tokens* here are this
/// body's own delimiters, and a nested comma arrives already sealed inside a child.
fn segment_delimited_body(
    node: &SyntaxNode,
    open_kind: SyntaxKind,
    close_kind: SyntaxKind,
    cx: LowerCtx<'_>,
    normalize_commas: bool,
) -> Option<GroupSegments> {
    let mut open = Ir::Nil;
    let mut close = Ir::Nil;
    let mut parts: Vec<Ir> = Vec::new();
    let mut splits = 0usize;
    let mut depth = 0usize;
    // Whether the last content token was a `WORD` ending in `,` at depth 0, so the
    // *next* gap is a break opportunity.
    let mut open_entry = false;
    // Whether the entry currently being accumulated holds any content yet — what
    // tells [`push_entry_word`] a leading comma closes a real entry rather than an
    // empty one.
    let mut entry_open = false;
    let mut iter = strip_virtual_dtx_framing(node.children_with_tokens(), cx)
        .into_iter()
        .peekable();
    while let Some(element) = iter.next() {
        match element {
            SyntaxElement::Token(t) if t.kind() == open_kind && matches!(open, Ir::Nil) => {
                open = Ir::verbatim(t.text());
            }
            SyntaxElement::Token(t) if t.kind() == close_kind => {
                close = Ir::verbatim(t.text());
            }
            SyntaxElement::Token(t) if t.kind() == SyntaxKind::L_BRACKET => {
                depth += 1;
                open_entry = false;
                entry_open = true;
                parts.push(Ir::verbatim(t.text()));
            }
            SyntaxElement::Token(t) if is_collapsible_trivia(t.kind()) => {
                let gap = consume_gap(&t, &mut iter);
                if gap == Gap::Blank {
                    return None; // a blank-line `\par`: keep the block form
                }
                if open_entry && depth == 0 {
                    parts.push(if normalize_commas {
                        tight_line()
                    } else {
                        gap.separator()
                    });
                    splits += 1;
                    entry_open = false;
                } else {
                    // Not a split point: the gap rides at its flat spelling, which
                    // collapses a lone newline to a single space and keeps pure
                    // inline whitespace verbatim, matching the generic lowering.
                    parts.push(Ir::verbatim(gap.flat()));
                }
                open_entry = false;
            }
            SyntaxElement::Token(t) if t.kind() == SyntaxKind::COMMENT => return None,
            SyntaxElement::Token(t) if t.kind() == SyntaxKind::WORD && depth == 0 => {
                if normalize_commas && t.text().starts_with(',') {
                    let _ = peel_padding(&mut parts, Edge::Trailing);
                }
                splits += push_entry_word(t.text(), normalize_commas, &mut parts, entry_open);
                // A word ending in `,` closes its entry and opens an empty one.
                open_entry = t.text().ends_with(',');
                entry_open = !open_entry;
            }
            SyntaxElement::Token(t) => {
                parts.push(lower_loose_token(&t, cx));
                open_entry = false;
                entry_open = true;
            }
            SyntaxElement::Node(child) => {
                let ir = lower_node(&child, cx);
                if ir.contains_forced_break() {
                    return None; // nested block content: keep the block form
                }
                parts.push(ir);
                open_entry = false;
                entry_open = true;
            }
        }
    }
    // A trailing separator (`[a, b, ]`) would put the closing `]` two lines down.
    // Drop it — but an `Ir::Line` replaced authored whitespace above, and an
    // optional is textual, so that space token must survive as trailing padding
    // (`[a, ]` and `[a,\n]` both keep it); a normalized tight separator stood
    // for nothing and restores nothing.
    let mut dropped_gap = false;
    while parts.last().is_some_and(is_segment_separator) {
        dropped_gap |= matches!(parts.last(), Some(Ir::Line));
        parts.pop();
        splits = splits.saturating_sub(1);
    }
    if dropped_gap {
        parts.push(Ir::verbatim(" "));
    }
    Some(GroupSegments {
        open,
        parts,
        close,
        splits,
    })
}

/// Push one body `WORD` onto `parts`, cutting it at each *interior* comma when
/// `normalize_commas` licenses it, and return how many separators were emitted. The comma
/// stays on the piece it terminates (`xmin=-5,` / `xmax=5,`), since it belongs to
/// the key before it.
///
/// A comma whose entry holds nothing at all is an empty entry (a doubled `,`), not
/// a key: it never earns a line of its own and rides along on the next piece
/// instead. That emptiness is *not* a property of this word alone — the lexer ends
/// a `WORD` at every control sequence, so a key list routinely hands us a word that
/// opens with the comma closing the previous token's entry
/// (`width=` `\figurewidth` `,xmin=-5,…`). Hence `entry_open`: whether the caller
/// has already emitted content for the entry this word continues. Every proven
/// whitespace-insensitive separator uses [`tight_line`]: it is empty when flat and
/// an ordinary line break when broken. On the next pass that newline reaches the
/// matching gap arm above and normalizes to the same conditional separator, keeping
/// width decisions at a fixed point (issue #121). The caller's `Keyval` or
/// `TokenList` proof is what licenses removing otherwise-significant whitespace.
fn push_entry_word(
    text: &str,
    normalize_commas: bool,
    parts: &mut Vec<Ir>,
    entry_open: bool,
) -> usize {
    if !normalize_commas || !text.contains(',') {
        parts.push(Ir::verbatim(text));
        return 0;
    }
    let mut splits = 0usize;
    let mut start = 0usize;
    let mut pushed = 0usize;
    let mut has_content = entry_open;
    for (i, _) in text.match_indices(',') {
        let piece = &text[start..=i];
        if piece.len() == 1 && !has_content {
            continue; // an empty entry: let the comma ride on the next piece
        }
        if pushed > 0 {
            parts.push(tight_line());
            splits += 1;
        }
        parts.push(Ir::verbatim(piece));
        pushed += 1;
        start = i + 1;
        has_content = false;
    }
    if start < text.len() {
        if pushed > 0 {
            parts.push(tight_line());
            splits += 1;
        }
        parts.push(Ir::verbatim(&text[start..]));
    }
    splits
}

/// A comma-list separator that is glued in flat mode and breaks like an ordinary
/// [`Ir::Line`] in break mode. Its conservative fit width preserves the surrounding
/// layout's established break decisions while its rendering owns the no-space
/// keyval style.
fn tight_line() -> Ir {
    Ir::tight_line()
}

/// Whether `ir` is a separator emitted by [`segment_delimited_body`].
fn is_segment_separator(ir: &Ir) -> bool {
    matches!(ir, Ir::Line | Ir::TightLine | Ir::SoftLine)
}

/// Whether `command`'s signature marks any argument the [`lower_command`] path
/// must handle specially — a non-[`Opaque`](ContentKind::Opaque) content kind
/// (such as [`Prose`](ContentKind::Prose) or [`Identifier`](ContentKind::Identifier)).
/// The cheap guard that gates the
/// [`lower_command`] path in [`lower_node`]: a command with no such argument (the
/// overwhelming common case) lowers generically, so nothing regresses.
fn command_has_managed_arg(command: &SyntaxNode, cx: LowerCtx<'_>) -> bool {
    cx.signatures.command_at(command).is_some_and(|sig| {
        sig.args
            .iter()
            .any(|spec| spec.content != ContentKind::Opaque)
    })
}

fn command_has_math_arg(command: &SyntaxNode, cx: LowerCtx<'_>) -> bool {
    cx.signatures.command_at(command).is_some_and(|sig| {
        sig.args
            .iter()
            .any(|spec| spec.domain == ArgumentDomain::Math)
    })
}

/// Whether `command` is an *inline* prose command — one whose prose argument sits
/// in running text (`\footnote`, `\emph`, `\textbf`, …) rather than heading its own
/// line. Such a command is flattened into the surrounding reflow stream (see
/// [`flatten_inline_prose`]) so its body wraps as part of the paragraph and its
/// `{`/`}` glue to the adjacent words, instead of block-breaking the braces onto
/// their own lines ([`lower_prose_group`]).
///
/// Driven by the signature DB's explicit [`CommandSig::inline`] flag, not derived:
/// block-level prose commands that head their own line (`\section`, `\caption`)
/// leave it unset and keep the block treatment.
fn command_is_inline_prose(command: &SyntaxNode, cx: LowerCtx<'_>) -> bool {
    cx.signatures.command_at(command).is_some_and(|sig| {
        sig.inline
            && sig
                .args
                .iter()
                .any(|spec| spec.content == ContentKind::Prose)
    })
}

/// Whether `command` is an *inline* command that sits in running text (`\citep`,
/// `\ref`, `\emph`, …), per the signature DB's [`CommandSig::inline`] flag. Paragraph
/// reflow uses this so such a command flows into the fill as an atom even when the
/// author isolated it on its own source line, rather than being preserved as a
/// command-only line (see [`line_is_command_only`]). Broader than
/// [`command_is_inline_prose`], which additionally requires a prose argument.
fn command_is_inline(command: &SyntaxNode, cx: LowerCtx<'_>) -> bool {
    cx.signatures
        .command_at(command)
        .is_some_and(|sig| sig.inline)
}

/// Return the curated grammatical role of an inline citation whose key argument
/// is a token list. The content flag is the structural proof that distinguishes
/// citations from other inline commands such as `\ref` or `\emph`; a local
/// redefinition shadows the built-in signature and therefore withdraws the role.
fn command_citation_placement(command: &SyntaxNode, cx: LowerCtx<'_>) -> Option<CitationPlacement> {
    cx.signatures.command_at(command).and_then(|sig| {
        (sig.inline
            && sig
                .args
                .iter()
                .any(|spec| spec.content == ContentKind::TokenList))
        .then_some(sig.citation)
        .flatten()
    })
}

/// Whether `command` is a *sectioning* command (`\part` … `\subparagraph`), per the
/// signature DB's [`CommandSig::sectioning`] level. Prose reflow treats such a
/// command as a block-level statement. When the command is a direct child of a
/// prose paragraph, it becomes a paragraph-separated block with one blank line on
/// each side (see [`reflow_elements`]).
///
/// Read from the semantic layer, never from a name list in the formatter (decision
/// #2): sectioning level is exactly the kind of meaning the signature DB owns, and
/// `\section` is only a heading because the DB says so.
fn command_is_sectioning(command: &SyntaxNode, cx: LowerCtx<'_>) -> bool {
    cx.signatures
        .command_at(command)
        .is_some_and(|sig| sig.sectioning.is_some())
}

/// Whether `command` is the canonical label command that may remain attached to a
/// preceding section heading.
fn command_is_label(command: &SyntaxNode) -> bool {
    command_name(command).as_deref() == Some("label")
}

/// Whether `command` is a curated *block-level* command (`\usepackage`,
/// `\newcommand`, `\maketitle`, …), per the signature DB's [`CommandSig::block`]
/// flag. Prose reflow treats such a command like a sectioning one — a block-level
/// statement on its own line, whatever trivia the author wrote — except that a
/// block command **glued** to adjacent non-trivia keeps its authored adjacency
/// (see [`reflow_elements`]): breaking there materializes a space token TeX
/// typesets (`\ProcessOptions\relax`), where a heading's own `\par` makes the
/// materialized glue provably inert.
///
/// Read from the semantic layer rather than a formatter-owned name list. The
/// flag is positive and curated-only, so an un-signatured or
/// scanned-definition command is *not* block here and falls back to the residual
/// authored-break rule in [`line_is_command_only`].
///
/// A command whose signature declares a *required* argument must actually carry
/// an attached argument node. A **bare** head — `\newcommand` in
/// `\newcommand\foo{…}`, where the control-word run break leaves every argument
/// unattached — is a shape the attachment model did not capture, and
/// intercepting it is not pass-stable: glued to a forced-break sibling it is
/// stranded at end-of-line by the ride path (glue the engine itself breaks), so
/// the adjacency this gate reads differs between passes
/// (`pgfcomp-version-0-65.sty`). A bare head falls to the residual rule, whose
/// authored-break preservation *is* the fixed point of that stranding.
fn command_is_block(command: &SyntaxNode, cx: LowerCtx<'_>) -> bool {
    cx.signatures.command_at(command).is_some_and(|sig| {
        sig.block
            && (sig.args.iter().all(|arg| !arg.required) || command.children().next().is_some())
    })
}

/// Lower one `STATEMENT` node (a `;`-terminated statement in a curated
/// `statementBody` environment body) under [`WrapMode::Reflow`] — the
/// structural statement lowering, entered from `reflow_elements_checked`'s
/// `STATEMENT` arm.
///
/// The interior reflows under [`ReflowKind::StatementInterior`]: a lone
/// newline is a plain atom boundary the width fill re-decides, the
/// command-only residue is off (a width-owned body must not mint forced
/// breaks), a comment still rides and ends its line, and a forced-break child
/// (a `{label}` holding an environment) commits as its own segment with a
/// glued `;` riding its last line. Gaps additionally consult the TikZ unit
/// model (`semantic::tikz::statement_glue`): a unit-internal gap — an operator
/// and what it connects, `at` and its coordinate, a coordinate and its
/// operation, an operation and its argument — renders as a single space and
/// never breaks, so a width wrap lands only at unit boundaries (idiomatically,
/// before a path operator). The enclosing [`Ir::indent`] then hangs every
/// continuation line after the statement head — width wraps, post-comment
/// lines, and block segments alike — one step under it, so a wrapped
/// `\node[…] at (2,3)` / `{…};` reads as a continuation rather than a sibling.
/// Two leading shapes remain outside that hang: a bound
/// [`SyntaxKind::DOC_COMMENT`], which documents the head, and a maximal run of
/// comment-terminated command-only lines before the eventual TikZ statement.
/// [`statement_leading_comment`] and
/// [`statement_leading_command_comment_prefix`] identify those shapes so their
/// commands and the statement head all remain at the body indentation (issue
/// #158). Once non-command statement content begins, a post-comment tail remains
/// a genuine hanging continuation.
///
/// Fixed point (Tier 1): the hang is *emitted*, never read. Statement extent
/// re-derives from the terminating `;` on every parse, however the emitted
/// layout breaks — an emitted wrap is leading line trivia to the next parse —
/// and the interior reads only width, gluedness, comment presence, and
/// non-trivia token text (the unit model), all preserved or content-derived.
/// A peeled leading comment is re-emitted on its own line immediately above the
/// head, so it rebinds as the same `DOC_COMMENT` on the next parse. A peeled
/// command prefix retains each trailing comment, and therefore each forced line
/// boundary, so the same maximal prefix is recognized on the next pass.
/// So `fmt(fmt(x)) == fmt(x)` holds by structure, where the
/// flush-continuation contract this replaces had to *forbid* the hang.
fn lower_statement(node: &SyntaxNode, cx: LowerCtx<'_>) -> Ir {
    let comment = statement_leading_comment(node, cx);
    let body_cx = LowerCtx {
        omitted_leading_comment: comment
            .as_ref()
            .map(SyntaxNode::text_range)
            .or(cx.omitted_leading_comment),
        ..cx
    };
    let elements: Vec<SyntaxElement> = node.children_with_tokens().collect();
    let prefix_end = statement_leading_command_comment_prefix(&elements, body_cx);

    if comment.is_none() && prefix_end == 0 {
        return Ir::indent(reflow_elements(
            elements.into_iter(),
            cx,
            ReflowKind::StatementInterior,
        ));
    }

    let mut leading = Vec::new();
    if let Some(comment) = comment {
        leading.push(Ir::join(
            Ir::hard_line(),
            comment
                .children_with_tokens()
                .filter_map(SyntaxElement::into_token)
                .filter(|token| token.kind() == SyntaxKind::COMMENT)
                .map(|token| Ir::verbatim(token.text())),
        ));
    }
    if prefix_end > 0 {
        if !leading.is_empty() {
            leading.push(Ir::hard_line());
        }
        leading.push(reflow_elements(
            elements[..prefix_end].iter().cloned(),
            body_cx,
            ReflowKind::StatementInterior,
        ));
    }
    let body = reflow_elements(
        elements[prefix_end..].iter().cloned(),
        body_cx,
        ReflowKind::StatementInterior,
    );
    Ir::concat([Ir::concat(leading), Ir::hard_line(), Ir::indent(body)])
}

/// The maximal statement prefix made of comment-terminated command-only lines.
/// The trailing comment is the structural boundary: it forces the line break
/// and preserves this positional classification across passes without
/// consulting authored newline shape. Non-command content before a comment ends
/// the prefix, so a genuine path tail such as `\draw (0,0) % note` remains
/// hanging-indented. This is only a layout gate; it makes no claim about what an
/// arbitrary macro expands to.
fn statement_leading_command_comment_prefix(elements: &[SyntaxElement], cx: LowerCtx<'_>) -> usize {
    let mut prefix_end = 0;
    let mut saw_command = false;
    for (idx, element) in elements.iter().enumerate() {
        match element {
            SyntaxElement::Token(token) if token.kind() == SyntaxKind::WHITESPACE => {}
            SyntaxElement::Token(token) if token.kind() == SyntaxKind::NEWLINE => {
                if saw_command {
                    return prefix_end;
                }
            }
            SyntaxElement::Node(node)
                if node.kind() == SyntaxKind::COMMAND
                    && !cx.suppressed(node.text_range())
                    && !(cx.is_dtx && contains_indented_dtx_comment(node)) =>
            {
                saw_command = true;
            }
            SyntaxElement::Token(token)
                if token.kind() == SyntaxKind::COMMENT
                    && saw_command
                    && !cx.suppressed(token.text_range()) =>
            {
                prefix_end = idx + 1;
                saw_command = false;
            }
            _ => return prefix_end,
        }
    }
    prefix_end
}

/// The bound own-line comment at the front of a structural statement, if its
/// enclosing construct is available to ordinary lowering. The parser keeps the
/// comment inside the command or conditional it documents, and the statement
/// correctly owns that construct; this positional read distinguishes that
/// leading annotation from a genuine mid-statement comment. Its own-line status
/// is a preserved predicate: [`lower_statement`] re-emits the separating hard
/// line that makes it bind forward again.
fn statement_leading_comment(node: &SyntaxNode, cx: LowerCtx<'_>) -> Option<SyntaxNode> {
    let construct = node.first_child()?;
    if cx.suppressed(construct.text_range())
        || cx.is_dtx && contains_indented_dtx_comment(&construct)
    {
        return None;
    }
    let comment = construct
        .first_child()
        .filter(|child| child.kind() == SyntaxKind::DOC_COMMENT)
        .filter(|comment| comment.text_range().start() == node.text_range().start())?;
    (!cx.suppressed(comment.text_range())).then_some(comment)
}

/// Pre-pass over a paragraph element stream: splice each `STATEMENT` wrapper's
/// children into the stream, restoring the sibling layout a pre-statement parse
/// produced. Taken by every path that lays a statement-body paragraph out as a
/// *line stream* — the non-`Reflow` prose modes and the `Preserve` paragraph arm
/// — so the wrapper changes no bytes there; only the structural `Reflow`
/// lowering ([`lower_statement`]) reads the node itself.
fn flatten_statements(elements: Vec<SyntaxElement>) -> Vec<SyntaxElement> {
    if !elements.iter().any(|e| {
        e.as_node()
            .is_some_and(|n| n.kind() == SyntaxKind::STATEMENT)
    }) {
        return elements;
    }
    let mut out = Vec::with_capacity(elements.len());
    for element in elements {
        match &element {
            SyntaxElement::Node(node) if node.kind() == SyntaxKind::STATEMENT => {
                out.extend(node.children_with_tokens());
            }
            _ => out.push(element),
        }
    }
    out
}

/// Pre-pass over a reflow element stream: replace each *inline* prose command
/// ([`command_is_inline_prose`]) with its surface tokens, splicing its prose
/// argument's body directly into the stream. The body's inter-word whitespace then
/// becomes break opportunities in the surrounding paragraph fill, and the prose
/// `{`/`}` glue onto the adjacent words — so an inline footnote wraps as running
/// text instead of exploding into a block. Non-prose arguments and the control
/// word are kept verbatim; nested inline prose commands are expanded recursively.
/// `glue_matched_args` is enabled only in ordinary prose and prose-argument
/// reflow. A code-like statement or preserve-mode stream keeps its
/// argument-boundary trivia because flattening an inner command must not make an
/// opaque parent group newly flat on the next pass. Virtual `.dtx` margin prose
/// and other margin-carrying streams also keep it: the corpus gate shows that
/// deleting the boundary there is not yet fixed-point stable.
fn flatten_inline_prose(
    elements: Vec<SyntaxElement>,
    cx: LowerCtx<'_>,
    glue_matched_args: bool,
) -> Vec<SyntaxElement> {
    let mut out = Vec::new();
    for element in elements {
        match &element {
            SyntaxElement::Node(node)
                if node.kind() == SyntaxKind::COMMAND && command_is_inline_prose(node, cx) =>
            {
                expand_inline_prose(node, cx, glue_matched_args, &mut out);
            }
            _ => out.push(element),
        }
    }
    out
}

/// Expand one inline prose command into `out` (see [`flatten_inline_prose`]): the
/// control word and any non-prose argument are emitted verbatim, while each prose
/// argument is spliced delimiter-and-body via [`splice_prose_group`]. At
/// prose-reflow altitude, collapsible trivia before a matched argument slot is
/// dropped: TeX's undelimited argument scanner already ignores it, and retaining
/// it would let an authored space versus newline choose the command's layout. A
/// comment remains a barrier because the line ending it consumes cannot be
/// removed. Slot matching mirrors [`lower_command`] so an omitted optional does
/// not misalign positions.
fn expand_inline_prose(
    node: &SyntaxNode,
    cx: LowerCtx<'_>,
    glue_matched_args: bool,
    out: &mut Vec<SyntaxElement>,
) {
    let Some(sig) = cx.signatures.command_at(node) else {
        out.push(SyntaxElement::Node(node.clone()));
        return;
    };
    let mut slot = 0usize;
    let mut pending_trivia = Vec::new();
    for child in node.children_with_tokens() {
        if is_collapsible_trivia_element(&child) {
            pending_trivia.push(child);
            continue;
        }
        match child {
            SyntaxElement::Node(group)
                if matches!(group.kind(), SyntaxKind::GROUP | SyntaxKind::OPTIONAL) =>
            {
                let is_bracket = group.kind() == SyntaxKind::OPTIONAL;
                let kind = if is_bracket {
                    ArgKind::Bracket
                } else {
                    ArgKind::Brace
                };
                let spec = match_arg_slot(&sig.args, &mut slot, kind);
                let follows_comment = out.last().is_some_and(
                    |element| matches!(element, SyntaxElement::Token(t) if t.kind() == SyntaxKind::COMMENT),
                );
                if spec.is_none() || follows_comment || !glue_matched_args {
                    out.append(&mut pending_trivia);
                } else {
                    pending_trivia.clear();
                }
                let prose = spec.is_some_and(|spec| spec.content == ContentKind::Prose);
                if prose {
                    let (open, close) = if is_bracket {
                        (SyntaxKind::L_BRACKET, SyntaxKind::R_BRACKET)
                    } else {
                        (SyntaxKind::L_BRACE, SyntaxKind::R_BRACE)
                    };
                    splice_prose_group(&group, open, close, cx, glue_matched_args, out);
                } else {
                    out.push(SyntaxElement::Node(group));
                }
            }
            SyntaxElement::Token(token) if token.kind() == SyntaxKind::VERB => {
                out.append(&mut pending_trivia);
                if token.text().starts_with('{') {
                    match_verbatim_arg_slot(&sig.args, &mut slot);
                }
                out.push(SyntaxElement::Token(token));
            }
            other => {
                out.append(&mut pending_trivia);
                out.push(other);
            }
        }
    }
    out.append(&mut pending_trivia);
}

/// Splice a prose group's delimiters and body into `out` (see
/// [`flatten_inline_prose`]). The group's own `open`/`close` tokens are emitted
/// around the body; the body's leading and trailing whitespace is dropped so the
/// delimiters glue tight to the first and last words, and nested inline prose
/// commands inside the body are expanded recursively.
///
/// The delimiter kinds are the node's *own* pair — `{`/`}` for a `GROUP`, `[`/`]`
/// for an `OPTIONAL` — never "any closer". A bracket is ordinary prose content
/// inside a brace group (`\emph{a [b] c}`), and matching it as a delimiter dropped
/// it from the body: the `open` arm is guarded by `open.is_none()`, but a `close`
/// arm matching both kinds is overwritten by the group's real closer, so the `]`
/// vanished from the output entirely — the whitespace-only invariant broken at
/// default settings. Kind-matching is sufficient without also demanding the *last*
/// such token: the formatter only runs on clean parses, where a `GROUP` holds
/// exactly one `R_BRACE` (a second would have closed it) and the parser ends an
/// `OPTIONAL` at its first `]`.
fn splice_prose_group(
    group: &SyntaxNode,
    open_kind: SyntaxKind,
    close_kind: SyntaxKind,
    cx: LowerCtx<'_>,
    glue_matched_args: bool,
    out: &mut Vec<SyntaxElement>,
) {
    let mut open: Option<SyntaxElement> = None;
    let mut close: Option<SyntaxElement> = None;
    let mut body: Vec<SyntaxElement> = Vec::new();
    for element in group.children_with_tokens() {
        match &element {
            SyntaxElement::Token(t) if t.kind() == open_kind && open.is_none() => {
                open = Some(element);
            }
            SyntaxElement::Token(t) if t.kind() == close_kind => {
                close = Some(element);
            }
            _ => body.push(element),
        }
    }
    while body.first().is_some_and(is_collapsible_trivia_element) {
        body.remove(0);
    }
    while body.last().is_some_and(is_collapsible_trivia_element) {
        body.pop();
    }
    if let Some(open) = open {
        out.push(open);
    }
    out.extend(flatten_inline_prose(body, cx, glue_matched_args));
    if let Some(close) = close {
        out.push(close);
    }
}

/// True when `element` is a collapsible-trivia token (whitespace/newline), the
/// boundary whitespace [`splice_prose_group`] trims so a prose delimiter glues to
/// its body.
fn is_collapsible_trivia_element(element: &SyntaxElement) -> bool {
    matches!(element, SyntaxElement::Token(t) if is_collapsible_trivia(t.kind()))
}

/// Split an inline command's token-list argument into paragraph-fill atoms.
/// Everything before the first entry (the command head and any optional arguments)
/// stays on that atom; everything after the final entry (the closing delimiter and
/// any attached suffix) stays on the last. Returns `None` when there is no useful
/// top-level comma split or the body carries a preserved predicate that forbids
/// segmentation.
fn inline_token_list_atoms(node: &SyntaxNode, cx: LowerCtx<'_>) -> Option<Vec<Ir>> {
    let sig = cx.signatures.command_at(node)?;
    let mut slot = 0usize;
    let mut found = false;
    let mut atoms: Vec<Vec<Ir>> = vec![Vec::new()];

    for element in alignment_cell_elements(node.children_with_tokens(), cx) {
        match element {
            SyntaxElement::Node(group)
                if matches!(group.kind(), SyntaxKind::GROUP | SyntaxKind::OPTIONAL) =>
            {
                let is_bracket = group.kind() == SyntaxKind::OPTIONAL;
                let (open_kind, close_kind, kind) = if is_bracket {
                    (
                        SyntaxKind::L_BRACKET,
                        SyntaxKind::R_BRACKET,
                        ArgKind::Bracket,
                    )
                } else {
                    (SyntaxKind::L_BRACE, SyntaxKind::R_BRACE, ArgKind::Brace)
                };
                let spec = match_arg_slot(&sig.args, &mut slot, kind);
                if spec.is_some_and(|spec| spec.content == ContentKind::TokenList) {
                    let GroupSegments {
                        open,
                        mut parts,
                        close,
                        splits,
                    } = segment_delimited_body(&group, open_kind, close_kind, cx, true)?;
                    if splits == 0 {
                        return None;
                    }
                    let _ = peel_padding(&mut parts, Edge::Leading);
                    let _ = peel_padding(&mut parts, Edge::Trailing);
                    atoms.last_mut().unwrap().push(open);
                    for part in parts {
                        if is_segment_separator(&part) {
                            atoms.push(Vec::new());
                        } else {
                            atoms.last_mut().unwrap().push(part);
                        }
                    }
                    atoms.last_mut().unwrap().push(close);
                    found = true;
                } else {
                    atoms.last_mut().unwrap().push(lower_node(&group, cx));
                }
            }
            SyntaxElement::Token(token) if token.kind() == SyntaxKind::VERB => {
                if token.text().starts_with('{') {
                    match_verbatim_arg_slot(&sig.args, &mut slot);
                }
                atoms
                    .last_mut()
                    .unwrap()
                    .push(lower_loose_token(&token, cx));
            }
            SyntaxElement::Node(child) => atoms.last_mut().unwrap().push(lower_node(&child, cx)),
            SyntaxElement::Token(token) => {
                atoms
                    .last_mut()
                    .unwrap()
                    .push(lower_loose_token(&token, cx));
            }
        }
    }

    found.then(|| atoms.into_iter().map(Ir::concat).collect())
}

/// Lower a `COMMAND` whose signature marks an argument's content kind (see
/// [`command_has_managed_arg`], which gates this path). Each attached `{…}`/`[…]`
/// group is matched to its signature slot — kind-aware, so an omitted optional does
/// not misalign positions (`\section{Title}` binds the `{title}` slot, not a
/// leading `[short]`) — and a group filling a prose slot is reflowed via
/// [`lower_inline_prose_group`] or [`lower_prose_group`], according to the command's
/// inline flag. Everything else (non-prose slots, groups past the declared
/// arity that the greedy parser over-attached, trivia) lowers exactly as the generic
/// path would.
fn lower_command(node: &SyntaxNode, cx: LowerCtx<'_>) -> Ir {
    lower_command_with_math_spacing(node, cx, MathSpacing::Normal)
}

fn lower_command_with_math_spacing(
    node: &SyntaxNode,
    cx: LowerCtx<'_>,
    math_spacing: MathSpacing,
) -> Ir {
    let Some(sig) = cx.signatures.command_at(node) else {
        // Defensive: the guard already proved a prose signature exists.
        return Ir::concat(lower_element_stream(node.children_with_tokens(), cx));
    };
    let math_only = sig
        .args
        .iter()
        .any(|spec| spec.domain == ArgumentDomain::Math);

    let mut out: Vec<Ir> = Vec::new();
    let mut slot = 0usize;
    let mut iter = alignment_cell_elements(node.children_with_tokens(), cx)
        .into_iter()
        .peekable();
    while let Some(element) = iter.next() {
        match element {
            SyntaxElement::Node(child)
                if matches!(child.kind(), SyntaxKind::GROUP | SyntaxKind::OPTIONAL) =>
            {
                let is_bracket = child.kind() == SyntaxKind::OPTIONAL;
                let (open, close) = if is_bracket {
                    (SyntaxKind::L_BRACKET, SyntaxKind::R_BRACKET)
                } else {
                    (SyntaxKind::L_BRACE, SyntaxKind::R_BRACE)
                };
                let kind = if is_bracket {
                    ArgKind::Bracket
                } else {
                    ArgKind::Brace
                };
                let spec = match_arg_slot(&sig.args, &mut slot, kind);
                if math_only {
                    if spec.is_some_and(|spec| spec.domain == ArgumentDomain::Math) {
                        out.push(lower_math_argument_group(&child, cx, math_spacing));
                    } else {
                        out.push(Ir::verbatim(child.text().to_string()));
                    }
                    continue;
                }
                match spec.map(|s| s.content) {
                    Some(ContentKind::Identifier) => {
                        out.push(lower_identifier_group(&child));
                    }
                    Some(ContentKind::Algorithm2e) => {
                        out.push(lower_prose_group_kind(
                            &child,
                            open,
                            close,
                            cx,
                            ReflowKind::Algorithm2e,
                        ));
                    }
                    Some(ContentKind::Prose) if sig.inline => {
                        out.push(lower_inline_prose_group(&child, open, close, cx));
                    }
                    Some(ContentKind::Prose) => {
                        out.push(lower_prose_group(&child, open, close, cx));
                    }
                    Some(ContentKind::TokenList) => {
                        // Outside a paragraph fill, keep a token list as one inline
                        // atom. The paragraph path exposes its top-level entries as
                        // fill atoms before reaching this lowering.
                        out.push(
                            lower_token_list_group(&child, open, close, cx)
                                .unwrap_or_else(|| lower_node(&child, cx)),
                        );
                    }
                    // A proven `key=value` list: its processor strips spaces around
                    // entries, so the layout may also break at a comma the author
                    // glued (see [`ContentKind::Keyval`]). A `{…}` reaches this only
                    // through the curated tier — the setters (`\pgfkeys`, `\tikzset`)
                    // whose whole mandatory argument is the key list.
                    Some(ContentKind::Keyval) => {
                        out.push(
                            lower_segmented_group(&child, open, close, cx, true)
                                .unwrap_or_else(|| lower_node(&child, cx)),
                        );
                    }
                    _ => out.push(lower_node(&child, cx)),
                }
            }
            SyntaxElement::Node(child) if math_only => {
                out.push(Ir::verbatim(child.text().to_string()))
            }
            SyntaxElement::Node(child) => out.push(lower_node(&child, cx)),
            SyntaxElement::Token(token) if token.kind() == SyntaxKind::VERB => {
                if token.text().starts_with('{') {
                    match_verbatim_arg_slot(&sig.args, &mut slot);
                }
                out.push(lower_loose_token(&token, cx));
            }
            SyntaxElement::Token(token) if math_only => out.push(Ir::verbatim(token.text())),
            SyntaxElement::Token(token) if is_collapsible_trivia(token.kind()) => {
                out.push(classify_trivia(
                    consume_gap_widened(&token, &mut iter),
                    cx.in_alignment_cell,
                ));
            }
            SyntaxElement::Token(token) => out.push(lower_loose_token(&token, cx)),
        }
    }
    Ir::concat(out)
}

/// Inline prose can reach command lowering inside an opaque group, outside the
/// paragraph path that normally flattens it. Include its delimiters in the prose
/// stream so they stay glued to the first and last words even when following
/// content overflows. Block-style delimiter breaks would introduce space tokens
/// inside the argument (issue #188).
fn lower_inline_prose_group(
    node: &SyntaxNode,
    open: SyntaxKind,
    close: SyntaxKind,
    cx: LowerCtx<'_>,
) -> Ir {
    let mut elements = Vec::new();
    splice_prose_group(node, open, close, cx, false, &mut elements);
    reflow_elements(elements.into_iter(), cx, ReflowKind::ProseArg)
}

/// Lower a block-level prose argument: like [`lower_bracketed`], but the body is reflowed
/// to the line width ([`reflow_elements`]) and the whole thing is wrapped in a soft
/// [`Ir::group`] so it stays on one line when it fits (`\caption{short}`) and
/// breaks the delimiters onto their own lines, indenting and word-wrapping the body,
/// when it does not. Empty bodies collapse to the bare delimiters.
///
/// A `%` comment at either edge of the body takes [`lower_bracketed`]'s two
/// guards, for the same reasons — the soft group is the only thing that makes
/// them look different here:
///
/// - A comment **glued to the open delimiter** rides the opener's line. Pushing
///   it to its own indented line would turn the newline the formatter writes
///   after `{` into a real space token inside the group, changing `\caption{%\n}`
///   (empty — the `%` eats the source newline) into `\caption{ }`.
/// - A comment the body **ends** with forces the group open, so the close
///   delimiter takes its own line. Flat, the group renders `\caption{x%}` and the
///   `%` comments the closing brace out — a content deletion, and one the
///   whitespace-only oracle sees only as a comment growing a `}`.
///
/// Both bite exactly when the whole body reflows to a *single* line: any second
/// line puts a hard separator between them, which already forces the group.
fn lower_prose_group(
    node: &SyntaxNode,
    open: SyntaxKind,
    close: SyntaxKind,
    cx: LowerCtx<'_>,
) -> Ir {
    lower_prose_group_kind(node, open, close, cx, ReflowKind::ProseArg)
}

fn lower_prose_group_kind(
    node: &SyntaxNode,
    open: SyntaxKind,
    close: SyntaxKind,
    cx: LowerCtx<'_>,
    kind: ReflowKind,
) -> Ir {
    let mut open_ir = Ir::Nil;
    let mut close_ir = Ir::Nil;
    let mut body_elements: Vec<SyntaxElement> = Vec::new();
    for element in alignment_cell_elements(node.children_with_tokens(), cx) {
        match &element {
            SyntaxElement::Token(t) if t.kind() == open && matches!(open_ir, Ir::Nil) => {
                open_ir = Ir::verbatim(t.text());
            }
            SyntaxElement::Token(t) if t.kind() == close => {
                close_ir = Ir::verbatim(t.text());
            }
            _ => body_elements.push(element),
        }
    }

    // The parser emits leading whitespace/newlines as their own trivia tokens, so
    // the first body element is a `COMMENT` iff it was glued to the opener.
    let has_leading_comment = body_elements
        .first()
        .and_then(SyntaxElement::as_token)
        .is_some_and(|t| t.kind() == SyntaxKind::COMMENT);
    let open_ir = if has_leading_comment {
        let comment = body_elements.remove(0);
        Ir::concat([open_ir, Ir::verbatim(comment.as_token().unwrap().text())])
    } else {
        open_ir
    };
    let has_trailing_comment = body_ends_with_comment(node, close);

    let body = reflow_elements(body_elements.into_iter(), cx, kind);
    if matches!(body, Ir::Nil) {
        if has_leading_comment {
            // `\caption{%\n}`: the comment already rode the open delimiter, so
            // the close must still drop to its own line.
            Ir::concat([open_ir, Ir::hard_line(), close_ir])
        } else {
            Ir::concat([open_ir, close_ir])
        }
    } else {
        let brk: fn() -> Ir =
            if has_leading_comment || has_trailing_comment || kind == ReflowKind::Algorithm2e {
                Ir::hard_line
            } else {
                Ir::soft_line
            };
        Ir::group(Ir::concat([
            open_ir,
            Ir::indent(Ir::concat([brk(), body])),
            brk(),
            close_ir,
        ]))
    }
}

/// Whether the last content token of `node`'s body — the `close` delimiter and
/// any trailing collapsible trivia skipped — is a `%` comment, i.e. whatever the
/// body lowers to ends a line and nothing may follow it there.
///
/// Read at any depth, because a comment nested in the last child is still the
/// last thing emitted *unless* that child's own lowering already put a break
/// after it, which is exactly what the delimiter-bearing lowerings do
/// (`\caption{\emph{a%\n}}` ends on `\emph`'s `}`, not on the comment).
fn body_ends_with_comment(node: &SyntaxNode, close: SyntaxKind) -> bool {
    let mut token = node.last_token();
    while let Some(t) = token {
        if !node.text_range().contains_range(t.text_range()) {
            return false; // walked out of the group (an unclosed body)
        }
        match t.kind() {
            SyntaxKind::COMMENT => return true,
            k if k == close || is_collapsible_trivia(k) => token = t.prev_token(),
            _ => return false,
        }
    }
    false
}

/// Keep a token list together outside paragraph fill while normalizing its
/// top-level entry padding. Nested groups retain their own whitespace semantics.
fn lower_token_list_group(
    node: &SyntaxNode,
    open_kind: SyntaxKind,
    close_kind: SyntaxKind,
    cx: LowerCtx<'_>,
) -> Option<Ir> {
    if node
        .children_with_tokens()
        .any(|element| matches!(element.kind(), SyntaxKind::DOC_MARGIN | SyntaxKind::GUARD))
    {
        return None;
    }
    let GroupSegments {
        open,
        mut parts,
        close,
        ..
    } = segment_delimited_body(node, open_kind, close_kind, cx, true)?;
    let _ = peel_padding(&mut parts, Edge::Leading);
    let _ = peel_padding(&mut parts, Edge::Trailing);
    Some(Ir::concat([
        open,
        Ir::concat(parts.into_iter().filter(|part| !is_segment_separator(part))),
        close,
    ]))
}

/// Keep keys searchable as one source line. Only a flat literal admits gap
/// normalization; nested syntax may carry whitespace-sensitive macro arguments,
/// and comments must retain the line endings they consume. The preserved form
/// is its own fixed point, independent of width or authored newline shape.
fn lower_identifier_group(node: &SyntaxNode) -> Ir {
    let preserve = || Ir::verbatim(node.text().to_string());
    let mut parts = Vec::new();
    let mut iter = node.children_with_tokens().peekable();
    while let Some(element) = iter.next() {
        match element {
            SyntaxElement::Token(token) if is_collapsible_trivia(token.kind()) => {
                let gap = consume_gap(&token, &mut iter);
                if gap == Gap::Blank {
                    return preserve();
                }
                parts.push(Ir::verbatim(gap.flat()));
            }
            SyntaxElement::Token(token)
                if matches!(
                    token.kind(),
                    SyntaxKind::COMMENT | SyntaxKind::DOC_MARGIN | SyntaxKind::GUARD
                ) =>
            {
                return preserve();
            }
            SyntaxElement::Token(token) => parts.push(Ir::verbatim(token.text())),
            SyntaxElement::Node(_) => return preserve(),
        }
    }
    Ir::concat(parts)
}

/// Collapse top-level argument gaps without exposing word-wrap opportunities.
/// Opaque environment values use this to stay together while retaining interior
/// spaces. Interior newlines collapse to spaces.
///
/// Returns `None` — the caller falls back to the generic form ([`lower_node`]) — when
/// the group is *not* safely collapsible: it holds a blank-line paragraph break, a `%`
/// comment, docstrip framing, or force-break content (a nested environment,
/// display math, `\\`). Those keep the indented multi-line block form. Mirrors
/// [`lower_bracketed`]'s delimiter handling and edge-break trimming.
fn collapse_arg_group(
    node: &SyntaxNode,
    open: SyntaxKind,
    close: SyntaxKind,
    cx: LowerCtx<'_>,
) -> Option<Ir> {
    let mut open_ir = Ir::Nil;
    let mut close_ir = Ir::Nil;
    let mut body: Vec<Ir> = Vec::new();
    let mut iter = node.children_with_tokens().peekable();
    while let Some(element) = iter.next() {
        match element {
            SyntaxElement::Token(t) if t.kind() == open && matches!(open_ir, Ir::Nil) => {
                open_ir = Ir::verbatim(t.text());
            }
            SyntaxElement::Token(t) if t.kind() == close => {
                close_ir = Ir::verbatim(t.text());
            }
            SyntaxElement::Token(t) if is_collapsible_trivia(t.kind()) => {
                let gap = consume_gap(&t, &mut iter);
                if gap == Gap::Blank {
                    return None; // a blank-line `\par`: keep the block form
                }
                // The gap's flat spelling: a lone newline collapses to a single
                // space, pure inline whitespace stays verbatim, matching the
                // one-line generic lowering.
                body.push(Ir::verbatim(gap.flat()));
            }
            // Comments and docstrip framing must retain their line boundaries.
            SyntaxElement::Token(t)
                if matches!(
                    t.kind(),
                    SyntaxKind::COMMENT | SyntaxKind::DOC_MARGIN | SyntaxKind::GUARD
                ) =>
            {
                return None;
            }
            SyntaxElement::Token(t) => body.push(Ir::verbatim(t.text())),
            SyntaxElement::Node(child) => {
                let ir = lower_node(&child, cx);
                if ir.contains_forced_break() {
                    return None; // nested block content: keep the block form
                }
                body.push(ir);
            }
        }
    }
    let body = trim_trailing_break(trim_leading_break(Ir::concat(body)));
    Some(Ir::concat([open_ir, body, close_ir]))
}

/// Present a specialized lowerer with the virtual document's LaTeX stream rather
/// than the physical `.dtx` framing stored in the lossless CST. A documentation
/// margin and its following padding belong to the region wrapper; every other
/// element, including the preceding newline, remains available to the layout.
fn strip_virtual_dtx_framing(
    elements: impl IntoIterator<Item = SyntaxElement>,
    cx: LowerCtx<'_>,
) -> Vec<SyntaxElement> {
    if !cx.in_dtx_doc_region {
        return elements.into_iter().collect();
    }

    let mut stripped = Vec::new();
    let mut after_margin = false;
    for element in elements {
        match &element {
            SyntaxElement::Token(token) if token.kind() == SyntaxKind::DOC_MARGIN => {
                after_margin = true;
            }
            SyntaxElement::Token(token)
                if after_margin && token.kind() == SyntaxKind::WHITESPACE => {}
            _ => {
                after_margin = false;
                stripped.push(element);
            }
        }
    }
    stripped
}

/// Present a nested non-math alignment-cell lowerer with the same virtual stream
/// as the grid itself. Outside that narrow context, retain the node's ordinary
/// stream so enabling virtual grids does not alter unrelated document lowering.
fn alignment_cell_elements(
    elements: impl IntoIterator<Item = SyntaxElement>,
    cx: LowerCtx<'_>,
) -> Vec<SyntaxElement> {
    if cx.in_alignment_cell {
        strip_virtual_dtx_framing(elements, cx)
    } else {
        elements.into_iter().collect()
    }
}

/// Lower inline `$…$`/`\(…\)` or display `$$…$$`/`\[…\]` math. The delimiter
/// tokens are direct children of the math node and are emitted verbatim; the
/// `MATH` child (the body) is formatted by [`lower_math_body`].
fn lower_math(node: &SyntaxNode, cx: LowerCtx<'_>) -> Ir {
    Ir::concat(
        strip_virtual_dtx_framing(node.children_with_tokens(), cx)
            .into_iter()
            .map(|el| match el {
                SyntaxElement::Node(n) if n.kind() == SyntaxKind::MATH => lower_math_body(&n, cx),
                SyntaxElement::Node(n) => lower_node(&n, cx),
                SyntaxElement::Token(t) => Ir::verbatim(t.text()),
            }),
    )
}

/// Lower display math (`$$…$$` or `\[…\]`) as a block: the delimiters land on
/// their own lines with the body collapsed by [`lower_math_body`] and indented one
/// level, mirroring [`lower_bracketed`]'s shape. Display math is conceptually its
/// own vertical space, so unlike inline math (`\[ F \]` → `\[F\]`) it never
/// collapses onto a single line. An empty body degenerates to the bare adjacent
/// delimiters (`\[\]`, `$$$$`).
fn lower_display_math(node: &SyntaxNode, cx: LowerCtx<'_>) -> Ir {
    // Delimiters are one token for `\[`/`\]` but two `DOLLAR` tokens for `$$`, so
    // accumulate every delimiter token on each side of the `MATH` body.
    let mut open = String::new();
    let mut close = String::new();
    let mut body = Ir::Nil;
    let mut body_empty = true;
    let mut seen_body = false;
    let mut open_has_comment = false;
    for element in strip_virtual_dtx_framing(node.children_with_tokens(), cx) {
        match element {
            SyntaxElement::Node(n) if n.kind() == SyntaxKind::MATH => {
                // A `%` trailing the opening delimiter on the same source line
                // rides that line, exactly as an environment's `\begin`-line
                // comment does ([`split_environment`]), and is dropped from the
                // body by identity.
                let lifted = leading_inline_comment(&[SyntaxElement::Node(n.clone())]);
                if let Some(comment) = &lifted {
                    open.push_str(comment.text());
                    open_has_comment = true;
                }
                let elements: Vec<SyntaxElement> = n
                    .children_with_tokens()
                    .filter(|e| !is_lifted_comment(e, lifted.as_ref()))
                    .collect();
                let body_cx = cx.absorbing_trailing_control_newline(&elements);
                body_empty = elements.iter().all(|e| {
                    e.as_token()
                        .is_some_and(|t| is_collapsible_trivia(t.kind()))
                });
                body = trim_trailing_break(lower_display_formula_elements(&elements, body_cx));
                seen_body = true;
            }
            SyntaxElement::Token(t) if is_collapsible_trivia(t.kind()) => {}
            SyntaxElement::Token(t) if seen_body => close.push_str(t.text()),
            SyntaxElement::Token(t) => open.push_str(t.text()),
            // Unexpected non-MATH node child: defer to generic lowering.
            SyntaxElement::Node(n) => {
                body = lower_node(&n, cx);
                body_empty = false;
                seen_body = true;
            }
        }
    }

    if body_empty {
        if open_has_comment {
            // The lifted comment runs to end of line, so the closing delimiter
            // must not collapse onto it (it would be commented out).
            Ir::concat([Ir::verbatim(open), Ir::hard_line(), Ir::verbatim(close)])
        } else {
            Ir::concat([Ir::verbatim(open), Ir::verbatim(close)])
        }
    } else {
        Ir::concat([
            Ir::verbatim(open),
            Ir::indent(Ir::concat([Ir::hard_line(), body])),
            Ir::hard_line(),
            Ir::verbatim(close),
        ])
    }
}

/// Format a math body (a `MATH` node, or a `{…}` group body in math): collapse
/// internal `WHITESPACE`/`NEWLINE` runs to a single space, drop the runs at the
/// edges (trimming just inside the delimiters), keep `^`/`_` scripts tight, and
/// let a `%` comment force a line break (so a trailing comment never swallows the
/// closing delimiter).
fn lower_math_body(node: &SyntaxNode, cx: LowerCtx<'_>) -> Ir {
    lower_math_seq(node.children_with_tokens(), cx, MathSpacing::Normal, false)
}

/// Lower a single-formula display-math body per the resolved [`MathWrap`]
/// policy (`LowerCtx::math_wrap`): `Break` routes through the amsmath-style
/// breaker, `SingleLine` through the plain collapsing body (overflowing if too
/// long, like inline math), and `Preserve` keeps authored newlines as hard
/// breaks. `Auto` is resolved away in [`format_root`] and cannot reach here;
/// map it to the breaker defensively rather than panic. Takes the `MATH` node's
/// elements rather than the node itself so a caller can drop the lifted
/// opener-line comment ([`split_environment`]) before the lowering.
fn lower_display_formula_elements(elements: &[SyntaxElement], cx: LowerCtx<'_>) -> Ir {
    // A leading `\label{…}` is equation bookkeeping, not part of the formula: give
    // it its own line so the math starts fresh below it, under every wrap policy.
    // Grids handle leading labels in `build_alignment_grid`. The split recurses
    // so the remaining formula lowers under its normal `MathWrap` policy (and a
    // `\label\label` run peels one label per level).
    if let Some((label, rest)) = split_leading_label(elements) {
        return Ir::concat([
            lower_math_element(label, cx, MathSpacing::Normal),
            Ir::hard_line(),
            lower_display_formula_elements(rest, cx),
        ]);
    }
    match cx.math_wrap {
        MathWrap::Auto | MathWrap::Break => lower_display_math_body(elements, cx),
        MathWrap::SingleLine => {
            lower_math_seq(elements.iter().cloned(), cx, MathSpacing::Normal, false)
        }
        MathWrap::Preserve => {
            lower_math_seq(elements.iter().cloned(), cx, MathSpacing::Normal, true)
        }
    }
}

/// Split a display-math body whose first non-trivia atom is a `\label{…}` into
/// that label and the remaining formula elements. Returns `None` when the body
/// does not lead with a label, or when nothing but trivia follows it (a body that
/// is only a label stays on one line rather than gaining a dangling break). Scoped
/// to the single `\label` command by name — a trailing label, or any other
/// bookkeeping command, is deliberately left in place (see the formatter book).
fn split_leading_label(elements: &[SyntaxElement]) -> Option<(SyntaxElement, &[SyntaxElement])> {
    let idx = elements
        .iter()
        .position(|e| !is_collapsible_trivia_element(e))?;
    let node = elements[idx].as_node()?;
    if node.kind() != SyntaxKind::COMMAND
        || crate::ast::command_name(node).as_deref() != Some("label")
    {
        return None;
    }
    let rest = &elements[idx + 1..];
    if rest.iter().all(is_collapsible_trivia_element) {
        return None;
    }
    Some((elements[idx].clone(), rest))
}

/// The line-breaking role of a top-level math atom (see [`lower_display_math_body`]).
#[derive(Clone, Copy, PartialEq)]
enum MathRole {
    /// A term: a variable, number, group, command-with-arguments, script, etc.
    Operand,
    /// A binary operator (`+`, `-`, `\cdot`, `\times`, …) sitting between two
    /// operands. A break may be inserted *before* it.
    Binary,
    /// A relation (`=`, `\leq`, `\to`, …). The first one anchors the alignment;
    /// a later one is also a break point.
    Relation,
    /// A one-sided-limit `-` immediately before a closing delimiter. It is an
    /// ordinary postfix atom, so both adjacent boundaries stay tight.
    PostfixLeftLimit,
}

impl MathRole {
    fn is_operand_like(self) -> bool {
        matches!(self, Self::Operand | Self::PostfixLeftLimit)
    }
}

/// Source-spacing policy for a math list. Script arguments use TeX's compact
/// convention, while ordinary math lists receive explicit binary/relation padding.
#[derive(Clone, Copy, PartialEq)]
enum MathSpacing {
    Normal,
    Script,
}

/// One top-level atom of a display-math body, paired with its [`MathRole`].
struct MathPiece {
    ir: Ir,
    role: MathRole,
    block_break_before: bool,
    /// Whether this operator may start a continuation line. Multiplicative
    /// operators stay attached to the factor on their left, so a short
    /// additive term is not stranded between two breaks.
    break_before: bool,
    colon_relation_prefix: bool,
    spaced_slash: bool,
    slash: bool,
    /// Whether authored whitespace preceded this atom. Drives operand-operand
    /// spacing exactly as [`lower_math_seq`] does, so a tight command boundary
    /// (`\gamma)`, `}.`) stays tight rather than gaining a spurious space.
    space_before: bool,
    sized_delimiter: Option<DelimiterRole>,
    /// Net change in bracket nesting (`(`/`[`/`\{`/named delimiters vs their
    /// closers) contributed by this atom's own text. Used to suppress operator
    /// breaks inside a bracketed subexpression (`(1 - \gamma)` must not break at
    /// `-`, and a relation inside a set-builder `\{ … \}` is not an anchor).
    bracket_delta: i32,
}

struct MathSurfaceAtom {
    sized_delimiter: Option<DelimiterRole>,
    ir: Ir,
    multiline_block: bool,
    block_suffix: bool,
    class: MathClass,
    break_kind: MathBreakKind,
    delimiter: Option<DelimiterRole>,
    colon_relation_prefix: bool,
    starts_equals_relation: bool,
    spaced_slash: bool,
    slash: bool,
    control_word_operator: bool,
    starts_control_word_letter: bool,
    ends_control_word: bool,
    postfix_left_limit: bool,
}

/// Treat a size command and its following delimiter as one layout atom. TeX
/// consumes that delimiter as the command's argument, so no separator belongs
/// between them. Comments remain barriers and are handled by the caller.
fn lower_sized_math_atoms(
    element: SyntaxElement,
    iter: &mut impl Iterator<Item = SyntaxElement>,
    cx: LowerCtx<'_>,
    spacing: MathSpacing,
) -> Vec<MathSurfaceAtom> {
    let name = match &element {
        SyntaxElement::Node(node) if node.kind() == SyntaxKind::COMMAND => {
            let mut children = node.children_with_tokens();
            match (children.next(), children.next()) {
                (Some(SyntaxElement::Token(token)), None) => token.text().to_owned(),
                _ => return lower_math_atoms(element, cx, spacing),
            }
        }
        _ => return lower_math_atoms(element, cx, spacing),
    };
    let sized = ["big", "Big", "bigg", "Bigg"].iter().any(|prefix| {
        name.strip_prefix('\\')
            .and_then(|name| name.strip_prefix(prefix))
            .is_some_and(|suffix| matches!(suffix, "" | "l" | "r" | "m"))
    });
    if !sized {
        return lower_math_atoms(element, cx, spacing);
    }
    let mut next = element.next_sibling_or_token();
    let mut skip = 0;
    while next
        .as_ref()
        .is_some_and(|el| is_collapsible_trivia(el.kind()))
    {
        next = next.and_then(|element| element.next_sibling_or_token());
        skip += 1;
    }
    let Some(delimiter) = next else {
        return lower_math_atoms(element, cx, spacing);
    };
    let role = math_atoms(&delimiter)
        .next()
        .and_then(|atom| atom.delimiter);
    let Some(mut role) = role else {
        return lower_math_atoms(element, cx, spacing);
    };
    if name.ends_with('l') {
        role = DelimiterRole::Open;
    } else if name.ends_with('r') {
        role = DelimiterRole::Close;
    } else if name.ends_with('m') {
        role = DelimiterRole::Fence;
    }
    for _ in 0..skip {
        iter.next();
    }
    let Some(delimiter) = iter.next() else {
        return lower_math_atoms(element, cx, spacing);
    };
    let mut atoms = lower_math_atoms(delimiter, cx, spacing);
    if let Some(first) = atoms.first_mut() {
        let middle = name.ends_with('m');
        first.ir = Ir::concat([Ir::verbatim(name), first.ir.clone()]);
        first.starts_control_word_letter = false;
        first.sized_delimiter = Some(role);
        first.delimiter = Some(role);
        first.control_word_operator = middle;
        first.class = match role {
            DelimiterRole::Open => MathClass::Open,
            DelimiterRole::Close => MathClass::Close,
            DelimiterRole::Fence if middle => MathClass::Rel,
            DelimiterRole::Fence => MathClass::Fence,
        };
    }
    atoms
}

fn sized_delimiter_space(previous: Option<DelimiterRole>, current: Option<DelimiterRole>) -> bool {
    (previous == Some(DelimiterRole::Open) || current == Some(DelimiterRole::Close))
        && !(previous == Some(DelimiterRole::Open) && current == Some(DelimiterRole::Close))
}

/// Keep sibling math blocks at the body indent without detaching punctuation
/// or an operator prefix such as `=`, which can still introduce the next block.
#[derive(Default)]
struct MathBlockBoundaries {
    seen_block: bool,
    after_block: bool,
    line_has_expression: bool,
}

impl MathBlockBoundaries {
    fn before(&mut self, atom: &MathSurfaceAtom) -> bool {
        let should_break = self.after_block && !atom.block_suffix
            || atom.multiline_block && self.seen_block && self.line_has_expression;
        if should_break {
            self.line_has_expression = false;
        }
        if atom.multiline_block {
            self.seen_block = true;
            self.after_block = true;
            self.line_has_expression = false;
        } else if !atom.block_suffix {
            self.after_block = false;
            self.line_has_expression |= !matches!(atom.class, MathClass::Bin | MathClass::Rel);
        }
        should_break
    }
}

/// Formatter-owned precedence for the few math operators whose TeX atom class
/// alone is too coarse for readable line breaking.
#[derive(Clone, Copy, PartialEq)]
enum MathBreakKind {
    /// Use the ordinary [`MathClass`] binary/relation behavior.
    Class,
    /// A low-precedence additive operator, retained as an explicit break point.
    Additive,
    /// A multiplicative operator, kept with the surrounding term.
    Multiplicative,
    /// A conditional relation, kept inline and used to protect its condition's
    /// relation operators from equation-chain alignment.
    Conditional,
}

fn math_break_kind(element: &SyntaxElement) -> MathBreakKind {
    let SyntaxElement::Node(node) = element else {
        return match element.as_token().map(|token| token.text()) {
            Some("+" | "-") => MathBreakKind::Additive,
            _ => MathBreakKind::Class,
        };
    };
    if node.kind() == SyntaxKind::SCRIPTED {
        return node
            .children_with_tokens()
            .find(|element| {
                !matches!(
                    element.kind(),
                    SyntaxKind::WHITESPACE
                        | SyntaxKind::NEWLINE
                        | SyntaxKind::SUBSCRIPT
                        | SyntaxKind::SUPERSCRIPT
                )
            })
            .as_ref()
            .map_or(MathBreakKind::Class, math_break_kind);
    }
    if node.kind() != SyntaxKind::COMMAND {
        return MathBreakKind::Class;
    }
    match crate::ast::command_name(node).as_deref() {
        Some("pm" | "mp") => MathBreakKind::Additive,
        Some("cdot") => MathBreakKind::Multiplicative,
        Some("mid") => MathBreakKind::Conditional,
        _ => MathBreakKind::Class,
    }
}

/// Lower one CST element into the semantic atoms that its source surface
/// contains. Structural nodes stay indivisible; a coalesced `WORD` is sliced at
/// Unicode scalar boundaries. Consecutive relation scalars and a colon run
/// followed by `=` remain one surface atom, so authored compound spellings such
/// as `<=`, `:=`, and `::=` are not separated.
fn lower_math_atoms(
    el: SyntaxElement,
    cx: LowerCtx<'_>,
    spacing: MathSpacing,
) -> Vec<MathSurfaceAtom> {
    let atoms: Vec<_> = math_atoms(&el).collect();
    let break_kind = math_break_kind(&el);
    let SyntaxElement::Token(token) = &el else {
        let starts_control_word_letter = element_starts_control_word_letter(&el);
        let ends_control_word = element_ends_control_word(&el);
        let starts_with_equals = element_starts_with_token_text(&el, "=");
        let atom = atoms
            .into_iter()
            .next()
            .expect("a structural math element has one semantic atom");
        let control_word_operator =
            ends_control_word && matches!(atom.class, MathClass::Bin | MathClass::Rel);
        let block_suffix = atom.class == MathClass::Punct
            || atom.delimiter == Some(DelimiterRole::Close)
            || el.kind() == SyntaxKind::LINE_BREAK;
        // Comments can also make groups multiline, but only environment
        // wrappers establish sibling block boundaries.
        let block_shape = el.kind() == SyntaxKind::ENVIRONMENT
            || matches!(
                el.kind(),
                SyntaxKind::LEFT_RIGHT | SyntaxKind::GROUP | SyntaxKind::SCRIPTED
            ) && el.as_node().is_some_and(|node| {
                node.descendants()
                    .any(|child| child.kind() == SyntaxKind::ENVIRONMENT)
            });
        let ir = lower_math_element(el, cx, spacing);
        let multiline_block = block_shape && ir.contains_forced_break();
        return vec![MathSurfaceAtom {
            sized_delimiter: None,
            ir,
            multiline_block,
            block_suffix,
            class: atom.class,
            break_kind,
            delimiter: atom.delimiter,
            colon_relation_prefix: false,
            starts_equals_relation: atom.class == MathClass::Rel && starts_with_equals,
            spaced_slash: false,
            slash: false,
            control_word_operator,
            starts_control_word_letter,
            ends_control_word,
            postfix_left_limit: false,
        }];
    };
    if token.kind() != SyntaxKind::WORD {
        let starts_control_word_letter = token
            .text()
            .chars()
            .next()
            .is_some_and(is_control_word_letter);
        let ends_control_word = token.kind() == SyntaxKind::CONTROL_WORD;
        let atom = atoms
            .into_iter()
            .next()
            .expect("a math token has one semantic atom");
        let control_word_operator =
            ends_control_word && matches!(atom.class, MathClass::Bin | MathClass::Rel);
        return vec![MathSurfaceAtom {
            sized_delimiter: None,
            ir: lower_math_element(el, cx, spacing),
            multiline_block: false,
            block_suffix: atom.class == MathClass::Punct
                || atom.delimiter == Some(DelimiterRole::Close),
            class: atom.class,
            break_kind,
            delimiter: atom.delimiter,
            colon_relation_prefix: false,
            starts_equals_relation: false,
            spaced_slash: false,
            slash: false,
            control_word_operator,
            starts_control_word_letter,
            ends_control_word,
            postfix_left_limit: false,
        }];
    }

    let token_start = token.text_range().start();
    let mut surface: Vec<MathSurfaceAtom> = Vec::with_capacity(atoms.len());
    for atom in atoms {
        let start = usize::from(atom.range.start() - token_start);
        let end = usize::from(atom.range.end() - token_start);
        let text = &token.text()[start..end];
        if let Some(previous) = surface.last_mut() {
            let extends_colon_prefix =
                previous.colon_relation_prefix && atom.class == MathClass::Punct && text == ":";
            let completes_colon_relation =
                previous.colon_relation_prefix && atom.class == MathClass::Rel && text == "=";
            if previous.class == MathClass::Rel && atom.class == MathClass::Rel
                || extends_colon_prefix
                || completes_colon_relation
            {
                previous.ir = Ir::concat([previous.ir.clone(), Ir::verbatim(text)]);
                if completes_colon_relation {
                    previous.class = MathClass::Rel;
                    previous.colon_relation_prefix = false;
                }
                continue;
            }
        }
        surface.push(MathSurfaceAtom {
            sized_delimiter: None,
            ir: Ir::verbatim(text),
            multiline_block: false,
            block_suffix: atom.class == MathClass::Punct
                || atom.delimiter == Some(DelimiterRole::Close)
                || text == ".",
            class: atom.class,
            break_kind: match text {
                "+" | "-" => MathBreakKind::Additive,
                _ => MathBreakKind::Class,
            },
            delimiter: atom.delimiter,
            colon_relation_prefix: atom.class == MathClass::Punct && text == ":",
            starts_equals_relation: false,
            spaced_slash: atom.class == MathClass::Ord
                && text == "/"
                && (start == 0
                    && token
                        .prev_token()
                        .is_some_and(|token| is_collapsible_trivia(token.kind()))
                    || end == token.text().len()
                        && token
                            .next_token()
                            .is_some_and(|token| is_collapsible_trivia(token.kind()))),
            slash: atom.class == MathClass::Ord && text == "/",
            control_word_operator: false,
            starts_control_word_letter: text.chars().next().is_some_and(is_control_word_letter),
            ends_control_word: false,
            postfix_left_limit: text == "-",
        });
    }
    let following_token_closes = || {
        let mut next = token.next_token();
        while next
            .as_ref()
            .is_some_and(|token| is_collapsible_trivia(token.kind()))
        {
            next = next.and_then(|token| token.next_token());
        }
        next.and_then(|token| math_atoms(&SyntaxElement::Token(token)).next())
            .is_some_and(|atom| atom.delimiter == Some(DelimiterRole::Close))
    };
    for index in 0..surface.len() {
        let followed_by_closer = surface
            .get(index + 1)
            .is_some_and(|next| next.delimiter == Some(DelimiterRole::Close))
            || index + 1 == surface.len() && following_token_closes();
        surface[index].postfix_left_limit &= followed_by_closer;
    }
    // Anticipate the gap the sequencer will add before a following operator;
    // otherwise that gap makes only the next pass recognize the slash as spaced.
    for index in 0..surface.len() {
        if !surface[index].slash || surface[index].spaced_slash {
            continue;
        }
        let next_operator = surface.get(index + 1).is_some_and(|next| {
            spacing == MathSpacing::Normal && matches!(next.class, MathClass::Bin | MathClass::Rel)
        }) || index + 1 == surface.len()
            && token.next_token().is_some_and(|next| {
                let class = math_atoms(&SyntaxElement::Token(next.clone()))
                    .next()
                    .map(|atom| atom.class);
                class.is_some_and(|class| {
                    matches!(class, MathClass::Bin | MathClass::Rel)
                        && (spacing == MathSpacing::Normal
                            || next.kind() == SyntaxKind::CONTROL_WORD)
                })
            });
        surface[index].spaced_slash = next_operator;
    }
    surface
}

fn is_control_word_letter(character: char) -> bool {
    character.is_alphabetic()
}

fn element_starts_control_word_letter(element: &SyntaxElement) -> bool {
    let first = match element {
        SyntaxElement::Node(node) => node
            .descendants_with_tokens()
            .filter_map(|element| element.into_token())
            .find(|token| !is_collapsible_trivia(token.kind())),
        SyntaxElement::Token(token) => Some(token.clone()),
    };
    first
        .as_ref()
        .and_then(|token| token.text().chars().next())
        .is_some_and(is_control_word_letter)
}

fn element_starts_with_token_text(element: &SyntaxElement, expected: &str) -> bool {
    match element {
        SyntaxElement::Node(node) => node
            .descendants_with_tokens()
            .filter_map(|element| element.into_token())
            .find(|token| !is_collapsible_trivia(token.kind())),
        SyntaxElement::Token(token) => Some(token.clone()),
    }
    .is_some_and(|token| token.text() == expected)
}

fn element_ends_control_word(element: &SyntaxElement) -> bool {
    let last = match element {
        SyntaxElement::Node(node) => node
            .descendants_with_tokens()
            .filter_map(|element| element.into_token())
            .filter(|token| !is_collapsible_trivia(token.kind()))
            .last(),
        SyntaxElement::Token(token) => Some(token.clone()),
    };
    last.is_some_and(|token| token.kind() == SyntaxKind::CONTROL_WORD)
}

/// The [`MathRole`] of a top-level math atom. `prev_class` is the TeX class of the
/// preceding atom and `prev_opener` whether it ended with an opening delimiter: a
/// `+`/`-` (or any binary operator) with no operand to its left — either the first
/// atom, one after a binary, large operator, relation, opener, or punctuation —
/// is unary, so it glues to its operand and is *not* a break point, degrading to
/// an [`MathRole::Operand`]. A structurally proven postfix left-limit sign gets
/// its own tight role. The full class is needed here because [`MathRole`]
/// deliberately collapses operators and punctuation into `Operand`.
fn math_atom_role(
    class: MathClass,
    prev_class: MathClass,
    prev_opener: bool,
    postfix_left_limit: bool,
) -> MathRole {
    if postfix_left_limit {
        return MathRole::PostfixLeftLimit;
    }
    let raw = match class {
        MathClass::Bin => MathRole::Binary,
        MathClass::Rel => MathRole::Relation,
        _ => MathRole::Operand,
    };
    if raw == MathRole::Binary
        && (matches!(
            prev_class,
            MathClass::Op | MathClass::Bin | MathClass::Rel | MathClass::Punct
        ) || prev_opener)
    {
        MathRole::Operand
    } else {
        raw
    }
}

/// Collect the top-level atoms of a display-math `MATH` body as [`MathPiece`]s,
/// collapsing trivia runs exactly as [`lower_math_seq`] does. Returns `None` —
/// signalling the caller to take the plain non-breaking path — when the body
/// holds a comment or explicit line break (either forces its own break, which
/// does not compose with the operator-break layout), or has fewer than two atoms
/// (nothing to break).
fn collect_math_pieces(elements: &[SyntaxElement], cx: LowerCtx<'_>) -> Option<Vec<MathPiece>> {
    let mut pieces: Vec<MathPiece> = Vec::new();
    let mut blocks = MathBlockBoundaries::default();
    // Start as a non-operand so a leading `+`/`-` (no left operand) reads as unary
    // and glues to its operand rather than becoming a break point — e.g. `-x`.
    let mut prev_class = MathClass::Rel;
    let mut prev_opener = false;
    let mut bracket_depth = 0_i32;
    let mut conditional_at_top_level = false;
    let mut pending_space = false;
    let mut iter = strip_virtual_dtx_framing(elements.iter().cloned(), cx)
        .into_iter()
        .peekable();
    while let Some(el) = iter.next() {
        match el {
            SyntaxElement::Token(t) if is_collapsible_trivia(t.kind()) => {
                consume_gap(&t, &mut iter);
                if !pieces.is_empty() {
                    pending_space = true;
                }
            }
            SyntaxElement::Token(t) if t.kind() == SyntaxKind::COMMENT => return None,
            SyntaxElement::Node(n) if n.kind() == SyntaxKind::LINE_BREAK => return None,
            other => {
                for atom in lower_sized_math_atoms(other, &mut iter, cx, MathSpacing::Normal) {
                    let role = math_atom_role(
                        atom.class,
                        prev_class,
                        prev_opener,
                        atom.postfix_left_limit,
                    );
                    let completes_colon_relation = !pending_space
                        && pieces
                            .last()
                            .is_some_and(|piece| piece.colon_relation_prefix)
                        && atom.starts_equals_relation;
                    if completes_colon_relation {
                        let previous = pieces.last_mut().expect("checked above");
                        previous.ir = Ir::concat([previous.ir.clone(), atom.ir]);
                        previous.role = MathRole::Relation;
                        previous.colon_relation_prefix = false;
                        previous.bracket_delta += match atom.delimiter {
                            Some(DelimiterRole::Open) => 1,
                            Some(DelimiterRole::Close) => -1,
                            Some(DelimiterRole::Fence) | None => 0,
                        };
                        prev_class = MathClass::Rel;
                        prev_opener = atom.delimiter == Some(DelimiterRole::Open);
                        continue;
                    }
                    prev_class = atom.class;
                    prev_opener = atom.delimiter == Some(DelimiterRole::Open);
                    let bracket_delta = match atom.delimiter {
                        Some(DelimiterRole::Open) => 1,
                        Some(DelimiterRole::Close) => -1,
                        Some(DelimiterRole::Fence) | None => 0,
                    };
                    let block_break_before = blocks.before(&atom);
                    pieces.push(MathPiece {
                        ir: atom.ir,
                        role,
                        block_break_before,
                        break_before: !matches!(
                            atom.break_kind,
                            MathBreakKind::Multiplicative | MathBreakKind::Conditional
                        ) && !(role == MathRole::Relation
                            && conditional_at_top_level),
                        colon_relation_prefix: atom.colon_relation_prefix,
                        spaced_slash: atom.spaced_slash,
                        slash: atom.slash,
                        space_before: pending_space,
                        sized_delimiter: atom.sized_delimiter,
                        bracket_delta,
                    });
                    if atom.break_kind == MathBreakKind::Conditional && bracket_depth == 0 {
                        conditional_at_top_level = true;
                    }
                    bracket_depth += bracket_delta;
                    pending_space = false;
                }
            }
        }
    }
    // The display breaker computes every effective role up front, so it can make
    // operator-created slash gaps symmetric before building either layout.
    for index in 0..pieces.len() {
        if pieces[index].slash
            && (index > 0 && !pieces[index - 1].role.is_operand_like()
                || pieces
                    .get(index + 1)
                    .is_some_and(|next| !next.role.is_operand_like()))
        {
            pieces[index].spaced_slash = true;
        }
    }
    (pieces.len() >= 2).then_some(pieces)
}

/// Lower a display-math `MATH` body, additionally letting a too-long body *break*
/// before its eligible top-level binary/relation operators (amsmath style). The
/// layout is two-level: equation-chain *relations* align in a single column (a
/// chain of `=` reads as a stack, the second `=` under the first), their right-hand
/// sides share a column after the widest relation, and a breakable *binary*
/// operator hangs under that expression column. Multiplicative and
/// conditional operators use the narrower policy in [`math_break_kind`]. The
/// left-hand side and the first relation stay flat on the opening line. The whole
/// run between multiline environment boundaries is one [`Ir::group`], so an
/// ordinary expression stays on a single line whenever it fits. A comment or
/// explicit line break falls back to the shared math sequencer. Each segment's right-hand side is
/// its own nested group: breaking the body at its relations does not also break a
/// segment at its binary operators unless that segment overflows its own line. If
/// the LHS-derived relation column would make a continuation overflow, or padding
/// a shorter opening relation would make the first line overflow, the
/// printer breaks before the first relation and hangs the relation stack at the
/// display body's base indent instead.
fn lower_display_math_body(elements: &[SyntaxElement], cx: LowerCtx<'_>) -> Ir {
    let Some(pieces) = collect_math_pieces(elements, cx) else {
        return lower_math_seq_with_blocks(
            elements.iter().cloned(),
            cx,
            MathSpacing::Normal,
            false,
            true,
        );
    };

    // Each structural block boundary restarts relation alignment at the display
    // body's indent, so a preceding expression cannot push its siblings right.
    let mut parts = Vec::new();
    let mut start = 0;
    let mut depth = 0;
    for end in 1..=pieces.len() {
        if end == pieces.len() || pieces[end].block_break_before {
            if start > 0 {
                parts.push(Ir::hard_line());
            }
            parts.push(lower_math_piece_chain(&pieces[start..end], depth));
            depth += pieces[start..end]
                .iter()
                .map(|piece| piece.bracket_delta)
                .sum::<i32>();
            start = end;
        }
    }
    Ir::concat(parts)
}

fn lower_math_piece_chain(pieces: &[MathPiece], initial_depth: i32) -> Ir {
    let flat_width = |ir: &Ir| {
        Printer::new(FormatStyle::default())
            .print_flat(ir)
            .chars()
            .count()
    };

    // Bracket depth entering each atom (running sum of the preceding atoms'
    // deltas). A top-level break/relation is one seen at depth 0; operators
    // inside a parenthesized subexpression are structurally interior.
    let depth_before: Vec<i32> = {
        let mut acc = initial_depth;
        let mut v = Vec::with_capacity(pieces.len());
        for p in pieces {
            v.push(acc);
            acc += p.bracket_delta;
        }
        v
    };

    // Non-breaking separator between atoms `k-1` and `k`, mirroring
    // [`lower_math_seq`]: a space around any operator (either side) or across an
    // authored gap, nothing between two operands authored tight.
    let space_sep = |k: usize| -> Ir {
        if sized_delimiter_space(pieces[k - 1].sized_delimiter, pieces[k].sized_delimiter)
            || pieces[k].spaced_slash
            || pieces[k - 1].spaced_slash
        {
            Ir::verbatim(" ")
        } else if pieces[k].role == MathRole::PostfixLeftLimit
            || pieces[k - 1].role == MathRole::PostfixLeftLimit
        {
            Ir::Nil
        } else if pieces[k].role != MathRole::Operand
            || pieces[k - 1].role != MathRole::Operand
            || pieces[k].space_before
        {
            Ir::text(" ")
        } else {
            Ir::Nil
        }
    };
    // Whether a break may be inserted before atom `k`: an eligible top-level
    // binary operator with an operand to its left (a genuine infix `+`, not a
    // unary sign, a multiplicative join, or one nested in parentheses).
    let breakable = |k: usize| -> bool {
        pieces[k].break_before
            && pieces[k].role == MathRole::Binary
            && pieces[k - 1].role == MathRole::Operand
            && depth_before[k] == 0
    };
    // The first eligible top-level relation (a relation nested in parentheses or
    // protected by a conditional bar does not anchor or split a segment).
    let is_anchor = |k: usize| {
        pieces[k].break_before && pieces[k].role == MathRole::Relation && depth_before[k] == 0
    };

    // With no eligible top-level relation, continuation lines hang at the base
    // indent: the body breaks before each eligible top-level binary operator.
    let Some(anchor) = (0..pieces.len()).find(|&k| is_anchor(k)) else {
        let mut parts: Vec<Ir> = Vec::with_capacity(pieces.len() * 2);
        for (i, piece) in pieces.iter().enumerate() {
            if i > 0 {
                parts.push(if breakable(i) {
                    Ir::line()
                } else {
                    space_sep(i)
                });
            }
            parts.push(piece.ir.clone());
        }
        return Ir::group(Ir::concat(parts));
    };

    let mut lhs: Vec<Ir> = Vec::new();
    // Left-hand side, flat on the opening line.
    for (i, piece) in pieces[..anchor].iter().enumerate() {
        if i > 0 {
            lhs.push(space_sep(i));
        }
        lhs.push(piece.ir.clone());
    }
    // A multi-line left-hand side (a nested matrix/`aligned`/`cases` environment)
    // has no meaningful flat width — using it as the relation column would push
    // every hanging line dozens of columns right. Anchor the relations at the
    // base indent instead, and break before the first relation so the segment's
    // hanging indent still corresponds to real columns.
    let lhs_multiline = pieces[..anchor]
        .iter()
        .any(|p| p.ir.contains_forced_break());
    // The relation column: the left-hand side sits flat on the opening line, and
    // the first relation follows one space later. Every top-level relation aligns
    // here.
    let rel_col = if anchor == 0 || lhs_multiline {
        0
    } else {
        flat_width(&Ir::concat(lhs.clone())) + 1
    };

    let relation_width = (anchor..pieces.len())
        .filter(|&k| is_anchor(k))
        .map(|k| flat_width(&pieces[k].ir))
        .max()
        .expect("the first relation anchors the chain");
    let first_rhs = anchor + 1;
    let pad_first_relation = relation_width > flat_width(&pieces[anchor].ir)
        && first_rhs < pieces.len()
        && !is_anchor(first_rhs);

    let build_relation_layout = |relation_indent: usize, break_before_first: bool| {
        let mut parts = lhs.clone();
        // Each relation opens a segment running to the next relation. In the
        // aligned layout, the first relation stays beside the LHS and later
        // relations return to `relation_indent`. The base-indent fallback also
        // breaks before the first relation, so an over-deep LHS cannot dictate
        // the width of every continuation line.
        let mut i = anchor;
        let mut first_segment = true;
        while i < pieces.len() {
            if first_segment {
                if lhs_multiline {
                    parts.push(Ir::hard_line());
                } else if break_before_first {
                    parts.push(Ir::line());
                } else if anchor > 0 {
                    parts.push(space_sep(anchor));
                }
            } else {
                parts.push(Ir::line());
            }
            parts.push(pieces[i].ir.clone());
            let relw = flat_width(&pieces[i].ir);

            let start = i + 1;
            let mut j = start;
            while j < pieces.len() && !is_anchor(j) {
                j += 1;
            }
            if start < j && relw < relation_width {
                // Padding follows the chain's break decision, even when this
                // expression's own group can stay flat.
                parts.push(Ir::if_break(
                    Ir::Nil,
                    Ir::text(" ".repeat(relation_width - relw)),
                ));
            }
            let mut rhs: Vec<Ir> = Vec::with_capacity((j - start) * 2);
            for (offset, piece) in pieces[start..j].iter().enumerate() {
                let k = start + offset;
                rhs.push(if breakable(k) {
                    Ir::line()
                } else {
                    space_sep(k)
                });
                rhs.push(piece.ir.clone());
            }
            parts.push(Ir::group(Ir::align(relation_width + 1, Ir::concat(rhs))));

            first_segment = false;
            i = j;
        }

        Ir::align(relation_indent, Ir::concat(parts))
    };

    let body = if rel_col == 0 {
        build_relation_layout(0, lhs_multiline)
    } else {
        Ir::bounded_align(
            build_relation_layout(rel_col, false),
            build_relation_layout(0, true),
            pad_first_relation,
        )
    };
    Ir::group(body)
}

/// The shared math-atom sequencer (see [`lower_math_body`]). Ordinary math spacing
/// is *role-aware*: a single space is placed around every binary/relation operator
/// (`a+b` → `a + b`, `x=-b` → `x = -b`), reusing [`math_atom_role`]'s unary
/// detection so a `+`/`-` with no left operand stays glued to its operand
/// (`-x`, `2^{-5}`). Script-size lists suppress padding around punctuation
/// operators recursively, but retain spaces around control-word operators such as
/// `\in`; function application remains tight to its opener (`\Gamma(x)`). Both
/// modes preserve a fully glued slash but symmetrize a gap on either side, and
/// retain any separator required to avoid merging a control word with a following
/// letter. Plain operand juxtaposition keeps its authored spacing (a gap collapses
/// to one space, no gap stays tight). A `%`
/// comment forces a hard line break, and an authored own-line comment remains
/// own-line under every wrap mode so its association cannot change. A trailing
/// break (a comment at the body's end) is emitted rather than trimmed so the
/// caller's closing delimiter lands on its own line, while a trailing space is
/// dropped.
///
/// With `preserve_newlines` ([`MathWrap::Preserve`]) a trivia run spanning at
/// least one newline becomes a hard break instead of a space — the author's
/// line structure survives while in-line spacing is still normalized. A blank
/// run (≥2 newlines, invalid inside math anyway) also collapses to a single
/// break, and an edge run is still trimmed (the caller's delimiters own their
/// lines).
fn lower_math_seq(
    elements: impl Iterator<Item = SyntaxElement>,
    cx: LowerCtx<'_>,
    spacing: MathSpacing,
    preserve_newlines: bool,
) -> Ir {
    lower_math_seq_with_blocks(elements, cx, spacing, preserve_newlines, false)
}

fn lower_math_seq_with_blocks(
    elements: impl Iterator<Item = SyntaxElement>,
    cx: LowerCtx<'_>,
    spacing: MathSpacing,
    preserve_newlines: bool,
    break_blocks: bool,
) -> Ir {
    let mut out: Vec<Ir> = Vec::new();
    let mut blocks = MathBlockBoundaries::default();
    let mut started = false;
    // Start as a non-operand so a leading `+`/`-` reads as unary (see
    // [`collect_math_pieces`]).
    let mut prev_role = MathRole::Relation;
    let mut prev_class = MathClass::Rel;
    let mut prev_opener = false; // the previous atom ended with an opening delimiter
    let mut prev_delimiter_edge = false;
    let mut prev_sized_delimiter = None;
    let mut prev_spaced_slash = false;
    let mut prev_control_word_operator = false;
    let mut prev_ends_control_word = false;
    let mut prev_colon_relation_prefix = false;
    let mut prev_colon_needs_left_space = false;
    let mut prev_atom_ir_index = 0;
    let mut pending_space = false; // authored whitespace since the last atom
    let mut pending_break = false; // a comment forced a hard line break
    let mut pending_newline = false; // a preserved authored line break
    let mut pending_comment_own_line = false; // the next comment must retain its association
    let mut iter = strip_virtual_dtx_framing(elements, cx)
        .into_iter()
        .peekable();
    while let Some(el) = iter.next() {
        match el {
            // Tier 2 under `preserve_newlines` ([`MathWrap::Preserve`]) only: that
            // mode's contract is the author's line structure, and reproducing a
            // break as a break is preservation-only, hence its own fixed point.
            SyntaxElement::Token(t) if is_collapsible_trivia(t.kind()) => {
                let gap = consume_gap_widened(&t, &mut iter);
                if started {
                    pending_space = true;
                    pending_newline = preserve_newlines && gap.newlines > 0;
                    pending_comment_own_line = gap.newlines > 0;
                }
            }
            SyntaxElement::Token(t) if t.kind() == SyntaxKind::COMMENT => {
                if pending_break || pending_newline || pending_comment_own_line {
                    out.push(Ir::hard_line());
                } else if pending_space {
                    out.push(Ir::verbatim(" "));
                }
                out.push(Ir::verbatim(t.text()));
                started = true;
                pending_space = false;
                pending_newline = false;
                pending_comment_own_line = false;
                pending_break = true;
            }
            other => {
                // A top-level `\\` (a `LINE_BREAK` node) ends its line: emit it, then
                // force a hard break before the next atom. This is how a row stack
                // (`\[ a \\ b \]`, or an aligned body that fell back off the grid)
                // keeps each row on its own line. A `&` in a cell never reaches here,
                // so cells are unaffected.
                let is_line_break = matches!(
                    &other,
                    SyntaxElement::Node(n) if n.kind() == SyntaxKind::LINE_BREAK
                );
                for atom in lower_sized_math_atoms(other, &mut iter, cx, spacing) {
                    let block_break = break_blocks && blocks.before(&atom);
                    let completes_colon_relation = prev_colon_relation_prefix
                        && atom.starts_equals_relation
                        && !pending_space
                        && !pending_break
                        && !pending_newline;
                    if completes_colon_relation && prev_colon_needs_left_space {
                        out[prev_atom_ir_index] =
                            Ir::concat([Ir::verbatim(" "), out[prev_atom_ir_index].clone()]);
                    }
                    let role = math_atom_role(
                        atom.class,
                        prev_class,
                        prev_opener,
                        atom.postfix_left_limit,
                    );
                    let spaced_slash = atom.spaced_slash
                        || atom.slash
                            && (spacing == MathSpacing::Normal && !prev_role.is_operand_like()
                                || spacing == MathSpacing::Script && prev_control_word_operator);
                    let touches_spaced_slash = prev_spaced_slash || spaced_slash;
                    let delimiter_edge = matches!(
                        atom.delimiter,
                        Some(DelimiterRole::Open | DelimiterRole::Close)
                    );
                    let touches_delimiter = prev_delimiter_edge || delimiter_edge;
                    let spaced_operands = role == MathRole::Operand
                        && prev_role == MathRole::Operand
                        && pending_space
                        && !touches_delimiter;
                    let script_operator_spacing =
                        atom.control_word_operator || prev_control_word_operator;
                    let normal_spacing = spacing == MathSpacing::Normal
                        && role != MathRole::PostfixLeftLimit
                        && prev_role != MathRole::PostfixLeftLimit
                        && (role != MathRole::Operand
                            || prev_role != MathRole::Operand
                            || pending_space);
                    let separator_start = out.len();
                    if !started {
                        // no separator before the first atom
                    } else if pending_break || pending_newline || block_break {
                        out.push(Ir::hard_line());
                    } else if completes_colon_relation {
                        // The preceding colon and this scripted equals form one
                        // relation atom, so their boundary stays tight.
                    } else if prev_ends_control_word && atom.starts_control_word_letter {
                        // Tight script spacing must not merge `\in A` into the
                        // distinct control word `\inA`.
                        out.push(Ir::verbatim(" "));
                    } else if sized_delimiter_space(prev_sized_delimiter, atom.sized_delimiter)
                        || touches_spaced_slash
                        || normal_spacing
                        || spacing == MathSpacing::Script
                            && (script_operator_spacing || spaced_operands)
                    {
                        // Space around a binary/relation operator (either side), or a
                        // collapsed authored gap between ordinary operands. Script-size
                        // lists suppress incidental gaps around operators.
                        out.push(Ir::verbatim(" "));
                    }
                    prev_opener = atom.delimiter == Some(DelimiterRole::Open);
                    prev_delimiter_edge = delimiter_edge;
                    prev_sized_delimiter = atom.sized_delimiter;
                    prev_spaced_slash = spaced_slash;
                    prev_control_word_operator = atom.control_word_operator;
                    prev_ends_control_word = atom.ends_control_word;
                    prev_colon_relation_prefix = atom.colon_relation_prefix;
                    prev_colon_needs_left_space = atom.colon_relation_prefix
                        && spacing == MathSpacing::Normal
                        && started
                        && separator_start == out.len();
                    prev_atom_ir_index = out.len();
                    out.push(atom.ir);
                    started = true;
                    pending_space = false;
                    pending_newline = false;
                    pending_comment_own_line = false;
                    pending_break = is_line_break;
                    prev_role = role;
                    prev_class = atom.class;
                }
                if is_line_break {
                    blocks = MathBlockBoundaries::default();
                }
            }
        }
    }
    if pending_break {
        out.push(Ir::hard_line());
    }
    Ir::concat(out)
}

/// Lower one math atom (a non-trivia element of a math body).
fn lower_math_element(el: SyntaxElement, cx: LowerCtx<'_>, spacing: MathSpacing) -> Ir {
    match el {
        SyntaxElement::Node(n) => match n.kind() {
            SyntaxKind::SCRIPTED => lower_scripted(&n, cx, spacing),
            SyntaxKind::SUBSCRIPT | SyntaxKind::SUPERSCRIPT => lower_script(&n, cx),
            SyntaxKind::GROUP => lower_math_group(&n, cx, spacing),
            SyntaxKind::LEFT_RIGHT => lower_left_right(&n, cx, spacing),
            // Only signature-proven math slots recurse. A scanned redefinition
            // shadows the built-in with unknown domains and restores the
            // whole-command preservation fallback.
            SyntaxKind::COMMAND if command_has_math_arg(&n, cx) => {
                lower_command_with_math_spacing(&n, cx, spacing)
            }
            SyntaxKind::COMMAND => Ir::verbatim(n.text().to_string()),
            // A block environment is an indivisible math atom whose continuation
            // lines hang from its rendered start column. Generic indentation alone
            // would instead return to the enclosing math body's base indentation.
            SyntaxKind::ENVIRONMENT => Ir::align_current(lower_node(&n, cx)),
            // Anything unexpected: defer to generic lowering.
            _ => lower_node(&n, cx),
        },
        SyntaxElement::Token(t) => lower_loose_token(&t, cx),
    }
}

/// Lower a `{…}` math group: keep the braces, format the body in math mode. The
/// body sits one column past the `{` ([`Ir::align`]), so a multi-line body (a
/// nested block environment) hangs at its own start column — its `\end{…}` under
/// its `\begin{…}` — instead of at the `{`. A single-line body is unaffected.
fn lower_math_group(node: &SyntaxNode, cx: LowerCtx<'_>, spacing: MathSpacing) -> Ir {
    let inner = node
        .children_with_tokens()
        .filter(|el| !matches!(el.kind(), SyntaxKind::L_BRACE | SyntaxKind::R_BRACE));
    Ir::concat([
        Ir::verbatim("{"),
        Ir::align(1, lower_math_seq(inner, cx, spacing, false)),
        Ir::verbatim("}"),
    ])
}

/// Lower a signature-proven math argument while retaining its authored brace or
/// bracket delimiters. Only the body enters recursive math lowering.
fn lower_math_argument_group(node: &SyntaxNode, cx: LowerCtx<'_>, spacing: MathSpacing) -> Ir {
    let (open_kind, close_kind, open, close) = match node.kind() {
        SyntaxKind::GROUP => (SyntaxKind::L_BRACE, SyntaxKind::R_BRACE, "{", "}"),
        SyntaxKind::OPTIONAL => (SyntaxKind::L_BRACKET, SyntaxKind::R_BRACKET, "[", "]"),
        _ => return Ir::verbatim(node.text().to_string()),
    };
    let inner = node.children_with_tokens().filter(
        |element| !matches!(element.kind(), kind if kind == open_kind || kind == close_kind),
    );
    Ir::concat([
        Ir::verbatim(open),
        Ir::align(1, lower_math_seq(inner, cx, spacing, false)),
        Ir::verbatim(close),
    ])
}

/// Lower a `\left( … \right)` pair: the `\left`/`\right` control words and their
/// delimiter tokens are emitted verbatim, the inner `MATH` body is trimmed and
/// collapsed by [`lower_math_body`], and the trivia the parser kept between a
/// delimiter command and its delimiter (for losslessness) is dropped.
///
/// A non-empty body is set off by one space just inside each delimiter, so
/// `\left (  x + y  \right )` becomes `\left( x + y \right)`. That spacing is also
/// what keeps a control-word delimiter from gluing onto the body (`\left\langle x`
/// stays two tokens, never `\left\langlex`). An empty body stays tight
/// (`\left.\right.`).
///
/// The body sits just inside the opening delimiter, and its [`Ir::align`] width is
/// that flat opening width — so a multi-line body (a nested block environment)
/// hangs at its own start column, its `\end{…}` under its `\begin{…}` rather than
/// under the `\left`. A single-line body is unaffected.
fn lower_left_right(node: &SyntaxNode, cx: LowerCtx<'_>, spacing: MathSpacing) -> Ir {
    let mut parts: Vec<Ir> = Vec::new();
    // The flat width of the opening run (`\left(`, `\left\langle`, …): every
    // delimiter token seen before the body.
    let mut open_width = 0usize;
    let mut seen_body = false;
    for el in node.children_with_tokens() {
        match el {
            SyntaxElement::Token(t) if is_collapsible_trivia(t.kind()) => {}
            SyntaxElement::Node(n) if n.kind() == SyntaxKind::MATH => {
                seen_body = true;
                if !math_body_is_empty(&n) {
                    parts.push(Ir::align(
                        open_width + 1,
                        Ir::concat([
                            Ir::verbatim(" "),
                            lower_math_seq(n.children_with_tokens(), cx, spacing, false),
                            Ir::verbatim(" "),
                        ]),
                    ));
                }
            }
            SyntaxElement::Token(t) => {
                if !seen_body {
                    open_width += t.text().chars().count();
                }
                parts.push(Ir::verbatim(t.text()));
            }
            SyntaxElement::Node(n) => parts.push(lower_node(&n, cx)),
        }
    }
    Ir::concat(parts)
}

/// Whether a math body has no visible content (only whitespace/newlines), so a
/// `\left( … \right)` around it should not gain inner spaces.
fn math_body_is_empty(node: &SyntaxNode) -> bool {
    node.text().to_string().trim().is_empty()
}

/// Lower a `SCRIPTED` atom: the base then its `^`/`_` scripts, all tight (the
/// trivia the parser kept inside the node for losslessness is dropped here).
fn lower_scripted(node: &SyntaxNode, cx: LowerCtx<'_>, spacing: MathSpacing) -> Ir {
    Ir::concat(
        strip_virtual_dtx_framing(node.children_with_tokens(), cx)
            .into_iter()
            .filter_map(|el| match el {
                SyntaxElement::Token(t) if is_collapsible_trivia(t.kind()) => None,
                SyntaxElement::Node(n)
                    if matches!(n.kind(), SyntaxKind::SUBSCRIPT | SyntaxKind::SUPERSCRIPT) =>
                {
                    Some(lower_script(&n, cx))
                }
                other => Some(lower_math_element(other, cx, spacing)),
            }),
    )
}

/// Lower a `SUBSCRIPT`/`SUPERSCRIPT`: the `_`/`^` glued tightly to its argument.
/// Braces are kept verbatim — dropping redundant single-token braces (`x^{2}` ->
/// `x^2`) is a *content* rewrite, not layout, so it lives in the linter's
/// `redundant-script-braces` autofix, keeping this layout engine whitespace-only.
fn lower_script(node: &SyntaxNode, cx: LowerCtx<'_>) -> Ir {
    Ir::concat(
        strip_virtual_dtx_framing(node.children_with_tokens(), cx)
            .into_iter()
            .filter_map(|el| match el {
                SyntaxElement::Token(t) if is_collapsible_trivia(t.kind()) => None,
                SyntaxElement::Token(t)
                    if matches!(t.kind(), SyntaxKind::CARET | SyntaxKind::UNDERSCORE) =>
                {
                    Some(Ir::verbatim(t.text()))
                }
                other => Some(lower_math_element(other, cx, MathSpacing::Script)),
            }),
    )
}

/// True if `node` directly contains a `NEWLINE` token — **the unsafe
/// lone-newline predicate** (trivia-invariant layout, `formatter.md`).
///
/// Its two surviving readers — the `GROUP` arm's non-[`WrapMode::Reflow`] /
/// doc-margined branch in [`lower_node`] and [`lower_optional`]'s
/// non-`wraps_prose` / doc-margined early return — decide block-vs-inline for
/// a delimited group and are sanctioned **Tier 2**, on this fixed-point
/// argument: the block form ([`lower_bracketed`]) always ends with a newline
/// before its closing delimiter, so its output re-reads as multi-line and
/// takes the block form again, byte-stably (its body renderers —
/// [`ReflowKind::Statement`], the generic stream — carry their own fixed-point
/// contracts); an empty multi-line body collapses to the bare delimiters,
/// which re-read single-line and *stay* on the inline path; and the inline
/// path emits no newline inside the group, so a single-line group re-reads
/// single-line. Every layout either reader can emit re-reads to itself.
///
/// Both readers are reachable only under the Tier-2 wrap modes
/// (`Preserve`/`Stable`/`Sentence`/`Semantic`, modes defined by authored
/// breaks) or behind `doc_margin_opens_line` (a preserved column-0 predicate).
/// Under the default `Reflow`, [`lower_opaque_group`] and [`lower_optional`]
/// decide from width, content, and preserved predicates only and never
/// consult this. **Don't add a reader in a Tier-1 position.**
fn spans_multiple_lines(node: &SyntaxNode) -> bool {
    node.children_with_tokens()
        .filter_map(|e| e.into_token())
        .any(|t| t.kind() == SyntaxKind::NEWLINE)
}

/// True if `node` contains a `.dtx` documentation margin or docstrip guard at
/// any depth — a construct continuing across margined or guarded lines. Both
/// tokens are line-oriented column-0 facts (a `%` or `%<…>` recognized at line
/// start only), so any relayout that merges or re-indents their lines silently
/// turns them into ordinary comments on the next parse. Always false outside
/// the `.dtx` lexer mode (only it emits these kinds), which is why `cx.is_dtx`
/// short-circuits it — the same gate [`doc_margin_opens_line`] carries, and not
/// merely an optimization at this size. This is a *match guard* on most of
/// [`lower_node`]'s relayout arms, so it runs for every group, environment, and
/// math node; the walk is `O(subtree)`, and a nested construct re-walks at each
/// level. Ungated it was ~52% of the run on `{{{…}}}` nested 4000 deep, and made
/// lowering quadratic in nesting depth for every file, `.dtx` or not.
fn contains_doc_margin(node: &SyntaxNode, cx: LowerCtx<'_>) -> bool {
    if !cx.is_dtx || cx.in_dtx_doc_region {
        return false;
    }
    node.descendants_with_tokens()
        .filter_map(|e| e.into_token())
        .any(|t| matches!(t.kind(), SyntaxKind::DOC_MARGIN | SyntaxKind::GUARD))
}

/// Whether a forced-break block's interior lines all ride their own column-0
/// margins: the subtree spans at least one newline, every `NEWLINE` token is
/// immediately followed (still inside the node) by a `DOC_MARGIN` or `GUARD`,
/// and no other token embeds a newline (a multi-line `VERB`, a `\`-newline
/// control symbol). Every relayout arm of [`lower_node`] refuses a doc-margined
/// subtree, so such a block lowers through the byte-faithful stream and
/// reproduces its margins exactly when committed raw — only its first line
/// needs the canonical margin re-attached
/// ([`LineBuilder::push_margined_block`]). A node-final `NEWLINE` (nothing
/// follows it inside the node) fails conservatively: the check cannot see what
/// the next line carries.
fn block_rides_own_margins(node: &SyntaxNode) -> bool {
    let tokens: Vec<SyntaxToken> = node
        .descendants_with_tokens()
        .filter_map(|e| e.into_token())
        .collect();
    let mut saw_newline = false;
    for (i, token) in tokens.iter().enumerate() {
        if !token.text().contains('\n') {
            continue;
        }
        if token.kind() != SyntaxKind::NEWLINE {
            return false;
        }
        saw_newline = true;
        if !tokens
            .get(i + 1)
            .is_some_and(|next| matches!(next.kind(), SyntaxKind::DOC_MARGIN | SyntaxKind::GUARD))
        {
            return false;
        }
    }
    saw_newline
}

/// True if `node` directly contains a `VERBATIM_BODY` token — i.e. it is a
/// verbatim-like environment whose body must be emitted byte-for-byte.
fn has_verbatim_body(node: &SyntaxNode) -> bool {
    node.children_with_tokens()
        .filter_map(|e| e.into_token())
        .any(|t| t.kind() == SyntaxKind::VERBATIM_BODY)
}

/// A **normalized** trivia boundary: everything the layout is allowed to know
/// about the gap between two neighbouring elements.
///
/// What this type cannot say is the point of it. There is deliberately no
/// `Newline` variant — inline whitespace and a lone newline both arrive as
/// [`Self::Space`] — because the formatter converts freely between those two
/// spellings in *both* directions (`alpha\nbeta` → `alpha beta`, and a width wrap
/// back again). A layout decision keyed on which one the author wrote is
/// therefore a latent idempotency bug, and it is the root cause of the whole
/// K&R/Allman family (issues #71, #94, #96, #97). Discipline caught those one at
/// a time; deleting the information at the boundary is the enforcement, because a
/// rule cannot key on what it cannot see.
///
/// Glued-ness, blank-line presence, and comment presence *are* predicates the
/// formatter preserves (`P(fmt(x)) == P(x)`), so they keep their own variants and
/// layout may read them freely.
///
/// A Tier-2 site — one whose contract *is* the authored line structure — reads
/// the newline count through [`WideGap`] instead, and owes the written
/// fixed-point argument that goes with it.
#[derive(Clone, Debug, PartialEq, Eq)]
enum Gap {
    /// No trivia at all: the neighbours abut (`\ifmmode y\else`,
    /// `xmin=-5,xmax=5`). Breaking here would materialize a space token TeX
    /// contributes to the horizontal list — a typeset change the CST oracles
    /// cannot see, since whitespace is trivia to them and content to TeX — so it
    /// is a break opportunity only where a processor is proven to discard that
    /// space (see [`ContentKind::Keyval`]).
    Glued,
    /// Collapsible trivia carrying no blank line: inline whitespace, a lone
    /// newline, or any mix of the two. A break opportunity, and the one variant
    /// that must never be split back into its two spellings.
    ///
    /// `flat` is what a one-line rendering writes here, from [`Gap::flat`].
    Space { flat: String },
    /// Two or more newlines: an authored `\par`.
    Blank,
    /// The boundary ends at a `%` comment. The comment must terminate its line, so
    /// a break here is forced — and costs nothing, because the `%` already absorbs
    /// the line end.
    Comment,
}

impl Gap {
    /// A gap whose flat spelling is a single space: what every gap the layout is
    /// free to break renders as when it does not.
    fn space() -> Gap {
        Gap::Space {
            flat: " ".to_string(),
        }
    }

    /// Normalize a consumed trivia run. A run holding *any* newline flattens to one
    /// space — the only spelling a break reproduces — and its leading indentation is
    /// dropped, since the printer owns indentation and recreates it.
    fn from_run(newlines: usize, trailing_ws: String) -> Gap {
        match newlines {
            0 => Gap::Space { flat: trailing_ws },
            1 => Gap::space(),
            _ => Gap::Blank,
        }
    }

    /// What a *flat* rendering writes at this boundary: nothing where the author
    /// glued, the authored whitespace verbatim for a newline-free run, and a single
    /// space wherever the run carried a newline (blank line included).
    ///
    /// So a lone newline and a single authored space are indistinguishable here —
    /// that is the whole point — while a wider run (`\pgfpoint@oncoil{0    }`) still
    /// rides verbatim. That is not a leak of the unsafe predicate: every reader of
    /// `flat` emits it unchanged, so "the run was wider than one space" is a
    /// predicate they all preserve.
    fn flat(&self) -> &str {
        match self {
            Gap::Space { flat } => flat,
            Gap::Blank => " ",
            Gap::Glued | Gap::Comment => "",
        }
    }

    /// How a split point at this boundary renders in each mode: an [`Ir::Line`] (a
    /// space flat, a newline broken) wherever the author already wrote whitespace —
    /// the whitespace ↔ newline exchange that is TeX-identical anywhere, so it needs
    /// no permission — and an [`Ir::SoftLine`] (*nothing* flat) where they glued, so
    /// a fitting line stays byte-identical to the source and only the broken form
    /// materializes a space token.
    fn separator(&self) -> Ir {
        match self {
            Gap::Glued => Ir::soft_line(),
            _ => Ir::line(),
        }
    }
}

/// A [`Gap`] with the **unsafe** predicate — how many newlines the run spanned —
/// still readable.
///
/// Handed only to the Tier-2 sites whose contract *is* the authored line
/// structure: the byte-faithful stream ([`classify_trivia`]), the preserve-shaped
/// wrap modes, [`ReflowKind::Statement`]'s fallback content (a picture body's
/// `STATEMENT` nodes are structural, [`lower_statement`]), the expl3 fallback
/// statement, and the
/// command-only-line residue. Each owes a written fixed-point argument showing
/// every layout it can emit re-reads to itself; preservation-only rules have the
/// easy one (a hard line prints a newline, which re-reads as a newline, and is
/// kept again in place). Everything else takes [`Self::narrow`].
struct WideGap {
    gap: Gap,
    /// **Tier 2.** Reading this is reading the predicate the formatter does not
    /// preserve. Do not add a read without the fixed-point argument.
    newlines: usize,
}

impl WideGap {
    fn narrow(self) -> Gap {
        self.gap
    }
}

/// Consume the maximal run of collapsible trivia beginning at `first` and
/// normalize it to a [`Gap`] — the boundary every width-driven lowering takes,
/// and the reason none of them *can* key on a lone newline.
fn consume_gap(
    first: &SyntaxToken,
    iter: &mut Peekable<impl Iterator<Item = SyntaxElement>>,
) -> Gap {
    consume_gap_widened(first, iter).narrow()
}

/// The Tier-2 form of [`consume_gap`]: the same run, with the newline count left
/// readable. See [`WideGap`] for who may call this and what they owe.
///
/// The run's newline count is taken over the whole run; the whitespace following
/// the *last* newline is the run's leading indentation, which the printer owns, and
/// whitespace *before* a newline is trailing whitespace — both are dropped by
/// [`Gap::from_run`]. For a run with no newline the whole run is the gap's flat
/// spelling.
fn consume_gap_widened(
    first: &SyntaxToken,
    iter: &mut Peekable<impl Iterator<Item = SyntaxElement>>,
) -> WideGap {
    let mut newlines = 0;
    let mut trailing_ws = String::new();
    absorb(first, &mut newlines, &mut trailing_ws);
    loop {
        match iter.peek() {
            Some(SyntaxElement::Token(tok)) if is_collapsible_trivia(tok.kind()) => {}
            _ => break,
        }
        let token = match iter.next() {
            Some(SyntaxElement::Token(tok)) => tok,
            _ => unreachable!("peeked a collapsible trivia token"),
        };
        absorb(&token, &mut newlines, &mut trailing_ws);
    }
    WideGap {
        gap: Gap::from_run(newlines, trailing_ws),
        newlines,
    }
}

/// Consume the maximal run of collapsible trivia in `elements` beginning at
/// `*i`, advancing `*i` past it and returning the number of newlines it spans.
/// The index-based analogue of [`consume_gap_widened`], used by the two reflow
/// drivers, which need to look ahead past the run (the peekable iterator form
/// cannot).
///
/// **Tier 2**, like everything else that reads a newline count: both callers are
/// line-structure-preserving sites carrying their own fixed-point arguments (see
/// [`WideGap`]). Neither needs the flat spelling — a reflow re-derives spacing
/// from the fill — so this returns the count bare rather than a [`WideGap`].
fn consume_widened_gap_slice(elements: &[SyntaxElement], i: &mut usize) -> usize {
    let mut newlines = 0;
    while let Some(SyntaxElement::Token(tok)) = elements.get(*i) {
        if !is_collapsible_trivia(tok.kind()) {
            break;
        }
        if tok.kind() == SyntaxKind::NEWLINE {
            newlines += 1;
        }
        *i += 1;
    }
    newlines
}

/// Whether the physical source line beginning at `start` in `elements` consists
/// solely of non-inline command(s) and inline whitespace — the unit
/// [`reflow_elements`]'s *residual* rule keeps on its own line rather than
/// reflowing into its neighbours. The line runs until the next newline, comment,
/// or end of the stream; any non-trivia element that is not such a command (a
/// word, a control symbol, a group, math, a `\\`, a block, or an *inline* command
/// like `\citep`/`\ref` — see [`command_is_inline`]) disqualifies it. A line with
/// no command (e.g. an empty or comment-only line) is not a command line.
///
/// Residual: a curated block command ([`command_is_block`]) is intercepted by the
/// block-statement arm and gets its own line regardless of this test, so the
/// authored-break preservation decided here matters only for un-signatured and
/// scanned-definition commands (block-ness undecidable without meaning) and for
/// block commands glued to adjacent content.
///
/// That read of the lone-newline predicate is sanctioned **Tier 2**, on this
/// fixed-point argument: the rule is preservation-only — its entire effect is to
/// harden a gap that already holds a newline, never to write a break where none
/// was or to move content across one. So a newline in the output is either (a) a
/// break this rule kept, which re-reads as the same command-only line at the same
/// gap (nothing moves non-trivia across a hard line end, so command-only-ness is
/// itself preserved) and is kept again in place; or (b) a break the fill emitted,
/// which the next pass may *harden* when the printed line it opened or closed
/// happens to be command-only — a width wrap stranding an un-signatured command
/// alone. Hardening a break the greedy fill already chose is layout-neutral:
/// filling is first-fit, so the lines before the hardened break refill unchanged
/// and the fill after it restarts from column 0 exactly as the soft break did
/// (`reflow_command_stranded_by_width` pins this corner; the Tier-2 render modes'
/// own contracts cover their breaks the same way). Strict trivia invariance is
/// deliberately not claimed — preserving the authored break *is* the rule — so
/// `--checks trivia-strict` still reports these shapes
/// (`trivia_strict_check_fires_where_an_authored_break_is_preserved`), and the
/// [`Gap`] normalization carries this read as a [`WideGap`].
/// Retiring the rule outright would glue every authored `\mymacro`-on-its-own-line
/// into the fill — a policy change, not a fix.
///
/// A `CONDITIONAL` reaches this as a single non-`COMMAND` element and so
/// disqualifies its line, which is correct: [`lower_conditional`] owns where its
/// dividers fall, and the divider commands never appear in a reflow stream of
/// their own.
fn line_is_command_only(elements: &[SyntaxElement], start: usize, cx: LowerCtx<'_>) -> bool {
    let mut saw_command = false;
    for element in &elements[start..] {
        match element {
            SyntaxElement::Token(t) if t.kind() == SyntaxKind::NEWLINE => break,
            SyntaxElement::Token(t) if t.kind() == SyntaxKind::COMMENT => break,
            SyntaxElement::Token(t) if cx.is_dtx && t.kind() == SyntaxKind::DOC_MARGIN => {
                continue;
            }
            SyntaxElement::Token(t) if t.kind() == SyntaxKind::WHITESPACE => continue,
            SyntaxElement::Node(n)
                if n.kind() == SyntaxKind::COMMAND && !command_is_inline(n, cx) =>
            {
                saw_command = true
            }
            _ => return false,
        }
    }
    saw_command
}

/// Whether the element after `idx` leaves a break opportunity: the stream ends, or
/// the next element is collapsible trivia (whitespace, a newline) or a `COMMENT`
/// (a glued `%` is the line-continuation idiom and rides the committed line via
/// the `after_block` path). Anything else is glued to `elements[idx]`, and the
/// engine's rule is that adjacent non-whitespace elements form one unbreakable
/// atom — splitting there would materialize a space token TeX typesets. Keyed on
/// adjacency alone, a predicate the formatter preserves (it never converts
/// glued↔spaced), so the block-statement gate may read it.
fn next_is_separated(elements: &[SyntaxElement], idx: usize) -> bool {
    match elements.get(idx + 1) {
        None => true,
        Some(SyntaxElement::Token(t)) => matches!(
            t.kind(),
            SyntaxKind::WHITESPACE | SyntaxKind::NEWLINE | SyntaxKind::COMMENT
        ),
        Some(SyntaxElement::Node(_)) => false,
    }
}

/// Whether the next non-framing element in the paragraph is a `\label` command.
/// A `.dtx` margin is physical line framing, so a heading and label separated by
/// one remains structurally adjacent.
fn next_nontrivia_is_label(elements: &[SyntaxElement], idx: usize, cx: LowerCtx<'_>) -> bool {
    elements[idx + 1..]
        .iter()
        .find(|element| !is_section_label_framing(element, cx))
        .is_some_and(|element| {
            matches!(element, SyntaxElement::Node(node) if node.kind() == SyntaxKind::COMMAND && command_is_label(node))
        })
}

fn is_section_label_framing(element: &SyntaxElement, cx: LowerCtx<'_>) -> bool {
    is_collapsible_trivia_element(element)
        || matches!(element, SyntaxElement::Token(token) if cx.is_dtx && token.kind() == SyntaxKind::DOC_MARGIN)
}

/// Whether `elements[idx]` is in a label run immediately after a sectioning
/// command. The paragraph boundary supplies the outer structural gate; scanning
/// only within this flattened sibling stream cannot attach a label across
/// intervening prose, comments, or another construct. Glued seams are admitted
/// because the sectioning rule itself puts the heading and first label on separate
/// lines; accepting that source shape on pass one is required for idempotence.
fn label_follows_sectioning_run(elements: &[SyntaxElement], idx: usize, cx: LowerCtx<'_>) -> bool {
    for element in elements[..idx].iter().rev() {
        match element {
            element if is_section_label_framing(element, cx) => {}
            SyntaxElement::Node(node)
                if node.kind() == SyntaxKind::COMMAND && command_is_label(node) => {}
            SyntaxElement::Node(node)
                if node.kind() == SyntaxKind::COMMAND && command_is_sectioning(node, cx) =>
            {
                return node
                    .parent()
                    .is_some_and(|parent| parent.kind() == SyntaxKind::PARAGRAPH);
            }
            _ => return false,
        }
    }
    false
}

fn absorb(tok: &SyntaxToken, newlines: &mut usize, trailing_ws: &mut String) {
    if tok.kind() == SyntaxKind::NEWLINE {
        *newlines += 1;
        trailing_ws.clear();
    } else {
        trailing_ws.push_str(tok.text());
    }
}

/// Map a trivia run to a single IR primitive: no newline → the inline whitespace
/// (a genuine inter-word space) kept verbatim; one newline → a [`Ir::hard_line`];
/// two or more → a single [`Ir::empty_line`] (one blank line). A non-math grid
/// cell softens the one-newline case to [`Ir::line`] so continuation lines join
/// even when parser attachment nests the trivia inside a command. Whitespace
/// that followed the last newline is *indentation*, which the printer owns and
/// recreates, so it is dropped by [`Gap::from_run`] — keeping it would
/// double-indent on reformat.
///
/// This is normally the byte-faithful stream's boundary, and the reason it takes a
/// [`WideGap`]: reproducing the author's line structure is its entire contract, so
/// it reads the newline count by definition. **Tier 2**, with the trivial
/// fixed-point argument — a `hard_line` re-reads as one newline, an `empty_line`
/// re-reads as two, and verbatim whitespace re-reads as itself. The alignment-cell
/// exception inherits the grid's existing continuation rule: it emits the cell on
/// one line, which reparses without the softened newline and remains on that same
/// flat path; a blank line is never softened.
fn classify_trivia(gap: WideGap, soften_newline: bool) -> Ir {
    match gap.newlines {
        0 => Ir::verbatim(gap.gap.flat()),
        1 if soften_newline => Ir::line(),
        1 => Ir::hard_line(),
        _ => Ir::empty_line(),
    }
}

/// A break the indenter supplies itself and so trims from a body edge: a forced
/// line break, an inline whitespace chunk (indentation), or [`Ir::Nil`]. A
/// `VERBATIM_BODY` (force-break verbatim, or non-blank text) is never trimmable,
/// so protected content survives.
fn is_trimmable_break(ir: &Ir) -> bool {
    match ir {
        Ir::HardLine | Ir::PreservedLine | Ir::EmptyLine | Ir::Nil => true,
        Ir::Verbatim { text, force_break } => {
            !force_break && text.chars().all(|c| c == ' ' || c == '\t')
        }
        _ => false,
    }
}

/// Drop leading break/indentation IR from `ir`, reporting whether the trimmed-away
/// break carried a blank line (an [`Ir::empty_line`]). Recurses into a leading
/// `Concat` (the body's first break is often buried inside the first paragraph).
/// [`lower_environment`] uses the blank flag to re-supply one blank line at the
/// body's leading edge; callers that only want the trim use [`trim_leading_break`].
fn peel_leading_break(ir: Ir) -> (bool, Ir) {
    if is_trimmable_break(&ir) {
        return (matches!(ir, Ir::EmptyLine), Ir::Nil);
    }
    match ir {
        Ir::Concat(items) => {
            let mut v: Vec<Ir> = items.iter().cloned().collect();
            let mut blank = false;
            while !v.is_empty() {
                let (b, head) = peel_leading_break(v.remove(0));
                blank |= b;
                if matches!(head, Ir::Nil) {
                    continue;
                }
                v.insert(0, head);
                break;
            }
            (blank, Ir::concat(v))
        }
        other => (false, other),
    }
}

/// Mirror of [`peel_leading_break`] for the trailing edge.
fn peel_trailing_break(ir: Ir) -> (bool, Ir) {
    if is_trimmable_break(&ir) {
        return (matches!(ir, Ir::EmptyLine), Ir::Nil);
    }
    match ir {
        Ir::Concat(items) => {
            let mut v: Vec<Ir> = items.iter().cloned().collect();
            let mut blank = false;
            while let Some(last) = v.pop() {
                let (b, tail) = peel_trailing_break(last);
                blank |= b;
                if matches!(tail, Ir::Nil) {
                    continue;
                }
                v.push(tail);
                break;
            }
            (blank, Ir::concat(v))
        }
        other => (false, other),
    }
}

/// Drop leading break/indentation IR from `ir`, discarding the blank flag (see
/// [`peel_leading_break`]).
fn trim_leading_break(ir: Ir) -> Ir {
    peel_leading_break(ir).1
}

/// Drop trailing break/indentation IR from `ir`, discarding the blank flag (see
/// [`peel_trailing_break`]).
fn trim_trailing_break(ir: Ir) -> Ir {
    peel_trailing_break(ir).1
}

#[cfg(test)]
mod expl3_region_tests {
    use super::*;
    use crate::parser::parse;

    /// Run [`head_command_has_grouped_sibling_arg`] on the *innermost* `GROUP`
    /// descendant of `input` whose text contains `marker`.
    fn grouped_sibling_walk(input: &str, marker: &str) -> bool {
        let parsed = parse(input);
        assert!(parsed.errors.is_empty(), "test input should parse cleanly");
        let root = parsed.syntax();
        // Preorder puts an enclosing group before a nested one, so the last
        // match is the innermost.
        let group = root
            .descendants()
            .filter(|n| n.kind() == SyntaxKind::GROUP && n.text().to_string().contains(marker))
            .last()
            .expect("a group containing the marker");
        head_command_has_grouped_sibling_arg(&group)
    }

    #[test]
    fn grouped_sibling_walk_stops_at_the_statement_boundary() {
        // A grouped command in the *previous statement* must not suppress the
        // trailing-hang treatment for an unrelated `\bool_if:NF \l… {body}` —
        // free under the node-local read, since a node's children are one
        // statement by construction.
        let src = "\\ExplSyntaxOn\n\\tl_set:Nn \\l_x { v }\n\\bool_if:NF \\l_bool { body }\n\\ExplSyntaxOff\n";
        assert!(!grouped_sibling_walk(src, "body"));

        // Within one call the earlier grouped argument still counts: arity
        // attachment puts a recognized `\prop_get:NnNTF` call's `{#2}` and its
        // branches on one head node, so the multi-argument shape whose branch
        // list the hang path already lays out stably is read off the node.
        let src = "\\ExplSyntaxOn\n\\prop_get:NnNTF \\g_prop {#2} \\l_tl { branch } { f }\n\\ExplSyntaxOff\n";
        assert!(grouped_sibling_walk(src, "branch"));

        // An *aborted* call (the `F` branch is missing, so the five-slot spec
        // never resolves) keeps its groups on the small trailing command. An
        // unresolvable call is not the multi-argument shape this suppresses.
        let src =
            "\\ExplSyntaxOn\n\\prop_get:NnNTF \\g_prop {#2} \\l_tl { branch }\n\\ExplSyntaxOff\n";
        assert!(!grouped_sibling_walk(src, "branch"));
    }

    #[test]
    fn grouped_sibling_walk_matches_the_body_stream_segmentation() {
        // Inside a brace body, a preceding *statement's* grouped call must not
        // suppress the hang for the next statement's `\bool_if:NF … {body}` —
        // under the node-local read the earlier call's group belongs to that
        // call's own node, never to `{body}`'s owner.
        let src = "\\ExplSyntaxOn\n\\use:n { \\tl_set:Nn \\l_x { v } \\bool_if:NF \\l_b { body } }\n\\ExplSyntaxOff\n";
        assert!(!grouped_sibling_walk(src, "body"));
    }

    #[test]
    fn grouped_sibling_walk_ignores_out_of_region_prefix() {
        // The out-of-region `\emph{y}` sharing the authored line must not
        // read as an earlier grouped argument of the `\tl_put_right:Ne`
        // statement — its group belongs to `\emph`'s node, not the owner's.
        let src = "x \\emph{y} \\ExplSyntaxOn \\tl_put_right:Ne \\l_t { body } \\ExplSyntaxOff\n";
        assert!(!grouped_sibling_walk(src, "body"));
    }

    /// The expl3 regions of `input`, as `(start, end)` byte pairs.
    fn regions(input: &str) -> Vec<(usize, usize)> {
        let parsed = parse(input);
        assert!(parsed.errors.is_empty(), "test input should parse cleanly");
        expl3_regions(&parsed.syntax())
            .into_iter()
            .map(|r| (r.start().into(), r.end().into()))
            .collect()
    }

    #[test]
    fn on_off_pair_spans_both_toggles() {
        let input = r"a \ExplSyntaxOn b \ExplSyntaxOff c";
        let start = input.find(r"\ExplSyntaxOn").unwrap();
        let end = input.find(r"\ExplSyntaxOff").unwrap() + r"\ExplSyntaxOff".len();
        assert_eq!(regions(input), vec![(start, end)]);
    }

    #[test]
    fn unclosed_region_runs_to_eof() {
        let input = r"x \ExplSyntaxOn y z";
        let start = input.find(r"\ExplSyntaxOn").unwrap();
        assert_eq!(regions(input), vec![(start, input.len())]);
    }

    #[test]
    fn provides_expl_opens_to_eof() {
        let input = "\\ProvidesExplPackage\n\\cs_new:N \\foo:";
        assert_eq!(regions(input), vec![(0, input.len())]);
    }

    #[test]
    fn definee_provides_does_not_open_region() {
        // `\ProvidesExplPackage` as the definee of `\protected\def` is tokenized,
        // never executed, so it opens no formatter-owned region (issue #69).
        let input = "\\protected\\def\\ProvidesExplPackage{\\ProvidesPackage{demo}}\ntext";
        assert!(regions(input).is_empty());
    }

    #[test]
    fn definee_off_does_not_close_a_real_region() {
        // A gated-out `\ExplSyntaxOff` in definee position must not close the open
        // region — the gate skips it, so the region still runs to EOF.
        let input = "\\ExplSyntaxOn a \\let\\ExplSyntaxOff\\relax b";
        assert_eq!(regions(input), vec![(0, input.len())]);
    }

    #[test]
    fn stored_toggle_in_group_does_not_open_region() {
        // An `\ExplSyntaxOn` stored inside a definition body / attached group is
        // never executed at load, so it opens no region (issue #69).
        let input = "\\def\\store{\\ExplSyntaxOn \\foo:n {x}}\ntext";
        assert!(regions(input).is_empty());
    }

    #[test]
    fn top_level_provides_after_gated_definee_still_opens() {
        // The gate rejects only the false positives: a genuine top-level
        // `\ProvidesExplPackage` still opens a region even when a definee one
        // precedes it.
        let input = "\\def\\x{\\ExplSyntaxOn}\n\\ProvidesExplPackage\n\\cs_new:N \\foo:";
        let start = input.rfind("\\ProvidesExplPackage").unwrap();
        assert_eq!(regions(input), vec![(start, input.len())]);
    }

    #[test]
    fn stray_off_is_ignored() {
        assert!(regions(r"a \ExplSyntaxOff b").is_empty());
    }

    #[test]
    fn redundant_inner_on_does_not_restart() {
        let input = r"\ExplSyntaxOn a \ExplSyntaxOn b \ExplSyntaxOff";
        let end = input.find(r"\ExplSyntaxOff").unwrap() + r"\ExplSyntaxOff".len();
        assert_eq!(regions(input), vec![(0, end)]);
    }

    #[test]
    fn toggle_inside_verb_is_not_a_region() {
        // `\ExplSyntaxOn` inside a `\verb` argument lexes as a `VERB` token, never a
        // `CONTROL_WORD`, so it must not open a region (mirrors the lexer).
        assert!(regions(r"\verb|\ExplSyntaxOn| text").is_empty());
    }

    #[test]
    fn toggle_inside_comment_is_not_a_region() {
        assert!(regions("% \\ExplSyntaxOn\ntext").is_empty());
    }

    /// The expl3 regions of `input` parsed as a `.dtx`, as `(start, end)` pairs.
    fn regions_dtx(input: &str) -> Vec<(usize, usize)> {
        let config = crate::parser::LexConfig {
            flavor: LatexFlavor::Document,
            dtx: true,
        };
        let parsed = parse_with_flavor(input, config);
        assert!(parsed.errors.is_empty(), "test input should parse cleanly");
        expl3_regions(&parsed.syntax())
            .into_iter()
            .map(|r| (r.start().into(), r.end().into()))
            .collect()
    }

    #[test]
    fn dtx_region_owns_only_macrocode_bodies() {
        // The unmargined `␣%` line between the chunks is documentation, not code
        // (issue #58): the region intersects with the chunk bodies, so neither it
        // nor the margined doc line is formatter-owned.
        let input = "%    \\begin{macrocode}\n\
                     \\ExplSyntaxOn\n\
                     %    \\end{macrocode}\n\
                     \x20%\n\
                     % doc\n\
                     %    \\begin{macrocode}\n\
                     \\foo\n\
                     %    \\end{macrocode}\n";
        let first_frame = input.find("%    \\end{macrocode}").unwrap();
        // The `\begin` frame line's hole spans through its newline, so the second
        // region opens at the body's first code token.
        let second_body = input.find("\\foo").unwrap();
        let second_frame = input.rfind("%    \\end{macrocode}").unwrap();
        assert_eq!(
            regions_dtx(input),
            vec![
                (input.find("\\ExplSyntaxOn").unwrap(), first_frame),
                (second_body, second_frame),
            ]
        );
    }
}

#[cfg(test)]
mod gap_tests {
    use super::*;
    use crate::parser::parse;

    /// The [`Gap`] the boundary reads from the first collapsible-trivia run in
    /// `input`, taken through the same `consume_gap` every width-driven lowering
    /// uses. `descendants` is preorder, so the outermost node holding a trivia run
    /// answers first.
    fn first_gap(input: &str) -> Gap {
        let parsed = parse(input);
        assert!(parsed.errors.is_empty(), "test input should parse cleanly");
        for node in parsed.syntax().descendants() {
            let mut iter = node.children_with_tokens().peekable();
            while let Some(element) = iter.next() {
                let SyntaxElement::Token(token) = element else {
                    continue;
                };
                if is_collapsible_trivia(token.kind()) {
                    return consume_gap(&token, &mut iter);
                }
            }
        }
        panic!("no trivia run in {input:?}");
    }

    /// The property the whole normalization exists for: the two spellings the
    /// formatter converts between are one value at the boundary, so no rule taking
    /// a narrow [`Gap`] can tell them apart. This is the mechanical guard the
    /// K&R/Allman family (issues #71, #94, #96, #97) never had — each of those was
    /// one decision keying on exactly this difference.
    #[test]
    fn a_lone_newline_and_a_space_are_the_same_gap() {
        assert_eq!(first_gap("\\foo{a b}"), first_gap("\\foo{a\nb}"));
        assert_eq!(first_gap("\\foo{a\nb}"), Gap::space());
        // Indentation after the newline is the printer's to recreate, so it is
        // dropped rather than becoming part of the gap.
        assert_eq!(first_gap("\\foo{a\n    b}"), Gap::space());
        assert_eq!(first_gap("\\foo{a \n b}"), Gap::space());
    }

    /// Blank-line presence *is* preserved, so it keeps its own variant — and
    /// flattens to a single space, the only spelling a one-line rendering can
    /// write.
    #[test]
    fn a_blank_line_stays_visible() {
        assert_eq!(first_gap("\\foo{a\n\nb}"), Gap::Blank);
        assert_eq!(first_gap("\\foo{a\n\n\nb}"), Gap::Blank);
        assert_eq!(Gap::Blank.flat(), " ");
    }

    /// A run wider than one space rides verbatim wherever `flat` is read, so
    /// distinguishing it is not a read of the unsafe predicate — every reader
    /// preserves it. It is still not confusable with a break.
    #[test]
    fn a_wide_run_rides_verbatim() {
        assert_eq!(
            first_gap("\\foo{a    b}"),
            Gap::Space {
                flat: "    ".to_string()
            }
        );
        assert_ne!(first_gap("\\foo{a    b}"), first_gap("\\foo{a\nb}"));
    }

    /// The split-point rendering the folded-in `DividerGap`/`KeyBreak` prototypes
    /// agreed on: a glued junction renders as *nothing* flat, so a fitting line
    /// stays byte-identical to the source and only the broken form materializes a
    /// space token TeX would typeset.
    #[test]
    fn a_glued_junction_separates_without_a_flat_space() {
        assert!(matches!(Gap::Glued.separator(), Ir::SoftLine));
        assert_eq!(Gap::Glued.flat(), "");
        assert!(matches!(Gap::space().separator(), Ir::Line));
        assert!(matches!(Gap::Blank.separator(), Ir::Line));
        assert!(matches!(Gap::Comment.separator(), Ir::Line));
    }
}