durthang 0.1.0

A modern, terminal-based MUD client with TLS, GMCP, automap, aliases, triggers, and a sidebar panel system
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
// Copyright (c) 2026 Raimo Geisel
// SPDX-License-Identifier: GPL-3.0-only
//
// Durthang is free software: you can redistribute it and/or modify it under
// the terms of the GNU General Public License as published by the Free
// Software Foundation, version 3.  See <https://www.gnu.org/licenses/gpl-3.0.html>.

//! Game view — the main screen shown while connected to a MUD server.
//!
//! Layout:
//!   ┌─ Server Name ──────────────────────────────┐
//!   │                                            │
//!   │  [scrollable output / scrollback buffer]   │
//!   │                                            │
//!   └─────────────── ↑42 lines ─────────────────┘
//!   ▶ input line with cursor█
//!   ● ServerName / CharName   lat 12ms   Ctrl+Q disconnect

use std::collections::VecDeque;

use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
use ratatui::{
    Frame,
    layout::{Constraint, Layout},
    style::{Color, Modifier, Style},
    text::{Line, Span, Text},
    widgets::{Block, Clear, Paragraph, Wrap},
};
use regex::Regex;
use tracing::warn;

use crate::config::{Alias, SidebarSide, Trigger as TriggerCfg};
use crate::map::{Direction, WorldMap};
use crate::ui::sidebar::{self, SidebarKeyResult, SidebarState};

// ---------------------------------------------------------------------------
// Public action type
// ---------------------------------------------------------------------------

/// Actions returned by [`handle_key`] to the main event loop.
#[derive(Debug)]
pub enum GameAction {
    /// Send a line to the server.
    SendLine(String),
    /// Gracefully disconnect and return to the selection screen.
    Disconnect,
    /// Quit the application.
    Quit,
    /// Copy text to the system clipboard via the OSC 52 escape sequence.
    CopyToClipboard(String),
    /// Persist a new (or updated) alias for the connected character.
    AddAlias { name: String, expansion: String },
    /// Delete an alias by name.
    RemoveAlias(String),
    /// Persist a new trigger for the connected character.
    AddTrigger {
        pattern: String,
        color: Option<String>,
        send: Option<String>,
    },
    /// Delete a trigger whose id starts with the given prefix.
    RemoveTrigger(String),
    /// The sidebar layout was changed and should be persisted to the character config.
    SaveSidebarLayout,
}

// ---------------------------------------------------------------------------
// Compiled trigger (runtime representation)
// ---------------------------------------------------------------------------

struct CompiledTrigger {
    id: String,
    regex: Regex,
    color: Option<Color>,
    send: Option<String>,
}

// ---------------------------------------------------------------------------
// Constants
// ---------------------------------------------------------------------------

/// Maximum number of lines kept in the scrollback buffer.
const SCROLLBACK_MAX: usize = 5_000;
/// Number of latency samples used for the rolling average.
const LATENCY_AVG_SAMPLES: usize = 8;

// ---------------------------------------------------------------------------
// GameState
// ---------------------------------------------------------------------------

/// All mutable state for the active game view.
///
/// `GameState` is created once per connection inside [`crate::main`] and kept
/// alive until the session ends.  It owns:
/// * The scrollback buffer and the current scroll position.
/// * The input line and its cursor, plus the sent-command history.
/// * A rolling latency measurement updated from [`crate::net::NetEvent::Latency`].
/// * Compiled trigger and alias lists loaded from [`crate::config`].
/// * A queue of lines to auto-send (populated by trigger `send` actions).
/// * The [`SidebarState`] (automap world, notes, focus).
pub struct GameState {
    /// Decoded + ANSI-parsed scrollback lines.
    pub lines: VecDeque<Line<'static>>,
    /// Number of logical lines we're scrolled up from the bottom (0 = live view).
    pub scroll_offset: usize,
    /// Current input line content.
    pub input: String,
    /// Byte offset of the cursor within `input`.
    pub input_cursor: usize,
    /// Sent-command history (oldest → newest).
    pub history: Vec<String>,
    /// Index into `history` while browsing (None = live edit).
    pub history_idx: Option<usize>,
    /// Snapshot of input before we started history browsing.
    history_snapshot: String,
    /// Latest latency reading in ms.
    pub latency: Option<u64>,
    /// Recent latency samples for the rolling average shown in the status bar.
    latency_samples: VecDeque<u64>,
    /// True while the connection is alive.
    pub connected: bool,
    /// Partial prompt line received without a trailing newline.
    pub prompt: Option<Line<'static>>,
    /// Height of the output inner area (updated every draw call, used for PgUp/PgDn).
    last_output_height: usize,
    // ---- Phase 6 additions ----
    /// Per-character aliases loaded at session start.
    pub aliases: Vec<Alias>,
    /// Compiled trigger patterns for this session.
    triggers: Vec<CompiledTrigger>,
    /// Lines queued for auto-send by trigger actions; drained by the main loop.
    pub auto_send_queue: Vec<String>,
    /// Whether copy mode is currently active.
    pub copy_mode: bool,
    /// Fullscreen map overlay mode.
    pub map_fullscreen: bool,
    /// Manual pan offset for fullscreen map viewport.
    pub map_pan: (i32, i32, i32),
    /// Sidebar panel state (data + focus + layout).
    pub sidebar: SidebarState,
}

impl GameState {
    /// Create a new, empty [`GameState`] ready for a fresh connection.
    pub fn new() -> Self {
        Self {
            lines: VecDeque::new(),
            scroll_offset: 0,
            input: String::new(),
            input_cursor: 0,
            history: Vec::new(),
            history_idx: None,
            history_snapshot: String::new(),
            latency: None,
            latency_samples: VecDeque::new(),
            connected: false,
            prompt: None,
            last_output_height: 20,
            aliases: Vec::new(),
            triggers: Vec::new(),
            auto_send_queue: Vec::new(),
            copy_mode: false,
            map_fullscreen: false,
            map_pan: (0, 0, 0),
            sidebar: SidebarState::default(),
        }
    }

