okf-studio 0.2.6

okf studio: an interactive terminal IDE and mission control center for Open Knowledge Format (OKF) bundles, built on the okf-core engine.
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
//! The application model: state, messages, commands, and the reducer.
//!
//! Elm-style unidirectional flow: `update(&mut App, Msg)` is pure state
//! mutation — it never blocks and never touches the disk. Side effects are
//! requested as [`Command`]s that the main loop drains to the worker thread,
//! plus two main-thread specials (`$EDITOR` suspension and OSC 52 copy).

use crate::graph::{LayoutEngine, LayoutMode};
use crate::keymap::{Action, Context, resolve};
use crate::snapshot::{LeafKind, Snapshot, TreeNode};
use crate::theme::Theme;
use crate::{StudioOptions, Tab};
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers, MouseEvent};
use okf_core::{ConceptId, Date, RefactorError, TrustTier};
use okf_core::{MergeReport, MoveReport, RemoveReport, RenameSectionReport, SplitReport};
use std::collections::{HashSet, VecDeque};
use std::path::PathBuf;
use std::sync::Arc;

/// Ticks (250 ms each) a toast stays visible.
const TOAST_TTL: u8 = 12;

/// An input message consumed by [`App::update`].
#[derive(Debug)]
pub enum Msg {
    /// A key press.
    Key(KeyEvent),
    /// A mouse event.
    Mouse(MouseEvent),
    /// The terminal was resized.
    Resize,
    /// The 250 ms heartbeat: spinners, toast expiry, debounced previews,
    /// incremental layout.
    Tick,
    /// The watcher saw files change on disk.
    FilesChanged,
    /// The worker finished building a snapshot.
    SnapshotReady(Arc<Snapshot>),
    /// The worker failed to load the bundle.
    SnapshotFailed(String),
    /// A refactor dry-run finished.
    PreviewReady(u64, Result<PreviewReport, RefactorError>),
    /// A write operation finished: `Ok(toast)` or `Err(message)`.
    Applied(Result<String, String>),
    /// A bundle-wide fix dry-run finished.
    FixReportReady(Box<okf_core::BundleFixReport>),
    /// A background error worth a toast.
    Error(String),
}

/// A side-effect request executed by the worker thread.
#[derive(Clone, Debug)]
pub enum Command {
    /// Rebuild the snapshot from disk.
    Reload,
    /// Dry-run a refactor; answer with [`Msg::PreviewReady`] carrying the id.
    Preview {
        /// Correlation id for the answer.
        request: u64,
        /// The operation to simulate.
        op: RefactorOp,
    },
    /// Apply a refactor for real.
    Apply(RefactorOp),
    /// Append a verification stamp to a concept.
    StampVerification(ConceptId),
    /// Write a new `stale_after` date.
    SetStaleAfter(ConceptId, Date),
    /// Scaffold a new concept file.
    CreateConcept {
        /// Path relative to the bundle root (`.md` optional).
        rel_path: String,
        /// The concept `type`.
        type_: String,
        /// Optional explicit title.
        title: Option<String>,
    },
    /// Dry-run the bundle fix engine.
    PreviewFix,
    /// Apply all safe fixes.
    ApplyFix,
    /// Apply safe fixes to one file.
    ApplyFixFile(PathBuf),
    /// Re-pin the evaluation date (`None` returns to the wall clock).
    SetToday(Option<Date>),
    /// Stop the worker thread.
    Shutdown,
}

/// A refactor operation, shared by preview and apply.
#[derive(Clone, Debug)]
pub enum RefactorOp {
    /// Move / rename a concept.
    Move {
        /// Current id.
        source: ConceptId,
        /// New id.
        target: ConceptId,
        /// Overwrite an existing target.
        force: bool,
    },
    /// Remove a concept.
    Remove {
        /// The concept to remove.
        target: ConceptId,
        /// Redirect inbound links here.
        redirect_to: Option<ConceptId>,
        /// Unlink inbound links to plain text.
        unlink: bool,
        /// Remove even with inbound links.
        force: bool,
    },
    /// Merge `source` into `target`.
    Merge {
        /// The concept that disappears.
        source: ConceptId,
        /// The surviving concept.
        target: ConceptId,
    },
    /// Split a section out into a new concept.
    Split {
        /// The concept holding the section.
        source: ConceptId,
        /// The new concept's id.
        target: ConceptId,
        /// The section heading to extract.
        section: String,
        /// Title for the new concept.
        title: Option<String>,
        /// Overwrite an existing target.
        force: bool,
    },
    /// Rename a section heading.
    RenameSection {
        /// The concept holding the section.
        concept: ConceptId,
        /// The current heading.
        old: String,
        /// The new heading.
        new: String,
    },
}

/// A dry-run (or applied) refactor report.
#[derive(Clone, Debug)]
pub enum PreviewReport {
    /// From [`okf_core::move_concept`].
    Move(MoveReport),
    /// From [`okf_core::remove_concept`].
    Remove(RemoveReport),
    /// From [`okf_core::merge_concepts`].
    Merge(MergeReport),
    /// From [`okf_core::split_concept`].
    Split(SplitReport),
    /// From [`okf_core::rename_section`].
    RenameSection(RenameSectionReport),
}

/// What the tree selection points at. Selection is held by identifier, not
/// index, so it survives snapshot swaps.
#[derive(Clone, Debug, PartialEq, Eq)]
pub enum TreeSel {
    /// A directory (by `/`-joined relative path).
    Dir(String),
    /// A concept.
    Concept(ConceptId),
    /// A reserved or broken file.
    File(PathBuf),
}

/// One visible row of the flattened tree.
#[derive(Clone, Debug)]
pub struct TreeRow {
    /// Indentation depth.
    pub depth: usize,
    /// The selection identifier.
    pub sel: TreeSel,
    /// Display name.
    pub name: String,
    /// Row payload.
    pub kind: TreeRowKind,
}

/// The payload of a tree row.
#[derive(Clone, Debug)]
pub enum TreeRowKind {
    /// A directory row.
    Dir {
        /// Recursive concept count.
        count: usize,
        /// Whether it is currently collapsed.
        collapsed: bool,
    },
    /// A file row.
    Leaf(LeafKind),
}

/// Which explorer pane has focus.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum ExplorerPane {
    /// The tree.
    #[default]
    Tree,
    /// The document viewer.
    Viewer,
}

/// Explorer workspace state.
#[derive(Debug, Default)]
pub struct ExplorerState {
    /// Focused pane.
    pub pane: ExplorerPane,
    /// Current selection.
    pub selected: Option<TreeSel>,
    /// Collapsed directory paths.
    pub collapsed: HashSet<String>,
    /// Tree scroll offset, written back by the view.
    pub tree_offset: std::cell::Cell<usize>,
    /// Viewer scroll offset (rendered lines).
    pub scroll: usize,
    /// The height-clamped maximum scroll, written back by the view.
    pub max_scroll: std::cell::Cell<usize>,
    /// Focused link index in the rendered document.
    pub focused_link: Option<usize>,
    /// Whether frontmatter shows as raw YAML.
    pub raw_yaml: bool,
    /// Inspector tab: 0 Meta, 1 Links, 2 Sources, 3 History.
    pub inspector_tab: usize,
    /// Back stack of viewed concepts.
    pub history: Vec<ConceptId>,
    /// Forward stack.
    pub future: Vec<ConceptId>,
}

/// The dimension the graph colors nodes by.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum ColorBy {
    /// Trust tier (the default and the studio's primary hue).
    #[default]
    Trust,
    /// Lifecycle status.
    Status,
    /// Staleness.
    Staleness,
    /// Concept type.
    Type,
    /// Open diagnostics.
    Diagnostics,
}

impl ColorBy {
    /// The next dimension in the cycle.
    #[must_use]
    pub const fn next(self) -> Self {
        match self {
            Self::Trust => Self::Status,
            Self::Status => Self::Staleness,
            Self::Staleness => Self::Type,
            Self::Type => Self::Diagnostics,
            Self::Diagnostics => Self::Trust,
        }
    }

    /// Display name.
    #[must_use]
    pub const fn name(self) -> &'static str {
        match self {
            Self::Trust => "trust",
            Self::Status => "status",
            Self::Staleness => "staleness",
            Self::Type => "type",
            Self::Diagnostics => "diagnostics",
        }
    }
}

/// Graph workspace state.
#[derive(Debug)]
pub struct GraphState {
    /// The layout engine (positions persist across snapshots).
    pub layout: LayoutEngine,
    /// The layout algorithm.
    pub mode: LayoutMode,
    /// Pan offset in layout space.
    pub pan: (f64, f64),
    /// Zoom factor (larger = closer).
    pub zoom: f64,
    /// Selected node key.
    pub selected: Option<String>,
    /// Egocentric focus: center node key and hop count (cycles 1→2→3→off).
    pub focus: Option<(String, usize)>,
    /// Coloring dimension.
    pub color_by: ColorBy,
    /// Whether external source nodes show.
    pub show_sources: bool,
    /// Whether broken-target nodes show.
    pub show_broken: bool,
    /// Whether derivation edges show.
    pub show_derivations: bool,
    /// The applied fuzzy node filter.
    pub filter: String,
    /// The filter input line, when open.
    pub filter_input: Option<String>,
}

impl Default for GraphState {
    fn default() -> Self {
        Self {
            layout: LayoutEngine::default(),
            mode: LayoutMode::default(),
            pan: (0.0, 0.0),
            zoom: 1.0,
            selected: None,
            focus: None,
            color_by: ColorBy::default(),
            show_sources: false,
            show_broken: true,
            show_derivations: true,
            filter: String::new(),
            filter_input: None,
        }
    }
}

