File size: 23,414 Bytes
f8e1a1c
 
 
 
 
e0f232f
 
f8e1a1c
 
 
 
 
 
 
 
 
 
 
 
 
 
e0f232f
f8e1a1c
 
 
 
e0f232f
 
 
 
 
 
 
 
 
 
 
f8e1a1c
 
 
 
 
 
e0f232f
f8e1a1c
 
e0f232f
 
 
f8e1a1c
 
 
 
 
 
 
 
e0f232f
 
 
 
 
 
f8e1a1c
 
 
e0f232f
 
 
f8e1a1c
 
 
 
 
 
 
 
 
 
 
e0f232f
 
 
 
f8e1a1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e0f232f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
f8e1a1c
 
 
e0f232f
 
 
 
 
 
 
 
 
 
 
 
f8e1a1c
 
 
e0f232f
 
 
 
f8e1a1c
 
 
 
 
 
 
 
e0f232f
f8e1a1c
 
 
 
e0f232f
 
 
f8e1a1c
 
 
 
e0f232f
 
 
 
 
 
f8e1a1c
 
 
 
e0f232f
 
 
f8e1a1c
 
e0f232f
 
 
f8e1a1c
 
e0f232f
 
 
f8e1a1c
 
e0f232f
 
 
 
 
 
 
 
 
f8e1a1c
 
e0f232f
 
 
f8e1a1c
 
e0f232f
 
 
 
 
 
 
 
 
 
 
 
 
f8e1a1c
 
e0f232f
 
 
 
f8e1a1c
 
 
 
 
 
e0f232f
 
 
 
f8e1a1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e0f232f
 
 
 
 
 
f8e1a1c
 
e0f232f
f8e1a1c
 
 
e0f232f
 
 
f8e1a1c
 
 
 
 
 
 
e0f232f
 
f8e1a1c
 
 
 
 
e0f232f
 
f8e1a1c
 
e0f232f
 
 
f8e1a1c
 
 
 
 
 
 
 
 
 
 
 
e0f232f
 
 
f8e1a1c
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e0f232f
 
 
 
 
 
 
f8e1a1c
 
 
 
 
 
 
 
 
 
 
 
 
e0f232f
f8e1a1c
 
 
 
 
 
 
 
 
 
 
e0f232f
 
 
f8e1a1c
 
 
 
 
 
e0f232f
 
f8e1a1c
 
 
 
 
 
 
 
 
e0f232f
 
f8e1a1c
 
 
 
e0f232f
 
f8e1a1c
e0f232f
f8e1a1c
 
 
 
 
e0f232f
 
f8e1a1c
 
 
 
e0f232f
 
 
 
 
 
 
 
 
 
 
f8e1a1c
 
e0f232f
f8e1a1c
 
 
 
 
 
e0f232f
 
 
 
f8e1a1c
 
 
 
 
 
e0f232f
f8e1a1c
 
e0f232f
f8e1a1c
 
 
 
e0f232f
 
 
4e60f97
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bb6135e
f8e1a1c
e0f232f
 
 
4e60f97
 
 
 
 
 
 
 
 
 
f8e1a1c
e0f232f
 
 
4e60f97
f8e1a1c
bb6135e
 
 
 
32c4979
 
 
bb6135e
 
 
 
 
32c4979
bb6135e
 
 
32c4979
bb6135e
 
 
 
 
 
 
 
 
 
 
32c4979
 
 
bb6135e
 
 
32c4979
 
bb6135e
 
 
32c4979
 
bb6135e
 
 
 
 
 
 
 
 
 
 
 
 
f8e1a1c
e0f232f
 
 
 
f8e1a1c
 
e0f232f
 
 
 
 
32c4979
bb6135e
 
 
 
 
4e60f97
 
 
 
 
 
 
f8e1a1c
 
bb6135e
 
32c4979
bb6135e
 
 
 
 
 
 
 
 
 
f8e1a1c
 
 
 
