/**
 * Smart TOC UX - Styles CSS
 */

/* Variables CSS */
:root {
    --smart-toc-bg: #f8f9fa;
    --smart-toc-bg-end: #ffffff; /* Ajout pour le dégradé */
    --smart-toc-text: #333333;
    --smart-toc-active: #007cba;
    --smart-toc-hover: #005a87;
    --smart-toc-border: #e0e0e0;
    --smart-toc-shadow: rgba(0, 0, 0, 0.1);
    --smart-toc-radius: 8px;
    --smart-toc-transition: all 0.3s ease;
}

/* Reset et base */
.smart-toc-block *,
.smart-toc-sidebar *,
.smart-toc-mobile-panel * {
    box-sizing: border-box;
    margin: 0;
    padding: 0;
}

/* Empêcher le débordement horizontal */
body.smart-toc-active {
    overflow-x: hidden;
}

/* ===== TOC Block (Top of article) ===== */
.smart-toc-block {
    background: var(--smart-toc-bg);
    border: 1px solid var(--smart-toc-border);
    border-radius: var(--smart-toc-radius);
    padding: 1.5rem;
    margin: 2rem 0;
    font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", Roboto, "Helvetica Neue", Arial, sans-serif;
}

.smart-toc-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
}

.smart-toc-title {
    font-size: 1.25rem;
    font-weight: 600;
    color: var(--smart-toc-text);
    margin: 0;
}

.smart-toc-toggle {
    background: transparent;
    border: 1px solid var(--smart-toc-border);
    border-radius: 4px;
    padding: 0.25rem 0.5rem;
    cursor: pointer;
    transition: var(--smart-toc-transition);
    color: var(--smart-toc-text);
}

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

.smart-toc-content {
    overflow: hidden;
    transition: max-height 0.3s ease;
    max-height: 2000px;
}

.smart-toc-content--collapsed {
    max-height: 0;
}

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

.smart-toc-item {
    margin: 0.25rem 0;
}

.smart-toc-level-3 {
    padding-left: 1.5rem;
}

.smart-toc-level-4 {
    padding-left: 3rem;
}

.smart-toc-link {
    display: flex;
    align-items: baseline;
    color: var(--smart-toc-text);
    text-decoration: none;
    padding: 0.375rem 0.5rem;
    border-radius: 4px;
    transition: var(--smart-toc-transition);
}

.smart-toc-link:hover {
    background: rgba(0, 124, 186, 0.1);
    color: var(--smart-toc-active);
}

.smart-toc-link.smart-toc-active {
    background: var(--smart-toc-active);
    color: white;
}

.smart-toc-number {
    min-width: 2rem;
    margin-right: 0.5rem;
    font-weight: 500;
    opacity: 0.7;
}

.smart-toc-text {
    flex: 1;
    line-height: 1.5;
}

/* Marqueur de position pour la sidebar */
.smart-toc-sidebar-marker {
    position: relative;
    height: 0;
    width: 0;
    visibility: hidden;
}

/* ===== Sidebar Sticky Desktop ===== */
.smart-toc-sidebar {
    position: fixed;
    top: 50%; /* Centrage vertical */
    transform: translateY(-50%); /* Centrage vertical */
    right: -320px; /* Positionnée hors de l'écran par défaut */
    width: 280px;
    max-height: 70vh; /* Augmentation légère pour le confort */
    background: linear-gradient(160deg, var(--smart-toc-bg) 0%, var(--smart-toc-bg-end) 100%); /* Dégradé */
    border: 1px solid var(--smart-toc-border);
    border-right: none;
    border-radius: var(--smart-toc-radius) 0 0 var(--smart-toc-radius);
    box-shadow: 0 4px 15px var(--smart-toc-shadow);
    z-index: 1000;
    transition: right 0.4s cubic-bezier(0.25, 0.8, 0.25, 1);
    display: flex;
    flex-direction: column;
    overflow: hidden; /* Empêche le débordement global */
}

