gravityfile-tui 0.2.2

Terminal user interface for gravityfile
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
//! Main application state and logic.

mod commands;
mod constants;
mod deletion;
pub mod input;
mod navigation;
mod render;
pub mod state;
mod scanning;

use std::collections::{HashMap, HashSet};
use std::path::PathBuf;
use std::time::Duration;

use crossterm::event::{Event, EventStream};
use futures::StreamExt;
use ratatui::buffer::Buffer;
use ratatui::layout::Rect;
use ratatui::widgets::Widget;
use ratatui::{DefaultTerminal, Frame};
use tokio::sync::mpsc;

use gravityfile_analyze::{AgeReport, DuplicateReport};
use gravityfile_core::FileTree;
use gravityfile_ops::{
    Conflict, CopyOptions, CopyResult, MoveOptions, MoveResult, OperationProgress, UndoLog,
};
use gravityfile_scan::ScanProgress;

use crate::event::KeyAction;
use crate::preview::PreviewState;
use crate::search::SearchState;
use crate::theme::Theme;
use crate::ui::{TreeState, TreeView};
use crate::TuiConfig;

use self::commands::{parse_command, CommandAction, CommandInput, CommandKeyResult, LayoutCommand, SortCommand, ThemeCommand};
use self::constants::{PAGE_SIZE, TICK_INTERVAL_MS};
use self::input::{InputResult, InputState};
use self::render::{render_app, RenderContext};
use self::state::{
    AppMode, ClipboardMode, ClipboardState, DeletionProgress, DuplicatesViewState, LayoutMode,
    PendingOperation, ScanResult, SelectedInfo, SettingsState, SortMode, TabManager, UserSettings, View,
};

/// Application result type.
pub type AppResult<T> = color_eyre::Result<T>;

/// Data collected for synchronizing selection between layout modes.
struct LayoutSyncData {
    /// Current view root path.
    view_path: PathBuf,
    /// Index in miller view (when switching from tree to miller).
    miller_index: Option<usize>,
    /// Index in tree view (when switching from miller to tree).
    tree_index: Option<usize>,
    /// New view root if we need to change it to show the selected item.
    new_view_root: Option<PathBuf>,
}

/// Main application state.
pub struct App {
    /// Path being analyzed (scan root).
    path: PathBuf,
    /// Current view root for drill-down navigation (can be different from scan root).
    view_root: PathBuf,
    /// Navigation history stack for going back up (path, selected_index, expanded_set).
    view_history: Vec<(PathBuf, usize, HashSet<PathBuf>)>,
    /// Forward navigation history stack (for returning after going back).
    forward_history: Vec<(PathBuf, usize, HashSet<PathBuf>)>,
    /// Current mode.
    mode: AppMode,
    /// Current view (normal mode).
    view: View,
    /// Color theme.
    theme: Theme,
    /// Scanned file tree.
    tree: Option<FileTree>,
    /// Tree view state.
    tree_state: TreeState,
    /// Cached count of visible tree items (to avoid flatten on every nav).
    cached_tree_len: usize,
    /// Duplicate analysis report.
    duplicates: Option<DuplicateReport>,
    /// Age analysis report.
    age_report: Option<AgeReport>,
    /// Duplicates view state (expanded groups, selected files).
    duplicates_state: DuplicatesViewState,
    /// Selected stale directory index.
    selected_stale_dir: usize,
    /// Show details panel.
    show_details: bool,
    /// Error message to display.
    error: Option<String>,
    /// Paths marked/selected for operations.
    marked: HashSet<PathBuf>,
    /// Clipboard state for yank/cut/paste.
    clipboard: ClipboardState,
    /// Undo log for reversible operations.
    #[allow(dead_code)]
    undo_log: UndoLog,
    /// Layout mode (tree vs miller).
    layout_mode: LayoutMode,
    /// Miller columns state.
    miller_state: crate::ui::MillerState,
    /// Cached count of entries in current directory for Miller view.
    cached_miller_len: usize,
    /// Input state for rename/create modes.
    input_state: Option<InputState>,
    /// Current file operation progress (copy/move).
    operation_progress: Option<OperationProgress>,
    /// Pending conflict requiring resolution.
    pending_conflict: Option<Conflict>,
    /// Pending operation that needs conflict resolution before proceeding.
    pending_operation: Option<PendingOperation>,
    /// Last operation result message.
    operation_message: Option<(bool, String)>,
    /// Last deletion result message.
    deletion_message: Option<(bool, String)>,
    /// Current deletion progress (during async deletion).
    deletion_progress: Option<DeletionProgress>,
    /// Current scan progress (for display during scanning).
    scan_progress: Option<ScanProgress>,
    /// Channel for receiving scan results.
    scan_rx: Option<mpsc::Receiver<ScanResult>>,
    /// Scan warnings/errors (populated in real-time during scan).
    warnings: Vec<gravityfile_core::ScanWarning>,
    /// Selected warning index.
    selected_warning: usize,
    /// Whether analysis is running.
    analyzing: bool,
    /// Whether a full recursive scan has been completed (vs just quick_list).
    has_full_scan: bool,
    /// Flag indicating UI needs redraw.
    needs_redraw: bool,
    /// Command palette input state.
    command_input: CommandInput,
    /// Current sort mode for file listings.
    sort_mode: SortMode,
    /// Fuzzy search state.
    search_state: SearchState,
    /// Directory tab manager.
    tab_manager: TabManager,
    /// File preview state.
    preview_state: PreviewState,
    /// Whether to auto-scan on startup.
    scan_on_startup: bool,
    /// User settings (persistent configuration).
    user_settings: UserSettings,
    /// Settings modal state.
    settings_state: Option<SettingsState>,
    /// Cached parent tree for Miller columns when at tree root.
    /// This allows showing the parent column even when navigated beyond the original scan root.
    cached_parent_tree: Option<FileTree>,
    /// Pending command that requires terminal suspension (e.g., opening files in editor).
    pending_suspend_command: Option<std::process::Command>,
    /// Cache of scanned directories by path.
    /// This preserves scan data when navigating to parent directories and allows
    /// restoring it when navigating back.
    scanned_cache: HashMap<PathBuf, FileTree>,
}

impl App {
    /// Create a new application with default config.
    /// Immediately loads a quick directory listing for instant display.
    pub fn new(path: PathBuf) -> Self {
        Self::with_config(path, TuiConfig::default())
    }

    /// Create a new application with custom config.
    /// Immediately loads a quick directory listing for instant display,
    /// and optionally starts a full recursive scan based on config.
    pub fn with_config(path: PathBuf, config: TuiConfig) -> Self {
        // Load user settings from disk
        let user_settings = UserSettings::load();

        // Load quick tree immediately for instant display
        let quick_tree = gravityfile_scan::quick_list(&path).ok();

        // Determine scan_on_startup: CLI flag overrides user settings
        // CLI flag (-S) is explicit; if not provided, use user settings
        let scan_on_startup = if config.scan_on_startup {
            true // CLI flag was explicitly set
        } else {
            user_settings.scan_on_startup
        };

        let mut app = Self {
            path: path.clone(),
            view_root: path.clone(),
            view_history: Vec::new(),
            forward_history: Vec::new(),
            mode: AppMode::default(),
            view: View::default(),
            theme: Theme::dark(),
            tree: quick_tree,
            tree_state: TreeState::new(path.clone()),
            cached_tree_len: 0,
            duplicates: None,
            age_report: None,
            duplicates_state: DuplicatesViewState::new(),
            selected_stale_dir: 0,
            show_details: true,
            error: None,
            marked: HashSet::new(),
            clipboard: ClipboardState::default(),
            undo_log: UndoLog::new(100),
            layout_mode: LayoutMode::default(),
            miller_state: crate::ui::MillerState::new(),
            cached_miller_len: 0,
            input_state: None,
            operation_progress: None,
            pending_conflict: None,
            pending_operation: None,
            operation_message: None,
            deletion_message: None,
            deletion_progress: None,
            scan_progress: None,
            scan_rx: None,
            warnings: Vec::new(),
            selected_warning: 0,
            analyzing: false,
            has_full_scan: false,
            needs_redraw: true,
            command_input: CommandInput::new(),
            sort_mode: SortMode::default(),
            search_state: SearchState::new(),
            tab_manager: TabManager::with_initial_tab(path, 10),
            preview_state: PreviewState::new(),
            scan_on_startup,
            user_settings,
            settings_state: None,
            cached_parent_tree: None,
            pending_suspend_command: None,
            scanned_cache: HashMap::new(),
        };

        // Update cached lengths for immediate navigation
        app.update_cached_tree_len();
        app.update_cached_miller_len();
        // Cache parent for Miller columns display
        app.update_cached_parent();

        app
    }

    /// Run the application with async event loop.
    pub async fn run(mut self, mut terminal: DefaultTerminal) -> AppResult<()> {
        // Start scan if configured to do so
        if self.scan_on_startup {
            self.start_scan();
        }
        // Otherwise quick list is already loaded in new() for immediate display

        let period = Duration::from_millis(TICK_INTERVAL_MS);
        let mut interval = tokio::time::interval(period);
        let mut events = EventStream::new();

        while self.mode != AppMode::Quit {
            if self.needs_redraw {
                terminal.draw(|frame| self.render(frame))?;
                self.needs_redraw = false;
            }

            tokio::select! {
                biased;

                Some(Ok(event)) = events.next() => {
                    if let Event::Key(key_event) = event {
                        if key_event.kind == crossterm::event::KeyEventKind::Press {
                            if self.mode == AppMode::Command {
                                self.handle_command_input(key_event);
                            } else if self.mode == AppMode::Search {
                                self.handle_search_input(key_event);
                            } else if matches!(self.mode, AppMode::Renaming | AppMode::CreatingFile | AppMode::CreatingDirectory | AppMode::Taking | AppMode::GoingTo) {
                                self.handle_input_event(key_event);
                            } else if self.mode == AppMode::ConflictResolution {
                                self.handle_conflict_key(key_event);
                            } else {
                                let action = KeyAction::from_key_event(key_event);
                                self.handle_action(action);
                            }
                        }
                    }

                    // Drain any additional pending events
                    while crossterm::event::poll(Duration::ZERO)? {
                        if let Ok(Event::Key(key_event)) = crossterm::event::read() {
                            if key_event.kind == crossterm::event::KeyEventKind::Press {
                                if self.mode == AppMode::Command {
                                    self.handle_command_input(key_event);
                                } else if self.mode == AppMode::Search {
                                    self.handle_search_input(key_event);
                                } else if matches!(self.mode, AppMode::Renaming | AppMode::CreatingFile | AppMode::CreatingDirectory | AppMode::Taking | AppMode::GoingTo) {
                                    self.handle_input_event(key_event);
                                } else if self.mode == AppMode::ConflictResolution {
                                    self.handle_conflict_key(key_event);
                                } else {
                                    let action = KeyAction::from_key_event(key_event);
                                    self.handle_action(action);
                                }
                                if self.mode == AppMode::Quit {
                                    break;
                                }
                            }
                        }
                    }
                    self.needs_redraw = true;
                }

                Some(result) = async {
                    if let Some(rx) = &mut self.scan_rx {
                        rx.recv().await
                    } else {
                        std::future::pending().await
                    }
                } => {
                    self.handle_scan_result(result);
                    self.needs_redraw = true;
                }

                _ = interval.tick() => {
                    // Periodic tick for background updates
                }
            }

            // Handle pending suspend command (opening files in external apps)
            if let Some(mut cmd) = self.pending_suspend_command.take() {
                // Restore terminal before running external command
                ratatui::restore();

                // Run the command and wait for it to complete
                let _ = cmd.status();

                // Reinitialize terminal
                terminal = ratatui::init();
                self.needs_redraw = true;
            }
        }

        Ok(())
    }

    /// Start a background scan of the current view_root directory.
    /// Note: Scanning runs in the background while the app remains in Normal mode.
    /// The UI will detect scanning state via `scan_progress.is_some()`.
    /// If we have a quick tree, it remains visible during scanning.
    fn start_scan(&mut self) {
        // Update the scan root to the current view_root
        self.path = self.view_root.clone();

        self.scan_progress = Some(ScanProgress::new());
        // Don't clear tree - keep quick listing visible during scan
        // self.tree = None;
        self.duplicates = None;
        self.age_report = None;
        self.warnings.clear();
        self.selected_warning = 0;
        // Don't reset cached lengths - keep them for navigation
        // self.cached_tree_len = 0;

        self.scan_rx = Some(scanning::start_scan(self.view_root.clone()));
    }

