inkhaven 2.2.0

Inkhaven — TUI literary work editor for Typst books
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
//! WBLD-1 (WB-P0) — the `WorldbuilderApp` shell.
//!
//! A third [`crate::tui_host::TuiHost`] consumer beside `research::ResearchApp`
//! and `linguistic::LinguisticApp`. WB-P0 stands up the frame: two (empty) left
//! panes over the Facts and World books, a full-width Query prompt, a right pane
//! (Chat | Research | Map | Ledger), a status bar, the focus/Tab model, and the
//! session sidecar. Later phases fill the panes with behaviour.

use anyhow::Result;
use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use ratatui::Frame;
use ratatui::Terminal;
use ratatui::backend::Backend;
use tui_textarea::TextArea;

use tokio::sync::mpsc;

use crate::ai::stream::{ChatTurn as AiTurn, StreamMsg, spawn_chat_stream};
use crate::config::Config;
use crate::project::ProjectLayout;
use crate::store::hierarchy::Hierarchy;
use crate::store::{NodeKind, SYSTEM_TAG_FACTS, SYSTEM_TAG_WORLD, Store};
use crate::system_tree::SystemBookTree;
use crate::tui::theme::Theme;
use crate::tui_host::TuiHost;
use uuid::Uuid;

use super::WorldbuilderInvocation;
use super::focus::{Focus, RightPane};
use super::session::WorldbuilderSession;

/// The tag that marks a Facts-book paragraph as an *invented-world* fact — the
/// `/wfact` output and the `Ctrl+T` toggle. Rendered `◎` in the Facts pane.
pub(super) const FACT_WORLD_TAG: &str = "fact:world";

/// AI cost-dashboard category for worldbuilder chat inferences.
const WB_CATEGORY: &str = "worldbuilder";

/// One chat exchange in the worldbuilder conversation.
pub(super) struct WorldbuilderTurn {
    pub prompt: String,
    pub response: String,
    pub streaming: bool,
}

/// A declared landmark resolved to a source-grid cell, for the map overlay (P2).
pub(super) struct MapMarker {
    pub x: usize,
    pub y: usize,
    pub name: String,
    pub kind: String,
}

/// What a map name-entry will place once a name is given (P2/P3).
pub(super) enum MapPlacement {
    /// A `geography.landmarks[]` entry of `geo_kind` at `(x, y)`.
    Landmark { geo_kind: &'static str, x: usize, y: usize },
    /// A `hydrology.rivers[]` entry from `from` to `to` (P3).
    River { from: (usize, usize), to: (usize, usize) },
    /// A `geography.regions[]` entry anchored at `(x, y)`, biome taken from the
    /// compiled cell under the cursor (P4).
    Region { x: usize, y: usize, biome: String },
}

/// An in-progress name entry for a feature being placed (P2/P3).
pub(super) struct MapInput {
    pub label: &'static str,
    pub placement: MapPlacement,
    pub buffer: String,
}

/// A multi-step map tool in progress (P3). `r` starts a river; the first Enter
/// sets its source, the second its mouth (then a name is requested).
pub(super) enum MapTool {
    River { source: Option<(usize, usize)> },
    /// A road connecting two landmarks; `from` is the first once picked (P6).
    Road { from: Option<String> },
}

/// World-book chapters owned by `realworld compile` — read-only in the
/// worldbuilder (change them by editing `world.hjson` + recompiling, not by hand).
/// Matched case-insensitively against a node's title or an ancestor's.
const WORLD_COMPILER_CHAPTERS: &[&str] =
    &["astronomy", "geology", "climate", "hydrology", "demographics"];

pub(crate) struct WorldbuilderApp {
    pub(super) layout: ProjectLayout,
    // Read by WB-P2 (system prompt), WB-P3 (plausibility weights), etc.
    #[allow(dead_code)]
    pub(super) cfg: Config,
    // Read by WB-P2+ (retrieval, world state, fact insert).
    #[allow(dead_code)]
    pub(super) store: Store,
    #[allow(dead_code)]
    pub(super) hierarchy: Hierarchy,

    // — Left pane A: Facts book (rendered + navigated in WB-P1) ————————
    #[allow(dead_code)]
    pub(super) facts_tree: SystemBookTree,
    #[allow(dead_code)]
    pub(super) facts_pins: Vec<Uuid>,

    // — Left pane B: World book (WB-P1) ————————————————————————————————
    #[allow(dead_code)]
    pub(super) world_tree: SystemBookTree,
    #[allow(dead_code)]
    pub(super) world_pins: Vec<Uuid>,

    // — Sizing (persisted to the session) ——————————————————————————————
    pub(super) left_split: u8,  // Facts / World vertical split   (2–8)
    pub(super) split_ratio: u8, // left column / right pane ratio (2–8)

    // — Focus + right pane ————————————————————————————————————————————
    pub(super) focus: Focus,
    pub(super) prev_focus: Focus,
    pub(super) right_pane: RightPane,
    /// `z` zooms one left pane to fill the left column; `Some(pane)` while zoomed.
    pub(super) zoom: Option<Focus>,
    /// `Shift+F` in the Facts pane: emphasise only `fact:world` paragraphs.
    pub(super) facts_filter_world: bool,

    // — Query prompt (full width) ——————————————————————————————————————
    pub(super) query: TextArea<'static>,

    // — Chat (RightPane::Chat) + streaming ————————————————————————————
    pub(super) chat: Vec<WorldbuilderTurn>,
    pub(super) chat_scroll: u16,
    stream_rx: Option<mpsc::UnboundedReceiver<StreamMsg>>,
    streaming_turn: Option<usize>,

    // — World shaping (WB-P4) ——————————————————————————————————————————
    /// Accepted-but-uncommitted edits; `/write` folds them into world.hjson.
    pub(super) pending_ops: Vec<super::commands::Op>,
    /// A shaping delta awaiting the author's y/n confirmation.
    pub(super) hjson_preview: Option<(String, Vec<super::commands::Op>)>,

    // — Plausibility (WB-P3) ——————————————————————————————————————————
    pub(super) plausibility_score: Option<u8>,
    plausibility_prev: Option<u8>,
    plausibility_warnings: Vec<crate::world::plausibility::Warning>,

    // — Compiled world state (WB-P5) ——————————————————————————————————
    /// The last `/compile` result: a deterministic prose summary of the compiled
    /// layer chain. Fed to the chat system prompt instead of the declaration-only
    /// summary when present. Invalidated whenever the world changes.
    compiled_summary: Option<String>,
    /// The compiled layer grids from the last `/compile`, rendered as an ASCII
    /// biome minimap in the Map right-pane (WB-P6). Invalidated with the summary.
    pub(super) compiled_layers: Option<crate::world::plausibility::CompiledLayers>,

    // — Raster map (WS-P2) —————————————————————————————————————————————
    /// The terminal's image protocol, if it can display images and
    /// `images.preview_enabled` is on. `None` → the Map pane is always ASCII.
    image_picker: Option<ratatui_image::picker::Picker>,
    /// The last `/map` plakat raster, ready to paint in the Map pane. `RefCell`
    /// because the stateful image protocol mutates on render but `TuiHost::render`
    /// is `&self`. Invalidated whenever the world changes.
    pub(super) map_raster: Option<std::cell::RefCell<ratatui_image::protocol::StatefulProtocol>>,

    // — Map editor (MAPED-P1/P2) ———————————————————————————————————————
    /// Whether the Map pane is in edit mode (a movable grid cursor). Requires a
    /// compiled map (the ASCII grid); the raster is suppressed while editing.
    pub(super) map_edit: bool,
    /// The edit cursor in *source-grid* coordinates (`geology.width × height`).
    pub(super) map_cursor: (usize, usize),
    /// Declared landmarks resolved to source cells, for the map overlay (P2).
    /// Refreshed with `compiled_layers`; cleared when the world changes.
    pub(super) map_landmarks: Vec<MapMarker>,
    /// Declared regions with an anchor cell, for the map overlay (P4).
    pub(super) map_regions: Vec<MapMarker>,
    /// Declared roads as source-cell endpoint pairs, for the overlay (P6).
    pub(super) map_roads: Vec<((usize, usize), (usize, usize))>,

    // — Terrain sculpt (MAPED-P7) ——————————————————————————————————————
    /// An in-progress edited heightmap (`geology.width × height`, 0..1), seeded
    /// from the compiled terrain on the first `+`/`-`. When present the map draws
    /// this instead of the biome grid, so the sculpt previews live.
    pub(super) map_terrain: Option<Vec<f32>>,
    /// The sea-level threshold (0..1) for the terrain preview + DEM export.
    pub(super) map_terrain_sea: f32,
    /// The sculpt brush radius in source cells (P7).
    pub(super) map_brush: usize,
    /// The Map pane's inner rect from the last render, so a mouse click can be
    /// mapped back to a source cell (P8). `Cell` because `render` is `&self`.
    pub(super) map_pane_rect: std::cell::Cell<Option<ratatui::layout::Rect>>,
    /// An open name-entry prompt for a feature being placed (P2/P3).
    pub(super) map_input: Option<MapInput>,
    /// A multi-step map tool in progress (P3 rivers).
    pub(super) map_tool: Option<MapTool>,
    /// The last `/mapcheck` findings — flagged on the map (`!`) and jumpable with
    /// `f` (P5). Cleared when the world changes.
    pub(super) map_findings: Vec<super::map::MapFinding>,
    /// Cursor into the spatial findings for `f` (P5).
    map_finding_idx: usize,

    // — World-fact research (WB-P7) ————————————————————————————————————
    /// The last `/research` query + its retrieved Facts passages, shown in the
    /// Research right-pane. `◎` marks passages already tagged `fact:world`.
    pub(super) research_query: Option<String>,
    pub(super) research_hits: Vec<crate::book_rag::RetrievedPassage>,
    /// Cursor into `research_hits` for the accept-as-fact keystroke (WS-P3).
    pub(super) research_cursor: usize,

    // — Interview (WB-P8) ——————————————————————————————————————————————
    /// The active guided interview, if the author started one (`/interview` or
    /// `--interview`). While `Some`, plain Query input answers the current step.
    pub(super) interview: Option<super::interview::Interview>,

    // — Magic ledger (WB-P9) ———————————————————————————————————————————
    /// The magic ledger of the current world (disk + pending), refreshed on every
    /// world change. Rendered + linted in the Ledger right-pane. `None` = no magic.
    pub(super) ledger_snapshot: Option<crate::world::types::MagicLedger>,

    // — Session ————————————————————————————————————————————————————————
    pub(super) session: WorldbuilderSession,