/// Which mission-control panel has focus.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum TrustPanel {
    /// The attention queue.
    #[default]
    Queue,
    /// The trust-tier distribution bars.
    TrustBars,
    /// The lifecycle distribution bars.
    LifecycleBars,
    /// The freshness distribution bars.
    FreshnessBars,
    /// The activity sparkline.
    Activity,
}

impl TrustPanel {
    /// The next panel in the focus cycle.
    #[must_use]
    pub const fn next(self) -> Self {
        match self {
            Self::Queue => Self::TrustBars,
            Self::TrustBars => Self::LifecycleBars,
            Self::LifecycleBars => Self::FreshnessBars,
            Self::FreshnessBars => Self::Activity,
            Self::Activity => Self::Queue,
        }
    }
}

/// A cohort filter applied to the attention queue.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Cohort {
    /// Filter to a trust tier.
    Tier(TrustTier),
    /// Filter to a status bucket (index into `status_counts`).
    Status(usize),
    /// Freshness bucket: 0 fresh, 1 stale, 2 stale-soon.
    Fresh(usize),
}

/// Attention-queue sort order.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum QueueSort {
    /// By risk score (default).
    #[default]
    Risk,
    /// By days overdue.
    Staleness,
    /// By blast radius (backlinks).
    Backlinks,
}

impl QueueSort {
    /// The next sort in the cycle.
    #[must_use]
    pub const fn next(self) -> Self {
        match self {
            Self::Risk => Self::Staleness,
            Self::Staleness => Self::Backlinks,
            Self::Backlinks => Self::Risk,
        }
    }

    /// Display name.
    #[must_use]
    pub const fn name(self) -> &'static str {
        match self {
            Self::Risk => "risk",
            Self::Staleness => "staleness",
            Self::Backlinks => "backlinks",
        }
    }
}

/// Mission-control workspace state.
#[derive(Debug, Default)]
pub struct TrustState {
    /// Focused panel.
    pub panel: TrustPanel,
    /// Selected queue row.
    pub queue_sel: usize,
    /// Selected bar row within the focused bar panel.
    pub bar_sel: usize,
    /// Active cohort filter.
    pub cohort: Option<Cohort>,
    /// Queue sort.
    pub sort: QueueSort,
}

/// Which computations pane has focus.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum CompPane {
    /// The contract list.
    #[default]
    List,
    /// The playground form.
    Form,
}

/// Computations workspace state.
#[derive(Debug, Default)]
pub struct ComputationsState {
    /// Focused pane.
    pub pane: CompPane,
    /// Selected contract index.
    pub selected: usize,
    /// Focused form field.
    pub field: usize,
    /// Parameter values, keyed by `concept-id\u{0}param-name`.
    pub values: std::collections::HashMap<String, String>,
}

/// The palette's mode, derived from its input prefix.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum PaletteMode {
    /// Omnisearch over concepts.
    Search,
    /// Command mode (`>` prefix).
    Command,
}

/// Palette overlay state.
#[derive(Debug, Default)]
pub struct PaletteState {
    /// The typed query.
    pub input: String,
    /// Selected result row.
    pub sel: usize,
}

impl PaletteState {
    /// The active mode.
    #[must_use]
    pub fn mode(&self) -> PaletteMode {
        if self.input.trim_start().starts_with('>') {
            PaletteMode::Command
        } else {
            PaletteMode::Search
        }
    }
}

/// Diagnostics overlay filter.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum DiagFilter {
    /// Everything.
    #[default]
    All,
    /// Validation errors only.
    Errors,
    /// Validation warnings only.
    Warnings,
    /// Lint findings only.
    Lint,
}

/// Diagnostics overlay state.
#[derive(Debug, Default)]
pub struct DiagnosticsState {
    /// Active filter.
    pub filter: DiagFilter,
    /// Selected row.
    pub sel: usize,
}

/// What a confirm overlay confirms.
#[derive(Clone, Debug)]
pub enum ConfirmAction {
    /// Stamp a verification.
    Verify(ConceptId),
}

/// Confirm overlay state.
#[derive(Debug)]
pub struct ConfirmState {
    /// Title line.
    pub title: String,
    /// Body lines.
    pub body: Vec<String>,
    /// What Enter does.
    pub action: ConfirmAction,
}

/// Date-picker overlay state (stale extension / today pinning).
#[derive(Debug)]
pub struct DatePickerState {
    /// The concept whose `stale_after` is written; `None` pins `--today`.
    pub id: Option<ConceptId>,
    /// The typed date.
    pub input: String,
}

/// The refactor verb a modal drives.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum VerbKind {
    /// Move / rename.
    Move,
    /// Remove.
    Remove,
    /// Merge into another concept.
    Merge,
    /// Split a section out.
    Split,
    /// Rename a section heading.
    RenameSection,
}

/// The remove modal's decision, mapped 1:1 to [`okf_core::RemoveOptions`].
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum RemoveChoice {
    /// No inbound links (or not decided yet).
    #[default]
    Plain,
    /// Redirect inbound links.
    Redirect,
    /// Unlink inbound links to plain text.
    Unlink,
    /// Force: leave broken links.
    Force,
}

/// Refactor modal state: intent → (decision) → preview.
#[derive(Debug)]
pub struct RefactorState {
    /// The verb.
    pub verb: VerbKind,
    /// The subject (studio already knows what you're looking at).
    pub subject: ConceptId,
    /// The section, for the heading verbs.
    pub section: Option<String>,
    /// Main input: target id or new heading text.
    pub input: String,
    /// Secondary input: split title / remove redirect target.
    pub extra: String,
    /// Which input is active (0 main, 1 extra).
    pub field: usize,
    /// The remove decision, when the dry-run surfaced inbound links.
    pub choice: RemoveChoice,
    /// Whether the move target may overwrite (set after the engine asks).
    pub force: bool,
    /// The latest dry-run result.
    pub preview: Option<Result<PreviewReport, RefactorError>>,
    /// The latest request id sent.
    pub request: u64,
    /// Debounce flag: a preview should be sent on the next tick.
    pub needs_preview: bool,
}

/// New-concept form state.
#[derive(Debug)]
pub struct NewConceptState {
    /// Field values: path, type, title.
    pub fields: [String; 3],
    /// Focused field.
    pub field: usize,
}

/// Fix preview overlay state.
#[derive(Debug)]
pub struct FixPreviewState {
    /// The dry-run report (contents carry before/after text).
    pub report: Box<okf_core::BundleFixReport>,
    /// Selected changed file.
    pub file_sel: usize,
    /// Diff scroll.
    pub scroll: usize,
}

/// Outline overlay state.
#[derive(Debug)]
pub struct OutlineState {
    /// The concept whose outline shows.
    pub id: ConceptId,
    /// Selected heading row.
    pub sel: usize,
    /// When set, the selected heading is being renamed with this input.
    pub rename: Option<String>,
}

/// An open overlay. Overlays stack; `Esc` closes the topmost.
#[derive(Debug)]
pub enum Overlay {
    /// Omnisearch / command palette.
    Palette(PaletteState),
    /// Diagnostics & fix engine.
    Diagnostics(DiagnosticsState),
    /// Help cheatsheet (scroll offset).
    Help(usize),
    /// Confirmation prompt.
    Confirm(ConfirmState),
    /// Date picker.
    DatePicker(DatePickerState),
    /// Refactor modal.
    Refactor(Box<RefactorState>),
    /// New-concept form.
    NewConcept(NewConceptState),
    /// Fix preview diff.
    FixPreview(FixPreviewState),
    /// Full merged log view (scroll offset).
    LogView(usize),
    /// Outline jump list.
    Outline(OutlineState),
}

/// A transient status message.
#[derive(Clone, Debug)]
pub struct Toast {
    /// The text.
    pub text: String,
    /// Remaining ticks.
    pub ttl: u8,
    /// Whether it reports an error.
    pub error: bool,
}

/// What background work is in flight, for the status-bar spinner.
#[derive(Clone, Copy, Debug, Default, PartialEq, Eq)]
pub enum LoadPhase {
    /// Nothing in flight.
    #[default]
    Idle,
    /// A snapshot rebuild is in flight.
    Reloading,
    /// A write operation is in flight.
    Applying,
}

/// The whole application state.
#[allow(clippy::struct_excessive_bools)]
pub struct App {
    /// The immutable world model (absent until the first load lands).
    pub snapshot: Option<Arc<Snapshot>>,
    /// The active workspace.
    pub tab: Tab,
    /// Explorer state.
    pub explorer: ExplorerState,
    /// Graph state.
    pub graph: GraphState,
    /// Mission-control state.
    pub trust: TrustState,
    /// Computations state.
    pub computations: ComputationsState,
    /// The overlay stack.
    pub overlays: Vec<Overlay>,
    /// Transient status messages.
    pub toasts: VecDeque<Toast>,
    /// Background work indicator.
    pub loading: LoadPhase,
    /// The theme.
    pub theme: Theme,
    /// Author identity for stamps and log entries.
    pub author: String,
    /// Commands awaiting dispatch to the worker (drained by the main loop).
    pub pending_commands: Vec<Command>,
    /// A file to open in `$EDITOR` (handled by the main loop).
    pub editor_request: Option<PathBuf>,
    /// Text to copy to the clipboard via OSC 52 (handled by the main loop).
    pub copy_request: Option<String>,
    /// Set when the user quits.
    pub should_quit: bool,
    /// Tick counter.
    pub tick: u64,
    /// A reload arrived while one was in flight.
    pub reload_queued: bool,
    /// Monotonic preview-request counter.
    pub next_request: u64,
    /// The bundle directory's display name.
    pub root_name: String,
    /// A fatal load error, shown instead of a workspace.
    pub load_error: Option<String>,
    /// Whether the watcher is disabled.
    pub no_watch: bool,
    /// The first snapshot has not yet been inspected (focus-mode default).
    first_snapshot: bool,
}

