File size: 36,586 Bytes
3a4bdd3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
565a379
 
 
3a4bdd3
565a379
 
 
3a4bdd3
 
565a379
 
4b022cd
565a379
3a4bdd3
 
565a379
 
3a4bdd3
 
 
 
 
 
 
 
565a379
 
 
 
3a4bdd3
565a379
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c00b41f
 
565a379
c00b41f
 
 
565a379
3a4bdd3
 
 
 
565a379
 
c00b41f
565a379
c00b41f
565a379
 
 
 
3a4bdd3
 
 
 
 
 
 
ac078d2
3a4bdd3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
565a379
c14a92e
3a4bdd3
 
 
 
 
 
 
 
c14a92e
3a4bdd3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c14a92e
3a4bdd3
 
 
 
 
 
ac078d2
3a4bdd3
 
ac078d2
3a4bdd3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c14a92e
3a4bdd3
 
c14a92e
 
3a4bdd3
c14a92e
3a4bdd3
c43db9b
3a4bdd3
c14a92e
3a4bdd3
 
 
 
c14a92e
3a4bdd3
c14a92e
565a379
3a4bdd3
c14a92e
3a4bdd3
 
 
 
 
 
c14a92e
3a4bdd3
c14a92e
565a379
3a4bdd3
c14a92e
3a4bdd3
 
 
 
 
 
 
 
 
 
c14a92e
3a4bdd3
c14a92e
 
3a4bdd3
c14a92e
3a4bdd3
c43db9b
3a4bdd3
c14a92e
3a4bdd3
c14a92e
3a4bdd3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c14a92e
cc01c7a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
565a379
3a4bdd3
 
 
565a379
3a4bdd3
 
 
 
565a379
 
c00b41f
4b022cd
c00b41f
3a4bdd3
565a379
3a4bdd3
565a379
3a4bdd3
565a379
3a4bdd3
 
 
 
 
 
 
565a379
 
 
3a4bdd3
565a379
3a4bdd3
 
 
 
 
 
 
565a379
3a4bdd3
 
 
 
 
 
 
565a379
 
 
4b022cd
 
 
 
 
 
 
 
 
 
 
 
 
 
c14a92e
 
3a4bdd3
c14a92e
 
c00b41f
3a4bdd3
565a379
 
 
3a4bdd3
c00b41f
 
565a379
c00b41f
3a4bdd3
 
c00b41f
 
565a379
 
c14a92e
3a4bdd3
 
c14a92e
 
3a4bdd3
 
c14a92e
3a4bdd3
c00b41f
565a379
3a4bdd3
565a379
3a4bdd3
c00b41f
565a379
3a4bdd3
565a379
3a4bdd3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
ac078d2
 
 
 
 
 
 
 
3a4bdd3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
565a379
3a4bdd3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c14a92e
565a379
 
3a4bdd3
 
 
 
c00b41f
3a4bdd3
 
 
 
cc2fef3
3a4bdd3
a0f64e8
 
 
 
 
 
3a4bdd3
 
 
 
 
 
 
 
 
 
 
a0f64e8
 
 
 
 
 
 
 
 
565a379
3a4bdd3
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
565a379
e1c762e
3a4bdd3
c00b41f
565a379
ed6c1cd
565a379
3a4bdd3
565a379
 
3a4bdd3
 
 
c00b41f
c14a92e
 
 
3a4bdd3
 
 
565a379
3a4bdd3
565a379
 
3a4bdd3
 
 
 
c00b41f
565a379
 
 
 
3a4bdd3
 
 
565a379
 
3a4bdd3
 
 
 
 
 
e1c762e
 
 
 
 
 
 
3a4bdd3
 
 
 
565a379
3a4bdd3
ed6c1cd
565a379
 
c00b41f
3a4bdd3
 
 
c00b41f
3a4bdd3
 
 
 
c14a92e
3a4bdd3
 
 
 
 
 
 
 
c14a92e
3a4bdd3
 