.smart-toc-sidebar--visible {
    right: 0; /* Alignée à droite de l'écran */
}

.smart-toc-sidebar-wrapper {
    padding: 1.25rem;
    height: 100%;
    display: flex;
    flex-direction: column;
    min-height: 0; /* Permet au flex de rétrécir */
}

.smart-toc-sidebar-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    margin-bottom: 1rem;
    padding-bottom: 0.75rem;
    border-bottom: 1px solid var(--smart-toc-border);
}

.smart-toc-sidebar-title {
    font-size: 1rem;
    font-weight: 600;
    color: var(--smart-toc-text);
}

.smart-toc-sidebar-close {
    background: transparent;
    border: none;
    font-size: 1.5rem;
    cursor: pointer;
    color: #999;
    line-height: 1;
    padding: 0;
    width: 50px;
    height: 50px;
    display: flex;
    align-items: center;
    justify-content: center;
    border-radius: 50%;
    transition: var(--smart-toc-transition);
}
.smart-toc-sidebar-close:hover {
    color: var(--smart-toc-text);
    background-color: rgba(0,0,0,0.05);
}
.smart-toc-sidebar-close svg {
    width: 28px;
    height: 28px;
}


/* ===== Bouton d'ouverture de la Sidebar (Hamburger) ===== */
.smart-toc-sidebar-opener {
    position: fixed;
    top: 50%;
    right: 20px;
    transform: translateY(-50%);
    background: var(--smart-toc-active);
    color: white;
    border: none;
    width: 60px;
    height: 60px;
    border-radius: 50%;
    cursor: pointer;
    z-index: 999;
    display: flex;
    align-items: center;
    justify-content: center;
    box-shadow: 0 2px 8px var(--smart-toc-shadow);
    opacity: 0;
    visibility: hidden;
    transition: var(--smart-toc-transition);
}

.smart-toc-sidebar-opener--visible {
    opacity: 1;
    visibility: visible;
}

.smart-toc-sidebar-opener:hover {
    background: var(--smart-toc-hover);
    transform: translateY(-50%) scale(1.1);
}

.smart-toc-sidebar-opener svg {
    width: 32px;
    height: 32px;
}

/* ===== Foveated Scroll pour Sidebar (VERSION AMÉLIORÉE) ===== */
.smart-toc-sidebar--foveated .smart-toc-sidebar-nav {
    flex: 1 1 auto; /* Prend l'espace disponible mais peut rétrécir */
    min-height: 0; /* Crucial pour permettre le scroll dans flexbox */
    overflow-y: auto;
    scroll-behavior: smooth; /* Comportement de défilement fluide */
    /* Masque pour estomper uniquement le bas */
    -webkit-mask-image: linear-gradient(to bottom, black 0%, black 85%, transparent 100%);
    mask-image: linear-gradient(to bottom, black 0%, black 85%, transparent 100%);
    scrollbar-width: none; /* Cache la scrollbar sur Firefox */
    -ms-overflow-style: none; /* Cache la scrollbar sur IE/Edge */
}
.smart-toc-sidebar--foveated .smart-toc-sidebar-nav::-webkit-scrollbar {
    display: none; /* Cache la scrollbar sur Chrome/Safari/Opera */
}

.smart-toc-sidebar--foveated .smart-toc-sidebar-item {
    /* Transition douce pour l'opacité et la taille */
    transition: opacity 0.4s ease, transform 0.4s ease;
    /* État par défaut (non-actif) */
    opacity: 0.5;
    transform: scale(0.95);
    will-change: opacity, transform; /* Optimisation des performances */
}

.smart-toc-sidebar--foveated .smart-toc-sidebar-item.is-active {
    /* État actif : entièrement visible et taille normale */
    opacity: 1;
    transform: scale(1);
}

