chute-kun 0.1.0

TaskChute + Todoist CLI/TUI — Rust TUI template using ratatui + crossterm
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
use ratatui::{
    layout::Rect,
    prelude::*,
    widgets::{Block, Borders, Cell, Clear, Paragraph, Row, Table, Wrap},
};
use unicode_width::UnicodeWidthStr;

use crate::app::{App, DisplayMode, View};
use crate::clock::Clock;
use crate::task::Category as TaskCategory;
use crate::task::TaskState;

// Theme: darker list highlights for better contrast with default light (white) text.
// These colors aim to keep contrast acceptable on common terminals while avoiding
// the eye‑searing effect of bright Blue/Cyan backgrounds.
pub const SELECTED_ROW_BG: Color = Color::Rgb(0, 60, 120); // dark blue
pub const HOVER_ROW_BG: Color = Color::Rgb(0, 100, 100); // dark cyan/teal
                                                         // Drag visuals
pub const DRAG_SOURCE_BG_A: Color = Color::Rgb(100, 0, 120); // purple (pulse A)
pub const DRAG_SOURCE_BG_B: Color = Color::Rgb(140, 0, 160); // brighter purple (pulse B)
pub const DRAG_TARGET_BG_A: Color = Color::Rgb(0, 120, 60); // greenish (pulse A)
pub const DRAG_TARGET_BG_B: Color = Color::Rgb(0, 160, 80); // brighter greenish (pulse B)

const MIN_LIST_LINES: u16 = 3; // table header + at least two rows

