/**
 * Capital FM Ad Styles - COMPLETE CORRECTED VERSION
 * Includes top ad container + all fixes
 * 
 * PHP outputs:
 * <div class="cfm-ad cfm-ad--{slot_name}">
 *   <div id="div-gpt-{slot}-{counter}" data-ad-slot="..."></div>
 * </div>
 * 
 * Location: /wp-content/themes/capital-fm/css/ads.css
 * Version: 2.1 - Added mobile anchor unit + second in-article spacing
 */

/* ============================================================================
   ARTICLE TOP AD CONTAINER (Above article)
   ============================================================================ */

   .article-top-ad-container {
    max-width: 100%;
    margin: 0 auto 0px;
    background: #f8f9fa;
    border-bottom: 1px solid #e9ecef;
    text-align: center;
    overflow: hidden;
    max-width: 1400px;
}

.article-top-ad-container .cfm-ad {
    margin: 0 auto;
    max-width: 1200px;
}

.article-top-ad-container .cfm-ad > div {
    min-height: 90px;
}

/* ============================================================================
   BASE AD CONTAINER STYLES
   ============================================================================ */

.cfm-ad {
    margin: 20px auto;
    text-align: center;
    position: relative;
    overflow: hidden;
    max-width: 100%;
    clear: both;
}

.cfm-ad > div {
    min-height: 90px;
    display: flex;
    align-items: center;
    justify-content: center;
    background: #f5f5f5;
    position: relative;
    margin: 0 auto;
    transition: background 0.3s ease;
}

/* ============================================================================
   LOADING STATE
   ============================================================================ */

