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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
#!/usr/bin/env python3

# SMOLAGENTS 1.19 FIX - Must be imported before anything else
from final_fix import apply_final_fix
from browser_agent_fix import validate_listing_url_for_nyc

# NEW: Import fixed address extraction (prioritizes mapaddress and structured data)
from fixed_address_extraction import apply_fixed_extraction

# Apply all fixes at startup
apply_final_fix()
apply_fixed_extraction()

import gradio as gr
import json
import pandas as pd
import re
from datetime import datetime, timezone
from typing import Dict, List, Any, Optional
from agent_setup import initialize_caseworker_agent
from tools import final_answer
import ast

# Import our new utilities and constants
from utils import log_tool_action, current_timestamp, parse_observation_data
from constants import StageEvent, RiskLevel, Borough, VoucherType
from browser_agent import BrowserAgent
from violation_checker_agent import ViolationCheckerAgent

# Import V0's enhanced email handling
from email_handler import EmailTemplateHandler, enhanced_classify_message, enhanced_handle_email_request

# Import shortlist utilities
from shortlist_utils import (
    add_to_shortlist, remove_from_shortlist, get_shortlist, 
    is_shortlisted, get_shortlist_summary, get_shortlisted_ids
)

# --- Internationalization Setup ---
i18n_dict = {
    "en": {
        "app_title": "🏠 NYC Voucher Housing Navigator",
        "app_subtitle": "Your personal AI Caseworker for finding voucher-friendly housing with building safety insights.",
        "language_selector": "Language / Idioma / 语言 / ভাষা",
        "conversation_label": "Conversation with VoucherBot",
        "message_label": "Your Message",
        "message_placeholder": "Start by telling me your voucher type, required bedrooms, and max rent...",
        "preferences_title": "🎛️ Search Preferences",
        "strict_mode_label": "Strict Mode (Only show buildings with 0 violations)",
        "borough_label": "Preferred Borough",
        "max_rent_label": "Maximum Rent",
        "listings_label": "Matching Listings",
        "status_label": "Status",
        "status_ready": "Ready to search...",
        "no_listings": "I don't have any listings to show you right now. Please search for apartments first!",
        "no_listings_title": "📋 No Current Listings",
        "invalid_listing": "I only have {count} listings available. Please ask for a listing between 1 and {count}.",
        "invalid_listing_title": "❌ Invalid Listing Number",
        "showing_listings": "Showing {count} listings",
        "strict_applied": "🔒 Strict mode applied: {count} listings with 0 violations",
        "strict_applied_title": "🔒 Filtering Applied",
        "results_found": "✅ Found {count} voucher-friendly listings with safety information!",
        "results_title": "✅ Results Ready",
        "no_safe_listings": "No listings meet your safety criteria. Try disabling strict mode to see all available options.",
        "no_safe_title": "⚠️ No Safe Listings",
        "search_error": "❌ Search error: {error}",
        "search_error_title": "❌ Search Error",
        "error_occurred": "I apologize, but I encountered an error: {error}",
        "error_title": "❌ Error",
        "general_response_title": "💬 General Response",
        "conversation_mode": "Conversation mode",
        "no_criteria": "No listings meet criteria",
        "what_if_analysis": "What-if analysis",
        "what_if_error_title": "❌ What-If Error",
        "error_what_if": "I encountered an error processing your what-if scenario: {error}",
        "error_listings_available": "Error - {count} listings available",
        "error_what_if_processing": "Error in what-if processing",
        "error_conversation": "Error in conversation",
        "col_address": "Address",
        "col_price": "Price",
        "col_risk_level": "Risk Level", 
        "col_violations": "Violations",
        "col_last_inspection": "Last Inspection",
        "col_link": "Link",
        "col_summary": "Summary",
        "col_shortlist": "Shortlist",
        "link_not_available": "No link available",
        "shortlist_save": "➕",
        "shortlist_saved": "✅",
        "shortlist_empty": "Your shortlist is empty. Save some listings to get started!",
        "shortlist_title": "Your Shortlist",
        "shortlist_added": "Added to shortlist",
        "shortlist_removed": "Removed from shortlist",
        "shortlist_cleared": "Shortlist cleared",
        "intro_greeting": """👋 **Hi there! I'm Navi, your personal NYC Housing Navigator!**

I'm here to help you find safe, affordable, and voucher-friendly housing in New York City. I understand that finding the right home can feel overwhelming, but you don't have to do this alone—I'm here to guide you every step of the way, and answer any questions you have about housing vouchers or the process! 😊

**To get started, just tell me:**
• What type of voucher do you have? (Section 8, CityFHEPS, HASA, etc.)
• How many bedrooms do you need? 🛏️
• What's your maximum rent budget? 💰
• Do you have a preferred borough? 🗽"""
    },
    "es": {
        "app_title": "🏠 Navegador de Vivienda con Voucher de NYC",
        "app_subtitle": "Tu trabajador social personal de IA para encontrar vivienda que acepta vouchers con información de seguridad del edificio.",
        "language_selector": "Idioma / Language / 语言 / ভাষা",
        "conversation_label": "Conversación con VoucherBot",
        "message_label": "Tu Mensaje",
        "message_placeholder": "Comienza diciéndome tu tipo de voucher, habitaciones requeridas y renta máxima...",
        "preferences_title": "🎛️ Preferencias de Búsqueda",
        "strict_mode_label": "Modo Estricto (Solo mostrar edificios con 0 violaciones)",
        "borough_label": "Distrito Preferido",
        "max_rent_label": "Renta Máxima",
        "listings_label": "Listados Coincidentes",
        "status_label": "Estado",
        "status_ready": "Listo para buscar...",
        "no_listings": "No tengo listados para mostrarte ahora. ¡Por favor busca apartamentos primero!",
        "no_listings_title": "📋 Sin Listados Actuales",
        "invalid_listing": "Solo tengo {count} listados disponibles. Por favor pide un listado entre 1 y {count}.",
        "invalid_listing_title": "❌ Número de Listado Inválido",
        "showing_listings": "Mostrando {count} listados",
        "strict_applied": "🔒 Modo estricto aplicado: {count} listados con 0 violaciones",
        "strict_applied_title": "🔒 Filtro Aplicado",
        "results_found": "✅ ¡Encontrado {count} listados que aceptan vouchers con información de seguridad!",
        "results_title": "✅ Resultados Listos",
        "no_safe_listings": "Ningún listado cumple tus criterios de seguridad. Intenta desactivar el modo estricto para ver todas las opciones disponibles.",
        "no_safe_title": "⚠️ Sin Listados Seguros",
        "search_error": "❌ Error de búsqueda: {error}",
        "search_error_title": "❌ Error de Búsqueda",
        "error_occurred": "Me disculpo, pero encontré un error: {error}",
        "error_title": "❌ Error",
        "general_response_title": "💬 Respuesta General",
        "conversation_mode": "Modo conversación",
        "no_criteria": "Ningún listado cumple criterios",
        "what_if_analysis": "Análisis de qué pasaría si",
        "what_if_error_title": "❌ Error de Qué Pasaría Si",
        "error_what_if": "Encontré un error procesando tu escenario de qué pasaría si: {error}",
        "error_listings_available": "Error - {count} listados disponibles",
        "error_what_if_processing": "Error en procesamiento de qué pasaría si",
        "error_conversation": "Error en conversación",
        "col_address": "Dirección",
        "col_price": "Precio",
        "col_risk_level": "Nivel de Riesgo",
        "col_violations": "Violaciones",
        "col_last_inspection": "Última Inspección",
        "col_link": "Enlace",
        "col_summary": "Resumen",
        "col_shortlist": "Lista Favorita",
        "link_not_available": "Sin enlace disponible",
        "shortlist_save": "➕",
        "shortlist_saved": "✅",
        "shortlist_empty": "Tu lista favorita está vacía. ¡Guarda algunos listados para comenzar!",
        "shortlist_title": "Tu Lista Favorita",
        "shortlist_added": "Agregado a lista favorita",
        "shortlist_removed": "Removido de lista favorita",
        "shortlist_cleared": "Lista favorita limpiada",
        "intro_greeting": """👋 **¡Hola! Soy Navi, tu Navegadora Personal de Vivienda de NYC!**

Estoy aquí para ayudarte a encontrar vivienda segura, asequible y que acepta vouchers en la Ciudad de Nueva York. Entiendo que encontrar el hogar perfecto puede sentirse abrumador, pero no tienes que hacerlo solo—¡estoy aquí para guiarte en cada paso del camino y responder cualquier pregunta que tengas sobre vouchers de vivienda o el proceso! 😊

**Para comenzar, solo dime:**
• ¿Qué tipo de voucher tienes? (Section 8, CityFHEPS, HASA, etc.)
• ¿Cuántas habitaciones necesitas? 🛏️
• ¿Cuál es tu presupuesto máximo de renta? 💰
• ¿Tienes un distrito preferido? 🗽"""
    },
    "zh": {
        "app_title": "🏠 纽约市住房券导航器",
        "app_subtitle": "您的个人AI社工,帮助您找到接受住房券的房屋,并提供建筑安全信息。",
        "language_selector": "语言 / Language / Idioma / ভাষা",
        "conversation_label": "与VoucherBot对话",
        "message_label": "您的消息",
        "message_placeholder": "请先告诉我您的住房券类型、所需卧室数量和最高租金...",
        "preferences_title": "🎛️ 搜索偏好",
        "strict_mode_label": "严格模式(仅显示0违规的建筑)",
        "borough_label": "首选区域",
        "max_rent_label": "最高租金",
        "listings_label": "匹配房源",
        "status_label": "状态",
        "status_ready": "准备搜索...",
        "no_listings": "我现在没有房源可以显示给您。请先搜索公寓!",
        "no_listings_title": "📋 当前无房源",
        "invalid_listing": "我只有{count}个可用房源。请询问1到{count}之间的房源。",
        "invalid_listing_title": "❌ 无效房源号码",
        "showing_listings": "显示{count}个房源",
        "strict_applied": "🔒 严格模式已应用:{count}个0违规房源",
        "strict_applied_title": "🔒 已应用过滤",
        "results_found": "✅ 找到{count}个接受住房券的房源,包含安全信息!",
        "results_title": "✅ 结果准备就绪",
        "no_safe_listings": "没有房源符合您的安全标准。尝试禁用严格模式以查看所有可用选项。",
        "no_safe_title": "⚠️ 无安全房源",
        "search_error": "❌ 搜索错误:{error}",
        "search_error_title": "❌ 搜索错误",
        "error_occurred": "抱歉,我遇到了一个错误:{error}",
        "error_title": "❌ 错误",
        "general_response_title": "💬 一般回复",
        "conversation_mode": "对话模式",
        "no_criteria": "没有房源符合条件",
        "what_if_analysis": "假设分析",
        "what_if_error_title": "❌ 假设错误",
        "error_what_if": "处理您的假设场景时遇到错误:{error}",
        "error_listings_available": "错误 - {count}个房源可用",
        "error_what_if_processing": "假设处理错误",
        "error_conversation": "对话错误",
        "col_address": "地址",
        "col_price": "价格",
        "col_risk_level": "风险级别",
        "col_violations": "违规",
        "col_last_inspection": "最后检查",
        "col_link": "链接",
        "col_summary": "摘要",
        "col_shortlist": "收藏清单",
        "link_not_available": "无可用链接",
        "shortlist_save": "➕",
        "shortlist_saved": "✅",
        "shortlist_empty": "您的收藏清单为空。保存一些房源开始吧!",
        "shortlist_title": "您的收藏清单",
        "shortlist_added": "已添加到收藏清单",
        "shortlist_removed": "已从收藏清单移除",
        "shortlist_cleared": "收藏清单已清空",
        "intro_greeting": """👋 **您好!我是Navi,您的个人纽约市住房导航员!**

我在这里帮助您在纽约市找到安全、经济实惠且接受住房券的住房。我理解找到合适的家可能让人感到不知所措,但您不必独自面对这一切—我会在每一步中指导您,并回答您关于住房券或申请流程的任何问题!😊

**开始使用时,请告诉我:**
• 您有什么类型的住房券?(Section 8、CityFHEPS、HASA等)
• 您需要多少间卧室?🛏️
• 您的最高租金预算是多少?💰
• 您有首选的行政区吗?🗽"""
    },
    "bn": {
        "app_title": "🏠 NYC ভাউচার হাউজিং নেভিগেটর",
        "app_subtitle": "ভাউচার-বান্ধব আবাসন খোঁজার জন্য আপনার ব্যক্তিগত AI কেসওয়ার্কার, বিল্ডিং নিরাপত্তা তথ্যসহ।",
        "language_selector": "ভাষা / Language / Idioma / 语言",
        "conversation_label": "VoucherBot এর সাথে কথোপকথন",
        "message_label": "আপনার বার্তা",
        "message_placeholder": "আপনার ভাউচারের ধরন, প্রয়োজনীয় বেডরুম এবং সর্বোচ্চ ভাড়া বলে শুরু করুন...",
        "preferences_title": "🎛️ অনুসন্ধান পছন্দ",
        "strict_mode_label": "কঠোর মোড (শুধুমাত্র ০ লঙ্ঘনের বিল্ডিং দেখান)",
        "borough_label": "পছন্দের বরো",
        "max_rent_label": "সর্বোচ্চ ভাড়া",
        "listings_label": "মিলে যাওয়া তালিকা",
        "status_label": "অবস্থা",
        "status_ready": "অনুসন্ধানের জন্য প্রস্তুত...",
        "no_listings": "এই মুহূর্তে আপনাকে দেখানোর মতো কোন তালিকা নেই। প্রথমে অ্যাপার্টমেন্ট অনুসন্ধান করুন!",
        "no_listings_title": "📋 বর্তমান তালিকা নেই",
        "invalid_listing": "আমার কাছে শুধুমাত্র {count}টি তালিকা উপলব্ধ। অনুগ্রহ করে ১ থেকে {count} এর মধ্যে একটি তালিকা চান।",
        "invalid_listing_title": "❌ অবৈধ তালিকা নম্বর",
        "showing_listings": "{count}টি তালিকা দেখাচ্ছে",
        "strict_applied": "🔒 কঠোর মোড প্রয়োগ করা হয়েছে: ০ লঙ্ঘনের {count}টি তালিকা",
        "strict_applied_title": "🔒 ফিল্টার প্রয়োগ করা হয়েছে",
        "results_found": "✅ নিরাপত্তা তথ্যসহ {count}টি ভাউচার-বান্ধব তালিকা পাওয়া গেছে!",
        "results_title": "✅ ফলাফল প্রস্তুত",
        "no_safe_listings": "কোন তালিকা আপনার নিরাপত্তা মানদণ্ড পূরণ করে না। সমস্ত উপলব্ধ বিকল্প দেখতে কঠোর মোড নিষ্ক্রিয় করার চেষ্টা করুন।",
        "no_safe_title": "⚠️ কোন নিরাপদ তালিকা নেই",
        "search_error": "❌ অনুসন্ধান ত্রুটি: {error}",
        "search_error_title": "❌ অনুসন্ধান ত্রুটি",
        "error_occurred": "আমি দুঃখিত, কিন্তু আমি একটি ত্রুটির সম্মুখীন হয়েছি: {error}",
        "error_title": "❌ ত্রুটি",
        "general_response_title": "💬 সাধারণ উত্তর",
        "conversation_mode": "কথোপকথন মোড",
        "no_criteria": "কোন তালিকা মানদণ্ড পূরণ করে না",
        "what_if_analysis": "যদি-তাহলে বিশ্লেষণ",
        "what_if_error_title": "❌ যদি-তাহলে ত্রুটি",
        "error_what_if": "আপনার যদি-তাহলে পরিস্থিতি প্রক্রিয়া করতে আমি ত্রুটির সম্মুখীন হয়েছি: {error}",
        "error_listings_available": "ত্রুটি - {count}টি তালিকা উপলব্ধ",
        "error_what_if_processing": "যদি-তাহলে প্রক্রিয়াকরণে ত্রুটি",
        "error_conversation": "কথোপকথনে ত্রুটি",
        "col_address": "ঠিকানা",
        "col_price": "দাম",
        "col_risk_level": "ঝুঁকির স্তর",
        "col_violations": "লঙ্ঘন",
        "col_last_inspection": "শেষ পরিদর্শন",
        "col_link": "লিংক",
        "col_summary": "সারাংশ",
        "col_shortlist": "পছন্দের তালিকা",
        "link_not_available": "কোন লিংক উপলব্ধ নেই",
        "shortlist_save": "➕",
        "shortlist_saved": "✅",
        "shortlist_empty": "আপনার পছন্দের তালিকা খালি। শুরু করতে কিছু তালিকা সংরক্ষণ করুন!",
        "shortlist_title": "আপনার পছন্দের তালিকা",
        "shortlist_added": "পছন্দের তালিকায় যোগ করা হয়েছে",
        "shortlist_removed": "পছন্দের তালিকা থেকে সরানো হয়েছে",
        "shortlist_cleared": "পছন্দের তালিকা পরিষ্কার করা হয়েছে",
        "intro_greeting": """👋 **নমস্কার! আমি নবি, আপনার ব্যক্তিগত NYC হাউজিং নেভিগেটর!**

আমি এখানে আছি নিউইয়র্ক সিটিতে আপনাকে নিরাপদ, সাশ্রয়ী এবং ভাউচার-বান্ধব আবাসন খুঁজে পেতে সাহায্য করার জন্য। আমি বুঝি যে সঠিক বাড়ি খোঁজা অভিভূতকর মনে হতে পারে, কিন্তু আপনাকে একা এটি করতে হবে না—আমি প্রতিটি পদক্ষেপে আপনাকে গাইড করার জন্য এখানে আছি, এবং হাউজিং ভাউচার বা প্রক্রিয়া সম্পর্কে আপনার যেকোনো প্রশ্নের উত্তর দিতে পারি! 😊

**শুরু করতে, শুধু আমাকে বলুন:**
• আপনার কি ধরনের ভাউচার আছে? (Section 8, CityFHEPS, HASA, ইত্যাদি)
• আপনার কতটি বেডরুম প্রয়োজন? 🛏️
• আপনার সর্বোচ্চ ভাড়ার বাজেট কত? 💰
• আপনার কি কোন পছন্দের বরো আছে? 🗽"""
    }
}

