File size: 76,606 Bytes
f44aac9
4791c0a
 
 
 
 
 
ffcf6c4
 
 
 
 
 
4791c0a
 
 
 
 
 
 
f44aac9
 
 
 
 
 
5a83369
9eec184
3b181a1
36ed450
5a83369
 
9219266
f44aac9
 
dd8c015
 
 
 
 
 
9eec184
f68e817
5a83369
d659d2d
52c63cf
50124a7
7d1e08d
 
 
6d9770a
 
 
 
 
 
 
50124a7
9eec184
6d9770a
41ae6a8
 
8c014b8
f44aac9
 
5a83369
9eec184
 
 
3b181a1
1afd4af
 
c4383f3
41e12b2
8baaa6e
7d1e08d
 
 
 
73bbb0c
6d9770a
 
4791c0a
07b5572
4791c0a
 
 
 
ffcf6c4
 
 
 
 
 
 
f44aac9
3628f6f
4791c0a
 
be083b8
f44aac9
 
 
ac37bfa
f44aac9
 
5a83369
 
 
dd8c015
 
 
 
 
 
 
 
 
 
5a83369
 
a685195
5a83369
 
 
1ec322d
 
f68e817
 
 
 
5a83369
 
 
 
 
41ae6a8
d659d2d
41ae6a8
52c63cf
50124a7
c4383f3
50124a7
 
4791c0a
 
 
 
 
 
 
 
 
 
 
 
ffcf6c4
 
 
 
 
 
 
 
 
 
 
 
 
7d1e08d
 
 
 
 
73bbb0c
7d1e08d
 
 
 
 
 
 
 
 
 
9eec184
3b181a1
9eec184
ff36d65
3b181a1
9eec184
3b181a1
9eec184
 
1c7ce9d
50124a7
9eec184
3b181a1
 
 
 
 
 
ff36d65
3b181a1
 
 
 
 
 
 
 
e25f6ac
50124a7
3b181a1
 
67e347f
 
 
 
 
 
dd8c015
 
 
 
 
 
 
4791c0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ffcf6c4
4791c0a
ffcf6c4
4791c0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ffcf6c4
 
 
4791c0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ffcf6c4
 
4791c0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ffcf6c4
 
4791c0a
ffcf6c4
4791c0a
 
 
ffcf6c4
c810fc6
4791c0a
 
 
c810fc6
ffcf6c4
4791c0a
 
 
 
c810fc6
 
 
 
 
 
 
 
 
 
 
 
ffcf6c4
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
4791c0a
 
 
 
 
07b5572
 
 
 
4791c0a
 
 
 
 
 
ffcf6c4
 
4791c0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ffcf6c4
 
 
 
 
 
 
 
 
 
4791c0a
 
 
 
 
 
 
 
ffcf6c4
 
 
4791c0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ffcf6c4
 
4791c0a
 
 
 
ffcf6c4
 
 
 
 
 
 
 
 
4791c0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1147a5f
 
 
 
 
 
4791c0a
 
04ad98e
4791c0a
 
 
 
 
 
 
 
 
 
 
 
1147a5f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
04ad98e
 
 
 
4791c0a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1a39d73
 
 
 
 
 
 
 
 
 
4791c0a
 
 
 
 
 
 
 
 
 
ffcf6c4
 
 
 
 
4791c0a
 
 
 
 
 
 
5a83369
ac37bfa
41e12b2
dd8c015
f44aac9
 
5a83369
8baaa6e
f44aac9
 
5a83369
1ccc715
6d9770a
1afd4af
f44aac9
a685195
f44aac9
151c180
 
 
 
 
 
 
f44aac9
151c180
 
f44aac9
151c180
 
f44aac9
a685195
f44aac9
1afd4af
1c7ce9d
1afd4af
f44aac9
 
1afd4af
6d9770a
f44aac9
8baaa6e
ff36d65
f44aac9
 
a685195
 
 
151c180
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
a685195
 
 
 
 
 
 
 
5a83369
f44aac9
7d1e08d
73bbb0c
 
7d1e08d
 
73bbb0c
7d1e08d
 
 
 
3628f6f
7d1e08d
 
 
 
73bbb0c
 
 
7d1e08d
 
 
 
 
 
 
 
 
 
73bbb0c
7d1e08d
 
 
 
 
 
73bbb0c
7d1e08d
 
73bbb0c
 
 
 
 
 
 
 
7d1e08d
 
 
73bbb0c
7d1e08d
 
 
 
73bbb0c
 
 
 
7d1e08d
 
 
 
73bbb0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7d1e08d
 
 
 
 
 
 
 
 
 
 
 
 
73bbb0c
7d1e08d
 
 
 
 
 
73bbb0c
7d1e08d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
73bbb0c
7d1e08d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f44aac9
 
be083b8
f44aac9
c4383f3
9eec184
 
 
 
 
3b181a1
c4383f3
9219266
9eec184
3b181a1
50124a7
f44aac9
3628f6f
f44aac9
 
be083b8
 
 
 
 
 
 
8baaa6e
be083b8
 
 
 
 
 
dd8c015
be083b8
9eec184
be083b8
 
 
 
 
 
 
c4383f3
 
 
9eec184
c4383f3
 
 
dd8c015
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41e12b2
 
 
 
 
 
 
 
 
 
dd8c015
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
41e12b2
 
8baaa6e
 
9eec184
8baaa6e
 
 
 
 
 
 
 
dd8c015
 
 
7d1e08d
 
 
 
73bbb0c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8baaa6e
 
c4383f3
 
41e12b2
c4383f3
 
 
 
41e12b2
 
8baaa6e
c4383f3
1c7ce9d
c4383f3
 
9eec184
c4383f3
 
dd8c015
c4383f3
 
 
25af88b
c4383f3
41e12b2
ff36d65
c4383f3
 
 
 
f68e817
41e12b2
dd8c015
f68e817
 
8baaa6e
f68e817
 
1c7ce9d
f68e817
 
 
1c7ce9d
f68e817
 
1c7ce9d
f68e817
 
 
 
8baaa6e
ff36d65
f68e817
 
 
 
 
 
 
9eec184
1ccc715
a3e1f0c
f68e817
1c7ce9d
f68e817
 
dd8c015
f68e817
 
 
 
9eec184
f68e817
 
 
 
 
 
 
 
 
 
ff36d65
1ccc715
f68e817
 
 
9219266
e8d7906
 
 
 
 
 
9219266
 
50124a7
4d09659
 
 
 
 
 
98631d7
 
4d09659
 
 
 
67e347f
 
 
 
 
 
1ccc715
dd8c015
1ccc715
 
67e347f
 
 
50124a7
25af88b
50124a7
 
 
dd8c015
50124a7
25af88b
50124a7
 
 
ff36d65
1ccc715
50124a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9eec184
 
 
50124a7
 
 
 
 
 
 
4d09659
1ccc715
 
50124a7
 
 
1ccc715
 
 
 
 
 
 
9eec184
96da637
 
 
 
 
 
 
 
 
 
 
 
 
 
 
50124a7
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9eec184
 
 
 
 
 
3b181a1
 
9eec184
 
3b181a1
9eec184
3b181a1
96da637
 
9eec184
96da637
8baaa6e
96da637
 
dd8c015
 
 
9eec184
96da637
 
 
3b181a1
9eec184
3b181a1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
dd8c015
3b181a1
8baaa6e
3b181a1
 
 
 
 
 
f44aac9
 
 
 
 
 
 
 
 
 
6d9770a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f44aac9
1afd4af
f44aac9
 
 
 
 
6d9770a
 
 
1afd4af
 
 
 
 
f44aac9
3b181a1
9eec184
1ccc715
 
3f78502
4d09659
 
 
 
 
 
902a11f
 
 
 
3f78502
 
902a11f
f44aac9
9eec184
3b181a1
5a83369
 
f44aac9
dd8c015
5a83369
f44aac9
 
3f78502
 
caed1d4
 
 
 
f44aac9
5a83369
 
36ed450
5a83369
ff36d65
50124a7
5a83369
 
 
3f78502
 
 
 
1c7ce9d
3f78502
 
 
 
4d09659
 
 
 
5a83369
dd8c015
5a83369
 
1c7ce9d
5a83369
 
c909baf
5a83369
9eec184
67e347f
dd8c015
 
67e347f
 
dd8c015
8baaa6e
67e347f
 
2b5cc0f
5a83369
dd8c015
 
 
 
5a83369
dd8c015
9eec184
5a83369
 
f44aac9
 
 
c909baf
 
 
 
 
 
 
2b5cc0f
 
 
 
 
 
 
 
67e347f
 
 
 
 
 
 
 
 
98631d7
41e12b2
67e347f
9eec184
 
67e347f
07636bb
 
9eec184
67e347f
98631d7
 
1ccc715
 
67e347f
 
 
07636bb
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7575503
 
 
 
07636bb
ff36d65
07636bb
 
 
 
ff36d65
07636bb
 
9eec184
 
96da637
 
5a83369
 
dd8c015
5a83369
 
 
e50180d
5a83369
 
 
 
dd8c015
 
 
 
5a83369
 
 
 
 
 
36ed450
 
 
1c7ce9d
36ed450
 
 
dd8c015
36ed450
 
6d9770a
 
 
36ed450
 
 
 
 
 
 
 
 
 
 
 
 
dd8c015
 
 
 
 
 
 
36ed450
dd8c015
1c7ce9d
dd8c015
36ed450
 
1c7ce9d
f44aac9
 
25af88b
f44aac9
 
 
 
dd8c015
f44aac9
 
 
490a71e
 
f44aac9
 
 
 
902a11f
 
 
1c7ce9d
902a11f
 
 
 
 
dd8c015
902a11f
 
 
 
 
490a71e
902a11f
490a71e
 
dd8c015
902a11f
 
 
 
 
f44aac9
 
 
f5031de
f44aac9
 
 
dd8c015
 
 
 
 
f44aac9
 
 
dd8c015
f44aac9
 
 
 
 
5a83369
 
 
1c7ce9d
5a83369
 
 
 
 
 
 
 
 
 
 
8fb1ae9
d659d2d
52c63cf
ff36d65
d659d2d
ff36d65
d659d2d
52c63cf
aa61566
8fb1ae9
 
 
1afd4af
 
 
1c7ce9d
1afd4af
 
 
1c7ce9d
1afd4af
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1ec322d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
6d9770a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9eec184
3b181a1
 
 
9eec184
3b181a1
 
e25f6ac
 
 
 
 
 
 
 
92ccffa
ff36d65
e25f6ac
 
 
 
 
 
 
 
 
 
 
7575503
e25f6ac
 
 
 
 
 
 
 
d659d2d
41ae6a8
151c180
41ae6a8
 
 
 
 
d659d2d
 
 
52c63cf
41ae6a8
151c180
41ae6a8
 
 
 
 
52c63cf
41ae6a8
 
 
 
41e12b2
dd8c015
41ae6a8
dd8c015
1ccc715
 
 
41ae6a8
151c180
 
 
 
 
 
41ae6a8
151c180
 
41ae6a8
41e12b2
41ae6a8
1ccc715
 
41ae6a8
41e12b2
1ccc715
 
41ae6a8
dd8c015
ff36d65
41ae6a8
 
52c63cf
 
18b8de2
dd8c015
18b8de2
8c014b8
dd8c015
8c014b8
 
 
 
