mcraw-tui 0.2.0

Cross-platform TUI for browsing and exploring MotionCam (.mcraw) files
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
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
use anyhow::Result;
use crossterm::{
    terminal::{disable_raw_mode, enable_raw_mode, EnterAlternateScreen, LeaveAlternateScreen},
    event::{Event, KeyEventKind, EnableBracketedPaste, DisableBracketedPaste, EnableMouseCapture, DisableMouseCapture},
};
use percent_encoding::percent_decode_str;
use ratatui::backend::CrosstermBackend;
use std::cell::Cell;
use std::path::PathBuf;
use std::sync::mpsc;
use std::time::{Duration, Instant};
use tokio::time;

use crate::cli::{Cli, CliCommands, ResolvedCli};
use crate::color::{ColorSpace, TransferFunction};
use crate::export::{
    Av1Profile, CodecFamily, DnxhrProfile, H264Profile, HevcProfile,
    ProResProfile, RateControl, Vp9Profile,
};
use crate::hardware::probe_hardware;
use std::sync::Arc;
use std::sync::atomic::{AtomicBool, Ordering};
use crate::decoder::Decoder;
use crate::encoder::{EncodeJob, EncodeStatus, Encoder, OutputFormat};
use crate::file::McrawFileInfo;
use crate::file_browser::FileBrowser;
use crate::preset::ExportPreset;
use crate::stats::PipelineStats;

/// Braille spinner frames for the rendering indicator (500ms cycle at 50ms/tick).
pub const SPINNER_FRAMES: &[&str] = &["", "", "", "", "", "", "", "", "", ""];
use crate::ui::{self, ClickAction};

// ---------------------------------------------------------------------------
// Data types for the media pool / queue workflow
// ---------------------------------------------------------------------------

#[derive(Debug, Clone)]
pub struct ImportedFile {
    pub path: String,
    pub info: McrawFileInfo,
    pub selected: bool,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum QueueStatus {
    Waiting,
    Rendering,
    Completed,
    Failed(String),
}

#[derive(Debug, Clone)]
pub struct QueuedFile {
    pub path: String,
    pub info: McrawFileInfo,
    pub selected: bool,
    pub status: QueueStatus,
    pub progress: f64,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum FocusTarget {
    MediaPool,
    Queue,
    ExportSettings,
    Preview,
    Grade,
}

#[derive(Debug, Clone, Copy)]
pub struct GradeSliders {
    pub exposure: f32,
    pub contrast: f32,
    pub saturation: f32,
    pub shadows: f32,
    pub highlights: f32,
    pub temperature: f32,
    pub tint: f32,
    pub sharpen: f32,
}

impl GradeSliders {
    pub fn name(index: usize) -> &'static str {
        match index {
            0 => "Exposure",
            1 => "Contrast",
            2 => "Saturation",
            3 => "Shadows",
            4 => "Highlights",
            5 => "Temp",
            6 => "Tint",
            7 => "Sharpen",
            _ => "",
        }
    }

    pub fn default_val(index: usize) -> f32 {
        match index {
            0 => 0.0,
            1 => 1.0,
            2 => 1.0,
            3 => 0.0,
            4 => 0.0,
            5 => 5200.0,
            6 => 0.0,
            7 => 0.0,
            _ => 0.0,
        }
    }

    pub fn min(index: usize) -> f32 {
        match index {
            0 => -5.0,
            1 => 0.0,
            2 => 0.0,
            3 => -1.0,
            4 => -1.0,
            5 => 2000.0,
            6 => -100.0,
            7 => 0.0,
            _ => 0.0,
        }
    }

    pub fn max(index: usize) -> f32 {
        match index {
            0 => 5.0,
            1 => 2.0,
            2 => 2.0,
            3 => 1.0,
            4 => 1.0,
            5 => 10000.0,
            6 => 100.0,
            7 => 1.0,
            _ => 1.0,
        }
    }

    pub fn step_small(index: usize) -> f32 {
        match index {
            0 => 0.1,
            5 => 50.0,
            6 => 1.0,
            _ => 0.05,
        }
    }

    pub fn step_large(index: usize) -> f32 {
        match index {
            0 => 1.0,
            5 => 500.0,
            6 => 10.0,
            _ => 0.25,
        }
    }

    pub fn value(&self, index: usize) -> f32 {
        match index {
            0 => self.exposure,
            1 => self.contrast,
            2 => self.saturation,
            3 => self.shadows,
            4 => self.highlights,
            5 => self.temperature,
            6 => self.tint,
            7 => self.sharpen,
            _ => 0.0,
        }
    }

    pub fn normalized(&self, index: usize) -> f32 {
        let v = self.value(index);
        let lo = Self::min(index);
        let hi = Self::max(index);
        if hi <= lo { return 0.5; }
        ((v - lo) / (hi - lo)).clamp(0.0, 1.0)
    }

    pub fn display_value(&self, index: usize) -> String {
        let sign = |x: f32| if x >= 0.0 { "+" } else { "" };
        match index {
            0 => format!("{}{:.1} stops", sign(self.exposure), self.exposure),
            1 => format!("{:.2}x", self.contrast),
            2 => format!("{:.2}x", self.saturation),
            3 => format!("{}{:.2}", sign(self.shadows), self.shadows),
            4 => format!("{}{:.2}", sign(self.highlights), self.highlights),
            5 => format!("{:.0}K", self.temperature),
            6 => format!("{}{:.0}", sign(self.tint), self.tint),
            _ => format!("{:.2}", self.sharpen),
        }
    }

    pub fn set(&mut self, index: usize, v: f32) {
        let lo = Self::min(index);
        let hi = Self::max(index);
        let v = v.clamp(lo, hi);
        match index {
            0 => self.exposure = v,
            1 => self.contrast = v,
            2 => self.saturation = v,
            3 => self.shadows = v,
            4 => self.highlights = v,
            5 => self.temperature = v,
            6 => self.tint = v,
            7 => self.sharpen = v,
            _ => {}
        }
    }

    pub fn apply_delta(&mut self, index: usize, step: f32) {
        let cur = self.value(index);
        self.set(index, cur + step);
    }

    pub fn count() -> usize { 8 }
}

impl Default for GradeSliders {
    fn default() -> Self {
        Self {
            exposure: 0.0,
            contrast: 1.0,
            saturation: 1.0,
            shadows: 0.0,
            highlights: 0.0,
            temperature: 5200.0,
            tint: 0.0,
            sharpen: 0.0,
        }
    }
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ImportPopupState {
    Hidden,
    DroppedFiles {
        files: Vec<String>,
        folder: String,
        all_in_folder: Vec<String>,
    },
}

#[derive(Debug)]
pub enum ExportEvent {
    Progress(f64),
    Stats(Arc<PipelineStats>),
    Done(Result<()>),
}

/// Snapshot of the most recently finished export. Kept so the UI can show a
/// post-render summary (codec, settings, elapsed time, output path, etc.)
/// instead of immediately reverting to the preview panel.
#[derive(Debug, Clone)]
pub struct ExportSummary {
    pub output_path: String,
    pub codec_label: String,
    pub profile_label: String,
    pub color_space: String,
    pub transfer: String,
    pub rate_control: String,
    pub frame_count: usize,
    pub elapsed: Duration,
    pub result: Result<(), String>,
}

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Screen {
    Browse,
    Info,
    Export,
}

/// Tracks real render-loop frame rate using a simple per-second counter.
///
/// Updates once per second with EMA smoothing (`0.9 * prev + 0.1 * current`)
/// to dampen visual jitter. The value is exposed via `fps()` and rendered
/// right-aligned in the header bar.
#[derive(Debug, Clone)]
pub struct FPSCounter {
    last_draw: Instant,
    frames_this_second: u32,
    second_start: Instant,
    smooth_fps: f64,
}

impl FPSCounter {
    pub fn new() -> Self {
        Self {
            last_draw: Instant::now(),
            frames_this_second: 0,
            second_start: Instant::now(),
            smooth_fps: 0.0,
        }
    }

    /// Call once per frame before measuring elapsed time.
    pub fn tick(&mut self) {
        let now = Instant::now();
        self.frames_this_second += 1;
        if now.duration_since(self.second_start).as_secs_f64() >= 1.0 {
            let fps = self.frames_this_second as f64;
            if self.smooth_fps == 0.0 {
                self.smooth_fps = fps;
            } else {
                self.smooth_fps = self.smooth_fps * 0.9 + fps * 0.1;
            }
            self.frames_this_second = 0;
            self.second_start = now;
        }
        self.last_draw = now;
    }

    pub fn fps(&self) -> f64 {
        self.smooth_fps
    }
}

pub struct App {
    pub running: bool,
    pub screen: Screen,
    pub file_path: Option<String>,
    pub file_info: Option<McrawFileInfo>,
    pub frame_index: usize,
    pub frame_count: usize,
    pub encode_jobs: Vec<EncodeJob>,
    pub status_message: String,
    pub show_help: bool,
    pub error: Option<String>,
    pub browser: FileBrowser,

    pub is_exporting: bool,
    pub export_cancelled: bool,
    pub export_progress: f64,
    pub export_rx: Option<mpsc::Receiver<ExportEvent>>,
    pub cancel_token: Option<Arc<AtomicBool>>,

    /// Snapshot of the most-recent finished export — drives the post-render
    /// summary panel. Cleared when the user starts a new export.
    pub last_export_summary: Option<ExportSummary>,

    /// Settings captured at `start_export` time so `poll_export` can build
    /// an accurate `ExportSummary` even if the user has since cycled the
    /// export-settings panel to different values.
    pub pending_export_summary: Option<ExportSummary>,

    // Which queue item is currently being rendered (for sequential batch)
    pub current_rendering_index: Option<usize>,

    // Export folder for the current session
    pub export_folder: Option<std::path::PathBuf>,

    // Favourite folders for quick browser navigation
    pub favourite_folders: Vec<std::path::PathBuf>,

    // Help overlay scroll position
    pub help_scroll: u16,

    // Culling mode flag
    pub show_culling: bool,

    // Full-screen grade mode (Shift+G)
    pub show_grade_screen: bool,

    // Persistent export settings
    pub export_color_space: ColorSpace,
    pub export_transfer_function: TransferFunction,
    pub export_codec_family: CodecFamily,
    pub export_focus: ExportFocus,
    pub export_start_time: Option<Instant>,

    // Sticky per-codec profiles
    pub prores_profile: ProResProfile,
    pub dnxhr_profile: DnxhrProfile,
    pub hevc_profile: HevcProfile,
    pub h264_profile: H264Profile,
    pub av1_profile: Av1Profile,
    pub vp9_profile: Vp9Profile,

    // Runtime hardware probe result
    pub hardware_caps: crate::hardware::HardwareCaps,

    // Rate control
    pub active_rate_control: RateControl,
    pub is_editing_custom_rate: bool,

    // Grading sliders (Phase 2)
    pub grade_sliders: GradeSliders,
    pub grade_focus: usize,
    /// Active mouse drag on a grade slider: (slider_index, track_x, track_width)
    pub grade_dragging: Option<(usize, u16, u16)>,

    // Media pool / queue workflow
    pub imported_files: Vec<ImportedFile>,
    pub media_pool_index: usize,

    pub queue: Vec<QueuedFile>,
    pub queue_index: usize,

    pub show_browser: bool,
    pub import_popup: ImportPopupState,

    pub focus_target: FocusTarget,

    pub show_full_info: bool,

    // Browser double-click detection
    pub last_browser_click: Option<(Instant, usize)>,

    // Grade slider double-click detection
    pub last_grade_click: Option<(Instant, usize)>,

    // Drag-drop visual feedback
    pub drop_highlight: Option<Instant>,

    // Async drag-drop import state
    pub drop_import_rx: Option<mpsc::Receiver<DropImportEvent>>,
    pub drop_import_cancel: Option<Arc<AtomicBool>>,

    // Drop preview overlay for visual feedback
    pub drop_preview: Option<DropPreview>,

    // Persistent ListState offset for browser (prevents viewport jumping on click)
    pub browser_scroll_offset: Cell<usize>,

    // Pinned favourites bar toggle
    pub show_favourites_bar: bool,

    // When true, the browser list is replaced by a flat view of the
    // user's favourite folders (f-key toggle). `..` is hidden in this
    // view because the favourites list isn't a filesystem hierarchy.
    pub browsing_favourites: bool,

    // Persistent ListState offset for the favourites list view
    pub favourites_scroll_offset: Cell<usize>,

    // Timestamp + index of last clicked favourite (for d-key removal)
    pub last_clicked_favourite: Option<(Instant, usize)>,

    // -------------------------------------------------------------------
    // Export presets
    // -------------------------------------------------------------------
    /// User-saved export setting bundles. Loaded from
    /// `presets.json` at startup, written back on every change.
    pub presets: Vec<crate::preset::ExportPreset>,

    /// Name of the preset that was last applied, if any. Shown in the
    /// Export Settings panel header so the user can see *why* the current
    /// settings look the way they do.
    pub active_preset: Option<String>,

    /// State of the preset-picker overlay.
    pub preset_picker: PresetPickerState,

    /// True while the user is typing a name for a new preset. Captures
    /// the live text and the cursor position. Esc cancels, Enter saves.
    pub preset_naming: Option<PresetNamingState>,

    // Animation state
    pub spinner_frame: u8,
    pub progress_anim_offset: u8,

    // Real-time render-loop performance meter
    pub fps_counter: FPSCounter,

    // Heatwave shockwave countdown (0 = inactive)
    pub shockwave_ticks_remaining: u8,

    // Focus strip state — whether the single-line HUD is in expanded slider view
    pub grade_strip_active: bool,
    // Parameter morph animation: (old_index, ticks_remaining)
    pub grade_morph: Option<(usize, u8)>,
    // Phosphor trail: (track_position 0..1, ticks_remaining)
    pub phosphor_trail: Vec<(f32, u8)>,
    // Snapshot for before/after comparison (B key)
    pub grade_before_snapshot: Option<GradeSliders>,
    // Focus strip idle counter: decrements each tick
    pub grade_strip_idle_ticks: u8,
}

/// Overlay state for the preset-picker. `Shown` holds the list, cursor
/// index, and a transient error/info string rendered at the bottom.
#[derive(Debug, Clone, Default)]
pub struct PresetPickerState {
    pub open: bool,
    pub index: usize,
    pub message: Option<String>,
}

#[derive(Debug, Clone)]
pub struct PresetNamingState {
    pub name: String,
    pub message: Option<String>,
}

/// Event from async drag-drop import worker
pub enum DropImportEvent {
    FileReady { path: String, info: McrawFileInfo },
    Failed { path: String, error: String },
    Complete { imported: usize, failed: usize },
}

/// Visual preview of dropped files
pub struct DropPreview {
    pub files: Vec<String>,
    pub start_time: Instant,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ExportFocus {
    ColorSpace,
    TransferFunction,
    CodecFamily,
    Profile,
    RateControl,
}

impl App {
    fn favourites_file() -> Option<PathBuf> {
        let mut dir = dirs::config_dir()?;
        dir.push("mcraw-tui");
        std::fs::create_dir_all(&dir).ok()?;
        dir.push("favourites.json");
        Some(dir)
    }

