/* ============================================
   TOC MODULE - CSS (HIERARCHICAL NUMBERING)
   Version: 6.0.0 - Автоматическая вложенная нумерация
   Description: Нумерация 1. → 1.1. → 1.1.1. + Защита от стилей темы
   ============================================ */

/* ============================================
   1. КОНТЕЙНЕР БЛОКА TOC
   ============================================ */
.toc-block {
    background: transparent;
    border: 2px solid var(--info);
    border-radius: 5px;
    padding: 0;
    margin-bottom: 0;
    display: table;
    width: auto;
    max-width: 100%;
    transition: all 0.3s ease;
}

.toc-block.toc-collapsed {
    display: table !important;
    width: auto !important;
}

.toc-block:not(.toc-collapsed) {
    display: table !important;
    width: auto !important;
}

/* ============================================
   2. HEADER (Заголовок + Кнопка Toggle)
   ============================================ */
.toc-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 8px 12px;
    cursor: pointer;
    transition: var(--transition-base);
}

.toc-header:hover {
    background: rgba(var(--info-rgb), 0.05);
}

.toc-title {
    font-family: var(--font-headings);
    font-size: var(--font-md);
    font-weight: var(--font-bold);
    color: var(--text-primary) !important;
    margin-bottom: 2px !important;
    padding: 0;
    line-height: 1.4;
    white-space: nowrap;
    text-shadow: var(--text-shadow-sm);
}

.toc-toggle {
    background: transparent;
    border: none;
    color: var(--info);
    font-size: 22px;
    cursor: pointer;
    padding: 0;
    margin-left: 12px;
    transition: transform 0.3s ease;
    display: flex;
    align-items: center;
    justify-content: center;
    width: 24px;
    height: 24px;
    flex-shrink: 0;
}

.toc-toggle:hover {
    transform: scale(1.1);
}

.toc-block.toc-collapsed .toc-toggle {
    transform: rotate(180deg);
}

.toc-block:not(.toc-collapsed) .toc-toggle {
    transform: rotate(0deg);
}

/* ============================================
   3. CONTENT (Навигационный список)
   ============================================ */
.toc-content {
    display: none;
    padding: 0 var(--spacing-md);
}

.toc-content.expanded {
    display: block;
    padding: var(--spacing-sm) var(--spacing-md) var(--spacing-md);
    border-top: 1px solid rgba(var(--info-rgb), 0.2);
}

/* ============================================
   4. АГРЕССИВНЫЙ СБРОС СТИЛЕЙ + НАСТРОЙКА СЧЕТЧИКОВ
   ============================================ */

/* Полный сброс для всех списков */
.toc-block ul,
.toc-block ol,
.toc-block li,
.toc-content ul, 
.toc-content ol,
.toc-content li,
.toc-list,
.toc-list ul,
.toc-list li {
    list-style: none !important;
    list-style-type: none !important;
    list-style-image: none !important;
    background: none !important;
    margin: 0 !important;
    padding: 0 !important;
    text-indent: 0 !important;
    border: none !important;
}

/* ✅ КЛЮЧЕВАЯ НАСТРОЙКА: Инициализация счетчика на каждом уровне */
.toc-list,
.toc-list ul {
    counter-reset: item; /* Каждый <ul> создает новый контекст нумерации */
}

.toc-list li {
    counter-increment: item; /* Каждый <li> увеличивает счетчик */
    position: relative;
    font-size: 18px;
    line-height: 1.5;
}

/* Убираем браузерные маркеры */
.toc-block li::marker,
.toc-content li::marker,
.toc-list li::marker {
    content: "" !important;
    display: none !important;
}

/* ============================================
   5. АВТОМАТИЧЕСКАЯ ВЛОЖЕННАЯ НУМЕРАЦИЯ
   ============================================ */

.toc-list li::before {
    /* ✅ МАГИЯ: counters() автоматически создает вложенную нумерацию */
    /* Для H2: "1." */
    /* Для H3 внутри H2: "1.1." */
    /* Для H4 внутри H3: "1.1.1." */
    content: counters(item, ".") ". " !important;
    
    color: var(--info) !important;
    font-weight: var(--font-bold) !important;
    margin-right: 8px !important;
    display: inline-block !important;
    
    /* Принудительное восстановление видимости */
    visibility: visible !important; 
    opacity: 1 !important;
    position: static !important;
    transform: none !important;
}

/* ============================================
   6. ВИЗУАЛЬНАЯ ИЕРАРХИЯ (ОТСТУПЫ)
   ============================================ */