c14a92e
3a4bdd3
c14a92e
 
 
3a4bdd3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
"""
frontend/app.py β€” MathMinds AI Streamlit frontend.

STRUCTURE
─────────
  1. CONFIG & CONSTANTS     β€” env vars, page config, CSS
  2. SESSION STATE          β€” defaults, init, clear
  3. API LAYER              β€” all HTTP calls, no st.* calls, returns plain dicts
  4. RENDER HELPERS         β€” pure display functions, never mutate state
  5. SESSION MANAGEMENT     β€” state mutations, never call st.rerun()
  6. CHAT INTERFACE         β€” the 3-state machine (idle / processing / done)
  7. MAIN ENTRY             β€” auth gate + router

STATE MACHINE (why the previous version had bugs)
──────────────────────────────────────────────────
Every bug in the old version came from collapsing 3 distinct states into one
render pass: add user message + call API + render answer all happened together.

The fix is a strict 3-state machine:

  IDLE        β†’ render history + show input
                user submits β†’ _add_message("user") β†’ is_processing=True β†’ rerun()

  PROCESSING  β†’ render history (user question VISIBLE above spinner)
                make API call β†’ _add_message("assistant") β†’ is_processing=False β†’ rerun()

  IDLE again  β†’ render history (assistant answer now visible)

Rules that make this impossible to break:
  - st.rerun() is ALWAYS the last statement β€” never inside a with-block
  - The API call NEVER happens inside with st.chat_message()
  - Render helpers never write session_state
  - load_messages() only called on session switch, never after an answer
"""

import streamlit as st
import requests
import base64
import logging
import io
import os
import time
import uuid
from PIL import Image
from streamlit_drawable_canvas import st_canvas
from dotenv import load_dotenv
from firebase_utils import sign_in_with_email, sign_up_with_email, send_password_reset_email

logging.basicConfig(level=logging.INFO)
logger = logging.getLogger(__name__)
load_dotenv()


# ══════════════════════════════════════════════════════════════════════════════
# 1. CONFIG & CONSTANTS
# ══════════════════════════════════════════════════════════════════════════════

BACKEND_URL = os.getenv("BACKEND_URL", "http://localhost:8000")
ENABLE_AUTH = os.getenv("ENABLE_AUTH", "True").lower() == "true"

st.set_page_config(
    page_title="MathMinds AI",
    page_icon="🧠",
    layout="wide",
    initial_sidebar_state="expanded",
)

st.markdown("""
<style>
    .stApp {
        background: radial-gradient(circle at 10% 20%, rgb(17 24 39) 0%, rgb(10 10 10) 90%);
        font-family: 'Inter', sans-serif;
    }
    section[data-testid="stSidebar"] {
        background-color: rgba(30, 41, 59, 0.4);
        backdrop-filter: blur(12px);
        border-right: 1px solid rgba(255, 255, 255, 0.1);
    }
    div[data-testid="stChatMessageUser"] {
        background: linear-gradient(135deg, #6366f1 0%, #a855f7 100%);
        border-radius: 20px 20px 4px 20px;
        padding: 1rem 1.5rem;
        box-shadow: 0 4px 15px rgba(0,0,0,0.1);
        border: 1px solid rgba(255,255,255,0.1);
    }
    div[data-testid="stChatMessageAssistant"] {
        background: rgba(31, 41, 55, 0.7);
        backdrop-filter: blur(10px);
        border-radius: 20px 20px 20px 4px;
        padding: 1rem 1.5rem;
        border: 1px solid rgba(255,255,255,0.05);
        box-shadow: 0 4px 6px -1px rgba(0, 0, 0, 0.1);
    }
    h1, h2, h3 { color: #f3f4f6; letter-spacing: -0.5px; }
    p, li { color: #e5e7eb; line-height: 1.6; }
    .badge {
        display: inline-flex; align-items: center;
        padding: 0.25rem 0.75rem; border-radius: 9999px;
        font-size: 0.75rem; font-weight: 600; margin-right: 0.5rem;
    }
    .badge-blue   { background:rgba(59,130,246,0.2);  color:#93c5fd; border:1px solid rgba(59,130,246,0.3); }
    .badge-purple { background:rgba(168,85,247,0.2);  color:#d8b4fe; border:1px solid rgba(168,85,247,0.3); }
    .badge-green  { background:rgba(34,197,94,0.2);   color:#86efac; border:1px solid rgba(34,197,94,0.3); }
    .badge-red    { background:rgba(239,68,68,0.2);   color:#fca5a5; border:1px solid rgba(239,68,68,0.3); }
    button[kind="primary"] {
        background: linear-gradient(to right, #4f46e5, #7c3aed);
        border: none; box-shadow: 0 4px 6px -1px rgba(79,70,229,0.3); transition: all 0.2s;
    }
    button[kind="primary"]:hover { transform: translateY(-1px); }
</style>
""", unsafe_allow_html=True)