    fn load_favourites() -> Vec<PathBuf> {
        let path = match Self::favourites_file() {
            Some(p) => p,
            None => return Vec::new(),
        };
        let data = match std::fs::read_to_string(&path) {
            Ok(d) => d,
            Err(_) => return Vec::new(),
        };
        serde_json::from_str(&data).unwrap_or_default()
    }

    fn save_favourites(&self) {
        let path = match Self::favourites_file() {
            Some(p) => p,
            None => return,
        };
        if let Ok(data) = serde_json::to_string(&self.favourite_folders) {
            let _ = std::fs::write(path, data);
        }
    }

    pub fn new() -> Self {
        let caps = probe_hardware();
        App {
            running: true,
            screen: Screen::Browse,
            file_path: None,
            file_info: None,
            frame_index: 0,
            frame_count: 0,
            encode_jobs: Vec::new(),
            status_message: String::from("Ready | Drag-drop .mcraw files or press b to browse"),
            show_help: false,
            error: None,
            browser: FileBrowser::new(),

            is_exporting: false,
            export_cancelled: false,
            export_progress: 0.0,
            export_rx: None,
            cancel_token: None,
            last_export_summary: None,
            pending_export_summary: None,

            export_color_space: ColorSpace::Rec709,
            export_transfer_function: TransferFunction::Gamma24,
            export_codec_family: CodecFamily::HEVC,
            export_focus: ExportFocus::CodecFamily,
            export_start_time: None,

            prores_profile: ProResProfile::HQ,
            dnxhr_profile: DnxhrProfile::HQX,
            hevc_profile: HevcProfile::Main10_420,
            h264_profile: H264Profile::Main8bit,
            av1_profile: Av1Profile::Profile0_420_10bit,
            vp9_profile: Vp9Profile::Profile2_420_10bit,

            hardware_caps: caps,
            active_rate_control: RateControl::Lossless,
            is_editing_custom_rate: false,

            imported_files: Vec::new(),
            media_pool_index: 0,
            queue: Vec::new(),
            queue_index: 0,
            show_browser: true,
            current_rendering_index: None,
            export_folder: None,
            favourite_folders: Self::load_favourites(),
            help_scroll: 0,
            show_culling: false,
            show_grade_screen: false,
            import_popup: ImportPopupState::Hidden,
            focus_target: FocusTarget::MediaPool,
            show_full_info: false,
            last_browser_click: None,
            last_grade_click: None,
            drop_highlight: None,
            drop_import_rx: None,
            drop_import_cancel: None,
            drop_preview: None,
            browser_scroll_offset: Cell::new(0),
            show_favourites_bar: true,
            last_clicked_favourite: None,
            browsing_favourites: false,
            favourites_scroll_offset: Cell::new(0),
            presets: ExportPreset::load_all(),
            active_preset: None,
            preset_picker: PresetPickerState::default(),
            preset_naming: None,

            spinner_frame: 0,
            progress_anim_offset: 0,
            fps_counter: FPSCounter::new(),
            shockwave_ticks_remaining: 0,
            grade_sliders: GradeSliders::default(),
            grade_focus: 0,
            grade_dragging: None,
            grade_strip_active: true,
            grade_morph: None,
            phosphor_trail: Vec::new(),
            grade_before_snapshot: None,
            grade_strip_idle_ticks: 0,
        }
    }

    // -----------------------------------------------------------------------
    // File loading
    // -----------------------------------------------------------------------

    pub fn load_file(&mut self, path: String) {
        tracing::info!("load_file: path={}", path);
        self.error = None;
        self.status_message = String::new();
        match McrawFileInfo::from_path(&path) {
            Ok(mut info) => {
                tracing::debug!("file parsed: frames={} {}x{} fps={}", info.frame_count, info.width, info.height, info.fps);
                if let Ok(decoder) = Decoder::new(&path) {
                    if let Ok(container_meta) = decoder.container_metadata() {
                        let as_f64 = |v: &[f32; 9]| -> [f64; 9] {
                            let mut r = [0.0; 9];
                            for (i, &x) in v.iter().enumerate() { r[i] = x as f64; }
                            r
                        };
                        let non_zero = |m: &[f32; 9]| m.iter().any(|&x| x != 0.0);

                        info.camera_metadata.color_matrix = Some(as_f64(&container_meta.color_matrix1));
                        if non_zero(&container_meta.color_matrix2) {
                            info.camera_metadata.color_matrix2 = Some(as_f64(&container_meta.color_matrix2));
                        }
                        if non_zero(&container_meta.forward_matrix1) {
                            info.camera_metadata.forward_matrix1 = Some(as_f64(&container_meta.forward_matrix1));
                        }
                        if non_zero(&container_meta.forward_matrix2) {
                            info.camera_metadata.forward_matrix2 = Some(as_f64(&container_meta.forward_matrix2));
                        }
                        if container_meta.has_calibration_illuminants {
                            info.camera_metadata.calibration_illuminant1 = Some(container_meta.calibration_illuminant1);
                            info.camera_metadata.calibration_illuminant2 = Some(container_meta.calibration_illuminant2);
                        }

                        if container_meta.white_level > 0.0 {
                            info.white_level = container_meta.white_level;
                        }
                        if container_meta.black_level_count > 0 {
                            info.black_level = container_meta.black_level[0];
                        }
                    }
                    if let Ok(timestamps) = decoder.timestamps() {
                        info.frame_count = timestamps.len() as u32;
                        if timestamps.len() >= 2 {
                            let duration_ns = timestamps[timestamps.len() - 1] - timestamps[0];
                            if duration_ns > 0 {
                                let duration_in_seconds = duration_ns as f64 / 1_000_000_000.0;
                                info.fps = (info.frame_count.saturating_sub(1)) as f64 / duration_in_seconds;
                            }
                        }
                        if let Ok(first_frame_meta) = decoder.load_frame_metadata(timestamps[0]) {
                            info.width = first_frame_meta.width as u16;
                            info.height = first_frame_meta.height as u16;
                        }
                        // Initialize grade temperature from file white balance
                        if let Some(wb) = info.camera_metadata.wb_multipliers {
                            let r_gain = wb[0];
                            let b_gain = wb[2];
                            let ratio = (r_gain / b_gain.max(1e-6)).clamp(0.1, 10.0);
                            let temp = if ratio >= 1.0 {
                                5200.0 + (ratio - 1.0) * 3000.0
                            } else {
                                5200.0 - (1.0 - ratio) * 3000.0
                            };
                            self.grade_sliders.set(5, temp.clamp(2000.0, 10000.0));
                        } else {
                            self.grade_sliders.set(5, 5200.0);
                        }
                    }
                }

                self.file_info = Some(info.clone());
                self.frame_count = info.frame_count as usize;
                self.file_path = Some(path.clone());

                let already = self.imported_files.iter().any(|f| f.path == path);
                if !already {
                    self.imported_files.push(ImportedFile {
                        path: path.clone(),
                        info: info.clone(),
                        selected: true,
                    });
                    self.media_pool_index = self.imported_files.len() - 1;
                    tracing::info!("file added to media pool: index={}", self.media_pool_index);
                } else {
                    tracing::debug!("file already in media pool, skipping");
                }

                self.status_message = format!("Imported: {}", path);
                tracing::info!("file loaded successfully: {}", path);
            }
            Err(e) => {
                tracing::error!("failed to load file {}: {}", path, e);
                self.error = Some(format!("Failed to load file: {}", e));
                self.status_message = format!("Error: {}", e);
            }
        }
    }

    /// Add multiple files to the media pool (used by drag-drop).
    /// Returns (imported_count, failed_count).
    pub fn load_files_batch(&mut self, paths: &[String]) -> (usize, usize) {
        tracing::info!("load_files_batch: count={}", paths.len());
        let mut imported = 0;
        let mut failed = 0;
        for path in paths {
            self.error = None;
            match McrawFileInfo::from_path(path) {
                Ok(mut info) => {
                    if let Ok(decoder) = Decoder::new(path) {
                        if let Ok(container_meta) = decoder.container_metadata() {
                            let as_f64 = |v: &[f32; 9]| -> [f64; 9] {
                                let mut r = [0.0; 9];
                                for (i, &x) in v.iter().enumerate() { r[i] = x as f64; }
                                r
                            };
                            let non_zero = |m: &[f32; 9]| m.iter().any(|&x| x != 0.0);
                            info.camera_metadata.color_matrix = Some(as_f64(&container_meta.color_matrix1));
                            if non_zero(&container_meta.color_matrix2) {
                                info.camera_metadata.color_matrix2 = Some(as_f64(&container_meta.color_matrix2));
                            }
                            if non_zero(&container_meta.forward_matrix1) {
                                info.camera_metadata.forward_matrix1 = Some(as_f64(&container_meta.forward_matrix1));
                            }
                            if non_zero(&container_meta.forward_matrix2) {
                                info.camera_metadata.forward_matrix2 = Some(as_f64(&container_meta.forward_matrix2));
                            }
                            if container_meta.has_calibration_illuminants {
                                info.camera_metadata.calibration_illuminant1 = Some(container_meta.calibration_illuminant1);
                                info.camera_metadata.calibration_illuminant2 = Some(container_meta.calibration_illuminant2);
                            }
                            if container_meta.white_level > 0.0 {
                                info.white_level = container_meta.white_level;
                            }
                            if container_meta.black_level_count > 0 {
                                info.black_level = container_meta.black_level[0];
                            }
                        }
                        if let Ok(timestamps) = decoder.timestamps() {
                            info.frame_count = timestamps.len() as u32;
                            if timestamps.len() >= 2 {
                                let duration_ns = timestamps[timestamps.len() - 1] - timestamps[0];
                                if duration_ns > 0 {
                                    let duration_in_seconds = duration_ns as f64 / 1_000_000_000.0;
                                    info.fps = (info.frame_count.saturating_sub(1)) as f64 / duration_in_seconds;
                                }
                            }
                            if let Ok(first_frame_meta) = decoder.load_frame_metadata(timestamps[0]) {
                                info.width = first_frame_meta.width as u16;
                                info.height = first_frame_meta.height as u16;
                            }
                        }
                    }

                    let already = self.imported_files.iter().any(|f| f.path == *path);
                    if !already {
                        self.imported_files.push(ImportedFile {
                            path: path.clone(),
                            info: info.clone(),
                            selected: true,
                        });
                        imported += 1;
                        tracing::debug!("batch imported: {} ({} total)", path, self.imported_files.len());
                    }
                }
                Err(e) => {
                    failed += 1;
                    tracing::warn!("batch import failed for {}: {}", path, e);
                }
            }
        }
        // Select the first newly imported file
        if imported > 0 && self.imported_files.len() > 0 {
            self.media_pool_index = self.imported_files.len() - imported;
            self.file_info = Some(self.imported_files[self.media_pool_index].info.clone());
            self.file_path = Some(self.imported_files[self.media_pool_index].path.clone());
            self.frame_count = self.imported_files[self.media_pool_index].info.frame_count as usize;
        }
        (imported, failed)
    }

    /// Start async import of dropped files on a background thread.
    /// Returns immediately; results arrive via DropImportEvent channel.
    pub fn start_async_import(&mut self, paths: Vec<String>) {
        // Cancel any in-progress import
        if let Some(cancel) = self.drop_import_cancel.take() {
            cancel.store(true, Ordering::Relaxed);
        }

        let (tx, rx) = mpsc::channel::<DropImportEvent>();
        let cancel_flag = Arc::new(AtomicBool::new(false));
        self.drop_import_cancel = Some(cancel_flag.clone());
        self.drop_import_rx = Some(rx);

        // Show preview overlay
        self.drop_preview = Some(DropPreview {
            files: paths.iter()
                .filter(|p| p.to_lowercase().ends_with(".mcraw"))
                .map(|p| p.clone())
                .collect(),
            start_time: Instant::now(),
        });

        let total = paths.len();
        self.status_message = format!("Importing {} file(s)...", total);

        std::thread::spawn(move || {
            let mut imported = 0;
            let mut failed = 0;

            for path in paths {
                if cancel_flag.load(Ordering::Relaxed) {
                    tracing::info!("async drag-drop import cancelled");
                    break;
                }

                let path_clone = path.clone();
                match McrawFileInfo::from_path(&path) {
                    Ok(mut info) => {
                        // Enhance with decoder metadata (same as load_file)
                        if let Ok(decoder) = Decoder::new(&path) {
                            if let Ok(container_meta) = decoder.container_metadata() {
                                let as_f64 = |v: &[f32; 9]| -> [f64; 9] {
                                    let mut r = [0.0; 9];
                                    for (i, &x) in v.iter().enumerate() { r[i] = x as f64; }
                                    r
                                };
                                let non_zero = |m: &[f32; 9]| m.iter().any(|&x| x != 0.0);
                                info.camera_metadata.color_matrix = Some(as_f64(&container_meta.color_matrix1));
                                if non_zero(&container_meta.color_matrix2) {
                                    info.camera_metadata.color_matrix2 = Some(as_f64(&container_meta.color_matrix2));
                                }
                                if non_zero(&container_meta.forward_matrix1) {
                                    info.camera_metadata.forward_matrix1 = Some(as_f64(&container_meta.forward_matrix1));
                                }
                                if non_zero(&container_meta.forward_matrix2) {
                                    info.camera_metadata.forward_matrix2 = Some(as_f64(&container_meta.forward_matrix2));
                                }
                                if container_meta.has_calibration_illuminants {
                                    info.camera_metadata.calibration_illuminant1 = Some(container_meta.calibration_illuminant1);
                                    info.camera_metadata.calibration_illuminant2 = Some(container_meta.calibration_illuminant2);
                                }
                                if container_meta.white_level > 0.0 {
                                    info.white_level = container_meta.white_level;
                                }
                                if container_meta.black_level_count > 0 {
                                    info.black_level = container_meta.black_level[0];
                                }
                            }
                            if let Ok(timestamps) = decoder.timestamps() {
                                info.frame_count = timestamps.len() as u32;
                                if timestamps.len() >= 2 {
                                    let duration_ns = timestamps[timestamps.len() - 1] - timestamps[0];
                                    if duration_ns > 0 {
                                        let duration_in_seconds = duration_ns as f64 / 1_000_000_000.0;
                                        info.fps = (info.frame_count.saturating_sub(1)) as f64 / duration_in_seconds;
                                    }
                                }
                                if let Ok(first_frame_meta) = decoder.load_frame_metadata(timestamps[0]) {
                                    info.width = first_frame_meta.width as u16;
                                    info.height = first_frame_meta.height as u16;
                                }
                            }
                        }

                        let _ = tx.send(DropImportEvent::FileReady { path: path_clone, info });
                        imported += 1;
                    }
                    Err(e) => {
                        let _ = tx.send(DropImportEvent::Failed {
                            path: path_clone,
                            error: e.to_string(),
                        });
                        failed += 1;
                        tracing::warn!("async drag-drop import failed: {}: {}", path, e);
                    }
                }
            }

            let _ = tx.send(DropImportEvent::Complete { imported, failed });
        });
    }