    /// Parse an ANSI string, apply trigger highlighting, and append to the scrollback buffer.
    /// Any trigger auto-sends are pushed to `auto_send_queue`.
    pub fn push_line(&mut self, s: &str) {
        let mut line = ansi_to_line(s);
        // Apply triggers.
        for trigger in &self.triggers {
            if trigger.regex.is_match(s) {
                if let Some(color) = trigger.color {
                    line = Line::from(
                        line.spans
                            .into_iter()
                            .map(|sp| Span::styled(sp.content, sp.style.fg(color)))
                            .collect::<Vec<_>>(),
                    );
                }
                if let Some(cmd) = &trigger.send {
                    if !cmd.is_empty() {
                        self.auto_send_queue.push(cmd.clone());
                    }
                }
            }
        }
        self.lines.push_back(line);
        while self.lines.len() > SCROLLBACK_MAX {
            self.lines.pop_front();
        }
        // A full line supersedes any pending prompt.
        self.prompt = None;
    }

    /// Store a partial line (MUD prompt) shown just below the last full line.
    pub fn push_prompt(&mut self, s: &str) {
        self.prompt = Some(ansi_to_line(s));
    }

    // ------------------------------------------------------------------
    // Scrollback
    // ------------------------------------------------------------------

    pub fn scroll_up(&mut self, n: usize) {
        let max = self.lines.len().saturating_sub(1);
        self.scroll_offset = self.scroll_offset.saturating_add(n).min(max);
    }

    /// Scroll the output view down by `n` logical lines toward the live view.
    pub fn scroll_down(&mut self, n: usize) {
        self.scroll_offset = self.scroll_offset.saturating_sub(n);
    }

    /// Jump the output view to the live bottom.
    pub fn scroll_to_bottom(&mut self) {
        self.scroll_offset = 0;
    }

    /// Jump the output view to the oldest retained line.
    pub fn scroll_to_top(&mut self) {
        self.scroll_offset = self.lines.len().saturating_sub(1);
    }

    // ------------------------------------------------------------------
    // History
    // ------------------------------------------------------------------

    fn history_prev(&mut self) {
        if self.history.is_empty() {
            return;
        }
        let idx = match self.history_idx {
            None => {
                // Save live input before we start browsing.
                self.history_snapshot = self.input.clone();
                self.history.len() - 1
            }
            Some(0) => 0,
            Some(i) => i - 1,
        };
        self.history_idx = Some(idx);
        self.input = self.history[idx].clone();
        self.input_cursor = self.input.len();
    }

    fn history_next(&mut self) {
        match self.history_idx {
            None => {}
            Some(i) if i + 1 >= self.history.len() => {
                self.history_idx = None;
                self.input = self.history_snapshot.clone();
                self.input_cursor = self.input.len();
            }
            Some(i) => {
                let idx = i + 1;
                self.history_idx = Some(idx);
                self.input = self.history[idx].clone();
                self.input_cursor = self.input.len();
            }
        }
    }

    // ------------------------------------------------------------------
    // Input confirm
    // ------------------------------------------------------------------

    /// Finalise the input, push to history, expand aliases, and return the
    /// action to perform.
    fn confirm_input(&mut self) -> Option<GameAction> {
        let raw = self.input.trim_end().to_string();
        self.input.clear();
        self.input_cursor = 0;
        self.history_idx = None;
        self.history_snapshot.clear();

        if raw.is_empty() {
            return None;
        }

        // Always record raw input in history.
        if self.history.last().map(|l| l != &raw).unwrap_or(true) {
            self.history.push(raw.clone());
        }

        // Meta-commands start with '/'.
        if raw.starts_with('/') {
            return self.handle_meta_command(&raw);
        }

        // Expand aliases.
        let line = self.expand_alias(&raw);

        // Echo into scrollback.
        let echo = Line::from(vec![
            Span::styled(
                "",
                Style::default()
                    .fg(Color::Green)
                    .add_modifier(Modifier::BOLD),
            ),
            Span::raw(line.clone()),
        ]);
        self.lines.push_back(echo);
        while self.lines.len() > SCROLLBACK_MAX {
            self.lines.pop_front();
        }

        Some(GameAction::SendLine(line))
    }

    // ------------------------------------------------------------------
    // Reset on new connection
    // ------------------------------------------------------------------

    /// Reset game state at the start of a new connection.
    ///
    /// Marks the session as connected, clears latency samples and the
    /// fullscreen map state, and scrolls to the live bottom.
    pub fn on_connect(&mut self) {
        self.connected = true;
        self.latency = None;
        self.latency_samples.clear();
        self.map_fullscreen = false;
        self.map_pan = (0, 0, 0);
        self.scroll_to_bottom();
        self.prompt = None;
    }

    /// Update state when the connection is lost.
    ///
    /// Marks the session as disconnected and clears latency samples and the
    /// fullscreen map toggle.  The scrollback buffer is retained so the user
    /// can review the last output before reconnecting.
    pub fn on_disconnect(&mut self) {
        self.connected = false;
        self.latency = None;
        self.latency_samples.clear();
        self.map_fullscreen = false;
        self.map_pan = (0, 0, 0);
    }

    /// Record a new latency sample and update the rolling average window.
    ///
    /// The latest raw value is stored in `latency`; the window used for the
    /// rolling average is capped at [`LATENCY_AVG_SAMPLES`] entries.
    pub fn record_latency(&mut self, ms: u64) {
        self.latency = Some(ms);
        self.latency_samples.push_back(ms);
        while self.latency_samples.len() > LATENCY_AVG_SAMPLES {
            self.latency_samples.pop_front();
        }
    }

    /// Compute the rolling average of the last [`LATENCY_AVG_SAMPLES`] latency
    /// measurements.  Returns `None` if no samples have been recorded yet.
    fn latency_avg(&self) -> Option<u64> {
        if self.latency_samples.is_empty() {
            return None;
        }
        let sum: u64 = self.latency_samples.iter().copied().sum();
        let count = u64::try_from(self.latency_samples.len()).unwrap_or(1);
        Some(sum / count)
    }

    // ------------------------------------------------------------------
    // Aliases & triggers
    // ------------------------------------------------------------------

    /// Update the alias list for the current session.
    ///
    /// The entire list is replaced atomically so that add/remove operations
    /// performed via [`GameAction`] are always consistent.
    pub fn set_aliases(&mut self, aliases: Vec<Alias>) {
        self.aliases = aliases;
    }

