(function(){const c=document.createElement("link").relList;if(c&&c.supports&&c.supports("modulepreload"))return;for(const s of document.querySelectorAll('link[rel="modulepreload"]'))t(s);new MutationObserver(s=>{for(const o of s)if(o.type==="childList")for(const a of o.addedNodes)a.tagName==="LINK"&&a.rel==="modulepreload"&&t(a)}).observe(document,{childList:!0,subtree:!0});function r(s){const o={};return s.integrity&&(o.integrity=s.integrity),s.referrerPolicy&&(o.referrerPolicy=s.referrerPolicy),s.crossOrigin==="use-credentials"?o.credentials="include":s.crossOrigin==="anonymous"?o.credentials="omit":o.credentials="same-origin",o}function t(s){if(s.ep)return;s.ep=!0;const o=r(s);fetch(s.href,o)}})();function ot(e){const c=new URLSearchParams(location.search).get("host")??"*",r=s=>window.parent.postMessage({m3:1,...s},c);let t=0;return window.addEventListener("message",s=>{var a,d,u;if(s.source!==window.parent||c!=="*"&&s.origin!==c)return;const o=s.data;if(!(!o||o.m3!==1))switch(o.type){case"init":e.onInit(o),r({type:"ready"});break;case"state":e.onState(o.env);break;case"ack":(a=e.onAck)==null||a.call(e,o);break;case"view_changed":(d=e.onViewChanged)==null||d.call(e,o.view);break;case"cmd_failed":(u=e.onCmdFailed)==null||u.call(e,o.req_id,o.code);break}}),r({type:"loaded"}),{cmd(s,o={}){const a=++t;return r({type:"cmd",cmd_type:s,payload:o,req_id:a}),a},toast(s,o,a="info"){r({type:"toast",text_fa:s,text_en:o,kind:a})}}}const E=` `,P=` `;function rt(e){const c=e?E:P,r=e?"#0f172a":"#020617",t=e?"url(#cp-w-body)":"url(#cp-b-body)",s=e?"url(#cp-w-spec)":"url(#cp-b-spec)",o=e?"url(#cp-w-gold)":"url(#cp-b-gold)";return` ${c} `}function st(e){const c=e?E:P,r=e?"#0f172a":"#020617",t=e?"url(#cp-w-body)":"url(#cp-b-body)",s=e?"url(#cp-w-spec)":"url(#cp-b-spec)",o=e?"url(#cp-w-gold)":"url(#cp-b-gold)",a=e?"url(#cp-ruby)":"url(#cp-sapphire)";return` ${c} `}function ct(e){const c=e?E:P,r=e?"#0f172a":"#020617",t=e?"url(#cp-w-body)":"url(#cp-b-body)",s=e?"url(#cp-w-spec)":"url(#cp-b-spec)",o=e?"url(#cp-w-gold)":"url(#cp-b-gold)";return` ${c} `}function at(e){const c=e?E:P,r=e?"#0f172a":"#020617",t=e?"url(#cp-w-body)":"url(#cp-b-body)",s=e?"url(#cp-w-spec)":"url(#cp-b-spec)",o=e?"url(#cp-w-gold)":"url(#cp-b-gold)";return` ${c} `}function nt(e){const c=e?E:P,r=e?"#0f172a":"#020617",t=e?"url(#cp-w-body)":"url(#cp-b-body)",s=e?"url(#cp-w-spec)":"url(#cp-b-spec)",o=e?"url(#cp-w-gold)":"url(#cp-b-gold)",a=e?"url(#cp-ruby)":"url(#cp-sapphire)",d=e?"url(#cp-pearl)":"url(#cp-amethyst)";return` ${c} `}function lt(e){const c=e?E:P,r=e?"#0f172a":"#020617",t=e?"url(#cp-w-body)":"url(#cp-b-body)",s=e?"url(#cp-w-spec)":"url(#cp-b-spec)",o=e?"url(#cp-w-gold)":"url(#cp-b-gold)";return` ${c} `}const it={p:rt,n:st,b:ct,r:at,q:nt,k:lt},T={};function _(e,c){if(!e||!c)return"";const r=`${e}-${c}`;if(!T[r]){const t=it[e];t&&(T[r]=t(c==="w"))}return T[r]??""}class dt{constructor(c){this.particles=[],this.shockwaves=[],this.animId=null,this.lastTime=0,this.canvas=c,this.ctx=c.getContext("2d"),this.resize(),window.addEventListener("resize",()=>this.resize()),this.startLoop()}resize(){var t,s,o;const c=(t=this.canvas.parentElement)==null?void 0:t.getBoundingClientRect();if(!c)return;const r=window.devicePixelRatio||1;this.canvas.width=Math.round(c.width*r),this.canvas.height=Math.round(c.height*r),this.canvas.style.width=`${c.width}px`,this.canvas.style.height=`${c.height}px`,(o=(s=this.ctx).resetTransform)==null||o.call(s),this.ctx.scale(r,r)}startLoop(){if(this.animId)return;const c=r=>{const t=this.lastTime?Math.min((r-this.lastTime)/1e3,.1):.016;this.lastTime=r,this.update(t),this.render(),this.animId=requestAnimationFrame(c)};this.animId=requestAnimationFrame(c)}update(c){for(let r=this.shockwaves.length-1;r>=0;r--){const t=this.shockwaves[r];t.radius+=(t.maxRadius-t.radius)*12*c,t.alpha-=t.decay*c,(t.alpha<=0||t.radius>=t.maxRadius*.98)&&this.shockwaves.splice(r,1)}for(let r=this.particles.length-1;r>=0;r--){const t=this.particles[r];t.x+=t.vx*c,t.y+=t.vy*c,t.vx*=Math.pow(t.drag,c*60),t.vy*=Math.pow(t.drag,c*60),t.vy+=t.gravity*c,t.vRot&&t.rotation!==void 0&&(t.rotation+=t.vRot*c),t.alpha-=t.decay*c,t.alpha<=0&&this.particles.splice(r,1)}}render(){const c=this.canvas.width/(window.devicePixelRatio||1),r=this.canvas.height/(window.devicePixelRatio||1);this.ctx.clearRect(0,0,c,r);for(const t of this.shockwaves)this.ctx.save(),this.ctx.beginPath(),this.ctx.arc(t.x,t.y,Math.max(1,t.radius),0,Math.PI*2),this.ctx.strokeStyle=t.color,this.ctx.globalAlpha=Math.max(0,t.alpha),this.ctx.lineWidth=t.lineWidth,this.ctx.stroke(),this.ctx.restore();for(const t of this.particles){if(this.ctx.save(),this.ctx.globalAlpha=Math.max(0,t.alpha),this.ctx.fillStyle=t.color,t.shape==="rect"||t.shape==="spark"){this.ctx.translate(t.x,t.y),t.rotation!==void 0&&this.ctx.rotate(t.rotation);const s=t.size||t.radius*2;t.shape==="spark"?this.ctx.fillRect(-s,-s*.25,s*2,s*.5):this.ctx.fillRect(-s*.5,-s*.5,s,s)}else this.ctx.beginPath(),this.ctx.arc(t.x,t.y,Math.max(.5,t.radius),0,Math.PI*2),this.ctx.fill();this.ctx.restore()}}captureImpact(c,r,t="w"){this.shockwaves.push({x:c,y:r,radius:4,maxRadius:48,color:t==="w"?"rgba(255, 235, 180, 0.9)":"rgba(120, 140, 180, 0.9)",alpha:.9,decay:2.8,lineWidth:3});const s=t==="w"?["#ffffff","#fef08a","#f59e0b","#fbbf24"]:["#94a3b8","#cbd5e1","#64748b","#38bdf8"],o=18;for(let a=0;a{if(l<0||l>7||p<0||p>7)return!1;const g=v(l,p),i=t.get(g);return!i||!i.color?(d.push({target:g,isCapture:!1}),!0):(i.color!==r&&d.push({target:g,isCapture:!0}),!1)};switch(s.type){case"p":{const l=r==="w"?1:-1,p=r==="w"?1:6,g=v(o,a+l),i=t.get(g);if(i&&!i.color&&(d.push({target:g,isCapture:!1}),a===p)){const f=v(o,a+l*2),h=t.get(f);h&&!h.color&&d.push({target:f,isCapture:!1})}for(const f of[-1,1])if(o+f>=0&&o+f<=7){const h=v(o+f,a+l),y=t.get(h);y&&y.color&&y.color!==r&&d.push({target:h,isCapture:!0})}break}case"n":{const l=[[-2,-1],[-2,1],[-1,-2],[-1,2],[1,-2],[1,2],[2,-1],[2,1]];for(const[p,g]of l){const i=o+p,f=a+g;if(i>=0&&i<=7&&f>=0&&f<=7){const h=v(i,f),y=t.get(h);!y||!y.color?d.push({target:h,isCapture:!1}):y.color!==r&&d.push({target:h,isCapture:!0})}}break}case"b":{const l=[[-1,-1],[-1,1],[1,-1],[1,1]];for(const[p,g]of l){let i=o+p,f=a+g;for(;u(i,f);)i+=p,f+=g}break}case"r":{const l=[[-1,0],[1,0],[0,-1],[0,1]];for(const[p,g]of l){let i=o+p,f=a+g;for(;u(i,f);)i+=p,f+=g}break}case"q":{const l=[[-1,-1],[-1,1],[1,-1],[1,1],[-1,0],[1,0],[0,-1],[0,1]];for(const[p,g]of l){let i=o+p,f=a+g;for(;u(i,f);)i+=p,f+=g}break}case"k":{for(let p=-1;p<=1;p++)for(let g=-1;g<=1;g++){if(p===0&&g===0)continue;const i=o+p,f=a+g;if(i>=0&&i<=7&&f>=0&&f<=7){const h=v(i,f),y=t.get(h);!y||!y.color?d.push({target:h,isCapture:!1}):y.color!==r&&d.push({target:h,isCapture:!0})}}const l=r==="w"?0:7;if(a===l&&o===4){const p=t.get(v(5,l)),g=t.get(v(6,l)),i=t.get(v(7,l));p&&!p.color&&g&&!g.color&&(i==null?void 0:i.type)==="r"&&i.color===r&&d.push({target:v(6,l),isCapture:!1});const f=t.get(v(3,l)),h=t.get(v(2,l)),y=t.get(v(1,l)),m=t.get(v(0,l));f&&!f.color&&h&&!h.color&&y&&!y.color&&(m==null?void 0:m.type)==="r"&&m.color===r&&d.push({target:v(2,l),isCapture:!1})}break}}return d.filter(l=>!ft(e,l.target,c,r))}function ft(e,c,r,t){const s=r.map(i=>{if(i.square===e)return{square:e,type:null,color:null};if(i.square===c){const f=r.find(h=>h.square===e);return{square:c,type:(f==null?void 0:f.type)??null,color:t}}return{...i}}),o=s.find(i=>i.type==="k"&&i.color===t);if(!o)return!1;const{f:a,r:d}=H(o.square),u=t==="w"?"b":"w",l=new Map;for(const i of s)l.set(i.square,i);const p=[[-2,-1],[-2,1],[-1,-2],[-1,2],[1,-2],[1,2],[2,-1],[2,1]];for(const[i,f]of p){const h=a+i,y=d+f;if(h>=0&&h<=7&&y>=0&&y<=7){const m=l.get(v(h,y));if(m&&m.color===u&&m.type==="n")return!0}}const g=u==="w"?1:-1;for(const i of[-1,1]){const f=a-i,h=d-g;if(f>=0&&f<=7&&h>=0&&h<=7){const y=l.get(v(f,h));if(y&&y.color===u&&y.type==="p")return!0}}for(const[i,f]of[[-1,0],[1,0],[0,-1],[0,1]]){let h=a+i,y=d+f;for(;h>=0&&h<=7&&y>=0&&y<=7;){const m=l.get(v(h,y));if(m&&m.color){if(m.color===u&&(m.type==="r"||m.type==="q"))return!0;break}h+=i,y+=f}}for(const[i,f]of[[-1,-1],[-1,1],[1,-1],[1,1]]){let h=a+i,y=d+f;for(;h>=0&&h<=7&&y>=0&&y<=7;){const m=l.get(v(h,y));if(m&&m.color){if(m.color===u&&(m.type==="b"||m.type==="q"))return!0;break}h+=i,y+=f}}for(let i=-1;i<=1;i++)for(let f=-1;f<=1;f++){if(i===0&&f===0)continue;const h=a+i,y=d+f;if(h>=0&&h<=7&&y>=0&&y<=7){const m=l.get(v(h,y));if(m&&m.color===u&&m.type==="k")return!0}}return!1}let n=null,F=null,Z=null,$=null,C="",M=[],x=null,R=null,N=!1;const w=(e,c)=>(Z==null?void 0:Z.locale)==="en"?c:e,O=ot({onInit(e){Z=e,document.documentElement.lang=e.locale,document.documentElement.dir=e.dir,document.getElementById("board-grid")||j(),k()},onState(e){e.t==="snap"&&(document.getElementById("board-grid")||j(),F=n,n=e.body,pt(F,n),n.turn!==n.me&&(C="",M=[],x=null),k())},onAck(e){e.status==="rejected"&&O.toast("حرکت پذیرفته نشد","Move not accepted","warn")}});function pt(e,c){var d;if(!e||!c||!$)return;const r=new Map(e.board.map(u=>[u.square,u])),t=new Map(c.board.map(u=>[u.square,u]));let s="",o="",a=null;for(const[u,l]of t.entries()){const p=r.get(u);p&&(p.color&&!l.color?s=u:l.color&&(!p.color||p.color!==l.color||p.type!==l.type)&&(o=u,p.color&&p.color!==l.color&&(a=p)))}if(s&&o){R={from:s,to:o};const u=document.querySelector(`[data-square="${o}"]`);if(u){const l=u.getBoundingClientRect(),p=(d=document.getElementById("vfx-canvas"))==null?void 0:d.getBoundingClientRect();if(p){const i=l.left-p.left+l.width/2,f=l.top-p.top+l.height/2;a?$.captureImpact(i,f,a.color):$.landingPuff(i,f)}const g=u.querySelector(".piece-wrapper");g&&(g.classList.remove("anim-land"),g.offsetWidth,g.classList.add("anim-land"))}}c.phase==="over"&&c.winner!==null&&!N&&(N=!0,setTimeout(()=>$==null?void 0:$.victoryConfetti(),350))}function j(){const e=document.getElementById("app");e.innerHTML=`
`;const c=document.getElementById("vfx-canvas");$=new dt(c);const r=document.getElementById("board-grid");r.addEventListener("click",t=>{const s=t.target.closest(".cell");s&&s.dataset.square&&!s.disabled&&yt(s.dataset.square)});for(let t=0;t<64;t++){const s=document.createElement("button");s.className="cell",s.dataset.index=String(t),r.appendChild(s)}}function k(){var m;if(!n)return;const e=n.me,c=e!=null&&n.players[e]!==void 0;let r,t;c?(r=n.players[e],t=n.players.find(b=>b.seat!==e)||n.players[1-e]):(r=n.players.find(b=>b.color==="w")||n.players[0],t=n.players.find(b=>b.color==="b")||n.players[1]);const s=(r==null?void 0:r.color)||"w",o=s==="b",a=r?n.turn===r.seat&&n.phase==="play":!1,d=t?n.turn===t.seat&&n.phase==="play":!1;z("top-bar",t,d,!1),z("bottom-bar",r,a,c);const u=new Map;for(const b of n.board)u.set(b.square,b);const l=o?[7,6,5,4,3,2,1,0]:[0,1,2,3,4,5,6,7],p=o?[0,1,2,3,4,5,6,7]:[7,6,5,4,3,2,1,0],g=document.getElementById("board-grid");if(!g)return;const i=g.children;let f=0;const h=n.phase==="play"&&n.turn===e,y=n.check&&n.phase==="play"?(m=n.board.find(b=>{var L;return b.type==="k"&&b.color===((L=n.players[n.turn])==null?void 0:L.color)}))==null?void 0:m.square:null;for(const b of p)for(const L of l){const I=String.fromCharCode(97+L)+(b+1),q=u.get(I)||{type:null,color:null},S=i[f++];S.dataset.square=I,S.dataset.color=q.color||"";const K=(L+b)%2===0?"dark":"light",A=C===I,U=R&&(R.from===I||R.to===I),V=y===I;S.className=`cell ${K}`+(A?" selected":"")+(U?" last-move":"")+(V?" in-check":"");const Y=M.some(B=>B.target===I),Q=M.some(B=>B.target===I&&B.isCapture);let G="";const J=o?L===7:L===0,X=o?b===7:b===0;if(J&&(G+=`${b+1}`),X&&(G+=`${String.fromCharCode(97+L)}`),q.type&&q.color){const B=_(q.type,q.color),et=q.type==="n"&&A?" anim-knight":"";G+=`
${B}
`}Q?G+='
':Y&&(G+='
'),S.innerHTML=G;const W=h&&!0&&q.color===s,tt=C?h:W;S.disabled=!tt}gt()}function z(e,c,r,t){const s=document.getElementById(e);if(!s||!c||!n)return;s.className="player-bar"+(r?" active":"");const o=ut(c.color),a=ht(c.color),d=a>0?`+${a}`:"",u=c.bot?'BOT':"",l=t?`${w("شما","YOU")}`:"",p=n.phase==="over"?n.winner===c.seat?w("برنده 🏆","Winner 🏆"):n.winner===null?w("تساوی 🤝","Draw 🤝"):"":r?t?w("نوبت شماست","Your turn"):w("در حال تفکر…","Thinking…"):"";s.innerHTML=`
${c.color==="w"?"♔":"♚"}
${c.name} ${u} ${l}
${p}
${o.map(g=>`
${_(g.type,g.color)}
`).join("")} ${d}
`}function ut(e){if(!n)return[];const c=e==="w"?"b":"w",r={p:8,n:2,b:2,r:2,q:1},t={p:0,n:0,b:0,r:0,q:0};for(const o of n.board)o.color===c&&o.type&&t[o.type]!==void 0&&t[o.type]++;const s=[];for(const o of["q","r","b","n","p"]){const a=r[o]-(t[o]||0);for(let d=0;d
${w("ارتقای پیاده به:","Promote pawn to:")}
${["q","r","b","n"].map(o=>` `).join("")}
`,e.querySelectorAll("[data-promo]").forEach(o=>{o.addEventListener("click",()=>{var d;const a=o.dataset.promo;if(x&&a){O.cmd("move",{from:x.from,to:x.to,promotion:a});const u=document.querySelector(`[data-square="${x.to}"]`);if(u){const l=u.getBoundingClientRect(),p=(d=document.getElementById("vfx-canvas"))==null?void 0:d.getBoundingClientRect();p&&($==null||$.promotionBurst(l.left-p.left+l.width/2,l.top-p.top+l.height/2))}x=null,C="",M=[],k()}})}),(r=e.querySelector("[data-cancel-promo]"))==null||r.addEventListener("click",()=>{x=null,k()});return}if(n.phase==="over"){n.winner!==null&&(n.winner,n.me);const s=n.winner===null;let o="",a="";s?n.result_reason==="stalemate"?(o=w("پات — مساوی!","Stalemate — Draw!"),a=w("شاه راه حرکتی ندارد","King has no legal moves")):n.result_reason==="insufficient_material"?(o=w("تساوی — عدم کفایت مهره‌ها","Draw — Insufficient Material"),a=w("مهره کافی برای مات کردن در صفحه وجود ندارد","Neither side has sufficient pieces to force checkmate")):(o=w("بازی مساوی شد","Draw!"),a=w("هیچ‌کدام از طرفین برنده نشد","No side achieved victory")):(o=`${(t=n.players[n.winner])==null?void 0:t.name} ${w("برنده شد! 🏆","Wins! 🏆")}`,a=n.result_reason==="checkmate"?w("با کیش و مات حریف","By checkmate"):n.result_reason==="abandoned"?w("به دلیل ترک حریف","By abandonment"):""),e.innerHTML=`
${o}
${a}
`;return}e.innerHTML=""}}function yt(e){var s;if(!n||n.phase!=="play"||x)return;const c=n.me;if(c==null||n.turn!==c)return;const r=(s=n.players[c])==null?void 0:s.color;if(!r)return;const t=n.board.find(o=>o.square===e);if(C){if(C===e){C="",M=[],k();return}if(t!=null&&t.color&&t.color===r){C=e;try{M=D(e,n.board,r)}catch{M=[]}k();return}const o=n.board.find(d=>d.square===C),a=r==="b"?"1":"8";if((o==null?void 0:o.type)==="p"&&e[1]===a){x={from:C,to:e},C="",M=[],k();return}O.cmd("move",{from:C,to:e}),C="",M=[],k()}else if(t&&t.color===r){C=e;try{M=D(e,n.board,r)}catch{M=[]}k()}}