    /// Handle a scan result from the background task.
    fn handle_scan_result(&mut self, result: ScanResult) {
        match result {
            ScanResult::Progress(progress) => {
                self.scan_progress = Some(progress);
            }
            ScanResult::Warning(warning) => {
                self.warnings.push(warning);
            }
            ScanResult::PartialTree(tree) => {
                // Update tree with partial results during scanning
                // This allows the UI to show progressive updates as directories are scanned
                let root_path = tree.root_path.clone();
                self.tree = Some(tree);

                // Preserve selection position if possible
                if self.tree_state.expanded.is_empty() {
                    self.tree_state = TreeState::new(root_path.clone());
                }
                self.tree_state.expand(&root_path);
                self.update_cached_tree_len();
                self.update_cached_miller_len();

                // Clamp selection to valid range
                if self.tree_state.selected >= self.cached_tree_len && self.cached_tree_len > 0 {
                    self.tree_state.selected = self.cached_tree_len - 1;
                }
            }
            ScanResult::ScanComplete(Ok(tree)) => {
                let root_path = tree.root_path.clone();

                // Merge any final warnings from tree
                for warning in &tree.warnings {
                    if !self.warnings.iter().any(|w| w.path == warning.path) {
                        self.warnings.push(warning.clone());
                    }
                }

                let tree_for_analysis = tree.clone();

                // Cache the scanned tree for later restoration
                self.scanned_cache.insert(root_path.clone(), tree.clone());

                self.tree = Some(tree);

                // Preserve tree state (selection, expanded) across rescans
                // Only reset if this is a different root path
                if self.tree_state.expanded.is_empty()
                    || !self.tree_state.expanded.iter().any(|p| p.starts_with(&root_path))
                {
                    self.tree_state = TreeState::new(root_path);
                } else {
                    // Just ensure root is expanded
                    self.tree_state.expand(&root_path);
                }
                self.update_cached_tree_len();
                self.update_cached_miller_len();

                // Update cached parent tree for Miller columns display
                self.update_cached_parent();

                // Clamp selection to valid range
                if self.tree_state.selected >= self.cached_tree_len && self.cached_tree_len > 0 {
                    self.tree_state.selected = self.cached_tree_len - 1;
                }
                self.mode = AppMode::Normal;
                self.error = None;
                self.scan_progress = None;
                self.has_full_scan = true;

                // Start background analysis
                self.analyzing = true;
                self.scan_rx = Some(scanning::start_analysis(tree_for_analysis));
            }
            ScanResult::ScanComplete(Err(e)) => {
                self.error = Some(e.to_string());
                self.mode = AppMode::Normal;
                self.scan_progress = None;
                self.scan_rx = None;
                self.analyzing = false;
            }
            ScanResult::AnalysisComplete {
                duplicates,
                age_report,
            } => {
                self.duplicates = Some(duplicates);
                self.age_report = Some(age_report);
                self.analyzing = false;
                self.scan_rx = None;
            }
            ScanResult::DeletionProgress(progress) => {
                self.deletion_progress = Some(progress);
            }
            ScanResult::DeletionComplete {
                deleted,
                failed,
                bytes_freed,
            } => {
                self.deletion_progress = None;
                self.scan_rx = None;

                let (success, msg) = deletion::format_deletion_result(deleted, failed, bytes_freed);
                self.deletion_message = Some((success, msg));
                self.mode = AppMode::Normal;

                // Refresh scan after deletion
                self.start_scan();
            }
            ScanResult::OperationProgress(progress) => {
                self.operation_progress = Some(progress);
            }
            ScanResult::OperationConflict(conflict) => {
                self.pending_conflict = Some(conflict);
                self.mode = AppMode::ConflictResolution;
            }
            ScanResult::OperationComplete {
                operation_type,
                succeeded,
                failed,
                bytes_processed: _,
            } => {
                self.operation_progress = None;
                self.scan_rx = None;

                let action = match operation_type {
                    gravityfile_ops::OperationType::Copy => "Copied",
                    gravityfile_ops::OperationType::Move => "Moved",
                    gravityfile_ops::OperationType::Delete => "Deleted",
                    gravityfile_ops::OperationType::Rename => "Renamed",
                    gravityfile_ops::OperationType::CreateFile => "Created file",
                    gravityfile_ops::OperationType::CreateDirectory => "Created directory",
                };

                let success = failed == 0;
                let msg = if success {
                    format!("{} {} items", action, succeeded)
                } else {
                    format!("{} {} items, {} failed", action, succeeded, failed)
                };
                self.operation_message = Some((success, msg));
                self.mode = AppMode::Normal;

                // Refresh scan after operation
                self.start_scan();
            }
        }
    }

    /// Update cached tree item count.
    fn update_cached_tree_len(&mut self) {
        if let Some((node, root_path)) = self.get_view_root_node() {
            let items = TreeView::new(node, &root_path, &self.theme, &self.marked, &self.clipboard).flatten(&self.tree_state);
            self.cached_tree_len = items.len();
        } else {
            self.cached_tree_len = 0;
        }
    }

    /// Update cached miller entry count.
    fn update_cached_miller_len(&mut self) {
        if let Some((node, _)) = self.get_view_root_node() {
            self.cached_miller_len = node.children.len();
        } else {
            self.cached_miller_len = 0;
        }
    }

    /// Synchronize selection state when toggling between Tree and Miller layouts.
    /// Maintains the same selected item across layout switches.
    fn sync_layout_selection(&mut self) {
        // Collect all data needed before mutating self
        let sync_data = self.collect_layout_sync_data();

        let Some(data) = sync_data else {
            // No view node, just toggle and reset
            self.layout_mode = self.layout_mode.toggle();
            if self.layout_mode == LayoutMode::Miller {
                self.miller_state.reset();
                self.update_cached_miller_len();
                self.update_preview();
            } else {
                self.update_cached_tree_len();
                self.preview_state.content = crate::preview::PreviewContent::Empty;
            }
            return;
        };

        match self.layout_mode {
            LayoutMode::Tree => {
                // Switching Tree → Miller
                self.layout_mode = LayoutMode::Miller;

                // If we need to change view_root to show the selected item
                if let Some(new_view_root) = data.new_view_root {
                    self.view_root = new_view_root;
                    self.tree_state.expand(&self.view_root);
                }

                if let Some(miller_idx) = data.miller_index {
                    self.miller_state.selected = miller_idx;
                    self.miller_state.offset = 0;
                    self.update_cached_miller_len();
                    self.miller_state.ensure_visible(20);
                    self.update_preview();
                } else {
                    // Fallback: reset miller state
                    self.miller_state.reset();
                    self.update_cached_miller_len();
                    self.update_preview();
                }
            }
            LayoutMode::Miller => {
                // Switching Miller → Tree
                self.layout_mode = LayoutMode::Tree;
                self.preview_state.content = crate::preview::PreviewContent::Empty;

                // Ensure the view_root is expanded in tree
                self.tree_state.expand(&data.view_path);

                if let Some(tree_idx) = data.tree_index {
                    self.tree_state.selected = tree_idx;
                    self.tree_state.offset = 0;
                    self.tree_state.ensure_visible(20);
                }
                self.update_cached_tree_len();
            }
        }
    }

    /// Collect data needed for layout synchronization without holding borrows.
    fn collect_layout_sync_data(&self) -> Option<LayoutSyncData> {
        let (view_node, view_path) = self.get_view_root_node()?;

        match self.layout_mode {
            LayoutMode::Tree => {
                // Get the currently selected path in tree view
                let items = TreeView::new(view_node, &view_path, &self.theme, &self.marked, &self.clipboard)
                    .flatten(&self.tree_state);

                let selected_path = items.get(self.tree_state.selected).map(|item| item.path.clone());

                let Some(selected) = selected_path else {
                    return Some(LayoutSyncData {
                        view_path: view_path.clone(),
                        miller_index: None,
                        tree_index: None,
                        new_view_root: None,
                    });
                };

                // If we're on the root itself, preserve current miller selection
                if selected == view_path {
                    let miller_index = if self.miller_state.selected < view_node.children.len() {
                        Some(self.miller_state.selected)
                    } else if view_node.children.is_empty() {
                        None
                    } else {
                        Some(0)
                    };
                    return Some(LayoutSyncData {
                        view_path: view_path.clone(),
                        miller_index,
                        tree_index: None,
                        new_view_root: None,
                    });
                }

                // Check if selected is a direct child of view_root
                let is_direct_child = selected.parent() == Some(view_path.as_path());

                if is_direct_child {
                    // Direct child - find it in miller
                    let name = selected.file_name()?.to_string_lossy();
                    let miller_index = view_node.children.iter().position(|c| c.name.as_str() == name);
                    Some(LayoutSyncData {
                        view_path: view_path.clone(),
                        miller_index,
                        tree_index: None,
                        new_view_root: None,
                    })
                } else {
                    // Nested item - need to change view_root to the parent of selected
                    // so that Miller can display the selected item
                    let new_view_root = selected.parent()?.to_path_buf();

                    // Find the selected item's name to locate it in the new view
                    let selected_name = selected.file_name()?.to_string_lossy().to_string();

                    // We need to find the node at new_view_root to get its children
                    let tree = self.tree.as_ref()?;
                    let new_view_node = Self::find_node_at_path(&tree.root, &new_view_root, &tree.root_path)?;
                    let miller_index = new_view_node.children.iter().position(|c| c.name.as_str() == selected_name);

                    Some(LayoutSyncData {
                        view_path: view_path.clone(),
                        miller_index,
                        tree_index: None,
                        new_view_root: Some(new_view_root),
                    })
                }
            }
            LayoutMode::Miller => {
                // Get the target path from miller selection
                let target_path = view_node
                    .children
                    .get(self.miller_state.selected)
                    .map(|c| view_path.join(&*c.name));

                // Use existing tree_state expansion to find the index
                // First ensure view_path is expanded
                let mut tree_state_for_search = self.tree_state.clone();
                tree_state_for_search.expand(&view_path);

                let items = TreeView::new(view_node, &view_path, &self.theme, &self.marked, &self.clipboard)
                    .flatten(&tree_state_for_search);

                let tree_index = target_path.as_ref().and_then(|target| {
                    items.iter().position(|item| &item.path == target)
                });

                Some(LayoutSyncData {
                    view_path: view_path.clone(),
                    miller_index: None,
                    tree_index,
                    new_view_root: None,
                })
            }
        }
    }

    /// Update the file preview for the currently selected item in Miller mode.
    fn update_preview(&mut self) {
        // Only update preview in Miller mode for non-directory items
        if self.layout_mode != LayoutMode::Miller {
            self.preview_state.content = crate::preview::PreviewContent::Empty;
            return;
        }

        let Some((view_node, _)) = self.get_view_root_node() else {
            self.preview_state.content = crate::preview::PreviewContent::Empty;
            return;
        };

        let Some(selected_child) = view_node.children.get(self.miller_state.selected) else {
            self.preview_state.content = crate::preview::PreviewContent::Empty;
            return;
        };

        // Only show file preview for non-directories
        if selected_child.is_dir() {
            // For directories, we show child list directly via miller columns
            self.preview_state.content = crate::preview::PreviewContent::Empty;
            return;
        }

        // Get the full path to the selected file
        let file_path = self.view_root.join(&*selected_child.name);

        // Update preview if path changed
        self.preview_state.update(Some(&file_path));
    }

    /// Render the application.
    fn render(&self, frame: &mut Frame) {
        frame.render_widget(self, frame.area());
    }