18b8de2
 
 
 
 
 
 
 
 
 
 
8c014b8
 
 
18b8de2
8c014b8
 
 
18b8de2
dd8c015
8c014b8
 
 
 
18b8de2
 
 
 
 
 
5a83369
 
d659d2d
 
8fb1ae9
 
 
 
 
 
 
18b8de2
 
 
5a83369
 
f44aac9
 
 
 
 
 
 
 
9219266
3b181a1
 
 
 
 
 
 
 
 
 
dd8c015
 
 
 
 
 
 
 
 
 
36ed450
 
 
 
9219266
 
 
 
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
const form = document.querySelector("#turn-form");
const atlasView = document.querySelector("#atlas-view");
const advisorView = document.querySelector("#advisor-view");
const openAdvisorButton = document.querySelector("#open-advisor");
const openAtlasButton = document.querySelector("#open-atlas");
const refreshDashboardButton = document.querySelector("#refresh-dashboard");
const atlasStatusEl = document.querySelector("#atlas-status");
const atlasSearchForm = document.querySelector("#atlas-search-form");
const atlasSearchInput = document.querySelector("#atlas-search");
const atlasSearchClearButton = document.querySelector("#atlas-search-clear");
const atlasSearchSectionEl = document.querySelector("#atlas-search-section");
const atlasSearchSummaryEl = document.querySelector("#atlas-search-summary");
const atlasSearchResultsEl = document.querySelector("#atlas-search-results");
const atlasStatsEl = document.querySelector("#atlas-stats");
const atlasClustersEl = document.querySelector("#atlas-clusters");
const atlasQuestsEl = document.querySelector("#atlas-quests");
const atlasSvgEl = document.querySelector("#atlas-svg");
const atlasDetailEl = document.querySelector("#atlas-detail");
const atlasReportEl = document.querySelector("#atlas-report");
const atlasRefreshProgressEl = document.querySelector("#atlas-refresh-progress");
const input = document.querySelector("#message");
const submit = document.querySelector("#submit");
const ink = document.querySelector("#ink");
const corrections = document.querySelector("#corrections");
const projectsEl = document.querySelector("#projects");
const whitespaceEl = document.querySelector("#whitespace");
const ideasEl = document.querySelector("#ideas");
const goalsEl = document.querySelector("#goals");
const profileEl = document.querySelector("#profile");
const woodMapEl = document.querySelector("#wood-map");
const scoreEl = document.querySelector("#score");
const planEl = document.querySelector("#plan");
const provenanceEl = document.querySelector("#provenance");
const verdictEl = document.querySelector("#verdict");
const overallEl = document.querySelector("#overall");
const sealEl = document.querySelector("#seal");
const sealVerdictEl = document.querySelector("#seal-verdict");
const sealCopyEl = document.querySelector("#seal-copy");
const verdictStampEl = document.querySelector("#verdict-stamp");
const spreadEl = document.querySelector("#spread");
const ideaCountEl = document.querySelector("#idea-count");
const goalCountEl = document.querySelector("#goal-count");
const demoButton = document.querySelector("#load-demo");
const exportButton = document.querySelector("#export-artifact");
const exportNotesButton = document.querySelector("#export-notes");
const exportChapterButton = document.querySelector("#export-chapter");
const resetButton = document.querySelector("#reset-session");
const recordVoiceButton = document.querySelector("#record-voice");
const uploadVoiceButton = document.querySelector("#upload-voice");
const voiceFileInput = document.querySelector("#voice-file");
const turnProgressEl = document.querySelector("#turn-progress");
const turnStageIconEl = document.querySelector("#turn-stage-icon");
const turnStageTextEl = document.querySelector("#turn-stage-text");
const turnTokensEl = document.querySelector("#turn-tokens");
const turnEtaEl = document.querySelector("#turn-eta");
const turnBarFillEl = document.querySelector("#turn-bar-fill");
const toolChipsEl = document.querySelector("#tool-chips");

const SESSION_STORAGE_KEY = "hackathon-advisor-session-v2";
const STAGE_ICONS = { planning: "🪶", running_tool: "🔧", writing: "✍️" };
const FIELD_NOTES_FILENAME = "hackathon-advisor-field-notes.md";
const CHAPTER_FILENAME = "hackathon-advisor-chapter.md";
const PNG_EXPORT_LABEL = "PNG";

let session = {};
let currentArtifact = null;
let goalOptions = [];
let goalProfiles = [];
let goalProfileById = new Map();
let profileFields = [];
let turnWatchdog = null;
let sawTurnToken = false;
let bootstrapData = null;
let sessionRevision = 0;
let sessionControlsLocked = false;
let voiceBusy = false;
let voiceRecorder = null;
let voiceStream = null;
let voiceChunks = [];
let voiceRecordingState = "idle";
let decodeStartedAt = 0;
let turnProgressTimer = null;
let dashboardData = null;
const SELF_PROJECT_ID = "build-small-hackathon/hackathon-advisor";
let selectedClusterId = "";
let selectedQuestId = "";
let selectedProjectId = "";
let dashboardRefreshTimer = null;
let atlasSearchQuery = "";
let atlasSearchResults = [];
let atlasSearchResultIds = new Set();
let atlasSearchTimer = null;
let atlasSearchController = null;
let atlasSearchUnavailable = false;
let atlasSearchBusy = false;

setVoiceRecordingState("idle");
setupViewRouting();
loadDashboard().catch(handleDashboardError);
bootstrap().catch(handleBootstrapError);

form.addEventListener("submit", async (event) => {
  event.preventDefault();
  if (sessionControlsLocked || submit.disabled || input.disabled) return;
  const message = input.value.trim();
  if (!message) return;
  await runTurn(message);
});

input.addEventListener("keydown", (event) => {
  if (event.key !== "Enter" || event.shiftKey) return;
  event.preventDefault();
  form.requestSubmit();
});

document.querySelectorAll(".mobile-nav [data-tab]").forEach((button) => {
  button.addEventListener("click", () => setActiveTab(button.dataset.tab || "page"));
});

document.querySelectorAll("[data-command]").forEach((button) => {
  button.addEventListener("click", async () => {
    await runCommand(button.dataset.command || "");
  });
});

setupMenus();

demoButton.addEventListener("click", async () => {
  await loadDemoSession();
});

exportButton.addEventListener("click", () => {
  if (!currentArtifact) return;
  exportArtifact(currentArtifact);
});

exportNotesButton.addEventListener("click", () => exportNotes());

exportChapterButton.addEventListener("click", () => exportChapter());

resetButton.addEventListener("click", () => {
  resetSession();
});

openAdvisorButton?.addEventListener("click", () => {
  window.location.hash = "advisor";
});

openAtlasButton?.addEventListener("click", () => {
  window.location.hash = "atlas";
});

refreshDashboardButton?.addEventListener("click", async () => {
  await startDashboardRefresh();
});

atlasSearchForm?.addEventListener("submit", (event) => {
  event.preventDefault();
  runAtlasSearch(atlasSearchInput?.value || "");
});

atlasSearchInput?.addEventListener("input", () => {
  scheduleAtlasSearch(atlasSearchInput.value || "");
});

atlasSearchClearButton?.addEventListener("click", () => {
  clearAtlasSearch();
});

recordVoiceButton.addEventListener("click", async () => {
  await toggleVoiceRecording();
});

uploadVoiceButton.addEventListener("click", () => {
  if (uploadVoiceButton.disabled || voiceBusy || sessionControlsLocked || voiceRecordingState !== "idle") return;
  voiceFileInput.click();
});

voiceFileInput.addEventListener("change", async () => {
  const file = voiceFileInput.files?.[0] || null;
  voiceFileInput.value = "";
  if (!file) return;
  await transcribeVoiceBlob(file, file.name || "voice-note.audio");
});

goalsEl.addEventListener("change", (event) => {
  const target = event.target;
  if (!(target instanceof HTMLInputElement) || !target.dataset.goal) return;
  bumpSessionRevision();
  const checked = new Set(
    Array.from(goalsEl.querySelectorAll("input[data-goal]:checked")).map((input) => input.dataset.goal),
  );
  session.goals = goalOptions.filter((option) => checked.has(option));
  syncCurrentIdeaGoals();
  invalidateCurrentSeal("Goals updated. Press Ink or Plan to refresh the score.");
  saveSession();
  renderGoals(session.goals);
  renderIdeas(session.ideas || []);
});

profileEl.addEventListener("input", (event) => {
  const target = event.target;
  if (!(target instanceof HTMLInputElement) || !target.dataset.profileField) return;
  bumpSessionRevision();
  const profile = { ...(session.profile || {}) };
  const value = target.value.trim();
  if (value) {
    profile[target.dataset.profileField] = value;
  } else {
    delete profile[target.dataset.profileField];
  }
  session.profile = profile;
  invalidateCurrentPlan("Profile updated. Press Plan to refresh the build path.");
  saveSession();
});

ideasEl.addEventListener("click", (event) => {
  const card = event.target.closest("[data-idea-id]");
  if (!(card instanceof HTMLElement) || !ideasEl.contains(card)) return;
  selectIdea(card.dataset.ideaId || "");
});

whitespaceEl.addEventListener("click", async (event) => {
  const card = event.target.closest("[data-gap-prompt]");
  if (!(card instanceof HTMLButtonElement) || !whitespaceEl.contains(card)) return;
  if (card.disabled) return;
  await runTurn(card.dataset.gapPrompt || "");
});

function setupViewRouting() {
  window.addEventListener("hashchange", applyCurrentView);
  applyCurrentView();
}