    /// Compile and install trigger rules for this session.
    /// Invalid regexes are logged as warnings and skipped.
    pub fn set_triggers(&mut self, triggers: Vec<TriggerCfg>) {
        self.triggers = triggers
            .into_iter()
            .filter_map(|t| match Regex::new(&t.pattern) {
                Ok(regex) => Some(CompiledTrigger {
                    id: t.id,
                    regex,
                    color: t.color.as_deref().and_then(parse_color_name),
                    send: t.send,
                }),
                Err(e) => {
                    warn!("Invalid trigger regex {:?}: {e}", t.pattern);
                    None
                }
            })
            .collect();
    }

    /// Expand an alias in the given `line`.
    ///
    /// Resolution order:
    /// 1. **Exact-line match**: if any alias `name` equals the entire `line`,
    ///    return its expansion verbatim.
    /// 2. **First-word match**: if any alias `name` equals the first whitespace-
    ///    delimited token, replace that token with the expansion and append the
    ///    remainder of the line.
    /// 3. **No match**: return `line` unchanged.
    pub(crate) fn expand_alias(&self, line: &str) -> String {
        // Exact-line match first, then first-word match (with tail appended).
        if let Some(a) = self.aliases.iter().find(|a| a.name == line) {
            return a.expansion.clone();
        }
        let first = line.split_whitespace().next().unwrap_or("");
        if let Some(a) = self.aliases.iter().find(|a| a.name == first) {
            let tail = &line[first.len()..];
            return format!("{}{tail}", a.expansion);
        }
        line.to_string()
    }

    // ------------------------------------------------------------------
    // System messages
    // ------------------------------------------------------------------

    /// Push a client-side informational message into the scrollback buffer.
    pub fn push_system(&mut self, msg: &str) {
        let line = Line::from(vec![
            Span::styled("\u{00bb} ", Style::default().fg(Color::Cyan)),
            Span::styled(msg.to_string(), Style::default().fg(Color::Cyan)),
        ]);
        self.lines.push_back(line);
        while self.lines.len() > SCROLLBACK_MAX {
            self.lines.pop_front();
        }
    }

    // ------------------------------------------------------------------
    // Meta-command parsing  (/alias, /trigger, /disconnect, /quit, …)
    // ------------------------------------------------------------------

    fn handle_meta_command(&mut self, input: &str) -> Option<GameAction> {
        let stripped = &input[1..];
        let (cmd, args) = stripped
            .split_once(' ')
            .map(|(c, a)| (c, a.trim()))
            .unwrap_or((stripped, ""));

        match cmd.to_lowercase().as_str() {
            "disconnect" | "disc" => Some(GameAction::Disconnect),
            "quit" | "exit" => Some(GameAction::Quit),

            // ------ Sidebar visibility (/sidebar) ----------------------------
            "sidebar" | "sb" => match args {
                "right" | "r" => {
                    self.sidebar.toggle_right();
                    let st = if self.sidebar.layout.right_visible {
                        "shown"
                    } else {
                        "hidden"
                    };
                    self.push_system(&format!("Right sidebar {st}."));
                    Some(GameAction::SaveSidebarLayout)
                }
                _ => {
                    let r = if self.sidebar.layout.right_visible {
                        "shown"
                    } else {
                        "hidden"
                    };
                    self.push_system(&format!("Right sidebar: {r}  /sidebar right to toggle"));
                    None
                }
            },

            // ------ Automap controls (/map) ---------------------------------
            "map" => {
                let (sub, rest) = args
                    .split_once(' ')
                    .map(|(s, r)| (s, r.trim()))
                    .unwrap_or((args, ""));

                match sub {
                    "" | "show" => {
                        if let Some(room) = self.sidebar.automap.current_room() {
                            self.push_system(&format!(
                                "Map current: id={}  name='{}'  pos=({}, {}, {})  exits={} ",
                                room.id,
                                room.name,
                                room.x,
                                room.y,
                                room.z,
                                room.exits.len()
                            ));
                        } else {
                            self.push_system("Map: no current room yet.");
                        }
                        None
                    }
                    "setpos" => {
                        let parts: Vec<&str> = rest.split_whitespace().collect();
                        if parts.len() < 3 {
                            self.push_system("Usage: /map setpos <room_id|current> <x> <y> [z]");
                            return None;
                        }
                        let room_id = if parts[0] == "current" {
                            self.sidebar
                                .map_current_room_id()
                                .unwrap_or_else(|| "current".to_string())
                        } else {
                            parts[0].to_string()
                        };
                        let x = parts[1].parse::<i32>();
                        let y = parts[2].parse::<i32>();
                        let z = if parts.len() >= 4 {
                            parts[3].parse::<i32>()
                        } else {
                            Ok(0)
                        };
                        match (x, y, z) {
                            (Ok(x), Ok(y), Ok(z)) => {
                                self.sidebar.map_set_position(&room_id, x, y, z);
                                self.push_system(&format!("Map: set {room_id} -> ({x}, {y}, {z})"));
                            }
                            _ => {
                                self.push_system("Usage: /map setpos <room_id|current> <x> <y> [z]")
                            }
                        }
                        None
                    }
                    "full" | "fullscreen" | "fs" => {
                        self.map_fullscreen = !self.map_fullscreen;
                        if self.map_fullscreen {
                            self.map_pan = (0, 0, 0);
                            self.push_system("Map fullscreen enabled.  Arrows pan, u/d z-level, c center, Esc/F6 close.");
                        } else {
                            self.push_system("Map fullscreen disabled.");
                        }
                        None
                    }
                    "link" => {
                        let parts: Vec<&str> = rest.split_whitespace().collect();
                        if parts.len() != 3 {
                            self.push_system("Usage: /map link <from_id|current> <dir> <to_id>");
                            return None;
                        }
                        let from_id = if parts[0] == "current" {
                            self.sidebar
                                .map_current_room_id()
                                .unwrap_or_else(|| "current".to_string())
                        } else {
                            parts[0].to_string()
                        };
                        let Some(dir) = Direction::parse(parts[1]) else {
                            self.push_system("Direction must be one of n/s/e/w/u/d.");
                            return None;
                        };
                        let to_id = parts[2].to_string();
                        self.sidebar.map_link_rooms(&from_id, dir, &to_id);
                        self.push_system(&format!(
                            "Map: linked {from_id} --{}--> {to_id}",
                            dir.as_str()
                        ));
                        None
                    }
                    _ => {
                        self.push_system("Usage: /map [show|fullscreen|setpos <room_id|current> <x> <y> [z]|link <from_id|current> <dir> <to_id>]");
                        None
                    }
                }
            }

            "alias" | "al" => {
                if args.is_empty() {
                    if self.aliases.is_empty() {
                        self.push_system("No aliases defined.  Usage: /alias <name> <expansion>");
                    } else {
                        self.push_system("Aliases:");
                        let msgs: Vec<String> = self
                            .aliases
                            .iter()
                            .map(|a| format!("  {} \u{2192} {}", a.name, a.expansion))
                            .collect();
                        for m in msgs {
                            self.push_system(&m);
                        }
                    }
                    None
                } else {
                    match args.split_once(' ') {
                        None => {
                            match self.aliases.iter().find(|a| a.name == args) {
                                Some(a) => self
                                    .push_system(&format!("  {} \u{2192} {}", a.name, a.expansion)),
                                None => self.push_system(&format!("No alias named '{args}'.")),
                            }
                            None
                        }
                        Some((name, expansion)) => Some(GameAction::AddAlias {
                            name: name.to_string(),
                            expansion: expansion.to_string(),
                        }),
                    }
                }
            }

            "unalias" | "unal" => {
                if args.is_empty() {
                    self.push_system("Usage: /unalias <name>");
                    None
                } else {
                    Some(GameAction::RemoveAlias(args.to_string()))
                }
            }

            "trigger" | "trig" => {
                let (sub, rest) = args
                    .split_once(' ')
                    .map(|(s, r)| (s, r.trim()))
                    .unwrap_or((args, ""));

                match sub {
                    "" | "list" => {
                        if self.triggers.is_empty() {
                            self.push_system("No triggers defined.  Usage: /trigger add <pattern> [color=NAME] [send=CMD]");
                        } else {
                            self.push_system("Triggers:");
                            let items: Vec<String> = self
                                .triggers
                                .iter()
                                .map(|t| {
                                    let id_short = &t.id[..8.min(t.id.len())];
                                    let send = t.send.as_deref().unwrap_or("-");
                                    format!("  [{id_short}]  /{}/ send={send}", t.regex.as_str())
                                })
                                .collect();
                            for s in items {
                                self.push_system(&s);
                            }
                        }
                        None
                    }
                    "add" => {
                        if rest.is_empty() {
                            self.push_system(
                                "Usage: /trigger add <pattern> [color=NAME] [send=CMD]",
                            );
                            None
                        } else {
                            let (pattern, color, send) = parse_trigger_args(rest);
                            Some(GameAction::AddTrigger {
                                pattern,
                                color,
                                send,
                            })
                        }
                    }
                    "del" | "rm" | "remove" | "delete" => {
                        if rest.is_empty() {
                            self.push_system("Usage: /trigger del <id>");
                            None
                        } else {
                            Some(GameAction::RemoveTrigger(rest.to_string()))
                        }
                    }
                    _ => {
                        self.push_system("Usage: /trigger list | /trigger add <pattern> [color=NAME] [send=CMD] | /trigger del <id>");
                        None
                    }
                }
            }

            _ => {
                self.push_system(&format!("Unknown command: {input}"));
                self.push_system(
                    "  Available: /alias, /unalias, /trigger, /sidebar, /map, /disconnect, /quit",
                );
                None
            }
        }
    }
} // end impl GameState

