Files changed (1) hide show
  1. app.py +508 -14
app.py CHANGED
@@ -653,6 +653,404 @@ QUY TẮC PHẢI TUÂN THỦ:
653
  save_chat_log(user_message, ai_reply)
654
  return "", history
655
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
656
  custom_css = """
657
  .gradio-container {
658
  background: linear-gradient(180deg, #eff5f3 0%, #e1ebe7 100%) !important;
@@ -806,28 +1204,124 @@ custom_css = """
806
  0%, 100% { transform: scale(1); filter: drop-shadow(0 2px 5px rgba(0,129,112,0.2)); }
807
  50% { transform: scale(1.04); filter: drop-shadow(0 4px 12px rgba(0,191,165,0.4)); }
808
  }
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
809
  """
810
 
811
  initial_ticket_text, initial_ticket_img = check_user_expert_status()
812
 
813
- with gr.Blocks(title="Bệnh Viện Cây Trồng - Hệ Thống Chẩn Đoán AI", css=custom_css) as demo:
814
  with gr.Row(visible=True, elem_id="login_wrapper") as login_panel:
815
- with gr.Column(scale=1): pass
816
- with gr.Column(scale=1, elem_classes="login-card"):
 
 
 
 
 
 
 
 
 
 
 
 
 
 
817
  gr.HTML("""
818
- <div class="logo-container">
819
- <span class="rice-icon-left">🌾</span>
820
- <div class="medical-cross-css"></div>
821
- <span class="rice-icon-right">🌾</span>
822
  </div>
