kanzo 0.4.1

Self-hosted kanban for humans and agents. One 4.7 MB binary, starts in 3 ms, zero setup.
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
"use strict";

// The board runs on browsers back to Safari 13.1, Chrome 80 and Firefox 78. These
// two are newer than that, and are all it needs on top.
if (!Element.prototype.replaceChildren) {
  Element.prototype.replaceChildren = function (...nodes) {
    this.textContent = "";
    this.append(...nodes);
  };
}
if (!HTMLFormElement.prototype.requestSubmit) {
  HTMLFormElement.prototype.requestSubmit = function () {
    if (this.reportValidity()) this.dispatchEvent(new Event("submit", { cancelable: true }));
  };
}

const $ = (sel, root = document) => root.querySelector(sel);
const STATUS_COLORS = ["#8a8a83", "#3b82f6", "#e8a317", "#2f9e44", "#ae3ec9", "#e8590c"];

const state = {
  project: "all",
  board: null,
  changes: null,
  task: null,
  dragging: null,
  dropped: false,
  layout: "",
  allDone: false,
  menu: null,
  net: null,
  guests: null,
  selected: new Set(),
  sweeping: false,
};

async function api(method, path, body) {
  const image = body instanceof Blob;
  const res = await fetch("/api" + path, {
    method,
    headers: body ? { "Content-Type": image ? body.type : "application/json" } : {},
    body: image || !body ? body : JSON.stringify(body),
  });
  noticeVersion(res.headers.get("x-kanzo-version"));
  const data = await res.json().catch(() => ({}));
  if (!res.ok) throw Object.assign(new Error(data.error || res.statusText), { data });
  return data;
}

// An update restarts the board under the page, which then reloads to match it. Not while
// someone types, though: the next reply tries again.
let pageVersion = null;

function noticeVersion(version) {
  if (!version) return;
  pageVersion ??= version;
  if (version !== pageVersion && !document.activeElement?.matches("input, textarea, select")) location.reload();
}

function plural(n, word) {
  // In zh-CN the noun's translation carries its measure word: 个任务, 条评论, 台电脑.
  const w = DICT?.[word] ?? `${word}${n === 1 ? "" : "s"}`;
  return `${n} ${w}`;
}

// Storage can be off, as in private windows.
function localStorageGet(k) {
  try { return localStorage.getItem(k); } catch { return null; }
}

function localStorageSet(k, v) {
  try { localStorage.setItem(k, v); } catch {}
}

// The board's language: a saved choice wins, else the browser's. English is the
// source strings, so it runs with no dictionary at all.
const LANG = (() => {
  const saved = localStorageGet("kanzo.lang");
  if (saved === "zh-cn" || saved === "en") return saved;
  return (navigator.language || "").toLowerCase().startsWith("zh") ? "zh-cn" : "en";
})();

let DICT = null;

// Looks a string up in the dictionary; {0}, {1}… fill in the pieces that move
// around in another language. Without a dictionary every string is itself.
function tr(s, ...args) {
  const out = LANG === "en" || !DICT ? s : (DICT[s] ?? s);
  return out.replace(/\{(\d+)\}/g, (_, i) => args[i] ?? "");
}

function el(tag, attrs = {}, ...children) {
  const node = document.createElement(tag);
  for (const [k, v] of Object.entries(attrs)) {
    if (v == null || v === false) continue;
    if (k.startsWith("on")) node.addEventListener(k.slice(2), v);
    else if (k === "class") node.className = v;
    else node.setAttribute(k, v === true ? "" : v);
  }
  node.append(...children.flat().filter((c) => c != null && c !== false));
  return node;
}

// From Lucide, ISC licence.
const ICONS = {
  newProject: '<path d="M12 3H5a2 2 0 0 0-2 2v14a2 2 0 0 0 2 2h14a2 2 0 0 0 2-2v-7"/><path d="M18.4 2.6a2.1 2.1 0 0 1 3 3L12 15l-4 1 1-4Z"/>',
  board: '<rect width="18" height="18" x="3" y="3" rx="2"/><path d="M8 7v7M12 7v4M16 7v9"/>',
  folder: '<path d="M20 20a2 2 0 0 0 2-2V8a2 2 0 0 0-2-2h-7.9a2 2 0 0 1-1.69-.9L9.6 3.9A2 2 0 0 0 7.93 3H4a2 2 0 0 0-2 2v13a2 2 0 0 0 2 2Z"/>',
  search: '<circle cx="11" cy="11" r="7"/><path d="m20 20-3.5-3.5"/>',
  close: '<path d="M18 6 6 18M6 6l12 12"/>',
  more: '<circle cx="5" cy="12" r="1"/><circle cx="12" cy="12" r="1"/><circle cx="19" cy="12" r="1"/>',
  plus: '<path d="M5 12h14M12 5v14"/>',
  list: '<path d="M4 6h16M4 12h16M4 18h16"/>',
  trash: '<path d="M3 6h18M8 6V4h8v2M19 6l-1 14H6L5 6M10 11v6M14 11v6"/>',
  check: '<path d="M20 6 9 17l-5-5"/>',
  copy: '<rect width="14" height="14" x="8" y="8" rx="2"/><path d="M4 16a2 2 0 0 1-2-2V4a2 2 0 0 1 2-2h10a2 2 0 0 1 2 2"/>',
  human: '<circle cx="12" cy="8" r="4"/><path d="M5 21v-1a6 6 0 0 1 6-6h2a6 6 0 0 1 6 6v1"/>',
  url: '<path d="M10 13a5 5 0 0 0 7.54.54l3-3a5 5 0 0 0-7.07-7.07l-1.72 1.71"/><path d="M14 11a5 5 0 0 0-7.54-.54l-3 3a5 5 0 0 0 7.07 7.07l1.71-1.71"/>',
  github: '<path d="M15 22v-4a4.8 4.8 0 0 0-1-3.5c3 0 6-2 6-5.5.1-1.3-.3-2.5-1-3.5.3-1.2.3-2.4 0-3.5 0 0-1 0-3 1.5a12 12 0 0 0-8 0C6 2 5 2 5 2c-.3 1.2-.3 2.4 0 3.5A5.4 5.4 0 0 0 4 9c0 3.5 3 5.5 6 5.5-.4.5-.7 1-.8 1.7-.2.6-.3 1.2-.2 1.8v4"/><path d="M9 18c-4.5 2-5-2-7-2"/>',
  sync: '<path d="M12 13v8"/><path d="M4 14.9A7 7 0 1 1 15.7 8h1.8a4.5 4.5 0 0 1 2.5 8.2"/><path d="m8 17 4-4 4 4"/>',
  collapse: '<path d="m13 17 5-5-5-5M6 17l5-5-5-5"/>',
  expand: '<path d="m11 17-5-5 5-5M18 17l-5-5 5-5"/>',
  rename: '<path d="M12 20h9"/><path d="M16.5 3.5a2.1 2.1 0 0 1 3 3L7 19l-4 1 1-4Z"/>',
  update: '<circle cx="12" cy="12" r="9"/><path d="M12 7v9M8 12l4 4 4-4"/>',
  settings: '<path d="M12.22 2h-.44a2 2 0 0 0-2 2v.18a2 2 0 0 1-1 1.73l-.43.25a2 2 0 0 1-2 0l-.15-.08a2 2 0 0 0-2.73.73l-.22.38a2 2 0 0 0 .73 2.73l.15.1a2 2 0 0 1 1 1.72v.51a2 2 0 0 1-1 1.74l-.15.09a2 2 0 0 0-.73 2.73l.22.38a2 2 0 0 0 2.73.73l.15-.08a2 2 0 0 1 2 0l.43.25a2 2 0 0 1 1 1.73V20a2 2 0 0 0 2 2h.44a2 2 0 0 0 2-2v-.18a2 2 0 0 1 1-1.73l.43-.25a2 2 0 0 1 2 0l.15.08a2 2 0 0 0 2.73-.73l.22-.39a2 2 0 0 0-.73-2.73l-.15-.08a2 2 0 0 1-1-1.74v-.5a2 2 0 0 1 1-1.74l.15-.09a2 2 0 0 0 .73-2.73l-.22-.38a2 2 0 0 0-2.73-.73l-.15.08a2 2 0 0 1-2 0l-.43-.25a2 2 0 0 1-1-1.73V4a2 2 0 0 0-2-2z"/><circle cx="12" cy="12" r="3"/>',
  send: '<path d="M12 19V5M5 12l7-7 7 7"/>',
  info: '<circle cx="12" cy="12" r="9"/><path d="M12 16v-4M12 8h.01"/>',
  sidebar: '<rect x="3" y="4" width="18" height="16" rx="4"/><path d="M9 4v16"/>',
  waits: '<path d="M6 3h12M6 21h12M7 3v4l5 5 5-5V3M7 21v-4l5-5 5 5v4"/>',
  news: '<path d="M12 3l2.6 5.6 6.1.7-4.5 4.2 1.2 6-5.4-3-5.4 3 1.2-6-4.5-4.2 6.1-.7z"/>',
  comment: '<path d="M21 15a2 2 0 0 1-2 2H7l-4 4V5a2 2 0 0 1 2-2h14a2 2 0 0 1 2 2z"/>',
  share: '<circle cx="18" cy="5" r="3"/><circle cx="6" cy="12" r="3"/><circle cx="18" cy="19" r="3"/><path d="m8.6 13.5 6.8 4M15.4 6.5l-6.8 4"/>',
};

function icon(name, color) {
  const span = el("span", { class: "icon", style: color ? `color:${color}` : null });
  span.innerHTML = `<svg viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="1.8" stroke-linecap="round" stroke-linejoin="round" aria-hidden="true">${ICONS[name]}</svg>`;
  return span;
}

document.querySelectorAll("[data-icon]").forEach((n) => n.replaceWith(icon(n.dataset.icon)));

let toastTimer;
// The clipboard API needs a secure page; a board opened from another device over plain
// HTTP falls back.
async function copyKey(text) {
  try {
    await navigator.clipboard.writeText(text);
  } catch {
    const area = el("textarea", { style: "position:fixed;opacity:0" });
    area.value = text;
    document.body.append(area);
    area.select();
    document.execCommand("copy");
    area.remove();
  }
  toast(tr("Copied {0}", text));
}

function toast(message) {
  document.querySelector(".toast")?.remove();
  const t = el("div", { class: "toast" }, message);
  document.body.append(t);
  clearTimeout(toastTimer);
  toastTimer = setTimeout(() => t.remove(), 3500);
}

// The names match PROJECT_COLORS in store.rs.
const PROJECT_COLORS = {
  gray: "#8e8e93", red: "#ef4444", orange: "#f97316", yellow: "#eab308", green: "#22c55e",
  teal: "#14b8a6", blue: "#3b82f6", purple: "#a855f7", pink: "#ec4899",
};

function projectColorName(slug) {
  const picked = state.board?.projects.find((p) => p.slug === slug)?.color;
  if (PROJECT_COLORS[picked]) return picked;
  const names = Object.keys(PROJECT_COLORS).filter((n) => n !== "gray");
  let h = 0;
  for (const c of slug) h = (h * 31 + c.charCodeAt(0)) % 997;
  return names[h % names.length];
}

function projectColor(slug) {
  return PROJECT_COLORS[projectColorName(slug)];
}

function statusStep(status) {
  const { statuses } = state.board;
  const i = statuses.indexOf(status);
  return i < 0 ? 0 : i - statuses.indexOf("todo");
}

function statusColor(status) {
  const i = state.board ? statusStep(status) : 0;
  return STATUS_COLORS[Math.max(i, 0) % STATUS_COLORS.length];
}

