xberg 1.0.3

High-performance document intelligence library for Rust. Extract text, metadata, and structured data from PDFs, Office documents, images, and 98 formats and 306 programming languages via tree-sitter code intelligence with async/sync APIs.
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
//! Heading classification for paragraphs using font-size clustering.

use super::constants::{
    MAX_BOLD_HEADING_WORD_COUNT, MAX_HEADING_DISTANCE_MULTIPLIER, MAX_HEADING_WORD_COUNT, MIN_BLOCKS_FOR_FONT_HEADING,
    MIN_HEADING_FONT_GAP, MIN_HEADING_FONT_RATIO,
};
use super::regions::{looks_like_bare_url, looks_like_figure_label};
use super::types::{LayoutHintClass, PdfParagraph};

const SAL_DIRECTION_PREFIXES: [&str; 6] = [
    "__deref__inout",
    "__deref__out",
    "__deref__in",
    "__inout",
    "__out",
    "__in",
];
const SAL_DIRECTION_MODIFIERS: [&str; 7] = ["opt", "ecount", "bcount", "full", "part", "z", "nz"];
const CHANGELOG_VERSION_MAX_LEFT_OFFSET_RATIO: f32 = 0.75;
const CHANGELOG_VERSION_MAX_VERTICAL_GAP_RATIO: f32 = 2.0;
const CHANGELOG_VERSION_MAX_VERTICAL_OVERLAP_RATIO: f32 = 0.25;
const CHANGELOG_COMBINED_MIN_HEIGHT_RATIO: f32 = 1.5;
const CHANGELOG_COMBINED_MIN_BASELINE_GAP_RATIO: f32 = 0.5;
type ParagraphBbox = (f32, f32, f32, f32);

/// Demote structure-tree heading tags attached to standalone SAL annotations.
///
/// Tagged API references sometimes expose direction annotations such as `__in`
/// as headings. These are parameter metadata, not document structure. Keep the
/// guard deliberately narrow, and retain headings backed by a layout heading
/// class or code-block classification.
pub(super) fn demote_structure_annotation_headings(paragraphs: &mut [PdfParagraph]) {
    for para in paragraphs {
        if para.heading_level.is_none()
            || para.is_code_block
            || matches!(
                para.layout_class,
                Some(LayoutHintClass::Title | LayoutHintClass::SectionHeader)
            )
        {
            continue;
        }

        let text = paragraph_plain_text(para);
        if is_sal_direction_annotation(text.trim()) {
            para.heading_level = None;
        }
    }
}

fn is_sal_direction_annotation(text: &str) -> bool {
    let base = if let Some((base, arguments)) = text.split_once('(') {
        let Some(arguments) = arguments.strip_suffix(')') else {
            return false;
        };
        if arguments.is_empty()
            || !arguments
                .chars()
                .all(|c| c.is_ascii_alphanumeric() || matches!(c, '_' | ','))
        {
            return false;
        }
        base
    } else {
        text
    };

    SAL_DIRECTION_PREFIXES.iter().any(|prefix| {
        let Some(suffix) = base.strip_prefix(prefix) else {
            return false;
        };
        suffix.is_empty()
            || suffix.strip_prefix('_').is_some_and(|modifiers| {
                !modifiers.is_empty()
                    && modifiers
                        .split('_')
                        .all(|modifier| SAL_DIRECTION_MODIFIERS.contains(&modifier))
            })
    })
}

/// Classify paragraphs as headings or body using the global heading map and bold heuristic.
pub(super) fn classify_paragraphs(paragraphs: &mut [PdfParagraph], heading_map: &[(f32, Option<u8>)]) {
    tracing::debug!(
        paragraph_count = paragraphs.len(),
        heading_clusters = heading_map.len(),
        body_font = heading_map
            .iter()
            .find(|(_, l)| l.is_none())
            .map(|(c, _)| *c)
            .unwrap_or(0.0),
        "classify_paragraphs: start"
    );
    let gap_info = precompute_gap_info(heading_map);
    let body_font_size = heading_map
        .iter()
        .find(|(_, level)| level.is_none())
        .map(|(centroid, _)| *centroid)
        .unwrap_or(0.0);
    for para in paragraphs.iter_mut() {
        let word_count = para.word_count;

        let layout_says_text = para.layout_class == Some(super::types::LayoutHintClass::Text);
        let heading_level = find_heading_level(para.dominant_font_size, heading_map, &gap_info);
        let heading_level = if layout_says_text {
            if para.is_bold && heading_level.is_some() {
                heading_level
            } else {
                None
            }
        } else {
            heading_level
        };

        let para_text: String = if !para.text.is_empty() {
            para.text.clone()
        } else {
            para.lines
                .iter()
                .flat_map(|l| l.segments.iter())
                .map(|s| s.text.as_str())
                .collect::<Vec<_>>()
                .join(" ")
        };

        if let Some(level) = heading_level
            && word_count <= MAX_HEADING_WORD_COUNT
            && !super::layout_classify::is_separator_text(&para_text)
            && !looks_like_bare_url(&para_text)
        {
            para.heading_level = Some(level);
            continue;
        }

        let is_italic = if !para.text.is_empty() {
            para.lines
                .first()
                .and_then(|l| l.segments.first())
                .is_some_and(|s| s.is_italic)
        } else {
            !para.lines.is_empty() && para.lines.iter().all(|l| l.segments.iter().all(|s| s.is_italic))
        };
        let layout_text_overridable = if layout_says_text {
            body_font_size > 0.0 && para.dominant_font_size > body_font_size + 1.0
        } else {
            true
        };
        if (para.is_bold || is_italic)
            && !para.is_list_item
            && layout_text_overridable
            && word_count <= MAX_BOLD_HEADING_WORD_COUNT
        {
            let t = para_text.trim();
            let italic_ok = if is_italic && !para.is_bold {
                !t.contains('@') && !t.contains(',') && t.chars().next().is_some_and(|c| c.is_uppercase())
            } else {
                true
            };
            let too_short_at_body =
                word_count <= 2 && body_font_size > 0.0 && para.dominant_font_size <= body_font_size + 0.5;
            let period_ok = !ends_with_sentence_period(t) || is_section_pattern(t);
            let colon_ok = !t.ends_with(':') || is_all_caps_text(t);
            if italic_ok
                && !too_short_at_body
                && period_ok
                && colon_ok
                && !looks_like_figure_label(t)
                && !looks_like_bare_url(t)
                && !super::layout_classify::is_separator_text(t)
            {
                let level = infer_bold_heading_level(para.dominant_font_size, body_font_size, t);
                para.heading_level = Some(level);
            }
        }

        if para.heading_level.is_none()
            && !para.is_list_item
            && !para.is_code_block
            && (2..=MAX_HEADING_WORD_COUNT).contains(&word_count)
        {
            let t = para_text.trim();
            if starts_with_section_number(t)
                && !t.ends_with(':')
                && !looks_like_figure_label(t)
                && !super::layout_classify::is_separator_text(t)
            {
                let at_or_above_body = body_font_size <= 0.0 || para.dominant_font_size >= body_font_size - 0.5;
                let layout_ok = !layout_says_text
                    || (body_font_size > 0.0 && para.dominant_font_size > body_font_size + 1.0)
                    || para.is_bold;
                if at_or_above_body && layout_ok {
                    let level = infer_section_level(t);
                    para.heading_level = Some(level);
                }
            }
        }

        if para.is_code_block {
            para.heading_level = None;
        }

        if para.heading_level.is_none()
            && !para.is_list_item
            && !para.is_code_block
            && !para.is_formula
            && word_count <= 30
        {
            let math_char_count = para_text.chars().filter(|c| is_math_character(*c)).count();
            let total_chars = para_text.chars().count();
            if total_chars > 0 && (math_char_count >= 3 || (math_char_count as f64 / total_chars as f64) >= 0.15) {
                para.is_formula = true;
                para.heading_level = None;
            }
        }

        if para.heading_level.is_none()
            && !para.is_list_item
            && !para.is_code_block
            && !para.is_page_furniture
            && body_font_size > 0.0
            && para.dominant_font_size >= body_font_size + 1.0
        {
            let rescue_text = para_text.trim();
            let rescue_wc = rescue_text.split_whitespace().count();
            let rescue_colon_ok = !rescue_text.ends_with(':') || is_all_caps_text(rescue_text);
            if (1..=8).contains(&rescue_wc)
                && !ends_with_sentence_period(rescue_text)
                && rescue_colon_ok
                && !looks_like_figure_label(rescue_text)
                && !super::layout_classify::is_separator_text(rescue_text)
                && !starts_with_lowercase_or_continuation(rescue_text)
            {
                let ratio = para.dominant_font_size / body_font_size;
                let rescue_level = if ratio > 1.6 {
                    1
                } else if ratio > 1.3 {
                    2
                } else {
                    3
                };
                para.heading_level = Some(rescue_level);
            }
        }
    }

    demote_continuation_headings(paragraphs);

    for para in paragraphs.iter_mut() {
        if para.heading_level.is_some()
            || para.is_list_item
            || para.is_code_block
            || para.is_formula
            || para.is_page_furniture
        {
            continue;
        }

        let text: String = if !para.text.is_empty() {
            para.text.clone()
        } else {
            para.lines
                .iter()
                .flat_map(|l| l.segments.iter())
                .map(|s| s.text.as_str())
                .collect::<Vec<_>>()
                .join(" ")
        };
        let t = text.trim();
        let wc = t.split_whitespace().count();
        if wc == 0 || wc > 15 {
            continue;
        }

        if wc == 1 && !t.ends_with(':') {
            continue;
        }

        if !is_all_caps_text(t) {
            continue;
        }

        if looks_like_figure_label(t) || super::layout_classify::is_separator_text(t) {
            continue;
        }

        let level = if body_font_size > 0.0 {
            let ratio = para.dominant_font_size / body_font_size;
            if ratio > 1.4 {
                1
            } else if ratio > 1.2 || wc <= 5 {
                2
            } else {
                3
            }
        } else if wc <= 5 {
            2
        } else {
            3
        };
        para.heading_level = Some(level);
    }

    detect_indentation_based_lists(paragraphs);

    detect_monospace_code_blocks(paragraphs);
}

/// W2.B: Detect indentation-based lists (unordered by indent level).
///
/// Paragraphs that are horizontally shifted right relative to surrounding paragraphs
/// are often list items. This pass identifies them by:
/// 1. Computing the modal left X across all paragraphs on each page
/// 2. Finding paragraphs indented >= 12 points right of the modal
/// 3. Requiring they follow or precede another short paragraph at the same indent
fn detect_indentation_based_lists(paragraphs: &mut [PdfParagraph]) {
    if paragraphs.len() < 2 {
        return;
    }

    const INDENT_THRESHOLD: f32 = 12.0;

    let mut left_positions: Vec<f32> = paragraphs
        .iter()
        .filter_map(|p| p.block_bbox.map(|(left, _, _, _)| left))
        .collect();

    if left_positions.is_empty() {
        return;
    }

    left_positions.sort_by(|a, b| a.total_cmp(b));
    let modal_left = {
        let mut freq_map: std::collections::HashMap<i32, usize> = std::collections::HashMap::new();
        for &pos in &left_positions {
            let bucket = (pos * 10.0) as i32;
            *freq_map.entry(bucket).or_default() += 1;
        }
        let (bucket, _) = freq_map.into_iter().max_by_key(|(_, count)| *count).unwrap_or((0, 0));
        bucket as f32 / 10.0
    };

    for i in 0..paragraphs.len() {
        let para = &paragraphs[i];

        if para.is_list_item || para.is_code_block || para.is_formula || para.heading_level.is_some() {
            continue;
        }

        let left_x = para.block_bbox.map(|(left, _, _, _)| left).unwrap_or(0.0);
        let is_indented = left_x >= modal_left + INDENT_THRESHOLD;

        if !is_indented {
            continue;
        }

        let para_text = if !para.text.is_empty() {
            para.text.clone()
        } else {
            para.lines
                .iter()
                .flat_map(|l| l.segments.iter())
                .map(|s| s.text.as_str())
                .collect::<Vec<_>>()
                .join(" ")
        };
        let word_count = para_text.split_whitespace().count();

        if super::pipeline::is_probable_author_byline(&para_text) || is_numeric_prose_continuation(&para_text) {
            continue;
        }

        let has_context = if i > 0 {
            let prev_text = if !paragraphs[i - 1].text.is_empty() {
                paragraphs[i - 1].text.clone()
            } else {
                paragraphs[i - 1]
                    .lines
                    .iter()
                    .flat_map(|l| l.segments.iter())
                    .map(|s| s.text.as_str())
                    .collect::<Vec<_>>()
                    .join(" ")
            };
            let prev_wc = prev_text.split_whitespace().count();
            prev_wc <= 20
        } else {
            false
        };

        let followed_by_context = if i + 1 < paragraphs.len() {
            let next_text = if !paragraphs[i + 1].text.is_empty() {
                paragraphs[i + 1].text.clone()
            } else {
                paragraphs[i + 1]
                    .lines
                    .iter()
                    .flat_map(|l| l.segments.iter())
                    .map(|s| s.text.as_str())
                    .collect::<Vec<_>>()
                    .join(" ")
            };
            let next_wc = next_text.split_whitespace().count();
            next_wc <= 20
        } else {
            false
        };

        if (word_count <= 30) && (has_context || followed_by_context) {
            paragraphs[i].is_list_item = true;
            paragraphs[i].layout_class = Some(LayoutHintClass::ListItem);
        }
    }
}

fn is_numeric_prose_continuation(text: &str) -> bool {
    let mut words = text.split_whitespace();
    let Some(first) = words.next() else {
        return false;
    };
    if first.is_empty() || !first.chars().all(|c| c.is_ascii_digit()) {
        return false;
    }
    words
        .next()
        .is_none_or(|word| word.chars().next().is_some_and(char::is_lowercase))
}

