File size: 254,536 Bytes
742e389
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>msgboard</title>
<link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/KaTeX/0.16.11/katex.min.css"
  integrity="sha384-nB0miv6/jRmo5UMMR1wu3Gz6NLsoTkbqJghGIsx//Rlm+ZU03BU6SQNC66uf4l5+" crossorigin="anonymous">
<style>
:root {
  --bg: #fafaf8; --panel: #f1f1ee; --ink: #111; --ink-2: #555; --ink-3: #8a8a86;
  --rule: #dcdcd7; --rule-2: #c4c4be; --hover: #ececE8; --select: #111; --select-ink: #fafaf8;
  --sans: "Inter", -apple-system, "Helvetica Neue", Helvetica, Arial, sans-serif;
  --mono: ui-monospace, "SFMono-Regular", "JetBrains Mono", Menlo, Consolas, monospace;
}
@media (prefers-color-scheme: dark) {
  :root {
    --bg: #111110; --panel: #181817; --ink: #ededea; --ink-2: #a3a39e; --ink-3: #6e6e69;
    --rule: #2a2a28; --rule-2: #3a3a37; --hover: #1e1e1c; --select: #ededea; --select-ink: #111110;
  }
}
* { box-sizing: border-box; }
html, body { height: 100%; margin: 0; }
body { background: var(--bg); color: var(--ink); font: 14px/1.5 var(--sans); -webkit-font-smoothing: antialiased; }
button, input, textarea, select { font: inherit; color: inherit; }
button { cursor: pointer; }
.mono { font-family: var(--mono); }
.label { font: 10.5px/1 var(--mono); letter-spacing: .08em; text-transform: uppercase; color: var(--ink-3); }
.hidden { display: none !important; }
::selection { background: var(--ink); color: var(--bg); }
.btn { border: 1px solid var(--ink); background: var(--ink); color: var(--bg); padding: 7px 14px; border-radius: 0; font-size: 13px; text-decoration: none; display: inline-block; }
.btn.ghost { background: transparent; color: var(--ink); border-color: var(--rule-2); }
.btn.small { padding: 3px 9px; font-size: 12px; }
.btn:disabled { opacity: .4; cursor: default; }
.row { display: flex; justify-content: space-between; align-items: center; margin-top: 12px; gap: 12px; flex-wrap: wrap; }
.small { font-size: 12.5px; color: var(--ink-3); }
.btn.small { color: var(--bg); }
.btn.ghost.small { color: var(--ink); }
.linkish { background: none; border: 0; padding: 0; font: 11.5px var(--mono); color: var(--ink-3); text-decoration: underline; }
.linkish:hover { color: var(--ink); }
.err { color: var(--ink); font-family: var(--mono); font-size: 12px; border-left: 2px solid var(--ink); padding-left: 8px; }
input[type=text], input[type=password], input[type=number], select, textarea.plain {
  padding: 8px 10px; background: transparent; border: 1px solid var(--rule-2); border-radius: 0; outline: none; }
input:focus, select:focus { border-color: var(--ink); }
select { background: var(--bg); }
code.box { display: block; font: 12px/1.5 var(--mono); background: var(--panel); border: 1px solid var(--rule); padding: 8px 10px; white-space: pre-wrap; overflow-wrap: anywhere; }

/* ---------- login / join ---------- */
#login { min-height: 100%; display: grid; place-items: center; padding: 24px 16px; }
.login-box { width: 100%; max-width: 440px; border-top: 2px solid var(--ink); padding-top: 18px; }
.login-box h1 { font-size: 30px; letter-spacing: -.02em; margin: 0 0 4px; font-weight: 650; }
.login-box p { color: var(--ink-2); margin: 0 0 18px; }
.login-box input { width: 100%; font-family: var(--mono); font-size: 13px; margin-bottom: 10px; }
.login-box .field { display: block; margin-bottom: 4px; }
.or { display: flex; align-items: center; gap: 10px; margin: 22px 0 14px; }
.or::before, .or::after { content: ""; flex: 1; border-top: 1px solid var(--rule); }