4e60f97
f8e1a1c
 
4e60f97
 
 
 
 
 
 
 
 
 
 
 
a7f0469
 
 
 
 
 
 
 
e0f232f
 
a7f0469
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e0f232f
4e60f97
 
 
 
 
 
a7f0469
 
 
 
 
e0f232f
f8e1a1c
 
 
 
 
 
 
 
 
e0f232f
f8e1a1c
 
 
 
 
 
e0f232f
f8e1a1c
 
 
 
 
 
 
 
 
 
 
 
e0f232f
 
 
f8e1a1c
 
 
e0f232f
f8e1a1c
e0f232f
 
 
f8e1a1c
 
 
 
 
 
 
e0f232f
 
 
 
 
 
f8e1a1c
 
 
 
 
 
 
 
 
e0f232f
f8e1a1c
 
 
 
 
 
 
 
 
 
 
 
 
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
/* ============================================
   ECHO TUTOR PRO β€” Phase 3 Features Module
   Theme, Gamification, Exercises, PDF, Whisper
   ============================================ */

window.EchoFeatures = (function () {
  const $ = (id) => document.getElementById(id);

  // ─── THEME TOGGLE ───
  const Theme = {
    init() {
      const saved = localStorage.getItem('echo_theme') || 'dark';
      document.documentElement.setAttribute('data-theme', saved);
      const btn = $('theme-btn');
      if (btn) btn.addEventListener('click', () => this.toggle());
    },
    toggle() {
      const current = document.documentElement.getAttribute('data-theme');
      const next = current === 'dark' ? 'light' : 'dark';
      document.documentElement.setAttribute('data-theme', next);
      localStorage.setItem('echo_theme', next);
    },
  };

  // ─── XP & GAMIFICATION ───
  const XP = {
    data: {
      totalXP: 0,
      streak: 0,
      lastDay: '',
      messages: 0,
      corrections: 0,
      vocab: 0,
      exercises: 0,
      perfect_pronun: 0,
      badges: [],
    },
    load() {
      try {
        const saved = localStorage.getItem('echo_xp');
        if (saved) this.data = { ...this.data, ...JSON.parse(saved) };
        this.updateStreak();
        this.render();
      } catch (e) {}
    },
    save() {
      try {
        localStorage.setItem('echo_xp', JSON.stringify(this.data));
      } catch (e) {}
    },
    add(amount, reason) {
      this.data.totalXP += amount;
      this.save();
      this.render();
      this.showPopup(amount, reason);
      this.checkBadges();
    },
    getLevel() {
      return Math.floor(this.data.totalXP / 100) + 1;
    },
    getLevelProgress() {
      return this.data.totalXP % 100;
    },
    updateStreak() {
      const today = new Date().toISOString().slice(0, 10);
      if (this.data.lastDay === today) return;
      const yesterday = new Date(Date.now() - 86400000)
        .toISOString()
        .slice(0, 10);
      if (this.data.lastDay === yesterday) {
        this.data.streak++;
      } else if (this.data.lastDay !== today) {
        this.data.streak = 1;
      }
      this.data.lastDay = today;
      this.save();
    },
    render() {
      const lvl = this.getLevel();
      const prog = this.getLevelProgress();
      const el = (id, val) => {
        const e = $(id);
        if (e) e.textContent = val;
      };
      el('xp-level', `Lv.${lvl}`);
      el('xp-value', `${this.data.totalXP} XP`);
      el('streak-count', `πŸ”₯ ${this.data.streak}`);
      const fill = $('xp-bar-fill');
      if (fill) fill.style.width = prog + '%';
    },
    showPopup(amount, reason) {
      const popup = $('xp-popup');
      if (!popup) return;
      popup.textContent = `+${amount} XP${reason ? ' Β· ' + reason : ''}`;
      popup.classList.remove('show');
      void popup.offsetWidth;
      popup.classList.add('show');
      setTimeout(() => popup.classList.remove('show'), 1600);
    },
    trackMessage() {
      this.data.messages++;
      this.add(5, 'Message');
      this.save();
    },
    trackCorrection() {
      this.data.corrections++;
      this.add(3, 'Learning');
      this.save();
    },
    trackVocab() {
      this.data.vocab++;
      this.add(2, 'New Word');
      this.save();
    },
    trackExercise(correct) {
      this.data.exercises++;
      this.add(correct ? 10 : 2, correct ? 'Correct!' : 'Practice');
      this.save();
    },
    trackPronunciation(score) {
      if (score >= 10) {
        this.data.perfect_pronun++;
        this.add(15, 'Perfect!');
      }
      this.save();
    },
    checkBadges() {
      const d = this.data;
      const checks = {
        first_chat: d.messages >= 1,
        ten_messages: d.messages >= 10,
        fifty_messages: d.messages >= 50,
        first_correction: d.corrections >= 1,
        vocab_10: d.vocab >= 10,
        streak_3: d.streak >= 3,
        streak_7: d.streak >= 7,
        streak_30: d.streak >= 30,
        exercise_10: d.exercises >= 10,
        level_5: d.totalXP >= 500,
        level_10: d.totalXP >= 1000,
        perfect_pronun: d.perfect_pronun >= 1,
      };
      let newBadge = false;
      Object.entries(checks).forEach(([id, met]) => {
        if (met && !d.badges.includes(id)) {
          d.badges.push(id);
          newBadge = true;
        }
      });
      if (newBadge) this.save();
      return newBadge;
    },
    renderBadges(containerId, allBadges) {
      const grid = $(containerId);
      if (!grid || !allBadges) return;
      grid.innerHTML = '';
      allBadges.forEach((b) => {
        const earned = this.data.badges.includes(b.id);
        grid.innerHTML += `<div class="badge-item ${earned ? 'earned' : 'locked'}"><span class="badge-icon">${b.icon}</span><span class="badge-label">${b.label}</span><span class="badge-desc">${b.description}</span></div>`;
      });
    },
    getSessionXP() {
      return this.data.totalXP;
    },
  };

  // ─── EXERCISES ENGINE ───
  const Exercises = {
    pool: [],
    current: null,
    index: 0,
    score: 0,
    total: 0,
    type: 'fill_blank',
    async load(level, type) {
      this.type = type;
      if (type === 'ai_generated') return this.loadAI(level);
      try {
        const res = await fetch(
          `/exercises?level=${level}&type=${type}&count=10`,
        );
        const data = await res.json();
        this.pool = data.exercises || [];
        this.index = 0;
        this.score = 0;
        this.total = this.pool.length;
        if (this.pool.length) this.show();
        else this.showEmpty();
      } catch (e) {
        this.showEmpty();
      }
    },
    async loadAI(level) {
      const corrections = JSON.parse(
        localStorage.getItem('echo_corrections') || '[]',
      );
      if (!corrections.length) {
        this.showEmpty('Chat first to get personalized exercises!');
        return;
      }
      $('exercise-question').textContent =
        'Generating exercises from your mistakes...';
      try {
        const res = await fetch('/exercises/generate', {
          method: 'POST',
          headers: { 'Content-Type': 'application/json' },
          body: JSON.stringify({ errors: corrections.slice(-10), level }),
        });
        const data = await res.json();
        this.pool = (data.exercises || []).map((e, i) => ({
          ...e,
          id: 'ai_' + i,
          type: 'fill_blank',
        }));
        this.index = 0;
        this.score = 0;
        this.total = this.pool.length;
        if (this.pool.length) this.show();
        else this.showEmpty('Could not generate exercises.');
      } catch (e) {
        this.showEmpty('Error generating exercises.');
      }
    },
    show() {
      if (this.index >= this.pool.length) {
        this.showComplete();
        return;
      }
      this.current = this.pool[this.index];
      const q = $('exercise-question');
      const opts = $('exercise-options');
      const result = $('exercise-result');
      const explain = $('exercise-explain');
      const inputArea = $('exercise-input-area');
      if (result) {
        result.classList.add('hidden');
        result.className = 'exercise-result hidden';
      }
      if (explain) explain.classList.add('hidden');
      if (inputArea) inputArea.classList.add('hidden');
      $('exercise-progress').textContent = `${this.index + 1}/${this.total}`;
      $('exercise-score').textContent = `Score: ${this.score}/${this.index}`;

      const ex = this.current;
      if (!ex) return;

      // Determine exercise type from data
      const exType = ex.type || this.type;
      if (exType === 'sentence_reorder' || (ex.words && !ex.options)) {
        this.showReorder(ex);
      } else if (exType === 'translation') {
        this.showTranslation(ex);
      } else {
        // fill_blank or word_match β€” multiple choice
        if (q) q.innerHTML = ex.q || ex.word || '';
        if (ex.type === 'synonym')
          q.innerHTML +=
            '<br><small style="color:var(--text-muted)">Find the synonym</small>';
        if (ex.type === 'antonym')
          q.innerHTML +=
            '<br><small style="color:var(--text-muted)">Find the antonym</small>';
        opts.innerHTML = '';
        opts.classList.remove('hidden');
        (ex.options || []).forEach((opt) => {
          const btn = document.createElement('button');
          btn.className = 'exercise-option';
          btn.textContent = opt;
          btn.addEventListener('click', () =>
            this.checkAnswer(opt, ex.answer, btn),
          );
          opts.appendChild(btn);
        });
      }
    },
    showReorder(ex) {
      const q = $('exercise-question');
      const opts = $('exercise-options');
      q.innerHTML =
        '<span style="color:var(--text-muted)">Arrange the words in correct order:</span>';
      opts.innerHTML = '';
      const words = [...ex.words].sort(() => Math.random() - 0.5);
      const selected = [];
      const answerArea = document.createElement('div');
      answerArea.className = 'reorder-area';
      answerArea.innerHTML =
        '<span style="color:var(--text-hint);font-size:0.8rem">Click words to build the sentence</span>';
      opts.appendChild(answerArea);
      const wordPool = document.createElement('div');
      wordPool.style.cssText =
        'display:flex;flex-wrap:wrap;gap:8px;margin-top:10px';
      words.forEach((w) => {
        const chip = document.createElement('span');
        chip.className = 'reorder-word';
        chip.textContent = w;
        chip.addEventListener('click', () => {
          if (chip.classList.contains('placed')) {
            chip.classList.remove('placed');
            const idx = selected.indexOf(w);
            if (idx >= 0) selected.splice(idx, 1);
          } else {
            chip.classList.add('placed');
            selected.push(w);
          }
          answerArea.innerHTML =
            selected.join(' ') ||
            '<span style="color:var(--text-hint);font-size:0.8rem">Click words</span>';
          if (selected.length === words.length) {
            const userAns = selected.join(' ');
            const correct = userAns.toLowerCase() === ex.answer.toLowerCase();
            if (correct) this.score++;
            this.showResult(correct, ex.answer);
          }
        });
        wordPool.appendChild(chip);
      });
      opts.appendChild(wordPool);
    },
    showTranslation(ex) {
      const q = $('exercise-question');
      const opts = $('exercise-options');
      const inputArea = $('exercise-input-area');
      q.innerHTML = `<span style="color:var(--text-muted)">Translate to ${ex.target_lang === 'en' ? 'English' : ex.target_lang}:</span><br><br>"${ex.source}"`;
      opts.innerHTML = '';
      if (inputArea) {
        inputArea.classList.remove('hidden');
        const inp = $('exercise-input');
        const checkBtn = $('exercise-check');
        if (inp) inp.value = '';
        if (checkBtn) {
          const handler = () => {
            const userAns = (inp?.value || '')
              .trim()
              .toLowerCase()
              .replace(/[?.!,]/g, '');
            const correct = [ex.answer, ...(ex.alternatives || [])].some(
              (a) => userAns === a.toLowerCase().replace(/[?.!,]/g, ''),
            );
            if (correct) this.score++;
            this.showResult(correct, ex.answer);
            checkBtn.removeEventListener('click', handler);
          };
          checkBtn.addEventListener('click', handler);
        }
      }
    },
    checkAnswer(selected, correct, btn) {
      const opts = $('exercise-options');
      const isCorrect = selected === correct;
      if (isCorrect) this.score++;
      btn.classList.add(isCorrect ? 'correct' : 'wrong');
      opts.querySelectorAll('.exercise-option').forEach((b) => {
        b.classList.add('disabled');
        if (b.textContent === correct) b.classList.add('correct');
      });
      this.showResult(isCorrect, correct);
    },
    showResult(correct, answer) {
      const result = $('exercise-result');
      const explain = $('exercise-explain');
      if (result) {
        result.classList.remove('hidden');
        result.className = `exercise-result ${correct ? 'correct' : 'wrong'}`;
        result.textContent = correct
          ? 'Correct! Well done!'
          : `Incorrect. Answer: ${answer}`;
      }
      if (explain && this.current?.explain) {
        explain.classList.remove('hidden');
        explain.textContent = this.current.explain;
      }
      XP.trackExercise(correct);
      $('exercise-score').textContent =
        `Score: ${this.score}/${this.index + 1}`;
    },
    next() {
      this.index++;
      if (this.index >= this.pool.length) this.showComplete();
      else this.show();
    },
    showComplete() {
      const q = $('exercise-question');
      const opts = $('exercise-options');
      if (q)
        q.innerHTML = `<div style="text-align:center;padding:20px"><h3 style="margin-bottom:10px">Exercise Complete!</h3><p style="color:var(--text-muted)">Score: ${this.score}/${this.total}</p><p style="margin-top:10px">${this.score === this.total ? 'Perfect score! Amazing!' : this.score >= this.total * 0.7 ? 'Great job!' : 'Keep practicing!'}</p></div>`;
      if (opts) opts.innerHTML = '';
    },
    showEmpty(msg) {
      const q = $('exercise-question');
      if (q)
        q.textContent = msg || 'No exercises available for this type/level.';
      $('exercise-options').innerHTML = '';
    },
  };

  // ─── PDF EXPORT ───
  const PDF = {
    export(sessionData) {
      const { duration, messages, corrections, vocab, avgPronun, xp } =
        sessionData;
      const lines = [
        'ECHO TUTOR PRO β€” Session Report',
        '═'.repeat(40),
        `Date: ${new Date().toLocaleDateString()} ${new Date().toLocaleTimeString()}`,
        `Duration: ${duration}`,
        `Messages: ${messages}`,
        `Corrections: ${corrections}`,
        `New Words: ${vocab}`,
        `Avg Pronunciation: ${avgPronun}`,
        `XP Earned: +${xp}`,
        '',
        '─'.repeat(40),
        'CONVERSATION:',
        '─'.repeat(40),
        '',
      ];
      const msgEls = document.querySelectorAll('#messages .message');
      msgEls.forEach((m) => {
        const role = m.classList.contains('user') ? 'YOU' : 'ECHO';
        lines.push(`[${role}] ${m.textContent.trim()}`);
        lines.push('');
      });
      if (corrections > 0) {
        lines.push('─'.repeat(40), 'CORRECTIONS:', '─'.repeat(40), '');
        const stored = JSON.parse(
          localStorage.getItem('echo_corrections') || '[]',
        );
        stored.forEach((c) => lines.push(`βœ— ${c.wrong}  β†’  βœ“ ${c.right}`));
      }
      const text = lines.join('\n');
      const blob = new Blob([text], { type: 'text/plain;charset=utf-8' });
      const url = URL.createObjectURL(blob);
      const a = document.createElement('a');
      a.href = url;
      a.download = `echo-session-${new Date().toISOString().slice(0, 10)}.txt`;
      a.click();
      URL.revokeObjectURL(url);
    },
  };

  // ─── WHISPER STT ───
  const Whisper = {
    recorder: null,
    chunks: [],
    recording: false,
    mimeType: 'audio/webm',
    fileExt: 'webm',
    pickMimeType() {
      try {
        if (!window.MediaRecorder?.isTypeSupported) return '';
        const candidates = [
          'audio/webm;codecs=opus',
          'audio/webm',
          'audio/mp4',
          'audio/mpeg',
        ];
        for (const mimeType of candidates) {
          if (window.MediaRecorder.isTypeSupported(mimeType)) {
            return mimeType;
          }
        }
      } catch (e) {}
      return '';
    },
    async start(lang, onSilence) {
      try {
        const stream = await navigator.mediaDevices.getUserMedia({
          audio: true,
        });
        const mimeType = this.pickMimeType();
        this.mimeType = mimeType || 'audio/webm';
        this.fileExt = this.mimeType.includes('mp4')
          ? 'm4a'
          : this.mimeType.includes('mpeg')
            ? 'mp3'
            : 'webm';
        this.recorder = mimeType
          ? new MediaRecorder(stream, { mimeType })
          : new MediaRecorder(stream);
        this.chunks = [];
        this.recorder.ondataavailable = (e) => {
          if (e.data.size > 0) this.chunks.push(e.data);
        };
        this.recorder.start(250);
        this.recording = true;

        // Simple VAD (Voice Activity Detection) for auto-stop
        if (onSilence) {
          try {
            const audioCtx = new (
              window.AudioContext || window.webkitAudioContext
            )();
            const source = audioCtx.createMediaStreamSource(stream);
            const analyser = audioCtx.createAnalyser();
            analyser.fftSize = 512;
            analyser.smoothingTimeConstant = 0.1;
            source.connect(analyser);

            const dataArray = new Uint8Array(analyser.frequencyBinCount);
            let silenceStart = Date.now();
            let hasSpoken = false;

            const checkSilence = () => {
              if (!this.recording) {
                audioCtx.close().catch(() => {});
                return;
              }
              analyser.getByteFrequencyData(dataArray);
              let sum = 0;
              for (let i = 0; i < dataArray.length; i++) {
                sum += dataArray[i];
              }
              const average = sum / dataArray.length;

              if (average > 15) {
                // Threshold for speech
                hasSpoken = true;
                silenceStart = Date.now();
              } else {
                if (hasSpoken && Date.now() - silenceStart > 2000) {
                  // 2s silence after speech
                  onSilence();
                  audioCtx.close().catch(() => {});
                  return;
                } else if (!hasSpoken && Date.now() - silenceStart > 10000) {
                  // 10s silence without speech
                  onSilence();
                  audioCtx.close().catch(() => {});
                  return;
                }
              }
              requestAnimationFrame(checkSilence);
            };
            checkSilence();
          } catch (e) {
            console.warn('VAD setup failed:', e);
          }
        }

        return stream;
      } catch (e) {
        console.error('Whisper mic error:', e);
        return null;
      }
    },
    stop() {
      return new Promise((resolve) => {
        if (!this.recorder || !this.recording) {
          resolve(null);
          return;
        }

        const finish = () => {
          this.recording = false;
          if (this.recorder && this.recorder.stream) {
            this.recorder.stream.getTracks().forEach((t) => t.stop());
          }
          if (!this.chunks.length) {
            resolve(null);
            return;
          }
          const blob = new Blob(this.chunks, {
            type: this.mimeType || this.chunks[0]?.type || 'audio/webm',
          });
          resolve(blob);
        };

        this.recorder.onstop = finish;

        try {
          if (this.recorder.state !== 'inactive') {
            this.recorder.stop();
          } else {
            finish();
          }
        } catch (e) {
          console.error('Error stopping recorder:', e);
          finish();
        }
      });
    },
    async transcribe(blob, lang) {
      const form = new FormData();
      form.append('audio', blob, `recording.${this.fileExt || 'webm'}`);
      form.append('language', lang || 'en');
      try {
        const controller =
          typeof AbortController !== 'undefined' ? new AbortController() : null;
        const timeout = controller
          ? setTimeout(() => controller.abort(), 45000)
          : null;

        const res = await fetch('/transcribe', {
          method: 'POST',
          body: form,
          signal: controller?.signal,
        });
        if (timeout) clearTimeout(timeout);
        let payload = null;
        try {
          payload = await res.json();
        } catch (e) {}

        if (!res.ok) {
          return {
            ok: false,
            error:
              payload?.error || `Transcription request failed (${res.status})`,
          };
        }

        if (!payload?.text) {
          return {
            ok: false,
            error: payload?.error || 'No transcript returned from Whisper',
          };
        }

        return {
          ok: true,
          text: payload.text,
          language: payload.language || lang || 'en',
        };
      } catch (e) {
        if (e?.name === 'AbortError') {
          return {
            ok: false,
            error: 'Transcription timed out. Try again.',
          };
        }
        return {
          ok: false,
          error: 'Could not reach transcription service',
        };
      }
    },
  };

  // ─── SCENARIO HELPERS ───
  const Scenarios = {
    data: [],
    render(containerId) {
      const grid = $(containerId);
      if (!grid || !this.data.length) return;
      grid.innerHTML = '';
      this.data.forEach((s) => {
        const card = document.createElement('button');
        card.className = 'scenario-card';
        card.dataset.scenario = s.id;
        card.innerHTML = `<span class="scenario-icon">${s.icon}</span><span class="scenario-name">${s.label}</span><span class="scenario-desc">${s.description}</span>`;
        grid.appendChild(card);
      });
    },
  };

  // ─── INIT ───
  function init() {
    Theme.init();
    XP.load();

    // Exercise modal controls
    $('exercises-btn')?.addEventListener('click', () => {
      $('exercises-modal')?.classList.toggle('hidden');
      Exercises.load('intermediate', 'fill_blank');
    });
    $('exercises-close')?.addEventListener('click', () =>
      $('exercises-modal')?.classList.add('hidden'),
    );
    $('exercise-next')?.addEventListener('click', () => Exercises.next());

    // Exercise type tabs
    document.querySelectorAll('.ex-type-btn').forEach((btn) => {
      btn.addEventListener('click', () => {
        document
          .querySelectorAll('.ex-type-btn')
          .forEach((b) => b.classList.remove('active'));
        btn.classList.add('active');
        const level = window._echoLevel || 'intermediate';
        Exercises.load(level, btn.dataset.type);
      });
    });

    // XP display click -> badges
    $('xp-display')?.addEventListener('click', () =>
      $('badges-modal')?.classList.toggle('hidden'),
    );
    $('badges-close')?.addEventListener('click', () =>
      $('badges-modal')?.classList.add('hidden'),
    );

    // PDF export
    $('summary-pdf')?.addEventListener('click', () => {
      PDF.export({
        duration: $('sum-time')?.textContent || '0:00',
        messages: $('sum-msgs')?.textContent || '0',
        corrections: $('sum-corrections')?.textContent || '0',
        vocab: $('sum-vocab')?.textContent || '0',
        avgPronun: $('sum-pronun')?.textContent || '-',
        xp: $('sum-xp')?.textContent || '0',
      });
    });
  }

  // Auto-init on DOMContentLoaded (may already have fired)
  if (document.readyState === 'loading') {
    document.addEventListener('DOMContentLoaded', init);
  } else {
    init();
  }

  return { Theme, XP, Exercises, PDF, Whisper, Scenarios };
})();