# ══════════════════════════════════════════════════════════════════════════════
# 2. SESSION STATE
# ══════════════════════════════════════════════════════════════════════════════

_DEFAULTS = {
    "user":                None,
    "current_view":        "Chat",
    "current_mode":        "solve",
    "chat_sessions":       [],
    "active_session_id":   None,
    "messages":            [],
    "loaded_for_user":     None,
    "loaded_for_session":  None,
    "is_processing":       False,
    "renaming_session_id": None,
    "canvas_key":          "main_canvas",
}

for _k, _v in _DEFAULTS.items():
    if _k not in st.session_state:
        st.session_state[_k] = _v

# Dev mode bypass
if not ENABLE_AUTH and st.session_state.user is None:
    st.session_state.user = {
        "email": "dev@mathminds.ai",
        "token": "mock_dev_token",
        "uid":   "dev_user_123",
    }


def _reset_state(keep_user=False):
    """Clear all state. Optionally preserve the user object."""
    saved_user = st.session_state.user if keep_user else None
    for k, v in _DEFAULTS.items():
        st.session_state[k] = v
    st.session_state.canvas_key = f"canvas_{uuid.uuid4()}"
    if keep_user:
        st.session_state.user = saved_user


def _get_headers() -> dict:
    u = st.session_state.user
    return {"Authorization": f"Bearer {u['token']}"} if u and "token" in u else {}


def _add_message(role: str, content, **kwargs):
    msg = {"role": role, "content": content, "timestamp": time.time()}
    msg.update(kwargs)
    st.session_state.messages.append(msg)


def _is_blank_canvas(image_data) -> bool:
    if image_data is None:
        return True
    import numpy as np
    return image_data[:, :, 3].max() == 0


# ══════════════════════════════════════════════════════════════════════════════
# 3. API LAYER
# ─────────────────────────────────────────────────────────────────────────────
# Rules:
#   - NO st.* calls anywhere in this section
#   - Returns plain dict, always has "ok" key
#   - Never raises β€” exceptions are caught and returned as {"ok": False}
# ══════════════════════════════════════════════════════════════════════════════

@st.cache_data(ttl=30)
def api_health() -> dict:
    """Cached β€” only hits the backend once every 30 seconds, not on every rerender."""
    try:
        r = requests.get(f"{BACKEND_URL}/health", timeout=5)
        return {"ok": r.status_code == 200, **r.json()} if r.status_code == 200 else {"ok": False}
    except Exception:
        return {"ok": False}


def api_solve(text: str, image, session_id: str, request_id: str, mode: str = "solve") -> dict:
    try:
        r = requests.post(
            f"{BACKEND_URL}/{mode}",
            json={"text": text, "image": image, "session_id": session_id, "request_id": request_id},
            headers=_get_headers(),
            timeout=360,
        )
        if r.status_code == 200:
            return {"ok": True, **r.json()}
        if r.status_code == 401:
            return {"ok": False, "error": "AUTH_EXPIRED"}
        if r.status_code == 429:
            return {"ok": False, "error": "Daily limit reached. Please try again tomorrow."}
        return {"ok": False, "error": f"Backend error {r.status_code}"}
    except requests.exceptions.ConnectionError:
        return {"ok": False, "error": "Cannot reach backend. Is the server running?"}
    except requests.exceptions.Timeout:
        return {"ok": False, "error": "Request timed out."}
    except Exception as e:
        logger.error(f"api_solve: {e}")
        return {"ok": False, "error": "Unexpected error. Please try again."}


def api_load_messages(session_id: str) -> dict:
    try:
        r = requests.get(
            f"{BACKEND_URL}/chat/sessions/{session_id}/messages",
            headers=_get_headers(), timeout=30,
        )
        if r.status_code == 200:   return {"ok": True,  "messages": r.json()}
        if r.status_code == 404:   return {"ok": False, "error": "SESSION_NOT_FOUND"}
        if r.status_code == 401:   return {"ok": False, "error": "AUTH_EXPIRED"}
        return {"ok": False, "error": f"Status {r.status_code}"}
    except Exception as e:
        return {"ok": False, "error": str(e)}


