File size: 47,721 Bytes
674fb4e
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
import React, { useState, useEffect, useCallback } from 'react';
import { useAuth } from '../context/AuthContext';
import { BarChart2, TrendingUp, AlertTriangle, CheckCircle, RefreshCw, Zap, FileText, GitCommit, MessageSquare } from 'lucide-react';

const API_BASE = import.meta.env.VITE_API_BASE_URL || 'http://localhost:8000/api';

interface EvalDashboard {
  total_evaluations: number;
  avg_overall_score: number;
  avg_faithfulness: number;
  avg_relevancy: number;
  hallucination_rate: number;
  trend_data: TrendPoint[];
}

interface TrendPoint {
  timestamp: string;
  overall_score: number;
  faithfulness: number;
  answer_relevancy: number;
  hallucination_detected: boolean;
  document_id?: string;
}

interface EvalForm {
  question: string;
  answer: string;
  contexts: string;
}

// Helper: build markdown from report result
function buildReportMarkdown(result: any, topic: string): string {
  const lines: string[] = [];
  lines.push(`# ${result.topic || topic}`);
  lines.push('');
  if (result.confidence !== undefined) {
    lines.push(`**Confidence:** ${(result.confidence * 100).toFixed(1)}%`);
  }
  if (result.tool_calls_made !== undefined) {
    lines.push(`**Tool calls:** ${result.tool_calls_made}`);
  }
  lines.push(`**Generated:** ${new Date().toLocaleString()}`);
  lines.push('');

  if (result.executive_summary) {
    lines.push('## Executive Summary');
    lines.push('');
    lines.push(result.executive_summary);
    lines.push('');
  }

  if (result.sections && typeof result.sections === 'object' && !Array.isArray(result.sections)) {
    Object.entries(result.sections).forEach(([title, content], i) => {
      lines.push(`## ${i + 1}. ${title}`);
      lines.push('');
      lines.push(String(content));
      lines.push('');
    });
  } else if (Array.isArray(result.sections)) {
    result.sections.forEach((s: any, i: number) => {
      lines.push(`## ${i + 1}. ${s.title || ''}`);
      lines.push('');
      lines.push(s.content || '');
      lines.push('');
    });
  } else if (!result.sections) {
    lines.push(result.report || result.content || result.markdown || JSON.stringify(result, null, 2));
    lines.push('');
  }

  if (result.key_entities && result.key_entities.length > 0) {
    lines.push('## Key Entities');
    lines.push('');
    lines.push(result.key_entities.join(', '));
  }

  return lines.join('\n');
}

