File size: 226,427 Bytes
42a25fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
505c280
 
 
 
 
42a25fd
 
 
 
505c280
 
 
 
 
42a25fd
 
 
 
 
 
 
 
505c280
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42a25fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
505c280
 
 
 
 
 
 
 
 
42a25fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
505c280
 
 
 
 
 
 
 
42a25fd
 
 
505c280
42a25fd
 
 
 
505c280
42a25fd
 
505c280
42a25fd
505c280
 
42a25fd
 
 
 
 
 
505c280
42a25fd
505c280
42a25fd
 
 
 
 
 
 
 
505c280
42a25fd
 
 
 
 
505c280
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42a25fd
 
 
505c280
 
 
 
 
 
 
 
 
 
 
42a25fd
 
 
 
 
 
505c280
 
42a25fd
 
505c280
42a25fd
 
 
505c280
42a25fd
 
 
 
505c280
 
 
 
 
42a25fd
 
505c280
42a25fd
 
505c280
 
 
 
42a25fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
505c280
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42a25fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
505c280
42a25fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
505c280
 
 
 
 
42a25fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
505c280
 
 
 
 
 
42a25fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
505c280
 
 
 
 
 
 
 
 
 
 
42a25fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
505c280
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42a25fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
505c280
 
 
 
 
 
 
 
 
42a25fd
 
 
 
505c280
42a25fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
505c280
42a25fd
 
 
505c280
 
42a25fd
 
 
 
 
 
 
505c280
42a25fd
 
 
505c280
42a25fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
505c280
 
42a25fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
0206746
42a25fd
 
 
 
 
0206746
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42a25fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ee04696
42a25fd
ee04696
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
42a25fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
505c280
 
 
 
 
42a25fd
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
;
;
/* ============================================================

   MAIN β€” game setup, loop, win/lose, wiring

   ============================================================ */
let running=false, paused=false, demoMode=false, gameEnded=false;
/* Declared with the rest of the match state rather than beside

   mfReportSimFailure further down: resetWorld() clears it and sits above that

   point in the file, so keeping the binding here means a future top-level

   caller cannot hit the temporal dead zone. */
let mfSimFailure=null;
let matchLive=false;            // false during the carrier drop / deploy phase
let difficulty=1;
let defenseFocus=0;             // 0 combined arms, 1 fortress / tower-defence cadence
let infestationOn=true;         // neutral map nests, guards, spread, eruptions and tides
let deploymentPackage='prepared'; // supported opening for newcomers; classic start remains selectable
/* The Galactic strategic layer is the game's primary career shell. This

   callback is assigned during UI wiring; tactical battles stay in this

   document and the strategic shell hands deployments back through the bridge. */
let mfOpenExploration=null,mfExplorationMenuSync=null,mfExplorationLaunching=false;
let hudDeck='orders';           // one secondary command row at a time on phones
/* OTA source can run inside an older packaged HTML shell. Upgrade the two new

   HUD surfaces before binding controls so the patch remains usable without an

   APK reinstall. New packages already contain this markup, making these no-ops. */
function ensureCommanderLocatorShell(){
  const bar=$('heroBar');if(!bar||$('heroPortraitImg'))return;
  bar.innerHTML='<span id="heroPortrait" aria-hidden="true"><img id="heroPortraitImg" alt=""><i id="heroPortraitFallback">C</i><b id="heroRankEm">πŸŽ—</b></span>'
    +'<span class="heroProfileBody"><span class="heroProfileHead"><strong id="heroNameTxt">COMMANDER</strong><b id="heroLvlTxt">LV 1</b></span>'
    +'<span id="heroCallsignTxt">FIELD COMMAND</span><span class="heroVital"><small>HP</small><span class="heroBarOuter"><i id="heroHpFill"></i></span><b id="heroHpTxt">100%</b></span>'
    +'<span class="heroVital heroXp"><small>XP</small><span id="xpOuter"><i id="xpFill"></i></span><b id="heroXpTxt">0%</b></span></span>';
}
function ensureBuildingsDeckShell(){
  const tabs=$('hudDeckTabs');if(!tabs)return;
  tabs.setAttribute('role','tablist');
  let b=tabs.querySelector('[data-deck="buildings"]');
  if(!b){
    b=document.createElement('button');b.className='hudDeckBtn';b.dataset.deck='buildings';b.innerHTML='<span>βŒ‚</span>BUILDINGS';
    tabs.insertBefore(b,tabs.querySelector('[data-deck="abilities"]')||tabs.lastElementChild);
  }
  for(const tab of tabs.querySelectorAll('.hudDeckBtn')){
    tab.setAttribute('role','tab');tab.setAttribute('aria-selected',tab.dataset.deck===hudDeck?'true':'false');
  }
  /* Old shells still carry the ambiguous BASE action. The first-class tab owns

     navigation now; leaving both visible would make the same feature appear to

     behave two different ways. */
  const legacy=$('baseFindBtn');if(legacy){legacy.hidden=true;legacy.setAttribute('aria-hidden','true');}
}
/* Phone command dock is platoon-first (4 groups, deck tabs). Army/box/rings

   still treat the army as one blob. Stage A HUD (after Stage 0 screenshots,

   not this slice): ring LOD, hologram sampling, box-select via spatial hash.

   Army select does not auto-split into P1–P4. selInfo lists 3 unit types. */
let activeWarMode='standard';   // Standard, Co-op and MMO share one planetary catalogue

/* First Contact is deliberately NOT the Tutorial. Training owns KEEL,

   objectives, highlights and its own mode; Standard gets only a short series

   of ordinary notice-rail prompts during a new career's first three prepared

   landings. Keeping this at the battle boundary stops Training state ever

   leaking into a normal session again. */
let firstContactTimers=[];
function clearFirstContactGuide(){
  for(const t of firstContactTimers) clearTimeout(t);
  firstContactTimers.length=0;
}
function startFirstContactGuide(){
  clearFirstContactGuide();
  if(activeWarMode!=='standard'||!assistedOpeningActive()||deploymentPackage!=='prepared') return;
  if(typeof TUT!=='undefined'&&TUT.trainingMode) return;
  const battle=(META.standardMatches|0)+1;
  const say=(delay,msg)=>firstContactTimers.push(setTimeout(()=>{
    if(running&&matchLive&&activeWarMode==='standard'&&!(typeof TUT!=='undefined'&&TUT.trainingMode)) toast(msg);
  },delay));
  if(battle===1) say(900,'β—‡ FIRST CONTACT 1/3 β€” HQ online. Locked systems stay on the Galaxy map.');
  else say(900,'β—‡ FIRST CONTACT '+battle+'/3 β€” Base online. Build your economy next.');
  say(11000,'β—† CLAIM MASS β€” Build an Extractor on a nearby deposit, then defend it.');
  say(28500,'βš” FORM A SCREEN β€” Queue units. Tap to attack-move; double-tap to retreat.');
  say(56000,'βŒ– HOLD TERRITORY β€” Targeting Arrays extend your construction range.');
}

function setHudDeck(deck,quiet){
  const valid=['orders','platoons','buildings','abilities','view'];
  hudDeck=valid.includes(deck)?deck:'orders';
  if(hudDeck!=='abilities'&&typeof hotUtilityClose==='function')hotUtilityClose(true);
  document.body.classList.toggle('hudViewDeck',hudDeck==='view');
  let activeTab=null;
  document.querySelectorAll('.hudDeckBtn').forEach(b=>{
    const on=b.dataset.deck===hudDeck;b.classList.toggle('on',on);b.setAttribute('aria-selected',on?'true':'false');
    if(on)activeTab=b;
  });
  /* Scaled labels can make the tab row horizontally scrollable. Keep the

     newly selected tab inside that scroller instead of leaving its hit target

     painted under the battlefield canvas at the clipped edge. */
  if(activeTab)requestAnimationFrame(()=>activeTab.scrollIntoView({block:'nearest',inline:'nearest'}));
  for(const id of ['camRow','tacRow','grpRow','hotSlots']){
    const row=$(id); if(row) row.style.display='none';
  }
  const rowId={orders:'tacRow',platoons:'grpRow',abilities:'hotSlots',view:'camRow'}[hudDeck];
  const row=$(rowId);
  if(row) row.style.display=hudDeck==='abilities'?(row.children.length?'flex':'none'):'flex';
  if(hudDeck==='buildings'){
    if(typeof openBaseFinder==='function')openBaseFinder();
  }else if(typeof closeBaseFinder==='function')closeBaseFinder(false);
  if(hudDeck==='abilities'&&typeof hotSlotSync==='function') hotSlotSync(true);
  if(typeof updateSelInfo==='function') updateSelInfo();
  if(typeof hotSlotPlace==='function') requestAnimationFrame(hotSlotPlace);
  if(!quiet){sfx('ui');buzz(8);}
}
function showHudDock(on,deck){
  document.body.classList.toggle('hudTacticalDock',!!on);
  const tabs=$('hudDeckTabs'); if(tabs) tabs.style.display=on?'flex':'none';
  const primary=$('primaryRow'); if(primary) primary.style.display=on?'flex':'none';
  if(on) setHudDeck(deck||hudDeck,true);
  else {
    if(typeof hotUtilityClose==='function')hotUtilityClose(true);
    if(typeof closeBaseFinder==='function')closeBaseFinder(false);
    for(const id of ['tacRow','grpRow','hotSlots']){const row=$(id);if(row)row.style.display='none';}
  }
}

/* Nine authored deployment plateaus. They are deliberately inset rather

   than sitting on the border: the camera can frame them, builders have room to

   expand, and every zone can be raised to dry land by the terrain generator. */
const START_ZONES=[
  {id:'nw',n:'1',x:.18,y:.18},{id:'n', n:'2',x:.50,y:.15},
  {id:'ne',n:'3',x:.82,y:.18},{id:'e', n:'4',x:.85,y:.50},
  {id:'se',n:'5',x:.82,y:.82},{id:'s', n:'6',x:.50,y:.85},
  {id:'sw',n:'7',x:.18,y:.82},{id:'w', n:'8',x:.15,y:.50},
  {id:'c', n:'9',x:.50,y:.50}
];
/* MAP is baked into terrain, pathfinding, fog and shaders. Resizing that buffer

   at runtime would desynchronise all four, so map size defines a playable

   theatre inside the fixed 2.6 km terrain. Spawn separation, neutral resource

   bounds and resource counts all use the same theatre scale; this produces a

   genuinely faster Compact match without allocating a second terrain stack. */
const BATTLEFIELD_PRESETS={
  compact:{nm:'COMPACT',km:'2.2 KM',dur:'10\u201314 MIN',world:.6875,start:.72,spread:.90,nodes:16,geysers:1,site:.90,haz:.92,
           ds:'Close spawns, concentrated resource fields and quick clashes.'},
  standard:{nm:'STANDARD',km:'2.6 KM',dur:'15\u201322 MIN',world:.8125,start:.86,spread:1.08,nodes:20,geysers:2,site:1.06,haz:1.04,
             ds:'More breathing room for expansion, scouting and multi-front defence.'},
  large:{nm:'LARGE',km:'3.2 KM',dur:'20\u201330 MIN',world:1,start:1,spread:1.20,nodes:24,geysers:3,site:1.22,haz:1.14,
         ds:'Maximum separation, additional fields and room for multi-front wars.'}
};
let battlefieldPreset='standard';
const DEPLOYMENT_PACKAGES={
  prepared:{nm:'PREPARED LANDING',em:'\u25c8',ds:'HQ, Reactor, Factory and Constructor deploy together. Recommended while learning.'},
  expedition:{nm:'EXPEDITION LANDING',em:'\u25c7',ds:'HQ and Constructor only. Classic build-from-zero RTS opening.'}
};
function deploymentPackageDef(){return DEPLOYMENT_PACKAGES[deploymentPackage]||DEPLOYMENT_PACKAGES.prepared;}
function deploymentGraceSeconds(diff){return [180,120,75][clamp(diff|0,0,2)];}
function assistedOpeningActive(){return ((META&&META.standardMatches)||0)<3;}
function openingContactRemaining(){
  if(!matchLive||typeof AI==='undefined'||AI.openingGrace==null)return 0;
  return Math.max(0,AI.openingGrace-(AI.t||0));
}
function battlefieldPresetKey(key){ return key==='grand'?'large':(BATTLEFIELD_PRESETS[key]?key:'standard'); }
function battlefieldPresetDef(){ return BATTLEFIELD_PRESETS[battlefieldPresetKey(battlefieldPreset)]; }
function battlefieldPresetHint(){
  const P=battlefieldPresetDef();
  return P.km+' \u00b7 '+battlefieldFactionCap()+' FACTIONS MAX \u00b7 EST. '+P.dur+' \u2014 '+P.ds;
}
function battlefieldPlayBounds(pad){
  const P=battlefieldPresetDef(),span=MAP*(P.world||1),inset=(MAP-span)*.5;
  const lo=inset+(pad||0),hi=MAP-inset-(pad||0);
  return lo<hi?{lo,hi,span}:{lo:MAP*.5-40,hi:MAP*.5+40,span:80};
}
/* THEATRE SILHOUETTES.

   Map size still owns one stable square allocation, but the playable edge is

   not the same square stamped onto every biome.  Each theme receives a

   deterministic superellipse plus low-frequency authored inlets/notches.

   The same formula is duplicated in the terrain vertex shader and sampled by

   the holographic border, so gameplay, red line and exclusion art agree. */
function battlefieldShapeStyle(){
  const D=(typeof MAPDEFS!=='undefined'&&MAPDEFS[curMap])?MAPDEFS[curMap]:null;
  return D&&D.seabed?1:(D&&D.crater?2:
    (D&&D.relief>=1.28?3:(curTheme==='ashland'?2:curTheme==='arctic'?3:0)));
}
function battlefieldShapeRadius(angle,pad){
  const B=battlefieldPlayBounds(0),half=Math.max(45,(B.hi-B.lo)*.5-(pad||0));
  const style=battlefieldShapeStyle(),ca=Math.abs(Math.cos(angle)),sa=Math.abs(Math.sin(angle));
  const power=style===1?3.15:style===2?6.25:style===3?4.15:5.0;
  const base=half/Math.pow(Math.pow(ca,power)+Math.pow(sa,power),1/power);
  let shape;
  if(style===1)      shape=.945+.024*Math.sin(angle*3+.65)+.016*Math.sin(angle*7-1.10);
  else if(style===2) shape=.958+.020*Math.sin(angle*5+.30)+.012*Math.sin(angle*9+1.20);
  else if(style===3) shape=.948+.025*Math.sin(angle*2-.80)+.016*Math.sin(angle*6+.45);
  else               shape=.966+.014*Math.sin(angle*4+.35)+.010*Math.sin(angle*7-.55);
  return Math.max(36,base*shape);
}
function battlefieldSignedDistance(x,y,pad){
  const B=battlefieldPlayBounds(0),cx=(B.lo+B.hi)*.5,cy=cx,dx=x-cx,dy=y-cy;
  const r=Math.hypot(dx,dy),a=r>0.001?Math.atan2(dy,dx):0;
  return battlefieldShapeRadius(a,pad)-r;
}
function battlefieldContains(x,y,pad){ return battlefieldSignedDistance(x,y,pad)>=0; }
function battlefieldClampPoint(x,y,pad){
  const B=battlefieldPlayBounds(0),cx=(B.lo+B.hi)*.5,cy=cx,dx=x-cx,dy=y-cy,r=Math.hypot(dx,dy);
  const a=r>0.001?Math.atan2(dy,dx):0,limit=battlefieldShapeRadius(a,pad);
  if(r<=limit)return [x,y];
  const q=limit/Math.max(.001,r);return [cx+dx*q,cy+dy*q];
}
function battlefieldBoundaryPoint(angle,pad){
  const B=battlefieldPlayBounds(0),c=(B.lo+B.hi)*.5,r=battlefieldShapeRadius(angle,pad);
  return [c+Math.cos(angle)*r,c+Math.sin(angle)*r];
}
/* Max orthographic span for THIS theatre.

   SPAN_MAX=3400 is the engine ceiling, sized for the 3.2 km Large mesh. Compact

   and Standard are smaller playable discs inside that same mesh, so a single

   ceiling lets the view run into exclusion haze, the heightfield rim, and the

   skirt void. Pan stays inside the playable square (red rungs). FAR zoom is

   allowed to spend Compact/Standard's unused MAP inset so infantry can finish

   converting to icons (~2196 span at VH=915) without Large seeing the mesh

   skirt (MAP-88 still wins there). Pitch and aspect both change the ground

   footprint β€” a tilted portrait view is tall, a landscape view is wide β€” so

   this is not a per-preset constant. */
function battlefieldViewInner(){
  const B=typeof battlefieldPlayBounds==='function'?battlefieldPlayBounds(0):{lo:0,hi:MAP,span:MAP};
  const play=Math.max(80,B.span||(B.hi-B.lo));
  /* play-24 keeps a hair inside the theatre square so the red rungs sit on

     the visible edge. MAP-88 keeps Large's heightfield rim off-screen without

     cropping the rungs (they sit ~60wu inside MAP). Compact/Standard are

     already inset from MAP, so the theatre wins there. Pan/look-at uses this

     tight square; FAR zoom uses battlefieldZoomInner() so icons can engage. */
  return Math.min(play-24,MAP-88);
}
function battlefieldZoomInner(){
  const inner=battlefieldViewInner();
  const B=typeof battlefieldPlayBounds==='function'?battlefieldPlayBounds(0):{lo:0,hi:MAP,span:MAP};
  const play=Math.max(80,B.span||(B.hi-B.lo));
  /* Compact FAR used to sit at ~2020 span β€” below the infantry icon-out

     crossing (~2196 at VH=915), so the strategic tier never fully took over.

     Spend some of Compact/Standard's unused MAP inset as border haze (already

     fog-coloured) so FAR zoom reaches that band. 560 wu is past infantry

     full-convert with margin; MAP-88 still blocks Large's heightfield rim.

     Pan stays on the tight inner. */
  const haze=Math.min(560,Math.max(0,(MAP-play)*0.56));
  return Math.min(inner+haze,MAP-88);
}
function spanMaxNow(){
  const inner=battlefieldZoomInner();
  const pitch=clamp(typeof camPitch==='number'?camPitch:1.19,
    typeof PITCH_MIN==='number'?PITCH_MIN:1.05,
    typeof PITCH_MAX==='number'?PITCH_MAX:1.50);
  const sinP=Math.max(0.30,Math.sin(pitch));
  /* Fit the pitched ground HEIGHT inside `inner`. The previous formula used

     max(width,height) of the ground AABB, so a landscape window was capped

     at inner/aspect (~1748 at 1080p) β€” below the infantry icon crossing.

     Screen-right may hang into border haze; shaders already fade that to

     fog colour. Portrait phones were already height-limited, so their cap

     only moves by battlefieldZoomInner(). Yaw is left out on purpose:

     turning used to shrink span and fight the icon band. */
  const k=1/sinP;
  const hi=typeof SPAN_MAX==='number'?SPAN_MAX:3400;
  const lo=spanMinNow();
  return clamp(inner/Math.max(0.35,k),lo,hi);
}
/* mesh.js SPAN_MIN=420 is a company view. On a 412Γ—900 phone that floor never

   reaches commander/unit tactical mesh (~200–270). Icon fade is footprint-

   based and still off at 200; the old floor just hid the mesh band. */
function spanMinNow(){ return 200; }
/* mesh.js owns clampCam / zoomBy / camTick. Do not edit that file for this β€”

   FS3D/AO work lives there. Takeover keeps the pitch clamp and rewrites span

   + look-at against the current theatre. distTarget must be clamped too:

   camTick lerps orthoSpan toward it, so leaving it at SPAN_MAX would fight

   the cap every frame. */
const _mfClampCamMesh=clampCam;
clampCam=function(){
  const lo=spanMinNow(), mx=spanMaxNow();
  if(orthoSpan>mx) orthoSpan=mx;
  if(distTarget>mx) distTarget=mx;
  const want=orthoSpan;
  _mfClampCamMesh();
  /* mesh clampCam raises anything under 420. Restore the tactical floor. */
  if(want<SPAN_MIN) orthoSpan=clamp(want,lo,mx);
  if(orthoSpan>mx) orthoSpan=mx;
  if(orthoSpan<lo) orthoSpan=lo;
  distTarget=clamp(distTarget,lo,mx);
  camDist=orthoSpan;
  cam.z=1400/orthoSpan;
  /* LOOK-AT, not view-hull. Subtracting max(hw,depth) on a tall phone ate

     the slack box (~Β±515 at span 1500) and left SW HQ (576,2624) outside

     it β€” pan stuck and HQ follow yanked back to centre every frame. Haze

     already hides overhang. */
  const B=battlefieldPlayBounds(0);
  /* Look-at stays inside the theatre. Positive overhang plus a portrait

     hull is the gray triangle at the map edge; HQ follow still works

     because start zones sit inside these bounds. */
  cam.x=clamp(cam.x,B.lo,B.hi);
  cam.y=clamp(cam.y,B.lo,B.hi);
};
zoomBy=function(f){ distTarget=clamp(distTarget/f,spanMinNow(),spanMaxNow()); };
const _mfCamTickMesh=camTick;
camTick=function(dt){
  /* mesh camTick follows even after camUser(). Pinch/pan must win. */
  if(camFollow>=0&&typeof camAutoAllowed==='function'&&!camAutoAllowed()) camFollow=-1;
  _mfCamTickMesh(dt);
};
/* MEDIUM/LOW scissor projects the ground AABB. A pitched portrait view

   leaves fog-clear strips (the grey border). Full frame on tall phones. */
if(typeof mfGfxScissor==='function'){
  const _mfGfxScissorMesh=mfGfxScissor;
  mfGfxScissor=function(on){
    if(!on){ _mfGfxScissorMesh(false); return; }
    if(typeof VH==='number'&&typeof VW==='number'&&VH>VW){
      if(typeof gl!=='undefined'&&gl) gl.disable(gl.SCISSOR_TEST);
      return;
    }
    _mfGfxScissorMesh(true);
  };
}
let playerStartZone='sw', spawnPick='player';
let aiSlots=[
  {on:true, diff:1,zone:'ne',ally:false,behavior:'balanced'},
  {on:false,diff:1,zone:'nw',ally:false,behavior:'balanced'},
  {on:false,diff:1,zone:'se',ally:false,behavior:'balanced'},
  {on:false,diff:1,zone:'c', ally:false,behavior:'balanced'}
];
/* Theatre size owns participant density while every participant owns one hard

   500-unit Commander-seat cap. Authored seats: Compact duel SW–NE; second

   enemy SE never NW; ally NW; Large 1v3 uses all four corners. The Brood may

   appear as a fifth system force, but never consumes a participant seat. */
const BATTLEFIELD_FACTION_CAP={compact:2,standard:3,large:4};
const SPAWN_CORNERS=['nw','ne','se','sw'];
const SPAWN_LARGE_FIFTH='c';
const SPAWN_FAIR_MIN_M=1400;
function battlefieldFactionCap(){return BATTLEFIELD_FACTION_CAP[populationTheatre()]||3;}
function battlefieldAiCap(){return Math.max(1,battlefieldFactionCap()-1);}
function spawnZoneIsCorner(id){ return SPAWN_CORNERS.indexOf(id)>=0; }
function spawnZoneAllowed(id){return spawnZoneIsCorner(id)||(populationTheatre()==='large'&&id===SPAWN_LARGE_FIFTH);}
function normalizeAiSlotsForBattlefield(){
  const max=battlefieldAiCap();
  for(const A of aiSlots)A.behavior=typeof aiBehaviorKey==='function'?aiBehaviorKey(A.behavior):'balanced';
  for(let i=max;i<aiSlots.length;i++)aiSlots[i].on=false;
  if(!aiSlots.slice(0,max).some(A=>A.on))aiSlots[0].on=true;
  /* A skirmish still needs an opponent. Compact therefore cannot spend its

     only AI slot on an ally; larger theatres can trade any additional slot. */
  if(!aiSlots.slice(0,max).some(A=>A.on&&!A.ally)){
    const first=aiSlots.findIndex((A,i)=>i<max&&A.on);if(first>=0)aiSlots[first].ally=false;
  }
  if(spawnPick!=='player'){
    const i=+spawnPick.slice(2);if(i>=max||!aiSlots[i]||!aiSlots[i].on)spawnPick='player';
  }
  reseatSpawnPlanner(max);
}
function reseatSpawnPlanner(max){
  /* Stage 0 MAP contract. Compact is a SW–NE duel; extras are already off.

     On Standard/Large, allies claim NW, the second enemy is SE (never NW β€”

     that is the ally chair / west-edge crush vs player SW), and a third enemy

     takes the leftover corner so Large 1v3 is four corners. Brood nests use

     neutral placement and do not claim the retired fifth center seat. */
  if(max==null) max=battlefieldAiCap();
  if((typeof populationTheatre==='function'?populationTheatre():'standard')==='compact'){
    playerStartZone='sw';
    aiSlots[0].on=true; aiSlots[0].ally=false; aiSlots[0].zone='ne';
    return;
  }
  if(!spawnZoneIsCorner(playerStartZone)) playerStartZone='sw';
  let allyN=0;
  for(let i=0;i<aiSlots.length;i++){
    if(i>=max||!aiSlots[i].on||!aiSlots[i].ally) continue;
    if(allyN===0){
      if(playerStartZone==='nw') playerStartZone='sw';
      aiSlots[i].zone='nw';
    }
    allyN++;
  }
  const taken=new Set([playerStartZone]);
  for(let i=0;i<aiSlots.length;i++) if(i<max&&aiSlots[i].on&&aiSlots[i].ally) taken.add(aiSlots[i].zone);
  const enemyIdx=[];
  for(let i=0;i<aiSlots.length;i++) if(i<max&&aiSlots[i].on&&!aiSlots[i].ally) enemyIdx.push(i);
  const nE=enemyIdx.length;
  /* 1v1/1v2 omit NW so the second enemy cannot sit on the ally chair. 1v3

     puts NW last; 1v4 adds the fair center seat after all corners. */
  const prefer=nE>=4?['ne','se','nw','c','sw']:(nE>=3?['ne','se','nw','sw']:['ne','se','sw']);
  for(const i of enemyIdx){
    const A=aiSlots[i];
    const nwBanned=A.zone==='nw'&&(i===1||nE<3);
    const ok=spawnZoneAllowed(A.zone)&&!taken.has(A.zone)&&!nwBanned;
    if(!ok){
      const pick=prefer.find(z=>!taken.has(z)&&!(i===1&&z==='nw'))||SPAWN_CORNERS.concat(SPAWN_LARGE_FIFTH).find(z=>spawnZoneAllowed(z)&&!taken.has(z));
      if(pick) A.zone=pick;
    }
    taken.add(A.zone);
  }
  allyN=0;
  for(let i=0;i<aiSlots.length;i++){
    if(i>=max||!aiSlots[i].on||!aiSlots[i].ally) continue;
    allyN++;
    if(allyN===1){ taken.add(aiSlots[i].zone); continue; }
    if(spawnZoneIsCorner(aiSlots[i].zone)&&!taken.has(aiSlots[i].zone)){ taken.add(aiSlots[i].zone); continue; }
    const pick=SPAWN_CORNERS.find(z=>!taken.has(z));
    if(pick){ aiSlots[i].zone=pick; taken.add(pick); }
  }
}
function startZone(id){
  const Z=START_ZONES.find(z=>z.id===id)||START_ZONES[0],s=battlefieldPresetDef().start;
  return {id:Z.id,n:Z.n,x:.5+(Z.x-.5)*s,y:.5+(Z.y-.5)*s};
}
function activeAiSlots(){ return aiSlots.map((s,i)=>({...s,slot:i})).filter(s=>s.on); }
function activeEnemySlots(){return activeAiSlots().filter(S=>!S.ally);}
function activeAllySlots(){return activeAiSlots().filter(S=>S.ally);}
function spawnAiRosterPick(fac, used){
  /* Identity only. Chassis stays FACTIONS[fac].hero (4 / 28 / 29 / 30).

     applyCommanderChoice is the player's doctrine stack β€” never run it here

     or three AI seats would multiply player perks. */
  const key=typeof commanderFactionKey==='function'?commanderFactionKey(fac):fac;
  const roster=(typeof COMMANDER_ROSTERS!=='undefined'&&COMMANDER_ROSTERS[key])||[];
  let pick=null;
  for(let i=0;i<roster.length;i++) if(used.indexOf(roster[i].id)<0){ pick=roster[i]; break; }
  if(!pick&&roster.length) pick=roster[used.length%roster.length];
  if(pick) used.push(pick.id);
  return pick;
}
function skirmishSpawnPoints(){
  const P=startZone(playerStartZone), out=[{kind:'player',slot:-1,x:P.x*MAP,y:P.y*MAP,diff:0,zone:P.id}];
  for(const A of activeAiSlots()){
    const Z=startZone(A.zone); out.push({kind:A.ally?'ally':'ai',ally:!!A.ally,slot:A.slot,x:Z.x*MAP,y:Z.y*MAP,diff:A.diff,zone:Z.id,behavior:A.behavior});
  }
  return out;
}
/* Terrain sites and opening economy both consult the active spawn seats. Map

   and theme alone are therefore not a complete cache identity. Difficulty,

   faction and behavior do not change geometry and intentionally stay out. */