pub fn draw(f: &mut Frame, app: &App) {
    let area: Rect = f.area();
    let header_line = header_title_line(app_display_base(app), app);
    let actions_line = header_action_buttons_line(app);
    // Left-align stats and right-align action buttons independently on the title bar
    let block = Block::default()
        .title(header_line.left_aligned())
        .title(actions_line.right_aligned())
        .borders(Borders::ALL);
    let inner = block.inner(area);
    f.render_widget(block, area);

    // Optional active-task banner just under the tabs
    let active_banner = format_active_banner(app);

    // Pre-compute wrapped help lines for current width, to size the layout.
    let help_lines = help_lines_for_width(app, inner.width.max(1));
    // Clamp help height so the task table keeps at least header + two rows visible
    let mut help_height = help_lines.len() as u16; // 1+ lines depending on width
    let reserved = 1 /* tabs */ + if active_banner.is_some() { 1 } else { 0 } + MIN_LIST_LINES;
    let max_help = inner.height.saturating_sub(reserved);
    if max_help > 0 {
        help_height = help_height.min(max_help);
    }
    // Ensure at least three lines (gauge + labels + help) when space allows
    help_height = if max_help >= 3 {
        help_height.max(3)
    } else if max_help >= 2 {
        help_height.max(2)
    } else {
        help_height.max(1)
    };

    // Split inner area: tabs, optional banner, task list, help block.
    // Use Min(0) for the list so rendering can gracefully degrade in tiny terminals.
    let mut constraints: Vec<Constraint> = vec![Constraint::Length(1)];
    if active_banner.is_some() {
        constraints.push(Constraint::Length(1));
    }
    constraints.push(Constraint::Min(0));
    constraints.push(Constraint::Length(help_height.max(1)));
    let chunks =
        Layout::default().direction(Direction::Vertical).constraints(constraints).split(inner);

    // Tabs for date views (custom-rendered to support hover + precise hitboxes)
    render_tabs_line(f, chunks[0], app);

    // If we have an active banner, render it right below tabs
    let mut content_idx = 1usize; // index into chunks for main content
    if let Some(line) = active_banner {
        // Underline the entire banner line for visibility
        let para = Paragraph::new(line.patch_style(Modifier::UNDERLINED));
        f.render_widget(para, chunks[1]);
        content_idx = 2;
    }

    // Main content: always keep task list/table; popups render as overlays
    // Table-based rendering: columns on the left for planned and actual logs
    let now = app_display_base(app);
    let tasks_slice: Vec<crate::task::Task> = match app.view() {
        View::Past => app.history_tasks().clone(),
        View::Today => app.day.tasks.clone(),
        View::Future => app.tomorrow_tasks().clone(),
    };
    if tasks_slice.is_empty() {
        let para = Paragraph::new("No tasks — press 'i' to add");
        f.render_widget(para, chunks[content_idx]);
    } else {
        match app.display_mode() {
            DisplayMode::List => {
                let table = build_task_table(now, app, &tasks_slice);
                f.render_widget(table, chunks[content_idx]);
            }
            DisplayMode::Calendar => {
                render_calendar_day_at(
                    f,
                    chunks[content_idx],
                    app,
                    &tasks_slice,
                    crate::clock::system_now_minutes(),
                );
            }
        }
    }

    // Help block with 24h gauge at the top of the help area (help text below)
    // When an active banner is present, help resides at the last chunk, not index 2.
    let help_idx = chunks.len().saturating_sub(1);
    if chunks[help_idx].height > 0 {
        let help_area = chunks[help_idx];
        match help_area.height {
            0 => {}
            1 => {
                // Not enough space: show help text, skip gauge/labels
                let help_text = help_lines.join("\n");
                let help = Paragraph::new(help_text)
                    .style(Style::default().fg(Color::DarkGray))
                    .wrap(Wrap { trim: true });
                f.render_widget(help, help_area);
            }
            2 => {
                // Show gauge on first line, help text on second; skip labels to keep help visible
                let gauge_area =
                    Rect { x: help_area.x, y: help_area.y, width: help_area.width, height: 1 };
                render_bottom_24h_gauge(f, app, gauge_area, crate::clock::system_now_minutes());
                let help_text_area =
                    Rect { x: help_area.x, y: help_area.y + 1, width: help_area.width, height: 1 };
                let help_text = help_lines.join("\n");
                let help = Paragraph::new(help_text)
                    .style(Style::default().fg(Color::DarkGray))
                    .wrap(Wrap { trim: true });
                f.render_widget(help, help_text_area);
            }
            _ => {
                // ≥3 lines: gauge, labels, then help text
                let gauge_area =
                    Rect { x: help_area.x, y: help_area.y, width: help_area.width, height: 1 };
                render_bottom_24h_gauge(f, app, gauge_area, crate::clock::system_now_minutes());
                let label_rect =
                    Rect { x: help_area.x, y: help_area.y + 1, width: help_area.width, height: 1 };
                render_gauge_labels(f, label_rect);
                let help_text_area = Rect {
                    x: help_area.x,
                    y: help_area.y + 2,
                    width: help_area.width,
                    height: help_area.height.saturating_sub(2),
                };
                if help_text_area.height > 0 {
                    let help_text = help_lines.join("\n");
                    let help = Paragraph::new(help_text)
                        .style(Style::default().fg(Color::DarkGray))
                        .wrap(Wrap { trim: true });
                    f.render_widget(help, help_text_area);
                }
            }
        }
    }

    // Overlay: centered estimate editor popup (date + slider + OK/Cancel)
    if let Some(popup) = compute_estimate_popup_rect(app, area) {
        let border = Style::default().fg(Color::Yellow);
        let title_line =
            Line::from(Span::styled(" Estimate ", border.add_modifier(Modifier::BOLD)));
        let block = Block::default().borders(Borders::ALL).title(title_line).border_style(border);
        f.render_widget(Clear, popup);
        f.render_widget(block.clone(), popup);
        let inner = block.inner(popup);
        let msg = {
            let t = app.day.tasks.get(app.selected_index()).map(|t| t.title.as_str()).unwrap_or("");
            format!("Estimate: {}m — {}", app.selected_estimate().unwrap_or(0), t)
        };
        let msg_rect = Rect { x: inner.x, y: inner.y, width: inner.width, height: 1 };
        f.render_widget(
            Paragraph::new(Span::styled(msg, Style::default().fg(Color::Yellow))),
            msg_rect,
        );
        if let Some(t) = app.day.tasks.get(app.selected_index()) {
            render_date_line(f, app, popup, inner, Color::Yellow, t.planned_ymd);
        }
        let (track, ok, cancel) = estimate_slider_hitboxes(app, popup);
        render_slider_line(f, track, app.selected_estimate().unwrap_or(0));
        let btn_y = ok.y;
        let mut spans: Vec<Span> = Vec::new();
        let pad = (ok.x.saturating_sub(inner.x)) as usize;
        if pad > 0 {
            spans.push(Span::raw(" ".repeat(pad)));
        }
        let ok_style = if matches!(app.popup_hover_button(), Some(crate::app::PopupButton::EstOk)) {
            Style::default().fg(Color::Black).bg(Color::Cyan).add_modifier(Modifier::BOLD)
        } else {
            Style::default().fg(Color::Black).bg(Color::Blue).add_modifier(Modifier::BOLD)
        };
        spans.push(Span::styled("OK".to_string(), ok_style));
        let gap2 = cancel.x.saturating_sub(ok.x + ok.width) as usize;
        if gap2 > 0 {
            spans.push(Span::raw(" ".repeat(gap2)));
        }
        let cancel_style =
            if matches!(app.popup_hover_button(), Some(crate::app::PopupButton::EstCancel)) {
                Style::default().fg(Color::Black).bg(Color::Cyan).add_modifier(Modifier::BOLD)
            } else {
                Style::default().fg(Color::Black).bg(Color::Gray).add_modifier(Modifier::BOLD)
            };
        spans.push(Span::styled("Cancel".to_string(), cancel_style));
        let btn_rect = Rect { x: inner.x, y: btn_y, width: inner.width, height: 1 };
        f.render_widget(Paragraph::new(Line::from(spans)), btn_rect);
    }

    // Overlay: Start Time slider popup (Space)
    if let Some(popup) = compute_start_time_popup_rect(app, area) {
        let border = Style::default().fg(Color::Cyan);
        let title_line =
            Line::from(Span::styled(" Start Time ", border.add_modifier(Modifier::BOLD)));
        let block = Block::default().borders(Borders::ALL).title(title_line).border_style(border);
        f.render_widget(Clear, popup);
        f.render_widget(block.clone(), popup);
        let inner = block.inner(popup);
        let mins =
            app.input_buffer().and_then(|s| s.parse::<u16>().ok()).unwrap_or(app_display_base(app));
        let hh = (mins / 60) % 24;
        let mm = mins % 60;
        let title = app.day.tasks.get(app.selected_index()).map(|t| t.title.as_str()).unwrap_or("");
        let msg = if title.is_empty() {
            format!("Start: {:02}:{:02}", hh, mm)
        } else {
            format!("Start: {:02}:{:02}{}", hh, mm, title)
        };
        let msg_rect = Rect { x: inner.x, y: inner.y, width: inner.width, height: 1 };
        f.render_widget(Paragraph::new(Span::styled(msg, border)), msg_rect);
        let (track, ok, cancel) = estimate_slider_hitboxes(app, popup);
        render_time_slider_line(f, track, mins);
        let btn_y = ok.y;
        let mut spans: Vec<Span> = Vec::new();
        let pad = (ok.x.saturating_sub(inner.x)) as usize;
        if pad > 0 {
            spans.push(Span::raw(" ".repeat(pad)));
        }
        let ok_style = if matches!(app.popup_hover_button(), Some(crate::app::PopupButton::EstOk)) {
            Style::default().fg(Color::Black).bg(Color::Cyan).add_modifier(Modifier::BOLD)
        } else {
            Style::default().fg(Color::Black).bg(Color::Blue).add_modifier(Modifier::BOLD)
        };
        spans.push(Span::styled("OK".to_string(), ok_style));
        let gap2 = cancel.x.saturating_sub(ok.x + ok.width) as usize;
        if gap2 > 0 {
            spans.push(Span::raw(" ".repeat(gap2)));
        }
        let cancel_style =
            if matches!(app.popup_hover_button(), Some(crate::app::PopupButton::EstCancel)) {
                Style::default().fg(Color::Black).bg(Color::Cyan).add_modifier(Modifier::BOLD)
            } else {
                Style::default().fg(Color::Black).bg(Color::Gray).add_modifier(Modifier::BOLD)
            };
        spans.push(Span::styled("Cancel".to_string(), cancel_style));
        let btn_rect = Rect { x: inner.x, y: btn_y, width: inner.width, height: 1 };
        f.render_widget(Paragraph::new(Line::from(spans)), btn_rect);
    }

    // Overlay: centered input popup (styled buttons)
    if let Some(popup) = compute_input_popup_rect(app, area) {
        let border = Style::default().fg(Color::Cyan);
        let title_line =
            Line::from(Span::styled(" New Task ", border.add_modifier(Modifier::BOLD)));
        let block = Block::default().borders(Borders::ALL).title(title_line).border_style(border);
        f.render_widget(Clear, popup);
        f.render_widget(block.clone(), popup);
        let inner = block.inner(popup);
        let buf = app.input_buffer().unwrap_or("");
        let msg = format!("Title: {} _", buf);
        let msg_rect = Rect { x: inner.x, y: inner.y, width: inner.width, height: 1 };
        f.render_widget(
            Paragraph::new(Span::styled(msg, Style::default().fg(Color::Cyan))),
            msg_rect,
        );
        let (add, cancel) = input_popup_button_hitboxes(app, popup);
        let btn_y = add.y;
        let mut spans: Vec<Span> = Vec::new();
        let pad = (add.x.saturating_sub(inner.x)) as usize;
        if pad > 0 {
            spans.push(Span::raw(" ".repeat(pad)));
        }
        let add_style =
            if matches!(app.popup_hover_button(), Some(crate::app::PopupButton::InputAdd)) {
                Style::default().fg(Color::Black).bg(Color::Cyan).add_modifier(Modifier::BOLD)
            } else {
                Style::default().fg(Color::Black).bg(Color::Green).add_modifier(Modifier::BOLD)
            };
        spans.push(Span::styled("OK".to_string(), add_style));
        let gap = cancel.x.saturating_sub(add.x + add.width) as usize;
        if gap > 0 {
            spans.push(Span::raw(" ".repeat(gap)));
        }
        let cancel_style =
            if matches!(app.popup_hover_button(), Some(crate::app::PopupButton::InputCancel)) {
                Style::default().fg(Color::Black).bg(Color::Cyan).add_modifier(Modifier::BOLD)
            } else {
                Style::default().fg(Color::Black).bg(Color::Gray).add_modifier(Modifier::BOLD)
            };
        spans.push(Span::styled("Cancel".to_string(), cancel_style));
        let btn_rect = Rect { x: inner.x, y: btn_y, width: inner.width, height: 1 };
        f.render_widget(Paragraph::new(Line::from(spans)), btn_rect);
    }

    // Overlay: command palette popup (two buttons Run/Cancel)
    if let Some(popup) = compute_command_popup_rect(app, area) {
        let border = Style::default().fg(Color::Magenta);
        let title_line = Line::from(Span::styled(" Command ", border.add_modifier(Modifier::BOLD)));
        let block = Block::default().borders(Borders::ALL).title(title_line).border_style(border);
        f.render_widget(Clear, popup);
        f.render_widget(block.clone(), popup);
        let inner = block.inner(popup);
        let buf = app.input_buffer().unwrap_or("");
        let title = app.day.tasks.get(app.selected_index()).map(|t| t.title.as_str()).unwrap_or("");
        let suffix = if title.is_empty() { "".to_string() } else { format!("{}", title) };
        let msg = format!("Command: {} _{}", buf, suffix);
        let msg_rect = Rect { x: inner.x, y: inner.y, width: inner.width, height: 1 };
        f.render_widget(Paragraph::new(Span::styled(msg, border)), msg_rect);
        let (run, cancel) = command_popup_button_hitboxes(app, popup);
        let btn_y = run.y;
        let mut spans: Vec<Span> = Vec::new();
        let pad = (run.x.saturating_sub(inner.x)) as usize;
        if pad > 0 {
            spans.push(Span::raw(" ".repeat(pad)));
        }
        let run_style =
            if matches!(app.popup_hover_button(), Some(crate::app::PopupButton::InputAdd)) {
                Style::default().fg(Color::Black).bg(Color::Cyan).add_modifier(Modifier::BOLD)
            } else {
                Style::default().fg(Color::Black).bg(Color::Blue).add_modifier(Modifier::BOLD)
            };
        spans.push(Span::styled("Run".to_string(), run_style));
        let gap = cancel.x.saturating_sub(run.x + run.width) as usize;
        if gap > 0 {
            spans.push(Span::raw(" ".repeat(gap)));
        }
        let cancel_style =
            if matches!(app.popup_hover_button(), Some(crate::app::PopupButton::InputCancel)) {
                Style::default().fg(Color::Black).bg(Color::Cyan).add_modifier(Modifier::BOLD)
            } else {
                Style::default().fg(Color::Black).bg(Color::Gray).add_modifier(Modifier::BOLD)
            };
        spans.push(Span::styled("Cancel".to_string(), cancel_style));
        let btn_rect = Rect { x: inner.x, y: btn_y, width: inner.width, height: 1 };
        f.render_widget(Paragraph::new(Line::from(spans)), btn_rect);
    }

    // Overlay: new-task estimate slider input popup
    if let Some(popup) = compute_new_task_estimate_popup_rect(app, area) {
        let border = Style::default().fg(Color::Green);
        let title_line =
            Line::from(Span::styled(" Estimate ", border.add_modifier(Modifier::BOLD)));
        let block = Block::default().borders(Borders::ALL).title(title_line).border_style(border);
        f.render_widget(Clear, popup);
        f.render_widget(block.clone(), popup);
        let inner = block.inner(popup);
        let cur_est: u16 = app
            .input_buffer()
            .and_then(|s| s.parse::<u16>().ok())
            .or_else(|| app.new_task_default_estimate())
            .unwrap_or(25);
        let title = app.new_task_title().unwrap_or("");
        let msg = if title.is_empty() {
            format!("Estimate: {}m", cur_est)
        } else {
            format!("Estimate: {}m — {}", cur_est, title)
        };
        let msg_rect = Rect { x: inner.x, y: inner.y, width: inner.width, height: 1 };
        f.render_widget(
            Paragraph::new(Span::styled(msg, Style::default().fg(Color::Green))),
            msg_rect,
        );
        if let Some(ymd) = app.new_task_planned_ymd() {
            render_date_line(f, app, popup, inner, Color::Green, ymd);
        }
        // Slider track
        let (track, _ok, _cancel) = estimate_slider_hitboxes(app, popup);
        render_slider_line(f, track, cur_est);
        let (add, cancel) = input_popup_button_hitboxes(app, popup);
        let btn_y = add.y;
        let mut spans: Vec<Span> = Vec::new();
        let pad = (add.x.saturating_sub(inner.x)) as usize;
        if pad > 0 {
            spans.push(Span::raw(" ".repeat(pad)));
        }
        let add_style =
            if matches!(app.popup_hover_button(), Some(crate::app::PopupButton::InputAdd)) {
                Style::default().fg(Color::Black).bg(Color::Cyan).add_modifier(Modifier::BOLD)
            } else {
                Style::default().fg(Color::Black).bg(Color::Green).add_modifier(Modifier::BOLD)
            };
        spans.push(Span::styled("Add".to_string(), add_style));
        let gap = cancel.x.saturating_sub(add.x + add.width) as usize;
        if gap > 0 {
            spans.push(Span::raw(" ".repeat(gap)));
        }
        let cancel_style =
            if matches!(app.popup_hover_button(), Some(crate::app::PopupButton::InputCancel)) {
                Style::default().fg(Color::Black).bg(Color::Cyan).add_modifier(Modifier::BOLD)
            } else {
                Style::default().fg(Color::Black).bg(Color::Gray).add_modifier(Modifier::BOLD)
            };
        spans.push(Span::styled("Cancel".to_string(), cancel_style));
        let btn_rect = Rect { x: inner.x, y: btn_y, width: inner.width, height: 1 };
        f.render_widget(Paragraph::new(Line::from(spans)), btn_rect);
    }

    // Overlay: centered delete confirmation popup with colored text + styled buttons
    if app.is_confirm_delete() {
        let popup = compute_delete_popup_rect(app, area).unwrap();
        let border_style = Style::default().fg(Color::Red);
        let title_line =
            Line::from(Span::styled(" Confirm ", border_style.add_modifier(Modifier::BOLD)));
        let block =
            Block::default().borders(Borders::ALL).title(title_line).border_style(border_style);
        f.render_widget(Clear, popup);
        f.render_widget(block.clone(), popup);
        let inner_popup = block.inner(popup);
        // First inner line: red message
        let title = app.day.tasks.get(app.selected_index()).map(|t| t.title.as_str()).unwrap_or("");
        let msg = format!("Delete? — {}  (Enter=Delete Esc=Cancel)", title);
        let msg_para = Paragraph::new(Span::styled(msg, Style::default().fg(Color::Red)));
        let msg_rect =
            Rect { x: inner_popup.x, y: inner_popup.y, width: inner_popup.width, height: 1 };
        f.render_widget(msg_para, msg_rect);
        // Second inner line: pill-styled buttons aligned with hitboxes
        let (del_rect, cancel_rect) = delete_popup_button_hitboxes(app, popup);
        let btn_y = del_rect.y;
        let mut spans: Vec<Span> = Vec::new();
        let pad_del = (del_rect.x.saturating_sub(inner_popup.x)) as usize;
        if pad_del > 0 {
            spans.push(Span::raw(" ".repeat(pad_del)));
        }
        let del_style = if matches!(app.popup_hover_button(), Some(crate::app::PopupButton::Delete))
        {
            Style::default().fg(Color::Black).bg(Color::Cyan).add_modifier(Modifier::BOLD)
        } else {
            Style::default().fg(Color::Black).bg(Color::Red).add_modifier(Modifier::BOLD)
        };
        spans.push(Span::styled("Delete".to_string(), del_style));
        let gap = cancel_rect.x.saturating_sub(del_rect.x + del_rect.width) as usize;
        if gap > 0 {
            spans.push(Span::raw(" ".repeat(gap)));
        }
        let can_style = if matches!(app.popup_hover_button(), Some(crate::app::PopupButton::Cancel))
        {
            Style::default().fg(Color::Black).bg(Color::Cyan).add_modifier(Modifier::BOLD)
        } else {
            Style::default().fg(Color::Black).bg(Color::Gray).add_modifier(Modifier::BOLD)
        };
        spans.push(Span::styled("Cancel".to_string(), can_style));
        let btn_line = Paragraph::new(Line::from(spans));
        let btn_rect = Rect { x: inner_popup.x, y: btn_y, width: inner_popup.width, height: 1 };
        f.render_widget(btn_line, btn_rect);
    }

    // Overlay: category picker
    if let Some(popup) = compute_category_popup_rect(app, area) {
        // Title
        let header_line = header_title_line(app_display_base(app), app);
        let block = Block::default().title(header_line).borders(Borders::ALL);
        f.render_widget(block, area);
        // Inner box for list
        let inner = Rect { x: popup.x, y: popup.y, width: popup.width, height: popup.height };
        let mut rows: Vec<Row> = Vec::new();
        let options = category_options(app);
        for (i, (label, color, _cat)) in options.iter().enumerate() {
            let bullet = Span::styled("".to_string(), Style::default().fg(*color));
            let text = Span::styled(
                label.clone(),
                Style::default().fg(*color).add_modifier(Modifier::BOLD),
            );
            let line = Line::from(vec![bullet, Span::raw(" "), text]);
            let mut row = Row::new(vec![Cell::from(line)]);
            if app.category_pick_index() == i {
                row = row.style(Style::default().bg(Color::Blue));
            }
            rows.push(row);
        }
        let table = Table::new(rows, [Constraint::Min(10)])
            .header(
                Row::new(vec![Cell::from("Category")])
                    .style(Style::default().fg(Color::Yellow).add_modifier(Modifier::BOLD)),
            )
            .block(Block::default().borders(Borders::ALL).title("Select Category (Enter)"));
        f.render_widget(table, inner);
    }
}