def api_load_sessions() -> dict:
    try:
        r = requests.get(f"{BACKEND_URL}/chat/sessions", headers=_get_headers(), timeout=10)
        if r.status_code == 200:   return {"ok": True,  "sessions": r.json()}
        if r.status_code == 401:   return {"ok": False, "error": "AUTH_EXPIRED"}
        return {"ok": False, "error": f"Status {r.status_code}"}
    except requests.exceptions.ConnectionError:
        return {"ok": False, "error": "BACKEND_OFFLINE"}
    except Exception as e:
        return {"ok": False, "error": str(e)}


def api_new_session() -> dict:
    try:
        r = requests.post(f"{BACKEND_URL}/chat/sessions", headers=_get_headers(), timeout=30)
        return {"ok": True, "session": r.json()} if r.status_code == 200 else {"ok": False, "error": f"Status {r.status_code}"}
    except Exception as e:
        return {"ok": False, "error": str(e)}


def api_delete_session(sid: str) -> dict:
    try:
        r = requests.delete(f"{BACKEND_URL}/chat/sessions/{sid}", headers=_get_headers(), timeout=30)
        return {"ok": r.status_code == 200}
    except Exception as e:
        return {"ok": False, "error": str(e)}


def api_rename_session(sid: str, title: str) -> dict:
    try:
        r = requests.patch(
            f"{BACKEND_URL}/chat/sessions/{sid}",
            json={"title": title}, headers=_get_headers(), timeout=30,
        )
        return {"ok": r.status_code == 200}
    except Exception as e:
        return {"ok": False, "error": str(e)}


# ══════════════════════════════════════════════════════════════════════════════
# 4. RENDER HELPERS
# ─────────────────────────────────────────────────────────────────────────────
# Rules:
#   - Only READ session_state, never write
#   - Only call st.* display functions
#   - Never call st.rerun() or make API calls
# ══════════════════════════════════════════════════════════════════════════════

def _badge(source: str, status: str) -> str:
    b = ""
    if source == "sympy_preflight":
        b += '<span class="badge badge-green">⚑ INSTANT</span>'
    elif source in ("cache", "semantic_cache"):
        b += '<span class="badge badge-blue">πŸ’Ύ CACHED</span>'
    elif source in ("google_adk_agent", "agent"):
        b += '<span class="badge badge-purple">πŸ€– AI</span>'
    if status == "error":
        b += '<span class="badge badge-red">πŸ”΄ ERROR</span>'
    return b


def _render_message(msg: dict):
    role = msg.get("role", "assistant")
    with st.chat_message(role, avatar="πŸ‘€" if role == "user" else "πŸ€–"):
        if role == "user":
            if msg.get("image_data"):
                try:
                    st.image(base64.b64decode(msg["image_data"]), width=300)
                except Exception:
                    pass
            st.write(msg.get("content", ""))
        else:
            meta   = msg.get("metadata") or {}
            status = meta.get("status") or msg.get("status", "success")
            badges = _badge(meta.get("source", ""), status)
            if badges:
                st.markdown(badges, unsafe_allow_html=True)

            logic = meta.get("logic_trace") or msg.get("reasoning")
            if logic:
                steps = [s for s in (logic if isinstance(logic, list) else logic.split("\n")) if s]
                if steps:
                    with st.expander("πŸ’­ Reasoning", expanded=False):
                        for step in steps:
                            st.caption(step)

            content = msg.get("content", "")
            if status == "error":
                st.error(content)
            elif isinstance(content, dict) and "final_answer" in content:
                st.markdown(content["final_answer"])
            else:
                st.markdown(str(content))

            # --- EXPORT BUTTONS ---
            if role == "assistant" and status != "error" and content:
                try:
                    from export_utils import generate_latex, compile_pdf
                    
                    msg_id = msg.get("request_id") or str(int(msg.get("timestamp", 0)))
                    pdf_state_key = f"pdf_bytes_{msg_id}"
                    
                    with st.expander("πŸ“₯ Export Solution (LaTeX / PDF)"):
                        tex_str = generate_latex(str(content))
                        c1, c2 = st.columns(2)
                        
                        with c1:
                            st.download_button(
                                label="πŸ“„ Download .tex Source",
                                data=tex_str,
                                file_name=f"mathminds_{msg_id[:8]}.tex",
                                mime="text/plain",
                                key=f"dl_tex_{msg_id}"
                            )
                        
                        with c2:
                            if pdf_state_key not in st.session_state:
                                if st.button("πŸ› οΈ Generate PDF", key=f"btn_compile_{msg_id}"):
                                    with st.spinner("Compiling PDF via remote TeX Live..."):
                                        pdf_data = compile_pdf(tex_str)
                                        if pdf_data:
                                            st.session_state[pdf_state_key] = pdf_data
                                            st.rerun()
                                        else:
                                            st.error("❌ Failed to compile PDF from external API.")
                            else:
                                st.download_button(
                                    label="βœ… Click to Save PDF",
                                    data=st.session_state[pdf_state_key],
                                    file_name=f"mathminds_{msg_id[:8]}.pdf",
                                    mime="application/pdf",
                                    key=f"dl_pdf_{msg_id}"
                                )
                except Exception as e:
                    logger.error(f"Render export error: {e}")