function mfWorldTopologyKey(){
  const theatre=typeof battlefieldPresetKey==='function'?battlefieldPresetKey(battlefieldPreset):String(battlefieldPreset||'standard');
  const seats=activeAiSlots().map(A=>A.slot+':'+(A.ally?'ally':'enemy')+':'+A.zone).join(',');
  return theatre+'|player:'+playerStartZone+'|ai:'+seats;
}
function farFromStartZones(x,y,d){
  for(const S of skirmishSpawnPoints()) if(dist2(x,y,S.x,S.y)<d*d) return false;
  return true;
}
function spawnTargetZone(key){
  if(key==='player') return playerStartZone;
  const i=+String(key).replace('ai',''); return aiSlots[i]?aiSlots[i].zone:playerStartZone;
}
function spawnEnemyNwBanned(key,zone){
  /* AI 2 (index 1) is never an enemy on NW β€” that chair is the ally / 1v3

     leftover. A two-front also must not park its second enemy there. */
  if(zone!=='nw'||key==='player') return false;
  const i=+String(key).replace('ai','');
  if(aiSlots[i]==null||aiSlots[i].ally) return false;
  if(i===1) return true;
  let n=0;
  for(let k=0;k<aiSlots.length;k++) if(aiSlots[k].on&&!aiSlots[k].ally) n++;
  if(!aiSlots[i].on) n++;
  return n<3;
}
function setSpawnTargetZone(key,zone){
  if(!spawnZoneAllowed(zone)){
    toast('CARDINAL STARTS ARE CRUSH β€” use a corner'+(populationTheatre()==='large'?' or center':''));
    return false;
  }
  if((typeof populationTheatre==='function'?populationTheatre():'standard')==='compact'&&zone!=='sw'&&zone!=='ne'){
    toast('COMPACT DUEL IS SW–NE');
    return false;
  }
  if(spawnEnemyNwBanned(key,zone)){
    toast('SECOND ENEMY IS SE β€” NW IS THE ALLY CHAIR');
    return false;
  }
  const old=spawnTargetZone(key);
  let occupied='';
  if(playerStartZone===zone) occupied='player';
  for(let i=0;i<aiSlots.length;i++) if(aiSlots[i].on&&aiSlots[i].zone===zone) occupied='ai'+i;
  if(occupied&&occupied!==key){
    if(occupied==='player') playerStartZone=old;
    else aiSlots[+occupied.slice(2)].zone=old;
  }
  if(key==='player') playerStartZone=zone;
  else { const i=+key.slice(2); if(aiSlots[i]) aiSlots[i].zone=zone; }
  return true;
}
function drawSpawnPlanner(){
  const cv3=$('spawnMap'); if(!cv3||typeof drawMapPreview!=='function') return;
  /* Square backing store, enforced here rather than in the markup: the world

     is square, and this is the surface a player reads positions off. */
  const SIDE=384; if(cv3.width!==SIDE||cv3.height!==SIDE){ cv3.width=SIDE; cv3.height=SIDE; }
  drawMapPreview(cv3,MAPDEFS[curMap]||MAPDEFS.vanguard,curTheme,false,battlefieldPreset);
  const c=cv3.getContext('2d'), W=cv3.width,H=cv3.height;
  const picked=spawnTargetZone(spawnPick);
  for(const Z0 of START_ZONES){
    const Z=startZone(Z0.id),x=previewFrac2Px(Z.x,battlefieldPreset,W),y=previewFrac2Px(Z.y,battlefieldPreset,H), sel=Z.id===picked;
    c.beginPath(); c.arc(x,y,sel?14:11,0,TAU);
    c.fillStyle='rgba(5,13,23,.82)'; c.fill();
    c.lineWidth=sel?3:1.5; c.strokeStyle=sel?'#fff':'rgba(210,235,250,.72)'; c.stroke();
    c.fillStyle='#d9edf8'; c.font='800 10px sans-serif'; c.textAlign='center'; c.textBaseline='middle'; c.fillText(Z.n,x,y+.5);
  }
  const marks=[{key:'player',zone:playerStartZone,label:'YOU',col:'#41c8ff',fac:playerFaction}];
  for(let i=0;i<aiSlots.length;i++) if(aiSlots[i].on) marks.push({key:'ai'+i,zone:aiSlots[i].zone,
    label:(aiSlots[i].ally?'ALLY ':'ENEMY ')+(i+1),col:aiSlots[i].ally?'#66e5a2':'#ff5d43',
    fac:aiSlots[i].ally?playerFaction:(aiFactionSel==='random'?'':aiFactionSel),behavior:aiSlots[i].behavior});
  for(const M of marks){
    const Z=startZone(M.zone),x=previewFrac2Px(Z.x,battlefieldPreset,W),y=previewFrac2Px(Z.y,battlefieldPreset,H);
    c.beginPath(); c.arc(x,y,11,0,TAU); c.fillStyle='rgba(4,12,21,.94)'; c.fill();
    c.lineWidth=2.5;c.strokeStyle=M.col;c.stroke();
    const I=typeof facIconCanvas==='function'?facIconCanvas(M.fac,drawSpawnPlanner):null;
    if(I)c.drawImage(I,x-8,y-8,16,16);
    else{c.fillStyle=M.col;c.font='900 11px sans-serif';c.textAlign='center';c.textBaseline='middle';c.fillText(M.fac?'\u25c8':'?',x,y+.5);}
    c.font='800 8px sans-serif';c.textAlign='center';c.textBaseline='bottom';c.fillStyle='#fff';c.fillText(M.label,x,y-13);
    c.font='800 7px sans-serif';c.textBaseline='top';c.fillStyle=M.col;c.fillText(aiBehaviorDef(M.behavior).nm.slice(0,3),x,y+13);
  }
}
function renderSpawnPlanner(){
  const pick=$('spawnPick'), list=$('aiSlotList'); if(!pick||!list) return;
  normalizeAiSlotsForBattlefield();
  const aiMax=battlefieldAiCap();
  const live=activeAiSlots(),same=live.every(A=>A.diff===live[0].diff);
  const conquestFloor=typeof mfConquestDifficultyFloor==='function'?mfConquestDifficultyFloor(curMap):0;
  let gdiff=$('globalDiffRow');
  if(!gdiff){
    gdiff=document.createElement('div'); gdiff.id='globalDiffRow'; gdiff.className='optRow';
    list.parentNode.insertBefore(gdiff,list);
  }
  gdiff.innerHTML=['EASY','NORMAL','HARD'].map((n,d)=>'<button type="button" class="globalDiff dbtn'+(same&&d===live[0].diff?' on':'')+(d<conquestFloor?' mapFloor':'')+'" data-d="'+d+'">'+n+'</button>').join('');
  gdiff.querySelectorAll('.globalDiff').forEach(b=>b.addEventListener('pointerdown',e=>{
    e.stopPropagation();
    if(+b.dataset.d<conquestFloor){toast('CONQUEST THREAT FLOOR β€” this battlefield begins at '+['EASY','NORMAL','HARD'][conquestFloor]);sfx('deny');return;}
    difficulty=+b.dataset.d;
    for(const A of aiSlots) if(A.on) A.diff=difficulty;
    if(typeof META!=='undefined'&&META&&!(typeof storyCampaignPlanBorrowed==='function'&&storyCampaignPlanBorrowed())){
      META.setup=META.setup||{}; META.setup.d=difficulty; if(typeof metaSave==='function') metaSave();
    }
    renderSpawnPlanner(); initAudio(); sfx('ui');
  }));
  document.querySelectorAll('.globalDiff').forEach(b=>{b.classList.toggle('on',same&&+b.dataset.d===live[0].diff);b.classList.toggle('mapFloor',+b.dataset.d<conquestFloor);});
  const targets=[{key:'player',nm:'YOU',on:true}].concat(aiSlots.map((s,i)=>({key:'ai'+i,nm:'AI '+(i+1),on:s.on})));
  pick.innerHTML=targets.map(T=>{
    const Z=startZone(spawnTargetZone(T.key));
    return '<button class="spawnTarget'+(spawnPick===T.key?' on':'')+(T.on?'':' off')+'" data-sp="'+T.key+'">'+T.nm+'<b>ZONE '+Z.n+'</b></button>';
  }).join('');
  pick.querySelectorAll('.spawnTarget').forEach(b=>b.addEventListener('pointerdown',e=>{
    e.stopPropagation(); const k=b.dataset.sp;
    if(k!=='player'){
      const i=+k.slice(2);
      if(i>=aiMax){toast('LARGER THEATRE REQUIRED \u2014 '+battlefieldFactionCap()+' factions maximum here');sfx('deny');return;}
      if(!aiSlots[i].on) aiSlots[i].on=true;
    }
    spawnPick=k; renderSpawnPlanner(); sfx('ui');
  }));
  list.innerHTML=aiSlots.map((A,i)=>{
    const Z=startZone(A.zone),locked=i>=aiMax,behavior=aiBehaviorKey(A.behavior),BD=aiBehaviorDef(behavior);
    const behaviorOptions=Object.keys(AI_BEHAVIOR_TYPES).map(k=>'<option value="'+k+'"'+(k===behavior?' selected':'')+(!aiBehaviorAvailable(k)?' disabled':'')+'>'+AI_BEHAVIOR_TYPES[k].em+' '+AI_BEHAVIOR_TYPES[k].nm+(k==='naval'&&!aiBehaviorAvailable(k)?' β€” NO WATER':'')+'</option>').join('');
    return '<div class="aiSlot'+(A.on?'':' disabled')+(locked?' mapLocked':'')+'" data-ai="'+i+'">'
      +'<div class="aiSlotHead"><span class="aiSlotName">'+(typeof facIcon==='function'?facIcon(A.ally?playerFaction:(aiFactionSel==='random'?'':aiFactionSel),20,'aiMapFac'):'')+'<i>AI COMMANDER '+(i+1)+'</i></span><span class="aiSlotZone">ZONE '+Z.n+'</span>'
      +'<button class="aiRole '+(A.ally?'ally':'enemy')+'">'+(A.ally?'ALLY':'ENEMY')+'</button>'
      +'<button class="aiToggle'+(A.on?' on':'')+'">'+(locked?'MAP LOCK':(A.on?'ENABLED':'OFF'))+'</button></div>'
      +'<div class="aiBehaviorRow"><label>BEHAVIOR</label><select class="aiBehavior" aria-label="AI behavior">'+behaviorOptions+'</select></div>'
      +'<p class="aiBehaviorHint">'+BD.ds+'</p>'
      +'<div class="aiDiffRow">'+['EASY','NORMAL','HARD'].map((n,d)=>'<button class="aiDiff'+(A.diff===d?' on':'')+(d<conquestFloor?' mapFloor':'')+'" data-ad="'+d+'">'+n+'</button>').join('')+'</div></div>';
  }).join('');
  list.querySelectorAll('.aiSlot').forEach(row=>{
    const i=+row.dataset.ai,locked=i>=aiMax;
    row.querySelector('.aiSlotName').addEventListener('pointerdown',()=>{ if(aiSlots[i].on){spawnPick='ai'+i;renderSpawnPlanner();sfx('ui');} });
    row.querySelector('.aiRole').addEventListener('pointerdown',()=>{
      if(locked){toast('LARGER THEATRE REQUIRED');sfx('deny');return;}
      if(!aiSlots[i].ally&&activeEnemySlots().length===1){toast('At least one enemy faction is required');sfx('deny');return;}
      aiSlots[i].on=true;aiSlots[i].ally=!aiSlots[i].ally;spawnPick='ai'+i;
      normalizeAiSlotsForBattlefield();difficulty=Math.max(...activeEnemySlots().map(A=>A.diff));renderSpawnPlanner();sfx('confirm');
    });
    row.querySelector('.aiBehavior').addEventListener('change',e=>{
      if(locked){renderSpawnPlanner();sfx('deny');return;}
      const k=e.target.value;if(!aiBehaviorAvailable(k)){toast('NAVAL AI REQUIRES A CONNECTED OCEAN OR RIVER');renderSpawnPlanner();sfx('deny');return;}
      aiSlots[i].behavior=k;aiSlots[i].on=true;spawnPick='ai'+i;renderSpawnPlanner();sfx('confirm');
    });
    row.querySelector('.aiToggle').addEventListener('pointerdown',()=>{
      if(locked){toast('LARGER THEATRE REQUIRED \u2014 choose '+(i===1?'Standard':'Large')+' for this faction slot');sfx('deny');return;}
      if(aiSlots[i].on&&!aiSlots[i].ally&&activeEnemySlots().length===1){ toast('At least one enemy faction is required'); return; }
      aiSlots[i].on=!aiSlots[i].on;
      if(!aiSlots[i].on&&spawnPick==='ai'+i) spawnPick='player';
      else if(aiSlots[i].on) spawnPick='ai'+i;
      normalizeAiSlotsForBattlefield();difficulty=Math.max(...activeEnemySlots().map(A=>A.diff)); renderSpawnPlanner(); sfx('ui');
    });
    row.querySelectorAll('.aiDiff').forEach(b=>b.addEventListener('pointerdown',()=>{
      if(locked){toast('LARGER THEATRE REQUIRED');sfx('deny');return;}
      if(+b.dataset.ad<conquestFloor){toast('CONQUEST THREAT FLOOR β€” this battlefield begins at '+['EASY','NORMAL','HARD'][conquestFloor]);sfx('deny');return;}
      aiSlots[i].diff=+b.dataset.ad; aiSlots[i].on=true; spawnPick='ai'+i;
      difficulty=Math.max(...activeEnemySlots().map(A=>A.diff)); renderSpawnPlanner(); sfx('ui');
    }));
  });
  const fair=$('spawnFairness');
  if(fair){
    const S=skirmishSpawnPoints(); let md=1e9;
    for(let a=0;a<S.length;a++) for(let b=a+1;b<S.length;b++) md=Math.min(md,Math.sqrt(dist2(S[a].x,S[a].y,S[b].x,S[b].y)));
    const crush=md<SPAWN_FAIR_MIN_M;
    fair.style.background=crush?'rgba(90,18,22,.55)':'';
    fair.style.borderColor=crush?'rgba(255,93,67,.45)':'';
    fair.innerHTML=crush
      ?'<b style="color:#ff6d55">\u26A0 CRUSH '+Math.round(md)+'m</b><span style="color:#ffb3a6">Nearest commanders under '+SPAWN_FAIR_MIN_M+'m β€” use separated corners or the Large center seat</span>'
      :'<b>\u2713 1 PLAYER Β· '+activeAllySlots().length+' AI ALLY Β· '+activeEnemySlots().length+' ENEMY</b><span>3 mass + 1 energy site each Β· nearest commanders '+Math.round(md)+'m apart</span>';
  }
  drawSpawnPlanner();
}
function initSpawnPlanner(){
  const cv3=$('spawnMap'); if(!cv3||cv3.dataset.wired) return;
  cv3.dataset.wired='1';
  cv3.addEventListener('pointerdown',e=>{
    e.stopPropagation(); const r=cv3.getBoundingClientRect(),u=previewPx2Frac((e.clientX-r.left)/r.width,battlefieldPreset),v=previewPx2Frac((e.clientY-r.top)/r.height,battlefieldPreset);
    let best=START_ZONES[0],bd=1e9;
    for(const Z0 of START_ZONES){ const Z=startZone(Z0.id),d=(u-Z.x)*(u-Z.x)+(v-Z.y)*(v-Z.y);if(d<bd){bd=d;best=Z;} }
    if(setSpawnTargetZone(spawnPick,best.id)===false){ sfx('deny'); return; }
    renderSpawnPlanner(); sfx('confirm');
  });
  renderSpawnPlanner();
}

function resetWorld(){
  clearFirstContactGuide();
  /* A halted simulation is match-scoped. The halt latch is deliberately sticky

     inside a match (see frame()), so without clearing it here one bad match

     would leave every later match frozen from the first frame with no message

     - the report already fired for the previous one. */
  mfSimFailure=null;
  try{window.mfSimFailure=null;}catch(e){}
  /* Commander dialogue is match-scoped. Clear queued subtitles/audio gates at

     the same authoritative world boundary as units and projectiles so a cue

     from the previous operation cannot leak into the next deployment. */
  if(typeof commanderDialogueReset==='function') commanderDialogueReset();
  if(typeof cmdrTxReset==='function') cmdrTxReset();
  if(typeof mfNoticeMatchReset==='function') mfNoticeMatchReset();
  if(typeof mfCoachMatchReset==='function') mfCoachMatchReset();
  ualive.fill(0); usel.fill(0); ugen.fill(0); utgtg.fill(-1);
  freeList=[]; unitHigh=0; teamCount[0]=0; teamCount[1]=0;
  if(typeof activeUnitReset==='function')activeUnitReset();
  if(typeof tick==='number')tick=0;
  acc=0;
  if(typeof populationResetLedgers==='function') populationResetLedgers();
  if(typeof uAllyBase!=='undefined') uAllyBase.fill(-1);
  rebuildGrid();                                   // spatial grid must never hold stale chains
  heroIdx=-1; enemyHeroIdx=-1; enemyHeroIdxs.length=0;
  blds.length=0; rebuildBGrid();
  if(typeof mfNavReset==='function')mfNavReset();
  else if(typeof mfNavProgressReset==='function')mfNavProgressReset();
  if(typeof mfBuildingWorkFxReset==='function')mfBuildingWorkFxReset();
  craters.length=0; wrecks.length=0; rubbles.length=0; groundBurns.length=0;
  pFree=[]; pHigh=0; palive.fill(0); pSplit.fill(0);
  pSmokeT.fill(0);pFlightCue.fill(0);artShellSmoke.length=0;
  flife.fill(0); fCount=0; fHead=0;
  /* Optional effect pools live outside the legacy particle arrays above.

     Without an explicit reset, rubble and queued volumes from the previous

     battlefield survive into the next match until their individual TTLs run

     out, now sampling and rendering against a different heightfield. */
  if(typeof mfPhysClear==='function') mfPhysClear();
  if(typeof volFxClear==='function') volFxClear();
  if(typeof mfOrdnanceTrailSimReset==='function') mfOrdnanceTrailSimReset();
  if(typeof mfAirReset==='function') mfAirReset(typeof seed==='number'?seed:undefined);
  if(typeof macroFxReset==='function') macroFxReset();
  if(typeof shieldFxReset==='function') shieldFxReset();
  if(typeof mfShockwaveReset==='function') mfShockwaveReset();
  if(typeof mfCloudFxReset==='function') mfCloudFxReset();
  /* Wholesale reset, not a credit. econSetBanks also restores both caps,

     which is why the RES_MCAP/RES_ECAP line that used to follow is gone. */
  econSetBanks(260,1000);

  mSpend=eSpend=mSpendAcc=eSpendAcc=spendT=0; mWasted=0;
  aiHpMult=1;
  stats.kills=[0,0,0]; stats.built=[0,0]; stats.t=0; stats.nests=0; stats.reclaimed=0;
  heroLvl=1; heroXp=0; heroXpNext=100; pendingLevels=0;
  heroDmgMult=1; heroRegen=14; commanderHpMult=1;armyDmgMult=1; stimDmgMult=1; boostDmgT=0; bonusMass=0; bonusEnergy=0;
  playerBuildMult=1; blastRadius=110;
  titanCount[0]=0; titanCount[1]=0; teamCount[2]=0;
  beams.length=0; fogCov.fill(0); fogSeen.fill(0); fogSources.fill(0); fogScans.length=0;
  if(typeof mmPings!=='undefined') mmPings.length=0;
  meteors.length=0; stormTimer=200;
  ushielded.fill(0); umarch.fill(0);
  ufireT.fill(0); uheal.fill(0);
  if(typeof uCrash!=='undefined'){ uCrash.fill(0); ualt.fill(0); uCtime.fill(0); }
  ubroodLed.fill(0);uMineT.fill(0);uMineNode.fill(-1);broodMassT=0;
  researched={}; resDone=0; researchCarry={}; resHpMult=1; resRngMult=1; resEnergyMult=1;
  if(typeof resetTypeBoosts==='function') resetTypeBoosts();
  resBldHpMult=1; resDefDmgMult=1; labBufferMult=1;
  deformQ.length=0; musicInt=0; lastDmgTotal=0; dmgAccum=[0,0,0]; dayT=0.08; shLife.fill(0);
  /* FIVE entries: commander.js declares abUnlock with five and develop.js's

     craftable EMP module writes abUnlock[4]. Resetting to a four-entry array

     silently truncated it, so a paid-for research reward could never be set.

     (The ability itself is still not dispatched β€” see the note in commander.js.) */
  abCool=[0,0,0,0,0]; abUnlock=[false,false,false,false,false]; aiming=-1;
  /* Per-match timers that outlived their match: the alarm suppressor and the

     jump-jet cooldown. Both are compared against a clock that restarts at 0. */
  if(typeof alarmT!=='undefined') alarmT=0;
  if(typeof heroJumpCool!=='undefined') heroJumpCool=0;
  if(typeof artBarrageReset==='function') artBarrageReset();
  if(typeof classAbilityReset==='function') classAbilityReset();
  salvageMult=1; bldHpMult=1; matchClock=timeLimit; goalDone=false;
  infestT=230; nestSpreadT=170; infestLvl=0; tideT=260; birds.length=0; bugQ.length=0;
  if(typeof hazReset==='function') hazReset();          // map hazard clock
  uhold.fill(0);uPatrolRoute.fill(-1);uPatrolStep.fill(0);uPatrolSlot.fill(0);patrolRoutes.length=0;
  uMoveCohort.fill(-1);uCohesion.fill(1);moveCohorts.fill(null);moveCohortNext=0;
  armRally=-1;armPatrol=false;patrolDraft=null;armFormation=false;orderPreview=null;orderConfirm=null;novaSrc=-1;
  carrier.active=false; carrier.phase=2; carrier.alt=0; carrier.clearance=0; carrier.fac='nova';
  aiDeployArrivals.length=0; matchLive=false;
  camYaw=yawTarget=0.6; camPitch=pitchTarget=1.19; orthoSpan=distTarget=1500; camFollow=-1;
  crates.length=0; crateT=55; sitePickupT=28; boostDmgT=0;
  for(let g=0;g<4;g++){ctrlGroups[g]=[];groupForms[g]=0;}
  activePlatoon=-1;updateGroupBadges();patrolButtonState();setFormation(0,true);
  tl.length=0; tlLast=0;
  placing=null; openBld=-1; gameEnded=false;
  if(typeof clearWaveWarning==='function') clearWaveWarning();
  umode.fill(0); umodeT.fill(0); relief.length=0; wrecks.length=0;
  fortTimer=0; fortTierSeen=[0,0,0]; FORT.length=0; reclTip=0; razeTip=0; lastPlaceRot=0;
  const _mb=$('modeBtn'); if(_mb) _mb.style.display='none';
  showHudDock(false); $('camRow').style.display='flex';
  setupDeposits(); setupDoodads();
  closeMenus(); cancelPlace();
  $('gameOver').style.display='none';
  $('levelUp').style.display='none';
}

function setupOnBtn(sel){ return document.querySelector(sel); }
function commitSetupFromDom(){
  /* Deploy can highlight a pick whose live global was overwritten (training

     snapshot restore, shared .fbtn paint). Launch copies the buttons. Scoped

     per row so the player's Nova chip cannot be read as the enemy. */
  const mc=document.querySelector('#mapRow .mapCard.sel');
  if(mc&&mc.dataset.map&&typeof MAPDEFS!=='undefined'&&MAPDEFS[mc.dataset.map]
     &&typeof syncBattlefieldFromMap==='function'){
    const key=mc.dataset.map;
    const keepMedium=typeof activeWarMode!=='undefined'&&activeWarMode==='standard'
      &&MAPDEFS[key].size==='compact'&&MAPDEFS[curMap]&&MAPDEFS[curMap].size==='standard';
    if(!keepMedium) syncBattlefieldFromMap(key);
  }
  if(typeof activeWarMode!=='undefined'&&activeWarMode==='standard'
     &&typeof theatreMapId==='function'&&MAPDEFS[curMap]&&MAPDEFS[curMap].size!=='standard'
     &&window._mfTheatrePick!=='compact'&&window._mfTheatrePick!=='large'){
    const pk=typeof planetForMap==='function'?planetForMap(curMap):null;
    const P=pk&&typeof PLANETS!=='undefined'&&PLANETS[pk];
    const R=P&&P.regions.find(r=>r.maps&&r.maps.indexOf(curMap)>=0);
    const med=R&&theatreMapId(R.maps,'standard');
    if(med) syncBattlefieldFromMap(med);
  }
  const ons=[...document.querySelectorAll('#facRow .fbtn.on')];
  const specific=ons.find(b=>b.dataset.f&&b.dataset.f!=='random');
  if(specific&&(typeof FACTIONS!=='undefined'&&FACTIONS[specific.dataset.f])) aiFactionSel=specific.dataset.f;
  else if(!(aiFactionSel&&(aiFactionSel==='random'||(typeof FACTIONS!=='undefined'&&FACTIONS[aiFactionSel])))){
    const fac=ons[0];
    if(fac&&fac.dataset.f&&(fac.dataset.f==='random'||(typeof FACTIONS!=='undefined'&&FACTIONS[fac.dataset.f]))) aiFactionSel=fac.dataset.f;
    else if(typeof META!=='undefined'&&META&&META.setup&&META.setup.f
            &&(META.setup.f==='random'||(typeof FACTIONS!=='undefined'&&FACTIONS[META.setup.f])))
      aiFactionSel=META.setup.f;
  }
  if(typeof persistEnemyFacPick==='function') persistEnemyFacPick();
  const pf=setupOnBtn('#pfacRow .fbtn.on');
  if(pf&&pf.dataset.f&&typeof playableFactions==='function'&&playableFactions().includes(pf.dataset.f))
    playerFaction=pf.dataset.f;
  const cmd=setupOnBtn('#commanderRow .commanderCard.on')||setupOnBtn('#mfQuickCommanders .mfQuickCommander.on');
  if(cmd) playerCommanderId=cmd.dataset.commander||cmd.dataset.mfCommander||playerCommanderId;
  const gl=setupOnBtn('#goalRow .glbtn.on'); if(gl&&gl.dataset.g) goalSel=gl.dataset.g;
  const tm=setupOnBtn('#timeRow .tmbtn.on'); if(tm) timeLimit=+tm.dataset.t||0;
  const pc=setupOnBtn('#paceRow .pcbtn.on'); if(pc) resPace=+pc.dataset.p;
  const cr=setupOnBtn('#crRow .crbtn.on'); if(cr) crateRate=crateRateBase=+cr.dataset.c;
  const inf=setupOnBtn('#infestRow .ifbtn.on'); if(inf) infestationOn=!!+inf.dataset.i;
  const df=setupOnBtn('#defFocusRow .dfbtn.on'); if(df) defenseFocus=+df.dataset.df;
  const pkg=setupOnBtn('#deployPkgRow .pkgbtn.on');
  if(pkg&&DEPLOYMENT_PACKAGES[pkg.dataset.pkg]) deploymentPackage=pkg.dataset.pkg;
  const gd=setupOnBtn('#globalDiffRow .globalDiff.on')||setupOnBtn('.globalDiff.on');
  if(gd){
    difficulty=+gd.dataset.d;
    for(const A of aiSlots) if(A.on&&!A.ally) A.diff=difficulty;
  } else if(typeof activeEnemySlots==='function'&&activeEnemySlots().length)
    difficulty=Math.max(...activeEnemySlots().map(A=>A.diff));
  const wc=setupOnBtn('#wcRowSel .wbtn.on'); if(wc) wcChoice=+wc.dataset.w;
  const th=setupOnBtn('#threatRow .thBtn.on');
  if(th&&typeof META!=='undefined'&&META) META.threatSel=+th.dataset.t||META.threatSel;
}
let matchSetupArmed=false;
function armMatchSetup(){ commitSetupFromDom(); matchSetupArmed=true; }
function consumeMatchSetup(){ if(!matchSetupArmed) return; matchSetupArmed=false; commitSetupFromDom(); }
function mfRescueHiddenSetupCards(){
  /* Galaxy flow hides leftover .setupScroll > .setupCard. Landing package was

     never moved into Advanced, so Prepared/Expedition never reached the match.

     Rescue without touching the GALAXY→SYSTEM→PLANET stage machine. */
  const body=$('mfAdvancedBody'); if(!body) return;
  const el=$('deployPkgRow'), card=el&&el.closest('.setupCard');
  if(card&&!body.contains(card)){ card.removeAttribute('data-setup-tab'); body.appendChild(card); }
}

