.elementor-3935 .elementor-element.elementor-element-efc892c{--display:flex;--flex-direction:column;--container-widget-width:100%;--container-widget-height:initial;--container-widget-flex-grow:0;--container-widget-align-self:initial;--flex-wrap-mobile:wrap;}/* Start custom CSS for html, class: .elementor-element-e281df3 */*{
margin:0;
padding:0;
box-sizing:border-box;
}

.hero{

padding:70px 20px;

background:
radial-gradient(circle at top right,#dff5ff 0%,transparent 30%),
radial-gradient(circle at bottom left,#ffe9f3 0%,transparent 25%),
#fff;

}

.container{

max-width:950px;
margin:auto;

}

.hero-box{

background:#fff;

border:1px solid #edf4fb;

border-radius:28px;

padding:50px;

text-align:center;

box-shadow:0 20px 50px rgba(39,162,234,.08);

}

.badge{

display:inline-block;

padding:8px 18px;

background:#eef8ff;

border-radius:30px;

color:#2d9ee8;

font-size:14px;

margin-bottom:18px;

}

.hero h1{

font-size:42px;

font-weight:800;

line-height:1.5;

color:#173a58;

margin-bottom:18px;

}

.hero h1 span{

display:block;

color:#2da5ef;

}

.hero p{

max-width:700px;

margin:0 auto 35px;

font-size:16px;

line-height:2;

color:#64809b;

}

.search-box{

display:flex;

align-items:center;

gap:10px;

background:#fff;

border:2px solid #dfeffc;

border-radius:60px;

padding:10px;

max-width:720px;

margin:auto;

box-shadow:0 8px 30px rgba(0,0,0,.04);

}

.google-icon{

width:42px;

height:42px;

border-radius:50%;

background:#f8fbff;

display:flex;

align-items:center;

justify-content:center;

position:relative;

flex-shrink:0;

}

.google-icon span{

position:absolute;

width:14px;

height:14px;

border-radius:50%;

}

.blue{

background:#4285F4;

top:7px;

left:14px;

}

.red{

background:#EA4335;

top:14px;

right:7px;

}

.yellow{

background:#FBBC05;

bottom:7px;

right:14px;

}

.green{

background:#34A853;

bottom:14px;

left:7px;

}

.search-box input{

flex:1;

border:none;

outline:none;

font-size:16px;

font-family:inherit;

}

.search-box button{

border:none;

background:#35a7f5;

color:#fff;

padding:14px 28px;

border-radius:40px;

font-size:15px;

font-weight:700;

cursor:pointer;

transition:.3s;

}

.search-box button:hover{

background:#2498e8;

}

.tags{

display:flex;

justify-content:center;

gap:12px;

flex-wrap:wrap;

margin-top:28px;

}

.tags span{

padding:9px 16px;

background:#f7fbff;

border:1px solid #e8f3fb;

border-radius:30px;

font-size:14px;

color:#5f7892;

}

@media(max-width:768px){

.hero{

padding:40px 15px;

}

.hero-box{

padding:30px 20px;

}

.hero h1{

font-size:30px;

}

.search-box{

flex-direction:column;

border-radius:20px;

}

.search-box input{

width:100%;

padding:10px;

text-align:center;

}

.search-box button{

width:100%;

}

}/* End custom CSS */
/* Start custom CSS for html, class: .elementor-element-c929797 */.problem-section{

padding:90px 20px;

background:#f9fcff;

}

.problem-section .container{

max-width:1150px;

margin:auto;

}

.section-title{

text-align:center;

max-width:760px;

margin:auto auto 60px;

}

.section-title span{

display:inline-block;

padding:8px 18px;

background:#eaf8ff;

border-radius:30px;

color:#2aa4ee;

font-size:14px;

margin-bottom:18px;

}

.section-title h2{

font-size:38px;

color:#173a58;

margin-bottom:18px;

line-height:1.6;

}

.section-title p{

font-size:16px;

line-height:2;

color:#68829b;

}

.problem-grid{

display:grid;

grid-template-columns:repeat(3,1fr);

gap:25px;

}

.problem-card{

background:#fff;

border-radius:22px;

padding:35px 30px;

border:1px solid #eaf2fb;

transition:.3s;

text-align:center;

}

.problem-card:hover{

transform:translateY(-8px);

box-shadow:0 15px 40px rgba(0,0,0,.06);

}

.problem-card.active{

border:2px solid #35a8f5;

box-shadow:0 15px 35px rgba(53,168,245,.12);

}

.icon{

width:70px;

height:70px;

margin:auto;

border-radius:50%;

background:#eef9ff;

display:flex;

align-items:center;

justify-content:center;

font-size:28px;

margin-bottom:20px;

}

.problem-card h3{

font-size:22px;

color:#173a58;

margin-bottom:15px;

}

.problem-card p{

font-size:15px;

line-height:2;

color:#69839b;

}

@media(max-width:900px){

.problem-grid{

grid-template-columns:1fr;

}

.section-title h2{

font-size:30px;

}

}/* End custom CSS */
/* Start custom CSS for html, class: .elementor-element-074f15f */.how-work{

padding:90px 20px;

background:#fff;

}

.how-work .container{

max-width:1150px;

margin:auto;

}

.section-head{

text-align:center;

max-width:700px;

margin:auto auto 70px;

}

.section-head span{

display:inline-block;

padding:8px 18px;

background:#eef9ff;

color:#30a6ef;

border-radius:30px;

font-size:14px;

margin-bottom:18px;

}

.section-head h2{

font-size:38px;

color:#173a58;

margin-bottom:18px;

line-height:1.6;

}

.section-head p{

color:#68839c;

font-size:16px;

line-height:2;

}

.steps{

display:grid;

grid-template-columns:repeat(3,1fr);

gap:30px;

position:relative;

}

.steps::before{

content:"";

position:absolute;

top:38px;

right:15%;

left:15%;

height:2px;

background:#d9eefc;

z-index:0;

}

.step{

position:relative;

background:#fff;

border:1px solid #ebf3fb;

border-radius:22px;

padding:35px;

text-align:center;

z-index:1;

transition:.3s;

}

.step:hover{

transform:translateY(-8px);

box-shadow:0 20px 40px rgba(44,167,243,.08);

}

.number{

width:75px;

height:75px;

margin:auto auto 25px;

background:linear-gradient(135deg,#35a8f5,#84d7ff);

border-radius:50%;

display:flex;

align-items:center;

justify-content:center;

font-size:24px;

font-weight:700;

color:#fff;

}

.step h3{

font-size:22px;

color:#173a58;

margin-bottom:15px;

}

.step p{

font-size:15px;

line-height:2;

color:#6a849d;

}

@media(max-width:900px){

.steps{

grid-template-columns:1fr;

}

.steps::before{

display:none;

}

.section-head h2{

font-size:30px;

}

}/* End custom CSS */
/* Start custom CSS for html, class: .elementor-element-b4c0a72 */@import url('https://fonts.googleapis.com/css2?family=Vazirmatn:wght@400;700;800&display=swap');

  :root {
    color-scheme: light;
    font-family: "Vazirmatn", "Segoe UI", Tahoma, Geneva, Verdana, sans-serif;
    line-height: 1.6;
    font-weight: 400;
    text-rendering: optimizeLegibility;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
    --bg: #f4f9ff;
    --surface: #ffffff;
    --surface-soft: #f8fbff;
    --border: #dce7f5;
    --text: #10233f;
    --muted: #5f748f;
    --accent: #4d8fe7;
    --accent-strong: #2d6fca;
    --accent-soft: #eaf4ff;
  }

  * { box-sizing: border-box; margin: 0; padding: 0; }

  body {
    min-width: 320px;
    min-height: 100vh;
    background: var(--bg);
    color: var(--text);
    direction: rtl;
  }

  button, input, textarea { font: inherit; }

  .app-shell {
    min-height: 100vh;
    padding: 24px 16px;
    background: linear-gradient(135deg, #f4f9ff 0%, #f8fbff 45%, #f0f7ff 100%);
  }

  .app-inner {
    max-width: 900px;
    margin: 0 auto;
    display: flex;
    flex-direction: column;
    gap: 22px;
  }

  .tool-card {
    background: var(--surface);
    border: 1px solid var(--border);
    border-radius: 24px;
    box-shadow: 0 16px 44px rgba(16, 35, 63, 0.07);
    padding: 26px;
  }

  .tool-head {
    margin-bottom: 20px;
    border-bottom: 1px solid var(--border);
    padding-bottom: 18px;
  }

  .eyebrow {
    margin: 0 0 8px 0;
    color: var(--accent-strong);
    font-size: 0.78rem;
    font-weight: 700;
    letter-spacing: 0.24em;
  }

  .tool-head h1 { margin: 0 0 8px 0; font-size: 1.55rem; line-height: 1.5; }
  .sub { margin: 0; color: var(--muted); line-height: 1.9; }

  .form-panel { display: flex; flex-direction: column; gap: 14px; }
  .field-group { display: flex; flex-direction: column; gap: 7px; }
  .field-group label { font-size: 0.95rem; font-weight: 700; }

  .input, .textarea, .file-input {
    width: 100%;
    border: 1px solid var(--border);
    border-radius: 14px;
    padding: 12px 14px;
    color: var(--text);
    background: #fff;
    transition: border-color 180ms ease, box-shadow 180ms ease;
  }

  .input:focus, .textarea:focus {
    outline: none;
    border-color: var(--accent);
    box-shadow: 0 0 0 4px rgba(77, 143, 231, 0.16);
  }

  .textarea { min-height: 120px; resize: vertical; }
  .file-meta { font-size: 0.85rem; color: var(--muted); }

  .primary-btn {
    border: 0;
    border-radius: 999px;
    padding: 13px 16px;
    background: linear-gradient(135deg, var(--accent) 0%, var(--accent-strong) 100%);
    color: #fff;
    font-weight: 700;
    font-size: 1rem;
    cursor: pointer;
    transition: transform 180ms ease, box-shadow 180ms ease;
  }

  .primary-btn:hover { transform: translateY(-1px); box-shadow: 0 10px 24px rgba(45, 111, 202, 0.2); }
  .primary-btn:disabled { opacity: 0.7; cursor: wait; transform: none; box-shadow: none; }

  .status-line { margin: 2px 0 0 0; font-size: 0.9rem; color: var(--muted); text-align: center; }

  .analysis-card {
    background: #0b1220;
    border: 1px solid #1c2740;
    border-radius: 28px;
    padding: 30px;
    box-shadow: 0 22px 60px rgba(8, 15, 30, 0.28);
    text-align: right;
  }

  .analysis-title { margin: 0 0 22px 0; color: #fff; font-size: 1.9rem; font-weight: 800; }

  .analysis-block {
    background: #131c30;
    border: 1px solid #22304d;
    border-radius: 18px;
    padding: 20px 22px;
    margin-bottom: 18px;
  }

  .analysis-block:last-child { margin-bottom: 0; }
  .block-label { margin: 0 0 10px 0; color: #9aa6f0; font-size: 1.1rem; font-weight: 800; }
  .block-text { margin: 0; color: #d7deec; line-height: 2; font-size: 1.02rem; }

  .dotted-list {
    margin: 0; padding-right: 22px; color: #d7deec;
    display: flex; flex-direction: column; gap: 12px; font-size: 1.02rem;
    list-style: disc;
  }
  .dotted-list li { line-height: 1.9; }
  .dotted-list li::marker { color: #7d8cf0; }

  .missing-title { display: block; color: #fff; font-weight: 700; }
  .missing-url { display: block; color: #8ea1c4; font-size: 0.85rem; margin-top: 3px; word-break: break-all; }

  .pill-row { display: flex; flex-wrap: wrap; gap: 8px; }

  .pill {
    display: inline-flex; align-items: center;
    padding: 10px 18px; border-radius: 999px;
    background: #1b2439; color: #c7d2ea;
    font-weight: 700; font-size: 0.9rem; line-height: 1; white-space: nowrap;
  }

  .diff-0 { background: rgba(52,199,130,0.16); color: #7ee6b0; }
  .diff-1 { background: rgba(240,190,90,0.16); color: #f2cf85; }
  .diff-2 { background: rgba(240,110,110,0.16); color: #f3a0a0; }

  .st-0 { background: rgba(240,190,90,0.2); color: #ffd479; }
  .st-1 { background: rgba(240,110,110,0.18); color: #f3a0a0; }
  .st-2 { background: rgba(52,199,130,0.16); color: #7ee6b0; }
  .st-3 { background: rgba(125,140,240,0.18); color: #b7c1f6; }

  .on-yes { background: rgba(52,199,130,0.14); color: #7ee6b0; }
  .on-no  { background: rgba(150,165,190,0.14); color: #a6b4cc; }

  .kw-list { display: flex; flex-direction: column; }

  .kw-card {
    border-bottom: 1px solid #22304d;
    padding: 18px 4px;
  }
  .kw-card:first-child { padding-top: 4px; }
  .kw-card:last-child { border-bottom: 0; padding-bottom: 4px; }

  .kw-head {
    display: flex; flex-wrap: wrap; align-items: center;
    justify-content: space-between; gap: 10px; margin-bottom: 12px;
  }
  .kw-name { color: #fff; font-weight: 800; font-size: 1.08rem; }
  .kw-meta { margin-bottom: 12px; }
  .kw-titles { margin-top: 4px; }
  .kw-titles li { color: #d7deec; }

  .loader { display: inline-block; width: 18px; height: 18px; border: 3px solid rgba(255,255,255,0.3); border-top-color: #fff; border-radius: 50%; animation: spin 0.7s linear infinite; vertical-align: middle; margin-left: 8px; }
  @keyframes spin { to { transform: rotate(360deg); } }

  @media (max-width: 640px) {
    .app-shell { padding: 14px; }
    .tool-card, .analysis-card { padding: 20px; border-radius: 20px; }
    .tool-head h1 { font-size: 1.3rem; }
    .analysis-title { font-size: 1.5rem; }
  }
<script>(function(){

var API_KEY = "NkdXc081OEI5aVVpZE05dmJqWjc6QTdoTEhQcGh6SkVyQldVNU5zVE9zQQ==";
var GEMINI_URL = "https://generativelanguage.googleapis.com/v1beta/models/gemini-2.5-flash:generateContent";
var SERP_URL  = "https://serpapi.com/search.json";

var excelKws = [];
var $status  = document.getElementById("statusLine");
var $meta    = document.getElementById("fileMeta");
var $btn     = document.getElementById("submitBtn");
var $results = document.getElementById("results");

/* ── helpers ── */

function esc(s){
  var d=document.createElement("div");
  d.appendChild(document.createTextNode(s));
  return d.innerHTML;
}

function norm(t){
  return t.toLowerCase()
    .replace(/[^\p{L}\p{N}\s]/gu," ")
    .replace(/\s+/g," ")
    .trim();
}

function domain(url){
  try{
    var p=new URL(/^https?:\/\//.test(url)?url:"https://"+url);
    return p.hostname.replace(/^www\./,"");
  }catch(e){
    return url||"سایت شما";
  }
}

function diff(kw,vol){
  var n=parseInt(vol.replace(/[^\d]/g,""),10),
      w=kw.trim().split(/\s+/).length;

  if(!isNaN(n)){
    return n>=5000?2:n>=800?1:0;
  }

  return w>=4?0:w===3?1:2;
}

function fmtVol(v){
  if(!v) return "نامشخص";

  var n=parseInt(v.replace(/[^\d]/g,""),10);
  return isNaN(n)?v:n.toLocaleString("fa-IR");
}

var diffLabel=["آسان","متوسط","سخت"];
var statusLabel=["شکاف طلایی","عقب از رقیب","فرصت جدید","پوشش داده‌شده"];
var diffClass=["diff-0","diff-1","diff-2"];

function statusIdx(own,comp){
  return !own&&comp?0:
         own&&comp?1:
         own&&!comp?3:
         2;
}

function fallbackTitles(kw){
  var y=new Date().getFullYear(),
      k=kw.replace(/^\s*[-•]\s*/,"").trim();

  return [
    "راهنمای کامل "+k+"؛ از انتخاب تا اجرای اصولی",
    "هزینه "+k+" در سال "+y+" + عوامل مؤثر بر قیمت",
    "۵ اشتباه رایج در "+k+" و روش جلوگیری از آن‌ها"
  ];
}

/* ── اکسل ── */

function readExcel(file){
  return new Promise(function(ok){

    var r=new FileReader();

    r.onload=function(e){

      try{

        var wb=XLSX.read(
          new Uint8Array(e.target.result),
          {type:"array"}
        );

        var out=[];

        var kh=[
          "keyword",
          "keywords",
          "کلمه",
          "کلمه کلیدی",
          "کیورد",
          "عبارت",
          "query",
          "search term"
        ];

        var vh=[
          "volume",
          "search volume",
          "حجم",
          "حجم سرچ",
          "حجم جستجو",
          "sv",
          "vol",
          "monthly searches"
        ];

        wb.SheetNames.forEach(function(sn){

          var sh=wb.Sheets[sn];

          var rows=XLSX.utils.sheet_to_json(
            sh,
            {
              header:1,
              blankrows:false
            }
          );

          if(!rows.length) return;

          var hdr=rows[0].map(function(c){
            return String(c||"").trim().toLowerCase();
          });

          var kc=hdr.findIndex(function(h){
            return kh.some(function(k){
              return h.includes(k);
            });
          });

          var vc=hdr.findIndex(function(h){
            return vh.some(function(v){
              return h.includes(v);
            });
          });

          var hasH=kc!==-1||vc!==-1;

          if(kc===-1) kc=0;

          (hasH?rows.slice(1):rows).forEach(function(row){

            if(!Array.isArray(row)) return;

            var kw=String(row[kc]||"").trim();

            if(
              kw.length<2||
              kw.length>90||
              /^\d+([.,]\d+)?$/.test(kw)
            ){
              return;
            }

            var vol="";

            if(vc!==-1){
              vol=String(row[vc]||"").trim();
            }else{

              var num=row.find(function(c,i){

                return i!==kc &&
                  /^\s*[\d,،.]+\s*$/.test(
                    String(c||"")
                  );

              });

              if(num!==undefined){
                vol=String(num).trim();
              }

            }

            out.push({
              keyword:kw,
              volume:vol
            });

          });

        });

        var seen={};

        ok(

          out.filter(function(o){

            var k=o.keyword.toLowerCase();

            if(seen[k]) return false;

            seen[k]=true;

            return true;

          }).slice(0,60)

        );

      }catch(err){

        ok([]);

      }

    };

    r.onerror=function(){

      ok([]);

    };

    r.readAsArrayBuffer(file);

  });
}
/* ── سرچ ── */

function serp(q){

  var p=new URLSearchParams({
    api_key:API_KEY,
    engine:"google",
    q:q,
    hl:"fa",
    gl:"ir",
    num:"6"
  });

  return fetch(SERP_URL+"?"+p)

    .then(function(r){

      return r.ok
        ? r.json().then(function(d){

            return (d.organic_results||[])
              .filter(function(i){
                return i.title&&i.link;
              })
              .slice(0,6)
              .map(function(i){
                return {
                  title:i.title,
                  url:i.link,
                  snippet:i.snippet||""
                };
              });

          })
        : [];

    })

    .catch(function(){

      return [];

    });

}

/* ── پوشش ── */

function covered(kw,res){

  var words=norm(kw)
    .split(" ")
    .filter(function(w){
      return w.length>2;
    });

  if(!words.length) return false;

  return res.some(function(r){

    var hay=norm(
      r.title+
      " "+
      r.snippet+
      " "+
      r.url
    );

    var m=words.filter(function(w){

      return hay.includes(w);

    }).length;

    return m>=Math.max(
      1,
      Math.ceil(words.length*0.75)
    );

  });

}

/* ── AI ── */

function aiTitles(payload){

  var y=new Date().getFullYear();

  var miss=payload.miss
    .slice(0,8)
    .map(function(i){
      return "- "+i.title;
    })
    .join("\n");

  var kws=payload.kws
    .slice(0,25)
    .map(function(k){

      return k.keyword+
        (
          k.volume
          ? " (حجم سرچ: "+k.volume+")"
          : ""
        );

    })
    .join("\n");

  var prompt=

    "تو یک استراتژیست ارشد سئو و کپی‌رایتر حرفه‌ای فارسی هستی.\n\n"+

    "موضوع اصلی و اجباری: «"+
    payload.topic+
    "»\n"+

    "⚠️ بسیار مهم: تمام عنوان‌ها باید ۱۰۰٪ و دقیقاً درباره‌ی «"+
    payload.topic+
    "» باشند.\n\n"+

    "سایت کاربر: "+
    payload.site+
    "\n"+

    "رقبا: "+
    (payload.comps.join("، ")||"نامشخص")+
    "\n\n"+

    "کلمات کلیدی از اکسل:\n"+
    (kws||"موجود نیست")+

    "\n\nمقاله‌های رقبا:\n"+
    (miss||"موجود نیست")+

    "\n\nقوانین:\n"+
    "- برای هر کلمه کلیدی دقیقاً ۲ عنوان بده.\n"+
    "- هر عنوان شامل کلمه کلیدی باشد و در موضوع «"+
    payload.topic+
    "» بماند.\n"+
    "- از عدد، سال "+
    y+
    "، مقایسه، راهنما، هزینه، چک‌لیست استفاده کن.\n"+
    "- طول هر عنوان بین ۶ تا ۱۴ کلمه.\n\n"+

    'خروجی JSON معتبر، بدون متن اضافه:\n'+
    '{"کلمه اول":["عنوان ۱","عنوان ۲"],"کلمه دوم":["عنوان ۱","عنوان ۲"]}';

  return fetch(
    GEMINI_URL+"?key="+API_KEY,
    {
      method:"POST",
      headers:{
        "Content-Type":"application/json"
      },
      body:JSON.stringify({

        contents:[
          {
            parts:[
              {
                text:prompt
              }
            ]
          }
        ],

        generationConfig:{
          temperature:0.85,
          responseMimeType:"application/json"
        }

      })
    }
  )

  .then(function(r){

    return r.ok
      ? r.json()
      : null;

  })

  .then(function(d){

    if(!d) return null;

    var t=

      (
        d.candidates &&
        d.candidates[0] &&
        d.candidates[0].content &&
        d.candidates[0].content.parts &&
        d.candidates[0].content.parts[0] &&
        d.candidates[0].content.parts[0].text
      ) || "";

    var m=t.match(/\{[\s\S]*\}/);

    if(!m) return null;

    try{

      var p=JSON.parse(m[0]);

      return typeof p==="object"
        ? p
        : null;

    }catch(e){

      return null;

    }

  })

  .catch(function(){

    return null;

  });

}
/* ── ارتباط کلمه با موضوع ── */

function topicRelevance(kwNorm, tWords){

  if(!tWords.length) return 1;

  var matched=0;

  tWords.forEach(function(tw){

    if(kwNorm.includes(tw)){
      matched++;
    }

  });

  return matched===0
    ? 2
    : matched===tWords.length
      ? 0
      : 1;

}

/* ── ساخت سطرهای خروجی ── */

function buildRows(kws,topic,ownRes,compMap,aiT){

  var src=kws.length
    ? kws
    : [
        {
          keyword:topic.trim()||"ساختمان‌سازی",
          volume:""
        }
      ];

  var compDomains=Object.keys(compMap);

  var rows=src.slice(0,30).map(function(item){

    var ai=aiT&&aiT[item.keyword];

    var titles=
      (ai&&ai.length)
        ? ai.slice(0,2)
        : fallbackTitles(item.keyword);

    var own=covered(item.keyword,ownRes);

    var comps=compDomains.map(function(d){

      return {
        domain:d,
        covered:covered(
          item.keyword,
          compMap[d]
        )
      };

    });

    var anyComp=comps.some(function(c){

      return c.covered;

    });

    return {

      keyword:item.keyword,

      volume:fmtVol(item.volume),

      diff:diff(
        item.keyword,
        item.volume
      ),

      own:own,

      comps:comps,

      si:statusIdx(
        own,
        anyComp
      ),

      titles:titles

    };

  });

  var tWords=norm(topic)
    .split(" ")
    .filter(function(w){

      return w.length>1;

    });

  rows.sort(function(a,b){

    var aRel=topicRelevance(
      norm(a.keyword),
      tWords
    );

    var bRel=topicRelevance(
      norm(b.keyword),
      tWords
    );

    if(aRel!==bRel){

      return aRel-bRel;

    }

    return a.si-b.si;

  });

  return rows.slice(0,20);

}

/* ── رندر خروجی ── */

function render(data){

  var h='<section class="analysis-card">';

  h+='<h2 class="analysis-title">نتایج تحلیل</h2>';

  h+='<div class="analysis-block">';
  h+='<p class="block-label">بزرگ‌ترین نقطه ضعف:</p>';
  h+='<p class="block-text">'+
      esc(data.weakness)+
      '</p>';
  h+='</div>';

  if(data.miss.length){

    h+='<div class="analysis-block">';
    h+='<p class="block-label">';
    h+='مقاله‌هایی که رقبا دارند و شما ندارید:';
    h+='</p>';

    h+='<ul class="dotted-list">';

    data.miss.forEach(function(i){

      h+='<li>';

      h+='<span class="missing-title">';
      h+=esc(i.title);
      h+='</span>';

      h+='<span class="missing-url">';
      h+=esc(i.url);
      h+='</span>';

      h+='</li>';

    });

    h+='</ul>';
    h+='</div>';

  }

  h+='<div class="analysis-block">';
  h+='<p class="block-label">';
  h+='تحلیل کلمات کلیدی و پیشنهاد عنوان:';
  h+='</p>';

  h+='<div class="kw-list">';

  data.rows.forEach(function(r){

    h+='<div class="kw-card">';

    h+='<div class="kw-head">';

    h+='<span class="kw-name">';
    h+=esc(r.keyword);
    h+='</span>';

    h+='<span class="pill st-'+r.si+'">';
    h+=statusLabel[r.si];
    h+='</span>';

    h+='</div>';

    h+='<div class="pill-row kw-meta">';

    h+='<span class="pill">';
    h+='حجم سرچ: '+esc(r.volume);
    h+='</span>';

    h+='<span class="pill '+diffClass[r.diff]+'">';
    h+='رقابت: '+diffLabel[r.diff];
    h+='</span>';

    h+='<span class="pill '+(r.own?"on-yes":"on-no")+'">';
    h+='سایت شما: ';
    h+=r.own?"دارد":"ندارد";
    h+='</span>';

    if(r.comps.length){

      r.comps.forEach(function(c){

        h+='<span class="pill '+(c.covered?"on-yes":"on-no")+'">';

        h+=esc(c.domain);

        h+=': ';

        h+=c.covered
          ?"دارد"
          :"ندارد";

        h+='</span>';

      });

    }else{

      h+='<span class="pill on-no">';
      h+='رقیبی وارد نشده';
      h+='</span>';

    }

    h+='</div>';

    h+='<ul class="dotted-list kw-titles">';

    r.titles.forEach(function(t){

      h+='<li>';
      h+=esc(t);
      h+='</li>';

    });

    h+='</ul>';

    h+='</div>';

  });

  h+='</div>';
  h+='</div>';
  h+='</section>';

  $results.innerHTML=h;

}
/* ── انتخاب فایل اکسل ── */

document.getElementById("file").addEventListener("change", async function(e){

  var f=e.target.files&&e.target.files[0];

  if(!f){
    $meta.textContent="فایل اکسل یا داده اضافه نشده";
    excelKws=[];
    return;
  }

  $meta.textContent=f.name;

  $status.textContent="در حال خواندن فایل اکسل...";

  excelKws=await readExcel(f);

  $status.textContent=

    excelKws.length

      ? excelKws.length+
        " کلمه کلیدی از اکسل خوانده شد. حالا دکمه تحلیل را بزن."

      : "فایل خوانده شد اما کلمه کلیدی معتبری پیدا نشد؛ می‌توانی موضوع را دستی وارد کنی.";

});


/* ── ارسال فرم ── */

document.getElementById("mainForm").addEventListener("submit",async function(e){

  e.preventDefault();

  var siteUrl=document.getElementById("siteUrl").value;
  var topic=document.getElementById("topic").value;
  var comps=document.getElementById("competitors").value;

  $btn.disabled=true;

  $btn.innerHTML='در حال تحلیل...<span class="loader"></span>';

  $status.textContent="در حال بررسی رقبا و تولید بهترین عنوان مقاله‌ها...";

  $results.innerHTML="";

  var siteDomain=domain(siteUrl);

  var topicText=topic.trim()||"ساختمان‌سازی";

  var compList=comps
    .split(/[\n,]/)
    .map(function(s){
      return domain(s.trim());
    })
    .filter(Boolean)
    .slice(0,12);

  /* کلمه موضوع همیشه اول جستجو می‌شود */

  var searchKws=[topicText];

  excelKws.slice(0,5).forEach(function(k){

    if(norm(k.keyword)!==norm(topicText)){

      searchKws.push(k.keyword);

    }

  });

  searchKws=searchKws.slice(0,6);

  try{

    /* سایت خود */

    var ownArrs=await Promise.all(

      searchKws.map(function(kw){

        return serp("site:"+siteDomain+" "+kw);

      })

    );

    var ownRes=ownArrs.flat();


    /* رقبا */

    var compMap={};

    await Promise.all(

      compList.map(async function(d){

        var arrs=await Promise.all(

          searchKws.map(function(kw){

            return serp("site:"+d+" "+kw);

          })

        );

        compMap[d]=arrs.flat();

      })

    );


    /* مقاله‌های رقبا که سایت ندارد */

    var ownTitles={};

    ownRes.forEach(function(i){

      ownTitles[norm(i.title)]=true;

    });

    var allComp=Object.values(compMap).flat();

    var miss=allComp.filter(function(i){

      var n=norm(i.title);

      return n.length>5 && !ownTitles[n];

    }).slice(0,6);


    /* هوش مصنوعی */

    var aiT=await aiTitles({

      topic:topicText,

      site:siteUrl,

      comps:compList,

      kws:excelKws,

      miss:miss

    });


    /* ساخت خروجی */

    var rows=buildRows(

      excelKws,

      topicText,

      ownRes,

      compMap,

      aiT

    );

    var gaps=rows.filter(function(r){

      return r.si===0;

    }).length;


    var weakness=

      excelKws.length

      ? "از "+excelKws.length+

        " کلمه کلیدی اکسل، "+

        gaps+

        " کلمه «شکاف طلایی» هستند؛ یعنی حداقل یک رقیب برای آن‌ها محتوا دارد ولی سایت شما ("+

        siteDomain+

        ") ندارد. این کلمات بهترین اولویت برای نوشتن مقاله در حوزه «"+

        topicText+

        "» هستند."

      :

        "در موضوع «"+

        topicText+

        "» فرصت اصلی این است که به‌جای محتوای کلی، سراغ عنوان‌های تخصصی و راهنمامحور بروید تا از رقبا متمایز شوید.";


    render({

      weakness:weakness,

      rows:rows,

      miss:miss

    });


    $status.textContent=

      aiT

      ? "تحلیل "+rows.length+

        " کلمه در برابر "+

        compList.length+

        " رقیب با کمک هوش مصنوعی آماده شد."

      : "تحلیل "+rows.length+

        " کلمه در برابر "+

        compList.length+

        " رقیب بر اساس اکسل و الگوهای سئو آماده شد.";


  }catch(err){

    render({

      weakness:
      "در موضوع «"+
      topicText+
      "» فرصت اصلی این است که به‌جای محتوای کلی، سراغ عنوان‌های تخصصی و راهنمامحور بروید.",

      rows:buildRows(

        excelKws,

        topicText,

        [],

        {},

        null

      ),

      miss:[]

    });

    $status.textContent="اتصال مستقیم در دسترس نبود؛ تحلیل بر اساس اکسل و الگوهای سئو ساخته شد.";

  }finally{

    $btn.disabled=false;

    $btn.textContent="تحلیل و پیشنهاد عنوان مقاله";

  }

});

})();
</script>/* End custom CSS */