823
- <h1 class="app-main-title">BỆNH VIỆN CÂY TRỒNG</h1>
824
- <p class="app-sub-title">Cổng kết nối Trợ lý Bác sĩ AI xét nghiệm tế bào lá</p>
825
  """)
826
- username_input = gr.Textbox(label="Tên đăng nhập / Tài khoản", placeholder="Nhập tài khoản đăng nhập...", max_lines=1)
827
- password_input = gr.Textbox(label="Mật khẩu bảo mật", type="password", placeholder="Nhập mật khẩu...", max_lines=1)
828
- error_output = gr.HTML("")
829
- btn_login = gr.Button("KÍCH HOẠT HỆ THỐNG XÉT NGHIỆM", variant="primary", elem_classes="scan-btn")
830
- with gr.Column(scale=1): pass
831
 
832
  with gr.Column(visible=False) as main_panel:
833
  with gr.Row(elem_classes="hospital-header"):
 
653
  save_chat_log(user_message, ai_reply)
654
  return "", history
655
 
656
+ LEAF_HERO_HEAD = r"""
657
+ <link rel="preconnect" href="https://fonts.googleapis.com">
658
+ <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
659
+ <link href="https://fonts.googleapis.com/css2?family=Be+Vietnam+Pro:wght@300;400;500;600;700;800&display=swap" rel="stylesheet">
660
+ <script>
661
+ (function () {
662
+ "use strict";
663
+ const TAU = Math.PI * 2, RAD = Math.PI / 180;
664
+ const LEAF_CASES = [
665
+ { name:"Khỏe mạnh", a:0.387, e:0.206, i:7, node:48, peri:29, M0:175, color:"#4caf50", size:7, glow:1.15 },
666
+ { name:"Thiếu đạm", a:0.723, e:0.007, i:3.4, node:77, peri:55, M0:50, color:"#8bc34a", size:9 },
667
+ { name:"Đốm nâu", a:1.0, e:0.017, i:0, node:349, peri:114, M0:358, color:"#6d4c2a", size:10 },
668
+ { name:"Phấn trắng", a:1.524, e:0.093, i:1.8, node:50, peri:286, M0:19, color:"#a5d6a7", size:8 },
669
+ { name:"Gỉ sắt", a:5.203, e:0.048, i:1.3, node:100, peri:274, M0:20, color:"#e08d3c", size:13 },
670
+ { name:"Cháy bìa lá", a:9.537, e:0.054, i:2.5, node:114, peri:339, M0:317, color:"#795548", size:12 },
671
+ ];
672
+ const PLANE_FAN = [[58,35],[27,145],[71,250],[40,80],[84,190],[33,310]];
673
+ const ECC_FAN = [0.52, 0.34, 0.63, 0.44, 0.3, 0.58];
674
+
675
+ function eccentricAnomaly(M, e) {
676
+ let m = M % TAU; if (m < 0) m += TAU;
677
+ let E = m + e * Math.sin(m) * (1 + e * Math.cos(m));
678
+ for (let k = 0; k < 8; k++) {
679
+ const step = (E - e * Math.sin(E) - m) / (1 - e * Math.cos(E));
680
+ E -= step; if (Math.abs(step) < 1e-10) break;
681
+ }
682
+ return E;
683
+ }
684
+ function parseRGB(c) {
685
+ if (c[0]==="#") { const h=c.slice(1); const f=h.length===3?h[0]+h[0]+h[1]+h[1]+h[2]+h[2]:h; const n=parseInt(f,16); return[(n>>16)&255,(n>>8)&255,n&255]; }
686
+ const m=c.match(/(\d+)/g); return m?[+m[0],+m[1],+m[2]]:[0,0,0];
687
+ }
688
+ function mulberry32(seed) {
689
+ let t=seed>>>0;
690
+ return()=>{t+=0x6d2b79f5;let x=t;x=Math.imul(x^(x>>>15),x|1);x^=x+Math.imul(x^(x>>>7),x|61);return((x^(x>>>14))>>>0)/4294967296;};
691
+ }
692
+
693
+ function boot() {
694
+ if (document.getElementById("leaf-bg")) return true;
695
+ if (!document.body) return false;
696
+ const canvas = document.createElement("canvas");
697
+ canvas.id = "leaf-bg";
698
+ canvas.setAttribute("aria-hidden", "true");
699
+ document.body.insertBefore(canvas, document.body.firstChild);
700
+ const ctx = canvas.getContext("2d");
701
+
702
+ const C = {
703
+ leaves: LEAF_CASES, cycleSeconds: 16, trailCycles: 2.6, compress: 0.42,
704
+ maxTurns: 3, planeSpread: 1, eccentricity: 0.25, alignToCourse: 1,
705
+ driftSpeed: 1.5, driftDir: [272, 53], viewRadius: 3.1, tilt: 45, spin: 252, roll: 13.5,
706
+ lead: 0.12, focus: [0.74, 0.42], scrim: "left", scrimStrength: 0.90,
707
+ moteCount: 400, glow: 1, showCoreBeam: true, interactive: true, crossColor: "#2d7a12",
708
+ pulseColor: "#10b981", rayColor: "#8fd45a"
709
+ };
710
+ const PULSE_RGB = parseRGB(C.pulseColor), RAY_RGB = parseRGB(C.rayColor);
711
+ const reduceMotion = window.matchMedia && window.matchMedia('(prefers-reduced-motion: reduce)').matches;
712
+
713
+ function applyResponsive() {
714
+ const w = window.innerWidth, narrow = w <= 767;
715
+ if (narrow) { C.focus = [0.5, 0.62]; }
716
+ else {
717
+ // đặt chữ thập vào giữa khoảng trống bên phải khối đăng nhập (khối ~13vw + 27rem)
718
+ const panelRight = 0.13 * w + Math.min(432, 0.81 * w);
719
+ const fx = ((panelRight + w) / 2) / w;
720
+ C.focus = [Math.min(0.82, Math.max(0.62, fx)), 0.5];
721
+ }
722
+ C.scrim = narrow ? "top" : "left";
723
+ C.scrimStrength = narrow ? 0.90 : 0.90;
724
+ C.viewRadius = narrow ? 2.4 : 3.1;
725
+ C.lead = narrow ? 0.05 : 0.12;
726
+ C.glow = narrow ? 0.8 : 1;
727
+ }
728
+ applyResponsive();
729
+
730
+ let width=0, height=0, dpr=1, years=0, lastFrame=0, clock=0, pxPerAU=1, cx=0, cy=0, camDist=1;
731
+ const RIGHT={x:1,y:0,z:0}, UP={x:0,y:1,z:0}, FWD={x:0,y:0,z:1};
732
+
733
+ function setCamera(yaw,pitch,roll) {
734
+ const A=yaw*RAD, B=pitch*RAD, ca=Math.cos(A),sa=Math.sin(A),cb=Math.cos(B),sb=Math.sin(B);
735
+ FWD.x=sa*sb; FWD.y=-ca*sb; FWD.z=cb;
736
+ const rx=ca,ry=sa,rz=0,ux=-sa*cb,uy=ca*cb,uz=sb,Cr=roll*RAD, cr=Math.cos(Cr), sr=Math.sin(Cr);
737
+ RIGHT.x=rx*cr+ux*sr; RIGHT.y=ry*cr+uy*sr; RIGHT.z=rz*cr+uz*sr;
738
+ UP.x=-rx*sr+ux*cr; UP.y=-ry*sr+uy*cr; UP.z=-rz*sr+uz*cr;
739
+ }
740
+
741
+ const P={x:0,y:0,depth:0,s:0,ok:false};
742
+ function project(dx,dy,dz) {
743
+ const vx=dx*RIGHT.x+dy*RIGHT.y+dz*RIGHT.z, vy=dx*UP.x+dy*UP.y+dz*UP.z, vz=dx*FWD.x+dy*FWD.y+dz*FWD.z, depth=camDist-vz;
744
+ if(depth<0.6){P.ok=false;return;}
745
+ const s=camDist/depth;
746
+ P.x=cx+vx*pxPerAU*s; P.y=cy-vy*pxPerAU*s; P.depth=depth; P.s=s; P.ok=true;
747
+ }
748
+
749
+ const DIR={x:0,y:0,z:0};
750
+ function setDrift(lon,lat){const l=lon*RAD,b=lat*RAD;DIR.x=Math.cos(b)*Math.cos(l);DIR.y=Math.cos(b)*Math.sin(l);DIR.z=Math.sin(b);}
751
+
752
+ function elementsOf(p,idx,gamma,spread,ecc,align){
753
+ const aDraw=Math.pow(p.a,gamma), period=Math.pow(aDraw,1.5), fan=PLANE_FAN[idx%PLANE_FAN.length];
754
+ const inc=(p.i+spread*fan[0])*RAD, node=(p.node+spread*fan[1])*RAD, ci=Math.cos(inc),si=Math.sin(inc),cn=Math.cos(node),sn=Math.sin(node);
755
+ const target=ECC_FAN[idx%ECC_FAN.length];
756
+ return{p,rgb:parseRGB(p.color),e:Math.min(0.85,p.e+ecc*(target-p.e)),aDraw,period,n:TAU/period,cw:Math.cos(p.peri*RAD),sw:Math.sin(p.peri*RAD),ci,si,cn,sn,M0:p.M0*RAD,swing:align>0?swingToCourse(si*sn,-si*cn,ci,align):null};
757
+ }
758
+
759
+ function swingToCourse(nx,ny,nz,align){
760
+ const s=nx*DIR.x+ny*DIR.y+nz*DIR.z>=0?1:-1;
761
+ let tx=nx+align*(s*DIR.x-nx),ty=ny+align*(s*DIR.y-ny),tz=nz+align*(s*DIR.z-nz);
762
+ const tl=Math.hypot(tx,ty,tz); if(tl<1e-9)return null;
763
+ tx/=tl;ty/=tl;tz/=tl;
764
+ let ax=ny*tz-nz*ty,ay=nz*tx-nx*tz,az=nx*ty-ny*tx;
765
+ const al=Math.hypot(ax,ay,az); if(al<1e-9)return null;
766
+ ax/=al;ay/=al;az/=al;
767
+ const c=Math.max(-1,Math.min(1,nx*tx+ny*ty+nz*tz)),sA=al>1?1:al,k=1-c;
768
+ return[c+ax*ax*k,ax*ay*k-az*sA,ax*az*k+ay*sA,ay*ax*k+az*sA,c+ay*ay*k,ay*az*k-ax*sA,az*ax*k-ay*sA,az*ay*k+ax*sA,c+az*az*k];
769
+ }
770
+
771
+ const R3={x:0,y:0,z:0};
772
+ function helio(el,M,gamma){
773
+ const e=el.e,E=eccentricAnomaly(M,e),xo=el.p.a*(Math.cos(E)-e),yo=el.p.a*Math.sqrt(1-e*e)*Math.sin(E);
774
+ const x1=xo*el.cw-yo*el.sw,y1=xo*el.sw+yo*el.cw,y2=y1*el.ci,z2=y1*el.si;
775
+ let x=x1*el.cn-y2*el.sn,y=x1*el.sn+y2*el.cn,z=z2;
776
+ const S=el.swing;
777
+ if(S){const rx=S[0]*x+S[1]*y+S[2]*z,ry=S[3]*x+S[4]*y+S[5]*z,rz=S[6]*x+S[7]*y+S[8]*z;x=rx;y=ry;z=rz;}
778
+ if(gamma!==1){const r=Math.sqrt(x*x+y*y+z*z);if(r>1e-9){const s=Math.pow(r,gamma-1);x*=s;y*=s;z*=s;}}
779
+ R3.x=x;R3.y=y;R3.z=z;
780
+ }
781
+
782
+ let elems=[];
783
+ function syncElements(){elems=C.leaves.map((p,i)=>elementsOf(p,i,C.compress,C.planeSpread,C.eccentricity,C.alignToCourse));}
784
+
785
+ let D_NEAR=60,D_FAR=1400,sx=[],sy=[],sz=[],sMag=[],sPhase=[],sTint=[],starN=0,rand=mulberry32(0xc0ffee), dist=0;
786
+ function seedStar(k,depth,edge){
787
+ const d=depth??D_NEAR*Math.pow(D_FAR/D_NEAR,Math.pow(rand(),0.55)),halfW=width*0.5*1.15,halfH=height*0.5*1.15;
788
+ let ox,oy;
789
+ if(edge===0){ox=-halfW;oy=(rand()*2-1)*halfH;}
790
+ else if(edge===1){ox=halfW;oy=(rand()*2-1)*halfH;}
791
+ else if(edge===2){ox=(rand()*2-1)*halfW;oy=-halfH;}
792
+ else if(edge===3){ox=(rand()*2-1)*halfW;oy=halfH;}
793
+ else{ox=(rand()*2-1)*halfW;oy=(rand()*2-1)*halfH;}
794
+ const scale=d/(camDist*pxPerAU),vx=ox*scale,vy=-oy*scale,vz=camDist-d;
795
+ sx[k]=vx*RIGHT.x+vy*UP.x+vz*FWD.x+DIR.x*dist; sy[k]=vx*RIGHT.y+vy*UP.y+vz*FWD.y+DIR.y*dist; sz[k]=vx*RIGHT.z+vy*UP.z+vz*FWD.z+DIR.z*dist;
796
+ sMag[k]=Math.pow(rand(),2.4);sPhase[k]=rand()*TAU; const t=rand();sTint[k]=t>0.85?1:t<0.1?2:0;
797
+ }
798
+ function buildStars(){
799
+ starN=Math.max(40,Math.round(C.moteCount*Math.min(2,(width*height)/(1440*900))));
800
+ sx=new Array(starN);sy=new Array(starN);sz=new Array(starN);sMag=new Array(starN);sPhase=new Array(starN);sTint=new Array(starN);
801
+ rand=mulberry32(0xc0ffee); for(let k=0;k<starN;k++)seedStar(k);
802
+ }
803
+
804
+ const BORDER_LEAVES = [], LEAF_GREENS = ["#2e7d32","#388e3c","#43a047","#66bb6a","#81c784","#4caf50","#1b5e20","#558b2f"];
805
+ function initBorderLeaves() {
806
+ BORDER_LEAVES.length = 0; const n = Math.max(8, Math.round(16 * Math.min(1.5, width / 1200))), rng = mulberry32(42);
807
+ for (let i = 0; i < n; i++) {
808
+ const edge = i % 4; let x, y;
809
+ if (edge === 0) { x = rng() * width * 0.25; y = rng() * height; }
810
+ else if (edge === 1) { x = width - rng() * width * 0.25; y = rng() * height; }
811
+ else if (edge === 2) { x = rng() * width; y = rng() * height * 0.2; }
812
+ else { x = rng() * width; y = height - rng() * height * 0.2; }
813
+ BORDER_LEAVES.push({ x, y, size: 40 + rng() * 60, baseAngle: (rng() - 0.5) * 1.2, swaySpeed: 0.4 + rng() * 0.8, swayAmp: 0.06 + rng() * 0.1, phase: rng() * TAU, color: LEAF_GREENS[Math.floor(rng() * LEAF_GREENS.length)], alpha: 0.35 + rng() * 0.45 });
814
+ }
815
+ }
816
+ function drawBorderLeaves(t) {
817
+ for (const lf of BORDER_LEAVES) {
818
+ const angle = lf.baseAngle + Math.sin(t * lf.swaySpeed + lf.phase) * lf.swayAmp, [r,g,b] = parseRGB(lf.color), sz = lf.size;
819
+ ctx.save(); ctx.translate(lf.x, lf.y); ctx.rotate(angle); ctx.globalAlpha = lf.alpha;
820
+ ctx.beginPath(); ctx.moveTo(0, -sz * 0.55); ctx.quadraticCurveTo(sz * 0.35, -sz * 0.05, 0, sz * 0.5); ctx.quadraticCurveTo(-sz * 0.35, -sz * 0.05, 0, -sz * 0.55); ctx.closePath();
821
+ ctx.fillStyle = `rgb(${r},${g},${b})`; ctx.fill(); ctx.strokeStyle = `rgba(255,255,255,0.3)`; ctx.lineWidth = Math.max(0.8, sz * 0.02); ctx.beginPath(); ctx.moveTo(0, -sz * 0.45); ctx.lineTo(0, sz * 0.42); ctx.stroke();
822
+ ctx.globalAlpha = 1; ctx.restore();
823
+ }
824
+ }
825
+
826
+ const glowCache = new Map();
827
+ function glowSprite(color) {
828
+ const hit = glowCache.get(color); if (hit) return hit;
829
+ const R = 64, c = document.createElement("canvas"); c.width = c.height = R * 2;
830
+ const g2 = c.getContext("2d"), [r, g, b] = parseRGB(color), grad = g2.createRadialGradient(R, R, 0, R, R, R);
831
+ grad.addColorStop(0, `rgba(${r},${g},${b},0.7)`); grad.addColorStop(0.2, `rgba(${r},${g},${b},0.4)`); grad.addColorStop(0.5, `rgba(${r},${g},${b},0.1)`); grad.addColorStop(1, `rgba(${r},${g},${b},0)`);
832
+ g2.fillStyle = grad; g2.fillRect(0, 0, R * 2, R * 2); glowCache.set(color, c); return c;
833
+ }
834
+
835
+ function resize() {
836
+ const w = window.innerWidth, h = window.innerHeight; dpr = Math.min(window.devicePixelRatio || 1, 2);
837
+ width = w; height = h; canvas.width = Math.round(w * dpr); canvas.height = Math.round(h * dpr);
838
+ ctx.setTransform(dpr, 0, 0, dpr, 0, 0); layout(); buildStars(); initBorderLeaves();
839
+ }
840
+ function layout() {
841
+ pxPerAU = (Math.min(width, height) * 0.5) / C.viewRadius; camDist = C.viewRadius * 3.1;
842
+ D_NEAR = camDist * 5; D_FAR = camDist * 120; setDrift(C.driftDir[0], C.driftDir[1]); setCamera(C.spin, C.tilt, C.roll);
843
+ }
844
+
845
+ let pointerX = 0, pointerY = 0, camX = 0, camY = 0;
846
+ window.addEventListener("pointermove", (ev) => {
847
+ if (!C.interactive) return;
848
+ pointerX = (ev.clientX / window.innerWidth - 0.5) * 2; pointerY = (ev.clientY / window.innerHeight - 0.5) * 2;
849
+ });
850
+
851
+ // Nhịp tim lub-dub (0..1)
852
+ function heartEnv(clk) {
853
+ if (reduceMotion) return 0.15;
854
+ const T = 1.05, p = (clk % T) / T, g = (x, mu, s) => Math.exp(-((x - mu) * (x - mu)) / (2 * s * s));
855
+ return Math.min(1, g(p, 0.07, 0.045) + 0.58 * g(p, 0.27, 0.05));
856
+ }
857
+ function crossBaseR() { return Math.max(30, Math.min(width, height) * 0.066); }
858
+
859
+ // Tia sáng xoay quanh chữ thập
860
+ function drawRays(clk, env) {
861
+ const baseR = crossBaseR(), [r, g, b] = RAY_RGB;
862
+ ctx.save(); ctx.globalCompositeOperation = "lighter"; ctx.translate(cx, cy); ctx.rotate(reduceMotion ? 0 : clk * 0.12);
863
+ const n = 16, inner = baseR * 1.1;
864
+ for (let i = 0; i < n; i++) {
865
+ const ang = (i / n) * TAU, long = (i % 2 === 0), outer = baseR * (long ? 3.9 : 2.5), w = long ? 0.022 : 0.013, a = (long ? 0.11 : 0.07) + env * 0.06;
866
+ const ci = Math.cos(ang), si = Math.sin(ang), cwp = Math.cos(ang - w), swp = Math.sin(ang - w), cwn = Math.cos(ang + w), swn = Math.sin(ang + w);
867
+ const grad = ctx.createLinearGradient(ci * inner, si * inner, ci * outer, si * outer);
868
+ grad.addColorStop(0, `rgba(${r},${g},${b},${a})`); grad.addColorStop(1, `rgba(${r},${g},${b},0)`);
869
+ ctx.fillStyle = grad; ctx.beginPath();
870
+ ctx.moveTo(cwp * inner, swp * inner); ctx.lineTo(cwp * outer, swp * outer); ctx.lineTo(cwn * outer, swn * outer); ctx.lineTo(cwn * inner, swn * inner); ctx.closePath(); ctx.fill();
871
+ }
872
+ ctx.restore();
873
+ }
874
+
875
+ // Nhịp tim: hào quang + vòng lan tỏa (xanh ngọc)
876
+ function drawHeartbeat(clk, env) {
877
+ const baseR = crossBaseR(), [r, g, b] = PULSE_RGB;
878
+ ctx.save(); ctx.globalCompositeOperation = "lighter";
879
+ const glowR = baseR * (1.5 + env * 1.4), grad = ctx.createRadialGradient(cx, cy, baseR * 0.5, cx, cy, glowR);
880
+ grad.addColorStop(0, `rgba(${r},${g},${b},0)`); grad.addColorStop(0.62, `rgba(${r},${g},${b},${(0.30 * env).toFixed(3)})`); grad.addColorStop(1, `rgba(${r},${g},${b},0)`);
881
+ ctx.fillStyle = grad; ctx.beginPath(); ctx.arc(cx, cy, glowR, 0, TAU); ctx.fill();
882
+ if (!reduceMotion) {
883
+ const T = 1.05, p = (clk % T) / T;
884
+ [[0.07, 1], [0.27, 0.7]].forEach(function (mw) {
885
+ const mu = mw[0], w = mw[1]; let age = p - mu; if (age < 0) age += 1;
886
+ if (age < 0.55) {
887
+ const prog = age / 0.55, R = baseR * (1.0 + prog * 2.4), alpha = (1 - prog) * 0.35 * w;
888
+ ctx.strokeStyle = `rgba(${r},${g},${b},${alpha.toFixed(3)})`; ctx.lineWidth = (1 - prog) * 2.6 + 0.5;
889
+ ctx.beginPath(); ctx.arc(cx, cy, R, 0, TAU); ctx.stroke();
890
+ }
891
+ });
892
+ }
893
+ ctx.restore();
894
+ }
895
+
896
+ function drawCross(k, env) {
897
+ const [r, g, b] = parseRGB(C.crossColor), scale = 1 + env * 0.06, baseR = crossBaseR() * scale;
898
+ const haze = ctx.createRadialGradient(cx, cy, 0, cx, cy, baseR * 4);
899
+ haze.addColorStop(0, `rgba(${r},${g},${b},${0.12 * k})`); haze.addColorStop(0.4, `rgba(${r},${g},${b},${0.04 * k})`); haze.addColorStop(1, `rgba(${r},${g},${b},0)`);
900
+ ctx.fillStyle = haze; ctx.beginPath(); ctx.arc(cx, cy, baseR * 4, 0, TAU); ctx.fill();
901
+ const mid = ctx.createRadialGradient(cx, cy, 0, cx, cy, baseR * 2);
902
+ mid.addColorStop(0, `rgba(${r},${g},${b},${0.25 * k})`); mid.addColorStop(0.5, `rgba(${r},${g},${b},${0.08 * k})`); mid.addColorStop(1, `rgba(${r},${g},${b},0)`);
903
+ ctx.fillStyle = mid; ctx.beginPath(); ctx.arc(cx, cy, baseR * 2, 0, TAU); ctx.fill();
904
+ const badgeR = baseR * 0.95, badge = ctx.createRadialGradient(cx, cy - badgeR * 0.15, badgeR * 0.2, cx, cy, badgeR);
905
+ badge.addColorStop(0, "#8fcb5e"); badge.addColorStop(1, "#3c8b22");
906
+ ctx.fillStyle = badge; ctx.beginPath(); ctx.arc(cx, cy, badgeR, 0, TAU); ctx.fill();
907
+ const [pr, pg, pb] = PULSE_RGB;
908
+ ctx.strokeStyle = `rgba(${pr},${pg},${pb},${(0.35 + env * 0.5).toFixed(3)})`; ctx.lineWidth = 2 + env * 1.5;
909
+ ctx.beginPath(); ctx.arc(cx, cy, badgeR + 3, 0, TAU); ctx.stroke();
910
+ const armLen = badgeR * 0.6, armW = badgeR * 0.22, rad = armW * 0.45;
911
+ ctx.fillStyle = "#fff";
912
+ ctx.beginPath(); ctx.roundRect(cx - armW, cy - armLen, armW * 2, armLen * 2, rad); ctx.fill();
913
+ ctx.beginPath(); ctx.roundRect(cx - armLen, cy - armW, armLen * 2, armW * 2, rad); ctx.fill();
914
+ }
915
+
916
+ function drawLeafHead(x, y, size, color, angle, bright) {
917
+ const [r, g, b] = parseRGB(color); ctx.save(); ctx.translate(x, y); ctx.rotate(angle);
918
+ const L = size * 2.8, W = size * 1.6;
919
+ ctx.beginPath(); ctx.moveTo(0, -L * 0.5); ctx.quadraticCurveTo(W * 0.5, -L * 0.06, 0, L * 0.48); ctx.quadraticCurveTo(-W * 0.5, -L * 0.06, 0, -L * 0.5); ctx.closePath();
920
+ ctx.fillStyle = `rgba(${r},${g},${b},${Math.min(1, 0.9 * bright).toFixed(3)})`; ctx.fill();
921
+ ctx.strokeStyle = `rgba(255,255,255,${Math.min(1, 0.4 * bright).toFixed(3)})`; ctx.lineWidth = Math.max(0.6, size * 0.08);
922
+ ctx.beginPath(); ctx.moveTo(0, -size * 1.3); ctx.lineTo(0, size * 1.15); ctx.stroke(); ctx.restore();
923
+ }
924
+
925
+ function render(t) {
926
+ const k = C.glow, env = heartEnv(clock); layout(); syncElements();
927
+ camX += (pointerX - camX) * 0.04; camY += (pointerY - camY) * 0.04;
928
+ setCamera(C.spin + camX * 7, C.tilt + camY * 5, C.roll);
929
+ dist = C.driftSpeed * t; cx = width * C.focus[0]; cy = height * C.focus[1];
930
+ project(DIR.x, DIR.y, DIR.z);
931
+ if (P.ok) {
932
+ const dxs = P.x - cx, dys = P.y - cy, len = Math.hypot(dxs, dys) || 1, push = Math.min(width, height) * C.lead;
933
+ cx += (dxs / len) * push; cy += (dys / len) * push;
934
+ }
935
+ ctx.globalCompositeOperation = "source-over";
936
+ const bg = ctx.createRadialGradient(cx, cy, 0, cx, cy, Math.max(width, height) * 0.85);
937
+ bg.addColorStop(0, "#f0fae0"); bg.addColorStop(0.4, "#e4f4cc"); bg.addColorStop(1, "#d2eaab");
938
+ ctx.fillStyle = bg; ctx.fillRect(0, 0, width, height);
939
+ // Vòng orbit nét đứt trang trí
940
+ ctx.setLineDash([3, 14]);
941
+ ctx.strokeStyle = "rgba(120,160,80,0.20)";
942
+ ctx.lineWidth = 1.5;
943
+ [160, 110].forEach(r => {
944
+ const R = Math.min(width, height) * r / 417;
945
+ ctx.beginPath(); ctx.arc(cx, cy, R, 0, TAU); ctx.stroke();
946
+ });
947
+ ctx.setLineDash([]);
948
+ const dRef = D_NEAR * 3.3, left = -width * 0.12, right = width * 1.12, top = -height * 0.12, bottom = height * 1.12;
949
+ for (let s = 0; s < starN; s++) {
950
+ project(sx[s] - DIR.x * dist, sy[s] - DIR.y * dist, sz[s] - DIR.z * dist);
951
+ if (!P.ok || P.depth > D_FAR * 1.25) { seedStar(s); continue; }
952
+ if (P.x < left || P.x > right || P.y < top || P.y > bottom) { seedStar(s, undefined, P.x < left ? 1 : P.x > right ? 0 : P.y < top ? 3 : 2); continue; }
953
+ if (P.depth < D_NEAR * 0.75) continue;
954
+ const near = Math.min(1, (P.depth - D_NEAR * 0.75) / (D_NEAR * 0.6));
955
+ const far = 1 - Math.max(0, (P.depth - D_FAR * 0.78) / (D_FAR * 0.32));
956
+ let a = (0.4 + sMag[s] * 1.5) * Math.pow(dRef / P.depth, 0.8) * near * far;
957
+ if (a <= 0.01) continue;
958
+ a *= 0.85 + 0.15 * Math.sin(t * 7 + sPhase[s]);
959
+ const col = sTint[s] === 1 ? "211,84,0" : sTint[s] === 2 ? "20,90,20" : "39,174,96";
960
+ const size = Math.min(5.5, 1.5 + sMag[s] * 4.0 * Math.pow(dRef / P.depth, 0.5));
961
+ ctx.globalAlpha = Math.min(0.95, a);
962
+ ctx.fillStyle = `rgb(${col})`;
963
+ if (size < 1.2) ctx.fillRect(P.x, P.y, size, size);
964
+ else { ctx.beginPath(); ctx.arc(P.x, P.y, size * 0.5, 0, TAU); ctx.fill(); }
965
+ ctx.globalAlpha = 1;
966
+ }
967
+
968
+ const shots = [];
969
+ for (const el of elems) {
970
+ const [r, g, b] = el.rgb, bright = (el.p.glow ?? 1) * k, span = Math.min(C.trailCycles, C.maxTurns * el.period), turns = span / el.period;
971
+ const N = Math.max(48, Math.min(360, Math.ceil(turns * 46 * (1 + 2.2 * el.e)) + 48));
972
+ const xs = new Float64Array(N + 1), ys = new Float64Array(N + 1), okArr = new Uint8Array(N + 1);
973
+ for (let q = 0; q <= N; q++) {
974
+ const age = (1 - q / N) * span;
975
+ helio(el, el.M0 + el.n * (t - age), C.compress);
976
+ const back = C.driftSpeed * age;
977
+ project(R3.x - DIR.x * back, R3.y - DIR.y * back, R3.z - DIR.z * back);
978
+ xs[q] = P.x; ys[q] = P.y; okArr[q] = P.ok ? 1 : 0;
979
+ if (q === N && P.ok) shots.push({ el, x: P.x, y: P.y, depth: P.depth, s: P.s });
980
+ }
981
+ ctx.lineCap = "round"; ctx.lineJoin = "round"; ctx.strokeStyle = `rgba(${r},${g},${b},${(0.06 * bright).toFixed(3)})`; ctx.lineWidth = 5;
982
+ ctx.beginPath(); let started = false;
983
+ for (let q = Math.floor(N * 0.7); q <= N; q++) {
984
+ if (!okArr[q]) { started = false; continue; }
985
+ if (!started) { ctx.moveTo(xs[q], ys[q]); started = true; } else ctx.lineTo(xs[q], ys[q]);
986
+ }
987
+ ctx.stroke();
988
+ ctx.lineCap = "butt"; ctx.lineWidth = 1.5;
989
+ for (let q = 0; q < N; q++) {
990
+ if (!okArr[q] || !okArr[q + 1]) continue;
991
+ const f = (q + 1) / N, a = Math.pow(f, 2.4) * 0.7 * bright;
992
+ if (a < 0.005) continue;
993
+ ctx.strokeStyle = `rgba(${r},${g},${b},${a.toFixed(3)})`; ctx.beginPath(); ctx.moveTo(xs[q], ys[q]); ctx.lineTo(xs[q + 1], ys[q + 1]); ctx.stroke();
994
+ }
995
+ }
996
+
997
+ shots.sort((a, b) => b.depth - a.depth);
998
+ const sizeScale = Math.min(width, height) / 500;
999
+ const drawShot = (o, index) => {
1000
+ const depth = Math.min(1.3, Math.max(0.5, o.s)), size = o.el.p.size * depth * sizeScale, bright = (o.el.p.glow ?? 1) * k, R = size * 3;
1001
+ ctx.globalAlpha = Math.min(0.7, 0.5 * bright); ctx.drawImage(glowSprite(o.el.p.color), o.x - R, o.y - R, R * 2, R * 2); ctx.globalAlpha = 1;
1002
+ const angle = Math.sin(t * 1.3 + index * 1.7) * 0.18; drawLeafHead(o.x, o.y, size, o.el.p.color, angle, bright);
1003
+ };
1004
+
1005
+ let idx = 0;
1006
+ while (idx < shots.length && shots[idx].depth > camDist) drawShot(shots[idx], idx++);
1007
+ drawRays(clock, env);
1008
+ drawHeartbeat(clock, env);
1009
+ drawCross(k, env);
1010
+ while (idx < shots.length) drawShot(shots[idx], idx++);
1011
+ drawBorderLeaves(t);
1012
+
1013
+ if (C.scrim !== "none") {
1014
+ const s = Math.max(0, Math.min(1, C.scrimStrength));
1015
+ const g = C.scrim === "left" ? ctx.createLinearGradient(0, 0, width, 0) : ctx.createLinearGradient(0, 0, 0, height);
1016
+ for (let q = 0; q <= 12; q++) { const x = q / 12; g.addColorStop(x, `rgba(232,245,214,${(s * Math.pow(1 - x, 2.2)).toFixed(4)})`); }
1017
+ ctx.fillStyle = g; ctx.fillRect(0, 0, width, height);
1018
+ }
1019
+ }
1020
+
1021
+ let active = true;
1022
+ function tick(now) {
1023
+ requestAnimationFrame(tick);
1024
+ if (!active || document.hidden) { lastFrame = now; return; }
1025
+ const dt = lastFrame ? Math.min(0.05, (now - lastFrame) / 1000) : 0;
1026
+ lastFrame = now;
1027
+ clock = now / 1000;
1028
+ years += dt / Math.max(0.1, C.cycleSeconds);
1029
+ render(years);
1030
+ }
1031
+
1032
+ resize(); render(years); requestAnimationFrame(tick);
1033
+ window.addEventListener("resize", () => { applyResponsive(); resize(); });
1034
+
1035
+ setInterval(function () {
1036
+ const el = document.getElementById("login_wrapper");
1037
+ const onLogin = !!el && el.offsetParent !== null;
1038
+ document.body.classList.toggle("leaf-on", onLogin);
1039
+ active = onLogin;
1040
+ if (!onLogin) lastFrame = 0;
1041
+ }, 250);
1042
+
1043
+ return true;
1044
+ }
1045
+
1046
+ let tries = 0;
1047
+ const timer = setInterval(function () {
1048
+ if (boot() || ++tries > 120) clearInterval(timer);
1049
+ }, 100);
1050
+ })();
1051
+ </script>
1052
+ """
1053
+
1054
  custom_css = """
