pyroclear 0.1.0

Watch your terminal go up in flames! A fire animation for clearing your terminal.
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
// tui.rs — raw terminal, key input, interactive picker & settings TUI.

use std::io::{self, Write};
use std::time::Duration;
use crate::{ESC, palettes::*, config::{AnimSettings, PaletteChoice}, engine::{terminal_size, Rng}};

// ── Raw terminal guard ────────────────────────────────────────────────

/// RAII guard: enters raw mode on creation, restores termios on drop.
/// Also switches to the alternate screen buffer.
pub struct TermRawGuard {
    orig: libc::termios,
}

impl TermRawGuard {
    pub fn enter() -> io::Result<Self> {
        let mut orig: libc::termios = unsafe { std::mem::zeroed() };
        if unsafe { libc::tcgetattr(libc::STDIN_FILENO, &mut orig) } != 0 {
            return Err(io::Error::last_os_error());
        }
        let mut raw = orig;
        raw.c_lflag &= !(libc::ICANON | libc::ECHO | libc::ISIG);
        raw.c_cc[libc::VMIN]  = 1;
        raw.c_cc[libc::VTIME] = 0;
        unsafe { libc::tcsetattr(libc::STDIN_FILENO, libc::TCSANOW, &raw) };
        print!("{ESC}[?1049h{ESC}[?25l"); // alternate screen, hide cursor
        io::stdout().flush().ok();
        Ok(Self { orig })
    }
}

impl Drop for TermRawGuard {
    fn drop(&mut self) {
        print!("{ESC}[?1049l{ESC}[?25h"); // restore screen and cursor
        io::stdout().flush().ok();
        unsafe { libc::tcsetattr(libc::STDIN_FILENO, libc::TCSANOW, &self.orig) };
    }
}

// ── Key reading ───────────────────────────────────────────────────────

#[derive(Debug, PartialEq)]
pub enum Key {
    Up, Down, Left, Right,
    PageUp, PageDown,
    Enter,
    Char(char),
    Esc,
    Backspace,
    Other,
}

pub fn read_key() -> Key {
    let mut buf = [0u8; 6];
    let n = unsafe { libc::read(libc::STDIN_FILENO, buf.as_mut_ptr() as *mut _, 6) };
    if n <= 0 { return Key::Other; }
    match &buf[..n as usize] {
        [0x1b, b'[', b'A', ..] => Key::Up,
        [0x1b, b'[', b'B', ..] => Key::Down,
        [0x1b, b'[', b'C', ..] => Key::Right,
        [0x1b, b'[', b'D', ..] => Key::Left,
        [0x1b, b'[', b'5', b'~', ..] => Key::PageUp,
        [0x1b, b'[', b'6', b'~', ..] => Key::PageDown,
        [0x1b, ..] if n == 1  => Key::Esc,
        [0x0d] | [0x0a]       => Key::Enter,
        [0x7f] | [0x08]       => Key::Backspace,
        [c] if *c >= 0x20 && *c < 0x7f => Key::Char(*c as char),
        _ => Key::Other,
    }
}

// ── Hex prompt ────────────────────────────────────────────────────────

pub fn prompt_hex(label: &str, row: u16) -> Option<String> {
    let mut input = String::new();
    loop {
        print!("{ESC}[{row};1H{ESC}[2K{ESC}[38;2;255;200;80m{label}{ESC}[0m {input}_");
        io::stdout().flush().ok();
        match read_key() {
            Key::Enter => {
                let s = input.trim().to_string();
                if s.is_empty() { return None; }
                if hex_to_rgb(&s).is_some() { return Some(s); }
                print!(
                    "{ESC}[{row};1H{ESC}[2K\
                     {ESC}[38;2;255;70;70m  ✗ invalid hex — need #rrggbb{ESC}[0m"
                );
                io::stdout().flush().ok();
                std::thread::sleep(Duration::from_millis(800));
                input.clear();
            }
            Key::Backspace => { input.pop(); }
            Key::Char(c)   => { if input.len() < 7 { input.push(c); } }
            Key::Esc       => return None,
            _              => {}
        }
    }
}

