function analyzeSEO() {
var text = document.getElementById('seoContent').value;
var keyword = document.getElementById('focusKeyword').value.toLowerCase();
var resultsDiv = document.getElementById('seoResults');
var feedbackDiv = document.getElementById('seoFeedback');
if (!text) { alert('Please enter some content.'); return; }
resultsDiv.style.display = 'block';
var words = text.trim().split(/\s+/).filter(function(w) { return w.length > 0; });
var wordCount = words.length;
document.getElementById('wordCount').innerText = wordCount;
var kwCount = 0;
if (keyword) {
var regExp = new RegExp('\\b' + keyword + '\\b', 'gi');
var matches = text.match(regExp);
kwCount = matches ? matches.length : 0;
}
var density = wordCount > 0 ? ((kwCount / wordCount) * 100).toFixed(2) : 0;
document.getElementById('keywordDensity').innerText = density + '%';
var sentences = text.split(/[.!?]+/).filter(function(s) { return s.trim().length > 0; }).length;
var avgSentenceLength = wordCount / (sentences || 1);
var readability = "Good";
if (avgSentenceLength > 20) readability = "Hard";
if (avgSentenceLength < 12) readability = "Easy";
document.getElementById('readabilityScore').innerText = readability;
var tips = '
';
if (wordCount < 300) {
tips += '- Thin content: Aim for at least 600-1000 words for better ranking potential.
';
} else {
tips += '- Word count looks healthy for SEO.
';
}
if (density < 0.5 && keyword) {
tips += '- Keyword density is low. Try to include "' + keyword + '" more naturally.
';
} else if (density > 3 && keyword) {
tips += '- Keyword density is too high (' + density + '%). Avoid keyword stuffing.
';
} else if (keyword) {
tips += '- Keyword usage is optimized.
';
}
if (avgSentenceLength > 20) {
tips += '- Sentences are long (Avg: ' + Math.round(avgSentenceLength) + ' words). Break them up to improve UX.
';
}
tips += '
';
feedbackDiv.innerHTML = tips;
}