function newSkirmish(){
  const networkSetup=window.__MF_NETWORK_SETUP__&&window.__MF_NETWORK_SETUP__.schema===1;
  const recovering=typeof sessPending!=='undefined'&&!!sessPending;
  if(typeof invAbandonReadied==='function')invAbandonReadied();
  _mfMatchCons=[];_mfMatchGear=[];
  /* Game speed is a per-match control, not a profile setting. It was never

     reset here, so one tap to 1.5x or 2x silently carried into every later

     match in the session - and since it multiplies the whole simulation

     accumulator, everything simply ran fast with no indication why. */
  gameSpeed=1; try{ const _b=document.getElementById('spdBtn'); if(_b) _b.textContent='1Γ—'; }catch(e){}
  if(!networkSetup)consumeMatchSetup();
  if(!networkSetup&&typeof sessBindMatchProfile==='function')
    sessBindMatchProfile(recovering?sessPending.profileId:
      typeof PROFILES!=='undefined'&&PROFILES&&PROFILES.active);
  /* Pull the player faction's radio bank now, while the loading screen is up,

     so the first order of the match speaks instead of falling back to whatever

     the OS narrator happens to be. */
  try{ if(typeof voPrewarm==='function'&&typeof radioFaction==='function') voPrewarm((radioFaction()||{}).id||'nova'); }catch(e){}
  stopAttract();          // the menu diorama gives way to the real match
  normalizeAiSlotsForBattlefield(); // final contract gate before any ally can spawn
  resetWorld();
  demoMode=false;
  fogOn=networkSetup||META.settings.fog!==false;
  if(networkSetup){
    /* Online peers share a neutral match profile. Armory, modules and ready

       consumables belong to local accounts and cannot alter a common world or

       spend items in an unverified match. The account state is left untouched. */
    WC={};wcActive=[];crateRate=crateRateBase;
    if(typeof AB_CD!=='undefined'&&typeof AB_BASE!=='undefined')for(let i=0;i<AB_CD.length;i++)AB_CD[i]=AB_BASE[i];
    if(typeof bldSpeedMult!=='undefined')bldSpeedMult=1;
  }else{
    pickWildcards(wcChoice);                    // danger modifiers (chosen count, random draw)
    applyMetaPerks();                           // permanent Armory perks
    if(typeof applyModules==='function') applyModules();   // crafted modules, on top
  }
  /* Preview the loadout now, but commit its one-use charges only when the

     carrier actually deploys. Recovery restores captured modifiers and never

     consumes a second charge from the account bag. */
  if(!networkSetup&&!recovering&&typeof invApplyLoadout==='function'){
    const _b=invBag();
    _mfMatchGear=[];
    for(const s of ['weapon','armor','utility']){ const g=INV_GEAR.find(x=>x.id===_b.equipped[s]); if(g) _mfMatchGear.push(g); }
    _mfMatchCons=invApplyLoadout();
  }
  if(typeof applyCommanderChoice==='function')applyCommanderChoice();
  if(typeof applyFactionDoctrineChoice==='function')applyFactionDoctrineChoice();
  if(WC.meteor||(typeof mapHazardKey==='function'&&mapHazardKey(curMap)==='meteor')) stormTimer=40+mfSimRandom()*30;
  renderWcRow();
  /* ---- orbital drop ----------------------------------------------------

     Spawns are inset from the map corners. The camera clamps its VIEW to the

     battlefield, so a ship parked at 16% of the map sat in a corner the

     camera physically could not centre β€” you could neither see it properly

     nor fly it. 26% keeps both starts comfortably reachable while leaving

     plenty of ground between them. */
  const starts=skirmishSpawnPoints(), PS=starts[0];
  const pxs=PS.x, pys=PS.y;
  matchLive=false;
  carrier.active=true; carrier.phase=0; carrier.alt=980; carrier.clearance=0;
  /* The drop is the FIRST thing anyone sees of their own army, and it was

     always Nova β€” "Nova is the current playable army" stopped being true once

     the faction picker went into battle setup. It now arrives in the colours

     and hardware of whatever the player actually chose. The query override

     stays, ahead of the choice, so QA can still inspect any registered

     deployer without touching progression. */
  const carrierFacQa=(location.search.match(/[?&]carrierfac=(nova|terran|frontline|federation|legion|ascendancy|red|syndicate|machine|coalition|horde|brood|swarm|infestation)/)||[])[1]||'';
  const carrierFacOwn=(typeof playerKitKey==='function')?playerKitKey():'nova';
  carrier.fac=typeof dropFactionKey==='function'?dropFactionKey(carrierFacQa||carrierFacOwn):'nova';
  carrier.x=pxs; carrier.y=pys; carrier.tx=pxs; carrier.ty=pys; carrier.ang=0; carrier.dust=0;
  // AI start (mirrored)
  /* The enemy fields its FACTION'S hero, not a mirror of yours. The Legion

     walks a siege battery, the Syndicate a shielded hover platform, the Horde a

     brood hive that keeps spawning β€” so "kill the enemy Commander" is a

     different fight depending on who you drew. */
  aiPickFaction();          // must precede the hero spawn AND setupNests()
  /* Upload only this match's enemy architecture kit. Deferring the other two

     avoids keeping three unused structure libraries resident on phones. */
  if(typeof ensureBldFactionMeshes==='function'){
    ensureBldFactionMeshes(AI.fac);
    /* The player's own architecture too. bldMeshFor() would fault it in lazily,

       but that lands on the frame the first structure finishes rather than

       here in the carrier phase where an allocation is invisible. */
    if(typeof playerKitKey==='function') ensureBldFactionMeshes(playerKitKey());
    /* Neutral nests use Horde architecture even when another faction owns the

       enemy base. Upload it during the carrier/deployment phase, where a short

       allocation is invisible, instead of hitching the first frame a nest

       enters view. If infestation is disabled no second kit is resident. */
    if(infestationOn&&AI.fac!=='horde') ensureBldFactionMeshes('horde');
  }
  const FH=FACTIONS[AI.fac]||FACTIONS.legion;
  const ownFH=(typeof FACTIONS!=='undefined'&&FACTIONS[playerFaction])||null;
  const aiBases=[],allyBases=[];
  const enemyRosterUsed=[], allyRosterUsed=playerCommanderId?[playerCommanderId]:[];
  for(const S of starts.slice(1)){
    const exs=S.x,eys=S.y,egx=Math.round(exs/SNAP_GRID)*SNAP_GRID,egy=Math.round(eys/SNAP_GRID)*SNAP_GRID;
    const ally=!!S.ally,team=ally?0:1,fac=ally?carrierFacOwn:AI.fac;
    /* Chassis is the faction hero type. Identity is a distinct roster id so

       Large 1v3 is not three Vexes / Renns / Sovereigns. */
    const heroType=ally?(ownFH&&ownFH.hero!=null?ownFH.hero:4):(FH.hero!=null?FH.hero:4);
    const rosterFac=ally?(typeof commanderFactionKey==='function'?commanderFactionKey(playerFaction):playerFaction)||'nova':AI.fac;
    const cmd=spawnAiRosterPick(rosterFac, ally?allyRosterUsed:enemyRosterUsed);
    const h=spawnUnit(heroType,team,exs,eys,S.slot);
    if(h>=0){if(ally)uAllyBase[h]=S.slot;else enemyHeroIdxs.push(h);}
    const a=Math.atan2(MAP*.5-eys,MAP*.5-exs),fx=Math.cos(a),fy=Math.sin(a),rx=-fy,ry=fx;
    /* B.allyAI had FOUR readers and ZERO writers. Ally bases stamped only

       aiBaseSlot, so: the ally-factory lookup in aiAllyTick never matched and

       allies NEVER produced a unit after their opening guard squad; the

       econTick guard never fired, so ally reactors donated their income to

       the player bank - the exact thing its own comment forbids; and

       commanderSlotForBuilding billed every ally structure to the player

       population seat. One missing stamp, three shipped bugs. */
    const baseB=(type,x,y)=>{const B=addBld(type,team,x,y,true);B.fac=fac;B.aiBaseSlot=S.slot;if(ally)B.allyAI=S.slot;B.aiBehavior=aiBehaviorKey(S.behavior);if(ally)B.allyAI=S.slot;return B;};
    baseB('pgen',egx+fx*105+rx*45,egy+fy*105+ry*45);
    baseB('fac',egx+fx*105-rx*70,egy+fy*105-ry*70);
    if(S.diff>=1)baseB('turret',egx+fx*178,egy+fy*178);
    if(S.diff>=2){
      baseB('turret',egx+fx*125+rx*155,egy+fy*125+ry*155);
      baseB('pgen',egx+fx*45-rx*145,egy+fy*45-ry*145);
    }
    const guard=[1,3,5][S.diff];
    for(let k=0;k<guard;k++){
      const ty=S.diff>=2&&k>2?1:0;
      const u=spawnUnit(ty,team,egx+fx*(210+mfSimRange(-30,45))+rx*mfSimRange(-100,100),egy+fy*(210+mfSimRange(-30,45))+ry*mfSimRange(-100,100),S.slot);
      if(u>=0){ustate[u]=1;utx[u]=egx;uty[u]=egy;if(ally)uAllyBase[u]=S.slot;}
    }
    let bd=1e12,bi=-1;
    for(let d=0;d<deposits.length;d++){ const D=deposits[d];
      if(D.taken||depositTier(D)<=0) continue; const dd=dist2(D.x,D.y,exs,eys); if(dd<bd){bd=dd;bi=d;} }
    if(bi>=0){ deposits[bi].taken=true; baseB('mex',deposits[bi].x,deposits[bi].y); }
    /* Store the generation alongside the slot so ai.js can tell "still the same

       Commander" from "that slot was recycled into somebody else's unit". */
    const base={x:egx,y:egy,diff:S.diff,slot:S.slot,behavior:aiBehaviorKey(S.behavior),commander:h,fac,
                commanderId:cmd&&cmd.id,commanderNm:cmd&&cmd.nm,
                commanderGen:(h>=0&&typeof ugen!=='undefined')?ugen[h]:null,spawnT:18,orderT:4,mass:220,energy:900};
    (ally?allyBases:aiBases).push(base);
    aiDeployArrivals.push({x:egx,y:egy,fac,ang:a+Math.PI,depart:0});
  }
  livingEnemyCommanders();
  setupNests();
  setupRelics();
  /* Deterministic device/browser QA scene. It is inert in every normal game;

     the query hook lets release checks reproduce a roof crossing or a city

     landing without relying on a lucky random camera route. */
  const carrierQa=(location.search.match(/[?&]carrierqa=(fly|land)/)||[])[1]||'';
  let carrierQaFocus=null;
  if(carrierQa&&relics.length){
    const R=relics.find(o=>o.alive&&o.kind===0)||relics.find(o=>o.alive);
    if(R){
      carrier.phase=1; carrier.alt=0; carrier.ang=0;
      if(carrierQa==='fly'){
        carrier.x=clamp(R.x-220,70,MAP-70); carrier.y=R.y;
        carrier.tx=clamp(R.x+220,70,MAP-70); carrier.ty=R.y;
        carrier.clearance=0;
      } else {
        carrier.x=R.x; carrier.y=R.y; carrier.tx=R.x; carrier.ty=R.y;
        carrier.clearance=carrierObstacleClearanceAt(R.x,R.y,0);
      }
      carrierQaFocus=R;
      window.__carrierQa={mode:carrierQa,roofX:R.x,roofY:R.y,roofKind:R.kind,
                          blocksBefore:carrierLandingBlockCount(),deployed:false};
    }
  }
  aiSetup(difficulty,aiBases,allyBases);
    /* Open framed on the ship, close enough to fly it. */
  cam.x=carrierQaFocus?carrierQaFocus.x:pxs;
  cam.y=carrierQaFocus?carrierQaFocus.y:pys;
  orthoSpan=distTarget=carrierQaFocus?520:680; clampCam(); camUpdateMatrices();
  camFollow=-1;
  running=true; paused=false;
  updateFog();
  showHudDock(false); $('camRow').style.display='flex';
  toast('β˜„ ORBITAL DROP β€” your super carrier is coming down');
  sfx('flyby',carrier.x,carrier.y,0.72);
}

/* ---------- deploy the carrier: this is what actually starts the match ---------- */
function deployCarrier(){
  /* LANDING HAPPENS ONCE.



     Resuming a session arms the carrier through newSkirmish() and then polls

     every 250ms to land it automatically, because a player nine minutes into a

     fight should not be asked to pick a drop site again. Meanwhile the DEPLOY

     button is still on screen - this function is what hides it - and its

     handler calls straight in with no check of its own.



     So there are two callers racing, and the only thing between them was the

     poll's own `if(matchLive) return`. matchLive is not set until partway

     through the landing, well after the HQ, the commander and the constructor

     have been spawned, so a tap inside that window ran the whole sequence a

     second time: two drops, two HQs, two starting armies.



     carrierCanDeploy() cannot catch this - it asks whether the GROUND is

     landable, not whether you have already landed on it. The guard belongs

     here rather than on either caller, so every future entry point inherits it.

     Silent on purpose: a duplicate is not a player error to report. */
  if(typeof matchLive!=='undefined'&&matchLive) return;
  if(!carrierCanDeploy()){
    toast('β›” Cannot deploy here β€” need solid, flat ground clear of active structures');
    sfx('alarm'); return;
  }
  if(!window.__MF_NETWORK_SETUP__&&!(typeof sessPending!=='undefined'&&sessPending)&&
     typeof invCommitReadied==='function'&&!invCommitReadied()){
    toast('Supply save failed β€” deployment paused. Retry when account storage is available');
    sfx('alarm'); return;
  }
  /* Snap the landing point to the build grid. The HQ anchors every later

     placement β€” structures axis-align to their neighbours β€” so an off-grid HQ

     silently pushed the whole base half a cell out of alignment forever. */
  const snap=carrierSnapPosition();
  const cx2=snap[0], cy2=snap[1];
  carrier.x=cx2; carrier.y=cy2;
  const landing=carrierClearLandingZone(cx2,cy2);
  if(window.__carrierQa){
    window.__carrierQa.deployed=true;
    window.__carrierQa.blocksCleared=landing.blocks;
    window.__carrierQa.tanksCleared=landing.tanks;
    window.__carrierQa.overlapsAfter=carrierLandingRelics(cx2,cy2).length;
  }
  carrier.phase=2; carrier.active=false;
  const departNow=performance.now()/1000;
  for(const A of aiDeployArrivals) A.depart=departNow;
  /* You land with a Carrier HQ, your Commander and ONE constructor β€” nothing

     else. Handing the player a pre-built reactor, factory and extractor made

     the opening a formality; the layout that matters most is the one you lay

     down yourself, and it should be yours from the first structure. The HQ

     generates enough on its own to fund that first move. */
  const hq=addBld('hq',0,cx2,cy2,true);
  /* Visual-only unfolding window. The HQ exists immediately for simulation

     and placement rules, while the renderer grows it out of the landing site

     instead of making the dropship vanish and a finished base pop into view. */
  hq.deployT=performance.now()/1000;
  /* You field YOUR faction's hero, the same way the enemy fields theirs. The

     Dominion walks Lord Darion Vex, the Syndicate a shielded hover platform,

     the Brood its Sovereign β€” and Nova keeps the Commander. Until now only the

     AI got this, so choosing a faction changed who you fought as everywhere

     except the one unit you spend the whole match looking at.

     No hero builds anything β€” `builder:1` belongs to the Constructor, which

     lands beside you regardless β€” so this swaps a fighter, not an economy. */
  const PH=(typeof FACTIONS!=='undefined'&&FACTIONS[playerFaction])||null;
  heroIdx=spawnUnit(PH&&PH.hero!=null?PH.hero:4,0,cx2+52,cy2+44);
  spawnUnit(UT_ENGINEER,0,cx2-52,cy2+44);
  /* PREPARED LANDING is explicit onboarding support, not a hidden economy

     bonus. The AI already owns these two structures. addBld deliberately owns

     the placement so prefab ground is graded through the normal foundation

     path instead of hovering over the landing plateau. */
  if(deploymentPackage==='prepared'){
    const snapB=(v)=>Math.round(v/SNAP_GRID)*SNAP_GRID;
    const sites=[['pgen',snapB(cx2-102),snapB(cy2+4)],['fac',snapB(cx2+112),snapB(cy2+6)]];
    for(const S of sites){
      const B=addBld(S[0],0,S[1],S[2],true);
      B.deployT=performance.now()/1000+0.14;
    }
  }
  AI.base.x=AI.base.x; // (AI base already placed)
  matchLive=true; stats.t=0; matchClock=timeLimit;
  if(typeof commanderVoicePrewarm==='function') commanderVoicePrewarm(playerCommanderId);
  /* The HUD drains gameplay dialogue on the fixed-step match clock, not page

     time. This cue is raised at the same zero boundary as stats.t so a slow

     boot cannot age it before the first simulated battle frame. */
  if(typeof commanderCue==='function') commanderCue('objective','assigned',{
    subject:(typeof goalDef==='function'&&goalDef().id)||'match',now:0,force:true
  });
  /* The setup modifier strip is useful before launch but becomes a duplicate

     of the compact MOD chip once the battle begins. Hide it at the state

     transition instead of waiting for another setup render that never comes. */
  if(typeof renderWcRow==='function')renderWcRow();
  if(typeof storyCampaignOnDeploy==='function')storyCampaignOnDeploy();
  /* A dropped session is laid down here, AFTER the world has regenerated from

     the same setup and seed β€” so we are restoring a fight onto a battlefield

     that came back identically, not trying to persist the battlefield itself. */
  if(typeof sessPending!=='undefined'&&sessPending){
    const snap=sessPending; sessPending=null;
    window.__mfSessionFallback=null;
    const restored=typeof sessRestoreInto==='function'&&sessRestoreInto(snap);
    if(restored){
      toast('β—ˆ SESSION RECOVERED β€” '+Math.round((snap.t||0)/60)+' minutes restored');
      if(typeof sfx==='function') sfx('deploy');
    }else{
      /* The restore owns a destructive replay after its preflight. If an

         unexpected replay invariant still fails, regenerate immediately and

         return before this abandoned landing emits effects into the clean

         world. The rejected snapshot was already cleared by session.js. */
      const code=window.__mfSessionReject||'SESSION_RESTORE_FAILED';
      if(typeof newSkirmish==='function')newSkirmish();
      window.__mfSessionFallback={code,fresh:true};
      toast('β—ˆ SESSION COULD NOT BE RECOVERED Β· FRESH DEPLOYMENT READY');
      return;
    }
  }
  shake=Math.max(shake,16); flashScreen();
  addParticle(3,cx2,cy2,0,0,1.2,420, 140,220,255);
  for(let k=0;k<34;k++){
    const a=Math.random()*TAU, sp=110+Math.random()*220;
    addParticle(1,cx2,cy2,Math.cos(a)*sp,Math.sin(a)*sp,1.7,30, 150,142,124);
  }
  /* Landing destroys the surroundings, not the ground the new HQ already

     occupies. The old single crater ran AFTER addBld() flattened and paved the

     foundation, excavating a lake directly beneath the base. Four shallow

     exhaust scars sit outside the HQ footprint and leave its engineered pad

     intact while preserving the violent deployment event. */
  for(let q=0;q<4;q++){
    const a=q*TAU/4+Math.PI*.25,rx=cx2+Math.cos(a)*118,ry=cy2+Math.sin(a)*118;
    deformTerrain(rx,ry,42,0.012);
    if(typeof addGroundBurn==='function')addGroundBurn(rx,ry,58,0);
  }
  sfx('carrier_deploy',cx2,cy2,1.18); buzz(45);
  showHudDock(true,'orders');
  startFirstContactGuide();
  $('deployBtn').style.display='none';
  updateFog();
  if(wcActive.length) toast('⚠ '+wcActive.length+' modifiers active Β· +'+Math.round((wcRewardMult()-1)*100)+'% payout β€” tap MOD for details');
  else if(landing.blocks||landing.tanks) toast('πŸ™ LANDING ZONE CLEARED β€” '+landing.blocks+' city block'+(landing.blocks===1?'':'s')+' demolished');
  else toast('🏭 BASE DEPLOYED β€” your HQ powers itself. Build ⛏ Extractors on β—† deposits');
  if(typeof showModSplash==='function') showModSplash();
  setTimeout(()=>{ if(running&&matchLive) toast('⬑ BUILD GRID β€” establish inside HQ range; research a Targeting Array to extend territory'); },5200);
}

function newDemo(){
  /* The 10,000-unit Mega / SANDBOX bench is GONE. It was a player-facing

     stress toy that no longer represented the game, and it sat on the front

     strip beside CAREER and INTEL as if it were a mode. newDemo() now serves

     only the three hidden QA capture labs; with no lab token in the URL there

     is nothing to enter, so bail before touching world or HUD state. */
  const cannonLab=location.search.indexOf('cannonshow=1')>=0;
  const defenseLab=location.search.indexOf('defenseshow=1')>=0;
  const ammoLab=location.search.indexOf('ammoshow=1')>=0;
  if(!cannonLab&&!defenseLab&&!ammoLab) return;
  stopAttract();
  resetWorld();
  demoMode=true; matchLive=true;
  carrier.active=false; carrier.phase=2;
  WC={}; wcActive=[]; renderWcRow();
  showHudDock(true,'powers');
  $('deployBtn').style.display='none';
  fogOn=false;
  /* Hidden device-QA layout. `?cannonshow=1` keeps the real combat simulation

     but brings smaller opposing lines into cannon range for readable captures. */
  if(defenseLab){
    const cx=MAP/2, cy=MAP/2+45, g=v=>Math.round(v/SNAP_GRID)*SNAP_GRID;
    const base=[
      ['hq',0,215],['techlab',0,120],['sgen',-125,135,3],['uplink',130,140,3],
      ['bunker',-250,35,3],['turret',-155,35,3],['rail',-55,25],['bastion',55,25,3],
      ['aatower',155,35],['hellstorm',250,35,3],
      ['minelaser',-205,-85,3],['arc',-100,-90,3],['plasma',0,-90,3],
      ['missilebastion',110,-90,3],['nova',220,-85,3],
      ['wall',-200,-195],['wall',-145,-195],['wall',-90,-195],['gate',-30,-195],
      ['gate',30,-195],['wall',90,-195],['wall',145,-195],['wall',200,-195]
    ];
    for(const [ty,dx,dy,lvl] of base){ const B=addBld(ty,0,g(cx+dx),g(cy+dy),true,0); if(lvl) B.lvl=lvl; }
    for(let k=0;k<22;k++){
      const a=(k-10.5)*.105, d=280+(k%4)*28;
      const ty=[0,1,2,4,9,20,21,23][k%8];
      let i=spawnUnit(ty,1,cx+Math.sin(a)*d,cy-190-Math.cos(a)*d*.35);
      if(i>=0){ ustate[i]=2; utx[i]=cx+mfSimRange(-90,90); uty[i]=cy+40; }
    }
    for(let k=0;k<12;k++){
      const ty=[0,1,2,9,11,19][k%6];
      const i=spawnUnit(ty,0,cx-130+k%6*52,cy+105+(k>5?38:0));
      if(i>=0){ ustate[i]=3; utx[i]=ux[i]; uty[i]=uy[i]; }
    }
    cam.x=cx; cam.y=cy-5; orthoSpan=distTarget=860; camPitch=pitchTarget=1.12; camYaw=yawTarget=.08;
    clampCam(); camUpdateMatrices(); running=true; paused=false;
    toast('πŸ›‘ DEFENSIVE NETWORK β€” 10 contact-sheet structure families live-fire');
    return;
  }
  if(ammoLab){
    /* One mirrored duel per armed chassis. A dense army turns every munition

       into the same white explosion; separated lanes let the player actually

       read the launch, flight and impact language of each weapon family. */
    const roster=[0,1,2,3,4,6,7,8,9,10,14,15,16,17,18,20,21,22,23,25,26,27,28,29];
    const cx=MAP/2, cy=MAP/2;
    for(let k=0;k<roster.length;k++){
      const col=k%6, row=(k/6)|0, mx=cx+(col-2.5)*175, my=cy+(row-1.5)*175;
      const a=spawnUnit(roster[k],0,mx-52,my), b=spawnUnit(roster[k],1,mx+52,my);
      if(a>=0&&b>=0){
        /* Showcase durability only: keep both emitters alive long enough to

           compare several volleys instead of capturing 23 simultaneous deaths. */
        uhpm[a]*=10; uhp[a]=uhpm[a]; uhpm[b]*=10; uhp[b]=uhpm[b];
        utgt[a]=b; utgtg[a]=ugen[b]; ustate[a]=2; utx[a]=ux[b]; uty[a]=uy[b];
        utgt[b]=a; utgtg[b]=ugen[a]; ustate[b]=2; utx[b]=ux[a]; uty[b]=uy[a];
      }
    }
    cam.x=cx; cam.y=cy; orthoSpan=distTarget=800; camPitch=pitchTarget=1.08; camYaw=yawTarget=.05;
    clampCam(); camUpdateMatrices(); running=true; paused=false;
    toast('⚑ AMMUNITION LAB β€” 24 mirrored weapon duels running live');
    return;
  }
  /* 72 a side, not 5000: the only caller left is the cannon capture lab. */
  const perSide=72, cols=12;
  for(let k=0;k<perSide;k++){
    const col=k%cols, row=(k/cols)|0;
    /* The stress battle also doubles as a visual combat showcase. Keep the

       rank-and-file majority cheap, but seed both armies with every major

       weapon language: lance/sniper/thermal/sonic beams, missiles, incendiary

       shells and heavy kinetic rounds. */
    const t = cannonLab ? (k%18===0?4:(k%5===0?1:0))
            : k%503===0?4 : k%251===0?8 : k%43===0?18 : k%31===0?6 : k%67===0?23
            : k%79===0?7 : k%109===0?20 : k%83===0?21
            : k%23===0?2 : k%9===0?1 : k%37===0?3 : 0;
    const nearY=cannonLab?0.525:0.72, farY=cannonLab?0.475:0.28;
    // team 0 bottom half marching up
    let i=spawnUnit(t,0, MAP/2+(col-cols/2)*22+mfSimRange(-6,6), MAP*nearY+row*16+mfSimRange(-5,5));
    if(i>=0){ ustate[i]=2; utx[i]=ux[i]; uty[i]=MAP*farY; }
    i=spawnUnit(t,1, MAP/2+(col-cols/2)*22+mfSimRange(-6,6), MAP*farY-row*16+mfSimRange(-5,5));
    if(i>=0){ ustate[i]=2; utx[i]=ux[i]; uty[i]=MAP*nearY; }
  }
  cam.x=MAP/2; cam.y=MAP/2; orthoSpan=distTarget=cannonLab?720:3200; clampCam(); camUpdateMatrices();
  running=true; paused=false;
  toast('πŸ’₯ COMMANDER CANNON QA β€” live damage and effects');
}

function mfResolveBattlefieldOutcome(win,reason){
  const C=typeof window!=='undefined'&&window.MFMatchCommandConsumer;
  /* The relay does not yet verify ordinary victories. Do not pass a local win

     to downstream victory wrappers, which could offer the next paid map. */
  const online=!!(C&&typeof C.sessionActive==='function'&&C.sessionActive());
  endGame(online?false:win,online?'Local battlefield stopped; the relay has not confirmed the outcome.':reason);
}
function checkVictory(){
  if(gameEnded||!running||(!matchLive&&!demoMode)) return;
  if(demoMode){
    if(teamCount[0]===0||teamCount[1]===0){
      mfResolveBattlefieldOutcome(teamCount[1]===0,'Simulation complete');
    }
    return;
  }
  // losing your Commander always ends the run
  if(heroIdx<0){ mfResolveBattlefieldOutcome(false,'Your Commander was destroyed'); return; }
  const g=goalDef();
  if(g.id==='annihilate'){
    if(livingEnemyCommanders().length===0){ mfResolveBattlefieldOutcome(true,'Enemy commanders destroyed'); return; }
  } else if(g.id==='purge'){
    if(liveNests().length===0){ mfResolveBattlefieldOutcome(true,'Every hive purged from the surface'); return; }
    if(livingEnemyCommanders().length===0&&liveNests().length===0){ mfResolveBattlefieldOutcome(true,'Planet cleansed'); return; }
  } else if(g.id==='survival'){
    /* Last Stand at UNLIMITED had a defeat condition and no victory condition

       at all: the kill-the-commander win is excluded for survival, and the

       timed resolver below is gated on timeLimit>0. A player could wipe the

       enemy off the map and the match would simply keep running. Breaking the

       siege outright is a win by any honest reading. */
    if(timeLimit<=0&&livingEnemyCommanders().length===0){ mfResolveBattlefieldOutcome(true,'The siege was broken'); return; }
  } else {
    if(livingEnemyCommanders().length===0){ mfResolveBattlefieldOutcome(true,'Enemy commanders destroyed'); return; }
  }
  /* NOTHING ON A PHONE MAY RUN FOREVER. "Unlimited" means no SCHEDULED end, not

     no end β€” and against an AI commander parked inside a fortified base, an

     unlimited match could run until the player force-quit. A force-quit never

     reaches endGame(), so it also paid nothing. Forty minutes is past any

     honest match and short of a dead battery. */
  if(timeLimit<=0&&stats.t>=MATCH_HARD_CAP){
    const a=territoryScore(0), b=territoryScore(1);
    mfResolveBattlefieldOutcome(a>=b,'Stalemate β€” decided on territory'); return;
  }
  // timed goals resolve on the clock
  if(timeLimit>0&&matchClock<=0){
    if(g.id==='domination'){
      const a=territoryScore(0), b=territoryScore(1);
      mfResolveBattlefieldOutcome(a>=b, a>=b?'Time β€” you held the most territory':'Time β€” the enemy held more territory');
    } else if(g.id==='survival') mfResolveBattlefieldOutcome(true,'Time β€” you survived the siege');
    else if(g.id==='purge') mfResolveBattlefieldOutcome(false,'Time β€” hives still stand');
    else mfResolveBattlefieldOutcome(territoryScore(0)>=territoryScore(1),'Time β€” decided on territory');
  }
}
let mfResultPresentationEpoch=0,mfLastResultCanRestart=false;
function endGame(win,reason){
  const C=typeof window!=='undefined'&&window.MFMatchCommandConsumer,
    online=!!(C&&typeof C.sessionActive==='function'&&C.sessionActive()),
    networkStatus=online&&window.MFMatchRuntime&&typeof MFMatchRuntime.status==='function'?MFMatchRuntime.status():null,
    localSeat=networkStatus&&networkStatus.seat||0,
    localAuthority=online&&C&&typeof C.seatAuthority==='function'?C.seatAuthority(localSeat):null,
    localTeam=localAuthority&&localAuthority.team===1?1:0,hostileTeam=localTeam===0?1:0;
  if(online&&gameEnded)return;
  /* Weekly and authored missions give their borrowed plan back while scoring

     the result. Capture retry eligibility before that cleanup changes mode

     flags; a defeated UGA ticket or online seat must never be replayed here. */
  const bridge=typeof window!=='undefined'&&window.__MF_GALACTIC_BRIDGE;
  mfLastResultCanRestart=!win&&!online&&!demoMode&&activeWarMode==='standard'
    &&!(typeof weeklyMode!=='undefined'&&weeklyMode)
    &&!(typeof storyCampaignActiveId!=='undefined'&&storyCampaignActiveId)
    &&!(typeof trainingMissionActive==='function'&&trainingMissionActive())
    &&!window.__MF_NETWORK_SETUP__
    &&!(bridge&&(bridge.active||bridge.isolation&&bridge.isolation.active));
  const presentationEpoch=++mfResultPresentationEpoch;
  /* Emit the terminal commander line before gameEnded hides the battlefield

     rail. Audio may be absent, but the deterministic event still reaches the

     transcript and any active presentation listener exactly once. */
  if(!online&&!gameEnded&&typeof commanderCue==='function'){
    const cueNow=Math.max(0,(Number(stats.t)||0)*1000);
    commanderCue('outcome',win?'victory':'defeat',{subject:String(reason||'match'),now:cueNow,force:true});
    if(typeof commanderDialogueDrain==='function') commanderDialogueDrain(cueNow);
  }
  gameEnded=true;
  if(online){
    /* This is a local or relay terminal, not a verified payable outcome. Stop

       immediately and release the socket so the room cannot tick forever. The

       consumer keeps match ownership latched until explicit menu cleanup. */
    running=false;paused=false;
    if(window.MFMatchRuntime&&typeof MFMatchRuntime.close==='function')MFMatchRuntime.close();
  }
  // report match result to a hosting shell (Base44 app) if embedded
  try{if(!online){
    if(window.parent!==window) window.parent.postMessage({
      type:'massfront-result', win:!!win, demo:!!demoMode,
      difficulty, theme:curTheme, duration:stats.t|0,
      kills:stats.kills[0]|0, losses:stats.kills[1]|0,
      heroLevel:heroLvl|0, built:stats.built[0]|0
    },'*');
  }}catch(e){}
  const rw=demoMode||online?null:metaGrant(win);      // cross-game rewards (persisted immediately)
  /* The endgame layer scores the run, advances the threat ladder, ticks the

     mastery grid and records a weekly best β€” all from what actually happened. */
  const dv=demoMode||online?null:(typeof developRecord==='function'?developRecord({
    win:!!win, kills:stats.kills[0]|0, built:(stats.built[0])|0, nests:stats.nests|0,
    fieldMass:Math.max(0,Math.floor(resM[0]||0)),fieldEnergy:Math.max(0,Math.floor(resE[0]||0)),
    reclaimed:Math.max(0,Math.round(stats.reclaimed||0))}):null);
  const eg=demoMode||online?null:(typeof endgameRecord==='function'?endgameRecord({
    win:!!win, kills:stats.kills[0]|0, built:(stats.built[0])|0,
    seconds:stats.t|0, difficulty:difficulty|0}):null);
  /* The dispatch waits for the results screen to be dismissed β€” a story beat

     landing on top of a victory screen reads as an interruption. */
  setTimeout(()=>{
    if(presentationEpoch!==mfResultPresentationEpoch)return;
    running=false;
    $('goTitle').textContent=online?'ONLINE RESULT UNVERIFIED':win?'MISSION COMPLETE':'MISSION FAILED';
    $('goTitle').style.color=online?'#f4cf86':win?'#9fffc4':'#ff8d7a';
    const mins=(stats.t/60)|0, secs=(stats.t%60)|0;
    const outcome=$('goOutcome');
    if(outcome) outcome.textContent=(online?'NO PROGRESSION REWARD Β· ':'')+goalDef().nm.toUpperCase()+' Β· '+reason;
    $('goStats').innerHTML='<div class="goStatGrid">'
      +'<div><b>'+mins+'m '+secs+'s</b><span>MISSION TIME</span></div>'
      +'<div><b>'+stats.kills[localTeam]+'</b><span>HOSTILES DESTROYED</span></div>'
      +'<div><b>'+stats.kills[hostileTeam]+'</b><span>UNITS LOST</span></div>'
      +'<div><b>'+(online?'SEAT '+localSeat:'LV '+heroLvl)+'</b><span>'+(online?'LOCAL SEAT':'COMMANDER')+'</span></div>'
      +'<div><b>'+stats.built[localTeam]+'</b><span>STRUCTURES BUILT</span></div>'
      +'<div><b>'+(eg&&eg.score?eg.score.toLocaleString():'β€”')+'</b><span>OPERATION SCORE</span></div></div>';
    if(rw){
      const fld=rw.field||{mass:0,energy:0,reclaimed:0};
      const mats=dv?Object.keys(dv.mats).filter(k=>dv.mats[k]>0).map(k=>
        '<div class="goRes"><b>'+MATS[k].em+' '+dv.mats[k]+'</b><span>'+MATS[k].nm.toUpperCase()+'</span></div>').join(''):'';
      const dataBreak=(rw.dataParts||[]).map(p=>'<span>'+p[0]+' <b>+'+p[1]+'</b></span>').join('');
      let loot='';
      if(rw.loot&&rw.loot.gear){
        const g=rw.loot.gear, rr=invRarity(g.rarity);
        loot+='<div class="goLoot" style="--rar:'+rr.col+'"><span class="goLootEm">'+g.em+'</span><div><i>'+rr.nm+' GEAR</i><b>'+g.nm+'</b>'
          +mfOwnershipBadgeHTML('equipped')+'<small>'+(g.duplicate?'DUPLICATE Β· OWNED Γ—'+g.count:'AUTO-EQUIPPED IF SLOT WAS EMPTY')+'</small></div></div>';
      }
      if(rw.loot) for(const c of rw.loot.consumables||[]){
        const rr=invRarity(c.rarity);
        loot+='<div class="goLoot" style="--rar:'+rr.col+'"><span class="goLootEm">'+c.em+'</span><div><i>'+rr.nm+' '+(c.exclusive?'MODE REWARD':'CONSUMABLE')+'</i><b>'+c.nm+' Γ—'+c.count+'</b>'+mfOwnershipBadgeHTML('match')+'<small>'+(c.exclusive?'EXCLUSIVE '+String(c.mode||'').toUpperCase()+' VICTORY SUPPLY':'BANKED TO ARMORY INVENTORY')+'</small></div></div>';
      }
      $('goRewards').innerHTML='<section class="goSection"><h3>MISSION PAYOUT'
        +(rw.modeContract&&rw.modeContract.xp>1?'<small>'+rw.modeContract.nm+' Β· +'+Math.round((rw.modeContract.xp-1)*100)+'% XP</small>'
          :rw.mult>1.001?'<small>Γ—'+rw.mult.toFixed(2)+' OPERATION MULTIPLIER</small>':'')+'</h3>'
        +'<div class="goPayout"><div><b>+'+rw.xp+'</b><span>XP</span></div><div><b>+'+rw.cores+'</b><span>⬑ CORES</span></div>'
        +'<div><b>+'+(rw.data||0)+'</b><span>β—† RESEARCH DATA</span></div></div>'
        +'<div class="goBreak">'+rw.parts.map(p=>'<span>'+p[0]+' <b>+'+p[1]+'</b></span>').join('')+'</div>'
        +(dataBreak?'<div class="goBreak research"><strong>RESEARCH DATA</strong>'+dataBreak+'</div>':'')+'</section>'
        +'<section class="goSection"><h3>FIELD RECOVERY</h3><div class="goResourceGrid">'
        +'<div class="goRes"><b>'+fld.mass.toLocaleString()+'</b><span>MASS IN RESERVE</span></div>'
        +'<div class="goRes"><b>'+fld.energy.toLocaleString()+'</b><span>ENERGY IN RESERVE</span></div>'
        +'<div class="goRes"><b>'+fld.reclaimed.toLocaleString()+'</b><span>MASS RECLAIMED</span></div></div>'
        +(mats?'<h4 class="goAccountSalvage">ACCOUNT SALVAGE <small>Persisted to Development inventory</small></h4>'
          +'<div class="goResourceGrid account">'+mats+'</div>':'')+'</section>'
        +(loot?'<section class="goSection"><h3>ITEMS ACQUIRED</h3>'+loot+'</section>':'')
        +(rw.conquest?'<div class="goNotice good">β—ˆ '+rw.conquest.title+' β€” '+rw.conquest.unlock+'<br>FIRST CLEAR Β· +'+rw.conquest.xp+' XP Β· +'+rw.conquest.cores+' CORES</div>':'')
        +(rw.modeReward?'<div class="goNotice good">'+rw.modeReward.em+' '+rw.modeContract.nm+' REWARD β€” '+rw.modeReward.nm+' banked to Account Armory</div>':'')
        +(rw.rankUp?'<div class="goNotice good">β—ˆ PROMOTED β€” '+rw.rankUp.em+' '+rw.rankUp.nm.toUpperCase()+'</div>':'')
        +(eg&&eg.msgs.length?'<div class="goNotice">'+eg.msgs.join('<br>')+'</div>':'')
        +(dv&&dv.broke.length?'<div class="goNotice bad">βœ– BROKEN β€” '+dv.broke.map(b=>b.em+' '+b.nm).join(', ')+'</div>':'');
    } else $('goRewards').innerHTML=online?'<div class="goNotice">The relay has not verified this battlefield result. No XP, Cores, loot, or campaign progress was awarded.</div>':'';
    /* The previous result may have exposed CONTINUE. Hide and disarm it before

       the mode-specific painter runs, or a rapid defeat tap can activate the

       stale victory action for a few frames. */
    const cont=$('goContinueBtn');
    if(cont){cont.style.display='none';cont.disabled=true;cont.dataset.action='';}
    if(online){
      if(typeof mfDepart!=='undefined')mfDepart.fromVictory=false;
    }
    if(online){const chart=$('goChart');if(chart)chart.style.display='none';}
    else drawMatchChart();
    if(!online&&typeof adShowPostMatchAd==='function') adShowPostMatchAd(win);
    $('gameOver').style.display='flex';
    if(!online)sfx(win?'level':'alarm');
  },1400);
}
window.addEventListener('massfront-match:matchEnd',event=>{
  const C=window.MFMatchCommandConsumer;if(!C||typeof C.sessionActive!=='function'||!C.sessionActive()||gameEnded)return;
  const detail=event&&event.detail||{},seat=Number(detail.winnerSeat),winner=Number.isInteger(seat)&&seat>0?' Β· winning seat '+seat:'';
  endGame(false,'Relay ended the match: '+String(detail.reason||'unknown')+winner);
});
window.addEventListener('massfront-match:protocolError',event=>{
  const C=window.MFMatchCommandConsumer;if(!C||typeof C.sessionActive!=='function'||!C.sessionActive())return;
  const code=String(event&&event.detail&&event.detail.code||'unknown');
  /* A failed bootstrap can occur before newSkirmish clears the previous

     operation's gameEnded flag. It still needs a visible no-reward result. */
  if(gameEnded){if(!/^bootstrap_/.test(code))return;gameEnded=false;}
  endGame(false,'Match connection failed: '+code);
});

