/**
 * TD TOC Automático AAA - CSS Otimizado
 * WCAG AAA Compliant - Contraste 7:1+ e fontes legíveis
 * Version: 3.0
 */

/* ====================================
   VARIÁVEIS AAA
   ==================================== */
:root {
    /* Cores AAA (contraste 7:1 ou superior) */
    --td-blue-aaa: #004D73;        /* Contraste 8.59:1 ✅ */
    --td-blue-light: #006BA1;      /* Contraste 7.12:1 ✅ */
    --td-text-primary: #0F172A;    /* Contraste 16.26:1 ✅ */
    --td-text-secondary: #475569;  /* Contraste 8.59:1 ✅ */
    --td-bg-white: #FFFFFF;
    --td-bg-light: #F8FAFC;
    --td-border: #E2E8F0;

    /* Fontes (dinâmicas via PHP, mas fallback aqui) */
    --td-toc-font-size: 16px;
    --td-toc-title-size: 18px;
    --td-toc-sticky-offset: 20px;

    /* Sombras */
    --td-shadow-sm: 0 2px 8px rgba(0, 77, 115, 0.08);
    --td-shadow-md: 0 4px 12px rgba(0, 77, 115, 0.12);
}

/* ====================================
   SCREEN READER
   ==================================== */
.screen-reader-text {
    position: absolute;
    width: 1px;
    height: 1px;
    padding: 0;
    margin: -1px;
    overflow: hidden;
    clip: rect(0, 0, 0, 0);
    white-space: nowrap;
    border: 0;
}

/* ====================================
   WRAPPER - SIDEBAR MODE
   ==================================== */
.td-toc-wrapper {
    display: grid;
    grid-template-columns: 1fr 280px; /* TOC: 280px */
    gap: 48px;
    max-width: 1600px;
    margin: 0 auto;
    padding: 0 24px;
    align-items: start; /* Importante para sticky */
}

/* ====================================
   MAIN CONTENT
   ==================================== */
.td-toc-main-content {
    min-width: 0; /* Evita overflow */
    max-width: 900px;
}

/* ====================================
   SIDEBAR - STICKY CORRIGIDO
   ==================================== */
.td-toc-sidebar {
    position: sticky;
    top: var(--td-toc-sticky-offset);
    max-height: calc(100vh - var(--td-toc-sticky-offset) - 40px);
    overflow-y: auto;
    overflow-x: hidden;

    /* Scrollbar customizado AAA */
    scrollbar-width: thin;
    scrollbar-color: var(--td-blue-aaa) transparent;
}

.td-toc-sidebar::-webkit-scrollbar {
    width: 6px;
}

.td-toc-sidebar::-webkit-scrollbar-track {
    background: transparent;
}

.td-toc-sidebar::-webkit-scrollbar-thumb {
    background: var(--td-blue-aaa);
    border-radius: 6px;
}

.td-toc-sidebar::-webkit-scrollbar-thumb:hover {
    background: var(--td-blue-light);
}

/* ====================================
   CONTAINER
   ==================================== */
.td-toc-container {
    background: var(--td-bg-white);
    border: 2px solid var(--td-blue-aaa);
    border-radius: 12px;
    padding: 20px;
    box-shadow: var(--td-shadow-sm);
    transition: box-shadow 0.2s ease;
}

.td-toc-container:hover {
    box-shadow: var(--td-shadow-md);
}

/* ====================================
   HEADER
   ==================================== */
.td-toc-header {
    display: flex;
    align-items: center;
    gap: 10px;
    margin-bottom: 16px;
    padding-bottom: 12px;
    border-bottom: 2px solid var(--td-border);
}

.td-toc-icon {
    font-size: 20px;
    line-height: 1;
}

.td-toc-title {
    font-family: 'Oswald', -apple-system, BlinkMacSystemFont, sans-serif;
    font-size: var(--td-toc-title-size);
    font-weight: 700;
    color: var(--td-text-primary);
    margin: 0;
    text-transform: uppercase;
    letter-spacing: 0.5px;
    line-height: 1.2;
}

/* ====================================
   CAMPO DE BUSCA
   ==================================== */
.td-toc-search {
    margin-bottom: 16px;
}

.td-toc-search input {
    width: 100%;
    padding: 10px 12px;
    border: 2px solid var(--td-border);
    border-radius: 8px;
    font-size: 16px; /* AAA: mínimo 16px */
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    color: var(--td-text-primary);
    background: var(--td-bg-light);
    transition: all 0.2s ease;
}

.td-toc-search input:focus {
    outline: none;
    border-color: var(--td-blue-aaa);
    background: var(--td-bg-white);
    box-shadow: 0 0 0 3px rgba(0, 77, 115, 0.1);
}

.td-toc-search input::placeholder {
    color: var(--td-text-secondary);
    opacity: 0.7;
}

/* ====================================
   LISTA
   ==================================== */