    pub(super) should_quit: bool,
    pub(super) status: String,
    pub(super) show_hints: bool,
    pub(super) theme: Theme,
}

impl WorldbuilderApp {
    pub(crate) fn new(
        layout: ProjectLayout,
        cfg: Config,
        store: Store,
        hierarchy: Hierarchy,
        inv: WorldbuilderInvocation,
    ) -> Result<WorldbuilderApp> {
        let facts_tree = SystemBookTree::new(&hierarchy, SYSTEM_TAG_FACTS);
        let world_tree = SystemBookTree::new(&hierarchy, SYSTEM_TAG_WORLD);

        let now = chrono::Utc::now().to_rfc3339();
        let session_name = inv.session.as_deref().unwrap_or("default");
        let session = WorldbuilderSession::open_or_create(&layout, session_name, now)?;

        let theme = Theme::from_config(&cfg.theme);
        let cfg_images_preview = cfg.images.preview_enabled;
        let mut query = TextArea::default();
        query.set_cursor_line_style(ratatui::style::Style::default());

        let left_split = session.left_split.clamp(2, 8);
        let split_ratio = session.split_ratio.clamp(2, 8);

        let mut app = WorldbuilderApp {
            layout,
            cfg,
            store,
            hierarchy,
            facts_tree,
            facts_pins: Vec::new(),
            world_tree,
            world_pins: Vec::new(),
            left_split,
            split_ratio,
            focus: Focus::QueryPrompt,
            prev_focus: Focus::QueryPrompt,
            right_pane: RightPane::Chat,
            zoom: None,
            facts_filter_world: false,
            query,
            chat: Vec::new(),
            chat_scroll: 0,
            stream_rx: None,
            streaming_turn: None,
            // WB-P10 — restore the accepted-but-uncommitted delta from the session.
            pending_ops: session.pending_ops.clone(),
            hjson_preview: None,
            plausibility_score: None,
            compiled_summary: None,
            compiled_layers: None,
            // WS-P2 — query the terminal once for image support (gated by config).
            image_picker: if cfg_images_preview {
                ratatui_image::picker::Picker::from_query_stdio().ok()
            } else {
                None
            },
            map_raster: None,
            map_edit: false,
            map_cursor: (0, 0),
            map_landmarks: Vec::new(),
            map_regions: Vec::new(),
            map_roads: Vec::new(),
            map_terrain: None,
            map_terrain_sea: 0.5,
            map_brush: 2,
            map_pane_rect: std::cell::Cell::new(None),
            map_input: None,
            map_tool: None,
            map_findings: Vec::new(),
            map_finding_idx: 0,
            research_query: None,
            research_hits: Vec::new(),
            research_cursor: 0,
            interview: None,
            ledger_snapshot: None,
            plausibility_prev: None,
            plausibility_warnings: Vec::new(),
            session,
            should_quit: false,
            status: "worldbuilder — Tab cycles panes · Ctrl+Q quits".to_string(),
            show_hints: true,
            theme,
        };
        app.refresh_plausibility();
        if !app.pending_ops.is_empty() {
            app.status = format!(
                "restored {} pending delta(s) from session · /diff to review · /write to commit",
                app.pending_ops.len()
            );
        }
        // `--interview` (and, until the plakat map-first flow lands, `--from-map`)
        // opens straight into the guided interview.
        if inv.interview || inv.from_map {
            app.start_interview();
        }
        Ok(app)
    }

    pub(crate) fn run<B: Backend>(&mut self, terminal: &mut Terminal<B>) -> Result<()> {
        crate::tui_host::run_loop(self, terminal)
    }

    /// The `world_name` for the title bar (session, else placeholder).
    pub(super) fn world_name(&self) -> &str {
        if self.session.world_name.is_empty() {
            "untitled world"
        } else {
            &self.session.world_name
        }
    }

    /// Persist pane sizing back to the session (best-effort).
    fn persist_sizing(&mut self) {
        self.session.left_split = self.left_split;
        self.session.split_ratio = self.split_ratio;
        if let Err(e) = self.session.save(&self.layout) {
            self.status = format!("session save failed: {e}");
        }
    }

    /// WB-P10 — mirror the live pending delta + world name into the session and
    /// persist. Called after every change to `pending_ops`, so an accepted-but-
    /// uncommitted delta survives a quit.
    fn persist_pending(&mut self) {
        self.session.pending_ops = self.pending_ops.clone();
        if self.session.world_name.is_empty() {
            self.session.world_name = self.world_name().to_string();
        }
        if let Err(e) = self.session.save(&self.layout) {
            self.status = format!("session save failed: {e}");
        }
    }

    /// WB-P10 — append a turn to the session timeline (the "Worldbuilding
    /// Journey") and persist. `facts` are Facts-book node ids created this turn.
    fn record_turn(&mut self, user: String, summary: String, facts: Vec<String>) {
        let seq = self.session.turns.len() as u64 + 1;
        let at = chrono::Utc::now().to_rfc3339();
        self.session.turns.push(super::session::SessionTurn {
            seq,
            at,
            user,
            assistant_summary: summary,
            plausibility_before: self.plausibility_prev,
            plausibility_after: self.plausibility_score,
            facts_inserted: facts,
            ..Default::default()
        });
        // Keep the pending delta persisted in the same write.
        self.session.pending_ops = self.pending_ops.clone();
        if let Err(e) = self.session.save(&self.layout) {
            self.status = format!("session save failed: {e}");
        }
    }

    fn handle_key(&mut self, key: KeyEvent) {
        let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
        // Universal quit.
        if ctrl && matches!(key.code, KeyCode::Char('c') | KeyCode::Char('q')) {
            self.should_quit = true;
            return;
        }
        // MAPED-P2 — a landmark name-entry prompt swallows all input.
        if self.map_input.is_some() {
            match key.code {
                KeyCode::Esc => {
                    self.map_input = None;
                    self.status = "placement cancelled".into();
                }
                KeyCode::Enter => {
                    if let Some(mi) = self.map_input.take() {
                        let name = mi.buffer.trim().to_string();
                        if name.is_empty() {
                            self.status = "name required — placement cancelled".into();
                        } else {
                            match mi.placement {
                                MapPlacement::Landmark { geo_kind, x, y } => {
                                    self.place_landmark(x, y, name, geo_kind)
                                }
                                MapPlacement::River { from, to } => {
                                    self.place_river(from, to, name)
                                }
                                MapPlacement::Region { x, y, biome } => {
                                    self.place_region(x, y, name, biome)
                                }
                            }
                        }
                    }
                }
                KeyCode::Backspace => {
                    if let Some(mi) = self.map_input.as_mut() {
                        mi.buffer.pop();
                    }
                }
                KeyCode::Char(c) => {
                    if let Some(mi) = self.map_input.as_mut() {
                        mi.buffer.push(c);
                    }
                }
                _ => {}
            }
            return;
        }
        // Ctrl+R cycles the right-pane view from anywhere.
        if ctrl && key.code == KeyCode::Char('r') {
            self.right_pane = self.right_pane.next();
            self.status = format!("right pane: {}", self.right_pane.title());
            return;
        }
        // Focus movement (the overlay, once it exists, is sticky via Focus::next).
        match key.code {
            KeyCode::Tab => {
                self.focus = self.focus.next();
                return;
            }
            KeyCode::BackTab => {
                self.focus = self.focus.prev();
                return;
            }
            _ => {}
        }

        // Pane-specific keys.
        match self.focus {
            Focus::QueryPrompt => match key.code {
                KeyCode::Esc => {
                    // Esc leaves an active interview first; then clears the line;
                    // then steps focus back to where we came from.
                    if self.interview.is_some() {
                        self.interview = None;
                        self.query.select_all();
                        self.query.cut();
                        self.status = "interview left — pending edits kept (/diff to review)".into();
                    } else if self.query.is_empty() {
                        self.focus = self.prev_focus;
                    } else {
                        self.query.select_all();
                        self.query.cut();
                    }
                }
                KeyCode::Enter => {
                    let text = self.query.lines().join("\n");
                    self.query.select_all();
                    self.query.cut();
                    let trimmed = text.trim();
                    // While interviewing, plain text answers the current step; a
                    // `/command` still runs (so /diff, /write work mid-interview).
                    if self.interview.is_some() && !trimmed.starts_with('/') {
                        self.submit_interview_answer(trimmed);
                    } else if trimmed.is_empty() {
                        // nothing to send
                    } else if trimmed.starts_with('/') {
                        self.dispatch_command(&text);
                    } else {
                        self.send_chat(text);
                    }
                }
                _ => {
                    self.query.input(key);
                }
            },
            Focus::FactsPane => self.left_pane_key(key, true),
            Focus::WorldPane => self.left_pane_key(key, false),
            Focus::RightPane => {
                if self.common_pane_key(key) {
                    return;
                }
                if self.right_pane == RightPane::Chat {
                    match key.code {
                        // `u16::MAX` pins to the bottom (also while streaming).
                        KeyCode::Char('G') => self.chat_scroll = u16::MAX,
                        KeyCode::Char('g') => self.chat_scroll = 0,
                        KeyCode::Char('k') | KeyCode::Up => {
                            if self.chat_scroll != u16::MAX {
                                self.chat_scroll = self.chat_scroll.saturating_sub(1);
                            }
                        }
                        KeyCode::Char('j') | KeyCode::Down => {
                            if self.chat_scroll != u16::MAX {
                                self.chat_scroll = self.chat_scroll.saturating_add(1);
                            }
                        }
                        _ => {}
                    }
                } else if self.right_pane == RightPane::Map {
                    // MAPED-P1/P2/P3 — grid cursor, placement tools, river tool.
                    if self.map_edit {
                        if self.try_map_move(key) {
                            // cursor moved
                        } else if self.map_tool.is_some() {
                            // A multi-step tool is active: Enter advances it.
                            match key.code {
                                KeyCode::Esc => {
                                    self.map_tool = None;
                                    self.status = "tool cancelled".into();
                                }
                                KeyCode::Enter => self.advance_map_tool(),
                                _ => {}
                            }
                        } else {
                            match key.code {
                                KeyCode::Esc | KeyCode::Char('e') => {
                                    self.map_edit = false;
                                    self.status = "left map edit".into();
                                }
                                KeyCode::Char('t') => self.open_map_input("Town name", "city"),
                                KeyCode::Char('n') => self.open_map_input("Landmark name", "landmark"),
                                KeyCode::Char('g') => self.open_region_input(),
                                KeyCode::Char('o') => {
                                    self.map_tool = Some(MapTool::Road { from: None });
                                    self.status =
                                        "road — move to the first landmark, Enter · Esc cancel".into();
                                }
                                KeyCode::Char('f') => self.jump_next_finding(),
                                // P7 terrain sculpt.
                                KeyCode::Char('+') => self.sculpt_terrain(0.05),
                                KeyCode::Char('-') => self.sculpt_terrain(-0.05),
                                KeyCode::Char('.') => {
                                    self.map_brush = (self.map_brush + 1).min(12);
                                    self.status = format!("brush r{}", self.map_brush);
                                }
                                KeyCode::Char(',') => {
                                    self.map_brush = self.map_brush.saturating_sub(1);
                                    self.status = format!("brush r{}", self.map_brush);
                                }
                                KeyCode::Char('d') | KeyCode::Char('x') => {
                                    self.delete_feature_at_cursor()
                                }
                                KeyCode::Char('r') => {
                                    self.map_tool = Some(MapTool::River { source: None });
                                    self.status =
                                        "river — move to the source, Enter to set · Esc cancel".into();
                                }
                                _ => {}
                            }
                        }
                    } else if key.code == KeyCode::Char('e') {
                        self.enter_map_edit();
                    }
                } else if self.right_pane == RightPane::Research {
                    // WS-P3 — move over hits; `a` promotes one to a world fact.
                    match key.code {
                        KeyCode::Char('j') | KeyCode::Down => {
                            let n = self.research_hits.len();
                            if n > 0 {
                                self.research_cursor = (self.research_cursor + 1).min(n - 1);
                            }
                        }
                        KeyCode::Char('k') | KeyCode::Up => {
                            self.research_cursor = self.research_cursor.saturating_sub(1);
                        }
                        KeyCode::Char('a') => self.accept_research_hit(),
                        _ => {}
                    }
                }
            }
            Focus::ConfirmationOverlay => match key.code {
                KeyCode::Char('y') => self.accept_pending(),
                KeyCode::Char('n') | KeyCode::Esc => {
                    self.hjson_preview = None;
                    self.focus = Focus::QueryPrompt;
                    self.status = "delta discarded".into();
                }
                _ => {}
            },
        }
    }