function applyCurrentView() {
  const view = window.location.hash.replace(/^#/, "") === "advisor" ? "advisor" : "atlas";
  document.body.dataset.view = view;
  if (atlasView) atlasView.hidden = view !== "atlas";
  if (advisorView) advisorView.hidden = view !== "advisor";
  if (view === "advisor" && input && !sessionControlsLocked) {
    window.setTimeout(() => input.focus(), 30);
  }
}

async function loadDashboard() {
  const response = await fetch("/api/dashboard");
  if (!response.ok) throw new Error(`dashboard failed with ${response.status}`);
  const data = await response.json();
  dashboardData = data;
  renderDashboard(data);
  renderRefreshState(data.refresh || {});
  if (data.refresh?.status === "running") scheduleRefreshPoll();
}

function handleDashboardError(error) {
  console.error("Atlas could not load.", error);
  dashboardData = null;
  if (atlasStatusEl) atlasStatusEl.textContent = "Atlas could not load.";
  if (atlasSvgEl) atlasSvgEl.innerHTML = "";
  if (atlasStatsEl) atlasStatsEl.innerHTML = "";
  if (atlasDetailEl) atlasDetailEl.innerHTML = `<p>Reload the page to try again.</p>`;
}

async function startDashboardRefresh() {
  if (!refreshDashboardButton || refreshDashboardButton.disabled) return;
  refreshDashboardButton.disabled = true;
  if (atlasStatusEl) atlasStatusEl.textContent = "Starting refresh.";
  try {
    const response = await fetch("/api/dashboard/refresh", { method: "POST" });
    const data = await response.json();
    if (!response.ok) throw new Error(data.detail || `refresh failed with ${response.status}`);
    renderRefreshState(data);
    scheduleRefreshPoll();
  } catch (error) {
    console.error("Dashboard refresh could not start.", error);
    if (atlasStatusEl) atlasStatusEl.textContent = "Refresh could not start.";
    if (atlasRefreshProgressEl) atlasRefreshProgressEl.hidden = true;
    refreshDashboardButton.disabled = false;
  }
}

function scheduleRefreshPoll() {
  if (dashboardRefreshTimer) window.clearTimeout(dashboardRefreshTimer);
  dashboardRefreshTimer = window.setTimeout(pollDashboardRefresh, 1400);
}

async function pollDashboardRefresh() {
  try {
    const response = await fetch("/api/dashboard/refresh");
    if (!response.ok) throw new Error(`refresh status failed with ${response.status}`);
    const state = await response.json();
    renderRefreshState(state);
    if (state.status === "running") {
      scheduleRefreshPoll();
      return;
    }
    if (state.status === "succeeded") {
      await loadDashboard();
    }
  } catch (error) {
    console.error("Dashboard refresh status unavailable.", error);
    if (atlasStatusEl) atlasStatusEl.textContent = "Refresh status unavailable.";
  } finally {
    if (_refreshIsSettled()) refreshDashboardButton.disabled = false;
  }
}

function _refreshIsSettled() {
  const status = String(dashboardData?.refresh?.status || "");
  return status !== "running";
}

function renderRefreshState(state) {
  if (dashboardData) dashboardData.refresh = state || {};
  const status = String(state?.status || "idle");
  const stage = state?.stage_label || state?.stage || "";
  if (atlasStatusEl) {
    if (status === "running") {
      atlasStatusEl.textContent = stage ? `Refresh running: ${stage}.` : "Refresh running.";
    } else if (status === "succeeded") {
      atlasStatusEl.textContent = `Atlas refreshed: ${state.result?.project_count || "current"} projects mapped.`;
    } else if (status === "failed") {
      if (state.error) console.error("Dashboard refresh failed.", state.error);
      atlasStatusEl.textContent = "Refresh did not complete; current map is unchanged.";
    } else if (dashboardData) {
      atlasStatusEl.textContent = atlasSearchQuery ? atlasSearchStatusCopy() : atlasProvenanceCopy(dashboardData);
    }
  }
  if (atlasRefreshProgressEl) {
    const show = status === "running";
    const cacheCopy = refreshQuestCacheCopy(state?.quest_cache || {});
    atlasRefreshProgressEl.hidden = !show;
    atlasRefreshProgressEl.textContent =
      status === "running"
        ? `${stage || "Working"}${cacheCopy ? ` · ${cacheCopy}` : ""} · run ${state.run_id || ""}`
        : "";
  }
  if (refreshDashboardButton) refreshDashboardButton.disabled = status === "running";
}

function refreshQuestCacheCopy(cache) {
  const total = Number(cache.project_count || 0);
  if (!total) return "";
  const hits = Number(cache.hit_count || 0);
  const misses = Number(cache.miss_count || 0);
  const analyzed = Number(cache.analyzed_count || 0);
  const remaining = Number(cache.remaining_count || 0);
  if (!hits && !misses && !analyzed) return "";
  if (remaining > 0) return `${hits} cached, ${analyzed}/${misses} analyzed`;
  return `${hits} cached, ${analyzed} analyzed`;
}

function scheduleAtlasSearch(rawQuery) {
  const query = String(rawQuery || "").trim();
  if (atlasSearchTimer) window.clearTimeout(atlasSearchTimer);
  if (!query) {
    clearAtlasSearch();
    return;
  }
  atlasSearchTimer = window.setTimeout(() => runAtlasSearch(query), 260);
}

async function runAtlasSearch(rawQuery) {
  const query = String(rawQuery || "").trim();
  if (!query) {
    clearAtlasSearch();
    return;
  }
  atlasSearchQuery = query;
  atlasSearchUnavailable = false;
  atlasSearchBusy = true;
  renderAtlasSearch();
  if (atlasSearchController) atlasSearchController.abort();
  atlasSearchController = new AbortController();
  try {
    const response = await fetch(`/api/dashboard/search?q=${encodeURIComponent(query)}&limit=12`, {
      signal: atlasSearchController.signal,
    });
    if (!response.ok) throw new Error(`search failed with ${response.status}`);
    const payload = await response.json();
    if (query !== String(atlasSearchInput?.value || "").trim()) return;
    atlasSearchResults = payload.results || [];
    atlasSearchResultIds = new Set(atlasSearchResults.map((result) => result.project_id).filter(Boolean));
    atlasSearchUnavailable = false;
    atlasSearchBusy = false;
    if (atlasSearchResults.length) selectedProjectId = atlasSearchResults[0].project_id || selectedProjectId;
    if (dashboardData) renderDashboard(dashboardData);
  } catch (error) {
    if (error.name === "AbortError") return;
    console.error("Atlas search failed.", error);
    atlasSearchResults = [];
    atlasSearchResultIds = new Set();
    atlasSearchUnavailable = true;
    atlasSearchBusy = false;
    if (dashboardData) renderDashboard(dashboardData);
  }
}

function clearAtlasSearch() {
  if (atlasSearchTimer) window.clearTimeout(atlasSearchTimer);
  atlasSearchTimer = null;
  if (atlasSearchController) atlasSearchController.abort();
  atlasSearchController = null;
  atlasSearchQuery = "";
  atlasSearchResults = [];
  atlasSearchResultIds = new Set();
  atlasSearchUnavailable = false;
  atlasSearchBusy = false;
  if (atlasSearchInput) atlasSearchInput.value = "";
  if (dashboardData) renderDashboard(dashboardData);
}

function atlasSearchStatusCopy() {
  if (!atlasSearchQuery) return dashboardData ? atlasProvenanceCopy(dashboardData) : "";
  if (atlasSearchBusy) return "Searching.";
  if (atlasSearchUnavailable) return "Search unavailable.";
  if (!atlasSearchResults.length) return `No matches for "${atlasSearchQuery}".`;
  return `${atlasSearchResults.length} matches for "${atlasSearchQuery}".`;
}

function renderAtlasSearch() {
  if (!atlasSearchSectionEl || !atlasSearchResultsEl || !atlasSearchSummaryEl) return;
  const active = Boolean(atlasSearchQuery);
  atlasSearchSectionEl.hidden = !active;
  if (atlasSearchClearButton) atlasSearchClearButton.hidden = !active;
  if (!active) {
    atlasSearchResultsEl.innerHTML = "";
    atlasSearchSummaryEl.textContent = "";
    return;
  }
  atlasSearchSummaryEl.textContent = atlasSearchStatusCopy();
  atlasSearchResultsEl.innerHTML = "";
  if (atlasSearchUnavailable || !atlasSearchResults.length) return;
  for (const result of atlasSearchResults.slice(0, 8)) {
    atlasSearchResultsEl.append(atlasSearchResultButton(result));
  }
}

function atlasSearchResultButton(result) {
  const button = document.createElement("button");
  button.type = "button";
  button.className = `atlas-search-result ${result.project_id === selectedProjectId ? "active" : ""}`;
  const title = result.title || result.project?.title || result.project_id || "Untitled project";
  const terms = (result.matched_terms || []).slice(0, 4).join(", ");
  const snippet = (result.snippets || [])[0];
  const width = Math.max(8, Math.min(100, Number(result.score || 0) * 100)).toFixed(0);
  button.innerHTML = `
    <strong>${escapeHtml(title)}</strong>
    <span class="atlas-search-meta">${escapeHtml(terms || "Related project")}</span>
    <span class="atlas-search-score" aria-hidden="true"><i style="width: ${width}%"></i></span>
    ${
      snippet
        ? `<span class="atlas-search-snippet">${escapeHtml(snippet.source)}: ${escapeHtml(snippet.text)}</span>`
        : ""
    }
  `;
  button.addEventListener("click", () => {
    selectedProjectId = result.project_id || selectedProjectId;
    if (dashboardData) renderDashboard(dashboardData);
  });
  return button;
}

function renderDashboard(data) {
  if (!data?.points?.length) {
    handleDashboardError(new Error("empty dashboard payload"));
    return;
  }
  if (!selectedProjectId) {
    const selfPoint = data.points.find((point) => point.id === SELF_PROJECT_ID);
    selectedProjectId = selfPoint?.id || mostLikedPoint(data.points)?.id || data.points[0].id;
  }
  renderAtlasStats(data);
  renderAtlasClusters(data);
  renderAtlasQuests(data);
  renderAtlasSvg(data);
  renderAtlasDetail(currentAtlasPoint(data));
  renderAtlasReport(data);
  renderAtlasSearch();
  if (atlasStatusEl) atlasStatusEl.textContent = atlasSearchQuery ? atlasSearchStatusCopy() : atlasProvenanceCopy(data);
}

function atlasProvenanceCopy(data) {
  const count = Number(data.project_count || data.points?.length || 0);
  const updated = shortDate(data.provenance?.snapshot_generated_at || data.generated_at);
  return `${count} projects mapped · ${data.layout?.algorithm || "layout"} · updated ${updated}`;
}

function renderAtlasStats(data) {
  if (!atlasStatsEl) return;
  const analyzed = data.quest_report?.status === "analyzed";
  const questCount = (data.quest_report?.quests || []).filter((quest) => Number(quest.project_count || 0) > 0).length;
  atlasStatsEl.innerHTML = `
    <div class="atlas-stat"><strong>${Number(data.project_count || 0)}</strong><span>Projects</span></div>
    <div class="atlas-stat"><strong>${Number(data.clusters?.length || 0)}</strong><span>Clusters</span></div>
    <div class="atlas-stat"><strong>${Number(data.links?.length || 0)}</strong><span>Near links</span></div>
    <div class="atlas-stat"><strong>${analyzed ? questCount : "..."}</strong><span>Quest groups</span></div>
  `;
}

function renderAtlasClusters(data) {
  if (!atlasClustersEl) return;
  const allActive = !selectedClusterId;
  atlasClustersEl.innerHTML = "";
  atlasClustersEl.append(
    atlasFilterButton({
      label: "All clusters",
      meta: `${data.project_count || data.points.length} projects`,
      active: allActive,
      onClick: () => {
        selectedClusterId = "";
        renderDashboard(data);
      },
    }),
  );
  for (const cluster of data.clusters || []) {
    atlasClustersEl.append(
      atlasFilterButton({
        label: cluster.label || cluster.id,
        meta: `${cluster.project_count || 0} projects`,
        active: selectedClusterId === cluster.id,
        onClick: () => {
          selectedClusterId = selectedClusterId === cluster.id ? "" : cluster.id;
          renderDashboard(data);
        },
      }),
    );
  }
}

function renderAtlasQuests(data) {
  if (!atlasQuestsEl) return;
  const quests = data.quest_report?.quests || [];
  const analyzed = data.quest_report?.status === "analyzed";
  atlasQuestsEl.innerHTML = "";
  atlasQuestsEl.append(
    atlasFilterButton({
      label: "All quests",
      meta: analyzed ? "No quest filter" : "Refresh to analyze",
      active: !selectedQuestId,
      onClick: () => {
        selectedQuestId = "";
        renderDashboard(data);
      },
    }),
  );
  for (const quest of quests) {
    atlasQuestsEl.append(
      atlasFilterButton({
        label: quest.label || quest.id,
        meta: analyzed ? `${quest.project_count || 0} projects` : "Not analyzed",
        active: selectedQuestId === quest.id,
        onClick: () => {
          selectedQuestId = selectedQuestId === quest.id ? "" : quest.id;
          renderDashboard(data);
        },
      }),
    );
  }
}

function atlasFilterButton({ label, meta, active, onClick }) {
  const button = document.createElement("button");
  button.type = "button";
  button.className = `atlas-filter ${active ? "active" : ""}`;
  button.innerHTML = `<strong>${escapeHtml(label)}</strong><span>${escapeHtml(meta || "")}</span>`;
  button.addEventListener("click", onClick);
  return button;
}

function renderAtlasSvg(data) {
  if (!atlasSvgEl) return;
  atlasSvgEl.innerHTML = "";
  const pointsById = new Map((data.points || []).map((point) => [point.id, point]));
  const visible = new Set(visibleAtlasPoints(data).map((point) => point.id));
  const clusterIndex = new Map((data.clusters || []).map((cluster, index) => [cluster.id, index]));

  for (const link of data.links || []) {
    const source = pointsById.get(link.source);
    const target = pointsById.get(link.target);
    if (!source || !target) continue;
    const line = svgEl("line");
    line.setAttribute("x1", source.x);
    line.setAttribute("y1", source.y);
    line.setAttribute("x2", target.x);
    line.setAttribute("y2", target.y);
    line.setAttribute("class", `atlas-link ${visible.has(source.id) && visible.has(target.id) ? "" : "dim"}`);
    atlasSvgEl.append(line);
  }

  for (const point of data.points || []) {
    if (!atlasSearchResultIds.has(point.id)) continue;
    const ring = svgEl("circle");
    ring.setAttribute("cx", point.x);
    ring.setAttribute("cy", point.y);
    ring.setAttribute("r", (atlasPointRadiusNumber(point) + 0.62).toFixed(3));
    ring.setAttribute("class", `atlas-search-ring ${visible.has(point.id) ? "" : "dim"}`);
    atlasSvgEl.append(ring);
  }

  for (const point of data.points || []) {
    const circle = svgEl("circle");
    circle.setAttribute("cx", point.x);
    circle.setAttribute("cy", point.y);
    circle.setAttribute("r", atlasPointRadius(point));
    circle.setAttribute("fill", atlasColor(clusterIndex.get(point.cluster_id) || 0));
    circle.setAttribute(
      "class",
      `atlas-dot ${visible.has(point.id) ? "" : "dim"} ${point.id === selectedProjectId ? "selected" : ""} ${
        atlasSearchResultIds.has(point.id) ? "search-match" : ""
      }`,
    );
    circle.setAttribute("tabindex", "0");
    circle.setAttribute("role", "button");
    circle.setAttribute("aria-label", point.title || point.id);
    circle.addEventListener("mouseenter", () => renderAtlasDetail(point));
    circle.addEventListener("focus", () => renderAtlasDetail(point));
    circle.addEventListener("click", () => {
      selectedProjectId = point.id;
      renderDashboard(data);
    });
    circle.append(svgTitle(point.title || point.id));
    atlasSvgEl.append(circle);
  }

  for (const point of labelAtlasPoints(data)) {
    const text = svgEl("text");
    text.setAttribute("x", boundedPercent(point.x + 1.4));
    text.setAttribute("y", boundedPercent(point.y - 1.1));
    text.setAttribute("class", "atlas-label");
    text.textContent = atlasShortTitle(point.title || point.id);
    atlasSvgEl.append(text);
  }
}

function visibleAtlasPoints(data) {
  return (data.points || []).filter((point) => {
    const clusterMatch = !selectedClusterId || point.cluster_id === selectedClusterId;
    const questMatch = !selectedQuestId || (point.quest_ids || []).includes(selectedQuestId);
    const searchMatch = !atlasSearchQuery || atlasSearchResultIds.has(point.id);
    return clusterMatch && questMatch && searchMatch;
  });
}

function labelAtlasPoints(data) {
  if (atlasSearchQuery && atlasSearchResults.length) {
    const pointsById = new Map((data.points || []).map((point) => [point.id, point]));
    const visibleIds = new Set(visibleAtlasPoints(data).map((point) => point.id));
    return atlasSearchResults
      .map((result) => pointsById.get(result.project_id))
      .filter(Boolean)
      .filter((point) => visibleIds.has(point.id))
      .slice(0, 16);
  }
  const visible = visibleAtlasPoints(data);
  return [...visible].sort((left, right) => Number(right.likes || 0) - Number(left.likes || 0)).slice(0, 12);
}

function currentAtlasPoint(data) {
  return (data.points || []).find((point) => point.id === selectedProjectId) || mostLikedPoint(data.points || []);
}

function mostLikedPoint(points) {
  return [...(points || [])].sort((left, right) => Number(right.likes || 0) - Number(left.likes || 0))[0] || null;
}

function renderAtlasDetail(point) {
  if (!atlasDetailEl) return;
  if (!point) {
    atlasDetailEl.innerHTML = `<p>Select a project dot to inspect its cluster and quest matches.</p>`;
    return;
  }
  const quests = (point.quest_matches || [])
    .map((match) => {
      const confidence = (Number(match.confidence) * 100).toFixed(0);
      const label = atlasQuestLabel(match.quest);
      const hint = questBadgeHint(match, label, confidence);
      return (
        `<span title="${escapeAttribute(hint)}" aria-label="${escapeAttribute(hint)}">` +
        `${escapeHtml(label)} ${confidence}%</span>`
      );
    })
    .join("");
  const tags = [...(point.models || []).slice(0, 3), ...visibleProjectTags(point.tags || []).slice(0, 3)]
    .map((tag) => `<span>${escapeHtml(tag)}</span>`)
    .join("");
  atlasDetailEl.innerHTML = `
    <h2>${escapeHtml(point.title || "Untitled project")}</h2>
    ${point.summary ? `<p>${escapeHtml(point.summary)}</p>` : `<p>${escapeHtml(point.id || "")}</p>`}
    <p>${Number(point.likes || 0)} likes · ${escapeHtml(point.sdk || "unknown sdk")}</p>
    <p><a href="${escapeAttribute(point.url || "#")}" target="_blank" rel="noreferrer">Open Space</a></p>
    <div class="atlas-tags">${quests || `<span>Quest analysis pending</span>`}</div>
    <div class="atlas-tags">${tags}</div>
  `;
}

function questBadgeHint(match, label, confidence) {
  const evidence = String(match?.evidence || "").trim();
  const source = questEvidenceSourceLabel(match?.source);
  const parts = [`${label} ${confidence}% confidence`];
  if (evidence) parts.push(`${source}: ${evidence}`);
  return parts.join(". ");
}

function questEvidenceSourceLabel(source) {
  const normalized = String(source || "").trim().toLowerCase();
  if (normalized === "readme") return "README evidence";
  if (normalized === "app_file") return "App file evidence";
  return "Evidence";
}

function visibleProjectTags(tags) {
  return (tags || []).filter((tag) => !String(tag || "").toLowerCase().startsWith("region:"));
}

function renderAtlasReport(data) {
  if (!atlasReportEl) return;
  const cluster = selectedClusterId
    ? (data.clusters || []).find((item) => item.id === selectedClusterId)
    : (data.clusters || [])[0];
  if (!cluster) {
    atlasReportEl.innerHTML = `<p>No cluster report is available.</p>`;
    return;
  }
  const projects = (cluster.representative_projects || [])
    .map(
      (project) =>
        `<p><a href="${escapeAttribute(project.url || "#")}" target="_blank" rel="noreferrer">` +
        `${escapeHtml(project.title || project.id)}</a></p>`,
    )
    .join("");
  atlasReportEl.innerHTML = `
    <h2>${escapeHtml(cluster.label || cluster.id)}</h2>
    <p>${Number(cluster.project_count || 0)} projects · ${escapeHtml(
      (cluster.keywords || []).join(", ") || "mixed signals",
    )}</p>
    ${projects}
  `;
}

function svgEl(tagName) {
  return document.createElementNS("http://www.w3.org/2000/svg", tagName);
}

function svgTitle(text) {
  const title = svgEl("title");
  title.textContent = text;
  return title;
}

function atlasColor(index) {
  const palette = [
    "#9a2b22",
    "#b07d12",
    "#2f6b41",
    "#6f4b1d",
    "#3f8453",
    "#74201b",
    "#8a714c",
    "#d8a226",
    "#5d4528",
    "#7c6849",
  ];
  return palette[index % palette.length];
}

function atlasQuestLabel(questId) {
  const quest = (dashboardData?.quest_report?.quests || []).find((item) => item.id === questId);
  return quest?.label || questId;
}

function atlasPointRadius(point) {
  return atlasPointRadiusNumber(point).toFixed(3);
}

function atlasPointRadiusNumber(point) {
  return 0.62 + Math.min(0.72, Math.sqrt(Number(point.likes || 0)) * 0.12);
}

function atlasShortTitle(title) {
  const cleaned = String(title || "").trim();
  return cleaned.length > 22 ? `${cleaned.slice(0, 20).trim()}...` : cleaned;
}

async function runTurn(message) {
  if (sessionControlsLocked) return false;
  bumpSessionRevision();
  setActiveTab("page");
  input.value = "";
  submit.disabled = true;
  setCommandDisabled(true);
  setSessionControlsDisabled(true);
  ink.classList.remove("bleed", "gold");
  corrections.textContent = "";
  planEl.innerHTML = "";
  delete session.ui_status;
  resetTurnProgress();
  startTurnWatchdog();

  let completed = false;
  try {
    const response = await fetch("/api/agent-turn", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({
        message,
        session_json: JSON.stringify(session),
      }),
    });
    if (!response.ok) throw new Error(`advisor failed with ${response.status}`);
    if (!response.body) throw new Error("advisor stream was empty");

    for await (const raw of readNdjson(response.body)) {
      handleEvent(JSON.parse(raw));
    }
    completed = true;
  } catch (error) {
    clearTurnWatchdog();
    ink.textContent = `The advisor could not answer: ${error.message}`;
    ink.classList.remove("thinking");
    ink.classList.add("bleed");
  } finally {
    clearTurnWatchdog();
    hideTurnProgress();
    submit.disabled = false;
    setSessionControlsDisabled(false);
    setCommandDisabled(false);
    input.focus();
  }
  return completed;
}