// ---------- main loop ----------
let acc=0, lastT=0, fpsN=0, fpsT=0, fpsShow=60;
const MF_SIM_DT=1/30,MF_SIM_MAX_STEPS=3,MF_SIM_DEBT_TICKS=8;
let mfSimDebtClamped=0,mfSimNetworkWaitFrames=0;
function mfMatchConsumer(){return typeof window!=='undefined'&&window.MFMatchCommandConsumer||null;}
/* Mobile panels commonly refresh at 90/120/144 Hz, but this renderer targets

   a stable 60 Hz presentation. Keep the authoritative 30 Hz simulation on

   every RAF callback while suppressing only duplicate presentation work. A

   short consecutive-sample gate avoids mistaking one early 60 Hz callback for

   a high-refresh display, and the deadline accumulator gives 90 Hz panels the

   correct alternating 1/2-callback cadence instead of collapsing to 45 Hz. */
const MF_PRESENT_MS=1000/60;
let mfRafSampleT=0,mfFastRafStreak=0,mfPresentationCapped=false;
let mfPresentNext=0,mfPresentDt=0,mfPresentationSkips=0,mfPresentationFrames=0;
function mfPresentationDue(ts){
  if(!mfRafSampleT){mfRafSampleT=ts;mfPresentNext=ts;mfPresentationFrames++;return true;}
  const gap=ts-mfRafSampleT;mfRafSampleT=ts;
  if(gap>=4&&gap<13.5)mfFastRafStreak++;
  else if(gap>=13.5)mfFastRafStreak=0;
  if(!mfPresentationCapped&&mfFastRafStreak>=8){
    mfPresentationCapped=true;mfPresentNext=ts;
  }
  if(!mfPresentationCapped){mfPresentationFrames++;return true;}
  if(ts+0.75<mfPresentNext){mfPresentationSkips++;return false;}
  const slots=Math.max(1,Math.floor((ts-mfPresentNext)/MF_PRESENT_MS)+1);
  mfPresentNext+=slots*MF_PRESENT_MS;mfPresentationFrames++;return true;
}
let aiAcc=0, fogAcc=0;
/* Tell the boot loader the patch is good. Called on the FIRST frame rather

   than at the end of setup: reaching a frame means the renderer, the sim and

   the UI all initialised, which is the only definition of "it boots" worth

   acting on. */
let bootConfirmed=false,bootIncompleteCount=-1;
function confirmBoot(){
  if(bootConfirmed) return;
  /* ALL-OR-NOTHING BOOT.

     __bootOk() clears probation, which is what permanently marks a patch as

     good. Today that is safe by accident: the OTA is ONE concatenated blob in

     ONE script tag, so a throw anywhere kills the whole payload, no frame ever

     lands, confirmBoot never runs, and boot.js rolls the device back.



     A per-file payload destroys that property. Separate script tags do not

     stop one another, so a throw in artifact 50 of 86 leaves the other 85

     running - the frame still lands, confirmBoot still fires, and a HALF-BOOTED

     build gets promoted to good and keeps its probation cleared. The device is

     then stuck on a broken version with the recovery state deleted.



     So the payload counts itself. Each artifact increments __MF_OTA_RAN as its

     last statement (only a file that ran to completion counts), and the first

     artifact stamps __MF_OTA_EXPECT with the count baked in at publish time.

     A short count means we withhold __bootOk: probation survives, and boot.js

     quarantines and rolls back after its second failed start, exactly as it

     does for a blob that threw.



     A packaged build sets neither global, so this is a no-op there and the

     behaviour is unchanged for anyone who has never taken an OTA. */
  const expect=window.__MF_OTA_EXPECT|0;
  if(expect>0 && (window.__MF_OTA_RAN|0)!==expect){
    /* main.js is not the final OTA artifact. On Android the first rAF can land

       while the tail scripts are still executing. Latching bootConfirmed here

       made that harmless timing window permanent: probation survived, and the

       next launch rolled the update back to the old APK. Leave the latch open

       so the frame loop retries; log only when the observed count changes. A

       genuinely throwing/missing artifact still never reaches `expect`, so the

       existing rollback safety remains intact. */
    const ran=window.__MF_OTA_RAN|0;
    if(bootIncompleteCount!==ran){
      bootIncompleteCount=ran;
      try{ console.warn('boot: OTA payload still loading - '+ran+' of '+expect+
                        ' artifacts ran; boot confirmation will retry'); }catch(e){}
    }
    return;
  }
  bootConfirmed=true;
  if(window.__MASSFRONT_RELEASE_INPUT_GUARD)
    window.__MASSFRONT_RELEASE_INPUT_GUARD();
  if(window.__bootOk) window.__bootOk();
}
/* ============================================================================

   ATTRACT MODE β€” the menu is a window onto the game, not a picture of it

   ----------------------------------------------------------------------------

   The start screen used to be a gradient. Everything needed to make it a live

   scene was already built and idle: terrain, models, lighting, the day cycle.

   So the menu now sits over a real battlefield with a real base on it, lit by

   the real sun, with the camera making a slow orbit β€” and the panels float in

   front of it in perspective rather than lying flat on top.



   It is deliberately small: a few dozen units and a handful of structures, a

   slow sim tick, no AI and no fog. A menu should never be the most expensive

   thing the game does. */
let attractOn=false, attractT=0, attractCX=0, attractCY=0;
/* Lower bound the frame-rate scaler may not go below. Zero on every preset but

   CINEMATIC, which exists precisely to stop the automatic backing-off. */
let perfFloor=0;
/* A snapshot waiting to be laid down once the world has regenerated. */
let sessPending=null;
/* THE PLAYER'S OWN FACTION.

   src/audio.js has been reading `playerFaction` since the radio system landed,

   but nothing ever declared it β€” so it was permanently undefined and the player

   always spoke with Nova's voice no matter what they were fielding. The setup

   screen let you pick your OPPONENT's faction and not your own, which is an odd

   thing for a game whose four armies have genuinely different chassis kits.

   'nova' is the Terran line and stays the default. */
let playerFaction='nova';
let playerCommanderId='nova_kai';
function menuBg(){ return (META&&META.settings&&META.settings.menubg)||'dim'; }
/* Off keeps the menu chrome β€” the HUD must still be hidden β€” but skips building

   and ticking the diorama entirely. menuMode is what suppresses the in-match

   HUD, so it stays on either way; only attractOn changes. */
function applyMenuBackdrop(){
  const ss=document.getElementById('startScreen');
  const training=typeof trainingMissionActive==='function'&&trainingMissionActive();
  /* Training owns the carrier/loading transition before `running` flips true.

     Treating that short window as a menu let applySettings() restart the

     attract scene and re-add menuMode over the live tutorial HUD. */
  if(training){ document.body.classList.remove('menuMode'); return; }
  const onMenu=!running && ss && ss.style.display!=='none';
  if(!onMenu) return;
  document.body.classList.add('menuMode');
  if(menuBg()==='off'){ attractOn=false; return; }
  if(!attractOn) setupAttract();
}
function setupAttract(){
  /* Cold launch now passes through the visual updater before the main menu.

     Building the terrain diorama behind a download wastes memory, heat and GPU

     time, so the launcher keeps this function dormant until PLAY hands off. */
  if(typeof mfLauncherShouldDeferAttract==='function'&&mfLauncherShouldDeferAttract()){
    attractOn=false;document.body.classList.add('menuMode');return;
  }
  if(typeof trainingMissionActive==='function'&&trainingMissionActive()){
    stopAttract();
    return;
  }
  if(!terrVerts) return;
  if(menuBg()==='off'){ document.body.classList.add('menuMode'); attractOn=false; return; }
  resetWorld();
  /* A completed match leaves albedo scars and Brood relief behind. Clear the

     old entities first, then rebuild only dirty terrain before the diorama

     paints its own foundations; clearing creepF alone leaves purple albedo. */
  if(mmDirty) applyTheme();
  if(typeof materialV2SetupAttract==='function'&&materialV2SetupAttract())return;
  demoMode=false; matchLive=false; fogOn=false;
  /* Somewhere scenic and reachable: the map's own player start, which is

     guaranteed to be buildable ground. */
  const bx=MAP*SP_LO, by=MAP*SP_HI;
  attractCX=bx; attractCY=by;
  const g=v=>Math.round(v/SNAP_GRID)*SNAP_GRID;
  addBld('hq',0,g(bx),g(by),true,0);
  for(const [ty,dx,dy] of [['fac',150,-90],['pgen',-140,-70],['pgen',-140,30],
                           ['turret',60,170],['techlab',150,80],['silo',-140,130],
                           ['aatower',-40,-190],['uplink',180,0]]){
    const x=g(bx+dx), y=g(by+dy);
    if(!footBlocked(ty,x,y,0)&&footOnLand(ty,x,y,0)) addBld(ty,0,x,y,true,0);
  }
  /* A patrol worth watching: a mixed column walking a slow circuit past the

     base, so there is movement, dust and walk cycles rather than a still life. */
  for(let k=0;k<34;k++){
    const ty=[0,1,2,9,11,19,22,20][k%8];
    const a=k/34*TAU, d=190+Math.random()*160;
    const i=spawnUnit(ty,0,bx+Math.cos(a)*d,by+Math.sin(a)*d);
    if(i>=0){
      ustate[i]=5;                                   // patrol
      upx1[i]=ux[i]; upy1[i]=uy[i];
      upx2[i]=bx+Math.cos(a+2.2)*d; upy2[i]=by+Math.sin(a+2.2)*d;
      utx[i]=upx2[i]; uty[i]=upy2[i];
    }
  }
  orthoSpan=distTarget=880; camPitch=pitchTarget=1.16;
  cam.x=bx; cam.y=by; camYaw=yawTarget=0.4;
  clampCam(); camUpdateMatrices();
  attractOn=true; attractT=0;
  /* The in-match HUD has no business showing through the menu. */
  document.body.classList.add('menuMode');
}
function attractTick(dt){
  if(typeof materialV2TickAttract==='function'&&materialV2TickAttract(dt))return;
  attractT+=dt;
  /* A slow orbit with a gentle breathing zoom. Slow enough to read as

     cinematic; never so slow that it looks frozen on a first glance.

     Dimmed halves every rate again: the eye tracks change, not brightness, so

     slowing the motion does more for legibility than darkening alone. */
  const m = menuBg()==='dim' ? 0.45 : 1;
  camYaw=yawTarget=0.4+attractT*0.055*m;
  orthoSpan=distTarget=880+Math.sin(attractT*0.17*m)*150;
  camPitch=pitchTarget=1.18+Math.sin(attractT*0.11*m)*0.06;
  cam.x=attractCX+Math.cos(attractT*0.09*m)*40;
  cam.y=attractCY+Math.sin(attractT*0.07*m)*40;
  clampCam(); camUpdateMatrices();
  /* One slow sim step: the patrol walks, guns idle, dust drifts. */
  const sd=1/24;
  unitTick(sd); projTick(sd); bldTick(sd); beamTick(sd); envTick(sd);
  updParticles(sd); shardTick(sd);
  /* Parallax: the UI leans against the orbit, so the panels feel like they are

     standing in the scene rather than pasted over it. */
  const ss=document.getElementById('startScreen');
  if(ss&&ss.style.display!=='none'){
    const lx=Math.sin(attractT*0.055)*5.0*m, ly=Math.cos(attractT*0.041)*3.0*m;
    ss.style.setProperty('--px',lx.toFixed(2)+'deg');
    ss.style.setProperty('--py',ly.toFixed(2)+'deg');
  }
}
function stopAttract(){
  attractOn=false; document.body.classList.remove('menuMode');
  /* #gl is display:none under menuMode.bgOff. Remeasure after it is shown

     so a landscape desktop window is not stuck with the hidden-canvas size. */
  if(typeof resize==='function') resize();
}
/* A FROZEN MATCH MUST SAY SO.



   frame() schedules its own next frame on its FIRST line, then runs the whole

   simulation step unguarded. One exception anywhere in that step - a bad unit,

   a null structure, a malformed order - and the rest of the frame never runs,

   while requestAnimationFrame keeps firing forever. The result is the worst

   shape a failure can take: the canvas holds its last good frame, HUD CSS

   animations and audio keep playing so the game looks alive, pause does nothing

   because `paused` is only read inside the body that is throwing, and queued

   abilities are never consumed. The player reports "it froze" and there is

   nothing in the log, because the same error is thrown and discarded sixty

   times a second with no one listening.



   This does not swallow the error and carry on - continuing a deterministic

   simulation past an exception is how you turn a crash into a desync. It stops

   the match deliberately, once, and names the cause: console, a visible toast,

   and window.mfSimFailure so a probe or a bug report can read it back. A halted

   match that explains itself can be diagnosed; a silent freeze cannot. */
function mfReportSimFailure(error,phase){
  if(mfSimFailure) return;
  const detail={
    phase:phase||'sim',
    message:String(error&&error.message||error),
    stack:String(error&&error.stack||''),
    tick:typeof tick==='number'?tick:null,
    clock:typeof matchClock==='number'?matchClock:null,
    when:new Date().toISOString()
  };
  mfSimFailure=detail;
  try{window.mfSimFailure=detail;}catch(e){}
  try{console.error('MASSFRONT '+detail.phase+' failure at tick '+detail.tick,error);}catch(e){}
  /* Halt rather than repeat. The next frame would throw in the same place. */
  try{paused=true;}catch(e){}
  try{if(typeof toast==='function')toast('Simulation halted: '+detail.message.slice(0,80));}catch(e){}
}

function frame(ts){
  requestAnimationFrame(frame);
  if(typeof mfPerfFrameBegin==='function') mfPerfFrameBegin();
  if(!bootConfirmed) confirmBoot();
  if(!lastT) lastT=ts;
  let dt=(ts-lastT)/1000; lastT=ts;
  if(dt>0.25) dt=0.25;
  mfPresentDt=Math.min(0.25,mfPresentDt+dt);
  const presentDue=mfPresentationDue(ts);
  const presentDt=presentDue?mfPresentDt:0;
  if(presentDue)mfPresentDt=0;
  // fps
  if(presentDue)fpsN++;
  fpsT+=dt;
  if(fpsT>=0.6){
    fpsShow=Math.round(fpsN/fpsT); fpsN=0; fpsT=0;
    const total=teamCount[0]+teamCount[1]+teamCount[2];
    /* HYSTERESIS. Dozens of effects gate or scale on this value; a bare

       threshold on a noisy fps signal flipped it 0.55<->1.0 twice a second at

       36-44 fps and strobed all of them at once β€” glows, beams, dust, water

       sparkle. Drop instantly to protect the frame, rise only with headroom.

       (This block also used to EASE the visible value "so changes arrive as

       fades, not steps". That reintroduced the very strobe it sits above β€” see

       the note on the assignment below β€” and has been removed.) */
    const band=fpsShow<28?0.25:fpsShow<42?0.55:1;
    if(band<perfBand) perfBand=band;
    else if(band>perfBand&&fpsShow>(perfBand<0.5?34:48)) perfBand=band;
    /* QUANTISED, NOT EASED β€” this is the fix for the half-second FX strobe.



       About seventy effects gate on perfScale, and their thresholds sit in one

       tight cluster between 0.30 and 0.56: water sparkle, shadow decals, tread

       dust, debris, rubble, repair beams, muzzle flashes, and ambient occlusion

       at 0.50. Easing the value dragged it through EVERY threshold in that

       cluster on a single band change, so all of those effects switched off and

       came back over about half a second. Measured on a real GPU: one 1.00 ->

       0.25 ramp crossed all six sampled gates (tools/verify-perfscale-gates.mjs).



       perfBand above is ALREADY the anti-strobe mechanism β€” drop instantly,

       rise only with headroom. Reading it directly gives one deliberate step

       per genuine performance change. The ease this replaces existed so

       particle COUNTS would fade rather than step, but a count going from 5 to

       3 is invisible while a shadow pass vanishing is not. */
    perfScale=perfBand;
    /* These were 7000 and 18000 β€” thresholds inherited from the 10,000-unit

       Mega demo, which no longer exists. A real match caps at FACTION_POP_CAP

       (500) per commander seat, so four participants top out at 2000 real

       units; a Hard Brood system force may add 500 more. Both old thresholds

       sat above even that maximum and could never fire. Re-scaled so a heavy

       fight is protected before the frame is already gone. */
    if(total>1300) perfScale=Math.min(perfScale,0.55);
    if(total>1700) perfScale=Math.min(perfScale,0.3);
    if(META.settings.perf==='low') perfScale=Math.min(perfScale,0.45);
    /* CINEMATIC sets a floor under the automatic scaler, so a heavy fight

       cannot quietly strip the frame back to the low preset β€” which is the

       whole reason someone picks it. */
    if(typeof perfFloor!=='undefined'&&perfFloor>0) perfScale=Math.max(perfScale,perfFloor);
    /* Density is a SETTING, not an observation, so it is applied on the way out

       of a value recomputed from perfBand each sample β€” never folded back into

       an accumulator. It used to compound: `perfScale *= GFX.particles` ran

       after the ease, so its own output was re-eased and re-multiplied every

       sample. Measured on a real GPU it settled at 3.63-3.86 on CINEMATIC

       (x1.5) against an intended ceiling of 1.0 β€” roughly 4x the particles it

       was asked for β€” and at 0.31 on LOW (x0.5) at a locked 60fps, which

       silently disabled every effect gated above 0.32. */
    if(typeof GFX!=='undefined'&&GFX.particles) perfScale*=GFX.particles;
  }
  /* A halt is STICKY. Reporting is one-shot, so without this gate the pause

     menu's RESUME clears `paused`, the sim re-enters the same throwing code,

     and mfReportSimFailure returns early with nothing on screen β€” the player

     is back in the original silent freeze, having been told once that it was

     explained. resetWorld() clears the latch, so a new match still starts. */
  if(running&&!paused&&!gameEnded&&!mfSimFailure){
    if(typeof mfPerfBegin==='function') mfPerfBegin('sim');
    const simDt=MF_SIM_DT;
    try{
    acc+=dt*gameSpeed;
    const debtCap=simDt*MF_SIM_DEBT_TICKS;
    if(acc>debtCap){mfSimDebtClamped+=Math.floor((acc-debtCap)/simDt);acc=debtCap;}
    let steps=0;
    while(acc>=simDt&&steps<MF_SIM_MAX_STEPS){
      const consumer=mfMatchConsumer(),nextTick=typeof mfDetTick==='number'?mfDetTick+1:tick+1;
      if(consumer&&typeof consumer.requiresLockstep==='function'&&consumer.requiresLockstep()&&
         typeof consumer.canAdvance==='function'&&!consumer.canAdvance(nextTick)){
        mfSimNetworkWaitFrames++;break;
      }
      if(consumer&&typeof consumer.beginTick==='function'&&consumer.beginTick(nextTick)===false){
        if(typeof consumer.commitTick==='function')consumer.commitTick(nextTick);break;
      }
      acc-=simDt; steps++;
      if(typeof mfPerfCount==='function')mfPerfCount('authoritySteps');
      if(typeof mfDeterminismBeginStep==='function')mfDeterminismBeginStep(simDt,nextTick);
      carrierTick(simDt);
      camAuthTick(simDt);
      /* God Mode is deliberately obvious and deterministic: the gold badge

         stays visible while the solo-test economy refills every sim step. */
      if(!demoMode&&matchLive&&META.settings.godMode&&!window.__MF_NETWORK_SETUP__){
        econFillBanks(0);
        for(let k=0;k<abCool.length;k++) abCool[k]=0;
      }
      /* Only reel the camera in when the ship has genuinely left the view, and

         only while the player is not driving. 210 units is well inside a single

         screen, so the old threshold fired during ordinary panning. */
      if(carrier.active&&carrier.phase<2&&Math.hypot(carrier.x-cam.x,carrier.y-cam.y)>orthoSpan*0.55)
        carrierFollow();
      if(typeof mfPerfBegin==='function')mfPerfBegin('simUnits');
      unitTick(simDt);
      if(typeof mfPerfEnd==='function')mfPerfEnd('simUnits');
      if(typeof mfPerfBegin==='function')mfPerfBegin('simProjectiles');
      projTick(simDt);
      if(typeof mfPerfEnd==='function')mfPerfEnd('simProjectiles');
      if(typeof mfPerfBegin==='function')mfPerfBegin('simStructures');
      bldTick(simDt);
      fortTick(simDt);
      buildZoneTick(simDt);
      if(typeof mfPerfEnd==='function')mfPerfEnd('simStructures');
      if(typeof mfPerfBegin==='function')mfPerfBegin('simEconomy');
      reclaimTick(simDt);
      econTick(simDt);
      if(typeof mfPerfEnd==='function')mfPerfEnd('simEconomy');
      if(typeof mfPerfBegin==='function')mfPerfBegin('simCombatEffects');
      abilTick(simDt);
      beamTick(simDt);
      if(typeof mfPerfEnd==='function')mfPerfEnd('simCombatEffects');
      if(typeof mfPerfBegin==='function')mfPerfBegin('simWorld');
      envTick(simDt);
      crateTick(simDt);
      sceneryTick(simDt);
      shardTick(simDt);
      if(typeof mfPerfEnd==='function')mfPerfEnd('simWorld');
      if(typeof mfPerfBegin==='function')mfPerfBegin('simParticles');
      updParticles(simDt);
      if(typeof mfPerfEnd==='function')mfPerfEnd('simParticles');
      if(typeof mfPerfBegin==='function')mfPerfBegin('simDeformation');
      processDeforms();
      if(typeof waterFloodTick==='function') waterFloodTick(simDt);
      deformMaintain(simDt);
      if(typeof mfPerfEnd==='function')mfPerfEnd('simDeformation');
      if(!demoMode&&!matchLive){         // keep vision live while flying the carrier in
        fogAcc+=simDt;
        if(fogAcc>=0.4){
          if(typeof mfPerfBegin==='function')mfPerfBegin('fog');
          updateFog();fogAcc=0;
          if(typeof mfPerfEnd==='function')mfPerfEnd('fog');
        }
      }
      if(matchLive&&!demoMode&&timeLimit>0&&matchClock>0) matchClock=Math.max(0,matchClock-simDt);
      if(!demoMode&&matchLive){          // the war only runs once you've planted your base
        aiAcc+=simDt;
        if(aiAcc>=0.5){
          if(typeof mfPerfBegin==='function')mfPerfBegin('ai');
          aiTick(aiAcc);aiAcc=0;
          if(typeof mfPerfEnd==='function')mfPerfEnd('ai');
        }
        fogAcc+=simDt;
        /* Fog also records explored terrain and discovered entities. Preserve

           its authority cadence; profile the pulse before splitting it. */
        if(fogAcc>=0.5){
          if(typeof mfPerfBegin==='function')mfPerfBegin('fog');
          updateFog();fogAcc=0;
          if(typeof mfPerfEnd==='function')mfPerfEnd('fog');
        }
        if(stats.t-tlLast>=5){tlLast=stats.t;tlRecord();}
      }
      if(consumer&&typeof consumer.commitTick==='function')consumer.commitTick(nextTick);
      if(typeof mfDeterminismEndStep==='function')mfDeterminismEndStep();
      /* Resolve the same terminal tick on fast and stalled clients. Waiting

         until a whole render catch-up batch finishes allowed extra combat and

         rewards during the result screen's 1.4-second presentation delay. */
      checkVictory();
      if(gameEnded){acc=0;break;}
    }
    }catch(error){ mfReportSimFailure(error,'sim'); }
    if(typeof mfPerfEnd==='function') mfPerfEnd('sim');
  }
  if(!running && attractOn && attractVisible){
    attractTick(dt);
    if(presentDue){
      processDeforms();
      render(presentDt);
    }
    musicTickFrame(dt);
    if(typeof mfPerfFrameEnd==='function') mfPerfFrameEnd();
    return;
  }
  camTick(dt);
  if(running){
    if(!paused&&perfScale>0.4) weatherTick(dt);
    if(presentDue){
      render(presentDt);
      floatTextTick(presentDt);
      if(typeof mfPerfBegin==='function') mfPerfBegin('minimap');
      renderMinimap();
      if(typeof mfPerfEnd==='function') mfPerfEnd('minimap');
      if(typeof mfPerfBegin==='function') mfPerfBegin('hud');
      updateHUD(fpsShow);
      if(typeof mfPerfEnd==='function') mfPerfEnd('hud');
    }
  }
  musicTickFrame(dt);
  if(typeof mfPerfFrameEnd==='function') mfPerfFrameEnd();
}

/* Mid-tier CPU at 500 pop per commander seat. GPU fillrate is the quality preset; this is

   sim/HUD/pathfinding. Takeover, not edits to the renderer files. */
function mfCpuBind(){
  /* Pathfinding is simulation authority. Replacing computeField/requestField

     here made runtime behavior depend on boot order and silently discarded

     blocker, clearance and replay fixes made in src/game/sim.js. Keep CPU

     tuning on the authoritative implementation instead of shadowing it. */
  mfCpuBindMinimap();
  mfCpuBindIcons();
  mfCpuBindFx();
}
/* mfGfxKey() lives in src/engine/gl.js (manifest index 3). This file is index

   79, so a duplicate here silently overwrote that one β€” editing the documented

   definition in gl.js would have had no effect. Do not re-add it.

   tools/verify-global-scope.mjs fails the build on this class of shadowing. */
