File size: 71,092 Bytes
58dd7d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b83c9c
58dd7d3
 
 
1b83c9c
 
 
 
 
 
 
 
 
 
58dd7d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b83c9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58dd7d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b83c9c
58dd7d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b83c9c
58dd7d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b83c9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58dd7d3
 
1b83c9c
58dd7d3
 
 
 
49d46a6
 
1b83c9c
49d46a6
 
 
 
1b83c9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49d46a6
1b83c9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58dd7d3
 
 
 
 
 
1b83c9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58dd7d3
 
 
 
 
 
 
 
 
 
 
49d46a6
 
 
 
 
 
 
 
 
58dd7d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b83c9c
58dd7d3
 
 
 
 
 
1b83c9c
 
58dd7d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b83c9c
 
 
 
58dd7d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0f55048
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49d46a6
1b83c9c
49d46a6
1b83c9c
 
49d46a6
 
1b83c9c
 
49d46a6
 
0f55048
 
49d46a6
0f55048
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
49d46a6
0f55048
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58dd7d3
49d46a6
58dd7d3
 
49d46a6
1b83c9c
49d46a6
 
 
 
1b83c9c
 
 
 
 
 
 
 
 
 
 
58dd7d3
 
 
 
 
 
0f55048
 
1b83c9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58dd7d3
 
 
 
 
 
49d46a6
 
 
 
 
 
 
 
1b83c9c
 
 
 
58dd7d3
 
 
 
 
 
 
 
 
 
 
 
0f55048
 
 
1b83c9c
0f55048
58dd7d3
 
 
 
 
 
 
 
 
 
 
 
 
 
0f55048
1b83c9c
58dd7d3
0f55048
 
 
 
 
 
 
 
58dd7d3
 
49d46a6
 
 
 
 
 
1b83c9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58dd7d3
 
 
 
 
 
0f55048
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58dd7d3
 
 
 
 
 
 
 
 
 
1b83c9c
 
 
 
 
 
 
 
 
 
 
58dd7d3
1b83c9c
 
 
 
 
 
 
 
58dd7d3
1b83c9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
58dd7d3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1b83c9c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
browser_session.py
InsightUX research browser.