    /// Handle a key action.
    fn handle_action(&mut self, action: KeyAction) {
        // Clear deletion message on any action
        self.deletion_message = None;

        // Handle special modes
        match self.mode {
            AppMode::Help => {
                match action {
                    KeyAction::ToggleHelp | KeyAction::Quit | KeyAction::Cancel => {
                        self.mode = AppMode::Normal;
                    }
                    _ => {}
                }
                return;
            }
            AppMode::Settings => {
                self.handle_settings_input(action);
                return;
            }
            AppMode::ConfirmDelete => {
                match action {
                    KeyAction::Confirm | KeyAction::DrillDown => {
                        self.execute_deletion();
                    }
                    KeyAction::Quit | KeyAction::Cancel => {
                        // Clear marks if user cancels (they may have been auto-marked)
                        self.marked.clear();
                        self.mode = AppMode::Normal;
                    }
                    _ => {}
                }
                return;
            }
            AppMode::Deleting => {
                return;
            }
            AppMode::Command => {
                match action {
                    KeyAction::Quit | KeyAction::Cancel => {
                        self.command_input.clear();
                        self.mode = AppMode::Normal;
                    }
                    _ => {}
                }
                return;
            }
            AppMode::ConflictResolution => {
                // Handled in run loop via handle_conflict_key
                return;
            }
            AppMode::Copying | AppMode::Moving => {
                // Operations in progress - ignore most input
                return;
            }
            _ => {}
        }

        match action {
            KeyAction::Quit => {
                self.mode = AppMode::Quit;
            }
            KeyAction::ForceQuit => {
                self.mode = AppMode::Quit;
            }
            KeyAction::Cancel => {
                // Esc clears clipboard first, then marks - does NOT quit
                if !self.clipboard.is_empty() {
                    self.clipboard.clear();
                } else if !self.marked.is_empty() {
                    self.marked.clear();
                }
                // If nothing to clear, Esc does nothing in normal mode
            }

            KeyAction::NextView => {
                self.view = self.view.next();
            }
            KeyAction::PrevView => {
                self.view = self.view.prev();
            }

            // Directory tabs
            KeyAction::NewDirTab => {
                // Open new tab with current view_root
                self.tab_manager.new_tab(self.view_root.clone());
            }
            KeyAction::CloseDirTab => {
                self.tab_manager.close_active_tab();
            }
            KeyAction::NextDirTab => {
                // Only switch tabs if there are multiple
                if self.tab_manager.len() > 1 {
                    self.tab_manager.next_tab();
                    self.sync_from_active_tab();
                }
            }
            KeyAction::PrevDirTab => {
                // Only switch tabs if there are multiple
                if self.tab_manager.len() > 1 {
                    self.tab_manager.prev_tab();
                    self.sync_from_active_tab();
                }
            }
            KeyAction::DirTab(n) => {
                // Only switch tabs if there are multiple
                if self.tab_manager.len() > 1 {
                    self.tab_manager.switch_to_number(n as usize);
                    self.sync_from_active_tab();
                }
            }

            KeyAction::MoveUp => self.move_up(),
            KeyAction::MoveDown => self.move_down(),
            KeyAction::PageUp => self.page_up(),
            KeyAction::PageDown => self.page_down(),
            KeyAction::JumpToTop => self.jump_to_top(),
            KeyAction::JumpToBottom => self.jump_to_bottom(),

            KeyAction::MoveRight | KeyAction::Expand => {
                if self.view == View::Explorer {
                    match self.layout_mode {
                        LayoutMode::Tree => self.expand_selected(),
                        LayoutMode::Miller => self.drill_into_miller_selected(),
                    }
                } else if self.view == View::Duplicates {
                    // l expands group in duplicates view
                    if !self.duplicates_state.is_expanded(self.duplicates_state.selected_group) {
                        self.duplicates_state.toggle_expand();
                    }
                }
            }
            KeyAction::MoveLeft | KeyAction::Collapse => {
                if self.view == View::Explorer {
                    match self.layout_mode {
                        LayoutMode::Tree => self.collapse_selected(),
                        LayoutMode::Miller => self.navigate_back(),
                    }
                } else if self.view == View::Duplicates {
                    // h collapses expanded group in duplicates view
                    if self.duplicates_state.is_expanded(self.duplicates_state.selected_group) {
                        self.duplicates_state.toggle_expand();
                    }
                }
            }
            KeyAction::ToggleExpand => {
                if self.view == View::Explorer && self.layout_mode == LayoutMode::Tree {
                    self.toggle_selected();
                } else if self.view == View::Duplicates {
                    self.duplicates_state.toggle_expand();
                }
            }

            // Selection
            KeyAction::ToggleMark => {
                self.toggle_mark();
            }
            KeyAction::ClearMarks => {
                self.marked.clear();
            }

            // Clipboard operations
            KeyAction::Yank => {
                self.yank_selection();
            }
            KeyAction::Cut => {
                self.cut_selection();
            }
            KeyAction::Paste => {
                self.paste_clipboard();
            }

            // File operations
            KeyAction::Delete => {
                // If nothing marked, auto-mark items for deletion based on current view
                if self.marked.is_empty() {
                    if self.view == View::Duplicates {
                        // In duplicates view, special handling
                        // Collect paths first to avoid borrow issues
                        let paths_to_mark: Vec<PathBuf> = if let Some((filtered, _)) = self.get_filtered_duplicates() {
                            let is_header = !self.duplicates_state.is_expanded(self.duplicates_state.selected_group)
                                || self.duplicates_state.selected_item(self.duplicates_state.selected_group) == 0;

                            if is_header {
                                // Mark all duplicates except the first (the "original")
                                filtered
                                    .get(self.duplicates_state.selected_group)
                                    .map(|g| g.paths.iter().skip(1).cloned().collect())
                                    .unwrap_or_default()
                            } else {
                                // Mark the selected file
                                self.duplicates_state
                                    .selected_file_path(&filtered)
                                    .cloned()
                                    .into_iter()
                                    .collect()
                            }
                        } else {
                            Vec::new()
                        };
                        for path in paths_to_mark {
                            self.marked.insert(path);
                        }
                    } else if self.view == View::Errors {
                        // In Errors view, get the selected warning's path
                        if let Some(warning) = self.warnings.get(self.selected_warning) {
                            // Only allow deleting broken symlinks, not permission errors etc.
                            if warning.kind == gravityfile_core::WarningKind::BrokenSymlink {
                                self.marked.insert(warning.path.clone());
                            }
                        }
                    } else if let Some(path) = self.get_selected_path() {
                        self.marked.insert(path);
                    }
                }
                if !self.marked.is_empty() {
                    self.mode = AppMode::ConfirmDelete;
                }
            }
            KeyAction::Rename => {
                self.start_rename();
            }
            KeyAction::CreateFile => {
                self.start_create_file();
            }
            KeyAction::CreateDirectory => {
                self.start_create_directory();
            }
            KeyAction::Take => {
                self.start_take();
            }
            KeyAction::GoTo => {
                self.start_goto();
            }
            KeyAction::Undo => {
                self.execute_undo();
            }

            KeyAction::Confirm => {
                if !self.marked.is_empty() {
                    self.mode = AppMode::ConfirmDelete;
                } else if self.view == View::Explorer {
                    self.toggle_selected();
                }
            }

            KeyAction::DrillDown | KeyAction::OpenFile => {
                if self.view == View::Explorer {
                    // Check if selected item is a file or directory
                    let is_file = self.is_selected_file();
                    if is_file {
                        self.open_selected_file();
                    } else {
                        match self.layout_mode {
                            LayoutMode::Tree => self.drill_into_selected(),
                            LayoutMode::Miller => self.drill_into_miller_selected(),
                        }
                    }
                }
            }
            KeyAction::NavigateBack => {
                if self.view == View::Explorer {
                    self.navigate_back();
                }
            }

            KeyAction::CommandMode => {
                self.command_input.clear();
                self.mode = AppMode::Command;
            }

            KeyAction::ToggleDetails => {
                self.show_details = !self.show_details;
            }
            KeyAction::ToggleHelp => {
                self.mode = AppMode::Help;
            }
            KeyAction::ToggleTheme => {
                self.theme = self.theme.toggle();
            }
            KeyAction::ToggleLayout => {
                self.sync_layout_selection();
            }
            KeyAction::CyclePreviewMode => {
                self.preview_state.cycle_mode();
            }
            KeyAction::OpenSettings => {
                self.settings_state = Some(SettingsState::new(self.user_settings.clone()));
                self.mode = AppMode::Settings;
            }
            KeyAction::Refresh => {
                self.marked.clear();
                self.start_scan();
            }
            KeyAction::Sort => {
                self.cycle_sort_mode();
            }
            KeyAction::ReverseSort => {
                self.reverse_sort_mode();
            }
            KeyAction::Search => {
                self.start_search();
            }

            _ => {}
        }
    }

    /// Toggle marking the currently selected item.
    fn toggle_mark(&mut self) {
        let path = match self.view {
            View::Explorer => {
                if let Some((node, root_path)) = self.get_view_root_node() {
                    match self.layout_mode {
                        LayoutMode::Tree => {
                            let items =
                                TreeView::new(node, &root_path, &self.theme, &self.marked, &self.clipboard).flatten(&self.tree_state);
                            items.get(self.tree_state.selected).map(|i| i.path.clone())
                        }
                        LayoutMode::Miller => {
                            node.children.get(self.miller_state.selected).map(|child| {
                                self.view_root.join(&*child.name)
                            })
                        }
                    }
                } else {
                    None
                }
            }
            View::Duplicates => {
                // Collect data first to avoid borrow issues
                let (is_on_header, group_paths, selected_file) = if let Some((filtered, _)) = self.get_filtered_duplicates() {
                    let is_on_header = !self.duplicates_state.is_expanded(self.duplicates_state.selected_group)
                        || self.duplicates_state.selected_item(self.duplicates_state.selected_group) == 0;

                    let group_paths: Vec<PathBuf> = filtered
                        .get(self.duplicates_state.selected_group)
                        .map(|g| g.paths.clone())
                        .unwrap_or_default();

                    let selected_file = if !is_on_header {
                        self.duplicates_state.selected_file_path(&filtered).cloned()
                    } else {
                        None
                    };

                    (is_on_header, group_paths, selected_file)
                } else {
                    return;
                };

                if is_on_header {
                    // Header selected - toggle ALL files in the group
                    let any_marked = group_paths.iter().any(|p| self.marked.contains(p));

                    if any_marked {
                        // Unmark all files in the group
                        for path in &group_paths {
                            self.marked.remove(path);
                        }
                    } else {
                        // Mark all files in the group
                        for path in group_paths {
                            if path != self.path {
                                self.marked.insert(path);
                            }
                        }
                    }
                    return; // Early return since we handled it here
                } else {
                    // Specific file selected within expanded group
                    selected_file
                }
            }
            View::Age => {
                if let Some(filtered) = self.get_filtered_stale_dirs() {
                    let selected = self.selected_stale_dir.min(filtered.len().saturating_sub(1));
                    filtered.get(selected).map(|d| d.path.clone())
                } else {
                    None
                }
            }
            View::Errors => {
                // Only allow marking broken symlinks for deletion
                if let Some(warning) = self.warnings.get(self.selected_warning) {
                    if warning.kind == gravityfile_core::WarningKind::BrokenSymlink {
                        Some(warning.path.clone())
                    } else {
                        None
                    }
                } else {
                    None
                }
            }
        };

        if let Some(path) = path {
            if path != self.path {
                if self.marked.contains(&path) {
                    self.marked.remove(&path);
                } else {
                    self.marked.insert(path);
                }
            }
        }
    }

    /// Execute deletion of all marked items asynchronously.
    fn execute_deletion(&mut self) {
        let paths_with_sizes: Vec<(PathBuf, u64)> = self
            .marked
            .iter()
            .map(|p| (p.clone(), self.get_path_size(p).unwrap_or(0)))
            .collect();

        let total = paths_with_sizes.len();
        if total == 0 {
            return;
        }

        self.marked.clear();
        self.mode = AppMode::Deleting;
        self.deletion_progress = Some(DeletionProgress {
            total,
            deleted: 0,
            failed: 0,
            bytes_freed: 0,
            current: paths_with_sizes.first().map(|(p, _)| p.clone()),
        });

        self.scan_rx = Some(deletion::start_deletion(paths_with_sizes));
    }

    /// Get the size of a path from the tree.
    fn get_path_size(&self, target: &PathBuf) -> Option<u64> {
        let tree = self.tree.as_ref()?;

        fn find_size(
            node: &gravityfile_core::FileNode,
            target: &PathBuf,
            current: &PathBuf,
        ) -> Option<u64> {
            if current == target {
                return Some(node.size);
            }
            for child in &node.children {
                let child_path = current.join(&*child.name);
                if let Some(size) = find_size(child, target, &child_path) {
                    return Some(size);
                }
            }
            None
        }

        find_size(&tree.root, target, &tree.root_path)
    }

    /// Yank (copy) selected items to clipboard.
    fn yank_selection(&mut self) {
        let paths: Vec<PathBuf> = if self.marked.is_empty() {
            self.get_selected_path().into_iter().collect()
        } else {
            self.marked.iter().cloned().collect()
        };

        if !paths.is_empty() {
            self.clipboard.yank(paths, self.view_root.clone());
            self.marked.clear();
        }
    }

    /// Cut (move) selected items to clipboard.
    fn cut_selection(&mut self) {
        let paths: Vec<PathBuf> = if self.marked.is_empty() {
            self.get_selected_path().into_iter().collect()
        } else {
            self.marked.iter().cloned().collect()
        };

        if !paths.is_empty() {
            self.clipboard.cut(paths, self.view_root.clone());
            self.marked.clear();
        }
    }

    /// Paste from clipboard to appropriate destination.
    /// - If cursor is on a directory → paste INTO that directory
    /// - If cursor is on a file → paste into the same parent directory
    fn paste_clipboard(&mut self) {
        self.paste_with_resolution(None);
    }