// ── String helper ─────────────────────────────────────────────────────

/// Truncate to at most `max_chars` display characters (Unicode-safe).
fn truncate_display(s: &str, max_chars: usize) -> &str {
    if max_chars == 0 { return ""; }
    let mut n = 0usize;
    for (byte_idx, _) in s.char_indices() {
        if n >= max_chars { return &s[..byte_idx]; }
        n += 1;
    }
    s
}

// ── Picker filter ─────────────────────────────────────────────────────

/// Build a filtered list of NAMED_PALETTES indices matching `search`.
/// Index == NAMED_PALETTES.len() is the "Custom" sentinel.
fn apply_filter(search: &str) -> Vec<usize> {
    let s = search.to_lowercase();
    if s.is_empty() {
        let mut v: Vec<usize> = (0..NAMED_PALETTES.len()).collect();
        v.push(NAMED_PALETTES.len());
        return v;
    }
    let mut v: Vec<usize> = NAMED_PALETTES
        .iter()
        .enumerate()
        .filter(|(_, entry)| {
            let (id, display, desc, _, _) = entry;
            id.to_lowercase().contains(s.as_str())
                || display.to_lowercase().contains(s.as_str())
                || desc.to_lowercase().contains(s.as_str())
        })
        .map(|(i, _)| i)
        .collect();
    if "custom".contains(s.as_str()) { v.push(NAMED_PALETTES.len()); }
    v
}

// ── Picker draw ───────────────────────────────────────────────────────