async function* readNdjson(stream) {
  const reader = stream.getReader();
  const decoder = new TextDecoder();
  let buffer = "";

  while (true) {
    const { value, done } = await reader.read();
    if (done) break;
    buffer += decoder.decode(value, { stream: true });
    let newlineIndex = buffer.indexOf("\n");
    while (newlineIndex >= 0) {
      const line = buffer.slice(0, newlineIndex).trim();
      buffer = buffer.slice(newlineIndex + 1);
      if (line) yield line;
      newlineIndex = buffer.indexOf("\n");
    }
  }

  buffer += decoder.decode();
  const finalLine = buffer.trim();
  if (finalLine) yield finalLine;
}

async function runCommand(command) {
  if (!command) return;
  const draft = input.value.trim();
  if (draft) {
    const savedDraft = await runTurn(draft);
    if (!savedDraft) return;
  }
  await runTurn(command);
}

async function toggleVoiceRecording() {
  if (voiceRecordingState === "recording" && voiceRecorder?.state === "recording") {
    stopVoiceRecording();
    return;
  }
  if (voiceRecordingState !== "idle") return;
  await startVoiceRecording();
}

async function startVoiceRecording() {
  if (!bootstrapData || sessionControlsLocked || voiceBusy || voiceRecordingState !== "idle") return;
  if (!navigator.mediaDevices?.getUserMedia || !window.MediaRecorder) {
    setSessionStatus("Voice recording is not available in this browser. Upload a voice note instead.");
    return;
  }
  setVoiceRecordingState("starting");
  submit.disabled = true;
  setCommandDisabled(true);
  try {
    voiceStream = await navigator.mediaDevices.getUserMedia({ audio: true });
    voiceChunks = [];
    const mimeType = recordingMimeType();
    voiceRecorder = new MediaRecorder(voiceStream, mimeType ? { mimeType } : undefined);
    voiceRecorder.addEventListener("dataavailable", (event) => {
      if (event.data?.size) voiceChunks.push(event.data);
    });
    voiceRecorder.addEventListener("stop", () => {
      const recorderMimeType = voiceRecorder?.mimeType || mimeType || "audio/webm";
      const recordedChunks = voiceChunks;
      stopVoiceStream();
      const extension = recorderMimeType.includes("mp4")
        ? "m4a"
        : recorderMimeType.includes("ogg")
          ? "ogg"
          : "webm";
      const blob = new Blob(recordedChunks, { type: recorderMimeType });
      voiceRecorder = null;
      voiceChunks = [];
      if (!blob.size) {
        setVoiceRecordingState("idle");
        submit.disabled = false;
        setCommandDisabled(false);
        setSessionStatus("Voice note is empty.");
        return;
      }
      setVoiceRecordingState("transcribing");
      transcribeVoiceBlob(blob, `recorded-idea.${extension}`);
    });
    voiceRecorder.start();
    setVoiceRecordingState("recording");
    setSessionStatus("Listening. Press Stop when your idea is ready.");
  } catch (error) {
    stopVoiceStream();
    voiceRecorder = null;
    voiceChunks = [];
    setVoiceRecordingState("idle");
    submit.disabled = false;
    setCommandDisabled(false);
    setSessionStatus(`Voice recording could not start: ${error.message}`);
  }
}

