1
File size: 47,088 Bytes
67fbe47
301564d
 
 
 
67fbe47
 
 
6e19931
301564d
 
 
2b66a2f
3200594
2b66a2f
 
 
301564d
6e19931
bc577f6
 
 
 
 
 
440fb65
 
 
301564d
440fb65
 
 
 
4d2b105
 
 
 
67fbe47
440fb65
 
301564d
67fbe47
 
301564d
 
 
67fbe47
301564d
67fbe47
301564d
 
67fbe47
 
301564d
 
2b66a2f
301564d
 
440fb65
301564d
440fb65
 
 
301564d
440fb65
301564d
440fb65
301564d
d64c134
 
d0e19af
67fbe47
 
 
 
 
301564d
d64c134
2b66a2f
301564d
bc577f6
 
301564d
 
bc577f6
 
 
301564d
bc577f6
301564d
67fbe47
301564d
bc577f6
 
440fb65
301564d
440fb65
301564d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
440fb65
301564d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67fbe47
301564d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bc577f6
440fb65
301564d
440fb65
301564d
 
 
 
 
 
 
 
 
 
 
 
440fb65
301564d
 
 
 
 
 
 
 
 
 
 
 
 
 
440fb65
301564d
 
 
 
 
 
 
 
 
440fb65
 
301564d
440fb65
301564d
 
440fb65
 
301564d
 
440fb65
301564d
 
 
 
 
440fb65
 
301564d
 
 
 
 
 
 
 
 
 
 
 
 
 
bc577f6
440fb65
301564d
 
bc577f6
301564d
440fb65
 
301564d
440fb65
301564d
440fb65
 
 
301564d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
440fb65
 
 
301564d
 
440fb65
301564d
 
 
 
 
 
440fb65
 
 
301564d
 
 
 
 
 
 
440fb65
 
301564d
440fb65
 
 
301564d
 
 
440fb65
 
301564d
440fb65
301564d
 
 
 
 
 
 
 
67fbe47
301564d
 
67fbe47
301564d
 
 
 
 
 
440fb65
301564d
bc577f6
440fb65
301564d
 
 
 
440fb65
 
301564d
440fb65
301564d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67fbe47
301564d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bc577f6
301564d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
bc577f6
 
301564d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
440fb65
301564d
440fb65
301564d
 
 
 
 
 
 
 
 
 
 
 
440fb65
bc577f6
301564d
 
 
 
 
 
 
67fbe47
301564d
440fb65
301564d
440fb65
301564d
 
 
67fbe47
301564d
67fbe47
301564d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
440fb65
 
301564d
 
 
 
 
 
 
440fb65
301564d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
440fb65
 
67fbe47
301564d
67fbe47
440fb65
301564d
 
 
 
 
 
 
 
440fb65
67fbe47
301564d
d64c134
301564d
67fbe47
d64c134
67fbe47
d64c134
301564d
d64c134
301564d
 
 
 
 
 
67fbe47
301564d
67fbe47
301564d
 
 
 
 
 
 
d64c134
301564d
4d2b105
440fb65
301564d
440fb65
301564d
 
 
 
 
 
 
 
 
440fb65
67fbe47
bc577f6
301564d
4d2b105
301564d
 
 
bc577f6
301564d
67fbe47
301564d
67fbe47
301564d
67fbe47
301564d
440fb65
 
301564d
440fb65
 
301564d
440fb65
67fbe47
301564d
26799ac
301564d
 
 
67fbe47
301564d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
440fb65
301564d
 
 
 
 
 
 
 
 
 
 
 
 
9ee53de
67fbe47
301564d
 
 
 
 
 
 
 
 
9ee53de
301564d
67fbe47
301564d
67fbe47
301564d
 
 
 
 
 
 
 
 
 
 
67fbe47
 
 
301564d
9ee53de
67fbe47
301564d
 
 
 
 
9ee53de
67fbe47
301564d
 
 
 
67fbe47
440fb65
301564d
 
 
 
 
 
 
 
440fb65
67fbe47
301564d
 
 
 
 
 
 
 
67fbe47
301564d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
2b66a2f
3200594
440fb65
 
 
301564d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67fbe47
 
301564d
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
67fbe47
301564d
 
440fb65
301564d
440fb65
301564d
67fbe47
301564d
 
67fbe47
 
d64c134
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
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
"""
DEEP ANALYSIS ENGINE v4 β€” HuggingFace Spaces
Academic sources: Semantic Scholar Β· OpenAlex Β· arXiv Β· Consensus
Web sources: DuckDuckGo multi-query Β· real page fetching
4 parallel claim workers Β· progressive display Β· PDF export
Requires: GROQ_API_KEY as HF Secret
"""

import gradio as gr
import os, time, json, re, threading, urllib.parse, urllib.request
import xml.etree.ElementTree as ET
from datetime import datetime
import pypdf
from groq import Groq
from docx import Document
from io import BytesIO
import base64
from concurrent.futures import ThreadPoolExecutor, as_completed, TimeoutError as FutTimeout

try:
    from duckduckgo_search import DDGS
    DDG_OK = True
except ImportError:
    DDG_OK = False

try:
    import requests
    from bs4 import BeautifulSoup
    import urllib3; urllib3.disable_warnings()
    FETCH_OK = True
except ImportError:
    FETCH_OK = False

try:
    client = Groq(api_key=os.environ.get("GROQ_API_KEY"))
except Exception as e:
    client = None

DEEP = "llama-3.3-70b-versatile"
FAST = "llama-3.1-8b-instant"
YEAR = datetime.now().year

DEPTHS = {
    "3  claims": 3, "5  claims": 5,  "8  claims": 8,
    "10 claims":10, "15 claims":15,  "20 claims":20,
    "25 claims":25, "30 claims":30,
}
LANGS = ["Nederlands","English","Deutsch","FranΓ§ais","EspaΓ±ol","δΈ­ζ–‡"]
VERDICT_COLOR = {
    "CONFIRMED":"#00f5c3","REFUTED":"#ff4d6d",
    "PARTIAL":"#ffd166","UNCERTAIN":"#7b8fa6",
}
VERDICT_LABEL = {
    "CONFIRMED":"βœ… BEVESTIGD","REFUTED":"❌ WEERLEGD",
    "PARTIAL":"⚠️ GEDEELTELIJK","UNCERTAIN":"❓ ONZEKER",
}
BROWSER_HDR = {
    "User-Agent": "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 Chrome/124.0 Safari/537.36",
    "Accept-Language": "en-US,en;q=0.9,zh;q=0.8",
    "Accept": "text/html,application/xhtml+xml,*/*;q=0.8",
}