    /// Poll for async drag-drop import results. Call every frame.
    pub fn poll_drop_import(&mut self) {
        let rx = match self.drop_import_rx.take() {
            Some(rx) => rx,
            None => return,
        };

        let mut keep_rx = true;
        while let Ok(event) = rx.try_recv() {
            match event {
                DropImportEvent::FileReady { path, info } => {
                    let already = self.imported_files.iter().any(|f| f.path == path);
                    if !already {
                        self.imported_files.push(ImportedFile {
                            path: path.clone(),
                            info: info.clone(),
                            selected: true,
                        });
                        // Select the first imported file
                        if self.imported_files.len() == 1 {
                            self.media_pool_index = 0;
                            self.file_info = Some(info.clone());
                            self.file_path = Some(path.clone());
                            self.frame_count = info.frame_count as usize;
                        }
                        tracing::debug!("async imported: {} ({} total)", path, self.imported_files.len());
                    }
                }
                DropImportEvent::Failed { path, error } => {
                    tracing::warn!("async import failed: {}: {}", path, error);
                }
                DropImportEvent::Complete { imported, failed } => {
                    keep_rx = false;
                    self.drop_import_cancel = None;
                    if imported > 0 {
                        self.media_pool_index = self.imported_files.len().saturating_sub(imported);
                        if let Some(f) = self.imported_files.get(self.media_pool_index) {
                            self.file_info = Some(f.info.clone());
                            self.file_path = Some(f.path.clone());
                            self.frame_count = f.info.frame_count as usize;
                        }
                    }
                    if failed > 0 {
                        self.status_message = format!("Imported {} file(s), {} failed", imported, failed);
                    } else {
                        self.status_message = format!("Imported {} file(s)", imported);
                    }
                    tracing::info!("async drag-drop import complete: {} imported, {} failed", imported, failed);
                }
            }
        }

        if keep_rx {
            self.drop_import_rx = Some(rx);
        }
    }

    pub fn load_all_in_folder(&mut self, dir: &std::path::Path) {
        if let Ok(entries) = std::fs::read_dir(dir) {
            let mut mcraw_paths: Vec<String> = entries
                .filter_map(|e| e.ok())
                .map(|e| e.path())
                .filter(|p| p.extension().map_or(false, |ext| ext == "mcraw"))
                .map(|p| p.to_string_lossy().to_string())
                .collect();
            mcraw_paths.sort();
            let count = mcraw_paths.len();
            for path in mcraw_paths {
                self.load_file(path);
            }
            if count > 0 {
                self.status_message = format!("Imported {} .mcraw files from {}", count, dir.display());
            } else {
                self.status_message = format!("No .mcraw files found in {}", dir.display());
            }
        }
    }

    // -----------------------------------------------------------------------
    // Media pool helpers
    // -----------------------------------------------------------------------

    pub fn focused_file_info(&self) -> Option<&McrawFileInfo> {
        self.imported_files.get(self.media_pool_index).map(|f| &f.info)
    }

    pub fn toggle_media_pool_selection(&mut self) {
        if let Some(f) = self.imported_files.get_mut(self.media_pool_index) {
            f.selected = !f.selected;
        }
    }

    pub fn add_selected_to_queue(&mut self) {
        let selected: Vec<ImportedFile> = self.imported_files.iter()
            .filter(|f| f.selected)
            .cloned()
            .collect();
        if selected.is_empty() {
            self.status_message = "No files selected - use Space to select, then a to add".to_string();
            return;
        }
        let count = selected.len();
        for imp in &selected {
            let already = self.queue.iter().any(|q| q.path == imp.path);
            if !already {
                self.queue.push(QueuedFile {
                    path: imp.path.clone(),
                    info: imp.info.clone(),
                    selected: true,
                    status: QueueStatus::Waiting,
                    progress: 0.0,
                });
            }
        }
        self.status_message = format!("Added {} file(s) to render queue", count);
    }

    pub fn add_all_to_queue(&mut self) {
        if self.imported_files.is_empty() {
            self.status_message = "No files in media pool".to_string();
            return;
        }
        let count = self.imported_files.len();
        for imp in &self.imported_files {
            let already = self.queue.iter().any(|q| q.path == imp.path);
            if !already {
                self.queue.push(QueuedFile {
                    path: imp.path.clone(),
                    info: imp.info.clone(),
                    selected: true,
                    status: QueueStatus::Waiting,
                    progress: 0.0,
                });
            }
        }
        self.status_message = format!("Added all {} file(s) to render queue", count);
    }

    pub fn remove_from_media_pool(&mut self) {
        if self.imported_files.is_empty() {
            return;
        }
        let name = self.imported_files[self.media_pool_index]
            .path
            .split(std::path::MAIN_SEPARATOR)
            .last()
            .unwrap_or("unknown")
            .to_string();
        self.imported_files.remove(self.media_pool_index);
        if self.media_pool_index >= self.imported_files.len() && self.imported_files.len() > 0 {
            self.media_pool_index = self.imported_files.len() - 1;
        }
        self.status_message = format!("Removed {} from media pool", name);
    }

    // -----------------------------------------------------------------------
    // Queue helpers
    // -----------------------------------------------------------------------

    pub fn toggle_queue_selection(&mut self) {
        if let Some(q) = self.queue.get_mut(self.queue_index) {
            q.selected = !q.selected;
        }
    }

    pub fn remove_from_queue(&mut self) {
        if self.queue.is_empty() {
            return;
        }
        let has_selected = self.queue.iter().any(|q| q.selected);
        if has_selected {
            self.queue.retain(|q| !q.selected);
            self.status_message = "Removed selected items from queue".to_string();
        } else {
            let name = self.queue[self.queue_index]
                .path
                .split(std::path::MAIN_SEPARATOR)
                .last()
                .unwrap_or("unknown")
                .to_string();
            self.queue.remove(self.queue_index);
            if self.queue_index >= self.queue.len() && self.queue.len() > 0 {
                self.queue_index = self.queue.len() - 1;
            }
            self.status_message = format!("Removed {} from queue", name);
        }
        if self.queue_index >= self.queue.len() && !self.queue.is_empty() {
            self.queue_index = self.queue.len() - 1;
        }
    }

    pub fn clear_completed_queue(&mut self) {
        let before = self.queue.len();
        self.queue.retain(|q| !matches!(q.status, QueueStatus::Completed | QueueStatus::Failed(_)));
        let removed = before - self.queue.len();
        if removed > 0 {
            self.status_message = format!("Cleared {} completed/failed item(s)", removed);
        } else {
            self.status_message = "No completed/failed items to clear".to_string();
        }
        if self.queue_index >= self.queue.len() && !self.queue.is_empty() {
            self.queue_index = self.queue.len() - 1;
        }
    }

    pub fn render_selected(&mut self) {
        let selected_indices: Vec<usize> = self.queue.iter()
            .enumerate()
            .filter(|(_, q)| q.selected)
            .map(|(i, _)| i)
            .collect();
        if selected_indices.is_empty() {
            self.status_message = "No items selected in queue - use Space to select".to_string();
            return;
        }
        self.status_message = format!("Starting render of {} selected file(s)...", selected_indices.len());
        // Start the first one
        if let Some(&first_idx) = selected_indices.first() {
            self.current_rendering_index = Some(first_idx);
            let q = &self.queue[first_idx];
            self.file_info = Some(q.info.clone());
            self.file_path = Some(q.path.clone());
            self.frame_count = q.info.frame_count as usize;
            self.start_export();
        }
    }

    pub fn render_all(&mut self) {
        if self.queue.is_empty() {
            self.status_message = "Queue is empty".to_string();
            return;
        }
        self.status_message = format!("Starting render of all {} file(s)...", self.queue.len());
        for q in &mut self.queue {
            q.selected = true;
        }
        // Start from the first item
        self.current_rendering_index = Some(0);
        if let Some(q) = self.queue.first() {
            self.file_info = Some(q.info.clone());
            self.file_path = Some(q.path.clone());
            self.frame_count = q.info.frame_count as usize;
            self.start_export();
        }
    }

    fn start_next_queued_render(&mut self) {
        // Find the next selected queue item that's Waiting
        if let Some(current) = self.current_rendering_index {
            let next_idx = (current + 1..self.queue.len())
                .find(|&i| self.queue[i].selected && self.queue[i].status == QueueStatus::Waiting);
            if let Some(idx) = next_idx {
                self.current_rendering_index = Some(idx);
                self.queue[idx].status = QueueStatus::Rendering;
                let q = &self.queue[idx];
                self.file_info = Some(q.info.clone());
                self.file_path = Some(q.path.clone());
                self.frame_count = q.info.frame_count as usize;
                self.start_export();
            } else {
                // No more items to render
                self.current_rendering_index = None;
                let done = self.queue.iter().filter(|q| q.selected && q.status == QueueStatus::Completed).count();
                let total = self.queue.iter().filter(|q| q.selected).count();
                self.status_message = format!("Batch render complete: {}/{} done", done, total);
            }
        }
    }

    // -----------------------------------------------------------------------
    // Export profile helpers
    // -----------------------------------------------------------------------

    pub fn active_profile_is_8bit(&self) -> bool {
        match self.export_codec_family {
            CodecFamily::ProRes => false,
            CodecFamily::DNxHR => false,
            CodecFamily::HEVC => self.hevc_profile.is_8bit(),
            CodecFamily::H264 => self.h264_profile.is_8bit(),
            CodecFamily::AV1 => self.av1_profile.is_8bit(),
            CodecFamily::VP9 => self.vp9_profile.is_8bit(),
        }
    }

    pub fn active_profile_name(&self) -> &'static str {
        match self.export_codec_family {
            CodecFamily::ProRes => self.prores_profile.name(),
            CodecFamily::DNxHR => self.dnxhr_profile.name(),
            CodecFamily::HEVC => self.hevc_profile.name(),
            CodecFamily::H264 => self.h264_profile.name(),
            CodecFamily::AV1 => self.av1_profile.name(),
            CodecFamily::VP9 => self.vp9_profile.name(),
        }
    }

    pub fn cycle_rate_control(&mut self) {
        self.active_rate_control = self.active_rate_control.next();
        self.is_editing_custom_rate = false;
        self.status_message = format!("Rate: {}", self.active_rate_control.name());
    }

    pub fn cycle_codec(&mut self, forward: bool) {
        self.export_codec_family = if forward {
            self.export_codec_family.next()
        } else {
            self.export_codec_family.prev()
        };
        self.export_focus = ExportFocus::CodecFamily;
        self.status_message = format!("Codec: {}", self.export_codec_family.name());
    }

    pub fn cycle_profile(&mut self, forward: bool) {
        match self.export_codec_family {
            CodecFamily::ProRes => {
                self.prores_profile = if forward { self.prores_profile.next() } else { self.prores_profile.prev() };
                self.status_message = format!("Profile: {}", self.prores_profile.name());
            }
            CodecFamily::DNxHR => {
                self.dnxhr_profile = if forward { self.dnxhr_profile.next() } else { self.dnxhr_profile.prev() };
                self.status_message = format!("Profile: {}", self.dnxhr_profile.name());
            }
            CodecFamily::HEVC => {
                self.hevc_profile = if forward { self.hevc_profile.next() } else { self.hevc_profile.prev() };
                self.status_message = format!("Profile: {}", self.hevc_profile.name());
            }
            CodecFamily::H264 => {
                self.h264_profile = if forward { self.h264_profile.next() } else { self.h264_profile.prev() };
                self.status_message = format!("Profile: {}", self.h264_profile.name());
            }
            CodecFamily::AV1 => {
                self.av1_profile = if forward { self.av1_profile.next() } else { self.av1_profile.prev() };
                self.status_message = format!("Profile: {}", self.av1_profile.name());
            }
            CodecFamily::VP9 => {
                self.vp9_profile = if forward { self.vp9_profile.next() } else { self.vp9_profile.prev() };
                self.status_message = format!("Profile: {}", self.vp9_profile.name());
            }
        }
        self.export_focus = ExportFocus::Profile;
    }