    /// Route a `/command` from the Query prompt (WB-P4).
    fn dispatch_command(&mut self, input: &str) {
        use super::commands::Command;
        match super::commands::parse(input) {
            Command::Shape { label, ops } => {
                // Preview before it enters the pending delta.
                self.hjson_preview = Some((label, ops));
                self.focus = Focus::ConfirmationOverlay;
                self.status = "review the delta — y accept · n discard".into();
            }
            Command::Write => self.write_pending(),
            Command::Undo => self.undo_pending(),
            Command::Reset => {
                let n = self.pending_ops.len();
                self.pending_ops.clear();
                self.refresh_plausibility();
                self.persist_pending();
                self.status = format!("reset — discarded {n} pending delta(s)");
            }
            Command::Diff => {
                self.status = if self.pending_ops.is_empty() {
                    "no pending deltas".into()
                } else {
                    format!(
                        "{} pending delta(s): {}",
                        self.pending_ops.len(),
                        self.pending_ops.iter().map(|o| o.preview()).collect::<Vec<_>>().join(" · ")
                    )
                };
            }
            Command::Compile => self.run_compile(),
            Command::Validate => self.run_validate(),
            Command::Wfact(text) => self.run_wfact(&text),
            Command::Research(query) => self.run_research(&query),
            Command::Interview => self.start_interview(),
            Command::Journey => self.run_journey(),
            Command::Sessions => self.run_sessions(),
            Command::Export { pdf } => self.run_export(pdf),
            Command::Switch(name) => self.run_switch(&name),
            Command::Roll(n) => self.run_roll(n),
            Command::Map => self.run_map(),
            Command::MapCheck => self.run_mapcheck(),
            Command::Terrain => self.write_terrain_dem(),
            Command::Unknown(msg) => self.status = msg,
        }
    }

    /// Collect every `fact:world` paragraph as `(title, body)`, in tree order.
    fn collect_world_facts(&self) -> Vec<(String, String)> {
        let Some(book_id) = self.facts_tree.root else { return Vec::new() };
        let mut out = Vec::new();
        // Walk the Facts tree depth-first so facts export in reading order.
        let mut stack: Vec<Uuid> = self
            .hierarchy
            .children_of(Some(book_id))
            .into_iter()
            .rev()
            .map(|n| n.id)
            .collect();
        while let Some(id) = stack.pop() {
            if let Some(node) = self.hierarchy.get(id) {
                if node.kind == NodeKind::Paragraph
                    && node.tags.iter().any(|t| t == FACT_WORLD_TAG)
                {
                    let body = self
                        .store
                        .get_content(id)
                        .ok()
                        .flatten()
                        .map(|b| String::from_utf8_lossy(&b).into_owned())
                        .unwrap_or_default();
                    out.push((node.title.clone(), body));
                }
                for child in self.hierarchy.children_of(Some(id)).into_iter().rev() {
                    stack.push(child.id);
                }
            }
        }
        out
    }

    /// `/export` — assemble a readable Markdown dossier (compiled state,
    /// plausibility, ledger, `fact:world` facts, and the Journey) and write it
    /// atomically under `exports/`. Compiles the world fresh so it works even
    /// `/roll [n]` (WS-P1) — compile `n` candidate worlds from the current
    /// declaration on derived seeds (`base + i`) and report a comparison into
    /// Chat. Pure and deterministic — the same declaration under different seeds
    /// yields decorrelated worlds (the compiler keys its SplitMix64 on the seed),
    /// so this explores the space of worlds the physics implies. `/adopt <seed>`
    /// then sets one as a pending edit.
    fn run_roll(&mut self, n: usize) {
        let Some(base) = self.current_world_def() else {
            self.status = "no world to roll — declare one first (interview or /set)".into();
            return;
        };
        let base_seed = base.seed_u64();
        let mut body = format!("Seed roll — {n} candidate(s), base 0x{base_seed:x}\n");
        body.push_str("  seed              ★   cont  sea%    pop     °C\n");
        for i in 0..n {
            let seed = base_seed.wrapping_add(i as u64);
            let mut def = base.clone();
            def.seed = crate::world::types::SeedValue::Str(format!("0x{seed:x}"));
            let layers = crate::world::plausibility::compile_layers(&def);
            let warns = crate::world::plausibility::run_fast(&def);
            let score = crate::world::plausibility::compute_plausibility_score(&warns);
            let (g, c, d) = (&layers.geology, &layers.climate, &layers.demographics);
            let marker = if i == 0 { "*" } else { " " };
            body.push_str(&format!(
                "{marker} 0x{seed:<13x} {score:>3}  {:>4}  {:>4.0}  {:>7}  {:>5.1}\n",
                g.continents,
                g.sea_coverage_pct,
                Self::compact_pop(d.total_population),
                c.mean_land_temp_c,
            ));
        }
        body.push_str("(* = current) · /adopt <seed> to set one (a pending edit → /write)");
        self.push_turn(format!("/roll {n}"), body);
        self.status = format!("rolled {n} candidate world(s) · /adopt <seed>");
    }

    /// `/map` (WS-P2) — render the world map with plakat and show the raster in
    /// the Map pane on image-capable terminals; otherwise the ASCII biome map
    /// stands in. Graceful at every step: no image support, no plakat, or a render
    /// failure each fall back to ASCII with a status note.
    fn run_map(&mut self) {
        self.right_pane = RightPane::Map;
        // Keep the ASCII fallback populated so the pane always shows something.
        if self.compiled_layers.is_none() {
            if let Some(def) = self.current_world_def() {
                self.populate_map_caches(&def);
            }
        }
        if self.image_picker.is_none() {
            self.status = "Map: this terminal can't display images — showing the ASCII map \
                           (needs kitty/iTerm2/sixel + images.preview_enabled)"
                .into();
            return;
        }
        let Some(def) = self.current_world_def() else {
            self.status = "no world to map — declare one first (interview or /set)".into();
            return;
        };
        let art = match crate::cli::realworld::render_world_map(&self.layout.root, &def, None) {
            Ok(a) => a,
            Err(e) => {
                self.map_raster = None;
                self.status = format!("map: {e} — showing the ASCII map");
                return;
            }
        };
        let img = std::fs::read(&art.png_path)
            .ok()
            .and_then(|bytes| image::load_from_memory(&bytes).ok());
        match img {
            Some(img) => {
                // Borrow the picker only for the protocol build, then store.
                let proto = self.image_picker.as_ref().unwrap().new_resize_protocol(img);
                self.map_raster = Some(std::cell::RefCell::new(proto));
                self.status = "rendered the world map (plakat raster)".into();
            }
            None => {
                self.map_raster = None;
                self.status = "map rendered but its PNG could not be read — ASCII map".into();
            }
        }
    }

    /// The source-grid dimensions of the compiled map, if any (MAPED-P1).
    fn map_source_dims(&self) -> Option<(usize, usize)> {
        self.compiled_layers.as_ref().map(|l| (l.geology.width, l.geology.height))
    }

    /// MAPED-P8 — a left-click in the Map pane focuses it, enters edit mode, and
    /// positions the cursor on the clicked source cell. Other mouse events are
    /// ignored (placement stays on the single-key tools).
    fn handle_mouse(&mut self, mouse: crossterm::event::MouseEvent) {
        use crossterm::event::{MouseButton, MouseEventKind};
        if !matches!(mouse.kind, MouseEventKind::Down(MouseButton::Left)) {
            return;
        }
        let Some(inner) = self.map_pane_rect.get() else { return };
        let Some((sw, sh)) = self.map_source_dims() else { return };
        let Some((sx, sy)) = super::map::click_to_source(inner, mouse.column, mouse.row, sw, sh)
        else {
            return;
        };
        self.right_pane = RightPane::Map;
        self.focus = Focus::RightPane;
        if !self.map_edit {
            self.enter_map_edit();
        }
        self.map_cursor = (sx, sy);
        self.status = format!("cursor ({sx},{sy}) — t/n/g place · r river · o road · +/− terrain");
    }

    /// Raise (`delta > 0`) or lower terrain under the brush (MAPED-P7). Seeds the
    /// edit buffer from the compiled heightmap on first use; the map then previews
    /// the sculpt live. No world write until `/terrain`.
    fn sculpt_terrain(&mut self, delta: f32) {
        let Some((w, h)) = self.map_source_dims() else {
            self.status = "no terrain — run /compile first".into();
            return;
        };
        if self.map_terrain.as_ref().map(|t| t.len()) != Some(w * h) {
            let Some(layers) = self.compiled_layers.as_ref() else { return };
            if layers.geology.heightmap.len() != w * h {
                self.status = "terrain grid unavailable".into();
                return;
            }
            self.map_terrain = Some(layers.geology.heightmap.clone());
            self.map_terrain_sea = layers.geology.sea_level;
        }
        let (cx, cy) = self.map_cursor;
        let radius = self.map_brush;
        if let Some(hm) = self.map_terrain.as_mut() {
            super::map::apply_brush(hm, w, h, cx, cy, radius, delta);
        }
        let verb = if delta > 0.0 { "raised" } else { "lowered" };
        self.status = format!("terrain {verb} at ({cx},{cy}) · brush r{radius} · /terrain to save");
    }

