/**
 * AutoTOC Plugin Styles
 * 
 * Modern, elegant table of contents styling
 * Compatible with YOOtheme and standard Joomla templates
 * 
 * @package    Joomla.Plugin
 * @subpackage Content.autotoc
 * @copyright  (C) 2025 Your Company. All rights reserved.
 * @license    GNU General Public License version 2 or later
 */

/* ==========================================
   CSS Variables (customizable via params)
   ========================================== */
:root {
    --autotoc-primary: #1e3a5f;
    --autotoc-hover: #2563eb;
    --autotoc-active: #3b82f6;
    --autotoc-bg: #f8fafc;
    --autotoc-border: #e2e8f0;
    --autotoc-radius: 8px;
    --autotoc-transition: 0.2s ease;
    --autotoc-shadow: 0 1px 3px rgba(0, 0, 0, 0.1);
    --autotoc-shadow-hover: 0 4px 12px rgba(0, 0, 0, 0.15);
}

/* ==========================================
   Main TOC Container
   ========================================== */
.autotoc {
    background: var(--autotoc-bg);
    border: 1px solid var(--autotoc-border);
    border-radius: var(--autotoc-radius);
    margin-bottom: 2rem;
    overflow: hidden;
    font-family: -apple-system, BlinkMacSystemFont, 'Segoe UI', Roboto, Oxygen, Ubuntu, sans-serif;
    box-shadow: var(--autotoc-shadow);
}

/* ==========================================
   TOC Header
   ========================================== */
.autotoc-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 0.875rem 1.25rem;
    background: linear-gradient(135deg, var(--autotoc-primary) 0%, color-mix(in srgb, var(--autotoc-primary) 80%, black) 100%);
    color: white;
}

.autotoc-title {
    display: flex;
    align-items: center;
    gap: 0.5rem;
    font-weight: 600;
    font-size: 0.95rem;
    letter-spacing: 0.01em;
}

.autotoc-title-icon {
    width: 18px;
    height: 18px;
    opacity: 0.9;
}

/* ==========================================
   Toggle Button
   ========================================== */
.autotoc-toggle {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 28px;
    height: 28px;
    padding: 0;
    background: rgba(255, 255, 255, 0.15);
    border: none;
    border-radius: 4px;
    cursor: pointer;
    transition: background var(--autotoc-transition), transform var(--autotoc-transition);
    color: white;
}

.autotoc-toggle:hover {
    background: rgba(255, 255, 255, 0.25);
}

.autotoc-toggle:focus {
    outline: 2px solid rgba(255, 255, 255, 0.5);
    outline-offset: 2px;
}

.autotoc-icon {
    width: 16px;
    height: 16px;
    transition: transform var(--autotoc-transition);
}

.autotoc-toggle[aria-expanded="false"] .autotoc-icon,
.autotoc-icon-collapsed {
    transform: rotate(-90deg);
}

/* ==========================================
   TOC Content
   ========================================== */
.autotoc-content {
    padding: 1rem 1.25rem;
    max-height: 500px;
    overflow: hidden;
    transition: max-height 0.3s ease, padding 0.3s ease, opacity 0.3s ease;
}

.autotoc-collapsed .autotoc-content {
    max-height: 0;
    padding-top: 0;
    padding-bottom: 0;
    opacity: 0;
}

/* ==========================================
   List Layout
   ========================================== */
.autotoc-items-list {
    display: flex;
    flex-direction: column;
    gap: 0.125rem;
}

.autotoc-items-list .autotoc-link {
    display: flex;
    align-items: baseline;
    gap: 0.5rem;
    padding: 0.5rem 0.75rem;
    color: var(--autotoc-primary);
    text-decoration: none;
    border-radius: 6px;
    transition: all var(--autotoc-transition);
    font-size: 0.9rem;
    line-height: 1.4;
}

.autotoc-items-list .autotoc-link:hover {
    background: rgba(37, 99, 235, 0.08);
    color: var(--autotoc-hover);
    padding-left: 1rem;
}

.autotoc-items-list .autotoc-link.is-active {
    background: rgba(59, 130, 246, 0.12);
    color: var(--autotoc-active);
    font-weight: 500;
    border-left: 3px solid var(--autotoc-active);
    margin-left: -3px;
}

/* Indentation for nested headings */
.autotoc-items-list .autotoc-link[data-indent="1"] {
    padding-left: 1.75rem;
}
.autotoc-items-list .autotoc-link[data-indent="2"] {
    padding-left: 2.5rem;
}
.autotoc-items-list .autotoc-link[data-indent="3"] {
    padding-left: 3.25rem;
}
.autotoc-items-list .autotoc-link[data-indent="4"] {
    padding-left: 4rem;
}

/* ==========================================
   Grid Layout
   ========================================== */
.autotoc-items-grid {
    display: grid;
    grid-template-columns: repeat(auto-fill, minmax(200px, 1fr));
    gap: 0.75rem;
}

