File size: 32,193 Bytes
1993562
8800c81
1993562
8800c81
 
 
 
 
 
 
 
 
 
 
7676842
 
 
8800c81
 
b606b67
8800c81
 
 
1993562
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8800c81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
53d8367
7676842
 
 
 
8800c81
 
 
 
53d8367
8800c81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
7676842
 
 
 
 
8800c81
1993562
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8800c81
 
c05acd1
 
 
8800c81
 
53d8367
 
 
 
 
 
 
 
 
 
 
 
1993562
53d8367
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
c05acd1
53d8367
 
c05acd1
53d8367
 
 
 
 
 
 
 
 
 
 
1993562
53d8367
 
 
 
 
 
8800c81
 
 
 
 
 
b53e6f3
8800c81
 
53d8367
8800c81
 
 
53d8367
8800c81
53d8367
 
1993562
8800c81
 
1993562
8800c81
1993562
8800c81
1993562
8800c81
 
1993562
8800c81
 
1993562
8800c81
1993562
 
 
 
 
ba7fa59
 
 
 
 
c05acd1
ba7fa59
 
c05acd1
ba7fa59
 
 
 
c05acd1
 
ba7fa59
 
 
 
 
 
 
 
 
 
 
 
 
1993562
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8800c81
53d8367
 
 
 
 
 
 
 
c05acd1
53d8367
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8800c81
 
1993562
c05acd1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8800c81
c05acd1
8800c81
 
 
c05acd1
 
 
 
 
 
 
ba7fa59
c05acd1
 
 
 
 
 
 
7676842
c05acd1
 
 
 
 
 
7676842
c05acd1
 
 
 
 
 
 
 
 
 
 
7676842
c05acd1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8800c81
c05acd1
 
8800c81
c05acd1
 
 
 
 
 
 
8800c81
c05acd1
 
 
 
8800c81
c05acd1
 
 
 
 
 
8800c81
c05acd1
 
 
 
 
 
 
 
 
8800c81
c05acd1
 
 
 
8800c81
c05acd1
 
 
 
8800c81
 
c05acd1
 
8800c81
 
 
 
c05acd1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8800c81
c05acd1
8800c81
c05acd1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8800c81
c05acd1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8800c81
c05acd1
 
 
 
 
8800c81
 
c05acd1
 
 
 
 
 
 
8800c81
c05acd1
 
8800c81
c05acd1
 
 
 
8800c81
c05acd1
8800c81
c05acd1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8800c81
c05acd1
 
8800c81
c05acd1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8800c81
c05acd1
 
8800c81
c05acd1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8800c81
c05acd1
 
 
 
8800c81
c05acd1
 
 
 
8800c81
c05acd1
 
 
 
 
 
 
 
 
8800c81
c05acd1
 
8800c81
 
 
 
c05acd1
 
 
 
 
 
 
8800c81
c05acd1
 
8800c81
 
c05acd1
 
8800c81
c05acd1
 
 
 
 
 
8800c81
c05acd1
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
8800c81
c05acd1
 
 
8800c81
c05acd1
 
 
 
 
 
 
 
 
 
 
 
 
8800c81
c05acd1
 
 
8800c81
 
 
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
import React, { useState, useRef, useEffect } from 'react';
import {
  ChevronDown,
  Sparkles,
  RotateCcw,
  Trash2,
  Play,
  RefreshCw,
  AlertTriangle,
  Check,
  CheckCircle2,
  XCircle,
  Save,
  Award,
  Info,
  FolderGit2,
  ExternalLink
} from 'lucide-react';
import CodeMirror from '@uiw/react-codemirror';
import { codeStudioExtensions } from './codeEditorExtensions';
import type { SandboxRunResult } from '../../services/reviewService';
import type { AiEvaluationResult } from '../../types/reviewTypes';

const AVAILABLE_LANGUAGES = [
  { id: 'python', name: 'Python', version: '3.14' }
] as const;

