browsy-core 0.1.1

Zero-render browser engine for AI agents — browsy.dev
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
use crate::css::{Display, Visibility};
use crate::dom::NodeType;
use crate::layout::LayoutNode;
use serde::{Serialize, Deserialize};
use std::collections::HashMap;

/// The Spatial DOM — the primary output of agentbrowser.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SpatialDom {
    pub url: String,
    pub title: String,
    pub vp: [f32; 2],
    pub scroll: [f32; 2],
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub suggested_actions: Vec<SuggestedAction>,
    #[serde(default, skip_serializing_if = "PageType::is_other")]
    pub page_type: PageType,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub captcha: Option<CaptchaInfo>,
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub blocked: Option<BlockedInfo>,
    pub els: Vec<SpatialElement>,
    /// O(1) lookup: element ID → index in `els`.
    #[serde(skip)]
    id_index: HashMap<u32, usize>,
}

/// CAPTCHA information detected on the page.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct CaptchaInfo {
    /// The type of CAPTCHA detected.
    pub captcha_type: CaptchaType,
    /// Site key for the CAPTCHA service (from data-sitekey attribute).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub sitekey: Option<String>,
}

/// Blocked / anti-bot guidance detected on the page.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct BlockedInfo {
    pub reason: String,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub signals: Vec<String>,
    #[serde(default, skip_serializing_if = "Vec::is_empty")]
    pub recommendations: Vec<String>,
    pub require_human: bool,
}

/// Known CAPTCHA types.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub enum CaptchaType {
    /// Google reCAPTCHA v2 or v3
    ReCaptcha,
    /// hCaptcha
    HCaptcha,
    /// Cloudflare Turnstile
    Turnstile,
    /// Cloudflare JS challenge ("Just a moment...")
    CloudflareChallenge,
    /// Custom image-grid CAPTCHA (select images matching a prompt)
    ImageGrid,
    /// Text-based CAPTCHA (type characters from an image)
    TextCaptcha,
    /// CAPTCHA detected but type is unknown
    Unknown,
}

/// A single element in the Spatial DOM.
#[derive(Debug, Serialize, Deserialize, Clone, PartialEq)]
pub struct SpatialElement {
    pub id: u32,
    pub tag: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub role: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub text: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub ph: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub href: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub val: Option<String>,
    #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
    pub input_type: Option<String>,
    /// ARIA: whether the element is disabled
    #[serde(skip_serializing_if = "Option::is_none")]
    pub disabled: Option<bool>,
    /// ARIA: whether the element is checked (checkbox/radio)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub checked: Option<bool>,
    /// ARIA: whether expanded (dropdown, accordion)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub expanded: Option<bool>,
    /// ARIA: whether selected (tab, option)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub selected: Option<bool>,
    /// ARIA: whether required
    #[serde(skip_serializing_if = "Option::is_none")]
    pub required: Option<bool>,
    /// HTML name attribute (for form fields: input, textarea, select)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    /// Associated label text (from `<label for="id">`)
    #[serde(skip_serializing_if = "Option::is_none")]
    pub label: Option<String>,
    /// Alert type: "alert", "status", "error", "success", "warning"
    #[serde(skip_serializing_if = "Option::is_none")]
    pub alert_type: Option<String>,
    /// Whether the element is hidden (display:none, visibility:hidden, aria-hidden, hidden attr).
    /// Hidden elements are still included so agents can see dropdown menus, accordion panels,
    /// modal content, tabs, etc. without JS execution.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub hidden: Option<bool>,
    /// Bounds: [x, y, width, height]
    pub b: [i32; 4],
}

/// A suggested action for the agent based on page content analysis.
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "action")]
pub enum SuggestedAction {
    Login {
        username_id: u32,
        password_id: u32,
        submit_id: u32,
        #[serde(skip_serializing_if = "Option::is_none")]
        remember_me_id: Option<u32>,
    },
    EnterCode {
        input_id: u32,
        submit_id: u32,
        #[serde(skip_serializing_if = "Option::is_none")]
        code_length: Option<usize>,
    },
    Search {
        input_id: u32,
        submit_id: u32,
    },
    Consent {
        approve_ids: Vec<u32>,
        deny_ids: Vec<u32>,
    },
    SelectFromList {
        items: Vec<u32>,
    },
    CookieConsent {
        accept_id: u32,
        #[serde(skip_serializing_if = "Option::is_none")]
        reject_id: Option<u32>,
    },
    Paginate {
        #[serde(skip_serializing_if = "Option::is_none")]
        next_id: Option<u32>,
        #[serde(skip_serializing_if = "Option::is_none")]
        prev_id: Option<u32>,
    },
    Register {
        #[serde(skip_serializing_if = "Option::is_none")]
        email_id: Option<u32>,
        #[serde(skip_serializing_if = "Option::is_none")]
        username_id: Option<u32>,
        password_id: u32,
        #[serde(skip_serializing_if = "Option::is_none")]
        confirm_password_id: Option<u32>,
        #[serde(skip_serializing_if = "Option::is_none")]
        name_id: Option<u32>,
        submit_id: u32,
    },
    Contact {
        #[serde(skip_serializing_if = "Option::is_none")]
        name_id: Option<u32>,
        #[serde(skip_serializing_if = "Option::is_none")]
        email_id: Option<u32>,
        message_id: u32,
        submit_id: u32,
    },
    FillForm {
        fields: Vec<FormField>,
        submit_id: u32,
    },
    CaptchaChallenge {
        captcha_type: CaptchaType,
        #[serde(skip_serializing_if = "Option::is_none")]
        sitekey: Option<String>,
        #[serde(skip_serializing_if = "Option::is_none")]
        submit_id: Option<u32>,
    },
    RetryGuidance {
        reason: String,
        recommendations: Vec<String>,
        require_human: bool,
    },
    Download {
        items: Vec<DownloadItem>,
    },
}

/// A downloadable item detected on the page.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DownloadItem {
    pub id: u32,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub text: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub href: Option<String>,
}

/// A labeled form field for the FillForm action recipe.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FormField {
    pub id: u32,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub label: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub name: Option<String>,
    #[serde(rename = "type", skip_serializing_if = "Option::is_none")]
    pub input_type: Option<String>,
}

impl SpatialDom {
    /// Deserialize from JSON and rebuild the ID index.
    pub fn from_json(json: &str) -> Result<Self, serde_json::Error> {
        let mut dom: SpatialDom = serde_json::from_str(json)?;
        dom.rebuild_index();
        Ok(dom)
    }

    /// O(1) element lookup by ID.
    pub fn get(&self, id: u32) -> Option<&SpatialElement> {
        self.id_index.get(&id).map(|&idx| &self.els[idx])
    }

    /// Rebuild the ID index (call after mutating `els`).
    pub fn rebuild_index(&mut self) {
        self.id_index = self.els.iter().enumerate().map(|(i, e)| (e.id, i)).collect();
    }

    /// Return only visible (non-hidden) elements.
    pub fn visible(&self) -> Vec<&SpatialElement> {
        self.els.iter().filter(|e| e.hidden != Some(true)).collect()
    }

    /// Return elements whose top edge is within the viewport (above the fold).
    pub fn above_fold(&self) -> Vec<&SpatialElement> {
        let fold_y = self.vp[1] as i32;
        self.els.iter().filter(|e| e.b[1] < fold_y).collect()
    }

    /// Return elements whose top edge is below the viewport fold.
    pub fn below_fold(&self) -> Vec<&SpatialElement> {
        let fold_y = self.vp[1] as i32;
        self.els.iter().filter(|e| e.b[1] >= fold_y).collect()
    }

    /// Return a new SpatialDom with only above-fold elements (for token-limited contexts).
    pub fn filter_above_fold(&self) -> SpatialDom {
        let fold_y = self.vp[1] as i32;
        let els: Vec<SpatialElement> = self.els.iter().filter(|e| e.b[1] < fold_y).cloned().collect();
        let id_index = els.iter().enumerate().map(|(i, e)| (e.id, i)).collect();
        SpatialDom {
            url: self.url.clone(),
            title: self.title.clone(),
            vp: self.vp,
            scroll: self.scroll,
            suggested_actions: self.suggested_actions.clone(),
            page_type: self.page_type.clone(),
            captcha: self.captcha.clone(),
            blocked: self.blocked.clone(),
            els,
            id_index,
        }
    }
}

/// Tags that are always interactive.
const INTERACTIVE_TAGS: &[&str] = &[
    "a", "button", "input", "select", "textarea", "details", "summary",
];

/// Tags that bear meaningful text content for the agent.
const TEXT_TAGS: &[&str] = &[
    "h1", "h2", "h3", "h4", "h5", "h6", "p", "label", "span", "li", "td", "th", "dt", "dd",
    "figcaption", "blockquote", "pre", "code", "em", "strong", "b", "i", "mark", "small",
];

/// Tags that commonly wrap interactive elements (dedup candidates).
const WRAPPER_TAGS: &[&str] = &["li", "td", "th", "span", "p", "dt", "dd"];

/// Landmark tags — emitted as structural markers (role only, no recursive text).
const LANDMARK_TAGS: &[&str] = &["nav", "main", "header", "footer", "aside", "section", "form"];

/// Generate the Spatial DOM from a laid-out tree.
pub fn generate_spatial_dom(
    root: &LayoutNode,
    viewport_width: f32,
    viewport_height: f32,
) -> SpatialDom {
    let mut els = Vec::new();
    let mut id_counter = 1u32;

    // Collect label associations: HTML id -> label text
    let label_map = collect_label_associations(root);

    collect_elements(root, &mut els, &mut id_counter, false, &label_map);

    // Extract title from the tree
    let title = find_title(root).unwrap_or_default();

    // Scan the layout tree for CAPTCHA signals before building the SpatialDom.
    // This must happen before detect_page_type since CAPTCHA detection uses these signals.
    let captcha = detect_captcha_from_tree(root);

    let id_index = els.iter().enumerate().map(|(i, e)| (e.id, i)).collect();
    let mut dom = SpatialDom {
        url: String::new(), // Set by caller
        title,
        vp: [viewport_width, viewport_height],
        scroll: [0.0, 0.0],
        suggested_actions: Vec::new(),
        page_type: PageType::Other,
        captcha,
        blocked: None,
        els,
        id_index,
    };

    // Detect page type and suggested actions
    dom.blocked = detect_blocked_info(&dom);
    dom.page_type = detect_page_type(&dom);
    dom.suggested_actions = detect_suggested_actions(&dom);

    dom
}