.autotoc-items-grid .autotoc-link {
    display: flex;
    flex-direction: column;
    gap: 0.25rem;
    padding: 0.875rem 1rem;
    background: white;
    border: 1px solid var(--autotoc-border);
    border-radius: 6px;
    color: var(--autotoc-primary);
    text-decoration: none;
    transition: all var(--autotoc-transition);
    font-size: 0.875rem;
    line-height: 1.35;
}

.autotoc-items-grid .autotoc-link:hover {
    border-color: var(--autotoc-hover);
    box-shadow: var(--autotoc-shadow-hover);
    transform: translateY(-2px);
    color: var(--autotoc-hover);
}

.autotoc-items-grid .autotoc-link.is-active {
    border-color: var(--autotoc-active);
    background: linear-gradient(135deg, rgba(59, 130, 246, 0.08) 0%, rgba(59, 130, 246, 0.04) 100%);
    color: var(--autotoc-active);
}

/* ==========================================
   Numbering
   ========================================== */
.autotoc-number {
    font-weight: 600;
    font-size: 0.8em;
    color: var(--autotoc-primary);
    opacity: 0.7;
    min-width: 2em;
    flex-shrink: 0;
}

.autotoc-items-grid .autotoc-number {
    font-size: 0.75rem;
    color: var(--autotoc-hover);
    opacity: 0.8;
}

/* ==========================================
   Sticky Bar
   ========================================== */
.autotoc-sticky-bar {
    position: fixed;
    top: -100%;
    left: 0;
    right: 0;
    z-index: 999;
    padding: 0;
    background: linear-gradient(135deg, var(--autotoc-primary) 0%, color-mix(in srgb, var(--autotoc-primary) 85%, black) 100%);
    box-shadow: 0 2px 10px rgba(0, 0, 0, 0.2);
    transition: top 0.3s ease;
}

.autotoc-sticky-bar.is-visible {
    top: 0;
}

.autotoc-sticky-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 0.5rem;
    width: 100%;
    padding: 0.75rem 1.5rem;
    background: transparent;
    border: none;
    color: white;
    font-size: 0.9rem;
    font-weight: 500;
    cursor: pointer;
    transition: background var(--autotoc-transition);
}

.autotoc-sticky-btn:hover {
    background: rgba(255, 255, 255, 0.1);
}

.autotoc-sticky-btn svg {
    width: 18px;
    height: 18px;
}

/* ==========================================
   Sidebar (Wikipedia Style)
   ========================================== */
.autotoc-sidebar {
    position: fixed;
    top: 50%;
    transform: translateY(-50%);
    z-index: 998;
}

.autotoc-sidebar-left {
    left: 0;
}

.autotoc-sidebar-right {
    right: 0;
}

.autotoc-sidebar-toggle {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 44px;
    height: 44px;
    background: var(--autotoc-primary);
    border: none;
    color: white;
    cursor: pointer;
    transition: all var(--autotoc-transition);
    box-shadow: var(--autotoc-shadow-hover);
}

.autotoc-sidebar-left .autotoc-sidebar-toggle {
    border-radius: 0 var(--autotoc-radius) var(--autotoc-radius) 0;
}

.autotoc-sidebar-right .autotoc-sidebar-toggle {
    border-radius: var(--autotoc-radius) 0 0 var(--autotoc-radius);
}

.autotoc-sidebar-toggle:hover {
    background: var(--autotoc-hover);
    transform: scale(1.05);
}

.autotoc-sidebar-toggle svg {
    width: 20px;
    height: 20px;
}

/* Sidebar Content */
.autotoc-sidebar-content {
    position: fixed;
    top: 0;
    bottom: 0;
    width: 320px;
    max-width: 90vw;
    background: white;
    box-shadow: var(--autotoc-shadow-hover);
    transform: translateX(-100%);
    transition: transform 0.3s ease;
    display: flex;
    flex-direction: column;
    z-index: 1000;
}

.autotoc-sidebar-left .autotoc-sidebar-content {
    left: 0;
    transform: translateX(-100%);
}

.autotoc-sidebar-right .autotoc-sidebar-content {
    right: 0;
    transform: translateX(100%);
}

.autotoc-sidebar.is-open .autotoc-sidebar-content {
    transform: translateX(0);
}

.autotoc-sidebar-header {
    display: flex;
    align-items: center;
    justify-content: space-between;
    padding: 1rem 1.25rem;
    background: var(--autotoc-primary);
    color: white;
}

.autotoc-sidebar-title {
    font-weight: 600;
    font-size: 1rem;
}

.autotoc-sidebar-close {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 32px;
    height: 32px;
    padding: 0;
    background: rgba(255, 255, 255, 0.15);
    border: none;
    border-radius: 4px;
    color: white;
    cursor: pointer;
    transition: background var(--autotoc-transition);
}

.autotoc-sidebar-close:hover {
    background: rgba(255, 255, 255, 0.25);
}