.smart-toc-sidebar--foveated .smart-toc-sidebar-item.is-active .smart-toc-sidebar-link:not(.smart-toc-active) {
    /* Style plus prononcé pour le lien actif (sauf s'il est vraiment actif) */
    color: var(--smart-toc-active);
    font-weight: 600;
}

.smart-toc-sidebar--foveated .smart-toc-sidebar-item.is-active .smart-toc-sidebar-link.smart-toc-active {
    /* Lien actif en mode foveated : blanc sur fond bleu */
    color: white;
    background: var(--smart-toc-active);
}

.smart-toc-sidebar--foveated .smart-toc-sidebar-link:focus-visible {
    outline: 2px solid var(--smart-toc-active);
    outline-offset: 2px;
    border-radius: 4px;
}

.smart-toc-sidebar-nav {
    overflow-y: auto;
    flex: 1 1 auto;
    min-height: 0; /* Crucial pour le scroll dans flexbox */
    padding-right: 0.5rem;
    scrollbar-width: thin;
    scrollbar-color: var(--smart-toc-border) transparent;
    scroll-behavior: smooth;
}

.smart-toc-sidebar-nav::-webkit-scrollbar {
    width: 4px;
}
.smart-toc-sidebar-nav::-webkit-scrollbar-thumb {
    background: var(--smart-toc-border);
    border-radius: 2px;
}
.smart-toc-sidebar-nav::-webkit-scrollbar-thumb:hover {
    background: var(--smart-toc-active);
}
.smart-toc-sidebar-nav::-webkit-scrollbar-track {
    background: transparent;
}

.smart-toc-sidebar-list {
    list-style: none;
}

.smart-toc-sidebar-item {
    margin: 0.125rem 0;
}

.smart-toc-sidebar-item[data-level="3"] {
    padding-left: 1rem;
}

.smart-toc-sidebar-item[data-level="4"] {
    padding-left: 2rem;
}

.smart-toc-sidebar-link {
    display: block;
    color: #000000;
    text-decoration: none;
    padding: 0.375rem 0.5rem;
    border-radius: 4px;
    font-size: 16px;
    line-height: 1.5;
    transition: var(--smart-toc-transition);
}

.smart-toc-sidebar-link:hover {
    background: rgba(0, 124, 186, 0.1);
    color: var(--smart-toc-active);
}

.smart-toc-sidebar-link.smart-toc-active {
    background: var(--smart-toc-active);
    color: white;
    font-weight: 500;
}


/* ===== Mobile Styles ===== */
/* Nouvelle barre fixe en bas style TikTok */
.smart-toc-mobile-bar {
    display: none;
    position: fixed !important;
    bottom: 0 !important;
    left: 0 !important;
    right: 0 !important;
    width: 100% !important;
    height: 60px;
    background: rgba(255, 255, 255, 0.95);
    backdrop-filter: blur(10px);
    -webkit-backdrop-filter: blur(10px);
    border-top: 1px solid rgba(0, 0, 0, 0.1);
    z-index: 1000;
    align-items: center;
    justify-content: center;
    box-shadow: 0 -2px 10px rgba(0, 0, 0, 0.1);
    transform: none !important;
    transition: none !important;
}

.smart-toc-mobile-bar--visible {
    display: flex !important;
}


.smart-toc-mobile-fab {
    background: transparent;
    color: var(--smart-toc-active);
    border: none;
    cursor: pointer;
    z-index: 1001;
    display: flex; /* Toujours visible dans la barre */
    flex-direction: column;
    align-items: center;
    gap: 4px;
    font-size: 10px;
    font-weight: 500;
    transition: var(--smart-toc-transition);
    padding: 8px 12px;
    border-radius: 8px;
}

.smart-toc-mobile-fab:hover {
    background: rgba(0, 124, 186, 0.1);
}

.smart-toc-mobile-fab:active {
    transform: scale(0.95);
}

.smart-toc-mobile-icon {
    width: 24px;
    height: 24px;
}