    /// `/terrain` (MAPED-P7) — write the edited heightmap as a 16-bit DEM PNG under
    /// `assets/maps/` and set `geology.dem` (a pending edit). The `realworld`
    /// compiler is DEM-aware, so a subsequent compile builds the world from the
    /// sculpted terrain. (The worldbuilder's own `/compile` stays procedural.)
    fn write_terrain_dem(&mut self) {
        let Some((w, h)) = self.map_source_dims() else {
            self.status = "no terrain to save".into();
            return;
        };
        let Some(hm) = self.map_terrain.clone() else {
            self.status = "no terrain edits yet — raise/lower with + / − first".into();
            return;
        };
        if hm.len() != w * h {
            self.status = "terrain grid size mismatch".into();
            return;
        }
        let dir = self.layout.root.join("assets").join("maps");
        if let Err(e) = std::fs::create_dir_all(&dir) {
            self.status = format!("terrain save failed: {e}");
            return;
        }
        let rel = "assets/maps/terrain.png";
        let raw: Vec<u16> = hm.iter().map(|&e| (e.clamp(0.0, 1.0) * 65535.0) as u16).collect();
        let Some(img) =
            image::ImageBuffer::<image::Luma<u16>, Vec<u16>>::from_raw(w as u32, h as u32, raw)
        else {
            self.status = "terrain buffer invalid".into();
            return;
        };
        if let Err(e) = img.save(self.layout.root.join(rel)) {
            self.status = format!("terrain PNG write failed: {e}");
            return;
        }
        let sea_px = (self.map_terrain_sea.clamp(0.0, 1.0) * 65535.0) as u16;
        let value = serde_json::json!({ "path": rel, "sea_level_pixel_value": sea_px });
        self.pending_ops.push(super::commands::Op::Set {
            path: vec!["geology".into(), "dem".into()],
            value,
        });
        // Rebuild caches (procedural — the worldbuilder compile isn't DEM-aware),
        // but keep the terrain preview.
        let preview = self.map_terrain.take();
        self.refresh_plausibility();
        if let Some(def) = self.current_world_def() {
            self.populate_map_caches(&def);
        }
        self.map_terrain = preview;
        self.record_turn("map: terrain DEM".into(), format!("wrote {rel}"), Vec::new());
        self.status = format!("wrote {rel} + geology.dem · /write to commit (realworld compile uses it)");
    }

    /// `/mapcheck` (MAPED-P5) — check the map layer against the compiled world.
    /// Reports the plausibility score, the deterministic physics warnings, and the
    /// spatial map findings (a town in the sea, an off-map coord) into Chat, flags
    /// the offending cells on the map (`!`), and jumps the cursor to the first.
    fn run_mapcheck(&mut self) {
        self.refresh_plausibility();
        let Some(def) = self.current_world_def() else {
            self.status = "no world to check — declare one first (interview or /set)".into();
            return;
        };
        self.populate_map_caches(&def);
        let Some(layers) = self.compiled_layers.as_ref() else {
            self.status = "no world to check".into();
            return;
        };
        let findings = super::map::lint_map(&def, layers);

        let mut body = format!(
            "Map check — plausibility {}/100\n",
            self.plausibility_score.unwrap_or(100)
        );
        if findings.is_empty() {
            body.push_str("Map layer: no problems found.\n");
        } else {
            body.push_str(&format!("Map layer — {} issue(s):\n", findings.len()));
            for f in &findings {
                body.push_str(&format!("  ! {}\n", f.text));
            }
        }
        if !self.plausibility_warnings.is_empty() {
            body.push_str("World checks:\n");
            for w in self.plausibility_warnings.iter().take(8) {
                body.push_str(&format!("  · {}\n", w.text));
            }
        }

        let spatial = findings.iter().filter(|f| f.at.is_some()).count();
        self.map_findings = findings;
        self.map_finding_idx = 0;
        self.right_pane = RightPane::Map;
        if let Some(at) = self.map_findings.iter().find_map(|f| f.at) {
            self.map_cursor = at;
            self.enter_map_edit();
        }
        self.push_turn("/mapcheck".into(), body);
        self.status = format!("map check — {spatial} spatial issue(s) · f: jump to next");
    }

    /// `f` in the Map pane — jump the cursor to the next spatial `/mapcheck`
    /// finding and echo its message (P5).
    fn jump_next_finding(&mut self) {
        let spatial: Vec<(usize, usize, String)> = self
            .map_findings
            .iter()
            .filter_map(|f| f.at.map(|at| (at.0, at.1, f.text.clone())))
            .collect();
        if spatial.is_empty() {
            self.status = "no map-check findings — run /mapcheck".into();
            return;
        }
        self.map_finding_idx = (self.map_finding_idx + 1) % spatial.len();
        let (x, y, text) = &spatial[self.map_finding_idx];
        self.map_cursor = (*x, *y);
        self.status = format!("[{}/{}] {text}", self.map_finding_idx + 1, spatial.len());
    }

    /// Enter Map edit mode. Auto-compiles the map if needed. Clamps the cursor
    /// into the current grid.
    fn enter_map_edit(&mut self) {
        if self.compiled_layers.is_none() {
            if let Some(def) = self.current_world_def() {
                self.populate_map_caches(&def);
            }
        }
        let Some((w, h)) = self.map_source_dims() else {
            self.status = "no world to map — declare one first (interview or /set)".into();
            return;
        };
        self.map_edit = true;
        let (cx, cy) = self.map_cursor;
        self.map_cursor = (cx.min(w.saturating_sub(1)), cy.min(h.saturating_sub(1)));
        self.status = "map edit — hjkl move · t town · n name · d delete · Esc leave".into();
    }

    /// Open the name-entry prompt for placing a landmark of `geo_kind` at the
    /// cursor (P2).
    fn open_map_input(&mut self, label: &'static str, geo_kind: &'static str) {
        let (x, y) = self.map_cursor;
        self.map_input = Some(MapInput {
            label,
            placement: MapPlacement::Landmark { geo_kind, x, y },
            buffer: String::new(),
        });
        self.status = format!("{label} at ({x},{y}) — type a name, Enter to place, Esc to cancel");
    }

    /// Open the region name-entry, auto-filling the biome from the compiled cell
    /// under the cursor (P4).
    fn open_region_input(&mut self) {
        let (x, y) = self.map_cursor;
        let biome = self
            .compiled_layers
            .as_ref()
            .and_then(|l| {
                let idx = y.min(l.climate.height.saturating_sub(1)) * l.climate.width
                    + x.min(l.climate.width.saturating_sub(1));
                l.climate.biome.get(idx).map(|b| b.as_str().to_string())
            })
            .unwrap_or_default();
        self.map_input = Some(MapInput {
            label: "Region name",
            placement: MapPlacement::Region { x, y, biome: biome.clone() },
            buffer: String::new(),
        });
        self.status = format!("Region at ({x},{y}) · biome {biome} — type a name, Enter to place");
    }

    /// Place a region anchor into `geography.regions[]` as a pending edit (P4).
    fn place_region(&mut self, x: usize, y: usize, name: String, biome: String) {
        let value = serde_json::json!({ "name": name, "biome": biome, "x": x, "y": y });
        self.pending_ops.push(super::commands::Op::Push {
            path: vec!["geography".into(), "regions".into()],
            value,
        });
        self.refresh_plausibility();
        if let Some(def) = self.current_world_def() {
            self.populate_map_caches(&def);
        }
        self.record_turn(format!("map: region {name}"), format!("{biome} at ({x},{y})"), Vec::new());
        self.status = format!("placed region '{name}' ({biome}) at ({x},{y}) · /write to commit");
    }

    /// The declared-landmark name exactly under the cursor, if any (P6).
    fn landmark_name_at_cursor(&self) -> Option<String> {
        let c = self.map_cursor;
        self.map_landmarks.iter().find(|m| (m.x, m.y) == c).map(|m| m.name.clone())
    }

    /// Place a road into `geography.roads[]` connecting two named landmarks (P6).
    fn place_road(&mut self, from: String, to: String) {
        let value = serde_json::json!({ "from": from, "to": to, "kind": "road" });
        self.pending_ops.push(super::commands::Op::Push {
            path: vec!["geography".into(), "roads".into()],
            value,
        });
        self.refresh_plausibility();
        if let Some(def) = self.current_world_def() {
            self.populate_map_caches(&def);
        }
        self.record_turn(format!("map: road {from}{to}"), String::new(), Vec::new());
        self.status = format!("road '{from}' – '{to}' · /write to commit");
    }

    /// Advance the active multi-step map tool on `Enter` (P3 river / P6 road).
    fn advance_map_tool(&mut self) {
        match self.map_tool.take() {
            Some(MapTool::River { source }) => {
                self.map_tool = Some(MapTool::River { source });
                self.river_pick();
            }
            Some(MapTool::Road { from }) => match from {
                None => match self.landmark_name_at_cursor() {
                    Some(name) => {
                        self.map_tool = Some(MapTool::Road { from: Some(name.clone()) });
                        self.status =
                            format!("road from '{name}' — move to the other landmark, Enter");
                    }
                    None => {
                        self.map_tool = Some(MapTool::Road { from: None });
                        self.status = "put the cursor on a landmark first".into();
                    }
                },
                Some(first) => match self.landmark_name_at_cursor() {
                    Some(second) if second != first => self.place_road(first, second),
                    Some(_) => {
                        self.map_tool = Some(MapTool::Road { from: Some(first) });
                        self.status = "pick a different landmark".into();
                    }
                    None => {
                        self.map_tool = Some(MapTool::Road { from: Some(first) });
                        self.status = "put the cursor on the other landmark".into();
                    }
                },
            },
            None => {}
        }
    }

    /// Advance the river tool (P3): the first Enter fixes the source, the second
    /// fixes the mouth and asks for a name.
    fn river_pick(&mut self) {
        let cursor = self.map_cursor;
        match self.map_tool {
            Some(MapTool::River { source: None }) => {
                self.map_tool = Some(MapTool::River { source: Some(cursor) });
                self.status = format!(
                    "river source ({},{}) — move to the mouth, Enter to set · Esc cancel",
                    cursor.0, cursor.1
                );
            }
            Some(MapTool::River { source: Some(src) }) => {
                self.map_tool = None;
                self.map_input = Some(MapInput {
                    label: "River name",
                    placement: MapPlacement::River { from: src, to: cursor },
                    buffer: String::new(),
                });
                self.status = "name the river — Enter to place, Esc to cancel".into();
            }
            _ => {}
        }
    }