def _render_login():
    _, c, _ = st.columns([1, 2, 1])
    with c:
        st.markdown("""
        <div style="text-align:center;padding:3rem 4rem;background:rgba(255,255,255,0.05);
                    border-radius:20px;border:1px solid rgba(255,255,255,0.1);margin:2rem 0;">
            <h1 style="margin:0">🧠 MathMinds AI</h1>
            <p style="color:#9ca3af;margin-top:0.5rem;">Your intelligent math assistant.</p>
        </div>
        """, unsafe_allow_html=True)

        tab_in, tab_up, tab_reset = st.tabs(["Login", "Sign Up", "Reset Password"])

        with tab_in:
            with st.form("login_form"):
                email    = st.text_input("Email",    placeholder="student@university.edu")
                password = st.text_input("Password", type="password")
                if st.form_submit_button("Sign In", use_container_width=True, type="primary"):
                    if email and password:
                        token, uid, user_email, error = sign_in_with_email(email, password)
                        if token:
                            _reset_state()
                            st.session_state.user = {"email": user_email, "token": token, "uid": uid}
                            st.rerun()
                        else:
                            st.error(f"Login failed: {error}")
                    else:
                        st.error("Please enter email and password.")

        with tab_up:
            with st.form("signup_form"):
                new_email = st.text_input("Email",           placeholder="new@student.edu")
                new_pass  = st.text_input("Password",        type="password")
                confirm   = st.text_input("Confirm Password", type="password")
                if st.form_submit_button("Create Account", use_container_width=True, type="primary"):
                    if new_email and new_pass:
                        if new_pass != confirm:
                            st.error("Passwords do not match.")
                        else:
                            token, uid, user_email, error = sign_up_with_email(new_email, new_pass)
                            if token:
                                _reset_state()
                                st.session_state.user = {"email": user_email, "token": token, "uid": uid}
                                st.rerun()
                            else:
                                st.error(f"Sign up failed: {error}")
                    else:
                        st.error("Please fill all fields.")

        with tab_reset:
            with st.form("reset_form"):
                st.markdown("Enter your email address to receive a secure password reset link.")
                reset_email = st.text_input("Email", placeholder="student@university.edu", key="reset_email_input")
                if st.form_submit_button("Send Reset Link", use_container_width=True, type="primary"):
                    if reset_email:
                        success, error = send_password_reset_email(reset_email)
                        if success:
                            st.success("βœ… Password reset link sent! Please check your email inbox.")
                        else:
                            st.error(f"Failed to send reset link: {error}")
                    else:
                        st.error("Please enter your email.")

        st.markdown(
            "<p style='text-align:center;font-size:0.8rem;color:#6b7280;'>Powered by Gemini & SymPy</p>",
            unsafe_allow_html=True,
        )


