(function() {
  'use strict';
  
  var SORO_BLOG_TITLE = 'atsdkarate.com';
  var SORO_ARTICLES = [{"id":"b519f286-72fa-4398-84a3-f74a1b21482a","title":"Martial Arts Training That Builds More Than Skill","slug":"martial-arts-training-builds-more-than-skill","excerpt":"Martial arts training builds focus, confidence, fitness, and self-defense skills through structured classes for children, teens, and adults in Lansdale.","content":null,"date":"November 4, 2026","isoDate":"2026-11-04T03:25:12.348+00:00","image":"https://afocirmbqdxnkyescnev.supabase.co/storage/v1/object/public/featured-images/e79999b7-0539-4ec0-8624-ec607ed948fe/workflows/5c0fd590-013f-4b25-987d-bfc70f3e34bf.webp"},{"id":"b8d9bbee-b85a-4ecb-95dc-5a09b66fbed3","title":"Adult Karate: Build Strength, Focus, and Confidence","slug":"adult-karate-strength-focus-confidence","excerpt":"Adult karate builds practical self-defense, fitness, focus, and confidence. See how structured Tang Soo Do training helps beginners progress in Lansdale.","content":null,"date":"November 2, 2026","isoDate":"2026-11-02T03:22:46.205+00:00","image":"https://afocirmbqdxnkyescnev.supabase.co/storage/v1/object/public/featured-images/e79999b7-0539-4ec0-8624-ec607ed948fe/workflows/d3d45de7-7764-43c9-82fc-c0c1163c666e.webp"},{"id":"d61ddcff-c9dd-4c98-abb7-52dd27876a80","title":"Tang Soo Do Benefits for Kids and Adults","slug":"tang-soo-do-benefits-kids-adults","excerpt":"Explore Tang Soo Do benefits for children and adults, from practical self-defense and fitness to stronger focus, respect, confidence, and community too.","content":null,"date":"October 31, 2026","isoDate":"2026-10-31T03:21:52.864+00:00","image":"https://afocirmbqdxnkyescnev.supabase.co/storage/v1/object/public/featured-images/e79999b7-0539-4ec0-8624-ec607ed948fe/workflows/1c37301a-3ab5-474e-b505-d3897cb76001.webp"},{"id":"ea85d6c0-e1ef-4bf4-80e5-643304b242d4","title":"Self Defense Skills Begin With Better Habits","slug":"self-defense-skills-better-habits","excerpt":"Build practical self defense skills through traditional Tang Soo Do training that strengthens awareness, confidence, discipline, and sound decision-making.","content":null,"date":"October 29, 2026","isoDate":"2026-10-29T03:30:57.549+00:00","image":"https://afocirmbqdxnkyescnev.supabase.co/storage/v1/object/public/featured-images/e79999b7-0539-4ec0-8624-ec607ed948fe/workflows/4285d07c-86c5-43f5-b632-602df4d250c1.webp"},{"id":"507855a9-fdd8-4471-a8fe-c328cb106845","title":"Karate Fitness Builds Strength, Focus, and Confidence","slug":"karate-fitness-strength-focus-confidence","excerpt":"Karate fitness combines conditioning, practical self-defense, and disciplined training to help children and adults grow stronger, focused, and confident.","content":null,"date":"October 27, 2026","isoDate":"2026-10-27T03:24:23.106+00:00","image":"https://afocirmbqdxnkyescnev.supabase.co/storage/v1/object/public/featured-images/e79999b7-0539-4ec0-8624-ec607ed948fe/workflows/6b629097-45bc-4413-9bfd-dbaf70e4c75f.webp"},{"id":"c19448a9-e17a-40a1-9463-22409022883b","title":"Are Karate Classes Safe for Kids and Adults?","slug":"are-karate-classes-safe","excerpt":"Are karate classes safe? Learn how qualified instruction, age-appropriate training, rules, and respectful partners help children and adults train safely.","content":null,"date":"October 25, 2026","isoDate":"2026-10-25T03:20:48.374+00:00","image":"https://afocirmbqdxnkyescnev.supabase.co/storage/v1/object/public/featured-images/e79999b7-0539-4ec0-8624-ec607ed948fe/workflows/07464005-adfa-4ab7-8c70-c9427d45d5af.webp"},{"id":"746daba1-70b8-4496-8846-90e5304ba962","title":"Can Adults Start Karate? Yes, With the Right School","slug":"can-adults-start-karate","excerpt":"Can adults start karate? Absolutely. Learn what beginners need, how training builds confidence, and why age is not a barrier to progress at any stage.","content":null,"date":"October 23, 2026","isoDate":"2026-10-23T03:21:48.295+00:00","image":"https://afocirmbqdxnkyescnev.supabase.co/storage/v1/object/public/featured-images/e79999b7-0539-4ec0-8624-ec607ed948fe/workflows/66ecdee4-1949-431c-a89d-7fe4bdd793b1.webp"},{"id":"19ab4865-3cb6-4804-a1cf-463de0cf298d","title":"Building Child Confidence Through Karate","slug":"child-confidence-through-karate","excerpt":"Build child confidence through karate with structured lessons that teach focus, respect, resilience, and practical skills in a supportive local community.","content":null,"date":"October 21, 2026","isoDate":"2026-10-21T03:20:44.281+00:00","image":"https://afocirmbqdxnkyescnev.supabase.co/storage/v1/object/public/featured-images/e79999b7-0539-4ec0-8624-ec607ed948fe/workflows/479314c5-de90-4b82-90a0-d167e60cbb3c.webp"},{"id":"09c15755-aa0c-46d1-ab4c-6450880cd98a","title":"Teen Karate Classes That Build Real Confidence","slug":"teen-karate-classes","excerpt":"Teen karate classes build confidence, focus, fitness, and practical self-defense through structured training, high standards, and a supportive community.","content":null,"date":"October 19, 2026","isoDate":"2026-10-19T03:21:28.948+00:00","image":"https://afocirmbqdxnkyescnev.supabase.co/storage/v1/object/public/featured-images/e79999b7-0539-4ec0-8624-ec607ed948fe/workflows/332d32dc-425e-4855-8bb1-84744479f6a3.webp"},{"id":"e1967049-5d65-490d-9106-17d76e055389","title":"Traditional Karate Terminology Guide for Beginners","slug":"traditional-karate-terminology-guide","excerpt":"Use this traditional karate terminology guide to learn common commands, stances, strikes, and respectful dojo habits for confident training in every karate class.","content":null,"date":"October 17, 2026","isoDate":"2026-10-17T03:32:08.312+00:00","image":"https://afocirmbqdxnkyescnev.supabase.co/storage/v1/object/public/featured-images/e79999b7-0539-4ec0-8624-ec607ed948fe/e1967049-5d65-490d-9106-17d76e055389.webp"},{"id":"47d11a95-91de-41f8-9f24-b535c1c019f7","title":"Karate Training for Focus Builds Discipline","slug":"karate-training-for-focus","excerpt":"Karate training for focus gives children and adults a practical way to strengthen attention, self-control, confidence, and daily discipline at every age.","content":null,"date":"October 16, 2026","isoDate":"2026-10-16T03:41:39.148+00:00","image":"https://afocirmbqdxnkyescnev.supabase.co/storage/v1/object/public/featured-images/e79999b7-0539-4ec0-8624-ec607ed948fe/47d11a95-91de-41f8-9f24-b535c1c019f7.webp"},{"id":"d51a4217-75fd-494c-a750-363816fa53e8","title":"Martial Arts Confidence Building That Lasts","slug":"martial-arts-confidence-building","excerpt":"Martial arts confidence building gives children and adults lasting focus, self-respect, and courage through disciplined Tang Soo Do practice.","content":null,"date":"October 15, 2026","isoDate":"2026-10-15T03:31:02.926+00:00","image":"https://afocirmbqdxnkyescnev.supabase.co/storage/v1/object/public/featured-images/e79999b7-0539-4ec0-8624-ec607ed948fe/d51a4217-75fd-494c-a750-363816fa53e8.webp"},{"id":"d4e9069e-f552-4204-a415-fbcaeace16bd","title":"Family Karate Classes Near Me in Lansdale","slug":"family-karate-classes-near-me-lansdale","excerpt":"Searching for family karate classes near me? Build confidence, focus, fitness, and respect together with traditional Tang Soo Do training in Lansdale for all ages.","content":null,"date":"October 14, 2026","isoDate":"2026-10-14T03:42:40.428+00:00","image":"https://afocirmbqdxnkyescnev.supabase.co/storage/v1/object/public/featured-images/e79999b7-0539-4ec0-8624-ec607ed948fe/workflows/2110416c-1355-41bd-aaaa-5f43174b3f77.webp"},{"id":"7bd18e2c-94f3-4973-9bff-0c7450af826b","title":"Martial Arts Summer Camp Builds More Than Skills","slug":"martial-arts-summer-camp","excerpt":"See how a martial arts summer camp helps kids build focus, confidence, fitness, respect, and practical self-defense skills in a structured setting each day.","content":null,"date":"October 13, 2026","isoDate":"2026-10-13T03:43:02.289+00:00","image":"https://afocirmbqdxnkyescnev.supabase.co/storage/v1/object/public/featured-images/e79999b7-0539-4ec0-8624-ec607ed948fe/7bd18e2c-94f3-4973-9bff-0c7450af826b.webp"},{"id":"b1d5094a-1a4b-454a-ac5b-4d8da166d92b","title":"Youth Karate Tournament Training That Builds Poise","slug":"youth-karate-tournament-training","excerpt":"Youth karate tournament training develops calm competitors through sound technique, focused practice, respect, and smart preparation for tournament day.","content":null,"date":"October 12, 2026","isoDate":"2026-10-12T03:40:32.973+00:00","image":"https://afocirmbqdxnkyescnev.supabase.co/storage/v1/object/public/featured-images/e79999b7-0539-4ec0-8624-ec607ed948fe/b1d5094a-1a4b-454a-ac5b-4d8da166d92b.webp"},{"id":"2a8a7b5b-4110-46e5-9d2c-334698d5dc12","title":"Karate Belt Test Preparation That Builds Confidence","slug":"karate-belt-test-preparation","excerpt":"Karate belt test preparation builds more than technique. Learn how focused practice, respect, and steady habits help students test with calm confidence.","content":null,"date":"October 11, 2026","isoDate":"2026-10-11T03:41:19.055+00:00","image":"https://afocirmbqdxnkyescnev.supabase.co/storage/v1/object/public/featured-images/e79999b7-0539-4ec0-8624-ec607ed948fe/workflows/6e4a51d6-aa4e-4f1a-b466-e90fb01ef321.webp"},{"id":"ef3c8ec1-b742-4f8a-86fa-e0b0b104b646","title":"Free Trial Karate Class: What to Expect First","slug":"free-trial-karate-class-what-to-expect","excerpt":"Try a free trial karate class in Lansdale and see how traditional Tang Soo Do builds confidence, focus, fitness, and respect at every age with support.","content":null,"date":"October 10, 2026","isoDate":"2026-10-10T03:43:33.533+00:00","image":"https://afocirmbqdxnkyescnev.supabase.co/storage/v1/object/public/featured-images/e79999b7-0539-4ec0-8624-ec607ed948fe/workflows/8e15fad1-a4eb-403a-aabc-75bd1d2d03f1.webp"},{"id":"f31c3626-0e70-422b-9f66-04e2f142fd70","title":"Self Defense Classes for Children Build Confidence","slug":"self-defense-classes-for-children","excerpt":"Self defense classes for children build practical awareness, confidence, focus, and respect through structured training with caring local instructors.","content":null,"date":"October 9, 2026","isoDate":"2026-10-09T03:41:09.014+00:00","image":"https://afocirmbqdxnkyescnev.supabase.co/storage/v1/object/public/featured-images/e79999b7-0539-4ec0-8624-ec607ed948fe/workflows/b1d62d52-84db-48e4-be7f-29ca9776acda.webp"},{"id":"dab7fb6e-d513-415a-9180-48f780a2eda0","title":"Tang Soo Do for Beginners: First Class Basics","slug":"tang-soo-do-for-beginners","excerpt":"Tang Soo Do for beginners builds confidence, focus, fitness, and practical self-defense through structured local karate training and support today.","content":null,"date":"October 8, 2026","isoDate":"2026-10-08T03:51:47.808+00:00","image":"https://afocirmbqdxnkyescnev.supabase.co/storage/v1/object/public/featured-images/e79999b7-0539-4ec0-8624-ec607ed948fe/workflows/ec9b008a-35a9-4190-bbbd-27fc009482a9.webp"},{"id":"02ddea62-3ad4-4a83-b34b-9d36d1355fd7","title":"Adult Beginner Karate Classes: Start Strong","slug":"adult-beginner-karate-classes","excerpt":"Adult beginner karate classes build fitness, confidence, and practical skills in a supportive setting. Start training with clear goals and expert guidance.","content":null,"date":"October 7, 2026","isoDate":"2026-10-07T03:53:42.684+00:00","image":"https://afocirmbqdxnkyescnev.supabase.co/storage/v1/object/public/featured-images/e79999b7-0539-4ec0-8624-ec607ed948fe/workflows/f77743e6-093d-44bf-9841-3bdefee65c64.webp"},{"id":"fbfb4f1a-559f-4fda-a832-7c7aea779205","title":"Karate Classes for Kids Lansdale Families Trust","slug":"karate-classes-for-kids-lansdale","excerpt":"Karate classes for kids Lansdale parents choose can build focus, confidence, respect, fitness, and practical self-defense in a supportive local setting.","content":null,"date":"October 6, 2026","isoDate":"2026-10-06T03:50:48.733+00:00","image":"https://afocirmbqdxnkyescnev.supabase.co/storage/v1/object/public/featured-images/e79999b7-0539-4ec0-8624-ec607ed948fe/workflows/bc28d054-e65f-47d4-ac22-62c11aa446dd.webp"},{"id":"e3f4282d-6630-484a-8d57-67b486386143","title":"Choosing a Karate School for Lasting Growth","slug":"choosing-a-karate-school","excerpt":"A karate school should build skill, confidence, and character. Learn what Lansdale families should look for in instruction, community, and growth nearby.","content":null,"date":"October 5, 2026","isoDate":"2026-10-05T03:44:34.705+00:00","image":"https://afocirmbqdxnkyescnev.supabase.co/storage/v1/object/public/featured-images/e79999b7-0539-4ec0-8624-ec607ed948fe/workflows/f5403598-f048-44b6-8c66-db8b2722b365.webp"},{"id":"5bb46d99-1356-44fd-a2c0-9ecb58fe2037","title":"Karate Lessons That Build Skill and Character","slug":"karate-lessons-build-skill-character","excerpt":"Structured karate lessons in Lansdale help children and adults build confidence, focus, fitness, self-defense skills, and lasting respect through practice.","content":null,"date":"October 4, 2026","isoDate":"2026-10-04T18:40:19.648+00:00","image":"https://afocirmbqdxnkyescnev.supabase.co/storage/v1/object/public/featured-images/6afeb405-74c1-4114-b0fa-ee3487de83d6/5bb46d99-1356-44fd-a2c0-9ecb58fe2037.webp"}];
  var SORO_TOKEN = '19ba0316-2014-49f1-a5da-b22be476836f';
  var SORO_API_BASE = 'https://app.trysoro.com';
  var SORO_T = {"allArticles":"All Articles","backToAll":"Back to all articles","loading":"Loading article...","error":"Failed to load article. Please try again.","empty":"Published articles may take up to 60 minutes to appear here.","blogArticles":"Blog articles","pagination":"Pagination","previousPage":"Previous page","nextPage":"Next page","page":"Page {n}"};
  
  // Content cache (loaded on demand)
  var contentCache = {};
  
  // Pre-populate cache with any server-included content (for SEO deep links)
  SORO_ARTICLES.forEach(function(article) {
    if (article.content) {
      contentCache[article.id] = article.content;
    }
  });
  
  // Find the container
  var container = document.getElementById('soro-blog');
  if (!container) {
    console.error('Soro Blog: No element with id="soro-blog" found');
    return;
  }
  
  // Save the original page title so we can restore it
  var originalTitle = document.title;
  var originalDescription = '';
  var descMeta = document.querySelector('meta[name="description"]');
  if (descMeta) originalDescription = descMeta.getAttribute('content') || '';
  
  // Detect if running inside a srcdoc iframe (website builders like GoDaddy, Wix, etc.)
  var isSrcdocIframe = false;
  var parentBaseUrl = null;
  try {
    isSrcdocIframe = window.location.href === 'about:srcdoc' || 
                     window.location.pathname === '/srcdoc' ||
                     (window.self !== window.top && window.location.origin === 'null');
  } catch(e) {
    isSrcdocIframe = true; // cross-origin means we're in an iframe
  }
  
  // Try to get the parent page URL for building article links
  if (isSrcdocIframe) {
    try {
      var pUrl = new URL(window.parent.location.href);
      pUrl.searchParams.delete('post');
      parentBaseUrl = pUrl.origin + pUrl.pathname + pUrl.search;
    } catch(e) {
      parentBaseUrl = null;
    }
  }
  
  // Detect deep-linked article from URL query param: ?post=slug
  function getRequestedSlug() {
    try {
      if (isSrcdocIframe) {
        // In srcdoc iframe, check parent URL if accessible
        try {
          var params = new URLSearchParams(window.parent.location.search);
          return params.get('post') || null;
        } catch(e) {
          return null;
        }
      }
      var params = new URLSearchParams(window.location.search);
      return params.get('post') || null;
    } catch(e) {
      return null;
    }
  }
  
  // Build article URL with query param (preserves existing params)
  function getArticleUrl(slug) {
    try {
      if (isSrcdocIframe && parentBaseUrl) {
        // Build full parent page URL with ?post= param so new tab works
        var sep = parentBaseUrl.indexOf('?') !== -1 ? '&' : '?';
        return parentBaseUrl + sep + 'post=' + encodeURIComponent(slug);
      }
      if (isSrcdocIframe) {
        return '#post=' + encodeURIComponent(slug);
      }
      var url = new URL(window.location.href);
      url.searchParams.set('post', slug);
      return url.pathname + url.search;
    } catch(e) {
      return '#post=' + encodeURIComponent(slug);
    }
  }
  
  // Get the base list URL (current page without post param)
  function getListUrl() {
    try {
      if (isSrcdocIframe && parentBaseUrl) {
        return parentBaseUrl;
      }
      if (isSrcdocIframe) {
        return '#';
      }
      var url = new URL(window.location.href);
      url.searchParams.delete('post');
      var search = url.search;
      return url.pathname + (search ? search : '');
    } catch(e) {
      return window.location.pathname;
    }
  }
  
  // Safe pushState wrapper (fails silently in srcdoc iframes)
  function safePushState(state, title, url) {
    if (isSrcdocIframe) return;
    try {
      history.pushState(state, title, url);
    } catch(e) {
      // pushState not available (srcdoc iframe, cross-origin, etc.)
    }
  }
  
  // Update page meta tags for SEO (title, description, canonical, JSON-LD)
  // Skip in srcdoc iframes since we can't meaningfully update the parent page
  function updatePageMeta(article) {
    if (isSrcdocIframe) return;
    if (article) {
      // Article view: set article-specific meta
      document.title = article.title + ' | ' + SORO_BLOG_TITLE;
      
      var desc = document.querySelector('meta[name="description"]');
      if (!desc) {
        desc = document.createElement('meta');
        desc.setAttribute('name', 'description');
        document.head.appendChild(desc);
      }
      desc.setAttribute('content', article.excerpt);
      
      // Set canonical URL to the article deep link
      var canonical = document.querySelector('link[rel="canonical"][data-soro]');
      if (!canonical) {
        canonical = document.createElement('link');
        canonical.setAttribute('rel', 'canonical');
        canonical.setAttribute('data-soro', 'true');
        document.head.appendChild(canonical);
      }
      canonical.setAttribute('href', window.location.href);
      
      // JSON-LD: single BlogPosting
      injectJsonLd({
        '@context': 'https://schema.org',
        '@type': 'BlogPosting',
        'headline': article.title,
        'description': article.excerpt,
        'datePublished': article.isoDate,
        'image': article.image || undefined,
        'url': window.location.href
      });
    } else {
      // List view: restore original meta
      document.title = originalTitle;
      
      var desc = document.querySelector('meta[name="description"]');
      if (desc && originalDescription) {
        desc.setAttribute('content', originalDescription);
      }
      
      // Remove soro canonical
      var canonical = document.querySelector('link[rel="canonical"][data-soro]');
      if (canonical) canonical.remove();
      
      // JSON-LD: Blog with all articles
      var blogPosting = SORO_ARTICLES.map(function(a) {
        var item = {
          '@type': 'BlogPosting',
          'headline': a.title,
          'description': a.excerpt,
          'datePublished': a.isoDate,
          'url': window.location.origin + getArticleUrl(a.slug)
        };
        if (a.image) item.image = a.image;
        return item;
      });
      
      injectJsonLd({
        '@context': 'https://schema.org',
        '@type': 'Blog',
        'name': SORO_BLOG_TITLE,
        'blogPost': blogPosting
      });
    }
  }
  
  // Inject styles
  var styleId = 'soro-blog-styles-light';
  if (!document.getElementById(styleId)) {
    var style = document.createElement('style');
    style.id = styleId;
    style.textContent = `
      .soro-blog {
        font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
        max-width: 800px;
        margin: 0 auto;
        color: #1a1a1a;
        background: transparent;
      }
      .soro-blog * {
        box-sizing: border-box;
      }
      .soro-blog a {
        text-decoration: none;
        color: inherit;
      }
      .soro-blog-header {
        display: none;
        align-items: center;
        margin-bottom: 16px;
      }
      .soro-blog-back {
        display: inline-flex;
        align-items: center;
        gap: 6px;
        background: transparent;
        border: 1px solid #e5e5e5;
        color: #666;
        cursor: pointer;
        font-size: 14px;
        padding: 8px 16px;
        border-radius: 6px;
        transition: background 0.2s, border-color 0.2s;
        text-decoration: none;
      }
      .soro-blog-back:hover {
        background: #f5f5f5;
        border-color: #d0d0d0;
      }
      .soro-blog-list {
        display: flex;
        flex-direction: column;
        gap: 24px;
      }
      .soro-blog-card {
        display: flex;
        gap: 20px;
        padding: 20px;
        background: transparent;
        border: 1px solid #e5e5e5;
        border-radius: 12px;
        cursor: pointer;
        transition: box-shadow 0.2s, border-color 0.2s;
        text-decoration: none;
        color: inherit;
      }
      .soro-blog-card:hover {
        box-shadow: 0 4px 12px rgba(0,0,0,0.08);
        border-color: #d0d0d0;
      }
      .soro-blog-card-image {
        width: 140px;
        height: 100px;
        object-fit: cover;
        border-radius: 8px;
        flex-shrink: 0;
      }
      .soro-blog-card-content {
        flex: 1;
        min-width: 0;
      }
      .soro-blog-card-title {
        font-size: 18px;
        font-weight: 600;
        margin: 0 0 8px 0;
        line-height: 1.3;
        color: #1a1a1a;
      }
      h2.soro-blog-card-title {
        font-size: 18px;
      }
      .soro-blog-card-excerpt {
        font-size: 14px;
        color: #666;
        margin: 0 0 12px 0;
        line-height: 1.5;
        display: -webkit-box;
        -webkit-line-clamp: 2;
        -webkit-box-orient: vertical;
        overflow: hidden;
      }
      .soro-blog-card-date {
        font-size: 12px;
        color: #999;
      }
      .soro-blog-article {
        display: none;
      }
      .soro-blog-article.visible {
        display: block;
      }
      .soro-blog-article-title {
        font-size: 32px;
        font-weight: 700;
        margin: 0 0 12px 0;
        line-height: 1.2;
        color: #1a1a1a;
      }
      .soro-blog-article-date {
        font-size: 14px;
        color: #666;
        margin-bottom: 24px;
      }
      .soro-blog-article-image {
        width: 100%;
        max-height: 400px;
        object-fit: cover;
        border-radius: 12px;
        margin-bottom: 24px;
      }
      .soro-blog-article-content {
        font-size: 16px;
        line-height: 1.7;
        color: #1a1a1a;
      }
      .soro-blog-article-content h1,
      .soro-blog-article-content h2,
      .soro-blog-article-content h3 {
        margin-top: 32px;
        margin-bottom: 16px;
        line-height: 1.3;
        color: #1a1a1a;
      }
      .soro-blog-article-content h2 {
        font-size: 24px;
      }
      .soro-blog-article-content h3 {
        font-size: 20px;
      }
      .soro-blog-article-content p {
        margin: 0 0 16px 0;
      }
      .soro-blog-article-content ul,
      .soro-blog-article-content ol {
        margin: 0 0 16px 0;
        padding-left: 24px;
        padding-right: 0;
      }
      .soro-blog-article-content li {
        margin-bottom: 8px;
      }
      .soro-blog-article-content a {
        color: #0066cc;
      }
      .soro-blog-article-content img {
        max-width: 100%;
        height: auto;
        border-radius: 8px;
      }
      .soro-blog-empty {
        text-align: center;
        padding: 48px 24px;
        color: #666;
      }
      .soro-blog-loading {
        display: flex;
        align-items: center;
        justify-content: center;
        padding: 48px 24px;
        color: #666;
      }
      .soro-blog-spinner {
        width: 24px;
        height: 24px;
        border: 2px solid #e5e5e5;
        border-top-color: #1a1a1a;
        border-radius: 50%;
        animation: soro-spin 0.8s linear infinite;
        margin-right: 12px;
      }
      @keyframes soro-spin {
        to { transform: rotate(360deg); }
      }
      .soro-blog-error {
        text-align: center;
        padding: 48px 24px;
        color: #dc2626;
      }
      @media (max-width: 600px) {
        .soro-blog-card {
          flex-direction: column;
          gap: 12px;
        }
        .soro-blog-card-image {
          width: 100%;
          height: 180px;
        }
        .soro-blog-article-title {
          font-size: 24px;
        }
      }
    `;
    document.head.appendChild(style);
  }
  
  // Inject or update JSON-LD structured data
  function injectJsonLd(data) {
    var jsonLdId = 'soro-blog-jsonld';
    var existing = document.getElementById(jsonLdId);
    if (existing) existing.remove();
    
    var script = document.createElement('script');
    script.id = jsonLdId;
    script.type = 'application/ld+json';
    script.textContent = JSON.stringify(data);
    document.head.appendChild(script);
  }
  
  // State
  var currentView = 'list';
  var currentArticle = null;
  var isLoading = false;
  
  // Fetch article content on demand
  function fetchArticleContent(articleId, callback) {
    // Check cache first (includes server-pre-rendered content for deep links)
    if (contentCache[articleId]) {
      callback(null, contentCache[articleId]);
      return;
    }
    
    var url = SORO_API_BASE + '/api/embed/' + SORO_TOKEN + '/article/' + articleId;
    
    fetch(url)
      .then(function(response) {
        if (!response.ok) throw new Error('Failed to load article');
        return response.json();
      })
      .then(function(data) {
        contentCache[articleId] = data.content;
        callback(null, data.content);
      })
      .catch(function(error) {
        callback(error, null);
      });
  }
  
  // Render article list — cards are real <a> links for SEO crawlability
  function renderList() {
    if (SORO_ARTICLES.length === 0) {
      return '<div class="soro-blog-empty"><p>' + SORO_T.empty + '</p></div>';
    }
    
    var html = '<section class="soro-blog-list" role="feed" aria-label="' + SORO_T.blogArticles + '">';
    SORO_ARTICLES.forEach(function(article) {
      var articleUrl = getArticleUrl(article.slug);
      var targetAttr = isSrcdocIframe ? ' target="_blank" rel="noopener"' : '';
      html += '<a href="' + articleUrl + '"' + targetAttr + ' class="soro-blog-card" data-slug="' + escapeHtml(article.slug) + '" itemscope itemtype="https://schema.org/BlogPosting">';
      if (article.image) {
        html += '<img class="soro-blog-card-image" src="' + article.image + '" alt="' + escapeHtml(article.title) + '" loading="lazy" itemprop="image">';
      }
      html += '<div class="soro-blog-card-content">';
      html += '<h2 class="soro-blog-card-title" itemprop="headline">' + escapeHtml(article.title) + '</h2>';
      html += '<p class="soro-blog-card-excerpt" itemprop="description">' + escapeHtml(article.excerpt) + '</p>';
      html += '<time class="soro-blog-card-date" datetime="' + article.isoDate + '" itemprop="datePublished">' + article.date + '</time>';
      html += '</div></a>';
    });
    html += '</section>';
    return html;
  }
  
  function renderArticle(article, content) {
    var html = '<article class="soro-blog-article visible" itemscope itemtype="https://schema.org/BlogPosting">';
    html += '<header>';
    html += '<h1 class="soro-blog-article-title" itemprop="headline">' + escapeHtml(article.title) + '</h1>';
    html += '<time class="soro-blog-article-date" datetime="' + article.isoDate + '" itemprop="datePublished">' + article.date + '</time>';
    html += '</header>';
    if (article.image) {
      html += '<img class="soro-blog-article-image" src="' + article.image + '" alt="' + escapeHtml(article.title) + '" itemprop="image">';
    }
    html += '<div class="soro-blog-article-content" itemprop="articleBody">' + content + '</div>';
    html += '</article>';
    return html;
  }
  
  function renderLoading() {
    return '<div class="soro-blog-loading"><div class="soro-blog-spinner"></div>' + SORO_T.loading + '</div>';
  }
  
  function renderError() {
    return '<div class="soro-blog-error"><p>' + SORO_T.error + '</p></div>';
  }
  
  function escapeHtml(str) {
    var div = document.createElement('div');
    div.textContent = str;
    return div.innerHTML;
  }
  
  // Make all links inside article content open in a new tab
  // This is critical for embeds running on external sites or inside iframes
  function processContentLinks(parentEl) {
    var links = parentEl.querySelectorAll('.soro-blog-article-content a');
    for (var i = 0; i < links.length; i++) {
      links[i].setAttribute('target', '_blank');
      links[i].setAttribute('rel', 'noopener noreferrer');
    }
  }
  
  // Navigate to article view (updates URL without full reload)
  function showArticle(slug) {
    var article = null;
    for (var i = 0; i < SORO_ARTICLES.length; i++) {
      if (SORO_ARTICLES[i].slug === slug) {
        article = SORO_ARTICLES[i];
        break;
      }
    }
    if (!article) {
      // Unknown slug (deleted or unpublished article) — show the list instead of a blank widget
      showList();
      return;
    }

    currentArticle = article;
    currentView = 'article';
    
    // Update URL (pushState so back button works)
    var articleUrl = getArticleUrl(slug);
    if (!isSrcdocIframe && window.location.pathname + window.location.search !== articleUrl) {
      safePushState({ post: slug }, '', articleUrl);
    }
    
    // Update page meta for SEO
    updatePageMeta(article);
    
    var header = container.querySelector('.soro-blog-header');
    var content = container.querySelector('.soro-blog-content');
    header.style.display = 'flex';
    
    // Check if content is already available (pre-rendered or cached)
    if (contentCache[article.id]) {
      content.innerHTML = renderArticle(article, contentCache[article.id]);
      processContentLinks(content);
    } else {
      // Show loading, then fetch
      isLoading = true;
      content.innerHTML = renderLoading();
      
      fetchArticleContent(article.id, function(error, articleContent) {
        isLoading = false;
        if (error) {
          content.innerHTML = renderError();
        } else {
          content.innerHTML = renderArticle(article, articleContent);
          processContentLinks(content);
        }
      });
    }
    
    container.scrollIntoView({ behavior: 'smooth', block: 'start' });
  }
  
  // Navigate to list view
  function showList() {
    currentView = 'list';
    currentArticle = null;
    isLoading = false;
    
    // Update URL
    var listUrl = getListUrl();
    if (!isSrcdocIframe && window.location.pathname + window.location.search !== listUrl) {
      safePushState({ post: null }, '', listUrl);
    }
    
    // Restore page meta
    updatePageMeta(null);
    
    var header = container.querySelector('.soro-blog-header');
    var content = container.querySelector('.soro-blog-content');
    header.style.display = 'none';
    content.innerHTML = renderList();
    
    // Intercept card link clicks
    var cards = content.querySelectorAll('.soro-blog-card');
    cards.forEach(function(card) {
      card.addEventListener('click', function(e) {
        e.preventDefault();
        var slug = card.getAttribute('data-slug');
        if (isSrcdocIframe) {
          // In srcdoc iframe, open article in a new tab with real URL
          var url = getArticleUrl(slug);
          window.open(url, '_blank');
        } else {
          showArticle(slug);
        }
      });
    });
  }
  
  // Initial render
  var backArrow = '<svg width="16" height="16" viewBox="0 0 24 24" fill="none" stroke="currentColor" stroke-width="2"><path d="M19 12H5M12 19l-7-7 7-7"/></svg>';
  container.innerHTML = '<div class="soro-blog">' +
    '<div class="soro-blog-header" style="display: none;">' +
      '<a href="' + getListUrl() + '" class="soro-blog-back" aria-label="' + SORO_T.backToAll + '">' +
        backArrow +
        SORO_T.allArticles +
      '</a>' +
    '</div>' +
    '<div class="soro-blog-content"></div>' +
  '</div>';
  var root = container.querySelector('.soro-blog');

  // Handle browser back/forward buttons (not applicable in srcdoc iframes).
  // A single-page site can remove the widget without a reload and add it again later, running
  // this script anew. A widget no longer on the page stops listening, so it neither fails on its
  // emptied element nor changes the title of the page the visitor is on.
  if (!isSrcdocIframe) {
    window.addEventListener('popstate', function onPopState() {
      if (!root.isConnected) {
        window.removeEventListener('popstate', onPopState);
        return;
      }
      var slug = getRequestedSlug();
      if (slug) {
        showArticle(slug);
      } else {
        showList();
      }
    });
  }

  // Add back button handler (intercept for SPA navigation)
  container.querySelector('.soro-blog-back').addEventListener('click', function(e) {
    e.preventDefault();
    showList();
  });
  
  // Check if URL has a deep-linked article on initial load
  var initialSlug = getRequestedSlug();
  if (initialSlug) {
    showArticle(initialSlug);
  } else {
    showList();
  }
})();