/**
 * 移动端深度优化样式
 * 专注于小屏幕设备的交互和布局体验
 */

/* ==================== 全局移动端优化 ==================== */

/* 防止 iOS 双击缩放 */
* {
    -webkit-tap-highlight-color: transparent;
    -webkit-touch-callout: none;
}

/* 优化触摸滚动 */
body {
    -webkit-overflow-scrolling: touch;
    overscroll-behavior-y: contain;
    overflow-x: hidden; /* 防止水平滚动 */
}

/* PC端默认隐藏移动端导航按钮和菜单 */
.mobile-menu-btn,
.mobile-nav-menu,
.mobile-nav-overlay {
    display: none;
}

/* 中等屏幕也隐藏面包屑，避免换行 */
@media (max-width: 1024px) {
    .nav-breadcrumb {
        display: none !important;
    }
}

/* 全局布局居中和宽度控制 */
@media (max-width: 768px) {
    html {
        overflow-x: hidden;
    }

    .container {
        max-width: 100%;
        width: 100%;
        margin: 0 auto;
        padding: 16px;
        box-sizing: border-box;
        overflow-x: hidden;
    }

    .user-card {
        margin: 0 auto;
        width: 100%;
        box-sizing: border-box;
        overflow-x: hidden;
    }

    /* 所有卡片和内容区域宽度控制 */
    .message-card,
    .link-card,
    .page-header,
    .page-title,
    .nav-grid,
    .welcome-section {
        width: 100%;
        max-width: 100%;
        box-sizing: border-box;
        overflow-x: hidden;
    }
}

/* ==================== 导航栏移动端优化 ==================== */