function statusIcon(status) {
  const step = statusStep(status), steps = statusStep("done");
  const f = Math.max(step, 0) / steps;
  const color = statusColor(status);
  const dash = step < 0 ? ' stroke-dasharray="2.36 2.36"' : "";
  let inside = "";
  if (f >= 1) {
    inside = `<circle cx="7" cy="7" r="6" fill="${color}"/>` +
      '<path d="m4.4 7.2 1.8 1.8 3.4-3.6" fill="none" stroke="var(--bg)" stroke-width="1.6" stroke-linecap="round" stroke-linejoin="round"/>';
  } else if (f > 0) {
    const r = 3.4, a = f * 2 * Math.PI;
    const x = (7 + r * Math.sin(a)).toFixed(2), y = (7 - r * Math.cos(a)).toFixed(2);
    inside = `<path d="M7 7L7 ${7 - r}A${r} ${r} 0 ${f > 0.5 ? 1 : 0} 1 ${x} ${y}Z" fill="${color}"/>`;
  }
  const span = el("span", { class: "status-icon" });
  span.innerHTML = `<svg viewBox="0 0 14 14" aria-hidden="true"><circle cx="7" cy="7" r="6" fill="none" stroke="${color}" stroke-width="1.5"${dash}/>${inside}</svg>`;
  return span;
}

const key = (t) => `${t.project}#${t.id}`;

function parseTime(ts) {
  return new Date(ts.replace(" ", "T"));
}

function ago(ts) {
  const s = (Date.now() - parseTime(ts)) / 1000;
  if (s < 60) return tr("just now");
  if (s < 3600) return tr("{0}m ago", Math.floor(s / 60));
  if (s < 86400) return tr("{0}h ago", Math.floor(s / 3600));
  if (s < 86400 * 7) return tr("{0}d ago", Math.floor(s / 86400));
  return parseTime(ts).toLocaleDateString();
}

function timeEl(ts) {
  return el("time", { datetime: ts, title: parseTime(ts).toLocaleString() }, ago(ts));
}

function escapeHtml(s) {
  return s.replace(/&/g, "&amp;").replace(/</g, "&lt;").replace(/>/g, "&gt;").replace(/"/g, "&quot;");
}

// Code spans, links and bare URLs are swapped for placeholders as soon as they are
// rendered, so later rules never see, or rewrite, the HTML they produced. The input
// is already HTML-escaped, so it holds no tags or raw quotes of its own.
function inline(s) {
  const saved = [];
  const keep = (html) => `\u0000${saved.push(html) - 1}\u0000`;
  const link = (url, text) => keep(`<a href="${url}" target="_blank" rel="noopener">${text}</a>`);
  s = s
    .replace(/\u0000/g, "")
    .replace(/`([^`]+)`/g, (_, code) => keep(`<code>${code}</code>`))
    .replace(/\[([^\]]+)\]\(((?:https?:\/\/|mailto:|file:\/\/\/)[^\s)\u0000]+)\)/g, (_, text, url) => link(url, emphasis(text)))
    .replace(/(^|[\s(])(https?:\/\/[^\s)\u0000]+)/g, (_, before, url) => before + link(url, url));
  const restore = (html) => html.replace(/\u0000(\d+)\u0000/g, (_, i) => restore(saved[i]));
  return restore(emphasis(s));
}

function emphasis(s) {
  return s
    .replace(/\*\*(.+?)\*\*/g, "<strong>$1</strong>")
    .replace(/(^|[^\w*])\*(?!\s)(.+?)\*(?!\w)/g, "$1<em>$2</em>")
    .replace(/(^|[^\w])_(?!\s)(.+?)_(?!\w)/g, "$1<em>$2</em>")
    .replace(/~~(.+?)~~/g, "<del>$1</del>");
}

// The cells of a table row, with its outer pipes dropped. An escaped \| stays in its cell.
function tableCells(line) {
  const row = line.trim().replace(/\\\|/g, "\u0001").replace(/^\|/, "").replace(/\|$/, "");
  return row.split("|").map((c) => c.trim().replace(/\u0001/g, "|"));
}

const TABLE_DELIMITER = /^\s*\|?\s*:?-+:?\s*(\|\s*:?-+:?\s*)*\|?\s*$/;

function markdown(src) {
  const lines = escapeHtml(src.replace(/\r\n?/g, "\n")).split("\n");
  const out = [];
  let para = [];
  let list = null;
  const flushPara = () => { if (para.length) out.push(`<p>${inline(para.join("<br>"))}</p>`); para = []; };
  const flushList = () => {
    if (!list) return;
    out.push(`<${list.type}>${list.items.join("")}</${list.type}>`);
    list = null;
  };
  for (let i = 0; i < lines.length; i++) {
    const line = lines[i];
    let m;
    if ((m = line.match(/^\s*```/))) {
      flushPara(); flushList();
      const code = [];
      while (++i < lines.length && !/^\s*```/.test(lines[i])) code.push(lines[i]);
      out.push(`<pre><code>${code.join("\n")}</code></pre>`);
    } else if ((m = line.match(/^(#{1,6})\s+(.*)$/))) {
      flushPara(); flushList();
      const n = Math.min(m[1].length, 4);
      out.push(`<h${n}>${inline(m[2])}</h${n}>`);
    } else if (line.includes("|") && TABLE_DELIMITER.test(lines[i + 1] || "")
               && tableCells(line).length === tableCells(lines[i + 1]).length) {
      // A table may follow a line of text directly, as on GitHub. Its body runs to the
      // first line with no pipe; short rows get empty cells and long ones are cut.
      flushPara(); flushList();
      const align = tableCells(lines[++i]).map((d) =>
        d.startsWith(":") ? (d.endsWith(":") ? "center" : "left") : d.endsWith(":") ? "right" : "");
      const row = (tag, cells) => "<tr>" + align.map((a, j) =>
        `<${tag}${a ? ` style="text-align:${a}"` : ""}>${inline(cells[j] || "")}</${tag}>`).join("") + "</tr>";
      const body = [];
      while (i + 1 < lines.length && lines[i + 1].includes("|")) body.push(row("td", tableCells(lines[++i])));
      out.push(`<div class="table"><table><thead>${row("th", tableCells(line))}</thead>`
        + (body.length ? `<tbody>${body.join("")}</tbody>` : "") + "</table></div>");
    } else if ((m = line.match(/^\s*(?:[-*+]|(\d+)[.)])\s+(.*)$/))) {
      flushPara();
      const type = m[1] ? "ol" : "ul";
      if (list && list.type !== type) flushList();
      list = list || { type, items: [] };
      const check = m[2].match(/^\[( |x|X)\]\s+(.*)$/);
      list.items.push(check
        ? `<li class="check"><input type="checkbox" disabled ${check[1] === " " ? "" : "checked"}> ${inline(check[2])}</li>`
        : `<li>${inline(m[2])}</li>`);
    } else if ((m = line.match(/^&gt;\s?(.*)$/))) {
      flushPara(); flushList();
      const quote = [m[1]];
      while (i + 1 < lines.length && (m = lines[i + 1].match(/^&gt;\s?(.*)$/))) { quote.push(m[1]); i++; }
      out.push(`<blockquote>${inline(quote.join("<br>"))}</blockquote>`);
    } else if (/^\s*([-*_])(\s*\1){2,}\s*$/.test(line)) {
      flushPara(); flushList();
      out.push("<hr>");
    } else if (line.trim() === "") {
      flushPara(); flushList();
    } else if (list && /^\s{2,}\S/.test(line)) {
      list.items[list.items.length - 1] = list.items[list.items.length - 1].replace(/<\/li>$/, ` ${inline(line.trim())}</li>`);
    } else {
      flushList();
      para.push(line);
    }
  }
  flushPara(); flushList();
  return out.join("");
}

// Loads can overlap (a poll, a drop, an edit); only the newest one may render,
// so a slow older reply never undoes what is on screen.
let boardRequest = 0;

async function loadBoard() {
  const request = ++boardRequest;
  try {
    const done = state.allDone ? "&done=all" : "";
    const q = filterQuery();
    const board = await api("GET", "/board?project=" + encodeURIComponent(state.project) + done
                            + (q ? "&q=" + encodeURIComponent(q) : ""));
    if (request !== boardRequest) return;
    state.board = board;
    document.documentElement.dataset.hide = (board.hidden_properties || []).join(" ");
    state.changes = board.changes;
    $("#offline").hidden = true;
    render();
  } catch (e) {
    if (request !== boardRequest) return;
    if (state.project !== "all" && /no project/.test(e.message)) {
      setProject("all");
    } else {
      toast(e.message);
    }
  }
}

function updateHash() {
  const hash = "#" + state.project + (state.task ? "/" + state.task : "");
  if (location.hash !== hash) history.replaceState(null, "", hash);
}

function readHash() {
  let hash = location.hash.slice(1);
  try { hash = decodeURIComponent(hash); } catch {}
  const [slug, task] = hash.split("/");
  return { slug: slug || null, task: Number(task) || null };
}

function setProject(slug) {
  state.project = slug;
  localStorageSet("kanzo.project", slug);
  updateHash();
  state.layout = "";
  loadBoard();
}

function render() {
  renderNav();
  renderHeader();
  renderColumns();
}

function renderNav() {
  // Redrawing would take the field away from someone renaming a project.
  if (renaming && document.contains(renaming.input)) return;
  // Nor while dragging a project: it would cancel the drag.
  if (movingProject) return;
  const { projects } = state.board;
  const open = projects.reduce((n, p) => n + p.open, 0);
  const link = (slug, name, count, glyph) =>
    el("a", { href: "#" + slug, title: name, draggable: "false", class: state.project === slug ? "active" : null,
              onclick: (e) => { e.preventDefault(); setProject(slug); } },
      glyph,
      el("span", { class: "name" }, name),
      el("span", { class: "count" }, String(count)));
  const item = (p) => renaming && renaming.slug === p.slug
    ? el("div", { class: "item renaming" }, icon("folder", projectColor(p.slug)), renaming.input)
    : el("div", { class: "item" + (state.menu === p.slug ? " menu-open" : ""), draggable: "true", "data-slug": p.slug },
    link(p.slug, p.name, p.open, icon("folder", projectColor(p.slug))),
    el("button", { class: "more", title: tr("More"), "aria-label": tr("More for {0}", p.name), "aria-haspopup": "menu",
                   onclick: (e) => toggleProjectMenu(p, e.currentTarget) }, icon("more")));
  $("#nav").replaceChildren(
    link("all", tr("All projects"), open, icon("board")),
    ...(projects.length ? [el("div", { class: "label" }, tr("Projects"))] : []),
    ...projects.map(item),
  );
}

// `state.menu` holds the slug, so the row keeps its highlight while polling redraws the sidebar.
function toggleProjectMenu(p, button) {
  if (!toggleMenu(p.slug)) return;
  button.closest(".item").classList.add("menu-open");
  const swatches = el("div", { class: "swatches", role: "group", "aria-label": tr("Color") });
  const drawSwatches = () => swatches.replaceChildren(...Object.entries(PROJECT_COLORS).map(([name, hex]) => {
    const current = projectColorName(p.slug) === name;
    return el("button", { class: "swatch", style: `background:${hex}`, title: tr(name), "aria-label": tr(name),
                          "aria-pressed": String(current), onclick: () => pickColor(p.slug, name).then(drawSwatches) },
      current ? icon("check") : null);
  }));
  drawSwatches();
  const repos = el("div", { class: "repos" });
  const drawRepos = (list, focus) => {
    const input = el("input", { class: "field", placeholder: tr("Link a repo: owner/repo"), "aria-label": tr("Link a GitHub repo"),
                                autocomplete: "off", spellcheck: "false" });
    input.addEventListener("keydown", async (e) => {
      if (e.key !== "Enter" || !input.value.trim()) return;
      const q = await changeRepo(p.slug, "POST", input.value);
      if (q) drawRepos(q.repos, true);
    });
    repos.replaceChildren(...list.map((r) => el("div", { class: "repo" }, el("span", { class: "name" }, r),
      el("button", { class: "unlink", title: tr("Unlink {0}", r), "aria-label": tr("Unlink {0}", r),
                     onclick: async () => { const q = await changeRepo(p.slug, "DELETE", r); if (q) drawRepos(q.repos); } },
        icon("close")))), input);
    if (focus) input.focus();
  };
  drawRepos(p.repos);
  const rename = el("button", { onclick: () => startRename(p) }, icon("rename"), tr("Rename"));
  const remove = el("button", { class: "danger", onclick: () => { closeMenu(); deleteProject(p); } },
    icon("trash"), tr("Delete project"));
  const menu = el("div", { class: "menu", role: "menu", tabindex: "-1" },
    el("div", { class: "label" }, tr("Color")), swatches, el("div", { class: "label" }, tr("Repos")), repos,
    el("div", { class: "sep" }), rename, remove);
  document.body.append(menu);
  const r = button.getBoundingClientRect();
  const below = r.bottom + 4 + menu.offsetHeight <= innerHeight;
  menu.style.top = `${below ? r.bottom + 4 : r.top - 4 - menu.offsetHeight}px`;
  menu.style.left = `${Math.max(8, Math.min(r.left, innerWidth - menu.offsetWidth - 8))}px`;
  // Focus the menu itself, so Tab reaches its buttons without one looking selected.
  menu.focus();
}

let renaming = null;
let movingProject = null;
const projectLine = el("div", { class: "drop-line" });
const projectAfter = (y) => [...$("#nav").querySelectorAll("[data-slug]")]
  .find((i) => { const r = i.getBoundingClientRect(); return y < r.top + r.height / 2; }) || null;

$("#nav").addEventListener("dragstart", (e) => {
  // Or it's text dragged out of the field renaming a project.
  movingProject = e.target.dataset?.slug;
  if (!movingProject) return;
  e.dataTransfer.effectAllowed = "move";
  e.dataTransfer.setData("text/plain", movingProject);
});
$("#nav").addEventListener("dragover", (e) => {
  if (!movingProject) return;
  e.preventDefault();
  $("#nav").insertBefore(projectLine, projectAfter(e.clientY));
});
$("#nav").addEventListener("dragleave", (e) => { if (!$("#nav").contains(e.relatedTarget)) projectLine.remove(); });
$("#nav").addEventListener("drop", async (e) => {
  if (!movingProject) return;
  e.preventDefault();
  const before = projectAfter(e.clientY)?.dataset.slug || null;
  try { await api("POST", `/projects/${encodeURIComponent(movingProject)}/move`, { before }); } catch (err) { toast(err.message); }
  loadBoard();
});
$("#nav").addEventListener("dragend", () => {
  movingProject = null;
  projectLine.remove();
  renderNav();
});

function startRename(p) {
  closeMenu();
  const input = el("input", { class: "field", "aria-label": tr("Rename {0}", p.name), spellcheck: "false", autocomplete: "off" });
  input.value = p.name;
  renaming = { slug: p.slug, input };
  let finished = false;
  const finish = async (save) => {
    if (finished) return;
    finished = true;
    const name = input.value.trim();
    renaming = null;
    if (save && name && name !== p.name) {
      try { await api("PATCH", `/projects/${encodeURIComponent(p.slug)}`, { name }); } catch (err) { toast(err.message); }
    }
    loadBoard();
  };
  input.addEventListener("keydown", (e) => {
    if (e.key === "Enter") { e.preventDefault(); finish(true); }
    else if (e.key === "Escape") { e.stopPropagation(); finish(false); }
  });
  input.addEventListener("blur", () => finish(true));
  renderNav();
  input.focus();
  input.select();
}

async function changeRepo(slug, method, repo) {
  try {
    const p = await api(method, `/projects/${encodeURIComponent(slug)}/repos`, { repo });
    loadBoard();
    return p;
  } catch (err) { toast(err.message); return null; }
}

async function pickColor(slug, color) {
  try {
    await api("PATCH", `/projects/${encodeURIComponent(slug)}`, { color });
    await loadBoard();
  } catch (err) { toast(err.message); }
}

function toggleMenu(name) {
  const wasOpen = state.menu === name;
  closeMenu();
  if (wasOpen) return false;
  state.menu = name;
  return true;
}

function closeMenu() {
  if (!state.menu) return;
  state.menu = null;
  document.querySelector(".menu")?.remove();
  document.querySelectorAll(".nav .menu-open").forEach((i) => i.classList.remove("menu-open"));
  $("#settings").classList.remove("open");
  $("#network-menu").hidden = true;
  $("#network").classList.remove("open");
}

// A click on a menu's own button is left to toggle it.
document.addEventListener("mousedown", (e) => {
  if (!e.target.closest(".menu, .nav .more, #settings, #network, #network-menu")) closeMenu();
});
document.addEventListener("mousedown", (e) => {
  if (state.task && e.button === 0 && !e.target.closest("#panel, .card, .menu, .search-dialog")) closeTask();
});
window.addEventListener("resize", closeMenu);
window.addEventListener("blur", closeMenu);
$("#nav").addEventListener("scroll", closeMenu);

function currentProject() {
  return state.board.projects.find((p) => p.slug === state.project) || null;
}

function renderHeader() {
  const p = currentProject();
  const { projects, tasks } = state.board;
  const header = $("#header");
  $("#new-task").hidden = !projects.length;
  $("#human-only").hidden = !projects.length || isPropertyHidden("human");
  $("#filter-box").hidden = !projects.length;
  $("#view").hidden = !projects.length;
  $("#view").setAttribute("aria-pressed", String(isList()));
  $("#filters").replaceChildren(...(projects.length ? savedFilters() : []).map((f, i) =>
    el("span", { class: "chip" }, el("span", {}, queryNodes(f)),
      el("button", { type: "button", title: tr(`Remove "{0}"`, f), "aria-label": tr("Remove the filter {0}", f),
                     onclick: () => setFilters(savedFilters().filter((_, j) => j !== i)) }, icon("close")))));
  $("#human-only").setAttribute("aria-pressed", String(isHumanOnly()));
  if (!projects.length) { header.replaceChildren(); return; }
  const open = (n) => plural(n, "open task");
  if (!p) {
    const count = projects.reduce((n, p) => n + p.open, 0);
    header.replaceChildren(el("h1", {}, tr("All projects")),
      el("span", { class: "sub" }, tr("{0} in {1}", open(count), plural(projects.length, "project"))));
    return;
  }
  header.replaceChildren(
    el("h1", {}, p.name),
    el("span", { class: "sub" }, open(p.open)),
    ...p.repos.map((r) => el("a", { class: "repo-link", href: `https://github.com/${r}`, target: "_blank", rel: "noopener",
                                    title: tr("Open {0} on GitHub", r) }, icon("github"), r)),
  );
}