const PythonLogo: React.FC<{ className?: string }> = ({ className = 'w-3.5 h-3.5' }) => (
  <svg viewBox="0 0 128 128" className={className} xmlns="http://www.w3.org/2000/svg">
    <path
      d="M63.454 4.018c-14.775 0-23.774 6.425-23.774 18.775v13.626h24.168v3.424H30.082C17.518 39.843 8 47.962 8 63.673c0 15.65 8.358 23.486 21.056 23.486h11.905v-16.89c0-13.064 11.233-24.113 24.331-24.113h23.773V32.793c0-11.83-9.988-19.775-23.773-19.775H63.454v-9zm-9.845 7.426a3.84 3.84 0 0 1 3.86 3.84 3.84 3.84 0 0 1-3.86 3.841 3.84 3.84 0 0 1-3.86-3.84 3.84 3.84 0 0 1 3.86-3.841z"
      fill="#387eb8"
    />
    <path
      d="M64.546 123.982c14.775 0 23.774-6.425 23.774-18.775V91.581H64.152v-3.424h33.766c12.564 0 22.082-8.119 22.082-23.83 0-15.65-8.358-23.486-21.056-23.486H87.04v16.89c0 13.064-11.234 24.113-24.332 24.113H38.934v13.363c0 11.83 9.989 19.775 23.774 19.775h1.838v9zm9.845-7.426a3.84 3.84 0 0 1-3.86-3.84 3.84 3.84 0 0 1 3.86-3.841 3.84 3.84 0 0 1 3.86 3.84 3.84 3.84 0 0 1-3.86 3.841z"
      fill="#ffe052"
    />
  </svg>
);

interface CodeStudioPanelProps {
  mobileView: 'spec' | 'code' | 'ref';
  isAutocompleteEnabled: boolean;
  onToggleAutocomplete: (enabled: boolean) => void;
  onResetStarterCode: () => void;
  code: string;
  onChangeCode: (code: string) => void;
  sandboxRunning: boolean;
  sandboxError: string | null;
  sandboxResults: SandboxRunResult | null;
  onRunSandbox: () => void;
  savingReference: boolean;
  savedRefSuccess: boolean;
  saveRefError: string | null;
  onSaveReference: () => void;
  aiLoading: boolean;
  aiError: string | null;
  aiResult: AiEvaluationResult | null;
  onAiEvaluation: () => void;
  optimalTime: string;
  optimalSpace: string;
  isNarrow?: boolean;
  onPushToGitHub?: () => void;
  pushingToGitHub?: boolean;
  githubPushedUrl?: string | null;
  githubPushError?: string | null;
}