    /// Place a river into `hydrology.rivers[]` as a pending edit, re-populate the
    /// map (the compiled hydrology honours the declared course, so it renders),
    /// and surface `lint_rivers` immediately (P3).
    fn place_river(&mut self, from: (usize, usize), to: (usize, usize), name: String) {
        let value = serde_json::json!({
            "name": name,
            "from": [from.0, from.1],
            "to": [to.0, to.1],
        });
        self.pending_ops.push(super::commands::Op::Push {
            path: vec!["hydrology".into(), "rivers".into()],
            value,
        });
        self.refresh_plausibility();
        if let Some(def) = self.current_world_def() {
            self.populate_map_caches(&def);
        }
        self.record_turn(
            format!("map: river {name}"),
            format!("({},{}) → ({},{})", from.0, from.1, to.0, to.1),
            Vec::new(),
        );
        let lint = self.river_lint_summary();
        if lint.is_empty() {
            self.status = format!("placed river '{name}' · /write to commit");
        } else {
            self.push_turn(format!("river {name}"), format!("{lint}"));
            self.status = format!("river '{name}' placed — check: {lint}");
        }
    }

    /// The current declared-river lint findings, joined for a status/chat line.
    fn river_lint_summary(&self) -> String {
        let Some(def) = self.current_world_def() else { return String::new() };
        let Some(layers) = self.compiled_layers.as_ref() else { return String::new() };
        let Some(hydro_def) = def.hydrology.as_ref() else { return String::new() };
        crate::world::compile::hydrology_layer::lint_rivers(hydro_def, &layers.geology)
            .iter()
            .map(|w| w.text.clone())
            .collect::<Vec<_>>()
            .join(" · ")
    }

    /// Place a named landmark into `geography.landmarks[]` as a pending edit, then
    /// re-populate the map caches so it appears immediately (P2).
    fn place_landmark(&mut self, x: usize, y: usize, name: String, geo_kind: &str) {
        let value = serde_json::json!({ "name": name, "kind": geo_kind, "x": x, "y": y });
        self.pending_ops.push(super::commands::Op::Push {
            path: vec!["geography".into(), "landmarks".into()],
            value,
        });
        self.refresh_plausibility();
        if let Some(def) = self.current_world_def() {
            self.populate_map_caches(&def);
        }
        self.record_turn(format!("map: {geo_kind} {name}"), format!("placed at ({x},{y})"), Vec::new());
        self.status = format!("placed {geo_kind} '{name}' at ({x},{y}) · /write to commit");
    }

    /// Delete the declared feature under the cursor — a landmark first, else a
    /// region — via `Op::RemoveAt` (index computed against the current disk +
    /// pending array, which is the array state at write time) (P2/P4).
    fn delete_feature_at_cursor(&mut self) {
        let Some((w, h)) = self.map_source_dims() else { return };
        let cursor = self.map_cursor;
        let Some(def) = self.current_world_def() else { return };
        let geo = def.geography.as_ref();
        // Landmark under the cursor?
        if let Some(i) =
            geo.and_then(|g| g.landmarks.iter().position(|lm| lm.grid(w, h) == Some(cursor)))
        {
            self.remove_feature("landmarks", i, "landmark", cursor);
            return;
        }
        // Else a region anchored on this cell?
        if let Some(i) = geo.and_then(|g| {
            g.regions.iter().position(|r| match (r.x, r.y) {
                (Some(x), Some(y)) => (x.min(w - 1), y.min(h - 1)) == cursor,
                _ => false,
            })
        }) {
            self.remove_feature("regions", i, "region", cursor);
            return;
        }
        self.status = "no landmark or region at the cursor".into();
    }

    /// Push a `RemoveAt` for `geography.<array>[index]`, re-populate, and report.
    fn remove_feature(&mut self, array: &str, index: usize, label: &str, at: (usize, usize)) {
        self.pending_ops.push(super::commands::Op::RemoveAt {
            path: vec!["geography".into(), array.into()],
            index,
        });
        self.refresh_plausibility();
        if let Some(def) = self.current_world_def() {
            self.populate_map_caches(&def);
        }
        self.status = format!("removed {label} at ({},{}) · /write to commit", at.0, at.1);
    }

    /// Route a movement key to the cursor, returning whether it was one. Handles
    /// coarse (hjkl / arrows) and fine (HJKL / Shift+arrows) steps. Shared by all
    /// map tools so navigation always works.
    fn try_map_move(&mut self, key: KeyEvent) -> bool {
        let shift = key.modifiers.contains(KeyModifiers::SHIFT);
        match key.code {
            KeyCode::Left => self.move_map_cursor(-1, 0, shift),
            KeyCode::Right => self.move_map_cursor(1, 0, shift),
            KeyCode::Up => self.move_map_cursor(0, -1, shift),
            KeyCode::Down => self.move_map_cursor(0, 1, shift),
            KeyCode::Char('h') => self.move_map_cursor(-1, 0, false),
            KeyCode::Char('l') => self.move_map_cursor(1, 0, false),
            KeyCode::Char('k') => self.move_map_cursor(0, -1, false),
            KeyCode::Char('j') => self.move_map_cursor(0, 1, false),
            KeyCode::Char('H') => self.move_map_cursor(-1, 0, true),
            KeyCode::Char('L') => self.move_map_cursor(1, 0, true),
            KeyCode::Char('K') => self.move_map_cursor(0, -1, true),
            KeyCode::Char('J') => self.move_map_cursor(0, 1, true),
            _ => return false,
        }
        true
    }

    /// Move the edit cursor in source-grid cells. `fine` steps a single cell;
    /// otherwise a coarse step (~1/40 of the grid) for quick travel. Clamped.
    fn move_map_cursor(&mut self, dx: i32, dy: i32, fine: bool) {
        let Some((w, h)) = self.map_source_dims() else { return };
        let step_x = if fine { 1 } else { (w / 40).max(1) } as i32;
        let step_y = if fine { 1 } else { (h / 40).max(1) } as i32;
        let (cx, cy) = self.map_cursor;
        let nx = (cx as i32 + dx * step_x).clamp(0, w as i32 - 1) as usize;
        let ny = (cy as i32 + dy * step_y).clamp(0, h as i32 - 1) as usize;
        self.map_cursor = (nx, ny);
    }

    /// A compact population string (`4.1M`, `820k`, `512`) for the roll table.
    fn compact_pop(p: u64) -> String {
        if p >= 1_000_000 {
            format!("{:.1}M", p as f64 / 1_000_000.0)
        } else if p >= 1_000 {
            format!("{:.0}k", p as f64 / 1_000.0)
        } else {
            p.to_string()
        }
    }

    /// before `/compile`.
    fn run_export(&mut self, pdf: bool) {
        let compiled = self
            .current_world_def()
            .map(|def| {
                let layers = crate::world::plausibility::compile_layers(&def);
                crate::world::plausibility::summarise_compiled(&def, &layers)
            });
        let facts = self.collect_world_facts();
        let at = chrono::Utc::now().to_rfc3339();
        let world_name = self.world_name().to_string();
        let input = super::export::DossierInput {
            world_name: &world_name,
            generated_at: &at,
            compiled: compiled.as_deref(),
            score: self.plausibility_score,
            warnings: &self.plausibility_warnings,
            ledger: self.ledger_snapshot.as_ref(),
            facts: &facts,
            journey: &self.session.turns,
        };
        let md = super::export::build_dossier(&input);
        let dir = self.layout.root.join("exports");
        if let Err(e) = std::fs::create_dir_all(&dir) {
            self.status = format!("export failed: {e}");
            return;
        }
        let md_path = dir.join(format!("dossier-{}.md", self.session.slug));
        if let Err(e) = crate::io_atomic::write(&md_path, md.as_bytes()) {
            self.status = format!("export failed: {e}");
            return;
        }
        let md_rel = md_path.strip_prefix(&self.layout.root).unwrap_or(&md_path).display().to_string();

        if !pdf {
            self.push_turn("/export".into(), format!("Wrote world dossier → {md_rel}"));
            self.status = format!("exported → {md_rel}");
            return;
        }

        // `--pdf`: render the same dossier through Typst, in-process.
        let typ = super::export::build_dossier_typst(&input);
        let settings = crate::typst_world::WorldSettings::from_cfg(&self.cfg.typst_compile);
        match crate::typst_inprocess::compile_source_to_pdf(&self.layout.root, typ, settings) {
            Ok(bytes) => {
                let pdf_path = dir.join(format!("dossier-{}.pdf", self.session.slug));
                match crate::io_atomic::write(&pdf_path, &bytes) {
                    Ok(()) => {
                        let pdf_rel = pdf_path.strip_prefix(&self.layout.root).unwrap_or(&pdf_path).display();
                        self.push_turn("/export --pdf".into(), format!("Wrote {md_rel} and {pdf_rel}"));
                        self.status = format!("exported → {pdf_rel}");
                    }
                    Err(e) => self.status = format!("wrote {md_rel}; PDF write failed: {e}"),
                }
            }
            Err(e) => {
                let first = e.lines().next().unwrap_or("compile error");
                self.push_turn("/export --pdf".into(), format!("Wrote {md_rel}; PDF compile failed: {first}"));
                self.status = format!("wrote {md_rel}; PDF failed (see Chat)");
            }
        }
    }

    /// `/journey` — render the session timeline (the Worldbuilding Journey) into
    /// the Chat pane: each recorded turn with its plausibility arc.
    fn run_journey(&mut self) {
        let count = self.session.turns.len();
        if count == 0 {
            self.push_turn("/journey".into(), "No journey yet — shape the world and it fills in.".into());
            self.status = "journey — empty".into();
            return;
        }
        let mut body = format!(
            "Worldbuilding Journey · {} · {count} step(s)\n",
            self.session.name,
        );
        for t in &self.session.turns {
            // Trim the timestamp to the date+minute for a compact line.
            let when = t.at.get(..16).unwrap_or(&t.at);
            let arc = match (t.plausibility_before, t.plausibility_after) {
                (Some(b), Some(a)) if a != b => format!("{b}{a}"),
                (_, Some(a)) => format!("{a}"),
                _ => String::new(),
            };
            let facts = if t.facts_inserted.is_empty() {
                String::new()
            } else {
                format!("{}", t.facts_inserted.len())
            };
            body.push_str(&format!(
                "{:>3}. {when}  {}{}{arc}{facts}\n",
                t.seq,
                t.user.trim(),
                t.assistant_summary.trim(),
            ));
        }
        self.push_turn("/journey".into(), body);
        self.status = format!("journey — {count} step(s)");
    }

    /// `/sessions` — list the project's worldbuilder sessions (the current one
    /// marked). Session switching is a later refinement; this surfaces what exists.
    fn run_sessions(&mut self) {
        let slugs = super::session::WorldbuilderSession::list(&self.layout);
        if slugs.is_empty() {
            self.status = "no sessions".into();
            return;
        }
        let list = slugs
            .iter()
            .map(|s| if *s == self.session.slug { format!("{s} (current)") } else { format!("  {s}") })
            .collect::<Vec<_>>()
            .join("\n");
        self.push_turn("/sessions".into(), format!("Sessions:\n{list}"));
        self.status = format!("{} session(s) — /switch <name> or --session <name>", slugs.len());
    }