    pub fn start_export(&mut self) {
        if self.is_exporting {
            tracing::info!("export cancelled by user (was already exporting)");
            self.cancel_export();
            self.status_message = "Export cancelled. Press V again to restart.".to_string();
            return;
        }
        let info = match self.file_info.clone() {
            Some(i) => i,
            None => {
                tracing::warn!("start_export called with no file loaded");
                self.status_message = "No file loaded".to_string();
                return;
            }
        };

        if self.export_transfer_function.requires_10bit() && self.active_profile_is_8bit() {
            tracing::warn!("export blocked: log/HDR to 8-bit codec not supported");
            self.status_message = "Cannot export Log/HDR to 8-bit codec".to_string();
            return;
        }

        let input_path = std::path::Path::new(&info.path);
        let parent = self.export_folder.clone().unwrap_or_else(|| {
            input_path.parent().unwrap_or_else(|| std::path::Path::new(".")).to_path_buf()
        });
        let stem = input_path.file_stem().and_then(|s| s.to_str()).unwrap_or("output");

        let ext = match self.export_codec_family {
            CodecFamily::ProRes | CodecFamily::DNxHR => "mov",
            CodecFamily::VP9 => "webm",
            _ => "mp4",
        };
        let tf_label = self.export_transfer_function.name().replace([' ', '(', ')', '.'], "");
        let cs_label = self.export_color_space.name().replace([' ', '(', ')', '.'], "");
        let filename = format!("{}_{}_{}.{}", stem, tf_label, cs_label, ext);
        let mut file = parent.join(&filename);
        let mut suffix = 1;
        while file.exists() {
            let base = format!("{}_{}_{}_{}", stem, tf_label, cs_label, suffix);
            file = parent.join(&base).with_extension(ext);
            suffix += 1;
        }
        let output_path = file.to_string_lossy().to_string();
        tracing::info!("export starting: output={} codec={} profile={} rate={}",
            output_path, self.export_codec_family.name(),
            self.active_profile_name(), self.active_rate_control.name());
        let cs = self.export_color_space;
        let tf = self.export_transfer_function;
        let cf = self.export_codec_family;
        let pp = self.prores_profile;
        let dp = self.dnxhr_profile;
        let hp = self.hevc_profile;
        let h4p = self.h264_profile;
        let ap = self.av1_profile;
        let vp = self.vp9_profile;
        let hevc_enc = self.hardware_caps.best_hevc_encoder.clone();
        let h264_enc = self.hardware_caps.best_h264_encoder.clone();
        let av1_enc = self.hardware_caps.best_av1_encoder.clone();
        let prores_enc = self.hardware_caps.best_prores_encoder.clone();

        self.is_exporting = true;
        self.export_cancelled = false;
        self.export_progress = 0.0;
        self.export_start_time = Some(Instant::now());
        // Starting a fresh export — drop any previous summary so the UI
        // switches from the post-render panel back to the live progress
        // panel.
        self.last_export_summary = None;
        // Capture the settings that this export was launched with so the
        // summary stays accurate even if the user cycles the export-settings
        // panel mid-render.
        self.pending_export_summary = Some(ExportSummary {
            output_path: output_path.clone(),
            codec_label: cf.name().to_string(),
            profile_label: self.active_profile_name().to_string(),
            color_space: cs.name().to_string(),
            transfer: tf.name().to_string(),
            rate_control: self.active_rate_control.name(),
            frame_count: info.frame_count as usize,
            elapsed: Duration::default(),
            result: Ok(()),
        });
        // Mark queue item as Rendering
        if let Some(idx) = self.current_rendering_index {
            if idx < self.queue.len() {
                self.queue[idx].status = QueueStatus::Rendering;
            }
        }
        let cancel_flag = Arc::new(AtomicBool::new(false));
        self.cancel_token = Some(cancel_flag.clone());
        let (tx, rx) = mpsc::channel::<ExportEvent>();
        self.export_rx = Some(rx);
        self.status_message = format!(
            "Starting export: {} / {} via {} {} ...",
            cs.name(),
            tf.name(),
            cf.name(),
            self.active_profile_name(),
        );

        let progress_cb = {
            let prog_tx = tx.clone();
            Arc::new(move |pct: f64| { let _ = prog_tx.send(ExportEvent::Progress(pct)); })
        };

        let rate_control = self.active_rate_control.clone();
        let stats = Arc::new(PipelineStats::new());
        let stats_for_event = Arc::clone(&stats);

        std::thread::spawn(move || {
            let result = std::panic::catch_unwind(std::panic::AssertUnwindSafe(|| {
                crate::pipeline::run_export(
                    info, output_path, progress_cb, cancel_flag, stats,
                    cs, tf, cf, pp, dp, hp, h4p, ap, vp,
                    hevc_enc, h264_enc, av1_enc, prores_enc,
                    rate_control,
                )
            }));
            // Always emit stats before Done so the UI can persist them,
            // even on panic/cancel.
            let _ = tx.send(ExportEvent::Stats(stats_for_event));
            match result {
                Ok(export_result) => {
                    let _ = tx.send(ExportEvent::Done(export_result));
                }
                Err(panic) => {
                    tracing::error!("export thread panicked: {:?}", panic);
                    let _ = tx.send(ExportEvent::Done(Err(anyhow::anyhow!("Export thread panicked"))));
                }
            }
        });
    }

    pub fn remove_selected_from_media_pool(&mut self) {
        let has_selected = self.imported_files.iter().any(|f| f.selected);
        if has_selected {
            let count = self.imported_files.iter().filter(|f| f.selected).count();
            self.imported_files.retain(|f| !f.selected);
            if self.media_pool_index >= self.imported_files.len() && !self.imported_files.is_empty() {
                self.media_pool_index = self.imported_files.len() - 1;
            }
            self.status_message = format!("Removed {} selected file(s) from media pool", count);
        } else {
            self.status_message = "No files selected - use Space to select".to_string();
        }
    }

    pub fn set_export_folder(&mut self, folder: std::path::PathBuf) {
        self.export_folder = Some(folder);
        self.status_message = format!("Export folder set");
    }

    pub fn toggle_favourite_folder(&mut self, folder: PathBuf) {
        if let Some(pos) = self.favourite_folders.iter().position(|f| f == &folder) {
            self.favourite_folders.remove(pos);
            self.status_message = "Removed from favourites".to_string();
        } else {
            self.favourite_folders.push(folder);
            self.status_message = "Added to favourites".to_string();
        }
        self.save_favourites();
    }

    // -----------------------------------------------------------------------
    // Export presets
    // -----------------------------------------------------------------------

    /// Snapshot the current export settings as a named preset and persist
    /// the full preset list to disk. If a preset with the same name already
    /// exists it is replaced in place.
    pub fn save_current_as_preset(&mut self, name: String) {
        let name = name.trim().to_string();
        if name.is_empty() {
            self.status_message = "Preset name cannot be empty".to_string();
            return;
        }
        let preset = ExportPreset::snapshot(
            name.clone(),
            self.export_color_space,
            self.export_transfer_function,
            self.export_codec_family,
            self.prores_profile,
            self.dnxhr_profile,
            self.hevc_profile,
            self.h264_profile,
            self.av1_profile,
            self.vp9_profile,
            self.active_rate_control.clone(),
            self.export_folder.clone(),
        );
        ExportPreset::upsert(&mut self.presets, preset);
        ExportPreset::save_all(&self.presets);
        self.active_preset = Some(name.clone());
        self.status_message = format!("Saved preset: {}", name);
    }

    /// Apply the preset at the given index, copying every field onto the
    /// app's live state.
    pub fn apply_preset(&mut self, index: usize) {
        if index >= self.presets.len() {
            return;
        }
        let p = self.presets[index].clone();
        self.export_color_space = p.color_space;
        self.export_transfer_function = p.transfer_function;
        self.export_codec_family = p.codec_family;
        self.prores_profile = p.prores_profile;
        self.dnxhr_profile = p.dnxhr_profile;
        self.hevc_profile = p.hevc_profile;
        self.h264_profile = p.h264_profile;
        self.av1_profile = p.av1_profile;
        self.vp9_profile = p.vp9_profile;
        self.active_rate_control = p.rate_control;
        self.export_folder = p.export_folder;
        // Exit custom-rate edit mode if the preset isn't a custom rate.
        if !matches!(self.active_rate_control, RateControl::Custom(_)) {
            self.is_editing_custom_rate = false;
        }
        self.active_preset = Some(p.name.clone());
        self.status_message = format!("Applied preset: {}", p.name);
    }

    /// Delete the preset at the given index. If that preset was the active
    /// one, clear the active marker.
    pub fn delete_preset(&mut self, index: usize) {
        if index >= self.presets.len() {
            return;
        }
        let removed_name = self.presets[index].name.clone();
        self.presets.remove(index);
        ExportPreset::save_all(&self.presets);
        if self.active_preset.as_deref() == Some(removed_name.as_str()) {
            self.active_preset = None;
        }
        // Keep the cursor in bounds.
        if !self.presets.is_empty() && self.preset_picker.index >= self.presets.len() {
            self.preset_picker.index = self.presets.len() - 1;
        }
        self.preset_picker.message = Some(format!("Deleted preset: {}", removed_name));
        self.status_message = format!("Deleted preset: {}", removed_name);
    }

    /// Open the preset picker overlay. If there are no presets, surface a
    /// hint in the status bar instead of opening an empty list.
    pub fn open_preset_picker(&mut self) {
        if self.presets.is_empty() {
            self.status_message = "No presets yet — press [p] to save the current settings".to_string();
            return;
        }
        self.preset_picker.open = true;
        self.preset_picker.index = self.presets.len().saturating_sub(1).min(self.preset_picker.index);
        self.preset_picker.message = None;
    }

    pub fn close_preset_picker(&mut self) {
        self.preset_picker.open = false;
        self.preset_picker.message = None;
    }

    /// Enter the in-line naming mode for a new preset. The user types the
    /// name and presses Enter to save.
    pub fn begin_naming_preset(&mut self) {
        let default_name = match &self.active_preset {
            Some(n) => format!("{} (copy)", n),
            None => "My Preset".to_string(),
        };
        self.preset_naming = Some(PresetNamingState { name: default_name, message: None });
        self.preset_picker.open = false;
    }

    pub fn cancel_naming_preset(&mut self) {
        self.preset_naming = None;
    }

    /// Finalize naming: save the preset and exit the naming state.
    pub fn commit_naming_preset(&mut self) {
        let name = match self.preset_naming.as_ref() {
            Some(s) => s.name.clone(),
            None => return,
        };
        self.preset_naming = None;
        self.save_current_as_preset(name);
    }

    /// True if the current settings exactly match the named preset (best
    /// effort: only checked for the fields we know about).
    pub fn current_matches_preset(&self, name: &str) -> bool {
        if let Some(p) = self.presets.iter().find(|p| p.name == name) {
            p.color_space == self.export_color_space
                && p.transfer_function == self.export_transfer_function
                && p.codec_family == self.export_codec_family
                && p.prores_profile == self.prores_profile
                && p.dnxhr_profile == self.dnxhr_profile
                && p.hevc_profile == self.hevc_profile
                && p.h264_profile == self.h264_profile
                && p.av1_profile == self.av1_profile
                && p.vp9_profile == self.vp9_profile
                && p.rate_control.name() == self.active_rate_control.name()
                && p.export_folder == self.export_folder
        } else {
            false
        }
    }

    pub fn import_selected_from_browser(&mut self) {
        let paths = self.browser.selected_mcraw_paths();
        if paths.is_empty() {
            self.status_message = "No .mcraw files selected in browser".to_string();
            return;
        }
        let count = paths.len();
        let (imported, failed) = self.load_files_batch(&paths);
        let msg = if failed > 0 {
            format!("Imported {} file(s), {} failed", imported, failed)
        } else {
            format!("Imported {} file(s)", imported)
        };
        self.status_message = msg;
        // Clear selection checkboxes on imported files
        for entry in self.browser.entries.iter_mut() {
            if entry.selected && entry.name.to_lowercase().ends_with(".mcraw") {
                entry.selected = false;
            }
        }
        if count > 0 {
            self.show_browser = false;
        }
    }

    pub fn cancel_export(&mut self) {
        if let Some(ref token) = self.cancel_token {
            tracing::info!("export cancellation requested");
            token.store(true, Ordering::Relaxed);
            self.export_cancelled = true;
            self.status_message = "Cancelling export...".to_string();
        }
    }

    pub fn poll_export(&mut self) {
        let rx = match self.export_rx.take() {
            Some(rx) => rx,
            None => return,
        };
        let mut keep_rx = true;
        while let Ok(event) = rx.try_recv() {
            match event {
                ExportEvent::Progress(pct) => {
                    self.export_progress = pct;
                    if let Some(q) = self.queue.iter_mut().find(|q| matches!(q.status, QueueStatus::Rendering)) {
                        q.progress = pct;
                    }
                }
                ExportEvent::Stats(_stats) => {
                    // Stats are collected internally for future TUI display
                    // (FPS meter, phase timing chart). No terminal output.
                }
                ExportEvent::Done(result) => {
                    self.is_exporting = false;
                    keep_rx = false;
                    self.cancel_token = None;
                    let elapsed = self.export_start_time
                        .take()
                        .map(|t| t.elapsed())
                        .unwrap_or_default();
                    // Mark the currently rendering item
                    if let Some(idx) = self.current_rendering_index {
                        if idx < self.queue.len() {
                            self.queue[idx].progress = 100.0;
                            if self.export_cancelled {
                                self.queue[idx].status = QueueStatus::Waiting;
                            } else {
                                match &result {
                                    Ok(()) => {
                                        self.queue[idx].status = QueueStatus::Completed;
                                    }
                                    Err(e) => {
                                        self.queue[idx].status = QueueStatus::Failed(e.to_string());
                                    }
                                }
                            }
                        }
                    }
                    // Build the post-render summary. Always shown (success,
                    // failure, or cancellation) so the user can see what
                    // ran and for how long.
                    if let Some(mut summary) = self.pending_export_summary.take() {
                        summary.elapsed = elapsed;
                        summary.result = if self.export_cancelled {
                            Err("Cancelled by user".to_string())
                        } else {
                            match &result {
                                Ok(()) => Ok(()),
                                Err(e) => Err(e.to_string()),
                            }
                        };
                        self.last_export_summary = Some(summary);
                    }
                    if self.export_cancelled {
                        self.status_message = "Export cancelled".to_string();
                        self.export_cancelled = false;
                        self.current_rendering_index = None;
                    } else {
                        let mins = elapsed.as_secs() / 60;
                        let secs = elapsed.as_secs() % 60;
                        match result {
                            Ok(()) => {
                                tracing::info!("export completed in {:02}m {:02}s", mins, secs);
                                self.status_message = format!(
                                    "Video export completed ({:02}m {:02}s)", mins, secs
                                );
                                self.shockwave_ticks_remaining = 30;
                            }
                            Err(e) => {
                                tracing::error!("export failed: {}", e);
                                self.status_message = format!("Export failed: {}", e);
                            }
                        }
                        // Auto-start next queued item
                        self.start_next_queued_render();
                    }
                    self.export_start_time = None;
                }
            }
        }
        if keep_rx {
            self.export_rx = Some(rx);
        }
    }

    pub fn add_encode_job(&mut self, format: OutputFormat) {
        let job = EncodeJob::new(uuid::Uuid::new_v4().to_string()[..8].to_string(), format);
        self.encode_jobs.push(job);
        self.status_message = "Export job added".to_string();
    }

    // -----------------------------------------------------------------------
    // Browser navigation
    // -----------------------------------------------------------------------

    pub fn select_file(&mut self) {
        let entry_data = self.browser.selected_entry().map(|e| (e.is_dir, e.name.clone(), e.path.clone()));
        if let Some((is_dir, name, path)) = entry_data {
            if is_dir {
                self.browser.enter();
                self.status_message = format!("Entered: {}", name);
                self.show_favourites_bar = false;
            } else if name.ends_with(".mcraw") {
                let path_str = path.to_string_lossy().to_string();
                self.load_file(path_str);
                self.show_browser = false;
            } else {
                self.status_message = format!("Cannot open: {} (not a .mcraw file)", name);
            }
        }
    }

