/* ========================================
   目录切换按钮
   ======================================== */
.sidebar-toggle {
    --sidebar-toggle-bottom: 20px;
    display: none;
    position: fixed;
    bottom: calc(var(--sidebar-toggle-bottom) + env(safe-area-inset-bottom, 0px));
    left: 20px;
    z-index: 1080;
    padding: 0 20px;
    height: 40px;
    border-radius: 20px;
    background: var(--primary-color);
    color: white;
    border: none;
    outline: none;
    box-shadow: var(--shadow-lg);
    cursor: pointer;
    transition: var(--transition);
    font-size: 0.85rem;
    align-items: center;
    gap: 6px;
    white-space: nowrap;
    -webkit-tap-highlight-color: transparent;
}

.sidebar-toggle.is-visible {
    display: flex !important;
    align-items: center;
    justify-content: center;
    visibility: visible !important;
    opacity: 1 !important;
    pointer-events: auto !important;
}

.sidebar-toggle:hover {
    background: var(--primary-dark);
    transform: scale(1.1);
}

.sidebar-toggle:active {
    transform: scale(0.95);
}

.sidebar-toggle:focus {
    outline: none;
    box-shadow: var(--shadow-lg), 0 0 0 3px rgba(59, 130, 246, 0.4);
}

.sidebar-toggle:focus:not(:focus-visible) {
    box-shadow: var(--shadow-lg);
}

.sidebar-toggle:focus-visible {
    outline: none;
    box-shadow: var(--shadow-lg), 0 0 0 3px rgba(59, 130, 246, 0.4);
}

/* 显示在目录顶部的收起按钮 */
.sidebar-header-toggle {
    display: none;
    align-items: center;
    gap: 0.5rem;
    padding: 0.5rem 1rem;
    background: var(--primary-bg);
    color: var(--primary-color);
    border: none;
    border-radius: var(--radius-md);
    cursor: pointer;
    font-size: 0.85rem;
    font-weight: 500;
    transition: var(--transition);
}

.sidebar-header-toggle:hover {
    background: var(--primary-color);
    color: white;
}

.sidebar-header-toggle i {
    font-size: 0.9rem;
}

/* 平板端：目录从左侧滑出 */
@media (max-width: 1200px) {
    body.resources-page {
        --resources-subject-tabs-height: 88px;
    }

    .resources-subject-toolbar {
        flex-direction: column;
        gap: 0;
        padding-right: 10px;
        padding-left: 10px;
    }

    .resources-subject-tabs {
        width: 100%;
        flex: 0 0 44px;
    }

    .resources-subject-tools {
        width: 100%;
        height: 44px;
        flex: 0 0 44px;
        overflow-x: auto;
        overflow-y: hidden;
        overscroll-behavior-inline: contain;
        -webkit-overflow-scrolling: touch;
        scrollbar-width: none;
        box-shadow: inset 0 -1px #e5e7eb;
    }

    .resources-subject-tools::-webkit-scrollbar {
        display: none;
    }

    .sidebar-toggle {
        display: flex;
        align-items: center;
        justify-content: center;
        left: 20px;
        right: auto;
    }

    .sidebar-header {
        display: flex;
        justify-content: space-between;
        align-items: center;
    }

    .sidebar-header-toggle {
        display: flex;
    }

    .course-layout {
        position: relative;
        margin-top: var(--site-nav-height, 54px);
        height: calc(100vh - var(--site-nav-height, 54px));
        height: calc(100dvh - var(--site-nav-height, 54px));
        max-height: calc(100vh - var(--site-nav-height, 54px));
        max-height: calc(100dvh - var(--site-nav-height, 54px));
    }

    body.resources-page .course-layout {
        margin-top: 0;
        height: calc(100vh - var(--site-nav-height, 54px) - var(--resources-subject-tabs-height) - 1px);
        height: calc(100dvh - var(--site-nav-height, 54px) - var(--resources-subject-tabs-height) - 1px);
        max-height: calc(100vh - var(--site-nav-height, 54px) - var(--resources-subject-tabs-height) - 1px);
        max-height: calc(100dvh - var(--site-nav-height, 54px) - var(--resources-subject-tabs-height) - 1px);
    }

    /* 目录从左侧滑出。 */
    .sidebar {
        position: fixed;
        left: 0;
        right: auto;
        top: var(--site-nav-height, 54px);
        bottom: 0;
        /* 覆盖基础样式的 height: 100%，让 top 与 bottom 共同决定高度。 */
        height: auto;
        width: 300px;
        min-width: 300px;
        z-index: 900;
        transform: translateX(-100%);
        transition: transform 0.3s ease;
        box-shadow: var(--shadow-xl);
    }

    body.resources-page .sidebar {
        top: calc(var(--site-nav-height, 54px) + var(--resources-subject-tabs-height) + 1px);
    }

    .sidebar.visible {
        transform: translateX(0);
    }

    .sidebar.hidden {
        transform: translateX(-100%);
    }

    .sidebar-overlay {
        position: fixed;
        top: var(--site-nav-height, 54px);
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.5);
        z-index: 899;
        opacity: 0;
        visibility: hidden;
        transition: opacity 0.3s ease, visibility 0.3s ease;
    }

    body.resources-page .sidebar-overlay {
        top: calc(var(--site-nav-height, 54px) + var(--resources-subject-tabs-height) + 1px);
    }

    .sidebar-overlay.show {
        opacity: 1;
        visibility: visible;
    }

    .content-area {
        padding: 0;
        margin-left: 0;
        width: 100%;
    }

}