/// Walk the tree to find <label for="xxx"> elements and map input IDs to label text.
fn collect_label_associations(root: &LayoutNode) -> std::collections::HashMap<String, String> {
    let mut map = std::collections::HashMap::new();
    collect_labels_recursive(root, &mut map);
    map
}

fn collect_labels_recursive(node: &LayoutNode, map: &mut std::collections::HashMap<String, String>) {
    if node.tag == "label" {
        if let Some(for_id) = node.attributes.get("for") {
            let text = if !node.text_content.is_empty() {
                node.text_content.clone()
            } else {
                collect_visible_text(node)
            };
            if !text.is_empty() {
                map.insert(for_id.clone(), text);
            }
        }
    }
    for child in &node.children {
        collect_labels_recursive(child, map);
    }
}

/// Resolve all relative URLs in the SpatialDom against a base URL.
pub fn resolve_urls(dom: &mut SpatialDom, base_url: &str) {
    // Try to parse base URL; if invalid, skip resolution
    let base = match url::Url::parse(base_url) {
        Ok(u) => u,
        Err(_) => return,
    };

    for el in &mut dom.els {
        if let Some(ref href) = el.href {
            // Skip already-absolute URLs, javascript:, mailto:, tel:, data:, #anchors
            if href.starts_with("http://")
                || href.starts_with("https://")
                || href.starts_with("javascript:")
                || href.starts_with("mailto:")
                || href.starts_with("tel:")
                || href.starts_with("data:")
                || href.starts_with('#')
            {
                continue;
            }
            // Resolve relative URL
            if let Ok(resolved) = base.join(href) {
                el.href = Some(resolved.to_string());
            }
        }
    }
}

fn collect_elements(
    node: &LayoutNode,
    els: &mut Vec<SpatialElement>,
    id_counter: &mut u32,
    parent_hidden: bool,
    label_map: &std::collections::HashMap<String, String>,
) {
    // aria-hidden="true" hides the element and all children
    let aria_hidden = node
        .attributes
        .get("aria-hidden")
        .map(|v| v == "true")
        .unwrap_or(false);

    // Determine if this node is hidden (cascades to children)
    let is_hidden = parent_hidden
        || node.style.display == Display::None
        || node.style.visibility == Visibility::Hidden
        || aria_hidden
        || node.attributes.contains_key("hidden");

    // Skip zero-size visible elements (layout artifacts, not meaningful content)
    if !is_hidden
        && node.bounds.width <= 0.0
        && node.bounds.height <= 0.0
        && node.node_type == NodeType::Element
    {
        for child in &node.children {
            collect_elements(child, els, id_counter, is_hidden, label_map);
        }
        return;
    }

    let tag = node.tag.as_str();
    let is_interactive = INTERACTIVE_TAGS.contains(&tag)
        || node.attributes.contains_key("onclick")
        || node.attributes.contains_key("role")
        || node.attributes.get("tabindex").is_some();

    let is_text = TEXT_TAGS.contains(&tag);
    let has_role = node.attributes.contains_key("role");
    let is_landmark = LANDMARK_TAGS.contains(&tag);

    // Image with alt text
    let is_img_with_alt = tag == "img" && node.attributes.contains_key("alt");

    let should_emit = is_interactive || is_text || has_role || is_img_with_alt || is_landmark;

    if should_emit {
        // Landmarks: emit as structural markers with role only, no recursive text.
        // Children carry the actual content — avoid duplicating it in the parent.
        let is_landmark_role = is_landmark || is_landmark_role_attr(node);
        if is_landmark_role {
            // Emit with empty text (role-only marker)
            emit_element(node, els, id_counter, Some(String::new()), is_hidden, label_map);
            for child in &node.children {
                collect_elements(child, els, id_counter, is_hidden, label_map);
            }
            return;
        }

        // Skip text-only elements with trivial content (just punctuation/separators)
        if is_text && !is_interactive && !has_role {
            let text_content = if !node.text_content.is_empty() {
                &node.text_content
            } else {
                ""
            };
            if is_trivial_text(text_content) {
                for child in &node.children {
                    collect_elements(child, els, id_counter, is_hidden, label_map);
                }
                return;
            }
        }
        // Deduplication: for wrapper tags that only wrap interactive children,
        // skip the wrapper and let the children carry the text.
        let has_interactive = has_interactive_descendants(node);
        let is_wrapper = WRAPPER_TAGS.contains(&tag);
        let should_dedup = !is_interactive && has_interactive
            && (is_wrapper || is_text);

        if should_dedup {
            let own_text = collect_own_text(node);
            if own_text.is_empty() || is_trivial_text(&own_text) {
                for child in &node.children {
                    collect_elements(child, els, id_counter, is_hidden, label_map);
                }
                return;
            }
            emit_element(node, els, id_counter, Some(own_text), is_hidden, label_map);
        } else {
            emit_element(node, els, id_counter, None, is_hidden, label_map);
        }
    }

    // Recurse into children
    for child in &node.children {
        collect_elements(child, els, id_counter, is_hidden, label_map);
    }
}

fn emit_element(
    node: &LayoutNode,
    els: &mut Vec<SpatialElement>,
    id_counter: &mut u32,
    text_override: Option<String>,
    is_hidden: bool,
    label_map: &std::collections::HashMap<String, String>,
) {
    let tag = node.tag.as_str();

    let text = if let Some(t) = text_override {
        if t.is_empty() { None } else { Some(t) }
    } else if tag == "img" {
        // Use alt text for images
        node.attributes.get("alt").cloned().filter(|s| !s.is_empty())
    } else {
        let text_content = if !node.text_content.is_empty() {
            node.text_content.clone()
        } else {
            collect_visible_text(node)
        };
        if !text_content.is_empty() {
            Some(text_content)
        } else {
            // Fallback chain for text-less interactive elements (links, buttons)
            node.attributes.get("aria-label").cloned().filter(|s| !s.is_empty())
                .or_else(|| node.attributes.get("title").cloned().filter(|s| !s.is_empty()))
                .or_else(|| find_child_img_alt(node))
        }
    };

    let role = determine_role(node);
    let ph = node.attributes.get("placeholder").cloned();
    let href = node.attributes.get("href").cloned();
    let val = node.attributes.get("value").cloned();
    let input_type = if tag == "input" {
        node.attributes.get("type").cloned()
    } else {
        None
    };

    let disabled = parse_bool_attr(node, "disabled")
        .or_else(|| parse_bool_attr(node, "aria-disabled"));
    let checked = parse_bool_attr(node, "checked")
        .or_else(|| parse_aria_bool(node, "aria-checked"));
    let expanded = parse_aria_bool(node, "aria-expanded");
    let selected = parse_bool_attr(node, "selected")
        .or_else(|| parse_aria_bool(node, "aria-selected"));
    let required = parse_bool_attr(node, "required")
        .or_else(|| parse_aria_bool(node, "aria-required"));

    // HTML name attribute for form fields
    let name = if matches!(tag, "input" | "select" | "textarea") {
        node.attributes.get("name").cloned()
    } else {
        None
    };

    // Associate label via <label for="id">
    let label = if matches!(tag, "input" | "select" | "textarea") {
        node.attributes.get("id")
            .and_then(|id| label_map.get(id))
            .cloned()
    } else {
        None
    };

    // Alert type detection from role or CSS classes
    let alert_type = detect_alert_type(node);

    let el = SpatialElement {
        id: *id_counter,
        tag: tag.to_string(),
        role,
        text,
        ph,
        href,
        val,
        input_type,
        disabled,
        checked,
        expanded,
        selected,
        required,
        name,
        label,
        alert_type,
        hidden: if is_hidden { Some(true) } else { None },
        b: [
            node.bounds.x.round() as i32,
            node.bounds.y.round() as i32,
            node.bounds.width.round() as i32,
            node.bounds.height.round() as i32,
        ],
    };

    *id_counter += 1;
    els.push(el);
}

/// Find alt text from child <img> or <title> from child <svg>.
/// Used as fallback for text-less links/buttons that contain only images or icons.
fn find_child_img_alt(node: &LayoutNode) -> Option<String> {
    for child in &node.children {
        // <img alt="...">
        if child.tag == "img" {
            if let Some(alt) = child.attributes.get("alt") {
                if !alt.is_empty() {
                    return Some(alt.clone());
                }
            }
        }
        // <svg> → aria-label (extracted from <title> during DOM parsing) or <title> descendant
        if child.tag == "svg" {
            if let Some(label) = child.attributes.get("aria-label") {
                if !label.is_empty() {
                    return Some(label.clone());
                }
            }
            if let Some(title) = find_svg_title(child) {
                return Some(title);
            }
        }
        // Recurse (e.g., <a><span><img alt="..."></span></a>)
        if let Some(alt) = find_child_img_alt(child) {
            return Some(alt);
        }
    }
    None
}

/// Find the text content of a <title> element inside an SVG.
fn find_svg_title(node: &LayoutNode) -> Option<String> {
    for child in &node.children {
        if child.tag == "title" {
            let text = collect_visible_text(child);
            if !text.is_empty() {
                return Some(text);
            }
        }
        if let Some(title) = find_svg_title(child) {
            return Some(title);
        }
    }
    None
}

/// ARIA landmark roles — elements with these roles should not collect recursive text.
const LANDMARK_ROLES: &[&str] = &[
    "navigation", "main", "banner", "contentinfo", "complementary", "region", "form",
];