    /// Scan a folder for all .mcraw files and return sorted paths
    pub fn scan_mcraw_files_in_folder(&self, folder: &str) -> Vec<String> {
        if let Ok(entries) = std::fs::read_dir(folder) {
            let mut files: Vec<String> = entries
                .filter_map(|e| e.ok())
                .map(|e| e.path())
                .filter(|p| p.extension().map_or(false, |ext| ext.to_ascii_lowercase() == "mcraw"))
                .map(|p| p.to_string_lossy().to_string())
                .collect();
            files.sort();
            files
        } else {
            Vec::new()
        }
    }

    pub fn navigate_browser(&mut self, direction: BrowserDirection) {
        match direction {
            BrowserDirection::Up => {
                self.browser.navigate_up();
            }
            BrowserDirection::Down => {
                self.browser.navigate_down();
            }
            BrowserDirection::Enter => self.select_file(),
            BrowserDirection::GoUp => {
                self.browser.go_up();
                self.show_favourites_bar = false;
            }
            BrowserDirection::ToggleHidden => self.browser.toggle_hidden(),
        }
    }

    /// Move the favourites-list cursor by `delta`. Clamps to bounds.
    pub fn navigate_favourites(&mut self, delta: i64) {
        if self.favourite_folders.is_empty() {
            return;
        }
        let cur = self.favourites_scroll_offset.get() as i64;
        let max = (self.favourite_folders.len() as i64) - 1;
        let next = (cur + delta).clamp(0, max);
        self.favourites_scroll_offset.set(next as usize);
    }

    /// Navigate into the favourite at the current cursor position.
    pub fn open_selected_favourite(&mut self) {
        let idx = self.favourites_scroll_offset.get();
        if let Some(path) = self.favourite_folders.get(idx).cloned() {
            self.status_message = format!("Navigated to favourite: {}", path.display());
            self.browser = FileBrowser::from_path(path);
            self.browser_scroll_offset = Cell::new(0);
            self.browsing_favourites = false;
            self.show_favourites_bar = false;
        }
    }

    /// Delete the favourite at the current cursor position.
    pub fn delete_selected_favourite(&mut self) {
        let idx = self.favourites_scroll_offset.get();
        if idx < self.favourite_folders.len() {
            let name = self.favourite_folders[idx].display().to_string();
            self.favourite_folders.remove(idx);
            self.save_favourites();
            if self.favourite_folders.is_empty() {
                self.browsing_favourites = false;
            } else if self.favourites_scroll_offset.get() >= self.favourite_folders.len() {
                self.favourites_scroll_offset.set(self.favourite_folders.len() - 1);
            }
            self.status_message = format!("Removed favourite: {}", name);
        }
    }

    // -----------------------------------------------------------------------
    // Focus cycling
    // -----------------------------------------------------------------------

    pub fn cycle_focus(&mut self) {
        self.focus_target = match self.focus_target {
            FocusTarget::MediaPool => FocusTarget::Preview,
            FocusTarget::Preview => FocusTarget::Grade,
            FocusTarget::Grade => FocusTarget::ExportSettings,
            FocusTarget::ExportSettings => FocusTarget::Queue,
            FocusTarget::Queue => FocusTarget::MediaPool,
        };
        let label = match self.focus_target {
            FocusTarget::MediaPool => "Media Pool",
            FocusTarget::Preview => "Preview",
            FocusTarget::Grade => "Grade",
            FocusTarget::ExportSettings => "Export Settings",
            FocusTarget::Queue => "Render Queue",
        };
        self.status_message = format!("Focus: {}", label);
    }

