File size: 23,504 Bytes
cee43ad
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="vi">
<head>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0">
  <meta http-equiv="Cache-Control" content="no-cache, no-store, must-revalidate">
  <meta http-equiv="Pragma" content="no-cache">
  <meta http-equiv="Expires" content="0">
  <link rel="icon" href="data:image/svg+xml,<svg xmlns='http://www.w3.org/2000/svg' viewBox='0 0 100 100'><rect width='100' height='100' rx='22' fill='%23151515'/><text x='50' y='68' font-size='52' font-family='Arial' font-weight='bold' fill='%23ff5e00' text-anchor='middle'>A</text></svg>">
<title>Đăng nhập - ANIVIETDB</title>
  <link href="https://fonts.googleapis.com/css2?family=Roboto:wght@400;500;700&display=swap" rel="stylesheet">
  <style>
    * { box-sizing: border-box; margin: 0; padding: 0; }
    body {
      font-family: 'Roboto', sans-serif;
      background: linear-gradient(135deg, #0b0d16 0%, #1a1033 50%, #2d1b4e 100%);
      min-height: 100vh;
      display: flex; align-items: center; justify-content: center;
      padding: 20px; color: #e8e8f0;
    }
    body::before {
      content: "";
      position: fixed;
      inset: 0;
      z-index: -2;
      pointer-events: none;
      background-color: #151515;
      background-image:
        url("https://phim.nguonc.com/public/images/Film/76.jpg"),
        url("https://phim.nguonc.com/public/images/Film/666.jpg"),
        url("https://phim.nguonc.com/public/images/Film/77.jpg"),
        url("https://phim.nguonc.com/public/images/Film/667.jpg"),
        url("https://phim.nguonc.com/public/images/Film/259.jpg"),
        url("https://phim.nguonc.com/public/images/Film/2738.jpg"),
        url("https://phim.nguonc.com/public/images/Film/3485.jpg"),
        url("https://phim.nguonc.com/public/images/Film/141438.jpg"),
        url("https://phim.nguonc.com/public/images/Film/date-a-bullet-thumb.jpg"),
        url("https://phim.nguonc.com/public/images/Film/3103.jpg"),
        url("https://phim.nguonc.com/public/images/Post/5/aishiteru-game-wo-owarasetai.jpg"),
        url("https://phim.nguonc.com/public/images/Film/541.jpg"),
        url("https://phim.nguonc.com/public/images/Film/536.jpg"),
        url("https://phim.nguonc.com/public/images/Film/56167.jpg"),
        url("https://phim.nguonc.com/public/images/Film/56643.jpg"),
        url("https://phim.nguonc.com/public/images/Film/538.jpg"),
        url("https://phim.nguonc.com/public/images/Film/540.jpg"),
        url("https://phim.nguonc.com/public/images/Film/60351.jpg"),
        url("https://phim.nguonc.com/public/images/Film/2607.jpg"),
        url("https://phim.nguonc.com/public/images/Film/2661.jpg"),
        url("https://phim.nguonc.com/public/images/Film/5MgMCnslrxQfOfaq6T5ndkLlcCk.jpg"),
        url("https://phim.nguonc.com/public/images/Film/134808.jpg"),
        url("https://phim.nguonc.com/public/images/Film/138379.jpg"),
        url("https://phim.nguonc.com/public/images/Post/9/seishun-buta-yarou-wa-santa-claus-no-yume-wo-minai-1.jpg");
      background-size: 12.5% 33.333%, 12.5% 33.333%, 12.5% 33.333%, 12.5% 33.333%, 12.5% 33.333%, 12.5% 33.333%, 12.5% 33.333%, 12.5% 33.333%, 12.5% 33.333%, 12.5% 33.333%, 12.5% 33.333%, 12.5% 33.333%, 12.5% 33.333%, 12.5% 33.333%, 12.5% 33.333%, 12.5% 33.333%, 12.5% 33.333%, 12.5% 33.333%, 12.5% 33.333%, 12.5% 33.333%, 12.5% 33.333%, 12.5% 33.333%, 12.5% 33.333%, 12.5% 33.333%;
      background-position: 0% 0%, 14.2857% 0%, 28.5714% 0%, 42.8571% 0%, 57.1429% 0%, 71.4286% 0%, 85.7143% 0%, 100% 0%, 0% 50%, 14.2857% 50%, 28.5714% 50%, 42.8571% 50%, 57.1429% 50%, 71.4286% 50%, 85.7143% 50%, 100% 50%, 0% 100%, 14.2857% 100%, 28.5714% 100%, 42.8571% 100%, 57.1429% 100%, 71.4286% 100%, 85.7143% 100%, 100% 100%;
      background-repeat: no-repeat;
      filter: brightness(0.42) saturate(0.65);
    }
    body::after {
      content: "";
      position: fixed;
      inset: 0;
      z-index: -1;
      pointer-events: none;
      background: linear-gradient(180deg, rgba(21,21,21,0.55), rgba(21,21,21,0.78) 68%, #151515 100%),
        radial-gradient(120% 90% at 50% 0%, rgba(21,21,21,0) 42%, rgba(21,21,21,0.5) 100%);
    }
    .auth-card {
      width: 100%; max-width: 400px;
      background: rgba(20, 22, 38, 0.92);
      border: 1px solid rgba(120, 100, 230, 0.35);
      border-radius: 16px;
      padding: 32px 28px;
      box-shadow: 0 20px 50px rgba(0, 0, 0, 0.55);
    }
    .auth-logo {
      text-align: center; font-size: 22px; font-weight: 700; letter-spacing: 0.5px;
      margin-bottom: 6px;
    }
    .auth-logo span { color: #8b7bff; }
    .auth-sub { text-align: center; color: #9a9ab0; font-size: 13px; margin-bottom: 22px; }
    .auth-tabs { display: flex; margin-bottom: 22px; border-bottom: 1px solid rgba(120,100,230,0.25); }
    .auth-tab {
      flex: 1; text-align: center; padding: 10px 0; cursor: pointer;
      color: #9a9ab0; font-weight: 500; border-bottom: 2px solid transparent; transition: all .2s;
    }
    .auth-tab.active { color: #fff; border-bottom-color: #8b7bff; }
    .field { margin-bottom: 16px; }
    .field label { display: block; font-size: 12px; color: #a9a9c4; margin-bottom: 6px; }
    .field input {
      width: 100%; padding: 12px 14px; border-radius: 10px;
      border: 1px solid rgba(120,100,230,0.3); background: #10121f;
      color: #fff; font-size: 14px; outline: none; transition: border .2s;
    }
    .field input:focus { border-color: #8b7bff; }
    .auth-btn {
      width: 100%; padding: 13px; border: none; border-radius: 10px;
      background: linear-gradient(135deg, #7c5cff, #5a3bd6);
      color: #fff; font-size: 15px; font-weight: 700; cursor: pointer;
      transition: transform .15s, filter .15s;
    }
    .auth-btn:hover { filter: brightness(1.1); transform: translateY(-1px); }
    .auth-btn:disabled { opacity: 0.6; cursor: not-allowed; }
    .auth-error {
      min-height: 20px; color: #ff8a8a; font-size: 13px; margin-top: 12px; text-align: center;
    }
    .auth-ok {
      color: #7ee787; font-size: 13px; text-align: center; margin-top: 12px; min-height: 20px;
    }
    .logout-row { display: flex; align-items: center; justify-content: space-between; margin-top: 18px; }    .logged-in { text-align: center; padding: 10px 0; }
    .logged-in .name { font-size: 18px; font-weight: 700; margin-bottom: 4px; }
    .logged-in .email { font-size: 13px; color: #9a9ab0; margin-bottom: 18px; }
    .logged-title {
      text-align: center; font-size: 13px; color: #7ee787; font-weight: 500; margin-bottom: 18px;
    }
    .btn-ghost {
      width: 100%; padding: 12px; border-radius: 10px;
      border: 1px solid rgba(255,138,138,0.4); background: transparent;
      color: #ff8a8a; font-size: 14px; font-weight: 600; cursor: pointer;
    }
    .btn-ghost:hover { background: rgba(255,138,138,0.08); }
    .btn-admin {
      display: block; width: 100%; padding: 12px; border-radius: 10px;
      border: 1px solid rgba(126,231,135,0.45); background: rgba(126,231,135,0.08);
      color: #7ee787; font-size: 14px; font-weight: 700; cursor: pointer;
      text-align: center; text-decoration: none;
    }
    .btn-admin:hover { background: rgba(126,231,135,0.18); }
    .btn-home {
      display: block; width: 100%; padding: 12px; border-radius: 10px;
      border: 1px solid rgba(139,123,255,0.45); background: rgba(139,123,255,0.1);
      color: #b7abff; font-size: 14px; font-weight: 700; cursor: pointer;
      text-align: center; text-decoration: none; margin-bottom: 10px;
    }
    .btn-home:hover { background: rgba(139,123,255,0.2); }
    .btn-shop {
      display: block; width: 100%; padding: 12px; border-radius: 10px;
      border: 1px solid rgba(255,94,0,0.5); background: rgba(255,94,0,0.1);
      color: #ff9a5a; font-size: 14px; font-weight: 700; cursor: pointer;
      text-align: center; text-decoration: none; margin-bottom: 10px;
    }
    .btn-shop:hover { background: rgba(255,94,0,0.2); }
    .auth-back {
      display: block; text-align: center; margin-top: 18px;
      color: #9a9ab0; font-size: 13px; text-decoration: none;
    }
    .auth-back:hover { color: #fff; text-decoration: underline; }
    .auth-copyright {
      text-align: center; margin-top: 20px; padding-top: 14px;
      border-top: 1px solid rgba(120,100,230,0.15);
      color: #6d6d85; font-size: 12px;
    }
    .terms-row {
      display: flex; align-items: flex-start; gap: 8px;
      font-size: 12px; color: #a9a9c4; line-height: 1.5; margin-bottom: 16px;
    }
    .terms-row input { margin-top: 2px; accent-color: #8b7bff; }
    .turnstile-wrap { display: flex; justify-content: center; margin-bottom: 16px; }
    .terms-row a { color: #8b7bff; text-decoration: none; }
    .terms-row a:hover { text-decoration: underline; }
    .terms-modal {
      position: fixed; inset: 0; z-index: 1000;
      display: none; align-items: center; justify-content: center;
      background: rgba(0,0,0,0.6); padding: 20px;
    }
    .terms-modal.open { display: flex; }
    .terms-modal-card {
      width: 100%; max-width: 520px; max-height: 80vh;
      background: #141622; border: 1px solid rgba(120,100,230,0.35);
      border-radius: 14px; display: flex; flex-direction: column; overflow: hidden;
    }
    .terms-modal-head {
      display: flex; align-items: center; justify-content: space-between;
      padding: 14px 18px; border-bottom: 1px solid rgba(120,100,230,0.25);
    }
    .terms-modal-head h3 { font-size: 16px; color: #fff; }
    .terms-modal-close {
      background: none; border: none; color: #9a9ab0; font-size: 20px;
      cursor: pointer; line-height: 1;
    }
    .terms-modal-close:hover { color: #fff; }
    .terms-modal-body { padding: 16px 18px; overflow-y: auto; font-size: 13px; line-height: 1.7; color: #c6c6da; }
    .terms-modal-body h4 { color: #fff; font-size: 14px; margin: 14px 0 6px; }
    .terms-modal-body h4:first-child { margin-top: 0; }
    .terms-modal-foot {
      padding: 12px 18px; border-top: 1px solid rgba(120,100,230,0.25); text-align: right;
    }
    .terms-modal-foot button {
      padding: 9px 18px; border: none; border-radius: 8px;
      background: linear-gradient(135deg, #7c5cff, #5a3bd6); color: #fff;
      font-size: 13px; font-weight: 700; cursor: pointer;
    }
  </style>
</head>
<body>
  <div class="auth-card">
    <div class="auth-logo">ANIVIET<span>DB</span></div>
    <p class="auth-sub">Đăng nhập hoặc tạo tài khoản</p>

    <div class="auth-tabs" id="authTabs">
      <div class="auth-tab active" data-mode="login">Đăng nhập</div>
      <div class="auth-tab" data-mode="register">Đăng ký</div>
    </div>

    <div id="authForms">
      <div class="field">
        <label id="loginLabel">Email hoặc tên đăng nhập</label>
        <input type="text" id="email" placeholder="Email hoặc tên đăng nhập" autocomplete="username">
      </div>
      <div class="field register-only" style="display:none;">
        <label>Tên đăng nhập</label>
        <input type="text" id="name" placeholder="Tên của bạn" autocomplete="name">
      </div>
      <div class="field">
        <label>Mật khẩu</label>
        <input type="password" id="password" placeholder="••••••••" autocomplete="current-password">
      </div>
      <div class="field register-only" style="display:none;">
        <label>Nhập lại mật khẩu</label>
        <input type="password" id="password2" placeholder="••••••••" autocomplete="new-password">
      </div>
      <label class="terms-row register-only" style="display:none;">
        <input type="checkbox" id="termsCheck">
        <span>Tôi đã đọc và đồng ý với <a href="javascript:void(0)" id="termsLink">Điều khoản sử dụng</a></span>
      </label>
      <div class="turnstile-wrap">
        <div id="turnstile-login" class="cf-turnstile login-only"
             data-sitekey="0x4AAAAAAEQz9I-TXTQoVuii"
             data-action="login"
             data-callback="onTurnstileSuccess"
             data-expired-callback="onTurnstileExpired"
             data-error-callback="onTurnstileError"></div>
        <div id="turnstile-register" class="cf-turnstile register-only" style="display:none;"
             data-sitekey="0x4AAAAAAEQz9I-TXTQoVuii"
             data-action="register"
             data-callback="onTurnstileSuccess"
             data-expired-callback="onTurnstileExpired"
             data-error-callback="onTurnstileError"></div>
      </div>
      <button class="auth-btn" id="submitBtn">Đăng nhập</button>
    </div>

    <div id="loggedIn" style="display:none;">
      <div class="logged-in">
        <div class="name" id="userName"></div>
        <div class="email" id="userEmail"></div>
        <div class="logged-title">Bạn đã đăng nhập</div>
      </div>
      <div style="display:none; margin-bottom:10px;" id="adminPanelWrap">
        <a href="control-panel.html" class="btn-admin">Control Panel</a>
      </div>
      <a href="sfw/index.html" class="btn-home">Trang chủ</a>
      <a href="shop/index.html" class="btn-shop">&#128722; Cửa hàng</a>
      <button class="btn-ghost" id="logoutBtn">Đăng xuất</button>
    </div>

    <div class="auth-error" id="error"></div>
    <div class="auth-ok" id="ok"></div>
    <a href="index.html" class="auth-back" id="authBack">&larr; Về trang chủ</a>
    <div class="auth-copyright">© 2026 ANIVIETDB. All rights reserved.</div>
  </div>

  <div class="terms-modal" id="termsModal">
    <div class="terms-modal-card">
      <div class="terms-modal-head">
        <h3>Điều khoản sử dụng</h3>
        <button class="terms-modal-close" id="termsClose" aria-label="Đóng">&times;</button>
      </div>
      <div class="terms-modal-body">
        <h4>1. Chấp nhận điều khoản</h4>
        <p>Bằng việc truy cập và sử dụng ANIVIETDB, bạn xác nhận đã đọc, hiểu và đồng ý tuân thủ các điều khoản dưới đây. Nếu không đồng ý, vui lòng ngừng sử dụng dịch vụ.</p>
        <h4>2. Mục đích sử dụng</h4>
        <p>ANIVIETDB là nền tảng tổng hợp thông tin anime, manga, manhwa, manhua và light novel phục vụ nhu cầu tìm kiếm, theo dõi và giải trí cá nhân của người dùng. Không được dùng thông tin tại đây cho mục đích thương mại hoặc trái pháp luật.</p>
        <h4>3. Tài khoản người dùng</h4>
        <p>Bạn chịu trách nhiệm bảo mật mật khẩu và mọi hoạt động diễn ra trên tài khoản của mình. Không được tạo tài khoản giả mạo, mạo danh người khác, hoặc lợi dụng tài khoản để gây ảnh hưởng tiêu cực tới cộng đồng.</p>
        <h4>4. Nội dung và bản quyền</h4>
        <p>Toàn bộ thông tin trên ANIVIETDB chỉ mang tính tham khảo. Quyền sở hữu trí tuệ của các tác phẩm thuộc về tác giả và đơn vị phát hành. Nếu bạn là chủ sở hữu và muốn yêu cầu gỡ nội dung, vui lòng liên hệ ban quản trị.</p>
        <h4>5. Hành vi bị cấm</h4>
        <p>Cấm spam, phát tán thông tin sai lệch, phần mềm độc hại, hoặc thực hiện hành vi xâm phạm hệ thống và tài khoản người dùng khác trên nền tảng.</p>
        <h4>6. Từ chối trách nhiệm</h4>
        <p>ANIVIETDB nỗ lực cập nhật thông tin chính xác nhưng không đảm bảo tính đầy đủ, kịp thời của dữ liệu. Website không chịu trách nhiệm về bất kỳ thiệt hại phát sinh từ việc sử dụng dịch vụ.</p>
        <h4>7. Thay đổi điều khoản</h4>
        <p>ANIVIETDB có quyền cập nhật điều khoản bất cứ lúc nào. Việc tiếp tục sử dụng website sau khi có thay đổi đồng nghĩa với việc bạn chấp nhận điều khoản mới.</p>
      </div>
      <div class="terms-modal-foot">
        <button id="termsAgree">Tôi đồng ý</button>
      </div>
    </div>
  </div>

  <script src="https://challenges.cloudflare.com/turnstile/v0/api.js" async defer></script>
  <script src="signup-loginsystem.js?v=8"></script>
  <script>
    var mode = 'login';
    var emailEl = document.getElementById('email');
    var nameEl = document.getElementById('name');
    var passEl = document.getElementById('password');
    var pass2El = document.getElementById('password2');
    var submitBtn = document.getElementById('submitBtn');
    var errorEl = document.getElementById('error');
    var okEl = document.getElementById('ok');
    var authForms = document.getElementById('authForms');
    var loggedIn = document.getElementById('loggedIn');

    var cooldownTimer = null;
    function updateCooldown() {
      var wait = window.AuthSystem.cooldownRemaining();
      if (wait <= 0) {
        if (cooldownTimer) { clearInterval(cooldownTimer); cooldownTimer = null; }
        errorEl.textContent = '';
        submitBtn.disabled = false;
        return;
      }
      errorEl.textContent = 'Vui lòng đợi ' + Math.ceil(wait / 1000) + ' giây trước khi đăng nhập hoặc tạo tài khoản.';
      submitBtn.disabled = true;
    }
    function startCooldown() {
      if (cooldownTimer) { clearInterval(cooldownTimer); }
      cooldownTimer = setInterval(updateCooldown, 200);
      updateCooldown();
    }

    function showSession(user) {
      authForms.style.display = 'none';
      document.getElementById('authTabs').style.display = 'none';
      document.querySelector('.auth-sub').style.display = 'none';
      document.getElementById('authBack').style.display = 'none';
      loggedIn.style.display = 'block';
      document.getElementById('userName').textContent = user.username || user.name || '';
      document.getElementById('userEmail').textContent = user.email || '';
      errorEl.textContent = '';
      okEl.textContent = 'Đã đăng nhập.';
      var isAdmin = user.role === 'admin';
      document.getElementById('adminPanelWrap').style.display = isAdmin ? 'block' : 'none';
    }

    function resetWidget() {
      var el = activeWidget();
      if (el && window.turnstile && typeof window.turnstile.reset === 'function') {
        try { window.turnstile.reset(el); } catch (e) { /* ignore */ }
      }
      submitBtn.disabled = true;
    }

    function showError(msg) {
      okEl.textContent = '';
      errorEl.textContent = msg;
      if (window.turnstile && !turnstileSolved()) submitBtn.disabled = true;
      else submitBtn.disabled = false;
    }

    function setMode(m) {
      mode = m;
      document.querySelectorAll('.auth-tab').forEach(function (t) {
        t.classList.toggle('active', t.getAttribute('data-mode') === m);
      });
      document.querySelectorAll('.register-only').forEach(function (el) {
        el.style.display = (m === 'register') ? '' : 'none';
      });
      document.querySelectorAll('.login-only').forEach(function (el) {
        el.style.display = (m === 'login') ? '' : 'none';
      });
      submitBtn.textContent = (m === 'register') ? 'Tạo tài khoản' : 'Đăng nhập';
      errorEl.textContent = '';
      okEl.textContent = '';
      if (window.turnstile && !turnstileSolved()) submitBtn.disabled = true;
    }

    function activeWidget() {
      return mode === 'register'
        ? document.getElementById('turnstile-register')
        : document.getElementById('turnstile-login');
    }

    function turnstileSolved() {
      var el = activeWidget();
      return !!(window.turnstile && el && window.turnstile.getResponse(el));
    }

    function onTurnstileSuccess() { submitBtn.disabled = false; }
    function onTurnstileExpired() { submitBtn.disabled = true; }
    function onTurnstileError() { submitBtn.disabled = true; }

    var termsModal = document.getElementById('termsModal');
    var termsCheck = document.getElementById('termsCheck');
    document.getElementById('termsLink').addEventListener('click', function () {
      termsModal.classList.add('open');
    });
    document.getElementById('termsClose').addEventListener('click', function () {
      termsModal.classList.remove('open');
    });
    document.getElementById('termsAgree').addEventListener('click', function () {
      termsCheck.checked = true;
      termsModal.classList.remove('open');
    });
    termsModal.addEventListener('click', function (e) {
      if (e.target === termsModal) termsModal.classList.remove('open');
    });

    document.querySelectorAll('.auth-tab').forEach(function (t) {
      t.addEventListener('click', function () { setMode(t.getAttribute('data-mode')); });
    });

    function doSubmit() {
      if (!window.AuthSystem || !window.AuthSystem.isConfigured()) {
        showError('Backend chưa được cấu hình. Vui lòng dán URL API.');
        return;
      }
      var wait = window.AuthSystem.cooldownRemaining();
      if (wait > 0) {
        showError('Vui lòng đợi ' + Math.ceil(wait / 1000) + ' giây trước khi đăng nhập hoặc tạo tài khoản.');
        return;
      }
      var loginId = emailEl.value.trim();
      var password = passEl.value;
      if (!loginId || !password) { showError('Vui lòng nhập email/tên đăng nhập và mật khẩu.'); return; }
      if (window.turnstile && !turnstileSolved()) {
        showError('Vui lòng hoàn tất xác minh "Tôi không phải người máy".');
        return;
      }

      if (mode === 'register') {
        var name = nameEl.value.trim();
        if (!name) { showError('Vui lòng nhập tên đăng nhập.'); return; }
        if (!/^[^\s@]+@[^\s@]+\.[^\s@]+$/.test(loginId)) { showError('Email không hợp lệ.'); return; }
        if (password.length < 6) { showError('Mật khẩu phải có ít nhất 6 ký tự.'); return; }
        if (password !== pass2El.value) { showError('Mật khẩu nhập lại không khớp.'); return; }
        if (!termsCheck.checked) { showError('Vui lòng đồng ý với Điều khoản sử dụng.'); return; }
        submitBtn.disabled = true;
        AuthSystem.register(loginId, name, password).then(function (data) {
          showSession(data.user);
          setTimeout(function () { window.location.href = 'sfw/'; }, 1200);
        }).catch(function (e) { resetWidget(); showError(e.message); });
      } else {
        submitBtn.disabled = true;
        AuthSystem.login(loginId, password).then(function (data) {
          showSession(data.user);
          setTimeout(function () {
            window.location.href = (data.user && data.user.role === 'admin') ? 'control-panel.html' : 'sfw/';
          }, 1200);
        }).catch(function (e) { resetWidget(); showError(e.message); });
      }
    }

    submitBtn.addEventListener('click', doSubmit);
    document.addEventListener('keydown', function (e) { if (e.key === 'Enter') doSubmit(); });
    document.getElementById('logoutBtn').addEventListener('click', function () {
      AuthSystem.logout().then(function () {
        loggedIn.style.display = 'none';
        authForms.style.display = '';
        document.getElementById('authTabs').style.display = '';
        document.querySelector('.auth-sub').style.display = '';
        document.getElementById('authBack').style.display = '';
        setMode('login');
        startCooldown();
      });
    });

    if (window.AuthSystem.cooldownRemaining() > 0) startCooldown();
    window.AuthSystem.me().then(function (user) { if (user) showSession(user); });
  </script>
</body>
</html>