async function deleteProject(p) {
  const tasks = p.total ? tr(" and its {0}, with their comments", plural(p.total, "task")) : "";
  if (!confirm(tr("Delete project {0}{1}? This cannot be undone.", p.slug, tasks))) return;
  try {
    await api("DELETE", `/projects/${encodeURIComponent(p.slug)}`);
    if (panelTask?.project === p.slug) { fresh = null; closeTask(); }
    if (state.project === p.slug) setProject("all"); else loadBoard();
  } catch (err) { toast(err.message); }
}

function renderColumns() {
  const { statuses, projects, tasks } = state.board;
  const columns = $("#columns");
  const inList = isList() && projects.length > 0;
  columns.hidden = inList;
  $("#list").hidden = $("#list-tools").hidden = !inList;
  if (!projects.length) {
    state.layout = "";
    columns.style.display = "flex";
    columns.replaceChildren(el("div", { class: "empty" },
      el("h1", {}, tr("Welcome to Kanzo")),
      el("p", {}, tr("Create your first project in the sidebar. Link it to a GitHub repo and agents working in that repo will find it on their own.")),
      el("button", { class: "btn primary", onclick: showNewProject }, tr("New project"))));
    return;
  }
  columns.style.display = "";

  // Build the columns once per layout so a refresh never wipes a half-typed task.
  const layout = statuses.join(",") + "|" + state.project;
  if (state.layout !== layout) {
    state.layout = layout;
    columns.replaceChildren(...statuses.map((status) => {
      const collapsible = status === "backlog" || status === "done";
      const col = el("section", { class: "column", "data-status": status },
        el("h2", {},
          statusIcon(status), tr(status),
          el("span", { class: "count" }),
          collapsible ? el("button", { class: "collapse", onclick: (e) => { e.stopPropagation(); setCollapsed(status, !isCollapsed(status)); } }) : null),
        el("div", { class: "cards" }),
      );
      wireDrop(col, col.querySelector(".cards"), status);
      if (collapsible) col.addEventListener("click", () => { if (isCollapsed(status)) setCollapsed(status, false); });
      return col;
    }));
    showCollapsed();
    $("#bulk-status").replaceChildren(el("option", { value: "", hidden: true }, "Move to…"), ...statuses.map((s) => el("option", { value: s }, s)));
  }

  // During a drag the DOM is being moved by hand; refresh once it ends.
  if (state.dragging || state.sweeping || state.settling) return;
  const all = state.project === "all";
  const humanOnly = isHumanOnly();
  const query = filterQuery();
  $("#filter").classList.toggle("on", query.length > 0);
  // Filtered, the server's count of older done tasks says nothing about human ones.
  const filtered = humanOnly || query.length > 0;
  const hiddenDone = state.allDone ? 0 : state.board.done_hidden || 0;
  const more = hiddenDone
    ? el("button", { class: "ghost more", onclick: () => { state.allDone = true; loadBoard(); } },
         filtered ? tr("Show older") : tr("Show {0} older", hiddenDone))
    : null;
  if (inList) {
    renderList(tasks.filter((t) => !humanOnly || t.human), all, more);
  } else {
    for (const status of statuses) {
      const col = columns.querySelector(`[data-status="${CSS.escape(status)}"]`);
      const mine = tasks.filter((t) => t.status === status && (!humanOnly || t.human));
      const last = status === statuses[statuses.length - 1];
      const count = mine.length + (!last || filtered ? 0 : hiddenDone);
      col.querySelector("h2 .count").textContent = count ? String(count) : "";
      col.querySelector(".cards").replaceChildren(...(last && more ? [more] : []), ...mine.map((t) => card(t, all)));
    }
  }
  highlight("filter", inList ? $("#list") : columns, wordsOf(query));
  for (const id of [...state.selected]) {
    if (!document.querySelector(`.card[data-id="${id}"]`)) state.selected.delete(id);
  }
  showSelection();
}

function isList() {
  return localStorageGet("kanzo.view") === "list";
}

// Only the view on screen is rendered: every refresh would otherwise build both.
$("#view").addEventListener("click", () => {
  localStorageSet("kanzo.view", isList() ? "" : "list");
  state.layout = "";
  render();
});
for (const [select, name] of [[$("#list-sort"), "kanzo.sort"], [$("#list-group"), "kanzo.group"]]) {
  select.value = localStorageGet(name) || "";
  select.addEventListener("change", () => { localStorageSet(name, select.value); renderColumns(); });
}

function renderList(tasks, all, more) {
  const { statuses, projects } = state.board;
  const by = $("#list-sort").value, group = $("#list-group").value;
  const sorted = !by ? tasks : [...tasks].sort((a, b) =>
    by === "id" ? a.id - b.id : by === "title" ? a.title.localeCompare(b.title) : b[by].localeCompare(a[by]));
  const groups = new Map((group === "status" ? statuses : group === "project" ? projects.map((p) => p.slug) : []).map((g) => [g, []]));
  for (const t of sorted) {
    for (const g of group === "tags" ? (t.tags.length ? t.tags : [""]) : [group ? t[group] : ""]) {
      if (!groups.has(g)) groups.set(g, []);
      groups.get(g).push(t);
    }
  }
  const names = group === "tags" ? [...groups.keys()].sort((a, b) => !a - !b || a.localeCompare(b)) : [...groups.keys()];
  $("#list").replaceChildren(...names.flatMap((g) => !groups.get(g).length ? [] : [
    group ? el("h2", { style: group === "status" ? "text-transform:capitalize" : null }, g || "No tag", el("span", { class: "count" }, String(groups.get(g).length))) : null,
    ...groups.get(g).map((t) => card(t, all, true)),
  ]).filter(Boolean), ...(more ? [more] : []));
}

function showSelection() {
  $("#bulk").hidden = !state.selected.size;
  $("#bulk-count").textContent = `${state.selected.size} selected`;
}