fn draw_picker(
    selected: usize,
    filter: &[usize],
    search: &str,
    search_active: bool,
    (cols, rows): (usize, usize),
) {
    let sw_w = (cols.saturating_sub(50)).clamp(10, 30);

    // Row 1: title bar
    print!("{ESC}[1;1H");
    let title = " pyroclear  ◆  color picker ";
    let gap   = cols.saturating_sub(title.len());
    print!(
        "{ESC}[48;2;20;20;36m{ESC}[38;2;255;200;80m{title}\
         {ESC}[38;2;50;50;72m{}{ESC}[0m",
        " ".repeat(gap)
    );

    // Row 2: search bar
    print!("{ESC}[2;1H{ESC}[2K");
    let match_count = filter.iter().filter(|&&i| i < NAMED_PALETTES.len()).count();
    if search.is_empty() && !search_active {
        print!(
            "  {ESC}[38;2;55;55;78m/{ESC}[0m \
             {ESC}[38;2;52;52;72msearch palettes...{ESC}[0m  \
             {ESC}[38;2;52;52;72m{match_count} palettes{ESC}[0m"
        );
    } else {
        let caret = if search_active { "_" } else { "" };
        let col   = if search_active { "255;220;80" } else { "150;150;180" };
        print!(
            "  {ESC}[38;2;{col}m/{search}{caret}{ESC}[0m  \
             {ESC}[38;2;95;95;115m{match_count} match{}{ESC}[0m",
            if match_count == 1 { "" } else { "es" }
        );
    }

    // Palette list
    let list_start = 2usize;
    let list_end   = rows.saturating_sub(4);
    let visible    = list_end.saturating_sub(list_start);
    let offset     = if selected >= visible { selected - visible + 1 } else { 0 };

    for slot in 0..visible {
        let fi_pos = slot + offset;
        let row    = (list_start + slot + 1) as u16;
        print!("{ESC}[{row};1H{ESC}[2K");
        if fi_pos >= filter.len() { continue; }

        let fi     = filter[fi_pos];
        let is_sel = fi_pos == selected;
        let cursor = if is_sel { "" } else { " " };

        if is_sel { print!("{ESC}[48;2;20;26;46m"); }

        let (name_str, desc_str, sw_str, fhex, thex) = if fi < NAMED_PALETTES.len() {
            let (id, display, desc, fh, th) = NAMED_PALETTES[fi];
            let p = if id == "fire" {
                soften(&FIRE_PALETTE, SOFTEN_DESATURATE, SOFTEN_BRIGHTEN)
            } else {
                let from = hex_to_rgb(fh).unwrap_or((0, 0, 0));
                let to   = hex_to_rgb(th).unwrap_or((255, 255, 255));
                soften(&generate_palette(from, to), SOFTEN_DESATURATE, SOFTEN_BRIGHTEN)
            };
            (display, desc, palette_swatch(&p, sw_w), fh, th)
        } else {
            ("Custom", "enter your own #rrggbb gradient",
             swatch((50, 0, 60), (255, 180, 80), sw_w), "", "")
        };

        if is_sel {
            print!("{ESC}[1;38;2;255;230;100m");
        } else {
            print!("{ESC}[38;2;178;178;205m");
        }

        print!("  {cursor} {name_str:<18}  {sw_str}");

        if fi < NAMED_PALETTES.len() {
            print!(
                "  {ESC}[38;2;82;82;108m{fhex}\
                 {ESC}[38;2;48;48;68m→\
                 {ESC}[38;2;82;82;108m{thex}{ESC}[0m"
            );
        }

        let used_approx = 4 + 18 + 2 + sw_w + 2 + 17;
        if cols > used_approx + 6 {
            let max_d = cols.saturating_sub(used_approx + 4);
            let td    = truncate_display(desc_str, max_d);
            let dc    = if is_sel { "140;140;172" } else { "62;62;82" };
            print!("  {ESC}[38;2;{dc}m{td}{ESC}[0m");
        }

        print!("{ESC}[0m");
    }

    // Separator
    let sep_row = (list_end + 1) as u16;
    print!(
        "{ESC}[{sep_row};1H{ESC}[2K\
         {ESC}[38;2;35;35;55m{}{ESC}[0m",
        "".repeat(cols)
    );

    // Preview swatch for selected entry
    let prev_row = (rows - 2) as u16;
    print!("{ESC}[{prev_row};1H{ESC}[2K");
    if let Some(&fi) = filter.get(selected) {
        if fi < NAMED_PALETTES.len() {
            let (id, display, _, fh, th) = NAMED_PALETTES[fi];
            let p = if id == "fire" {
                soften(&FIRE_PALETTE, SOFTEN_DESATURATE, SOFTEN_BRIGHTEN)
            } else {
                let from = hex_to_rgb(fh).unwrap_or((0, 0, 0));
                let to   = hex_to_rgb(th).unwrap_or((255, 255, 255));
                soften(&generate_palette(from, to), SOFTEN_DESATURATE, SOFTEN_BRIGHTEN)
            };
            let pw = cols.saturating_sub(22).min(72);
            let ps = palette_swatch(&p, pw);
            print!("  {ESC}[38;2;92;92;115m▸ {ESC}[38;2;172;172;198m{display:<16}{ESC}[0m  {ps}");
        } else {
            print!("  {ESC}[38;2;92;92;115m▸ Custom gradient — press Enter to configure{ESC}[0m");
        }
    }

    // Key hints
    let hint_row = rows as u16;
    print!("{ESC}[{hint_row};1H{ESC}[2K");
    print!(
        "{ESC}[48;2;15;15;28m \
         {ESC}[38;2;255;200;80m↑↓{ESC}[38;2;98;98;128m move  \
         {ESC}[38;2;255;200;80mPgUp/Dn{ESC}[38;2;98;98;128m page  \
         {ESC}[38;2;255;200;80m/{ESC}[38;2;98;98;128m search  \
         {ESC}[38;2;255;200;80mr{ESC}[38;2;98;98;128m random  \
         {ESC}[38;2;255;200;80mEnter{ESC}[38;2;98;98;128m select  \
         {ESC}[38;2;255;200;80mEsc{ESC}[38;2;98;98;128m·{ESC}[38;2;255;200;80mq{ESC}[38;2;98;98;128m quit \
         {ESC}[0m"
    );

    io::stdout().flush().ok();
}

