/* ============================================
   掌灯智管系统 移动端375px响应式适配 CSS
   版本：v2.0（基于portal真实DOM修正）
   适配范围：max-width: 768px（重点375px）
   品牌色：主色#0F6E56 辅助#25A84C
   暗色主题：sidebar #1A1A1A, content #2A2A2A, body #1E1E1E
   ============================================ */

/* ===== 桌面端：隐藏移动端专用元素 ===== */
.mobile-hamburger,
.mobile-overlay,
.mobile-sidebar {
    display: none;
}

/* ============================================
   媒体查询：max-width 768px
   ============================================ */
@media screen and (max-width: 768px) {

    /* ---------- 关键修复：覆盖portal内联 min-width:480px ---------- */
    #content-area table,
    #content-area .data-table,
    .card table,
    .card .data-table {
        min-width: 0 !important;
    }
    /* 非卡片表格保留横向滚动 */
    #content-area table:not(.mobile-card-table):not(.mobile-detail-table) {
        min-width: 480px !important;
    }

    /* ---------- 全局重置 ---------- */
    html, body {
        overflow-x: hidden !important;
        max-width: 100% !important;
        min-width: 0 !important;          /* 覆盖 body 的 min-width:1024px */
    }

    /* 根容器：#main-page / .main-container */
    #main-page,
    .main-container {
        width: 100% !important;
        min-height: 100vh;
        margin: 0 !important;
        padding: 0 !important;
    }

    /* 顶部栏保持，但为汉堡按钮留空间 */
    .header {
        padding: 0 12px 0 56px !important;
        height: 48px !important;
        position: sticky !important;
        top: 0;
        z-index: 100;
    }
    .header .system-title {
        font-size: 16px !important;
    }

    /* 隐藏桌面端侧边栏 */
    .sidebar {
        display: none !important;
    }

    /* 内容区：main.content-wrapper */
    main.content-wrapper,
    .content-wrapper {
        margin-left: 0 !important;
        padding: 10px !important;
        width: 100% !important;
        box-sizing: border-box;
        height: auto !important;
        min-height: calc(100vh - 48px);
    }

    /* 内容渲染目标：#content-area */
    #content-area {
        padding: 12px !important;
        min-height: auto !important;
        border-radius: 6px !important;
    }

    /* 面包屑 */
    .breadcrumb {
        font-size: 12px !important;
        margin-bottom: 12px !important;
        padding-bottom: 8px !important;
        white-space: nowrap;
        overflow-x: auto;
        -webkit-overflow-scrolling: touch;
    }

    /* ---------- 汉堡按钮 ---------- */
    .mobile-hamburger {
        display: flex !important;
        position: fixed;
        top: 6px;
        left: 8px;
        z-index: 10003;
        width: 36px;
        height: 36px;
        background: #0F6E56;
        border: none;
        border-radius: 6px;
        cursor: pointer;
        align-items: center;
        justify-content: center;
        flex-direction: column;
        gap: 4px;
        box-shadow: 0 2px 6px rgba(0,0,0,0.4);
    }
    .mobile-hamburger span {
        display: block;
        width: 18px;
        height: 2px;
        background: #fff;
        border-radius: 1px;
        transition: all 0.25s;
    }
    .mobile-hamburger.active span:nth-child(1) {
        transform: translateY(6px) rotate(45deg);
    }
    .mobile-hamburger.active span:nth-child(2) {
        opacity: 0;
    }
    .mobile-hamburger.active span:nth-child(3) {
        transform: translateY(-6px) rotate(-45deg);
    }

    /* ---------- 遮罩层 ---------- */
    .mobile-overlay {
        display: none;
        position: fixed;
        top: 0; left: 0;
        width: 100%; height: 100%;
        background: rgba(0,0,0,0.55);
        z-index: 10001;
    }
    .mobile-overlay.show {
        display: block;
    }

    /* ---------- 移动端侧边栏 ---------- */
    .mobile-sidebar {
        display: flex !important;
        flex-direction: column;
        position: fixed;
        top: 0; left: -280px;
        width: 270px;
        max-width: 82vw;
        height: 100vh;
        background: #1A1A1A;
        z-index: 10002;
        transition: left 0.28s ease;
        overflow-y: auto;
        -webkit-overflow-scrolling: touch;
        box-shadow: 2px 0 16px rgba(0,0,0,0.5);
    }
    .mobile-sidebar.open {
        left: 0;
    }
    .mobile-sidebar-header {
        padding: 16px 20px 12px;
        border-bottom: 1px solid #333;
        background: #222;
    }
    .mobile-sidebar-header .user-name {
        font-size: 14px;
        color: #E0E0E0;
        font-weight: 600;
    }
    .mobile-sidebar-header .user-role {
        font-size: 12px;
        color: #25A84C;
        margin-top: 4px;
    }
    .mobile-sidebar-menu {
        flex: 1;
        padding: 8px 0;
    }
    .mobile-sidebar .ms-item {
        display: block;
        padding: 13px 20px;
        color: #D0D0D0;
        font-size: 14px;
        cursor: pointer;
        border-bottom: 1px solid rgba(255,255,255,0.05);
        text-decoration: none;
        transition: background 0.15s;
    }
    .mobile-sidebar .ms-group-label {
        display: block !important;
        font-size: 11px !important;
        color: #666 !important;
        text-transform: uppercase;
        letter-spacing: 1px;
        padding: 14px 20px 6px !important;
        font-weight: 600;
        height: auto !important;
        overflow: visible !important;
        border-top: 1px solid #2A2A2A;
    }
    .mobile-sidebar .ms-group-label:first-child {
        border-top: none;
        padding-top: 8px;
    }
    .mobile-sidebar .ms-item:hover,
    .mobile-sidebar .ms-item.active {
        background: #0F6E56;
        color: #fff;
    }
    .mobile-sidebar-footer {
        padding: 12px 20px;
        border-top: 1px solid #333;
    }
    .mobile-sidebar-footer .btn {
        width: 100%;
    }

    /* ---------- 页面标题 ---------- */
    .page-title {
        font-size: 18px !important;
        margin-bottom: 12px !important;
    }

    /* ---------- 统计卡片 .stat-grid > .stat-card ---------- */
    .stat-grid {
        display: grid !important;
        grid-template-columns: repeat(2, 1fr) !important;
        gap: 10px !important;
        margin-bottom: 16px !important;
    }
    .stat-card {
        padding: 14px 10px !important;
        border-radius: 8px !important;
        text-align: center;
        background: #1A1A1A !important;
        border: 1px solid #333;
    }
    .stat-card .stat-value {
        font-size: 22px !important;
        font-weight: 700;
        color: #0F6E56 !important;
        line-height: 1.2;
    }
    .stat-card .stat-label {
        font-size: 12px !important;
        color: #999 !important;
        margin-top: 4px;
    }

    /* ---------- 卡片容器 .card ---------- */
    .card {
        padding: 12px !important;
        margin-bottom: 12px !important;
        border-radius: 6px !important;
    }

    /* ---------- 工具栏 .toolbar ---------- */
    .toolbar {
        display: flex !important;
        flex-wrap: wrap !important;
        gap: 8px !important;
        align-items: center !important;
        margin-bottom: 12px !important;
    }
    .toolbar .search-input {
        width: 100% !important;
        flex: 1 1 100% !important;
        min-width: 0;
        margin-bottom: 4px;
    }
    .toolbar select.form-input {
        flex: 1 1 auto;
        min-width: 100px;
    }
    .toolbar .btn {
        white-space: nowrap;
    }

    /* 搜索框全局 */
    .search-input {
        width: 100% !important;
        font-size: 14px !important;
        padding: 10px 12px !important;
    }

    /* ---------- 按钮 ---------- */
    .btn {
        min-height: 36px;
        font-size: 14px !important;
    }
    .btn-sm {
        padding: 6px 12px !important;
        font-size: 12px !important;
        min-height: 32px;
    }
    /* 表格操作列中的按钮：允许换行 */
    table.data-table tbody td .btn-sm,
    .mobile-card-table tbody td .btn-sm {
        margin: 2px 4px 2px 0;
    }

    /* ---------- 分页 .pagination ---------- */
    .pagination {
        display: flex !important;
        flex-wrap: wrap !important;
        gap: 6px !important;
        justify-content: center !important;
        margin-top: 12px !important;
    }
    .pagination button {
        padding: 6px 14px !important;
        font-size: 13px;
    }
    .pagination span {
        font-size: 13px;
        color: #999;
        align-self: center;
    }

    /* ---------- 空状态 ---------- */
    .empty-state {
        padding: 40px 16px !important;
        font-size: 14px;
    }

    /* ---------- 加载状态 ---------- */
    .loading {
        padding: 30px !important;
        font-size: 14px;
    }

    /* ============================================
       表格转卡片布局
       两种表格：
       1. table.data-table（有thead）→ 卡片式，data-label显示列名
       2. table 无class（详情页inline表，无thead）→ label-value垂直堆叠
       ============================================ */

    /* 所有表格容器：横向滚动兜底 */
    #content-area table,
    .card table {
        font-size: 13px !important;
    }

    /* --- 类型1：有thead的data-table，转卡片 --- */
    /* 通过JS给需要转卡片的表格加 .mobile-card-table */
    table.mobile-card-table {
        display: block !important;
        width: 100% !important;
        min-width: 0 !important;
        max-width: 100% !important;
        border-collapse: collapse !important;
        background: transparent !important;
        border-radius: 0 !important;
        overflow: visible !important;
        table-layout: auto !important;
    }
    table.mobile-card-table thead {
        display: none !important;
    }
    table.mobile-card-table tbody {
        display: block !important;
        width: 100%;
    }
    table.mobile-card-table tbody tr {
        display: block !important;
        width: 100% !important;
        min-width: 0 !important;
        box-sizing: border-box !important;
        background: #2D2D2D !important;
        border: 1px solid #404040 !important;
        border-radius: 8px !important;
        margin-bottom: 10px !important;
        padding: 4px 12px !important;
        box-shadow: 0 1px 4px rgba(0,0,0,0.25);
    }
    table.mobile-card-table tbody tr:hover td {
        background: transparent !important;
    }
    table.mobile-card-table tbody td {
        display: flex !important;
        justify-content: space-between !important;
        align-items: flex-start !important;
        padding: 9px 0 !important;
        border: none !important;
        border-bottom: 1px solid #3a3a3a !important;
        font-size: 13px !important;
        text-align: right !important;
        gap: 8px !important;
        line-height: 1.4;
        min-width: 0 !important;
        max-width: 100% !important;
        box-sizing: border-box !important;
        word-break: break-word !important;
        overflow-wrap: break-word !important;
    }
    table.mobile-card-table tbody td:last-child {
        border-bottom: none !important;
        padding-bottom: 6px !important;
    }
    /* data-label 伪元素显示字段名 */
    table.mobile-card-table tbody td::before {
        content: attr(data-label);
        font-weight: 600;
        color: #999;
        font-size: 12px;
        flex-shrink: 0;
        text-align: left;
        min-width: 0;
        max-width: 45%;
        overflow: hidden;
        text-overflow: ellipsis;
        white-space: nowrap;
    }
    /* 值部分（td的直接文本和子元素）允许收缩换行 */
    table.mobile-card-table tbody td > *:not(::before) {
        min-width: 0 !important;
        max-width: 55% !important;
        overflow-wrap: break-word !important;
        word-break: break-word !important;
    }

    /* 第一列（主字段）突出显示 */
    table.mobile-card-table tbody td:first-child {
        font-weight: 600;
        color: #25A84C !important;
        font-size: 14px !important;
        border-bottom: 2px solid #0F6E56 !important;
        padding-bottom: 10px !important;
        margin-bottom: 2px;
    }
    table.mobile-card-table tbody td:first-child::before {
        color: #0F6E56;
        font-size: 13px;
    }
    /* 最后一列（操作列）：按钮左对齐排列 */
    table.mobile-card-table tbody td:last-child {
        justify-content: flex-start !important;
        flex-wrap: wrap;
    }
    table.mobile-card-table tbody td:last-child::before {
        display: none;
    }
    /* 覆盖inline style的白底 */
    table.mobile-card-table[style*="background:#fff"],
    table.mobile-card-table[style*="background: #fff"],
    table.mobile-card-table[style*="background:white"] {
        background: transparent !important;
    }

    /* --- 类型2：无thead的inline详情表，label-value堆叠 --- */
    /* 通过JS给此类表格加 .mobile-detail-table */
    table.mobile-detail-table {
        display: block !important;
        width: 100% !important;
        background: transparent !important;
    }
    table.mobile-detail-table tbody {
        display: block !important;
    }
    table.mobile-detail-table tr {
        display: block !important;
        padding: 4px 0 !important;
        border-bottom: 1px solid #333 !important;
    }
    table.mobile-detail-table tr:last-child {
        border-bottom: none !important;
    }
    /* 每个td独占一行，label在上value在下 */
    table.mobile-detail-table td {
        display: block !important;
        padding: 4px 0 !important;
        border: none !important;
        text-align: left !important;
        font-size: 13px !important;
    }
    /* label格（奇数位，color:#999的） */
    table.mobile-detail-table td:nth-child(odd) {
        color: #888 !important;
        font-size: 12px !important;
        padding-top: 8px !important;
        width: auto !important;
    }
    /* value格（偶数位） */
    table.mobile-detail-table td:nth-child(even) {
        color: #E0E0E0 !important;
        font-weight: 500;
        padding-bottom: 8px !important;
    }
    /* 4列行（label,value,label,value）：用grid */
    table.mobile-detail-table tr.has-4cols td:nth-child(1) { grid-area: l1; }
    table.mobile-detail-table tr.has-4cols td:nth-child(2) { grid-area: v1; }
    table.mobile-detail-table tr.has-4cols td:nth-child(3) { grid-area: l2; }
    table.mobile-detail-table tr.has-4cols td:nth-child(4) { grid-area: v2; }
    table.mobile-detail-table tr.has-4cols {
        display: grid !important;
        grid-template-columns: 1fr 1fr;
        grid-template-areas:
            "l1 l2"
            "v1 v2";
        gap: 2px 12px;
        align-items: baseline;
    }

    /* ============================================
       表单适配
       ============================================ */
    .form-section {
        padding: 14px !important;
        margin-bottom: 12px !important;
    }
    .form-grid-2,
    .form-grid-3 {
        grid-template-columns: 1fr !important;
        gap: 12px !important;
    }
    .form-row {
        flex-direction: column !important;
        gap: 12px !important;
    }
    .form-row .form-group {
        margin-bottom: 0;
    }
    .form-actions {
        flex-wrap: wrap !important;
        justify-content: stretch !important;
    }
    .form-actions .btn {
        flex: 1;
        min-width: 100px;
    }
    .form-field input,
    .form-field select,
    .form-field textarea {
        font-size: 14px !important;
        padding: 10px !important;
    }
    /* 防止iOS缩放：input font-size >= 16px */
    .form-input {
        font-size: 16px !important;
    }

    /* ---------- 表单内可添加行的表格（商品明细等）保持横向滚动 ---------- */
    .form-section .data-table:not(.mobile-card-table) {
        display: table;
        width: 100%;
        font-size: 12px;
    }
    .form-section .data-table:not(.mobile-card-table) td,
    .form-section .data-table:not(.mobile-card-table) th {
        padding: 6px 8px !important;
        font-size: 12px !important;
        white-space: nowrap;
    }

    /* ---------- Toast ---------- */
    .toast {
        right: 10px !important;
        left: 10px !important;
        top: 56px !important;
        font-size: 13px !important;
        text-align: center;
    }

    /* ---------- 打印预览遮罩 ---------- */
    .hiprint-preview-bar {
        padding: 10px 12px !important;
        gap: 8px !important;
    }
    .hiprint-preview-body {
        max-height: 65vh !important;
    }

    /* ---------- 状态标签 ---------- */
    .status-tag {
        font-size: 11px !important;
        padding: 2px 6px !important;
    }

    /* ---------- 示例数据提示条 ---------- */
    .sample-toggle-bar {
        flex-wrap: wrap !important;
        gap: 6px !important;
        padding: 8px 12px !important;
    }
    .sample-toggle-bar .hint {
        width: 100%;
    }

    /* ---------- 仪表盘进度条 ---------- */
    #content-area [style*="width:200px"],
    #content-area [style*="width: 200px"] {
        width: 120px !important;
    }

    /* ---------- 安全底部留白（悬浮按钮高度~60px + safe area） ---------- */
    body {
        padding-bottom: calc(72px + env(safe-area-inset-bottom, 0));
    }
    #content-area {
        padding-bottom: 16px !important;
    }

    /* ===== v2.2 修复：紧凑表格 cl-table 保持横滑不转卡片 ===== */
    .cl-table-wrap {
        overflow-x: auto !important;
        overflow-y: visible !important;
        -webkit-overflow-scrolling: touch;
        padding-bottom: 12px !important;
        margin-bottom: -4px;
        border: 1px solid #333;
        border-radius: 6px;
        background: #1A1A1A;
    }
    table.cl-table {
        display: table !important;
        width: 100% !important;
        min-width: 560px !important;
        border-collapse: collapse !important;
        background: transparent !important;
        margin-bottom: 0 !important;
    }
    table.cl-table thead { display: table-header-group !important; }
    table.cl-table tbody { display: table-row-group !important; width: auto !important; }
    table.cl-table tbody tr {
        display: table-row !important;
        width: auto !important;
        background: transparent !important;
        border: none !important;
        border-radius: 0 !important;
        margin: 0 !important;
        padding: 0 !important;
        box-shadow: none !important;
    }
    table.cl-table tbody tr:hover td { background: #243a33 !important; }
    table.cl-table tbody td {
        display: table-cell !important;
        justify-content: normal !important;
        align-items: normal !important;
        padding: 9px 12px !important;
        border: none !important;
        border-bottom: 1px solid #2A2A2A !important;
        font-size: 13px !important;
        text-align: left !important;
        white-space: nowrap !important;
        line-height: 1.4;
    }
    table.cl-table tbody td:last-child { border-bottom: 1px solid #2A2A2A !important; }
    table.cl-table .cl-sticky {
        position: sticky !important;
        left: 0 !important;
        z-index: 2 !important;
        box-shadow: 2px 0 4px rgba(0,0,0,0.25) !important;
    }
    table.cl-table thead th.cl-sticky { z-index: 4 !important; }

    /* 滚动条样式：细条品牌色 */
    .cl-table-wrap::-webkit-scrollbar { height: 6px; width: 6px; }
    .cl-table-wrap::-webkit-scrollbar-track { background: #1A1A1A; border-radius: 3px; }
    .cl-table-wrap::-webkit-scrollbar-thumb { background: #0F6E56; border-radius: 3px; }

    /* 分页栏在手机端底部留白避开FAB(灯灯bottom:76px) */
    .cl-pagination {
        padding-bottom: 96px !important;
    }

    /* ===== v2.2 修复：汉堡按钮不遮挡页面标题 ===== */
    /* 内容顶部第一个标题/工具栏留出汉堡空间 */
    #content-area .page-title,
    #content-area .cl-toolbar > h3.page-title,
    #content-area > h2.page-title,
    #content-area > h3.page-title {
        padding-left: 44px;
    }
    #content-area .cl-toolbar {
        padding-left: 0;
    }
    /* 工具栏内部的第一个标题也要避让 */
    .cl-toolbar h3.page-title,
    .cl-toolbar .page-title {
        padding-left: 44px !important;
        margin-left: -44px;
    }
    /* 详情页返回按钮区域不避让 */
    .form-page-header, [style*="align-items:center"][style*="gap:12px"] .page-title {
        padding-left: 0 !important;
    }

}