impl App {
    /// Creates the initial state from the launch options.
    #[must_use]
    pub fn new(options: &StudioOptions) -> Self {
        let root_name = options
            .root
            .canonicalize()
            .unwrap_or_else(|_| options.root.clone())
            .file_name()
            .map_or_else(
                || options.root.display().to_string(),
                |n| n.to_string_lossy().into_owned(),
            );
        Self {
            snapshot: None,
            tab: options.initial_tab.unwrap_or(Tab::Explorer),
            explorer: ExplorerState::default(),
            graph: GraphState::default(),
            trust: TrustState::default(),
            computations: ComputationsState::default(),
            overlays: Vec::new(),
            toasts: VecDeque::new(),
            loading: LoadPhase::Reloading,
            theme: Theme::from_env(),
            author: options
                .author
                .clone()
                .unwrap_or_else(okf_core::default_author),
            pending_commands: vec![Command::Reload],
            editor_request: None,
            copy_request: None,
            should_quit: false,
            tick: 0,
            reload_queued: false,
            next_request: 0,
            root_name,
            load_error: None,
            no_watch: options.no_watch,
            first_snapshot: true,
        }
    }

    /// Pushes a toast.
    pub fn toast(&mut self, text: impl Into<String>, error: bool) {
        self.toasts.push_back(Toast {
            text: text.into(),
            ttl: TOAST_TTL,
            error,
        });
        while self.toasts.len() > 3 {
            self.toasts.pop_front();
        }
    }

    fn send(&mut self, command: Command) {
        self.pending_commands.push(command);
    }

    /// The concept the current selection denotes, if any — the subject every
    /// contextual verb acts on.
    #[must_use]
    pub fn selected_concept(&self) -> Option<ConceptId> {
        let snapshot = self.snapshot.as_ref()?;
        match self.tab {
            Tab::Explorer => match &self.explorer.selected {
                Some(TreeSel::Concept(id)) => Some(id.clone()),
                _ => None,
            },
            Tab::Graph => {
                let key = self.graph.selected.as_ref()?;
                snapshot
                    .graph
                    .nodes
                    .iter()
                    .find(|n| &n.key == key)
                    .and_then(|n| n.id.clone())
            }
            Tab::Trust => self
                .filtered_queue()
                .get(self.trust.queue_sel)
                .map(|item| item.id.clone()),
            Tab::Computations => snapshot
                .contracts
                .get(self.computations.selected)
                .map(|c| c.id.clone()),
        }
    }

    /// The on-disk file the current selection denotes (for `$EDITOR`).
    #[must_use]
    pub fn selected_path(&self) -> Option<PathBuf> {
        let snapshot = self.snapshot.as_ref()?;
        if self.tab == Tab::Explorer {
            match &self.explorer.selected {
                Some(TreeSel::File(path)) => return Some(path.clone()),
                Some(TreeSel::Dir(_)) | None => return None,
                Some(TreeSel::Concept(_)) => {}
            }
        }
        self.selected_concept()
            .map(|id| id.to_path(snapshot.bundle.root()))
    }

    /// The visible tree rows given the current collapse state.
    #[must_use]
    pub fn tree_rows(&self) -> Vec<TreeRow> {
        let mut rows = Vec::new();
        if let Some(snapshot) = &self.snapshot {
            flatten_tree(&snapshot.tree.roots, 0, &self.explorer.collapsed, &mut rows);
        }
        rows
    }

    /// The attention queue after the cohort filter and sort.
    #[must_use]
    pub fn filtered_queue(&self) -> Vec<crate::snapshot::AttentionItem> {
        let Some(snapshot) = &self.snapshot else {
            return Vec::new();
        };
        let mut items: Vec<crate::snapshot::AttentionItem> = snapshot
            .attention
            .iter()
            .filter(|item| {
                let Some(meta) = snapshot.meta(&item.id) else {
                    return false;
                };
                match self.trust.cohort {
                    None => true,
                    Some(Cohort::Tier(tier)) => meta.tier == tier,
                    Some(Cohort::Status(ix)) => {
                        ix == match meta.status {
                            okf_core::Status::Draft => 0,
                            okf_core::Status::Stable => 1,
                            okf_core::Status::Deprecated => 2,
                            okf_core::Status::Other(_) => 3,
                        }
                    }
                    Some(Cohort::Fresh(ix)) => match ix {
                        1 => meta.stale,
                        2 => meta.stale_in_days.is_some(),
                        _ => !meta.stale && meta.stale_in_days.is_none(),
                    },
                }
            })
            .cloned()
            .collect();
        match self.trust.sort {
            QueueSort::Risk => {}
            QueueSort::Staleness => items.sort_by_key(|item| {
                std::cmp::Reverse(
                    self.snapshot
                        .as_ref()
                        .and_then(|s| s.meta(&item.id))
                        .and_then(|m| m.overdue_days)
                        .unwrap_or(i64::MIN),
                )
            }),
            QueueSort::Backlinks => items.sort_by_key(|item| {
                std::cmp::Reverse(
                    self.snapshot
                        .as_ref()
                        .and_then(|s| s.meta(&item.id))
                        .map_or(0, |m| m.in_degree),
                )
            }),
        }
        items
    }

    /// Applies one message. Never blocks, never touches disk.
    pub fn update(&mut self, msg: Msg) {
        match msg {
            Msg::Key(key) => self.on_key(key),
            Msg::Mouse(_) | Msg::Resize => {}
            Msg::Tick => self.on_tick(),
            Msg::FilesChanged => {
                if self.loading == LoadPhase::Idle {
                    self.loading = LoadPhase::Reloading;
                    self.send(Command::Reload);
                } else {
                    self.reload_queued = true;
                }
            }
            Msg::SnapshotReady(snapshot) => self.on_snapshot(snapshot),
            Msg::SnapshotFailed(e) => {
                self.load_error = Some(e);
                self.loading = LoadPhase::Idle;
            }
            Msg::PreviewReady(request, result) => {
                if let Some(Overlay::Refactor(state)) = self.overlays.last_mut()
                    && state.request == request
                {
                    if let Err(RefactorError::HasInboundLinks { .. }) = &result
                        && state.verb == VerbKind::Remove
                        && state.choice == RemoveChoice::Plain
                    {
                        // The decision stage: the modal now asks exactly the
                        // question the engine raised.
                    }
                    state.preview = Some(result);
                }
            }
            Msg::Applied(result) => {
                self.loading = LoadPhase::Idle;
                match result {
                    Ok(text) => self.toast(text, false),
                    Err(e) => self.toast(format!("✗ {e}"), true),
                }
            }
            Msg::FixReportReady(report) => {
                if report.is_empty() {
                    self.toast("✔ nothing to fix", false);
                } else {
                    self.overlays.push(Overlay::FixPreview(FixPreviewState {
                        report,
                        file_sel: 0,
                        scroll: 0,
                    }));
                }
            }
            Msg::Error(e) => self.toast(format!("✗ {e}"), true),
        }
    }

    fn on_tick(&mut self) {
        self.tick += 1;
        for toast in &mut self.toasts {
            toast.ttl = toast.ttl.saturating_sub(1);
        }
        self.toasts.retain(|t| t.ttl > 0);

        // Debounced refactor preview.
        let mut to_send: Option<(u64, RefactorOp)> = None;
        if let Some(Overlay::Refactor(state)) = self.overlays.last_mut()
            && state.needs_preview
        {
            state.needs_preview = false;
            if let Some(op) = build_op(state) {
                self.next_request += 1;
                state.request = self.next_request;
                to_send = Some((state.request, op));
            } else {
                state.preview = None;
            }
        }
        if let Some((request, op)) = to_send {
            self.send(Command::Preview { request, op });
        }

        // Incremental layout stepping while the graph is visible.
        if self.tab == Tab::Graph
            && self.graph.mode == LayoutMode::Force
            && let Some(snapshot) = self.snapshot.clone()
        {
            let included = self.graph_included(&snapshot);
            self.graph.layout.step(&snapshot.graph, &included, 12);
        }
    }

    /// The nodes currently visible in the graph, honoring toggles, focus
    /// mode, and the fuzzy filter (filtered nodes stay visible but dimmed —
    /// they remain in layout).
    #[must_use]
    pub fn graph_included(&self, snapshot: &Snapshot) -> Vec<bool> {
        let model = &snapshot.graph;
        let mut included: Vec<bool> = model
            .nodes
            .iter()
            .map(|node| match node.kind {
                crate::graph::NodeKind::Source => self.graph.show_sources,
                crate::graph::NodeKind::Phantom => self.graph.show_broken,
                _ => true,
            })
            .collect();
        if let Some((center_key, k)) = &self.graph.focus
            && let Some(center) = model.nodes.iter().position(|n| &n.key == center_key)
        {
            let hood = model.neighborhood(center, *k);
            for (i, inc) in included.iter_mut().enumerate() {
                *inc = *inc && hood[i];
            }
        }
        included
    }