/// Check if an element has an explicit ARIA role that is a landmark role.
fn is_landmark_role_attr(node: &LayoutNode) -> bool {
    node.attributes
        .get("role")
        .map(|r| LANDMARK_ROLES.contains(&r.as_str()))
        .unwrap_or(false)
}

/// Check if a node has any interactive descendants.
fn has_interactive_descendants(node: &LayoutNode) -> bool {
    for child in &node.children {
        let tag = child.tag.as_str();
        if INTERACTIVE_TAGS.contains(&tag)
            || child.attributes.contains_key("onclick")
            || child.attributes.contains_key("role")
            || child.attributes.get("tabindex").is_some()
        {
            return true;
        }
        if has_interactive_descendants(child) {
            return true;
        }
    }
    false
}

/// Collect text directly owned by this node, NOT from interactive or text-tag descendants.
/// This gives us text that wouldn't be captured by child elements emitted separately.
fn collect_own_text(node: &LayoutNode) -> String {
    let mut result = String::new();
    // Iterate children (not node itself) so the tag-based skip applies to children only
    for child in &node.children {
        collect_own_text_recursive(child, &mut result);
    }
    result.trim().to_string()
}

fn collect_own_text_recursive(node: &LayoutNode, out: &mut String) {
    if node.node_type == NodeType::Text {
        let t = node.text.trim();
        if !t.is_empty() {
            if !out.is_empty() && !out.ends_with(' ') {
                out.push(' ');
            }
            out.push_str(t);
        }
        return;
    }
    // Skip children that will be emitted as their own elements
    let tag = node.tag.as_str();
    if INTERACTIVE_TAGS.contains(&tag)
        || TEXT_TAGS.contains(&tag)
        || node.attributes.contains_key("onclick")
        || node.attributes.contains_key("role")
        || node.attributes.get("tabindex").is_some()
    {
        return;
    }
    for child in &node.children {
        collect_own_text_recursive(child, out);
    }
}

/// Check if text is trivial (only punctuation, separators, or whitespace).
/// These elements add noise without conveying meaningful content.
fn is_trivial_text(text: &str) -> bool {
    let trimmed = text.trim();
    if trimmed.is_empty() {
        return true;
    }
    // Skip if all chars are just separators/punctuation
    trimmed.chars().all(|c| matches!(c, '|' | '·' | '' | '-' | '' | '' | '/' | '\\' | ',' | '.' | ':' | ';' | '(' | ')' | '[' | ']' | '{' | '}' | ' ' | '\t' | '\n'))
}

fn collect_visible_text(node: &LayoutNode) -> String {
    let mut result = String::new();
    collect_text_recursive(node, &mut result);
    result.trim().to_string()
}

fn collect_text_recursive(node: &LayoutNode, out: &mut String) {
    if node.node_type == NodeType::Text {
        let t = node.text.trim();
        if !t.is_empty() {
            if !out.is_empty() && !out.ends_with(' ') {
                out.push(' ');
            }
            out.push_str(t);
        }
        return;
    }
    for child in &node.children {
        collect_text_recursive(child, out);
    }
}

fn determine_role(node: &LayoutNode) -> Option<String> {
    // Explicit ARIA role
    if let Some(role) = node.attributes.get("role") {
        return Some(role.clone());
    }

    // Implicit roles from tag
    match node.tag.as_str() {
        "a" => Some("link".to_string()),
        "button" => Some("button".to_string()),
        "input" => {
            let input_type = node
                .attributes
                .get("type")
                .map(|s| s.as_str())
                .unwrap_or("text");
            match input_type {
                "checkbox" => Some("checkbox".to_string()),
                "radio" => Some("radio".to_string()),
                "submit" | "button" => Some("button".to_string()),
                "search" => Some("searchbox".to_string()),
                _ => Some("textbox".to_string()),
            }
        }
        "select" => Some("combobox".to_string()),
        "textarea" => Some("textbox".to_string()),
        "h1" | "h2" | "h3" | "h4" | "h5" | "h6" => Some("heading".to_string()),
        "nav" => Some("navigation".to_string()),
        "main" => Some("main".to_string()),
        "aside" => Some("complementary".to_string()),
        "header" => Some("banner".to_string()),
        "footer" => Some("contentinfo".to_string()),
        "form" => Some("form".to_string()),
        "section" => Some("region".to_string()),
        "img" => Some("img".to_string()),
        _ => None,
    }
}

/// Parse a boolean HTML attribute (present = true).
fn parse_bool_attr(node: &LayoutNode, attr: &str) -> Option<bool> {
    if node.attributes.contains_key(attr) {
        Some(true)
    } else {
        None
    }
}

/// Parse an ARIA boolean attribute (value = "true" or "false").
fn parse_aria_bool(node: &LayoutNode, attr: &str) -> Option<bool> {
    node.attributes.get(attr).map(|v| v == "true")
}

fn find_title(node: &LayoutNode) -> Option<String> {
    if node.tag == "title" {
        let text = collect_visible_text(node);
        if !text.is_empty() {
            return Some(text);
        }
    }
    for child in &node.children {
        if let Some(title) = find_title(child) {
            return Some(title);
        }
    }
    None
}

/// Classify element width as a semantic size hint for form elements.
fn classify_size(width: i32, vp_width: f32) -> Option<&'static str> {
    let pct = width as f32 / vp_width * 100.0;
    if pct > 90.0 { Some("full") }
    else if pct > 50.0 { Some("wide") }
    else if pct < 15.0 && width > 0 { Some("narrow") }
    else { None }
}

/// Classify element position into a 3×3 grid region (or "below" if below fold).
fn classify_region(b: &[i32; 4], vp: &[f32; 2]) -> &'static str {
    let cy = b[1] as f32 + b[3] as f32 / 2.0;
    if cy > vp[1] { return "below"; }
    let cx = b[0] as f32 + b[2] as f32 / 2.0;
    let col = if cx < vp[0] / 3.0 { 0 } else if cx < vp[0] * 2.0 / 3.0 { 1 } else { 2 };
    let row = if cy < vp[1] / 3.0 { 0 } else if cy < vp[1] * 2.0 / 3.0 { 1 } else { 2 };
    const NAMES: [[&str; 3]; 3] = [
        ["top-L", "top", "top-R"],
        ["mid-L", "mid", "mid-R"],
        ["bot-L", "bot", "bot-R"],
    ];
    NAMES[row][col]
}

/// Generate the compact string format for extreme token budgets.
pub fn to_compact_string(dom: &SpatialDom) -> String {
    // Pre-pass: count (tag, text) tuples to detect duplicates needing disambiguation
    let mut tuple_counts: HashMap<(String, Option<String>), usize> = HashMap::new();
    for el in &dom.els {
        let key = (el.tag.clone(), el.text.clone());
        *tuple_counts.entry(key).or_insert(0) += 1;
    }

    let mut lines = Vec::new();
    for el in &dom.els {
        let mut parts = Vec::new();
        let hidden_marker = if el.hidden == Some(true) { "!" } else { "" };
        parts.push(format!("{}{}:{}", hidden_marker, el.id, el.tag));

        if let Some(ref t) = el.input_type {
            if t != "text" {
                parts.last_mut().unwrap().push_str(&format!(":{}", t));
            }
        }

        // Form state markers
        if let Some(ref n) = el.name {
            parts.push(format!("[{}]", n));
        }
        if el.checked == Some(true) {
            parts.push("[v]".to_string());
        }
        if el.required == Some(true) {
            parts.push("[*]".to_string());
        }
        if let Some(ref v) = el.val {
            if !v.is_empty() {
                parts.push(format!("[={}]", v));
            }
        }

        if let Some(ref text) = el.text {
            parts.push(format!("\"{}\"", text));
        } else if let Some(ref ph) = el.ph {
            parts.push(format!("\"{}\"", ph));
        }

        if let Some(ref href) = el.href {
            parts.push(format!("->{}", href));
        }

        // Size hint for form elements
        if matches!(el.tag.as_str(), "input" | "button" | "textarea" | "select") {
            if let Some(size) = classify_size(el.b[2], dom.vp[0]) {
                parts.push(size.to_string());
            }
        }

        // Region label only when duplicate (tag, text) tuples exist
        let key = (el.tag.clone(), el.text.clone());
        if tuple_counts.get(&key).copied().unwrap_or(0) > 1 {
            parts.push(format!("@{}", classify_region(&el.b, &dom.vp)));
        }

        lines.push(format!("[{}]", parts.join(" ")));
    }
    lines.join("\n")
}

/// Delta output — only the changes between two SpatialDoms.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct DeltaDom {
    /// Elements that were added or changed.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub changed: Vec<SpatialElement>,
    /// IDs of elements that were removed.
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub removed: Vec<u32>,
    /// Viewport dimensions for size hint computation.
    #[serde(default = "default_vp")]
    pub vp: [f32; 2],
}

fn default_vp() -> [f32; 2] {
    [1920.0, 1080.0]
}

/// Compute the diff between two SpatialDoms.
/// Returns only added/changed/removed elements.
pub fn diff(old: &SpatialDom, new: &SpatialDom) -> DeltaDom {
    let mut changed = Vec::new();
    let mut removed = Vec::new();

    // Build lookup of old elements by a content key (tag + text + href + bounds)
    // We match by content similarity, not by ID (since IDs are assigned sequentially)
    let old_set: std::collections::HashSet<ElementKey> = old.els.iter().map(ElementKey::from).collect();
    let new_set: std::collections::HashSet<ElementKey> = new.els.iter().map(ElementKey::from).collect();

    // Elements in new but not in old → added/changed
    for el in &new.els {
        let key = ElementKey::from(el);
        if !old_set.contains(&key) {
            changed.push(el.clone());
        }
    }

    // Elements in old but not in new → removed
    for el in &old.els {
        let key = ElementKey::from(el);
        if !new_set.contains(&key) {
            removed.push(el.id);
        }
    }

    DeltaDom { changed, removed, vp: new.vp }
}