function mfCpuBindSep(){
  if(typeof unitSeparation!=='function') return;
  const orig=unitSeparation;
  unitSeparation=function(i,T,isBug,swarmLOD,total){
    /* HIGH keeps the original 800/every-other skip. MEDIUM starts earlier and

       strides harder so 400–800 pop does not pay HIGH-class pair tests. */
    const q=mfGfxKey();
    const gate=q==='low'?280:q==='medium'?420:800;
    const stride=q==='low'?3:q==='medium'?(total>900?3:2):2;
    if(total>gate && ((i+tick)%stride) && !usel[i] && T.cat!=='hero'){
      sepVX=0; sepVY=0; sepHits=0; sepVisited=0;
      return;
    }
    return orig(i,T,isBug,swarmLOD,total);
  };
}
function mfCpuBindMinimap(){
  if(typeof renderMinimap!=='function') return;
  const orig=renderMinimap;
  let keep=null, keepT=0, skip=0, keepGen=-1;
  renderMinimap=function(){
    const tot=(teamCount[0]+teamCount[1]+teamCount[2])|0;
    const now=performance.now();
    const q=mfGfxKey();
    /* Deform stamps null mmBg on every crater. The 2048β†’256 civic downsample

       (drawImage of terrainCanvas) is the #2 CPU hotspot at 500 pop per seat.

       HIGH/CINEMATIC refresh scars sooner; MEDIUM holds the last bake longer. */
    const hold=q==='low'?1600:q==='medium'?1100:q==='cinematic'?320:520;
    /* Hold only a bake from this map gen. Restoring a black/stale keep

       after applyTheme nulls mmBg is why MEDIUM phones kept a black square. */
    if(keep && keepGen===mmBgGen && now-keepT<hold) mmBg=keep;
    const skipN=q==='low'?3:q==='medium'?(tot>280?2:1):(tot>700?2:1);
    if(keep && keepGen===mmBgGen && skipN>1 && (++skip%skipN)) return;
    orig();
    if(typeof mmBg!=='undefined'&&mmBg){ keep=mmBg; keepT=now; keepGen=mmBgGen; }
    else { keep=null; keepGen=-1; }
  };
}
function mfCpuBindIcons(){
  /* Screen-footprint handover already lives in tacticons.js (24β†’15 px).

     MEDIUM/LOW convert a few pixels earlier so infantry drop meshes sooner.

     HIGH/CINEMATIC keep the authored ramp β€” do not make the flagship cheaper. */
  if(typeof mfIconQ!=='function') return;
  const orig=mfIconQ;
  mfIconQ=function(worldSpan){
    const q=mfGfxKey();
    if(q==='high'||q==='cinematic') return orig(worldSpan);
    const px=worldSpan/(typeof mfWorldPx==='function'?mfWorldPx():Math.max(.24,orthoSpan/Math.max(1,VH)));
    if(q==='medium') return clamp((28-px)/(28-17),0,1);
    return clamp((34-px)/(34-20),0,1);
  };
}
function mfCpuBindFx(){
  /* GPUFX already scales burst counts via GFX.particles. This only thins the

     CPU sprite ring (MAXPART=9000) for atmosphere/dust β€” combat flashes stay. */
  if(typeof addParticle!=='function') return;
  const orig=addParticle;
  let dustN=0;
  addParticle=function(type,x,y,vx,vy,life,size,r,g,b){
    const q=mfGfxKey();
    if(q==='medium'||q==='low'){
      /* Thin atmosphere, do not delete it β€” LOW keeps a quarter, MEDIUM half. */
      if(type===9 && ((++dustN)&(q==='low'?3:1))) return;
      if(type===10 && (dustN&(q==='low'?3:1))) return;
      if(type===1 && q==='medium' && size<9 && (dustN&1)) return;
    }
    return orig(type,x,y,vx,vy,life,size,r,g,b);
  };
}

// ---------- UI wiring ----------
let builtTheme='verdant', builtMap='aelos_north_medium', builtTopology='';
function applyTheme(){
  /* setupDeposits and spawn normalization happen before buildTerrain reaches

     its exact-plan gate. Keep the previous live world by identity so a rejected

     destination cannot leave fresh economy or selector state attached to the

     prior terrain texture. buildTerrain owns the matching CPU-terrain rollback. */
  const own=k=>Object.prototype.hasOwnProperty.call(window,k);
  const prop=k=>({had:own(k),value:window[k]});
  const before={
    aiRefs:aiSlots.slice(),aiRows:aiSlots.map(A=>({...A})),playerStartZone,spawnPick,
    deposits:deposits.slice(),geysers:geysers.slice(),seed:_seed,
    civicKitSeq:typeof civicKitSeq==='number'?civicKitSeq:null,
    depPts:prop('__depPts'),relocation:prop('__mfResourceRelocation'),
    cityAt:prop('__cityAt'),reclaimTip:prop('__reclaimTip')
  };
  const restoreProp=(key,S)=>{if(S.had)window[key]=S.value;else delete window[key];};
  const restoreSetup=()=>{
    aiSlots.length=0;for(const A of before.aiRefs)aiSlots.push(A);
    for(let i=0;i<aiSlots.length;i++){
      for(const k in aiSlots[i])delete aiSlots[i][k];
      Object.assign(aiSlots[i],before.aiRows[i]);
    }
    playerStartZone=before.playerStartZone;spawnPick=before.spawnPick;
    deposits.length=0;for(const D of before.deposits)deposits.push(D);
    geysers.length=0;for(const G of before.geysers)geysers.push(G);
    _seed=before.seed;if(before.civicKitSeq!=null)civicKitSeq=before.civicKitSeq;
    restoreProp('__depPts',before.depPts);restoreProp('__mfResourceRelocation',before.relocation);
    restoreProp('__cityAt',before.cityAt);restoreProp('__reclaimTip',before.reclaimTip);
  };
  let locationPreview=null,topology='',cacheHit=false;
  try{
    /* Generation rejects sites against spawn seats, so normalize those seats at

       the generation boundary rather than later inside newSkirmish. Preflight

       follows normalization so its topology is the topology that is built. */
    if(typeof normalizeAiSlotsForBattlefield==='function')normalizeAiSlotsForBattlefield();
    locationPreview=typeof mfPreflightLocationPlanV1==='function'?mfPreflightLocationPlanV1(curMap):null;
    if(locationPreview&&(!locationPreview.ok||locationPreview.status==='HYBRID_V1')){
      const code=!locationPreview.ok?(locationPreview.error&&locationPreview.error.code||'LOCATION_PREFLIGHT_FAILED'):
        'LOCATION_HYBRID_UNSUPPORTED';
      const e=new Error(code);e.code=code;e.locationPlan=locationPreview;throw e;
    }
    topology=typeof mfWorldTopologyKey==='function'?mfWorldTopologyKey():'';
    window.__reclaimTip=0;
  /* Theme/map equality is not proof that terrain exists. On a cold boot those

     defaults already match before the attract scene has produced heightF;

     launching Training or Standard immediately then called setupDoodads()

     against null terrain. The cache is reusable only when every CPU/GPU side

     needed by simulation is present. */
    cacheHit=!!(heightF&&PASS&&terrainTex&&curTheme===builtTheme&&curMap===builtMap&&topology===builtTopology&&!mmDirty);
    if(!cacheHit){
      setupDeposits();                             // node layout follows the map seed
      terrainTex=buildTerrain(curTheme,locationPreview);
    }
  }catch(error){restoreSetup();throw error;}
  /* Swap the ground albedo variant only after the destination passed its exact

     plan gate. The async decoder may finish later; a rejected map never starts

     replacing the prior world's detail pair. */
  if(typeof reloadTerrainThemeTextures==='function') reloadTerrainThemeTextures();
  if(cacheHit)return;                              // rebuild also when scarred by battle
  /* Settlements are part of the world: generated right after the ground exists

     so a rebuilt map (context loss, theme change) grows the same places back. */
  if(typeof worldSitesGenerate==='function'){
    try{ const ns=worldSitesGenerate(); if(ns) console.log('world sites:',ns); }
    catch(e){ console.warn('worldsites:',e&&e.message); }
  }
  builtTheme=curTheme; builtMap=curMap; builtTopology=topology;
  mmBg=null; mmDirty=false; mipDirty=false; mipUrgent=false;
  if(typeof mmBgGen==='number') mmBgGen++;
}
let pitchIdx=1;
let gameSpeed=1;
let perfBand=1;                 // hysteretic quality band behind perfScale
/* ---------- match timeline (post-game graph) ---------- */
const tl=[]; let tlLast=0;
function tlRecord(){
  let av0=0, av1=0;
  for(let i=0;i<unitHigh;i++){
    if(!ualive[i]) continue;
    const c=TYPES[utype[i]].cm+TYPES[utype[i]].ce*0.1 || 60;
    if(uteam[i]===0) av0+=c; else if(uteam[i]===1) av1+=c;
  }
  tl.push({t:stats.t, a0:av0, a1:av1, k0:stats.kills[0], k1:stats.kills[1]});
  if(tl.length>400) tl.shift();
}
function drawMatchChart(){
  const cv3=$('goChart'); if(!cv3||tl.length<3){ if(cv3) cv3.style.display='none'; return; }
  cv3.style.display='block';
  const c=cv3.getContext('2d'), W=cv3.width, H=cv3.height;
  c.clearRect(0,0,W,H);
  c.fillStyle='rgba(10,18,30,.85)'; c.fillRect(0,0,W,H);
  let mx=10; for(const p of tl) mx=Math.max(mx,p.a0,p.a1);
  const X=i2=>8+(W-16)*i2/(tl.length-1), Y=v=>H-10-(H-24)*v/mx;
  c.lineWidth=1; c.strokeStyle='rgba(120,160,190,.25)';
  for(let g2=1;g2<4;g2++){ c.beginPath(); c.moveTo(8,Y(mx*g2/4)); c.lineTo(W-8,Y(mx*g2/4)); c.stroke(); }
  const line=(key,col)=>{
    c.strokeStyle=col; c.lineWidth=2; c.beginPath();
    tl.forEach((p,i2)=>{ i2? c.lineTo(X(i2),Y(p[key])) : c.moveTo(X(0),Y(p[key])); });
    c.stroke();
  };
  line('a1','rgba(255,110,84,.95)');
  line('a0','rgba(96,200,255,.95)');
  c.font='700 9px Rajdhani,sans-serif';
  c.fillStyle='#8fd8ff'; c.fillText('YOUR ARMY',10,12);
  c.fillStyle='#ffab98'; c.fillText('ENEMY',70,12);
  c.fillStyle='#7fa8c4'; c.fillText('ARMY VALUE OVER TIME',W-118,12);
}
/* An OTA patch replaces the SCRIPTS, not the shell: a device patched over an

   older APK still has the old index.html (no #planetRow) and the old ui.css.

   So the planet section is created on demand and its styles are injected here

   too β€” the same rules that ui.css ships for fresh builds. */
function mfPlanetRowStyles(){
  if(document.getElementById('mfPlanetRowCss')) return;
  const st=document.createElement('style');
  st.id='mfPlanetRowCss';
  st.textContent='#planetRow{display:flex;flex-direction:column;width:100%;box-sizing:border-box;gap:8px}'
    +'.warTableCard{overflow:hidden}.regionMissionDeck{margin-top:10px;padding-top:10px;border-top:1px solid rgba(120,205,255,.2)}'
    +'.regionMissionHead{display:flex;justify-content:space-between;gap:8px;align-items:end;margin-bottom:8px}'
    +'.regionMissionHead span{font:900 11px/1 var(--fT);letter-spacing:.14em;color:#eaf8ff}'
    +'.regionMissionHead small{font:800 8px/1 var(--fT);letter-spacing:.1em;color:#6e9bb7}';
  (document.head||document.documentElement).appendChild(st);
}
function ensurePlanetRow(){
  mfPlanetRowStyles();
  let row=$('planetRow');
  if(!row){
    const host=document.querySelector('#setupScr [data-setup-tab="map"]');
    if(!host||!host.parentNode) return null;
    const sec=document.createElement('section');
    sec.className='setupCard warTableCard';
    sec.setAttribute('data-setup-tab','map');
    sec.innerHTML='<div class="secLbl">PLANETARY WAR TABLE</div><div class="optRow" id="planetRow"></div>';
    host.parentNode.insertBefore(sec,host); row=$('planetRow');
  }
  /* OTA patches run inside old APK shells. Move their separate map row into

     the War Table once, and retire the independent size card: each site now

     carries its Small/Medium/Large theatre contract. */
  const card=row.closest('.setupCard'),maps=$('mapRow');
  if(card&&maps&&!card.contains(maps)){
    const old=maps.closest('.setupCard'),deck=document.createElement('div');
    deck.className='regionMissionDeck';
    deck.innerHTML='<div class="regionMissionHead"><span id="regionMissionName">REGION</span><small>SELECT A BATTLEFIELD SITE</small></div>';
    deck.appendChild(maps); card.appendChild(deck);
    if(old&&old!==card) old.style.display='none';
  }
  const scale=$('battleScaleRow');
  if(scale){scale.hidden=true;const sc=scale.closest('.setupCard');if(sc&&sc!==card)sc.style.display='none';}
  const scaleHint=$('battleScaleHint');if(scaleHint){scaleHint.hidden=true;scaleHint.style.display='none';}
  return row;
}
let planetYaw = 0.5, planetPitch = 0.2, curRegionId = 'aelos_north';
let isDraggingPlanet = false, lastDragX = 0, lastDragY = 0, planetDragDx = 0, planetDragDy = 0;

function selectPlanetKey(key){
  const P=typeof PLANETS!=='undefined'&&PLANETS[key];if(!P)return false;
  if(typeof mfConquestPlanetOpen==='function'&&!mfConquestPlanetOpen(key)){
    if(typeof toast==='function') toast('πŸ”’ CONQUER THE PREVIOUS PLANET TO OPEN '+P.nm);
    if(typeof sfx==='function') sfx('deny');
    return false;
  }
  curTheme=P.theme;curRegionId=P.regions[0].id;
  const maps=P.regions[0].maps||[];if(maps.length)syncBattlefieldFromMap(theatreMapId(maps,'standard'));
  if(typeof sfx==='function')sfx('ui');
  renderPlanetRow();renderMapRow();renderSpawnPlanner();
  return true;
}

function renderPlanetRow(){
  const row=ensurePlanetRow();
  if(!row||typeof PLANETS==='undefined') return;
  const activeKey=typeof planetForMap==='function'?planetForMap(curMap):planetForTheme(curTheme);
  const P=PLANETS[activeKey]||PLANETS.aelos;

  const planetRail=Object.keys(PLANETS).map(k=>{
    const Q=PLANETS[k],col=(Q.regions&&Q.regions[0]&&Q.regions[0].color)||'#65d8ff';
    return '<button type="button" class="orbitWorld'+(k===activeKey?' on':'')+'" data-planet="'+k+'" aria-pressed="'+(k===activeKey)+'" style="--world:'+col+'">'
      +'<i class="orbitWorldOrb"></i><span><b>'+Q.nm+'</b><small>4 REGIONS</small></span></button>';
  }).join('');
  row.innerHTML = '<nav class="worldOrbitRail" aria-label="Select planet">'+planetRail+'</nav>'
    + '<div class="planetHeaderWrap" style="text-align:center;margin-bottom:8px">'
    + '<div style="font-family:var(--fT);font-size:16px;letter-spacing:0.25em;color:#eaf7ff;font-weight:900;text-transform:uppercase">' + P.nm + '</div>'
    + '<div style="margin-top:3px;font:800 8px/1 var(--fT);letter-spacing:.16em;color:#65d8ff">'+String(activeWarMode||'standard').toUpperCase()+' THEATRE</div>'
    + '<div style="display:flex;justify-content:space-around;margin-top:6px;padding:6px 2px;background:rgba(5,15,25,0.65);border-radius:6px;border:1px solid rgba(140,230,255,0.2);font-size:9px;color:#8abfdc">'
    + '<div><span style="display:block;font-size:8px;color:#5a84a0;letter-spacing:0.05em">SECTOR</span><b style="color:#ffffff;font-size:9px">' + (P.sector||'Helios Core') + '</b></div>'
    + '<div><span style="display:block;font-size:8px;color:#5a84a0;letter-spacing:0.05em">DIAMETER</span><b style="color:#ffffff;font-size:9px">' + (P.diameter||'56,780 km') + '</b></div>'
    + '<div><span style="display:block;font-size:8px;color:#5a84a0;letter-spacing:0.05em">DAY LENGTH</span><b style="color:#ffffff;font-size:9px">' + (P.dayLen||'12 hrs') + '</b></div>'
    + '<div><span style="display:block;font-size:8px;color:#5a84a0;letter-spacing:0.05em">AVG TEMP</span><b style="color:#ffffff;font-size:9px">' + (P.temp||'25Β°C') + '</b></div>'
    + '<div><span style="display:block;font-size:8px;color:#5a84a0;letter-spacing:0.05em">CLIMATE</span><b style="color:#ffffff;font-size:9px">' + (P.climate||'Tropical') + '</b></div>'
    + '</div></div>'
    + '<div style="position:relative;width:100%;height:230px;margin:0 auto;background:rgba(3,10,18,0.85);border-radius:8px;border:1px solid rgba(140,230,255,0.25);overflow:hidden;box-sizing:border-box">'
    + '<canvas id="planetSphereCanvas" width="440" height="230" style="width:100%;height:100%;object-fit:contain;cursor:grab;touch-action:none"></canvas>'
    + '<div style="position:absolute;bottom:6px;left:10px;right:10px;display:flex;justify-content:space-between;pointer-events:none;font-size:10px;color:#a0d4f5;font-weight:700">'
    + '<span>⬑ REVERSE BIODOME: '+ (P.biodome||'ACTIVE') +'</span>'
    + '<span>TAP REGION β†Ί</span>'
    + '</div></div>';

  const cv = document.getElementById('planetSphereCanvas');
  row.querySelectorAll('.orbitWorld').forEach(b=>{
    const choose=e=>{e.preventDefault();e.stopPropagation();selectPlanetKey(b.dataset.planet);};
    if(typeof mfBindTap==='function')mfBindTap(b,choose);else b.addEventListener('pointerdown',choose);
  });
  if(!cv) return;

  const draw = () => {
    draw3DPlanetSphere(cv, activeKey, planetYaw, planetPitch, curRegionId);
  };
  draw();

  cv.onpointerdown = (e) => {
    isDraggingPlanet = true;
    lastDragX = e.clientX; lastDragY = e.clientY;
    planetDragDx = 0; planetDragDy = 0;
    cv.style.cursor = 'grabbing';
    cv.setPointerCapture(e.pointerId);
  };
  cv.onpointermove = (e) => {
    if(!isDraggingPlanet) return;
    const dx = e.clientX - lastDragX, dy = e.clientY - lastDragY;
    planetDragDx = (planetDragDx||0) + Math.abs(dx);
    planetDragDy = (planetDragDy||0) + Math.abs(dy);
    lastDragX = e.clientX; lastDragY = e.clientY;
    planetYaw += dx * 0.01;
    planetPitch = Math.max(-0.8, Math.min(0.8, planetPitch - dy * 0.01));
    draw();
  };
  cv.onpointerup = cv.onpointercancel = (e) => {
    isDraggingPlanet = false;
    cv.style.cursor = 'grab';
  };

  cv.onclick = (e) => {
    const rect = cv.getBoundingClientRect();
    const mx = e.clientX - rect.left, my = e.clientY - rect.top;

    /* Ignore clicks that dragged more than a few pixels; the globe is rotatable,

       and without a threshold every tap ends up nudging the planet. */
    if(typeof planetDragDx==='number' && (Math.abs(planetDragDx)>4 || Math.abs(planetDragDy)>4)) return;

    /* Globe taps select one of the four surface regions, never another world. */
    const W = cv.width, H = cv.height, R = Math.min(W,H)*0.32, cx = W*0.5, cy = H*0.53;
    for(const reg of P.regions){
      const rLat=reg.lat, rLon=reg.lon+planetYaw;
      const cosLat=Math.cos(rLat), sinLat=Math.sin(rLat);
      const cosLon=Math.cos(rLon), sinLon=Math.sin(rLon);
      const px=cx + R * cosLat * sinLon;
      const py=cy - R * (sinLat * Math.cos(planetPitch) - cosLat * cosLon * Math.sin(planetPitch));
      const pz=cosLat * cosLon * Math.cos(planetPitch) + sinLat * Math.sin(planetPitch);

      const sectorHitRadius = Math.max(38, (reg.rad || 0.38) * R * 1.1);
      if(pz > -0.15 && Math.hypot(mx - px, my - py) < sectorHitRadius){
        if(typeof mfConquestRegionOpen==='function'&&!mfConquestRegionOpen(activeKey,reg.id)){
          if(typeof toast==='function') toast('πŸ”’ LIBERATE THE PREVIOUS REGION FIRST');
          if(typeof sfx==='function') sfx('deny');
          break;
        }
        curRegionId = reg.id;
        if(reg.maps && reg.maps.length > 0) syncBattlefieldFromMap(theatreMapId(reg.maps,'standard'));
        if(typeof sfx==='function') sfx('ui');
        renderPlanetRow(); renderMapRow(); renderSpawnPlanner();
        break;
      }
    }
  };
}

/* Bounded, least-recently-used. Square backing stores took each entry from

   0.37 MB to 0.59 MB; all 48 sites would be ~28 MB of canvases on a device

   whose heightfield family already costs ~76 MB. 12 covers a browsing session

   without the tail sitting in memory for the rest of the match. */
const mapPrevCache={}; const mapPrevOrder=[]; const MAP_PREV_MAX=12;
function mapPrevTouch(k){
  const at=mapPrevOrder.indexOf(k); if(at>=0) mapPrevOrder.splice(at,1);
  mapPrevOrder.push(k);
  while(mapPrevOrder.length>MAP_PREV_MAX){
    const old=mapPrevOrder.shift();
    if(old!==k) delete mapPrevCache[old];
  }
}
function syncBattlefieldFromMap(key){
  const def=MAPDEFS[key]; if(!def) return false;
  if(typeof mfConquestMapOpen==='function'&&!mfConquestMapOpen(key))return false;
  curMap=key;
  /* Every picker (War Table, galaxy UI, conquest advance) funnels through here,

     so this is the one place the region can be kept in step with the site. */
  if(def.region) curRegionId=def.region;
  if(def.theme&&typeof THEMES!=='undefined'&&THEMES[def.theme]) curTheme=def.theme;
  if(def.size) battlefieldPreset=battlefieldPresetKey(def.size);
  document.querySelectorAll('.bsbtn').forEach(b=>b.classList.toggle('on',b.dataset.bs===battlefieldPreset));
  const h=$('battleScaleHint'); if(h) h.textContent=battlefieldPresetHint();
  return true;
}
function renderMapRow(){
  const row=$('mapRow'); if(!row) return;
  const planet=typeof planetForMap==='function'?planetForKey(planetForMap(curMap)):planetForKey(planetForTheme(curTheme));
  const reg=(planet.regions && planet.regions.find(r => r.id === curRegionId)) || (planet.regions && planet.regions[0]);
  const regionKeys=(reg && reg.maps) || getPlanetMaps(planet);
  const rn=$('regionMissionName'); if(rn) rn.textContent=(reg&&reg.nm)||'REGION';

  row.innerHTML='';
  for(const key of regionKeys){
    const def=MAPDEFS[key];
    if(!def) continue;
    const card=document.createElement('div');
    const conquestOpen=typeof mfConquestMapOpen!=='function'||mfConquestMapOpen(key),conquestWon=typeof mfConquestWon==='function'&&mfConquestWon(key);
    card.className='mapCard'+(curMap===key?' sel':'')+(conquestOpen?'':' locked')+(conquestWon?' conquered':'');
    card.dataset.map=key;
    const ck=key+'_'+(def.theme||curTheme);
    let cv3=mapPrevCache[ck];
    if(!cv3){
      /* 96Γ—72 was acceptable while these were icon-sized thumbnails. The War

         Table makes one site nearly phone-wide, where that source becomes a

         visibly blocky stamp. 192Γ—120 stays under five MB even if every one of

         the 48 sites is browsed and cached, but keeps rivers, roads and city

         footprints legible during map choice. */
      cv3=document.createElement('canvas'); cv3.width=384; cv3.height=384;
      drawMapPreview(cv3,def,def.theme||curTheme);
      mapPrevCache[ck]=cv3;
      mapPrevTouch(ck);
    }
    card.appendChild(cv3);
    const hz=(typeof mapHazardDef==='function')?mapHazardDef(key):((typeof MAPHAZ!=='undefined'&&MAPHAZ[key])?MAPHAZ[key]:null);
    const sz={compact:'COMPACT Β· 2.2 KM',standard:'STANDARD Β· 2.6 KM',large:'LARGE Β· 3.2 KM'}[def.size]||'BATTLEFIELD';
    const CQ=typeof mfConquestLocate==='function'?mfConquestLocate(key):null;
    const firstClear=CQ&&typeof mfConquestReward==='function'?mfConquestReward(key):null;
    const contract=typeof modeRewardContract==='function'?modeRewardContract('standard'):null;
    const rewardXp=firstClear?Math.round(firstClear.xp*(contract?contract.xp:1)):0;
    const rewardItem=contract&&contract.item&&typeof INV_CONSUMABLES!=='undefined'?INV_CONSUMABLES.find(x=>x.id===contract.item):null;
    card.insertAdjacentHTML('beforeend','<div class="mSize">'+sz+'</div><div class="mNm">'+def.nm+'</div><div class="mDs">'+def.ds+'</div>'
      +(CQ?'<div class="mConquest"><span>FRONT '+CQ.tier+' / 48</span><b>'+(conquestWon?'SECURED':conquestOpen?['EASY','NORMAL','HARD'][CQ.mi]+' THREAT':'LOCKED')+'</b></div>':'')
      +(firstClear?'<div class="mReward"><span>FIRST CLEAR Β· +'+rewardXp+' XP Β· +'+firstClear.cores+' CORES</span><b>'+(rewardItem?rewardItem.em+' '+rewardItem.nm+' Β· ONE MATCH':'')+'</b></div>':'')
      +(hz?'<div class="mHz"><b>'+hz.em+' '+hz.nm+'</b>'+hz.ds+'</div>':''));
    card.addEventListener('pointerdown',()=>{if(!conquestOpen){toast('πŸ”’ SECURE THE PREVIOUS BATTLEFIELD FIRST');sfx('deny');return;}window._mfTheatrePick=def.size;syncBattlefieldFromMap(key); if(typeof sfx==='function') sfx('ui'); renderMapRow(); renderSpawnPlanner();
      /* The region's threat/weather dossier is map-specific. Keep it in sync

         when a site changes without recursively rebuilding the card row. */
      if($('setupScr')?.classList.contains('galaxyStage-region')&&typeof mfGalaxyRenderRegion==='function')mfGalaxyRenderRegion(false);
    });
    row.appendChild(card);
  }
  if(typeof drawSpawnPlanner==='function') drawSpawnPlanner();
}
let settingsFrom='menu';
let inboxFromMatch=false;
const FRONT_SCREEN_IDS=['startScreen','warScr','setupScr','devScr','opsScr','dailyScr','dossierScr','inboxScr','socialScr','updScr','profileScr','settingsScr','armory'];
/* STAGE8_FRONT_FOCUS_BEGIN

   Front screens replace one another instead of using browser navigation, so

   the browser cannot infer either the new reading start or where Back should

   return focus. Keep a tiny route stack: opening records the focused trigger;

   returning to an existing route restores that trigger. Modal focus traps stay

   authoritative and block delayed front-screen focus from stealing it. */