    /// Paste with a specific conflict resolution (or None to check for conflicts).
    fn paste_with_resolution(&mut self, resolution: Option<gravityfile_ops::ConflictResolution>) {
        if self.clipboard.is_empty() {
            return;
        }

        // Determine destination based on current selection
        let destination = self.get_paste_destination();

        let sources = self.clipboard.paths.clone();
        let mode = self.clipboard.mode;

        // Pre-flight conflict detection (only if no resolution provided)
        if resolution.is_none() {
            if let Some(conflict) = self.check_paste_conflict(&sources, &destination) {
                // Store the pending operation and show conflict modal
                self.pending_operation = Some(PendingOperation::Paste {
                    sources: sources.clone(),
                    destination: destination.clone(),
                    mode,
                });
                self.pending_conflict = Some(conflict);
                self.mode = AppMode::ConflictResolution;
                return;
            }
        }

        match mode {
            ClipboardMode::Copy => {
                self.mode = AppMode::Copying;
                let options = CopyOptions {
                    conflict_resolution: resolution,
                    preserve_timestamps: false,
                };
                let rx = gravityfile_ops::start_copy(sources, destination, options);
                self.scan_rx = Some(Self::adapt_copy_rx(rx));
            }
            ClipboardMode::Cut => {
                self.mode = AppMode::Moving;
                let options = MoveOptions {
                    conflict_resolution: resolution,
                };
                let rx = gravityfile_ops::start_move(sources, destination, options);
                self.scan_rx = Some(Self::adapt_move_rx(rx));
                self.clipboard.clear();
            }
            ClipboardMode::Empty => {}
        }
    }

    /// Check if pasting would cause a conflict with an existing file.
    fn check_paste_conflict(&self, sources: &[PathBuf], destination: &PathBuf) -> Option<Conflict> {
        use gravityfile_ops::{ConflictKind};

        for source in sources {
            if let Some(filename) = source.file_name() {
                let dest_path = destination.join(filename);

                // Check if destination already exists
                if dest_path.exists() {
                    // Determine conflict kind
                    let kind = if source == &dest_path {
                        // Same file - source and destination are identical
                        ConflictKind::SameFile
                    } else if dest_path.is_dir() {
                        ConflictKind::DirectoryExists
                    } else {
                        ConflictKind::FileExists
                    };

                    return Some(Conflict::new(
                        source.clone(),
                        dest_path,
                        kind,
                    ));
                }
            }
        }
        None
    }

    /// Get the appropriate paste destination based on current selection.
    /// - If selected item is a directory → return that directory
    /// - If selected item is a file → return the parent directory
    /// - If nothing selected → return view_root
    fn get_paste_destination(&self) -> PathBuf {
        // Try to get the currently selected item
        if let Some(selected_path) = self.get_selected_path() {
            // Check if the selected item is a directory
            if selected_path.is_dir() {
                return selected_path;
            } else {
                // It's a file, use its parent directory
                if let Some(parent) = selected_path.parent() {
                    return parent.to_path_buf();
                }
            }
        }
        // Default to view_root if nothing selected
        self.view_root.clone()
    }

    /// Adapt copy result channel to ScanResult channel.
    fn adapt_copy_rx(mut rx: mpsc::Receiver<CopyResult>) -> mpsc::Receiver<ScanResult> {
        let (tx, result_rx) = mpsc::channel(100);
        tokio::spawn(async move {
            while let Some(result) = rx.recv().await {
                let scan_result = match result {
                    CopyResult::Progress(p) => ScanResult::OperationProgress(p),
                    CopyResult::Conflict(c) => ScanResult::OperationConflict(c),
                    CopyResult::Complete(c) => ScanResult::OperationComplete {
                        operation_type: c.operation_type,
                        succeeded: c.succeeded,
                        failed: c.failed,
                        bytes_processed: c.bytes_processed,
                    },
                };
                if tx.send(scan_result).await.is_err() {
                    break;
                }
            }
        });
        result_rx
    }

    /// Adapt move result channel to ScanResult channel.
    fn adapt_move_rx(mut rx: mpsc::Receiver<MoveResult>) -> mpsc::Receiver<ScanResult> {
        let (tx, result_rx) = mpsc::channel(100);
        tokio::spawn(async move {
            while let Some(result) = rx.recv().await {
                let scan_result = match result {
                    MoveResult::Progress(p) => ScanResult::OperationProgress(p),
                    MoveResult::Conflict(c) => ScanResult::OperationConflict(c),
                    MoveResult::Complete(c) => ScanResult::OperationComplete {
                        operation_type: c.operation_type,
                        succeeded: c.succeeded,
                        failed: c.failed,
                        bytes_processed: c.bytes_processed,
                    },
                };
                if tx.send(scan_result).await.is_err() {
                    break;
                }
            }
        });
        result_rx
    }

    /// Handle key press during conflict resolution mode.
    fn handle_conflict_key(&mut self, event: crossterm::event::KeyEvent) {
        use crossterm::event::KeyCode;
        use gravityfile_ops::ConflictResolution;

        // Check if this is a SameFile conflict - only Skip and Abort are valid
        let is_same_file = self.pending_conflict
            .as_ref()
            .map(|c| matches!(c.kind, gravityfile_ops::ConflictKind::SameFile))
            .unwrap_or(false);

        let resolution = match event.code {
            // Skip
            KeyCode::Char('s') => Some(ConflictResolution::Skip),
            // Skip All (not valid for same file)
            KeyCode::Char('S') if !is_same_file => Some(ConflictResolution::SkipAll),
            // Overwrite (not valid for same file)
            KeyCode::Char('o') if !is_same_file => Some(ConflictResolution::Overwrite),
            // Overwrite All (not valid for same file)
            KeyCode::Char('O') if !is_same_file => Some(ConflictResolution::OverwriteAll),
            // Rename (auto-rename)
            KeyCode::Char('r') => Some(ConflictResolution::AutoRename),
            // Abort/Cancel
            KeyCode::Esc => Some(ConflictResolution::Abort),
            // Enter also acts as skip for same-file conflicts
            KeyCode::Enter if is_same_file => Some(ConflictResolution::Skip),
            // For SourceIsAncestor, Enter also acknowledges
            KeyCode::Enter => {
                if let Some(conflict) = &self.pending_conflict {
                    if matches!(conflict.kind, gravityfile_ops::ConflictKind::SourceIsAncestor) {
                        Some(ConflictResolution::Abort)
                    } else {
                        None
                    }
                } else {
                    None
                }
            }
            _ => None,
        };

        if let Some(res) = resolution {
            // Clear the pending conflict
            self.pending_conflict = None;

            // Take the pending operation
            let pending = self.pending_operation.take();

            // If aborting or skipping, cancel the operation and return to normal mode
            if matches!(res, ConflictResolution::Abort | ConflictResolution::Skip | ConflictResolution::SkipAll) {
                self.mode = AppMode::Normal;
                self.scan_rx = None;
                self.operation_progress = None;
                // Keep clipboard for Abort (user might want to try elsewhere)
                // Clear for Skip only if it was a cut operation that we're fully canceling
                return;
            }

            // Resume the pending operation with the chosen resolution
            if let Some(pending_op) = pending {
                match pending_op {
                    PendingOperation::Paste { sources, destination: _, mode } => {
                        // Temporarily set clipboard to resume the paste
                        let old_clipboard = self.clipboard.clone();
                        self.clipboard.paths = sources;
                        self.clipboard.mode = mode;

                        // Execute paste with the chosen resolution
                        self.paste_with_resolution(Some(res));

                        // For Copy mode, restore clipboard after paste starts
                        // For Cut mode, the paste_with_resolution already clears it
                        if mode == ClipboardMode::Copy {
                            self.clipboard = old_clipboard;
                        }
                    }
                }
            } else {
                // No pending operation, just return to normal
                self.mode = AppMode::Normal;
                self.scan_rx = None;
                self.operation_progress = None;
            }
        }
    }

    /// Start rename operation for current selection.
    fn start_rename(&mut self) {
        if let Some(path) = self.get_selected_path() {
            let name = path
                .file_name()
                .map(|n| n.to_string_lossy().to_string())
                .unwrap_or_default();
            let mut input = InputState::with_initial(&name);
            input.context_path = Some(path);
            self.input_state = Some(input);
            self.mode = AppMode::Renaming;
        }
    }

    /// Start create file operation.
    fn start_create_file(&mut self) {
        let mut input = InputState::new();
        input.context_path = Some(self.view_root.clone());
        self.input_state = Some(input);
        self.mode = AppMode::CreatingFile;
    }

    /// Start create directory operation.
    fn start_create_directory(&mut self) {
        let mut input = InputState::new();
        input.context_path = Some(self.view_root.clone());
        self.input_state = Some(input);
        self.mode = AppMode::CreatingDirectory;
    }

    /// Start take operation (create directory and cd into it).
    fn start_take(&mut self) {
        let mut input = InputState::new();
        input.context_path = Some(self.view_root.clone());
        self.input_state = Some(input);
        self.mode = AppMode::Taking;
    }

    /// Start go to directory operation.
    fn start_goto(&mut self) {
        // Pre-fill with current view root for convenience
        let current_path = self.view_root.to_string_lossy().to_string();
        let mut input = InputState::with_initial(&current_path);
        input.context_path = Some(self.view_root.clone());
        self.input_state = Some(input);
        self.mode = AppMode::GoingTo;
    }

    /// Execute go to directory.
    fn execute_goto(&mut self, path_str: &str) {
        let path = if path_str.starts_with('~') {
            // Expand tilde
            if let Some(home) = dirs::home_dir() {
                home.join(&path_str[1..].trim_start_matches('/'))
            } else {
                std::path::PathBuf::from(path_str)
            }
        } else if path_str.starts_with('/') {
            std::path::PathBuf::from(path_str)
        } else {
            // Relative path from current view root
            self.view_root.join(path_str)
        };

        // Canonicalize to resolve .. and .
        let path = path.canonicalize().unwrap_or(path);

        if path.is_dir() {
            // Check if target path is within our already-scanned tree
            let within_scanned_tree = if let Some(tree) = &self.tree {
                path.starts_with(&tree.root_path) || tree.root_path.starts_with(&path)
            } else {
                false
            };

            if within_scanned_tree {
                // Navigate within the existing tree - no rescan needed
                self.view_root = path.clone();
                self.view_history.clear();
                self.forward_history.clear();
                self.tree_state.selected = 0;
                self.tree_state.offset = 0;
                self.tree_state.expand(&self.view_root);
                self.miller_state.reset();
                self.update_cached_tree_len();
                self.update_cached_miller_len();
                self.preview_state.content = crate::preview::PreviewContent::Empty;

                // Update the active tab view_root but keep the tree
                if let Some(tab) = self.tab_manager.active_tab_mut() {
                    tab.view_root = self.view_root.clone();
                }
            } else {
                // Going to a new location - use quick_list for immediate display
                // Don't auto-scan; user can press R to scan if needed
                self.path = path.clone();
                self.view_root = path.clone();
                self.view_history.clear();
                self.forward_history.clear();
                self.tree_state = TreeState::new(self.path.clone());
                self.miller_state.reset();
                self.marked.clear();
                self.clipboard.clear();
                self.duplicates = None;
                self.age_report = None;
                self.warnings.clear();
                self.preview_state.content = crate::preview::PreviewContent::Empty;
                self.scan_progress = None;

                // Load quick tree for immediate display
                self.tree = gravityfile_scan::quick_list(&path).ok();
                self.update_cached_tree_len();
                self.update_cached_miller_len();

                // Update the active tab
                if let Some(tab) = self.tab_manager.active_tab_mut() {
                    tab.path = self.path.clone();
                    tab.view_root = self.view_root.clone();
                    tab.label = self.path
                        .file_name()
                        .map(|n| n.to_string_lossy().to_string())
                        .unwrap_or_else(|| self.path.to_string_lossy().to_string());
                    tab.history.clear();
                }

                // Don't auto-start scan - user can press R when ready
            }
        } else {
            self.error = Some(format!("Not a directory: {}", path.display()));
        }
    }

    /// Execute undo of the last operation.
    fn execute_undo(&mut self) {
        // TODO: Implement undo
        // For now, just show a message that undo is not yet implemented
        self.operation_message = Some((false, "Undo not yet implemented".to_string()));
    }

    /// Handle key events in input modes (Renaming, CreatingFile, CreatingDirectory).
    fn handle_input_event(&mut self, key: crossterm::event::KeyEvent) {
        let current_mode = self.mode;

        if let Some(ref mut input) = self.input_state {
            match input.handle_key(key) {
                InputResult::Continue => {}
                InputResult::Cancel => {
                    self.input_state = None;
                    self.mode = AppMode::Normal;
                }
                InputResult::Submit(value) => {
                    let context_path = input.context_path.clone();
                    self.input_state = None;

                    match current_mode {
                        AppMode::Renaming => {
                            if let Some(source) = context_path {
                                self.execute_rename(source, value);
                            }
                        }
                        AppMode::CreatingFile => {
                            if let Some(parent) = context_path {
                                self.execute_create_file(parent, value);
                            }
                        }
                        AppMode::CreatingDirectory => {
                            if let Some(parent) = context_path {
                                self.execute_create_directory(parent, value);
                            }
                        }
                        AppMode::Taking => {
                            if let Some(parent) = context_path {
                                self.execute_take(parent, value);
                            }
                        }
                        AppMode::GoingTo => {
                            self.execute_goto(&value);
                        }
                        _ => {}
                    }

                    self.mode = AppMode::Normal;
                }
            }
        } else {
            // No input state, just cancel
            self.mode = AppMode::Normal;
        }
    }

