/**
 * 移动端响应式样式
 * 适配手机和平板设备，不影响 PC 端显示
 */

/* ==================== 移动端侧边栏控制 ==================== */

/* 汉堡菜单按钮（仅移动端显示） */
.mobile-menu-btn {
    display: none;
    position: fixed;
    top: 12px;
    right: 16px;
    z-index: 1000;
    width: 44px;
    height: 44px;
    background-color: #3b82f6;
    border-radius: 8px;
    border: none;
    cursor: pointer;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    gap: 4px;
    transition: all 0.3s ease;
}

.mobile-menu-btn span {
    display: block;
    width: 20px;
    height: 2px;
    background-color: white;
    transition: all 0.3s ease;
}

.mobile-menu-btn.active span:nth-child(1) {
    transform: rotate(45deg) translate(5px, 5px);
}

.mobile-menu-btn.active span:nth-child(2) {
    opacity: 0;
}

.mobile-menu-btn.active span:nth-child(3) {
    transform: rotate(-45deg) translate(5px, -5px);
}

/* 移动端遮罩层 */
/*
 * 说明：默认不拦截点击（pointer-events: none），仅在激活时可交互，
 * 解决遮罩层透明但仍挡住页面导致“按钮无法点击/滚动失效”的问题。
 */
.mobile-overlay {
    display: block;
    position: fixed;
    top: 0;
    left: 0;
    right: 0;
    bottom: 0;
    background-color: rgba(0, 0, 0, 0.5);
    z-index: 998;
    opacity: 0;                 /* 默认不可见 */
    pointer-events: none;       /* 默认不拦截事件 */
    transition: opacity 0.3s ease;
}

.mobile-overlay.active {
    opacity: 1;                 /* 激活时可见 */
    pointer-events: auto;       /* 激活时才拦截事件 */
}

/* ==================== 平板设备优化 (768px - 1024px) ==================== */

@media (max-width: 1024px) and (min-width: 768px) {
    /* 侧边栏缩窄 */
    .sidebar {
        width: 200px !important;
    }

    /* 调整内容区域间距 */
    .flex-1.overflow-y-auto {
        padding: 1.5rem !important;
    }

    /* 卡片网格优化 */
    .grid.grid-cols-1.md\\:grid-cols-2.lg\\:grid-cols-4 {
        grid-template-columns: repeat(2, 1fr) !important;
    }
}

/* ==================== 移动端适配 (< 768px) ==================== */