# Create the I18n instance with keyword arguments for each language
i18n = gr.I18n(
    en=i18n_dict["en"],
    es=i18n_dict["es"],
    zh=i18n_dict["zh"],
    bn=i18n_dict["bn"]
)

# --- Initialize Agents and State Management ---
print("Initializing VoucherBot Agents...")
caseworker_agent = initialize_caseworker_agent()
browser_agent = BrowserAgent()
violation_agent = ViolationCheckerAgent()
print("Agents Initialized. Ready for requests.")

# --- State Management Functions ---
def create_initial_state() -> Dict:
    """Create initial app state."""
    return {
        "listings": [],
        "current_listing": None,  # Track the currently discussed listing
        "current_listing_index": None,  # Track the index of the current listing
        "preferences": {
            "borough": "",
            "max_rent": 4000,
            "min_bedrooms": 1,
            "voucher_type": "",
            "strict_mode": False,
            "language": "en"  # Add language to preferences
        },
        "shortlist": []  # Changed from favorites to shortlist
    }

def update_app_state(current_state: Dict, updates: Dict) -> Dict:
    """Update app state with new data."""
    new_state = current_state.copy()
    for key, value in updates.items():
        if key == "preferences" and isinstance(value, dict):
            new_state["preferences"].update(value)
        else:
            new_state[key] = value
    return new_state

def filter_listings_strict_mode(listings: List[Dict], strict: bool = False) -> List[Dict]:
    """Filter listings based on strict mode (no violations)."""
    if not strict:
        return listings
    
    return [
        listing for listing in listings 
        if listing.get("building_violations", 0) == 0
    ]

def create_chat_message_with_metadata(content: str, title: str, 
                                    duration: Optional[float] = None,
                                    parent_id: Optional[str] = None) -> Dict:
    """Create a ChatMessage with metadata for better UX."""
    metadata = {
        "title": title,
        "timestamp": current_timestamp()
    }
    
    if duration is not None:
        metadata["duration"] = duration
    
    if parent_id is not None:
        metadata["parent_id"] = parent_id
    
    return {
        "role": "assistant",
        "content": content,
        "metadata": metadata
    }

def detect_context_dependent_question(message: str) -> bool:
    """Detect if the message is asking about something in the current context (like 'which lines?')"""
    message_lower = message.lower().strip()
    
    # Short questions that likely refer to current context
    context_patterns = [
        r'^which\s+(lines?|train|subway)',  # "which lines", "which line", "which train"
        r'^what\s+(lines?|train|subway)',   # "what lines", "what line", "what train"
        r'^how\s+(far|close|near)',         # "how far", "how close", "how near"
        r'^(lines?|train|subway)$',         # just "lines", "line", "train", "subway"
        r'^what\s+about',                   # "what about..."
        r'^tell\s+me\s+about',             # "tell me about..."
        r'^more\s+(info|details)',         # "more info", "more details"
        r'^(distance|walk|walking)',       # "distance", "walk", "walking"
        r'^any\s+other',                   # "any other..."
        r'^is\s+it\s+(near|close|far)',    # "is it near", "is it close", "is it far"
        # Add patterns for subway and school proximity questions
        r'nearest\s+(subway|train|school|transit)', # "nearest subway", "nearest school", "nearest train", "nearest transit"
        r'closest\s+(subway|train|school|transit)', # "closest subway", "closest school", "closest train", "closest transit"
        r'what\'?s\s+the\s+(nearest|closest)\s+(subway|train|school|transit)', # "what's the nearest/closest subway"
        r'where\s+is\s+the\s+(nearest|closest)\s+(subway|train|school|transit)', # "where is the nearest/closest subway"
        r'how\s+far\s+is\s+the\s+(subway|train|school|transit)', # "how far is the subway"
        r'(subway|train|school|transit)\s+(distance|proximity)', # "subway distance", "school proximity"
        r'^(subway|train|school|transit)\?$',      # just "subway?", "school?", "transit?"
        r'^closest\s+(subway|train|school|transit)\?$', # "closest subway?", "closest school?", "closest transit?"
        # Add broader patterns for context questions with "can i see", "show me" etc.
        r'can\s+i\s+see\s+.*?(nearest|closest|subway|train|school)', # "can i see the nearest subway"
        r'show\s+me\s+.*?(nearest|closest|subway|train|school)', # "show me the nearest subway"
        r'tell\s+me\s+.*?(nearest|closest|subway|train|school)', # "tell me the nearest subway"
        r'what.*?(nearest|closest)\s+(subway|train|school)', # "what's the nearest subway"
        r'where.*?(nearest|closest)\s+(subway|train|school)', # "where is the nearest subway"
    ]
    
    # Check if message matches context-dependent patterns using search instead of match
    import re
    for pattern in context_patterns:
        if re.search(pattern, message_lower):
            return True
    
    # Also check for very short questions (likely context-dependent)
    words = message_lower.split()
    if len(words) <= 3 and any(word in ['which', 'what', 'how', 'where', 'lines', 'train', 'subway'] for word in words):
        return True
    
    return False