# ─────────────────────────────────────────────────────────────────────────────
# Utils
# ─────────────────────────────────────────────────────────────────────────────
def esc(s): return str(s).replace("&","&amp;").replace("<","&lt;").replace(">","&gt;")

def read_docs(files):
    parts = []
    for f in (files or []):
        try:
            nm = f.name
            if nm.endswith(".pdf"):
                parts.extend(p.extract_text() or "" for p in pypdf.PdfReader(nm).pages)
            elif nm.endswith(".docx"):
                parts.extend(p.text for p in Document(nm).paragraphs if p.text.strip())
        except Exception as e: print(f"[!] {f.name}: {e}")
    return " ".join(parts)

def llm(system, user, model=DEEP, max_tokens=1600):
    r = client.chat.completions.create(
        model=model,
        messages=[{"role":"system","content":system},{"role":"user","content":user}],
        max_tokens=max_tokens, temperature=0.1,
    )
    return r.choices[0].message.content.strip()

def safe_json(raw):
    raw = raw.strip()
    for fence in ["```json","```"]:
        if fence in raw:
            raw = raw.split(fence,1)[1].rsplit("```",1)[0]; break
    return json.loads(raw.strip())

# ─────────────────────────────────────────────────────────────────────────────
# Academic Source APIs
# ─────────────────────────────────────────────────────────────────────────────
def semantic_scholar(query, n=8):
    """Semantic Scholar free API β€” papers + abstracts + citation counts."""
    try:
        resp = requests.get(
            "https://api.semanticscholar.org/graph/v1/paper/search",
            params={"query":query,
                    "fields":"title,abstract,year,authors,citationCount,url,externalIds",
                    "limit":n},
            timeout=15, headers={"User-Agent":"DeepFactChecker/4.0"}
        )
        out = []
        for p in resp.json().get("data",[]):
            out.append({
                "source":"Semantic Scholar",
                "title": p.get("title",""),
                "abstract": (p.get("abstract") or "")[:500],
                "year":  p.get("year",""),
                "citations": p.get("citationCount",0),
                "url":   p.get("url",""),
            })
        return out
    except Exception as e:
        print(f"[!] SemanticScholar: {e}"); return []

def openalex(query, n=6):
    """OpenAlex β€” open academic graph (successor to Microsoft Academic / Research Rabbit data layer)."""
    try:
        resp = requests.get(
            "https://api.openalex.org/works",
            params={"search":query, "per_page":n,
                    "select":"title,abstract_inverted_index,doi,publication_year,cited_by_count"},
            timeout=15,
            headers={"User-Agent":"DeepFactChecker/4.0 (mailto:research@hf.co)"}
        )
        out = []
        for w in resp.json().get("results",[]):
            inv = w.get("abstract_inverted_index") or {}
            if inv:
                pos = {}
                for word, plist in inv.items():
                    for p in plist: pos[p] = word
                abstract = " ".join(pos[k] for k in sorted(pos))[:500]
            else:
                abstract = ""
            doi = (w.get("doi") or "").replace("https://doi.org/","")
            url = f"https://doi.org/{doi}" if doi else ""
            out.append({
                "source":"OpenAlex",
                "title": w.get("title",""),
                "abstract": abstract,
                "year": w.get("publication_year",""),
                "citations": w.get("cited_by_count",0),
                "url": url,
            })
        return out
    except Exception as e:
        print(f"[!] OpenAlex: {e}"); return []

def arxiv(query, n=5):
    """arXiv preprints β€” especially good for recent tech/space/AI topics."""
    try:
        enc = urllib.parse.quote(query)
        resp = requests.get(
            f"https://export.arxiv.org/api/query?search_query=all:{enc}"
            f"&max_results={n}&sortBy=relevance",
            timeout=15
        )
        ns = {"a":"http://www.w3.org/2005/Atom"}
        root = ET.fromstring(resp.text)
        out = []
        for e in root.findall("a:entry",ns):
            t = e.find("a:title",ns); s = e.find("a:summary",ns)
            link = e.find("a:id",ns);  pub = e.find("a:published",ns)
            out.append({
                "source":"arXiv",
                "title":    t.text.strip() if t is not None else "",
                "abstract": s.text.strip()[:500] if s is not None else "",
                "year":     pub.text[:4] if pub is not None else "",
                "citations": 0,
                "url":      link.text.strip() if link is not None else "",
            })
        return out
    except Exception as e:
        print(f"[!] arXiv: {e}"); return []

def consensus_fetch(query, timeout=12):
    """Fetch Consensus.app search results page (best-effort, JS-light pages only)."""
    try:
        url = f"https://consensus.app/search/?q={urllib.parse.quote(query)}"
        resp = requests.get(url, headers=BROWSER_HDR, timeout=timeout, verify=False)
        soup = BeautifulSoup(resp.text, "html.parser")
        for t in soup(["script","style","nav","footer","header"]): t.decompose()
        lines = [l.strip() for l in soup.get_text("\n",strip=True).splitlines()
                 if len(l.strip())>40]
        return "\n".join(lines[:40])
    except Exception as e:
        print(f"[!] Consensus: {e}"); return ""

def parallel_academic(query):
    """Run all academic sources simultaneously."""
    results = {"ss":[], "oa":[], "ax":[], "cs":""}
    with ThreadPoolExecutor(max_workers=4) as ex:
        fs = {
            ex.submit(semantic_scholar, query, 8): "ss",
            ex.submit(openalex,         query, 6): "oa",
            ex.submit(arxiv,            query, 5): "ax",
            ex.submit(consensus_fetch,  query):    "cs",
        }
        for fut in as_completed(fs, timeout=25):
            key = fs[fut]
            try: results[key] = fut.result()
            except Exception: pass
    return results

def fmt_academic(acad):
    """Format academic results into evidence text."""
    parts = []
    all_papers = acad["ss"] + acad["oa"] + acad["ax"]
    if all_papers:
        parts.append("=== ACADEMIC PAPERS ===")
        for p in all_papers[:15]:
            src  = p.get("source","")
            yr   = p.get("year","")
            cit  = p.get("citations",0)
            url  = p.get("url","")
            title= p.get("title","")
            ab   = p.get("abstract","")
            parts.append(
                f"[{src} {yr} | {cit} citations]\n"
                f"Title: {title}\nURL: {url}\nAbstract: {ab}\n"
            )
    if acad["cs"]:
        parts.append(f"=== CONSENSUS.APP ===\n{acad['cs'][:1500]}")
    return "\n\n".join(parts) if parts else "No academic sources found."