    pub fn set_focus(&mut self, target: FocusTarget) {
        self.focus_target = target;
        let label = match target {
            FocusTarget::MediaPool => "Media Pool",
            FocusTarget::Preview => "Preview",
            FocusTarget::Grade => "Grade",
            FocusTarget::ExportSettings => "Export Settings",
            FocusTarget::Queue => "Render Queue",
        };
        self.status_message = format!("Focus: {}", label);
    }

}

fn execute_click_action(app: &mut App, action: ClickAction) {
    match action {
        ClickAction::ToggleBrowser => {
            app.show_browser = !app.show_browser;
            app.status_message = if app.show_browser { "Browser shown" } else { "Browser hidden" }.to_string();
        }
        ClickAction::ToggleFileSelection(i) => {
            if let Some(f) = app.imported_files.get_mut(i) {
                f.selected = !f.selected;
            }
        }
        ClickAction::ToggleQueueSelection(i) => {
            if let Some(q) = app.queue.get_mut(i) {
                q.selected = !q.selected;
            }
        }
        ClickAction::SelectMediaPoolItem(i) => {
            if i < app.imported_files.len() {
                app.media_pool_index = i;
                app.set_focus(FocusTarget::MediaPool);
            }
        }
        ClickAction::SelectQueueItem(i) => {
            if i < app.queue.len() {
                app.queue_index = i;
                app.set_focus(FocusTarget::Queue);
            }
        }
        ClickAction::FocusMediaPool => {
            app.set_focus(FocusTarget::MediaPool);
        }
        ClickAction::FocusQueue => {
            app.set_focus(FocusTarget::Queue);
        }
        ClickAction::FocusExport => {
            app.set_focus(FocusTarget::ExportSettings);
        }
        ClickAction::FocusPreview => {
            app.set_focus(FocusTarget::Preview);
        }
        ClickAction::FocusGrade => {
            app.show_grade_screen = !app.show_grade_screen;
            if app.show_grade_screen {
                app.set_focus(FocusTarget::Grade);
                app.status_message = "Grade screen — Esc to exit".to_string();
            } else {
                app.grade_dragging = None;
                app.set_focus(FocusTarget::Preview);
                app.status_message = "Normal view".to_string();
            }
        }
        ClickAction::AddSelectedToQueue => app.add_selected_to_queue(),
        ClickAction::AddAllToQueue => app.add_all_to_queue(),
        ClickAction::RemoveSelectedFromMediaPool => app.remove_selected_from_media_pool(),
        ClickAction::ToggleBrowserSelection(i) => {
            if let Some(entry) = app.browser.entries.get_mut(i) {
                if entry.name.to_lowercase().ends_with(".mcraw") {
                    entry.selected = !entry.selected;
                }
            }
        }
        ClickAction::RenderSelected => app.render_selected(),
        ClickAction::RenderAll => app.render_all(),
        ClickAction::ClearQueue => app.clear_completed_queue(),
        ClickAction::CycleCodec => {
            app.set_focus(FocusTarget::ExportSettings);
            app.cycle_codec(true);
        }
        ClickAction::CycleGamut => {
            app.set_focus(FocusTarget::ExportSettings);
            app.export_focus = ExportFocus::ColorSpace;
            app.export_color_space = app.export_color_space.next();
            app.status_message = format!("Gamut: {}", app.export_color_space.name());
        }
        ClickAction::CycleTransfer => {
            app.set_focus(FocusTarget::ExportSettings);
            app.export_focus = ExportFocus::TransferFunction;
            app.export_transfer_function = app.export_transfer_function.next();
            app.status_message = format!("Transfer: {}", app.export_transfer_function.name());
        }
        ClickAction::CycleProfile => {
            app.set_focus(FocusTarget::ExportSettings);
            app.cycle_profile(true);
        }
        ClickAction::CycleRate => {
            app.set_focus(FocusTarget::ExportSettings);
            app.export_focus = ExportFocus::RateControl;
            app.cycle_rate_control();
        }
        ClickAction::ImportOption1 => {
            if app.import_popup != ImportPopupState::Hidden {
                if let ImportPopupState::DroppedFiles { files, .. } = &app.import_popup {
                    let files = files.clone();
                    if !files.is_empty() {
                        let count = files.len();
                        app.status_message = format!("Importing {} file(s)...", count);
                        let (imported, failed) = app.load_files_batch(&files);
                        if failed > 0 {
                            app.status_message = format!("Imported {} file(s), {} failed", imported, failed);
                        } else {
                            app.status_message = format!("Imported {} file(s)", imported);
                        }
                    }
                    app.import_popup = ImportPopupState::Hidden;
                    app.show_browser = false;
                }
            } else if app.show_browser {
                app.import_selected_from_browser();
            }
        }
        ClickAction::ImportOption2 => {
            if app.import_popup != ImportPopupState::Hidden {
                if let ImportPopupState::DroppedFiles { all_in_folder, .. } = &app.import_popup {
                    let all_in_folder = all_in_folder.clone();
                    if !all_in_folder.is_empty() {
                        let count = all_in_folder.len();
                        app.status_message = format!("Importing all {} file(s) from folder...", count);
                        let (imported, failed) = app.load_files_batch(&all_in_folder);
                        if failed > 0 {
                            app.status_message = format!("Imported {} file(s), {} failed", imported, failed);
                        } else {
                            app.status_message = format!("Imported all {} file(s)", imported);
                        }
                    }
                    app.import_popup = ImportPopupState::Hidden;
                    app.show_browser = false;
                }
            } else if app.show_browser {
                let folder = app.browser.current_path.clone();
                app.load_all_in_folder(&folder);
                app.show_browser = false;
            }
        }
        ClickAction::ClosePopup => { app.import_popup = ImportPopupState::Hidden; }
        ClickAction::ToggleHelp => { app.show_help = !app.show_help; }
        ClickAction::BrowserNavigate(i) => {
            let now = Instant::now();
            let was_same = app.last_browser_click.as_ref().map(|&(_, idx)| idx == i).unwrap_or(false);
            let is_double = app.last_browser_click.as_ref().map(|&(t, _)| now.duration_since(t).as_millis() < 400).unwrap_or(false);

            app.browser.selected_index = i;

            if was_same && is_double {
                app.select_file();
                app.last_browser_click = None;
            } else {
                app.last_browser_click = Some((now, i));
            }
        }
        ClickAction::BrowserSelectAndEnter(i) => {
            let now = Instant::now();
            let was_same = app.last_browser_click.as_ref().map(|&(_, idx)| idx == i).unwrap_or(false);
            let is_double = app.last_browser_click.as_ref().map(|&(t, _)| now.duration_since(t).as_millis() < 400).unwrap_or(false);

            app.browser.selected_index = i;

            if was_same && is_double {
                app.select_file();
                app.last_browser_click = None;
            } else {
                app.last_browser_click = Some((now, i));
            }
        }
        ClickAction::BrowserEnter => {
            app.navigate_browser(BrowserDirection::Enter);
        }
        ClickAction::BrowserGoUp => {
            app.navigate_browser(BrowserDirection::GoUp);
        }
        ClickAction::FavouriteNavigate(i) => {
            if i < app.favourite_folders.len() {
                let path = app.favourite_folders[i].clone();
                app.browser = FileBrowser::from_path(path);
                app.browser_scroll_offset = Cell::new(0);
                app.show_favourites_bar = false;
                app.last_clicked_favourite = Some((Instant::now(), i));
                app.status_message = "Navigated to favourite folder".to_string();
            }
        }
        ClickAction::OpenPresetPicker => {
            app.open_preset_picker();
        }
        ClickAction::GradeSlider(i) => {
            app.grade_focus = i;
            app.set_focus(FocusTarget::Grade);
        }
    }
}

pub enum BrowserDirection {
    Up,
    Down,
    Enter,
    GoUp,
    ToggleHidden,
}

pub async fn run(args: Cli) -> Result<()> {
    let mut app = App::new();
    tracing::info!("app initialized: hardware_caps={:?}", app.hardware_caps);

    match args.resolve() {
        ResolvedCli::Command(CliCommands::Open { file }) => {
            if let Some(path) = file {
                app.load_file(path);
            }
        }
        ResolvedCli::Command(CliCommands::Info { file }) => {
            let path = match file {
                Some(p) => p,
                None => return Err(anyhow::anyhow!("No file specified")),
            };
            match McrawFileInfo::from_path(&path) {
                Ok(mut info) => {
                    info.enhance_with_decoder();
                    return Ok(());
                }
                Err(e) => return Err(e),
            }
        }
        ResolvedCli::Command(CliCommands::Export { file, format, output }) => {
            if file.is_none() {
                return Err(anyhow::anyhow!("No file specified"));
            }
            if let Err(e) = Cli::validate_export_format(&format) {
                anyhow::bail!("{}", e);
            }
            let format = match format.to_lowercase().as_str() {
                "dng" => OutputFormat::DNG { output_path: std::path::PathBuf::from(&output) },
                "prores" => OutputFormat::ProRes { output_path: std::path::PathBuf::from(&output) },
                "h264" => OutputFormat::H264 { output_path: std::path::PathBuf::from(&output) },
                "hevc" => OutputFormat::HEVC { output_path: std::path::PathBuf::from(&output) },
                _ => anyhow::bail!("Invalid format: {}", format),
            };

            let encoder = Encoder::new();
            let mut job = EncodeJob::new("cli-export".to_string(), format.clone());
            job.status = EncodeStatus::Running;

            match encoder.start_job(job.clone()).await {
                Ok(()) => { job.status = EncodeStatus::Completed; }
                Err(e) => { job.status = EncodeStatus::Failed(e.to_string()); }
            }
            return Ok(());
        }
        ResolvedCli::NoFile => {
            app.status_message = "No file specified. Use: mcraw-tui -f <path>".to_string();
        }
    }

    let stdout = std::io::stdout();
    let backend = CrosstermBackend::new(stdout);
    let mut terminal = ratatui::Terminal::new(backend)?;
    terminal.clear()?;
    crossterm::execute!(
        std::io::stdout(),
        EnterAlternateScreen,
        EnableBracketedPaste,
        EnableMouseCapture,
    )?;
    terminal.hide_cursor()?;

    enable_raw_mode()?;
    tracing::info!("terminal initialized: alternate_screen, bracketed_paste, mouse_capture enabled");

    let event_loop_running = Arc::new(AtomicBool::new(true));
    let elr = event_loop_running.clone();

    let (tx, rx) = mpsc::channel();
    tokio::spawn(async move {
        event_loop(tx, elr).await;
    });

    let encoder = Encoder::new();
    tracing::info!("entering main event loop");

    while app.running {
        app.poll_export();
        app.poll_drop_import();
        app.browser.try_refresh();

        // Record render timestamp BEFORE drawing so the FPS meter includes
        // the draw and sleep overhead, giving a realistic "frames the user
        // actually sees" reading.
        app.fps_counter.tick();

        let mut click_regions = Vec::new();
        terminal.draw(|frame| ui::render(frame, &app, &mut click_regions))?;

        // Advance animation state
        app.spinner_frame = app.spinner_frame.wrapping_add(1);
        // Slow the dither animation to ~800ms cycle (every 4th tick)
        if app.spinner_frame % 4 == 0 {
            app.progress_anim_offset = app.progress_anim_offset.wrapping_add(1);
        }
        if app.shockwave_ticks_remaining > 0 {
            app.shockwave_ticks_remaining -= 1;
        }
        // Decay grade morph animation
        if let Some((_, ref mut t)) = app.grade_morph {
            *t = t.saturating_sub(1);
            if *t == 0 { app.grade_morph = None; }
        }
        // Decay phosphor trail
        app.phosphor_trail.iter_mut().for_each(|(_, t)| *t = t.saturating_sub(1));
        app.phosphor_trail.retain(|(_, t)| *t > 0);
        // Decay focus strip idle counter
        if app.grade_strip_idle_ticks > 0 {
            app.grade_strip_idle_ticks = app.grade_strip_idle_ticks.saturating_sub(1);
        } else if app.show_grade_screen {
            app.grade_strip_active = false;
        }

        // Drain ALL pending events each frame — critical for drag-drop where
        // the terminal sends a burst of events that must be consumed together.
        // Processing only one per frame causes input lag and wrong key events
        // leaking through between paste characters.
        while let Ok(event) = rx.try_recv() {
            handle_event(&mut app, event, &encoder, &click_regions).await;
        }

        time::sleep(Duration::from_millis(16)).await;
    }

    event_loop_running.store(false, Ordering::Relaxed);
    drop(rx);
    tokio::task::yield_now().await;

    disable_raw_mode()?;
    terminal.show_cursor()?;
    crossterm::execute!(
        std::io::stdout(),
        DisableMouseCapture,
        DisableBracketedPaste,
        LeaveAlternateScreen,
    )?;
    tracing::info!("terminal shutdown: raw_mode disabled, screen restored");

    Ok(())
}

async fn event_loop(tx: mpsc::Sender<Event>, running: Arc<AtomicBool>) {
    tracing::debug!("event_loop started");
    while running.load(Ordering::Relaxed) {
        if crossterm::event::poll(Duration::from_millis(8)).unwrap() {
            if let Ok(event) = crossterm::event::read() {
                if tx.send(event).is_err() {
                    break;
                }
            }
        }
    }
}

// ---------------------------------------------------------------------------
// Drag-drop path parsing helpers
// ---------------------------------------------------------------------------

/// Strip surrounding quotes from a path string (handles nested quotes).
fn strip_surrounding_quotes(s: &str) -> String {
    let s = s.trim();
    if s.len() >= 2 {
        let first = s.chars().next().unwrap();
        let last = s.chars().last().unwrap();
        if (first == '"' && last == '"') || (first == '\'' && last == '\'') {
            return s[1..s.len() - 1].to_string();
        }
    }
    s.to_string()
}

/// Expand ~ to home directory.
fn expand_tilde(s: &str) -> String {
    if s == "~" {
        if let Some(home) = dirs::home_dir() {
            return home.to_string_lossy().to_string();
        }
    }
    if let Some(rest) = s.strip_prefix("~/") {
        if let Some(home) = dirs::home_dir() {
            return home.join(rest).to_string_lossy().to_string();
        }
    }
    s.to_string()
}

/// Decode file:// URIs to native paths.
/// Handles file:///C:/... (Windows) and file:///home/... (Unix).
fn decode_file_uri(s: &str) -> String {
    if let Some(rest) = s.strip_prefix("file:///") {
        // file:///C:/path → C:/path (Windows) or file:///home → /home (Unix)
        if cfg!(windows) && rest.len() >= 2 {
            let chars: Vec<char> = rest.chars().collect();
            if chars.len() >= 2 && chars[0].is_ascii_alphabetic() && chars[1] == ':' {
                return rest.to_string();
            }
        }
        // Unix: file:///home/user → /home/user
        return format!("/{}", rest);
    }
    if let Some(rest) = s.strip_prefix("file://") {
        // file://hostname/path (network paths) — strip hostname
        if let Some(slash_pos) = rest.find('/') {
            return rest[slash_pos..].to_string();
        }
        return rest.to_string();
    }
    s.to_string()
}

/// Percent-decode URI-encoded characters (e.g. %20 → space, %C3%A9 → é).
fn percent_decode_path(s: &str) -> String {
    if !s.contains('%') {
        return s.to_string();
    }
    match percent_decode_str(s).decode_utf8() {
        Ok(decoded) => decoded.into_owned(),
        Err(_) => s.to_string(), // Fall back to original if decoding fails
    }
}

/// Normalize path separators for the current platform.
fn normalize_path(s: &str) -> String {
    if cfg!(windows) {
        // Preserve UNC paths (\\server\share)
        if s.starts_with("\\\\") {
            return s.to_string();
        }
        // Convert forward slashes to backslashes
        s.replace('/', "\\")
    } else {
        s.to_string()
    }
}

/// Validate and canonicalize a path. Returns None if path doesn't exist.
fn validate_path(s: &str) -> Option<String> {
    let path = std::path::Path::new(s);

    // Check if path exists
    if !path.exists() {
        tracing::debug!("path validation: does not exist: {}", s);
        return None;
    }

    // Try to canonicalize (resolves symlinks and normalizes)
    // Fall back to original if canonicalization fails
    match path.canonicalize() {
        Ok(canonical) => Some(canonical.to_string_lossy().to_string()),
        Err(_) => {
            tracing::debug!("path validation: canonicalize failed, using original: {}", s);
            Some(s.to_string())
        }
    }
}

async fn handle_event(app: &mut App, event: Event, _encoder: &Encoder, click_regions: &[ui::ClickRegion]) {
    match event {
        // -------------------------------------------------------------------
        // Drag & Drop: pasted file paths
        // -------------------------------------------------------------------
        Event::Paste(pasted) => {
            tracing::trace!("drag-drop: raw pasted bytes={:?} len={}", pasted.as_bytes(), pasted.len());

            let paths: Vec<String> = pasted
                .lines()
                .filter_map(|line| {
                    let line = line.trim();
                    if line.is_empty() {
                        return None;
                    }

                    // Strip surrounding quotes (handles "path with spaces")
                    let stripped = strip_surrounding_quotes(line);

                    // Expand ~ to home directory
                    let expanded = expand_tilde(&stripped);

                    // Decode file:// URI if present
                    let decoded = decode_file_uri(&expanded);

                    // Percent-decode URI-encoded characters (e.g. %20 → space, %C3%A9 → é)
                    let percent_decoded = percent_decode_path(&decoded);

                    // Platform-specific path normalization
                    let normalized = normalize_path(&percent_decoded);

                    // Validate path exists and canonicalize
                    validate_path(&normalized)
                })
                .collect();

            tracing::trace!("drag-drop: parsed {} paths: {:?}", paths.len(), paths);

            if paths.is_empty() {
                app.status_message = "Drag-drop: no valid paths received".to_string();
                return;
            }

            // Separate .mcraw files and directories
            let mut mcraw_files: Vec<String> = Vec::new();
            let mut folders: Vec<String> = Vec::new();

            for p in &paths {
                let path = std::path::Path::new(p);
                if path.is_dir() {
                    folders.push(p.clone());
                } else if p.to_lowercase().ends_with(".mcraw") {
                    mcraw_files.push(p.clone());
                }
            }

            // If folders were dropped, scan them for .mcraw files
            for folder in &folders {
                if let Ok(entries) = std::fs::read_dir(folder) {
                    let mut files: Vec<String> = entries
                        .filter_map(|e| e.ok())
                        .map(|e| e.path())
                        .filter(|p| p.extension().map_or(false, |ext| ext.to_ascii_lowercase() == "mcraw"))
                        .map(|p| p.to_string_lossy().to_string())
                        .collect();
                    files.sort();
                    mcraw_files.extend(files);
                }
            }

            // Deduplicate while preserving order
            let mut seen = std::collections::HashSet::new();
            mcraw_files.retain(|f| seen.insert(f.clone()));

            tracing::info!("drag-drop: {} .mcraw files, {} folders", mcraw_files.len(), folders.len());

            if mcraw_files.is_empty() {
                app.status_message = "Drag-drop: no .mcraw files found in dropped items".to_string();
                return;
            }

            // Trigger visual feedback
            app.drop_highlight = Some(Instant::now());

            // Smart import: instant for small batches, async for larger ones
            // Threshold: <= 3 files = async (smooth UI), > 3 = popup for confirmation
            const ASYNC_THRESHOLD: usize = 3;

            if mcraw_files.len() <= ASYNC_THRESHOLD && folders.is_empty() {
                // Small batch: use async import for smooth UI
                app.start_async_import(mcraw_files);
            } else {
                // Large batch or folders: show import popup
                // Check if single file is alone in its folder
                if mcraw_files.len() == 1 {
                    let file = &mcraw_files[0];
                    let folder = std::path::Path::new(file)
                        .parent()
                        .map(|p| p.to_string_lossy().to_string())
                        .unwrap_or_else(|| ".".to_string());

                    let all_in_folder: Vec<String> = if let Ok(entries) = std::fs::read_dir(&folder) {
                        let mut files: Vec<String> = entries
                            .filter_map(|e| e.ok())
                            .map(|e| e.path())
                            .filter(|p| p.extension().map_or(false, |ext| ext.to_ascii_lowercase() == "mcraw"))
                            .map(|p| p.to_string_lossy().to_string())
                            .collect();
                        files.sort();
                        files
                    } else {
                        Vec::new()
                    };

                    // Only skip popup if this is truly the only .mcraw in the folder
                    if all_in_folder.len() == 1 {
                        app.start_async_import(mcraw_files);
                        return;
                    }
                }

                // Determine the primary folder for the import popup
                let folder = if !folders.is_empty() {
                    folders[0].clone()
                } else {
                    std::path::Path::new(&mcraw_files[0])
                        .parent()
                        .map(|p| p.to_string_lossy().to_string())
                        .unwrap_or_else(|| ".".to_string())
                };

                // Scan ALL .mcraw files in the primary folder
                let all_in_folder: Vec<String> = if let Ok(entries) = std::fs::read_dir(&folder) {
                    let mut files: Vec<String> = entries
                        .filter_map(|e| e.ok())
                        .map(|e| e.path())
                        .filter(|p| p.extension().map_or(false, |ext| ext.to_ascii_lowercase() == "mcraw"))
                        .map(|p| p.to_string_lossy().to_string())
                        .collect();
                    files.sort();
                    files
                } else {
                    Vec::new()
                };

                // Show import popup
                app.import_popup = ImportPopupState::DroppedFiles {
                    files: mcraw_files,
                    folder,
                    all_in_folder,
                };
            }
        }

        // -------------------------------------------------------------------
        // Mouse events
        // -------------------------------------------------------------------
        Event::Mouse(mouse_event) => {
            use crossterm::event::{MouseEventKind, MouseButton};

            // Allow mouse on import popup (has its own click regions)
            if app.import_popup != ImportPopupState::Hidden {
                let col = mouse_event.column;
                let row = mouse_event.row;
                match mouse_event.kind {
                    MouseEventKind::Down(MouseButton::Left) => {
                        for region in click_regions.iter().rev() {
                            if col >= region.area.x && col < region.area.x + region.area.width
                                && row >= region.area.y && row < region.area.y + region.area.height {
                                match &region.action {
                                    ClickAction::ImportOption1 | ClickAction::ImportOption2 => {
                                        execute_click_action(app, region.action.clone());
                                    }
                                    _ => {}
                                }
                                break;
                            }
                        }
                    }
                    _ => {}
                }
                return;
            }

            // Block mouse events when full info overlay is active
            if app.show_full_info {
                return;
            }

            match mouse_event.kind {
                MouseEventKind::ScrollUp => {
                    if app.show_help {
                        app.help_scroll = app.help_scroll.saturating_sub(1);
                    } else if app.show_browser {
                        if app.browsing_favourites {
                            app.navigate_favourites(-1);
                        } else if app.browser.selected_index > 0 {
                            app.browser.selected_index -= 1;
                        }
                    } else {
                        match app.focus_target {
                            FocusTarget::MediaPool => { if app.media_pool_index > 0 { app.media_pool_index -= 1; } }
                            FocusTarget::Queue => { if app.queue_index > 0 { app.queue_index -= 1; } }
                            FocusTarget::ExportSettings => {
                                // Cycle VALUES of the currently focused setting
                                match app.export_focus {
                                    ExportFocus::CodecFamily => app.cycle_codec(false),
                                    ExportFocus::ColorSpace => {
                                        app.export_color_space = app.export_color_space.prev();
                                        app.status_message = format!("Gamut: {}", app.export_color_space.name());
                                    }
                                    ExportFocus::TransferFunction => {
                                        app.export_transfer_function = app.export_transfer_function.prev();
                                        app.status_message = format!("Transfer: {}", app.export_transfer_function.name());
                                    }
                                    ExportFocus::Profile => app.cycle_profile(false),
                                    ExportFocus::RateControl => {
                                        app.active_rate_control = app.active_rate_control.prev();
                                        app.status_message = format!("Rate: {}", app.active_rate_control.name());
                                    }
                                }
                            }
                            FocusTarget::Preview => {}
                            FocusTarget::Grade => {
                                let step = if mouse_event.modifiers.contains(crossterm::event::KeyModifiers::SHIFT) {
                                    GradeSliders::step_large(app.grade_focus)
                                } else {
                                    GradeSliders::step_small(app.grade_focus)
                                };
                                app.grade_sliders.apply_delta(app.grade_focus, step);
                            }
                        }
                    }
                }
                MouseEventKind::ScrollDown => {
                    if app.show_help {
                        app.help_scroll = app.help_scroll.saturating_add(1);
                    } else if app.show_browser {
                        if app.browsing_favourites {
                            app.navigate_favourites(1);
                        } else {
                            let len = app.browser.entries.len();
                            if len > 0 { app.browser.selected_index = (app.browser.selected_index + 1).min(len - 1); }
                        }
                    } else {
                        match app.focus_target {
                            FocusTarget::MediaPool => {
                                let len = app.imported_files.len();
                                if len > 0 { app.media_pool_index = (app.media_pool_index + 1).min(len - 1); }
                            }
                            FocusTarget::Queue => {
                                let len = app.queue.len();
                                if len > 0 { app.queue_index = (app.queue_index + 1).min(len - 1); }
                            }
                            FocusTarget::ExportSettings => {
                                match app.export_focus {
                                    ExportFocus::CodecFamily => app.cycle_codec(true),
                                    ExportFocus::ColorSpace => {
                                        app.export_color_space = app.export_color_space.next();
                                        app.status_message = format!("Gamut: {}", app.export_color_space.name());
                                    }
                                    ExportFocus::TransferFunction => {
                                        app.export_transfer_function = app.export_transfer_function.next();
                                        app.status_message = format!("Transfer: {}", app.export_transfer_function.name());
                                    }
                                    ExportFocus::Profile => app.cycle_profile(true),
                                    ExportFocus::RateControl => app.cycle_rate_control(),
                                }
                            }
                            FocusTarget::Preview => {}
                            FocusTarget::Grade => {
                                let step = if mouse_event.modifiers.contains(crossterm::event::KeyModifiers::SHIFT) {
                                    GradeSliders::step_large(app.grade_focus)
                                } else {
                                    GradeSliders::step_small(app.grade_focus)
                                };
                                app.grade_sliders.apply_delta(app.grade_focus, -step);
                            }
                        }
                    }
                }
                MouseEventKind::Down(MouseButton::Left) => {
                    let col = mouse_event.column;
                    let row = mouse_event.row;
                    for region in click_regions.iter().rev() {
                        if col >= region.area.x && col < region.area.x + region.area.width
                            && row >= region.area.y && row < region.area.y + region.area.height {
                            match &region.action {
                                ClickAction::GradeSlider(i) => {
                                    let now = Instant::now();
                                    let is_double = app.last_grade_click.as_ref()
                                        .map(|&(t, idx)| idx == *i && now.duration_since(t).as_millis() < 400)
                                        .unwrap_or(false);
                                    if is_double {
                                        // Double-click: reset to default
                                        let def = GradeSliders::default_val(*i);
                                        app.grade_sliders.set(*i, def);
                                        app.last_grade_click = None;
                                        app.status_message = format!("Reset {} to default", GradeSliders::name(*i));
                                    } else {
                                        // Single click: set value from x position + start drag
                                        let x_offset = col.saturating_sub(region.area.x);
                                        let norm = (x_offset as f32 / region.area.width.max(1) as f32).clamp(0.0, 1.0);
                                        let lo = GradeSliders::min(*i);
                                        let hi = GradeSliders::max(*i);
                                        app.grade_sliders.set(*i, lo + norm * (hi - lo));
                                        app.grade_focus = *i;
                                        app.grade_dragging = Some((*i, region.area.x, region.area.width));
                                        app.last_grade_click = Some((now, *i));
                                    }
                                }
                                _ => execute_click_action(app, region.action.clone()),
                            }
                            break;
                        }
                    }
                }
                MouseEventKind::Drag(MouseButton::Left) => {
                    if let Some((i, track_x, track_w)) = app.grade_dragging {
                        let col = mouse_event.column;
                        let x_offset = col.saturating_sub(track_x);
                        let norm = (x_offset as f32 / track_w.max(1) as f32).clamp(0.0, 1.0);
                        let lo = GradeSliders::min(i);
                        let hi = GradeSliders::max(i);
                        app.grade_sliders.set(i, lo + norm * (hi - lo));
                    }
                }
                MouseEventKind::Up(MouseButton::Left) => {
                    app.grade_dragging = None;
                }
                _ => {}
            }
        }

        // -------------------------------------------------------------------
        // Keyboard events
        // -------------------------------------------------------------------
        Event::Key(key_event) if key_event.kind == KeyEventKind::Press => {
            if let crossterm::event::KeyCode::Char('c') = key_event.code {
                if key_event.modifiers.contains(crossterm::event::KeyModifiers::CONTROL) {
                    tracing::info!("ctrl+c received, quitting");
                    app.running = false;
                    return;
                }
            }
            // Ctrl+X cancels an in-progress export. Outside of an export it
            // is a no-op so it never accidentally trashes the queue.
            if let crossterm::event::KeyCode::Char('x') = key_event.code {
                if key_event.modifiers.contains(crossterm::event::KeyModifiers::CONTROL) {
                    if app.is_exporting {
                        tracing::info!("ctrl+x received, cancelling export");
                        app.cancel_export();
                    }
                    return;
                }
            }

            tracing::debug!("key event: code={:?} modifiers={:?}", key_event.code, key_event.modifiers);

            // ----------------------------------------------------------------
            // Preset naming (inline text entry)
            // ----------------------------------------------------------------
            if app.preset_naming.is_some() {
                let naming = app.preset_naming.clone().unwrap();
                match key_event.code {
                    crossterm::event::KeyCode::Char(c) => {
                        if let Some(state) = app.preset_naming.as_mut() {
                            state.name.push(c);
                        }
                    }
                    crossterm::event::KeyCode::Backspace => {
                        if let Some(state) = app.preset_naming.as_mut() {
                            state.name.pop();
                        }
                    }
                    crossterm::event::KeyCode::Enter => {
                        app.commit_naming_preset();
                    }
                    crossterm::event::KeyCode::Esc => {
                        app.cancel_naming_preset();
                        app.status_message = "Preset save cancelled".to_string();
                    }
                    _ => {}
                }
                let _ = naming; // Silence unused warning if not used.
                return;
            }

            // ----------------------------------------------------------------
            // Preset picker overlay
            // ----------------------------------------------------------------
            if app.preset_picker.open {
                match key_event.code {
                    crossterm::event::KeyCode::Esc => app.close_preset_picker(),
                    crossterm::event::KeyCode::Up | crossterm::event::KeyCode::Char('k') => {
                        if app.preset_picker.index > 0 {
                            app.preset_picker.index -= 1;
                        }
                        app.preset_picker.message = None;
                    }
                    crossterm::event::KeyCode::Down | crossterm::event::KeyCode::Char('j') => {
                        if app.preset_picker.index + 1 < app.presets.len() {
                            app.preset_picker.index += 1;
                        }
                        app.preset_picker.message = None;
                    }
                    crossterm::event::KeyCode::Enter => {
                        let idx = app.preset_picker.index;
                        app.close_preset_picker();
                        app.apply_preset(idx);
                    }
                    crossterm::event::KeyCode::Delete | crossterm::event::KeyCode::Backspace => {
                        let idx = app.preset_picker.index;
                        app.delete_preset(idx);
                    }
                    _ => {}
                }
                return;
            }

            // ----------------------------------------------------------------
            // Import popup
            // ----------------------------------------------------------------
            if app.import_popup != ImportPopupState::Hidden {
                let has_option2 = if let ImportPopupState::DroppedFiles { files, all_in_folder, .. } = &app.import_popup {
                    all_in_folder.len() > files.len()
                } else {
                    false
                };

                match key_event.code {
                    crossterm::event::KeyCode::Char('1') => {
                        let files = if let ImportPopupState::DroppedFiles { files, .. } = &app.import_popup {
                            files.clone()
                        } else {
                            Vec::new()
                        };
                        if !files.is_empty() {
                            let count = files.len();
                            app.status_message = format!("Importing {} file(s)...", count);
                            let (imported, failed) = app.load_files_batch(&files);
                            if failed > 0 {
                                app.status_message = format!("Imported {} file(s), {} failed", imported, failed);
                            } else {
                                app.status_message = format!("Imported {} file(s)", imported);
                            }
                        }
                        app.import_popup = ImportPopupState::Hidden;
                        app.show_browser = false;
                    }
                    crossterm::event::KeyCode::Char('2') if has_option2 => {
                        let all_in_folder = if let ImportPopupState::DroppedFiles { all_in_folder, .. } = &app.import_popup {
                            all_in_folder.clone()
                        } else {
                            Vec::new()
                        };
                        if !all_in_folder.is_empty() {
                            let count = all_in_folder.len();
                            app.status_message = format!("Importing all {} file(s) from folder...", count);
                            let (imported, failed) = app.load_files_batch(&all_in_folder);
                            if failed > 0 {
                                app.status_message = format!("Imported {} file(s), {} failed", imported, failed);
                            } else {
                                app.status_message = format!("Imported all {} file(s)", imported);
                            }
                        }
                        app.import_popup = ImportPopupState::Hidden;
                        app.show_browser = false;
                    }
                    crossterm::event::KeyCode::Enter => {
                        let files = if let ImportPopupState::DroppedFiles { files, .. } = &app.import_popup {
                            files.clone()
                        } else {
                            Vec::new()
                        };
                        if !files.is_empty() {
                            let count = files.len();
                            app.status_message = format!("Importing {} file(s)...", count);
                            let (imported, failed) = app.load_files_batch(&files);
                            if failed > 0 {
                                app.status_message = format!("Imported {} file(s), {} failed", imported, failed);
                            } else {
                                app.status_message = format!("Imported {} file(s)", imported);
                            }
                        }
                        app.import_popup = ImportPopupState::Hidden;
                        app.show_browser = false;
                    }
                    crossterm::event::KeyCode::Esc => {
                        app.import_popup = ImportPopupState::Hidden;
                    }
                    _ => {}
                }
                return;
            }

            // ----------------------------------------------------------------
            // Custom rate inline editing
            // ----------------------------------------------------------------
            if app.is_editing_custom_rate {
                match key_event.code {
                    crossterm::event::KeyCode::Char(c) => {
                        if c.is_ascii_alphanumeric() || c == '_' || c == '-' || c == 'M' || c == 'k' || c == 'm' {
                            if let RateControl::Custom(ref mut val) = app.active_rate_control {
                                val.push(c);
                            }
                        }
                    }
                    crossterm::event::KeyCode::Backspace => {
                        if let RateControl::Custom(ref mut val) = app.active_rate_control {
                            val.pop();
                        }
                    }
                    crossterm::event::KeyCode::Enter | crossterm::event::KeyCode::Esc => {
                        app.is_editing_custom_rate = false;
                        app.status_message = format!("Rate: {}", app.active_rate_control.name());
                    }
                    _ => {}
                }
                return;
            }

            // ----------------------------------------------------------------
            // Normal character-key dispatch
            // ----------------------------------------------------------------
            if let crossterm::event::KeyCode::Char(c) = key_event.code {
                match c {
                    'q' => {
                        app.running = false;
                    }
                    '?' => {
                        app.show_help = !app.show_help;
                    }
                    'b' => {
                        // In grade mode, 'b' does before/after; otherwise browser toggle
                        if app.show_grade_screen || app.focus_target == FocusTarget::Grade {
                            if app.grade_before_snapshot.is_none() {
                                app.grade_before_snapshot = Some(app.grade_sliders);
                                app.grade_sliders = GradeSliders::default();
                                app.shockwave_ticks_remaining = 8;
                                app.status_message = "BEFORE — holding original values".to_string();
                            }
                        } else {
                            app.show_browser = !app.show_browser;
                            app.status_message = if app.show_browser {
                                "Browser shown"
                            } else {
                                "Browser hidden"
                            }.to_string();
                        }
                    }
                    'B' => {
                        // Release before/after: restore snapshot
                        if let Some(snap) = app.grade_before_snapshot.take() {
                            app.grade_sliders = snap;
                            app.shockwave_ticks_remaining = 5;
                            app.status_message = "AFTER — restored grade".to_string();
                        }
                    }
                    'e' => {
                        app.set_focus(FocusTarget::ExportSettings);
                    }
                    'a' => {
                        app.add_selected_to_queue();
                    }
                    'A' => {
                        app.add_all_to_queue();
                    }
                    'D' => {
                        if app.focus_target == FocusTarget::MediaPool {
                            app.remove_selected_from_media_pool();
                        }
                    }
                    'd' => {
                        // Remove the last-clicked favourite (within 2 seconds)
                        if app.show_browser && app.show_favourites_bar {
                            if let Some((ts, idx)) = app.last_clicked_favourite.take() {
                                if ts.elapsed() < Duration::from_secs(2) && idx < app.favourite_folders.len() {
                                    app.favourite_folders.remove(idx);
                                    app.status_message = "Removed from favourites".to_string();
                                    app.save_favourites();
                                    return;
                                }
                            }
                        }
                        match app.focus_target {
                            FocusTarget::MediaPool => app.remove_from_media_pool(),
                            FocusTarget::Queue => app.remove_from_queue(),
                            FocusTarget::ExportSettings => {}
                            FocusTarget::Preview => {}
                            FocusTarget::Grade => {}
                        }
                    }
                    'x' => {
                        // When an export is running, `x` (and Ctrl+X) cancel it.
                        // Otherwise it clears completed/failed items from the queue.
                        if app.is_exporting {
                            app.cancel_export();
                        } else {
                            app.clear_completed_queue();
                        }
                    }
                    'X' => {
                        if app.is_exporting {
                            app.cancel_export();
                        } else {
                            app.clear_completed_queue();
                        }
                    }
                    'v' => {
                        app.render_selected();
                    }
                    'R' => {
                        app.render_all();
                    }
                    'r' => {
                        if app.show_grade_screen || app.focus_target == FocusTarget::Grade {
                            let def = GradeSliders::default_val(app.grade_focus);
                            app.grade_sliders.set(app.grade_focus, def);
                            app.status_message = format!("Reset {} to default", GradeSliders::name(app.grade_focus));
                            app.grade_strip_active = true;
                            app.grade_strip_idle_ticks = 15;
                        } else if app.focus_target == FocusTarget::ExportSettings {
                            app.export_focus = ExportFocus::RateControl;
                            app.cycle_rate_control();
                        }
                    }
                    't' => {
                        if app.focus_target == FocusTarget::ExportSettings {
                            app.export_focus = ExportFocus::TransferFunction;
                            app.export_transfer_function = app.export_transfer_function.next();
                            app.status_message = format!("Transfer: {}", app.export_transfer_function.name());
                        }
                    }
                    'g' => {
                        if app.focus_target == FocusTarget::ExportSettings {
                            app.export_focus = ExportFocus::ColorSpace;
                            app.export_color_space = app.export_color_space.next();
                            app.status_message = format!("Gamut: {}", app.export_color_space.name());
                        }
                    }
                    'c' => {
                        if app.focus_target == FocusTarget::ExportSettings {
                            app.cycle_codec(true);
                        }
                    }
                    'o' => {
                        if app.show_browser {
                            app.set_export_folder(app.browser.current_path.clone());
                        }
                    }
                    'f' => {
                        if app.show_browser {
                            // `f` toggles between the normal folder view and
                            // a flat list of favourite folders. The bar at
                            // the top of the browser (when visible) is still
                            // mouse-only; this gives a keyboard-first path
                            // through the favourites and also fixes the
                            // `..` occlusion bug because the favourites are
                            // rendered through the normal list widget.
                            if app.browsing_favourites {
                                app.browsing_favourites = false;
                                app.status_message = "Folder view".to_string();
                            } else if app.favourite_folders.is_empty() {
                                app.status_message = "No favourites yet — press [F] to add the current folder".to_string();
                            } else {
                                app.browsing_favourites = true;
                                app.favourites_scroll_offset = Cell::new(0);
                                app.status_message = "Favourites view (press [f] or [Esc] to return)".to_string();
                            }
                        }
                    }
                    'F' => {
                        if app.show_browser {
                            app.toggle_favourite_folder(app.browser.current_path.clone());
                        }
                    }
                    'i' => {
                        if app.focus_target == FocusTarget::ExportSettings
                            && matches!(app.active_rate_control, RateControl::Custom(_))
                        {
                            app.is_editing_custom_rate = !app.is_editing_custom_rate;
                            if app.is_editing_custom_rate {
                                app.status_message = "Type a rate value (e.g. 20, 400M, 50000k). Press Enter to confirm, Esc to cancel.".to_string();
                            }
                        } else {
                            app.show_full_info = !app.show_full_info;
                            if app.show_full_info {
                                app.status_message = "Full file info shown (press i or Esc to close)".to_string();
                            }
                        }
                    }
                    'p' => {
                        if app.focus_target == FocusTarget::ExportSettings {
                            // Save the current export settings as a new preset.
                            app.begin_naming_preset();
                        } else {
                            app.cycle_profile(true);
                        }
                    }
                    'P' => {
                        // Open the preset picker (regardless of focus —
                        // most useful from the Export Settings panel but
                        // works from anywhere for power users).
                        app.open_preset_picker();
                    }
                    's' => {
                        app.status_message = "Settings (coming soon)".to_string();
                    }
                    'n' => {
                        if let Some(info) = app.focused_file_info().cloned().or_else(|| app.file_info.clone()) {
                            let output_path = "naked_dump.raw";
                            app.status_message = "Starting naked raw dump...".to_string();
                            match crate::pipeline::run_naked(&info, output_path) {
                                Ok(_) => {
                                    app.status_message = format!("Naked dump done: {}", output_path);
                                }
                                Err(e) => {
                                    app.status_message = format!("Naked dump failed: {}", e);
                                }
                            }
                        }
                    }
                    '.' => {
                        if app.show_browser {
                            app.browser.toggle_hidden();
                            app.status_message = if app.browser.show_hidden {
                                "Showing hidden files"
                            } else {
                                "Hiding hidden files"
                            }.to_string();
                        }
                    }
                    'L' => {
                        let folder = app.browser.current_path.clone();
                        app.load_all_in_folder(&folder);
                        app.show_browser = false;
                    }
                    'I' => {
                        if app.show_browser {
                            app.import_selected_from_browser();
                        }
                    }
                    'C' => {
                        if !app.imported_files.is_empty() {
                            app.show_culling = !app.show_culling;
                            app.status_message = if app.show_culling { "Culling mode" } else { "Normal mode" }.to_string();
                        }
                    }
                    'G' => {
                        app.show_grade_screen = !app.show_grade_screen;
                        if app.show_grade_screen {
                            app.set_focus(FocusTarget::Grade);
                            app.status_message = "Grade screen — Esc to exit".to_string();
                        } else {
                            app.grade_dragging = None;
                            app.set_focus(FocusTarget::Preview);
                            app.status_message = "Normal view".to_string();
                        }
                    }
                    _ => {}
                }
            }

            // ----------------------------------------------------------------
            // Non-character keys
            // ----------------------------------------------------------------
            match key_event.code {
                crossterm::event::KeyCode::Esc => {
                    if app.import_popup != ImportPopupState::Hidden {
                        app.import_popup = ImportPopupState::Hidden;
                    } else if app.show_full_info {
                        app.show_full_info = false;
                    } else if app.browsing_favourites {
                        app.browsing_favourites = false;
                        app.status_message = "Folder view".to_string();
                    } else if app.show_browser {
                        app.show_browser = false;
                    } else if app.show_grade_screen {
                        app.show_grade_screen = false;
                        app.grade_dragging = None;
                        app.set_focus(FocusTarget::Preview);
                        app.status_message = "Normal view".to_string();
                    } else if app.show_help {
                        app.show_help = false;
                    } else {
                        app.running = false;
                    }
                }
                crossterm::event::KeyCode::Delete => {
                    if app.browsing_favourites {
                        app.delete_selected_favourite();
                    }
                }
                crossterm::event::KeyCode::Tab => {
                    app.cycle_focus();
                }
                crossterm::event::KeyCode::Enter => {
                    if app.focus_target == FocusTarget::ExportSettings
                        && matches!(app.active_rate_control, RateControl::Custom(_))
                    {
                        app.is_editing_custom_rate = !app.is_editing_custom_rate;
                        if app.is_editing_custom_rate {
                            app.status_message = "Type a rate value. Enter to confirm, Esc to cancel.".to_string();
                        }
                    } else if app.browsing_favourites {
                        app.open_selected_favourite();
                    } else if app.show_browser {
                        app.navigate_browser(BrowserDirection::Enter);
                    }
                }
                crossterm::event::KeyCode::Right | crossterm::event::KeyCode::Char('l') => {
                    if app.focus_target == FocusTarget::Grade {
                        let step = if key_event.modifiers.contains(crossterm::event::KeyModifiers::SHIFT) {
                            GradeSliders::step_large(app.grade_focus)
                        } else {
                            GradeSliders::step_small(app.grade_focus)
                        };
                        let old_norm = app.grade_sliders.normalized(app.grade_focus);
                        app.grade_sliders.apply_delta(app.grade_focus, step);
                        app.phosphor_trail.push((old_norm, 4));
                        app.grade_strip_active = true;
                        app.grade_strip_idle_ticks = 15;
                    } else if app.frame_index < app.frame_count.saturating_sub(1) {
                        app.frame_index += 1;
                    }
                }
                crossterm::event::KeyCode::Left | crossterm::event::KeyCode::Char('h') => {
                    if app.focus_target == FocusTarget::Grade {
                        let step = if key_event.modifiers.contains(crossterm::event::KeyModifiers::SHIFT) {
                            GradeSliders::step_large(app.grade_focus)
                        } else {
                            GradeSliders::step_small(app.grade_focus)
                        };
                        let old_norm = app.grade_sliders.normalized(app.grade_focus);
                        app.grade_sliders.apply_delta(app.grade_focus, -step);
                        app.phosphor_trail.push((old_norm, 4));
                        app.grade_strip_active = true;
                        app.grade_strip_idle_ticks = 15;
                    } else if app.frame_index > 0 {
                        app.frame_index -= 1;
                    }
                }
                crossterm::event::KeyCode::Char('L') => {
                    if app.focus_target == FocusTarget::Grade {
                        let step = GradeSliders::step_large(app.grade_focus);
                        let old_norm = app.grade_sliders.normalized(app.grade_focus);
                        app.grade_sliders.apply_delta(app.grade_focus, step);
                        app.phosphor_trail.push((old_norm, 4));
                        app.grade_strip_active = true;
                        app.grade_strip_idle_ticks = 15;
                    } else {
                        let jump = 10.min(app.frame_count.saturating_sub(app.frame_index + 1));
                        app.frame_index = app.frame_index.saturating_add(jump);
                    }
                }
                crossterm::event::KeyCode::Char('H') => {
                    if app.focus_target == FocusTarget::Grade {
                        let step = GradeSliders::step_large(app.grade_focus);
                        let old_norm = app.grade_sliders.normalized(app.grade_focus);
                        app.grade_sliders.apply_delta(app.grade_focus, -step);
                        app.phosphor_trail.push((old_norm, 4));
                        app.grade_strip_active = true;
                        app.grade_strip_idle_ticks = 15;
                    } else {
                        app.frame_index = app.frame_index.saturating_sub(10);
                    }
                }
                crossterm::event::KeyCode::Up | crossterm::event::KeyCode::Char('k') => {
                    if app.show_help {
                        app.help_scroll = app.help_scroll.saturating_sub(1);
                    } else if app.browsing_favourites {
                        app.navigate_favourites(-1);
                    } else if app.show_browser {
                        app.navigate_browser(BrowserDirection::Up);
                    } else {
                        match app.focus_target {
                            FocusTarget::MediaPool => {
                                if app.media_pool_index > 0 {
                                    app.media_pool_index -= 1;
                                }
                            }
                            FocusTarget::Queue => {
                                if app.queue_index > 0 {
                                    app.queue_index -= 1;
                                }
                            }
                            FocusTarget::ExportSettings => {
                                app.export_focus = match app.export_focus {
                                    ExportFocus::ColorSpace => ExportFocus::RateControl,
                                    ExportFocus::TransferFunction => ExportFocus::ColorSpace,
                                    ExportFocus::CodecFamily => ExportFocus::TransferFunction,
                                    ExportFocus::Profile => ExportFocus::CodecFamily,
                                    ExportFocus::RateControl => ExportFocus::Profile,
                                };
                            }
                            FocusTarget::Preview => {}
                            FocusTarget::Grade => {
                                if app.grade_focus > 0 {
                                    app.grade_morph = Some((app.grade_focus, 4));
                                    app.grade_focus -= 1;
                                    app.grade_strip_active = true;
                                    app.grade_strip_idle_ticks = 15;
                                }
                            }
                        }
                    }
                }
                crossterm::event::KeyCode::Down | crossterm::event::KeyCode::Char('j') => {
                    if app.show_help {
                        app.help_scroll = app.help_scroll.saturating_add(1);
                    } else if app.browsing_favourites {
                        app.navigate_favourites(1);
                    } else if app.show_browser {
                        app.navigate_browser(BrowserDirection::Down);
                    } else {
                        match app.focus_target {
                            FocusTarget::MediaPool => {
                                if app.media_pool_index + 1 < app.imported_files.len() {
                                    app.media_pool_index += 1;
                                }
                            }
                            FocusTarget::Queue => {
                                if app.queue_index + 1 < app.queue.len() {
                                    app.queue_index += 1;
                                }
                            }
                            FocusTarget::ExportSettings => {
                                app.export_focus = match app.export_focus {
                                    ExportFocus::ColorSpace => ExportFocus::TransferFunction,
                                    ExportFocus::TransferFunction => ExportFocus::CodecFamily,
                                    ExportFocus::CodecFamily => ExportFocus::Profile,
                                    ExportFocus::Profile => ExportFocus::RateControl,
                                    ExportFocus::RateControl => ExportFocus::ColorSpace,
                                };
                            }
                            FocusTarget::Preview => {}
                            FocusTarget::Grade => {
                                if app.grade_focus + 1 < GradeSliders::count() {
                                    app.grade_morph = Some((app.grade_focus, 4));
                                    app.grade_focus += 1;
                                    app.grade_strip_active = true;
                                    app.grade_strip_idle_ticks = 15;
                                }
                            }
                        }
                    }
                }
                crossterm::event::KeyCode::Char(' ') => {
                    if app.show_browser {
                        app.browser.toggle_selection();
                    } else {
                        match app.focus_target {
                            FocusTarget::MediaPool => app.toggle_media_pool_selection(),
                            FocusTarget::Queue => app.toggle_queue_selection(),
                            FocusTarget::ExportSettings => {}
                            FocusTarget::Preview => {}
                            FocusTarget::Grade => {}
                        }
                    }
                }
                crossterm::event::KeyCode::PageUp => {
                    if app.show_help {
                        app.help_scroll = app.help_scroll.saturating_sub(10);
                    } else if app.browsing_favourites {
                        app.navigate_favourites(-10);
                    } else if app.show_browser {
                        let entries_len = app.browser.entries.len();
                        if entries_len > 0 {
                            let new_index = app.browser.selected_index.saturating_sub(10.min(entries_len));
                            app.browser.selected_index = new_index;
                        }
                    } else if app.focus_target == FocusTarget::MediaPool {
                        let len = app.imported_files.len();
                        if len > 0 {
                            app.media_pool_index = app.media_pool_index.saturating_sub(10.min(len));
                        }
                    } else if app.focus_target == FocusTarget::Queue {
                        let len = app.queue.len();
                        if len > 0 {
                            app.queue_index = app.queue_index.saturating_sub(10.min(len));
                        }
                    }
                }
                crossterm::event::KeyCode::PageDown => {
                    if app.show_help {
                        app.help_scroll = app.help_scroll.saturating_add(10);
                    } else if app.browsing_favourites {
                        app.navigate_favourites(10);
                    } else if app.show_browser {
                        let entries_len = app.browser.entries.len();
                        if entries_len > 0 {
                            let new_index = (app.browser.selected_index + 10).min(entries_len - 1);
                            app.browser.selected_index = new_index;
                        }
                    } else if app.focus_target == FocusTarget::MediaPool {
                        let len = app.imported_files.len();
                        if len > 0 {
                            app.media_pool_index = (app.media_pool_index + 10).min(len - 1);
                        }
                    } else if app.focus_target == FocusTarget::Queue {
                        let len = app.queue.len();
                        if len > 0 {
                            app.queue_index = (app.queue_index + 10).min(len - 1);
                        }
                    }
                }
                crossterm::event::KeyCode::Home => {
                    if app.browsing_favourites {
                        app.favourites_scroll_offset = Cell::new(0);
                    } else if app.show_browser {
                        app.browser.selected_index = 0;
                    } else if app.focus_target == FocusTarget::MediaPool {
                        app.media_pool_index = 0;
                    } else if app.focus_target == FocusTarget::Queue {
                        app.queue_index = 0;
                    } else {
                        app.frame_index = 0;
                    }
                }
                crossterm::event::KeyCode::End => {
                    if app.browsing_favourites {
                        if !app.favourite_folders.is_empty() {
                            app.favourites_scroll_offset
                                .set(app.favourite_folders.len() - 1);
                        }
                    } else if app.show_browser {
                        let entries_len = app.browser.entries.len();
                        if entries_len > 0 {
                            app.browser.selected_index = entries_len - 1;
                        }
                    } else if app.focus_target == FocusTarget::MediaPool {
                        if !app.imported_files.is_empty() {
                            app.media_pool_index = app.imported_files.len() - 1;
                        }
                    } else if app.focus_target == FocusTarget::Queue {
                        if !app.queue.is_empty() {
                            app.queue_index = app.queue.len() - 1;
                        }
                    } else {
                        app.frame_index = app.frame_count.saturating_sub(1);
                    }
                }
                crossterm::event::KeyCode::Backspace => {
                    if app.browsing_favourites {
                        app.browsing_favourites = false;
                        app.status_message = "Folder view".to_string();
                    } else if app.show_browser {
                        app.navigate_browser(BrowserDirection::GoUp);
                    }
                }
                _ => {}
            }
        }
        _ => {}
    }
}