/**
 * 移动端汉堡菜单 & 页首 fixed 适配（仅新增/覆盖）
 * 屏幕宽度 ≤ 640px 时生效
 *
 * 层级关系（自下而上）：
 *   正文 / 遮罩 .sidebar-overlay  z-index 1100
 *   抽屉 .sidebar                  z-index 1150
 *   页首 .site-header              z-index 1200  ← 始终在最上层
 *
 * 抽屉从页首下方开始（top: --tmt-header-h），不会压住页首，
 * 所以汉堡按钮打开后仍停在原位置、可直接点它关闭（变成 ×）。
 *
 * 选择器统一加 body 前缀，用于压过各页面内联样式里后出现的 .sidebar 规则。
 */

/* 桌面端隐藏汉堡按钮（确保默认隐藏） */
.mobile-nav-toggle {
    display: none;
}

@media (max-width: 640px) {
    html {
        /* 页首高度，由 mobile-menu.js 实测写入，用于抽屉与遮罩定位 */
        --tmt-header-h: 56px;
        overflow-x: hidden !important;
        width: 100% !important;
        max-width: 100% !important;
        overscroll-behavior-x: none;
        position: relative;
    }
    body {
        overflow-x: hidden !important;
        width: 100% !important;
        max-width: 100vw !important;
        position: relative;
        margin: 0 !important;
        overscroll-behavior-x: none;
    }

    /* 1. 页首 fixed，且层级最高（汉堡按钮打开后仍可点） */
    body .site-header {
        position: fixed;
        top: 0;
        left: 0;
        width: 100%;
        z-index: 1200;
        height: auto !important;
        background: rgba(255, 255, 255, 0.82);
        backdrop-filter: blur(12px);
        -webkit-backdrop-filter: blur(12px);
        border-bottom: 2px solid rgba(37, 99, 235, 0.25);
        box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
    }

    /* 页首单行：汉堡按钮 → LOGO → 公告按钮 → 用户名 */
    body .header-inner {
        height: auto !important;
        min-height: 52px;
        padding: 7px 10px;
        flex-wrap: nowrap;          /* 强制单行，页首不会因为自适应换行而变高 */
        justify-content: space-between;
        align-items: center;
        gap: 0 4px;
        margin: 0;
    }

    /* 汉堡按钮：贴近最左，尺寸固定不参与挤压 */
    body .mobile-nav-toggle {
        display: flex;
        flex-direction: column;
        justify-content: center;
        align-items: center;
        width: 32px;
        height: 32px;
        flex: 0 0 auto;
        background: transparent;
        border: none;
        cursor: pointer;
        gap: 4px;
        padding: 4px;
        order: 0;
    }
    body .mobile-nav-toggle span {
        display: block;
        width: 20px;
        height: 2px;
        background: #0f1a2e;
        border-radius: 2px;
        transition: all 0.3s ease;
    }
    body .mobile-nav-toggle.open span:nth-child(1) {
        transform: translateY(6px) rotate(45deg);
    }
    body .mobile-nav-toggle.open span:nth-child(2) {
        opacity: 0;
    }
    body .mobile-nav-toggle.open span:nth-child(3) {
        transform: translateY(-6px) rotate(-45deg);
    }

    /* LOGO：吃掉剩余空间，等比缩放（不裁剪、不变形） */
    body .logo-area {
        order: 1;
        flex: 1 1 auto;
        min-width: 0;
        display: flex;
        align-items: center;
        justify-content: center;
        overflow: hidden;
    }
    /* 部分页面的 LOGO 外面还包了一层 <a>，一并允许收缩 */
    body .logo-area > a {
        display: flex;
        align-items: center;
        justify-content: center;
        flex: 0 1 auto;
        min-width: 0;
        max-width: 100%;
    }
    body .logo-area img {
        display: block;
        height: auto;
        width: auto;
        min-width: 0;
        max-width: 100%;
        max-height: 36px;
        object-fit: contain;
        border-radius: 4px;
    }

    /* 招新页的北京时间（若有）：排在 LOGO 之后、公告按钮之前，不抢 LOGO 空间 */
    body .header-time {
        order: 2;
        flex: 0 0 auto;
        margin: 0;
        font-size: 12px;
        font-weight: 600;
        white-space: nowrap;
    }

    /* 公告按钮（由 announcement.js 注入到 .user-area 之前） */
    body .header-inner .tmt-ann-header-btn {
        order: 3;
        flex: 0 0 auto;
        width: 30px;
        height: 30px;
        margin: 0;
    }
    body .header-inner .tmt-ann-header-btn svg {
        width: 16px;
        height: 16px;
    }

    /* 用户名：固定宽度上限并省略，保证 LOGO 有足够空间 */
    body .user-area {
        order: 4;
        flex: 0 0 auto;
        min-width: 0;
        margin-left: 0;
        gap: 0;
    }
    body .auth-btn { font-size: 12px; padding: 4px 12px; white-space: nowrap; }
    body .user-menu-trigger { padding: 5px 8px; gap: 4px; max-width: 100%; }
    body .user-name {
        font-size: 12px;
        min-width: 0;
        max-width: 7em;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }
    body .user-role { font-size: 11px; white-space: nowrap; flex-shrink: 0; }
    body .arrow-down { font-size: 9px; }

    /* 隐藏页首原来的搜索框（它会在汉堡面板里出现） */
    body .header-search {
        display: none !important;
    }

    /* 2. 侧边栏改造为页首下方的左侧抽屉：固定全高、可滚动 */
    body .sidebar {
        display: block !important;   /* 覆盖部分页面里的 .sidebar{display:none} */
        position: fixed;
        top: var(--tmt-header-h, 56px);
        left: 0;
        width: 86vw;
        max-width: 340px;
        height: calc(100vh - var(--tmt-header-h, 56px));      /* 旧浏览器回退 */
        height: calc(100dvh - var(--tmt-header-h, 56px));     /* 动态视口，底部不被工具栏裁掉 */
        box-sizing: border-box;
        padding: 0 18px calc(24px + env(safe-area-inset-bottom));
        background: rgba(255, 255, 255, 0.98);
        backdrop-filter: blur(10px);
        -webkit-backdrop-filter: blur(10px);
        box-shadow: 2px 0 20px rgba(0, 0, 0, 0.18);
        border-right: none;
        z-index: 1150;               /* 低于页首(1200)，高于遮罩(1100) */
        transform: translateX(-100%);
        transition: transform 0.3s ease;
        overflow-y: auto;
        overflow-x: hidden;
        overscroll-behavior: contain;
        -webkit-overflow-scrolling: touch;
    }
    body .sidebar.open {
        transform: translateX(0);
    }

    /* 面板顶部：标题 + 搜索框（滚动时吸顶，保证搜索框始终完整可见） */
    body .mobile-panel-top {
        position: sticky;
        top: 0;
        z-index: 3;
        margin: 0 -18px;
        padding: 12px 18px 10px;
        background: rgba(255, 255, 255, 0.98);
        backdrop-filter: blur(10px);
        -webkit-backdrop-filter: blur(10px);
        border-bottom: 1px solid rgba(0, 0, 0, 0.06);
    }
    body .mobile-panel-head {
        display: flex;
        align-items: center;
        gap: 8px;
        margin: 0 0 10px;
    }
    body .mobile-panel-title {
        display: flex;
        align-items: center;
        gap: 8px;
        font-size: 15px;
        font-weight: 600;
        color: var(--text-dark, #0f1a2e);
    }
    body .mobile-panel-title::before {
        content: '';
        width: 3px;
        height: 16px;
        border-radius: 2px;
        background: #2563eb;
    }

    /* 面板内搜索框（由 JS 注入） */
    body .mobile-panel-search {
        display: block;
        margin: 0;
    }
    body .mobile-panel-search .search-form {
        display: flex;
        align-items: center;
        width: 100%;
        max-width: none;
        margin: 0;
        background: #f5f8fd;
        border: 1px solid rgba(37, 99, 235, 0.28);
        border-radius: 24px;
        overflow: hidden;
    }
    body .mobile-panel-search .search-form:focus-within {
        border-color: var(--primary-blue, #2563eb);
        box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.12);
    }
    body .mobile-panel-search .search-form input {
        flex: 1 1 auto;
        min-width: 0;                /* 允许收缩，输入框不会被按钮挤出面板 */
        padding: 9px 14px;
        font-size: 14px;
        color: var(--text-dark, #0f1a2e);
        background: transparent;
        border: none;
        outline: none;
    }
    body .mobile-panel-search .search-form button {
        flex: 0 0 auto;              /* 按钮不缩小 */
        display: flex;
        align-items: center;
        gap: 6px;
        padding: 9px 16px;
        font-size: 14px;
        color: #fff;
        background: #2563eb;
        border: none;
        cursor: pointer;
        white-space: nowrap;
    }

    /* 面板顶部已提供标题，隐藏页内原有的标题与分割线，避免重复 */
    body .sidebar-title,
    body .sidebar-divider {
        display: none !important;
    }

    /* 导航列表竖向（flex-shrink:0 保证内容不被压缩，超出部分由面板滚动） */
    body .sidebar-list {
        display: flex;
        flex-direction: column;
        list-style: none;
        margin: 0;
        padding: 8px 0 0;
        gap: 4px;
        overflow: visible;
    }
    body .sidebar-list li {
        flex-shrink: 0;
        border-bottom: 1px solid rgba(0, 0, 0, 0.05);
    }
    body .sidebar-list li:last-child {
        border-bottom: none;
    }
    body .sidebar-list a {
        display: block;
        padding: 12px 16px;
        font-size: 15px;
        white-space: normal;
        border-radius: 8px;
        background: transparent;
        color: var(--text-mid, #4a5b74);
        transition: background 0.2s ease, color 0.2s ease;
    }
    body .sidebar-list a::before {
        display: none;
    }
    body .sidebar-list a:hover,
    body .sidebar-list a:active {
        background: rgba(37, 99, 235, 0.10);
        color: var(--primary-blue, #2563eb);
    }
    body .sidebar-list a.active {
        background: rgba(37, 99, 235, 0.12);
        color: var(--primary-blue, #2563eb);
        font-weight: 600;
    }

    /* 部分页面（如招新管理后台）抽屉里用的是 .sidebar-nav，保持同样的行样式 */
    body .sidebar-nav {
        display: block;
        list-style: none;
        margin: 0;
        padding: 8px 0 0;
        overflow: visible;
    }
    body .sidebar-nav li {
        flex-shrink: 0;
        border-bottom: 1px solid rgba(0, 0, 0, 0.05);
    }
    body .sidebar-nav li:last-child {
        border-bottom: none;
    }
    body .sidebar-nav a {
        display: block;
        padding: 12px 16px;
        font-size: 15px;
        white-space: normal;
        border-radius: 8px;
        background: transparent;
        color: var(--text-mid, #4a5b74);
        transition: background 0.2s ease, color 0.2s ease;
    }
    body .sidebar-nav a::before {
        display: none;
    }
    body .sidebar-nav a:hover,
    body .sidebar-nav a:active {
        background: rgba(37, 99, 235, 0.10);
        color: var(--primary-blue, #2563eb);
    }
    body .sidebar-nav a.active {
        background: rgba(37, 99, 235, 0.12);
        color: var(--primary-blue, #2563eb);
        font-weight: 600;
    }
    /* 二级菜单：缩进一级，层级更清楚 */
    body .sidebar-subnav {
        list-style: none;
        margin: 0 0 6px;
        padding: 0 0 0 12px;
    }
    body .sidebar-subnav li {
        border-bottom: none;
    }
    body .sidebar-subnav a {
        padding: 9px 14px;
        font-size: 14px;
        color: var(--text-mid, #4a5b74);
    }

    /* 遮罩层：从页首下方铺满，页首保持可点（汉堡按钮在原位置变 × 关闭） */
    body .sidebar-overlay {
        display: none;
        position: fixed;
        top: var(--tmt-header-h, 56px);
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.5);
        z-index: 1100;
        touch-action: none;          /* 遮罩上滑动不带动页面滚动 */
        overscroll-behavior: none;
        -webkit-tap-highlight-color: transparent;
    }
    body .sidebar-overlay.active {
        display: block;
    }
}

/* 超窄屏：隐藏身份组，把空间让给 LOGO */
@media (max-width: 380px) {
    body .user-role {
        display: none;
    }
}