/// Generate compact string format for a delta.
pub fn delta_to_compact_string(delta: &DeltaDom) -> String {
    let mut lines = Vec::new();

    if !delta.removed.is_empty() {
        lines.push(format!("-[{}]", delta.removed.iter().map(|id| id.to_string()).collect::<Vec<_>>().join(",")));
    }

    for el in &delta.changed {
        let mut parts = Vec::new();
        parts.push(format!("+{}:{}", el.id, el.tag));

        if let Some(ref t) = el.input_type {
            if t != "text" {
                parts.last_mut().unwrap().push_str(&format!(":{}", t));
            }
        }

        if let Some(ref text) = el.text {
            parts.push(format!("\"{}\"", text));
        } else if let Some(ref ph) = el.ph {
            parts.push(format!("\"{}\"", ph));
        }

        if let Some(ref href) = el.href {
            parts.push(format!("->{}", href));
        }

        // Size hint for form elements (no disambiguation in deltas — changed elements have unique +id:tag)
        if matches!(el.tag.as_str(), "input" | "button" | "textarea" | "select") {
            if let Some(size) = classify_size(el.b[2], delta.vp[0]) {
                parts.push(size.to_string());
            }
        }

        lines.push(format!("[{}]", parts.join(" ")));
    }

    lines.join("\n")
}

#[derive(Hash, PartialEq, Eq)]
struct ElementKey {
    tag: String,
    text: Option<String>,
    ph: Option<String>,
    href: Option<String>,
    input_type: Option<String>,
    bounds: [i32; 4],
}

impl From<&SpatialElement> for ElementKey {
    fn from(el: &SpatialElement) -> Self {
        Self {
            tag: el.tag.clone(),
            text: el.text.clone(),
            ph: el.ph.clone(),
            href: el.href.clone(),
            input_type: el.input_type.clone(),
            bounds: el.b,
        }
    }
}

// --- Alert detection ---

/// Detect alert type from role attributes or CSS class names.
fn detect_alert_type(node: &LayoutNode) -> Option<String> {
    // Check role attribute first
    if let Some(role) = node.attributes.get("role") {
        match role.as_str() {
            "alert" => return Some("alert".to_string()),
            "status" => return Some("status".to_string()),
            _ => {}
        }
    }

    // Check CSS classes for alert patterns.
    // Only match compound alert classes (e.g. "alert-error", "msg-danger", "flash-success")
    // to avoid false positives on generic classes like "error" used for non-alert purposes
    // (e.g. old Reddit uses class="error" on loading placeholder spans).
    if let Some(classes) = node.attributes.get("class") {
        let lower = classes.to_lowercase();
        let class_list: Vec<&str> = lower.split_whitespace().collect();
        let is_compound = |cls: &str| {
            cls.contains('-') || cls.contains('_')
                || cls.starts_with("alert") || cls.starts_with("msg")
        };

        for cls in &class_list {
            // Require compound patterns: "alert-error", "msg-error", "form-error", etc.
            // A bare "error" class is too ambiguous.
            if (cls.contains("error") || cls.contains("danger")) && is_compound(cls) {
                return Some("error".to_string());
            }
            if cls.contains("success") && is_compound(cls) {
                return Some("success".to_string());
            }
            if cls.contains("warning") && is_compound(cls) {
                return Some("warning".to_string());
            }
            // "alert" as a class by itself is typically intentional (Bootstrap, etc.)
            if *cls == "alert" || cls.starts_with("alert-") || cls.starts_with("alert_") {
                return Some("alert".to_string());
            }
            if cls.contains("notice") || cls.contains("flash") {
                return Some("alert".to_string());
            }
        }
    }

    None
}

impl SpatialDom {
    /// Return elements with an alert_type set.
    pub fn alerts(&self) -> Vec<&SpatialElement> {
        self.els.iter().filter(|e| e.alert_type.is_some()).collect()
    }
}

// --- Table extraction ---

/// Structured table data extracted from the Spatial DOM.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct TableData {
    pub headers: Vec<String>,
    pub rows: Vec<Vec<String>>,
}

impl SpatialDom {
    /// Extract structured table data from the Spatial DOM.
    /// Groups `th` elements as headers and `td` elements as row cells,
    /// using Y coordinates to determine row grouping.
    pub fn tables(&self) -> Vec<TableData> {
        // Collect all th and td elements
        let ths: Vec<&SpatialElement> = self.els.iter().filter(|e| e.tag == "th").collect();
        let tds: Vec<&SpatialElement> = self.els.iter().filter(|e| e.tag == "td").collect();

        if ths.is_empty() && tds.is_empty() {
            return Vec::new();
        }

        // Group headers by Y coordinate
        let headers = group_by_row(&ths);
        // Group data cells by Y coordinate
        let data_rows = group_by_row(&tds);

        // For simplicity, treat all th cells as a single header row
        // and all td cells as data rows. If there are multiple tables,
        // we'd need to cluster by X/Y proximity, but for now a single
        // table per page is the common case.
        let header_texts: Vec<String> = if !headers.is_empty() {
            headers[0].iter().map(|e| e.text.clone().unwrap_or_default()).collect()
        } else {
            Vec::new()
        };

        let row_data: Vec<Vec<String>> = data_rows
            .iter()
            .map(|row| row.iter().map(|e| e.text.clone().unwrap_or_default()).collect())
            .collect();

        if header_texts.is_empty() && row_data.is_empty() {
            return Vec::new();
        }

        vec![TableData {
            headers: header_texts,
            rows: row_data,
        }]
    }
}

/// Group elements into rows by Y coordinate (elements at the same Y = same row).
fn group_by_row<'a>(elements: &[&'a SpatialElement]) -> Vec<Vec<&'a SpatialElement>> {
    if elements.is_empty() {
        return Vec::new();
    }

    let mut sorted: Vec<&SpatialElement> = elements.to_vec();
    sorted.sort_by_key(|e| (e.b[1], e.b[0]));

    let mut rows: Vec<Vec<&'a SpatialElement>> = Vec::new();
    let mut current_row: Vec<&SpatialElement> = vec![sorted[0]];
    let mut current_y = sorted[0].b[1];

    for &el in &sorted[1..] {
        // Elements within 5px of the same Y are considered the same row
        if (el.b[1] - current_y).abs() <= 5 {
            current_row.push(el);
        } else {
            rows.push(current_row);
            current_row = vec![el];
            current_y = el.b[1];
        }
    }
    rows.push(current_row);
    rows
}

// --- Page type detection ---

/// Detected page type for agent decision-making.
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize, Default)]
pub enum PageType {
    Login,
    TwoFactorAuth,
    OAuthConsent,
    Captcha,
    Blocked,
    Search,
    SearchResults,
    Inbox,
    EmailBody,
    Dashboard,
    Form,
    Article,
    List,
    Error,
    #[default]
    Other,
}

impl PageType {
    pub fn is_other(&self) -> bool {
        matches!(self, PageType::Other)
    }
}

// --- Pagination detection ---

/// Pagination links detected on the page.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Pagination {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub next: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub prev: Option<String>,
    /// Numbered page links: (label, url)
    #[serde(skip_serializing_if = "Vec::is_empty")]
    pub pages: Vec<(String, String)>,
}

impl SpatialDom {
    /// Detect pagination links on the page.
    pub fn pagination(&self) -> Option<Pagination> {
        let links: Vec<&SpatialElement> = self.els.iter()
            .filter(|e| e.role.as_deref() == Some("link") && e.href.is_some())
            .collect();

        let mut next: Option<String> = None;
        let mut prev: Option<String> = None;
        let mut pages: Vec<(String, String)> = Vec::new();

        for link in &links {
            let text = link.text.as_deref().unwrap_or("").trim().to_lowercase();
            let href = link.href.as_deref().unwrap_or("");

            if text == "next" || text == "next page" || text == ">" || text == ">>"
                || text == "\u{203a}" || text == "\u{00bb}"
            {
                next = Some(href.to_string());
            } else if text == "previous" || text == "prev" || text == "prev page"
                || text == "<" || text == "<<"
                || text == "\u{2039}" || text == "\u{00ab}"
            {
                prev = Some(href.to_string());
            } else if text.chars().all(|c| c.is_ascii_digit()) && !text.is_empty() {
                pages.push((text.clone(), href.to_string()));
            }
        }

        if next.is_some() || prev.is_some() || !pages.is_empty() {
            Some(Pagination { next, prev, pages })
        } else {
            None
        }
    }
}

// --- Verification code extraction ---

impl SpatialDom {
    /// Extract verification codes (4-8 digit sequences) from page text near code-related keywords.
    pub fn find_codes(&self) -> Vec<String> {
        let code_keywords = [
            "verification code", "security code", "your code",
            "enter code", "otp", "passcode", "one-time",
        ];

        let mut codes = Vec::new();

        for el in &self.els {
            let text = match &el.text {
                Some(t) => t,
                None => continue,
            };

            let text_lower = text.to_lowercase();
            let has_keyword = code_keywords.iter().any(|kw| text_lower.contains(kw));

            // Proximity check: only match short label elements within 100px Y
            let near_keyword = if !has_keyword {
                let el_y = el.b[1];
                self.els.iter().any(|other| {
                    (other.b[1] - el_y).abs() < 100
                        && other.text.as_ref().map(|t| {
                            t.len() < 80 && {
                                let lower = t.to_lowercase();
                                code_keywords.iter().any(|kw| lower.contains(kw))
                            }
                        }).unwrap_or(false)
                })
            } else {
                false
            };

            if !has_keyword && !near_keyword {
                continue;
            }

            // Extract 4-8 digit sequences, filtering out year-like numbers
            let chars: Vec<char> = text.chars().collect();
            let mut i = 0;
            while i < chars.len() {
                if chars[i].is_ascii_digit() {
                    let start = i;
                    while i < chars.len() && chars[i].is_ascii_digit() {
                        i += 1;
                    }
                    let len = i - start;
                    if len >= 4 && len <= 8 {
                        let code: String = chars[start..i].iter().collect();
                        // Filter out year-like 4-digit numbers (1900-2099)
                        if len == 4 {
                            if let Ok(n) = code.parse::<u32>() {
                                if (1900..=2099).contains(&n) {
                                    continue;
                                }
                            }
                        }
                        if !codes.contains(&code) {
                            codes.push(code);
                        }
                    }
                } else {
                    i += 1;
                }
            }
        }

        codes
    }
}