.autotoc-sidebar-close svg {
    width: 18px;
    height: 18px;
}

/* Sidebar Items */
.autotoc-sidebar-items {
    flex: 1;
    overflow-y: auto;
    padding: 1rem 0;
}

.autotoc-sidebar-link {
    display: flex;
    align-items: baseline;
    gap: 0.5rem;
    padding: 0.625rem 1.25rem;
    color: var(--autotoc-primary);
    text-decoration: none;
    font-size: 0.9rem;
    line-height: 1.4;
    transition: all var(--autotoc-transition);
    border-left: 3px solid transparent;
}

.autotoc-sidebar-link:hover {
    background: rgba(37, 99, 235, 0.06);
    color: var(--autotoc-hover);
    border-left-color: var(--autotoc-hover);
}

.autotoc-sidebar-link.is-active {
    background: rgba(59, 130, 246, 0.1);
    color: var(--autotoc-active);
    border-left-color: var(--autotoc-active);
    font-weight: 500;
}

/* Sidebar indentation */
.autotoc-sidebar-link[data-indent="1"] {
    padding-left: 2rem;
}
.autotoc-sidebar-link[data-indent="2"] {
    padding-left: 2.75rem;
}
.autotoc-sidebar-link[data-indent="3"] {
    padding-left: 3.5rem;
}
.autotoc-sidebar-link[data-indent="4"] {
    padding-left: 4.25rem;
}

/* Sidebar Overlay */
.autotoc-sidebar-overlay {
    position: fixed;
    inset: 0;
    background: rgba(0, 0, 0, 0.4);
    opacity: 0;
    visibility: hidden;
    transition: all 0.3s ease;
    z-index: 999;
}

.autotoc-sidebar.is-open ~ .autotoc-sidebar-overlay,
.autotoc-sidebar.is-open .autotoc-sidebar-overlay {
    opacity: 1;
    visibility: visible;
}

/* ==========================================
   Smooth Scroll Target Highlight
   ========================================== */
:target {
    scroll-margin-top: 100px;
}

@keyframes autotoc-highlight {
    0% {
        background: rgba(59, 130, 246, 0.2);
    }
    100% {
        background: transparent;
    }
}

.autotoc-target-highlight {
    animation: autotoc-highlight 1.5s ease-out;
}

/* ==========================================
   Responsive Design
   ========================================== */
@media (max-width: 768px) {
    .autotoc {
        margin-bottom: 1.5rem;
    }
    
    .autotoc-header {
        padding: 0.75rem 1rem;
    }
    
    .autotoc-content {
        padding: 0.75rem 1rem;
    }
    
    .autotoc-items-grid {
        grid-template-columns: 1fr;
    }
    
    .autotoc-items-list .autotoc-link {
        font-size: 0.875rem;
        padding: 0.625rem 0.75rem;
    }
    
    .autotoc-sidebar-content {
        width: 280px;
    }
    
    .autotoc-sidebar-toggle {
        width: 40px;
        height: 40px;
    }
}

@media (max-width: 480px) {
    .autotoc-title {
        font-size: 0.875rem;
    }
    
    .autotoc-items-grid .autotoc-link {
        padding: 0.75rem;
    }
    
    /* Hide sidebar toggle on very small screens, use sticky bar instead */
    .autotoc-sidebar-toggle {
        width: 36px;
        height: 36px;
    }
}

/* ==========================================
   Print Styles
   ========================================== */
@media print {
    .autotoc {
        box-shadow: none;
        border: 1px solid #ddd;
        page-break-inside: avoid;
    }
    
    .autotoc-header {
        background: #333 !important;
        -webkit-print-color-adjust: exact;
        print-color-adjust: exact;
    }
    
    .autotoc-toggle,
    .autotoc-sticky-bar,
    .autotoc-sidebar {
        display: none !important;
    }
    
    .autotoc-collapsed .autotoc-content {
        max-height: none;
        opacity: 1;
        padding: 1rem 1.25rem;
    }
}

/* ==========================================
   Accessibility
   ========================================== */
@media (prefers-reduced-motion: reduce) {
    .autotoc,
    .autotoc-content,
    .autotoc-toggle,
    .autotoc-icon,
    .autotoc-link,
    .autotoc-items-grid .autotoc-link,
    .autotoc-sticky-bar,
    .autotoc-sidebar-content,
    .autotoc-sidebar-toggle {
        transition: none;
    }
    
    .autotoc-target-highlight {
        animation: none;
    }
}

/* Focus visible for keyboard navigation */
.autotoc-link:focus-visible,
.autotoc-sidebar-link:focus-visible {
    outline: 2px solid var(--autotoc-active);
    outline-offset: 2px;
}

/* High contrast mode support */
@media (prefers-contrast: high) {
    .autotoc {
        border-width: 2px;
    }
    
    .autotoc-link,
    .autotoc-sidebar-link {
        text-decoration: underline;
    }
}