/// Like `draw`, but uses an injected `Clock` for current time.
pub fn draw_with_clock(f: &mut Frame, app: &App, clock: &dyn Clock) {
    let area: Rect = f.area();
    let now = clock.now_minutes();
    let header_line = header_title_line(now, app);
    let block = Block::default().title(header_line).borders(Borders::ALL);
    let inner = block.inner(area);
    f.render_widget(block, area);

    // Keep layout consistent with `draw`: tabs, optional banner, content, help block
    let active_banner = format_active_banner(app);
    let help_lines = help_lines_for_width(app, inner.width.max(1));
    // Clamp help height so the task table keeps at least header + two rows visible
    let mut help_height = help_lines.len() as u16;
    let reserved = 1 /* tabs */ + if active_banner.is_some() { 1 } else { 0 } + MIN_LIST_LINES;
    let max_help = inner.height.saturating_sub(reserved);
    if max_help > 0 {
        help_height = help_height.min(max_help);
    }
    help_height = if max_help >= 3 {
        help_height.max(3)
    } else if max_help >= 2 {
        help_height.max(2)
    } else {
        help_height.max(1)
    };
    let mut constraints: Vec<Constraint> = vec![Constraint::Length(1)];
    if active_banner.is_some() {
        constraints.push(Constraint::Length(1));
    }
    constraints.push(Constraint::Min(0));
    constraints.push(Constraint::Length(help_height.max(1)));
    let chunks =
        Layout::default().direction(Direction::Vertical).constraints(constraints).split(inner);

    render_tabs_line(f, chunks[0], app);

    // Optional banner under tabs
    let mut content_idx = 1usize;
    if let Some(line) = active_banner {
        let para = Paragraph::new(line.patch_style(Modifier::UNDERLINED));
        f.render_widget(para, chunks[1]);
        content_idx = 2;
    }

    // Content with injected clock: render as a table
    let tasks_slice: Vec<crate::task::Task> = match app.view() {
        View::Past => app.history_tasks().clone(),
        View::Today => app.day.tasks.clone(),
        View::Future => app.tomorrow_tasks().clone(),
    };
    if tasks_slice.is_empty() {
        let para = Paragraph::new("No tasks — press 'i' to add");
        if chunks.len() > content_idx && chunks[content_idx].height > 0 {
            f.render_widget(para, chunks[content_idx]);
        }
    } else if chunks.len() > content_idx && chunks[content_idx].height > 0 {
        match app.display_mode() {
            DisplayMode::List => {
                let table = build_task_table(now, app, &tasks_slice);
                f.render_widget(table, chunks[content_idx]);
            }
            DisplayMode::Calendar => {
                render_calendar_day_at(
                    f,
                    chunks[content_idx],
                    app,
                    &tasks_slice,
                    clock.now_minutes(),
                );
            }
        }
    }

    // Help block with gauge on top under injected clock
    let help_idx = chunks.len().saturating_sub(1);
    if chunks[help_idx].height > 0 {
        let help_area = chunks[help_idx];
        match help_area.height {
            0 => {}
            1 => {
                let help_text = help_lines.join("\n");
                let help = Paragraph::new(help_text)
                    .style(Style::default().fg(Color::DarkGray))
                    .wrap(Wrap { trim: true });
                f.render_widget(help, help_area);
            }
            2 => {
                let gauge_area =
                    Rect { x: help_area.x, y: help_area.y, width: help_area.width, height: 1 };
                render_bottom_24h_gauge(f, app, gauge_area, clock.now_minutes());
                let help_text_area =
                    Rect { x: help_area.x, y: help_area.y + 1, width: help_area.width, height: 1 };
                let help_text = help_lines.join("\n");
                let help = Paragraph::new(help_text)
                    .style(Style::default().fg(Color::DarkGray))
                    .wrap(Wrap { trim: true });
                f.render_widget(help, help_text_area);
            }
            _ => {
                let gauge_area =
                    Rect { x: help_area.x, y: help_area.y, width: help_area.width, height: 1 };
                render_bottom_24h_gauge(f, app, gauge_area, clock.now_minutes());
                let label_rect =
                    Rect { x: help_area.x, y: help_area.y + 1, width: help_area.width, height: 1 };
                render_gauge_labels(f, label_rect);
                let help_text_area = Rect {
                    x: help_area.x,
                    y: help_area.y + 2,
                    width: help_area.width,
                    height: help_area.height.saturating_sub(2),
                };
                if help_text_area.height > 0 {
                    let help_text = help_lines.join("\n");
                    let help = Paragraph::new(help_text)
                        .style(Style::default().fg(Color::DarkGray))
                        .wrap(Wrap { trim: true });
                    f.render_widget(help, help_text_area);
                }
            }
        }
    }

    // Overlay: centered delete confirmation popup with colored text
    if app.is_confirm_delete() {
        let title = app.day.tasks.get(app.selected_index()).map(|t| t.title.as_str()).unwrap_or("");
        let msg = format!("Delete? — {}  (Enter=Delete Esc=Cancel)", title);
        let content_w = UnicodeWidthStr::width(msg.as_str()) as u16;
        let popup_w = content_w.saturating_add(4).min(inner.width).max(20).min(inner.width);
        let popup_h: u16 = 3;
        let px = inner.x + (inner.width.saturating_sub(popup_w)) / 2;
        let py = inner.y + (inner.height.saturating_sub(popup_h)) / 2;
        let popup = Rect { x: px, y: py, width: popup_w, height: popup_h };

        let border_style = Style::default().fg(Color::Red);
        let title =
            Line::from(Span::styled(" Confirm ", border_style.add_modifier(Modifier::BOLD)));
        let block = Block::default().borders(Borders::ALL).title(title).border_style(border_style);
        f.render_widget(Clear, popup);
        f.render_widget(block.clone(), popup);
        let inner_popup = block.inner(popup);
        let para = Paragraph::new(Span::styled(msg.clone(), Style::default().fg(Color::Red)));
        f.render_widget(para, inner_popup);
    }
    // Overlay: Start Time popup under injected clock path as well
    if let Some(popup) = compute_start_time_popup_rect(app, area) {
        let border = Style::default().fg(Color::Cyan);
        let title_line =
            Line::from(Span::styled(" Start Time ", border.add_modifier(Modifier::BOLD)));
        let block = Block::default().borders(Borders::ALL).title(title_line).border_style(border);
        f.render_widget(Clear, popup);
        f.render_widget(block.clone(), popup);
        let inner = block.inner(popup);
        let mins =
            app.input_buffer().and_then(|s| s.parse::<u16>().ok()).unwrap_or(app_display_base(app));
        let hh = (mins / 60) % 24;
        let mm = mins % 60;
        let title = app.day.tasks.get(app.selected_index()).map(|t| t.title.as_str()).unwrap_or("");
        let msg = if title.is_empty() {
            format!("Start: {:02}:{:02}", hh, mm)
        } else {
            format!("Start: {:02}:{:02}{}", hh, mm, title)
        };
        let msg_rect = Rect { x: inner.x, y: inner.y, width: inner.width, height: 1 };
        f.render_widget(Paragraph::new(Span::styled(msg, border)), msg_rect);
        let (track, ok, cancel) = estimate_slider_hitboxes(app, popup);
        render_time_slider_line(f, track, mins);
        let btn_y = ok.y;
        let mut spans: Vec<Span> = Vec::new();
        let pad = (ok.x.saturating_sub(inner.x)) as usize;
        if pad > 0 {
            spans.push(Span::raw(" ".repeat(pad)));
        }
        let ok_style = if matches!(app.popup_hover_button(), Some(crate::app::PopupButton::EstOk)) {
            Style::default().fg(Color::Black).bg(Color::Cyan).add_modifier(Modifier::BOLD)
        } else {
            Style::default().fg(Color::Black).bg(Color::Blue).add_modifier(Modifier::BOLD)
        };
        spans.push(Span::styled("OK".to_string(), ok_style));
        let gap2 = cancel.x.saturating_sub(ok.x + ok.width) as usize;
        if gap2 > 0 {
            spans.push(Span::raw(" ".repeat(gap2)));
        }
        let cancel_style =
            if matches!(app.popup_hover_button(), Some(crate::app::PopupButton::EstCancel)) {
                Style::default().fg(Color::Black).bg(Color::Cyan).add_modifier(Modifier::BOLD)
            } else {
                Style::default().fg(Color::Black).bg(Color::Gray).add_modifier(Modifier::BOLD)
            };
        spans.push(Span::styled("Cancel".to_string(), cancel_style));
        let btn_rect = Rect { x: inner.x, y: btn_y, width: inner.width, height: 1 };
        f.render_widget(Paragraph::new(Line::from(spans)), btn_rect);
    }
}

// Tab metadata for the date views (Past/Today/Future).
// Returned as (titles, selected_index) to keep rendering logic decoupled for testing.
pub fn tab_titles(app: &App) -> (Vec<String>, usize) {
    let titles = vec!["Past".to_string(), "Today".to_string(), "Future".to_string()];
    let selected = match app.view() {
        View::Past => 0,
        View::Today => 1,
        View::Future => 2,
    };
    (titles, selected)
}

pub fn format_task_lines(app: &App) -> Vec<String> {
    format_task_lines_at(app_display_base(app), app)
}

// Deterministic variant for tests: inject current minutes since midnight.
pub fn format_task_lines_at(now_min: u16, app: &App) -> Vec<String> {
    // For estimate edit/new-task estimate/title input/delete confirm, keep main list lines
    match app.view() {
        View::Past => render_list_slice(now_min, app, app.history_tasks()),
        View::Today => render_list_slice(now_min, app, &app.day.tasks),
        View::Future => render_list_slice(now_min, app, app.tomorrow_tasks()),
    }
}

/// Compute planned start minutes for each task, respecting per-task fixed start times.
/// Algorithm:
/// - `cursor` starts at `now_min` (typically day_start)
/// - For each task in order: if it has `fixed_start_min`, set `cursor = max(cursor, fixed)`.
///   The task's start is `cursor`; then advance `cursor += estimate_min`.
fn compute_planned_starts(now_min: u16, tasks: &[crate::task::Task]) -> Vec<u16> {
    let mut cursor = now_min;
    let mut out: Vec<u16> = Vec::with_capacity(tasks.len());
    for t in tasks.iter() {
        if let Some(fs) = t.fixed_start_min {
            cursor = cursor.max(fs);
        }
        out.push(cursor);
        cursor = cursor.saturating_add(t.estimate_min);
    }
    out
}

fn render_list_slice(now_min: u16, app: &App, tasks: &[crate::task::Task]) -> Vec<String> {
    if tasks.is_empty() {
        return vec!["No tasks — press 'i' to add".to_string()];
    }
    // active index not needed for seconds rendering anymore (per-task seconds)

    // Build schedule start times considering per-task fixed start time.
    let starts: Vec<u16> = compute_planned_starts(now_min, tasks);

    tasks
        .iter()
        .enumerate()
        .map(|(i, t)| {
            let sel = if i == app.selected_index() { "" } else { " " };
            let secs = match t.state {
                TaskState::Active | TaskState::Paused => t.actual_carry_sec,
                _ => 0,
            };
            let hh = (starts[i] / 60) % 24;
            let mm = starts[i] % 60;
            let planned = format!(
                "{:02}:{:02} {} {} {} (est:{}m act:{}m {}s)",
                hh,
                mm,
                sel,
                state_icon(t.state),
                t.title,
                t.estimate_min,
                t.actual_min,
                secs
            );
            // Actual start/end column
            let act_col = match (t.started_at_min, t.finished_at_min) {
                (Some(s), Some(e)) => format!(
                    "実測 {:02}:{:02}-{:02}:{:02}",
                    (s / 60) % 24,
                    s % 60,
                    (e / 60) % 24,
                    e % 60
                ),
                (Some(s), None) => {
                    format!("実測 {:02}:{:02}-", (s / 60) % 24, s % 60)
                }
                _ => "実測 --:--".to_string(),
            };
            format!("{}  |  {}", planned, act_col)
        })
        .collect()
}

fn format_actual_last_finish_time(t: &crate::task::Task) -> String {
    // Prefer the explicit finished_at_min if present (final finish)
    if let Some(e) = t.finished_at_min {
        return format!("{:02}:{:02}", (e / 60) % 24, e % 60);
    }
    // Otherwise, find the most recent session with an end time
    if let Some(e) = t.sessions.iter().rev().find_map(|s| s.end_min) {
        return format!("{:02}:{:02}", (e / 60) % 24, e % 60);
    }
    // No finished session yet
    "--:--".to_string()
}