const mfFrontFocusPath=[];
const mfFrontFocusOrigins=new Map();
const MF_PAUSE_MODAL_EXEMPT=new Set(['pauseOverlay','accDlg','apConfirmOverlay','apOverlay','dispatch']);
const mfPauseInertRestore=[];
let mfFrontFocusTicket=0,mfPauseFocusOrigin=null,mfPauseModalActive=false;
function mfFrontElementVisible(el){
  if(!el||el.isConnected===false)return false;
  try{const s=getComputedStyle(el);return s.display!=='none'&&s.visibility!=='hidden';}catch(e){return true;}
}
function mfFrontFocusBlocked(){
  for(const id of ['apConfirmOverlay','apOverlay','accDlg','dispatch','pauseOverlay']){
    const el=$(id);if(mfFrontElementVisible(el))return true;
  }
  return false;
}
function mfFrontFocusCandidate(el){
  if(!el)return null;
  if(el.id==='startScreen')return $('startBtn')||el;
  const explicit=el.querySelector&&el.querySelector('[data-front-focus]');if(explicit)return explicit;
  const heading=el.querySelector&&el.querySelector('h1,h2,[role="heading"]');
  if(heading){heading.tabIndex=-1;return heading;}
  return el.querySelector&&el.querySelector('button:not(:disabled),input:not(:disabled),select:not(:disabled),textarea:not(:disabled),[tabindex]:not([tabindex="-1"])')||el;
}
function mfFrontFocusRebind(target,root){
  if(!target||!root)return null;
  if(target.isConnected!==false&&root.contains(target))return target;
  /* Several front screens rebuild their cards with innerHTML before Back.

     Preserve the control's semantic identity so focus lands on the replacement

     instead of falling back to the screen heading. */
  if(target.id){
    const current=$(target.id);
    if(current&&current.isConnected!==false&&root.contains(current))return current;
  }
  const data=target.dataset||{};
  for(const key of ['mode','set','tab','deck','map','fac','char']){
    if(data[key]==null||!root.querySelectorAll)continue;
    for(const current of root.querySelectorAll('[data-'+key+']')){
      if(current.dataset&&current.dataset[key]===data[key])return current;
    }
  }
  return null;
}
function mfFrontFocusLater(el,preferred){
  const ticket=++mfFrontFocusTicket;
  requestAnimationFrame(()=>{
    if(ticket!==mfFrontFocusTicket||!el||document.body.dataset.frontScreen!==el.id||!mfFrontElementVisible(el)||mfFrontFocusBlocked())return;
    let target=mfFrontFocusRebind(preferred,el);
    if(!target||!mfFrontElementVisible(target))target=mfFrontFocusCandidate(el);
    if(!target||typeof target.focus!=='function')return;
    try{target.focus({preventScroll:true});}catch(e){target.focus();}
  });
}
function mfFrontFocusRoute(from,id,el){
  if(!mfFrontFocusPath.length&&from)mfFrontFocusPath.push(from);
  const active=document.activeElement,backAt=mfFrontFocusPath.lastIndexOf(id);
  let preferred=null;
  if(from&&from!==id&&backAt>=0){
    preferred=mfFrontFocusOrigins.get(from)||null;
    for(let i=mfFrontFocusPath.length-1;i>backAt;i--)mfFrontFocusOrigins.delete(mfFrontFocusPath[i]);
    mfFrontFocusPath.length=backAt+1;
  }else if(from!==id){
    const source=from&&$(from);
    if(active&&active!==document.body&&active.isConnected!==false&&(!source||source.contains(active)))
      mfFrontFocusOrigins.set(id,active);
    if(backAt>=0)mfFrontFocusPath.length=backAt+1;else mfFrontFocusPath.push(id);
  }else if(!mfFrontFocusPath.length)mfFrontFocusPath.push(id);
  mfFrontFocusLater(el,preferred);
}
function mfFrontFocusRestore(id,fallback){
  const target=mfFrontFocusOrigins.get(id)||fallback;
  mfFrontFocusOrigins.delete(id);
  if(mfFrontFocusPath[mfFrontFocusPath.length-1]===id)mfFrontFocusPath.pop();
  const ticket=++mfFrontFocusTicket;
  requestAnimationFrame(()=>{
    if(ticket!==mfFrontFocusTicket||!mfFrontElementVisible(target)||typeof target.focus!=='function')return;
    try{target.focus({preventScroll:true});}catch(e){target.focus();}
  });
}
function mfFrontFocusReset(){
  ++mfFrontFocusTicket;mfFrontFocusPath.length=0;mfFrontFocusOrigins.clear();
}
function mfPauseSetModal(active){
  const overlay=$('pauseOverlay');if(!overlay)return;
  if(active){
    if(mfPauseModalActive)return;
    mfPauseModalActive=true;mfPauseInertRestore.length=0;
    for(const node of Array.from(document.body.children||[])){
      if(node===overlay||MF_PAUSE_MODAL_EXEMPT.has(node.id)||node.tagName==='SCRIPT')continue;
      mfPauseInertRestore.push({node,property:!!node.inert,attribute:node.hasAttribute&&node.hasAttribute('inert')});
      node.inert=true;if(node.setAttribute)node.setAttribute('inert','');
    }
  }else{
    if(!mfPauseModalActive)return;
    mfPauseModalActive=false;
    for(const prior of mfPauseInertRestore){
      if(!prior.node||prior.node.isConnected===false)continue;
      prior.node.inert=prior.property;
      if(prior.attribute){if(prior.node.setAttribute)prior.node.setAttribute('inert','');}
      else if(prior.node.removeAttribute)prior.node.removeAttribute('inert');
    }
    mfPauseInertRestore.length=0;
  }
}
function mfPauseHigherModalVisible(){
  for(const id of ['accDlg','apConfirmOverlay','apOverlay','dispatch'])if(mfFrontElementVisible($(id)))return true;
  return false;
}
function mfPauseFocusable(){
  const overlay=$('pauseOverlay');if(!overlay||!overlay.querySelectorAll)return [];
  return Array.from(overlay.querySelectorAll('button:not(:disabled),[href],input:not(:disabled),select:not(:disabled),textarea:not(:disabled),[tabindex]:not([tabindex="-1"])'))
    .filter(mfFrontElementVisible);
}
function mfPauseTrapKeydown(ev){
  const overlay=$('pauseOverlay');
  if(!mfFrontElementVisible(overlay)||mfPauseHigherModalVisible())return;
  const controls=mfPauseFocusable();if(!controls.length)return;
  if((ev.key==='Enter'||ev.key===' ')&&!overlay.contains(ev.target)){
    ev.preventDefault();ev.stopPropagation();if(ev.stopImmediatePropagation)ev.stopImmediatePropagation();
    controls[0].focus({preventScroll:true});return;
  }
  if(ev.key!=='Tab')return;
  const first=controls[0],last=controls[controls.length-1],active=document.activeElement;
  if(ev.shiftKey&&(active===first||!overlay.contains(active))){
    ev.preventDefault();ev.stopPropagation();last.focus({preventScroll:true});
  }else if(!ev.shiftKey&&(active===last||!overlay.contains(active))){
    ev.preventDefault();ev.stopPropagation();first.focus({preventScroll:true});
  }
}
function mfOpenPause(){
  if(!running)return;
  const active=document.activeElement;
  if(active&&active!==document.body&&active.isConnected!==false)mfPauseFocusOrigin=active;
  const net=typeof window!=='undefined'&&window.MFMatchRuntime&&typeof MFMatchRuntime.status==='function'?MFMatchRuntime.status():null;
  /* A local overlay may not stop a server-authoritative room. Online play

     keeps consuming 30 Hz ticks while the menu is open. */
  const live=!!(net&&net.state==='running');
  paused=!live;
  $('pauseTitle').textContent=live?'MATCH LIVE':'PAUSED';
  $('pauseStatus').hidden=!live;
  $('resumeBtn').textContent=live?'β–Ά  RETURN TO MATCH':'β–Ά  RESUME';
  $('pauseOverlay').style.display='flex';
  mfPauseSetModal(true);
  const target=$('resumeBtn');requestAnimationFrame(()=>{if(mfFrontElementVisible($('pauseOverlay'))&&target)target.focus({preventScroll:true});});
}
function mfClosePause(){
  paused=false;mfPauseSetModal(false);$('pauseOverlay').style.display='none';
  let target=mfPauseFocusOrigin;
  if(!target||target.isConnected===false||!mfFrontElementVisible(target))target=$('menuBtn');
  mfPauseFocusOrigin=null;
  requestAnimationFrame(()=>{if(mfFrontElementVisible(target)&&target)target.focus({preventScroll:true});});
}
/* STAGE8_FRONT_FOCUS_END */
/* THE DIORAMA IS THE MENU'S BACKGROUND, AND NOTHING ELSE'S.

   Every other front screen β€” War Room, Development, Operations, Armory, the

   Dossier, the store, Settings β€” is an opaque full-screen panel. Behind them

   the attract branch in frame() was still stepping the entire simulation and

   drawing a complete 3D frame (terrain, every model, SSAO, bloom, at full

   resolution) for nobody to see. On a phone that is the research tree quietly

   heating the device, and then handing a thermally throttled GPU to the match

   the player opens next. */
let attractVisible=true;
function frontRouteTarget(id){
  const el=(typeof id==='string'&&id)?$(id):null;
  if(el)return el;
  /* Mixed OTA installs can briefly run newer JS against an older HTML shell.

     Refuse the transition before touching the currently visible route; a

     missing optional screen must leave a usable menu, never a blank app. */
  try{console.warn('[front-route] target unavailable:',id);}catch(e){}
  return null;
}
function hideFrontScreens(except){
  for(const id of FRONT_SCREEN_IDS){
    if(id===except) continue;
    const el=$(id); if(el) el.style.display='none';
  }
  if(typeof apClose==='function') try{ apClose(); }catch(e){}
  const ap=$('apOverlay'); if(ap) ap.style.display='none';
  const apc=$('apConfirmOverlay'); if(apc) apc.style.display='none';
  if(except===undefined){
    mfFrontFocusReset();
    mfPauseSetModal(false);
    attractVisible=false;   // bare call = dropping into a match
    if(document.body&&document.body.dataset){
      document.body.dataset.frontScreen='';
      delete document.body.dataset.frontPopup;
    }
    if(typeof audMusicEnterMatch==='function') audMusicEnterMatch();
  }
}
function showFrontScreen(id){
  const el=frontRouteTarget(id);if(!el)return false;
  const from=document.body.dataset.frontScreen||'';
  hideFrontScreens(id);
  el.style.display='flex';
  /* The menu artwork should arrive like a live console, but the route itself

     remains synchronous and focus-safe. Toggling one scoped class restarts the

     short compositor-only reveal without moving controls beneath a held touch. */
  el.classList.remove('mfFrontEntering');
  void el.offsetWidth;
  el.classList.add('mfFrontEntering');
  document.body.dataset.frontScreen=id;
  delete document.body.dataset.frontPopup;
  attractVisible=(id==='startScreen');
  /* Re-evaluated every time the menu is shown rather than once at boot: a

     session can be dropped mid-play, and the offer has to be there when the

     player lands back here β€” which is the moment they are looking for it. */
  if(id==='startScreen'&&typeof sessRenderResume==='function') sessRenderResume();
  if(typeof audMusicEnterScreen==='function') audMusicEnterScreen(id);
  mfFrontFocusRoute(from,id,el);
  return true;
}
/* The battlefield already owns a shared-context 3D preview renderer. Reuse it

   in menu rooms instead of inventing screenshots of units: one context serves

   every hidden/visible card and the pump pauses work for hidden destinations. */
function mfEnsureMenu3D(hostId,kind,id,tag){
  const host=$(hostId);
  if(!host||host.dataset.ready||typeof mfIntelPreviewWindow!=='function')return;
  host.replaceChildren(mfIntelPreviewWindow(kind,id,tag,typeof playerKitKey==='function'?playerKitKey():'nova'));
  host.dataset.ready='1';
}
/* Match overlays are not front screens: opening one must keep match music and

   closing it must reveal the battlefield rather than route to the menu. They

   still use a validated, explicit route so optional OTA UI cannot blank the

   shell and Android Back can close the same state the HUD button opened. */
function showMatchPopup(id){
  const el=frontRouteTarget(id);if(!el)return false;
  hideFrontScreens();
  el.style.display='flex';
  document.body.dataset.frontPopup=id;
  return true;
}
function closeMatchPopup(id){
  const el=frontRouteTarget(id);if(!el)return false;
  el.style.display='none';
  if(document.body.dataset.frontPopup===id)delete document.body.dataset.frontPopup;
  /* Closing a front-screen-shaped match popup must restore the HUD in the same

     turn. Depending only on hudflow's observer left mfMenuOpen latched in short

     landscape until an unrelated mutation happened, so Back looked inert even

     though the Inbox itself had closed. */
  if(typeof mfFlowLayout==='function')mfFlowLayout();
  return true;
}
function openSettings(from){
  settingsFrom=from;
  renderSettings();
  if(from==='pause'){mfPauseSetModal(false);$('pauseOverlay').style.display='none';}
  showFrontScreen('settingsScr');
}
let nativeBackExitAt=0;
function nativeLayerVisible(id){
  const el=$(id);return !!(el&&getComputedStyle(el).display!=='none');
}
function nativeBackTap(id){
  const el=$(id);if(!el)return false;
  if(typeof MF_POINTER_COMMIT!=='undefined') MF_POINTER_COMMIT=-1e9;
  /* Blur FIRST. Android Back used to navigate straight out of a screen with a

     text field still focused, so the field's commit handler never ran and

     whatever had been typed was discarded β€” silently, which is the worst way to

     lose a name someone chose. */
  const a=document.activeElement;
  if(a&&a!==document.body&&typeof a.blur==='function') a.blur();
  el.click();
  return true;
}
function handleNativeBack(AppPlugin){
  if(nativeLayerVisible('mfNoticeHistory')){if(typeof mfNoticeHistoryClose==='function')mfNoticeHistoryClose();return;}
  /* Close the topmost modal first. Android Back now follows the same visual

     hierarchy as the on-screen controls instead of terminating the Activity. */
  if(nativeLayerVisible('apConfirmOverlay')){nativeBackTap('apConfirmNo');return;}
  if(nativeLayerVisible('apOverlay')){if(typeof apClose==='function')apClose();return;}
  if(nativeLayerVisible('accDlg')){nativeBackTap('accDlgN');return;}
  if(nativeLayerVisible('dispatch')){nativeBackTap('dispOk');return;}
  const layers=[
    ['settingsScr','setBack'],['profileScr','profBack'],['armory','armoryBack'],
    ['setupScr','setupBack'],['warScr','warBack'],['devScr','devBack'],['opsScr','opsBack'],
    ['dailyScr','dailyBack'],['dossierScr','dossBack'],['socialScr','socialBack'],['inboxScr','inboxBack'],['updScr','updBack'],['gameOver','restartBtn']
  ];
  for(const L of layers)if(nativeLayerVisible(L[0])){nativeBackTap(L[1]);return;}
  if(nativeLayerVisible('pauseOverlay')){nativeBackTap('resumeBtn');return;}
  if(typeof aiming!=='undefined'&&aiming>=0){
    if(aiming===5&&typeof cancelArtilleryBarrageAim==='function')cancelArtilleryBarrageAim(true);
    else aiming=-1;
    toast('Targeting cancelled');sfx('ui');return;
  }
  if(typeof placing!=='undefined'&&placing){cancelPlace();sfx('ui');return;}
  if(nativeLayerVisible('buildMenu')||nativeLayerVisible('bldMenu2')||nativeLayerVisible('prodMenu')){
    closeMenus();sfx('ui');return;
  }
  if(nativeLayerVisible('baseFinder')){if(typeof closeBaseFinder==='function')closeBaseFinder(true);else $('baseFinder').style.display='none';sfx('ui');return;}
  /* Drafted map commands outrank Pause. Leaving either mode armed while opening

     the overlay makes the first tap after Resume execute an old instruction. */
  if(typeof armQueue!=='undefined'&&armQueue){cancelQueueDraft(false);return;}
  if(typeof armRally!=='undefined'&&armRally>=0){
    if(typeof disarmRally==='function')disarmRally();else armRally=-1;
    toast('Rally placement cancelled');sfx('ui');return;
  }
  if(typeof armPatrol!=='undefined'&&armPatrol){cancelPatrolDraft(false);return;}
  if(typeof armFormation!=='undefined'&&armFormation){
    armFormation=false;orderPreview=null;
    const f=$('formBtn');if(f)f.classList.remove('on');toast('Formation placement cancelled');sfx('ui');return;
  }
  if(typeof boxMode!=='undefined'&&boxMode){boxMode=false;const b=$('boxBtn');if(b)b.classList.remove('on');sfx('ui');return;}
  if(running){mfOpenPause();sfx('ui');return;}
  const now=performance.now();
  if(now-nativeBackExitAt<1800){if(AppPlugin&&AppPlugin.exitApp)AppPlugin.exitApp();return;}
  nativeBackExitAt=now;toast('Press Back again to exit MASSFRONT');
}
function nativeBackEscape(e){
  /* Desktop Escape follows the same hierarchy as Android Back. Capture-owned

     modals (the intro and account portal) either preventDefault or stop the

     event first, so their local focus traps remain authoritative. */
  if(e.key!=='Escape'||e.defaultPrevented||e.repeat||e.isComposing)return;
  e.preventDefault();e.stopPropagation();handleNativeBack();
}
function initNativeNavigation(){
  if(typeof window!=='undefined'&&!window.__mfNativeEscapeReady){
    window.__mfNativeEscapeReady=true;
    window.addEventListener('keydown',nativeBackEscape);
  }
  const C=typeof window!=='undefined'&&window.Capacitor;
  const A=C&&C.Plugins&&C.Plugins.App;
  if(!A||typeof A.addListener!=='function')return;
  A.addListener('backButton',()=>handleNativeBack(A));
}

/* One exit transaction for victory, defeat and Pause > Main Menu. Partial

   visibility flips left pointer capture, tutorial chrome and modal layers from

   the match alive behind the front end. Resetting them together makes the

   return path deterministic and gives Android Back the same clean hierarchy as

   a cold launch. */
function returnToMainMenu(){
  mfResultPresentationEpoch++;
  mfLastResultCanRestart=false;
  const networkRuntime=typeof window!=='undefined'&&window.MFMatchRuntime,
    networkConsumer=typeof window!=='undefined'&&window.MFMatchCommandConsumer;
  /* Only an explicit menu exit releases the match's input/lockstep ownership.

     A closed socket or terminal screen must never reopen offline mutation. */
  if(networkRuntime&&typeof networkRuntime.close==='function')networkRuntime.close();
  if(networkConsumer&&typeof networkConsumer.leaveSession==='function')networkConsumer.leaveSession();
  if(window.MFSocialUI&&typeof MFSocialUI.clearFinishedMatch==='function')MFSocialUI.clearFinishedMatch();
  /* Training's protected rules are scoped to its operation. This is the one

     intentional early-exit path; incidental UI state changes must not call it. */
  if(typeof cancelTrainingMission==='function') cancelTrainingMission();
  /* Weekly borrows the player's threat/modifier plan for its authored brief.

     endgameRecord hands it back at the end of a FINISHED run β€” abandoning one

     never reached that code, so quitting a Weekly kept the loan forever. */
  if(typeof weeklyMode!=='undefined'&&weeklyMode&&typeof restoreWeeklyConfig==='function'){
    restoreWeeklyConfig(); weeklyMode=false;
  }
  /* Same contract for an authored campaign mission's modifier set. */
  if(typeof storyCampaignRestoreMods==='function') storyCampaignRestoreMods();
  paused=false;mfPauseSetModal(false);mfPauseFocusOrigin=null;running=false;matchLive=false;
  if(typeof audMusicLeaveMatch==='function') audMusicLeaveMatch();
  if(typeof resetInputState==='function') resetInputState();
  /* Do not leave an old front-end screen in the DOM hit-test stack. All menu

     overlays share a z-index, so one stale `display:flex` layer can sit above

     the newly shown main menu and silently eat every tap. */
  for(const id of FRONT_SCREEN_IDS.concat(['pauseOverlay','gameOver','levelUp','loadScr'])){
    const el=$(id); if(el) el.style.display='none';
  }
  if(typeof adClearPostMatchAd==='function') adClearPostMatchAd();
  if(typeof apClose==='function') apClose();
  document.querySelectorAll('.apOverlay,.apConfirmOverlay').forEach(el=>{ el.style.display='none'; });
  const dlg=$('accDlg'); if(dlg) dlg.style.display='none';
  const dp=$('dispatch'); if(dp){ dp.style.display='none'; dp.classList.remove('in'); }
  closeMenus(); cancelPlace();
  showHudDock(false);
  showFrontScreen('startScreen');
  renderMetaHead(); setupAttract();
  if(typeof storyCheck==='function') setTimeout(storyCheck,420);
}
/* Takeovers may wrap returnToMainMenu more than once across OTA/source load

   orders. Protected onboarding must retain one stable path to the real base

   cleanup so its faction gate is not unloaded by a Galactic return wrapper. */
window.__MF_RETURN_TO_BASE_FOR_COMMISSIONING__=returnToMainMenu;
function restartCurrentBattle(){
  if(!gameEnded||!mfLastResultCanRestart)return false;
  mfLastResultCanRestart=false;
  mfResultPresentationEpoch++;
  /* A completed loss may leave a pre-defeat recovery snapshot. Clear it before

     building a genuinely fresh attempt on the same selected battlefield. */
  if(typeof sessClear==='function')sessClear();
  if(typeof adClearPostMatchAd==='function')adClearPostMatchAd();
  if(typeof mfDepart!=='undefined')mfDepart.fromVictory=false;
  paused=false;running=false;matchLive=false;
  if(typeof resetInputState==='function')resetInputState();
  const go=$('gameOver');if(go)go.style.display='none';
  closeMenus();cancelPlace();hideFrontScreens();
  mfLoadScreenFill();$('loadScr').style.display='flex';
  requestAnimationFrame(()=>requestAnimationFrame(()=>{
    applyTheme();newSkirmish();
    $('loadScr').style.display='none';
    stopAttract();
    if(typeof mfFlowLayout==='function')mfFlowLayout();
  }));
  return true;
}
function continueToNextMap(){
  const C=typeof window!=='undefined'&&window.MFMatchCommandConsumer;
  if(C&&typeof C.sessionActive==='function'&&C.sessionActive())return returnToMainMenu();
  /* Rewards already landed in endGame/metaGrant. This only launches the next

     unlocked War Table site β€” do not call returnToMainMenu first or the

     attract diorama eats the loadout. */
  if(typeof mfDepart!=='undefined') mfDepart.fromVictory=false;
  if(typeof mfConquestHasNextMap!=='function'||!mfConquestHasNextMap()){
    if(typeof returnToMainMenu==='function') returnToMainMenu();
    return;
  }
  const map=mfConquestNextMap();
  const L=typeof mfConquestLocate==='function'?mfConquestLocate(map):null;
  if(!L||typeof syncBattlefieldFromMap!=='function'||!syncBattlefieldFromMap(map)){
    if(typeof toast==='function') toast('NO NEXT BATTLEFIELD');
    if(typeof returnToMainMenu==='function') returnToMainMenu();
    return;
  }
  curTheme=L.P.theme; curRegionId=L.R.id;
  if(!(typeof storyCampaignPlanBorrowed==='function'&&storyCampaignPlanBorrowed())){
    META.setup=META.setup||{};
    META.setup.m=curMap; META.setup.t=curTheme;
    if(typeof metaSave==='function') metaSave();
  }
  paused=false; running=false; matchLive=false;
  if(typeof audMusicLeaveMatch==='function') audMusicLeaveMatch();
  const go=$('gameOver'); if(go) go.style.display='none';
  if(typeof adClearPostMatchAd==='function') adClearPostMatchAd();
  closeMenus(); cancelPlace();
  hideFrontScreens();
  mfLoadScreenFill();
  $('loadScr').style.display='flex';
  requestAnimationFrame(()=>requestAnimationFrame(()=>{
    applyTheme(); newSkirmish();
    $('loadScr').style.display='none';
    stopAttract();
    if(typeof mfFlowLayout==='function') mfFlowLayout();
  }));
}
/* Fill the loading screen with the intel the game already authored. PLANETS

   (gl.js:1768) carries ds / biodome / sector / diameter / dayLen / temp /

   climate / lore, and each region carries poi + hook β€” a genuinely large body

   of worldbuilding that the player never saw, because the only place it could

   appear was a screen that said "SURVEYING TERRAIN…" and nothing else.

   Everything is optional-chained: an unknown map must still load. */