// --- Search input matching ---

/// Check if an element is a search input (input or textarea used for search queries).
fn is_search_input(e: &SpatialElement) -> bool {
    let is_text_input = match e.tag.as_str() {
        "input" => !matches!(
            e.input_type.as_deref(),
            Some("checkbox") | Some("radio") | Some("hidden") | Some("submit")
            | Some("button") | Some("image") | Some("password")
        ),
        "textarea" => true,
        _ => return false,
    };
    if !is_text_input { return false; }

    e.input_type.as_deref() == Some("search")
        || e.role.as_deref() == Some("searchbox")
        || e.name.as_deref() == Some("q")
        || e.name.as_ref().map(|n| n.to_lowercase().contains("search")).unwrap_or(false)
        || e.ph.as_ref().map(|p| p.to_lowercase().contains("search")).unwrap_or(false)
        || e.label.as_ref().map(|l| l.to_lowercase().contains("search")).unwrap_or(false)
}

/// Find the ID of a visible email input field.
fn find_email_input_id(dom: &SpatialDom) -> Option<u32> {
    dom.els.iter().find(|e| {
        e.hidden != Some(true) && (
            e.input_type.as_deref() == Some("email")
            || e.name.as_ref().map(|n| {
                let lower = n.to_lowercase();
                lower == "email" || lower == "e-mail"
            }).unwrap_or(false)
        )
    }).map(|e| e.id)
}

fn detect_blocked_info(dom: &SpatialDom) -> Option<BlockedInfo> {
    let mut signals = Vec::new();

    let title_lower = dom.title.to_lowercase();
    let mut text = String::new();
    text.push_str(&title_lower);
    for e in &dom.els {
        if let Some(t) = &e.text {
            text.push(' ');
            text.push_str(&t.to_lowercase());
        }
        if let Some(l) = &e.label {
            text.push(' ');
            text.push_str(&l.to_lowercase());
        }
        if let Some(p) = &e.ph {
            text.push(' ');
            text.push_str(&p.to_lowercase());
        }
        if let Some(a) = &e.alert_type {
            text.push(' ');
            text.push_str(&a.to_lowercase());
        }
        if text.len() > 32_000 {
            break;
        }
    }

    let signal_terms = [
        ("captcha", "captcha"),
        ("verify you are human", "human_check"),
        ("verify you're human", "human_check"),
        ("unusual traffic", "unusual_traffic"),
        ("access denied", "access_denied"),
        ("forbidden", "forbidden"),
        ("blocked", "blocked"),
        ("bot detection", "bot_detection"),
        ("cloudflare", "cloudflare"),
        ("turnstile", "turnstile"),
        ("perimeterx", "perimeterx"),
        ("datadome", "datadome"),
        ("akamai", "akamai"),
        ("incapsula", "incapsula"),
        ("rate limit", "rate_limit"),
        ("too many requests", "rate_limit"),
    ];

    for (needle, label) in signal_terms.iter() {
        if text.contains(needle) && !signals.contains(&label.to_string()) {
            signals.push(label.to_string());
        }
    }

    if dom.captcha.is_some() && !signals.contains(&"captcha_service".to_string()) {
        signals.push("captcha_service".to_string());
    }

    if signals.is_empty() {
        return None;
    }

    let mut recommendations = Vec::new();
    if signals.iter().any(|s| s == "rate_limit") {
        recommendations.push("Back off and retry with exponential delay".to_string());
        recommendations.push("Reduce request rate for this host".to_string());
    }
    if signals.iter().any(|s| s == "captcha" || s == "captcha_service" || s == "turnstile" || s == "cloudflare") {
        recommendations.push("Ask a human to solve the challenge".to_string());
    }
    recommendations.push("Retry with a different user agent".to_string());
    recommendations.push("Try fetching only visible or above-fold content".to_string());
    recommendations.push("If authenticated, ensure cookies/session are set".to_string());

    let require_human = signals.iter().any(|s| {
        s == "captcha" || s == "captcha_service" || s == "turnstile" || s == "cloudflare"
    });

    let reason = if require_human {
        "captcha_or_challenge".to_string()
    } else if signals.iter().any(|s| s == "rate_limit") {
        "rate_limited".to_string()
    } else {
        "blocked".to_string()
    };

    Some(BlockedInfo {
        reason,
        signals,
        recommendations,
        require_human,
    })
}

// --- Page type detection ---

fn detect_page_type(dom: &SpatialDom) -> PageType {
    let title_lower = dom.title.to_lowercase();

    let title_has = |keywords: &[&str]| keywords.iter().any(|kw| title_lower.contains(kw));
    let heading_has = |keywords: &[&str]| {
        dom.els.iter().any(|e| {
            e.role.as_deref() == Some("heading")
                && e.text.as_ref().map(|t| {
                    let lower = t.to_lowercase();
                    keywords.iter().any(|kw| lower.contains(kw))
                }).unwrap_or(false)
        })
    };

    let visible_count = dom.els.iter().filter(|e| e.hidden != Some(true)).count();

    // Error
    let has_error_alerts = dom.els.iter().any(|e| {
        e.alert_type.as_deref() == Some("error")
    });
    let title_has_error = title_has(&["404", "500", "403", "not found", "error"]);
    if has_error_alerts || title_has_error {
        return PageType::Error;
    }

    // Captcha — multiple detection signals:
    // 1. Title/heading keywords
    // 2. CAPTCHA service detected in HTML structure (reCAPTCHA, hCaptcha, Turnstile, etc.)
    // 3. Cloudflare challenge page ("Just a moment...")
    let captcha_title_keywords = &[
        "captcha", "verify you're human", "verify you are human", "robot",
        "security check", "challenge", "just a moment",
        "attention required", "are you human",
    ];
    let has_captcha_title = title_has(captcha_title_keywords);
    let has_captcha_heading = heading_has(&[
        "captcha", "verify you're human", "security check", "are you human",
        "complete the challenge", "human verification",
    ]);
    let has_captcha_service = dom.captcha.is_some();
    if has_captcha_title || has_captcha_heading || has_captcha_service {
        return PageType::Captcha;
    }

    // Blocked / anti-bot (non-captcha)
    if dom.blocked.is_some() {
        return PageType::Blocked;
    }

    // Login
    let has_password = dom.els.iter().any(|e| {
        e.hidden != Some(true) && e.input_type.as_deref() == Some("password")
    });
    if has_password {
        return PageType::Login;
    }

    // TwoFactorAuth — use specific phrases to avoid false positives on programming content
    // (bare "code" matches any page about source code)
    let verification_keywords = &[
        "verification", "verify your", "enter code", "security code", "verification code",
        "2fa", "two-factor", "two factor", "otp", "one-time", "passcode",
    ];
    let has_verification_context = title_has(verification_keywords) || heading_has(verification_keywords);
    let has_code_input = dom.els.iter().any(|e| {
        e.hidden != Some(true) && e.tag == "input" && {
            let t = e.input_type.as_deref().unwrap_or("text");
            t == "text" || t == "number" || t == "tel"
        }
    });
    if has_verification_context && has_code_input {
        return PageType::TwoFactorAuth;
    }

    // OAuthConsent
    let oauth_keywords = &["authorize", "allow access", "grant permission", "oauth"];
    if title_has(oauth_keywords) || heading_has(oauth_keywords) {
        return PageType::OAuthConsent;
    }

    // Inbox
    let inbox_keywords = &["inbox", "mail", "messages"];
    let link_count = dom.els.iter()
        .filter(|e| e.hidden != Some(true) && e.role.as_deref() == Some("link"))
        .count();
    if title_has(inbox_keywords) && link_count >= 10 {
        return PageType::Inbox;
    }

    // EmailBody
    let email_markers = ["from:", "to:", "subject:", "date:"];
    let marker_count = email_markers.iter().filter(|marker| {
        dom.els.iter().any(|e| {
            e.text.as_ref().map(|t| t.to_lowercase().contains(*marker)).unwrap_or(false)
        })
    }).count();
    if marker_count >= 3 {
        return PageType::EmailBody;
    }

    // Dashboard
    let dashboard_keywords = &["dashboard", "welcome back", "overview"];
    let has_nav = dom.els.iter().any(|e| e.role.as_deref() == Some("navigation"));
    let has_main = dom.els.iter().any(|e| e.role.as_deref() == Some("main"));
    if (title_has(dashboard_keywords) || heading_has(dashboard_keywords)) && has_nav && has_main {
        return PageType::Dashboard;
    }

    // Article (before Search — many content pages have search bars)
    // When a page has many links (typical of list pages), require more long text to
    // classify as Article. This prevents content-heavy list pages (e.g. subreddits with
    // long post descriptions) from being misclassified.
    //
    // Key distinction: articles have lots of body text between headings (high paragraph-to-heading
    // ratio). List pages (news homepages) have many headings with short descriptions (low ratio).
    // Wikipedia: 56 headings, 102 long paragraphs (ratio ~1.8). BBC News: 61 headings, ~25 long
    // paragraphs (ratio ~0.4).
    let headings = dom.els.iter().filter(|e| e.role.as_deref() == Some("heading")).count();
    let long_texts = dom.els.iter().filter(|e| {
        e.tag == "p" && e.text.as_ref().map(|t| t.len() > 100).unwrap_or(false)
    }).count();
    let long_text_threshold = if link_count >= 20 { 10 } else { 2 };
    // When there are many headings (15+), require substantial body text per heading to
    // distinguish real articles (Wikipedia) from heading-heavy list pages (BBC News).
    let is_heading_heavy_list = headings >= 15 && link_count >= 10
        && (long_texts as f64) < (headings as f64) * 0.8;
    if headings >= 3 && long_texts >= long_text_threshold && !is_heading_heavy_list {
        return PageType::Article;
    }

    // SearchResults — must come before List, since search result pages have many links.
    // Multiple signals: title/heading keywords, URL query params, search input presence.
    let has_visible_search_input = dom.els.iter().any(|e| {
        e.hidden != Some(true) && is_search_input(e)
    });
    let has_any_search_input = dom.els.iter().any(|e| is_search_input(e));
    let search_results_keywords = &["search results", "results for", "search:", "found"];
    let has_search_results_context = title_has(search_results_keywords)
        || heading_has(search_results_keywords)
        || title_has(&["search"]);
    // URL-based signal: ?q=, ?query=, ?s=, ?search=, or /search path
    let url_lower = dom.url.to_lowercase();
    let has_search_url = url_lower.contains("?q=") || url_lower.contains("&q=")
        || url_lower.contains("?query=") || url_lower.contains("&query=")
        || url_lower.contains("?s=") || url_lower.contains("&s=")
        || url_lower.contains("?search=") || url_lower.contains("&search=")
        || url_lower.contains("/search?") || url_lower.contains("/search/");
    if has_any_search_input && link_count >= 8
        && (has_search_results_context || has_search_url)
    {
        return PageType::SearchResults;
    }

    // List (before Search — many list pages have search bars in nav)
    if link_count >= 10 {
        return PageType::List;
    }

    // Search — a search input without enough links to be a results page or list
    if has_visible_search_input {
        return PageType::Search;
    }

    // Form — count visible data-entry inputs. Allow checkbox/radio-only forms if a submit is present.
    let input_count = dom.els.iter().filter(|e| {
        e.hidden != Some(true) && match e.tag.as_str() {
            "textarea" | "select" => true,
            "input" => !matches!(
                e.input_type.as_deref(),
                Some("hidden") | Some("submit") | Some("button") | Some("image")
            ),
            _ => false,
        }
    }).count();
    let checkable_count = dom.els.iter().filter(|e| {
        e.hidden != Some(true)
            && e.tag == "input"
            && matches!(e.input_type.as_deref(), Some("checkbox") | Some("radio"))
    }).count();
    let has_submit = dom.els.iter().any(|e| {
        e.hidden != Some(true)
            && (e.tag == "button"
                || (e.tag == "input" && e.input_type.as_deref() == Some("submit")))
    });
    if input_count >= 2 || (checkable_count >= 2 && has_submit) {
        return PageType::Form;
    }

    // Search (hidden fallback) — JS-rendered search engines (e.g. DuckDuckGo) hide the search
    // input without JS. Detect these when the page has very few visible elements.
    if visible_count < 5 {
        let has_hidden_search = dom.els.iter().any(|e| {
            e.hidden == Some(true) && is_search_input(e)
        });
        if has_hidden_search {
            return PageType::Search;
        }
    }

    PageType::Other
}