# ─────────────────────────────────────────────────────────────────────────────
# Web search layer
# ─────────────────────────────────────────────────────────────────────────────
def _ddg(query, region, max_r):
    with DDGS() as d: return list(d.text(query, region=region, max_results=max_r))

def ddg_one(query, region="wt-wt", max_r=8, timeout=16):
    if not DDG_OK or not query.strip(): return []
    for attempt in range(2):
        try:
            with ThreadPoolExecutor(1) as ex:
                return ex.submit(_ddg,query,region,max_r).result(timeout=timeout)
        except FutTimeout: return []
        except Exception: time.sleep(2*(attempt+1))
    return []

def parallel_ddg(qr_pairs, max_r=7):
    hits, seen = [], set()
    with ThreadPoolExecutor(max_workers=min(len(qr_pairs),8)) as ex:
        futures = {ex.submit(ddg_one,q,r,max_r):(q,r) for q,r in qr_pairs}
        for fut in as_completed(futures, timeout=35):
            try:
                for h in fut.result():
                    url = h.get("href","")
                    if url and url not in seen: seen.add(url); hits.append(h)
            except Exception: pass
    return hits

def fetch_page(url, timeout=14, max_chars=3500):
    if not FETCH_OK or not url: return ""
    try:
        r = requests.get(url, headers=BROWSER_HDR, timeout=timeout,
                         allow_redirects=True, verify=False)
        r.raise_for_status()
        if "html" not in r.headers.get("Content-Type",""): return ""
        soup = BeautifulSoup(r.text, "html.parser")
        for t in soup(["script","style","nav","footer","header","aside",
                        "form","noscript","iframe","img","svg"]): t.decompose()
        lines = [l.strip() for l in soup.get_text("\n",strip=True).splitlines()
                 if len(l.strip())>35]
        return "\n".join(lines)[:max_chars]
    except Exception as e:
        print(f"[!] fetch {url[:55]}: {e}"); return ""

def parallel_fetch(hits, max_pages=7):
    if not FETCH_OK: return ""
    results = []
    with ThreadPoolExecutor(max_workers=max_pages) as ex:
        fmap = {ex.submit(fetch_page,h.get("href","")):[h] for h in hits[:max_pages] if h.get("href","")}
        for fut,hl in fmap.items():
            try:
                txt = fut.result(timeout=18)
                if txt and len(txt)>60:
                    h = hl[0]
                    results.append(f"[{h.get('title','')}]\n{h.get('href','')}\n{txt}")
            except Exception: pass
    return "\n\n---\n\n".join(results)

def zh_translate(text):
    try: return llm("Translate to Simplified Chinese. Output only.", text, model=FAST, max_tokens=200)
    except Exception: return text

def build_queries(claim, context):
    raw = llm(
        "Search query generator. Return ONLY a JSON array of strings.",
        f'Generate 6 varied English search queries to verify this claim:\n'
        f'Claim: "{claim}"\nContext: {context}\nYear: {YEAR}\n\n'
        f'Angles: (1) exact claim + year, (2) official/gov source, '
        f'(3) named entities + numbers, (4) independent verification, '
        f'(5) counter-evidence, (6) Chinese sources\n'
        f'Return ONLY: ["q1","q2","q3","q4","q5","q6"]',
        model=FAST, max_tokens=350,
    )
    try:
        qs = safe_json(raw)
        return [q for q in qs if isinstance(q,str) and len(q)>4][:6]
    except Exception:
        return [claim[:120], f"{claim[:80]} {YEAR}", f"{claim[:80]} official source"]

# ─────────────────────────────────────────────────────────────────────────────
# Core verification
# ─────────────────────────────────────────────────────────────────────────────
def gather_and_verify(obj, lang):
    claim   = obj.get("claim","")
    context = obj.get("context","")

    # Run academic + web search in parallel
    with ThreadPoolExecutor(max_workers=2) as ex:
        acad_fut = ex.submit(parallel_academic, claim)

        # Build web queries while academic runs
        en_queries = build_queries(claim, context)
        zh_q = zh_translate(claim)
        qr_pairs = ([(q,"wt-wt") for q in en_queries]
                    + [(zh_q,"cn-zh"),(zh_q,"wt-wt"),
                       (f'"{claim[:85]}"',"wt-wt"),
                       (f'{claim[:70]} site:gov.cn OR site:xinhuanet.com OR site:reuters.com OR site:spacenews.com',"wt-wt"),
                       (f'{claim[:70]} {YEAR}',"wt-wt")])
        web_fut = ex.submit(parallel_ddg, qr_pairs, 7)

        acad = acad_fut.result(timeout=30)
        hits = web_fut.result(timeout=35)

    # Prioritise authoritative domains
    def score(h):
        url = h.get("href","").lower()
        for d in ["gov.cn","xinhua","reuters","bbc","ap.org","spacenews","nature.com",
                  "science","un.org","nasa.gov","esa.int","iau.org"]:
            if d in url: return 0
        return 1
    hits.sort(key=score)

    pages = parallel_fetch(hits, max_pages=7)
    snippets = "\n".join(
        f"β€’ [{h.get('title','')}] {h.get('href','')} β€” {h.get('body','')[:220]}"
        for h in hits[:16]
    ) or "Geen web resultaten."

    academic_text = fmt_academic(acad)

    evidence = (
        f"=== ACADEMIC SOURCES (Semantic Scholar + OpenAlex + arXiv + Consensus) ===\n"
        f"{academic_text[:5000]}\n\n"
        f"=== WEB QUERIES USED ===\n{chr(10).join(en_queries)}\n\n"
        f"=== WEB SNIPPETS ({len(hits)} results) ===\n{snippets}\n\n"
        f"=== FULL PAGE CONTENT ===\n{pages[:8000]}"
    )

    prompt = (
        f'Expert international fact-checker with academic + web evidence.\n'
        f'Verify claim thoroughly. Extract specific facts from ALL sources below.\n\n'
        f'Claim: "{claim}"\nContext: {context}\n\n'
        f'{evidence}\n\n'
        f'RULES:\n'
        f'- Academic papers: cite title, year, citation count\n'
        f'- Web pages: quote specific numbers/dates/names found\n'
        f'- UNCERTAIN only if truly zero relevant evidence across all sources\n'
        f'- Note if academic consensus differs from official claims\n\n'
        f'Return ONLY valid JSON:\n'
        f'{{"verdict":"CONFIRMED|REFUTED|PARTIAL|UNCERTAIN",'
        f'"confidence":0-100,'
        f'"analysis":"4-6 sentences in {lang} with specific cited facts",'
        f'"supporting":"direct evidence from sources confirming the claim",'
        f'"opposing":"evidence contradicting or nuancing the claim",'
        f'"academic_evidence":"relevant papers found (title, year, key finding)",'
        f'"sources_used":"URLs of most useful pages (comma-separated)",'
        f'"chinese_angle":"what Chinese-language sources say, or N/A",'
        f'"red_flags":"inconsistencies, missing data, propaganda signals, or none"}}'
    )

    raw = llm(f"Expert fact-checker. Return ONLY valid JSON. Text in {lang}.", prompt, max_tokens=1400)
    try:
        return safe_json(raw)
    except Exception:
        return {"verdict":"UNCERTAIN","confidence":30,"analysis":raw[:400],
                "supporting":"","opposing":"","academic_evidence":"",
                "sources_used":"","chinese_angle":"","red_flags":""}