fn build_task_table(now_min: u16, app: &App, tasks_slice: &[crate::task::Task]) -> Table<'static> {
    // If empty, show the hint paragraph to save space
    let mut rows: Vec<Row> = Vec::new();
    // Build schedule start times similar to `render_list_slice`
    let starts: Vec<u16> = compute_planned_starts(now_min, tasks_slice);

    let selected = app.selected_index().min(tasks_slice.len().saturating_sub(1));
    let hovered = app.hovered_index();
    let dragging = app.is_dragging();
    let drag_from = app.drag_source_index();
    let pulse_on = app.pulse_on();
    for (i, t) in tasks_slice.iter().enumerate() {
        let hh = (starts[i] / 60) % 24;
        let mm = starts[i] % 60;
        let mut planned_cell = Cell::from(format!("{:02}:{:02}", hh, mm));
        if t.fixed_start_min.is_some() {
            planned_cell = planned_cell.style(Style::default().fg(Color::Cyan));
        }
        let actual_cell = Cell::from(format_actual_last_finish_time(t));
        // Title cell with colored state icon and plain title (estimate is a dedicated column)
        let mut spans: Vec<Span> = Vec::new();
        // Drag target indicator arrow before icon (only while dragging over this row)
        if dragging && hovered == Some(i) {
            let arrow = match drag_from {
                Some(from) if from < i => "",
                Some(from) if from > i => "",
                _ => "",
            };
            let arrow_style = Style::default().fg(Color::White).add_modifier(Modifier::BOLD);
            spans.push(Span::styled(arrow.to_string(), arrow_style));
            spans.push(Span::raw(" "));
        }
        spans.push(state_icon_span(t.state));
        spans.push(Span::raw(" "));
        // Category colored dot (configurable)
        let cat_color = app.config.category_color(t.category);
        spans.push(Span::styled("".to_string(), Style::default().fg(cat_color)));
        spans.push(Span::raw(" "));
        // Title color: by category; Done overrides to gray. Keep strikethrough for Done.
        let title_fg = if matches!(t.state, TaskState::Done) { Color::DarkGray } else { cat_color };
        let mut title_style = if matches!(t.state, TaskState::Done) {
            Style::default().add_modifier(Modifier::CROSSED_OUT)
        } else {
            Style::default()
        };
        title_style = title_style.fg(title_fg);
        spans.push(Span::styled(t.title.clone(), title_style));
        let title_cell = Cell::from(Line::from(spans));
        // New dedicated estimate column
        let est_cell = Cell::from(format!("{}m", t.estimate_min));
        // New dedicated accumulated time column with seconds
        // Show seconds while Active or Paused
        let secs = if matches!(t.state, TaskState::Active | TaskState::Paused) {
            t.actual_carry_sec
        } else {
            0
        };
        let act_cell = Cell::from(format!("{}m {}s", t.actual_min, secs));
        let highlight_bg = if dragging {
            if Some(i) == drag_from {
                Some(if pulse_on { DRAG_SOURCE_BG_B } else { DRAG_SOURCE_BG_A })
            } else if hovered == Some(i) {
                Some(if pulse_on { DRAG_TARGET_BG_B } else { DRAG_TARGET_BG_A })
            } else if i == selected {
                Some(SELECTED_ROW_BG)
            } else {
                None
            }
        } else if i == selected {
            Some(SELECTED_ROW_BG)
        } else if hovered == Some(i) {
            Some(HOVER_ROW_BG)
        } else {
            None
        };
        // Column order: Plan | Est | Task | Act | Actual
        let mut row = Row::new(vec![planned_cell, est_cell, title_cell, act_cell, actual_cell]);
        if let Some(bg) = highlight_bg {
            let s = Style::default().bg(bg);
            row = row.style(s);
        }
        rows.push(row);
    }

    // Header
    // Header labels follow the same order: Plan | Est | Task | Act | Actual
    let header = Row::new(vec![
        Cell::from("Plan"),
        Cell::from("Est"),
        Cell::from("Task"),
        Cell::from("Act"),
        Cell::from("Actual"),
    ])
    .style(Style::default().fg(Color::Yellow).add_modifier(Modifier::BOLD));

    // Column widths: Plan fixed 5, Est fits e.g. "120m" (4), Task grows, Act fits e.g. "120m 59s" (9), Actual label width (6)
    let widths = [
        Constraint::Length(5), // Plan
        Constraint::Length(4), // Est
        Constraint::Min(10),   // Task
        Constraint::Length(9), // Act
        Constraint::Length(6), // Actual
    ];

    // Render table using a minimal block to avoid nested borders (outer block already drawn)
    Table::new(rows, widths).header(header).column_spacing(1).block(Block::default())
}

fn state_icon(state: TaskState) -> &'static str {
    match state {
        TaskState::Planned => " ",
        TaskState::Active => ">",
        TaskState::Paused => "=",
        TaskState::Done => "x",
    }
}

fn state_icon_span(state: TaskState) -> Span<'static> {
    let sym = state_icon(state).to_string();
    let style = match state {
        TaskState::Active => Style::default().fg(Color::Green),
        TaskState::Paused => Style::default().fg(Color::Yellow),
        TaskState::Done => Style::default().fg(Color::DarkGray),
        TaskState::Planned => Style::default(),
    };
    Span::styled(sym, style)
}

/// Colorful, lazygit-inspired title line for the outer `Block`.
/// This keeps `format_header_line` unchanged for tests that rely on plain text,
/// while rendering a more readable, colorful header in the UI.
pub fn header_title_line(now_min: u16, app: &App) -> Line<'static> {
    // Reuse the same numbers as `format_header_line` to stay consistent.
    let esd_min = app.day.esd(now_min);
    let esd_h = esd_min / 60;
    let esd_m = esd_min % 60;

    let total_est_min: u32 = app.day.tasks.iter().map(|t| t.estimate_min as u32).sum();
    let total_act_min: u32 = app.day.tasks.iter().map(|t| t.actual_min as u32).sum();
    let carry_sec: u32 = app.day.tasks.iter().map(|t| t.actual_carry_sec as u32).sum();
    let total_act_sec = total_act_min * 60 + carry_sec;
    let rem_total_sec = (total_est_min * 60).saturating_sub(total_act_sec);
    let rem_m = (rem_total_sec / 60) as u16;
    let rem_s = (rem_total_sec % 60) as u16;
    let act_m = (total_act_sec / 60) as u16;
    let act_s = (total_act_sec % 60) as u16;

    // Colors: pills with bright BGs and bold text, separators in dim gray.
    let sep_style = Style::default().fg(Color::DarkGray);

    let pill = |label: &str, bg: Color| -> Span {
        Span::styled(
            label.to_string(),
            Style::default().fg(Color::Black).bg(bg).add_modifier(Modifier::BOLD),
        )
    };
    let val = |text: String, fg: Color| -> Span {
        Span::styled(text, Style::default().fg(fg).add_modifier(Modifier::BOLD))
    };

    let mut line: Line<'static> = Line::default();
    // ESD
    line.spans.push(pill("ESD", Color::Blue));
    line.spans.push(Span::raw(" "));
    line.spans.push(val(format!("{:02}:{:02}", esd_h, esd_m), Color::Cyan));
    line.spans.push(Span::styled("  |  ", sep_style));
    // Est remaining
    line.spans.push(pill("Est", Color::Green));
    line.spans.push(Span::raw(" "));
    line.spans.push(val(format!("{}m {}s", rem_m, rem_s), Color::Green));
    line.spans.push(Span::styled("  |  ", sep_style));
    // Actual total
    line.spans.push(pill("Act", Color::Magenta));
    line.spans.push(Span::raw(" "));
    line.spans.push(val(format!("{}m {}s", act_m, act_s), Color::Magenta));

    line
}

// ---- Category picker UI helpers ----

pub fn category_options(app: &App) -> Vec<(String, Color, crate::task::Category)> {
    vec![
        (
            app.config.category_name(crate::task::Category::General),
            app.config.category_color(crate::task::Category::General),
            crate::task::Category::General,
        ),
        (
            app.config.category_name(crate::task::Category::Work),
            app.config.category_color(crate::task::Category::Work),
            crate::task::Category::Work,
        ),
        (
            app.config.category_name(crate::task::Category::Home),
            app.config.category_color(crate::task::Category::Home),
            crate::task::Category::Home,
        ),
        (
            app.config.category_name(crate::task::Category::Hobby),
            app.config.category_color(crate::task::Category::Hobby),
            crate::task::Category::Hobby,
        ),
    ]
}

pub fn compute_category_popup_rect(app: &App, area: Rect) -> Option<Rect> {
    if !app.is_category_picker() {
        return None;
    }
    let width: u16 = 24;
    let height: u16 = 1 /* header */ + category_options(app).len() as u16 + 2; // box padding
    let x = area.x + (area.width.saturating_sub(width)) / 2;
    let y = area.y + (area.height.saturating_sub(height)) / 2;
    Some(Rect { x, y, width, height })
}

pub fn category_picker_hitboxes(app: &App, popup: Rect) -> Vec<Rect> {
    let list_y = popup.y + 2; // leave a small margin
    let mut rects = Vec::new();
    for i in 0..category_options(app).len() as u16 {
        rects.push(Rect {
            x: popup.x + 1,
            y: list_y + i,
            width: popup.width.saturating_sub(2),
            height: 1,
        });
    }
    rects
}

/// Right-aligned action buttons for the title bar: New | Start | Stop | Finish | Delete.
/// Buttons render as bold, black-on-colored "pills" similar to other UI elements.
pub fn header_action_buttons_line(app: &App) -> Line<'static> {
    let hovered = app.hovered_header_button();
    let labels = header_action_button_labels();
    let enabled = header_action_button_enabled(app);
    let colors = [Color::Green, Color::Blue, Color::Yellow, Color::Magenta, Color::Red];
    let mut spans: Vec<Span> = Vec::new();
    for i in 0..labels.len() {
        let label = &labels[i];
        let is_enabled = enabled[i];
        let is_hover = matches!(
            (i, hovered),
            (0, Some(crate::app::HeaderButton::New))
                | (1, Some(crate::app::HeaderButton::Start))
                | (2, Some(crate::app::HeaderButton::Stop))
                | (3, Some(crate::app::HeaderButton::Finish))
                | (4, Some(crate::app::HeaderButton::Delete))
        );
        let style = if is_enabled {
            let bg = if is_hover { Color::Cyan } else { colors[i] };
            Style::default().fg(Color::Black).bg(bg).add_modifier(Modifier::BOLD)
        } else {
            Style::default().fg(Color::DarkGray)
        };
        spans.push(Span::styled(label.clone(), style));
        if i + 1 != labels.len() {
            spans.push(Span::raw(" "));
        }
    }
    Line::from(spans)
}

/// Compute hitboxes (terminal rectangles) for each header action button on the top border line.
/// Returns boxes in the same order as rendering: [New, Start, Stop, Finish, Delete].
/// Coordinates are relative to the full `area` passed to the app draw loop.
pub fn header_action_buttons_hitboxes(area: Rect) -> Vec<Rect> {
    // Available width for titles excludes the two corner cells.
    let available = area.width.saturating_sub(2);
    let labels = header_action_button_labels(); // include shortcut hints
    let gaps = 4u16; // 4 spaces between 5 labels
    let labels_w: u16 = labels.iter().map(|s| UnicodeWidthStr::width(s.as_str()) as u16).sum();
    let total_w = labels_w + gaps;
    // Start X for the right-aligned group: left corner + (available - total)
    let start_x = area.x + 1 + available.saturating_sub(total_w);
    let mut xs = start_x;
    let mut rects: Vec<Rect> = Vec::with_capacity(labels.len());
    for (i, s) in labels.iter().enumerate() {
        let w = UnicodeWidthStr::width(s.as_str()) as u16;
        rects.push(Rect { x: xs, y: area.y, width: w.max(1), height: 1 });
        xs = xs.saturating_add(w);
        if i + 1 != labels.len() {
            xs = xs.saturating_add(1); // gap space
        }
    }
    rects
}