/// W2.C: Detect monospace code-block sequences.
///
/// Multiple consecutive paragraphs where all lines are monospace should be
/// marked as code blocks. This handles code snippets that don't have explicit
/// code block markers.
fn detect_monospace_code_blocks(paragraphs: &mut [PdfParagraph]) {
    if paragraphs.len() < 2 {
        return;
    }

    let mut i = 0;
    while i < paragraphs.len() {
        let para = &paragraphs[i];

        if para.is_code_block || para.is_list_item || para.is_formula || para.heading_level.is_some() {
            i += 1;
            continue;
        }

        let is_all_monospace = !para.lines.is_empty() && para.lines.iter().all(|l| l.is_monospace);

        if !is_all_monospace {
            i += 1;
            continue;
        }

        if i + 1 < paragraphs.len() {
            let next_para = &paragraphs[i + 1];
            let next_is_monospace = !next_para.lines.is_empty()
                && next_para.lines.iter().all(|l| l.is_monospace)
                && !next_para.is_list_item
                && !next_para.is_formula
                && next_para.heading_level.is_none();

            if next_is_monospace {
                paragraphs[i].is_code_block = true;
                paragraphs[i].layout_class = Some(LayoutHintClass::Code);
                paragraphs[i + 1].is_code_block = true;
                paragraphs[i + 1].layout_class = Some(LayoutHintClass::Code);

                let mut j = i + 2;
                while j < paragraphs.len() {
                    let para_j = &paragraphs[j];
                    let is_monospace_j = !para_j.lines.is_empty()
                        && para_j.lines.iter().all(|l| l.is_monospace)
                        && !para_j.is_list_item
                        && !para_j.is_formula
                        && para_j.heading_level.is_none();

                    if is_monospace_j {
                        paragraphs[j].is_code_block = true;
                        paragraphs[j].layout_class = Some(LayoutHintClass::Code);
                        j += 1;
                    } else {
                        break;
                    }
                }

                i = j;
                continue;
            }
        }

        i += 1;
    }
}

/// Check if text starts with a lowercase letter or a common sentence-continuation word.
///
/// Mid-sentence fragments from column breaks often start with lowercase words or
/// common continuation words. Real headings typically start with uppercase letters,
/// numbers, or section markers.
fn starts_with_lowercase_or_continuation(text: &str) -> bool {
    let first_char = text.chars().next();
    if first_char.is_some_and(|c| c.is_lowercase()) {
        return true;
    }

    let first_word = text.split_whitespace().next().unwrap_or("");
    let lower = first_word.to_lowercase();
    matches!(
        lower.as_str(),
        "is" | "are"
            | "was"
            | "were"
            | "to"
            | "of"
            | "in"
            | "on"
            | "for"
            | "and"
            | "or"
            | "but"
            | "the"
            | "a"
            | "an"
            | "that"
            | "which"
            | "with"
    )
}

/// Check if text is ALL-CAPS (>80% of alphabetic characters are uppercase).
///
/// Used to identify label-headings like "AGENCY:", "DEPARTMENT OF TRANSPORTATION",
/// "SUMMARY:" that are common in government and legal documents.
fn is_all_caps_text(text: &str) -> bool {
    let alpha_chars: Vec<char> = text.chars().filter(|c| c.is_alphabetic()).collect();
    if alpha_chars.len() < 2 {
        return false;
    }
    let upper_count = alpha_chars.iter().filter(|c| c.is_uppercase()).count();
    (upper_count as f64 / alpha_chars.len() as f64) > 0.8
}

/// Demote headings that follow paragraphs ending without sentence-terminating punctuation.
///
/// When a paragraph doesn't end with `.`, `?`, `!`, `:`, or `;`, the next paragraph
/// is likely a continuation (e.g., from a multi-column layout split). Headings
/// promoted by the rescue pass in this position are demoted back to body text.
///
/// Only demotes headings that were NOT confirmed by font-size clustering (i.e.,
/// headings at levels that could have come from the rescue pass).
fn demote_continuation_headings(paragraphs: &mut [PdfParagraph]) {
    if paragraphs.len() < 2 {
        return;
    }

    for i in 1..paragraphs.len() {
        if paragraphs[i].heading_level.is_none() {
            continue;
        }

        let prev = &paragraphs[i - 1];
        if prev.heading_level.is_some()
            || prev.is_list_item
            || prev.is_code_block
            || prev.is_formula
            || prev.is_page_furniture
        {
            continue;
        }

        let prev_text = if !prev.text.is_empty() {
            prev.text.clone()
        } else {
            prev.lines
                .iter()
                .flat_map(|l| l.segments.iter())
                .map(|s| s.text.as_str())
                .collect::<Vec<_>>()
                .join(" ")
        };
        let prev_trimmed = prev_text.trim_end();
        let ends_with_terminator = matches!(prev_trimmed.chars().last(), Some('.' | '?' | '!' | ':' | ';'));

        if !ends_with_terminator && !prev_trimmed.is_empty() {
            paragraphs[i].heading_level = None;
        }
    }
}

/// Find the heading level for a given font size by matching against the cluster centroids.
pub(super) fn find_heading_level(font_size: f32, heading_map: &[(f32, Option<u8>)], gap_info: &GapInfo) -> Option<u8> {
    if heading_map.is_empty() {
        return None;
    }
    if heading_map.len() == 1 {
        return heading_map[0].1;
    }

    let mut best_distance = f32::INFINITY;
    let mut best_level: Option<u8> = None;
    for &(centroid, level) in heading_map {
        let dist = (font_size - centroid).abs();
        if dist < best_distance {
            best_distance = dist;
            best_level = level;
        }
    }

    if best_distance > MAX_HEADING_DISTANCE_MULTIPLIER * gap_info.avg_gap {
        return None;
    }

    best_level
}

pub(super) struct GapInfo {
    avg_gap: f32,
}

pub(super) fn precompute_gap_info(heading_map: &[(f32, Option<u8>)]) -> GapInfo {
    if heading_map.len() <= 1 {
        return GapInfo { avg_gap: f32::INFINITY };
    }

    let mut centroids: Vec<f32> = heading_map.iter().map(|(c, _)| *c).collect();
    centroids.sort_by(|a, b| a.total_cmp(b));
    let gaps: Vec<f32> = centroids.windows(2).map(|w| (w[1] - w[0]).abs()).collect();
    let avg_gap = if gaps.is_empty() {
        f32::INFINITY
    } else {
        gaps.iter().sum::<f32>() / gaps.len() as f32
    };

    GapInfo { avg_gap }
}

/// Refine heading levels across the entire document.
///
/// 1. Promotes the first heading to H1 when no H1 exists (title inference).
/// 2. Merges consecutive H1 headings at the same font size into one title (any page).
/// 3. Demotes numbered section headings from H1 to H2 when a non-numbered title H1 exists.
/// 4. Preserves changelog section/version hierarchy where geometry confirms adjacency.
pub(super) fn refine_heading_hierarchy(all_pages: &mut [Vec<PdfParagraph>]) {
    split_combined_changelog_version_headings(all_pages);
    refine_heading_hierarchy_inner(all_pages);
    promote_changelog_version_headings(all_pages);
}

fn refine_heading_hierarchy_inner(all_pages: &mut [Vec<PdfParagraph>]) {
    let h1_count: usize = all_pages
        .iter()
        .flat_map(|page| page.iter())
        .filter(|p| p.heading_level == Some(1))
        .count();

    if h1_count == 0 {
        let has_any_heading = all_pages
            .iter()
            .flat_map(|page| page.iter())
            .any(|p| p.heading_level.is_some());
        if has_any_heading {
            promote_title_heading(all_pages);
        }

        let still_no_h1 = !all_pages
            .iter()
            .flat_map(|page| page.iter())
            .any(|p| p.heading_level == Some(1));
        if still_no_h1 && !all_pages.is_empty() && !all_pages[0].is_empty() {
            let total_paragraphs: usize = all_pages.iter().map(|page| page.len()).sum();
            let page0 = &all_pages[0];
            let max_font_on_page = page0.iter().map(|p| p.dominant_font_size).fold(0.0f32, f32::max);
            let first = &page0[0];
            let first_text = paragraph_plain_text(first);
            let first_wc = first_text.split_whitespace().count();
            let rest_font_size = other_paragraphs_font_size(all_pages, 0, 0);
            let clears_font_gate = rest_font_size.is_none_or(|body_font| {
                body_font <= 0.0
                    || (first.dominant_font_size >= body_font * MIN_HEADING_FONT_RATIO
                        && first.dominant_font_size >= body_font + MIN_HEADING_FONT_GAP)
            });
            if total_paragraphs >= MIN_BLOCKS_FOR_FONT_HEADING
                && clears_font_gate
                && first.dominant_font_size >= max_font_on_page
                && first_wc <= 10
                && first_wc > 0
                && !first.is_page_furniture
                && !looks_like_bare_url(&first_text)
                && !is_changelog_hierarchy_member(page0, 0)
            {
                all_pages[0][0].heading_level = Some(1);
            }
        }
    }

    let h1_count: usize = all_pages
        .iter()
        .flat_map(|page| page.iter())
        .filter(|p| p.heading_level == Some(1))
        .count();

    if h1_count <= 1 {
        return;
    }

    for page in all_pages.iter_mut() {
        merge_consecutive_h1s(page);
    }

    let h1_count: usize = all_pages
        .iter()
        .flat_map(|page| page.iter())
        .filter(|p| p.heading_level == Some(1))
        .count();

    if h1_count <= 1 {
        return;
    }

    let first_h1_is_title = all_pages
        .iter()
        .flat_map(|page| page.iter())
        .find(|p| p.heading_level == Some(1))
        .is_some_and(|p| !starts_with_section_number(&paragraph_plain_text(p)));

    if !first_h1_is_title {
        return;
    }

    let mut found_first = false;
    for page in all_pages.iter_mut() {
        for para in page.iter_mut() {
            if para.heading_level == Some(1) {
                if !found_first {
                    found_first = true;
                    continue;
                }
                if starts_with_section_number(&paragraph_plain_text(para)) {
                    para.heading_level = Some(2);
                }
            }
        }
    }
}

fn split_combined_changelog_version_headings(all_pages: &mut [Vec<PdfParagraph>]) {
    for page in all_pages {
        let mut index = 0;
        while index < page.len() {
            let Some(version) = combined_changelog_version(&page[index]) else {
                index += 1;
                continue;
            };
            if !page[index].is_bold
                || page[index].is_list_item
                || page[index].is_code_block
                || page[index].is_formula
                || page[index].is_page_furniture
            {
                index += 1;
                continue;
            }
            let Some((parent_bbox, version_bbox)) = combined_changelog_split_bboxes(&page[index]) else {
                index += 1;
                continue;
            };

            let mut version_paragraph = page[index].clone();
            page[index].text = "Recent Change Log".to_string();
            page[index].lines.clear();
            page[index].heading_level = Some(2);
            page[index].block_bbox = Some(parent_bbox);
            page[index].word_count = 3;

            version_paragraph.text = version;
            version_paragraph.lines.clear();
            version_paragraph.heading_level = Some(3);
            version_paragraph.is_list_item = false;
            version_paragraph.is_code_block = false;
            version_paragraph.is_formula = false;
            version_paragraph.is_page_furniture = false;
            version_paragraph.block_bbox = Some(version_bbox);
            version_paragraph.word_count = 1;
            page.insert(index + 1, version_paragraph);
            index += 2;
        }
    }
}

fn combined_changelog_version(paragraph: &PdfParagraph) -> Option<String> {
    let text = effective_text(paragraph);
    let text = text.trim();
    let version = if let Some(version) = text.strip_prefix("Recent Change Log\n") {
        version
    } else {
        if !combined_changelog_has_multiline_geometry(paragraph) {
            return None;
        }
        text.strip_prefix("Recent Change Log ")?
    }
    .trim();
    (!version.contains(char::is_whitespace) && is_semantic_version(version)).then(|| version.to_string())
}

fn combined_changelog_has_multiline_geometry(paragraph: &PdfParagraph) -> bool {
    let baseline_gap = paragraph
        .lines
        .iter()
        .map(|line| line.baseline_y)
        .fold(None, |bounds, baseline| match bounds {
            None => Some((baseline, baseline)),
            Some((minimum, maximum)) => Some((minimum.min(baseline), maximum.max(baseline))),
        })
        .is_some_and(|(minimum, maximum)| {
            maximum - minimum >= paragraph.dominant_font_size * CHANGELOG_COMBINED_MIN_BASELINE_GAP_RATIO
        });
    let tall_bbox = paragraph.block_bbox.is_some_and(|(_, bottom, _, top)| {
        top - bottom >= paragraph.dominant_font_size * CHANGELOG_COMBINED_MIN_HEIGHT_RATIO
    });

    baseline_gap || tall_bbox
}

fn combined_changelog_split_bboxes(paragraph: &PdfParagraph) -> Option<(ParagraphBbox, ParagraphBbox)> {
    let (left, bottom, right, top) = paragraph.block_bbox.or_else(|| {
        let mut segments = paragraph.lines.iter().flat_map(|line| line.segments.iter());
        let first = segments.next()?;
        Some(segments.fold(
            (first.x, first.y, first.x + first.width, first.y + first.height),
            |(left, bottom, right, top), segment| {
                (
                    left.min(segment.x),
                    bottom.min(segment.y),
                    right.max(segment.x + segment.width),
                    top.max(segment.y + segment.height),
                )
            },
        ))
    })?;
    let midpoint = bottom + (top - bottom) / 2.0;
    Some(((left, midpoint, right, top), (left, bottom, right, midpoint)))
}

fn promote_changelog_version_headings(all_pages: &mut [Vec<PdfParagraph>]) {
    for page in all_pages {
        for index in 0..page.len().saturating_sub(1) {
            let (before, after) = page.split_at_mut(index + 1);
            let parent = &mut before[index];
            let version = &mut after[0];

            if !is_changelog_version_pair(parent, version) {
                continue;
            }

            parent.heading_level = Some(2);
            version.heading_level = Some(3);
        }
    }
}