function mfLoadScreenFill(){
  const $=id=>document.getElementById(id);
  const setT=(id,v)=>{ const e=$(id); if(e) e.textContent=v||''; };
  const MD=(typeof MAPDEFS!=='undefined'&&typeof curMap!=='undefined')?MAPDEFS[curMap]:null;
  const mapKey=typeof curMap!=='undefined'?String(curMap):'';
  let P=null,R=null,pk='';
  try{
    pk=(typeof planetForMap==='function')?planetForMap(curMap):'';
    P=(pk&&typeof PLANETS!=='undefined')?PLANETS[pk]:null;
    if(P&&P.regions) R=P.regions.find(r=>r.maps&&r.maps.indexOf(curMap)>=0)||null;
  }catch(e){}
  /* Title prefers the authored map name over the generic status line. */
  setT('loadTitle',(MD&&MD.nm)||'GENERATING BATTLEFIELD');
  setT('loadEyebrow',P?('DEPLOYING TO  Β·  '+P.nm):'DEPLOYING');
  /* poi is the named location; hook is the one-line scene set. */
  setT('loadPoi',(R&&R.poi)||(MD&&MD.poi)||'');
  setT('loadHook',(R&&R.hook)||(MD&&MD.ds)||'');
  const host=$('loadStats');
  if(host){
    const chips=[];
    const add=(k,v)=>{ if(v) chips.push('<div class="lsChip"><span class="lsK">'+k+
      '</span><span class="lsV">'+String(v).toUpperCase()+'</span></div>'); };
    add('REGION',R&&R.nm);
    add('SECTOR',P&&P.sector);
    add('CLIMATE',P&&P.climate);
    add('TEMP',P&&P.temp);
    add('DAY',P&&P.dayLen);
    add('SCALE',MD&&MD.size);
    add('HAZARD',MD&&MD.hazard);
    host.innerHTML=chips.join('');
  }
  /* Drive the composition from the same authored assets the selected match

     will actually render. The flat world survey is deliberately not an orb:

     it reads as a deployment cartography layer, while the location sheet is

     the real tactical ground material. A stable map hash shifts the crop so

     sibling battlefields do not present as one repeated picture. */
  const load=$('loadScr');
  if(load){
    let surface=null,profile=null,worldArt='';
    try{
      if(typeof mfTerrainSurfaceSelection==='function') surface=mfTerrainSurfaceSelection(
        typeof curTheme!=='undefined'?curTheme:null,mapKey,R&&R.id);
      if(typeof mfTerrainLocationProfile==='function') profile=mfTerrainLocationProfile(mapKey);
      const art=typeof MF_PLANET_ART_V1!=='undefined'&&MF_PLANET_ART_V1.worlds&&MF_PLANET_ART_V1.worlds[pk];
      worldArt=art&&art.channels&&art.channels.basecolor||'';
    }catch(e){}
    const safeArt=value=>/^(?:\.\/)?assets\/[a-z0-9_./-]+$/i.test(String(value||''))?String(value):'';
    /* Relative url() values substituted into ui.css custom properties resolve

       against src/styles/, not this document. Keep OTA data URIs, and anchor

       packaged paths to the app entry before handing them to the stylesheet. */
    const cssArtURL=value=>{
      const rel=safeArt(value);if(!rel)return '';
      const mapped=typeof mf2AssetURL==='function'?mf2AssetURL(rel):'./'+rel.replace(/^\.\//,'');
      return mapped.startsWith('data:')?mapped:new URL(mapped,document.baseURI).href;
    };
    const terrainArt=cssArtURL(surface&&surface.albedo),surveyArt=cssArtURL(worldArt);
    const accent=R&&/^#[0-9a-f]{6}$/i.test(R.color||'')?R.color:'#5ad4ff';
    const n=parseInt(accent.slice(1),16),rgb=[(n>>16)&255,(n>>8)&255,n&255].join(',');
    let seed=0;for(let i=0;i<mapKey.length;i++)seed=(seed*31+mapKey.charCodeAt(i))>>>0;
    load.style.setProperty('--mf-match-terrain',terrainArt?'url("'+terrainArt+'")':'none');
    load.style.setProperty('--mf-match-world',surveyArt?'url("'+surveyArt+'")':'none');
    load.style.setProperty('--mf-match-accent',accent);
    load.style.setProperty('--mf-match-accent-rgb',rgb);
    load.style.setProperty('--mf-match-x',(28+(seed%45))+'%');
    load.style.setProperty('--mf-match-y',(24+((seed>>>7)%53))+'%');
    load.dataset.world=pk||'unknown';load.dataset.region=R&&R.id||'unknown';load.dataset.map=mapKey||'unknown';
    load.dataset.surface=surface&&surface.key||'base';load.dataset.locationProfile=profile&&profile.key||'natural';
    load.setAttribute('aria-label','Preparing '+((MD&&MD.nm)||'battlefield')+(P?' on '+P.nm:''));
    setT('loadMissionCode',[(pk||'field').toUpperCase(),(R&&R.id||'open-site').toUpperCase(),mapKey.toUpperCase()].join(' // '));
    setT('loadSurface',[(profile&&profile.key||'natural'),(surface&&surface.key||'base')].join(' Β· ').toUpperCase()+' MATERIAL SET');
  }
  /* Rotate the status line so a long generate does not look frozen on one

     string. Terrain gen blocks the main thread, so this is set once per show

     rather than animated β€” an interval would not tick anyway. */
  const subs=['SURVEYING TERRAIN…','MAPPING HYDROLOGY…','PLACING RESOURCE NODES…',
              'RAISING SETTLEMENTS…','SEEDING FLORA…'];
  setT('loadSub',subs[(Math.random()*subs.length)|0]);
}
function mfBuildingServiceFailure(code){
  const copy={
    'building-not-owned':'STRUCTURE CONTROL DENIED',
    'building-gone':'STRUCTURE NO LONGER AVAILABLE',
    'invalid-building':'STRUCTURE NO LONGER AVAILABLE',
    'full-health':'FULL HEALTH',
    'under-construction':'REPAIR UNAVAILABLE DURING CONSTRUCTION',
    'zero-investment':'THIS STRUCTURE CANNOT SELF-REPAIR',
    'unsupported-owner':'STRUCTURE OWNER UNSUPPORTED',
    'service-unavailable':'STRUCTURE SERVICE UNAVAILABLE',
    'network-service-unavailable':'NETWORK STRUCTURE SERVICE UNAVAILABLE',
    'network-service-error':'NETWORK STRUCTURE SERVICE REJECTED'
  };
  return copy[code]||'STRUCTURE SERVICE REJECTED';
}
/* Realtime owns every mutation once its match is active. The public consumer

   returns false only when there is no realtime match, which is the sole case

   allowed to continue into the deterministic offline service. */
function mfRequestBuildingService(kind,B,active){
  if(!B||!B.alive)return {ok:false,code:'building-gone'};
  if(typeof mfLocalOwnsBuilding==='function'&&!mfLocalOwnsBuilding(B))return {ok:false,code:'building-not-owned'};
  const id=blds.indexOf(B);if(id<0)return {ok:false,code:'invalid-building'};
  const C=window.MFMatchCommandConsumer,networkOwned=!!(C&&typeof C.sessionActive==='function'&&C.sessionActive()),
    target=C&&typeof C.buildingRef==='function'?C.buildingRef(id):{id,type:B.type},
    method=kind==='repair'?'submitRepair':'submitRecycle';
  if(C&&typeof C[method]==='function'){
    try{
      const handled=kind==='repair'?C[method](target,!!active):C[method](target);
      if(handled!==false)return {ok:true,network:true,pending:true};
    }catch(e){
      const st=window.MFMatchRuntime&&typeof MFMatchRuntime.status==='function'?MFMatchRuntime.status():null;
       if(networkOwned||st&&st.state==='running')return {ok:false,code:'network-service-error'};
    }
  }
  const st=window.MFMatchRuntime&&typeof MFMatchRuntime.status==='function'?MFMatchRuntime.status():null;
  if(networkOwned||st&&st.state==='running')return {ok:false,code:'network-service-unavailable'};
  const S=window.MFBuildingService;
  if(!S)return {ok:false,code:'service-unavailable'};
  return kind==='repair'&&typeof S.setRepair==='function'?S.setRepair(id,!!active):
    kind==='recycle'&&typeof S.recycle==='function'?S.recycle(id):{ok:false,code:'service-unavailable'};
}
function mfRefreshBuildingServiceControls(B){
  if(typeof mfRenderBuildingServiceControls!=='function')return;
  const row=$('mfBldServiceActions'),panel=row&&row.parentElement&&row.parentElement.id==='prodMenu'?'prodMenu':'bldMenu2';
  mfRenderBuildingServiceControls(B,panel);
}
function mfBuildingUpgradePress(all){
  if(openBld<0||!blds[openBld]||!blds[openBld].alive)return;
  const B=blds[openBld],I=mfBuildingUpgradeBatchInfo(openBld),C=window.MFMatchCommandConsumer,
    R=window.MFMatchRuntime,S=R&&typeof R.status==='function'?R.status():null;
  let result;
  if(all?!I.canUpgradeAll:!I.canUpgradeSelected)result={ok:false,message:all?I.batchReason:I.selectedReason};
  else if(C&&typeof C.sessionActive==='function'&&C.sessionActive()){
    if(!S||S.state!=='running'){
      toast('Reconnecting β€” upgrade order not sent');sfx('reject');return {ok:false,message:'Waiting for network connection'};
    }
    const receipt=C&&typeof C.submitUpgrade==='function'?C.submitUpgrade(openBld,!!all):null;
    result=receipt?{ok:true,queued:true,message:'Upgrade order sent'}:
      {ok:false,message:C&&C.lastFailure()||'Network upgrade unavailable; no resources spent'};
  }else if(all)result=mfStartBuildingUpgradeBatch(openBld);
  else {const err=startUpgrade(openBld);result={ok:!err,message:err||'Upgrading '+I.name};}
  toast(result.message);sfx(result.ok?'ui':'reject');
  if(B.type==='fac')renderProdMenu();else renderBldPanel();
  return result;
}
function mfBuildingRepairPress(){
  if(openBld<0||!blds[openBld]||!blds[openBld].alive)return;
  const B=blds[openBld],S=window.MFBuildingService,Q=S&&typeof S.quote==='function'?S.quote(B):null;
  if(!Q){toast(mfBuildingServiceFailure('service-unavailable'));sfx('reject');return;}
  const enable=!Q.active,R=mfRequestBuildingService('repair',B,enable);
  if(!R||!R.ok){toast(mfBuildingServiceFailure(R&&R.code));sfx('reject');mfRefreshBuildingServiceControls(B);return;}
  if(!R.network){toast(enable?'REPAIR ENABLED':'REPAIR CANCELLED');sfx('ui');}
  mfRefreshBuildingServiceControls(B);
}
function mfBuildingRecyclePress(){
  if(openBld<0||!blds[openBld]||!blds[openBld].alive)return;
  const B=blds[openBld],now=Date.now(),refund=bldRecycleMass(B);
  /* First release arms, second release inside three seconds submits exactly one

     authoritative recycle command. No refund or death mutation lives here. */
  if(!(B.recycleConfirmAt>now)){
    const armedUntil=B.recycleConfirmAt=now+3000,armedIndex=openBld;
    /* The timestamp already makes a late second tap safe, but leaving the red

       CONFIRM label mounted after that timestamp expired made the UI promise a

       destructive action the next tap could not perform. Expire only the same

       arm token and only repaint while this exact building is still open; a

       confirmed command, a re-arm after timer throttling, or another selected

       building must never be overwritten by the old callback. */
    const expireArm=()=>{
      if(B.recycleConfirmAt!==armedUntil)return;
      const remaining=armedUntil-Date.now();
      if(remaining>0){setTimeout(expireArm,remaining+1);return;}
      B.recycleConfirmAt=0;
      if(openBld===armedIndex&&blds[armedIndex]===B&&B.alive)mfRefreshBuildingServiceControls(B);
    };
    setTimeout(expireArm,3001);
    toast('RECYCLE '+BT[B.type].name.toUpperCase()+' Β· +'+refund+' MASS Β· TAP AGAIN');
    mfRefreshBuildingServiceControls(B);sfx('ui');return;
  }
  B.recycleConfirmAt=0;
  const R=mfRequestBuildingService('recycle',B);
  if(!R||!R.ok){toast(mfBuildingServiceFailure(R&&R.code));sfx('reject');mfRefreshBuildingServiceControls(B);return;}
  if(R.network){mfRefreshBuildingServiceControls(B);return;}
  addParticle(3,B.x,B.y,0,0,.5,BT[B.type].size,120,230,255);
  toast(BT[B.type].name+' recycled Β· +'+R.refund+' mass');closeMenus();sfx('ui');
}
function wire(){
  ensureCommanderLocatorShell();ensureBuildingsDeckShell();
  if(typeof mfEnsureBuildingServiceControls==='function')mfEnsureBuildingServiceControls('bldMenu2');
  if(typeof window!=='undefined'&&!window.__mfPauseFocusTrapReady){
    window.__mfPauseFocusTrapReady=true;
    window.addEventListener('keydown',mfPauseTrapKeydown,true);
  }
  document.querySelectorAll('.hudDeckBtn').forEach(b=>mfBindTap(b,e=>{
    e.preventDefault(); setHudDeck(b.dataset.deck);
  }));
  document.querySelectorAll('.globalDiff').forEach(b=>{
    b.addEventListener('pointerdown',()=>{
      const floor=typeof mfConquestDifficultyFloor==='function'?mfConquestDifficultyFloor(curMap):0;
      if(+b.dataset.d<floor){toast('CONQUEST THREAT FLOOR β€” this battlefield begins at '+['EASY','NORMAL','HARD'][floor]);sfx('deny');return;}
      document.querySelectorAll('.globalDiff').forEach(x=>x.classList.remove('on'));
      b.classList.add('on'); difficulty=+b.dataset.d;
      for(const A of aiSlots) if(A.on) A.diff=difficulty;
      renderSpawnPlanner(); initAudio(); sfx('ui');
    });
  });
  const pick=(sel,fn)=>document.querySelectorAll(sel).forEach(b=>{
    b.addEventListener('pointerdown',ev=>{
      ev.stopPropagation();
      document.querySelectorAll(sel).forEach(x=>x.classList.remove('on'));
      b.classList.add('on'); fn(b); initAudio(); sfx('ui');
    });
  });
  pick('.glbtn',b=>{ goalSel=b.dataset.g; $('goalHint').textContent=goalDef().ds; });
  pick('.tmbtn',b=>{ timeLimit=+b.dataset.t;
    /* SHORT FORM PAYS THE ECONOMY FORWARD. The AI's first wave lands around

       eighty seconds and the design assumes a long build-up, so at normal pace

       a five-minute match ends during the opening build order β€” that is not a

       short match, it is a truncated one. A faster economy and denser crates

       move first contact early enough that five minutes is a battle. */
    if(timeLimit&&timeLimit<=300){ resPace=1.6; crateRate=1.5; }
    else if(resPace===1.6){ resPace=1; crateRate=1; } });
  pick('.pcbtn',b=>{ resPace=+b.dataset.p; });
  pick('.crbtn',b=>{ crateRate=crateRateBase=+b.dataset.c; });
  pick('.bsbtn',b=>{
    battlefieldPreset=battlefieldPresetKey(b.dataset.bs);
    normalizeAiSlotsForBattlefield();
    const h=$('battleScaleHint');
    if(h) h.textContent=battlefieldPresetHint();
    renderSpawnPlanner();
  });
  pick('.ifbtn',b=>{
    infestationOn=!!+b.dataset.i;
    $('infestHint').textContent=infestationOn
      ?'Neutral nests spread and erupt during the battle.'
      :'No neutral nests, infestation guards, eruptions, spread, or map-wide tides.';
  });
  pick('.dfbtn',b=>{
    defenseFocus=+b.dataset.df;
    $('defFocusHint').textContent=defenseFocus
      ?'Tower defence: +20% defence HP, +15% damage, +10% range, 25% faster construction β€” enemy waves arrive 18% faster.'
      :'Classic RTS balance between mobile armies and static defences.';
  });
  pick('.pkgbtn',b=>{
    deploymentPackage=DEPLOYMENT_PACKAGES[b.dataset.pkg]?b.dataset.pkg:'prepared';
    const h=$('deployPkgHint');if(h)h.textContent=deploymentPackageDef().ds;
  });
  initSpawnPlanner();
  /* Battle setup is five short task-focused pages. Showing all of them at once

     duplicated controls under the wrong highlighted tab and recreated the

     overlong mobile form these pages were designed to replace. */
  const setup=$('setupScr');setup.dataset.tab=setup.dataset.tab||'map';
  const setupNames={map:'WORLD & MAP',forces:'ARMIES & STARTS',rules:'VICTORY RULES',economy:'MATCH ECONOMY',modifiers:'RISK MODIFIERS'};
  const setupContext=$('setupContext');if(setupContext)setupContext.textContent=setupNames[setup.dataset.tab]||setupNames.map;
  const oldAdv=$('advToggle');if(oldAdv)oldAdv.remove();
  document.querySelectorAll('.setupTabBtn').forEach(b=>mfBindTap(b,()=>{
    setup.dataset.tab=b.dataset.tab;setup.classList.remove('setupAll');
    document.querySelectorAll('.setupTabBtn').forEach(x=>x.classList.toggle('on',x===b));
    if(setupContext)setupContext.textContent=setupNames[b.dataset.tab]||'';sfx('ui');
    if(b.dataset.tab==='modifiers'&&typeof renderOps==='function') renderOps();
    const sc=setup.querySelector('.setupScroll');if(sc)sc.scrollTop=0;
  }));
  /* The enemy picker is generated from FACTIONS now β€” see renderFacRow() in

     src/factions.js β€” so it carries the real crests and cannot fall out of step

     with a rename. */
  if(typeof renderFacRow==='function') renderFacRow();
  if(typeof initFactionTheme==='function') initFactionTheme();
  document.querySelectorAll('.wbtn').forEach(b=>{
    b.addEventListener('pointerdown',()=>{
      if(b.disabled) return;
      document.querySelectorAll('.wbtn').forEach(x=>x.classList.remove('on'));
      b.classList.add('on'); wcChoice=+b.dataset.w;
      /* Random quick-picks and hand-selected modifiers are intentionally

         exclusive, so neither control can silently override the other. */
      META.opmods={};
      META.wcPref=wcChoice; metaSave(); initAudio(); sfx('ui');
      if(typeof renderOps==='function') renderOps();
    });
  });
  /* GALACTIC EXPLORATION β€” same-tab strategic layer.

     The module is a separate document with its own three.js and its own WebGL

     canvas, so it is opened as a document rather than imported: two contexts

     fighting over one canvas is the failure this avoids, and a crash in an

     unfinished module then cannot take the RTS down with it.

     Complete installers enable this layer directly. Older/slim shells retain

     their opt-in and installation fallback, checked by a launch-time HEAD probe.

     START enters the stable Galactic home. Orbital travel begins only from its

     deliberate Depart control; Training remains available from the War Table.

     Nothing here runs during a match or touches saves. */
  (function mfWireExploration(){
    const URL_='./modules/space_exploration/index.html';
    mfOpenExploration=async function(entryView,options){
      const enabled=window.__MF_BUILD_HAS_GALACTIC_EXPLORATION===true||window.__MF_OTA_HAS_GALACTIC_DELIVERY===true;
      if(!enabled){ if(typeof toast==='function') toast('Galactic game content is not available in this installation'); return false; }
      entryView=entryView==='system'?'system':'campaign_hub';
      if(mfExplorationLaunching)return false;
      mfExplorationLaunching=true;
      const launchButton=options&&options.launchButtonId?$(options.launchButtonId)
        :entryView==='campaign_hub'?$('ugaBtn'):$('startBtn');
      const finishLaunch=message=>{
        mfExplorationLaunching=false;
        if(typeof mfLaunchVeilClose==='function')mfLaunchVeilClose();
        if(launchButton){launchButton.classList.remove('is-launching');launchButton.removeAttribute('aria-busy');}
        if(message&&typeof toast==='function')toast(message);
        return false;
      };
      if(launchButton){launchButton.classList.add('is-launching');launchButton.setAttribute('aria-busy','true');}
      /* The pressed button alone is a 2px bar the player reported as nothing

         happening, and the toast below lands in a rail the menus hide. Raise

         the full-screen launch state in the same task as the tap: it is the

         only feedback that survives the whole window in which this document

         keeps painting while the module document loads. */
      /* Name the veil after the control the player pressed, not the internal

         entry view. UGA COMMAND deliberately opens entryView 'system' (see the

         door below), so keying the title off the view announced a UGA COMMAND

         tap as "UGA EXPEDITION" β€” a destination the player never asked for. */
      const veilHome=(options&&options.launchButtonId==='ugaBtn')||entryView==='campaign_hub';
      if(typeof mfLaunchVeilOpen==='function')mfLaunchVeilOpen(
        veilHome?'UGA COMMAND':'UGA EXPEDITION',
        veilHome?'Strategic controls are preparing':'Navigation is preparing',
        ()=>finishLaunch(veilHome
          ?'UGA Command did not open β€” tap again to retry'
          :'The UGA expedition did not open β€” tap again to retry'));
      if(typeof toast==='function')toast(entryView==='campaign_hub'
        ?'Opening UGA Command β€” strategic controls are preparing'
        :'Opening the UGA expedition β€” navigation is preparing');
      /* Unlock and play BEFORE the first await. iOS only lets a page start audio

         while it is still inside the user-gesture task; once execution resumes

         after an await that permission is gone, so the confirmation tick for the

         single most important button in the game was silent on exactly the

         platform that ships as a PWA. Keep it synchronous with the tap. */
      initAudio(); sfx('ui');
      /* Give the pressed control and status rail one real paint before same-tab

         navigation starts parsing the separate WebGL document. On phones that

         paint is the difference between a confirmed command and a dead tap. */
      await new Promise(resolve=>requestAnimationFrame(()=>resolve()));
      let present=window.__MF_BUILD_HAS_GALACTIC_EXPLORATION===true,packUrl=URL_;
      try{ if(!present){const r=await fetch(URL_,{method:'HEAD',cache:'no-store'}); present=r.ok;} }
      catch(e){}
      if(!present){
        let packed=null;
        try{packed=typeof mfInstallExplorationPack==='function'&&await mfInstallExplorationPack();}
        catch(e){return finishLaunch('Galactic content could not be prepared on this device');}
        if(!packed||!packed.ok)return finishLaunch('Galactic preview is not installed in this build');
        /* Cached bytes need a same-origin resource mount before navigation. */
        if(packed.openUrl) packUrl=packed.openUrl;
        else return finishLaunch('Galactic pack downloaded β€” War Room stays available in this build');
      }
      /* The isolated module returns through an opaque operation nonce.

         Bind that future request to the profile which actually opened it before

         leaving this document; a URL alone must never be enough to start an RTS

         match. sessionStorage is shared by the two same-origin documents but is

         not portable-save data, so the bridge cannot alter the career schema. */
      const now=Date.now(),profileId=PROFILES&&PROFILES.active;
      const gate=window.MFNewCareerFactionGate;
      let gateState=null;
      try{gateState=gate&&typeof gate.state==='function'?gate.state():null;}catch(e){}
      let ticketReady=false;
      try{
        if(typeof profileId!=='string'||!profileId)throw new Error('Profile unavailable');
        if(typeof commanderRosterSnapshotV1!=='function')throw new Error('Commander roster unavailable');
        const roster=commanderRosterSnapshotV1();
        if(!roster||roster.fingerprint!=='fnv1a32:0aadcd2d'
          ||!roster.commander1ByCampaignFaction||!Array.isArray(roster.commanders))throw new Error('Commander roster stale');
        /* Only the resolved commissioning workflow may put an assignment on

           the bridge. A pass-through profile can still contain the historical

           META Nova quick-pick; treating that default as a choice recreated

           the hidden preselection this ticket boundary is designed to remove. */
        const commissioning={factionId:null,commanderId:null};
        let commissionedReady=false;
        const okAuthority=(factionId,commanderId)=>{
          const row=roster.commanders.find(c=>c&&c.id===commanderId);
          return !!(factionId&&commanderId&&roster.commander1ByCampaignFaction[factionId]===commanderId
            &&row&&row.campaignFactionId===factionId);
        };
        /* AN ESTABLISHED CAREER ALREADY HAS ITS ISSUED COMMANDER.

           The resolved-gate branch below is the only way a commissioning

           assignment used to reach the bridge, and `arm()` DELETES that gate as

           soon as a career has played anything. So every career past its first

           match sent {null,null} forever: the module showed "Hire your first

           commander" permanently, and its HIRE COMMANDER button routed to

           new-career-faction, whose openFromRoute() returns false when the gate

           is not pending β€” rejectMenuRoute then dropped the player on the main

           menu with no way through. A played career has committed to its

           faction, so META.setup.pf is a real choice rather than the historical

           Nova quick-pick this boundary exists to reject, and its Commander 1

           is the commander it was issued. Same roster authority either way; a

           legacy career whose data fails that check simply stays uncommissioned

           instead of breaking entry. */
        const careerPlayed=!!((META.matches|0)>0||(META.standardMatches|0)>0||META.firstPlayed);
        const resolvedGate=!!(gateState&&gateState.armed===true&&gateState.phase==='ready');
        if(gateState&&gateState.pending===false&&gateState.canEnterSpaceCareer===true
          &&(resolvedGate||careerPlayed)){
          const factionId=gateState.factionId,commanderId=gateState.starterCommanderId;
          if(resolvedGate&&!okAuthority(factionId,commanderId))
            throw new Error('Commissioning authority mismatch');
          if(okAuthority(factionId,commanderId)){
            commissioning.factionId=factionId;commissioning.commanderId=commanderId;
            commissionedReady=true;
          }
        }
        const ticket={schemaVersion:2,kind:'MassfrontGalacticEntryV2',profileId,
                      issuedAt:now,expiresAt:now+7*24*60*60*1000,source:'massfront-base',entryView,
                      introRequired:entryView==='system'&&!commissionedReady,
                      commanderRosterSnapshot:roster,commanderRosterFingerprint:roster.fingerprint,
                      commissioning};
        const key='massfront.galactic.entry.v1',text=JSON.stringify(ticket);
        sessionStorage.setItem(key,text);
        const restoredText=sessionStorage.getItem(key),stored=JSON.parse(restoredText||'null');
        ticketReady=!!(profileId&&restoredText===text&&stored&&stored.schemaVersion===2
          &&stored.kind==='MassfrontGalacticEntryV2'&&stored.profileId===profileId
          &&stored.issuedAt===ticket.issuedAt&&stored.expiresAt===ticket.expiresAt
          &&stored.source==='massfront-base'&&stored.entryView===entryView
          &&stored.introRequired===ticket.introRequired
          &&stored.commanderRosterFingerprint==='fnv1a32:0aadcd2d'
          &&stored.commanderRosterSnapshot&&stored.commanderRosterSnapshot.fingerprint===stored.commanderRosterFingerprint
          &&stored.commissioning&&stored.commissioning.factionId===commissioning.factionId
          &&stored.commissioning.commanderId===commissioning.commanderId);
      }catch(e){}
      if(!ticketReady)return finishLaunch('Galactic link could not be secured on this device');
      /* Same tab: the ship and RTS renderers must never compete for WebGL. */
      try{
        if(typeof mfBeginExplorationMountLaunch==='function'&&!(await mfBeginExplorationMountLaunch(packUrl))){
          return finishLaunch('Galactic content is not ready to launch safely');
        }
        if(options&&options.explicitRetry===true&&window.__MF_GALACTIC_BRIDGE
           &&typeof window.__MF_GALACTIC_BRIDGE.clearClassicFallback==='function'){
          window.__MF_GALACTIC_BRIDGE.clearClassicFallback();
        }
        location.href=packUrl;return true;
      }
      catch(e){return finishLaunch('Galactic preview could not be opened');}
    };
    /* boot() is asynchronous while the tail manifest scripts continue loading.

       If the career takeover mounted before wire(), this readiness handshake

       gives it the exact moment the base opener becomes wrappable. If it loads

       later, its ordinary init path sees the function directly. */
    try{window.dispatchEvent(new CustomEvent('massfront:exploration-ready'));}catch(e){}
    /* MAIN MENU DOOR. The strategic layer was reachable only from

       Settings > Gameplay > Return to UGA Headquarters, so the ship, galaxy

       map, survey scanner and expedition contracts never appeared anywhere a

       player looks for a mode. The Settings row stays as a returning shortcut;

       this is the entry. Hidden - not disabled - when the payload is absent so

       a slim install is not advertising content it cannot open. */
    mfExplorationMenuSync=function(){
      const btn=$('ugaBtn');if(!btn)return;
      const on=window.__MF_BUILD_HAS_GALACTIC_EXPLORATION===true
             ||window.__MF_OTA_HAS_GALACTIC_DELIVERY===true;
      btn.style.display=on?'':'none';
      const dot=$('ugaDot');
      if(dot)dot.style.display=on&&!(META&&META.flags&&META.flags.ugaSeen)?'':'none';
    };
    mfBindTap($('ugaBtn'),()=>{
      if(typeof META!=='undefined'&&META){META.flags=META.flags||{};META.flags.ugaSeen=true;metaSave();}
      mfExplorationMenuSync();
      /* UGA COMMAND OPENS IN SPACE. This door used to land on the War Table

         panel, which is DEPLOY MASSFRONT's job β€” two buttons, one destination,

         and the ship the button is named after never appeared. 'system' is the

         existing entry view for exactly this (the career gate's own

         'full-uga-space' step already uses it): the orbital scene with the

         Ark, from which the ship interior, the galaxy map and the War Table

         are all one deliberate step away. */
      if(typeof mfOpenExploration==='function')mfOpenExploration('system',{explicitRetry:true,launchButtonId:'ugaBtn'});
    });
    mfExplorationMenuSync();
  })();

  mfBindTap($('armoryBtn'),()=>{
    initAudio(); sfx('ui');
    renderMetaHead(); renderArmory();
    mfEnsureMenu3D('armoryMenuModel','unit',1,'LIVE CHASSIS');
    showFrontScreen('armory');
  });
  mfBindTap($('armoryBack'),()=>{
    sfx('ui'); renderMetaHead();
    showFrontScreen('startScreen');
  });
  document.querySelectorAll('.tbtn').forEach(b=>{
    b.addEventListener('pointerdown',()=>{
      document.querySelectorAll('.tbtn').forEach(x=>x.classList.remove('on'));
      b.classList.add('on'); curTheme=b.dataset.t; initAudio(); sfx('ui');
      renderMapRow();                          // previews follow the biome palette
      if(typeof renderPlanetRow==='function') renderPlanetRow(); // keep the planet highlight honest
    });
  });
  /* The planet row owns the biome now (see PLANETS in gl.js); the old biome

     strip is gone, so this loop is a live fallback only. If anything re-adds

     .tbtn buttons they still work β€” but they must stay inside the selected

     planet or the picker shows a region the world cannot render. */
  /* Skirmish setup is no longer what the primary button does β€” it is what the

     STANDARD card in the War Room does. Kept as a named function so the War

     Room, the back stack and any future deep link all enter it the same way. */
  window.openPlanetarySetup=(mode)=>{
    /* Campaign / MMO / Co-op are advertised on the War Room but unimplemented.

       Opening the planetary catalogue for them was a trap: a locked card still

       walked into a stub war table that played like a mode. */
    if(mode==='coop'||mode==='mmo'||mode==='campaign'){
      initAudio(); sfx('deny');
      toast((mode==='mmo'?'MMO':mode==='coop'?'CO-OP':'CAMPAIGN')+' is not available yet.');
      return;
    }
    initAudio(); sfx('ui');
    /* Training owns a borrowed ruleset and must never leak into the Standard

       war table. tutorial.js is IIFE-scoped, so this calls its explicit export

       rather than relying on a local declaration magically becoming global. */
    if(mode==='standard'&&typeof trainingMissionActive==='function'&&trainingMissionActive()
       &&typeof cancelTrainingMission==='function') cancelTrainingMission();
    activeWarMode='standard';
    /* A saved plan can name a legacy map (vanguard) or a locked later world.

       Key off the map catalogue, not the biome β€” sibling themes would snap

       Aelos High Shelf (arctic) onto Nordhall. Conquest then clamps the drop. */
    if(typeof mfConquestNormalizeSelection==='function') mfConquestNormalizeSelection();
    else {
      const key=typeof planetForMap==='function'?planetForMap(curMap):planetForTheme(curTheme);
      const P=planetForKey(key);
      let R=P&&P.regions.find(r=>r.id===curRegionId&&r.maps.indexOf(curMap)>=0);
      if(P&&!R){R=P.regions[0];curRegionId=R.id;if(R.maps.length)syncBattlefieldFromMap(theatreMapId(R.maps,'standard'));}
    }
    const h=document.querySelector('#setupScr .setupHead h2');
    if(h)h.textContent='βš” STANDARD WAR TABLE';
    const launch=$('setupStart');
    if(launch){launch.textContent='β–Ά START BATTLE';launch.classList.remove('disabled');}
    renderPlanetRow(); renderMapRow(); renderSpawnPlanner();
    if(typeof renderOps==='function') renderOps();
    showFrontScreen('setupScr'); };
  window.openSkirmishSetup=()=>window.openPlanetarySetup('standard');
  /* The integrated strategic shell is a separate document. Its secured route

     adapter needs one stable, non-rewarding entry point for the base game's

     real mode chooser; reaching into this wire() closure made the existing

     Training / Standard / Campaign cards impossible to open from UGA Command.

     Keep START pointed at the integrated shell. This export is only the return

     lane into the already-authored War Room. */
  window.openWarRoom=()=>{
    /* Very old installs predate the War Room shell entirely (#warScr does not

       exist in their APK), so opening it would dead-end. Fall back to Battle

       Setup β€” where the planet/region picker lives β€” and the button works on

       every shell that has ever shipped. */
    if($('warScr')){
      if(typeof renderWarRoom==='function') renderWarRoom();
      return showFrontScreen('warScr');
    }
    if(typeof openSkirmishSetup==='function'){ openSkirmishSetup(); return true; }
    return false;
  };
  const openLegacyWarRoom=()=>window.openWarRoom();
  /* SAFARI HANDS THE PAGE BACK WITH ITS JAVASCRIPT STILL RUNNING.



     mfExplorationLaunching is a one-shot latch that stops a fast second tap

     from opening a duplicate launch while the first is still painting. It is

     set before same-tab navigation into the module and cleared only by

     finishLaunch, because on the success path this document is gone.



     It is not gone. Safari restores back/forward navigations from bfcache,

     which resumes the JavaScript heap exactly as it was - so the player walks

     into UGA Command, presses Back, and lands on a menu whose latch is still

     true. Every DEPLOY MASSFRONT tap from then on hits the duplicate guard and

     returns silently. The button is not disabled and throws no error, which is

     why it reads as the menu being dead rather than as one stuck boolean.



     pageshow with persisted=true is the only event that fires on a bfcache

     restore; load does not. pagehide clears it too, so a cancelled navigation

     cannot strand the button either. Both also drop the pressed styling, which

     otherwise comes back frozen mid-launch. */
  const mfResetExplorationLaunchLatch=()=>{
    mfExplorationLaunching=false;
    for(const id of ['startBtn','ugaBtn']){
      const btn=$(id);if(!btn)continue;
      btn.classList.remove('is-launching');btn.removeAttribute('aria-busy');
    }
  };
  window.addEventListener('pageshow',e=>{if(e&&e.persisted)mfResetExplorationLaunchLatch();});
  window.addEventListener('pagehide',mfResetExplorationLaunchLatch);
  mfBindTap($('startBtn'),async()=>{
    /* A fast second tap arrives while the first launch is painting its status

       rail. mfOpenExploration intentionally returns false for that duplicate;

       treating it as a real failure opened the legacy War Room underneath the

       in-flight Galactic navigation. The active launch already owns the tap. */
    if(mfExplorationLaunching)return;
    initAudio(); sfx('ui');
    /* This retained dashboard button is a compact access/fallback layer, not a

       second primary flow. It returns to the same stable UGA home as cold

       launch; the player deliberately departs into orbital travel from there.

       Older/slim installations still need their working local War Room. */
    const enabled=window.__MF_BUILD_HAS_GALACTIC_EXPLORATION===true||window.__MF_OTA_HAS_GALACTIC_DELIVERY===true;
    const opened=enabled&&typeof mfOpenExploration==='function'&&await mfOpenExploration('campaign_hub',{explicitRetry:true,launchButtonId:'startBtn'});
    if(!opened)openLegacyWarRoom();
  });
  mfBindTap($('warBack'),()=>{ sfx('ui');
    renderMetaHead(); showFrontScreen('startScreen'); });
  mfBindTap($('setupBack'),()=>{ sfx('ui');
    /* Battle Setup doubles as an authored mission's brief. Backing out of it is

       DECLINING the mission, so the borrowed plan comes back here β€” otherwise

       the player's next ordinary skirmish quietly ran the mission's map, theme,

       difficulty and AI slots, and the menu diorama stayed in its biome. */
    if(typeof storyCampaignRestoreMods==='function'&&storyCampaignRestoreMods()
       &&typeof storyCampaignSyncSetup==='function') storyCampaignSyncSetup();
    if(typeof renderWarRoom==='function') renderWarRoom();
    showFrontScreen('warScr'); });
  mfBindTap($('setupStart'),()=>{
    initAudio();
    const campaignMission=activeWarMode==='campaign'
      &&typeof storyCampaignActiveId!=='undefined'&&!!storyCampaignActiveId;
    if(activeWarMode==='coop'||activeWarMode==='mmo'||(activeWarMode==='campaign'&&!campaignMission)){
      sfx('deny');
      toast((activeWarMode==='mmo'?'MMO':activeWarMode==='coop'?'CO-OP':'CAMPAIGN')+' is not available yet.');
      return;
    }
    /* Standard must be a clean session boundary. The normal setup entry clears

       Training already, but an older OTA shell or a deep-linked setup screen can

       bypass that entry point. Do the inexpensive idempotent cleanup again here

       so KEEL objectives and Training's borrowed rules can never ride into a

       Standard battle. needsTraining does NOT auto-start on this path β€” that

       would replace FIRST CONTACT with KEEL. Only an explicit onboarding

       Training choice may divert START BATTLE into startTrainingMission. */
    const choseTraining=typeof META!=='undefined'&&META.onboarding&&META.onboarding.choice==='training';
    const needTrain=typeof needsTraining==='function'&&needsTraining();
    if(activeWarMode==='standard'&&choseTraining&&needTrain&&typeof resumeTrainingMission==='function'){
      resumeTrainingMission();
      return;
    }
    if(activeWarMode==='standard'&&typeof cancelTrainingMission==='function') cancelTrainingMission();
    /* Training restore rewinds live globals. The buttons still show the plan

       the player just set β€” copy them back before META.setup is written. */
    armMatchSetup();
    const loan=(typeof weeklyMode!=='undefined'&&weeklyMode)||campaignMission;
    const training=typeof trainingMissionActive==='function'&&trainingMissionActive();
    if(!loan&&!training){
      if(typeof isHomeworldMap==='function'&&!isHomeworldMap(curMap)){
        sfx('deny'); toast('DROP WORLDS ARE THE FOUR HOMEWORLDS');
        if(typeof mfConquestNormalizeSelection==='function') mfConquestNormalizeSelection();
        return;
      }
      if(typeof mfConquestMapOpen==='function'&&!mfConquestMapOpen(curMap)){
        sfx('deny'); toast('πŸ”’ SECURE THE PREVIOUS BATTLEFIELD FIRST');
        return;
      }
    }
    /* "Remember loadout" means the PLAYER'S loadout. An authored campaign

       mission borrows every field below for the length of its run, so saving

       here while a preset was on loan wrote the mission's plan over the

       player's own β€” permanently, across restarts. That is how one prologue

       mission authored on theme:'arctic' made the snow biome the saved

       default and the menu diorama came up in it every launch. */
    if(!(typeof storyCampaignPlanBorrowed==='function'&&storyCampaignPlanBorrowed())){
      META.setup={d:difficulty,t:curTheme,m:curMap,f:aiFactionSel,pf:playerFaction,pc:playerCommanderId,bs:battlefieldPreset,pkg:deploymentPackage,
                  g:goalSel,tl:timeLimit,rp:resPace,cr:crateRate,
                  ps:playerStartZone,ais:aiSlots.map(A=>({on:!!A.on,diff:A.diff|0,zone:A.zone,ally:!!A.ally,behavior:aiBehaviorKey(A.behavior)})),
                  df:defenseFocus,inf:infestationOn?1:0}; metaSave();   // remember loadout
    }
    hideFrontScreens(); sfx('ui');
    // terrain generation is heavy at this resolution β€” show it, don't freeze on it
    mfLoadScreenFill();
    $('loadScr').style.display='flex';
    requestAnimationFrame(()=>requestAnimationFrame(()=>{
      applyTheme(); newSkirmish();
      $('loadScr').style.display='none';
      /* hideFrontScreens() measures while the loading screen is still visible,

         so HUD traffic control marks the app as menu-occluded at that instant.

         Recompute after the loader leaves or mfMenuOpen can remain latched. */
      stopAttract();
      if(typeof mfFlowLayout==='function') mfFlowLayout();
    }));
  });
  mfBindNativePress($('spdBtn'),()=>{
    gameSpeed=gameSpeed===1?1.5:gameSpeed===1.5?2:1;
    $('spdBtn').textContent=gameSpeed+'Γ—';
    $('spdBtn').setAttribute('aria-label','Game speed '+gameSpeed+' times. Activate to change speed');
    toast('⏩ Game speed '+gameSpeed+'Γ—'); sfx('ui');
  });
  /* The header is a tap target for Profile, but it now CONTAINS buttons β€” the

     rank badge, the update dot and the mailbox. mfBindTap fires for any

     descendant of the element it is bound to, so without this guard every one

     of those opened its own screen and then had Profile shown on top of it by

     the header's own handler running on the same bubbling pointerup. The

     mailbox looked like it simply did nothing. */
  mfBindTap($('metaHead'),e=>{
    if(e&&e.target&&e.target.closest&&e.target.closest('button')) return;
    initAudio(); sfx('ui'); renderProfile(); showFrontScreen('profileScr');
  });
  mfBindTap($('restartBtn'),returnToMainMenu);
  const goCont=$('goContinueBtn');
  if(goCont&&goCont.dataset.bound!=='1'){
    goCont.dataset.bound='1';
    mfBindTap(goCont,()=>{
      if(goCont.dataset.action==='restart') restartCurrentBattle();
      else if(goCont.dataset.action==='continue'){
        if(typeof mfVictoryContinue==='function') mfVictoryContinue();
        else if(typeof continueToNextMap==='function') continueToNextMap();
      }
    });
  }
  mfBindTap($('menuBtn'),mfOpenPause);
  mfBindTap($('resumeBtn'),mfClosePause);
  mfBindTap($('quitBtn'),()=>{
    const go=()=>returnToMainMenu();
    if(typeof accConfirm==='function') accConfirm('Abandon this operation? The unfinished match grants no mission payout.',go);
    else go();
  });
  mfBindTap($('pauseSettings'),()=>{ openSettings('pause'); });
  mfBindTap($('setBack'),()=>{
    $('settingsScr').style.display='none'; sfx('ui');
    if(settingsFrom==='pause'){
      $('pauseOverlay').style.display='flex';
      mfPauseSetModal(true);
      delete document.body.dataset.frontScreen;
      mfFrontFocusRestore('settingsScr',$('pauseSettings')||$('resumeBtn'));
    }
    else showFrontScreen('startScreen');
  });
  // ---- profile (account) screen ----
  const openProfile=()=>{
    initAudio(); sfx('ui'); renderProfile();
    showFrontScreen('profileScr');
  };
  mfBindTap($('profileBtn'),openProfile);
  /* The rank badge is the only rank UI left on the menu, so it has to lead to

     the screen the rest of the ladder moved to. */
  if($('rankEm')) mfBindTap($('rankEm'),openProfile);
  mfBindTap($('settingsBtn'),()=>{ initAudio(); sfx('ui'); openSettings('menu'); });
  mfBindTap($('updDot'),()=>{
    initAudio(); sfx('ui');
    /* The full Game Version screen is already the detail view. Leaving the

       updater in its menu-sized collapsed state hid the Stable/Preview

       selector and rollback controls even though there was ample room. */
    if(typeof mfLauncherOpenDetails==='function') mfLauncherOpenDetails();
    else {
      if(typeof updOpen!=='undefined') updOpen=true;
      if(typeof renderUpdatePanel==='function') renderUpdatePanel();
      showFrontScreen('updScr');
    }
  });
  mfBindTap($('updBack'),()=>{ sfx('ui');
    if(typeof mfLauncherHandleBack==='function'&&mfLauncherHandleBack())return;
    renderMetaHead(); showFrontScreen('startScreen'); });
  mfBindTap($('inboxBtn'),()=>{
    initAudio(); sfx('ui');
    if(typeof renderInbox==='function') renderInbox();
    if(showFrontScreen('inboxScr'))inboxFromMatch=false;
  });
  mfBindTap($('inboxBack'),()=>{ sfx('ui');
    if(inboxFromMatch||document.body.dataset.frontPopup==='inboxScr'){ inboxFromMatch=false; closeMatchPopup('inboxScr'); return; }
    renderMetaHead(); showFrontScreen('startScreen'); });
  /* In-match the Inbox is an explicit popup route, not a front screen. Close

     every other panel first so it never opens on top of a build menu or a unit

     card; closeMatchPopup returns to the still-running battlefield. */
  mfBindTap($('inboxHudBtn'),()=>{
    initAudio(); sfx('ui');
    if(!showMatchPopup('inboxScr'))return;
    if(typeof closeMenus==='function') closeMenus();
    const uc=$('unitCard'); if(uc) uc.style.display='none';
    if(typeof renderInbox==='function') renderInbox();
    inboxFromMatch=true;
  });
  mfBindTap($('profBack'),()=>{
    renderMetaHead(); sfx('ui'); showFrontScreen('startScreen');
  });
  /* `change` alone loses the name. On a phone the field is committed by the

     player leaving the screen, and the two ways they actually do that β€” the

     hardware Back button, and a tap on BACK that fires from pointerdown before

     the input has blurred β€” neither produces a `change` event. So a commander

     typed their name, went back, and the game had forgotten it. Commit on

     `blur` as well, and see nativeBackTap() for the other half. */
  const profNameCommit=()=>{
    const P=activeProf(), v=$('profName').value.trim();
    if(v&&P&&P.name!==v.slice(0,14)){ P.name=v.slice(0,14); profSave(); renderMetaHead(); renderProfile(); }
  };
  $('profName').addEventListener('change',profNameCommit);
  $('profName').addEventListener('blur',profNameCommit);
  mfBindNativePress($('profNew'),()=>{
    if(PROFILES.list.length>=6){ toast('Profile limit reached (6)'); return; }
    const id='p'+(++PROFILES.seq);
    PROFILES.list.push({id,name:'Commander '+PROFILES.seq,emblem:EMBLEMS[PROFILES.list.length%EMBLEMS.length]});
    profSave(); switchProfile(id); renderProfile(); sfx('level');
    toast('πŸ‘€ New profile created β€” progress now saves here');
  });
  let resetArm=0;
  mfBindTap($('profReset'),()=>{
    if(performance.now()-resetArm>2600){
      resetArm=performance.now();
      toast('⚠ Tap RESET again to wipe this profile\'s progress'); sfx('alarm'); return;
    }
    META={...META_DEF,owned:{},facWins:{},mapWins:{},settings:{...META.settings}};
    metaSave(); applyColor(); renderMetaHead(); renderProfile(); sfx('ui');
    toast('Progress reset for '+activeProf().name);
  });
  let delArm=0;
  mfBindTap($('profDel'),()=>{
    if(PROFILES.list.length<=1){ toast('Cannot delete the last profile'); return; }
    if(performance.now()-delArm>2600){
      delArm=performance.now();
      toast('⚠ Tap DELETE again to remove this profile forever'); sfx('alarm'); return;
    }
    const P=activeProf();
    try{ localStorage.removeItem('massfront_meta_'+P.id); }catch(e){}
    PROFILES.list=PROFILES.list.filter(p=>p.id!==P.id);
    profSave(); switchProfile(PROFILES.list[0].id); renderProfile(); sfx('ui');
    toast('Profile deleted');
  });
  mfBindNativePress($('upBtn'),()=>mfBuildingUpgradePress(false));
  mfBindNativePress($('bp_fire'),()=>{
    if(openBld<0) return;
    const B=blds[openBld];
    if(B.type!=='nova'||B.cool>0) return;
    novaSrc=openBld; aiming=2;
    closeMenus();
    toast('β˜„ NOVA ARMED β€” tap anywhere on the map (or minimap-jump first)');
    sfx('alarm');
  });
  mfBindNativePress($('bp_prio'),()=>{
    if(openBld<0) return;
    const B=blds[openBld];
    B.prio=((B.prio||0)+1)%3;
    renderBldPanel(); sfx('ui');
    toast('🎯 '+BT[B.type].name+' targeting: '+['nearest enemy','air first','strongest first'][B.prio]);
  });
  mfBindNativePress($('bp_up'),()=>mfBuildingUpgradePress(false));
  mfBindTap($('bp_repair'),mfBuildingRepairPress);
  mfBindTap($('bp_sell'),mfBuildingRecyclePress);
  mfBindNativePress($('armyBtn'),()=>{ selectArmy(); });
  if($('idleBuilderBtn')) mfBindNativePress($('idleBuilderBtn'),()=>{ selectIdleBuilders(); });
  // ---- tactics bar: patrol / hold / formation ----
  mfBindNativePress($('patrolBtn'),()=>{togglePatrolPlanner();});
  /* Keep the control alive until finger-up. Hiding it on pointerdown exposed

     the platoon row underneath to the same release, producing a ghost P1/P2

     recall toast immediately after an otherwise successful deployment. */
  mfBindNativePress($('deployBtn'),ev=>{ ev.stopPropagation(); deployCarrier(); },'pointerup');
  mfBindNativePress($('rotL'),()=>{ yawTarget-=Math.PI/4; sfx('ui'); });
  mfBindNativePress($('rotR'),()=>{ yawTarget+=Math.PI/4; sfx('ui'); });
  /* OTA patches can run against an older installed HTML shell.  Create the

     new camera controls when that shell predates them instead of crashing the

     entire input binding pass on a missing element. */
  const ensureCamControl=(id,glyph,label,before)=>{
    let el=$(id);if(el)return el;
    el=document.createElement('button');el.id=id;el.className='cbtn camBtn';
    el.setAttribute('aria-label',label);el.innerHTML='<span class="em">'+glyph+'</span><span>'+label+'</span>';
    const row=$('camRow'),ref=$(before);if(row)row.insertBefore(el,ref||null);return el;
  };
  const zoomInControl=ensureCamControl('zoomIn','οΌ‹','Near','tiltBtn');
  const zoomOutControl=ensureCamControl('zoomOut','βˆ’','Far','rotR');
  mfBindNativePress(zoomInControl,()=>{
    camFollow=-1;if(typeof camUser==='function')camUser();zoomBy(1.28);toast('CAMERA Β· ZOOM IN');sfx('ui');
  });
  mfBindNativePress(zoomOutControl,()=>{
    camFollow=-1;if(typeof camUser==='function')camUser();zoomBy(.78);toast('CAMERA Β· ZOOM OUT');sfx('ui');
  });
  mfBindNativePress($('tiltBtn'),()=>{
    /* Four framing presets: overhead for reading a base layout, through to a

       low chase angle for watching a battle line. */
    pitchIdx=(pitchIdx+1)%4;
    /* Four command-view angles. All of them keep the battlefield readable β€”

       there is deliberately no ground-level option. */
    pitchTarget=[1.49,1.32,1.19,1.07][pitchIdx];
    toast(['β›° View: straight down','β›° View: high','β›° View: standard','β›° View: low'][pitchIdx]);
    sfx('ui');
  });
  mfBindNativePress($('holdBtn'),()=>{ orderHold(); });
  // control groups: tap = recall, hold = save
  for(let n=0;n<4;n++){
    const btn=$('grpBtn'+(n+1)); if(!btn) continue;
    let saveT=0,saved=false,lastTap=0;
    btn.addEventListener('pointerdown',()=>{
      saved=false;
      saveT=setTimeout(()=>{ saved=true; saveGroup(n); },480);
    });
    const fin=()=>{
      clearTimeout(saveT);
      if(!saved&&running){const now=performance.now(),focus=now-lastTap<430;lastTap=now;recallGroup(n,focus);}
      saved=false;
    };
    mfBindNativePress(btn,fin,'pointerup');
    btn.addEventListener('pointercancel',()=>clearTimeout(saveT));
  }
  mfBindNativePress($('formBtn'),()=>{armFormationOrder();});
  mfBindNativePress($('rallyBtn'),()=>{
    if(openBld<0) return;
    const B=blds[openBld];
    if(!B||!B.alive||!(typeof mfLocalOwnsBuilding==='function'?mfLocalOwnsBuilding(B):B.team===0)||!Array.isArray(B.queue))return;
    armRally=openBld;
    closeMenus();
    toast('βš‘ Tap the map to place the rally flag');
    sfx('ui');
  });
  mfBindNativePress($('boxBtn'),()=>{
    boxMode=!boxMode; $('boxBtn').classList.toggle('on',boxMode);
    if(boxMode) toast('⬚ Drag on the map to box-select your units');
  });
  mfBindNativePress($('stopBtn'),()=>{ stopSelected(); });
  mfBindNativePress($('atkAlert'),()=>{ jumpToAlert(); });
  mfBindNativePress($('waveAlert'),()=>{ jumpToWaveWarning(); });
  mfBindNativePress($('moveBtn'),()=>{ toggleMoveMode(); });
  mfBindNativePress($('clearBtn'),()=>{ clearSel(); updateSelInfo(); sfx('ui'); });
  mfBindNativePress($('buildBtn'),()=>{
    if(typeof closeBaseFinder==='function')closeBaseFinder(false);
    if(hudDeck==='buildings')setHudDeck('orders',true);
    if(placing) cancelPlace();                 // never overlap menu with placement mode
    const bm=$('buildMenu');
    if(bm.style.display==='block'){ bm.style.display='none'; }
    else {
      closeMenus(); renderBuildMenu(); bm.style.display='block';          // re-render: unlock states follow CDR level
      flashBuildZone();                                                   // preview HQ/uplink command territory before choosing a structure
    }
    sfx('ui');
  });
  mfBindNativePress($('placeOk'),confirmPlace);
  mfBindNativePress($('placeNo'),()=>{ cancelPlace(); sfx('ui'); });
  mfBindNativePress($('modeBtn'),e=>{ e.preventDefault(); cycleSelectedModes(); });
  mfBindNativePress($('placeRotL'),e=>{ e.preventDefault(); rotatePlacement(-1); });
  mfBindNativePress($('placeRotR'),e=>{ e.preventDefault(); rotatePlacement(1); });
  mfBindNativePress($('repeatBtn'),()=>{
    if(openBld<0) return;
    const B=blds[openBld];
    if(!B||!B.alive||!(typeof mfLocalOwnsBuilding==='function'?mfLocalOwnsBuilding(B):B.team===0)||!Array.isArray(B.queue))return;
    const C=mfMatchConsumer();
    if(C&&C.requiresLockstep()){
      const active=!C.repeatIntent(openBld).active,receipt=C.submitRepeat(openBld,active);
      if(!receipt){toast(C.lastFailure()||'Repeat order could not be sent');return;}
      $('repeatBtn').textContent='REPEAT: '+(active?'ON':'OFF')+' β€” PENDING';
      $('repeatBtn').classList.toggle('on',active);
      $('repeatBtn').setAttribute('aria-pressed',String(active));
      toast(active?'Repeat ON queued for the shared match tick':'Repeat OFF queued β€” existing queued units are retained');
      sfx('ui');return;
    }
    B.repeat=!B.repeat;
    $('repeatBtn').textContent='REPEAT: '+(B.repeat?'ON':'OFF');
    $('repeatBtn').classList.toggle('on',B.repeat);
    $('repeatBtn').setAttribute('aria-pressed',String(B.repeat));
    if(typeof toast==='function')toast(B.repeat?'Repeat enabled β€” completed units return to this queue':
      'Repeat off β€” no new repeats; '+B.queue.length+' queued unit'+(B.queue.length===1?'':'s')+' remaining');
    sfx('ui');
  });
  mfBindNativePress($('abOver'),()=>{ if(aiming===0){aiming=-1;return;} tryAbility(0); });
  mfBindNativePress($('abHeal'),()=>tryAbility(1));
  mfBindNativePress($('abRage'),()=>tryAbility(2));
  mfBindNativePress($('abLance'),()=>tryAbility(3));
  if($('abEmp')) mfBindNativePress($('abEmp'),()=>tryAbility(4));
  if($('abJump')) mfBindNativePress($('abJump'),()=>tryCommanderJump());
  mfBindNativePress($('abBarrage'),()=>tryArtilleryBarrage());
  if($('abClass')) mfBindNativePress($('abClass'),()=>tryClassAbility());
  mfBindNativePress($('abHero'),()=>selectHero());
  if($('baseFindBtn')) mfBindNativePress($('baseFindBtn'),()=>setHudDeck('buildings'));
  const focusCommander=()=>{
    const h=typeof mfLocalCommander==='function'?mfLocalCommander():heroIdx;
    if(h<0||!ualive[h]){toast('Commander is down');sfx('reject');return;}
    selectHero();
    /* selectHero owns selection and camera centring. Reassert the camera target

       after it returns so a stale follow target cannot pull the view away on the

       next frame. */
    camFollow=-1;cam.x=ux[h];cam.y=uy[h];clampCam();camUpdateMatrices();
    toast('β˜… COMMANDER β€” selected and centered');
  };
  mfBindTap($('heroBar'),focusCommander);
  $('heroBar').addEventListener('keydown',ev=>{
    if(ev.key!=='Enter'&&ev.key!==' ') return;
    ev.preventDefault(); focusCommander();
  });
  document.body.addEventListener('pointerdown',initAudio,{once:true});
  setInterval(()=>{
    if(openBld<0||!blds[openBld]) return;
    renderQueue();
    if($('bldMenu2').style.display==='block') renderBldPanel();
    if($('prodMenu').style.display==='block'&&blds[openBld].type==='techlab'&&blds[openBld].res<0) renderResearchMenu();
  },800);
}

// ---------- boot ----------
async function boot(){
  profLoad();                    // user accounts
  metaLoad();                    // active profile's progression + settings
  applyColor();
  applySettings();
  wcChoice=clamp(META.wcPref|0,0,3);
  document.querySelectorAll('.wbtn').forEach(b=>b.classList.toggle('on',+b.dataset.w===wcChoice));
  // restore the last battle loadout
  const su=META.setup||{};
  if(su.d!=null&&su.d>=0&&su.d<=2) difficulty=su.d;
  /* Last loadout may name a legacy map. Keep the eight for training, but the

     career picker starts on a homeworld site. */
  if(su.m&&MAPDEFS[su.m]&&(typeof isHomeworldMap!=='function'||isHomeworldMap(su.m))) curMap=su.m;
  else if(typeof isHomeworldMap==='function'&&!isHomeworldMap(curMap)) curMap='aelos_north_medium';
  const mapDef=MAPDEFS[curMap];
  if(mapDef&&mapDef.theme&&THEMES[mapDef.theme]) curTheme=mapDef.theme;
  else if(THEMES[su.t]) curTheme=su.t;
  /* `grand` was the pre-1.32 name for Large. Keep old setup saves valid while

     storing only the player-facing Compact / Standard / Large vocabulary. */
  if(BATTLEFIELD_PRESETS[su.bs]||su.bs==='grand') battlefieldPreset=battlefieldPresetKey(su.bs);
  /* Old Standard loadouts stored aelos_north_small while preset stayed

     standard. Compact stays compact only when the saved theatre is compact. */
  if(typeof theatreMapId==='function'&&MAPDEFS[curMap]&&MAPDEFS[curMap].size!==battlefieldPresetKey(battlefieldPreset)){
    const pk=typeof planetForMap==='function'?planetForMap(curMap):null;
    const P=pk&&typeof PLANETS!=='undefined'&&PLANETS[pk];
    const R=P&&P.regions.find(r=>r.maps&&r.maps.indexOf(curMap)>=0);
    const hit=R&&theatreMapId(R.maps,battlefieldPreset);
    if(hit&&MAPDEFS[hit]) curMap=hit;
  }
  /* THE REGION HAS TO FOLLOW THE MAP.

     Boot restored the last-played map but left curRegionId at its 'aelos_north'

     declaration default, so a career whose last battle was on Karak/Vespera

     reopened the War Table showing Aelos's region deck with a site selected

     that is not in it. This is also what made a UGA operation's landing site

     disagree with the War Table on return. */
  if(MAPDEFS[curMap]&&MAPDEFS[curMap].region) curRegionId=MAPDEFS[curMap].region;
  deploymentPackage=DEPLOYMENT_PACKAGES[su.pkg]?su.pkg:'expedition';
  /* First-three Standard battles keep the supported landing. Theatre size

     follows the selected site β€” Standard is medium, not Compact or Large. */
  if(assistedOpeningActive()) deploymentPackage='prepared';
  if(su.f&&(su.f==='random'||FACTIONS[su.f])) aiFactionSel=su.f;
  if(su.g&&GOALS.some(g=>g.id===su.g)) goalSel=su.g;
  if(su.tl!=null) timeLimit=+su.tl||0;
  if(su.rp) resPace=+su.rp;
  if(su.cr!=null) crateRate=crateRateBase=+su.cr;
  if(su.inf!=null) infestationOn=!!su.inf;
  if(START_ZONES.some(z=>z.id===su.ps)) playerStartZone=su.ps;
  if(Array.isArray(su.ais)) for(let i=0;i<Math.min(aiSlots.length,su.ais.length);i++){
    const S=su.ais[i]||{};
    aiSlots[i].on=!!S.on;
    aiSlots[i].diff=clamp(S.diff|0,0,2);
    aiSlots[i].ally=!!S.ally;
    aiSlots[i].behavior=typeof aiBehaviorKey==='function'?aiBehaviorKey(S.behavior):'balanced';
    if(START_ZONES.some(z=>z.id===S.zone)) aiSlots[i].zone=S.zone;
  }
  if(!aiSlots.some(A=>A.on)) aiSlots[0].on=true;
  /* Old saves can stack two commanders or park an enemy on NW / a cardinal.

     Reseat lives in normalize (corners, SE second enemy, NW ally) so this

     must not pick n/e/s/w from START_ZONES β€” that is the 1028 m crush. */
  normalizeAiSlotsForBattlefield();
  /* Validate against the PICKER's own list, not against FACTIONS. FACTIONS has

     no 'nova' entry β€” it is the enemy roster β€” so validating a saved player

     faction against it silently rejects the one everybody starts on. It happens

     to land on 'nova' anyway because that is the declared default, which is

     exactly the kind of accident that stops being true the moment the default

     moves. */
  if(su.pf&&typeof playableFactions==='function'&&playableFactions().includes(su.pf)) playerFaction=su.pf;
  else playerFaction='nova';
  if(su.pc&&typeof commanderById==='function'){
    const facKey=typeof commanderFactionKey==='function'?commanderFactionKey(playerFaction):playerFaction;
    const C=commanderById(su.pc),R=typeof COMMANDER_ROSTERS!=='undefined'?COMMANDER_ROSTERS[facKey]||[]:[];
    if(C&&!C.aiOnly&&R.indexOf(C)>=0) playerCommanderId=su.pc;
    else playerCommanderId=((R.find(c=>!c.aiOnly)||(COMMANDER_ROSTERS.nova&&COMMANDER_ROSTERS.nova[0]))||{id:'nova_kai'}).id;
  }
  if(su.df!=null) defenseFocus=su.df?1:0;
  difficulty=Math.max(...activeEnemySlots().map(A=>A.diff));
  document.querySelectorAll('.glbtn').forEach(b=>b.classList.toggle('on',b.dataset.g===goalSel));
  document.querySelectorAll('.tmbtn').forEach(b=>b.classList.toggle('on',+b.dataset.t===timeLimit));
  document.querySelectorAll('.pcbtn').forEach(b=>b.classList.toggle('on',+b.dataset.p===resPace));
  document.querySelectorAll('.crbtn').forEach(b=>b.classList.toggle('on',+b.dataset.c===crateRate));
  document.querySelectorAll('.bsbtn').forEach(b=>b.classList.toggle('on',b.dataset.bs===battlefieldPreset));
  document.querySelectorAll('.pkgbtn').forEach(b=>b.classList.toggle('on',b.dataset.pkg===deploymentPackage));
  const pkh=$('deployPkgHint');if(pkh)pkh.textContent=deploymentPackageDef().ds;
  const bsh=$('battleScaleHint');
  if(bsh) bsh.textContent=battlefieldPresetHint();
  document.querySelectorAll('.ifbtn').forEach(b=>b.classList.toggle('on',!!+b.dataset.i===infestationOn));
  const ifh=$('infestHint'); if(ifh) ifh.textContent=infestationOn
    ?'Neutral nests spread and erupt during the battle.'
    :'No neutral nests, infestation guards, eruptions, spread, or map-wide tides.';
  const gh=$('goalHint'); if(gh) gh.textContent=goalDef().ds;
  const allSame=activeAiSlots().every(A=>A.diff===activeAiSlots()[0].diff);
  document.querySelectorAll('.globalDiff').forEach(b=>b.classList.toggle('on',allSame&&+b.dataset.d===difficulty));
  if(typeof renderPlanetRow==='function') renderPlanetRow();
  document.querySelectorAll('#facRow .fbtn').forEach(b=>b.classList.toggle('on',b.dataset.f===aiFactionSel));
  document.querySelectorAll('#pfacRow .fbtn').forEach(b=>b.classList.toggle('on',b.dataset.f===playerFaction));
  document.querySelectorAll('.dfbtn').forEach(b=>b.classList.toggle('on',+b.dataset.df===defenseFocus));
  const dfh=$('defFocusHint'); if(dfh) dfh.textContent=defenseFocus
    ?'Tower defence: +20% defence HP, +15% damage, +10% range, 25% faster construction β€” enemy waves arrive 18% faster.'
    :'Classic RTS balance between mobile armies and static defences.';
  builtTheme=curTheme; builtMap=curMap; builtTopology=mfWorldTopologyKey();
  renderMetaHead();
  resize();
  await preloadMatAtlases();     // load premade PBR atlases if present
  initGL3D();                    // depth buffer, culling, the lit + additive programs
  if(typeof shieldFxBoot==='function') shieldFxBoot(); // instanced domes, plates and hit ripples
  buildMatAtlas();               // upload premade atlases or generate procedurally
  if(typeof initMaterialV2==='function') initMaterialV2(); // allocates only for ?materiallab=1
  initBillboards();              // sprite layer for smoke, fire and energy
  if(typeof macroFxBoot==='function') macroFxBoot();   // authored one-billboard macro layers
  initModels();                  // procedural geometry for every unit, structure and prop
  atlasTex=buildAtlas();         // (2D atlas retained for HUD icons and the minimap)
  /* Baked unit sheet. loadUnitSheet had NO call site anywhere in the tree, so

     unitTexReady stayed false forever and shatterFrame (sim.js) returned on its

     first line - meaning large units died without their textured shatter, while

     the 1.62 MB sheet shipped in every payload regardless. Both UNIT_ROWS and

     UNIT_SHEET_B64 are already loaded by the manifest; only the call was

     missing, and git shows it arriving with the reconstructed baseline rather

     than being deliberately retired. Async and self-contained: it decodes an

     image and flips unitTexReady when ready, so a slow decode costs nothing at

     boot and simply leaves shatter off until it lands. */
  loadUnitSheet();
  if(typeof mfIconInitGL==='function') mfIconInitGL();   // tactical icon sheet + its batch
  buildDetailTex();
  loadTerrainTextures();   // real tileable ground art (async decode)
  initFloatText();
  setupDeposits();               // must precede terrain (raises land under deposits)
  terrainTex=buildTerrain();
  orthoSpan=distTarget=1500; camPitch=pitchTarget=1.19; camYaw=yawTarget=0.6;
  cam.x=MAP*0.5; cam.y=MAP*0.5; clampCam(); camUpdateMatrices();
  setupDoodads();
  renderBuildMenu();
  const hm=$('abHero').querySelector('.em');       // real commander render on the CDR button
  hm.textContent=''; hm.appendChild(unitIconEl(4,32));
  wire();
  if(typeof initDaily==='function') initDaily();       // daily orders + boosters
  if(typeof initOps==='function') initOps();           // threat ladder + weekly
  if(typeof initDevelop==='function') initDevelop();   // research + crafting
  const showScr=id=>{ showFrontScreen(id); sfx('ui'); };
  mfBindTap($('dailyBtn'),()=>{ renderDaily(); showScr('dailyScr'); });
  mfBindTap($('dailyBack'),()=>{ showFrontScreen('startScreen'); renderMetaHead(); sfx('ui'); });
  /* One purpose, so no tab row: the Dossier is the faction codex. Dispatches

     moved to the mailbox whole, sealed entries included (see renderInbox). */
  mfBindTap($('dossierBtn'),()=>{
    if(typeof renderCodex==='function') renderCodex();
    showScr('dossierScr');
  });
  mfBindTap($('opsBtn'),()=>{ renderOps(); mfEnsureMenu3D('opsMenuModel','building','turret','LIVE DEFENSE'); showScr('opsScr'); });
  mfBindTap($('devBtn'),()=>{ renderDevelop(); mfEnsureMenu3D('devMenuModel','building','techlab','LIVE SYSTEM'); showScr('devScr'); });
  mfBindTap($('devBack'),()=>{ showFrontScreen('startScreen'); renderMetaHead(); sfx('ui'); });
  mfBindTap($('opsBack'),()=>{ showFrontScreen('startScreen'); sfx('ui'); });
  /* One pinned button, two operations. Its label is set by opsSyncGo(); this

     is the matching action, so the CAMPAIGN tab starts a mission and the

     WEEKLY tab starts the weekly, and neither needs its own buried control. */
  mfBindTap($('weeklyGo'),e=>{
    const mode=e.currentTarget&&e.currentTarget.dataset.opsMode;
    if(mode==='campaign'){
      if(typeof storyCampaignStartNext==='function') storyCampaignStartNext();
      else { sfx('deny'); toast('CAMPAIGN systems are still loading.'); }
      return;
    }
    startWeekly();
  });
  /* Weekly no longer has its own menu tile β€” Operations owns the weekly card.

     The listener stays, guarded, so an older shell that still has the button

     keeps working instead of throwing here and killing every wiring below. */
  const wkB=$('weeklyBtn');
  if(wkB) wkB.addEventListener('pointerdown',()=>{ renderOps(); showScr('opsScr');
    setTimeout(()=>{ const w=$('weeklyBox'); if(w) w.scrollIntoView({behavior:'smooth',block:'center'}); },120); });
  mfBindTap($('dossBack'),()=>{ showFrontScreen('startScreen'); sfx('ui'); });
  /* Dismissing a dispatch marks it read, but nothing used to re-render the list

     underneath β€” the corner badge cleared while the row it came from still

     showed a green dot and NEW until you left the screen and came back. Redraw

     the mailbox on dismiss (never from inside showDispatch, which would rebuild

     the list under a live modal). handleNativeBack routes #dispatch here too,

     so hardware Back gets the same fix. */
  mfBindTap($('dispOk'),()=>{ $('dispatch').style.display='none';
    if(typeof storyRefreshBadge==='function') storyRefreshBadge();
    const ib=$('inboxScr');
    if(ib&&ib.style.display!=='none'&&typeof renderInbox==='function') renderInbox();
    sfx('ui'); });
  /* New subsystems. Each lives in its own module and registers its own UI, so

     none of them need to touch index.html or this file beyond this line. */
  for(const fn of ['initOffline','initSampleAudio','initAssetPacks','initAuthPortal','initSocialUI','initTutorial','initAdBoards','initStoreUI','initResTree3D','initEconomyNet','initIntro','initLauncherGateway','initGalaxyUI','initWarPrimer'])
    /* NEVER skip in silence. A function belonging to a file that loads AFTER

       main.js does not exist yet, and this guard used to drop it with no

       error - the feature was simply absent at runtime. That is how the War

       Table lost its galaxy flow three separate times. Defer anything not yet

       defined to the next macrotask, by which point every manifest script has

       run; if it is STILL missing, say so loudly and record it. */
    if(typeof window[fn]==='function'){ try{ window[fn](); }catch(e){ console.error(fn,e); } }
    else (function(name){
      setTimeout(function(){
        if(typeof window[name]==='function'){
          console.warn('init deferred (declared after main.js in the manifest): '+name);
          try{ window[name](); }catch(e){ console.error(name,e); }
        } else {
          (window.__MF_INIT_MISSING=window.__MF_INIT_MISSING||[]).push(name);
          console.error('init function NEVER became available: '+name+' - a feature is silently absent');
        }
      },0);
    })(fn);
  mfRescueHiddenSetupCards();
  /* The auth portal loads its cached AP_SESSION synchronously. Initialise the

     Profile account/save panel afterwards so it renders that real session,

     rather than briefly and incorrectly labelling the player signed out. */
  if(typeof initAccounts==='function') initAccounts();
  initNativeNavigation();
  if(typeof initUpdater==='function') initUpdater();   // start-menu patcher
  mfCpuBind();
  setupAttract();                                      // live 3D behind the menu
  requestAnimationFrame(frame);
}
boot();