/**
 * V94 移动端响应式样式（P2-4 移动端适配）
 * 底部 TabBar 导航、触摸优化、移动端全面适配
 * 涵盖：布局、导航、表单、表格、弹窗、Toast、搜索、图表等
 */

/* ========== 平板横屏适配 (768-1024px) ========== */
@media (max-width: 1024px) {
  .sidebar { width: 200px; }
  .sidebar .nav-item { padding: 8px 12px; font-size: 12px; }
  .sidebar .nav-item .nav-icon { font-size: 16px; }
  .top-bar { left: 200px; }
  .stat-card { min-width: 140px; }

  /* 面板适配 */
  .panel-grid { grid-template-columns: 1fr; }
  .dashboard-widgets { grid-template-columns: repeat(2, 1fr); }
}

/* ========== 平板竖屏适配 (600-768px) ========== */
@media (max-width: 768px) {
  /* 侧边栏折叠为图标模式 */
  .sidebar { width: 56px; overflow: hidden; }
  .sidebar.collapsed { width: 56px; }
  .sidebar:hover,
  .sidebar.mobile-open { width: 220px; z-index: 100; }
  .sidebar .nav-item { padding: 10px 14px; }
  .sidebar .nav-item .nav-label { display: none; }
  .sidebar:hover .nav-item .nav-label,
  .sidebar.mobile-open .nav-item .nav-label { display: inline; }
  .sidebar .nav-item .nav-icon { font-size: 18px; margin-right: 0; }
  .sidebar:hover .nav-item .nav-icon,
  .sidebar.mobile-open .nav-item .nav-icon { margin-right: 8px; }

  .top-bar { left: 56px; }

  /* 卡片网格 */
  .stats-grid { grid-template-columns: repeat(2, 1fr); gap: 8px; }
  .card-grid { grid-template-columns: 1fr; }

  /* 模态框 */
  .modal-container { width: 90vw; max-height: 85vh; }

  /* 搜索 */
  #globalSearch, .global-search input { width: 160px; }

  /* 图表 */
  .chart-container { height: 250px; }
}

