huddle 0.7.11

Decentralized, terminal-native chat — LAN mDNS, direct IP dial, or invite link, all Megolm end-to-end encrypted.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
4072
4073
4074
4075
4076
4077
4078
4079
4080
4081
4082
4083
4084
4085
4086
4087
4088
4089
4090
4091
4092
4093
4094
4095
4096
4097
4098
4099
4100
4101
4102
4103
4104
4105
4106
4107
4108
4109
4110
4111
4112
4113
4114
4115
4116
4117
4118
4119
4120
4121
4122
4123
4124
4125
4126
4127
4128
4129
4130
4131
4132
4133
4134
4135
4136
4137
4138
4139
4140
4141
4142
4143
4144
4145
4146
4147
4148
4149
4150
4151
4152
4153
4154
4155
4156
4157
4158
4159
4160
4161
4162
4163
4164
4165
4166
4167
4168
4169
4170
4171
4172
4173
4174
4175
4176
4177
4178
4179
4180
4181
4182
4183
4184
4185
4186
4187
4188
4189
4190
4191
4192
4193
4194
4195
4196
4197
4198
4199
4200
4201
4202
4203
4204
4205
4206
4207
4208
4209
4210
4211
4212
4213
4214
4215
4216
4217
4218
4219
4220
4221
4222
4223
4224
4225
4226
4227
4228
4229
4230
4231
4232
4233
4234
4235
4236
4237
4238
4239
4240
4241
4242
4243
4244
4245
4246
4247
4248
4249
4250
4251
4252
4253
4254
4255
4256
4257
4258
4259
4260
4261
4262
4263
4264
4265
4266
4267
4268
4269
4270
4271
4272
4273
4274
4275
4276
4277
4278
4279
4280
4281
4282
4283
4284
4285
4286
4287
4288
4289
4290
4291
4292
4293
4294
4295
4296
4297
4298
4299
4300
4301
4302
4303
4304
4305
4306
4307
4308
4309
4310
4311
4312
4313
4314
4315
4316
4317
4318
4319
4320
4321
4322
4323
4324
4325
4326
4327
4328
4329
4330
4331
4332
4333
4334
4335
4336
4337
4338
4339
4340
4341
4342
4343
4344
4345
4346
4347
4348
4349
4350
4351
4352
4353
4354
4355
4356
4357
4358
4359
4360
4361
4362
4363
4364
4365
4366
4367
4368
4369
4370
4371
4372
4373
4374
4375
4376
4377
4378
4379
4380
4381
4382
4383
4384
4385
4386
4387
4388
4389
4390
4391
4392
4393
4394
4395
4396
4397
4398
4399
4400
4401
4402
4403
4404
4405
4406
4407
4408
4409
4410
4411
4412
4413
4414
4415
4416
4417
4418
4419
4420
4421
4422
4423
4424
4425
4426
4427
4428
4429
4430
4431
4432
4433
4434
4435
4436
4437
4438
4439
4440
4441
4442
4443
4444
4445
4446
4447
4448
4449
4450
4451
4452
4453
4454
4455
4456
4457
4458
4459
4460
4461
4462
4463
4464
4465
4466
4467
4468
4469
4470
4471
4472
4473
4474
4475
4476
4477
4478
4479
4480
4481
4482
4483
4484
4485
4486
4487
4488
4489
4490
4491
4492
4493
4494
4495
4496
4497
4498
4499
4500
4501
4502
4503
4504
4505
4506
4507
4508
4509
4510
4511
4512
4513
4514
4515
4516
4517
4518
4519
4520
4521
4522
4523
4524
4525
4526
4527
4528
4529
4530
4531
4532
4533
4534
4535
4536
4537
4538
4539
4540
4541
4542
4543
4544
4545
4546
4547
4548
4549
4550
4551
4552
4553
4554
4555
4556
4557
4558
4559
4560
4561
4562
4563
4564
4565
4566
4567
4568
4569
4570
4571
4572
4573
4574
4575
4576
4577
4578
4579
4580
4581
4582
4583
4584
4585
4586
4587
4588
4589
4590
4591
4592
4593
4594
4595
4596
4597
4598
4599
4600
4601
4602
4603
4604
4605
4606
4607
4608
4609
4610
4611
4612
4613
4614
4615
4616
4617
4618
4619
4620
4621
4622
4623
4624
4625
4626
4627
4628
4629
4630
4631
4632
4633
4634
4635
4636
4637
4638
4639
4640
4641
4642
4643
4644
4645
4646
4647
4648
4649
4650
4651
4652
4653
4654
4655
4656
4657
4658
4659
4660
4661
4662
4663
4664
4665
4666
4667
4668
4669
4670
4671
4672
4673
4674
4675
4676
4677
4678
4679
4680
4681
4682
4683
4684
4685
4686
4687
4688
4689
4690
4691
4692
4693
4694
4695
4696
4697
4698
4699
4700
4701
4702
4703
4704
4705
4706
4707
4708
4709
4710
4711
4712
4713
4714
4715
4716
4717
4718
4719
4720
4721
4722
4723
4724
4725
4726
4727
4728
4729
4730
4731
4732
4733
4734
4735
4736
4737
4738
4739
4740
4741
4742
4743
4744
4745
4746
4747
4748
4749
4750
4751
4752
4753
4754
4755
4756
4757
4758
4759
4760
4761
4762
4763
4764
4765
4766
4767
4768
4769
4770
4771
4772
4773
4774
4775
4776
4777
4778
4779
4780
4781
4782
4783
4784
4785
4786
4787
4788
4789
4790
4791
4792
4793
4794
4795
4796
4797
4798
4799
4800
4801
4802
4803
4804
4805
4806
4807
4808
4809
4810
4811
4812
4813
4814
4815
4816
4817
4818
4819
4820
4821
4822
4823
4824
4825
4826
4827
4828
4829
4830
4831
4832
4833
4834
4835
4836
4837
4838
4839
4840
4841
4842
4843
4844
4845
4846
4847
4848
4849
4850
4851
4852
4853
4854
4855
4856
4857
4858
4859
4860
4861
4862
4863
4864
4865
4866
4867
4868
4869
4870
4871
4872
4873
4874
4875
4876
4877
4878
4879
4880
4881
4882
4883
4884
4885
4886
4887
4888
4889
4890
4891
4892
4893
4894
4895
4896
4897
4898
4899
4900
4901
4902
4903
4904
4905
4906
4907
4908
4909
4910
4911
4912
4913
4914
4915
4916
4917
4918
4919
4920
4921
4922
4923
4924
4925
4926
4927
4928
4929
4930
4931
4932
4933
4934
4935
4936
4937
4938
4939
4940
4941
4942
4943
4944
4945
4946
4947
4948
4949
4950
4951
4952
4953
4954
4955
4956
4957
4958
4959
4960
4961
4962
4963
4964
4965
4966
4967
4968
4969
4970
4971
4972
4973
4974
4975
4976
4977
4978
4979
4980
4981
4982
4983
4984
4985
4986
4987
4988
4989
4990
4991
4992
4993
4994
use std::cell::Cell;
use std::collections::{HashMap, HashSet, VecDeque};
use std::io;
use std::sync::{Arc, Mutex};
use std::time::{Duration, Instant};

use anyhow::Result;
use crossterm::{
    event::{
        self, poll, DisableFocusChange, DisableMouseCapture, EnableFocusChange,
        EnableMouseCapture, Event, MouseButton, MouseEventKind,
    },
    execute,
    terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
};
use ratatui::prelude::*;
use ratatui::Terminal;

use base64::Engine;
use huddle_core::app::events::{AppEvent, DiscoveredRoom};
use huddle_core::app::{AppHandle, KnownPeerStatus};
use huddle_core::network::NetworkMode;
use huddle_core::storage::repo::{StoredAttachment, StoredRoomMessage};
use libp2p::PeerId;

use crate::input::{self, Action};

/// Default lifetime for transient status-bar messages.
const STATUS_TTL: Duration = Duration::from_secs(6);

/// Maximum entries kept in the status history ring buffer.
pub const STATUS_HISTORY_CAP: usize = 100;

/// Maximum modals we queue behind the active one. Beyond this we drop
/// the oldest — see `enqueue_modal`.
pub const PENDING_MODAL_CAP: usize = 16;

/// huddle 0.6: an onboarding page tagged with the version it was
/// introduced in. Users only see pages whose `min_version` is newer
/// than their last_seen_onboarding_version, so a version bump
/// surfaces only the new "what's new" page — not the foundational
/// cards from before.
pub struct OnboardingPage {
    pub title: &'static str,
    pub body: &'static [&'static str],
    /// Pages with `min_version` greater than the user's last-seen
    /// onboarding version are surfaced. "0.0.0" = foundational pages
    /// shown only on true first launch.
    pub min_version: &'static str,
}

/// Phase H + huddle 0.6: onboarding pages. The mental-model and
/// passphrase pages are foundational (min_version="0.0.0"); each
/// release that ships a user-visible change adds one "what's new"
/// page tagged with its release version.
pub const ONBOARDING_PAGES: &[OnboardingPage] = &[
    OnboardingPage {
        title: "huddle is not iMessage",
        body: &[
            "every member is a peer — no host, no central server.",
            "rooms outlive whoever created them.",
            "anyone with the room passphrase can join, send, rotate the key.",
            "leaderless + persistent mesh; the protocol has no admin tier",
            "by default, only the soft 'owner' role you can grant per room.",
            "",
            "press → / Tab / Enter / Space to continue.",
        ],
        min_version: "0.0.0",
    },
    OnboardingPage {
        title: "passphrase ≠ password",
        body: &[
            "the master passphrase encrypts your LOCAL database (rooms,",
            "messages, members, Megolm sessions, attachments).",
            "room passphrases are the access keys to encrypted rooms.",
            "neither is recoverable — there's no reset, by design.",
            "",
            "for sharing access without leaking your passphrase, use",
            "  ^J  generate a 10-min single-use join code",
            "  ^V→s  SAS-verify a member's fingerprint",
            "  ^I  produce an invite link (passphrase still OOB)",
        ],
        min_version: "0.0.0",
    },
    OnboardingPage {
        title: "what's new in 0.5",
        body: &[
            "  a    add friend by HD ID or username — races LAN / IP / relay",
            "  ,→u  set / clear your username (signed broadcast)",
            "  Alt+Shift+1  delete account + wipe data dir (go dark)",
            "  ✓    green tag next to SAS-verified peers in chat",
            "  HD-  branded ID, shown alongside username everywhere",
        ],
        min_version: "0.5.0",
    },
    OnboardingPage {
        title: "what's new in 0.6 — UX overhaul",
        body: &[
            "  Ctrl+P  command palette — fuzzy search every action",
            "  Ctrl+H  notification history (last 100 status events)",
            "  Shift+? re-open this card anytime ('show what's new')",
            "  ?       help screen is now generated from input.rs —",
            "          every key is documented, scroll with j/k.",
            "  R       (lobby) mark every room read",
            "",
            "  · version + clock in the lobby header",
            "  · live peer counter next to the NAT badge",
            "  · per-tab unread counts (instead of '*')",
            "  · scroll indicator + day separators in chat",
            "  · '[N pending]' badge when a modal event was queued",
            "  · update banner (opt-in) — checks crates.io every 24h",
            "  · `huddle doctor` CLI subcommand for bug reports",
        ],
        min_version: "0.6.0",
    },
    OnboardingPage {
        title: "what's new in 0.7 — TUI 2.0",
        body: &[
            "huddle 0.7 rewrites the TUI around a sidebar:",
            "",
            "  Profile        you, your HD-ID, NAT badge",
            "  Direct messages  1-1 DMs (encrypted, 2-people-forever)",
            "  Group rooms      every multi-peer room you've joined",
            "  People           known peers + verified + blocked",
            "  Activity         status history + transfers",
            "  Settings         toggles + go-dark",
            "",
            "New keys:",
            "  m   start a DM (Compose-DM modal)",
            "  g   start a group room",
            "  p   jump to People pane",
            "  ,   jump to Settings pane",
            "  Tab/Shift+Tab   jump between sidebar sections",
            "  Space/←/→       expand/collapse a section",
            "  Alt+M           toggle the member margin in a Group",
            "",
            "Retired: tab-bar, Ctrl+B (back-to-lobby), numeric tab jump.",
            "Direct chats and group chats are now visually distinct;",
            "DMs land in the Direct messages section and stay 1-1.",
            "",
            "Note: v1 DMs aren't end-to-end encrypted on the room layer",
            "(visibility-filter + topic-ID obscurity); v0.8 will add E2E.",
        ],
        min_version: "0.7.0",
    },
    OnboardingPage {
        title: "what's new in 0.7.1 — E2E DMs",
        body: &[
            "DMs are now end-to-end encrypted on the room layer.",
            "",
            "Each DM derives a Megolm wrap key from an Ed25519→X25519",
            "ECDH between you and the other party's identity keys,",
            "bound to the canonical room_id via HKDF-SHA256. No shared",
            "passphrase, no extra prompt — `m` starts a DM and it's",
            "E2E from the first wrapped session key onward.",
            "",
            "The wire shape didn't change: DMs ride the same MemberAnnounce",
            "+ wrapped-Megolm-session-key flow as encrypted group rooms.",
            "Only the derivation of the wrap key changed.",
            "",
            "DMs created on 0.7.0 stay in their original (encrypted=false)",
            "mode for back-compat; new DMs created on 0.7.1 are E2E.",
        ],
        min_version: "0.7.1",
    },
    OnboardingPage {
        title: "what's new in 0.7.4 — desktop notifications",
        body: &[
            "huddle now fires native desktop notifications when a",
            "message arrives and the terminal isn't focused. Switch to",
            "another app, lock your screen, drag the window off-display —",
            "you'll still get pinged. When the terminal IS focused, no",
            "notification fires (the message is right in front of you).",
            "",
            "Catch-up summary: when you reopen huddle, any messages that",
            "arrived during the 5-second initial sync window are batched",
            "into one notification — \"N new messages while you were away\".",
            "",
            "Go dark moved to Alt+Shift+1 (Option+Shift+1 on macOS,",
            "Alt+Shift+1 on Linux/Windows). Plain `!` was one keystroke",
            "away from nuking your account — the extra modifier is a",
            "deliberate friction. The Settings pane row and the modal",
            "prompt both render `Alt+Shift+1` now.",
            "",
            "macOS only: the first notification triggers a one-time",
            "permission prompt for Script Editor / Terminal — click Allow.",
        ],
        min_version: "0.7.4",
    },
];

/// huddle 0.6: pages that should be shown to a user with the given
/// last-seen onboarding version. Brand-new users see everything;
/// upgrading users see only the pages tagged with versions newer
/// than their last_seen.
pub fn pages_to_show(last_seen: Option<&str>, legacy_onboarding_seen: bool) -> Vec<usize> {
    let baseline = match (last_seen, legacy_onboarding_seen) {
        (Some(v), _) => v.to_string(),
        // Legacy user from before version tracking: they already saw
        // the 0.5 foundational + "what's new" cards. Treat them as
        // having last_seen=0.5.2 so only newer pages surface.
        (None, true) => "0.5.2".to_string(),
        // Brand-new user: every page.
        (None, false) => "0.0.0".to_string(),
    };
    ONBOARDING_PAGES
        .iter()
        .enumerate()
        .filter(|(_, p)| semver_lt(&baseline, p.min_version))
        .map(|(i, _)| i)
        .collect()
}

/// Tiny numeric semver compare — splits on '.' and parses each segment
/// as a u32, treating missing or non-numeric trailers as 0. Enough for
/// our 0.X.Y release numbering; we never ship pre-release tags.
fn semver_lt(a: &str, b: &str) -> bool {
    parse_semver(a) < parse_semver(b)
}

fn parse_semver(s: &str) -> (u32, u32, u32) {
    let mut it = s.split('.');
    let major = it.next().unwrap_or("0").parse().unwrap_or(0);
    let minor = it.next().unwrap_or("0").parse().unwrap_or(0);
    let patch_raw = it.next().unwrap_or("0");
    let patch_num: String = patch_raw.chars().take_while(|c| c.is_ascii_digit()).collect();
    let patch = patch_num.parse().unwrap_or(0);
    (major, minor, patch)
}

/// huddle 0.7: which pane the right side of the layout renders.
/// Replaces the legacy `Screen::{Lobby, InRoom}` binary. Selection
/// happens via the sidebar (or jump-shortcuts like `,` for Settings).
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Pane {
    Welcome,
    Profile,
    Dm(String),
    Group(String),
    People,
    Activity,
    Settings,
}

/// huddle 0.7: sidebar section identifiers. Section order is fixed at
/// render time so this enum mirrors the visual order top-to-bottom.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum SidebarSection {
    Profile,
    Direct,
    Group,
    People,
    Activity,
    Settings,
}

/// huddle 0.7: a single addressable row in the sidebar. `Section(s)` is
/// the section header itself (clickable to expand/collapse); the other
/// variants are children rendered under an expanded section.
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum SidebarItem {
    Section(SidebarSection),
    Profile,
    /// huddle 0.7.8: pinned "+ Add Friend" row at the top of an
    /// expanded Direct messages section. Selecting fires
    /// `Action::OpenComposeDm`.
    DirectAddFriend,
    Dm(String),
    /// huddle 0.7.8: pinned "+ New Group" row at the top of an
    /// expanded Group rooms section. Selecting fires
    /// `Action::OpenStartRoom`.
    GroupNew,
    Group(String),
    GroupDiscover,
    /// huddle 0.7.8: pinned "📩 N friend requests" badge row when the
    /// People section is expanded and `pending_requests` is non-empty.
    /// Selecting jumps to `Pane::People` with `PeopleFocus::Pending`.
    PeoplePendingBadge,
    Person(String),
    Activity,
    Settings,
}

/// huddle 0.7.8: which tab the Settings pane shows. Account = identity
/// (mirrors Profile's read-only view + edit affordances). Network = mDNS
/// toggle, listen addrs, relays, connectivity. Appearance = theme
/// placeholder. Privacy = verified-only inbound, notifications, update
/// check, blocked peers, go-dark.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SettingsTab {
    Account,
    Network,
    Appearance,
    Privacy,
}

impl Default for SettingsTab {
    fn default() -> Self {
        SettingsTab::Account
    }
}

impl SettingsTab {
    pub fn label(self) -> &'static str {
        match self {
            SettingsTab::Account => "Account",
            SettingsTab::Network => "Network",
            SettingsTab::Appearance => "Appearance",
            SettingsTab::Privacy => "Privacy",
        }
    }

    pub fn next(self) -> Self {
        match self {
            SettingsTab::Account => SettingsTab::Network,
            SettingsTab::Network => SettingsTab::Appearance,
            SettingsTab::Appearance => SettingsTab::Privacy,
            SettingsTab::Privacy => SettingsTab::Account,
        }
    }

    pub fn prev(self) -> Self {
        match self {
            SettingsTab::Account => SettingsTab::Privacy,
            SettingsTab::Network => SettingsTab::Account,
            SettingsTab::Appearance => SettingsTab::Network,
            SettingsTab::Privacy => SettingsTab::Appearance,
        }
    }
}

/// huddle 0.7: keyboard focus is either on the sidebar (j/k navigates
/// rows) or on the pane (typing in chat, scrolling messages). Tab/Esc
/// toggles between them.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum SidebarFocus {
    Sidebar,
    Pane,
}

/// huddle 0.7: sidebar state — which item is selected, which sections
/// are expanded, and whether keyboard focus is on the sidebar or the
/// pane. Owns sidebar navigation; the pane router is purely read-only
/// for this state.
#[derive(Debug, Clone)]
pub struct SidebarState {
    pub selection: SidebarItem,
    pub expanded: HashSet<SidebarSection>,
    pub focus: SidebarFocus,
}

impl Default for SidebarState {
    fn default() -> Self {
        let mut expanded = HashSet::new();
        expanded.insert(SidebarSection::Profile);
        expanded.insert(SidebarSection::Direct);
        expanded.insert(SidebarSection::Group);
        expanded.insert(SidebarSection::People);
        Self {
            selection: SidebarItem::Section(SidebarSection::Profile),
            expanded,
            focus: SidebarFocus::Sidebar,
        }
    }
}

/// huddle 0.7: which sublist has focus inside the People pane —
/// Known peers, Verified, or Blocked. Tab cycles. Used for per-row
/// action targeting.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum PeopleFocus {
    /// huddle 0.7.7: pending inbound friend requests, spilled to disk
    /// when their 15s modal window times out. First tab so a forgotten
    /// request from earlier is the first thing the user sees on
    /// landing in People.
    Pending,
    Known,
    Verified,
    Blocked,
}

impl Default for PeopleFocus {
    fn default() -> Self {
        PeopleFocus::Known
    }
}