/// Labels (with keyboard shortcut hints) used for header buttons in both render and hitboxes.
pub fn header_action_button_labels() -> Vec<String> {
    // Keep labels short to avoid overlapping the left stats header on narrow terminals.
    // Keyboard shortcuts remain documented in the help line.
    vec![
        "New".to_string(),
        "Start".to_string(),
        "Stop".to_string(),
        "Finish".to_string(),
        "Delete".to_string(),
    ]
}

/// Enabled state for each header button (New, Start, Stop, Finish, Delete).
pub fn header_action_button_enabled(app: &App) -> [bool; 5] {
    use crate::app::View;
    let on_today = matches!(app.view(), View::Today);
    let has_today = !app.day.tasks.is_empty();
    let selected_eligible = on_today
        && app
            .day
            .tasks
            .get(app.selected_index())
            .map(|t| {
                matches!(t.state, crate::task::TaskState::Paused | crate::task::TaskState::Planned)
            })
            .unwrap_or(false);
    let has_active = on_today && app.day.active_index().is_some();
    let can_finish = on_today && has_today;
    let can_delete = on_today && has_today;
    [true, selected_eligible, has_active, can_finish, can_delete]
}

pub fn format_header_line(now_min: u16, app: &App) -> String {
    let _remaining = app.day.remaining_total_min();
    let esd_min = app.day.esd(now_min);
    let esd_h = esd_min / 60;
    let esd_m = esd_min % 60;

    let total_est_min: u32 = app.day.tasks.iter().map(|t| t.estimate_min as u32).sum();
    let total_act_min: u32 = app.day.tasks.iter().map(|t| t.actual_min as u32).sum();
    // 正攻法: 全タスクの部分秒を合算
    let carry_sec: u32 = app.day.tasks.iter().map(|t| t.actual_carry_sec as u32).sum();

    let total_act_sec = total_act_min * 60 + carry_sec;
    let rem_total_sec = (total_est_min * 60).saturating_sub(total_act_sec);

    let rem_m = (rem_total_sec / 60) as u16;
    let rem_s = (rem_total_sec % 60) as u16;
    let act_m = (total_act_sec / 60) as u16;
    let act_s = (total_act_sec % 60) as u16;

    format!("ESD {:02}:{:02} | Est {}m {}s | Act {}m {}s", esd_h, esd_m, rem_m, rem_s, act_m, act_s)
}

// Local time retrieval moved to `crate::clock`.

/// Generic keyboard help string (superset). Used by tests and as fallback.
pub fn format_help_line() -> String {
    // Keep wording substrings aligned with tests
    // - quitting / navigation
    let nav = "q: quit | Tab: switch view";
    // - task lifecycle and operations (Today view only in optimized variant)
    let task =
        "Enter: start/pause | Shift+Enter/f: finish | Space: time | i: interrupt | p: postpone | x: delete | b: bring | [: up | ]: down | e: edit | j/k";
    format!("{} | {}", nav, task)
}

/// Optimized help depending on the current view.
/// - Today: show full task actions
/// - Past/Future: show only navigation and quit to reduce noise
pub fn format_help_line_for(app: &App) -> String {
    // Build items using the same source as wrapped help
    let items = help_items_for(app);
    items.join(" | ")
}

/// Build help items depending on the current view. Used for wrapping.
pub fn help_items_for(app: &App) -> Vec<String> {
    use crate::config::join_key_labels as join;
    // Popup‑scoped help: when a popup is open, restrict to its operations only.
    if app.is_confirm_delete() {
        return vec!["Enter/y: delete".to_string(), "Esc/n: cancel".to_string()];
    }
    if app.is_start_time_edit() {
        return vec![
            "Enter: OK".to_string(),
            "Esc: cancel".to_string(),
            "Left/Right/Up/Down/j/k: +/-5m".to_string(),
            "click slider: set time".to_string(),
        ];
    }
    if app.is_estimate_editing() {
        return vec![
            "Enter: OK".to_string(),
            "Esc: cancel".to_string(),
            "Left/Right/Up/Down/j/k: +/-5m".to_string(),
            ".,: +/-1 day".to_string(),
            "click slider: set estimate".to_string(),
            "click < >: date".to_string(),
        ];
    }
    if app.is_new_task_estimate() {
        return vec![
            "Enter: add".to_string(),
            "Esc: cancel".to_string(),
            ".,: +/-1 day".to_string(),
            "click slider: set estimate".to_string(),
            "click < >: date".to_string(),
        ];
    }
    if app.is_command_mode() {
        return vec![
            "Enter: run".to_string(),
            "Esc: cancel".to_string(),
            "type/backspace: edit".to_string(),
        ];
    }
    if app.is_text_input_mode() {
        return vec![
            "Enter: next".to_string(),
            "Esc: cancel".to_string(),
            "type/backspace: edit".to_string(),
        ];
    }

    // Default (no popup): view‑aware general help
    let km = &app.config.keys;
    let mut items: Vec<String> =
        vec![format!("{}: quit", join(&km.quit)), format!("{}: switch view", join(&km.view_next))];
    match app.view() {
        View::Today => {
            items.push(format!("{}: start/pause", join(&km.start_or_resume)));
            items.push(format!("{}: time", join(&km.popup)));
            items.push(format!("{}: finish", join(&km.finish_active)));
            // Interrupt: reflect configured keys
            items.push(format!("{}: interrupt", join(&km.add_interrupt)));
            items.push(format!("{}: postpone", join(&km.postpone)));
            // delete key now configurable
            items.push(format!("{}: delete", join(&km.delete)));
            items.push(format!("{}: up", join(&km.reorder_up)));
            items.push(format!("{}: down", join(&km.reorder_down)));
            items.push(format!("{}: edit", join(&km.estimate_plus)));
            // Toggle display mode (List <-> Calendar)
            items.push(format!("{}: calendar", join(&km.toggle_blocks)));
            // Category cycle (configurable)
            items.push(format!("{}: category", join(&km.category_cycle)));
            // Category picker open (configurable)
            items.push(format!("{}: picker", join(&km.category_picker)));
            // Compact vim-like navigation chars as trailing hint (if present)
            let up_chars: Vec<char> = km
                .select_up
                .iter()
                .filter_map(|k| match k.code {
                    crossterm::event::KeyCode::Char(c) if k.modifiers.is_empty() => Some(c),
                    _ => None,
                })
                .collect();
            let down_chars: Vec<char> = km
                .select_down
                .iter()
                .filter_map(|k| match k.code {
                    crossterm::event::KeyCode::Char(c) if k.modifiers.is_empty() => Some(c),
                    _ => None,
                })
                .collect();
            if let (Some(d), Some(u)) = (down_chars.first(), up_chars.first()) {
                items.push(format!("{}/{}", d, u));
            } else {
                items.push("j/k".to_string());
            }
        }
        View::Past => {
            // Minimal + category operations now available
            items.push(format!("{}: category", join(&km.category_cycle)));
            items.push(format!("{}: picker", join(&km.category_picker)));
        }
        View::Future => {
            items.push(format!("{}: bring", join(&km.bring_to_today)));
            items.push(format!("{}: category", join(&km.category_cycle)));
            items.push(format!("{}: picker", join(&km.category_picker)));
        }
    }
    items
}

/// Wrap help items into lines that fit within `width` cells, inserting ` | ` between items.
/// This uses Unicode width to count display cells.
pub fn wrap_help_items_to_width(items: &[String], width: u16) -> Vec<String> {
    let width = width as usize;
    if width == 0 {
        return vec![String::new()];
    }
    let mut lines: Vec<String> = Vec::new();
    let mut cur = String::new();
    let sep = " | ";
    for item in items.iter() {
        if cur.is_empty() {
            cur.push_str(item.as_str());
            continue;
        }
        let candidate = format!("{}{}{}", cur, sep, item);
        if UnicodeWidthStr::width(candidate.as_str()) <= width {
            cur = candidate;
        } else {
            // commit current line and start a new one
            lines.push(cur);
            cur = item.to_string();
        }
    }
    if !cur.is_empty() {
        lines.push(cur);
    }
    lines
}

/// Convenience: get wrapped help lines for current app state and width.
pub fn help_lines_for_width(app: &App, width: u16) -> Vec<String> {
    let items = help_items_for(app);
    wrap_help_items_to_width(&items, width)
}

fn app_display_base(app: &App) -> u16 {
    app.config.day_start_minutes
}

/// Build a one-line banner describing the currently running task, if any.
/// Example: "Now: > Focus Work (est:30m act:3m 12s)"
pub fn format_active_banner(app: &App) -> Option<Line<'static>> {
    let idx = app.day.active_index()?;
    let t = &app.day.tasks[idx];
    let mut line = Line::default();
    line.spans.push(Span::styled(
        "Now:".to_string(),
        Style::default().fg(Color::Yellow).add_modifier(Modifier::BOLD),
    ));
    line.spans.push(Span::raw(" "));
    line.spans.push(state_icon_span(t.state));
    line.spans.push(Span::raw(" "));
    // Running task title (no underline here; banner is underlined as a whole)
    line.spans.push(Span::styled(t.title.clone(), Style::default().fg(Color::Cyan)));
    line.spans.push(Span::raw(format!(
        " (est:{}m act:{}m {}s)",
        t.estimate_min, t.actual_min, t.actual_carry_sec
    )));
    Some(line)
}

fn render_tabs_line(f: &mut Frame, rect: Rect, app: &App) {
    let (titles, selected) = tab_titles(app);
    let hover = app.hovered_tab_index();
    let mut line = Line::default();
    for (i, title) in titles.iter().enumerate() {
        let mut style = Style::default();
        if Some(i) == hover && Some(i) != Some(selected) {
            style = style.fg(Color::Cyan);
        }
        if i == selected {
            style = style.fg(Color::Yellow).add_modifier(Modifier::BOLD);
        }
        line.spans.push(Span::styled(title.clone(), style));
        if i + 1 != titles.len() {
            line.spans.push(Span::styled("".to_string(), Style::default().fg(Color::DarkGray)));
        }
    }
    let para = Paragraph::new(line);
    f.render_widget(para, rect);
}

/// Render a one-line 24h horizontal gauge showing actual work sessions by category.
/// - Range is fixed to 00:00..24:00 mapped across `rect.width` cells.
/// - Colored segments reflect task categories; overlapping sessions pick the last seen.
/// - Ongoing session uses `now_min` as its temporary end.
fn render_bottom_24h_gauge(f: &mut Frame, app: &App, rect: Rect, now_min: u16) {
    if rect.width == 0 || rect.height == 0 {
        return;
    }
    let w = rect.width as usize;
    // Prepare per-cell colors and glyphs; default None means no work recorded
    let mut cells: Vec<Option<Color>> = vec![None; w];
    let mut glyphs: Vec<char> = vec!['·'; w];
    // Gather all actual sessions for the current view's tasks
    let tasks_slice: Vec<crate::task::Task> = match app.view() {
        View::Past => app.history_tasks().clone(),
        View::Today => app.day.tasks.clone(),
        View::Future => app.tomorrow_tasks().clone(),
    };
    for t in tasks_slice.iter() {
        let cat_color = app.config.category_color(t.category);
        for s in t.sessions.iter() {
            let s_min = s.start_min.min(23 * 60 + 59);
            let e_min = s.end_min.unwrap_or(now_min).min(23 * 60 + 59);
            if e_min < s_min {
                continue;
            }
            let x0 = ((s_min as u32) * (rect.width as u32) / 1440) as usize;
            let x1 = ((e_min as u32) * (rect.width as u32) / 1440) as usize;
            let x0 = x0.min(w.saturating_sub(1));
            let x1 = x1.min(w.saturating_sub(1)).max(x0);
            for i in x0..=x1 {
                cells[i] = Some(cat_color);
                glyphs[i] = '';
            }
        }
    }
    // Overlay hour ticks every 60 minutes (only on empty cells to avoid hiding data)
    for h in (0..=24).map(|h| h * 60) {
        let x = ((h as u32) * (rect.width as u32) / 1440) as usize;
        if x < w && cells[x].is_none() {
            glyphs[x] = '|';
            cells[x] = Some(Color::DarkGray);
        }
    }
    // Overlay thicker ticks at 6h, 12h, 18h (always visible)
    for h in [6u16, 12u16, 18u16] {
        let x = ((h as u32 * 60) * (rect.width as u32) / 1440) as usize;
        if x < w {
            glyphs[x] = '';
            cells[x] = Some(Color::DarkGray);
        }
    }
    // Overlay current time marker '^' in red, always on top
    let x_now = ((now_min as u32) * (rect.width as u32) / 1440) as usize;
    if x_now < w {
        glyphs[x_now] = '^';
        cells[x_now] = Some(Color::Red);
    }

    // Render labels above the gauge if there is space handled by caller
    // Compress into styled spans (by (glyph, color))
    let mut spans: Vec<Span> = Vec::new();
    let mut i = 0usize;
    while i < w {
        let g = glyphs[i];
        let c = cells[i];
        let mut j = i + 1;
        while j < w && glyphs[j] == g && cells[j] == c {
            j += 1;
        }
        // MSRV 1.74: `std::iter::repeat_n` stabilized later; use `repeat(...).take(...)`.
        let text: String = std::iter::repeat(g).take(j - i).collect();
        let style = match c {
            Some(_) if g == '|' => Style::default().fg(Color::DarkGray),
            Some(color) => Style::default().fg(color).add_modifier(Modifier::BOLD),
            None => Style::default().fg(Color::DarkGray),
        };
        spans.push(Span::styled(text, style));
        i = j;
    }
    let line = Line::from(spans);
    let para = Paragraph::new(line);
    f.render_widget(para, rect);
}