def detect_language_from_message(message: str) -> str:
    """Detect language from user message using simple keyword matching."""
    message_lower = message.lower()
    
    # Spanish keywords - removed English terms and borough names
    spanish_keywords = [
        'hola', 'apartamento', 'vivienda', 'casa', 'alquiler', 'renta', 'busco', 
        'necesito', 'ayuda', 'donde', 'como', 'que', 'soy', 'tengo', 'quiero',
        'habitacion', 'habitaciones', 'dormitorio', 'precio', 'costo', 'dinero',
        'gracias', 'por favor', 'dime', 'dame', 'encuentro', 'cuanto',
        'cuantas', 'puedo', 'puedes', 'buscar', 'encontrar'
    ]
    
    # Chinese keywords (simplified)
    chinese_keywords = [
        '你好', '公寓', '住房', '房屋', '租金', '寻找', '需要', '帮助', '在哪里',
        '怎么', '什么', '我', '有', '要', '房间', '卧室', '价格', '钱',
        '住房券', '布朗克斯', '布鲁克林', '曼哈顿', '皇后区', '谢谢', '请',
        '告诉', '给我', '找到'
    ]
    
    # Bengali keywords
    bengali_keywords = [
        'নমস্কার', 'অ্যাপার্টমেন্ট', 'বাড়ি', 'ভাড়া', 'খুঁজছি', 'প্রয়োজন',
        'সাহায্য', 'কোথায়', 'কিভাবে', 'কি', 'আমি', 'আছে', 'চাই',
        'রুম', 'বেডরুম', 'দাম', 'টাকা', 'ভাউচার', 'ব্রঙ্কস', 'ব্রুকলিন',
        'ম্যানহাটান', 'কুইন্স', 'ধন্যবাদ', 'দয়া করে', 'বলুন', 'দিন', 'খুঁজে'
    ]
    
    # Count matches for each language
    spanish_count = sum(1 for keyword in spanish_keywords if keyword in message_lower)
    chinese_count = sum(1 for keyword in chinese_keywords if keyword in message)
    bengali_count = sum(1 for keyword in bengali_keywords if keyword in message)
    
    # Return language with highest count (minimum 2 matches required)
    if spanish_count >= 3:  # Increased threshold for Spanish
        return "es"
    elif chinese_count >= 2:
        return "zh"
    elif bengali_count >= 2:
        return "bn"
    else:
        return "en"  # Default to English

# Define the theme using Origin
theme = gr.themes.Origin(
    primary_hue="indigo",
    secondary_hue="indigo",
    neutral_hue="teal",
)

# --- Gradio UI Definition ---
# Original CSS (for easy revert):
# .app-header { text-align: center; margin-bottom: 2rem; }
# .app-title { font-size: 2.2rem; margin-bottom: 0.5rem; }
# .app-subtitle { font-size: 1.1rem; color: #666; margin-bottom: 1rem; }
# .dark .app-title { color: #f9fafb !important; }
# .dark .app-subtitle { color: #d1d5db !important; }
# .dark .gradio-container { background-color: #1f2937 !important; }
# .dark { background-color: #111827 !important; }