fn is_changelog_version_pair(parent: &PdfParagraph, version: &PdfParagraph) -> bool {
    parent.heading_level.is_some()
        && effective_text(parent).trim() == "Recent Change Log"
        && version.is_bold
        && is_semantic_version(effective_text(version).trim())
        && !version.is_list_item
        && !version.is_code_block
        && !version.is_formula
        && !version.is_page_furniture
        && changelog_version_is_near_and_aligned(parent, version)
}

fn is_semantic_version(text: &str) -> bool {
    let version = text.strip_prefix('v').unwrap_or(text);
    let mut parts = version.split('.');
    let first = parts.next();
    let second = parts.next();
    let third = parts.next();
    let has_extra = parts.next().is_some();

    !has_extra
        && first.is_some_and(is_ascii_digits)
        && second.is_some_and(is_ascii_digits)
        && third.is_none_or(is_ascii_digits)
}

fn is_ascii_digits(part: &str) -> bool {
    !part.is_empty() && part.bytes().all(|byte| byte.is_ascii_digit())
}

fn changelog_version_is_near_and_aligned(parent: &PdfParagraph, version: &PdfParagraph) -> bool {
    let (Some((parent_left, parent_bottom, _, _)), Some((version_left, _, _, version_top))) =
        (parent.block_bbox, version.block_bbox)
    else {
        return false;
    };

    let reference_size = parent.dominant_font_size.max(version.dominant_font_size);
    let left_offset = (parent_left - version_left).abs();
    let vertical_gap = parent_bottom - version_top;

    left_offset <= reference_size * CHANGELOG_VERSION_MAX_LEFT_OFFSET_RATIO
        && vertical_gap >= -reference_size * CHANGELOG_VERSION_MAX_VERTICAL_OVERLAP_RATIO
        && vertical_gap <= reference_size * CHANGELOG_VERSION_MAX_VERTICAL_GAP_RATIO
}

/// Determine heading level for a bold/italic paragraph based on font-size ratio to body.
///
/// - Font size > 1.2× body → H2 (clearly larger sub-heading)
/// - Font size at body size but bold → H3 (same-size bold sub-heading)
/// - Section numbering overrides: uses dot count for depth (e.g., "3.2" → H3)
fn infer_bold_heading_level(font_size: f32, body_font_size: f32, text: &str) -> u8 {
    if starts_with_section_number(text) {
        return infer_section_level(text);
    }

    if body_font_size > 0.0 {
        let ratio = font_size / body_font_size;
        if ratio > 1.2 {
            return 2;
        }
        return 3;
    }

    2
}

/// Infer heading level from section numbering in text.
///
/// Determines depth from the numbering pattern:
/// - "1 Introduction" or "I. INTRO" or "A. Proofs" → H2 (top-level section)
/// - "1.1 Details" or "A.1 Sub" → H3 (sub-section)
/// - "1.1.1 Deep" → H4 (sub-sub-section)
fn infer_section_level(text: &str) -> u8 {
    let trimmed = text.trim();

    let first_char = trimmed.chars().next().unwrap_or(' ');
    let is_alpha_prefix = first_char.is_ascii_alphabetic()
        && trimmed.len() >= 2
        && matches!(trimmed.as_bytes().get(1), Some(b'.' | b')' | b' '));

    let numbering_end = if is_alpha_prefix {
        let after_letter = &trimmed[1..];
        let rest_end = after_letter
            .find(|c: char| !c.is_ascii_digit() && c != '.')
            .unwrap_or(0);
        1 + rest_end
    } else {
        let roman_chars: &[u8] = b"IVXLCDM";
        let bytes = trimmed.as_bytes();
        let roman_end = bytes.iter().position(|b| !roman_chars.contains(b)).unwrap_or(0);
        if roman_end > 0 && roman_end <= 5 && roman_end < bytes.len() {
            let next = bytes[roman_end];
            if (next == b'.' || next == b' ' || next == b')') && is_valid_roman(&trimmed[..roman_end]) {
                return 2;
            }
        }
        trimmed.find(|c: char| !c.is_ascii_digit() && c != '.').unwrap_or(0)
    };

    if numbering_end == 0 {
        return 2;
    }

    let numbering = &trimmed[..numbering_end];
    let dot_count = numbering.chars().filter(|&c| c == '.').count();
    let effective_dots = if numbering.ends_with('.') {
        dot_count.saturating_sub(1)
    } else {
        dot_count
    };

    match effective_dots {
        0 => 2,
        1 => 3,
        _ => 4,
    }
}

/// Check whether text ends with a genuine sentence-terminating period.
///
/// A trailing ellipsis (`...` or the `…` glyph) is a truncation marker — common
/// in headings and truncated titles ("Impaired Glucose Tolerance ...") — not a
/// sentence terminator, so it does not disqualify a line from being a heading.
pub(super) fn ends_with_sentence_period(text: &str) -> bool {
    let t = text.trim_end();
    t.ends_with('.') && !t.ends_with("..")
}

/// Check if text looks like a section/legal heading that legitimately ends with a period.
/// Uses language-agnostic structural signals only:
/// - Starts with § (universal section symbol)
/// - All-caps short text (e.g., "ARTICLE IV.", "CHAPITRE 3.")
/// - Starts with a section number (e.g., "3.2. Methods")
pub(super) fn is_section_pattern(text: &str) -> bool {
    let t = text.trim();
    if t.starts_with('§') {
        return true;
    }
    let words = t.split_whitespace().count();
    if words <= 6 && t.chars().filter(|c| c.is_alphabetic()).all(|c| c.is_uppercase()) {
        return true;
    }
    starts_with_section_number(t)
}

/// Check if text starts like a numbered SECTION HEADING as opposed to a list item.
///
/// `is_section_pattern` treats ANY leading number as a section marker, which
/// makes numbered list items ("1. First point") unclassifiable as lists. This
/// tighter predicate only flags patterns that are reliably headings:
/// - multi-level numbering: "3.2 Methods", "3.2.1 Details"
/// - roman-numeral markers: "IV. Results", "II) Scope"
/// - single number with an ALL-CAPS remainder: "1. INTRODUCTION"
///
/// A single-level number followed by mixed-case text ("1. Énumération") is a
/// list item and returns `false`.
pub(super) fn is_numbered_section_heading(text: &str) -> bool {
    let t = text.trim();
    let bytes = t.as_bytes();
    if bytes.is_empty() {
        return false;
    }

    let roman_chars: &[u8] = b"IVXLCDM";
    let roman_end = bytes.iter().position(|b| !roman_chars.contains(b)).unwrap_or(0);
    if roman_end > 0
        && roman_end < bytes.len()
        && matches!(bytes[roman_end], b'.' | b' ' | b')')
        && is_valid_roman(&t[..roman_end])
    {
        return true;
    }

    let mut levels = 0usize;
    let mut idx = 0usize;
    loop {
        let digit_len = bytes[idx..]
            .iter()
            .position(|b| !b.is_ascii_digit())
            .unwrap_or(bytes.len() - idx);
        if digit_len == 0 {
            break;
        }
        levels += 1;
        idx += digit_len;
        if bytes.get(idx) == Some(&b'.') && bytes.get(idx + 1).is_some_and(|b| b.is_ascii_digit()) {
            idx += 1;
        } else {
            break;
        }
    }
    if levels == 0 {
        return false;
    }
    if levels >= 2 {
        return true;
    }

    if matches!(bytes.get(idx), Some(b'.') | Some(b')')) {
        idx += 1;
    }
    let remainder = t[idx..].trim_start();
    remainder.chars().any(|c| c.is_alphabetic())
        && remainder
            .chars()
            .filter(|c| c.is_alphabetic())
            .all(|c| c.is_uppercase())
}

/// Check if text starts with a section number pattern (e.g., "1 ", "2.1 ", "A.", "III.").
pub(super) fn starts_with_section_number(text: &str) -> bool {
    let trimmed = text.trim();
    let bytes = trimmed.as_bytes();
    if bytes.is_empty() {
        return false;
    }
    let digit_end = bytes.iter().position(|&b| !b.is_ascii_digit()).unwrap_or(0);
    if digit_end > 0 && digit_end < bytes.len() {
        let next = bytes[digit_end];
        if next == b' ' || next == b'.' || next == b')' {
            return true;
        }
    }
    let roman_chars: &[u8] = b"IVXLCDM";
    let roman_end = bytes.iter().position(|b| !roman_chars.contains(b)).unwrap_or(0);
    if roman_end > 0 && roman_end <= 5 && roman_end < bytes.len() {
        let next = bytes[roman_end];
        if next == b'.' || next == b' ' || next == b')' {
            let prefix = &trimmed[..roman_end];
            if is_valid_roman(prefix) {
                return true;
            }
        }
    }
    false
}

/// Check if a string is a valid roman numeral (I-XX range, covers most section numbering).
fn is_valid_roman(s: &str) -> bool {
    matches!(
        s,
        "I" | "II"
            | "III"
            | "IV"
            | "V"
            | "VI"
            | "VII"
            | "VIII"
            | "IX"
            | "X"
            | "XI"
            | "XII"
            | "XIII"
            | "XIV"
            | "XV"
            | "XVI"
            | "XVII"
            | "XVIII"
            | "XIX"
            | "XX"
    )
}

/// Demote unnumbered H2 headings to H3 when they appear between numbered H2 sections.
///
/// In documents with numbered sections (e.g., "1 INTRODUCTION", "5 EXPERIMENTS"),
/// unnumbered headings between consecutive numbered H2s are typically sub-sections.
/// For example, "Baselines for Object Detection" between "5 EXPERIMENTS" and
/// "6 CONCLUSION" should be H3, not H2.
///
/// Only applies when the document has at least 3 numbered H2 headings, indicating
/// a consistent numbering scheme.
pub(super) fn demote_unnumbered_subsections(all_pages: &mut [Vec<PdfParagraph>]) {
    let mut h2_info: Vec<(usize, usize, bool)> = Vec::new();
    for (page_idx, page) in all_pages.iter().enumerate() {
        for (para_idx, para) in page.iter().enumerate() {
            if para.heading_level == Some(2) {
                let text = paragraph_plain_text(para);
                h2_info.push((page_idx, para_idx, starts_with_section_number(&text)));
            }
        }
    }

    let numbered_count = h2_info.iter().filter(|(_, _, numbered)| *numbered).count();
    if numbered_count < 3 {
        return;
    }

    let numbered_positions: Vec<usize> = h2_info
        .iter()
        .enumerate()
        .filter(|(_, (_, _, numbered))| *numbered)
        .map(|(idx, _)| idx)
        .collect();

    for window in numbered_positions.windows(2) {
        let start = window[0];
        let end = window[1];
        for &(page_idx, para_idx, is_numbered) in &h2_info[start + 1..end] {
            if !is_numbered {
                let layout_confirmed = matches!(
                    all_pages[page_idx][para_idx].layout_class,
                    Some(super::types::LayoutHintClass::SectionHeader | super::types::LayoutHintClass::Title)
                );
                if !layout_confirmed {
                    all_pages[page_idx][para_idx].heading_level = Some(3);
                }
            }
        }
    }
}

/// Demote long runs of consecutive same-level headings to body text.
///
/// When the layout model (or font-size classification) produces 4+ consecutive
/// headings at the same level with no intervening body text, they're likely
/// misclassified (e.g., song lyrics, list items, short centered paragraphs).
/// Real documents rarely have more than 3 consecutive headings.
pub(super) fn demote_heading_runs(all_pages: &mut [Vec<PdfParagraph>]) {
    const MAX_CONSECUTIVE: usize = 3;

    for page in all_pages.iter_mut() {
        let mut run_start = 0;
        while run_start < page.len() {
            let Some(level) = page[run_start].heading_level else {
                run_start += 1;
                continue;
            };

            let mut run_end = run_start + 1;
            while run_end < page.len()
                && page[run_end].heading_level == Some(level)
                && page[run_end].layout_region_path == page[run_start].layout_region_path
            {
                run_end += 1;
            }

            let run_len = run_end - run_start;
            if run_len > MAX_CONSECUTIVE {
                for para in &mut page[run_start + 1..run_end] {
                    let layout_confirmed = matches!(
                        para.layout_class,
                        Some(super::types::LayoutHintClass::SectionHeader | super::types::LayoutHintClass::Title)
                    );
                    if !layout_confirmed {
                        para.heading_level = None;
                    }
                }
            }

            run_start = run_end;
        }
    }
}

/// Char-weighted dominant font size of every paragraph in the document except
/// the one at `(exclude_page, exclude_index)`.
///
/// Used to gate the "no heading anywhere" title-promotion fallback: the
/// excluded paragraph is the force-promotion candidate, so this answers "how
/// much larger is the candidate than the rest of the document's text", the
/// same ratio/gap test `assign_heading_levels_smart` applies to font-size
/// clusters. Returns `None` when there is no other text to compare against.
fn other_paragraphs_font_size(
    all_pages: &[Vec<PdfParagraph>],
    exclude_page: usize,
    exclude_index: usize,
) -> Option<f32> {
    let mut weighted_sum = 0.0f64;
    let mut total_chars = 0usize;
    for (page_idx, page) in all_pages.iter().enumerate() {
        for (para_idx, para) in page.iter().enumerate() {
            if page_idx == exclude_page && para_idx == exclude_index {
                continue;
            }
            let char_count = paragraph_plain_text(para).chars().count();
            if char_count == 0 {
                continue;
            }
            weighted_sum += f64::from(para.dominant_font_size) * char_count as f64;
            total_chars += char_count;
        }
    }
    if total_chars == 0 {
        None
    } else {
        Some((weighted_sum / total_chars as f64) as f32)
    }
}