.td-toc-list {
    list-style: none;
    padding: 0;
    margin: 0;
}

.td-toc-item {
    margin-bottom: 4px;
}

.td-toc-item.hidden {
    display: none;
}

/* ====================================
   LINKS - TAMANHO AAA
   ==================================== */
.td-toc-link {
    display: block;
    padding: 10px 12px;
    color: var(--td-text-primary);
    text-decoration: none;
    border-radius: 6px;
    font-size: var(--td-toc-font-size); /* Mínimo 16px */
    line-height: 1.5;
    font-family: 'Inter', -apple-system, BlinkMacSystemFont, sans-serif;
    transition: all 0.15s ease;
    border-left: 3px solid transparent;
    font-weight: 500;
}

.td-toc-link:hover {
    background: var(--td-bg-light);
    color: var(--td-blue-aaa);
    border-left-color: var(--td-blue-aaa);
    transform: translateX(2px);
}

.td-toc-link:focus {
    outline: 3px solid var(--td-blue-aaa);
    outline-offset: 2px;
}

.td-toc-link.active {
    background: var(--td-blue-aaa);
    color: var(--td-bg-white);
    border-left-color: var(--td-blue-aaa);
    font-weight: 600;
}

/* H2 links */
.td-toc-link.h2 {
    font-weight: 600;
}

/* H3 links - indentados */
.td-toc-link.h3 {
    padding-left: 24px;
    font-size: calc(var(--td-toc-font-size) - 1px); /* Sempre >= 15px */
    min-font-size: 15px;
    color: var(--td-text-secondary);
}

.td-toc-link.h3:hover,
.td-toc-link.h3.active {
    padding-left: 26px;
}

/* ====================================
   NENHUM RESULTADO
   ==================================== */
.td-toc-no-results {
    display: none;
    text-align: center;
    padding: 16px;
    color: var(--td-text-secondary);
    font-size: 15px;
    font-style: italic;
}

.td-toc-no-results.show {
    display: block;
}

/* ====================================
   INLINE MODE (depois do hero)
   ==================================== */
.td-toc-inline-wrapper {
    margin: 32px 0;
    max-width: 700px;
}

.td-toc-inline-wrapper .td-toc-sidebar {
    position: relative;
    top: 0;
    max-height: none;
}

.td-toc-inline-wrapper .td-toc-container {
    max-height: 500px;
    overflow-y: auto;
}

/* ====================================
   RESPONSIVE
   ==================================== */

/* Tablet landscape (< 1200px) */
@media (max-width: 1200px) {
    .td-toc-wrapper {
        grid-template-columns: 1fr 260px;
        gap: 32px;
    }
}

/* Tablet (< 1024px) */
@media (max-width: 1024px) {
    .td-toc-wrapper {
        grid-template-columns: 1fr;
        gap: 0;
    }

    .td-toc-sidebar {
        position: relative;
        top: 0;
        max-height: none;
        margin-bottom: 32px;
        order: -1; /* TOC antes do conteúdo */
    }

    .td-toc-container {
        max-height: 400px;
        overflow-y: auto;
    }

    .td-toc-main-content {
        max-width: 100%;
    }
}

/* Mobile (< 640px) */
@media (max-width: 640px) {
    .td-toc-wrapper {
        padding: 0 16px;
    }

    .td-toc-container {
        padding: 16px;
        max-height: 300px;
    }

    .td-toc-link {
        padding: 12px 10px;
        font-size: 16px; /* Mantém 16px no mobile */
    }

    .td-toc-link.h3 {
        font-size: 15px;
        padding-left: 20px;
    }

    .td-toc-search input {
        font-size: 16px; /* iOS não dá zoom com 16px+ */
    }
}

/* ====================================
   ACESSIBILIDADE
   ==================================== */

/* Modo escuro (automático) */
@media (prefers-color-scheme: dark) {
    :root {
        --td-bg-white: #1E293B;
        --td-bg-light: #0F172A;
        --td-text-primary: #F1F5F9;
        --td-text-secondary: #CBD5E1;
        --td-border: #334155;
        --td-blue-aaa: #38BDF8; /* Azul claro para fundo escuro */
    }
}

/* Reduzir movimento */
@media (prefers-reduced-motion: reduce) {
    .td-toc-link,
    .td-toc-search input,
    .td-toc-container {
        transition: none;
        transform: none;
    }
}

/* Contraste aumentado */
@media (prefers-contrast: more) {
    .td-toc-container {
        border-width: 3px;
    }

    .td-toc-link {
        border-left-width: 4px;
    }
}

/* ====================================
   PRINT
   ==================================== */
@media print {
    .td-toc-sidebar {
        position: relative !important;
        page-break-inside: avoid;
        max-height: none;
    }

    .td-toc-search {
        display: none;
    }

    .td-toc-wrapper {
        display: block;
    }
}