// --- Suggested action detection ---

fn detect_suggested_actions(dom: &SpatialDom) -> Vec<SuggestedAction> {
    let mut actions = Vec::new();

    if let Some(a) = detect_blocked_action(dom) {
        actions.push(a);
    }

    // Register vs Login: prefer Register when registration context is present.
    // Both detect password fields, but Register also checks for confirm-password or
    // registration keywords in title/heading.
    if let Some(a) = detect_register_action(dom) {
        actions.push(a);
    } else if let Some(a) = detect_login_action(dom) {
        actions.push(a);
    }
    if let Some(a) = detect_enter_code_action(dom) {
        actions.push(a);
    }
    if let Some(a) = detect_consent_action(dom) {
        actions.push(a);
    }
    if let Some(a) = detect_contact_action(dom) {
        actions.push(a);
    }
    if let Some(a) = detect_search_action(dom) {
        actions.push(a);
    }
    if let Some(a) = detect_select_from_list_action(dom) {
        actions.push(a);
    }
    if let Some(a) = detect_cookie_consent_action(dom) {
        actions.push(a);
    }
    if let Some(a) = detect_paginate_action(dom) {
        actions.push(a);
    }
    if let Some(a) = detect_fill_form_action(dom, &actions) {
        actions.push(a);
    }
    if let Some(a) = detect_download_action(dom) {
        actions.push(a);
    }
    if let Some(a) = detect_captcha_challenge_action(dom) {
        actions.push(a);
    }

    actions
}

fn detect_blocked_action(dom: &SpatialDom) -> Option<SuggestedAction> {
    let info = dom.blocked.as_ref()?;
    Some(SuggestedAction::RetryGuidance {
        reason: info.reason.clone(),
        recommendations: info.recommendations.clone(),
        require_human: info.require_human,
    })
}

fn detect_login_action(dom: &SpatialDom) -> Option<SuggestedAction> {
    let password = dom.els.iter().find(|e| {
        e.hidden != Some(true) && e.input_type.as_deref() == Some("password")
    })?;
    let password_id = password.id;
    let password_y = password.b[1];

    // Find nearest text/email input within 500px Y distance
    let username = dom.els.iter()
        .filter(|e| e.hidden != Some(true) && e.tag == "input")
        .filter(|e| {
            let t = e.input_type.as_deref().unwrap_or("text");
            t == "text" || t == "email"
        })
        .filter(|e| (e.b[1] - password_y).abs() < 500)
        .min_by_key(|e| (e.b[1] - password_y).abs())?;
    let username_id = username.id;

    let submit_id = find_nearest_submit_button(dom, password_id)?;

    // Optional: find "remember me" checkbox
    let remember_me_id = dom.els.iter()
        .filter(|e| e.hidden != Some(true))
        .filter(|e| e.input_type.as_deref() == Some("checkbox"))
        .find(|e| {
            let label = e.label.as_deref().unwrap_or("").to_lowercase();
            let name = e.name.as_deref().unwrap_or("").to_lowercase();
            label.contains("remember") || name.contains("remember")
        })
        .map(|e| e.id);

    Some(SuggestedAction::Login {
        username_id,
        password_id,
        submit_id,
        remember_me_id,
    })
}

fn detect_enter_code_action(dom: &SpatialDom) -> Option<SuggestedAction> {
    let verification_keywords = [
        "verification", "verify your", "enter code", "security code", "verification code",
        "2fa", "two-factor", "two factor", "otp", "one-time", "passcode",
    ];

    let title_lower = dom.title.to_lowercase();
    let has_keyword_in_title = verification_keywords.iter().any(|kw| title_lower.contains(kw));

    let has_keyword_in_heading = dom.els.iter().any(|e| {
        e.role.as_deref() == Some("heading")
            && e.text.as_ref().map(|t| {
                let lower = t.to_lowercase();
                verification_keywords.iter().any(|kw| lower.contains(kw))
            }).unwrap_or(false)
    });

    if !has_keyword_in_title && !has_keyword_in_heading {
        return None;
    }

    // Don't emit EnterCode if there's a password field (that's Login)
    if dom.els.iter().any(|e| e.hidden != Some(true) && e.input_type.as_deref() == Some("password")) {
        return None;
    }

    let code_keywords = ["code", "otp", "pin", "verify"];

    // Find code-like inputs by name/label/placeholder
    let code_inputs: Vec<&SpatialElement> = dom.els.iter()
        .filter(|e| e.hidden != Some(true) && e.tag == "input")
        .filter(|e| {
            let t = e.input_type.as_deref().unwrap_or("text");
            t == "text" || t == "number" || t == "tel"
        })
        .filter(|e| {
            let name = e.name.as_deref().unwrap_or("").to_lowercase();
            let label = e.label.as_deref().unwrap_or("").to_lowercase();
            let ph = e.ph.as_deref().unwrap_or("").to_lowercase();
            code_keywords.iter().any(|kw| name.contains(kw) || label.contains(kw) || ph.contains(kw))
        })
        .collect();

    // Check for separate digit inputs (width < 60px, count 4-8)
    let narrow_inputs: Vec<&SpatialElement> = dom.els.iter()
        .filter(|e| e.hidden != Some(true) && e.tag == "input")
        .filter(|e| {
            let t = e.input_type.as_deref().unwrap_or("text");
            (t == "text" || t == "number" || t == "tel") && e.b[2] < 60
        })
        .collect();

    let (input_id, code_length);

    if narrow_inputs.len() >= 4 && narrow_inputs.len() <= 8 {
        code_length = Some(narrow_inputs.len());
        input_id = narrow_inputs[0].id;
    } else if !code_inputs.is_empty() {
        input_id = code_inputs[0].id;
        code_length = None;
    } else {
        // Fallback: any visible text/number/tel input on a verification page
        let any_input = dom.els.iter()
            .filter(|e| e.hidden != Some(true) && e.tag == "input")
            .find(|e| {
                let t = e.input_type.as_deref().unwrap_or("text");
                t == "text" || t == "number" || t == "tel"
            })?;
        input_id = any_input.id;
        code_length = None;
    }

    let submit_id = find_nearest_submit_button(dom, input_id)?;

    Some(SuggestedAction::EnterCode {
        input_id,
        submit_id,
        code_length,
    })
}

fn detect_search_action(dom: &SpatialDom) -> Option<SuggestedAction> {
    // Prefer visible search inputs, but fall back to hidden ones (JS-rendered search engines)
    let search_input = dom.els.iter()
        .filter(|e| e.hidden != Some(true))
        .find(|e| is_search_input(e))
        .or_else(|| dom.els.iter().find(|e| is_search_input(e)))?;

    // Try visible submit buttons first, then fall back to any (including hidden)
    let submit_id = find_nearest_submit_button(dom, search_input.id)
        .or_else(|| find_nearest_button(dom, search_input.id));

    Some(SuggestedAction::Search {
        input_id: search_input.id,
        submit_id: submit_id?,
    })
}

