# All files / src/pages Dashboard.tsx

0% Statements 0/687
0% Branches 0/694
0% Functions 0/276
0% Lines 0/552

Press n or j to go to the next uncovered block, b, p or k for the previous block.

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

     import { useState, useRef, useEffect, useCallback, useMemo } from 'react';
    import ReactMarkdown from 'react-markdown';
    import remarkGfm from 'remark-gfm';
    import { projects as projectsApi, mcps as mcpsApi, agents as agentsApi, discussions as discussionsApi, config as configApi, stats as statsApi } from '../lib/api';
    import { useApi } from '../hooks/useApi';
    import type { Project, AgentDetection, Discussion, AgentType } from '../types/generated';
    import { useT } from '../lib/I18nContext';
    import { UI_LOCALES } from '../lib/i18n';
    import { AGENT_COLORS, agentColor } from '../lib/constants';
    import { McpPage } from './McpPage';
    import { WorkflowsPage } from './WorkflowsPage';
    import {
      Folder, Server, ChevronRight, Cpu, Workflow,
      Plus, Trash2, Search, Zap, Settings, Eye,
      Download, Upload, Check, Loader2, RefreshCw,
      MessageSquare, Send, X, Key, AlertTriangle, Save, Users,
      StopCircle, RotateCcw, Pencil, HardDrive, Play, FileCode, ShieldCheck, ExternalLink, EyeOff,
    } from 'lucide-react';

    type Page = 'projects' | 'mcps' | 'workflows' | 'discussions' | 'settings';

    interface DashboardProps {
      onReset: () => void;
    }

    const isHiddenPath = (path: string) => path.split('/').some(s => s.startsWith('.'));

    const isAiReady = (p: Project) => p.audit_status !== 'NoTemplate';

    /** Agent is usable: locally installed OR available via npx/uvx runtime fallback */
    const isUsable = (a: AgentDetection) => (a.installed || a.runtime_available) && a.enabled;

    const isValidationDisc = (title: string) => title === 'Validation audit AI';

    const AI_CONFIG_LABELS: Record<string, string> = {
      ClaudeMd: 'CLAUDE.md',
      ClauseDir: '.claude/',
      AiDir: '.ai/',
      CursorRules: '.cursorrules',
      ContinueDev: '.continue/',
      McpJson: '.mcp.json',
      Custom: 'custom',
    };

    /** Output languages for agents (sent to backend, not related to UI i18n) */
    const LANGUAGES: { code: string; label: string; flag: string }[] = [
      { code: 'fr', label: 'Français', flag: 'FR' },
      { code: 'en', label: 'English', flag: 'EN' },
      { code: 'es', label: 'Español', flag: 'ES' },
      { code: 'zh', label: '中文', flag: 'ZH' },
      { code: 'br', label: 'Brezhoneg', flag: 'BR' },
    ];

    const ALL_AGENT_MENTIONS: { trigger: string; type: AgentType; label: string }[] = [
      { trigger: '@claude', type: 'ClaudeCode', label: 'Claude Code' },
      { trigger: '@codex', type: 'Codex', label: 'Codex' },
      { trigger: '@vibe', type: 'Vibe', label: 'Vibe' },
      { trigger: '@gemini', type: 'GeminiCli', label: 'Gemini CLI' },
    ];

    // Sort score for project readiness
    const readinessScore = (p: Project) => {
      return p.ai_config.detected ? 0 : 1;
    };

    export function Dashboard({ onReset }: DashboardProps) {
      const { t, locale, setLocale } = useT();
      const [page, setPage] = useState<Page>('projects');
      const [expandedId, setExpandedId] = useState<string | null>(null);
      const [showHidden, setShowHidden] = useState(false);
      const [projectSearch, setProjectSearch] = useState('');
      const [projectDisplayLimit, setProjectDisplayLimit] = useState(20);
      const [installing, setInstalling] = useState<string | null>(null);
      const [activeDiscussionId, setActiveDiscussionId] = useState<string | null>(null);
      const [showNewDiscussion, setShowNewDiscussion] = useState(false);
      const [newDiscTitle, setNewDiscTitle] = useState('');
      const [newDiscAgent, setNewDiscAgent] = useState<AgentType | ''>('');
      const [newDiscProjectId, setNewDiscProjectId] = useState<string>('');
      const [newDiscPrompt, setNewDiscPrompt] = useState('');
      const [newDiscPrefilled, setNewDiscPrefilled] = useState(false);
      const [chatInput, setChatInput] = useState('');
      const [sendingMap, setSendingMap] = useState<Record<string, boolean>>({});
      const [streamingMap, setStreamingMap] = useState<Record<string, string>>({});
      const [editingMsgId, setEditingMsgId] = useState<string | null>(null);
      const [collapsedDiscGroups, setCollapsedDiscGroups] = useState<Set<string>>(new Set());
      const [editingText, setEditingText] = useState('');
      const abortControllers = useRef<Record<string, AbortController>>({});
      const [showDebatePopover, setShowDebatePopover] = useState(false);
      const [debateAgents, setDebateAgents] = useState<AgentType[]>([]);
      const [debateRounds, setDebateRounds] = useState(2);
      // Orchestration live state: per-discussion, tracks current agent streaming and round
      const [orchState, setOrchState] = useState<Record<string, {
        active: boolean;
        round: number | string;
        totalRounds: number;
        currentAgent: string | null;
        agentStreams: { agent: string; agentType: string; round: number | string; text: string; done: boolean }[];
        systemMessages: string[];
      }>>({});
      const [mentionQuery, setMentionQuery] = useState<string | null>(null);
      const [mentionIndex, setMentionIndex] = useState(0);
      const [lastSeenMsgCount, setLastSeenMsgCount] = useState<Record<string, number>>(() => {
        try { return JSON.parse(localStorage.getItem('kronn:lastSeenMsgCount') ?? '{}'); } catch { return {}; }
      });
      const chatInputRef = useRef<HTMLInputElement>(null);
      const [newKeyInputs, setNewKeyInputs] = useState<Record<string, { name: string; value: string }>>({});
      const [addingKeyFor, setAddingKeyFor] = useState<string | null>(null);
      const [tokenVisible, setTokenVisible] = useState<Set<string>>(new Set());
      const chatEndRef = useRef<HTMLDivElement>(null);
      // AI audit state
      const [installingTemplate, setInstallingTemplate] = useState<string | null>(null);
      const [auditAgentChoice, setAuditAgentChoice] = useState<Record<string, AgentType>>({});
      const [auditState, setAuditState] = useState<Record<string, {
        active: boolean;
        step: number;
        totalSteps: number;
        currentFile: string;
      }>>({});

      const { data: projectList, refetch } = useApi(() => projectsApi.list(), []);
      const { data: registry } = useApi(() => mcpsApi.registry(), []);
      const { data: mcpOverviewData, refetch: refetchMcps } = useApi(() => mcpsApi.overview(), []);
      const { data: agentList, refetch: refetchAgents } = useApi(() => agentsApi.detect(), []);
      const { data: discussionList, refetch: refetchDiscussions } = useApi(() => discussionsApi.list(), []);
      const { data: tokenConfig, refetch: refetchTokens } = useApi(() => configApi.getTokens(), []);
      const { data: configLanguage, refetch: refetchLanguage } = useApi(() => configApi.getLanguage(), []);
      const { data: dbInfo, refetch: refetchDbInfo } = useApi(() => configApi.dbInfo(), []);
      const { data: agentAccess, refetch: refetchAgentAccess } = useApi(() => configApi.getAgentAccess(), []);
      const { data: agentUsageData, refetch: refetchAgentUsage } = useApi(() => statsApi.agentUsage(), []);
      const [usageExpanded, setUsageExpanded] = useState<string | null>(null);
      const [usageSearch, setUsageSearch] = useState('');

      const projects = projectList ?? [];
      const mcpRegistry = registry ?? [];
      const mcpOverview = mcpOverviewData ?? { servers: [], configs: [], customized_contexts: [] };
      const agents = agentList ?? [];
      const allDiscussions = discussionList ?? [];

      const activeDiscussion = allDiscussions.find(d => d.id === activeDiscussionId) ?? null;

      // Abort all pending streams on unmount
      useEffect(() => {
        const controllers = abortControllers.current;
        return () => { Object.values(controllers).forEach(c => c.abort()); };
      }, []);

      // Auto-select first installed agent if current selection is invalid (in useEffect, not during render)
      const installedAgentsList = agents.filter(isUsable);
      useEffect(() => {
        if (installedAgentsList.length > 0 && !installedAgentsList.some(a => a.agent_type === newDiscAgent)) {
          setNewDiscAgent(installedAgentsList[0].agent_type);
        }
      }, [installedAgentsList.length, newDiscAgent]);

      const sending = activeDiscussionId ? !!sendingMap[activeDiscussionId] : false;
      const streamingText = activeDiscussionId ? (streamingMap[activeDiscussionId] ?? '') : '';

      const AGENT_MENTIONS = useMemo(() => {
        const activeAgentTypes = new Set(agents.filter(isUsable).map(a => a.agent_type));
        return ALL_AGENT_MENTIONS.filter(m => activeAgentTypes.has(m.type));
      }, [agents]);

      // Track "last seen" message count for unread badges (persisted in localStorage)
      // Only update when the user explicitly selects a discussion (not on data refresh)
      const markDiscussionSeen = useCallback((discId: string, msgCount: number) => {
        setLastSeenMsgCount(prev => {
          const next = { ...prev, [discId]: msgCount };
          localStorage.setItem('kronn:lastSeenMsgCount', JSON.stringify(next));
          return next;
        });
      }, []);

      // Mark active discussion as seen when user selects it or when messages arrive while viewing
      useEffect(() => {
        if (activeDiscussionId && activeDiscussion && !sendingMap[activeDiscussionId]) {
          // Only mark as seen when the agent is NOT currently streaming
          // This way, the badge stays until the user actually "sees" the final result
          markDiscussionSeen(activeDiscussionId, activeDiscussion.messages.length);
        }
      }, [activeDiscussionId, activeDiscussion?.messages.length, sendingMap]);

      // Poll discussions every 10s only when on discussions tab
      useEffect(() => {
        if (page !== 'discussions') return;
        const interval = setInterval(() => {
          refetchDiscussions();
        }, 10000);
        return () => clearInterval(interval);
      }, [refetchDiscussions, page]);

      // Compute total unseen count for tab indicator
      const totalUnseen = useMemo(() => allDiscussions.reduce((acc, disc) => {
        const unseen = disc.messages.length - (lastSeenMsgCount[disc.id] ?? 0);
        return acc + (unseen > 0 && disc.id !== activeDiscussionId ? unseen : 0);
      }, 0), [allDiscussions, lastSeenMsgCount, activeDiscussionId]);

      // Update document title with unread indicator
      useEffect(() => {
        document.title = totalUnseen > 0 ? `(${totalUnseen}) Kronn` : 'Kronn';
      }, [totalUnseen]);

      useEffect(() => {
        chatEndRef.current?.scrollIntoView({ behavior: 'smooth' });
      }, [activeDiscussion?.messages.length, streamingText]);

      const handleDeleteProject = async (id: string) => {
        await projectsApi.delete(id);
        refetch();
      };

      const handleInstallTemplate = async (projectId: string) => {
        setInstallingTemplate(projectId);
        try {
          await projectsApi.installTemplate(projectId);
          refetch();
        } catch (e) {
          console.error('Failed to install template:', e);
        } finally {
          setInstallingTemplate(null);
        }
      };

      const handleLaunchAudit = async (projectId: string) => {
        setAuditState(prev => ({
          ...prev,
          [projectId]: { active: true, step: 0, totalSteps: 10, currentFile: 'Demarrage...' }
        }));
        try {
          const auditAgent = auditAgentChoice[projectId] ?? agents.filter(isUsable)[0]?.agent_type ?? 'ClaudeCode';
          await projectsApi.auditStream(projectId, { agent: auditAgent }, {
            onStepStart: (step, total, file) => {
              setAuditState(prev => ({
                ...prev,
                [projectId]: { active: true, step, totalSteps: total, currentFile: file }
              }));
            },
            onChunk: () => { /* progress tracked via step_start */ },
            onStepDone: () => { /* step progress */ },
            onDone: () => {
              setAuditState(prev => ({
                ...prev,
                [projectId]: { ...prev[projectId], active: false }
              }));
              refetch();
            },
            onError: (error) => {
              console.error('Audit error:', error);
              setAuditState(prev => ({
                ...prev,
                [projectId]: { ...prev[projectId], active: false }
              }));
            },
          });
        } catch (e) {
          console.error('Audit failed:', e);
          setAuditState(prev => ({
            ...prev,
            [projectId]: { ...prev[projectId], active: false }
          }));
        }
      };

      const handleScan = async () => {
        const repos = await projectsApi.scan();
        for (const repo of repos) {
          if (!repo.has_project && !repo.hidden) {
            await projectsApi.create(repo);
          }
        }
        refetch();
      };

      const handleInstallAgent = async (agent: AgentDetection) => {
        setInstalling(agent.name);
        try {
          await agentsApi.install(agent.agent_type);
          refetchAgents();
        } catch {
          // silently fail
        } finally {
          setInstalling(null);
        }
      };

      return (
        <div style={s.app}>
          <style>{`@keyframes spin { to { transform: rotate(360deg) } }`}</style>
          {/* Nav */}
          <nav style={s.nav}>
            <div style={s.navBrand}>
              <Zap size={18} style={{ color: '#c8ff00' }} />
              <span style={s.navTitle}>Kronn</span>
            </div>
            {([
              ['projects', Folder, t('nav.projects')],
              ['discussions', MessageSquare, t('nav.discussions')],
              ['mcps', Server, t('nav.mcps')],
              ['workflows', Workflow, t('nav.workflows')],
              ['settings', Settings, t('nav.config')],
            ] as [string, typeof Folder, string][]).map(([id, Icon, label]) => (
              <button key={id} style={{ ...s.navBtn(page === id), position: 'relative' }} onClick={() => setPage(id as Page)}>
                <Icon size={14} /> {label}
                {id === 'discussions' && totalUnseen > 0 && (
                  <span style={{
                    position: 'absolute', top: 2, right: 2,
                    background: '#c8ff00', color: '#0a0c10', fontSize: 8, fontWeight: 800,
                    borderRadius: 6, padding: '1px 5px', minWidth: 14, textAlign: 'center',
                    lineHeight: '12px',
                  }}>{totalUnseen}</span>
                )}
              </button>
            ))}
            <div style={{ flex: 1 }} />
            <button style={s.scanBtn} onClick={handleScan}>
              <Search size={14} /> {t('nav.scan')}
            </button>
          </nav>

          {/* Content */}
          <main style={s.main}>

            {/* ════════ PROJETS ════════ */}
            {page === 'projects' && (() => {
              const visibleProjects = projects.filter(p => !isHiddenPath(p.path));
              const hiddenProjects = projects.filter(p => isHiddenPath(p.path));
              const baseProjects = showHidden ? projects : visibleProjects;
              // Filter by search
              const searchLower = projectSearch.toLowerCase();
              const filteredProjects = projectSearch
                ? baseProjects.filter(p => p.name.toLowerCase().includes(searchLower) || p.path.toLowerCase().includes(searchLower))
                : baseProjects;
              // Sort: AI+MCP > AI only > MCP only > nothing, then by name
              const sortedProjects = [...filteredProjects].sort((a, b) => {
                const diff = readinessScore(a) - readinessScore(b);
                return diff !== 0 ? diff : a.name.localeCompare(b.name);
              });
              const displayProjects = projectSearch ? sortedProjects : sortedProjects.slice(0, projectDisplayLimit);
              const remainingCount = sortedProjects.length - displayProjects.length;
              const aiCount = visibleProjects.filter(isAiReady).length;
              return (
              <div>
                <div style={s.pageHeader}>
                  <div>
                    <h1 style={s.h1}>{t('projects.title')}</h1>
                    <p style={s.meta}>
                      {aiCount}/{visibleProjects.length} {t('projects.aiReady')}
                      {hiddenProjects.length > 0 && (
                        <span style={{ color: 'rgba(255,255,255,0.25)' }}> + {hiddenProjects.length} {hiddenProjects.length > 1 ? t('projects.hiddenPlural') : t('projects.hidden')}</span>
                      )}
                    </p>
                  </div>
                  <div style={{ display: 'flex', gap: 8, alignItems: 'center' }}>
                    {hiddenProjects.length > 0 && (
                      <button style={s.iconBtn} onClick={() => setShowHidden(!showHidden)} title={showHidden ? t('projects.hideHidden') : t('projects.showHidden')}>
                        <Eye size={14} style={{ color: showHidden ? '#c8ff00' : undefined }} />
                      </button>
                    )}
                  </div>
                </div>

                {/* Search bar for projects */}
                {baseProjects.length > 3 && (
                  <div style={{ position: 'relative', marginBottom: 12 }}>
                    <Search size={14} style={{ position: 'absolute', left: 10, top: '50%', transform: 'translateY(-50%)', color: 'rgba(255,255,255,0.25)', pointerEvents: 'none' }} />
                    <input
                      style={{ background: 'rgba(255,255,255,0.04)', border: '1px solid rgba(255,255,255,0.08)', borderRadius: 6, padding: '8px 12px 8px 32px', color: '#e8eaed', fontSize: 12, fontFamily: 'inherit', width: '100%', outline: 'none' }}
                      placeholder={t('projects.search')}
                      value={projectSearch}
                      onChange={(e) => setProjectSearch(e.target.value)}
                    />
                    {projectSearch && (
                      <button
                        style={{ position: 'absolute', right: 8, top: '50%', transform: 'translateY(-50%)', background: 'none', border: 'none', cursor: 'pointer', color: 'rgba(255,255,255,0.3)', padding: 2 }}
                        onClick={() => setProjectSearch('')}
                      >
                        <X size={12} />
                      </button>
                    )}
                  </div>
                )}

                {displayProjects.map((proj: Project) => {
                  const isOpen = expandedId === proj.id;
                  const projHidden = isHiddenPath(proj.path);
                  const validationDisc = allDiscussions.find(d => d.project_id === proj.id && d.title === 'Validation audit AI');
                  const validationInProgress = !!validationDisc && proj.audit_status === 'Audited';
                  return (
                    <div key={proj.id} style={{ ...s.card(isOpen), opacity: projHidden ? 0.5 : 1 }}>
                      <div style={s.cardHeader} onClick={() => setExpandedId(isOpen ? null : proj.id)}>
                        <ChevronRight size={14} style={{ color: '#c8ff00', transform: isOpen ? 'rotate(90deg)' : 'none', transition: 'transform 0.2s' }} />
                        <div style={{ flex: 1 }}>
                          <div style={{ display: 'flex', alignItems: 'center', gap: 6, flexWrap: 'wrap' }}>
                            <span style={s.projName}>{proj.name}</span>
                            {/* AI context badge */}
                            {proj.audit_status === 'NoTemplate' ? (
                              <span style={s.badgeGray}><FileCode size={9} /> AI context</span>
                            ) : (
                              <span style={s.badgeGreen}><FileCode size={9} /> AI context</span>
                            )}
                            {/* AI audit badge */}
                            {auditState[proj.id]?.active ? (
                              <span style={s.badgeOrange}>
                                <Loader2 size={9} style={{ animation: 'spin 1s linear infinite' }} /> AI audit {auditState[proj.id].step}/{auditState[proj.id].totalSteps}
                              </span>
                            ) : (proj.audit_status === 'Audited' || proj.audit_status === 'Validated') ? (
                              <span style={s.badgeGreen}><Cpu size={9} /> AI audit</span>
                            ) : proj.audit_status === 'TemplateInstalled' ? (
                              <span style={s.badgeOrange}><Cpu size={9} /> AI audit</span>
                            ) : (
                              <span style={s.badgeGray}><Cpu size={9} /> AI audit</span>
                            )}
                            {/* Validated badge */}
                            {proj.audit_status === 'Validated' ? (
                              <span style={s.badgeGreen}><ShieldCheck size={9} /> Validated</span>
                            ) : validationInProgress ? (
                              <span style={{ ...s.badgeOrange, cursor: 'pointer' }} onClick={(e) => { e.stopPropagation(); setActiveDiscussionId(validationDisc.id); setPage('discussions'); }}>
                                <Loader2 size={9} style={{ animation: 'spin 1s linear infinite' }} /> Validation
                              </span>
                            ) : (proj.audit_status === 'Audited' || proj.audit_status === 'TemplateInstalled') ? (
                              <span style={s.badgeGray}><ShieldCheck size={9} /> Validated</span>
                            ) : null}
                          </div>
                          {/* AI config files on second line */}
                          {proj.ai_config.configs.length > 0 && (
                            <div style={{ display: 'flex', alignItems: 'center', gap: 4, marginTop: 4, flexWrap: 'wrap' }}>
                              {proj.ai_config.configs.map(c => (
                                <span key={c} style={s.aiBadge}>{AI_CONFIG_LABELS[c] ?? c}</span>
                              ))}
                            </div>
                          )}
                          <div style={s.projPath}>{proj.path}</div>
                        </div>
                        <div style={s.projMeta}>
                          <span style={s.metaItem}><Server size={12} /> {mcpOverview.configs.filter(c => c.is_global || c.project_ids.includes(proj.id)).length}</span>
                          <span style={s.metaItem}><MessageSquare size={12} /> {allDiscussions.filter(d => d.project_id === proj.id).length}</span>
                        </div>
                      </div>

                      {isOpen && (
                        <div style={s.cardBody} onClick={(e) => e.stopPropagation()}>
                          {/* MCPs */}
                          {(() => {
                            const projMcps = mcpOverview.configs.filter(c => c.is_global || c.project_ids.includes(proj.id));
                            return (
                              <div style={s.section}>
                                <div style={s.sectionHeader}>
                                  <Server size={14} /> <span style={s.sectionTitle}>MCP</span>
                                  <span style={s.count}>{projMcps.length}</span>
                                </div>
                                {projMcps.map(cfg => (
                                  <div key={cfg.id} style={s.row}>
                                    <div style={s.dot(true)} />
                                    <div style={{ flex: 1 }}>
                                      <span style={{ fontWeight: 600, fontSize: 12 }}>{cfg.server_name}</span>
                                      <span style={{ marginLeft: 6, fontSize: 11, color: 'rgba(255,255,255,0.3)' }}>{cfg.label}</span>
                                      {cfg.is_global && <span style={{ marginLeft: 4, fontSize: 9, color: '#c8ff00' }}>GLOBAL</span>}
                                    </div>
                                    <button
                                      style={s.iconBtn}
                                      onClick={() => setPage('mcps')}
                                      title={t('projects.manageMcps')}
                                    >
                                      <ChevronRight size={12} />
                                    </button>
                                  </div>
                                ))}
                                {projMcps.length === 0 && (
                                  <div style={{ fontSize: 11, color: 'rgba(255,255,255,0.25)', padding: '4px 0' }}>
                                    {t('projects.noMcp').split(' — ')[0]} — <button style={{ ...s.iconBtn, fontSize: 11, color: '#c8ff00' }} onClick={() => setPage('mcps')}>{t('projects.noMcp').split(' — ')[1]}</button>
                                  </div>
                                )}
                              </div>
                            );
                          })()}

                          {/* Discussions */}
                          <div style={s.section}>
                            <div style={s.sectionHeader}>
                              <MessageSquare size={14} /> <span style={s.sectionTitle}>Discussions</span>
                              <span style={s.count}>{allDiscussions.filter(d => d.project_id === proj.id).length}</span>
                            </div>
                            {allDiscussions.filter(d => d.project_id === proj.id).slice(0, 3).map(disc => (
                              <div key={disc.id} style={s.row}>
                                <div style={s.dot(true)} />
                                <div style={{ flex: 1 }}>
                                  <span style={{ fontWeight: 600, fontSize: 12, display: 'inline-flex', alignItems: 'center', gap: 4 }}>
                                    {isValidationDisc(disc.title) && <ShieldCheck size={10} style={{ color: '#c8ff00' }} />}
                                    {disc.title}
                                  </span>
                                  <span style={{ marginLeft: 8, fontSize: 11, color: 'rgba(255,255,255,0.3)' }}>
                                    {disc.messages.length} msg · {disc.agent}
                                  </span>
                                </div>
                                <button style={s.iconBtn} onClick={() => { setActiveDiscussionId(disc.id); setPage('discussions'); }}>
                                  <ChevronRight size={12} />
                                </button>
                              </div>
                            ))}
                            <button
                              style={{ ...s.iconBtn, marginTop: 8, fontSize: 11, gap: 4 }}
                              onClick={() => { setPage('discussions'); setShowNewDiscussion(true); setNewDiscProjectId(proj.id); }}
                            >
                              <Plus size={12} /> {t('disc.newTitle')}
                            </button>
                          </div>

                          {/* AI Audit */}
                          <div style={s.section}>
                            <div style={s.sectionHeader}>
                              <FileCode size={14} /> <span style={s.sectionTitle}>AI Context</span>
                              <span style={s.count}>
                                {proj.audit_status === 'Validated' ? t('projects.status.valid') : validationInProgress ? t('projects.status.validating') : proj.audit_status === 'Audited' ? t('projects.status.auditOk') : proj.audit_status === 'TemplateInstalled' ? t('projects.status.template') : t('projects.status.none')}
                              </span>
                            </div>

                            {proj.audit_status === 'NoTemplate' && (
                              <div style={{ padding: '8px 0' }}>
                                <p style={{ fontSize: 11, color: 'rgba(255,255,255,0.35)', margin: '0 0 8px' }}>
                                  {t('audit.noTemplate')}
                                </p>
                                <button
                                  style={{ ...s.iconBtn, fontSize: 11, gap: 4, color: '#c8ff00', borderColor: 'rgba(200,255,0,0.2)' }}
                                  onClick={() => handleInstallTemplate(proj.id)}
                                  disabled={installingTemplate === proj.id}
                                >
                                  {installingTemplate === proj.id
                                    ? <><Loader2 size={12} style={{ animation: 'spin 1s linear infinite' }} /> Installation...</>
                                    : <><Download size={12} /> {t('audit.installTemplate')}</>
                                  }
                                </button>
                              </div>
                            )}

                            {proj.audit_status === 'TemplateInstalled' && !auditState[proj.id]?.active && (
                              <div style={{ padding: '8px 0' }}>
                                <p style={{ fontSize: 11, color: 'rgba(255,200,0,0.7)', margin: '0 0 8px', display: 'flex', alignItems: 'center', gap: 4 }}>
                                  <AlertTriangle size={11} /> {t('audit.description')}
                                </p>
                                <p style={{ fontSize: 10, color: 'rgba(255,255,255,0.25)', margin: '0 0 8px' }}>
                                  {t('audit.warning')}
                                </p>
                                <div style={{ display: 'flex', alignItems: 'center', gap: 8 }}>
                                  <select
                                    style={{ ...ds.selectStyled, width: 'auto', minWidth: 140, fontSize: 12, padding: '6px 32px 6px 10px' }}
                                    value={auditAgentChoice[proj.id] ?? agents.filter(isUsable)[0]?.agent_type ?? 'ClaudeCode'}
                                    onChange={e => setAuditAgentChoice(prev => ({ ...prev, [proj.id]: e.target.value as AgentType }))}
                                  >
                                    {agents.filter(isUsable).map(a => (
                                      <option key={a.agent_type} value={a.agent_type}>{a.name}</option>
                                    ))}
                                    {agents.filter(isUsable).length === 0 && (
                                      <option value="" disabled>{t('disc.noAgent')}</option>
                                    )}
                                  </select>
                                  <button
                                    style={{ ...s.iconBtn, fontSize: 11, gap: 4, color: '#c8ff00', borderColor: 'rgba(200,255,0,0.2)' }}
                                    onClick={() => handleLaunchAudit(proj.id)}
                                    disabled={agents.filter(isUsable).length === 0}
                                  >
                                    <Play size={12} /> {t('audit.launch')}
                                  </button>
                                </div>
                              </div>
                            )}

                            {auditState[proj.id]?.active && (
                              <div style={{ padding: '8px 0' }}>
                                <div style={{ display: 'flex', alignItems: 'center', gap: 8, marginBottom: 8 }}>
                                  <Loader2 size={14} style={{ animation: 'spin 1s linear infinite', color: '#c8ff00' }} />
                                  <span style={{ fontSize: 12, color: 'rgba(255,255,255,0.6)' }}>
                                    {t('audit.step', auditState[proj.id].step, auditState[proj.id].totalSteps, auditState[proj.id].currentFile)}
                                  </span>
                                </div>
                                <div style={{ height: 3, background: 'rgba(255,255,255,0.06)', borderRadius: 2, overflow: 'hidden' }}>
                                  <div style={{
                                    height: '100%',
                                    width: `${(auditState[proj.id].step / auditState[proj.id].totalSteps) * 100}%`,
                                    background: '#c8ff00',
                                    borderRadius: 2,
                                    transition: 'width 0.5s ease',
                                  }} />
                                </div>
                              </div>
                            )}

                            {proj.audit_status === 'Audited' && !auditState[proj.id]?.active && (
                              <div style={{ padding: '8px 0' }}>
                                {validationInProgress ? (
                                  <>
                                    <p style={{ fontSize: 11, color: 'rgba(255,200,0,0.7)', margin: '0 0 8px', display: 'flex', alignItems: 'center', gap: 4 }}>
                                      <Loader2 size={11} style={{ animation: 'spin 1s linear infinite' }} /> {t('audit.validationInProgress', validationDisc.messages.length)}
                                    </p>
                                    <p style={{ fontSize: 10, color: 'rgba(255,255,255,0.25)', margin: '0 0 8px' }}>
                                      {t('audit.validationHint')}
                                    </p>
                                    <button
                                      style={{ ...s.iconBtn, fontSize: 11, gap: 4, color: '#c8ff00', borderColor: 'rgba(200,255,0,0.2)' }}
                                      onClick={() => { setActiveDiscussionId(validationDisc.id); setPage('discussions'); }}
                                    >
                                      <MessageSquare size={12} /> {t('audit.resumeValidation')}
                                    </button>
                                  </>
                                ) : (
                                  <>
                                    <p style={{ fontSize: 11, color: 'rgba(255,255,255,0.35)', margin: '0 0 8px' }}>
                                      {t('audit.readyToValidate')}
                                    </p>
                                    <button
                                      style={{ ...s.iconBtn, fontSize: 11, gap: 4, color: '#c8ff00', borderColor: 'rgba(200,255,0,0.2)' }}
                                      onClick={() => {
                                        setPage('discussions');
                                        setShowNewDiscussion(true);
                                        setNewDiscPrefilled(true);
                                        setNewDiscProjectId(proj.id);
                                        setNewDiscTitle('Validation audit AI');
                                        setNewDiscPrompt(`Voici le contexte AI du projet (dossier ai/). Analyse l'ensemble des fichiers ai/, identifie les zones d'ambiguite, les questions non resolues, les marqueurs <!-- TODO --> et les incoherences. Pose-moi tes questions une par une. Important : a chaque reponse de ma part, mets immediatement a jour les fichiers ai/ concernes avant de poser la question suivante — cela evite de repeter les memes questions et garde la documentation a jour en continu. Quand toutes mes reponses seront comprises, n'hesite pas a poser des questions de suivi. Une fois tout clarifie, termine ton message par la phrase exacte : "KRONN:VALIDATION_COMPLETE".`);
                                      }}
                                    >
                                      <ShieldCheck size={12} /> {t('audit.validate')}
                                    </button>
                                  </>
                                )}
                              </div>
                            )}

                            {proj.audit_status === 'Validated' && !auditState[proj.id]?.active && (
                              <div style={{ padding: '4px 0', fontSize: 11, color: 'rgba(200,255,0,0.5)', display: 'flex', alignItems: 'center', gap: 4 }}>
                                <ShieldCheck size={11} /> {t('audit.done')}
                              </div>
                            )}
                          </div>

                          <div style={{ display: 'flex', justifyContent: 'flex-end', marginTop: 12, paddingTop: 12, borderTop: '1px solid rgba(255,255,255,0.05)' }}>
                            <button style={s.dangerBtn} onClick={() => handleDeleteProject(proj.id)}>
                              <Trash2 size={12} /> {t('projects.delete')}
                            </button>
                          </div>
                        </div>
                      )}
                    </div>
                  );
                })}

                {/* Show more / less buttons */}
                {remainingCount > 0 && (
                  <button
                    style={{ display: 'block', width: '100%', padding: '10px 0', background: 'rgba(200,255,0,0.04)', border: '1px solid rgba(200,255,0,0.1)', borderRadius: 8, color: '#c8ff00', fontSize: 12, cursor: 'pointer', fontFamily: 'inherit', marginTop: 8 }}
                    onClick={() => setProjectDisplayLimit(prev => prev + 20)}
                  >
                    {t('projects.showMore', remainingCount, remainingCount > 1 ? 's' : '', remainingCount > 1 ? 's' : '')}
                  </button>
                )}
                {!projectSearch && projectDisplayLimit > 20 && remainingCount === 0 && sortedProjects.length > 20 && (
                  <button
                    style={{ display: 'block', width: '100%', padding: '8px 0', background: 'transparent', border: '1px solid rgba(255,255,255,0.06)', borderRadius: 8, color: 'rgba(255,255,255,0.3)', fontSize: 11, cursor: 'pointer', fontFamily: 'inherit', marginTop: 8 }}
                    onClick={() => setProjectDisplayLimit(20)}
                  >
                    {t('projects.collapse')}
                  </button>
                )}

                {displayProjects.length === 0 && (
                  <div style={{ ...s.card(false), textAlign: 'center', padding: 40 }}>
                    <Folder size={32} style={{ color: 'rgba(255,255,255,0.15)', marginBottom: 12 }} />
                    <p style={{ color: 'rgba(255,255,255,0.4)', fontSize: 13 }}>
                      {projectSearch ? t('projects.emptySearch') : t('projects.empty')}
                    </p>
                  </div>
                )}
              </div>
              );
            })()}

            {/* ════════ MCPs ════════ */}
            {page === 'mcps' && (
              <McpPage projects={projects} mcpOverview={mcpOverview} mcpRegistry={mcpRegistry} refetchMcps={refetchMcps} />
            )}

            {/* ════════ WORKFLOWS ════════ */}
            {page === 'workflows' && (
              <WorkflowsPage projects={projects} installedAgentTypes={agents.filter(isUsable).map(a => a.agent_type)} />
            )}

            {/* ════════ DISCUSSIONS ════════ */}
            {page === 'discussions' && (() => {
              const installedAgents = agents.filter(isUsable);
              // Group discussions by project (null = global)
              const discByProject = new Map<string | null, Discussion[]>();
              for (const d of allDiscussions) {
                const key = d.project_id ?? null;
                const list = discByProject.get(key) ?? [];
                list.push(d);
                discByProject.set(key, list);
              }

              const cleanupStream = (discId: string) => {
                setSendingMap(prev => ({ ...prev, [discId]: false }));
                setStreamingMap(prev => { const n = { ...prev }; delete n[discId]; return n; });
                delete abortControllers.current[discId];
                refetchDiscussions();
              };

              const handleCreateDiscussion = async () => {
                if (!newDiscPrompt.trim() || !newDiscAgent) return;
                const prompt = newDiscPrompt.trim();
                const title = newDiscTitle.trim() || prompt.slice(0, 60);
                const disc = await discussionsApi.create({
                  project_id: newDiscProjectId || null,
                  title,
                  agent: newDiscAgent as AgentType,
                  language: configLanguage ?? 'fr',
                  initial_prompt: prompt,
                });
                setShowNewDiscussion(false);
                setNewDiscTitle('');
                setNewDiscPrompt('');
                setNewDiscPrefilled(false);
                setActiveDiscussionId(disc.id);
                refetchDiscussions();

                const discId = disc.id;
                const controller = new AbortController();
                abortControllers.current[discId] = controller;
                setSendingMap(prev => ({ ...prev, [discId]: true }));
                setStreamingMap(prev => ({ ...prev, [discId]: '' }));
                await discussionsApi.runAgent(
                  discId,
                  (text) => setStreamingMap(prev => ({ ...prev, [discId]: (prev[discId] ?? '') + text })),
                  () => cleanupStream(discId),
                  (error) => { console.error('Agent error:', error); cleanupStream(discId); },
                  controller.signal,
                );
              };

              const parseMention = (text: string): { targetAgent?: AgentType } => {
                for (const m of AGENT_MENTIONS) {
                  if (text.toLowerCase().startsWith(m.trigger + ' ') || text.toLowerCase() === m.trigger) {
                    return { targetAgent: m.type };
                  }
                }
                return {};
              };

              const handleSendMessage = async () => {
                if (!activeDiscussionId || !chatInput.trim() || sending) return;
                const discId = activeDiscussionId;
                const msg = chatInput.trim();
                const { targetAgent } = parseMention(msg);
                setChatInput('');
                setMentionQuery(null);
                const controller = new AbortController();
                abortControllers.current[discId] = controller;
                setStreamingMap(prev => ({ ...prev, [discId]: '' }));

                await discussionsApi.sendMessageStream(
                  discId,
                  { content: msg, target_agent: targetAgent },
                  (text) => setStreamingMap(prev => ({ ...prev, [discId]: (prev[discId] ?? '') + text })),
                  () => cleanupStream(discId),
                  (error) => { console.error('Agent error:', error); cleanupStream(discId); },
                  controller.signal,
                  () => {
                    // Backend has added the user message — refetch to show it, then show streaming
                    refetchDiscussions();
                    setSendingMap(prev => ({ ...prev, [discId]: true }));
                  },
                );
              };

              const handleStop = () => {
                if (!activeDiscussionId) return;
                const discId = activeDiscussionId;
                const controller = abortControllers.current[discId];
                if (controller) controller.abort();
                // Ensure UI clears even if SSE abort callback doesn't fire
                cleanupStream(discId);
              };

              const handleRetry = async () => {
                if (!activeDiscussionId || sending) return;
                const discId = activeDiscussionId;
                // Delete trailing agent messages, then re-run
                await discussionsApi.deleteLastAgentMessages(discId);
                await refetchDiscussions();
                const controller = new AbortController();
                abortControllers.current[discId] = controller;
                setSendingMap(prev => ({ ...prev, [discId]: true }));
                setStreamingMap(prev => ({ ...prev, [discId]: '' }));
                await discussionsApi.runAgent(
                  discId,
                  (text) => setStreamingMap(prev => ({ ...prev, [discId]: (prev[discId] ?? '') + text })),
                  () => cleanupStream(discId),
                  (error) => { console.error('Agent error:', error); cleanupStream(discId); },
                  controller.signal,
                );
              };

              const handleEditMessage = async () => {
                if (!activeDiscussionId || !editingMsgId || !editingText.trim() || sending) return;
                const discId = activeDiscussionId;
                // Delete trailing agent messages, edit the user message, then re-run
                await discussionsApi.deleteLastAgentMessages(discId);
                await discussionsApi.editLastUserMessage(discId, editingText.trim());
                setEditingMsgId(null);
                setEditingText('');
                await refetchDiscussions();
                const controller = new AbortController();
                abortControllers.current[discId] = controller;
                setSendingMap(prev => ({ ...prev, [discId]: true }));
                setStreamingMap(prev => ({ ...prev, [discId]: '' }));
                await discussionsApi.runAgent(
                  discId,
                  (text) => setStreamingMap(prev => ({ ...prev, [discId]: (prev[discId] ?? '') + text })),
                  () => cleanupStream(discId),
                  (error) => { console.error('Agent error:', error); cleanupStream(discId); },
                  controller.signal,
                );
              };

              const handleOrchestrate = async () => {
                if (!activeDiscussionId || debateAgents.length < 2) return;
                const discId = activeDiscussionId;
                const controller = new AbortController();
                abortControllers.current[discId] = controller;
                setShowDebatePopover(false);
                setSendingMap(prev => ({ ...prev, [discId]: true }));
                setOrchState(prev => ({
                  ...prev,
                  [discId]: { active: true, round: 0, totalRounds: 3, currentAgent: null, agentStreams: [], systemMessages: [] },
                }));

                await discussionsApi.orchestrate(discId, { agents: debateAgents, max_rounds: debateRounds }, {
                  onSystem: (text) => {
                    setOrchState(prev => {
                      const s = prev[discId];
                      return s ? { ...prev, [discId]: { ...s, systemMessages: [...s.systemMessages, text] } } : prev;
                    });
                  },
                  onRound: (round, total) => {
                    setOrchState(prev => {
                      const s = prev[discId];
                      return s ? { ...prev, [discId]: { ...s, round, totalRounds: total } } : prev;
                    });
                  },
                  onAgentStart: (agent, agentType, round) => {
                    setOrchState(prev => {
                      const s = prev[discId];
                      if (!s) return prev;
                      return { ...prev, [discId]: {
                        ...s, currentAgent: agent,
                        agentStreams: [...s.agentStreams, { agent, agentType, round, text: '', done: false }],
                      }};
                    });
                  },
                  onChunk: (text, agent, _agentType, _round) => {
                    setOrchState(prev => {
                      const s = prev[discId];
                      if (!s) return prev;
                      const streams = [...s.agentStreams];
                      const last = [...streams].reverse().find((st: typeof streams[0]) => st.agent === agent && !st.done);
                      if (last) last.text = (last.text ?? '') + text;
                      return { ...prev, [discId]: { ...s, agentStreams: streams } };
                    });
                  },
                  onAgentDone: (agent) => {
                    setOrchState(prev => {
                      const s = prev[discId];
                      if (!s) return prev;
                      const streams = s.agentStreams.map(st =>
                        st.agent === agent && !st.done ? { ...st, done: true } : st
                      );
                      return { ...prev, [discId]: { ...s, currentAgent: null, agentStreams: streams } };
                    });
                  },
                  onDone: () => {
                    setSendingMap(prev => ({ ...prev, [discId]: false }));
                    delete abortControllers.current[discId];
                    setOrchState(prev => {
                      const s = prev[discId];
                      return s ? { ...prev, [discId]: { ...s, active: false, currentAgent: null } } : prev;
                    });
                    refetchDiscussions();
                  },
                  onError: (error) => {
                    console.error('Orchestration error:', error);
                    setSendingMap(prev => ({ ...prev, [discId]: false }));
                    delete abortControllers.current[discId];
                    setOrchState(prev => {
                      const s = prev[discId];
                      return s ? { ...prev, [discId]: { ...s, active: false } } : prev;
                    });
                    refetchDiscussions();
                  },
                }, controller.signal);
              };

              return (
              <div style={{ display: 'flex', height: 'calc(100vh - 56px)', margin: '-24px -20px', overflow: 'hidden' }}>
                {/* Sidebar */}
                <div style={ds.sidebar}>
                  <div style={ds.sidebarHeader}>
                    <span style={{ fontWeight: 600, fontSize: 13 }}>Discussions</span>
                    <button style={s.scanBtn} onClick={() => { setShowNewDiscussion(true); }}>
                      <Plus size={12} /> {t('disc.new')}
                    </button>
                  </div>

                  {/* Discussion list grouped by project */}
                  <div style={ds.sidebarList}>
                    {/* Global discussions (no project) */}
                    {(() => {
                      const globalDiscs = discByProject.get(null) ?? [];
                      if (globalDiscs.length === 0) return null;
                      const isCollapsed = collapsedDiscGroups.has('__global__');
                      return (
                        <div>
                          <div
                            style={{ ...ds.projectGroup, borderTop: 'none', cursor: 'pointer', userSelect: 'none' as const }}
                            onClick={() => setCollapsedDiscGroups(prev => { const n = new Set(prev); isCollapsed ? n.delete('__global__') : n.add('__global__'); return n; })}
                          >
                            <ChevronRight size={10} style={{ transform: isCollapsed ? 'none' : 'rotate(90deg)', transition: 'transform 0.15s' }} />
                            <MessageSquare size={10} /> {t('disc.general')}
                            <span style={{ fontWeight: 400, opacity: 0.5, marginLeft: 'auto' }}>{globalDiscs.length}</span>
                          </div>
                          {!isCollapsed && globalDiscs.sort((a, b) => b.updated_at.localeCompare(a.updated_at)).map(disc => (
                            <button
                              key={disc.id}
                              style={ds.discItem(disc.id === activeDiscussionId)}
                              onClick={() => { setActiveDiscussionId(disc.id); markDiscussionSeen(disc.id, disc.messages.length); }}
                            >
                              <div style={{ flex: 1, minWidth: 0 }}>
                                <div style={{ fontSize: 12, fontWeight: 500, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap', display: 'flex', alignItems: 'center', gap: 4 }}>
                                  {isValidationDisc(disc.title) && <ShieldCheck size={10} style={{ color: '#c8ff00', flexShrink: 0 }} />}
                                  {disc.title}
                                  {(() => {
                                    const unseen = disc.messages.length - (lastSeenMsgCount[disc.id] ?? 0);
                                    return unseen > 0 && disc.id !== activeDiscussionId ? (
                                      <span style={{
                                        background: '#c8ff00', color: '#0a0c10', fontSize: 8, fontWeight: 800,
                                        borderRadius: 6, padding: '1px 5px', minWidth: 14, textAlign: 'center', flexShrink: 0,
                                      }}>{unseen}</span>
                                    ) : null;
                                  })()}
                                </div>
                                <div style={{ fontSize: 10, color: 'rgba(255,255,255,0.3)', marginTop: 2, display: 'flex', alignItems: 'center', gap: 4 }}>
                                  {sendingMap[disc.id] && <Loader2 size={8} style={{ animation: 'spin 1s linear infinite', color: '#c8ff00' }} />}
                                  {(disc.participants?.length ?? 0) > 1 && (
                                    <Users size={8} style={{ color: '#8b5cf6' }} />
                                  )}
                                  {disc.messages.length} msg · {disc.agent}
                                </div>
                              </div>
                            </button>
                          ))}
                        </div>
                      );
                    })()}

                    {/* Project discussions */}
                    {projects.filter(p => !isHiddenPath(p.path)).map(proj => {
                      const projDiscs = discByProject.get(proj.id) ?? [];
                      if (projDiscs.length === 0) return null;
                      const isCollapsed = collapsedDiscGroups.has(proj.id);
                      return (
                        <div key={proj.id}>
                          <div
                            style={{ ...ds.projectGroup, cursor: 'pointer', userSelect: 'none' as const }}
                            onClick={() => setCollapsedDiscGroups(prev => { const n = new Set(prev); isCollapsed ? n.delete(proj.id) : n.add(proj.id); return n; })}
                          >
                            <ChevronRight size={10} style={{ transform: isCollapsed ? 'none' : 'rotate(90deg)', transition: 'transform 0.15s' }} />
                            <Folder size={10} /> {proj.name}
                            <span style={{ fontWeight: 400, opacity: 0.5, marginLeft: 'auto' }}>{projDiscs.length}</span>
                          </div>
                          {!isCollapsed && projDiscs.sort((a, b) => b.updated_at.localeCompare(a.updated_at)).map(disc => (
                            <button
                              key={disc.id}
                              style={ds.discItem(disc.id === activeDiscussionId)}
                              onClick={() => { setActiveDiscussionId(disc.id); markDiscussionSeen(disc.id, disc.messages.length); }}
                            >
                              <div style={{ flex: 1, minWidth: 0 }}>
                                <div style={{ fontSize: 12, fontWeight: 500, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap', display: 'flex', alignItems: 'center', gap: 4 }}>
                                  {isValidationDisc(disc.title) && <ShieldCheck size={10} style={{ color: '#c8ff00', flexShrink: 0 }} />}
                                  {disc.title}
                                  {(() => {
                                    const unseen = disc.messages.length - (lastSeenMsgCount[disc.id] ?? 0);
                                    return unseen > 0 && disc.id !== activeDiscussionId ? (
                                      <span style={{
                                        background: '#c8ff00', color: '#0a0c10', fontSize: 8, fontWeight: 800,
                                        borderRadius: 6, padding: '1px 5px', minWidth: 14, textAlign: 'center', flexShrink: 0,
                                      }}>{unseen}</span>
                                    ) : null;
                                  })()}
                                </div>
                                <div style={{ fontSize: 10, color: 'rgba(255,255,255,0.3)', marginTop: 2, display: 'flex', alignItems: 'center', gap: 4 }}>
                                  {sendingMap[disc.id] && <Loader2 size={8} style={{ animation: 'spin 1s linear infinite', color: '#c8ff00' }} />}
                                  {(disc.participants?.length ?? 0) > 1 && (
                                    <Users size={8} style={{ color: '#8b5cf6' }} />
                                  )}
                                  {disc.messages.length} msg · {disc.agent}
                                </div>
                              </div>
                            </button>
                          ))}
                        </div>
                      );
                    })}

                    {allDiscussions.length === 0 && !showNewDiscussion && (
                      <div style={{ padding: 24, textAlign: 'center', color: 'rgba(255,255,255,0.25)', fontSize: 12, whiteSpace: 'pre-line' }}>
                        {t('disc.empty')}
                      </div>
                    )}
                  </div>
                </div>

                {/* Main area */}
                <div style={ds.chatArea}>
                  {/* New discussion form */}
                  {showNewDiscussion && (
                    <div style={ds.newDiscOverlay}>
                      <div
                        style={ds.newDiscCard}
                        onKeyDown={e => { if (e.key === 'Enter' && (e.ctrlKey || e.metaKey) && newDiscPrompt.trim()) handleCreateDiscussion(); }}
                      >
                        <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: 20 }}>
                          <span style={{ fontWeight: 700, fontSize: 15, color: '#e8eaed' }}>{t('disc.newTitle')}</span>
                          <button style={s.iconBtn} onClick={() => { setShowNewDiscussion(false); setNewDiscPrefilled(false); }}><X size={14} /></button>
                        </div>

                        <div style={{ display: 'grid', gridTemplateColumns: '1fr 1fr', gap: 12, marginBottom: 12 }}>
                          <div>
                            <label style={ds.label}>{t('disc.project')}</label>
                            <select style={{ ...ds.selectStyled, ...(newDiscPrefilled ? { opacity: 0.5, pointerEvents: 'none' as const } : {}) }} value={newDiscProjectId} onChange={e => setNewDiscProjectId(e.target.value)} disabled={newDiscPrefilled}>
                              <option value="">{t('disc.noProject')}</option>
                              {projects.filter(p => !isHiddenPath(p.path)).map(p => (
                                <option key={p.id} value={p.id}>{p.name}</option>
                              ))}
                            </select>
                          </div>
                          <div>
                            <label style={ds.label}>{t('disc.agent')}</label>
                            <select style={ds.selectStyled} value={newDiscAgent} onChange={e => setNewDiscAgent(e.target.value as AgentType)}>
                              {installedAgents.map(a => (
                                <option key={a.name} value={a.agent_type}>{a.name}</option>
                              ))}
                              {installedAgents.length === 0 && (
                                <option value="" disabled>{t('disc.noAgent')}</option>
                              )}
                            </select>
                          </div>
                        </div>

                        <label style={ds.label}>{t('disc.title')}</label>
                        <input
                          style={{ ...ds.inputStyled, ...(newDiscPrefilled ? { opacity: 0.5, cursor: 'not-allowed' } : {}) }}
                          placeholder={t('disc.titlePlaceholder')}
                          value={newDiscTitle}
                          onChange={e => !newDiscPrefilled && setNewDiscTitle(e.target.value)}
                          readOnly={newDiscPrefilled}
                        />

                        <label style={{ ...ds.label, marginTop: 12 }}>{t('disc.prompt')}</label>
                        <textarea
                          style={{ ...ds.textareaStyled, ...(newDiscPrefilled ? { opacity: 0.5, cursor: 'not-allowed' } : {}) }}
                          placeholder={t('disc.promptPlaceholder')}
                          value={newDiscPrompt}
                          onChange={e => !newDiscPrefilled && setNewDiscPrompt(e.target.value)}
                          readOnly={newDiscPrefilled}
                          rows={4}
                          autoFocus={!newDiscPrefilled}
                        />

                        {/* Warnings for validation discussion */}
                        {newDiscPrefilled && (
                          <div style={{ marginTop: 12, padding: '10px 12px', borderRadius: 8, background: 'rgba(255,200,0,0.06)', border: '1px solid rgba(255,200,0,0.12)' }}>
                            <p style={{ fontSize: 11, color: 'rgba(255,200,0,0.7)', margin: '0 0 6px', display: 'flex', alignItems: 'center', gap: 4 }}>
                              <AlertTriangle size={11} /> {t('disc.auditWarn')}
                            </p>
                            <p style={{ fontSize: 10, color: 'rgba(255,255,255,0.3)', margin: 0 }}>
                              {t('disc.auditHint')}
                            </p>
                          </div>
                        )}

                        <button
                          style={{
                            marginTop: 16, width: '100%', padding: '11px 16px', borderRadius: 8,
                            border: 'none', background: newDiscPrompt.trim() ? '#c8ff00' : 'rgba(255,255,255,0.06)',
                            color: newDiscPrompt.trim() ? '#0a0c10' : 'rgba(255,255,255,0.25)',
                            fontWeight: 700, fontSize: 13, fontFamily: 'inherit', cursor: 'pointer',
                            display: 'flex', alignItems: 'center', justifyContent: 'center', gap: 8,
                            transition: 'all 0.15s',
                          }}
                          onClick={handleCreateDiscussion}
                          disabled={!newDiscPrompt.trim() || !newDiscAgent}
                        >
                          <MessageSquare size={14} /> {t('disc.start')}
                          <span style={{ fontSize: 10, opacity: 0.6, marginLeft: 4 }}>Ctrl+Enter</span>
                        </button>
                      </div>
                    </div>
                  )}

                  {/* Active discussion chat */}
                  {activeDiscussion && !showNewDiscussion ? (
                    <>
                      {/* Chat header */}
                      <div style={ds.chatHeader}>
                        <div style={{ flex: 1 }}>
                          <div style={{ fontWeight: 600, fontSize: 14, display: 'flex', alignItems: 'center', gap: 6 }}>
                            {isValidationDisc(activeDiscussion.title) && <ShieldCheck size={14} style={{ color: '#c8ff00', flexShrink: 0 }} />}
                            {activeDiscussion.title}
                          </div>
                          <div style={{ fontSize: 11, color: 'rgba(255,255,255,0.35)', marginTop: 2 }}>
                            {activeDiscussion.project_id ? (projects.find(p => p.id === activeDiscussion.project_id)?.name ?? '?') : t('disc.general')} · {activeDiscussion.agent}
                          </div>
                        </div>
                        <button
                          style={{ ...s.iconBtn, color: '#ff4d6a' }}
                          onClick={async () => {
                            await discussionsApi.delete(activeDiscussion.id);
                            setActiveDiscussionId(null);
                            refetchDiscussions();
                          }}
                        >
                          <Trash2 size={12} />
                        </button>
                      </div>

                      {/* Messages */}
                      <div style={ds.messages}>
                        {activeDiscussion.messages.map((msg, idx) => {
                          const msgs = activeDiscussion.messages;
                          const isLastUser = msg.role === 'User' && !msgs.slice(idx + 1).some(m => m.role === 'User');
                          const isLastAgent = msg.role === 'Agent' && idx === msgs.length - 1;
                          const isEditing = editingMsgId === msg.id;

                          return (
                          <div key={msg.id} style={ds.msgRow(msg.role === 'User')}>
                            <div style={{
                              ...ds.msgBubble(msg.role === 'User'),
                              ...(msg.role === 'System' ? { borderColor: 'rgba(255,77,106,0.3)', background: 'rgba(255,77,106,0.06)' } : {}),
                            }}>
                              {msg.role === 'Agent' && (
                                <div style={{ ...ds.msgAgent, color: agentColor(msg.agent_type ?? activeDiscussion.agent) }}>
                                  <Cpu size={10} /> {msg.agent_type ?? activeDiscussion.agent}
                                </div>
                              )}
                              {msg.role === 'System' && (
                                <div style={{ ...ds.msgAgent, color: '#ff4d6a' }}>
                                  <AlertTriangle size={10} /> {t('disc.system')}
                                </div>
                              )}
                              {isEditing ? (
                                <div style={{ display: 'flex', flexDirection: 'column', gap: 8 }}>
                                  <textarea
                                    value={editingText}
                                    onChange={e => setEditingText(e.target.value)}
                                    onKeyDown={e => { if (e.key === 'Enter' && (e.ctrlKey || e.metaKey)) { e.preventDefault(); handleEditMessage(); } }}
                                    style={{
                                      width: '100%', minHeight: 60, padding: 8, borderRadius: 6,
                                      background: 'rgba(255,255,255,0.06)', border: '1px solid rgba(200,255,0,0.3)',
                                      color: '#e8eaed', fontFamily: 'inherit', fontSize: 12, resize: 'vertical',
                                    }}
                                    autoFocus
                                  />
                                  <div style={{ display: 'flex', gap: 6, justifyContent: 'flex-end' }}>
                                    <button
                                      style={{ ...s.iconBtn, fontSize: 11, padding: '4px 10px', color: 'rgba(255,255,255,0.4)' }}
                                      onClick={() => { setEditingMsgId(null); setEditingText(''); }}
                                    >
                                      {t('disc.cancel')}
                                    </button>
                                    <button
                                      style={{ ...s.scanBtn, fontSize: 11, padding: '4px 10px' }}
                                      onClick={handleEditMessage}
                                      disabled={!editingText.trim()}
                                    >
                                      <Send size={10} /> {t('disc.resend')}
                                      <span style={{ fontSize: 9, opacity: 0.5, marginLeft: 4 }}>Ctrl+Enter</span>
                                    </button>
                                  </div>
                                </div>
                              ) : (
                                <MarkdownContent content={msg.content} />
                              )}
                              {/api.?key|invalid.*key|key.*not.*config|authenticat|unauthori|login|sign.?in/i.test(msg.content) && (
                                <div style={{ display: 'flex', gap: 8, marginTop: 8, flexWrap: 'wrap' }}>
                                  <button
                                    style={{ ...s.scanBtn, fontSize: 11, padding: '5px 12px' }}
                                    onClick={() => setPage('settings')}
                                  >
                                    <Key size={11} /> {t('disc.overrideKey')}
                                  </button>
                                  <span style={{ fontSize: 10, color: 'rgba(255,255,255,0.25)', alignSelf: 'center' }}>
                                    {t('disc.orCheckAgent')}
                                  </span>
                                </div>
                              )}
                              <div style={{ display: 'flex', alignItems: 'center', justifyContent: 'space-between', marginTop: 4 }}>
                                <div style={{ ...ds.msgTime, display: 'flex', alignItems: 'center', gap: 6 }}>
                                  {new Date(msg.timestamp).toLocaleTimeString(undefined, { hour: '2-digit', minute: '2-digit' })}
                                  {msg.tokens_used > 0 && (
                                    <span style={{ color: 'rgba(255,255,255,0.2)', fontSize: 9 }}>
                                      {msg.tokens_used.toLocaleString()} tok
                                    </span>
                                  )}
                                  {msg.auth_mode && (
                                    <span style={{ color: msg.auth_mode === 'override' ? 'rgba(200,255,0,0.3)' : 'rgba(255,255,255,0.15)', fontSize: 9 }}>
                                      {msg.auth_mode === 'override' ? 'API key' : 'auth locale'}
                                    </span>
                                  )}
                                </div>
                                {!sending && !isEditing && (isLastUser || isLastAgent) && (
                                  <div style={{ display: 'flex', gap: 4 }}>
                                    {isLastUser && (
                                      <button
                                        style={{ ...s.iconBtn, padding: '2px 6px', fontSize: 10, color: 'rgba(255,255,255,0.3)' }}
                                        onClick={() => { setEditingMsgId(msg.id); setEditingText(msg.content); }}
                                        title={t('disc.editResend')}
                                      >
                                        <Pencil size={10} />
                                      </button>
                                    )}
                                    {isLastAgent && (
                                      <button
                                        style={{ ...s.iconBtn, padding: '2px 6px', fontSize: 10, color: 'rgba(255,255,255,0.3)' }}
                                        onClick={handleRetry}
                                        title={t('disc.retryResponse')}
                                      >
                                        <RotateCcw size={10} />
                                      </button>
                                    )}
                                  </div>
                                )}
                              </div>
                            </div>
                          </div>
                          );
                        })}

                        {/* Streaming: single agent mode */}
                        {sending && !orchState[activeDiscussion.id]?.active && (
                          <div style={ds.msgRow(false)}>
                            <div style={ds.msgBubble(false)}>
                              <div style={{ ...ds.msgAgent, color: agentColor(activeDiscussion.agent) }}>
                                <Cpu size={10} /> {activeDiscussion.agent}
                                <Loader2 size={10} style={{ animation: 'spin 1s linear infinite', marginLeft: 4 }} />
                              </div>
                              {streamingText ? (
                                <MarkdownContent content={streamingText} />
                              ) : (
                                <div style={{ fontSize: 12, color: 'rgba(255,255,255,0.3)', fontStyle: 'italic' }}>
                                  {t('disc.running')}
                                </div>
                              )}
                            </div>
                          </div>
                        )}

                        {/* Streaming: orchestration mode */}
                        {orchState[activeDiscussion.id] && (() => {
                          const orch = orchState[activeDiscussion.id];
                          return (
                            <>
                              {orch.agentStreams.map((as_, i) => (
                                <div key={i} style={ds.msgRow(false)}>
                                  <div style={{
                                    ...ds.msgBubble(false),
                                    borderLeft: `3px solid ${agentColor(as_.agentType || as_.agent)}`,
                                  }}>
                                    <div style={{
                                      display: 'flex', alignItems: 'center', gap: 4, fontSize: 10,
                                      fontWeight: 600, color: agentColor(as_.agentType || as_.agent), marginBottom: 4,
                                    }}>
                                      <Cpu size={10} /> {as_.agent}
                                      <span style={{ fontSize: 9, color: 'rgba(255,255,255,0.2)', marginLeft: 4 }}>
                                        {as_.round === 'synthesis' ? t('disc.synthesis') : `Round ${as_.round}`}
                                      </span>
                                      {!as_.done && <Loader2 size={9} style={{ animation: 'spin 1s linear infinite', marginLeft: 4 }} />}
                                    </div>
                                    {as_.text ? (
                                      <MarkdownContent content={as_.text} />
                                    ) : !as_.done ? (
                                      <div style={{ fontSize: 12, color: 'rgba(255,255,255,0.3)', fontStyle: 'italic' }}>
                                        {t('disc.thinking', as_.agent)}
                                      </div>
                                    ) : null}
                                  </div>
                                </div>
                              ))}
                            </>
                          );
                        })()}

                        {/* Validation complete banner */}
                        {(() => {
                          if (activeDiscussion.title !== 'Validation audit AI' || !activeDiscussion.project_id) return null;
                          const proj = projects.find(p => p.id === activeDiscussion.project_id);
                          if (!proj || proj.audit_status !== 'Audited') return null;
                          const lastAgentMsg = [...activeDiscussion.messages].reverse().find(m => m.role === 'Agent');
                          const isComplete = lastAgentMsg && lastAgentMsg.content.includes('KRONN:VALIDATION_COMPLETE');
                          if (!isComplete) return null;
                          return (
                            <div style={{ margin: '12px 16px', padding: '12px 16px', borderRadius: 10, background: 'rgba(200,255,0,0.06)', border: '1px solid rgba(200,255,0,0.15)' }}>
                              <p style={{ fontSize: 12, color: 'rgba(200,255,0,0.8)', margin: '0 0 8px', display: 'flex', alignItems: 'center', gap: 6 }}>
                                <ShieldCheck size={14} /> {t('audit.validationComplete')}
                              </p>
                              <button
                                style={{
                                  padding: '8px 16px', borderRadius: 8, border: 'none',
                                  background: '#c8ff00', color: '#0a0c10', fontWeight: 700,
                                  fontSize: 12, fontFamily: 'inherit', cursor: 'pointer',
                                  display: 'flex', alignItems: 'center', gap: 6,
                                }}
                                onClick={async () => {
                                  await projectsApi.validateAudit(proj.id);
                                  refetch();
                                  refetchDiscussions();
                                }}
                              >
                                <Check size={12} /> {t('audit.markValid')}
                              </button>
                            </div>
                          );
                        })()}

                        <div ref={chatEndRef} />
                      </div>

                      {/* Input */}
                      <div style={ds.inputBar}>
                        <div style={{ flex: 1, position: 'relative' }}>
                          <input
                            ref={chatInputRef}
                            style={ds.chatInput}
                            placeholder={activeDiscussion && (activeDiscussion.participants?.length ?? 0) > 1 && AGENT_MENTIONS.length > 0
                              ? t('disc.mentionHint', AGENT_MENTIONS.map(m => m.trigger).join(', '))
                              : t('disc.messagePlaceholder')}
                            value={chatInput}
                            onChange={e => {
                              const val = e.target.value;
                              setChatInput(val);
                              // Detect @mention at start
                              const atMatch = val.match(/^@(\w*)$/);
                              if (atMatch) {
                                setMentionQuery(atMatch[1].toLowerCase());
                                setMentionIndex(0);
                              } else {
                                setMentionQuery(null);
                              }
                            }}
                            onKeyDown={e => {
                              if (mentionQuery !== null) {
                                const filtered = AGENT_MENTIONS.filter(m => m.trigger.slice(1).startsWith(mentionQuery ?? ''));
                                if (e.key === 'ArrowDown') { e.preventDefault(); setMentionIndex(i => Math.min(i + 1, filtered.length - 1)); return; }
                                if (e.key === 'ArrowUp') { e.preventDefault(); setMentionIndex(i => Math.max(i - 1, 0)); return; }
                                if ((e.key === 'Tab' || e.key === 'Enter') && filtered.length > 0) {
                                  e.preventDefault();
                                  setChatInput(filtered[mentionIndex].trigger + ' ');
                                  setMentionQuery(null);
                                  return;
                                }
                                if (e.key === 'Escape') { setMentionQuery(null); return; }
                              }
                              if (e.key === 'Enter' && !e.shiftKey) { e.preventDefault(); handleSendMessage(); }
                            }}
                            disabled={sending}
                          />
                          {/* @mention autocomplete dropdown */}
                          {mentionQuery !== null && (() => {
                            const filtered = AGENT_MENTIONS.filter(m => m.trigger.slice(1).startsWith(mentionQuery ?? ''));
                            if (filtered.length === 0) return null;
                            return (
                              <div style={{
                                position: 'absolute', bottom: '100%', left: 0, marginBottom: 4,
                                background: '#1a1d26', border: '1px solid rgba(200,255,0,0.2)',
                                borderRadius: 8, overflow: 'hidden', boxShadow: '0 4px 16px rgba(0,0,0,0.4)',
                                minWidth: 180,
                              }}>
                                {filtered.map((m, i) => (
                                  <button
                                    key={m.trigger}
                                    style={{
                                      display: 'flex', alignItems: 'center', gap: 8,
                                      width: '100%', padding: '8px 12px', border: 'none', cursor: 'pointer',
                                      background: i === mentionIndex ? 'rgba(200,255,0,0.1)' : 'transparent',
                                      color: '#e8eaed', fontFamily: 'inherit', fontSize: 12, textAlign: 'left',
                                    }}
                                    onMouseDown={e => {
                                      e.preventDefault();
                                      setChatInput(m.trigger + ' ');
                                      setMentionQuery(null);
                                      chatInputRef.current?.focus();
                                    }}
                                    onMouseEnter={() => setMentionIndex(i)}
                                  >
                                    <Cpu size={12} style={{ color: '#c8ff00' }} />
                                    <span style={{ fontWeight: 600, color: '#c8ff00' }}>{m.trigger}</span>
                                    <span style={{ color: 'rgba(255,255,255,0.4)' }}>{m.label}</span>
                                  </button>
                                ))}
                              </div>
                            );
                          })()}
                        </div>
                        {/* Debate button */}
                        <div style={{ position: 'relative' }}>
                          <button
                            style={{
                              ...ds.sendBtn,
                              background: showDebatePopover ? 'rgba(139,92,246,0.2)' : 'rgba(139,92,246,0.08)',
                              border: '1px solid rgba(139,92,246,0.3)',
                              color: '#8b5cf6',
                            }}
                            onClick={() => {
                              if (!showDebatePopover) {
                                // Pre-select all installed agents
                                setDebateAgents(installedAgents.map(a => a.agent_type));
                              }
                              setShowDebatePopover(!showDebatePopover);
                            }}
                            disabled={sending}
                            title={t('debate.title')}
                          >
                            <Users size={16} />
                          </button>
                          {showDebatePopover && (
                            <div style={{
                              position: 'absolute', bottom: '100%', right: 0, marginBottom: 8,
                              width: 260, padding: 14, borderRadius: 10,
                              background: '#1a1d26', border: '1px solid rgba(139,92,246,0.2)',
                              boxShadow: '0 8px 32px rgba(0,0,0,0.5)',
                            }}>
                              <div style={{ fontSize: 12, fontWeight: 700, color: '#8b5cf6', marginBottom: 10, display: 'flex', alignItems: 'center', gap: 6 }}>
                                <Users size={12} /> {t('debate.header')}
                              </div>
                              <p style={{ fontSize: 11, color: 'rgba(255,255,255,0.4)', marginBottom: 10, lineHeight: 1.4 }}>
                                {t('debate.instructions')}
                              </p>
                              {installedAgents.map(a => {
                                const isPrincipal = a.agent_type === activeDiscussion?.agent;
                                const checked = debateAgents.includes(a.agent_type);
                                return (
                                  <label key={a.name} style={{
                                    display: 'flex', alignItems: 'center', gap: 8, padding: '6px 0',
                                    cursor: isPrincipal ? 'default' : 'pointer', fontSize: 12,
                                    color: checked ? '#e8eaed' : 'rgba(255,255,255,0.4)',
                                  }}>
                                    <input
                                      type="checkbox"
                                      checked={checked}
                                      disabled={isPrincipal}
                                      onChange={() => {
                                        if (isPrincipal) return;
                                        setDebateAgents(prev =>
                                          prev.includes(a.agent_type)
                                            ? prev.filter(t => t !== a.agent_type)
                                            : [...prev, a.agent_type]
                                        );
                                      }}
                                      style={{ accentColor: '#8b5cf6' }}
                                    />
                                    <Cpu size={11} style={{ color: isPrincipal ? '#c8ff00' : '#8b5cf6' }} />
                                    {a.name}
                                    {isPrincipal && (
                                      <span style={{ fontSize: 9, color: '#c8ff00', marginLeft: 'auto' }}>{t('debate.main')}</span>
                                    )}
                                  </label>
                                );
                              })}
                              <div style={{ display: 'flex', alignItems: 'center', gap: 8, marginTop: 10 }}>
                                <span style={{ fontSize: 11, color: 'rgba(255,255,255,0.4)' }}>{t('debate.rounds')}</span>
                                {[1, 2, 3].map(n => (
                                  <button
                                    key={n}
                                    style={{
                                      width: 28, height: 28, borderRadius: 6, border: 'none', fontFamily: 'inherit',
                                      fontSize: 12, fontWeight: 700, cursor: 'pointer',
                                      background: debateRounds === n ? '#8b5cf6' : 'rgba(255,255,255,0.06)',
                                      color: debateRounds === n ? '#fff' : 'rgba(255,255,255,0.4)',
                                    }}
                                    onClick={() => setDebateRounds(n)}
                                  >
                                    {n}
                                  </button>
                                ))}
                              </div>
                              <button
                                style={{
                                  marginTop: 8, width: '100%', padding: '8px 12px', borderRadius: 6,
                                  border: 'none', fontFamily: 'inherit', fontSize: 12, fontWeight: 700, cursor: 'pointer',
                                  background: debateAgents.length >= 2 ? '#8b5cf6' : 'rgba(255,255,255,0.06)',
                                  color: debateAgents.length >= 2 ? '#fff' : 'rgba(255,255,255,0.25)',
                                }}
                                disabled={debateAgents.length < 2}
                                onClick={handleOrchestrate}
                              >
                                {t('debate.launch', debateAgents.length)}
                              </button>
                            </div>
                          )}
                        </div>
                        {sending ? (
                          <button
                            style={{
                              ...ds.sendBtn,
                              background: 'rgba(255,77,106,0.15)',
                              border: '1px solid rgba(255,77,106,0.4)',
                              color: '#ff4d6a',
                            }}
                            onClick={handleStop}
                            title={t('disc.stopThinking')}
                          >
                            <StopCircle size={16} />
                          </button>
                        ) : (
                          <button
                            style={ds.sendBtn}
                            onClick={handleSendMessage}
                            disabled={!chatInput.trim()}
                          >
                            <Send size={16} />
                          </button>
                        )}
                      </div>
                    </>
                  ) : !showNewDiscussion && (
                    <div style={{ display: 'flex', flexDirection: 'column', alignItems: 'center', justifyContent: 'center', flex: 1, color: 'rgba(255,255,255,0.2)' }}>
                      <MessageSquare size={48} style={{ marginBottom: 16, opacity: 0.3 }} />
                      <p style={{ fontSize: 14 }}>{t('disc.selectOrCreate')}</p>
                    </div>
                  )}
                </div>
              </div>
              );
            })()}

            {/* ════════ CONFIG ════════ */}
            {page === 'settings' && (() => {
              return (
              <div>
                <h1 style={s.h1}>Configuration</h1>
                <p style={{ ...s.meta, marginBottom: 20 }}>{t('config.subtitle')}</p>

                {/* UI Language */}
                <div style={s.card(false)}>
                  <div style={{ padding: '16px 20px' }}>
                    <div style={{ display: 'flex', alignItems: 'center', gap: 8, marginBottom: 8 }}>
                      <MessageSquare size={14} style={{ color: '#c8ff00' }} />
                      <span style={{ fontWeight: 600, fontSize: 14 }}>{t('config.uiLanguage')}</span>
                    </div>
                    <p style={{ color: 'rgba(255,255,255,0.35)', fontSize: 12, marginBottom: 12 }}>
                      {t('config.uiLanguageHint')}
                    </p>
                    <div style={{ display: 'flex', gap: 8 }}>
                      {UI_LOCALES.map(l => (
                        <button
                          key={l.code}
                          style={{
                            padding: '8px 16px', borderRadius: 6, cursor: 'pointer',
                            fontFamily: 'inherit', fontSize: 12, fontWeight: 600,
                            background: locale === l.code ? 'rgba(200,255,0,0.15)' : 'rgba(255,255,255,0.04)',
                            color: locale === l.code ? '#c8ff00' : 'rgba(255,255,255,0.4)',
                            border: locale === l.code ? '1px solid rgba(200,255,0,0.3)' : '1px solid rgba(255,255,255,0.08)',
                          }}
                          onClick={() => setLocale(l.code)}
                        >
                          {l.flag} {l.label}
                        </button>
                      ))}
                    </div>
                  </div>
                </div>

                {/* Output Language */}
                <div style={s.card(false)}>
                  <div style={{ padding: '16px 20px' }}>
                    <div style={{ display: 'flex', alignItems: 'center', gap: 8, marginBottom: 8 }}>
                      <MessageSquare size={14} style={{ color: '#c8ff00' }} />
                      <span style={{ fontWeight: 600, fontSize: 14 }}>{t('config.outputLanguage')}</span>
                    </div>
                    <p style={{ color: 'rgba(255,255,255,0.35)', fontSize: 12, marginBottom: 12 }}>
                      {t('config.outputLanguageHint')}
                    </p>
                    <div style={{ display: 'flex', gap: 8 }}>
                      {LANGUAGES.map(l => (
                        <button
                          key={l.code}
                          style={{
                            padding: '8px 16px', borderRadius: 6, cursor: 'pointer',
                            fontFamily: 'inherit', fontSize: 12, fontWeight: 600,
                            background: (configLanguage ?? 'fr') === l.code ? 'rgba(200,255,0,0.15)' : 'rgba(255,255,255,0.04)',
                            color: (configLanguage ?? 'fr') === l.code ? '#c8ff00' : 'rgba(255,255,255,0.4)',
                            border: (configLanguage ?? 'fr') === l.code ? '1px solid rgba(200,255,0,0.3)' : '1px solid rgba(255,255,255,0.08)',
                          }}
                          onClick={async () => {
                            await configApi.saveLanguage(l.code);
                            refetchLanguage();
                          }}
                        >
                          {l.flag} {l.label}
                        </button>
                      ))}
                    </div>
                  </div>
                </div>

                {/* Agents */}
                <div style={s.card(false)}>
                  <div style={{ padding: '16px 20px' }}>
                    <div style={{ display: 'flex', alignItems: 'center', gap: 8, marginBottom: 12 }}>
                      <Cpu size={14} style={{ color: '#c8ff00' }} />
                      <span style={{ fontWeight: 600, fontSize: 14 }}>{t('config.agents')}</span>
                      <span style={{ fontSize: 11, color: 'rgba(255,255,255,0.3)', marginLeft: 'auto' }}>
                        {agents.filter(a => a.installed || a.runtime_available).length}/{agents.length} {agents.filter(a => a.installed || a.runtime_available).length > 1 ? t('config.installedPlural') : t('config.installed')}
                      </span>
                      <button style={s.iconBtn} onClick={() => refetchAgents()} title={t('config.refresh')}>
                        <RefreshCw size={12} />
                      </button>
                    </div>

                    {(() => {
                      const isWSL = agents.some(a => a.host_label === 'WSL');
                      const hasDockerAgent = agents.some(a => a.installed && !a.host_managed);
                      return isWSL && hasDockerAgent ? (
                        <div style={{ padding: '8px 12px', marginBottom: 8, borderRadius: 6, background: 'rgba(255,180,0,0.06)', border: '1px solid rgba(255,180,0,0.15)', display: 'flex', alignItems: 'flex-start', gap: 8 }}>
                          <AlertTriangle size={12} style={{ color: '#ffb400', flexShrink: 0, marginTop: 2 }} />
                          <span style={{ fontSize: 11, color: 'rgba(255,255,255,0.5)', lineHeight: 1.4 }}>{t('config.wslWarning')}</span>
                        </div>
                      ) : null;
                    })()}

                    {agents.map(agent => {
                      const permFlag: Record<string, { flag: string; descKey: string }> = {
                        ClaudeCode: { flag: '--dangerously-skip-permissions', descKey: 'config.fullAccess' },
                        Codex: { flag: '--full-auto', descKey: 'config.autoApply' },
                        GeminiCli: { flag: '--yolo', descKey: 'config.fullAccess' },
                      };
                      const perm = permFlag[agent.agent_type];
                      const tokenField: Record<string, { key: 'anthropic' | 'openai' | 'google'; hint: string; url: string }> = {
                        ClaudeCode: { key: 'anthropic', hint: 'ANTHROPIC_API_KEY', url: 'https://console.anthropic.com/settings/keys' },
                        Codex: { key: 'openai', hint: 'OPENAI_API_KEY', url: 'https://platform.openai.com/api-keys' },
                        GeminiCli: { key: 'google', hint: 'GEMINI_API_KEY', url: 'https://aistudio.google.com/apikey' },
                      };
                      const tf = tokenField[agent.agent_type];
                      const isFullAccess = agent.agent_type === 'ClaudeCode'
                        ? agentAccess?.claude_code?.full_access ?? false
                        : agent.agent_type === 'Codex'
                          ? agentAccess?.codex?.full_access ?? false
                          : agent.agent_type === 'GeminiCli'
                            ? agentAccess?.gemini_cli?.full_access ?? false
                            : false;

                      return (
                      <div key={agent.name} style={{ padding: '10px 0', borderTop: '1px solid rgba(255,255,255,0.05)' }}>
                        <div style={{ display: 'flex', alignItems: 'center', gap: 10 }}>
                          <div style={s.dot((agent.installed || agent.runtime_available) && agent.enabled)} />
                          <div style={{ flex: 1 }}>
                            <div style={{ display: 'flex', alignItems: 'center', gap: 6 }}>
                              <span style={{ fontWeight: 600, fontSize: 12 }}>{agent.name}</span>
                              <span style={s.originBadge}>{agent.origin}</span>
                              {agent.version && <code style={{ ...s.code, fontSize: 10 }}>v{agent.version}</code>}
                              {agent.latest_version && agent.latest_version !== agent.version && (
                                <span style={s.updateBadge}>&#x2B06; {agent.latest_version}</span>
                              )}
                            </div>
                            {!agent.installed && !agent.runtime_available && (
                              <div style={{ fontSize: 10, color: 'rgba(255,255,255,0.25)', marginTop: 2 }}>
                                <code style={s.code}>{agent.install_command}</code>
                              </div>
                            )}
                            {!agent.installed && agent.runtime_available && (
                              <div style={{ fontSize: 10, color: 'rgba(52,211,153,0.5)', marginTop: 2 }}>
                                runtime OK <span style={{ color: 'rgba(255,255,255,0.2)' }}>— via npx</span>
                              </div>
                            )}
                          </div>
                          {(agent.installed || agent.runtime_available) ? (
                            <div style={{ display: 'flex', alignItems: 'center', gap: 6 }}>
                              <button
                                style={{
                                  ...s.iconBtn,
                                  fontSize: 10,
                                  padding: '2px 8px',
                                  borderRadius: 4,
                                  background: agent.enabled ? 'rgba(200,255,0,0.1)' : 'rgba(255,100,0,0.1)',
                                  border: agent.enabled ? '1px solid rgba(200,255,0,0.2)' : '1px solid rgba(255,100,0,0.2)',
                                  color: agent.enabled ? '#c8ff00' : '#ff8c00',
                                }}
                                title={agent.enabled ? t('config.toggleDisable') : t('config.toggleEnable')}
                                onClick={async () => {
                                  try {
                                    await agentsApi.toggle(agent.agent_type);
                                  } catch { /* ignore */ }
                                  refetchAgents();
                                }}
                                disabled={installing !== null}
                              >
                                {agent.enabled ? t('config.enabled') : t('config.disabled')}
                              </button>
                              {agent.host_managed && (
                                <span style={{ fontSize: 9, color: 'rgba(255,255,255,0.25)', marginLeft: 2 }} title={t('config.hostManaged')}>{agent.host_label ?? 'host'}</span>
                              )}
                              <button
                                style={{ ...s.iconBtn, color: 'rgba(255,255,255,0.2)' }}
                                title={t('config.uninstall')}
                                onClick={async () => {
                                  if (!confirm(t('config.uninstallConfirm', agent.name))) return;
                                  setInstalling(agent.name);
                                  try {
                                    await agentsApi.uninstall(agent.agent_type);
                                  } catch {
                                    alert(t('config.uninstallFailed'));
                                    setInstalling(null);
                                    return;
                                  }
                                  // Re-detect: if agent is still installed, uninstall had no effect
                                  const updated = await agentsApi.detect();
                                  const still = updated?.find((a: AgentDetection) => a.agent_type === agent.agent_type);
                                  if (still?.installed) {
                                    alert(t('config.uninstallFailed'));
                                  }
                                  refetchAgents();
                                  setInstalling(null);
                                }}
                                disabled={installing !== null}
                              >
                                <Trash2 size={11} />
                              </button>
                            </div>
                          ) : (
                            <button
                              style={{ ...s.installBtn, padding: '4px 10px', fontSize: 11 }}
                              onClick={() => handleInstallAgent(agent)}
                              disabled={installing !== null}
                            >
                              {installing === agent.name ? (
                                <><Loader2 size={10} style={{ animation: 'spin 1s linear infinite' }} /> ...</>
                              ) : (
                                <><Download size={10} /> Installer</>
                              )}
                            </button>
                          )}
                        </div>
                        {perm && (agent.installed || agent.runtime_available) && (
                          <div style={{ marginLeft: 22, marginTop: 8, padding: '8px 12px', borderRadius: 6, background: 'rgba(255,255,255,0.02)', border: '1px solid rgba(255,255,255,0.05)' }}>
                            <div
                              style={{ display: 'flex', alignItems: 'center', gap: 8, cursor: 'pointer' }}
                              onClick={async () => {
                                await configApi.setAgentAccess({ agent: agent.agent_type, full_access: !isFullAccess });
                                refetchAgentAccess();
                              }}
                            >
                              <div style={{
                                width: 30, height: 16, borderRadius: 8, position: 'relative' as const, transition: 'background 0.2s',
                                background: isFullAccess ? 'rgba(200,255,0,0.3)' : 'rgba(255,255,255,0.1)',
                                border: isFullAccess ? '1px solid rgba(200,255,0,0.4)' : '1px solid rgba(255,255,255,0.15)',
                              }}>
                                <div style={{
                                  width: 12, height: 12, borderRadius: '50%', position: 'absolute' as const, top: 1, transition: 'left 0.2s',
                                  left: isFullAccess ? 16 : 1,
                                  background: isFullAccess ? '#c8ff00' : 'rgba(255,255,255,0.3)',
                                }} />
                              </div>
                              <code style={{ fontSize: 10, color: isFullAccess ? '#c8ff00' : 'rgba(255,255,255,0.4)' }}>{perm.flag}</code>
                            </div>
                            <p style={{ fontSize: 10, color: 'rgba(255,255,255,0.3)', marginTop: 4, marginLeft: 38 }}>
                              {t(perm.descKey)}
                            </p>
                          </div>
                        )}
                        {tf && (agent.installed || agent.runtime_available) && (() => {
                          const providerKeys = tokenConfig?.keys?.filter(k => k.provider === tf.key) ?? [];
                          const isDisabled = tokenConfig?.disabled_overrides?.includes(tf.key);
                          const isAdding = addingKeyFor === tf.key;
                          const newInput = newKeyInputs[tf.key] ?? { name: '', value: '' };
                          return (
                          <div style={{ marginLeft: 22, marginTop: 6 }}>
                            {/* Provider-level override toggle */}
                            {providerKeys.length > 0 && (
                              <div style={{ display: 'flex', alignItems: 'center', gap: 6, marginBottom: 4 }}>
                                <Key size={10} style={{ color: 'rgba(255,255,255,0.25)', flexShrink: 0 }} />
                                <button
                                  style={{ ...s.iconBtn, padding: 0 }}
                                  title={isDisabled ? t('config.enableOverride') : t('config.disableOverride')}
                                  onClick={async () => {
                                    await configApi.toggleTokenOverride(tf.key);
                                    refetchTokens();
                                  }}
                                >
                                  {isDisabled
                                    ? <Play size={10} style={{ color: 'rgba(255,255,255,0.25)' }} />
                                    : <StopCircle size={10} style={{ color: 'rgba(52,211,153,0.5)' }} />}
                                </button>
                                <span style={{ fontSize: 10, color: isDisabled ? 'rgba(255,255,255,0.25)' : 'rgba(52,211,153,0.6)' }}>
                                  {isDisabled ? t('config.overrideDisabled') : t('config.overrideActive')}
                                </span>
                                <a
                                  href={tf.url} target="_blank" rel="noopener noreferrer"
                                  style={{ display: 'flex', alignItems: 'center', color: 'rgba(255,255,255,0.25)', flexShrink: 0, marginLeft: 'auto' }}
                                  title={t('config.getKey')}
                                >
                                  <ExternalLink size={10} />
                                </a>
                              </div>
                            )}

                            {/* Existing keys list */}
                            {providerKeys.map(k => {
                              const isVis = tokenVisible.has(k.id);
                              return (
                              <div key={k.id} style={{
                                display: 'flex', alignItems: 'center', gap: 6, padding: '2px 0 2px 16px',
                                opacity: isDisabled ? 0.4 : 1,
                              }}>
                                {/* Active indicator / activate button */}
                                {k.active ? (
                                  <Check size={9} style={{ color: 'rgba(52,211,153,0.7)', flexShrink: 0 }} />
                                ) : (
                                  <button style={{ ...s.iconBtn, padding: 0 }} title={t('config.activateKey')}
                                    onClick={async () => { await configApi.activateApiKey(k.id); refetchTokens(); }}>
                                    <div style={{ width: 9, height: 9, borderRadius: '50%', border: '1px solid rgba(255,255,255,0.2)' }} />
                                  </button>
                                )}
                                <span style={{ fontSize: 10, color: 'rgba(255,255,255,0.5)', minWidth: 60, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>
                                  {k.name}
                                </span>
                                <span style={{
                                  fontSize: 10, padding: '1px 6px', borderRadius: 4, fontFamily: isVis ? 'monospace' : 'inherit',
                                  background: isDisabled ? 'rgba(255,255,255,0.04)' : 'rgba(52,211,153,0.1)',
                                  color: isDisabled ? 'rgba(255,255,255,0.25)' : 'rgba(52,211,153,0.7)',
                                  textDecoration: isDisabled ? 'line-through' : 'none',
                                }}>
                                  {isVis ? k.masked_value : k.masked_value.replace(/[^.]/g, '\u2022')}
                                </span>
                                <button style={{ ...s.iconBtn, padding: 0 }} title={isVis ? 'Hide' : 'Show'}
                                  onClick={() => setTokenVisible(prev => {
                                    const next = new Set(prev);
                                    if (next.has(k.id)) next.delete(k.id); else next.add(k.id);
                                    return next;
                                  })}>
                                  {isVis ? <EyeOff size={9} style={{ color: '#c8ff00' }} /> : <Eye size={9} style={{ color: 'rgba(255,255,255,0.25)' }} />}
                                </button>
                                <button style={{ ...s.iconBtn, padding: 0 }} title={t('config.deleteKey')}
                                  onClick={async () => {
                                    if (confirm(t('config.deleteKeyConfirm').replace('{0}', k.name))) {
                                      await configApi.deleteApiKey(k.id);
                                      refetchTokens();
                                    }
                                  }}>
                                  <Trash2 size={9} style={{ color: 'rgba(255,107,107,0.5)' }} />
                                </button>
                              </div>
                              );
                            })}

                            {/* No keys yet */}
                            {providerKeys.length === 0 && !isAdding && (
                              <div style={{ display: 'flex', alignItems: 'center', gap: 8, paddingLeft: 16 }}>
                                <Key size={10} style={{ color: 'rgba(255,255,255,0.25)', flexShrink: 0 }} />
                                <span style={{ fontSize: 10, color: 'rgba(255,255,255,0.25)' }}>{t('config.localAuth')}</span>
                                <a href={tf.url} target="_blank" rel="noopener noreferrer"
                                  style={{ display: 'flex', alignItems: 'center', color: 'rgba(255,255,255,0.25)', flexShrink: 0 }}
                                  title={t('config.getKey')}>
                                  <ExternalLink size={10} />
                                </a>
                              </div>
                            )}

                            {/* Add key button / form */}
                            {isAdding ? (
                              <div style={{ display: 'flex', alignItems: 'center', gap: 6, padding: '4px 0 2px 16px' }}>
                                <input
                                  type="text"
                                  style={{ ...s.input, fontSize: 10, padding: '3px 6px', width: 100 }}
                                  placeholder={t('config.keyName')}
                                  value={newInput.name}
                                  onChange={e => setNewKeyInputs(prev => ({ ...prev, [tf.key]: { ...newInput, name: e.target.value } }))}
                                />
                                <input
                                  type="password"
                                  style={{ ...s.input, flex: 1, fontSize: 10, padding: '3px 6px', maxWidth: 180 }}
                                  placeholder={tf.hint}
                                  value={newInput.value}
                                  onChange={e => setNewKeyInputs(prev => ({ ...prev, [tf.key]: { ...newInput, value: e.target.value } }))}
                                />
                                {newInput.value && (
                                  <button style={{ ...s.iconBtn, fontSize: 10, color: '#c8ff00' }}
                                    onClick={async () => {
                                      try {
                                        await configApi.saveApiKey({
                                          id: null,
                                          name: newInput.name || t('config.defaultKeyName'),
                                          provider: tf.key,
                                          value: newInput.value,
                                        });
                                        setNewKeyInputs(prev => ({ ...prev, [tf.key]: { name: '', value: '' } }));
                                        setAddingKeyFor(null);
                                        refetchTokens();
                                        if (confirm(t('config.syncTokensConfirm'))) {
                                          const synced = await configApi.syncAgentTokens();
                                          if (synced.length > 0) {
                                            alert(t('config.syncTokensDone').replace('{0}', synced.join(', ')));
                                          } else {
                                            alert(t('config.syncTokensNone'));
                                          }
                                        }
                                      } catch { /* done */ }
                                    }}>
                                    <Save size={10} />
                                  </button>
                                )}
                                <button style={{ ...s.iconBtn, padding: 0 }} onClick={() => setAddingKeyFor(null)}>
                                  <X size={10} style={{ color: 'rgba(255,255,255,0.3)' }} />
                                </button>
                              </div>
                            ) : (
                              <button
                                style={{ ...s.iconBtn, fontSize: 10, color: 'rgba(255,255,255,0.3)', padding: '2px 0 0 16px', display: 'flex', alignItems: 'center', gap: 4 }}
                                onClick={() => {
                                  setAddingKeyFor(tf.key);
                                  setNewKeyInputs(prev => ({
                                    ...prev,
                                    [tf.key]: { name: providerKeys.length === 0 ? t('config.defaultKeyName') : '', value: '' },
                                  }));
                                }}
                              >
                                <Plus size={9} /> {t('config.addKey')}
                              </button>
                            )}
                          </div>
                          );
                        })()}
                      </div>
                      );
                    })}
                  </div>
                </div>

                {/* Token Usage per Agent */}
                {agentUsageData && agentUsageData.length > 0 && (
                <div style={s.card(false)}>
                  <div style={{ padding: '16px 20px' }}>
                    <div style={{ display: 'flex', alignItems: 'center', gap: 8, marginBottom: 12 }}>
                      <Zap size={14} style={{ color: '#c8ff00' }} />
                      <span style={{ fontWeight: 600, fontSize: 14 }}>{t('config.tokenUsage')}</span>
                      <span style={{ fontSize: 11, color: 'rgba(255,255,255,0.3)', marginLeft: 'auto' }}>
                        {agentUsageData.reduce((s, a) => s + a.total_tokens, 0).toLocaleString()} tokens
                      </span>
                      <button style={s.iconBtn} onClick={() => refetchAgentUsage()} title={t('config.refresh')}>
                        <RefreshCw size={12} />
                      </button>
                    </div>

                    {agentUsageData.map(agent => {
                      const isExpanded = usageExpanded === agent.agent_type;
                      const color = AGENT_COLORS[agent.agent_type] ?? '#8b5cf6';
                      const filteredProjects = isExpanded
                        ? agent.by_project.filter(p => !usageSearch || p.project_name.toLowerCase().includes(usageSearch.toLowerCase()))
                        : [];

                      return (
                      <div key={agent.agent_type} style={{ marginBottom: 8 }}>
                        <div
                          style={{
                            display: 'flex', alignItems: 'center', gap: 8, padding: '8px 12px', cursor: 'pointer',
                            borderRadius: 6, background: isExpanded ? 'rgba(255,255,255,0.04)' : 'transparent',
                          }}
                          onClick={() => setUsageExpanded(isExpanded ? null : agent.agent_type)}
                        >
                          <ChevronRight size={12} style={{ color: 'rgba(255,255,255,0.3)', transform: isExpanded ? 'rotate(90deg)' : 'none', transition: 'transform .15s' }} />
                          <Cpu size={12} style={{ color }} />
                          <span style={{ fontWeight: 600, fontSize: 12, color }}>{agent.agent_type}</span>
                          <span style={{ fontSize: 11, color: 'rgba(255,255,255,0.4)', marginLeft: 'auto' }}>
                            {agent.total_tokens.toLocaleString()} tok
                          </span>
                          <span style={{ fontSize: 10, color: 'rgba(255,255,255,0.2)' }}>
                            {agent.message_count} msg
                          </span>
                        </div>

                        {isExpanded && (
                          <div style={{ paddingLeft: 32, paddingRight: 12, paddingBottom: 8 }}>
                            {agent.by_project.length > 5 && (
                              <input
                                type="text"
                                placeholder={t('projects.search')}
                                value={usageSearch}
                                onChange={e => setUsageSearch(e.target.value)}
                                style={{ ...s.input, fontSize: 10, padding: '4px 8px', marginBottom: 6, width: '100%' }}
                              />
                            )}
                            {filteredProjects.map(p => (
                              <div key={p.project_id} style={{ display: 'flex', alignItems: 'center', gap: 8, padding: '3px 0', fontSize: 11 }}>
                                <span style={{ color: 'rgba(255,255,255,0.6)', flex: 1, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>
                                  {p.project_name}
                                </span>
                                <span style={{ color: 'rgba(255,255,255,0.3)', fontSize: 10, flexShrink: 0 }}>
                                  {p.tokens_used.toLocaleString()} tok
                                </span>
                                <span style={{ color: 'rgba(255,255,255,0.15)', fontSize: 9, flexShrink: 0 }}>
                                  {p.message_count} msg
                                </span>
                              </div>
                            ))}
                            {filteredProjects.length === 0 && usageSearch && (
                              <div style={{ fontSize: 10, color: 'rgba(255,255,255,0.2)', padding: '4px 0' }}>
                                {t('projects.noResult')}
                              </div>
                            )}
                          </div>
                        )}
                      </div>
                      );
                    })}
                  </div>
                </div>
                )}

                {/* Database */}
                <div style={s.card(false)}>
                  <div style={{ padding: '16px 20px' }}>
                    <div style={{ display: 'flex', alignItems: 'center', gap: 8, marginBottom: 12 }}>
                      <HardDrive size={14} style={{ color: '#c8ff00' }} />
                      <span style={{ fontWeight: 600, fontSize: 14 }}>{t('config.database')}</span>
                      {dbInfo && (
                        <span style={{ fontSize: 11, color: 'rgba(255,255,255,0.3)', marginLeft: 'auto' }}>
                          {dbInfo.size_bytes < 1024 * 1024
                            ? `${(dbInfo.size_bytes / 1024).toFixed(1)} Ko`
                            : `${(dbInfo.size_bytes / (1024 * 1024)).toFixed(1)} Mo`}
                        </span>
                      )}
                    </div>

                    {dbInfo && (
                      <div style={{ display: 'flex', gap: 16, marginBottom: 16 }}>
                        {[
                          { label: t('config.dbProjects'), value: dbInfo.project_count },
                          { label: t('config.dbDiscussions'), value: dbInfo.discussion_count },
                          { label: t('config.dbMessages'), value: dbInfo.message_count },
                          { label: t('config.dbMcps'), value: dbInfo.mcp_count },
                          { label: t('config.dbTasks'), value: dbInfo.task_count },
                        ].map(({ label, value }) => (
                          <div key={label} style={{ textAlign: 'center' }}>
                            <div style={{ fontSize: 18, fontWeight: 700, color: '#c8ff00' }}>{value}</div>
                            <div style={{ fontSize: 10, color: 'rgba(255,255,255,0.35)', marginTop: 2 }}>{label}</div>
                          </div>
                        ))}
                      </div>
                    )}

                    <div style={{ display: 'flex', gap: 8 }}>
                      <button
                        style={s.scanBtn}
                        onClick={async () => {
                          const data = await configApi.exportData();
                          if (!data) return;
                          const blob = new Blob([JSON.stringify(data, null, 2)], { type: 'application/json' });
                          const url = URL.createObjectURL(blob);
                          const a = document.createElement('a');
                          a.href = url;
                          a.download = `kronn-export-${new Date().toISOString().slice(0, 10)}.json`;
                          a.click();
                          URL.revokeObjectURL(url);
                        }}
                      >
                        <Download size={12} /> {t('config.export')}
                      </button>
                      <button
                        style={s.scanBtn}
                        onClick={() => {
                          const input = document.createElement('input');
                          input.type = 'file';
                          input.accept = '.json';
                          input.onchange = async () => {
                            const file = input.files?.[0];
                            if (!file) return;
                            try {
                              const text = await file.text();
                              const data = JSON.parse(text);
                              if (!data.version || !data.projects || !data.discussions) {
                                alert(t('config.importInvalid'));
                                return;
                              }
                              if (!confirm(t('config.importConfirm'))) return;
                              await configApi.importData(data);
                              refetch();
                              refetchDiscussions();
                              refetchDbInfo();
                            } catch {
                              alert(t('config.importError'));
                            }
                          };
                          input.click();
                        }}
                      >
                        <Upload size={12} /> {t('config.import')}
                      </button>
                    </div>
                  </div>
                </div>

                {/* General */}
                <div style={{ ...s.card(false), marginTop: 16 }}>
                  <div style={{ padding: '16px 20px' }}>
                    <p style={{ color: 'rgba(255,255,255,0.4)', fontSize: 13, marginBottom: 16 }}>
                      {t('config.configFile')} : <code style={s.code}>~/.config/kronn/config.toml</code>
                    </p>
                    <div style={{ borderTop: '1px solid rgba(255,255,255,0.05)', paddingTop: 16 }}>
                      <p style={{ color: 'rgba(255,255,255,0.35)', fontSize: 12, marginBottom: 12 }}>
                        {t('config.resetHint')}
                      </p>
                      <button style={s.dangerBtn} onClick={onReset}>
                        <Trash2 size={12} /> {t('config.reset')}
                      </button>
                    </div>
                  </div>
                </div>
              </div>
              );
            })()}
          </main>
        </div>
      );
    }

    // ─── Styles ─────────────────────────────────────────────────────────────────

    const s = {
      app: { minHeight: '100vh', background: '#0a0c10' } as const,
      nav: { display: 'flex', alignItems: 'center', gap: 4, padding: '10px 16px', borderBottom: '1px solid rgba(255,255,255,0.07)', background: '#12151c', position: 'sticky' as const, top: 0, zIndex: 50 },
      navBrand: { display: 'flex', alignItems: 'center', gap: 8, marginRight: 20 } as const,
      navTitle: { fontWeight: 700, fontSize: 14, letterSpacing: '-0.02em' } as const,
      navBtn: (active: boolean) => ({ padding: '7px 14px', borderRadius: 6, border: 'none', background: active ? 'rgba(200,255,0,0.1)' : 'transparent', color: active ? '#c8ff00' : 'rgba(255,255,255,0.45)', cursor: 'pointer', fontSize: 12, fontFamily: 'inherit', fontWeight: active ? 600 : 400, display: 'flex', alignItems: 'center', gap: 6 } as const),
      scanBtn: { padding: '7px 14px', borderRadius: 6, border: '1px solid rgba(200,255,0,0.2)', background: 'rgba(200,255,0,0.05)', color: '#c8ff00', cursor: 'pointer', fontSize: 12, fontFamily: 'inherit', fontWeight: 500, display: 'flex', alignItems: 'center', gap: 6 } as const,
      main: { maxWidth: 1000, margin: '0 auto', padding: '24px 20px' } as const,
      mainFull: { margin: 0, padding: '24px 20px', flex: 1 } as const,
      pageHeader: { display: 'flex', justifyContent: 'space-between', alignItems: 'center', marginBottom: 20 } as const,
      h1: { fontSize: 20, fontWeight: 700, letterSpacing: '-0.02em' } as const,
      meta: { color: 'rgba(255,255,255,0.4)', fontSize: 12, marginTop: 4 } as const,
      card: (active: boolean) => ({ background: '#12151c', border: `1px solid ${active ? 'rgba(255,255,255,0.12)' : 'rgba(255,255,255,0.07)'}`, borderRadius: 10, marginBottom: 12, transition: 'border-color 0.2s' } as const),
      cardHeader: { display: 'flex', alignItems: 'center', gap: 12, padding: '16px 20px', cursor: 'pointer' } as const,
      cardBody: { padding: '0 20px 20px', borderTop: '1px solid rgba(255,255,255,0.05)' } as const,
      projName: { fontWeight: 600, fontSize: 14 } as const,
      projPath: { fontSize: 11, color: 'rgba(255,255,255,0.25)', fontFamily: 'JetBrains Mono, monospace', marginTop: 2 } as const,
      projMeta: { display: 'flex', gap: 14, fontSize: 11, color: 'rgba(255,255,255,0.4)', flexShrink: 0 } as const,
      metaItem: { display: 'flex', alignItems: 'center', gap: 4 } as const,
      section: { marginTop: 16 } as const,
      sectionHeader: { display: 'flex', alignItems: 'center', gap: 8, marginBottom: 10, color: 'rgba(255,255,255,0.6)', fontSize: 12 } as const,
      sectionTitle: { fontWeight: 600 } as const,
      sectionLabel: { fontSize: 11, fontWeight: 600, textTransform: 'uppercase' as const, letterSpacing: '0.05em', color: 'rgba(255,255,255,0.4)', marginBottom: 10 },
      count: { fontSize: 10, padding: '1px 6px', borderRadius: 10, background: 'rgba(255,255,255,0.06)', color: 'rgba(255,255,255,0.4)' } as const,
      row: { display: 'flex', alignItems: 'center', gap: 10, padding: '8px 12px', borderRadius: 6, background: 'rgba(255,255,255,0.02)', marginBottom: 4 } as const,
      dot: (on: boolean) => ({ width: 7, height: 7, borderRadius: '50%', background: on ? '#34d399' : 'rgba(255,255,255,0.15)', boxShadow: on ? '0 0 6px rgba(52,211,153,0.4)' : 'none', flexShrink: 0 } as const),
      badge: { display: 'inline-block', padding: '2px 8px', borderRadius: 4, fontSize: 10, background: 'rgba(255,255,255,0.06)', color: 'rgba(255,255,255,0.5)' } as const,
      aiBadge: { display: 'inline-block', padding: '2px 7px', borderRadius: 4, fontSize: 10, fontWeight: 600, background: 'rgba(52,211,153,0.1)', color: 'rgba(52,211,153,0.7)', border: '1px solid rgba(52,211,153,0.15)' } as const,
      badgeGreen: { display: 'inline-flex', alignItems: 'center', gap: 3, padding: '2px 7px', borderRadius: 4, fontSize: 10, fontWeight: 600, background: 'rgba(200,255,0,0.1)', color: 'rgba(200,255,0,0.7)', border: '1px solid rgba(200,255,0,0.15)' } as const,
      badgeOrange: { display: 'inline-flex', alignItems: 'center', gap: 3, padding: '2px 7px', borderRadius: 4, fontSize: 10, fontWeight: 600, background: 'rgba(255,200,0,0.08)', color: 'rgba(255,200,0,0.6)', border: '1px solid rgba(255,200,0,0.12)' } as const,
      badgeGray: { display: 'inline-flex', alignItems: 'center', gap: 3, padding: '2px 7px', borderRadius: 4, fontSize: 10, fontWeight: 600, background: 'rgba(255,255,255,0.03)', color: 'rgba(255,255,255,0.2)', border: '1px solid rgba(255,255,255,0.05)' } as const,
      agentBadge: { display: 'inline-flex', alignItems: 'center', gap: 3, padding: '2px 7px', borderRadius: 4, fontSize: 10, fontWeight: 600, background: 'rgba(139,92,246,0.1)', color: 'rgba(139,92,246,0.7)', border: '1px solid rgba(139,92,246,0.15)' } as const,
      iconBtn: { background: 'none', border: '1px solid rgba(255,255,255,0.08)', borderRadius: 4, padding: '4px 8px', color: 'rgba(255,255,255,0.5)', cursor: 'pointer', display: 'flex', alignItems: 'center', fontSize: 11 } as const,
      dangerBtn: { background: 'rgba(255,77,106,0.08)', border: '1px solid rgba(255,77,106,0.2)', borderRadius: 6, padding: '6px 14px', color: '#ff4d6a', cursor: 'pointer', display: 'flex', alignItems: 'center', gap: 6, fontSize: 12, fontFamily: 'inherit' } as const,
      installBtn: { padding: '6px 14px', background: 'rgba(200,255,0,0.1)', color: '#c8ff00', border: '1px solid rgba(200,255,0,0.2)', borderRadius: 6, fontSize: 12, cursor: 'pointer', display: 'flex', alignItems: 'center', gap: 6, fontFamily: 'inherit' } as const,
      input: { width: '100%', padding: '8px 12px', background: 'rgba(255,255,255,0.03)', border: '1px solid rgba(255,255,255,0.08)', borderRadius: 6, color: '#e8eaed', fontSize: 12, fontFamily: 'inherit', outline: 'none' } as const,
      mcpDropdown: { marginTop: 4, borderRadius: 8, border: '1px solid rgba(255,255,255,0.08)', background: '#181c26', overflow: 'hidden' } as const,
      mcpOption: { display: 'flex', alignItems: 'center', gap: 8, padding: '10px 12px', cursor: 'pointer', fontSize: 12, borderBottom: '1px solid rgba(255,255,255,0.04)' } as const,
      empty: { padding: 16, textAlign: 'center' as const, color: 'rgba(255,255,255,0.25)', fontSize: 12 },
      agentCard: { display: 'flex', alignItems: 'center', gap: 12, padding: '12px 16px', borderRadius: 8, background: 'rgba(255,255,255,0.03)', marginBottom: 6 } as const,
      originBadge: { fontSize: 10, fontWeight: 600, padding: '1px 6px', borderRadius: 4, background: 'rgba(100,180,255,0.1)', color: 'rgba(100,180,255,0.7)', border: '1px solid rgba(100,180,255,0.15)' } as const,
      updateBadge: { fontSize: 10, fontWeight: 600, padding: '1px 6px', borderRadius: 4, background: 'rgba(255,200,0,0.1)', color: '#ffc800', marginLeft: 6 } as const,
      badgeOk: { display: 'flex', alignItems: 'center', gap: 4, fontSize: 11, color: '#34d399', padding: '4px 10px', borderRadius: 20, background: 'rgba(52,211,153,0.1)' } as const,
      code: { fontSize: 11, fontFamily: 'JetBrains Mono, monospace', background: 'rgba(255,255,255,0.06)', padding: '2px 6px', borderRadius: 4 } as const,
      registryChip: { display: 'flex', alignItems: 'center', gap: 4, padding: '5px 10px', borderRadius: 6, background: 'rgba(255,255,255,0.03)', border: '1px solid rgba(255,255,255,0.06)', fontSize: 11, color: 'rgba(255,255,255,0.5)' } as const,
      mcpCard: { display: 'flex', alignItems: 'flex-start', gap: 12, padding: '14px 16px', borderRadius: 8, background: 'rgba(255,255,255,0.03)', marginBottom: 8, border: '1px solid rgba(255,255,255,0.06)' } as const,
      sourceBadge: { fontSize: 10, fontWeight: 600, padding: '1px 6px', borderRadius: 4, background: 'rgba(200,255,0,0.08)', color: 'rgba(200,255,0,0.6)', border: '1px solid rgba(200,255,0,0.12)' } as const,
      projectChip: { display: 'flex', alignItems: 'center', gap: 3, padding: '2px 8px', borderRadius: 4, fontSize: 10, background: 'rgba(255,255,255,0.04)', color: 'rgba(255,255,255,0.4)', border: '1px solid rgba(255,255,255,0.06)' } as const,
      projectToggle: (active: boolean) => ({ display: 'flex', alignItems: 'center', gap: 4, padding: '4px 10px', borderRadius: 5, fontSize: 11, fontFamily: 'inherit', cursor: 'pointer', border: `1px solid ${active ? 'rgba(200,255,0,0.2)' : 'rgba(255,255,255,0.08)'}`, background: active ? 'rgba(200,255,0,0.06)' : 'rgba(255,255,255,0.02)', color: active ? 'rgba(200,255,0,0.8)' : 'rgba(255,255,255,0.35)' } as const),
    };

    // ─── Discussion styles ──────────────────────────────────────────────────────

    const mdStyles: Record<string, React.CSSProperties> = {
      p: { margin: '4px 0' },
      h1: { fontSize: 18, fontWeight: 700, margin: '12px 0 6px', color: '#e8eaed' },
      h2: { fontSize: 16, fontWeight: 700, margin: '10px 0 4px', color: '#e8eaed' },
      h3: { fontSize: 14, fontWeight: 600, margin: '8px 0 4px', color: '#e8eaed' },
      ul: { margin: '4px 0', paddingLeft: 20 },
      ol: { margin: '4px 0', paddingLeft: 20 },
      li: { margin: '2px 0' },
      code: { background: 'rgba(255,255,255,0.08)', padding: '1px 5px', borderRadius: 4, fontSize: 12, fontFamily: 'monospace' },
      pre: { background: 'rgba(0,0,0,0.3)', padding: '10px 12px', borderRadius: 8, overflowX: 'auto', margin: '6px 0', border: '1px solid rgba(255,255,255,0.06)' },
      preCode: { background: 'none', padding: 0, fontSize: 12, fontFamily: 'monospace', color: '#c8ff00' },
      table: { borderCollapse: 'collapse' as const, width: '100%', margin: '8px 0', fontSize: 12 },
      th: { border: '1px solid rgba(255,255,255,0.12)', padding: '6px 10px', background: 'rgba(255,255,255,0.05)', fontWeight: 600, textAlign: 'left' as const },
      td: { border: '1px solid rgba(255,255,255,0.08)', padding: '5px 10px' },
      blockquote: { borderLeft: '3px solid rgba(200,255,0,0.3)', margin: '6px 0', paddingLeft: 12, color: 'rgba(255,255,255,0.6)' },
      hr: { border: 'none', borderTop: '1px solid rgba(255,255,255,0.1)', margin: '10px 0' },
      a: { color: '#c8ff00', textDecoration: 'underline' },
      strong: { fontWeight: 700, color: '#f0f0f0' },
    };

    const MarkdownContent = ({ content }: { content: string }) => (
      <div style={{ fontSize: 13, lineHeight: 1.55 }}>
        <ReactMarkdown
          remarkPlugins={[remarkGfm]}
          components={{
            p: ({ children }) => <p style={mdStyles.p}>{children}</p>,
            h1: ({ children }) => <h1 style={mdStyles.h1}>{children}</h1>,
            h2: ({ children }) => <h2 style={mdStyles.h2}>{children}</h2>,
            h3: ({ children }) => <h3 style={mdStyles.h3}>{children}</h3>,
            ul: ({ children }) => <ul style={mdStyles.ul}>{children}</ul>,
            ol: ({ children }) => <ol style={mdStyles.ol}>{children}</ol>,
            li: ({ children }) => <li style={mdStyles.li}>{children}</li>,
            code: ({ className, children }) => {
              const isBlock = className?.includes('language-');
              return isBlock
                ? <code style={mdStyles.preCode}>{children}</code>
                : <code style={mdStyles.code}>{children}</code>;
            },
            pre: ({ children }) => <pre style={mdStyles.pre}>{children}</pre>,
            table: ({ children }) => <table style={mdStyles.table}>{children}</table>,
            th: ({ children }) => <th style={mdStyles.th}>{children}</th>,
            td: ({ children }) => <td style={mdStyles.td}>{children}</td>,
            blockquote: ({ children }) => <blockquote style={mdStyles.blockquote}>{children}</blockquote>,
            hr: () => <hr style={mdStyles.hr} />,
            a: ({ href, children }) => <a href={href} style={mdStyles.a} target="_blank" rel="noopener noreferrer">{children}</a>,
            strong: ({ children }) => <strong style={mdStyles.strong}>{children}</strong>,
          }}
        >
          {content}
        </ReactMarkdown>
      </div>
    );

    const ds = {
      sidebar: { width: 280, borderRight: '1px solid rgba(255,255,255,0.07)', background: '#0e1117', display: 'flex', flexDirection: 'column' as const, flexShrink: 0 },
      sidebarHeader: { display: 'flex', justifyContent: 'space-between', alignItems: 'center', padding: '14px 14px 10px', borderBottom: '1px solid rgba(255,255,255,0.06)' } as const,
      sidebarList: { flex: 1, overflowY: 'auto' as const, padding: '8px 0' },
      projectGroup: {
        fontSize: 10, fontWeight: 700, textTransform: 'uppercase' as const, letterSpacing: '0.06em',
        color: 'rgba(200,255,0,0.5)', padding: '14px 14px 6px',
        marginTop: 4, borderTop: '1px solid rgba(255,255,255,0.05)',
        display: 'flex', alignItems: 'center', gap: 6,
      },
      discItem: (active: boolean) => ({
        display: 'flex', alignItems: 'center', gap: 8, width: '100%', padding: '8px 14px 8px 22px', border: 'none',
        background: active ? 'rgba(200,255,0,0.06)' : 'transparent',
        borderLeft: active ? '2px solid #c8ff00' : '2px solid transparent',
        color: active ? '#e8eaed' : 'rgba(255,255,255,0.5)',
        cursor: 'pointer', textAlign: 'left' as const, fontFamily: 'inherit',
      }),
      chatArea: { flex: 1, display: 'flex', flexDirection: 'column' as const, minWidth: 0, background: '#0a0c10' },
      chatHeader: { display: 'flex', alignItems: 'center', gap: 12, padding: '14px 20px', borderBottom: '1px solid rgba(255,255,255,0.07)', background: '#12151c', flexShrink: 0 } as const,
      messages: { flex: 1, overflowY: 'auto' as const, padding: '20px 20px 10px' },
      msgRow: (isUser: boolean) => ({ display: 'flex', justifyContent: isUser ? 'flex-end' : 'flex-start', marginBottom: 12 }),
      msgBubble: (isUser: boolean) => ({
        maxWidth: '70%', padding: '10px 14px', borderRadius: 12,
        background: isUser ? 'rgba(200,255,0,0.08)' : 'rgba(255,255,255,0.04)',
        border: `1px solid ${isUser ? 'rgba(200,255,0,0.15)' : 'rgba(255,255,255,0.07)'}`,
        color: '#e8eaed',
      }),
      msgAgent: { display: 'flex', alignItems: 'center', gap: 4, fontSize: 10, fontWeight: 600, color: 'rgba(139,92,246,0.7)', marginBottom: 4 } as const,
      msgTime: { fontSize: 10, color: 'rgba(255,255,255,0.2)', marginTop: 4, textAlign: 'right' as const },
      inputBar: { display: 'flex', gap: 8, padding: '12px 20px', borderTop: '1px solid rgba(255,255,255,0.07)', background: '#12151c', flexShrink: 0 } as const,
      chatInput: { width: '100%', padding: '10px 14px', background: 'rgba(255,255,255,0.04)', border: '1px solid rgba(255,255,255,0.1)', borderRadius: 8, color: '#e8eaed', fontSize: 13, fontFamily: 'inherit', outline: 'none' } as const,
      sendBtn: { padding: '10px 16px', borderRadius: 8, border: '1px solid rgba(200,255,0,0.2)', background: 'rgba(200,255,0,0.1)', color: '#c8ff00', cursor: 'pointer', display: 'flex', alignItems: 'center', justifyContent: 'center' } as const,
      newDiscOverlay: { display: 'flex', alignItems: 'center', justifyContent: 'center', flex: 1 } as const,
      newDiscCard: { width: 420, padding: 24, borderRadius: 12, background: '#12151c', border: '1px solid rgba(255,255,255,0.1)' } as const,
      label: { display: 'block', fontSize: 11, fontWeight: 600, color: 'rgba(255,255,255,0.5)', marginBottom: 4, marginTop: 8 } as const,
      select: { width: '100%', padding: '8px 12px', background: 'rgba(255,255,255,0.04)', border: '1px solid rgba(255,255,255,0.1)', borderRadius: 6, color: '#e8eaed', fontSize: 12, fontFamily: 'inherit', outline: 'none' } as const,
      selectStyled: {
        width: '100%', padding: '9px 12px', background: '#1a1d26', border: '1px solid rgba(255,255,255,0.12)',
        borderRadius: 8, color: '#e8eaed', fontSize: 13, fontFamily: 'inherit', outline: 'none',
        cursor: 'pointer', appearance: 'none' as const,
        backgroundImage: `url("data:image/svg+xml,%3Csvg xmlns='http://www.w3.org/2000/svg' width='12' height='12' viewBox='0 0 24 24' fill='none' stroke='%23888' stroke-width='2'%3E%3Cpolyline points='6 9 12 15 18 9'%3E%3C/polyline%3E%3C/svg%3E")`,
        backgroundRepeat: 'no-repeat', backgroundPosition: 'right 10px center',
        paddingRight: 32,
      } as const,
      inputStyled: {
        width: '100%', padding: '9px 12px', background: '#1a1d26', border: '1px solid rgba(255,255,255,0.12)',
        borderRadius: 8, color: '#e8eaed', fontSize: 13, fontFamily: 'inherit', outline: 'none',
        boxSizing: 'border-box' as const,
      } as const,
      textareaStyled: {
        width: '100%', padding: '10px 12px', background: '#1a1d26', border: '1px solid rgba(255,255,255,0.12)',
        borderRadius: 8, color: '#e8eaed', fontSize: 13, fontFamily: 'inherit', outline: 'none',
        resize: 'vertical' as const, boxSizing: 'border-box' as const, lineHeight: 1.5,
      } as const,
      textarea: { width: '100%', padding: '10px 12px', background: 'rgba(255,255,255,0.04)', border: '1px solid rgba(255,255,255,0.1)', borderRadius: 6, color: '#e8eaed', fontSize: 12, fontFamily: 'inherit', outline: 'none', resize: 'vertical' as const } as const,
    };