/// Render labels for major ticks (6, 12, 18) on the provided one-line rect.
fn render_gauge_labels(f: &mut Frame, rect: Rect) {
    if rect.width == 0 || rect.height == 0 {
        return;
    }
    let w = rect.width as usize;
    let mut line: Vec<char> = vec![' '; w];
    let put = |line: &mut [char], x: usize, s: &str| {
        for (i, ch) in s.chars().enumerate() {
            if x + i < line.len() {
                line[x + i] = ch;
            }
        }
    };
    let map_x = |m: u16, width: u16| -> usize { ((m as u32) * (width as u32) / 1440u32) as usize };
    let x6 = map_x(6 * 60, rect.width);
    let x12 = map_x(12 * 60, rect.width);
    let x18 = map_x(18 * 60, rect.width);
    put(&mut line, x6, "6");
    put(&mut line, x12, "12");
    put(&mut line, x18, "18");
    let text: String = line.into_iter().collect();
    let para = Paragraph::new(Span::styled(text, Style::default().fg(Color::DarkGray)));
    f.render_widget(para, rect);
}

/// Compute hitboxes for tab titles inside the `tabs_rect`.
pub fn tab_hitboxes(app: &App, tabs_rect: Rect) -> Vec<Rect> {
    let (titles, _sel) = tab_titles(app);
    let mut xs = tabs_rect.x;
    let mut boxes = Vec::with_capacity(titles.len());
    for (i, t) in titles.iter().enumerate() {
        let w = UnicodeWidthStr::width(t.as_str()) as u16;
        let r = Rect { x: xs, y: tabs_rect.y, width: w.max(1), height: 1 };
        boxes.push(r);
        xs = xs.saturating_add(w);
        if i + 1 != titles.len() {
            // account for divider "│"
            xs = xs.saturating_add(1);
        }
    }
    boxes
}

pub fn compute_delete_popup_rect(app: &App, area: Rect) -> Option<Rect> {
    if !app.is_confirm_delete() {
        return None;
    }
    // Reconstruct inner same as draw
    let area: Rect = area;
    let header_line = header_title_line(app_display_base(app), app);
    let block = Block::default().title(header_line).borders(Borders::ALL);
    let inner = block.inner(area);
    // Message content identical to draw
    let title = app.day.tasks.get(app.selected_index()).map(|t| t.title.as_str()).unwrap_or("");
    let msg = format!("Delete? — {}  (Enter=Delete Esc=Cancel)", title);
    let content_w = UnicodeWidthStr::width(msg.as_str()) as u16;
    let popup_w = content_w.saturating_add(4).min(inner.width).max(20).min(inner.width);
    let popup_h: u16 = 4;
    let px = inner.x + (inner.width.saturating_sub(popup_w)) / 2;
    let py = inner.y + (inner.height.saturating_sub(popup_h)) / 2;
    Some(Rect { x: px, y: py, width: popup_w, height: popup_h })
}

pub fn delete_popup_button_hitboxes(_app: &App, popup: Rect) -> (Rect, Rect) {
    // Buttons are rendered on the second inner line, left-aligned, separated by two spaces
    let inner_popup = Rect {
        x: popup.x + 1,
        y: popup.y + 1,
        width: popup.width.saturating_sub(2),
        height: popup.height.saturating_sub(2),
    };
    let btn_y = inner_popup.y + 1;
    let del_w = UnicodeWidthStr::width("Delete") as u16;
    let can_w = UnicodeWidthStr::width("Cancel") as u16;
    let total = del_w + 2 + can_w;
    let start_x = inner_popup.x + (inner_popup.width.saturating_sub(total)) / 2;
    let del_rect = Rect { x: start_x, y: btn_y, width: del_w, height: 1 };
    let can_x = start_x + del_w + 2;
    let cancel_rect = Rect { x: can_x, y: btn_y, width: can_w, height: 1 };
    (del_rect, cancel_rect)
}

// Estimate popup
pub fn compute_estimate_popup_rect(app: &App, area: Rect) -> Option<Rect> {
    if !app.is_estimate_editing() {
        return None;
    }
    let block =
        Block::default().title(header_title_line(app_display_base(app), app)).borders(Borders::ALL);
    let inner = block.inner(area);
    let title = app.day.tasks.get(app.selected_index()).map(|t| t.title.as_str()).unwrap_or("");
    let msg = format!("Estimate: {}m — {}", app.selected_estimate().unwrap_or(0), title);
    let content_w = (UnicodeWidthStr::width(msg.as_str()).max(date_line_min_width().into()) as u16)
        .saturating_add(0);
    let popup_w = content_w.saturating_add(4).min(inner.width).max(34).min(inner.width);
    let popup_h: u16 = 6; // message + date + slider + buttons line
    let px = inner.x + (inner.width.saturating_sub(popup_w)) / 2;
    let py = inner.y + (inner.height.saturating_sub(popup_h)) / 2;
    Some(Rect { x: px, y: py, width: popup_w, height: popup_h })
}

// Slider hitboxes for estimate editor
pub fn estimate_slider_hitboxes(app: &App, popup: Rect) -> (Rect, Rect, Rect) {
    let inner = Rect {
        x: popup.x + 1,
        y: popup.y + 1,
        width: popup.width.saturating_sub(2),
        height: popup.height.saturating_sub(2),
    };
    // In estimate popups, one extra line (date) is above the slider
    let track_y = if app.is_new_task_estimate() || app.is_estimate_editing() {
        inner.y + 2
    } else {
        inner.y + 1
    };
    let track =
        Rect { x: inner.x + 2, y: track_y, width: inner.width.saturating_sub(4), height: 1 };
    let ok_w = UnicodeWidthStr::width("OK") as u16;
    let ca_w = UnicodeWidthStr::width("Cancel") as u16;
    let total = ok_w + 2 + ca_w;
    let start_x = inner.x + (inner.width.saturating_sub(total)) / 2;
    let ok = Rect { x: start_x, y: track_y + 1, width: ok_w, height: 1 };
    let cancel = Rect { x: start_x + ok_w + 2, y: track_y + 1, width: ca_w, height: 1 };
    (track, ok, cancel)
}

pub fn slider_x_for_minutes(track: Rect, min: u16, max: u16, step: u16, minutes: u16) -> u16 {
    let minutes = minutes.clamp(min, max);
    let steps = ((max - min) / step).max(1);
    let pos = ((minutes - min) / step).min(steps);
    let w = track.width.max(1) as u32;
    let x = track.x as u32 + (pos as u32 * (w - 1)) / (steps as u32);
    x as u16
}

pub fn minutes_from_slider_x(track: Rect, min: u16, max: u16, step: u16, x: u16) -> u16 {
    let w = track.width.max(1);
    let x = x.clamp(track.x, track.x + w.saturating_sub(1));
    let rel = (x - track.x) as u32;
    let steps = ((max - min) / step).max(1) as u32;
    let pos = (rel * steps + (w as u32 - 1) / 2) / (w as u32 - 1).max(1);
    (min + (pos as u16) * step).clamp(min, max)
}

// Input popup (Normal/Interrupt)
pub fn compute_input_popup_rect(app: &App, area: Rect) -> Option<Rect> {
    if !(app.is_text_input_mode()) {
        return None;
    }
    let block =
        Block::default().title(header_title_line(app_display_base(app), app)).borders(Borders::ALL);
    let inner = block.inner(area);
    let buf = app.input_buffer().unwrap_or("");
    let msg = format!("Title: {} _", buf);
    let content_w = UnicodeWidthStr::width(msg.as_str()) as u16;
    let popup_w = content_w.saturating_add(4).min(inner.width).max(30).min(inner.width);
    let popup_h: u16 = 4; // message + buttons
    let px = inner.x + (inner.width.saturating_sub(popup_w)) / 2;
    let py = inner.y + (inner.height.saturating_sub(popup_h)) / 2;
    Some(Rect { x: px, y: py, width: popup_w, height: popup_h })
}

// New-task estimate popup (slider)
pub fn compute_new_task_estimate_popup_rect(app: &App, area: Rect) -> Option<Rect> {
    if !app.is_new_task_estimate() {
        return None;
    }
    let block =
        Block::default().title(header_title_line(app_display_base(app), app)).borders(Borders::ALL);
    let inner = block.inner(area);
    let est = app
        .input_buffer()
        .and_then(|s| s.parse::<u16>().ok())
        .or_else(|| app.new_task_default_estimate())
        .unwrap_or(25);
    let title = app.new_task_title().unwrap_or("");
    let msg = if title.is_empty() {
        format!("Estimate: {}m", est)
    } else {
        format!("Estimate: {}m — {}", est, title)
    };
    let content_w = (UnicodeWidthStr::width(msg.as_str()).max(date_line_min_width().into()) as u16)
        .saturating_add(0);
    let popup_w = content_w.saturating_add(4).min(inner.width).max(34).min(inner.width);
    // Include: message, date, slider, buttons (inner height 4)
    let popup_h: u16 = 6;
    let px = inner.x + (inner.width.saturating_sub(popup_w)) / 2;
    let py = inner.y + (inner.height.saturating_sub(popup_h)) / 2;
    Some(Rect { x: px, y: py, width: popup_w, height: popup_h })
}

// Command popup geometry (single-line prompt + buttons)
pub fn compute_command_popup_rect(app: &App, area: Rect) -> Option<Rect> {
    if !app.is_command_mode() {
        return None;
    }
    let inner = Block::default().borders(Borders::ALL).inner(area);
    if inner.width < 10 || inner.height < 3 {
        return None;
    }
    let buf = app.input_buffer().unwrap_or("");
    let title = app.day.tasks.get(app.selected_index()).map(|t| t.title.as_str()).unwrap_or("");
    let suffix = if title.is_empty() { "".to_string() } else { format!("{}", title) };
    let content = format!("Command: {} _{}", buf, suffix);
    let content_w = content.width() as u16;
    let popup_w = content_w.saturating_add(4).min(inner.width).max(30).min(inner.width);
    let popup_h: u16 = 4; // message + buttons
    let px = inner.x + (inner.width.saturating_sub(popup_w)) / 2;
    let py = inner.y + (inner.height.saturating_sub(popup_h)) / 2;
    Some(Rect { x: px, y: py, width: popup_w, height: popup_h })
}

pub fn input_popup_button_hitboxes(_app: &App, popup: Rect) -> (Rect, Rect) {
    let inner = Rect {
        x: popup.x + 1,
        y: popup.y + 1,
        width: popup.width.saturating_sub(2),
        height: popup.height.saturating_sub(2),
    };
    // Place buttons on the last inner line, so it adapts to both 4-line and 5-line popups
    let y = inner.y + inner.height.saturating_sub(1);
    let add_w = UnicodeWidthStr::width("OK") as u16;
    let ca_w = UnicodeWidthStr::width("Cancel") as u16;
    let total = add_w + 2 + ca_w;
    let start_x = inner.x + (inner.width.saturating_sub(total)) / 2;
    let add = Rect { x: start_x, y, width: add_w, height: 1 };
    let cancel = Rect { x: start_x + add_w + 2, y, width: ca_w, height: 1 };
    (add, cancel)
}