    /// `/switch <name>` (WS-P3) — persist the current session, then open (or
    /// create) the named one and swap in its pending delta. `world.hjson` is
    /// shared across a project's sessions, so switching changes only the pending
    /// edits, timeline, and pane sizing — and starts a fresh conversation.
    fn run_switch(&mut self, name: &str) {
        // Flush the current session (pending + turns are mirrored on change;
        // capture the latest sizing too).
        self.session.left_split = self.left_split;
        self.session.split_ratio = self.split_ratio;
        self.session.pending_ops = self.pending_ops.clone();
        let _ = self.session.save(&self.layout);

        let now = chrono::Utc::now().to_rfc3339();
        let target = match super::session::WorldbuilderSession::open_or_create(&self.layout, name, now) {
            Ok(s) => s,
            Err(e) => {
                self.status = format!("switch failed: {e}");
                return;
            }
        };
        let slug = target.slug.clone();
        self.left_split = target.left_split.clamp(2, 8);
        self.split_ratio = target.split_ratio.clamp(2, 8);
        self.pending_ops = target.pending_ops.clone();
        self.session = target;
        // Fresh conversation + research state for the new session.
        self.chat.clear();
        self.chat_scroll = 0;
        self.research_query = None;
        self.research_hits.clear();
        self.research_cursor = 0;
        self.interview = None;
        self.hjson_preview = None;
        self.refresh_plausibility(); // rescores pending; clears compiled/map caches
        self.status = format!("switched to session '{slug}' · {} pending delta(s)", self.pending_ops.len());
    }

    /// Start (or restart) the guided interview: focus the Query prompt, surface
    /// the Chat pane, and post the first question. Answers accumulate into the
    /// pending delta (reviewable with `/diff`, committed with `/write`).
    fn start_interview(&mut self) {
        let iv = super::interview::Interview::new();
        self.interview = Some(iv);
        self.right_pane = RightPane::Chat;
        self.focus = Focus::QueryPrompt;
        self.push_turn(
            String::new(),
            "Interview — I'll ask about the sky, land, people, and rules. Answer in your own \
             words (blank to skip a question, Esc to leave). Your answers become pending edits; \
             review them with /diff and commit with /write, then /compile."
                .to_string(),
        );
        self.post_interview_question();
        self.status = "interview started — answer in the Query prompt · Esc to leave".into();
    }

    /// Post the current interview step as a chat turn (pinned to the bottom, so it
    /// stays visible as the conversation grows).
    fn post_interview_question(&mut self) {
        let q = self.interview.as_ref().and_then(|iv| {
            iv.current().map(|s| {
                let (n, total) = iv.progress();
                format!("[{} · {n}/{total}] {}", s.stage.label(), s.prompt)
            })
        });
        if let Some(q) = q {
            self.push_turn(String::new(), q);
        }
    }

    /// Feed one answer to the active interview: fill the current step's command
    /// template, record its ops into the pending delta, and advance. A blank
    /// answer skips; a malformed one is reported and the step is retried.
    fn submit_interview_answer(&mut self, answer: &str) {
        let answer = answer.trim();
        let Some(step) = self.interview.as_ref().and_then(|iv| iv.current()) else {
            self.interview = None;
            return;
        };
        // Blank → skip this question.
        if answer.is_empty() {
            if let Some(iv) = self.interview.as_mut() {
                iv.advance();
            }
            self.push_turn(String::new(), "(skipped)".into());
            self.after_interview_step();
            return;
        }

        let cmd = step.template.replace("{}", answer);
        match super::commands::parse(&cmd) {
            super::commands::Command::Shape { label, ops } => {
                self.pending_ops.extend(ops);
                self.refresh_plausibility();
                let d = self.plausibility_delta_chip();
                let note = if d.is_empty() {
                    format!("recorded · {label}")
                } else {
                    format!("recorded · {label}  (★ {d})")
                };
                self.push_turn(answer.to_string(), note);
                self.record_turn(format!("interview: {answer}"), label, Vec::new());
                if let Some(iv) = self.interview.as_mut() {
                    iv.advance();
                }
                self.after_interview_step();
            }
            super::commands::Command::Unknown(msg) => {
                // Keep the step; let the author retry.
                self.push_turn(answer.to_string(), format!("didn't take that — {msg}"));
            }
            _ => {
                // A template that parses to a session command shouldn't happen;
                // skip defensively rather than loop.
                if let Some(iv) = self.interview.as_mut() {
                    iv.advance();
                }
                self.after_interview_step();
            }
        }
    }

    /// Post the next question, or close the interview when the script is done.
    fn after_interview_step(&mut self) {
        let done = self.interview.as_ref().map(|iv| iv.done()).unwrap_or(true);
        if done {
            self.interview = None;
            let n = self.pending_ops.len();
            self.push_turn(
                String::new(),
                format!(
                    "That's the frame — {n} pending edit(s). Review with /diff, commit with \
                     /write, then /compile to see the world your choices imply."
                ),
            );
            self.status = format!("interview complete — {n} pending edit(s) · /diff · /write");
        } else {
            self.post_interview_question();
            self.status = "interview — answer in the Query prompt · Esc to leave".into();
        }
    }

    /// `/wfact <statement>` — record an author-decided world fact into the Facts
    /// book, tagged `fact:world`, using the shared research insertion primitive
    /// (auto-reembeds → immediately retrievable). No AI: the author decides, the
    /// worldbuilder records. Inserts near the tree cursor when it is inside Facts,
    /// else at the end of the book; then reveals the new fact.
    fn run_wfact(&mut self, text: &str) {
        let text = text.trim();
        if text.is_empty() {
            return;
        }
        let Some(book_id) = self.facts_tree.root else {
            self.status = "no Facts book — create one in the editor first".into();
            return;
        };
        // A short title from the first clause/line; the full statement is the body.
        let title: String = text.split(['.', '\n']).next().unwrap_or(text).trim().chars().take(60).collect();
        let target = self.facts_tree.selected();
        let new_id = match crate::research::insert::insert_paragraph(
            &self.store,
            &self.cfg,
            &self.hierarchy,
            book_id,
            target,
            &title,
            text,
        ) {
            Ok(id) => id,
            Err(e) => {
                self.status = format!("fact write failed: {e}");
                return;
            }
        };
        // Tag it fact:world (the whole point — it must show as ◎ and feed the
        // world-fact RAG). Reload first so the node is in the hierarchy.
        self.reload_hierarchy();
        if let Some(node) = self.hierarchy.get(new_id) {
            let mut updated = node.clone();
            if !updated.tags.iter().any(|t| t == FACT_WORLD_TAG) {
                updated.tags.push(FACT_WORLD_TAG.to_string());
                if let Err(e) = self.store.raw().update_metadata(new_id, updated.to_json()) {
                    self.status = format!("recorded, but tag write failed: {e}");
                    return;
                }
            }
        }
        self.reload_hierarchy();
        self.facts_tree.reveal(&self.hierarchy, new_id);
        self.record_turn(format!("/wfact {title}"), "◎ recorded fact:world".into(), vec![new_id.to_string()]);
        self.status = "◎ recorded fact:world".into();
    }

    /// `/research <query>` — semantically retrieve related Facts (the whole Facts
    /// book, not only `fact:world`, so the author can discover material to promote)
    /// and surface them in the Research right-pane. Pure retrieval, no generation.
    fn run_research(&mut self, query: &str) {
        let query = query.trim();
        if query.is_empty() {
            return;
        }
        let Some(book_id) = self.facts_tree.root else {
            self.status = "no Facts book to research".into();
            return;
        };
        match crate::book_rag::retrieval::retrieve(
            &self.store,
            &self.hierarchy,
            &self.cfg.book_rag,
            book_id,
            query,
        ) {
            Ok(hits) => {
                let n = hits.len();
                self.research_hits = hits;
                self.research_cursor = 0;
                self.research_query = Some(query.to_string());
                self.right_pane = RightPane::Research;
                self.status = format!("research · {n} passage(s) for “{query}” · Ctrl+R → Research");
            }
            Err(e) => self.status = format!("research failed: {e}"),
        }
    }

    /// `/compile` — run the pure layer chain over the current world (disk +
    /// pending), cache a compiled-state summary for the chat prompt, and echo it
    /// into the conversation as a Simulation turn. No LLM, no disk writes.
    fn run_compile(&mut self) {
        match self.current_world_def() {
            Some(def) => {
                self.populate_map_caches(&def);
                let summary = self.compiled_summary.clone().unwrap_or_default();
                self.push_turn("/compile".into(), format!("Compiled world state —\n{summary}"));
                self.status =
                    "compiled — chat reasons over the simulated world · Ctrl+R → Map".into();
            }
            None => {
                self.status = "no world to compile — declare one first (interview or /set)".into();
            }
        }
    }

    /// Compile the world and refresh the Map caches together — `compiled_layers`
    /// (the biome/height grid), `compiled_summary`, and `map_landmarks` (declared
    /// landmarks resolved to source cells for the overlay). Landmark/river/region
    /// edits don't change the grid, so the editor re-runs this after a placement
    /// to keep the map live (refresh_plausibility having cleared the caches).
    fn populate_map_caches(&mut self, def: &crate::world::types::WorldDefinition) {
        let layers = crate::world::plausibility::compile_layers(def);
        self.compiled_summary =
            Some(crate::world::plausibility::summarise_compiled(def, &layers));
        let (w, h) = (layers.geology.width, layers.geology.height);
        self.map_landmarks = def
            .geography
            .as_ref()
            .map(|g| {
                g.landmarks
                    .iter()
                    .filter_map(|lm| {
                        lm.grid(w, h).map(|(x, y)| MapMarker {
                            x,
                            y,
                            name: lm.name.clone(),
                            kind: lm.kind.clone(),
                        })
                    })
                    .collect()
            })
            .unwrap_or_default();
        self.map_regions = def
            .geography
            .as_ref()
            .map(|g| {
                g.regions
                    .iter()
                    .filter_map(|r| match (r.x, r.y) {
                        (Some(x), Some(y)) => Some(MapMarker {
                            x: x.min(w.saturating_sub(1)),
                            y: y.min(h.saturating_sub(1)),
                            name: r.name.clone(),
                            kind: if r.biome.is_empty() { "region".into() } else { r.biome.clone() },
                        }),
                        _ => None,
                    })
                    .collect()
            })
            .unwrap_or_default();
        // P6 — resolve declared roads to endpoint cells by landmark name.
        let by_name: std::collections::HashMap<&str, (usize, usize)> =
            self.map_landmarks.iter().map(|m| (m.name.as_str(), (m.x, m.y))).collect();
        let roads = def
            .geography
            .as_ref()
            .map(|g| {
                g.roads
                    .iter()
                    .filter_map(|r| {
                        let a = *by_name.get(r.from.as_str())?;
                        let b = *by_name.get(r.to.as_str())?;
                        Some((a, b))
                    })
                    .collect()
            })
            .unwrap_or_default();
        drop(by_name);
        self.map_roads = roads;
        self.compiled_layers = Some(layers);
    }