function stopVoiceRecording() {
  if (!voiceRecorder || voiceRecorder.state !== "recording") return;
  setVoiceRecordingState("stopping");
  setSessionStatus("Stopping recording.");
  try {
    voiceRecorder.stop();
  } catch (error) {
    stopVoiceStream();
    voiceRecorder = null;
    voiceChunks = [];
    setVoiceRecordingState("idle");
    submit.disabled = false;
    setCommandDisabled(false);
    setSessionStatus(`Voice recording could not stop: ${error.message}`);
  }
}

function recordingMimeType() {
  const candidates = ["audio/webm;codecs=opus", "audio/webm", "audio/ogg;codecs=opus", "audio/mp4"];
  return candidates.find((type) => MediaRecorder.isTypeSupported(type)) || "";
}

function stopVoiceStream() {
  if (!voiceStream) return;
  voiceStream.getTracks().forEach((track) => track.stop());
  voiceStream = null;
}

async function transcribeVoiceBlob(blob, filename) {
  if (sessionControlsLocked || voiceBusy) return false;
  if (voiceRecordingState !== "idle" && voiceRecordingState !== "transcribing") return false;
  if (!blob?.size) {
    setSessionStatus("Voice note is empty.");
    return false;
  }
  const revision = bumpSessionRevision();
  voiceBusy = true;
  setVoiceRecordingState("transcribing");
  submit.disabled = true;
  input.disabled = true;
  setCommandDisabled(true);
  setSessionControlsDisabled(true);
  setSessionStatus("Transcribing voice note.");
  try {
    const formData = new FormData();
    formData.append("audio", blob, filename || "voice-note.audio");
    const response = await fetch("/api/transcribe", {
      method: "POST",
      body: formData,
    });
    if (!response.ok) throw new Error(`voice note failed with ${response.status}`);
    const data = await response.json();
    const transcript = String(data.transcript || "").trim();
    if (!transcript) throw new Error("empty transcript");
    if (!isCurrentSessionRevision(revision)) return false;
    input.value = mergeDraftWithTranscript(input.value, transcript);
    session.ui_status = "Voice note transcribed. Edit the draft or press Ink.";
    corrections.textContent = session.ui_status;
    saveSession();
    return true;
  } catch (error) {
    if (isCurrentSessionRevision(revision)) setSessionStatus(`Voice note could not be transcribed: ${error.message}`);
    return false;
  } finally {
    voiceBusy = false;
    setVoiceRecordingState("idle");
    if (isCurrentSessionRevision(revision)) {
      submit.disabled = false;
      input.disabled = false;
      setSessionControlsDisabled(false);
      setCommandDisabled(false);
      input.focus();
    }
  }
}

function mergeDraftWithTranscript(draft, transcript) {
  const current = String(draft || "").trim();
  return current ? `${current}\n${transcript}` : transcript;
}

async function bootstrap() {
  const response = await fetch("/api/bootstrap");
  if (!response.ok) throw new Error(`project index failed with ${response.status}`);
  const data = await response.json();
  bootstrapData = data;
  const rawProfiles = Array.isArray(data.goal_profiles) ? data.goal_profiles : [];
  const rawOptions = Array.isArray(data.goal_options) ? data.goal_options : [];
  goalProfiles = normalizeGoalProfiles(rawProfiles, rawOptions);
  goalOptions = goalProfiles.map((goal) => goal.id);
  goalProfileById = new Map(goalProfiles.map((goal) => [goal.id, goal]));
  profileFields = data.profile_fields || [];
  session = normalizeSession(readSavedSession(), defaultSession(data));
  renderProvenance(data);
  renderGoals(session.goals);
  renderProfile(session.profile);
  renderRestoredSession(data);
  renderWhitespace(data.whitespace || []);
  setVoiceRecordingState("idle");
}

function handleBootstrapError(error) {
  bootstrapData = null;
  currentArtifact = null;
  session = {};
  submit.disabled = true;
  input.disabled = true;
  setCommandDisabled(true);
  setSessionControlsDisabled(true);
  ink.textContent = `The project index could not be opened: ${error.message}`;
  ink.classList.remove("thinking", "gold");
  ink.classList.add("bleed");
  corrections.textContent = "Reload the page to try again.";
  provenanceEl.textContent = "index unavailable";
  renderScore(null);
  setVerdictDisplay("INDEX CLOSED", 0, null);
  renderWoodMap(null);
  renderGoals([]);
  renderProfile({});
  renderIdeas([]);
  renderProjects([]);
  renderWhitespace([]);
  renderPlan([]);
}

function defaultSession(data = bootstrapData) {
  return {
    profile: {},
    goals: data?.default_goals || goalOptions.slice(0, 3),
  };
}

function setActiveTab(tab) {
  if (!spreadEl) return;
  const next = ["page", "proof", "almanac"].includes(tab) ? tab : "page";
  spreadEl.dataset.tab = next;
  document.querySelectorAll(".mobile-nav [data-tab]").forEach((button) => {
    button.classList.toggle("active", button.dataset.tab === next);
  });
}

function setVerdictDisplay(verdict = "READY", overall = 0, score = null) {
  const text = String(verdict || "READY");
  const isEcho = text.startsWith("ECHO");
  const isUnwritten = text.startsWith("UNWRITTEN");
  const numericOverall = Number(overall || score?.overall || 0);

  verdictEl.textContent = text;
  overallEl.textContent = numericOverall.toFixed(1);
  sealEl.classList.toggle("echo", isEcho);
  sealEl.classList.toggle("unwritten", isUnwritten);

  sealVerdictEl.textContent = text;
  sealVerdictEl.classList.toggle("echo", isEcho);
  sealVerdictEl.classList.toggle("unwritten", isUnwritten);
  sealVerdictEl.classList.toggle("ready", !isEcho && !isUnwritten);

  verdictStampEl.classList.toggle("verdict-echo", isEcho);
  verdictStampEl.classList.toggle("verdict-unwritten", isUnwritten);
  verdictStampEl.classList.toggle("verdict-ready", !isEcho && !isUnwritten);

  if (!score) {
    sealCopyEl.textContent = text === "INDEX CLOSED" ? "The project map did not load." : "No idea has been scored yet.";
  } else if (isEcho) {
    sealCopyEl.textContent = "Nearby projects already cover parts of this idea.";
  } else {
    sealCopyEl.textContent = "This idea sits in a quieter part of the current map.";
  }
}

function bumpSessionRevision() {
  sessionRevision += 1;
  return sessionRevision;
}

function isCurrentSessionRevision(revision) {
  return revision === sessionRevision;
}

function restoreExportButtonLabels() {
  setActionButtonLabel(exportNotesButton, "Notes");
  setActionButtonLabel(exportChapterButton, "Chapter");
  setActionButtonLabel(exportButton, PNG_EXPORT_LABEL);
}

function actionButtonLabel(button) {
  return button?.dataset.actionLabel || button?.textContent.trim() || "";
}

function setActionButtonLabel(button, label) {
  if (!button) return;
  button.dataset.actionLabel = label;
  const textNode = Array.from(button.childNodes).find(
    (node) => node.nodeType === Node.TEXT_NODE && node.textContent.trim(),
  );
  if (textNode) {
    textNode.textContent = ` ${label}`;
  } else {
    button.append(document.createTextNode(` ${label}`));
  }
}

function setSessionControlsDisabled(disabled) {
  sessionControlsLocked = disabled;
  goalsEl.querySelectorAll("input[data-goal]").forEach((target) => {
    target.disabled = disabled;
  });
  profileEl.querySelectorAll("input[data-profile-field]").forEach((field) => {
    field.disabled = disabled;
  });
  ideasEl.querySelectorAll("button[data-idea-id]").forEach((idea) => {
    idea.disabled = disabled;
  });
  whitespaceEl.querySelectorAll("button[data-gap-prompt]").forEach((gap) => {
    gap.disabled = disabled;
  });
  setVoiceControlsDisabled(disabled);
}

function setVoiceControlsDisabled(disabled) {
  const recording = voiceRecordingState === "recording" && voiceRecorder?.state === "recording";
  const lockedForState = ["starting", "stopping", "transcribing"].includes(voiceRecordingState);
  recordVoiceButton.disabled = !bootstrapData || voiceBusy || lockedForState || (disabled && !recording);
  uploadVoiceButton.disabled = !bootstrapData || voiceBusy || disabled || voiceRecordingState !== "idle";
}

function setVoiceRecordingState(state) {
  voiceRecordingState = state;
  recordVoiceButton.dataset.voiceState = state;
  recordVoiceButton.classList.toggle("recording", state === "recording");
  recordVoiceButton.setAttribute("aria-pressed", state === "recording" ? "true" : "false");
  const labels = {
    idle: "Speak",
    starting: "Starting...",
    recording: "Stop",
    stopping: "Stopping...",
    transcribing: "Hearing...",
  };
  setActionButtonLabel(recordVoiceButton, labels[state] || "Speak");
  setVoiceControlsDisabled(sessionControlsLocked);
}

function resetSession() {
  if (!bootstrapData) return;
  bumpSessionRevision();
  clearTurnWatchdog();
  clearSavedSession();
  session = defaultSession(bootstrapData);
  currentArtifact = null;
  submit.disabled = false;
  input.disabled = false;
  setSessionControlsDisabled(false);
  input.value = "";
  ink.textContent = "The book is open. Describe an idea to start a new page.";
  ink.classList.remove("thinking", "bleed", "gold");
  corrections.textContent = "Session reset.";
  renderGoals(session.goals);
  renderProfile(session.profile);
  renderScore(null);
  setVerdictDisplay("READY", 0, null);
  renderWoodMap(null);
  renderIdeas([]);
  renderPlan([]);
  renderProjects([], "Score an idea to see nearby echoes.");
  renderWhitespace(bootstrapData.whitespace || []);
  restoreExportButtonLabels();
  setCommandDisabled(false);
  saveSession();
  input.focus();
}