/// Extract plain text from a paragraph.
fn paragraph_plain_text(para: &PdfParagraph) -> String {
    para.lines
        .iter()
        .flat_map(|l| l.segments.iter())
        .map(|s| s.text.as_str())
        .collect::<Vec<_>>()
        .join(" ")
}

/// Promote the most likely title heading to H1 when no H1 exists.
///
/// Strategy:
/// 1. If any heading has layout_class == Title, promote it to H1.
/// 2. Otherwise, on the first page only, promote the heading with the largest
///    font size IF it's clearly larger than other headings (at least 1.5pt gap).
fn promote_title_heading(all_pages: &mut [Vec<PdfParagraph>]) {
    for page in all_pages.iter_mut() {
        for index in 0..page.len() {
            if is_changelog_hierarchy_member(page, index) {
                continue;
            }
            let para = &mut page[index];
            if para.heading_level.is_some() && para.layout_class == Some(super::types::LayoutHintClass::Title) {
                para.heading_level = Some(1);
                return;
            }
        }
    }

    if all_pages.is_empty() {
        return;
    }
    let page = &all_pages[0];
    let headings: Vec<(usize, f32)> = page
        .iter()
        .enumerate()
        .filter(|(index, p)| p.heading_level.is_some() && !is_changelog_hierarchy_member(page, *index))
        .map(|(i, p)| (i, p.dominant_font_size))
        .collect();

    if headings.is_empty() {
        return;
    }

    if headings.len() == 1 {
        all_pages[0][headings[0].0].heading_level = Some(1);
        return;
    }

    let max_size = headings.iter().map(|(_, s)| *s).fold(0.0f32, f32::max);
    let second_max = headings
        .iter()
        .map(|(_, s)| *s)
        .filter(|s| *s < max_size)
        .fold(0.0f32, f32::max);

    if max_size - second_max >= 1.5
        && let Some(&(idx, _)) = headings.iter().find(|(_, s)| *s == max_size)
    {
        all_pages[0][idx].heading_level = Some(1);
    }
}

fn is_changelog_hierarchy_member(page: &[PdfParagraph], index: usize) -> bool {
    (index + 1 < page.len() && is_changelog_version_pair(&page[index], &page[index + 1]))
        || (index > 0 && is_changelog_version_pair(&page[index - 1], &page[index]))
}

/// Merge consecutive H1 paragraphs at the same font size into a single heading.
///
/// Split titles (e.g., "KAISUN HOLDINGS" on one line, "LIMITED" on the next)
/// often produce separate H1 paragraphs. When they share the same font size
/// and look like grammatical continuations they should be a single heading.
///
/// Product model codes ("HR 22", "HR 28") at the same font size are NOT merged —
/// each is a distinct item, not a split title.
fn merge_consecutive_h1s(page: &mut Vec<PdfParagraph>) {
    let mut i = 0;
    while i < page.len() {
        if page[i].heading_level != Some(1) {
            i += 1;
            continue;
        }
        let base_fs = page[i].dominant_font_size;
        let mut run_end = i + 1;
        while run_end < page.len()
            && page[run_end].heading_level == Some(1)
            && page[run_end].layout_region_path == page[i].layout_region_path
            && (page[run_end].dominant_font_size - base_fs).abs() < 0.5
            && looks_like_title_continuation(&page[run_end - 1], &page[run_end])
        {
            run_end += 1;
        }
        if run_end - i > 1 {
            let mut merged_lines = std::mem::take(&mut page[i].lines);
            let mut merged_text_parts: Vec<String> = if page[i].text.is_empty() {
                Vec::new()
            } else {
                vec![std::mem::take(&mut page[i].text)]
            };
            for para in &page[i + 1..run_end] {
                merged_lines.extend(para.lines.clone());
                if !para.text.is_empty() {
                    merged_text_parts.push(para.text.clone());
                }
            }
            page[i].lines = merged_lines;
            if !merged_text_parts.is_empty() {
                page[i].text = merged_text_parts.join(" ");
            }
            page.drain(i + 1..run_end);
        }
        i += 1;
    }
}

/// True when `next` looks like a grammatical continuation of `prev` (split title).
///
/// Returns false for product-code-style lines ("HR 22", "HR 28/24") and numbered
/// items — those are standalone headings, not continuations of the previous line.
///
/// The 4-word limit is conservative by design: real split titles ("KAISUN HOLDINGS" /
/// "LIMITED", "BANCO CENTRAL" / "DO BRASIL") are invariably short. Longer first lines
/// are more likely lists or section labels than wrapped titles. Trades occasional
/// under-merging of unusually long company names for zero false merges on cover-page
/// model-code lists — the class of bug this function was introduced to fix.
fn looks_like_title_continuation(prev: &PdfParagraph, next: &PdfParagraph) -> bool {
    let next_text = effective_text(next);
    if looks_like_standalone_heading_text(&next_text) {
        return false;
    }
    let prev_text = effective_text(prev);
    if prev_text.trim_end().ends_with(['.', '!', '?', ':']) {
        return false;
    }
    let prev_wc = prev_text.split_whitespace().count();
    let next_wc = next_text.split_whitespace().count();
    prev_wc <= 4 && next_wc <= 4
}

/// Return the effective plain text for a paragraph, preferring `para.text` (the
/// full-text heuristic path) over segment-joined `para.lines` (structure-tree path).
///
/// Mirrors the `get_text` closure in `assembly::push_paragraph_element` so that
/// classification helpers and rendering always agree on what a paragraph says.
fn effective_text(para: &PdfParagraph) -> String {
    if !para.text.is_empty() {
        para.text.clone()
    } else {
        paragraph_plain_text(para)
    }
}

/// True when text looks like a standalone heading rather than a title continuation.
///
/// Matches product model codes ("HR 22", "HR 28/24"), mixed alphanumeric tokens
/// ("A4", "HR28"), and lines that start with a digit.
fn looks_like_standalone_heading_text(text: &str) -> bool {
    let trimmed = text.trim();
    let mut words = trimmed.splitn(3, ' ');
    let first = words.next().unwrap_or("");
    let second = words.next().unwrap_or("");

    if first.chars().next().is_some_and(|c| c.is_ascii_digit()) {
        return true;
    }
    let first_has_alpha = first.chars().any(|c| c.is_ascii_alphabetic());
    let first_has_digit = first.chars().any(|c| c.is_ascii_digit());
    if first_has_alpha && first_has_digit {
        return true;
    }
    if !second.is_empty()
        && first.chars().all(|c| c.is_ascii_alphabetic())
        && second.chars().next().is_some_and(|c| c.is_ascii_digit())
    {
        return true;
    }
    false
}

/// Detect paragraphs in page margins that repeat across pages.
///
/// Only considers paragraphs whose bounding box falls in the page margins
/// (top 10%, bottom 10%, or narrow left/right strips). If the same text
/// appears in the margins on >50% of pages, it's furniture (running headers,
/// footers, metadata stamps, watermarks).
///
/// `page_heights` provides the height of each page for margin calculation.
pub(super) fn mark_cross_page_repeating_text(all_pages: &mut [Vec<PdfParagraph>], page_heights: &[f32]) {
    if all_pages.len() < 4 {
        return;
    }

    let margin_frac = 0.10;

    let mut text_page_count: ahash::AHashMap<String, usize> = ahash::AHashMap::new();
    let mut alphanum_to_exact: ahash::AHashMap<String, ahash::AHashSet<String>> = ahash::AHashMap::new();
    let mut first_seen_page: ahash::AHashMap<String, usize> = ahash::AHashMap::new();

    for (page_idx, page) in all_pages.iter().enumerate() {
        let page_h = page_heights.get(page_idx).copied().unwrap_or(792.0);
        let top_margin_y = page_h * (1.0 - margin_frac);
        let bottom_margin_y = page_h * margin_frac;

        let mut seen: ahash::AHashSet<String> = ahash::AHashSet::new();
        for para in page {
            if para.is_page_furniture {
                continue;
            }

            let in_margin = para
                .block_bbox
                .is_some_and(|(_, bottom, _, top)| top > top_margin_y || bottom < bottom_margin_y);
            if !in_margin {
                continue;
            }

            let text = paragraph_plain_text(para);
            let normalized = text.trim().to_lowercase();
            if normalized.is_empty() {
                continue;
            }

            let alphanum_key: String = normalized.chars().filter(|c| c.is_alphanumeric()).collect();
            if alphanum_key.is_empty() {
                continue;
            }

            alphanum_to_exact
                .entry(alphanum_key.clone())
                .or_default()
                .insert(normalized.clone());

            if seen.insert(alphanum_key.clone()) {
                let count = text_page_count.entry(alphanum_key.clone()).or_insert(0);
                if *count == 0 {
                    first_seen_page.insert(alphanum_key, page_idx);
                }
                *count += 1;
            }
        }
    }

    let threshold = all_pages.len() / 2;

    let mut repeating: ahash::AHashSet<String> = ahash::AHashSet::new();
    for (alphanum_key, count) in &text_page_count {
        if *count > threshold
            && let Some(variants) = alphanum_to_exact.get(alphanum_key)
        {
            for v in variants {
                repeating.insert(v.clone());
            }
        }
    }

    if repeating.is_empty() {
        return;
    }

    tracing::debug!(
        repeating_count = repeating.len(),
        threshold,
        total_pages = all_pages.len(),
        "cross-page margin repeating text detected"
    );

    for (page_idx, page) in all_pages.iter_mut().enumerate() {
        let page_h = page_heights.get(page_idx).copied().unwrap_or(792.0);
        let top_margin_y = page_h * (1.0 - margin_frac);
        let bottom_margin_y = page_h * margin_frac;

        for para in page.iter_mut() {
            if para.is_page_furniture {
                continue;
            }
            let in_margin = para
                .block_bbox
                .is_some_and(|(_, bottom, _, top)| top > top_margin_y || bottom < bottom_margin_y);
            if !in_margin {
                continue;
            }
            let text = paragraph_plain_text(para);
            let normalized = text.trim().to_lowercase();
            if repeating.contains(&normalized) {
                let alphanum_key: String = normalized.chars().filter(|c| c.is_alphanumeric()).collect();
                if first_seen_page.get(&alphanum_key).copied() == Some(page_idx) {
                    continue;
                }
                tracing::trace!(
                    text = %normalized.chars().take(60).collect::<String>(),
                    was_heading = ?para.heading_level,
                    "marking margin text as furniture"
                );
                para.is_page_furniture = true;
                para.heading_level = None;
            }
        }
    }
}

/// Check if a character is a math/formula character.
///
/// Includes common math operators, Greek letters, set theory symbols,
/// and other characters commonly found in mathematical formulas.
fn is_math_character(c: char) -> bool {
    matches!(
        c,
        '\u{2200}'
            | '\u{2203}'
            | '\u{2208}'
            | '\u{2209}'
            | '\u{2282}'
            | '\u{2283}'
            | '\u{222A}'
            | '\u{2229}'
            | '\u{2211}'
            | '\u{222B}'
            | '\u{220F}'
            | '\u{2202}'
            | '\u{2207}'
            | '\u{2264}'
            | '\u{2265}'
            | '\u{2260}'
            | '\u{2248}'
            | '\u{00B1}'
            | '\u{221E}'
            | '\u{221A}'
            | '\u{2192}'
            | '\u{2190}'
            | '\u{2194}'
            | '\u{21D2}'
            | '\u{21D0}'
            | '\u{27E8}'
            | '\u{27E9}'
            | '\u{00D7}'
            | '\u{00F7}'
            | '+'
            | '='
            | '^'
            | '\u{00B9}'
            | '\u{00B2}'
            | '\u{00B3}'
            | '\u{2070}'..='\u{209F}'
    ) || is_greek_letter(c)
}

/// Check if a character is a Greek letter (lowercase α-ω or uppercase Α-Ω).
fn is_greek_letter(c: char) -> bool {
    matches!(c, '\u{0391}'..='\u{03A9}' | '\u{03B1}'..='\u{03C9}')
}

/// Remove arXiv watermark/sidebar noise from paragraphs on the first pages.
///
/// Handles two cases:
/// 1. Short standalone paragraphs that are just the arXiv identifier → mark as furniture.
/// 2. arXiv identifier appended to the end of a longer paragraph (LaTeX sidebar
///    text that the PDF extractor concatenates with body text) → strip the trailing noise.
pub(super) fn mark_arxiv_noise(all_pages: &mut [Vec<PdfParagraph>]) {
    let arxiv_re = regex::Regex::new(r"arXiv:\d{4}\.\d{4,5}").expect("valid regex");
    let trailing_re = regex::Regex::new(
        r"(?:\s+(?:\S+\s+){0,8})?arXiv:\d{4}\.\d{4,5}(?:v\d+)?(?:\s*\[[\w.-]+\])?\s*(?:\d{1,2}\s+\w+\s+\d{4})?\s*$",
    )
    .expect("valid regex");

    for page in all_pages.iter_mut().take(2) {
        for para in page.iter_mut() {
            if para.is_page_furniture {
                continue;
            }
            let text = paragraph_plain_text(para);
            let trimmed = text.trim();
            let word_count = trimmed.split_whitespace().count();

            if !arxiv_re.is_match(trimmed) {
                continue;
            }

            if word_count <= 25 {
                tracing::trace!(
                    text = %trimmed.chars().take(80).collect::<String>(),
                    "marking arXiv watermark as furniture"
                );
                para.is_page_furniture = true;
                para.heading_level = None;
            } else if let Some(m) = trailing_re.find(trimmed) {
                let noise = &trimmed[m.start()..];
                tracing::trace!(
                    stripped = %noise.chars().take(80).collect::<String>(),
                    "stripping trailing arXiv watermark from paragraph"
                );
                strip_trailing_text_from_paragraph(para, noise.trim());
            }
        }
    }
}