/* ========== 手机端适配 (< 600px) ========== */
@media (max-width: 600px) {
  /* ── 侧边栏：隐藏并以底栏导航替代 ── */
  .sidebar { display: none; }

  /* 移动端侧边栏抽屉模式（当通过TabBar打开模块列表时） */
  .sidebar.mobile-drawer {
    display: block;
    position: fixed;
    top: 0; left: 0; bottom: 0;
    width: 260px;
    z-index: 1100;
    box-shadow: 4px 0 24px rgba(0,0,0,.25);
    transform: translateX(-100%);
    transition: transform .25s cubic-bezier(.4,0,.2,1);
  }
  .sidebar.mobile-drawer.open {
    transform: translateX(0);
  }
  /* 抽屉遮罩 */
  .mobile-drawer-overlay {
    display: none;
    position: fixed; inset: 0;
    background: rgba(0,0,0,.4);
    z-index: 1099;
    opacity: 0;
    transition: opacity .25s;
  }
  .mobile-drawer-overlay.show {
    display: block; opacity: 1;
  }
  .sidebar.mobile-drawer .sidebar-header { display: flex; }
  .sidebar.mobile-drawer .sidebar-footer { display: flex; }

  /* ── 底部 TabBar 导航 ── */
  #mobileTabBar {
    display: flex !important;
    position: fixed;
    bottom: 0; left: 0; right: 0;
    height: 64px;
    background: #fff;
    border-top: 1px solid #e5e7eb;
    z-index: 1000;
    align-items: center;
    justify-content: space-around;
    padding: 0 4px;
    box-shadow: 0 -2px 16px rgba(0,0,0,.08);
    padding-bottom: env(safe-area-inset-bottom, 0);
  }

  #mobileTabBar .m-tab {
    display: flex;
    flex-direction: column;
    align-items: center;
    justify-content: center;
    padding: 4px 6px;
    min-width: 52px;
    cursor: pointer;
    color: #9ca3af;
    font-size: 10px;
    transition: color .2s, transform .15s;
    -webkit-tap-highlight-color: transparent;
    user-select: none;
    position: relative;
  }
  #mobileTabBar .m-tab:active {
    transform: scale(.94);
  }
  #mobileTabBar .m-tab.active { color: var(--primary, #1e40af); }
  #mobileTabBar .m-tab .m-icon { font-size: 22px; margin-bottom: 2px; transition: transform .2s; }
  #mobileTabBar .m-tab.active .m-icon { transform: translateY(-2px); }
  /* 更多按钮的徽标 */
  #mobileTabBar .m-tab .m-badge {
    position: absolute;
    top: 2px;
    right: 2px;
    width: 7px;
    height: 7px;
    border-radius: 50%;
    background: #ef4444;
  }

  /* ── 移动端"更多"底部弹出面板 ── */
  .mobile-more-sheet {
    position: fixed;
    bottom: 0; left: 0; right: 0;
    z-index: 1200;
    background: #fff;
    border-radius: 16px 16px 0 0;
    box-shadow: 0 -4px 24px rgba(0,0,0,.18);
    transform: translateY(100%);
    transition: transform .3s cubic-bezier(.4,0,.2,1);
    padding: 8px 0 calc(env(safe-area-inset-bottom, 0) + 72px);
    max-height: 60vh;
    overflow-y: auto;
    -webkit-overflow-scrolling: touch;
  }
  .mobile-more-sheet.open {
    transform: translateY(0);
  }
  .mobile-more-sheet .sheet-handle {
    width: 36px; height: 4px;
    border-radius: 2px;
    background: #d1d5db;
    margin: 8px auto 12px;
  }
  .mobile-more-sheet .sheet-title {
    text-align: center;
    font-size: 13px;
    font-weight: 600;
    color: #6b7280;
    margin-bottom: 8px;
    letter-spacing: .5px;
  }
  .mobile-more-sheet .sheet-grid {
    display: grid;
    grid-template-columns: repeat(4, 1fr);
    gap: 8px;
    padding: 4px 12px;
  }
  .mobile-more-sheet .sheet-item {
    display: flex;
    flex-direction: column;
    align-items: center;
    padding: 10px 4px;
    border-radius: 12px;
    cursor: pointer;
    transition: background .15s;
    -webkit-tap-highlight-color: transparent;
    user-select: none;
    font-size: 11px;
    color: #4b5563;
    text-align: center;
    gap: 4px;
  }
  .mobile-more-sheet .sheet-item:active {
    background: #f3f4f6;
    transform: scale(.95);
  }
  .mobile-more-sheet .sheet-item .si-icon {
    font-size: 26px;
    line-height: 1;
  }
  .mobile-more-sheet .sheet-item .si-label {
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
    max-width: 100%;
  }

  /* 更多面板遮罩 */
  .mobile-more-overlay {
    position: fixed; inset: 0;
    z-index: 1199;
    background: rgba(0,0,0,.35);
    opacity: 0;
    pointer-events: none;
    transition: opacity .25s;
  }
  .mobile-more-overlay.show {
    opacity: 1;
    pointer-events: auto;
  }

  /* ── 内容区 ── */
  .content {
    padding: 8px;
    padding-bottom: 80px; /* 为底栏留空间 */
  }
  .group-sub-tab-bar-container {
    padding: 8px 8px 0;
  }

  /* ── 顶部栏 ── */
  .top-bar {
    left: 0 !important;
    padding: 8px 10px;
    padding-top: calc(8px + env(safe-area-inset-top, 0px));
    gap: 6px;
    min-height: 48px;
    flex-wrap: wrap;
  }
  .top-bar .logo-text { font-size: 14px; }
  .top-bar .user-info {
    display: flex;
    align-items: center;
    gap: 4px;
    font-size: 12px;
  }
  .top-bar .user-info span { display: none; }
  .top-bar .user-info .user-avatar {
    width: 28px; height: 28px;
    font-size: 12px;
  }
  .top-bar .header-right {
    gap: 6px;
    flex-shrink: 0;
  }
  .top-bar .header-right .icon-btn {
    width: 32px; height: 32px;
    font-size: 14px;
  }

  /* ── 搜索框 ── */
  #globalSearch, .global-search input {
    width: 120px;
    font-size: 13px;
    height: 32px;
  }
  .global-search { flex-shrink: 1; min-width: 0; }
  .search-results-panel {
    width: 100vw;
    max-width: 100vw;
    right: 0;
    left: 0;
    top: 48px;
    border-radius: 0;
    max-height: 50vh;
  }

  /* ── 页面标题 ── */
  .page-header {
    flex-direction: column;
    align-items: flex-start;
    gap: 6px;
    padding: 10px 12px;
  }
  .page-header h2 { font-size: 16px; }
  .page-header .header-actions {
    width: 100%;
    display: flex;
    gap: 6px;
    flex-wrap: wrap;
  }

  /* ── 模块工具栏 ── */
  .module-toolbar {
    flex-direction: column;
    gap: 8px;
    padding: 10px;
  }
  .module-toolbar .toolbar-left,
  .module-toolbar .toolbar-right {
    width: 100%;
    flex-wrap: wrap;
    gap: 4px;
  }
  .module-toolbar .toolbar-right {
    justify-content: flex-start;
  }

  /* ── Tab 导航水平滚动 ── */
  .tab-nav {
    overflow-x: auto;
    white-space: nowrap;
    -webkit-overflow-scrolling: touch;
    flex-wrap: nowrap;
    gap: 2px;
    padding: 4px 0;
    scrollbar-width: none;
  }
  .tab-nav::-webkit-scrollbar { display: none; }
  .tab-nav .tab-btn {
    flex-shrink: 0;
    padding: 8px 14px;
    font-size: 12px;
  }

  /* ── 表格适配 ── */
  .table-wrapper {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    border-radius: 8px;
  }
  table {
    display: block;
    overflow-x: auto;
    white-space: nowrap;
    -webkit-overflow-scrolling: touch;
    font-size: 11px;
  }
  table th, table td { padding: 6px 8px; font-size: 11px; }
  /* 表格移动端紧凑操作按钮 */
  table .action-cell { white-space: nowrap; }
  table .action-cell button,
  table .action-cell .btn-sm {
    padding: 4px 6px;
    font-size: 10px;
    min-height: 26px;
  }

  /* ── 卡片与统计 ── */
  .stats-grid {
    grid-template-columns: repeat(2, 1fr);
    gap: 6px;
  }
  .stat-card {
    padding: 10px;
    min-width: auto;
  }
  .stat-card .stat-value { font-size: 20px; }
  .stat-card .stat-label { font-size: 11px; }
  .stat-card .stat-icon { font-size: 24px; }

  /* 信息卡片网格 */
  .card-grid { grid-template-columns: 1fr; gap: 8px; }
  .info-card { padding: 12px; }

  /* ── 看板/Dashboard ── */
  .dashboard-welcome {
    flex-direction: column;
    text-align: center;
    padding: 16px;
    gap: 8px;
  }
  .dashboard-welcome h3 { font-size: 16px; }
  .dashboard-welcome p { font-size: 12px; }
  .dashboard-widgets {
    grid-template-columns: 1fr;
    gap: 8px;
  }

  /* ── 模态框 ── */
  .modal-overlay {
    align-items: flex-end;
  }
  .modal-container {
    width: 100vw;
    height: auto;
    max-height: 90vh;
    border-radius: 16px 16px 0 0;
    margin: 0;
  }
  .modal-container.modal-wide {
    width: 100vw;
    max-width: 100vw;
    height: 100vh;
    max-height: 100vh;
    border-radius: 0;
    margin: 0;
  }
  .modal-header {
    padding: 12px 16px;
    border-radius: 16px 16px 0 0;
  }
  .modal-header h3 { font-size: 15px; }
  .modal-body { padding: 12px 16px; }
  .modal-footer {
    padding: 10px 16px;
    flex-wrap: wrap;
    gap: 6px;
    padding-bottom: calc(10px + env(safe-area-inset-bottom, 0));
  }
  .modal-footer button {
    flex: 1;
    min-width: 80px;
    min-height: 40px;
    font-size: 13px;
  }

  /* ERP 模态框增强 */
  .erp-modal-overlay {
    align-items: flex-end;
  }
  .erp-modal {
    width: 100vw;
    border-radius: 16px 16px 0 0;
    max-height: 90vh;
    margin: 0;
  }

  /* ── 登录模态框 ── */
  #loginModal .modal-container {
    width: 92vw;
    height: auto;
    max-height: 90vh;
    border-radius: 16px;
    margin: auto;
  }
  #loginModal .modal-overlay {
    align-items: center;
  }

  /* ── 表单适配 ── */
  .form-row {
    flex-direction: column;
    gap: 8px;
  }
  .form-row .form-group { width: 100% !important; }
  .form-group { margin-bottom: 10px; }
  .form-group label {
    font-size: 13px;
    margin-bottom: 4px;
  }
  input, select, textarea {
    font-size: 16px !important;
    padding: 10px 12px;
    min-height: 42px;
  }
  /* 避免 iOS 自动缩放 */
  input[type="text"], input[type="search"], input[type="email"],
  input[type="number"], input[type="tel"], input[type="password"],
  select, textarea {
    font-size: 16px !important;
  }

  /* ── 按钮优化 ── */
  .btn, button {
    min-height: 38px;
    padding: 8px 16px;
    font-size: 13px;
    -webkit-tap-highlight-color: transparent;
  }
  .btn:active, button:active {
    transform: scale(.97);
    transition: transform .1s;
  }
  .btn-sm {
    min-height: 30px;
    padding: 5px 10px;
    font-size: 11px;
  }
  .btn-lg {
    min-height: 44px;
    padding: 10px 20px;
    font-size: 15px;
  }
  /* 底部固定操作栏 */
  .mobile-fab {
    position: fixed;
    bottom: calc(env(safe-area-inset-bottom, 0) + 72px);
    right: 16px;
    z-index: 900;
    width: 48px;
    height: 48px;
    border-radius: 50%;
    background: var(--primary, #1e40af);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 22px;
    cursor: pointer;
    box-shadow: 0 4px 16px rgba(0,0,0,.2);
    border: none;
    -webkit-tap-highlight-color: transparent;
    transition: transform .2s, box-shadow .2s;
  }
  .mobile-fab:active {
    transform: scale(.92);
    box-shadow: 0 2px 8px rgba(0,0,0,.15);
  }

  /* ── 消息中心面板 ── */
  #msgCenterPanel,
  .search-results-panel {
    width: 100vw;
    max-width: 100vw;
    right: 0;
    left: 0;
    border-radius: 0;
    top: 48px;
    max-height: calc(100vh - 120px);
    bottom: auto;
  }

  /* ── Toast 通知 ── */
  .toast-container {
    top: auto;
    bottom: calc(env(safe-area-inset-bottom, 0) + 76px);
    right: 8px;
    left: 8px;
    align-items: center;
  }
  .toast {
    min-width: auto;
    max-width: 100%;
    width: 100%;
    font-size: 13px;
    padding: 12px 16px;
  }

  /* ── 图表适配 ── */
  .chart-container {
    height: 220px;
    min-height: 200px;
  }
  .chart-container canvas {
    max-width: 100% !important;
  }

  /* ── 条件筛选器 ── */
  .filter-bar {
    flex-direction: column;
    gap: 6px;
  }
  .filter-bar .filter-item {
    width: 100%;
  }
  .filter-bar select,
  .filter-bar input {
    width: 100%;
  }

  /* ── 分页 ── */
  .pagination {
    flex-wrap: wrap;
    justify-content: center;
    gap: 4px;
    font-size: 12px;
  }
  .pagination button {
    min-width: 32px;
    min-height: 32px;
    padding: 4px 8px;
    font-size: 11px;
  }
  .pagination .page-info {
    text-align: center;
    width: 100%;
    font-size: 11px;
    order: -1;
  }

  /* ── 批量操作栏 ── */
  .batch-actions {
    flex-wrap: wrap;
    gap: 4px;
    padding: 8px;
    font-size: 12px;
  }

  /* ── 设置/配置面板 ── */
  .settings-panel {
    width: 100vw;
    max-width: 100vw;
    border-radius: 0;
  }

  /* ── 浮动提示/弹出菜单 ── */
  .popup-menu, .dropdown-menu {
    max-width: 90vw;
    min-width: 160px;
    font-size: 13px;
  }
  .popup-menu .menu-item,
  .dropdown-menu .menu-item {
    padding: 10px 16px;
    min-height: 40px;
  }

  /* ── 折叠面板 ── */
  .collapse-header {
    padding: 12px;
    font-size: 13px;
  }
  .collapse-body {
    padding: 10px;
  }

  /* ── 可折叠分组（PLM等） ── */
  .group-header {
    padding: 10px 12px;
    font-size: 13px;
  }

  /* ── 移动端侧边栏抽屉模式 ── */
  .sidebar.mobile-drawer .role-banner { display: flex; }
  .sidebar.mobile-drawer .sidebar-search { display: block; }
  .sidebar.mobile-drawer .sidebar-menu { display: block; }
  .sidebar.mobile-drawer .sidebar-header {
    display: flex;
    align-items: center;
    padding: 16px;
    border-bottom: 1px solid #e5e7eb;
    background: var(--primary, #1e40af);
    color: #fff;
  }
  .sidebar.mobile-drawer .sidebar-footer { display: flex; }

  /* ── 移动端表格卡片视图模式（通过 JS 切换 class）── */
  .table-card-view table,
  .table-card-view thead,
  .table-card-view tbody,
  .table-card-view th,
  .table-card-view td,
  .table-card-view tr {
    display: block;
  }
  .table-card-view thead { display: none; }
  .table-card-view tr {
    margin-bottom: 10px;
    border: 1px solid #e5e7eb;
    border-radius: 10px;
    padding: 10px;
    background: #fff;
    box-shadow: 0 1px 3px rgba(0,0,0,.05);
  }
  .table-card-view td {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 6px 0;
    border: none;
    border-bottom: 1px solid #f3f4f6;
    font-size: 13px;
    white-space: normal;
  }
  .table-card-view td:last-child { border-bottom: none; }
  .table-card-view td::before {
    content: attr(data-label);
    font-weight: 600;
    color: #6b7280;
    font-size: 11px;
    flex-shrink: 0;
    margin-right: 8px;
    text-transform: uppercase;
    letter-spacing: .5px;
  }

  /* ── 下拉刷新指示器 ── */
  .pull-indicator {
    position: fixed;
    top: 0; left: 0; right: 0;
    height: 0;
    background: var(--primary, #1e40af);
    color: #fff;
    display: flex;
    align-items: center;
    justify-content: center;
    font-size: 13px;
    overflow: hidden;
    transition: height .2s;
    z-index: 2000;
  }
  .pull-indicator.active {
    height: 40px;
  }

  /* ── 移动端滚动条隐藏 ── */
  .content::-webkit-scrollbar,
  .modal-body::-webkit-scrollbar,
  .mobile-more-sheet::-webkit-scrollbar {
    width: 0;
    height: 0;
  }
}

/* ========== 小屏手机适配 (< 380px) ========== */
@media (max-width: 380px) {
  .stats-grid { grid-template-columns: 1fr; }
  #mobileTabBar .m-tab { padding: 2px 2px; font-size: 9px; }
  #mobileTabBar .m-tab .m-icon { font-size: 18px; }
  #mobileTabBar { height: 56px; }
  .content { padding-bottom: 70px; }
  .top-bar { padding: 6px 8px; }
  .top-bar .logo-text { font-size: 12px; }
  #globalSearch, .global-search input { width: 100px; }
}

/* ========== V100: 移动端智能快捷操作栏 ========== */
@media (max-width: 600px) {
  #mobileSmartBar {
    display: flex !important;
  }
  /* 调整内容底部间距避开智能栏 */
  .content {
    padding-bottom: 140px !important;
  }
  /* 调整FAB位置避开智能栏 */
  .mobile-fab {
    bottom: 130px !important;
  }
}

@media (min-width: 601px) {
  #mobileSmartBar {
    display: none !important;
  }
}