async function loadDemoSession() {
  bumpSessionRevision();
  setActiveTab("page");
  submit.disabled = true;
  setCommandDisabled(true);
  setSessionControlsDisabled(true);
  ink.classList.remove("bleed", "gold");
  ink.classList.add("thinking");
  ink.textContent = "Loading an example idea board.";
  corrections.textContent = "";
  try {
    const response = await fetch("/api/demo-session");
    if (!response.ok) throw new Error(`example session failed with ${response.status}`);
    applyDemoSession(await response.json());
  } catch (error) {
    ink.textContent = `The example session could not be loaded: ${error.message}`;
    ink.classList.remove("thinking");
    ink.classList.add("bleed");
  } finally {
    submit.disabled = false;
    setSessionControlsDisabled(false);
    setCommandDisabled(false);
    input.focus();
  }
}

function applyDemoSession(data) {
  session = data.session || {};
  session.profile = session.profile || {};
  session.goals = Array.isArray(session.goals) ? session.goals : [];
  session.last_response = data.response || session.last_response || "";
  session.ui_status = "Example idea board loaded with a plan and share page.";
  currentArtifact = data.artifact || session.last_artifact || null;
  ink.textContent = data.response || "Example session loaded.";
  ink.classList.remove("thinking");
  if (data.score) {
    setVerdictDisplay(data.score.verdict, data.score.overall, data.score);
    renderScore(data.score);
    ink.classList.toggle("bleed", data.score.verdict.startsWith("ECHO"));
    ink.classList.toggle("gold", data.score.verdict.startsWith("UNWRITTEN"));
  }
  renderGoals(session.goals);
  renderProfile(session.profile);
  renderIdeas(session.ideas || []);
  renderPlan(data.plan || session.last_plan || []);
  renderWhitespace(data.whitespace || []);
  if (currentArtifact?.wood_map) renderWoodMap(currentArtifact.wood_map);
  if (data.score?.echoes?.length) {
    renderCitations(data.score.echoes);
  } else {
    renderProjects(data.projects || []);
  }
  setCommandDisabled(false);
  corrections.textContent = session.ui_status;
  saveSession();
}

function renderProvenance(data) {
  const projectCount = Number(data.project_count || data.top_projects?.length || 0);
  const countLabel = projectCount
    ? `${projectCount} project page${projectCount === 1 ? "" : "s"} mapped`
    : "Current project map loaded";
  const updated = shortDate(data.snapshot_generated_at || data.index_generated_at);
  provenanceEl.textContent = `${countLabel} · updated ${updated}`;
}

function renderRestoredSession(data) {
  const restoredProjectReference = isProjectReferenceTool(session.last_tool_resolution?.call?.name || "");
  if (restoredProjectReference) {
    currentArtifact = null;
    renderProjectReferenceState();
    renderProjects(session.last_projects || [], "No project page matched this request.");
    renderIdeas(session.ideas || []);
    delete session.last_plan;
    renderPlan([]);
    setCommandDisabled(false);
    restoreSessionCopy();
    return;
  }
  const idea = currentIdea();
  const storedArtifact = session.last_artifact || null;
  currentArtifact = !idea || storedArtifact?.title === idea.title ? storedArtifact : null;
  const score = currentArtifact?.seal || idea?.score || null;
  if (score) {
    renderScore(score);
    const verdict = currentArtifact?.verdict || score.verdict || "UNWRITTEN";
    setVerdictDisplay(verdict, currentArtifact?.overall || score.overall || 0, score);
    ink.classList.toggle("bleed", verdict.startsWith("ECHO"));
    ink.classList.toggle("gold", verdict.startsWith("UNWRITTEN"));
    renderWoodMap(currentArtifact?.wood_map || null);
    if (score.echoes?.length) {
      renderCitations(score.echoes);
    } else {
      renderProjects([]);
    }
  } else {
    renderScore(null);
    setVerdictDisplay("READY", 0, null);
    renderWoodMap(null);
    renderProjects([], "Score an idea to see nearby echoes.");
  }
  renderIdeas(session.ideas || []);
  renderPlan(session.last_plan || []);
  setCommandDisabled(false);
  restoreSessionCopy();
}

function readSavedSession() {
  try {
    const raw = window.localStorage.getItem(SESSION_STORAGE_KEY);
    if (!raw) return null;
    const parsed = JSON.parse(raw);
    return parsed && typeof parsed === "object" ? parsed : null;
  } catch {
    return null;
  }
}

function normalizeSession(savedSession, defaultSession) {
  const normalized = { ...defaultSession };
  if (!savedSession) return normalized;
  normalized.profile = savedSession.profile && typeof savedSession.profile === "object" ? savedSession.profile : {};
  const savedGoals = Array.isArray(savedSession.goals) ? savedSession.goals : defaultSession.goals;
  normalized.goals = goalOptions.filter((option) => savedGoals.includes(option));
  if (!normalized.goals.length && defaultSession.goals?.length) normalized.goals = [...defaultSession.goals];
  if (Array.isArray(savedSession.ideas)) normalized.ideas = savedSession.ideas;
  if (Array.isArray(savedSession.trace)) normalized.trace = savedSession.trace;
  if (Array.isArray(savedSession.last_plan)) normalized.last_plan = savedSession.last_plan;
  if (savedSession.current_idea_id) normalized.current_idea_id = savedSession.current_idea_id;
  if (savedSession.current_whitespace) normalized.current_whitespace = savedSession.current_whitespace;
  if (savedSession.last_tool_resolution) normalized.last_tool_resolution = savedSession.last_tool_resolution;
  if (savedSession.last_artifact) normalized.last_artifact = savedSession.last_artifact;
  if (Array.isArray(savedSession.last_projects)) normalized.last_projects = savedSession.last_projects;
  if (typeof savedSession.last_response === "string") normalized.last_response = savedSession.last_response;
  if (typeof savedSession.ui_status === "string") normalized.ui_status = savedSession.ui_status;
  return normalized;
}

function restoreSessionCopy() {
  const response = typeof session.last_response === "string" ? session.last_response.trim() : "";
  if (response) ink.textContent = response;
  const status = typeof session.ui_status === "string" ? session.ui_status.trim() : "";
  if (status) corrections.textContent = status;
}

function normalizeGoalProfiles(profiles, options) {
  const byId = new Map(
    profiles
      .filter((profile) => profile && typeof profile.id === "string")
      .map((profile) => [
        profile.id,
        {
          id: profile.id,
          label: String(profile.label || profile.id),
          description: String(profile.description || ""),
        },
      ]),
  );
  return options.map((id) => byId.get(id) || { id, label: id, description: "" });
}

function saveSession() {
  try {
    window.localStorage.setItem(SESSION_STORAGE_KEY, JSON.stringify(session));
  } catch {
    // Storage may be disabled in some embeds; the app still works in-memory.
  }
}

function clearSavedSession() {
  try {
    window.localStorage.removeItem(SESSION_STORAGE_KEY);
  } catch {
    // Nothing else to clear when storage is unavailable.
  }
}

function renderGoals(selectedGoals) {
  const selected = new Set(selectedGoals || []);
  if (goalCountEl) goalCountEl.textContent = selected.size;
  goalsEl.innerHTML = "";
  if (!goalOptions.length) {
    goalsEl.innerHTML = `<div class="empty">No goals loaded.</div>`;
    return;
  }
  for (const option of goalOptions) {
    const profile = goalProfileById.get(option) || { label: option, description: "" };
    const label = document.createElement("label");
    label.className = `goal-toggle goal ${selected.has(option) ? "on" : ""}`;
    label.innerHTML = `
      <input
        type="checkbox"
        data-goal="${escapeAttribute(option)}"
        aria-label="${escapeAttribute(profile.label)}"
        ${sessionControlsLocked ? "disabled" : ""}
        ${selected.has(option) ? "checked" : ""}
      />
      <span class="check" aria-hidden="true">
        <svg class="icon"><use href="#icon-check"></use></svg>
      </span>
      <span class="goal-copy">
        <strong>${escapeHtml(profile.label)}</strong>
        ${profile.description ? `<small>${escapeHtml(profile.description)}</small>` : ""}
      </span>
    `;
    goalsEl.append(label);
  }
}

function renderProfile(profile) {
  profileEl.innerHTML = "";
  if (!profileFields.length) {
    profileEl.innerHTML = `<div class="empty">No profile fields.</div>`;
    return;
  }
  for (const field of profileFields) {
    const row = document.createElement("label");
    row.className = "profile-field";
    row.innerHTML = `
      <span>${escapeHtml(fieldLabel(field))}</span>
      <input
        data-profile-field="${escapeAttribute(field)}"
        value="${escapeAttribute(profile?.[field] || "")}"
        placeholder="${escapeAttribute(fieldPlaceholder(field))}"
        autocomplete="off"
        ${sessionControlsLocked ? "disabled" : ""}
      />
    `;
    profileEl.append(row);
  }
}

function handleEvent(event) {
  if (event.type === "start") {
    if (event.corrections?.length) {
      corrections.textContent = event.corrections
        .map((item) => `heard: ${item.original} -> ${item.canonical}`)
        .join("   ");
    }
    return;
  }

  if (event.type === "stage") {
    setTurnStage(event.stage, event.label);
    return;
  }

  if (event.type === "model_progress") {
    renderModelProgress(event.tokens, event.max_tokens);
    return;
  }

  if (event.type === "tool_event") {
    addToolChip(event);
    return;
  }

  if (event.type === "fallback") {
    renderComputeFallback(event);
    return;
  }

  if (event.type === "token") {
    markFirstTokenSeen();
    ink.textContent += event.text;
    return;
  }

  if (event.type === "done") {
    setTurnBar(100);
    if (turnEtaEl) turnEtaEl.textContent = "";
    hideTurnProgress();
    if (!sawTurnToken) {
      clearTurnWatchdog();
      ink.textContent = event.response || ink.textContent;
      ink.classList.remove("thinking");
    }
    session = event.state || {};
    session.profile = session.profile || {};
    session.goals = Array.isArray(session.goals) ? session.goals : [];
    session.last_response = event.response || session.last_response || "";
    delete session.ui_status;
    const toolName = session.last_tool_resolution?.call?.name || "";
    const projectReferenceOnly = !event.score && !event.artifact?.title && isProjectReferenceTool(toolName);
    if (projectReferenceOnly) {
      session.last_projects = Array.isArray(event.projects) ? event.projects : [];
    } else {
      delete session.last_projects;
    }
    if (event.score?.echoes?.length) {
      renderCitations(event.score.echoes);
    } else if (event.projects?.length) {
      renderProjects(event.projects);
    } else if (projectReferenceOnly) {
      renderProjects([], "No project page matched this request.");
    }
    if (event.whitespace?.length) renderWhitespace(event.whitespace);
    renderGoals(session.goals);
    renderProfile(session.profile);
    renderIdeas(session.ideas || []);
    renderPlan(event.plan || []);
    if (event.score) {
      setVerdictDisplay(event.score.verdict, event.score.overall, event.score);
      renderScore(event.score);
      ink.classList.toggle("bleed", event.score.verdict.startsWith("ECHO"));
      ink.classList.toggle("gold", event.score.verdict.startsWith("UNWRITTEN"));
    } else if (projectReferenceOnly) {
      renderProjectReferenceState();
    } else if (!event.projects?.length) {
      const idea = currentIdea();
      renderSelectedIdeaSeal(idea);
      renderSelectedIdeaArtifact(idea || {});
    }
    if (event.artifact?.title) {
      currentArtifact = event.artifact;
      renderWoodMap(event.artifact.wood_map || null);
    }
    setCommandDisabled(false);
    saveSession();
  }
}