    /// Execute rename operation.
    fn execute_rename(&mut self, source: std::path::PathBuf, new_name: String) {
        use gravityfile_ops::start_rename;

        // Validate name
        if new_name.is_empty() || new_name.contains('/') || new_name.contains('\\') {
            self.operation_message = Some((false, "Invalid file name".to_string()));
            return;
        }

        let rx = start_rename(source.clone(), new_name.clone());

        // Adapt the rename result receiver
        let adapted_rx = Self::adapt_rename_rx(rx);
        self.scan_rx = Some(adapted_rx);
        self.mode = AppMode::Normal;

        // Show pending message
        self.operation_message = Some((true, format!("Renaming to {}...", new_name)));
    }

    /// Adapt rename receiver to ScanResult.
    fn adapt_rename_rx(
        mut rx: mpsc::Receiver<gravityfile_ops::RenameResult>,
    ) -> mpsc::Receiver<ScanResult> {
        let (tx, adapted_rx) = mpsc::channel(100);

        tokio::spawn(async move {
            while let Some(result) = rx.recv().await {
                let scan_result = match result {
                    gravityfile_ops::RenameResult::Progress(progress) => {
                        ScanResult::OperationProgress(progress)
                    }
                    gravityfile_ops::RenameResult::Complete(complete) => {
                        ScanResult::OperationComplete {
                            operation_type: complete.operation_type,
                            succeeded: complete.succeeded,
                            failed: complete.failed,
                            bytes_processed: complete.bytes_processed,
                        }
                    }
                };
                if tx.send(scan_result).await.is_err() {
                    break;
                }
            }
        });

        adapted_rx
    }

    /// Execute create file operation.
    fn execute_create_file(&mut self, parent: std::path::PathBuf, name: String) {
        use gravityfile_ops::start_create_file;

        // Validate name
        if name.is_empty() || name.contains('/') || name.contains('\\') {
            self.operation_message = Some((false, "Invalid file name".to_string()));
            return;
        }

        let path = parent.join(&name);
        let rx = start_create_file(path.clone());

        let adapted_rx = Self::adapt_create_rx(rx);
        self.scan_rx = Some(adapted_rx);

        self.operation_message = Some((true, format!("Creating file {}...", name)));
    }

    /// Execute create directory operation.
    fn execute_create_directory(&mut self, parent: std::path::PathBuf, name: String) {
        use gravityfile_ops::start_create_directory;

        // Validate name
        if name.is_empty() || name.contains('/') || name.contains('\\') {
            self.operation_message = Some((false, "Invalid directory name".to_string()));
            return;
        }

        let path = parent.join(&name);
        let rx = start_create_directory(path.clone());

        let adapted_rx = Self::adapt_create_rx(rx);
        self.scan_rx = Some(adapted_rx);

        self.operation_message = Some((true, format!("Creating directory {}...", name)));
    }

    /// Execute take operation (create directory and navigate into it).
    fn execute_take(&mut self, parent: std::path::PathBuf, name: String) {
        // Validate name
        if name.is_empty() || name.contains('/') || name.contains('\\') {
            self.operation_message = Some((false, "Invalid directory name".to_string()));
            return;
        }

        let path = parent.join(&name);

        // Check if already exists
        if path.exists() {
            if path.is_dir() {
                // Directory exists - just navigate into it
                self.operation_message = Some((true, format!("Directory {} already exists, navigating...", name)));
            } else {
                // File exists with same name
                self.operation_message = Some((false, format!("A file named {} already exists", name)));
                return;
            }
        } else {
            // Create the directory synchronously (it's a simple operation)
            if let Err(e) = std::fs::create_dir(&path) {
                self.operation_message = Some((false, format!("Failed to create directory: {}", e)));
                return;
            }
            self.operation_message = Some((true, format!("Created and entered {}", name)));
        }

        // Navigate into the new directory
        // Save current state to history
        let saved_expanded = self.tree_state.expanded.clone();
        let saved_selected = match self.layout_mode {
            LayoutMode::Tree => self.tree_state.selected,
            LayoutMode::Miller => self.miller_state.selected,
        };
        self.view_history.push((self.view_root.clone(), saved_selected, saved_expanded));

        // Update view root to the new directory
        self.view_root = path;

        // Reset state for new directory
        match self.layout_mode {
            LayoutMode::Tree => {
                self.tree_state.selected = 0;
                self.tree_state.offset = 0;
                self.tree_state.expand(&self.view_root);
            }
            LayoutMode::Miller => {
                self.miller_state.reset();
            }
        }

        self.sync_to_active_tab();

        // Refresh to show new directory in tree
        self.start_scan();
    }

    /// Adapt create result receiver to ScanResult.
    fn adapt_create_rx(
        mut rx: mpsc::Receiver<gravityfile_ops::CreateResult>,
    ) -> mpsc::Receiver<ScanResult> {
        let (tx, adapted_rx) = mpsc::channel(100);

        tokio::spawn(async move {
            while let Some(result) = rx.recv().await {
                let scan_result = match result {
                    gravityfile_ops::CreateResult::Progress(progress) => {
                        ScanResult::OperationProgress(progress)
                    }
                    gravityfile_ops::CreateResult::Complete(complete) => {
                        ScanResult::OperationComplete {
                            operation_type: complete.operation_type,
                            succeeded: complete.succeeded,
                            failed: complete.failed,
                            bytes_processed: complete.bytes_processed,
                        }
                    }
                };
                if tx.send(scan_result).await.is_err() {
                    break;
                }
            }
        });

        adapted_rx
    }

    // Navigation methods
    fn move_up(&mut self) {
        match self.view {
            View::Explorer => match self.layout_mode {
                LayoutMode::Tree => self.tree_state.move_up(1),
                LayoutMode::Miller => {
                    self.miller_state.move_up(1);
                    self.update_preview();
                }
            },
            View::Duplicates => {
                // Get counts first to avoid borrow issues
                let (group_count, file_counts) = if let Some((filtered, _)) = self.get_filtered_duplicates() {
                    let counts: Vec<usize> = filtered.iter().map(|g| g.paths.len()).collect();
                    (filtered.len(), counts)
                } else {
                    (0, Vec::new())
                };
                if group_count > 0 {
                    self.duplicates_state.move_up(group_count, |idx| {
                        file_counts.get(idx).copied().unwrap_or(0)
                    });
                }
            }
            View::Age => {
                self.selected_stale_dir = self.selected_stale_dir.saturating_sub(1);
            }
            View::Errors => {
                self.selected_warning = self.selected_warning.saturating_sub(1);
            }
        }
    }

    fn move_down(&mut self) {
        match self.view {
            View::Explorer => match self.layout_mode {
                LayoutMode::Tree => {
                    self.tree_state.move_down(1, self.cached_tree_len);
                }
                LayoutMode::Miller => {
                    self.miller_state.move_down(1, self.cached_miller_len);
                    self.update_preview();
                }
            },
            View::Duplicates => {
                // Get counts first to avoid borrow issues
                let (group_count, file_counts) = if let Some((filtered, _)) = self.get_filtered_duplicates() {
                    let counts: Vec<usize> = filtered.iter().map(|g| g.paths.len()).collect();
                    (filtered.len(), counts)
                } else {
                    (0, Vec::new())
                };
                if group_count > 0 {
                    self.duplicates_state.move_down(group_count, |idx| {
                        file_counts.get(idx).copied().unwrap_or(0)
                    });
                }
            }
            View::Age => {
                if let Some(filtered) = self.get_filtered_stale_dirs() {
                    let max = filtered.len().saturating_sub(1);
                    self.selected_stale_dir = (self.selected_stale_dir + 1).min(max);
                }
            }
            View::Errors => {
                let max = self.warnings.len().saturating_sub(1);
                self.selected_warning = (self.selected_warning + 1).min(max);
            }
        }
    }

    fn page_up(&mut self) {
        match self.view {
            View::Explorer => match self.layout_mode {
                LayoutMode::Tree => self.tree_state.move_up(PAGE_SIZE),
                LayoutMode::Miller => {
                    self.miller_state.move_up(PAGE_SIZE);
                    self.update_preview();
                }
            },
            View::Duplicates => {
                // Page up - move multiple groups up
                self.duplicates_state.selected_group =
                    self.duplicates_state.selected_group.saturating_sub(PAGE_SIZE);
            }
            View::Age => {
                self.selected_stale_dir = self.selected_stale_dir.saturating_sub(PAGE_SIZE);
            }
            View::Errors => {
                self.selected_warning = self.selected_warning.saturating_sub(PAGE_SIZE);
            }
        }
    }

    fn page_down(&mut self) {
        match self.view {
            View::Explorer => match self.layout_mode {
                LayoutMode::Tree => {
                    self.tree_state.move_down(PAGE_SIZE, self.cached_tree_len);
                }
                LayoutMode::Miller => {
                    self.miller_state.move_down(PAGE_SIZE, self.cached_miller_len);
                    self.update_preview();
                }
            },
            View::Duplicates => {
                // Page down - move multiple groups down
                if let Some((filtered, _)) = self.get_filtered_duplicates() {
                    let max = filtered.len().saturating_sub(1);
                    self.duplicates_state.selected_group =
                        (self.duplicates_state.selected_group + PAGE_SIZE).min(max);
                }
            }
            View::Age => {
                if let Some(filtered) = self.get_filtered_stale_dirs() {
                    let max = filtered.len().saturating_sub(1);
                    self.selected_stale_dir = (self.selected_stale_dir + PAGE_SIZE).min(max);
                }
            }
            View::Errors => {
                let max = self.warnings.len().saturating_sub(1);
                self.selected_warning = (self.selected_warning + PAGE_SIZE).min(max);
            }
        }
    }

    fn jump_to_top(&mut self) {
        match self.view {
            View::Explorer => match self.layout_mode {
                LayoutMode::Tree => self.tree_state.jump_to_top(),
                LayoutMode::Miller => {
                    self.miller_state.jump_to_top();
                    self.update_preview();
                }
            },
            View::Duplicates => self.duplicates_state.reset(),
            View::Age => self.selected_stale_dir = 0,
            View::Errors => self.selected_warning = 0,
        }
    }

    fn jump_to_bottom(&mut self) {
        match self.view {
            View::Explorer => match self.layout_mode {
                LayoutMode::Tree => {
                    self.tree_state.jump_to_bottom(self.cached_tree_len);
                }
                LayoutMode::Miller => {
                    self.miller_state.jump_to_bottom(self.cached_miller_len);
                    self.update_preview();
                }
            },
            View::Duplicates => {
                if let Some((filtered, _)) = self.get_filtered_duplicates() {
                    self.duplicates_state.selected_group = filtered.len().saturating_sub(1);
                }
            }
            View::Age => {
                if let Some(filtered) = self.get_filtered_stale_dirs() {
                    self.selected_stale_dir = filtered.len().saturating_sub(1);
                }
            }
            View::Errors => {
                self.selected_warning = self.warnings.len().saturating_sub(1);
            }
        }
    }

    fn expand_selected(&mut self) {
        if let Some((node, root_path)) = self.get_view_root_node() {
            let items = TreeView::new(node, &root_path, &self.theme, &self.marked, &self.clipboard).flatten(&self.tree_state);
            if let Some(item) = items.get(self.tree_state.selected) {
                // Lazy load directory contents if directory has no children
                if matches!(item.node.kind, crate::ui::VisibleNodeKind::Directory { .. }) {
                    let needs_lazy_load = if let Some(tree) = &self.tree {
                        if let Some(node) = Self::find_node_at_path(&tree.root, &item.path, &tree.root_path) {
                            node.children.is_empty()
                        } else {
                            false
                        }
                    } else {
                        false
                    };
                    if needs_lazy_load {
                        self.lazy_load_directory(&item.path);
                    }
                }
                self.tree_state.expand(&item.path);
                self.update_cached_tree_len();
            }
        }
    }