Flow:
    1. Window opens on real google.com β€” search and click through like a
       normal browser (no scraping, no fake results β€” it's just Google).
    2. Land on whatever page you want to study. Press S to start tracking.
    3. Gaze dot + AOI highlighting (same as run_session.py) kicks in and
       logs sessions/<timestamp>/gaze_log.jsonl + dom_log.jsonl.
    4. Press E to stop. The window auto-navigates to a generated
       analysis_report.html: ranked attention, dwell timeline, heatmap.

Prerequisite:
    calibration.pkl must exist in the working directory (run calibrate.py first).

Run:
    python browser_session.py
"""

import os
import sys
import json
import time
import math
import threading
import subprocess
from dataclasses import replace
from datetime import datetime

# Windows consoles default to a non-UTF-8 codepage (cp1252) β€” a stray
# unicode character in any print() (ours, or one of calibrate.py/validate.py's
# once relaunched as a subprocess sharing this process's stdout handle) would
# otherwise crash the whole process with UnicodeEncodeError. Confirmed this
# happened for real during a calibration run mid-session.
if hasattr(sys.stdout, "reconfigure"):
    sys.stdout.reconfigure(encoding="utf-8", errors="replace")
if hasattr(sys.stderr, "reconfigure"):
    sys.stderr.reconfigure(encoding="utf-8", errors="replace")

import cv2
import numpy as np
import webview
import pyautogui

from preprocessing.preprocessing_pipeline import (
    create_face_mesh,
    estimate_camera_matrix,
    estimate_head_pose,
    compute_iris_radius,
    compute_ear,
    step1_normalize,
    step2_illumination,
    LEFT_EYE_INDICES,
    LEFT_EAR_INDICES,
    LEFT_IRIS_INDICES,
    RIGHT_EYE_INDICES,
    RIGHT_EAR_INDICES,
    RIGHT_IRIS_INDICES,
)
from inference_pipeline import InsightUXPipeline, GazeAngleSmoother
from session_logger import GazeLogger
from analysis import generate_report


# =============================================================================
# CONFIG β€” kept identical to run_session.py so calibration.pkl stays valid
# =============================================================================

# Bundled read-only assets (models/checkpoints) vs. per-user writable data
# (calibration.pkl, sessions/, the generated landing page) need different
# roots once frozen by PyInstaller β€” onedir nests bundled data under
# _internal/, alongside a persistent folder holding InsightUX.exe itself.
if getattr(sys, "frozen", False):
    RESOURCE_DIR = sys._MEIPASS
    DATA_DIR     = os.path.dirname(sys.executable)
else:
    RESOURCE_DIR = os.path.dirname(os.path.abspath(__file__))
    DATA_DIR     = RESOURCE_DIR
BASE_DIR         = DATA_DIR   # kept as an alias: subprocess cwd for calibrate/validate relaunches

# Bump alongside packaging/installer.iss's AppVersion on every release, and
# update version.json in the same commit β€” see packaging/README.md.
VERSION = "1.0.0"
VERSION_CHECK_URL = "https://huggingface.co/arpitasethiii/insightux/resolve/main/version.json"

ONNX_PATH        = os.path.join(RESOURCE_DIR, "models", "gaze_cnn_v4.onnx")
CALIBRATION_PATH = os.path.join(DATA_DIR, "calibration.pkl")
SCREEN_W, SCREEN_H = pyautogui.size()

PATCH_SOURCE   = "blended"
POSE_SMOOTH    = 0.65
MAX_JUMP       = 220
NO_FACE_RESET  = 15
DOM_EVERY      = 6

EURO_MINCUTOFF = 0.35
EURO_BETA      = 0.12

# Rolling-median window applied to (pitch, yaw) BEFORE the RBF query.
# See GazeAngleSmoother in inference_pipeline.py for why this is essential
# and not merely a nicety. Must match validate.py to stay comparable.
ANGLE_SMOOTH_WINDOW = 10

POSE_NORM_SCALE         = 30.0
HEAD_PITCH_COMPENSATION = 0.0  # reverted β€” 0.35 made accuracy worse, not better
HEAD_YAW_COMPENSATION   = 0.0

SESSIONS_ROOT = os.path.join(DATA_DIR, "sessions")

# The window opens on this local, fully InsightUX-branded page instead of
# raw google.com. What you type here still hits REAL Google β€” no fake
# results, no scraping β€” this page is just the visible skin around that
# one redirect step. Typed URLs go straight there instead.
_LANDING_HTML = r"""<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="insightux-landing" content="true">
<title>InsightUX β€” Eye-Tracking Research Browser</title>
<style>
  * { box-sizing: border-box; }
  body {
    margin: 0; height: 100vh; display: flex; flex-direction: column;
    align-items: center; justify-content: center;
    background: radial-gradient(circle at 50% 35%, #221a2e, #100e16 70%);
    font-family: -apple-system, 'Segoe UI', Arial, sans-serif; color: #f0ecf7;
  }
  .logo {
    font-size: 42px; font-weight: 700; letter-spacing: -0.01em;
    background: linear-gradient(90deg, #9B59FF, #FF2DF0);
    -webkit-background-clip: text; -webkit-text-fill-color: transparent;
    margin-bottom: 6px;
  }
  .tagline { color: #8a8098; font-size: 14px; margin-bottom: 36px; }
  form { width: 560px; max-width: 88vw; }
  input {
    width: 100%; padding: 15px 20px; font-size: 15px; border-radius: 26px;
    border: 1.5px solid #3a3348; background: #1c1826; color: #f0ecf7;
    outline: none; transition: border-color 0.15s;
  }
  input:focus { border-color: #9B59FF; }
  input::placeholder { color: #635a72; }
  .hint {
    margin-top: 18px; font-size: 12px; color: #6b6278; text-align: center;
    line-height: 1.6;
  }
  .hint b { color: #9B59FF; }
</style>
</head>
<body>
  <div class="logo">InsightUX</div>
  <div class="tagline">Eye-Tracking Research Browser</div>
  <form id="searchForm">
    <input id="searchInput" type="text" autofocus
           placeholder="Search Google, or enter a website address">
  </form>
  <div class="hint">
    Search results and websites open below as normal.<br>
    Once you land on the page you want to study, press <b>S</b> to start eye-tracking,
    <b>E</b> to stop.
  </div>
<script>
  document.getElementById('searchForm').addEventListener('submit', function(e){
    e.preventDefault();
    const raw = document.getElementById('searchInput').value.trim();
    if (!raw) return;

    const looksLikeUrl = /^https?:\/\//i.test(raw) ||
                          (/^[\w-]+(\.[\w-]+)+([/?#].*)?$/i.test(raw) && !raw.includes(' '));

    if (looksLikeUrl) {
      window.location.href = raw.startsWith('http') ? raw : ('https://' + raw);
    } else {
      window.location.href = 'https://www.google.com/search?q=' + encodeURIComponent(raw);
    }
  });
</script>
</body>
</html>
"""


def _write_landing_page():
    path = os.path.join(DATA_DIR, "insightux_landing.html")
    with open(path, "w", encoding="utf-8") as f:
        f.write(_LANDING_HTML)
    return "file://" + path.replace(os.sep, "/")


LANDING_URL = _write_landing_page()


def normalize_pose(pitch_deg, yaw_deg, roll_deg):
    return np.array([
        pitch_deg / POSE_NORM_SCALE,
        yaw_deg   / POSE_NORM_SCALE,
        roll_deg  / POSE_NORM_SCALE,
    ], dtype=np.float32)

def compensate_pitch(raw_pitch, head_pitch_deg):
    return raw_pitch - np.radians(head_pitch_deg) * HEAD_PITCH_COMPENSATION

def compensate_yaw(raw_yaw, head_yaw_deg):
    return raw_yaw - np.radians(head_yaw_deg) * HEAD_YAW_COMPENSATION


# =============================================================================
# ONE EURO FILTER (display smoothing only)
# =============================================================================

class OneEuroFilter:
    def __init__(self, mincutoff=0.8, beta=0.4, dcutoff=1.0):
        self.mincutoff = mincutoff
        self.beta = beta
        self.dcutoff = dcutoff
        self.x_prev = None
        self.dx_prev = 0.0
        self.t_prev = None

    @staticmethod
    def _alpha(cutoff, dt):
        tau = 1.0 / (2 * math.pi * cutoff)
        return 1.0 / (1.0 + tau / dt)

    def __call__(self, x, t):
        if self.x_prev is None:
            self.x_prev, self.t_prev = x, t
            return x
        dt = t - self.t_prev
        if dt <= 0:
            dt = 1e-3
        self.t_prev = t
        dx = (x - self.x_prev) / dt
        a_d = self._alpha(self.dcutoff, dt)
        dx_hat = a_d * dx + (1 - a_d) * self.dx_prev
        cutoff = self.mincutoff + self.beta * abs(dx_hat)
        a = self._alpha(cutoff, dt)
        x_hat = a * x + (1 - a) * self.x_prev
        self.x_prev, self.dx_prev = x_hat, dx_hat
        return x_hat

    def reset(self):
        self.x_prev = None
        self.dx_prev = 0.0
        self.t_prev = None


# =============================================================================
# JS: browser chrome (always present) β€” top toolbar (back/forward/reload/
# home/address bar) + left sidebar (Calibrate/Validate/Start Session), so the
# window reads as a normal browser instead of a bare content pane. Purely an
# HTML/CSS/JS overlay drawn on top of whatever page is loaded β€” pywebview
# 4.4.1 has no native toolbar API, so this is the only way to add persistent
# chrome without switching GUI frameworks.
#
# The page itself is pushed down/right by exactly the toolbar height and
# sidebar width (via forced <body> padding, not just floating the chrome on
# top of it) so real page content is never hidden underneath β€” the chrome
# and the website occupy clearly separate regions instead of overlapping.
# overflow-x is force-hidden as a safety net against the small width
# reduction ever introducing a horizontal scrollbar. The one edge case this
# can't fully solve: a small minority of sites use position:fixed elements
# of their own pinned to the true viewport edges (independent of body
# padding) β€” those can still end up visually behind our chrome.
# =============================================================================

CHROME_JS = r"""
(function(){
  if (window.__insightuxControl) { return; }
  window.__insightuxControl = true;

  // S/E (start/stop tracking) only make sense on an actual website β€” not on
  // the InsightUX landing/search page and not on a generated insights
  // report page. The toolbar/sidebar, and X (quit), always work everywhere.
  const isLanding   = !!document.querySelector('meta[name="insightux-landing"]');
  const isReport    = !!document.querySelector('meta[name="insightux-report"]');
  const isTrackable = !isLanding && !isReport;

  const style = document.createElement('style');
  style.textContent = `
    html {
      overflow-x: hidden !important;
    }
    body {
      box-sizing: border-box !important;
      width: 100vw !important;
      margin: 0 !important;
      padding-top: 46px !important;
      padding-left: 84px !important;
      overflow-x: hidden !important;
    }
    #__insightux_toolbar {
      position:fixed; top:0; left:0; right:0; height:46px; z-index:2147483647;
      display:flex; align-items:center; gap:10px; padding:0 12px; box-sizing:border-box;
      background:linear-gradient(90deg, rgba(20,18,26,0.97), rgba(30,20,40,0.97));
      border-bottom:2px solid; border-image:linear-gradient(90deg,#7B2FBE,#FF2DF0) 1;
      box-shadow:0 3px 14px rgba(0,0,0,0.35);
      font:12px -apple-system,'Segoe UI',Arial; color:#f0ecf7;
    }
    #__insightux_toolbar button {
      flex:0 0 auto; width:30px; height:30px; border-radius:8px; cursor:pointer;
      background:#241f2e; border:1px solid #3a3348; color:#f0ecf7; font-size:15px;
      display:flex; align-items:center; justify-content:center;
    }
    #__insightux_toolbar button:hover { background:#302a3d; }
    #__iux_addr_form { flex:1 1 auto; min-width:0; }
    #__iux_addr {
      width:100%; box-sizing:border-box; padding:7px 14px; border-radius:16px;
      border:1.5px solid #3a3348; background:#1c1826; color:#f0ecf7; outline:none;
      font-size:12.5px;
    }
    #__iux_addr:focus { border-color:#9B59FF; }
    #__insightux_status {
      flex:0 0 auto; color:#c9a6f5; white-space:nowrap; max-width:30vw;
      overflow:hidden; text-overflow:ellipsis;
    }
    #__insightux_sidebar {
      position:fixed; top:46px; left:0; bottom:0; width:84px; z-index:2147483647;
      display:flex; flex-direction:column; align-items:stretch; gap:8px; padding:10px 8px;
      box-sizing:border-box; font:11px -apple-system,'Segoe UI',Arial;
      background:linear-gradient(180deg, rgba(24,20,32,0.97), rgba(16,14,22,0.97));
      border-right:2px solid; border-image:linear-gradient(180deg,#7B2FBE,#FF2DF0) 1;
      box-shadow:3px 0 14px rgba(0,0,0,0.35);
    }
    #__insightux_sidebar button {
      background:#241f2e; border:1px solid #3a3348; color:#f0ecf7; border-radius:10px;
      padding:10px 4px; cursor:pointer; line-height:1.5; font-size:11px; text-align:center;
    }
    #__insightux_sidebar button:hover { background:#302a3d; }
    #__iux_session.active { background:linear-gradient(135deg,#7B2FBE,#FF2DF0); border-color:transparent; }
    #__insightux_sidebar .__iux_hints {
      margin-top:auto; color:#7a7288; font-size:9.5px; line-height:1.6; text-align:center;
    }
    #__insightux_update {
      display:none; cursor:pointer; flex:0 0 auto; white-space:nowrap;
      padding:5px 12px; border-radius:12px; font-weight:600; font-size:11px;
      background:linear-gradient(135deg,#7B2FBE,#FF2DF0); color:#fff;
    }
    #__insightux_update:hover { filter:brightness(1.12); }
  `;
  document.head.appendChild(style);

  const toolbar = document.createElement('div');
  toolbar.id = '__insightux_toolbar';
  toolbar.innerHTML = `
    <button type="button" id="__iux_back" title="Back">&#8592;</button>
    <button type="button" id="__iux_fwd" title="Forward">&#8594;</button>
    <button type="button" id="__iux_reload" title="Reload">&#8635;</button>
    <button type="button" id="__iux_home" title="Home">&#8962;</button>
    <form id="__iux_addr_form"><input id="__iux_addr" type="text" autocomplete="off"
      placeholder="Search or enter address"></form>
    <span id="__insightux_update" title="Click to open the download page"></span>
    <span id="__insightux_status">Press S to start tracking &middot; E to stop &middot; H heatmap &middot; M mouse panel &middot; X quit</span>
  `;
  document.documentElement.appendChild(toolbar);

  const sidebar = document.createElement('div');
  sidebar.id = '__insightux_sidebar';
  sidebar.innerHTML = `
    <button type="button" id="__iux_calibrate">&#128065;<br>Calibrate</button>
    <button type="button" id="__iux_validate">&#127919;<br>Validate</button>
    <button type="button" id="__iux_session">&#9654;<br>Start<br>Session</button>
    <div class="__iux_hints">S/E start&#47;stop<br>H heatmap<br>M panel<br>X quit</div>
  `;
  document.documentElement.appendChild(sidebar);

  setInterval(function(){
    if (!document.documentElement.contains(toolbar)) document.documentElement.appendChild(toolbar);
    if (!document.documentElement.contains(sidebar)) document.documentElement.appendChild(sidebar);
  }, 1000);

  const statusEl = () => document.getElementById('__insightux_status');
  window.insightuxSetStatus = function(text){
    const el = statusEl();
    if (el) el.textContent = text;
  };
  window.insightuxSetSessionUI = function(isTracking){
    const btn = document.getElementById('__iux_session');
    if (!btn) return;
    btn.innerHTML = isTracking ? '&#9632;<br>Stop<br>Session' : '&#9654;<br>Start<br>Session';
    btn.classList.toggle('active', isTracking);
  };
  window.insightuxShowUpdate = function(version, url){
    const el = document.getElementById('__insightux_update');
    if (!el) return;
    el.textContent = 'Update available (v' + version + ')';
    el.style.display = 'inline-block';
    el.onclick = function(){
      if (window.pywebview && window.pywebview.api && window.pywebview.api.open_update_page) {
        window.pywebview.api.open_update_page(url);
      }
    };
  };

  // -- address bar / navigation (pure DOM history/location, no Python round-trip) --
  const addrInput = document.getElementById('__iux_addr');
  addrInput.value = isLanding ? '' : location.href;
  document.getElementById('__iux_addr_form').addEventListener('submit', function(e){
    e.preventDefault();
    const raw = addrInput.value.trim();
    if (!raw) return;
    const looksLikeUrl = /^https?:\/\//i.test(raw) ||
                          (/^[\w-]+(\.[\w-]+)+([/?#].*)?$/i.test(raw) && !raw.includes(' '));
    if (looksLikeUrl) {
      location.href = raw.startsWith('http') ? raw : ('https://' + raw);
    } else {
      location.href = 'https://www.google.com/search?q=' + encodeURIComponent(raw);
    }
  });
  document.getElementById('__iux_back').addEventListener('click', function(){ history.back(); });
  document.getElementById('__iux_fwd').addEventListener('click', function(){ history.forward(); });
  document.getElementById('__iux_reload').addEventListener('click', function(){ location.reload(); });
  document.getElementById('__iux_home').addEventListener('click', function(){
    location.href = __INSIGHTUX_LANDING_URL__;
  });

  // -- sidebar actions (Python decides validity β€” camera/session conflicts,
  // wrong page β€” and reports back through the status text) --
  function callApi(method){
    if (window.pywebview && window.pywebview.api && window.pywebview.api[method]) {
      window.pywebview.api[method]();
    }
  }
  document.getElementById('__iux_calibrate').addEventListener('click', function(){ callApi('start_calibration'); });
  document.getElementById('__iux_validate').addEventListener('click', function(){ callApi('run_validation'); });
  document.getElementById('__iux_session').addEventListener('click', function(){ callApi('toggle_session'); });

  function isTypingTarget(el){
    if (!el) return false;
    const tag = el.tagName ? el.tagName.toLowerCase() : '';
    return tag === 'input' || tag === 'textarea' || tag === 'select' ||
           el.isContentEditable === true;
  }

  document.addEventListener('keydown', function(e){
    const typingBlocked = isTypingTarget(e.target) || isTypingTarget(document.activeElement);
    const apiReady = !!(window.pywebview && window.pywebview.api);

    if (e.key === 'x' || e.key === 'X') {
      if (typingBlocked || !apiReady) return;
      window.pywebview.api.quit_app();
      return;
    }

    if (!isTrackable) return;  // S/E do nothing on the landing page or a report page

    if (e.key === 's' || e.key === 'S' || e.key === 'e' || e.key === 'E') {
      console.log('[insightux] key=' + e.key, 'typingBlocked=' + typingBlocked, 'apiReady=' + apiReady, 'focusedEl=' + (document.activeElement && document.activeElement.tagName));
    }
    if (!apiReady) return;
    if (typingBlocked) {
      window.insightuxSetStatus("Click somewhere on the page (not a text field) before pressing S/E");
      return;
    }

    if (e.key === 's' || e.key === 'S') {
      window.pywebview.api.start_tracking();
    } else if (e.key === 'e' || e.key === 'E') {
      window.pywebview.api.stop_tracking();
    }
  }, true);  // capture phase β€” fires before page scripts can intercept/stop the event
})();
"""
CHROME_JS = CHROME_JS.replace("__INSIGHTUX_LANDING_URL__", json.dumps(LANDING_URL))

# JS: tracking overlay (gaze dot + AOI highlighting) β€” injected only once
# tracking actually starts. Ported directly from run_session.py's JS_SETUP.
TRACKING_JS = r"""
(function(){
  if (window.__insightux) { return; }
  const OWN_UI_IDS = new Set(['__insightux_toolbar', '__insightux_sidebar', '__insightux_pill', '__insightux_canvas', '__insightux_mouse_panel']);
  const OWN_UI_SELECTOR = '#__insightux_toolbar, #__insightux_sidebar, #__insightux_pill, #__insightux_canvas, #__insightux_mouse_panel';
  const state = { aois: [] };
  const dwell = { pendLabel: null, pendSince: 0, activeLabel: null, emptySince: 0 };
  const DWELL_MS = 420;
  const RELEASE_MS = 1200;

  let activeBox = null;
  const target  = { fx: 0.5, fy: 0.5 };
  const dot     = { x: null, y: null };
  const DOT_LERP = 0.07;

  const cv = document.createElement('canvas');
  cv.id = '__insightux_canvas';
  cv.style.cssText = 'position:fixed;left:0;top:0;width:100vw;height:100vh;pointer-events:none;z-index:2147483646;';
  (document.body || document.documentElement).appendChild(cv);
  const ctx = cv.getContext('2d');
  function resize(){ cv.width = window.innerWidth; cv.height = window.innerHeight; }
  resize();
  window.addEventListener('resize', resize, {passive:true});
  setInterval(function(){
    if (!document.body.contains(cv)) document.body.appendChild(cv);
  }, 1000);

  const MIN_W=40, MIN_H=24, MAX_AOIS=90, MAX_SCAN=2500;
  const PAD_PX = 90;
  const TALL_WRAPPER_H = 220;

  const ALWAYS_SELECTOR = "nav, header, footer, img, video, iframe, h1, h2, h3, button, figure, [class*='hero'], [class*='banner'], [class*='card']";
  const TEXT_CONTAINER_SELECTOR = "div, span, section, article, main, aside, p, li";

  function hasOwnText(el){
    for (const node of el.childNodes){
      if (node.nodeType === 3 && node.textContent.trim().length > 2) return true;
    }
    return false;
  }

  function labelFor(el){
    if (el.dataset && el.dataset.aoi) return el.dataset.aoi.slice(0,40);
    const tag = el.tagName.toLowerCase();
    if (tag==='nav') return 'navbar';
    if (tag==='header') return 'header';
    if (tag==='footer') return 'footer';
    if (tag==='img'){
      const alt=(el.getAttribute('alt')||'').trim();
      if (alt) return 'img: '+alt.slice(0,30);
      const src=el.getAttribute('src')||'';
      const name=src.split('/').pop().split('?')[0];
      return 'img: '+(name||'image').slice(0,30);
    }
    if (tag==='video') return 'video';
    if (tag==='iframe') return 'embed';
    if (tag==='h1'||tag==='h2'){
      const t=(el.innerText||'').trim().replace(/\s+/g,' ');
      if (t) return tag+': '+t.slice(0,30);
    }
    const id=el.id?('#'+el.id):'';
    let cls='';
    if (el.className && typeof el.className==='string'){
      const f=el.className.trim().split(/\s+/)[0];
      if (f) cls='.'+f;
    }
    const txt=(el.innerText||'').trim().replace(/\s+/g,' ').slice(0,24);
    const base=id||cls||tag;
    return txt?(base+' ('+txt+')'):base;
  }

  function refresh(){
    const vh = window.innerHeight;
    const raw = [];
    const seen = new Set();
    let full = false;

    function consider(el){
      if (full) return;
      // Never track InsightUX's own injected UI (toolbar, sidebar, the
      // gaze-dot canvas, the mouse tracker panel) as if it were page content.
      if (OWN_UI_IDS.has(el.id)) return;
      if (el.closest && el.closest(OWN_UI_SELECTOR)) return;
      const tag = el.tagName.toLowerCase();
      const explicit = !!(el.dataset && el.dataset.aoi);
      if (!explicit){
        const isAlways = (tag==='nav'||tag==='header'||tag==='footer'||tag==='img'||
                           tag==='video'||tag==='iframe'||
                           tag==='h1'||tag==='h2'||tag==='h3'||tag==='button'||tag==='figure') ||
                          (el.className && typeof el.className==='string' &&
                           /hero|banner|card/i.test(el.className));
        if (!isAlways && !hasOwnText(el)) return;
      }
      const r = el.getBoundingClientRect();
      if (r.width<MIN_W || r.height<MIN_H) return;
      if (r.bottom<0 || r.top>vh) return;
      const label = labelFor(el);
      if (!label) return;
      const key = label+'@'+Math.round(r.left)+','+Math.round(r.top);
      if (seen.has(key)) return;
      seen.add(key);
      const pos = getComputedStyle(el).position;
      raw.push({el:el, label:label, x:Math.round(r.left), y:Math.round(r.top),
                w:Math.round(r.width), h:Math.round(r.height),
                sticky:(pos==='sticky'||pos==='fixed')});
      if (raw.length >= MAX_AOIS*2) full = true;
    }

    document.querySelectorAll('[data-aoi]').forEach(consider);
    document.querySelectorAll(ALWAYS_SELECTOR).forEach(consider);

    const candidates = document.querySelectorAll(TEXT_CONTAINER_SELECTOR);
    for (let i=0; i<candidates.length && i<MAX_SCAN && !full; i++){
      consider(candidates[i]);
    }

    const filtered = raw.filter(o => {
      const isTallWrapper = o.h > TALL_WRAPPER_H &&
                             raw.some(o2 => o2 !== o && o.el.contains(o2.el));
      return !isTallWrapper;
    });

    state.aois = filtered.slice(0, MAX_AOIS).map(o => ({
      label:o.label, x:o.x, y:o.y, w:o.w, h:o.h, sticky:o.sticky
    }));
  }
  refresh();
  window.addEventListener('scroll', refresh, {passive:true});
  setInterval(refresh, 400);

  window.insightuxAOIs = function(){
    return JSON.stringify({
      url: location.href,
      scrollX: Math.round(window.scrollX),
      scrollY: Math.round(window.scrollY),
      viewport:{w:window.innerWidth,h:window.innerHeight},
      page:{w:document.documentElement.scrollWidth,h:document.documentElement.scrollHeight},
      aois: state.aois
    });
  };

  window.insightuxUpdate = function(fx, fy){
    target.fx = fx;
    target.fy = fy;
  };

  function renderLoop(){
    const w = window.innerWidth, h = window.innerHeight;
    const tx = target.fx * w, ty = target.fy * h;
    if (dot.x === null){ dot.x = tx; dot.y = ty; }
    dot.x += (tx - dot.x) * DOT_LERP;
    dot.y += (ty - dot.y) * DOT_LERP;

    const now = performance.now();
    const px = dot.x, py = dot.y;

    let cand = null;
    for (const a of state.aois){
      if (px>=a.x-PAD_PX && px<=a.x+a.w+PAD_PX && py>=a.y-PAD_PX && py<=a.y+a.h+PAD_PX){
        if (!cand || (a.w*a.h)<(cand.w*cand.h)) cand = a;
      }
    }

    const candLabel = cand ? cand.label : null;
    if (candLabel !== dwell.pendLabel){ dwell.pendLabel = candLabel; dwell.pendSince = now; }
    if (cand){
      dwell.emptySince = 0;
      if (dwell.activeLabel !== cand.label && (now - dwell.pendSince) >= DWELL_MS){
        dwell.activeLabel = cand.label;
      }
    } else {
      if (dwell.emptySince === 0) dwell.emptySince = now;
      if (dwell.activeLabel && (now - dwell.emptySince) >= RELEASE_MS){
        dwell.activeLabel = null;
      }
    }

    let active = null;
    if (dwell.activeLabel){
      for (const a of state.aois){ if (a.label === dwell.activeLabel){ active = a; break; } }
      if (!active) dwell.activeLabel = null;
    }
    activeBox = active;

    ctx.clearRect(0,0,cv.width,cv.height);

    if (activeBox){
      ctx.fillStyle = 'rgba(123,47,190,0.16)';
      ctx.fillRect(activeBox.x, activeBox.y, activeBox.w, activeBox.h);
      ctx.strokeStyle = '#7B2FBE'; ctx.lineWidth = 3;
      ctx.strokeRect(activeBox.x, activeBox.y, activeBox.w, activeBox.h);
      const label = activeBox.label;
      ctx.font = 'bold 13px Arial';
      const tw = ctx.measureText(label).width + 16;
      const ly = Math.max(0, activeBox.y - 22);
      ctx.fillStyle = '#7B2FBE';
      ctx.fillRect(activeBox.x, ly, tw, 20);
      ctx.fillStyle = '#FFFFFF';
      ctx.fillText(label, activeBox.x + 8, ly + 14);
    }

    ctx.beginPath();
    ctx.arc(dot.x, dot.y, 13, 0, 2*Math.PI);
    ctx.fillStyle = 'rgba(255,255,255,0.9)';
    ctx.fill();
    ctx.beginPath();
    ctx.arc(dot.x, dot.y, 9, 0, 2*Math.PI);
    ctx.fillStyle = '#FF2DF0';
    ctx.fill();
    ctx.lineWidth = 2;
    ctx.strokeStyle = 'rgba(0,0,0,0.55)';
    ctx.stroke();

    requestAnimationFrame(renderLoop);
  }
  requestAnimationFrame(renderLoop);

  window.__insightux = true;
})();
"""


# JS: mouse tracking overlay β€” ported feature-for-feature from the
# "Mouse Tracker & Heatmap" Chrome extension (Mouse/content.js + background.js
# + popup.js), adapted to run as a single injected script instead of a
# content-script/background/popup trio (pywebview has no extension host).
# Same trail/heatmap/dwell sampling loop, same click interest labeling, same
# heatmap render, plus an in-page panel that replaces the extension's popup
# (Start/Stop/Clear/Toggle Heatmap/View Logs, timer, "Most Viewed Element").
# =============================================================================

MOUSE_JS = r"""
(function(){
  if (window.__insightuxMouse) { return; }
  window.__insightuxMouse = true;

  let isTracking = true;
  let canvas = null;
  let panelOpen = false;
  let logsOpen = false;

  // Per-sync buffers (flushed to Python every 2s for on-disk persistence)
  let trailBuffer = [];
  let heatmapBuffer = [];
  let clickBuffer = [];
  let dwellBuffer = [];

  // Cumulative in-page state (mirrors the extension's background.js state)
  let allTrail = [];
  let allHeatmap = [];
  let allClicks = [];
  let dwellTotals = {};

  let sessionStart = Date.now();
  let sessionStop = null;

  let lastClientX = 0, lastClientY = 0;
  let lastPageX = 0, lastPageY = 0;
  let lastSampleTime = 0;
  const sampleRate = 50;
  const DWELL_THRESHOLD = 5000;
  let stationaryStart = 0;
  let isStationary = false;

  window.insightuxMouseSetTracking = function(on){
    isTracking = !!on;
    if (isTracking) {
      sessionStart = Date.now();
      sessionStop = null;
    } else {
      sessionStop = Date.now();
      flushDwell();
    }
    updatePanel();
  };

  // ---- sampling loop (trail while moving, heatmap dwell points while still) ----
  setInterval(function(){
    if (!isTracking) return;
    const currentScrollX = window.scrollX, currentScrollY = window.scrollY;
    if (lastPageX === 0 && lastPageY === 0 && lastClientX !== 0) {
      lastPageX = lastClientX + currentScrollX;
      lastPageY = lastClientY + currentScrollY;
    }
    const currentPageX = (lastClientX !== 0) ? (lastClientX + currentScrollX) : lastPageX;
    const currentPageY = (lastClientY !== 0) ? (lastClientY + currentScrollY) : lastPageY;
    if (currentPageX === 0 && currentPageY === 0) return;

    const now = Date.now();
    const dt = now - lastSampleTime;
    if (dt > 1000) { lastSampleTime = now; return; }

    const dx = currentPageX - lastPageX, dy = currentPageY - lastPageY;
    const distance = Math.sqrt(dx * dx + dy * dy);

    if (distance > 2) {
      const pt = { x: currentPageX, y: currentPageY };
      trailBuffer.push(pt); allTrail.push(pt);
      lastPageX = currentPageX; lastPageY = currentPageY;
      isStationary = false; stationaryStart = 0;
    } else {
      if (!isStationary) { isStationary = true; stationaryStart = now; }
      else {
        const dwellDuration = now - stationaryStart;
        if (dwellDuration > DWELL_THRESHOLD) {
          const pt = { x: currentPageX, y: currentPageY };
          heatmapBuffer.push(pt); allHeatmap.push(pt);
        }
      }
    }
    lastSampleTime = now;
  }, sampleRate);

  // ---- dwell / element-interest tracking ----
  let currentHoverLabel = null, currentHoverStartTime = 0;
  function handleHoverChange(newLabel){
    if (!isTracking) return;
    if (newLabel !== currentHoverLabel) {
      const now = Date.now();
      if (currentHoverLabel && currentHoverStartTime > 0) {
        const duration = now - currentHoverStartTime;
        if (duration > 10) recordDwell(currentHoverLabel, duration);
      }
      currentHoverLabel = newLabel;
      currentHoverStartTime = newLabel ? now : 0;
    }
  }
  function recordDwell(element, duration){
    dwellBuffer.push({ element: element, duration: duration });
    dwellTotals[element] = (dwellTotals[element] || 0) + duration;
  }
  function flushDwell(){
    if (currentHoverLabel && currentHoverStartTime > 0) {
      const now = Date.now();
      const duration = now - currentHoverStartTime;
      if (duration > 50) recordDwell(currentHoverLabel, duration);
      currentHoverStartTime = now;
    }
  }

  // ---- sync to Python every 2s -> mouse_log.jsonl in the session folder ----
  setInterval(function(){
    flushDwell();
    if (trailBuffer.length || heatmapBuffer.length || clickBuffer.length || dwellBuffer.length) {
      const payload = {
        trail: trailBuffer.length ? trailBuffer : null,
        heatmap: heatmapBuffer.length ? heatmapBuffer : null,
        click: clickBuffer.length ? clickBuffer : null,
        dwell: dwellBuffer.length ? dwellBuffer : null,
      };
      trailBuffer = []; heatmapBuffer = []; clickBuffer = []; dwellBuffer = [];
      if (window.pywebview && window.pywebview.api && window.pywebview.api.log_mouse_data) {
        try { window.pywebview.api.log_mouse_data(payload).catch(function(){}); } catch (e) {}
      }
    }
    updatePanel();
  }, 2000);

  // ---- exclude InsightUX's own injected UI from every measurement ----
  // (the toolbar, sidebar, mouse-tracker panel, heatmap canvas) β€” only
  // real website elements should ever show up in trail/heatmap/dwell/clicks.
  const OWN_UI_IDS = new Set(['__insightux_toolbar', '__insightux_sidebar', '__insightux_pill', '__insightux_canvas', '__insightux_mouse_panel']);
  const OWN_UI_SELECTOR = '#__insightux_toolbar, #__insightux_sidebar, #__insightux_pill, #__insightux_canvas, #__insightux_mouse_panel';
  function isOwnUI(el){
    if (!el) return false;
    if (OWN_UI_IDS.has(el.id)) return true;
    return !!(el.closest && el.closest(OWN_UI_SELECTOR));
  }

  // ---- mouse position + hover tracking ----
  function updateMousePos(e){
    if (isOwnUI(e.target)) return;
    lastClientX = e.clientX; lastClientY = e.clientY;
    if (lastPageX === 0) lastPageX = e.pageX;
    if (lastPageY === 0) lastPageY = e.pageY;
    if (isTracking) handleHoverChange(getSmartLabel(e.target));
  }
  document.addEventListener('mousemove', updateMousePos, true);
  document.addEventListener('mouseenter', updateMousePos, true);
  document.addEventListener('mouseover', updateMousePos, true);
  document.addEventListener('click', updateMousePos, true);
  document.addEventListener('mouseleave', function(){
    lastClientX = 0; lastClientY = 0;
    handleHoverChange(null);
  }, true);

  document.addEventListener('click', function(e){
    if (!isTracking) return;
    if (e.target === canvas) return;
    if (isOwnUI(e.target)) return;
    if (!isInteractable(e.target)) return;

    const x = e.pageX, y = e.pageY;
    const label = getSmartLabel(e.target) || e.target.tagName;
    if (['BODY', 'HTML', 'DIV', 'SPAN'].includes(label) && !e.target.innerText.trim()) return;

    const logEntry = {
      timestamp: new Date().toLocaleTimeString(),
      x: x, y: y, element: label,
      text: e.target.innerText ? e.target.innerText.substring(0, 30).replace(/(\r\n|\n|\r)/gm, ' ').trim() : '',
      url: window.location.href
    };
    clickBuffer.push(logEntry); allClicks.push(logEntry);
    if (canvas) drawClick(x, y);
    updatePanel();
  }, true);

  function isInteractable(el){
    if (!el) return false;
    const tag = el.tagName.toLowerCase();
    if (['a', 'button', 'input', 'select', 'textarea', 'details', 'summary', 'label'].includes(tag)) return true;
    const role = el.getAttribute('role');
    if (role === 'button' || role === 'link' || role === 'menuitem' || role === 'tab') return true;
    try { if (window.getComputedStyle(el).cursor === 'pointer') return true; } catch (e) {}
    let parent = el.parentElement, depth = 0;
    while (parent && depth < 3) {
      const pTag = parent.tagName.toLowerCase();
      if (['a', 'button'].includes(pTag)) return true;
      if (parent.getAttribute('role') === 'button') return true;
      parent = parent.parentElement; depth++;
    }
    if (tag === 'code' || tag === 'pre' || el.classList.contains('code') || el.closest('pre')) return true;
    return false;
  }

  function getSmartLabel(el){
    if (!el) return null;
    const tag = el.tagName.toLowerCase();
    if (['body', 'html', 'main', 'div', 'span', 'section', 'article'].includes(tag)) {
      if (el.id && (el.id.includes('logo') || el.id.includes('wrapper') || el.id.includes('container'))) return null;
      if (el.className && typeof el.className === 'string' &&
          (el.className.toLowerCase().includes('logo') || el.className.toLowerCase().includes('brand'))) return null;
      const aria = el.getAttribute('aria-label');
      if (aria) return 'Element: ' + aria;
      if (el.children.length === 0) {
        const txt = el.innerText.trim();
        if (txt.length > 2 && txt.length < 50 && /[a-zA-Z0-9]/.test(txt)) return 'Element: ' + txt;
      }
      return null;
    }
    if (tag === 'a') return 'Link: ' + (el.innerText.trim().substring(0, 30) || 'Link');
    if (tag === 'button') return 'Button: ' + (el.innerText.trim().substring(0, 30) || 'Button');
    if (tag === 'input') return 'Input: ' + (el.placeholder || el.name || el.id || 'Input');
    if (tag === 'textarea') return 'Input: Text Area';
    if (tag === 'img') return 'Image: ' + (el.alt || 'Image');
    if (['h1', 'h2', 'h3', 'h4', 'h5', 'h6'].includes(tag)) return tag.toUpperCase() + ': ' + el.innerText.trim().substring(0, 40);
    if (tag === 'code' || tag === 'pre') return 'Code: ' + el.innerText.trim().substring(0, 30);
    const text = el.innerText.trim();
    if (text && text.length > 2) {
      if (el.classList.contains('code') || el.closest('pre')) return 'Code: ' + text.substring(0, 30);
      if (text.toLowerCase().includes('no message found')) return null;
      const isHex = /^#[0-9A-F]{6}$/i.test(text) || /^#[0-9A-F]{3}$/i.test(text);
      const isColorName = ['red', 'blue', 'green', 'yellow', 'black', 'white', 'orange', 'purple', 'gray', 'grey', 'pink', 'brown', 'cyan', 'magenta'].includes(text.toLowerCase());
      if (isHex || isColorName) return null;
      if (text.length > 50) return 'Text: ' + text.substring(0, 47) + '...';
      return 'Text: ' + text;
    }
    return null;
  }

  // ---- heatmap overlay canvas (ported 1:1 from the extension) ----
  window.insightuxMouseToggleHeatmap = function(){
    if (canvas) { document.body.removeChild(canvas); canvas = null; return; }
    canvas = document.createElement('canvas');
    canvas.style.position = 'absolute';
    canvas.style.top = '0'; canvas.style.left = '0';
    canvas.style.zIndex = '2147483645';
    canvas.style.pointerEvents = 'none';
    canvas.width = Math.max(document.documentElement.scrollWidth, document.body.scrollWidth, document.documentElement.offsetWidth);
    canvas.height = Math.max(document.documentElement.scrollHeight, document.body.scrollHeight, document.documentElement.offsetHeight);
    document.body.appendChild(canvas);
    drawHeatmapHighQuality(allHeatmap, allTrail, allClicks);
  };

  function drawClick(x, y){
    if (!canvas) return;
    const ctx = canvas.getContext('2d');
    ctx.save();
    ctx.beginPath();
    ctx.strokeStyle = '#00FF00'; ctx.lineWidth = 3;
    ctx.shadowColor = 'black'; ctx.shadowBlur = 2;
    const size = 10;
    ctx.moveTo(x - size, y - size); ctx.lineTo(x + size, y + size);
    ctx.moveTo(x + size, y - size); ctx.lineTo(x - size, y + size);
    ctx.stroke();
    ctx.beginPath(); ctx.arc(x, y, size + 5, 0, Math.PI * 2); ctx.stroke();
    ctx.restore();
  }

  function drawHeatmapHighQuality(heatmapData, trailData, clickData){
    if (!canvas) return;
    const ctx = canvas.getContext('2d');
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    let allHeatPoints = [];
    if (heatmapData && heatmapData.length) allHeatPoints = allHeatPoints.concat(heatmapData);
    if (trailData && trailData.length) allHeatPoints = allHeatPoints.concat(trailData);
    if (clickData && clickData.length) {
      clickData.forEach(function(c){ for (let i = 0; i < 5; i++) allHeatPoints.push({ x: c.x, y: c.y }); });
    }
    if (!allHeatPoints.length) return;
    const radius = 60;
    const brushCanvas = document.createElement('canvas');
    brushCanvas.width = radius * 2; brushCanvas.height = radius * 2;
    const brushCtx = brushCanvas.getContext('2d');
    const g = brushCtx.createRadialGradient(radius, radius, 0, radius, radius, radius);
    g.addColorStop(0, 'rgba(0, 0, 0, 0.05)'); g.addColorStop(1, 'rgba(0, 0, 0, 0)');
    brushCtx.fillStyle = g; brushCtx.fillRect(0, 0, radius * 2, radius * 2);
    allHeatPoints.forEach(function(point){ ctx.drawImage(brushCanvas, point.x - radius, point.y - radius); });
    const imageData = ctx.getImageData(0, 0, canvas.width, canvas.height);
    const data = imageData.data;
    const gradientMap = createGradientMap();
    for (let i = 0; i < data.length; i += 4) {
      const alpha = data[i + 3];
      if (alpha > 0) {
        let mapIndex = Math.floor(alpha * 1.5);
        if (mapIndex > 255) mapIndex = 255;
        const cIndex = mapIndex * 4;
        data[i] = gradientMap[cIndex]; data[i + 1] = gradientMap[cIndex + 1]; data[i + 2] = gradientMap[cIndex + 2];
        data[i + 3] = Math.min(255, 150 + alpha);
      }
    }
    ctx.putImageData(imageData, 0, 0);
  }

  function createGradientMap(){
    const c = document.createElement('canvas'); c.width = 256; c.height = 1;
    const ctx = c.getContext('2d');
    const g = ctx.createLinearGradient(0, 0, 256, 0);
    g.addColorStop(0.0, 'rgba(0, 0, 255, 0)');
    g.addColorStop(0.1, 'rgba(0, 0, 255, 1)');
    g.addColorStop(0.4, 'rgba(0, 255, 255, 1)');
    g.addColorStop(0.6, 'rgba(0, 255, 0, 1)');
    g.addColorStop(0.8, 'rgba(255, 255, 0, 1)');
    g.addColorStop(1.0, 'rgba(255, 0, 0, 1)');
    ctx.fillStyle = g; ctx.fillRect(0, 0, 256, 1);
    return ctx.getImageData(0, 0, 256, 1).data;
  }

  window.addEventListener('resize', function(){
    if (canvas) {
      canvas.width = Math.max(document.documentElement.scrollWidth, document.body.scrollWidth, document.documentElement.offsetWidth);
      canvas.height = Math.max(document.documentElement.scrollHeight, document.body.scrollHeight, document.documentElement.offsetHeight);
    }
  });

  // ---- insights panel: in-page port of the extension's popup.html/popup.js ----
  const style = document.createElement('style');
  style.textContent = `
    #__insightux_mouse_panel {
      position: fixed; top: 44px; right: 16px; width: 260px; z-index: 2147483647;
      background: linear-gradient(180deg, rgba(28,24,38,0.97), rgba(20,18,26,0.97));
      border: 1px solid #3a3348; border-radius: 12px; box-shadow: 0 8px 28px rgba(0,0,0,0.45);
      font: 12px -apple-system, 'Segoe UI', Arial; color: #f0ecf7; padding: 14px;
      pointer-events: auto; display: none;
    }
    #__insightux_mouse_panel.open { display: block; }
    #__insightux_mouse_panel h3 { margin: 0 0 8px 0; font-size: 13px; color: #f0ecf7;
      display:flex; align-items:center; justify-content:space-between; }
    #__insightux_mouse_panel .muted { color: #9a92ad; font-size: 11px; }
    #__insightux_mouse_panel .row { display: flex; gap: 6px; margin-bottom: 8px; flex-wrap: wrap; }
    #__insightux_mouse_panel button {
      flex: 1 1 auto; padding: 6px 8px; font-size: 11px; border-radius: 6px; border: 1px solid #3a3348;
      background: #241f2e; color: #f0ecf7; cursor: pointer;
    }
    #__insightux_mouse_panel button:hover { background: #302a3d; }
    #__insightux_mouse_panel .timer { font-weight: 600; margin-bottom: 6px; }
    #__insightux_mouse_panel .hero {
      background: rgba(123,47,190,0.15); border: 1px solid #7B2FBE; border-radius: 8px;
      padding: 8px; text-align: center; margin-bottom: 8px;
    }
    #__insightux_mouse_panel .hero .lbl { font-size: 9px; text-transform: uppercase; color: #c9a6f5; letter-spacing: 0.05em; }
    #__insightux_mouse_panel .hero .el { font-weight: 600; margin: 3px 0; color: #FF2DF0; }
    #__insightux_mouse_panel .interest-item, #__insightux_mouse_panel .log-item {
      background: #241f2e; border-radius: 6px; padding: 5px 7px; margin-bottom: 4px; font-size: 11px;
    }
    #__insightux_mouse_panel .list { max-height: 160px; overflow-y: auto; }
  `;
  document.head.appendChild(style);

  const panel = document.createElement('div');
  panel.id = '__insightux_mouse_panel';
  panel.innerHTML = `
    <h3>Mouse Tracker <span class="muted" id="__mt_status">idle</span></h3>
    <div class="timer" id="__mt_timer">Session Time: 00:00</div>
    <div class="row">
      <button id="__mt_clear">Clear</button>
      <button id="__mt_heatmap">Heatmap</button>
      <button id="__mt_logs">Logs</button>
    </div>
    <div id="__mt_hero" class="hero" style="display:none;">
      <div class="lbl">Most Viewed Element</div>
      <div class="el" id="__mt_hero_el">-</div>
      <div id="__mt_hero_time">0s</div>
    </div>
    <div id="__mt_interests" class="list"></div>
    <div id="__mt_logs_list" class="list" style="display:none;"></div>
  `;
  document.documentElement.appendChild(panel);

  document.getElementById('__mt_clear').addEventListener('click', function(){
    trailBuffer = []; heatmapBuffer = []; clickBuffer = []; dwellBuffer = [];
    allTrail = []; allHeatmap = []; allClicks = []; dwellTotals = {};
    sessionStart = Date.now(); sessionStop = null;
    if (canvas) { document.body.removeChild(canvas); canvas = null; }
    updatePanel();
  });
  document.getElementById('__mt_heatmap').addEventListener('click', function(){ window.insightuxMouseToggleHeatmap(); });
  document.getElementById('__mt_logs').addEventListener('click', function(){
    logsOpen = !logsOpen;
    document.getElementById('__mt_logs_list').style.display = logsOpen ? 'block' : 'none';
    document.getElementById('__mt_interests').style.display = logsOpen ? 'none' : 'block';
    updatePanel();
  });

  window.insightuxMouseTogglePanel = function(){
    panelOpen = !panelOpen;
    panel.classList.toggle('open', panelOpen);
    if (panelOpen) updatePanel();
  };

  function formatTime(ms){ return Math.round(ms / 1000) + 's'; }

  function updatePanel(){
    if (!panelOpen) return;
    document.getElementById('__mt_status').textContent = isTracking ? 'tracking' : 'stopped';
    const startTs = sessionStart;
    const diff = isTracking ? Math.floor((Date.now() - startTs) / 1000)
                             : (sessionStop ? Math.floor((sessionStop - startTs) / 1000) : 0);
    const mins = Math.floor(diff / 60).toString().padStart(2, '0');
    const secs = (diff % 60).toString().padStart(2, '0');
    document.getElementById('__mt_timer').textContent = 'Session Time: ' + mins + ':' + secs;

    const interests = Object.entries(dwellTotals).map(function(e){ return { element: e[0], duration: e[1] }; })
      .sort(function(a, b){ return b.duration - a.duration; }).slice(0, 6);

    const hero = document.getElementById('__mt_hero');
    if (interests.length) {
      hero.style.display = 'block';
      document.getElementById('__mt_hero_el').textContent = interests[0].element;
      document.getElementById('__mt_hero_time').textContent = formatTime(interests[0].duration);
    } else {
      hero.style.display = 'none';
    }

    const interestsEl = document.getElementById('__mt_interests');
    if (!logsOpen) {
      if (interests.length > 1) {
        interestsEl.innerHTML = interests.slice(1).map(function(it, i){
          return '<div class="interest-item">' + (i + 2) + '. <b>' + it.element + '</b> β€” ' + formatTime(it.duration) + '</div>';
        }).join('');
      } else {
        interestsEl.innerHTML = '<div class="muted">No other interests yet.</div>';
      }
    }

    const logsEl = document.getElementById('__mt_logs_list');
    if (logsOpen) {
      if (allClicks.length) {
        logsEl.innerHTML = allClicks.slice().reverse().slice(0, 20).map(function(l){
          return '<div class="log-item"><span class="muted">' + l.timestamp + '</span><br><b>' + l.element + '</b><br>"' + l.text + '"</div>';
        }).join('');
      } else {
        logsEl.innerHTML = '<div class="muted">No clicks recorded.</div>';
      }
    }
  }
  setInterval(updatePanel, 1000);

  // ---- hotkeys: H = toggle heatmap, M = toggle insights panel ----
  function isTypingTarget(el){
    if (!el) return false;
    const tag = el.tagName ? el.tagName.toLowerCase() : '';
    return tag === 'input' || tag === 'textarea' || tag === 'select' || el.isContentEditable === true;
  }
  document.addEventListener('keydown', function(e){
    const typingBlocked = isTypingTarget(e.target) || isTypingTarget(document.activeElement);
    if (typingBlocked) return;
    if (e.key === 'h' || e.key === 'H') window.insightuxMouseToggleHeatmap();
    else if (e.key === 'm' || e.key === 'M') window.insightuxMouseTogglePanel();
  }, true);

  updatePanel();
})();
"""


# =============================================================================
# API exposed to JS β€” start/stop/quit are the entry points
# =============================================================================

# Pages tracking must never start on, checked against window.get_current_url()
# as a Python-side backstop to CHROME_JS's isTrackable check (belt and
# suspenders β€” the JS guard can't run before the JS bridge is up).
_NON_TRACKABLE_URL_MARKERS = ("insightux_landing.html", "analysis_report.html")


def _relaunch_args(mode):
    """Command to spawn calibrate.py/validate.py's logic in a fresh process.
    Frozen (PyInstaller) builds have no bundled interpreter to hand a .py
    file to, and sys.executable IS the frozen exe itself β€” so a frozen build
    relaunches itself with --mode instead. Unpackaged/dev mode still runs
    from the venv exactly as before."""
    if getattr(sys, "frozen", False):
        return [sys.executable, "--mode", mode]
    return [sys.executable, os.path.abspath(__file__), "--mode", mode]


class Api:
    def __init__(self):
        self.window = None
        self.tracking = False
        self.stop_event = threading.Event()
        self.thread = None
        self.mouse_log_f = None
        self.mouse_close_timer = None
        self.calib_proc = None
        self.validate_proc = None
        self.update_info = None

    def _notify_update(self, version, url):
        try:
            self.window.evaluate_js(
                f"window.insightuxShowUpdate && window.insightuxShowUpdate("
                f"{json.dumps(version)}, {json.dumps(url)})"
            )
        except Exception:
            pass

    def open_update_page(self, url):
        print(f"[browser_session] open_update_page() called from JS: {url}")
        import webbrowser
        try:
            webbrowser.open(url)
        except Exception as e:
            print(f"[browser_session] failed to open update url: {e}")
        return True

    def toggle_session(self):
        """Single entry point for the sidebar's Start/Stop Session button β€”
        lets the JS button stay dumb (always call the same thing) while
        Python decides which action makes sense from current state."""
        return self.stop_tracking() if self.tracking else self.start_tracking()

    def start_tracking(self):
        print("[browser_session] start_tracking() called from JS")
        if self.tracking:
            print("[browser_session]   already tracking, ignoring")
            return False
        try:
            current_url = self.window.get_current_url() or ""
        except Exception:
            current_url = ""
        if any(marker in current_url for marker in _NON_TRACKABLE_URL_MARKERS):
            print(f"[browser_session]   refusing to start on non-website page: {current_url}")
            self._set_status("Start tracking only works on a website β€” search or open a page first.")
            return False
        if self._external_proc_running():
            print("[browser_session]   refusing to start, calibration/validation still running")
            self._set_status("Wait for calibration/validation to finish before starting a session.")
            return False
        if not os.path.exists(CALIBRATION_PATH):
            print(f"[browser_session]   no {CALIBRATION_PATH} found β€” run calibrate.py first")
            self._set_status("No calibration.pkl found β€” run calibrate.py first.")
            return False

        self.tracking = True
        self.stop_event.clear()
        session_dir = os.path.join(SESSIONS_ROOT, datetime.now().strftime("%Y%m%d_%H%M%S"))
        os.makedirs(session_dir, exist_ok=True)
        print(f"[browser_session]   starting tracking thread -> {session_dir}")

        self._inject_tracking_overlay()
        self._inject_mouse_overlay()
        self._open_mouse_log(session_dir)
        self._set_mouse_tracking(True)
        self._set_session_ui(True)
        self._set_status("Recording gaze + mouse... Press E to stop, H for heatmap, M for mouse panel")

        self.thread = threading.Thread(
            target=gaze_worker, args=(self.window, self.stop_event, session_dir, self),
            daemon=True
        )
        self.thread.start()
        return True

    def stop_tracking(self):
        print("[browser_session] stop_tracking() called from JS")
        if not self.tracking:
            print("[browser_session]   not currently tracking, ignoring")
            return False
        self._set_status("Wrapping up your session...")
        self._set_mouse_tracking(False)
        self._set_session_ui(False)
        self.stop_event.set()

        # Give the page a moment to flush its last batch of mouse data over
        # log_mouse_data() before the file handle is closed.
        if self.mouse_close_timer:
            self.mouse_close_timer.cancel()
        self.mouse_close_timer = threading.Timer(2.5, self._close_mouse_log)
        self.mouse_close_timer.daemon = True
        self.mouse_close_timer.start()
        return True

    def quit_app(self):
        # X is an emergency kill switch, not a graceful shutdown β€” the user
        # wants the terminal process gone immediately, camera/threads and all.
        print("[browser_session] quit_app() called from JS -- terminating process")
        os._exit(0)

    # -- calibration / validation ------------------------------------------
    # calibrate.py and validate.py are unmodified, standalone OpenCV/pyautogui
    # scripts (their own fullscreen window, their own event loop) β€” they were
    # never meant to share a process with pywebview's GUI loop. Launching them
    # as a subprocess reuses them exactly as-is instead of rewriting their
    # display logic into the browser.

    def _external_proc_running(self):
        running = lambda p: p is not None and p.poll() is None
        return running(self.calib_proc) or running(self.validate_proc)

    def start_calibration(self):
        print("[browser_session] start_calibration() called from JS")
        if self.tracking:
            self._set_status("Stop the current session (E) before calibrating.")
            return False
        if self._external_proc_running():
            self._set_status("Calibration/validation is already running in another window.")
            return False
        try:
            self.calib_proc = subprocess.Popen(_relaunch_args("calibrate"), cwd=BASE_DIR)
        except Exception as e:
            print(f"[browser_session] failed to launch calibrate.py: {e}")
            self._set_status("Could not launch calibration β€” see terminal for details.")
            return False
        self._set_status("Calibration launched in a separate window β€” follow the dots there.")
        threading.Thread(
            target=self._watch_external_proc, args=(self.calib_proc, "Calibration"), daemon=True
        ).start()
        return True

    def run_validation(self):
        print("[browser_session] run_validation() called from JS")
        if self.tracking:
            self._set_status("Stop the current session (E) before validating.")
            return False
        if self._external_proc_running():
            self._set_status("Calibration/validation is already running in another window.")
            return False
        if not os.path.exists(CALIBRATION_PATH):
            self._set_status("No calibration.pkl found β€” run Calibrate first.")
            return False
        try:
            self.validate_proc = subprocess.Popen(_relaunch_args("validate"), cwd=BASE_DIR)
        except Exception as e:
            print(f"[browser_session] failed to launch validate.py: {e}")
            self._set_status("Could not launch validation β€” see terminal for details.")
            return False
        self._set_status("Validation launched in a separate window β€” look at each dot.")
        threading.Thread(
            target=self._watch_external_proc, args=(self.validate_proc, "Validation"), daemon=True
        ).start()
        return True

    def _watch_external_proc(self, proc, label):
        proc.wait()
        print(f"[browser_session] {label} process exited (code {proc.returncode})")
        if label == "Calibration":
            self._set_status("Calibration finished β€” check the terminal for the quality readout, then press S or Start Session.")
        else:
            self._set_status("Validation finished β€” check the terminal for the accuracy numbers.")

    def _set_session_ui(self, is_tracking):
        try:
            flag = "true" if is_tracking else "false"
            self.window.evaluate_js(
                f"window.insightuxSetSessionUI && window.insightuxSetSessionUI({flag})"
            )
        except Exception:
            pass

    def _inject_tracking_overlay(self):
        try:
            self.window.evaluate_js(TRACKING_JS)
        except Exception as e:
            print(f"[browser_session] overlay inject failed: {e}")

    def _inject_mouse_overlay(self):
        try:
            self.window.evaluate_js(MOUSE_JS)
        except Exception as e:
            print(f"[browser_session] mouse overlay inject failed: {e}")

    def _set_mouse_tracking(self, on):
        try:
            flag = "true" if on else "false"
            self.window.evaluate_js(
                f"window.insightuxMouseSetTracking && window.insightuxMouseSetTracking({flag})"
            )
        except Exception:
            pass

    def _open_mouse_log(self, session_dir):
        self._close_mouse_log()
        path = os.path.join(session_dir, "mouse_log.jsonl")
        try:
            self.mouse_log_f = open(path, "w", buffering=1)
            self.mouse_log_f.write(json.dumps({"type": "meta", "t": round(time.time(), 4)}) + "\n")
            print(f"[browser_session] writing mouse stream to {path}")
        except Exception as e:
            print(f"[browser_session] could not open mouse log: {e}")
            self.mouse_log_f = None

    def _close_mouse_log(self):
        if self.mouse_log_f:
            try:
                self.mouse_log_f.close()
                print("[browser_session] mouse log closed")
            except Exception:
                pass
            self.mouse_log_f = None

    def log_mouse_data(self, payload):
        """Called from the injected Mouse Tracker overlay (MOUSE_JS) roughly
        every 2s while tracking, with a batch of trail / heatmap / click /
        dwell records β€” same cadence as the extension's background.js sync
        loop, just persisted to mouse_log.jsonl instead of chrome.storage."""
        if not self.mouse_log_f:
            return False
        try:
            rec = dict(payload) if isinstance(payload, dict) else {}
            rec["type"] = "mouse_batch"
            rec["t"] = round(time.time(), 4)
            self.mouse_log_f.write(json.dumps(rec) + "\n")
        except Exception as e:
            print(f"[browser_session] mouse log write failed: {e}")
        return True

    def _set_status(self, text):
        try:
            self.window.evaluate_js(f"window.insightuxSetStatus && window.insightuxSetStatus({json.dumps(text)})")
        except Exception:
            pass


api = Api()


def _version_tuple(v):
    return tuple(int(p) for p in v.split(".") if p.isdigit())


def check_for_update():
    """Runs once in a background thread at startup. Publishing a new release
    is still fully manual (bump VERSION here + AppVersion in installer.iss +
    version.json, rebuild, push) β€” this only automates the *checking* side,
    per the confirmed scope. No internet / any failure here is silent:
    the user just doesn't see an update banner, never an error."""
    import urllib.request
    try:
        with urllib.request.urlopen(VERSION_CHECK_URL, timeout=4) as resp:
            data = json.loads(resp.read().decode("utf-8"))
        latest = data.get("latest", "")
        url = data.get("url", "")
        if latest and url and _version_tuple(latest) > _version_tuple(VERSION):
            api.update_info = {"version": latest, "url": url}
            api._notify_update(latest, url)
            print(f"[browser_session] update available: {VERSION} -> {latest}")
    except Exception as e:
        print(f"[browser_session] update check skipped: {e}")


def on_page_loaded(window):
    """Fires on every page load (navigation, back/forward, reload β€” not just
    the first page). The chrome must be re-injected every time since each
    navigation is a fresh document. If a session is already running, the
    gaze/mouse overlays need the same treatment or navigating mid-session
    would silently kill the dot and the mouse tracker on the new page."""
    try:
        window.evaluate_js(CHROME_JS)
    except Exception as e:
        print(f"[browser_session] chrome inject failed (will retry): {e}")

    if api.tracking:
        api._inject_tracking_overlay()
        api._inject_mouse_overlay()
        api._set_mouse_tracking(True)
        api._set_session_ui(True)

    # CHROME_JS was just re-injected fresh on this page and has no memory of
    # an update found on a previous page β€” re-tell it if one was found.
    if api.update_info:
        api._notify_update(api.update_info["version"], api.update_info["url"])


# =============================================================================
# GAZE WORKER β€” runs in a background thread, started/stopped via Api
# =============================================================================

def gaze_worker(window, stop_event, session_dir, api_ref):
    pipeline  = InsightUXPipeline(ONNX_PATH, CALIBRATION_PATH)
    face_mesh = create_face_mesh(static_image_mode=False)
    cap       = cv2.VideoCapture(0)
    logger    = GazeLogger(SCREEN_W, SCREEN_H, session_dir=session_dir)
    dom_path  = os.path.join(session_dir, "dom_log.jsonl")
    dom_f     = open(dom_path, "w", buffering=1)

    screens_dir = os.path.join(session_dir, "screenshots")
    os.makedirs(screens_dir, exist_ok=True)
    shot_count = 0
    last_shot_scroll = None
    last_shot_time = 0.0
    SCROLL_SHOT_THRESHOLD = 60   # px scrolled before a new snapshot is worth taking
    MAX_SHOT_INTERVAL      = 2.5  # seconds β€” take one anyway if you just sit still

    def maybe_capture_screenshot(scroll_y):
        """Grab a full-screen shot when the page has scrolled enough, or
        periodically even if it hasn't β€” this is what lets the report show
        the heatmap ON TOP of the actual page instead of floating in a void."""
        nonlocal shot_count, last_shot_scroll, last_shot_time
        now = time.time()
        scrolled_enough = last_shot_scroll is None or abs(scroll_y - last_shot_scroll) >= SCROLL_SHOT_THRESHOLD
        time_elapsed = (now - last_shot_time) >= MAX_SHOT_INTERVAL
        if not (scrolled_enough or time_elapsed):
            return None
        try:
            img = pyautogui.screenshot()
            # Force to the same pixel space as gaze coordinates (SCREEN_W x
            # SCREEN_H) β€” Windows DPI scaling can otherwise return a
            # screenshot at a different resolution than pyautogui.size(),
            # which would silently misalign every heatmap point.
            if img.size != (SCREEN_W, SCREEN_H):
                img = img.resize((SCREEN_W, SCREEN_H))
            shot_name = f"shot_{shot_count:04d}.png"
            img.save(os.path.join(screens_dir, shot_name))
            shot_count += 1
            last_shot_scroll = scroll_y
            last_shot_time = now
            return f"screenshots/{shot_name}"
        except Exception as e:
            print(f"[browser_session] screenshot capture failed: {e}")
            return None

    print(f"[browser_session] tracking started -> {session_dir}")

    cam_matrix    = None
    last_x        = SCREEN_W / 2
    last_y        = SCREEN_H / 2
    fil_x         = OneEuroFilter(mincutoff=EURO_MINCUTOFF, beta=EURO_BETA)
    fil_y         = OneEuroFilter(mincutoff=EURO_MINCUTOFF, beta=EURO_BETA)
    angle_smoother = GazeAngleSmoother(window=ANGLE_SMOOTH_WINDOW)
    sm_pitch = sm_yaw = sm_roll = None
    no_face_count = 0
    frame_i       = 0

    while not stop_event.is_set():
        ret, frame = cap.read()
        if not ret:
            break

        if cam_matrix is None:
            cam_matrix = estimate_camera_matrix(frame.shape)

        rgb     = cv2.cvtColor(frame, cv2.COLOR_BGR2RGB)
        results = face_mesh.process(rgb)

        if not results.multi_face_landmarks:
            no_face_count += 1
            if no_face_count >= NO_FACE_RESET:
                last_x, last_y = SCREEN_W / 2, SCREEN_H / 2
                fil_x.reset(); fil_y.reset()
                angle_smoother.reset()
                sm_pitch = sm_yaw = sm_roll = None
                no_face_count = 0
            continue
        no_face_count = 0

        lms       = results.multi_face_landmarks[0].landmark
        head_pose = estimate_head_pose(lms, frame.shape, cam_matrix)
        if head_pose is None:
            continue

        if sm_pitch is None:
            sm_pitch, sm_yaw, sm_roll = head_pose.pitch, head_pose.yaw, head_pose.roll
        else:
            b = POSE_SMOOTH
            sm_pitch = b*sm_pitch + (1-b)*head_pose.pitch
            sm_yaw   = b*sm_yaw   + (1-b)*head_pose.yaw
            sm_roll  = b*sm_roll  + (1-b)*head_pose.roll
        head_pose = replace(head_pose, pitch=sm_pitch, yaw=sm_yaw, roll=sm_roll)

        pose_vec = normalize_pose(sm_pitch, sm_yaw, sm_roll)

        # Eye aperture β€” the vertical cue. Calibration decides whether this or
        # the CNN's pitch actually tracks screen-Y, and uses the better one.
        ear_now = 0.5 * (compute_ear(lms, LEFT_EAR_INDICES, frame.shape) +
                         compute_ear(lms, RIGHT_EAR_INDICES, frame.shape))

        def get_patch(eye_idx, ear_idx, iris_idx):
            s1 = step1_normalize(frame, lms, head_pose, eye_idx, ear_idx, iris_idx)
            if not s1.is_open:
                return None
            if PATCH_SOURCE == "norm":
                return s1.norm_crop
            ir = compute_iris_radius(lms, iris_idx, frame.shape)
            s2 = step2_illumination(s1, ir)
            return s2.blended if s2.is_usable else None

        left_patch  = get_patch(LEFT_EYE_INDICES,  LEFT_EAR_INDICES,  LEFT_IRIS_INDICES)
        right_patch = get_patch(RIGHT_EYE_INDICES, RIGHT_EAR_INDICES, RIGHT_IRIS_INDICES)
        if left_patch is None and right_patch is None:
            continue
        if left_patch  is None: left_patch  = right_patch
        if right_patch is None: right_patch = left_patch

        _, _, raw_pitch, raw_yaw = pipeline.predict_gaze_vector(left_patch, pose_vec, right_patch)
        pitch = compensate_pitch(raw_pitch, sm_pitch)
        yaw   = compensate_yaw(raw_yaw, sm_yaw)

        # Smooth the ANGLES before the RBF sees them. The RBF amplifies input
        # noise (steep gradient along the low-variance pitch axis), so noise
        # must be removed here β€” smoothing the screen coords afterwards cannot
        # undo amplification that already happened through a nonlinear map.
        pitch, yaw, ear_s = angle_smoother(pitch, yaw, ear_now)

        sx, sy = pipeline.calibration.predict(pitch, yaw, ear_s)
        sx = max(0.0, min(sx, SCREEN_W))
        sy = max(0.0, min(sy, SCREEN_H))

        jump = np.hypot(sx - last_x, sy - last_y)
        if jump > MAX_JUMP:
            sx = last_x + (sx - last_x) * 0.3
            sy = last_y + (sy - last_y) * 0.3
        last_x, last_y = sx, sy

        logger.log(sx, sy)

        now = time.time()
        fx = fil_x(sx / SCREEN_W, now)
        fy = fil_y(sy / SCREEN_H, now)
        try:
            window.evaluate_js(f"window.insightuxUpdate({fx:.5f},{fy:.5f})")
        except Exception:
            pass

        frame_i += 1
        if frame_i % DOM_EVERY == 0:
            try:
                raw = window.evaluate_js("window.insightuxAOIs()")
                if raw:
                    rec = json.loads(raw)
                    rec["type"] = "dom"
                    rec["t"] = round(time.time(), 4)
                    shot_ref = maybe_capture_screenshot(rec.get("scrollY", 0))
                    if shot_ref:
                        rec["screenshot"] = shot_ref
                    dom_f.write(json.dumps(rec) + "\n")
            except Exception:
                pass

        time.sleep(0.005)

    cap.release()
    logger.close()
    dom_f.close()
    print(f"[browser_session] session saved in {session_dir}")

    # generate report and hand control back
    try:
        report_path = generate_report(session_dir)
        api_ref.tracking = False
        window.load_url("file://" + report_path)
        print(f"[browser_session] report ready -> {report_path}")
    except Exception as e:
        print(f"[browser_session] report generation failed: {e}")
        api_ref.tracking = False


# =============================================================================
# MAIN
# =============================================================================

def _go_fullscreen(window):
    # Small delay so WebView2 fully initializes and grabs keyboard focus
    # BEFORE going fullscreen. Passing fullscreen=True straight to
    # create_window is what causes the "can't type" issue and the
    # AccessibilityObject.Bounds recursion crash on Windows β€” both trace
    # back to the WinForms host not finishing its focus/accessibility
    # setup before the fullscreen transition happens.
    time.sleep(0.4)
    try:
        window.toggle_fullscreen()
    except Exception as e:
        print(f"[browser_session] fullscreen toggle failed: {e}")


if __name__ == "__main__":
    # A packaged build is one exe with no separate calibrate.py/validate.py
    # files to shell out to β€” --mode makes it a single self-dispatching
    # entry point instead. See _relaunch_args() above for the launch side.
    import argparse
    parser = argparse.ArgumentParser()
    parser.add_argument("--mode", choices=["browser", "calibrate", "validate"], default="browser")
    args = parser.parse_args()

    if args.mode == "calibrate":
        import calibrate
        calibrate.main()
    elif args.mode == "validate":
        import validate
        validate.main()
    else:
        os.makedirs(SESSIONS_ROOT, exist_ok=True)

        window = webview.create_window(
            "InsightUX β€” Eye-Tracking Research Browser", LANDING_URL,
            width=SCREEN_W, height=SCREEN_H, js_api=api
        )
        api.window = window
        window.events.loaded += lambda: on_page_loaded(window)
        window.events.shown += lambda: threading.Thread(
            target=_go_fullscreen, args=(window,), daemon=True
        ).start()
        threading.Thread(target=check_for_update, daemon=True).start()

        # debug=True enables right-click > Inspect (DevTools) so you can see
        # the [insightux] console.log diagnostics from CHROME_JS directly β€”
        # keyboard handling runs entirely in the browser, so JS-side issues
        # never show up in this terminal, only in DevTools' Console tab.
        webview.start(debug=True)