fn detect_consent_action(dom: &SpatialDom) -> Option<SuggestedAction> {
    let oauth_keywords = ["authorize", "allow access", "grant permission", "oauth", "consent"];

    let title_lower = dom.title.to_lowercase();
    let has_keyword_in_title = oauth_keywords.iter().any(|kw| title_lower.contains(kw));

    let has_keyword_in_heading = dom.els.iter().any(|e| {
        matches!(e.tag.as_str(), "h1" | "h2")
            && e.text.as_ref().map(|t| {
                let lower = t.to_lowercase();
                oauth_keywords.iter().any(|kw| lower.contains(kw))
            }).unwrap_or(false)
    });

    if !has_keyword_in_title && !has_keyword_in_heading {
        return None;
    }

    let approve_words = ["allow", "authorize", "accept", "approve", "grant"];
    let deny_words = ["deny", "cancel", "decline", "reject"];

    let find_button_ids = |words: &[&str]| -> Vec<u32> {
        dom.els.iter()
            .filter(|e| e.hidden != Some(true))
            .filter(|e| e.tag == "button" || e.role.as_deref() == Some("button"))
            .filter(|e| {
                e.text.as_ref().map(|t| {
                    let lower = t.to_lowercase();
                    words.iter().any(|w| lower.contains(w))
                }).unwrap_or(false)
            })
            .map(|e| e.id)
            .collect()
    };

    let approve_ids = find_button_ids(&approve_words);
    let deny_ids = find_button_ids(&deny_words);

    if approve_ids.is_empty() && deny_ids.is_empty() {
        return None;
    }

    Some(SuggestedAction::Consent { approve_ids, deny_ids })
}

fn detect_select_from_list_action(dom: &SpatialDom) -> Option<SuggestedAction> {
    let mut links: Vec<&SpatialElement> = dom.els.iter()
        .filter(|e| e.hidden != Some(true))
        .filter(|e| e.tag == "a" && e.href.is_some())
        .collect();

    if links.len() < 5 {
        return None;
    }

    links.sort_by_key(|e| e.b[1]);

    // Group into rows (within 30px Y = same row)
    let mut rows: Vec<Vec<u32>> = Vec::new();
    let mut current_row: Vec<u32> = vec![links[0].id];
    let mut current_y = links[0].b[1];

    for link in &links[1..] {
        if (link.b[1] - current_y).abs() <= 30 {
            current_row.push(link.id);
        } else {
            rows.push(current_row);
            current_row = vec![link.id];
            current_y = link.b[1];
        }
    }
    rows.push(current_row);

    if rows.len() < 5 {
        return None;
    }

    let items: Vec<u32> = rows.iter().map(|row| row[0]).collect();
    Some(SuggestedAction::SelectFromList { items })
}

/// Find the nearest visible submit button to an input element.
/// Prefers buttons below the input; scores by Manhattan distance (Y weighted 2x).
pub(crate) fn find_nearest_submit_button(dom: &SpatialDom, input_id: u32) -> Option<u32> {
    find_nearest_button_impl(dom, input_id, true)
}

/// Find the nearest button (including hidden) to an input element.
/// Used as fallback when no visible submit button exists (e.g. JS-rendered search engines).
fn find_nearest_button(dom: &SpatialDom, input_id: u32) -> Option<u32> {
    find_nearest_button_impl(dom, input_id, false)
}

fn detect_cookie_consent_action(dom: &SpatialDom) -> Option<SuggestedAction> {
    // Find elements containing cookie-related text (the banner/notice itself)
    let cookie_keywords = ["cookie", "cookies", "gdpr", "privacy notice", "consent to"];
    let cookie_elements: Vec<&SpatialElement> = dom.els.iter()
        .filter(|e| {
            e.text.as_ref().map(|t| {
                // Must be a substantial text block mentioning cookies, not just a word
                let lower = t.to_lowercase();
                t.len() > 30 && cookie_keywords.iter().any(|kw| lower.contains(kw))
            }).unwrap_or(false)
        })
        .collect();

    if cookie_elements.is_empty() {
        return None;
    }

    // Cookie-specific accept/reject buttons (more specific than generic "accept")
    let accept_words = [
        "accept all", "accept cookies", "allow cookies", "allow all", "agree",
        "got it", "i understand", "i agree",
    ];
    let reject_words = ["reject all", "reject cookies", "decline all", "refuse"];

    // Only look at buttons (not links — "accept" as a link is usually not a cookie button)
    let buttons: Vec<&SpatialElement> = dom.els.iter()
        .filter(|e| e.tag == "button" || e.role.as_deref() == Some("button"))
        .collect();

    let find_button_with_words = |words: &[&str]| -> Option<u32> {
        buttons.iter().find(|e| {
            e.text.as_ref().map(|t| {
                let lower = t.to_lowercase().trim().to_string();
                words.iter().any(|w| lower == *w || lower.contains(w))
            }).unwrap_or(false)
        }).map(|e| e.id)
    };

    let accept_id = find_button_with_words(&accept_words)?;
    let reject_id = find_button_with_words(&reject_words);

    Some(SuggestedAction::CookieConsent { accept_id, reject_id })
}

fn detect_paginate_action(dom: &SpatialDom) -> Option<SuggestedAction> {
    let pagination = dom.pagination()?;

    // Find element IDs for next/prev links
    let find_link_id = |url: &str| -> Option<u32> {
        dom.els.iter().find(|e| {
            e.href.as_deref() == Some(url)
        }).map(|e| e.id)
    };

    let next_id = pagination.next.as_ref().and_then(|url| find_link_id(url));
    let prev_id = pagination.prev.as_ref().and_then(|url| find_link_id(url));

    if next_id.is_none() && prev_id.is_none() {
        return None;
    }

    Some(SuggestedAction::Paginate { next_id, prev_id })
}

/// Detect a registration form: password + confirm password or email/name fields,
/// but NOT already a login page (login has its own action).
fn detect_register_action(dom: &SpatialDom) -> Option<SuggestedAction> {
    // Must have a visible password field
    let password = dom.els.iter().find(|e| {
        e.hidden != Some(true) && e.input_type.as_deref() == Some("password")
    })?;
    let password_id = password.id;

    // Must NOT be a login page — registration has either a confirm password field,
    // or registration-related keywords in heading/title, or "register"/"sign up" in submit button
    let all_passwords: Vec<&SpatialElement> = dom.els.iter().filter(|e| {
        e.hidden != Some(true) && e.input_type.as_deref() == Some("password")
    }).collect();

    let title_lower = dom.title.to_lowercase();
    let register_keywords = ["register", "sign up", "signup", "create account", "join", "new account"];
    let login_keywords = ["login", "log in", "sign in", "signin"];

    // If title or any visible text says "login"/"sign in", this is at least partially a
    // login page. Pages like HN have both login and registration — Login takes priority.
    let has_login_title = login_keywords.iter().any(|kw| title_lower.contains(kw));
    // Check headings and bold text for login keywords (catches HN's <b>Login</b>)
    let heading_or_bold_tags = ["h1", "h2", "h3", "h4", "h5", "h6", "b", "strong"];
    let has_login_heading_or_bold = dom.els.iter().any(|e| {
        e.hidden != Some(true)
            && (e.role.as_deref() == Some("heading") || heading_or_bold_tags.contains(&e.tag.as_str()))
            && e.text.as_ref().map(|t| {
                let lower = t.to_lowercase();
                login_keywords.iter().any(|kw| lower == *kw || lower.contains(kw))
            }).unwrap_or(false)
    });

    let has_confirm_password = all_passwords.len() >= 2;

    // When a page has both login and registration sections (2+ password fields + login text),
    // prefer Login. The Login detector will pick up the first form.
    if (has_login_title || has_login_heading_or_bold) && has_confirm_password {
        return None;
    }
    let has_register_title = register_keywords.iter().any(|kw| title_lower.contains(kw));
    let has_register_heading = dom.els.iter().any(|e| {
        e.role.as_deref() == Some("heading")
            && e.text.as_ref().map(|t| {
                let lower = t.to_lowercase();
                register_keywords.iter().any(|kw| lower.contains(kw))
            }).unwrap_or(false)
    });

    if !has_confirm_password && !has_register_title && !has_register_heading {
        return None;
    }

    let email_id = find_email_input_id(dom);

    // Find username field (text input with name suggesting username)
    let username_id = dom.els.iter().find(|e| {
        e.hidden != Some(true) && e.tag == "input"
            && matches!(e.input_type.as_deref(), Some("text") | None)
            && e.name.as_ref().map(|n| {
                let lower = n.to_lowercase();
                lower == "username" || lower == "user" || lower == "login"
            }).unwrap_or(false)
    }).map(|e| e.id);

    // Find name field
    let name_id = dom.els.iter().find(|e| {
        e.hidden != Some(true) && e.tag == "input"
            && matches!(e.input_type.as_deref(), Some("text") | None)
            && e.name.as_ref().map(|n| {
                let lower = n.to_lowercase();
                lower == "name" || lower == "fullname" || lower == "full_name" || lower == "display_name"
            }).unwrap_or(false)
    }).map(|e| e.id);

    // Confirm password is the second password field
    let confirm_password_id = if has_confirm_password {
        Some(all_passwords[1].id)
    } else {
        None
    };

    // Find submit button near the password field
    let submit_id = find_nearest_submit_button(dom, password_id)?;

    Some(SuggestedAction::Register {
        email_id,
        username_id,
        password_id,
        confirm_password_id,
        name_id,
        submit_id,
    })
}