def _render_profile():
    st.title("πŸ‘€ User Profile")
    if "profile_data" not in st.session_state:
        try:
            r = requests.get(f"{BACKEND_URL}/users/profile", headers=_get_headers(), timeout=30)
            st.session_state.profile_data = r.json() if r.status_code == 200 else {}
        except Exception:
            st.session_state.profile_data = {}

    data          = st.session_state.profile_data
    levels        = ["High School", "Undergraduate", "Graduate", "Researcher"]
    interests_all = ["Algebra", "Calculus", "Geometry", "Statistics", "Physics", "Computer Science", "Finance"]

    with st.form("profile_form"):
        display_name = st.text_input("Display Name", value=data.get("display_name", ""))
        math_level   = st.selectbox(
            "Math Level", levels,
            index=levels.index(data["math_level"]) if data.get("math_level") in levels else 1,
        )
        interests = st.multiselect(
            "Interests", interests_all,
            default=[i for i in data.get("interests", []) if i in interests_all],
        )
        if st.form_submit_button("Save", use_container_width=True, type="primary"):
            payload = {"display_name": display_name, "math_level": math_level, "interests": interests}
            try:
                r = requests.post(f"{BACKEND_URL}/users/profile", json=payload, headers=_get_headers())
                if r.status_code == 200:
                    st.success("Saved!")
                    st.session_state.profile_data = payload
                else:
                    st.error(f"Failed: {r.text}")
            except Exception as e:
                st.error(str(e))


# ══════════════════════════════════════════════════════════════════════════════
# 5. SESSION MANAGEMENT
# ─────────────────────────────────────────────────────────────────────────────
# These functions mutate session_state but never call st.rerun().
# Callers decide when to rerun.
# ══════════════════════════════════════════════════════════════════════════════

def _refresh_sessions():
    result = api_load_sessions()
    if result["ok"]:
        st.session_state.chat_sessions   = result["sessions"]
        st.session_state.loaded_for_user = st.session_state.user["uid"]
    elif result.get("error") == "AUTH_EXPIRED":
        _reset_state()
    elif result.get("error") == "BACKEND_OFFLINE":
        st.info("βŒ› Backend warming up...")
        st.stop()


def _switch_session(sid: str):
    """Load messages for a session. Only fetches if session actually changed."""
    if st.session_state.loaded_for_session == sid:
        st.session_state.active_session_id = sid
        return

    result = api_load_messages(sid)
    if result["ok"]:
        st.session_state.active_session_id  = sid
        st.session_state.messages           = result["messages"]
        st.session_state.loaded_for_session = sid
    elif result.get("error") == "SESSION_NOT_FOUND":
        st.warning("Session not found.")
        st.session_state.active_session_id = None
        st.session_state.messages          = []
    elif result.get("error") == "AUTH_EXPIRED":
        _reset_state()


def _ensure_session() -> bool:
    """
    Make sure there's an active session. Returns True if ready, False if rerun needed.
    """
    if st.session_state.active_session_id:
        return True

    sessions = st.session_state.chat_sessions
    if sessions:
        _switch_session(sessions[0]["session_id"])
        return True

    # Create first session
    result = api_new_session()
    if result["ok"]:
        sess = result["session"]
        st.session_state.active_session_id  = sess["session_id"]
        st.session_state.messages           = []
        st.session_state.loaded_for_session = sess["session_id"]
        _refresh_sessions()
        return False  # caller must rerun
    return True