    fn on_snapshot(&mut self, snapshot: Arc<Snapshot>) {
        self.load_error = None;
        self.loading = LoadPhase::Idle;

        // Selection survival: fall back when the selected thing vanished.
        if let Some(TreeSel::Concept(id)) = &self.explorer.selected
            && !snapshot.bundle.contains(id)
        {
            self.explorer.selected = None;
        }
        if self.explorer.selected.is_none() {
            self.explorer.selected = snapshot
                .bundle
                .concepts()
                .first()
                .map(|c| TreeSel::Concept(c.id.clone()));
        }
        if let Some(key) = &self.graph.selected
            && !snapshot.graph.nodes.iter().any(|n| &n.key == key)
        {
            self.graph.selected = None;
        }
        self.computations.selected = self
            .computations
            .selected
            .min(snapshot.contracts.len().saturating_sub(1));

        // Layout: carry positions over; seed new nodes; keep radial layouts.
        self.graph.layout.seed(&snapshot.graph);
        if self.graph.mode == LayoutMode::Radial {
            self.graph.layout.radial(&snapshot.graph);
        }

        // Egocentric default for large bundles: the full hairball is not a
        // useful first picture past ~500 nodes.
        if self.first_snapshot {
            self.first_snapshot = false;
            if snapshot.graph.nodes.len() > 500 {
                let center = snapshot.bundle.concepts().first().map(|c| c.id.to_string());
                if let Some(center) = center {
                    self.graph.focus = Some((center, 1));
                }
            }
        }

        self.snapshot = Some(snapshot);
        if self.reload_queued {
            self.reload_queued = false;
            self.loading = LoadPhase::Reloading;
            self.send(Command::Reload);
        }
    }
}

fn flatten_tree(
    nodes: &[TreeNode],
    depth: usize,
    collapsed: &HashSet<String>,
    out: &mut Vec<TreeRow>,
) {
    for node in nodes {
        match node {
            TreeNode::Dir {
                name,
                path,
                concept_count,
                children,
            } => {
                let is_collapsed = collapsed.contains(path);
                out.push(TreeRow {
                    depth,
                    sel: TreeSel::Dir(path.clone()),
                    name: name.clone(),
                    kind: TreeRowKind::Dir {
                        count: *concept_count,
                        collapsed: is_collapsed,
                    },
                });
                if !is_collapsed {
                    flatten_tree(children, depth + 1, collapsed, out);
                }
            }
            TreeNode::Leaf { name, kind } => {
                let sel = match kind {
                    LeafKind::Concept(id) => TreeSel::Concept(id.clone()),
                    LeafKind::Index(p) | LeafKind::Log(p) | LeafKind::Broken(p) => {
                        TreeSel::File(p.clone())
                    }
                };
                out.push(TreeRow {
                    depth,
                    sel,
                    name: name.clone(),
                    kind: TreeRowKind::Leaf(kind.clone()),
                });
            }
        }
    }
}

/// Builds the refactor operation a modal currently describes, or `None` when
/// its inputs are not yet valid.
fn build_op(state: &RefactorState) -> Option<RefactorOp> {
    match state.verb {
        VerbKind::Move => {
            let target = ConceptId::parse(state.input.trim()).ok()?;
            Some(RefactorOp::Move {
                source: state.subject.clone(),
                target,
                force: state.force,
            })
        }
        VerbKind::Remove => {
            let redirect_to = if state.choice == RemoveChoice::Redirect {
                Some(ConceptId::parse(state.extra.trim()).ok()?)
            } else {
                None
            };
            Some(RefactorOp::Remove {
                target: state.subject.clone(),
                redirect_to,
                unlink: state.choice == RemoveChoice::Unlink,
                force: state.choice == RemoveChoice::Force,
            })
        }
        VerbKind::Merge => {
            let target = ConceptId::parse(state.input.trim()).ok()?;
            if target == state.subject {
                return None;
            }
            Some(RefactorOp::Merge {
                source: state.subject.clone(),
                target,
            })
        }
        VerbKind::Split => {
            let target = ConceptId::parse(state.input.trim()).ok()?;
            let title = if state.extra.trim().is_empty() {
                None
            } else {
                Some(state.extra.trim().to_string())
            };
            Some(RefactorOp::Split {
                source: state.subject.clone(),
                target,
                section: state.section.clone()?,
                title,
                force: state.force,
            })
        }
        VerbKind::RenameSection => {
            let new = state.input.trim();
            if new.is_empty() {
                return None;
            }
            Some(RefactorOp::RenameSection {
                concept: state.subject.clone(),
                old: state.section.clone()?,
                new: new.to_string(),
            })
        }
    }
}

// ---------------------------------------------------------------------------
// Key dispatch.
// ---------------------------------------------------------------------------

impl App {
    fn on_key(&mut self, key: KeyEvent) {
        if key.modifiers.contains(KeyModifiers::CONTROL) && key.code == KeyCode::Char('c') {
            self.should_quit = true;
            return;
        }
        if self.overlays.is_empty() {
            // Graph filter input captures keys before the keymap.
            if self.tab == Tab::Graph && self.graph.filter_input.is_some() {
                self.graph_filter_key(key);
                return;
            }
            // Playground form typing captures printable keys.
            if self.tab == Tab::Computations
                && self.computations.pane == CompPane::Form
                && self.playground_form_key(&key)
            {
                return;
            }
            let mut contexts: Vec<Context> = vec![match (self.tab, self.explorer.pane) {
                (Tab::Explorer, ExplorerPane::Tree) => Context::Tree,
                (Tab::Explorer, ExplorerPane::Viewer) => Context::Viewer,
                (Tab::Graph, _) => Context::Graph,
                (Tab::Trust, _) => Context::Trust,
                (Tab::Computations, _) => Context::Computations,
            }];
            if self.selected_concept().is_some() {
                contexts.push(Context::Concept);
            }
            contexts.push(Context::Global);
            if let Some(action) = resolve(&contexts, &key) {
                self.run_action(action);
            }
            return;
        }
        self.overlay_key(key);
    }

    /// Executes one keymap action (also the palette command executor).
    #[allow(clippy::too_many_lines)]
    pub fn run_action(&mut self, action: Action) {
        match action {
            Action::SwitchTab(ix) => {
                self.tab = match ix {
                    1 => Tab::Graph,
                    2 => Tab::Trust,
                    3 => Tab::Computations,
                    _ => Tab::Explorer,
                };
            }
            Action::OpenPalette => self
                .overlays
                .push(Overlay::Palette(PaletteState::default())),
            Action::OpenDiagnostics => self
                .overlays
                .push(Overlay::Diagnostics(DiagnosticsState::default())),
            Action::OpenHelp => self.overlays.push(Overlay::Help(0)),
            Action::Reload => {
                self.loading = LoadPhase::Reloading;
                self.send(Command::Reload);
            }
            Action::OpenEditor => {
                if let Some(path) = self.selected_path() {
                    self.editor_request = Some(path);
                } else {
                    self.toast("no file selected", true);
                }
            }
            Action::Quit => self.should_quit = true,
            Action::Verify => {
                if let Some(id) = self.selected_concept() {
                    self.overlays.push(Overlay::Confirm(ConfirmState {
                        title: format!("Stamp verification on {id}"),
                        body: vec![
                            format!("by {}", self.author),
                            "Appends a { by, at } event to `verified` and logs it.".to_string(),
                        ],
                        action: ConfirmAction::Verify(id),
                    }));
                }
            }
            Action::ExtendStale => {
                if let Some(id) = self.selected_concept() {
                    let current = self
                        .snapshot
                        .as_ref()
                        .and_then(|s| s.meta(&id))
                        .and_then(|m| m.stale_after.as_ref())
                        .and_then(|f| f.datetime.map(|dt| dt.date));
                    let today = self.snapshot.as_ref().map_or(
                        Date {
                            year: 2026,
                            month: 1,
                            day: 1,
                        },
                        |s| s.today,
                    );
                    let default = Date::from_days_since_epoch(
                        current
                            .unwrap_or(today)
                            .days_since_epoch()
                            .max(today.days_since_epoch())
                            + 90,
                    );
                    self.overlays.push(Overlay::DatePicker(DatePickerState {
                        id: Some(id),
                        input: default.to_string(),
                    }));
                }
            }
            Action::MoveOrRename => {
                if let Some(id) = self.selected_concept() {
                    self.open_refactor(VerbKind::Move, id, None);
                }
            }
            Action::Remove => {
                if let Some(id) = self.selected_concept() {
                    self.open_refactor(VerbKind::Remove, id, None);
                }
            }
            Action::Merge => {
                if let Some(id) = self.selected_concept() {
                    self.open_refactor(VerbKind::Merge, id, None);
                }
            }
            Action::SplitSection | Action::Outline => self.open_outline(),
            Action::NewConcept => {
                let dir = match &self.explorer.selected {
                    Some(TreeSel::Dir(path)) => path.clone(),
                    Some(TreeSel::Concept(id)) => {
                        id.parent().map(|p| p.to_string()).unwrap_or_default()
                    }
                    _ => String::new(),
                };
                let path = if dir.is_empty() {
                    String::new()
                } else {
                    format!("{dir}/")
                };
                self.overlays.push(Overlay::NewConcept(NewConceptState {
                    fields: [path, "Concept".to_string(), String::new()],
                    field: 0,
                }));
            }
            Action::CycleInspector => {
                self.explorer.inspector_tab = (self.explorer.inspector_tab + 1) % 4;
            }
            Action::ToggleRawYaml => self.explorer.raw_yaml = !self.explorer.raw_yaml,
            Action::NextLink | Action::PrevLink => self.cycle_link(action == Action::NextLink),
            Action::Back => self.nav_back(),
            Action::Forward => self.nav_forward(),
            Action::ZoomIn => self.graph.zoom = (self.graph.zoom * 1.25).min(40.0),
            Action::ZoomOut => self.graph.zoom = (self.graph.zoom / 1.25).max(0.05),
            Action::NextNode | Action::PrevNode => self.cycle_node(action == Action::NextNode),
            Action::FocusMode => self.cycle_focus_mode(),
            Action::CycleColor => self.graph.color_by = self.graph.color_by.next(),
            Action::ToggleSources => self.graph.show_sources = !self.graph.show_sources,
            Action::ToggleBroken => self.graph.show_broken = !self.graph.show_broken,
            Action::ToggleDerivations => {
                self.graph.show_derivations = !self.graph.show_derivations;
            }
            Action::PauseLayout => self.graph.layout.toggle_running(),
            Action::GraphFilter => {
                self.graph.filter_input = Some(self.graph.filter.clone());
            }
            Action::CycleLayout => {
                self.graph.mode = self.graph.mode.next();
                if let Some(snapshot) = &self.snapshot {
                    match self.graph.mode {
                        LayoutMode::Radial => self.graph.layout.radial(&snapshot.graph),
                        LayoutMode::Force => {
                            self.graph.layout.running = true;
                            self.graph.layout.toggle_running();
                            self.graph.layout.toggle_running();
                        }
                    }
                }
            }
            Action::CyclePanel => match self.tab {
                Tab::Trust => {
                    self.trust.panel = self.trust.panel.next();
                    self.trust.bar_sel = 0;
                }
                Tab::Computations => {
                    self.computations.pane = match self.computations.pane {
                        CompPane::List => CompPane::Form,
                        CompPane::Form => CompPane::List,
                    };
                }
                _ => {}
            },
            Action::CycleSort => self.trust.sort = self.trust.sort.next(),
            Action::CopySketch => self.copy_sketch(),
            Action::FixAll => self.send(Command::PreviewFix),
            Action::PinToday => {
                let today = self
                    .snapshot
                    .as_ref()
                    .map_or_else(String::new, |s| s.today.to_string());
                self.overlays.push(Overlay::DatePicker(DatePickerState {
                    id: None,
                    input: today,
                }));
            }
            Action::OpenLog => self.overlays.push(Overlay::LogView(0)),
            Action::Up
            | Action::Down
            | Action::Left
            | Action::Right
            | Action::PageUp
            | Action::PageDown
            | Action::HalfUp
            | Action::HalfDown
            | Action::Home
            | Action::End
            | Action::Activate => self.navigate(action),
        }
    }

