File size: 23,499 Bytes
2c7a564
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="zh-TW">
<head>
  <meta charset="UTF-8" />
  <meta name="viewport" content="width=device-width, initial-scale=1.0" />
  <title>ไปŠๅฝฉ539 ้ ๆธฌๅ‘ฝไธญ็އๅ›žๆธฌๅ ฑๅ‘Š</title>
  <meta name="description" content="ไปŠๅฝฉ539 AI ้ ๆธฌๅ‘ฝไธญ็އๅ›žๆธฌ๏ผŒๅฐ่ฟ‘1000ๆœŸๆญทๅฒ้–‹็Ž็ตๆžœ้€ฒ่กŒ้ ๆธฌ็ฒพๆบ–ๅบฆ้ฉ—่ญ‰ใ€‚" />
  <link rel="preconnect" href="https://fonts.googleapis.com" />
  <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin />
  <link href="https://fonts.googleapis.com/css2?family=Outfit:wght@300;400;500;600;700;800;900&display=swap" rel="stylesheet" />
  <link rel="stylesheet" href="style.css" />
  <style>
    /* โ”€โ”€โ”€ Backtest-specific styles โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€ */
    .kpi-grid {
      display: grid;
      grid-template-columns: repeat(auto-fit, minmax(180px, 1fr));
      gap: 16px;
      margin-bottom: 0;
    }
    .kpi-card {
      background: var(--bg-glass);
      border: 1px solid var(--border-glass);
      border-radius: var(--radius-md);
      padding: 20px;
      text-align: center;
      transition: all 0.3s;
    }
    .kpi-card:hover { border-color: var(--border-glass-hover); transform: translateY(-2px); }
    .kpi-value {
      font-size: 2.2rem; font-weight: 800; line-height: 1;
      background: linear-gradient(135deg, var(--purple-400), var(--cyan-400));
      -webkit-background-clip: text; -webkit-text-fill-color: transparent; background-clip: text;
    }
    .kpi-value.hot-val {
      background: linear-gradient(135deg, var(--gold-400), var(--red-400));
      -webkit-background-clip: text; -webkit-text-fill-color: transparent; background-clip: text;
    }
    .kpi-label { font-size: 0.75rem; color: var(--text-muted); margin-top: 6px; text-transform: uppercase; letter-spacing: 0.06em; }
    .kpi-sub { font-size: 0.85rem; color: var(--text-secondary); margin-top: 4px; font-weight: 600; }

    /* Distribution bars */
    .dist-grid { display: flex; flex-direction: column; gap: 12px; }
    .dist-row { display: flex; align-items: center; gap: 12px; }
    .dist-label { width: 48px; text-align: right; font-weight: 700; font-size: 0.9rem; color: var(--text-secondary); flex-shrink: 0; }
    .dist-bar-wrap { flex: 1; height: 28px; background: rgba(255,255,255,0.04); border-radius: var(--radius-full); overflow: hidden; position: relative; }
    .dist-bar {
      height: 100%; border-radius: var(--radius-full);
      transition: width 1s cubic-bezier(0.25, 0.46, 0.45, 0.94);
      display: flex; align-items: center; padding-left: 10px;
      font-size: 0.75rem; font-weight: 700; color: white;
      white-space: nowrap;
    }
    .dist-count { width: 60px; text-align: right; font-size: 0.8rem; color: var(--text-secondary); flex-shrink: 0; }

    /* Hit colors per level */
    .hit-5 { background: linear-gradient(90deg, #f59e0b, #ef4444); }
    .hit-4 { background: linear-gradient(90deg, #f97316, #dc2626); }
    .hit-3 { background: linear-gradient(90deg, #8b5cf6, #ec4899); }
    .hit-2 { background: linear-gradient(90deg, #3b82f6, #6366f1); }
    .hit-1 { background: linear-gradient(90deg, #0891b2, #3b82f6); }
    .hit-0 { background: rgba(255,255,255,0.08); }

    /* Table region */
    .hit-badge {
      display: inline-block; padding: 3px 10px; border-radius: var(--radius-full);
      font-size: 0.75rem; font-weight: 700; white-space: nowrap;
    }
    .hb-0 { background: rgba(255,255,255,0.08); color: var(--text-muted); }
    .hb-1 { background: rgba(8,145,178,0.2); color: #67e8f9; border: 1px solid rgba(8,145,178,0.3); }
    .hb-2 { background: rgba(99,102,241,0.2); color: #a5b4fc; border: 1px solid rgba(99,102,241,0.3); }
    .hb-3 { background: rgba(139,92,246,0.3); color: #c4b5fd; border: 1px solid rgba(139,92,246,0.5); }
    .hb-4 { background: rgba(249,115,22,0.25); color: #fdba74; border: 1px solid rgba(249,115,22,0.4); }
    .hb-5 { background: rgba(251,191,36,0.3); color: #fde68a; border: 1px solid rgba(251,191,36,0.5); box-shadow: 0 0 12px rgba(251,191,36,0.3); }

    .num-mini {
      display: inline-flex; align-items: center; justify-content: center;
      width: 28px; height: 28px; border-radius: 50%;
      font-size: 0.75rem; font-weight: 700;
      border: 1px solid rgba(139,92,246,0.3);
      background: rgba(124,58,237,0.15);
      color: var(--purple-400);
    }
    .num-mini.hit {
      background: radial-gradient(circle at 30% 30%, rgba(255,255,255,0.2), transparent 60%), var(--gradient-hot);
      border-color: rgba(239,68,68,0.5);
      color: white;
      box-shadow: 0 0 8px rgba(239,68,68,0.4);
    }

    .nav-back { display: inline-flex; align-items: center; gap: 6px; color: var(--purple-400); text-decoration: none; font-size: 0.85rem; font-weight: 600; margin-bottom: 4px; }
    .nav-back:hover { color: var(--indigo-400); }

    /* Filter controls */
    .filter-bar { display: flex; gap: 8px; align-items: center; flex-wrap: wrap; }
    .filter-chip {
      padding: 5px 14px; border-radius: var(--radius-full);
      font-family: var(--font); font-size: 0.78rem; font-weight: 600;
      border: 1px solid var(--border-glass); background: var(--bg-glass);
      color: var(--text-secondary); cursor: pointer; transition: all 0.2s;
    }
    .filter-chip:hover { color: var(--text-primary); border-color: var(--border-glass-hover); }
    .filter-chip.active { background: rgba(124,58,237,0.25); border-color: rgba(124,58,237,0.5); color: var(--purple-400); }

    /* Chart #2 */
    .trend-canvas-wrap { width: 100%; height: 260px; }
    #trendChart { width: 100% !important; height: 100% !important; }
  </style>
</head>
<body>
  <div class="bg-particles" id="bgParticles"></div>

  <header class="site-header">
    <div class="header-inner">
      <div class="logo">
        <a href="index.html" class="nav-back">โ† ่ฟ”ๅ›ž้ ๆธฌ</a>
        <span class="logo-icon" style="margin-left:12px">๐Ÿ“Š</span>
        <div class="logo-text">
          <h1>ๅ‘ฝไธญ็އๅ›žๆธฌๅ ฑๅ‘Š</h1>
          <span id="headerSubtitle">่ผ‰ๅ…ฅไธญ...</span>
        </div>
      </div>
      <div id="headerMeta" class="header-meta">
        <div class="meta-item"><span class="meta-label">ๅ›žๆธฌๆœŸๆ•ธ</span><span class="meta-value" id="metaN">โ€”</span></div>
        <div class="meta-item"><span class="meta-label">ๅ›žๆธฌ็ฏ„ๅœ</span><span class="meta-value" id="metaRange">โ€”</span></div>
        <div class="meta-item"><span class="meta-label">ๆ•ด้ซ”ๅ‘ฝไธญ็އ</span><span class="meta-value" id="metaRate">โ€”</span></div>
      </div>
    </div>
  </header>

  <main class="main-content">

    <!-- KPI Cards -->
    <section class="section">
      <div class="kpi-grid" id="kpiGrid">
        <div class="kpi-card"><div class="kpi-value" id="kpiRate">โ€”</div><div class="kpi-label">ๆ•ด้ซ”่™Ÿ็ขผๅ‘ฝไธญ็އ</div><div class="kpi-sub">ๆฏๆœŸ5ๅ€‹่™Ÿ็ขผไธญๅนณๅ‡ๅ‘ฝไธญๆ•ธ</div></div>
        <div class="kpi-card"><div class="kpi-value" id="kpi1">โ€”</div><div class="kpi-label">่‡ณๅฐ‘ๅ‘ฝไธญ 1 ๅ€‹</div><div class="kpi-sub" id="kpi1n">โ€”</div></div>
        <div class="kpi-card"><div class="kpi-value" id="kpi2">โ€”</div><div class="kpi-label">่‡ณๅฐ‘ๅ‘ฝไธญ 2 ๅ€‹</div><div class="kpi-sub" id="kpi2n">โ€”</div></div>
        <div class="kpi-card"><div class="kpi-value" id="kpi3">โ€”</div><div class="kpi-label">่‡ณๅฐ‘ๅ‘ฝไธญ 3 ๅ€‹</div><div class="kpi-sub" id="kpi3n">โ€”</div></div>
        <div class="kpi-card"><div class="kpi-value hot-val" id="kpi5">โ€”</div><div class="kpi-label">ๅฎŒๅ…จๅ‘ฝไธญ 5/5 ๐ŸŽ‰</div><div class="kpi-sub" id="kpi5n">โ€”</div></div>
      </div>
    </section>

    <!-- Distribution + Trend charts -->
    <section class="section">
      <div style="display:grid;grid-template-columns:1fr 1fr;gap:24px;flex-wrap:wrap" id="chartsRow">
        <div class="glass-card">
          <div class="section-header"><h2 class="section-title">๐ŸŽฏ ๅ‘ฝไธญๆ•ธๅˆ†ไฝˆ</h2></div>
          <div class="dist-grid" id="distGrid"></div>
        </div>
        <div class="glass-card">
          <div class="section-header"><h2 class="section-title">๐Ÿ“ˆ ๅ‘ฝไธญ่ถจๅ‹ข (ๆฏ100ๆœŸ)</h2></div>
          <div class="trend-canvas-wrap"><canvas id="trendChart"></canvas></div>
        </div>
      </div>
    </section>

    <!-- Detailed Results Table -->
    <section class="section">
      <div class="glass-card">
        <div class="section-header">
          <h2 class="section-title">๐Ÿ“‹ ้€ๆœŸๅ›žๆธฌ่ฉณๆƒ…</h2>
          <div class="history-controls">
            <div class="filter-bar" id="filterBar">
              <span style="font-size:0.8rem;color:var(--text-muted)">็ฏฉ้ธๅ‘ฝไธญๆ•ธ๏ผš</span>
              <button class="filter-chip active" onclick="setFilter('all', this)">ๅ…จ้ƒจ</button>
              <button class="filter-chip" onclick="setFilter(3, this)">โ‰ฅ3 ๅ‘ฝไธญ</button>
              <button class="filter-chip" onclick="setFilter(2, this)">โ‰ฅ2 ๅ‘ฝไธญ</button>
              <button class="filter-chip" onclick="setFilter(1, this)">โ‰ฅ1 ๅ‘ฝไธญ</button>
              <button class="filter-chip" onclick="setFilter(0, this)">0 ๅ‘ฝไธญ</button>
            </div>
            <span class="result-count" id="btResultCount"></span>
          </div>
        </div>
        <div class="table-wrapper">
          <table class="history-table">
            <thead>
              <tr>
                <th>ๆœŸ่™Ÿ</th>
                <th>ๆ—ฅๆœŸ</th>
                <th>้ ๆธฌ่™Ÿ็ขผ</th>
                <th>ๅฏฆ้š›่™Ÿ็ขผ</th>
                <th>ๅ‘ฝไธญๆ•ธ</th>
                <th>ๅ‘ฝไธญ่™Ÿ็ขผ</th>
              </tr>
            </thead>
            <tbody id="btBody">
              <tr><td colspan="6" class="loading-row">โณ ่ผ‰ๅ…ฅๅ›žๆธฌ่ณ‡ๆ–™...</td></tr>
            </tbody>
          </table>
        </div>
        <div class="pagination" id="btPagination"></div>
      </div>
    </section>

    <!-- Note -->
    <section class="section">
      <div class="glass-card" style="background:rgba(139,92,246,0.05);border-color:rgba(139,92,246,0.2)">
        <h3 style="color:var(--purple-400);margin-bottom:12px">๐Ÿ“Œ ้—œๆ–ผๅ›žๆธฌๆ–นๆณ•</h3>
        <p style="color:var(--text-secondary);font-size:0.875rem;line-height:1.8">
          ๆœฌๅ›žๆธฌไฝฟ็”จ <strong>Walk-Forward ๆปพๅ‹•ๆธฌ่ฉฆ</strong>๏ผšๅฐ็ฌฌ <code id="noteStart">โ€”</code> ๅˆฐ็ฌฌ <code id="noteEnd">โ€”</code> ๆœŸๅ…ฑ <code>1000</code> ๆœŸ้€ฒ่กŒ้ฉ—่ญ‰ใ€‚<br>
          ๆฏไธ€ๆœŸ็š„้ ๆธฌ๏ผŒ<strong>ๅƒ…ไฝฟ็”จ่ฉฒๆœŸไน‹ๅ‰็š„ๆ‰€ๆœ‰ๆญทๅฒ่ณ‡ๆ–™</strong>ไฝœ็‚บ่จ“็ทด้›†๏ผˆไธไฝฟ็”จๆœชไพ†่ณ‡ๆ–™๏ผ‰๏ผŒ็ขบไฟๆธฌ่ฉฆ็š„ๅ…ฌๅนณๆ€งใ€‚<br>
          <strong>ๅ‘ฝไธญๅฎš็พฉ</strong>๏ผš้ ๆธฌ็š„5ๅ€‹่™Ÿ็ขผไธญ๏ผŒๆœ‰ๅนพๅ€‹่ˆ‡็•ถๆœŸๅฏฆ้š›้–‹ๅ‡บ่™Ÿ็ขผ้‡็–Šใ€‚<br>
          <strong>ๆณจๆ„</strong>๏ผšไปŠๅฝฉ539็‚บ้šจๆฉŸๆŠฝ็Ž๏ผŒ็†่ซ–้šจๆฉŸๅŸบๆบ–ๅ‘ฝไธญ็އ็‚บ 5/39 โ‰ˆ <strong>12.82%</strong>ใ€‚
          ๆœฌๆจกๅž‹ๅ‘ฝไธญ็އ่ˆ‡้šจๆฉŸๅŸบๆบ–็›ธ่ฟ‘๏ผŒ่ชชๆ˜Žๅฝฉๅˆธๅ…ทๆœ‰ๅผท็ƒˆ้šจๆฉŸๆ€งใ€‚
        </p>
      </div>
    </section>
  </main>

  <footer class="site-footer">
    <p>่ณ‡ๆ–™ไพ†ๆบ๏ผš<a href="https://www.pilio.idv.tw/lto539/list.asp" target="_blank" rel="noopener">pilio.idv.tw</a> &nbsp;ยท&nbsp; ๆœฌ็ณป็ตฑๅƒ…ไพ›ๅƒ่€ƒๅจ›ๆจ‚๏ผŒไธไฟ่ญ‰้ ๆธฌๆบ–็ขบๆ€ง</p>
  </footer>

  <script>
    // โ”€โ”€โ”€ State โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    let allResults = [];
    let filtered = [];
    let filterMin = 'all';
    let currentPage = 1;
    const PAGE_SIZE = 50;

    // โ”€โ”€โ”€ Load Data โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    async function loadData() {
      try {
        const res = await fetch('backtest_results.json');
        const data = await res.json();
        const { meta, summary, results } = data;

        // newest first
        allResults = [...results].reverse();
        filtered = allResults;

        // Update header
        document.getElementById('headerSubtitle').textContent =
          `็ฌฌ ${meta.backtestStart} ~ ${meta.backtestEnd} ๆœŸ`;
        document.getElementById('metaN').textContent = `${meta.backtestN} ๆœŸ`;
        document.getElementById('metaRange').textContent =
          `${meta.backtestStart} ~ ${meta.backtestEnd}`;
        document.getElementById('metaRate').textContent = `${summary.overallHitRate}%`;
        document.getElementById('noteStart').textContent = meta.backtestStart;
        document.getElementById('noteEnd').textContent = meta.backtestEnd;

        // KPIs
        document.getElementById('kpiRate').textContent = `${summary.overallHitRate}%`;
        document.getElementById('kpi1').textContent = `${summary.atLeast1Pct}%`;
        document.getElementById('kpi1n').textContent = `${summary.atLeast1} ๆœŸ`;
        document.getElementById('kpi2').textContent = `${summary.atLeast2Pct}%`;
        document.getElementById('kpi2n').textContent = `${summary.atLeast2} ๆœŸ`;
        document.getElementById('kpi3').textContent = `${summary.atLeast3Pct}%`;
        document.getElementById('kpi3n').textContent = `${summary.atLeast3} ๆœŸ`;
        document.getElementById('kpi5').textContent = `${summary.perfect5Pct}%`;
        document.getElementById('kpi5n').textContent = `${summary.perfect5} ๆœŸ`;

        renderDistribution(summary.distribution, summary.totalTests);
        renderTrendChart(results);
        renderTable();
      } catch (e) {
        document.getElementById('btBody').innerHTML =
          `<tr><td colspan="6" class="loading-row">โŒ ็„กๆณ•่ผ‰ๅ…ฅๅ›žๆธฌ่ณ‡ๆ–™๏ผš${e.message}<br>่ซ‹ๅ…ˆๅŸท่กŒ node backtest.js</td></tr>`;
      }
    }

    // โ”€โ”€โ”€ Distribution Bars โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    function renderDistribution(dist, total) {
      const container = document.getElementById('distGrid');
      const maxCount = Math.max(...Object.values(dist));
      const labels = ['5/5 ๐ŸŽ‰', '4/5', '3/5', '2/5', '1/5', '0/5'];
      const colorClasses = ['hit-5', 'hit-4', 'hit-3', 'hit-2', 'hit-1', 'hit-0'];

      container.innerHTML = [5, 4, 3, 2, 1, 0].map((k, i) => {
        const cnt = dist[k] || 0;
        const pct = ((cnt / total) * 100).toFixed(1);
        const barW = maxCount > 0 ? ((cnt / maxCount) * 100) : 0;
        return `
          <div class="dist-row">
            <div class="dist-label">${labels[i]}</div>
            <div class="dist-bar-wrap">
              <div class="dist-bar ${colorClasses[i]}" style="width:${barW}%">
                ${pct + '%'}
              </div>
            </div>
            <div class="dist-count">${cnt} ๆœŸ</div>
          </div>`;
      }).join('');
    }

    // โ”€โ”€โ”€ Trend Chart (hits per 100-period block) โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    function renderTrendChart(results) {
      const canvas = document.getElementById('trendChart');
      const ctx = canvas.getContext('2d');
      const BLOCK = 100;
      const blocks = [];
      for (let i = 0; i < results.length; i += BLOCK) {
        const slice = results.slice(i, i + BLOCK);
        const hits = slice.reduce((s, r) => s + r.hits, 0);
        const pct = (hits / (slice.length * 5)) * 100;
        const label = `${slice[0].period}~${slice[slice.length-1].period}`;
        blocks.push({ label, pct, count: slice.length });
      }

      const dpr = window.devicePixelRatio || 1;
      const rect = canvas.parentElement.getBoundingClientRect();
      canvas.width = rect.width * dpr;
      canvas.height = 260 * dpr;
      ctx.scale(dpr, dpr);
      const W = rect.width, H = 260;
      ctx.clearRect(0, 0, W, H);

      const padL = 40, padR = 12, padT = 20, padB = 50;
      const cW = W - padL - padR, cH = H - padT - padB;
      const maxPct = Math.max(...blocks.map(b => b.pct), 20);

      // Baseline (random = 5/39 = 12.82%)
      const baselineY = padT + cH * (1 - 12.82 / maxPct);
      ctx.setLineDash([4, 4]);
      ctx.strokeStyle = 'rgba(251,191,36,0.4)';
      ctx.lineWidth = 1;
      ctx.beginPath(); ctx.moveTo(padL, baselineY); ctx.lineTo(padL + cW, baselineY); ctx.stroke();
      ctx.setLineDash([]);
      ctx.fillStyle = 'rgba(251,191,36,0.6)';
      ctx.font = '9px Outfit';
      ctx.fillText('้šจๆฉŸๅŸบๆบ– 12.82%', padL + 4, baselineY - 3);

      // Grid
      ctx.strokeStyle = 'rgba(255,255,255,0.05)';
      ctx.lineWidth = 1;
      for (let g = 0; g <= 4; g++) {
        const y = padT + cH * (g / 4);
        ctx.beginPath(); ctx.moveTo(padL, y); ctx.lineTo(padL + cW, y); ctx.stroke();
        ctx.fillStyle = 'rgba(148,163,184,0.5)';
        ctx.font = '9px Outfit'; ctx.textAlign = 'right';
        ctx.fillText((maxPct * (1 - g / 4)).toFixed(0) + '%', padL - 3, y + 3);
      }

      // Bars
      const bw = (cW / blocks.length) * 0.65;
      blocks.forEach((b, i) => {
        const x = padL + (i / blocks.length) * cW + ((cW / blocks.length) - bw) / 2;
        const bh = (b.pct / maxPct) * cH;
        const y = padT + cH - bh;
        const grad = ctx.createLinearGradient(x, y, x, y + bh);
        grad.addColorStop(0, 'rgba(139,92,246,0.9)');
        grad.addColorStop(1, 'rgba(59,130,246,0.5)');
        ctx.fillStyle = grad;
        ctx.beginPath();
        ctx.roundRect ? ctx.roundRect(x, y, bw, bh, 3) : ctx.rect(x, y, bw, bh);
        ctx.fill();
        // value label on bar
        ctx.fillStyle = 'rgba(196,181,253,0.9)';
        ctx.font = '9px Outfit'; ctx.textAlign = 'center';
        if (bh > 14) ctx.fillText(b.pct.toFixed(1) + '%', x + bw / 2, y + 11);
        // x label
        ctx.fillStyle = 'rgba(148,163,184,0.5)';
        ctx.font = '8px Outfit';
        ctx.save(); ctx.translate(x + bw / 2, H - padB + 8);
        ctx.rotate(-Math.PI / 4); ctx.textAlign = 'right';
        ctx.fillText(b.label, 0, 0); ctx.restore();
      });
    }

    // โ”€โ”€โ”€ Table โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    function setFilter(min, btn) {
      filterMin = min;
      document.querySelectorAll('.filter-chip').forEach(c => c.classList.remove('active'));
      btn.classList.add('active');
      filtered = min === 'all' ? allResults : allResults.filter(r => r.hits >= min);
      currentPage = 1;
      renderTable();
    }

    function renderTable() {
      const total = filtered.length;
      const start = (currentPage - 1) * PAGE_SIZE;
      const page = filtered.slice(start, start + PAGE_SIZE);
      document.getElementById('btResultCount').textContent =
        filterMin === 'all' ? `ๅ…ฑ ${total} ๆœŸ` : `็ฏฉ้ธๅ‡บ ${total} ๆœŸ`;

      const tbody = document.getElementById('btBody');
      if (page.length === 0) {
        tbody.innerHTML = '<tr><td colspan="6" class="loading-row">๐Ÿ” ็„ก็ฌฆๅˆ่ณ‡ๆ–™</td></tr>';
      } else {
        tbody.innerHTML = page.map(r => {
          const actualSet = new Set(r.actual);

          // Use predictedWithScores (score-sorted) if available, else fall back
          const predsRanked = r.predictedWithScores
            ? [...r.predictedWithScores]                              // already score-desc from backtest
            : r.predicted.map(n => ({ num: n, score: null }));

          const predNums = predsRanked.map(({ num, score }) => {
            const isHit = actualSet.has(num);
            const scoreLabel = score !== null
              ? `<div style="font-size:0.6rem;color:${isHit ? 'rgba(255,200,100,0.9)' : 'rgba(148,163,184,0.6)'};margin-top:2px;line-height:1">${score}ๆœŸ</div>`
              : '';
            return `<span style="display:inline-flex;flex-direction:column;align-items:center;margin:1px">
              <span class="num-mini ${isHit ? 'hit' : ''}">${num}</span>
              ${scoreLabel}
            </span>`;
          }).join('');

          const actNums = r.actual.map(n =>
            `<span class="num-mini ${r.predicted.includes(n) ? 'hit' : ''}">${n}</span>`
          ).join(' ');
          const hitNums = r.predicted.filter(p => actualSet.has(p));
          const hitStr = hitNums.length > 0
            ? hitNums.map(n => `<span class="num-mini hit">${n}</span>`).join(' ')
            : '<span style="color:var(--text-muted)">โ€”</span>';
          return `<tr class="fade-in">
            <td class="draw-idx">${r.period}</td>
            <td class="draw-date">${r.date}</td>
            <td style="vertical-align:top;padding-top:6px">${predNums}</td>
            <td>${actNums}</td>
            <td><span class="hit-badge hb-${r.hits}">${r.hits}/5</span></td>
            <td>${hitStr}</td>
          </tr>`;
        }).join('');
      }


      // pagination
      const totalPages = Math.ceil(total / PAGE_SIZE);
      const pag = document.getElementById('btPagination');
      if (totalPages <= 1) { pag.innerHTML = ''; return; }
      const btns = [];
      btns.push(`<button class="page-btn" onclick="changePage(${currentPage-1})" ${currentPage===1?'disabled':''}>โ€น</button>`);
      const range = [];
      for (let i = 1; i <= totalPages; i++) {
        if (i === 1 || i === totalPages || (i >= currentPage - 2 && i <= currentPage + 2)) range.push(i);
      }
      let prev = null;
      for (const p of range) {
        if (prev && p - prev > 1) btns.push(`<span class="page-btn" style="opacity:0.4;pointer-events:none">โ€ฆ</span>`);
        btns.push(`<button class="page-btn ${p===currentPage?'active':''}" onclick="changePage(${p})">${p}</button>`);
        prev = p;
      }
      btns.push(`<button class="page-btn" onclick="changePage(${currentPage+1})" ${currentPage===totalPages?'disabled':''}>โ€บ</button>`);
      pag.innerHTML = btns.join('');
    }

    function changePage(p) {
      const totalPages = Math.ceil(filtered.length / PAGE_SIZE);
      if (p < 1 || p > totalPages) return;
      currentPage = p;
      renderTable();
      document.querySelector('.history-section, .section:last-of-type')?.scrollIntoView?.({ behavior: 'smooth', block: 'start' });
    }

    // โ”€โ”€โ”€ BG Particles โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€โ”€
    function initParticles() {
      const c = document.getElementById('bgParticles');
      for (let i = 0; i < 15; i++) {
        const p = document.createElement('div');
        p.style.cssText = `position:absolute;width:${2+Math.random()*2}px;height:${2+Math.random()*2}px;border-radius:50%;background:rgba(139,92,246,${0.15+Math.random()*0.2});left:${Math.random()*100}%;top:${Math.random()*100}%;animation:floatParticle ${6+Math.random()*8}s ease-in-out ${Math.random()*4}s infinite alternate;`;
        c.appendChild(p);
      }
      const s = document.createElement('style');
      s.textContent = `@keyframes floatParticle{from{transform:translateY(0) translateX(0);opacity:.3}to{transform:translateY(-30px) translateX(20px);opacity:.8}}`;
      document.head.appendChild(s);
    }

    let rTimer;
    window.addEventListener('resize', () => { clearTimeout(rTimer); rTimer = setTimeout(() => { if (allResults.length) { renderTrendChart([...allResults].reverse()); } }, 250); });

    initParticles();
    loadData();
  </script>
</body>
</html>