// ---------------------------------------------------------------------------
// ANSI / VT100 parser
// ---------------------------------------------------------------------------

/// Convert a string that may contain ANSI SGR escape codes into a ratatui
/// `Line<'static>` with per-span styles applied.
fn ansi_to_line(input: &str) -> Line<'static> {
    let mut spans: Vec<Span<'static>> = Vec::new();
    let mut style = Style::default();
    let bytes = input.as_bytes();
    let mut text_start = 0;
    let mut i = 0;

    while i < input.len() {
        // Look for ESC [  (CSI — Control Sequence Introducer)
        if bytes[i] == b'\x1b' && i + 1 < input.len() && bytes[i + 1] == b'[' {
            // Flush accumulated plain text.
            if text_start < i {
                spans.push(Span::styled(input[text_start..i].to_string(), style));
            }

            // Find the end of the escape sequence.
            // Per ANSI X3.64 the final byte is in the range 0x40–0x7E ('@'–'~').
            // Parameter/intermediate bytes (0x20–0x3F) precede it.  We also
            // stop if we hit another ESC (0x1B) — that means this CSI was
            // truncated and a new escape sequence starts.
            let seq_start = i + 2;
            let seq_end = bytes[seq_start..]
                .iter()
                .position(|&b| (0x40..=0x7E).contains(&b) || b == 0x1b)
                .map(|j| seq_start + j)
                .unwrap_or(input.len());

            if seq_end < input.len() && bytes[seq_end] != 0x1b {
                let terminator = bytes[seq_end];
                let params = &input[seq_start..seq_end];
                if terminator == b'm' {
                    style = apply_sgr(style, params);
                }
                // All other escape sequences (cursor movement, mode switches, etc.)
                // are silently skipped.
                i = seq_end + 1;
            } else {
                // Unterminated CSI (hit another ESC or end of input) — skip
                // the broken params; the next iteration picks up the new ESC.
                i = seq_end;
            }
            text_start = i;
        } else if bytes[i] == b'\x1b' {
            // Non-CSI ESC sequence (no '[' after ESC).
            // These are always terminal control sequences, never printable text.
            // Skip the ESC plus its payload:
            //   - ESC <single byte>          (e.g. ESC= ESC> ESC7 ESC8)
            //   - ESC ( X  ESC ) X  etc.    (G0/G1 charset designation, 3 bytes total)
            if text_start < i {
                spans.push(Span::styled(input[text_start..i].to_string(), style));
            }
            i += 1; // skip ESC
            if i < bytes.len() {
                let next = bytes[i];
                i += 1; // skip the byte immediately after ESC
                // Charset-designation sequences have one additional designator byte.
                // Don't consume the next byte if it is ESC — that starts a
                // new escape sequence and must not be swallowed as a charset code.
                if matches!(next, b'(' | b')' | b'*' | b'+') && i < bytes.len() && bytes[i] != 0x1b
                {
                    i += 1; // skip the charset code (e.g. 'B', '0', 'U', …)
                }
            }
            text_start = i;
        } else {
            // Advance by one UTF-8 character.
            i += input[i..].chars().next().map(|c| c.len_utf8()).unwrap_or(1);
        }
    }

    // Flush remaining text.
    if text_start < input.len() {
        spans.push(Span::styled(input[text_start..].to_string(), style));
    }

    if spans.is_empty() {
        spans.push(Span::raw(String::new()));
    }

    Line::from(spans)
}

