File size: 20,145 Bytes
f93c1b8
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8"/>
<meta name="viewport" content="width=device-width, initial-scale=1"/>
<title>PathQA — interactive demo</title>
<style>
  :root{
    --bg:#0f1419; --panel:#161c24; --panel2:#1c242e; --line:#2a343f;
    --text:#e6edf3; --muted:#8b98a5; --accent:#2dd4bf; --accent2:#38bdf8;
    --warn:#f59e0b; --ok:#34d399;
  }
  *{box-sizing:border-box}
  body{margin:0;background:var(--bg);color:var(--text);
    font-family:-apple-system,BlinkMacSystemFont,"Segoe UI",Roboto,Helvetica,Arial,sans-serif}
  .wrap{max-width:1120px;margin:0 auto;padding:24px}
  header h1{margin:0;font-size:20px}
  header .sub{color:var(--muted);font-size:13px;margin-top:2px}
  .banner{margin:14px 0 18px;padding:10px 14px;border:1px solid var(--line);
    border-left:3px solid var(--accent);background:var(--panel);border-radius:8px;
    font-size:13px;color:var(--muted)}
  .banner b{color:var(--text)}
  .demoflag{margin:0 0 18px;padding:8px 12px;border:1px dashed var(--warn);border-radius:8px;
    font-size:12px;color:var(--warn);background:rgba(245,158,11,.06)}
  .tabs{display:flex;gap:8px;margin-bottom:16px;align-items:center}
  .tab{background:var(--panel);border:1px solid var(--line);color:var(--muted);
    padding:8px 14px;border-radius:8px;font-size:13px;cursor:pointer}
  .tab.active{color:var(--text);border-color:var(--accent);background:var(--panel2)}
  .spacer{flex:1}
  .cards{display:grid;grid-template-columns:repeat(4,1fr);gap:12px;margin-bottom:16px}
  .card{background:var(--panel);border:1px solid var(--line);border-radius:10px;padding:14px}
  .card .k{color:var(--muted);font-size:12px;text-transform:uppercase;letter-spacing:.4px}
  .card .v{font-size:24px;font-weight:600;margin-top:6px}
  .card .v small{font-size:13px;color:var(--muted);font-weight:400}
  .controls{display:flex;align-items:center;gap:14px;background:var(--panel);
    border:1px solid var(--line);border-radius:10px;padding:14px;margin-bottom:16px;flex-wrap:wrap}
  .controls label{font-size:13px;color:var(--muted)}
  input[type=range]{accent-color:var(--accent);width:210px;vertical-align:middle}
  .pill{font-size:12px;padding:3px 8px;border-radius:999px;border:1px solid var(--line)}
  .pill.tumor{color:#fca5a5;border-color:#5b2b2b;background:#2a1414}
  .pill.normal{color:#a7f3d0;border-color:#1f4d3d;background:#0f241d}
  table{width:100%;border-collapse:collapse;background:var(--panel);border:1px solid var(--line);
    border-radius:10px;overflow:hidden}
  th,td{padding:10px 12px;text-align:left;font-size:13px;border-bottom:1px solid var(--line)}
  th{color:var(--muted);font-weight:500;text-transform:uppercase;font-size:11px;letter-spacing:.5px}
  tr:last-child td{border-bottom:none}
  tr.done{opacity:.5}
  .mono{font-family:ui-monospace,SFMono-Regular,Menlo,monospace;font-size:12px}
  canvas.thumb{width:52px;height:52px;border-radius:6px;border:1px solid var(--line);
    cursor:pointer;image-rendering:pixelated;display:block}
  .confbar{height:6px;background:var(--panel2);border-radius:3px;overflow:hidden;width:90px}
  .confbar>span{display:block;height:100%;background:linear-gradient(90deg,var(--accent),var(--accent2))}
  .btn{font-size:12px;padding:6px 10px;border-radius:7px;border:1px solid var(--line);
    background:var(--panel2);color:var(--text);cursor:pointer}
  .btn:hover{border-color:var(--accent)}
  .btn.amend{border-color:#7a4a12;color:#fcd34d}
  .btn.confirm{border-color:#1f4d3d;color:#a7f3d0}
  .btn.ghost{background:transparent}
  .tag{font-size:11px;padding:2px 8px;border-radius:999px}
  .tag.confirm{background:#0f241d;color:#a7f3d0}
  .tag.amend{background:#2a1c0a;color:#fcd34d}
  .empty{padding:40px;text-align:center;color:var(--muted)}
  .slidegrid{display:grid;grid-template-columns:repeat(2,1fr);gap:14px}
  .slidecard{background:var(--panel);border:1px solid var(--line);border-radius:12px;overflow:hidden}
  .slidecard.done{opacity:.55}
  .slidecard canvas.heat{width:100%;display:block;cursor:pointer;image-rendering:pixelated;border-bottom:1px solid var(--line)}
  .scbody{padding:12px 14px}
  .foot{color:var(--muted);font-size:12px;margin-top:22px;line-height:1.6}
  .modal{position:fixed;inset:0;background:rgba(0,0,0,.6);display:flex;align-items:center;justify-content:center;z-index:20}
  .modal .box{background:var(--panel);border:1px solid var(--line);border-radius:12px;padding:20px;max-width:460px;width:92%}
  .modal canvas{width:180px;height:180px;image-rendering:pixelated;border-radius:8px;border:1px solid var(--line)}
  .note{background:var(--panel2);border:1px solid var(--line);color:var(--text);border-radius:6px;padding:5px 7px;font-size:12px;width:120px}
</style>
</head>
<body>
<div class="wrap">
  <header>
    <h1>PathQA · second-reader QA</h1>
    <div class="sub">An AI safety-net for the pathology sign-out — quality assurance, not diagnosis.</div>
  </header>
  <div class="banner"><b>Assist, not replace.</b> PathQA lists only the cases where the model confidently
    disagrees with the sign-out, so a human can take a second look. You decide the outcome. No sign-out is ever changed automatically.</div>
  <div class="demoflag">Interactive browser demo — synthetic H&amp;E patches and a lightweight in-page model stand in for the backend,
    so you can click through the workflow with zero install. The production app runs the real trained PyTorch model locally (<span class="mono">./pathqa-app</span>).</div>

  <div class="tabs">
    <button class="tab active" id="tab-patch">Patch QA</button>
    <button class="tab" id="tab-slide">Whole-slide QA</button>
    <span class="spacer"></span>
    <button class="btn ghost" id="regen">↻ Regenerate cohort</button>
  </div>

  <div id="view"></div>

  <div class="foot">Every decision would be appended to a local audit log (<span class="mono">audit_log.jsonl</span>) in the real app.
    PathQA outputs re-check recommendations only — it does not diagnose. The pathologist is in control.</div>
</div>

<div id="modalRoot"></div>

<script>
"use strict";
/* ---------- deterministic RNG ---------- */
function mulberry32(a){return function(){a|=0;a=a+0x6D2B79F5|0;let t=Math.imul(a^a>>>15,1|a);t=t+Math.imul(t^t>>>7,61|t)^t;return((t^t>>>14)>>>0)/4294967296;};}
let SEED = 1234;
let rng = mulberry32(SEED);
const rand=(a,b)=>a+(b-a)*rng();
const clamp=(x,a,b)=>Math.max(a,Math.min(b,x));

/* ---------- state ---------- */
let view="patch";
let tauPatch=0.90, tauSlide=0.70;
let patches=[], slides=[];
let decisions={};       // id -> "confirm"|"amend"
const PATCH_N=120, SLIDE_N=8, GRID=14;

/* ---------- patch cohort ---------- */
function makePatch(i){
  const truth = (rng()<0.5)?1:0;                 // real tissue label
  const correct = rng() > 0.10;                  // model ~90% accurate
  let prob;
  if(truth===1) prob = correct ? rand(0.72,0.99) : rand(0.10,0.40);
  else          prob = correct ? rand(0.01,0.28) : rand(0.58,0.90);
  const call = prob>=0.5?1:0;
  const conf = Math.max(prob,1-prob);
  const err = rng()<0.07;                         // pathologist mis-read
  const path = err ? 1-truth : truth;
  // nuclei layout (stored so patch + Grad-CAM stay consistent)
  const nuclei=[]; const cluster={cx:rand(0.3,0.7),cy:rand(0.3,0.7),rad:rand(0.16,0.26)};
  const scatter = truth?12:18;
  for(let k=0;k<scatter;k++) nuclei.push({x:rng(),y:rng(),r:rand(0.02,0.045)});
  if(truth===1){ for(let k=0;k<70;k++){const a=rng()*6.28,d=Math.sqrt(rng())*cluster.rad;
    nuclei.push({x:clamp(cluster.cx+Math.cos(a)*d,0,1),y:clamp(cluster.cy+Math.sin(a)*d,0,1),r:rand(0.02,0.05),dense:true});}}
  return {id:"case_"+String(i).padStart(6,"0"),truth,prob,call,conf,path,nuclei,cluster};
}
/* ---------- draw a patch ---------- */
function drawPatch(cv,p){
  const S=96; cv.width=S; cv.height=S; const g=cv.getContext("2d");
  g.fillStyle = p.truth? "#d7b6df" : "#ecccd6"; g.fillRect(0,0,S,S);
  // speckle
  const img=g.getImageData(0,0,S,S), d=img.data;
  for(let j=0;j<d.length;j+=4){const n=(Math.random()-0.5)*22; d[j]+=n; d[j+1]+=n*0.6; d[j+2]+=n;}
  g.putImageData(img,0,0);
  // nuclei
  for(const nu of p.nuclei){
    g.beginPath(); g.arc(nu.x*S,nu.y*S,nu.r*S,0,6.283);
    g.fillStyle = nu.dense? "rgba(84,40,110,0.85)" : "rgba(140,70,120,0.6)"; g.fill();
  }
}
/* ---------- draw Grad-CAM overlay ---------- */
function drawCAM(cv,p){
  drawPatch(cv,p); const S=cv.width, g=cv.getContext("2d");
  const cx=(p.call?p.cluster.cx:0.5)*S, cy=(p.call?p.cluster.cy:0.5)*S;
  const intensity = clamp((p.conf-0.5)*2,0,1);            // stronger for confident calls
  const R = (p.call? p.cluster.rad*1.9 : 0.5)*S;
  const grd=g.createRadialGradient(cx,cy,2,cx,cy,R);
  grd.addColorStop(0,`rgba(255,40,40,${0.65*intensity})`);
  grd.addColorStop(0.5,`rgba(255,120,30,${0.35*intensity})`);
  grd.addColorStop(1,"rgba(255,80,40,0)");
  g.fillStyle=grd; g.fillRect(0,0,S,S);
}

/* ---------- slide cohort ---------- */
function makeSlide(i){
  const truth=(i%2===0)?1:0;
  const n=GRID; const grid=[]; const tumorCells=new Set();
  if(truth===1){ // grow a blob
    let cr=2+Math.floor(rng()*(n-4)), cc=2+Math.floor(rng()*(n-4));
    const cells=[[cr,cc]]; const K=Math.floor(0.10*n*n);
    while(cells.length<K){const b=cells[Math.floor(rng()*cells.length)];
      const nr=clamp(b[0]+Math.floor(rand(-1,2)),0,n-1), nc=clamp(b[1]+Math.floor(rand(-1,2)),0,n-1);
      cells.push([nr,nc]);}
    cells.forEach(c=>tumorCells.add(c[0]+"_"+c[1]));
  }
  const probs=[];
  for(let r=0;r<n;r++){const row=[];for(let c=0;c<n;c++){
    const dist=Math.pow((r-n/2)/(n/2),2)+Math.pow((c-n/2)/(n/2),2);
    if(dist>1.05){row.push(-1);continue;}                 // glass / background
    const isT=tumorCells.has(r+"_"+c);
    row.push(isT? rand(0.75,0.99) : rand(0.0,0.18));
  }row.push();grid.push(row);}
  const flat=grid.flat().filter(v=>v>=0).sort((a,b)=>b-a);
  const K=Math.max(4,Math.floor(0.02*flat.length));
  const score = flat.slice(0,K).reduce((a,b)=>a+b,0)/K;
  const call=score>=0.5?1:0, conf=Math.max(score,1-score);
  const tumorTiles = flat.filter(v=>v>=0.5).length, tissue=flat.length;
  const err = rng()<0.25; const path = err? 1-truth : truth;
  return {id:"slide_"+String(i).padStart(3,"0"),truth,grid,score,call,conf,tumorTiles,tissue,path};
}
function drawHeat(cv,s){
  const n=GRID, cell=14; cv.width=n*cell; cv.height=n*cell; const g=cv.getContext("2d");
  g.fillStyle="#0f1419"; g.fillRect(0,0,cv.width,cv.height);
  for(let r=0;r<n;r++)for(let c=0;c<n;c++){
    const p=s.grid[r][c]; if(p<0) continue;
    let col;
    if(p<0.5){const t=p/0.5; col=`rgb(${29+t*160|0},${158+t*40|0},${117-t*80|0})`;}
    else{const t=(p-0.5)/0.5; col=`rgb(${245},${199-t*160|0},${39+t*36|0})`;}
    g.fillStyle=col; g.fillRect(c*cell,r*cell,cell,cell);
  }
}

/* ---------- build ---------- */
function rebuild(){
  rng=mulberry32(SEED); decisions={};
  patches=[]; for(let i=0;i<PATCH_N;i++) patches.push(makePatch(i));
  slides=[]; for(let i=0;i<SLIDE_N;i++) slides.push(makeSlide(i));
  render();
}

/* ---------- helpers ---------- */
const pill=(v)=>`<span class="pill ${v?'tumor':'normal'}">${v?'tumor':'normal'}</span>`;
const pct=(x)=>(100*x).toFixed(1)+"%";
function el(html){const d=document.createElement("div"); d.innerHTML=html.trim(); return d.firstChild;}

/* ---------- patch view ---------- */
function patchWorklist(){ // flagged = confident disagreement
  return patches.map(p=>({p,flagged:(p.call!==p.path)&&p.conf>=tauPatch}))
    .filter(x=>x.flagged).sort((a,b)=>b.p.conf-a.p.conf).map(x=>x.p);
}
function renderPatch(){
  const disN = patches.filter(p=>p.call!==p.path).length;
  const wl = patchWorklist();
  const resolved = wl.filter(p=>decisions[p.id]).length;
  const view=document.getElementById("view");
  view.innerHTML=`
    <div class="cards">
      <div class="card"><div class="k">Cases reviewed</div><div class="v">${patches.length}</div></div>
      <div class="card"><div class="k">Disagreements</div><div class="v">${disN} <small>· ${pct(disN/patches.length)}</small></div></div>
      <div class="card"><div class="k">Flagged</div><div class="v">${wl.length} <small>· ${pct(wl.length/patches.length)}</small></div></div>
      <div class="card"><div class="k">Resolved</div><div class="v">${resolved} / ${wl.length}</div></div>
    </div>
    <div class="controls">
      <label>Flag confidence threshold (τ)</label>
      <input type="range" id="tp" min="0.5" max="0.999" step="0.005" value="${tauPatch}">
      <span class="mono" id="tpv">τ = ${tauPatch.toFixed(3)}</span>
      <span style="color:var(--muted);font-size:12px">lower τ → catch more · higher τ → shorter queue</span>
    </div>
    <div id="ptable"></div>`;
  const tp=document.getElementById("tp");
  tp.oninput=()=>{document.getElementById("tpv").textContent="τ = "+parseFloat(tp.value).toFixed(3);};
  tp.onchange=()=>{tauPatch=parseFloat(tp.value); renderPatch();};
  const holder=document.getElementById("ptable");
  if(wl.length===0){holder.innerHTML='<div class="empty">Nothing left to re-check at this threshold. ✅</div>'; return;}
  const table=el('<table><thead><tr><th>#</th><th>Patch</th><th>Case</th><th>Sign-out</th><th>Model reads</th><th>Confidence</th><th>Re-review outcome</th></tr></thead><tbody></tbody></table>');
  const tb=table.querySelector("tbody");
  wl.forEach((p,idx)=>{
    const done=decisions[p.id];
    const tr=el(`<tr class="${done?'done':''}">
      <td>${idx+1}</td><td></td><td class="mono">${p.id}</td>
      <td>${pill(p.path)}</td><td>${pill(p.call)}</td>
      <td><div class="confbar"><span style="width:${(100*p.conf)}%"></span></div><span class="mono" style="font-size:11px">${p.conf.toFixed(3)}</span></td>
      <td></td></tr>`);
    const cv=document.createElement("canvas"); cv.className="thumb"; drawPatch(cv,p);
    cv.onclick=()=>openPatchModal(p); tr.children[1].appendChild(cv);
    const out=tr.children[6];
    if(done){ out.innerHTML=`<span class="tag ${done}">${done==='amend'?'Amended':'Sign-out confirmed'}</span>`; }
    else{
      const c=el('<button class="btn confirm">Confirm sign-out</button>');
      const a=el('<button class="btn amend" style="margin-left:6px">Amend (model was right)</button>');
      c.onclick=()=>{decisions[p.id]="confirm"; renderPatch();};
      a.onclick=()=>{decisions[p.id]="amend"; renderPatch();};
      out.appendChild(c); out.appendChild(a);
    }
    tb.appendChild(tr);
  });
  holder.appendChild(table);
}
function openPatchModal(p){
  const root=document.getElementById("modalRoot");
  root.innerHTML="";
  const m=el(`<div class="modal"><div class="box">
     <div style="display:flex;justify-content:space-between;margin-bottom:10px">
       <span class="mono">${p.id}</span><span style="cursor:pointer;color:var(--muted)" id="x">✕</span></div>
     <div style="display:flex;gap:14px;justify-content:center">
       <div style="text-align:center"><canvas id="mp"></canvas><div style="font-size:11px;color:var(--muted);margin-top:4px">patch</div></div>
       <div style="text-align:center"><canvas id="mc"></canvas><div style="font-size:11px;color:var(--muted);margin-top:4px">model attention (Grad-CAM)</div></div>
     </div>
     <p style="font-size:13px;color:var(--muted);margin-top:12px">Model reads <b>${p.call?'tumor':'normal'}</b> (confidence ${p.conf.toFixed(2)}) but sign-out is <b>${p.path?'tumor':'normal'}</b> — recommend re-review.</p>
   </div></div>`);
  root.appendChild(m);
  drawPatch(m.querySelector("#mp"),p); drawCAM(m.querySelector("#mc"),p);
  m.onclick=(e)=>{if(e.target===m||e.target.id==="x") root.innerHTML="";};
}

/* ---------- slide view ---------- */
function slideWorklist(){
  return slides.map(s=>({s,flagged:(s.call!==s.path)&&s.conf>=tauSlide}))
    .filter(x=>x.flagged).sort((a,b)=>b.s.conf-a.s.conf).map(x=>x.s);
}
function renderSlide(){
  const wl=slideWorklist();
  const resolved=wl.filter(s=>decisions[s.id]).length;
  const view=document.getElementById("view");
  view.innerHTML=`
    <div class="cards" style="grid-template-columns:repeat(3,1fr)">
      <div class="card"><div class="k">Slides reviewed</div><div class="v">${slides.length}</div></div>
      <div class="card"><div class="k">Flagged for re-review</div><div class="v">${wl.length}</div></div>
      <div class="card"><div class="k">Resolved</div><div class="v">${resolved} / ${wl.length}</div></div>
    </div>
    <div class="controls">
      <label>Slide flag threshold (τ)</label>
      <input type="range" id="ts" min="0.5" max="0.999" step="0.005" value="${tauSlide}">
      <span class="mono" id="tsv">τ = ${tauSlide.toFixed(3)}</span>
      <span style="color:var(--muted);font-size:12px">heatmap: green = normal, red = suspicious tissue</span>
    </div>
    <div id="sgrid"></div>`;
  const ts=document.getElementById("ts");
  ts.oninput=()=>{document.getElementById("tsv").textContent="τ = "+parseFloat(ts.value).toFixed(3);};
  ts.onchange=()=>{tauSlide=parseFloat(ts.value); renderSlide();};
  const holder=document.getElementById("sgrid");
  if(wl.length===0){holder.innerHTML='<div class="empty">No slides flagged at this threshold. ✅</div>'; return;}
  const grid=el('<div class="slidegrid"></div>');
  wl.forEach((s,idx)=>{
    const done=decisions[s.id];
    const card=el(`<div class="slidecard ${done?'done':''}"></div>`);
    const cv=document.createElement("canvas"); cv.className="heat"; drawHeat(cv,s); cv.onclick=()=>openSlideModal(s);
    card.appendChild(cv);
    const body=el(`<div class="scbody">
      <div style="display:flex;justify-content:space-between;align-items:center">
        <span class="mono">#${idx+1} · ${s.id}</span>
        <span class="mono" style="font-size:11px;color:var(--muted)">score ${s.score.toFixed(2)}</span></div>
      <div style="margin:8px 0;font-size:13px">sign-out ${pill(s.path)} &nbsp;vs&nbsp; model ${pill(s.call)}</div>
      <div style="font-size:12px;color:var(--muted);margin-bottom:10px">${s.tumorTiles} suspicious / ${s.tissue} tissue tiles · conf ${s.conf.toFixed(2)}</div>
      <div class="acts"></div></div>`);
    const acts=body.querySelector(".acts");
    if(done){acts.innerHTML=`<span class="tag ${done}">${done==='amend'?'Amended':'Sign-out confirmed'}</span>`;}
    else{
      const c=el('<button class="btn confirm">Confirm sign-out</button>');
      const a=el('<button class="btn amend" style="margin-left:6px">Amend (model was right)</button>');
      c.onclick=()=>{decisions[s.id]="confirm"; renderSlide();};
      a.onclick=()=>{decisions[s.id]="amend"; renderSlide();};
      acts.appendChild(c); acts.appendChild(a);
    }
    card.appendChild(body); grid.appendChild(card);
  });
  holder.appendChild(grid);
}
function openSlideModal(s){
  const root=document.getElementById("modalRoot"); root.innerHTML="";
  const m=el(`<div class="modal"><div class="box">
     <div style="display:flex;justify-content:space-between;margin-bottom:10px">
       <span class="mono">${s.id}</span><span style="cursor:pointer;color:var(--muted)" id="x">✕</span></div>
     <div style="text-align:center"><canvas id="mh" style="width:280px;height:280px"></canvas></div>
     <p style="font-size:13px;color:var(--muted);margin-top:12px">Model reads <b>${s.call?'tumor':'normal'}</b> (slide score ${s.score.toFixed(2)}, ${s.tumorTiles} suspicious tiles) but sign-out is <b>${s.path?'tumor':'normal'}</b> — recommend re-review.</p>
   </div></div>`);
  root.appendChild(m); drawHeat(m.querySelector("#mh"),s);
  m.onclick=(e)=>{if(e.target===m||e.target.id==="x") root.innerHTML="";};
}

/* ---------- shell ---------- */
function render(){ view==="patch"?renderPatch():renderSlide(); }
document.getElementById("tab-patch").onclick=()=>{view="patch";
  document.getElementById("tab-patch").classList.add("active");
  document.getElementById("tab-slide").classList.remove("active"); render();};
document.getElementById("tab-slide").onclick=()=>{view="slide";
  document.getElementById("tab-slide").classList.add("active");
  document.getElementById("tab-patch").classList.remove("active"); render();};
document.getElementById("regen").onclick=()=>{SEED=(SEED*1103515245+12345)>>>0; rebuild();};
rebuild();
</script>
</body>
</html>