const InsightsView: React.FC = () => {
  const { token } = useAuth();
  const [dashboard, setDashboard] = useState<EvalDashboard | null>(null);
  const [loading, setLoading] = useState(false);
  const [evalForm, setEvalForm] = useState<EvalForm>({ question: '', answer: '', contexts: '' });
  const [evalResult, setEvalResult] = useState<any>(null);
  const [evalLoading, setEvalLoading] = useState(false);
  const [communities, setCommunities] = useState<any[]>([]);
  const [communityLoading, setCommunityLoading] = useState(false);
  const [activeTab, setActiveTab] = useState<'metrics' | 'evaluate' | 'communities' | 'export' | 'report' | 'graph-update' | 'entity-chat'>('metrics');

  // Report Agent state
  const [reportTopic, setReportTopic] = useState('');
  const [reportDepth, setReportDepth] = useState(3);
  const [reportResult, setReportResult] = useState<any>(null);
  const [reportLoading, setReportLoading] = useState(false);
  const [reportTimestamp, setReportTimestamp] = useState<Date | null>(null);
  const [copyDone, setCopyDone] = useState(false);

  // Graph Update state
  const [updateText, setUpdateText] = useState('');
  const [updateResult, setUpdateResult] = useState<any>(null);
  const [updateLoading, setUpdateLoading] = useState(false);

  // Entity Chat state
  const [entities, setEntities] = useState<any[]>([]);
  const [selectedEntity, setSelectedEntity] = useState('');
  const [entityContext, setEntityContext] = useState('');
  const [chatMsg, setChatMsg] = useState('');
  const [chatHistory, setChatHistory] = useState<{role: string; content: string}[]>([]);
  const [chatLoading, setChatLoading] = useState(false);

  const fetchDashboard = useCallback(async () => {
    setLoading(true);
    try {
      const res = await fetch(`${API_BASE}/eval/dashboard?limit=200`, {
        headers: { Authorization: `Bearer ${token}` }
      });
      if (res.ok) setDashboard(await res.json());
    } catch (e) { console.error(e); }
    setLoading(false);
  }, [token]);

  const fetchCommunities = useCallback(async () => {
    setCommunityLoading(true);
    try {
      const res = await fetch(`${API_BASE}/graph/communities?limit=30`, {
        headers: { Authorization: `Bearer ${token}` }
      });
      if (res.ok) {
        const data = await res.json();
        setCommunities(data.communities || []);
      }
    } catch (e) { console.error(e); }
    setCommunityLoading(false);
  }, [token]);

  const fetchEntities = useCallback(async () => {
    try {
      const res = await fetch(`${API_BASE}/admin/graph/nodes?query=&limit=200`, {
        headers: { Authorization: `Bearer ${token}` }
      });
      if (res.ok) {
        const data = await res.json();
        setEntities((data.nodes || []).slice(0, 100));
      }
    } catch {}
  }, [token]);

  useEffect(() => {
    fetchDashboard();
    fetchCommunities();
    fetchEntities();
  }, [fetchDashboard, fetchCommunities, fetchEntities]);

  const runEval = async () => {
    if (!evalForm.question || !evalForm.answer || !evalForm.contexts) return;
    setEvalLoading(true);
    setEvalResult(null);
    try {
      const res = await fetch(`${API_BASE}/eval/score`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` },
        body: JSON.stringify({
          question: evalForm.question,
          answer: evalForm.answer,
          contexts: evalForm.contexts.split('\n---\n').map(c => c.trim()).filter(Boolean)
        })
      });
      if (res.ok) {
        setEvalResult(await res.json());
        fetchDashboard(); // Refresh metrics
      }
    } catch (e) { console.error(e); }
    setEvalLoading(false);
  };

  const runReport = async () => {
    if (!reportTopic.trim()) return;
    setReportLoading(true); setReportResult(null);
    try {
      const res = await fetch(`${API_BASE}/report`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` },
        body: JSON.stringify({ topic: reportTopic, depth: reportDepth })
      });
      if (res.ok) {
        setReportResult(await res.json());
        setReportTimestamp(new Date());
      }
    } catch (e) { console.error(e); }
    setReportLoading(false);
  };

  const runGraphUpdate = async () => {
    if (!updateText.trim()) return;
    setUpdateLoading(true); setUpdateResult(null);
    try {
      const res = await fetch(`${API_BASE}/graph/update`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` },
        body: JSON.stringify({ text: updateText })
      });
      if (res.ok) setUpdateResult(await res.json());
    } catch (e) { console.error(e); }
    setUpdateLoading(false);
  };

  const sendEntityChat = async () => {
    if (!selectedEntity || !chatMsg.trim()) return;
    const userMsg = { role: 'user', content: chatMsg };
    setChatHistory(h => [...h, userMsg]);
    setChatMsg('');
    setChatLoading(true);
    try {
      const res = await fetch(`${API_BASE}/entities/${encodeURIComponent(selectedEntity)}/chat`, {
        method: 'POST',
        headers: { 'Content-Type': 'application/json', Authorization: `Bearer ${token}` },
        body: JSON.stringify({ message: userMsg.content })
      });
      if (res.ok) {
        const data = await res.json();
        setChatHistory(h => [...h, { role: 'assistant', content: data.response || data.answer || JSON.stringify(data) }]);
      }
    } catch (e) { console.error(e); }
    setChatLoading(false);
  };

  const assignCommunities = async () => {
    setCommunityLoading(true);
    try {
      const res = await fetch(`${API_BASE}/graph/communities/assign`, {
        method: 'POST',
        headers: { Authorization: `Bearer ${token}` }
      });
      if (res.ok) {
        const data = await res.json();
        alert(`✓ ${data.message}`);
        fetchCommunities();
      }
    } catch (e) { console.error(e); }
    setCommunityLoading(false);
  };

  const exportGraph = async (fmt: string) => {
    const res = await fetch(`${API_BASE}/graph/export?format=${fmt}`, {
      headers: { Authorization: `Bearer ${token}` }
    });
    if (!res.ok) return;
    const blob = await res.blob();
    const ext = fmt === 'json' ? 'json' : fmt === 'cypher' ? 'cypher' : 'graphml';
    const url = URL.createObjectURL(blob);
    const a = document.createElement('a');
    a.href = url; a.download = `graph_export.${ext}`; a.click();
    URL.revokeObjectURL(url);
  };

  const handleCopyReport = () => {
    if (!reportResult) return;
    const text = buildReportMarkdown(reportResult, reportTopic);
    navigator.clipboard.writeText(text).then(() => {
      setCopyDone(true);
      setTimeout(() => setCopyDone(false), 2000);
    });
  };

  const handleDownloadReport = () => {
    if (!reportResult) return;
    const text = buildReportMarkdown(reportResult, reportTopic);
    const blob = new Blob([text], { type: 'text/markdown' });
    const url = URL.createObjectURL(blob);
    const a = document.createElement('a');
    a.href = url;
    a.download = `report_${reportTopic.slice(0, 30).replace(/\s+/g, '_')}.md`;
    a.click();
    URL.revokeObjectURL(url);
  };

  const score2color = (s: number) => {
    if (s >= 0.8) return '#16a34a';
    if (s >= 0.6) return '#d97706';
    return '#dc2626';
  };
  const score2label = (s: number) => s >= 0.8 ? 'HIGH' : s >= 0.6 ? 'MEDIUM' : 'LOW';

  const ScoreBar: React.FC<{ label: string; value: number }> = ({ label, value }) => (
    <div style={{ marginBottom: '1rem' }}>
      <div style={{ display: 'flex', justifyContent: 'space-between', marginBottom: '0.25rem', fontFamily: 'var(--font-mono)', fontSize: '0.8rem' }}>
        <span>{label}</span>
        <span style={{ color: score2color(value), fontWeight: 700 }}>{(value * 100).toFixed(1)}%</span>
      </div>
      <div style={{ height: '8px', background: '#e5e7eb', position: 'relative' }}>
        <div style={{ position: 'absolute', left: 0, top: 0, height: '100%', width: `${value * 100}%`, background: score2color(value), transition: 'width 0.6s ease' }} />
      </div>
    </div>
  );

  const confidenceBadgeColor = (c: number) =>
    c >= 0.8 ? '#16a34a' : c >= 0.6 ? '#d97706' : '#dc2626';

  return (
    <div className="container" style={{ maxWidth: '1100px', paddingBottom: '3rem' }}>
      <div className="page-header flex-between" style={{ marginBottom: '2rem' }}>
        <div>
          <h1>INSIGHTS HQ</h1>
          <p className="mono-text">QUALITY METRICS // EVAL DASHBOARD // GRAPH INTELLIGENCE</p>
        </div>
        <div style={{ display: 'flex', gap: '0.5rem' }}>
          <button className="app-btn mono-text" onClick={fetchDashboard} disabled={loading} style={{ padding: '0.5rem 1rem', fontSize: '0.8rem' }}>
            <RefreshCw size={14} style={{ display: 'inline', marginRight: '0.4rem' }} />
            REFRESH
          </button>
        </div>
      </div>

      {/* Tab Nav */}
      <div style={{ display: 'flex', borderBottom: '3px solid #000', marginBottom: '2rem', gap: 0, flexWrap: 'wrap' }}>
        {([
          { id: 'metrics', label: 'METRICS' },
          { id: 'evaluate', label: 'EVALUATE' },
          { id: 'communities', label: 'COMMUNITIES' },
          { id: 'export', label: 'EXPORT' },
          { id: 'report', label: '⚡ REPORT' },
          { id: 'graph-update', label: '↑ LIVE UPDATE' },
          { id: 'entity-chat', label: '💬 ENTITY CHAT' },
        ] as const).map(t => (
          <button
            key={t.id}
            onClick={() => setActiveTab(t.id as any)}
            className="mono-text"
            style={{
              padding: '0.65rem 1.2rem', fontWeight: 700, fontSize: '0.78rem', letterSpacing: '0.8px',
              border: 'none', borderBottom: activeTab === t.id ? '3px solid #000' : 'none',
              background: activeTab === t.id ? '#000' : 'transparent',
              color: activeTab === t.id ? '#fff' : '#000', cursor: 'pointer',
              marginBottom: '-3px', textTransform: 'uppercase', whiteSpace: 'nowrap'
            }}
          >
            {t.label}
          </button>
        ))}
      </div>

      {/* ── METRICS TAB ── */}
      {activeTab === 'metrics' && (
        <div>
          {loading ? (
            <div className="mono-text" style={{ textAlign: 'center', padding: '3rem', color: '#aaa' }}>LOADING METRICS...</div>
          ) : !dashboard || dashboard.total_evaluations === 0 ? (
            <div style={{ border: '3px solid #000', padding: '3rem', textAlign: 'center' }}>
              <BarChart2 size={48} style={{ marginBottom: '1rem', opacity: 0.3 }} />
              <p className="mono-text" style={{ color: '#777' }}>NO EVALUATION DATA YET</p>
              <p className="mono-text" style={{ color: '#999', fontSize: '0.85rem', marginTop: '0.5rem' }}>
                Use the EVALUATE tab to score Q&A pairs and build your quality history.
              </p>
            </div>
          ) : (
            <>
              {/* KPI Cards */}
              <div style={{ display: 'grid', gridTemplateColumns: 'repeat(auto-fit, minmax(200px, 1fr))', gap: '1rem', marginBottom: '2rem' }}>
                {[
                  { label: 'TOTAL EVALS', value: dashboard.total_evaluations, raw: true, icon: <BarChart2 size={20} /> },
                  { label: 'AVG QUALITY', value: dashboard.avg_overall_score, icon: <TrendingUp size={20} /> },
                  { label: 'FAITHFULNESS', value: dashboard.avg_faithfulness, icon: <CheckCircle size={20} /> },
                  { label: 'HALLUCINATION RATE', value: dashboard.hallucination_rate, invert: true, icon: <AlertTriangle size={20} /> },
                ].map(card => (
                  <div key={card.label} style={{ border: '3px solid #000', padding: '1.5rem', background: (card.invert ? dashboard.hallucination_rate > 0.3 : false) ? '#fff5f5' : '#fff' }}>
                    <div style={{ display: 'flex', justifyContent: 'space-between', alignItems: 'flex-start', marginBottom: '1rem' }}>
                      <span className="mono-text" style={{ fontSize: '0.7rem', fontWeight: 700, letterSpacing: '1px', color: '#666' }}>{card.label}</span>
                      {card.icon}
                    </div>
                    <div className="mono-text" style={{
                      fontSize: '2rem', fontWeight: 900,
                      color: card.raw ? '#000' : score2color(card.invert ? 1 - (card.value as number) : (card.value as number))
                    }}>
                      {card.raw ? card.value : `${((card.value as number) * 100).toFixed(1)}%`}
                    </div>
                  </div>
                ))}
              </div>

              {/* Score Breakdown */}
              <div style={{ border: '3px solid #000', padding: '1.5rem', marginBottom: '2rem' }}>
                <h3 className="mono-text" style={{ marginBottom: '1.5rem', borderBottom: '1px dotted #000', paddingBottom: '0.5rem' }}>METRIC BREAKDOWN</h3>
                <ScoreBar label="Overall Quality Score" value={dashboard.avg_overall_score} />
                <ScoreBar label="Faithfulness (grounding)" value={dashboard.avg_faithfulness} />
                <ScoreBar label="Answer Relevancy" value={dashboard.avg_relevancy} />
                <ScoreBar label="Non-hallucination Rate" value={1 - dashboard.hallucination_rate} />
              </div>

              {/* Trend Table */}
              {dashboard.trend_data.length > 0 && (
                <div style={{ border: '3px solid #000', padding: '1.5rem' }}>
                  <h3 className="mono-text" style={{ marginBottom: '1.5rem', borderBottom: '1px dotted #000', paddingBottom: '0.5rem' }}>EVALUATION HISTORY (LATEST {Math.min(dashboard.trend_data.length, 20)})</h3>
                  <div style={{ overflowX: 'auto' }}>
                    <table style={{ width: '100%', borderCollapse: 'collapse', fontFamily: 'var(--font-mono)', fontSize: '0.8rem' }}>
                      <thead>
                        <tr style={{ borderBottom: '2px solid #000' }}>
                          {['TIMESTAMP', 'QUALITY', 'FAITHFULNESS', 'RELEVANCY', 'HALLUCINATION'].map(h => (
                            <th key={h} style={{ padding: '0.5rem', textAlign: 'left', fontWeight: 700 }}>{h}</th>
                          ))}
                        </tr>
                      </thead>
                      <tbody>
                        {dashboard.trend_data.slice(0, 20).map((p, i) => (
                          <tr key={i} style={{ borderBottom: '1px dotted #ddd', background: i % 2 === 0 ? '#fafafa' : '#fff' }}>
                            <td style={{ padding: '0.5rem' }}>{p.timestamp}</td>
                            <td style={{ padding: '0.5rem', color: score2color(p.overall_score), fontWeight: 700 }}>{(p.overall_score * 100).toFixed(1)}%</td>
                            <td style={{ padding: '0.5rem', color: score2color(p.faithfulness) }}>{(p.faithfulness * 100).toFixed(1)}%</td>
                            <td style={{ padding: '0.5rem', color: score2color(p.answer_relevancy) }}>{(p.answer_relevancy * 100).toFixed(1)}%</td>
                            <td style={{ padding: '0.5rem' }}>
                              <span style={{ background: p.hallucination_detected ? '#dc2626' : '#16a34a', color: '#fff', padding: '0.1rem 0.5rem', fontSize: '0.7rem', fontWeight: 700 }}>
                                {p.hallucination_detected ? 'YES' : 'NO'}
                              </span>
                            </td>
                          </tr>
                        ))}
                      </tbody>
                    </table>
                  </div>
                </div>
              )}
            </>
          )}
        </div>
      )}

      {/* ── EVALUATE TAB ── */}
      {activeTab === 'evaluate' && (
        <div>
          <div style={{ border: '3px solid #000', padding: '2rem', marginBottom: '2rem' }}>
            <h3 className="mono-text" style={{ marginBottom: '1.5rem' }}>
              <Zap size={16} style={{ display: 'inline', marginRight: '0.5rem' }} />
              SCORE A Q&A PAIR
            </h3>
            <p className="mono-text" style={{ fontSize: '0.8rem', color: '#666', marginBottom: '1.5rem' }}>
              Paste a question, its generated answer, and the retrieved context chunks (separate chunks with "---").
            </p>

            <div style={{ display: 'flex', flexDirection: 'column', gap: '1rem' }}>
              <div>
                <label className="mono-text" style={{ fontSize: '0.8rem', fontWeight: 700, display: 'block', marginBottom: '0.5rem' }}>QUESTION</label>
                <textarea value={evalForm.question} onChange={e => setEvalForm(f => ({ ...f, question: e.target.value }))}
                  rows={2} style={{ width: '100%', border: '2px solid #000', padding: '0.75rem', fontFamily: 'var(--font-mono)', fontSize: '0.9rem', resize: 'vertical', boxSizing: 'border-box' }} />
              </div>
              <div>
                <label className="mono-text" style={{ fontSize: '0.8rem', fontWeight: 700, display: 'block', marginBottom: '0.5rem' }}>GENERATED ANSWER</label>
                <textarea value={evalForm.answer} onChange={e => setEvalForm(f => ({ ...f, answer: e.target.value }))}
                  rows={4} style={{ width: '100%', border: '2px solid #000', padding: '0.75rem', fontFamily: 'var(--font-mono)', fontSize: '0.9rem', resize: 'vertical', boxSizing: 'border-box' }} />
              </div>
              <div>
                <label className="mono-text" style={{ fontSize: '0.8rem', fontWeight: 700, display: 'block', marginBottom: '0.5rem' }}>CONTEXT CHUNKS (separate with "---" on its own line)</label>
                <textarea value={evalForm.contexts} onChange={e => setEvalForm(f => ({ ...f, contexts: e.target.value }))}
                  rows={6} placeholder={'Context chunk 1 text...\n---\nContext chunk 2 text...'} style={{ width: '100%', border: '2px solid #000', padding: '0.75rem', fontFamily: 'var(--font-mono)', fontSize: '0.85rem', resize: 'vertical', boxSizing: 'border-box' }} />
              </div>
              <button className="app-btn mono-text" onClick={runEval} disabled={evalLoading || !evalForm.question || !evalForm.answer || !evalForm.contexts}
                style={{ alignSelf: 'flex-start', padding: '0.75rem 2rem' }}>
                {evalLoading ? 'EVALUATING...' : 'RUN EVALUATION'}
              </button>
            </div>
          </div>

          {evalResult && (
            <div style={{ border: '3px solid #000', padding: '2rem', background: evalResult.hallucination_detected ? '#fff5f5' : '#f0fdf4' }}>
              <h3 className="mono-text" style={{ marginBottom: '1.5rem' }}>
                {evalResult.hallucination_detected
                  ? <><AlertTriangle size={16} style={{ display: 'inline', marginRight: '0.5rem', color: '#dc2626' }} />HALLUCINATION RISK DETECTED</>
                  : <><CheckCircle size={16} style={{ display: 'inline', marginRight: '0.5rem', color: '#16a34a' }} />EVALUATION RESULTS</>
                }
              </h3>
              <div style={{ display: 'grid', gridTemplateColumns: 'repeat(auto-fit, minmax(150px, 1fr))', gap: '1rem', marginBottom: '1.5rem' }}>
                {[
                  { label: 'OVERALL', value: evalResult.overall_score },
                  { label: 'FAITHFULNESS', value: evalResult.faithfulness },
                  { label: 'RELEVANCY', value: evalResult.answer_relevancy },
                  { label: 'PRECISION', value: evalResult.context_precision },
                ].map(m => (
                  <div key={m.label} style={{ border: `2px solid ${score2color(m.value)}`, padding: '1rem', textAlign: 'center' }}>
                    <div className="mono-text" style={{ fontSize: '0.7rem', fontWeight: 700, color: '#666', marginBottom: '0.5rem' }}>{m.label}</div>
                    <div className="mono-text" style={{ fontSize: '1.8rem', fontWeight: 900, color: score2color(m.value) }}>
                      {(m.value * 100).toFixed(0)}%
                    </div>
                    <div className="mono-text" style={{ fontSize: '0.65rem', color: score2color(m.value), marginTop: '0.25rem' }}>{score2label(m.value)}</div>
                  </div>
                ))}
              </div>
              <div className="mono-text" style={{ fontSize: '0.75rem', color: '#777', borderTop: '1px dotted #ccc', paddingTop: '0.75rem' }}>
                Saved to Neo4j for trending. Eval ID: {evalResult.eval_id || 'N/A'}
              </div>
            </div>
          )}
        </div>
      )}

      {/* ── COMMUNITIES TAB ── */}
      {activeTab === 'communities' && (
        <div>
          <div style={{ display: 'flex', gap: '1rem', marginBottom: '2rem', alignItems: 'center', flexWrap: 'wrap' }}>
            <button className="app-btn mono-text" onClick={assignCommunities} disabled={communityLoading} style={{ padding: '0.75rem 1.5rem' }}>
              {communityLoading ? 'DETECTING...' : '⚡ DETECT COMMUNITIES'}
            </button>
            <span className="mono-text" style={{ fontSize: '0.8rem', color: '#666' }}>
              Run after ingesting documents to cluster entities into related groups (enables community search).
            </span>
            {communities.length === 0 && !communityLoading && (
              <span style={{ 
                background: '#dc2626', 
                color: '#fff', 
                padding: '0.25rem 0.75rem', 
                borderRadius: '4px', 
                fontFamily: 'var(--font-mono)', 
                fontSize: '0.75rem', 
                fontWeight: 700, 
                display: 'flex', 
                alignItems: 'center', 
                gap: '0.35rem',
                animation: 'pulse 2s infinite'
              }}>
                <AlertTriangle size={14} /> 
                REQUIRED: GENERATE REPORTS
              </span>
            )}
          </div>

          {communities.length === 0 ? (
            <div style={{ border: '3px solid #000', padding: '3rem', textAlign: 'center' }}>
              <p className="mono-text" style={{ color: '#777' }}>NO COMMUNITIES DETECTED YET</p>
              <p className="mono-text" style={{ color: '#999', fontSize: '0.85rem', marginTop: '0.5rem' }}>Click "Detect Communities" above to cluster your knowledge graph entities.</p>
            </div>
          ) : (
            <div style={{ display: 'grid', gridTemplateColumns: 'repeat(auto-fill, minmax(300px, 1fr))', gap: '1rem' }}>
              {communities.map((c, i) => (
                <div key={i} style={{ border: '2px solid #000', padding: '1.25rem' }}>
                  <div style={{ display: 'flex', justifyContent: 'space-between', marginBottom: '0.75rem' }}>
                    <span className="mono-text" style={{ fontWeight: 700, fontSize: '0.85rem' }}>CLUSTER #{c.community_id}</span>
                    <span className="mono-text" style={{ fontSize: '0.75rem', background: '#000', color: '#fff', padding: '0.15rem 0.5rem' }}>
                      {c.entity_count} entities
                    </span>
                  </div>
                  <div style={{ display: 'flex', flexWrap: 'wrap', gap: '0.35rem' }}>
                    {(c.sample_entities || []).map((e: string, ei: number) => (
                      <span key={ei} style={{ background: '#f3f4f6', border: '1px solid #d1d5db', padding: '0.15rem 0.5rem', fontSize: '0.75rem', fontFamily: 'var(--font-mono)' }}>{e}</span>
                    ))}
                    {c.entity_count > (c.sample_entities || []).length && (
                      <span style={{ color: '#666', fontSize: '0.75rem', fontFamily: 'var(--font-mono)', padding: '0.15rem 0' }}>
                        +{c.entity_count - (c.sample_entities || []).length} more
                      </span>
                    )}
                  </div>
                </div>
              ))}
            </div>
          )}
        </div>
      )}

      {/* ── EXPORT TAB ── */}
      {activeTab === 'export' && (
        <div>
          <div style={{ border: '3px solid #000', padding: '2rem', marginBottom: '2rem' }}>
            <h3 className="mono-text" style={{ marginBottom: '1.5rem' }}>EXPORT KNOWLEDGE GRAPH</h3>
            <p className="mono-text" style={{ fontSize: '0.85rem', color: '#555', marginBottom: '2rem', lineHeight: '1.6' }}>
              Export your knowledge graph for use in external tools, backups, or further analysis.
            </p>
            <div style={{ display: 'grid', gridTemplateColumns: 'repeat(auto-fit, minmax(250px, 1fr))', gap: '1rem' }}>
              {[
                { fmt: 'json', label: 'JSON', desc: 'Nodes & edges as JSON. For custom processing or visualization.' },
                { fmt: 'cypher', label: 'CYPHER', desc: 'Cypher CREATE statements. Re-import to any Neo4j instance.' },
                { fmt: 'graphml', label: 'GRAPHML', desc: 'GraphML XML. Compatible with Gephi, yEd, and most graph tools.' },
              ].map(e => (
                <div key={e.fmt} style={{ border: '2px solid #000', padding: '1.5rem' }}>
                  <div className="mono-text" style={{ fontWeight: 900, fontSize: '1.1rem', marginBottom: '0.5rem' }}>.{e.label}</div>
                  <p style={{ fontSize: '0.82rem', color: '#555', marginBottom: '1.25rem', fontFamily: 'var(--font-sans)', lineHeight: '1.5' }}>{e.desc}</p>
                  <button className="app-btn mono-text" onClick={() => exportGraph(e.fmt)} style={{ width: '100%', textAlign: 'center' }}>
                    DOWNLOAD .{e.label}
                  </button>
                </div>
              ))}
            </div>
          </div>
        </div>
      )}

      {/* ── REPORT AGENT TAB ── */}
      {activeTab === 'report' && (
        <div>
          <div className="page-info-bar">
            <FileText size={14}/>
            <span><strong>REPORT AGENT</strong> — Enter any topic or question. The ReACT agent autonomously queries the knowledge graph using multiple retrieval strategies and synthesizes a deep multi-section report. More <strong>depth</strong> = more reasoning steps.</span>
          </div>

          {/* Input form */}
          <div style={{ border: '3px solid #000', padding: '2rem', marginBottom: '2rem' }}>
            <h3 className="mono-text" style={{ marginBottom: '1.5rem' }}>
              <Zap size={16} style={{ display: 'inline', marginRight: '0.5rem' }}/>GENERATE ANALYTICAL REPORT
            </h3>
            <div style={{ display: 'flex', flexDirection: 'column', gap: '1rem' }}>
              <div>
                <label className="mono-text" style={{ fontSize: '0.8rem', fontWeight: 700, display: 'block', marginBottom: '0.5rem' }}>TOPIC OR QUESTION</label>
                <textarea value={reportTopic} onChange={e => setReportTopic(e.target.value)}
                  rows={3} placeholder="e.g. 'Summarize all relationships between Company X and its investors'"
                  style={{ width: '100%', border: '2px solid #000', padding: '0.75rem', fontFamily: 'var(--font-mono)', fontSize: '0.9rem', resize: 'vertical', boxSizing: 'border-box' }}/>
              </div>
              <div style={{ display: 'flex', gap: '1rem', alignItems: 'center' }}>
                <div>
                  <label className="mono-text" style={{ fontSize: '0.8rem', fontWeight: 700, display: 'block', marginBottom: '0.5rem' }}>DEPTH (REASONING STEPS)</label>
                  <select value={reportDepth} onChange={e => setReportDepth(Number(e.target.value))}
                    style={{ border: '2px solid #000', padding: '0.5rem 0.75rem', fontFamily: 'var(--font-mono)', fontSize: '0.9rem' }}>
                    {[1, 2, 3, 4, 5].map(n => <option key={n} value={n}>{n} {n === 1 ? '(fast)' : n === 5 ? '(deep)' : ''}</option>)}
                  </select>
                </div>
                <button className="app-btn mono-text" onClick={runReport} disabled={reportLoading || !reportTopic.trim()}
                  style={{ alignSelf: 'flex-end', padding: '0.75rem 2rem' }}>
                  {reportLoading ? 'GENERATING...' : 'GENERATE REPORT'}
                </button>
              </div>
            </div>
          </div>

          {/* Empty state */}
          {!reportResult && !reportLoading && (
            <div style={{ border: '3px dashed #e5e5e5', padding: '4rem 2rem', textAlign: 'center' }}>
              <FileText size={44} style={{ opacity: 0.18, marginBottom: '1rem' }} />
              <p className="mono-text" style={{ color: '#aaa', fontSize: '0.85rem', letterSpacing: '1px' }}>NO REPORT GENERATED YET</p>
              <p style={{ color: '#bbb', fontSize: '0.8rem', marginTop: '0.5rem', fontFamily: 'var(--font-sans)' }}>
                Enter a topic above and click Generate to produce an AI-powered analytical report from the knowledge graph.
              </p>
            </div>
          )}

          {/* Loading state */}
          {reportLoading && (
            <div style={{ border: '3px solid #000', padding: '2.5rem', textAlign: 'center' }}>
              <div style={{ fontFamily: 'var(--font-mono)', fontWeight: 700, fontSize: '0.9rem', marginBottom: '0.75rem' }}>
                ⚡ AGENT REASONING…
              </div>
              <div style={{ fontFamily: 'var(--font-sans)', fontSize: '0.82rem', color: 'var(--muted-color)' }}>
                Querying knowledge graph with depth {reportDepth}. This may take 20–60 seconds.
              </div>
            </div>
          )}

          {/* Report output */}
          {reportResult && (
            <div style={{ border: '3px solid #000' }}>
              {/* Report header bar */}
              <div style={{ background: '#000', color: '#fff', padding: '0.85rem 1.5rem', display: 'flex', alignItems: 'center', gap: '0.75rem', flexWrap: 'wrap' }}>
                <span style={{ fontFamily: 'var(--font-mono)', fontWeight: 700, fontSize: '0.82rem', flex: 1, minWidth: 0, overflow: 'hidden', textOverflow: 'ellipsis', whiteSpace: 'nowrap' }}>
                  📄 {(reportResult.topic || reportTopic).toUpperCase()}
                </span>
                {/* Confidence badge */}
                {reportResult.confidence !== undefined && (
                  <span style={{
                    background: confidenceBadgeColor(reportResult.confidence),
                    color: '#fff',
                    padding: '2px 10px',
                    fontFamily: 'var(--font-mono)',
                    fontSize: '0.72rem',
                    fontWeight: 700,
                    letterSpacing: '0.5px',
                    flexShrink: 0,
                  }}>
                    CONF: {(reportResult.confidence * 100).toFixed(0)}%
                  </span>
                )}
                {reportResult.tool_calls_made !== undefined && (
                  <span style={{ background: '#333', color: '#fff', padding: '2px 10px', fontFamily: 'var(--font-mono)', fontSize: '0.72rem', fontWeight: 700, flexShrink: 0 }}>
                    {reportResult.tool_calls_made} CALLS
                  </span>
                )}
                {/* Actions */}
                <button
                  onClick={handleCopyReport}
                  style={{ background: copyDone ? '#16a34a' : 'transparent', color: '#fff', border: '1px solid #555', padding: '3px 10px', fontFamily: 'var(--font-mono)', fontSize: '0.72rem', cursor: 'pointer', flexShrink: 0 }}
                >
                  {copyDone ? '✓ COPIED' : '📋 COPY'}
                </button>
                <button
                  onClick={handleDownloadReport}
                  style={{ background: 'transparent', color: '#fff', border: '1px solid #555', padding: '3px 10px', fontFamily: 'var(--font-mono)', fontSize: '0.72rem', cursor: 'pointer', flexShrink: 0 }}
                >
                  ⬇ .MD
                </button>
              </div>

              <div style={{ padding: '2rem' }}>
                {/* Meta info row */}
                <div style={{ display: 'flex', gap: '1.5rem', flexWrap: 'wrap', marginBottom: '2rem', paddingBottom: '1rem', borderBottom: '1px dotted #ddd', fontFamily: 'var(--font-mono)', fontSize: '0.72rem', color: '#888' }}>
                  {reportTimestamp && <div>Generated: {reportTimestamp.toLocaleString()}</div>}
                  {reportResult.tool_calls_made !== undefined && (
                    <div>Depth: {reportDepth} · Tool calls: {reportResult.tool_calls_made}</div>
                  )}
                  {reportResult.confidence !== undefined && (
                    <div style={{ color: confidenceBadgeColor(reportResult.confidence), fontWeight: 700 }}>
                      Confidence: {(reportResult.confidence * 100).toFixed(1)}%
                    </div>
                  )}
                </div>

                {/* Executive summary */}
                {reportResult.executive_summary && (
                  <div style={{ marginBottom: '2.5rem', borderLeft: '4px solid #f59e0b', paddingLeft: '1.25rem' }}>
                    <h4 className="mono-text" style={{ fontSize: '0.72rem', marginBottom: '0.75rem', color: '#d97706', letterSpacing: '1.5px' }}>
                      EXECUTIVE SUMMARY
                    </h4>
                    <p style={{ fontFamily: 'var(--font-sans)', lineHeight: 1.9, fontSize: '1rem', fontWeight: 500, whiteSpace: 'pre-wrap', color: '#111' }}>
                      {reportResult.executive_summary}
                    </p>
                  </div>
                )}

                {/* Sections */}
                {reportResult.sections && typeof reportResult.sections === 'object' && !Array.isArray(reportResult.sections) ? (
                  Object.entries(reportResult.sections).map(([title, content], i) => (
                    <div key={i} style={{ marginBottom: '2rem', paddingBottom: '1.5rem', borderBottom: '1px dotted #e5e5e5' }}>
                      <h4 className="mono-text" style={{ fontSize: '0.8rem', marginBottom: '1rem', color: '#000', display: 'flex', alignItems: 'center', gap: '0.5rem' }}>
                        <span style={{ background: '#000', color: '#fff', padding: '2px 8px', fontSize: '0.65rem', flexShrink: 0 }}>{i + 1}</span>
                        {title.toUpperCase()}
                      </h4>
                      <p style={{ fontFamily: 'var(--font-sans)', lineHeight: 1.85, fontSize: '0.92rem', whiteSpace: 'pre-wrap', color: '#333' }}>
                        {String(content)}
                      </p>
                    </div>
                  ))
                ) : (
                  reportResult.sections?.map?.((s: any, i: number) => (
                    <div key={i} style={{ marginBottom: '2rem', paddingBottom: '1.5rem', borderBottom: '1px dotted #e5e5e5' }}>
                      <h4 className="mono-text" style={{ fontSize: '0.8rem', marginBottom: '1rem', color: '#000', display: 'flex', alignItems: 'center', gap: '0.5rem' }}>
                        <span style={{ background: '#000', color: '#fff', padding: '2px 8px', fontSize: '0.65rem', flexShrink: 0 }}>{i + 1}</span>
                        {s.title?.toUpperCase()}
                      </h4>
                      <p style={{ fontFamily: 'var(--font-sans)', lineHeight: 1.85, fontSize: '0.92rem', whiteSpace: 'pre-wrap', color: '#333' }}>
                        {s.content}
                      </p>
                    </div>
                  ))
                )}

                {!reportResult.sections && (
                  <p style={{ fontFamily: 'var(--font-sans)', lineHeight: 1.85, whiteSpace: 'pre-wrap', color: '#333' }}>
                    {reportResult.report || reportResult.content || reportResult.markdown || JSON.stringify(reportResult, null, 2)}
                  </p>
                )}

                {/* Key entities */}
                {reportResult.key_entities && reportResult.key_entities.length > 0 && (
                  <div style={{ marginTop: '2rem', paddingTop: '1rem', borderTop: '1px dotted #ccc' }}>
                    <h4 className="mono-text" style={{ fontSize: '0.68rem', color: '#888', marginBottom: '0.5rem', letterSpacing: '1.5px' }}>
                      KEY ENTITIES REFERENCED
                    </h4>
                    <div style={{ display: 'flex', flexWrap: 'wrap', gap: '0.4rem' }}>
                      {reportResult.key_entities.map((e: string, i: number) => (
                        <span key={i} style={{ background: '#f3f4f6', border: '1.5px solid #e5e5e5', padding: '2px 8px', fontSize: '0.75rem', fontFamily: 'var(--font-mono)', fontWeight: 600 }}>
                          {e}
                        </span>
                      ))}
                    </div>
                  </div>
                )}
              </div>
            </div>
          )}
        </div>
      )}

      {/* ── LIVE GRAPH UPDATE TAB ── */}
      {activeTab === 'graph-update' && (
        <div>
          <div className="page-info-bar">
            <GitCommit size={14}/>
            <span><strong>LIVE GRAPH UPDATE</strong> — Paste any text (news article, note, policy). The system extracts entities and relationships and merges them into the live knowledge graph without re-running ingestion. Uses <strong>MERGE</strong> to prevent duplicates.</span>
          </div>
          <div style={{ border: '3px solid #000', padding: '2rem', marginBottom: '2rem' }}>
            <h3 className="mono-text" style={{ marginBottom: '1.5rem' }}><GitCommit size={16} style={{ display: 'inline', marginRight: '0.5rem' }}/>INJECT KNOWLEDGE INTO GRAPH</h3>
            <div style={{ display: 'flex', flexDirection: 'column', gap: '1rem' }}>
              <div>
                <label className="mono-text" style={{ fontSize: '0.8rem', fontWeight: 700, display: 'block', marginBottom: '0.5rem' }}>TEXT TO INJECT</label>
                <textarea value={updateText} onChange={e => setUpdateText(e.target.value)}
                  rows={8} placeholder="Paste any text — news, reports, notes, or raw facts..."
                  style={{ width: '100%', border: '2px solid #000', padding: '0.75rem', fontFamily: 'var(--font-mono)', fontSize: '0.88rem', resize: 'vertical', boxSizing: 'border-box' }}/>
              </div>
              <button className="app-btn mono-text" onClick={runGraphUpdate} disabled={updateLoading || !updateText.trim()}
                style={{ alignSelf: 'flex-start', padding: '0.75rem 2rem' }}>
                {updateLoading ? 'INJECTING...' : 'INJECT INTO GRAPH'}
              </button>
            </div>
          </div>
          {updateResult && (
            <div style={{ border: '3px solid #000', padding: '1.5rem', background: '#f0fdf4' }}>
              <h3 className="mono-text" style={{ marginBottom: '1rem', color: '#166534' }}>✓ GRAPH UPDATED</h3>
              <div style={{ display: 'grid', gridTemplateColumns: 'repeat(auto-fit, minmax(140px,1fr))', gap: '0.75rem', marginBottom: '1rem' }}>
                {[
                  ['Entities Added', updateResult.entities_added ?? updateResult.nodes_created],
                  ['Relationships Added', updateResult.relationships_added ?? updateResult.edges_created],
                  ['Merged Existing', updateResult.entities_merged ?? '—'],
                ].map(([label, val]) => (
                  <div key={label as string} style={{ border: '2px solid #000', padding: '1rem', textAlign: 'center' }}>
                    <div className="mono-text" style={{ fontSize: '0.68rem', color: '#888', marginBottom: '0.35rem' }}>{label}</div>
                    <div className="mono-text" style={{ fontSize: '1.6rem', fontWeight: 900 }}>{val ?? '—'}</div>
                  </div>
                ))}
              </div>
              {updateResult.message && (
                <p className="mono-text" style={{ fontSize: '0.82rem', color: '#555' }}>{updateResult.message}</p>
              )}
            </div>
          )}
        </div>
      )}

      {/* ── ENTITY CHAT TAB ── */}
      {activeTab === 'entity-chat' && (
        <div>
          <div className="page-info-bar">
            <MessageSquare size={14}/>
            <span><strong>ENTITY CHAT</strong> — Select an entity from the graph and interview it. The agent synthesizes answers from its neighborhood context, relationships, and LLM-generated profile.</span>
          </div>
          <div style={{ display: 'grid', gridTemplateColumns: '1fr 2fr', gap: '1.5rem' }}>
            {/* Config panel */}
            <div style={{ border: '3px solid #000', padding: '1.5rem' }}>
              <h3 className="mono-text" style={{ marginBottom: '1.25rem', fontSize: '0.9rem' }}>SELECT ENTITY</h3>
              <div style={{ marginBottom: '1rem' }}>
                <label className="mono-text" style={{ fontSize: '0.75rem', fontWeight: 700, color: '#888', display: 'block', marginBottom: '0.4rem' }}>ENTITY NAME</label>
                <select value={selectedEntity} onChange={e => { setSelectedEntity(e.target.value); setChatHistory([]); }}
                  style={{ width: '100%', border: '2px solid #000', padding: '0.5rem', fontFamily: 'var(--font-mono)', fontSize: '0.85rem' }}>
                  <option value="">— Select entity —</option>
                  {entities.map((n: any) => (
                    <option key={n.id} value={n.properties?.name || n.properties?.label || n.id}>
                      {n.labels?.[0]} · {n.properties?.name || n.properties?.label || n.id}
                    </option>
                  ))}
                </select>
              </div>
              <div style={{ marginBottom: '1rem' }}>
                <label className="mono-text" style={{ fontSize: '0.75rem', fontWeight: 700, color: '#888', display: 'block', marginBottom: '0.4rem' }}>EXTRA CONTEXT (OPTIONAL)</label>
                <textarea value={entityContext} onChange={e => setEntityContext(e.target.value)}
                  rows={3} placeholder="Add any additional context or constraints..."
                  style={{ width: '100%', border: '2px solid #000', padding: '0.5rem', fontFamily: 'var(--font-mono)', fontSize: '0.82rem', resize: 'vertical' }}/>
              </div>
              <button className="app-btn mono-text" style={{ width: '100%', justifyContent: 'center' }}
                onClick={() => setChatHistory([])}>CLEAR CHAT</button>
            </div>
            {/* Chat window */}
            <div style={{ border: '3px solid #000', display: 'flex', flexDirection: 'column', minHeight: 400 }}>
              <div style={{ background: '#000', color: '#fff', padding: '0.5rem 1rem', fontFamily: 'var(--font-mono)', fontSize: '0.78rem', fontWeight: 700 }}>
                {selectedEntity ? `CHATTING WITH: ${selectedEntity.toUpperCase()}` : 'SELECT AN ENTITY TO BEGIN'}
              </div>
              <div style={{ flex: 1, overflowY: 'auto', padding: '1rem', display: 'flex', flexDirection: 'column', gap: '0.75rem', minHeight: 280, maxHeight: 400 }}>
                {chatHistory.length === 0 && (
                  <div style={{ textAlign: 'center', padding: '2rem', color: '#ccc', fontFamily: 'var(--font-mono)', fontSize: '0.82rem' }}>
                    {selectedEntity ? 'Ask this entity anything…' : 'Select an entity to start chatting'}
                  </div>
                )}
                {chatHistory.map((m, i) => (
                  <div key={i} style={{
                    alignSelf: m.role === 'user' ? 'flex-end' : 'flex-start',
                    background: m.role === 'user' ? '#000' : '#f3f4f6',
                    color: m.role === 'user' ? '#fff' : '#000',
                    padding: '0.6rem 1rem',
                    maxWidth: '85%',
                    fontFamily: 'var(--font-sans)', fontSize: '0.88rem', lineHeight: 1.6
                  }}>
                    {m.content}
                  </div>
                ))}
                {chatLoading && (
                  <div style={{ alignSelf: 'flex-start', background: '#f3f4f6', padding: '0.6rem 1rem', fontFamily: 'var(--font-mono)', fontSize: '0.82rem', color: '#888' }}>
                    Thinking…
                  </div>
                )}
              </div>
              <div style={{ borderTop: '2px solid #000', display: 'flex', gap: 0 }}>
                <input type="text" value={chatMsg} onChange={e => setChatMsg(e.target.value)}
                  onKeyDown={e => e.key === 'Enter' && !e.shiftKey && sendEntityChat()}
                  placeholder={selectedEntity ? 'Ask a question…' : 'Select an entity first'}
                  disabled={!selectedEntity || chatLoading}
                  style={{ flex: 1, border: 'none', padding: '0.75rem 1rem', fontFamily: 'var(--font-mono)', fontSize: '0.88rem', outline: 'none' }}/>
                <button className="app-btn" onClick={sendEntityChat} disabled={!selectedEntity || !chatMsg.trim() || chatLoading}
                  style={{ border: 'none', borderLeft: '2px solid #000', borderRadius: 0, minWidth: 80 }}>
                  SEND
                </button>
              </div>
            </div>
          </div>
        </div>
      )}
    </div>
  );
};

export default InsightsView;