    /// Collapse the selected item, or navigate back if already collapsed/not a directory.
    /// This provides vim-style navigation where 'h' goes up when there's nothing to collapse.
    fn collapse_selected(&mut self) {
        if let Some((node, root_path)) = self.get_view_root_node() {
            let items = TreeView::new(node, &root_path, &self.theme, &self.marked, &self.clipboard).flatten(&self.tree_state);
            if let Some(item) = items.get(self.tree_state.selected) {
                // Check if this is an expanded directory
                let is_expanded_dir = matches!(
                    item.node.kind,
                    crate::ui::VisibleNodeKind::Directory { expanded: true }
                );

                if is_expanded_dir {
                    // Collapse the directory
                    self.tree_state.collapse(&item.path);
                    self.update_cached_tree_len();
                } else {
                    // Not expanded or not a directory - navigate back to parent
                    self.navigate_back();
                }
            }
        }
    }

    fn toggle_selected(&mut self) {
        if let Some((node, root_path)) = self.get_view_root_node() {
            let items = TreeView::new(node, &root_path, &self.theme, &self.marked, &self.clipboard).flatten(&self.tree_state);
            if let Some(item) = items.get(self.tree_state.selected) {
                self.tree_state.toggle_expand(&item.path);
                self.update_cached_tree_len();
            }
        }
    }

    fn get_selected_info(&self) -> Option<SelectedInfo> {
        let tree = self.tree.as_ref()?;
        let (view_node, view_path) = self.get_view_root_node()?;

        match self.layout_mode {
            LayoutMode::Tree => {
                let items = TreeView::new(view_node, &view_path, &self.theme, &self.marked, &self.clipboard).flatten(&self.tree_state);
                let item = items.get(self.tree_state.selected)?;
                let node = Self::find_node_at_path(&tree.root, &item.path, &tree.root_path)?;

                Some(SelectedInfo {
                    name: item.node.name.clone(),
                    path: item.path.clone(),
                    size: node.size,
                    file_count: node.file_count(),
                    dir_count: node.dir_count(),
                    modified: node.timestamps.modified,
                    is_dir: node.is_dir(),
                })
            }
            LayoutMode::Miller => {
                let child = view_node.children.get(self.miller_state.selected)?;
                let path = self.view_root.join(&*child.name);

                Some(SelectedInfo {
                    name: child.name.to_string().into(),
                    path,
                    size: child.size,
                    file_count: child.file_count(),
                    dir_count: child.dir_count(),
                    modified: child.timestamps.modified,
                    is_dir: child.is_dir(),
                })
            }
        }
    }

    /// Check if the currently selected item is a file (not a directory).
    fn is_selected_file(&self) -> bool {
        match self.layout_mode {
            LayoutMode::Tree => {
                if let Some((view_node, view_path)) = self.get_view_root_node() {
                    let items = TreeView::new(view_node, &view_path, &self.theme, &self.marked, &self.clipboard)
                        .flatten(&self.tree_state);
                    if let Some(item) = items.get(self.tree_state.selected) {
                        return !matches!(item.node.kind, crate::ui::VisibleNodeKind::Directory { .. });
                    }
                }
                false
            }
            LayoutMode::Miller => {
                if let Some((view_node, _)) = self.get_view_root_node() {
                    if let Some(child) = view_node.children.get(self.miller_state.selected) {
                        return !child.is_dir();
                    }
                }
                false
            }
        }
    }

    /// Get the path of the currently selected item.
    fn get_selected_path(&self) -> Option<PathBuf> {
        match self.layout_mode {
            LayoutMode::Tree => {
                if let Some((view_node, view_path)) = self.get_view_root_node() {
                    let items = TreeView::new(view_node, &view_path, &self.theme, &self.marked, &self.clipboard)
                        .flatten(&self.tree_state);
                    return items.get(self.tree_state.selected).map(|item| item.path.clone());
                }
                None
            }
            LayoutMode::Miller => {
                if let Some((view_node, _)) = self.get_view_root_node() {
                    if let Some(child) = view_node.children.get(self.miller_state.selected) {
                        return Some(self.view_root.join(&*child.name));
                    }
                }
                None
            }
        }
    }

    /// Open the currently selected file with the configured opener.
    fn open_selected_file(&mut self) {
        let Some(path) = self.get_selected_path() else {
            return;
        };

        // Get opener configuration
        let openers = &self.user_settings.openers;
        let editor_config = &self.user_settings.editor;

        match crate::opener::open_file(&path, openers, editor_config) {
            crate::opener::OpenResult::Opened => {
                // File opened with system command (non-blocking)
            }
            crate::opener::OpenResult::NeedsSuspend(cmd) => {
                // Need to suspend terminal and run command
                self.suspend_and_run(cmd);
            }
            crate::opener::OpenResult::NotSupported(msg) => {
                self.error = Some(msg);
            }
            crate::opener::OpenResult::Error(msg) => {
                self.error = Some(msg);
            }
        }
    }

    /// Suspend the terminal, run a command, and restore.
    fn suspend_and_run(&mut self, cmd: std::process::Command) {
        // We need to restore terminal before running the command
        // and reinitialize after. This is handled by setting a flag
        // that the main loop will process.
        self.pending_suspend_command = Some(cmd);
    }

    /// Drill into the currently selected directory.
    fn drill_into_selected(&mut self) {
        let Some((view_node, view_path)) = self.get_view_root_node() else {
            return;
        };

        let items = TreeView::new(view_node, &view_path, &self.theme, &self.marked, &self.clipboard).flatten(&self.tree_state);
        let Some(item) = items.get(self.tree_state.selected) else {
            return;
        };

        if !matches!(
            item.node.kind,
            crate::ui::VisibleNodeKind::Directory { .. }
        ) {
            return;
        }

        // Check if directory needs lazy loading (has no children loaded yet)
        let needs_lazy_load = if let Some(tree) = &self.tree {
            if let Some(node) = Self::find_node_at_path(&tree.root, &item.path, &tree.root_path) {
                node.children.is_empty()
            } else {
                true
            }
        } else {
            true
        };

        // Lazy load directory contents if needed
        if needs_lazy_load {
            self.lazy_load_directory(&item.path);
        }

        if item.path == self.view_root {
            self.tree_state.expand(&item.path);
            self.update_cached_tree_len();
            return;
        }

        let saved_expanded = self.tree_state.expanded.clone();
        let saved_selected = self.tree_state.selected;
        self.view_history
            .push((self.view_root.clone(), saved_selected, saved_expanded));

        let target_path = item.path.clone();

        // Check if forward history has this path - if so, restore that state
        if let Some(forward_idx) = self.forward_history.iter().rposition(|(p, _, _)| p == &target_path) {
            let (_, fwd_selected, fwd_expanded) = self.forward_history.remove(forward_idx);
            // Clear any forward history entries after this one
            self.forward_history.truncate(forward_idx);

            self.view_root = target_path;
            self.tree_state.selected = fwd_selected;
            self.tree_state.offset = 0;
            self.tree_state.expanded = fwd_expanded;
            self.tree_state.expand(&self.view_root);
            self.update_cached_tree_len();
            self.sync_to_active_tab();
            return;
        }

        // Clear forward history - user is navigating to a new location
        self.forward_history.clear();

        self.view_root = target_path;

        self.tree_state.selected = 0;
        self.tree_state.offset = 0;
        self.tree_state.expand(&self.view_root);
        self.update_cached_tree_len();
        self.sync_to_active_tab();
    }

    /// Drill into the selected directory in Miller columns mode.
    fn drill_into_miller_selected(&mut self) {
        let Some((view_node, _)) = self.get_view_root_node() else {
            return;
        };

        let Some(selected_child) = view_node.children.get(self.miller_state.selected) else {
            return;
        };

        // Only drill into directories
        if !selected_child.is_dir() {
            return;
        }

        let target_path = self.view_root.join(&*selected_child.name);

        // Lazy loading: if directory has no children (from quick_list), load them now
        if selected_child.children.is_empty() {
            self.lazy_load_directory(&target_path);
        }

        // Save current state to history
        let saved_expanded = self.tree_state.expanded.clone();
        let saved_selected = self.miller_state.selected;
        self.view_history
            .push((self.view_root.clone(), saved_selected, saved_expanded));

        // Check if forward history has this path - if so, restore that state
        if let Some(forward_idx) = self.forward_history.iter().rposition(|(p, _, _)| p == &target_path) {
            let (_, fwd_selected, fwd_expanded) = self.forward_history.remove(forward_idx);
            // Clear any forward history entries after this one
            self.forward_history.truncate(forward_idx);

            self.view_root = target_path;
            self.miller_state.selected = fwd_selected;
            self.miller_state.offset = 0;
            self.tree_state.expanded = fwd_expanded;
            self.update_cached_miller_len();
            self.miller_state.ensure_visible(20);
            self.update_preview();
            self.sync_to_active_tab();
            return;
        }

        // Clear forward history - user is navigating to a new location
        self.forward_history.clear();

        // Update view root
        self.view_root = target_path;
        self.miller_state.reset();
        self.update_cached_miller_len();
        self.update_preview();
        self.sync_to_active_tab();
    }

    /// Lazy load a directory's children using quick_list.
    /// This updates the tree in-place with the new children.
    fn lazy_load_directory(&mut self, path: &PathBuf) {
        // First check if we have cached scan data for this directory
        if let Some(cached_tree) = self.scanned_cache.get(path).cloned() {
            // Use cached scan data - it has full size/structure information
            if let Some(tree) = &mut self.tree {
                if let Some(node) = Self::find_node_mut(&mut tree.root, path, &tree.root_path) {
                    // Replace with the cached node (preserves all scan data)
                    *node = cached_tree.root;
                }
            }
            return;
        }

        // Quick load directory contents for immediate display
        // When the full scan completes, it will replace this with accurate data
        if let Ok(quick_tree) = gravityfile_scan::quick_list(path) {
            // Find the node in our tree and update its children
            if let Some(tree) = &mut self.tree {
                if let Some(node) = Self::find_node_mut(&mut tree.root, path, &tree.root_path) {
                    node.children = quick_tree.root.children;
                }
            }
        }
    }