with gr.Blocks(theme=theme, css="""
    /* Material Design-inspired styles - Two-Column Layout */
    body, .gr-root {
        font-family: 'Roboto', 'Helvetica Neue', Arial, sans-serif;
        color: #222;
        background: #f5f5f7;
    }
    
    /* Style the expand/collapse arrow */
    button.svelte-vzs2gq.padded {
        background: transparent !important;
        border: none !important;
        padding: 4px !important;
        cursor: pointer !important;
        width: 24px !important;
        height: 24px !important;
        display: inline-flex !important;
        align-items: center !important;
        justify-content: center !important;
    }
    
    .dropdown-arrow {
        width: 18px !important;
        height: 18px !important;
        display: block !important;
    }
    
    /* Hide only the circle background */
    .dropdown-arrow .circle {
        fill: transparent !important;
        stroke: none !important;
    }
    
    /* Style the arrow path */
    .dropdown-arrow path {
        fill: #666 !important;
        transform-origin: center !important;
    }
    
    /* Header spanning both columns */
    .app-header {
        text-align: center;
        margin-bottom: 2rem;
        padding: 1.5rem;
        background: linear-gradient(135deg, #00695c 0%, #004d40 100%);
        border-radius: 12px;
        color: white;
        box-shadow: 0 4px 16px rgba(0,105,92,0.15);
    }
    .app-title {
        font-size: 2.5rem;
        margin-bottom: 0.5rem;
        font-weight: 700;
        color: white;
        letter-spacing: 0.5px;
        text-shadow: 0 2px 8px rgba(0,0,0,0.1);
    }
    .app-subtitle {
        font-size: 1.2rem;
        color: rgba(255,255,255,0.9);
        margin-bottom: 0;
        font-weight: 400;
    }
    
    /* Header controls */
    .header-controls {
        position: absolute;
        top: 1rem;
        right: 1rem;
        display: flex;
        gap: 0.5rem;
    }
    .header-controls button {
        background: rgba(255,255,255,0.2);
        border: 1px solid rgba(255,255,255,0.3);
        color: white;
        padding: 0.5rem 1rem;
        border-radius: 6px;
        font-size: 0.9rem;
    }
    .header-controls button:hover {
        background: rgba(255,255,255,0.3);
    }
    
    /* Two-column layout */
    .main-layout {
        display: flex;
        gap: 2rem;
        min-height: 70vh;
    }
    .chat-column {
        flex: 1;
        max-width: 50%;
        display: flex;
        flex-direction: column;
    }
    .info-column {
        flex: 1;
        max-width: 50%;
        display: flex;
        flex-direction: column;
    }
    
    /* Onboarding/Help Section */
    .onboarding-box {
        background: #fff;
        border-radius: 12px;
        padding: 1.5rem;
        margin-bottom: 1rem;
        box-shadow: 0 4px 16px rgba(0,105,92,0.08);
        border-left: 4px solid #00695c;
    }
    .onboarding-title {
        font-size: 1.1rem;
        font-weight: 600;
        color: #00695c;
        margin-bottom: 0.5rem;
    }
    .onboarding-text {
        color: #666;
        line-height: 1.5;
        margin-bottom: 1rem;
    }
    
    /* Suggested Prompts */
    .suggested-prompts {
        margin-bottom: 1rem;
    }
    .prompt-chips {
        display: flex;
        flex-wrap: wrap;
        gap: 0.5rem;
        margin-bottom: 1rem;
    }
    .prompt-chip {
        background: #e8eaf6;
        color: #6200ea;
        border: 1px solid #6200ea;
        border-radius: 20px;
        padding: 0.5rem 1rem;
        font-size: 0.9rem;
        cursor: pointer;
        transition: all 0.2s;
    }
    .prompt-chip:hover {
        background: #6200ea;
        color: white;
        transform: translateY(-1px);
        box-shadow: 0 2px 8px rgba(98,0,234,0.2);
    }
    
    /* Chat area styling */
    .gr-chatbot {
        flex: 1;
        margin-bottom: 1rem;
        border-radius: 12px;
        box-shadow: 0 4px 16px rgba(0,105,92,0.08);
        position: relative;
    }
    
    /* Simple fix for green blocks - just target the specific elements causing issues */
    .gr-chatbot .prose::marker,
    .gr-chatbot .prose li::marker {
        color: inherit !important;
    }
    
    /* Remove any custom background colors from markers */
    .gr-chatbot .prose li::before {
        background: none !important;
    }
    
    /* Ensure expandable sections use arrows */
    .gr-chatbot details > summary {
        list-style: revert !important;
        cursor: pointer;
    }
    
    .gr-chatbot details > summary::marker,
    .gr-chatbot details > summary::-webkit-details-marker {
        color: #666 !important;
    }
    
    /* Remove any Material Design overrides for expandable sections */
    .gr-chatbot details,
    .gr-chatbot summary {
        background: transparent !important;
    }
    
    /* Make trash/delete button smaller and positioned correctly */
    .gr-chatbot button[aria-label*="Delete"], 
    .gr-chatbot button[aria-label*="Clear"], 
    .gr-chatbot .gr-button[title*="Delete"], 
    .gr-chatbot .gr-button[title*="Clear"] {
        width: 28px !important;
        height: 28px !important;
        min-width: 28px !important;
        min-height: 28px !important;
        padding: 4px !important;
        font-size: 0.75rem !important;
        position: absolute !important;
        top: 8px !important;
        right: 8px !important;
        z-index: 10 !important;
        border-radius: 50% !important;
        background: rgba(0,105,92,0.8) !important;
    }
    
    .gr-chatbot button[aria-label*="Delete"]:hover, 
    .gr-chatbot button[aria-label*="Clear"]:hover, 
    .gr-chatbot .gr-button[title*="Delete"]:hover, 
    .gr-chatbot .gr-button[title*="Clear"]:hover {
        background: rgba(0,77,64,0.9) !important;
        transform: scale(1.05) !important;
    }
    
    /* Input area */
    .chat-input-area {
        background: #fff;
        border-radius: 12px;
        padding: 1rem;
        box-shadow: 0 4px 16px rgba(0,105,92,0.08);
        margin-bottom: 1rem;
    }
    
    /* Toggles section */
    .toggles-section {
        background: #fff;
        border-radius: 12px;
        padding: 1rem;
        box-shadow: 0 4px 16px rgba(0,105,92,0.08);
    }
    .toggle-title {
        font-weight: 600;
        color: #333;
        margin-bottom: 0.5rem;
    }
    
    /* Right column - Info panel */
    .results-header {
        background: #fff;
        border-radius: 12px;
        padding: 1rem;
        margin-bottom: 1rem;
        box-shadow: 0 4px 16px rgba(0,105,92,0.08);
        text-align: center;
        font-weight: 600;
        color: #00695c;
    }
    .results-dataframe {
        flex: 1;
        background: #fff;
        border-radius: 12px;
        padding: 1rem;
        box-shadow: 0 4px 16px rgba(0,105,92,0.08);
        margin-bottom: 1rem;
    }
    .status-panel {
        background: #fff;
        border-radius: 12px;
        padding: 1rem;
        box-shadow: 0 4px 16px rgba(0,105,92,0.08);
    }

    /* Buttons - Enhanced Material Design */
    button, .gr-button {
        background: #00695c;
        color: #fff;
        border-radius: 6px;
        box-shadow: 0 4px 12px rgba(0,105,92,0.15);
        font-weight: 600;
        font-size: 1rem;
        padding: 0.75em 1.5em;
        min-height: 44px;
        position: relative;
        overflow: hidden;
        transition: all 0.2s;
        border: none;
    }
    button:hover, .gr-button:hover {
        background: #004d40;
        box-shadow: 0 6px 20px rgba(0,105,92,0.2);
        transform: translateY(-1px);
    }
    button:active, .gr-button:active {
        transform: translateY(0);
    }

    /* Inputs - Enhanced styling */
    input, textarea, .gr-textbox input, .gr-textbox textarea {
        border: 2px solid #e0e0e0;
        border-radius: 8px;
        padding: 12px 16px;
        font-size: 1rem;
        background: #fff;
        transition: all 0.2s;
        color: #222;
    }
    input:focus, textarea:focus, .gr-textbox input:focus, .gr-textbox textarea:focus {
        border-color: #00695c;
        box-shadow: 0 0 0 3px rgba(0,105,92,0.1);
        outline: none;
    }
    /* Fix input font color in dark mode */
    .dark input, .dark textarea, .dark .gr-textbox input, .dark .gr-textbox textarea {
        color: #f3f4f6 !important;
        background: #222 !important;
        border-color: #444 !important;
    }
    
    /* DataFrame styling */
    .gr-dataframe {
        border-radius: 8px;
        box-shadow: 0 2px 8px rgba(0,0,0,0.05);
        width: 100% !important;
        max-width: 100% !important;
        overflow-x: auto !important;
        position: relative !important;
    }
    
    /* Prevent layout shift on header click */
    .gr-dataframe table {
        width: 100% !important;
        min-width: 100% !important;
        table-layout: fixed !important;
    }
    
    /* Header button styling */
    .gr-dataframe .header-button {
        width: 100% !important;
        text-align: left !important;
        white-space: nowrap !important;
        overflow: hidden !important;
        text-overflow: ellipsis !important;
    }
    
    /* Ensure container maintains scroll */
    .results-dataframe {
        overflow-x: auto !important;
        max-width: 100% !important;
        position: relative !important;
    }

    /* Make DataFrame cells non-editable */
    .gr-dataframe td {
        pointer-events: auto !important;
        user-select: none !important;
        -webkit-user-select: none !important;
    }

    /* Style link cells specifically */
    .gr-dataframe td:nth-child(7) {  /* 7th column is Link */
        cursor: pointer !important;
    }

    /* Style and prevent editing of input elements in DataFrame */
    .gr-dataframe input.svelte-1y3tas2 {
        pointer-events: none !important;
        user-select: none !important;
        -webkit-user-select: none !important;
        -webkit-user-modify: read-only !important;
        -moz-user-modify: read-only !important;
        user-modify: read-only !important;
        background: transparent !important;
        border: none !important;
        padding: 0 !important;
        margin: 0 !important;
        font-family: inherit !important;
        font-size: inherit !important;
        color: inherit !important;
        cursor: pointer !important;
    }

    /* Style links in DataFrame */
    .gr-dataframe a {
        color: #2196F3 !important;
        text-decoration: underline !important;
        cursor: pointer !important;
        pointer-events: auto !important;
    }

    .gr-dataframe a:hover {
        color: #1976D2 !important;
        text-decoration: underline !important;
    }

    /* Allow interaction only with shortlist column inputs */
    .gr-dataframe td:last-child input.svelte-1y3tas2 {
        pointer-events: none !important;
        user-select: none !important;
        -webkit-user-select: none !important;
        text-align: center !important;
        cursor: pointer !important;
    }

    /* Remove focus styles from inputs */
    .gr-dataframe input.svelte-1y3tas2:focus {
        outline: none !important;
        box-shadow: none !important;
        background: transparent !important;
    }

    /* Allow interaction only with shortlist column */
    .gr-dataframe td:last-child {
        pointer-events: auto !important;
        cursor: pointer !important;
        user-select: none !important;
        -webkit-user-select: none !important;
        -webkit-user-modify: read-only !important;
        -moz-user-modify: read-only !important;
        user-modify: read-only !important;
        font-family: system-ui, -apple-system, sans-serif !important;
        font-size: 16px !important;
        line-height: 1 !important;
        text-align: center !important;
        color: #666 !important;
        background: transparent !important;
        transition: all 0.2s ease !important;
    }

    /* Style shortlist column hover state */
    .gr-dataframe td:last-child:hover {
        color: #00695c !important;
        background: rgba(0,105,92,0.1) !important;
        border-radius: 4px;
    }

    /* Dark mode adaptations */
    .dark .gr-dataframe td:last-child {
        color: #d1d5db !important;
    }

    .dark .gr-dataframe td:last-child:hover {
        color: #9ca3af !important;
        background: rgba(156,163,175,0.1) !important;
    }

    /* Prevent cell editing */
    .gr-dataframe td[contenteditable="true"] {
        -webkit-user-modify: read-only !important;
        -moz-user-modify: read-only !important;
        user-modify: read-only !important;
    }

    /* Fix for teal blocks around interactive elements */
    .gr-dataframe button,
    .gr-dataframe .gr-button,
    .gr-dataframe .svelte-vzs2gq,
    .cell-menu-button.svelte-vt38nd {
        background: transparent !important;
        border: none !important;
        box-shadow: none !important;
    }

    /* Hide cell menu buttons */
    .cell-menu-button.svelte-vt38nd,
    button[aria-label="Open cell menu"],
    .cell-menu-button {
        display: none !important;
        visibility: hidden !important;
        opacity: 0 !important;
        pointer-events: none !important;
        position: absolute !important;
        width: 0 !important;
        height: 0 !important;
        overflow: hidden !important;
    }

    /* Hide selection buttons */
    .selection-button.svelte-1mp8yw1,
    button.selection-button,
    .selection-button-row,
    .selection-button-column {
        display: none !important;
        visibility: hidden !important;
        opacity: 0 !important;
        pointer-events: none !important;
        position: absolute !important;
        width: 0 !important;
        height: 0 !important;
        overflow: hidden !important;
    }

    /* Style the expand/collapse arrow */
    button.svelte-vzs2gq.padded {
        background: transparent !important;
        border: none !important;
        padding: 4px !important;
        cursor: pointer !important;
        width: 24px !important;
        height: 24px !important;
        display: inline-flex !important;
        align-items: center !important;
        justify-content: center !important;
    }

    /* Remove any default button styling from DataFrame cells */
    .gr-dataframe td:last-child {
        background: transparent !important;
        cursor: pointer;
    }

    .gr-dataframe td:last-child:hover {
        background: rgba(0,105,92,0.1) !important;
        border-radius: 4px;
    }
    
    /* Responsive design */
    @media (max-width: 768px) {
        .main-layout {
            flex-direction: column;
        }
        .chat-column, .info-column {
            max-width: 100%;
        }
        .header-controls {
            position: relative;
            margin-top: 1rem;
        }
        .prompt-chips {
            flex-direction: column;
        }
    }

    /* Dark mode button - Compact styling */
    .dark-mode-btn {
        width: 36px !important;
        height: 36px !important;
        min-width: 36px !important;
        min-height: 36px !important;
        padding: 6px !important;
        font-size: 1rem !important;
        border-radius: 50% !important;
        background: rgba(0,105,92,0.1) !important;
        border: 1px solid rgba(0,105,92,0.3) !important;
        color: #00695c !important;
        box-shadow: 0 2px 6px rgba(0,105,92,0.1) !important;
        transition: all 0.2s ease !important;
    }
    .dark-mode-btn:hover {
        background: rgba(0,105,92,0.2) !important;
        transform: scale(1.05) !important;
        box-shadow: 0 3px 8px rgba(0,105,92,0.2) !important;
    }

    /* Dark mode adaptations */
    .dark {
        background-color: #111827 !important;
    }
    .dark .app-title { color: #f9fafb !important; }
    .dark .app-subtitle { color: #d1d5db !important; }
    .dark .gradio-container { background-color: #1f2937 !important; }
    .dark .onboarding-box, .dark .chat-input-area, .dark .toggles-section,
    .dark .results-header, .dark .results-dataframe, .dark .status-panel {
        background: #374151 !important;
        color: #f3f4f6 !important;
    }
    .dark .dark-mode-btn {
        background: rgba(255,255,255,0.1) !important;
        border: 1px solid rgba(255,255,255,0.2) !important;
        color: #f3f4f6 !important;
    }
    .dark .dark-mode-btn:hover {
        background: rgba(255,255,255,0.2) !important;
    }
    
    /* Make chatbot response font one size bigger */
    .gr-chatbot .prose, .gr-chatbot .prose * {
        font-size: 1.25rem !important;
        line-height: 1.7 !important;
    }
""") as demo:
    # Header Section
    with gr.Row():
        with gr.Column():
            gr.HTML("""
                <div class="app-header">
                    <h1 class="app-title">🏠 NYC Voucher Housing Navigator</h1>
                    <p class="app-subtitle">Find safe, voucher-friendly housing in NYC with AI assistance</p>
                </div>
            """)
    
    # Header controls row
    with gr.Row():
        with gr.Column(scale=8):
            pass  # Spacer
        with gr.Column(scale=1):
            dark_mode_toggle = gr.Button("🌙", size="sm", elem_classes=["dark-mode-btn"])
    
    # Initialize app state
    app_state = gr.State(create_initial_state())
    
    # Create initial greeting message for Navi
    def create_initial_greeting(language="en"):
        greeting_message = {
            "role": "assistant",
            "content": i18n_dict[language]["intro_greeting"]
        }
        return [greeting_message]
    
    # Main two-column layout
    with gr.Row(elem_classes=["main-layout"]):
        # LEFT COLUMN: Chat Panel
        with gr.Column(elem_classes=["chat-column"]):
            # Language dropdown above chat
            language_dropdown = gr.Dropdown(
                label="Language / Idioma / 语言 / ভাষা",
                choices=[("English", "en"), ("Español", "es"), ("中文", "zh"), ("বাংলা", "bn")],
                value="en",
                container=True
            )
            
            # Chat Section
            chatbot = gr.Chatbot(
                label="💬 Conversation",
                height=400,
                type="messages",
                value=create_initial_greeting(),
                elem_classes=["gr-chatbot"],
                show_label=True,
                render_markdown=True
            )
            
            # Chat Input Area
            with gr.Column(elem_classes=["chat-input-area"]):
                msg = gr.Textbox(
                    label="Your Message",
                    placeholder="Type your request, like '2 bedroom in Queens under $2500'...",
                    lines=2,
                    container=False
                )
                send_btn = gr.Button("Send Message", variant="primary")
                
                # Placeholder state for strict mode (UI removed)
                strict_mode_toggle = gr.State(False)
            
        # RIGHT COLUMN: Aggregated Information Panel
        with gr.Column(elem_classes=["info-column"]):
            # Results Header/Status
            progress_info = gr.HTML(
                value='<div class="results-header">🏠 Ready to search for listings...</div>',
                elem_classes=["results-header"]
            )
            
            # DataFrame Section
            with gr.Column(elem_classes=["results-dataframe"]):
                            results_df = gr.DataFrame(
                value=pd.DataFrame(),
                label="📋 Found Listings",
                interactive=True,  # Make interactive for shortlist functionality
                row_count=(10, "dynamic"),
                wrap=True,
                visible=False,
                datatype=["number", "str", "str", "str", "number", "str", "html", "str", "str"],  # Changed Link column to html type
                column_widths=["50px", "300px", "100px", "100px", "100px", "120px", "100px", "200px", "80px"]  # Set fixed column widths
                )
            
            # Shortlist Panel
            with gr.Column(elem_classes=["status-panel"]):
                def create_initial_shortlist_display():
                    return """
                    <div style="text-align: center; color: #666;">
                        <h4>📌 Your Shortlist (0 saved)</h4>
                        <p>Click ➕ in the listings table to save properties to your shortlist.<br/>
                        Use chat commands like "show my shortlist" to manage saved listings.</p>
                        <hr style="margin: 1rem 0; border: 1px solid #eee;">
                        <div style="color: #999; font-style: italic;">No saved listings yet</div>
                    </div>
                    """
                
                shortlist_display = gr.HTML(
                    value=create_initial_shortlist_display(),
                    elem_id="shortlist-display"
                )

    # Add all the handler functions before wiring up events
    def update_shortlist_display(state: Dict) -> str:
        """Create HTML for the shortlist display panel."""
        shortlist = get_shortlist(state)
        count = len(shortlist)
        
        if count == 0:
            return """
            <div style="text-align: center; color: #666;">
                <h4>📌 Your Shortlist (0 saved)</h4>
                <p>Click ➕ in the listings table to save properties to your shortlist.<br/>
                Use chat commands like "show my shortlist" to manage saved listings.</p>
                <hr style="margin: 1rem 0; border: 1px solid #eee;">
                <div style="color: #999; font-style: italic;">No saved listings yet</div>
            </div>
            """
        
        # Create HTML for shortlist items
        items_html = ""
        for i, item in enumerate(shortlist[:5], 1):  # Show top 5
            priority_badge = ""
            if item.get("priority"):
                priority_badge = f'<span style="background: #ff9800; color: white; padding: 2px 6px; border-radius: 10px; font-size: 0.8em;">⭐ {item["priority"]}</span>'
            
            items_html += f"""
            <div style="margin: 0.5rem 0; padding: 0.5rem; background: #f9f9f9; border-radius: 6px; text-align: left;">
                <div style="font-weight: 600; font-size: 0.9em;">{item['address'][:40]}{'...' if len(item['address']) > 40 else ''}</div>
                <div style="color: #666; font-size: 0.8em;">{item['price']}{item['risk_level']}</div>
                {priority_badge}
            </div>
            """
        
        if count > 5:
            items_html += f'<div style="color: #999; font-style: italic; text-align: center;">... and {count - 5} more</div>'
        
        return f"""
        <div style="color: #666;">
            <h4 style="text-align: center;">📌 Your Shortlist ({count} saved)</h4>
            <p style="text-align: center; font-size: 0.9em;">Click ➕/✅ in the table or use chat commands</p>
            <hr style="margin: 1rem 0; border: 1px solid #eee;">
            {items_html}
        </div>
        """

    def handle_shortlist_click(evt: gr.SelectData, state: Dict):
        """Handle shortlist button clicks and link clicks in the DataFrame."""
        try:
            # Handle clicks on the Link column (index 6)
            if evt.index[1] == 6:  # Link column
                listings = state.get("listings", [])
                if listings and evt.index[0] < len(listings):
                    listing = listings[evt.index[0]]
                    url = listing.get("url", "")
                    if url and url != "No link available":
                        # The URL will be opened by the browser since we're using an HTML anchor tag
                        return gr.update(), gr.update(), gr.update(), state
                return gr.update(), gr.update(), gr.update(), state
            
            # Handle clicks on the Shortlist column (index 8)
            if evt.index[1] != 8:  # Shortlist column is index 8 (0-based)
                return gr.update(), gr.update(), gr.update(), state
            
            listings = state.get("listings", [])
            if not listings or evt.index[0] >= len(listings):
                return gr.update(), gr.update(), gr.update(), state
                
            listing = listings[evt.index[0]]
            
            # Toggle shortlist status
            if is_shortlisted(listing, state):
                # Remove from shortlist
                listing_id = str(listing.get("id", listing.get("address", "")))
                updated_state, message = remove_from_shortlist(listing_id, state)
            else:
                # Add to shortlist
                updated_state, message = add_to_shortlist(listing, state)
                
            # Update DataFrame display
            df = create_listings_dataframe(listings, updated_state)
            
            # Update progress info with shortlist count
            shortlist_count = len(updated_state.get('shortlist', []))
            status_text = f"Showing {len(listings)} listings ({shortlist_count} in shortlist)"
            
            # Update shortlist display
            shortlist_html = update_shortlist_display(updated_state)
            
            return gr.update(value=df), gr.update(value=status_text), gr.update(value=shortlist_html), updated_state
            
        except Exception as e:
            print(f"Error in handle_shortlist_click: {e}")
            return gr.update(), gr.update(), gr.update(), state

    def handle_shortlist_command(message: str, history: list, state: Dict):
        """Handle shortlist-related chat commands."""
        message_lower = message.lower()
        listings = state.get("listings", [])
        
        # Show shortlist command
        if "show shortlist" in message_lower or "view shortlist" in message_lower or "my shortlist" in message_lower:
            shortlist_summary = get_shortlist_summary(state)
            shortlist_msg = create_chat_message_with_metadata(
                shortlist_summary,
                "📋 Your Shortlist"
            )
            history.append(shortlist_msg)
            
            # Update DataFrame and shortlist display
            if listings:
                current_df = create_listings_dataframe(listings, state)
                shortlist_count = len(state.get("shortlist", []))
                status_text = f"Showing {len(listings)} listings ({shortlist_count} in shortlist)"
                return (history, gr.update(value=current_df, visible=True), 
                       gr.update(value=status_text), state)
            else:
                return (history, gr.update(), gr.update(value="Shortlist displayed"), state)
        
        # Save listing command (e.g., "save listing 2", "add listing 3 to shortlist")
        save_patterns = ["save listing", "add listing", "shortlist listing"]
        if any(pattern in message_lower for pattern in save_patterns):
            # Extract listing number
            import re
            numbers = re.findall(r'\d+', message_lower)
            if numbers and listings:
                try:
                    listing_index = int(numbers[0]) - 1  # Convert to 0-based index
                    if 0 <= listing_index < len(listings):
                        listing = listings[listing_index]
                        updated_state, status_message = add_to_shortlist(listing, state)
                        
                        success_msg = create_chat_message_with_metadata(
                            status_message,
                            "📌 Shortlist Updated"
                        )
                        history.append(success_msg)
                        
                        # Update DataFrame and shortlist display
                        current_df = create_listings_dataframe(listings, updated_state)
                        shortlist_count = len(updated_state.get("shortlist", []))
                        status_text = f"Showing {len(listings)} listings ({shortlist_count} in shortlist)"
                        return (history, gr.update(value=current_df, visible=True), 
                               gr.update(value=status_text), updated_state)
                    else:
                        error_msg = create_chat_message_with_metadata(
                            f"❌ Invalid listing number. Please specify a number between 1 and {len(listings)}.",
                            "❌ Error"
                        )
                        history.append(error_msg)
                except ValueError:
                    error_msg = create_chat_message_with_metadata(
                        "❌ Please specify a valid listing number (e.g., 'save listing 2').",
                        "❌ Error"
                    )
                    history.append(error_msg)
            else:
                if not listings:
                    error_msg = create_chat_message_with_metadata(
                        "❌ No listings available to save. Please search for apartments first.",
                        "❌ No Listings"
                    )
                else:
                    error_msg = create_chat_message_with_metadata(
                        "❌ Please specify which listing to save (e.g., 'save listing 2').",
                        "❌ Missing Number"
                    )
                history.append(error_msg)
        
        # Clear shortlist command
        elif "clear shortlist" in message_lower or "empty shortlist" in message_lower:
            from shortlist_utils import clear_shortlist
            updated_state, status_message = clear_shortlist(state)
            
            clear_msg = create_chat_message_with_metadata(
                status_message,
                "📋 Shortlist Cleared"
            )
            history.append(clear_msg)
            
            # Update DataFrame and shortlist display
            if listings:
                current_df = create_listings_dataframe(listings, updated_state)
                status_text = f"Showing {len(listings)} listings (shortlist cleared)"
                return (history, gr.update(value=current_df, visible=True), 
                       gr.update(value=status_text), updated_state)
            else:
                return (history, gr.update(), gr.update(value="Shortlist cleared"), updated_state)
        
        # Default: preserve current state
        if listings:
            current_df = create_listings_dataframe(listings, state)
            shortlist_count = len(state.get("shortlist", []))
            status_text = f"Showing {len(listings)} listings ({shortlist_count} in shortlist)"
            return (history, gr.update(value=current_df, visible=True), 
                   gr.update(value=status_text), state)
        else:
            return (history, gr.update(), gr.update(value="Shortlist command processed"), state)
    
    def handle_listing_question(message: str, history: list, state: Dict):
        """Handle questions about existing listings."""
        listings = state.get("listings", [])
        
        if not listings:
            no_listings_msg = create_chat_message_with_metadata(
                "I don't have any listings to show you yet. Please search for apartments first!",
                "📋 No Listings Available"
            )
            history.append(no_listings_msg)
            return (history, gr.update(), gr.update(value="No search criteria set"), state)
        
        message_lower = message.lower()
        
        # Parse which listing they're asking about
        listing_index = None
        
        # Number word mapping
        number_words = {
            'zero': 0, 'one': 1, 'two': 2, 'three': 3, 'four': 4, 'five': 5,
            'six': 6, 'seven': 7, 'eight': 8, 'nine': 9, 'ten': 10,
            'eleven': 11, 'twelve': 12, 'thirteen': 13, 'fourteen': 14, 'fifteen': 15,
            'sixteen': 16, 'seventeen': 17, 'eighteen': 18, 'nineteen': 19, 'twenty': 20
        }
        
        # Navigation patterns that depend on current state
        if "current_listing_index" in state and state["current_listing_index"] is not None:
            current_idx = state["current_listing_index"]
            if any(word in message_lower for word in ["next", "forward"]):
                listing_index = min(current_idx + 1, len(listings) - 1)
            elif any(word in message_lower for word in ["previous", "prev", "back"]):
                listing_index = max(current_idx - 1, 0)
        
        # If navigation wasn't used, try other patterns
        if listing_index is None:
            # Check for relative position patterns first
            relative_patterns = {
                r"(?:the\s+)?last(?:\s+listing)?": lambda: len(listings) - 1,
                r"(?:the\s+)?(?:second\s+to\s+last|next\s+to\s+last|penultimate|last\s+but\s+one)(?:\s+listing)?": lambda: len(listings) - 2 if len(listings) > 1 else 0,
                r"(?:the\s+)?(first|1st)(?:\s+listing)?": lambda: 0,
                r"(?:the\s+)?(second|2nd)(?:\s+listing)?": lambda: 1,
                r"(?:the\s+)?(third|3rd)(?:\s+listing)?": lambda: 2,
                r"(?:the\s+)?(fourth|4th)(?:\s+listing)?": lambda: 3,
                r"(?:the\s+)?(fifth|5th)(?:\s+listing)?": lambda: 4
            }
            
            for pattern, index_func in relative_patterns.items():
                if re.search(pattern, message_lower):
                    listing_index = index_func()
                    break
            
            # If no relative position found, try numeric numbers
            if listing_index is None:
                numbers = re.findall(r'\b(-?\d+(?:\.\d+)?)\b', message_lower)
                if numbers:
                    try:
                        # Convert to float first to handle decimals
                        num = float(numbers[0])
                        # Check if it's a whole number and non-negative
                        if num.is_integer() and num > 0:
                            listing_index = int(num) - 1
                    except:
                        pass
                
                # If no valid numeric number found, try word numbers
                if listing_index is None:
                    for word, number in number_words.items():
                        word_patterns = [
                            f"number {word}",
                            f"no. {word}",
                            f"#{word}",
                            f"listing {word}",
                            f" {word} ",  # Space-bounded word
                            f"^{word} ",  # Start of string
                            f" {word}$"   # End of string
                        ]
                        if any(pattern in f" {message_lower} " for pattern in word_patterns):
                            if number > 0:  # Only accept positive numbers
                                listing_index = number - 1
                            break
                    
                    # If still no match, try patterns
                    if listing_index is None:
                        # Check for various patterns of asking about a listing
                        listing_patterns = {
                            # Basic patterns with flexible spacing
                            r"(?:can\s+(?:i|you)\s+)?(?:see|show|tell\s+(?:me\s+)?about|what\s+about|view)\s+listing\s*#?\s*(-?\d+)": lambda x: int(x) - 1,
                            r"(?:what\s+is|what's|whats|how\s+about|details\s+for)\s+listing\s*#?\s*(-?\d+)": lambda x: int(x) - 1,
                            r"#\s*(-?\d+)": lambda x: int(x) - 1,
                            r"listing\s*#?\s*(-?\d+)": lambda x: int(x) - 1,
                            
                            # Informal/casual patterns
                            r"(?:hey|please|could you|i'd like to|let me)\s+(?:see|show)\s+listing\s*#?\s*(-?\d+)": lambda x: int(x) - 1,
                            r"(?:apartment|property|unit)\s*#?\s*(-?\d+)": lambda x: int(x) - 1,
                            r"number\s*(-?\d+)(?:\s+please)?": lambda x: int(x) - 1,
                            
                            # Alternative number formats
                            r"listing\s+(?:no\.?|number)\s+(-?\d+)": lambda x: int(x) - 1,
                            
                            # Mixed format patterns
                            r"(?:first|1st)\s+(?:show|see)\s+listing\s*#?\s*(-?\d+)": lambda x: int(x) - 1,
                            r"(?:either\s+)?listing\s*#?\s*(-?\d+)(?:\s+or\s+(?:the\s+)?(?:second|2nd|third|3rd)\s+(?:one|listing))?": lambda x: int(x) - 1
                        }
                        
                        # Try each pattern
                        for pattern, index_func in listing_patterns.items():
                            match = re.search(pattern, message_lower)
                            if match:
                                try:
                                    num = int(match.group(1))
                                    if num > 0:  # Only accept positive numbers
                                        listing_index = index_func(match.group(1))
                                    break
                                except:
                                    continue
        
        # Validate index if one was found
        if listing_index is not None:
            # Handle special cases
            if listing_index < 0 or listing_index >= len(listings):
                invalid_msg = create_chat_message_with_metadata(
                    f"I only have {len(listings)} listings available. Please ask about a listing number between 1 and {len(listings)}.",
                    "❌ Invalid Listing Number"
                )
                history.append(invalid_msg)
                # Preserve the current DataFrame
                current_df = create_listings_dataframe(listings, state)
                return (history, gr.update(value=current_df, visible=True), 
                       gr.update(value=f"Showing {len(listings)} listings"), state)
        else:
            # Default to first listing if no specific index found
            listing_index = 0
        
        # Get the requested listing
        listing = listings[listing_index]
        listing_num = listing_index + 1
        
        # Update state to track current listing
        updated_state = update_app_state(state, {
            "current_listing": listing,
            "current_listing_index": listing_index
        })
        
        # Create detailed response
        address = listing.get("address") or listing.get("title", "N/A")
        price = listing.get("price", "N/A")
        url = listing.get("url", "No link available")
        risk_level = listing.get("risk_level", "❓")
        violations = listing.get("building_violations", 0)
        
        response_text = f"""
**Listing #{listing_num} Details:**

🏠 **Address:** {address}
💰 **Price:** {price}
{risk_level} **Safety Level:** {violations} violations
🔗 **Link:** {url}

You can copy and paste this link into your browser to view the full listing with photos and contact information!

**Would you like to know more about this listing? I can help you with:**
1. 🚇 See the nearest subway/transit options
2. 🏫 See nearby schools
3. 📧 Draft an email to inquire about this listing
4. 🏠 View another listing

Just let me know what information you'd like to see!
        """.strip()
        
        listing_response_msg = create_chat_message_with_metadata(
            response_text,
            f"🏠 Listing #{listing_num} Details"
        )
        history.append(listing_response_msg)
        
        # Preserve existing DataFrame
        current_df = create_listings_dataframe(listings, updated_state)
        return (history, gr.update(value=current_df, visible=True), 
               gr.update(value=f"Showing {len(listings)} listings"), updated_state)

    # Add this function before handle_chat_message
    def handle_listing_context_question(message: str, history: list, state: Dict):
        """Handle context-dependent questions about the current listing (subway, school proximity)."""
        current_listing = state.get("current_listing")
        current_listing_index = state.get("current_listing_index")
        
        # If no current listing but user is asking about transit/schools, give helpful response
        if not current_listing:
            message_lower = message.lower().strip()
            
            # Check if it's a transit/school question
            transit_school_patterns = [
                r'nearest\s+(subway|train|school|transit)',
                r'closest\s+(subway|train|school|transit)',
                r'what.*?(nearest|closest)\s+(subway|train|school|transit)',
                r'where.*?(nearest|closest)\s+(subway|train|school|transit)',
                r'how.*?far.*?(subway|train|school|transit)',
                r'^(subway|train|school|transit)\?$'
            ]
            
            import re
            if any(re.search(pattern, message_lower) for pattern in transit_school_patterns):
                response_text = """🚇 **Transit & School Information**

To find the nearest subway stations and schools for a specific listing, I need to know which property you're interested in. 

**Please:**
1. First search for listings using a command like "find me apartments in Brooklyn" or "search for Section 8 housing"
2. Then ask about transit/schools for a specific listing (e.g., "what's the nearest subway for listing #1")

**Or if you have a specific address in mind:**
Tell me the address and I can look up transit and school information for that location.

Would you like me to help you search for listings first?"""
                
                context_msg = create_chat_message_with_metadata(
                    response_text,
                    "🚇 Transit & School Info"
                )
                history.append(context_msg)
                return (history, gr.update(), gr.update(value="Please search for listings first"), state)
            else:
                return None  # Not a transit/school question, continue with normal flow
        
        # Continue with normal context question handling for when we have a current listing
        
        message_lower = message.lower().strip()
        listing_num = (current_listing_index or 0) + 1
        address = current_listing.get("address") or current_listing.get("title", "N/A")
        
        # Check for subway/transit request
        subway_patterns = [
            r'subway', r'transit', r'train', r'nearest.*subway', r'closest.*subway',
            r'nearest.*transit', r'closest.*transit', r'what.*nearest.*subway', 
            r'where.*nearest.*subway', r'how.*far.*subway', r'what.*nearest.*transit',
            r'where.*nearest.*transit', r'how.*far.*transit'
        ]
        
        # Check for school request  
        school_patterns = [
            r'school', r'nearest.*school', r'closest.*school', r'what.*nearest.*school',
            r'where.*nearest.*school', r'how.*far.*school'
        ]
        
        import re
        
        def get_coordinates_for_listing(listing):
            """Get coordinates for a listing, using geocoding if necessary."""
            # First try to get coordinates directly from listing
            if listing.get("latitude") and listing.get("longitude"):
                return (float(listing["latitude"]), float(listing["longitude"]))
            
            # If no direct coordinates, try to geocode the address
            address = listing.get("address")
            if not address or address == "N/A":
                return None
            
            try:
                # Use the geocoding tool
                from geocoding_tool import GeocodingTool
                geocoder = GeocodingTool()
                geocode_result_json = geocoder.forward(address)
                geocode_result = json.loads(geocode_result_json)
                
                if geocode_result.get("status") == "success":
                    data = geocode_result.get("data", {})
                    lat = data.get("latitude")
                    lon = data.get("longitude")
                    if lat and lon:
                        return (float(lat), float(lon))
                
                return None
            except Exception as e:
                print(f"❌ Geocoding error: {e}")
                return None
        
        # Handle subway questions
        if any(re.search(pattern, message_lower) for pattern in subway_patterns):
            # Try to get coordinates from the current listing
            coordinates = get_coordinates_for_listing(current_listing)
            
            if coordinates:
                try:
                    # Use the nearest subway tool directly
                    from nearest_subway_tool import nearest_subway_tool
                    subway_result_json = nearest_subway_tool.forward(coordinates[0], coordinates[1])
                    subway_result = json.loads(subway_result_json)
                    
                    if subway_result.get("status") == "success":
                        data = subway_result.get("data", {})
                        station_name = data.get("station_name", "Unknown")
                        lines = data.get("lines", "N/A")
                        distance = data.get("distance_miles", 0)
                        is_accessible = data.get("is_accessible", False)
                        entrance_type = data.get("entrance_type", "Unknown")
                        
                        accessibility_text = "♿ Wheelchair accessible" if is_accessible else f"⚠️ Not wheelchair accessible ({entrance_type} entrance)"
                        walking_time = round(distance * 20) if distance else "N/A"  # 20 minutes per mile at 3 mph
                        
                        response_text = f"""
🚇 **Nearest Subway Information for Listing #{listing_num}:**

**Station:** {station_name}
**Lines:** {lines}
**Distance:** {distance:.2f} miles (about {walking_time} minute walk)
**Accessibility:** {accessibility_text}

Would you like to:
1. 🏫 See nearby schools for this listing?
2. 📧 Draft an email to inquire about this listing?
3. 🏠 View another listing?
                        """.strip()
                    else:
                        response_text = f"""
🚇 **Subway Information for Listing #{listing_num}:**

I couldn't find detailed subway information for this listing at the moment. 

**Address:** {address}

You can check the MTA website or app for nearby stations using this address.

Would you like to:
1. 🏫 See nearby schools for this listing?
2. 📧 Draft an email to inquire about this listing?
3. 🏠 View another listing?
                        """.strip()
                        
                except Exception as e:
                    response_text = f"""
🚇 **Subway Information for Listing #{listing_num}:**

I encountered an error while looking up subway information: {str(e)}

**Address:** {address}

You can check the MTA website or app for nearby stations using this address.

Would you like to:
1. 🏫 See nearby schools for this listing?
2. 📧 Draft an email to inquire about this listing?
3. 🏠 View another listing?
                    """.strip()
            else:
                response_text = f"""
🚇 **Subway Information for Listing #{listing_num}:**

I don't have coordinate data for this listing to find nearby subway stations.

**Address:** {address}

You can check the MTA website or app for nearby stations using this address.

Would you like to:
1. 🏫 See nearby schools for this listing?
2. 📧 Draft an email to inquire about this listing?
3. 🏠 View another listing?
            """.strip()
            
            subway_msg = create_chat_message_with_metadata(
                response_text,
                f"🚇 Subway Info - Listing #{listing_num}"
            )
            history.append(subway_msg)
            
            # Preserve existing DataFrame
            listings = state.get("listings", [])
            current_df = create_listings_dataframe(listings, state)
            return (history, gr.update(value=current_df, visible=True), 
                   gr.update(value=f"Showing {len(listings)} listings"), state)
        
        # Handle school questions
        elif any(re.search(pattern, message_lower) for pattern in school_patterns):
            # Try to get coordinates from the current listing
            coordinates = get_coordinates_for_listing(current_listing)
            
            if coordinates:
                try:
                    # Use the near school tool directly
                    from near_school_tool import near_school_tool
                    school_result_json = near_school_tool.forward(coordinates[0], coordinates[1])
                    school_result = json.loads(school_result_json)
                    
                    if school_result.get("status") == "success":
                        schools = school_result.get("data", {}).get("schools", [])
                        
                        if schools:
                            response_text = f"🏫 **Nearby Schools for Listing #{listing_num}:**\n\n"
                            
                            for i, school in enumerate(schools[:3], 1):  # Show top 3 schools
                                name = school.get("school_name", "Unknown School")
                                school_type = school.get("school_type", "Unknown")
                                grades = school.get("grades", "N/A")
                                distance = school.get("distance_miles", 0)
                                walking_time = school.get("walking_time_minutes", "N/A")
                                school_address = school.get("address", "N/A")
                                
                                response_text += f"""
{i}. **{name}**
   - Type: {school_type}
   - Grades: {grades}
   - Distance: {distance:.2f} miles ({walking_time} minute walk)
   - Address: {school_address}
"""
                            
                            response_text += f"""
Would you like to:
1. 🚇 See the nearest subway/transit options?
2. 📧 Draft an email to inquire about this listing?
3. 🏠 View another listing?
                            """.strip()
                        else:
                            response_text = f"""
🏫 **School Information for Listing #{listing_num}:**

No schools found within a reasonable distance of this listing.

**Address:** {address}

You can check the NYC Department of Education website for school zone information.

Would you like to:
1. 🚇 See the nearest subway/transit options?
2. 📧 Draft an email to inquire about this listing?
3. 🏠 View another listing?
                            """.strip()
                    else:
                        response_text = f"""
🏫 **School Information for Listing #{listing_num}:**

I couldn't find detailed school information for this listing at the moment.

**Address:** {address}

You can check the NYC Department of Education website for nearby schools using this address.

Would you like to:
1. 🚇 See the nearest subway/transit options?
2. 📧 Draft an email to inquire about this listing?
3. 🏠 View another listing?
                        """.strip()
                        
                except Exception as e:
                    response_text = f"""
🏫 **School Information for Listing #{listing_num}:**

I encountered an error while looking up school information: {str(e)}

**Address:** {address}

You can check the NYC Department of Education website for nearby schools using this address.

Would you like to:
1. 🚇 See the nearest subway/transit options?
2. 📧 Draft an email to inquire about this listing?
3. 🏠 View another listing?
                    """.strip()
            else:
                response_text = f"""
🏫 **School Information for Listing #{listing_num}:**

I don't have coordinate data for this listing to find nearby schools.

**Address:** {address}

You can check the NYC Department of Education website for nearby schools using this address.

Would you like to:
1. 🚇 See the nearest subway/transit options?
2. 📧 Draft an email to inquire about this listing?
3. 🏠 View another listing?
            """.strip()
            
            school_msg = create_chat_message_with_metadata(
                response_text,
                f"🏫 School Info - Listing #{listing_num}"
            )
            history.append(school_msg)
            
            # Preserve existing DataFrame
            listings = state.get("listings", [])
            current_df = create_listings_dataframe(listings, state)
            return (history, gr.update(value=current_df, visible=True), 
                   gr.update(value=f"Showing {len(listings)} listings"), state)
        
        # Not a recognized context question
        return None

    def handle_chat_message(message: str, history: list, current_state: Dict, 
                           strict_mode: bool):
        """Enhanced chat handler with new agent workflow and state management."""
        
        # CRITICAL DEBUG: Log everything at the entry point
        print(f"🚨 CHAT HANDLER CALLED:")
        print(f"  Message: '{message}'")
        print(f"  Strict mode: {strict_mode}")
        
        log_tool_action("GradioApp", "user_message_received", {
            "message": message,
            "timestamp": current_timestamp()
        })
        
        # Detect language from user message
        detected_language = detect_language_from_message(message)
        current_language = current_state.get("preferences", {}).get("language", "en")
        
        # Check if language has changed based on user input
        language_changed = False
        if detected_language != current_language and detected_language != "en":
            # Language changed - update state and greeting
            current_language = detected_language
            language_changed = True
            print(f"🌍 Language detected: {detected_language}")
        
        # Add user message to history
        history.append({"role": "user", "content": message})
        
        # Update preferences in state (including detected language)
        new_state = update_app_state(current_state, {
            "preferences": {
                "strict_mode": strict_mode,
                "language": current_language
            }
        })
        
        try:
            # Check for context-dependent questions about current listing first
            if detect_context_dependent_question(message) and new_state.get("current_listing"):
                print(f"🔍 CALLING handle_listing_context_question")
                context_result = handle_listing_context_question(message, history, new_state)
                if context_result:
                    return context_result
            
            # Use V0's enhanced classification
            message_type = enhanced_classify_message(message, new_state)
            
            if message_type == "email_request":
                print(f"📧 CALLING enhanced_handle_email_request")
                # Call V0's enhanced email handler
                enhanced_result = enhanced_handle_email_request(message, history, new_state)
                # Return with state preservation
                return (enhanced_result[0], enhanced_result[1], 
                       gr.update(value="Email template generated"), new_state)
            elif message_type == "shortlist_command":
                print(f"📌 CALLING handle_shortlist_command")
                return handle_shortlist_command(message, history, new_state)
            elif message_type == "new_search":
                print(f"🏠 CALLING handle_housing_search")
                return handle_housing_search(message, history, new_state, strict_mode)
            elif message_type == "listing_question":
                print(f"📋 CALLING handle_listing_question")
                return handle_listing_question(message, history, new_state)
            else:
                print(f"💬 CALLING handle_general_conversation")
                # Handle general conversation with caseworker agent
                return handle_general_conversation(message, history, new_state)
                
        except Exception as e:
            log_tool_action("GradioApp", "error", {
                "error": str(e),
                "message": message
            })
            
            error_msg = create_chat_message_with_metadata(
                f"I apologize, but I encountered an error: {str(e)}",
                "❌ Error"
            )
            history.append(error_msg)
            
            return (history, gr.update(value=pd.DataFrame(), visible=False), 
                   gr.update(value="Error occurred"), new_state)

    def handle_housing_search(message: str, history: list, state: Dict, 
                            strict_mode: bool):
        """Handle housing search requests with the new agent workflow."""
        search_id = f"search_{datetime.now(timezone.utc).strftime('%Y%m%d_%H%M%S')}"
        
        # Extract borough from message if mentioned
        message_lower = message.lower()
        detected_borough = None
        borough_map = {
            "bronx": "bronx",
            "brooklyn": "brooklyn", 
            "manhattan": "manhattan",
            "queens": "queens",
            "staten island": "staten_island"
        }
        
        for borough_name, borough_code in borough_map.items():
            if borough_name in message_lower:
                detected_borough = borough_code
                break
        
        # Use detected borough from message
        if detected_borough:
            target_borough = detected_borough
            print(f"🎯 Using detected borough from message: {detected_borough}")
        else:
            target_borough = None
            print(f"🌍 No borough specified - will search all boroughs")
        
        # Update search message based on target
        if target_borough:
            search_text = f"🔍 Searching for voucher-friendly listings in {target_borough.title()}..."
            print(f"🎯 BOROUGH FILTER ACTIVE: Searching only {target_borough.upper()}")
        else:
            search_text = "🔍 Searching for voucher-friendly listings across NYC..."
            print(f"🌍 NO BOROUGH FILTER: Searching all NYC boroughs")
            
        search_msg = create_chat_message_with_metadata(
            search_text,
            "🔍 Searching Listings",
            parent_id=search_id
        )
        history.append(search_msg)
        
        try:
            # Use BrowserAgent to search for listings
            search_query = "Section 8"
            
            # Debug: Log exactly what we're passing to browser agent
            boroughs_param = target_borough if target_borough else ""
            print(f"📡 Calling browser_agent.forward with boroughs='{boroughs_param}'")
            
            browser_result = browser_agent.forward(
                query=search_query,
                boroughs=boroughs_param
            )
            
            browser_data = json.loads(browser_result)
            
            if browser_data.get("status") != "success":
                error_msg = create_chat_message_with_metadata(
                    f"❌ Search failed: {browser_data.get('error', 'Unknown error')}",
                    "❌ Search Failed"
                )
                history.append(error_msg)
                return (history, gr.update(), gr.update(value="Search failed"), state)
            
            listings = browser_data["data"]["listings"]
            search_duration = browser_data["data"]["metadata"]["duration"]
            
            # Update search completion message
            search_complete_msg = create_chat_message_with_metadata(
                f"✅ Found {len(listings)} potential listings",
                "🔍 Search Complete",
                duration=search_duration,
                parent_id=search_id
            )
            history.append(search_complete_msg)
            
            if not listings:
                no_results_msg = create_chat_message_with_metadata(
                    "I couldn't find any voucher-friendly listings matching your criteria. Try adjusting your search parameters.",
                    "📋 No Results"
                )
                history.append(no_results_msg)
                return (history, gr.update(), gr.update(value="No listings found"), state)
            
            # Stage 2: Enrich listings with violation data
            violation_msg = create_chat_message_with_metadata(
                f"🏢 Checking building safety for {len(listings)} listings...",
                "🏢 Safety Analysis",
                parent_id=search_id
            )
            history.append(violation_msg)
            
            enriched_listings = []
            for i, listing in enumerate(listings):
                address = listing.get("address") or listing.get("title", "")
                if not address:
                    # Add default violation data if no address
                    enriched_listing = {
                        **listing,
                        "building_violations": 0,
                        "risk_level": RiskLevel.UNKNOWN.value,
                        "last_inspection": "N/A",
                        "violation_summary": "No address available"
                    }
                    enriched_listings.append(enriched_listing)
                    continue
                
                try:
                    violation_result = violation_agent.forward(address)
                    violation_data = json.loads(violation_result)
                    
                    if violation_data.get("status") == "success":
                        data = violation_data.get("data", {})
                        enriched_listing = {
                            **listing,
                            "building_violations": data.get("violations", 0),
                            "risk_level": data.get("risk_level", RiskLevel.UNKNOWN.value),
                            "last_inspection": data.get("last_inspection", "N/A"),
                            "violation_summary": data.get("summary", "No data available")
                        }
                    else:
                        # Add default violation data if check failed
                        enriched_listing = {
                            **listing,
                            "building_violations": 0,
                            "risk_level": RiskLevel.UNKNOWN.value,
                            "last_inspection": "N/A",
                            "violation_summary": "Could not verify"
                        }
                    
                    enriched_listings.append(enriched_listing)
                    
                except Exception as e:
                    print(f"❌ Failed to check violations for {address}: {str(e)}")
                    # Add default violation data on error
                    enriched_listing = {
                        **listing,
                        "building_violations": 0,
                        "risk_level": RiskLevel.UNKNOWN.value,
                        "last_inspection": "N/A",
                        "violation_summary": "Check failed"
                    }
                    enriched_listings.append(enriched_listing)
            
            # Update enrichment completion message
            enrichment_complete_msg = create_chat_message_with_metadata(
                f"✅ Safety analysis complete for {len(enriched_listings)} listings",
                "🏢 Safety Analysis Complete",
                parent_id=search_id
            )
            history.append(enrichment_complete_msg)
            
            # Apply strict mode filtering if enabled
            filtered_listings = filter_listings_strict_mode(enriched_listings, strict_mode)
            
            # Update state with listings
            updated_state = update_app_state(state, {
                "listings": filtered_listings,
                "current_listing": None,
                "current_listing_index": None
            })
            
            # Create DataFrame for display
            if filtered_listings:
                df = create_listings_dataframe(filtered_listings, updated_state)
                
                results_msg = create_chat_message_with_metadata(
                    f"🎉 Found {len(filtered_listings)} voucher-friendly listings for you!",
                    "✅ Search Results"
                )
                history.append(results_msg)
                
                return (history, gr.update(value=df, visible=True), 
                       gr.update(value=f"Showing {len(filtered_listings)} listings"), 
                       updated_state)
            else:
                no_safe_msg = create_chat_message_with_metadata(
                    "No safe listings found with current criteria. Try adjusting your filters.",
                    "📋 No Safe Listings"
                )
                history.append(no_safe_msg)
                
                return (history, gr.update(visible=False), 
                       gr.update(value="No listings match criteria"), 
                       updated_state)
                
        except Exception as e:
            error_msg = create_chat_message_with_metadata(
                f"Search failed with error: {str(e)}",
                "❌ Search Error"
            )
            history.append(error_msg)
            return (history, gr.update(), gr.update(value="Search error occurred"), state)

    def handle_general_conversation(message: str, history: list, state: Dict):
        """Handle general conversation using the caseworker agent."""
        try:
            current_language = state.get("preferences", {}).get("language", "en")
            
            # Enhanced message context with comprehensive question type detection
            message_lower = message.lower()
            
            # Detect question type
            is_documentation_request = any(pattern in message_lower for pattern in [
                "where can i find", "how do i find", "where do i find",
                "documentation", "guide", "tutorial", "instructions",
                "forms", "paperwork", "application", "documents",
                "where are the", "where is the", "where are", "where is"
            ])
            
            is_voucher_how_to = any(pattern in message_lower for pattern in [
                "how do i", "how can i", "what do i do", "what's the process",
                "how to use", "how does", "what should i"
            ])
            
            is_voucher_info = any(pattern in message_lower for pattern in [
                "what's the difference", "what does", "can i", "does my voucher",
                "am i eligible", "do i have to", "is it possible",
                "difference between", "vs", "versus", "compared to"
            ])
            
            is_timeline_question = any(pattern in message_lower for pattern in [
                "when do i", "how long does", "why haven't i", "what's the status",
                "when will", "deadline", "extension", "expire", "expiration"
            ])
            
            is_rights_question = any(pattern in message_lower for pattern in [
                "can a landlord", "is it legal", "discrimination", "rights",
                "allowed to", "required to", "refuse", "deny", "reject"
            ])
            
            # Determine question type for context
            if is_documentation_request:
                question_type = "documentation request"
            elif is_voucher_how_to:
                question_type = "voucher usage guidance"
            elif is_timeline_question:
                question_type = "timeline and process question"
            elif is_rights_question:
                question_type = "rights and legal information"
            elif is_voucher_info:
                question_type = "voucher information request"
            else:
                question_type = "general voucher question"
            
            enhanced_message = f"""
User message: {message}

Context: This is a {question_type} from someone seeking information about voucher-friendly housing in NYC.

Key Response Guidelines:
1. If this is a voucher usage question, provide step-by-step guidance
2. If this is about rights/discrimination, include relevant legal information
3. If this is about timelines/deadlines, be specific about processes and requirements
4. If they ask about specific listings without having searched, let them know they need to search first
5. Always be helpful, empathetic, and knowledgeable about housing, NYC neighborhoods, and voucher programs

For voucher questions, include:
- Clear, actionable steps when applicable
- References to relevant agencies or resources
- Common pitfalls to avoid
- Next steps or follow-up actions
            """.strip()
            
            # Add language context to the message
            language_context = f"""
IMPORTANT: The user's preferred language is '{current_language}'. Please respond in this language:
- en = English
- es = Spanish  
- zh = Chinese (Simplified)
- bn = Bengali

User message: {enhanced_message}
            """.strip()
            
            agent_output = caseworker_agent.run(language_context, reset=False)
            response_text = str(agent_output)
            
            # Use appropriate title based on question type
            title_map = {
                "voucher usage guidance": "📋 Voucher Usage Guide",
                "voucher information request": "ℹ️ Voucher Information",
                "timeline and process question": "⏱️ Timeline & Process",
                "rights and legal information": "⚖️ Rights & Legal Info",
                "documentation request": "📚 Documentation Help",
                "general voucher question": "💬 Voucher Assistance"
            }
            
            title = title_map.get(question_type, "💬 General Response")
            
            general_msg = {
                "role": "assistant",
                "content": response_text,
                "metadata": {
                    "title": title,
                    "timestamp": datetime.now().isoformat()
                }
            }
            
            history.append(general_msg)
            
            # Preserve existing DataFrame if we have listings
            listings = state.get("listings", [])
            if listings:
                current_df = create_listings_dataframe(listings, state)
                return (history, gr.update(value=current_df, visible=True), 
                       gr.update(value=f"Showing {len(listings)} listings"), state)
            else:
                return (history, gr.update(), gr.update(value="Conversation mode"), state)
            
        except Exception as e:
            error_msg = create_chat_message_with_metadata(
                f"I apologize, but I encountered an error: {str(e)}",
                "❌ Error"
            )
            history.append(error_msg)
            
            # Preserve existing DataFrame even on error
            listings = state.get("listings", [])
            if listings:
                current_df = create_listings_dataframe(listings, state)
                return (history, gr.update(value=current_df, visible=True), 
                       gr.update(value=f"Error occurred - {len(listings)} listings still available"), state)
            else:
                return (history, gr.update(), gr.update(value="Error in conversation"), state)

    def create_listings_dataframe(listings: List[Dict], app_state: Dict = None) -> pd.DataFrame:
        """Create a formatted DataFrame from listings data with shortlist status."""
        df_data = []
        
        # Get shortlisted IDs for quick lookup
        shortlisted_ids = set()
        if app_state:
            shortlisted_ids = get_shortlisted_ids(app_state)
        
        for i, listing in enumerate(listings, 1):  # Start enumeration at 1
            # Get the address from either 'address' or 'title' field
            address = listing.get("address") or listing.get("title", "N/A")
            
            # Get the URL for the listing and create a shorter display version
            url = listing.get("url", "No link available")
            if url != "No link available":
                # Create a shorter display version of the URL with styling
                url_display = f"""
                <div style="text-align: center;">
                    <a href="{url}" 
                       target="_blank" 
                       style="color: #2196F3; 
                              text-decoration: none; 
                              padding: 4px 8px; 
                              border-radius: 4px;
                              transition: all 0.2s ease;
                              display: inline-block;
                              font-weight: 500;"
                       onmouseover="this.style.backgroundColor='#e3f2fd'; this.style.textDecoration='underline';"
                       onmouseout="this.style.backgroundColor='transparent'; this.style.textDecoration='none';">
                        View Listing →
                    </a>
                </div>"""
            else:
                url_display = '<div style="text-align: center; color: #666;">No link</div>'
            
            # Check if listing is shortlisted
            listing_id = str(listing.get("id", address))
            shortlist_status = "★" if listing_id in shortlisted_ids else "+"
            
            # Format the address to be more readable
            formatted_address = address.replace("section-8", "").replace("section 8", "").strip()
            formatted_address = re.sub(r'\s+', ' ', formatted_address)  # Remove extra spaces
            
            df_data.append({
                "#": i,  # Add the listing number
                "Address": formatted_address,
                "Price": listing.get("price", "N/A"),
                "Risk Level": listing.get("risk_level", "❓"),
                "Violations": listing.get("building_violations", 0),
                "Last Inspection": listing.get("last_inspection", "N/A"),
                "Link": url_display,  # Use the HTML anchor tag version
                "Summary": listing.get("violation_summary", "")[:50] + "..." if len(listing.get("violation_summary", "")) > 50 else listing.get("violation_summary", ""),
                "Shortlist": shortlist_status
            })
        
        return pd.DataFrame(df_data)

    # Wire up the submit action with state management
    send_btn.click(
        handle_chat_message, 
        [msg, chatbot, app_state, strict_mode_toggle], 
        [chatbot, results_df, progress_info, app_state]
    )
    # Add a secondary submit to clear the input box for better UX
    send_btn.click(lambda: "", [], [msg])
    
    # Wire up Enter key submission
    msg.submit(
        handle_chat_message, 
        [msg, chatbot, app_state, strict_mode_toggle], 
        [chatbot, results_df, progress_info, app_state]
    )
    msg.submit(lambda: "", [], [msg])
    
    # Wire up DataFrame shortlist click handler
    results_df.select(
        handle_shortlist_click,
        [app_state],
        [results_df, progress_info, shortlist_display, app_state]
    )
    
    # Language change handler
    def change_language(language, current_state, current_history):
        """Handle language change with greeting update."""
        # Update the language in state
        new_state = update_app_state(current_state, {
            "preferences": {"language": language}
        })
        
        # Create new greeting in the selected language
        new_greeting = create_initial_greeting(language)
        
        # Replace the first message (greeting) if it exists, otherwise add it
        if current_history and len(current_history) > 0 and current_history[0]["role"] == "assistant":
            updated_history = [new_greeting[0]] + current_history[1:]
        else:
            updated_history = new_greeting + current_history
        
        return updated_history, new_state
    
    # Language change event
    language_dropdown.change(
        change_language,
        [language_dropdown, app_state, chatbot],
        [chatbot, app_state]
    )
    
    # Dark mode toggle using the correct JavaScript approach
    dark_mode_toggle.click(
        fn=None,
        js="""
        () => {
            document.body.classList.toggle('dark');
        }
        """
    )
    
if __name__ == "__main__":
    demo.launch(i18n=i18n)