function renderProjectReferenceState() {
  currentArtifact = null;
  renderScore(null);
  setVerdictDisplay("READY", 0, null);
  sealCopyEl.textContent = "Project pages are shown below. Write or select an idea to score it.";
  ink.classList.remove("bleed", "gold");
  renderWoodMap(null);
}

function isProjectReferenceTool(toolName) {
  return ["list_projects", "get_project"].includes(toolName);
}

function renderIdeas(ideas) {
  if (ideaCountEl) ideaCountEl.textContent = ideas.length;
  ideasEl.innerHTML = "";
  if (!ideas.length) {
    ideasEl.innerHTML = `<div class="empty">Your idea board is empty. Write an idea or choose an under-explored direction.</div>`;
    return;
  }
  for (const idea of visibleIdeas(ideas)) {
    const score = idea.score?.overall ? Number(idea.score.overall).toFixed(1) : "0.0";
    const goals = (idea.goals || []).slice(0, 3).map(goalDisplayName).join(" · ");
    const selected = idea.id === session.current_idea_id;
    const verdict = idea.score?.verdict || "DRAFT";
    const isEcho = String(verdict).startsWith("ECHO");
    const item = document.createElement("button");
    item.type = "button";
    item.className = `idea idea-card ${selected ? "current" : ""} ${isEcho ? "bleed" : ""}`;
    item.disabled = sessionControlsLocked;
    item.dataset.ideaId = idea.id || "";
    item.setAttribute("aria-pressed", selected ? "true" : "false");
    item.setAttribute("aria-label", ideaCardAriaLabel(idea, score, verdict));
    item.innerHTML = `
      <div class="ihead">
        <strong>${escapeHtml(idea.title)}</strong>
        <span class="iscore">${score}</span>
      </div>
      <p>${escapeHtml((idea.pitch || "").slice(0, 120))}</p>
      <span class="iverdict ${isEcho ? "echo" : "unwritten"}">${escapeHtml(verdict)}</span>
      ${goals ? `<small>${escapeHtml(goals)}</small>` : ""}
    `;
    ideasEl.append(item);
  }
}

function visibleIdeas(ideas) {
  const currentId = session.current_idea_id;
  const current = currentId ? ideas.find((idea) => idea.id === currentId) : null;
  const remaining = ideas.filter((idea) => idea.id !== currentId).slice(-3).reverse();
  return current ? [current, ...remaining] : ideas.slice(-4).reverse();
}

function ideaCardAriaLabel(idea, score, verdict) {
  const title = String(idea?.title || "Untitled idea").trim() || "Untitled idea";
  const parts = [`Select idea: ${title}`];
  if (score) parts.push(`score ${score}`);
  if (verdict) parts.push(String(verdict));
  return parts.join(", ");
}

function currentIdea() {
  const ideas = Array.isArray(session.ideas) ? session.ideas : [];
  return ideas.find((idea) => idea.id === session.current_idea_id) || ideas[ideas.length - 1] || null;
}

function selectIdea(ideaId) {
  if (!ideaId || !Array.isArray(session.ideas)) return;
  const idea = session.ideas.find((item) => item.id === ideaId);
  if (!idea) return;
  const changedIdea = idea.id !== session.current_idea_id;
  bumpSessionRevision();
  session.current_idea_id = idea.id;
  if (Array.isArray(idea.goals) && idea.goals.length) {
    session.goals = goalOptions.filter((option) => idea.goals.includes(option));
  }
  renderSelectedIdeaSeal(idea);
  renderSelectedIdeaArtifact(idea);
  renderGoals(session.goals || []);
  renderIdeas(session.ideas);
  if (changedIdea) delete session.last_plan;
  renderPlan(changedIdea ? [] : session.last_plan || []);
  session.ui_status = `selected: ${idea.title}`;
  corrections.textContent = session.ui_status;
  saveSession();
}

function renderSelectedIdeaSeal(idea) {
  const score = idea?.score || null;
  if (!score) {
    renderScore(null);
    setVerdictDisplay("READY", 0, null);
    ink.classList.remove("bleed", "gold");
    renderProjects([], "Score this idea to see nearby echoes.");
    return;
  }
  setVerdictDisplay(score.verdict || "DRAFT", score.overall || 0, score);
  renderScore(score);
  ink.classList.toggle("bleed", String(score.verdict || "").startsWith("ECHO"));
  ink.classList.toggle("gold", String(score.verdict || "").startsWith("UNWRITTEN"));
  if (score.echoes?.length) {
    renderCitations(score.echoes);
  } else {
    renderProjects([]);
  }
}

function renderSelectedIdeaArtifact(idea) {
  const artifact = idea.artifact || (session.last_artifact?.title === idea.title ? session.last_artifact : null);
  if (artifact) {
    currentArtifact = artifact;
    session.last_artifact = artifact;
    renderWoodMap(currentArtifact.wood_map || null);
    setCommandDisabled(false);
    return;
  }
  currentArtifact = null;
  renderWoodMap(null);
  setCommandDisabled(false);
}

function goalDisplayName(goal) {
  return goalProfileById.get(goal)?.label || goal;
}

function renderScore(score) {
  const rows = [
    ["Original", score?.originality || 0],
    ["Delight", score?.delight || 0],
    ["AI Need", score?.ai_necessity || 0],
    ["Feasible", score?.feasibility || 0],
    ["Goal Fit", score?.goal_fit || 0],
  ];
  scoreEl.innerHTML = rows
    .map(
      ([label, value]) => `
        <div class="quad">
          <span class="ql">${label}</span>
          <span class="qbar"><span class="qfill" style="width: ${Number(value) * 10}%"></span></span>
          <span class="qv">${value}</span>
        </div>
      `,
    )
    .join("");
}

function renderWoodMap(map) {
  woodMapEl.innerHTML = "";
  if (!map?.dots?.length) {
    woodMapEl.innerHTML = `<div class="wood"><div class="empty wood-empty">Score an idea to plot it on the map.</div></div>`;
    return;
  }
  const field = document.createElement("div");
  field.className = "wood";
  for (const dot of map.dots) {
    const marker = document.createElement(dot.url ? "a" : "span");
    // Namespace the kind class (wood-idea/wood-echo/wood-inked) so it never collides with the
    // global .idea/.echo card styles. The "you" dot stays green regardless of verdict.
    marker.className = `wood-dot wood-${dot.kind || "inked"}`;
    marker.style.left = `${boundedPercent(dot.x)}%`;
    marker.style.top = `${boundedPercent(dot.y)}%`;
    const radius = Math.max(3, Math.min(10, Number(dot.radius || 4)));
    marker.style.width = `${radius * 2}px`;
    marker.style.height = `${radius * 2}px`;
    marker.title = dot.kind === "idea" ? `You: ${dot.title}` : `${dot.title}${dot.score ? ` (${dot.score})` : ""}`;
    if (dot.url) {
      marker.href = dot.url;
      marker.target = "_blank";
      marker.rel = "noreferrer";
    }
    field.append(marker);
  }
  const legend = document.createElement("div");
  legend.className = "wood-legend";
  legend.innerHTML = `
    <span><i style="background: var(--leaf)"></i> You</span>
    <span><i style="background: var(--oxblood)"></i> Echo</span>
    <span><i style="background: rgba(73, 49, 22, 0.34)"></i> Indexed</span>
  `;
  const caption = document.createElement("p");
  caption.className = "wood-cap";
  caption.textContent = map.caption || "Your idea is plotted against the current project map.";
  woodMapEl.append(field, legend, caption);
}

function renderProjects(projects, emptyMessage = "No nearby projects yet.") {
  projectsEl.innerHTML = "";
  if (!projects.length) {
    projectsEl.innerHTML = `<div class="empty">${escapeHtml(emptyMessage)}</div>`;
    return;
  }
  for (const project of projects.slice(0, 5)) {
    const item = document.createElement("a");
    item.className = "project echo-item";
    item.href = project.url;
    item.target = "_blank";
    item.rel = "noreferrer";
    const summary = project.summary ? `<p>${escapeHtml(project.summary)}</p>` : "";
    item.innerHTML = `<strong>${escapeHtml(project.title || "Untitled project")}</strong>${summary}`;
    projectsEl.append(item);
  }
}

function renderCitations(echoes) {
  projectsEl.innerHTML = "";
  if (!echoes.length) {
    projectsEl.innerHTML = `<div class="empty">No nearby project echoes yet.</div>`;
    return;
  }
  for (const echo of echoes.slice(0, 5)) {
    const project = echo.project || {};
    const item = document.createElement("a");
    item.className = "project echo-item citation";
    item.href = project.url || project.host || "#";
    item.target = "_blank";
    item.rel = "noreferrer";
    item.title = project.title || project.id || "Project citation";
    const matched = (echo.matched_terms || []).slice(0, 5).join(", ") || "no shared terms";
    const summary = project.summary ? `<p>${escapeHtml(project.summary)}</p>` : "";
    item.innerHTML = `
      <strong>Page ${escapeHtml(echo.page_number || "?")} · ${escapeHtml(project.title || "Untitled project")}</strong>
      ${summary}
      <span class="matched">${Number(echo.score || 0).toFixed(3)} · ${escapeHtml(matched)}</span>
    `;
    projectsEl.append(item);
  }
}

function renderWhitespace(items) {
  whitespaceEl.innerHTML = "";
  if (!items.length) {
    whitespaceEl.innerHTML = `<div class="empty">No starting directions are loaded yet.</div>`;
    return;
  }
  for (const item of items.slice(0, 4)) {
    const gap = document.createElement("button");
    gap.type = "button";
    gap.className = "gap gap-item";
    gap.disabled = sessionControlsLocked;
    gap.dataset.gapPrompt = `idea: ${item.label} -- ${item.pitch}`;
    gap.innerHTML = `
      <strong>${escapeHtml(item.label)}</strong>
      <p>${escapeHtml(item.pitch)}</p>
      <span class="use">Use this direction</span>
    `;
    whitespaceEl.append(gap);
  }
}

function renderPlan(steps) {
  planEl.innerHTML = "";
  if (!steps.length) {
    planEl.innerHTML = `<li class="empty">Press Plan to draft build steps for the selected idea.</li>`;
    return;
  }
  for (const step of steps) {
    const item = document.createElement("li");
    item.textContent = step;
    planEl.append(item);
  }
}

function setCommandDisabled(disabled) {
  document.querySelectorAll(".command-row button").forEach((button) => {
    const isArtifact = button.id === "export-artifact";
    const isNotes = button.id === "export-notes";
    const isChapter = button.id === "export-chapter";
    const locked = disabled || sessionControlsLocked;
    button.disabled =
      locked ||
      (isArtifact && !currentArtifact) ||
      (isNotes && !session.trace?.length) ||
      (isChapter && !session.ideas?.length);
  });
}

function startTurnWatchdog() {
  clearTurnWatchdog();
  sawTurnToken = false;
  ink.textContent = "Checking the current project map.";
  ink.classList.add("thinking");
  turnWatchdog = window.setTimeout(() => {
    if (sawTurnToken) return;
    ink.textContent = "Still comparing against nearby projects.";
  }, 2200);
}