/// Apply one or more SGR parameter codes to a style and return the result.
fn apply_sgr(mut style: Style, params: &str) -> Style {
    if params.is_empty() {
        return Style::default();
    }

    let codes: Vec<u32> = params
        .split(';')
        .filter_map(|s| s.trim().parse().ok())
        .collect();

    let mut i = 0;
    while i < codes.len() {
        match codes[i] {
            0 => style = Style::default(),
            1 => style = style.add_modifier(Modifier::BOLD),
            2 => style = style.add_modifier(Modifier::DIM),
            3 => style = style.add_modifier(Modifier::ITALIC),
            4 => style = style.add_modifier(Modifier::UNDERLINED),
            5 | 6 => style = style.add_modifier(Modifier::SLOW_BLINK),
            7 => style = style.add_modifier(Modifier::REVERSED),
            9 => style = style.add_modifier(Modifier::CROSSED_OUT),
            22 => {
                style = style
                    .remove_modifier(Modifier::BOLD)
                    .remove_modifier(Modifier::DIM);
            }
            23 => style = style.remove_modifier(Modifier::ITALIC),
            24 => style = style.remove_modifier(Modifier::UNDERLINED),
            25 => style = style.remove_modifier(Modifier::SLOW_BLINK),
            27 => style = style.remove_modifier(Modifier::REVERSED),
            29 => style = style.remove_modifier(Modifier::CROSSED_OUT),

            // Standard foreground colors (ANSI 30-37 → ratatui equivalents)
            30 => style = style.fg(Color::Black),
            31 => style = style.fg(Color::Red),
            32 => style = style.fg(Color::Green),
            33 => style = style.fg(Color::Yellow),
            34 => style = style.fg(Color::Blue),
            35 => style = style.fg(Color::Magenta),
            36 => style = style.fg(Color::Cyan),
            37 => style = style.fg(Color::Gray), // "white" in standard palette
            38 => {
                if i + 2 < codes.len() && codes[i + 1] == 5 {
                    // 256-color fg: ESC[38;5;Nm
                    style = style.fg(Color::Indexed(codes[i + 2] as u8));
                    i += 2;
                } else if i + 4 < codes.len() && codes[i + 1] == 2 {
                    // True-color fg: ESC[38;2;R;G;Bm
                    style = style.fg(Color::Rgb(
                        codes[i + 2] as u8,
                        codes[i + 3] as u8,
                        codes[i + 4] as u8,
                    ));
                    i += 4;
                }
            }
            39 => style = style.fg(Color::Reset),

            // Standard background colors
            40 => style = style.bg(Color::Black),
            41 => style = style.bg(Color::Red),
            42 => style = style.bg(Color::Green),
            43 => style = style.bg(Color::Yellow),
            44 => style = style.bg(Color::Blue),
            45 => style = style.bg(Color::Magenta),
            46 => style = style.bg(Color::Cyan),
            47 => style = style.bg(Color::Gray),
            48 => {
                if i + 2 < codes.len() && codes[i + 1] == 5 {
                    style = style.bg(Color::Indexed(codes[i + 2] as u8));
                    i += 2;
                } else if i + 4 < codes.len() && codes[i + 1] == 2 {
                    style = style.bg(Color::Rgb(
                        codes[i + 2] as u8,
                        codes[i + 3] as u8,
                        codes[i + 4] as u8,
                    ));
                    i += 4;
                }
            }
            49 => style = style.bg(Color::Reset),

            // Bright / high-intensity foreground (ANSI 90-97)
            90 => style = style.fg(Color::DarkGray),
            91 => style = style.fg(Color::LightRed),
            92 => style = style.fg(Color::LightGreen),
            93 => style = style.fg(Color::LightYellow),
            94 => style = style.fg(Color::LightBlue),
            95 => style = style.fg(Color::LightMagenta),
            96 => style = style.fg(Color::LightCyan),
            97 => style = style.fg(Color::White), // bright white

            // Bright background (ANSI 100-107)
            100 => style = style.bg(Color::DarkGray),
            101 => style = style.bg(Color::LightRed),
            102 => style = style.bg(Color::LightGreen),
            103 => style = style.bg(Color::LightYellow),
            104 => style = style.bg(Color::LightBlue),
            105 => style = style.bg(Color::LightMagenta),
            106 => style = style.bg(Color::LightCyan),
            107 => style = style.bg(Color::White),

            _ => {} // Unknown SGR codes are silently ignored.
        }
        i += 1;
    }

    style
}

// ---------------------------------------------------------------------------
// Key handling
// ---------------------------------------------------------------------------

