File size: 40,499 Bytes
7563305
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
9124577
 
 
 
e7c5e70
9124577
7563305
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
e7c5e70
 
 
 
 
 
 
 
 
 
9124577
 
 
 
 
 
 
 
 
 
7563305
 
 
 
 
420e493
7563305
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
420e493
7563305
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1">
<title>Recall — your AI study partner</title>
<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=Bricolage+Grotesque:opsz,wght@12..96,500;12..96,600;12..96,700;12..96,800&family=Hanken+Grotesk:wght@400;500;600;700;800&family=Spline+Sans+Mono:wght@400;500&display=swap" rel="stylesheet">
<style>
  *{box-sizing:border-box;}
  html,body{margin:0;padding:0;}
  body{min-height:100vh;background:#f5f3ee;color:#17160f;font-family:'Hanken Grotesk',system-ui,sans-serif;-webkit-font-smoothing:antialiased;}
  textarea{font-family:'Hanken Grotesk',sans-serif;}
  ::selection{background:#d6dbfb;}
  @keyframes recBob{0%,100%{transform:translateY(0)}50%{transform:translateY(-4px)}}
  @keyframes recFlick{0%,100%{transform:scale(1) rotate(-2deg)}50%{transform:scale(1.1) rotate(2deg)}}
  @keyframes recPop{0%{transform:scale(0);opacity:0}60%{transform:scale(1.18)}100%{transform:scale(1);opacity:1}}
  @keyframes recShimmer{0%{background-position:-240px 0}100%{background-position:240px 0}}
  @keyframes recBanner{0%{transform:translateY(12px);opacity:0}100%{transform:translateY(0);opacity:1}}
  @keyframes recSlideIn{0%{transform:translateY(14px) scale(.99)}100%{transform:translateY(0) scale(1)}}
  @keyframes recRise{0%{transform:translateY(8px);opacity:0}100%{transform:translateY(0);opacity:1}}
  @keyframes recDot{0%,100%{opacity:.25;transform:translateY(0)}50%{opacity:1;transform:translateY(-3px)}}

  .ta{width:100%;resize:vertical;border:1px solid #e7e3d9;border-radius:15px;font-size:15px;line-height:1.6;color:#17160f;background:#faf9f6;outline:none;transition:border-color .15s,box-shadow .15s,background .15s;}
  .ta:focus{border-color:#3b5bdb;background:#fff;box-shadow:0 0 0 4px #eceefc;}
  button{font-family:'Hanken Grotesk',sans-serif;}
  .btn-primary{appearance:none;border:none;cursor:pointer;font-weight:700;color:#fff;background:#3b5bdb;border-radius:14px;box-shadow:0 2px 0 #2a3fa6;transition:background .15s,transform .06s,box-shadow .06s;}
  .btn-primary:hover{background:#3450cf;}
  .btn-primary:active{transform:translateY(1px);box-shadow:0 1px 0 #2a3fa6;}
  .btn-dark{appearance:none;border:none;cursor:pointer;font-weight:700;color:#fff;background:#17160f;border-radius:13px;transition:background .15s,transform .06s;}
  .btn-dark:hover{background:#2c2a20;}
  .btn-dark:active{transform:translateY(1px);}
  .btn-ghost{appearance:none;border:1px solid #d8d3c7;background:#fff;cursor:pointer;font-weight:600;color:#3d3b33;border-radius:11px;transition:border-color .15s;}
  .btn-ghost:hover{border-color:#17160f;}
  .pick:hover{border-color:#3b5bdb !important;}
  .pick-warn:hover{border-color:#e8590c !important;}
  .chip-sample:hover{border-color:#3b5bdb !important;color:#2b3da8 !important;}
  .scan-prev{position:relative;display:inline-flex}
  .scan-prev-pop{position:absolute;bottom:calc(100% + 10px);left:50%;transform:translateX(-50%) translateY(4px);width:320px;max-width:80vw;border-radius:12px;box-shadow:0 14px 34px rgba(20,18,10,.22);opacity:0;visibility:hidden;transition:opacity .15s ease,transform .15s ease;z-index:40;pointer-events:none;background:#fff;border:1px solid #e7e3d9;overflow:hidden}
  .scan-prev:hover .scan-prev-pop,.scan-prev:focus-within .scan-prev-pop{opacity:1;visibility:visible;transform:translateX(-50%) translateY(0)}
  .scan-prev-pop img{display:block;width:100%}
  .scan-prev-text{display:block;padding:14px 15px;font-size:12px;line-height:1.55;color:#3b3a32;text-align:left;max-height:190px;overflow:auto}
  .scan-prev-cap{font-family:'Spline Sans Mono',monospace;font-size:10px;color:#9a9789;letter-spacing:.03em;padding:7px 10px;border-top:1px solid #efece4;background:#faf9f6;text-align:left}
  .diff-easier:hover{border-color:#1f8a5b !important;color:#1f8a5b !important;}
  .diff-harder:hover{border-color:#e8590c !important;color:#c2410c !important;}
  .tab{appearance:none;border:none;cursor:pointer;font-size:14px;padding:10px 16px;border-radius:11px 11px 0 0;background:transparent;color:#6f6d61;font-weight:600;}
  .tab.active{background:#eceefc;color:#2b3da8;font-weight:700;}
</style>
</head>
<body>
<div id="app"></div>
<input id="fileInput" type="file" accept=".pdf,.txt" style="display:none">

<script>
const C = { ink:'#17160f', ink2:'#6f6d61', line:'#e7e3d9', indigo:'#3b5bdb', green:'#1f8a5b', orange:'#e8590c' };

const S = {
  screen:'upload', mode:'paste',
  sid:null, card:null, view:null,
  pasteText:'', fileName:'', sample:'',
  generating:false, uploadError:'',
  phase:'answering', answer:'', answerShown:'', result:null,
  best:0, showBanner:false, missed:'', injectedIds:[],
  regenBusy:false, recap:null, animateCard:false,
};
let selectedFile = null;

/* ---------- visual helpers (ported from the design) ---------- */
function mascot(mood){
  let k = '';
  k += `<line x1="32" y1="11" x2="32" y2="3" stroke="${C.indigo}" stroke-width="3" stroke-linecap="round"/>`;
  k += `<circle cx="32" cy="2.5" r="3" fill="${C.orange}"/>`;
  k += `<rect x="6" y="10" width="52" height="50" rx="18" fill="${C.indigo}"/>`;
  k += `<rect x="14" y="30" width="36" height="24" rx="13" fill="rgba(255,255,255,0.10)"/>`;
  const eyeY = mood === 'thinking' ? 25 : 28;
  if (mood === 'happy' || mood === 'celebrate'){
    k += `<path d="M19 29 q5 -6 10 0" stroke="#fff" stroke-width="2.8" fill="none" stroke-linecap="round"/>`;
    k += `<path d="M35 29 q5 -6 10 0" stroke="#fff" stroke-width="2.8" fill="none" stroke-linecap="round"/>`;
    k += `<path d="M25 40 q7 8 14 0" stroke="#fff" stroke-width="2.8" fill="none" stroke-linecap="round"/>`;
    if (mood === 'celebrate'){
      k += `<circle cx="10" cy="16" r="2" fill="${C.orange}"/>`;
      k += `<circle cx="54" cy="18" r="2" fill="${C.green}"/>`;
      k += `<circle cx="52" cy="40" r="1.6" fill="${C.orange}"/>`;
    }
  } else if (mood === 'oops'){
    k += `<circle cx="24" cy="28" r="6" fill="#fff"/><circle cx="24" cy="29" r="2.6" fill="${C.ink}"/>`;
    k += `<circle cx="40" cy="28" r="6" fill="#fff"/><circle cx="40" cy="29" r="2.6" fill="${C.ink}"/>`;
    k += `<circle cx="32" cy="45" r="3.2" fill="#fff"/>`;
  } else if (mood === 'thinking'){
    k += `<circle cx="24" cy="${eyeY}" r="5" fill="#fff"/><circle cx="25" cy="${eyeY-1.5}" r="2.3" fill="${C.ink}"/>`;
    k += `<circle cx="40" cy="${eyeY}" r="5" fill="#fff"/><circle cx="41" cy="${eyeY-1.5}" r="2.3" fill="${C.ink}"/>`;
    k += `<path d="M28 44 h9" stroke="#fff" stroke-width="2.6" fill="none" stroke-linecap="round"/>`;
  } else {
    k += `<circle cx="24" cy="28" r="5" fill="#fff"/><circle cx="24" cy="29" r="2.4" fill="${C.ink}"/>`;
    k += `<circle cx="40" cy="28" r="5" fill="#fff"/><circle cx="40" cy="29" r="2.4" fill="${C.ink}"/>`;
    k += `<path d="M27 42 q5 4 10 0" stroke="#fff" stroke-width="2.6" fill="none" stroke-linecap="round"/>`;
  }
  return `<svg width="100%" height="100%" viewBox="0 0 64 64" style="display:block;overflow:visible">${k}</svg>`;
}
function flame(){
  return `<svg width="100%" height="100%" viewBox="0 0 24 24" style="display:block">`+
    `<path d="M12.5 2c.6 3.2 2.7 4.6 3.8 6.6A6.3 6.3 0 0 1 17 11.6C17 15.8 14.1 19.5 11.7 19.5 8.7 19.5 6.2 17 6.2 13.5c0-2.1 1.1-3.6 2.1-4.8.2 1.1.9 1.9 1.8 2.4C9.6 9.3 9.3 6.7 12.5 2z" fill="#e8590c"/>`+
    `<path d="M11.6 19.5c1.6 0 3-1.4 3-3.2 0-1.4-1-2.6-1.9-3.6-.3 1.5-1.6 2-1.6 3.4 0 .6.3 1.1.6 1.6-1.1-.2-1.7-1-1.7-2.1-.7.7-1.1 1.5-1.1 2.4 0 .8.9 1.5 2.6 1.5z" fill="#f8b24a"/></svg>`;
}
function scoreDots(r){
  if (!r) return '';
  const col = r.correct ? C.green : C.orange;
  return `<div style="display:flex;gap:5px">` + [0,1,2,3,4].map(i =>
    `<div style="width:11px;height:11px;border-radius:99px;background:${i < r.score ? col : '#e7e3d9'}"></div>`).join('') + `</div>`;
}
function masteryBars(){
  const stats = (S.view && S.view.topicStats) || {};
  const entries = Object.entries(stats);
  if (!entries.length)
    return `<div style="font-family:'Spline Sans Mono',monospace;font-size:10px;color:${C.ink2};letter-spacing:.08em;text-transform:uppercase">Mastery · building…</div>`;
  const bars = entries.map(([t, s]) => {
    const pct = s.total ? s.correct / s.total : 0;
    const col = s.total === 0 ? C.line : (pct >= 0.7 ? C.green : C.orange);
    const h = Math.max(14, pct * 100);
    return `<div title="${esc(t)}: ${Math.round(pct*100)}%" style="width:8px;height:26px;border-radius:5px;background:#e7e3d9;display:flex;align-items:flex-end;overflow:hidden">`+
      `<div style="width:100%;height:${h}%;background:${col};border-radius:5px;transition:height .5s cubic-bezier(.2,.8,.2,1)"></div></div>`;
  }).join('');
  return `<div style="display:flex;align-items:center;gap:12px">`+
    `<span style="font-family:'Spline Sans Mono',monospace;font-size:10px;color:${C.ink2};letter-spacing:.1em;text-transform:uppercase">Mastery</span>`+
    `<div style="display:flex;gap:6px;align-items:flex-end;height:26px">${bars}</div></div>`;
}
function queueRail(){
  const rail = (S.view && S.view.rail) || [];
  const pos = (S.view && S.view.answered) || 0;
  return `<div style="display:flex;gap:6px;align-items:center;flex-wrap:wrap">` + rail.map((c, i) => {
    const injected = S.injectedIds.includes(c.id) || c.injected;
    let bg = '#ded9cd', w = 11;
    if (i < pos) bg = C.ink;
    if (i === pos) { bg = C.indigo; w = 28; }
    if (i > pos && injected) bg = C.orange;
    let extra = '';
    if (S.injectedIds.includes(c.id)) extra = 'animation:recPop .5s cubic-bezier(.2,.8,.2,1) both;box-shadow:0 0 0 3px rgba(232,89,12,.18);';
    return `<div title="${esc(c.topic)}" style="height:11px;width:${w}px;border-radius:6px;background:${bg};transition:all .45s cubic-bezier(.2,.8,.2,1);flex:none;${extra}"></div>`;
  }).join('') + `</div>`;
}
function diffLabel(card){
  if (!card) return '';
  if (card.diffLabel) return card.diffLabel;
  if (card.parent_id) return 'drill';
  return ({1:'easy',2:'medium',3:'hard'})[card.difficulty] || 'medium';
}
function sourceLabel(card){
  if (!card) return '';
  return (card.parent_id ? 'Follow-up · ' : 'Notes · ') + card.topic;
}

/* ---------- screens ---------- */
function renderUpload(){
  const errBlock = S.uploadError ? `
    <div style="margin-top:15px;display:flex;gap:11px;align-items:flex-start;background:#fdeee2;border:1px solid #f6c9aa;border-radius:14px;padding:13px 15px;animation:recRise .25s ease both">
      <span style="flex:none;width:20px;height:20px;border-radius:99px;background:#e8590c;color:#fff;font-weight:800;font-size:13px;display:flex;align-items:center;justify-content:center;margin-top:1px">!</span>
      <span style="font-size:14px;color:#9a3a06;line-height:1.5">${esc(S.uploadError)}</span>
    </div>` : '';

  const paste = `
    <textarea id="pasteInput" oninput="S.pasteText=this.value" placeholder="Paste your lecture notes, a chapter, anything you want to be quizzed on…" class="ta" style="min-height:158px;padding:15px 17px"></textarea>
    <div style="margin-top:13px;display:flex;gap:10px;align-items:center;flex-wrap:wrap">
      <button class="chip-sample" onclick="loadSample()" style="appearance:none;border:1px solid #e7e3d9;background:#fff;cursor:pointer;font-family:'Spline Sans Mono',monospace;font-size:12px;color:#6f6d61;padding:8px 13px;border-radius:99px">↺ Paste sample notes</button>
      <span style="font-family:'Spline Sans Mono',monospace;font-size:11px;color:#9a9789">Biology · Photosynthesis &amp; respiration</span>
    </div>`;

  const filePill = S.fileName ? `
      <div style="margin-top:18px;display:inline-flex;align-items:center;gap:10px;background:#eceefc;border:1px solid #c9d0fb;padding:9px 14px;border-radius:99px">
        <span style="font-size:13px;font-weight:700;color:#2b3da8">${esc(S.fileName)}</span>
        <button onclick="clearFile()" style="appearance:none;border:none;background:transparent;cursor:pointer;color:#6f6d61;font-size:15px;line-height:1;padding:0">✕</button>
      </div>` : '';
  const upload = `
    <div onclick="if(!S.sample&&!S.fileName)document.getElementById('fileInput').click()" style="border:1.5px dashed #cfcabb;border-radius:18px;padding:28px 22px;text-align:center;background:#faf9f6;cursor:pointer">
      <div style="font-family:'Spline Sans Mono',monospace;font-size:12px;color:#9a9789;letter-spacing:.04em">DROP A PDF HERE — OR TRY A SAMPLE</div>
      <div style="display:flex;gap:10px;justify-content:center;margin-top:16px;flex-wrap:wrap">
        <span class="scan-prev">
          <button class="pick" onclick="event.stopPropagation();pickBio()" style="display:flex;align-items:center;gap:9px;appearance:none;border:1px solid #e7e3d9;background:#fff;cursor:pointer;padding:9px 13px;border-radius:12px">
            <span style="font-family:'Spline Sans Mono',monospace;font-size:9px;font-weight:500;color:#fff;background:#3b5bdb;padding:3px 5px;border-radius:4px;letter-spacing:.04em">PDF</span>
            <span style="font-size:13px;font-weight:600;color:#17160f">biology-notes.pdf</span>
          </button>
          <span class="scan-prev-pop" aria-hidden="true">
            <span class="scan-prev-text">Photosynthesis happens in the chloroplast. The light-dependent reactions occur in the thylakoid membranes, where water is split, ATP and NADPH are produced, and oxygen is released. The Calvin cycle takes place in the stroma, where the enzyme RuBisCO fixes CO2 onto RuBP. Cellular respiration occurs in the mitochondria; most ATP is made during oxidative phosphorylation.</span>
            <span class="scan-prev-cap">plain-text notes · chunked into a deck</span>
          </span>
        </span>
        <span class="scan-prev">
          <button class="pick" onclick="event.stopPropagation();pickScan()" style="display:flex;align-items:center;gap:9px;appearance:none;border:1px solid #e7e3d9;background:#fff;cursor:pointer;padding:9px 13px;border-radius:12px">
            <span style="font-family:'Spline Sans Mono',monospace;font-size:9px;font-weight:500;color:#fff;background:#3b5bdb;padding:3px 5px;border-radius:4px;letter-spacing:.04em">IMG</span>
            <span style="font-size:13px;font-weight:600;color:#17160f">scanned-slides.pdf</span>
          </button>
          <span class="scan-prev-pop" aria-hidden="true">
            <img src="/api/sample/scan.png" alt="Preview of the synthetic scanned slide" loading="lazy">
            <span class="scan-prev-cap">synthetic image · no text layer → OCR'd by the vision model</span>
          </span>
        </span>
      </div>
      ${filePill}
    </div>`;

  const overlay = S.generating ? `
    <div style="position:absolute;inset:0;border-radius:26px;background:rgba(255,255,255,.94);backdrop-filter:blur(3px);display:flex;flex-direction:column;align-items:center;justify-content:center;gap:14px;text-align:center;padding:24px">
      <div style="width:66px;height:66px;animation:recBob 1.5s ease-in-out infinite">${mascot('thinking')}</div>
      <div style="font-family:'Bricolage Grotesque',sans-serif;font-weight:700;font-size:21px">Reading your notes…</div>
      <div style="font-family:'Spline Sans Mono',monospace;font-size:12px;color:#9a9789">pulling out concepts · writing your deck</div>
      <div style="display:flex;flex-direction:column;gap:9px;width:min(360px,76%);margin-top:8px">
        ${[100,86,64].map(w=>`<div style="height:11px;border-radius:6px;width:${w}%;background:linear-gradient(90deg,#efece4 25%,#e1dccf 37%,#efece4 63%);background-size:400px 100%;animation:recShimmer 1.3s linear infinite"></div>`).join('')}
      </div>
    </div>` : '';

  return `
  <div style="max-width:940px;margin:0 auto;padding:46px 24px 90px">
    <div style="display:flex;align-items:center;gap:13px;margin-bottom:46px">
      <div style="width:46px;height:46px;animation:recBob 4s ease-in-out infinite">${mascot('idle')}</div>
      <div style="display:flex;flex-direction:column;line-height:1">
        <span style="font-family:'Bricolage Grotesque',sans-serif;font-weight:800;font-size:25px;letter-spacing:-.025em">Recall</span>
        <span style="font-family:'Spline Sans Mono',monospace;font-size:11px;color:#6f6d61;letter-spacing:.12em;margin-top:5px">AI STUDY PARTNER</span>
      </div>
    </div>

    <h1 style="font-family:'Bricolage Grotesque',sans-serif;font-weight:800;font-size:clamp(34px,5.6vw,58px);line-height:1.06;letter-spacing:-.035em;margin:0 0 20px;max-width:16ch;text-wrap:balance">Turn your notes into a quiz that fights back.</h1>
    <p style="font-size:18px;line-height:1.55;color:#6f6d61;max-width:48ch;margin:0 0 34px">Drop in a PDF or paste your notes. Recall builds a deck, grades your free-text answers, and the moment you slip it spins up new questions to drill exactly what you missed.</p>

    <div style="background:#fff;border:1px solid #e7e3d9;border-radius:26px;box-shadow:0 1px 2px rgba(20,18,10,.04),0 22px 48px -24px rgba(20,18,10,.22);position:relative">
      <div style="display:flex;gap:6px;padding:16px 18px 0;border-bottom:1px solid #f0ede5">
        <button class="tab ${S.mode==='paste'?'active':''}" onclick="setMode('paste')">Paste notes</button>
        <button class="tab ${S.mode==='upload'?'active':''}" onclick="setMode('upload')">Upload PDF</button>
      </div>
      <div style="padding:20px 22px 22px">
        ${S.mode==='paste'?paste:upload}
        ${errBlock}
        <div style="display:flex;align-items:center;justify-content:space-between;margin-top:22px;gap:16px;flex-wrap:wrap">
          <span style="font-family:'Spline Sans Mono',monospace;font-size:11px;color:#9a9789;letter-spacing:.03em">12 questions · ~5 min · free-text</span>
          <button class="btn-primary" onclick="generate()" style="font-size:16px;padding:14px 24px">Generate quiz deck →</button>
        </div>
      </div>
      ${overlay}
    </div>
  </div>`;
}

function renderStudy(){
  const card = S.card || { topic:'', difficulty:2, question:'', id:'' };
  const v = S.view || { posDisplay:1, total:1, streak:0 };
  const moodHead = S.phase === 'grading' ? 'thinking' : 'idle';

  let body = '';
  if (S.phase === 'answering'){
    body = `
      <div style="margin-top:24px">
        <textarea id="answerInput" oninput="S.answer=this.value" placeholder="Type your answer in your own words…" class="ta" style="min-height:104px;padding:14px 16px;font-size:16px"></textarea>
        <div style="display:flex;align-items:center;justify-content:space-between;gap:14px;margin-top:16px;flex-wrap:wrap">
          <div style="display:flex;align-items:center;gap:8px">
            <span style="font-family:'Spline Sans Mono',monospace;font-size:10px;color:#9a9789;letter-spacing:.06em">DIFFICULTY</span>
            <button class="diff-easier" onclick="regen('easier')" style="appearance:none;border:1px solid #d8d3c7;background:#fff;cursor:pointer;font-size:13px;font-weight:600;color:#3d3b33;padding:7px 13px;border-radius:99px">↓ Easier</button>
            <button class="diff-harder" onclick="regen('harder')" style="appearance:none;border:1px solid #d8d3c7;background:#fff;cursor:pointer;font-size:13px;font-weight:600;color:#3d3b33;padding:7px 13px;border-radius:99px">↑ Harder</button>
          </div>
          <div style="display:flex;align-items:center;gap:14px">
            <span style="font-family:'Spline Sans Mono',monospace;font-size:11px;color:#9a9789">↵ to submit</span>
            <button class="btn-primary" onclick="submit()" style="font-size:15px;padding:12px 22px;border-radius:13px">Check answer</button>
          </div>
        </div>
      </div>`;
  } else if (S.phase === 'grading'){
    body = `
      <div style="margin-top:24px;display:flex;align-items:center;gap:16px;background:#faf9f6;border:1px solid #e7e3d9;border-radius:16px;padding:18px 20px">
        <div style="width:46px;height:46px;animation:recBob 1.4s ease-in-out infinite">${mascot('thinking')}</div>
        <div>
          <div style="font-family:'Bricolage Grotesque',sans-serif;font-weight:700;font-size:17px">Grading your answer…</div>
          <div style="font-family:'Spline Sans Mono',monospace;font-size:11px;color:#9a9789;margin-top:2px">comparing to the source · scoring 0–5</div>
        </div>
        <div style="margin-left:auto;display:flex;gap:5px">
          ${[0,.15,.3].map(d=>`<div style="width:7px;height:7px;border-radius:99px;background:#3b5bdb;animation:recDot 1s ease-in-out ${d}s infinite"></div>`).join('')}
        </div>
      </div>`;
  } else { // graded
    const r = S.result;
    const mood = r && r.correct ? 'happy' : 'oops';
    const panel = r && r.correct ? `
        <div style="margin-top:18px;background:#e6f5ec;border:1px solid #b7e3c8;border-radius:18px;padding:20px 22px;display:flex;gap:16px">
          <div style="width:52px;height:52px;flex:none">${mascot(mood)}</div>
          <div style="flex:1">
            <div style="display:flex;align-items:center;gap:12px;flex-wrap:wrap">
              <span style="font-family:'Bricolage Grotesque',sans-serif;font-weight:800;font-size:18px;color:#1f8a5b">Correct!</span>
              <div>${scoreDots(r)}</div>
              <span style="font-family:'Spline Sans Mono',monospace;font-size:10px;color:#6f6d61">SCORE ${r.score}/5</span>
            </div>
            <p style="font-size:15px;line-height:1.55;color:#2f3a33;margin:10px 0 0">${esc(r.explanation)}</p>
            <div style="margin-top:12px;display:flex;align-items:center;gap:8px">
              <div style="width:18px;height:18px">${flame()}</div>
              <span style="font-size:14px;font-weight:700;color:#c2410c">Streak ${v.streak} — keep it lit.</span>
            </div>
          </div>
        </div>` : `
        <div style="margin-top:18px;background:#fdeee2;border:1px solid #f6c9aa;border-radius:18px;padding:20px 22px;display:flex;gap:16px">
          <div style="width:52px;height:52px;flex:none">${mascot(mood)}</div>
          <div style="flex:1">
            <div style="display:flex;align-items:center;gap:12px;flex-wrap:wrap">
              <span style="font-family:'Bricolage Grotesque',sans-serif;font-weight:800;font-size:18px;color:#c2410c">Not quite.</span>
              <div>${scoreDots(r)}</div>
              <span style="font-family:'Spline Sans Mono',monospace;font-size:10px;color:#6f6d61">SCORE ${r.score}/5</span>
            </div>
            <p style="font-size:15px;line-height:1.55;color:#5a4334;margin:10px 0 0">${esc(r.explanation)}</p>
            <div style="margin-top:14px;background:#fff;border:1px dashed #e8a37a;border-radius:12px;padding:11px 14px">
              <span style="font-family:'Spline Sans Mono',monospace;font-size:10px;color:#c2410c;letter-spacing:.06em">MISSED CONCEPT</span>
              <div style="font-family:'Bricolage Grotesque',sans-serif;font-weight:700;font-size:16px;color:#9a3a06;margin-top:3px">${esc(r.missed)}</div>
            </div>
          </div>
        </div>`;
    body = `
      <div style="margin-top:22px">
        <div style="font-family:'Spline Sans Mono',monospace;font-size:10px;color:#9a9789;letter-spacing:.08em">YOUR ANSWER</div>
        <p style="font-size:16px;line-height:1.5;color:#56544a;margin:6px 0 0">${esc(S.answerShown)}</p>
      </div>
      ${panel}
      <div style="display:flex;justify-content:flex-end;margin-top:22px">
        <button class="btn-dark" onclick="next()" style="font-size:15px;padding:12px 22px">Next card →</button>
      </div>`;
  }

  const banner = S.showBanner ? `
    <div style="margin-top:16px;background:#e8590c;color:#fff;border-radius:18px;padding:17px 22px;display:flex;align-items:center;gap:16px;box-shadow:0 18px 34px -18px rgba(232,89,12,.65);animation:recBanner .45s cubic-bezier(.2,.85,.25,1) both">
      <div style="width:44px;height:44px;flex:none;display:flex;align-items:center;justify-content:center;background:rgba(255,255,255,.2);border-radius:13px;font-family:'Bricolage Grotesque',sans-serif;font-weight:800;font-size:18px">+${S.injectedIds.length}</div>
      <div style="flex:1">
        <div style="font-family:'Spline Sans Mono',monospace;font-size:10px;opacity:.82;letter-spacing:.1em">ADAPTIVE FOLLOW-UP</div>
        <div style="font-family:'Bricolage Grotesque',sans-serif;font-weight:700;font-size:18px;line-height:1.28;margin-top:3px">You missed ${esc(S.missed)} → ${S.injectedIds.length} new question${S.injectedIds.length===1?'':'s'} just slid into your deck to drill it.</div>
      </div>
    </div>` : '';

  const shimmer = S.regenBusy ? `<div style="position:absolute;inset:-8px;border-radius:14px;background:linear-gradient(90deg,rgba(245,243,238,0),rgba(245,243,238,.85) 50%,rgba(245,243,238,0));background-size:300px 100%;animation:recShimmer 1s linear infinite"></div>` : '';
  const cardAnim = S.animateCard ? 'animation:recSlideIn .42s cubic-bezier(.2,.85,.25,1) both;' : '';

  return `
  <div>
    <div style="position:sticky;top:0;z-index:5;background:rgba(245,243,238,.86);backdrop-filter:blur(10px);border-bottom:1px solid #e7e3d9">
      <div style="max-width:1080px;margin:0 auto;padding:13px 24px;display:flex;align-items:center;gap:20px">
        <div style="display:flex;align-items:center;gap:12px">
          <div style="width:36px;height:36px">${mascot(moodHead)}</div>
          <div style="display:flex;flex-direction:column;line-height:1.15">
            <span style="font-family:'Bricolage Grotesque',sans-serif;font-weight:800;font-size:16px;letter-spacing:-.02em">Recall</span>
            <span style="font-family:'Spline Sans Mono',monospace;font-size:10px;color:#6f6d61;letter-spacing:.08em">CARD ${v.posDisplay} / ${v.total}</span>
          </div>
        </div>
        <div style="margin:0 auto">${masteryBars()}</div>
        <div style="display:flex;align-items:center;gap:14px">
          <div style="display:flex;align-items:center;gap:7px;background:#fdeee2;border:1px solid #f6c9aa;padding:7px 13px;border-radius:99px">
            <div style="width:20px;height:20px;animation:recFlick 1.5s ease-in-out infinite">${flame()}</div>
            <span style="font-family:'Bricolage Grotesque',sans-serif;font-weight:800;font-size:16px;color:#c2410c">${v.streak}</span>
          </div>
          <button class="btn-ghost" onclick="finish()" style="font-size:14px;padding:9px 15px">Finish &amp; recap</button>
        </div>
      </div>
    </div>

    <div style="max-width:760px;margin:0 auto;padding:38px 24px 64px">
      <div style="background:#fff;border:1px solid #e7e3d9;border-radius:26px;box-shadow:0 1px 2px rgba(20,18,10,.04),0 24px 50px -26px rgba(20,18,10,.2);padding:30px 34px;${cardAnim}">
        <div style="display:flex;align-items:center;justify-content:space-between;gap:12px;margin-bottom:22px">
          <div style="display:flex;gap:8px;align-items:center;flex-wrap:wrap">
            <span style="font-size:12px;font-weight:700;color:#2b3da8;background:#eceefc;padding:5px 11px;border-radius:99px">${esc(card.topic)}</span>
            <span style="font-family:'Spline Sans Mono',monospace;font-size:11px;color:#6f6d61;border:1px solid #e7e3d9;padding:4px 9px;border-radius:99px;text-transform:uppercase;letter-spacing:.04em">${esc(diffLabel(card))}</span>
          </div>
          <span style="font-family:'Spline Sans Mono',monospace;font-size:11px;color:#9a9789">${esc(sourceLabel(card))}</span>
        </div>
        <div style="position:relative">
          <p style="font-family:'Bricolage Grotesque',sans-serif;font-weight:600;font-size:clamp(22px,3.5vw,31px);line-height:1.2;letter-spacing:-.012em;margin:0;text-wrap:pretty">${esc(card.question)}</p>
          ${shimmer}
        </div>
        ${body}
      </div>
      ${banner}
      <div style="margin-top:28px">
        <div style="font-family:'Spline Sans Mono',monospace;font-size:10px;color:#9a9789;letter-spacing:.08em;margin-bottom:11px;display:flex;justify-content:space-between">
          <span>YOUR DECK</span><span>${v.posDisplay} / ${v.total}</span>
        </div>
        ${queueRail()}
      </div>
    </div>
  </div>`;
}

function renderRecap(){
  const stats = (S.view && S.view.topicStats) || {};
  let totalT = 0, totalC = 0;
  Object.values(stats).forEach(st => { totalT += st.total; totalC += st.correct; });
  const acc = totalT ? Math.round(totalC/totalT*100) : 0;

  // Membership comes from the server recap (average-grade based) so the
  // mastered/weak split shown here is exactly what the scheduler resurfaces —
  // the % beside each topic is just a display stat from topicStats.
  const pctLabel = (t) => {
    const st = stats[t]; const pct = st && st.total ? st.correct / st.total : 0;
    return Math.round(pct * 100) + '%';
  };
  const mastered = ((S.recap && S.recap.mastered) || []).map(t => ({ topic: t, pctLabel: pctLabel(t) }));
  const weak = ((S.recap && S.recap.weak_topics) || []).map(t => ({ topic: t, pctLabel: pctLabel(t) }));
  const answered = (S.view && S.view.answered) || 0;
  const recapTitle = acc >= 80 ? 'Strong session.' : acc >= 50 ? 'Solid progress.' : 'Good start.';
  const reflection = (S.recap && S.recap.reflection) ||
    'You turned shaky topics into targeted drills — spaced repetition will bring the weak ones back first.';

  const masteredBlock = mastered.length ? `<div style="display:grid;gap:9px">` + mastered.map(m => `
      <div style="display:flex;align-items:center;gap:12px;background:#e6f5ec;border:1px solid #cdeada;border-radius:13px;padding:11px 15px">
        <span style="width:18px;height:18px;border-radius:99px;background:#1f8a5b;color:#fff;font-size:11px;font-weight:800;display:flex;align-items:center;justify-content:center;flex:none">✓</span>
        <span style="font-weight:700;font-size:15px;flex:1">${esc(m.topic)}</span>
        <span style="font-family:'Spline Sans Mono',monospace;font-size:12px;color:#1f8a5b">${m.pctLabel}</span>
      </div>`).join('') + `</div>`
    : `<p style="font-size:14px;color:#9a9789;margin:0">Nothing fully locked in yet — another pass and these will turn green.</p>`;

  const weakBlock = weak.length ? `<div style="display:grid;gap:9px">` + weak.map(w => `
      <div style="display:flex;align-items:center;gap:12px;background:#fdeee2;border:1px solid #f6d2b5;border-radius:13px;padding:11px 15px">
        <span style="width:18px;height:18px;border-radius:99px;background:#e8590c;color:#fff;font-size:11px;font-weight:800;display:flex;align-items:center;justify-content:center;flex:none">↑</span>
        <span style="font-weight:700;font-size:15px;flex:1">${esc(w.topic)}</span>
        <span style="font-family:'Spline Sans Mono',monospace;font-size:12px;color:#c2410c">${w.pctLabel}</span>
      </div>`).join('') + `</div>`
    : `<p style="font-size:14px;color:#9a9789;margin:0">Clean sweep — no weak topics this round. 🔥-worthy.</p>`;

  return `
  <div style="max-width:720px;margin:0 auto;padding:54px 24px 90px;text-align:center">
    <div style="width:112px;height:112px;margin:0 auto 6px;animation:recBob 3.6s ease-in-out infinite">${mascot('celebrate')}</div>
    <div style="font-family:'Spline Sans Mono',monospace;font-size:11px;color:#9a9789;letter-spacing:.14em">SESSION RECAP</div>
    <h1 style="font-family:'Bricolage Grotesque',sans-serif;font-weight:800;font-size:clamp(32px,6.4vw,50px);letter-spacing:-.035em;margin:8px 0 8px">${recapTitle}</h1>
    <p style="font-size:18px;color:#6f6d61;max-width:44ch;margin:0 auto 30px;line-height:1.5;text-wrap:pretty">${esc(reflection)}</p>

    <div style="display:flex;gap:13px;justify-content:center;flex-wrap:wrap;margin-bottom:36px">
      <div style="background:#fff;border:1px solid #e7e3d9;border-radius:18px;padding:18px 26px;min-width:132px">
        <div style="display:flex;align-items:center;justify-content:center;gap:6px"><div style="width:24px;height:24px">${flame()}</div><span style="font-family:'Bricolage Grotesque',sans-serif;font-weight:800;font-size:34px;color:#c2410c">${S.best}</span></div>
        <div style="font-family:'Spline Sans Mono',monospace;font-size:10px;color:#9a9789;letter-spacing:.06em;margin-top:4px">BEST STREAK</div>
      </div>
      <div style="background:#fff;border:1px solid #e7e3d9;border-radius:18px;padding:18px 26px;min-width:132px">
        <div style="font-family:'Bricolage Grotesque',sans-serif;font-weight:800;font-size:38px">${acc}%</div>
        <div style="font-family:'Spline Sans Mono',monospace;font-size:10px;color:#9a9789;letter-spacing:.06em;margin-top:4px">ACCURACY</div>
      </div>
      <div style="background:#fff;border:1px solid #e7e3d9;border-radius:18px;padding:18px 26px;min-width:132px">
        <div style="font-family:'Bricolage Grotesque',sans-serif;font-weight:800;font-size:38px">${answered}</div>
        <div style="font-family:'Spline Sans Mono',monospace;font-size:10px;color:#9a9789;letter-spacing:.06em;margin-top:4px">CARDS ANSWERED</div>
      </div>
    </div>

    <div style="text-align:left;display:grid;gap:16px">
      <div style="background:#fff;border:1px solid #e7e3d9;border-radius:20px;padding:20px 22px">
        <div style="display:flex;align-items:center;gap:9px;margin-bottom:14px">
          <span style="width:22px;height:22px;border-radius:99px;background:#1f8a5b;color:#fff;font-size:13px;font-weight:800;display:flex;align-items:center;justify-content:center">✓</span>
          <span style="font-family:'Bricolage Grotesque',sans-serif;font-weight:700;font-size:17px">Mastered <span style="color:#6f6d61;font-weight:500">· ${mastered.length}</span></span>
        </div>
        ${masteredBlock}
      </div>
      <div style="background:#fff;border:1px solid #e7e3d9;border-radius:20px;padding:20px 22px">
        <div style="display:flex;align-items:center;gap:9px;margin-bottom:6px">
          <span style="width:22px;height:22px;border-radius:99px;background:#e8590c;color:#fff;font-size:13px;font-weight:800;display:flex;align-items:center;justify-content:center">↑</span>
          <span style="font-family:'Bricolage Grotesque',sans-serif;font-weight:700;font-size:17px">Coming back sooner <span style="color:#6f6d61;font-weight:500">· ${weak.length}</span></span>
        </div>
        <p style="font-size:12px;color:#9a9789;margin:0 0 14px;font-family:'Spline Sans Mono',monospace;letter-spacing:.02em">spaced repetition will resurface these first</p>
        ${weakBlock}
      </div>
    </div>

    <div style="display:flex;gap:12px;justify-content:center;margin-top:34px;flex-wrap:wrap">
      <button class="btn-primary" onclick="restart()" style="font-size:16px;padding:14px 24px">Study this deck again</button>
      <button class="btn-ghost" onclick="newMaterial()" style="font-size:16px;padding:14px 22px;border-radius:14px">New material</button>
    </div>
  </div>`;
}

/* ---------- render + state plumbing ---------- */
function esc(s){ return String(s==null?'':s).replace(/[&<>"]/g, c => ({'&':'&amp;','<':'&lt;','>':'&gt;','"':'&quot;'}[c])); }

function render(){
  const root = document.getElementById('app');
  if (S.screen === 'upload') root.innerHTML = renderUpload();
  else if (S.screen === 'study') root.innerHTML = renderStudy();
  else root.innerHTML = renderRecap();

  // restore uncontrolled input values
  const p = document.getElementById('pasteInput'); if (p) p.value = S.pasteText;
  const a = document.getElementById('answerInput');
  if (a){ a.value = S.answer; a.addEventListener('keydown', onAnswerKey); a.focus(); }
  S.animateCard = false;
}

function onAnswerKey(e){ if (e.key === 'Enter' && !e.shiftKey){ e.preventDefault(); submit(); } }

async function postJSON(url, body){
  const r = await fetch(url, { method:'POST', headers:{'Content-Type':'application/json'}, body: JSON.stringify(body) });
  return r.json();
}

/* ---------- upload actions ---------- */
function setMode(m){ S.mode = m; S.uploadError = ''; render(); }
function loadSample(){ S.pasteText = SAMPLE; S.mode = 'paste'; S.uploadError = ''; render(); }
function pickBio(){ S.sample = 'bio'; S.fileName = 'biology-notes.pdf'; selectedFile = null; S.uploadError = ''; render(); }
function pickScan(){ S.sample = 'scan'; S.fileName = 'scanned-slides.pdf'; selectedFile = null; S.uploadError = ''; render(); }
function clearFile(){ S.fileName = ''; S.sample = ''; selectedFile = null; S.uploadError = ''; render(); }
document.getElementById('fileInput').addEventListener('change', (e) => {
  const f = e.target.files[0]; if (!f) return;
  selectedFile = f; S.fileName = f.name; S.sample = ''; S.mode = 'upload'; S.uploadError = ''; render();
});

const SAMPLE = 'Photosynthesis happens in the chloroplast. The light-dependent reactions occur in the thylakoid membranes, where water is split, ATP and NADPH are produced, and oxygen is released. The Calvin cycle takes place in the stroma, where the enzyme RuBisCO fixes CO2 onto RuBP. Cellular respiration occurs in the mitochondria; most ATP is made during oxidative phosphorylation, as the electron transport chain pumps protons and oxygen acts as the final electron acceptor, forming water.';

async function generate(){
  if (S.generating) return;
  S.uploadError = '';
  if (S.mode === 'paste') S.pasteText = (document.getElementById('pasteInput')||{}).value || S.pasteText;
  S.generating = true; render();
  const fd = new FormData();
  if (S.mode === 'paste') fd.append('text', S.pasteText || '');
  else { if (S.sample) fd.append('sample', S.sample); else if (selectedFile) fd.append('file', selectedFile); }
  try {
    const r = await fetch('/api/generate', { method:'POST', body: fd });
    const d = await r.json();
    if (d.error){ S.generating = false; S.uploadError = d.error; render(); return; }
    S.sid = d.sid; S.card = d.card; S.view = d.view;
    S.screen = 'study'; S.phase = 'answering'; S.answer = ''; S.result = null;
    S.best = 0; S.showBanner = false; S.injectedIds = []; S.generating = false;
    S.animateCard = true; render();
  } catch (e){ S.generating = false; S.uploadError = 'Something went wrong reaching the server. Try again.'; render(); }
}

/* ---------- study actions ---------- */
async function submit(){
  if (S.phase !== 'answering') return;
  S.answer = (document.getElementById('answerInput')||{}).value || '';
  S.phase = 'grading'; render();
  const d = await postJSON('/api/grade', { sid: S.sid, answer: S.answer });
  if (d.error){ S.phase = 'answering'; render(); return; }
  S.result = d.grade; S.view = d.view;
  S.answerShown = S.answer.trim() ? S.answer : '(left blank)';
  S.best = Math.max(S.best, d.view.streak);
  S.injectedIds = []; S.showBanner = false; S.phase = 'graded';
  render();
  // The adaptive follow-up lands a beat after the verdict — the hero moment.
  if (!d.grade.correct && d.injectedIds && d.injectedIds.length){
    setTimeout(() => { S.missed = d.grade.missed; S.injectedIds = d.injectedIds; S.showBanner = true; render(); }, 600);
  }
}

async function regen(dir){
  if (S.regenBusy || S.phase !== 'answering') return;
  S.answer = (document.getElementById('answerInput')||{}).value || S.answer;
  S.regenBusy = true; render();
  const d = await postJSON('/api/regenerate', { sid: S.sid, direction: dir });
  if (d.card){ S.card = d.card; S.view = d.view; }
  S.regenBusy = false; render();
}

async function next(){
  const d = await postJSON('/api/next', { sid: S.sid });
  if (!d.card){ finish(); return; }
  S.card = d.card; S.view = d.view; S.phase = 'answering';
  S.answer = ''; S.result = null; S.showBanner = false; S.injectedIds = [];
  S.animateCard = true; render();
}

async function finish(){
  const d = await postJSON('/api/recap', { sid: S.sid });
  S.recap = d.recap; S.view = d.view; S.screen = 'recap'; render();
}

async function restart(){
  const d = await postJSON('/api/restart', { sid: S.sid });
  S.card = d.card; S.view = d.view; S.screen = 'study'; S.phase = 'answering';
  S.answer = ''; S.result = null; S.best = 0; S.showBanner = false; S.injectedIds = [];
  S.animateCard = true; render();
}

function newMaterial(){
  S.screen = 'upload'; S.mode = 'paste'; S.pasteText = ''; S.fileName = ''; S.sample = '';
  selectedFile = null; S.uploadError = ''; S.card = null; S.view = null; S.sid = null;
  render();
}

// graded → Enter / ArrowRight advances (keyboard-friendly)
window.addEventListener('keydown', (e) => {
  if (S.screen === 'study' && S.phase === 'graded' && (e.key === 'Enter' || e.key === 'ArrowRight')){
    e.preventDefault(); next();
  }
});

render();
</script>
</body>
</html>