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