def _render_sidebar():
    with st.sidebar:
        st.markdown("### 🧠 MathMinds")
        st.caption(f"πŸ‘€ {st.session_state.user['email']}")

        view = st.radio("Nav", ["Chat", "Profile"],
                        index=0 if st.session_state.current_view == "Chat" else 1,
                        label_visibility="collapsed")
        if view != st.session_state.current_view:
            st.session_state.current_view = view
            st.rerun()

        st.divider()
        st.markdown("#### Mode")
        mode_options = {"solve": "⚑ Solver", "analyze": "πŸ” Analyzer", "tutor": "πŸŽ“ Tutor"}
        selected = st.selectbox("Select Agent", options=list(mode_options.keys()), format_func=lambda x: mode_options[x], label_visibility="collapsed")
        if selected != st.session_state.current_mode:
            st.session_state.current_mode = selected
            st.rerun()

        # Health
        st.divider()
        health = api_health()
        if health.get("ok"):
            st.markdown('<div class="badge badge-green">● Online</div>', unsafe_allow_html=True)
            svc = health.get("services", {})
            if svc.get("redis") != "healthy":
                st.warning(f"Redis: {svc.get('redis')}")
            if svc.get("mongodb") != "healthy":
                st.warning(f"MongoDB: {svc.get('mongodb')}")
        else:
            st.markdown('<div class="badge badge-red">● Offline</div>', unsafe_allow_html=True)

        # Stuck lock reset
        if st.session_state.is_processing:
            st.divider()
            if st.button("πŸ”“ Reset Lock", help="Use if UI appears stuck"):
                st.session_state.is_processing = False
                st.rerun()

        st.divider()

        if st.session_state.current_view == "Chat":
            if st.button("βž• New Chat", use_container_width=True, type="primary"):
                result = api_new_session()
                if result["ok"]:
                    sess = result["session"]
                    st.session_state.active_session_id  = sess["session_id"]
                    st.session_state.messages           = []
                    st.session_state.loaded_for_session = sess["session_id"]
                    _refresh_sessions()
                    st.rerun()
                else:
                    st.error("Failed to create session.")

            st.markdown("#### History")
            for sess in st.session_state.chat_sessions:
                sid   = sess["session_id"]
                title = sess.get("title", "Chat")
                cols  = st.columns([0.78, 0.11, 0.11])

                with cols[0]:
                    active = st.session_state.active_session_id == sid
                    if st.button(title, key=f"s_{sid}", use_container_width=True,
                                 type="primary" if active else "secondary"):
                        _switch_session(sid)
                        st.rerun()

                with cols[1]:
                    if st.button("✏️", key=f"r_{sid}"):
                        st.session_state.renaming_session_id = (
                            sid if st.session_state.renaming_session_id != sid else None
                        )
                        st.rerun()

                with cols[2]:
                    if st.button("πŸ—‘οΈ", key=f"d_{sid}"):
                        api_delete_session(sid)
                        if st.session_state.active_session_id == sid:
                            st.session_state.active_session_id  = None
                            st.session_state.messages           = []
                            st.session_state.loaded_for_session = None
                        _refresh_sessions()
                        st.rerun()

                if st.session_state.renaming_session_id == sid:
                    new_title = st.text_input("Title", value=title,
                                              key=f"ti_{sid}", label_visibility="collapsed")
                    if st.button("Save", key=f"sv_{sid}", use_container_width=True):
                        api_rename_session(sid, new_title)
                        st.session_state.renaming_session_id = None
                        _refresh_sessions()
                        st.rerun()

        st.divider()
        if st.button("Sign Out", use_container_width=True):
            _reset_state()
            st.rerun()


# ══════════════════════════════════════════════════════════════════════════════
# 6. CHAT INTERFACE β€” 3-STATE MACHINE
# ══════════════════════════════════════════════════════════════════════════════