function setSelected(id, on) {
  if (on) state.selected.add(id); else state.selected.delete(id);
  // Grouped by tag, a task can have several rows.
  document.querySelectorAll(`.card[data-id="${id}"]`).forEach((n) => n.classList.toggle("selected", on));
}

function clearSelection() {
  state.selected.forEach((id) => setSelected(id, false));
  showSelection();
}

// Closes the panel: one or the other, so the bar can't cover it.
function toggleSelected(id) {
  if (state.task) closeTask();
  setSelected(id, !state.selected.has(id));
  showSelection();
}

// Drag on empty board space to select, with ⌘ to add to what's selected. Cards are measured once, when
// the drag starts, so moving the mouse never forces a layout.
function sweep(down) {
  const marquee = el("div", { class: "marquee" });
  const kept = new Set(down.metaKey || down.ctrlKey ? state.selected : []);
  let cards, x, y, frame = 0;
  const paint = () => {
    frame = 0;
    const x1 = Math.min(down.clientX, x), x2 = Math.max(down.clientX, x), y1 = Math.min(down.clientY, y), y2 = Math.max(down.clientY, y);
    marquee.style.cssText = `left:${x1}px;top:${y1}px;width:${x2 - x1}px;height:${y2 - y1}px`;
    for (const { id, r, list } of cards) {
      // A card scrolled out of its column can't be under the box.
      const hit = r.right > x1 && r.left < x2 && r.bottom > Math.max(y1, list.top) && r.top < Math.min(y2, list.bottom);
      if ((hit || kept.has(id)) !== state.selected.has(id)) setSelected(id, !state.selected.has(id));
    }
    showSelection();
  };
  const move = (e) => {
    x = e.clientX; y = e.clientY;
    if (!cards) {
      if (Math.hypot(x - down.clientX, y - down.clientY) < 4) return;
      state.sweeping = true;
      document.body.append(marquee);
      cards = [...$("#columns").querySelectorAll(".column:not(.collapsed) .card")].map((n) =>
        ({ id: +n.dataset.id, r: n.getBoundingClientRect(), list: n.parentNode.getBoundingClientRect() }));
    }
    if (!frame) frame = requestAnimationFrame(paint);
  };
  const up = () => {
    cancelAnimationFrame(frame);
    document.removeEventListener("mousemove", move);
    document.removeEventListener("mouseup", up);
    marquee.remove();
    state.sweeping = false;
    if (cards) renderColumns();
    else if (!kept.size) clearSelection();
  };
  document.addEventListener("mousemove", move);
  document.addEventListener("mouseup", up);
}

$("#columns").addEventListener("mousedown", (e) => {
  const onScrollbar = e.offsetX > e.target.clientWidth || e.offsetY > e.target.clientHeight;
  if (e.button || onScrollbar || e.target.closest(".card, button, a, .collapsed")) return;
  e.preventDefault();
  sweep(e);
});

async function changeSelected(change) {
  const ids = state.board.tasks.filter((t) => state.selected.has(t.id)).map((t) => t.id);
  try { await api("POST", "/tasks/bulk", { ids, ...change }); } catch (err) { toast(err.message); }
  loadBoard();
}

$("#bulk").addEventListener("keydown", (e) => {
  const { name, value } = e.target;
  if (e.key === "Escape") { e.stopPropagation(); e.target.blur(); clearSelection(); }
  else if (e.key === "Enter" && name && value.trim()) { changeSelected({ [name]: value }); e.target.value = ""; }
});
$("#bulk-status").addEventListener("change", (e) => {
  changeSelected({ status: e.target.value });
  e.target.value = "";
});
$("#bulk-delete").addEventListener("click", () => {
  if (confirm(`Delete ${plural(state.selected.size, "task")}, with comments and images? This cannot be undone.`)) changeSelected({ delete: true });
});
$("#bulk-clear").addEventListener("click", clearSelection);

function savedFilters() {
  try { return JSON.parse(localStorageGet("kanzo.filters")) || []; } catch { return []; }
}

function setFilters(filters) {
  localStorageSet("kanzo.filters", JSON.stringify(filters));
  loadBoard();
}

function filterQuery() {
  return [...savedFilters(), $("#filter").value].join(" ").trim();
}

const anyText = () => true;
const isNumber = (v) => /^[+-]?\d+$/.test(v);
const isDate = (v) => /^\d{4}-\d{2}-\d{2}$/.test(v);
const inRange = (side) => (v) => {
  const ends = v.split("..");
  return ends.length > 1 ? ends.length === 2 && ends.every(side) : side(v.replace(/^(>=|<=|>|<)/, ""));
};
const isField = (v) => ["description", "tags", "url", "comments", "images"].includes(v);
// What the server takes as each qualifier's value: anything else makes it a plain word.
const QUALIFIERS = new Map([
  ["tag", anyText], ["status", anyText], ["project", anyText], ["title", anyText], ["description", anyText],
  ["url", anyText], ["comment", anyText], ["id", inRange(isNumber)], ["comments", inRange(isNumber)],
  ["images", inRange(isNumber)], ["created", inRange(isDate)], ["updated", inRange(isDate)],
  ["is", (v) => v === "human" || v === "waiting"], ["has", isField], ["no", isField],
]);