@media (max-width: 767px) {
    /* 显示汉堡菜单（遮罩层仅在激活时交互，无需强制 display） */
    .mobile-menu-btn {
        display: flex;
    }

    /* 顶部导航栏优化 */
    nav.bg-white.shadow-lg {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        z-index: 999;
    }

    nav.bg-white.shadow-lg .px-4 {
        /* 左侧为汉堡按钮预留空间，避免与标题重叠 */
        padding-left: 4rem !important; /* 64px，对应按钮(44px)+边距(16px)+余量 */
        padding-right: 1rem !important;
    }

    nav.bg-white.shadow-lg h1 {
        font-size: 1rem !important;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        max-width: 180px;
    }

    /* 用户名显示优化 */
    #usernameDisplay {
        display: none !important;
    }

    /* 退出按钮优化 */
    nav button[onclick="logout()"] {
        font-size: 0.75rem !important;
        padding: 0.25rem 0.5rem !important;
    }

    /* 主容器调整 */
    .flex[style*="calc(100vh - 64px)"] {
        flex-direction: column;
        height: calc(100vh - 64px) !important;
        margin-top: 64px;
    }

    /* 侧边栏移动端样式 */
    .sidebar {
        position: fixed !important;
        top: 64px;
        left: -256px;
        width: 256px !important;
        height: calc(100vh - 64px) !important;
        z-index: 999;
        transition: left 0.3s ease;
        overflow-y: auto;
    }

    .sidebar.mobile-open {
        left: 0;
    }

    /* 侧边栏链接优化 */
    .sidebar-link {
        padding: 1rem 1.5rem !important;
        font-size: 0.95rem;
    }

    /* 内容区域 */
    .flex-1.overflow-y-auto {
        width: 100% !important;
        padding: 1rem !important;
        margin-top: 0 !important;
    }

    /* 页面标题优化 */
    h2.text-2xl {
        font-size: 1.5rem !important;
        margin-bottom: 1rem !important;
    }

    /* 卡片网格单列显示 */
    .grid.grid-cols-1.md\\:grid-cols-2,
    .grid.grid-cols-1.md\\:grid-cols-2.lg\\:grid-cols-4,
    .grid.grid-cols-1.md\\:grid-cols-3 {
        grid-template-columns: 1fr !important;
        gap: 1rem !important;
    }

    /* 卡片内边距优化 */
    .bg-white.rounded-lg.shadow {
        padding: 1rem !important;
    }

    .bg-white.rounded-lg.shadow .p-6 {
        padding: 1rem !important;
    }

    /* 按钮组优化 */
    .flex.space-x-4,
    .flex.gap-4 {
        flex-wrap: wrap;
        gap: 0.5rem !important;
    }

    .flex.space-x-4 > *,
    .flex.gap-4 > * {
        margin: 0 !important;
    }

    /* 表格优化 */
    table {
        font-size: 0.875rem;
    }

    table th,
    table td {
        padding: 0.5rem !important;
    }

    /* 表单优化 */
    input[type="text"],
    input[type="password"],
    input[type="email"],
    input[type="number"],
    textarea,
    select {
        font-size: 16px !important; /* 防止 iOS 自动放大 */
    }

    /* 标签页按钮优化 */
    .tab-button {
        padding: 0.5rem 1rem !important;
        font-size: 0.875rem !important;
    }

    /* 搜索框优化 */
    input[placeholder*="搜索"],
    input[type="search"] {
        width: 100% !important;
        margin-bottom: 0.5rem;
    }

    /* 登录页面优化 */
    .min-h-screen.flex.items-center.justify-center {
        padding: 1rem;
    }

    .max-w-md {
        max-width: 100% !important;
        margin: 0 1rem;
    }

    /* 仪表盘统计卡片 */
    .text-2xl.font-bold {
        font-size: 1.5rem !important;
    }

    /* 系统资源监控优化 */
    .grid.grid-cols-1.md\\:grid-cols-3.gap-6 {
        gap: 1rem !important;
    }

    /* 弹窗优化 */
    .swal2-popup {
        width: 90% !important;
        padding: 1.5rem !important;
    }
}

/* ==================== 小屏手机优化 (< 375px) ==================== */

@media (max-width: 374px) {
    /* 进一步缩小标题 */
    nav.bg-white.shadow-lg h1 {
        font-size: 0.875rem !important;
        max-width: 140px;
    }

    /* 缩小按钮文字 */
    button,
    .btn {
        font-size: 0.75rem !important;
        padding: 0.5rem 0.75rem !important;
    }

    /* 卡片内边距进一步优化 */
    .bg-white.rounded-lg.shadow {
        padding: 0.75rem !important;
    }

    /* 表格字体更小 */
    table {
        font-size: 0.75rem;
    }
}

/* ==================== 横屏模式优化 ==================== */

@media (max-height: 500px) and (orientation: landscape) {
    /* 侧边栏高度调整 */
    .sidebar {
        height: calc(100vh - 56px) !important;
    }

    /* 减小顶部导航栏高度 */
    nav.bg-white.shadow-lg .flex {
        height: 56px !important;
    }

    /* 主容器高度调整 */
    .flex[style*="calc(100vh - 64px)"] {
        height: calc(100vh - 56px) !important;
        margin-top: 56px;
    }

    /* 减小内边距 */
    .flex-1.overflow-y-auto {
        padding: 0.5rem !important;
    }
}

/* ==================== 触摸优化 ==================== */

@media (hover: none) and (pointer: coarse) {
    /* 增大可点击区域 */
    a,
    button,
    .clickable {
        min-height: 44px;
        min-width: 44px;
    }

    /* 侧边栏链接增大 */
    .sidebar-link {
        min-height: 48px;
    }

    /* 移除 hover 效果，使用 active */
    .hover\\:bg-gray-100:hover {
        background-color: inherit;
    }

    .hover\\:bg-gray-100:active {
        background-color: rgb(243, 244, 246);
    }
}

/* ==================== 深色模式支持（可选） ==================== */

@media (prefers-color-scheme: dark) {
    /* 如需支持深色模式，可在此添加样式 */
}

/* ==================== 移动端控件位置调整（避免与右上角按钮遮挡） ==================== */
@media (max-width: 767px) {
    /*
     * 将汉堡按钮定位到左侧，避免与右上角“退出登录”等按钮相互遮挡，
     * 同时配合上方 nav 左侧 padding 留出布局空间。
     */
    .mobile-menu-btn {
        left: 16px;
        right: auto;
    }
}