/// Modal overlays (mutually exclusive).
#[derive(Debug, Clone)]
pub enum Modal {
    None,
    StartRoom(StartRoomState),
    JoinRoom(JoinRoomState),
    DialPeer(DialPeerState),
    AttachPicker(AttachPickerState),
    /// "Rotate the current room's key" — entered with ^R.
    RotateRoom(RotateRoomState),
    /// Someone else rotated our room's key; ask the user for the new
    /// passphrase so we can continue receiving messages.
    AcceptRotation(AcceptRotationState),
    /// Verify member fingerprints (^V).
    Verify(VerifyState),
    /// Search room history (^F).
    Search(SearchState),
    /// QR code of our identity, scannable for fingerprint comparison.
    QrIdentity,
    /// Phase A: an unknown peer dialed us. User chooses accept/reject/
    /// trust-and-accept. Dismiss without choosing (Esc, 15s timeout) =
    /// auto-reject — anything more permissive would defeat the gate.
    InboundDial(InboundDialState),
    /// Phase B: owner-only picker for kick / grant-owner on a member.
    MemberAction(MemberActionState),
    /// Phase G: SAS verification in progress — initial waiting state
    /// before the partner's ephemeral pubkey arrives, then code-display
    /// + match-confirm.
    Sas(SasState),
    /// huddle 0.5: single-field text editor for the local user's
    /// self-declared username. Empty input clears (None) and the user
    /// renders as `[anonymous]` to themselves and peers. On confirm,
    /// triggers a signed ProfileUpdate broadcast to every joined room.
    EditUsername(EditUsernameState),
    /// huddle 0.5: irreversible account-delete modal. Two fields:
    /// master passphrase + a `DELETE EVERYTHING` confirmation phrase.
    /// Hitting Confirm with both filled calls `AppHandle::go_dark`.
    GoDark(GoDarkState),
    /// huddle 0.5.1: "add friend by HD ID or username" — single-field
    /// text input. On confirm, resolves the input to a fingerprint
    /// (HD-prefix / bare hex / username lookup) and dials via the
    /// usual flow.
    AddFriend(AddFriendState),
    /// huddle 0.7: "message who?" — Compose-DM modal. Single field
    /// with inline autocomplete; on confirm, resolves to a fingerprint
    /// and starts a DM via `AppHandle::start_direct`. Falls back to
    /// the AddFriend morph (same modal recycled) when input is
    /// unrecognized — no modal-on-modal.
    ComposeDm(ComposeDmState),
    /// Phase F: an owner just generated a short-lived join code for
    /// the current encrypted room. The modal shows it big so the
    /// owner can read it aloud / copy it / pass it OOB.
    ShowJoinCode(ShowJoinCodeState),
    /// Phase F: joiner enters a code shared by an owner of an
    /// encrypted room. On confirm, sends a signed CodeJoinRequest;
    /// the room opens read-only on the owner's response.
    JoinWithCode(JoinWithCodeState),
    /// Phase C: show a freshly-generated invite link (URL form),
    /// optionally room-scoped, so the user can copy + share.
    ShowInvite(ShowInviteState),
    /// Phase C: paste-an-invite text field.
    PasteInvite(PasteInviteState),
    /// Phase C: parsed invite — confirm before dialing.
    ConfirmInvite(ConfirmInviteState),
    /// Phase H + huddle 0.6: onboarding card. `pages` is a filtered
    /// index list into `ONBOARDING_PAGES` so a version bump can show
    /// only the new "what's new" page without re-walking the
    /// foundational cards.
    Onboarding {
        pages: Vec<usize>,
        cursor: usize,
    },
    /// huddle 0.6: scrollable list of the last `STATUS_HISTORY_CAP`
    /// status-bar messages. Opens on Ctrl+H. Doubles as a notification
    /// center.
    StatusHistory {
        scroll: u16,
    },
    /// huddle 0.6: command palette — fuzzy-search every action that
    /// has a `palette_label` in `crate::keybindings::BINDINGS`. Opens
    /// on Ctrl+P. Type to filter, Enter to execute.
    CommandPalette(CommandPaletteState),
    /// huddle 0.6: first-launch opt-in for the update check. Shown
    /// when `handle.update_check_enabled().is_none()`. Yes records
    /// the user opted in and triggers the first poll; No disables.
    UpdateCheckOptIn,
    /// huddle 0.7.7: pick known peers and auto-DM them an invite to
    /// the current group room. Tiered candidate list (Verified → DM
    /// partners → Known peers), multi-select with `/` filter and a
    /// soft-cap. `Shift+I` keeps the OOB link flow; this is the
    /// in-band picker.
    InvitePicker(InvitePickerState),
    QuitConfirm,
    /// huddle 0.7.11: confirmation before wiping the entire blocklist.
    /// Pre-0.7.11 the bare `c` keystroke on Settings → Privacy cleared
    /// the blocklist instantly — one keystroke from data loss, and the
    /// same `c` opened the join-code modal in the lobby so muscle
    /// memory was destructive. Now `c` opens this modal and the user
    /// must press Enter or `y` to actually clear.
    ConfirmClearBlocked,
    Help,
    Error(String),
    Info(String),
}

#[derive(Debug, Clone, Default)]
pub struct CommandPaletteState {
    pub query: String,
    /// Full list of (label, keys, action_id) for every palette-eligible
    /// binding plus a few synthetic entries (e.g. "toggle update check"
    /// that doesn't have a keybinding). Filtered by `query` at render.
    pub selected: usize,
}

#[derive(Debug, Clone, Default)]
pub struct EditUsernameState {
    pub input: String,
}

/// huddle 0.7.6: single-field Go Dark modal. Mode is fixed at open time:
/// if the user has a master passphrase, `requires_passphrase = true` and
/// the single field is the passphrase itself; otherwise the field is the
/// typed `DELETE EVERYTHING` confirmation (since there's no passphrase
/// to compare against in `--no-master-passphrase` sessions).
///
/// Replaced the two-field (passphrase + typed phrase) flow from 0.5: the
/// Tab-between-fields UX made it easy to fill only one and bounce off a
/// silent inline error, leaving "looks like nothing happened" reports.
#[derive(Debug, Clone, Default)]
pub struct GoDarkState {
    pub input: String,
    pub requires_passphrase: bool,
    /// Set after a wrong-passphrase / wrong-phrase attempt so the modal
    /// can flash an inline error without dismissing.
    pub last_error: Option<String>,
}

/// Confirmation phrase for `--no-master-passphrase` sessions (the only
/// gate they have, since there's no persisted key to compare against).
/// Sessions with a master passphrase use the passphrase itself.
pub const GO_DARK_CONFIRM_PHRASE: &str = "DELETE EVERYTHING";

#[derive(Debug, Clone, Default)]
pub struct AddFriendState {
    pub input: String,
}

/// huddle 0.7: Compose-DM modal state. Single-field input; autocomplete
/// surfaces inline (rendered from `known_peers` + `peer_profiles` cache).
#[derive(Debug, Clone, Default)]
pub struct ComposeDmState {
    pub input: String,
}

#[derive(Debug, Clone)]
pub struct ShowJoinCodeState {
    /// Room identifier so the modal can render a short hash next to
    /// the room name — useful when two rooms share a name (e.g.
    /// multiple "general"s discovered across networks).
    pub room_id: String,
    pub room_name: String,
    pub code: String,
}

#[derive(Debug, Clone)]
pub struct JoinWithCodeState {
    pub room_id: String,
    pub room_name: String,
    pub code: String,
}

#[derive(Debug, Clone)]
pub struct ShowInviteState {
    pub url: String,
    pub includes_room: Option<String>,
}

#[derive(Debug, Clone)]
pub struct PasteInviteState {
    pub url: String,
}

#[derive(Debug, Clone)]
pub struct ConfirmInviteState {
    pub invite: huddle_core::invite::InviteLink,
}

/// Phase G: stages of an in-flight SAS verification. The state advances
/// from `Waiting` (just started, no code yet) → `Comparing` (both sides
/// have the code, user is deciding) on `AppEvent::SasCodeReady`.
#[derive(Debug, Clone)]
pub enum SasStage {
    /// Initiator only — we sent SasInit, waiting for the partner's
    /// SasResponse to arrive so we can derive the code.
    Waiting,
    /// Both ephemeral pubkeys exchanged; the SAS code is shown for
    /// OOB comparison.
    Comparing {
        emoji_string: String,
        emoji_labels: String,
        decimal: String,
        /// True once we've broadcast our SasConfirm. Used to hide the
        /// "Match" button after pressing it (avoid double-fire).
        our_matched: bool,
    },
}

#[derive(Debug, Clone)]
pub struct SasState {
    /// Room the SAS exchange started in. Surfaced in the modal title
    /// ("SAS · #room-name") so the user knows which conversation
    /// they're verifying — important when multiple rooms are open.
    pub room_id: String,
    pub partner_fingerprint: String,
    pub tx_id: String,
    pub stage: SasStage,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum MemberActionKind {
    Kick,
    Grant,
}

#[derive(Debug, Clone)]
pub struct MemberActionState {
    pub room_id: String,
    pub kind: MemberActionKind,
    /// (fingerprint, is_already_owner). For Grant, owners are filtered
    /// out at open-time so the list only shows promotable members.
    pub members: Vec<(String, bool)>,
    pub selected: usize,
}

#[derive(Debug, Clone)]
pub struct InboundDialState {
    pub peer_id: PeerId,
    pub fingerprint: String,
    pub address: String,
    /// When the modal opened. Used to auto-reject after 15s — the
    /// network connection stays in our `pending_inbound` map all that
    /// time, so a user who never sees the modal won't accidentally
    /// keep an unknown peer attached forever.
    pub opened_at: Instant,
}

/// huddle 0.7.7: tier label for an `InviteCandidate`. Drives section
/// headers in the picker and sort order (Verified at the top, then DM
/// partners, then plain Known peers).
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum InviteTier {
    /// SAS-verified peer. Safest to in-band-DM an invite — we have
    /// strong, OOB-confirmed assurance this peer is who they claim.
    Verified,
    /// We already have a DM open with this peer. Implies prior trust
    /// (we initiated the DM, they accepted, or vice versa).
    DmPartner,
    /// A known dial peer with a learned fingerprint. Trust is weaker —
    /// we just have proof they own the Ed25519 key behind this address.
    Known,
}

#[derive(Debug, Clone)]
pub struct InviteCandidate {
    pub fingerprint: String,
    pub username: Option<String>,
    pub tier: InviteTier,
}

/// huddle 0.7.7: maximum number of peers selectable per send. Beyond
/// this we surface a hint and refuse to add more. Two-fold purpose:
/// (a) gently discourage spam-blasting, (b) keep the auto-send batch
/// snappy — every `start_direct` + `send_room_message` is a sequential
/// gossipsub publish.
pub const INVITE_PICKER_SOFT_CAP: usize = 20;

#[derive(Debug, Clone, Default)]
pub struct InvitePickerState {
    /// Room we're inviting *into*. Captured at open time so the
    /// generated invite stays consistent if the user switches panes
    /// while the modal is open (modals survive pane changes).
    pub room_id: String,
    pub room_name: String,
    /// All deduped candidates, pre-sorted by tier. Filter narrows the
    /// rendered slice but never mutates this list.
    pub candidates: Vec<InviteCandidate>,
    /// Fingerprints currently checked. Insertion-order isn't visible,
    /// but capped at `INVITE_PICKER_SOFT_CAP`.
    pub selected: HashSet<String>,
    /// Live filter input. Matches case-insensitively against username
    /// AND short HD-ID prefix.
    pub filter: String,
    /// Cursor in the *filtered* slice. Reset to 0 every time the
    /// filter changes.
    pub cursor: usize,
    /// Non-fatal user-feedback line shown above the hint bar. Cleared
    /// on the next keystroke. Used for "20-selection cap reached" and
    /// post-send status.
    pub status_line: Option<String>,
}

#[derive(Debug, Clone)]
pub struct RotateRoomState {
    pub room_id: String,
    pub passphrase: String,
}

#[derive(Debug, Clone)]
pub struct AcceptRotationState {
    pub room_id: String,
    pub rotator_fingerprint: String,
    pub new_salt: Vec<u8>,
    pub passphrase: String,
}

#[derive(Debug, Clone)]
pub struct SearchState {
    pub room_id: String,
    pub query: String,
    pub results: Vec<StoredRoomMessage>,
    pub selected: usize,
    pub searched: bool,
}

#[derive(Debug, Clone)]
pub struct VerifyState {
    pub room_id: String,
    pub our_fingerprint: String,
    /// (fingerprint, currently-verified)
    pub members: Vec<(String, bool)>,
    pub selected: usize,
}

#[derive(Debug, Clone)]
pub struct AttachEntry {
    pub name: String,
    pub is_dir: bool,
}

#[derive(Debug, Clone)]
pub struct AttachPickerState {
    pub cwd: std::path::PathBuf,
    pub entries: Vec<AttachEntry>,
    pub selected: usize,
    pub error: Option<String>,
}

impl AttachPickerState {
    pub fn new() -> Self {
        let start = dirs::download_dir()
            .or_else(dirs::home_dir)
            .unwrap_or_else(|| std::path::PathBuf::from("/"));
        let mut s = Self {
            cwd: start,
            entries: Vec::new(),
            selected: 0,
            error: None,
        };
        s.reload();
        s
    }

    pub fn reload(&mut self) {
        self.error = None;
        self.entries.clear();
        self.selected = 0;
        match std::fs::read_dir(&self.cwd) {
            Ok(rd) => {
                let mut tmp: Vec<AttachEntry> = Vec::new();
                for entry in rd.flatten() {
                    let name = entry.file_name().to_string_lossy().to_string();
                    if name.starts_with('.') {
                        continue;
                    }
                    let is_dir = entry.file_type().map(|t| t.is_dir()).unwrap_or(false);
                    tmp.push(AttachEntry { name, is_dir });
                }
                tmp.sort_by(|a, b| match (a.is_dir, b.is_dir) {
                    (true, false) => std::cmp::Ordering::Less,
                    (false, true) => std::cmp::Ordering::Greater,
                    _ => a.name.to_lowercase().cmp(&b.name.to_lowercase()),
                });
                self.entries = tmp;
            }
            Err(e) => {
                self.error = Some(format!("cannot read {}: {}", self.cwd.display(), e));
            }
        }
    }

    pub fn descend(&mut self) {
        if let Some(e) = self.entries.get(self.selected) {
            if e.is_dir {
                self.cwd.push(&e.name);
                self.reload();
            }
        }
    }

    pub fn ascend(&mut self) {
        if let Some(parent) = self.cwd.parent() {
            self.cwd = parent.to_path_buf();
            self.reload();
        }
    }

    pub fn selected_path(&self) -> Option<std::path::PathBuf> {
        let e = self.entries.get(self.selected)?;
        if e.is_dir {
            None
        } else {
            Some(self.cwd.join(&e.name))
        }
    }
}

#[derive(Debug, Clone, Default)]
pub struct DialPeerState {
    pub address: String,
    pub status: Option<String>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum StartField {
    Name,
    Encrypted,
    Passphrase,
}

#[derive(Debug, Clone)]
pub struct StartRoomState {
    pub name: String,
    pub encrypted: bool,
    pub passphrase: String,
    pub focus: StartField,
}

impl StartRoomState {
    pub fn new() -> Self {
        Self {
            name: String::new(),
            encrypted: false,
            passphrase: String::new(),
            focus: StartField::Name,
        }
    }
}

#[derive(Debug, Clone)]
pub struct JoinRoomState {
    pub room_id: String,
    pub room_name: String,
    pub encrypted: bool,
    pub passphrase: String,
}

/// Minimum gap between successive Typing broadcasts per room.
const TYPING_DEBOUNCE: Duration = Duration::from_millis(800);

/// A room we're currently in (a tab in the in-room view).
pub struct OpenRoom {
    pub room_id: String,
    pub name: String,
    pub encrypted: bool,
    pub members: Vec<String>,
    pub messages: Vec<StoredRoomMessage>,
    /// Attachments currently in this room, in chronological order.
    /// Refreshed from the AppHandle on render and on file events.
    pub attachments: Vec<StoredAttachment>,
    pub input: String,
    pub input_active: bool,
    /// Number of lines skipped from the top of the wrapped message
    /// buffer. Bounded by `last_max_scroll` at render time.
    pub scroll: u16,
    /// Last time we broadcast a Typing pulse for this room; used to
    /// debounce ChatTypeChar so we don't spam gossipsub.
    pub last_typing_sent: Option<Instant>,
    /// When true, render anchors to the bottom regardless of `scroll` —
    /// new messages stay visible. Any ScrollUp / PgUp / Home disables it.
    pub follow_mode: bool,
    /// Last-rendered maximum scroll value (total_lines − visible_height).
    /// Updated by `render_messages` so action handlers can clamp / detect
    /// "we just hit the bottom" without re-running the wrap.
    pub last_max_scroll: Cell<u16>,
    /// When true and input is blurred, j/k navigate file cards instead
    /// of scrolling. Enter activates the focused card.
    pub card_focus: bool,
    /// Index into the visible cards (filtered from `attachments`).
    pub focused_card_idx: usize,
    /// huddle 0.6: number of unread messages since the tab was last
    /// focused. Reset to 0 on tab activation. Replaces the old bool
    /// flag — gives users an "exact count" instead of a vague star.
    pub unread: u32,
}

// LobbyFocus removed in huddle 0.7: sidebar focus model replaces it.
// `SidebarState::focus` is the new source of truth.

/// huddle 0.6: a single entry in the status-history ring buffer.
/// `timestamp` is wall-clock seconds since UNIX epoch (so the
/// notification overlay renders absolute time, not "12s ago").
#[derive(Debug, Clone)]
pub struct StatusEntry {
    pub message: String,
    pub timestamp: i64,
}

pub struct TuiApp {
    pub handle: AppHandle,
    pub mode: NetworkMode,
    /// huddle 0.7: which pane the right side of the layout renders.
    /// Driven by sidebar selection + jump-shortcuts.
    pub pane: Pane,
    /// huddle 0.7: sidebar state (selection, expansion, focus). The
    /// pane router is read-only on this — sidebar owns its model.
    pub sidebar: SidebarState,
    /// huddle 0.7: centralized color/style palette. All renderers take
    /// a `&Theme`. Default = dark.
    pub theme: crate::ui::theme::Theme,
    /// huddle 0.7: unread counts keyed by room_id. Increments on
    /// `MessageReceived` for rooms that aren't the current pane;
    /// clears when that room becomes the active pane.
    pub unread: HashMap<String, u32>,
    /// huddle 0.7: toggle the right-margin member list in Group panes.
    /// Default on; Ctrl+I toggles.
    pub show_member_margin: bool,
    /// huddle 0.7: which People-pane sublist has focus (Known/Verified/
    /// Blocked). Tab cycles.
    pub people_focus: PeopleFocus,
    /// huddle 0.7: cursor inside the People pane's Known sublist.
    pub selected_known_idx: usize,
    /// huddle 0.7: cursor inside the People pane's Blocked sublist.
    pub selected_blocked_idx: usize,
    /// huddle 0.7.7: cursor inside the People pane's Pending sublist.
    pub selected_pending_idx: usize,
    /// huddle 0.7.7: cached snapshot of `pending_friend_requests` rows
    /// rendered in the People pane. Refreshed on People focus, after
    /// accept/reject, and after the 15s spill.
    pub pending_requests: Vec<huddle_core::storage::repo::PendingFriendRequest>,
    pub modal: Modal,
    /// huddle 0.6: a FIFO queue of async-event modals (errors, rotation
    /// requests, inbound dials) that arrived while another modal held
    /// the foreground. Replaces the single-slot Option<Modal> — events
    /// no longer get silently dropped past the second. Capped at
    /// `PENDING_MODAL_CAP`; oldest is shed on overflow.
    pub pending_modals: VecDeque<Modal>,
    pub discovered_rooms: Vec<DiscoveredRoom>,
    pub known_peers: Vec<KnownPeerStatus>,
    pub open_rooms: Vec<OpenRoom>,
    pub listen_addresses: Vec<String>,
    /// Bottom-bar status: text + expiry instant. After expiry, treated
    /// as None by the renderer.
    pub status_message: Option<(String, Instant)>,
    /// huddle 0.6: every status-bar message that's ever been displayed
    /// in this session, capped at `STATUS_HISTORY_CAP`. Opens on
    /// Ctrl+H. Replaces the "goldfish" status bar where two events in
    /// quick succession overwrote each other.
    pub status_history: VecDeque<StatusEntry>,
    /// huddle 0.6: scroll offset of the Help modal. Lives on TuiApp
    /// (not Modal) so the Modal enum stays simple.
    pub help_scroll: u16,
    /// huddle 0.6: latest crates.io poll result. `Some(version)`
    /// renders a banner under the lobby header; `None` hides it.
    /// Set by the spawned update-check task via `update_check_slot`.
    pub update_banner: Option<String>,
    /// huddle 0.6: shared mailbox between the spawned update-check
    /// task (writer) and the main loop (reader). The task writes a
    /// detected newer version here; the main loop drains it once
    /// per tick and copies into `update_banner`. Stays empty when
    /// the user hasn't opted in.
    pub update_check_slot: Arc<Mutex<Option<String>>>,
    /// Phase D follow-up: the lobby header renders this as a
    /// reachability badge. `None` until AutoNAT delivers its first
    /// transition; `Some("reachable")` once any external address
    /// passes a probe; `Some("private")` if a reachable address
    /// later disappears (all probes failing). The TUI maps this to
    /// '🌐 reachable' / '🏠 private' emoji in `lobby::render_status`.
    pub nat_status: Option<String>,
    /// huddle 0.5: set to `Instant::now()` when `AppEvent::WentDark`
    /// arrives. The main loop polls this and quits the process once
    /// `GO_DARK_FAREWELL` has elapsed so the goodbye modal stays
    /// visible for a beat.
    pub went_dark_at: Option<Instant>,
    /// huddle 0.7.4: count of inbound messages observed during the
    /// startup catch-up window. After `STARTUP_GRACE` elapses, the
    /// main loop emits ONE summary desktop notification and resets
    /// this counter to zero. After that, individual unfocused-window
    /// notifications fire one-per-message.
    pub startup_catchup_count: u32,
    /// huddle 0.7.4: when `Some`, the main loop is still inside the
    /// catch-up grace window; messages accumulate into
    /// `startup_catchup_count` instead of triggering per-message
    /// notifications. Cleared (set to `None`) the first tick after
    /// the deadline passes.
    pub startup_grace_until: Option<Instant>,
    /// huddle 0.7.5: absolute deadline beyond which we stop sliding
    /// the catch-up window forward. Prevents a sustained-traffic
    /// room from indefinitely suppressing live notifications.
    pub startup_grace_cap: Instant,
    /// huddle 0.7.8: which tab is active in the Settings pane.
    pub settings_tab: SettingsTab,
    /// huddle 0.7.8: which row is highlighted in the Profile pane for
    /// copy-to-clipboard. 0 = username, 1 = HD-ID, 2 = Safety Code,
    /// 3 = fingerprint, 4..N = listen addresses (clamped at render).
    pub profile_cursor: usize,
}

/// huddle 0.5: how long the goodbye modal stays on screen after
/// `WentDark` before the process exits.
pub const GO_DARK_FAREWELL: Duration = Duration::from_secs(2);

/// huddle 0.7.4: how long after startup we batch inbound messages into
/// a single "N new messages while you were away" notification instead
/// of firing per-message notifications. 5s comfortably covers libp2p
/// dial + gossipsub catch-up on a healthy LAN; longer would risk
/// missing live messages.
pub const STARTUP_GRACE: Duration = Duration::from_secs(5);

/// huddle 0.7.5: each MessageReceived during the grace window pushes
/// the deadline forward by this much, so a slow catch-up (large
/// backlog or sluggish gossipsub) still batches correctly instead of
/// firing per-message notifications. Bounded by `STARTUP_GRACE_MAX`.
pub const STARTUP_GRACE_EXTEND: Duration = Duration::from_secs(2);

/// huddle 0.7.5: absolute cap on the catch-up window from
/// `started_at`. Once we cross this, the grace ends and any further
/// inbound messages route through the live notification path —
/// otherwise a sustained-traffic room could indefinitely silence
/// per-message alerts.
pub const STARTUP_GRACE_MAX: Duration = Duration::from_secs(30);

impl TuiApp {
    pub fn new(handle: AppHandle) -> Self {
        let mode = handle.mode();
        let known_peers = handle.known_peers();
        // huddle 0.7.7: capture the pending-request snapshot before
        // `handle` is moved into Self so we don't pay a clone.
        let pending_requests = handle.list_pending_friend_requests();
        // huddle 0.6: onboarding-pages-to-show is now version-driven.
        // First-launch users see every page; upgrading users see only
        // the "what's new in X.Y" page for releases newer than their
        // last_seen_onboarding_version.
        let last_seen = handle.last_seen_onboarding_version();
        let legacy_seen = handle.onboarding_seen();
        let pages = pages_to_show(last_seen.as_deref(), legacy_seen);
        let mut pending_modals: VecDeque<Modal> = VecDeque::new();
        if !pages.is_empty() {
            pending_modals.push_back(Modal::Onboarding { pages, cursor: 0 });
        }
        // huddle 0.6: ask first-launch users to opt in to the update
        // check. If they've already answered (Some(true) or Some(false))
        // we skip the modal. The prompt sits behind onboarding so new
        // users see the welcome card first.
        if handle.update_check_enabled().is_none() && legacy_seen {
            pending_modals.push_back(Modal::UpdateCheckOptIn);
        }
        Self {
            handle,
            mode,
            pane: Pane::Welcome,
            sidebar: SidebarState::default(),
            theme: crate::ui::theme::Theme::dark(),
            unread: HashMap::new(),
            show_member_margin: true,
            people_focus: PeopleFocus::default(),
            selected_known_idx: 0,
            selected_blocked_idx: 0,
            selected_pending_idx: 0,
            pending_requests,
            modal: Modal::None,
            pending_modals,
            discovered_rooms: Vec::new(),
            known_peers,
            open_rooms: Vec::new(),
            listen_addresses: Vec::new(),
            status_message: None,
            status_history: VecDeque::new(),
            help_scroll: 0,
            update_banner: None,
            update_check_slot: Arc::new(Mutex::new(None)),
            nat_status: None,
            went_dark_at: None,
            startup_catchup_count: 0,
            startup_grace_until: Some(Instant::now() + STARTUP_GRACE),
            startup_grace_cap: Instant::now() + STARTUP_GRACE_MAX,
            settings_tab: SettingsTab::default(),
            profile_cursor: 0,
        }
    }