def chat_interface():

    # Ensure active session exists
    if not _ensure_session():
        st.rerun()
        return

    active_sid  = st.session_state.active_session_id
    active_sess = next((s for s in st.session_state.chat_sessions
                        if s["session_id"] == active_sid), None)
    st.title(active_sess["title"] if active_sess else "Chat")

    # ── ALWAYS render history first ────────────────────────────────────────
    # Wrap history in a fixed-height scrolling container so the page doesn't grow
    # infinitely, keeping the tabs and sidebar perfectly frozen.
    chat_container = st.container(height=500, border=False)
    with chat_container:
        for msg in st.session_state.messages:
            _render_message(msg)

    # ══════════════════════════════════════════════════════════════════════
    # STATE: PROCESSING
    # Entered when: is_processing=True (user just submitted, prev rerun set flag)
    # Exit: add assistant message β†’ is_processing=False β†’ rerun
    # ══════════════════════════════════════════════════════════════════════
    if st.session_state.is_processing:
        last       = st.session_state.messages[-1]
        request_id = str(uuid.uuid4())

        # Spinner appears BELOW the rendered history (user question visible above)
        with chat_container:
            with st.spinner("Thinking..."):
                result = api_solve(
                    text       = last.get("content", ""),
                    image      = last.get("image_data"),
                    session_id = active_sid,
                    request_id = request_id,
                    mode       = st.session_state.current_mode,
                )

        # Auth expired β€” logout
        if result.get("error") == "AUTH_EXPIRED":
            _reset_state()
            st.rerun()
            return

        # Add assistant response to state
        if result["ok"]:
            answer   = result.get("answer") or "No answer received."
            metadata = result.get("metadata") or {}
            _add_message(
                "assistant", answer,
                request_id = request_id,
                metadata   = {**metadata, "status": result.get("status", "success")},
            )
        else:
            _add_message(
                "assistant", f"⚠️ {result.get('error', 'Unknown error')}",
                request_id = request_id,
                metadata   = {"status": "error"},
            )

        # Transition β†’ IDLE
        # st.rerun() is the LAST statement, outside all with-blocks
        st.session_state.is_processing = False
        st.rerun()
        return

    # ══════════════════════════════════════════════════════════════════════
    # STATE: IDLE
    # Show input. If user submits, transition to PROCESSING.
    # ══════════════════════════════════════════════════════════════════════
    st.divider()
    tab_text, tab_draw, tab_upload, tab_camera = st.tabs(["πŸ’¬ Text", "✏️ Draw", "πŸ“€ Upload", "πŸ“· Camera"])

    prompt    = None
    image_b64 = None

    with tab_text:
        prompt = st.chat_input("Ask a math question...")

    with tab_draw:
        col_c, col_ctrl = st.columns([3, 1])
        with col_c:
            canvas = st_canvas(
                stroke_width=3, stroke_color="#FFFFFF", background_color="#000000",
                height=300, width=600, drawing_mode="freedraw",
                key=st.session_state.canvas_key,
            )
            draw_q = st.text_input("Question (optional)", placeholder="Solve this...",
                                   key="draw_q")
        with col_ctrl:
            st.caption("Controls")
            if st.button("πŸ—‘οΈ Clear"):
                st.session_state.canvas_key = f"canvas_{uuid.uuid4()}"
                st.rerun()
                return
            if st.button("β–Ά Solve", type="primary"):
                if canvas.image_data is not None and not _is_blank_canvas(canvas.image_data):
                    img = Image.fromarray(canvas.image_data.astype("uint8"), "RGBA")
                    bg  = Image.new("RGB", img.size, (0, 0, 0))
                    bg.paste(img, mask=img.split()[3])
                    buf = io.BytesIO()
                    bg.save(buf, format="PNG")
                    image_b64 = base64.b64encode(buf.getvalue()).decode()
                    prompt = draw_q or "Solve this handwritten math problem."
                else:
                    st.warning("Please draw something first.")

    with tab_upload:
        uploaded = st.file_uploader("Upload image", type=["png", "jpg"])
        up_q     = st.text_input("Question", placeholder="Analyze...", key="up_q")
        if uploaded and st.button("β–Ά Analyze", type="primary"):
            image_b64 = base64.b64encode(uploaded.getvalue()).decode()
            prompt    = up_q or "Analyze this image."

    with tab_camera:
        camera_photo = st.camera_input("Take a picture of a math problem")
        cam_q = st.text_input("Question", placeholder="Analyze this photo...", key="cam_q")
        if camera_photo and st.button("β–Ά Analyze Photo", type="primary", key="btn_cam"):
            image_b64 = base64.b64encode(camera_photo.getvalue()).decode()
            prompt = cam_q or "Analyze this image."

    # ── Transition: IDLE β†’ PROCESSING ─────────────────────────────────────
    # Add user message to state, set flag, rerun.
    # The API call happens on the NEXT pass (PROCESSING state above).
    # This is what makes the user question visible before the answer.
    if prompt:
        _add_message("user", prompt, image_data=image_b64)
        st.session_state.is_processing = True
        st.rerun()


# ══════════════════════════════════════════════════════════════════════════════
# 7. MAIN ENTRY
# ══════════════════════════════════════════════════════════════════════════════

# Auth gate
if not st.session_state.user:
    _render_login()
    st.stop()

# User switch detection β€” clear state when a different user logs in
_uid = st.session_state.user["uid"]
if st.session_state.loaded_for_user != _uid:
    _saved = st.session_state.user
    _reset_state()
    st.session_state.user            = _saved
    st.session_state.loaded_for_user = _uid
    _refresh_sessions()

# Sidebar always renders
_render_sidebar()

# Main content
if st.session_state.current_view == "Chat":
    chat_interface()
elif st.session_state.current_view == "Profile":
    _render_profile()