# ─────────────────────────────────────────────────────────────────────────────
# Claim extraction + summary
# ─────────────────────────────────────────────────────────────────────────────
def extract_claims(txt, lang, n):
    raw = llm(
        f"Expert fact-checker. Return ONLY valid JSON array. Claims in {lang}.",
        f"Extract {n} most important verifiable factual claims.\n"
        f"Focus on: numbers, statistics, names, dates, plans, timelines, technical specs.\n"
        f'Return ONLY: [{{"claim":"exact statement","context":"1-sentence context",'
        f'"importance":"high|medium|low"}},...]\n\nDocument:\n{txt[:8000]}',
        max_tokens=min(400+n*140,4000),
    )
    try: return safe_json(raw)
    except Exception: return [{"claim":txt[:200],"context":"","importance":"high"}]

def do_summary(txt, lang):
    yield mk_status("SCANNING DOCUMENT...")
    raw = llm(
        f"Expert analyst. Write in {lang}.",
        f"Structured summary:\n1. Core subject & purpose\n2. Key findings/arguments\n"
        f"3. Important data, names, dates, statistics\n4. Conclusions\n5. Gaps or bias\n\n"
        f"Document:\n{txt[:9000]}",
        max_tokens=2200,
    )
    body = esc(raw).replace("\n","<br>")
    yield (HOLO_HEADER
           + f"<div class='holo-card'><div class='card-label'>DOCUMENT SUMMARY</div>"
             f"<div style='color:#c8d8e8;line-height:1.9;font-size:13px'>{body}</div></div>"
           + mk_pdf_btn(raw))

# ─────────────────────────────────────────────────────────────────────────────
# Parallel fact-check engine
# ─────────────────────────────────────────────────────────────────────────────
def do_factcheck(txt, lang, n_claims):
    yield mk_status(f"EXTRACTING {n_claims} VERIFIABLE CLAIMS...")
    claims = extract_claims(txt, lang, n_claims)
    total  = len(claims)

    results  = [None]  * total
    statuses = ["QUEUE"] * total
    lock     = threading.Lock()

    def worker(idx, obj):
        with lock: statuses[idx] = "SCAN"
        try:
            res = gather_and_verify(obj, lang)
        except Exception as e:
            print(f"[!] claim {idx}: {e}")
            res = {"verdict":"UNCERTAIN","confidence":0,"analysis":str(e)[:200],
                   "supporting":"","opposing":"","academic_evidence":"",
                   "sources_used":"","chinese_angle":"","red_flags":""}
        with lock:
            results[idx]  = res
            statuses[idx] = res.get("verdict","UNCERTAIN")
        return idx

    with ThreadPoolExecutor(max_workers=4) as pool:
        futures = {pool.submit(worker,i,obj):i for i,obj in enumerate(claims)}
        done_set   = set()
        last_yield = time.time()
        while len(done_set) < total:
            time.sleep(0.35)
            changed = False
            for fut in list(futures):
                idx = futures[fut]
                if idx not in done_set and fut.done():
                    try: fut.result()
                    except Exception: pass
                    done_set.add(idx); changed = True
            if changed or time.time()-last_yield > 1.8:
                last_yield = time.time()
                yield _render(claims,results,statuses,total,done_set)

    yield _render(claims,results,statuses,total,set(range(total))) + mk_status("SYNTHESISING REPORT...")

    try:
        synth = llm(
            f"Expert analyst. Write in {lang}.",
            "Final credibility report (350 words):\n"
            + "\n".join(f"#{i+1} [{r.get('verdict','?')} {r.get('confidence',0)}/100] "
                        f"{c.get('claim','')}"
                        for i,(c,r) in enumerate(zip(claims,results)) if r)
            + f"\n\nDocument excerpt:\n{txt[:1000]}\n\n"
              "Include: reliability score 0-100, main red flags, academic vs official discrepancies, recommendation.",
            max_tokens=600,
        )
    except Exception as e:
        synth = f"Synthesis error: {e}"

    plain = _plain(claims,results,synth)
    yield (_render(claims,results,statuses,total,set(range(total)))
           + mk_synthesis(synth) + mk_pdf_btn(plain))