@media (max-width: 768px) {
    .resources-subject-toolbar {
        padding-right: 8px;
        padding-left: 8px;
    }

    .resources-subject-tabs {
        gap: 0.25rem;
    }

    .resources-subject-tab {
        padding: 0 0.8rem;
        font-size: 0.84rem;
    }

    .resources-toolbar-search {
        width: 190px;
    }

    .sidebar {
        width: 85%;
        max-width: 300px;
        /* JavaScript 会按可视视口动态设置 top 和 height。 */
        top: var(--site-nav-height, 54px);
    }

    body.resources-page .sidebar {
        top: calc(var(--site-nav-height, 54px) + var(--resources-subject-tabs-height) + 1px);
    }

    .sidebar-overlay {
        top: var(--site-nav-height, 54px);
    }

    body.resources-page .sidebar-overlay {
        top: calc(var(--site-nav-height, 54px) + var(--resources-subject-tabs-height) + 1px);
    }

    .content-area {
        padding: 0;
        flex: 1;
        height: 100%;
    }

    /* 收藏区域手机端优化 */
    .favorites-header {
        padding: 0.6rem 0.75rem;
    }

    .favorites-list {
        max-height: 150px;
    }

    .favorite-item {
        padding: 0.5rem 0.75rem;
    }

    .favorite-item-title {
        font-size: 0.8rem;
    }

    .sidebar-toggle {
        --sidebar-toggle-bottom: 65px;
        left: max(15px, calc(env(safe-area-inset-left, 0px) + 15px));
    }

    /* GGB 面板手机端优化 */
    .ggb-panel {
        border-radius: 0;
    }

    .ggb-header {
        padding: 0.5rem 1rem;
        flex-wrap: wrap;
        gap: 0.5rem;
    }

    .ggb-header-left h3 {
        font-size: 0.95rem;
    }

    .ggb-path {
        font-size: 0.7rem;
    }

    /* 手机端按钮行允许换行，并使用适合触屏的间距。 */
    .ggb-header-right {
        flex-wrap: wrap;
        gap: 0.3rem;
        width: 100%;
        justify-content: flex-start;
    }

    .ggb-header-right button:not(.rotate-fullscreen-btn) {
        min-width: 44px;
        height: 44px;
        padding: 0 0.65rem;
        font-size: 0.8rem;
    }

    .fullscreen-btn {
        padding: 0.4rem 0.8rem;
        font-size: 0.8rem;
    }

    .fullscreen-btn span {
        position: absolute;
        width: 1px;
        height: 1px;
        padding: 0;
        margin: -1px;
        overflow: hidden;
        clip: rect(0, 0, 0, 0);
        clip-path: inset(50%);
        white-space: nowrap;
        border: 0;
    }

    .ggb-tips {
        padding: 0.4rem 0.75rem;
        font-size: 0.7rem;
    }

    /* 欢迎面板手机端优化 */
    .welcome-panel {
        padding: 1.5rem;
    }

    .welcome-icon {
        font-size: 3rem;
        margin-bottom: 1rem;
    }

    .welcome-panel h2 {
        font-size: 1.2rem;
    }

    .welcome-features {
        flex-direction: column;
        gap: 0.75rem;
    }

    .welcome-feature {
        padding: 0.5rem 1rem;
        font-size: 0.85rem;
    }
}