pub fn command_popup_button_hitboxes(_app: &App, popup: Rect) -> (Rect, Rect) {
    let inner = Rect {
        x: popup.x + 1,
        y: popup.y + 1,
        width: popup.width.saturating_sub(2),
        height: popup.height.saturating_sub(2),
    };
    let y = inner.y + inner.height.saturating_sub(1);
    let run_w = UnicodeWidthStr::width("Run") as u16;
    let ca_w = UnicodeWidthStr::width("Cancel") as u16;
    let total = run_w + 2 + ca_w;
    let start_x = inner.x + (inner.width.saturating_sub(total)) / 2;
    let run = Rect { x: start_x, y, width: run_w, height: 1 };
    let cancel = Rect { x: start_x + run_w + 2, y, width: ca_w, height: 1 };
    (run, cancel)
}

// note: helpers that parsed message text for positions were removed in favor of
// explicit hitbox geometry to reduce dead code and simplify clippy compliance.

fn date_label_for(ymd: u32) -> String {
    let base = if crate::date::is_valid_ymd(ymd) { ymd } else { crate::date::today_ymd() };
    let wd = crate::date::weekday_short_en(base);
    if base == crate::date::today_ymd() {
        format!("Today ({})", wd)
    } else if base == crate::date::add_days_to_ymd(crate::date::today_ymd(), 1) {
        format!("Tomorrow ({})", wd)
    } else {
        format!("{} ({})", crate::date::format_ymd(base), wd)
    }
}

fn date_line_min_width() -> u16 {
    use unicode_width::UnicodeWidthStr as UW;
    let w1 = UW::width("Date: Today (Wed)") as u16;
    let w2 = UW::width("Date: Tomorrow (Wed)") as u16;
    let w3 = UW::width("Date: 2099-12-31 (Wed)") as u16;
    w1.max(w2).max(w3)
}

/// Return hitboxes for the date picker line: (prev_btn, label, next_btn).
/// The line lives at `inner.y + 1` in both estimate popups.
pub fn date_picker_hitboxes(_app: &App, popup: Rect) -> (Rect, Rect, Rect) {
    let inner = Rect {
        x: popup.x + 1,
        y: popup.y + 1,
        width: popup.width.saturating_sub(2),
        height: popup.height.saturating_sub(2),
    };
    let y = inner.y + 1; // date line
                         // Stable anchors: fixed buttons so mouse targets do not move with label width
    let prev = Rect { x: inner.x + 2, y, width: 1, height: 1 };
    let next = Rect { x: inner.x + inner.width.saturating_sub(3), y, width: 1, height: 1 };
    // Label fills the space between prev and next minus single spaces around it
    let label_x = prev.x + 2; // "< " then label
    let label_w = next.x.saturating_sub(label_x).saturating_sub(1);
    let label_rect = Rect { x: label_x, y, width: label_w, height: 1 };
    (prev, label_rect, next)
}

// Simple quick popup geometry
pub fn compute_start_time_popup_rect(app: &App, area: Rect) -> Option<Rect> {
    if !app.is_start_time_edit() {
        return None;
    }
    let inner = Block::default().borders(Borders::ALL).inner(area);
    if inner.width < 24 || inner.height < 4 {
        return None;
    }
    let title = app.day.tasks.get(app.selected_index()).map(|t| t.title.as_str()).unwrap_or("");
    let mins =
        app.input_buffer().and_then(|s| s.parse::<u16>().ok()).unwrap_or(app_display_base(app));
    let hh = (mins / 60) % 24;
    let mm = mins % 60;
    let msg = if title.is_empty() {
        format!("Start: {:02}:{:02}", hh, mm)
    } else {
        format!("Start: {:02}:{:02}{}", hh, mm, title)
    };
    let content_w = UnicodeWidthStr::width(msg.as_str()) as u16;
    let popup_w = content_w.saturating_add(6).min(inner.width).max(28).min(inner.width);
    let popup_h: u16 = 4; // message + slider + buttons
    let px = inner.x + (inner.width.saturating_sub(popup_w)) / 2;
    let py = inner.y + (inner.height.saturating_sub(popup_h)) / 2;
    Some(Rect { x: px, y: py, width: popup_w, height: popup_h })
}

fn render_time_slider_line(f: &mut Frame, track: Rect, minutes: u16) {
    let min = 0u16;
    let max = 23 * 60 + 59;
    let step = 5u16;
    let knob_x = slider_x_for_minutes(track, min, max, step, minutes);
    let mut line = Line::default();
    line.spans.push(Span::styled("[".to_string(), Style::default().fg(Color::DarkGray)));
    for x in track.x..track.x + track.width {
        if x == knob_x {
            line.spans.push(Span::styled(
                "".to_string(),
                Style::default().fg(Color::Yellow).add_modifier(Modifier::BOLD),
            ));
        } else if x < knob_x {
            line.spans.push(Span::styled("=".to_string(), Style::default().fg(Color::Green)));
        } else {
            line.spans.push(Span::styled("·".to_string(), Style::default().fg(Color::DarkGray)));
        }
    }
    line.spans.push(Span::styled("]".to_string(), Style::default().fg(Color::DarkGray)));
    let para = Paragraph::new(line);
    let expanded = Rect {
        x: track.x.saturating_sub(1),
        y: track.y,
        width: track.width.saturating_add(2),
        height: 1,
    };
    f.render_widget(para, expanded);
}

fn render_date_line(f: &mut Frame, app: &App, popup: Rect, inner: Rect, color: Color, ymd: u32) {
    let (prev, label_rect, next) = date_picker_hitboxes(app, popup);
    let date_label = date_label_for(ymd);
    let text = format!("Date: {}", date_label);
    let mut spans: Vec<Span> = Vec::new();
    // pad until prev
    let left_pad = prev.x.saturating_sub(inner.x) as usize;
    if left_pad > 0 {
        spans.push(Span::raw(" ".repeat(left_pad)));
    }
    let prev_style = if matches!(app.popup_hover_button(), Some(crate::app::PopupButton::DatePrev))
    {
        Style::default().fg(Color::Black).bg(Color::Cyan).add_modifier(Modifier::BOLD)
    } else {
        Style::default().fg(color).add_modifier(Modifier::BOLD)
    };
    spans.push(Span::styled("<".to_string(), prev_style));
    spans.push(Span::raw(" "));
    // Fit label to available width
    let fitted = fit_to_width(&text, label_rect.width as usize);
    spans.push(Span::styled(fitted, Style::default().fg(color)));
    // Compute spaces so that '>' appears at next.x
    let printed_w = (UnicodeWidthStr::width(text.as_str()) as u16).min(label_rect.width);
    let gap = next.x.saturating_sub(prev.x + 2 + printed_w) as usize;
    if gap > 0 {
        spans.push(Span::raw(" ".repeat(gap)));
    }
    let next_style = if matches!(app.popup_hover_button(), Some(crate::app::PopupButton::DateNext))
    {
        Style::default().fg(Color::Black).bg(Color::Cyan).add_modifier(Modifier::BOLD)
    } else {
        Style::default().fg(color).add_modifier(Modifier::BOLD)
    };
    spans.push(Span::styled(">".to_string(), next_style));
    let date_rect = Rect { x: inner.x, y: label_rect.y, width: inner.width, height: 1 };
    f.render_widget(Paragraph::new(Line::from(spans)), date_rect);
}

fn fit_to_width(s: &str, width: usize) -> String {
    use unicode_width::UnicodeWidthChar;
    if UnicodeWidthStr::width(s) <= width {
        return s.to_string();
    }
    let mut out = String::new();
    let mut used = 0usize;
    for ch in s.chars() {
        let w = ch.width().unwrap_or(1);
        if used + w > width {
            break;
        }
        out.push(ch);
        used += w;
    }
    out
}

fn render_slider_line(f: &mut Frame, track: Rect, minutes: u16) {
    // Styled slider: [====●····]
    let min = 0u16;
    let max = 240u16;
    let step = 5u16;
    let knob_x = slider_x_for_minutes(track, min, max, step, minutes);
    let mut line = Line::default();
    // Left bracket just before track
    line.spans.push(Span::styled("[".to_string(), Style::default().fg(Color::DarkGray)));
    for x in track.x..track.x + track.width {
        if x == knob_x {
            line.spans.push(Span::styled(
                "".to_string(),
                Style::default().fg(Color::Yellow).add_modifier(Modifier::BOLD),
            ));
        } else if x < knob_x {
            line.spans.push(Span::styled("=".to_string(), Style::default().fg(Color::Green)));
        } else {
            line.spans.push(Span::styled("·".to_string(), Style::default().fg(Color::DarkGray)));
        }
    }
    line.spans.push(Span::styled("]".to_string(), Style::default().fg(Color::DarkGray)));
    let para = Paragraph::new(line);
    // Expand rect by one cell left/right to contain brackets when possible
    let expanded = Rect {
        x: track.x.saturating_sub(1),
        y: track.y,
        width: track.width.saturating_add(2),
        height: 1,
    };
    f.render_widget(para, expanded);
}

/// Compute key layout rectangles used by `draw`, for hit testing and tests.
/// Returns (tabs, optional active banner, list/content, help) within the inner bordered area.
pub fn compute_layout(app: &App, area: Rect) -> (Rect, Option<Rect>, Rect, Rect) {
    // Replicate the same sizing logic as `draw`.
    // First, account for the outer Block's borders.
    let inner = Rect {
        x: area.x.saturating_add(1),
        y: area.y.saturating_add(1),
        width: area.width.saturating_sub(2),
        height: area.height.saturating_sub(2),
    };

    // Optional active banner allocates one line under tabs
    let has_banner = format_active_banner(app).is_some();

    // Help height depends on wrapping for the current width; keep at least
    // table header + two rows visible in the list area.
    let help_lines = help_lines_for_width(app, inner.width.max(1));
    let mut help_height = help_lines.len() as u16;
    let reserved = 1 /* tabs */ + if has_banner { 1 } else { 0 } + MIN_LIST_LINES;
    let max_help = inner.height.saturating_sub(reserved);
    if max_help > 0 {
        help_height = help_height.min(max_help);
    }
    // Ensure at least three lines (gauge + labels + help) when space allows;
    // otherwise keep at least two (gauge + help) or one (help only).
    help_height = if max_help >= 3 {
        help_height.max(3)
    } else if max_help >= 2 {
        help_height.max(2)
    } else {
        help_height.max(1)
    };
    let tabs = Rect { x: inner.x, y: inner.y, width: inner.width, height: 1 };
    let mut y = inner.y + 1;
    let banner = if has_banner {
        let b = Rect { x: inner.x, y, width: inner.width, height: 1 };
        y += 1;
        Some(b)
    } else {
        None
    };
    // List/content takes remaining minus help
    let list_height = inner.height.saturating_sub(y - inner.y).saturating_sub(help_height);
    let list = Rect { x: inner.x, y, width: inner.width, height: list_height };
    let help = Rect {
        x: inner.x,
        y: inner.y + inner.height.saturating_sub(help_height),
        width: inner.width,
        height: help_height,
    };
    (tabs, banner, list, help)
}

// ---- Time Blocks (Timeline) Mode ----

// (removed horizontal time blocks view)