    /// `/validate` — run the deterministic plausibility lints over the current
    /// world and report the score + warnings into the conversation. Reuses the
    /// already-computed `plausibility_warnings` (kept fresh by every delta).
    fn run_validate(&mut self) {
        match self.plausibility_score {
            Some(score) => {
                let mut body = format!("Plausibility {score}/100");
                if self.plausibility_warnings.is_empty() {
                    body.push_str(" — no warnings.");
                } else {
                    body.push_str(&format!("{} warning(s):\n", self.plausibility_warnings.len()));
                    for w in &self.plausibility_warnings {
                        let sev = match w.severity {
                            crate::world::plausibility::Severity::High => "HIGH",
                            crate::world::plausibility::Severity::Medium => "MED ",
                            crate::world::plausibility::Severity::Low => "LOW ",
                        };
                        body.push_str(&format!("  [{sev}] {}\n", w.text));
                    }
                }
                self.push_turn("/validate".into(), body);
                self.status = format!("validated — plausibility {score}/100");
            }
            None => {
                self.status = "no world to validate — declare one first (interview or /set)".into();
            }
        }
    }

    /// `y` in the preview — fold the previewed ops into the pending delta and
    /// rescore (the score responds before `/write` commits to disk).
    fn accept_pending(&mut self) {
        if let Some((label, ops)) = self.hjson_preview.take() {
            self.pending_ops.extend(ops);
            self.refresh_plausibility();
            let d = self.plausibility_delta_chip();
            self.status = if d.is_empty() {
                format!("{label} · /write to commit")
            } else {
                format!("{label}{d} · /write to commit")
            };
            self.record_turn(label, "shaping delta accepted".into(), Vec::new());
        }
        self.focus = Focus::QueryPrompt;
    }

    /// `/write` — fold every pending op into `world.hjson` atomically.
    fn write_pending(&mut self) {
        if self.pending_ops.is_empty() {
            self.status = "nothing to write".into();
            return;
        }
        let path = self.layout.root.join("world.hjson");
        let mut value = std::fs::read_to_string(&path)
            .ok()
            .and_then(|r| serde_hjson::from_str::<serde_json::Value>(&r).ok())
            .unwrap_or_else(|| serde_json::json!({}));
        for op in &self.pending_ops {
            op.apply(&mut value);
        }
        let json = serde_json::to_string_pretty(&value).unwrap_or_default();
        match crate::io_atomic::write(&path, json.as_bytes()) {
            Ok(()) => {
                let n = self.pending_ops.len();
                self.pending_ops.clear();
                self.refresh_plausibility();
                self.status = format!("✓ wrote {n} delta(s) to world.hjson");
                self.record_turn("/write".into(), format!("committed {n} delta(s)"), Vec::new());
            }
            Err(e) => self.status = format!("write failed: {e}"),
        }
    }

    /// `/undo` — drop the last pending op and rescore.
    fn undo_pending(&mut self) {
        if self.pending_ops.pop().is_some() {
            self.refresh_plausibility();
            self.persist_pending();
            self.status = format!("undone — {} pending delta(s) left", self.pending_ops.len());
        } else {
            self.status = "nothing to undo".into();
        }
    }

    /// Shell-wide keys reachable from any navigable pane. Returns whether the key
    /// was consumed.
    fn common_pane_key(&mut self, key: KeyEvent) -> bool {
        match key.code {
            KeyCode::Char('?') => {
                self.show_hints = !self.show_hints;
                true
            }
            KeyCode::Char('q') => {
                self.should_quit = true;
                true
            }
            KeyCode::Char('{') => {
                self.left_split = self.left_split.saturating_sub(1).clamp(2, 8);
                self.persist_sizing();
                true
            }
            KeyCode::Char('}') => {
                self.left_split = (self.left_split + 1).clamp(2, 8);
                self.persist_sizing();
                true
            }
            KeyCode::Char('[') => {
                self.split_ratio = self.split_ratio.saturating_sub(1).clamp(2, 8);
                self.persist_sizing();
                true
            }
            KeyCode::Char(']') => {
                self.split_ratio = (self.split_ratio + 1).clamp(2, 8);
                self.persist_sizing();
                true
            }
            _ => false,
        }
    }

    /// Cursor / fold navigation on the focused tree. Returns whether consumed.
    fn tree_nav_key(&mut self, key: KeyEvent) -> bool {
        let (tree, h) = match self.focus {
            Focus::WorldPane => (&mut self.world_tree, &self.hierarchy),
            _ => (&mut self.facts_tree, &self.hierarchy),
        };
        match key.code {
            KeyCode::Char('j') | KeyCode::Down => {
                tree.move_down();
                true
            }
            KeyCode::Char('k') | KeyCode::Up => {
                tree.move_up();
                true
            }
            KeyCode::Char('g') => {
                tree.to_top();
                true
            }
            KeyCode::Char('G') => {
                tree.to_bottom();
                true
            }
            KeyCode::Char('l') | KeyCode::Right => {
                tree.step_in(h);
                true
            }
            KeyCode::Char('h') | KeyCode::Left => {
                tree.step_out(h);
                true
            }
            KeyCode::Enter => {
                tree.toggle(h);
                true
            }
            _ => false,
        }
    }

    /// Keys for a left (Facts / World) pane. `is_facts` selects Facts-only keys.
    fn left_pane_key(&mut self, key: KeyEvent, is_facts: bool) {
        let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
        if ctrl {
            match key.code {
                KeyCode::Char('p') => {
                    self.pin_toggle();
                    return;
                }
                KeyCode::Char('t') if is_facts => {
                    self.toggle_world_tag();
                    return;
                }
                _ => {}
            }
        }
        if self.common_pane_key(key) {
            return;
        }
        if self.tree_nav_key(key) {
            return;
        }
        match key.code {
            KeyCode::Char('z') => {
                self.zoom = if self.zoom == Some(self.focus) { None } else { Some(self.focus) };
            }
            KeyCode::Char('F') if is_facts => {
                self.facts_filter_world = !self.facts_filter_world;
                self.status = if self.facts_filter_world {
                    "filter: fact:world only".into()
                } else {
                    "filter off".into()
                };
            }
            // Tree editing: WB-P1 rejects compiler-owned World nodes; author edits
            // (add / edit / rename / delete modals) land in a later phase.
            KeyCode::Char('n') | KeyCode::Char('e') | KeyCode::Char('r') | KeyCode::Char('d') => {
                self.reject_or_stub_edit(is_facts);
            }
            _ => {}
        }
    }

    /// `Ctrl+P` — toggle the selected node in the focused pane's pin list.
    fn pin_toggle(&mut self) {
        let sel = match self.focus {
            Focus::WorldPane => self.world_tree.selected(),
            _ => self.facts_tree.selected(),
        };
        let Some(id) = sel else { return };
        let pins = match self.focus {
            Focus::WorldPane => &mut self.world_pins,
            _ => &mut self.facts_pins,
        };
        if let Some(pos) = pins.iter().position(|p| *p == id) {
            pins.remove(pos);
            self.status = "unpinned".into();
        } else {
            pins.push(id);
            self.status = "pinned".into();
        }
    }

    /// `Ctrl+T` (Facts pane) — add/remove the `fact:world` tag on the selected
    /// paragraph, persisted to the store.
    fn toggle_world_tag(&mut self) {
        let Some(id) = self.facts_tree.selected() else { return };
        let Some(node) = self.hierarchy.get(id) else { return };
        if node.kind != NodeKind::Paragraph {
            self.status = "select a fact (a paragraph) to tag fact:world".into();
            return;
        }
        let mut updated = node.clone();
        let now_tagged = match updated.tags.iter().position(|t| t == FACT_WORLD_TAG) {
            Some(pos) => {
                updated.tags.remove(pos);
                false
            }
            None => {
                updated.tags.push(FACT_WORLD_TAG.to_string());
                true
            }
        };
        if let Err(e) = self.store.raw().update_metadata(id, updated.to_json()) {
            self.status = format!("tag write failed: {e}");
            return;
        }
        self.reload_hierarchy();
        self.status = if now_tagged {
            "◎ tagged fact:world".into()
        } else {
            "untagged fact:world".into()
        };
    }

    /// WS-P3 — promote the research hit under the cursor to a world fact: tag its
    /// (existing) Facts paragraph `fact:world`, so it joins the `◎` set and the
    /// world-fact RAG.
    fn accept_research_hit(&mut self) {
        let Some(hit) = self.research_hits.get(self.research_cursor) else {
            return;
        };
        let id = hit.id;
        let Some(node) = self.hierarchy.get(id) else {
            self.status = "that passage is no longer available".into();
            return;
        };
        if node.tags.iter().any(|t| t == FACT_WORLD_TAG) {
            self.status = "already a ◎ world fact".into();
            return;
        }
        let mut updated = node.clone();
        updated.tags.push(FACT_WORLD_TAG.to_string());
        if let Err(e) = self.store.raw().update_metadata(id, updated.to_json()) {
            self.status = format!("tag write failed: {e}");
            return;
        }
        self.reload_hierarchy();
        self.status = "◎ promoted to world fact".into();
    }

    fn reject_or_stub_edit(&mut self, is_facts: bool) {
        if !is_facts {
            if let Some(id) = self.world_tree.selected() {
                if self.is_world_compiler_owned(id) {
                    self.status =
                        "Managed by realworld compile — edit world.hjson, then /compile".into();
                    return;
                }
            }
        }
        self.status = "tree editing (add/edit/rename/delete) lands in a later phase".into();
    }

