/* ============================================
   Layout — Sidebar, Main, Header, Footer
   ============================================ */

/* ============ Progress Bar (CSS scroll-driven) ============ */
.progress-bar {
    position: fixed;
    top: 0;
    left: 0;
    width: 100%;
    height: 3px;
    background: var(--accent-gradient);
    z-index: var(--z-progress);
    transform-origin: left;
    scale: 0 1;
    animation: grow-progress auto linear forwards;
    animation-timeline: scroll(root);
}

@keyframes grow-progress {
    from { scale: 0 1; }
    to { scale: 1 1; }
}

/* JS fallback for browsers without scroll-timeline */
.no-scroll-timeline .progress-bar {
    animation: none;
    scale: none;
    width: 0%;
    transition: width 100ms linear;
}

/* ============ Sidebar ============ */
.sidebar {
    position: fixed;
    top: 0;
    left: 0;
    width: var(--sidebar-width);
    height: 100dvh;
    background: var(--bg-sidebar);
    border-right: 1px solid var(--border-color);
    backdrop-filter: blur(20px);
    z-index: var(--z-sidebar);
    display: flex;
    flex-direction: column;
    transition: transform var(--duration-normal) var(--ease-out);
    overflow: hidden;
    contain: layout style;
}

.sidebar__header {
    padding: var(--space-lg);
    border-bottom: 1px solid var(--border-color);
    display: flex;
    align-items: center;
    justify-content: space-between;
    flex-shrink: 0;
}

.sidebar__logo {
    display: flex;
    align-items: center;
    gap: var(--space-xs);
}

.sidebar__logo-icon {
    font-size: 28px;
    line-height: 1;
}

.sidebar__logo-text {
    font-size: var(--text-sm);
    font-weight: 700;
    line-height: 1.2;
    background: var(--accent-gradient);
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
}

.sidebar__close {
    display: none;
    color: var(--text-secondary);
    font-size: 20px;
    padding: var(--space-2xs);
    border-radius: var(--radius-sm);

    &:hover {
        color: var(--text-primary);
        background: var(--bg-input);
    }
}

.sidebar__search {
    padding: var(--space-sm) var(--space-md);
    border-bottom: 1px solid var(--border-color);
    flex-shrink: 0;
}

.sidebar__search-input {
    width: 100%;
    padding: var(--space-xs) var(--space-sm);
    background: var(--bg-input);
    border: 1px solid var(--border-color);
    border-radius: var(--radius-sm);
    font-size: var(--text-sm);
    outline: none;
    transition: all var(--duration-normal) var(--ease-out);

    &::placeholder {
        color: var(--text-muted);
    }

    &:focus {
        border-color: var(--accent-primary);
        box-shadow: 0 0 0 3px var(--accent-glow);
    }
}

.sidebar__search-hint {
    display: inline-block;
    margin-top: var(--space-2xs);
    font-size: 10px;
    color: var(--text-muted);

    & kbd {
        display: inline-block;
        padding: 1px 5px;
        font-size: 10px;
        font-family: var(--font-mono);
        background: var(--bg-input);
        border: 1px solid var(--border-color);
        border-radius: 3px;
    }
}

/* Navigation */
.nav-list {
    padding: var(--space-xs) 0;
    overflow-y: auto;
    flex: 1;
    overscroll-behavior: contain;
}

.nav-category {
    padding: var(--space-md) var(--space-lg) var(--space-2xs);
    font-size: 11px;
    font-weight: 600;
    text-transform: uppercase;
    letter-spacing: 1.2px;
    color: var(--text-muted);
    user-select: none;
}

.nav-link {
    display: flex;
    align-items: center;
    gap: var(--space-xs);
    padding: var(--space-xs) var(--space-lg);
    color: var(--text-secondary);
    font-size: var(--text-sm);
    font-weight: 500;
    transition: all var(--duration-fast) var(--ease-out);
    border-left: 3px solid transparent;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;

    &:hover {
        color: var(--text-primary);
        background: var(--bg-input);
    }

    &[aria-current="true"],
    &.active {
        color: var(--accent-primary);
        border-left-color: var(--accent-primary);
        background: rgba(59, 130, 246, 0.08);
    }

    &[hidden] {
        display: none;
    }

    &--quiz {
        background: linear-gradient(135deg, rgba(59, 130, 246, 0.08), rgba(139, 92, 246, 0.08));
        border-left-color: var(--accent-secondary) !important;
        font-weight: 600;
        margin-top: var(--space-xs);

        &:hover {
            background: linear-gradient(135deg, rgba(59, 130, 246, 0.15), rgba(139, 92, 246, 0.15));
        }
    }
}

/* Sidebar footer */
.sidebar__footer {
    padding: var(--space-md) var(--space-lg);
    border-top: 1px solid var(--border-color);
    flex-shrink: 0;
}

.progress-stats {
    font-size: var(--text-xs);
    color: var(--text-secondary);
}