def _render(claims, results, statuses, total, done_set):
    done = len(done_set)
    pct  = int(done/max(total,1)*100)

    # Build worker grid
    worker_pills = ""
    for i,st in enumerate(statuses):
        if st=="QUEUE":   col,bdr = "#1a2f3f","#1a2f3f"
        elif st=="SCAN":  col,bdr = "#001a26","#00d4ff"
        elif st=="CONFIRMED": col,bdr = "#001a1a","#00f5c3"
        elif st=="REFUTED":   col,bdr = "#1a0010","#ff4d6d"
        elif st=="PARTIAL":   col,bdr = "#1a1500","#ffd166"
        else:                  col,bdr = "#101520","#7b8fa6"
        icon = {"QUEUE":"β—‹","SCAN":"β—ˆ","CONFIRMED":"β—‰","REFUTED":"β—‰",
                "PARTIAL":"β—ˆ","UNCERTAIN":"β—Œ"}.get(st,"β—‹")
        tc = "#7b8fa6" if st=="QUEUE" else ("#00d4ff" if st=="SCAN" else VERDICT_COLOR.get(st,"#7b8fa6"))
        worker_pills += (
            f"<span style='display:inline-flex;align-items:center;gap:4px;padding:3px 8px;"
            f"background:{col};border:1px solid {bdr};border-radius:4px;margin:2px;"
            f"font-family:monospace;font-size:10px;color:{tc}'>"
            f"{icon} {i+1:02d}</span>"
        )

    html = (
        HOLO_HEADER
        + f"<div style='margin-bottom:14px'>"
          f"<div style='display:flex;justify-content:space-between;align-items:center;"
          f"margin-bottom:6px'>"
          f"<span style='font-family:monospace;font-size:10px;color:#3a5a6a;"
          f"letter-spacing:.1em'>β–Έ ANALYSIS PROGRESS</span>"
          f"<span style='font-family:monospace;font-size:10px;color:#00d4ff'>"
          f"{done}/{total} Β· {pct}%</span></div>"
          f"<div style='height:2px;background:#0a1520;border-radius:1px;position:relative'>"
          f"<div style='height:100%;width:{pct}%;background:linear-gradient(90deg,#0066aa,#00d4ff);"
          f"border-radius:1px;transition:width .6s ease'></div></div>"
          f"<div style='margin-top:8px;line-height:2'>{worker_pills}</div>"
          f"</div>"
    )

    # Scoreboard
    done_results = [results[i] for i in done_set if results[i]]
    if done_results:
        html += _scoreboard(done_results)

    # Source badges
    html += (
        "<div style='display:flex;gap:6px;flex-wrap:wrap;margin:10px 0 14px'>"
        + "".join(f"<span style='font-family:monospace;font-size:9px;padding:2px 8px;"
                  f"background:#000d1a;border:1px solid #0a3050;border-radius:3px;"
                  f"color:#3a7a9a;letter-spacing:.06em'>{src}</span>"
                  for src in ["SEMANTIC SCHOLAR","OPENALEX","ARXIV","CONSENSUS",
                               "DUCKDUCKGO EN","DUCKDUCKGO ZH","PAGE FETCH"])
        + "</div>"
    )

    # Claim cards
    order = sorted(range(total), key=lambda i:(0 if i in done_set else 1,i))
    for i in order:
        claim = claims[i].get("claim","")
        if i in done_set and results[i]:
            html += mk_card(i+1,claim,results[i],total)
        else:
            st = statuses[i]
            anim = "animation:scan-pulse 1.2s ease-in-out infinite" if st=="SCAN" else ""
            bdr  = "#00d4ff44" if st=="SCAN" else "#0a1a24"
            lbl  = "SCANNING..." if st=="SCAN" else "QUEUED"
            html += (
                f"<div style='border:1px solid {bdr};border-radius:10px;"
                f"padding:14px 18px;margin:7px 0;background:#050d15;{anim}'>"
                f"<div style='display:flex;justify-content:space-between;margin-bottom:6px'>"
                f"<span style='font-family:monospace;font-size:10px;color:#1a3a4a;letter-spacing:.1em'>"
                f"CLAIM {i+1:02d}/{total:02d}</span>"
                f"<span style='font-family:monospace;font-size:10px;color:#00d4ff44'>{lbl}</span>"
                f"</div>"
                f"<span style='color:#1a3a4a;font-size:12px;font-style:italic'>"
                f'"{esc(claim[:110])}…"</span></div>'
            )
    return html

def _scoreboard(results):
    counts = {"CONFIRMED":0,"REFUTED":0,"PARTIAL":0,"UNCERTAIN":0}
    for r in results: v=r.get("verdict","UNCERTAIN"); counts[v]=counts.get(v,0)+1
    avg = int(sum(r.get("confidence",50) for r in results)/max(len(results),1))
    def cell(lbl,n,col):
        return (f"<div style='text-align:center;padding:10px 6px;background:#040c14;"
                f"border:1px solid {col}25;border-radius:7px'>"
                f"<div style='color:{col};font-size:20px;font-weight:700;font-family:monospace'>{n}</div>"
                f"<div style='color:#2a4a5a;font-size:9px;font-family:monospace;margin-top:2px;"
                f"letter-spacing:.08em'>{lbl}</div></div>")
    return ("<div style='display:grid;grid-template-columns:repeat(5,1fr);gap:6px;margin:10px 0 14px'>"
            +cell("CONFIRMED",counts["CONFIRMED"],"#00f5c3")
            +cell("REFUTED",counts["REFUTED"],"#ff4d6d")
            +cell("PARTIAL",counts["PARTIAL"],"#ffd166")
            +cell("UNCERTAIN",counts["UNCERTAIN"],"#7b8fa6")
            +cell("AVG SCORE",avg,"#00d4ff")
            +"</div>")

def _plain(claims,results,synth):
    parts = [f"DEEP FACT-CHECK RAPPORT\n{'═'*52}\n\n"]
    for i,(c,r) in enumerate(zip(claims,results),1):
        if not r: continue
        parts.append(
            f"Claim #{i} [{c.get('importance','')}]: {c.get('claim','')}\n"
            f"Verdict   : {r.get('verdict','?')} ({r.get('confidence',0)}/100)\n"
            f"Analyse   : {r.get('analysis','')}\n"
            f"Support   : {r.get('supporting','')}\n"
            f"Tegenstrd : {r.get('opposing','')}\n"
            f"Acad.     : {r.get('academic_evidence','')}\n"
            f"Bronnen   : {r.get('sources_used','')}\n"
            f"China     : {r.get('chinese_angle','')}\n"
            f"Red flags : {r.get('red_flags','')}\n\n"
        )
    parts.append(f"EINDCONCLUSIE:\n{synth}\n")
    return "".join(parts)

# ─────────────────────────────────────────────────────────────────────────────
# UI components
# ─────────────────────────────────────────────────────────────────────────────
HOLO_HEADER = (
    "<div style='display:flex;align-items:center;gap:10px;padding:10px 16px;"
    "background:linear-gradient(90deg,#020c18,#020e1c);border:1px solid #0a2535;"
    "border-radius:8px;margin-bottom:10px;position:relative;overflow:hidden'>"
    "<div style='position:absolute;top:0;left:0;right:0;bottom:0;"
    "background:repeating-linear-gradient(0deg,transparent,transparent 3px,"
    "#00d4ff04 3px,#00d4ff04 4px);pointer-events:none'></div>"
    "<div style='width:6px;height:6px;border-radius:50%;background:#00f5c3;"
    "box-shadow:0 0 8px #00f5c3,0 0 16px #00f5c380;flex-shrink:0'></div>"
    f"<span style='color:#1a4a5a;font-size:10px;font-family:monospace;letter-spacing:.12em;z-index:1'>"
    f"DEEP ANALYSIS ENGINE v4 Β· SEMANTIC SCHOLAR Β· OPENALEX Β· ARXIV Β· CONSENSUS Β· {YEAR}</span>"
    "</div>"
)

