// snippets/Loading/Inline-CSS-Info-and-Size.js | sha256:4248d11299b3cb45 | https://github.com/nucliweb/webperf-snippets/blob/main/snippets/Loading/Inline-CSS-Info-and-Size.js (()=>{function e(e){if(null==e||Number.isNaN(e))return"-";if(0===e)return"0 B";const t=["B","KB","MB","GB"],s=Math.max(0,Math.min(Math.floor(Math.log(e)/Math.log(1024)),t.length-1));return(e/Math.pow(1024,s)).toFixed(1)+" "+t[s]}const t=Array.from(document.querySelectorAll("style"));if(0===t.length)return{script:"Inline-CSS-Info-and-Size",status:"ok",count:0,items:[],issues:[]};const s=t.map((e,t)=>{const s=e.innerHTML,a=new Blob([s]).size,n=e.parentElement?.tagName.toLowerCase()||"unknown",i=null!==e.closest("head"),o=(s.match(/\{[^}]*\}/g)||[]).length,l=s.replace(/\s+/g," ").trim().slice(0,80),r=/@media/i.test(s),c=/@keyframes/i.test(s);return{index:t+1,element:e,size:a,sizeKB:(d=a,(d/1024).toFixed(2)),parent:n,inHead:i,ruleCount:o,preview:l+(s.length>80?"...":""),hasMediaQueries:r,hasKeyframes:c,content:s};var d}),a=s.reduce((e,t)=>e+t.size,0),n=s.reduce((e,t)=>e+t.ruleCount,0),i=14336,o=new Map;s.forEach(e=>{const t=e.content.replace(/\s+/g," ").trim();o.has(t)||o.set(t,[]),o.get(t).push(e.index)});const l=Array.from(o.entries()).filter(([,e])=>e.length>1).map(([,e])=>e);let r,c;a<=7168?(r="Good",c="#22c55e"):a<=i?(r="Acceptable",c="#f59e0b"):(r="Too Large",c="#ef4444");const d=Math.min(a/i*100,100),u=Math.round(d/5);"█".repeat(u),"░".repeat(20-u),s.map(t=>({"#":t.index,Location:t.inHead?"head":"body",Size:e(t.size),Rules:t.ruleCount,"Media Queries":t.hasMediaQueries?"Yes":"-",Animations:t.hasKeyframes?"Yes":"-",Preview:t.preview})),s.forEach(e=>{});const g=[];a>i&&g.push({code:"over-budget",type:"size",message:`Total inline CSS (${e(a)}) exceeds 14 KB critical budget`,suggestion:"Move non-critical styles to external stylesheet"}),l.length>0&&g.push({code:"duplicate-blocks",type:"duplicate",message:`Found ${l.length} duplicated style block(s)`,details:l.map(e=>`Tags #${e.join(", #")} have identical content`),suggestion:"Consolidate duplicate styles into a single block"});const h=s.filter(e=>e.size>5120);h.length>0&&g.push({code:"large-blocks",type:"large",message:`${h.length} style block(s) over 5 KB`,details:h.map(t=>`Tag #${t.index}: ${e(t.size)}`),suggestion:"Consider moving large blocks to external cached stylesheets"});const p=s.filter(e=>!e.inHead);return p.length>0&&g.push({code:"styles-in-body",type:"location",message:`${p.length} style block(s) in `,details:p.map(e=>`Tag #${e.index}`),suggestion:"Move styles to or use external stylesheets for better parsing"}),g.length>0&&g.forEach(e=>{e.details&&e.details.forEach(e=>{})}),{script:"Inline-CSS-Info-and-Size",status:"ok",count:s.length,details:{totalSizeBytes:a,totalRules:n,criticalBudgetBytes:i,duplicateBlocks:l.length,bodyStyleBlocks:s.filter(e=>!e.inHead).length},items:s.map(e=>({index:e.index,sizeBytes:e.size,parent:e.parent,inHead:e.inHead,ruleCount:e.ruleCount,hasMediaQueries:e.hasMediaQueries,hasKeyframes:e.hasKeyframes})),issues:g.map(e=>({code:e.code,severity:"warning",message:e.message}))}})();