    fn open_refactor(&mut self, verb: VerbKind, subject: ConceptId, section: Option<String>) {
        let input = match verb {
            VerbKind::Move => subject.to_string(),
            VerbKind::RenameSection => section.clone().unwrap_or_default(),
            VerbKind::Split => section
                .as_deref()
                .map(|s| {
                    let slug = okf_core::heading_slug(s);
                    subject
                        .parent()
                        .map_or_else(|| slug.clone(), |p| format!("{p}/{slug}"))
                })
                .unwrap_or_default(),
            _ => String::new(),
        };
        let extra = match verb {
            VerbKind::Split => section.clone().unwrap_or_default(),
            _ => String::new(),
        };
        let mut state = RefactorState {
            verb,
            subject,
            section,
            input,
            extra,
            field: 0,
            choice: RemoveChoice::Plain,
            force: false,
            preview: None,
            request: 0,
            needs_preview: true,
        };
        // Remove needs no input: dry-run immediately.
        if verb == VerbKind::Remove {
            state.needs_preview = true;
        }
        self.overlays.push(Overlay::Refactor(Box::new(state)));
    }

    fn open_outline(&mut self) {
        if self.tab != Tab::Explorer {
            return;
        }
        if let Some(TreeSel::Concept(id)) = &self.explorer.selected {
            self.overlays.push(Overlay::Outline(OutlineState {
                id: id.clone(),
                sel: 0,
                rename: None,
            }));
        }
    }

    fn copy_sketch(&mut self) {
        let Some(snapshot) = &self.snapshot else {
            return;
        };
        let Some(info) = snapshot.contracts.get(self.computations.selected) else {
            return;
        };
        let mut args: Vec<String> = Vec::new();
        for parameter in &info.contract.parameters {
            let name = parameter.name.clone().unwrap_or_default();
            let key = format!("{}\u{0}{}", info.id, name);
            let value = self
                .computations
                .values
                .get(&key)
                .cloned()
                .unwrap_or_default();
            if !value.is_empty() || parameter.is_required() {
                args.push(format!("{name}={value}"));
            }
        }
        let sketch = format!("{}({})", info.id.name(), args.join(", "));
        self.copy_request = Some(sketch.clone());
        self.toast(format!("✔ copied: {sketch}"), false);
    }

    fn graph_filter_key(&mut self, key: KeyEvent) {
        let Some(input) = self.graph.filter_input.as_mut() else {
            return;
        };
        match key.code {
            KeyCode::Esc => {
                self.graph.filter_input = None;
                self.graph.filter.clear();
            }
            KeyCode::Enter => {
                self.graph.filter = self.graph.filter_input.take().unwrap_or_default();
            }
            KeyCode::Backspace => {
                input.pop();
            }
            KeyCode::Char(c) => input.push(c),
            _ => {}
        }
    }

    /// Handles printable input for the playground form. Returns `true` when
    /// the key was consumed.
    fn playground_form_key(&mut self, key: &KeyEvent) -> bool {
        let Some(snapshot) = &self.snapshot else {
            return false;
        };
        let Some(info) = snapshot.contracts.get(self.computations.selected) else {
            return false;
        };
        let params: Vec<String> = info
            .contract
            .parameters
            .iter()
            .map(|p| p.name.clone().unwrap_or_default())
            .collect();
        if params.is_empty() {
            return false;
        }
        let field = self.computations.field.min(params.len() - 1);
        let value_key = format!("{}\u{0}{}", info.id, params[field]);
        match key.code {
            KeyCode::Char(c)
                if !key.modifiers.contains(KeyModifiers::CONTROL)
                    && c != '?'
                    && c != '!'
                    && c != '/' =>
            {
                self.computations
                    .values
                    .entry(value_key)
                    .or_default()
                    .push(c);
                true
            }
            KeyCode::Backspace => {
                self.computations.values.entry(value_key).or_default().pop();
                true
            }
            KeyCode::Up => {
                self.computations.field = field.saturating_sub(1);
                true
            }
            KeyCode::Down => {
                self.computations.field = (field + 1).min(params.len() - 1);
                true
            }
            _ => false,
        }
    }

    // -- Navigation -------------------------------------------------------

    #[allow(clippy::too_many_lines)]
    fn navigate(&mut self, action: Action) {
        match self.tab {
            Tab::Explorer => match self.explorer.pane {
                ExplorerPane::Tree => self.tree_navigate(action),
                ExplorerPane::Viewer => self.viewer_navigate(action),
            },
            Tab::Graph => self.graph_navigate(action),
            Tab::Trust => self.trust_navigate(action),
            Tab::Computations => self.computations_navigate(action),
        }
    }

    fn tree_navigate(&mut self, action: Action) {
        let rows = self.tree_rows();
        if rows.is_empty() {
            return;
        }
        let current = self
            .explorer
            .selected
            .as_ref()
            .and_then(|sel| rows.iter().position(|row| &row.sel == sel))
            .unwrap_or(0);
        let mut next = current;
        match action {
            Action::Up => next = current.saturating_sub(1),
            Action::Down => next = (current + 1).min(rows.len() - 1),
            Action::PageUp => next = current.saturating_sub(10),
            Action::PageDown => next = (current + 10).min(rows.len() - 1),
            Action::Home => next = 0,
            Action::End => next = rows.len() - 1,
            Action::Left => {
                match &rows[current].sel {
                    TreeSel::Dir(path) if !self.explorer.collapsed.contains(path) => {
                        self.explorer.collapsed.insert(path.clone());
                    }
                    sel => {
                        // Jump to the parent directory row.
                        let parent = match sel {
                            TreeSel::Concept(id) => id.parent().map(|p| p.to_string()),
                            TreeSel::Dir(path) => {
                                path.rsplit_once('/').map(|(parent, _)| parent.to_string())
                            }
                            TreeSel::File(_) => None,
                        };
                        if let Some(parent) = parent
                            && let Some(ix) = rows
                                .iter()
                                .position(|row| row.sel == TreeSel::Dir(parent.clone()))
                        {
                            next = ix;
                        }
                    }
                }
            }
            Action::Right => match &rows[current].sel {
                TreeSel::Dir(path) => {
                    self.explorer.collapsed.remove(path);
                }
                _ => self.explorer.pane = ExplorerPane::Viewer,
            },
            Action::Activate => match &rows[current].sel {
                TreeSel::Dir(path) => {
                    if self.explorer.collapsed.contains(path) {
                        self.explorer.collapsed.remove(path);
                    } else {
                        self.explorer.collapsed.insert(path.clone());
                    }
                }
                _ => self.explorer.pane = ExplorerPane::Viewer,
            },
            _ => {}
        }
        if next != current {
            self.select_row(&rows[next].sel);
        }
    }

    fn select_row(&mut self, sel: &TreeSel) {
        self.explorer.selected = Some(sel.clone());
        self.explorer.scroll = 0;
        self.explorer.focused_link = None;
    }

    fn viewer_navigate(&mut self, action: Action) {
        let max = self.explorer.max_scroll.get();
        let scroll = &mut self.explorer.scroll;
        match action {
            Action::Up => *scroll = scroll.saturating_sub(1),
            Action::Down => *scroll = (*scroll + 1).min(max),
            Action::PageUp => *scroll = scroll.saturating_sub(20),
            Action::PageDown => *scroll = (*scroll + 20).min(max),
            Action::HalfUp => *scroll = scroll.saturating_sub(10),
            Action::HalfDown => *scroll = (*scroll + 10).min(max),
            Action::Home => *scroll = 0,
            Action::End => *scroll = max,
            Action::Left => self.explorer.pane = ExplorerPane::Tree,
            Action::Activate => self.follow_focused_link(),
            _ => {}
        }
    }