    /// Find a mutable reference to a node by path.
    fn find_node_mut<'a>(
        node: &'a mut gravityfile_core::FileNode,
        target: &PathBuf,
        current_path: &PathBuf,
    ) -> Option<&'a mut gravityfile_core::FileNode> {
        if current_path == target {
            return Some(node);
        }

        for child in &mut node.children {
            let child_path = current_path.join(&*child.name);
            if target.starts_with(&child_path) {
                if let Some(found) = Self::find_node_mut(child, target, &child_path) {
                    return Some(found);
                }
            }
        }

        None
    }

    /// Navigate back up to the previous view root.
    /// Supports navigation beyond the scan root, all the way to filesystem root.
    fn navigate_back(&mut self) {
        // Prepare current state for forward history (will be pushed if navigation succeeds)
        let current_selected = match self.layout_mode {
            LayoutMode::Tree => self.tree_state.selected,
            LayoutMode::Miller => self.miller_state.selected,
        };
        let current_state = (
            self.view_root.clone(),
            current_selected,
            self.tree_state.expanded.clone(),
        );

        // First, try to use view history if available
        if let Some((prev_root, saved_selected, saved_expanded)) = self.view_history.pop() {
            // Navigation will succeed - save current state to forward history
            self.forward_history.push(current_state);
            self.view_root = prev_root;
            self.tree_state.expanded = saved_expanded;

            // Restore selection based on layout mode
            match self.layout_mode {
                LayoutMode::Tree => {
                    self.tree_state.selected = saved_selected;
                    self.tree_state.offset = 0;
                    self.update_cached_tree_len();
                }
                LayoutMode::Miller => {
                    self.miller_state.selected = saved_selected;
                    self.miller_state.offset = 0;
                    self.update_cached_miller_len();
                }
            }
            self.sync_to_active_tab();
            return;
        }

        // No history - try to navigate to parent
        let Some(parent) = self.view_root.parent() else {
            return; // Already at root
        };

        let parent = parent.to_path_buf();
        let dir_we_came_from = self.view_root.clone();

        // Navigation will succeed - save current state to forward history
        self.forward_history.push(current_state);

        // Check if parent is within our current tree
        let parent_in_tree = parent.starts_with(&self.path) || parent == self.path;

        if parent_in_tree {
            // Navigate within existing tree
            self.tree_state.collapse(&dir_we_came_from);
            self.view_root = parent;
            self.tree_state.expand(&self.view_root);
        } else {
            // Navigate beyond scan root - need to extend our tree
            self.navigate_to_parent_beyond_scan_root(&parent, &dir_we_came_from);
            return;
        }

        // Select the directory we just came from
        self.select_child_by_name(&dir_we_came_from);
        self.sync_to_active_tab();
    }

    /// Navigate to a parent directory that's outside the current scan root.
    /// This extends the tree by lazy-loading the parent.
    fn navigate_to_parent_beyond_scan_root(&mut self, parent: &PathBuf, dir_we_came_from: &PathBuf) {
        // Cache current tree before navigating away (if we have scan data)
        if let Some(tree) = &self.tree {
            if self.has_full_scan {
                self.scanned_cache.insert(tree.root_path.clone(), tree.clone());
            }
        }

        // Load the parent directory contents
        let Ok(mut parent_tree) = gravityfile_scan::quick_list(parent) else {
            return; // Can't access parent directory
        };

        // Merge any cached scans into the parent tree's children
        self.merge_cached_scans_into_tree(&mut parent_tree.root, parent);

        // Create a new tree rooted at the parent
        self.path = parent.clone();
        self.tree = Some(parent_tree);
        self.view_root = parent.clone();
        self.view_history.clear();
        // Note: don't clear forward_history here - we want to allow navigating back

        // The parent tree is quick-listed, not fully scanned
        self.has_full_scan = false;

        // Reset tree state for the new root
        self.tree_state = TreeState::new(parent.clone());

        // Update cached parent for Miller columns display
        self.update_cached_parent();

        // Select the directory we came from
        self.select_child_by_name(dir_we_came_from);
        self.sync_to_active_tab();
    }

    /// Merge cached scan data into a tree node's children.
    /// For each child directory in the node, if we have cached scan data,
    /// replace the placeholder child with the fully scanned version.
    fn merge_cached_scans_into_tree(&self, node: &mut gravityfile_core::FileNode, node_path: &PathBuf) {
        for child in &mut node.children {
            if child.is_dir() {
                let child_path = node_path.join(&*child.name);
                if let Some(cached_tree) = self.scanned_cache.get(&child_path) {
                    // Replace this child with the cached scan data
                    *child = cached_tree.root.clone();
                }
            }
        }
    }

    /// Select a child entry by matching its name.
    fn select_child_by_name(&mut self, target_path: &PathBuf) {
        let dir_name = target_path
            .file_name()
            .and_then(|n| n.to_str())
            .unwrap_or("");

        match self.layout_mode {
            LayoutMode::Tree => {
                // In tree view, find the index of the target in flattened list
                if let Some((node, root_path)) = self.get_view_root_node() {
                    let items = TreeView::new(node, &root_path, &self.theme, &self.marked, &self.clipboard)
                        .flatten(&self.tree_state);
                    if let Some(idx) = items.iter().position(|item| {
                        item.path.file_name().and_then(|n| n.to_str()) == Some(dir_name)
                    }) {
                        self.tree_state.selected = idx;
                    } else {
                        self.tree_state.selected = 0;
                    }
                } else {
                    self.tree_state.selected = 0;
                }
                self.tree_state.offset = 0;
                self.update_cached_tree_len();
            }
            LayoutMode::Miller => {
                // In miller view, find the index in children
                if let Some((node, _)) = self.get_view_root_node() {
                    let idx = node
                        .children
                        .iter()
                        .position(|c| c.name.as_str() == dir_name)
                        .unwrap_or(0);
                    self.miller_state.selected = idx;
                } else {
                    self.miller_state.selected = 0;
                }
                self.miller_state.offset = 0;
                self.update_cached_miller_len();
            }
        }
    }

    /// Find a node by path in the tree.
    fn find_node_at_path<'a>(
        node: &'a gravityfile_core::FileNode,
        target_path: &PathBuf,
        current_path: &PathBuf,
    ) -> Option<&'a gravityfile_core::FileNode> {
        if current_path == target_path {
            return Some(node);
        }
        for child in &node.children {
            let child_path = current_path.join(&*child.name);
            if let Some(found) = Self::find_node_at_path(child, target_path, &child_path) {
                return Some(found);
            }
        }
        None
    }

    /// Get the current view root node.
    fn get_view_root_node(&self) -> Option<(&gravityfile_core::FileNode, PathBuf)> {
        let tree = self.tree.as_ref()?;
        let node = Self::find_node_at_path(&tree.root, &self.view_root, &tree.root_path)?;
        Some((node, self.view_root.clone()))
    }

    /// Get the parent node of the current view root (for Miller columns).
    fn get_parent_node(&self) -> Option<&gravityfile_core::FileNode> {
        let tree = self.tree.as_ref()?;
        let parent_path = self.view_root.parent()?.to_path_buf();

        // First, check if parent is within the main tree
        if parent_path.starts_with(&tree.root_path) || parent_path == tree.root_path {
            return Self::find_node_at_path(&tree.root, &parent_path, &tree.root_path);
        }

        // If we're at the tree root and have a cached parent, use that
        if self.view_root == tree.root_path {
            if let Some(cached) = &self.cached_parent_tree {
                return Some(&cached.root);
            }
        }

        None
    }

    /// Update the cached parent tree for Miller columns display.
    /// Called when navigating to ensure the parent column can be shown.
    fn update_cached_parent(&mut self) {
        let Some(tree) = &self.tree else { return };

        // Only need to cache parent if we're at or near the tree root
        if self.view_root == tree.root_path {
            if let Some(parent_path) = self.view_root.parent() {
                let parent_path_buf = parent_path.to_path_buf();
                // Load the parent directory
                if let Ok(mut parent_tree) = gravityfile_scan::quick_list(&parent_path_buf) {
                    // Merge any cached scans into the parent tree's children
                    // This ensures previously scanned directories show their data
                    self.merge_cached_scans_into_tree(&mut parent_tree.root, &parent_path_buf);
                    self.cached_parent_tree = Some(parent_tree);
                }
            }
        } else {
            // Don't need cached parent when navigating within the tree
            self.cached_parent_tree = None;
        }
    }

    /// Get the current directory name for highlighting in parent column.
    fn get_current_dir_name(&self) -> Option<String> {
        self.view_root
            .file_name()
            .and_then(|n| n.to_str())
            .map(|s| s.to_string())
    }

    /// Handle raw key input for command mode.
    fn handle_command_input(&mut self, key: crossterm::event::KeyEvent) {
        match self.command_input.handle_key(key) {
            CommandKeyResult::Continue => {}
            CommandKeyResult::Cancel => {
                self.mode = AppMode::Normal;
            }
            CommandKeyResult::Execute(cmd) => {
                self.mode = AppMode::Normal;
                self.execute_command(&cmd);
            }
        }
    }

    /// Execute a command from the command palette.
    fn execute_command(&mut self, cmd: &str) {
        match parse_command(cmd) {
            CommandAction::None => {}
            CommandAction::Quit => {
                self.mode = AppMode::Quit;
            }
            CommandAction::Refresh => {
                self.marked.clear();
                self.start_scan();
            }
            CommandAction::NavigateTo(target) => {
                self.cd_to_path(&target);
            }
            CommandAction::GoToRoot => {
                self.go_to_root();
            }
            CommandAction::NavigateBack => {
                self.navigate_back();
            }
            CommandAction::ShowHelp => {
                self.mode = AppMode::Help;
            }
            CommandAction::SwitchView(view) => {
                self.view = view;
            }
            CommandAction::ClearMarks => {
                self.marked.clear();
            }
            CommandAction::ToggleDetails => {
                self.show_details = !self.show_details;
            }
            CommandAction::SetTheme(theme_cmd) => match theme_cmd {
                ThemeCommand::Dark => self.theme = Theme::dark(),
                ThemeCommand::Light => self.theme = Theme::light(),
                ThemeCommand::Toggle => self.theme = self.theme.toggle(),
            },
            CommandAction::SetLayout(layout_cmd) => {
                match layout_cmd {
                    LayoutCommand::Tree => {
                        if self.layout_mode != LayoutMode::Tree {
                            // Use sync to preserve selection when switching to tree
                            self.sync_layout_selection();
                        }
                    }
                    LayoutCommand::Miller => {
                        if self.layout_mode != LayoutMode::Miller {
                            // Use sync to preserve selection when switching to miller
                            self.sync_layout_selection();
                        }
                    }
                    LayoutCommand::Toggle => {
                        self.sync_layout_selection();
                    }
                }
            }
            CommandAction::SetSort(sort_cmd) => {
                match sort_cmd {
                    SortCommand::SizeDesc => self.sort_mode = SortMode::SizeDescending,
                    SortCommand::SizeAsc => self.sort_mode = SortMode::SizeAscending,
                    SortCommand::NameAsc => self.sort_mode = SortMode::NameAscending,
                    SortCommand::NameDesc => self.sort_mode = SortMode::NameDescending,
                    SortCommand::DateDesc => self.sort_mode = SortMode::ModifiedDescending,
                    SortCommand::DateAsc => self.sort_mode = SortMode::ModifiedAscending,
                    SortCommand::CountDesc => self.sort_mode = SortMode::CountDescending,
                    SortCommand::CountAsc => self.sort_mode = SortMode::CountAscending,
                    SortCommand::Cycle => self.sort_mode = self.sort_mode.next(),
                    SortCommand::Reverse => self.sort_mode = self.sort_mode.reverse(),
                }
                self.apply_sort();
            }
            CommandAction::Yank => {
                self.yank_selection();
            }
            CommandAction::Cut => {
                self.cut_selection();
            }
            CommandAction::Paste => {
                self.paste_clipboard();
            }
            CommandAction::Delete => {
                if !self.marked.is_empty() {
                    self.mode = AppMode::ConfirmDelete;
                }
            }
            CommandAction::Rename(name) => {
                if let Some(name) = name {
                    // Direct rename with provided name
                    if let Some(path) = self.get_selected_path() {
                        self.execute_rename(path, name);
                    }
                } else {
                    // Enter rename mode
                    self.start_rename();
                }
            }
            CommandAction::CreateFile(name) => {
                if let Some(name) = name {
                    // Direct create with provided name
                    self.execute_create_file(self.view_root.clone(), name);
                } else {
                    // Enter create file mode
                    self.start_create_file();
                }
            }
            CommandAction::CreateDirectory(name) => {
                if let Some(name) = name {
                    // Direct create with provided name
                    self.execute_create_directory(self.view_root.clone(), name);
                } else {
                    // Enter create directory mode
                    self.start_create_directory();
                }
            }
            CommandAction::Take(name) => {
                if let Some(name) = name {
                    // Direct take with provided name
                    self.execute_take(self.view_root.clone(), name);
                } else {
                    // Enter take mode
                    self.start_take();
                }
            }
            CommandAction::Undo => {
                self.execute_undo();
            }
        }
    }

    /// Go to the scan root.
    fn go_to_root(&mut self) {
        self.view_root = self.path.clone();
        self.view_history.clear();
        self.forward_history.clear();
        self.tree_state.selected = 0;
        self.tree_state.offset = 0;
        self.tree_state.expand(&self.view_root);
        self.update_cached_tree_len();
        self.sync_to_active_tab();
    }

    /// Change directory to a path.
    fn cd_to_path(&mut self, target: &str) {
        if target == "/" || target == "~" {
            self.go_to_root();
            return;
        }
        if target == ".." {
            self.navigate_back();
            return;
        }

        let Some(tree) = &self.tree else {
            return;
        };

        let target_path = if target.starts_with('/') {
            let relative = target.trim_start_matches('/');
            self.path.join(relative)
        } else {
            self.view_root.join(target)
        };

        if let Some(node) = Self::find_node_at_path(&tree.root, &target_path, &tree.root_path) {
            if node.is_dir() {
                let saved_expanded = self.tree_state.expanded.clone();
                let saved_selected = self.tree_state.selected;
                self.view_history
                    .push((self.view_root.clone(), saved_selected, saved_expanded));

                self.view_root = target_path;
                self.tree_state.selected = 0;
                self.tree_state.offset = 0;
                self.tree_state.expand(&self.view_root);
                self.update_cached_tree_len();
                self.sync_to_active_tab();
            }
        }
    }

    /// Check if a path is marked for deletion.
    pub fn is_marked(&self, path: &PathBuf) -> bool {
        self.marked.contains(path)
    }

    /// Cycle to the next sort mode and apply sorting.
    fn cycle_sort_mode(&mut self) {
        self.sort_mode = self.sort_mode.next();
        self.apply_sort();
    }

    /// Reverse the current sort direction.
    fn reverse_sort_mode(&mut self) {
        self.sort_mode = self.sort_mode.reverse();
        self.apply_sort();
    }

    /// Sync current tab state to the active tab.
    fn sync_to_active_tab(&mut self) {
        if let Some(tab) = self.tab_manager.active_tab_mut() {
            tab.view_root = self.view_root.clone();
            tab.label = self.view_root
                .file_name()
                .map(|n| n.to_string_lossy().to_string())
                .unwrap_or_else(|| self.view_root.to_string_lossy().to_string());
        }
    }

    /// Sync app state from the active tab.
    fn sync_from_active_tab(&mut self) {
        if let Some(tab) = self.tab_manager.active_tab() {
            // Only sync if view_root is different
            if tab.view_root != self.view_root {
                self.view_root = tab.view_root.clone();
                // Reset tree state for the new view
                self.tree_state.selected = 0;
                self.tree_state.offset = 0;
                self.tree_state.expand(&self.view_root);
                self.miller_state.reset();
                match self.layout_mode {
                    LayoutMode::Tree => self.update_cached_tree_len(),
                    LayoutMode::Miller => self.update_cached_miller_len(),
                }
            }
        }
    }

    /// Start fuzzy search mode.
    fn start_search(&mut self) {
        // Populate search paths from the current tree
        self.update_search_paths();
        self.search_state.activate();
        self.mode = AppMode::Search;
    }

    /// Update search paths from the file tree.
    fn update_search_paths(&mut self) {
        let mut paths = Vec::new();

        if let Some(tree) = &self.tree {
            Self::collect_paths_recursive(
                &tree.root,
                &tree.root_path,
                &self.view_root,
                &mut paths,
            );
        }

        self.search_state.update_paths(paths);
    }

    /// Recursively collect paths from the file tree.
    fn collect_paths_recursive(
        node: &gravityfile_core::FileNode,
        current_path: &PathBuf,
        view_root: &PathBuf,
        paths: &mut Vec<(PathBuf, String, bool)>,
    ) {
        // Only include paths under view_root
        if !current_path.starts_with(view_root) && current_path != view_root {
            return;
        }

        // Compute relative display path from view_root
        let display = if current_path == view_root {
            ".".to_string()
        } else {
            current_path
                .strip_prefix(view_root)
                .map(|p| p.to_string_lossy().to_string())
                .unwrap_or_else(|_| node.name.to_string())
        };

        paths.push((current_path.clone(), display, node.is_dir()));

        // Recurse into children
        for child in &node.children {
            let child_path = current_path.join(&*child.name);
            Self::collect_paths_recursive(child, &child_path, view_root, paths);
        }
    }

    /// Handle key input during search mode.
    fn handle_search_input(&mut self, key: crossterm::event::KeyEvent) {
        use crossterm::event::{KeyCode, KeyModifiers};

        match (key.code, key.modifiers) {
            // Cancel search
            (KeyCode::Esc, _) => {
                self.search_state.deactivate();
                self.mode = AppMode::Normal;
            }
            // Execute search (navigate to selected)
            (KeyCode::Enter, _) => {
                if let Some(result) = self.search_state.selected_result() {
                    let target_path = result.path.clone();
                    self.navigate_to_search_result(&target_path);
                }
                self.search_state.deactivate();
                self.mode = AppMode::Normal;
            }
            // Navigation in results
            (KeyCode::Up, _) | (KeyCode::Char('k'), KeyModifiers::CONTROL) => {
                self.search_state.move_up();
            }
            (KeyCode::Down, _) | (KeyCode::Char('j'), KeyModifiers::CONTROL) => {
                self.search_state.move_down();
            }
            // Cycle search mode
            (KeyCode::Tab, _) => {
                self.search_state.cycle_mode();
            }
            // Cursor movement
            (KeyCode::Left, _) => {
                self.search_state.move_cursor_left();
            }
            (KeyCode::Right, _) => {
                self.search_state.move_cursor_right();
            }
            (KeyCode::Home, _) | (KeyCode::Char('a'), KeyModifiers::CONTROL) => {
                self.search_state.move_cursor_start();
            }
            (KeyCode::End, _) | (KeyCode::Char('e'), KeyModifiers::CONTROL) => {
                self.search_state.move_cursor_end();
            }
            // Delete
            (KeyCode::Backspace, _) => {
                self.search_state.delete_char_before();
            }
            (KeyCode::Delete, _) => {
                self.search_state.delete_char_at();
            }
            // Clear line
            (KeyCode::Char('u'), KeyModifiers::CONTROL) => {
                self.search_state.clear_query();
            }
            // Type character
            (KeyCode::Char(c), KeyModifiers::NONE | KeyModifiers::SHIFT) => {
                self.search_state.insert_char(c);
            }
            _ => {}
        }
    }

    /// Navigate to a search result path.
    fn navigate_to_search_result(&mut self, target: &PathBuf) {
        // If it's a directory, drill into it
        // If it's a file, navigate to its parent and select it
        if target.is_dir() {
            // Save current state
            let saved_expanded = self.tree_state.expanded.clone();
            let saved_selected = match self.layout_mode {
                LayoutMode::Tree => self.tree_state.selected,
                LayoutMode::Miller => self.miller_state.selected,
            };
            self.view_history.push((self.view_root.clone(), saved_selected, saved_expanded));

            // Navigate to directory
            self.view_root = target.clone();
            self.tree_state.selected = 0;
            self.tree_state.offset = 0;
            self.tree_state.expand(&self.view_root);
            self.miller_state.reset();

            match self.layout_mode {
                LayoutMode::Tree => self.update_cached_tree_len(),
                LayoutMode::Miller => self.update_cached_miller_len(),
            }
            self.sync_to_active_tab();
        } else {
            // It's a file - navigate to parent and select it
            if let Some(parent) = target.parent() {
                // Save current state
                let saved_expanded = self.tree_state.expanded.clone();
                let saved_selected = match self.layout_mode {
                    LayoutMode::Tree => self.tree_state.selected,
                    LayoutMode::Miller => self.miller_state.selected,
                };
                self.view_history.push((self.view_root.clone(), saved_selected, saved_expanded));

                // Navigate to parent
                self.view_root = parent.to_path_buf();
                self.tree_state.expand(&self.view_root);

                // Get file name before borrowing for position search
                let file_name = target.file_name().and_then(|n| n.to_str()).map(|s| s.to_string());
                let target_clone = target.clone();

                match self.layout_mode {
                    LayoutMode::Tree => {
                        // For tree view, we need to find the index in flattened list
                        self.tree_state.selected = 0;
                        self.update_cached_tree_len();

                        // Find position in flattened tree
                        if let Some((node, _)) = self.get_view_root_node() {
                            let items = TreeView::new(node, &self.view_root, &self.theme, &self.marked, &self.clipboard)
                                .flatten(&self.tree_state);
                            if let Some(pos) = items.iter().position(|i| i.path == target_clone) {
                                self.tree_state.selected = pos;
                            }
                        }
                    }
                    LayoutMode::Miller => {
                        // Find the index of the file in the parent's children
                        if let (Some(file_name), Some((node, _))) = (&file_name, self.get_view_root_node()) {
                            let idx = node
                                .children
                                .iter()
                                .position(|c| c.name.as_str() == file_name)
                                .unwrap_or(0);
                            self.miller_state.selected = idx;
                        }
                        self.update_cached_miller_len();
                    }
                }
                self.sync_to_active_tab();
            }
        }
    }

    /// Handle key input during settings mode.
    fn handle_settings_input(&mut self, action: KeyAction) {
        match action {
            KeyAction::Cancel | KeyAction::Quit => {
                // Close settings without saving
                self.settings_state = None;
                self.mode = AppMode::Normal;
            }
            KeyAction::MoveUp => {
                if let Some(ref mut state) = self.settings_state {
                    state.move_up();
                }
            }
            KeyAction::MoveDown => {
                if let Some(ref mut state) = self.settings_state {
                    state.move_down();
                }
            }
            KeyAction::ToggleMark | KeyAction::DrillDown => {
                // Space or Enter toggles the current setting
                if let Some(ref mut state) = self.settings_state {
                    state.toggle_selected();
                }
            }
            KeyAction::Sort => {
                // 's' saves settings
                if let Some(ref state) = self.settings_state {
                    if state.dirty {
                        // Save settings to disk
                        self.user_settings = state.settings.clone();
                        if let Err(e) = self.user_settings.save() {
                            self.error = Some(format!("Failed to save settings: {}", e));
                        } else {
                            // Apply settings immediately where applicable
                            self.scan_on_startup = self.user_settings.scan_on_startup;
                        }
                    }
                }
                self.settings_state = None;
                self.mode = AppMode::Normal;
            }
            _ => {}
        }
    }

    /// Apply the current sort mode to the file tree.
    fn apply_sort(&mut self) {
        if let Some(ref mut tree) = self.tree {
            Self::sort_node_recursive(&mut tree.root, self.sort_mode);
            // Update cached lengths after sorting
            match self.layout_mode {
                LayoutMode::Tree => self.update_cached_tree_len(),
                LayoutMode::Miller => self.update_cached_miller_len(),
            }
        }
    }

    /// Recursively sort a node and all its children.
    fn sort_node_recursive(node: &mut gravityfile_core::FileNode, mode: SortMode) {
        // Sort children based on mode
        node.children.sort_by(|a, b| {
            match mode {
                SortMode::SizeDescending => b.size.cmp(&a.size),
                SortMode::SizeAscending => a.size.cmp(&b.size),
                SortMode::NameAscending => a.name.cmp(&b.name),
                SortMode::NameDescending => b.name.cmp(&a.name),
                SortMode::ModifiedDescending => b.timestamps.modified.cmp(&a.timestamps.modified),
                SortMode::ModifiedAscending => a.timestamps.modified.cmp(&b.timestamps.modified),
                SortMode::CountDescending => {
                    let a_count = a.children.len();
                    let b_count = b.children.len();
                    b_count.cmp(&a_count)
                }
                SortMode::CountAscending => {
                    let a_count = a.children.len();
                    let b_count = b.children.len();
                    a_count.cmp(&b_count)
                }
            }
        });

        // Recursively sort children
        for child in &mut node.children {
            Self::sort_node_recursive(child, mode);
        }
    }

    /// Get duplicate groups filtered to current view_root.
    fn get_filtered_duplicates(
        &self,
    ) -> Option<(Vec<&gravityfile_analyze::DuplicateGroup>, u64)> {
        let dups = self.duplicates.as_ref()?;

        if self.view_root == self.path {
            let total: u64 = dups.groups.iter().map(|g| g.wasted_bytes).sum();
            return Some((dups.groups.iter().collect(), total));
        }

        let filtered: Vec<&gravityfile_analyze::DuplicateGroup> = dups
            .groups
            .iter()
            .filter(|g| g.paths.iter().any(|p| p.starts_with(&self.view_root)))
            .collect();

        let total_wasted: u64 = filtered
            .iter()
            .map(|g| {
                let paths_in_view = g
                    .paths
                    .iter()
                    .filter(|p| p.starts_with(&self.view_root))
                    .count();
                if paths_in_view > 1 {
                    g.size * (paths_in_view as u64 - 1)
                } else {
                    0
                }
            })
            .sum();

        Some((filtered, total_wasted))
    }

    /// Get stale directories filtered to current view_root.
    fn get_filtered_stale_dirs(&self) -> Option<Vec<&gravityfile_analyze::StaleDirectory>> {
        let age = self.age_report.as_ref()?;

        if self.view_root == self.path {
            return Some(age.stale_directories.iter().collect());
        }

        let filtered: Vec<&gravityfile_analyze::StaleDirectory> = age
            .stale_directories
            .iter()
            .filter(|d| d.path.starts_with(&self.view_root))
            .collect();

        Some(filtered)
    }
}