// ── Interactive picker ────────────────────────────────────────────────

/// Run the interactive palette picker. Returns the chosen PaletteChoice
/// or None if the user cancelled.
pub fn interactive_pick() -> Option<PaletteChoice> {
    let _guard = TermRawGuard::enter().ok()?;

    let mut selected      = 0usize;
    let mut search        = String::new();
    let mut search_active = false;
    let mut filter        = apply_filter("");
    let page_size         = 10usize;

    loop {
        let size = terminal_size();
        draw_picker(selected, &filter, &search, search_active, size);

        let key = read_key();

        if search_active {
            match key {
                Key::Esc | Key::Enter => { search_active = false; }
                Key::Backspace => {
                    search.pop();
                    filter   = apply_filter(&search);
                    selected = 0;
                }
                Key::Char(c) => {
                    search.push(c);
                    filter   = apply_filter(&search);
                    selected = 0;
                }
                _ => {}
            }
        } else {
            match key {
                Key::Up   => { if selected > 0 { selected -= 1; } }
                Key::Down => {
                    if !filter.is_empty() && selected + 1 < filter.len() { selected += 1; }
                }
                Key::PageUp   => { selected = selected.saturating_sub(page_size); }
                Key::PageDown => {
                    if !filter.is_empty() {
                        selected = (selected + page_size).min(filter.len() - 1);
                    }
                }
                Key::Char('/') => { search_active = true; }
                Key::Char('r') | Key::Char('R') => {
                    if !filter.is_empty() {
                        let mut rng = Rng::new();
                        selected = (rng.next_u64() % filter.len() as u64) as usize;
                    }
                }
                Key::Enter => {
                    if let Some(&fi) = filter.get(selected) {
                        if fi < NAMED_PALETTES.len() {
                            let (id, _, _, _, _) = NAMED_PALETTES[fi];
                            return Some(PaletteChoice::Named(id.to_string()));
                        } else {
                            let (_, rows) = terminal_size();
                            let base = rows as u16 - 4;
                            print!("{ESC}[{base};1H{ESC}[J");
                            print!(
                                "{ESC}[{base};1H\
                                 {ESC}[38;2;175;175;200m  Enter hex colors (e.g. #ff0000){ESC}[0m\n"
                            );
                            io::stdout().flush().ok();
                            let from_str = prompt_hex("  From:", base + 2)?;
                            let to_str   = prompt_hex("  To:  ", base + 3)?;
                            let from = hex_to_rgb(&from_str)?;
                            let to   = hex_to_rgb(&to_str)?;
                            return Some(PaletteChoice::Custom { from, to });
                        }
                    }
                }
                Key::Esc => {
                    if !search.is_empty() {
                        search.clear();
                        filter   = apply_filter("");
                        selected = 0;
                    } else {
                        return None;
                    }
                }
                Key::Char('q') => return None,
                _ => {}
            }
        }
    }
}

// ── Settings draw ─────────────────────────────────────────────────────