/// Strip trailing noise text from the last segment(s) of a paragraph.
fn strip_trailing_text_from_paragraph(para: &mut PdfParagraph, noise: &str) {
    for line in para.lines.iter_mut().rev() {
        for seg in line.segments.iter_mut().rev() {
            if let Some(pos) = seg.text.find(noise) {
                seg.text = seg.text[..pos].trim_end().to_string();
                return;
            }
            let seg_trimmed = seg.text.trim();
            if !seg_trimmed.is_empty() && noise.contains(seg_trimmed) {
                seg.text.clear();
            } else {
                return;
            }
        }
    }
}

/// Second-tier cross-page repeating text detection.
///
/// Supplements `mark_cross_page_repeating_text` by scanning ALL paragraphs
/// (not just margin-positioned ones) for short text that repeats on a
/// supermajority of pages. Catches inline conference headers, journal running
/// titles, and similar repeated boilerplate that appears outside the margin zone.
pub(super) fn mark_cross_page_repeating_short_text(all_pages: &mut [Vec<PdfParagraph>]) {
    if all_pages.len() < 5 {
        return;
    }

    let max_words = 20;
    let threshold = (all_pages.len() as f64 * 0.7).ceil() as usize;

    let mut text_page_count: ahash::AHashMap<String, usize> = ahash::AHashMap::new();
    let mut first_seen_page: ahash::AHashMap<String, usize> = ahash::AHashMap::new();
    for (page_idx, page) in all_pages.iter().enumerate() {
        let mut seen: ahash::AHashSet<String> = ahash::AHashSet::new();
        for para in page {
            if para.is_page_furniture {
                continue;
            }
            let text = paragraph_plain_text(para);
            let normalized = text.trim().to_lowercase();
            if normalized.is_empty() {
                continue;
            }
            let word_count = normalized.split_whitespace().count();
            if word_count > max_words {
                continue;
            }
            let alphanum_key: String = normalized.chars().filter(|c| c.is_alphanumeric()).collect();
            if alphanum_key.is_empty() {
                continue;
            }
            if seen.insert(alphanum_key.clone()) {
                let count = text_page_count.entry(alphanum_key.clone()).or_insert(0);
                if *count == 0 {
                    first_seen_page.insert(alphanum_key, page_idx);
                }
                *count += 1;
            }
        }
    }

    let repeating: ahash::AHashSet<String> = text_page_count
        .into_iter()
        .filter(|(_, count)| *count >= threshold)
        .map(|(key, _)| key)
        .collect();

    if repeating.is_empty() {
        return;
    }

    tracing::debug!(
        repeating_count = repeating.len(),
        threshold,
        total_pages = all_pages.len(),
        "cross-page short-text repeating detection (tier 2)"
    );

    for (page_idx, page) in all_pages.iter_mut().enumerate() {
        for para in page.iter_mut() {
            if para.is_page_furniture {
                continue;
            }
            let text = paragraph_plain_text(para);
            let normalized = text.trim().to_lowercase();
            let word_count = normalized.split_whitespace().count();
            if word_count > max_words {
                continue;
            }
            let alphanum_key: String = normalized.chars().filter(|c| c.is_alphanumeric()).collect();
            if repeating.contains(&alphanum_key) {
                if first_seen_page.get(&alphanum_key).copied() == Some(page_idx) {
                    continue;
                }
                tracing::trace!(
                    text = %normalized.chars().take(60).collect::<String>(),
                    "marking repeating short text as furniture (tier 2)"
                );
                para.is_page_furniture = true;
                para.heading_level = None;
            }
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::pdf::hierarchy::SegmentData;

    fn make_paragraph(font_size: f32, segment_count: usize) -> PdfParagraph {
        let segments: Vec<SegmentData> = (0..segment_count)
            .map(|i| SegmentData {
                text: format!("word{}", i),
                x: i as f32 * 50.0,
                y: 700.0,
                width: 40.0,
                height: font_size,
                font_size,
                is_bold: false,
                is_italic: false,
                is_monospace: false,
                baseline_y: 700.0,
                assigned_role: None,
            })
            .collect();

        let lines = vec![super::super::types::PdfLine {
            segments,
            baseline_y: 700.0,
            dominant_font_size: font_size,
            is_bold: false,
            is_monospace: false,
        }];
        let word_count = PdfParagraph::compute_word_count("", &lines);

        PdfParagraph {
            text: String::new(),
            lines,
            dominant_font_size: font_size,
            heading_level: None,
            is_bold: false,
            is_list_item: false,
            is_code_block: false,
            is_formula: false,
            is_page_furniture: false,
            layout_class: None,
            layout_region_path: None,
            caption_for: None,
            block_bbox: None,
            word_count,
        }
    }

    #[test]
    fn heading_runs_stop_at_layout_region_boundaries() {
        use crate::pdf::structure::types::{LayoutHintClass, LayoutRegionPath, LayoutRegionTag};

        let mut page = (0..4)
            .map(|index| {
                let mut paragraph = make_paragraph(18.0, 1);
                paragraph.heading_level = Some(2);
                paragraph.layout_region_path = Some(LayoutRegionPath {
                    root: LayoutRegionTag {
                        id: usize::from(index >= 2),
                        class_name: Some(LayoutHintClass::Text),
                    },
                    child: None,
                });
                paragraph
            })
            .collect::<Vec<_>>();

        demote_heading_runs(std::slice::from_mut(&mut page));
        assert!(page.iter().all(|paragraph| paragraph.heading_level == Some(2)));
    }

    #[test]
    fn test_classify_heading() {
        let heading_map = vec![(18.0, Some(1)), (12.0, None)];
        let mut paragraphs = vec![make_paragraph(18.0, 3)];
        classify_paragraphs(&mut paragraphs, &heading_map);
        assert_eq!(paragraphs[0].heading_level, Some(1));
    }

    #[test]
    fn test_classify_body() {
        let heading_map = vec![(18.0, Some(1)), (12.0, None)];
        let mut paragraphs = vec![make_paragraph(12.0, 5)];
        classify_paragraphs(&mut paragraphs, &heading_map);
        assert_eq!(paragraphs[0].heading_level, None);
    }

    #[test]
    fn test_classify_too_many_segments_for_heading() {
        let heading_map = vec![(18.0, Some(1)), (12.0, None)];
        let mut paragraphs = vec![make_paragraph(18.0, 21)];
        classify_paragraphs(&mut paragraphs, &heading_map);
        assert_eq!(paragraphs[0].heading_level, None);
    }

    #[test]
    fn test_find_heading_level_empty_map() {
        let gap_info = precompute_gap_info(&[]);
        assert_eq!(find_heading_level(12.0, &[], &gap_info), None);
    }

    #[test]
    fn test_find_heading_level_single_entry() {
        let heading_map = vec![(12.0, Some(1))];
        let gap_info = precompute_gap_info(&heading_map);
        assert_eq!(find_heading_level(12.0, &heading_map, &gap_info), Some(1));
    }

    #[test]
    fn test_find_heading_level_outlier_rejected() {
        let heading_map = vec![(12.0, None), (16.0, Some(2)), (20.0, Some(1))];
        let gap_info = precompute_gap_info(&heading_map);
        assert_eq!(find_heading_level(50.0, &heading_map, &gap_info), None);
    }

    #[test]
    fn test_find_heading_level_close_match() {
        let heading_map = vec![(12.0, None), (16.0, Some(2)), (20.0, Some(1))];
        let gap_info = precompute_gap_info(&heading_map);
        assert_eq!(find_heading_level(15.5, &heading_map, &gap_info), Some(2));
    }

    #[test]
    fn test_classify_bold_short_paragraph_promoted_to_heading() {
        let heading_map = vec![(12.0, None)];
        let mut para = make_paragraph(12.0, 3);
        para.is_bold = true;
        para.lines[0].is_bold = true;
        let mut paragraphs = vec![para];
        classify_paragraphs(&mut paragraphs, &heading_map);
        assert_eq!(paragraphs[0].heading_level, Some(3));
    }

    #[test]
    fn test_classify_bold_long_paragraph_not_promoted() {
        let heading_map = vec![(12.0, None)];
        let mut para = make_paragraph(12.0, 20);
        para.is_bold = true;
        let mut paragraphs = vec![para];
        classify_paragraphs(&mut paragraphs, &heading_map);
        assert_eq!(paragraphs[0].heading_level, None);
    }

    #[test]
    fn test_classify_bold_list_item_not_promoted() {
        let heading_map = vec![(12.0, None)];
        let mut para = make_paragraph(12.0, 3);
        para.is_bold = true;
        para.is_list_item = true;
        let mut paragraphs = vec![para];
        classify_paragraphs(&mut paragraphs, &heading_map);
        assert_eq!(paragraphs[0].heading_level, None);
    }

    #[test]
    fn test_classify_few_segments_many_words_not_heading() {
        let segments: Vec<SegmentData> = (0..3)
            .map(|i| SegmentData {
                text: "one two three four five six seven".to_string(),
                x: i as f32 * 200.0,
                y: 700.0,
                width: 180.0,
                height: 18.0,
                font_size: 18.0,
                is_bold: false,
                is_italic: false,
                is_monospace: false,
                baseline_y: 700.0,
                assigned_role: None,
            })
            .collect();

        let lines = vec![super::super::types::PdfLine {
            segments,
            baseline_y: 700.0,
            dominant_font_size: 18.0,
            is_bold: false,
            is_monospace: false,
        }];
        let word_count = PdfParagraph::compute_word_count("", &lines);

        let mut paragraphs = vec![PdfParagraph {
            text: String::new(),
            lines,
            dominant_font_size: 18.0,
            heading_level: None,
            is_bold: false,
            is_list_item: false,
            is_code_block: false,
            is_formula: false,
            is_page_furniture: false,
            layout_class: None,
            layout_region_path: None,
            caption_for: None,
            block_bbox: None,
            word_count,
        }];
        let heading_map = vec![(18.0, Some(1)), (12.0, None)];
        classify_paragraphs(&mut paragraphs, &heading_map);
        assert_eq!(paragraphs[0].heading_level, None);
    }

    fn make_h1(font_size: f32, text: &str) -> PdfParagraph {
        let mut p = make_paragraph(font_size, 1);
        p.lines[0].segments[0].text = text.to_string();
        p.heading_level = Some(1);
        p
    }

    fn make_h1_with_text(font_size: f32, text: &str) -> PdfParagraph {
        let mut p = make_h1(font_size, text);
        p.text = text.to_string();
        p
    }

    #[test]
    fn test_merge_consecutive_h1s_same_font() {
        let mut page = vec![
            make_h1(24.0, "KAISUN HOLDINGS"),
            make_h1(24.0, "LIMITED"),
            make_paragraph(12.0, 3),
        ];
        merge_consecutive_h1s(&mut page);
        assert_eq!(page.len(), 2);
        assert_eq!(page[0].heading_level, Some(1));
        assert_eq!(page[0].lines.len(), 2);
    }

    #[test]
    fn test_merge_h1s_text_field_synced() {
        let mut page = vec![
            make_h1_with_text(24.0, "KAISUN HOLDINGS"),
            make_h1_with_text(24.0, "LIMITED"),
        ];
        merge_consecutive_h1s(&mut page);
        assert_eq!(page.len(), 1);
        assert!(
            page[0].text.contains("KAISUN HOLDINGS"),
            "merged text must include first heading: {:?}",
            page[0].text
        );
        assert!(
            page[0].text.contains("LIMITED"),
            "merged text must include second heading: {:?}",
            page[0].text
        );
    }

    #[test]
    fn test_merge_h1s_model_codes_not_merged() {
        let mut page = vec![
            make_h1_with_text(24.0, "HR 22"),
            make_h1_with_text(24.0, "HR 28"),
            make_h1_with_text(24.0, "HR 28/24"),
            make_h1_with_text(24.0, "HR 36/30"),
        ];
        merge_consecutive_h1s(&mut page);
        assert_eq!(
            page.len(),
            4,
            "product model codes at same font size must stay as separate headings"
        );
    }

    #[test]
    fn test_merge_h1s_different_font_no_merge() {
        let mut page = vec![make_h1(24.0, "Title"), make_h1(18.0, "Subtitle")];
        merge_consecutive_h1s(&mut page);
        assert_eq!(page.len(), 2);
    }

    #[test]
    fn test_merge_h1s_long_first_line_not_merged() {
        let mut page = vec![
            make_h1_with_text(24.0, "The Quick Brown Fox Jumped"),
            make_h1_with_text(24.0, "Over"),
        ];
        merge_consecutive_h1s(&mut page);
        assert_eq!(
            page.len(),
            2,
            "first heading with >4 words must not merge with the next"
        );
        assert_eq!(page[0].text, "The Quick Brown Fox Jumped");
        assert_eq!(page[1].text, "Over");
    }

    #[test]
    fn test_merge_h1s_terminal_punctuation_not_merged() {
        for suffix in [".", "!", "?", ":"] {
            let first = format!("SECTION ONE{suffix}");
            let mut page = vec![make_h1_with_text(24.0, &first), make_h1_with_text(24.0, "INTRODUCTION")];
            merge_consecutive_h1s(&mut page);
            assert_eq!(
                page.len(),
                2,
                "heading ending with '{suffix}' must not merge: got {:?}",
                page[0].text
            );
        }
    }

    /// Create a paragraph with bbox in the top margin of a 792pt page.
    fn make_margin_body(text: &str) -> PdfParagraph {
        let mut p = make_paragraph(12.0, 1);
        p.lines[0].segments[0].text = text.to_string();
        p.block_bbox = Some((50.0, 740.0, 300.0, 760.0));
        p
    }

    /// Create a paragraph with bbox in the body (not margin) area.
    fn make_body_center(text: &str) -> PdfParagraph {
        let mut p = make_paragraph(12.0, 1);
        p.lines[0].segments[0].text = text.to_string();
        p.block_bbox = Some((50.0, 400.0, 300.0, 420.0));
        p
    }

    #[test]
    fn test_cross_page_repeating_text() {
        let page_heights = vec![792.0; 4];
        let mut pages = vec![
            vec![make_margin_body("Page 1 of 10"), make_body_center("Unique content A")],
            vec![make_margin_body("Page 1 of 10"), make_body_center("Unique content B")],
            vec![make_margin_body("Page 1 of 10"), make_body_center("Unique content C")],
            vec![make_margin_body("Page 1 of 10"), make_body_center("Unique content D")],
        ];
        mark_cross_page_repeating_text(&mut pages, &page_heights);
        assert!(!pages[0][0].is_page_furniture, "first occurrence must not be furniture");
        assert!(pages[1][0].is_page_furniture);
        assert!(pages[2][0].is_page_furniture);
        assert!(pages[3][0].is_page_furniture);
        assert!(!pages[0][1].is_page_furniture);
    }

    #[test]
    fn test_cross_page_repeating_marks_repeated_headings_as_furniture() {
        let page_heights = vec![792.0; 6];
        let mut pages = vec![];
        for _ in 0..6 {
            let mut h = make_h1(24.0, "Chapter");
            h.heading_level = Some(1);
            h.block_bbox = Some((50.0, 740.0, 300.0, 770.0));
            let mut body = make_paragraph(12.0, 3);
            body.block_bbox = Some((50.0, 400.0, 300.0, 420.0));
            pages.push(vec![h, body]);
        }
        mark_cross_page_repeating_text(&mut pages, &page_heights);
        assert!(!pages[0][0].is_page_furniture, "first occurrence must not be furniture");
        assert!(pages[1][0].is_page_furniture);
        assert!(pages[1][0].heading_level.is_none());
        assert!(pages[5][0].is_page_furniture);
    }

    #[test]
    fn test_cross_page_repeating_fuzzy_matches_iso_variants() {
        let page_heights = vec![792.0; 6];
        let mut pages = vec![
            vec![
                make_margin_body("O ISO 2021 All rights reserved"),
                make_body_center("Section content A"),
            ],
            vec![
                make_margin_body("O ISO 2021 All rights reserved"),
                make_body_center("Section content B"),
            ],
            vec![
                make_margin_body("O ISO 2021 All rights reserved"),
                make_body_center("Section content C"),
            ],
            vec![
                make_margin_body("OISO 2021Allrightsreserved"),
                make_body_center("Section content D"),
            ],
            vec![
                make_margin_body("OISO 2021Allrightsreserved"),
                make_body_center("Section content E"),
            ],
            vec![
                make_margin_body("OISO 2021Allrightsreserved"),
                make_body_center("Section content F"),
            ],
        ];
        mark_cross_page_repeating_text(&mut pages, &page_heights);
        assert!(
            !pages[0][0].is_page_furniture,
            "first occurrence of copyright notice must be preserved"
        );
        assert!(
            pages[1][0].is_page_furniture,
            "subsequent even-page copyright variant should be furniture"
        );
        assert!(
            pages[3][0].is_page_furniture,
            "odd-page copyright variant should be furniture"
        );
        assert!(!pages[0][1].is_page_furniture);
        assert!(!pages[3][1].is_page_furniture);
    }

    #[test]
    fn test_layout_text_bold_heading_font_promoted() {
        let heading_map = vec![(16.0, Some(2)), (12.0, None)];
        let mut para = make_paragraph(16.0, 3);
        para.is_bold = true;
        para.layout_class = Some(super::super::types::LayoutHintClass::Text);
        let mut paragraphs = vec![para];
        classify_paragraphs(&mut paragraphs, &heading_map);
        assert_eq!(paragraphs[0].heading_level, Some(2));
    }

    #[test]
    fn test_layout_text_non_bold_heading_font_not_promoted() {
        let heading_map = vec![(16.0, Some(2)), (12.0, None)];
        let mut para = make_paragraph(16.0, 3);
        para.layout_class = Some(super::super::types::LayoutHintClass::Text);
        let mut paragraphs = vec![para];
        classify_paragraphs(&mut paragraphs, &heading_map);
        assert_eq!(paragraphs[0].heading_level, None);
    }

    #[test]
    fn test_layout_text_bold_body_font_not_promoted_pass1() {
        let heading_map = vec![(16.0, Some(2)), (12.0, None)];
        let mut para = make_paragraph(12.0, 3);
        para.is_bold = true;
        para.layout_class = Some(super::super::types::LayoutHintClass::Text);
        let mut paragraphs = vec![para];
        classify_paragraphs(&mut paragraphs, &heading_map);
        assert_eq!(paragraphs[0].heading_level, None);
    }

    #[test]
    fn test_layout_text_bold_larger_font_promoted_pass2() {
        let heading_map = vec![(12.0, None)];
        let mut para = make_paragraph(14.0, 3);
        para.is_bold = true;
        para.layout_class = Some(super::super::types::LayoutHintClass::Text);
        let mut paragraphs = vec![para];
        classify_paragraphs(&mut paragraphs, &heading_map);
        assert_eq!(paragraphs[0].heading_level, Some(3));
    }

    #[test]
    fn test_layout_text_bold_much_larger_font_promoted_h2() {
        let heading_map = vec![(12.0, None)];
        let mut para = make_paragraph(15.0, 3);
        para.is_bold = true;
        para.layout_class = Some(super::super::types::LayoutHintClass::Text);
        let mut paragraphs = vec![para];
        classify_paragraphs(&mut paragraphs, &heading_map);
        assert_eq!(paragraphs[0].heading_level, Some(2));
    }

    #[test]
    fn test_infer_bold_heading_level_large_ratio_h2() {
        assert_eq!(infer_bold_heading_level(15.0, 12.0, "Methods"), 2);
    }

    #[test]
    fn test_infer_bold_heading_level_body_size_h3() {
        assert_eq!(infer_bold_heading_level(12.0, 12.0, "Methods"), 3);
    }

    #[test]
    fn test_infer_bold_heading_level_numbered_section() {
        assert_eq!(infer_bold_heading_level(12.0, 12.0, "3 Processing pipeline"), 2);
        assert_eq!(infer_bold_heading_level(12.0, 12.0, "3.2 AI models"), 3);
        assert_eq!(infer_bold_heading_level(12.0, 12.0, "3.2.1 Details"), 4);
    }

    #[test]
    fn test_infer_section_level_numeric() {
        assert_eq!(infer_section_level("1 Introduction"), 2);
        assert_eq!(infer_section_level("3.2 AI models"), 3);
        assert_eq!(infer_section_level("3.2.1 Details"), 4);
    }

    #[test]
    fn test_infer_section_level_roman() {
        assert_eq!(infer_section_level("I. INTRODUCTION"), 2);
        assert_eq!(infer_section_level("IV RESULTS"), 2);
    }

    #[test]
    fn test_infer_section_level_alpha() {
        assert_eq!(infer_section_level("A. Proofs"), 2);
        assert_eq!(infer_section_level("A.1 Sub-section"), 3);
    }

    #[test]
    fn test_infer_section_level_no_number() {
        assert_eq!(infer_section_level("Layout Analysis Model"), 2);
    }

    #[test]
    fn structure_sal_annotations_are_not_headings() {
        for annotation in [
            "__in",
            "__in_opt",
            "__out",
            "__out_opt",
            "__inout",
            "__inout_bcount_full(n)",
            "__deref__out_bcount(n)",
        ] {
            let mut para = make_text_paragraph(12.0, annotation, false);
            para.heading_level = Some(3);
            demote_structure_annotation_headings(std::slice::from_mut(&mut para));
            assert_eq!(para.heading_level, None, "annotation {annotation} remained a heading");
        }
    }

    #[test]
    fn structure_identifier_headings_are_preserved() {
        for identifier in ["__init__", "__input", "__in_section", "API_V2"] {
            let mut para = make_text_paragraph(18.0, identifier, true);
            para.heading_level = Some(2);
            demote_structure_annotation_headings(std::slice::from_mut(&mut para));
            assert_eq!(para.heading_level, Some(2), "identifier {identifier} was demoted");
        }
    }

    #[test]
    fn structure_sal_heading_with_strong_layout_evidence_is_preserved() {
        let mut para = make_text_paragraph(18.0, "__in", true);
        para.heading_level = Some(2);
        para.layout_class = Some(LayoutHintClass::SectionHeader);
        demote_structure_annotation_headings(std::slice::from_mut(&mut para));
        assert_eq!(para.heading_level, Some(2));
    }

    #[test]
    fn structure_sal_code_block_is_preserved() {
        let mut para = make_text_paragraph(12.0, "__out", false);
        para.heading_level = Some(3);
        para.is_code_block = true;
        demote_structure_annotation_headings(std::slice::from_mut(&mut para));
        assert_eq!(para.heading_level, Some(3));
    }

    /// Helper to create a paragraph with specific text.
    fn make_text_paragraph(font_size: f32, text: &str, is_bold: bool) -> PdfParagraph {
        let segments = vec![SegmentData {
            text: text.to_string(),
            x: 0.0,
            y: 700.0,
            width: 200.0,
            height: font_size,
            font_size,
            is_bold,
            is_italic: false,
            is_monospace: false,
            baseline_y: 700.0,
            assigned_role: None,
        }];
        let lines = vec![super::super::types::PdfLine {
            segments,
            baseline_y: 700.0,
            dominant_font_size: font_size,
            is_bold,
            is_monospace: false,
        }];
        let word_count = PdfParagraph::compute_word_count("", &lines);

        PdfParagraph {
            text: String::new(),
            lines,
            dominant_font_size: font_size,
            heading_level: None,
            is_bold,
            is_list_item: false,
            is_code_block: false,
            is_formula: false,
            is_page_furniture: false,
            layout_class: None,
            layout_region_path: None,
            caption_for: None,
            block_bbox: None,
            word_count,
        }
    }

    fn paragraph_at_indent(text: &str, left: f32) -> PdfParagraph {
        let mut paragraph = make_text_paragraph(12.0, text, false);
        paragraph.block_bbox = Some((left, 0.0, left + 200.0, 12.0));
        paragraph
    }

    #[test]
    fn indentation_does_not_promote_author_byline() {
        let mut paragraphs = vec![
            paragraph_at_indent("Header", 0.0),
            paragraph_at_indent("O. Sanni, A.P.I. Popoola / Data in Brief", 20.0),
            paragraph_at_indent("Footer", 0.0),
        ];

        detect_indentation_based_lists(&mut paragraphs);

        assert!(!paragraphs[1].is_list_item);
    }

    #[test]
    fn indentation_does_not_promote_numeric_continuations() {
        let mut paragraphs = vec![
            paragraph_at_indent("Header", 0.0),
            paragraph_at_indent("457", 20.0),
            paragraph_at_indent("8 show the remaining figures", 20.0),
            paragraph_at_indent("Footer", 0.0),
        ];

        detect_indentation_based_lists(&mut paragraphs);

        assert!(!paragraphs[1].is_list_item);
        assert!(!paragraphs[2].is_list_item);
    }

    #[test]
    fn numeric_prose_continuation_gate_is_narrow() {
        assert!(is_numeric_prose_continuation("457"));
        assert!(is_numeric_prose_continuation("8 show the remaining figures"));
        assert!(!is_numeric_prose_continuation("8 Results"));
        assert!(!is_numeric_prose_continuation("first item"));
    }

    #[test]
    fn test_section_numbering_promotes_non_bold_heading() {
        let heading_map = vec![(12.0, None)];
        let mut paragraphs = vec![make_text_paragraph(12.0, "3 Processing pipeline", false)];
        classify_paragraphs(&mut paragraphs, &heading_map);
        assert_eq!(paragraphs[0].heading_level, Some(2));
    }

    #[test]
    fn test_section_numbering_blocked_by_layout_text() {
        let heading_map = vec![(12.0, None)];
        let mut para = make_text_paragraph(12.0, "3 Processing pipeline", false);
        para.layout_class = Some(super::super::types::LayoutHintClass::Text);
        let mut paragraphs = vec![para];
        classify_paragraphs(&mut paragraphs, &heading_map);
        assert_eq!(paragraphs[0].heading_level, None);
    }

    #[test]
    fn test_section_numbering_promotes_bold_at_body_size() {
        let heading_map = vec![(12.0, None)];
        let mut paragraphs = vec![make_text_paragraph(12.0, "3 Processing pipeline", true)];
        classify_paragraphs(&mut paragraphs, &heading_map);
        assert_eq!(paragraphs[0].heading_level, Some(2));
    }

    #[test]
    fn test_section_numbering_subsection_bold() {
        let heading_map = vec![(12.0, None)];
        let mut paragraphs = vec![make_text_paragraph(12.0, "3.2 AI models", true)];
        classify_paragraphs(&mut paragraphs, &heading_map);
        assert_eq!(paragraphs[0].heading_level, Some(3));
    }

    #[test]
    fn test_bold_heading_with_heading_cluster_keeps_cluster_level() {
        let heading_map = vec![(18.0, Some(1)), (12.0, None)];
        let mut paragraphs = vec![make_text_paragraph(18.0, "Title", true)];
        classify_paragraphs(&mut paragraphs, &heading_map);
        assert_eq!(paragraphs[0].heading_level, Some(1));
    }

    #[test]
    fn test_formula_detection_math_symbols() {
        let heading_map = vec![(12.0, None)];
        let mut paragraphs = vec![make_text_paragraph(12.0, "∑ x∈S f(x) ≤ ∞", false)];
        classify_paragraphs(&mut paragraphs, &heading_map);
        assert!(paragraphs[0].is_formula, "should detect formula with math symbols");
        assert_eq!(paragraphs[0].heading_level, None);
    }

    #[test]
    fn test_formula_detection_greek_letters() {
        let heading_map = vec![(12.0, None)];
        let mut paragraphs = vec![make_text_paragraph(12.0, "α + β = γ", false)];
        classify_paragraphs(&mut paragraphs, &heading_map);
        assert!(paragraphs[0].is_formula, "should detect formula with Greek letters");
    }

    #[test]
    fn test_formula_detection_not_regular_text() {
        let heading_map = vec![(12.0, None)];
        let mut paragraphs = vec![make_text_paragraph(
            12.0,
            "This is a normal paragraph with regular text.",
            false,
        )];
        classify_paragraphs(&mut paragraphs, &heading_map);
        assert!(!paragraphs[0].is_formula, "normal text should not be a formula");
    }

    #[test]
    fn test_formula_detection_skips_headings() {
        let heading_map = vec![(18.0, Some(1)), (12.0, None)];
        let mut paragraphs = vec![make_text_paragraph(18.0, "∑ Results", false)];
        classify_paragraphs(&mut paragraphs, &heading_map);
        assert_eq!(paragraphs[0].heading_level, Some(1));
    }

    #[test]
    fn test_formula_detection_ascii_operators() {
        let heading_map = vec![(12.0, None)];
        let mut paragraphs = vec![make_text_paragraph(12.0, "a2 + 8 = 12", false)];
        classify_paragraphs(&mut paragraphs, &heading_map);
        assert!(paragraphs[0].is_formula, "ASCII equation should be a formula");
    }

    #[test]
    fn test_formula_detection_superscript_exponent() {
        let heading_map = vec![(12.0, None)];
        let mut paragraphs = vec![make_text_paragraph(12.0, "a\u{00B2} + 8 = 12", false)];
        classify_paragraphs(&mut paragraphs, &heading_map);
        assert!(
            paragraphs[0].is_formula,
            "superscript exponent equation should be a formula"
        );
    }

    #[test]
    fn test_formula_detection_prose_with_equals_not_flagged() {
        let heading_map = vec![(12.0, None)];
        let mut paragraphs = vec![make_text_paragraph(
            12.0,
            "The default setting is size = large for all new documents created here.",
            false,
        )];
        classify_paragraphs(&mut paragraphs, &heading_map);
        assert!(!paragraphs[0].is_formula, "prose with a single = must stay prose");
    }

    #[test]
    fn test_formula_detection_high_density() {
        let heading_map = vec![(12.0, None)];
        let mut paragraphs = vec![make_text_paragraph(12.0, "x→y", false)];
        classify_paragraphs(&mut paragraphs, &heading_map);
        assert!(
            paragraphs[0].is_formula,
            "high density of math chars should trigger formula"
        );
    }

    #[test]
    fn test_is_math_character() {
        assert!(is_math_character(''));
        assert!(is_math_character(''));
        assert!(is_math_character('α'));
        assert!(is_math_character('Ω'));
        assert!(is_math_character(''));
        assert!(is_math_character(''));
        assert!(!is_math_character('a'));
        assert!(!is_math_character('1'));
        // '+', '=' and '^' are math characters: `a2 + 8 = 12` must classify as a
        // formula (test_formula_detection_ascii_operators). ~keep
        assert!(is_math_character('+'));
        assert!(is_math_character('='));
        assert!(is_math_character('^'));
    }

    #[test]
    fn test_is_greek_letter() {
        assert!(is_greek_letter('α'));
        assert!(is_greek_letter('ω'));
        assert!(is_greek_letter('Α'));
        assert!(is_greek_letter('Ω'));
        assert!(!is_greek_letter('a'));
        assert!(!is_greek_letter('Z'));
    }

    #[test]
    fn test_rescue_pass_promotes_large_font_short_paragraph() {
        let heading_map = vec![(12.0, None)];
        let mut paragraphs = vec![make_text_paragraph(15.0, "Results and Discussion", false)];
        classify_paragraphs(&mut paragraphs, &heading_map);
        assert_eq!(paragraphs[0].heading_level, Some(3));
    }

    #[test]
    fn test_rescue_pass_h1_for_very_large_font() {
        let heading_map = vec![(10.0, None)];
        let mut paragraphs = vec![make_text_paragraph(17.0, "Document Title", false)];
        classify_paragraphs(&mut paragraphs, &heading_map);
        assert_eq!(paragraphs[0].heading_level, Some(1));
    }

    #[test]
    fn test_rescue_pass_h3_for_slightly_larger_font() {
        let heading_map = vec![(12.0, None)];
        let mut paragraphs = vec![make_text_paragraph(13.0, "Methods", false)];
        classify_paragraphs(&mut paragraphs, &heading_map);
        assert_eq!(paragraphs[0].heading_level, Some(3));
    }

    #[test]
    fn test_rescue_pass_skips_long_paragraphs() {
        let heading_map = vec![(12.0, None)];
        let mut paragraphs = vec![make_text_paragraph(
            15.0,
            "This is a very long paragraph that has way too many words",
            false,
        )];
        classify_paragraphs(&mut paragraphs, &heading_map);
        assert_eq!(paragraphs[0].heading_level, None);
    }

    #[test]
    fn test_rescue_pass_skips_period_ending() {
        let heading_map = vec![(12.0, None)];
        let mut paragraphs = vec![make_text_paragraph(15.0, "Some text here.", false)];
        classify_paragraphs(&mut paragraphs, &heading_map);
        assert_eq!(paragraphs[0].heading_level, None);
    }

    #[test]
    fn test_rescue_pass_skips_list_items() {
        let heading_map = vec![(12.0, None)];
        let mut para = make_text_paragraph(15.0, "Item One", false);
        para.is_list_item = true;
        let mut paragraphs = vec![para];
        classify_paragraphs(&mut paragraphs, &heading_map);
        assert_eq!(paragraphs[0].heading_level, None);
    }

    #[test]
    fn test_rescue_pass_skips_body_font_size() {
        let heading_map = vec![(12.0, None)];
        let mut paragraphs = vec![make_text_paragraph(12.0, "Methods", false)];
        classify_paragraphs(&mut paragraphs, &heading_map);
        assert_eq!(paragraphs[0].heading_level, None);
    }

    #[test]
    fn test_rescue_pass_skips_lowercase_start() {
        let heading_map = vec![(12.0, None)];
        let mut paragraphs = vec![make_text_paragraph(15.0, "is necessary to address", false)];
        classify_paragraphs(&mut paragraphs, &heading_map);
        assert_eq!(
            paragraphs[0].heading_level, None,
            "lowercase-starting fragment should not be promoted"
        );
    }

    #[test]
    fn test_rescue_pass_skips_continuation_word_the() {
        let heading_map = vec![(12.0, None)];
        let mut paragraphs = vec![make_text_paragraph(15.0, "The unsafe condition", false)];
        classify_paragraphs(&mut paragraphs, &heading_map);
        assert_eq!(
            paragraphs[0].heading_level, None,
            "continuation word 'The' should not be promoted"
        );
    }

    #[test]
    fn test_rescue_pass_skips_continuation_word_and() {
        let heading_map = vec![(12.0, None)];
        let mut paragraphs = vec![make_text_paragraph(15.0, "And furthermore", false)];
        classify_paragraphs(&mut paragraphs, &heading_map);
        assert_eq!(
            paragraphs[0].heading_level, None,
            "continuation word 'And' should not be promoted"
        );
    }

    #[test]
    fn test_rescue_pass_allows_proper_heading() {
        let heading_map = vec![(12.0, None)];
        let mut paragraphs = vec![make_text_paragraph(15.0, "Results and Discussion", false)];
        classify_paragraphs(&mut paragraphs, &heading_map);
        assert_eq!(
            paragraphs[0].heading_level,
            Some(3),
            "proper heading should still be promoted"
        );
    }

    #[test]
    fn test_demote_continuation_heading_after_unterminated_paragraph() {
        let heading_map = vec![(12.0, None)];
        let mut paragraphs = vec![
            make_text_paragraph(12.0, "the regulation requires that all operators", false),
            make_text_paragraph(15.0, "Safety Procedures", false),
        ];
        classify_paragraphs(&mut paragraphs, &heading_map);
        assert_eq!(
            paragraphs[1].heading_level, None,
            "heading after unterminated paragraph should be demoted"
        );
    }

    #[test]
    fn test_keep_heading_after_terminated_paragraph() {
        let heading_map = vec![(12.0, None)];
        let mut paragraphs = vec![
            make_text_paragraph(12.0, "The previous section covered the basics.", false),
            make_text_paragraph(15.0, "Safety Procedures", false),
        ];
        classify_paragraphs(&mut paragraphs, &heading_map);
        assert_eq!(
            paragraphs[1].heading_level,
            Some(3),
            "heading after terminated paragraph should stay"
        );
    }

    #[test]
    fn test_keep_heading_after_another_heading() {
        let heading_map = vec![(12.0, None)];
        let mut paragraphs = vec![
            make_text_paragraph(15.0, "Chapter One", false),
            make_text_paragraph(14.0, "Introduction", false),
        ];
        classify_paragraphs(&mut paragraphs, &heading_map);
        assert!(
            paragraphs[0].heading_level.is_some(),
            "first heading should be promoted"
        );
        assert!(
            paragraphs[1].heading_level.is_some(),
            "heading after heading should not be demoted by continuation check"
        );
    }

    #[test]
    fn test_starts_with_lowercase_or_continuation() {
        assert!(starts_with_lowercase_or_continuation("is necessary"));
        assert!(starts_with_lowercase_or_continuation("the quick fox"));
        assert!(starts_with_lowercase_or_continuation("The quick fox"));
        assert!(starts_with_lowercase_or_continuation("And furthermore"));
        assert!(starts_with_lowercase_or_continuation("But however"));
        assert!(starts_with_lowercase_or_continuation("Which means"));
        assert!(!starts_with_lowercase_or_continuation("Results"));
        assert!(!starts_with_lowercase_or_continuation("Safety Procedures"));
        assert!(!starts_with_lowercase_or_continuation("3 Methods"));
    }

    /// A 2-paragraph document is too sparse for the "no heading anywhere"
    /// fallback to trust a font-size difference: a larger opening line in a
    /// tiny document is as likely to be display prose (see `hello_structure.pdf`,
    /// `issue-987-test.pdf`) as an actual title. This replaces the previous
    /// `test_refine_promotes_first_largest_font_paragraph_to_h1`, which asserted
    /// promotion for this same 2-paragraph shape; that assertion encoded the
    /// over-promotion bug this sparsity gate fixes.
    #[test]
    fn test_refine_does_not_promote_sparse_two_paragraph_doc() {
        let mut pages = vec![vec![
            make_text_paragraph(18.0, "Annual Report", false),
            make_text_paragraph(12.0, "Some body text here for the document", false),
        ]];
        refine_heading_hierarchy(&mut pages);
        assert_eq!(
            pages[0][0].heading_level, None,
            "a 2-paragraph document must not force-promote its larger first line to h1"
        );
    }

    /// At and above the sparsity floor, a first paragraph that is both the
    /// largest font on the page and meaningfully larger than the rest of the
    /// document's text is still promoted to h1.
    #[test]
    fn test_refine_promotes_first_largest_font_paragraph_to_h1_at_floor() {
        let mut pages = vec![vec![
            make_text_paragraph(18.0, "Annual Report", false),
            make_text_paragraph(12.0, "Some body text here for the document", false),
            make_text_paragraph(12.0, "More body text in a second paragraph", false),
            make_text_paragraph(12.0, "Yet another paragraph of body text", false),
            make_text_paragraph(12.0, "A final paragraph rounding out the document", false),
        ]];
        refine_heading_hierarchy(&mut pages);
        assert_eq!(
            pages[0][0].heading_level,
            Some(1),
            "at the sparsity floor a clearly larger title line must still be promoted"
        );
    }

    #[test]
    fn test_refine_preserves_changelog_h2_and_promotes_adjacent_version_to_h3() {
        let mut title = make_text_paragraph(24.0, "Project Guide", false);
        title.heading_level = Some(1);
        let mut changelog = make_text_paragraph(16.0, "Recent Change Log", false);
        changelog.heading_level = Some(2);
        changelog.block_bbox = Some((50.0, 700.0, 210.0, 718.0));
        let mut version = make_text_paragraph(12.0, "v0.9.1", true);
        version.block_bbox = Some((50.5, 680.0, 105.0, 692.0));
        let body = make_text_paragraph(12.0, "Maintenance details follow.", true);
        let mut pages = vec![vec![title, changelog, version, body]];

        refine_heading_hierarchy(&mut pages);

        assert_eq!(pages[0][1].heading_level, Some(2));
        assert_eq!(pages[0][2].heading_level, Some(3));
        assert_eq!(pages[0][3].heading_level, None);
    }

    #[test]
    fn test_refine_splits_combined_changelog_and_version_heading() {
        let mut title = make_text_paragraph(24.0, "Project Guide", false);
        title.heading_level = Some(1);
        let mut combined = make_text_paragraph(16.0, "Recent Change Log v0.9.1", true);
        combined.text = "Recent Change Log v0.9.1".to_string();
        combined.block_bbox = Some((50.0, 664.0, 210.0, 696.0));
        let mut pages = vec![vec![title, combined]];

        refine_heading_hierarchy(&mut pages);

        assert_eq!(pages[0].len(), 3);
        assert_eq!(pages[0][1].text, "Recent Change Log");
        assert_eq!(pages[0][1].heading_level, Some(2));
        assert_eq!(pages[0][2].text, "v0.9.1");
        assert_eq!(pages[0][2].heading_level, Some(3));
        assert!(!pages[0][2].is_list_item);
        assert!(!pages[0][2].is_code_block);
        assert!(!pages[0][2].is_formula);
        assert!(!pages[0][2].is_page_furniture);
        assert_eq!(pages[0][1].block_bbox, Some((50.0, 680.0, 210.0, 696.0)));
        assert_eq!(pages[0][2].block_bbox, Some((50.0, 664.0, 210.0, 680.0)));
    }

    #[test]
    fn test_refine_splits_combined_changelog_without_preexisting_h1() {
        let mut combined = make_text_paragraph(16.0, "Recent Change Log v0.9.1", true);
        combined.text = "Recent Change Log v0.9.1".to_string();
        combined.block_bbox = Some((50.0, 664.0, 210.0, 696.0));
        let mut pages = vec![vec![
            combined,
            make_text_paragraph(12.0, "First body paragraph.", false),
            make_text_paragraph(12.0, "Second body paragraph.", false),
            make_text_paragraph(12.0, "Third body paragraph.", false),
            make_text_paragraph(12.0, "Fourth body paragraph.", false),
        ]];

        refine_heading_hierarchy(&mut pages);

        assert_eq!(pages[0][0].heading_level, Some(2));
        assert_eq!(pages[0][1].heading_level, Some(3));
        assert!(
            pages[0].iter().all(|paragraph| paragraph.heading_level != Some(1)),
            "a synthetic changelog hierarchy must not create an unrelated h1"
        );
    }

    #[test]
    fn test_combined_changelog_split_rejects_ordinary_one_line_heading() {
        let mut combined = make_text_paragraph(16.0, "Recent Change Log v0.9.1", true);
        combined.text = "Recent Change Log v0.9.1".to_string();
        combined.block_bbox = Some((50.0, 680.0, 310.0, 696.0));
        let mut pages = vec![vec![combined]];

        split_combined_changelog_version_headings(&mut pages);

        assert_eq!(pages[0].len(), 1);
        assert_eq!(pages[0][0].text, "Recent Change Log v0.9.1");
        assert_eq!(pages[0][0].heading_level, None);
    }

    #[test]
    fn test_combined_changelog_split_accepts_distinct_line_baselines() {
        let mut combined = make_text_paragraph(16.0, "Recent Change Log v0.9.1", true);
        combined.text = "Recent Change Log v0.9.1".to_string();
        let mut version_line = combined.lines[0].clone();
        version_line.baseline_y = 680.0;
        version_line.segments[0].y = 680.0;
        version_line.segments[0].baseline_y = 680.0;
        combined.lines.push(version_line);
        let mut pages = vec![vec![combined]];

        split_combined_changelog_version_headings(&mut pages);

        assert_eq!(pages[0].len(), 2);
        assert_eq!(pages[0][0].heading_level, Some(2));
        assert_eq!(pages[0][1].heading_level, Some(3));
        assert!(is_changelog_version_pair(&pages[0][0], &pages[0][1]));
    }

    #[test]
    fn test_combined_changelog_split_requires_geometry_and_plain_heading_state() {
        let make_combined = || {
            let mut paragraph = make_text_paragraph(16.0, "Recent Change Log v0.9.1", true);
            paragraph.text = "Recent Change Log v0.9.1".to_string();
            paragraph
        };

        let mut without_geometry = vec![vec![make_combined()]];
        split_combined_changelog_version_headings(&mut without_geometry);
        assert_eq!(without_geometry[0].len(), 1);

        for invalid_state in 0..4 {
            let mut paragraph = make_combined();
            paragraph.block_bbox = Some((50.0, 680.0, 210.0, 696.0));
            match invalid_state {
                0 => paragraph.is_list_item = true,
                1 => paragraph.is_code_block = true,
                2 => paragraph.is_formula = true,
                3 => paragraph.is_page_furniture = true,
                _ => unreachable!(),
            }
            let mut pages = vec![vec![paragraph]];
            split_combined_changelog_version_headings(&mut pages);
            assert_eq!(pages[0].len(), 1);
        }
    }

    #[test]
    fn test_standalone_recent_change_log_title_can_be_promoted_to_h1() {
        let mut pages = vec![vec![
            make_text_paragraph(18.0, "Recent Change Log", false),
            make_text_paragraph(12.0, "First body paragraph.", false),
            make_text_paragraph(12.0, "Second body paragraph.", false),
            make_text_paragraph(12.0, "Third body paragraph.", false),
            make_text_paragraph(12.0, "Fourth body paragraph.", false),
        ]];

        refine_heading_hierarchy(&mut pages);

        assert_eq!(pages[0][0].heading_level, Some(1));
    }

    #[test]
    fn test_refine_does_not_promote_distant_or_unaligned_changelog_versions() {
        let mut title = make_text_paragraph(24.0, "Project Guide", false);
        title.heading_level = Some(1);
        let mut changelog = make_text_paragraph(16.0, "Recent Change Log", false);
        changelog.heading_level = Some(2);
        changelog.block_bbox = Some((50.0, 700.0, 210.0, 718.0));
        let mut distant_version = make_text_paragraph(12.0, "1.2.3", true);
        distant_version.block_bbox = Some((50.0, 600.0, 100.0, 612.0));
        let mut second_changelog = changelog.clone();
        second_changelog.block_bbox = Some((50.0, 700.0, 210.0, 718.0));
        let mut unaligned_version = make_text_paragraph(12.0, "v2.0", true);
        unaligned_version.block_bbox = Some((100.0, 680.0, 150.0, 692.0));
        let mut pages = vec![
            vec![title, changelog, distant_version],
            vec![second_changelog, unaligned_version],
        ];

        refine_heading_hierarchy(&mut pages);

        assert_eq!(pages[0][1].heading_level, Some(2));
        assert_eq!(pages[0][2].heading_level, None);
        assert_eq!(pages[1][0].heading_level, Some(2));
        assert_eq!(pages[1][1].heading_level, None);
    }

    #[test]
    fn test_semantic_version_match_is_bounded_to_two_or_three_numeric_parts() {
        assert!(is_semantic_version("0.9"));
        assert!(is_semantic_version("v1.2.3"));
        assert!(!is_semantic_version("1"));
        assert!(!is_semantic_version("1.2.3.4"));
        assert!(!is_semantic_version("version 1.2"));
    }

    #[test]
    fn test_is_all_caps_text_positive() {
        assert!(is_all_caps_text("DEPARTMENT OF TRANSPORTATION"));
        assert!(is_all_caps_text("AGENCY:"));
        assert!(is_all_caps_text("SUMMARY:"));
        assert!(is_all_caps_text("ACTION:"));
    }

    #[test]
    fn test_is_all_caps_text_negative() {
        assert!(!is_all_caps_text("Department of Transportation"));
        assert!(!is_all_caps_text("Some regular text here"));
        assert!(!is_all_caps_text("a"));
    }

    #[test]
    fn test_all_caps_short_promoted_to_h2() {
        let heading_map = vec![(12.0, None)];
        let mut paragraphs = vec![make_text_paragraph(12.0, "EXECUTIVE SUMMARY", false)];
        classify_paragraphs(&mut paragraphs, &heading_map);
        assert_eq!(
            paragraphs[0].heading_level,
            Some(2),
            "short ALL-CAPS should be promoted to H2"
        );
    }

    #[test]
    fn test_all_caps_longer_promoted_to_h3() {
        let heading_map = vec![(12.0, None)];
        let mut paragraphs = vec![make_text_paragraph(
            12.0,
            "DEPARTMENT OF TRANSPORTATION FEDERAL AVIATION ADMINISTRATION",
            false,
        )];
        classify_paragraphs(&mut paragraphs, &heading_map);
        assert_eq!(
            paragraphs[0].heading_level,
            Some(3),
            "longer ALL-CAPS should be promoted to H3"
        );
    }

    #[test]
    fn test_all_caps_with_colon_promoted() {
        let heading_map = vec![(12.0, None)];
        let mut paragraphs = vec![make_text_paragraph(12.0, "AGENCY:", false)];
        classify_paragraphs(&mut paragraphs, &heading_map);
        assert_eq!(
            paragraphs[0].heading_level,
            Some(2),
            "ALL-CAPS colon-ending label should be promoted"
        );
    }

    #[test]
    fn test_all_caps_single_word_no_colon_not_promoted() {
        let heading_map = vec![(12.0, None)];
        let mut paragraphs = vec![make_text_paragraph(12.0, "USA", false)];
        classify_paragraphs(&mut paragraphs, &heading_map);
        assert_eq!(
            paragraphs[0].heading_level, None,
            "single-word abbreviation should not be promoted"
        );
    }

    #[test]
    fn test_all_caps_too_long_not_promoted() {
        let heading_map = vec![(12.0, None)];
        let text = "THIS IS A VERY LONG ALL CAPS TEXT THAT HAS WAY TOO MANY WORDS TO BE A HEADING IN ANY DOCUMENT";
        let mut paragraphs = vec![make_text_paragraph(12.0, text, false)];
        classify_paragraphs(&mut paragraphs, &heading_map);
        assert_eq!(
            paragraphs[0].heading_level, None,
            "ALL-CAPS text >15 words should not be promoted"
        );
    }

    #[test]
    fn test_all_caps_list_item_not_promoted() {
        let heading_map = vec![(12.0, None)];
        let mut para = make_text_paragraph(12.0, "ITEM ONE", false);
        para.is_list_item = true;
        let mut paragraphs = vec![para];
        classify_paragraphs(&mut paragraphs, &heading_map);
        assert_eq!(
            paragraphs[0].heading_level, None,
            "ALL-CAPS list item should not be promoted"
        );
    }

    #[test]
    fn test_all_caps_larger_font_gets_higher_level() {
        let heading_map = vec![(12.0, None)];
        let mut paragraphs = vec![make_text_paragraph(18.0, "TITLE", false)];
        classify_paragraphs(&mut paragraphs, &heading_map);
        assert!(
            paragraphs[0].heading_level.is_some(),
            "large ALL-CAPS should be promoted"
        );
    }

    #[test]
    fn test_bold_all_caps_colon_promoted() {
        let heading_map = vec![(12.0, None)];
        let mut paragraphs = vec![make_text_paragraph(12.0, "ACTION:", true)];
        classify_paragraphs(&mut paragraphs, &heading_map);
        assert!(
            paragraphs[0].heading_level.is_some(),
            "bold ALL-CAPS with colon should be promoted"
        );
    }

    #[test]
    fn test_bold_mixed_case_colon_not_promoted() {
        let heading_map = vec![(12.0, None)];
        let mut paragraphs = vec![make_text_paragraph(12.0, "Agency:", true)];
        classify_paragraphs(&mut paragraphs, &heading_map);
        assert_eq!(
            paragraphs[0].heading_level, None,
            "bold mixed-case with colon should not be promoted"
        );
    }

    /// A title block that also appears as a running header on subsequent pages must
    /// NOT be marked as furniture on the first page where it occurs.
    /// Regression test for #917: MCID-tagged title dropped in markdown/html output.
    #[test]
    fn test_cross_page_repeating_text_preserves_first_occurrence() {
        let page_heights = vec![792.0_f32; 6];
        let title = "Analysis of Thermodynamic Properties";
        let mut pages: Vec<Vec<PdfParagraph>> = (0..6)
            .map(|_| vec![make_margin_body(title), make_body_center("body text here")])
            .collect();
        mark_cross_page_repeating_text(&mut pages, &page_heights);

        assert!(
            !pages[0][0].is_page_furniture,
            "first occurrence of title on page 0 must be preserved (issue #917)"
        );
        for (i, page) in pages.iter().enumerate().take(6).skip(1) {
            assert!(page[0].is_page_furniture, "page {i} running header should be furniture");
        }
        for (i, page) in pages.iter().enumerate().take(6) {
            assert!(
                !page[1].is_page_furniture,
                "body text on page {i} must not be furniture"
            );
        }
    }

    /// Short-text tier-2 detection must also exempt the first occurrence of repeating text.
    /// Regression test for #917: short title paragraphs dropped in markdown/html output.
    #[test]
    fn test_cross_page_repeating_short_text_preserves_first_occurrence() {
        let title = "Xberg Conference Proceedings 2024";
        let mut pages: Vec<Vec<PdfParagraph>> = (0..5)
            .map(|_| vec![make_margin_body(title), make_body_center("section body")])
            .collect();
        mark_cross_page_repeating_short_text(&mut pages);

        assert!(
            !pages[0][0].is_page_furniture,
            "first occurrence of short repeating title must be preserved (issue #917)"
        );
        for (i, page) in pages.iter().enumerate().take(5).skip(1) {
            assert!(
                page[0].is_page_furniture,
                "page {i} short repeating text should be furniture"
            );
        }
    }
}

#[cfg(test)]
mod numbered_section_heading_tests {
    use super::{ends_with_sentence_period, is_numbered_section_heading};

    #[test]
    fn multilevel_numbers_are_headings() {
        assert!(is_numbered_section_heading("3.2 Methods"));
        assert!(is_numbered_section_heading("3.2.1 Details"));
        assert!(is_numbered_section_heading("3.2. Methods"));
    }

    #[test]
    fn roman_markers_are_headings() {
        assert!(is_numbered_section_heading("IV. Results"));
        assert!(is_numbered_section_heading("II) Scope"));
        assert!(is_numbered_section_heading("I INTRODUCTION"));
    }

    #[test]
    fn single_number_with_all_caps_remainder_is_heading() {
        assert!(is_numbered_section_heading("1. INTRODUCTION"));
        assert!(is_numbered_section_heading("2) RELATED WORK"));
    }

    #[test]
    fn numbered_list_items_are_not_headings() {
        assert!(!is_numbered_section_heading("1. First point"));
        assert!(!is_numbered_section_heading("1. Énumération 1"));
        assert!(!is_numbered_section_heading("12) apples and oranges"));
        assert!(!is_numbered_section_heading("1.\nÉnumération 1"));
    }

    #[test]
    fn non_numbered_text_is_not_a_heading_marker() {
        assert!(!is_numbered_section_heading("Introduction"));
        assert!(!is_numbered_section_heading(""));
        assert!(!is_numbered_section_heading("1."));
    }

    #[test]
    fn sentence_period_detected_but_ellipsis_is_not() {
        // A genuine sentence-terminating period disqualifies a heading. ~keep
        assert!(ends_with_sentence_period("This is a sentence."));
        assert!(ends_with_sentence_period("Ends here.  "));
        // A trailing ellipsis is a truncation marker, not a terminator. ~keep
        assert!(!ends_with_sentence_period("Impaired Glucose Tolerance ..."));
        assert!(!ends_with_sentence_period("Thermal Comfort As Related To …"));
        assert!(!ends_with_sentence_period("No period at all"));
    }
}