// A part of a query, read as the server reads it. Null for a word.
function qualifierIn(part) {
  const match = !part.startsWith('"') && part.replace(/"/g, "").toLowerCase().match(/^(-?)([^:]+):(.*)$/);
  const valid = match && QUALIFIERS.get(match[2]);
  if (!valid) return null;
  const [, negated, name, value] = match;
  // With no value yet it's still being typed; with a wrong one, it's a word.
  return !value || valid(value) ? { negated: negated === "-", name, value } : null;
}

// The text to highlight on cards: words, phrases, and what tag:, title: and description: look for.
function wordsOf(query) {
  const words = [];
  for (const part of query.match(/(?:[^\s"]+|"[^"]*"?)+/g) || []) {
    const qualifier = qualifierIn(part);
    if (!qualifier) words.push(part.replace(/"/g, "").toLowerCase());
    else if (!qualifier.negated && ["tag", "title", "description"].includes(qualifier.name)) words.push(qualifier.value);
  }
  return words.filter(Boolean);
}

// A query as text, with its qualifiers marked. Keeps every character, for .query-mirror to line up.
function queryNodes(query) {
  return (query.match(/\s+|(?:[^\s"]+|"[^"]*"?)+/g) || []).flatMap((part) => {
    if (!qualifierIn(part)) return part;
    const key = part.indexOf(":") + 1;
    return [el("span", { class: "qualifier-key" }, part.slice(0, key)), part.slice(key)];
  });
}

// An input can't color part of its text, so a copy behind it shows it, and the input keeps the caret.
function paintQuery(input) {
  const mirror = input.previousElementSibling;
  mirror.firstElementChild.replaceChildren(...queryNodes(input.value));
  mirror.scrollLeft = input.scrollLeft;
}

for (const input of document.querySelectorAll(".query input")) {
  for (const type of ["input", "scroll", "keyup", "click"]) input.addEventListener(type, () => paintQuery(input));
}

function highlight(name, root, words) {
  if (!window.Highlight) return;
  const re = new RegExp(words.map((w) => w.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("|"), "gi");
  const ranges = [];
  for (const node of words.length ? root.querySelectorAll(".title, .desc, .tag") : []) {
    const text = node.firstChild;
    for (const m of text?.nodeType === Node.TEXT_NODE ? text.data.matchAll(re) : []) {
      const range = new Range();
      range.setStart(text, m.index);
      range.setEnd(text, m.index + m[0].length);
      ranges.push(range);
    }
  }
  CSS.highlights.set(name, new Highlight(...ranges));
}

function summary(t, ...extra) {
  const chips = [...extra.filter(Boolean), ...(isPropertyHidden("tags") ? [] : t.tags.map((tag) => el("span", { class: "tag" }, tag)))];
  return [
    t.description ? el("span", { class: "desc" }, plainText(t.description)) : null,
    chips.length ? el("span", { class: "tags" }, chips) : null,
  ];
}

// Only on cards: search rows are buttons, and a link can't sit inside one.
function urlChip(t) {
  if (!t.url || isPropertyHidden("url")) return null;
  return el("a", { class: "tag url", href: t.url, target: "_blank", rel: "noopener", title: t.url, draggable: "false",
                   onclick: (e) => e.stopPropagation() },
    icon("url"), t.url.split("/")[2].replace(/^www\./, ""));
}

// Only on cards, like the URL. Most tasks have no waiting_on at all, to keep the board's reply small.
function waitChip(t) {
  return t.waiting_on ? el("span", { class: "waits" }, icon("waits"), `Waits on ${t.waiting_on.map((id) => `#${id}`).join(", ")}`) : null;
}

// A board on an older version sends no hidden_properties.
function isPropertyHidden(property) {
  return (state.board?.hidden_properties || []).includes(property);
}

function isHumanOnly() {
  return localStorageGet("kanzo.humanOnly") === "1" && !isPropertyHidden("human");
}

function setHumanOnly(humanOnly) {
  localStorageSet("kanzo.humanOnly", humanOnly ? "1" : "0");
  render();
}

function isCollapsed(status) {
  return localStorageGet(`kanzo.${status}Collapsed`) !== "0";
}

function setCollapsed(status, collapsed) {
  localStorageSet(`kanzo.${status}Collapsed`, collapsed ? "1" : "0");
  showCollapsed();
}

function showCollapsed() {
  const columns = $("#columns");
  const cols = [...columns.querySelectorAll(".column")];
  if (!cols.length) return;
  for (const col of cols) {
    const button = col.querySelector(".collapse");
    if (!button) continue;
    const { status } = col.dataset;
    const collapsed = isCollapsed(status);
    col.classList.toggle("collapsed", collapsed);
    // The arrows point the way the column folds: to the right edge, or for backlog,
    // to the left one.
    const left = status === "backlog";
    button.replaceChildren(icon(collapsed !== left ? "expand" : "collapse"));
    button.title = collapsed ? tr("Show {0}", tr(status)) : tr("Collapse {0}", tr(status));
    button.setAttribute("aria-label", button.title);
  }
  columns.style.gridTemplateColumns = cols.map((c) => (c.classList.contains("collapsed") ? "48px" : "minmax(260px, 1fr)")).join(" ");
}

function plainText(md) {
  return md.split("\n")
    .map((l) => l.replace(/^\s*(#{1,6}|>|[-*+]|\d+[.)])\s+/, "").replace(/^\[[ xX]\]\s+/, ""))
    .filter((l) => l.trim() && !/^\s*```/.test(l))
    .join(" ")
    .replace(/\*\*|__|`/g, "")
    .replace(/\s+/g, " ")
    .trim();
}

// Dragging a selected card takes the others along: they fly behind it, and spread out where it lands.
const MOTION = { duration: 180, easing: "ease-out" };
const MOST_FLYING = 30;
const STACK_TOP = 8;

function calm() {
  return matchMedia("(prefers-reduced-motion: reduce)").matches;
}

function inView(card) {
  const r = card.getBoundingClientRect(), list = card.parentNode.getBoundingClientRect();
  return r.width > 0 && r.bottom > list.top && r.top < list.bottom;
}

// The browser draws the dragged image, and only from what's on the page when the drag starts.
function stackImage(card, count) {
  const stack = el("div", { class: "stack", style: `width:${card.offsetWidth}px` },
    card.cloneNode(true), el("span", { class: "stack-count" }, String(count)));
  document.body.append(stack);
  requestAnimationFrame(() => stack.remove());
  return stack;
}

// A copy above the board, so that its column can't clip it.
function fly(card, from, to, fade) {
  const { width, height } = card.getBoundingClientRect();
  const ghost = card.cloneNode(true);
  ghost.classList.add("flying");
  ghost.style.cssText = `left:${from.left}px;top:${from.top}px;width:${width}px;height:${height}px`;
  document.body.append(ghost);
  const end = `translate(${to.left - from.left}px,${to.top - from.top}px)`;
  const flight = ghost.animate([{ transform: "none", opacity: 1 }, { transform: end, opacity: fade ? 0 : 1 }], MOTION);
  return new Promise((done) => { flight.onfinish = () => { ghost.remove(); done(); }; });
}

// Makes a change to the board, then glides the cards it moved from where they were.
function shift(change) {
  const tops = new Map(calm() ? [] : [...$("#columns").querySelectorAll(".card:not(.gathered)")].filter(inView)
    .map((c) => [c, c.getBoundingClientRect().top]));
  change();
  tops.forEach((top, c) => {
    const r = c.getBoundingClientRect();
    if (r.width && r.top !== top) c.animate([{ transform: `translateY(${top - r.top}px)` }, { transform: "none" }], MOTION);
  });
}

function gather(group, grabbed) {
  const to = grabbed.getBoundingClientRect();
  const others = group.filter((c) => c !== grabbed);
  if (!calm()) others.filter(inView).slice(0, MOST_FLYING).forEach((c) => fly(c, c.getBoundingClientRect(), to, true));
  shift(() => {
    grabbed.classList.add("dragging");
    others.forEach((c) => c.classList.add("dragging", "gathered"));
  });
}

// `place` puts the cards where they land. A refresh waits, as it would replace what's flying.
async function spread(group, grabbed, from, place) {
  state.settling = true;
  shift(() => {
    place();
    group.forEach((c) => c.classList.remove("dragging", "gathered"));
  });
  const others = calm() ? [] : group.filter((c) => c !== grabbed && inView(c)).slice(0, MOST_FLYING);
  const flying = calm() || !inView(grabbed) ? others : [...others, grabbed];
  await Promise.all(flying.map((c) => {
    const landed = fly(c, from, c.getBoundingClientRect(), false);
    c.classList.add("landing");
    return landed.then(() => c.classList.remove("landing"));
  }));
  state.settling = false;
}

// In the list, a row: no drag to reorder, since a sorted list has no one position to drop into.
function card(t, showProject, row) {
  const node = el("div", { class: "card" + (row ? " row" : "") + (state.task === t.id ? " open" : "") + (t.waiting_on ? " waiting" : "") + (state.selected.has(t.id) ? " selected" : ""),
                           draggable: row ? null : "true", "data-id": t.id,
                           onclick: (e) => { if (e.metaKey || e.ctrlKey) toggleSelected(t.id); else openTask(t.id); } },
    row ? statusIcon(t.status) : null,
    el("div", { class: "head" },
      el("div", { class: "title" }, t.title),
      t.human ? el("span", { class: "human-mark", title: tr("Human task: agents skip it") }, icon("human")) : null,
      el("span", { class: "num", title: tr("Copy {0}", key(t)), onclick: (e) => { e.stopPropagation(); copyKey(key(t)); } },
        showProject ? el("span", { class: "dot", style: `background:${projectColor(t.project)}` }) : null, `#${t.id}`)),
    ...summary(t, waitChip(t), urlChip(t)).slice(row ? 1 : 0));
  node.addEventListener("dragstart", (e) => {
    state.dragging = t.id;
    e.dataTransfer.effectAllowed = "move";
    e.dataTransfer.setData("text/plain", String(t.id));
    const selected = state.selected.has(t.id) ? [...$("#columns").querySelectorAll(".card.selected")] : [];
    const group = state.group = selected.length > 1 ? selected : null;
    if (group) {
      const r = node.getBoundingClientRect();
      state.grip = { x: e.clientX - r.left, y: e.clientY - r.top };
      e.dataTransfer.setDragImage(stackImage(node, group.length), state.grip.x, state.grip.y + STACK_TOP);
    }
    requestAnimationFrame(() => { if (group) gather(group, node); else node.classList.add("dragging"); });
  });
  node.addEventListener("dragend", async () => {
    node.classList.remove("dragging");
    document.querySelectorAll(".drop-line").forEach((d) => d.remove());
    state.dragging = null;
    const { dropped, group } = state;
    state.dropped = false;
    state.group = null;
    // A drop reloads once the move is saved; a cancelled drag puts the card back.
    if (dropped) return;
    if (group) await spread(group, node, node.getBoundingClientRect(), () => {});
    loadBoard();
  });
  return node;
}

function wireDrop(col, list, status) {
  const line = el("div", { class: "drop-line" });
  const cardAfter = (y) => {
    const cards = [...list.querySelectorAll(".card:not(.dragging)")];
    return cards.find((c) => { const r = c.getBoundingClientRect(); return y < r.top + r.height / 2; }) || null;
  };
  col.addEventListener("dragover", (e) => {
    if (!state.dragging) return;
    e.preventDefault();
    e.dataTransfer.dropEffect = "move";
    const after = cardAfter(e.clientY);
    if (after) list.insertBefore(line, after); else list.append(line);
  });
  col.addEventListener("dragleave", (e) => {
    if (!col.contains(e.relatedTarget)) line.remove();
  });
  col.addEventListener("drop", async (e) => {
    e.preventDefault();
    const id = state.dragging;
    if (!id) return;
    const after = cardAfter(e.clientY);
    const dragged = document.querySelector(`.card[data-id="${id}"]`);
    const spot = line.parentNode === list ? line : null;
    state.dropped = true;
    if (fresh === id) fresh = null;
    const before = after ? Number(after.dataset.id) : null;
    const { group } = state;
    if (group) {
      const from = { left: e.clientX - state.grip.x, top: e.clientY - state.grip.y };
      const ids = group.map((c) => Number(c.dataset.id));
      const landed = spread(group, dragged, from, () => {
        group.forEach((c) => list.insertBefore(c, spot));
        line.remove();
      });
      try {
        await api("POST", "/tasks/move", { ids, status, before });
        if (ids.includes(state.task)) refreshTask();
      } catch (err) { toast(err.message); }
      await landed;
      loadBoard();
      return;
    }
    if (dragged) list.insertBefore(dragged, spot);
    line.remove();
    try {
      await api("POST", `/tasks/${id}/move`, { status, before });
      if (state.task === id) refreshTask();
    } catch (err) { toast(err.message); }
    loadBoard();
  });
}

const NEW_TITLE = "New task";
let fresh = null;

async function newTask() {
  const { projects } = state.board;
  if (!projects.length) { toast(tr("Create a project first")); return; }
  const last = localStorageGet("kanzo.newTaskProject");
  const project = state.project !== "all" ? state.project
    : projects.some((p) => p.slug === last) ? last : projects[0].slug;
  try {
    const task = await api("POST", "/tasks", { project, title: NEW_TITLE });
    await openTask(task.id);
    fresh = task.id;
    loadBoard();
    $("#p-title").focus({ preventScroll: true });
    $("#p-title").select();
  } catch (err) { toast(err.message); }
}

function dropFresh() {
  if (!fresh) return;
  const id = fresh;
  fresh = null;
  api("DELETE", `/tasks/${id}`).then(loadBoard, () => {});
}

// A reload or a closed tab may never blur the field being typed in.
window.addEventListener("pagehide", () => {
  const active = document.activeElement;
  const fields = !panelTask ? null
    : active === $("#p-desc") && active.value.trim() !== panelTask.description ? { description: active.value }
    : active === $("#p-tags") && cleanTags(active.value).join() !== panelTask.tags.join() ? { tags: cleanTags(active.value).join() }
    : null;
  if (fields) {
    fresh = null;
    fetch(`/api/tasks/${panelTask.id}`, { method: "PATCH", keepalive: true,
                                          headers: { "Content-Type": "application/json" }, body: JSON.stringify(fields) });
  }
  if (fresh) fetch(`/api/tasks/${fresh}`, { method: "DELETE", keepalive: true });
});

function showNewProject() {
  $("#project-dialog").hidden = false;
  $("#new-project [name=slug]").focus();
}

function hideNewProject() {
  $("#new-project").reset();
  $("#project-dialog").hidden = true;
}

$("#new-task").addEventListener("click", newTask);
$("#human-only").addEventListener("click", () => setHumanOnly(!isHumanOnly()));
let filterTimer;
$("#filter").addEventListener("input", () => { clearTimeout(filterTimer); filterTimer = setTimeout(loadBoard, 120); });
$("#filter").addEventListener("keydown", (e) => {
  const box = e.target, text = box.value.trim();
  if (e.key === "Escape") { e.stopPropagation(); box.value = ""; paintQuery(box); loadBoard(); box.blur(); }
  else if (e.key === "Enter" && text) { box.value = ""; paintQuery(box); setFilters([...savedFilters(), text]); }
  else if (e.key === "Backspace" && !box.value && savedFilters().length) setFilters(savedFilters().slice(0, -1));
});
$("#new-project-toggle").addEventListener("click", showNewProject);
$("#new-project-cancel").addEventListener("click", hideNewProject);
$("#new-project").addEventListener("submit", async (e) => {
  e.preventDefault();
  const f = new FormData(e.target);
  try {
    const p = await api("POST", "/projects", { slug: f.get("slug"), name: f.get("name"), repo: f.get("repo") || null });
    hideNewProject();
    setProject(p.slug);
  } catch (err) { toast(err.message); }
});

const panel = $("#panel");
let panelTask = null;
let panelWaits = "";
// Unsent comments by task, so closing the panel doesn't lose one.
const drafts = new Map();

const taskLink = (t) => el("button", { type: "button", class: "tag", title: key(t), onclick: () => pick(t) },
  statusIcon(t.status), el("span", {}, t.title));

async function openTask(id) {
  clearSelection();
  leavePanelField();
  if (fresh !== id) dropFresh();
  state.task = id;
  document.querySelectorAll(".card.open").forEach((c) => c.classList.remove("open"));
  document.querySelector(`.card[data-id="${id}"]`)?.classList.add("open");
  $("#p-comment").value = drafts.get(id) || "";
  pending = [];
  renderPending();
  fit($("#p-comment"));
  updateHash();
  await refreshTask();
  if (state.task !== id) return;
  panel.classList.add("shown");
  panel.setAttribute("aria-hidden", "false");
}

// Blurring a field saves it, so do that before the panel lets go of a task.
function leavePanelField() {
  if (panel.contains(document.activeElement)) document.activeElement.blur();
}

function closeTask() {
  leavePanelField();
  dropFresh();
  state.task = null;
  panelTask = null;
  updateHash();
  panel.classList.remove("shown");
  panel.setAttribute("aria-hidden", "true");
  document.querySelectorAll(".card.open").forEach((c) => c.classList.remove("open"));
}

async function refreshTask() {
  const id = state.task;
  if (!id) return;
  let data;
  try {
    data = await api("GET", `/tasks/${id}`);
  } catch (e) {
    if (state.task === id && /no task/.test(e.message)) { toast(tr("That task was deleted")); closeTask(); }
    return;
  }
  // Another task may have taken the panel meanwhile.
  if (state.task !== id) return;
  // A board on an older version sends neither list.
  const { task, comments, images, waits_on = [], blocks = [] } = data;
  panelTask = task;
  panelWaits = waits_on.map(key).join(", ");
  $("#p-key").textContent = key(task);
  $("#p-key").title = tr("Copy {0}", key(task));
  // Never overwrite what is being typed or picked.
  if (!$("#p-project").contains(document.activeElement)) renderProjectPick(task);
  for (const [field, value] of [[$("#p-title"), task.title], [$("#p-desc"), task.description], [$("#p-tags"), task.tags.join(", ")], [$("#p-url"), task.url || ""], [$("#p-waits"), panelWaits]]) {
    if (document.activeElement !== field) { field.value = value; fit(field); }
  }
  showDesc();
  const status = $("#p-status");
  const statuses = state.board ? state.board.statuses : [task.status];
  status.replaceChildren(...statuses.map((s) => el("option", { value: s }, s)));
  if (!statuses.includes(task.status)) status.append(el("option", { value: task.status }, task.status));
  status.value = task.status;
  $("#p-human").checked = task.human;
  $("#p-waits-links").replaceChildren(...waits_on.map(taskLink));
  $("#p-blocks").replaceChildren(...blocks.map(taskLink));
  $("#p-blocks-row").hidden = !blocks.length;
  $("#p-created").replaceChildren(timeEl(task.created_at));
  $("#p-updated").replaceChildren(timeEl(task.updated_at));
  $("#p-comment-count").textContent = comments.length ? String(comments.length) : "";
  $("#p-images").replaceChildren(...images.filter((i) => i.comment_id === null).map((i) =>
    thumb(i.file, async () => {
      try { await api("DELETE", `/images/${i.id}`); refreshTask(); } catch (err) { toast(err.message); }
    })));
  $("#p-comments").replaceChildren(...comments.map((c) => {
    const body = el("div", { class: "md" });
    body.innerHTML = markdown(c.body);
    const attached = images.filter((i) => i.comment_id === c.id);
    return el("div", { class: "comment" },
      el("div", { class: "who" }, el("b", {}, c.author), " · ", timeEl(c.created_at),
        el("button", { class: "remove", title: tr("Delete comment"), "aria-label": tr("Delete comment"),
                       onclick: () => deleteComment(c) }, icon("trash"))),
      c.body ? body : null,
      attached.length ? el("div", { class: "thumbs" }, attached.map((i) => thumb(i.file))) : null);
  }));
}

async function deleteComment(c) {
  if (!confirm(tr("Delete this comment? This cannot be undone."))) return;
  try {
    await api("DELETE", `/comments/${c.id}`);
    await refreshTask();
    loadBoard();
  } catch (err) { toast(err.message); }
}

function renderProjectPick(task) {
  const projects = state.board ? state.board.projects.map((p) => p.slug) : [];
  if (!projects.includes(task.project)) projects.push(task.project);
  const select = el("select", { title: tr("Project"), "aria-label": tr("Project") },
    ...projects.map((slug) => el("option", { value: slug }, slug)));
  select.value = task.project;
  select.addEventListener("change", () => {
    localStorageSet("kanzo.newTaskProject", select.value);
    patchTask({ project: select.value });
  });
  $("#p-project").replaceChildren(el("label", { class: "tag project-pick" },
    el("span", { class: "dot", style: `background:${projectColor(task.project)}` }), select));
}

function fit(t) {
  if (t.tagName !== "TEXTAREA") return;
  t.style.height = "auto";
  t.style.height = t.scrollHeight + "px";
}

async function patchTask(fields) {
  if (!panelTask) return;
  if (fresh === panelTask.id) fresh = null;
  try {
    await api("PATCH", `/tasks/${panelTask.id}`, fields);
    await refreshTask();
    loadBoard();
  } catch (e) { toast(e.message); refreshTask(); }
}

function cancelField(e, saved) {
  e.stopPropagation();
  if (fresh && fresh === state.task) { closeTask(); return; }
  e.target.value = saved;
  fit(e.target);
  e.target.blur();
}

// For writing worth keeping: Esc leaves the field, and leaving it saves. Closing a new task
// saves what was typed, and drops the task if nothing was.
function leaveField(e) {
  e.stopPropagation();
  if (fresh && fresh === state.task) closeTask();
  else e.target.blur();
}

// A page can't open a file: link, so the board does, on the computer it runs on.
panel.addEventListener("click", async (e) => {
  const link = e.target.closest('a[href^="file:///"]');
  if (!link) return;
  e.preventDefault();
  try {
    const path = decodeURIComponent(link.getAttribute("href").slice(7)).replace(/^\/([a-zA-Z]:)/, "$1");
    await api("POST", "/open", { path });
  } catch (err) { toast(err.message); }
});

// "" is the default width. A drag saves and refits only when it ends: browsers send one move a frame.
function setPanelWidth(width, save) {
  panel.style.setProperty("--panel-width", width);
  if (!save) return;
  localStorageSet("kanzo.panelWidth", width);
  [$("#p-title"), $("#p-desc"), $("#p-comment")].forEach(fit);
}

setPanelWidth(localStorageGet("kanzo.panelWidth") || "");
$("#p-wide").addEventListener("click", () => setPanelWidth(panel.style.getPropertyValue("--panel-width") === "100vw" ? "" : "100vw", true));
$("#p-edge").addEventListener("dblclick", () => setPanelWidth("", true));
$("#p-edge").addEventListener("pointerdown", (down) => {
  const edge = down.target;
  edge.setPointerCapture(down.pointerId);
  edge.onpointermove = (e) => setPanelWidth(`${Math.max(360, innerWidth - e.clientX)}px`);
  edge.onpointerup = () => {
    edge.onpointermove = edge.onpointerup = null;
    setPanelWidth(panel.style.getPropertyValue("--panel-width"), true);
  };
});

$("#p-close").addEventListener("click", closeTask);
$("#p-key").addEventListener("click", () => { if (panelTask) copyKey(key(panelTask)); });
for (const field of [$("#p-title"), $("#p-desc")]) field.addEventListener("input", () => fit(field));

$("#p-title").addEventListener("keydown", (e) => {
  if (e.key === "Enter" && (e.metaKey || e.ctrlKey)) { e.preventDefault(); closeTask(); }
  else if ((e.key === "Enter" || e.key === "Tab") && !e.shiftKey) { e.preventDefault(); editDesc(); }
  else if (e.key === "Escape") cancelField(e, panelTask ? panelTask.title : "");
});
$("#p-title").addEventListener("blur", (e) => {
  if (!panelTask) return;
  const v = e.target.value.trim();
  if (v && v !== panelTask.title) patchTask({ title: v });
  else { e.target.value = panelTask.title; fit(e.target); }
});

$("#p-desc").addEventListener("keydown", (e) => {
  if (e.key === "Enter" && (e.metaKey || e.ctrlKey)) { e.preventDefault(); closeTask(); }
  else if (e.key === "Escape") leaveField(e);
});
$("#p-desc").addEventListener("blur", (e) => {
  if (panelTask && e.target.value.trim() !== panelTask.description) patchTask({ description: e.target.value });
  showDesc();
});

// Empty, the textarea stays, so its placeholder has something to click.
function showDesc() {
  const desc = $("#p-desc"), view = $("#p-desc-view");
  const editing = document.activeElement === desc || !desc.value.trim();
  desc.hidden = !editing;
  view.hidden = editing;
  // Hidden, it measured 0, as it does when the task before this one had a description.
  if (editing) fit(desc);
  else view.innerHTML = markdown(desc.value);
}

function editDesc() {
  const desc = $("#p-desc");
  $("#p-desc-view").hidden = true;
  desc.hidden = false;
  fit(desc);
  desc.focus();
}

// A click on a link opens it instead of editing.
$("#p-desc-view").addEventListener("mousedown", (e) => { if (e.target.closest("a")) e.preventDefault(); });
$("#p-desc-view").addEventListener("focus", editDesc);

$("#p-status").addEventListener("change", (e) => patchTask({ status: e.target.value }));
$("#p-human").addEventListener("change", (e) => patchTask({ human: e.target.checked }));

let pending = [];

function imagesIn(transfer) {
  if (!transfer) return [];
  const fromItems = transfer.items ? [...transfer.items].filter((i) => i.kind === "file").map((i) => i.getAsFile()) : [];
  return (fromItems.length ? fromItems : [...(transfer.files || [])]).filter((f) => f && f.type.startsWith("image/"));
}

async function uploadImage(image) {
  return (await api("POST", "/images", image)).file;
}

async function attachImages(images, toComment) {
  if (!panelTask) return;
  const task = panelTask.id;
  try {
    for (const image of images) {
      const file = await uploadImage(image);
      if (toComment) {
        pending.push(file);
        renderPending();
      } else {
        await api("POST", `/tasks/${task}/images`, { file });
        if (fresh === task) fresh = null;
      }
    }
    if (!toComment) refreshTask();
  } catch (err) { toast(err.message); }
}

function thumb(file, onRemove) {
  return el("div", { class: "thumb" },
    el("a", { href: `/images/${file}`, target: "_blank", rel: "noopener" }, el("img", { src: `/images/${file}`, alt: tr("Attached image") })),
    onRemove ? el("button", { type: "button", class: "remove-image", title: tr("Remove image"), "aria-label": tr("Remove image"), onclick: onRemove }, icon("close")) : null);
}

function renderPending() {
  $("#p-pending").replaceChildren(...pending.map((file, i) => thumb(file, () => { pending.splice(i, 1); renderPending(); })));
  $("#p-send").disabled = !$("#p-comment").value.trim() && !pending.length;
}

for (const field of [$("#p-title"), $("#p-desc"), $("#p-comment")]) {
  field.addEventListener("paste", (e) => {
    const images = imagesIn(e.clipboardData);
    if (!images.length) return;
    e.preventDefault();
    attachImages(images, field.id === "p-comment");
  });
}
for (const [zone, toComment] of [[$("#p-desc").closest(".section"), false], [$("#p-comment-form"), true]]) {
  zone.addEventListener("dragover", (e) => {
    if (!e.dataTransfer || ![...e.dataTransfer.types].includes("Files")) return;
    e.preventDefault();
    zone.classList.add("dropping");
  });
  zone.addEventListener("dragleave", (e) => { if (!zone.contains(e.relatedTarget)) zone.classList.remove("dropping"); });
  zone.addEventListener("drop", (e) => {
    zone.classList.remove("dropping");
    const images = imagesIn(e.dataTransfer);
    if (!images.length) return;
    e.preventDefault();
    attachImages(images, toComment);
  });
}

const cleanTags = (text) => [...new Set(text.split(",").map((t) => t.trim().replace(/\s+/g, " ").toLowerCase()).filter(Boolean))];
$("#p-tags").addEventListener("keydown", (e) => {
  if (e.key === "Enter") { e.preventDefault(); e.target.blur(); }
  else if (e.key === "Escape") leaveField(e);
});
$("#p-tags").addEventListener("blur", (e) => {
  if (!panelTask) return;
  const tags = cleanTags(e.target.value);
  if (tags.join(",") !== panelTask.tags.join(",")) patchTask({ tags: tags.join(", ") });
  else e.target.value = panelTask.tags.join(", ");
});

$("#p-url").addEventListener("keydown", (e) => {
  if (e.key === "Enter") { e.preventDefault(); e.target.blur(); }
  else if (e.key === "Escape") cancelField(e, panelTask ? panelTask.url || "" : "");
});
$("#p-url").addEventListener("blur", (e) => {
  if (!panelTask) return;
  const url = e.target.value.trim();
  if (url !== (panelTask.url || "")) patchTask({ url });
  else e.target.value = url;
});

let waitResults = [], waitSelected = 0, waitRequest = 0, waitTimer;

function closeWaits() {
  clearTimeout(waitTimer);
  waitRequest++;
  waitResults = [];
  $("#p-waits-list").hidden = true;
  $("#p-waits").setAttribute("aria-expanded", "false");
}

// The search behind the filter, for what's typed after the last comma.
async function findWaits() {
  const parts = $("#p-waits").value.split(",").map((part) => part.trim());
  const words = parts[parts.length - 1];
  if (!words || !panelTask) { closeWaits(); return; }
  const { id, project } = panelTask;
  const request = ++waitRequest;
  try {
    const found = await api("GET", "/search?q=" + encodeURIComponent(`project:${project} -status:done ${words}`));
    if (request !== waitRequest) return;
    waitResults = found.filter((t) => t.id !== id && !parts.includes(key(t))).slice(0, 8);
    waitSelected = 0;
    const list = $("#p-waits-list");
    list.replaceChildren(...waitResults.map((t, i) => el("button", { type: "button", tabindex: "-1", role: "option", class: "result" + (i === 0 ? " selected" : ""),
                                                                     onmousemove: () => selectWait(i),
                                                                     // Before the field loses focus, which would save half a word.
                                                                     onmousedown: (e) => { e.preventDefault(); pickWait(t); } },
      el("span", { class: "mono" }, `#${t.id}`),
      el("span", { class: "title" }, t.title),
      el("span", { class: "meta" }, t.status))));
    if (!waitResults.length) list.append(el("span", { class: "note" }, "No tasks match."));
    list.hidden = false;
    $("#p-waits").setAttribute("aria-expanded", "true");
    highlight("search", list, wordsOf(words));
  } catch (err) { toast(err.message); }
}

function selectWait(i) {
  const rows = $("#p-waits-list").querySelectorAll(".result");
  waitSelected = (i + rows.length) % rows.length;
  rows.forEach((r, j) => r.classList.toggle("selected", j === waitSelected));
  rows[waitSelected].scrollIntoView({ block: "nearest" });
}

function pickWait(t) {
  const input = $("#p-waits");
  const kept = input.value.split(",").slice(0, -1).map((part) => part.trim()).filter(Boolean);
  input.value = [...kept, key(t)].join(", ") + ", ";
  closeWaits();
}

$("#p-waits").addEventListener("input", () => { clearTimeout(waitTimer); waitTimer = setTimeout(findWaits, 120); });
$("#p-waits").addEventListener("keydown", (e) => {
  const listed = waitResults.length > 0;
  if ((e.key === "ArrowDown" || e.key === "ArrowUp") && listed) { e.preventDefault(); selectWait(waitSelected + (e.key === "ArrowDown" ? 1 : -1)); }
  else if (e.key === "Enter") { e.preventDefault(); if (listed) pickWait(waitResults[waitSelected]); else e.target.blur(); }
  else if (e.key === "Escape" && !$("#p-waits-list").hidden) { e.stopPropagation(); closeWaits(); }
  else if (e.key === "Escape") cancelField(e, panelWaits);
});
$("#p-waits").addEventListener("blur", (e) => {
  closeWaits();
  e.target.value = e.target.value.replace(/[,\s]+$/, "");
  if (panelTask && e.target.value !== panelWaits) patchTask({ waits_on: e.target.value });
});

$("#p-comment-form").addEventListener("submit", async (e) => {
  e.preventDefault();
  const body = $("#p-comment").value.trim();
  if ((!body && !pending.length) || !panelTask) return;
  try {
    await api("POST", `/tasks/${panelTask.id}/comments`, { body, images: pending });
    if (fresh === panelTask.id) fresh = null;
    drafts.delete(panelTask.id);
    $("#p-comment").value = "";
    pending = [];
    renderPending();
    fit($("#p-comment"));
    await refreshTask();
    loadBoard();
  } catch (err) { toast(err.message); }
});
$("#p-comment").addEventListener("input", (e) => {
  if (e.target.value) drafts.set(state.task, e.target.value); else drafts.delete(state.task);
  $("#p-send").disabled = !e.target.value.trim() && !pending.length;
  fit(e.target);
});
$("#p-comment").addEventListener("keydown", (e) => {
  if (e.key === "Enter" && (e.metaKey || e.ctrlKey)) { e.preventDefault(); $("#p-comment-form").requestSubmit(); }
});

$("#p-delete").addEventListener("click", async () => {
  if (!panelTask) return;
  const extra = panelTask.comments ? tr(" and its {0}", plural(panelTask.comments, "comment")) : "";
  if (!confirm(tr("Delete {0}{1}? This cannot be undone.", key(panelTask), extra))) return;
  try {
    if (fresh === panelTask.id) fresh = null;
    await api("DELETE", `/tasks/${panelTask.id}`);
    closeTask();
    loadBoard();
  } catch (err) { toast(err.message); }
});

let results = [], selected = 0, searchRequest = 0, searchTimer;

function openSearch() {
  closeMenu();
  $("#search-dialog").hidden = false;
  $("#search").value = "";
  paintQuery($("#search"));
  $("#search").focus();
  runSearch();
}

function closeSearch() {
  $("#search-dialog").hidden = true;
}

async function runSearch() {
  const q = $("#search").value.trim();
  const request = ++searchRequest;
  try {
    const found = await api("GET", "/search?q=" + encodeURIComponent(q));
    if (request !== searchRequest) return;
    results = found;
    selected = 0;
    $("#search-results").replaceChildren(
      ...(results.length ? [el("div", { class: "label" }, q ? tr("Tasks") : tr("Recent"))] : []),
      ...results.map((t, i) => el("button", { type: "button", class: "result" + (i === 0 ? " selected" : ""),
                                             onmousemove: () => select(i), onclick: () => pick(t) },
        el("span", { class: "dot", style: `background:${projectColor(t.project)}` }),
        el("span", { class: "body" }, el("span", { class: "title" }, t.title), ...summary(t)),
        el("span", { class: "meta" },
          t.comments ? el("span", { class: "comments", title: plural(t.comments, "comment") },
                          icon("comment"), String(t.comments)) : null,
          el("span", { class: "mono" }, key(t)), t.status))),
      ...(results.length ? [] : [el("div", { class: "note" }, q ? tr("No tasks match.") : tr("No tasks yet."))]));
    highlight("search", $("#search-results"), wordsOf(q));
  } catch (err) { toast(err.message); }
}

function select(i) {
  const rows = $("#search-results").querySelectorAll(".result");
  if (!rows.length) return;
  selected = (i + rows.length) % rows.length;
  rows.forEach((r, j) => r.classList.toggle("selected", j === selected));
  rows[selected].scrollIntoView({ block: "nearest" });
}

function pick(t) {
  closeSearch();
  if (state.project !== "all" && state.project !== t.project) setProject(t.project);
  openTask(t.id);
}

$("#search-open").addEventListener("click", openSearch);
$("#search").addEventListener("input", () => { clearTimeout(searchTimer); searchTimer = setTimeout(runSearch, 120); });
$("#search").addEventListener("keydown", (e) => {
  if (e.key === "ArrowDown" || e.key === "ArrowUp") { e.preventDefault(); select(selected + (e.key === "ArrowDown" ? 1 : -1)); }
  else if (e.key === "Enter" && results[selected]) { e.preventDefault(); pick(results[selected]); }
});

document.addEventListener("keydown", (e) => {
  const typing = e.target.closest("input, textarea, select");
  const plain = !typing && !e.metaKey && !e.ctrlKey;
  if (e.key === "Escape" && state.menu) { closeMenu(); return; }
  // Only in the installed app: in a browser tab, these keys belong to the browser.
  if (/^\d$/.test(e.key) && (e.metaKey || e.ctrlKey) && state.board && matchMedia("(display-mode: standalone)").matches) {
    const { projects } = state.board;
    const slug = e.key === "0" ? "all" : projects[e.key === "9" ? projects.length - 1 : e.key - 1]?.slug;
    e.preventDefault();
    if (slug) setProject(slug);
  }
  if (e.key === "Escape" && state.task && !typing) closeTask();
  if (e.key === "Escape" && !typing) clearSelection();
  if ((e.key === "/" && plain) || (e.key === "k" && (e.metaKey || e.ctrlKey))) {
    e.preventDefault();
    openSearch();
  }
  if (e.key === "n" && plain && !$("#new-task").hidden) { e.preventDefault(); newTask(); }
  if (e.key === "f" && plain && !$("#filter-box").hidden) { e.preventDefault(); $("#filter").focus(); }
  if (e.key === "h" && plain && !$("#human-only").hidden) { e.preventDefault(); setHumanOnly(!isHumanOnly()); }
});

window.addEventListener("hashchange", () => {
  const { slug, task } = readHash();
  if ((slug || "all") !== state.project) setProject(slug || "all");
  if (task && task !== state.task) openTask(task);
  else if (!task && state.task) closeTask();
});

async function poll() {
  if (document.hidden) return schedulePoll();
  try {
    const { changes, guests = null } = await api("GET", "/changes");
    $("#offline").hidden = true;
    if (guests !== state.guests) {
      state.guests = guests;
      renderNetwork();
      if (state.menu === "network") loadNetwork();
    }
    if (changes !== state.changes) {
      state.changes = changes;
      // Joining or leaving a board, even from the terminal, shows another board.
      loadNetwork();
      await loadBoard();
      if (state.task) refreshTask();
    }
  } catch {
    $("#offline").hidden = false;
  }
  schedulePoll();
}

// The next check starts only after this one finished, so a slow or busy server
// never piles up requests.
function schedulePoll() {
  setTimeout(poll, 2000);
}

let syncing = false;

async function syncNow(keep) {
  if (syncing) return;
  syncing = true;
  toast(tr("Syncing…"));
  let diverged = null;
  try {
    toast((await api("POST", "/sync", { keep })).message);
    loadBoard();
  } catch (err) {
    if (err.data?.diverged) diverged = err.data;
    else toast(err.message);
  }
  syncing = false;
  if (diverged) chooseSide(diverged);
}

function chooseSide({ first }) {
  const repo = repoName(syncRemote);
  if (first) {
    if (confirm(tr("{0} already has a board. Use it on this machine?\n\nThis machine's current board is kept in kanzo.db.bak.", repo))) syncNow("remote");
    else if (confirm(tr("Replace the board in {0} with this machine's board instead?", repo))) syncNow("local");
    return;
  }
  if (confirm(tr("This machine and another one both changed the board since the last sync, and boards can't be merged.\n\nKeep THIS machine's board, and overwrite the other one?"))) syncNow("local");
  else if (confirm(tr("Take the other machine's board instead? This machine's board is kept in kanzo.db.bak."))) syncNow("remote");
}

let syncRemote = null;

function repoName(url) {
  return (url || "").replace(/\.git$/, "").split(/[/:]/).filter(Boolean).slice(-2).join("/");
}

async function loadSyncRemote() {
  try { syncRemote = (await api("GET", "/sync")).remote; } catch {}
  return syncRemote;
}

async function sync() {
  closeMenu();
  if (await loadSyncRemote()) syncNow();
  else openSettings("sync");
}

$("#sync-form").addEventListener("submit", async (e) => {
  e.preventDefault();
  const save = $("#sync-save");
  save.disabled = true;
  save.textContent = tr("Checking…");
  try {
    syncRemote = (await api("PUT", "/sync", { url: $("#sync-url").value })).remote;
    closeSettings();
    syncNow();
  } catch (err) {
    $("#sync-error").textContent = err.message;
    $("#sync-error").hidden = false;
  }
  save.disabled = false;
  save.textContent = tr("Save and sync");
});

async function loadNetwork() {
  let net = null;
  try { net = await api("GET", "/network"); } catch {}
  setNet(net);
}

function setNet(net) {
  state.net = net;
  // A joined board's count comes from its host, with each check of /api/changes.
  if (net && net.state !== "joined") state.guests = net.state === "sharing" ? net.guests.length : null;
  renderNetwork();
}

function renderNetwork() {
  const net = state.net;
  const button = $("#network");
  // Opened from another computer's browser, the board can't tell which computer it is on.
  const away = !net && !["localhost", "127.0.0.1", "[::1]"].includes(location.hostname);
  const hosted = net?.state === "joined" || away;
  const host = net?.host?.replace(/:4242$/, "") || location.hostname;
  const n = state.guests;
  const shared = hosted || n != null;
  const computers = n == null ? "" : tr(": {0} connected", plural(n, "computer"));
  button.replaceChildren(icon("share"), ...(shared ? [tr("Shared")] : []), ...(shared && n != null ? [el("span", { class: "count" }, String(n))] : []));
  button.classList.toggle("on", shared);
  button.title = hosted ? tr("Shared by {0}{1}", host, computers)
    : shared ? tr("Shared on your network{0}", computers) : tr("Share this board, or join another");
  if (state.menu === "network") renderNetworkMenu();
}

// While a change is on its way, the popover keeps showing it.
let networkBusy = false;

function renderNetworkMenu() {
  if (networkBusy) return;
  const net = state.net;
  const now = net?.state;
  const share = $("#network-share");
  share.checked = now === "sharing";
  share.disabled = !net || now === "joined";
  $("#network-share-row").classList.toggle("disabled", share.disabled);
  $("#network-share-note").textContent =
    !net ? tr("Only the computer with the board can change this")
    : now === "sharing" ? tr("Other computers join it at {0}", net.addresses.length > 1 ? tr("the address for their network")
      : net.addresses.length ? tr("this address") : tr("this computer's IP address"))
    : now === "joined" ? tr("Leave the shared board to share this one")
    : tr("Other computers on your network can use it");
  const addresses = $("#network-addresses");
  addresses.hidden = now !== "sharing" || !net.addresses.length;
  if (now === "sharing") {
    addresses.replaceChildren(...net.addresses.map((a) => el("button", { class: "copy", type: "button", title: tr("Copy the address") },
      el("span", { class: "mono" }, a), icon("copy"))));
  }
  const guests = $("#network-guests");
  guests.hidden = now !== "sharing";
  if (now === "sharing") {
    guests.replaceChildren(
      el("span", { class: "label" }, net.guests.length ? tr("Connected now") : tr("No other computer is connected yet")),
      ...net.guests.map((g) => el("div", { class: "guest" },
        el("span", { class: "dot" }), g.name || tr("A browser"), el("span", { class: "address" }, g.address))));
  }
  const joined = now === "joined";
  $("#network-join").hidden = !net;
  $("#network-join").classList.toggle("disabled", now === "sharing");
  $("#network-join-text").replaceChildren(joined ? tr("Connected to {0}", net.host) : tr("Join a shared board"),
    el("span", {}, joined ? tr("This computer uses that board")
      : now === "sharing" ? tr("Stop sharing to join another board")
      : tr("The address the sharing computer shows")));
  $("#network-leave").hidden = !joined;
  $("#network-join-fields").hidden = joined;
  $("#join-host").disabled = $("#join-submit").disabled = now === "sharing";
}

function toggleNetworkMenu() {
  if (!toggleMenu("network")) return;
  const button = $("#network");
  button.classList.add("open");
  $("#network-error").hidden = true;
  renderNetworkMenu();
  const menu = $("#network-menu");
  menu.hidden = false;
  const r = button.getBoundingClientRect();
  menu.style.top = `${r.bottom + 6}px`;
  menu.style.left = `${Math.max(8, Math.min(r.right - menu.offsetWidth, innerWidth - menu.offsetWidth - 8))}px`;
  loadNetwork();
}

// Joining or leaving shows another board, so the page reloads.
async function changeNetwork(change) {
  $("#network-error").hidden = true;
  networkBusy = true;
  let net = state.net;
  try {
    net = await api("POST", "/network", change);
    if (change.action === "join" || change.action === "leave") { location.reload(); return; }
  } catch (err) {
    $("#network-error").textContent = err.message;
    $("#network-error").hidden = false;
  }
  networkBusy = false;
  setNet(net);
}

$("#network").addEventListener("click", toggleNetworkMenu);
$("#network-share").addEventListener("change", (e) => changeNetwork({ action: e.target.checked ? "on" : "off" }));
$("#network-addresses").addEventListener("click", (e) => {
  const button = e.target.closest(".copy");
  if (!button) return;
  copyKey(button.textContent);
  button.querySelector(".icon").replaceWith(icon("check"));
  setTimeout(() => button.querySelector(".icon").replaceWith(icon("copy")), 1500);
});
$("#network-leave").addEventListener("click", () => changeNetwork({ action: "leave" }));
$("#network-join").addEventListener("submit", async (e) => {
  e.preventDefault();
  const submit = $("#join-submit");
  submit.disabled = true;
  submit.textContent = tr("Joining…");
  await changeNetwork({ action: "join", host: $("#join-host").value });
  submit.disabled = false;
  submit.textContent = tr("Join");
});

function toggleSettingsMenu() {
  if (!state.board || !toggleMenu("settings")) return;
  const button = $("#settings");
  button.classList.add("open");
  const menu = el("div", { class: "menu", role: "menu", tabindex: "-1" },
    el("button", { onclick: () => openSettings() }, icon("settings"), tr("Settings")),
    el("button", { onclick: () => { closeMenu(); checkForUpdates(); } }, icon("update"), tr("Check for updates"),
      el("span", { class: "value" }, `v${state.board.version}`)),
    el("button", { onclick: () => openSettings("about") }, icon("news"), tr("What's new?")),
    el("button", { onclick: sync }, icon("sync"), tr("Sync")));
  document.body.append(menu);
  const r = button.getBoundingClientRect();
  menu.style.width = `${r.width}px`;
  menu.style.left = `${r.left}px`;
  menu.style.top = `${r.top - 6 - menu.offsetHeight}px`;
  menu.focus();
}

$("#settings").addEventListener("click", toggleSettingsMenu);

function openSettings(section = "board") {
  closeMenu();
  $("#settings-backlog").checked = state.board?.statuses.includes("backlog") ?? false;
  document.querySelectorAll("[data-property]").forEach((box) => { box.checked = !isPropertyHidden(box.dataset.property); });
  $("#sync-url").value = syncRemote || "";
  $("#sync-create").hidden = !!syncRemote;
  $("#sync-error").hidden = true;
  $("#settings-version").textContent = state.board ? `Kanzo ${state.board.version}` : "";
  showSection(section);
  $("#settings-dialog").hidden = false;
  if (section === "sync") $("#sync-url").focus();
  else $("#settings-close").focus();
}

function showSection(section) {
  document.querySelectorAll("#settings-nav button").forEach((b) => b.classList.toggle("active", b.dataset.section === section));
  document.querySelectorAll(".settings-pane").forEach((p) => { p.hidden = p.dataset.section !== section; });
  if (section === "about") loadWhatsNew();
}

// Fetched when first shown, so the page doesn't carry the notes on every load.
async function loadWhatsNew() {
  const notes = $("#whats-new");
  if (notes.firstChild) return;
  try {
    const res = await fetch("/changelog.md");
    if (res.ok) notes.innerHTML = markdown(await res.text());
  } catch {}
}

function closeSettings() {
  $("#settings-dialog").hidden = true;
}

$("#settings-nav").addEventListener("click", (e) => {
  const b = e.target.closest("button");
  if (b) showSection(b.dataset.section);
});
$("#settings-close").addEventListener("click", closeSettings);
for (const [dialog, close] of [[$("#project-dialog"), hideNewProject], [$("#search-dialog"), closeSearch],
                               [$("#settings-dialog"), closeSettings]]) {
  dialog.addEventListener("mousedown", (e) => { if (e.target === dialog) close(); });
  dialog.addEventListener("keydown", (e) => { if (e.key === "Escape") { e.stopPropagation(); close(); } });
}
async function saveSetting(box, fields) {
  try {
    await api("PATCH", "/settings", fields);
    loadBoard();
  } catch (err) { box.checked = !box.checked; toast(err.message); }
}
$("#settings-backlog").addEventListener("change", (e) => saveSetting(e.target, { backlog: e.target.checked }));
$('.settings-pane[data-section="board"]').append(...["Description", "Tags", "URL", "Human", "Waiting", "Created", "Updated", "Comments", "Images"].map((name) => {
  const property = name.toLowerCase();
  return el("label", { class: "setting" }, el("span", { class: "text" }, tr(name)),
    el("span", { class: "switch" },
      el("input", { type: "checkbox", "data-property": property, "aria-label": tr(name),
                    onchange: (e) => saveSetting(e.target, { property, visible: e.target.checked }) }),
      el("span")));
}));
{
  // The language choice, held in localStorage like the filters. Changing it
  // reloads, so every string and <html lang> come back in one piece.
  const pane = $('.settings-pane[data-section="board"]');
  const choose = el("select", { class: "field", style: "width:auto", "aria-label": tr("Language"),
                                onchange: (e) => { localStorageSet("kanzo.lang", e.target.value); location.reload(); } },
    el("option", { value: "en" }, "English"), el("option", { value: "zh-cn" }, "中文"));
  choose.value = LANG;
  pane.insertBefore(el("label", { class: "setting" },
    el("span", { class: "text" }, tr("Language"), el("span", {}, tr("The language of the board's interface"))), choose),
    pane.querySelector(".setting"));
}
$("#settings-update").addEventListener("click", checkForUpdates);

async function checkUpdate() {
  try {
    showUpdate(await api("GET", "/update"));
  } catch {}
}

function showUpdate({ current, available }) {
  const button = $("#update");
  button.hidden = !available;
  if (!available) return;
  $("#update-label").textContent = tr("Update to {0}", available);
  button.title = tr("You have {0}", current);
  button.onclick = () => {
    if (confirm(tr("Update Kanzo from {0} to {1}? The board restarts, which takes a few seconds.", current, available))) installUpdate(available);
  };
}

async function checkForUpdates() {
  toast(tr("Checking for updates…"));
  try {
    const status = await api("GET", "/update?now=1");
    showUpdate(status);
    if (status.available) {
      if (confirm(tr("Kanzo {0} is out (you have {1}). Download and install it now? The board restarts.", status.available, status.current))) {
        installUpdate(status.available);
      }
    } else if (status.latest) {
      toast(tr("Kanzo is up to date ({0})", status.current));
    } else {
      toast(tr("Can't reach GitHub to check for updates"));
    }
  } catch (err) { toast(err.message); }
}

async function installUpdate(available) {
  const button = $("#update");
  button.hidden = false;
  button.disabled = true;
  $("#update-label").textContent = tr("Updating…");
  let res;
  try {
    res = await api("POST", "/update");
  } catch (err) {
    toast(err.message);
    button.disabled = false;
    $("#update-label").textContent = tr("Update to {0}", available);
    return;
  }
  if (!res.restarting) {
    $("#update-label").textContent = tr("Restart Kanzo to finish");
    toast(tr("Kanzo {0} is installed. Restart Kanzo to use it.", available));
    return;
  }
  // The old program answered, then gives way to the new one: wait for it, then reload.
  await new Promise((r) => setTimeout(r, 1500));
  for (let i = 0; i < 60; i++) {
    try { await api("GET", "/update"); break; } catch { await new Promise((r) => setTimeout(r, 1000)); }
  }
  location.reload();
}

function setRail(on) {
  document.documentElement.classList.toggle("rail", on);
  $("#rail").setAttribute("aria-expanded", String(!on));
  localStorageSet("kanzo.rail", on ? "1" : "");
}
setRail(localStorageGet("kanzo.rail") === "1");
document.documentElement.classList.add("ready");
$("#rail").addEventListener("click", () => setRail($("#rail").getAttribute("aria-expanded") === "true"));

// Chromium's browsers ask this only while the board isn't installed; the others never do.
let installPrompt;
window.addEventListener("beforeinstallprompt", (e) => {
  e.preventDefault();
  installPrompt = e;
  $("#install").hidden = false;
});
window.addEventListener("appinstalled", () => { $("#install").hidden = true; });
$("#install").addEventListener("click", () => installPrompt.prompt());

setInterval(() => {
  document.querySelectorAll("time[datetime]").forEach((t) => { t.textContent = ago(t.getAttribute("datetime")); });
}, 60000);

// The dictionary is served separately and only fetched when the board isn't
// English. Everything below waits for it, so the first paint is never half
// translated, and a missing dictionary just stays English.
function loadDictionary() {
  if (LANG === "en") return Promise.resolve();
  return new Promise((resolve) => {
    const script = el("script", { src: `/i18n/${LANG}.js` });
    script.addEventListener("load", () => { try { DICT = ZH_CN; } catch { DICT = null; } resolve(); });
    script.addEventListener("error", () => resolve());
    document.head.append(script);
  });
}

// Text, title, aria-label and placeholder from index.html: an exact dictionary
// match swaps them, anything else keeps the English original. Runs once, while
// the board's dynamic parts are still empty, so no task content is touched.
function walkStatic() {
  if (!DICT) return;
  document.documentElement.lang = LANG === "zh-cn" ? "zh-CN" : "en";
  for (const node of document.body.querySelectorAll("*")) {
    for (const attr of ["title", "aria-label", "placeholder"]) {
      const value = node.getAttribute(attr);
      if (value && DICT[value]) node.setAttribute(attr, DICT[value]);
    }
  }
  const walker = document.createTreeWalker(document.body, NodeFilter.SHOW_TEXT);
  for (let node = walker.nextNode(); node; node = walker.nextNode()) {
    const text = node.textContent, hit = DICT[text.trim()];
    if (hit) node.textContent = text.replace(text.trim(), hit);
  }
}

loadDictionary().then(() => {
  walkStatic();
  checkUpdate();
  setInterval(checkUpdate, 24 * 60 * 60 * 1000);
  loadSyncRemote();
  loadNetwork();
  schedulePoll();
  {
    const { slug, task } = readHash();
    state.project = slug || localStorageGet("kanzo.project") || "all";
    loadBoard().then(() => { if (task) openTask(task); });
  }
});