/// Google Calendar風の縦軸タイムライン。左に時刻ラベル、右に2レーン(Plan/Actual)。
fn render_calendar_day_at(
    f: &mut Frame,
    rect: Rect,
    app: &App,
    tasks: &[crate::task::Task],
    now_min: u16,
) {
    if rect.height == 0 || rect.width < 12 {
        return;
    }
    let start_min = app_display_base(app);
    // Planned end accumulates estimates sequentially from day start
    let mut cur = start_min;
    let mut planned_ranges: Vec<(u16, u16, String, TaskCategory)> = Vec::new();
    for t in tasks.iter() {
        if let Some(fs) = t.fixed_start_min {
            cur = cur.max(fs);
        }
        let s = cur;
        let e = cur.saturating_add(t.estimate_min);
        planned_ranges.push((s, e, t.title.clone(), t.category));
        cur = e;
    }
    let mut act_ranges: Vec<(usize, u16, u16, String, TaskCategory, bool)> = Vec::new();
    for (ti, t) in tasks.iter().enumerate() {
        for s in t.sessions.iter() {
            let end = s.end_min.unwrap_or(now_min);
            let closed = s.end_min.is_some();
            act_ranges.push((ti, s.start_min, end, t.title.clone(), t.category, closed));
        }
    }
    // Hide sub-minute work on the calendar: drop zero-minute ranges (start == end)
    act_ranges.retain(|(_ti, s, e, _title, _cat, _closed)| e > s);
    let latest = planned_ranges
        .iter()
        .map(|&(_, e, _, _)| e)
        .chain(act_ranges.iter().map(|&(_, _, e, _, _, _)| e))
        .max()
        .unwrap_or(start_min);
    let end_min = latest.max(start_min + 90); // ensure some space (≥1.5h)
    let span = end_min.saturating_sub(start_min).max(1);

    // Layout: [gutter 6] [space 1] [plan lane] [gap 1] [act lane]
    let gutter = 6u16; // e.g., "09:00"
    let gaps = 2u16; // spaces between lanes and gutter
    let lanes_w = rect.width.saturating_sub(gutter + gaps);
    let lane_w = (lanes_w.saturating_sub(1)) / 2; // leave 1 col gap between lanes
    if lane_w == 0 {
        return;
    }
    // lanes start after the gutter; we render strings via Paragraph

    // Map minute -> y row in [0..h-1]
    let to_y = |m: u16, h: u16| -> u16 {
        let rel = m.saturating_sub(start_min) as u32;
        let y = (rel * (h.saturating_sub(1) as u32)) / (span as u32);
        y as u16
    };

    // Prepare line-by-line strings for lanes
    let mut lines_plan: Vec<String> = vec![" ".repeat(lane_w as usize); rect.height as usize];
    // We'll build Actual lane strings later (supports horizontal split)
    let mut lines_act: Vec<String> = vec![" ".repeat(lane_w as usize); rect.height as usize];
    // Per-row colors derived from the category of the task covering the row (plan side)
    let mut plan_colors: Vec<Option<Color>> = vec![None; rect.height as usize];
    // For actual side after columnization (per-column colors are tracked separately)

    for (s, e, title, cat) in planned_ranges.into_iter() {
        let y0 = to_y(s, rect.height);
        let y1 = to_y(e, rect.height).max(y0);
        // Resolve this block's category color directly from range
        let this_color = app.config.category_color(cat);
        for y in y0..=y1 {
            if let Some(row) = lines_plan.get_mut(y as usize) {
                *row = "".repeat(lane_w as usize);
            }
            if let Some(slot) = plan_colors.get_mut(y as usize) {
                *slot = Some(this_color);
            }
        }
        // Put the task title on the first row of its planned block
        if let Some(row) = lines_plan.get_mut(y0 as usize) {
            let fitted = fit_to_width(&title, lane_w as usize);
            use unicode_width::UnicodeWidthStr as UW;
            let w = UW::width(fitted.as_str()) as u16;
            let mut s = String::new();
            s.push_str(&fitted);
            if w < lane_w {
                s.push_str(&"".repeat((lane_w - w) as usize));
            }
            *row = s;
        }
    }
    // ---- Actual lane with overlap split into columns ----
    #[derive(Clone)]
    struct Block {
        ti: usize,
        s: u16,
        e: u16,
        y0: u16,
        y1: u16,
        title: String,
        cat: TaskCategory,
        closed: bool,
        col: usize,
    }
    let mut blocks_raw: Vec<Block> = act_ranges
        .iter()
        .cloned()
        .map(|(ti, s, e, title, cat, closed)| {
            let y0 = to_y(s, rect.height);
            let y1 = to_y(e, rect.height).max(y0);
            Block { ti, s, e, y0, y1, title, cat, closed, col: 0 }
        })
        .collect();
    // Merge per-task blocks that overlap or touch visually
    blocks_raw.sort_by_key(|b| (b.ti, b.y0, b.y1));
    let mut blocks: Vec<Block> = Vec::new();
    let mut k = 0usize;
    while k < blocks_raw.len() {
        let mut cur = blocks_raw[k].clone();
        k += 1;
        while k < blocks_raw.len() && blocks_raw[k].ti == cur.ti && blocks_raw[k].y0 <= cur.y1 {
            let b = blocks_raw[k].clone();
            cur.y1 = cur.y1.max(b.y1);
            cur.e = cur.e.max(b.e);
            cur.closed = cur.closed && b.closed;
            k += 1;
        }
        blocks.push(cur);
    }
    // Assign columns greedily
    blocks.sort_by_key(|b| b.s);
    // Track last occupied row (y1) per column to detect visual overlap (row-level)
    let mut col_yend: Vec<u16> = Vec::new();
    for b in blocks.iter_mut() {
        let mut placed = false;
        for (ci, yend) in col_yend.iter_mut().enumerate() {
            // Non-overlap visually when previous end row is strictly above next start row
            if *yend < b.y0 {
                b.col = ci;
                *yend = b.y1;
                placed = true;
                break;
            }
        }
        if !placed {
            b.col = col_yend.len();
            col_yend.push(b.y1);
        }
    }
    let mut ncols = col_yend.len().max(1);
    // Omission rule: show at most 3 columns
    if ncols > 3 {
        ncols = 3;
    }
    if ncols as u16 > lane_w {
        ncols = lane_w as usize;
    }
    let base_w = (lane_w / ncols as u16).max(1);
    let rem = (lane_w % ncols as u16) as usize;
    let col_widths: Vec<u16> = (0..ncols).map(|i| base_w + if i < rem { 1 } else { 0 }).collect();
    let mut lines_act_cols: Vec<Vec<String>> =
        vec![vec![String::new(); ncols]; rect.height as usize];
    let mut act_col_colors: Vec<Vec<Option<Color>>> = vec![vec![None; ncols]; rect.height as usize];
    for row in lines_act_cols.iter_mut().take(rect.height as usize) {
        for (c, cell) in row.iter_mut().enumerate().take(ncols) {
            *cell = " ".repeat(col_widths[c] as usize);
        }
    }
    // Fill blocks
    for b in blocks.iter() {
        let col = b.col.min(ncols.saturating_sub(1));
        let y0 = to_y(b.s, rect.height);
        let y1 = to_y(b.e, rect.height).max(y0);
        let cw = col_widths[col] as usize;
        for y in y0..=y1 {
            let yi = y as usize;
            if let Some(cell) = lines_act_cols.get_mut(yi).and_then(|row| row.get_mut(col)) {
                *cell = "".repeat(cw);
            }
            if let Some(slot) = act_col_colors.get_mut(yi).and_then(|row| row.get_mut(col)) {
                if slot.is_none() {
                    *slot = Some(app.config.category_color(b.cat));
                }
            }
        }
    }
    // Overlay titles per column on their start rows for closed sessions
    for b in blocks.iter() {
        if b.closed && b.e <= now_min {
            let y = to_y(b.s, rect.height).min(rect.height.saturating_sub(1));
            let yi = y as usize;
            let col = b.col.min(ncols.saturating_sub(1));
            let cw = col_widths[col] as usize;
            let fitted = fit_to_width(&b.title, cw);
            use unicode_width::UnicodeWidthStr as UW;
            let w = UW::width(fitted.as_str()) as usize;
            if let Some(cell) = lines_act_cols.get_mut(yi).and_then(|row| row.get_mut(col)) {
                let mut sline = String::new();
                sline.push_str(&fitted);
                if w < cw {
                    sline.push_str(&"".repeat(cw - w));
                }
                *cell = sline;
            }
            if let Some(slot) = act_col_colors.get_mut(yi).and_then(|row| row.get_mut(col)) {
                if slot.is_none() {
                    *slot = Some(app.config.category_color(b.cat));
                }
            }
        }
    }
    // Join columns per row into act strings
    for y in 0..rect.height as usize {
        let mut s = String::new();
        for c in 0..ncols {
            s.push_str(&lines_act_cols[y][c]);
        }
        let w = s.chars().count() as u16;
        if w < lane_w {
            s.push_str(&" ".repeat((lane_w - w) as usize));
        }
        lines_act[y] = s;
    }

    // Precompute hour labels mapped to row positions to avoid missing due to discretization
    let mut hour_labels: Vec<Option<String>> = vec![None; rect.height as usize];
    let mut hmark = start_min.saturating_sub(start_min % 60); // floor to hour
    while hmark <= end_min {
        let y = to_y(hmark, rect.height);
        let label = format!("{:02}:00", (hmark / 60) % 24);
        if (y as usize) < hour_labels.len() {
            hour_labels[y as usize] = Some(label);
        }
        hmark = hmark.saturating_add(60);
    }

    // Render per line: gutter label | plan lane | gap | act lane
    let active_title: Option<String> = if matches!(app.view(), View::Today) {
        app.day.active_index().and_then(|idx| app.day.tasks.get(idx)).map(|t| t.title.clone())
    } else {
        None
    };
    for i in 0..rect.height {
        let y = rect.y + i;
        let label = hour_labels[i as usize].clone().unwrap_or_default();
        let mut left = format!("{label:>6}");
        // ensure width
        if left.len() < gutter as usize {
            left = format!("{:>width$}", left, width = gutter as usize);
        }
        let is_now_row = {
            let y_now = {
                let rel = now_min.saturating_sub(start_min) as u32;
                (rel * (rect.height.saturating_sub(1) as u32) / (span as u32)) as u16
            };
            i == y_now
        };
        let left_span = Span::styled(left, Style::default().fg(Color::DarkGray));
        let plan_cell = if is_now_row {
            // Show Now HH:MM at the head of the plan lane, then extend with line
            let hh = (now_min / 60) % 24;
            let mm = now_min % 60;
            let label = format!("Now {:02}:{:02}", hh, mm);
            let fitted = fit_to_width(&label, lane_w as usize);
            use unicode_width::UnicodeWidthStr as UW;
            let w = UW::width(fitted.as_str()) as u16;
            let mut s = String::new();
            s.push_str(&fitted);
            if w < lane_w {
                s.push_str(&"".repeat((lane_w - w) as usize));
            }
            s
        } else {
            lines_plan[i as usize].clone()
        };
        let act_cell = if is_now_row {
            if let Some(title) = active_title.as_ref() {
                let fitted = fit_to_width(title, lane_w as usize);
                use unicode_width::UnicodeWidthStr as UW;
                let w = UW::width(fitted.as_str()) as u16;
                let mut s = String::new();
                s.push_str(&fitted);
                if w < lane_w {
                    s.push_str(&"".repeat((lane_w - w) as usize));
                }
                s
            } else {
                "".repeat(lane_w as usize)
            }
        } else {
            lines_act[i as usize].clone()
        };
        let plan_span = Span::styled(
            plan_cell,
            if is_now_row {
                Style::default().fg(Color::Red).add_modifier(Modifier::BOLD)
            } else {
                Style::default().fg(plan_colors[i as usize].unwrap_or(Color::Green))
            },
        );
        let gap_span = if is_now_row {
            Span::styled("", Style::default().fg(Color::Red))
        } else {
            Span::raw(" ")
        };
        let act_style = if is_now_row {
            if active_title.is_some() {
                Style::default().fg(Color::DarkGray)
            } else {
                Style::default().fg(Color::Red)
            }
        } else {
            // Unused for per-column styles; keep default
            Style::default()
        };
        let mut parts: Vec<Span> = vec![left_span, Span::raw(" "), plan_span, gap_span];
        if is_now_row {
            parts.push(Span::styled(act_cell, act_style));
        } else {
            // Build per-column styled spans
            let yi = i as usize;
            for (c, seg) in lines_act_cols[yi].iter().enumerate() {
                let fg = act_col_colors[yi][c].unwrap_or(Color::Magenta);
                parts.push(Span::styled(seg.clone(), Style::default().fg(fg)));
            }
        }
        let line = Line::from(parts);
        let para = Paragraph::new(line);
        f.render_widget(para, Rect { x: rect.x, y, width: rect.width, height: 1 });
    }
}