.cfm-ad > div:empty::before {
    content: 'Advertisement';
    font-size: 11px;
    color: #999;
    text-transform: uppercase;
    letter-spacing: 1px;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

@keyframes adPulse {
    0%, 100% { opacity: 0.6; background-color: #f5f5f5; }
    50% { opacity: 0.8; background-color: #f0f0f0; }
}

.cfm-ad > div:empty {
    animation: adPulse 1.5s ease-in-out infinite;
}

.cfm-ad > div:not(:empty) {
    animation: none;
    background: transparent;
}

.cfm-ad::after {
    content: 'Advertisement';
    display: block;
    font-size: 10px;
    color: #999;
    text-transform: uppercase;
    margin-top: 4px;
    letter-spacing: 0.5px;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, sans-serif;
}

/* ============================================================================
   LEADERBOARD ADS (top/mid/bottom on all pages)
   ============================================================================ */

.cfm-ad--leaderboard {
    margin: 30px auto;
    padding: 15px 0;
}

.cfm-ad--leaderboard > div {
    min-height: 90px;
    max-width: 970px;
}

/* ============================================================================
   IN-ARTICLE ADS (inline in content)
   ============================================================================ */

.cfm-ad--in_article {
    margin: 30px auto;
    padding: 20px 0;
}

.cfm-ad--in_article > div {
    min-height: 90px;
    max-width: 728px;
}

/* ============================================================================
   SECTION ADS (between homepage sections)
   ============================================================================ */

.cfm-ad--section_ad {
    margin: 40px auto;
    padding: 20px 0;
}

.cfm-ad--section_ad > div {
    min-height: 90px;
    max-width: 1200px;
}

/* ============================================================================
   SIDEBAR ADS (desktop sidebar) - STICKY
   ============================================================================ */

.cfm-ad--sidebar {
    position: sticky;
    top: 100px;
    margin: 0 0 20px 0;
    padding-top: 20px;
    max-height: calc(100vh - 120px);
}

.cfm-ad--sidebar > div {
    min-height: 250px;
    max-width: 300px;
}

.latest-stories-container {
    display: flex;
    flex-direction: column;
}

.latest-stories-container .cfm-ad {
    width: 100%;
    max-width: 300px;
    margin: 0 auto;
    display: flex;
    align-items: center;
    flex-direction: column;
}

/* ============================================================================
   MOBILE ANCHOR / STICKY UNIT
   Fixed 320x50 at bottom of screen on mobile.
   Stays visible throughout the entire scroll — highest viewability of any unit.

   To implement:
   1. Add to ad-manager.php slots:
      'anchor' => array(
          'path'    => '/7965891/anchor',
          'sizes'   => array( array(320,50), array(320,100) ),
          'id_base' => 'div-gpt-anchor',
      ),

   2. Add to footer.php before </body>:
      <?php if ( function_exists('capital_fm_render_ad') && wp_is_mobile() ) {
          capital_fm_render_ad('anchor', array('position' => 'anchor'));
      } ?>

   3. GAM: Create ad unit /7965891/anchor with 320x50 + 320x100 sizes.
      Add pos=anchor to line item targeting.
   ============================================================================ */

.cfm-ad--anchor {
    display: none; /* Hidden by default — only shown on mobile via media query */
}

@media (max-width: 767px) {
    .cfm-ad--anchor {
        display: block;
        position: fixed;
        bottom: 0;
        left: 0;
        right: 0;
        z-index: 9999;
        background: #ffffff;
        border-top: 1px solid #e0e0e0;
        text-align: center;
        padding: 4px 0;
        /* Safe area: handles iPhone notch and Android home indicator */
        padding-bottom: calc(4px + env(safe-area-inset-bottom));
        box-shadow: 0 -2px 8px rgba(0,0,0,0.1);
    }

    /* Push page content up so anchor never covers article text */
    body.has-anchor-ad {
        padding-bottom: 64px;
    }

    /* Hide the "Advertisement" label on anchor — too small */
    .cfm-ad--anchor::after {
        display: none;
    }

    .cfm-ad--anchor > div {
        min-height: 50px;
        max-width: 320px;
        background: transparent;
    }

    /* Close button — add this button inside the PHP render */
    .cfm-anchor-close {
        position: absolute;
        top: -12px;
        right: 8px;
        width: 22px;
        height: 22px;
        background: rgba(0,0,0,0.55);
        color: #ffffff;
        border: none;
        border-radius: 50%;
        font-size: 13px;
        line-height: 22px;
        text-align: center;
        cursor: pointer;
        padding: 0;
        z-index: 10000;
        font-family: -apple-system, BlinkMacSystemFont, sans-serif;
    }

    .cfm-anchor-close:hover {
        background: rgba(0,0,0,0.75);
    }
}

/* Anchor close button JS — add this to ads.js or custom.js:
   document.addEventListener('DOMContentLoaded', function() {
       var closeBtn = document.querySelector('.cfm-anchor-close');
       if (closeBtn) {
           closeBtn.addEventListener('click', function() {
               var anchor = document.querySelector('.cfm-ad--anchor');
               if (anchor) anchor.style.display = 'none';
               document.body.classList.remove('has-anchor-ad');
           });
       }
       // Add body class if anchor ad exists
       if (document.querySelector('.cfm-ad--anchor')) {
           document.body.classList.add('has-anchor-ad');
       }
   });
*/

/* ============================================================================
   RESPONSIVE - DESKTOP (1200px+)
   ============================================================================ */

@media (min-width: 1200px) {
    .cfm-ad--leaderboard > div {
        min-height: 90px;
        max-width: 1200px;
    }

    .cfm-ad--sidebar > div {
        min-height: 600px;
        max-width: 300px;
    }

    .cfm-ad--section_ad > div {
        min-height: 90px;
        max-width: 970px;
    }

    .article-top-ad-container {
        /* padding: 5px; */
    }
}

/* ============================================================================
   RESPONSIVE - TABLET (768px - 1199px)
   ============================================================================ */

@media (min-width: 768px) and (max-width: 1199px) {
    .cfm-ad--leaderboard > div {
        min-height: 90px;
        max-width: 728px;
    }

    .cfm-ad--in_article > div {
        min-height: 90px;
        max-width: 468px;
    }

    .cfm-ad--section_ad > div {
        min-height: 90px;
        max-width: 728px;
    }

    .cfm-ad--sidebar {
        position: relative;
        top: auto;
        margin: 20px auto;
    }

    .cfm-ad--sidebar > div {
        min-height: 250px;
        max-width: 300px;
    }

    .article-top-ad-container {
        padding: 15px 10px;
    }
}

/* ============================================================================
   RESPONSIVE - MOBILE (up to 767px)
   ============================================================================ */

@media (max-width: 767px) {
    .cfm-ad {
        margin: 15px auto;
        padding: 10px 0;
    }

    .cfm-ad--leaderboard > div {
        min-height: 50px;
        max-width: 320px;
    }

    .cfm-ad--in_article > div {
        min-height: 50px;
        max-width: 320px;
    }

    .cfm-ad--section_ad {
        margin: 20px auto;
        padding: 15px 0;
    }

    .cfm-ad--section_ad > div {
        min-height: 50px;
        max-width: 320px;
    }

    .cfm-ad--sidebar {
        display: none;
    }

    .article-top-ad-container {
        padding: 15px 10px;
        margin-bottom: 20px;
    }

    .article-top-ad-container .cfm-ad > div {
        min-height: 50px;
        max-width: 320px;
        margin: 0 auto;
    }
}

/* ============================================================================
   VERY SMALL MOBILE (up to 374px)
   ============================================================================ */

@media (max-width: 374px) {
    .cfm-ad > div {
        min-height: 50px;
        max-width: 300px;
    }

    .article-top-ad-container .cfm-ad > div {
        max-width: 300px;
    }
}

/* ============================================================================
   INTEGRATION WITH CONTENT
   ============================================================================ */

.homepage-columns + .cfm-ad {
    margin-top: 40px;
}

.categories-section + .cfm-ad,
.category-section + .cfm-ad {
    margin: 30px auto;
}

.cfm-ad + .categories-section,
.cfm-ad + .category-section {
    margin-top: 30px;
}

.entry-content .cfm-ad {
    margin: 30px auto;
}

.article-top-ad-container + .post-container {
    margin-top: 0;
}

/* ============================================================================
   PREVENT LAYOUT SHIFT (CLS)
   ============================================================================ */

.cfm-ad[data-sizes*="728,90"] > div { min-height: 90px; }
.cfm-ad[data-sizes*="970,90"] > div { min-height: 90px; }
.cfm-ad[data-sizes*="300,250"] > div { min-height: 250px; }
.cfm-ad[data-sizes*="300,600"] > div { min-height: 600px; }
.cfm-ad[data-sizes*="320,50"] > div { min-height: 50px; }
.cfm-ad[data-sizes*="160,600"] > div { min-height: 600px; }

/* ============================================================================
   DARK MODE SUPPORT
   ============================================================================ */

@media (prefers-color-scheme: dark) {
    .cfm-ad > div {
        background: #2a2a2a;
    }

    .cfm-ad::after,
    .cfm-ad > div:empty::before {
        color: #666;
    }

    .article-top-ad-container {
        background: #ebeef1;
    }

    .cfm-ad--anchor {
        background: #1a1a1a;
        border-top-color: #333;
    }

    @keyframes adPulse {
        0%, 100% { opacity: 0.6; background-color: #2a2a2a; }
        50% { opacity: 0.8; background-color: #353535; }
    }
}

/* ============================================================================
   PRINT STYLES
   ============================================================================ */

@media print {
    .cfm-ad,
    .article-top-ad-container,
    .cfm-ad--anchor {
        display: none !important;
    }
}

/* ============================================================================
   ACCESSIBILITY
   ============================================================================ */

.cfm-ad-skip {
    position: absolute;
    left: -9999px;
    top: auto;
    width: 1px;
    height: 1px;
    overflow: hidden;
}

.cfm-ad-skip:focus {
    position: static;
    width: auto;
    height: auto;
    padding: 10px;
    background: #000;
    color: #fff;
    z-index: 10000;
}

.cfm-ad-sr-only {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border-width: 0;
}

/* ============================================================================
   UTILITY CLASSES
   ============================================================================ */

.cfm-ad--hidden {
    display: none !important;
}

.cfm-ad--debug {
    border: 2px dashed #ff6b6b !important;
}

.cfm-ad--debug::before {
    content: 'AD SLOT: ' attr(data-ad-slot);
    display: block;
    background: #ff6b6b;
    color: white;
    padding: 5px 10px;
    font-size: 11px;
    font-weight: bold;
    margin-bottom: 10px;
    text-align: left;
}

/* ============================================================================
   GPT IFRAME STYLES
   ============================================================================ */

.cfm-ad iframe {
    display: block;
    margin: 0 auto;
    border: 0;
    max-width: 100%;
}

.cfm-ad > div > iframe {
    vertical-align: top;
}

/* ============================================================================
   PERFORMANCE OPTIMIZATIONS
   ============================================================================ */

.cfm-ad--sidebar {
    will-change: transform;
    contain: layout style paint;
}

.cfm-ad--sidebar,
.post-container ~ .cfm-ad {
    content-visibility: auto;
}