    /// huddle 0.7: lookup an `OpenRoom` by its `room_id`. Replaces the
    /// old `active_room()` access pattern which keyed by `active_tab`.
    /// The Vec storage is kept for now; this method abstracts the
    /// lookup so pane/sidebar code doesn't depend on the storage shape.
    pub fn open_room(&self, room_id: &str) -> Option<&OpenRoom> {
        self.open_rooms.iter().find(|r| r.room_id == room_id)
    }

    pub fn open_room_mut(&mut self, room_id: &str) -> Option<&mut OpenRoom> {
        self.open_rooms.iter_mut().find(|r| r.room_id == room_id)
    }

    /// huddle 0.7: returns the room_id of the currently-active chat
    /// pane (`Pane::Dm(id) | Pane::Group(id)`); `None` for non-chat
    /// panes.
    pub fn current_pane_room_id(&self) -> Option<&str> {
        match &self.pane {
            Pane::Dm(id) | Pane::Group(id) => Some(id.as_str()),
            _ => None,
        }
    }

    pub fn mode_str(&self) -> &'static str {
        match self.mode {
            NetworkMode::Mdns => "LAN (mDNS)",
            NetworkMode::Direct => "Direct (manual dial)",
        }
    }

    /// huddle 0.7: clear unread for a room. Called when that room
    /// becomes the active pane. (huddle 0.7.11: removed unused
    /// `unread_count` accessor — `app.unread.get(...)` is used
    /// directly at every call site.)
    pub fn clear_unread(&mut self, room_id: &str) {
        self.unread.remove(room_id);
    }

    /// huddle 0.7: switch the active pane to a given room id. Routes
    /// `Direct` rooms to `Pane::Dm`, anything else to `Pane::Group`.
    /// Also clears the unread counter for that room.
    pub fn switch_to_room(&mut self, room_id: &str) {
        let kind = self
            .handle
            .active_room_info(room_id)
            .map(|r| r.kind)
            .or_else(|| {
                self.handle
                    .discovered_rooms()
                    .into_iter()
                    .find(|d| d.room_id == room_id)
                    .map(|d| d.kind)
            })
            .unwrap_or(huddle_core::storage::repo::RoomKind::Group);
        self.pane = match kind {
            huddle_core::storage::repo::RoomKind::Direct => Pane::Dm(room_id.to_string()),
            huddle_core::storage::repo::RoomKind::Group => Pane::Group(room_id.to_string()),
        };
        self.clear_unread(room_id);
        self.sidebar.focus = SidebarFocus::Pane;
    }

    pub fn refresh_known_peers(&mut self) {
        self.known_peers = self.handle.known_peers();
        if self.selected_known_idx >= self.known_peers.len() && !self.known_peers.is_empty() {
            self.selected_known_idx = self.known_peers.len() - 1;
        }
    }

    /// huddle 0.7.7: re-snapshot the pending-requests list from disk.
    /// Called on People focus, after accept/reject, and after the 15s
    /// spill. Re-clamps the cursor so it doesn't dangle past the last
    /// row when the list shrinks.
    pub fn refresh_pending_requests(&mut self) {
        self.pending_requests = self.handle.list_pending_friend_requests();
        if self.selected_pending_idx >= self.pending_requests.len()
            && !self.pending_requests.is_empty()
        {
            self.selected_pending_idx = self.pending_requests.len() - 1;
        }
    }

    /// Set the bottom status line with the default 6s TTL. Also
    /// appends to the status history ring buffer (huddle 0.6) so the
    /// Ctrl+H overlay can show a backlog even when events fire faster
    /// than the TTL.
    pub fn set_status(&mut self, msg: impl Into<String>) {
        let msg = msg.into();
        self.record_status(&msg);
        self.status_message = Some((msg, Instant::now() + STATUS_TTL));
    }

    /// Set the status line with an explicit TTL. Used for transient
    /// notifications worth a longer dwell than the default 6 s — e.g.
    /// DCUtR upgrade success ("direct connection to <peer>") which we
    /// want the user to actually see before it scrolls.
    pub fn set_status_for(&mut self, msg: impl Into<String>, ttl: Duration) {
        let msg = msg.into();
        self.record_status(&msg);
        self.status_message = Some((msg, Instant::now() + ttl));
    }

    /// huddle 0.6: append to the status history ring buffer. Dedupes
    /// adjacent duplicates so a re-render of the same message (which
    /// happens because tick_status calls set_status indirectly via
    /// app events) doesn't fill the buffer with the same line.
    fn record_status(&mut self, msg: &str) {
        if let Some(last) = self.status_history.back() {
            if last.message == msg {
                return;
            }
        }
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .map(|d| d.as_secs() as i64)
            .unwrap_or(0);
        self.status_history.push_back(StatusEntry {
            message: msg.to_string(),
            timestamp: now,
        });
        while self.status_history.len() > STATUS_HISTORY_CAP {
            self.status_history.pop_front();
        }
    }

    /// Returns the current status text if it hasn't expired.
    pub fn current_status(&self) -> Option<&str> {
        self.status_message.as_ref().and_then(|(msg, exp)| {
            if *exp > Instant::now() {
                Some(msg.as_str())
            } else {
                None
            }
        })
    }

    /// Drop the stored status if it's already past its expiry. Cheap; safe
    /// to call every tick.
    pub fn tick_status(&mut self) {
        if let Some((_, exp)) = &self.status_message {
            if *exp <= Instant::now() {
                self.status_message = None;
            }
        }
    }

    pub fn active_room(&self) -> Option<&OpenRoom> {
        let id = self.current_pane_room_id()?;
        self.open_room(id)
    }

    pub fn active_room_mut(&mut self) -> Option<&mut OpenRoom> {
        let id = self.current_pane_room_id()?.to_string();
        self.open_room_mut(&id)
    }

    pub fn refresh_discovered(&mut self) {
        self.discovered_rooms = self.handle.discovered_rooms();
    }

    /// Refresh attachments for every open room from the AppHandle. Called
    /// on tick so card state stays in sync with chunks arriving.
    pub fn refresh_attachments(&mut self) {
        let handle = self.handle.clone();
        for room in &mut self.open_rooms {
            room.attachments = handle.list_room_attachments(&room.room_id).unwrap_or_default();
            if room.attachments.is_empty() {
                room.focused_card_idx = 0;
                room.card_focus = false;
            } else if room.focused_card_idx >= room.attachments.len() {
                room.focused_card_idx = room.attachments.len() - 1;
            }
        }
    }

    /// Show `m` now if the user isn't mid-interaction with a modal,
    /// otherwise enqueue it. Dismissible modals (None / Error / Info)
    /// are pure output and safe to displace; input modals hold
    /// unsaved user state and must not be clobbered by an async event.
    ///
    /// huddle 0.6: the queue is now a `VecDeque` (was a single Option)
    /// — concurrent inbound dials / errors no longer drop the second.
    /// Bounded at `PENDING_MODAL_CAP`; oldest shed on overflow so a
    /// runaway error storm can't grow without bound.
    fn replace_modal_if_idle(&mut self, m: Modal) {
        if matches!(self.modal, Modal::None | Modal::Error(_) | Modal::Info(_)) {
            self.modal = m;
        } else {
            self.enqueue_modal(m);
        }
    }

    /// huddle 0.6: append a modal to the pending queue with overflow
    /// protection. Caller usually goes through `replace_modal_if_idle`.
    pub fn enqueue_modal(&mut self, m: Modal) {
        self.pending_modals.push_back(m);
        while self.pending_modals.len() > PENDING_MODAL_CAP {
            self.pending_modals.pop_front();
        }
    }

    // huddle 0.7.11: removed `pending_count` — the modal-queue badge
    // ended up not shipping in 0.6 and no caller uses the accessor.
    // `pending_modals` is still tracked internally.