/* ========== 触屏优化 ========== */
@media (hover: none) and (pointer: coarse) {
  /* 增大可点击区域 */
  .nav-item, .tab-btn, .btn, button, .clickable,
  select, input[type="checkbox"], input[type="radio"],
  .sheet-item, .m-tab, .action-btn, .icon-btn {
    min-height: 44px;
    -webkit-tap-highlight-color: transparent;
  }

  /* 表格行触摸反馈 */
  tbody tr:active { background: #eff6ff !important; }

  /* 长按菜单 */
  .long-press-target {
    -webkit-touch-callout: none;
    user-select: none;
  }

  /* iOS 平滑滚动 */
  .content, .modal-body, .mobile-more-sheet,
  .table-wrapper, #mobileTabBar {
    -webkit-overflow-scrolling: touch;
  }

  /* 禁止 iOS 橡皮筋效果影响模态框 */
  .modal-overlay.show {
    touch-action: none;
  }
  .modal-overlay.show .modal-container {
    touch-action: auto;
  }

  /* 避免双击缩放 */
  button, a, [role="button"], .btn {
    touch-action: manipulation;
  }
}

/* ========== 横屏手机适配 ========== */
@media (max-width: 900px) and (orientation: landscape) {
  .sidebar { display: none; }
  .content { padding: 8px; }
  .top-bar { left: 0 !important; }
  #mobileTabBar { display: flex !important; }
  .modal-container { max-height: 95vh; }
}

/* ========== 打印适配 ========== */
@media print {
  .sidebar, .top-bar, #mobileTabBar, .tab-nav,
  .modal-overlay, .toast-container, #msgCenterPanel,
  .mobile-more-sheet, .mobile-more-overlay, .mobile-drawer-overlay,
  .mobile-fab {
    display: none !important;
  }
  .content { padding: 0 !important; }
  body { background: #fff !important; }
  table { display: table; overflow-x: visible; }
}