.smart-toc-mobile-fab-text {
    font-size: 16px;
    margin-top: 2px;
}

.smart-toc-mobile-overlay {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background: rgba(0, 0, 0, 0.5);
    z-index: 1001;
    opacity: 0;
    transition: opacity 0.3s ease;
}

.smart-toc-mobile-overlay--visible {
    display: block;
    opacity: 1;
}

.smart-toc-mobile-panel {
    position: fixed;
    bottom: 0;
    left: 0;
    right: 0;
    height: 50vh;
    max-height: 400px;
    background: white;
    z-index: 1002;
    transform: translateY(100%);
    transition: transform 0.3s cubic-bezier(0.25, 0.8, 0.25, 1);
    display: flex;
    flex-direction: column;
    border-radius: 16px 16px 0 0;
    box-shadow: 0 -4px 20px rgba(0, 0, 0, 0.15);
}

.smart-toc-mobile-panel--open {
    transform: translateY(0);
}

.smart-toc-mobile-header {
    display: flex;
    justify-content: center;
    align-items: center;
    padding: 0.75rem 1rem;
    border-bottom: 1px solid var(--smart-toc-border);
    background: var(--smart-toc-bg);
    position: relative;
}

.smart-toc-mobile-handle {
    position: absolute;
    top: 8px;
    left: 50%;
    transform: translateX(-50%);
    width: 36px;
    height: 4px;
    background: #ccc;
    border-radius: 2px;
}

.smart-toc-mobile-title {
    font-size: 1.125rem;
    font-weight: 600;
    color: var(--smart-toc-text);
}

.smart-toc-mobile-close {
    position: absolute;
    right: 1rem;
    background: transparent;
    border: none;
    padding: 4px;
    cursor: pointer;
    border-radius: 50%;
    transition: var(--smart-toc-transition);
}

.smart-toc-mobile-close:hover {
    background: rgba(0, 0, 0, 0.05);
}

.smart-toc-mobile-close svg {
    width: 24px;
    height: 24px;
    fill: #666;
}

.smart-toc-mobile-nav {
    flex: 1;
    overflow-y: auto;
    padding: 1rem;
}

.smart-toc-mobile-list {
    list-style: none;
}

.smart-toc-mobile-item {
    margin: 0.25rem 0;
}

.smart-toc-mobile-link {
    display: flex;
    align-items: baseline;
    color: var(--smart-toc-text);
    text-decoration: none;
    padding: 0.5rem;
    border-radius: 4px;
    transition: var(--smart-toc-transition);
}

.smart-toc-mobile-link[data-level="3"] {
    padding-left: 2rem;
}

.smart-toc-mobile-link[data-level="4"] {
    padding-left: 3rem;
}

.smart-toc-mobile-link:hover {
    background: rgba(0, 124, 186, 0.1);
}

.smart-toc-mobile-link.smart-toc-active {
    background: var(--smart-toc-active);
    color: white;
}

.smart-toc-mobile-number {
    min-width: 2rem;
    margin-right: 0.5rem;
    font-weight: 500;
    opacity: 0.7;
}

.smart-toc-mobile-text {
    flex: 1;
}

/* ===== Back to Top Button ===== */
.smart-toc-back-to-top {
    position: fixed;
    bottom: 20px;
    left: 20px;
    background: var(--smart-toc-active);
    color: white;
    border: none;
    border-radius: 50%;
    width: 44px;
    height: 44px;
    font-size: 20px;
    cursor: pointer;
    box-shadow: 0 2px 8px var(--smart-toc-shadow);
    opacity: 0;
    visibility: hidden;
    transition: var(--smart-toc-transition);
    z-index: 999;
}

.smart-toc-back-to-top--visible {
    opacity: 1;
    visibility: visible;
}

.smart-toc-back-to-top:hover {
    background: var(--smart-toc-hover);
    transform: scale(1.1);
}

