/**
 * Blog TOC - Premium Floating Table of Contents
 * 
 * Provides State Parks-style floating TOC for blog posts and planning pages.
 * 
 * @version 2.0.0
 */

/* ==========================================================================
   HIDE LUCYWP TOC PLUGIN (Duplicate with our Floating TOC)
   ========================================================================== */

/* Hide the LuckyWP TOC plugin block on pages with our floating TOC */
.has-blog-toc .lwptoc,
.has-blog-toc .lwptoc_i,
.has-blog-toc .toc-sidebar,
.has-blog-toc #toc_container,
.has-blog-toc .ez-toc-container,
.has-blog-toc .sp-toc {
    display: none !important;
}

/* ==========================================================================
   BREADCRUMB CAPITALIZATION FIX
   ========================================================================== */

/* Capitalize breadcrumb text (fixes lowercase category names like "hunting" -> "Hunting") */
.ast-breadcrumbs-wrapper .trail-items .trail-item a span,
.ast-breadcrumbs-wrapper .trail-items .trail-item>span span,
.breadcrumb-trail .trail-items .trail-item span {
    text-transform: capitalize;
}

/* ==========================================================================
   CSS CUSTOM PROPERTIES (Reuse State Parks design tokens)
   ========================================================================== */

:root {
    --blog-toc-primary: #1a5a1a;
    --blog-toc-primary-light: #2e7d32;
    --blog-toc-accent: #4caf50;
    --blog-toc-text: #2d3748;
    --blog-toc-text-light: #718096;
    --blog-toc-white: #ffffff;
    --blog-toc-bg: #f8f9fa;
    --blog-toc-border: #e2e8f0;
    --blog-toc-shadow: 0 4px 20px rgba(0, 0, 0, 0.08);
    --blog-toc-radius: 12px;
    --blog-toc-width: 260px;
}

/* ==========================================================================
   BASE - Hidden by default (shown via media queries)
   ========================================================================== */

.blog-floating-toc {
    display: none;
}

/* CLS Prevention: Reserve no space before JS initializes the TOC on mobile.
   The TOC is injected via wp_body_open and positioned sticky on mobile.
   Without containment, the sticky element causes a 65px layout shift (CLS 0.40).
   Solution: Start with height: 0 and overflow: hidden, then JS adds .toc-initialized
   class once the TOC is built and the user scrolls past the hero. */
@media (max-width: 1199px) {
    .blog-floating-toc:not(.toc-initialized) {
        height: 0 !important;
        overflow: hidden !important;
        padding: 0 !important;
        border: none !important;
        visibility: hidden;
    }
}

/* Hide TOC until hero is scrolled past */
.blog-floating-toc.is-hidden-before-hero {
    opacity: 0;
    pointer-events: none;
    transition: opacity 0.3s ease;
}

/* ==========================================================================
   MOBILE/TABLET (<1200px) - Horizontal Sticky Pill-Bar
   ========================================================================== */

