/* CLS (Cumulative Layout Shift) Fixes for NiftyButtons */
/* Score: 0.28 -> Target: < 0.1 */

/* 1. Font Loading Optimization */
@font-face {
    font-family: 'system-ui-fallback';
    src: local('system-ui'), local('-apple-system'), local('BlinkMacSystemFont'), local('Segoe UI'), local('Roboto');
    font-display: swap;
}

/* 2. SVG Icon Container Stabilization */
.icon-tile svg,
.niftybutton-icon svg,
svg[class*="niftybutton-"] {
    display: block !important;
    width: 32px !important;
    height: 32px !important;
    aspect-ratio: 1 / 1 !important;
    contain: layout style paint !important;
}

/* 3. Icon Tile Grid Layout Stabilization */
.icon-shape-picker {
    display: grid !important;
    grid-template-columns: repeat(auto-fill, minmax(90px, 1fr)) !important;
    gap: 0.5rem !important;
    contain: layout !important;
}

.icon-tile {
    min-height: 90px !important;
    aspect-ratio: 1 / 1.2 !important;
    contain: layout style !important;
}

.icon-tile label {
    min-height: 85px !important;
    display: flex !important;
    flex-direction: column !important;
    align-items: center !important;
    justify-content: center !important;
}

/* 4. Sticky Container Pre-allocation */
.sticky-container {
    min-height: 120px !important;
    contain: layout !important;
}

.icon-preview {
    min-height: 100px !important;
}

/* 5. Button Container Stabilization */
.button-container,
.icon-buttons-container {
    min-height: 60px !important;
    contain: layout style !important;
}

/* 6. Hero Section Layout Stability */
.hero-section {
    min-height: 400px !important;
    contain: layout !important;
}

/* 7. Section Headers */
.section-header {
    min-height: 60px !important;
    contain: layout !important;
}

/* 8. Prevent Layout Shifts from Dynamic Content */
.icon-section-content {
    min-height: 200px !important;
    contain: layout !important;
    will-change: height !important;
    transition: height 0.3s ease-out !important;
}

/* 9. Form Elements Stability */
input[type="text"],
input[type="url"],
textarea,
select {
    min-height: 40px !important;
    box-sizing: border-box !important;
}

/* 10. Footer Pre-allocation */
footer {
    min-height: 300px !important;
    contain: layout !important;
}

/* 11. Prevent Flash of Unstyled Content (FOUC) */
.icon-shape-picker > div {
    opacity: 1 !important;
    transition: opacity 0.2s ease-in !important;
}

/* 12. Loading State for Dynamic Content */
.loading {
    min-height: 100px !important;
    background: linear-gradient(90deg,
        rgba(255,255,255,0.1) 0%,
        rgba(255,255,255,0.2) 50%,
        rgba(255,255,255,0.1) 100%);
    background-size: 200% 100%;
    animation: shimmer 1.5s infinite;
}

@keyframes shimmer {
    0% { background-position: -200% center; }
    100% { background-position: 200% center; }
}

/* 13. Prevent Shifts from Scrollbars */
html {
    overflow-y: scroll !important;
    scrollbar-gutter: stable !important;
}

/* 14. Image and Media Elements */
img,
video,
iframe {
    aspect-ratio: attr(width) / attr(height);
    max-width: 100%;
    height: auto;
}

/* 15. Typography Stability */
h1, h2, h3, h4, h5, h6 {
    line-height: 1.2 !important;
    margin-top: 1em !important;
    margin-bottom: 0.5em !important;
}

p {
    line-height: 1.6 !important;
    margin-bottom: 1em !important;
}

/* 16. Prevent Icon Size Variations */
.icon-tile svg path,
.icon-tile svg rect,
.icon-tile svg circle {
    vector-effect: non-scaling-stroke !important;
}

/* 17. Collapsible Sections Animation */
.collapsed {
    overflow: hidden !important;
    max-height: 0 !important;
    transition: max-height 0.3s cubic-bezier(0, 1, 0, 1) !important;
}

.expanded {
    max-height: 9999px !important;
    transition: max-height 0.3s cubic-bezier(1, 0, 1, 0) !important;
}

/* 18. Mobile Responsive Stability */
@media (max-width: 768px) {
    .icon-shape-picker {
        grid-template-columns: repeat(auto-fill, minmax(80px, 1fr)) !important;
    }

    .icon-tile {
        min-height: 80px !important;
    }
}

/* 19. Lazy Loading Placeholders */
[loading="lazy"] {
    background: rgba(255,255,255,0.05);
    min-height: 32px;
    min-width: 32px;
}

/* 20. Critical Web Font Loading */
.font-loading * {
    font-family: 'system-ui-fallback', system-ui, -apple-system, sans-serif !important;
}

/* 21. Prevent CLS from Web Fonts */
body {
    font-synthesis: none;
    text-rendering: optimizeLegibility;
    -webkit-font-smoothing: antialiased;
    -moz-osx-font-smoothing: grayscale;
}

/* 22. Reserve Space for Dynamic UI Elements */
.code-output,
.preview-container {
    min-height: 200px !important;
    contain: layout style !important;
}

/* 23. Optimize Reflow Performance */
* {
    will-change: auto !important;
}

.animating {
    will-change: transform, opacity !important;
}

/* 24. Prevent Layout Thrashing */
.batch-update {
    contain: strict !important;
    isolation: isolate !important;
}