File size: 29,991 Bytes
df9973f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
84e1dd3
633f37b
 
df9973f
 
 
 
84e1dd3
df9973f
84e1dd3
df9973f
633f37b
84e1dd3
 
633f37b
df9973f
 
84e1dd3
633f37b
 
 
 
 
 
84e1dd3
 
 
633f37b
 
df9973f
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AI Pro Suite - Math Solver</title>
    
    <link rel="manifest" href="/manifest.json">
    <meta name="theme-color" content="#0f172a">
    
    <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=Kalam:wght@400;700&family=Poppins:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/localforage/1.10.0/localforage.min.js" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
    <script src="https://cdnjs.cloudflare.com/polyfill/v3/polyfill.min.js?features=es6" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/html2canvas/1.4.1/html2canvas.min.js" crossorigin="anonymous" referrerpolicy="no-referrer"></script>
    
    <script>

      MathJax = { tex: { inlineMath: [['$', '$'], ['\\(', '\\)']], displayMath: [['$$', '$$'], ['\\[', '\\]']] } };

    </script>
    <script id="MathJax-script" async src="https://cdn.jsdelivr.net/npm/mathjax@3/es5/tex-mml-chtml.js" crossorigin="anonymous" referrerpolicy="no-referrer"></script>

    <link rel="stylesheet" href="style.css">
    
    <style>

        mjx-container { padding: 5px 0 !important; margin: 0 !important; }

        .typing-indicator { display: flex; gap: 5px; padding: 10px; }

        .typing-indicator span { width: 8px; height: 8px; background: #c084fc; border-radius: 50%; animation: pulse 1s infinite alternate; }

        .typing-indicator span:nth-child(2) { animation-delay: 0.2s; }

        .typing-indicator span:nth-child(3) { animation-delay: 0.4s; }

        @keyframes pulse { from { opacity: 0.3; transform: scale(0.8); } to { opacity: 1; transform: scale(1.2); } }

        

        .digital-paper-container { width: 100%; overflow-x: auto; margin-top: 5px; border-radius: 8px; box-shadow: 0 10px 25px rgba(0,0,0,0.5); margin-bottom: 10px; }

        .digital-paper { background-color: #fdfbf7; background-image: repeating-linear-gradient(transparent, transparent 29px, #93c5fd 29px, #93c5fd 30px); line-height: 30px; padding: 30px 20px 30px 65px; position: relative; font-family: 'Kalam', cursive; font-size: 20px; color: #1e3a8a; min-width: 300px; white-space: pre-wrap; }

        .digital-paper::before { content: ''; position: absolute; top: 0; bottom: 0; left: 45px; width: 2px; background: #fca5a5; }



        /* FIX: Prevents dotted circles in Hindi Matras by enforcing strict font fallbacks */

        .bubble, #mathChatHistory {

            font-family: 'Poppins', 'Noto Sans Devanagari', 'Arial Unicode MS', sans-serif !important;

            line-height: 1.8 !important;

        }



        /* FIX: Mobile UI Scaling (Smaller fonts, fits screen properly) */

        @media (max-width: 600px) {

            .appTitle { font-size: 16px !important; letter-spacing: 0.5px; }

            .topbar { padding: 0 10px; }

            body, input, textarea, select, button, .bubble, .card { font-size: 13.5px !important; }

            h1, h2, h3, .cardTitle, .dash-title { font-size: 18px !important; }

            .chat-action-btn { width: 45px !important; height: 45px !important; font-size: 18px !important; }

            #imgModeBtn, #langToggleBtn, button[onclick^="toggleFocusMode"] { padding: 5px 10px !important; font-size: 11px !important; }

            .chat-input-wrapper textarea { min-height: 40px !important; font-size: 13.5px !important; padding: 10px !important; }

        }

    </style>
</head>
<body>

<div class="overlay" id="overlay" onclick="toggleSidebar()"></div>

<div class="topbar" id="appTopbar" style="display: flex; justify-content: space-between; align-items: center; padding-right: 15px; transition: opacity 0.5s ease, transform 0.5s ease;">
  <div style="display: flex; align-items: center; gap: 15px;">
      <div class="menuBtn" onclick="toggleSidebar()">☰</div>
      <div class="appTitle">AI Pro Suite</div>
  </div>
  <div style="display: flex; align-items: center; gap: 10px;">
      <button id="imgModeBtn" onclick="toggleImageModeBtn()" style="background: var(--input-bg); border: 2px solid var(--border-light); color: var(--text-main); border-radius: 20px; padding: 6px 14px; font-size: 13px; font-weight: bold; cursor: pointer; display: flex; align-items: center; gap: 5px; box-shadow: var(--glass-shadow); transition: 0.3s;">
          🖼️ Image Mode
      </button>

      <button onclick="toggleFocusMode(true)" style="background: var(--input-bg); border: 2px solid var(--border-light); color: var(--text-main); border-radius: 20px; padding: 6px 14px; font-size: 13px; font-weight: bold; cursor: pointer; display: flex; align-items: center; gap: 5px; box-shadow: var(--glass-shadow); transition: 0.2s;">
          🎯 Focus
      </button>
      
      <button id="langToggleBtn" onclick="toggleMathLanguage()" style="background: var(--input-bg); border: 2px solid var(--border-light); color: var(--text-main); border-radius: 20px; padding: 6px 14px; font-size: 13px; font-weight: bold; cursor: pointer; display: flex; align-items: center; gap: 5px; box-shadow: var(--glass-shadow); transition: 0.2s;">
          🌍 <span id="langText">HI</span>
      </button>
  </div>
</div>

<button id="exitFocusBtn" onclick="toggleFocusMode(false)" style="display: none; position: fixed; top: 20px; right: 20px; z-index: 999999; background: #ef4444; color: white; border: 2px solid white; border-radius: 50%; width: 50px; height: 50px; font-size: 20px; font-weight: bold; cursor: pointer; box-shadow: 0 4px 15px rgba(0,0,0,0.5); opacity: 0; transition: opacity 0.5s ease;">✖</button>

<div id="imageModeModal" style="display:none; position:fixed; inset:0; background:rgba(0,0,0,0.95); z-index:9999999; flex-direction:column; align-items:center; justify-content:center; opacity:0; transition:opacity 0.3s;">
    <div id="imageModeControls" style="position:absolute; top:0; left:0; right:0; padding:20px 30px; display:flex; justify-content:space-between; align-items:center; background:linear-gradient(rgba(0,0,0,0.8), transparent); transition:0.3s; z-index:100;">
        <button onclick="downloadImageMode()" style="background:var(--primary); border:2px solid white; color:white; border-radius:15px; padding:8px 15px; font-weight:bold; font-size:16px; cursor:pointer; box-shadow:0 4px 10px rgba(0,0,0,0.5);">📥 Download</button>
        <button onclick="closeImageMode()" style="background:#ef4444; border:2px solid white; color:white; border-radius:50%; width:40px; height:40px; font-weight:bold; font-size:18px; cursor:pointer; box-shadow:0 4px 10px rgba(0,0,0,0.5);">✖</button>
    </div>
    
    <button id="imgPrevBtn" onclick="prevImagePage(event)" style="position:absolute; left:20px; top:50%; transform:translateY(-50%); font-size:50px; background:none; border:none; color:white; cursor:pointer; z-index:100; display:none; filter:drop-shadow(0 0 5px black); transition:0.3s;">❮</button>
    <button id="imgNextBtn" onclick="nextImagePage(event)" style="position:absolute; right:20px; top:50%; transform:translateY(-50%); font-size:50px; background:none; border:none; color:white; cursor:pointer; z-index:100; display:none; filter:drop-shadow(0 0 5px black); transition:0.3s;">❯</button>

    <div id="imageModePaperWrapper" onclick="toggleImageControls()" style="width:100%; max-width:800px; max-height:80vh; overflow-y:auto; overflow-x:hidden; position:relative; background:#fdfbf7; border-radius:10px; box-shadow:0 15px 40px rgba(0,0,0,0.8); transition: transform 0.4s;">
        <div id="imageModePaper" class="digital-paper" style="min-height:100%; padding:40px 25px 40px 65px; margin:0;">
            </div>
    </div>
    <div id="imageModePageIndicator" style="color:white; margin-top:20px; font-weight:bold; font-size:18px; font-family:monospace; z-index:100; transition:0.3s;">Page 1 / 1</div>
</div>

<div class="apiTracker">
    <div>⏱️ PT: <span id="apiTimer" class="api-val timer">--:--:--</span></div>
    <div style="color: #f59e0b;">📊 Tot: <span id="apiTotal" class="api-val" style="color: #f59e0b;">0</span></div>
    <div>🖼️ <span id="apiVision" class="api-val vision">0</span></div>
    <div>📝 <span id="apiText" class="api-val text">0</span></div>
</div>

<div class="sidebar" id="sidebar" style="overflow-y:auto;">
  <button class="sidebar-close-btn" onclick="toggleSidebar()">✖ Close Menu</button>
  <button onclick="window.location.href='index.html'">🏠 Dashboard</button>
  <button onclick="window.location.href='lens.html'">✨ Lens Studio</button>
  <button onclick="window.location.href='books.html'">📚 Book Deep Study</button>
  <button onclick="window.location.href='maths.html'" style="border-left: 4px solid var(--primary); color: var(--primary); background: var(--border-light);">🧮 Math Solver</button>
  <button onclick="window.location.href='search.html'">🔎 Deep Search</button>
  <button onclick="window.location.href='downloader.html'">📥 Video Downloader</button>
  <button onclick="window.location.href='board.html'">📋 Whiteboard</button>
  <button onclick="window.location.href='clone.html'">📑 Website Cloner</button>
  <button onclick="window.location.href='runhtml.html'">🌐 Write HTML</button>
  <button onclick="window.location.href='youtube.html'">▶️ YouTube Search</button>
  <button onclick="window.location.href='managequiz.html'">⚙️ Manage Quiz</button>
  <button onclick="window.location.href='tutor.html'">👨‍🏫 AI Tutor</button>
  <button onclick="window.location.href='quiz.html'">📝 Take a Quiz!</button>
  <button onclick="window.location.href='book_quiz.html'">📝 Take a Book Quiz!</button>
  <button onclick="window.location.href='tables.html'">🔢 Maths Table</button>
  <button onclick="window.location.href='coding.html'">💻 AI Coding</button>
  <button onclick="window.location.href='chess.html'">♟️ Chess AI</button>
  <button onclick="window.location.href='translator.html'">🎙️ Text Translator</button>
  <button onclick="window.location.href='image.html'">🖼️ Image Translator</button>
  <button onclick="window.location.href='create.html'">🎨 Image Creator</button>
  <button onclick="window.location.href='qa.html'">📚 Document Q&A</button>
  <button onclick="window.location.href='history.html'">🕒 History & Downloads</button>
  <button onclick="window.location.href='storage.html'">📂 Storage</button>
  <button onclick="window.location.href='hub.html'">🛰️ HF Command Center</button>
  <button onclick="window.location.href='live.html'">🗣️ Gemini Live</button>
</div>

<div class="page active chat-page-layout" id="mathsPage">
  <div class="chat-scroll-area" id="mathScrollArea">
    <div class="card" style="background:linear-gradient(135deg,#3b82f6,#2563eb); border:none; text-align:center; margin-bottom: 20px; flex-shrink: 0;">
        <h1 style="font-size: 24px; color: white;">🧮 Math Solver</h1>
        <p style="font-size: 14px; opacity: 0.9; color: white;">Give Answers Of Maths.</p>
    </div>
    
    <button onclick="openWhatsAppModal()" style="background: #25D366; color: white; padding: 10px 15px; border: none; border-radius: 8px; font-weight: 700; cursor: pointer; display: flex; align-items: center; gap: 8px; margin-bottom: 15px; box-shadow: 0 4px 10px rgba(37, 211, 102, 0.3);">
        <svg width="18" height="18" fill="currentColor" viewBox="0 0 24 24"><path d="M12.031 0C5.385 0 0 5.384 0 12.031c0 2.115.548 4.183 1.587 6.008L.063 24l6.104-1.6c1.782.946 3.791 1.446 5.864 1.446 6.646 0 12.031-5.385 12.031-12.031S18.677 0 12.031 0zm3.843 17.202c-.173.486-.998.922-1.428.983-.393.056-1.155.155-3.327-.743-2.618-1.082-4.296-3.75-4.425-3.924-.131-.173-1.056-1.408-1.056-2.684 0-1.277.663-1.905.897-2.164.234-.258.508-.323.679-.323.173 0 .344.004.498.01.161.008.384-.064.602.46.223.535.753 1.84.819 1.968.066.13.111.28.024.453-.087.172-.132.28-.264.432-.131.153-.277.327-.393.454-.123.134-.254.281-.107.535.147.255.653 1.081 1.402 1.748.966.86 1.763 1.132 2.015 1.261.253.13.4.111.55-.064.148-.174.636-.74.808-.992.172-.254.344-.213.576-.126.232.087 1.468.692 1.721.821.253.129.423.193.484.301.062.107.062.624-.11 1.111z"/></svg>
        Share Full Chat via WhatsApp
    </button>

    <div id="mathChatHistory"></div>
  </div>
  <div class="chat-input-area">
    <div class="image-preview-chip" id="mathPreviewChip">
        <div class="image-preview-close" id="clearMathImgBtn" onclick="clearMathImage()" style="position:absolute; top:-5px; right:-5px; background:red; color:white; border-radius:50%; width:20px; height:20px; display:flex; justify-content:center; align-items:center; font-size:12px; cursor:pointer;">✕</div>
    </div>
    
    <div class="chat-input-wrapper">
        <div style="position: relative;">
            <button class="chat-action-btn" id="attachBtn" onclick="toggleAttachMenu(event)" title="Attach" style="background: rgba(59, 130, 246, 0.2); color: #3b82f6; font-size: 24px; padding-bottom: 4px;">➕</button>
            <div id="attachMenu" style="display: none; position: absolute; bottom: 120%; left: 0; background: var(--bg-surface); border: 2px solid var(--border-light); border-radius: 16px; padding: 12px; box-shadow: 0 10px 25px rgba(0,0,0,0.3); z-index: 1000; flex-direction: column; gap: 10px; min-width: 190px; transform-origin: bottom left; animation: popInCard 0.2s ease;">
                <button onclick="document.getElementById('mathGalleryInput').click(); closeAttachMenu();" style="background: var(--input-bg); border: 1px solid var(--border-light); color: var(--text-main); font-size: 14px; cursor: pointer; text-align: left; padding: 10px 15px; border-radius: 12px; font-weight: 600;">📁 Upload via folder</button>
                <button onclick="openCamera('math'); closeAttachMenu();" style="background: var(--input-bg); border: 1px solid var(--border-light); color: var(--text-main); font-size: 14px; cursor: pointer; text-align: left; padding: 10px 15px; border-radius: 12px; font-weight: 600;">📷 Open Camera</button>
            </div>
        </div>

        <input type="file" id="mathGalleryInput" accept="image/*" style="display:none;" onchange="handleMathGallery(event)">
        <textarea id="mathInstructionInput" placeholder="Ask math question, paste or attach image... (Press Enter)"></textarea>
        <button class="chat-action-btn send" id="sendMathBtn" onclick="executeMathFlow()">↑</button>
    </div>
  </div>
</div>

<div id="cameraModal" class="cameraModal">
    <video id="cameraVideo" autoplay playsinline></video>
    <div class="camera-top-bar">
        <button id="closeCameraBtn" class="cam-icon-btn" onclick="closeCamera()">❌</button>
    </div>
    <div class="cameraControls">
        <button id="toggleFlashBtn" class="cam-icon-btn">🔦</button>
        <div id="capturePhotoBtn" class="cameraCircle" onclick="capturePhoto()"></div>
        <button id="switchCameraBtn" class="cam-icon-btn" onclick="switchCamera()">🔄</button>
    </div>
    <canvas id="captureCanvas" style="display:none;"></canvas>
</div>

<div id="whatsappModal" style="display:none; position:fixed; inset:0; background:rgba(0,0,0,0.8); z-index:999999; justify-content:center; align-items:center;">
    <div class="card" style="background:var(--bg-surface); padding:25px; border-radius:24px; border:2px solid var(--primary); max-width: 400px; width: 90%; text-align: center; position:relative;">
        <h3 style="color: var(--primary); margin-bottom: 15px; font-size: 22px; font-weight: 900;">💬 Share Full Chat</h3>
        <p style="font-size: 14px; color: var(--text-muted); margin-bottom: 20px;">Enter the authorized mobile number to send the complete history.</p>
        <input type="number" id="waNumberInput" placeholder="e.g. 919876543210" style="width: 100%; padding: 15px; border-radius: 16px; border: 2px solid var(--border-light); background: var(--input-bg); color: var(--text-main); margin-bottom: 25px; font-weight: bold; text-align: center; font-size: 18px;">
        <div style="display: flex; gap: 10px;">
            <button class="btn green" style="flex: 1; padding: 12px; border-radius: 12px; background: #10b981; color: white; border: none; font-weight: bold; cursor: pointer;" onclick="submitWhatsAppNumber(event)">🚀 Send Chat</button>
            <button class="btn red" style="flex: 1; padding: 12px; border-radius: 12px; background: #ef4444; color: white; border: none; font-weight: bold; cursor: pointer;" onclick="closeWhatsAppModal()">✖ Cancel</button>
        </div>
    </div>
</div>

<script>

    let currentMathLang = "Hindi";

    let currentMathImage = null;



    window.toggleMathLanguage = function() {

        const langText = document.getElementById('langText');

        if (currentMathLang === "Hindi") {

            currentMathLang = "English";

            langText.innerText = "EN";

            if(typeof showToast === 'function') showToast("🌍 Language changed to English");

        } else {

            currentMathLang = "Hindi";

            langText.innerText = "HI";

            if(typeof showToast === 'function') showToast("🌍 Language changed to Hindi");

        }

    };



    window.setMathImage = function(base64) {

        currentMathImage = base64;

        if (typeof window.capturedImage !== 'undefined') window.capturedImage = base64;

        const chip = document.getElementById("mathPreviewChip");

        if(chip) { chip.style.backgroundImage = `url(${base64})`; chip.style.display = "block"; }

    };



    // --- 1. PLUS MENU LOGIC ---

    window.toggleAttachMenu = function(e) {

        e.stopPropagation();

        const menu = document.getElementById('attachMenu');

        menu.style.display = menu.style.display === 'flex' ? 'none' : 'flex';

    };



    window.closeAttachMenu = function() {

        const menu = document.getElementById('attachMenu');

        if (menu) menu.style.display = 'none';

    };



    document.addEventListener('click', function(e) {

        const menu = document.getElementById('attachMenu');

        const attachBtn = document.getElementById('attachBtn');

        if (menu && menu.style.display === 'flex' && !menu.contains(e.target) && (!attachBtn || !attachBtn.contains(e.target))) {

            menu.style.display = 'none';

        }

    });



    // --- 2. AGGRESSIVE IMAGE COMPRESSION (FIXES API CRASHES) ---

    function compressAndSetImage(file) {

        const reader = new FileReader();

        reader.onload = function(e) {

            const img = new Image();

            img.onload = function() {

                const canvas = document.createElement('canvas');

                let w = img.width, h = img.height;

                

                // Shrink to max 800px to ensure the string length stays extremely small

                const MAX_SIZE = 800; 

                if (w > h && w > MAX_SIZE) { h *= MAX_SIZE / w; w = MAX_SIZE; } 

                else if (h > MAX_SIZE) { w *= MAX_SIZE / h; h = MAX_SIZE; }

                

                canvas.width = w; canvas.height = h;

                

                // Fill with white background in case of transparent PNGs

                const ctx = canvas.getContext('2d');

                ctx.fillStyle = "#ffffff";

                ctx.fillRect(0, 0, w, h);

                ctx.drawImage(img, 0, 0, w, h);

                

                // Compress to 50% JPEG Quality

                window.setMathImage(canvas.toDataURL("image/jpeg", 0.5));

                if(typeof showToast === 'function') showToast("✅ Image attached safely!");

            };

            img.src = e.target.result;

        };

        reader.readAsDataURL(file);

    }



    window.handleMathGallery = function(event) {

        if (!event.target.files[0]) return;

        compressAndSetImage(event.target.files[0]);

        event.target.value = ""; 

    };



    const mathInput = document.getElementById("mathInstructionInput");

    if(mathInput) {

        const newMathInput = mathInput.cloneNode(true);

        mathInput.parentNode.replaceChild(newMathInput, mathInput);

        

        newMathInput.addEventListener("paste", function(e) {

            const items = (e.clipboardData || e.originalEvent.clipboardData).items;

            for (let index in items) {

                if (items[index].kind === 'file' && items[index].type.startsWith('image/')) {

                    compressAndSetImage(items[index].getAsFile());

                    e.preventDefault();

                }

            }

        });

        

        newMathInput.addEventListener("keypress", (e) => { 

            if(e.key === "Enter" && !e.shiftKey) { e.preventDefault(); window.executeMathFlow(); } 

        });

    }



    window.openWhatsAppModal = function() {

        const historyDiv = document.getElementById("mathChatHistory");

        const messages = historyDiv.querySelectorAll(".chat-msg");

        

        if (messages.length === 0) {

            alert("⚠️ No chat history available to share yet! Please ask a question first.");

            return;

        }

        

        document.getElementById('whatsappModal').style.display = 'flex';

        document.getElementById('waNumberInput').focus();

    };



    window.closeWhatsAppModal = function() {

        document.getElementById('whatsappModal').style.display = 'none';

        document.getElementById('waNumberInput').value = '';

    };



    window.submitWhatsAppNumber = async function(event) {

        const number = document.getElementById('waNumberInput').value.trim();

        if (!number) return alert("Please enter a mobile number.");



        const historyDiv = document.getElementById("mathChatHistory");

        const messages = historyDiv.querySelectorAll(".chat-msg");

        

        if (messages.length === 0) {

            alert("⚠️ No chat history to share.");

            return;

        }



        let fullChatText = "";



        messages.forEach(msg => {

            const isUser = msg.classList.contains("chat-user");

            const bubble = msg.querySelector(".bubble");

            if (!bubble) return;



            let clone = bubble.cloneNode(true);

            

            const garbageElements = clone.querySelectorAll('.action-buttons-container, .api-badge, button, select, .retry-dropdown, div[style*="border-top"]');

            garbageElements.forEach(el => el.remove());

            

            let htmlStr = clone.innerHTML;

            htmlStr = htmlStr.replace(/<br\s*[\/]?>/gi, '\n');

            htmlStr = htmlStr.replace(/<\/div>/gi, '\n');

            htmlStr = htmlStr.replace(/<\/p>/gi, '\n\n');

            

            clone.innerHTML = htmlStr;

            let msgText = clone.textContent || clone.innerText || "";

            msgText = msgText.replace(/\n\s*\n/g, '\n\n').trim();



            if (msgText.length > 0) {

                if (isUser) {

                    fullChatText += `👤 *YOU:* ${msgText}\n\n`;

                } else {

                    fullChatText += `🤖 *AI SOLVER:* \n${msgText}\n\n---------------------------------\n\n`;

                }

            }

        });



        const superscripts = {

            '0':'⁰','1':'¹','2':'²','3':'³','4':'⁴','5':'⁵','6':'⁶','7':'⁷','8':'⁸','9':'⁹',

            '+':'⁺','-':'⁻','=':'⁼','(':'⁽',')':'⁾','a':'ᵃ','b':'ᵇ','c':'ᶜ','n':'ⁿ','x':'ˣ','y':'ʸ'

        };

        const toSuper = (str) => str.split('').map(c => superscripts[c] || c).join('');



        fullChatText = fullChatText

            .replace(/\$\$/g, '') 

            .replace(/\$/g, '')   

            .replace(/\\\(/g, '') 

            .replace(/\\\)/g, '') 

            .replace(/\\frac{([^}]+)}{([^}]+)}/g, '$1/$2') 

            .replace(/\^\{([^}]+)\}/g, (m, p1) => toSuper(p1)) 

            .replace(/\^([0-9a-zA-Z+-]+)/g, (m, p1) => toSuper(p1)) 

            .replace(/\\sqrt{([^}]+)}/g, '√$1') 

            .replace(/\\times/g, '×') 

            .replace(/\\cdot/g, '·')  

            .replace(/\\pi/g, 'π')    

            .replace(/\\theta/g, 'θ') 

            .replace(/\\left/g, '')   

            .replace(/\\right/g, '')

            .replace(/\\/g, '');      



        const btn = event.target;

        const originalText = btn.innerHTML;

        btn.innerHTML = "⏳ Sending Full Chat...";

        btn.style.opacity = "0.7";

        btn.disabled = true;



        try {

            const response = await fetch("/api/secure-whatsapp", {

                method: 'POST',

                headers: { 'Content-Type': 'application/json' },

                body: JSON.stringify({

                    number: number,

                    message: "📚 *AI Pro Suite - Complete Session Export* 📚\n\n" + fullChatText.trim()

                })

            });



            if (response.status === 403) {

                alert("ERROR - 324 (UNAUTHORIZED PROTECTION)");

            } else if (response.ok) {

                alert("✅ Full chat history sent successfully to authorized device!");

                closeWhatsAppModal();

            } else {

                alert("❌ Failed to send. Server responded with an error.");

            }

        } catch (e) {

            alert("❌ Connection error. Is your server running?");

        }



        btn.innerHTML = originalText;

        btn.style.opacity = "1";

        btn.disabled = false;

    };

    

    function capturePhoto() { 

        const v = document.getElementById("cameraVideo"), c = document.getElementById("captureCanvas");

        let w = v.videoWidth, h = v.videoHeight; 

        

        // Match the 800px logic from the upload compressor to ensure API stability

        if(w > 800) { h *= 800/w; w = 800; } 

        

        c.width = w; c.height = h; 

        c.getContext("2d").drawImage(v, 0, 0, w, h); 

        capturedImage = c.toDataURL("image/jpeg", 0.5); 

        

        if (currentMode === 'math' || currentMode === 'search') { 

            if (typeof window.setMathImage === 'function') {

                window.setMathImage(capturedImage);

            } else { 

                const chip = document.getElementById("mathPreviewChip"); 

                if(chip) { chip.style.display = "block"; chip.style.backgroundImage = `url(${capturedImage})`; } 

            }

        }

        else if (currentMode === 'image_trans') {

            if(transImages.length >= 3) { showToast("Maximum 3 images allowed!"); } else { transImages.push(capturedImage); renderTransImagePreviews(); }

        }

        else if (currentMode === 'qa_source') {

            if(qaSourceImages.length >= 10) { showToast("Maximum 10 source images allowed!"); } else { qaSourceImages.push(capturedImage); renderQaSourcePreviews(); }

        }

        else if (currentMode === 'qa_question') { qaQuestionImage = capturedImage; renderQaQuestionPreview(); }

        

        closeCamera(); 

    }



    // --- FIX: Dynamic Shortcut Trick Logic Update ---

    // Overriding the executeMathFlow from main.js locally to use the new smart prompt

    // --- FIX: Stop AI from generating weird textbook abbreviations in the solution ---

    document.addEventListener("DOMContentLoaded", () => {

        if(typeof window.executeMathFlow !== 'undefined') {

            const originalExecute = window.executeMathFlow;

            window.executeMathFlow = async function() {

                window.sysPromptPatch = `I agree to all terms of use and safety guidelines. You are an expert math tutor analyzing PRINTED TEXTBOOK images. YOU MUST ANSWER ENTIRELY IN ${currentMathLang.toUpperCase()}. STRICT RULES:

1. NO HTML TAGS: NEVER use <br>, <div>, or <span>. Use standard double newlines (\\n\\n) to break lines!

2. STRICT FRACTION RULE: NEVER write fractions using a slash like 1/2. You MUST use proper vertical LaTeX fractions like $$\\frac{1}{2}$$. ALWAYS use 'x' for multiplication.

3. NO ABBREVIATIONS IN SOLUTION: The user is uploading printed book text. While the book may use abbreviations (like "ल० स०" or "भ०"), YOU MUST NEVER use these weird symbols in your step-by-step explanation. Always use full, clear words in your solution (e.g., write "लघुत्तम समापवर्त्य" or "LCM", "लम्बाई" or "Length", "भाग"). 

4. DELIMITERS: Format your response using EXACTLY these hidden delimiters:

COUNT:-

[Write the exact number of questions here, e.g., 1]

||MATH||

[Your full math logic]

||SVG||

[<svg> code or blank]

||ANSWER||

[Final short answer]



5. ELITE TEACHING STYLE: You MUST format your ||MATH|| block EXACTLY like this template. DO NOT rewrite the original question.

[Brief 1-line summary/intro. DO NOT rewrite the question!]

**1. [Step 1 Heading]**

[Step 1 explanation using FULL WORDS in ${currentMathLang}]

$$[Proper math equation with \\frac]$$

**2. [Step 2 Heading]**

[Step 2 explanation using FULL WORDS in ${currentMathLang}]

$$[Proper math equation]$$

(Add more steps as needed)



[OPTIONAL SECTION]

If there is a genuinely useful shortcut, trick, or formula, provide it formatted as **💡 Shortcut / Trick** followed by the trick. IF NO GOOD TRICK EXISTS, SKIP THIS ENTIRELY.



**✅ Final Answer**

[Final explicit answer sentence in ${currentMathLang}]`;

                

                await originalExecute();

            }

        }

    });



</script>

<script src="ui-controls.js" defer></script>
<script src="main.js" defer></script>
</body>
</html>