@media (max-width: 1199px) {
    .blog-floating-toc {
        display: block;
        position: sticky;
        top: 60px;
        left: 0;
        right: 0;
        z-index: 50;
        background: linear-gradient(135deg, #f8fafc 0%, #f1f5f9 100%);
        border-bottom: 1px solid #e2e8f0;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
        padding: 0.75rem 1rem;
        /* CLS fix: smooth reveal transition */
        transition: height 0.3s ease, padding 0.3s ease, visibility 0s;
    }

    /* Once JS initializes, show the TOC with proper dimensions */
    .blog-floating-toc.toc-initialized {
        height: auto !important;
        overflow: visible !important;
        visibility: visible !important;
    }

    /* Hide toggle button on mobile */
    .blog-floating-toc .blog-toc-toggle {
        display: none;
    }

    /* Content becomes horizontal scrolling container */
    .blog-floating-toc .blog-toc-content {
        display: flex;
        align-items: center;
        gap: 0.5rem;
        overflow-x: auto;
        overflow-y: hidden;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
        -ms-overflow-style: none;
        padding: 0;
        margin: 0;
        background: transparent;
        box-shadow: none;
        max-height: none;
        border-radius: 0;
    }

    .blog-floating-toc .blog-toc-content::-webkit-scrollbar {
        display: none;
    }

    /* Hide title on mobile */
    .blog-floating-toc .blog-toc-title {
        display: none;
    }

    /* List becomes horizontal flex */
    .blog-floating-toc .blog-toc-list {
        display: flex;
        gap: 0.5rem;
        margin: 0;
        padding: 0;
        list-style: none;
    }

    /* Items inline */
    .blog-floating-toc .blog-toc-item {
        flex-shrink: 0;
        margin: 0;
    }

    .blog-floating-toc .blog-toc-item-sub {
        padding-left: 0;
    }

    /* Hide triangle and children on mobile - flat pill layout */
    .blog-floating-toc .blog-toc-triangle {
        display: none;
    }

    .blog-floating-toc .blog-toc-children {
        display: none;
    }

    .blog-floating-toc .blog-toc-link-wrap {
        display: contents;
    }

    /* Pill-style links */
    .blog-floating-toc .blog-toc-link {
        display: inline-flex;
        align-items: center;
        padding: 0.5rem 0.875rem;
        background: #ffffff;
        border: 1px solid #e2e8f0;
        border-radius: 9999px;
        color: #2e7d32;
        font-size: 0.8rem;
        font-weight: 500;
        text-decoration: none;
        white-space: nowrap;
        transition: all 0.2s ease;
        line-height: 1.2;
    }

    .blog-floating-toc .blog-toc-link:hover {
        color: #14532d;
        background: #dcfce7;
        border-color: #86efac;
        transform: translateY(-1px);
        box-shadow: 0 2px 8px rgba(34, 197, 94, 0.15);
    }

    .blog-floating-toc .blog-toc-link.active {
        background: linear-gradient(135deg, #166534 0%, #14532d 100%);
        color: white;
        border-color: #166534;
        font-weight: 600;
        box-shadow: 0 2px 6px rgba(22, 101, 52, 0.35);
    }

    /* Hide progress bar on mobile */
    .blog-floating-toc .blog-toc-progress {
        display: none;
    }
}

/* ==========================================================================
   LARGE DESKTOP (≥1600px) - Full floating TOC (right side)
   ========================================================================== */

@media (min-width: 1600px) {
    .blog-floating-toc {
        display: block;
        position: fixed;
        /* Anchors TOC to the RIGHT side of the content container —
           symmetrical to the old left position: 50vw - 620px (half content) - 280px (TOC+gap)
           This places the TOC in the free right gutter beside the article */
        left: auto;
        right: max(20px, calc(50vw - 900px));
        top: 150px;
        width: var(--blog-toc-width);
        z-index: 100;
        transition: all 0.3s ease;
    }


    .blog-floating-toc.is-collapsed {
        width: 44px;
    }

    .blog-toc-content {
        background: rgba(255, 255, 255, 0.75);
        backdrop-filter: blur(16px);
        -webkit-backdrop-filter: blur(16px);
        border-radius: var(--blog-toc-radius);
        padding: 20px 16px;
        box-shadow: 0 8px 32px rgba(0, 0, 0, 0.06), inset 0 0 0 1px rgba(255, 255, 255, 0.5);
        max-height: calc(100vh - 200px);
        overflow-y: auto;
        transition: all 0.3s ease;
        scrollbar-width: thin;
        scrollbar-color: transparent transparent;
    }

    .blog-toc-content:hover {
        scrollbar-color: rgba(0, 0, 0, 0.15) transparent;
    }

    .blog-floating-toc.is-collapsed .blog-toc-content {
        padding: 6px;
        overflow: hidden;
    }

    /* In collapsed state: hide list and progress, keep header/toggle visible */
    .blog-floating-toc.is-collapsed .blog-toc-list,
    .blog-floating-toc.is-collapsed .blog-toc-progress {
        opacity: 0;
        visibility: hidden;
        height: 0;
        margin: 0;
        padding: 0;
        overflow: hidden;
    }

    .blog-floating-toc.is-collapsed .blog-toc-header {
        margin-bottom: 0;
    }

    .blog-floating-toc.is-collapsed .blog-toc-title {
        display: none;
    }

    /* Header with title and toggle button */
    .blog-toc-header {
        display: flex;
        align-items: center;
        justify-content: space-between;
        margin-bottom: 14px;
        padding-bottom: 12px;
        border-bottom: 2px solid var(--blog-toc-border);
    }

    .blog-toc-title {
        font-size: 0.7rem;
        font-weight: 700;
        color: var(--blog-toc-primary);
        text-transform: uppercase;
        letter-spacing: 0.8px;
        margin: 0;
        padding: 0;
        border: none;
        display: flex;
        align-items: center;
        gap: 6px;
    }

    .blog-toc-title::before {
        content: '📑';
        font-size: 1rem;
    }

    /* Toggle button in header - top right */
    .blog-toc-toggle {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 28px;
        height: 28px;
        border: 1px solid var(--blog-toc-border);
        border-radius: 6px;
        background: var(--blog-toc-bg);
        color: var(--blog-toc-primary);
        cursor: pointer;
        transition: all 0.2s ease;
        flex-shrink: 0;
    }

    .blog-toc-toggle:hover {
        background: var(--blog-toc-primary);
        color: var(--blog-toc-white);
        border-color: var(--blog-toc-primary);
    }

    .blog-toc-toggle svg {
        width: 14px;
        height: 14px;
        stroke-width: 2.5;
        flex-shrink: 0;
    }

    /* Icon visibility based on collapsed state */
    .blog-toc-icon-expand {
        display: none !important;
    }

    .blog-toc-icon-collapse {
        display: block !important;
    }

    .blog-floating-toc.is-collapsed .blog-toc-icon-collapse {
        display: none !important;
    }

    .blog-floating-toc.is-collapsed .blog-toc-icon-expand {
        display: block !important;
    }

    .blog-toc-list {
        list-style: none;
        margin: 0;
        padding: 0;
    }

    .blog-toc-item {
        margin-bottom: 2px;
    }

    .blog-toc-item-sub {
        padding-left: 0;
    }

    /* Link wrapper: holds triangle + link side by side */
    .blog-toc-link-wrap {
        display: flex;
        align-items: center;
        gap: 0;
    }

    /* Triangle toggle button */
    .blog-toc-triangle {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 22px;
        height: 22px;
        min-width: 22px;
        border: none;
        border-radius: 4px;
        background: transparent;
        color: var(--blog-toc-text-light);
        cursor: pointer;
        padding: 0;
        transition: all 0.2s ease;
        flex-shrink: 0;
    }

    .blog-toc-triangle svg {
        transition: transform 0.25s ease;
    }

    .blog-toc-triangle:hover {
        background: var(--blog-toc-bg);
        color: var(--blog-toc-primary);
    }

    /* Rotate triangle when open */
    .blog-toc-has-children.is-open>.blog-toc-link-wrap>.blog-toc-triangle svg {
        transform: rotate(90deg);
    }

    /* Collapsible child list (Notion-style hierarchy) */
    .blog-toc-children {
        list-style: none;
        margin: 2px 0 0 10px;
        padding: 0 0 0 10px;
        border-left: 1px solid rgba(0, 0, 0, 0.06);
        max-height: 0;
        overflow: hidden;
        opacity: 0;
        transition: max-height 0.3s ease, opacity 0.25s ease, padding 0.3s ease;
    }

    .blog-toc-has-children.is-open>.blog-toc-children {
        max-height: 500px;
        opacity: 1;
        padding: 4px 0 4px 10px;
    }

    .blog-toc-link {
        display: block;
        padding: 8px 10px;
        color: #2e7d32;
        text-decoration: none;
        font-size: 0.82rem;
        font-weight: 500;
        border-radius: 6px;
        border-left: 3px solid transparent;
        transition: all 0.2s ease;
        line-height: 1.4;
        white-space: normal;
        word-wrap: break-word;
        flex: 1;
        min-width: 0;
    }

    .blog-toc-link:hover {
        background: var(--blog-toc-bg);
        color: var(--blog-toc-primary);
    }

    .blog-toc-link.active {
        background: linear-gradient(135deg, rgba(76, 175, 80, 0.18), rgba(139, 195, 74, 0.12));
        color: #2e7d32;
        border-left-color: #4caf50;
        font-weight: 700;
        box-shadow: inset 0 0 0 1px rgba(76, 175, 80, 0.2);
        transform: translateX(4px);
        /* Fluid Active-State Micro-Animation */
    }

    /* When parent H2 has an active child, highlight the triangle subtly */
    .blog-toc-has-children.is-open>.blog-toc-link-wrap>.blog-toc-triangle {
        color: var(--blog-toc-primary-light);
    }

    /* Ghost Scrollbar */
    .blog-toc-content::-webkit-scrollbar {
        width: 4px;
    }

    .blog-toc-content::-webkit-scrollbar-track {
        background: transparent;
    }

    .blog-toc-content::-webkit-scrollbar-thumb {
        background-color: transparent;
        border-radius: 4px;
        transition: background-color 0.3s ease;
    }

    .blog-toc-content:hover::-webkit-scrollbar-thumb {
        background-color: rgba(0, 0, 0, 0.15);
    }
}

/* ==========================================================================
   MEDIUM DESKTOP (1200-1599px) - Collapsed by default, expand on click
   ========================================================================== */

@media (min-width: 1200px) and (max-width: 1599px) {
    .blog-floating-toc {
        display: block;
        position: fixed;
        left: auto;
        right: 16px;
        top: 150px;
        z-index: 100;
    }

    .blog-toc-toggle {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 40px;
        height: 40px;
        border: none;
        border-radius: 10px;
        background: var(--blog-toc-white);
        color: var(--blog-toc-primary);
        cursor: pointer;
        box-shadow: var(--blog-toc-shadow);
        transition: all 0.2s ease;
    }

    .blog-toc-toggle:hover {
        background: var(--blog-toc-primary);
        color: var(--blog-toc-white);
    }

    .blog-toc-toggle .blog-toc-icon-close {
        display: none;
    }

    .blog-floating-toc.is-expanded .blog-toc-icon-list {
        display: none;
    }

    .blog-floating-toc.is-expanded .blog-toc-icon-close {
        display: block;
    }

    .blog-toc-content {
        position: absolute;
        left: auto;
        right: 0;
        top: 48px;
        width: 220px;
        background: var(--blog-toc-white);
        border-radius: var(--blog-toc-radius);
        padding: 16px 20px;
        box-shadow: var(--blog-toc-shadow);
        max-height: calc(100vh - 200px);
        overflow-y: auto;
        opacity: 0;
        visibility: hidden;
        transform: translateY(-10px);
        transition: all 0.2s ease;
    }

    .blog-floating-toc.is-expanded .blog-toc-content {
        opacity: 1;
        visibility: visible;
        transform: translateY(0);
    }

    .blog-toc-title {
        font-size: 0.75rem;
        font-weight: 700;
        color: var(--blog-toc-primary);
        text-transform: uppercase;
        letter-spacing: 0.5px;
        margin: 0 0 12px;
        padding-bottom: 10px;
        border-bottom: 2px solid var(--blog-toc-border);
    }

    .blog-toc-list {
        list-style: none;
        margin: 0;
        padding: 0;
    }

    /* Link wrapper */
    .blog-toc-link-wrap {
        display: flex;
        align-items: center;
        gap: 0;
    }

    /* Triangle toggle */
    .blog-toc-triangle {
        display: flex;
        align-items: center;
        justify-content: center;
        width: 20px;
        height: 20px;
        min-width: 20px;
        border: none;
        border-radius: 4px;
        background: transparent;
        color: var(--blog-toc-text-light);
        cursor: pointer;
        padding: 0;
        transition: all 0.2s ease;
        flex-shrink: 0;
    }

    .blog-toc-triangle svg {
        transition: transform 0.25s ease;
    }

    .blog-toc-triangle:hover {
        background: var(--blog-toc-bg);
        color: var(--blog-toc-primary);
    }

    .blog-toc-has-children.is-open>.blog-toc-link-wrap>.blog-toc-triangle svg {
        transform: rotate(90deg);
    }

    /* Collapsible child list */
    .blog-toc-children {
        list-style: none;
        margin: 0;
        padding: 0 0 0 6px;
        max-height: 0;
        overflow: hidden;
        opacity: 0;
        transition: max-height 0.3s ease, opacity 0.25s ease, padding 0.3s ease;
    }

    .blog-toc-has-children.is-open>.blog-toc-children {
        max-height: 500px;
        opacity: 1;
        padding: 2px 0 4px 6px;
    }

    .blog-toc-link {
        display: block;
        padding: 6px 10px;
        color: #2e7d32;
        text-decoration: none;
        font-size: 0.8rem;
        font-weight: 500;
        border-radius: 6px;
        border-left: 2px solid transparent;
        transition: all 0.2s ease;
        line-height: 1.3;
        flex: 1;
        min-width: 0;
    }

    .blog-toc-link:hover {
        background: var(--blog-toc-bg);
        color: var(--blog-toc-primary);
    }

    .blog-toc-link.active {
        background: linear-gradient(135deg, rgba(76, 175, 80, 0.18), rgba(139, 195, 74, 0.12));
        color: #2e7d32;
        border-left-color: #4caf50;
        font-weight: 700;
        box-shadow: inset 0 0 0 1px rgba(76, 175, 80, 0.2);
    }

    .blog-toc-has-children.is-open>.blog-toc-link-wrap>.blog-toc-triangle {
        color: var(--blog-toc-primary-light);
    }
}

/* ==========================================================================
   HUB ARTICLE OVERRIDES
   
   Hub articles now use Astra's Page Builder layout (same as State Parks).
   The floating TOC works unchanged — standard left-gutter positioning
   applies without any hub-specific overrides needed.
   ========================================================================== */

/* ==========================================================================
   READING PROGRESS BAR (Optional enhancement)
   ========================================================================== */

.blog-toc-progress {
    height: 3px;
    background: var(--blog-toc-border);
    border-radius: 3px;
    margin-top: 16px;
    overflow: hidden;
}

.blog-toc-progress-bar {
    height: 100%;
    background: linear-gradient(90deg, var(--blog-toc-accent), var(--blog-toc-primary-light));
    width: 0%;
    transition: width 0.1s ease;
}