    /// Send a plain-language question to the World Builder AI, grounding it in the
    /// world state, pins, and retrieved world facts (WB-P2). Slash commands are
    /// routed elsewhere (WB-P4).
    fn send_chat(&mut self, query: String) {
        let query = query.trim().to_string();
        if query.is_empty() {
            return;
        }
        if self.stream_rx.is_some() {
            self.status = "a response is still streaming…".into();
            return;
        }
        let ai = match crate::ai::AiClient::from_config(&self.cfg.llm) {
            Ok(a) => a,
            Err(e) => {
                self.push_turn(query, format!("[no LLM provider: {e}]"));
                return;
            }
        };
        let (model, _env) = match ai.resolve_provider(&self.cfg.llm, None) {
            Ok(m) => m,
            Err(e) => {
                self.push_turn(query, format!("[provider error: {e}]"));
                return;
            }
        };

        // Assemble the four context sections (a leading warnings section arrives
        // in WB-P3). All immutable borrows — build the owned `system` before we
        // mutate the chat below.
        // Prefer the compiled world state (from `/compile`) — it reports what the
        // physics produced, not just what was declared — falling back to the
        // declaration summary until the author runs `/compile`.
        let world_state = self
            .compiled_summary
            .clone()
            .or_else(|| super::prompt::world_declaration_summary(&self.layout.root))
            .unwrap_or_default();
        let pinned_world = super::prompt::pinned_nodes_text(&self.store, &self.hierarchy, &self.world_pins);
        let world_facts = self
            .facts_tree
            .root
            .map(|bid| {
                super::prompt::retrieve_world_facts(&self.store, &self.hierarchy, &self.cfg, bid, &query)
            })
            .unwrap_or_default();
        let pinned_facts = super::prompt::pinned_nodes_text(&self.store, &self.hierarchy, &self.facts_pins);
        let warnings = self.plausibility_warnings_text();
        let system = super::prompt::build_system_prompt(
            self.world_name(),
            &self.cfg.language,
            &warnings,
            &world_state,
            &pinned_world,
            &world_facts,
            &pinned_facts,
        );
        let history = self.replay_history();

        self.chat.push(WorldbuilderTurn {
            prompt: query.clone(),
            response: String::new(),
            streaming: true,
        });
        self.streaming_turn = Some(self.chat.len() - 1);
        self.chat_scroll = u16::MAX; // pin to bottom while streaming
        self.right_pane = RightPane::Chat; // surface the answer
        self.status = "asking the World Builder…".into();

        let rx = spawn_chat_stream(
            ai.client.clone(),
            model.to_string(),
            Some(system),
            history,
            query,
            WB_CATEGORY,
        );
        self.stream_rx = Some(rx);
    }

    fn push_turn(&mut self, prompt: String, response: String) {
        self.chat.push(WorldbuilderTurn { prompt, response, streaming: false });
        self.chat_scroll = u16::MAX;
    }

    /// Prior completed turns, replayed to the model for follow-up context.
    fn replay_history(&self) -> Vec<AiTurn> {
        let mut h = Vec::new();
        for t in &self.chat {
            if t.streaming {
                continue;
            }
            h.push(AiTurn::User(t.prompt.clone()));
            if !t.response.trim().is_empty() && !t.response.starts_with('[') {
                h.push(AiTurn::Assistant(t.response.clone()));
            }
        }
        h
    }

    /// Drain the streaming channel into the in-flight turn (called each frame).
    fn drain_stream(&mut self) {
        let Some(rx) = self.stream_rx.as_mut() else { return };
        let Some(idx) = self.streaming_turn else { return };
        let mut done = false;
        loop {
            match rx.try_recv() {
                Ok(StreamMsg::Token(t)) => {
                    if let Some(turn) = self.chat.get_mut(idx) {
                        turn.response.push_str(&t);
                    }
                }
                Ok(StreamMsg::Done(_)) => {
                    done = true;
                    break;
                }
                Ok(StreamMsg::Error(e)) => {
                    if let Some(turn) = self.chat.get_mut(idx) {
                        turn.response.push_str(&format!("\n[error: {e}]"));
                    }
                    done = true;
                    break;
                }
                Err(mpsc::error::TryRecvError::Empty) => break,
                Err(mpsc::error::TryRecvError::Disconnected) => {
                    done = true;
                    break;
                }
            }
        }
        if done {
            self.stream_rx = None;
            self.streaming_turn = None;
            if let Some(turn) = self.chat.get_mut(idx) {
                turn.streaming = false;
            }
            self.status = "ready".into();
        }
    }

    /// WB-P3 — recompute the deterministic plausibility score + warnings from
    /// `world.hjson`. `None` when there is no world yet. Called on load; WB-P4
    /// deltas will re-trigger it. No LLM — `run_fast` compiles + lints.
    /// The world as it currently stands: `world.hjson` on disk PLUS the pending
    /// (accepted-but-uncommitted) deltas. `None` when there is no parseable world
    /// yet. Shared by the plausibility score, `/compile`, and `/validate` so they
    /// all reason over the same in-progress world.
    pub(super) fn current_world_def(&self) -> Option<crate::world::types::WorldDefinition> {
        let mut value = std::fs::read_to_string(self.layout.root.join("world.hjson"))
            .ok()
            .and_then(|r| serde_hjson::from_str::<serde_json::Value>(&r).ok())
            .unwrap_or_else(|| serde_json::json!({}));
        for op in &self.pending_ops {
            op.apply(&mut value);
        }
        serde_json::from_value::<crate::world::types::WorldDefinition>(value).ok()
    }

    pub(super) fn refresh_plausibility(&mut self) {
        // Score the world.hjson on disk PLUS the pending (accepted-but-uncommitted)
        // deltas, so the score responds the moment a delta is accepted. Any world
        // change also invalidates the cached `/compile` summary + map grids +
        // the plakat raster (all stale once the world moves).
        self.compiled_summary = None;
        self.compiled_layers = None;
        self.map_raster = None;
        self.map_landmarks.clear();
        self.map_regions.clear();
        self.map_roads.clear();
        self.map_findings.clear();
        let def = self.current_world_def();
        self.ledger_snapshot = def.as_ref().and_then(|d| d.magic.clone());
        self.plausibility_prev = self.plausibility_score;
        match def {
            Some(def) => {
                let warnings = crate::world::plausibility::run_fast(&def);
                self.plausibility_score =
                    Some(crate::world::plausibility::compute_plausibility_score(&warnings));
                self.plausibility_warnings = warnings;
            }
            None => {
                self.plausibility_score = None;
                self.plausibility_warnings.clear();
            }
        }
    }

    /// The score change since the previous recompute, as a status chip.
    pub(super) fn plausibility_delta_chip(&self) -> String {
        match (self.plausibility_prev, self.plausibility_score) {
            (Some(p), Some(c)) if c > p => format!("{}", c - p),
            (Some(p), Some(c)) if c < p => format!("{}", p - c),
            _ => String::new(),
        }
    }

    /// The plausibility warnings formatted for the chat system prompt's WARNINGS
    /// section (top N; `run_fast` order already groups by layer).
    fn plausibility_warnings_text(&self) -> String {
        let mut s = String::new();
        for w in self.plausibility_warnings.iter().take(8) {
            s.push_str(&format!("! {}\n", w.text));
        }
        s
    }

    /// Whether a World node is owned by `realworld compile` (one of the compiled
    /// layers, or a descendant of one) — read-only in the worldbuilder.
    pub(super) fn is_world_compiler_owned(&self, id: Uuid) -> bool {
        node_is_compiler_owned(&self.hierarchy, id)
    }

    fn reload_hierarchy(&mut self) {
        if let Ok(h) = Hierarchy::load(&self.store) {
            self.hierarchy = h;
            self.facts_tree.rebuild(&self.hierarchy);
            self.world_tree.rebuild(&self.hierarchy);
        }
    }
}

impl TuiHost for WorldbuilderApp {
    fn should_quit(&self) -> bool {
        self.should_quit
    }

    fn poll_async(&mut self) {
        self.drain_stream();
    }

    fn render(&self, frame: &mut Frame) {
        super::render::render(frame, self);
    }

    fn on_key(&mut self, key: KeyEvent) {
        // Remember where non-query focus was, so Esc from the prompt returns there.
        if self.focus != Focus::QueryPrompt {
            self.prev_focus = self.focus;
        }
        self.handle_key(key);
    }

    fn on_mouse(&mut self, mouse: crossterm::event::MouseEvent) {
        self.handle_mouse(mouse);
    }
}

/// Whether a World-book node is owned by `realworld compile` — it *is* one of the
/// compiled layer chapters (Astronomy/…/Demographics), or descends from one.
/// Free function so it's testable against a `Hierarchy` without a live store.
pub(super) fn node_is_compiler_owned(h: &Hierarchy, id: Uuid) -> bool {
    let Some(node) = h.get(id) else { return false };
    let is_layer = |n: &crate::store::node::Node| {
        WORLD_COMPILER_CHAPTERS.contains(&n.title.trim().to_lowercase().as_str())
    };
    is_layer(node) || h.ancestors(node).iter().any(|a| is_layer(a))
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::store::node::Node;

    fn wnode(
        id: Uuid,
        kind: &str,
        title: &str,
        parent: Option<Uuid>,
        order: u32,
        system_tag: Option<&str>,
        tags: &[&str],
    ) -> Node {
        let mut raw = serde_json::json!({
            "id": id,
            "kind": kind,
            "title": title,
            "slug": title.to_lowercase(),
            "path": [],
            "parent_id": parent,
            "order": order,
            "file": null,
            "modified_at": "2026-01-01T00:00:00Z",
            "tags": tags,
        });
        if let Some(t) = system_tag {
            raw["system_tag"] = serde_json::json!(t);
        }
        serde_json::from_value(raw).expect("test node deserialises")
    }

    #[test]
    fn compiler_owned_covers_layer_chapters_and_their_descendants() {
        let world = Uuid::now_v7();
        let astronomy = Uuid::now_v7(); // compiled layer chapter
        let calendar = Uuid::now_v7(); // paragraph under Astronomy → owned
        let culture = Uuid::now_v7(); // author chapter
        let ethos = Uuid::now_v7(); // paragraph under Culture → author-owned
        let nodes = vec![
            wnode(world, "book", "World", None, 1, Some("world"), &[]),
            wnode(astronomy, "chapter", "Astronomy", Some(world), 1, None, &[]),
            wnode(calendar, "paragraph", "Calendar", Some(astronomy), 1, None, &[]),
            wnode(culture, "chapter", "Culture", Some(world), 2, None, &[]),
            wnode(ethos, "paragraph", "Ethos", Some(culture), 1, None, &[]),
        ];
        let h = Hierarchy::from_nodes_for_test(nodes);
        assert!(node_is_compiler_owned(&h, astronomy), "the layer chapter itself");
        assert!(node_is_compiler_owned(&h, calendar), "a descendant of a layer");
        assert!(!node_is_compiler_owned(&h, culture), "an author chapter");
        assert!(!node_is_compiler_owned(&h, ethos), "an author paragraph");
        assert!(!node_is_compiler_owned(&h, Uuid::now_v7()), "an unknown id");
    }

    #[test]
    fn fact_world_tag_marks_a_world_fact() {
        let tagged = wnode(Uuid::now_v7(), "paragraph", "Velmari harbours", None, 1, None, &[FACT_WORLD_TAG]);
        let plain = wnode(Uuid::now_v7(), "paragraph", "Bronze ports", None, 2, None, &[]);
        assert!(tagged.tags.iter().any(|t| t == FACT_WORLD_TAG));
        assert!(!plain.tags.iter().any(|t| t == FACT_WORLD_TAG));
    }
}