/// Detect a contact form: email + message textarea.
fn detect_contact_action(dom: &SpatialDom) -> Option<SuggestedAction> {
    // Must have a visible textarea (message body)
    let textarea = dom.els.iter().find(|e| {
        e.hidden != Some(true) && e.tag == "textarea"
    })?;
    let message_id = textarea.id;

    // Must have contact-related context (title, heading, or form action)
    let title_lower = dom.title.to_lowercase();
    let contact_keywords = ["contact us", "contact form", "get in touch", "reach out", "send us a message", "inquiry"];
    let has_contact_context = contact_keywords.iter().any(|kw| title_lower.contains(kw))
        || dom.els.iter().any(|e| {
            e.role.as_deref() == Some("heading")
                && e.text.as_ref().map(|t| {
                    let lower = t.to_lowercase();
                    contact_keywords.iter().any(|kw| lower.contains(kw))
                }).unwrap_or(false)
        });

    if !has_contact_context {
        return None;
    }

    let email_id = find_email_input_id(dom);

    // Find name field
    let name_id = dom.els.iter().find(|e| {
        e.hidden != Some(true) && e.tag == "input"
            && matches!(e.input_type.as_deref(), Some("text") | None)
            && (e.name.as_ref().map(|n| {
                let lower = n.to_lowercase();
                lower == "name" || lower == "fullname" || lower == "full_name"
            }).unwrap_or(false)
            || e.label.as_ref().map(|l| {
                let lower = l.to_lowercase();
                lower.contains("name") && !lower.contains("email") && !lower.contains("user")
            }).unwrap_or(false))
    }).map(|e| e.id);

    let submit_id = find_nearest_submit_button(dom, message_id)?;

    Some(SuggestedAction::Contact {
        name_id,
        email_id,
        message_id,
        submit_id,
    })
}

/// Detect a generic form and extract labeled fields for the FillForm action.
/// Only fires for pages already classified as Form (2+ data-entry inputs) that
/// don't match more specific form actions (Login, Register, Contact).
fn detect_fill_form_action(dom: &SpatialDom, existing_actions: &[SuggestedAction]) -> Option<SuggestedAction> {
    // Only for Form/Error page types — don't generate FillForm on Login/Search pages
    if dom.page_type != PageType::Form && dom.page_type != PageType::Error {
        return None;
    }

    // Already have a more specific action?
    let has_specific = existing_actions.iter().any(|a| {
        matches!(a, SuggestedAction::Login { .. }
            | SuggestedAction::Register { .. }
            | SuggestedAction::Contact { .. }
            | SuggestedAction::Search { .. }
        )
    });
    if has_specific {
        return None;
    }

    // Collect visible data-entry fields
    let fields: Vec<FormField> = dom.els.iter().filter(|e| {
        e.hidden != Some(true) && match e.tag.as_str() {
            "textarea" | "select" => true,
            "input" => !matches!(
                e.input_type.as_deref(),
                Some("hidden") | Some("submit") | Some("button") | Some("image")
            ),
            _ => false,
        }
    }).map(|e| FormField {
        id: e.id,
        label: e.label.clone().or_else(|| e.ph.clone()),
        name: e.name.clone(),
        input_type: e.input_type.clone(),
    }).collect();

    if fields.len() < 2 {
        return None;
    }

    // Find a submit button near the last field
    let last_field_id = fields.last().unwrap().id;
    let submit_id = find_nearest_submit_button(dom, last_field_id)?;

    Some(SuggestedAction::FillForm { fields, submit_id })
}

/// Detect download links/buttons on the page.
/// Looks for links with download-related text or file extension hrefs.
fn detect_download_action(dom: &SpatialDom) -> Option<SuggestedAction> {
    let file_extensions = [
        ".zip", ".tar.gz", ".dmg", ".exe", ".msi", ".deb", ".rpm",
        ".pkg", ".appimage", ".pdf", ".csv", ".xlsx",
    ];

    let mut items = Vec::new();

    for el in &dom.els {
        if el.hidden == Some(true) { continue; }

        let is_link_or_button = el.tag == "a" || el.tag == "button"
            || el.role.as_deref() == Some("link")
            || el.role.as_deref() == Some("button");
        if !is_link_or_button { continue; }

        // Text match: "download" must be the primary action — starts with "download"
        // or is a short button text like "Download" / "Download now"
        let text_match = el.text.as_ref().map(|t| {
            let lower = t.to_lowercase().trim().to_string();
            lower.starts_with("download") && (lower.len() < 40 || lower.contains('.'))
        }).unwrap_or(false);

        let href_match = el.href.as_ref().map(|h| {
            let lower = h.to_lowercase();
            file_extensions.iter().any(|ext| lower.ends_with(ext))
        }).unwrap_or(false);

        if text_match || href_match {
            items.push(DownloadItem {
                id: el.id,
                text: el.text.clone(),
                href: el.href.clone(),
            });
        }
    }

    if items.is_empty() {
        return None;
    }

    Some(SuggestedAction::Download { items })
}

/// Detect a CaptchaChallenge action when CAPTCHA info is present or when the
/// page type is Captcha with image-grid patterns (custom CAPTCHAs without a
/// known service like reCAPTCHA/hCaptcha).
fn detect_captcha_challenge_action(dom: &SpatialDom) -> Option<SuggestedAction> {
    // Determine CAPTCHA type and sitekey: either from tree-detected service or
    // by inferring from SpatialDom elements on a Captcha page.
    let (ct, sk) = if let Some(captcha) = dom.captcha.as_ref() {
        (captcha.captcha_type.clone(), captcha.sitekey.clone())
    } else if dom.page_type == PageType::Captcha {
        // No known CAPTCHA service detected in the HTML tree.
        // Check for image grid pattern: multiple image buttons (≥4) suggest a
        // "select all images containing X" challenge.
        let image_buttons = dom.els.iter().filter(|e| {
            e.hidden != Some(true)
                && e.tag == "button"
                && e.input_type.as_deref() != Some("submit")
                && e.text.as_ref().map(|t| {
                    let lower = t.to_lowercase();
                    lower.contains("image") || lower.contains("img")
                }).unwrap_or(false)
        }).count();

        if image_buttons >= 4 {
            (CaptchaType::ImageGrid, None)
        } else {
            // Generic unknown CAPTCHA — still useful to surface
            (CaptchaType::Unknown, None)
        }
    } else {
        return None;
    };

    // Find the submit button: prefer one with action text, fall back to any visible button
    let visible_buttons: Vec<&SpatialElement> = dom.els.iter().filter(|e| {
        e.hidden != Some(true)
            && (e.tag == "button" || (e.tag == "input" && e.input_type.as_deref() == Some("submit")))
    }).collect();
    let submit_id = visible_buttons.iter()
        .find(|e| {
            e.text.as_ref().map(|t| {
                let lower = t.to_lowercase();
                lower.contains("verify") || lower.contains("submit") || lower.contains("continue")
                    || lower.contains("proceed")
            }).unwrap_or(false)
        })
        .or_else(|| visible_buttons.first())
        .map(|e| e.id);

    Some(SuggestedAction::CaptchaChallenge {
        captcha_type: ct,
        sitekey: sk,
        submit_id,
    })
}

// --- CAPTCHA detection from layout tree ---

/// Scan the layout tree for CAPTCHA service indicators.
/// Checks `<script src>`, `<iframe src>`, `<div class>`, `data-sitekey`, and
/// `<noscript>` content for reCAPTCHA, hCaptcha, Turnstile, and Cloudflare signals.
fn detect_captcha_from_tree(root: &LayoutNode) -> Option<CaptchaInfo> {
    let mut captcha_type: Option<CaptchaType> = None;
    let mut sitekey: Option<String> = None;

    scan_captcha_recursive(root, &mut captcha_type, &mut sitekey);

    captcha_type.map(|ct| CaptchaInfo {
        captcha_type: ct,
        sitekey,
    })
}

fn scan_captcha_recursive(
    node: &LayoutNode,
    captcha_type: &mut Option<CaptchaType>,
    sitekey: &mut Option<String>,
) {
    let tag = node.tag.as_str();

    // Check script/iframe src for CAPTCHA service URLs
    if matches!(tag, "script" | "iframe") {
        if let Some(src) = node.attributes.get("src") {
            let src_lower = src.to_lowercase();
            if src_lower.contains("recaptcha") || src_lower.contains("google.com/recaptcha") {
                *captcha_type = Some(CaptchaType::ReCaptcha);
            } else if src_lower.contains("hcaptcha.com") || src_lower.contains("newassets.hcaptcha.com") {
                *captcha_type = Some(CaptchaType::HCaptcha);
            } else if src_lower.contains("challenges.cloudflare.com/turnstile") {
                *captcha_type = Some(CaptchaType::Turnstile);
            }
        }
    }

    // Check div class for CAPTCHA containers and Cloudflare challenge IDs
    if tag == "div" {
        if let Some(class) = node.attributes.get("class") {
            let class_lower = class.to_lowercase();
            if class_lower.contains("g-recaptcha") {
                *captcha_type = Some(CaptchaType::ReCaptcha);
            } else if class_lower.contains("h-captcha") {
                *captcha_type = Some(CaptchaType::HCaptcha);
            } else if class_lower.contains("cf-turnstile") {
                *captcha_type = Some(CaptchaType::Turnstile);
            }
        }
        if let Some(id) = node.attributes.get("id") {
            let id_lower = id.to_lowercase();
            if (id_lower.contains("challenge-running") || id_lower.contains("cf-challenge"))
                && captcha_type.is_none()
            {
                *captcha_type = Some(CaptchaType::CloudflareChallenge);
            }
        }
    }

    // Check for data-sitekey attribute (all major CAPTCHA services use this)
    if let Some(key) = node.attributes.get("data-sitekey") {
        if !key.is_empty() {
            *sitekey = Some(key.clone());
        }
    }

    for child in &node.children {
        scan_captcha_recursive(child, captcha_type, sitekey);
    }
}

fn find_nearest_button_impl(dom: &SpatialDom, input_id: u32, visible_only: bool) -> Option<u32> {
    let input = dom.get(input_id)?;
    let input_y = input.b[1];
    let input_x = input.b[0];

    let mut best: Option<(u32, i32)> = None;

    for el in &dom.els {
        if visible_only && el.hidden == Some(true) { continue; }
        let is_button = el.tag == "button"
            || (el.tag == "input" && el.input_type.as_deref() == Some("submit"));
        if !is_button { continue; }

        let dy = el.b[1] - input_y;
        let dx = (el.b[0] - input_x).abs();
        // Heavy penalty for buttons above the input
        let score = if dy < 0 { dy.abs() * 4 + dx } else { dy * 2 + dx };

        if best.is_none() || score < best.unwrap().1 {
            best = Some((el.id, score));
        }
    }

    best.map(|(id, _)| id)
}