    pub fn handle_app_event(&mut self, ev: AppEvent) {
        match ev {
            AppEvent::RoomDiscovered(_) | AppEvent::RoomLost { .. } => {
                self.refresh_discovered();
            }
            AppEvent::RoomJoined { room_id } => {
                let info = self.handle.active_room_info(&room_id);
                let members = self.handle.room_members(&room_id);
                let messages = self.handle.room_messages(&room_id, 200).unwrap_or_default();
                let already_open = self.open_rooms.iter().any(|r| r.room_id == room_id);
                if !already_open {
                    if let Some(info) = info {
                        let attachments = self
                            .handle
                            .list_room_attachments(&room_id)
                            .unwrap_or_default();
                        self.open_rooms.push(OpenRoom {
                            room_id: room_id.clone(),
                            name: info.name,
                            encrypted: info.encrypted,
                            members,
                            messages,
                            attachments,
                            input: String::new(),
                            input_active: false,
                            last_typing_sent: None,
                            scroll: 0,
                            follow_mode: true,
                            last_max_scroll: Cell::new(0),
                            card_focus: false,
                            focused_card_idx: 0,
                            unread: 0,
                        });
                    }
                }
                // huddle 0.7 focus-steal policy: auto-switch the pane
                // to the freshly-joined room only if the user is on
                // Welcome / Profile (a "soft" pane). Don't steal focus
                // away from another active chat — surface the new
                // room in the sidebar (it'll appear via discovered_rooms)
                // and let the user pick it.
                let soft_pane = matches!(self.pane, Pane::Welcome | Pane::Profile);
                if soft_pane {
                    self.switch_to_room(&room_id);
                }
            }
            AppEvent::RoomLeft { room_id } => {
                if let Some(idx) = self.open_rooms.iter().position(|r| r.room_id == room_id) {
                    self.open_rooms.remove(idx);
                    if self.current_pane_room_id() == Some(room_id.as_str()) {
                        self.pane = Pane::Welcome;
                    }
                    self.unread.remove(&room_id);
                }
            }
            AppEvent::MemberJoined { room_id, fingerprint } => {
                if let Some(r) = self.open_rooms.iter_mut().find(|r| r.room_id == room_id) {
                    if !r.members.contains(&fingerprint) {
                        r.members.push(fingerprint);
                        r.members.sort();
                    }
                }
            }
            AppEvent::MemberLeft { room_id, fingerprint } => {
                if let Some(r) = self.open_rooms.iter_mut().find(|r| r.room_id == room_id) {
                    r.members.retain(|f| f != &fingerprint);
                }
            }
            AppEvent::MessageReceived {
                room_id,
                sender_fingerprint,
                body,
                sent_at,
            } => {
                let is_active = self.current_pane_room_id() == Some(room_id.as_str());
                // Snapshot fields needed for the notification before
                // we move body/sender into the stored message.
                let sender_for_notify = sender_fingerprint.clone();
                let body_for_notify = body.clone();
                if let Some(r) = self.open_room_mut(&room_id) {
                    r.messages.push(StoredRoomMessage {
                        id: 0,
                        room_id: room_id.clone(),
                        sender_fingerprint,
                        direction: "in".into(),
                        body,
                        sent_at,
                    });
                }
                if !is_active {
                    let count = self.unread.entry(room_id.clone()).or_insert(0);
                    *count = count.saturating_add(1);
                }
                // huddle 0.7.4: desktop notification routing.
                // * during startup grace → silent batch; the main loop
                //   drains it into one summary notification.
                // * after grace, fire per-message *only* when the
                //   terminal isn't focused. A focused terminal already
                //   shows the message; an unread badge is enough.
                if self.startup_grace_until.is_some() {
                    self.startup_catchup_count =
                        self.startup_catchup_count.saturating_add(1);
                    // huddle 0.7.5: extend the grace deadline so a
                    // slow gossipsub backlog still batches into the
                    // single summary notification instead of leaking
                    // into per-message alerts. Capped by
                    // `startup_grace_cap` so a hot room can't keep
                    // the grace open indefinitely.
                    let extended = Instant::now() + STARTUP_GRACE_EXTEND;
                    let new_deadline = extended.min(self.startup_grace_cap);
                    self.startup_grace_until = self
                        .startup_grace_until
                        .map(|d| d.max(new_deadline));
                } else if !crate::notifier::is_focused()
                    && self.handle.notifications_enabled()
                {
                    let room_name = self
                        .open_room(&room_id)
                        .map(|r| r.name.clone())
                        .or_else(|| {
                            self.handle.active_room_info(&room_id).map(|r| r.name)
                        })
                        .unwrap_or_else(|| short_room(&room_id));
                    let sender_name = self
                        .handle
                        .lookup_member_display_name(&sender_for_notify)
                        .unwrap_or_else(|| short_fp(&sender_for_notify));
                    let title = format!("huddle · {}", room_name);
                    let body = format!(
                        "{}: {}",
                        sender_name,
                        crate::notifier::preview(&body_for_notify)
                    );
                    crate::notifier::notify(&title, &body);
                }
            }
            AppEvent::MessageSent {
                room_id,
                body,
                message_id,
            } => {
                if let Some(r) = self.open_rooms.iter_mut().find(|r| r.room_id == room_id) {
                    let now = std::time::SystemTime::now()
                        .duration_since(std::time::UNIX_EPOCH)
                        .unwrap()
                        .as_secs() as i64;
                    r.messages.push(StoredRoomMessage {
                        id: message_id,
                        room_id: room_id.clone(),
                        sender_fingerprint: self.handle.fingerprint().to_string(),
                        direction: "out".into(),
                        body,
                        sent_at: now,
                    });
                }
            }
            AppEvent::ListeningOn { address } => {
                if !self.listen_addresses.contains(&address) {
                    self.listen_addresses.push(address);
                }
            }
            AppEvent::PeerDiscovered { .. } => {}
            AppEvent::PeerExpired { .. } => {
                self.refresh_known_peers();
            }
            AppEvent::Dialing { address } => {
                self.set_status(format!("dialing {}", address));
                if let Modal::DialPeer(s) = &mut self.modal {
                    s.status = Some(format!("dialing {}", address));
                }
            }
            AppEvent::DialSucceeded { address, .. } => {
                self.set_status(format!("connected to {}", address));
                if matches!(self.modal, Modal::DialPeer(_)) {
                    self.modal = Modal::None;
                }
                self.refresh_known_peers();
            }
            AppEvent::DialFailed { address, error } => {
                let msg = format!("dial {} failed: {}", address, error);
                self.set_status(msg.clone());
                if matches!(self.modal, Modal::DialPeer(_)) {
                    self.modal = Modal::Error(msg);
                }
                self.refresh_known_peers();
            }
            AppEvent::Error { description } => {
                self.replace_modal_if_idle(Modal::Error(description));
            }
            AppEvent::FileOffered {
                room_id,
                file_id: _,
                name,
                size_bytes,
                sender_fingerprint: _,
            } => {
                let on_active = self.current_pane_room_id() == Some(room_id.as_str());
                if !on_active {
                    let count = self.unread.entry(room_id.clone()).or_insert(0);
                    *count = count.saturating_add(1);
                }
                let _ = room_id;
                self.set_status(format!(
                    "file offered: {} ({} KB)",
                    name,
                    size_bytes / 1024
                ));
            }
            AppEvent::FileProgress { .. } => {
                // Progress is read on render from the attachments list;
                // no state change here.
            }
            AppEvent::FileReady { file_id: _ } => {
                self.set_status("file ready — press Enter to save");
            }
            AppEvent::FileSaved { file_id: _, path } => {
                self.set_status(format!("saved to {}", path));
            }
            AppEvent::FileFailed { file_id: _, reason } => {
                self.set_status(format!("transfer failed: {}", reason));
            }
            AppEvent::TypingChanged { .. } => {
                // The UI re-reads typers per-frame via handle.typers_in_room;
                // nothing else to do here.
            }
            AppEvent::MentionReceived { room_id, body } => {
                // BEL (0x07): most terminals beep or flash on this.
                use std::io::Write;
                let _ = write!(std::io::stdout(), "\x07");
                let _ = std::io::stdout().flush();
                self.set_status(format!("@you mentioned in #{}", short_room(&room_id)));
                let _ = body;
            }
            AppEvent::RotationRequested {
                room_id,
                rotator_fingerprint,
                new_salt,
            } => {
                self.replace_modal_if_idle(Modal::AcceptRotation(AcceptRotationState {
                    room_id,
                    rotator_fingerprint,
                    new_salt,
                    passphrase: String::new(),
                }));
            }
            AppEvent::InboundDial {
                peer_id,
                fingerprint,
                address,
            } => {
                self.replace_modal_if_idle(Modal::InboundDial(InboundDialState {
                    peer_id,
                    fingerprint,
                    address,
                    opened_at: Instant::now(),
                }));
            }
            AppEvent::SasCodeReady {
                room_id,
                partner_fingerprint,
                tx_id,
                emoji_string,
                emoji_labels,
                decimal,
            } => {
                // If our SAS modal already targets this tx_id, advance
                // it to Comparing. Otherwise this is a fresh inbound
                // SAS request — surface a new modal.
                let advanced = if let Modal::Sas(s) = &mut self.modal {
                    if s.tx_id == tx_id {
                        s.stage = SasStage::Comparing {
                            emoji_string: emoji_string.clone(),
                            emoji_labels: emoji_labels.clone(),
                            decimal: decimal.clone(),
                            our_matched: false,
                        };
                        true
                    } else {
                        false
                    }
                } else {
                    false
                };
                if !advanced {
                    self.replace_modal_if_idle(Modal::Sas(SasState {
                        room_id,
                        partner_fingerprint,
                        tx_id,
                        stage: SasStage::Comparing {
                            emoji_string,
                            emoji_labels,
                            decimal,
                            our_matched: false,
                        },
                    }));
                }
            }
            AppEvent::SasVerified {
                partner_fingerprint,
                ..
            } => {
                if matches!(self.modal, Modal::Sas(_)) {
                    self.modal = Modal::None;
                }
                self.set_status(format!(
                    "✓ verified {} via SAS",
                    short_fp(&partner_fingerprint)
                ));
            }
            AppEvent::CodeJoinTimedOut { room_id: _, reason } => {
                // The placeholder tab from `join_room_with_code` stays
                // open (the user might retry) — we just surface the
                // failure via the modal queue so it doesn't clobber
                // anything the user happens to be typing.
                self.replace_modal_if_idle(Modal::Error(format!("code join: {reason}")));
            }
            AppEvent::InviteFingerprintMismatch {
                address: _,
                claimed,
                actual,
            } => {
                // The connection has already been dropped by the app
                // handle; we just inform the user via the modal queue.
                let msg = format!(
                    "invite fingerprint mismatch — connection dropped.\nclaimed: {}\nactual:  {}\nthe invite link may be forged.",
                    short_fp(&claimed),
                    short_fp(&actual)
                );
                self.replace_modal_if_idle(Modal::Error(msg));
            }
            AppEvent::NatStatusChanged { label, reachable: _ } => {
                // The lobby renders this as an emoji badge via
                // `nat_status_badge()`; we just stash the raw label.
                self.nat_status = Some(label);
            }
            AppEvent::DcutrSucceeded { peer_label } => {
                self.set_status_for(
                    format!("direct connection to …{}", peer_label),
                    Duration::from_secs(10),
                );
            }
            AppEvent::PeerProfileUpdated { fingerprint, username } => {
                // huddle 0.5: a peer set / changed / cleared their
                // username. The chat + member list pull from the DB
                // every render, so a redraw is enough — but show a
                // transient hint so the user notices the rename live.
                let new_label = match &username {
                    Some(n) if !n.is_empty() => n.clone(),
                    _ => "[anonymous]".into(),
                };
                let short: String = fingerprint.chars().take(4).collect();
                self.set_status_for(
                    format!("{}… is now {}", short, new_label),
                    Duration::from_secs(4),
                );
            }
            AppEvent::WentDark => {
                // huddle 0.5: go_dark wiped everything. Show the final
                // farewell, then schedule a quit. The status TTL also
                // serves as the visibility window before exit.
                self.modal = Modal::Info(
                    "Goodbye. huddle has gone dark. Restart to begin fresh.".into(),
                );
                self.went_dark_at = Some(std::time::Instant::now());
            }
            AppEvent::AutoOpenDm {
                room_id,
                fingerprint,
            } => {
                // huddle 0.7.7: a user-initiated dial connected and
                // Identify landed. Switch into the DM pane so the user
                // can immediately chat without manually navigating.
                // Username (if cached) goes in the status hint so a
                // first-time peer with no profile still gets a clear
                // breadcrumb.
                let label = self
                    .handle
                    .lookup_username(&fingerprint)
                    .unwrap_or_else(|| format!("HD-{}", short_fp(&fingerprint).to_uppercase()));
                self.refresh_known_peers();
                self.switch_to_room(&room_id);
                self.set_status(format!("connected — chatting with {}", label));
            }
        }
    }

}

fn short_room(room_id: &str) -> String {
    room_id.chars().take(8).collect()
}

/// First chunk of a fingerprint for compact display — the first 4-4
/// groups of the `xxxx-xxxx-xxxx-...` form, which gives ~32 bits of
/// collision resistance, plenty for a status line.
fn short_fp(fp: &str) -> String {
    fp.split('-').take(2).collect::<Vec<_>>().join("-")
}

/// Phase B: gather a member list for the kick / grant picker. Filters:
/// - excludes ourselves (you can't kick yourself, granting yourself is
///   a no-op since `start_room` already made you an owner)
/// - for Grant, hides current owners so the picker only shows
///   promotable members
/// - returns None if we aren't an owner of the active room (gates the
///   feature in one place)
fn owner_action_members(
    app: &TuiApp,
    kind: MemberActionKind,
) -> Option<(String, Vec<(String, bool)>)> {
    let room_id = app.active_room()?.room_id.clone();
    let our_fp = app.handle.fingerprint().to_string();
    if !app.handle.is_owner(&room_id, &our_fp) {
        // Status message is set by the caller's wrapper if we'd like;
        // returning None here just makes the keybinding a no-op for
        // non-owners, which is the intended UX.
        return None;
    }
    let owners: std::collections::HashSet<String> =
        app.handle.room_owners(&room_id).into_iter().collect();
    let members: Vec<(String, bool)> = app
        .active_room()?
        .members
        .iter()
        .filter(|fp| *fp != &our_fp)
        .filter(|fp| match kind {
            MemberActionKind::Kick => true,
            // Already an owner → nothing to grant
            MemberActionKind::Grant => !owners.contains(*fp),
        })
        .map(|fp| (fp.clone(), owners.contains(fp)))
        .collect();
    if members.is_empty() {
        return None;
    }
    Some((room_id, members))
}

/// huddle 0.7: move the sidebar selection up (`delta < 0`) or down
/// (`delta > 0`). Wraps within the ordered item list. Section headers
/// are skipped over when the move would land on one and the original
/// selection wasn't a section header — keeps j/k navigation snappy.
fn sidebar_move(app: &mut TuiApp, delta: i32) {
    let items = crate::ui::sidebar::ordered_items(app);
    if items.is_empty() {
        return;
    }
    let mut idx = items
        .iter()
        .position(|it| *it == app.sidebar.selection)
        .unwrap_or(0) as i32;
    idx += delta;
    if idx < 0 {
        idx = 0;
    }
    if idx as usize >= items.len() {
        idx = items.len() as i32 - 1;
    }
    app.sidebar.selection = items[idx as usize].clone();
    sync_pane_from_selection(app);
}

/// huddle 0.7: jump to the next/prev sidebar section (`delta = ±1`).
/// Used by Tab / Shift+Tab. Lands on the section header (which the user
/// can then j/k into).
fn sidebar_jump_section(app: &mut TuiApp, delta: i32) {
    use SidebarSection::*;
    let order = [Profile, Direct, Group, People, Activity, Settings];
    let current = match &app.sidebar.selection {
        SidebarItem::Section(s) => *s,
        SidebarItem::Profile => Profile,
        SidebarItem::Dm(_) | SidebarItem::DirectAddFriend => Direct,
        SidebarItem::Group(_) | SidebarItem::GroupDiscover | SidebarItem::GroupNew => Group,
        SidebarItem::Person(_) | SidebarItem::PeoplePendingBadge => People,
        SidebarItem::Activity => Activity,
        SidebarItem::Settings => Settings,
    };
    let cur_idx = order.iter().position(|s| *s == current).unwrap_or(0) as i32;
    let mut next = cur_idx + delta;
    if next < 0 {
        next = order.len() as i32 - 1;
    } else if next as usize >= order.len() {
        next = 0;
    }
    app.sidebar.selection = SidebarItem::Section(order[next as usize]);
}

/// huddle 0.7: expand / collapse the currently-selected section.
fn sidebar_toggle_expand(app: &mut TuiApp) {
    let section = match &app.sidebar.selection {
        SidebarItem::Section(s) => *s,
        SidebarItem::Profile => SidebarSection::Profile,
        SidebarItem::Dm(_) | SidebarItem::DirectAddFriend => SidebarSection::Direct,
        SidebarItem::Group(_) | SidebarItem::GroupDiscover | SidebarItem::GroupNew => {
            SidebarSection::Group
        }
        SidebarItem::Person(_) | SidebarItem::PeoplePendingBadge => SidebarSection::People,
        SidebarItem::Activity => SidebarSection::Activity,
        SidebarItem::Settings => SidebarSection::Settings,
    };
    if app.sidebar.expanded.contains(&section) {
        app.sidebar.expanded.remove(&section);
    } else {
        app.sidebar.expanded.insert(section);
    }
}

/// huddle 0.7: side-effect of moving the sidebar — if the selection is
/// addressable (DM / Group / People / etc.), switch the pane to match
/// so live preview is always available as the cursor moves. The dual
/// "Enter to commit" model is unnecessary in a TUI with no mouse.
fn sync_pane_from_selection(app: &mut TuiApp) {
    if let Some(pane) = crate::ui::sidebar::pane_for_item(&app.sidebar.selection) {
        match &pane {
            Pane::Dm(id) | Pane::Group(id) => {
                let id = id.clone();
                app.clear_unread(&id);
                if app.handle.active_room_info(&id).is_some() {
                    open_existing_room_tab_quiet(app, &id);
                }
                app.pane = pane;
            }
            _ => app.pane = pane,
        }
    }
}

/// huddle 0.7.8: the labeled identity rows shown in Profile pane, in
/// fixed visual order. Tuple is (label-for-status-message, value-to-
/// copy). Listen addresses are spread across rows so each address is
/// individually yankable. Order MUST match `pane/profile.rs` rendering
/// so `profile_cursor` indices into the right thing.
pub fn profile_fields(app: &TuiApp) -> Vec<(String, String)> {
    let mut out: Vec<(String, String)> = Vec::new();
    let username = app
        .handle
        .display_name()
        .unwrap_or_else(|| "[anonymous]".into());
    out.push(("username".into(), username));
    let hd = crate::ui::display_id(app.handle.fingerprint());
    out.push(("HD-ID".into(), hd));
    out.push(("Safety Code".into(), app.handle.safety_code()));
    out.push(("fingerprint".into(), app.handle.fingerprint().to_string()));
    for (i, addr) in app.listen_addresses.iter().take(6).enumerate() {
        out.push((format!("listen address {}", i + 1), addr.clone()));
    }
    out
}

pub fn profile_field_count(app: &TuiApp) -> usize {
    profile_fields(app).len()
}

pub fn profile_field_at(app: &TuiApp, idx: usize) -> Option<(String, String)> {
    profile_fields(app).into_iter().nth(idx)
}

/// huddle 0.7: list of room ids in sidebar order (DMs first, then
/// groups). Used by `switch_chat_relative` and `switch_chat_absolute`.
fn chat_room_ids(app: &TuiApp) -> Vec<String> {
    let discovered = app.handle.discovered_rooms();
    let mut dms: Vec<_> = discovered
        .iter()
        .filter(|r| r.kind == huddle_core::storage::repo::RoomKind::Direct)
        .map(|r| r.room_id.clone())
        .collect();
    let mut groups: Vec<_> = discovered
        .iter()
        .filter(|r| r.kind != huddle_core::storage::repo::RoomKind::Direct)
        .map(|r| r.room_id.clone())
        .collect();
    dms.append(&mut groups);
    dms
}

/// huddle 0.7: switch to the next / previous chat (DM or Group) in
/// sidebar order, wrapping at the ends.
fn switch_chat_relative(app: &mut TuiApp, delta: i32) {
    let chats = chat_room_ids(app);
    if chats.is_empty() {
        return;
    }
    let current = app
        .current_pane_room_id()
        .and_then(|id| chats.iter().position(|c| c == id));
    let next = match current {
        Some(i) => {
            let mut n = i as i32 + delta;
            if n < 0 {
                n = chats.len() as i32 - 1;
            }
            n as usize % chats.len()
        }
        None => 0,
    };
    let id = chats[next].clone();
    app.switch_to_room(&id);
}

/// huddle 0.7: jump to the N-th chat (zero-based) in sidebar order.
fn switch_chat_absolute(app: &mut TuiApp, n: usize) {
    let chats = chat_room_ids(app);
    if let Some(id) = chats.get(n).cloned() {
        app.switch_to_room(&id);
    }
}

/// huddle 0.7: hydrate OpenRoom if missing, without changing pane or
/// stealing focus. Used by `sync_pane_from_selection` to ensure the
/// chat pane has data on first sight.
fn open_existing_room_tab_quiet(app: &mut TuiApp, room_id: &str) {
    if app.open_room(room_id).is_some() {
        return;
    }
    let info = match app.handle.active_room_info(room_id) {
        Some(i) => i,
        None => return,
    };
    let members = app.handle.room_members(room_id);
    let messages = app.handle.room_messages(room_id, 200).unwrap_or_default();
    let attachments = app.handle.list_room_attachments(room_id).unwrap_or_default();
    app.open_rooms.push(OpenRoom {
        room_id: room_id.to_string(),
        name: info.name,
        encrypted: info.encrypted,
        members,
        messages,
        attachments,
        input: String::new(),
        input_active: false,
        last_typing_sent: None,
        scroll: 0,
        follow_mode: true,
        last_max_scroll: Cell::new(0),
        card_focus: false,
        focused_card_idx: 0,
        unread: 0,
    });
}

/// Scroll the active room by `delta` lines (negative = up). Maintains
/// `follow_mode` semantics: scrolling up disables it, reaching the bottom
/// enables it.
fn scroll_by(app: &mut TuiApp, delta: i32) {
    let r = match app.active_room_mut() {
        Some(r) => r,
        None => return,
    };
    let max = r.last_max_scroll.get();
    let current = if r.follow_mode { max } else { r.scroll };
    let next = if delta < 0 {
        current.saturating_sub(delta.unsigned_abs() as u16)
    } else {
        current.saturating_add(delta as u16).min(max)
    };
    r.scroll = next;
    r.follow_mode = next >= max;
}

/// huddle 0.7: hydrate an OpenRoom for `room_id` if we're subscribed but
/// don't have it open yet, then switch the active pane to it. Mirrors
/// what the `AppEvent::RoomJoined` handler does, minus the actual join
/// call.
fn open_existing_room_tab(app: &mut TuiApp, room_id: &str) {
    let info = match app.handle.active_room_info(room_id) {
        Some(i) => i,
        None => return,
    };
    if app.open_room(room_id).is_none() {
        let members = app.handle.room_members(room_id);
        let messages = app.handle.room_messages(room_id, 200).unwrap_or_default();
        let attachments = app.handle.list_room_attachments(room_id).unwrap_or_default();
        app.open_rooms.push(OpenRoom {
            room_id: room_id.to_string(),
            name: info.name,
            encrypted: info.encrypted,
            members,
            messages,
            attachments,
            input: String::new(),
            input_active: false,
            last_typing_sent: None,
            scroll: 0,
            follow_mode: true,
            last_max_scroll: Cell::new(0),
            card_focus: false,
            focused_card_idx: 0,
            unread: 0,
        });
    }
    app.switch_to_room(room_id);
}

/// Restores the terminal on drop — raw mode off, alternate screen left,
/// mouse capture disabled. Holding one of these guarantees cleanup on
/// every exit path (normal return, early `?`, or a panic unwind), not
/// just the happy path.
struct TerminalGuard;

impl Drop for TerminalGuard {
    fn drop(&mut self) {
        let _ = disable_raw_mode();
        let _ = execute!(
            io::stdout(),
            LeaveAlternateScreen,
            DisableMouseCapture,
            DisableFocusChange
        );
    }
}

/// Install a panic hook that restores the terminal *before* the default
/// hook prints the panic message. Without this, a panic inside the TUI
/// prints into the alternate screen, which is then torn down — losing the
/// message. Call once at startup.
pub fn install_panic_hook() {
    let original = std::panic::take_hook();
    std::panic::set_hook(Box::new(move |info| {
        let _ = disable_raw_mode();
        let _ = execute!(
            io::stdout(),
            LeaveAlternateScreen,
            DisableMouseCapture,
            DisableFocusChange
        );
        original(info);
    }));
}

pub async fn run_tui(handle: AppHandle) -> Result<()> {
    enable_raw_mode()?;
    // From here on, every exit path restores the terminal.
    let _guard = TerminalGuard;
    let mut stdout = io::stdout();
    execute!(
        stdout,
        EnterAlternateScreen,
        EnableMouseCapture,
        EnableFocusChange
    )?;
    let backend = CrosstermBackend::new(stdout);
    let mut terminal = Terminal::new(backend)?;

    let mut app = TuiApp::new(handle);
    // huddle 0.6: if the user already opted in to update checks
    // (Some(true) on a prior launch), kick the once-per-24h poll.
    if matches!(app.handle.update_check_enabled(), Some(true)) {
        spawn_update_check(&app);
    }
    let mut event_rx = app.handle.subscribe();

    let result = main_loop(&mut terminal, &mut app, &mut event_rx).await;

    app.handle.shutdown().await;
    result
}

/// What `prompt_master_passphrase` hands back. An empty `passphrase`
/// means the user pressed Esc / Ctrl-C — caller should exit cleanly.
pub struct AuthPrompt {
    pub passphrase: String,
    /// Only populated on first-launch (sign-up) flow.
    pub username: Option<String>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
enum AuthField {
    Username,
    Passphrase,
    Confirm,
}

/// Prompt for the master passphrase before bringing up `AppHandle`.
/// First-launch (`is_new=true`) collects username + passphrase + confirm;
/// returning users only enter their passphrase. Tab cycles between
/// fields; Enter advances or submits.
pub fn prompt_master_passphrase(is_new: bool) -> Result<AuthPrompt> {
    use crossterm::event::{KeyCode, KeyModifiers};
    use ratatui::widgets::{Block, Borders, Clear, Padding, Paragraph};

    enable_raw_mode()?;
    let _guard = TerminalGuard;
    let mut stdout = io::stdout();
    execute!(stdout, EnterAlternateScreen)?;
    let backend = CrosstermBackend::new(stdout);
    let mut terminal = Terminal::new(backend)?;

    let mut username = String::new();
    let mut passphrase = String::new();
    let mut confirm = String::new();
    let mut field = if is_new {
        AuthField::Username
    } else {
        AuthField::Passphrase
    };
    let mut error: Option<String> = None;
    let mut outcome: Option<AuthPrompt> = None;

    while outcome.is_none() {
        terminal.draw(|f| {
            let height: u16 = if is_new { 18 } else { 12 };
            let area = crate::ui::centered_rect(64, height, f.area());
            f.render_widget(Clear, area);
            let block = Block::default()
                .borders(Borders::ALL)
                .border_style(Style::default().fg(Color::Cyan))
                .padding(Padding::uniform(1))
                .title(Span::styled(
                    if is_new {
                        " welcome to huddle — sign up "
                    } else {
                        " unlock huddle "
                    },
                    Style::default().fg(Color::Cyan).add_modifier(Modifier::BOLD),
                ));

            let masked = |s: &str| -> String { s.chars().map(|_| '').collect() };
            let label_style = |is_focused: bool| {
                if is_focused {
                    Style::default()
                        .fg(Color::Yellow)
                        .add_modifier(Modifier::BOLD)
                } else {
                    Style::default().fg(Color::DarkGray)
                }
            };
            let value_style = Style::default().fg(Color::White);
            let cursor = Span::styled("_", Style::default().fg(Color::DarkGray));

            let mut lines: Vec<Line> = Vec::new();
            lines.push(Line::from(""));
            if is_new {
                lines.push(Line::from(Span::styled(
                    "  pick a username (display name in chat — you can change it later)",
                    Style::default().fg(Color::DarkGray),
                )));
                lines.push(Line::from(Span::styled(
                    "  and a passphrase that encrypts your local database.",
                    Style::default().fg(Color::DarkGray),
                )));
                lines.push(Line::from(Span::styled(
                    "  forget the passphrase and your data is unrecoverable.",
                    Style::default().fg(Color::DarkGray),
                )));
                lines.push(Line::from(""));

                let u_focused = field == AuthField::Username;
                lines.push(Line::from(vec![
                    Span::styled("  username:    ", label_style(u_focused)),
                    Span::styled(username.clone(), value_style),
                    if u_focused {
                        cursor.clone()
                    } else {
                        Span::raw("")
                    },
                ]));
                let p_focused = field == AuthField::Passphrase;
                lines.push(Line::from(vec![
                    Span::styled("  passphrase:  ", label_style(p_focused)),
                    Span::styled(masked(&passphrase), value_style),
                    if p_focused {
                        cursor.clone()
                    } else {
                        Span::raw("")
                    },
                ]));
                let c_focused = field == AuthField::Confirm;
                lines.push(Line::from(vec![
                    Span::styled("  confirm:     ", label_style(c_focused)),
                    Span::styled(masked(&confirm), value_style),
                    if c_focused {
                        cursor.clone()
                    } else {
                        Span::raw("")
                    },
                ]));
            } else {
                lines.push(Line::from(Span::styled(
                    "  enter your passphrase to unlock the database.",
                    Style::default().fg(Color::DarkGray),
                )));
                lines.push(Line::from(""));
                lines.push(Line::from(vec![
                    Span::styled("  passphrase: ", label_style(true)),
                    Span::styled(masked(&passphrase), value_style),
                    cursor.clone(),
                ]));
            }

            if let Some(err) = &error {
                lines.push(Line::from(""));
                lines.push(Line::from(Span::styled(
                    format!("  ! {}", err),
                    Style::default().fg(Color::Red),
                )));
            }
            lines.push(Line::from(""));
            let hint_label = if is_new {
                if field == AuthField::Confirm {
                    " sign up   "
                } else {
                    " next field   "
                }
            } else {
                " unlock   "
            };
            lines.push(Line::from(vec![
                Span::styled(" Enter", Style::default().fg(Color::Yellow)),
                Span::styled(hint_label, Style::default().fg(Color::DarkGray)),
                Span::styled("Tab", Style::default().fg(Color::Yellow)),
                Span::styled(" cycle fields   ", Style::default().fg(Color::DarkGray)),
                Span::styled("Esc", Style::default().fg(Color::Yellow)),
                Span::styled(" cancel", Style::default().fg(Color::DarkGray)),
            ]));
            f.render_widget(Paragraph::new(lines).block(block), area);
        })?;

        if event::poll(Duration::from_millis(200))? {
            if let Event::Key(key) = event::read()? {
                if key.modifiers.contains(KeyModifiers::CONTROL)
                    && matches!(key.code, KeyCode::Char('c'))
                {
                    outcome = Some(AuthPrompt {
                        passphrase: String::new(),
                        username: None,
                    });
                    break;
                }
                match key.code {
                    KeyCode::Esc => {
                        outcome = Some(AuthPrompt {
                            passphrase: String::new(),
                            username: None,
                        });
                    }
                    KeyCode::Tab if is_new => {
                        field = match field {
                            AuthField::Username => AuthField::Passphrase,
                            AuthField::Passphrase => AuthField::Confirm,
                            AuthField::Confirm => AuthField::Username,
                        };
                    }
                    KeyCode::Backspace => match field {
                        AuthField::Username => {
                            username.pop();
                        }
                        AuthField::Passphrase => {
                            passphrase.pop();
                        }
                        AuthField::Confirm => {
                            confirm.pop();
                        }
                    },
                    KeyCode::Enter => {
                        if is_new {
                            match field {
                                AuthField::Username => {
                                    if username.trim().is_empty() {
                                        error = Some("username can't be empty".into());
                                    } else {
                                        error = None;
                                        field = AuthField::Passphrase;
                                    }
                                }
                                AuthField::Passphrase => {
                                    if passphrase.is_empty() {
                                        error = Some("passphrase can't be empty".into());
                                    } else {
                                        error = None;
                                        field = AuthField::Confirm;
                                    }
                                }
                                AuthField::Confirm => {
                                    if confirm != passphrase {
                                        error = Some("passphrases don't match — try again".into());
                                        confirm.clear();
                                    } else {
                                        outcome = Some(AuthPrompt {
                                            passphrase: passphrase.clone(),
                                            username: Some(username.trim().to_string()),
                                        });
                                    }
                                }
                            }
                        } else if passphrase.is_empty() {
                            error = Some("passphrase can't be empty".into());
                        } else {
                            outcome = Some(AuthPrompt {
                                passphrase: passphrase.clone(),
                                username: None,
                            });
                        }
                    }
                    KeyCode::Char(c) => match field {
                        AuthField::Username => username.push(c),
                        AuthField::Passphrase => passphrase.push(c),
                        AuthField::Confirm => confirm.push(c),
                    },
                    _ => {}
                }
            }
        }
    }

    Ok(outcome.unwrap_or(AuthPrompt {
        passphrase: String::new(),
        username: None,
    }))
}

/// Show the welcome card before bringing up `AppHandle`. Returns `Ok(true)`
/// when the user is ready to continue or `Ok(false)` if they pressed
/// Ctrl-C / q (caller exits without starting the app).
pub fn show_welcome() -> Result<bool> {
    use crossterm::event::{KeyCode, KeyModifiers};

    enable_raw_mode()?;
    let _guard = TerminalGuard;
    let mut stdout = io::stdout();
    execute!(stdout, EnterAlternateScreen)?;
    let backend = CrosstermBackend::new(stdout);
    let mut terminal = Terminal::new(backend)?;

    let outcome = loop {
        terminal.draw(crate::ui::picker::render_welcome)?;
        if poll(Duration::from_millis(200))? {
            if let Event::Key(key) = event::read()? {
                if key.modifiers.contains(KeyModifiers::CONTROL)
                    && matches!(key.code, KeyCode::Char('c'))
                {
                    break false;
                }
                match key.code {
                    KeyCode::Char('q') => break false,
                    _ => break true,
                }
            }
        }
    };
    Ok(outcome)
}

async fn main_loop(
    terminal: &mut Terminal<CrosstermBackend<io::Stdout>>,
    app: &mut TuiApp,
    event_rx: &mut tokio::sync::broadcast::Receiver<AppEvent>,
) -> Result<()> {
    let mut should_quit = false;
    let mut last_refresh = std::time::Instant::now();

    while !should_quit {
        terminal.draw(|f| crate::ui::render(f, app))?;

        // Drain any pending app events.
        while let Ok(ev) = event_rx.try_recv() {
            app.handle_app_event(ev);
        }

        // Refresh discovered rooms every second (covers TTL pruning).
        if last_refresh.elapsed() > Duration::from_secs(1) {
            app.refresh_discovered();
            app.refresh_attachments();
            last_refresh = std::time::Instant::now();
        }

        // Drop expired status-bar messages.
        app.tick_status();

        // huddle 0.7.4: end of the startup catch-up window. Drain the
        // accumulated counter into one summary desktop notification
        // ("N new messages while you were away") and disarm the gate
        // so future messages route through the per-message path.
        if let Some(deadline) = app.startup_grace_until {
            if Instant::now() >= deadline {
                let n = app.startup_catchup_count;
                app.startup_catchup_count = 0;
                app.startup_grace_until = None;
                if n > 0 && app.handle.notifications_enabled() {
                    let body = if n == 1 {
                        "1 new message while you were away".to_string()
                    } else {
                        format!("{} new messages while you were away", n)
                    };
                    crate::notifier::notify("huddle", &body);
                }
            }
        }

        // huddle 0.6: drain the update-check slot. The poll task
        // writes here when a newer version is detected; we copy
        // into `update_banner` once so the lobby renders the banner.
        if app.update_banner.is_none() {
            if let Ok(mut slot) = app.update_check_slot.lock() {
                if let Some(v) = slot.take() {
                    app.update_banner = Some(v);
                }
            }
        }

        // huddle 0.5: if go_dark fired, hold the goodbye modal on
        // screen for `GO_DARK_FAREWELL`, then quit. The data dir is
        // already wiped at this point; the network task is down.
        if let Some(t) = app.went_dark_at {
            if t.elapsed() >= GO_DARK_FAREWELL {
                should_quit = true;
                continue;
            }
        }

        // Phase A: an inbound-dial modal that's been ignored for 15s
        // gets spilled to the persistent `pending_friend_requests`
        // table (huddle 0.7.7). The live libp2p connection is closed
        // so we don't leave an unknown peer attached — but the
        // *request* lives on for up to 3 days, viewable + acceptable
        // from the People pane. A startup sweep removes rows older
        // than the TTL so the table stays bounded.
        let auto_reject_state: Option<InboundDialState> =
            if let Modal::InboundDial(s) = &app.modal {
                if s.opened_at.elapsed() >= Duration::from_secs(15) {
                    Some(s.clone())
                } else {
                    None
                }
            } else {
                None
            };
        if let Some(s) = auto_reject_state {
            // Persist first so a failed reject_inbound doesn't drop
            // the request on the floor.
            if let Err(e) =
                app.handle
                    .spill_pending_friend_request(s.peer_id, &s.fingerprint, &s.address)
            {
                tracing::warn!(%e, "failed to spill pending friend request");
            }
            // Disconnect-only — do NOT block_peer. The user hasn't
            // decided yet; reject_inbound's block was appropriate for
            // a transient 15s timeout but blocks a peer permanently,
            // which contradicts "remember the request for 3 days".
            app.handle.disconnect_peer(s.peer_id).await;
            app.set_status(format!(
                "saved request from {} — review in People → Pending",
                short_fp(&s.fingerprint)
            ));
            app.modal = Modal::None;
            app.refresh_pending_requests();
        }

        if poll(Duration::from_millis(33))? {
            match event::read()? {
                Event::Key(key) => {
                    let action = input::map_key(key, app);
                    should_quit = handle_action(action, app).await?;
                }
                Event::Mouse(m) => {
                    if matches!(m.kind, MouseEventKind::Down(MouseButton::Left))
                        && app.current_pane_room_id().is_some()
                    {
                        // Click-to-toggle: clicking anywhere inside the
                        // chat area while we're in a room enters card
                        // focus mode if there are cards. Precise hit-
                        // testing per-card requires Rect tracking from
                        // render — left as a follow-up.
                        if let Some(r) = app.active_room() {
                            if !r.attachments.is_empty() && !r.card_focus {
                                handle_action(input::Action::ToggleCardFocus, app).await?;
                            }
                        }
                    }
                }
                Event::FocusGained => crate::notifier::set_focused(true),
                Event::FocusLost => crate::notifier::set_focused(false),
                _ => {}
            }
        }

        // An async-event modal queued while the user was mid-interaction
        // (see `replace_modal_if_idle`) surfaces once the foreground modal
        // is dismissed — by any path, not just `Action::CloseModal`. The
        // queue is FIFO (huddle 0.6); we drain one per tick so the user
        // sees them in arrival order.
        if matches!(app.modal, Modal::None) {
            if let Some(m) = app.pending_modals.pop_front() {
                app.modal = m;
            }
        }
    }

    Ok(())
}

async fn handle_action(action: Action, app: &mut TuiApp) -> Result<bool> {
    match action {
        Action::Nothing => Ok(false),
        Action::Quit => Ok(true),
        Action::OpenQuitConfirm => {
            app.modal = Modal::QuitConfirm;
            Ok(false)
        }
        Action::OpenClearBlockedConfirm => {
            app.modal = Modal::ConfirmClearBlocked;
            Ok(false)
        }
        Action::CloseModal => {
            app.modal = Modal::None;
            Ok(false)
        }
        Action::OpenStartRoom => {
            app.modal = Modal::StartRoom(StartRoomState::new());
            Ok(false)
        }
        Action::OpenHelp => {
            app.help_scroll = 0;
            app.modal = Modal::Help;
            Ok(false)
        }
        Action::LobbyNavigateUp => {
            sidebar_move(app, -1);
            Ok(false)
        }
        Action::LobbyNavigateDown => {
            sidebar_move(app, 1);
            Ok(false)
        }
        Action::LobbyRefresh => {
            app.refresh_discovered();
            app.refresh_known_peers();
            Ok(false)
        }
        Action::LobbyFocusToggle => {
            // huddle 0.7: Tab now jumps to the next sidebar section
            // (rather than toggling between two flat lists). Acts as
            // pane-focus toggle when on the sidebar and a chat pane.
            sidebar_jump_section(app, 1);
            Ok(false)
        }
        Action::FocusSidebar => {
            // huddle 0.7.2: Ctrl+Left. Blur chat input if active so
            // typing keystrokes route to sidebar nav, not into the
            // last-active chat.
            if let Some(r) = app.active_room_mut() {
                r.input_active = false;
            }
            app.sidebar.focus = SidebarFocus::Sidebar;
            Ok(false)
        }
        Action::FocusPane => {
            // huddle 0.7.2: Ctrl+Right. On chat panes, also focus the
            // input so typing goes straight into the composer.
            app.sidebar.focus = SidebarFocus::Pane;
            if matches!(app.pane, Pane::Dm(_) | Pane::Group(_)) {
                if let Some(r) = app.active_room_mut() {
                    r.input_active = true;
                }
            }
            Ok(false)
        }
        Action::LobbyReconnectPeer => {
            if let Some(p) = app.known_peers.get(app.selected_known_idx).cloned() {
                if let Err(e) = app.handle.redial(&p.address).await {
                    app.modal = Modal::Error(format!("dial failed: {e}"));
                }
            }
            Ok(false)
        }
        Action::LobbyForgetPeer => {
            if let Some(p) = app.known_peers.get(app.selected_known_idx).cloned() {
                if let Err(e) = app.handle.forget_peer(&p.address).await {
                    app.modal = Modal::Error(format!("forget failed: {e}"));
                }
                app.refresh_known_peers();
                if app.selected_known_idx >= app.known_peers.len() && !app.known_peers.is_empty() {
                    app.selected_known_idx = app.known_peers.len() - 1;
                }
            }
            Ok(false)
        }
        Action::OpenDialPeer => {
            app.modal = Modal::DialPeer(DialPeerState::default());
            Ok(false)
        }
        Action::DialPeerTypeChar(c) => {
            if let Modal::DialPeer(s) = &mut app.modal {
                s.address.push(c);
            }
            Ok(false)
        }
        Action::DialPeerBackspace => {
            if let Modal::DialPeer(s) = &mut app.modal {
                s.address.pop();
            }
            Ok(false)
        }
        Action::DialPeerConfirm => {
            let address = match &app.modal {
                Modal::DialPeer(s) => s.address.clone(),
                _ => return Ok(false),
            };
            if address.trim().is_empty() {
                if let Modal::DialPeer(s) = &mut app.modal {
                    s.status = Some("address is empty".into());
                }
                return Ok(false);
            }
            match app.handle.dial(&address).await {
                Ok(()) => {
                    if let Modal::DialPeer(s) = &mut app.modal {
                        s.status = Some(format!("dialing {}", address));
                    }
                }
                Err(e) => {
                    app.modal = Modal::Error(format!("invalid address: {e}"));
                }
            }
            Ok(false)
        }
        Action::LobbyJoinSelected => {
            // huddle 0.7: Enter on the sidebar commits the selection.
            // For Dm/Group items, switch the pane. For GroupDiscover
            // children, walk the discovered list and join the first
            // unjoined group.
            match app.sidebar.selection.clone() {
                SidebarItem::Dm(room_id) | SidebarItem::Group(room_id) => {
                    if app.handle.active_room_info(&room_id).is_some() {
                        open_existing_room_tab(app, &room_id);
                        return Ok(false);
                    }
                    let room = app
                        .handle
                        .discovered_rooms()
                        .into_iter()
                        .find(|d| d.room_id == room_id);
                    if let Some(room) = room {
                        if room.encrypted {
                            app.modal = Modal::JoinRoom(JoinRoomState {
                                room_id: room.room_id.clone(),
                                room_name: room.name.clone(),
                                encrypted: true,
                                passphrase: String::new(),
                            });
                        } else if let Err(e) = app.handle.join_room(&room.room_id, None).await {
                            app.modal = Modal::Error(format!("join failed: {e}"));
                        }
                    }
                }
                SidebarItem::Section(s) => {
                    // Enter on a section header toggles its expand state.
                    if app.sidebar.expanded.contains(&s) {
                        app.sidebar.expanded.remove(&s);
                    } else {
                        app.sidebar.expanded.insert(s);
                    }
                }
                SidebarItem::Profile => app.pane = Pane::Profile,
                SidebarItem::Person(_) => app.pane = Pane::People,
                SidebarItem::Activity => app.pane = Pane::Activity,
                SidebarItem::Settings => app.pane = Pane::Settings,
                SidebarItem::DirectAddFriend => {
                    // huddle 0.7.8: pinned "+ Add Friend" row → fire the
                    // same flow as the global `m` shortcut.
                    return Box::pin(handle_action(Action::OpenComposeDm, app)).await;
                }
                SidebarItem::GroupNew => {
                    return Box::pin(handle_action(Action::OpenStartRoom, app)).await;
                }
                SidebarItem::PeoplePendingBadge => {
                    return Box::pin(handle_action(Action::JumpToPeoplePane, app)).await;
                }
                SidebarItem::GroupDiscover => {
                    // Find first unjoined group room and open its join modal.
                    if let Some(room) = app
                        .handle
                        .discovered_rooms()
                        .into_iter()
                        .find(|r| {
                            r.kind != huddle_core::storage::repo::RoomKind::Direct
                                && !app
                                    .handle
                                    .active_room_ids()
                                    .iter()
                                    .any(|aid| aid == &r.room_id)
                        })
                    {
                        if room.encrypted {
                            app.modal = Modal::JoinRoom(JoinRoomState {
                                room_id: room.room_id.clone(),
                                room_name: room.name.clone(),
                                encrypted: true,
                                passphrase: String::new(),
                            });
                        } else if let Err(e) = app.handle.join_room(&room.room_id, None).await {
                            app.modal = Modal::Error(format!("join failed: {e}"));
                        }
                    }
                }
            }
            Ok(false)
        }
        Action::StartRoomNextField => {
            if let Modal::StartRoom(s) = &mut app.modal {
                s.focus = match s.focus {
                    StartField::Name => StartField::Encrypted,
                    StartField::Encrypted => {
                        if s.encrypted {
                            StartField::Passphrase
                        } else {
                            StartField::Name
                        }
                    }
                    StartField::Passphrase => StartField::Name,
                };
            }
            Ok(false)
        }
        Action::StartRoomToggleEncrypted => {
            if let Modal::StartRoom(s) = &mut app.modal {
                s.encrypted = !s.encrypted;
                if !s.encrypted {
                    s.passphrase.clear();
                    // The passphrase field is hidden when encryption is
                    // off — don't strand focus on an invisible field.
                    if s.focus == StartField::Passphrase {
                        s.focus = StartField::Encrypted;
                    }
                }
            }
            Ok(false)
        }
        Action::StartRoomTypeChar(c) => {
            if let Modal::StartRoom(s) = &mut app.modal {
                match s.focus {
                    StartField::Name => s.name.push(c),
                    StartField::Passphrase => s.passphrase.push(c),
                    StartField::Encrypted => {}
                }
            }
            Ok(false)
        }
        Action::StartRoomBackspace => {
            if let Modal::StartRoom(s) = &mut app.modal {
                match s.focus {
                    StartField::Name => {
                        s.name.pop();
                    }
                    StartField::Passphrase => {
                        s.passphrase.pop();
                    }
                    StartField::Encrypted => {}
                }
            }
            Ok(false)
        }
        Action::StartRoomConfirm => {
            let (name, encrypted, passphrase) = match &app.modal {
                Modal::StartRoom(s) => (s.name.clone(), s.encrypted, s.passphrase.clone()),
                _ => return Ok(false),
            };
            if name.trim().is_empty() {
                app.modal = Modal::Error("room name cannot be empty".into());
                return Ok(false);
            }
            if encrypted && passphrase.is_empty() {
                app.modal = Modal::Error("encrypted room requires a passphrase".into());
                return Ok(false);
            }
            app.modal = Modal::None;
            let pp = if encrypted { Some(passphrase.as_str()) } else { None };
            if let Err(e) = app
                .handle
                .start_room(&name, encrypted, pp, huddle_core::storage::repo::RoomKind::Group)
                .await
            {
                app.modal = Modal::Error(format!("start failed: {e}"));
            }
            Ok(false)
        }
        Action::JoinRoomTypeChar(c) => {
            if let Modal::JoinRoom(j) = &mut app.modal {
                j.passphrase.push(c);
            }
            Ok(false)
        }
        Action::JoinRoomBackspace => {
            if let Modal::JoinRoom(j) = &mut app.modal {
                j.passphrase.pop();
            }
            Ok(false)
        }
        Action::JoinRoomConfirm => {
            let (room_id, passphrase) = match &app.modal {
                Modal::JoinRoom(j) => (j.room_id.clone(), j.passphrase.clone()),
                _ => return Ok(false),
            };
            app.modal = Modal::None;
            if let Err(e) = app.handle.join_room(&room_id, Some(&passphrase)).await {
                app.modal = Modal::Error(format!("join failed: {e}"));
            }
            Ok(false)
        }
        Action::TabNext => {
            // huddle 0.7: Tab-next now walks DM + Group rooms in
            // sidebar order. Retired the tab-bar concept; sidebar is
            // the navigation source of truth.
            switch_chat_relative(app, 1);
            Ok(false)
        }
        Action::TabPrev => {
            switch_chat_relative(app, -1);
            Ok(false)
        }
        Action::TabSelect(n) => {
            // huddle 0.7: Alt+1..9 jumps to the Nth chat (DM + Group
            // combined, sidebar order).
            switch_chat_absolute(app, n);
            Ok(false)
        }
        Action::BackToLobby => {
            // huddle 0.7: "back to lobby" is now "focus the sidebar".
            // The Welcome pane is the home screen.
            app.sidebar.focus = SidebarFocus::Sidebar;
            if matches!(app.pane, Pane::Dm(_) | Pane::Group(_)) {
                // Don't actually change pane — let the user choose. The
                // explicit way to leave a chat is `Ctrl+L` (LeaveRoom).
            } else {
                app.pane = Pane::Welcome;
            }
            Ok(false)
        }
        Action::LeaveRoom => {
            if let Some(room) = app.active_room() {
                let id = room.room_id.clone();
                match app.handle.leave_room(&id).await {
                    Ok(true) => {}
                    Ok(false) => app.set_status(
                        "left locally — peers may still see you until they time you out",
                    ),
                    Err(e) => app.modal = Modal::Error(format!("leave failed: {e}")),
                }
            }
            Ok(false)
        }
        Action::FocusInput => {
            if let Some(r) = app.active_room_mut() {
                r.input_active = true;
            }
            Ok(false)
        }
        Action::BlurInput => {
            if let Some(r) = app.active_room_mut() {
                r.input_active = false;
            }
            Ok(false)
        }
        Action::ScrollUp => {
            scroll_by(app, -1);
            Ok(false)
        }
        Action::ScrollDown => {
            scroll_by(app, 1);
            Ok(false)
        }
        Action::PageUp => {
            scroll_by(app, -10);
            Ok(false)
        }
        Action::PageDown => {
            scroll_by(app, 10);
            Ok(false)
        }
        Action::JumpTop => {
            if let Some(r) = app.active_room_mut() {
                r.scroll = 0;
                r.follow_mode = false;
            }
            Ok(false)
        }
        Action::JumpBottom => {
            if let Some(r) = app.active_room_mut() {
                r.follow_mode = true;
            }
            Ok(false)
        }
        Action::ChatTypeChar(c) => {
            let (room_id, should_pulse) = {
                let r = match app.active_room_mut() {
                    Some(r) if r.input_active => r,
                    _ => return Ok(false),
                };
                r.input.push(c);
                let pulse = match r.last_typing_sent {
                    Some(t) if t.elapsed() < TYPING_DEBOUNCE => false,
                    _ => true,
                };
                if pulse {
                    r.last_typing_sent = Some(Instant::now());
                }
                (r.room_id.clone(), pulse)
            };
            if should_pulse {
                app.handle.broadcast_typing(&room_id).await;
            }
            Ok(false)
        }
        Action::ChatBackspace => {
            if let Some(r) = app.active_room_mut() {
                if r.input_active {
                    r.input.pop();
                }
            }
            Ok(false)
        }
        Action::ChatSend => {
            let (room_id, body) = {
                match app.active_room_mut() {
                    Some(r) if r.input_active && !r.input.trim().is_empty() => {
                        let body = r.input.clone();
                        r.input.clear();
                        (r.room_id.clone(), body)
                    }
                    _ => return Ok(false),
                }
            };
            if let Err(e) = app.handle.send_room_message(&room_id, &body).await {
                app.modal = Modal::Error(format!("send failed: {e}"));
            }
            Ok(false)
        }
        Action::ChatInsertNewline => {
            if let Some(r) = app.active_room_mut() {
                if r.input_active {
                    r.input.push('\n');
                }
            }
            Ok(false)
        }
        Action::ToggleCardFocus => {
            if let Some(r) = app.active_room_mut() {
                if r.attachments.is_empty() {
                    return Ok(false);
                }
                r.card_focus = !r.card_focus;
                if r.card_focus {
                    r.input_active = false;
                    if r.focused_card_idx >= r.attachments.len() {
                        r.focused_card_idx = 0;
                    }
                }
            }
            Ok(false)
        }
        Action::CardNext => {
            if let Some(r) = app.active_room_mut() {
                if !r.attachments.is_empty() {
                    r.focused_card_idx = (r.focused_card_idx + 1) % r.attachments.len();
                }
            }
            Ok(false)
        }
        Action::CardPrev => {
            if let Some(r) = app.active_room_mut() {
                if !r.attachments.is_empty() {
                    r.focused_card_idx = if r.focused_card_idx == 0 {
                        r.attachments.len() - 1
                    } else {
                        r.focused_card_idx - 1
                    };
                }
            }
            Ok(false)
        }
        Action::ActivateFocusedCard => {
            let (room_id, file_id, status, encrypted) = match focused_card_info(app) {
                Some(t) => t,
                None => return Ok(false),
            };
            use huddle_core::storage::repo::AttachmentStatus;
            match status {
                AttachmentStatus::Offered | AttachmentStatus::Downloading => {
                    // Auto-pulled by gossipsub; just status nudge.
                    app.set_status("waiting for chunks…");
                }
                AttachmentStatus::Ready | AttachmentStatus::Saved => {
                    match app.handle.save_to_downloads(&room_id, &file_id).await {
                        Ok(path) => app.set_status(format!("saved to {}", path.display())),
                        Err(e) => app.modal = Modal::Error(format!("save failed: {e}")),
                    }
                }
                AttachmentStatus::Failed => {
                    app.set_status("retry not yet implemented — ask the sender to resend");
                }
                AttachmentStatus::Cancelled => {
                    app.set_status("transfer was cancelled");
                }
            }
            let _ = encrypted;
            Ok(false)
        }
        Action::OpenFocusedCard => {
            let (room_id, file_id, _, _) = match focused_card_info(app) {
                Some(t) => t,
                None => return Ok(false),
            };
            if let Err(e) = app.handle.open_saved(&room_id, &file_id) {
                app.modal = Modal::Error(format!("open failed: {e}"));
            }
            Ok(false)
        }
        Action::CancelFocusedCard => {
            let (room_id, file_id, _, _) = match focused_card_info(app) {
                Some(t) => t,
                None => return Ok(false),
            };
            if let Err(e) = app.handle.cancel_transfer(&room_id, &file_id).await {
                app.modal = Modal::Error(format!("cancel failed: {e}"));
            }
            Ok(false)
        }
        Action::SaveAgainFocusedCard => {
            let (room_id, file_id, _, _) = match focused_card_info(app) {
                Some(t) => t,
                None => return Ok(false),
            };
            match app.handle.save_to_downloads(&room_id, &file_id).await {
                Ok(path) => app.set_status(format!("saved to {}", path.display())),
                Err(e) => app.modal = Modal::Error(format!("save failed: {e}")),
            }
            Ok(false)
        }
        Action::OpenAttachmentPicker => {
            if app.active_room().is_none() {
                app.set_status("attach is only available inside a room");
                return Ok(false);
            }
            app.modal = Modal::AttachPicker(AttachPickerState::new());
            Ok(false)
        }
        Action::AttachPickerUp => {
            if let Modal::AttachPicker(s) = &mut app.modal {
                if s.selected > 0 {
                    s.selected -= 1;
                }
            }
            Ok(false)
        }
        Action::AttachPickerDown => {
            if let Modal::AttachPicker(s) = &mut app.modal {
                if s.selected + 1 < s.entries.len() {
                    s.selected += 1;
                }
            }
            Ok(false)
        }
        Action::AttachPickerAscend => {
            if let Modal::AttachPicker(s) = &mut app.modal {
                s.ascend();
            }
            Ok(false)
        }
        Action::OpenRotateRoom => {
            let room_id = match app.active_room() {
                Some(r) if r.encrypted => r.room_id.clone(),
                Some(_) => {
                    app.set_status("rotation only applies to encrypted rooms");
                    return Ok(false);
                }
                None => return Ok(false),
            };
            app.modal = Modal::RotateRoom(RotateRoomState {
                room_id,
                passphrase: String::new(),
            });
            Ok(false)
        }
        Action::RotateRoomTypeChar(c) => {
            if let Modal::RotateRoom(s) = &mut app.modal {
                s.passphrase.push(c);
            }
            Ok(false)
        }
        Action::RotateRoomBackspace => {
            if let Modal::RotateRoom(s) = &mut app.modal {
                s.passphrase.pop();
            }
            Ok(false)
        }
        Action::RotateRoomConfirm => {
            let (room_id, pp) = match &app.modal {
                Modal::RotateRoom(s) => (s.room_id.clone(), s.passphrase.clone()),
                _ => return Ok(false),
            };
            if pp.is_empty() {
                app.modal = Modal::Error("new passphrase cannot be empty".into());
                return Ok(false);
            }
            app.modal = Modal::None;
            match app.handle.rotate_room(&room_id, &pp).await {
                Ok(()) => app.set_status("rotation broadcast — share the new passphrase out-of-band"),
                Err(e) => app.modal = Modal::Error(format!("rotate failed: {e}")),
            }
            Ok(false)
        }
        Action::AcceptRotationTypeChar(c) => {
            if let Modal::AcceptRotation(s) = &mut app.modal {
                s.passphrase.push(c);
            }
            Ok(false)
        }
        Action::AcceptRotationBackspace => {
            if let Modal::AcceptRotation(s) = &mut app.modal {
                s.passphrase.pop();
            }
            Ok(false)
        }
        Action::AcceptRotationConfirm => {
            let (room_id, new_salt, pp) = match &app.modal {
                Modal::AcceptRotation(s) => {
                    (s.room_id.clone(), s.new_salt.clone(), s.passphrase.clone())
                }
                _ => return Ok(false),
            };
            if pp.is_empty() {
                return Ok(false);
            }
            app.modal = Modal::None;
            match app.handle.accept_rotation(&room_id, &new_salt, &pp).await {
                Ok(()) => app.set_status("accepted rotation — new key in use"),
                Err(e) => app.modal = Modal::Error(format!("accept rotation failed: {e}")),
            }
            Ok(false)
        }
        Action::OpenQrIdentity => {
            app.modal = Modal::QrIdentity;
            Ok(false)
        }
        Action::ToggleMute => {
            let room_id = match app.active_room() {
                Some(r) => r.room_id.clone(),
                None => return Ok(false),
            };
            let now_muted = app.handle.is_room_muted(&room_id);
            if let Err(e) = app.handle.set_room_muted(&room_id, !now_muted) {
                app.modal = Modal::Error(format!("mute toggle failed: {e}"));
            } else {
                app.set_status(if !now_muted { "muted" } else { "unmuted" });
            }
            Ok(false)
        }
        Action::OpenSearch => {
            let room_id = match app.active_room() {
                Some(r) => r.room_id.clone(),
                None => return Ok(false),
            };
            app.modal = Modal::Search(SearchState {
                room_id,
                query: String::new(),
                results: Vec::new(),
                selected: 0,
                searched: false,
            });
            Ok(false)
        }
        Action::SearchTypeChar(c) => {
            if let Modal::Search(s) = &mut app.modal {
                s.query.push(c);
            }
            Ok(false)
        }
        Action::SearchBackspace => {
            if let Modal::Search(s) = &mut app.modal {
                s.query.pop();
            }
            Ok(false)
        }
        Action::SearchSubmit => {
            let (room_id, query) = match &app.modal {
                Modal::Search(s) => (s.room_id.clone(), s.query.clone()),
                _ => return Ok(false),
            };
            if query.trim().is_empty() {
                return Ok(false);
            }
            let results = app
                .handle
                .search_room_messages(&room_id, &query, 100)
                .unwrap_or_default();
            if let Modal::Search(s) = &mut app.modal {
                s.results = results;
                s.selected = 0;
                s.searched = true;
            }
            Ok(false)
        }
        Action::SearchNext => {
            if let Modal::Search(s) = &mut app.modal {
                if s.selected + 1 < s.results.len() {
                    s.selected += 1;
                }
            }
            Ok(false)
        }
        Action::SearchPrev => {
            if let Modal::Search(s) = &mut app.modal {
                if s.selected > 0 {
                    s.selected -= 1;
                }
            }
            Ok(false)
        }
        Action::OpenVerify => {
            let room_id = match app.active_room() {
                Some(r) => r.room_id.clone(),
                None => return Ok(false),
            };
            let our_fp = app.handle.fingerprint().to_string();
            let verified_set: std::collections::HashSet<String> =
                app.handle.verified_fingerprints(&room_id).into_iter().collect();
            let members: Vec<(String, bool)> = app
                .active_room()
                .map(|r| {
                    r.members
                        .iter()
                        .filter(|fp| **fp != our_fp)
                        .map(|fp| (fp.clone(), verified_set.contains(fp)))
                        .collect()
                })
                .unwrap_or_default();
            if members.is_empty() {
                app.set_status("no other members to verify yet");
                return Ok(false);
            }
            app.modal = Modal::Verify(VerifyState {
                room_id,
                our_fingerprint: our_fp,
                members,
                selected: 0,
            });
            Ok(false)
        }
        Action::VerifyNext => {
            if let Modal::Verify(s) = &mut app.modal {
                if s.selected + 1 < s.members.len() {
                    s.selected += 1;
                }
            }
            Ok(false)
        }
        Action::VerifyPrev => {
            if let Modal::Verify(s) = &mut app.modal {
                if s.selected > 0 {
                    s.selected -= 1;
                }
            }
            Ok(false)
        }
        Action::VerifyToggle => {
            let (room_id, fp, new_state) = match &mut app.modal {
                Modal::Verify(s) => {
                    let m = match s.members.get_mut(s.selected) {
                        Some(x) => x,
                        None => return Ok(false),
                    };
                    m.1 = !m.1;
                    (s.room_id.clone(), m.0.clone(), m.1)
                }
                _ => return Ok(false),
            };
            if let Err(e) = app.handle.set_member_verified(&room_id, &fp, new_state) {
                app.modal = Modal::Error(format!("verify failed: {e}"));
            }
            Ok(false)
        }
        Action::OnboardingNext => {
            if let Modal::Onboarding { pages, cursor } = &mut app.modal {
                if *cursor + 1 < pages.len() {
                    *cursor += 1;
                } else {
                    let _ = app.handle.mark_onboarding_seen();
                    let _ = app
                        .handle
                        .set_last_seen_onboarding_version(env!("CARGO_PKG_VERSION"));
                    app.modal = Modal::None;
                }
            }
            Ok(false)
        }
        Action::OnboardingPrev => {
            if let Modal::Onboarding { cursor, .. } = &mut app.modal {
                if *cursor > 0 {
                    *cursor -= 1;
                }
            }
            Ok(false)
        }
        Action::OnboardingDismiss => {
            // Esc still records last_seen so the same pages don't re-pop
            // on next launch. Users replay via Settings → "Show what's
            // new" or the command palette.
            let _ = app.handle.mark_onboarding_seen();
            let _ = app
                .handle
                .set_last_seen_onboarding_version(env!("CARGO_PKG_VERSION"));
            app.modal = Modal::None;
            Ok(false)
        }
        // huddle 0.6: re-open onboarding regardless of last_seen. Shows
        // every page — gives users a way to revisit the welcome cards.
        Action::OpenWhatsNew => {
            let pages: Vec<usize> = (0..ONBOARDING_PAGES.len()).collect();
            app.modal = Modal::Onboarding { pages, cursor: 0 };
            Ok(false)
        }
        Action::OpenStatusHistory => {
            app.modal = Modal::StatusHistory { scroll: 0 };
            Ok(false)
        }
        Action::StatusHistoryScrollUp => {
            if let Modal::StatusHistory { scroll } = &mut app.modal {
                *scroll = scroll.saturating_sub(1);
            }
            Ok(false)
        }
        Action::StatusHistoryScrollDown => {
            if let Modal::StatusHistory { scroll } = &mut app.modal {
                *scroll = scroll.saturating_add(1);
            }
            Ok(false)
        }
        Action::StatusHistoryPageUp => {
            if let Modal::StatusHistory { scroll } = &mut app.modal {
                *scroll = scroll.saturating_sub(10);
            }
            Ok(false)
        }
        Action::StatusHistoryPageDown => {
            if let Modal::StatusHistory { scroll } = &mut app.modal {
                *scroll = scroll.saturating_add(10);
            }
            Ok(false)
        }
        Action::ClearStatusHistory => {
            app.status_history.clear();
            app.set_status("notification history cleared");
            app.modal = Modal::None;
            Ok(false)
        }
        Action::OpenCommandPalette => {
            app.modal = Modal::CommandPalette(CommandPaletteState::default());
            Ok(false)
        }
        Action::CommandPaletteTypeChar(c) => {
            if let Modal::CommandPalette(s) = &mut app.modal {
                s.query.push(c);
                s.selected = 0;
            }
            Ok(false)
        }
        Action::CommandPaletteBackspace => {
            if let Modal::CommandPalette(s) = &mut app.modal {
                s.query.pop();
                s.selected = 0;
            }
            Ok(false)
        }
        Action::CommandPaletteNext => {
            if let Modal::CommandPalette(s) = &mut app.modal {
                let total = palette_filtered(&s.query).len();
                if s.selected + 1 < total {
                    s.selected += 1;
                }
            }
            Ok(false)
        }
        Action::CommandPalettePrev => {
            if let Modal::CommandPalette(s) = &mut app.modal {
                if s.selected > 0 {
                    s.selected -= 1;
                }
            }
            Ok(false)
        }
        Action::CommandPaletteConfirm => {
            let picked: Option<String> = if let Modal::CommandPalette(s) = &app.modal {
                let filtered = palette_filtered(&s.query);
                filtered.get(s.selected).map(|e| e.label.to_string())
            } else {
                None
            };
            app.modal = Modal::None;
            if let Some(label) = picked {
                return run_palette_action(&label, app).await;
            }
            Ok(false)
        }
        Action::MarkAllRead => {
            let mut n = 0u32;
            for r in &mut app.open_rooms {
                if r.unread > 0 {
                    n = n.saturating_add(r.unread);
                    r.unread = 0;
                }
            }
            app.set_status(if n == 0 {
                "no unread to mark".to_string()
            } else {
                format!("marked {} message(s) read across {} room(s)", n, app.open_rooms.len())
            });
            Ok(false)
        }
        Action::HelpScrollUp => {
            app.help_scroll = app.help_scroll.saturating_sub(1);
            Ok(false)
        }
        Action::HelpScrollDown => {
            app.help_scroll = app.help_scroll.saturating_add(1);
            Ok(false)
        }
        Action::HelpPageUp => {
            app.help_scroll = app.help_scroll.saturating_sub(10);
            Ok(false)
        }
        Action::HelpPageDown => {
            app.help_scroll = app.help_scroll.saturating_add(10);
            Ok(false)
        }
        Action::UpdateCheckOptInYes => {
            let _ = app.handle.set_update_check_enabled(true);
            app.modal = Modal::None;
            app.set_status("update check enabled — polling crates.io once per day");
            // Kick off a check immediately so the user sees the
            // outcome rather than waiting for the 24h timer.
            spawn_update_check(app);
            Ok(false)
        }
        Action::UpdateCheckOptInNo => {
            let _ = app.handle.set_update_check_enabled(false);
            app.modal = Modal::None;
            app.set_status("update check disabled — toggle later in settings");
            Ok(false)
        }
        Action::ToggleUpdateCheck => {
            let cur = app.handle.update_check_enabled().unwrap_or(false);
            let _ = app.handle.set_update_check_enabled(!cur);
            app.set_status(if !cur {
                "update check ON — polling crates.io once per day"
            } else {
                "update check OFF"
            });
            if !cur {
                spawn_update_check(app);
            } else {
                app.update_banner = None;
            }
            Ok(false)
        }
        Action::DismissUpdateBanner => {
            app.update_banner = None;
            Ok(false)
        }
        Action::GenerateInvite => {
            // Build an invite payload from what we know. host_multiaddr
            // comes from the first listen address + our peer_id. Room
            // section is included iff we're in a room view.
            let our_peer = app.handle.peer_id().to_string();
            let our_fp = app.handle.fingerprint().to_string();
            let listen = match app.listen_addresses.first() {
                Some(a) => a.clone(),
                None => {
                    app.set_status("no listen address yet — try again in a sec");
                    return Ok(false);
                }
            };
            // libp2p multiaddrs need /p2p/<peer-id> appended so the
            // dialer's pubkey check fires. Listen addr is usually
            // /ip4/0.0.0.0/tcp/<port> — substitute a real host on the
            // receiver's end isn't ideal but the IP is what the user
            // shared OOB anyway. For LAN we leave the 0.0.0.0 as-is;
            // the user can fix the URL before sharing if their auto-
            // detected addr isn't reachable from outside.
            let host_multiaddr = format!("{}/p2p/{}", listen, our_peer);

            let room = match app.active_room() {
                Some(r) => {
                    if let Some(info) = app.handle.active_room_info(&r.room_id) {
                        let salt_b64 = info.passphrase_salt.as_ref().map(|s| {
                            base64::engine::general_purpose::STANDARD.encode(s)
                        });
                        Some(huddle_core::invite::InviteRoom {
                            id: info.id.clone(),
                            name: info.name.clone(),
                            encrypted: info.encrypted,
                            salt_b64,
                            creator_fingerprint: info.creator_fingerprint.clone(),
                            owner_fingerprints: app.handle.room_owners(&info.id),
                        })
                    } else {
                        None
                    }
                }
                _ => None,
            };

            let unsigned = huddle_core::invite::InviteLink {
                v: 1,
                host_multiaddr,
                fingerprint: our_fp,
                room: room.clone(),
                creator_pubkey_b64: None,
                signed_at_ms: 0,
                signature_b64: None,
            };
            // huddle 0.7.11: sign via AppHandle so the invite is bound
            // to the local Ed25519 identity. Falls back to v=1 only if
            // signing somehow fails — the receiver will then show the
            // "this invite is unsigned" warning.
            let invite = app.handle.sign_invite(unsigned.clone()).unwrap_or(unsigned);
            match huddle_core::invite::encode(&invite) {
                Ok(url) => {
                    app.modal = Modal::ShowInvite(ShowInviteState {
                        url,
                        includes_room: room.map(|r| r.name),
                    });
                }
                Err(e) => app.modal = Modal::Error(format!("encode invite: {e}")),
            }
            Ok(false)
        }
        Action::OpenPasteInvite => {
            app.modal = Modal::PasteInvite(PasteInviteState { url: String::new() });
            Ok(false)
        }
        Action::PasteInviteTypeChar(c) => {
            if let Modal::PasteInvite(s) = &mut app.modal {
                s.url.push(c);
            }
            Ok(false)
        }
        Action::PasteInviteBackspace => {
            if let Modal::PasteInvite(s) = &mut app.modal {
                s.url.pop();
            }
            Ok(false)
        }
        Action::PasteInviteConfirm => {
            let url = match &app.modal {
                Modal::PasteInvite(s) => s.url.clone(),
                _ => return Ok(false),
            };
            match huddle_core::invite::decode(url.trim()) {
                Ok(invite) => {
                    app.modal = Modal::ConfirmInvite(ConfirmInviteState { invite });
                }
                Err(e) => {
                    app.modal = Modal::Error(format!("bad invite link: {e}"));
                }
            }
            Ok(false)
        }
        Action::ConfirmInviteAccept => {
            let invite = match &app.modal {
                Modal::ConfirmInvite(s) => s.invite.clone(),
                _ => return Ok(false),
            };
            app.modal = Modal::None;
            // Dial via the invite-aware path. libp2p enforces the
            // embedded /p2p/<peer-id> check at the transport level
            // (structural MITM defense), AND once Identify lands the
            // app-layer post-dial arm compares the cryptographic fp
            // against `invite.fingerprint`, disconnecting on mismatch.
            // If a room was included, we open Join modal speculatively
            // — the joiner types the passphrase, and the actual
            // join_room call succeeds once the room announcement
            // arrives.
            match app
                .handle
                .dial_invite(&invite.host_multiaddr, &invite.fingerprint)
                .await
            {
                Ok(()) => app.set_status(format!("dialing {} via invite…", short_fp(&invite.fingerprint))),
                Err(e) => {
                    app.modal = Modal::Error(format!("dial failed: {e}"));
                    return Ok(false);
                }
            }
            if let Some(room) = invite.room {
                if room.encrypted {
                    app.modal = Modal::JoinRoom(JoinRoomState {
                        room_id: room.id.clone(),
                        room_name: room.name.clone(),
                        encrypted: true,
                        passphrase: String::new(),
                    });
                } else if let Err(e) = app.handle.join_room(&room.id, None).await {
                    app.modal = Modal::Error(format!("join failed: {e}"));
                }
            }
            Ok(false)
        }
        Action::OpenGenerateJoinCode => {
            let (room_id, room_name) = match app.active_room() {
                Some(r) => (r.room_id.clone(), r.name.clone()),
                None => return Ok(false),
            };
            match app.handle.generate_join_code(&room_id) {
                Ok(code) => {
                    app.modal = Modal::ShowJoinCode(ShowJoinCodeState {
                        room_id,
                        room_name,
                        code,
                    });
                }
                Err(e) => app.set_status(format!("can't generate code: {e}")),
            }
            Ok(false)
        }
        Action::OpenJoinWithCode => {
            // huddle 0.7: meaningful on a Group sidebar item or the
            // GroupDiscover row.
            let room = match &app.sidebar.selection {
                SidebarItem::Group(id) | SidebarItem::Dm(id) => app
                    .handle
                    .discovered_rooms()
                    .into_iter()
                    .find(|d| d.room_id == *id),
                _ => None,
            };
            let room = match room {
                Some(r) => r,
                None => {
                    app.set_status("select an encrypted group in the sidebar first");
                    return Ok(false);
                }
            };
            if !room.encrypted {
                app.set_status("code-join only applies to encrypted rooms");
                return Ok(false);
            }
            app.modal = Modal::JoinWithCode(JoinWithCodeState {
                room_id: room.room_id,
                room_name: room.name,
                code: String::new(),
            });
            Ok(false)
        }
        Action::JoinWithCodeTypeChar(c) => {
            if let Modal::JoinWithCode(s) = &mut app.modal {
                s.code.push(c);
            }
            Ok(false)
        }
        Action::JoinWithCodeBackspace => {
            if let Modal::JoinWithCode(s) = &mut app.modal {
                s.code.pop();
            }
            Ok(false)
        }
        Action::JoinWithCodeConfirm => {
            let (room_id, code) = match &app.modal {
                Modal::JoinWithCode(s) => (s.room_id.clone(), s.code.clone()),
                _ => return Ok(false),
            };
            if code.trim().is_empty() {
                return Ok(false);
            }
            app.modal = Modal::None;
            if let Err(e) = app.handle.join_room_with_code(&room_id, code.trim()).await {
                app.modal = Modal::Error(format!("code join failed: {e}"));
            } else {
                app.set_status("code submitted — waiting for owner (up to 30 s)");
            }
            Ok(false)
        }
        Action::SettingsTabNext => {
            app.settings_tab = app.settings_tab.next();
            Ok(false)
        }
        Action::SettingsTabPrev => {
            app.settings_tab = app.settings_tab.prev();
            Ok(false)
        }
        Action::SettingsTabSelect(tab) => {
            app.settings_tab = tab;
            Ok(false)
        }
        Action::SettingsToggleMdns => {
            let now_on = app.handle.mdns_enabled();
            if let Err(e) = app.handle.set_mdns_enabled(!now_on) {
                app.modal = Modal::Error(format!("save failed: {e}"));
                return Ok(false);
            }
            let new_state = !now_on;
            app.set_status(if new_state {
                "LAN discovery enabled — restart huddle to apply"
            } else {
                "LAN discovery disabled — restart huddle to apply"
            });
            Ok(false)
        }
        Action::SettingsToggleNotifications => {
            let now_on = app.handle.notifications_enabled();
            if let Err(e) = app.handle.set_notifications_enabled(!now_on) {
                app.modal = Modal::Error(format!("save failed: {e}"));
                return Ok(false);
            }
            app.set_status(if !now_on {
                "desktop notifications on (OS-local only)"
            } else {
                "desktop notifications off"
            });
            Ok(false)
        }
        Action::ProfileFieldUp => {
            if app.profile_cursor > 0 {
                app.profile_cursor -= 1;
            }
            Ok(false)
        }
        Action::ProfileFieldDown => {
            let max = profile_field_count(app).saturating_sub(1);
            if app.profile_cursor < max {
                app.profile_cursor += 1;
            }
            Ok(false)
        }
        Action::ProfileFieldYank => {
            let (label, value) = match profile_field_at(app, app.profile_cursor) {
                Some(v) => v,
                None => return Ok(false),
            };
            match crate::clipboard::copy(&value) {
                Ok(()) => app.set_status(format!("copied {} to clipboard", label)),
                Err(e) => app.set_status(format!("copy failed: {}", e)),
            }
            Ok(false)
        }
        Action::OpenEditUsername => {
            // Pre-fill the editor with the current username so the user
            // can tweak rather than retype. Empty submission clears.
            let current = app.handle.display_name().unwrap_or_default();
            app.modal = Modal::EditUsername(EditUsernameState { input: current });
            Ok(false)
        }
        Action::EditUsernameTypeChar(c) => {
            if let Modal::EditUsername(s) = &mut app.modal {
                if s.input.chars().count() < 32 {
                    s.input.push(c);
                }
            }
            Ok(false)
        }
        Action::EditUsernameBackspace => {
            if let Modal::EditUsername(s) = &mut app.modal {
                s.input.pop();
            }
            Ok(false)
        }
        Action::EditUsernameConfirm => {
            let input = match &app.modal {
                Modal::EditUsername(s) => s.input.clone(),
                _ => return Ok(false),
            };
            let trimmed = input.trim();
            let new_name = if trimmed.is_empty() {
                None
            } else {
                Some(trimmed.to_string())
            };
            app.modal = Modal::None;
            if let Err(e) = app.handle.set_username(new_name.as_deref()).await {
                app.modal = Modal::Error(format!("set username failed: {e}"));
            } else {
                app.set_status(match &new_name {
                    Some(n) => format!("username set to {n}"),
                    None => "username cleared — you are now [anonymous]".into(),
                });
            }
            Ok(false)
        }
        Action::OpenGoDarkModal => {
            app.modal = Modal::GoDark(GoDarkState {
                requires_passphrase: app.handle.has_master_passphrase(),
                ..GoDarkState::default()
            });
            Ok(false)
        }
        Action::OpenAddFriend => {
            app.modal = Modal::AddFriend(AddFriendState::default());
            Ok(false)
        }
        Action::AddFriendTypeChar(c) => {
            if let Modal::AddFriend(s) = &mut app.modal {
                if s.input.chars().count() < 64 {
                    s.input.push(c);
                }
            }
            Ok(false)
        }
        Action::AddFriendBackspace => {
            if let Modal::AddFriend(s) = &mut app.modal {
                s.input.pop();
            }
            Ok(false)
        }
        Action::AddFriendConfirm => {
            let input = match &app.modal {
                Modal::AddFriend(s) => s.input.clone(),
                _ => return Ok(false),
            };
            if input.trim().is_empty() {
                return Ok(false);
            }
            app.modal = Modal::None;
            match app.handle.dial_by_id_or_username(input.trim()).await {
                Ok(()) => {
                    // libp2p races every known address (LAN > public IP
                    // > relay-circuit). The status line says "dialing"
                    // generically — actual transport surfaces in the
                    // ListeningOn / connected-peer events afterwards.
                    app.set_status(format!("dialing {} (racing LAN / IP / relay)…", input.trim()));
                }
                Err(e) => {
                    app.modal = Modal::Error(format!("add friend: {e}"));
                }
            }
            Ok(false)
        }
        Action::GoDarkTypeChar(c) => {
            if let Modal::GoDark(s) = &mut app.modal {
                if s.input.chars().count() < 128 {
                    s.input.push(c);
                }
            }
            Ok(false)
        }
        Action::GoDarkBackspace => {
            if let Modal::GoDark(s) = &mut app.modal {
                s.input.pop();
            }
            Ok(false)
        }
        Action::GoDarkConfirm => {
            // Snapshot mode + input before touching the modal again.
            let (input, requires_passphrase) = match &app.modal {
                Modal::GoDark(s) => (s.input.clone(), s.requires_passphrase),
                _ => return Ok(false),
            };
            // huddle 0.7.6: single gate per session mode.
            //   master passphrase mode → passphrase IS the gate; `go_dark`
            //     does the constant-time check internally.
            //   --no-master-passphrase mode → typed `DELETE EVERYTHING`
            //     is the only gate (no key to compare against).
            if !requires_passphrase && input != GO_DARK_CONFIRM_PHRASE {
                if let Modal::GoDark(s) = &mut app.modal {
                    s.last_error = Some(format!(
                        "type `{}` exactly to confirm",
                        GO_DARK_CONFIRM_PHRASE
                    ));
                    s.input.clear();
                }
                return Ok(false);
            }
            // For passphrase mode, pass `input` as the passphrase.
            // For no-master mode, the passphrase argument is ignored
            // by `go_dark` (it short-circuits the check on a zeroed key).
            let passphrase_to_send = if requires_passphrase {
                input
            } else {
                String::new()
            };
            match app.handle.go_dark(&passphrase_to_send).await {
                Ok(()) => {
                    // WentDark event fires from go_dark; the handler
                    // schedules the actual exit so the goodbye modal
                    // is visible for a beat.
                    Ok(false)
                }
                Err(e) => {
                    if let Modal::GoDark(s) = &mut app.modal {
                        s.last_error = Some(format!("{e}"));
                        s.input.clear();
                    }
                    Ok(false)
                }
            }
        }
        Action::SettingsToggleGlobalVerifiedOnly => {
            // huddle 0.7.8: pane-driven toggle. The previous version
            // mutated a per-modal snapshot; the pane reads the live
            // value via `handle.verified_only_inbound()` every render
            // so we just flip and persist.
            let new_state = !app.handle.verified_only_inbound();
            if let Err(e) = app.handle.set_verified_only_inbound(new_state) {
                app.modal = Modal::Error(format!("save failed: {e}"));
                return Ok(false);
            }
            Ok(false)
        }
        Action::ClearBlockedPeers => {
            // Iterate the persisted blocklist and unblock each. We do
            // this in two phases (snapshot, then loop) so the SQLite
            // connection isn't borrowed across the unblock_peer calls.
            let blocked = app.handle.list_blocked_peers();
            let n = blocked.len();
            let mut errors = 0usize;
            for fp in blocked {
                if app.handle.unblock_peer(&fp).is_err() {
                    errors += 1;
                }
            }
            // huddle 0.7.8: the Settings pane reads the live blocklist
            // count on every render, so no snapshot needs updating here.
            let msg = if errors == 0 {
                format!("cleared {} blocked peer(s)", n)
            } else {
                format!(
                    "cleared {} blocked peer(s); {} error(s)",
                    n.saturating_sub(errors),
                    errors
                )
            };
            app.set_status(msg);
            // huddle 0.7.11: ClearBlockedPeers is now reached via the
            // ConfirmClearBlocked modal; close it on success.
            app.modal = Modal::None;
            Ok(false)
        }
        Action::ToggleRoomVerifiedOnly => {
            let room_id = match app.active_room() {
                Some(r) => r.room_id.clone(),
                None => return Ok(false),
            };
            let our_fp = app.handle.fingerprint().to_string();
            if !app.handle.is_owner(&room_id, &our_fp) {
                app.set_status("only an owner can toggle room verified-only");
                return Ok(false);
            }
            let new_state = !app.handle.room_verified_only(&room_id);
            if let Err(e) = app.handle.set_room_verified_only(&room_id, new_state) {
                app.modal = Modal::Error(format!("toggle failed: {e}"));
                return Ok(false);
            }
            app.set_status(if new_state {
                "room is now verified-only — non-SAS-verified joiners refused"
            } else {
                "room verified-only mode off"
            });
            Ok(false)
        }
        Action::VerifyStartSas => {
            // From the Verify modal: kick off an SAS exchange with the
            // focused member. Replaces the Verify modal with the SAS
            // in-progress modal (Waiting → Comparing on response).
            let (room_id, partner_fp) = match &app.modal {
                Modal::Verify(v) => match v.members.get(v.selected) {
                    Some((fp, _)) => (v.room_id.clone(), fp.clone()),
                    None => return Ok(false),
                },
                _ => return Ok(false),
            };
            match app.handle.sas_start(&room_id, &partner_fp).await {
                Ok(tx_id) => {
                    app.modal = Modal::Sas(SasState {
                        room_id,
                        partner_fingerprint: partner_fp,
                        tx_id,
                        stage: SasStage::Waiting,
                    });
                }
                Err(e) => app.modal = Modal::Error(format!("SAS start failed: {e}")),
            }
            Ok(false)
        }
        Action::SasMatch => {
            if let Modal::Sas(s) = &mut app.modal {
                let tx_id = s.tx_id.clone();
                if let SasStage::Comparing {
                    ref mut our_matched,
                    ..
                } = s.stage
                {
                    *our_matched = true;
                }
                if let Err(e) = app.handle.sas_match(&tx_id).await {
                    app.modal = Modal::Error(format!("SAS match failed: {e}"));
                    return Ok(false);
                }
                app.set_status("SAS match sent — waiting for partner to confirm");
            }
            Ok(false)
        }
        Action::SasCancel => {
            if let Modal::Sas(s) = &app.modal {
                app.handle.sas_cancel(&s.tx_id);
            }
            app.modal = Modal::None;
            Ok(false)
        }
        Action::OpenKickPicker => {
            let (room_id, members) = match owner_action_members(app, MemberActionKind::Kick) {
                Some(t) => t,
                None => return Ok(false),
            };
            app.modal = Modal::MemberAction(MemberActionState {
                room_id,
                kind: MemberActionKind::Kick,
                members,
                selected: 0,
            });
            Ok(false)
        }
        Action::OpenGrantPicker => {
            let (room_id, members) = match owner_action_members(app, MemberActionKind::Grant) {
                Some(t) => t,
                None => return Ok(false),
            };
            app.modal = Modal::MemberAction(MemberActionState {
                room_id,
                kind: MemberActionKind::Grant,
                members,
                selected: 0,
            });
            Ok(false)
        }
        Action::ShowRoomBans => {
            // Owners only. We use the same `we_are_owner` gate as
            // kick/grant so the keybinding is silently ignored for
            // non-owners (matching the rest of the moderation surface).
            let room_id = match app.active_room() {
                Some(r) => r.room_id.clone(),
                None => return Ok(false),
            };
            if !app.handle.we_are_owner(&room_id) {
                return Ok(false);
            }
            let bans = app.handle.list_room_bans(&room_id);
            let body = if bans.is_empty() {
                "no bans in this room.".to_string()
            } else {
                let mut s = format!("{} ban(s) in this room:\n\n", bans.len());
                for fp in &bans {
                    s.push_str(&format!("  {}\n", short_fp(fp)));
                }
                s.push_str("\nban is enforced by key rotation (the banned peer can no longer derive the room key). press any key to dismiss.");
                s
            };
            app.replace_modal_if_idle(Modal::Info(body));
            Ok(false)
        }
        Action::MemberActionNext => {
            if let Modal::MemberAction(s) = &mut app.modal {
                if s.selected + 1 < s.members.len() {
                    s.selected += 1;
                }
            }
            Ok(false)
        }
        Action::MemberActionPrev => {
            if let Modal::MemberAction(s) = &mut app.modal {
                if s.selected > 0 {
                    s.selected -= 1;
                }
            }
            Ok(false)
        }
        Action::MemberActionConfirm => {
            let snapshot = if let Modal::MemberAction(s) = &app.modal {
                s.members
                    .get(s.selected)
                    .map(|(fp, _)| (s.room_id.clone(), s.kind, fp.clone()))
            } else {
                None
            };
            let (room_id, kind, target_fp) = match snapshot {
                Some(t) => t,
                None => return Ok(false),
            };
            app.modal = Modal::None;
            match kind {
                MemberActionKind::Grant => {
                    match app.handle.grant_owner(&room_id, &target_fp).await {
                        Ok(()) => app.set_status(format!("granted owner to {}", short_fp(&target_fp))),
                        Err(e) => app.modal = Modal::Error(format!("grant failed: {e}")),
                    }
                }
                MemberActionKind::Kick => {
                    match app.handle.kick_member(&room_id, &target_fp).await {
                        Ok(new_pp) if new_pp.is_empty() => {
                            app.set_status(format!("kicked {}", short_fp(&target_fp)));
                        }
                        Ok(new_pp) => {
                            // Show the new passphrase prominently so the
                            // owner can copy + share OOB with remaining
                            // members. Modal::Info dismisses on any key.
                            app.modal = Modal::Info(format!(
                                "kicked {}. new passphrase (share OOB with remaining members):\n\n  {}",
                                short_fp(&target_fp),
                                new_pp
                            ));
                        }
                        Err(e) => app.modal = Modal::Error(format!("kick failed: {e}")),
                    }
                }
            }
            Ok(false)
        }
        Action::InboundDialAccept => {
            if let Modal::InboundDial(s) = app.modal.clone() {
                app.handle.accept_inbound(s.peer_id, &s.address).await;
                app.set_status(format!("connected to {}", short_fp(&s.fingerprint)));
                app.modal = Modal::None;
                app.refresh_known_peers();
            }
            Ok(false)
        }
        Action::InboundDialReject => {
            if let Modal::InboundDial(s) = app.modal.clone() {
                if let Err(e) = app.handle.reject_inbound(s.peer_id, &s.fingerprint).await {
                    app.modal = Modal::Error(format!("reject failed: {e}"));
                    return Ok(false);
                }
                app.set_status(format!("rejected {}", short_fp(&s.fingerprint)));
                app.modal = Modal::None;
            }
            Ok(false)
        }
        Action::InboundDialTrust => {
            if let Modal::InboundDial(s) = app.modal.clone() {
                if let Err(e) = app
                    .handle
                    .trust_inbound(s.peer_id, &s.fingerprint, &s.address)
                    .await
                {
                    app.modal = Modal::Error(format!("trust failed: {e}"));
                    return Ok(false);
                }
                app.set_status(format!("trusted {} — won't ask again", short_fp(&s.fingerprint)));
                app.modal = Modal::None;
                app.refresh_known_peers();
            }
            Ok(false)
        }
        Action::AttachPickerDescendOrPick => {
            let pick: Option<std::path::PathBuf> = match &mut app.modal {
                Modal::AttachPicker(s) => {
                    if let Some(e) = s.entries.get(s.selected) {
                        if e.is_dir {
                            s.descend();
                            None
                        } else {
                            s.selected_path()
                        }
                    } else {
                        None
                    }
                }
                _ => None,
            };
            if let Some(path) = pick {
                let room_id = match app.active_room() {
                    Some(r) => r.room_id.clone(),
                    None => return Ok(false),
                };
                app.modal = Modal::None;
                match app.handle.send_file(&room_id, &path).await {
                    Ok(file_id) => {
                        app.set_status(format!("sending {} ({})", path.display(), &file_id[..12]));
                    }
                    Err(e) => {
                        app.modal = Modal::Error(format!("send failed: {e}"));
                    }
                }
            }
            Ok(false)
        }
        // huddle 0.7 sidebar/pane helpers
        Action::SidebarSectionPrev => {
            sidebar_jump_section(app, -1);
            Ok(false)
        }
        Action::SidebarToggleExpand => {
            sidebar_toggle_expand(app);
            Ok(false)
        }
        Action::JumpToPeoplePane => {
            app.pane = Pane::People;
            app.sidebar.selection = SidebarItem::Section(SidebarSection::People);
            // huddle 0.7.7: pull fresh pending-request rows so the
            // section count is right + the cursor stays in range. If
            // there are pending requests, land on that tab — that's
            // where the user almost certainly wants to look first.
            app.refresh_pending_requests();
            if !app.pending_requests.is_empty() {
                app.people_focus = PeopleFocus::Pending;
                app.selected_pending_idx = 0;
            }
            Ok(false)
        }
        Action::JumpToSettingsPane => {
            app.pane = Pane::Settings;
            // huddle 0.7.8: reset to Account tab on jump so `,` from
            // anywhere lands on a predictable surface.
            app.settings_tab = SettingsTab::Account;
            app.sidebar.selection = SidebarItem::Section(SidebarSection::Settings);
            Ok(false)
        }
        Action::OpenComposeDm => {
            app.modal = Modal::ComposeDm(ComposeDmState::default());
            Ok(false)
        }
        Action::ComposeDmTypeChar(c) => {
            if let Modal::ComposeDm(s) = &mut app.modal {
                s.input.push(c);
            }
            Ok(false)
        }
        Action::ComposeDmBackspace => {
            if let Modal::ComposeDm(s) = &mut app.modal {
                s.input.pop();
            }
            Ok(false)
        }
        Action::ComposeDmCancel => {
            app.modal = Modal::None;
            Ok(false)
        }
        Action::ComposeDmConfirm => {
            let input = match &app.modal {
                Modal::ComposeDm(s) => s.input.trim().to_string(),
                _ => return Ok(false),
            };
            if input.is_empty() {
                return Ok(false);
            }
            // Resolve input to a fingerprint via lookup helpers, then
            // start the DM. On unresolvable input we morph to AddFriend.
            let resolved = resolve_dm_target(app, &input);
            match resolved {
                Some(fp) => {
                    app.modal = Modal::None;
                    match app.handle.start_direct(&fp).await {
                        Ok(room_id) => {
                            app.switch_to_room(&room_id);
                            app.set_status(format!("DM with {}", short_fp(&fp)));
                        }
                        Err(e) => app.modal = Modal::Error(format!("DM failed: {e}")),
                    }
                }
                None => {
                    // State C: unrecognized text → morph into AddFriend.
                    app.modal = Modal::AddFriend(AddFriendState { input });
                }
            }
            Ok(false)
        }
        Action::ToggleMemberMargin => {
            app.show_member_margin = !app.show_member_margin;
            Ok(false)
        }
        Action::OpenInvitePicker => {
            // Picker only makes sense inside a group room — the
            // generated invite has to scope to *some* room, and DMs
            // can't take a third member by design. From any other
            // pane (Welcome, Profile, People, Activity, Settings) we
            // surface a status hint and bail.
            let (room_id, room_name, is_group) = match app.active_room() {
                Some(r) => {
                    let info = app.handle.active_room_info(&r.room_id);
                    let is_group = info
                        .as_ref()
                        .map(|i| i.kind != huddle_core::storage::repo::RoomKind::Direct)
                        .unwrap_or(false);
                    (
                        r.room_id.clone(),
                        info.map(|i| i.name).unwrap_or_default(),
                        is_group,
                    )
                }
                None => {
                    app.set_status("open a group first — `Shift+I` for OOB or `s` to create");
                    return Ok(false);
                }
            };
            if !is_group {
                app.set_status("DMs are 1-1 by design — open a group to invite peers");
                return Ok(false);
            }
            let candidates = gather_invite_candidates(app, &room_id);
            if candidates.is_empty() {
                app.set_status(
                    "no peers to invite yet — verify someone with Ctrl+V or share `Shift+I` OOB",
                );
                return Ok(false);
            }
            app.modal = Modal::InvitePicker(InvitePickerState {
                room_id,
                room_name,
                candidates,
                selected: HashSet::new(),
                filter: String::new(),
                cursor: 0,
                status_line: None,
            });
            Ok(false)
        }
        Action::InvitePickerCancel => {
            app.modal = Modal::None;
            Ok(false)
        }
        Action::InvitePickerFilterTypeChar(c) => {
            if let Modal::InvitePicker(s) = &mut app.modal {
                s.filter.push(c);
                s.cursor = 0;
                s.status_line = None;
            }
            Ok(false)
        }
        Action::InvitePickerFilterBackspace => {
            if let Modal::InvitePicker(s) = &mut app.modal {
                s.filter.pop();
                s.cursor = 0;
                s.status_line = None;
            }
            Ok(false)
        }
        Action::InvitePickerCursorUp => {
            if let Modal::InvitePicker(s) = &mut app.modal {
                if s.cursor > 0 {
                    s.cursor -= 1;
                }
                s.status_line = None;
            }
            Ok(false)
        }
        Action::InvitePickerCursorDown => {
            if let Modal::InvitePicker(s) = &mut app.modal {
                let visible_len = filtered_invite_candidates(s).len();
                if s.cursor + 1 < visible_len {
                    s.cursor += 1;
                }
                s.status_line = None;
            }
            Ok(false)
        }
        Action::InvitePickerToggleSelected => {
            if let Modal::InvitePicker(s) = &mut app.modal {
                let visible = filtered_invite_candidates(s);
                if let Some(c) = visible.get(s.cursor).cloned() {
                    if s.selected.contains(&c.fingerprint) {
                        s.selected.remove(&c.fingerprint);
                        s.status_line = None;
                    } else if s.selected.len() >= INVITE_PICKER_SOFT_CAP {
                        s.status_line = Some(format!(
                            "selection cap: {} max per send",
                            INVITE_PICKER_SOFT_CAP
                        ));
                    } else {
                        s.selected.insert(c.fingerprint);
                        s.status_line = None;
                    }
                }
            }
            Ok(false)
        }
        Action::InvitePickerSend => {
            let (room_id, selected_fps) = match &app.modal {
                Modal::InvitePicker(s) => {
                    if s.selected.is_empty() {
                        if let Modal::InvitePicker(s2) = &mut app.modal {
                            s2.status_line = Some(
                                "Space to select peers · Enter sends · Esc cancels".into(),
                            );
                        }
                        return Ok(false);
                    }
                    (s.room_id.clone(), s.selected.iter().cloned().collect::<Vec<_>>())
                }
                _ => return Ok(false),
            };
            // Build the invite link exactly once — same code path as
            // `Shift+I` but scoped to the captured room. Failure here
            // is structural (no listen address yet) and shows an error.
            let invite_text = match build_room_invite_link(app, &room_id) {
                Ok(t) => t,
                Err(e) => {
                    if let Modal::InvitePicker(s) = &mut app.modal {
                        s.status_line = Some(format!("invite build failed: {e}"));
                    }
                    return Ok(false);
                }
            };
            let mut sent = 0usize;
            let mut failures: Vec<String> = Vec::new();
            for fp in &selected_fps {
                let dm_room_id = match app.handle.start_direct(fp).await {
                    Ok(rid) => rid,
                    Err(e) => {
                        failures.push(format!("{}: {}", short_fp(fp), e));
                        continue;
                    }
                };
                match app.handle.send_room_message(&dm_room_id, &invite_text).await {
                    Ok(()) => sent += 1,
                    Err(e) => failures.push(format!("{}: {}", short_fp(fp), e)),
                }
            }
            app.modal = Modal::None;
            if failures.is_empty() {
                app.set_status(format!("sent invite to {} peer(s)", sent));
            } else {
                app.set_status(format!(
                    "sent to {}; failed for {}",
                    sent,
                    failures.len()
                ));
                tracing::warn!(?failures, "invite-picker send had partial failures");
            }
            Ok(false)
        }
        Action::PeopleFocusNext => {
            // huddle 0.7.7: Pending joins the rotation. Skipped when
            // empty so the cycle doesn't land on an empty tab the
            // user can't act on. Known is always present (even with
            // zero peers, it shows the "no known peers" hint), so
            // it's the safe fallback at the end.
            app.refresh_pending_requests();
            let has_pending = !app.pending_requests.is_empty();
            app.people_focus = match app.people_focus {
                PeopleFocus::Pending => PeopleFocus::Known,
                PeopleFocus::Known => PeopleFocus::Verified,
                PeopleFocus::Verified => PeopleFocus::Blocked,
                PeopleFocus::Blocked => {
                    if has_pending {
                        PeopleFocus::Pending
                    } else {
                        PeopleFocus::Known
                    }
                }
            };
            Ok(false)
        }
        Action::PendingRequestUp => {
            if app.selected_pending_idx > 0 {
                app.selected_pending_idx -= 1;
            }
            Ok(false)
        }
        Action::PendingRequestDown => {
            if app.selected_pending_idx + 1 < app.pending_requests.len() {
                app.selected_pending_idx += 1;
            }
            Ok(false)
        }
        Action::PendingRequestAccept => {
            let fp = app
                .pending_requests
                .get(app.selected_pending_idx)
                .map(|r| r.fingerprint.clone());
            if let Some(fp) = fp {
                match app.handle.accept_pending_friend_request(&fp).await {
                    Ok(()) => {
                        app.set_status(format!("re-dialing {}", short_fp(&fp)));
                    }
                    Err(e) => {
                        app.modal = Modal::Error(format!("accept failed: {e}"));
                    }
                }
                app.refresh_pending_requests();
                if app.pending_requests.is_empty() {
                    app.people_focus = PeopleFocus::Known;
                }
            }
            Ok(false)
        }
        Action::PendingRequestReject => {
            let fp = app
                .pending_requests
                .get(app.selected_pending_idx)
                .map(|r| r.fingerprint.clone());
            if let Some(fp) = fp {
                if let Err(e) = app.handle.reject_pending_friend_request(&fp) {
                    app.modal = Modal::Error(format!("reject failed: {e}"));
                } else {
                    app.set_status(format!("rejected + blocked {}", short_fp(&fp)));
                }
                app.refresh_pending_requests();
                if app.pending_requests.is_empty() {
                    app.people_focus = PeopleFocus::Known;
                }
            }
            Ok(false)
        }
        Action::PeoplePersonReconnect => {
            if let Some(p) = app.known_peers.get(app.selected_known_idx).cloned() {
                if let Err(e) = app.handle.redial(&p.address).await {
                    app.modal = Modal::Error(format!("dial failed: {e}"));
                }
            }
            Ok(false)
        }
        Action::PeoplePersonBlock => {
            if let Some(p) = app.known_peers.get(app.selected_known_idx).cloned() {
                // huddle 0.7.7: read from `fingerprint`, not `label`
                // (label is only set on add-by-id / username-resolved
                // peers; plain `d` dial peers leave it as None).
                if let Some(fp) = p.fingerprint.as_deref() {
                    let _ = app.handle.block_peer(fp);
                    app.set_status(format!("blocked {}", short_fp(fp)));
                } else {
                    app.set_status("can't block — fingerprint not learned yet (try after Identify)");
                }
            }
            Ok(false)
        }
        Action::PeoplePersonUnblock => {
            let blocked = app.handle.list_blocked_peers();
            if let Some(fp) = blocked.get(app.selected_blocked_idx).cloned() {
                let _ = app.handle.unblock_peer(&fp);
                app.set_status(format!("unblocked {}", short_fp(&fp)));
            }
            Ok(false)
        }
        Action::PeoplePersonForget => {
            if let Some(p) = app.known_peers.get(app.selected_known_idx).cloned() {
                let _ = app.handle.forget_peer(&p.address).await;
                app.refresh_known_peers();
            }
            Ok(false)
        }
        Action::PeoplePersonStartDm => {
            if let Some(p) = app.known_peers.get(app.selected_known_idx).cloned() {
                // huddle 0.7.7: use `fingerprint` (populated post-Identify
                // for every dialed peer). Previously this used `label`,
                // which is `None` for plain `d` dials — silently no-op'd.
                if let Some(fp) = p.fingerprint.clone() {
                    match app.handle.start_direct(&fp).await {
                        Ok(rid) => app.switch_to_room(&rid),
                        Err(e) => app.modal = Modal::Error(format!("DM failed: {e}")),
                    }
                } else {
                    app.set_status("can't DM yet — peer hasn't identified (try after they connect)");
                }
            }
            Ok(false)
        }
    }
}

/// huddle 0.7: resolve user-typed compose-DM input to a fingerprint.
/// Accepts:
///   - exact HD-... ID (branded)
///   - bare 24-char hex
///   - any peer username (from `peer_profiles`) — first match wins
///   - any known-peer label
fn resolve_dm_target(app: &TuiApp, input: &str) -> Option<String> {
    let trimmed = input.trim();
    // Reuse the dial-by-id-or-username normalizer for HD/hex paths.
    let normalized = huddle_core::app::normalize_to_fingerprint(trimmed);
    if let Some(fp) = normalized {
        return Some(fp);
    }
    // Username lookup — peer_profiles cache keyed by username.
    for p in &app.known_peers {
        if let Some(label) = &p.label {
            if let Some(name) = app.handle.lookup_username(label) {
                if name.eq_ignore_ascii_case(trimmed) {
                    return Some(label.clone());
                }
            }
        }
    }
    None
}

/// Extract (room_id, file_id, status, encrypted) for the active room's
/// focused card. Returns None when no card is focused / available.
fn focused_card_info(
    app: &TuiApp,
) -> Option<(String, String, huddle_core::storage::repo::AttachmentStatus, bool)> {
    let r = app.active_room()?;
    let a = r.attachments.get(r.focused_card_idx)?;
    Some((r.room_id.clone(), a.file_id.clone(), a.status, a.encrypted))
}

// =========================================================================
// huddle 0.7.7: InvitePicker helpers
// =========================================================================

/// Build the tiered candidate list for the invite picker. Ordering:
///   1. Verified peers — SAS-completed; safest to in-band-DM.
///   2. DM partners — we already have a DM open with them.
///   3. Known peers — dialed at some point; weakest trust signal.
///
/// Dedup is fp-keyed: a peer who's both verified AND a DM partner
/// shows once at the highest tier. We filter out:
///   - our own fingerprint
///   - peers already in the room (no point inviting them)
///   - blocked peers (would silently fail anyway, and confusing UX)
///   - peers we don't have a username/profile cached for AND no
///     fingerprint (can't render or address them)
pub fn gather_invite_candidates(
    app: &TuiApp,
    room_id: &str,
) -> Vec<InviteCandidate> {
    let our_fp = app.handle.fingerprint().to_string();
    let in_room: HashSet<String> = app.handle.room_members(room_id).into_iter().collect();
    let blocked: HashSet<String> = app.handle.list_blocked_peers().into_iter().collect();

    let mut out: Vec<InviteCandidate> = Vec::new();
    let mut seen: HashSet<String> = HashSet::new();
    let push = |list: &mut Vec<InviteCandidate>,
                seen: &mut HashSet<String>,
                fp: String,
                tier: InviteTier,
                username: Option<String>| {
        if seen.insert(fp.clone()) {
            list.push(InviteCandidate {
                fingerprint: fp,
                username,
                tier,
            });
        }
    };

    // Tier 1: Verified (highest trust). lookup_username falls back to None
    // for peers that haven't broadcast a ProfileUpdate yet.
    for fp in app.handle.list_verified_peers() {
        if fp == our_fp || in_room.contains(&fp) || blocked.contains(&fp) {
            continue;
        }
        let name = app.handle.lookup_username(&fp);
        push(&mut out, &mut seen, fp, InviteTier::Verified, name);
    }

    // Tier 2: DM partners. Iterate active rooms; pick out those whose
    // `kind` is Direct, then ask for the other member's fingerprint.
    for rid in app.handle.active_room_ids() {
        let info = match app.handle.active_room_info(&rid) {
            Some(i) => i,
            None => continue,
        };
        if info.kind != huddle_core::storage::repo::RoomKind::Direct {
            continue;
        }
        if let Some(partner) = app.handle.dm_partner_fingerprint(&rid) {
            if partner == our_fp
                || in_room.contains(&partner)
                || blocked.contains(&partner)
            {
                continue;
            }
            let name = app.handle.lookup_username(&partner);
            push(&mut out, &mut seen, partner, InviteTier::DmPartner, name);
        }
    }

    // Tier 3: Known peers with a learned fingerprint. Skip peers
    // we couldn't dedup-skip earlier (already verified or in DM).
    for p in &app.known_peers {
        if let Some(fp) = &p.fingerprint {
            if fp == &our_fp || in_room.contains(fp) || blocked.contains(fp) {
                continue;
            }
            let name = app.handle.lookup_username(fp);
            push(&mut out, &mut seen, fp.clone(), InviteTier::Known, name);
        }
    }

    out
}

/// Apply the picker's filter to its candidate slice. Match is
/// case-insensitive against the cached username AND the short HD-ID
/// prefix. Empty filter returns every candidate in tier order.
/// Returns owned clones so the caller can use it across mutable
/// borrows of the modal state.
pub fn filtered_invite_candidates(state: &InvitePickerState) -> Vec<InviteCandidate> {
    if state.filter.is_empty() {
        return state.candidates.clone();
    }
    let needle = state.filter.to_lowercase();
    state
        .candidates
        .iter()
        .filter(|c| {
            if let Some(u) = &c.username {
                if u.to_lowercase().contains(&needle) {
                    return true;
                }
            }
            let short = crate::ui::short_fp(&c.fingerprint).to_lowercase();
            short.starts_with(&needle) || needle.starts_with("hd-") && {
                let after = needle.trim_start_matches("hd-").to_lowercase();
                short.starts_with(&after)
            }
        })
        .cloned()
        .collect()
}

/// Build a room-scoped invite link the same way `Action::GenerateInvite`
/// does — but reusable so the InvitePicker can call it without
/// duplicating the listen-address / encode dance. Errors when we don't
/// have a listen address yet (transient startup state).
pub fn build_room_invite_link(app: &TuiApp, room_id: &str) -> anyhow::Result<String> {
    use anyhow::anyhow;
    let our_peer = app.handle.peer_id().to_string();
    let our_fp = app.handle.fingerprint().to_string();
    let listen = app
        .listen_addresses
        .first()
        .ok_or_else(|| anyhow!("no listen address yet — try again in a sec"))?
        .clone();
    let host_multiaddr = format!("{}/p2p/{}", listen, our_peer);

    let info = app
        .handle
        .active_room_info(room_id)
        .ok_or_else(|| anyhow!("room not active locally"))?;
    let salt_b64 = info.passphrase_salt.as_ref().map(|s| {
        base64::engine::general_purpose::STANDARD.encode(s)
    });
    let room = huddle_core::invite::InviteRoom {
        id: info.id,
        name: info.name,
        encrypted: info.encrypted,
        salt_b64,
        creator_fingerprint: info.creator_fingerprint,
        owner_fingerprints: app.handle.room_owners(room_id),
    };
    let unsigned = huddle_core::invite::InviteLink {
        v: 1,
        host_multiaddr,
        fingerprint: our_fp,
        room: Some(room),
        creator_pubkey_b64: None,
        signed_at_ms: 0,
        signature_b64: None,
    };
    let invite = app
        .handle
        .sign_invite(unsigned.clone())
        .unwrap_or(unsigned);
    huddle_core::invite::encode(&invite).map_err(|e| anyhow!("encode failed: {e}"))
}

// =========================================================================
// huddle 0.6: command palette
// =========================================================================

/// One entry surfaced by the command palette. `label` is the
/// human-readable description shown to the user; `keys` is the
/// keybinding to display alongside. Confirm dispatches by matching
/// `label`.
#[derive(Debug, Clone, Copy)]
pub struct PaletteEntry {
    pub label: &'static str,
    pub keys: &'static str,
}

/// Static list of "extra" palette entries that don't have a normal
/// keybinding — toggles and settings rows reachable only via the
/// palette.
const EXTRA_PALETTE_ENTRIES: &[PaletteEntry] = &[
    PaletteEntry {
        label: "toggle update check (crates.io)",
        keys: "",
    },
    PaletteEntry {
        label: "dismiss update banner",
        keys: "",
    },
    PaletteEntry {
        label: "clear notification history",
        keys: "Ctrl+H · c",
    },
    PaletteEntry {
        label: "invite peers to room…",
        keys: "Alt+I",
    },
];

/// Build the palette entry list filtered by `query`. Each character
/// of the query must appear in order in the label (subsequence
/// match — the standard "fuzzy" pattern). Empty query returns all.
pub fn palette_filtered(query: &str) -> Vec<PaletteEntry> {
    use crate::keybindings::palette_entries;
    let entries: Vec<PaletteEntry> = palette_entries()
        .map(|(label, keys)| PaletteEntry { label, keys })
        .chain(EXTRA_PALETTE_ENTRIES.iter().copied())
        .collect();
    if query.trim().is_empty() {
        return entries;
    }
    let q_lower: String = query.to_lowercase();
    entries
        .into_iter()
        .filter(|e| fuzzy_match(&e.label.to_lowercase(), &q_lower))
        .collect()
}

fn fuzzy_match(haystack: &str, needle: &str) -> bool {
    let mut it = haystack.chars();
    'outer: for nc in needle.chars() {
        for hc in it.by_ref() {
            if hc == nc {
                continue 'outer;
            }
        }
        return false;
    }
    true
}

/// Dispatch a confirmed palette pick to the actual app action. Each
/// arm mirrors what the corresponding keybinding would do. Some
/// actions are no-ops in some contexts (e.g. "kick member" when not
/// in a room as owner) — they surface a status note instead of
/// throwing an error.
pub async fn run_palette_action(label: &str, app: &mut TuiApp) -> Result<bool> {
    match label {
        // === Lobby-style ===
        "start a new room" => {
            app.modal = Modal::StartRoom(StartRoomState::new());
        }
        "add friend by HD ID or username" => {
            app.modal = Modal::AddFriend(AddFriendState::default());
        }
        "dial peer by address" => {
            app.modal = Modal::DialPeer(DialPeerState::default());
        }
        "show your QR identity" => {
            app.modal = Modal::QrIdentity;
        }
        "open settings" => {
            app.pane = Pane::Settings;
            app.settings_tab = SettingsTab::Account;
            app.sidebar.selection = SidebarItem::Section(SidebarSection::Settings);
        }
        "join with code" => {
            app.set_status("select an encrypted room in the lobby first, then press c");
        }
        "generate invite link" | "generate invite for this room" => {
            return Box::pin(handle_action(Action::GenerateInvite, app)).await;
        }
        "paste invite link" => {
            app.modal = Modal::PasteInvite(PasteInviteState { url: String::new() });
        }
        "mark all rooms read" => {
            return Box::pin(handle_action(Action::MarkAllRead, app)).await;
        }
        "refresh rooms" => {
            app.refresh_discovered();
            app.refresh_known_peers();
            app.set_status("refreshed");
        }
        // === Global ===
        "show help" => {
            app.help_scroll = 0;
            app.modal = Modal::Help;
        }
        "show what's new / onboarding" => {
            let pages: Vec<usize> = (0..ONBOARDING_PAGES.len()).collect();
            app.modal = Modal::Onboarding { pages, cursor: 0 };
        }
        "show notification history" => {
            app.modal = Modal::StatusHistory { scroll: 0 };
        }
        "quit huddle" => {
            app.modal = Modal::QuitConfirm;
        }
        // === Room-context ===
        "switch to next room" => {
            return Box::pin(handle_action(Action::TabNext, app)).await;
        }
        "leave current room" => {
            return Box::pin(handle_action(Action::LeaveRoom, app)).await;
        }
        "back to lobby" => {
            app.pane = Pane::Welcome;
            app.sidebar.focus = SidebarFocus::Sidebar;
        }
        "search room history" => {
            return Box::pin(handle_action(Action::OpenSearch, app)).await;
        }
        "verify members" => {
            return Box::pin(handle_action(Action::OpenVerify, app)).await;
        }
        "rotate room key" => {
            return Box::pin(handle_action(Action::OpenRotateRoom, app)).await;
        }
        "attach a file" => {
            return Box::pin(handle_action(Action::OpenAttachmentPicker, app)).await;
        }
        "toggle room mute" => {
            return Box::pin(handle_action(Action::ToggleMute, app)).await;
        }
        "kick member" => {
            return Box::pin(handle_action(Action::OpenKickPicker, app)).await;
        }
        "grant owner" => {
            return Box::pin(handle_action(Action::OpenGrantPicker, app)).await;
        }
        "toggle verified-only joins" => {
            return Box::pin(handle_action(Action::ToggleRoomVerifiedOnly, app)).await;
        }
        "generate join code" => {
            return Box::pin(handle_action(Action::OpenGenerateJoinCode, app)).await;
        }
        "show room bans" => {
            return Box::pin(handle_action(Action::ShowRoomBans, app)).await;
        }
        // === Extras ===
        "toggle update check (crates.io)" => {
            return Box::pin(handle_action(Action::ToggleUpdateCheck, app)).await;
        }
        "dismiss update banner" => {
            return Box::pin(handle_action(Action::DismissUpdateBanner, app)).await;
        }
        "clear notification history" => {
            return Box::pin(handle_action(Action::ClearStatusHistory, app)).await;
        }
        "invite peers to room…" => {
            return Box::pin(handle_action(Action::OpenInvitePicker, app)).await;
        }
        other => {
            app.set_status(format!("no dispatch for '{}'", other));
        }
    }
    Ok(false)
}

// =========================================================================
// huddle 0.6: update detection (Tier 1 — passive banner, opt-in)
// =========================================================================

/// 24h between crates.io pings. The user opts in once; subsequent
/// launches respect this cache so we don't hammer the API.
const UPDATE_CHECK_INTERVAL_SECS: i64 = 24 * 60 * 60;

/// Spawn a tokio task that — only if the cache is older than 24h —
/// fetches crates.io's `huddle` crate metadata, extracts the
/// `max_stable_version`, and writes it into the app's shared
/// `update_check_slot` if it's newer than `CARGO_PKG_VERSION`. Safe
/// to call multiple times; the cache check serializes work.
pub fn spawn_update_check(app: &TuiApp) {
    let handle = app.handle.clone();
    let slot = app.update_check_slot.clone();
    tokio::spawn(async move {
        let result = tokio::task::spawn_blocking(move || run_update_check(&handle))
            .await
            .ok()
            .flatten();
        if let Some(v) = result {
            if let Ok(mut s) = slot.lock() {
                *s = Some(v);
            }
        }
    });
}

fn run_update_check(handle: &huddle_core::app::AppHandle) -> Option<String> {
    let now = std::time::SystemTime::now()
        .duration_since(std::time::UNIX_EPOCH)
        .ok()?
        .as_secs() as i64;
    let cached_at = handle.last_update_check_at();
    let cached_version = handle.last_known_remote_version();
    if now - cached_at < UPDATE_CHECK_INTERVAL_SECS {
        // Fresh cache: just compare the stored value.
        return cached_version.filter(|v| is_version_newer(v, env!("CARGO_PKG_VERSION")));
    }
    // Stale cache → fetch.
    let body = ureq::get("https://crates.io/api/v1/crates/huddle")
        .set("User-Agent", &format!("huddle/{}", env!("CARGO_PKG_VERSION")))
        .timeout(std::time::Duration::from_secs(10))
        .call()
        .ok()?
        .into_string()
        .ok()?;
    // Extract "max_stable_version":"X.Y.Z" by hand to avoid a
    // serde_json dependency. Crates.io returns this field at the
    // top of the JSON; substring-finding it is robust.
    let version = parse_max_stable_version(&body)?;
    let _ = handle.set_last_update_check_at(now);
    let _ = handle.set_last_known_remote_version(&version);
    if is_version_newer(&version, env!("CARGO_PKG_VERSION")) {
        Some(version)
    } else {
        None
    }
}

/// Extract `"max_stable_version":"X.Y.Z"` from a crates.io API body
/// by substring match. Returns the value, or None if the field is
/// absent / malformed.
fn parse_max_stable_version(body: &str) -> Option<String> {
    let needle = "\"max_stable_version\":\"";
    let start = body.find(needle)? + needle.len();
    let rest = &body[start..];
    let end = rest.find('"')?;
    let v = &rest[..end];
    // Guard against accidentally matching an empty or non-numeric
    // value.
    if v.is_empty() || !v.chars().next().is_some_and(|c| c.is_ascii_digit()) {
        return None;
    }
    Some(v.to_string())
}

/// Reuse the same semver-tuple compare from `parse_semver`. Returns
/// true iff `remote > current`.
fn is_version_newer(remote: &str, current: &str) -> bool {
    parse_semver(remote) > parse_semver(current)
}