impl Widget for &App {
    fn render(self, area: Rect, buf: &mut Buffer) {
        let ctx = RenderContext {
            mode: self.mode,
            view: self.view,
            theme: &self.theme,
            path: &self.path,
            view_root: &self.view_root,
            show_details: self.show_details,
            tree: self.tree.as_ref(),
            tree_state: &self.tree_state,
            layout_mode: self.layout_mode,
            miller_state: &self.miller_state,
            scan_progress: self.scan_progress.as_ref(),
            deletion_progress: self.deletion_progress.as_ref(),
            duplicates: self.duplicates.as_ref(),
            age_report: self.age_report.as_ref(),
            warnings: &self.warnings,
            duplicates_state: &self.duplicates_state,
            selected_stale_dir: self.selected_stale_dir,
            selected_warning: self.selected_warning,
            marked: &self.marked,
            deletion_message: self.deletion_message.as_ref(),
            operation_message: self.operation_message.as_ref(),
            error: self.error.as_deref(),
            command_input: self.command_input.buffer(),
            command_cursor: self.command_input.cursor(),
            input_state: self.input_state.as_ref(),
            operation_progress: self.operation_progress.as_ref(),
            pending_conflict: self.pending_conflict.as_ref(),
            clipboard: &self.clipboard,
            get_path_size: Box::new(|p| self.get_path_size(p)),
            get_selected_info: self.get_selected_info(),
            get_view_root_node: self.get_view_root_node(),
            get_parent_node: self.get_parent_node(),
            current_dir_name: self.get_current_dir_name(),
            get_filtered_duplicates: self.get_filtered_duplicates(),
            get_filtered_stale_dirs: self.get_filtered_stale_dirs(),
            sort_mode: self.sort_mode,
            search_state: &self.search_state,
            tab_manager: &self.tab_manager,
            preview_content: &self.preview_state.content,
            preview_mode: self.preview_state.mode,
            has_full_scan: self.has_full_scan,
            settings_state: self.settings_state.as_ref(),
        };

        render_app(&ctx, area, buf);
    }
}