fn draw_settings(selected: usize, settings: &AnimSettings, (cols, rows): (usize, usize)) {
    // Title bar
    print!("{ESC}[1;1H");
    let title = " pyroclear  ◆  animation settings ";
    let gap   = cols.saturating_sub(title.len());
    print!(
        "{ESC}[48;2;20;20;36m{ESC}[38;2;255;200;80m{title}\
         {ESC}[38;2;50;50;72m{}{ESC}[0m",
        " ".repeat(gap)
    );

    let items = [
        ("FPS / Speed    ", match settings.fps {
            15  => "15 fps  (extremely slow / cinematic)",
            30  => "30 fps  (standard retro feel)",
            45  => "45 fps  (smooth legacy animation)",
            60  => "60 fps  (default smooth 60fps)",
            75  => "75 fps  (high refresh rate)",
            90  => "90 fps  (ultra high refresh rate)",
            120 => "120 fps (blazing fast execution)",
            _   => "custom fps",
        }),
        ("Wind / Breeze  ", match settings.wind {
            -2 => "Strong Left   (blowing hard left)",
            -1 => "Gentle Left   (gently drifting left)",
             0 => "None          (rising straight up)",
             1 => "Gentle Right  (gently drifting right)",
             2 => "Strong Right  (blowing hard right)",
             _ => "unknown wind",
        }),
        ("Flame Height   ", match settings.height {
            0 => "Low           (fast decay, small fire)",
            1 => "Medium        (default height decay)",
            2 => "High          (slow decay, tall flames)",
            3 => "Extreme       (minimum decay, full screen)",
            _ => "unknown height",
        }),
    ];

    let start_row = 3u16;
    for (idx, (label, value)) in items.iter().enumerate() {
        let row    = start_row + idx as u16 * 2;
        let is_sel = idx == selected;
        let cursor = if is_sel { "" } else { " " };

        print!("{ESC}[{row};1H{ESC}[2K");
        if is_sel {
            print!("{ESC}[48;2;20;26;46m");
            print!("  {cursor} {ESC}[1;38;2;255;230;100m{label}{ESC}[0m");
            print!("{ESC}[48;2;20;26;46m   ◀  {ESC}[1;38;2;255;255;255m{value:<44}{ESC}[0m◀   ");
            let taken = 4 + 1 + label.len() + 6 + 44 + 4;
            if cols > taken { print!("{}", " ".repeat(cols - taken)); }
            print!("{ESC}[0m");
        } else {
            print!("    {label}      {ESC}[38;2;178;178;205m{value}{ESC}[0m");
        }
    }

    let sep_row = start_row + items.len() as u16 * 2 + 1;
    print!(
        "{ESC}[{sep_row};1H{ESC}[2K\
         {ESC}[38;2;35;35;55m{}{ESC}[0m",
        "".repeat(cols)
    );

    let hint_row = rows as u16;
    print!("{ESC}[{hint_row};1H{ESC}[2K");
    print!(
        "{ESC}[48;2;15;15;28m \
         {ESC}[38;2;255;200;80m↑↓{ESC}[38;2;98;98;128m navigate  \
         {ESC}[38;2;255;200;80m←→{ESC}[38;2;98;98;128m adjust  \
         {ESC}[38;2;255;200;80mEnter/s{ESC}[38;2;98;98;128m save & run  \
         {ESC}[38;2;255;200;80mEsc/q{ESC}[38;2;98;98;128m cancel \
         {ESC}[0m"
    );

    io::stdout().flush().ok();
}

// ── Interactive settings ──────────────────────────────────────────────

pub fn interactive_settings(current: &AnimSettings) -> Option<AnimSettings> {
    let _guard = TermRawGuard::enter().ok()?;
    let mut settings = current.clone();
    let mut selected = 0usize;
    let fps_options  = [15, 30, 45, 60, 75, 90, 120];

    loop {
        let size = terminal_size();
        draw_settings(selected, &settings, size);

        match read_key() {
            Key::Up   => { if selected > 0 { selected -= 1; } }
            Key::Down => { if selected < 2 { selected += 1; } }
            Key::Left => {
                match selected {
                    0 => {
                        if let Some(idx) = fps_options.iter().position(|&x| x == settings.fps) {
                            settings.fps = if idx > 0 {
                                fps_options[idx - 1]
                            } else {
                                fps_options[fps_options.len() - 1]
                            };
                        }
                    }
                    1 => {
                        settings.wind = if settings.wind > -2 { settings.wind - 1 } else { 2 };
                    }
                    2 => {
                        settings.height = if settings.height > 0 { settings.height - 1 } else { 3 };
                    }
                    _ => {}
                }
            }
            Key::Right => {
                match selected {
                    0 => {
                        if let Some(idx) = fps_options.iter().position(|&x| x == settings.fps) {
                            settings.fps = if idx + 1 < fps_options.len() {
                                fps_options[idx + 1]
                            } else {
                                fps_options[0]
                            };
                        }
                    }
                    1 => {
                        settings.wind = if settings.wind < 2 { settings.wind + 1 } else { -2 };
                    }
                    2 => {
                        settings.height = if settings.height < 3 { settings.height + 1 } else { 0 };
                    }
                    _ => {}
                }
            }
            Key::Enter | Key::Char('s') => return Some(settings),
            Key::Esc   | Key::Char('q') => return None,
            _ => {}
        }
    }
}