function markFirstTokenSeen() {
  if (sawTurnToken) return;
  sawTurnToken = true;
  clearTurnWatchdog();
  ink.textContent = "";
  ink.classList.remove("thinking");
}

function clearTurnWatchdog() {
  if (turnWatchdog) {
    window.clearTimeout(turnWatchdog);
    turnWatchdog = null;
  }
}

function setupMenus() {
  document.querySelectorAll(".menu").forEach((menu) => {
    const trigger = menu.querySelector(".menu-trigger");
    const list = menu.querySelector(".menu-list");
    if (!trigger || !list) return;
    trigger.addEventListener("click", (event) => {
      event.stopPropagation();
      const willOpen = list.hidden;
      closeAllMenus();
      if (willOpen) {
        list.hidden = false;
        trigger.setAttribute("aria-expanded", "true");
      }
    });
    list.addEventListener("click", () => closeAllMenus()); // selecting an item closes the menu
  });
  document.addEventListener("click", () => closeAllMenus());
  document.addEventListener("keydown", (event) => {
    if (event.key === "Escape") closeAllMenus();
  });
}

function closeAllMenus() {
  document.querySelectorAll(".menu-list").forEach((list) => {
    list.hidden = true;
  });
  document.querySelectorAll(".menu-trigger").forEach((trigger) => {
    trigger.setAttribute("aria-expanded", "false");
  });
}

// Coarse overall completion per stage, so the bar always advances even when token-level
// progress is unknown (e.g. the rules backend, or the fast tool/writing stages).
const STAGE_PROGRESS = { planning: 8, running_tool: 85, writing: 95 };

function resetTurnProgress() {
  if (!turnProgressEl) return;
  // Stay hidden on submit. Only reveal once the turn is genuinely executing — either real
  // token decoding starts, or it has been running long enough to be worth a progress bar.
  // A fast turn finishes before the timer fires, so the bar never flashes.
  turnProgressEl.hidden = true;
  decodeStartedAt = 0;
  if (toolChipsEl) toolChipsEl.innerHTML = "";
  if (turnTokensEl) turnTokensEl.textContent = "";
  if (turnEtaEl) turnEtaEl.textContent = "";
  setTurnBar(4);
  setTurnStageContent("planning", "Thinking");
  clearTurnProgressTimer();
  turnProgressTimer = window.setTimeout(revealTurnProgress, 450);
}

function revealTurnProgress() {
  if (turnProgressEl) turnProgressEl.hidden = false;
}

function clearTurnProgressTimer() {
  if (turnProgressTimer) {
    window.clearTimeout(turnProgressTimer);
    turnProgressTimer = null;
  }
}

function hideTurnProgress() {
  clearTurnProgressTimer();
  if (turnProgressEl) turnProgressEl.hidden = true;
}

function setTurnBar(percent) {
  if (!turnBarFillEl) return;
  const clamped = Math.max(0, Math.min(100, percent));
  turnBarFillEl.style.width = `${clamped}%`;
}

function setTurnStageContent(stage, label) {
  if (turnStageIconEl) turnStageIconEl.textContent = STAGE_ICONS[stage] || "🪶";
  if (turnStageTextEl) turnStageTextEl.textContent = label || "Thinking";
  if (stage in STAGE_PROGRESS) setTurnBar(STAGE_PROGRESS[stage]);
  if (stage && stage !== "planning" && turnEtaEl) turnEtaEl.textContent = "";
}

function setTurnStage(stage, label) {
  clearTurnWatchdog();
  setTurnStageContent(stage, label);
}

function renderModelProgress(tokens, maxTokens) {
  clearTurnWatchdog();
  revealTurnProgress(); // real token decoding is unambiguous execution — show it now
  const count = Number(tokens) || 0;
  if (turnTokensEl) turnTokensEl.textContent = count ? `· decoded ${count} tokens` : "";
  if (!count) return;
  if (!decodeStartedAt) decodeStartedAt = performance.now();

  const cap = Number(maxTokens) || 0;
  // Map token decode into the 8%–80% band of the overall bar.
  if (cap > 0) setTurnBar(8 + Math.min(1, count / cap) * 72);

  // Estimate remaining time from the live decode rate toward the token cap (an upper bound).
  const elapsed = (performance.now() - decodeStartedAt) / 1000;
  if (turnEtaEl && cap > 0 && elapsed > 0.3) {
    const rate = count / elapsed;
    const remaining = Math.max(0, cap - count) / Math.max(rate, 0.1);
    turnEtaEl.textContent = remaining >= 1 ? `~${Math.ceil(remaining)}s left` : "almost done";
  }
}

function addToolChip(event) {
  if (!toolChipsEl) return;
  const name = event.name || event.tool || "tool";
  const chip = document.createElement("span");
  chip.className = "tool-chip";
  if (event.summary) chip.title = event.summary;
  chip.innerHTML = `<span class="tc-name"></span><span class="tc-check">✓</span>`;
  chip.querySelector(".tc-name").textContent = name;
  toolChipsEl.append(chip);
}

function renderComputeFallback(event) {
  // Acceleration is automatic; a fallback is informational only (no control to flip).
  const reason = event.reason || "Running on CPU (slower).";
  if (turnStageTextEl) turnStageTextEl.textContent = reason;
  if (corrections) corrections.textContent = reason;
}

function syncCurrentIdeaGoals() {
  const currentId = session.current_idea_id;
  if (!currentId || !Array.isArray(session.ideas)) return;
  const idea = session.ideas.find((item) => item.id === currentId);
  if (idea) idea.goals = [...(session.goals || [])];
}

function invalidateCurrentSeal(message) {
  const idea = currentIdea();
  if (idea?.score) idea.score = null;
  clearCurrentArtifactFor(idea);
  invalidateCurrentPlan("");
  renderScore(null);
  setVerdictDisplay("READY", 0, null);
  renderWoodMap(null);
  renderProjects([]);
  setCommandDisabled(false);
  if (message) setSessionStatus(message);
}

function invalidateCurrentPlan(message) {
  if (Array.isArray(session.last_plan)) delete session.last_plan;
  renderPlan([]);
  if (message) setSessionStatus(message);
}

function clearCurrentArtifactFor(idea) {
  if (!idea || currentArtifact?.title === idea.title) currentArtifact = null;
  if (idea?.artifact) delete idea.artifact;
  if (!idea || session.last_artifact?.title === idea.title) delete session.last_artifact;
}

function setSessionStatus(message) {
  session.ui_status = message;
  corrections.textContent = message;
}

async function exportNotes() {
  await exportMarkdown({
    endpoint: "/api/field-notes",
    filename: FIELD_NOTES_FILENAME,
    button: exportNotesButton,
    busyLabel: "Notes...",
    pendingLabel: "Writing notes.",
    successLabel: "Notes saved",
  });
}

async function exportChapter() {
  await exportMarkdown({
    endpoint: "/api/chapter",
    filename: CHAPTER_FILENAME,
    button: exportChapterButton,
    busyLabel: "Chapter...",
    pendingLabel: "Writing chapter.",
    successLabel: "Chapter saved",
  });
}

async function exportMarkdown({ endpoint, filename, button, busyLabel, pendingLabel, successLabel }) {
  if (!button || button.disabled) return;
  const revision = sessionRevision;
  const idleLabel = actionButtonLabel(button);
  button.disabled = true;
  setActionButtonLabel(button, busyLabel);
  session.ui_status = pendingLabel;
  corrections.textContent = session.ui_status;
  saveSession();
  try {
    const response = await fetch(endpoint, {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify({
        session_json: JSON.stringify(session),
      }),
    });
    if (!response.ok) throw new Error(`export failed with ${response.status}`);
    const text = await response.text();
    if (!text.trim()) throw new Error("empty export");
    if (!isCurrentSessionRevision(revision)) return;
    downloadText(filename, text, "text/markdown;charset=utf-8");
    session.ui_status = `${successLabel}: ${filename}`;
    corrections.textContent = session.ui_status;
  } catch (error) {
    if (!isCurrentSessionRevision(revision)) return;
    session.ui_status = `Export failed: ${error.message}`;
    corrections.textContent = session.ui_status;
  } finally {
    setActionButtonLabel(button, idleLabel);
    if (isCurrentSessionRevision(revision)) saveSession();
    setCommandDisabled(false);
  }
}

async function exportArtifact(artifact) {
  const idleLabel = actionButtonLabel(exportButton);
  const revision = sessionRevision;
  exportButton.disabled = true;
  setActionButtonLabel(exportButton, "PNG...");
  session.ui_status = "Drawing PNG.";
  corrections.textContent = session.ui_status;
  saveSession();
  try {
    const response = await fetch("/api/artifact.png", {
      method: "POST",
      headers: { "Content-Type": "application/json" },
      body: JSON.stringify(artifact),
    });
    if (!response.ok) throw new Error(`PNG rendering failed with ${response.status}`);
    const blob = await response.blob();
    if (!blob.size || !blob.type.includes("png")) throw new Error("PNG rendering failed");
    if (!isCurrentSessionRevision(revision)) return;
    const filename = filenameFromContentDisposition(response.headers.get("content-disposition")) || "unwritten-page.png";
    downloadBlob(filename, blob);
    session.ui_status = `PNG saved: ${filename}`;
    corrections.textContent = session.ui_status;
  } catch (error) {
    if (!isCurrentSessionRevision(revision)) return;
    session.ui_status = `Export failed: ${error.message}`;
    corrections.textContent = session.ui_status;
  } finally {
    if (isCurrentSessionRevision(revision)) saveSession();
    setActionButtonLabel(exportButton, idleLabel || PNG_EXPORT_LABEL);
    setCommandDisabled(false);
  }
}

function downloadBlob(filename, blob) {
  const link = document.createElement("a");
  link.download = filename;
  link.href = URL.createObjectURL(blob);
  link.click();
  setTimeout(() => URL.revokeObjectURL(link.href), 0);
}

function downloadText(filename, text, type = "application/jsonl;charset=utf-8") {
  const blob = new Blob([text], { type });
  const link = document.createElement("a");
  link.download = filename;
  link.href = URL.createObjectURL(blob);
  link.click();
  setTimeout(() => URL.revokeObjectURL(link.href), 0);
}

function filenameFromContentDisposition(value) {
  const match = String(value || "").match(/filename="([^"]+)"/i);
  return match ? match[1] : "";
}

function escapeHtml(value) {
  return String(value)
    .replaceAll("&", "&amp;")
    .replaceAll("<", "&lt;")
    .replaceAll(">", "&gt;")
    .replaceAll('"', "&quot;")
    .replaceAll("'", "&#039;");
}

function escapeAttribute(value) {
  return escapeHtml(value).replaceAll("`", "&#096;");
}

function fieldLabel(value) {
  return String(value)
    .replaceAll("_", " ")
    .replace(/^\w/, (char) => char.toUpperCase());
}

function fieldPlaceholder(value) {
  const placeholders = {
    skills: "frontend, notebooks, prompt design",
    time: "one evening, weekend, 3 hours",
    preferences: "visual, playful, practical",
    constraints: "CPU-only, no paid APIs, solo build",
  };
  return placeholders[value] || "";
}

function boundedPercent(value) {
  return Math.max(4, Math.min(96, Number(value || 50)));
}

function shortDate(value) {
  if (!value) return "unknown";
  return String(value).replace("T", " ").replace(/\+00:00$/, "Z").slice(0, 16);
}