/* ===== Progress Bar ===== */
.smart-toc-progress-bar {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 3px;
    background: rgba(0, 0, 0, 0.1);
    z-index: 10000;
}

.smart-toc-progress-fill {
    height: 100%;
    background: var(--smart-toc-active);
    width: 0;
    transition: width 0.1s ease;
}

/* ===== Highlight Effect ===== */
.smart-toc-highlighted {
    animation: smart-toc-highlight 1s ease;
}

@keyframes smart-toc-highlight {
    0% {
        background-color: transparent;
    }
    50% {
        background-color: rgba(255, 235, 59, 0.4);
    }
    100% {
        background-color: transparent;
    }
}

/* ===== Media Queries ===== */
/* Masquer la barre mobile sur PC (écrans > 768px) */
@media (min-width: 769px) {
    .smart-toc-mobile-bar,
    .smart-toc-mobile-bar--visible {
        display: none !important;
    }
}

@media (max-width: 1280px) {
    .smart-toc-sidebar {
        width: 240px;
    }
}

@media (max-width: 768px) {
    .smart-toc-sidebar {
        width: 90%;
        max-width: 280px;
    }

    .smart-toc-mobile-bar {
        display: none;
        /* Corrections spécifiques pour mobile */
        position: fixed !important;
        bottom: 0 !important;
        transform: translateZ(0) !important; /* Force hardware acceleration */
        -webkit-transform: translateZ(0) !important;
        will-change: auto !important;
    }

    .smart-toc-mobile-bar--visible {
        display: flex !important;
    }

    .smart-toc-block {
        padding: 1rem;
        margin: 1rem 0;
    }

    .smart-toc-title {
        font-size: 1.125rem;
    }

    .smart-toc-back-to-top {
        bottom: 80px;
    }

    /* Masquer le bouton sidebar desktop sur mobile */
    .smart-toc-sidebar-opener {
        display: none !important;
    }
}

/* Media query plus large pour s'assurer que ça fonctionne sur tous les mobiles */
@media (max-width: 900px) {
    .smart-toc-mobile-bar {
        display: none !important;
        position: fixed !important;
        bottom: 0 !important;
    }

    .smart-toc-mobile-bar--visible {
        display: flex !important;
    }

    .smart-toc-sidebar-opener {
        display: none !important;
    }
}

/* Corrections spécifiques pour iOS Safari et Chrome mobile */
@supports (-webkit-touch-callout: none) {
    .smart-toc-mobile-bar {
        position: fixed !important;
        bottom: 0 !important;
        /* Utilise env() pour iOS avec encoche */
        bottom: env(safe-area-inset-bottom, 0) !important;
        padding-bottom: env(safe-area-inset-bottom, 0) !important;
    }
}

/* Support pour Android Chrome */
@media screen and (max-width: 768px) {
    .smart-toc-mobile-bar {
        position: fixed !important;
        bottom: 0 !important;
        -webkit-transform: translate3d(0, 0, 0) !important;
        transform: translate3d(0, 0, 0) !important;
    }
}

@media (max-width: 480px) {
    .smart-toc-mobile-panel {
        height: 60vh;
        max-height: 500px;
    }

    .smart-toc-mobile-bar {
        height: 70px;
    }

    .smart-toc-mobile-fab {
        padding: 6px 10px;
        font-size: 9px;
    }

    .smart-toc-mobile-icon {
        width: 40px;
        height: 40px;
    }
}

/* ===== Compatibilité Elementor ===== */
.elementor-widget-smart-toc .smart-toc-block {
    margin: 0;
}

/* ===== Print Styles ===== */
@media print {
    .smart-toc-sidebar,
    .smart-toc-sidebar-opener,
    .smart-toc-mobile-fab,
    .smart-toc-mobile-panel,
    .smart-toc-mobile-overlay,
    .smart-toc-back-to-top,
    .smart-toc-progress-bar {
        display: none !important;
    }

    .smart-toc-content {
        max-height: none !important;
    }
}