// ── Prompt string helper ──────────────────────────────────────────────

pub fn prompt_string(label: &str, row: u16) -> Option<String> {
    let mut input = String::new();
    loop {
        print!("{ESC}[{row};1H{ESC}[2K{ESC}[38;2;255;200;80m{label}{ESC}[0m {input}_");
        io::stdout().flush().ok();
        match read_key() {
            Key::Enter => {
                let s = input.trim().to_string();
                if !s.is_empty() { return Some(s); }
            }
            Key::Backspace => { input.pop(); }
            Key::Char(c)   => { if input.len() < 30 { input.push(c); } }
            Key::Esc       => return None,
            _              => {}
        }
    }
}

// ── Interactive Custom Palette Manager ─────────────────────────────────

pub fn interactive_custom() -> Option<PaletteChoice> {
    let _guard = TermRawGuard::enter().ok()?;
    let mut selected = 0usize;

    loop {
        let size = terminal_size();
        let (cols, rows) = size;
        
        let mut entries = crate::config::load_custom_palettes();

        // Draw header
        print!("{ESC}[1;1H");
        let title = " pyroclear  ◆  custom palettes ";
        let gap   = cols.saturating_sub(title.len());
        print!(
            "{ESC}[48;2;20;20;36m{ESC}[38;2;255;200;80m{title}\
             {ESC}[38;2;50;50;72m{}{ESC}[0m",
            " ".repeat(gap)
        );

        // Draw list
        let list_start = 2usize;
        let list_end   = rows.saturating_sub(4);
        let visible    = list_end.saturating_sub(list_start);
        
        if selected >= entries.len() && !entries.is_empty() {
            selected = entries.len() - 1;
        }

        let offset = if selected >= visible { selected - visible + 1 } else { 0 };

        for slot in 0..visible {
            let row = (list_start + slot + 1) as u16;
            print!("{ESC}[{row};1H{ESC}[2K");
            let idx = slot + offset;
            if idx >= entries.len() {
                continue;
            }

            let entry = &entries[idx];
            let is_sel = idx == selected;
            let cursor = if is_sel { "" } else { " " };

            if is_sel { print!("{ESC}[48;2;20;26;46m"); }

            let from_rgb = hex_to_rgb(&entry.from).unwrap_or((0,0,0));
            let to_rgb = hex_to_rgb(&entry.to).unwrap_or((255,255,255));
            let p = soften(&generate_palette(from_rgb, to_rgb), SOFTEN_DESATURATE, SOFTEN_BRIGHTEN);
            let sw_w = (cols.saturating_sub(55)).clamp(10, 30);
            let sw_str = palette_swatch(&p, sw_w);

            if is_sel {
                print!("{ESC}[1;38;2;255;230;100m");
            } else {
                print!("{ESC}[38;2;178;178;205m");
            }

            print!("  {cursor} {:<15} ({:<10})  {sw_str}  ", entry.display, entry.name);
            print!(
                "{ESC}[38;2;82;82;108m{}\
                 {ESC}[38;2;48;48;68m→\
                 {ESC}[38;2;82;82;108m{}{ESC}[0m",
                entry.from, entry.to
            );

            print!("{ESC}[0m");
        }

        if entries.is_empty() {
            print!("{ESC}[4;1H{ESC}[2K  No custom palettes saved yet. Press 'n' to create one!");
        }

        // Separator
        let sep_row = (list_end + 1) as u16;
        print!(
            "{ESC}[{sep_row};1H{ESC}[2K\
             {ESC}[38;2;35;35;55m{}{ESC}[0m",
            "".repeat(cols)
        );

        // Preview swatch for selected entry
        let prev_row = (rows - 2) as u16;
        print!("{ESC}[{prev_row};1H{ESC}[2K");
        if !entries.is_empty() && selected < entries.len() {
            let entry = &entries[selected];
            let from_rgb = hex_to_rgb(&entry.from).unwrap_or((0,0,0));
            let to_rgb = hex_to_rgb(&entry.to).unwrap_or((255,255,255));
            let p = soften(&generate_palette(from_rgb, to_rgb), SOFTEN_DESATURATE, SOFTEN_BRIGHTEN);
            let pw = cols.saturating_sub(22).min(72);
            let ps = palette_swatch(&p, pw);
            print!("  {ESC}[38;2;92;92;115m▸ {ESC}[38;2;172;172;198m{:<16}{ESC}[0m  {ps}", entry.display);
        }

        // Key hints
        let hint_row = rows as u16;
        print!("{ESC}[{hint_row};1H{ESC}[2K");
        print!(
            "{ESC}[48;2;15;15;28m \
             {ESC}[38;2;255;200;80m↑↓{ESC}[38;2;98;98;128m move  \
             {ESC}[38;2;255;200;80mn{ESC}[38;2;98;98;128m new  \
             {ESC}[38;2;255;200;80md{ESC}[38;2;98;98;128m delete  \
             {ESC}[38;2;255;200;80mEnter{ESC}[38;2;98;98;128m run  \
             {ESC}[38;2;255;200;80mEsc/q{ESC}[38;2;98;98;128m back \
             {ESC}[0m"
        );
        io::stdout().flush().ok();

        match read_key() {
            Key::Up => {
                if selected > 0 { selected -= 1; }
            }
            Key::Down => {
                if !entries.is_empty() && selected + 1 < entries.len() { selected += 1; }
            }
            Key::Char('n') | Key::Char('N') => {
                let base = rows as u16 - 4;
                print!("{ESC}[{base};1H{ESC}[J");
                print!(
                    "{ESC}[{base};1H\
                     {ESC}[38;2;175;175;200m  Create new custom palette{ESC}[0m\n"
                );
                io::stdout().flush().ok();
                
                if let Some(name) = prompt_string("  Slug (id):", base + 1) {
                    let slug = name.to_lowercase().replace(" ", "-");
                    if let Some(display) = prompt_string("  Name:     ", base + 2) {
                        if let Some(from_str) = prompt_hex("  From hex: ", base + 3) {
                            if let Some(to_str) = prompt_hex("  To hex:   ", base + 4) {
                                entries.push(crate::config::CustomPaletteEntry {
                                    name: slug,
                                    display,
                                    from: from_str,
                                    to: to_str,
                                });
                                crate::config::save_custom_palettes(&entries);
                                selected = entries.len() - 1;
                            }
                        }
                    }
                }
            }
            Key::Char('d') | Key::Char('D') => {
                if !entries.is_empty() && selected < entries.len() {
                    entries.remove(selected);
                    crate::config::save_custom_palettes(&entries);
                    if selected > 0 && selected >= entries.len() {
                        selected = entries.len() - 1;
                    }
                }
            }
            Key::Enter => {
                if !entries.is_empty() && selected < entries.len() {
                    let entry = &entries[selected];
                    if let Some(choice) = entry.to_palette_choice() {
                        return Some(choice);
                    }
                }
            }
            Key::Esc | Key::Char('q') => return None,
            _ => {}
        }
    }
}