/// Handle a key event in game mode.
///
/// Returns `Some(GameAction)` when an action needs to be taken by the caller.
/// Returns `None` for all other keystrokes (pure editing operations).
pub fn handle_key(state: &mut GameState, key: KeyEvent) -> Option<GameAction> {
    let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);

    if key.code == KeyCode::F(6) {
        state.map_fullscreen = !state.map_fullscreen;
        if state.map_fullscreen {
            state.map_pan = (0, 0, 0);
        }
        return None;
    }

    if state.map_fullscreen {
        return handle_map_fullscreen_mode(state, key);
    }

    // F1 — return keyboard focus to the game input.
    if key.code == KeyCode::F(1) {
        state.sidebar.focused_panel = None;
        return None;
    }

    // F-key sidebar controls.
    if let KeyCode::F(n) = key.code {
        match n {
            3 => {
                if state.sidebar.toggle_right() {
                    return Some(GameAction::SaveSidebarLayout);
                }
                return None;
            }
            4 => {
                state.sidebar.focus_next_panel();
                return None;
            }
            _ => return None,
        }
    }

    // Ctrl+Q — disconnect and return to the selection screen.
    if ctrl && key.code == KeyCode::Char('q') {
        return Some(GameAction::Disconnect);
    }

    // In copy mode most keys have different bindings.
    if state.copy_mode {
        return handle_copy_mode(state, key);
    }

    // When a sidebar panel is focused, route input there.
    if state.sidebar.focused_panel.is_some() {
        return match sidebar::handle_sidebar_key(&mut state.sidebar, key) {
            SidebarKeyResult::FocusGame => {
                state.sidebar.focused_panel = None;
                None
            }
            SidebarKeyResult::SaveLayout => Some(GameAction::SaveSidebarLayout),
            SidebarKeyResult::Consumed | SidebarKeyResult::Unhandled => None,
        };
    }

    match key.code {
        // ---- Confirm / send ----
        KeyCode::Enter => {
            state.scroll_to_bottom();
            state.confirm_input()
        }

        // ---- Scrollback ----
        KeyCode::PageUp => {
            let n = state.last_output_height.max(1);
            state.scroll_up(n);
            None
        }
        KeyCode::PageDown => {
            let n = state.last_output_height.max(1);
            state.scroll_down(n);
            None
        }
        KeyCode::Home if ctrl => {
            state.scroll_to_top();
            None
        }
        KeyCode::End if ctrl => {
            state.scroll_to_bottom();
            None
        }

        // ---- History navigation (Up/Down without Ctrl) ----
        KeyCode::Up if !ctrl => {
            state.history_prev();
            None
        }
        KeyCode::Down if !ctrl => {
            state.history_next();
            None
        }

        // ---- Scrollback with Ctrl+Up / Ctrl+Down ----
        KeyCode::Up if ctrl => {
            state.scroll_up(1);
            None
        }
        KeyCode::Down if ctrl => {
            state.scroll_down(1);
            None
        }

        // ---- Enter copy mode ----
        KeyCode::Char('y') if ctrl => {
            state.copy_mode = true;
            None
        }

        // ---- Cursor movement ----
        KeyCode::Home if !ctrl => {
            state.input_cursor = 0;
            None
        }
        KeyCode::End if !ctrl => {
            state.input_cursor = state.input.len();
            None
        }
        KeyCode::Left => {
            if state.input_cursor > 0 {
                let prev = state.input[..state.input_cursor]
                    .char_indices()
                    .next_back()
                    .map(|(i, _)| i)
                    .unwrap_or(0);
                state.input_cursor = prev;
            }
            None
        }
        KeyCode::Right => {
            if state.input_cursor < state.input.len() {
                let ch = state.input[state.input_cursor..].chars().next().unwrap();
                state.input_cursor += ch.len_utf8();
            }
            None
        }

        // ---- Editing ----
        KeyCode::Backspace => {
            if state.input_cursor > 0 {
                let prev = state.input[..state.input_cursor]
                    .char_indices()
                    .next_back()
                    .map(|(i, _)| i)
                    .unwrap_or(0);
                state.input.remove(prev);
                state.input_cursor = prev;
            }
            None
        }
        KeyCode::Delete => {
            if state.input_cursor < state.input.len() {
                state.input.remove(state.input_cursor);
            }
            None
        }

        // ---- Readline-style shortcuts ----
        KeyCode::Char('u') if ctrl => {
            state.input.drain(..state.input_cursor);
            state.input_cursor = 0;
            None
        }
        KeyCode::Char('k') if ctrl => {
            state.input.truncate(state.input_cursor);
            None
        }
        KeyCode::Char('a') if ctrl => {
            state.input_cursor = 0;
            None
        }
        KeyCode::Char('e') if ctrl => {
            state.input_cursor = state.input.len();
            None
        }
        KeyCode::Char('w') if ctrl => {
            // Delete the word before the cursor (like readline Ctrl+W).
            let before = &state.input[..state.input_cursor];
            let trim_end = before.trim_end_matches(|c: char| !c.is_whitespace()).len();
            let new_end = before[..trim_end.min(before.len())]
                .trim_end_matches(|c: char| c.is_whitespace())
                .len();
            state.input.drain(new_end..state.input_cursor);
            state.input_cursor = new_end;
            None
        }

        // ---- Regular character input ----
        KeyCode::Char(c) if !ctrl => {
            state.input.insert(state.input_cursor, c);
            state.input_cursor += c.len_utf8();
            state.history_idx = None;
            None
        }

        _ => None,
    }
}

/// Handle a key event while copy mode is active.
fn handle_copy_mode(state: &mut GameState, key: KeyEvent) -> Option<GameAction> {
    let ctrl = key.modifiers.contains(KeyModifiers::CONTROL);
    match key.code {
        KeyCode::Esc | KeyCode::Char('q') => {
            state.copy_mode = false;
            None
        }
        KeyCode::Up => {
            state.scroll_up(1);
            None
        }
        KeyCode::Down => {
            state.scroll_down(1);
            None
        }
        KeyCode::PageUp => {
            let n = state.last_output_height.max(1);
            state.scroll_up(n);
            None
        }
        KeyCode::PageDown => {
            let n = state.last_output_height.max(1);
            state.scroll_down(n);
            None
        }
        KeyCode::Home if ctrl => {
            state.scroll_to_top();
            None
        }
        KeyCode::End if ctrl => {
            state.scroll_to_bottom();
            None
        }
        KeyCode::Char('y') => {
            // Yank (copy) the bottom-most visible line.
            let buf_len = state.lines.len();
            let scroll = state.scroll_offset.min(buf_len.saturating_sub(1));
            let idx = buf_len.saturating_sub(1 + scroll);
            let text: String = state
                .lines
                .get(idx)
                .map(|l| l.spans.iter().map(|s| s.content.as_ref()).collect())
                .unwrap_or_default();
            state.copy_mode = false;
            if text.is_empty() {
                None
            } else {
                Some(GameAction::CopyToClipboard(text))
            }
        }
        _ => None,
    }
}

