aboutsummaryrefslogtreecommitdiff
path: root/static/bs/bracket.js
blob: 59167b941a60c5f0dac96bd50233796ab2b2259f (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
1289
1290
1291
1292
1293
1294
1295
1296
1297
1298
1299
1300
1301
1302
1303
1304
1305
1306
1307
1308
1309
1310
1311
1312
1313
1314
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
1339
1340
1341
1342
1343
1344
1345
1346
1347
1348
1349
1350
1351
1352
1353
1354
1355
1356
1357
1358
1359
1360
1361
1362
1363
1364
1365
1366
1367
1368
1369
1370
1371
1372
1373
1374
1375
1376
1377
1378
1379
1380
1381
1382
1383
1384
1385
1386
1387
1388
1389
1390
1391
1392
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
1420
1421
1422
1423
1424
1425
1426
1427
1428
1429
1430
1431
1432
1433
1434
1435
1436
1437
1438
1439
1440
1441
1442
1443
1444
1445
1446
1447
1448
1449
1450
1451
1452
1453
1454
1455
1456
1457
1458
1459
1460
1461
1462
1463
1464
1465
1466
1467
1468
1469
1470
1471
1472
1473
1474
1475
1476
1477
1478
1479
1480
1481
1482
1483
1484
1485
1486
1487
1488
1489
1490
1491
1492
1493
1494
1495
1496
1497
1498
1499
1500
1501
1502
1503
1504
1505
1506
1507
1508
1509
1510
1511
1512
1513
1514
1515
1516
1517
1518
1519
1520
1521
1522
1523
1524
1525
1526
1527
1528
1529
1530
1531
1532
1533
1534
1535
1536
1537
1538
1539
1540
1541
1542
1543
1544
1545
1546
1547
1548
1549
1550
1551
1552
1553
1554
1555
1556
1557
1558
1559
1560
1561
1562
1563
1564
1565
1566
1567
1568
1569
1570
1571
1572
1573
1574
1575
1576
1577
1578
1579
1580
1581
1582
1583
1584
1585
1586
1587
1588
1589
1590
1591
1592
1593
1594
1595
1596
1597
1598
1599
1600
1601
1602
1603
1604
1605
1606
1607
1608
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
1624
1625
1626
1627
1628
1629
1630
1631
1632
1633
1634
1635
1636
1637
1638
1639
1640
1641
1642
1643
1644
1645
1646
1647
1648
1649
1650
1651
1652
1653
1654
1655
1656
1657
1658
1659
1660
1661
1662
1663
1664
1665
1666
1667
1668
1669
1670
1671
1672
1673
1674
1675
1676
1677
1678
1679
1680
1681
1682
1683
1684
1685
1686
1687
1688
1689
1690
1691
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
1708
1709
1710
1711
1712
1713
1714
1715
1716
1717
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
1749
1750
1751
1752
1753
1754
1755
1756
1757
1758
1759
1760
1761
1762
1763
1764
1765
1766
1767
1768
1769
1770
1771
1772
1773
1774
1775
1776
1777
1778
1779
1780
1781
1782
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
1801
1802
1803
1804
1805
1806
1807
1808
1809
1810
1811
1812
1813
1814
1815
1816
1817
1818
1819
1820
1821
1822
1823
1824
1825
1826
1827
1828
1829
1830
1831
1832
1833
1834
1835
1836
1837
1838
1839
1840
1841
1842
1843
1844
1845
1846
1847
1848
1849
1850
1851
1852
1853
1854
1855
1856
1857
1858
1859
1860
1861
1862
1863
1864
1865
1866
1867
1868
1869
1870
1871
1872
1873
1874
1875
1876
1877
1878
1879
1880
1881
1882
1883
1884
1885
1886
1887
1888
1889
1890
1891
1892
1893
1894
1895
1896
1897
1898
1899
1900
1901
1902
1903
1904
1905
1906
1907
1908
1909
1910
1911
1912
1913
1914
1915
1916
1917
1918
1919
1920
1921
1922
1923
1924
1925
1926
1927
1928
1929
1930
1931
1932
1933
1934
1935
1936
1937
1938
1939
1940
1941
1942
1943
1944
1945
1946
1947
1948
1949
1950
1951
1952
1953
1954
1955
1956
1957
1958
1959
1960
1961
1962
1963
1964
1965
1966
1967
1968
1969
1970
1971
1972
1973
1974
1975
1976
1977
1978
1979
1980
1981
1982
1983
1984
1985
1986
1987
1988
1989
1990
1991
1992
1993
1994
1995
1996
1997
1998
1999
2000
2001
2002
2003
2004
2005
2006
2007
2008
2009
2010
2011
2012
2013
2014
2015
2016
2017
2018
2019
2020
2021
2022
2023
2024
2025
2026
2027
2028
2029
2030
2031
2032
2033
2034
2035
2036
2037
2038
2039
2040
2041
2042
2043
2044
2045
2046
2047
2048
2049
2050
2051
2052
2053
2054
2055
2056
2057
2058
2059
2060
2061
2062
2063
2064
2065
2066
2067
2068
2069
2070
2071
2072
2073
2074
2075
2076
2077
2078
2079
2080
2081
2082
2083
2084
2085
2086
2087
2088
2089
2090
2091
2092
2093
2094
2095
2096
2097
2098
2099
2100
2101
2102
2103
2104
2105
2106
2107
2108
2109
2110
2111
2112
2113
2114
2115
2116
2117
2118
2119
2120
2121
2122
2123
2124
2125
2126
2127
2128
2129
2130
2131
2132
2133
2134
2135
2136
2137
2138
2139
2140
2141
2142
2143
2144
2145
2146
2147
2148
2149
2150
2151
2152
2153
2154
2155
2156
2157
2158
2159
2160
2161
2162
2163
2164
2165
2166
2167
2168
2169
2170
2171
2172
2173
2174
2175
2176
2177
2178
2179
2180
2181
2182
2183
2184
2185
2186
2187
2188
2189
2190
2191
2192
2193
2194
2195
2196
2197
2198
2199
2200
2201
2202
2203
2204
2205
2206
2207
2208
2209
2210
2211
2212
2213
2214
2215
2216
2217
2218
2219
2220
2221
2222
2223
2224
2225
2226
2227
2228
2229
2230
2231
2232
2233
2234
2235
2236
2237
2238
2239
2240
2241
2242
2243
2244
2245
2246
2247
2248
2249
2250
2251
2252
2253
2254
2255
2256
2257
2258
2259
2260
2261
2262
2263
2264
2265
2266
2267
2268
2269
2270
2271
2272
2273
2274
2275
2276
2277
2278
2279
2280
2281
2282
2283
2284
2285
2286
2287
2288
2289
2290
2291
2292
2293
2294
2295
2296
2297
2298
2299
2300
2301
2302
2303
2304
2305
2306
2307
2308
2309
2310
2311
2312
2313
2314
2315
2316
2317
2318
2319
2320
2321
2322
2323
2324
2325
2326
2327
2328
2329
2330
2331
2332
2333
2334
2335
2336
2337
2338
2339
2340
2341
2342
2343
2344
2345
2346
2347
2348
2349
2350
2351
2352
2353
2354
2355
2356
2357
2358
2359
2360
2361
2362
2363
2364
2365
2366
2367
2368
2369
2370
2371
2372
2373
2374
2375
2376
2377
2378
2379
2380
2381
2382
2383
2384
2385
2386
2387
2388
2389
2390
2391
2392
2393
2394
2395
2396
2397
2398
2399
2400
2401
2402
2403
2404
2405
2406
2407
2408
2409
2410
2411
2412
2413
2414
2415
2416
2417
2418
2419
2420
2421
2422
2423
2424
2425
2426
2427
2428
2429
2430
2431
2432
2433
2434
2435
2436
2437
2438
2439
2440
2441
2442
2443
2444
2445
2446
2447
2448
2449
2450
2451
2452
2453
2454
2455
2456
2457
2458
2459
2460
2461
2462
2463
2464
2465
2466
2467
2468
2469
2470
2471
2472
2473
2474
2475
2476
2477
2478
2479
2480
2481
2482
2483
2484
2485
2486
2487
2488
2489
2490
2491
2492
2493
2494
2495
2496
2497
2498
2499
2500
2501
2502
2503
2504
2505
2506
2507
2508
2509
2510
2511
2512
2513
2514
2515
2516
2517
2518
2519
2520
2521
2522
2523
2524
2525
2526
2527
2528
2529
2530
2531
2532
2533
2534
2535
2536
2537
2538
2539
2540
2541
2542
2543
2544
2545
2546
2547
2548
2549
2550
2551
2552
2553
2554
2555
2556
2557
2558
2559
2560
2561
2562
2563
2564
2565
2566
2567
2568
2569
2570
2571
2572
2573
2574
2575
2576
2577
2578
2579
2580
2581
2582
2583
2584
2585
2586
2587
2588
2589
2590
2591
2592
2593
2594
2595
2596
2597
2598
2599
2600
2601
2602
2603
2604
2605
2606
2607
2608
2609
2610
2611
2612
2613
2614
2615
2616
2617
2618
2619
2620
2621
2622
2623
2624
2625
2626
2627
2628
2629
2630
2631
2632
2633
2634
2635
2636
2637
2638
2639
2640
2641
2642
2643
2644
2645
2646
2647
2648
2649
2650
2651
2652
2653
2654
2655
2656
2657
2658
2659
2660
2661
2662
2663
2664
2665
2666
2667
2668
2669
2670
2671
2672
2673
2674
2675
2676
2677
2678
2679
2680
2681
2682
2683
2684
2685
2686
2687
2688
2689
2690
2691
2692
2693
2694
2695
2696
2697
2698
2699
2700
2701
2702
2703
2704
2705
2706
2707
2708
2709
2710
2711
2712
2713
2714
2715
2716
2717
2718
2719
2720
2721
2722
2723
2724
2725
2726
2727
2728
2729
2730
2731
2732
2733
2734
2735
2736
2737
2738
2739
2740
2741
2742
2743
2744
2745
2746
2747
2748
2749
2750
2751
2752
2753
2754
2755
2756
2757
2758
2759
2760
2761
2762
2763
2764
2765
2766
2767
2768
2769
2770
2771
2772
2773
2774
2775
2776
2777
2778
2779
2780
2781
2782
2783
2784
2785
2786
2787
2788
2789
2790
2791
2792
2793
2794
2795
2796
2797
2798
2799
2800
2801
2802
2803
2804
2805
2806
2807
2808
2809
2810
2811
2812
2813
2814
2815
2816
2817
2818
2819
2820
2821
2822
2823
2824
2825
2826
2827
2828
2829
2830
2831
2832
2833
2834
2835
2836
2837
2838
2839
2840
2841
2842
2843
2844
2845
2846
2847
2848
2849
2850
2851
2852
2853
2854
2855
2856
2857
2858
2859
2860
2861
2862
2863
2864
2865
2866
2867
2868
2869
2870
2871
2872
2873
2874
2875
2876
2877
2878
2879
2880
2881
2882
2883
2884
2885
2886
2887
2888
2889
2890
2891
2892
2893
2894
2895
2896
2897
2898
2899
2900
2901
2902
2903
2904
2905
2906
2907
2908
2909
2910
2911
2912
2913
2914
2915
2916
2917
2918
2919
2920
2921
2922
2923
2924
2925
2926
2927
2928
2929
2930
2931
2932
2933
2934
2935
2936
2937
2938
2939
2940
2941
2942
2943
2944
2945
2946
2947
2948
2949
2950
2951
2952
2953
2954
2955
2956
2957
2958
2959
2960
2961
2962
2963
2964
2965
2966
2967
2968
2969
2970
2971
2972
2973
2974
2975
2976
2977
2978
2979
2980
2981
2982
2983
2984
2985
2986
2987
2988
2989
2990
2991
2992
2993
2994
2995
2996
2997
2998
2999
3000
3001
3002
3003
3004
3005
3006
3007
3008
3009
3010
3011
3012
3013
3014
3015
3016
3017
3018
3019
3020
3021
3022
3023
3024
3025
3026
3027
3028
3029
3030
3031
3032
3033
3034
3035
3036
3037
3038
3039
3040
3041
3042
3043
3044
3045
3046
3047
3048
3049
3050
3051
3052
3053
3054
3055
3056
3057
3058
3059
3060
3061
3062
3063
3064
3065
3066
3067
3068
3069
3070
3071
3072
3073
3074
3075
3076
3077
3078
3079
3080
3081
3082
3083
3084
3085
3086
3087
3088
3089
3090
3091
3092
3093
3094
3095
3096
3097
3098
3099
3100
3101
3102
3103
3104
3105
3106
3107
3108
3109
3110
3111
3112
3113
3114
3115
3116
3117
3118
3119
3120
3121
3122
3123
3124
3125
3126
3127
3128
3129
3130
3131
3132
3133
3134
3135
3136
3137
3138
3139
3140
3141
3142
3143
3144
3145
3146
3147
3148
3149
3150
3151
3152
3153
3154
3155
3156
3157
3158
3159
3160
3161
3162
3163
3164
3165
3166
3167
3168
3169
3170
3171
3172
3173
3174
3175
3176
3177
3178
3179
3180
3181
3182
3183
3184
3185
3186
3187
3188
3189
3190
3191
3192
3193
3194
3195
3196
3197
3198
3199
3200
3201
3202
3203
3204
3205
3206
3207
3208
3209
3210
3211
3212
3213
3214
3215
3216
3217
3218
3219
3220
3221
3222
3223
3224
3225
3226
3227
3228
3229
3230
3231
3232
3233
3234
3235
3236
3237
3238
3239
3240
3241
3242
3243
3244
3245
3246
3247
3248
3249
3250
3251
3252
3253
3254
3255
3256
3257
3258
3259
3260
3261
3262
3263
3264
3265
3266
3267
3268
3269
3270
3271
3272
3273
3274
3275
3276
3277
3278
3279
3280
3281
3282
3283
3284
3285
3286
3287
3288
3289
3290
3291
3292
3293
3294
3295
3296
3297
3298
3299
3300
3301
3302
3303
3304
3305
3306
3307
3308
3309
3310
3311
3312
3313
3314
3315
3316
3317
3318
3319
3320
3321
3322
3323
3324
3325
3326
3327
3328
3329
3330
3331
3332
3333
3334
3335
3336
3337
3338
3339
3340
3341
3342
3343
3344
3345
3346
3347
3348
3349
3350
3351
3352
3353
3354
3355
3356
3357
3358
3359
3360
3361
3362
3363
3364
3365
3366
3367
3368
3369
3370
3371
3372
3373
3374
3375
3376
3377
3378
3379
3380
3381
3382
3383
3384
3385
3386
3387
3388
3389
3390
3391
3392
3393
3394
3395
3396
3397
3398
3399
3400
3401
3402
3403
3404
3405
3406
3407
3408
3409
3410
3411
3412
3413
3414
3415
3416
3417
3418
3419
3420
3421
3422
3423
3424
3425
3426
3427
3428
3429
3430
3431
3432
3433
3434
3435
3436
3437
3438
3439
3440
3441
3442
3443
3444
3445
3446
3447
3448
3449
3450
3451
3452
3453
3454
3455
3456
3457
3458
3459
3460
3461
3462
3463
3464
3465
3466
3467
3468
3469
3470
3471
3472
3473
3474
3475
3476
3477
3478
3479
3480
3481
3482
3483
3484
3485
3486
3487
3488
3489
3490
3491
3492
3493
3494
3495
3496
3497
3498
3499
3500
3501
3502
3503
3504
3505
3506
3507
3508
3509
3510
3511
3512
3513
3514
3515
3516
3517
3518
3519
3520
3521
3522
3523
3524
3525
3526
3527
3528
3529
3530
3531
3532
3533
3534
3535
3536
3537
3538
3539
3540
3541
3542
3543
3544
3545
3546
3547
3548
3549
3550
3551
3552
3553
3554
3555
3556
3557
3558
3559
3560
3561
3562
3563
3564
3565
3566
3567
3568
3569
3570
3571
3572
3573
3574
3575
3576
3577
3578
3579
3580
3581
3582
3583
3584
3585
3586
3587
3588
3589
3590
3591
3592
3593
3594
3595
3596
3597
3598
3599
3600
3601
3602
3603
3604
3605
3606
3607
3608
3609
3610
3611
3612
3613
3614
3615
3616
3617
3618
3619
3620
3621
3622
3623
3624
3625
3626
3627
3628
3629
3630
3631
3632
3633
3634
3635
3636
3637
3638
3639
3640
3641
3642
3643
3644
3645
3646
3647
3648
3649
3650
3651
3652
3653
3654
3655
3656
3657
3658
3659
3660
3661
3662
3663
3664
3665
3666
3667
3668
3669
3670
3671
3672
3673
3674
3675
3676
3677
3678
3679
3680
3681
3682
3683
3684
3685
3686
3687
3688
3689
3690
3691
3692
3693
3694
3695
3696
3697
3698
3699
3700
3701
3702
3703
3704
3705
3706
3707
3708
3709
3710
3711
3712
3713
3714
3715
3716
3717
3718
3719
3720
3721
3722
3723
3724
3725
3726
3727
3728
3729
3730
3731
3732
3733
3734
3735
3736
3737
3738
3739
3740
3741
3742
3743
3744
3745
3746
3747
3748
3749
3750
3751
3752
3753
3754
3755
3756
3757
3758
3759
3760
3761
3762
3763
3764
3765
3766
3767
3768
3769
3770
3771
3772
3773
3774
3775
3776
3777
3778
3779
3780
3781
3782
3783
3784
3785
3786
3787
3788
3789
3790
3791
3792
3793
3794
3795
3796
3797
3798
3799
3800
3801
3802
3803
3804
3805
3806
3807
3808
3809
3810
3811
3812
3813
3814
3815
3816
3817
3818
3819
3820
3821
3822
3823
3824
3825
3826
3827
3828
3829
3830
3831
3832
3833
3834
3835
3836
3837
3838
3839
3840
3841
3842
3843
3844
3845
3846
3847
3848
3849
3850
3851
3852
3853
3854
3855
3856
3857
3858
3859
3860
3861
3862
3863
3864
3865
3866
3867
3868
3869
3870
3871
3872
3873
3874
3875
3876
3877
3878
3879
3880
3881
3882
3883
3884
3885
3886
3887
3888
3889
3890
3891
3892
3893
3894
3895
3896
3897
3898
3899
3900
3901
3902
3903
3904
3905
3906
3907
3908
3909
3910
3911
3912
3913
3914
3915
3916
3917
3918
3919
3920
3921
3922
3923
3924
3925
3926
3927
3928
3929
3930
3931
3932
3933
3934
3935
3936
3937
3938
3939
3940
3941
3942
3943
3944
3945
3946
3947
3948
3949
3950
3951
3952
3953
3954
3955
3956
3957
3958
3959
3960
3961
3962
3963
3964
3965
3966
3967
3968
3969
3970
3971
3972
3973
3974
3975
3976
3977
3978
3979
3980
3981
3982
3983
3984
3985
3986
3987
3988
3989
3990
3991
3992
3993
3994
3995
3996
3997
3998
3999
4000
4001
4002
4003
4004
4005
4006
4007
4008
4009
4010
4011
4012
4013
4014
4015
4016
4017
4018
4019
4020
4021
4022
4023
4024
4025
4026
4027
4028
4029
4030
4031
4032
4033
4034
4035
4036
4037
4038
4039
4040
4041
4042
4043
4044
4045
4046
4047
4048
4049
4050
4051
4052
4053
4054
4055
4056
4057
4058
4059
4060
4061
4062
4063
4064
4065
4066
4067
4068
4069
4070
4071
class BracketCityRank {
  /**
   * @typedef {Object} RankConfig
   * @property {number} peekPenalty - Points deducted per peek
   * @property {number} megaPeekPenalty - Points deducted per mega peek
   * @property {number} wrongGuessPenalty - Points deducted per wrong guess
   * @property {Object.<string, number>} rankThresholds - Score thresholds for each rank
   */
  
  /**
   * @param {Partial<RankConfig>} config - Optional configuration overrides
   */
  constructor(config = {}) {
    // Define rank emojis with consistent Unicode escapes
    this.rankEmojis = {
      'Tourist': '\u{1F4F8}',      // Camera with flash
      'Commuter': '\u{1F682}',     // Steam locomotive
      'Resident': '\u{1F3E0}',     // House
      'Council Member': '\u{1F3DB}', // Classical building
      'Chief of Police': '\u{1F46E}', // Police officer
      'Mayor': '\u{1F396}',        // Military medal
      'Power Broker': '\u{1F4BC}', // Briefcase
      'Kingmaker': '\u{1F451}',    // Crown
      'Puppet Master': '\u{1F52E}'  // Crystal ball
    };
  
    // Default configuration
    const defaultConfig = {
      peekPenalty: 5,
      megaPeekPenalty: 15,
      wrongGuessPenalty: 2, // For regular mode
      rankThresholds: {
        'Tourist': 0,
        'Commuter': 11,
        'Resident': 21,
        'Council Member': 31,
        'Chief of Police': 51,
        'Mayor': 68,
        'Power Broker': 79,
        'Kingmaker': 91,
        'Puppet Master': 100
      },
      regularRankThresholds: {
        'Tourist': 0,
        'Commuter': 10,
        'Resident': 20,
        'Council Member': 45,
        'Chief of Police': 65,
        'Mayor': 80,
        'Power Broker': 90,
        'Kingmaker': 100,
        'Puppet Master': 100
      }
    };
  
    // Merge provided config with defaults
    this.config = {
      ...defaultConfig,
      ...config,
      // Ensure rankThresholds is fully merged
      rankThresholds: {
        ...defaultConfig.rankThresholds,
        ...(config.rankThresholds || {})
      },
      regularRankThresholds: {
        ...defaultConfig.regularRankThresholds,
        ...(config.regularRankThresholds || {})
      }
    };
  }
  
  /**
   * Modified calculateScore method to account for different scoring systems
   * @param {number} efficiency - Base efficiency score (0-100)
   * @param {number} peekCount - Number of peeks used
   * @param {number} megaPeekCount - Number of mega peeks used
   * @param {number} wrongGuessCount - Number of wrong guesses
   * @returns {Object} Detailed score breakdown
   */
  calculateScore(efficiency, peekCount = 0, megaPeekCount = 0, wrongGuessCount = 0) {
    // Always ensure inputs are numbers and non-negative
    const safepeekCount = Math.max(0, Number(peekCount) || 0);
    const safeMegaPeekCount = Math.max(0, Number(megaPeekCount) || 0);
    const safeWrongGuessCount = Math.max(0, Number(wrongGuessCount) || 0);
    
    // Calculate penalties
    const peekPenalty = safepeekCount * this.config.peekPenalty;
    const megaPeekPenalty = safeMegaPeekCount * this.config.megaPeekPenalty;
    
    // Different base score calculation based on mode
    let baseScore, wrongGuessPenalty, totalPenalty, finalScore;
    
    if (this.inputMode === 'classic') {
      // Classic mode: score based on keystroke efficiency
      baseScore = Math.max(0, Number(efficiency) || 0);
      wrongGuessPenalty = 0; // No wrong guess penalty in classic mode
      totalPenalty = peekPenalty + megaPeekPenalty;
    } else {
      // Submit mode: score starts at 100, penalty for wrong guesses
      baseScore = 100; // Start at max
      wrongGuessPenalty = safeWrongGuessCount * this.config.wrongGuessPenalty;
      totalPenalty = peekPenalty + megaPeekPenalty + wrongGuessPenalty;
    }
    
    // Calculate final score (capped at 100)
    finalScore = Math.min(100, Math.max(0, baseScore - totalPenalty));
    
    return {
      baseScore,
      peekPenalty,
      megaPeekPenalty,
      wrongGuessPenalty,
      totalPenalty,
      finalScore
    };
  }

  /**
   * Generates detailed stats including rank and scoring information
   * Accounts for different modes
   * @param {number} efficiency - Base efficiency score
   * @param {number} peekCount - Number of peeks used
   * @param {number} megaPeekCount - Number of mega peeks used
   * @param {number} wrongGuessCount - Number of wrong guesses
   * @returns {Object} Detailed stats object
   */
  getDetailedStats(efficiency, peekCount = 0, megaPeekCount = 0, wrongGuessCount = 0) {
    const scoreDetails = this.calculateScore(efficiency, peekCount, megaPeekCount, wrongGuessCount);
    const rank = this.calculateRank(efficiency, peekCount, megaPeekCount, wrongGuessCount);
    
    // Calculate points needed for next rank
    const thresholds = this.inputMode === 'classic' ? 
      this.config.rankThresholds : 
      this.config.regularRankThresholds;
    
    const ranks = Object.entries(thresholds)
      .sort((a, b) => a[1] - b[1]); // Sort ascending
    
    const currentRankIndex = ranks.findIndex(([r]) => r === rank);
    const nextRank = currentRankIndex < ranks.length - 1 ? 
      ranks[currentRankIndex + 1] : null;
    
    const pointsToNextRank = nextRank ? 
      Math.max(0, nextRank[1] - scoreDetails.finalScore) : 0;
    
    return {
      rank,
      rankEmoji: this.rankEmojis[rank] || this.rankEmojis['Tourist'],
      ...scoreDetails,
      nextRankName: nextRank ? nextRank[0] : null,
      pointsToNextRank: Math.ceil(pointsToNextRank), // Round up for user-friendly display
      config: this.config
    };
  }
  
  /**
   * Determines rank based on final score
   * @param {number} efficiency - Base efficiency score
   * @param {number} peekCount - Number of peeks used
   * @param {number} megaPeekCount - Number of mega peeks used
   * @param {number} wrongGuessCount - Number of wrong guesses
   * @returns {string} Rank title
   */
  calculateRank(efficiency, peekCount = 0, megaPeekCount = 0, wrongGuessCount = 0) {
    const { finalScore } = this.calculateScore(efficiency, peekCount, megaPeekCount, wrongGuessCount);
    
    // Only keep the Puppet Master special case
    // Puppet Master: 100% keystroke-efficient (no excess keystrokes) AND no peeks/wrong guesses
    const isPerfect = efficiency === 100;
    const isNoHelp = peekCount === 0 && megaPeekCount === 0 && wrongGuessCount === 0;
    
    if (isPerfect && isNoHelp) {
      return 'Puppet Master';
    }
    
    // For all other cases, follow normal threshold logic
    const thresholds = this.inputMode === 'classic' ? 
      this.config.rankThresholds : 
      this.config.regularRankThresholds;
    
    const ranks = Object.entries(thresholds)
      .sort((a, b) => b[1] - a[1]);
    
    // Find highest rank threshold that score exceeds
    const rank = ranks.find(([_, threshold]) => finalScore >= threshold);
    return rank ? rank[0] : 'Tourist';
  }
}
  
  /**
   * @typedef {Object} GameState
   * @property {string} displayState - Current puzzle display with HTML markup
   * @property {Set<string>} solvedExpressions - Set of solved expression strings
   * @property {string} message - Current user message
   * @property {number} totalKeystrokes - Total keystrokes used
   * @property {number} minimumKeystrokes - Minimum keystrokes needed
   * @property {Array<{start: number, end: number, text: string, expression: string}>} activeClues
   * @property {Set<string>} hintModeClues - Clues that have been peeked
   * @property {Set<string>} peekedClues - Tracking for peek penalties
   * @property {Set<string>} megaPeekedClues - Clues that were fully revealed
   */
  
  
  class BracketPuzzle {

  /****************************
   Core initilization methods
  ****************************/

  /**
   * Constructor and Initialization
   */

  constructor(rootElement) {
    this.root = rootElement;
    this.API_ENDPOINT = 'https://user.4574.co.uk/bracket-api';
    this.PUZZLE_DATA = null;
    
    this.MODE_STORAGE_KEY = 'bracketCityInputMode';
    this.inputMode = this.getInputMode();

    // Detect device type
    this.isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
    
    // Add new property specifically for input method decision
    this.useCustomKeyboard = this.isMobile && !this.isTablet();

    // Track keyboard layout
    this.isAltKeyboard = false;

    // Initialize user ID
    this.userId = this.initializeUserId();

    // Initialize empty state immediately
    this.state = {
      displayState: '',
      solvedExpressions: new Set(),
      message: '',
      totalKeystrokes: 0,
      minimumKeystrokes: 0,
      activeClues: [],
      hintModeClues: new Set(),
      peekedClues: new Set(),
      megaPeekedClues: new Set(),
      wrongGuesses: 0
    };
    
    // Initialize rank calculator
    this.rankCalculator = new BracketCityRank();
    this.rankCalculator.inputMode = this.inputMode;

    // Start initialization
    this.initializeGame();
  }

  /**
   * Initializes the game
   */
  async initializeGame() {
    try {
        console.log('Starting game initialization');
        
        // Show loading state immediately
        this.root.innerHTML = this.generateLoadingHTML();
        
        // Get initial puzzle date, checking encoded puzzles first
        const urlParams = new URLSearchParams(window.location.search);
        const encoded = urlParams.get('d');
        
        if (encoded) {
            const decoded = PuzzleEncoder.decodePuzzle(encoded);
            if (decoded) {
                this.currentPuzzleDate = decoded.puzzleDate;
            } else {
                this.currentPuzzleDate = this.getPuzzleDateFromURL();
            }
        } else {
            this.currentPuzzleDate = this.getPuzzleDateFromURL();
        }
        
        console.log('Current puzzle date set to:', this.currentPuzzleDate);

        // Load saved input mode preference
        this.inputMode = this.getInputMode();
        
        // Update rankCalculator with the input mode
        this.rankCalculator = new BracketCityRank();
        this.rankCalculator.inputMode = this.inputMode;

        // Start showing basic UI elements
        this.root.innerHTML = this.generateInitialHTML();
        this.setupInputHandlers();

        // Load puzzle data
        console.log('Loading puzzle data');
        await this.initializePuzzleData();
        
        if (!this.PUZZLE_DATA) {
            throw new Error('Failed to initialize puzzle data');
        }

        // Load or initialize game state
        console.log('Loading saved state');
        const savedState = await this.loadSavedState(this.currentPuzzleDate);
        console.log('Saved state loaded:', savedState ? 'found' : 'not found');

        // Initialize game state
        if (savedState && savedState.initialPuzzle === this.PUZZLE_DATA.initialPuzzle) {
            console.log('Using saved state');
            this.updateGameState({
                displayState: savedState.displayState,
                solvedExpressions: new Set(savedState.solvedExpressions),
                message: savedState.message || '',
                totalKeystrokes: savedState.totalKeystrokes,
                minimumKeystrokes: savedState.minimumKeystrokes,
                activeClues: this.findActiveClues(savedState.displayState),
                hintModeClues: new Set(savedState.hintModeClues || []),
                peekedClues: new Set(savedState.peekedClues || []),
                megaPeekedClues: new Set(savedState.megaPeekedClues || []),
                wrongGuesses: savedState.wrongGuesses || 0 
            });
        } else {
            console.log('Initializing fresh state');
            this.updateGameState({
                displayState: this.PUZZLE_DATA.initialPuzzle,
                solvedExpressions: new Set(),
                message: '',
                totalKeystrokes: 0,
                minimumKeystrokes: this.calculateMinimumKeystrokes(),
                activeClues: this.findActiveClues(this.PUZZLE_DATA.initialPuzzle),
                hintModeClues: new Set(),
                peekedClues: new Set(),
                megaPeekedClues: new Set(),
                wrongGuesses: 0
            });
        }

        this.setupDatePicker();

        // Update URL to match current puzzle
        if (!encoded) {  // Only update URL if not an encoded puzzle
            this.updateURL(this.currentPuzzleDate);
        }

        // Do the initial render
        console.log('Performing initial render');
        this.render();

        // Load non-critical features
        requestAnimationFrame(() => {
            console.log('Setting up auxiliary features');
            this.setupShareButtons();
            this.setupPuzzleDisplay();
            
            this.checkAdjacentPuzzles(this.currentPuzzleDate).then(() => {
                this.setupNavigationHandlers();
                console.log('Navigation setup complete');
            });
        });
        // Show important announcement after UI is fully rendered
        // Change the id parameter for each new announcement to ensure it shows
        this.showImportantAnnouncement({
          title: '\u{1F6A8} Welcome to [Bracket Shitty] \u{1F6A8}',
          message: `
            <ul style="text-align: left; margin: 1rem 0; padding-left: 1.5rem;">
              <li>Click <span class='help-icon'>?</span> to access the <mark style="background-color:rgba(255, 255, 0, 0.2);">tutorial</mark></li>
              <li>Click <span class='help-icon'>!</span> to access <mark style="background-color:rgba(255, 255, 0, 0.2);">news and settings</mark></li>
              <li>Click the date header to access the <mark style="background-color:rgba(255, 255, 0, 0.2);">date picker</mark> and browse the archive</li>
            </ul>
            <p><strong>Thank you for playing Bracket Shitty!</strong></p>
          `,
          buttonText: 'ok great thanks',
          id: 'update-2025-3-11' // Change this ID for a new announcement
        });
        

    } catch (error) {
        console.error('Failed to initialize game:', error);
        this.showErrorMessage('Failed to load the game. Please refresh the page to try again.');
    }
  }

  /**
   * Initializes or retrieves the user ID
   * @returns {string} User ID
   */
  initializeUserId() {
    const storageKey = 'bracketCityUserId';
    let userId = localStorage.getItem(storageKey);
    
    if (!userId) {
      userId = crypto.randomUUID();
      localStorage.setItem(storageKey, userId);
    }
    
    return userId;
  }

  async initializePuzzleData() {
    const urlParams = new URLSearchParams(window.location.search);
    const encoded = urlParams.get('d');
    
    if (encoded) {
      // Try PuzzleEncoder format first
      const decodedPuzzle = PuzzleEncoder.decodePuzzle(encoded);
      if (decodedPuzzle) {
        this.PUZZLE_DATA = decodedPuzzle;
      } else {
        // Try direct base64 JSON format
        try {
          const jsonStr = atob(encoded);
          const puzzleData = JSON.parse(jsonStr);
          
          // Validate required fields
          if (puzzleData.initialPuzzle && puzzleData.solutions) {
            this.PUZZLE_DATA = {
              initialPuzzle: puzzleData.initialPuzzle,
              solutions: puzzleData.solutions
            };
          } else {
            console.error('Invalid puzzle data structure in URL');
            await this.fetchPuzzleForDate(this.currentPuzzleDate);
          }
        } catch (e) {
          console.error('Failed to decode puzzle data:', e);
          await this.fetchPuzzleForDate(this.currentPuzzleDate);
        }
      }
    } else {
      // No encoded puzzle, fetch for specific date or today
      await this.fetchPuzzleForDate(this.currentPuzzleDate);
    }
    
    if (!this.PUZZLE_DATA) {
      throw new Error('Failed to initialize puzzle data');
    }
  }

  /**
     * Checks if running in development environment
     */
  isDevelopmentEnvironment() {
    const hostname = window.location.hostname;
    return hostname === 'localhost' || 
           hostname === '127.0.0.1' ||
           hostname === 'staging.bracket.city' ||  
           hostname === 'dev.bracket.city';
  }


  /**
   * Gets the current input mode from localStorage or defaults to 'classic'
   * @returns {string} The input mode ('classic' or 'submit')
   */
  getInputMode() {
    return localStorage.getItem(this.MODE_STORAGE_KEY) || 'submit';
  }

  /**
   * Determines if a puzzle has been meaningfully started based on consistent criteria
   * @param {Object} puzzleData - The puzzle state to check
   * @returns {boolean} Whether the puzzle is considered started
   */
  isPuzzleStarted(puzzleData = this.state) {
    // If no data provided or using current state, check directly
    if (puzzleData === this.state) {
      return (this.state.totalKeystrokes > 5) || 
            (this.state.peekedClues && this.state.peekedClues.size > 0) ||
            (this.state.megaPeekedClues && this.state.megaPeekedClues.size > 0) ||
            (this.state.wrongGuesses > 0) ||
            (this.state.solvedExpressions && this.state.solvedExpressions.size > 0);
    }
    
    // For saved state data from localStorage (which has arrays instead of Sets)
    return (puzzleData.totalKeystrokes > 5) || 
          (puzzleData.peekedClues && puzzleData.peekedClues.length > 0) ||
          (puzzleData.megaPeekedClues && puzzleData.megaPeekedClues.length > 0) ||
          (puzzleData.wrongGuesses > 0) ||
          (puzzleData.solvedExpressions && puzzleData.solvedExpressions.length > 0);
  }

  /**
   * Sets the input mode and updates the UI
   * @param {string} mode - The input mode to set ('classic' or 'submit')
   */
  setInputMode(mode) {
    if (mode !== 'classic' && mode !== 'submit') {
      console.error('Invalid input mode:', mode);
      return;
    }
    
    // Check if the puzzle has been started
    const hasPuzzleStarted = this.isPuzzleStarted();
    
    // Only allow mode changes if the puzzle hasn't been started
    if (hasPuzzleStarted) {
      // Don't show a message - the disabled toggle with explanatory text is sufficient
      console.log('Mode change prevented: puzzle already started');
      return;
    }
    
    this.inputMode = mode;
    localStorage.setItem(this.MODE_STORAGE_KEY, mode);
    
    // Update rankCalculator's input mode
    this.rankCalculator.inputMode = mode;
    
    // Update the UI to reflect the new mode
    this.updateInputModeUI();
    
    // If help/info is currently visible, ensure inputs stay disabled
    if (this.state.helpVisible) {
      this.disableInputsForHelp();
    }
  }

  /**
   * New helper method to ensure inputs are disabled when help is visible
   */
  disableInputsForHelp() {
    // Get the input elements
    const desktopInput = this.root.querySelector('.answer-input');
    
    // Disable input while help is visible
    if (desktopInput) {
      desktopInput.disabled = true;
    }
    
    // Disable submit button (desktop)
    const submitButton = this.root.querySelector('.submit-answer');
    if (submitButton) {
      submitButton.disabled = true;
      submitButton.style.opacity = '0.5';
      submitButton.style.pointerEvents = 'none';
    }
    
    // For mobile, disable keyboard and input without hiding
    if (this.isMobile) {
      // Make custom input appear disabled
      const customInput = this.root.querySelector('.custom-input');
      if (customInput) {
        customInput.style.opacity = '0.5';
        customInput.style.pointerEvents = 'none';
      }
      
      // Disable all keyboard keys
      const keyboardKeys = this.root.querySelectorAll('.keyboard-key');
      keyboardKeys.forEach(key => {
        key.disabled = true;
        key.style.opacity = '0.5';
        key.style.pointerEvents = 'none';
      });
      
      // Disable mobile submit button
      const mobileSubmitButton = this.root.querySelector('.mobile-submit-button');
      if (mobileSubmitButton) {
        mobileSubmitButton.disabled = true;
        mobileSubmitButton.style.opacity = '0.5';
        mobileSubmitButton.style.pointerEvents = 'none';
      }
    }
  }

  /**
   * Updates the UI to match the current input mode
   */
  updateInputModeUI() {
    // Replace the input container HTML
    const inputContainer = this.root.querySelector('.input-container');
    if (!inputContainer) return;
    
    // Store current input value before replacing the container
    let currentInputValue = '';
    if (this.isMobile) {
      currentInputValue = this.customInputValue || '';
    } else {
      const inputEl = this.root.querySelector('.answer-input');
      currentInputValue = inputEl ? inputEl.value : '';
    }
    
    // Generate fresh input container HTML based on current mode
    inputContainer.innerHTML = this.generateInputContainerHTML();
    
    // Re-setup input handlers
    this.setupInputHandlers();
    
    // Restore input value
    if (this.isMobile) {
      this.customInputValue = currentInputValue;
      this.updateCustomInputDisplay();
    } else {
      const inputEl = this.root.querySelector('.answer-input');
      if (inputEl) inputEl.value = currentInputValue;
    }
    
    // Re-render to show updated stats if the puzzle is complete
    if (this.isPuzzleComplete()) {
      const keystrokeStats = this.root.querySelector('.keystroke-stats');
      if (keystrokeStats) {
        this.renderCompletionStats(keystrokeStats);
      }
    }
  }
  /****************
   State Management
  *****************/
  
    /**
     * Updates game state while maintaining consistency
     * @param {Partial<GameState>} updates - State properties to update
     */
    updateGameState(updates) {
      // Create new state with updates
      const newState = {
        ...this.state,
        ...updates
      };
  
      // Ensure Sets remain Sets
      if (updates.solvedExpressions) {
        newState.solvedExpressions = new Set(updates.solvedExpressions);
      }
      if (updates.hintModeClues) {
        newState.hintModeClues = new Set(updates.hintModeClues);
      }
      if (updates.peekedClues) {
        newState.peekedClues = new Set(updates.peekedClues);
      }
      if (updates.megaPeekedClues) {
        newState.megaPeekedClues = new Set(updates.megaPeekedClues);
      }
  
      // Update active clues if display state changes
      if (updates.displayState) {
        newState.activeClues = this.findActiveClues(updates.displayState);
      }
  
      // Update state and persist
      this.state = newState;
      this.saveState();
    }
    
    /**
     * Gets the storage key for the current puzzle - enhanced version with explicit date parameter
     * @param {string} puzzleDate - Optional specific puzzle date
     * @returns {string} Storage key
     */
    getStorageKey(puzzleDate) {
      const dateToUse = puzzleDate || this.currentPuzzleDate;
      if (!dateToUse) {
        console.error('No puzzle date available when getting storage key!');
        return null;
      }
      return `bracketPuzzle_${dateToUse}`;
    }
  
    /**
     * Enhanced loadSavedState method with input mode handling
     * @param {string} targetDate - The puzzle date to load state for
     * @returns {Object|null} Saved state or null if none exists or validation fails
     */
    async loadSavedState(targetDate) {
      try {
        // Ensure we're loading state for the correct puzzle date
        const puzzleDate = targetDate || this.currentPuzzleDate;
        
        if (!puzzleDate) {
          console.warn('Cannot load state: missing puzzle date');
          return null;
        }
        
        const storageKey = `bracketPuzzle_${puzzleDate}`;
        
        if (!this.PUZZLE_DATA) {
          console.warn('Cannot load state: missing puzzle data');
          return null;
        }

        const savedData = localStorage.getItem(storageKey);
        if (!savedData) {
          console.log('No saved state found for key:', storageKey);
          return null;
        }

        const parsed = JSON.parse(savedData);
        
        // Enhanced validation: verify the puzzle date in the saved state matches the requested date
        if (parsed.puzzleDate !== puzzleDate) {
          console.warn(`Saved state puzzle date (${parsed.puzzleDate}) doesn't match requested date (${puzzleDate})`);
          return null;
        }
        
        // Additional validation: verify the initialPuzzle matches
        if (parsed.initialPuzzle !== this.PUZZLE_DATA.initialPuzzle) {
          console.warn('Saved state initialPuzzle doesn\'t match current puzzle data');
          return null;
        }

        return {
          displayState: parsed.displayState,
          solvedExpressions: parsed.solvedExpressions || [],
          message: '', // Always start with empty message
          messageType: null,
          totalKeystrokes: parsed.totalKeystrokes || 0,
          minimumKeystrokes: parsed.minimumKeystrokes || this.calculateMinimumKeystrokes(),
          hintModeClues: parsed.hintModeClues || [],
          peekedClues: parsed.peekedClues || [],
          megaPeekedClues: parsed.megaPeekedClues || [],
          initialPuzzle: parsed.initialPuzzle,
          puzzleDate: parsed.puzzleDate,
          wrongGuesses: parsed.wrongGuesses || 0,
          // Include the saved input mode if it exists
          inputMode: parsed.inputMode || this.getInputMode(), 
          // Preserve help system state properties if they exist
          helpVisible: parsed.helpVisible || false,
          previousDisplay: parsed.previousDisplay || null
        };

      } catch (error) {
        console.error('Error loading saved state:', error);
        return null;
      }
    }
    
    saveState() {
      try {
        const storageKey = this.getStorageKey();
        if (!storageKey) {
          console.error('Cannot save state: missing storage key');
          return false;
        }
    
        const stateToSave = {
          displayState: this.state.displayState,
          solvedExpressions: Array.from(this.state.solvedExpressions),
          totalKeystrokes: this.state.totalKeystrokes,
          minimumKeystrokes: this.state.minimumKeystrokes,
          hintModeClues: Array.from(this.state.hintModeClues),
          peekedClues: Array.from(this.state.peekedClues),
          megaPeekedClues: Array.from(this.state.megaPeekedClues),
          initialPuzzle: this.PUZZLE_DATA.initialPuzzle,
          puzzleDate: this.currentPuzzleDate,
          isComplete: this.state.isComplete || false,
          completionStats: this.state.completionStats || null,
          wrongGuesses: this.state.wrongGuesses || 0,
          inputMode: this.inputMode // Save the input mode with puzzle state
        };
    
        localStorage.setItem(storageKey, JSON.stringify(stateToSave));
        return true;
    
      } catch (error) {
        console.error('Error saving state:', error);
        return false;
      }
    }

  
    /**
     * Resets puzzle state and storage
     */
    resetPuzzle() {
      if (!confirm('Are you sure you want to reset the puzzle? All progress will be lost.')) {
        return;
      }

      // Clear saved state.
      const storageKey = this.getStorageKey();
      if (storageKey) {
        localStorage.removeItem(storageKey);
      }
      
      // Get the user's preferred input mode
      const preferredInputMode = this.getInputMode();
      
      // Update the current input mode to the preferred mode
      this.inputMode = preferredInputMode;
      
      // Update the rankCalculator's input mode
      this.rankCalculator.inputMode = this.inputMode;

      // Reset game state
      this.updateGameState({
        displayState: this.PUZZLE_DATA.initialPuzzle,
        solvedExpressions: new Set(),
        message: '',
        totalKeystrokes: 0,
        minimumKeystrokes: this.calculateMinimumKeystrokes(),
        activeClues: this.findActiveClues(this.PUZZLE_DATA.initialPuzzle),
        hintModeClues: new Set(),
        peekedClues: new Set(),
        megaPeekedClues: new Set(),
        helpVisible: false,
        previousDisplay: null,
        wrongGuesses: 0 // Reset wrong guesses
      });

      // Remove any existing completion message and "completed" classes.
      const completionMessage = this.root.querySelector('.completion-message');
      if (completionMessage) {
        completionMessage.remove();
      }
      const container = this.root.querySelector('.puzzle-container');
      if (container) {
        container.classList.remove('completed');
      }
      const puzzleDisplay = this.root.querySelector('.puzzle-display');
      if (puzzleDisplay) {
        puzzleDisplay.classList.remove('completed');
      }

      // Rebuild the UI within your dedicated container.
      this.root.innerHTML = this.generateInitialHTML();

      // Re‑attach event handlers.
      this.setupInputHandlers();
      this.setupShareButtons();
      this.setupPuzzleDisplay();
      this.setupDatePicker();
      // Re‑attach navigation handlers. Use a timeout to allow the new DOM to settle.
      setTimeout(() => {
        this.setupNavigationHandlers();
        this.checkAdjacentPuzzles(this.currentPuzzleDate);
      }, 0);

      // Make sure the input container is visible.
      const inputContainer = this.root.querySelector('.input-container');
      if (inputContainer) {
        inputContainer.style.display = 'block';
      }
      if (!this.isMobile && this.root.querySelector('.answer-input')) {
        this.root.querySelector('.answer-input').focus();
      }

      this.render();
      this.showMessage('Puzzle reset successfully!');
    }
  
    calculateMinimumKeystrokes() {
      return Object.values(this.PUZZLE_DATA.solutions)
        .reduce((total, solution) => total + solution.length, 0);
    }

    /*************
     Data and API
    **************/

    /**
     * Fetches and processes puzzle data for a date
     */
    async fetchPuzzleForDate(targetDate) {
      try {
          // Try to get puzzle for specified date
          let response = await fetch(`${this.API_ENDPOINT}/${targetDate}`, {
              cache: 'no-store',
              headers: {
                  'Cache-Control': 'no-cache',
                  'Pragma': 'no-cache'
              }
          });
          
          // If that date's puzzle doesn't exist, get latest puzzle
          if (!response.ok) {
              // Get base list of puzzles
              response = await fetch(this.API_ENDPOINT, {
                  cache: 'no-store',
                  headers: {
                      'Cache-Control': 'no-cache',
                      'Pragma': 'no-cache'
                  }
              });
              if (!response.ok) {
                  throw new Error('Unable to fetch puzzle list');
              }
              
              const puzzles = await response.json();
              if (!puzzles || puzzles.length === 0) {
                  throw new Error('No puzzles available');
              }
              
              // Find the most recent puzzle date
              const latestPuzzleDate = puzzles.reduce((latest, current) => {
                  return latest > current ? latest : current;
              });
              
              // Fetch the latest puzzle
              response = await fetch(`${this.API_ENDPOINT}/${latestPuzzleDate}`, {
                  cache: 'no-store',
                  headers: {
                      'Cache-Control': 'no-cache',
                      'Pragma': 'no-cache'
                  }
              });
              if (!response.ok) {
                  throw new Error('Failed to fetch latest puzzle');
              }
          }
          
          const puzzleData = await response.json();
          
          /*console.log('Raw puzzle data received:', {
              date: targetDate,
              puzzleData,
              timestamp: new Date().toISOString()
          });*/

          // Clean up escaped quotes and store in PUZZLE_DATA
          this.PUZZLE_DATA = {
              ...puzzleData,
              initialPuzzle: this.unescapeString(puzzleData.initialPuzzle),
              solutions: Object.fromEntries(
                  Object.entries(puzzleData.solutions).map(([key, value]) => [
                      this.unescapeString(key),
                      this.unescapeString(value)
                  ])
              )
          };

          return true;

      } catch (error) {
          console.error('Error fetching puzzle:', error);
          return false;
      }
    }
    
    unescapeString(str) {
      if (typeof str !== 'string') return str;
      return str.replace(/\\"/g, '"')
                .replace(/\\'/g, "'")
                .replace(/\\\\/g, "\\");
    }
    
    /***********************
     Date and URL management
    ************************/
    
    getNYCDate() {
      // Create a date object
      const date = new Date();
      
      // Get the NY time options
      const nyOptions = {
        timeZone: 'America/New_York',
        year: 'numeric',
        month: '2-digit',
        day: '2-digit'
      };
      
      // Format date in NY timezone
      const nyDateStr = date.toLocaleString('en-US', nyOptions);
      
      // Split into components and rearrange into YYYY-MM-DD
      const [month, day, year] = nyDateStr.split('/');
      return `${year}-${month}-${day}`;
    }
  
    formatNYCDate(date = this.getNYCDate()) {
      // Split the YYYY-MM-DD format
      const [year, month, day] = date.split('-');
      
      // Create a Date object in the NYC timezone
      // First create a date string with the format that ensures it's interpreted as NYC time
      const nycDateString = `${year}-${month}-${day}T12:00:00-05:00`; // Noon in NYC to avoid DST issues
      const dateObj = new Date(nycDateString);
      
      // Format it for display using the NYC timezone
      return dateObj.toLocaleDateString('en-US', { 
        month: 'long', 
        day: 'numeric', 
        year: 'numeric',
        timeZone: 'America/New_York'
      });
    }
  
    /**
     * Updates URL with the current puzzle date
     * @param {string} puzzleDate - Date in YYYY-MM-DD format
     */
    updateURL(puzzleDate) {
      const url = new URL(window.location.href);
      
      // If we have an encoded puzzle, don't modify URL
      if (url.searchParams.has('d')) {
        return;
      }
      
      // Get today's date in NYC time zone
      const todayPuzzleDate = this.getNYCDate();
      
      // Check if we're using path format or query parameter format
      const isPathFormat = window.location.pathname.match(/\/puzzles\/\d{4}\/\d+\/\d+/);
      
      if (isPathFormat) {
        // If using path format, keep using path format
        if (puzzleDate !== todayPuzzleDate) {
          // Format the date components for the path
          const [year, month, day] = puzzleDate.split('-');
          const newPath = `/puzzles/${year}/${parseInt(month)}/${parseInt(day)}`;
          window.history.pushState({}, '', newPath);
        } else {
          // If it's today's puzzle, go to home
          window.history.pushState({}, '', '/');
        }
      } else {
        // Using query parameter format, stick with that
        url.searchParams.delete('date');
        if (puzzleDate !== todayPuzzleDate) {
          url.searchParams.set('date', puzzleDate);
        }
        window.history.pushState({}, '', url.toString());
      }
    }
  
    /**
     * Gets puzzle date from URL or defaults to current NYC date
     * @returns {string} Puzzle date in YYYY-MM-DD format
     */
    getPuzzleDateFromURL() {
      // NEW: First check path for /puzzles/YYYY/M/D pattern
      const pathMatch = window.location.pathname.match(/\/puzzles\/(\d{4})\/(\d{1,2})\/(\d{1,2})/);
      if (pathMatch) {
        const year = pathMatch[1];
        const month = pathMatch[2].padStart(2, '0');
        const day = pathMatch[3].padStart(2, '0');
        const dateStr = `${year}-${month}-${day}`;
        
        if (this.isValidPuzzleDate(dateStr)) {
          return dateStr;
        }
      }
      
      // UNCHANGED: Your existing query parameter logic stays exactly the same
      const urlParams = new URLSearchParams(window.location.search);
      
      if (urlParams.has('d')) {
        const decoded = PuzzleEncoder.decodePuzzle(urlParams.get('d'));
        return decoded?.puzzleDate || this.getNYCDate();
      }
      
      const dateParam = urlParams.get('date');
      if (dateParam && this.isValidPuzzleDate(dateParam)) {
        return dateParam;
      }
      
      return this.getNYCDate();
    }
  
    /**
     * Validates a puzzle date string
     * @param {string} dateStr - Date string to validate
     * @returns {boolean} Whether the date is valid
     */
    isValidPuzzleDate(dateStr) {
      // Check format (YYYY-MM-DD)
      if (!/^\d{4}-\d{2}-\d{2}$/.test(dateStr)) {
        return false;
      }
      
      const date = new Date(dateStr);
      if (isNaN(date.getTime())) {
        return false;
      }
      
      // On dev servers, allow any valid date
      if (this.isDevelopmentEnvironment()) {
        return true;
      }
      
      // On production, don't allow future dates
      const nycDate = new Date(this.getNYCDate());
      return date <= nycDate;
    }
    
    
    /**********
     Game logic
    ***********/

    /**
     * Finds all active clues in the current puzzle state
     * @param {string} puzzleText - Current puzzle text
     * @returns {Array<{start: number, end: number, text: string, expression: string}>}
     */
    findActiveClues(puzzleText) {
      const activeClues = [];
      
      function findClues(str, startOffset = 0) {
        let i = 0;
        while (i < str.length) {
          if (str[i] === '[') {
            const startIndex = i;
            let bracketCount = 1;
            let hasNestedBrackets = false;
            i++;
            
            let innerContent = '';
            while (i < str.length && bracketCount > 0) {
              if (str[i] === '[') {
                bracketCount++;
                hasNestedBrackets = true;
              } else if (str[i] === ']') {
                bracketCount--;
              }
              innerContent += str[i];
              i++;
            }
            
            innerContent = innerContent.slice(0, -1);
            
            if (!hasNestedBrackets) {
              // Clean expression text of HTML markup
              const cleanExpression = innerContent.replace(/<\/?[^>]+(>|$)/g, '');
              activeClues.push({
                start: startOffset + startIndex,
                end: startOffset + i,
                text: str.substring(startIndex, i),
                expression: cleanExpression
              });
            }
            
            if (hasNestedBrackets) {
              findClues(innerContent, startOffset + startIndex + 1);
            }
          } else {
            i++;
          }
        }
      }
      
      const cleanText = puzzleText.replace(/<\/?span[^>]*(>|$)/g, '');
      findClues(cleanText);
      return activeClues;
    }
  
    /**
     * Finds available expressions that can be solved
     * @param {string} text - Current puzzle text
     * @returns {Array<{expression: string, startIndex: number, endIndex: number}>}
     */
    findAvailableExpressions(text) {
      const cleanText = text.replace(/<\/?span[^>]*(>|$)/g, '');
      const results = [];
      const regex = /\[([^\[\]]+?)\]/g;
      const matchedPositions = new Set();
      let match;
      
      while ((match = regex.exec(cleanText)) !== null) {
        const startIdx = match.index;
        const endIdx = startIdx + match[0].length;
        
        if (!matchedPositions.has(startIdx)) {
          matchedPositions.add(startIdx);
          results.push({
            expression: match[1],
            startIndex: startIdx,
            endIndex: endIdx
          });
        }
      }
      
      return results;
    }

    /**
     * Generates new state after solving a clue
     * @param {Object} clue - Clue object
     * @param {string} solution - Solution text
     * @returns {string} New display state
     */
    generateSolvedClueState(clue, solution) {
      const cleanState = this.state.displayState.replace(/<\/?[^>]+(>|$)/g, '');
      const escapedSolution = solution
        .replace(/"/g, '&quot;')
        .replace(/'/g, '&#39;');
      
      return (
        cleanState.slice(0, clue.startIndex) + 
        `<mark class="solved">${escapedSolution}</mark>` + 
        cleanState.slice(clue.endIndex)
      );
    }
  
    /**
     * Handles user input for solving expressions
     * @param {string} input - User input string
     */
    handleInput(input) {
      const normalizedInput = this.normalizeInput(input);
      if (!normalizedInput) {
        return;
      }
    
      const match = this.findMatchingExpression(normalizedInput);
      if (!match) {
        return;
      }
    
      this.solveExpression(match);
    }
  
    /**
     * Normalizes and validates user input
     * @param {string} input - Raw user input
     * @returns {string|null} Normalized input or null if invalid
     */
    normalizeInput(input) {
      if (!input?.trim()) {
        return null;
      }
      return input.trim().toLowerCase();
    }

    /**
     * Handles submission in submit mode
     */
    handleSubmission() {
      const input = this.isMobile ? this.customInputValue : this.answerInput.value;
      if (!input || !input.trim()) return;

      const normalizedInput = this.normalizeInput(input);
      const match = this.findMatchingExpression(normalizedInput);
      
      if (match) {
        this.solveExpression(match);
        
        // Check if puzzle is complete after solving
        if (this.isPuzzleComplete()) {
          this.renderCompletedPuzzle(
            this.root.querySelector('.puzzle-display'),
            this.root.querySelector('.input-container'),
            this.root.querySelector('.keystroke-stats')
          );
        }
      } else {
        // Increment wrong guesses counter and show error message
        this.updateGameState({
          wrongGuesses: (this.state.wrongGuesses || 0) + 1
        });
        
        this.showMessage('Incorrect!', 'error');
      }

      // Clear input
      if (this.isMobile) {
        this.customInputValue = '';
        this.updateCustomInputDisplay();
      } else {
        this.answerInput.value = '';
        this.answerInput.focus();
      }
    }
  
    /**
     * Finds an unsolved expression matching the input
     * @param {string} normalizedInput - Normalized user input
     * @returns {Object|null} Matching expression info or null if not found
     */
    findMatchingExpression(normalizedInput) {
      const cleanState = this.state.displayState.replace(/<\/?[^>]+(>|$)/g, '');
      const availableExpressions = this.findAvailableExpressions(cleanState);
    
      for (const {expression, startIndex, endIndex} of availableExpressions) {
        const solution = this.PUZZLE_DATA.solutions[expression];
        
        if (solution?.toLowerCase() === normalizedInput && 
            !this.state.solvedExpressions.has(expression)) {
          return {
            expression,
            solution,
            startIndex,
            endIndex
          };
        }
      }
    
      return null;
    }
  
    /**
     * Processes a correct solution and updates game state
     * @param {Object} match - Expression match information
     */
    solveExpression(match) {
      const { expression, solution, startIndex, endIndex } = match;
      
      const isFirstSolve = this.state.solvedExpressions.size === 0;
      
      // Generate the new puzzle display state by replacing the solved expression
      const cleanState = this.state.displayState.replace(/<\/?[^>]+(>|$)/g, '');
      const escapedSolution = solution
          .replace(/"/g, '&quot;')
          .replace(/'/g, '&#39;');
      
      const newDisplayState = 
          cleanState.slice(0, startIndex) + 
          `<mark class="solved">${escapedSolution}</mark>` + 
          cleanState.slice(endIndex);
      
      // Add new expression at the beginning of the Set to maintain reverse chronological order
      const updatedExpressions = new Set([expression, ...Array.from(this.state.solvedExpressions)]);

      // turn off tutorial prompt
      this.turnOffTutorialPulse();

      // Update game state with the new display and mark the expression as solved
      this.updateGameState({
          displayState: newDisplayState,
          solvedExpressions: updatedExpressions
      });
      
      // Show a success message (auto-clears later)
      this.showMessage('Correct!', 'success');
      
      // Clear the input based on device type:
      if (this.isMobile) {
          this.customInputValue = '';
          if (this.customInputEl) {
              this.updateCustomInputDisplay();
          }
      } else {
          if (this.answerInput) {
              this.answerInput.value = '';
          }
      }

      // Re-render the puzzle display
      requestAnimationFrame(() => {
          this.render();
      });
    }

    /**
     * Processes a solution reveal for megapeek
     * @param {Object} match - Expression match information
     */
    processSolutionReveal(expression, solution) {
      // Clean the display state first
      const cleanState = this.state.displayState.replace(/<\/?[^>]+(>|$)/g, '');
      const availableExpressions = this.findAvailableExpressions(cleanState);
      
      for (const {startIndex, endIndex} of availableExpressions) {
        const currentExpression = this.unescapeString(
          cleanState.slice(startIndex + 1, endIndex - 1).trim()
        );
        
        if (this.unescapeString(expression) === currentExpression) {
          const escapedSolution = solution
            .replace(/"/g, '&quot;')
            .replace(/'/g, '&#39;');
          
          const newDisplayState = 
            cleanState.slice(0, startIndex) + 
            `<mark class="solved">${escapedSolution}</mark>` + 
            cleanState.slice(endIndex);
    
          return newDisplayState;
        }
      }
      return null;
    }

    /************
     Hint system
    *************/
    
    /**
     * Toggles hint mode for an expression between peek (show length) and mega peek (reveal answer)
     * @param {string} expression - The expression to toggle hints for
     */
    toggleHintMode(expression) {
      // Early validation
      if (!expression || this.state.megaPeekedClues.has(expression)) {
        return;
      }
    
      // Handle mega peek (second click)
      if (this.state.hintModeClues.has(expression)) {
        this.handleMegaPeek(expression);
        return;
      }
    
      // Handle first peek
      this.handleFirstPeek(expression);
    }
  
    /**
     * Handles the first peek at an expression (shows length)
     * @param {string} expression - The expression to peek at
     */
    handleFirstPeek(expression) {
      // Add confirmation dialog for regular peek
      if (!confirm('You sure you want to peek at the first letter of this answer?')) {
        return;
      }

      this.updateGameState({
        hintModeClues: new Set([...this.state.hintModeClues, expression]),
        peekedClues: new Set([...this.state.peekedClues, expression])
      });

      this.render();
    }
  
    /**
     * Handles the mega peek action (reveals answer)
     * @param {string} expression - The expression to reveal
     */
    handleMegaPeek(expression) {
      if (!confirm('You sure you want to reveal this answer?')) {
        return;
      }

      const solution = this.PUZZLE_DATA.solutions[expression];
      if (!solution) {
        console.error('No solution found for expression:', expression);
        return;
      }

      const newDisplayState = this.processSolutionReveal(expression, solution);
      if (!newDisplayState) {
        console.error('Could not find expression in puzzle state');
        return;
      }

      this.updateGameState({
        displayState: newDisplayState,
        totalKeystrokes: this.state.totalKeystrokes + solution.length,
        megaPeekedClues: new Set([...this.state.megaPeekedClues, expression]),
        solvedExpressions: new Set([expression, ...Array.from(this.state.solvedExpressions)]),
        hintModeClues: new Set([...this.state.hintModeClues].filter(e => e !== expression))
      });

      this.render();
    }

    /***************
     Input Handling
    ****************/
    
     /**
     * Conservatively detects if the current device is a tablet (iPad or Android)
     * @returns {boolean} True if the device is confidently identified as a tablet
     */
    isTablet() {
      // IPAD DETECTION
      const isLegacyIPad = /iPad/i.test(navigator.userAgent);
      const isModernIPad = !(/iPhone|iPod/.test(navigator.userAgent)) && 
                          navigator.platform === 'MacIntel' && 
                          navigator.maxTouchPoints > 1 && 
                          !window.MSStream;
      
      // Any iPad detection is sufficient
      const isIPad = isLegacyIPad || isModernIPad;
      
      // ANDROID TABLET DETECTION
      // Standard way: Android without "Mobile" in UA string typically means tablet
      const isAndroidTablet = /Android/i.test(navigator.userAgent) && 
                            !/Mobile/i.test(navigator.userAgent);
      
      // Alternative Android tablet signals                     
      const hasExplicitTabletIdentifier = /Tablet|Tab/i.test(navigator.userAgent) ||
                                        /SM-T[0-9]{3}/i.test(navigator.userAgent); // Samsung Galaxy Tab model numbers
      
      // GENERIC TABLET SIZE CHECK (as backup, not primary signal)
      // Most tablets have at least one dimension ≥ 768px
      const hasTabletDimensions = Math.max(
        screen.width, 
        screen.height, 
        window.innerWidth, 
        window.innerHeight
      ) >= 768;
      
      // EXPLICIT PHONE DETECTION (to avoid false positives)
      const isExplicitlyPhone = /iPhone|iPod|Android.*Mobile|Mobile.*Android|BlackBerry|IEMobile|Opera Mini|Windows Phone/i.test(navigator.userAgent);
      
      // Combined tablet detection:
      return isIPad || ((isAndroidTablet || hasExplicitTabletIdentifier) && 
                        hasTabletDimensions && 
                        !isExplicitlyPhone);
    }

    /**
     * Sets up input handlers based on current mode
     */
    setupInputHandlers() {
      if (this.useCustomKeyboard) {
        // Use custom keyboard for phones
        this.setupMobileInput();
      } else {
        // Use native keyboard for tablets and desktops
        this.setupDesktopInput();
      }
    }    
  
    /**
     * Sets up desktop input handlers
     */
    setupDesktopInput() {
      this.answerInput = this.root.querySelector('.answer-input');
      const submitButton = this.root.querySelector('.submit-answer');
      
      if (!this.answerInput) {
        console.error('Answer input element not found');
        return;
      }
      
      if (this.inputMode === 'submit') {
        // Submit mode - only handle input on submit button click
        if (submitButton) {
          submitButton.addEventListener('click', () => {
            this.handleSubmission();
          });
        }
        
        // Handle Enter key press
        this.answerInput.addEventListener('keypress', (e) => {
          if (e.key === 'Enter') {
            e.preventDefault();
            
            // Apply visual feedback if submit button exists
            if (submitButton) {
              submitButton.style.transform = 'scale(0.95)';
              submitButton.style.backgroundColor = '#1e40af';
              
              // Remove the styles after a short delay
              setTimeout(() => {
                submitButton.style.transform = '';
                submitButton.style.backgroundColor = '';
              }, 85);
            }
            
            this.handleSubmission();
          }
        });
      } else {
        // Classic mode - auto-snap answers as typed
        this.answerInput.addEventListener('input', (e) => {
          this.handleInput(e.target.value);
        });
      }
      
      this.answerInput.addEventListener('keydown', (e) => {
        this.handleKeyDown(e);
      });

      this.answerInput.addEventListener('paste', (e) => {
        e.preventDefault();
      });

      this.answerInput.focus();
    }

    /**
     * Sets up mobile input handlers
     */
    setupMobileInput() {
      this.customInputValue = this.customInputValue || '';
      this.customInputEl = this.root.querySelector('.custom-input');
      
      if (!this.customInputEl) {
        console.error('Custom input display element not found');
        return;
      }
      
      // Clean up old placeholder if it exists
      const oldPlaceholder = this.customInputEl.querySelector('.placeholder');
      if (oldPlaceholder) {
        oldPlaceholder.remove();
      }
      
      // Create fresh placeholder
      const placeholderEl = document.createElement('span');
      placeholderEl.className = 'placeholder';
      placeholderEl.style.color = '#9ca3af';
      placeholderEl.textContent = this.inputMode === 'classic' ? 'start typing answers...' : 'type any answer...';
      this.customInputEl.appendChild(placeholderEl);

      // Set up the mobile submit button (if in submit mode)
      if (this.inputMode === 'submit') {
        const mobileSubmitButton = this.root.querySelector('.mobile-submit-button');
        if (mobileSubmitButton) {
          mobileSubmitButton.addEventListener('click', () => {
            this.handleSubmission();
          });
        }
      }

      // Clean up any existing listeners first
      const oldKeyboardKeys = this.root.querySelectorAll('.keyboard-key');
      oldKeyboardKeys.forEach(keyEl => {
        const newKeyEl = keyEl.cloneNode(true);
        keyEl.parentNode.replaceChild(newKeyEl, keyEl);
      });

      // Attach fresh click listeners to keyboard keys
      const keyboardKeys = this.root.querySelectorAll('.keyboard-key');
      keyboardKeys.forEach(keyEl => {
        keyEl.addEventListener('click', () => {
          const key = keyEl.getAttribute('data-key');
          
          if (key === 'backspace') {
            this.customInputValue = this.customInputValue.slice(0, -1);
          } else if (key === '123' || key === 'ABC') {
            this.isAltKeyboard = !this.isAltKeyboard;
            // Re-render the keyboard
            const keyboardContainer = this.root.querySelector('.custom-keyboard');
            if (keyboardContainer) {
              keyboardContainer.innerHTML = this.generateKeyboardButtonsHTML();
            }
            // Re-attach event listeners while preserving input
            this.setupMobileInput();
            return;
          } else {
            this.customInputValue += key;
            
            this.updateGameState({ totalKeystrokes: this.state.totalKeystrokes + 1 });
            
          }
          
          this.updateCustomInputDisplay();
          
          // In classic mode, check for solution match after each key
          if (this.inputMode === 'classic') {
            this.handleInput(this.customInputValue);
          }
        });

        // Prevent dragging/swiping on the key
        keyEl.addEventListener('touchmove', (e) => {
          e.preventDefault();
        }, { passive: false });
      });

      // Enable :active states on iOS
      document.addEventListener('touchstart', () => {}, false);
      
      // Update display to show current input value
      this.updateCustomInputDisplay();
    }

    updateCustomInputDisplay() {
      if (!this.customInputEl) return;
    
      // Find or create placeholder
      let placeholderEl = this.customInputEl.querySelector('.placeholder');
      if (!placeholderEl) {
          placeholderEl = document.createElement('span');
          placeholderEl.className = 'placeholder';
          placeholderEl.style.color = '#9ca3af';
          placeholderEl.textContent = 'type any answer...';
      }
    
      // Clear the input
      this.customInputEl.innerHTML = '';
    
      // Show either placeholder or input value
      if (this.customInputValue.length > 0) {
          this.customInputEl.textContent = this.customInputValue;
      } else {
          this.customInputEl.appendChild(placeholderEl);
      }
    }

    /**
     * Handles keydown events for keystroke tracking
     * @param {KeyboardEvent} event - The keyboard event
     */
    handleKeyDown(event) {
       // turn off tutorial pulse
      // Only count meaningful keystrokes (not control keys)
      if (this.isCountableKeystroke(event)) {
        this.updateGameState({
          totalKeystrokes: this.state.totalKeystrokes + 1
        });
    
      }
    }
  
    /**
     * Determines if a keystroke should be counted
     * @param {KeyboardEvent} event - The keyboard event
     * @returns {boolean} Whether the keystroke should be counted
     */
    isCountableKeystroke(event) {
      // Only count single printable characters
      return event.key.length === 1 && 
             /^[a-zA-Z0-9!@#$%^&*()_+\-=\[\]{};':"\\|,.<>\/? ]$/.test(event.key);
    }

    /***********
     Navigation
    ************/
    
    /**
     * Creates and attaches the date picker functionality
     */
    setupDatePicker() {
      // Find the date element to attach the picker to
      const dateElement = this.root.querySelector('.puzzle-date');
      if (!dateElement) return;
      
      // Remove any existing date picker containers to prevent duplicates
      const existingContainers = this.root.querySelectorAll('.date-picker-container');
      existingContainers.forEach(container => container.remove());
      
      // Clean up any existing event listeners by cloning the element
      const dateParent = dateElement.parentNode;
      const newDateElement = dateElement.cloneNode(true);
      dateParent.replaceChild(newDateElement, dateElement);
      
      // Make it look clickable
      newDateElement.style.cursor = 'pointer';
      newDateElement.title = 'Tap to open puzzle calendar';
      
      // Add visual indication for mobile users
      const isMobile = /Android|iPhone|iPad|iPod/i.test(navigator.userAgent);
      if (isMobile) {
        // Add a calendar icon or more visible indicator for mobile
        dateParent.style.position = 'relative';
        
        // Enhance tap target size for mobile
        newDateElement.style.padding = '8px 4px';
        newDateElement.style.position = 'relative';
        newDateElement.style.zIndex = '1';
      }
      
      // Create a container for the date picker (initially hidden)
      const datePickerContainer = document.createElement('div');
      datePickerContainer.className = 'date-picker-container';
      datePickerContainer.style.display = 'none';
      datePickerContainer.style.position = 'absolute';
      datePickerContainer.style.top = '100%';
      datePickerContainer.style.left = '50%';
      datePickerContainer.style.transform = 'translateX(-50%)';
      datePickerContainer.style.zIndex = '1500'; // Increased z-index to ensure visibility
      datePickerContainer.style.backgroundColor = 'white';
      datePickerContainer.style.boxShadow = '0 4px 12px rgba(0,0,0,0.2)';
      datePickerContainer.style.borderRadius = '0.5rem';
      datePickerContainer.style.padding = '1rem';
      datePickerContainer.style.marginTop = '0.5rem';
      datePickerContainer.style.maxWidth = isMobile ? '300px' : '340px';
      datePickerContainer.style.width = isMobile ? '90vw' : '100%';
      datePickerContainer.style.boxSizing = 'border-box'; // Ensure padding is included in width
      
      // Insert the container after the date element
      dateParent.style.position = 'relative';
      dateParent.style.zIndex = '100'; // Ensure parent has reasonable z-index
      dateParent.appendChild(datePickerContainer);
      
      // Toggle function to open/close the date picker
      const toggleDatePicker = (e) => {
        // Prevent default to avoid any browser handling
        e.preventDefault();
        e.stopPropagation();
        
        // Add log for debugging
        console.log('Date picker toggle clicked', {
          containerDisplay: datePickerContainer.style.display,
          isCompletionState: this.isPuzzleComplete()
        });
        
        if (datePickerContainer.style.display === 'none') {
          this.renderDatePicker(datePickerContainer);
          datePickerContainer.style.display = 'block';
          
          // Create a close handler that works for both mouse and touch
          const closeCalendar = (event) => {
            // Don't close if clicked on the date element or within the picker
            if (!datePickerContainer.contains(event.target) && event.target !== newDateElement) {
              datePickerContainer.style.display = 'none';
              document.removeEventListener('click', closeCalendar);
              document.removeEventListener('touchend', closeCalendar);
            }
          };
          
          // Add both mouse and touch event listeners to handle closing
          document.addEventListener('click', closeCalendar);
          document.addEventListener('touchend', closeCalendar);
        } else {
          datePickerContainer.style.display = 'none';
        }
      };
      
      // Add event listeners for both click and touch events with preventTouchFocus fix
      const preventTouchFocus = (e) => {
        e.preventDefault(); // Prevent focus/highlight issues on some mobile browsers
        toggleDatePicker(e);
      };
      
      newDateElement.addEventListener('click', toggleDatePicker);
      newDateElement.addEventListener('touchend', preventTouchFocus);
      
      // Add visible debug helper in development environments
      if (this.isDevelopmentEnvironment()) {
        newDateElement.setAttribute('data-debug', 'date-picker-trigger');
      }
    }

    /**
     * Renders the date picker with available puzzles
     * @param {HTMLElement} container - The container to render the date picker in
     */
    async renderDatePicker(container) {
      // Show loading state
      container.innerHTML = '<div class="loading-calendar">Loading calendar...</div>';
      
      try {
        // Fetch available puzzles
        const availableDates = await this.fetchAvailablePuzzleDates();
        if (!availableDates || availableDates.length === 0) {
          container.innerHTML = '<div class="error">No puzzles available</div>';
          return;
        }
        
        // Get completion status for all puzzles
        const puzzleStatuses = this.getPuzzleCompletionStatuses(availableDates);
        
        // Determine the current month and year (in NYC timezone)
        const currentDateNYC = new Date(this.getNYCDate());
        let currentViewMonth = currentDateNYC.getMonth();
        let currentViewYear = currentDateNYC.getFullYear();
        
        // Render initial calendar
        this.renderMonthCalendar(container, currentViewMonth, currentViewYear, availableDates, puzzleStatuses);
        
      } catch (error) {
        console.error('Error rendering date picker:', error);
        container.innerHTML = '<div class="error">Failed to load calendar</div>';
      }
    }

    /**
     * Renders a specific month's calendar
     * @param {HTMLElement} container - The container element
     * @param {number} month - Month to render (0-11)
     * @param {number} year - Year to render
     * @param {string[]} availableDates - Array of available puzzle dates
     * @param {Object} puzzleStatuses - Map of dates to completion status
     */
    renderMonthCalendar(container, month, year, availableDates, puzzleStatuses) {
      // Calculate NYC date strings correctly
      const getConsistentDateStr = (year, month, day) => {
        // This ensures we're constructing dates consistently as YYYY-MM-DD
        return `${year}-${String(month + 1).padStart(2, '0')}-${String(day).padStart(2, '0')}`;
      };
      
      // Get month info - using local date objects for display purposes only
      const monthStart = new Date(year, month, 1);
      const monthName = monthStart.toLocaleString('en-US', { month: 'long', year: 'numeric' });
      
      // Get days in month
      const daysInMonth = new Date(year, month + 1, 0).getDate();
      
      // Get the day of week the month starts on (0 = Sunday, 6 = Saturday)
      const startDay = monthStart.getDay();
      
      // Get today's date in NYC
      const todayNYC = this.getNYCDate();
      
      // Create calendar HTML
      let calendarHTML = `
        <div class="date-picker-header" style="display: flex; justify-content: space-between; align-items: center; margin-bottom: 1rem;">
          <button class="month-nav prev" style="background: none; border: none; cursor: pointer; font-size: 1.2rem;">&lsaquo;</button>
          <h3 style="margin: 0; text-align: center; font-size: 1.2rem;">${monthName}</h3>
          <button class="month-nav next" style="background: none; border: none; cursor: pointer; font-size: 1.2rem;">&rsaquo;</button>
        </div>
        
        <div class="weekdays" style="display: grid; grid-template-columns: repeat(7, 1fr); text-align: center; font-weight: bold; margin-bottom: 0.5rem;">
          <div>Su</div>
          <div>Mo</div>
          <div>Tu</div>
          <div>We</div>
          <div>Th</div>
          <div>Fr</div>
          <div>Sa</div>
        </div>
        
        <div class="calendar-grid" style="display: grid; grid-template-columns: repeat(7, 1fr); gap: 5px; width: 100%;">
      `;
      
      // Add empty cells for days before the month starts
      for (let i = 0; i < startDay; i++) {
        calendarHTML += `<div class="empty-day"></div>`;
      }
      
      // Add cells for each day in the month
      for (let day = 1; day <= daysInMonth; day++) {
        // Format date string consistently
        const dateStr = getConsistentDateStr(year, month, day);
        
        // Check if this date has a puzzle available
        const isPuzzleAvailable = availableDates.includes(dateStr);
        
        // Get puzzle status for this date
        const puzzleStatus = puzzleStatuses[dateStr] || 'unsolved';
        
        // Determine if this is today's date
        const isToday = dateStr === todayNYC;
        
        // Determine if this is the current puzzle date
        const isCurrentPuzzle = dateStr === this.currentPuzzleDate;
        
        // Determine future dates (not yet available)
        const isFutureDate = dateStr > todayNYC && !this.isDevelopmentEnvironment();
        
      // Status-specific styles
        let statusStyle = '';
        if (isPuzzleAvailable) {
          switch (puzzleStatus) {
            case 'complete':
              statusStyle = 'background-color: #c6f6d5; color: #22543d; border: 1px solid #9ae6b4;';
              break;
            case 'started':
              statusStyle = 'background-color: #fefcbf; color: #744210; border: 1px solid #f6e05e;';
              break;
            case 'unsolved':
              statusStyle = 'background-color: #e9f5f9; color: #2c5282; border: 1px solid #bee3f8;';
              break;
            default:
              statusStyle = 'background-color: #fff; color: #1a202c; border: 1px solid #e2e8f0;';
          }
        } else {
          // No puzzle available
          statusStyle = 'background-color: #f9f9f9; color: #a0aec0; border: 1px solid #edf2f7;';
        }
        
        // Today and current puzzle styles
        const todayStyle = isToday ? 'font-weight: bold; border: 2px solid #4a5568;' : '';
        const currentStyle = isCurrentPuzzle ? 'box-shadow: 0 0 0 2px #4299e1;' : '';
        
        // Cell classes and attributes
        const cellClass = isPuzzleAvailable ? 'date-cell has-puzzle' : 'date-cell no-puzzle';
        const disabledAttr = (isFutureDate || !isPuzzleAvailable) ? 'disabled="true"' : '';
        const dataAttr = isPuzzleAvailable ? `data-date="${dateStr}" data-status="${puzzleStatus}"` : '';
        
        // Add cell to calendar
        calendarHTML += `
          <div 
            class="${cellClass}" 
            ${dataAttr}
            ${disabledAttr}
            style="
              padding: 0.4rem 0; 
              border-radius: 0.375rem; 
              text-align: center;
              ${isFutureDate || !isPuzzleAvailable ? 'cursor: default; opacity: 0.5;' : 'cursor: pointer;'}
              ${statusStyle}
              ${todayStyle}
              ${currentStyle}
            "
            title="${this.formatDateMonthDay(dateStr)}${isPuzzleAvailable ? ` (${puzzleStatus})` : ' (no puzzle)'}"
          >
            ${day}
          </div>
        `;
      }
      
      calendarHTML += `
        </div>
        <div class="calendar-footer" style="margin-top: 1rem; font-size: 0.8rem; display: flex; justify-content: center; gap: 1rem;">
          <span style="display: flex; align-items: center;">
            <span style="display: inline-block; width: 12px; height: 12px; margin-right: 4px; background-color: #c6f6d5; border: 1px solid #9ae6b4; border-radius: 2px;"></span> Complete
          </span>
          <span style="display: flex; align-items: center;">
            <span style="display: inline-block; width: 12px; height: 12px; margin-right: 4px; background-color: #fefcbf; border: 1px solid #f6e05e; border-radius: 2px;"></span> Started
          </span>
          <span style="display: flex; align-items: center;">
            <span style="display: inline-block; width: 12px; height: 12px; margin-right: 4px; background-color: #e9f5f9; border: 1px solid #bee3f8; border-radius: 2px;"></span> Available
          </span>
        </div>
      `;
      
      // Set the calendar HTML
      container.innerHTML = calendarHTML;
      
      // Add navigation handlers
      this.setupMonthNavigation(container, month, year, availableDates, puzzleStatuses);
      
      // Add date selection handlers
      this.setupDateSelectionHandlers(container);
    }

    /**
     * Format date as "Month Day" (e.g. "March 15")
     * @param {string} dateStr - Date string in YYYY-MM-DD format
     * @returns {string} Formatted date
     */
    formatDateMonthDay(dateStr) {
      // Parse the YYYY-MM-DD string directly
      const [year, month, day] = dateStr.split('-');
      
      // Create a date string that displays as intended
      return `${new Date(Number(year), Number(month) - 1, Number(day)).toLocaleDateString('en-US', { 
        month: 'long', 
        day: 'numeric' 
      })}`;
    }

    /**
     * Sets up month navigation buttons
     * @param {HTMLElement} container - Container element
     * @param {number} currentMonth - Current month view (0-11)
     * @param {number} currentYear - Current year view
     * @param {string[]} availableDates - Available puzzle dates
     * @param {Object} puzzleStatuses - Puzzle completion statuses
     */
    setupMonthNavigation(container, currentMonth, currentYear, availableDates, puzzleStatuses) {
      const prevButton = container.querySelector('.month-nav.prev');
      const nextButton = container.querySelector('.month-nav.next');
      
      // Make buttons more touch-friendly
      const enhanceButton = (button) => {
        button.style.width = '36px';
        button.style.height = '36px';
        button.style.fontSize = '1.5rem';
        button.style.cursor = 'pointer';
        button.style.userSelect = 'none';
        button.style.touchAction = 'manipulation';
      };
      
      enhanceButton(prevButton);
      enhanceButton(nextButton);
      
      // Handler for previous month - Works with both mouse and touch
      const handlePrevMonth = (e) => {
        e.preventDefault();
        e.stopPropagation();
        
        let newMonth = currentMonth - 1;
        let newYear = currentYear;
        
        if (newMonth < 0) {
          newMonth = 11;
          newYear--;
        }
        
        this.renderMonthCalendar(container, newMonth, newYear, availableDates, puzzleStatuses);
      };
      
      // Handler for next month - Works with both mouse and touch
      const handleNextMonth = (e) => {
        e.preventDefault();
        e.stopPropagation();
        
        let newMonth = currentMonth + 1;
        let newYear = currentYear;
        
        if (newMonth > 11) {
          newMonth = 0;
          newYear++;
        }
        
        this.renderMonthCalendar(container, newMonth, newYear, availableDates, puzzleStatuses);
      };
      
      // Add event listeners for both mouse and touch events
      prevButton.addEventListener('click', handlePrevMonth);
      prevButton.addEventListener('touchend', handlePrevMonth);
      
      nextButton.addEventListener('click', handleNextMonth);
      nextButton.addEventListener('touchend', handleNextMonth);
    }

    /**
     * Sets up event handlers for date selection
     * @param {HTMLElement} container - Calendar container
     */
    setupDateSelectionHandlers(container) {
      // Find all date cells with puzzles
      const dateCells = container.querySelectorAll('.date-cell.has-puzzle:not([disabled])');
      
      dateCells.forEach(cell => {
        // Create a visual feedback effect for touch
        cell.style.transition = 'transform 0.1s ease, opacity 0.1s ease';
        
        // Enhanced tap/click area
        cell.style.margin = '1px';
        cell.style.position = 'relative';
        
        // Handler function that works for both click and touch
        const handleDateSelection = async (e) => {
          e.preventDefault();
          e.stopPropagation(); // Prevent closing the calendar immediately
          
          // Visual feedback
          cell.style.transform = 'scale(0.95)';
          cell.style.opacity = '0.8';
          
          // Reset visual feedback after a short delay
          setTimeout(() => {
            cell.style.transform = 'scale(1)';
            cell.style.opacity = '1';
          }, 150);
          
          const targetDate = cell.dataset.date;
          if (!targetDate || targetDate === this.currentPuzzleDate) {
            // Already on this puzzle or invalid date, just close the picker
            container.style.display = 'none';
            return;
          }
          
          // Navigate to selected puzzle
          container.style.display = 'none';
          
          await this.navigateToPuzzle(targetDate);
        };
        
        // Add both mouse and touch event listeners
        cell.addEventListener('click', handleDateSelection);
        cell.addEventListener('touchend', handleDateSelection);
      });
    }

    /**
     * Fetches all available puzzle dates from the API
     * @returns {Promise<string[]>} Array of available dates in YYYY-MM-DD format
     */
    async fetchAvailablePuzzleDates() {
      try {
        const response = await fetch(this.API_ENDPOINT, {
          cache: 'no-store',
          headers: {
            'Cache-Control': 'no-cache',
            'Pragma': 'no-cache'
          }
        });
        
        if (!response.ok) {
          throw new Error('Failed to fetch available puzzles');
        }
        
        const puzzles = await response.json();
        
        // Filter out future puzzles if not in dev environment
        const today = this.getNYCDate();
        const isDevEnvironment = this.isDevelopmentEnvironment();
        
        return puzzles
          .map(puzzle => typeof puzzle === 'string' ? puzzle : (puzzle.puzzleDate || puzzle))
          .filter(date => isDevEnvironment || date <= today)
          .sort();
          
      } catch (error) {
        console.error('Error fetching available puzzles:', error);
        return [];
      }
    }

    /**
     * Scans localStorage to determine completion status for all puzzles
     * @param {string[]} dates - Array of puzzle dates to check
     * @returns {Object} Map of dates to completion status
     */
    getPuzzleCompletionStatuses(dates) {
      const statusMap = {};
      
      dates.forEach(date => {
        const storageKey = `bracketPuzzle_${date}`;
        const savedData = localStorage.getItem(storageKey);
        
        if (!savedData) {
          // No saved data - puzzle exists but not started
          statusMap[date] = 'unsolved';
        } else {
          try {
            const puzzleData = JSON.parse(savedData);
            
            // Check if puzzle is complete
            if (puzzleData.isComplete || !puzzleData.displayState.includes('[')) {
              statusMap[date] = 'complete';
            } else if (this.isPuzzleStarted(puzzleData)) {
              statusMap[date] = 'started';
            } else {
              statusMap[date] = 'unsolved';
            }
          } catch (e) {
            console.error('Error parsing saved puzzle data:', e);
            statusMap[date] = 'unsolved';
          }
        }
      });
      
      return statusMap;
    }

    /**
     * Checks for available puzzles before and after current date
     * @param {string} currentDate - Current puzzle date in YYYY-MM-DD format
     */
    async checkAdjacentPuzzles(currentDate) {
      try {
        // Get current NYC date for availability check
        const nycDate = this.getNYCDate();
        const isDevEnvironment = this.isDevelopmentEnvironment();
        
        // 1. Fetch available puzzles
        const response = await fetch(this.API_ENDPOINT, {
          cache: 'no-store',
          headers: {
            'Cache-Control': 'no-cache',
            'Pragma': 'no-cache'
          }
        });
        
        if (!response.ok) {
          throw new Error('Unable to fetch puzzle list');
        }
        
        const puzzles = await response.json();
        if (!puzzles?.length) {
          console.log('No puzzles available');
          return this.updateAdjacentPuzzles(null, null);
        }
  
        // 2. Extract and normalize dates
        const puzzleDates = puzzles
          .map(puzzle => puzzle.puzzleDate || puzzle.date)
          .filter(Boolean)
          .sort();  // Sort chronologically
        
        // 3. Find current puzzle's position
        const currentIndex = puzzleDates.indexOf(currentDate);
        if (currentIndex === -1) {
          console.warn('Current puzzle not found in available puzzles');
          return this.updateAdjacentPuzzles(null, null);
        }
        
        // 4. Determine previous puzzle (always available)
        const previousPuzzle = currentIndex > 0 ? puzzleDates[currentIndex - 1] : null;
        
        // 5. Determine next puzzle (with availability check)
        let nextPuzzle = null;
        if (currentIndex < puzzleDates.length - 1) {
          const potentialNext = puzzleDates[currentIndex + 1];
          // Allow future puzzles in dev, restrict in production
          if (isDevEnvironment || potentialNext <= nycDate) {
            nextPuzzle = potentialNext;
          }
        }
        
        // 6. Update navigation state
        this.updateAdjacentPuzzles(previousPuzzle, nextPuzzle);
        
      } catch (error) {
        console.error('Error checking adjacent puzzles:', error);
        // Fail gracefully - disable navigation if we can't determine availability
        this.updateAdjacentPuzzles(null, null);
      }
    }
  
    /**
     * Updates navigation state and UI for adjacent puzzles
     * @param {string|null} previous - Previous puzzle date
     * @param {string|null} next - Next puzzle date
     */
    updateAdjacentPuzzles(previous, next) {
      // Update state
      this.adjacentPuzzleDates = { previous, next };
      
      // Update UI
      const prevButton = this.root.querySelector('.nav-button.prev');
      const nextButton = this.root.querySelector('.nav-button.next');
      
      if (prevButton) {
        prevButton.disabled = !previous;
        prevButton.style.opacity = previous ? '1' : '0.3';
        prevButton.style.cursor = previous ? 'pointer' : 'default';
      }
      
      if (nextButton) {
        nextButton.disabled = !next;
        nextButton.style.opacity = next ? '1' : '0.3';
        nextButton.style.cursor = next ? 'pointer' : 'default';
      }
    }
    
    /**
     * Improved navigateToPuzzle method to handle input mode properly
     * @param {string} targetDate - Date to navigate to
     */
    async navigateToPuzzle(targetDate) {
      try {
        console.log('Starting navigation to date:', targetDate);
        this.setNavigationLoading(true);
        
        // Store the previous date before changing
        const previousDate = this.currentPuzzleDate;
        
        // Store the current preferred input mode (from localStorage)
        const preferredInputMode = this.getInputMode();
        
        // Reset mobile input state before navigation
        if (this.isMobile) {
          this.customInputValue = '';
          this.customInputEl = null;
        }

        // Fetch and process new puzzle data
        await this.fetchPuzzleForDate(targetDate);
        
        if (!this.PUZZLE_DATA) {
          throw new Error(`Failed to fetch puzzle for ${targetDate}`);
        }
        
        // Important: Update current puzzle date AFTER successful fetch but BEFORE loading state
        this.currentPuzzleDate = targetDate;
        this.updateURL(targetDate);

        // Check for available adjacent puzzles
        await this.checkAdjacentPuzzles(targetDate);

        // Reset state to prevent leakage
        this.state = {
          displayState: '',
          solvedExpressions: new Set(),
          message: '',
          messageType: null,
          totalKeystrokes: 0,
          minimumKeystrokes: 0,
          activeClues: [],
          hintModeClues: new Set(),
          peekedClues: new Set(),
          megaPeekedClues: new Set(),
          wrongGuesses: 0,
          helpVisible: false,
          previousDisplay: null
        };

        // Try to load saved state for this puzzle with explicit date parameter
        const savedState = await this.loadSavedState(targetDate);
        
        // Initialize game state
        if (savedState && savedState.puzzleDate === targetDate) { // Extra validation
          console.log(`Loading saved state for ${targetDate}`);
          
          // Check if the saved state has an input mode
          const savedInputMode = savedState.inputMode;
          
          // If the puzzle has been started (has progress), respect its saved mode
          // Otherwise use the preferred mode
          const hasPuzzleProgress = this.isPuzzleStarted(savedState);
          
          // Set the input mode based on progress status
          if (hasPuzzleProgress && savedInputMode) {
            this.inputMode = savedInputMode;
          } else {
            this.inputMode = preferredInputMode;
          }
          
          // Update rankCalculator with the correct input mode
          this.rankCalculator.inputMode = this.inputMode;
          
          this.updateGameState({
            displayState: savedState.displayState,
            solvedExpressions: new Set(savedState.solvedExpressions || []),
            totalKeystrokes: savedState.totalKeystrokes || 0,
            minimumKeystrokes: savedState.minimumKeystrokes || this.calculateMinimumKeystrokes(),
            activeClues: this.findActiveClues(savedState.displayState),
            hintModeClues: new Set(savedState.hintModeClues || []),
            peekedClues: new Set(savedState.peekedClues || []),
            megaPeekedClues: new Set(savedState.megaPeekedClues || []),
            wrongGuesses: savedState.wrongGuesses || 0,
            helpVisible: savedState.helpVisible || false,
            previousDisplay: savedState.previousDisplay || null
          });
        } else {
          console.log(`Initializing fresh state for ${targetDate}`);
          
          // For a fresh puzzle, use the preferred input mode
          this.inputMode = preferredInputMode;
          
          // Update rankCalculator with the correct input mode
          this.rankCalculator.inputMode = this.inputMode;
          
          this.updateGameState({
            displayState: this.PUZZLE_DATA.initialPuzzle,
            solvedExpressions: new Set(),
            message: '',
            totalKeystrokes: 0,
            minimumKeystrokes: this.calculateMinimumKeystrokes(),
            activeClues: this.findActiveClues(this.PUZZLE_DATA.initialPuzzle),
            hintModeClues: new Set(),
            peekedClues: new Set(),
            megaPeekedClues: new Set(),
            wrongGuesses: 0,
            helpVisible: false,
            previousDisplay: null
          });
        }
        
        // Reset UI and reattach handlers
        this.root.innerHTML = this.generateInitialHTML();
        this.setupInputHandlers();
        this.setupShareButtons();
        this.setupPuzzleDisplay();
        this.setupNavigationHandlers();
        
        // Render with new state
        this.render();

        this.setupDatePicker();
      } catch (error) {
        console.error('Navigation failed:', error);
        this.showErrorMessage('Failed to load puzzle. Please try again.');
      } finally {
        this.setNavigationLoading(false);
      }
    }
  
    /**
     * Sets the loading state for navigation buttons
     */
    setNavigationLoading(isLoading) {
      const prevButton = this.root.querySelector('.nav-button.prev');
      const nextButton = this.root.querySelector('.nav-button.next');
      
      if (prevButton) {
        prevButton.disabled = isLoading || !this.adjacentPuzzleDates.previous;
        prevButton.style.cursor = isLoading ? 'wait' : 'pointer';
        prevButton.style.opacity = prevButton.disabled ? '0.5' : '1';
      }
      if (nextButton) {
        nextButton.disabled = isLoading || !this.adjacentPuzzleDates.next;
        nextButton.style.cursor = isLoading ? 'wait' : 'pointer';
        nextButton.style.opacity = nextButton.disabled ? '0.5' : '1';
      }
    }
    
    /**
     * Sets up navigation button event handlers
     */
    setupNavigationHandlers() {
      // First remove any existing handlers to prevent duplicates
      this.cleanupEventListeners();
      
      const prevButton = this.root.querySelector('.nav-button.prev');
      const nextButton = this.root.querySelector('.nav-button.next');
      
      // Track if navigation is in progress to prevent double-clicks
      let isNavigating = false;
      
      // Previous puzzle handler
      if (prevButton) {
        prevButton.addEventListener('click', async () => {
          if (isNavigating || !this.adjacentPuzzleDates.previous) return;
          
          try {
            isNavigating = true;
            prevButton.style.cursor = 'wait';
            await this.navigateToPuzzle(this.adjacentPuzzleDates.previous);
          } catch (error) {
            console.error('Navigation failed:', error);
            this.showErrorMessage('Failed to load previous puzzle. Please try again.');
          } finally {
            isNavigating = false;
            prevButton.style.cursor = this.adjacentPuzzleDates.previous ? 'pointer' : 'default';
          }
        });
      }
      
      // Next puzzle handler
      if (nextButton) {
        nextButton.addEventListener('click', async () => {
          if (isNavigating || !this.adjacentPuzzleDates.next) return;
          
          try {
            isNavigating = true;
            nextButton.style.cursor = 'wait';
            await this.navigateToPuzzle(this.adjacentPuzzleDates.next);
          } catch (error) {
            console.error('Navigation failed:', error);
            this.showErrorMessage('Failed to load next puzzle. Please try again.');
          } finally {
            isNavigating = false;
            nextButton.style.cursor = this.adjacentPuzzleDates.next ? 'pointer' : 'default';
          }
        });
      }
    }
  
    /**
     * Removes existing event listeners to prevent duplicates
     */
    cleanupEventListeners() {
      const prevButton = this.root.querySelector('.nav-button.prev');
      const nextButton = this.root.querySelector('.nav-button.next');
      
      if (prevButton) {
        const newPrevButton = prevButton.cloneNode(true);
        prevButton.parentNode.replaceChild(newPrevButton, prevButton);
      }
      
      if (nextButton) {
        const newNextButton = nextButton.cloneNode(true);
        nextButton.parentNode.replaceChild(newNextButton, nextButton);
      }
    }

    /*************
     UI rendering
    **************/
    
    /*
    * Main render method - updates all UI elements based on current state
    */
    render() {
      const elements = this.getDOMElements();
      
      // Early return if required elements aren't found
      if (!this.validateElements(elements)) {
          console.error('Required DOM elements not found');
          return;
      }

      this.renderPuzzleDisplay(elements.puzzleDisplay);
      this.renderSolvedExpressions(elements.expressionsList);
      this.renderMessage(elements.message);
      
      // Handle completion state
      if (this.isPuzzleComplete()) {
          this.renderCompletedPuzzle(elements.puzzleDisplay, elements.inputContainer, elements.keystrokeStats);
      } else {
          this.renderInProgressState(elements.keystrokeStats);
      }
    }

    /**
     * Renders the main puzzle display
     * @param {HTMLElement} displayElement - Puzzle display element
     */
    renderPuzzleDisplay(displayElement) {
      let highlightedState = this.applyActiveClueHighlights(this.state.displayState);
      highlightedState = this.replaceUnderscoreSequences(highlightedState);
      displayElement.innerHTML = highlightedState;
    }

    replaceUnderscoreSequences(text) {
      // Regular expression to match sequences of 2 or more underscores
      return text.replace(/_{2,}/g, (match) => {
        // Calculate width based on number of underscores (em units work well here)
        const width = (match.length * 0.6) + 'em';
        return `<span class="blank-line" style="width: ${width};"></span>`;
      });
    }
  
    /**
     * Renders the solved expressions list
     * @param {HTMLElement} listElement - Expressions list element
     */
    renderSolvedExpressions(listElement) {
      if (!listElement) return;
      
      const solvedHTML = Array.from(this.state.solvedExpressions)
          .map(expression => this._generateSolvedExpressionItemHTML(
              expression, 
              this.PUZZLE_DATA.solutions[expression]
          ))
          .join('');
      
      listElement.innerHTML = solvedHTML;
    }
  
    /**
     * Renders the current message
     * @param {HTMLElement} messageElement - Message element
     */
    renderMessage(messageElement) {
      if (!messageElement) return;
    
      if (this.state.message) {
        messageElement.textContent = this.state.message;
        messageElement.className = `message ${this.state.messageType || 'success'}`;
        
        // Apply error styling if needed
        if (this.state.messageType === 'error') {
          messageElement.style.cssText = `
            padding: 1rem;
            margin: 1rem;
            background-color: #fee2e2;
            color: #ef4444;
            border-radius: 0.375rem;
          `;
        } else {
          messageElement.style.cssText = ''; // Reset styles for non-error messages
        }
      } else {
        messageElement.textContent = '';
        messageElement.className = 'message';
        messageElement.style.cssText = '';
      }
    }
  
    /**
     * Renders the in-progress state
     * @param {HTMLElement} statsElement - Stats element
     */
    renderInProgressState(statsElement) {
      // Hide stats during gameplay
      statsElement.style.display = 'none';
    }

    renderCompletedPuzzle(puzzleDisplay, inputContainer, keystrokeStats) {
      // Validate required elements
      if (!puzzleDisplay || !inputContainer || !keystrokeStats) {
          console.error('Missing required elements for completion rendering');
          return;
      }
    
      // Hide the input container (for both desktop and mobile)
      inputContainer.style.display = 'none';
      
      // Set up the completed state
      this.setupCompletionState(puzzleDisplay);
    
      // Use the puzzle display state; if the completion text isn't already present, prepend it
      const hasCompletionText = this.state.displayState.includes(this.PUZZLE_DATA.completionText);
      const finalDisplayText = hasCompletionText 
          ? this.state.displayState 
          : `<strong>${this.PUZZLE_DATA.completionText}</strong>\n\n${this.state.displayState}`;
      puzzleDisplay.innerHTML = finalDisplayText;
    
      // Remove any existing completion message to avoid duplicates
      const existingCompletionMessage = this.root.querySelector('.completion-message');
      if (existingCompletionMessage) {
          existingCompletionMessage.remove();
      }
      
      // Find the puzzle-content container
      const puzzleContent = this.root.querySelector('.puzzle-content');
      if (!puzzleContent) {
          console.error('Could not find puzzle-content container');
          return;
      }
    
      // Create the completion message
      const completionMessageWrapper = document.createElement('div');
      completionMessageWrapper.className = 'completion-message';
      completionMessageWrapper.innerHTML = this.generateCompletionMessage();
    
      // Insert after puzzle display
      const insertAfterElement = puzzleDisplay;
      if (insertAfterElement && insertAfterElement.nextSibling) {
          puzzleContent.insertBefore(completionMessageWrapper, insertAfterElement.nextSibling);
      } else {
          puzzleContent.appendChild(completionMessageWrapper);
      }
    
      // Show stats container and render completion stats UI
      keystrokeStats.style.display = 'block';
      
      // Calculate stats with wrong guesses
      const efficiency = ((this.state.minimumKeystrokes / this.state.totalKeystrokes) * 100);
      const stats = this.rankCalculator.getDetailedStats(
          efficiency, 
          this.state.peekedClues.size, 
          this.state.megaPeekedClues.size,
          this.state.wrongGuesses  // Include wrong guesses
      );
    
      // Render stats
      keystrokeStats.querySelector('.stats-content').innerHTML = `
          ${this.generateRankDisplay(stats)}
          ${this.generateStatItems(stats)}
      `;
    
      this.attachCompletionHandlers(keystrokeStats);
      
      // Scroll to top
      setTimeout(() => {
        window.scrollTo({
            top: 0,
            behavior: 'smooth'
        });
        this.setupDatePicker();
      }, 100);
    }
  
    renderCompletionStats(keystrokeStats) {
      const efficiency = ((this.state.minimumKeystrokes / this.state.totalKeystrokes) * 100);
      const stats = this.rankCalculator.getDetailedStats(
          efficiency, 
          this.state.peekedClues.size, 
          this.state.megaPeekedClues.size
      );
  
      keystrokeStats.style.display = 'block';
      keystrokeStats.querySelector('.stats-content').innerHTML = `
          ${this.generateRankDisplay(stats)}
          ${this.generateStatItems(stats)}
      `;
  
      this.attachCompletionHandlers(keystrokeStats);
    }

    /**
     * Apply highlighting to active clues with first letter peek
     * @param {string} text - Current puzzle text
     * @returns {string} HTML with highlights applied
     */
    applyActiveClueHighlights(text) {
      const cleanText = text.replace(/<\/?span[^>]*(>|$)/g, '');
      const activeClues = this.findActiveClues(cleanText);
      
      // Sort clues by start position in reverse order to process them from end to beginning
      activeClues.sort((a, b) => b.start - a.start);
      
      let highlightedText = cleanText;
      
      activeClues.forEach(clue => {
        const expressionText = clue.expression.trim();
        
        // Create a precise slice by using the exact indices
        const before = highlightedText.slice(0, clue.start);
        let clueText = highlightedText.slice(clue.start, clue.end);
        const after = highlightedText.slice(clue.end);
    
        // Handle different clue states
        if (!clueText.includes('[') && !this.state.hintModeClues.has(expressionText)) {
          // Already solved clue - leave as is
          highlightedText = before + clueText + after;
        } else {
          // Active or hint mode clue
          if (this.state.hintModeClues.has(expressionText)) {
            const solution = this.PUZZLE_DATA.solutions[expressionText];
            // Show the first letter hint
            if (solution) {
              const firstLetter = solution.charAt(0).toUpperCase();
              // Use exact position for end bracket to avoid issues with special characters
              const lastBracketPos = clueText.lastIndexOf(']');
              if (lastBracketPos !== -1) {
                clueText = clueText.substring(0, lastBracketPos) + ` (${firstLetter})]`;
              }
            }
          }
          
          // Use more robust string manipulation to ensure the entire expression is highlighted
          highlightedText = before + `<span class="active-clue">${clueText}</span>` + after;
        }
      });
    
      return highlightedText;
    }

    /***************
     HTML generation
    ****************/

     generateLoadingHTML() {
      return `
        <div class="loading">
          Loading today's puzzle...
        </div>
      `;
    }    
  
    /**
     * Generates HTML for the input container based on current mode
     * @returns {string} HTML markup
     */
    generateInputContainerHTML() {
      if (this.useCustomKeyboard) {
        if (this.inputMode === 'submit') {
          return `
            <div class="input-container">
              <div class="message"></div>
              <div class="input-submit-wrapper" style="display: flex; gap: 0.5rem; margin-bottom: 0.5rem;">
                <div class="custom-input" style="flex-grow: 1; height: 44px; display: flex; align-items: center;">
                  <span class="placeholder">type any answer...</span>
                </div>
                <button 
                  class="mobile-submit-button" 
                  style="height: 44px; background-color: #2563eb; color: white; padding: 0 1rem; border-radius: 6px; font-weight: 500; border: none;"
                >
                  [enter]
                </button>
              </div>
              <div class="custom-keyboard">
                ${this.generateKeyboardButtonsHTML()}
              </div>
            </div>
          `;
        } else {
          // Classic mode (auto-snap)
          return `
            <div class="input-container">
              <div class="message"></div>
              <div class="custom-input">
                <span class="placeholder">start typing answers...</span>
              </div>
              <div class="custom-keyboard">
                ${this.generateKeyboardButtonsHTML()}
              </div>
            </div>
          `;
        }
      } else {
        // Desktop
        if (this.inputMode === 'submit') {
          return `
            <div class="input-container">
              <div class="input-submit-wrapper" style="display: flex; gap: 0.5rem;">
                <input 
                  type="text" 
                  placeholder="type any answer..." 
                  name="silly-joke" 
                  class="answer-input" 
                  autocomplete="off" 
                  autocapitalize="off"
                >
                <button 
                  class="submit-answer"
                >
                  [enter]
                </button>
              </div>
              <div class="message"></div>
            </div>
          `;
        } else {
          // Classic mode (auto-snap)
          return `
            <div class="input-container">
              <input type="text" placeholder="Start typing answers..." name="silly-joke" class="answer-input" autocomplete="off" autocapitalize="off">
              <div class="message"></div>
            </div>
          `;
        }
      }
    }
    
// Modified generateKeyboardButtonsHTML method
// Modified generateKeyboardButtonsHTML method
generateKeyboardButtonsHTML() {
  const mainLayout = [
    { keys: 'qwertyuiop', columns: 10 },
    { keys: 'asdfghjkl', columns: 10 },
    { keys: [
      { key: '123', display: '123', small: true },
      'z', 'x', 'c', 'v', 'b', 'n', 'm',
      { key: 'backspace', display: '\u{232B}', wide: true }
    ], columns: 10 }
  ];
  
  const altLayout = [
    { keys: '1234567890', columns: 10 },
    { keys: '-/:;()$&@"', columns: 10 },
    { keys: [
      { key: 'ABC', display: 'ABC', small: true },
      '.', ',', '?', '!', "'", "=", "+",
      { key: 'backspace', display: '\u{232B}', wide: true }
    ], columns: 10 }
  ];
  
  const rows = this.isAltKeyboard ? altLayout : mainLayout;
  let html = ``;
  
  rows.forEach((row, rowIndex) => {
    // adding margin for middle row on default layout since it's only 9 keys vs, 10
    const leftPadding = (rowIndex === 1 && !this.isAltKeyboard) ? 'margin-left: 5%' : '';
    html += `<div class="keyboard-row" style="${leftPadding}">`;
    
    const keys = Array.isArray(row.keys) ? row.keys : row.keys.split('');
    keys.forEach(keyItem => {
      if (typeof keyItem === 'string') {
        html += this.generateKeyboardKeyHTML(keyItem);
      } else {
        html += this.generateSpecialKeyHTML(keyItem);
      }
    });
    
    html += '</div>';
  });
  
  html += '</div>';
  return html;
}

generateSpecialKeyHTML(keyItem) {
  const fontSize = keyItem.small ? '0.875rem' : '1.125rem';
  return `
    <button class="keyboard-key" data-key="${keyItem.key}" style="
      grid-column: ${keyItem.wide ? 'span 2' : 'span 1'};
      font-size: ${fontSize};
    ">
      ${keyItem.display}
    </button>
  `;
}
  
  generateKeyboardKeyHTML(key) {
      return `
          <button class="keyboard-key" data-key="${key}">
              ${key}
          </button>
      `;
  }
  
    /**
      * Generates initial HTML structure for the game
      * @returns {string} HTML markup
      */
    generateInitialHTML() {
        const displayDate = this.currentPuzzleDate || this.getNYCDate();
        
        return `
          <div class="puzzle-container">
            ${this.generateHeaderHTML(displayDate)}
            <div class="puzzle-content">
              ${this.generatePuzzleDisplayHTML()}
              ${this.generateInputContainerHTML()}
              ${this.generateStatsContainerHTML()}
              ${this.generateSolvedExpressionsHTML()}
            </div>
          </div>
        `;
    }

    generateHeaderHTML(displayDate) {
      return `
          <div class="puzzle-header">
              <button class="info-button">!</button>
              <h1>[Bracket Shitty]</h1>
              <button class="help-button">?</button>
              <div class="nav-container">
                  <button class="nav-button prev" ${!this.isMobile && 'disabled'}>&larr;</button>
                  <div class="puzzle-date">${this.formatNYCDate(displayDate)}</div>
                  <button class="nav-button next" ${!this.isMobile && 'disabled'}>&rarr;</button>
              </div>
          </div>
      `;
    }

    generatePuzzleDisplayHTML() {
        return `<div class="puzzle-display"></div>`;
    }

    generateStatsContainerHTML() {
        return `
            <div class="keystroke-stats" style="display: none;">
                <div class="stats-content"></div>
                <div class="share-buttons">
                    <button class="share-button copy">Copy Results</button>
                    <button class="share-button sms">Share via Text</button>
                    <button class="share-button reset">Reset Puzzle</button>
                </div>
                <div class="share-message" style="display: none;">Results copied to clipboard!</div>
            </div>
        `;
    }

    _generateSolvedExpressionItemHTML(expression, solution) {
      return `
          <div class="expression-item">
              <span class="expression">[${expression}]</span> = 
              <span class="solution">${solution}</span>
          </div>
      `;
    }

    generateSolvedExpressionsHTML() {
      // Convert Set to Array and reverse it to get most recent first
      const expressionsList = Array.from(this.state.solvedExpressions)
          .reverse()
          .map(expression => this._generateSolvedExpressionItemHTML(
              expression, 
              this.PUZZLE_DATA.solutions[expression]
          ))
          .join('');
    
      return `
          <div class="solved-expressions">
              <h3></h3>
              <div class="expressions-list">
                  ${expressionsList}
              </div>
          </div>
      `;
    }

    generateCompletionMessage() {
      return `
        <div class="message success completion-message">
          <span><strong>** Puzzle Solved! **</strong></span><br>
          <a href="${this.PUZZLE_DATA.completionURL}" 
            target="_blank" 
            rel="noopener noreferrer"
            class="completion-url">
            ${this.PUZZLE_DATA.completionURL}
          </a>
        </div>
      `;
    }

    generateRankDisplay(stats) {
      const margin = this.isMobile ? '0.5rem 0' : '1rem 0';
      const padding = this.isMobile ? '1rem' : '1.5rem';
      
      // Custom messages for certain ranks
      let nextRankMessage = '';
      
      if (stats.rank === 'Puppet Master') {
        nextRankMessage = '<br><span class="next-rank">now I am become Death, the destroyer of worlds</span>';
      } else if (stats.rank === 'Kingmaker') {
        nextRankMessage = '<br><span class="next-rank">ah finally the very top</span>';
      } else if (stats.rank === 'Mayor') {
        nextRankMessage = '<br><span class="next-rank">nobody\'s my boss, right?</span>';
      } else if (stats.rank === 'Power Broker') {
        nextRankMessage = '<br><span class="next-rank">this has got to be it...</span>';
      } else if (stats.nextRankName) {
        nextRankMessage = `<br><span class="next-rank">Almost: ${stats.nextRankName} (${Math.ceil(stats.pointsToNextRank)} points needed)</span>`;
      }
      
      return `
        <div class="rank-display" data-rank="${stats.rank}">
          You are ${(stats.rank === 'Chief of Police' || stats.rank === 'Mayor') ? 'the' : 'a'} Bracket Shitty<br>
          ${stats.rankEmoji} <b>${stats.rank}</b> ${stats.rankEmoji}
          ${nextRankMessage}
          <div class="share-hint">
            ${this.isMobile ? '(tap to share via text)' : '(click to copy results)'}
          </div>
        </div>
      `;
    }
  

    /**
     * Updated generateStatItems method to show different stats based on mode
     */
    generateStatItems(stats) {
      // Generate progress bar for score
      const segments = 10;
      const filledSegments = Math.round((stats.finalScore / 100) * segments);
      const emptySegments = segments - filledSegments;
      
      // Use purple squares for Puppet Master, otherwise use color based on score
      let segmentEmoji;
      if (stats.rank === 'Puppet Master') {
        segmentEmoji = '\u{1F7EA}'; // Purple Square emoji
      } else {
        segmentEmoji = stats.finalScore < 25 ? '\u{1F7E5}' :  // Red
                      stats.finalScore < 75 ? '\u{1F7E8}' :  // Yellow
                                            '\u{1F7E9}';     // Green
      }
      
      const progressBar = segmentEmoji.repeat(filledSegments) + '\u{2B1C}'.repeat(emptySegments);
      
      // Score bar HTML
      const scoreBarHTML = `
        <div class="score-bar">
          <div class="score-value">Score: ${stats.finalScore.toFixed(1)}</div>
          <div class="progress-bar">${progressBar}</div>
          ${this.inputMode === 'classic' ? '<div class="hard-mode-indicator" style="font-size: 1rem; text-align: center; margin-top: 5px; font-weight: bold;">\u{2620}\u{FE0F} hard mode!</div>' : ''}
        </div>
      `;
      
      // For classic mode (keystroke-based)
      if (this.inputMode === 'classic') {
        return `
          ${scoreBarHTML}
          <div class="stat-items">
            <div class="stat-item">Total keystrokes: ${this.state.totalKeystrokes}</div>
            <div class="stat-item">Minimum keystrokes needed: ${this.state.minimumKeystrokes}</div>
            <div class="stat-item">Excess keystrokes: ${this.state.totalKeystrokes - this.state.minimumKeystrokes}</div>
            <div class="stat-item">\u{1F440} Clues peeked: ${this.state.peekedClues.size}</div>
            <div class="stat-item">\u{1F6DF} Answers revealed: ${this.state.megaPeekedClues.size}</div>
            <div class="stat-item"><b>Score breakdown</b>
              <br>Base score (efficiency): ${stats.baseScore.toFixed(1)}
              <br>Peek penalty: -${stats.peekPenalty.toFixed(1)}
              ${stats.megaPeekPenalty > 0 ? `<br>Reveal penalty: -${stats.megaPeekPenalty.toFixed(1)}` : ''}
              <br>Final score: ${stats.finalScore.toFixed(1)}
            </div>
          </div>
        `;
      } 
      // For submit mode (wrong-guess-based)
      else {
        return `
          ${scoreBarHTML}
          <div class="stat-items">
            <div class="stat-item">\u{274C} Wrong guesses: ${this.state.wrongGuesses || 0}</div>
            <div class="stat-item">\u{1F440} Clues peeked: ${this.state.peekedClues.size}</div>
            <div class="stat-item">\u{1F6DF} Answers revealed: ${this.state.megaPeekedClues.size}</div>
            <div class="stat-item"><b>Score breakdown</b>
              <br>Base score: ${stats.baseScore.toFixed(1)}
              ${stats.wrongGuessPenalty > 0 ? `<br>Wrong guess penalty: -${stats.wrongGuessPenalty.toFixed(1)}` : ''}
              <br>Peek penalty: -${stats.peekPenalty.toFixed(1)}
              ${stats.megaPeekPenalty > 0 ? `<br>Reveal penalty: -${stats.megaPeekPenalty.toFixed(1)}` : ''}
              <br>Final score: ${stats.finalScore.toFixed(1)}
            </div>
            <div class="stat-item">Total keystrokes: ${this.state.totalKeystrokes}</div>
            <div class="stat-item">Minimum keystrokes needed: ${this.state.minimumKeystrokes}</div>
            <div class="stat-item">Excess keystrokes: ${this.state.totalKeystrokes - this.state.minimumKeystrokes}</div>
          </div>
        `;
      }
    }

    /**************************
     UI setup and configuration
    ***************************/

    /**
     * Sets up puzzle display and help system
     */
    setupPuzzleDisplay() {

        const puzzleDisplay = this.root.querySelector('.puzzle-display');
      if (!puzzleDisplay) {
        console.error('Puzzle display element not found');
        return;
      }

      this.configurePuzzleDisplayLayout(puzzleDisplay);
      this.setupHelpSystem();
      this.setupInfoSystem();
      this.setupClueClickHandlers(puzzleDisplay);

      // Add header click handler
      const header = this.root.querySelector('.puzzle-header h1');
      if (header) {
        header.addEventListener('click', async () => {
          const todayDate = this.getNYCDate();
          if (this.currentPuzzleDate !== todayDate) {
            await this.navigateToPuzzle(todayDate);
          }
        });
      }
    }
  
    /**
     * Configures puzzle display layout based on device
     * @param {HTMLElement} puzzleDisplay - The puzzle display element
     */
    configurePuzzleDisplayLayout(puzzleDisplay) {
      const isMobile = /Android|webOS|iPhone|iPad|iPod|BlackBerry|IEMobile|Opera Mini/i.test(navigator.userAgent);
      puzzleDisplay.classList.add(isMobile ? 'puzzle-display-mobile' : 'puzzle-display-desktop');
    }
    

    /**
     * Checks if the user has seen the current info version
     * @returns {boolean} Whether the user has seen the current info
     */
    hasSeenInfo() {
      // Check if the user has seen the current version of the info
      return localStorage.getItem('bracketCityInfoSeen') === this.INFO_VERSION;
    }

    /**
     * Marks the current info version as seen
     */
    markInfoAsSeen() {
      // Store the current version instead of just 'true'
      localStorage.setItem('bracketCityInfoSeen', this.INFO_VERSION);
    }

    /**
     * Sets up the info system with an inline toggle switch
     */
    setupInfoSystem() {
      // Define the current info version - increment this to reset for all users
      this.INFO_VERSION = '4'; // Increment version for the new layout
      
      const infoButton = this.root.querySelector('.info-button');
      if (!infoButton) return;

      // Check and apply the initial button state
      if (!this.hasSeenInfo()) {
        infoButton.classList.add('unseen-info');
      }
      
      // CSS for the iOS-style toggle
      const toggleStyle = `
        <style>
          /* iOS Toggle Switch */
          .inline-toggle {
            position: relative;
            display: inline-block;
            width: 36px;
            height: 20px;
            vertical-align: middle;
            margin: 0 6px;
          }
          
          .inline-toggle input {
            opacity: 0;
            width: 0;
            height: 0;
          }
          
          .toggle-slider {
            position: absolute;
            cursor: pointer;
            top: 0;
            left: 0;
            right: 0;
            bottom: 0;
            background-color: #ccc;
            transition: .3s;
            border-radius: 34px;
          }
          
          .toggle-slider:before {
            position: absolute;
            content: "";
            height: 16px;
            width: 16px;
            left: 2px;
            bottom: 2px;
            background-color: white;
            transition: .2s;
            border-radius: 50%;
            box-shadow: 0 1px 3px rgba(0,0,0,0.2);
          }
          
          input:checked + .toggle-slider {
            background-color: #2563eb;
          }
          
          input:disabled + .toggle-slider {
            background-color: #d1d5db;
            cursor: not-allowed;
          }
          
          input:checked:disabled + .toggle-slider {
            background-color: #93c5fd;
          }
          
          input:checked + .toggle-slider:before {
            transform: translateX(16px);
          }
          
          .mode-disabled-message {
            display: none;
          }
          
          .toggle-disabled .mode-disabled-message {
            display: inline;
          }
        </style>
      `;
      
      // Build the info content with the toggle at the top

      // old news
      //<div style="margin: 0.9em 0; font-size: 0.85em;">* clicking a <mark style="background-color: #fff9c4;">clue</mark> lets you peek at the answer's first letter</div>
      //<div style="margin: 0.9em 0; font-size: 0.85em;">* clicking <span class='help-icon'>?</span> takes you to an interactive tutorial</div>
        
      const infoContent = `
        ${toggleStyle}
        <div id="toggle-container" style="text-align: center; margin: 0.4em 0 1em 0;">
          <div style="display: flex; flex-direction: column; align-items: center; gap: 0;">
            <div style="display: flex; align-items: center; justify-content: center; gap: 6px;">
              <span style="font-weight: 500; font-size: 0.95em;">hard mode</span>
              <label class="inline-toggle" onclick="event.stopPropagation();" style="margin: 0 0 0 2px;">
                <input type="checkbox" id="mode-toggle" onclick="event.stopPropagation();">
                <span class="toggle-slider"></span>
              </label>
            </div>
            <div style="font-size: 0.7em; line-height: 1; margin-top: 1px; display: flex; justify-content: center; align-items: center; flex-wrap: wrap; gap: 4px;">
              <span style="font-style: italic; color: #6b7280;">no submit button & every keystroke counts!</span>
              <span class="mode-disabled-message" style="color: #ef4444; font-weight: 500;">
                (can't change once a puzzle started)
              </span>
            </div>
          </div>
        </div>
        
        <div style="margin: 0.9em 0; font-size: 0.85em;">* there is now a <mark style="background-color:rgba(255,255,0,0.2)">date picker</mark> - just click the date in the header and browse the archive</div>
      `;

      infoButton.addEventListener('click', (e) => {
        e.stopPropagation();
        
        // Mark as seen when clicked
        if (!this.hasSeenInfo()) {
          this.markInfoAsSeen();
          infoButton.classList.remove('unseen-info');
        }
        
        this.toggleHelp(infoContent);
        
        // Add event listener to the toggle switch
        setTimeout(() => {
          const modeToggle = document.getElementById('mode-toggle');
          const toggleContainer = document.getElementById('toggle-container');
          
          if (!modeToggle || !toggleContainer) return;
          
          // Always evaluate current puzzle state
          const hasPuzzleStarted = this.isPuzzleStarted();
          
          // Set the checked state to match the current inputMode
          modeToggle.checked = this.inputMode === 'classic';
          
          // Update the disabled state and container class based on current puzzle state
          modeToggle.disabled = hasPuzzleStarted;
          toggleContainer.className = hasPuzzleStarted ? 'toggle-disabled' : '';
          
          if (!modeToggle.disabled) {
            modeToggle.addEventListener('change', (e) => {
              e.stopPropagation();
              
              // Double-check puzzle state before changing mode
              if (this.isPuzzleStarted()) {
                // If puzzle has been started since info panel opened, disable toggle and show message
                modeToggle.disabled = true;
                toggleContainer.className = 'toggle-disabled';
                // Reset toggle to match current mode
                modeToggle.checked = this.inputMode === 'classic';
                return;
              }
              
              // Update mode based on toggle state
              // ON (checked) = classic mode, OFF (unchecked) = submit mode
              const newMode = modeToggle.checked ? 'classic' : 'submit';
              this.setInputMode(newMode);
            });
          }
          
          // Make sure all interactive elements stop propagation
          toggleContainer.addEventListener('click', (e) => {
            // Only stop propagation if clicking on the toggle itself
            if (e.target === modeToggle || e.target.closest('.inline-toggle')) {
              e.stopPropagation();
            }
          });
          
          // Add MutationObserver to update toggle state when puzzle state changes
          this.setupToggleStateObserver(modeToggle, toggleContainer, isPuzzleStarted);
        }, 100);
      });
    }

    /**
     * Checks if the user is a new player by examining localStorage
     * @returns {boolean} True if this appears to be a new player
     */
    isNewPlayer() {
      let count = 0;
    
      // Count the number of localStorage keys that start with 'bracketCity'
      for (let i = 0; i < localStorage.length; i++) {
        const key = localStorage.key(i);
        if (key && key.startsWith('bracketPuzzle')) {
          count++;
        }
      }
      
      // If more than one key is found, the player is not new
      if (count > 1 || localStorage.getItem('tutorialSeen')) {
        return false;
      }      
      
      // Otherwise, the player is considered new
      return true;
    }

    /**
     * Sets up the help/tutorial system with special highlighting for new players
     */
    setupHelpSystem() {
      const helpButton = this.root.querySelector('.help-button');
      if (!helpButton) return;

      // Check if this is a new player to determine if we should highlight the tutorial button
      if (this.isNewPlayer()) {
        helpButton.classList.add('new-player-highlight');
      }

      helpButton.addEventListener('click', (e) => {
        e.stopPropagation();
        
        // Remove the highlighting when clicked
        helpButton.classList.remove('new-player-highlight');
        
        if (confirm('Would you like to start the tutorial?')) {
          localStorage.setItem('tutorialSeen', 'true');
          this.saveState();
          window.location.href = 'tutorial';
        }
      });
    }

    turnOffTutorialPulse() {
      const helpButton = this.root.querySelector('.help-button');
      if (helpButton) {
        helpButton.classList.remove('new-player-highlight');
      }
      localStorage.setItem('tutorialSeen', 'true');   
    }
    
    /**
     * Toggles the help display with special handling for interactive elements
     * @param {string} helpContent - The help content to display
     */
    toggleHelp(helpContent) {
      const puzzleDisplay = this.root.querySelector('.puzzle-display');
      if (!puzzleDisplay) return;

      // Check if the puzzle is completed
      const isPuzzleCompleted = puzzleDisplay.classList.contains('completed');

      // Get the info button
      const infoButton = this.root.querySelector('.info-button');

      // Store current puzzle state when help is shown
      if (!this.state.helpVisible) {
        // Opening help
        this.updateGameState({
          helpVisible: true,
          previousDisplay: puzzleDisplay.innerHTML
        });
        
        if (isPuzzleCompleted) {
          // In completed state, just set content without scroll anchor
          puzzleDisplay.innerHTML = helpContent;
          // Don't auto-scroll in completed state
        } else {
          // Normal in-progress behavior with scroll anchor
          puzzleDisplay.innerHTML = '<div id="help-scroll-anchor"></div>' + helpContent;
          
          // Scroll to anchor - works reliably on iOS Safari
          const scrollAnchor = document.getElementById('help-scroll-anchor');
          if (scrollAnchor) {
            scrollAnchor.scrollIntoView({block: 'start', behavior: 'auto'});
          }
        }

        // Change the info button text from "!" to "X"
        if (infoButton) {
          infoButton.textContent = "X";
        }

        // Disable all inputs including submit button
        this.disableInputsForHelp();
        
        // Setup click handler for the puzzle display that closes help
        // But don't close when clicking on interactive elements
        puzzleDisplay.addEventListener('click', this.handleHelpClick = (event) => {
          // Check if the click was on an interactive element
          const isInteractiveElement = 
            event.target.tagName === 'INPUT' || 
            event.target.tagName === 'BUTTON' || 
            event.target.tagName === 'LABEL' || 
            event.target.tagName === 'A' ||
            event.target.classList.contains('toggle-container') ||
            event.target.classList.contains('toggle-labels') ||
            event.target.classList.contains('toggle-slider') ||
            event.target.closest('label') || 
            event.target.closest('a') ||
            event.target.closest('.toggle-container');
          
          // Only close help if it wasn't an interactive element
          if (!isInteractiveElement) {
            this.closeHelp();
          }
        });
        
      } else {
        this.closeHelp();
      }
    }

    /**
     * Closes the help panel and restores previous state
     */
    closeHelp() {
      const puzzleDisplay = this.root.querySelector('.puzzle-display');
      if (!puzzleDisplay) return;
      
      // Change the info button text back from "X" to "!"
      const infoButton = this.root.querySelector('.info-button');
      if (infoButton) {
        infoButton.textContent = "!";
      }
      
      // Remove the click handler to prevent memory leaks
      if (this.handleHelpClick) {
        puzzleDisplay.removeEventListener('click', this.handleHelpClick);
        this.handleHelpClick = null;
      }
      
      // Closing help - restore previous state and re-render
      this.updateGameState({
        helpVisible: false,
        previousDisplay: null
      });
      
      // Re-render the entire puzzle state
      this.render();

      // Re-enable input
      const desktopInput = this.root.querySelector('.answer-input');
      if (desktopInput) {
        desktopInput.disabled = false;
      }
      
      // Re-enable submit button (desktop)
      const submitButton = this.root.querySelector('.submit-answer');
      if (submitButton) {
        submitButton.disabled = false;
        submitButton.style.opacity = '1';
        submitButton.style.pointerEvents = 'auto';
      }
      
      // For mobile, re-enable keyboard and input
      if (this.isMobile) {
        // Restore custom input appearance
        const customInput = this.root.querySelector('.custom-input');
        if (customInput) {
          customInput.style.opacity = '1';
          customInput.style.pointerEvents = 'auto';
        }
        
        // Re-enable all keyboard keys
        const keyboardKeys = this.root.querySelectorAll('.keyboard-key');
        keyboardKeys.forEach(key => {
          key.disabled = false;
          key.style.opacity = '1';
          key.style.pointerEvents = 'auto';
        });
        
        // Re-enable mobile submit button
        const mobileSubmitButton = this.root.querySelector('.mobile-submit-button');
        if (mobileSubmitButton) {
          mobileSubmitButton.disabled = false;
          mobileSubmitButton.style.opacity = '1';
          mobileSubmitButton.style.pointerEvents = 'auto';
        }
      }
      
      // Restore focus when closing help (but only if not disabled)
      if (desktopInput && !desktopInput.disabled) {
        desktopInput.focus();
      }
    }

    /**
     * Sets up click handlers for puzzle clues
     * @param {HTMLElement} puzzleDisplay - The puzzle display element
     */
    setupClueClickHandlers(puzzleDisplay) {
      puzzleDisplay.addEventListener('click', (event) => {
        // If help is showing, check if we clicked on a link before closing
        if (this.state.helpVisible) {
          // Check if the click was on a link (or its descendants)
          const isLinkClick = event.target.tagName === 'A' || 
                              event.target.closest('a');
          
          // Only close help if it wasn't a link click
          if (!isLinkClick) {
            this.toggleHelp();
          }
          return;
        }
    
        const clueElement = event.target.closest('.active-clue');
        if (!clueElement) return;
    
        // Find the clicked expression
        const cleanText = this.state.displayState.replace(/<\/?[^>]+(>|$)/g, '');
        const activeClues = this.findActiveClues(cleanText);
        const cluePosition = Array.from(puzzleDisplay.querySelectorAll('.active-clue')).indexOf(clueElement);
        
        if (cluePosition >= 0 && cluePosition < activeClues.length) {
          const expression = activeClues[cluePosition].expression.trim();
          this.toggleHintMode(expression);
    
          // Return focus to input on desktop
          if (!this.isMobile && this.answerInput) {
            this.answerInput.focus();
          }
        }
      });
    }

    setupCompletionState(puzzleDisplay) {
      puzzleDisplay.classList.add('completed');

      // scroll to bottom fix
      document.body.style.overflow = 'auto';

      if (this.isMobile) {
          // Reduce bottom padding of puzzle display
          puzzleDisplay.style.paddingBottom = '0.5rem';
          
          // Adjust puzzle container styles for mobile
          const container = this.root.querySelector('.puzzle-container');
          if (container) {
              Object.assign(container.style, {
                  position: 'relative',
                  height: '100vh',
                  overflowY: 'auto',
                  display: 'block'
              });
          }
          
          // Reduce top margin and padding in the content area to decrease vertical space
          const content = this.root.querySelector('.puzzle-content');
          if (content) {
              Object.assign(content.style, {
                  position: 'relative',
                  top: '1.3rem',    
                  height: 'auto',
                  marginTop: '60px', // reduced from 85px
                  padding: '0.5rem',
                  paddingBottom: '1rem',
                  overflowY: 'visible'
              });
          }
      }
    }

    attachCompletionHandlers(keystrokeStats) {
      // Rank display click handler
      const rankDisplay = keystrokeStats.querySelector('.rank-display');
      if (rankDisplay) {
          rankDisplay.addEventListener('click', () => {
              if (this.isMobile) {
                  this.shareSMS();
              } else {
                  this.shareResults();
              }
          });
      }
    }

    setupShareButtons() {
      const copyButton = this.root.querySelector('.share-button.copy');
      const smsButton = this.root.querySelector('.share-button.sms');
      const resetButton = this.root.querySelector('.share-button.reset');
      
      copyButton.addEventListener('click', () => this.shareResults());
      
      if (!/Android|iPhone|iPad|iPod/i.test(navigator.userAgent)) {
        smsButton.style.display = 'none';
      }
      smsButton.addEventListener('click', () => this.shareSMS());
      resetButton.addEventListener('click', () => this.resetPuzzle());
      
    }

    /**************************
     Message system
    ***************************/

    /**
     * Updates the game message with proper state management
     * @param {string} text - Message text to display
     * @param {string} type - Message type ('success' or 'error')
     */
    showMessage(text, type = 'success') {
      const messageEl = this.root.querySelector('.message');
      if (!messageEl) return;
    
      // Update state
      this.updateGameState({
        message: text,
        messageType: type
      });
    
      // Update message element
      messageEl.textContent = text;
      messageEl.className = `message ${type}`;
      messageEl.style.display = 'block'; // Ensure message is visible
    
      // Auto-clear ALL messages (both success and error)
      setTimeout(() => {
        // Only clear if this message is still showing
        if (this.state.message === text) {
          this.updateGameState({
            message: '',
            messageType: null
          });
          messageEl.textContent = '';
          messageEl.className = 'message';
          messageEl.style.display = 'none';
        }
      }, 1500);
    }

    /**
     * Shows an error message with special handling
     * @param {string} message - Error message to display
     */
    showErrorMessage(message) {
      const messageEl = this.root.querySelector('.message') || document.createElement('div');
      
      // If we need to create a new message element
      if (!messageEl.parentNode) {
        messageEl.className = 'message';
        this.root.prepend(messageEl);
      }

      // Update state to track error
      this.updateGameState({
        message,
        messageType: 'error',
        lastError: {
          message,
          timestamp: Date.now()
        }
      });
    }

    
    /**************************
     Share system
    ***************************/

    /**
     * Modified generateShareText to only display mode when in classic/hard mode
     */
    generateShareText() {
      const puzzleDate = this.formatNYCDate(this.currentPuzzleDate);
      const efficiency = ((this.state.minimumKeystrokes / this.state.totalKeystrokes) * 100);
      
      // Calculate rank and stats
      const stats = this.rankCalculator.getDetailedStats(
        efficiency,
        this.state.peekedClues.size,
        this.state.megaPeekedClues.size,
        this.state.wrongGuesses || 0
      );
      
      // Generate progress bar
      const segments = 10;
      const filledSegments = Math.round((stats.finalScore / 100) * segments);
      const emptySegments = segments - filledSegments;
      
      // Use purple squares for Puppet Master, otherwise use color based on score
      let segmentEmoji;
      if (stats.rank === "Puppet Master") {
        segmentEmoji = '\u{1F7EA}'; // Purple Square emoji
      } else {
        segmentEmoji = stats.finalScore < 25 ? '\u{1F7E5}' :  // Red
                      stats.finalScore < 75 ? '\u{1F7E8}' :  // Yellow
                                            '\u{1F7E9}';     // Green
      }
      
      const progressBar = segmentEmoji.repeat(filledSegments) + '\u{2B1C}'.repeat(emptySegments);
      
      // Build share text with specific line breaks to match desired layout
      const shareItems = [
        '[Bracket Shitty]',
        puzzleDate,
        '',
        'https://user.4574.co.uk/bs',
        ''
      ];
      
      // Only show "hard mode" message if in classic mode
      if (this.inputMode === 'classic') {
        shareItems.push('\u{2620}\u{FE0F} hard mode!');
        shareItems.push(''); // Add a line break between mode and rank
      }
      
      // Add rank
      shareItems.push(`Rank: ${stats.rankEmoji} (${stats.rank})`);
      
      // Only include mode-specific stats
      // Replace it with this updated logic:
      if (this.inputMode === 'classic' || stats.rank === 'Puppet Master') {
        // Show keystroke stats in classic mode or for any Puppet Master
        shareItems.push(`\u{1F3B9} Total Keystrokes: ${this.state.totalKeystrokes}`);
        shareItems.push(`\u{1F3AF} Minimum Required: ${this.state.minimumKeystrokes}`);
      } else {
        // Submit mode stats
        shareItems.push(`\u{274C} Wrong guesses: ${this.state.wrongGuesses || 0}`);
      }
      
      // Add peek and reveal counts only if they exist
      if (this.state.peekedClues.size > 0) {
        shareItems.push(`\u{1F440} Peeks: ${this.state.peekedClues.size}`);
      }
      
      if (this.state.megaPeekedClues.size > 0) {
        shareItems.push(`\u{1F6DF} Answers Revealed: ${this.state.megaPeekedClues.size}`);
      }
      
      // Remove the input mode indicator - we now only show it for classic mode above the rank
      
      // Add score and progress bar with an empty line before score
      shareItems.push('');
      shareItems.push(`Total Score: ${stats.finalScore.toFixed(1)}`);
      shareItems.push(progressBar);
      
      return shareItems.join('\n');
    }
  
    /**
     * Handles sharing results via the user's preferred app using Web Share API
     * Falls back to SMS URL scheme if Web Share API is not available
     */
    shareSMS() {
      const shareText = this.generateShareText();
      
      // Check if Web Share API is available
      if (navigator.share) {
        navigator.share({
          title: 'Bracket Shitty Results',
          text: shareText
        })
        .then(() => {
          console.log('Successfully shared results');
        })
        .catch((error) => {
          console.error('Error sharing results:', error);
          // Fall back to SMS URL scheme if sharing fails
          this.fallbackToSMS(shareText);
        });
      } else {
        // Fall back to SMS URL scheme for older browsers
        this.fallbackToSMS(shareText);
      }
    }

    /**
     * Fallback method to use SMS URL scheme when Web Share API is not available
     * @param {string} shareText - The text to share
     */
    fallbackToSMS(shareText) {
      const encodedText = encodeURIComponent(shareText);
      
      // Use correct SMS URL scheme format
      window.location.href = `sms:?&body=${encodedText}`;
    }

    /**
 * Copies results to clipboard for desktop sharing
 */
shareResults() {
  const shareText = this.generateShareText();
  
  // Use navigator.clipboard API for modern browsers
  if (navigator.clipboard) {
    navigator.clipboard.writeText(shareText)
      .then(() => {
        // Show success message
        const shareMessage = this.root.querySelector('.share-message');
        if (shareMessage) {
          shareMessage.style.display = 'block';
          setTimeout(() => {
            shareMessage.style.display = 'none';
          }, 2000);
        }
      })
      .catch(err => {
        console.error('Failed to copy results: ', err);
        // Show error or fallback to execCommand
        this.fallbackCopyToClipboard(shareText);
      });
  } else {
    // Fallback for older browsers
    this.fallbackCopyToClipboard(shareText);
  }
}

/**
 * Fallback method to copy text to clipboard using execCommand
 * @param {string} text - Text to copy
 */
fallbackCopyToClipboard(text) {
  try {
    const textArea = document.createElement('textarea');
    textArea.value = text;
    textArea.style.position = 'fixed';  // Avoid scrolling to bottom
    document.body.appendChild(textArea);
    textArea.focus();
    textArea.select();
    
    const successful = document.execCommand('copy');
    if (successful) {
      // Show success message
      const shareMessage = this.root.querySelector('.share-message');
      if (shareMessage) {
        shareMessage.style.display = 'block';
        setTimeout(() => {
          shareMessage.style.display = 'none';
        }, 2000);
      }
    } else {
      console.error('execCommand failed');
    }
    
    document.body.removeChild(textArea);
  } catch (err) {
    console.error('Fallback copy method failed: ', err);
  }
}

    /**************************
     Utility methods
    ***************************/
      
    /**
     * Resets the input container to initial state
     * @param {HTMLElement} inputContainer - The input container element
     */
    resetInputContainer(inputContainer) {
      inputContainer.innerHTML = `
        <input type="text" placeholder="type any answer..." class="answer-input" autocomplete="off" autocapitalize="off">
        <div class="message"></div>
      `;
      // Re-setup input handlers for the new input element
      this.setupInputHandlers();
    }
  
    /**
     * Gets all required DOM elements
     * @returns {Object} Object containing DOM elements
     */
    getDOMElements() {
      return {
        puzzleDisplay: this.root.querySelector('.puzzle-display'),
        expressionsList: this.root.querySelector('.expressions-list'),
        inputContainer: this.root.querySelector('.input-container'),
        keystrokeStats: this.root.querySelector('.keystroke-stats'),
        message: this.root.querySelector('.message')
      };
    }
  
    /**
     * Validates that all required elements exist
     * @param {Object} elements - DOM elements object
     * @returns {boolean} Whether all required elements exist
     */
    validateElements(elements) {
      return Object.values(elements).every(element => element !== null);
    }
  
    /**
     * Checks if the puzzle is complete
     * @returns {boolean} Whether the puzzle is complete
     */

    isPuzzleComplete() {
      return !this.state.displayState.includes('[');
    } 

    cleanupEventListeners() {
      const prevButton = this.root.querySelector('.nav-button.prev');
      const nextButton = this.root.querySelector('.nav-button.next');
      if (prevButton) prevButton.replaceWith(prevButton.cloneNode(true));
      if (nextButton) nextButton.replaceWith(nextButton.cloneNode(true));
    }

    /**
     * Creates and displays an announcement modal
     * @param {string} title - The title of the announcement
     * @param {string} message - The message content (can include HTML)
     * @param {string} buttonText - The text for the dismiss button
     * @param {string} modalId - Unique identifier for the modal
     * @returns {boolean} Whether the modal was shown
     */
    showAnnouncementModal(title, message, buttonText = 'Got it', modalId) {
      // Check if we should show this modal
      if (!modalId || this.hasSeenAnnouncement(modalId)) {
        return false;
      }
      
      // Create modal HTML
      const modalHTML = `
        <div class="announcement-modal-overlay" id="announcement-overlay">
          <div class="announcement-modal">
            <div class="announcement-modal-title">${title}</div>
            <div class="announcement-modal-content">${message}</div>
            <button class="announcement-modal-button" id="announcement-dismiss">${buttonText}</button>
          </div>
        </div>
      `;
      
      // Append to the body
      document.body.insertAdjacentHTML('beforeend', modalHTML);
      
      // Get DOM references
      const overlay = document.getElementById('announcement-overlay');
      const dismissButton = document.getElementById('announcement-dismiss');
      
      // Add event listeners
      dismissButton.addEventListener('click', () => {
        this.dismissAnnouncementModal(modalId);
      });
      
      // Close on outside click too
      overlay.addEventListener('click', (e) => {
        if (e.target === overlay) {
          this.dismissAnnouncementModal(modalId);
        }
      });
      
      // Show the modal with a small delay
      setTimeout(() => {
        overlay.classList.add('visible');
      }, 100);
      
      return true;
    }

    /**
     * Dismisses the announcement modal and marks it as seen
     * @param {string} modalId - Unique identifier for the modal
     */
    dismissAnnouncementModal(modalId) {
      const overlay = document.getElementById('announcement-overlay');
      
      if (!overlay) return;
      
      // Add fade-out animation
      overlay.classList.remove('visible');
      
      // Remove from DOM after animation completes
      setTimeout(() => {
        overlay.remove();

        if (!this.isMobile) {
          const inputElement = this.root.querySelector('.answer-input');
          if (inputElement && !inputElement.disabled) {
            inputElement.focus();
          }
        }
        
      }, 300);
      
      // Mark as seen
      if (modalId) {
        this.markAnnouncementAsSeen(modalId);
      }
    }

    /**
     * Checks if the user has seen a specific announcement
     * @param {string} modalId - Unique identifier for the modal
     * @returns {boolean} Whether the announcement has been seen
     */
    hasSeenAnnouncement(modalId) {
      const storageKey = `bracketCityAnnouncement_${modalId}`;
      return localStorage.getItem(storageKey) === 'seen';
    }

    /**
     * Marks an announcement as seen in localStorage
     * @param {string} modalId - Unique identifier for the modal
     */
    markAnnouncementAsSeen(modalId) {
      const storageKey = `bracketCityAnnouncement_${modalId}`;
      localStorage.setItem(storageKey, 'seen');
    }

    /**
     * Helper method to display an important announcement
     * @param {Object} options - Announcement options
     * @param {string} options.title - The title of the announcement
     * @param {string} options.message - The announcement message
     * @param {string} options.buttonText - Text for the dismiss button
     * @param {string} options.id - Unique identifier for this announcement
     * @returns {boolean} Whether the announcement was shown
     */
    showImportantAnnouncement(options) {
      const {
        title = 'Important Announcement',
        message = '',
        buttonText = 'Got it',
        id = 'default-announcement'
      } = options;
      
      return this.showAnnouncementModal(title, message, buttonText, id);
    }
}