    fn cycle_link(&mut self, forward: bool) {
        if self.tab != Tab::Explorer {
            return;
        }
        self.explorer.pane = ExplorerPane::Viewer;
        // The number of links is only known to the renderer; the view stores
        // it alongside max_scroll. We advance optimistically and let the view
        // clamp via the cache key; link count lives in the render cache.
        let count = self.explorer_link_count();
        if count == 0 {
            self.explorer.focused_link = None;
            return;
        }
        let next = match self.explorer.focused_link {
            None if forward => 0,
            None => count - 1,
            Some(i) if forward => (i + 1) % count,
            Some(i) => (i + count - 1) % count,
        };
        self.explorer.focused_link = Some(next);
    }

    /// Renders the selected concept once (uncached) to count its links.
    fn explorer_link_count(&self) -> usize {
        let Some(snapshot) = &self.snapshot else {
            return 0;
        };
        let Some(TreeSel::Concept(id)) = &self.explorer.selected else {
            return 0;
        };
        let Some(concept) = snapshot.bundle.get(id) else {
            return 0;
        };
        crate::markdown::render_document(&concept.document.body, 80, &self.theme, None)
            .links
            .len()
    }

    fn follow_focused_link(&mut self) {
        let Some(snapshot) = self.snapshot.clone() else {
            return;
        };
        let Some(TreeSel::Concept(id)) = self.explorer.selected.clone() else {
            return;
        };
        let Some(concept) = snapshot.bundle.get(&id) else {
            return;
        };
        let Some(focus) = self.explorer.focused_link else {
            return;
        };
        let rendered =
            crate::markdown::render_document(&concept.document.body, 80, &self.theme, None);
        let Some(target) = rendered.links.get(focus) else {
            return;
        };
        match &target.kind {
            crate::markdown::FocusKind::Footnote(label) => {
                if let Some(&line) = rendered.footnote_defs.get(label) {
                    self.explorer.scroll = line;
                }
            }
            crate::markdown::FocusKind::Link { target, kind, .. } => {
                let link = okf_core::Link {
                    text: String::new(),
                    target: target.clone(),
                    kind: *kind,
                };
                if let Some(resolved) = link
                    .resolve_all(&id)
                    .into_iter()
                    .find(|t| snapshot.bundle.contains(t))
                {
                    self.open_concept(&resolved);
                } else if *kind == okf_core::LinkKind::External {
                    self.toast(format!("external: {target}"), false);
                } else {
                    self.toast(format!("✗ broken link: {target}"), true);
                }
            }
        }
    }

    /// Jumps the explorer to a concept, recording history.
    pub fn open_concept(&mut self, id: &ConceptId) {
        if let Some(TreeSel::Concept(current)) = &self.explorer.selected
            && current != id
        {
            self.explorer.history.push(current.clone());
            self.explorer.future.clear();
        }
        self.tab = Tab::Explorer;
        self.explorer.pane = ExplorerPane::Viewer;
        self.select_row(&TreeSel::Concept(id.clone()));
        self.reveal_in_tree(id);
    }

    /// Expands ancestors so the selection is visible in the tree.
    fn reveal_in_tree(&mut self, id: &ConceptId) {
        let mut prefix = String::new();
        let segments = id.segments();
        for segment in &segments[..segments.len().saturating_sub(1)] {
            if !prefix.is_empty() {
                prefix.push('/');
            }
            prefix.push_str(segment);
            self.explorer.collapsed.remove(&prefix);
        }
    }

    fn nav_back(&mut self) {
        if let Some(previous) = self.explorer.history.pop() {
            if let Some(TreeSel::Concept(current)) = &self.explorer.selected {
                self.explorer.future.push(current.clone());
            }
            self.select_row(&TreeSel::Concept(previous.clone()));
            self.reveal_in_tree(&previous);
        }
    }

    fn nav_forward(&mut self) {
        if let Some(next) = self.explorer.future.pop() {
            if let Some(TreeSel::Concept(current)) = &self.explorer.selected {
                self.explorer.history.push(current.clone());
            }
            self.select_row(&TreeSel::Concept(next.clone()));
            self.reveal_in_tree(&next);
        }
    }

    fn graph_navigate(&mut self, action: Action) {
        let step = 0.15 / self.graph.zoom;
        match action {
            Action::Up => self.graph.pan.1 += step,
            Action::Down => self.graph.pan.1 -= step,
            Action::Left => self.graph.pan.0 -= step,
            Action::Right => self.graph.pan.0 += step,
            Action::Activate => {
                if let Some(id) = self.selected_concept() {
                    self.open_concept(&id);
                }
            }
            _ => {}
        }
    }

    fn cycle_node(&mut self, forward: bool) {
        let Some(snapshot) = self.snapshot.clone() else {
            return;
        };
        let included = self.graph_included(&snapshot);
        // Reading order over layout positions gives a stable, spatial cycle.
        let mut visible: Vec<(usize, (f64, f64))> = snapshot
            .graph
            .nodes
            .iter()
            .enumerate()
            .filter(|(i, _)| included[*i])
            .map(|(i, node)| {
                (
                    i,
                    self.graph
                        .layout
                        .positions
                        .get(&node.key)
                        .copied()
                        .unwrap_or((0.0, 0.0)),
                )
            })
            .collect();
        if visible.is_empty() {
            return;
        }
        visible.sort_by(|a, b| {
            b.1.1
                .partial_cmp(&a.1.1)
                .unwrap_or(std::cmp::Ordering::Equal)
                .then(
                    a.1.0
                        .partial_cmp(&b.1.0)
                        .unwrap_or(std::cmp::Ordering::Equal),
                )
        });
        let current = self.graph.selected.as_ref().and_then(|key| {
            visible
                .iter()
                .position(|(i, _)| &snapshot.graph.nodes[*i].key == key)
        });
        let next_pos = match current {
            None => 0,
            Some(p) if forward => (p + 1) % visible.len(),
            Some(p) => (p + visible.len() - 1) % visible.len(),
        };
        let node = &snapshot.graph.nodes[visible[next_pos].0];
        self.graph.selected = Some(node.key.clone());
        // Center the selection.
        if let Some(&(x, y)) = self.graph.layout.positions.get(&node.key) {
            self.graph.pan = (x, y);
        }
    }

    fn cycle_focus_mode(&mut self) {
        let center = self
            .graph
            .selected
            .clone()
            .or_else(|| self.graph.focus.as_ref().map(|(key, _)| key.clone()));
        let Some(center) = center else {
            self.toast("select a node first (Tab)", false);
            return;
        };
        self.graph.focus = match &self.graph.focus {
            None => Some((center, 1)),
            Some((_, k)) if *k < 3 => Some((center, k + 1)),
            Some(_) => None,
        };
    }

    fn trust_navigate(&mut self, action: Action) {
        let queue_len = self.filtered_queue().len();
        match self.trust.panel {
            TrustPanel::Queue => match action {
                Action::Up => self.trust.queue_sel = self.trust.queue_sel.saturating_sub(1),
                Action::Down => {
                    self.trust.queue_sel =
                        (self.trust.queue_sel + 1).min(queue_len.saturating_sub(1));
                }
                Action::Home => self.trust.queue_sel = 0,
                Action::End => self.trust.queue_sel = queue_len.saturating_sub(1),
                Action::Activate => {
                    if let Some(item) = self.filtered_queue().get(self.trust.queue_sel) {
                        let id = item.id.clone();
                        self.open_concept(&id);
                    }
                }
                _ => {}
            },
            TrustPanel::Activity => {
                if action == Action::Activate {
                    self.overlays.push(Overlay::LogView(0));
                }
            }
            panel => {
                let rows = match panel {
                    TrustPanel::TrustBars | TrustPanel::FreshnessBars => 3,
                    _ => 4,
                };
                match action {
                    Action::Up => self.trust.bar_sel = self.trust.bar_sel.saturating_sub(1),
                    Action::Down => self.trust.bar_sel = (self.trust.bar_sel + 1).min(rows - 1),
                    Action::Activate => {
                        let cohort = match panel {
                            TrustPanel::TrustBars => Cohort::Tier(match self.trust.bar_sel {
                                2 => TrustTier::Unverified,
                                1 => TrustTier::MachineConfirmed,
                                _ => TrustTier::HumanReviewed,
                            }),
                            TrustPanel::LifecycleBars => Cohort::Status(match self.trust.bar_sel {
                                0 => 1, // stable listed first
                                1 => 0,
                                other => other,
                            }),
                            _ => Cohort::Fresh(self.trust.bar_sel),
                        };
                        // Selecting the active cohort again clears it.
                        self.trust.cohort = if self.trust.cohort == Some(cohort) {
                            None
                        } else {
                            Some(cohort)
                        };
                        self.trust.queue_sel = 0;
                    }
                    _ => {}
                }
            }
        }
    }

    fn computations_navigate(&mut self, action: Action) {
        let Some(snapshot) = &self.snapshot else {
            return;
        };
        let count = snapshot.contracts.len();
        match self.computations.pane {
            CompPane::List => match action {
                Action::Up => {
                    self.computations.selected = self.computations.selected.saturating_sub(1);
                }
                Action::Down => {
                    self.computations.selected =
                        (self.computations.selected + 1).min(count.saturating_sub(1));
                }
                Action::Activate => {
                    if let Some(id) = self.selected_concept() {
                        self.open_concept(&id);
                    }
                }
                _ => {}
            },
            CompPane::Form => {}
        }
    }
}