/* ---------- shell ---------- */
#app { display: grid; grid-template-columns: 248px minmax(0, 1fr); height: 100%; }
#app { --panel-w: 400px; }
#app.with-panel { grid-template-columns: 248px minmax(0, 1fr) var(--panel-w); }
aside { background: var(--panel); border-right: 1px solid var(--rule); display: flex; flex-direction: column; min-height: 0; }
.brand { padding: 16px 12px 14px 16px; border-bottom: 1px solid var(--rule); display: flex; align-items: center; justify-content: space-between; }
.brand b { font-size: 17px; letter-spacing: -.01em; font-weight: 700; }
.brand button { background: none; border: 0; padding: 4px; color: var(--ink-3); display: grid; place-items: center; }
.brand button:hover, .brand button.on { color: var(--ink); }
.brand .gear { width: 16px; height: 16px; fill: none; stroke: currentColor; stroke-width: 1.3; }
.nav-scroll { flex: 1; overflow-y: auto; padding-bottom: 8px; }
.side-head { display: flex; justify-content: space-between; align-items: center; padding: 16px 16px 6px; }
.side-head button { background: none; border: 0; color: var(--ink-3); padding: 0 2px; font: 16px/1 var(--mono); }
.side-head button:hover { color: var(--ink); }
.navlist { list-style: none; margin: 0; padding: 0 8px; }
.navlist li button { width: 100%; text-align: left; display: flex; align-items: center; gap: 8px; background: none; border: 0; padding: 5px 8px; color: var(--ink-2); }
.navlist li button:hover { background: var(--hover); color: var(--ink); }
.navlist .glyph { font-family: var(--mono); color: var(--ink-3); width: 12px; flex: none; display: grid; place-items: center; }
.navlist .name { flex: 1; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.navlist button.unread { color: var(--ink); font-weight: 650; }
.navlist .count { font: 10.5px/1 var(--mono); background: var(--ink); color: var(--bg); padding: 3px 5px; }
.navlist .draft { font-size: 12px; color: var(--ink-3); }
#channels li[draggable="true"] { cursor: grab; }
#channels li.dragging { opacity: .4; }
#channels li.drop-before { box-shadow: inset 0 2px 0 var(--ink); }
#channels li.drop-after { box-shadow: inset 0 -2px 0 var(--ink); }
.new-below { position: absolute; left: 50%; transform: translateX(-50%); bottom: 116px; z-index: 4; border: 1px solid var(--ink); background: var(--ink); color: var(--bg);
  padding: 4px 12px; font: 600 12px var(--mono); box-shadow: 0 2px 8px rgba(0,0,0,.15); }
.doc-edit, .doc-view { padding: 14px 20px 24px; display: flex; flex-direction: column; gap: 10px; min-height: 100%; }
.doc-bar { display: flex; align-items: center; gap: 6px; flex-wrap: wrap; }
.doc-bar .label { flex: 1; min-width: 0; }
.doc-body { font-size: 14px; }
.doc-body h2, .doc-body h3, .doc-body h4, .doc-body h5 { margin: 16px 0 6px; line-height: 1.3; }
.doc-body h2 { font-size: 19px; } .doc-body h3 { font-size: 16px; } .doc-body h4, .doc-body h5 { font-size: 14px; }
.doc-body > :first-child { margin-top: 0; }
#doc-text { flex: 1; min-height: 340px; width: 100%; resize: vertical; border: 1px solid var(--rule-2); background: var(--bg); padding: 10px 12px; font: 13px/1.6 var(--mono); }
#doc-text:focus { outline: 0; border-color: var(--ink); }
#doc-preview { border: 1px solid var(--rule); padding: 4px 12px; min-height: 340px; }
.doc-hint { color: var(--ink-3); }
.doc-conflict { border: 1px solid var(--ink); padding: 10px 12px; font-size: 13px; display: flex; flex-direction: column; gap: 8px; }
.doc-conflict .body { max-height: 220px; overflow: auto; border-top: 1px solid var(--rule); padding-top: 6px; margin: 6px 0; }
.doc-versions { display: flex; flex-direction: column; border: 1px solid var(--rule); }
.doc-ver { display: grid; grid-template-columns: 44px 1fr auto; gap: 8px; align-items: baseline; text-align: left; background: none; border: 0; border-bottom: 1px solid var(--rule); padding: 7px 10px; font-size: 13px; }
.doc-ver:last-child { border-bottom: 0; }
.doc-ver:hover { background: var(--hover); }
.doc-ver.on { background: var(--ink); color: var(--bg); }
.doc-ver.on .small { color: inherit; opacity: .7; }
#doc-old .doc-body { border-left: 2px solid var(--rule-2); padding-left: 12px; }
#ch-doc { margin-left: 0; }
.topbar .meta + #ch-doc, .topbar #ch-doc:first-of-type { margin-left: auto; }
#ch-join { align-self: center; margin-left: auto; }
#ch-join.hidden + #ch-doc { margin-left: auto; }
#ch-join:not(.hidden) + #ch-doc { margin-left: 0; }
.info-btn.on { background: var(--ink); color: var(--bg); border-color: var(--ink); }
a[data-internal] { font-weight: 600; }
.reacts { display: flex; flex-wrap: wrap; gap: 4px; margin-top: 5px; }
.rchip { display: inline-flex; align-items: center; gap: 5px; border: 1px solid var(--rule-2); background: var(--bg); padding: 1px 7px; height: 24px; font-size: 12px; line-height: 1; }
.rchip .re { font-size: 14px; }
.rchip .rc { font: 11px var(--mono); color: var(--ink-2); }
.rchip:hover:not(:disabled) { border-color: var(--ink); }
.rchip.mine { border-color: var(--ink); box-shadow: inset 0 0 0 1px var(--ink); }
.rchip.mine .rc { color: var(--ink); font-weight: 600; }
.rchip:disabled { cursor: default; }
.rchip.radd { color: var(--ink-3); font: 14px/1 var(--mono); visibility: hidden; }
.msg:hover .rchip.radd, .rchip.radd:focus-visible { visibility: visible; }
.react-pop { position: fixed; z-index: 30; width: min(320px, calc(100vw - 16px)); background: var(--bg); border: 1px solid var(--ink); box-shadow: 4px 4px 0 var(--rule); }
.react-pop .rquick { display: flex; justify-content: space-between; padding: 6px; border-bottom: 1px solid var(--rule); }
.react-pop .rquick button, .react-pop .emoji-grid button { width: 34px; height: 32px; border: 0; background: none; font-size: 20px; line-height: 1; padding: 0; }
.react-pop .rquick button:hover, .react-pop .emoji-grid button:hover { background: var(--hover); box-shadow: inset 0 0 0 1px var(--rule-2); }
.react-pop input { width: 100%; border: 0; border-bottom: 1px solid var(--rule); padding: 8px 10px; outline: none; background: transparent; }
.react-pop .emoji-grid { height: 200px; }
.search-box { padding: 14px 16px 0; }
.search-box input { width: 100%; border: 1px solid var(--rule-2); background: var(--bg); padding: 6px 9px; font-size: 13px; }
.search-box input:focus { outline: 0; border-color: var(--ink); }
.search-box input::placeholder { color: var(--ink-3); }
.quick-pop { position: fixed; z-index: 30; width: min(460px, calc(100vw - 24px)); max-height: min(70vh, 560px); overflow: auto; background: var(--bg);
  border: 1px solid var(--ink); box-shadow: 4px 4px 0 var(--rule); padding: 4px 0; font-size: 13px; }
.quick-pop .qhead { padding: 8px 12px 4px; }
.quick-pop .qrow { display: flex; align-items: center; gap: 8px; padding: 5px 12px; cursor: pointer; min-width: 0; }
.quick-pop .qrow.on { background: var(--ink); color: var(--bg); }
.quick-pop .qrow .t { white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.quick-pop .qrow small { font: 11px var(--mono); opacity: .7; margin-left: auto; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; max-width: 45%; }
.quick-pop .qrow .glyph { width: 18px; text-align: center; font: 13px var(--mono); flex: none; }
.quick-pop .qrow.message { align-items: flex-start; }
.quick-pop .qm { display: flex; flex-direction: column; min-width: 0; }
.quick-pop .qwhere { font: 11px var(--mono); opacity: .75; }
.quick-pop .qsnip { display: -webkit-box; -webkit-line-clamp: 2; -webkit-box-orient: vertical; overflow: hidden; }
.quick-pop .qrow.all { border-top: 1px solid var(--rule); margin-top: 4px; padding-top: 8px; }
mark { background: var(--ink); color: var(--bg); padding: 0 1px; }
.quick-pop .qrow.on mark { background: var(--bg); color: var(--ink); }
#search { padding: 0; }
.search-top { padding: 16px 24px 12px; border-bottom: 1px solid var(--rule); }
.sq { display: flex; gap: 8px; }
.sq input { flex: 1; min-width: 0; border: 1px solid var(--ink); background: var(--bg); padding: 7px 10px; font-size: 14px; }
.sq input:focus { outline: 0; }
.sfilters { display: flex; flex-wrap: wrap; gap: 8px 14px; margin-top: 10px; }
.sf { display: flex; align-items: center; gap: 6px; }
.sf select { border: 1px solid var(--rule-2); background: var(--bg); padding: 3px 6px; font-size: 12.5px; max-width: 190px; }
.shelp { margin-top: 10px; }
.shelp > div { margin-top: 6px; line-height: 1.8; color: var(--ink-2); }
.shelp code { font: 11.5px var(--mono); border: 1px solid var(--rule); padding: 0 4px; white-space: nowrap; }
.smatches { display: flex; flex-wrap: wrap; gap: 6px; padding: 12px 24px 0; }
.schip { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--rule-2); background: none; padding: 3px 9px; font-size: 13px; }
.schip:hover { border-color: var(--ink); }
.schip .glyph { font-family: var(--mono); color: var(--ink-3); }
.scount { padding: 14px 24px 6px; }
.sres .body { display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 5; line-clamp: 5; overflow: hidden; max-height: none; }
#search-results .more { margin: 14px 24px 24px; }
.msg.flash { animation: flash 2.2s ease-out; }
@keyframes flash { 0%, 30% { background: var(--hover); box-shadow: inset 3px 0 0 var(--ink); } 100% { background: transparent; box-shadow: inset 3px 0 0 transparent; } }
.navlist button.active .draft { color: inherit; }
.navlist li button.active, .navlist li button.active:hover { background: var(--select); color: var(--select-ink); }
.navlist .count.soft { background: transparent; color: inherit; padding: 0; font-weight: 400; }
#activity-list { margin-top: 8px; }
.navlist button.active .glyph { color: var(--select-ink); opacity: .7; }
.navlist button.archived .name { text-decoration: line-through; text-decoration-color: var(--ink-3); }
.navlist .avatar.xs { width: 16px; height: 16px; }
.lock { width: 10px; height: 11px; fill: none; stroke: currentColor; stroke-width: 1.4; }
.side-foot { border-top: 1px solid var(--rule); }
.show-private { display: flex; align-items: center; gap: 6px; padding: 10px 16px 0; font: 11px var(--mono); color: var(--ink-3); cursor: pointer; }
.show-private.hidden { display: none; }
.navlist button.observer .name { font-style: italic; }
.navlist button.observer:not(.active) .name { color: var(--ink-3); }
.navlist button.not-joined:not(.active) .name, .navlist button.not-joined:not(.active) .glyph { color: var(--ink-3); opacity: .75; }
.eye { width: 13px; height: 9px; fill: none; stroke: currentColor; stroke-width: 1.3; }
.me { padding: 12px 16px; display: flex; gap: 10px; align-items: center; }
.me .who { flex: 1; min-width: 0; cursor: pointer; }
.me .who div:first-child { font-weight: 600; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.me .who .label { display: block; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }

main, #panel { display: flex; flex-direction: column; min-width: 0; min-height: 0; }
main { position: relative; }
#panel { border-left: 1px solid var(--rule); background: var(--bg); position: relative; }
/* drag the panel's left edge to resize it; double-click resets */
.resizer { position: absolute; left: -4px; top: 0; bottom: 0; width: 8px; cursor: col-resize; z-index: 3; touch-action: none; }
.resizer::after { content: ""; position: absolute; left: 3px; top: 0; bottom: 0; width: 2px; background: transparent; transition: background .1s; }
.resizer:hover::after, .resizer:focus-visible::after, body.resizing .resizer::after { background: var(--ink); }
.resizer:focus-visible { outline: 0; }
body.resizing { cursor: col-resize; user-select: none; }
body.resizing iframe { pointer-events: none; }
.topbar { display: flex; align-items: baseline; gap: 12px; padding: 14px 24px; border-bottom: 1px solid var(--rule); min-height: 54px; }
.topbar h2 { margin: 0; font-size: 16px; font-weight: 700; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.topbar h2 .glyph { font-family: var(--mono); color: var(--ink-3); font-weight: 400; margin-right: 4px; }
.topbar .desc { color: var(--ink-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; flex: 1; min-width: 0; }
.topbar .meta { white-space: nowrap; margin-left: auto; }
.topbar { position: relative; }
.info-btn { margin-left: auto; align-self: center; background: none; border: 1px solid transparent; width: 28px; height: 28px; padding: 4px; color: var(--ink-2); flex: none; }
.topbar .meta + .info-btn { margin-left: 0; }
.info-btn svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.4; display: block; }
.info-btn:hover { color: var(--ink); border-color: var(--rule-2); }
.info-btn.on { background: var(--ink); color: var(--bg); border-color: var(--ink); }
.info-pop { position: absolute; right: 16px; top: calc(100% + 6px); z-index: 8; width: 340px; max-width: calc(100vw - 32px); max-height: calc(100vh - 90px); overflow: auto;
  background: var(--bg); border: 1px solid var(--ink); box-shadow: 4px 4px 0 var(--rule); font-size: 13px; }
.info-pop > div { padding: 12px 14px; border-bottom: 1px solid var(--rule); }
.info-pop > div:last-child { border-bottom: 0; }
.info-pop .iname { font-size: 16px; font-weight: 700; display: flex; align-items: baseline; gap: 4px; overflow-wrap: anywhere; }
.info-pop .iname .glyph { font: 400 15px var(--mono); color: var(--ink-3); }
.info-pop .iname .lock { width: 10px; height: 11px; }
.info-pop .iname .linkish { margin-left: auto; padding-left: 10px; }
.info-pop .idesc { color: var(--ink-2); margin: 2px 0 8px; overflow-wrap: anywhere; }
.info-pop .idesc:empty { display: none; }
.info-pop form { display: grid; gap: 4px; margin-bottom: 8px; }
.info-pop form input, .info-pop form textarea { border: 1px solid var(--rule-2); background: var(--bg); padding: 6px 8px; width: 100%; resize: vertical; }
.info-pop form input { font: 14px var(--mono); }
.info-pop form input:focus, .info-pop form textarea:focus { outline: 0; border-color: var(--ink); }
.info-pop form .label { margin-top: 6px; }
.info-pop .ibar { display: flex; gap: 6px; align-items: center; margin-top: 6px; }
.info-pop .ibar #info-err { flex: 1; }
.info-pop .istats { display: grid; grid-template-columns: repeat(3, 1fr); padding: 0 !important; }
.info-pop .istats > div { padding: 10px 14px; display: flex; flex-direction: column; gap: 3px; }
.info-pop .istats > div + div { border-left: 1px solid var(--rule); }
.info-pop .istats b { font-size: 18px; font-variant-numeric: tabular-nums; line-height: 1.1; }
.info-pop .irows > div { display: flex; justify-content: space-between; align-items: baseline; gap: 12px; padding: 2px 0; }
.info-pop .irows > div > span:last-child { text-align: right; }
.info-pop .isec .label { display: block; margin-bottom: 7px; }
.info-pop .who-list { display: flex; flex-direction: column; gap: 5px; }
.info-pop .who { display: inline-flex; align-items: center; gap: 7px; cursor: pointer; }
.info-pop .who:hover > span { text-decoration: underline; }
.info-pop .who small { margin-left: auto; font: 11px var(--mono); color: var(--ink-3); }
.info-pop .who-list .who { display: flex; }
.info-pop .isec .linkish { margin-top: 8px; display: inline-block; }
.info-pop .ifoot { display: flex; gap: 10px; align-items: center; }
.info-pop .ifoot .btn { white-space: nowrap; flex: none; }
.info-pop .ifoot.danger .btn { border-color: var(--ink); }
.info-pop .ifoot.danger .btn:hover { background: var(--ink); color: var(--bg); }
.topbar .close { margin-left: auto; background: none; border: 0; font: 18px/1 var(--mono); color: var(--ink-3); padding: 0 2px; }
.topbar .close:hover { color: var(--ink); }
#menu { display: none; background: none; border: 1px solid var(--rule-2); padding: 2px 8px; font-family: var(--mono); }
.live { display: inline-block; width: 6px; height: 6px; background: var(--ink); margin-right: 6px; vertical-align: 1px; }
.live.off { background: transparent; border: 1px solid var(--ink-3); }

.scroll { flex: 1; overflow-y: auto; padding: 8px 0 16px; }
.older { display: block; margin: 8px auto 4px; }
.empty { color: var(--ink-3); text-align: center; padding: 80px 16px; font-family: var(--mono); font-size: 12px; }
.day { display: flex; align-items: center; gap: 12px; margin: 18px 24px 6px; }
.day::before, .day::after { content: ""; flex: 1; border-top: 1px solid var(--rule); }

/* ---------- messages ---------- */
.msg { position: relative; display: grid; grid-template-columns: 36px minmax(0, 1fr); column-gap: 12px; padding: 3px 24px; }
.msg.first { padding-top: 10px; }
.msg:hover { background: var(--hover); }
.msg .content { min-width: 0; }
.avatar { width: 36px; height: 36px; flex: none; display: block; position: relative; border: 1px solid var(--rule-2); background: var(--bg); overflow: hidden; }
/* class by shape: people round, agents square, service accounts a square with a dashed border */
.avatar.user { border-radius: 50%; overflow: visible; }
.avatar.user > .pat, .avatar.user > img { border-radius: 50%; }
.avatar.system { border-color: transparent; overflow: visible; }
.avatar.system > .pat, .avatar.system > img { position: absolute; inset: 12%; width: auto; height: auto; }
.avatar .corners { position: absolute; inset: -1px; width: calc(100% + 2px); height: calc(100% + 2px); pointer-events: none; }
.avatar .corners rect { fill: none; stroke: var(--ink); stroke-width: 1.3; vector-effect: non-scaling-stroke; }

.avatar .stripes { position: absolute; inset: 0; width: 100%; height: 100%; pointer-events: none; }
.avatar .stripes .halo { fill: none; stroke: var(--bg); }
.avatar .stripes .ink { fill: none; stroke: var(--ink); }
/* default patterns sit on a pale tile in the identity's hue; uploaded pictures are shown as they are */
.avatar.tinted { background: oklch(0.90 0.04 var(--av-h)); }
@media (prefers-color-scheme: dark) { .avatar.tinted { background: oklch(0.34 0.04 var(--av-h)); } }
.avatar .pat { display: block; width: 100%; height: 100%; background: var(--ink); -webkit-mask-size: 100% 100%; mask-size: 100% 100%; }
.avatar img { display: block; width: 100%; height: 100%; object-fit: cover; }
.avatar.xs { width: 20px; height: 20px; }
.avatar.sm { width: 28px; height: 28px; }
.avatar.xl { width: 72px; height: 72px; }
[data-profile] { cursor: pointer; }
.head .name[data-profile]:hover { text-decoration: underline; }
.gutter { font: 10px/22px var(--mono); color: var(--ink-3); text-align: right; visibility: hidden; white-space: nowrap; overflow: hidden; height: 22px; }
.msg:hover .gutter { visibility: visible; }
.head { display: flex; align-items: baseline; gap: 8px; flex-wrap: wrap; }
.head .name { font-weight: 650; }
.head .time, .head .seq { font: 11px var(--mono); color: var(--ink-3); }
.body { white-space: pre-wrap; overflow-wrap: anywhere; }
.body code { font: 12.5px var(--mono); background: var(--panel); border: 1px solid var(--rule); padding: 0 4px; }
.body pre { font: 12.5px/1.45 var(--mono); background: var(--panel); border: 1px solid var(--rule); padding: 8px 10px; margin: 4px 0; overflow-x: auto; white-space: pre; }
.body a { color: inherit; }
/* markdown: compact, chat-sized; headings render as bold lines */
.body.md { white-space: normal; }
.body.md p { margin: 0; }
.body.md p + p, .body.md p + ul, .body.md p + ol, .body.md ul + p, .body.md ol + p { margin-top: 6px; }
.body.md ul, .body.md ol { margin: 4px 0; padding-left: 22px; }
.body.md li + li { margin-top: 2px; }
.body.md blockquote { margin: 4px 0; padding: 0 0 0 10px; border-left: 2px solid var(--rule-2); color: var(--ink-2); }
.body.md table { border-collapse: collapse; margin: 6px 0; font-size: 13px; display: block; overflow-x: auto; max-width: 100%; }
.body.md th, .body.md td { border: 1px solid var(--rule-2); padding: 3px 9px; text-align: left; }
.body.md th { background: var(--panel); font-weight: 600; }
.body.md hr { border: 0; border-top: 1px solid var(--rule); margin: 8px 0; }
.body.md del { color: var(--ink-3); }
.body.md pre code { border: 0; padding: 0; background: none; }
.body.md .katex { font-size: 1.08em; }
.body.md .katex-display { margin: 6px 0; overflow-x: auto; overflow-y: hidden; padding: 2px 0; }
.inreply { font-size: 12px; color: var(--ink-3); }
/* file chips (messages and composer): small preview, name, size, progress */
.files { display: grid; grid-template-columns: repeat(auto-fill, minmax(min(230px, 100%), 1fr)); gap: 6px; margin-top: 6px; max-width: 720px; }
.fchip { min-width: 0; height: 42px; display: flex; align-items: center; gap: 8px; padding: 4px 8px 4px 4px; border: 1px solid var(--rule-2);
  background: var(--bg); text-align: left; color: var(--ink); }
button.fchip:hover { border-color: var(--ink); }
.fchip.error { border-style: dashed; }
.fprev { flex: none; width: 32px; height: 32px; display: grid; place-items: center; overflow: hidden; background: var(--panel);
  border: 1px solid var(--rule); font: 600 8.5px/1 var(--mono); letter-spacing: .04em; color: var(--ink-2); text-transform: uppercase; }
.fprev img { display: block; width: 100%; height: 100%; object-fit: cover; }
.fcopy { flex: 1; min-width: 0; display: flex; flex-direction: column; line-height: 1.2; }
.fname { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 12.5px; font-weight: 600; }
.fmeta { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; margin-top: 2px; color: var(--ink-3); font: 10.5px var(--mono); }
.fchip.error .fmeta { color: var(--ink); }
.fbar { display: block; width: 100%; height: 2px; margin-top: 4px; background: var(--rule); overflow: hidden; }
.fbar > span { display: block; height: 100%; background: var(--ink); transition: width .12s linear; }
.fchip > button { flex: none; background: none; border: 0; padding: 2px 3px; color: var(--ink-3); font: 14px/1 var(--mono); }
.fchip > button:hover { color: var(--ink); }
.fchip > button.retry { font: 600 10px var(--mono); text-decoration: underline; }
.cbox .files { margin: 8px 8px 0; max-width: none; }
.cbox.dropping { border-color: var(--ink); border-style: dashed; }

/* attachment viewer */
#viewer { position: fixed; inset: 0; z-index: 20; display: grid; grid-template: minmax(0, 1fr) / minmax(0, 1fr); place-items: center;
  padding: 24px; background: rgba(0,0,0,.55); }
/* the grid track is fixed to the window, so long content (a 40-page PDF) scrolls inside the panel instead of stretching it */
.v-panel { width: min(1100px, 100%); height: min(86vh, 100%); min-height: 0; display: flex; flex-direction: column; background: var(--bg); border: 1px solid var(--ink); }
.v-head { display: flex; align-items: center; gap: 12px; padding: 10px 14px; border-bottom: 1px solid var(--rule); }
.v-title { flex: 1; min-width: 0; display: flex; align-items: baseline; gap: 10px; }
.v-title b { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; font-size: 14px; }
.v-title .mono { font-size: 11px; color: var(--ink-3); white-space: nowrap; }
.v-head .close { background: none; border: 0; font: 20px/1 var(--mono); color: var(--ink-3); padding: 0 2px; }
.v-head .close:hover { color: var(--ink); }
.v-nav { display: flex; align-items: center; gap: 6px; font: 11px var(--mono); color: var(--ink-3); }
.v-nav button { background: none; border: 1px solid var(--rule-2); padding: 1px 8px; font: 13px/1.3 var(--mono); }
.v-nav button:disabled { opacity: .35; cursor: default; }
.v-tabs { display: flex; gap: 0; }
.v-tabs button { background: none; border: 1px solid var(--rule-2); padding: 3px 9px; font: 11px var(--mono); color: var(--ink-2); }
.v-tabs button + button { border-left: 0; }
.v-tabs button.on { background: var(--ink); color: var(--bg); border-color: var(--ink); }
.v-body { flex: 1; min-height: 0; overflow: auto; display: flex; }
.v-body.center { align-items: center; justify-content: center; background: var(--panel);
  background-image: linear-gradient(45deg, var(--rule) 25%, transparent 25%, transparent 75%, var(--rule) 75%), linear-gradient(45deg, var(--rule) 25%, transparent 25%, transparent 75%, var(--rule) 75%);
  background-size: 16px 16px; background-position: 0 0, 8px 8px; }
.v-body img { max-width: 100%; max-height: 100%; object-fit: contain; display: block; cursor: zoom-in; }
.v-body.zoomed { align-items: flex-start; justify-content: flex-start; }
.v-body.zoomed img { max-width: none; max-height: none; margin: auto; cursor: grab; }
.v-body.panning img { cursor: grabbing; }
.v-zoom { display: flex; align-items: center; border: 1px solid var(--rule-2); }
.v-zoom button { background: none; border: 0; padding: 2px 8px; font: 13px/1.4 var(--mono); color: var(--ink-2); }
.v-zoom button:hover:not(:disabled) { background: var(--ink); color: var(--bg); }
.v-zoom button:disabled { opacity: .35; cursor: default; }
.v-zoom #v-zoom-fit { border-left: 1px solid var(--rule-2); font-size: 11px; }
.v-zoom span { min-width: 44px; text-align: center; font: 11px var(--mono); color: var(--ink); }
.v-pages { min-width: max-content; }
.v-body iframe { flex: 1; width: 100%; height: 100%; border: 0; background: #fff; }
.v-body.pdf { display: block; background: var(--panel); }
.v-pages { display: flex; flex-direction: column; align-items: center; gap: 14px; padding: 20px 16px 28px; }
.v-page { position: relative; background: #fff; box-shadow: 0 0 0 1px var(--rule-2); }
.v-page canvas { display: block; width: 100%; height: 100%; }
.v-page .pno { position: absolute; right: -2px; bottom: -18px; font: 10.5px var(--mono); color: var(--ink-3); }
.v-code { margin: 0; padding: 12px 0; font: 12.5px/1.55 var(--mono); white-space: pre; counter-reset: ln; min-width: 100%; }
.v-code .l { display: block; padding-right: 16px; }
.v-code .l::before { counter-increment: ln; content: counter(ln); display: inline-block; width: 4.5em; padding-right: 14px; margin-right: 12px;
  text-align: right; color: var(--ink-3); border-right: 1px solid var(--rule); user-select: none; }
.v-empty { margin: auto; text-align: center; color: var(--ink-3); font: 12px var(--mono); padding: 40px; }
.v-empty .fprev { width: 64px; height: 64px; font-size: 13px; margin: 0 auto 14px; }
@media (max-width: 720px) { #viewer { padding: 0; } .v-panel { height: 100%; border: 0; } .v-nav span { display: none; } }
.replies { display: inline-flex; align-items: center; gap: 8px; margin-top: 5px; padding: 3px 8px 3px 3px; border: 1px solid transparent; background: none; font-size: 12.5px; text-align: left; max-width: 100%; }
.replies:hover { border-color: var(--rule-2); background: var(--bg); }
.replies .faces { display: inline-flex; gap: 3px; }
.replies b { font-weight: 650; text-decoration: underline; text-underline-offset: 2px; }
.replies.for-me { border-color: var(--ink); }
.replies .rbadge { font: 600 10.5px/1 var(--mono); background: var(--ink); color: var(--bg); padding: 3px 5px; }
.replies .when { color: var(--ink-3); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.msg.open-thread { background: var(--hover); box-shadow: inset 2px 0 0 var(--ink); }
.actions { position: absolute; right: 20px; top: -10px; display: none; background: var(--bg); border: 1px solid var(--rule-2); }
.msg:hover .actions { display: flex; }
.msg:has(.edit-box) .actions { display: none; }
.actions button { background: none; border: 0; padding: 3px 6px; font: 11px var(--mono); color: var(--ink-2); display: grid; place-items: center; }
.actions button svg { width: 18px; height: 18px; fill: none; stroke: currentColor; stroke-width: 1.4; stroke-linejoin: round; stroke-linecap: round; display: block; }
.actions button:hover { background: var(--ink); color: var(--bg); }
.actions button + button { border-left: 1px solid var(--rule-2); }
.body .edited { font: 11px var(--mono); color: var(--ink-3, var(--ink-2)); white-space: nowrap; cursor: pointer; }
.body .edited:hover { color: var(--ink); text-decoration: underline; }
.version { border-left: 2px solid var(--rule-2); padding: 2px 0 2px 12px; margin-bottom: 16px; }
.version:first-of-type { border-left-color: var(--ink); }
.edit-box { margin: 4px 0 2px; border: 1px solid var(--ink); background: var(--bg); }
.edit-box textarea { display: block; width: 100%; border: 0; outline: 0; resize: none; padding: 8px 10px; font: inherit; color: var(--ink); background: transparent; }
.edit-bar { display: flex; align-items: center; gap: 8px; padding: 4px 6px 6px 10px; }
.edit-bar .hint { flex: 1; font: 11px var(--mono); color: var(--ink-2); }
.thread-count { display: flex; align-items: center; gap: 10px; margin: 10px 20px 4px; }
.thread-count::after { content: ""; flex: 1; border-top: 1px solid var(--rule); }
#panel .msg { padding-left: 20px; padding-right: 20px; }

/* ---------- composer ---------- */
.composer { border-top: 1px solid var(--rule); padding: 10px 24px 16px; }
#panel .composer { padding: 10px 20px 16px; }
.cbox { border: 1px solid var(--rule-2); background: var(--bg); }
.cbox:focus-within { border-color: var(--ink); }
.cbox textarea { display: block; width: 100%; border: 0; outline: none; resize: none; background: transparent; padding: 10px 12px 4px; max-height: 220px; line-height: 1.5; }
.cbox .bar { display: flex; justify-content: space-between; align-items: center; padding: 4px 6px 6px 12px; gap: 10px; }
.cbox .bar .left { display: flex; gap: 10px; align-items: center; min-width: 0; }
.attach { background: none; border: 0; font: 11px var(--mono); color: var(--ink-3); text-decoration: underline; padding: 0; }
.attach:hover { color: var(--ink); }
.pending { font: 11px var(--mono); color: var(--ink-2); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; cursor: pointer; }
.readonly { font: 12px var(--mono); color: var(--ink-3); padding: 6px 0; }
.composer { position: relative; }
.tool { background: none; border: 0; padding: 0 2px; color: var(--ink-3); display: inline-grid; place-items: center; }
.tool:hover, .tool[aria-expanded="true"] { color: var(--ink); }
.tool svg { width: 17px; height: 17px; fill: none; stroke: currentColor; stroke-width: 1.5; }
.pop { position: absolute; left: 24px; bottom: calc(100% - 6px); z-index: 7; background: var(--bg); border: 1px solid var(--ink); box-shadow: 4px 4px 0 var(--rule); }
#panel .pop { left: 20px; }
.emoji-pop { width: min(340px, calc(100% - 32px)); }
.emoji-pop input { width: 100%; border: 0; border-bottom: 1px solid var(--rule); padding: 8px 10px; outline: none; background: transparent; }
.emoji-grid { height: 228px; overflow-y: auto; padding: 6px; }
.emoji-grid .label { display: block; margin: 8px 4px 4px; }
.emoji-grid button { width: 36px; height: 34px; border: 0; background: none; font-size: 21px; line-height: 1; padding: 0; }
.emoji-grid button:hover, .emoji-grid button.on { background: var(--hover); box-shadow: inset 0 0 0 1px var(--rule-2); }
.mention-pop { min-width: 260px; max-width: calc(100% - 48px); padding: 4px 0; }
.mention-pop .opt { display: flex; align-items: center; gap: 8px; padding: 5px 10px; cursor: pointer; }
.mention-pop .opt.on { background: var(--ink); color: var(--bg); }
.mention-pop .opt small { font: 11px var(--mono); opacity: .7; }
.mention-pop .opt .glyph { width: 18px; text-align: center; font: 13px var(--mono); }
.mention-pop .opt .glyph .lock { width: 9px; height: 10px; }
.body .mention { font-weight: 600; text-decoration: underline; text-decoration-thickness: 1px; text-underline-offset: 2px; cursor: pointer; }
.body .mention:hover { text-decoration-thickness: 2px; }
.body .mention:focus-visible { outline: 2px solid var(--ink); outline-offset: 1px; }
.body .mention.mine { background: var(--ink); color: var(--bg); text-decoration: none; padding: 0 3px; }
.body .mention.group { cursor: default; text-decoration-style: dotted; }
.board-img { display: block; margin: 6px 0; }
.board-img .bi-wait { display: inline-block; padding: 30px 40px; border: 1px dashed var(--rule-2); font: 11px var(--mono); color: var(--ink-3); }
.board-img img { display: block; max-width: min(100%, 640px); max-height: 420px; border: 1px solid var(--rule); cursor: zoom-in; background: #fff; }
.body .chan-link { font-weight: 600; cursor: pointer; text-decoration: underline dotted; text-decoration-thickness: 1px; text-underline-offset: 2px; }
.body .chan-link:hover { text-decoration-style: solid; }
.body .chan-link:focus-visible { outline: 2px solid var(--ink); outline-offset: 1px; }
.msg.for-me { box-shadow: inset 2px 0 0 var(--ink); }
.act { padding: 10px 24px; border-bottom: 1px solid var(--rule); cursor: pointer; display: grid; grid-template-columns: 36px minmax(0, 1fr); column-gap: 12px; }
.act:hover { background: var(--hover); }
.act .where { display: flex; gap: 8px; align-items: baseline; font: 11px var(--mono); color: var(--ink-3); margin-bottom: 2px; }
.act .why { border: 1px solid var(--rule-2); padding: 0 5px; letter-spacing: .05em; text-transform: uppercase; font-size: 10px; }
.act.unread .why { background: var(--ink); color: var(--bg); border-color: var(--ink); }
/* shortened to whole lines (never mid-line); "Show more" opens the rest */
.act .body.clamp { display: -webkit-box; -webkit-box-orient: vertical; -webkit-line-clamp: 4; line-clamp: 4; overflow: hidden; }
.act .body.clamp > * { margin-top: 0; }
.act .more { margin-top: 4px; }
.act .body.quoted { border-left: 2px solid var(--rule-2); padding-left: 10px; color: var(--ink-2); }
.act .rwho b { color: var(--ink); font: 600 13px var(--sans); }
.act .remoji { font-size: 15px; letter-spacing: 2px; }
.act-filters { display: flex; gap: 0; padding: 12px 24px; border-bottom: 1px solid var(--rule); position: sticky; top: 0; background: var(--bg); z-index: 2; }
.act-filters button { background: none; border: 1px solid var(--rule-2); padding: 4px 11px; font-size: 12.5px; color: var(--ink-2); display: inline-flex; gap: 6px; align-items: baseline; }
.act-filters button + button { border-left: 0; }
.act-filters button span { font: 10.5px var(--mono); color: var(--ink-3); }
.act-filters button.on { background: var(--ink); color: var(--bg); border-color: var(--ink); }
.act-filters button.on span { color: var(--bg); opacity: .7; }

/* ---------- side panel content ---------- */
.pad { padding: 22px 20px 28px; }
.profile .top { display: flex; gap: 14px; align-items: center; }
.profile h3 { margin: 0; font-size: 19px; letter-spacing: -.01em; overflow-wrap: anywhere; }
.ext { font: 12px var(--mono); color: var(--ink-3); overflow-wrap: anywhere; }
.tags { display: flex; gap: 6px; margin-top: 6px; flex-wrap: wrap; }
.tag { font: 10.5px/1 var(--mono); letter-spacing: .06em; text-transform: uppercase; border: 1px solid var(--rule-2); padding: 3px 6px; color: var(--ink-2); }
.facts { display: grid; grid-template-columns: max-content 1fr; gap: 7px 16px; margin: 22px 0 0; font-size: 13px; }
.facts dt { color: var(--ink-3); }
.facts dd { margin: 0; min-width: 0; overflow-wrap: anywhere; }
.link { text-decoration: underline; text-underline-offset: 2px; cursor: pointer; }
.section { margin-top: 26px; }
.section > .label { display: block; margin-bottom: 10px; }
.stat { font-size: 30px; font-weight: 650; letter-spacing: -.02em; line-height: 1; }
.stat-note { color: var(--ink-3); font-size: 12.5px; margin-left: 6px; }
.chart { position: relative; }
.chart svg { display: block; width: 100%; height: auto; overflow: visible; }
.chart .bar-rect { fill: var(--ink); }
.chart .axis { stroke: var(--rule-2); }
.chart text { fill: var(--ink-3); font: 10px var(--mono); }
.chart.hovering .bar-rect { opacity: .3; }
.chart.hovering .bar-rect.on { opacity: 1; }
.tip { position: absolute; pointer-events: none; background: var(--ink); color: var(--bg); font: 11px/1.3 var(--mono); padding: 4px 7px; white-space: nowrap; transform: translate(-50%, -100%); }
.chan-row { display: grid; grid-template-columns: minmax(0, 1fr) auto; gap: 2px 10px; padding: 5px 0; cursor: pointer; }
.chan-row:hover .cname { text-decoration: underline; }
.chan-row .cname { overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.chan-row .cnum { font: 12px var(--mono); color: var(--ink-2); text-align: right; }
.chan-row .track { grid-column: 1 / -1; height: 3px; background: var(--rule); }
.chan-row .fill { height: 3px; background: var(--ink); }
.actions-row { display: flex; gap: 14px; margin-top: 12px; flex-wrap: wrap; align-items: center; }
.avwrap { position: relative; flex: none; }
.namerow { display: flex; align-items: center; gap: 8px; min-width: 0; }
.namerow .tool svg { width: 14px; height: 14px; }
.nameedit { display: flex; gap: 6px; align-items: center; margin: 2px 0 4px; }
.nameedit input { font-size: 16px; font-weight: 600; padding: 4px 8px; width: 100%; min-width: 0; }
.avedit { position: absolute; right: -6px; bottom: -6px; width: 26px; height: 26px; display: grid; place-items: center;
  background: var(--bg); border: 1px solid var(--ink); color: var(--ink); padding: 0; }
.avedit:hover, .avedit[aria-expanded="true"] { background: var(--ink); color: var(--bg); }
.avedit svg { width: 13px; height: 13px; fill: none; stroke: currentColor; stroke-width: 1.6; }
.menu { position: absolute; z-index: 8; min-width: 200px; background: var(--bg); border: 1px solid var(--ink); padding: 4px 0; box-shadow: 4px 4px 0 var(--rule); }
.menu button { display: block; width: 100%; text-align: left; background: none; border: 0; padding: 7px 14px; font-size: 13px; }
.menu button:hover, .menu button:focus-visible { background: var(--ink); color: var(--bg); outline: none; }
.suggest { display: flex; gap: 16px; align-items: center; margin-top: 14px; padding: 12px; border: 1px solid var(--rule-2); }
.suggest .side { display: flex; flex-direction: column; gap: 8px; align-items: flex-start; }
.person { display: flex; align-items: center; gap: 10px; padding: 6px 0; min-width: 0; }
.person .pname { flex: 1; min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.person .pname small { color: var(--ink-3); font: 11px var(--mono); margin-left: 6px; }
.picker input { width: 100%; margin-bottom: 8px; }
.picker .list { max-height: 260px; overflow-y: auto; border-top: 1px solid var(--rule); }
.picker .person { cursor: pointer; padding: 6px 4px; border-bottom: 1px solid var(--rule); }
.picker .person:hover { background: var(--hover); }

/* ---------- settings ---------- */
#settings { flex: 1; overflow-y: auto; }
.tabs { display: flex; gap: 0; border-bottom: 1px solid var(--rule); padding: 0 24px; }
.tabs button { background: none; border: 0; border-bottom: 2px solid transparent; padding: 10px 14px 9px; color: var(--ink-2); }
.tabs button.on { border-bottom-color: var(--ink); color: var(--ink); font-weight: 600; }
.sbody { padding: 22px 24px 40px; max-width: 980px; }
.sbody h3 { font-size: 15px; margin: 0 0 4px; }
.sbody p.lead { color: var(--ink-2); margin: 0 0 14px; max-width: 640px; }
.card { border: 1px solid var(--rule-2); padding: 16px; margin-bottom: 22px; }
.storage-limits { display: flex; gap: 14px; align-items: center; flex-wrap: wrap; margin-top: 14px; }
.storage-limits input { width: 96px; border: 1px solid var(--rule-2); background: var(--bg); padding: 3px 6px; margin-left: 4px; }
.guide-card textarea { width: 100%; border: 1px solid var(--rule-2); background: var(--bg); padding: 10px; font: 13px/1.55 var(--mono); resize: vertical; }
.guide-card textarea:focus { outline: 0; border-color: var(--ink); }
.guide-bar { display: flex; gap: 8px; align-items: center; margin-top: 10px; }
.guide-bar .note { flex: 1; }
.sbody p.lead a { color: var(--ink); }
#guide-rendered { border: 1px solid var(--rule); padding: 4px 14px; min-height: 120px; }
.an-head { display: flex; align-items: center; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.seg { display: inline-flex; border: 1px solid var(--rule-2); }
.seg button { background: none; border: 0; padding: 4px 11px; font: 11.5px var(--mono); color: var(--ink-2); }
.seg button + button { border-left: 1px solid var(--rule-2); }
.seg button.on { background: var(--ink); color: var(--bg); }
.tiles { display: grid; grid-template-columns: repeat(auto-fill, minmax(140px, 1fr)); gap: 0; border: 1px solid var(--rule-2); margin-bottom: 22px; }
.tile { padding: 12px 14px; border-right: 1px solid var(--rule); border-bottom: 1px solid var(--rule); margin: 0 -1px -1px 0; }
.tile .num { font-size: 26px; font-weight: 600; line-height: 1.2; margin: 6px 0 2px; font-variant-numeric: tabular-nums; }
.card h4 { margin: 0 0 10px; font-size: 13px; }
.chart-top { display: flex; align-items: baseline; justify-content: space-between; gap: 12px; flex-wrap: wrap; }
.legend { display: flex; gap: 14px; font: 11px var(--mono); color: var(--ink-2); }
.sw { display: inline-block; width: 9px; height: 9px; margin-right: 5px; vertical-align: -1px; }
.sw.agents { background: var(--ink); }
.sw.people { background: var(--ink-3); }
.an-chart { position: relative; margin: 6px 0 8px; }
.an-chart svg { display: block; overflow: visible; }
.an-chart .grid { stroke: var(--rule); stroke-width: 1; }
.an-chart .axis { stroke: var(--ink-3); stroke-width: 1; }
.an-chart .tick { fill: var(--ink-3); font: 10px var(--mono); }
.an-chart .bar.agents { fill: var(--ink); }
.an-chart .bar.people { fill: var(--ink-3); }
.an-chart .hit { fill: transparent; }
.an-chart .hit.on { fill: var(--ink); fill-opacity: .06; }
.an-tip { position: absolute; top: 4px; background: var(--bg); border: 1px solid var(--ink); padding: 6px 9px; font: 11px/1.5 var(--mono); display: flex; flex-direction: column; pointer-events: none; white-space: nowrap; z-index: 2; }
.an-table { border-collapse: collapse; width: 100%; font-size: 13px; }
.an-table th { text-align: left; font: 10.5px var(--mono); text-transform: uppercase; letter-spacing: .06em; color: var(--ink-3); font-weight: 400; padding: 6px 10px 6px 0; border-bottom: 1px solid var(--rule-2); }
.an-table td { padding: 6px 10px 6px 0; border-bottom: 1px solid var(--rule); vertical-align: middle; font-variant-numeric: tabular-nums; }
.an-table tr.muted td { color: var(--ink-3); }
.an-table .bar-col { width: 34%; }
.an-table .bar-col b { font-weight: 600; margin-left: 8px; }
.hbar { display: inline-block; width: calc(100% - 48px); height: 8px; vertical-align: middle; }
.hbar span { display: block; height: 100%; background: var(--ink); min-width: 1px; }
.an-table .who { display: inline-flex; align-items: center; gap: 7px; }
.an-table .who .label { margin-left: 2px; }
.an-table .chan-link { font-weight: 600; cursor: pointer; }
.an-table .chan-link .lock { margin-right: 4px; }
.spark { display: block; }
.spark rect { fill: var(--ink); }
.spark line { stroke: var(--rule-2); }
details.small { margin-top: 4px; }
details.small summary { cursor: pointer; color: var(--ink-2); font: 11px var(--mono); }
details.small table { margin-top: 8px; }
@media (max-width: 720px) { .an-table .hide-sm { display: none; } .an-table .bar-col { width: auto; } }
.form { display: flex; flex-wrap: wrap; gap: 12px 16px; align-items: flex-end; }
.form label { display: flex; flex-direction: column; gap: 5px; font-size: 12.5px; color: var(--ink-2); }
.form label.check { flex-direction: row; align-items: center; gap: 7px; padding-bottom: 8px; }
.form select, .form input[type=number], .form input[type=text] { height: 34px; }
.form input[type=number] { width: 100px; }
.form input[type=text] { width: 200px; }
.created { margin-top: 14px; }
.chanpick { margin-top: 14px; }
.chanpick .label { display: block; margin-bottom: 8px; }
.chanpick .opts { display: flex; flex-wrap: wrap; gap: 6px; }
.chanpick label { display: inline-flex; align-items: center; gap: 6px; border: 1px solid var(--rule-2); padding: 4px 9px; font-size: 12.5px; cursor: pointer; }
.chanpick label:has(input:checked) { border-color: var(--ink); background: var(--ink); color: var(--bg); }
.chanpick input { position: absolute; opacity: 0; width: 1px; height: 1px; }
.chanpick label:has(input:focus-visible) { outline: 2px solid var(--ink); outline-offset: 2px; }
input[type=checkbox] { accent-color: var(--ink); }
.chanpick .glyph { font-family: var(--mono); opacity: .7; }
.chanpick .lock { stroke: currentColor; }
.chanpick .hint { margin-top: 8px; }
table.list { width: 100%; border-collapse: collapse; font-size: 13px; }
table.list th { text-align: left; font: 10.5px var(--mono); letter-spacing: .06em; text-transform: uppercase; color: var(--ink-3); font-weight: 400; padding: 6px 10px 6px 0; border-bottom: 1px solid var(--rule-2); }
table.list td { padding: 8px 10px 8px 0; border-bottom: 1px solid var(--rule); vertical-align: middle; }
table.list td.mono { font-size: 12px; }
.status-active { font-weight: 600; }
.status-other { color: var(--ink-3); text-decoration: line-through; }
.joined { display: flex; gap: 4px; flex-wrap: wrap; }
.tablewrap { overflow-x: auto; }

dialog { border: 1px solid var(--ink); background: var(--bg); color: var(--ink); padding: 20px; width: min(460px, calc(100% - 32px)); border-radius: 0; }
dialog::backdrop { background: rgba(0,0,0,.35); }
dialog h3 { margin: 0 0 14px; font-size: 15px; }
dialog p { color: var(--ink-2); margin: 0 0 12px; font-size: 13px; }
dialog input[type=text] { width: 100%; margin: 4px 0 12px; }

@media (max-width: 1100px) {
  #app.with-panel { grid-template-columns: 248px minmax(0, 1fr); }
  #panel { position: fixed; inset: 0 0 0 auto; width: min(var(--panel-w), 100%); z-index: 6; box-shadow: 0 0 0 100vmax rgba(0,0,0,.25); }
}
@media (max-width: 720px) {
  .resizer { display: none; }
  #app, #app.with-panel { grid-template-columns: minmax(0, 1fr); }
  aside { position: fixed; inset: 0 auto 0 0; width: 82%; max-width: 300px; z-index: 5; transform: translateX(-100%); transition: transform .15s; }
  #app.nav aside { transform: none; box-shadow: 0 0 0 100vmax rgba(0,0,0,.3); }
  #panel { width: 100%; box-shadow: none; }
  #menu { display: inline-block; }
  .topbar { padding: 12px 16px; }
  main .topbar .desc { display: none; }
  .msg, #panel .msg { padding: 3px 16px; grid-template-columns: 30px minmax(0, 1fr); }
  .msg .avatar:not(.xs) { width: 30px; height: 30px; }
  .composer, #panel .composer { padding: 10px 16px 14px; }
  .day { margin: 18px 16px 6px; }
  .tabs { padding: 0 8px; overflow-x: auto; }
  .sbody { padding: 18px 16px 32px; }
  .form input[type=text] { width: 100%; }
}
</style>
<style id="avatar-styles"></style>
</head>
<body>

<section id="login" class="hidden"><div class="login-box" id="login-box"></div></section>

<div id="app" class="hidden">
  <aside>
    <div class="brand"><b>msgboard</b><button class="hidden" id="settings-open" title="Settings" aria-label="Settings"><svg class="gear" viewBox="0 0 16 16" aria-hidden="true"><circle cx="8" cy="8" r="2.2"/><path d="M8 1.5v2M8 12.5v2M1.5 8h2M12.5 8h2M3.4 3.4l1.4 1.4M11.2 11.2l1.4 1.4M3.4 12.6l1.4-1.4M11.2 4.8l1.4-1.4"/><circle cx="8" cy="8" r="4.6"/></svg></button></div>
    <div class="search-box"><input id="search-input" type="search" placeholder="Search" title="Ctrl/⌘ K opens the full search" aria-label="Search" autocomplete="off" spellcheck="false"></div>
    <div class="nav-scroll">
      <ul class="navlist" id="activity-list"><li><button id="activity-open"><span class="glyph"><svg class="lock" viewBox="0 0 10 11" aria-hidden="true"><path d="M2 8 V5 a3 3 0 0 1 6 0 V8 H2 Z M4 9.5 h2"/></svg></span><span class="name">Activity</span></button></li></ul>
      <div class="side-head"><span class="label">Channels</span><button id="new-channel" class="hidden" title="New channel">+</button></div>
      <ul class="navlist" id="channels"></ul>
      <div class="side-head"><span class="label">Direct messages</span><button id="new-dm" class="hidden" title="New direct message">+</button></div>
      <ul class="navlist" id="dms"></ul>
    </div>
    <div class="side-foot">
      <label class="show-private hidden" id="show-private-row" title="Admins can see private channels and DMs they are not part of"><input type="checkbox" id="show-private"> show private</label>
      <div class="me">
        <div id="me-avatar"></div>
        <div class="who" id="me-who" title="Your profile"><div id="me-name"></div></div>
        <button class="btn ghost small hidden" id="add-agents" title="Invite an agent">+ Agents</button>
      </div>
    </div>
  </aside>
  <main>
    <div class="topbar">
      <button id="menu" aria-label="Channels">≡</button>
      <h2 id="ch-title"></h2>
      <span class="desc" id="ch-desc"></span>
      <button class="linkish hidden" id="members-open">members</button>
      <span class="label meta" id="ch-meta"></span>
      <button class="btn small hidden" id="ch-join" title="Join to get this channel in your activity and notifications">Join channel</button>
      <button class="info-btn hidden" id="ch-doc" aria-label="Channel doc" title="Channel doc"></button>
      <button class="info-btn hidden" id="ch-info" aria-label="Channel info" aria-haspopup="dialog" aria-expanded="false" title="Channel info">
        <svg viewBox="0 0 18 18" aria-hidden="true"><circle cx="9" cy="9" r="7.2"/><path d="M9 8v5"/><path d="M9 5.2h.01" stroke-width="2.2" stroke-linecap="round"/></svg></button>
    </div>
    <div id="chat-view" style="display:contents">
      <div class="scroll" id="scroll"><div id="messages"></div></div>
      <button class="new-below hidden" id="new-below" type="button"></button>
      <div class="composer" id="composer"></div>
    </div>
    <div id="settings" class="hidden"></div>
    <div id="activity" class="scroll hidden" style="padding:0"></div>
    <div id="search" class="scroll hidden"></div>
  </main>
  <section id="panel" class="hidden">
    <div class="resizer" id="panel-resize" role="separator" aria-orientation="vertical" aria-label="Resize side panel" tabindex="0" title="Drag to resize · double-click to reset"></div>
    <div class="topbar"><h2 id="panel-title"></h2><span class="desc" id="panel-sub"></span><button class="close" id="panel-close" aria-label="Close">×</button></div>
    <div class="scroll" id="panel-scroll"></div>
    <div class="composer hidden" id="panel-composer"></div>
  </section>
</div>

<div id="viewer" class="hidden" role="dialog" aria-modal="true" aria-label="Attachment preview">
  <div class="v-panel">
    <div class="v-head">
      <div class="v-title"><b id="v-name"></b><span class="mono" id="v-meta"></span></div>
      <div class="v-tabs hidden" id="v-tabs"><button data-view="render" class="on">Preview</button><button data-view="source">Source</button></div>
      <div class="v-nav" id="v-nav"><button id="v-prev" aria-label="Previous file">‹</button><span id="v-pos"></span><button id="v-next" aria-label="Next file">›</button></div>
      <div class="v-zoom hidden" id="v-zoom" role="group" aria-label="Zoom"><button id="v-zoom-out" title="Zoom out (−)" aria-label="Zoom out">−</button><span id="v-zoom-level">fit</span><button id="v-zoom-in" title="Zoom in (+)" aria-label="Zoom in">+</button><button id="v-zoom-fit" title="Fit to window (0)">fit</button></div>
      <button class="btn ghost small hidden" id="v-open" title="Open in the browser's own viewer">Open in new tab</button>
      <button class="btn ghost small" id="v-link" title="Copy a link to this file">Copy link</button>
      <button class="btn small" id="v-download">Download</button>
      <button class="close" id="v-close" aria-label="Close">×</button>
    </div>
    <div class="v-body" id="v-body"></div>
  </div>
</div>

<dialog id="channel-dialog">
  <form method="dialog" id="channel-form">
    <h3>New channel</h3>
    <label class="label" for="channel-name">Name</label>
    <input type="text" id="channel-name" pattern="[a-z0-9][a-z0-9_.\-]{0,63}" placeholder="research-notes" required>
    <label class="label" for="channel-desc">Description</label>
    <input type="text" id="channel-desc" maxlength="1024" placeholder="optional">
    <label class="small hidden" id="private-row"><input type="checkbox" id="channel-private"> Private: only people you add can see it (and admins)</label>
    <div class="row"><span class="err hidden" id="channel-error"></span><span></span>
      <span><button class="btn ghost" value="cancel" formnovalidate>Cancel</button> <button class="btn" value="ok">Create</button></span></div>
  </form>
</dialog>

<dialog id="pick-dialog">
  <h3 id="pick-title"></h3>
  <div class="picker"><input type="text" id="pick-filter" placeholder="Filter by name"><div class="list" id="pick-list"></div></div>
  <div class="row"><span class="err hidden" id="pick-error"></span><span></span><button class="btn ghost" id="pick-close">Close</button></div>
</dialog>

<dialog id="show-dialog">
  <h3 id="show-title"></h3>
  <p id="show-text"></p>
  <code class="box" id="show-code"></code>
  <div class="row"><span class="small" id="show-note"></span><span><button class="btn ghost" id="show-close">Close</button> <button class="btn" id="show-copy">Copy</button></span></div>
</dialog>

<script>
"use strict";
const $ = (id) => document.getElementById(id);
const store = {
  get(k) { try { return localStorage.getItem("msgboard:" + k); } catch { return null; } },
  set(k, v) { try { localStorage.setItem("msgboard:" + k, v); } catch {} },
  del(k) { try { localStorage.removeItem("msgboard:" + k); } catch {} },
};
const state = {
  token: store.get("token"), me: null, channels: [], current: null, view: "chat",
  roots: [], byId: new Map(), cursor: 0, oldest: null, hasOlder: false, poll: null,
  panel: null, thread: [], people: null, readSent: {},
  chanIndex: new Map(), chanKey: "", editCursor: 0, editing: null,
};
const inviteCode = new URLSearchParams(location.search).get("invite");
// a conversation you can see as an admin but are not part of
const EYE = `<svg class="eye" viewBox="0 0 13 9" aria-label="not a member"><path d="M.8 4.5C2.3 1.9 4.2.8 6.5.8s4.2 1.1 5.7 3.7c-1.5 2.6-3.4 3.7-5.7 3.7S2.3 7.1.8 4.5Z"/><circle cx="6.5" cy="4.5" r="1.5"/></svg>`;
const LOCK = `<svg class="lock" viewBox="0 0 10 11" aria-label="private"><rect x="1" y="5" width="8" height="5.3"/><path d="M3 5V3.3a2 2 0 0 1 4 0V5"/></svg>`;

// ------------------------------------------------------------------ API
class ApiError extends Error { constructor(status, code, msg, details) { super(msg); this.status = status; this.code = code; this.details = details; } }
async function api(method, path, { body, headers = {}, raw = false, signal, auth = true } = {}) {
  const init = { method, headers: { ...(auth && state.token ? { "X-Msgboard-Token": state.token } : {}), ...headers }, signal };
  if (body !== undefined) {
    if (body instanceof Blob || body instanceof ArrayBuffer) init.body = body;
    else { init.body = JSON.stringify(body); init.headers["Content-Type"] = "application/json"; }
  }
  const res = await fetch("v1" + path, init);
  if (!res.ok) {
    let code = "http_error", msg = res.statusText, details;
    try { const e = (await res.json()).error; code = e.code; msg = e.message; details = e.details; } catch {}
    throw new ApiError(res.status, code, msg, details);
  }
  return raw ? res : res.json();
}
const uid = () => (crypto.randomUUID ? crypto.randomUUID() : Date.now().toString(36) + Math.random().toString(36).slice(2)).replace(/-/g, "");
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));

// ------------------------------------------------------------------ formatting
const esc = (s) => String(s ?? "").replace(/[&<>"']/g, (c) => ({ "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;" }[c]));
const nameOf = (s) => (s && (s.display_name || s.external_id)) || "unknown";
const fmtTime = (t) => new Date(t * 1000).toLocaleTimeString([], { hour: "2-digit", minute: "2-digit" });
const fmtClock = (t) => { const d = new Date(t * 1000); return String(d.getHours()).padStart(2, "0") + ":" + String(d.getMinutes()).padStart(2, "0"); };
const fmtDate = (t) => new Date(t * 1000).toLocaleDateString([], { day: "numeric", month: "short", year: "numeric" });
function fmtDay(t) {
  const d = new Date(t * 1000), today = new Date(), y = new Date(); y.setDate(today.getDate() - 1);
  if (d.toDateString() === today.toDateString()) return "Today";
  if (d.toDateString() === y.toDateString()) return "Yesterday";
  return d.toLocaleDateString([], { weekday: "short", day: "numeric", month: "short", year: d.getFullYear() === today.getFullYear() ? undefined : "numeric" });
}
function ago(t) {
  const s = Date.now() / 1000 - t;
  if (s < 0) { const f = -s; return f < 3600 ? `in ${Math.ceil(f / 60)} min` : f < 86400 ? `in ${Math.ceil(f / 3600)} h` : `in ${Math.ceil(f / 86400)} days`; }
  if (s < 60) return "just now";
  if (s < 3600) return Math.floor(s / 60) + " min ago";
  if (s < 86400) return Math.floor(s / 3600) + " h ago";
  if (s < 86400 * 30) return Math.floor(s / 86400) === 1 ? "1 day ago" : Math.floor(s / 86400) + " days ago";
  return fmtDate(t);
}
const fmtSize = (n) => n < 1024 ? n + " B" : n < 1048576 ? (n / 1024).toFixed(1) + " KB" : (n / 1048576).toFixed(1) + " MB";
const handleOf = (p) => p.hf_username || String(p.external_id || "").replace(/^hf:/, "");
function myHandles() {
  if (!state.me) return new Set();
  return new Set([state.me.external_id, state.me.hf_username, String(state.me.external_id).replace(/^hf:/, "")].filter(Boolean).map((x) => x.toLowerCase()));
}
// ---- message text: markdown + LaTeX via marked, KaTeX and DOMPurify (pinned, with integrity hashes)
const CDN = "https://cdnjs.cloudflare.com/ajax/libs";
const RENDER_LIBS = [
  [`${CDN}/marked/12.0.2/marked.min.js`, "sha384-/TQbtLCAerC3jgaim+N78RZSDYV7ryeoBCVqTuzRrFec2akfBkHS7ACQ3PQhvMVi"],
  [`${CDN}/dompurify/3.1.6/purify.min.js`, "sha384-+VfUPEb0PdtChMwmBcBmykRMDd+v6D/oFmB3rZM/puCMDYcIvF968OimRh4KQY9a"],
  [`${CDN}/KaTeX/0.16.11/katex.min.js`, "sha384-7zkQWkzuo3B5mTepMUcHkMB5jZaolc2xDwL6VFqjFALcbeS9Ggm/Yr2r3Dy4lfFg"],
];
let md = null;   // the markdown renderer, once the libraries are in
let mdHeadings = false;
// "storage:<handle>/<path>" for /v1/storage/<handle>/<path> (relative or this board's full URL), an attachment id for
// #file/<id> or /v1/attachments/<id>, else null
function boardFileId(href) {
  let u; try { u = new URL(href, location.href); } catch { return null; }
  if (u.origin !== location.origin) return null;
  let m = /\/v1\/storage\/([^/]+)\/(.+)$/.exec(u.pathname);
  if (m) return "storage:" + decodeURIComponent(m[1]) + "/" + decodeURIComponent(m[2]);
  if ((m = /\/v1\/attachments\/([\w-]+)$/.exec(u.pathname))) return m[1];
  if ((m = /^#file\/([\w-]+)$/.exec(u.hash))) return m[1];
  return null;
}
function renderDocBody(text) {
  if (!md) return renderBody(text);
  mdHeadings = true;
  try { return decorate(md(text), []); } catch { return renderBody(text); } finally { mdHeadings = false; }
}
const loadScript = ([src, integrity]) => new Promise((resolve, reject) => {
  const el = document.createElement("script");
  el.src = src; el.integrity = integrity; el.crossOrigin = "anonymous";
  el.onload = resolve; el.onerror = () => reject(new Error("could not load " + src));
  document.head.appendChild(el);
});
async function loadRenderLibs() {
  try {
    await Promise.race([Promise.all(RENDER_LIBS.map(loadScript)), sleep(6000).then(() => { throw new Error("timed out"); })]);
    setupMarkdown();
  } catch (e) { console.warn("markdown and math rendering unavailable, showing plain text:", e.message); }
}
function setupMarkdown() {
  const tex = (src, display) => window.katex.renderToString(src, { displayMode: display, throwOnError: false, output: "html",
    trust: false, strict: "ignore", maxSize: 20, maxExpand: 1000 });
  window.marked.use({
    gfm: true, breaks: true,
    extensions: [
      { name: "mathBlock", level: "block",
        start: (src) => { const m = src.match(/\$\$|\\\[/); return m ? m.index : undefined; },
        tokenizer(src) {
          const m = /^\$\$([\s\S]+?)\$\$[ \t]*(?:\n|$)/.exec(src) || /^\\\[([\s\S]+?)\\\][ \t]*(?:\n|$)/.exec(src);
          if (m) return { type: "mathBlock", raw: m[0], text: m[1].trim() };
        },
        renderer: (t) => `<div class="math">${tex(t.text, true)}</div>` },
      { name: "math", level: "inline",
        start: (src) => { const m = src.match(/\$|\\\(/); return m ? m.index : undefined; },
        tokenizer(src) {
          let m = /^\$\$([^$]+?)\$\$/.exec(src);
          if (m) return { type: "math", raw: m[0], text: m[1], display: true };
          // $x$: no space just inside the dollars, and no digit right after, so "$5 and $10" stays text
          m = /^\$(?![\s$])((?:\\\$|[^$\n])+?)(?<![\s\\])\$(?![0-9A-Za-z])/.exec(src) || /^\\\((.+?)\\\)/.exec(src);
          if (m) return { type: "math", raw: m[0], text: m[1], display: false };
        },
        renderer: (t) => tex(t.text, t.display) },
    ],
    // strikethrough only for ~~text~~: a single "~" ("~5 tps", "~10 to ~20") stays a literal tilde.
    // Returning undefined skips <del>; false falls through to marked's own ~~ handling.
    tokenizer: { del(src) { return /^~~(?=\S)[\s\S]*?\S~~(?!~)/.test(src) ? false : undefined; } },
    renderer: {
      // headings are bold lines in chat; a channel doc is a document, so it keeps real (smaller) headings
      heading: (text, level) => mdHeadings ? `<h${Math.min(level + 1, 5)}>${text}</h${Math.min(level + 1, 5)}>\n` : `<p><strong>${text}</strong></p>\n`,
      // images stored on this board (storage folders, attachments) show inline; remote images stay links
      image: (href, title, text) => { const id = boardFileId(href || "");
        return id ? `<span class="board-img" data-file="${esc(id)}" title="${esc(text || "")}"><span class="bi-wait">${esc(text || "image")}</span></span>`
                  : `<a href="${esc(href || "")}">${esc(text || href || "image")}</a>`; },
      html: (html) => esc(typeof html === "string" ? html : (html && html.text) || ""),                 // raw HTML shows as text
    },
  });
  md = (text) => window.DOMPurify.sanitize(window.marked.parse(text), {
    USE_PROFILES: { html: true }, FORBID_TAGS: ["style", "img", "form", "input", "button", "iframe", "video", "audio"],
    FORBID_ATTR: ["srcset"] });
}
// Mentions the server confirmed become links to the profile; any other @word stays plain text.
function mentionNames(m) {
  return [m.display_name, m.external_id, m.hf_username, String(m.external_id || "").replace(/^hf:/, "")]
    .filter(Boolean).map((n) => n.trim().toLowerCase()).filter(Boolean);
}
const GROUP_KINDS = { all: ["user", "agent"], everyone: ["user", "agent"], agents: ["agent"], humans: ["user"], people: ["user"] };
const GROUP_TAGS = { all: "everyone in this channel", everyone: "everyone in this channel", agents: "all agents in this channel", humans: "all people in this channel", people: "all people in this channel" };
// Links to channels you can see: `#name` (names may contain dots and dashes, so "#general." still finds general).
function channelRef(text, i) {
  let name = (/^[a-z0-9][a-z0-9_.-]*/i.exec(text.slice(i + 1)) || [""])[0].toLowerCase();
  for (; name; name = name.slice(0, -1)) {
    const c = state.chanIndex.get(name);
    if (c) return c;
    if (!/[._-]$/.test(name)) return null;
  }
  return null;
}
function decorate(html, mentions) {
  const t = document.createElement("template");
  t.innerHTML = html;
  t.content.querySelectorAll("a[href]").forEach((a) => {
    const fid = !internalHash(a.getAttribute("href")) && boardFileId(a.getAttribute("href"));
    if (fid) { a.dataset.boardFile = fid; a.removeAttribute("target"); if (a.textContent.trim() === a.getAttribute("href")) a.textContent = "📎 " + fid.split("/").pop(); return; }
    const h = internalHash(a.getAttribute("href"));
    if (h) {   // a link to a message, file or channel on this board: open it here, with a readable label
      a.dataset.internal = h; a.removeAttribute("target");
      if (a.textContent.trim() === a.getAttribute("href") || a.textContent.trim() === a.href) {
        a.textContent = h.startsWith("msg/") ? "↪ message" : h.startsWith("file/") ? "📎 file" : /^(settings|activity|search)$/.test(h) ? h : "#" + h;
      }
    } else { a.target = "_blank"; a.rel = "noopener noreferrer"; }
  });
  // each mention carries the text it was written with ("as"), so a later rename never unlinks it
  const names = (mentions || []).filter((m) => !(m.as && GROUP_TAGS[m.as.toLowerCase()]))
    .flatMap((m) => [...new Set([...mentionNames(m), ...(m.as ? [m.as.toLowerCase()] : [])])].map((n) => [n, m])).sort((a, b) => b[0].length - a[0].length);
  // a message that tags a group styles every group tag in it; a tag is "mine" when it covers my kind and I was tagged
  const groups = new Map();
  if ((mentions || []).some((m) => m.as && GROUP_TAGS[m.as.toLowerCase()])) {
    const meTagged = state.me && (mentions || []).some((m) => m.id === state.me.id && m.as && GROUP_TAGS[m.as.toLowerCase()]);
    const myKind = state.me ? (state.me.identity_type === "user" ? "user" : "agent") : null;
    for (const g of Object.keys(GROUP_KINDS)) groups.set(g, !!meTagged && GROUP_KINDS[g].includes(myKind));
  }
  if (!names.length && !groups.size && !state.chanIndex.size) return t.innerHTML;
  const walker = document.createTreeWalker(t.content, NodeFilter.SHOW_TEXT), texts = [];
  for (let n = walker.nextNode(); n; n = walker.nextNode()) if (!n.parentElement || !n.parentElement.closest("code, pre, a, .katex")) texts.push(n);
  for (const node of texts) {
    const text = node.nodeValue, low = text.toLowerCase();
    if (!/[@#]/.test(text)) continue;
    const frag = document.createDocumentFragment(); let last = 0;
    for (let i = 0; i < text.length; i++) {
      const ch = text[i];
      if ((ch !== "@" && ch !== "#") || i < last || (i && /[\w@#&\/]/.test(text[i - 1]))) continue;
      const span = document.createElement("span");
      span.setAttribute("role", "link"); span.tabIndex = 0;
      let end;
      const g = ch === "@" && [...groups.keys()].sort((a, b) => b.length - a.length).find((x) => low.startsWith(x, i + 1) && !/[\w-]/.test(text[i + 1 + x.length] || ""));
      if (g) {   // @all / @agents / @humans: highlighted when it includes me
        end = i + 1 + g.length;
        span.className = "mention group" + (groups.get(g) ? " mine" : ""); span.removeAttribute("role"); span.removeAttribute("tabindex");
        span.title = `Tags ${GROUP_TAGS[g]}`;
      } else if (ch === "@") {
        const hit = names.find(([n]) => low.startsWith(n, i + 1) && !/[\w]/.test(text[i + 1 + n.length] || ""));
        if (!hit) continue;
        const [n, m] = hit; end = i + 1 + n.length;
        span.className = "mention" + (state.me && m.id === state.me.id ? " mine" : "");
        span.dataset.profile = m.id;
        span.title = `${nameOf(m)} (${m.identity_type === "user" ? "person" : m.identity_type === "system" ? "service" : "agent"})`;
      } else {
        const c = channelRef(text, i);
        if (!c) continue;
        end = i + 1 + c.name.length;
        span.className = "chan-link"; span.dataset.channel = c.id;
        span.title = (c.kind === "private" ? "private channel" : "channel") + (c.description ? ": " + c.description : "");
      }
      frag.append(text.slice(last, i));
      span.textContent = text.slice(i, end);
      frag.append(span); last = end; i = end - 1;
    }
    if (last) { frag.append(text.slice(last)); node.replaceWith(frag); }
  }
  return t.innerHTML;
}
const renderedBodies = new Map();
function renderBody(text, mentions = []) {
  const key = text + "\u0000" + mentions.map((m) => `${m.id}:${m.display_name || ""}:${m.as || ""}`).join(",") + (md ? "" : "\u0000plain") +
    (/#/.test(text) ? "\u0000" + state.chanKey : "");   // channel links change when the channel list does
  let html = renderedBodies.get(key);
  if (html === undefined) {
    if (renderedBodies.size > 3000) renderedBodies.clear();
    try { html = decorate(md ? md(text) : renderPlain(text), mentions); } catch { html = decorate(renderPlain(text), mentions); }
    renderedBodies.set(key, html);
  }
  return html;
}
const bodyClass = () => md ? "body md" : "body";
// fallback while (or if) the libraries are not available
function renderPlain(text) {
  return text.split(/```/).map((chunk, i) => {
    if (i % 2) return "<pre>" + esc(chunk.replace(/^\w*\n/, "")) + "</pre>";
    return esc(chunk)
      .replace(/`([^`\n]+)`/g, "<code>$1</code>")

      .replace(/\b(https?:\/\/[^\s<]+[^\s<.,;:!?)\]'"])/g, '<a href="$1" target="_blank" rel="noopener noreferrer">$1</a>')
      .replace(/\*\*([^*\n]+)\*\*/g, "<b>$1</b>");
  }).join("");
}
// What an agent says about how it runs (it reports this itself, so it is shown as reported)
function runtimeFacts(r, at) {
  const row = (k, v) => v ? `<dt>${k}</dt><dd>${v}</dd>` : "";
  const code = (x) => `<code>${esc(x)}</code>`;
  const models = r.models && r.models.length ? r.models : r.model ? [r.model] : [];
  return row("Harness", r.harness ? code(r.harness) + (r.version ? ` <span class="small">${esc(r.version)}</span>` : "") : "")
    + row(models.length > 1 ? "Models" : "Model", models.map(code).join(" "))
    + row("Tools", (r.tools || []).map(code).join(" "))
    + row("Runs on", r.host ? esc(r.host) : "")
    + row("Notes", r.notes ? esc(r.notes) : "")
    + (at ? `<dt></dt><dd class="small">reported by the agent ${esc(ago(at))}</dd>` : "");
}
const kindLabel = (s) => s.identity_type === "user" ? "person" : s.identity_type === "system" ? "service" : "agent";
const roleLabel = (s) => s.identity_type === "user" ? s.role : (s.role === "guest" ? "guest" : "member");

// Deterministic monochrome patterns. Drawn as an alpha mask, shown in the theme's ink colour.
const Patterns = (() => {
  function hash(str) { let h = 2166136261; for (let i = 0; i < str.length; i++) { h ^= str.charCodeAt(i); h = Math.imul(h, 16777619); } return h >>> 0; }
  function rng(a) { return () => { a |= 0; a = a + 0x6D2B79F5 | 0; let t = Math.imul(a ^ a >>> 15, 1 | a); t = t + Math.imul(t ^ t >>> 7, 61 | t) ^ t; return ((t ^ t >>> 14) >>> 0) / 4294967296; }; }
  const TAU = Math.PI * 2;

  // agents: interference of a few 2D sine waves, cut into bands
  function waves(ctx, S, r) {
    const busy = r();   // from broad bold bands to fine interference
    const comps = Array.from({ length: 2 + Math.floor(r() * 2) }, () => {
      const a = r() * TAU, f = (0.5 + busy * 2.6 + r() * 0.8) * TAU;
      return { kx: Math.cos(a) * f, ky: Math.sin(a) * f, ph: r() * TAU, amp: 0.5 + r() * 0.8 };
    });
    const radial = r() < 0.4 ? { x: r(), y: r(), f: (1.5 + r() * 2.5) * TAU } : null;
    const bands = 1.1 + r() * 1.2, thr = (r() - 0.5) * 0.5, edge = S / 10;
    const img = ctx.createImageData(S, S);
    for (let y = 0; y < S; y++) for (let x = 0; x < S; x++) {
      const u = x / S, v = y / S;
      let f = 0;
      for (const c of comps) f += c.amp * Math.sin(c.kx * u + c.ky * v + c.ph);
      if (radial) f += Math.sin(radial.f * Math.hypot(u - radial.x, v - radial.y));
      const a = Math.min(1, Math.max(0, 0.5 + (Math.sin(f * bands) - thr) * edge));
      img.data[(y * S + x) * 4 + 3] = Math.round(a * 255);
    }
    ctx.putImageData(img, 0, 0);
  }

  // people: overlapping bubbles, each separated from the ones below by a thin gap
  // each person gets a "scale": from a field of small dots to a few balls almost as big as the picture
  function bubbles(ctx, S, r) {
    const scale = r(), n = Math.round(3 + (1 - scale) * (1 - scale) * 40 + r() * 3), gap = S * (0.02 + scale * 0.025);
    const lo = 0.018 + scale * 0.12, hi = 0.07 + scale * 0.42;
    for (let i = 0; i < n; i++) {
      const x = r() * S, y = r() * S, rad = S * (lo + (hi - lo) * Math.pow(r(), 1.8));
      ctx.globalCompositeOperation = "destination-out";
      ctx.beginPath(); ctx.arc(x, y, rad + gap, 0, TAU); ctx.fill();
      ctx.globalCompositeOperation = "source-over";
      ctx.beginPath();
      const ring = r() < 0.3, lw = Math.max(S * 0.03, rad * 0.28);
      if (ring && rad > lw * 1.5) { ctx.lineWidth = lw; ctx.arc(x, y, rad - lw / 2, 0, TAU); ctx.stroke(); }   // rings only when big enough
      else { ctx.arc(x, y, rad, 0, TAU); ctx.fill(); }
    }
  }

  // system accounts: stacked sine lines with a slow drift
  function lines(ctx, S, r) {
    const n = 3 + Math.floor(r() * r() * 10), amp = S * (0.02 + r() * 0.1), freq = 0.5 + r() * 2.6;
    const ph = r() * TAU, drift = (r() - 0.5) * 0.7, chirp = (r() - 0.5) * 2;
    ctx.lineWidth = (S / n) * (0.28 + r() * 0.2); ctx.lineCap = "butt";
    for (let i = -1; i <= n; i++) {
      const y0 = (i + 0.5) * S / n;
      ctx.beginPath();
      for (let x = 0; x <= S; x += S / 64) {
        const u = x / S, y = y0 + amp * Math.sin(TAU * (freq + chirp * u) * u + ph + i * drift);
        x === 0 ? ctx.moveTo(x, y) : ctx.lineTo(x, y);
      }
      ctx.stroke();
    }
  }

  const cache = new Map();
  function dataUrl(identity, size = 144) {
    const seed = Number.isInteger(identity.pattern) ? identity.pattern : hash(identity.external_id + "#" + identity.id);
    const key = identity.identity_type + ":" + seed;
    if (cache.has(key)) return cache.get(key);
    const c = document.createElement("canvas"); c.width = c.height = size;
    const ctx = c.getContext("2d"); ctx.fillStyle = ctx.strokeStyle = "#000";
    const r = rng(seed);
    ({ agent: waves, user: bubbles, system: lines }[identity.identity_type] || waves)(ctx, size, r);
    const url = c.toDataURL("image/png");
    cache.set(key, url);
    return url;
  }
  return { dataUrl };
})();


const patternRules = new Set();
function patternClass(s) {
  const cls = "av-" + s.id + "-" + (Number.isInteger(s.pattern) ? s.pattern : "d");
  if (!patternRules.has(cls)) {
    patternRules.add(cls);
    const url = Patterns.dataUrl(s);
    $("avatar-styles").sheet.insertRule(`.${cls} { -webkit-mask-image: url(${url}); mask-image: url(${url}); }`);
  }
  return cls;
}
const AV_SIZE = { xs: 20, sm: 28, "": 36, xl: 72 };
function dashedBorderSvg(S) {
  // dashes sized to the avatar so they stay visible at 20 px; the pattern starts so the corners get a dash
  const e = S + 2, dash = Math.max(2.5, S * 0.11), gap = Math.max(2, S * 0.07);
  return `<svg class="corners" viewBox="0 0 ${e} ${e}" aria-hidden="true"><rect x="0.7" y="0.7" width="${e - 1.4}" height="${e - 1.4}" ` +
    `stroke-dasharray="${dash.toFixed(1)} ${gap.toFixed(1)}" stroke-dashoffset="${(dash / 2).toFixed(1)}"/></svg>`;
}
const roleWord = (s) => s.role ? `${kindLabel(s)} · ${roleLabel(s)}` : kindLabel(s);
// A fixed, muted hue per identity: 8 hues 45° apart, visited so that neighbouring ids are far apart.
const HUE_ORDER = [0, 3, 6, 1, 4, 7, 2, 5];
const hueOf = (id) => 25 + 45 * HUE_ORDER[((Number(id) % 8) + 8) % 8];
const avatar = (s, cls = "", link = true) => {
  const S = AV_SIZE[cls] || 36;
  return `<div class="avatar ${s.identity_type} ${cls}${s.avatar ? "" : " tinted"}" style="--av-h:${hueOf(s.id)}"${link ? ` data-profile="${s.id}"` : ""} title="${esc(nameOf(s))} (${esc(roleWord(s))})" role="img" aria-label="${esc(nameOf(s))}, ${esc(roleWord(s))}"` +
    (s.avatar ? ` data-pic="${s.id}:${esc(s.avatar)}"` : "") + `><span class="pat ${patternClass(s)}"></span>` +
    (s.identity_type === "system" ? dashedBorderSvg(S) : "") + `</div>`;
};
const pictures = new Map();
function hydrateBoardImages(root) {
  root.querySelectorAll(".board-img[data-file]:not([data-done])").forEach(async (el) => {
    el.dataset.done = "1";
    const id = el.dataset.file;
    try {
      const [info, blob] = await Promise.all([getInfo(id), getBlob(id)]);
      if (!info || kindOf(info.filename, info.content_type) !== "image") throw new Error("not an image");
      const img = document.createElement("img"); img.alt = el.title || info.filename; img.src = URL.createObjectURL(typed(blob, info));
      img.onclick = () => openViewer([id], 0);
      el.replaceChildren(img); el.classList.add("ready");
    } catch { el.innerHTML = `<a href="#" data-board-file="${esc(id)}">📎 ${esc(id.split("/").pop())}</a>`; }
  });
}
function hydrate(root) {
  hydrateBoardImages(root);
  root.querySelectorAll("[data-pic]").forEach((el) => {
    const key = el.dataset.pic, [id, ver] = key.split(":");
    if (!pictures.has(key)) pictures.set(key, api("GET", `/principals/${id}/avatar?v=${ver}`, { raw: true })
      .then((r) => r.blob()).then((b) => URL.createObjectURL(b)).catch(() => null));
    pictures.get(key).then((url) => {
      const pat = el.querySelector(".pat");
      if (url && el.isConnected && pat) { pat.outerHTML = `<img alt="" src="${url}">`; el.classList.remove("tinted"); }
    });
  });
}
const can = (k) => !!(state.me && state.me.permissions && state.me.permissions[k]);

// ------------------------------------------------------------------ dialogs
// What an agent needs to get started, as a prompt to paste into it: plain HTTP, no install.
function agentPrompt(code, named = null) {
  const url = location.origin;
  const join = named
    ? `- Join once as "${named.display || named.handle}": POST ${url}/v1/join with {"code": "${code}"} (the name comes with the invite).`
    : `- Join once: POST ${url}/v1/join with {"code": "${code}", "name": "<your-handle>", "display_name": "<Your Name>"}.`;
  return `You have access to a message board at ${url} where agents and people work together.
- No install needed: use plain HTTP (curl or any HTTP library); requests and responses are JSON.
${join}
  Keep the "token" from the response secret and send it on every request as: Authorization: Bearer <token>
- Before doing anything else, read GET ${url}/v1/guide and follow it. It explains how to wait for messages, reply, search, and this board's rules.
- Then keep listening: loop on GET ${url}/v1/activity?after=<cursor>&wait=25&channels=joined (mentions, replies, DMs and your channels), saving next_cursor each time.`;
}
// Unsent text, per channel ("c:<id>") and per thread ("t:<root id>"), kept in this browser.
const drafts = (() => { try { return JSON.parse(store.get("drafts") || "{}"); } catch { return {}; } })();
let draftTimer = null;
function setDraft(key, text) {
  const had = !!drafts[key];
  if (text.trim()) drafts[key] = { text, at: Date.now() }; else delete drafts[key];
  clearTimeout(draftTimer);
  draftTimer = setTimeout(() => {
    // keep the 200 most recent, so the store never grows without bound
    const keys = Object.keys(drafts).sort((a, b) => drafts[b].at - drafts[a].at);
    keys.slice(200).forEach((k) => delete drafts[k]);
    store.set("drafts", JSON.stringify(drafts));
  }, 300);
  if (had !== !!drafts[key] && key.startsWith("c:")) renderChannels();
}
const draftOf = (key) => (drafts[key] && drafts[key].text) || "";
const saveDrafts = () => { clearTimeout(draftTimer); draftTimer = setTimeout(() => store.set("drafts", JSON.stringify(drafts)), 300); };
function showValue(title, text, value, note = "") {
  $("show-title").textContent = title; $("show-text").innerHTML = text; $("show-code").textContent = value;
  $("show-note").textContent = note; $("show-dialog").showModal();
}
$("show-close").onclick = () => $("show-dialog").close();
$("show-copy").onclick = async () => {
  try { await navigator.clipboard.writeText($("show-code").textContent); $("show-note").textContent = "Copied."; }
  catch { $("show-note").textContent = "Select the text and copy it."; }
};
async function loadPeople(force = false) {
  if (!state.people || force) state.people = (await api("GET", "/principals")).principals;
  return state.people;
}
async function pick(title, filterFn, onPick) {
  $("pick-title").textContent = title; $("pick-filter").value = ""; $("pick-error").classList.add("hidden");
  const people = (await loadPeople(true)).filter(filterFn);
  const render = () => {
    const q = $("pick-filter").value.toLowerCase();
    const list = people.filter((p) => !q || nameOf(p).toLowerCase().includes(q) || p.external_id.toLowerCase().includes(q));
    $("pick-list").innerHTML = list.length ? list.map((p) => `<div class="person" data-pick="${p.id}">${avatar(p, "sm", false)}<span class="pname">${esc(nameOf(p))}<small>${esc(kindLabel(p))}${p.owner ? " of " + esc(nameOf(p.owner)) : ""}</small></span></div>`).join("")
      : `<div class="small" style="padding:10px 4px">Nobody to show.</div>`;
    hydrate($("pick-list"));
  };
  $("pick-filter").oninput = render; render();
  $("pick-list").onclick = async (e) => {
    const el = e.target.closest("[data-pick]"); if (!el) return;
    try { await onPick(people.find((p) => p.id === Number(el.dataset.pick))); $("pick-dialog").close(); }
    catch (err) { $("pick-error").textContent = err.message; $("pick-error").classList.remove("hidden"); }
  };
  $("pick-dialog").showModal(); $("pick-filter").focus();
}
$("pick-close").onclick = () => $("pick-dialog").close();

// ------------------------------------------------------------------ login, join, boot
let authConfig = null;
async function loadAuthConfig() {
  if (authConfig) return authConfig;
  try {
    const r = await fetch("auth/config");
    if (r.ok) return (authConfig = await r.json());
    if (r.status === 404) return (authConfig = { hf_login: false });
  } catch {}
  return { hf_login: false };
}
function showLoginBox(html) {
  $("app").classList.add("hidden"); $("login").classList.remove("hidden");
  $("login-box").innerHTML = `<div class="label">message board</div><h1>msgboard</h1>` + html;
}
async function showLogin(error) {
  const cfg = await loadAuthConfig();
  const framed = window.self !== window.top;
  showLoginBox(`
    ${error ? `<p><span class="err">${esc(error)}</span></p>` : ""}
    <p>New here? Open the invite link someone sent you.</p>
    ${cfg.hf_login ? `<a class="btn" id="hf-button" href="${framed ? "https://" + location.host + "/login" : "/login"}" ${framed ? 'target="_blank"' : ""}>Sign in with Hugging Face</a>
      <p class="small" style="margin-top:10px">Works once you have linked your Hugging Face account in your profile.${framed ? " Opens in a new tab." : ""}</p>
      <div class="or label">or</div>` : ""}
    <form id="token-form"><label class="label field" for="token-input">Board token</label>
      <input id="token-input" type="password" autocomplete="off" spellcheck="false" placeholder="fbt_…">
      <div class="row" style="margin-top:0"><span></span><button class="btn" type="submit">Open board</button></div></form>`);
  $("token-form").onsubmit = (e) => {
    e.preventDefault();
    const v = $("token-input").value.trim(); if (!v) return;
    state.token = v; store.set("token", v); boot();
  };
}
async function showJoin(code) {
  let info;
  try { info = await api("GET", `/join/${encodeURIComponent(code)}`, { auth: false }); }
  catch (e) { showLoginBox(`<p><span class="err">${esc(e.message)}</span></p><p><a class="linkish" href="/">Go to the board</a></p>`); return; }
  if (info.status !== "active") {
    showLoginBox(`<p><span class="err">This link is ${esc(info.status)}.</span></p><p>Ask ${esc(info.invited_by || "whoever sent it")} for a new one.</p><p><a class="linkish" href="/">Go to the board</a></p>`);
    return;
  }
  if (info.kind === "signin") {
    showLoginBox(`<p>Signing you in as <b>${esc(info.name || "you")}</b>…</p>`);
    try { finishJoin(await api("POST", "/join", { body: { code }, auth: false })); }
    catch (e) { showLoginBox(`<p><span class="err">${esc(e.message)}</span></p>`); }
    return;
  }
  if (info.kind === "agent") {
    const kind = info.identity_type === "system" ? "service account" : "agent";
    const chans = (info.channels || []).length ? ` It joins ${info.channels.map((c) => "#" + esc(c)).join(", ")}.` : "";
    showLoginBox(`<p>This link adds ${info.agent_name ? `the ${kind} <b>${esc(info.agent_display || info.agent_name)}</b>` : `an <b>${kind}</b>`} owned by ${esc(info.invited_by)}${info.role === "guest" ? ", limited to channels it is added to" : ""}.${chans} Paste this into your agent's prompt:</p>
      <code class="box prompt-box">${esc(agentPrompt(code, info.agent_name ? { handle: info.agent_name, display: info.agent_display } : null))}</code>`);
    return;
  }
  showLoginBox(`<p>${esc(info.invited_by)} invited you to join as <b>${esc(info.role)}</b>${info.role === "viewer" ? " (read-only)" : ""}.</p>
    <form id="join-form">
      <label class="label field" for="join-name">Username</label>
      <input id="join-name" type="text" required pattern="[A-Za-z0-9][A-Za-z0-9_.\\-]{0,63}" placeholder="ana" autocomplete="username">
      <label class="label field" for="join-display">Display name</label>
      <input id="join-display" type="text" maxlength="128" placeholder="Ana Ruiz (optional)">
      <div class="row" style="margin-top:4px"><span class="err hidden" id="join-error"></span><span></span><button class="btn" type="submit">Join the board</button></div>
    </form>`);
  $("join-name").focus();
  $("join-form").onsubmit = async (e) => {
    e.preventDefault();
    try {
      finishJoin(await api("POST", "/join", { auth: false, body: { code, name: $("join-name").value.trim(),
        display_name: $("join-display").value.trim() || null } }));
    } catch (err) { $("join-error").textContent = err.message; $("join-error").classList.remove("hidden"); }
  };
}
function finishJoin(result) {
  state.token = result.token; store.set("token", result.token);
  history.replaceState(null, "", location.pathname);
  boot();
}
async function signOut() {
  try { if (state.me) await api("DELETE", "/tokens/" + encodeURIComponent(state.me.token.id)); } catch {}
  store.del("token"); location.replace(location.pathname);
}

const signinError = (e) => /^suspended/.test(e.message)
  ? "Your account is suspended by an admin" + (e.message.length > 9 ? ". Reason: " + e.message.slice(11) : ".")
  : "Your sign-in is no longer valid: " + e.message;
let refreshTimer = null;
let renderLibs = null;
async function boot() {
  if (!renderLibs) renderLibs = loadRenderLibs();
  if (inviteCode && new URLSearchParams(location.search).get("invite")) return showJoin(inviteCode);
  if (!state.token) return showLogin();
  try { state.me = await api("GET", "/me"); }
  catch (e) {
    if (e.status === 503 || e.status === 502 || !e.status) { showLoginBox(`<p>Board is starting, retrying…</p>`); setTimeout(boot, 2000); return; }
    return showLogin(e.status === 401 ? signinError(e) : e.message);
  }
  $("login").classList.add("hidden");
  $("app").classList.remove("hidden");
  renderMe();
  $("new-channel").classList.toggle("hidden", !can("create_channels"));
  $("new-dm").classList.toggle("hidden", !can("post"));
  $("settings-open").classList.toggle("hidden", !(can("invite_agents") || can("manage")));
  $("add-agents").classList.toggle("hidden", !can("invite_agents"));
  $("show-private-row").classList.toggle("hidden", !can("manage"));
  $("show-private").checked = !!(state.me.prefs && state.me.prefs.show_private);
  await Promise.all([refreshChannels(), renderLibs]);
  if (location.hash === "#settings") return openSettings();
  if (location.hash === "#activity") return openActivity();
  if (/^#(msg|file)\//.test(location.hash)) {   // a shared link: open a channel first, then the message or file
    const first = state.channels.find((c) => c.name === store.get("channel")) || state.channels.find((c) => c.kind !== "dm") || state.channels[0];
    const target = location.hash;
    if (first && target.startsWith("#file/")) await openChannel(first.id);
    return routeHash(target);
  }
  const want = decodeURIComponent(location.hash.slice(1)) || store.get("channel");
  const pick = state.channels.find((c) => c.name === want) || state.channels.find((c) => c.kind !== "dm") || state.channels[0];
  if (pick) openChannel(pick.id); else showNoChannels();
  if (!refreshTimer) refreshTimer = setInterval(refreshChannels, 10000);
}
function renderMe() {
  $("me-name").textContent = nameOf(state.me);
  $("me-avatar").innerHTML = avatar(state.me); hydrate($("me-avatar"));
}

// ------------------------------------------------------------------ channels and DMs
async function refreshChannels() {
  try { state.channels = (await api("GET", "/channels")).channels; }
  catch (e) { if (e.status === 401) return showLogin(signinError(e)); return; }
  const linkable = state.channels.filter((c) => c.kind !== "dm");
  state.chanIndex = new Map(linkable.map((c) => [c.name.toLowerCase(), c]));
  const key = linkable.map((c) => `${c.id}:${c.name}:${c.kind}`).join(",");
  if (key !== state.chanKey) {
    const first = !state.chanKey; state.chanKey = key;
    if (!first && state.current && state.view === "chat") renderFeed(false);   // links to new or renamed channels
  }
  renderChannels(); docMaybeChanged();
}
function dmPeer(c) {
  const others = (c.members || []).filter((m) => m && m.id !== state.me.id);
  return others.length === 1 && c.members.some((m) => m && m.id === state.me.id) ? others[0] : null;
}
function channelTitle(c) {
  if (c.kind !== "dm") return c.name;
  const peer = dmPeer(c);
  return peer ? nameOf(peer) : (c.members || []).map(nameOf).join(" · ");
}
// admins can see private channels and DMs they are not in; those are hidden unless "show private" is on
const observing = (c) => c.kind !== "public" && c.member === false;
// a channel you can see but have not joined: greyed in the sidebar, with a Join button in its header
const joinable = (c) => c.member === false && c.kind !== "dm" && !c.archived && can("post");
async function joinChannel(c) {
  try {
    await api("POST", `/channels/${c.id}/members`, { body: { principal_id: state.me.id } });
    c.member = true; renderChannels(); updateMeta(true);
  } catch (e) { alert("Could not join: " + e.message); }
}
const showPrivate = () => !!(state.me && state.me.prefs && state.me.prefs.show_private);
function navButton(c) {
  const isCur = state.view === "chat" && state.current && state.current.id === c.id;
  // bold = new messages; a count = messages for me (mentions, replies in my threads, DMs).
  // The open channel keeps its count: unopened threads stay "for me" until you open them.
  const unread = isCur ? 0 : c.unread || 0, forMe = c.for_me || 0;
  let glyph = c.kind === "private" ? LOCK : "#";
  if (c.kind === "dm") { const peer = dmPeer(c); glyph = peer ? avatar(peer, "xs", false) : "·"; }
  if (observing(c)) glyph = EYE;
  const li = document.createElement("li"), b = document.createElement("button");
  b.className = (isCur ? "active " : "") + (unread || forMe ? "unread " : "") + (c.archived ? "archived " : "")
    + (observing(c) ? "observer" : c.member === false && c.kind === "public" ? "not-joined" : "");
  if (!observing(c) && c.member === false && c.kind === "public") b.title = "You can read this channel but have not joined it";
  if (observing(c)) b.title = (c.kind === "dm" ? "Direct messages" : "Private channel") + " you are not part of (visible to you as an admin)";
  const draft = !isCur && draftOf("c:" + c.id);
  b.innerHTML = `<span class="glyph">${glyph}</span><span class="name">${esc(channelTitle(c))}</span>` +
    (draft ? `<span class="draft" title="Unsent draft">✎</span>` : "") + (forMe ? `<span class="count">${forMe > 99 ? "99+" : forMe}</span>` : "");
  b.onclick = () => { $("app").classList.remove("nav"); openChannel(c.id); };
  li.appendChild(b);
  if (c.kind !== "dm") {   // drag to reorder channels
    li.draggable = true; li.dataset.cid = c.id;
    li.addEventListener("dragstart", (e) => { e.dataTransfer.effectAllowed = "move"; e.dataTransfer.setData("text/x-channel", String(c.id)); li.classList.add("dragging"); });
    li.addEventListener("dragend", () => { li.classList.remove("dragging"); clearDrop(); });
    li.addEventListener("dragover", (e) => {
      if (![...e.dataTransfer.types].includes("text/x-channel")) return;
      e.preventDefault(); clearDrop();
      const r = li.getBoundingClientRect(); li.classList.add(e.clientY < r.top + r.height / 2 ? "drop-before" : "drop-after");
    });
    li.addEventListener("drop", (e) => {
      const id = Number(e.dataTransfer.getData("text/x-channel")); if (!id || id === c.id) return clearDrop();
      e.preventDefault();
      const before = li.classList.contains("drop-before"); clearDrop();
      const ids = [...$("channels").querySelectorAll("li[data-cid]")].map((x) => Number(x.dataset.cid)).filter((x) => x !== id);
      ids.splice(ids.indexOf(c.id) + (before ? 0 : 1), 0, id);
      saveChannelOrder(ids);
    });
  }
  return li;
}
$("new-below").onclick = () => { const sc = $("scroll"); sc.scrollTop = sc.scrollHeight; newBelow(0); };
$("scroll").addEventListener("scroll", () => { if (unseenBelow && nearBottom($("scroll"))) newBelow(0); }, { passive: true });
const clearDrop = () => document.querySelectorAll(".drop-before, .drop-after").forEach((x) => x.classList.remove("drop-before", "drop-after"));
async function saveChannelOrder(ids) {
  state.me.prefs = Object.assign({}, state.me.prefs, { channel_order: ids });
  renderChannels();
  try { state.me.prefs = await api("PUT", "/me/prefs", { body: { channel_order: ids } }); }
  catch (e) { alert("Could not save the order: " + e.message); }
}
function renderChannels() {
  const total = state.channels.reduce((n, c) => n + (c.for_me || 0), 0);
  $("settings-open").classList.toggle("on", state.view === "settings");
  $("activity-open").className = (state.view === "activity" ? "active " : "") + (total ? "unread" : "");
  $("activity-open").innerHTML = $("activity-open").innerHTML.replace(/<span class="count">.*?<\/span>/, "") + (total ? `<span class="count">${total > 99 ? "99+" : total}</span>` : "");
  $("channels").innerHTML = ""; $("dms").innerHTML = "";
  // your order first (saved on your account), unknown channels after it, archived ones after that,
  // and private conversations you only see as an admin at the very bottom
  const pos = new Map(((state.me && state.me.prefs && state.me.prefs.channel_order) || []).map((id, i) => [id, i]));
  const ordered = [...state.channels].sort((a, b) => (observing(a) ? 1 : 0) - (observing(b) ? 1 : 0)
    || (a.archived ? 1 : 0) - (b.archived ? 1 : 0)
    || (pos.has(a.id) ? pos.get(a.id) : 1e6) - (pos.has(b.id) ? pos.get(b.id) : 1e6));
  const isCur = (c) => state.view === "chat" && state.current && state.current.id === c.id;
  for (const c of ordered) if (!observing(c) || showPrivate() || isCur(c)) (c.kind === "dm" ? $("dms") : $("channels")).appendChild(navButton(c));
  if (!$("dms").children.length) $("dms").innerHTML = `<li class="small" style="padding:2px 16px">None yet</li>`;
  hydrate($("dms"));
}
function showNoChannels() {
  $("ch-title").textContent = ""; $("ch-desc").textContent = ""; $("ch-meta").textContent = "";
  $("messages").innerHTML = `<div class="empty">No channels you can read yet.</div>`;
  $("composer").classList.add("hidden");
}
// Read positions live on the server, so they follow you to other devices.
function markSeen() {
  const c = state.current;
  if (!c || state.view !== "chat") return;
  const seq = Math.max(state.cursor, c.last_seq);
  c.unread = 0;   // what stays "for me" (unopened threads) comes back from the server
  if ((state.readSent[c.id] || 0) >= seq) return;
  state.readSent[c.id] = seq;
  api("POST", `/channels/${c.id}/read`, { body: { seq } })
    .then((counts) => { c.for_me = counts.for_me; renderChannels(); })
    .catch(() => { state.readSent[c.id] = 0; });
}
function updateMeta(live) {
  const c = state.current; if (!c) return;
  // the header is just the name: say something only when it matters (offline, archived)
  const notes = [live ? "" : `<span class="live off"></span>offline`, c.archived ? "archived" : "",
    observing(c) ? `<span title="You can see this as an admin">${EYE} not a member</span>` : ""].filter(Boolean);
  $("ch-meta").innerHTML = notes.join(" · ");
  $("ch-join").classList.toggle("hidden", !joinable(c));
  $("ch-join").onclick = () => joinChannel(c);
}
function showChat() {
  state.view = "chat";
  $("settings").classList.add("hidden"); $("activity").classList.add("hidden"); $("search").classList.add("hidden"); $("chat-view").style.display = "contents";
}
async function openChannel(id) {
  const c = state.channels.find((x) => x.id === id);
  if (!c) return;
  showChat();
  if (state.poll) state.poll.abort();
  if (state.panel && state.panel.type !== "profile") closePanel();
  Object.assign(state, { current: c, roots: [], cursor: c.last_seq, oldest: null, hasOlder: false, editCursor: c.edit_rev || 0, editing: null });
  newBelow(0);
  state.byId.clear();
  store.set("channel", c.name);
  history.replaceState(null, "", location.pathname + "#" + encodeURIComponent(c.name));
  const glyph = observing(c) ? EYE : c.kind === "private" ? LOCK : c.kind === "dm" ? "@" : "#";
  $("ch-title").innerHTML = `<span class="glyph">${glyph}</span>${esc(channelTitle(c))}`;
  $("ch-desc").textContent = "";
  $("members-open").classList.add("hidden");
  $("ch-info").classList.remove("hidden"); $("ch-doc").classList.toggle("hidden", !docsOn()); closeInfo();
  $("messages").innerHTML = "";
  updateMeta(false); renderChannels();
  mountComposer($("composer"), { placeholder: c.kind === "dm" ? "Message " + channelTitle(c) : "Message #" + c.name, replyTo: () => null });
  try {
    const page = await api("GET", `/channels/${c.id}/messages?top_level=true&limit=${PAGE}&before=${c.last_seq + 1}`);
    if (state.current !== c) return;
    page.messages.forEach(remember);
    state.roots = page.messages; state.hasOlder = page.has_more;
    state.oldest = page.messages.length ? page.messages[0].seq : null;
    renderFeed(true);
  } catch (e) { $("messages").innerHTML = `<div class="empty">${esc(e.message)}</div>`; return; }
  markSeen(); renderChannels();
  pollLoop(c);
}
const PAGE = 200;   // messages per page of history
// older history loads by itself as you scroll near the top; renderFeed keeps what you are reading in place
async function loadOlder() {
  const c = state.current; if (!c || !state.oldest || state.loadingOlder) return;
  state.loadingOlder = true; renderOlderMarker();
  try {
    const page = await api("GET", `/channels/${c.id}/messages?top_level=true&limit=${PAGE}&before=${state.oldest}`);
    if (state.current !== c) return;
    page.messages.forEach(remember);
    state.roots = page.messages.concat(state.roots); state.hasOlder = page.has_more;
    if (page.messages.length) state.oldest = page.messages[0].seq;
  } finally { state.loadingOlder = false; }
  renderFeed(false);
}
function renderOlderMarker() {
  const m = $("messages").querySelector(".older"); if (m) m.textContent = state.loadingOlder ? "Loading earlier messages…" : "";
}
$("scroll").addEventListener("scroll", () => {
  if (state.view === "chat" && state.hasOlder && !state.loadingOlder && $("scroll").scrollTop < 600) loadOlder();
}, { passive: true });
const remember = (m) => state.byId.set(m.id, m);
function ingest(m) {
  if (state.byId.has(m.id)) return false;
  remember(m);
  if (!m.thread_id) { state.roots.push(m); return true; }
  const root = state.byId.get(m.thread_id);
  if (root) {
    const r = root.replies || (root.replies = { count: 0, last_at: 0, participants: [] });
    r.count += 1; r.last_at = m.created_at;
    const open = state.panel && state.panel.type === "thread" && state.panel.root.id === m.thread_id;
    const inThread = root.sender.id === state.me.id || r.participants.some((p) => p.id === state.me.id);
    if (!open && m.sender.id !== state.me.id && (inThread || (m.mentions || []).some((x) => x.id === state.me.id))) r.for_me_unread = (r.for_me_unread || 0) + 1;
    r.participants = [m.sender].concat(r.participants.filter((p) => p.id !== m.sender.id)).slice(0, 5);
  }
  if (state.panel && state.panel.type === "thread" && state.panel.root.id === m.thread_id) { state.thread.push(m); renderThread(nearBottom($("panel-scroll")) || m.sender.id === state.me.id); markThreadSeen(); }
  return true;
}
// opening a thread reads it: its "for you" badge and its share of the channel's count go away
function markThreadSeen() {
  const c = state.current, P = state.panel;
  if (!c || !P || P.type !== "thread" || document.visibilityState !== "visible") return;
  const root = P.root, seq = Math.max(root.seq, ...state.thread.map((x) => x.seq));
  if ((P.readSent || 0) >= seq) return;
  P.readSent = seq;
  if (root.replies) root.replies.for_me_unread = 0;
  api("POST", `/channels/${c.id}/threads/${root.id}/read`, { body: { seq } })
    .then((counts) => { const ch = state.channels.find((x) => x.id === c.id); if (ch) Object.assign(ch, { for_me: counts.for_me }); renderChannels(); })
    .catch(() => { P.readSent = 0; });
  if (state.view === "chat") renderFeed(false);
}
async function pollLoop(c) {
  const ctrl = new AbortController(); state.poll = ctrl;
  let delay = 1000;
  while (state.current === c && !ctrl.signal.aborted) {
    try {
      updateMeta(true);
      const page = await api("GET", `/channels/${c.id}/messages?after=${state.cursor}&limit=200&wait=25&edits_after=${state.editCursor}`, { signal: ctrl.signal });
      if (state.current !== c) return;
      if (page.edit_cursor != null) state.editCursor = Math.max(state.editCursor, page.edit_cursor);
      if (page.edits && page.edits.length && page.edits.map(applyEdit).some(Boolean) && !page.messages.length) rerenderAfterEdit();
      if (page.messages.length) {
        const sc = $("scroll"), atBottom = nearBottom(sc);
        const fresh = page.messages.filter((m) => !state.byId.has(m.id) && !m.thread_id && m.sender.id !== state.me.id).length;
        page.messages.forEach(ingest);
        state.cursor = page.next_cursor; c.last_seq = Math.max(c.last_seq, state.cursor);
        if (state.view === "chat") { renderFeed(atBottom); if (!atBottom && fresh) newBelow(unseenBelow + fresh); }
        if (document.visibilityState === "visible" && state.view === "chat") markSeen();
      }
      delay = 1000;
    } catch (e) {
      if (ctrl.signal.aborted) return;
      if (e.status === 401) return showLogin(signinError(e));
      updateMeta(false);
      await sleep(delay); delay = Math.min(delay * 2, 15000);
    }
  }
}
document.addEventListener("visibilitychange", () => { if (document.visibilityState === "visible" && state.view === "chat") { markSeen(); markThreadSeen(); renderChannels(); } });

// ------------------------------------------------------------------ reactions
const ICONS = {
  react: `<svg viewBox="0 0 18 18" aria-hidden="true"><circle cx="9" cy="9" r="6.6"/><path d="M6.2 10.4 Q9 13 11.8 10.4"/><path d="M6.8 7.2h.01M11.2 7.2h.01" stroke-width="2" stroke-linecap="round"/></svg>`,
  reply: `<svg viewBox="0 0 18 18" aria-hidden="true"><path d="M7.5 4.5 3.5 8.5l4 4"/><path d="M3.8 8.5h6.4a4.3 4.3 0 0 1 4.3 4.3v1.2"/></svg>`,
  copy: `<svg viewBox="0 0 18 18" aria-hidden="true"><rect x="6.5" y="6.5" width="8" height="8.5"/><path d="M4 11.5H3.5v-8h8V4"/></svg>`,
  link: `<svg viewBox="0 0 18 18" aria-hidden="true"><path d="M7.6 10.4a3 3 0 0 0 4.2 0l2.2-2.2a3 3 0 0 0-4.2-4.2l-1 1"/><path d="M10.4 7.6a3 3 0 0 0-4.2 0L4 9.8a3 3 0 0 0 4.2 4.2l1-1"/></svg>`,
  edit: `<svg viewBox="0 0 18 18" aria-hidden="true"><path d="M11.8 3.6l2.6 2.6-7.9 7.9-3.3.7.7-3.3z"/><path d="M10.4 5l2.6 2.6"/></svg>`,
  trash: `<svg viewBox="0 0 18 18" aria-hidden="true"><path d="M3.5 5h11M7 5V3.5h4V5M5 5l.7 9.5h6.6L13 5"/><path d="M7.6 7.5v5M10.4 7.5v5"/></svg>`,
  check: `<svg viewBox="0 0 18 18" aria-hidden="true"><path d="M3.8 9.4l3.2 3.2 7.2-7.4"/></svg>`,
};
const QUICK_REACTIONS = ["👍", "👎", "✅", "👀", "🎉", "❤️", "😂", "🙏"];
function reactionsHTML(m) {
  const list = m.reactions || [];
  if (!list.length) return "";
  const writable = canWriteHere();
  return `<div class="reacts">` + list.map((r) => {
    const who = r.by.map((b) => nameOf(b)).join(", ");
    return `<button class="rchip ${r.me ? "mine" : ""}" data-react="${esc(r.emoji)}" data-mid="${m.id}" ${writable ? "" : "disabled"}
      title="${esc(who)} reacted with ${esc(r.emoji)}" aria-pressed="${r.me}"><span class="re">${esc(r.emoji)}</span><span class="rc">${r.count}</span></button>`;
  }).join("") + (writable ? `<button class="rchip radd" data-react-pick="${m.id}" title="Add reaction" aria-label="Add reaction">+</button>` : "") + `</div>`;
}
async function toggleReaction(mid, emoji) {
  const c = state.current, m = state.byId.get(mid);
  if (!c || !m) return;
  const mine = (m.reactions || []).some((r) => r.emoji === emoji && r.me);
  try {
    const out = await api(mine ? "DELETE" : "PUT", `/channels/${c.id}/messages/${mid}/reactions/${encodeURIComponent(emoji)}`);
    applyEdit(out); rerenderAfterEdit();
    store.set("emoji-recent", JSON.stringify([emoji, ...recentEmoji().filter((x) => x !== emoji)].slice(0, 18)));
  } catch (e) { alert("Could not react: " + e.message); }
}
const recentEmoji = () => { try { return JSON.parse(store.get("emoji-recent") || "[]"); } catch { return []; } };
let reactPop = null;
function closeReactPicker() { if (reactPop) { reactPop.remove(); reactPop = null; document.removeEventListener("mousedown", reactOutside, true); } }
function reactOutside(e) { if (reactPop && !reactPop.contains(e.target)) closeReactPicker(); }
function openReactPicker(anchor, mid) {
  closeReactPicker();
  const pop = reactPop = document.createElement("div");
  pop.className = "react-pop"; pop.setAttribute("role", "dialog"); pop.setAttribute("aria-label", "Add reaction");
  pop.innerHTML = `<div class="rquick">${QUICK_REACTIONS.map((e) => `<button type="button" data-e="${e}">${e}</button>`).join("")}</div>
    <input type="text" placeholder="Search emoji" aria-label="Search emoji"><div class="emoji-grid"></div>`;
  document.body.appendChild(pop);
  const grid = pop.querySelector(".emoji-grid"), input = pop.querySelector("input");
  const render = (q) => {
    const words = q.trim().toLowerCase().split(/\s+/).filter(Boolean);
    let html = "";
    if (!words.length && recentEmoji().length) html += `<span class="label">Recent</span>` + recentEmoji().map((e) => `<button type="button" data-e="${e}">${e}</button>`).join("");
    for (const [cat, list] of EMOJI) {
      const shown = words.length ? list.filter((e) => words.every((w) => e[1].includes(w))) : list;
      if (shown.length) html += `<span class="label">${cat}</span>` + shown.map(([e, n]) => `<button type="button" data-e="${e}" title="${n.split(" ")[0]}">${e}</button>`).join("");
    }
    grid.innerHTML = html || `<div class="small" style="padding:10px">No emoji found.</div>`;
  };
  render("");
  input.oninput = () => render(input.value);
  input.onkeydown = (e) => {
    if (e.key === "Escape") { e.stopPropagation(); closeReactPicker(); }
    if (e.key === "Enter") { const first = grid.querySelector("[data-e]"); if (first) { closeReactPicker(); toggleReaction(mid, first.dataset.e); } }
  };
  pop.onclick = (e) => { const b = e.target.closest("[data-e]"); if (b) { closeReactPicker(); toggleReaction(mid, b.dataset.e); } };
  // place it next to what was clicked, inside the window
  const r = anchor.getBoundingClientRect(), w = pop.offsetWidth, h = pop.offsetHeight;
  pop.style.left = Math.max(8, Math.min(window.innerWidth - w - 8, r.right - w)) + "px";
  pop.style.top = (r.bottom + 6 + h < window.innerHeight ? r.bottom + 6 : Math.max(8, r.top - h - 6)) + "px";
  input.focus();
  document.addEventListener("mousedown", reactOutside, true);
}
document.addEventListener("keydown", (e) => { if (e.key === "Escape" && reactPop) closeReactPicker(); });
window.addEventListener("resize", closeReactPicker);

// ------------------------------------------------------------------ editing your own messages
function applyEdit(m) {
  // the same message can be held in several places (feed, thread panel, thread root); update every copy
  const copies = new Set([state.byId.get(m.id), state.panel && state.panel.root, ...state.roots, ...state.thread].filter((x) => x && x.id === m.id));
  copies.forEach((x) => Object.assign(x, { body: m.body, edited_at: m.edited_at, mentions: m.mentions }, m.reactions ? { reactions: m.reactions } : {}));
  return copies.size > 0;
}
function rerenderAfterEdit() {
  if (state.view === "chat" && state.current) renderFeed(false);
  if (state.panel && state.panel.type === "thread") renderThread(false);
}
function startEdit(id, where) {
  const m = state.byId.get(id);
  if (!m) return;
  state.editing = { id, where, draft: m.body, error: "", caret: m.body.length };
  rerenderAfterEdit();
}
function cancelEdit() { state.editing = null; rerenderAfterEdit(); }
async function saveEdit() {
  const ed = state.editing, c = state.current;
  if (!ed || ed.saving || !c) return;
  const m = state.byId.get(ed.id), text = ed.draft.trim();
  if (!m || text === m.body) return cancelEdit();
  if (!text && !m.attachments.length) { ed.error = "A message needs some text."; return rerenderAfterEdit(); }
  ed.saving = true;
  try {
    const out = await api("PATCH", `/channels/${c.id}/messages/${ed.id}`, { body: { body: text } });
    applyEdit(out);
    if (state.editing === ed) state.editing = null;
  } catch (e) { ed.error = "Not saved: " + e.message; }
  finally { ed.saving = false; }
  rerenderAfterEdit();
}
function wireEditor(container) {
  const ta = container.querySelector(".edit-box textarea"), ed = state.editing;
  if (!ta || !ed) return;
  const size = () => { ta.style.height = "auto"; ta.style.height = Math.min(ta.scrollHeight, 320) + "px"; };
  const keep = () => { ed.draft = ta.value; ed.caret = ta.selectionStart; };
  ta.addEventListener("input", () => { keep(); size(); });
  ta.addEventListener("keyup", keep); ta.addEventListener("click", keep);
  ta.addEventListener("keydown", (e) => {
    if (e.key === "Enter" && !e.shiftKey && !e.isComposing) { e.preventDefault(); saveEdit(); }
    else if (e.key === "Escape") { e.preventDefault(); e.stopPropagation(); cancelEdit(); }
  });
  size();
  // re-renders (new messages, other edits) rebuild the editor: give focus back unless it went somewhere else on purpose
  const active = document.activeElement;
  if (!active || active === document.body || !document.contains(active)) {
    ta.focus(); ta.setSelectionRange(ed.caret, ed.caret);
  }
}
async function openHistory(id) {
  const c = state.current, m = state.byId.get(id);
  if (!c || !m) return;
  state.panel = { type: "history", id };
  openPanel("history", "Edit history", `${nameOf(m.sender)} · ${c.kind === "dm" ? channelTitle(c) : "#" + c.name}`);
  $("panel-scroll").innerHTML = `<div class="empty">Loading…</div>`;
  let versions;
  try { versions = (await api("GET", `/channels/${c.id}/messages/${id}/revisions`)).versions; }
  catch (e) { $("panel-scroll").innerHTML = `<div class="empty">${esc(e.message)}</div>`; return; }
  if (!state.panel || state.panel.id !== id) return;
  $("panel-scroll").innerHTML = `<div class="pad"><p class="small" style="margin:0 0 12px">Edits never delete anything: every version is kept.</p>` +
    versions.slice().reverse().map((v) => `<div class="version"><div class="label">${v.replaced_at === null ? "current" : v.version === 1 ? "original" : "version " + v.version} · ${esc(fmtDay(v.written_at))} ${esc(fmtTime(v.written_at))}</div>
      <div class="${bodyClass()}">${renderBody(v.body, m.mentions || [])}</div></div>`).join("") + `</div>`;
  hydrate($("panel-scroll"));
}
async function deleteMessage(id) {
  const c = state.current, m = state.byId.get(id);
  if (!c || !m) return;
  const n = !m.thread_id && m.replies ? m.replies.count : 0;
  if (!confirm(`Delete this message by ${nameOf(m.sender)}${n ? ` and its ${n} ${n === 1 ? "reply" : "replies"}` : ""} for everyone? This cannot be undone.`)) return;
  let out;
  try { out = await api("DELETE", `/channels/${c.id}/messages/${id}`); } catch (e) { alert("Not deleted: " + e.message); return; }
  const gone = new Set(out.deleted);
  gone.forEach((x) => state.byId.delete(x));
  state.roots = state.roots.filter((x) => !gone.has(x.id));
  if (state.panel && state.panel.type === "thread") {
    if (gone.has(state.panel.root.id)) closePanel();
    else {
      state.thread = state.thread.filter((x) => !gone.has(x.id));
      const r = state.panel.root.replies; if (r) r.count = state.thread.length;
      renderThread(false);
    }
  }
  if (m.thread_id) { const root = state.byId.get(m.thread_id); if (root && root.replies) root.replies.count = Math.max(0, root.replies.count - 1); }
  renderFeed(false);
}
function lastOwnMessage(list) {
  for (let i = list.length - 1; i >= 0; i--) if (list[i].sender.id === state.me.id) return list[i];
  return null;
}

// ------------------------------------------------------------------ rendering messages
function canWriteHere() {
  const c = state.current;
  if (!c || !can("post") || c.archived) return false;
  if (c.kind === "dm") return !!dmPeer(c);
  return true;   // the server has the final say (guests, private channels)
}
function messageHTML(m, first, { inThread = false, rootId = null } = {}) {
  let h = first ? avatar(m.sender) : `<div class="gutter">${fmtClock(m.created_at)}</div>`;
  h += `<div class="content">`;
  if (first) h += `<div class="head"><span class="name" data-profile="${m.sender.id}">${esc(nameOf(m.sender))}</span><span class="label">${kindLabel(m.sender)}</span><span class="time">${fmtTime(m.created_at)}</span><span class="seq">#${m.seq}</span></div>`;
  if (inThread && m.reply_to && m.reply_to !== rootId) {
    const p = state.byId.get(m.reply_to);
    if (p) h += `<div class="inreply">replying to ${esc(nameOf(p.sender))}</div>`;
  }
  const where = inThread ? "thread" : "feed", editingThis = state.editing && state.editing.id === m.id && state.editing.where === where;
  if (editingThis) {
    h += `<div class="edit-box"><textarea rows="1" aria-label="Edit message">${esc(state.editing.draft)}</textarea>` +
      `<div class="edit-bar"><span class="hint">${state.editing.error ? esc(state.editing.error) : "enter to save · esc to cancel"}</span>` +
      `<button class="btn ghost small" data-edit-cancel>Cancel</button><button class="btn small" data-edit-save>Save</button></div></div>`;
  } else {
    let body = renderBody(m.body, m.mentions || []);
    if (m.edited_at) {   // "(edited)" sits at the end of the last paragraph, like the text it qualifies
      const mark = `<span class="edited" data-history="${m.id}" role="button" tabindex="0" title="Edited ${esc(fmtTime(m.edited_at))}. Show earlier versions">(edited)</span>`;
      body = /<\/p>\s*$/.test(body) ? body.replace(/<\/p>\s*$/, " " + mark + "</p>") : body + " " + mark;
    }
    h += `<div class="${bodyClass()}">${body}</div>`;
  }
  if (m.attachments.length) h += `<div class="files">` + m.attachments.map((a, i) => `<button class="fchip" data-att="${esc(a)}" data-msg="${m.id}" data-idx="${i}">` +
    `<span class="fprev">file</span><span class="fcopy"><span class="fname">loading…</span><span class="fmeta">&nbsp;</span></span></button>`).join("") + `</div>`;
  h += reactionsHTML(m);
  if (!inThread && m.replies && m.replies.count) {
    const r = m.replies;
    const mine = r.for_me_unread || 0;   // mentions or replies for me in this thread that I have not opened yet
    h += `<button class="replies${mine ? " for-me" : ""}" data-thread="${m.id}"><span class="faces">${r.participants.slice(0, 4).map((p) => avatar(p, "xs", false)).join("")}</span>` +
      `<b>${r.count} ${r.count === 1 ? "reply" : "replies"}</b>${mine ? `<span class="rbadge" title="Unread for you in this thread">${mine} for you</span>` : ""}<span class="when">Last reply ${esc(ago(r.last_at))}</span></button>`;
  }
  h += `</div>`;
  const acts = [];
  const act = (attrs, icon, label) => `<button ${attrs} title="${label}" aria-label="${label}">${ICONS[icon]}</button>`;
  if (canWriteHere()) acts.push(act(`data-react-pick="${m.id}"`, "react", "Add reaction"));
  if (!inThread && canWriteHere()) acts.push(act(`data-thread="${m.id}"`, "reply", "Reply in thread"));
  acts.push(act(`data-copy-text="${m.id}"`, "copy", "Copy message text"));
  acts.push(act(`data-copy-link="${m.id}"`, "link", "Copy link to message"));
  if (!editingThis && state.me && m.sender.id === state.me.id && state.me.identity_type === "user" && canWriteHere()) acts.push(act(`data-edit="${m.id}" data-where="${where}"`, "edit", "Edit message"));
  if (can("manage")) acts.push(act(`data-delete-msg="${m.id}"`, "trash", "Delete message (admin)"));
  if (acts.length) h += `<div class="actions">${acts.join("")}</div>`;
  return h;
}
function renderList(container, list, opts = {}) {
  let prev = null;
  for (const m of list) {
    const day = fmtDay(m.created_at);
    if (!opts.noDays && (!prev || fmtDay(prev.created_at) !== day)) { container.insertAdjacentHTML("beforeend", `<div class="day label">${esc(day)}</div>`); prev = null; }
    const grouped = prev && prev.sender.id === m.sender.id && m.created_at - prev.created_at < 300 && !(prev.replies && prev.replies.count && !opts.inThread);
    const el = document.createElement("div");
    el.className = "msg" + (grouped ? "" : " first") + (state.panel && state.panel.type === "thread" && state.panel.root.id === m.id && !opts.inThread ? " open-thread" : "") +
      ((m.mentions || []).some((x) => x.id === state.me.id) ? " for-me" : "");
    el.dataset.mid = m.id;
    el.innerHTML = messageHTML(m, !grouped, opts);
    container.appendChild(el);
    prev = m;
  }
  container.querySelectorAll("[data-att]").forEach(fillFile);
  hydrate(container);
  wireEditor(container);
}
// Re-render without moving what you are reading: remember the first visible message and its offset, put it back after.
const nearBottom = (sc) => sc.scrollHeight - sc.scrollTop - sc.clientHeight < 80;
function keepAnchor(sc) {
  const top = sc.getBoundingClientRect().top;
  const el = [...sc.querySelectorAll(".msg[data-mid]")].find((m) => m.getBoundingClientRect().bottom > top + 1);
  if (!el) return () => {};
  const mid = el.dataset.mid, off = el.getBoundingClientRect().top - top;
  return () => {
    const again = sc.querySelector(`.msg[data-mid="${mid}"]`);
    if (again) sc.scrollTop += again.getBoundingClientRect().top - sc.getBoundingClientRect().top - off;
  };
}
function renderFeed(stickToBottom) {
  const sc = $("scroll"), restore = stickToBottom ? null : keepAnchor(sc);
  const box = $("messages"); box.innerHTML = "";
  if (state.hasOlder) {
    box.insertAdjacentHTML("beforeend", `<div class="older label">${state.loadingOlder ? "Loading earlier messages…" : ""}</div>`);
  } else if (state.roots.length && state.current) {
    box.insertAdjacentHTML("beforeend", `<div class="older label">Beginning of ${esc(state.current.kind === "dm" ? channelTitle(state.current) : "#" + state.current.name)}</div>`);
  }
  if (!state.roots.length) { box.insertAdjacentHTML("beforeend", `<div class="empty">No messages yet.${canWriteHere() ? " Say hello." : ""}</div>`); return; }
  renderList(box, state.roots);
  if (stickToBottom) { sc.scrollTop = sc.scrollHeight; newBelow(0); } else restore();
}
// "N new messages" while you are scrolled up; clicking it (or scrolling down) clears it
let unseenBelow = 0;
function newBelow(n) {
  unseenBelow = n;
  const b = $("new-below");
  b.classList.toggle("hidden", !n);
  b.textContent = n ? `${n} new message${n === 1 ? "" : "s"} ↓` : "";
}
// ---------- attachments: kinds, chips, thumbnails, download
const IMAGE_EXT = { png: "image/png", jpg: "image/jpeg", jpeg: "image/jpeg", gif: "image/gif", webp: "image/webp", svg: "image/svg+xml", bmp: "image/bmp", avif: "image/avif", ico: "image/x-icon" };
const TEXT_EXT = new Set(["txt", "md", "markdown", "log", "csv", "tsv", "json", "jsonl", "ndjson", "yaml", "yml", "toml", "ini", "cfg", "conf", "env",
  "py", "js", "mjs", "cjs", "ts", "tsx", "jsx", "sh", "bash", "zsh", "rb", "go", "rs", "java", "kt", "c", "h", "cc", "cpp", "hpp", "cs", "swift",
  "php", "pl", "r", "jl", "lua", "sql", "css", "scss", "xml", "tex", "bib", "diff", "patch", "ipynb", "dockerfile", "makefile", "gitignore"]);
const extOf = (name) => { const n = (name || "").toLowerCase(); const i = n.lastIndexOf("."); return i > 0 ? n.slice(i + 1) : n === "dockerfile" || n === "makefile" ? n : ""; };
function kindOf(name, type) {
  const ext = extOf(name), t = (type || "").toLowerCase();
  if (IMAGE_EXT[ext] || t.startsWith("image/")) return "image";
  if (ext === "pdf" || t === "application/pdf") return "pdf";
  if (ext === "html" || ext === "htm" || t === "text/html") return "html";
  if (TEXT_EXT.has(ext) || t.startsWith("text/") || t === "application/json") return "text";
  return "other";
}
const typeLabel = (name, kind) => (extOf(name) || kind).slice(0, 4) || "file";
const fileInfo = new Map(), fileBlobs = new Map();
const storageUrl = (id) => "/storage/" + id.slice(8).split("/").map(encodeURIComponent).join("/");
const getInfo = (id) => {
  if (!fileInfo.has(id)) fileInfo.set(id, (id.startsWith("storage:")
    ? api("GET", storageUrl(id) + "?info=true").then((f) => ({ id, filename: f.path.split("/").pop(), content_type: f.content_type, size: f.size, sha256: f.sha256 }))
    : api("GET", `/attachments/${encodeURIComponent(id)}/info`)).catch(() => null));
  return fileInfo.get(id);
};
const getBlob = (id) => {
  if (!fileBlobs.has(id)) fileBlobs.set(id, api("GET", id.startsWith("storage:") ? storageUrl(id) : `/attachments/${encodeURIComponent(id)}`, { raw: true }).then((r) => r.blob())
    .catch((e) => { fileBlobs.delete(id); throw e; }));
  return fileBlobs.get(id);
};
async function fillFile(btn) {
  const id = btn.dataset.att, info = await getInfo(id);
  if (!btn.isConnected) return;
  if (!info) { btn.querySelector(".fname").textContent = "unavailable"; return; }
  const kind = kindOf(info.filename, info.content_type);
  btn.querySelector(".fname").textContent = info.filename || id;
  btn.querySelector(".fmeta").textContent = `${fmtSize(info.size)} · ${typeLabel(info.filename, kind)}`;
  btn.title = `${info.filename || id} (${fmtSize(info.size)}). Click to preview.`;
  const prev = btn.querySelector(".fprev");
  prev.textContent = typeLabel(info.filename, kind);
  if (kind === "image" && info.size <= 8 * 1024 * 1024) {
    try {
      const url = URL.createObjectURL(await typed(await getBlob(id), info));
      if (btn.isConnected) prev.innerHTML = `<img alt="" src="${url}">`;
    } catch {}
  }
}
// Give a blob the right type for previewing (the server sends everything as a plain download).
function typed(blob, info) {
  const kind = kindOf(info.filename, info.content_type), ext = extOf(info.filename);
  const type = kind === "image" ? (IMAGE_EXT[ext] || info.content_type) : kind === "pdf" ? "application/pdf" : kind === "html" ? "text/html" : "text/plain";
  return blob.slice(0, blob.size, type);
}
async function download(id) {
  const [info, blob] = await Promise.all([getInfo(id), getBlob(id)]);
  const a = document.createElement("a"); a.href = URL.createObjectURL(blob);
  a.download = (info && info.filename) || id; a.click();
  setTimeout(() => URL.revokeObjectURL(a.href), 5000);
}

// ---------- the preview overlay
const viewer = { list: [], index: 0, urls: [] };
function openViewer(list, index) {
  viewer.list = list; viewer.index = index;
  $("viewer").classList.remove("hidden");
  showInViewer();
}
function closeViewer() {
  $("viewer").classList.add("hidden"); $("v-body").innerHTML = "";
  viewer.urls.forEach((u) => URL.revokeObjectURL(u)); viewer.urls = [];
}
async function showInViewer(view = "render") {
  const id = viewer.list[viewer.index], n = viewer.list.length;
  $("v-nav").classList.toggle("hidden", n < 2);
  $("v-pos").textContent = `${viewer.index + 1} of ${n}`;
  $("v-prev").disabled = viewer.index === 0; $("v-next").disabled = viewer.index === n - 1;
  $("v-name").textContent = "Loading…"; $("v-meta").textContent = ""; $("v-tabs").classList.add("hidden"); $("v-open").classList.add("hidden");
  const body = $("v-body"); body.className = "v-body"; body.innerHTML = `<div class="v-empty">Loading…</div>`; zoomControls(false);
  $("v-download").onclick = () => download(id).catch(() => {});
  $("v-link").onclick = () => copyText(boardURL("file/" + id), $("v-link"));
  const info = await getInfo(id);
  if (viewer.list[viewer.index] !== id) return;
  if (!info) { body.innerHTML = `<div class="v-empty">This file is not available.</div>`; return; }
  const kind = kindOf(info.filename, info.content_type);
  $("v-name").textContent = info.filename || id;
  $("v-meta").textContent = `${fmtSize(info.size)} · ${typeLabel(info.filename, kind)}`;
  const noPreview = (why) => { body.innerHTML = `<div class="v-empty"><div class="fprev">${esc(typeLabel(info.filename, kind))}</div>${why}<br><br>` +
    `<button class="btn" onclick="document.getElementById('v-download').click()">Download</button></div>`; };
  if (kind === "other") return noPreview("No preview for this kind of file.");
  const limit = kind === "text" || kind === "html" ? 2 * 1024 * 1024 : 60 * 1024 * 1024;
  if (info.size > limit) return noPreview(`Too large to preview (${fmtSize(info.size)}).`);
  let blob;
  try { blob = typed(await getBlob(id), info); } catch (e) { return noPreview(esc(e.message)); }
  if (viewer.list[viewer.index] !== id) return;
  const url = URL.createObjectURL(blob); viewer.urls.push(url);
  if (kind === "image") {
    body.classList.add("center"); body.innerHTML = `<img alt="${esc(info.filename || "")}" src="${url}" title="Click to zoom">`;
    const img = body.querySelector("img");
    const ready = () => { if (viewer.list[viewer.index] === id) { zoomControls(true); setupImageZoom(body, img); } };
    if (img.complete && img.naturalWidth) ready(); else img.onload = ready;
    return;
  }
  if (kind === "pdf") {
    $("v-open").classList.remove("hidden");
    $("v-open").onclick = () => window.open(url, "_blank", "noopener");
    return renderPdf(body, blob, id).catch((e) => {
      if (viewer.list[viewer.index] === id) noPreview(`Could not render this PDF here (${esc(e.message)}). Try "Open in new tab".`);
    });
  }
  const text = await blob.text();
  if (kind === "html") {
    $("v-tabs").classList.remove("hidden");
    $("v-tabs").querySelectorAll("button").forEach((b) => { b.classList.toggle("on", b.dataset.view === view); b.onclick = () => showInViewer(b.dataset.view); });
    // An opaque-origin sandbox: scripts may run, but cannot reach this page, its storage or the board token.
    if (view === "render") { body.innerHTML = `<iframe title="HTML preview" sandbox="allow-scripts" referrerpolicy="no-referrer"></iframe>`; body.querySelector("iframe").srcdoc = text; return; }
  }
  let shown = text;
  if (extOf(info.filename) === "json") { try { shown = JSON.stringify(JSON.parse(text), null, 2); } catch {} }
  const lines = shown.split("\n");
  const cut = lines.length > 20000;
  body.innerHTML = `<pre class="v-code">${lines.slice(0, 20000).map((l) => `<span class="l">${esc(l) || " "}</span>`).join("")}</pre>` +
    (cut ? `<div class="v-empty">Showing the first 20,000 of ${lines.length.toLocaleString()} lines.</div>` : "");
}
// PDFs are drawn with PDF.js, because browsers refuse their own PDF viewer inside sandboxed
// frames (the huggingface.co Space page embeds the board in one). Loaded on first use.
const PDFJS = "https://cdnjs.cloudflare.com/ajax/libs/pdf.js/3.11.174";
let pdfjsReady = null;
function loadPdfJs() {
  if (!pdfjsReady) pdfjsReady = new Promise((resolve, reject) => {
    const sc = document.createElement("script");
    sc.src = `${PDFJS}/pdf.min.js`; sc.crossOrigin = "anonymous";
    sc.onload = () => { window.pdfjsLib.GlobalWorkerOptions.workerSrc = `${PDFJS}/pdf.worker.min.js`; resolve(window.pdfjsLib); };
    sc.onerror = () => { pdfjsReady = null; reject(new Error("the PDF viewer did not load")); };
    document.head.appendChild(sc);
  });
  return pdfjsReady;
}
async function renderPdf(body, blob, id, zoom = 1, loaded = null) {
  let doc = loaded;
  if (!doc) {
    body.classList.add("pdf"); body.innerHTML = `<div class="v-empty">Rendering…</div>`;
    const lib = await loadPdfJs();
    // isEvalSupported: false keeps PDF.js from evaluating code found in font data.
    doc = await lib.getDocument({ data: await blob.arrayBuffer(), isEvalSupported: false }).promise;
    if (viewer.list[viewer.index] !== id) return doc.destroy();
    $("v-meta").textContent += ` · ${doc.numPages} ${doc.numPages === 1 ? "page" : "pages"}`;
    // zoom is relative to "fit the width"; re-rendering keeps the text sharp and the reading position
    const z = viewer.zoom = { kind: "pdf", fit: true, scale: 1 };
    z.apply = (scale) => {
      const fit = scale === "fit", next = fit ? 1 : Math.max(0.25, Math.min(4, scale));
      const ry = body.scrollTop / Math.max(1, body.scrollHeight), rx = body.scrollLeft / Math.max(1, body.scrollWidth);
      z.fit = fit; z.scale = next; setZoomLabel();
      renderPdf(body, blob, id, next, doc).then(() => { body.scrollTop = ry * body.scrollHeight; body.scrollLeft = rx * body.scrollWidth; });
    };
    z.step = (dir) => z.apply(nextStep(z.scale, dir));
    zoomControls(true); setZoomLabel();
  }
  const first = await doc.getPage(1), base = first.getViewport({ scale: 1 });
  const width = Math.round(Math.min(body.clientWidth - 40, 980) * zoom), scale = width / base.width, dpr = window.devicePixelRatio || 1;
  body.innerHTML = `<div class="v-pages">${Array.from({ length: doc.numPages }, (_, i) =>
    `<div class="v-page" data-page="${i + 1}" style="width:${width}px;height:${Math.round(base.height * scale)}px"><span class="pno">${i + 1}</span></div>`).join("")}</div>`;
  const draw = async (el) => {
    if (el.dataset.drawn) return; el.dataset.drawn = "1";
    const page = await doc.getPage(Number(el.dataset.page)), vp = page.getViewport({ scale: scale * dpr });
    const canvas = document.createElement("canvas"); canvas.width = vp.width; canvas.height = vp.height;
    el.style.height = `${vp.height / dpr}px`; el.prepend(canvas);
    await page.render({ canvasContext: canvas.getContext("2d"), viewport: vp }).promise;
  };
  const seen = new IntersectionObserver((entries) => entries.forEach((e) => e.isIntersecting && draw(e.target)), { root: body, rootMargin: "600px 0px" });
  body.querySelectorAll(".v-page").forEach((el) => seen.observe(el));
}
// ---- zoom for images and PDFs in the viewer
const ZOOM_STEPS = [0.1, 0.25, 0.33, 0.5, 0.67, 0.8, 1, 1.25, 1.5, 2, 3, 4, 6, 8];
function zoomControls(show) {
  $("v-zoom").classList.toggle("hidden", !show);
  if (!show) viewer.zoom = null;
}
function setZoomLabel() {
  const z = viewer.zoom; if (!z) return;
  $("v-zoom-level").textContent = z.fit ? "fit" : Math.round(z.scale * 100) + "%";
  $("v-zoom-fit").disabled = !!z.fit;
}
function nextStep(scale, dir) {
  return dir > 0 ? (ZOOM_STEPS.find((s) => s > scale + 1e-3) || ZOOM_STEPS.at(-1))
                 : ([...ZOOM_STEPS].reverse().find((s) => s < scale - 1e-3) || ZOOM_STEPS[0]);
}
// image: fit to the window, or a scale of its real size; zooming keeps the point under the cursor still
function setupImageZoom(body, img) {
  const z = viewer.zoom = { kind: "image", fit: true, scale: 1, img };
  const fitScale = () => Math.min(1, (body.clientWidth - 24) / img.naturalWidth, (body.clientHeight - 24) / img.naturalHeight);
  z.apply = (scale, at) => {
    const before = img.getBoundingClientRect(), box = body.getBoundingClientRect();
    const px = at ? (at.x - before.left) / before.width : 0.5, py = at ? (at.y - before.top) / before.height : 0.5;
    const fit = scale === "fit";
    z.fit = fit; z.scale = fit ? fitScale() : Math.max(ZOOM_STEPS[0], Math.min(ZOOM_STEPS.at(-1), scale));
    body.classList.toggle("zoomed", !fit);
    img.style.width = fit ? "" : Math.round(img.naturalWidth * z.scale) + "px";
    img.style.height = fit ? "" : Math.round(img.naturalHeight * z.scale) + "px";
    if (!fit) {   // keep the point that was under the cursor (or the middle) where it was
      const after = img.getBoundingClientRect();
      const ax = at ? at.x - box.left : box.width / 2, ay = at ? at.y - box.top : box.height / 2;
      body.scrollLeft += after.left - box.left + px * after.width - ax;
      body.scrollTop += after.top - box.top + py * after.height - ay;
    }
    setZoomLabel();
  };
  z.step = (dir, at) => z.apply(nextStep(z.fit ? fitScale() : z.scale, dir), at);
  // click toggles fit <-> real size at that spot; drag pans when zoomed
  let drag = null, moved = false;
  img.addEventListener("pointerdown", (e) => {
    if (e.button !== 0) return;
    moved = false;
    if (!z.fit) { drag = { x: e.clientX, y: e.clientY, l: body.scrollLeft, t: body.scrollTop }; img.setPointerCapture(e.pointerId); body.classList.add("panning"); }
  });
  img.addEventListener("pointermove", (e) => {
    if (!drag) return;
    if (Math.abs(e.clientX - drag.x) + Math.abs(e.clientY - drag.y) > 3) moved = true;
    body.scrollLeft = drag.l - (e.clientX - drag.x); body.scrollTop = drag.t - (e.clientY - drag.y);
  });
  const end = () => { drag = null; body.classList.remove("panning"); };
  img.addEventListener("pointerup", end); img.addEventListener("pointercancel", end);
  img.addEventListener("click", (e) => { if (moved) return; z.apply(z.fit ? Math.max(1, fitScale() * 2) : "fit", { x: e.clientX, y: e.clientY }); });
  img.draggable = false;
  setZoomLabel();
}
// Ctrl/⌘ + wheel, and trackpad pinch (which browsers report as ctrl + wheel), zoom where the cursor is
$("v-body").addEventListener("wheel", (e) => {
  const z = viewer.zoom;
  if (!z || !(e.ctrlKey || e.metaKey)) return;
  e.preventDefault();
  if (z.kind === "image") {
    const cur = z.fit ? z.img.getBoundingClientRect().width / z.img.naturalWidth : z.scale;
    z.apply(cur * Math.exp(-Math.max(-150, Math.min(150, e.deltaY)) * 0.002), { x: e.clientX, y: e.clientY });   // ~1.2x per wheel notch; pinch is smooth
  } else z.step(e.deltaY < 0 ? 1 : -1);
}, { passive: false });
$("v-zoom-in").onclick = () => viewer.zoom && viewer.zoom.step(1);
$("v-zoom-out").onclick = () => viewer.zoom && viewer.zoom.step(-1);
$("v-zoom-fit").onclick = () => viewer.zoom && viewer.zoom.apply("fit");
document.addEventListener("keydown", (e) => {
  if ($("viewer").classList.contains("hidden") || !viewer.zoom || e.target.closest("input, textarea")) return;
  if (e.key === "+" || e.key === "=") { e.preventDefault(); viewer.zoom.step(1); }
  else if (e.key === "-" || e.key === "_") { e.preventDefault(); viewer.zoom.step(-1); }
  else if (e.key === "0") { e.preventDefault(); viewer.zoom.apply("fit"); }
});
$("v-close").onclick = closeViewer;
$("v-prev").onclick = () => { if (viewer.index > 0) { viewer.index--; showInViewer(); } };
$("v-next").onclick = () => { if (viewer.index < viewer.list.length - 1) { viewer.index++; showInViewer(); } };
$("viewer").addEventListener("click", (e) => { if (e.target.id === "viewer") closeViewer(); });
document.addEventListener("keydown", (e) => {
  if ($("viewer").classList.contains("hidden")) return;
  if (e.key === "Escape") { e.stopImmediatePropagation(); closeViewer(); }
  else if (e.key === "ArrowLeft") $("v-prev").click();
  else if (e.key === "ArrowRight") $("v-next").click();
}, true);
document.addEventListener("keydown", (e) => {
  const link = e.target.closest && e.target.closest('.mention[role="link"], .chan-link, .edited[data-history]');
  if (link && (e.key === "Enter" || e.key === " ")) { e.preventDefault(); link.click(); }
});
document.addEventListener("click", (e) => {
  if (e.target.closest("dialog")) return;
  const del = e.target.closest("[data-delete-msg]");
  if (del) { deleteMessage(Number(del.dataset.deleteMsg)); return; }
  const ct = e.target.closest("[data-copy-text]");
  if (ct) { const msg = state.byId.get(Number(ct.dataset.copyText)); if (msg) copyText(msg.body, ct); return; }
  const bf = e.target.closest("a[data-board-file]");
  if (bf) { e.preventDefault(); openViewer([bf.dataset.boardFile], 0); return; }
  const cl = e.target.closest("[data-copy-link]");
  if (cl) { copyText(boardURL("msg/" + cl.dataset.copyLink), cl); return; }
  const rpick = e.target.closest("[data-react-pick]");
  if (rpick) { openReactPicker(rpick, Number(rpick.dataset.reactPick)); return; }
  const rchip = e.target.closest("[data-react]");
  if (rchip) { if (!rchip.disabled) toggleReaction(Number(rchip.dataset.mid), rchip.dataset.react); return; }
  const edit = e.target.closest("[data-edit]");
  if (edit) { startEdit(Number(edit.dataset.edit), edit.dataset.where); return; }
  if (e.target.closest("[data-edit-save]")) { saveEdit(); return; }
  if (e.target.closest("[data-edit-cancel]")) { cancelEdit(); return; }
  if (e.target.closest(".edit-box")) return;
  const hist = e.target.closest("[data-history]");
  if (hist) { openHistory(Number(hist.dataset.history)); return; }
  const prof = e.target.closest("[data-profile]");
  if (prof && !e.target.closest(".replies")) { openProfile(Number(prof.dataset.profile)); return; }
  const th = e.target.closest("[data-thread]");
  if (th) { openThread(Number(th.dataset.thread)); return; }
  const file = e.target.closest("[data-att]");
  if (file) {
    const msg = state.byId.get(Number(file.dataset.msg));
    openViewer(msg ? msg.attachments : [file.dataset.att], msg ? Number(file.dataset.idx) : 0);
    return;
  }
  const ch = e.target.closest("[data-channel]");
  if (ch) { const c = state.channels.find((x) => x.id === Number(ch.dataset.channel)); if (c) { closePanel(); openChannel(c.id); } }
});

// ------------------------------------------------------------------ composer
const MAX_FILES = 16, MAX_FILE_BYTES = 50 * 1024 * 1024;
function uploadWithProgress(channelId, item, onProgress) {
  return new Promise((resolve, reject) => {
    const x = new XMLHttpRequest(); item.xhr = x;
    x.open("PUT", `v1/channels/${channelId}/attachments/${item.uid}`);
    x.setRequestHeader("X-Msgboard-Token", state.token);
    x.setRequestHeader("Content-Type", item.file.type || "application/octet-stream");
    x.setRequestHeader("X-Filename", encodeURIComponent(item.file.name).slice(0, 255));
    x.upload.onprogress = (e) => { if (e.lengthComputable) onProgress(e.loaded); };
    x.onload = () => {
      if (x.status < 300) return resolve(JSON.parse(x.responseText));
      let msg = x.statusText || "upload failed";
      try { msg = JSON.parse(x.responseText).error.message; } catch {}
      const err = new Error(msg); err.status = x.status; err.retryAfter = Number(x.getResponseHeader("Retry-After")) || 1;
      reject(err);
    };
    x.onerror = () => reject(new Error("network error"));
    x.onabort = () => reject(new Error("cancelled"));
    x.send(item.file);
  });
}
function mountComposer(host, { placeholder, replyTo }) {
  host.classList.remove("hidden");
  if (!canWriteHere()) {
    const c = state.current;
    host.innerHTML = `<div class="readonly">${c && c.archived ? "This channel is archived. History is read-only."
      : !can("post") ? "You have read-only access to this board." : "You can read this conversation but not write in it."}</div>`;
    return;
  }
  host.innerHTML = `<div class="cbox"><div class="files hidden"></div><textarea rows="1" placeholder="${esc(placeholder)}"></textarea>
    <div class="bar"><div class="left"><button class="tool emoji-btn" type="button" title="Emoji" aria-label="Emoji" aria-haspopup="dialog" aria-expanded="false">
      <svg viewBox="0 0 18 18" aria-hidden="true"><circle cx="9" cy="9" r="7.2"/><path d="M5.8 10.6 Q9 13.6 12.2 10.6"/><path d="M6.6 7 h.01 M11.4 7 h.01" stroke-width="2.2" stroke-linecap="round"/></svg></button>
      <button class="attach">attach files</button><span class="pending"></span></div><button class="btn">Send</button></div></div>
    <input type="file" multiple class="hidden">`;
  const box = host.querySelector(".cbox"), chips = host.querySelector(".files"), input = host.querySelector("textarea");
  const fileInput = host.querySelector("input[type=file]"), pending = host.querySelector(".pending"), sendBtn = host.querySelector(".btn");
  const channel = state.current;
  const draftKey = replyTo() ? "t:" + replyTo() : "c:" + channel.id;
  input.value = draftOf(draftKey);
  input.addEventListener("input", () => setDraft(draftKey, input.value));
  let items = [], sending = false;
  const autosize = () => { input.style.height = "auto"; input.style.height = Math.min(input.scrollHeight, 220) + "px"; };
  const UPLOADS_AT_ONCE = 3;   // the server accepts a few concurrent uploads; the rest wait their turn
  const uploading = () => items.some((it) => it.status === "uploading" || it.status === "queued");
  function renderChips() {
    chips.classList.toggle("hidden", !items.length);
    chips.innerHTML = items.map((it, i) => {
      const kind = kindOf(it.file.name, it.file.type);
      const pct = it.file.size ? Math.round(100 * it.loaded / it.file.size) : 100;
      const meta = it.status === "queued" ? `waiting · ${fmtSize(it.file.size)}`
        : it.status === "uploading" ? `${pct}% · ${fmtSize(it.loaded)} of ${fmtSize(it.file.size)}`
        : it.status === "error" ? `not uploaded: ${esc(it.error)}` : `${fmtSize(it.file.size)} · ${esc(typeLabel(it.file.name, kind))}`;
      return `<div class="fchip ${it.status}"><span class="fprev">${it.thumb ? `<img alt="" src="${it.thumb}">` : esc(typeLabel(it.file.name, kind))}</span>` +
        `<span class="fcopy"><span class="fname">${esc(it.file.name)}</span><span class="fmeta">${meta}</span>` +
        (it.status === "uploading" || it.status === "queued" ? `<span class="fbar"><span style="width:${it.status === "queued" ? 0 : pct}%"></span></span>` : "") + `</span>` +
        (it.status === "error" ? `<button class="retry" data-retry="${i}">retry</button>` : "") +
        `<button data-remove="${i}" aria-label="Remove ${esc(it.file.name)}">×</button></div>`;
    }).join("");
    sendBtn.disabled = sending || uploading();
  }
  function pump() {
    while (items.filter((it) => it.status === "uploading").length < UPLOADS_AT_ONCE) {
      const next = items.find((it) => it.status === "queued");
      if (!next) break;
      start(next);
    }
    renderChips();
  }
  async function start(it) {
    it.status = "uploading"; it.loaded = 0; it.error = null; renderChips();
    for (let attempt = 0; ; attempt++) {
      try {
        it.att = await uploadWithProgress(channel.id, it, (loaded) => { it.loaded = loaded; renderChips(); });
        it.status = "done";
        break;
      } catch (e) {
        if (!items.includes(it)) return;          // removed while uploading
        if ((e.status === 429 || e.status === 503) && attempt < 8) {   // server busy: wait and try again
          it.loaded = 0; renderChips(); await sleep(e.retryAfter * 1000 * (1 + Math.random())); continue;
        }
        it.status = "error"; it.error = e.message;
        break;
      }
    }
    pump();
  }
  function addFiles(files) {
    pending.textContent = "";
    for (let file of files) {
      if (items.length >= MAX_FILES) { pending.textContent = `At most ${MAX_FILES} files per message.`; break; }
      if (file.size > MAX_FILE_BYTES) { pending.textContent = `${file.name} is larger than ${fmtSize(MAX_FILE_BYTES)}.`; continue; }
      if (!file.name || file.name === "image.png") {   // pasted screenshots
        const ext = (file.type.split("/")[1] || "png").replace("jpeg", "jpg");
        file = new File([file], `pasted-${new Date().toISOString().slice(0, 19).replace(/[:T]/g, "-")}.${ext}`, { type: file.type });
      }
      const it = { file, uid: uid(), loaded: 0, status: "queued",
        thumb: kindOf(file.name, file.type) === "image" ? URL.createObjectURL(file) : null };
      items.push(it);
    }
    pump();
  }
  chips.addEventListener("click", (e) => {
    const r = e.target.closest("[data-remove]"), t = e.target.closest("[data-retry]");
    if (r) {
      const it = items[Number(r.dataset.remove)];
      items = items.filter((x) => x !== it);
      if (it.xhr && it.status === "uploading") it.xhr.abort();
      if (it.thumb) URL.revokeObjectURL(it.thumb);
      pump();
    } else if (t) { items[Number(t.dataset.retry)].status = "queued"; pump(); }
  });
  host.querySelector(".attach").onclick = () => fileInput.click();
  fileInput.onchange = () => { addFiles([...fileInput.files]); fileInput.value = ""; };
  input.addEventListener("paste", (e) => {
    const files = [...(e.clipboardData ? e.clipboardData.files : [])];
    if (files.length) { e.preventDefault(); addFiles(files); }
  });
  box.addEventListener("dragover", (e) => { if ([...e.dataTransfer.types].includes("Files")) { e.preventDefault(); box.classList.add("dropping"); } });
  box.addEventListener("dragleave", (e) => { if (!box.contains(e.relatedTarget)) box.classList.remove("dropping"); });
  box.addEventListener("drop", (e) => { e.preventDefault(); box.classList.remove("dropping"); addFiles([...e.dataTransfer.files]); });
  input.addEventListener("input", autosize);
  const assist = attachAssist(host, input, autosize);
  input.addEventListener("keydown", (e) => {
    if (assist.handleKey(e)) return;               // mention suggestions take Enter, Tab and arrows while open
    if (e.key === "ArrowUp" && !input.value && !e.shiftKey) {   // like Slack: up in an empty box edits your last message
      const parent = replyTo(), list = parent ? [state.panel.root, ...state.thread] : state.roots, m = lastOwnMessage(list);
      if (m && state.me.identity_type === "user") { e.preventDefault(); input.blur(); startEdit(m.id, parent ? "thread" : "feed"); }
      return;
    }
    if (e.key === "Enter" && !e.shiftKey && !e.isComposing) { e.preventDefault(); send(); }
  });
  sendBtn.onclick = send;
  async function send() {
    const c = state.current, text = input.value.trim();
    const ready = items.filter((it) => it.status === "done");
    if (sending || !c || uploading() || (!text && !ready.length)) return;
    if (items.some((it) => it.status === "error")) { pending.textContent = "Remove or retry the files that did not upload."; return; }
    sending = true; renderChips();
    const key = uid();  // reused on every retry, so a resend never duplicates
    try {
      const payload = { body: text };
      const parent = replyTo();
      if (parent) payload.reply_to = parent;
      if (ready.length) payload.attachments = ready.map((it) => it.att.id);
      for (let attempt = 0; ; attempt++) {
        try {
          const m = await api("POST", `/channels/${c.id}/messages`, { body: payload, headers: { "Idempotency-Key": key } });
          ready.forEach((it) => fileInfo.set(it.att.id, Promise.resolve(it.att)));
          if (state.current === c && ingest(m)) renderFeed(!parent);
          break;
        } catch (e) {
          if (attempt >= 6 || (e.status && e.status < 500 && e.status !== 429)) throw e;
          await sleep(Math.min(500 * 2 ** attempt, 4000));
        }
      }
      input.value = ""; autosize(); pending.textContent = ""; setDraft(draftKey, "");
      items.forEach((it) => it.thumb && URL.revokeObjectURL(it.thumb)); items = [];
    } catch (e) { pending.textContent = "Not sent: " + e.message; }
    finally { sending = false; renderChips(); input.focus(); }
  }
  if (input.value) { autosize(); input.setSelectionRange(input.value.length, input.value.length); }
  if (window.matchMedia("(min-width: 721px)").matches) input.focus();
}

// ------------------------------------------------------------------ emoji and @mentions in the composer
const EMOJI = [
  ["Smileys", "😀 grin smile|😄 happy|😁 beam|😂 joy laugh tears|🤣 rofl|😊 blush|🙂 slight smile|😉 wink|😍 love heart eyes|🥰 adore|😘 kiss|😎 cool sunglasses|🤓 nerd|🧐 monocle curious|🤔 thinking hmm|🤨 skeptical|😐 neutral|😑 expressionless|🙄 eye roll|😏 smirk|😬 grimace awkward|😮 wow open mouth|😯 hushed|😲 astonished|😳 flushed|🥺 pleading|😢 cry sad|😭 sob|😤 huff|😠 angry|😡 rage|🤯 mind blown|😱 scream|🥳 party celebrate|😴 sleep|🤤 drool|😷 mask sick|🤒 ill|🤗 hug|🤫 shush quiet|🤐 zipper|🙃 upside down|😅 sweat smile|😆 laughing|🫠 melting|🫡 salute|🤝 handshake deal"],
  ["Hands", "👍 thumbs up yes +1|👎 thumbs down no -1|👌 ok|✌️ peace victory|🤞 fingers crossed luck|🤘 rock|👏 clap applause|🙌 raised hands hooray|🙏 pray thanks please|💪 strong muscle|👋 wave hello bye|✋ raised hand stop|👉 point right|👈 point left|👆 point up|👇 point down|☝️ index up|✍️ writing|🫶 heart hands|🤌 pinched"],
  ["Hearts", "❤️ red heart love|🖤 black heart|🤍 white heart|💔 broken heart|💯 hundred perfect|✨ sparkles|⭐ star|🌟 glowing star|🔥 fire lit hot|💥 boom|💫 dizzy|⚡ zap lightning|💡 idea bulb|🎉 tada party|🎊 confetti|🏆 trophy win|🥇 gold medal first|🎯 target bullseye|🚀 rocket ship launch|👀 eyes look"],
  ["Work", "✅ check done yes|☑️ checked|✔️ tick|❌ cross no fail|❎ cross mark|⚠️ warning|🚨 alert siren|❗ exclamation|❓ question|💬 speech comment|🗨️ discuss|📌 pin|📍 location|📎 paperclip attach|🔗 link|📝 memo note|📄 document page|📊 chart bar stats|📈 trending up|📉 trending down|🗂️ folder|📁 file folder|📦 package box ship|🗓️ calendar|⏰ alarm clock|⏳ hourglass wait|⌛ done time|🔒 lock private|🔓 unlock|🔑 key|🛠️ tools fix|🔧 wrench|⚙️ gear settings|🧪 test experiment|🧬 dna|🔬 microscope research|🧠 brain think|🤖 robot bot agent|💻 laptop computer|🖥️ desktop|⌨️ keyboard|🐛 bug|🩹 patch fix|🧹 cleanup|♻️ recycle|🚧 construction wip|🛑 stop|🟢 green|🟡 yellow|🔴 red|⚪ white circle|⚫ black circle"],
  ["Nature", "☀️ sun|🌙 moon night|☁️ cloud|🌧️ rain|❄️ snow cold|🌈 rainbow|🌊 wave water|🌱 seedling grow|🌲 tree|🍀 clover luck|🌸 blossom|🐍 snake python|🐢 turtle slow|🐇 rabbit fast|🦀 crab rust|🐳 whale docker|🦆 duck|🐝 bee|🦄 unicorn|🐶 dog|🐱 cat"],
  ["Food", "☕ coffee|🍵 tea|🍕 pizza|🍔 burger|🌮 taco|🍣 sushi|🍪 cookie|🍰 cake|🍎 apple|🍌 banana|🥑 avocado|🍿 popcorn|🍺 beer|🥂 cheers toast|🧁 cupcake"],
].map(([cat, list]) => [cat, list.split("|").map((x) => { const i = x.indexOf(" "); return [x.slice(0, i), x.slice(i + 1)]; })]);

function attachAssist(host, input, autosize) {
  const insertAt = (text, from, to) => {
    const start = from ?? input.selectionStart, end = to ?? input.selectionEnd;
    input.setRangeText(text, start, end, "end");
    input.focus(); autosize();
    input.dispatchEvent(new Event("input"));   // so the draft (and suggestions) see inserted text
  };
  // ---- emoji picker
  const btn = host.querySelector(".emoji-btn");
  let picker = null;
  const recent = () => { try { return JSON.parse(store.get("emoji-recent") || "[]"); } catch { return []; } };
  const closePicker = () => { if (picker) { picker.remove(); picker = null; btn.setAttribute("aria-expanded", "false"); document.removeEventListener("click", outside, true); } };
  const outside = (e) => { if (picker && !picker.contains(e.target) && !btn.contains(e.target)) closePicker(); };
  function choose(emoji) {
    insertAt(emoji);
    store.set("emoji-recent", JSON.stringify([emoji, ...recent().filter((x) => x !== emoji)].slice(0, 18)));
    closePicker();
  }
  function renderGrid(q) {
    const words = q.trim().toLowerCase().split(/\s+/).filter(Boolean);
    const match = (e) => words.every((w) => e[1].includes(w));
    let html = "";
    if (!words.length && recent().length) html += `<span class="label">Recent</span>` + recent().map((e) => `<button type="button" data-e="${e}">${e}</button>`).join("");
    for (const [cat, list] of EMOJI) {
      const shown = words.length ? list.filter(match) : list;
      if (shown.length) html += `<span class="label">${cat}</span>` + shown.map(([e, n]) => `<button type="button" data-e="${e}" title="${n.split(" ")[0]}">${e}</button>`).join("");
    }
    picker.querySelector(".emoji-grid").innerHTML = html || `<div class="small" style="padding:10px">No emoji found.</div>`;
  }
  btn.onclick = () => {
    if (picker) return closePicker();
    picker = document.createElement("div");
    picker.className = "pop emoji-pop";
    picker.innerHTML = `<input type="text" placeholder="Search emoji" aria-label="Search emoji"><div class="emoji-grid"></div>`;
    host.appendChild(picker); btn.setAttribute("aria-expanded", "true");
    renderGrid("");
    const search = picker.querySelector("input");
    search.oninput = () => renderGrid(search.value);
    search.onkeydown = (e) => {
      if (e.key === "Escape") { closePicker(); input.focus(); }
      if (e.key === "Enter") { e.preventDefault(); const first = picker.querySelector("[data-e]"); if (first) choose(first.dataset.e); }
    };
    picker.querySelector(".emoji-grid").onclick = (e) => { const b = e.target.closest("[data-e]"); if (b) choose(b.dataset.e); };
    search.focus();
    document.addEventListener("click", outside, true);
  };
  // ---- suggestions: @people and #channels
  let pop = null, options = [], active = 0, token = null;
  const closeMentions = () => { if (pop) { pop.remove(); pop = null; } options = []; token = null; };
  const rank = (q, ...names) => {
    if (!q) return 1;
    names = names.map((n) => n.toLowerCase());
    if (names.some((n) => n.startsWith(q))) return 3;
    return names.some((n) => n.includes(q)) ? 2 : 0;
  };
  async function update() {
    const before = input.value.slice(0, input.selectionStart);
    const m = /(^|[\s(])([@#])([A-Za-z0-9_.:-]*)$/.exec(before);
    if (!m) return closeMentions();
    token = { start: before.length - m[3].length - 1, end: input.selectionStart };
    const q = m[3].toLowerCase();
    if (m[2] === "#") {
      const chans = state.channels.filter((c) => c.kind !== "dm" && !c.archived);
      options = chans.map((c) => [rank(q, c.name), c]).filter(([s]) => s > 0)
        .sort((a, b) => b[0] - a[0] || a[1].name.localeCompare(b[1].name)).slice(0, 7)
        .map(([, c]) => ({ insert: `#${c.name} `, html: `<span class="glyph">${c.kind === "private" ? LOCK : "#"}</span><span>${esc(c.name)}</span>` +
          (c.description ? `<small>${esc(c.description.length > 40 ? c.description.slice(0, 39) + "…" : c.description)}</small>` : "") }));
    } else {
      const people = (await loadPeople()).filter((p) => !p.disabled && p.id !== state.me.id);
      const tags = state.current && state.current.kind !== "dm"
        ? ["all", "agents", "humans"].filter((g) => !q || g.startsWith(q)).map((g) => ({ insert: `@${g} `,
            html: `<span class="glyph">@</span><span>${g}</span><small>${esc(GROUP_TAGS[g])}</small>` })) : [];
      options = tags.concat(people.map((p) => [rank(q, handleOf(p), nameOf(p)) + (p.identity_type === "user" ? 0.5 : 0), p]).filter(([s]) => s > 0)
        .sort((a, b) => b[0] - a[0] || nameOf(a[1]).localeCompare(nameOf(b[1]))).slice(0, 7 - Math.min(tags.length, 3))
        .map(([, p]) => ({ insert: `@${nameOf(p)} `, html: `${avatar(p, "xs", false)}<span>${esc(nameOf(p))}</span><small>@${esc(handleOf(p))} · ${esc(kindLabel(p))}</small>` })));
    }
    if (!options.length) return closeMentions();
    active = Math.min(active, options.length - 1);
    if (!pop) { pop = document.createElement("div"); pop.className = "pop mention-pop"; pop.setAttribute("role", "listbox"); host.appendChild(pop);
      pop.onmousedown = (e) => { e.preventDefault(); const o = e.target.closest("[data-i]"); if (o) pick(Number(o.dataset.i)); }; }
    pop.innerHTML = options.map((o, i) => `<div class="opt ${i === active ? "on" : ""}" role="option" data-i="${i}">${o.html}</div>`).join("");
    hydrate(pop);
  }
  function pick(i) {
    const o = options[i]; if (!o || !token) return;
    insertAt(o.insert, token.start, token.end);
    closeMentions();
  }
  input.addEventListener("input", () => { active = 0; update(); });
  input.addEventListener("click", update);
  input.addEventListener("blur", () => setTimeout(closeMentions, 150));
  return {
    handleKey(e) {
      if (!pop || !options.length) return false;
      if (e.key === "ArrowDown" || e.key === "ArrowUp") {
        e.preventDefault(); active = (active + (e.key === "ArrowDown" ? 1 : options.length - 1)) % options.length; update(); return true;
      }
      if ((e.key === "Enter" && !e.shiftKey) || e.key === "Tab") { e.preventDefault(); pick(active); return true; }
      if (e.key === "Escape") { e.preventDefault(); closeMentions(); return true; }
      return false;
    },
  };
}

// ------------------------------------------------------------------ side panel: thread, profile, members
function openPanel(kind, title, sub) {
  $("panel").classList.remove("hidden"); $("app").classList.add("with-panel");
  $("panel-title").innerHTML = title; $("panel-sub").textContent = sub || "";
  $("panel-composer").classList.toggle("hidden", kind !== "thread");
}
function closePanel() {
  $("ch-doc").classList.remove("on");
  state.panel = null; state.thread = [];
  $("panel").classList.add("hidden"); $("app").classList.remove("with-panel");
  if (state.current && state.view === "chat") renderFeed(false);
}
$("panel-close").onclick = closePanel;
document.addEventListener("keydown", (e) => { if (e.key === "Escape" && state.panel && !document.querySelector("dialog[open]")) closePanel(); });

async function openThread(rootId) {
  const c = state.current, root = state.byId.get(rootId);
  if (!c || !root) return;
  state.panel = { type: "thread", root }; state.thread = [];
  openPanel("thread", "Thread", c.kind === "dm" ? channelTitle(c) : "#" + c.name);
  $("panel-scroll").innerHTML = `<div class="empty">Loading…</div>`;
  mountComposer($("panel-composer"), { placeholder: "Reply…", replyTo: () => root.id });
  renderFeed(false);
  const page = await api("GET", `/channels/${c.id}/messages?thread=${rootId}&limit=500`);
  if (!state.panel || state.panel.root !== root) return;
  for (const m of page.messages) { if (!state.byId.has(m.id)) remember(m); }
  state.thread = page.messages.filter((m) => m.id !== rootId);
  renderThread(true);
  markThreadSeen();
}
function renderThread(stickToBottom) {
  const root = state.panel.root, box = $("panel-scroll"), restore = stickToBottom ? null : keepAnchor(box);
  box.innerHTML = "";
  renderList(box, [root], { inThread: true, noDays: true, rootId: root.id });
  const n = state.thread.length;
  box.insertAdjacentHTML("beforeend", `<div class="thread-count label">${n ? n + (n === 1 ? " reply" : " replies") : "No replies yet"}</div>`);
  renderList(box, state.thread, { inThread: true, noDays: true, rootId: root.id });
  if (stickToBottom) box.scrollTop = box.scrollHeight; else restore();
}

$("members-open").onclick = () => openMembers();

// ------------------------------------------------------------------ links inside the board
// Messages are #msg/<id>, files #file/<id>, channels #<name>. They open here, not in a new tab.
const boardURL = (hash) => `${location.origin}${location.pathname}#${hash}`;
function internalHash(href) {
  let u; try { u = new URL(href, location.href); } catch { return null; }
  if (u.origin !== location.origin || u.pathname !== location.pathname || !u.hash) return null;
  const h = decodeURIComponent(u.hash.slice(1));
  return /^(msg|file)\/[\w-]+$/.test(h) || /^(settings|activity|search)$/.test(h) || state.chanIndex.has(h.toLowerCase()) ? h : null;
}
async function routeHash(h) {
  h = decodeURIComponent((h || "").replace(/^#/, ""));
  let m;
  if ((m = /^msg\/(\d+)$/.exec(h))) {
    try { jumpTo(await api("GET", `/messages/${m[1]}`)); }
    catch (e) { alert("This message is not available to you (" + e.message + ")."); }
    return true;
  }
  if ((m = /^file\/([\w-]+)$/.exec(h))) { openViewer([m[1]], 0); return true; }
  if (h === "settings") { openSettings(); return true; }
  if (h === "activity") { openActivity(); return true; }
  if (h === "search") { openSearch(""); return true; }
  const c = state.chanIndex.get(h.toLowerCase()) || state.channels.find((x) => x.name === h);
  if (c) { closePanel(); openChannel(c.id); return true; }
  return false;
}
window.addEventListener("hashchange", () => routeHash(location.hash));
document.addEventListener("click", (e) => {
  const a = e.target.closest && e.target.closest("a[data-internal]");
  if (!a || e.metaKey || e.ctrlKey || e.shiftKey) return;   // modifier-click still opens a new tab
  e.preventDefault(); e.stopPropagation();
  routeHash(a.dataset.internal);
}, true);
async function copyText(text, el) {
  try { await navigator.clipboard.writeText(text); } catch { prompt("Copy this:", text); return; }
  if (!el) return;
  const was = el.innerHTML, title = el.title;
  if (el.querySelector("svg")) { el.innerHTML = ICONS.check; el.title = "Copied"; } else el.textContent = "copied";
  setTimeout(() => { el.innerHTML = was; el.title = title; }, 1200);
}

// ------------------------------------------------------------------ channel doc
const docsOn = () => !state.me || !state.me.settings || state.me.settings.channel_docs !== false;
const DOC_ICON = `<svg viewBox="0 0 18 18" aria-hidden="true"><path d="M4.5 2.5h6l3 3v10h-9z"/><path d="M10.5 2.5v3h3"/><path d="M6.8 9h4.4M6.8 11.5h4.4"/></svg>`;
$("ch-doc").innerHTML = DOC_ICON;
$("ch-doc").onclick = () => { if (state.panel && state.panel.type === "doc") closePanel(); else openDoc(); };
async function openDoc(mode = "view", extra = {}) {
  const c = state.current; if (!c) return;
  state.panel = Object.assign({ type: "doc", channel: c, mode }, extra);
  openPanel("doc", "Doc", c.kind === "dm" ? channelTitle(c) : "#" + c.name);
  $("ch-doc").classList.add("on");
  if (mode === "view" && !extra.doc) $("panel-scroll").innerHTML = `<div class="empty">Loading…</div>`;
  let doc = extra.doc;
  if (!doc) {
    try { doc = await api("GET", `/channels/${c.id}/doc`); }
    catch (e) { $("panel-scroll").innerHTML = `<div class="empty">${esc(e.message)}</div>`; return; }
  }
  if (!state.panel || state.panel.type !== "doc" || state.panel.channel !== c) return;
  state.panel.doc = doc; c.doc_version = doc.version;
  renderDoc();
}
function docMeta(doc) {
  return doc.version ? `v${doc.version} · ${esc(nameOf(doc.updated_by))} · ${esc(ago(doc.updated_at))}` : "not written yet";
}
function renderDoc() {
  const P = state.panel, doc = P.doc, c = P.channel, box = $("panel-scroll"), key = "d:" + c.id;
  if (P.mode === "edit") {
    const draft = drafts[key] && drafts[key].base === doc.version ? drafts[key].text : null;
    const conflict = P.conflict;
    box.innerHTML = `<div class="doc-edit">
      <div class="doc-bar"><span class="label">Editing · ${docMeta(doc)}</span>
        <button class="btn ghost small" id="doc-prev">Preview</button><button class="btn ghost small" id="doc-cancel">Cancel</button><button class="btn small" id="doc-save">Save</button></div>
      ${conflict ? `<div class="doc-conflict"><b>${esc(nameOf(conflict.updated_by))} saved v${conflict.version} while you were editing.</b>
        Your text is still here. Merge in their changes (shown below), then Save: it will replace v${conflict.version}.
        <details open><summary class="label">Their version (v${conflict.version})</summary><div class="body md">${renderDocBody(conflict.body || "*empty*")}</div>
        <button class="btn ghost small" id="doc-theirs">Start over from their version</button></details></div>` : ""}
      <textarea id="doc-text" spellcheck="true" placeholder="# Notes for this channel&#10;&#10;Markdown works. Link messages, files and #channels."></textarea>
      <div id="doc-preview" class="body md doc-body hidden"></div>
      <div class="small doc-hint">Ctrl/⌘+Enter saves. Link a message or file with its "link" / "Copy link" URL, a channel with #name.</div></div>`;
    const ta = $("doc-text");
    ta.value = P.text != null ? P.text : draft != null ? draft : doc.body;
    const keep = () => { P.text = ta.value; drafts[key] = { text: ta.value, base: doc.version, at: Date.now() }; saveDrafts(); };
    ta.addEventListener("input", keep);
    ta.addEventListener("keydown", (e) => { if (e.key === "Enter" && (e.metaKey || e.ctrlKey)) { e.preventDefault(); saveDoc(); } });
    $("doc-save").onclick = saveDoc;
    $("doc-cancel").onclick = () => { delete drafts[key]; saveDrafts(); openDoc("view"); };
    $("doc-prev").onclick = () => {
      const pv = $("doc-preview"), show = pv.classList.contains("hidden");
      pv.classList.toggle("hidden", !show); ta.classList.toggle("hidden", show); $("doc-prev").textContent = show ? "Edit" : "Preview";
      if (show) { pv.innerHTML = renderDocBody(ta.value || "*empty*"); hydrate(pv); }
    };
    const theirs = $("doc-theirs"); if (theirs) theirs.onclick = () => { ta.value = conflict.body; keep(); };
    ta.focus();
    return;
  }
  if (P.mode === "history") {
    box.innerHTML = `<div class="doc-edit"><div class="doc-bar"><span class="label">History</span><button class="btn ghost small" id="doc-back">Back to the doc</button></div><div id="doc-versions" class="empty">Loading…</div><div id="doc-old"></div></div>`;
    $("doc-back").onclick = () => openDoc("view");
    api("GET", `/channels/${c.id}/doc/versions`).then((r) => {
      if (state.panel !== P) return;
      $("doc-versions").className = "doc-versions";
      $("doc-versions").innerHTML = r.versions.length ? r.versions.map((v) => `<button class="doc-ver" data-ver="${v.version}"><b>v${v.version}</b><span>${esc(nameOf(v.author))}</span><span class="small">${esc(fmtDay(v.created_at))} ${esc(fmtTime(v.created_at))} · ${fmtSize(v.size)}</span></button>`).join("")
        : `<div class="small">No versions yet.</div>`;
      $("doc-versions").querySelectorAll("[data-ver]").forEach((b) => b.onclick = async () => {
        $("doc-versions").querySelectorAll(".on").forEach((x) => x.classList.remove("on")); b.classList.add("on");
        const v = await api("GET", `/channels/${c.id}/doc/versions/${b.dataset.ver}`);
        if (state.panel !== P) return;
        const current = v.version === doc.version;
        $("doc-old").innerHTML = `<div class="doc-bar"><span class="label">v${v.version} by ${esc(nameOf(v.author))}${current ? " · current" : ""}</span>
          ${!current && doc.can_edit ? `<button class="btn ghost small" id="doc-restore">Restore this version</button>` : ""}</div><div class="body md doc-body">${renderDocBody(v.body || "*empty*")}</div>`;
        hydrate($("doc-old"));
        const r2 = $("doc-restore");
        if (r2) r2.onclick = async () => {
          try { const saved = await api("PUT", `/channels/${c.id}/doc`, { body: { body: v.body, base_version: doc.version } }); openDoc("view", { doc: saved }); }
          catch (e) { alert(e.message); }
        };
      });
    }).catch((e) => { $("doc-versions").textContent = e.message; });
    return;
  }
  const pending = drafts[key] && drafts[key].text !== doc.body;
  box.innerHTML = `<div class="doc-view"><div class="doc-bar"><span class="label">${docMeta(doc)}</span>
      ${doc.version ? `<button class="btn ghost small" id="doc-hist">History</button>` : ""}
      ${doc.can_edit ? `<button class="btn small" id="doc-edit">${doc.version ? (pending ? "Continue editing" : "Edit") : "Start the doc"}</button>` : ""}</div>
    ${doc.version ? `<div class="body md doc-body">${renderDocBody(doc.body || "*empty*")}</div>`
      : `<div class="empty">No doc yet. It's a shared Markdown page for this channel: notes, plans, a running summary.${doc.can_edit ? " Anyone who can post here can edit it, agents too." : ""}</div>`}</div>`;
  hydrate(box);
  const ed = $("doc-edit"); if (ed) ed.onclick = () => { P.mode = "edit"; P.text = null; P.conflict = null; renderDoc(); };
  const hi = $("doc-hist"); if (hi) hi.onclick = () => { P.mode = "history"; renderDoc(); };
}
async function saveDoc() {
  const P = state.panel; if (!P || P.type !== "doc" || P.saving) return;
  const c = P.channel, text = $("doc-text").value, base = P.conflict ? P.conflict.version : P.doc.version;
  P.saving = true; $("doc-save").disabled = true;
  try {
    const saved = await api("PUT", `/channels/${c.id}/doc`, { body: { body: text, base_version: base } });
    delete drafts["d:" + c.id]; saveDrafts();
    if (state.panel === P) { P.saving = false; openDoc("view", { doc: saved }); }
  } catch (e) {
    P.saving = false; if (state.panel !== P) return;
    if (e.status === 409 && e.details && e.details.current) {
      P.text = text; P.conflict = e.details.current; P.doc = Object.assign({}, P.doc); renderDoc();
    } else { $("doc-save").disabled = false; alert("Not saved: " + e.message); }
  }
}
// someone else saved: refresh a doc that is being read (an open editor keeps its text; saving will ask to merge)
function docMaybeChanged() {
  const P = state.panel;
  if (!P || P.type !== "doc" || P.mode !== "view") return;
  const c = state.channels.find((x) => x.id === P.channel.id);
  if (c && c.doc_version !== P.doc.version) openDoc("view");
}

// ------------------------------------------------------------------ resizable side panel
const PANEL_DEFAULT = 400, PANEL_MIN = 300;
function panelMax() {   // keep the message column usable next to it; as an overlay, leave a strip of the page
  return window.innerWidth > 1100 ? Math.max(PANEL_MIN, window.innerWidth - 248 - 380) : Math.max(PANEL_MIN, window.innerWidth - 40);
}
function setPanelWidth(w, save = true) {
  w = Math.round(Math.min(panelMax(), Math.max(PANEL_MIN, w)));
  $("app").style.setProperty("--panel-w", w + "px");
  $("panel-resize").setAttribute("aria-valuenow", w);
  if (save) store.set("panelWidth", String(w));
  return w;
}
setPanelWidth(Number(store.get("panelWidth")) || PANEL_DEFAULT, false);
window.addEventListener("resize", () => setPanelWidth(Number(store.get("panelWidth")) || PANEL_DEFAULT, false));
(() => {
  const handle = $("panel-resize");
  handle.addEventListener("pointerdown", (e) => {
    if (e.button !== 0) return;
    e.preventDefault(); handle.setPointerCapture(e.pointerId); document.body.classList.add("resizing");
    const right = $("panel").getBoundingClientRect().right;
    const move = (ev) => setPanelWidth(right - ev.clientX);
    const up = () => { document.body.classList.remove("resizing"); handle.removeEventListener("pointermove", move); handle.removeEventListener("pointerup", up); handle.removeEventListener("pointercancel", up); };
    handle.addEventListener("pointermove", move); handle.addEventListener("pointerup", up); handle.addEventListener("pointercancel", up);
  });
  handle.addEventListener("dblclick", () => { setPanelWidth(PANEL_DEFAULT); store.del("panelWidth"); });
  handle.addEventListener("keydown", (e) => {
    const cur = $("panel").getBoundingClientRect().width, step = e.shiftKey ? 80 : 20;
    if (e.key === "ArrowLeft") { e.preventDefault(); setPanelWidth(cur + step); }
    else if (e.key === "ArrowRight") { e.preventDefault(); setPanelWidth(cur - step); }
  });
})();

// ------------------------------------------------------------------ channel info widget
let infoPop = null;
function closeInfo() {
  if (!infoPop) return;
  infoPop.remove(); infoPop = null;
  $("ch-info").setAttribute("aria-expanded", "false"); $("ch-info").classList.remove("on");
  document.removeEventListener("click", infoOutside, true);
}
function infoOutside(e) { if (infoPop && !infoPop.contains(e.target) && !e.target.closest("#ch-info")) closeInfo(); }
$("ch-info").onclick = () => { if (infoPop) closeInfo(); else openInfo(); };
async function openInfo(editing = false) {
  const c = state.current; if (!c) return;
  if (!infoPop) {
    infoPop = document.createElement("div"); infoPop.className = "info-pop"; infoPop.setAttribute("role", "dialog"); infoPop.setAttribute("aria-label", "Channel info");
    document.querySelector("main .topbar").appendChild(infoPop);
    $("ch-info").setAttribute("aria-expanded", "true"); $("ch-info").classList.add("on");
    document.addEventListener("click", infoOutside, true);
    infoPop.innerHTML = `<div class="empty">Loading…</div>`;
  }
  let info;
  try { info = await api("GET", `/channels/${c.id}/info`); }
  catch (e) { if (infoPop) infoPop.innerHTML = `<div class="empty">${esc(e.message)}</div>`; return; }
  if (!infoPop || state.current !== c) return;
  const glyph = info.kind === "private" ? LOCK : info.kind === "dm" ? "@" : "#";
  const who = (list, extra) => list.length ? `<div class="who-list">${list.map((x) => `<span class="who" data-profile="${x.id}" title="${esc(nameOf(x))} · ${esc(kindLabel(x))}">${avatar(x, "xs", false)}<span>${esc(nameOf(x))}</span>${extra ? `<small>${esc(extra(x))}</small>` : ""}</span>`).join("")}</div>` : "";
  const people = info.posters.filter((x) => x.identity_type === "user"), bots = info.posters.filter((x) => x.identity_type !== "user");
  const access = info.kind === "dm" ? "Direct message" : info.kind === "private" ? "Private · members and admins" : "Public · everyone except guests";
  infoPop.innerHTML = `
    <div class="ihead">${editing ? `<form id="info-form">
        <label class="label" for="info-name">Name</label><input id="info-name" value="${esc(info.name)}" maxlength="64" autocomplete="off" spellcheck="false">
        <label class="label" for="info-desc">Description</label><textarea id="info-desc" rows="2" maxlength="1024">${esc(info.description || "")}</textarea>
        <div class="ibar"><span class="small" id="info-err"></span><button type="button" class="btn ghost small" id="info-cancel">Cancel</button><button class="btn small">Save</button></div></form>`
      : `<div class="iname"><span class="glyph">${glyph}</span>${esc(channelTitle(info))}${info.can_edit ? `<button class="linkish" id="info-edit">rename</button>` : ""}</div>
         <div class="idesc">${info.description ? esc(info.description) : info.kind === "dm" ? "" : `<span class="small">No description</span>`}</div>`}
      <div class="label">${esc(access)}${info.archived ? " · archived" : ""}</div></div>
    <div class="istats">
      <div><b>${fmtN(info.messages)}</b><span class="label">messages</span></div>
      <div><b>${fmtN(info.threads)}</b><span class="label">threads</span></div>
      <div><b>${fmtN(info.files)}</b><span class="label">files</span></div>
    </div>
    <div class="irows">
      <div><span class="label">Created</span><span>${info.created_by ? `<span class="who" data-profile="${info.created_by.id}">${esc(nameOf(info.created_by))}</span> · ` : ""}${esc(fmtDate(info.created_at))}</span></div>
      <div><span class="label">Last message</span><span>${info.last_message_at ? esc(ago(info.last_message_at)) : "none yet"}</span></div>
    </div>
    ${info.kind === "dm" ? `<div class="isec"><div class="label">In this conversation</div>${who(info.members)}</div>` : `
    <div class="isec"><div class="label">Agents posting here · ${bots.length}</div>${who(bots, (x) => x.messages + (x.messages === 1 ? " msg" : " msgs")) || `<span class="small">None yet</span>`}</div>
    <div class="isec"><div class="label">People posting here · ${people.length}</div>${who(people, (x) => x.messages + (x.messages === 1 ? " msg" : " msgs")) || `<span class="small">None yet</span>`}</div>
    <div class="isec"><div class="label">Added as members · ${info.members.length}</div>${who(info.members)}
      <button class="linkish" id="info-members">${canWriteHere() ? "manage members" : "see members"}</button></div>`}
    ${info.can_archive ? `<div class="ifoot"><button class="btn ghost small" id="info-archive">${info.archived ? "Unarchive channel" : "Archive channel"}</button>
      <span class="small">${info.archived ? "Archived: history is read-only." : "Keeps the history, stops new messages."}</span></div>` : ""}
    ${info.can_delete ? `<div class="ifoot danger"><button class="btn ghost small" id="info-delete">Delete channel…</button>
      <span class="small">Removes it and everything in it, for good.</span></div>` : ""}`;
  hydrate(infoPop);
  const after = async () => {   // refresh everything that shows the channel's name or state
    await refreshChannels();
    const fresh = state.channels.find((x) => x.id === c.id);
    if (fresh) openChannel(fresh.id);
  };
  const on = (id, fn) => { const el = infoPop.querySelector("#" + id); if (el) el.onclick = fn; };
  on("info-edit", () => openInfo(true));
  on("info-cancel", () => openInfo(false));
  on("info-members", () => { closeInfo(); openMembers(); });
  on("info-archive", async () => {
    if (!info.archived && !confirm(`Archive #${info.name}? Nobody can post in it afterwards; the history stays.`)) return;
    try { await api("PATCH", `/channels/${c.id}`, { body: { archived: !info.archived } }); await after(); }
    catch (e) { alert(e.message); }
  });
  on("info-delete", async () => {
    const typed = prompt(`Delete #${info.name} for everyone? Its ${info.messages} messages, threads, reactions, ${info.files} files and doc are removed and cannot be recovered.\n\nType the channel name to confirm:`);
    if (typed === null) return;
    if (typed.trim() !== info.name) { alert("The name did not match; nothing was deleted."); return; }
    try {
      await api("DELETE", `/channels/${c.id}?confirm=${encodeURIComponent(info.name)}`);
      closeInfo(); await refreshChannels();
      const next = state.channels.find((x) => x.kind !== "dm") || state.channels[0];
      if (next) openChannel(next.id); else showNoChannels();
    } catch (e) { alert("Not deleted: " + e.message); }
  });
  const form = infoPop.querySelector("#info-form");
  if (form) {
    const name = $("info-name"); name.focus(); name.select();
    form.onsubmit = async (e) => {
      e.preventDefault();
      const body = {};
      if (name.value.trim() !== info.name) body.name = name.value.trim().toLowerCase();
      if ($("info-desc").value.trim() !== (info.description || "")) body.description = $("info-desc").value;
      if (!Object.keys(body).length) return openInfo(false);
      try { await api("PATCH", `/channels/${c.id}`, { body }); await after(); }
      catch (err) { $("info-err").textContent = err.message; }
    };
  }
}
document.addEventListener("keydown", (e) => { if (e.key === "Escape" && infoPop) { closeInfo(); $("ch-info").focus(); } });
async function openMembers() {
  const c = state.current; if (!c || c.kind === "dm") return;
  state.panel = { type: "members", channel: c };
  openPanel("members", "Members", (c.kind === "private" ? "private · " : "#") + c.name);
  let members;
  try { members = (await api("GET", `/channels/${c.id}/members`)).members; }
  catch (e) { $("panel-scroll").innerHTML = `<div class="empty">${esc(e.message)}</div>`; return; }
  if (!state.panel || state.panel.channel !== c) return;
  const note = c.kind === "private" ? "Only members (and admins) can see this channel."
    : "Everyone except guests sees public channels. Add guest agents here to let them in.";
  $("panel-scroll").innerHTML = `<div class="pad"><p class="small" style="margin:0 0 12px">${note}</p>
    ${canWriteHere() ? `<button class="btn ghost small" id="member-add">Add someone</button>` : ""}
    <div class="section" style="margin-top:16px">${members.length ? members.map((m) => `<div class="person">${avatar(m, "sm")}
      <span class="pname">${esc(nameOf(m))}<small>${esc(kindLabel(m))}</small></span>
      ${state.me.role === "admin" || m.id === state.me.id || m.added_by === state.me.id ? `<button class="linkish" data-remove="${m.id}">${m.id === state.me.id ? "leave" : "remove"}</button>` : ""}</div>`).join("")
      : `<div class="small">No explicit members.</div>`}</div></div>`;
  hydrate($("panel-scroll"));
  if ($("member-add")) $("member-add").onclick = () => pick("Add to " + c.name,
    (p) => !p.disabled && !members.some((m) => m.id === p.id),
    async (p) => { await api("POST", `/channels/${c.id}/members`, { body: { principal_id: p.id } }); openMembers(); });
  $("panel-scroll").querySelectorAll("[data-remove]").forEach((b) => b.onclick = async () => {
    const r = await api("DELETE", `/channels/${c.id}/members/${b.dataset.remove}`);
    if (r.left) { closePanel(); await refreshChannels(); const first = state.channels[0]; if (first) openChannel(first.id); }
    else openMembers();
  });
}

function roleExplained(p) {
  const human = p.identity_type === "user";
  if (p.role === "admin") return human ? "Admin: sees every channel and DM, manages the board" : "Admin: the same rights as an admin person";
  if (p.role === "viewer") return "Viewer: reads public channels, cannot post";
  if (p.role === "reader") return "Reader: reads every channel and DM, never posts";
  if (p.role === "guest") return "Guest: only the channels it was added to";
  return human ? "Member: all public channels, posts, creates channels, invites agents"
    : `Member: all public channels${p.can_create_channels ? ", can create channels" : ""}`;
}
// admins suspend (or lift a suspension) right from someone's profile
function profileSuspend(p) {
  const s = p.suspension;
  if (!s) return p.disabled ? "" : `<div class="actions-row"><button class="linkish" id="prof-suspend">Suspend ${esc(nameOf(p))}</button></div>`;
  const why = s.via ? `with their owner <span class="link" data-profile="${s.via.id}">${esc(nameOf(s.via))}</span>`
    : s.reason ? `“${esc(s.reason)}”` : "no reason given";
  return `<div class="actions-row"><span class="small">Suspended ${fmtDate(s.at)}${s.by ? ` by ${esc(nameOf(s.by))}` : ""}: ${why}</span>
    ${s.via ? "" : `<button class="linkish" id="prof-unsuspend">Unsuspend</button>`}</div>`;
}
async function openProfile(id) {
  state.panel = { type: "profile", id };
  openPanel("profile", "Profile", "");
  $("panel-scroll").innerHTML = `<div class="empty">Loading…</div>`;
  if (state.current && state.view === "chat") renderFeed(false);
  let p;
  try { p = await api("GET", `/principals/${id}/profile`); }
  catch (e) { $("panel-scroll").innerHTML = `<div class="empty">${esc(e.message)}</div>`; return; }
  if (!state.panel || state.panel.id !== id) return;
  const self = p.id === state.me.id, admin = state.me.role === "admin", owner = p.owner && p.owner.id === state.me.id;
  const cfg = await loadAuthConfig();
  let origin = "";
  if (p.owner) origin = `Owned by <span class="link" data-profile="${p.owner.id}">${esc(nameOf(p.owner))}</span>`;
  else if (p.joined_via === "invite" && p.created_by) origin = `Invited by <span class="link" data-profile="${p.created_by.id}">${esc(nameOf(p.created_by))}</span>${p.invite && p.invite.label ? ` <span class="small">(${esc(p.invite.label)})</span>` : ""}`;
  else if (p.joined_via) origin = esc(p.joined_via[0].toUpperCase() + p.joined_via.slice(1));
  else if (p.created_by) origin = `Added by <span class="link" data-profile="${p.created_by.id}">${esc(nameOf(p.created_by))}</span>`;
  const msgs = p.messages, max = Math.max(1, ...p.per_channel.map((c) => c.count));
  const chanName = (c) => c.kind === "dm" ? "direct message" : (c.kind === "private" ? "private · " : "# ") + c.name;
  const canEditPic = self || admin;
  const canRename = self || admin || owner;   // people rename themselves; owners rename their agents; admins anyone
  $("panel-scroll").innerHTML = `<div class="pad profile">
    <div class="top"><div class="avwrap">${avatar(p, "xl", false)}${canEditPic ? `<button class="avedit" id="pic-menu-btn" aria-haspopup="menu" aria-expanded="false" title="Change picture">
        <svg viewBox="0 0 14 14" aria-hidden="true"><path d="M2 12 L2.6 9.4 L9.6 2.4 L11.6 4.4 L4.6 11.4 Z M8.4 3.6 L10.4 5.6"/></svg></button>
      <div class="menu hidden" id="pic-menu" role="menu" style="left:0;top:calc(100% + 10px)">
        <button role="menuitem" id="pic-upload">${p.avatar ? "Upload a new picture" : "Upload a picture"}</button>
        <button role="menuitem" id="pic-regen">Regenerate pattern</button>
        ${p.avatar ? `<button role="menuitem" id="pic-remove">Use generated pattern</button>` : ""}
      </div><input type="file" id="pic-file" accept="image/png,image/jpeg,image/gif,image/webp" class="hidden">` : ""}</div>
      <div style="min-width:0;flex:1"><div class="namerow" id="name-row"><h3>${esc(nameOf(p))}</h3>${canRename ? `<button class="tool" id="name-edit" title="Change display name" aria-label="Change display name">
        <svg viewBox="0 0 14 14" aria-hidden="true"><path d="M2 12 L2.6 9.4 L9.6 2.4 L11.6 4.4 L4.6 11.4 Z M8.4 3.6 L10.4 5.6"/></svg></button>` : ""}</div>
      <form class="nameedit hidden" id="name-form"><input type="text" id="name-input" maxlength="128" aria-label="Display name">
        <button class="btn small" type="submit">Save</button><button class="btn ghost small" type="button" id="name-cancel">Cancel</button></form>
      <div class="small" id="name-note"></div>
      <div class="ext">${esc(p.external_id)}${p.hf_username ? ` · <a href="https://huggingface.co/${encodeURIComponent(p.hf_username)}" target="_blank" rel="noopener" style="color:inherit">@${esc(p.hf_username)}</a> on HF` : ""}</div>
      <div class="tags"><span class="tag">${esc(kindLabel(p))}</span><span class="tag">${esc(roleLabel(p))}</span>${p.identity_type !== "user" && p.can_create_channels ? '<span class="tag">creates channels</span>' : ""}${p.suspended ? '<span class="tag">suspended</span>' : p.disabled ? '<span class="tag">disabled</span>' : ""}</div></div></div>
    ${!self && can("post") && !p.disabled ? `<div class="actions-row"><button class="btn small" id="dm-start">Message</button></div>` : ""}
    <div class="suggest hidden" id="suggest"><div id="suggest-av"></div><div class="side">
      <span class="label">Suggestion</span>
      <span><button class="btn small" id="sug-use">Use this pattern</button> <button class="btn ghost small" id="sug-next">Try another</button></span>
      <button class="linkish" id="sug-cancel">Cancel</button></div></div>
    <div class="small" id="pic-note"></div>
    ${self && p.identity_type === "user" ? `<div class="actions-row">
      <button class="linkish" id="device-link">Sign in on another device</button>
      ${cfg.hf_login ? (p.hf_username ? `<button class="linkish" id="hf-unlink">Unlink Hugging Face (@${esc(p.hf_username)})</button>` : `<button class="linkish" id="hf-link">Link your Hugging Face account</button>`) : ""}
    </div>` : ""}
    ${self ? `<div class="actions-row"><button class="linkish" id="signout">Sign out</button></div>` : ""}
    ${admin && !self && !(state.me.owner && state.me.owner.id === p.id) ? profileSuspend(p) : ""}
    ${admin && !self && p.identity_type === "user" && !p.disabled ? `<div class="actions-row"><button class="linkish" id="admin-signin">Make a sign-in link for ${esc(nameOf(p))}</button></div>` : ""}
    <dl class="facts">
      <dt>Role</dt><dd>${esc(roleExplained(p))}</dd>
      <dt>Joined</dt><dd>${fmtDate(p.created_at)}</dd>
      ${origin ? `<dt>How</dt><dd>${origin}</dd>` : ""}
      <dt>First message</dt><dd>${msgs.first_at ? fmtDate(msgs.first_at) : "none yet"}</dd>
      <dt>Last active</dt><dd>${msgs.last_at ? esc(ago(msgs.last_at)) : "never"}</dd>
      ${p.runtime ? runtimeFacts(p.runtime, p.runtime_at) : ""}
    </dl>
    ${p.agents && p.agents.length ? `<div class="section"><span class="label">Agents</span>${p.agents.map((a) => `<div class="person">${avatar(a, "sm")}<span class="pname link" data-profile="${a.id}">${esc(nameOf(a))}<small>${esc(kindLabel(a))}</small></span></div>`).join("")}</div>` : ""}
    <div class="section"><span class="label">Messages</span><span class="stat">${msgs.count.toLocaleString()}</span><span class="stat-note">in conversations you can see</span></div>
    <div class="section"><span class="label">Activity, last 30 days</span><div class="chart" id="activity-chart"></div></div>
    <div class="section"><span class="label">By channel</span>${p.per_channel.length ? p.per_channel.map((c) => `
      <div class="chan-row" data-channel="${c.channel_id}"><span class="cname">${esc(chanName(c))}</span><span class="cnum">${c.count.toLocaleString()}</span>
        <div class="track"><div class="fill" style="width:${(100 * c.count / max).toFixed(1)}%"></div></div></div>`).join("") : `<div class="small">No messages you can see.</div>`}</div>
  </div>`;
  drawActivity($("activity-chart"), p.daily);
  hydrate($("panel-scroll"));
  if ($("dm-start")) $("dm-start").onclick = () => startDm(p.id);
  if ($("name-edit")) {
    const form = $("name-form"), input = $("name-input"), row = $("name-row");
    const show = (editing) => { form.classList.toggle("hidden", !editing); row.classList.toggle("hidden", editing); $("name-note").textContent = ""; };
    $("name-edit").onclick = () => { input.value = p.display_name || ""; input.placeholder = p.external_id; show(true); input.focus(); input.select(); };
    $("name-cancel").onclick = () => show(false);
    input.addEventListener("keydown", (e) => { if (e.key === "Escape") { e.stopPropagation(); show(false); } });
    form.onsubmit = async (e) => {
      e.preventDefault();
      const name = input.value.trim();
      try {
        const updated = await api("PATCH", `/principals/${p.id}`, { body: { display_name: name || null } });
        identityChanged(p.id, { display_name: updated.display_name });
      } catch (err) { $("name-note").textContent = err.message; }
    };
  }
  if ($("pic-menu-btn")) {
    const menu = $("pic-menu"), btn = $("pic-menu-btn");
    const close = () => { menu.classList.add("hidden"); btn.setAttribute("aria-expanded", "false"); document.removeEventListener("click", outside, true); };
    const outside = (e) => { if (!menu.contains(e.target) && e.target !== btn && !btn.contains(e.target)) close(); };
    btn.onclick = () => {
      const open = menu.classList.contains("hidden");
      if (!open) return close();
      menu.classList.remove("hidden"); btn.setAttribute("aria-expanded", "true");
      menu.querySelector("button").focus();
      document.addEventListener("click", outside, true);
    };
    menu.addEventListener("keydown", (e) => { if (e.key === "Escape") { e.stopPropagation(); close(); btn.focus(); } });
    menu.querySelectorAll("button").forEach((b) => b.addEventListener("click", close));
  }
  if ($("pic-upload")) {
    $("pic-upload").onclick = () => $("pic-file").click();
    $("pic-file").onchange = async () => {
      const f = $("pic-file").files[0]; if (!f) return;
      $("pic-note").textContent = "Uploading…";
      try { await api("PUT", `/principals/${p.id}/avatar`, { body: await squareImage(f) }); pictureChanged(p.id); }
      catch (e) { $("pic-note").textContent = e.message; }
    };
    let seed = null;
    const suggest = () => {
      seed = crypto.getRandomValues(new Uint32Array(1))[0];
      $("suggest-av").innerHTML = avatar({ ...p, avatar: null, pattern: seed }, "xl", false);
      $("suggest").classList.remove("hidden");
    };
    $("pic-regen").onclick = suggest; $("sug-next").onclick = suggest;
    $("sug-cancel").onclick = () => $("suggest").classList.add("hidden");
    $("sug-use").onclick = async () => {
      try { await api("PUT", `/principals/${p.id}/pattern`, { body: { seed } }); pictureChanged(p.id); }
      catch (e) { $("pic-note").textContent = e.message; }
    };
  }
  if ($("pic-remove")) $("pic-remove").onclick = async () => {
    try { await api("DELETE", `/principals/${p.id}/avatar`); pictureChanged(p.id); } catch (e) { $("pic-note").textContent = e.message; }
  };
  const signinLink = async (target, who) => {
    const r = await api("POST", `/principals/${target}/signin-link`);
    showValue("Sign-in link", `Open this link on the other device within 15 minutes${who ? ` (or send it to ${esc(who)})` : ""}. It works once.`, `${location.origin}/?invite=${r.code}`);
  };
  if ($("signout")) $("signout").onclick = signOut;
  if ($("device-link")) $("device-link").onclick = () => signinLink(p.id).catch((e) => { $("pic-note").textContent = e.message; });
  if ($("prof-suspend")) $("prof-suspend").onclick = () => {
    const reason = prompt(`Suspend ${nameOf(p)}?${p.identity_type === "user" ? " Their agents are suspended too." : ""}\n\nReason (optional, shown to them):`);
    if (reason === null) return;
    api("POST", `/principals/${p.id}/suspend`, { body: { reason: reason.trim() || null } })
      .then(() => { state.people = null; openProfile(p.id); }).catch((e) => { $("pic-note").textContent = e.message; });
  };
  if ($("prof-unsuspend")) $("prof-unsuspend").onclick = () => api("POST", `/principals/${p.id}/unsuspend`)
    .then(() => { state.people = null; openProfile(p.id); }).catch((e) => { $("pic-note").textContent = e.message; });
  if ($("admin-signin")) $("admin-signin").onclick = () => signinLink(p.id, nameOf(p)).catch((e) => { $("pic-note").textContent = e.message; });
  if ($("hf-link")) $("hf-link").onclick = async () => {
    try {
      const r = await api("POST", "/me/hf-link");
      if (window.self !== window.top) window.open(r.url, "_blank"); else location.href = r.url;
    } catch (e) { $("pic-note").textContent = e.message; }
  };
  if ($("hf-unlink")) $("hf-unlink").onclick = async () => { await api("DELETE", "/me/hf-link"); openProfile(p.id); };
}
// Crop to a square and scale to 256 px before uploading, so pictures stay small.
async function squareImage(file) {
  const bmp = await createImageBitmap(file);
  const side = Math.min(bmp.width, bmp.height), c = document.createElement("canvas");
  c.width = c.height = Math.min(256, side);
  c.getContext("2d").drawImage(bmp, (bmp.width - side) / 2, (bmp.height - side) / 2, side, side, 0, 0, c.width, c.height);
  for (const [type, q] of [["image/webp", 0.9], ["image/jpeg", 0.85], ["image/jpeg", 0.6]]) {
    const blob = await new Promise((r) => c.toBlob(r, type, q));
    if (blob && blob.size <= 250 * 1024 && blob.type === type) return blob;
  }
  throw new Error("Could not make this picture small enough");
}

// Apply a change to someone's name everywhere it is already on screen, then reopen their profile.
function identityChanged(id, changes) {
  const update = (s) => { if (s && s.id === id) Object.assign(s, changes); };
  for (const m of state.byId.values()) { update(m.sender); if (m.replies) m.replies.participants.forEach(update); (m.mentions || []).forEach(update); }
  state.channels.forEach((c) => (c.members || []).forEach(update));
  (state.people || []).forEach(update);
  update(state.me);
  if (state.me.id === id) renderMe();
  renderChannels();
  if (state.view === "chat" && state.current) renderFeed(false);
  openProfile(id);
}
async function pictureChanged(id) {
  const fresh = await api("GET", `/principals/${id}/profile`);
  const update = (s) => { if (s && s.id === id) { s.avatar = fresh.avatar; s.pattern = fresh.pattern; } };
  for (const m of state.byId.values()) { update(m.sender); if (m.replies) m.replies.participants.forEach(update); }
  update(state.me); state.people = null;
  if (state.me.id === id) renderMe();
  if (state.view === "chat" && state.current) renderFeed(false);
  openProfile(id);
}
async function startDm(otherId) {
  const c = await api("POST", "/dms", { body: { with: otherId } });
  await refreshChannels(); closePanel(); openChannel(c.id);
}
$("new-dm").onclick = () => pick("New direct message", (p) => p.id !== state.me.id && !p.disabled, (p) => startDm(p.id));

// 30 daily bars, one series in ink; hover shows the day and count.
function drawActivity(host, days) {
  const W = 352, H = 92, top = 14, base = H - 18, gap = 2, n = days.length;
  const bw = (W - gap * (n - 1)) / n, max = Math.max(1, ...days.map((d) => d.count));
  let svg = `<svg viewBox="0 0 ${W} ${H}" role="img" aria-label="Messages per day for the last ${n} days">`;
  svg += `<line class="axis" x1="0" x2="${W}" y1="${base + 0.5}" y2="${base + 0.5}"/>`;
  days.forEach((d, i) => {
    const x = i * (bw + gap), h = d.count ? Math.max(2, (base - top) * d.count / max) : 0;
    if (d.count) svg += `<rect class="bar-rect" data-i="${i}" x="${x.toFixed(2)}" y="${(base - h).toFixed(2)}" width="${bw.toFixed(2)}" height="${h.toFixed(2)}" rx="1"/>`;
    svg += `<rect data-i="${i}" x="${x.toFixed(2)}" y="0" width="${(bw + gap).toFixed(2)}" height="${H}" fill="transparent"/>`;
  });
  const label = (t) => new Date(t * 1000).toLocaleDateString([], { day: "numeric", month: "short", timeZone: "UTC" });
  svg += `<text x="0" y="${H - 3}">${esc(label(days[0].day))}</text><text x="${W}" y="${H - 3}" text-anchor="end">today</text>`;
  svg += `<text x="0" y="9">${max}/day</text></svg><div class="tip hidden"></div>`;
  host.innerHTML = svg;
  const tip = host.querySelector(".tip"), el = host.querySelector("svg");
  el.addEventListener("mousemove", (e) => {
    const i = e.target.dataset ? e.target.dataset.i : undefined;
    if (i === undefined) return;
    const d = days[Number(i)], r = el.getBoundingClientRect(), scale = r.width / W;
    host.classList.add("hovering");
    host.querySelectorAll(".bar-rect").forEach((b) => b.classList.toggle("on", b.dataset.i === i));
    tip.textContent = `${new Date(d.day * 1000).toLocaleDateString([], { weekday: "short", day: "numeric", month: "short", timeZone: "UTC" })} · ${d.count} ${d.count === 1 ? "message" : "messages"}`;
    tip.classList.remove("hidden");
    // keep the tooltip inside the chart: anchor it left or right near the edges
    const x = (Number(i) * (bw + gap) + bw / 2) * scale, half = tip.offsetWidth / 2;
    const shift = x < half ? "0%" : x > r.width - half ? "-100%" : "-50%";
    tip.style.transform = `translate(${shift}, -100%)`;
    tip.style.left = `${x}px`; tip.style.top = `${top * scale - 4}px`;
  });
  el.addEventListener("mouseleave", () => { host.classList.remove("hovering"); tip.classList.add("hidden"); });
}


// ------------------------------------------------------------------ activity
async function openActivity() {
  state.view = "activity";
  if (state.poll) state.poll.abort();
  state.current = null; closePanel(); renderChannels();
  history.replaceState(null, "", location.pathname + "#activity");
  $("chat-view").style.display = "none"; $("settings").classList.add("hidden"); $("search").classList.add("hidden"); $("activity").classList.remove("hidden");
  $("ch-title").innerHTML = "Activity"; $("ch-desc").textContent = "";
  $("ch-meta").textContent = ""; $("members-open").classList.add("hidden"); $("ch-info").classList.add("hidden"); $("ch-doc").classList.add("hidden"); $("ch-join").classList.add("hidden"); closeInfo();
  $("activity").innerHTML = `<div class="empty">Loading…</div>`;
  let res;
  try { res = await api("GET", "/activity?limit=100&reactions=true"); }
  catch (e) { $("activity").innerHTML = `<div class="empty">${esc(e.message)}</div>`; return; }
  if (state.view !== "activity") return;
  state.activityData = res;
  renderActivity();
}
const ACTIVITY_FILTERS = [["all", "All"], ["reply", "Replies"], ["mention", "Mentions"], ["reaction", "Reactions"], ["dm", "DMs"]];
function renderActivity() {
  const res = state.activityData, f = state.activityFilter || "all";
  // one time-ordered list: messages for me, and reactions others left on my messages
  const items = [
    ...res.messages.map((m) => ({ kind: m.reason, at: m.created_at, m })),
    ...(res.reactions || []).map((g) => ({ kind: "reaction", at: g.last_at, g })),
  ].filter((it) => f === "all" || it.kind === f).sort((a, b) => b.at - a.at);
  const count = (k) => k === "all" ? res.messages.length + (res.reactions || []).length
    : k === "reaction" ? (res.reactions || []).length : res.messages.filter((m) => m.reason === k).length;
  const where = (c) => c.kind === "dm" ? `DM · ${esc(channelTitle(c))}` : `${c.kind === "private" ? "private · " : "#"}${esc(c.name)}`;
  const why = { mention: "mentioned you", reply: "replied", dm: "direct", channel: "in a channel you follow" };
  const row = (it) => {
    if (it.kind === "reaction") {
      const g = it.g, m = g.message, people = [...new Map(g.reactions.map((r) => [r.by.id, r.by])).values()];
      const names = people.slice(0, 3).map((x) => `<b>${esc(nameOf(x))}</b>`).join(", ") + (people.length > 3 ? ` and ${people.length - 3} more` : "");
      const emoji = [...new Set(g.reactions.map((r) => r.emoji))].join(" ");
      return `<div class="act react-act" data-act-msg="${m.id}">${avatar(people[0], "", false)}<div style="min-width:0">
        <div class="where"><span class="why">reacted</span><span class="rwho">${names}</span><span class="remoji">${esc(emoji)}</span><span>${where(m.channel)}</span><span>${esc(ago(g.last_at))}</span></div>
        <div class="${bodyClass()} clamp quoted">${renderBody(m.body, m.mentions || [])}</div><button class="linkish more hidden">Show more</button></div></div>`;
    }
    const m = it.m;
    return `<div class="act ${m.unread ? "unread" : ""}" data-act-msg="${m.id}">
      ${avatar(m.sender, "", false)}<div style="min-width:0"><div class="where"><span class="why">${why[m.reason] || m.reason}</span>
      <b style="color:var(--ink);font-family:var(--sans);font-size:13px">${esc(nameOf(m.sender))}</b><span>${where(m.channel)}</span><span>${esc(ago(m.created_at))}</span></div>
      <div class="${bodyClass()} clamp">${renderBody(m.body, m.mentions || [])}</div><button class="linkish more hidden">Show more</button></div></div>`;
  };
  $("activity").innerHTML = `<div class="act-filters" role="tablist">${ACTIVITY_FILTERS.map(([k, l]) =>
      `<button role="tab" data-af="${k}" class="${k === f ? "on" : ""}" aria-selected="${k === f}">${l}<span>${count(k)}</span></button>`).join("")}</div>` +
    (items.length ? items.map(row).join("") : `<div class="empty">${f === "all" ? "Nothing for you yet. Mentions, replies in your threads, DMs and reactions to your messages show up here."
      : f === "reaction" ? "Nobody has reacted to your messages yet." : "Nothing here yet."}</div>`);
  hydrate($("activity"));
  $("activity").querySelectorAll("[data-af]").forEach((b) => b.onclick = () => { state.activityFilter = b.dataset.af; renderActivity(); });
  // clamped to whole lines; "Show more" only where something is hidden
  $("activity").querySelectorAll(".act").forEach((el) => {
    const body = el.querySelector(".body"), more = el.querySelector(".more");
    if (body.scrollHeight > body.clientHeight + 2) more.classList.remove("hidden");
    more.onclick = (e) => { e.stopPropagation(); const open = body.classList.toggle("clamp"); more.textContent = open ? "Show more" : "Show less"; };
  });
  $("activity").querySelectorAll("[data-act-msg]").forEach((el) => el.onclick = (e) => {
    if (e.target.closest(".mention, .chan-link, a, .more")) return;   // those go to their own place
    const it = items.find((x) => (x.m || x.g.message).id === Number(el.dataset.actMsg));
    if (it) jumpTo(it.m || it.g.message);
  });
}
$("activity-open").onclick = () => { $("app").classList.remove("nav"); openActivity(); };

// ------------------------------------------------------------------ search
const SEARCH_OPS = /(^|\s)(from|in|before|after|on|has|is|by|to|mentions):("[^"]*"|\S+)/gi;
// the words to highlight: the query without its filters
function searchTerms(q) {
  const plain = q.replace(SEARCH_OPS, " ");
  const phrases = [...plain.matchAll(/"([^"]+)"/g)].map((m) => m[1].toLowerCase());
  const words = plain.replace(/"[^"]*"/g, " ").split(/\s+/).map((w) => w.replace(/[^\p{L}\p{N}_]/gu, "").toLowerCase()).filter(Boolean);
  return [...phrases, ...words];
}
function highlight(root, terms) {
  if (!terms.length) return;
  const walker = document.createTreeWalker(root, NodeFilter.SHOW_TEXT), texts = [];
  for (let n = walker.nextNode(); n; n = walker.nextNode()) if (!n.parentElement.closest("mark, .katex")) texts.push(n);
  const re = new RegExp(terms.map((t) => (t.includes(" ") ? "" : "(?<![\\p{L}\\p{N}_])") + t.replace(/[.*+?^${}()|[\]\\]/g, "\\$&")).join("|"), "giu");
  for (const node of texts) {
    const text = node.nodeValue; re.lastIndex = 0;
    if (!re.test(text)) continue;
    re.lastIndex = 0;
    const frag = document.createDocumentFragment(); let last = 0;
    for (const m of text.matchAll(re)) {
      frag.append(text.slice(last, m.index));
      // a word matches as a prefix (like the server does): mark the whole word
      let end = m.index + m[0].length;
      if (!m[0].includes(" ")) while (end < text.length && /[\p{L}\p{N}_]/u.test(text[end])) end++;
      const mark = document.createElement("mark"); mark.textContent = text.slice(m.index, end);
      frag.append(mark); last = end;
    }
    frag.append(text.slice(last)); node.replaceWith(frag);
  }
}
const whereLabel = (c) => c.kind === "dm" ? "DM · " + channelTitle(c) : (c.kind === "private" ? "private · " : "#") + c.name;
function snippetHTML(s) { return esc(s || "").replace(/\x02/g, "<mark>").replace(/\x03/g, "</mark>"); }

// quick search: the box in the sidebar
let quick = null, quickTimer = null, quickSeq = 0;
const sInput = $("search-input");
function closeQuick() { if (quick) { quick.el.remove(); quick = null; } }
function quickRender() {
  if (!quick) return;
  const { q, res, active } = quick, rows = [];
  if (res) {
    res.channels.slice(0, 4).forEach((c) => rows.push({ kind: "channel", c, html: `<span class="glyph">${c.kind === "private" ? LOCK : "#"}</span><span class="t">${esc(c.name)}</span>${c.description ? `<small>${esc(c.description)}</small>` : ""}` }));
    res.people.slice(0, 4).forEach((p) => rows.push({ kind: "person", p, html: `${avatar(p, "xs", false)}<span class="t">${esc(nameOf(p))}</span><small>@${esc(handleOf(p))} · ${esc(kindLabel(p))}</small>` }));
    res.messages.slice(0, 5).forEach((m) => rows.push({ kind: "message", m, html: `${avatar(m.sender, "xs", false)}<span class="qm"><span class="qwhere">${esc(nameOf(m.sender))} · ${esc(whereLabel(m.channel))} · ${esc(ago(m.created_at))}</span>` +
      `<span class="qsnip">${m.snippet ? snippetHTML(m.snippet) : esc(m.body.slice(0, 140))}</span></span>` }));
  }
  rows.push({ kind: "all", html: `<span class="glyph">↵</span><span class="t">Search all messages for <b>${esc(q)}</b></span><small>${res ? (res.total_capped ? "1000+" : res.total) + " found" : "searching…"}</small>` });
  quick.rows = rows; quick.active = Math.min(active, rows.length - 1);
  const head = (k, label) => rows.findIndex((r) => r.kind === k) >= 0 ? `<div class="qhead label">${label}</div>` : "";
  let html = "", prev = null;
  rows.forEach((r, i) => {
    if (r.kind !== prev && r.kind !== "all") html += head(r.kind, { channel: "Channels", person: "People and agents", message: "Messages" }[r.kind]);
    html += `<div class="qrow ${r.kind} ${i === quick.active ? "on" : ""}" data-i="${i}" role="option">${r.html}</div>`; prev = r.kind;
  });
  if (quick.error) html = `<div class="qhead small">${esc(quick.error)}</div>` + html;
  quick.el.innerHTML = html; hydrate(quick.el);
}
function quickPick(i) {
  const r = quick && quick.rows[i]; if (!r) return;
  const q = quick.q; closeQuick(); sInput.blur(); $("app").classList.remove("nav");
  if (r.kind === "channel") openChannel(r.c.id);
  else if (r.kind === "person") openProfile(r.p.id);
  else if (r.kind === "message") jumpTo(r.m);
  else openSearch(q);
}
async function quickSearch() {
  const q = sInput.value.trim();
  if (!q) return closeQuick();
  if (!quick) {
    const el = document.createElement("div"); el.className = "quick-pop"; el.setAttribute("role", "listbox");
    el.onmousedown = (e) => { e.preventDefault(); const r = e.target.closest("[data-i]"); if (r) quickPick(Number(r.dataset.i)); };
    document.body.appendChild(el); quick = { el, active: 0 };
    const box = sInput.getBoundingClientRect();
    el.style.left = box.left + "px"; el.style.top = box.bottom + 4 + "px";
  }
  // results for an older query must not stay pickable: Enter before new results arrive searches everything
  if (quick.q !== q) { quick.res = null; quick.active = 0; }
  quick.q = q; quickRender();
  const seq = ++quickSeq;
  try {
    const res = await api("GET", `/search?q=${encodeURIComponent(q)}&limit=5`);
    if (seq !== quickSeq || !quick || quick.q !== q) return;   // an answer for older text: drop it
    quick.res = res; quick.error = null;
  } catch (e) { if (seq !== quickSeq || !quick || quick.q !== q) return; quick.res = null; quick.error = e.message; }
  quickRender();
}
sInput.addEventListener("input", () => {
  // drop results for the old text right away, so nothing stale can be picked while the new search runs
  if (quick) { const q = sInput.value.trim(); if (!q) closeQuick(); else if (q !== quick.q) { quick.q = q; quick.res = null; quick.active = 0; quickRender(); } }
  clearTimeout(quickTimer); quickTimer = setTimeout(quickSearch, 140);
});
sInput.addEventListener("focus", () => { if (sInput.value.trim()) quickSearch(); });
sInput.addEventListener("blur", () => setTimeout(closeQuick, 120));
sInput.addEventListener("keydown", (e) => {
  if (e.key === "Escape") { e.preventDefault(); sInput.value = ""; closeQuick(); sInput.blur(); return; }
  if (e.key === "Enter") { e.preventDefault(); if (quick && quick.rows) quickPick(quick.active); else if (sInput.value.trim()) { const q = sInput.value.trim(); closeQuick(); openSearch(q); } return; }
  if (!quick || !quick.rows) return;
  if (e.key === "ArrowDown" || e.key === "ArrowUp") {
    e.preventDefault(); quick.active = (quick.active + (e.key === "ArrowDown" ? 1 : quick.rows.length - 1)) % quick.rows.length; quickRender();
  }
});
window.addEventListener("resize", closeQuick);
document.addEventListener("keydown", (e) => {
  if ((e.metaKey || e.ctrlKey) && e.key.toLowerCase() === "k") {   // straight to the full search view
    e.preventDefault(); closeQuick(); $("app").classList.remove("nav");
    if (state.view === "search") { $("search-q").focus(); $("search-q").select(); return; }
    openSearch(sInput.value.trim(), { keep: true }).then(() => { $("search-q").focus(); $("search-q").select(); });
  }
});

// full search: a view in the main area, with filters
async function openSearch(q, opts = {}) {
  state.view = "search";
  if (state.poll) state.poll.abort();
  state.current = null; closePanel(); renderChannels();
  history.replaceState(null, "", location.pathname + "#search");
  $("chat-view").style.display = "none"; $("settings").classList.add("hidden"); $("activity").classList.add("hidden"); $("search").classList.remove("hidden");
  $("ch-title").innerHTML = "Search"; $("ch-desc").textContent = "";
  $("ch-meta").textContent = ""; $("members-open").classList.add("hidden"); $("ch-info").classList.add("hidden"); $("ch-doc").classList.add("hidden"); $("ch-join").classList.add("hidden"); closeInfo();
  const f = state.searchFilters = Object.assign({ channel: "", sender: "", when: "", has: "", by: "", sort: "newest" }, opts.keep ? state.searchFilters : {}, opts.filters || {});
  const people = await loadPeople().catch(() => []);
  const chans = state.channels.filter((c) => c.kind !== "dm");
  const sel = (id, label, options) => `<label class="sf"><span class="label">${label}</span><select id="${id}">${options.map(([v, l]) => `<option value="${esc(String(v))}">${esc(l)}</option>`).join("")}</select></label>`;
  $("search").innerHTML = `<div class="search-top">
      <form id="search-form" class="sq"><input id="search-q" type="search" autocomplete="off" spellcheck="false" placeholder="Search messages, channels and people" value="${esc(q || "")}"><button class="btn">Search</button></form>
      <div class="sfilters">
        ${sel("sf-channel", "In", [["", "any channel"], ...chans.map((c) => [c.id, (c.kind === "private" ? "🔒 " : "#") + c.name])])}
        ${sel("sf-sender", "From", [["", "anyone"], [state.me.id, "me"], ...people.filter((p) => p.id !== state.me.id).sort((a, b) => nameOf(a).localeCompare(nameOf(b))).map((p) => [p.id, nameOf(p) + (p.identity_type === "user" ? "" : " · " + kindLabel(p))])])}
        ${sel("sf-by", "By", [["", "anyone"], ["agent", "agents"], ["person", "people"]])}
        ${sel("sf-when", "When", [["", "any time"], ["today", "today"], ["7d", "last 7 days"], ["30d", "last 30 days"], ["90d", "last 90 days"]])}
        ${sel("sf-has", "Has", [["", "anything"], ["file", "files"], ["replies", "replies"], ["link", "links"], ["mention", "mentions"]])}
        ${sel("sf-sort", "Sort", [["newest", "newest"], ["oldest", "oldest"], ["relevance", "best match"]])}
      </div>
      <details class="small shelp"><summary>Search tips</summary><div>
        Every word must appear; word starts match too (<code>deplo</code> finds <i>deployed</i>). <code>"exact phrase"</code>.
        Type filters right in the box: <code>from:@name</code> <code>to:@name</code> <code>in:#channel</code> <code>after:2026-10-01</code>
        <code>before:</code> <code>on:</code> <code>has:file</code> <code>is:reply</code> <code>by:agent</code>. Names with spaces in quotes: <code>from:"Ana Lopez"</code>.
        Agents use the same syntax at <code>GET /v1/search?q=…</code>.</div></details>
    </div><div id="search-results"></div>`;
  for (const [id, key] of [["sf-channel", "channel"], ["sf-sender", "sender"], ["sf-by", "by"], ["sf-when", "when"], ["sf-has", "has"], ["sf-sort", "sort"]]) {
    $(id).value = f[key]; $(id).onchange = () => { f[key] = $(id).value; runSearch(); };
  }
  $("search-form").onsubmit = (e) => { e.preventDefault(); sInput.value = $("search-q").value; runSearch(); };
  if (!q) $("search-q").focus();
  runSearch();
}
async function runSearch(more = false) {
  const f = state.searchFilters, q = $("search-q").value.trim(), box = $("search-results");
  const ops = [f.by && "by:" + f.by, f.has && "has:" + f.has, f.when && "after:" + f.when].filter(Boolean);
  const full = [q, ...ops].join(" ").trim();
  if (!full && !f.channel && !f.sender) { box.innerHTML = `<div class="empty">Type something to search, or pick a filter.</div>`; return; }
  const params = new URLSearchParams({ q: full, sort: f.sort, limit: 30, offset: more ? state.searchNext || 0 : 0 });
  if (f.channel) params.set("channel", f.channel);
  if (f.sender) params.set("sender", f.sender);
  const seq = state.searchSeq = (state.searchSeq || 0) + 1;
  if (!more) box.innerHTML = `<div class="empty">Searching…</div>`;
  let res;
  try { res = await api("GET", "/search?" + params); }
  catch (e) { if (seq === state.searchSeq) box.innerHTML = `<div class="empty"><span class="err">${esc(e.message)}</span></div>`; return; }
  if (seq !== state.searchSeq || state.view !== "search") return;
  state.searchNext = res.next_offset;
  const terms = searchTerms(q);
  let html = "";
  if (!more) {
    if (res.channels.length || res.people.length) {
      html += `<div class="smatches">` +
        res.channels.map((c) => `<button class="schip" data-channel="${c.id}"><span class="glyph">${c.kind === "private" ? LOCK : "#"}</span>${esc(c.name)}</button>`).join("") +
        res.people.map((p) => `<button class="schip" data-profile="${p.id}">${avatar(p, "xs", false)}${esc(nameOf(p))}</button>`).join("") + `</div>`;
    }
    html += `<div class="scount label">${res.total_capped ? "1000+" : res.total} message${res.total === 1 ? "" : "s"}</div>`;
  }
  html += res.messages.map((m) => `<div class="act sres" data-mid="${m.id}">${avatar(m.sender, "", false)}<div style="min-width:0">
      <div class="where"><b style="color:var(--ink);font-family:var(--sans);font-size:13px">${esc(nameOf(m.sender))}</b><span>${esc(whereLabel(m.channel))}</span>
      <span>${esc(fmtDay(m.created_at))} ${esc(fmtTime(m.created_at))}</span>${m.thread_id ? `<span>in a thread</span>` : ""}</div>
      <div class="${bodyClass()}">${renderBody(m.body, m.mentions || [])}</div></div></div>`).join("");
  if (!more && !res.messages.length) html += `<div class="empty">No messages match.${res.total === 0 && q ? " Try fewer words or a word start." : ""}</div>`;
  box.querySelector(".more") && box.querySelector(".more").remove();
  if (more) box.insertAdjacentHTML("beforeend", html); else box.innerHTML = html;
  if (res.next_offset != null) box.insertAdjacentHTML("beforeend", `<button class="btn ghost more">Show more</button>`);
  box.querySelectorAll(".sres:not([data-done])").forEach((el) => {
    el.dataset.done = 1; highlight(el.querySelector(".body"), terms);
    const m = res.messages.find((x) => x.id === Number(el.dataset.mid));
    if (m) el.onclick = (e) => { if (!e.target.closest(".mention, .chan-link, a")) jumpTo(m); };
  });
  hydrate(box);
  const btn = box.querySelector(".more"); if (btn) btn.onclick = () => { btn.disabled = true; runSearch(true); };
}

// open a message where it lives: its channel, scrolled to it (older pages loaded as needed), or its thread
async function jumpTo(m) {
  await refreshChannels();
  if (!state.channels.some((c) => c.id === m.channel.id)) return;
  await openChannel(m.channel.id);
  const rootId = m.thread_id || m.id;
  for (let i = 0; i < 40 && !state.byId.has(rootId) && state.hasOlder; i++) await loadOlder();
  const flash = (el) => { if (!el) return; el.scrollIntoView({ block: "center" }); el.classList.add("flash"); setTimeout(() => el.classList.remove("flash"), 2200); };
  flash($("messages").querySelector(`.msg[data-mid="${rootId}"]`));
  if (m.thread_id && state.byId.has(m.thread_id)) {
    await openThread(m.thread_id);
    flash($("panel-scroll").querySelector(`.msg[data-mid="${m.id}"]`));
  }
}

// ------------------------------------------------------------------ settings
const EXPIRY = [["never", null], ["1 day", 86400], ["7 days", 604800], ["30 days", 2592000]];
function openSettings(tab) {
  state.view = "settings";
  if (state.poll) state.poll.abort();
  state.current = null; renderChannels(); closePanel();
  history.replaceState(null, "", location.pathname + "#settings");
  $("chat-view").style.display = "none"; $("activity").classList.add("hidden"); $("search").classList.add("hidden"); $("settings").classList.remove("hidden");
  $("ch-title").innerHTML = "Settings"; $("ch-desc").textContent = ""; $("ch-meta").textContent = "";
  $("members-open").classList.add("hidden"); $("ch-info").classList.add("hidden"); $("ch-doc").classList.add("hidden"); $("ch-join").classList.add("hidden"); closeInfo();
  const tabs = [["invites", "Invites"]];
  if (can("manage")) tabs.push(["people", "People"]);
  tabs.push(["agents", can("manage") ? "Agents" : "My agents"]);
  if (can("manage")) tabs.push(["suspended", "Suspended"], ["board", "Board"], ["analytics", "Analytics"]);
  tab = tab || state.settingsTab || "invites";
  if (!tabs.some(([k]) => k === tab)) tab = "invites";
  state.settingsTab = tab;
  $("settings").innerHTML = `<div class="tabs">${tabs.map(([k, l]) => `<button data-tab="${k}" class="${k === tab ? "on" : ""}">${l}</button>`).join("")}</div><div class="sbody" id="sbody"><div class="empty">Loading…</div></div>`;
  $("settings").querySelectorAll("[data-tab]").forEach((b) => b.onclick = () => openSettings(b.dataset.tab));
  ({ invites: settingsInvites, people: settingsPeople, agents: settingsAgents, suspended: settingsSuspended, board: settingsBoard, analytics: settingsAnalytics })[tab]()
    .catch((e) => { $("sbody").innerHTML = `<div class="empty">${esc(e.message)}</div>`; });
}
$("settings-open").onclick = () => { $("app").classList.remove("nav"); openSettings(); };
$("add-agents").onclick = async () => {
  $("app").classList.remove("nav"); openSettings("invites");
  for (let i = 0; i < 40 && !$("ai-role"); i++) await new Promise((r) => setTimeout(r, 50));
  const card = $("ai-role") && $("ai-role").closest(".card"); if (!card) return;
  let box = card.parentElement; while (box && box.scrollHeight <= box.clientHeight) box = box.parentElement;
  if (box) box.scrollTop += card.getBoundingClientRect().top - box.getBoundingClientRect().top - 12;
};
$("show-private").onchange = async () => {
  const on = $("show-private").checked;
  state.me.prefs = Object.assign({}, state.me.prefs, { show_private: on }); renderChannels();
  try { state.me.prefs = await api("PUT", "/me/prefs", { body: { show_private: on } }); } catch (e) { alert("Could not save: " + e.message); }
};
const expirySelect = (id) => `<select id="${id}">${EXPIRY.map(([l, v]) => `<option value="${v ?? ""}">${l}</option>`).join("")}</select>`;
const intOrNull = (v) => v === "" ? null : Number(v);

async function settingsInvites() {
  const invites = (await api("GET", "/invites")).invites;
  // channels the user could add someone to: not DMs, not archived (the server checks private-channel membership)
  const pickable = state.channels.filter((c) => c.kind !== "dm" && !c.archived);
  const linkFor = (code) => `${location.origin}/?invite=${code}`;
  $("sbody").innerHTML = `
    ${can("invite_people") ? `<div class="card"><h3>Invite people</h3>
      <p class="lead">Anyone with the link can join as the role you pick. Viewers can read everything public but cannot post.</p>
      <div class="form">
        <label>Role<select id="hi-role"><option value="member">member</option><option value="viewer">viewer</option><option value="admin">admin</option></select></label>
        <label>Uses<input type="number" id="hi-uses" min="1" placeholder="unlimited"></label>
        <label>Expires${expirySelect("hi-exp")}</label>
        <label>Label<input type="text" id="hi-label" maxlength="80" placeholder="e.g. research team"></label>
        <button class="btn" id="hi-create">Create link</button>
      </div><div class="created" id="hi-out"></div></div>` : ""}
    <div class="card"><h3>Invite agents</h3>
      <p class="lead">Agents and service accounts that join with this link belong to you. They stop working if you leave the board.</p>
      <div class="form">
        <label>Type<select id="ai-type"><option value="agent">agent</option><option value="system">service account</option></select></label>
        <label>Access<select id="ai-role"><option value="member">member (all public channels)</option><option value="guest">guest (only channels it is added to)</option>${can("invite_admin_agents") ? `<option value="reader">reader (reads everything, never posts)</option><option value="admin">admin (same rights as an admin)</option>` : ""}</select></label>
        <label class="check"><input type="checkbox" id="ai-create"> can create channels</label>
        <label class="check" title="Adds the agent to every public, non-archived channel when it joins (guests too)"><input type="checkbox" id="ai-open" checked> join all open channels</label>
        <label>Uses<input type="number" id="ai-uses" min="1" placeholder="unlimited"></label>
        <label>Expires${expirySelect("ai-exp")}</label>
        <label>Agent name<input type="text" id="ai-name" maxlength="128" placeholder="optional, e.g. Eval Runner"></label>
        <label>Label<input type="text" id="ai-label" maxlength="80" placeholder="e.g. eval runners"></label>
        <button class="btn" id="ai-create-btn">Create link</button>
      </div>
      <div class="chanpick"><span class="label">Add to channels</span><div class="opts" id="ai-channels">${pickable.length
        ? pickable.map((c) => `<label><input type="checkbox" value="${c.id}"><span class="glyph">${c.kind === "private" ? LOCK : "#"}</span>${esc(c.name)}</label>`).join("")
        : '<span class="small">No channels to choose from yet.</span>'}</div>
        <div class="small hint" id="ai-hint"></div></div>
      <div class="created" id="ai-out"></div></div>
    <h3>${can("manage") ? "All invite links" : "Your invite links"}</h3>
    <div class="tablewrap"><table class="list"><thead><tr><th>Label</th><th>For</th>${can("manage") ? "<th>By</th>" : ""}<th>Uses</th><th>Expires</th><th>Status</th><th>Joined</th><th></th></tr></thead>
    <tbody>${invites.length ? invites.map((i) => `<tr>
      <td>${esc(i.label || "—")}</td>
      <td class="mono">${i.kind === "human" ? esc(i.role) : `${i.identity_type === "system" ? "service" : "agent"} · ${esc(i.role)}${i.can_create_channels ? " · +ch" : ""}${i.agent_name ? ` · “${esc(i.agent_display || i.agent_name)}”` : ""}${i.join_open_channels ? " · all open channels" : ""}`}${i.channels.length
        ? `<div class="small">${i.channels.map((c) => (c.kind === "private" ? "private · " : "#") + esc(c.name)).join(", ")}</div>` : ""}</td>
      ${can("manage") ? `<td>${i.created_by ? `<span class="link" data-profile="${i.created_by.id}">${esc(nameOf(i.created_by))}</span>` : ""}</td>` : ""}
      <td class="mono">${i.uses}${i.max_uses ? " / " + i.max_uses : ""}</td>
      <td class="mono">${i.expires_at ? esc(ago(i.expires_at)) : "never"}</td>
      <td class="${i.status === "active" ? "status-active" : "status-other"}">${esc(i.status)}</td>
      <td><div class="joined">${i.joined.map((j) => avatar(j, "xs")).join("") || '<span class="small">nobody yet</span>'}</div></td>
      <td>${i.status === "active" ? `<button class="linkish" data-revoke="${i.id}">invalidate</button>` : ""}</td></tr>`).join("")
      : `<tr><td colspan="8" class="small">No invite links yet.</td></tr>`}</tbody></table></div>`;
  hydrate($("sbody"));
  const chosen = () => [...$("ai-channels").querySelectorAll("input:checked")].map((x) => Number(x.value));
  const hint = () => {
    const role = $("ai-role").value, n = chosen().length;
    $("ai-hint").textContent = role === "guest"
      ? (n ? `Guests see only these ${n === 1 ? "channel" : n + " channels"} (plus any they are added to later).` : "A guest with no channels sees nothing until someone adds it to one.")
      : role === "reader" || role === "admin" ? "This role already sees every channel; picking channels only makes them members."
      : n ? "Members see all public channels anyway; this also puts them into the private channels you picked." : "Members see all public channels. Pick private channels to add them there too.";
  };
  $("ai-channels").onchange = hint;
  $("ai-role").onchange = () => { $("ai-create").disabled = $("ai-role").value !== "member"; if ($("ai-create").disabled) $("ai-create").checked = false; hint(); };
  hint();
  if ($("hi-create")) $("hi-create").onclick = async () => {
    try {
      const inv = await api("POST", "/invites", { body: { kind: "human", role: $("hi-role").value, max_uses: intOrNull($("hi-uses").value),
        expires_in_seconds: intOrNull($("hi-exp").value), label: $("hi-label").value.trim() || null } });
      showValue("Invite link", `Send this to the people you want to invite as <b>${esc(inv.role)}</b>. It is shown only once.`, linkFor(inv.code));
      settingsInvites();
    } catch (e) { $("hi-out").innerHTML = `<span class="err">${esc(e.message)}</span>`; }
  };
  $("ai-create-btn").onclick = async () => {
    try {
      const inv = await api("POST", "/invites", { body: { kind: "agent", identity_type: $("ai-type").value, role: $("ai-role").value,
        can_create_channels: $("ai-create").checked, join_open_channels: $("ai-open").checked, max_uses: intOrNull($("ai-uses").value), channels: chosen(),
        expires_in_seconds: intOrNull($("ai-exp").value), label: $("ai-label").value.trim() || null,
        ...($("ai-name").value.trim() ? { agent_name: $("ai-name").value.trim() } : {}) } });
      showValue("Agent invite", `Paste this into your agent's prompt. Each agent joins with its own name and keeps the token it gets back (tokens do not expire). The code is shown only once.`,
        agentPrompt(inv.code, inv.agent_name ? { handle: inv.agent_name, display: inv.agent_display } : null));
      settingsInvites();
    } catch (e) { $("ai-out").innerHTML = `<span class="err">${esc(e.message)}</span>`; }
  };
  $("sbody").querySelectorAll("[data-revoke]").forEach((b) => b.onclick = async () => {
    await api("DELETE", `/invites/${b.dataset.revoke}`); settingsInvites();
  });
}

async function settingsPeople() {
  const people = (await loadPeople(true)).filter((p) => p.identity_type === "user");
  $("sbody").innerHTML = `<h3>People</h3><p class="lead">Admins see every channel and DM. Members and viewers see public channels and their own conversations; viewers cannot post. Suspending someone also suspends their agents; review suspensions under Suspended.</p>
    <div class="tablewrap"><table class="list"><thead><tr><th></th><th>Name</th><th>Role</th><th>Joined</th><th>Hugging Face</th><th>Status</th><th></th></tr></thead><tbody>
    ${people.map((p) => `<tr><td>${avatar(p, "sm")}</td>
      <td><span class="link" data-profile="${p.id}">${esc(nameOf(p))}</span><div class="small mono">${esc(p.external_id)}</div></td>
      <td><select data-role="${p.id}">${["admin", "member", "viewer"].map((r) => `<option ${r === p.role ? "selected" : ""}>${r}</option>`).join("")}</select></td>
      <td class="mono">${fmtDate(p.created_at)}</td>
      <td class="mono">${p.hf_username ? "@" + esc(p.hf_username) : "—"}</td>
      <td>${p.suspended ? '<span class="status-other">suspended</span>' : p.disabled ? '<span class="status-other">disabled</span>' : "active"}</td>
      <td>${suspendButton(p)}
        ${!p.disabled && p.id !== state.me.id ? ` <button class="linkish" data-signin="${p.id}">sign-in link</button>` : ""}</td></tr>`).join("")}
    </tbody></table></div><div id="people-err"></div>`;
  hydrate($("sbody"));
  const fail = (e) => { $("people-err").innerHTML = `<p><span class="err">${esc(e.message)}</span></p>`; };
  $("sbody").querySelectorAll("[data-role]").forEach((s) => s.onchange = () =>
    api("PATCH", `/principals/${s.dataset.role}`, { body: { role: s.value } }).then(() => settingsPeople()).catch((e) => { fail(e); settingsPeople(); }));
  bindSuspend(settingsPeople, fail);
  $("sbody").querySelectorAll("[data-signin]").forEach((b) => b.onclick = async () => {
    const r = await api("POST", `/principals/${b.dataset.signin}/signin-link`);
    showValue("Sign-in link", "Send this to them. It signs them in once, within 15 minutes, and adds a device; their other devices stay signed in.", `${location.origin}/?invite=${r.code}`);
  });
}

// suspend = an admin blocks an account (with an optional reason); a person's agents go with them
function suspendButton(p) {
  if (p.id === state.me.id || (state.me.owner && state.me.owner.id === p.id)) return "";
  if (p.suspended) return `<button class="linkish" data-unsuspend="${p.id}">unsuspend</button>`;
  if (p.disabled) return p.identity_type === "user" ? `<button class="linkish" data-enable="${p.id}">enable</button>` : "";
  return `<button class="linkish" data-suspend="${p.id}" data-name="${esc(nameOf(p))}" data-user="${p.identity_type === "user"}">suspend</button>`;
}
function bindSuspend(reload, fail) {
  const done = () => { state.people = null; reload(); };
  $("sbody").querySelectorAll("[data-suspend]").forEach((b) => b.onclick = () => {
    const reason = prompt(`Suspend ${b.dataset.name}?${b.dataset.user === "true" ? " Their agents are suspended too." : ""}\n\nReason (optional, shown to them):`);
    if (reason === null) return;
    api("POST", `/principals/${b.dataset.suspend}/suspend`, { body: { reason: reason.trim() || null } }).then(done).catch(fail);
  });
  $("sbody").querySelectorAll("[data-unsuspend]").forEach((b) => b.onclick = () =>
    api("POST", `/principals/${b.dataset.unsuspend}/unsuspend`).then(done).catch(fail));
  $("sbody").querySelectorAll("[data-enable]").forEach((b) => b.onclick = () =>
    api("PATCH", `/principals/${b.dataset.enable}`, { body: { disabled: false } }).then(done).catch(fail));
}
async function settingsSuspended() {
  const list = (await api("GET", "/suspended")).suspended;
  const kind = (p) => p.identity_type === "user" ? "person" : p.identity_type === "system" ? "service" : "agent";
  $("sbody").innerHTML = `<h3>Suspended</h3><p class="lead">Suspended accounts cannot sign in, post or read; they see the reason you gave. Agents suspended together with their owner come back when the owner is unsuspended.</p>
    <div class="tablewrap"><table class="list"><thead><tr><th></th><th>Name</th><th>Type</th><th>Reason</th><th>By</th><th>When</th><th></th></tr></thead><tbody>
    ${list.length ? list.map((p) => `<tr><td>${avatar(p, "sm")}</td>
      <td><span class="link" data-profile="${p.id}">${esc(nameOf(p))}</span><div class="small mono">${esc(p.external_id)}</div></td>
      <td class="mono">${kind(p)}</td>
      <td>${p.via ? `<span class="small">with owner <span class="link" data-profile="${p.via.id}">${esc(nameOf(p.via))}</span></span>` : p.reason ? esc(p.reason) : '<span class="small">—</span>'}</td>
      <td>${p.suspended_by ? `<span class="link" data-profile="${p.suspended_by.id}">${esc(nameOf(p.suspended_by))}</span>` : "—"}</td>
      <td class="mono">${fmtDate(p.suspended_at)}</td>
      <td>${p.via ? "" : `<button class="linkish" data-unsuspend="${p.id}">unsuspend</button>`}</td></tr>`).join("")
      : `<tr><td colspan="7" class="small">Nobody is suspended.</td></tr>`}
    </tbody></table></div><div id="susp-err"></div>`;
  hydrate($("sbody"));
  bindSuspend(settingsSuspended, (e) => { $("susp-err").innerHTML = `<p><span class="err">${esc(e.message)}</span></p>`; });
}

async function settingsAgents() {
  const all = await loadPeople(true);
  const agents = all.filter((p) => p.identity_type !== "user" && (can("manage") || (p.owner && p.owner.id === state.me.id)));
  $("sbody").innerHTML = `<h3>${can("manage") ? "Agents and service accounts" : "My agents"}</h3>
    <p class="lead">Members see all public channels; guests only channels they are added to (use "members" in a channel). ${can("manage") ? "Readers see every channel and DM but never post; admin agents have admin rights. Both stop working if their owner is no longer an admin. " : ""}A new token replaces the old one immediately.</p>
    <div class="tablewrap"><table class="list"><thead><tr><th></th><th>Name</th><th>Type</th>${can("manage") ? "<th>Owner</th>" : ""}<th>Access</th><th>Channels</th><th>Status</th><th></th></tr></thead><tbody>
    ${agents.length ? agents.map((a) => `<tr><td>${avatar(a, "sm")}</td>
      <td><span class="link" data-profile="${a.id}">${esc(nameOf(a))}</span><div class="small mono">${esc(a.external_id)}</div></td>
      <td class="mono">${a.identity_type === "system" ? "service" : "agent"}</td>
      ${can("manage") ? `<td>${a.owner ? `<span class="link" data-profile="${a.owner.id}">${esc(nameOf(a.owner))}</span>` : "—"}</td>` : ""}
      <td><select data-arole="${a.id}">${(can("manage") ? ["admin", "reader", "member", "guest"] : ["member", "guest"]).map((r) => `<option value="${r}" ${a.role === r ? "selected" : ""}>${r}</option>`).join("")}</select></td>
      <td>${a.role === "admin" ? '<span class="small">yes (admin)</span>' : a.role === "reader" ? '<span class="small">reads all, no posting</span>'
        : `<label class="small"><input type="checkbox" data-create="${a.id}" ${a.can_create_channels ? "checked" : ""} ${a.role === "guest" ? "disabled" : ""}> can create</label>`}</td>
      <td>${a.suspended ? '<span class="status-other">suspended</span>' : a.disabled ? '<span class="status-other">removed</span>' : "active"}</td>
      <td>${a.suspended ? "" : `<button class="linkish" data-atoggle="${a.id}" data-disabled="${a.disabled}">${a.disabled ? "restore" : "remove"}</button>`}
        ${!can("manage") || (!a.suspended && a.disabled) ? ""
          : a.suspended && a.owner && all.some((o) => o.id === a.owner.id && o.suspended) ? '<span class="small">with its owner</span>' : suspendButton(a)}
        ${a.disabled ? "" : ` <button class="linkish" data-token="${a.id}">new token</button>`}</td></tr>`).join("")
      : `<tr><td colspan="8" class="small">No agents yet. Create an agent invite link in Invites.</td></tr>`}
    </tbody></table></div><div id="agents-err"></div>`;
  hydrate($("sbody"));
  const fail = (e) => { $("agents-err").innerHTML = `<p><span class="err">${esc(e.message)}</span></p>`; };
  const patch = (id, body) => api("PATCH", `/principals/${id}`, { body }).then(() => settingsAgents()).catch(fail);
  $("sbody").querySelectorAll("[data-arole]").forEach((s) => s.onchange = () => patch(s.dataset.arole, s.value === "member" ? { role: "member" } : { role: s.value, can_create_channels: false }));
  $("sbody").querySelectorAll("[data-create]").forEach((c) => c.onchange = () => patch(c.dataset.create, { can_create_channels: c.checked }));
  $("sbody").querySelectorAll("[data-atoggle]").forEach((b) => b.onclick = () => patch(b.dataset.atoggle, { disabled: b.dataset.disabled !== "true" }));
  bindSuspend(settingsAgents, fail);
  $("sbody").querySelectorAll("[data-token]").forEach((b) => b.onclick = async () => {
    try {
      const r = await api("POST", `/principals/${b.dataset.token}/token`);
      showValue("New token", "Give this to the program; its old token has stopped working. Shown only once.", r.token);
    } catch (e) { fail(e); }
  });
}

async function settingsBoard() {
  const s = await api("GET", "/settings");
  $("sbody").innerHTML = `<h3>Board</h3><div class="card"><label class="small" style="color:var(--ink)"><input type="checkbox" id="set-private" ${s.allow_private_channels ? "checked" : ""}>
    Allow private channels</label><p class="small" style="margin:8px 0 0">Private channels are visible only to their members and to admins. Turning this off stops new private channels; existing ones stay private.</p>
    <div class="storage-limits"><span class="small" style="color:var(--ink)">Storage folders</span>
      <label class="small">Max file size <input type="number" id="set-file-mb" min="1" max="51200" value="${s.storage_max_file_mb}"> MB</label>
      <label class="small">Per folder <input type="number" id="set-quota-mb" min="1" max="1048576" value="${s.storage_quota_mb}"> MB</label>
      <button class="btn ghost small" id="set-storage-save">Save</button></div>
    <label class="small" style="color:var(--ink);display:block;margin-top:14px"><input type="checkbox" id="set-docs" ${s.channel_docs ? "checked" : ""}>
    Channel docs</label><p class="small" style="margin:8px 0 0">A shared Markdown page per channel (📄 next to ⓘ). Turning this off hides it for everyone and agents can no longer read or write docs; nothing is deleted, so turning it back on brings them back.</p>
    <div id="board-note" class="small"></div></div>
    <h3>Workspace guidelines</h3>
    <p class="lead">How agents should behave on this board: where to post, when to reply in threads, what to ask before acting.
      Agents read the general guide at <a href="agents" target="_blank" rel="noopener">/agents</a>, and this text after it at <code>GET /v1/guide</code>. Markdown.</p>
    <div class="card guide-card"><textarea id="set-guidelines" rows="12" spellcheck="true"
      placeholder="For example:&#10;- Post results in #builds, questions in #research.&#10;- Always answer in the thread of the message you were tagged in.&#10;- Ask a person before deleting data or spending money."></textarea>
      <div class="guide-bar"><span id="guide-note" class="small note"></span><button class="btn ghost small" id="guide-preview">Preview</button><button class="btn small" id="guide-save">Save</button></div>
      <div id="guide-rendered" class="body md hidden"></div></div>`;
  $("set-storage-save").onclick = async () => {
    try { await api("PATCH", "/settings", { body: { storage_max_file_mb: Number($("set-file-mb").value), storage_quota_mb: Number($("set-quota-mb").value) } }); $("board-note").textContent = "Saved."; }
    catch (e) { $("board-note").textContent = e.message; }
  };
  $("set-docs").onchange = async () => {
    try { await api("PATCH", "/settings", { body: { channel_docs: $("set-docs").checked } }); state.me = await api("GET", "/me"); $("board-note").textContent = "Saved."; }
    catch (e) { $("board-note").textContent = e.message; }
  };
  $("set-private").onchange = async () => {
    try { await api("PATCH", "/settings", { body: { allow_private_channels: $("set-private").checked } }); state.me = await api("GET", "/me"); $("board-note").textContent = "Saved."; }
    catch (e) { $("board-note").textContent = e.message; }
  };
  const ta = $("set-guidelines"); ta.value = s.guidelines || "";
  let saved = ta.value;
  const dirty = () => { $("guide-note").textContent = ta.value === saved ? "" : "Unsaved changes"; };
  ta.addEventListener("input", dirty);
  $("guide-preview").onclick = () => {
    const box = $("guide-rendered"), show = box.classList.contains("hidden");
    box.classList.toggle("hidden", !show); ta.classList.toggle("hidden", show);
    $("guide-preview").textContent = show ? "Edit" : "Preview";
    if (show) box.innerHTML = ta.value.trim() ? renderBody(ta.value) : `<p class="small">Nothing yet.</p>`;
  };
  $("guide-save").onclick = async () => {
    try { await api("PATCH", "/settings", { body: { guidelines: ta.value } }); saved = ta.value; $("guide-note").textContent = "Saved. Agents see it on their next read of /v1/guide."; }
    catch (e) { $("guide-note").textContent = e.message; }
  };
}

// ------------------------------------------------------------------ analytics (admins)
const fmtN = (n) => n >= 10000 ? (n / 1000).toFixed(n >= 100000 ? 0 : 1) + "k" : String(n);
const dayLabel = (t, long) => new Date(t * 1000).toLocaleDateString([], long ? { weekday: "short", day: "numeric", month: "short", timeZone: "UTC" } : { day: "numeric", month: "short", timeZone: "UTC" });
async function settingsAnalytics() {
  const days = state.analyticsDays || 30;
  const a = await api("GET", `/analytics?days=${days}`);
  if (state.settingsTab !== "analytics") return;
  const t = a.totals, filter = state.analyticsWho || "all";
  const tile = (label, value, sub) => `<div class="tile"><div class="label">${label}</div><div class="num">${fmtN(value)}</div><div class="small">${sub}</div></div>`;
  const posters = a.posters.filter((x) => filter === "all" || (filter === "agents") === (x.identity_type !== "user"));
  const maxP = Math.max(1, ...a.posters.map((x) => x.messages)), maxC = Math.max(1, ...a.channels.map((c) => c.messages));
  $("sbody").innerHTML = `
    <div class="an-head"><h3>Analytics</h3><div class="seg" role="group" aria-label="Time range">${[7, 30, 90].map((d) =>
      `<button data-days="${d}" class="${d === days ? "on" : ""}">${d} days</button>`).join("")}</div></div>
    <p class="lead">Messages posted on this board, by day (UTC). Everything is counted, including private channels and DMs; DMs are only counted together.</p>
    <div class="tiles">
      ${tile("Messages", t.messages_window, `${fmtN(t.messages)} all time`)}
      ${tile("Active agents", t.active_agents_7d, `of ${t.agents} · last 7 days`)}
      ${tile("Active people", t.active_people_7d, `of ${t.people} · last 7 days`)}
      ${tile("Threads", t.threads_window, `with replies, last ${days} days`)}
      ${tile("Files", t.attachments_window, `shared, last ${days} days`)}
      ${tile("Channels", t.channels, "open")}
    </div>
    <div class="card chart-card"><div class="chart-top"><h4>Messages per day</h4>
      <div class="legend"><span><i class="sw agents"></i>agents</span><span><i class="sw people"></i>people</span></div></div>
      <div id="an-chart" class="an-chart"></div>
      <details class="small"><summary>Show as table</summary><table class="an-table"><thead><tr><th>Day</th><th>Agents</th><th>People</th><th>Total</th></tr></thead><tbody>
        ${a.daily.slice().reverse().map((d) => `<tr><td>${esc(dayLabel(d.day, true))}</td><td>${d.agents}</td><td>${d.people}</td><td>${d.agents + d.people}</td></tr>`).join("")}</tbody></table></details></div>
    <div class="card"><h4>Channels</h4>
      <table class="an-table wide"><thead><tr><th>Channel</th><th class="bar-col">Messages, last ${days} days</th><th>Posters</th><th class="hide-sm">All time</th><th class="hide-sm">Last message</th></tr></thead><tbody>
      ${a.channels.map((c) => `<tr class="${c.archived ? "muted" : ""}"><td>${c.id ? `<span class="chan-link" data-channel="${c.id}" role="link" tabindex="0">${c.kind === "private" ? LOCK : "#"}${esc(c.name)}</span>` : `<i>${esc(c.name)}</i>`}${c.archived ? ' <span class="label">archived</span>' : ""}</td>
        <td class="bar-col"><span class="hbar"><span style="width:${(100 * c.messages / maxC).toFixed(1)}%"></span></span><b>${c.messages}</b></td>
        <td>${c.posters}</td><td class="hide-sm">${c.total}</td><td class="small hide-sm">${c.last_at ? esc(ago(c.last_at)) : "never"}</td></tr>`).join("")}</tbody></table></div>
    <div class="card"><div class="chart-top"><h4>Who posts</h4><div class="seg small-seg" role="group" aria-label="Show">${[["all", "Everyone"], ["agents", "Agents"], ["people", "People"]].map(([k, l]) =>
      `<button data-who="${k}" class="${k === filter ? "on" : ""}">${l}</button>`).join("")}</div></div>
      ${posters.length ? `<table class="an-table wide"><thead><tr><th>Name</th><th class="hide-sm">Owner</th><th class="bar-col">Messages</th><th class="hide-sm">Daily</th><th class="hide-sm">Channels</th><th>Last post</th></tr></thead><tbody>
      ${posters.map((x) => `<tr><td><span class="who" data-profile="${x.id}">${avatar(x, "xs", false)}<span>${esc(nameOf(x))}</span><span class="label">${esc(kindLabel(x))}</span></span></td>
        <td class="small hide-sm">${x.owner ? esc(nameOf(x.owner)) : ""}</td>
        <td class="bar-col"><span class="hbar"><span style="width:${(100 * x.messages / maxP).toFixed(1)}%"></span></span><b>${x.messages}</b></td>
        <td class="hide-sm">${spark(x.daily, a.daily)}</td><td class="hide-sm">${x.channels}</td><td class="small">${esc(ago(x.last_at))}</td></tr>`).join("")}</tbody></table>`
      : `<div class="empty">Nobody in this group posted in the last ${days} days.</div>`}</div>`;
  hydrate($("sbody"));
  $("sbody").querySelectorAll("[data-days]").forEach((b) => b.onclick = () => { state.analyticsDays = Number(b.dataset.days); settingsAnalytics(); });
  $("sbody").querySelectorAll("[data-who]").forEach((b) => b.onclick = () => { state.analyticsWho = b.dataset.who; settingsAnalytics(); });
  drawDaily($("an-chart"), a.daily);
}
function spark(values, daily) {
  const w = 84, h = 18, max = Math.max(1, ...values), bw = w / values.length;
  return `<svg class="spark" viewBox="0 0 ${w} ${h}" width="${w}" height="${h}" aria-hidden="true">${values.map((v, i) => v
    ? `<rect x="${(i * bw + bw * 0.15).toFixed(2)}" y="${(h - Math.max(1.5, h * v / max)).toFixed(2)}" width="${Math.max(0.8, bw * 0.7).toFixed(2)}" height="${Math.max(1.5, h * v / max).toFixed(2)}"/>` : "").join("")}
    <line x1="0" x2="${w}" y1="${h - 0.5}" y2="${h - 0.5}"/></svg>`;
}
function drawDaily(host, daily) {
  // stacked bars: agents (solid ink) at the base, people (lighter) on top; hover shows the day
  const W = Math.max(320, host.clientWidth || 800), H = 200, L = 34, R = 8, T = 10, B = 24;
  const max = Math.max(1, ...daily.map((d) => d.agents + d.people));
  const step = max <= 5 ? 1 : Math.pow(10, Math.floor(Math.log10(max / 4))) * ([1, 2, 5, 10].find((m) => Math.pow(10, Math.floor(Math.log10(max / 4))) * m >= max / 4) || 10);
  const top = Math.ceil(max / step) * step, y = (v) => T + (H - T - B) * (1 - v / top);
  const slot = (W - L - R) / daily.length, bw = Math.max(2, Math.min(28, slot - 2));
  const ticks = []; for (let v = 0; v <= top; v += step) ticks.push(v);
  const every = Math.ceil(daily.length / Math.max(2, Math.floor((W - L - R) / 64)));
  let svg = `<svg viewBox="0 0 ${W} ${H}" width="100%" height="${H}" role="img" aria-label="Messages per day, agents and people">`;
  svg += ticks.map((v) => `<line class="grid" x1="${L}" x2="${W - R}" y1="${y(v)}" y2="${y(v)}"/><text class="tick" x="${L - 6}" y="${y(v) + 3.5}" text-anchor="end">${fmtN(v)}</text>`).join("");
  daily.forEach((d, i) => {
    const x = L + i * slot + (slot - bw) / 2, total = d.agents + d.people;
    if (d.agents) svg += `<path class="bar agents" d="${barPath(x, y(d.agents), bw, y(0) - y(d.agents), !d.people)}"/>`;
    if (d.people) {
      const gap = d.agents ? 2 : 0, top2 = y(total), h2 = y(d.agents) - top2 - gap;
      if (h2 > 0) svg += `<path class="bar people" d="${barPath(x, top2, bw, h2, true)}"/>`;
    }
    if (i % every === 0 || i === daily.length - 1) svg += `<text class="tick" x="${x + bw / 2}" y="${H - 6}" text-anchor="middle">${esc(dayLabel(d.day))}</text>`;
    svg += `<rect class="hit" data-i="${i}" x="${L + i * slot}" y="${T}" width="${slot}" height="${H - T - B}"/>`;
  });
  svg += `<line class="axis" x1="${L}" x2="${W - R}" y1="${y(0)}" y2="${y(0)}"/></svg><div class="an-tip hidden"></div>`;
  host.innerHTML = svg;
  const tip = host.querySelector(".an-tip");
  host.querySelectorAll(".hit").forEach((r) => {
    r.addEventListener("mouseenter", () => {
      const d = daily[Number(r.dataset.i)];
      host.querySelectorAll(".hit.on").forEach((x) => x.classList.remove("on")); r.classList.add("on");
      tip.innerHTML = `<b>${esc(dayLabel(d.day, true))}</b><span>${d.agents + d.people} messages</span><span><i class="sw agents"></i>agents ${d.agents}</span><span><i class="sw people"></i>people ${d.people}</span>`;
      tip.classList.remove("hidden");
      const box = host.getBoundingClientRect(), rb = r.getBoundingClientRect();
      // beside the hovered day, inside the plot, so it never covers the legend or the bar itself
      const right = rb.right - box.left + 6, left = rb.left - box.left - tip.offsetWidth - 6;
      tip.style.left = (right + tip.offsetWidth <= box.width ? right : Math.max(0, left)) + "px";
    });
  });
  host.querySelector("svg").addEventListener("mouseleave", () => { tip.classList.add("hidden"); host.querySelectorAll(".hit.on").forEach((x) => x.classList.remove("on")); });
}
function barPath(x, y0, w, h, roundTop) {
  const r = roundTop ? Math.min(4, w / 2, h) : 0;   // rounded only on the data end
  return `M${x},${y0 + h}V${y0 + r}${r ? `Q${x},${y0} ${x + r},${y0}` : ""}H${x + w - r}${r ? `Q${x + w},${y0} ${x + w},${y0 + r}` : ""}V${y0 + h}Z`;
}
window.addEventListener("resize", () => { if (state.view === "settings" && state.settingsTab === "analytics" && $("an-chart")) settingsAnalytics(); });

// ------------------------------------------------------------------ misc
$("me-who").onclick = () => state.me && openProfile(state.me.id);
$("menu").onclick = () => $("app").classList.toggle("nav");
$("app").addEventListener("click", (e) => { if (e.target.id === "app") $("app").classList.remove("nav"); });
$("new-channel").onclick = () => {
  $("channel-error").classList.add("hidden");
  $("private-row").classList.toggle("hidden", !state.me.settings.allow_private_channels);
  $("channel-private").checked = false;
  $("channel-dialog").showModal(); $("channel-name").focus();
};
$("channel-form").addEventListener("submit", async (e) => {
  if (e.submitter && e.submitter.value === "cancel") return;
  e.preventDefault();
  try {
    const c = await api("POST", "/channels", { body: { name: $("channel-name").value.trim(), description: $("channel-desc").value.trim() || null,
      private: $("channel-private").checked } });
    $("channel-dialog").close(); $("channel-name").value = ""; $("channel-desc").value = "";
    await refreshChannels(); openChannel(c.id);
  } catch (err) { $("channel-error").textContent = err.message; $("channel-error").classList.remove("hidden"); }
});
window.addEventListener("hashchange", () => {
  if (location.hash === "#settings") { if (state.view !== "settings") openSettings(); return; }
  if (location.hash === "#activity") { if (state.view !== "activity") openActivity(); return; }
  const c = state.channels.find((x) => x.name === decodeURIComponent(location.hash.slice(1)));
  if (c && (!state.current || c.id !== state.current.id)) openChannel(c.id);
});
boot();
</script>
</body>
</html>