/* 超小屏幕优化 */
@media (max-width: 480px) {
    .resources-subject-tabs {
        gap: 0;
    }

    .resources-subject-tab {
        padding: 0 0.7rem;
        font-size: 0.8rem;
    }

    .resources-toolbar-search {
        width: 160px;
    }

    .sidebar {
        /* JavaScript 会按可视视口动态设置 top 和 height。 */
        top: var(--site-nav-height, 54px);
    }

    body.resources-page .sidebar {
        top: calc(var(--site-nav-height, 54px) + var(--resources-subject-tabs-height) + 1px);
    }

    .sidebar-overlay {
        top: var(--site-nav-height, 54px);
    }

    body.resources-page .sidebar-overlay {
        top: calc(var(--site-nav-height, 54px) + var(--resources-subject-tabs-height) + 1px);
    }

    .content-area {
        padding: 0;
    }
}

/* ========================================
   浏览模式切换区
   ======================================== */
.view-mode-section {
    display: flex;
    align-items: center;
    padding: 0;
    background: transparent;
    border-bottom: none;
    flex-shrink: 0;
    overflow: hidden;
}

.view-mode-toggle {
    display: flex;
    align-items: center;
    width: 100%;
    min-width: 0;
    background: #f1f5f9;
    border-radius: 12px;
    padding: 4px;
    margin: 12px 12px 8px 12px;
}

.mode-btn {
    display: flex;
    align-items: center;
    justify-content: center;
    gap: 4px;
    padding: 0.6rem 0.5rem;
    border: none;
    border-bottom: none;
    background: transparent;
    color: #475569;
    cursor: pointer;
    transition: color 0.25s cubic-bezier(0.4, 0, 0.2, 1),
        background-color 0.25s cubic-bezier(0.4, 0, 0.2, 1),
        box-shadow 0.25s cubic-bezier(0.4, 0, 0.2, 1);
    font-size: 0.85rem;
    font-weight: 500;
    white-space: nowrap;
    border-radius: 8px;
    flex: 1;
    margin-bottom: 0;
}

.mode-btn:last-child {
    border-right: none;
}

.mode-btn i {
    font-size: 0.9rem;
    opacity: 0.9;
}

.mode-btn:hover {
    color: #3b82f6;
    background: rgba(255, 255, 255, 0.5);
}

.mode-btn.active {
    color: var(--text-primary);
    background: #ffffff;
    font-weight: 600;
    box-shadow: 0 1px 3px rgba(0, 0, 0, 0.05), 0 1px 2px rgba(0, 0, 0, 0.1);
}

.mode-btn.active i {
    color: var(--subject-color, #3b82f6);
}

@media (prefers-reduced-motion: reduce) {
    .sidebar-toggle,
    .sidebar-header-toggle,
    .sidebar,
    .sidebar-overlay,
    .mode-btn {
        transition: none;
    }

    .sidebar-toggle:hover,
    .sidebar-toggle:active {
        transform: none;
    }
}
