/* 
 * URGENT CLS FIX - Add this to critical.css or top of styles.css
 * Purpose: Fix Cumulative Layout Shift from 0.96 to <0.1
 * Date: December 5, 2025
 */

/* 1. Prevent font loading shifts */
@font-face {
  font-family: system-font;
  font-style: normal;
  font-weight: 300 900;
  font-display: swap; /* CRITICAL: Prevents invisible text flash */
  src: local("system-ui");
}

body {
  font-display: swap;
}

/* 2. Reserve space for logo to prevent shift */
.logo-image,
.logo img {
  /* Logo dimensions are controlled by styles.css */
  /* width: auto; height: 120px; per main stylesheet */
  display: block;
}

/* 3. Reserve space for AdSense units */
.ad-container,
.adsbygoogle {
  min-height: 280px; /* Standard display ad height */
  width: 100%;
  background: #f8f9fa;
  border: 1px solid #e0e0e0;
  display: block;
  margin: 20px 0;
}

.ad-container::before {
  content: "Advertisement";
  display: block;
  text-align: center;
  font-size: 10px;
  color: #999;
  padding: 5px;
}

/* Mobile ad units */
@media (max-width: 768px) {
  .ad-container,
  .adsbygoogle {
    min-height: 250px;
  }
}

/* 4. Prevent dynamic content shifts */
.ip-result,
.result-container,
.info-container {
  min-height: 100px; /* Reserve space before content loads */
  transition: none; /* Remove animations that cause CLS */
}

/* 5. Fix image aspect ratios */
img:not([width]):not([height]) {
  aspect-ratio: attr(width) / attr(height);
  height: auto;
}

/* Specific image containers */
.tool-icon,
.feature-icon {
  width: 64px;
  height: 64px;
  display: flex;
  align-items: center;
  justify-content: center;
}

/* 6. Prevent header shifts on scroll */
.header {
  height: 44px; /* Fixed height - compact navbar */
  min-height: 44px;
}

.header-content {
  height: 44px;
  min-height: 44px;
}

/* 7. Reserve space for loading states */
.loading-skeleton {
  background: linear-gradient(90deg, #f0f0f0 25%, #e0e0e0 50%, #f0f0f0 75%);
  background-size: 200% 100%;
  animation: loading 1.5s infinite;
  min-height: 40px;
}

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

/* 8. Fix button and form element shifts */
button,
input,
select,
textarea {
  font-family: inherit;
  font-size: 100%;
  line-height: 1.15;
  margin: 0;
}

button {
  min-height: 44px; /* Touch-friendly + prevents shift */
}

/* 9. Prevent iframe shifts (embeds, maps, etc.) */
iframe {
  aspect-ratio: 16 / 9;
  width: 100%;
  height: auto;
}

/* 10. Fix navigation shifts */
.navbar {
  min-height: 70px;
  height: 70px;
}

.nav-links {
  display: flex;
  gap: 20px;
  height: 70px;
  align-items: center;
}

/* 11. Prevent table layout shifts */
table {
  table-layout: fixed;
  width: 100%;
}

/* 12. Fix grid/flex container shifts */
.grid,
.features-grid,
.tools-grid {
  display: grid;
  gap: 20px;
  grid-template-columns: repeat(auto-fit, minmax(280px, 1fr));
}

/* Ensure minimum height for grid items */
.grid > *,
.features-grid > *,
.tools-grid > * {
  min-height: 200px;
}

/* 13. Prevent modal/popup shifts */
.modal,
.popup,
.tooltip {
  position: fixed; /* Not absolute, prevents page reflow */
  will-change: opacity; /* GPU acceleration, smoother */
}

/* 14. Critical: Prevent invisible content from causing shifts */
[hidden],
[style*="display: none"],
.hidden {
  display: none !important;
  visibility: hidden !important;
  height: 0 !important;
  overflow: hidden !important;
}

/* 15. Ensure smooth content loading */
.content-loading {
  opacity: 0;
  transform: translateY(0); /* Don't use translateY for loading states */
  transition: opacity 0.3s ease;
}

.content-loaded {
  opacity: 1;
}

/* 16. Fix specific tool page layouts */
.speed-test-container,
.ip-lookup-container,
.vpn-detection-container {
  min-height: 400px;
  display: flex;
  flex-direction: column;
}

/* 17. Prevent chart/graph shifts */
.chart-container,
canvas {
  aspect-ratio: 16 / 9;
  max-width: 100%;
  height: auto !important; /* Override inline styles */
}

/* 18. Fix footer position (prevent push) */
.footer {
  margin-top: auto; /* Push to bottom without causing shift */
}

/* 19. Critical width containment */
* {
  max-width: 100%;
}

img,
video,
iframe,
embed {
  max-width: 100%;
  height: auto;
}

/* 20. Prevent text wrapping shifts */
h1,
h2,
h3,
h4,
h5,
h6 {
  overflow-wrap: break-word;
  word-wrap: break-word;
  hyphens: auto;
}

/* 21. Fix mobile menu shifts */
@media (max-width: 768px) {
  .mobile-menu {
    position: fixed;
    top: 70px; /* Below header */
    left: 0;
    right: 0;
    transform: translateX(-100%);
    transition: transform 0.3s ease;
  }

  .mobile-menu.open {
    transform: translateX(0);
  }

  /* Prevent body scroll when menu open */
  body.menu-open {
    overflow: hidden;
    position: fixed;
    width: 100%;
  }
}

/* 22. Prevent web font loading shifts (CRITICAL) */
.wf-loading body {
  opacity: 0; /* Hide during font load */
}

.wf-active body,
.wf-inactive body {
  opacity: 1;
  transition: opacity 0.2s;
}

/* 23. Reserve space for dynamic counters/stats */
.stat-value,
.counter {
  min-width: 60px;
  display: inline-block;
  text-align: center;
}

/* 24. Fix scroll-triggered animations (they cause CLS) */
.animate-on-scroll {
  /* Remove transform-based animations */
  opacity: 0.3; /* Just fade in */
}

.animate-on-scroll.in-view {
  opacity: 1;
  transition: opacity 0.3s ease;
}

/* 25. MOST IMPORTANT: Content visibility optimization */
.below-fold {
  content-visibility: auto;
  contain-intrinsic-size: 0 500px; /* Reserve approximate height */
}

/* 
 * TESTING CHECKLIST:
 * [ ] Run PageSpeed Insights before/after
 * [ ] Test on mobile device
 * [ ] Check all tool pages
 * [ ] Verify ads don't shift content
 * [ ] Test with slow 3G connection
 * [ ] Check header on scroll
 * [ ] Verify images load correctly
 * 
 * Target: CLS < 0.1 (Currently 0.96)
 */