fn handle_map_fullscreen_mode(state: &mut GameState, key: KeyEvent) -> Option<GameAction> {
    match key.code {
        KeyCode::Esc | KeyCode::F(6) => {
            state.map_fullscreen = false;
            None
        }
        KeyCode::Left => {
            state.map_pan.0 -= 1;
            None
        }
        KeyCode::Right => {
            state.map_pan.0 += 1;
            None
        }
        KeyCode::Up => {
            state.map_pan.1 -= 1;
            None
        }
        KeyCode::Down => {
            state.map_pan.1 += 1;
            None
        }
        KeyCode::PageUp | KeyCode::Char('u') => {
            state.map_pan.2 += 1;
            None
        }
        KeyCode::PageDown | KeyCode::Char('d') => {
            state.map_pan.2 -= 1;
            None
        }
        KeyCode::Char('c') => {
            state.map_pan = (0, 0, 0);
            None
        }
        _ => None,
    }
}

// ---------------------------------------------------------------------------
// Trigger / color helpers
// ---------------------------------------------------------------------------

fn parse_color_name(name: &str) -> Option<Color> {
    match name.to_lowercase().as_str() {
        "black" => Some(Color::Black),
        "red" => Some(Color::Red),
        "green" => Some(Color::Green),
        "yellow" => Some(Color::Yellow),
        "blue" => Some(Color::Blue),
        "magenta" => Some(Color::Magenta),
        "cyan" => Some(Color::Cyan),
        "gray" | "grey" => Some(Color::Gray),
        "dark_gray" | "darkgray" => Some(Color::DarkGray),
        "light_red" => Some(Color::LightRed),
        "light_green" => Some(Color::LightGreen),
        "light_yellow" => Some(Color::LightYellow),
        "light_blue" => Some(Color::LightBlue),
        "light_magenta" => Some(Color::LightMagenta),
        "light_cyan" => Some(Color::LightCyan),
        "white" => Some(Color::White),
        _ => None,
    }
}

/// Parse `color=NAME` and `send=CMD` key=value suffixes from a trigger add string.
/// Returns (pattern, color, send).
fn parse_trigger_args(s: &str) -> (String, Option<String>, Option<String>) {
    let mut rest = s.to_string();
    let mut color = None;
    let mut send = None;

    // `send=` is parsed first because it may contain spaces (consumed to end).
    if let Some(idx) = find_kv(&rest, "send") {
        send = Some(rest[idx + "send=".len()..].trim().to_string());
        rest = rest[..idx].trim().to_string();
    }
    if let Some(idx) = find_kv(&rest, "color") {
        let val: &str = rest[idx + "color=".len()..]
            .split_whitespace()
            .next()
            .unwrap_or("");
        if !val.is_empty() {
            color = Some(val.to_string());
        }
        rest = rest[..idx].trim().to_string();
    }

    (rest, color, send)
}

/// Find the byte position of `key=` in `s`, only at a word boundary.
fn find_kv(s: &str, key: &str) -> Option<usize> {
    let needle = format!("{key}=");
    if s.starts_with(&needle) {
        return Some(0);
    }
    s.rfind(&format!(" {needle}")).map(|p| p + 1)
}

fn build_map_lines(
    map: &WorldMap,
    width: u16,
    height: u16,
    pan: (i32, i32, i32),
) -> Vec<Line<'static>> {
    if width == 0 || height == 0 {
        return Vec::new();
    }
    let Some(cur) = map.current_room() else {
        return vec![Line::from("No map data yet")];
    };

    let half_w = (width.saturating_sub(1) / 2) as i32;
    let half_h = (height.saturating_sub(1) / 2) as i32;
    let z = cur.z + pan.2;

    let mut rows: Vec<Vec<char>> = vec![vec![' '; width as usize]; height as usize];
    for sy in 0..height as i32 {
        for sx in 0..width as i32 {
            let wx = cur.x + pan.0 + (sx - half_w);
            let wy = cur.y + pan.1 + (sy - half_h);
            if let Some(room) = map.room_at(wx, wy, z) {
                rows[sy as usize][sx as usize] = if room.id == cur.id && z == cur.z {
                    '@'
                } else {
                    '.'
                };
            }
        }
    }

    rows.into_iter()
        .map(|r| Line::raw(r.into_iter().collect::<String>()))
        .collect()
}

// ---------------------------------------------------------------------------
// Drawing
// ---------------------------------------------------------------------------