def mk_status(msg):
    return (HOLO_HEADER
            + f"<div style='display:flex;align-items:center;gap:8px;padding:10px 0'>"
              f"<div style='width:5px;height:5px;border-radius:50%;background:#00d4ff;"
              f"box-shadow:0 0 6px #00d4ff'></div>"
              f"<span style='color:#00d4ff;font-size:12px;font-family:monospace;"
              f"letter-spacing:.06em'>{msg}</span></div>")

def mk_card(i, claim, result, total):
    v    = result.get("verdict","UNCERTAIN")
    col  = VERDICT_COLOR.get(v,"#7b8fa6")
    lbl  = VERDICT_LABEL.get(v,"❓ ONZEKER")
    conf = max(0,min(100,result.get("confidence",50)))
    acad = result.get("academic_evidence","")
    srcs = result.get("sources_used","")

    def row(icon, label, val, c="#7b8fa6"):
        val = str(val).strip()
        if not val or val.lower() in ("n/a","none","geen","-",""): return ""
        if label in ("SOURCES","BRONNEN"):
            urls = [u.strip() for u in re.split(r'[,\s;]+',val) if u.startswith("http")]
            content = " ".join(
                f'<a href="{u}" target="_blank" style="color:#00a8cc;font-size:10px;'
                f'display:block;margin:1px 0;word-break:break-all">'
                f'{u[:85]}{"…" if len(u)>85 else ""}</a>'
                for u in urls[:5]
            ) or esc(val[:300])
            return (f"<div style='display:flex;gap:8px;margin-top:9px'>"
                    f"<span style='color:{col};flex-shrink:0;font-size:11px'>{icon}</span>"
                    f"<div><div style='color:#1a3a4a;font-size:9px;font-family:monospace;"
                    f"letter-spacing:.1em;margin-bottom:3px'>{label}</div>{content}</div></div>")
        return (f"<div style='display:flex;gap:8px;margin-top:9px;font-size:12px'>"
                f"<span style='color:{col};flex-shrink:0;width:14px'>{icon}</span>"
                f"<div><div style='color:#1a3a4a;font-size:9px;font-family:monospace;"
                f"letter-spacing:.1em;margin-bottom:2px'>{label}</div>"
                f"<div style='color:{c};line-height:1.7'>{esc(val)}</div></div></div>")

    return (
        f"<div style='border:1px solid {col}30;border-radius:10px;padding:16px 18px;"
        f"margin:7px 0;background:linear-gradient(160deg,#040c18,#050e1c);"
        f"position:relative;overflow:hidden'>"
        f"<div style='position:absolute;top:0;right:0;width:80px;height:2px;"
        f"background:linear-gradient(90deg,transparent,{col}60)'></div>"
        f"<div style='display:flex;justify-content:space-between;align-items:flex-start;margin-bottom:10px'>"
        f"<span style='color:#1a3a4a;font-size:9px;font-family:monospace;letter-spacing:.12em'>"
        f"CLAIM {i:02d}/{total:02d}</span>"
        f"<span style='color:{col};font-size:12px;font-weight:600;font-family:monospace'>"
        f"{lbl} &nbsp; {conf}/100</span></div>"
        f"<div style='color:#4a8fa8;font-size:12px;font-style:italic;padding:8px 12px;"
        f"border-left:1px solid {col}60;margin-bottom:10px;line-height:1.6;"
        f"background:{col}08;border-radius:0 6px 6px 0'>\"{esc(claim)}\"</div>"
        f"<div style='height:2px;background:#0a1520;border-radius:1px;margin-bottom:10px'>"
        f"<div style='height:100%;width:{conf}%;background:linear-gradient(90deg,#003050,{col});"
        f"border-radius:1px'></div></div>"
        + row("β–Έ","ANALYSIS",       result.get("analysis",""),         "#a8c8d8")
        + row("β—ˆ","ACADEMIC",       acad,                               "#90c0d0")
        + row("βœ“","SUPPORTING",     result.get("supporting",""),        "#80d4a8")
        + row("βœ—","OPPOSING",       result.get("opposing",""),          "#d4808a")
        + row("πŸ”—","SOURCES",       srcs,                               "#00a8cc")
        + row("πŸ‡¨πŸ‡³","CHINESE",     result.get("chinese_angle",""),     "#d4c080")
        + row("!","RED FLAGS",      result.get("red_flags",""),         "#ff6080")
        + "</div>"
    )

def mk_synthesis(text):
    body = esc(text).replace("\n","<br>")
    return (
        f"<div style='border:1px solid #0a3050;border-radius:10px;padding:20px;"
        f"margin-top:12px;background:linear-gradient(160deg,#020c18,#030e1e);"
        f"position:relative;overflow:hidden'>"
        f"<div style='position:absolute;top:0;left:0;right:0;height:1px;"
        f"background:linear-gradient(90deg,transparent,#00d4ff40,transparent)'></div>"
        f"<div style='color:#00d4ff;font-size:9px;letter-spacing:.14em;font-weight:600;"
        f"margin-bottom:14px;font-family:monospace'>β–Έ FINAL CREDIBILITY ASSESSMENT</div>"
        f"<div style='color:#98b8c8;line-height:1.9;font-size:13px'>{body}</div></div>"
    )