.progress-stats__bar {
    margin-top: var(--space-xs);
    height: 4px;
    background: var(--bg-input);
    border-radius: 2px;
    overflow: hidden;
}

.progress-stats__fill {
    height: 100%;
    background: var(--accent-gradient);
    border-radius: 2px;
    transition: width var(--duration-slow) var(--ease-out);
    width: 0%;
}

/* ============ Sidebar Overlay (mobile) ============ */
.sidebar-overlay {
    display: none;
    position: fixed;
    inset: 0;
    background: var(--bg-backdrop);
    z-index: calc(var(--z-sidebar) - 1);
    opacity: 0;
    transition: opacity var(--duration-normal) var(--ease-out);

    &--visible {
        display: block;
        opacity: 1;
    }
}

/* ============ Mobile Header ============ */
.mobile-header {
    display: none;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    height: var(--header-height);
    background: var(--bg-sidebar);
    border-bottom: 1px solid var(--border-color);
    backdrop-filter: blur(20px);
    z-index: calc(var(--z-sidebar) - 2);
    align-items: center;
    padding: 0 var(--space-md);
    gap: var(--space-sm);
}

.hamburger {
    display: flex;
    flex-direction: column;
    gap: 5px;
    padding: var(--space-2xs);
    border-radius: var(--radius-sm);

    & span {
        display: block;
        width: 22px;
        height: 2px;
        background: var(--text-primary);
        transition: all var(--duration-normal) var(--ease-out);
        border-radius: 1px;
    }

    &:hover span {
        background: var(--accent-primary);
    }

    &[aria-expanded="true"] {
        & span:nth-child(1) {
            transform: translateY(7px) rotate(45deg);
        }
        & span:nth-child(2) {
            opacity: 0;
        }
        & span:nth-child(3) {
            transform: translateY(-7px) rotate(-45deg);
        }
    }
}

.mobile-header__title {
    font-weight: 600;
    font-size: var(--text-base);
    background: var(--accent-gradient);
    -webkit-background-clip: text;
    background-clip: text;
    -webkit-text-fill-color: transparent;
}

/* ============ Main Content ============ */
.main-content {
    margin-left: var(--sidebar-width);
    min-height: 100dvh;
    contain: style;
}

/* ============ Section ============ */
.section {
    padding: var(--space-3xl) var(--space-2xl);
    max-width: var(--content-max-width);
    margin: 0 auto;
    border-bottom: 1px solid var(--border-color);

    &:last-of-type {
        border-bottom: none;
    }
}

.section__header {
    margin-bottom: var(--space-2xl);
}

.section__title {
    font-size: var(--text-3xl);
    font-weight: 800;
    letter-spacing: -0.5px;
    margin-bottom: var(--space-xs);
}

.section__desc {
    font-size: var(--text-lg);
    color: var(--text-secondary);
}

/* Section loading & idle states */
.section[data-state="loading"],
.section[data-state="idle"] {
    min-height: 200px;
    display: flex;
    align-items: center;
    justify-content: center;
}

.section__loader {
    width: 40px;
    height: 40px;
    border: 3px solid var(--border-color);
    border-top-color: var(--accent-primary);
    border-radius: var(--radius-full);
    animation: spin 0.8s linear infinite;
}

.section[data-state="idle"] .section__loader {
    border-top-color: var(--text-muted);
    opacity: 0.3;
}

@keyframes spin {
    to { transform: rotate(360deg); }
}

/* ============ Footer ============ */
.footer {
    padding: var(--space-2xl);
    border-top: 1px solid var(--border-color);
    text-align: center;

    & p {
        margin-bottom: var(--space-xs);
        font-size: var(--text-sm);
        color: var(--text-secondary);
    }
}

.footer__links {
    display: flex;
    justify-content: center;
    gap: var(--space-lg);
    flex-wrap: wrap;

    & a {
        color: var(--accent-primary);
        font-weight: 500;
        transition: color var(--duration-fast) var(--ease-out);

        &:hover {
            color: var(--accent-secondary);
        }
    }
}

.footer__note {
    font-size: var(--text-xs);
    color: var(--text-muted);
    margin-top: var(--space-xs);
}

/* ============ Responsive ============ */
@media (width <= 768px) {
    .sidebar {
        transform: translateX(-100%);

        &.sidebar--open {
            transform: translateX(0);
            box-shadow: var(--shadow-lg);
        }
    }

    .sidebar__close {
        display: block;
    }

    .mobile-header {
        display: flex;
    }

    .main-content {
        margin-left: 0;
        padding-top: var(--header-height);
    }

    .section {
        padding: var(--space-2xl) var(--space-md);
    }

    .theme-toggle {
        top: 10px;
        right: 10px;
        width: 38px;
        height: 38px;
    }
}

@media (width <= 480px) {
    .sidebar__search-hint {
        display: none;
    }
}