pub fn draw(frame: &mut Frame, state: &mut GameState, server_name: &str, char_name: &str) {
    let area = frame.area();

    // Top-level vertical split: [content area | status bar (1 row)]
    let [content_area, status_area] =
        Layout::vertical([Constraint::Fill(1), Constraint::Length(1)]).areas(area);

    // Horizontal split: game column | optional right sidebar.
    let has_right = state.sidebar.has_side_panels(&SidebarSide::Right);
    let show_right = has_right && state.sidebar.layout.right_visible;

    const MIN_GAME_W: u16 = 20;
    let right_w = if show_right {
        state
            .sidebar
            .layout
            .right_width
            .min(content_area.width.saturating_sub(MIN_GAME_W))
    } else {
        0
    };

    let (game_col, right_area_opt) = if right_w > 0 {
        let c = Layout::horizontal([Constraint::Fill(1), Constraint::Length(right_w)])
            .split(content_area);
        (c[0], Some(c[1]))
    } else {
        (content_area, None)
    };

    // Game column: [output area | input block (3 rows: border + content + border)]
    let [output_area, input_area] =
        Layout::vertical([Constraint::Fill(1), Constraint::Length(3)]).areas(game_col);

    // The bordered output block and input block each consume 2 rows for their borders.
    let output_inner_h = output_area.height.saturating_sub(2) as usize;
    state.last_output_height = output_inner_h.max(1);

    // --- Compute visible line window ---
    let buf_len = state.lines.len();
    // Clamp scroll so we never go past the top.
    let scroll = state.scroll_offset.min(buf_len.saturating_sub(1));

    // Suppress the live prompt while in copy mode so the highlighted line is always scrollback.
    let has_prompt = state.prompt.is_some() && scroll == 0 && !state.copy_mode;
    // Reserve one row for the prompt when it's visible.
    let rows_for_lines = output_inner_h.saturating_sub(if has_prompt { 1 } else { 0 });

    let line_end = buf_len.saturating_sub(scroll);
    let line_start = line_end.saturating_sub(rows_for_lines);

    let mut text_lines: Vec<Line<'static>> = state
        .lines
        .iter()
        .skip(line_start)
        .take(line_end - line_start)
        .cloned()
        .collect();

    if has_prompt {
        if let Some(p) = &state.prompt {
            text_lines.push(p.clone());
        }
    }

    // Scroll indicator shown in the bottom border of the output block.
    let scroll_hint = if scroll > 0 {
        format!("{scroll}/{buf_len} lines — PgDn / Ctrl+End to return ")
    } else {
        String::new()
    };

    // Highlight the bottom-most visible line in copy mode.
    if state.copy_mode && !text_lines.is_empty() {
        let last = text_lines.len() - 1;
        if let Some(line) = text_lines.get_mut(last) {
            *line = Line::from(
                line.spans
                    .iter()
                    .map(|sp| {
                        Span::styled(
                            sp.content.clone(),
                            sp.style
                                .bg(Color::Blue)
                                .fg(Color::White)
                                .add_modifier(Modifier::BOLD),
                        )
                    })
                    .collect::<Vec<_>>(),
            );
        }
    }

    let output_block = if state.copy_mode {
        Block::bordered()
            .title(format!(" {} ", server_name))
            .title_bottom(Line::from(Span::styled(
                " COPY MODE  \u{2191}\u{2193} scroll  y yank  Esc exit ",
                Style::default().fg(Color::Black).bg(Color::Yellow),
            )))
            .border_style(Style::default().fg(Color::Yellow))
    } else {
        Block::bordered()
            .title(format!(" {} ", server_name))
            .title_bottom(scroll_hint.as_str())
    };

    let output_para = Paragraph::new(Text::from(text_lines))
        .block(output_block)
        .wrap(Wrap { trim: false });
    frame.render_widget(output_para, output_area);

    // --- Input line (bordered) ---
    let before_cursor = &state.input[..state.input_cursor];
    let (cursor_ch, after_cursor) = if state.input_cursor < state.input.len() {
        let c = state.input[state.input_cursor..].chars().next().unwrap();
        let end = state.input_cursor + c.len_utf8();
        (c.to_string(), state.input[end..].to_string())
    } else {
        (" ".to_string(), String::new())
    };
    let input_line = Line::from(vec![
        Span::styled(
            "",
            Style::default()
                .fg(Color::Green)
                .add_modifier(Modifier::BOLD),
        ),
        Span::raw(before_cursor.to_string()),
        Span::styled(
            cursor_ch,
            Style::default().bg(Color::White).fg(Color::Black),
        ),
        Span::raw(after_cursor),
    ]);
    frame.render_widget(
        Paragraph::new(input_line).block(Block::bordered()),
        input_area,
    );

    // --- Sidebars ---
    sidebar::draw(frame, &state.sidebar, area, None, right_area_opt);

    if state.map_fullscreen {
        frame.render_widget(Clear, content_area);
        let block = Block::bordered()
            .title(" Map ")
            .title_bottom(" Arrows pan  u/d z-level  c center  Esc/F6 close ")
            .border_style(Style::default().fg(Color::Cyan));
        let inner = block.inner(content_area);
        let lines = build_map_lines(
            &state.sidebar.automap,
            inner.width,
            inner.height,
            state.map_pan,
        );
        frame.render_widget(block, content_area);
        frame.render_widget(
            Paragraph::new(lines).style(Style::default().fg(Color::White)),
            inner,
        );
    }

    // --- Status bar ---
    // Use REVERSED on the paragraph base so the bar adapts to dark/light themes.
    // Latency values are rendered as colored badges (explicit bg + black fg +
    // REVERSED removed) so their color is always visible in any theme.
    let base_style = Style::default().add_modifier(Modifier::REVERSED);
    let conn_icon = if state.connected {
        Span::styled("", Style::default().fg(Color::Green))
    } else {
        Span::styled("", Style::default().fg(Color::Red))
    };
    let disc_hint = if !state.connected {
        "  disconnected"
    } else {
        ""
    };
    let latency_badge_color = |value: Option<u64>| match value {
        Some(ms) if ms <= 120 => Color::Rgb(60, 180, 60),
        Some(ms) if ms <= 250 => Color::Rgb(200, 160, 0),
        Some(_) => Color::Rgb(200, 50, 50),
        None => Color::Reset,
    };
    // Badge style: colored bg, black text, no REVERSED so colors are exact.
    let badge = |color: Color, text: String, bold: bool| -> Span<'static> {
        let mut style = Style::default()
            .fg(Color::Black)
            .bg(color)
            .remove_modifier(Modifier::REVERSED);
        if bold {
            style = style.add_modifier(Modifier::BOLD);
        }
        Span::styled(text, style)
    };
    let current_badge_color = latency_badge_color(state.latency);
    //let avg_badge_color     = latency_badge_color(state.latency_avg());
    let latency_spans: Vec<Span<'static>> = match (state.latency, state.latency_avg()) {
        (Some(current), Some(_avg)) => vec![
            Span::raw("  "),
            badge(current_badge_color, format!(" {current}ms "), true),
            //Span::raw(" (ø"),
            //badge(avg_badge_color, format!(" {avg}ms "), false),
            //Span::raw(")"),
        ],
        (Some(current), None) => vec![
            Span::raw("  "),
            badge(current_badge_color, format!(" {current}ms "), true),
        ],
        _ => vec![Span::raw("  --")],
    };
    let mut status_spans: Vec<Span<'static>> = vec![
        conn_icon,
        Span::raw(format!(" {server_name}  /  {char_name}")),
    ];
    status_spans.extend(latency_spans);
    status_spans.push(Span::raw(format!(
        "{disc_hint}   \u{2191}\u{2193} hist   PgUp/Dn scroll   Ctrl+Y copy   F6 map   Ctrl+Q disc   F2/F3:sidebars  F4:panel"
    )));
    let status_line = Line::from(status_spans);
    frame.render_widget(Paragraph::new(status_line).style(base_style), status_area);
}