export const CodeStudioPanel: React.FC<CodeStudioPanelProps> = ({
  mobileView,
  isNarrow = false,
  isAutocompleteEnabled,
  onToggleAutocomplete,
  onResetStarterCode,
  code,
  onChangeCode,
  sandboxRunning,
  sandboxError,
  sandboxResults,
  onRunSandbox,
  savingReference,
  savedRefSuccess,
  saveRefError,
  onSaveReference,
  aiLoading,
  aiError,
  aiResult,
  onAiEvaluation,
  optimalTime,
  optimalSpace,
  onPushToGitHub,
  pushingToGitHub,
  githubPushedUrl,
  githubPushError
}) => {
  const [selectedLanguage, setSelectedLanguage] = useState<'python'>('python');
  const [isLangDropdownOpen, setIsLangDropdownOpen] = useState(false);
  const langDropdownRef = useRef<HTMLDivElement>(null);

  useEffect(() => {
    const handleClickOutside = (event: MouseEvent) => {
      if (langDropdownRef.current && !langDropdownRef.current.contains(event.target as Node)) {
        setIsLangDropdownOpen(false);
      }
    };
    const handleKeyDown = (event: KeyboardEvent) => {
      if (event.key === 'Escape') {
        setIsLangDropdownOpen(false);
      }
    };
    document.addEventListener('mousedown', handleClickOutside);
    document.addEventListener('keydown', handleKeyDown);
    return () => {
      document.removeEventListener('mousedown', handleClickOutside);
      document.removeEventListener('keydown', handleKeyDown);
    };
  }, []);

  const currentLang = AVAILABLE_LANGUAGES.find((l) => l.id === selectedLanguage) ?? AVAILABLE_LANGUAGES[0];

  return (
    <div
      className={`w-full ${isNarrow ? 'flex flex-1' : 'lg:w-[51%] flex'
        } flex-col bg-[#fffdfa] overflow-y-auto no-scrollbar ${isNarrow && mobileView !== 'code' ? 'hidden' : 'flex'
        }`}
    >
      {/* Studio Toolbar */}
      <div className="shrink-0 bg-[#f7f5ed] border-b-2 border-ink-black p-2 sm:px-3 flex flex-wrap items-center justify-between gap-1.5 sm:gap-2">
        {/* Left: Language Selector & Autocomplete */}
        <div className="flex items-center gap-1.5 sm:gap-2 flex-wrap">
          {/* Language Selector Dropdown */}
          <div className="relative" ref={langDropdownRef}>
            <button
              type="button"
              onClick={() => setIsLangDropdownOpen((prev) => !prev)}
              className="h-7 px-2.5 bg-white hover:bg-slate-100 border-2 border-ink-black shadow-[1px_1px_0px_0px_rgba(30,41,59,1)] active:translate-y-0.5 active:shadow-none flex items-center gap-1.5 font-arcade text-[9px] font-bold text-ink-black cursor-pointer transition-all select-none"
              title="Select programming language"
              aria-expanded={isLangDropdownOpen}
              aria-haspopup="listbox"
            >
              <PythonLogo className="w-3.5 h-3.5 shrink-0" />
              <span>{currentLang.name}</span>
              <ChevronDown className={`w-3 h-3 text-ink-black transition-transform duration-150 ${isLangDropdownOpen ? 'rotate-180' : ''}`} />
            </button>

            {/* Dropdown Menu */}
            {isLangDropdownOpen && (
              <div
                className="absolute top-full mt-1.5 left-0 z-50 w-52 bg-paper-white border-[3px] border-ink-black gadget-shadow p-1.5 space-y-1"
                role="listbox"
              >
                <div className="px-2 py-1 text-[8px] font-arcade font-bold text-trash-gray uppercase border-b border-slate-200">
                  SELECT LANGUAGE
                </div>
                {AVAILABLE_LANGUAGES.map((lang) => {
                  const isSelected = selectedLanguage === lang.id;
                  return (
                    <button
                      key={lang.id}
                      type="button"
                      role="option"
                      aria-selected={isSelected}
                      onClick={() => {
                        setSelectedLanguage(lang.id);
                        setIsLangDropdownOpen(false);
                      }}
                      className={`w-full flex items-center justify-between px-2.5 py-1.5 border-2 border-ink-black text-left transition-all cursor-pointer ${isSelected
                          ? 'bg-amber-200 shadow-[1px_1px_0px_0px_rgba(30,41,59,1)] font-bold text-ink-black'
                          : 'bg-white hover:bg-slate-100 text-slate-800'
                        }`}
                    >
                      <div className="flex items-center gap-1.5 font-arcade text-[9px]">
                        <PythonLogo className="w-3.5 h-3.5 shrink-0" />
                        <span>{lang.name}</span>
                      </div>
                      <span className="font-mono text-[8px] font-bold px-1.5 py-0.5 bg-white border border-ink-black text-slate-700 shadow-[1px_1px_0px_0px_rgba(30,41,59,0.3)]">
                        v{lang.version}
                      </span>
                    </button>
                  );
                })}
              </div>
            )}
          </div>

          <div className="h-4 w-[2px] bg-slate-300 mx-0.5 hidden sm:block" />

          {/* Autocomplete Toggle */}
          <label className="inline-flex items-center gap-1.5 cursor-pointer select-none">
            <input
              type="checkbox"
              checked={isAutocompleteEnabled}
              onChange={(e) => onToggleAutocomplete(e.target.checked)}
              className="sr-only peer"
              aria-label="Toggle code editor autocompletion"
            />
            <div className="relative w-7 h-3.5 bg-slate-300 border border-ink-black rounded-full peer peer-checked:bg-grass-green transition-all after:content-[''] after:absolute after:top-[1px] after:left-[2px] after:bg-white after:border after:border-ink-black after:rounded-full after:h-2 after:w-2 after:transition-all peer-checked:after:translate-x-3"></div>
            <span className="text-[7.5px] min-[400px]:text-[8px] font-arcade font-bold text-trash-gray peer-checked:text-ink-black whitespace-nowrap">
              AUTOCOMPLETE
            </span>
          </label>
        </div>

        {/* Middle: Secondary Tools (Reset, Delete, Save, GitHub) */}
        <div className="flex items-center gap-1.5 sm:gap-2">
          {/* Reset Starter Code */}
          <button
            onClick={onResetStarterCode}
            className="w-7 h-7 bg-white hover:bg-slate-100 border-2 border-ink-black shadow-[1px_1px_0px_0px_rgba(30,41,59,1)] active:translate-y-0.5 active:shadow-none text-ink-black flex items-center justify-center cursor-pointer transition-all"
            title="Reset to starter code"
            aria-label="Reset to starter code"
          >
            <RotateCcw className="w-3.5 h-3.5" />
          </button>

          {/* Clear Editor */}
          <button
            onClick={() => onChangeCode('')}
            className="w-7 h-7 bg-white hover:bg-rose-50 border-2 border-ink-black shadow-[1px_1px_0px_0px_rgba(30,41,59,1)] active:translate-y-0.5 active:shadow-none text-rose-700 flex items-center justify-center cursor-pointer transition-all"
            title="Clear editor"
            aria-label="Clear editor"
          >
            <Trash2 className="w-3.5 h-3.5 text-rose-600" />
          </button>

          {/* Overwrite / Save Solution with Studio Code */}
          {onSaveReference && (
            <button
              onClick={onSaveReference}
              disabled={savingReference || !code.trim()}
              className={`w-7 h-7 border-2 border-ink-black shadow-[1px_1px_0px_0px_rgba(30,41,59,1)] active:translate-y-0.5 active:shadow-none flex items-center justify-center cursor-pointer disabled:opacity-40 disabled:cursor-not-allowed transition-all ${savedRefSuccess
                  ? 'bg-emerald-600 hover:bg-emerald-500 text-white'
                  : 'bg-amber-400 hover:bg-amber-300 text-ink-black'
                }`}
              title={
                savingReference
                  ? 'Saving solution to personal database...'
                  : savedRefSuccess
                    ? 'Saved to personal database!'
                    : 'Overwrite saved solution with studio code'
              }
              aria-label="Overwrite saved solution with studio code"
            >
              {savingReference ? (
                <RefreshCw className="w-3.5 h-3.5 animate-spin text-ink-black" />
              ) : savedRefSuccess ? (
                <CheckCircle2 className="w-3.5 h-3.5 text-white" />
              ) : (
                <Save className="w-3.5 h-3.5 text-ink-black" />
              )}
            </button>
          )}

          {/* Push to GitHub */}
          {onPushToGitHub && (
            <button
              onClick={onPushToGitHub}
              disabled={pushingToGitHub || !code.trim()}
              className="w-7 h-7 bg-[#24292e] hover:bg-black border-2 border-ink-black shadow-[1px_1px_0px_0px_rgba(30,41,59,1)] active:translate-y-0.5 active:shadow-none text-white flex items-center justify-center cursor-pointer disabled:opacity-40 disabled:cursor-not-allowed transition-all"
              title={pushingToGitHub ? 'Pushing to GitHub...' : 'Push solution to GitHub repository'}
              aria-label="Push to GitHub"
            >
              {pushingToGitHub ? (
                <RefreshCw className="w-3.5 h-3.5 animate-spin text-yellow-300" />
              ) : (
                <FolderGit2 className="w-3.5 h-3.5 text-yellow-300" />
              )}
            </button>
          )}
        </div>

        {/* Right: Primary Actions (Run & Gemini Eval) */}
        <div className="flex items-center gap-1.5 sm:gap-2">
          {/* Run Tests (Local Python Sandbox) */}
          <button
            onClick={onRunSandbox}
            disabled={sandboxRunning || !code.trim()}
            className="w-7 h-7 bg-emerald-600 hover:bg-emerald-500 border-2 border-ink-black shadow-[1px_1px_0px_0px_rgba(30,41,59,1)] active:translate-y-0.5 active:shadow-none text-white flex items-center justify-center cursor-pointer disabled:opacity-40 disabled:cursor-not-allowed transition-all"
            title={sandboxRunning ? 'Running sandbox tests...' : 'Run tests in local sandbox'}
            aria-label="Run tests"
          >
            {sandboxRunning ? (
              <RefreshCw className="w-3.5 h-3.5 animate-spin" />
            ) : (
              <Play className="w-3.5 h-3.5 fill-white text-white ml-0.5" />
            )}
          </button>

          {/* Gemini AI Evaluation */}
          <button
            onClick={onAiEvaluation}
            disabled={aiLoading || !code.trim()}
            className="w-7 h-7 bg-purple-600 hover:bg-purple-500 border-2 border-ink-black shadow-[1px_1px_0px_0px_rgba(30,41,59,1)] active:translate-y-0.5 active:shadow-none text-white flex items-center justify-center cursor-pointer disabled:opacity-40 disabled:cursor-not-allowed transition-all"
            title={aiLoading ? 'Evaluating code with Gemini...' : 'Gemini Eval (AI code review & complexity)'}
            aria-label="Gemini Eval"
          >
            {aiLoading ? (
              <RefreshCw className="w-3.5 h-3.5 animate-spin" />
            ) : (
              <Sparkles className="w-3.5 h-3.5 text-amber-300" />
            )}
          </button>
        </div>
      </div>

      <div className="p-4 sm:p-5 flex-1 flex flex-col space-y-4">
        {/* Editor Shell - Pure Code Editor */}
        <div className="flex-1 flex flex-col min-h-[300px] sm:min-h-[360px] border-[3px] border-ink-black shadow-[4px_4px_0px_0px_rgba(30,41,59,1)] bg-[#0c111d] overflow-hidden">
          <div className="flex-1 overflow-hidden relative">
            <CodeMirror
              value={code}
              onChange={onChangeCode}
              placeholder="def solve(nums):&#10;    # Write your solution from memory here..."
              height="100%"
              theme="dark"
              extensions={codeStudioExtensions}
              basicSetup={{
                autocompletion: isAutocompleteEnabled,
                lineNumbers: true,
                foldGutter: true,
                dropCursor: true,
                allowMultipleSelections: true,
                indentOnInput: true,
                bracketMatching: true,
                closeBrackets: true,
                rectangularSelection: true,
                crosshairCursor: true,
                highlightActiveLine: true,
                highlightSelectionMatches: true,
                closeBracketsKeymap: true,
                defaultKeymap: true,
                searchKeymap: true,
                historyKeymap: true,
                foldKeymap: true,
                completionKeymap: true,
                lintKeymap: true,
              }}
              className="w-full h-full text-xs font-code-mono"
            />
          </div>
        </div>



        {/* Solution Saved Feedback Banner */}
        {savedRefSuccess && (
          <div className="p-3 bg-[#f0fdf4] border-2 border-emerald-600 text-emerald-950 text-xs font-semibold flex items-center gap-2 shadow-[2px_2px_0px_0px_rgba(30,41,59,1)]">
            <CheckCircle2 className="w-4 h-4 text-emerald-600 shrink-0" />
            <span>Successfully saved studio code as your personal reference solution!</span>
          </div>
        )}

        {/* Solution Save Error Alert */}
        {saveRefError && (
          <div className="p-3 bg-rose-50 border-2 border-rose-500 text-rose-800 text-xs font-semibold flex items-center gap-2 shadow-[2px_2px_0px_0px_rgba(30,41,59,1)]">
            <AlertTriangle className="w-4 h-4 text-rose-600 shrink-0" />
            <span>{saveRefError}</span>
          </div>
        )}

        {/* GitHub Commit Feedback Banner */}
        {githubPushedUrl && (
          <div className="p-3 bg-[#f0fdf4] border-2 border-emerald-600 text-emerald-950 text-xs font-semibold flex items-center justify-between shadow-[2px_2px_0px_0px_rgba(30,41,59,1)]">
            <div className="flex items-center gap-2">
              <CheckCircle2 className="w-4 h-4 text-emerald-600 shrink-0" />
              <span>Successfully pushed solution to your GitHub repository!</span>
            </div>
            <a
              href={githubPushedUrl}
              target="_blank"
              rel="noopener noreferrer"
              className="text-[11px] font-bold text-blue-700 hover:text-blue-900 underline flex items-center gap-1"
            >
              <span>View on GitHub</span>
              <ExternalLink className="w-3 h-3" />
            </a>
          </div>
        )}

        {/* GitHub Push Error Alert */}
        {githubPushError && (
          <div className="p-3 bg-rose-50 border-2 border-rose-500 text-rose-800 text-xs font-semibold flex items-center gap-2 shadow-[2px_2px_0px_0px_rgba(30,41,59,1)]">
            <AlertTriangle className="w-4 h-4 text-rose-600 shrink-0" />
            <span>{githubPushError}</span>
          </div>
        )}

        {/* Sandbox Running Scanner */}
        {sandboxRunning && (
          <div className="p-3 border-2 border-ink-black bg-emerald-50 shadow-[3px_3px_0px_0px_rgba(30,41,59,1)] flex items-center gap-3 animate-pulse">
            <RefreshCw className="w-5 h-5 text-emerald-600 animate-spin shrink-0" />
            <div>
              <h4 className="font-arcade text-[9px] font-bold text-ink-black uppercase">
                Running...
              </h4>
              <p className="font-sans text-[11px] text-trash-gray font-semibold mt-0.5">
                Executing against test cases...
              </p>
            </div>
          </div>
        )}

        {/* Sandbox Error Alert */}
        {sandboxError && (
          <div className="p-3 border-2 border-rose-500 bg-rose-50 text-rose-800 text-xs font-semibold shadow-[2px_2px_0px_0px_rgba(30,41,59,1)] flex items-center gap-2">
            <AlertTriangle className="w-4 h-4 text-rose-600 shrink-0" />
            <span>{sandboxError}</span>
          </div>
        )}

        {/* Sandbox Results Card */}
        {sandboxResults && (
          <div
            className={`border-[3px] border-ink-black p-4 sm:p-5 shadow-[4px_4px_0px_0px_rgba(30,41,59,1)] space-y-3.5 ${sandboxResults.status === 'passed' ? 'bg-[#f0fdf4]' : 'bg-[#fff1f2]'
              }`}
          >
            {/* Result Status Header */}
            <div className="flex flex-col sm:flex-row sm:items-center justify-between gap-2 pb-2.5 border-b-2 border-dashed border-ink-black">
              <div className="flex items-center gap-2.5">
                <div
                  className={`w-8 h-8 border-2 border-ink-black flex items-center justify-center font-bold shadow-[2px_2px_0px_0px_rgba(30,41,59,1)] ${sandboxResults.status === 'passed' ? 'bg-grass-green text-ink-black' : 'bg-rose-500 text-white'
                    }`}
                >
                  {sandboxResults.status === 'passed' ? (
                    <Check className="w-5 h-5 stroke-[3]" />
                  ) : (
                    <AlertTriangle className="w-4 h-4" />
                  )}
                </div>
                <div>
                  <div className="font-arcade text-[10px] font-bold text-ink-black flex items-center gap-1.5 uppercase">
                    {sandboxResults.status === 'passed' ? (
                      <>
                        <span>ALL TESTS PASSED</span>
                        <CheckCircle2 className="w-3.5 h-3.5 text-emerald-600 inline" />
                      </>
                    ) : (
                      <>
                        <span>{sandboxResults.status === 'error' ? 'EXECUTION ERROR' : 'TESTS FAILED'}</span>
                        <XCircle className="w-3.5 h-3.5 text-rose-600 inline" />
                      </>
                    )}
                  </div>
                  <div className="font-sans text-[11px] text-slate-600 font-semibold">
                    {sandboxResults.passed_count ?? 0} of {sandboxResults.total_tests ?? 0} test cases passed ({(sandboxResults.duration_ms ?? 0).toFixed(1)}ms)
                  </div>
                </div>
              </div>

              {/* Save Solution Button if passed */}
              {sandboxResults.status === 'passed' && (
                <button
                  onClick={onSaveReference}
                  disabled={savingReference}
                  className="px-3 py-1.5 bg-white hover:bg-emerald-100 border-2 border-ink-black text-ink-black font-arcade text-[8px] font-bold flex items-center gap-1.5 shadow-[2px_2px_0px_0px_rgba(30,41,59,1)] active:translate-y-0.5 active:shadow-none transition-all cursor-pointer shrink-0 disabled:opacity-50"
                >
                  {savingReference ? (
                    <RefreshCw className="w-3 h-3 animate-spin" />
                  ) : savedRefSuccess ? (
                    <CheckCircle2 className="w-3.5 h-3.5 text-emerald-600" />
                  ) : (
                    <Save className="w-3.5 h-3.5 text-blue-600" />
                  )}
                  <span>{savedRefSuccess ? 'SAVED TO DB!' : 'SAVE AS OFFICIAL SOLUTION'}</span>
                </button>
              )}
            </div>

            {saveRefError && (
              <div className="p-2 border border-rose-500 bg-rose-100 text-rose-800 text-[11px] font-semibold">
                {saveRefError}
              </div>
            )}

            {/* Test Case Breakdown */}
            {sandboxResults.results && sandboxResults.results.length > 0 && (
              <div className="space-y-2">
                <span className="font-arcade text-[8px] text-trash-gray uppercase font-bold block">
                  TEST CASE BREAKDOWN:
                </span>
                <div className="space-y-1.5 max-h-56 overflow-y-auto custom-scrollbar">
                  {sandboxResults.results.map((tc, idx) => (
                    <div
                      key={idx}
                      className={`p-2.5 border-2 border-ink-black text-xs font-mono shadow-[2px_2px_0px_0px_rgba(30,41,59,0.3)] ${tc.passed ? 'bg-white' : 'bg-rose-50'
                        }`}
                    >
                      <div className="flex items-center justify-between font-arcade text-[8px] mb-1">
                        <span className="font-bold text-ink-black">CASE #{idx + 1}</span>
                        <span className={tc.passed ? 'text-emerald-700 font-bold' : 'text-rose-700 font-bold'}>
                          {tc.passed ? '✓ PASSED' : '✗ FAILED'}
                        </span>
                      </div>
                      <div className="grid grid-cols-1 sm:grid-cols-3 gap-1.5 text-[10px]">
                        <div>
                          <span className="text-slate-500 block">Input:</span>
                          <span className="font-code-mono text-ink-black font-semibold break-all">{JSON.stringify(tc.input)}</span>
                        </div>
                        <div>
                          <span className="text-slate-500 block">Expected:</span>
                          <span className="font-code-mono text-emerald-700 font-semibold break-all">{JSON.stringify(tc.expected)}</span>
                        </div>
                        <div>
                          <span className="text-slate-500 block">Output:</span>
                          <span className={`font-code-mono font-semibold break-all ${tc.passed ? 'text-emerald-700' : 'text-rose-700'}`}>
                            {tc.output !== undefined ? JSON.stringify(tc.output) : 'None'}
                          </span>
                        </div>
                      </div>
                      {tc.error && (
                        <div className="mt-1.5 p-1.5 bg-rose-100 text-rose-900 text-[10px] font-mono border border-rose-300 break-all">
                          {tc.error}
                        </div>
                      )}
                    </div>
                  ))}
                </div>
              </div>
            )}

            {/* Error Trace if runtime error occurred */}
            {sandboxResults.error && (
              <div className="p-3 bg-[#1E293B] text-rose-400 font-code-mono text-[11px] border-2 border-ink-black overflow-x-auto whitespace-pre-wrap select-text">
                {sandboxResults.error}
              </div>
            )}

            {/* Smashed it shortcut hint if passed */}
            {sandboxResults.status === 'passed' && (
              <div className="p-2 bg-emerald-100/70 border border-emerald-300 text-emerald-900 text-xs flex items-center justify-between">
                <span className="font-sans font-medium">
                  ✨ All unit tests passed. Rate your recall below.
                </span>
              </div>
            )}
          </div>
        )}

        {/* AI Loading Scanner */}
        {aiLoading && (
          <div className="p-4 border-2 border-ink-black bg-amber-50 shadow-[3px_3px_0px_0px_rgba(30,41,59,1)] flex items-center gap-3 animate-pulse">
            <RefreshCw className="w-6 h-6 text-purple-600 animate-spin shrink-0" />
            <div>
              <h4 className="font-arcade text-[9px] font-bold text-ink-black uppercase">
                ANALYZING...
              </h4>
              <p className="font-sans text-[11px] text-trash-gray font-semibold mt-0.5">
                Checking edge cases, bounds, and Big-O efficiency against optimal constraints...
              </p>
            </div>
          </div>
        )}

        {/* AI Error Alert */}
        {aiError && (
          <div className="p-3 border-2 border-rose-500 bg-rose-50 text-rose-800 text-xs font-semibold shadow-[2px_2px_0px_0px_rgba(30,41,59,1)]">
            {aiError}
          </div>
        )}

        {/* AI Result Card */}
        {aiResult && (
          <div className="border-[3px] border-ink-black bg-[#fefce8] p-4 sm:p-5 shadow-[4px_4px_0px_0px_rgba(30,41,59,1)] space-y-4">
            {/* Verdict Banner */}
            <div className="flex items-center justify-between pb-3 border-b-2 border-dashed border-ink-black">
              <div className="flex items-center gap-3">
                <div
                  className={`w-9 h-9 border-2 border-ink-black flex items-center justify-center font-bold shadow-[2px_2px_0px_0px_rgba(30,41,59,1)] ${aiResult.is_correct
                      ? 'bg-grass-green text-ink-black'
                      : 'bg-rose-500 text-white'
                    }`}
                >
                  {aiResult.is_correct ? <Check className="w-5 h-5 stroke-[3]" /> : <Award className="w-5 h-5" />}
                </div>
                <div>
                  <span className="font-arcade text-[10px] font-bold text-ink-black flex items-center gap-1.5">
                    {aiResult.is_correct ? (
                      <>
                        STATUS: PASSED
                        <CheckCircle2 className="w-3.5 h-3.5 text-emerald-600 inline" />
                      </>
                    ) : (
                      <>
                        STATUS: FAILED
                        <XCircle className="w-3.5 h-3.5 text-rose-600 inline" />
                      </>
                    )}
                  </span>
                  <span className="font-sans text-[11px] text-slate-600 font-semibold">
                    {aiResult.is_correct
                      ? 'Algorithm matches expected logic and patterns.'
                      : 'Bugs or edge-case failures detected.'}
                  </span>
                </div>
              </div>

              <span
                className={`font-arcade text-[9px] px-2 py-1 border-2 border-ink-black uppercase font-bold shadow-[2px_2px_0px_0px_rgba(30,41,59,1)] ${aiResult.is_correct
                    ? 'bg-emerald-100 text-emerald-800'
                    : 'bg-rose-100 text-rose-800'
                  }`}
              >
                {aiResult.is_correct ? 'CORRECT' : 'INCORRECT'}
              </span>
            </div>

            {/* Complexity Comparison Grid */}
            <div className="grid grid-cols-2 gap-3">
              <div className="p-2.5 bg-white border-2 border-ink-black shadow-[2px_2px_0px_0px_rgba(30,41,59,1)]">
                <span className="font-arcade text-[8px] text-trash-gray block uppercase font-bold">
                  Time Complexity
                </span>
                <div className="flex items-baseline gap-2 mt-1">
                  <span className="font-code-mono text-xs font-bold text-ink-black">
                    {aiResult.detected_complexity?.time || 'N/A'}
                  </span>
                  <span className="text-[10px] font-sans text-slate-500 font-semibold">
                    (Target: {optimalTime || 'O(N)'})
                  </span>
                </div>
              </div>

              <div className="p-2.5 bg-white border-2 border-ink-black shadow-[2px_2px_0px_0px_rgba(30,41,59,1)]">
                <span className="font-arcade text-[8px] text-trash-gray block uppercase font-bold">
                  Space Complexity
                </span>
                <div className="flex items-baseline gap-2 mt-1">
                  <span className="font-code-mono text-xs font-bold text-ink-black">
                    {aiResult.detected_complexity?.space || 'N/A'}
                  </span>
                  <span className="text-[10px] font-sans text-slate-500 font-semibold">
                    (Target: {optimalSpace || 'O(1)'})
                  </span>
                </div>
              </div>
            </div>

            {/* Bugs or Traps */}
            {aiResult.bugs && aiResult.bugs.length > 0 && (
              <div className="p-3 bg-white border-2 border-ink-black space-y-1.5 shadow-[2px_2px_0px_0px_rgba(30,41,59,1)]">
                <span className="font-arcade text-[8px] text-rose-600 uppercase font-bold block flex items-center gap-1">
                  <AlertTriangle className="w-3 h-3 text-rose-500" />
                  TRACES & EDGE CASE ISSUES:
                </span>
                <ul className="list-disc list-inside text-xs text-slate-800 space-y-1 font-semibold pl-1">
                  {aiResult.bugs.map((bug: string, idx: number) => (
                    <li key={idx} className="leading-relaxed select-text">
                      {bug}
                    </li>
                  ))}
                </ul>
              </div>
            )}

            {/* Refactoring Tip */}
            {aiResult.key_suggestion && (
              <div className="p-3 bg-amber-50 border-2 border-ink-black space-y-1 shadow-[2px_2px_0px_0px_rgba(30,41,59,1)]">
                <span className="font-arcade text-[8px] text-amber-700 uppercase font-bold block flex items-center gap-1">
                  <Sparkles className="w-3 h-3 text-amber-500" />
                  TIP:
                </span>
                <p className="text-xs text-slate-800 font-medium leading-relaxed m-0 select-text">
                  {aiResult.key_suggestion}
                </p>
              </div>
            )}

            {/* Recommendation Callout */}
            <div className="p-2.5 bg-white border-2 border-dashed border-ink-black flex items-center justify-between">
              <span className="text-xs font-bold text-ink-black flex items-center gap-1.5">
                <Info className="w-4 h-4 text-primary" />
                Recommendation:
              </span>
              <span className="font-arcade text-[9px] font-bold text-purple-700">
                {aiResult.is_correct
                  ? 'Rate your recall below.'
                  : 'Review issues and rate recall below.'}
              </span>
            </div>
          </div>
        )}
      </div>
    </div>
  );
};