1055
  .gradio-container {
1056
  background: linear-gradient(180deg, #eff5f3 0%, #e1ebe7 100%) !important;
 
1204
  0%, 100% { transform: scale(1); filter: drop-shadow(0 2px 5px rgba(0,129,112,0.2)); }
1205
  50% { transform: scale(1.04); filter: drop-shadow(0 4px 12px rgba(0,191,165,0.4)); }
1206
  }
1207
+ /* ===================== LEAF HERO LOGIN (v4) ===================== */
1208
+ #leaf-bg {
1209
+ position: fixed; inset: 0; width: 100vw; height: 100vh; z-index: 0;
1210
+ opacity: 0; pointer-events: none; transition: opacity 0.6s ease;
1211
+ }
1212
+ body.leaf-on #leaf-bg { opacity: 1; }
1213
+ body.leaf-on { background: #e8f5d6 !important; margin: 0; padding: 0;
1214
+ font-family: "Be Vietnam Pro", -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif; }
1215
+ body.leaf-on .gradio-container { background: transparent !important; font-family: "Be Vietnam Pro", -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif !important; }
1216
+ #login_wrapper h1, #login_wrapper p, #login_wrapper label, #login_wrapper label span, #login_wrapper input, #login_wrapper textarea, #login_wrapper button { font-family: "Be Vietnam Pro", -apple-system, "Segoe UI", Roboto, Helvetica, Arial, sans-serif !important; }
1217
+
1218
+ #login_wrapper {
1219
+ position: relative; z-index: 2; min-height: 100vh;
1220
+ display: flex !important; align-items: center !important; justify-content: flex-start !important;
1221
+ padding: 0 6vw 0 13vw !important; box-sizing: border-box;
1222
+ }
1223
+ #login_wrapper.hide { display: none !important; }
1224
+
1225
+ /* Bỏ hẳn panel kính — chữ & khối nằm trực tiếp trên nền (như leaf v4) */
1226
+ #login_content_col {
1227
+ max-width: 27rem !important; width: 100% !important; margin: 0 !important;
1228
+ background: transparent !important; padding: 0 !important;
1229
+ border: none !important; box-shadow: none !important;
1230
+ -webkit-backdrop-filter: none !important; backdrop-filter: none !important;
1231
+ }
1232
+ @media (max-width: 767px) {
1233
+ #login_wrapper { padding: 12vh 1.5rem 0 !important; align-items: flex-start !important; }
1234
+ #login_content_col { max-width: 100% !important; }
1235
+ }
1236
+
1237
+ /* Brand head: badge (nhịp tim) + tên trung tâm nổi bật */
1238
+ .lv4-head { display: flex; align-items: flex-start; gap: .85rem; margin-bottom: .3rem; }
1239
+ .lv4-badge { width: 44px; height: 44px; flex: 0 0 44px; border-radius: 14px; margin-top: 3px;
1240
+ display: grid; place-items: center;
1241
+ background: radial-gradient(circle at 35% 30%, #8fcb5e, #2f8b1c);
1242
+ box-shadow: 0 8px 20px -8px rgba(45,122,18,0.7), inset 0 1px 0 rgba(255,255,255,0.5); }
1243
+ .lv4-badge svg { width: 23px; height: 23px; }
1244
+ .lv4-heart { transform-origin: center; animation: lv4-beat 1.05s ease-in-out infinite; }
1245
+ .lv4-copy { min-width: 0; }
1246
+ .lv4-title { margin: 0; font-weight: 800; line-height: 1.12; letter-spacing: -.02em;
1247
+ font-size: clamp(1.5rem, 2.6vw, 2.1rem); color: #16281a; }
1248
+ .lv4-accent { background: linear-gradient(120deg, #34d399, #059669 55%, #2d7a12);
1249
+ -webkit-background-clip: text; background-clip: text; -webkit-text-fill-color: transparent; color: transparent; }
1250
+ .lv4-sub { margin: .6rem 0 .2rem; font-size: .92rem; color: #3d5233; }
1251
+ @keyframes lv4-beat { 0%,100%{transform:scale(1);} 6%{transform:scale(1.22);} 12%{transform:scale(1);} 26%{transform:scale(1.13);} 34%{transform:scale(1);} }
1252
+ @media (prefers-reduced-motion: reduce){ .lv4-heart{ animation: none; } }
1253
+
1254
+ /* Ô nhập kiểu leaf v4 */
1255
+ #login_wrapper .gr-form, #login_wrapper .block, #login_wrapper .form { background: transparent !important; border: none !important; box-shadow: none !important; }
1256
+ #login_wrapper label span, #login_wrapper .gr-box label { color: #3d5233 !important; font-size: 13px !important; font-weight: 600 !important; letter-spacing: .01em !important; }
1257
+ #login_wrapper input[type="text"], #login_wrapper input[type="password"] {
1258
+ background: rgba(255,255,255,0.55) !important; border: 1px solid rgba(90,110,70,0.32) !important;
1259
+ border-radius: 14px !important; color: #16281a !important; font-size: 15px !important; min-height: 50px !important;
1260
+ padding: 0 16px !important; -webkit-backdrop-filter: blur(6px) !important; backdrop-filter: blur(6px) !important;
1261
+ transition: all 0.2s ease !important;
1262
+ }
1263
+ #login_wrapper input::placeholder { color: #5f7255 !important; }
1264
+ #login_wrapper input:hover { border-color: rgba(16,185,129,0.45) !important; }
1265
+ #login_wrapper input:focus, #login_wrapper input:focus-visible {
1266
+ border-color: #10b981 !important; background: rgba(255,255,255,0.9) !important; outline: none !important;
1267
+ box-shadow: 0 0 0 4px rgba(16,185,129,0.35) !important;
1268
+ }
1269
+
1270
+ /* Nút lớn KÍCH HOẠT PHIÊN XÉT NGHIỆM */
1271
+ #login_wrapper .scan-btn {
1272
+ border-radius: 14px !important; min-height: 56px !important; margin-top: 8px !important; width: 100% !important;
1273
+ font-size: 15px !important; font-weight: 800 !important; letter-spacing: .08em !important; text-transform: uppercase !important;
1274
+ color: #fff !important; border: none !important; cursor: pointer;
1275
+ background: linear-gradient(135deg, #10b981, #047857) !important;
1276
+ box-shadow: 0 16px 34px -14px rgba(5,150,105,0.75), inset 0 1px 0 rgba(255,255,255,0.25) !important;
1277
+ transition: all 0.2s ease !important;
1278
+ }
1279
+ #login_wrapper .scan-btn:hover { transform: translateY(-1px); filter: brightness(1.05); box-shadow: 0 20px 40px -14px rgba(5,150,105,0.85) !important; }
1280
+ #login_wrapper .scan-btn:active { transform: translateY(0); }
1281
+
1282
+ /* Tài khoản thử nghiệm */
1283
+ .lv4-demo { margin-top: 1.4rem; font-size: .8rem; color: #5f7255; }
1284
+ .lv4-demo-title { display: block; margin-bottom: .5rem; font-weight: 600; color: #3d5233; }
1285
+ .lv4-demo-row { display: flex; align-items: center; gap: .5rem; }
1286
+ .lv4-chip { font-family: ui-monospace, "SF Mono", Menlo, Consolas, monospace !important; font-size: .78rem; font-weight: 600;
1287
+ color: #047857; background: rgba(16,185,129,0.14); border: 1px solid rgba(16,185,129,0.28);
1288
+ padding: .22rem .6rem; border-radius: .5rem; }
1289
+
1290
+ .sp-error {
1291
+ margin-top: 12px; padding: 12px 16px; border-radius: 12px; background: rgba(231,76,60,0.1);
1292
+ border: 1px solid rgba(231,76,60,0.4); color: #c0392b; font-size: 13.5px; font-weight: 600;
1293
+ display: flex; align-items: center; gap: 10px;
1294
+ }
1295
+ /* =================== END LEAF HERO LOGIN =================== */
1296
  """
1297
 
1298
  initial_ticket_text, initial_ticket_img = check_user_expert_status()
1299
 
1300
+ with gr.Blocks(title="Bệnh Viện Cây Trồng - Hệ Thống Chẩn Đoán AI", css=custom_css, head=LEAF_HERO_HEAD) as demo:
1301
  with gr.Row(visible=True, elem_id="login_wrapper") as login_panel:
1302
+ with gr.Column(elem_id="login_content_col"):
1303
+ gr.HTML("""
1304
+ <div class="lv4-head">
1305
+ <div class="lv4-badge" aria-hidden="true">
1306
+ <svg class="lv4-heart" viewBox="0 0 24 24" fill="none"><path d="M12 5v14M5 12h14" stroke="#fff" stroke-width="3.4" stroke-linecap="round"/></svg>
1307
+ </div>
1308
+ <div class="lv4-copy">
1309
+ <h1 class="lv4-title">Trung tâm Chẩn đoán &amp; Điều trị Bệnh <span class="lv4-accent">Cây trồng</span> Công nghệ cao</h1>
1310
+ <p class="lv4-sub">Xác thực để mở phiên xét nghiệm mô lá.</p>
1311
+ </div>
1312
+ </div>
1313
+ """)
1314
+ username_input = gr.Textbox(label="Tên đăng nhập", placeholder="Nhập tên đăng nhập", max_lines=1)
1315
+ password_input = gr.Textbox(label="Mật khẩu", type="password", placeholder="••••••••", max_lines=1)
1316
+ error_output = gr.HTML("")
1317
+ btn_login = gr.Button("Kích hoạt phiên xét nghiệm", variant="primary", elem_classes="scan-btn")
1318
  gr.HTML("""
1319
+ <div class="lv4-demo">
1320
+ <span class="lv4-demo-title">Tài khoản thử nghiệm</span>
1321
+ <div class="lv4-demo-row"><span>Nhà nông:</span> <span class="lv4-chip">user / 456</span></div>
 
1322
  </div>
 
 
1323
  """)
1324
+
 
 
 
 
1325
 
1326
  with gr.Column(visible=False) as main_panel:
1327
  with gr.Row(elem_classes="hospital-header"):