/* H2 - Основные разделы (БЕЗ ОТСТУПА) */
.toc-block .toc-item-h2,
.toc-content .toc-item-h2,
.toc-list .toc-item-h2 {
    padding-left: 0 !important; /* ✅ КРИТИЧЕСКОЕ: БЕЗ ОТСТУПА */
    margin-left: 0 !important;
    margin-top: 10px !important;
    margin-bottom: 4px !important;
    font-weight: bold !important;
}

.toc-list .toc-item-h2:first-child {
    margin-top: 0 !important;
}

/* H3 - Подразделы (ОТСТУП 24px) */
.toc-block .toc-item-h3,
.toc-content .toc-item-h3,
.toc-list .toc-item-h3 {
    padding-left: 24px !important; /* ✅ ОТСТУП 24px */
    margin-top: 3px !important;
    margin-bottom: 3px !important;
}

/* H4 - Под-подразделы (ОТСТУП 48px) */
.toc-block .toc-item-h4,
.toc-content .toc-item-h4,
.toc-list .toc-item-h4 {
    padding-left: 48px !important; /* ✅ ОТСТУП 48px */
    margin-top: 2px !important;
    margin-bottom: 2px !important;
}

/* H5 - Максимальная вложенность (ОТСТУП 72px) */
.toc-block .toc-item-h5,
.toc-content .toc-item-h5,
.toc-list .toc-item-h5 {
    padding-left: 72px !important; /* ✅ ОТСТУП 72px */
    margin-top: 1px !important;
    margin-bottom: 1px !important;
}

/* ============================================
   7. СТИЛИЗАЦИЯ ПО УРОВНЯМ
   ============================================ */

/* H2: Жирный, крупнее */
.toc-item-h2 > a {
    font-weight: bold !important;
    font-size: 16px !important;
    color: var(--text-primary) !important;
}

.toc-item-h2::before {
    font-size: 16px !important;
    font-weight: var(--font-bold) !important;
}

/* H3: Обычный вес, чуть меньше */
.toc-item-h3 > a {
    font-weight: normal !important;
    font-size: 15px !important;
    color: var(--text-primary) !important;
}

.toc-item-h3::before {
    font-weight: var(--font-semibold) !important;
    opacity: 0.9;
}

/* H4: Еще меньше */
.toc-item-h4::before {
    font-weight: var(--font-medium) !important;
    opacity: 0.8;
}

/* H5: Минимальный */
.toc-item-h5::before {
    font-weight: var(--font-normal) !important;
    font-size: var(--font-xs);
    opacity: 0.7;
}

/* ============================================
   8. ССЫЛКИ (СОХРАНЕНИЕ ОРИГИНАЛЬНЫХ СТИЛЕЙ)
   ============================================ */
.toc-list a {
    color: var(--text-primary) !important;
    text-decoration: none !important;
    transition: var(--transition-base);
    display: inline !important;
    padding: 2px 4px;
    border-radius: var(--radius-sm);
    border-bottom: none !important;
}

/* Посещённые ссылки */
.toc-block .toc-list a.visited,
.toc-block .toc-content .toc-list a.visited {
    color: #ff0000 !important;
}

.toc-list a:hover {
    color: var(--info) !important;
    background: rgba(var(--info-rgb), 0.1);
}

/* Активная ссылка (при скролле) */
.toc-list a.active {
    color: var(--info) !important;
    font-weight: var(--font-semibold) !important;
}

/* ============================================
   9. SMOOTH SCROLL
   ============================================ */
html {
    scroll-behavior: smooth;
}

[id] {
    scroll-margin-top: 100px;
}

/* ============================================
   10. АДАПТИВНОСТЬ
   ============================================ */
@media (max-width: 768px) {
    .toc-block {
        display: block !important;
        width: 100% !important;
    }
    
    .toc-title {
        font-size: var(--font-md);
    }
    
    .toc-list .toc-item-h3 {
        padding-left: 20px !important;
    }
    
    .toc-list .toc-item-h4 {
        padding-left: 40px !important;
    }
    
    .toc-list .toc-item-h5 {
        padding-left: 60px !important;
    }
}

@media (max-width: 480px) {
    .toc-header {
        padding: var(--spacing-xs) var(--spacing-sm);
    }
    
    .toc-title {
        font-size: var(--font-md);
    }
    
    .toc-toggle {
        width: 20px;
        height: 20px;
        font-size: var(--font-lg);
    }
    
    .toc-list .toc-item-h3 {
        padding-left: 16px !important;
    }
    
    .toc-list .toc-item-h4 {
        padding-left: 32px !important;
    }
    
    .toc-list .toc-item-h5 {
        padding-left: 48px !important;
    }
    
    .toc-list a {
        font-size: 16px;
    }
}