def mk_pdf_btn(plain):
    try:
        from reportlab.platypus import SimpleDocTemplate, Paragraph, Spacer, HRFlowable
        from reportlab.lib.styles import getSampleStyleSheet, ParagraphStyle
        from reportlab.lib.pagesizes import A4
        from reportlab.lib import colors
        buf = BytesIO()
        doc = SimpleDocTemplate(buf,pagesize=A4,leftMargin=50,rightMargin=50,topMargin=60,bottomMargin=60)
        styles = getSampleStyleSheet()
        ts = ParagraphStyle("T",parent=styles["Title"],fontSize=16,spaceAfter=6,
                             textColor=colors.HexColor("#0a3060"))
        bs = ParagraphStyle("B",parent=styles["Normal"],fontSize=11,leading=16,spaceAfter=6)
        safe = plain.replace("&","&amp;").replace("<","&lt;").replace(">","&gt;")
        elems = [Paragraph("DEEP FACT-CHECK RAPPORT",ts),
                 HRFlowable(width="100%",thickness=1,color=colors.HexColor("#0a3060")),Spacer(1,12)]
        for line in safe.split("\n"):
            elems.append(Paragraph(line or "&nbsp;",bs))
        doc.build(elems)
        b64 = base64.b64encode(buf.getvalue()).decode()
        return (f"<div style='margin-top:18px'>"
                f"<a href='data:application/pdf;base64,{b64}' download='rapport.pdf'"
                f" style='display:inline-flex;align-items:center;gap:8px;padding:10px 22px;"
                f"background:#020c18;border:1px solid #0a3050;border-radius:7px;"
                f"color:#00d4ff;text-decoration:none;font-size:11px;"
                f"font-family:monospace;letter-spacing:.08em'>↓ PDF EXPORT</a></div>")
    except Exception as e:
        print(f"[!] PDF: {e}"); return ""

# ─────────────────────────────────────────────────────────────────────────────
# Entry point
# ─────────────────────────────────────────────────────────────────────────────
IDLE = (
    "<div style='display:flex;flex-direction:column;align-items:center;justify-content:center;"
    "min-height:200px;border:1px solid #0a1a25;border-radius:12px;background:#020c15'>"
    "<div style='font-family:monospace;font-size:11px;color:#0a2535;letter-spacing:.15em;"
    "margin-bottom:10px'>AWAITING DOCUMENT INPUT</div>"
    "<div style='display:flex;gap:4px'>"
    + "".join(f"<div style='width:4px;height:4px;border-radius:50%;background:#0a2535'></div>" for _ in range(5))
    + "</div></div>"
)

def run(files, mode, lang, depth_key):
    if not files:
        yield "<p style='color:#ffd166;font-family:monospace;font-size:12px'>⚠ UPLOAD DOCUMENT VEREIST.</p>"; return
    if not client:
        yield "<p style='color:#ff4d6d;font-family:monospace;font-size:12px'>βœ— GROQ_API_KEY MISSING.</p>"; return
    yield mk_status("READING DOCUMENT...")
    txt = read_docs(files)
    if len(txt) < 80:
        yield "<p style='color:#ff4d6d;font-family:monospace;font-size:12px'>βœ— INSUFFICIENT TEXT.</p>"; return
    if mode == "πŸ“– Samenvatting":
        yield from do_summary(txt, lang); return
    if not DDG_OK:
        yield "<p style='color:#ff4d6d;font-family:monospace;font-size:12px'>βœ— duckduckgo-search MISSING.</p>"; return
    n = DEPTHS.get(depth_key, 5)
    yield from do_factcheck(txt, lang, n)

def reset_all():
    return None, "πŸ” Fact-Check", "Nederlands", "5  claims", IDLE

# ─────────────────────────────────────────────────────────────────────────────
# CSS β€” Space / High-tech aesthetic
# ─────────────────────────────────────────────────────────────────────────────
CSS = """
@import url('https://fonts.googleapis.com/css2?family=Share+Tech+Mono&family=Exo+2:wght@300;400;600;700&display=swap');

@keyframes scan-pulse { 0%,100%{border-color:#00d4ff22} 50%{border-color:#00d4ff88} }
@keyframes star-drift { from{transform:translateY(0)} to{transform:translateY(-100%)} }
@keyframes blink { 0%,100%{opacity:1} 50%{opacity:0.3} }

html, body {
    background: #020c15 !important;
    font-family: 'Exo 2', sans-serif !important;
}
.gradio-container {
    background: #020c15 !important;
    min-height: 100vh;
    background-image:
        radial-gradient(ellipse at 20% 50%, #001a2e18 0%, transparent 60%),
        radial-gradient(ellipse at 80% 20%, #00264020 0%, transparent 50%),
        radial-gradient(1px 1px at 10% 15%, #ffffff18, transparent),
        radial-gradient(1px 1px at 30% 40%, #ffffff10, transparent),
        radial-gradient(1px 1px at 55% 25%, #ffffff14, transparent),
        radial-gradient(1px 1px at 75% 60%, #ffffff0c, transparent),
        radial-gradient(1px 1px at 88% 10%, #ffffff18, transparent),
        radial-gradient(1px 1px at 45% 80%, #ffffff0e, transparent),
        radial-gradient(1px 1px at 65% 90%, #ffffff12, transparent),
        radial-gradient(1px 1px at 5%  70%, #ffffff10, transparent) !important;
}
.panel {
    background: rgba(2,12,22,0.95) !important;
    border: 1px solid #0a2030 !important;
    border-radius: 12px !important;
    padding: 20px !important;
    position: relative;
    overflow: hidden;
}
.panel::before {
    content: '';
    position: absolute;
    top: 0; left: 0; right: 0; height: 1px;
    background: linear-gradient(90deg, transparent, #00d4ff30, transparent);
    pointer-events: none;
}

/* Inputs */
textarea, input[type=text], select {
    background: #010a14 !important;
    border: 1px solid #0a2535 !important;
    border-radius: 6px !important;
    color: #6a9ab0 !important;
    font-family: 'Share Tech Mono', monospace !important;
    font-size: 12px !important;
    letter-spacing: .03em !important;
}
textarea:focus, input:focus { border-color: #00d4ff60 !important; outline: none !important; }

/* Labels */
label > span, .label-wrap span, .block > label > span {
    color: #1a5a6a !important;
    font-family: 'Share Tech Mono', monospace !important;
    font-size: 10px !important;
    letter-spacing: .15em !important;
    text-transform: uppercase !important;
}

/* Buttons */
.gr-button {
    border-radius: 6px !important;
    font-family: 'Share Tech Mono', monospace !important;
    font-size: 11px !important;
    letter-spacing: .1em !important;
    text-transform: uppercase !important;
    transition: all .25s ease !important;
}
button.primary, .gr-button-primary {
    background: linear-gradient(135deg, #010e1c, #012535) !important;
    border: 1px solid #00d4ff !important;
    color: #00d4ff !important;
    font-weight: 600 !important;
    box-shadow: 0 0 12px #00d4ff15, inset 0 0 12px #00d4ff08 !important;
}
button.primary:hover, .gr-button-primary:hover {
    background: linear-gradient(135deg, #002030, #003050) !important;
    box-shadow: 0 0 20px #00d4ff40, inset 0 0 20px #00d4ff15 !important;
    color: #00f5ff !important;
    transform: none !important;
}
button.secondary {
    background: #010a14 !important;
    border: 1px solid #0a2030 !important;
    color: #2a5a6a !important;
}
button.secondary:hover {
    border-color: #ff4d6d80 !important;
    color: #ff4d6d !important;
    box-shadow: 0 0 10px #ff4d6d20 !important;
}

/* Radio */
.gr-radio label span {
    color: #5a8a9a !important;
    font-size: 12px !important;
    text-transform: none !important;
    letter-spacing: 0 !important;
    font-family: 'Exo 2', sans-serif !important;
}

/* File upload */
.gr-file {
    background: #010a14 !important;
    border: 1px dashed #0a2535 !important;
    border-radius: 8px !important;
}

/* Dropdown */
.gr-dropdown select, select option {
    background: #010a14 !important;
    color: #6a9ab0 !important;
}

/* Scrollbar */
::-webkit-scrollbar { width: 4px; }
::-webkit-scrollbar-track { background: #010a14; }
::-webkit-scrollbar-thumb { background: #0a3040; border-radius: 2px; }
::-webkit-scrollbar-thumb:hover { background: #00d4ff40; }
"""