// ---------------------------------------------------------------------------
// Overlays.
// ---------------------------------------------------------------------------

/// One row of the merged diagnostics overlay.
#[derive(Clone, Debug)]
pub struct DiagRow {
    /// `true` when the row comes from lint rather than validation.
    pub from_lint: bool,
    /// Severity.
    pub severity: okf_validator::Severity,
    /// The concept, when attributable.
    pub concept: Option<ConceptId>,
    /// The file, when attributable.
    pub path: Option<PathBuf>,
    /// The message.
    pub message: String,
    /// Whether `okf fix` can remediate it.
    pub fixable: bool,
}

/// The palette's computed results.
#[derive(Debug)]
pub enum PaletteResults {
    /// Omnisearch hits.
    Search(Vec<crate::search::SearchHit>),
    /// Command rows: `(label, key hint, action)`.
    Commands(Vec<(String, String, Action)>),
}

/// The curated command-mode entries: every studio action as a fuzzy-searchable
/// verb, each showing its direct keybinding as passive training.
fn command_entries() -> Vec<(&'static str, Action)> {
    vec![
        ("stamp verification", Action::Verify),
        ("extend stale_after…", Action::ExtendStale),
        ("move concept…", Action::MoveOrRename),
        ("remove concept…", Action::Remove),
        ("merge concept into…", Action::Merge),
        ("split section…", Action::SplitSection),
        ("new concept…", Action::NewConcept),
        ("fix all safe issues", Action::FixAll),
        ("diagnostics", Action::OpenDiagnostics),
        ("pin evaluation date (--today)…", Action::PinToday),
        ("open activity log", Action::OpenLog),
        ("open in $EDITOR", Action::OpenEditor),
        ("reload bundle", Action::Reload),
        ("toggle: color graph by next dimension", Action::CycleColor),
        ("toggle: graph source nodes", Action::ToggleSources),
        ("toggle: graph broken targets", Action::ToggleBroken),
        ("toggle: graph derivation edges", Action::ToggleDerivations),
        ("cycle graph layout", Action::CycleLayout),
        ("toggle raw YAML frontmatter", Action::ToggleRawYaml),
        ("help / cheatsheet", Action::OpenHelp),
        ("quit", Action::Quit),
    ]
}

impl App {
    /// Computes the palette's result rows for its current input.
    #[must_use]
    pub fn palette_results(&self, state: &PaletteState) -> PaletteResults {
        if state.mode() == PaletteMode::Command {
            let query = state.input.trim_start().trim_start_matches('>').trim();
            let mut rows: Vec<(i32, (String, String, Action))> = Vec::new();
            for (label, action) in command_entries() {
                let score = if query.is_empty() {
                    Some((0, Vec::new()))
                } else {
                    crate::search::fuzzy_match(query, label)
                };
                if let Some((score, _)) = score {
                    let key = crate::keymap::bindings()
                        .iter()
                        .find(|b| b.action == action && b.primary)
                        .map_or(String::new(), |b| b.label.to_string());
                    rows.push((score, (label.to_string(), key, action)));
                }
            }
            rows.sort_by_key(|a| std::cmp::Reverse(a.0));
            PaletteResults::Commands(rows.into_iter().map(|(_, row)| row).collect())
        } else {
            let hits = self.snapshot.as_ref().map_or_else(Vec::new, |snapshot| {
                snapshot.search.search(&state.input, 50)
            });
            PaletteResults::Search(hits)
        }
    }

    /// The merged validation + lint rows for a diagnostics filter.
    #[must_use]
    pub fn diag_rows(&self, filter: DiagFilter) -> Vec<DiagRow> {
        let Some(snapshot) = &self.snapshot else {
            return Vec::new();
        };
        let mut rows = Vec::new();
        if filter != DiagFilter::Lint {
            for d in &snapshot.validation.diagnostics {
                let include = match filter {
                    DiagFilter::Errors => d.severity == okf_validator::Severity::Error,
                    DiagFilter::Warnings => d.severity == okf_validator::Severity::Warning,
                    _ => true,
                };
                if include {
                    rows.push(DiagRow {
                        from_lint: false,
                        severity: d.severity,
                        concept: d.concept.clone(),
                        path: d.path.clone(),
                        message: d.message.clone(),
                        fixable: d.fixable,
                    });
                }
            }
        }
        if matches!(filter, DiagFilter::All | DiagFilter::Lint) {
            for d in &snapshot.lint.diagnostics {
                rows.push(DiagRow {
                    from_lint: true,
                    severity: d.severity,
                    concept: d.concept.clone(),
                    path: d.path.clone(),
                    message: d.message.clone(),
                    fixable: d.fixable,
                });
            }
        }
        // Errors first, then warnings, then the rest.
        rows.sort_by_key(|a| std::cmp::Reverse(a.severity));
        rows
    }

    #[allow(clippy::too_many_lines)]
    fn overlay_key(&mut self, key: KeyEvent) {
        if key.code == KeyCode::Esc {
            self.overlays.pop();
            return;
        }
        let Some(overlay) = self.overlays.pop() else {
            return;
        };
        match overlay {
            Overlay::Palette(state) => self.palette_key(state, key),
            Overlay::Diagnostics(state) => self.diagnostics_key(state, key),
            Overlay::Help(scroll) => {
                let next = match key.code {
                    KeyCode::Up | KeyCode::Char('k') => scroll.saturating_sub(1),
                    KeyCode::Down | KeyCode::Char('j') => scroll + 1,
                    KeyCode::PageUp => scroll.saturating_sub(20),
                    KeyCode::PageDown => scroll + 20,
                    KeyCode::Char('q' | '?') | KeyCode::Enter => {
                        return;
                    }
                    _ => scroll,
                };
                self.overlays.push(Overlay::Help(next));
            }
            Overlay::Confirm(state) => {
                if key.code == KeyCode::Enter {
                    match &state.action {
                        ConfirmAction::Verify(id) => {
                            self.loading = LoadPhase::Applying;
                            self.send(Command::StampVerification(id.clone()));
                        }
                    }
                } else {
                    self.overlays.push(Overlay::Confirm(state));
                }
            }
            Overlay::DatePicker(state) => self.date_picker_key(state, key),
            Overlay::Refactor(state) => self.refactor_key(*state, key),
            Overlay::NewConcept(state) => self.new_concept_key(state, key),
            Overlay::FixPreview(state) => self.fix_preview_key(state, key),
            Overlay::LogView(scroll) => {
                let next = match key.code {
                    KeyCode::Up | KeyCode::Char('k') => scroll.saturating_sub(1),
                    KeyCode::Down | KeyCode::Char('j') => scroll + 1,
                    KeyCode::PageUp => scroll.saturating_sub(20),
                    KeyCode::PageDown => scroll + 20,
                    KeyCode::Char('q') => return,
                    _ => scroll,
                };
                self.overlays.push(Overlay::LogView(next));
            }
            Overlay::Outline(state) => self.outline_key(state, key),
        }
    }

    fn palette_key(&mut self, mut state: PaletteState, key: KeyEvent) {
        match key.code {
            KeyCode::Char(c) if !key.modifiers.contains(KeyModifiers::CONTROL) => {
                state.input.push(c);
                state.sel = 0;
                self.overlays.push(Overlay::Palette(state));
            }
            KeyCode::Backspace => {
                state.input.pop();
                state.sel = 0;
                self.overlays.push(Overlay::Palette(state));
            }
            KeyCode::Up => {
                state.sel = state.sel.saturating_sub(1);
                self.overlays.push(Overlay::Palette(state));
            }
            KeyCode::Down => {
                state.sel += 1;
                self.overlays.push(Overlay::Palette(state));
            }
            KeyCode::Enter => match self.palette_results(&state) {
                PaletteResults::Search(hits) => {
                    if let Some(hit) = hits.get(state.sel.min(hits.len().saturating_sub(1))) {
                        let id = hit.id.clone();
                        if self.tab == Tab::Graph {
                            let key = id.to_string();
                            if let Some(&(x, y)) = self.graph.layout.positions.get(&key) {
                                self.graph.pan = (x, y);
                            }
                            self.graph.selected = Some(key);
                        } else {
                            self.open_concept(&id);
                        }
                    }
                }
                PaletteResults::Commands(commands) => {
                    if let Some((_, _, action)) =
                        commands.get(state.sel.min(commands.len().saturating_sub(1)))
                    {
                        self.run_action(*action);
                    }
                }
            },
            _ => self.overlays.push(Overlay::Palette(state)),
        }
    }

