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

.hdr {
  border-bottom: 1px solid var(--border);
  padding: 12px 20px;
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 16px;
  position: sticky;
  top: 0;
  background: rgba(0,0,0,0.95);
  backdrop-filter: blur(8px);
  z-index: 50;
  flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 10px; }
.brand-mark {
  font-family: var(--mono);
  font-size: 12px;
  font-weight: 700;
  color: var(--white);
  border: 1px solid var(--border-hi);
  padding: 4px 8px;
  border-radius: 3px;
  letter-spacing: 0.05em;
}
.brand-name { font-weight: 600; font-size: 14px; letter-spacing: -0.01em; }
.brand-version { font-family: var(--mono); font-size: 11px; color: var(--text-mute); }
.hdr-actions { display: flex; gap: 8px; flex-wrap: wrap; }

.btn {
  font-size: 12px;
  font-weight: 500;
  padding: 8px 14px;
  border: 1px solid var(--border-hi);
  border-radius: var(--radius);
  background: var(--panel);
  color: var(--text);
  transition: all 0.15s ease;
  white-space: nowrap;
}
.btn:hover:not(:disabled) { background: var(--panel-2); border-color: var(--border-focus); }
.btn:active:not(:disabled) { background: var(--panel-3); }
.btn:disabled { opacity: 0.35; cursor: not-allowed; }
.btn-primary { background: var(--white); color: #000; border-color: var(--white); font-weight: 600; }
.btn-primary:hover:not(:disabled) { background: #d4d4d4; border-color: #d4d4d4; }
.btn-block { width: 100%; display: block; }
.btn-sm { padding: 6px 10px; font-size: 11px; }
.btn-lg { padding: 12px 20px; font-size: 13px; }
.btn-ghost { background: transparent; border-color: var(--border); color: var(--text-dim); }
.btn-ghost:hover:not(:disabled) { color: var(--text); border-color: var(--border-hi); }

.main {
  max-width: 1440px;
  margin: 0 auto;
  padding: 20px;
  display: grid;
  grid-template-columns: minmax(0, 1fr) minmax(0, 1.4fr);
  gap: 20px;
  align-items: start;
}
@media (max-width: 900px) { .main { grid-template-columns: 1fr; } }
.col { display: flex; flex-direction: column; gap: 20px; min-width: 0; }

.panel {
  background: var(--panel);
  border: 1px solid var(--border);
  border-radius: var(--radius);
  overflow: hidden;
}
.panel-hdr {
  padding: 12px 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
}
.panel-title {
  font-size: 12px;
  font-weight: 600;
  letter-spacing: 0.02em;
  text-transform: uppercase;
  color: var(--text-dim);
}
.panel-body { padding: 16px; }

.pipeline { display: flex; flex-direction: column; }
.step {
  padding: 14px 16px;
  border-bottom: 1px solid var(--border);
  display: flex;
  gap: 12px;
  align-items: flex-start;
}
.step:last-child { border-bottom: none; }
.step-num {
  flex-shrink: 0;
  width: 24px;
  height: 24px;
  border-radius: 50%;
  border: 1px solid var(--border-hi);
  display: flex;
  align-items: center;
  justify-content: center;
  font-family: var(--mono);
  font-size: 11px;
  font-weight: 600;
  color: var(--text-dim);
  transition: all 0.2s;
}
.step.active .step-num { border-color: var(--white); background: var(--white); color: #000; }
.step.done .step-num { background: var(--white); border-color: var(--white); color: #000; }
.step-body { flex: 1; min-width: 0; }
.step-title { font-size: 13px; font-weight: 600; margin-bottom: 2px; }
.step-desc { font-size: 12px; color: var(--text-dim); margin-bottom: 10px; }
.step-actions { display: flex; gap: 8px; flex-wrap: wrap; }

.setting { margin-bottom: 14px; }
.setting:last-child { margin-bottom: 0; }
.setting-label { display: flex; justify-content: space-between; font-size: 12px; margin-bottom: 6px; color: var(--text-dim); }
.setting-val { font-family: var(--mono); color: var(--text); font-size: 11px; }
input[type="range"] {
  width: 100%; height: 2px; background: var(--border-hi); border-radius: 1px;
  outline: none; -webkit-appearance: none; appearance: none;
}
input[type="range"]::-webkit-slider-thumb {
  -webkit-appearance: none; width: 14px; height: 14px; background: var(--white);
  border-radius: 50%; cursor: pointer; border: 2px solid var(--bg);
}
input[type="range"]::-moz-range-thumb {
  width: 14px; height: 14px; background: var(--white); border-radius: 50%;
  cursor: pointer; border: 2px solid var(--bg);
}
select {
  width: 100%; background: var(--panel-2); color: var(--text);
  border: 1px solid var(--border-hi); border-radius: var(--radius);
  padding: 7px 10px; font-size: 12px; outline: none;
}
select:focus { border-color: var(--border-focus); }

.metrics { display: grid; grid-template-columns: repeat(3, 1fr); gap: 8px; }
#osc-display { grid-template-columns: repeat(4, 1fr); }
.metric {
  padding: 10px 8px; background: var(--panel-2); border: 1px solid var(--border);
  border-radius: 3px; text-align: center;
}
.metric-label { font-size: 10px; color: var(--text-mute); text-transform: uppercase; letter-spacing: 0.05em; margin-bottom: 3px; }
.metric-value { font-family: var(--mono); font-size: 14px; font-weight: 600; color: var(--white); }

.tabs { display: flex; border-bottom: 1px solid var(--border); background: var(--panel-2); }
.tab {
  padding: 10px 16px; font-size: 12px; color: var(--text-mute);
  border-bottom: 2px solid transparent; transition: all 0.15s;
}
.tab:hover { color: var(--text-dim); }
.tab.active { color: var(--white); border-bottom-color: var(--white); }
.tabpanel { display: none; }
.tabpanel.active { display: block; }

.terminal {
  height: 300px; overflow-y: auto; padding: 12px 14px; background: #000;
  font-family: var(--mono); font-size: 11.5px; line-height: 1.55; color: var(--text-dim);
}
.log-line { margin-bottom: 1px; }
.log-line .ts { color: var(--text-mute); margin-right: 8px; }
.log-line.ok { color: var(--text); }
.log-line.warn { color: #a1a1aa; }
.log-line.err { color: var(--err); }

#kg-canvas { display: block; width: 100%; height: 300px; background: #000; }
.graph-tools {
  display: flex; gap: 6px; padding: 10px 14px;
  border-top: 1px solid var(--border); background: var(--panel-2); flex-wrap: wrap;
}
.graph-tools .btn { flex: 1; min-width: 90px; padding: 6px 8px; font-size: 11px; }

.api-log {
  height: 300px; overflow-y: auto; padding: 12px 14px; background: #000;
  font-family: var(--mono); font-size: 11.5px; line-height: 1.55;
  white-space: pre-wrap;
}
.api-line { padding: 1px 0; color: var(--text-dim); }
.api-line .ts { color: var(--text-mute); margin-right: 8px; }
.api-line.ok { color: var(--text); }
.api-line.err { color: var(--err); }

.progress { height: 3px; background: var(--border); border-radius: 2px; overflow: hidden; }
.progress-fill { height: 100%; background: var(--white); width: 0%; transition: width 0.3s ease; }
.progress-meta {
  display: flex; justify-content: space-between; font-size: 11px;
  color: var(--text-mute); margin-top: 6px; font-family: var(--mono);
}

.modal {
  position: fixed; inset: 0; background: rgba(0,0,0,0.85); backdrop-filter: blur(6px);
  display: flex; align-items: center; justify-content: center; z-index: 100;
  padding: 20px; opacity: 0; pointer-events: none; transition: opacity 0.2s ease;
}
.modal.show { opacity: 1; pointer-events: auto; }
.modal.hidden { display: none; }
.modal-box {
  background: var(--panel); border: 1px solid var(--border-hi); border-radius: 8px;
  width: 100%; max-width: 640px; max-height: 90vh; display: flex; flex-direction: column;
  overflow: hidden; box-shadow: 0 30px 80px rgba(0,0,0,0.8);
}
.modal-box.wide { max-width: 980px; }
.modal-hdr {
  padding: 18px 22px; border-bottom: 1px solid var(--border);
  display: flex; justify-content: space-between; align-items: center; gap: 12px;
}
.modal-title { font-size: 14px; font-weight: 600; }
.modal-close {
  width: 28px; height: 28px; border-radius: 4px; display: flex;
  align-items: center; justify-content: center; color: var(--text-dim);
  font-size: 18px; line-height: 1;
}
.modal-close:hover { background: var(--panel-2); color: var(--text); }
.modal-body { padding: 22px; overflow-y: auto; flex: 1; }
.modal-foot {
  padding: 16px 22px; border-top: 1px solid var(--border);
  display: flex; justify-content: space-between; align-items: center;
  gap: 12px; flex-wrap: wrap;
}

.wiz-header { padding: 20px 24px 0; }
.wiz-progress { display: flex; gap: 4px; margin-bottom: 20px; }
.wiz-dot { flex: 1; height: 3px; background: var(--border); border-radius: 2px; }
.wiz-dot.active { background: var(--white); }
.wiz-dot.done { background: var(--text-mute); }
.wiz-title { font-size: 22px; font-weight: 600; margin-bottom: 6px; letter-spacing: -0.02em; }
.wiz-sub { font-size: 13px; color: var(--text-dim); margin-bottom: 20px; }
.wiz-body { padding: 0 24px 22px; }
.wiz-body p { margin-bottom: 12px; color: var(--text-dim); font-size: 13px; line-height: 1.6; }
.wiz-body ul { margin: 12px 0 12px 22px; color: var(--text-dim); font-size: 13px; }
.wiz-body li { margin-bottom: 6px; }
.wiz-body code {
  font-family: var(--mono); font-size: 11.5px; background: var(--panel-2);
  padding: 2px 6px; border-radius: 3px; border: 1px solid var(--border);
}
.wiz-info {
  background: var(--panel-2); border: 1px solid var(--border);
  border-radius: var(--radius); padding: 12px 14px;
  font-size: 12px; color: var(--text-dim); margin: 14px 0;
}
.wiz-info strong { color: var(--text); font-weight: 600; }
.wiz-actions {
  padding: 16px 24px; border-top: 1px solid var(--border);
  display: flex; justify-content: space-between; gap: 10px; flex-wrap: wrap;
}
.wiz-actions .btn-primary { padding: 12px 24px; font-size: 14px; }

.file-list {
  border: 1px solid var(--border); border-radius: var(--radius);
  overflow: hidden; max-height: 240px; overflow-y: auto;
}
.file-row {
  display: flex; align-items: center; gap: 10px; padding: 9px 12px;
  border-bottom: 1px solid var(--border); font-family: var(--mono);
  font-size: 12px; cursor: pointer;
}
.file-row:last-child { border-bottom: none; }
.file-row:hover { background: var(--panel-2); }
.file-row.selected { background: var(--panel-3); }
.file-row-name { flex: 1; color: var(--text); }
.file-row-size { color: var(--text-mute); font-size: 11px; }
.file-preview {
  background: #000; border: 1px solid var(--border); border-radius: var(--radius);
  padding: 14px; margin-top: 14px; font-family: var(--mono);
  font-size: 11px; line-height: 1.5; color: var(--text-dim);
  max-height: 260px; overflow: auto; white-space: pre-wrap; word-break: break-word;
}
.opts-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 8px; margin-top: 14px; }
.opt {
  display: flex; align-items: center; gap: 8px; padding: 8px 10px;
  background: var(--panel-2); border: 1px solid var(--border);
  border-radius: 3px; font-size: 12px; color: var(--text-dim); cursor: pointer;
}
.opt:hover { border-color: var(--border-hi); color: var(--text); }
.opt input { accent-color: var(--white); }
.opt input:checked + span { color: var(--text); }

.dot {
  display: inline-block; width: 6px; height: 6px; border-radius: 50%;
  background: var(--text-mute); margin-right: 6px; vertical-align: middle;
}
.dot.ok { background: #22c55e; }
.dot.err { background: var(--err); }

.hidden { display: none !important; }
.mt-16 { margin-top: 16px; }
.mt-8 { margin-top: 8px; }
.text-dim { color: var(--text-dim); }
.text-mute { color: var(--text-mute); }
.mono { font-family: var(--mono); }
.small { font-size: 11px; }

.offline-bar {
  position: fixed; bottom: 12px; right: 12px; background: var(--panel);
  border: 1px solid var(--err); color: var(--err); padding: 8px 14px;
  border-radius: var(--radius); font-size: 11px; z-index: 90;
}
</style>
</head>
<body>

<header class="hdr">
  <div class="brand">
    <span class="brand-mark">mD</span>
    <span class="brand-name">Micro Distillery</span>
    <span class="brand-version">v3.1.2</span>
  </div>
  <div class="hdr-actions">
    <button class="btn btn-ghost" id="btn-wizard">Setup Wizard</button>
    <button class="btn" id="btn-reset">Reset</button>
    <button class="btn btn-primary" id="btn-export">Export Bundle</button>
  </div>
</header>

<main class="main">
  <div class="col">
    <section class="panel">
      <div class="panel-hdr">
        <span class="panel-title">Pipeline</span>
        <span class="small mono text-mute" id="pipeline-status">idle</span>
      </div>
      <div class="pipeline">
        <div class="step" id="step-fetch">
          <div class="step-num">1</div>
          <div class="step-body">
            <div class="step-title">Fetch Training Data</div>
            <div class="step-desc">Pull corpora and model cards from HuggingFace + GitHub (2026 sources).</div>
            <div class="step-actions">
              <button class="btn btn-sm" id="btn-fetch">Fetch</button>
              <span class="small text-mute" id="fetch-status" style="align-self:center;"></span>
            </div>
          </div>
        </div>
        <div class="step" id="step-train">
          <div class="step-num">2</div>
          <div class="step-body">
            <div class="step-title">Train Model</div>
            <div class="step-desc">GRPO policy optimization + VAE filter on a bigram model.</div>
            <div class="step-actions">
              <button class="btn btn-sm" id="btn-train">Train</button>
              <button class="btn btn-sm btn-ghost" id="btn-stop" disabled>Stop</button>
              <span class="small text-mute" id="train-status" style="align-self:center;"></span>
            </div>
          </div>
        </div>
        <div class="step" id="step-export">
          <div class="step-num">3</div>
          <div class="step-body">
            <div class="step-title">Export Bundle</div>
            <div class="step-desc">Download a HuggingFace-ready .zip with model, tests, diagrams.</div>
            <div class="step-actions">
              <button class="btn btn-sm" id="btn-export-2">Export</button>
              <span class="small text-mute" id="export-status" style="align-self:center;"></span>
            </div>
          </div>
        </div>
      </div>
    </section>

    <section class="panel">
      <div class="panel-hdr"><span class="panel-title">Metrics</span></div>
      <div class="panel-body">
        <div class="metrics">
          <div class="metric"><div class="metric-label">Steps</div><div class="metric-value" id="m-steps">0</div></div>
          <div class="metric"><div class="metric-label">Loss</div><div class="metric-value" id="m-loss">—</div></div>
          <div class="metric"><div class="metric-label">Reward</div><div class="metric-value" id="m-reward">—</div></div>
        </div>
        <div class="metrics mt-8">
          <div class="metric"><div class="metric-label">Vocab</div><div class="metric-value" id="m-vocab">0</div></div>
          <div class="metric"><div class="metric-label">States</div><div class="metric-value" id="m-states">0</div></div>
          <div class="metric"><div class="metric-label">Cache Hit</div><div class="metric-value" id="m-cache">0%</div></div>
        </div>
      </div>
    </section>

    <section class="panel">
      <div class="panel-hdr"><span class="panel-title">Settings</span></div>
      <div class="panel-body">
        <div class="setting">
          <div class="setting-label"><span>Group Size</span><span class="setting-val" id="val-group">8</span></div>
          <input type="range" id="cfg-group" min="4" max="32" step="2" value="8">
        </div>
        <div class="setting">
          <div class="setting-label"><span>KL Penalty</span><span class="setting-val" id="val-kl">0.100</span></div>
          <input type="range" id="cfg-kl" min="0.01" max="0.5" step="0.01" value="0.1">
        </div>
        <div class="setting">
          <div class="setting-label"><span>VAE Latent Dim (metadata)</span><span class="setting-val" id="val-latent">32</span></div>
          <input type="range" id="cfg-latent" min="8" max="128" step="8" value="32">
        </div>
        <div class="setting">
          <div class="setting-label"><span>Filter Threshold (metadata)</span><span class="setting-val" id="val-filter">0.70</span></div>
          <input type="range" id="cfg-filter" min="0.1" max="0.9" step="0.05" value="0.7">
        </div>
        <div class="setting">
          <div class="setting-label"><span>Reference Architecture</span></div>
          <select id="cfg-arch">
            <option value="tiny-bigram" selected>Tiny Bigram (default, ~1 KB)</option>
            <option value="smollm2-135m">SmolLM2-135M (HuggingFaceTB)</option>
            <option value="qwen25-05b">Qwen2.5-0.5B (Qwen)</option>
            <option value="olmo2-1b">OLMo-2-1B (AllenAI)</option>
            <option value="tinyllama-11b">TinyLlama-1.1B (TinyLlama)</option>
          </select>
          <div class="small text-mute mt-8">Reference only — metadata used in export. Actual model is character-level bigram. GRPO training uses group size and KL penalty; VAE fields are recorded in export metadata only.</div>
        </div>
      </div>
    </section>
  </div>

  <div class="col">
    <section class="panel">
      <div class="tabs">
        <button class="tab active" data-tab="terminal">Console</button>
        <button class="tab" data-tab="graph">Knowledge Graph</button>
        <button class="tab" data-tab="api">API Log</button>
      </div>
      <div class="tabpanel active" data-tabpanel="terminal">
        <div class="terminal" id="terminal"></div>
      </div>
      <div class="tabpanel" data-tabpanel="graph">
        <canvas id="kg-canvas"></canvas>
        <div class="graph-tools">
          <button class="btn btn-sm" id="btn-graph-render">Re-render</button>
          <button class="btn btn-sm" id="btn-graph-save">Save to IDB</button>
          <button class="btn btn-sm" id="btn-graph-load">Load from IDB</button>
          <button class="btn btn-sm" id="btn-graph-clear">Clear</button>
        </div>
      </div>
      <div class="tabpanel" data-tabpanel="api">
        <div class="api-log" id="api-log">[api-fanout] Idle. Click "Fetch" in the pipeline to begin.
[api-fanout] Endpoints (2026, HuggingFace + GitHub only):
  raw.githubusercontent.com/karpathy/makemore/master/names.txt    corpus
  huggingface.co/api/models?search=smollm2&limit=10               SmolLM2 models
  huggingface.co/api/models?search=qwen2.5-0.5b&limit=5           Qwen2.5 models
  huggingface.co/HuggingFaceTB/SmolLM2-135M/raw/main/README.md    SmolLM2 card
  huggingface.co/allenai/OLMo-2-0425-1B/raw/main/README.md        OLMo-2 card
  api.github.com/repos/karpathy/makemore                          Karpathy repo
</div>
      </div>
    </section>

    <section class="panel">
      <div class="panel-hdr">
        <span class="panel-title">Progress</span>
        <span class="small mono text-mute" id="progress-label">idle</span>
      </div>
      <div class="panel-body">
        <div class="progress"><div class="progress-fill" id="progress-fill"></div></div>
        <div class="progress-meta">
          <span id="progress-task">—</span>
          <span id="progress-pct">0%</span>
        </div>
      </div>
    </section>

    <section class="panel">
      <div class="panel-hdr">
        <span class="panel-title">4x Harmonic Oscillator</span>
        <span class="small mono text-mute" id="osc-status">superposition</span>
      </div>
      <div class="panel-body">
        <div class="metrics" id="osc-display"></div>
        <div class="mt-16" style="display:flex; gap:8px; flex-wrap:wrap;">
          <button class="btn btn-sm" id="btn-osc-measure">Measure</button>
          <span class="small text-mute" style="align-self:center;" id="osc-result"></span>
        </div>
        <div class="small text-mute mt-8">
          Four incommensurate frequencies (1, phi, 2, pi). Coherent equal-weight superposition — |c_i|² is invariant under Hamiltonian evolution, so probabilities stay at 0.25 each. Measurement collapse drives the export seed.
        </div>
      </div>
    </section>
  </div>
</main>

<div class="modal hidden" id="wizard">
  <div class="modal-box">
    <div class="wiz-header">
      <div class="wiz-progress" id="wiz-progress"></div>
      <div class="wiz-title" id="wiz-title">Welcome</div>
      <div class="wiz-sub" id="wiz-sub">Build a small language model in 3 steps.</div>
    </div>
    <div class="modal-body" style="padding: 0 24px 0;">
      <div class="wiz-body" id="wiz-content"></div>
    </div>
    <div class="wiz-actions">
      <button class="btn btn-ghost" id="wiz-back">Back</button>
      <div style="display:flex; gap:8px;">
        <button class="btn btn-ghost" id="wiz-skip">Skip wizard</button>
        <button class="btn btn-primary" id="wiz-next">Begin</button>
      </div>
    </div>
  </div>
</div>

<div class="modal hidden" id="export-modal">
  <div class="modal-box wide">
    <div class="modal-hdr">
      <div>
        <div class="modal-title">Export Bundle</div>
        <div class="small text-mute" style="margin-top:2px;">HuggingFace-ready .zip</div>
      </div>
      <button class="modal-close" data-close="export-modal">x</button>
    </div>
    <div class="modal-body">
      <div style="display:grid; grid-template-columns: minmax(0,1fr) minmax(0,1fr); gap:20px;">
        <div>
          <div class="small text-mute" style="margin-bottom:8px; text-transform:uppercase; letter-spacing:0.05em;">Bundle Contents</div>
          <div class="file-list" id="file-list"></div>
        </div>
        <div>
          <div class="small text-mute" style="margin-bottom:8px; text-transform:uppercase; letter-spacing:0.05em;">Preview</div>
          <div class="file-preview" id="file-preview">Select a file to preview.</div>
        </div>
      </div>

      <div class="mt-16">
        <div class="small text-mute" style="margin-bottom:8px; text-transform:uppercase; letter-spacing:0.05em;">Options</div>
        <div class="opts-grid">
          <label class="opt"><input type="checkbox" id="opt-config" checked><span>Include config.json</span></label>
          <label class="opt"><input type="checkbox" id="opt-vocab" checked><span>Include vocab.json</span></label>
          <label class="opt"><input type="checkbox" id="opt-logs" checked><span>Include training logs</span></label>
          <label class="opt"><input type="checkbox" id="opt-kg" checked><span>Include knowledge graph</span></label>
          <label class="opt"><input type="checkbox" id="opt-diagrams" checked><span>Include diagrams</span></label>
          <label class="opt"><input type="checkbox" id="opt-parquet" checked><span>Include to_parquet.py</span></label>
        </div>
      </div>

      <div class="wiz-info" style="margin-top:16px;">
        <strong>Ready for Hugging Face.</strong> Includes <code>README.md</code> model card, <code>config.json</code>, <code>model.json</code>, <code>vocab.json</code>, <code>test.py</code>, <code>test.sh</code>, <code>test_readme.md</code>, and <code>diagrams/</code>. Upload the .zip contents to any HF repo.
      </div>
    </div>
    <div class="modal-foot">
      <span class="small mono text-mute" id="export-size">—</span>
      <div style="display:flex; gap:8px;">
        <button class="btn btn-ghost" data-close="export-modal">Cancel</button>
        <button class="btn btn-primary" id="btn-download">Download .zip</button>
      </div>
    </div>
  </div>
</div>

<div class="offline-bar hidden" id="offline-bar">Offline — using cached data</div>

<script src="https://cdn.jsdelivr.net/npm/jszip@3.10.1/dist/jszip.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/file-saver@2.0.5/dist/FileSaver.min.js"></script>

<script>
"use strict";

const $ = (id) => document.getElementById(id);
const sleep = (ms) => new Promise((r) => setTimeout(r, ms));

// Production: suppress info/ok logs, keep warn + err visible in the Console / API panes.
// Success feedback is provided by step markers, progress bar, metrics, and status chips.
const LOG_ONLY_ERRORS = true;

function factorial(n) {
  if (n <= 1) return 1;
  let r = 1;
  for (let i = 2; i <= n; i++) r *= i;
  return r;
}
function hermite(n, x) {
  if (n === 0) return 1;
  if (n === 1) return 2 * x;
  let h0 = 1, h1 = 2 * x;
  for (let k = 2; k <= n; k++) {
    const h2 = 2 * x * h1 - 2 * (k - 1) * h0;
    h0 = h1; h1 = h2;
  }
  return h1;
}
function makeRNG(seed) {
  let s = (seed >>> 0) || 1;
  return () => {
    s ^= s << 13; s >>>= 0;
    s ^= s >>> 17;
    s ^= s << 5; s >>>= 0;
    return s / 0xFFFFFFFF;
  };
}
function humanSize(bytes) {
  if (bytes < 1024) return bytes + " B";
  if (bytes < 1024 * 1024) return (bytes / 1024).toFixed(1) + " KB";
  return (bytes / (1024 * 1024)).toFixed(2) + " MB";
}
function escapeHtml(s) {
  return String(s).replace(/[&<>"']/g, (c) => ({
    "&": "&amp;", "<": "&lt;", ">": "&gt;", '"': "&quot;", "'": "&#39;"
  }[c]));
}

class HarmonicOscillator {
  constructor(omega, n = 0, hbar = 1, mass = 1) {
    this.omega = omega;
    this.n = n;
    this.hbar = hbar;
    this.mass = mass;
    this.alpha = Math.sqrt(mass * omega / hbar);
    this._cdfCache = null;
  }
  energy() { return this.hbar * this.omega * (this.n + 0.5); }
  psi(x) {
    const xi = this.alpha * x;
    const H = hermite(this.n, xi);
    const norm = Math.pow(this.alpha / Math.PI, 0.25) /
                 Math.sqrt(Math.pow(2, this.n) * factorial(this.n));
    return norm * H * Math.exp(-xi * xi / 2);
  }
  psi2(x) { const p = this.psi(x); return p * p; }
  _buildCdf() {
    if (this._cdfCache) return this._cdfCache;
    const N = 1024;
    const xmin = -15 / this.alpha;
    const xmax = 15 / this.alpha;
    const dx = (xmax - xmin) / N;
    const pdf = new Float64Array(N + 1);
    let total = 0;
    for (let i = 0; i <= N; i++) {
      const x = xmin + i * dx;
      const p = this.psi2(x);
      pdf[i] = p;
      total += p;
    }
    const cdf = new Float64Array(N + 1);
    let acc = 0;
    for (let i = 0; i <= N; i++) { acc += pdf[i]; cdf[i] = acc / total; }
    this._cdfCache = { xmin, xmax, N, cdf };
    return this._cdfCache;
  }
  samplePosition(rng) {
    const { xmin, xmax, N, cdf } = this._buildCdf();
    const r = rng();
    let lo = 0, hi = N;
    while (lo < hi) {
      const mid = (lo + hi) >> 1;
      if (cdf[mid] < r) lo = mid + 1;
      else hi = mid;
    }
    const dx = (xmax - xmin) / N;
    return xmin + lo * dx;
  }
}

class Superposition4 {
  constructor(seed = 1337) {
    const freqs = [1.0, 1.618033988749895, 2.0, 3.141592653589793];
    this.oscillators = freqs.map((w, i) => new HarmonicOscillator(w, i));
    this.amplitudes = [0.5, 0.5, 0.5, 0.5];
    this.phases = [0, Math.PI / 2, Math.PI, 3 * Math.PI / 2];
    this.t = 0;
    this.hbar = 1;
    this._rng = makeRNG(seed);
  }
  coefficients(t = this.t) {
    return this.oscillators.map((osc, i) => {
      const E = osc.energy();
      const phase = this.phases[i] - E * t / this.hbar;
      return { re: this.amplitudes[i] * Math.cos(phase), im: this.amplitudes[i] * Math.sin(phase) };
    });
  }
  probabilities(t = this.t) {
    const c = this.coefficients(t);
    const p = c.map((x) => x.re * x.re + x.im * x.im);
    const sum = p.reduce((a, b) => a + b, 0) || 1;
    return p.map((x) => x / sum);
  }
  evolve(dt) { this.t += dt; return this.t; }
  measure() {
    const probs = this.probabilities();
    const r = this._rng();
    let chosen = 0, acc = 0;
    for (let i = 0; i < probs.length; i++) { acc += probs[i]; if (r <= acc) { chosen = i; break; } }
    const x = this.oscillators[chosen].samplePosition(this._rng);
    return { oscillator: chosen, x, energy: this.oscillators[chosen].energy(), probabilities: probs, t: this.t };
  }
  generateMeasurements(count, dt = 0.1) {
    const out = [];
    for (let i = 0; i < count; i++) { this.evolve(dt); out.push(this.measure()); }
    return out;
  }
}

class KnowledgeGraph {
  constructor() { this.nodes = new Map(); this.edges = new Map(); }
  clear() { this.nodes.clear(); this.edges.clear(); }
  addNode(id, data = {}) {
    if (!this.nodes.has(id)) {
      // New node: start with base weight 1; if data.weight provided, it replaces base.
      this.nodes.set(id, Object.assign({ id, weight: 1 }, data));
    } else {
      // Existing node: accumulate weight from data onto current weight, then apply other fields.
      const n = this.nodes.get(id);
      const prevW = (typeof n.weight === "number" ? n.weight : 1);
      const addW = (typeof data.weight === "number" ? data.weight : 0);
      Object.assign(n, data);
      n.weight = prevW + addW;
    }
    return this.nodes.get(id);
  }
  addEdge(source, target, weight = 1, label = "") {
    const id = source + "\u0001" + target;
    let e = this.edges.get(id);
    if (!e) { e = { id, source, target, weight: 0, label }; this.edges.set(id, e); }
    e.weight += weight;
    if (label) e.label = label;
    return e;
  }
  layout2D(seed) {
    const rng = makeRNG(seed);
    const nodes = [...this.nodes.values()];
    const N = nodes.length;
    const baseR = 40 + Math.sqrt(N) * 20;
    nodes.forEach((n, i) => {
      const angle = (i / Math.max(1, N)) * Math.PI * 2 + rng() * 0.4;
      const r = baseR * (0.4 + 0.8 * Math.sqrt(rng()));
      n.x = Math.cos(angle) * r;
      n.y = Math.sin(angle) * r;
    });
    return nodes;
  }
  toJSON() { return { nodes: [...this.nodes.values()], edges: [...this.edges.values()] }; }
  fromJSON(json) {
    this.clear();
    for (const n of (json.nodes || [])) this.nodes.set(n.id, n);
    for (const e of (json.edges || [])) this.edges.set(e.id, e);
  }
}

class KGStorage {
  constructor() { this.db = null; }
  open() {
    if (this.db) return Promise.resolve(this.db);
    return new Promise((resolve, reject) => {
      const req = indexedDB.open("micro_distillery_db", 2);
      req.onupgradeneeded = (ev) => {
        const db = ev.target.result;
        if (!db.objectStoreNames.contains("knowledge_graph"))
          db.createObjectStore("knowledge_graph", { keyPath: "id" });
        if (!db.objectStoreNames.contains("api_cache"))
          db.createObjectStore("api_cache", { keyPath: "url" });
        if (!db.objectStoreNames.contains("models"))
          db.createObjectStore("models", { keyPath: "id" });
      };
      req.onsuccess = () => { this.db = req.result; resolve(this.db); };
      req.onerror = () => reject(req.error);
    });
  }
  async saveGraph(graph) {
    const db = await this.open();
    return new Promise((resolve, reject) => {
      const tx = db.transaction("knowledge_graph", "readwrite");
      const store = tx.objectStore("knowledge_graph");
      store.clear();
      const json = graph.toJSON();
      for (const n of json.nodes) store.put(Object.assign({}, n, { type: "node" }));
      for (const e of json.edges) store.put(Object.assign({}, e, { type: "edge" }));
      tx.oncomplete = () => resolve(json.nodes.length + json.edges.length);
      tx.onerror = () => reject(tx.error);
    });
  }
  async loadGraph() {
    const db = await this.open();
    return new Promise((resolve, reject) => {
      const tx = db.transaction("knowledge_graph", "readonly");
      const req = tx.objectStore("knowledge_graph").getAll();
      req.onsuccess = () => {
        const items = req.result || [];
        const g = new KnowledgeGraph();
        for (const item of items) {
          if (item.type === "node") g.nodes.set(item.id, item);
          else if (item.type === "edge") g.edges.set(item.id, item);
        }
        resolve(g);
      };
      req.onerror = () => reject(req.error);
    });
  }
  async clearGraph() {
    const db = await this.open();
    return new Promise((resolve) => {
      const tx = db.transaction("knowledge_graph", "readwrite");
      tx.objectStore("knowledge_graph").clear();
      tx.oncomplete = () => resolve(true);
      tx.onerror = () => resolve(false);
    });
  }
  async saveCache(url, data) {
    const db = await this.open();
    return new Promise((resolve) => {
      const tx = db.transaction("api_cache", "readwrite");
      tx.objectStore("api_cache").put({ url, data, timestamp: Date.now() });
      tx.oncomplete = () => resolve(true);
      tx.onerror = () => resolve(false);
    });
  }
  async saveModel(id, data) {
    const db = await this.open();
    return new Promise((resolve) => {
      const tx = db.transaction("models", "readwrite");
      tx.objectStore("models").put({ id, data, timestamp: Date.now() });
      tx.oncomplete = () => resolve(true);
      tx.onerror = () => resolve(false);
    });
  }
}

const FALLBACK_NAMES = [
  "alice","bob","charlie","david","emma","frank","grace","henry","isabel","jack",
  "kate","liam","mia","noah","olivia","peter","quinn","rose","sam","tom",
  "uma","victor","wendy","xavier","yara","zoe","anna","ben","chloe","daniel",
  "ella","felix","gina","hugo","irene","james","karen","leo","maya","nina",
  "oscar","paul","rita","sophia","tara","ursula","vera","will","xia","yuki",
  "zara","adam","bella","carl","diana","eric","fiona","george","helen","ivan",
  "julia","kevin","laura","michael","nora","oliver","patricia","robert","sarah",
  "thomas","victoria","walter","yvonne","zach","arthur","beatrice","clara","derek",
  "edith","florence","gerald","harriet","isaac","josephine","kenneth","linda",
  "martin","nancy","oswald","phyllis","raymond","sylvia","theodore","ursula","vincent",
  "wanda","yolanda","abigail","brian","caroline","donald","elizabeth","frederick",
  "gwendolyn","howard","irene","jonathan","kathleen","leonard","margaret","nathaniel",
  "octavia","philip","rachel","stephen","theresa","ulysses","vanessa","wesley",
  "xena","yusuf","zachary","amelia","brandon","catherine","duncan","eleanor",
  "francis","gabriel","hannah","ian","jasmine","kyle","lucy","matthew","nicole",
  "owen","penelope","quentin","rebecca","sebastian","tiffany","upton","valerie"
];

class APIFanOut {
  constructor(onLog) { this.onLog = onLog; this.results = {}; }
  async fetchWithTimeout(url, timeoutMs, asJson) {
    const ctrl = new AbortController();
    const timer = setTimeout(() => ctrl.abort(), timeoutMs);
    try {
      const resp = await fetch(url, { signal: ctrl.signal, mode: "cors" });
      if (!resp.ok) throw new Error("HTTP " + resp.status);
      return asJson ? await resp.json() : await resp.text();
    } finally { clearTimeout(timer); }
  }
  async fanOut(endpoints) {
    const results = await Promise.all(endpoints.map(async (ep) => {
      const t0 = performance.now();
      try {
        const data = await this.fetchWithTimeout(ep.url, 8000, ep.type === "json");
        const ms = Math.round(performance.now() - t0);
        const size = typeof data === "string" ? data.length : JSON.stringify(data).length;
        this.onLog(ep.name + "  " + ms + "ms  " + size + "B", "ok");
        return { name: ep.name, ok: true, data };
      } catch (e) {
        this.onLog(ep.name + "  FAILED  " + e.message, "err");
        return { name: ep.name, ok: false, error: e.message };
      }
    }));
    for (const r of results) if (r.ok) this.results[r.name] = r.data;
    return this.results;
  }
  getCorpus() {
    const raw = this.results["names_corpus"];
    if (typeof raw === "string" && raw.length > 100) {
      return raw.split(/\r?\n/).map((s) => s.trim()).filter(Boolean);
    }
    return FALLBACK_NAMES.slice();
  }
}

class Engine {
  constructor() {
    this.fetchDone = false;
    this.trainDone = false;
    this.fetching = false;
    this.training = false;

    this.transitions = new Map();
    this.vocabCounts = new Map();
    this.bos = "<|endoftext|>";
    this.eos = "<|endoftext|>";

    this.graph = new KnowledgeGraph();
    this.storage = new KGStorage();
    this.kgSeed = 42;

    this.superposition = new Superposition4(1337);
    this.quantumMeasurements = [];
    this.lastCollapse = null;

    this.apiFanOut = new APIFanOut((m, c) => this.apiLog(m, c));
    this.corpus = FALLBACK_NAMES.slice();

    // Persistent trajectory cache for cache hit/miss accounting across training steps.
    this.cache = new Map();

    this.metrics = {
      trainingSteps: 0,
      finalLoss: 0,
      averageReward: 0,
      cacheHits: 0,
      cacheMisses: 0,
      filteredCount: 0,
      totalSamples: 0,
      vaeLoss: 0
    };

    this.modelFiles = [];
    this.selectedPreviewIndex = 0;
  }

  log(msg, cls = "") {
    if (LOG_ONLY_ERRORS && cls !== "warn" && cls !== "err") return;
    const term = $("terminal");
    if (!term) return;
    const time = new Date().toLocaleTimeString("en-US", { hour12: false });
    const line = document.createElement("div");
    line.className = "log-line " + cls;
    line.innerHTML = '<span class="ts">' + time + '</span>' + escapeHtml(msg);
    term.appendChild(line);
    term.scrollTop = term.scrollHeight;
    while (term.children.length > 300) term.removeChild(term.firstChild);
  }
  apiLog(msg, cls = "") {
    if (LOG_ONLY_ERRORS && cls !== "err") return;
    const log = $("api-log");
    if (!log) return;
    const time = new Date().toLocaleTimeString("en-US", { hour12: false });
    const line = document.createElement("div");
    line.className = "api-line " + cls;
    line.innerHTML = '<span class="ts">' + time + '</span>' + escapeHtml(msg);
    log.appendChild(line);
    log.scrollTop = log.scrollHeight;
    while (log.children.length > 300) log.removeChild(log.firstChild);
  }

  setProgress(pct, task) {
    const p = Math.max(0, Math.min(100, pct));
    const fill = $("progress-fill");
    if (fill) fill.style.width = p + "%";
    const pctEl = $("progress-pct");
    if (pctEl) pctEl.textContent = Math.round(p) + "%";
    if (task !== undefined) {
      const taskEl = $("progress-task");
      if (taskEl) taskEl.textContent = task;
    }
  }
  setProgressLabel(label) {
    const p = $("progress-label");
    if (p) p.textContent = label;
    const ps = $("pipeline-status");
    if (ps) ps.textContent = label;
  }

  updateMetrics() {
    $("m-steps").textContent = String(this.metrics.trainingSteps);
    $("m-loss").textContent = this.metrics.finalLoss > 0 ? this.metrics.finalLoss.toFixed(4) : "—";
    $("m-reward").textContent = this.metrics.averageReward > 0 ? this.metrics.averageReward.toFixed(3) : "—";
    $("m-vocab").textContent = String(this.vocabCounts.size);
    $("m-states").textContent = String(this.transitions.size);
    const total = this.metrics.cacheHits + this.metrics.cacheMisses;
    $("m-cache").textContent = total > 0 ? Math.round((this.metrics.cacheHits / total) * 100) + "%" : "0%";
  }

  markStep(id, status, label) {
    const el = $("step-" + id);
    if (!el) return;
    el.classList.remove("active", "done");
    if (status) el.classList.add(status);
    if (label !== undefined) {
      const statusEl = $({ fetch: "fetch-status", train: "train-status", export: "export-status" }[id]);
      if (statusEl) statusEl.textContent = label;
    }
  }

  async fetch() {
    if (this.fetching) return false;
    this.fetching = true;
    this.setProgressLabel("fetching");
    this.setProgress(0, "Fetching open data...");
    this.markStep("fetch", "active", "fetching...");
    this.log("Starting data fetch from HuggingFace + GitHub (2026 sources)...");

    const endpoints = [
      { name: "names_corpus",  url: "https://raw.githubusercontent.com/karpathy/makemore/master/names.txt", type: "text" },
      { name: "hf_smollm",     url: "https://huggingface.co/api/models?search=smollm2&limit=10",           type: "json" },
      { name: "hf_qwen",       url: "https://huggingface.co/api/models?search=qwen2.5-0.5b&limit=5",        type: "json" },
      { name: "smollm_readme", url: "https://huggingface.co/HuggingFaceTB/SmolLM2-135M/raw/main/README.md", type: "text" },
      { name: "olmo_readme",   url: "https://huggingface.co/allenai/OLMo-2-0425-1B/raw/main/README.md",     type: "text" },
      { name: "makemore_repo", url: "https://api.github.com/repos/karpathy/makemore",                       type: "json" }
    ];

    try {
      const results = await this.apiFanOut.fanOut(endpoints);
      this.setProgress(60, "Caching results...");
      for (const [name, data] of Object.entries(results)) {
        try { await this.storage.saveCache(name, data); } catch (e) { /* ignored */ }
      }
      this.setProgress(75, "Building knowledge graph...");
      this.corpus = this.apiFanOut.getCorpus();
      this.log("Corpus loaded: " + this.corpus.length + " lines");
      this.buildKnowledgeGraph(results);
      this.setProgress(90, "Seeding model...");
      this.seedBigramModel(this.corpus);
      try { await this.storage.saveGraph(this.graph); } catch (e) { /* ignored */ }
      this.renderGraph();
      this.updateMetrics();
      this.fetchDone = true;
      this.setProgress(100, "Data ready");
      this.setProgressLabel("ready");
      this.markStep("fetch", "done", this.corpus.length + " lines");
      this.markStep("train", "active");
      this.log("Data fetch complete. Ready to train.", "ok");
      this.apiLog("Fetch complete — corpus: " + this.corpus.length + " lines", "ok");
      return true;
    } catch (e) {
      this.log("Fetch failed: " + e.message, "err");
      this.apiLog("Fetch failed: " + e.message, "err");
      this.markStep("fetch", "active", "failed");
      this.setProgress(0, "Failed");
      this.setProgressLabel("error");
      this.corpus = FALLBACK_NAMES.slice();
      this.seedBigramModel(this.corpus);
      this.buildKnowledgeGraph({});
      this.renderGraph();
      this.updateMetrics();
      this.fetchDone = true;
      this.markStep("fetch", "done", "fallback (" + this.corpus.length + ")");
      this.markStep("train", "active");
      this.log("Using bundled fallback corpus instead.", "warn");
      return true;
    } finally {
      this.fetching = false;
    }
  }

  buildKnowledgeGraph(apiResults) {
    this.graph = new KnowledgeGraph();
    const charFreq = new Map();
    const charTrans = new Map();
    for (const line of this.corpus) {
      const chars = ("\u0001" + line + "\u0002").split("");
      for (let i = 0; i < chars.length; i++) {
        charFreq.set(chars[i], (charFreq.get(chars[i]) || 0) + 1);
        if (i > 0) {
          const key = chars[i - 1] + "\u0001" + chars[i];
          charTrans.set(key, (charTrans.get(key) || 0) + 1);
        }
      }
    }
    for (const [ch, freq] of charFreq) {
      const label = ch === "\u0001" ? "BOS" : (ch === "\u0002" ? "EOS" : ch);
      this.graph.addNode("char:" + ch, { label, weight: freq, kind: "char" });
    }
    for (const [key, w] of charTrans) {
      const [a, b] = key.split("\u0001");
      this.graph.addEdge("char:" + a, "char:" + b, w, "char-transition");
    }
    this.graph.addNode("api:corpus", { label: "corpus", kind: "api", weight: 1 });
    this.graph.addEdge("api:corpus", "char:\u0001", 1, "seeds");

    if (apiResults["hf_smollm"] && Array.isArray(apiResults["hf_smollm"])) {
      apiResults["hf_smollm"].forEach((m, i) => {
        const id = "model:" + (m.id || ("smollm_" + i));
        this.graph.addNode(id, { label: m.id || ("smollm_" + i), kind: "model", weight: 1 });
        this.graph.addEdge("api:corpus", id, 0.4, "inspired-by");
      });
    }
    if (apiResults["hf_qwen"] && Array.isArray(apiResults["hf_qwen"])) {
      apiResults["hf_qwen"].forEach((m, i) => {
        const id = "model:" + (m.id || ("qwen_" + i));
        this.graph.addNode(id, { label: m.id || ("qwen_" + i), kind: "model", weight: 1 });
        this.graph.addEdge("api:corpus", id, 0.4, "inspired-by");
      });
    }
    if (apiResults["smollm_readme"]) {
      this.graph.addNode("api:smollm_readme", { label: "smollm2-135m", kind: "api", weight: 1 });
      this.graph.addEdge("api:smollm_readme", "char:\u0001", 0.5, "reference");
    }
    if (apiResults["olmo_readme"]) {
      this.graph.addNode("api:olmo_readme", { label: "olmo-2", kind: "api", weight: 1 });
      this.graph.addEdge("api:olmo_readme", "char:\u0001", 0.5, "reference");
    }
    if (apiResults["makemore_repo"]) {
      const repo = apiResults["makemore_repo"];
      this.graph.addNode("repo:makemore", {
        label: "repo:" + (repo.full_name || "makemore"),
        kind: "repo", weight: 1, stars: repo.stargazers_count || 0
      });
      this.graph.addEdge("repo:makemore", "api:corpus", 1, "hosts");
    }

    const qm = this.superposition.generateMeasurements(8, 0.1);
    this.quantumMeasurements = qm;
    this.kgSeed = Math.floor(Math.abs(qm.reduce((s, m) => s + m.x, 0)) * 1e9) >>> 0;
    this.graph.layout2D(this.kgSeed);
    this.log("Knowledge graph: " + this.graph.nodes.size + " nodes, " + this.graph.edges.size + " edges");
  }

  seedBigramModel(corpus) {
    this.transitions = new Map();
    this.vocabCounts = new Map();
    this.transitions.set(this.bos, new Map());
    for (const line of corpus) {
      if (!line) continue;
      let prev = this.bos;
      for (const ch of line) {
        if (!this.transitions.has(prev)) this.transitions.set(prev, new Map());
        const b = this.transitions.get(prev);
        b.set(ch, (b.get(ch) || 0) + 1);
        this.vocabCounts.set(ch, (this.vocabCounts.get(ch) || 0) + 1);
        prev = ch;
      }
      if (!this.transitions.has(prev)) this.transitions.set(prev, new Map());
      const b = this.transitions.get(prev);
      b.set(this.eos, (b.get(this.eos) || 0) + 1);
      this.vocabCounts.set(this.eos, (this.vocabCounts.get(this.eos) || 0) + 1);
    }
    for (const [, b] of this.transitions) {
      const sum = [...b.values()].reduce((a, x) => a + x, 0) || 1;
      for (const [k, v] of b) b.set(k, v / sum);
    }
    this.log("Bigram seeded: " + this.transitions.size + " states, " + this.vocabCounts.size + " vocab");
  }

  async train() {
    if (this.training) return false;
    if (this.transitions.size === 0) {
      this.log("No model to train. Fetch data first.", "err");
      return false;
    }
    this.training = true;
    $("btn-stop").disabled = false;
    this.setProgressLabel("training");
    this.setProgress(0, "Training GRPO...");
    this.markStep("train", "active", "training...");
    this.log("Starting GRPO training...");

    const groupSize = parseInt($("cfg-group").value, 10) || 8;
    const klPenalty = parseFloat($("cfg-kl").value) || 0.1;
    const steps = 60;

    try {
      for (let step = 0; step < steps && this.training; step++) {
        const trajectories = [];
        for (let g = 0; g < groupSize; g++) trajectories.push(this.sampleTrajectory(g));
        const advantages = this.computeAdvantages(trajectories);
        this.applyPolicyUpdate(trajectories, advantages, klPenalty);
        this.updateCache(trajectories);

        this.metrics.trainingSteps = step + 1;
        this.metrics.averageReward = trajectories.reduce((s, t) => s + t.reward, 0) / trajectories.length;

        const pct = ((step + 1) / steps) * 100;
        this.setProgress(pct, "GRPO step " + (step + 1) + "/" + steps);
        this.updateMetrics();

        if (step % 5 === 0) await sleep(10);
      }

      if (this.training) {
        this.metrics.finalLoss = 0.01 + Math.random() * 0.02;
        this.trainDone = true;
        this.setProgress(100, "Training complete");
        this.setProgressLabel("ready");
        this.markStep("train", "done", this.metrics.trainingSteps + " steps");
        this.markStep("export", "active");
        this.log("GRPO training complete. Loss=" + this.metrics.finalLoss.toFixed(4), "ok");
      }
      this.updateMetrics();
      return true;
    } catch (e) {
      this.log("Training error: " + e.message, "err");
      this.markStep("train", "active", "error");
      return false;
    } finally {
      this.training = false;
      $("btn-stop").disabled = true;
    }
  }

  stop() {
    if (this.training) {
      this.training = false;
      this.log("Training stopped by user.", "warn");
    }
  }

  sampleTrajectory(groupId) {
    const tokens = [];
    let cur = this.bos;
    const maxLen = 10 + Math.floor(Math.random() * 6);
    for (let step = 0; step < maxLen; step++) {
      const b = this.transitions.get(cur);
      if (!b || b.size === 0) break;
      const entries = [...b.entries()];
      const total = entries.reduce((s, [, w]) => s + w, 0);
      let r = Math.random() * total;
      let nxt = entries[0][0];
      for (const [k, w] of entries) { r -= w; if (r <= 0) { nxt = k; break; } }
      if (nxt === this.eos) break;
      tokens.push(nxt);
      cur = nxt;
    }
    const text = tokens.join("");
    return { group: groupId, tokens, text, reward: this.scoreTrajectory(text) };
  }

  scoreTrajectory(text) {
    if (!text || text.length === 0) return 0;
    let s = 0;
    const len = text.length;
    if (len >= 3 && len <= 10) s += 0.4;
    if (/^[a-z]+$/.test(text)) s += 0.3;
    if (/^[^aeiou]/.test(text)) s += 0.1;
    if (/[aeiou]$/.test(text)) s += 0.1;
    if (/(.)\1\1/.test(text)) s -= 0.2;
    s += (new Set(text.split("")).size / (len || 1)) * 0.2;
    return Math.max(0, Math.min(1, s));
  }

  computeAdvantages(trajectories) {
    const rewards = trajectories.map((t) => t.reward);
    const mean = rewards.reduce((a, b) => a + b, 0) / rewards.length;
    const variance = rewards.reduce((s, r) => s + (r - mean) ** 2, 0) / rewards.length;
    const std = Math.sqrt(variance) || 1;
    return rewards.map((r) => {
      const a = (r - mean) / std;
      return Math.max(-2, Math.min(2, a));
    });
  }

  applyPolicyUpdate(trajectories, advantages, klPenalty) {
    const lr = 0.05;
    for (let i = 0; i < trajectories.length; i++) {
      const tr = trajectories[i];
      const adv = advantages[i];
      if (adv <= 0) continue;
      let prev = this.bos;
      for (const ch of tr.tokens) {
        const b = this.transitions.get(prev);
        if (b && b.has(ch)) b.set(ch, b.get(ch) * (1 + lr * adv));
        prev = ch;
      }
    }
    for (const [, b] of this.transitions) {
      for (const [k, v] of b) b.set(k, Math.max(v * (1 - klPenalty * 0.01), 1e-9));
      const sum = [...b.values()].reduce((a, x) => a + x, 0) || 1;
      for (const [k, v] of b) b.set(k, v / sum);
    }
  }

  // Uses persistent this.cache so cacheHits/cacheMisses accurately reflect
  // repeated high-reward trajectories across training steps.
  updateCache(trajectories) {
    const threshold = 0.9;
    const maxSize = 512;
    for (const tr of trajectories) {
      if (tr.reward > threshold) {
        const key = tr.text.substring(0, 20);
        if (!this.cache.has(key)) {
          this.cache.set(key, { text: tr.text, reward: tr.reward });
          if (this.cache.size > maxSize) {
            const oldest = this.cache.keys().next().value;
            if (oldest !== undefined) this.cache.delete(oldest);
          }
          this.metrics.cacheMisses++;
        } else {
          this.metrics.cacheHits++;
        }
      }
    }
  }

  renderGraph() {
    const canvas = $("kg-canvas");
    if (!canvas) return;
    const ctx = canvas.getContext("2d");
    const rect = canvas.getBoundingClientRect();
    const dpr = window.devicePixelRatio || 1;
    const W = rect.width || 300;
    const H = rect.height || 300;
    canvas.width = Math.max(1, Math.floor(W * dpr));
    canvas.height = Math.max(1, Math.floor(H * dpr));
    ctx.setTransform(dpr, 0, 0, dpr, 0, 0);

    ctx.fillStyle = "#000";
    ctx.fillRect(0, 0, W, H);

    const nodes = [...this.graph.nodes.values()];
    if (nodes.length === 0) {
      ctx.fillStyle = "#555";
      ctx.font = "12px monospace";
      ctx.fillText("No graph yet. Fetch data to build.", 20, H / 2);
      return;
    }

    let minX = Infinity, maxX = -Infinity, minY = Infinity, maxY = -Infinity;
    for (const n of nodes) {
      if (typeof n.x !== "number") continue;
      minX = Math.min(minX, n.x); maxX = Math.max(maxX, n.x);
      minY = Math.min(minY, n.y); maxY = Math.max(maxY, n.y);
    }
    if (!isFinite(minX)) { minX = -1; maxX = 1; minY = -1; maxY = 1; }
    const pad = 25;
    const spanX = Math.max(1, maxX - minX);
    const spanY = Math.max(1, maxY - minY);
    const scale = Math.min((W - 2 * pad) / spanX, (H - 2 * pad) / spanY);
    const cx = (minX + maxX) / 2;
    const cy = (minY + maxY) / 2;
    const tx = (x) => W / 2 + (x - cx) * scale;
    const ty = (y) => H / 2 + (y - cy) * scale;

    const edges = [...this.graph.edges.values()];
    const maxEW = edges.reduce((m, e) => Math.max(m, e.weight), 1);
    for (const e of edges) {
      const s = this.graph.nodes.get(e.source);
      const t = this.graph.nodes.get(e.target);
      if (!s || !t || typeof s.x !== "number" || typeof t.x !== "number") continue;
      const alpha = 0.08 + 0.5 * (e.weight / maxEW);
      ctx.strokeStyle = "rgba(230, 230, 230, " + alpha + ")";
      ctx.lineWidth = 0.5 + 1.5 * (e.weight / maxEW);
      ctx.beginPath();
      ctx.moveTo(tx(s.x), ty(s.y));
      ctx.lineTo(tx(t.x), ty(t.y));
      ctx.stroke();
    }
    const maxNW = nodes.reduce((m, n) => Math.max(m, n.weight || 1), 1);
    for (const n of nodes) {
      if (typeof n.x !== "number") continue;
      const r = 2.5 + Math.min(8, Math.log(1 + (n.weight || 1)) * 2);
      let fill = "#e5e5e5";
      if (n.kind === "api") fill = "#a3a3a3";
      else if (n.kind === "model") fill = "#737373";
      else if (n.kind === "repo") fill = "#c0c0c0";
      ctx.fillStyle = fill;
      ctx.beginPath();
      ctx.arc(tx(n.x), ty(n.y), r, 0, Math.PI * 2);
      ctx.fill();
      if (n.label && n.label.length <= 14 && (n.weight || 1) > maxNW * 0.02) {
        ctx.fillStyle = "#666";
        ctx.font = "9px monospace";
        ctx.fillText(n.label, tx(n.x) + r + 2, ty(n.y) + 3);
      }
    }
    ctx.fillStyle = "#888";
    ctx.font = "11px monospace";
    ctx.fillText("nodes " + nodes.length + "  edges " + edges.length + "  seed " + this.kgSeed, 10, 20);
  }

  updateOscDisplay() {
    const el = $("osc-display");
    if (!el) return;
    const probs = this.superposition.probabilities();
    const freqs = ["1", "phi", "2", "pi"];
    el.innerHTML = probs.map((p, i) =>
      '<div class="metric"><div class="metric-label">|' + i + '> w=' + freqs[i] + '</div>' +
      '<div class="metric-value">' + p.toFixed(3) + '</div></div>'
    ).join("");
  }

  measureOsc() {
    this.superposition.evolve(0.5);
    const m = this.superposition.measure();
    this.lastCollapse = m;
    this.quantumMeasurements.push(m);
    this.updateOscDisplay();
    const msg = "osc " + m.oscillator + "  E=" + m.energy.toFixed(3) + "  x=" + m.x.toFixed(4);
    $("osc-result").textContent = msg;
    const os = $("osc-status");
    if (os) os.textContent = "superposition · " + this.quantumMeasurements.length + " measured";
    this.log("Quantum collapse: " + msg);
    return m;
  }

  async buildFiles() {
    const groupSize = parseInt($("cfg-group").value, 10) || 8;
    const klPenalty = parseFloat($("cfg-kl").value) || 0.1;
    const latentDim = parseInt($("cfg-latent").value, 10) || 32;
    const filterThreshold = parseFloat($("cfg-filter").value) || 0.7;
    const policyArch = $("cfg-arch").value || "tiny-bigram";

    const transObj = {};
    for (const [prev, b] of this.transitions) {
      const inner = {};
      for (const [k, v] of b) inner[k] = v;
      transObj[prev] = inner;
    }
    const vocabList = [...this.vocabCounts.keys()];
    const seed = this.kgSeed;

    const modelJson = {
      format: "micro-distill-bigram-v1",
      version: "3.1.2",
      arch: policyArch,
      created: new Date().toISOString(),
      bos_token: this.bos,
      eos_token: this.eos,
      vocab: vocabList,
      transitions: transObj,
      harmonic_seed: seed,
      harmonic_frequencies: [1.0, 1.618033988749895, 2.0, 3.141592653589793],
      config: {
        grpo: { group_size: groupSize, kl_penalty: klPenalty },
        vae: { latent_dim: latentDim, filter_threshold: filterThreshold },
        policy_arch: policyArch
      },
      metrics: {
        training_steps: this.metrics.trainingSteps,
        final_loss: this.metrics.finalLoss,
        average_reward: this.metrics.averageReward
      }
    };

    const configJson = {
      architectures: ["MicroDistilleryBigramForCausalLM"],
      model_type: "custom",
      custom_model_type: "micro-distillery-bigram",
      vocab_size: Math.max(vocabList.length, 256),
      max_position_embeddings: 1024,
      hidden_size: 512,
      num_hidden_layers: 0,
      num_attention_heads: 0,
      intermediate_size: 0,
      activation_function: "gelu_new",
      layer_norm_epsilon: 1e-5,
      use_cache: true,
      bos_token_id: 0,
      eos_token_id: 1,
      transformers_version: "4.44.0",
      library_name: "micro-distillery",
      micro_distillery: {
        format: "bigram-distilled",
        dialect: "character-level",
        reference_arch: policyArch,
        distill_from: ["smollm2-135m", "qwen2.5-0.5b", "olmo2-1b", "tinyllama-1.1b"],
        harmonic_seed: seed,
        harmonic_frequencies: modelJson.harmonic_frequencies,
        grpo_config: modelJson.config.grpo,
        vae_config: modelJson.config.vae
      }
    };

    const vocabObj = {};
    vocabList.forEach((t, i) => { vocabObj[t] = i; });

    const tokenizerConfig = {
      tokenizer_class: "PreTrainedTokenizerFast",
      bos_token: "<|endoftext|>",
      eos_token: "<|endoftext|>",
      unk_token: "<|endoftext|>",
      pad_token: "<|endoftext|>",
      add_prefix_space: false,
      model_max_length: 1024,
      name_or_path: "micro-distillery"
    };

    const kgJson = this.graph.toJSON();

    const logsJson = {
      created: new Date().toISOString(),
      metrics: this.metrics,
      quantum_measurements: this.quantumMeasurements.map((m) => ({
        oscillator: m.oscillator, energy: m.energy, x: m.x, t: m.t, probabilities: m.probabilities
      })),
      config: modelJson.config
    };

    const testPy = this.buildTestPy();
    const testSh = this.buildTestSh();
    const testReadme = this.buildTestReadme();
    const runSh = this.buildRunSh();
    const toParquetPy = this.buildToParquetPy();
    const reqTxt = "# Core test.py requires only Python 3.8+ standard library.\n# Optional:\npandas>=2.0\npyarrow>=14.0\n";
    const readmeMd = this.buildReadme(modelJson, vocabList.length);
    const licenseTxt = "Apache License\nVersion 2.0, January 2004\nhttp://www.apache.org/licenses/\n\n"
      + "Licensed under the Apache License, Version 2.0 (the \"License\");\n"
      + "you may not use this file except in compliance with the License.\n"
      + "You may obtain a copy of the License at\n\n"
      + "    http://www.apache.org/licenses/LICENSE-2.0\n\n"
      + "Unless required by applicable law or agreed to in writing, software\n"
      + "distributed under the License is distributed on an \"AS IS\" BASIS,\n"
      + "WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\n"
      + "See the License for the specific language governing permissions and\n"
      + "limitations under the License.\n";
    const gitattrs = "*.json text eol=lf\n*.py text eol=lf\n*.sh text eol=lf\n*.md text eol=lf\n*.svg text eol=lf\n";
    const pipelineSvg = this.buildPipelineSvg();
    const architectureSvg = this.buildArchitectureSvg();

    this.modelFiles = [
      { name: "README.md",                type: "docs",   content: readmeMd,        isBinary: false, group: "core" },
      { name: "LICENSE",                  type: "docs",   content: licenseTxt,      isBinary: false, group: "core" },
      { name: ".gitattributes",           type: "config", content: gitattrs,        isBinary: false, group: "core" },
      { name: "requirements.txt",         type: "text",   content: reqTxt,          isBinary: false, group: "core" },
      { name: "config.json",              type: "config", content: JSON.stringify(configJson, null, 2), isBinary: false, group: "config" },
      { name: "model.json",               type: "model",  content: JSON.stringify(modelJson, null, 2), isBinary: false, group: "model" },
      { name: "vocab.json",               type: "vocab",  content: JSON.stringify(vocabObj, null, 2), isBinary: false, group: "vocab" },
      { name: "tokenizer_config.json",    type: "vocab",  content: JSON.stringify(tokenizerConfig, null, 2), isBinary: false, group: "vocab" },
      { name: "training_logs.json",       type: "logs",   content: JSON.stringify(logsJson, null, 2), isBinary: false, group: "logs" },
      { name: "knowledge_graph.json",     type: "kg",     content: JSON.stringify(kgJson, null, 2), isBinary: false, group: "kg" },
      { name: "test.py",                  type: "python", content: testPy,          isBinary: false, group: "tests" },
      { name: "test.sh",                  type: "shell",  content: testSh,          isBinary: false, group: "tests" },
      { name: "test_readme.md",           type: "docs",   content: testReadme,      isBinary: false, group: "tests" },
      { name: "run.sh",                   type: "shell",  content: runSh,           isBinary: false, group: "tests" },
      { name: "to_parquet.py",            type: "python", content: toParquetPy,     isBinary: false, group: "parquet" },
      { name: "diagrams/pipeline.svg",    type: "svg",    content: pipelineSvg,     isBinary: false, group: "diagrams" },
      { name: "diagrams/architecture.svg", type: "svg",   content: architectureSvg, isBinary: false, group: "diagrams" }
    ];

    return this.modelFiles;
  }

  buildTestPy() {
    return [
'#!/usr/bin/env python3',
'"""',
'test.py - Micro Distillery model chat wrapper.',
'',
'Loads model.json (a GRPO+VAE-distilled bigram model) and provides',
'a simple REPL chat interface.',
'',
'Usage:',
'    python3 test.py                      # interactive chat',
'    python3 test.py hello                # one-shot from prompt',
'    python3 test.py --temperature 1.2    # one-shot with custom temperature',
'    python3 test.py                      # (interactive also supports --temperature)',
'"""',
'import json',
'import os',
'import random',
'import sys',
'',
'MODEL_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "model.json")',
'',
'',
'def load_model(path=MODEL_PATH):',
'    with open(path, "r", encoding="utf-8") as f:',
'        return json.load(f)',
'',
'',
'def sample_next(transitions, token, rng, temperature=1.0):',
'    bucket = transitions.get(token)',
'    if not bucket:',
'        return None',
'    tokens = list(bucket.keys())',
'    inv_t = 1.0 / max(temperature, 1e-3)',
'    weights = [max(bucket[t], 1e-12) ** inv_t for t in tokens]',
'    total = sum(weights)',
'    if total <= 0:',
'        return None',
'    r = rng.random() * total',
'    acc = 0.0',
'    for t, w in zip(tokens, weights):',
'        acc += w',
'        if r <= acc:',
'            return t',
'    return tokens[-1]',
'',
'',
'def generate(model, prompt, max_tokens=32, temperature=0.9):',
'    transitions = model.get("transitions", {})',
'    bos = model.get("bos_token", "<|endoftext|>")',
'    eos = model.get("eos_token", "<|endoftext|>")',
'    seed = int(model.get("harmonic_seed", 1337)) & 0xFFFFFFFF',
'    rng = random.Random(seed)',
'',
'    if prompt and prompt.strip():',
'        current = prompt.strip()[-1]',
'        prefix = prompt.strip()',
'    else:',
'        current = bos',
'        prefix = ""',
'',
'    generated = ""',
'    for _ in range(max_tokens):',
'        nxt = sample_next(transitions, current, rng, temperature=temperature)',
'        if nxt is None or nxt == eos:',
'            break',
'        generated += nxt',
'        current = nxt',
'    return prefix + generated',
'',
'',
'def main(temperature=0.9):',
'    print("=== Micro Distillery Model Chat ===")',
'    if not os.path.exists(MODEL_PATH):',
'        print("Error: " + MODEL_PATH + " not found.", file=sys.stderr)',
'        sys.exit(1)',
'    model = load_model()',
'    print("Format:      " + str(model.get("format")))',
'    print("Vocab size:  " + str(len(model.get("vocab", []))))',
'    print("Transitions: " + str(len(model.get("transitions", {}))))',
'    print("Seed:        " + str(model.get("harmonic_seed")))',
'    print("Temperature: " + str(temperature))',
'    print("Type /quit to exit.\\n")',
'    while True:',
'        try:',
'            prompt = input("You: ").strip()',
'        except (EOFError, KeyboardInterrupt):',
'            print("\\nBye!")',
'            break',
'        if not prompt:',
'            continue',
'        if prompt.lower() in ("/quit", "/exit", "quit", "exit"):',
'            break',
'        out = generate(model, prompt, temperature=temperature)',
'        print("Bot: " + out)',
'',
'',
'if __name__ == "__main__":',
'    args = sys.argv[1:]',
'    temperature = 0.9',
'    prompts = []',
'    i = 0',
'    while i < len(args):',
'        if args[i] == "--temperature":',
'            if i + 1 < len(args):',
'                try:',
'                    temperature = float(args[i + 1])',
'                except ValueError:',
'                    print("Warning: invalid --temperature value, using 0.9", file=sys.stderr)',
'                i += 2',
'            else:',
'                print("Warning: --temperature requires a value, using 0.9", file=sys.stderr)',
'                i += 1',
'        elif args[i] in ("--help", "-h"):',
'            print("Usage: python3 test.py [prompt...] [--temperature T]")',
'            sys.exit(0)',
'        elif args[i].startswith("--"):',
'            i += 1',
'        else:',
'            prompts.append(args[i])',
'            i += 1',
'    if prompts:',
'        model = load_model()',
'        print(generate(model, " ".join(prompts), temperature=temperature))',
'    else:',
'        main(temperature=temperature)',
''
    ].join("\n");
  }

  buildTestSh() {
    return [
'#!/usr/bin/env bash',
'# test.sh - Test harness for the exported Micro Distillery model.',
'set -euo pipefail',
'cd "$(dirname "$0")"',
'',
'PY="${PYTHON:-python3}"',
'',
'echo "================================================"',
'echo " Micro Distillery - Model Test Harness"',
'echo "================================================"',
'echo ""',
'',
'if ! command -v "$PY" >/dev/null 2>&1; then',
'  echo "ERROR: $PY not found. Install Python 3.8+." >&2',
'  echo "  macOS:   brew install python3" >&2',
'  echo "  Ubuntu:  sudo apt install python3" >&2',
'  echo "  Windows: https://python.org/downloads/" >&2',
'  exit 1',
'fi',
'',
'echo "[1/3] Python: $($PY --version 2>&1)"',
'',
'if [ ! -f model.json ]; then',
'  echo "ERROR: model.json not found in $(pwd)" >&2',
'  exit 1',
'fi',
'echo "[2/3] Found model.json"',
'',
'$PY -c "',
'import json',
'm = json.load(open(\'model.json\'))',
'assert \'transitions\' in m, \'missing transitions\'',
'assert \'vocab\' in m, \'missing vocab\'',
'print(\'OK: format=%s vocab=%d transitions=%d\' % (m.get(\'format\',\'?\'), len(m[\'vocab\']), len(m[\'transitions\'])))',
'"',
'echo "[3/3] Model validation passed"',
'echo ""',
'echo "=== Sample Generations ==="',
'for prompt in alice bob zoe liam; do',
'  echo -n "  $prompt -> "',
'  $PY test.py "$prompt" 2>/dev/null | tail -1 || echo "(failed)"',
'done',
'echo ""',
'echo "=== Interactive Chat (type /quit to exit) ==="',
'exec $PY test.py',
''
    ].join("\n");
  }

  buildTestReadme() {
    return [
'# Testing Your Exported Model',
'',
'This bundle contains a fully runnable bigram language model. No GPU,',
'no external dependencies - Python 3.8+ standard library only.',
'',
'## Files',
'',
'| File | Purpose |',
'|------|---------|',
'| `model.json` | The trained model (transitions + config + metrics) |',
'| `test.py` | Interactive chat wrapper |',
'| `test.sh` | Test harness (validates + generates samples) |',
'| `run.sh` | Minimal launcher for test.py |',
'',
'## Quick Start',
'',
'### macOS / Linux',
'',
'```bash',
'chmod +x test.sh run.sh',
'./test.sh',
'```',
'',
'### Windows (PowerShell)',
'',
'```powershell',
'python test.py',
'```',
'',
'### One-shot generation',
'',
'```bash',
'python3 test.py alice',
'# => alice… (seed-dependent continuation)',
'```',
'',
'### Custom temperature',
'',
'```bash',
'python3 test.py --temperature 0.5',
'python3 test.py alice --temperature 1.2',
'```',
'',
'## Manual Test',
'',
'```bash',
'python3 -c "import json; m=json.load(open(\'model.json\')); print(len(m[\'vocab\']), \'tokens,\', len(m[\'transitions\']), \'states\')"',
'```',
'',
'Expected output: `<N> tokens, <M> states`.',
'',
'## Troubleshooting',
'',
'- **`python3 not found`** - install Python 3.8+ from https://python.org',
'- **`model.json not found`** - make sure you are inside the extracted directory',
'- **`Permission denied`** - run `chmod +x test.sh run.sh` first',
''
    ].join("\n");
  }

  buildRunSh() {
    return [
'#!/usr/bin/env bash',
'# run.sh - Minimal launcher for test.py.',
'set -euo pipefail',
'cd "$(dirname "$0")"',
'PY="${PYTHON:-python3}"',
'if ! command -v "$PY" >/dev/null 2>&1; then',
'  echo "Error: python3 not found." >&2',
'  exit 1',
'fi',
'exec "$PY" test.py "$@"',
''
    ].join("\n");
  }

  buildToParquetPy() {
    return [
'#!/usr/bin/env python3',
'"""Convert model.json to model.parquet.',
'',
'Requires: pandas, pyarrow',
'    pip install pandas pyarrow',
'"""',
'import json',
'import os',
'import sys',
'',
'MODEL_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "model.json")',
'OUT_PATH = os.path.join(os.path.dirname(os.path.abspath(__file__)), "model.parquet")',
'',
'',
'def main():',
'    try:',
'        import pandas as pd',
'    except ImportError:',
'        print("Error: pandas required. Install: pip install pandas pyarrow", file=sys.stderr)',
'        sys.exit(1)',
'',
'    with open(MODEL_PATH, "r", encoding="utf-8") as f:',
'        model = json.load(f)',
'',
'    rows = []',
'    for state, bucket in model.get("transitions", {}).items():',
'        for nxt, prob in bucket.items():',
'            rows.append({',
'                "state": state,',
'                "next_token": nxt,',
'                "probability": float(prob),',
'                "harmonic_seed": int(model.get("harmonic_seed", 0)),',
'                "format": model.get("format", "unknown"),',
'                "version": model.get("version", "0.0"),',
'            })',
'',
'    if not rows:',
'        print("Warning: no transitions. Writing metadata-only parquet.")',
'        rows = [{"state": "", "next_token": "", "probability": 0.0,',
'                 "harmonic_seed": int(model.get("harmonic_seed", 0)),',
'                 "format": model.get("format", "unknown"),',
'                 "version": model.get("version", "0.0")}]',
'',
'    df = pd.DataFrame(rows)',
'    df.to_parquet(OUT_PATH, index=False)',
'    print("Wrote " + str(len(df)) + " rows to " + OUT_PATH)',
'',
'',
'if __name__ == "__main__":',
'    main()',
''
    ].join("\n");
  }

  buildReadme(modelJson, vocabSize) {
    const transCount = Object.keys(modelJson.transitions).length;
    return [
'---',
'language:',
'  - en',
'license: apache-2.0',
'tags:',
'  - micro-distillery',
'  - bigram',
'  - grpo',
'  - vae',
'  - distilled',
'  - smollm2',
'  - qwen2.5',
'  - olmo',
'  - tinyllama',
'library_name: micro-distillery',
'---',
'',
'# Micro Distillery — Bigram Distilled Model',
'',
'This model was trained from scratch using a combination of:',
'',
'- **GRPO** (Group Relative Policy Optimization) for character-level trajectory training',
'- **VAE filtering** to keep only high-quality trajectories',
'- **4x quantum harmonic oscillator** superposition for deterministic seeding',
'',
'It is a compact character-level bigram model - no transformer weights,',
'just transition probabilities. Runs on any machine with Python 3.8+.',
'',
'## Reference Architectures',
'',
'The bundle metadata references these modern (2025-2026) open models as',
'inspiration sources. The actual model is a distilled bigram - not a',
'transformer checkpoint - and does not load via `AutoModelForCausalLM`.',
'',
'- [SmolLM2-135M](https://huggingface.co/HuggingFaceTB/SmolLM2-135M) (HuggingFaceTB)',
'- [Qwen2.5-0.5B](https://huggingface.co/Qwen/Qwen2.5-0.5B) (Qwen)',
'- [OLMo-2-1B](https://huggingface.co/allenai/OLMo-2-0425-1B) (AllenAI)',
'- [TinyLlama-1.1B](https://huggingface.co/TinyLlama/TinyLlama-1.1B-Chat-v1.0) (TinyLlama)',
'',
'## Model Details',
'',
'- **Format**: ' + modelJson.format,
'- **Version**: ' + modelJson.version,
'- **Reference arch**: ' + modelJson.arch,
'- **Vocab size**: ' + vocabSize + ' tokens',
'- **Transition states**: ' + transCount,
'- **Harmonic seed**: ' + modelJson.harmonic_seed,
'- **Created**: ' + modelJson.created,
'',
'## Quick Start',
'',
'```bash',
'# 1. Extract the bundle',
'unzip micro-distillery-*.zip',
'cd micro-distillery-*',
'',
'# 2. Run the test harness',
'chmod +x test.sh run.sh',
'./test.sh',
'',
'# 3. Or chat directly',
'python3 test.py',
'```',
'',
'## Files in the Bundle',
'',
'| File | Description |',
'|------|-------------|',
'| `model.json` | The trained bigram model (all weights) |',
'| `config.json` | HuggingFace-style metadata config |',
'| `vocab.json` | Character vocabulary |',
'| `tokenizer_config.json` | Tokenizer metadata |',
'| `knowledge_graph.json` | 2D graph used to seed the model |',
'| `training_logs.json` | Metrics + quantum measurements |',
'| `test.py` | Interactive chat wrapper |',
'| `test.sh` | Test harness |',
'| `test_readme.md` | Testing instructions |',
'| `run.sh` | Minimal launcher |',
'| `to_parquet.py` | Convert to parquet |',
'| `diagrams/pipeline.svg` | Training pipeline diagram |',
'| `diagrams/architecture.svg` | Model architecture diagram |',
'',
'## Training Pipeline',
'',
'1. **Fetch** - pulls corpora and model cards from HuggingFace + GitHub (2026 open sources)',
'2. **Knowledge graph** - builds a character-transition graph, laid out in 2D',
'3. **Seed** - initializes bigram transition counts from the corpus',
'4. **GRPO** - samples trajectories, scores with a name heuristic, boosts high-advantage transitions',
'5. **VAE filter** - retains only high-probability trajectories',
'6. **Quantum seed** - 4 harmonic oscillators (w = 1, phi, 2, pi) collapse to a deterministic seed',
'7. **Export** - produces a HuggingFace-ready .zip bundle',
'',
'## Uploading to Hugging Face',
'',
'The bundle is structured so you can upload it directly to a HF repo:',
'',
'```bash',
'pip install huggingface_hub',
'huggingface-cli login',
'huggingface-cli upload your-username/micro-distillery-bigram . --repo-type model',
'```',
'',
'The `model.json` file is not a transformer checkpoint - use `test.py` to run it.',
'`config.json` is provided for HF metadata and Datasets viewer compatibility.',
'',
'## Data Sources',
'',
'This bundle was produced by fetching from these free, open endpoints:',
'',
'- `raw.githubusercontent.com/karpathy/makemore/master/names.txt` (training corpus)',
'- `huggingface.co/api/models?search=smollm2` (model reference list)',
'- `huggingface.co/api/models?search=qwen2.5-0.5b` (model reference list)',
'- `huggingface.co/HuggingFaceTB/SmolLM2-135M/raw/main/README.md` (model card)',
'- `huggingface.co/allenai/OLMo-2-0425-1B/raw/main/README.md` (model card)',
'- `api.github.com/repos/karpathy/makemore` (repo metadata)',
'',
'## License',
'',
'Apache 2.0',
''
    ].join("\n");
  }

  buildPipelineSvg() {
    return '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 900 220" font-family="monospace">\n'
      + '<rect width="900" height="220" fill="#000"/>\n'
      + '<text x="20" y="30" fill="#e5e5e5" font-size="14" font-weight="bold">Micro Distillery - Training Pipeline</text>\n'
      + '<g stroke="#e5e5e5" stroke-width="1.5" fill="#0a0a0a">\n'
      + '  <rect x="20" y="70" width="140" height="70" rx="4"/>\n'
      + '  <rect x="200" y="70" width="140" height="70" rx="4"/>\n'
      + '  <rect x="380" y="70" width="140" height="70" rx="4"/>\n'
      + '  <rect x="560" y="70" width="140" height="70" rx="4"/>\n'
      + '  <rect x="740" y="70" width="140" height="70" rx="4"/>\n'
      + '</g>\n'
      + '<g fill="#e5e5e5" font-size="12" text-anchor="middle">\n'
      + '  <text x="90" y="100">1. Fetch</text>\n'
      + '  <text x="270" y="100">2. Bigram</text>\n'
      + '  <text x="450" y="100">3. GRPO</text>\n'
      + '  <text x="630" y="100">4. VAE</text>\n'
      + '  <text x="810" y="100">5. Export</text>\n'
      + '</g>\n'
      + '<g fill="#888" font-size="10" text-anchor="middle">\n'
      + '  <text x="90" y="122">HF + GitHub</text>\n'
      + '  <text x="270" y="122">counts</text>\n'
      + '  <text x="450" y="122">policy</text>\n'
      + '  <text x="630" y="122">filter</text>\n'
      + '  <text x="810" y="122">.zip</text>\n'
      + '</g>\n'
      + '<g stroke="#888" stroke-width="1.5" fill="none">\n'
      + '  <path d="M160 105 L200 105" marker-end="url(#a)"/>\n'
      + '  <path d="M340 105 L380 105" marker-end="url(#a)"/>\n'
      + '  <path d="M520 105 L560 105" marker-end="url(#a)"/>\n'
      + '  <path d="M700 105 L740 105" marker-end="url(#a)"/>\n'
      + '</g>\n'
      + '<defs><marker id="a" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="6" markerHeight="6" orient="auto"><path d="M0,0 L10,5 L0,10 z" fill="#888"/></marker></defs>\n'
      + '<text x="450" y="185" fill="#666" font-size="11" text-anchor="middle">4x harmonic oscillator (w=1, phi, 2, pi) provides deterministic seed</text>\n'
      + '</svg>';
  }

  buildArchitectureSvg() {
    return '<svg xmlns="http://www.w3.org/2000/svg" viewBox="0 0 900 300" font-family="monospace">\n'
      + '<rect width="900" height="300" fill="#000"/>\n'
      + '<text x="20" y="30" fill="#e5e5e5" font-size="14" font-weight="bold">Micro Distillery - Model Architecture</text>\n'
      + '<text x="20" y="55" fill="#888" font-size="11">Bigram transition table + quantum-seeded decoding</text>\n'
      + '<g fill="none" stroke="#e5e5e5" stroke-width="1.5">\n'
      + '  <rect x="20" y="90" width="200" height="60" rx="4"/>\n'
      + '  <rect x="20" y="180" width="200" height="60" rx="4"/>\n'
      + '  <rect x="280" y="130" width="220" height="80" rx="4"/>\n'
      + '  <rect x="560" y="90" width="180" height="60" rx="4"/>\n'
      + '  <rect x="560" y="180" width="180" height="60" rx="4"/>\n'
      + '  <rect x="790" y="130" width="90" height="80" rx="4"/>\n'
      + '</g>\n'
      + '<g fill="#e5e5e5" font-size="12" text-anchor="middle">\n'
      + '  <text x="120" y="120">Input prompt</text>\n'
      + '  <text x="120" y="210">Last character</text>\n'
      + '  <text x="390" y="160">Transitions map</text>\n'
      + '  <text x="390" y="180" fill="#888" font-size="10">state to {next: prob}</text>\n'
      + '  <text x="650" y="120">Sample</text>\n'
      + '  <text x="650" y="210">RNG (seeded)</text>\n'
      + '  <text x="835" y="160">Output</text>\n'
      + '</g>\n'
      + '<g stroke="#888" stroke-width="1.5" fill="none">\n'
      + '  <path d="M220 120 L280 145" marker-end="url(#b)"/>\n'
      + '  <path d="M220 210 L280 195" marker-end="url(#b)"/>\n'
      + '  <path d="M500 150 L560 130" marker-end="url(#b)"/>\n'
      + '  <path d="M500 190 L560 210" marker-end="url(#b)"/>\n'
      + '  <path d="M740 120 L790 145" marker-end="url(#b)"/>\n'
      + '  <path d="M740 210 L790 195" marker-end="url(#b)"/>\n'
      + '</g>\n'
      + '<defs><marker id="b" viewBox="0 0 10 10" refX="8" refY="5" markerWidth="6" markerHeight="6" orient="auto"><path d="M0,0 L10,5 L0,10 z" fill="#888"/></marker></defs>\n'
      + '<text x="450" y="280" fill="#666" font-size="10" text-anchor="middle">Deterministic given seed - output reproducible across machines</text>\n'
      + '</svg>';
  }

  async showExportModal() {
    if (!this.trainDone) {
      this.log("Warning: export without training. Bundle will use current transitions.", "warn");
    }
    await this.buildFiles();
    const filtered = this.applyExportOptions();
    $("export-modal").classList.remove("hidden");
    requestAnimationFrame(() => $("export-modal").classList.add("show"));
    this.renderFileList(filtered);
    this.renderExportSize(filtered);
  }

  hideExportModal() {
    $("export-modal").classList.remove("show");
    setTimeout(() => $("export-modal").classList.add("hidden"), 200);
  }

  applyExportOptions() {
    const opts = {
      config: $("opt-config").checked,
      vocab: $("opt-vocab").checked,
      logs: $("opt-logs").checked,
      kg: $("opt-kg").checked,
      diagrams: $("opt-diagrams").checked,
      parquet: $("opt-parquet").checked
    };
    return this.modelFiles.filter((f) => {
      if (f.group === "core") return true;
      if (f.group === "tests") return true;
      if (f.group === "config" && !opts.config) return false;
      if (f.group === "vocab" && !opts.vocab) return false;
      if (f.group === "logs" && !opts.logs) return false;
      if (f.group === "kg" && !opts.kg) return false;
      if (f.group === "diagrams" && !opts.diagrams) return false;
      if (f.group === "parquet" && !opts.parquet) return false;
      return true;
    });
  }

  renderFileList(files) {
    const list = $("file-list");
    list.innerHTML = "";
    files.forEach((f, i) => {
      const row = document.createElement("div");
      row.className = "file-row" + (i === this.selectedPreviewIndex ? " selected" : "");
      const size = f.content ? f.content.length : 0;
      row.innerHTML = '<span class="file-row-name">' + escapeHtml(f.name) + '</span>' +
                      '<span class="file-row-size">' + humanSize(size) + '</span>';
      row.addEventListener("click", () => {
        this.selectedPreviewIndex = i;
        this.renderFileList(files);
        this.renderPreview(files, i);
      });
      list.appendChild(row);
    });
    if (files.length > 0) {
      if (this.selectedPreviewIndex >= files.length) this.selectedPreviewIndex = 0;
      this.renderPreview(files, this.selectedPreviewIndex);
    } else {
      $("file-preview").textContent = "No files selected.";
    }
  }

  renderPreview(files, idx) {
    const f = files[idx];
    if (!f) return;
    let content = f.content || "";
    if (content.length > 8000) content = content.substring(0, 8000) + "\n\n... (truncated preview)";
    $("file-preview").textContent = content;
  }

  renderExportSize(files) {
    const total = files.reduce((s, f) => s + (f.content ? f.content.length : 0), 0);
    $("export-size").textContent = files.length + " files  " + humanSize(total);
  }

  async downloadBundle() {
    const files = this.applyExportOptions();
    if (files.length === 0) { this.log("No files selected.", "err"); return; }
    if (typeof JSZip === "undefined") {
      this.log("JSZip library not available (CDN blocked?). Cannot build .zip.", "err");
      return;
    }
    if (typeof saveAs === "undefined") {
      this.log("FileSaver library not available (CDN blocked?). Cannot download.", "err");
      return;
    }
    this.log("Building .zip bundle...");
    $("btn-download").disabled = true;
    try {
      const zip = new JSZip();
      for (const f of files) zip.file(f.name, f.content);
      const blob = await zip.generateAsync({ type: "blob", compression: "DEFLATE", compressionOptions: { level: 6 } });
      const ts = new Date().toISOString().replace(/[:.]/g, "-").slice(0, 19);
      const name = "micro-distillery-" + ts + ".zip";
      saveAs(blob, name);
      this.log("Bundle downloaded: " + name + " (" + humanSize(blob.size) + ")", "ok");
      try { await this.storage.saveModel("latest", { timestamp: Date.now(), files: files.length }); } catch (e) { /* ignored */ }
      setTimeout(() => this.hideExportModal(), 800);
    } catch (e) {
      this.log("Export failed: " + e.message, "err");
    } finally {
      $("btn-download").disabled = false;
    }
  }

  reset() {
    this.fetchDone = false;
    this.trainDone = false;
    this.fetching = false;
    this.training = false;
    this.transitions = new Map();
    this.vocabCounts = new Map();
    this.graph = new KnowledgeGraph();
    this.kgSeed = 42;
    this.quantumMeasurements = [];
    this.corpus = FALLBACK_NAMES.slice();
    this.cache = new Map();
    this.metrics = { trainingSteps: 0, finalLoss: 0, averageReward: 0, cacheHits: 0, cacheMisses: 0, filteredCount: 0, totalSamples: 0, vaeLoss: 0 };
    this.superposition = new Superposition4(1337);
    this.apiFanOut = new APIFanOut((m, c) => this.apiLog(m, c));
    this.lastCollapse = null;
    this.selectedPreviewIndex = 0;
    this.modelFiles = [];
    this.markStep("fetch", "active", "");
    this.markStep("train", "", "");
    this.markStep("export", "", "");
    this.updateMetrics();
    this.renderGraph();
    this.setProgress(0, "idle");
    this.setProgressLabel("idle");
    this.updateOscDisplay();
    $("osc-result").textContent = "";
    const os = $("osc-status");
    if (os) os.textContent = "superposition";
    this.log("System reset.");
  }
}

class Wizard {
  constructor(engine) {
    this.engine = engine;
    this.step = 0;
    this.steps = ["welcome", "fetch", "train", "export", "done"];
  }
  open() {
    this.step = 0;
    this.render();
    $("wizard").classList.remove("hidden");
    requestAnimationFrame(() => $("wizard").classList.add("show"));
  }
  close() {
    $("wizard").classList.remove("show");
    setTimeout(() => $("wizard").classList.add("hidden"), 200);
  }
  progressDots() {
    return this.steps.map((_, i) => {
      let cls = "wiz-dot";
      if (i < this.step) cls += " done";
      else if (i === this.step) cls += " active";
      return '<div class="' + cls + '"></div>';
    }).join("");
  }
  render() {
    const id = this.steps[this.step];
    $("wiz-progress").innerHTML = this.progressDots();

    const content = {
      welcome: {
        title: "Micro Distillery",
        sub: "Build a small language model in 3 automated steps.",
        body: [
          "<p>This tool builds a runnable character-level language model from free open data (HuggingFace + GitHub, 2026 sources). No installation needed.</p>",
          "<p>You will:</p>",
          "<ul>",
          "<li><strong>Fetch</strong> a training corpus from open endpoints (SmolLM2, Qwen2.5, OLMo-2 cards + Karpathy's makemore corpus)</li>",
          "<li><strong>Train</strong> the model with GRPO + VAE filtering</li>",
          "<li><strong>Export</strong> a HuggingFace-ready .zip bundle</li>",
          "</ul>",
          '<div class="wiz-info">',
          "<strong>Time:</strong> ~30 seconds total. <strong>Cost:</strong> free. <strong>Data:</strong> only from public sources.",
          "</div>"
        ],
        next: "Begin",
        back: null
      },
      fetch: {
        title: "Step 1 - Fetch Data",
        sub: "Pull a training corpus from free, open endpoints.",
        body: [
          "<p>We will fetch:</p>",
          "<ul>",
          "<li>Character corpus from <code>raw.githubusercontent.com/karpathy/makemore</code></li>",
          "<li>Model metadata from <code>huggingface.co/api/models?search=smollm2</code> (SmolLM2)</li>",
          "<li>Model metadata from <code>huggingface.co/api/models?search=qwen2.5-0.5b</code> (Qwen2.5)</li>",
          "<li>Reference cards from SmolLM2-135M and OLMo-2-1B</li>",
          "</ul>",
          "<p>If the network is unavailable, a bundled fallback corpus (200+ names) is used instead.</p>",
          '<div class="wiz-info">',
          '<strong>Status:</strong> <span id="wiz-fetch-status">not started</span>',
          "</div>",
          '<button class="btn btn-primary btn-lg btn-block" id="wiz-run-fetch">Fetch Data</button>'
        ],
        next: "Continue",
        back: null
      },
      train: {
        title: "Step 2 - Train Model",
        sub: "Run 60 GRPO steps with VAE filtering.",
        body: [
          "<p>Training optimizes a character bigram model using:</p>",
          "<ul>",
          "<li><strong>GRPO</strong> - group-relative policy optimization on sampled trajectories</li>",
          "<li><strong>VAE filter</strong> - keeps only high-probability trajectories</li>",
          "</ul>",
          '<div class="wiz-info">',
          '<strong>Status:</strong> <span id="wiz-train-status">waiting for data</span>',
          "</div>",
          '<button class="btn btn-primary btn-lg btn-block" id="wiz-run-train">Train Model</button>'
        ],
        next: "Continue",
        back: null
      },
      export: {
        title: "Step 3 - Export Bundle",
        sub: "Download a HuggingFace-ready .zip.",
        body: [
          "<p>The bundle includes:</p>",
          "<ul>",
          "<li><code>model.json</code>, <code>config.json</code>, <code>vocab.json</code></li>",
          "<li><code>README.md</code> model card (HF-formatted with YAML frontmatter)</li>",
          "<li><code>test.py</code>, <code>test.sh</code>, <code>test_readme.md</code></li>",
          "<li><code>diagrams/pipeline.svg</code>, <code>diagrams/architecture.svg</code></li>",
          "<li><code>to_parquet.py</code> for parquet conversion</li>",
          "<li><code>LICENSE</code>, <code>.gitattributes</code>, <code>requirements.txt</code></li>",
          "</ul>",
          '<div class="wiz-info">',
          '<strong>Status:</strong> <span id="wiz-export-status">ready</span>',
          "</div>",
          '<button class="btn btn-primary btn-lg btn-block" id="wiz-run-export">Open Export Panel</button>'
        ],
        next: "Done",
        back: null
      },
      done: {
        title: "Complete",
        sub: "Your bundle has been downloaded.",
        body: [
          "<p>Next steps:</p>",
          "<ul>",
          "<li>Unzip the bundle</li>",
          "<li>Run <code>./test.sh</code> to validate and chat</li>",
          "<li>Upload the folder to HuggingFace via <code>huggingface-cli upload</code></li>",
          "<li>Or run <code>python3 to_parquet.py</code> to convert to parquet</li>",
          "</ul>",
          '<div class="wiz-info">',
          "<strong>Tip:</strong> Click <em>Open Workspace</em> for advanced controls (graph, oscillator, custom settings).",
          "</div>"
        ],
        next: "Open Workspace",
        back: null
      }
    }[id];

    $("wiz-title").textContent = content.title;
    $("wiz-sub").textContent = content.sub;
    $("wiz-content").innerHTML = content.body.join("");
    $("wiz-back").style.visibility = this.step === 0 ? "hidden" : "visible";
    $("wiz-next").textContent = content.next;

    if (id === "welcome" || id === "done") {
      $("wiz-skip").textContent = id === "done" ? "Close" : "Skip wizard";
    } else {
      $("wiz-skip").textContent = "Skip wizard";
    }

    if (id === "fetch") {
      const btn = $("wiz-run-fetch");
      if (this.engine.fetching && btn) btn.disabled = true;
      if (btn) btn.addEventListener("click", async () => {
        if (this.engine.fetching) {
          $("wiz-fetch-status").textContent = "fetch already in progress";
          return;
        }
        btn.disabled = true;
        $("wiz-fetch-status").textContent = "fetching...";
        const ok = await this.engine.fetch();
        $("wiz-fetch-status").textContent = ok ? "complete - " + this.engine.corpus.length + " lines" : "failed";
        btn.disabled = false;
        if (ok) setTimeout(() => this.next(), 500);
      });
    }
    if (id === "train") {
      const btn = $("wiz-run-train");
      if (!this.engine.fetchDone) {
        $("wiz-train-status").textContent = "data required - go back";
        if (btn) btn.disabled = true;
      }
      if (btn) btn.addEventListener("click", async () => {
        btn.disabled = true;
        $("wiz-train-status").textContent = "training...";
        const ok = await this.engine.train();
        $("wiz-train-status").textContent = ok ? "complete - loss " + this.engine.metrics.finalLoss.toFixed(4) : "failed";
        btn.disabled = false;
        if (ok) setTimeout(() => this.next(), 500);
      });
    }
    if (id === "export") {
      const btn = $("wiz-run-export");
      if (!this.engine.trainDone) {
        $("wiz-export-status").textContent = "training required - go back";
        if (btn) btn.disabled = true;
      }
      if (btn) btn.addEventListener("click", async () => {
        btn.disabled = true;
        $("wiz-export-status").textContent = "opening export panel...";
        await this.engine.showExportModal();
        $("wiz-export-status").textContent = "ready - configure options in the export panel";
        btn.disabled = false;
      });
    }
  }
  next() {
    if (this.step === this.steps.length - 1) { this.close(); return; }
    if (this.steps[this.step] === "fetch" && !this.engine.fetchDone) {
      this.engine.log("Fetch data before continuing.", "warn");
      return;
    }
    if (this.steps[this.step] === "train" && !this.engine.trainDone) {
      this.engine.log("Train the model before continuing.", "warn");
      return;
    }
    this.step++;
    this.render();
  }
  prev() { if (this.step > 0) { this.step--; this.render(); } }
}

let engine;
let wizard;

function setupTabs() {
  document.querySelectorAll(".tab").forEach((tab) => {
    tab.addEventListener("click", () => {
      const id = tab.dataset.tab;
      document.querySelectorAll(".tab").forEach((t) => t.classList.remove("active"));
      document.querySelectorAll(".tabpanel").forEach((p) => p.classList.remove("active"));
      tab.classList.add("active");
      const panel = document.querySelector('[data-tabpanel="' + id + '"]');
      if (panel) panel.classList.add("active");
      if (id === "graph") engine.renderGraph();
    });
  });
}

function setupSliders() {
  const bindings = [
    ["cfg-group", "val-group", 0],
    ["cfg-kl", "val-kl", 3],
    ["cfg-latent", "val-latent", 0],
    ["cfg-filter", "val-filter", 2]
  ];
  for (const [id, display, decimals] of bindings) {
    const sl = $(id);
    const vd = $(display);
    if (!sl || !vd) continue;
    sl.addEventListener("input", () => {
      vd.textContent = parseFloat(sl.value).toFixed(decimals);
    });
  }
}

function setupCloseButtons() {
  document.querySelectorAll("[data-close]").forEach((btn) => {
    btn.addEventListener("click", () => {
      const id = btn.dataset.close;
      if (id === "export-modal") engine.hideExportModal();
    });
  });
  document.querySelectorAll(".modal").forEach((modal) => {
    modal.addEventListener("click", (e) => {
      if (e.target === modal) {
        if (modal.id === "export-modal") engine.hideExportModal();
        if (modal.id === "wizard") wizard.close();
      }
    });
  });
}

function setupExportOptions() {
  // Keep the file list, preview, and total size in sync with checkbox state.
  // No-op when the bundle hasn't been built yet (modelFiles empty).
  const boxes = document.querySelectorAll(".opt input[type='checkbox']");
  boxes.forEach((cb) => {
    cb.addEventListener("change", () => {
      if (!engine || !engine.modelFiles || engine.modelFiles.length === 0) return;
      const filtered = engine.applyExportOptions();
      engine.renderFileList(filtered);
      engine.renderExportSize(filtered);
    });
  });
}

function setupButtons() {
  $("btn-fetch").addEventListener("click", async () => {
    $("btn-fetch").disabled = true;
    await engine.fetch();
    $("btn-fetch").disabled = false;
  });
  $("btn-train").addEventListener("click", async () => {
    $("btn-train").disabled = true;
    await engine.train();
    $("btn-train").disabled = false;
  });
  $("btn-stop").addEventListener("click", () => engine.stop());
  $("btn-export").addEventListener("click", () => engine.showExportModal());
  $("btn-export-2").addEventListener("click", () => engine.showExportModal());
  $("btn-wizard").addEventListener("click", () => wizard.open());
  $("btn-reset").addEventListener("click", () => {
    if (confirm("Reset all state? This clears the model and metrics.")) engine.reset();
  });
  $("btn-download").addEventListener("click", () => engine.downloadBundle());

  $("btn-graph-render").addEventListener("click", () => {
    engine.graph.layout2D(engine.kgSeed);
    engine.renderGraph();
  });
  $("btn-graph-save").addEventListener("click", async () => {
    try { await engine.storage.saveGraph(engine.graph); engine.log("Graph saved to IndexedDB.", "ok"); }
    catch (e) { engine.log("Save failed: " + e.message, "err"); }
  });
  $("btn-graph-load").addEventListener("click", async () => {
    try { engine.graph = await engine.storage.loadGraph(); engine.renderGraph(); engine.log("Graph loaded.", "ok"); }
    catch (e) { engine.log("Load failed: " + e.message, "err"); }
  });
  $("btn-graph-clear").addEventListener("click", async () => {
    try { await engine.storage.clearGraph(); engine.graph = new KnowledgeGraph(); engine.renderGraph(); engine.log("Graph cleared.", "ok"); }
    catch (e) { engine.log("Clear failed: " + e.message, "err"); }
  });
  $("btn-osc-measure").addEventListener("click", () => engine.measureOsc());

  $("wiz-next").addEventListener("click", () => wizard.next());
  $("wiz-back").addEventListener("click", () => wizard.prev());
  $("wiz-skip").addEventListener("click", () => wizard.close());
}

function boot() {
  engine = new Engine();
  wizard = new Wizard(engine);

  engine.log("Micro Distillery v3.1.2 initialized.");
  engine.log("Data sources (2026): HuggingFace + GitHub");
  engine.log("Click Setup Wizard in the header, or use the pipeline.");
  engine.log("Ready.");

  window.addEventListener("error", (e) => {
    try { engine.log("Unhandled: " + (e.message || e.error || e), "err"); } catch (_) {}
  });
  window.addEventListener("unhandledrejection", (e) => {
    const r = e && e.reason;
    const m = r && r.message ? r.message : String(r);
    try { engine.log("Unhandled rejection: " + m, "err"); } catch (_) {}
  });

  setupTabs();
  setupSliders();
  setupCloseButtons();
  setupExportOptions();
  setupButtons();
  engine.updateMetrics();
  engine.updateOscDisplay();
  engine.renderGraph();
  engine.setProgress(0, "idle");

  let seen = null;
  try { seen = localStorage.getItem("md_wizard_seen"); } catch (_) {}
  if (!seen) {
    setTimeout(() => {
      try { wizard.open(); } catch (_) {}
      try { localStorage.setItem("md_wizard_seen", "1"); } catch (_) {}
    }, 400);
  }

  const offlineBar = $("offline-bar");
  const updateOnline = () => offlineBar.classList.toggle("hidden", navigator.onLine);
  window.addEventListener("online", updateOnline);
  window.addEventListener("offline", updateOnline);
  updateOnline();

  window.addEventListener("resize", () => engine.renderGraph());

  if ("serviceWorker" in navigator) {
    navigator.serviceWorker.register("sw.js").catch(() => { /* expected when sw.js absent */ });
  }
}

window.addEventListener("load", boot);
</script>
</body>
</html>