# ─────────────────────────────────────────────────────────────────────────────
# Layout
# ─────────────────────────────────────────────────────────────────────────────
with gr.Blocks(title="Deep Analysis Engine", css=CSS) as demo:

    # Animated header
    gr.HTML("""
    <div style='padding:20px 24px 16px;border-bottom:1px solid #0a1e2a;margin-bottom:16px;
                position:relative;overflow:hidden'>
      <div style='position:absolute;top:0;left:0;right:0;bottom:0;
                  background:repeating-linear-gradient(90deg,transparent,transparent 79px,
                  #00d4ff06 79px,#00d4ff06 80px);pointer-events:none'></div>
      <div style='display:flex;align-items:flex-start;gap:16px;position:relative'>"
        <div style='flex-shrink:0;margin-top:4px'>
          <div style='width:10px;height:10px;border-radius:50%;background:#00f5c3;
                      box-shadow:0 0 12px #00f5c3,0 0 24px #00f5c360'></div>
        </div>
        <div>
          <div style='font-family:"Share Tech Mono",monospace;font-size:22px;
                      color:#e8f4f8;letter-spacing:-.01em;line-height:1'>
            DEEP ANALYSIS ENGINE
          </div>
          <div style='font-family:"Share Tech Mono",monospace;font-size:9px;
                      color:#0a3a4a;letter-spacing:.18em;margin-top:6px'>
            SEMANTIC SCHOLAR &nbsp;Β·&nbsp; OPENALEX &nbsp;Β·&nbsp; ARXIV &nbsp;Β·&nbsp;
            CONSENSUS &nbsp;Β·&nbsp; WEB MULTI-QUERY &nbsp;Β·&nbsp; 4Γ— PARALLEL WORKERS
          </div>
        </div>
        <div style='margin-left:auto;text-align:right;font-family:"Share Tech Mono",monospace;
                    font-size:9px;color:#0a2535;line-height:1.8'>
          <div style='color:#1a4a5a'>v4.0</div>
          <div id='sys-time'></div>
        </div>
      </div>
    </div>
    <script>
      function tick(){
        var d=new Date();
        var el=document.getElementById('sys-time');
        if(el) el.textContent=d.toUTCString().replace('GMT','UTC');
      }
      tick(); setInterval(tick,1000);
    </script>
    """)

    with gr.Row():
        # ── Control panel ──────────────────────────────────────────────────────
        with gr.Column(scale=1, min_width=265, elem_classes="panel"):

            gr.HTML("<div style='font-family:\"Share Tech Mono\",monospace;font-size:9px;"
                    "color:#1a5a6a;letter-spacing:.15em;margin-bottom:12px;"
                    "border-bottom:1px solid #0a1e2a;padding-bottom:8px'>"
                    "β–Έ DOCUMENT INPUT</div>")

            f_in = gr.File(file_count="multiple",
                           label="Upload  Β·  PDF / DOCX",
                           file_types=[".pdf",".docx"])

            gr.HTML("<div style='height:1px;background:#0a1e2a;margin:14px 0'></div>")
            gr.HTML("<div style='font-family:\"Share Tech Mono\",monospace;font-size:9px;"
                    "color:#1a5a6a;letter-spacing:.15em;margin-bottom:12px'>"
                    "β–Έ MISSION PARAMETERS</div>")

            mode = gr.Radio(choices=["πŸ” Fact-Check","πŸ“– Samenvatting"],
                            value="πŸ” Fact-Check", label="Mode")
            lang = gr.Dropdown(LANGS, value="Nederlands", label="Output Language")
            dep  = gr.Dropdown(list(DEPTHS.keys()), value="5  claims",
                               label="Claim Depth")

            gr.HTML("""
            <div style='font-family:"Share Tech Mono",monospace;font-size:9px;
                        color:#0a2535;line-height:2;margin-top:10px;
                        border-top:1px solid #0a1e2a;padding-top:10px'>
              <span style='color:#1a4a5a'>SOURCES</span><br>
              β—‹ Semantic Scholar<br>
              β—‹ OpenAlex (Research Rabbit layer)<br>
              β—‹ arXiv preprints<br>
              β—‹ Consensus.app<br>
              β—‹ DuckDuckGo EN + ZH<br>
              β—‹ Page content fetcher<br>
              <span style='color:#1a4a5a;margin-top:6px;display:block'>WORKERS</span>
              4 parallel Β· 6 queries/claim<br>
              30 claims β‰ˆ 12-18 min
            </div>""")

            gr.HTML("<div style='height:1px;background:#0a1e2a;margin:14px 0'></div>")
            with gr.Row():
                btn = gr.Button("β–Ά LAUNCH", variant="primary", scale=3)
                rst = gr.Button("β†Ί", variant="secondary", scale=1)

        # ── Output panel ───────────────────────────────────────────────────────
        with gr.Column(scale=2, elem_classes="panel"):
            out = gr.HTML(IDLE)

    btn.click(run,       inputs=[f_in,mode,lang,dep], outputs=out)
    rst.click(reset_all, inputs=[],                   outputs=[f_in,mode,lang,dep,out])

demo.queue(max_size=5)
demo.launch(server_name="0.0.0.0", server_port=7860)