    fn diagnostics_key(&mut self, mut state: DiagnosticsState, key: KeyEvent) {
        let rows = self.diag_rows(state.filter);
        match key.code {
            KeyCode::Char('a') => state.filter = DiagFilter::All,
            KeyCode::Char('e') => state.filter = DiagFilter::Errors,
            KeyCode::Char('w') => state.filter = DiagFilter::Warnings,
            KeyCode::Char('l') => state.filter = DiagFilter::Lint,
            KeyCode::Up | KeyCode::Char('k') => state.sel = state.sel.saturating_sub(1),
            KeyCode::Down | KeyCode::Char('j') => {
                state.sel = (state.sel + 1).min(rows.len().saturating_sub(1));
            }
            KeyCode::Enter => {
                if let Some(row) = rows.get(state.sel)
                    && let Some(id) = row.concept.clone()
                {
                    self.open_concept(&id);
                    return;
                }
            }
            KeyCode::Char('f') => {
                if let Some(row) = rows.get(state.sel) {
                    let path = row.path.clone().or_else(|| {
                        self.snapshot.as_ref().and_then(|s| {
                            row.concept.as_ref().map(|id| id.to_path(s.bundle.root()))
                        })
                    });
                    if let Some(path) = path {
                        self.loading = LoadPhase::Applying;
                        self.send(Command::ApplyFixFile(path));
                    } else {
                        self.toast("no file to fix", true);
                    }
                }
            }
            KeyCode::Char('F') => self.send(Command::PreviewFix),
            KeyCode::Char('t') => {
                let today = self
                    .snapshot
                    .as_ref()
                    .map_or_else(String::new, |s| s.today.to_string());
                self.overlays.push(Overlay::Diagnostics(state));
                self.overlays.push(Overlay::DatePicker(DatePickerState {
                    id: None,
                    input: today,
                }));
                return;
            }
            _ => {}
        }
        state.sel = state
            .sel
            .min(self.diag_rows(state.filter).len().saturating_sub(1));
        self.overlays.push(Overlay::Diagnostics(state));
    }

    fn date_picker_key(&mut self, mut state: DatePickerState, key: KeyEvent) {
        let shift = |input: &str, days: i64| -> Option<String> {
            Date::parse(input.trim())
                .map(|d| Date::from_days_since_epoch(d.days_since_epoch() + days).to_string())
        };
        match key.code {
            KeyCode::Char(c) if c.is_ascii_digit() || c == '-' => state.input.push(c),
            KeyCode::Backspace => {
                state.input.pop();
            }
            KeyCode::Up => {
                if let Some(next) = shift(&state.input, 1) {
                    state.input = next;
                }
            }
            KeyCode::Down => {
                if let Some(next) = shift(&state.input, -1) {
                    state.input = next;
                }
            }
            KeyCode::PageUp => {
                if let Some(next) = shift(&state.input, 30) {
                    state.input = next;
                }
            }
            KeyCode::PageDown => {
                if let Some(next) = shift(&state.input, -30) {
                    state.input = next;
                }
            }
            KeyCode::Enter => {
                match (&state.id, Date::parse(state.input.trim())) {
                    (Some(id), Some(date)) => {
                        self.loading = LoadPhase::Applying;
                        self.send(Command::SetStaleAfter(id.clone(), date));
                    }
                    (None, Some(date)) => {
                        self.loading = LoadPhase::Reloading;
                        self.send(Command::SetToday(Some(date)));
                        self.toast(format!("today pinned to {date}"), false);
                    }
                    (None, None) if state.input.trim().is_empty() => {
                        self.loading = LoadPhase::Reloading;
                        self.send(Command::SetToday(None));
                        self.toast("today unpinned (wall clock)", false);
                    }
                    _ => {
                        self.toast("not a YYYY-MM-DD date", true);
                        self.overlays.push(Overlay::DatePicker(state));
                    }
                }
                return;
            }
            _ => {}
        }
        self.overlays.push(Overlay::DatePicker(state));
    }

    #[allow(clippy::too_many_lines)]
    fn refactor_key(&mut self, mut state: RefactorState, key: KeyEvent) {
        let awaiting_decision = matches!(
            state.preview,
            Some(Err(RefactorError::HasInboundLinks { .. }))
        ) || state.choice != RemoveChoice::Plain;
        let target_exists = matches!(
            state.preview,
            Some(Err(RefactorError::ConceptAlreadyExists(_)))
        );
        match key.code {
            KeyCode::Enter => {
                if matches!(state.preview, Some(Ok(_)))
                    && let Some(op) = build_op(&state)
                {
                    self.loading = LoadPhase::Applying;
                    self.send(Command::Apply(op));
                    return;
                }
                state.needs_preview = true;
            }
            KeyCode::Tab | KeyCode::BackTab => {
                let fields = match state.verb {
                    VerbKind::Split => 2,
                    VerbKind::Remove if state.choice == RemoveChoice::Redirect => 2,
                    _ => 1,
                };
                state.field = (state.field + 1) % fields;
            }
            KeyCode::Char('r') if state.verb == VerbKind::Remove && awaiting_decision => {
                state.choice = RemoveChoice::Redirect;
                state.field = 1;
                state.needs_preview = true;
            }
            KeyCode::Char('u') if state.verb == VerbKind::Remove && awaiting_decision => {
                state.choice = RemoveChoice::Unlink;
                state.needs_preview = true;
            }
            KeyCode::Char('f') if state.verb == VerbKind::Remove && awaiting_decision => {
                state.choice = RemoveChoice::Force;
                state.needs_preview = true;
            }
            KeyCode::Char('o')
                if matches!(state.verb, VerbKind::Move | VerbKind::Split) && target_exists =>
            {
                state.force = true;
                state.needs_preview = true;
            }
            KeyCode::Char(c) if !key.modifiers.contains(KeyModifiers::CONTROL) => {
                let field = if state.field == 0 {
                    &mut state.input
                } else {
                    &mut state.extra
                };
                field.push(c);
                state.force = false;
                state.needs_preview = true;
            }
            KeyCode::Backspace => {
                let field = if state.field == 0 {
                    &mut state.input
                } else {
                    &mut state.extra
                };
                field.pop();
                state.force = false;
                state.needs_preview = true;
            }
            _ => {}
        }
        self.overlays.push(Overlay::Refactor(Box::new(state)));
    }

    fn new_concept_key(&mut self, mut state: NewConceptState, key: KeyEvent) {
        match key.code {
            KeyCode::Tab | KeyCode::Down => state.field = (state.field + 1) % 3,
            KeyCode::BackTab | KeyCode::Up => state.field = (state.field + 2) % 3,
            KeyCode::Char(c) if !key.modifiers.contains(KeyModifiers::CONTROL) => {
                state.fields[state.field].push(c);
            }
            KeyCode::Backspace => {
                state.fields[state.field].pop();
            }
            KeyCode::Enter => {
                let path = state.fields[0].trim().to_string();
                if path.is_empty() || path.ends_with('/') {
                    self.toast("enter a concept path", true);
                    self.overlays.push(Overlay::NewConcept(state));
                    return;
                }
                let type_ = if state.fields[1].trim().is_empty() {
                    "Concept".to_string()
                } else {
                    state.fields[1].trim().to_string()
                };
                let title = if state.fields[2].trim().is_empty() {
                    None
                } else {
                    Some(state.fields[2].trim().to_string())
                };
                self.loading = LoadPhase::Applying;
                self.send(Command::CreateConcept {
                    rel_path: path,
                    type_,
                    title,
                });
                return;
            }
            _ => {}
        }
        self.overlays.push(Overlay::NewConcept(state));
    }

    fn fix_preview_key(&mut self, mut state: FixPreviewState, key: KeyEvent) {
        let changed_count = state.report.files.iter().filter(|f| f.changed).count();
        match key.code {
            KeyCode::Up | KeyCode::Char('k') => state.scroll = state.scroll.saturating_sub(1),
            KeyCode::Down | KeyCode::Char('j') => state.scroll += 1,
            KeyCode::PageUp => state.scroll = state.scroll.saturating_sub(20),
            KeyCode::PageDown => state.scroll += 20,
            KeyCode::Left | KeyCode::BackTab => {
                state.file_sel = state.file_sel.saturating_sub(1);
                state.scroll = 0;
            }
            KeyCode::Right | KeyCode::Tab => {
                state.file_sel = (state.file_sel + 1).min(changed_count.saturating_sub(1));
                state.scroll = 0;
            }
            KeyCode::Enter => {
                self.loading = LoadPhase::Applying;
                self.send(Command::ApplyFix);
                return;
            }
            _ => {}
        }
        self.overlays.push(Overlay::FixPreview(state));
    }

    fn outline_key(&mut self, mut state: OutlineState, key: KeyEvent) {
        let headings: Vec<(usize, String)> = self
            .snapshot
            .as_ref()
            .and_then(|s| s.meta(&state.id))
            .map(|m| m.headings.clone())
            .unwrap_or_default();
        match key.code {
            KeyCode::Up | KeyCode::Char('k') => state.sel = state.sel.saturating_sub(1),
            KeyCode::Down | KeyCode::Char('j') => {
                state.sel = (state.sel + 1).min(headings.len().saturating_sub(1));
            }
            KeyCode::Enter => {
                if let Some((_, text)) = headings.get(state.sel) {
                    self.scroll_to_heading(&state.id, text);
                }
                return;
            }
            KeyCode::Char('s') => {
                if let Some((_, text)) = headings.get(state.sel) {
                    let id = state.id.clone();
                    self.open_refactor(VerbKind::Split, id, Some(text.clone()));
                }
                return;
            }
            KeyCode::Char('r') | KeyCode::F(2) => {
                if let Some((_, text)) = headings.get(state.sel) {
                    let id = state.id.clone();
                    self.open_refactor(VerbKind::RenameSection, id, Some(text.clone()));
                }
                return;
            }
            _ => {}
        }
        self.overlays.push(Overlay::Outline(state));
    }

    /// Scrolls the viewer to a heading (rendered-line index approximated at a
    /// standard width; the view clamps).
    fn scroll_to_heading(&mut self, id: &ConceptId, heading: &str) {
        let Some(snapshot) = &self.snapshot else {
            return;
        };
        let Some(concept) = snapshot.bundle.get(id) else {
            return;
        };
        let rendered =
            crate::markdown::render_document(&concept.document.body, 80, &self.theme, None);
        if let Some(pos) = rendered.headings.iter().find(|h| h.text == heading) {
            self.explorer.pane = ExplorerPane::Viewer;
            self.explorer.scroll = pos.line;
        }
    }
}