@media (max-width: 768px) {
    /* 响应式导航栏 */
    .header {
        position: sticky;
        top: 0;
        z-index: 100;
        background: linear-gradient(135deg, #2563eb 0%, #1d4ed8 100%);
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.1);
        padding: 10px 12px;
    }

    .header-left {
        flex: 1;
        min-width: 0;
    }

    .header h1 {
        font-size: 16px;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        text-align: center;
        flex: 1;
    }

    /* 隐藏面包屑导航 */
    .nav-breadcrumb {
        display: none !important;
    }

    /* 移动端导航菜单按钮 */
    .mobile-menu-btn {
        display: flex !important;
        align-items: center;
        justify-content: center;
        width: 40px;
        height: 40px;
        background: rgba(255, 255, 255, 0.15);
        border: none;
        border-radius: 8px;
        color: white;
        font-size: 20px;
        cursor: pointer;
        transition: all 0.3s;
    }

    .mobile-menu-btn:active {
        background: rgba(255, 255, 255, 0.25);
        transform: scale(0.95);
    }

    /* 调整 header-right 布局，让按钮在右侧 */
    .header-right {
        justify-content: flex-end !important;
        width: auto !important;
    }

    /* 用户信息隐藏，通过菜单访问 */
    .header-right .user-info,
    .header-right .logout-btn {
        display: none;
    }

    /* 二级导航菜单 */
    .mobile-nav-menu {
        position: fixed;
        top: 0;
        right: -100%;
        width: 280px;
        height: 100vh;
        background: white;
        box-shadow: -4px 0 20px rgba(0, 0, 0, 0.2);
        z-index: 9999;
        transition: right 0.3s cubic-bezier(0.4, 0, 0.2, 1);
        overflow-y: auto;
        display: flex;
        flex-direction: column;
    }

    .mobile-nav-menu.active {
        right: 0;
    }

    .mobile-nav-header {
        padding: 20px;
        background: linear-gradient(135deg, #2563eb 0%, #1d4ed8 100%);
        color: white;
        display: flex;
        justify-content: space-between;
        align-items: center;
    }

    .mobile-nav-header h3 {
        margin: 0;
        font-size: 18px;
        font-weight: 600;
    }

    .mobile-nav-close {
        width: 36px;
        height: 36px;
        background: rgba(255, 255, 255, 0.15);
        border: none;
        border-radius: 8px;
        color: white;
        font-size: 20px;
        cursor: pointer;
        display: flex;
        align-items: center;
        justify-content: center;
        transition: all 0.3s;
    }

    .mobile-nav-close:active {
        background: rgba(255, 255, 255, 0.25);
        transform: scale(0.95);
    }

    .mobile-nav-user {
        padding: 20px;
        border-bottom: 1px solid #e2e8f0;
        display: flex;
        align-items: center;
        gap: 12px;
    }

    .mobile-nav-user .user-avatar {
        width: 48px;
        height: 48px;
        background: linear-gradient(135deg, #2563eb 0%, #3b82f6 100%);
        border-radius: 50%;
        display: flex;
        align-items: center;
        justify-content: center;
        color: white;
        font-size: 20px;
        font-weight: 700;
    }

    .mobile-nav-user-info {
        flex: 1;
    }

    .mobile-nav-username {
        font-size: 16px;
        font-weight: 600;
        color: #1e293b;
        margin-bottom: 2px;
    }

    .mobile-nav-role {
        font-size: 12px;
        color: #64748b;
    }

    .mobile-nav-links {
        flex: 1;
        padding: 12px 0;
    }

    .mobile-nav-link {
        display: flex;
        align-items: center;
        gap: 12px;
        padding: 16px 20px;
        color: #1e293b;
        text-decoration: none;
        transition: all 0.2s;
        border-left: 3px solid transparent;
    }

    .mobile-nav-link:active {
        background: #f1f5f9;
    }

    .mobile-nav-link.active {
        background: #eff6ff;
        border-left-color: #2563eb;
        color: #2563eb;
        font-weight: 600;
    }

    .mobile-nav-link i {
        font-size: 20px;
        width: 24px;
        text-align: center;
    }

    .mobile-nav-footer {
        padding: 16px 20px;
        border-top: 1px solid #e2e8f0;
    }

    .mobile-nav-logout {
        width: 100%;
        padding: 12px;
        background: #ef4444;
        color: white;
        border: none;
        border-radius: 8px;
        font-size: 14px;
        font-weight: 600;
        cursor: pointer;
        display: flex;
        align-items: center;
        justify-content: center;
        gap: 8px;
        transition: all 0.2s;
    }

    .mobile-nav-logout:active {
        background: #dc2626;
        transform: scale(0.98);
    }

    /* 遮罩层 */
    .mobile-nav-overlay {
        position: fixed;
        top: 0;
        left: 0;
        right: 0;
        bottom: 0;
        background: rgba(0, 0, 0, 0.5);
        z-index: 9998;
        opacity: 0;
        visibility: hidden;
        transition: all 0.3s;
    }

    .mobile-nav-overlay.active {
        opacity: 1;
        visibility: visible;
    }

    /* 子导航优化 */
    .sub-nav {
        padding: 8px 12px;
        gap: 8px;
        overflow-x: auto;
        overflow-y: hidden;
        white-space: nowrap;
        scrollbar-width: none; /* Firefox */
        -ms-overflow-style: none; /* IE/Edge */
    }

    .sub-nav::-webkit-scrollbar {
        display: none; /* Chrome/Safari */
    }

    .sub-nav a {
        padding: 8px 14px;
        font-size: 13px;
        flex-shrink: 0;
        border-radius: 20px;
    }

    /* 滚动指示器 */
    .sub-nav::after {
        content: '';
        position: absolute;
        right: 0;
        top: 0;
        bottom: 0;
        width: 40px;
        background: linear-gradient(to left, rgba(241, 245, 249, 1), transparent);
        pointer-events: none;
    }
}

/* ==================== 按钮组移动端优化 ==================== */

@media (max-width: 768px) {
    /* 按钮组自动换行并对齐 */
    .button-group,
    .message-actions,
    .action-buttons {
        display: flex;
        flex-wrap: wrap;
        gap: 8px;
        justify-content: flex-start;
        align-items: stretch;
    }

    /* 表格内按钮组 - 统一大小和布局 */
    .user-table td:last-child {
        display: flex !important;
        flex-wrap: wrap;
        gap: 6px !important;
        justify-content: flex-start;
        padding: 12px !important;
    }

    .user-table td:last-child::before {
        display: none !important;
    }

    /* 统一所有按钮尺寸 */
    .user-table .btn,
    .button-group .btn,
    .message-actions .btn,
    .action-buttons .btn {
        flex: 1 1 calc(50% - 3px); /* 每行2个，减去间距 */
        min-width: 0;
        max-width: calc(50% - 3px);
        padding: 10px 8px !important;
        font-size: 12px !important;
        text-align: center;
        white-space: nowrap;
        overflow: hidden;
        text-overflow: ellipsis;
        box-sizing: border-box;
        height: 40px; /* 固定高度 */
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }

    /* 单个按钮占满宽度 */
    .button-group .btn.full-width,
    .action-buttons .btn.full-width {
        flex: 1 1 100%;
        max-width: 100%;
    }

    /* 图标按钮紧凑显示 */
    .btn i {
        margin-right: 4px;
        font-size: 11px;
    }

    /* 移动端按钮最小触摸区域 44x44px (Apple HIG) */
    .btn {
        min-height: 44px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }

    /* 特殊：5个按钮的情况（用户管理页面） */
    .user-table td:last-child .btn:nth-child(1),
    .user-table td:last-child .btn:nth-child(2),
    .user-table td:last-child .btn:nth-child(3),
    .user-table td:last-child .btn:nth-child(4) {
        flex: 1 1 calc(50% - 3px);
        max-width: calc(50% - 3px);
    }
    
    .user-table td:last-child .btn:nth-child(5) {
        flex: 1 1 100%;
        max-width: 100%;
    }
}

/* ==================== 表格卡片式布局 ==================== */

@media (max-width: 768px) {
    /* 隐藏传统表格，使用卡片布局 */
    .user-table,
    .data-table,
    table.responsive-table {
        display: block;
        border: none;
        width: 100%;
    }

    .user-table thead,
    .data-table thead,
    table.responsive-table thead {
        display: none;
    }

    .user-table tbody,
    .data-table tbody,
    table.responsive-table tbody {
        display: block;
        width: 100%;
    }

    .user-table tr,
    .data-table tr,
    table.responsive-table tr {
        display: block;
        margin-bottom: 16px;
        padding: 16px;
        background: white;
        border: 1px solid #e2e8f0;
        border-radius: 12px;
        box-shadow: 0 2px 8px rgba(0, 0, 0, 0.05);
        width: 100%;
        box-sizing: border-box;
    }

    .user-table td,
    .data-table td,
    table.responsive-table td {
        display: flex;
        justify-content: space-between;
        align-items: center;
        padding: 10px 0;
        border: none;
        text-align: right;
        font-size: 13px;
        width: 100%;
        box-sizing: border-box;
    }

    .user-table td:not(:last-child),
    .data-table td:not(:last-child),
    table.responsive-table td:not(:last-child) {
        border-bottom: 1px solid #f1f5f9;
    }

    /* 使用 data-label 显示字段名 */
    .user-table td::before,
    .data-table td::before,
    table.responsive-table td::before {
        content: attr(data-label);
        font-weight: 600;
        color: #64748b;
        font-size: 12px;
        text-align: left;
        flex-shrink: 0;
        margin-right: 12px;
        min-width: 80px;
    }

    /* 操作按钮列特殊处理 */
    .user-table td:last-child,
    .data-table td:last-child {
        display: flex;
        flex-wrap: wrap;
        gap: 6px;
        padding-top: 14px;
        margin-top: 10px;
        border-top: 2px solid #e2e8f0;
        justify-content: flex-start;
    }

    .user-table td:last-child::before,
    .data-table td:last-child::before {
        display: none;
    }

    /* 角色徽章在卡片中的显示 */
    .user-table td .role-badge {
        flex-shrink: 0;
    }
}

/* ==================== 模态框移动端优化 ==================== */

@media (max-width: 768px) {
    .modal-overlay {
        padding: 0;
        align-items: flex-end; /* 从底部弹出 */
    }

    .modal-content {
        max-width: 100%;
        max-height: 85vh;
        margin: 0;
        border-radius: 20px 20px 0 0;
        animation: slideUpMobile 0.3s cubic-bezier(0.4, 0, 0.2, 1);
    }

    @keyframes slideUpMobile {
        from {
            transform: translateY(100%);
        }
        to {
            transform: translateY(0);
        }
    }

    .modal-header {
        padding: 16px;
        border-radius: 20px 20px 0 0;
        position: relative;
    }

    /* 移动端模态框顶部拖动条 */
    .modal-header::before {
        content: '';
        position: absolute;
        top: 8px;
        left: 50%;
        transform: translateX(-50%);
        width: 40px;
        height: 4px;
        background: rgba(255, 255, 255, 0.5);
        border-radius: 2px;
    }

    .modal-header h2 {
        font-size: 18px;
        margin-top: 12px;
    }

    .modal-close {
        width: 32px;
        height: 32px;
        font-size: 20px;
    }

    .modal-body {
        padding: 16px;
        max-height: calc(85vh - 140px);
        overflow-y: auto;
    }

    .modal-footer {
        padding: 12px 16px;
        gap: 8px;
    }

    .modal-footer .btn {
        flex: 1;
    }
}

/* ==================== 表单移动端优化 ==================== */

@media (max-width: 768px) {
    .form-group {
        margin-bottom: 16px;
    }

    .form-group label {
        font-size: 13px;
        margin-bottom: 6px;
        display: block;
    }

    .form-group input,
    .form-group textarea,
    .form-group select {
        width: 100%;
        padding: 12px 14px;
        font-size: 16px; /* 防止 iOS 自动缩放 */
        border-radius: 10px;
        border: 1px solid #cbd5e1;
        -webkit-appearance: none; /* 移除默认样式 */
    }

    .form-group input:focus,
    .form-group textarea:focus,
    .form-group select:focus {
        border-color: #2563eb;
        outline: none;
        box-shadow: 0 0 0 3px rgba(37, 99, 235, 0.1);
    }

    /* 文本域 */
    .form-group textarea {
        min-height: 100px;
        resize: vertical;
    }

    /* 复选框和单选框 */
    .form-group input[type="checkbox"],
    .form-group input[type="radio"] {
        width: auto;
        min-width: 20px;
        min-height: 20px;
    }
}

/* ==================== 卡片网格移动端优化 ==================== */

@media (max-width: 768px) {
    .card-grid,
    .links-grid,
    .settings-grid {
        display: flex;
        flex-direction: column;
        gap: 12px;
        width: 100%;
    }

    .card,
    .link-card,
    .setting-card {
        margin: 0;
        border-radius: 12px;
        width: 100%;
        box-sizing: border-box;
    }

    /* 卡片内容紧凑显示 */
    .card-header,
    .link-card-header {
        padding: 12px 14px;
    }

    .card-body,
    .link-card-body {
        padding: 12px 14px;
    }

    .card-footer {
        padding: 10px 14px;
    }

    /* 确保所有卡片内容居中对齐 */
    .user-card,
    .page-card,
    .content-card {
        width: 100%;
        margin: 0 auto;
        box-sizing: border-box;
    }
}

/* ==================== 消息列表移动端优化 ==================== */

@media (max-width: 768px) {
    .message-item {
        padding: 14px;
        margin-bottom: 12px;
        border-radius: 12px;
    }

    .message-header {
        flex-direction: column;
        align-items: flex-start;
        gap: 10px;
    }

    .message-title {
        font-size: 15px;
        line-height: 1.4;
        word-break: break-word;
    }

    .message-content {
        font-size: 13px;
        line-height: 1.6;
        margin: 10px 0;
    }

    .message-meta {
        font-size: 11px;
        color: #94a3b8;
    }

    .message-actions {
        width: 100%;
        justify-content: flex-start;
        margin-top: 8px;
    }
}

/* ==================== Toast 通知移动端优化 ==================== */

@media (max-width: 768px) {
    .toast-container {
        top: auto;
        bottom: 20px;
        left: 12px;
        right: 12px;
    }

    .toast {
        width: 100%;
        padding: 14px 16px;
        font-size: 14px;
        border-radius: 12px;
        box-shadow: 0 4px 12px rgba(0, 0, 0, 0.15);
    }
}

/* ==================== 搜索框移动端优化 ==================== */

@media (max-width: 768px) {
    .search-box {
        width: 100%;
        margin-bottom: 12px;
    }

    .search-box input {
        width: 100%;
        padding: 10px 38px 10px 38px;
        font-size: 15px;
        border-radius: 22px;
    }

    .search-box i {
        font-size: 14px;
    }
}

/* ==================== 分页移动端优化 ==================== */

@media (max-width: 768px) {
    .pagination {
        justify-content: center;
        gap: 4px;
        flex-wrap: wrap;
    }

    .pagination .btn {
        min-width: 40px;
        height: 40px;
        padding: 0;
        font-size: 14px;
    }

    .pagination .page-info {
        width: 100%;
        text-align: center;
        margin: 8px 0;
        font-size: 12px;
    }
}

/* ==================== 空状态移动端优化 ==================== */

@media (max-width: 768px) {
    .empty-state {
        padding: 40px 20px;
    }

    .empty-state-icon {
        font-size: 48px;
        margin-bottom: 12px;
    }

    .empty-state p {
        font-size: 14px;
        color: #64748b;
    }

    /* 页面标题额外优化 */
    .page-title {
        flex-direction: column;
        gap: 12px;
        align-items: flex-start !important;
    }

    .page-title > span {
        width: 100%;
    }

    .page-title > button,
    .page-title > .btn {
        width: 100% !important;
        max-width: 100%;
        margin: 0 !important;
    }
}

/* ==================== 加载动画移动端优化 ==================== */

@media (max-width: 768px) {
    .loading-overlay {
        background: rgba(255, 255, 255, 0.95);
    }

    .loading-spinner {
        width: 36px;
        height: 36px;
    }

    .loading-text {
        font-size: 13px;
        margin-top: 12px;
    }
}

/* ==================== 角色徽章移动端优化 ==================== */

@media (max-width: 768px) {
    .role-badge {
        padding: 4px 10px;
        font-size: 11px;
        border-radius: 12px;
    }
}

/* ==================== 触摸友好的交互 ==================== */

@media (max-width: 768px) {
    /* 增大可点击区域 */
    a, button, .clickable {
        min-height: 44px;
        min-width: 44px;
        display: inline-flex;
        align-items: center;
        justify-content: center;
    }

    /* 触摸反馈 */
    a:active, button:active, .clickable:active {
        opacity: 0.7;
        transform: scale(0.98);
    }

    /* 移除悬停效果（触摸设备不需要） */
    @media (hover: none) {
        .btn:hover,
        .card:hover,
        a:hover {
            transform: none;
            box-shadow: none;
        }
    }
}

/* ==================== 横屏优化 ==================== */

@media (max-width: 768px) and (orientation: landscape) {
    .modal-content {
        max-height: 90vh;
    }

    .modal-body {
        max-height: calc(90vh - 140px);
    }

    .header {
        padding: 8px 12px;
    }

    .sub-nav {
        padding: 6px 12px;
    }
}

/* ==================== 安全区域（iOS 刘海屏） ==================== */

@supports (padding: env(safe-area-inset-bottom)) {
    @media (max-width: 768px) {
        body {
            padding-bottom: env(safe-area-inset-bottom);
        }

        .header {
            padding-top: max(10px, env(safe-area-inset-top));
        }

        .modal-content {
            padding-bottom: env(safe-area-inset-bottom);
        }

        .toast-container {
            bottom: calc(20px + env(safe-area-inset-bottom));
        }
    }
}

/* ==================== 性能优化 ==================== */

@media (max-width: 768px) {
    /* 减少阴影和渐变，提升性能 */
    .performance-mode .card,
    .performance-mode .btn,
    .performance-mode .modal-content {
        box-shadow: none;
        border: 1px solid #e2e8f0;
    }

    .performance-mode .header {
        background: #667eea;
    }

    /* 禁用动画（可选） */
    @media (prefers-reduced-motion: reduce) {
        * {
            animation: none !important;
            transition: none !important;
        }
    }
}

/* ==================== 移动端页脚优化 ==================== */

@media (max-width: 768px) {
    .footer {
        padding: 16px;
    }
    
    .footer-content p {
        font-size: 12px;
        line-height: 1.8;
        display: flex;
        flex-direction: column;
        align-items: center;
        gap: 2px;
        text-align: center;
    }
    
    /* 隐藏分隔符 */
    .footer-separator {
        display: none;
    }
    
    /* 每行独立显示 */
    .footer-line {
        display: block;
    }
}
