/* ============================================================================
 * 移动端适配层 v1.26.0 — 由 assets/perm.js 的 sfMobileInit() 激活
 * 作用域：@media screen and (max-width: 820px)
 * 设计原则：只覆盖布局，不改业务逻辑；桌面端（>820px）零影响；打印不受影响
 * 页面：index.html / sales_order.html / admin.html / login.html
 * ========================================================================== */

/* ---------- 基线：未激活移动层时不显示任何移动端专属元素 ---------- */
.sf-mtop,
.sf-moverlay,
.sf-rt-allbar { display: none; }

/* ---------- 0. 全局：只在手机生效，禁止横向溢出 ---------- */
@media screen and (max-width: 820px) {

  html { -webkit-text-size-adjust: 100%; text-size-adjust: 100%; }

  body.sf-mobile {
    overflow-x: hidden;
    overscroll-behavior-y: none;
  }
  body.sf-mobile.sf-nav-open { overflow: hidden; }

  /* iOS 聚焦缩放的根治：输入类元素字号 ≥16px */
  body.sf-mobile input,
  body.sf-mobile select,
  body.sf-mobile textarea {
    font-size: 16px !important;
  }

  /* ==========================================================================
   * 1. 顶部固定栏（由 JS 注入）
   * ======================================================================== */
  .sf-mtop {
    display: none;
    position: fixed;
    top: 0; left: 0; right: 0;
    height: 52px;
    z-index: 90;
    align-items: center;
    gap: 10px;
    padding: 0 10px;
    padding-top: env(safe-area-inset-top);
    height: calc(52px + env(safe-area-inset-top));
    background: #111827;
    color: #fff;
    box-shadow: 0 1px 4px rgba(0,0,0,.18);
  }
  body.sf-mobile .sf-mtop { display: flex; }

  .sf-mtop-btn {
    flex: 0 0 auto;
    width: 40px; height: 40px;
    border: none;
    border-radius: 8px;
    background: rgba(255,255,255,.13);
    color: #fff;
    font-size: 19px;
    line-height: 1;
    cursor: pointer;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    -webkit-tap-highlight-color: transparent;
  }
  .sf-mtop-btn:active { background: rgba(255,255,255,.26); }

  .sf-mtop-title {
    flex: 1 1 auto;
    min-width: 0;
    font-size: 15px;
    font-weight: 600;
    white-space: nowrap;
    overflow: hidden;
    text-overflow: ellipsis;
  }

  .sf-mtop-user {
    flex: 0 0 auto;
    width: 36px; height: 36px;
    border-radius: 50%;
    background: #059669;
    color: #fff;
    font-size: 13px;
    font-weight: 700;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    cursor: pointer;
    -webkit-tap-highlight-color: transparent;
  }

  /* ==========================================================================
   * 2. 侧边栏 → 抽屉（Off-canvas）
   * ======================================================================== */
  body.sf-mobile .sf-moverlay {
    display: block;
    position: fixed;
    inset: 0;
    background: rgba(0,0,0,.45);
    z-index: 95;
    opacity: 0;
    pointer-events: none;
    transition: opacity .22s ease;
  }
  body.sf-mobile.sf-nav-open .sf-moverlay { opacity: 1; pointer-events: auto; }

  body.sf-mobile .sidebar {
    width: 272px;
    max-width: 84vw;
    transform: translateX(-100%);
    transition: transform .25s ease;
    box-shadow: 4px 0 18px rgba(0,0,0,.28);
    z-index: 100;
    height: 100vh;
    height: 100dvh;
    overflow-y: auto;
  }
  body.sf-mobile.sf-nav-open .sidebar { transform: translateX(0); }

  /* 抽屉里恢复完整文字（覆盖页面自带的 60px 图标栏规则） */
  body.sf-mobile .sidebar .sidebar-header h1,
  body.sf-mobile .sidebar .sidebar-header .sub,
  body.sf-mobile .sidebar .nav-item > span:not(.icon),
  body.sf-mobile .sidebar .sidebar-footer { display: block !important; }
  body.sf-mobile .sidebar .sidebar-header h1 { display: block !important; }
  body.sf-mobile .sidebar .nav-item {
    justify-content: flex-start !important;
    padding: 13px 22px !important;
    font-size: 15px;
  }
  body.sf-mobile .sidebar .nav-item .badge { display: inline-block !important; margin-left: auto; }
  body.sf-mobile .sidebar-footer { display: block !important; padding: 14px 22px !important; }

  /* 主区域让位给顶栏，取消左侧 240px 缩进 */
  body.sf-mobile .main {
    margin-left: 0 !important;
    padding: 14px 12px calc(28px + env(safe-area-inset-bottom)) 12px !important;
    padding-top: calc(64px + env(safe-area-inset-top)) !important;
    width: 100%;
    max-width: 100%;
  }
  body.sf-mobile .app { display: block; }

  /* ==========================================================================
   * 3. 通用：标题栏 / 按钮 / 卡片
   * ======================================================================== */
  body.sf-mobile .page-header {
    flex-wrap: wrap;
    gap: 10px;
    margin-bottom: 16px;
    align-items: flex-start;
  }
  body.sf-mobile .page-title { font-size: 19px; line-height: 1.35; }
  body.sf-mobile .page-subtitle { font-size: 12px; }
  body.sf-mobile .page-header h2 { font-size: 18px; }
  body.sf-mobile .page-header .desc { font-size: 12px; }

  /* 标题栏右侧按钮组铺满一行，手指好按 */
  body.sf-mobile .page-header > .flex,
  body.sf-mobile .page-header > .toolbar {
    width: 100%;
    flex-wrap: wrap;
    gap: 8px;
  }
  body.sf-mobile .page-header > .flex > .btn,
  body.sf-mobile .page-header > .toolbar > .btn { flex: 1 1 auto; justify-content: center; }

  body.sf-mobile .btn {
    min-height: 40px;
    padding: 9px 14px;
    justify-content: center;
    -webkit-tap-highlight-color: transparent;
  }
  body.sf-mobile .btn-sm { min-height: 36px; padding: 7px 12px; }
  body.sf-mobile .action-btns { flex-wrap: wrap; }

  body.sf-mobile .card,
  body.sf-mobile .table-wrap {
    border-radius: 10px;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
  }
  body.sf-mobile .card-body { padding: 14px 14px; }
  body.sf-mobile .card.mb-24 { margin-bottom: 14px; }

  /* ==========================================================================
   * 4. 表格：紧凑 + 横向滚动 + 首列吸附
   * ======================================================================== */
  body.sf-mobile table { font-size: 13px; }
  body.sf-mobile th { padding: 8px 10px; font-size: 11px; }
  body.sf-mobile td { padding: 9px 10px; font-size: 13px; }

  /* 普通数据表：最小宽度兜底，保证列不被压扁（外层容器负责滚动） */
  body.sf-mobile table:not(.sf-rt):not(.qc-table):not(.pt-table):not(.pt-sign) {
    min-width: 560px;
  }

  /* 首列吸附，横向滚动时不丢订单号 */
  body.sf-mobile table:not(.sf-rt):not(.qc-table):not(.pt-table):not(.pt-sign) thead th:first-child,
  body.sf-mobile table:not(.sf-rt):not(.qc-table):not(.pt-table):not(.pt-sign) tbody td:first-child {
    position: sticky;
    left: 0;
    z-index: 1;
    background: #fff;
    box-shadow: 1px 0 0 var(--gray-200, #e5e7eb);
  }
  body.sf-mobile table:not(.sf-rt):not(.qc-table):not(.pt-table):not(.pt-sign) thead th:first-child {
    background: var(--gray-50, #f9fafb);
    z-index: 2;
  }
  body.sf-mobile table:not(.sf-rt):not(.qc-table):not(.pt-table):not(.pt-sign) tbody tr:hover td:first-child {
    background: var(--gray-50, #f9fafb);
  }

  /* ---- 卡片模式（sf-rt，由 JS 自动标注） ---- */
  body.sf-mobile table.sf-rt {
    min-width: 0;
    width: 100%;
    display: block;
  }
  body.sf-mobile table.sf-rt thead { display: none; }
  body.sf-mobile table.sf-rt tbody { display: block; }
  body.sf-mobile table.sf-rt tr {
    display: block;
    background: #fff;
    border: 1px solid var(--gray-200, #e5e7eb);
    border-radius: 10px;
    padding: 10px 12px;
    margin-bottom: 10px;
    box-shadow: 0 1px 2px rgba(0,0,0,.04);
  }
  body.sf-mobile table.sf-rt td {
    display: flex;
    align-items: flex-start;
    justify-content: space-between;
    gap: 10px;
    width: auto;
    padding: 4px 0;
    border: none;
    text-align: right !important;
    font-size: 13px;
  }
  body.sf-mobile table.sf-rt td::before {
    content: attr(data-l);
    flex: 0 0 auto;
    max-width: 44%;
    text-align: left;
    font-size: 11.5px;
    font-weight: 600;
    color: var(--gray-500, #6b7280);
    line-height: 1.5;
    padding-top: 1px;
  }
  /* 主标题行：首格（无标签）+ 次格 */
  body.sf-mobile table.sf-rt td.sf-rt-title {
    display: block;
    text-align: left !important;
    border-bottom: 1px solid var(--gray-100, #f3f4f6);
    padding-bottom: 7px;
    margin-bottom: 5px;
    font-size: 15px;
    font-weight: 700;
    color: var(--gray-900, #111827);
    word-break: break-all;
  }
  body.sf-mobile table.sf-rt td.sf-rt-title::before { display: none; }
  body.sf-mobile table.sf-rt td.sf-rt-title input[type=checkbox] {
    width: 18px; height: 18px;
    margin: 0 8px 0 0;
    vertical-align: -3px;
  }
  /* 空白单元格不占位 */
  body.sf-mobile table.sf-rt td.sf-rt-empty { display: none; }
  /* 长文本（技术要求 / 备注 / 退回原因）整行铺开 */
  body.sf-mobile table.sf-rt td.sf-rt-long { display: block; text-align: left !important; }
  body.sf-mobile table.sf-rt td.sf-rt-long::before {
    display: block;
    max-width: 100%;
    margin-bottom: 2px;
  }
  /* 同族订单「└─」子行 */
  body.sf-mobile table.sf-rt tr.sf-rt-child {
    background: #fafafa;
    margin-left: 14px;
    border-style: dashed;
  }
  /* 单格占位行（暂无数据 / 加载中） */
  body.sf-mobile table.sf-rt tr.sf-rt-msg {
    background: transparent;
    border: none;
    box-shadow: none;
    padding: 18px 6px;
    text-align: center;
    color: var(--gray-500, #6b7280);
  }
  body.sf-mobile table.sf-rt tr.sf-rt-msg td { display: block; text-align: center !important; }
  body.sf-mobile table.sf-rt tr.sf-rt-msg td::before { display: none; }
  /* 年份分组行 → 分节标题 */
  body.sf-mobile table.sf-rt tr.sf-rt-group {
    background: transparent;
    border: none;
    box-shadow: none;
    padding: 14px 2px 6px;
  }
  body.sf-mobile table.sf-rt tr.sf-rt-group td {
    display: block;
    text-align: left !important;
    font-size: 13px;
    font-weight: 700;
    color: var(--gray-600, #4b5563);
  }
  body.sf-mobile table.sf-rt tr.sf-rt-group td::before { display: none; }
  /* 操作格：整行铺开 */
  body.sf-mobile table.sf-rt td.sf-rt-act {
    display: flex;
    flex-wrap: wrap;
    justify-content: flex-start;
    gap: 6px;
    border-top: 1px dashed var(--gray-200, #e5e7eb);
    margin-top: 6px;
    padding-top: 9px;
    text-align: left !important;
  }
  body.sf-mobile table.sf-rt td.sf-rt-act::before { display: none; }
  body.sf-mobile table.sf-rt td.sf-rt-act .btn { flex: 1 1 44%; }

  /* 卡片模式下补出来的「全选本页」条 */
  body.sf-mobile .sf-rt-allbar {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 9px 12px;
    margin-bottom: 8px;
    background: var(--gray-50, #f9fafb);
    border: 1px solid var(--gray-200, #e5e7eb);
    border-radius: 8px;
    font-size: 13px;
    color: var(--gray-600, #4b5563);
  }
  body.sf-mobile .sf-rt-allbar input { width: 18px; height: 18px; }

  /* 检验表 / 打印表：保持表格 + 横向滚动 */
  body.sf-mobile .qc-table { min-width: 720px; }
  body.sf-mobile .pt-table { min-width: 660px; }
  body.sf-mobile .pt-sign { min-width: 520px; }

  /* ==========================================================================
   * 5. 工具栏 / 搜索 / 筛选
   * ======================================================================== */
  body.sf-mobile .table-toolbar {
    padding: 12px;
    gap: 8px;
  }
  body.sf-mobile .search-box,
  body.sf-mobile .toolbar .search,
  body.sf-mobile .search {
    flex: 1 1 100%;
    min-width: 0;
  }
  body.sf-mobile .table-toolbar select,
  body.sf-mobile .toolbar select {
    flex: 1 1 calc(50% - 4px);
    min-width: 0;
  }
  body.sf-mobile .table-toolbar > .btn,
  body.sf-mobile .toolbar > .btn { flex: 1 1 auto; }
  body.sf-mobile #soBatchBar { width: 100%; }
  body.sf-mobile #soBatchBar > .btn { flex: 1 1 calc(50% - 4px); }

  body.sf-mobile .filter-tabs {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }
  body.sf-mobile .filter-tabs::-webkit-scrollbar { display: none; }
  body.sf-mobile .filter-tab {
    flex: 0 0 auto;
    white-space: nowrap;
    padding: 8px 14px;
    font-size: 13px;
  }

  body.sf-mobile .sub-tabs {
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
    flex-wrap: nowrap;
    gap: 6px;
    margin-bottom: 14px;
  }
  body.sf-mobile .sub-tabs::-webkit-scrollbar { display: none; }
  body.sf-mobile .sub-tab {
    flex: 0 0 auto;
    white-space: nowrap;
    padding: 9px 14px;
    font-size: 13px;
    min-height: 40px;
    display: inline-flex;
    align-items: center;
  }

  /* ==========================================================================
   * 6. 统计卡片
   * ======================================================================== */
  body.sf-mobile .stats-grid {
    grid-template-columns: 1fr 1fr;
    gap: 10px;
    margin-bottom: 14px;
  }
  body.sf-mobile .stat-card { padding: 12px; gap: 10px; border-radius: 10px; }
  body.sf-mobile .stat-icon { width: 38px; height: 38px; font-size: 18px; border-radius: 10px; }
  body.sf-mobile .stat-info .stat-value { font-size: 21px; }
  body.sf-mobile .stat-info .stat-label { font-size: 12px; }
  /* 已归档订单页统计条（内联 4 列栅格） */
  body.sf-mobile #soStatsBar {
    grid-template-columns: 1fr 1fr !important;
    gap: 10px !important;
  }
  /* sales_order 的 .stat-row */
  body.sf-mobile .stat-row { gap: 10px; margin-bottom: 14px; }
  body.sf-mobile .stat-row > .stat {
    flex: 1 1 calc(50% - 5px);
    min-width: 0;
    padding: 12px 14px;
  }
  body.sf-mobile .stat .n { font-size: 20px; }

  /* ==========================================================================
   * 7. 表单 / 详情字段
   * ======================================================================== */
  body.sf-mobile .form-grid { grid-template-columns: 1fr !important; gap: 12px; }
  body.sf-mobile .detail-info-grid { grid-template-columns: 1fr !important; gap: 10px; }
  body.sf-mobile .detail-header { flex-direction: column; align-items: stretch; gap: 12px; }
  body.sf-mobile .detail-header .flex,
  body.sf-mobile .detail-header .toolbar { width: 100%; flex-wrap: wrap; gap: 8px; }
  body.sf-mobile .detail-header .btn { flex: 1 1 calc(50% - 4px); }
  body.sf-mobile .detail-info .order-no { font-size: 18px; }
  body.sf-mobile .form-group.full { grid-column: 1 / -1; }

  /* ==========================================================================
   * 8. 部门流转
   * ======================================================================== */
  body.sf-mobile .flex.gap-8 { flex-wrap: wrap; }
  body.sf-mobile .dept-step { font-size: 11.5px; padding: 5px 9px; }
  /* 卡片/窄屏里箭头换行后指向会错乱，隐藏箭头，靠顺序 + ‖ 并行标记表达 */
  body.sf-mobile .dept-arrow { display: none; }
  body.sf-mobile .dept-timeline {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 8px;
    overflow: visible;
  }
  body.sf-mobile .dept-timeline-item {
    min-width: 0;
    border-radius: 10px !important;
    padding: 12px 10px;
  }
  body.sf-mobile .dept-timeline-item::after { display: none !important; }
  body.sf-mobile .dept-timeline-item .dept-name { font-size: 13px; }

  /* ==========================================================================
   * 9. 弹窗 → 全屏
   * ======================================================================== */
  body.sf-mobile .modal-overlay { padding: 0; align-items: stretch; }
  body.sf-mobile .modal {
    max-width: 100% !important;
    width: 100%;
    height: 100dvh;
    max-height: 100dvh;
    border-radius: 0;
  }
  body.sf-mobile .modal-header {
    padding: 12px 14px;
    padding-top: calc(12px + env(safe-area-inset-top));
    position: sticky;
    top: 0;
    background: #fff;
    z-index: 2;
  }
  body.sf-mobile .modal-header h2 { font-size: 16px; }
  body.sf-mobile .modal-close { width: 40px; height: 40px; font-size: 20px; }
  body.sf-mobile .modal-body {
    padding: 14px;
    overflow: auto;
    -webkit-overflow-scrolling: touch;
  }
  body.sf-mobile .modal-footer {
    padding: 10px 14px calc(10px + env(safe-area-inset-bottom));
    position: sticky;
    bottom: 0;
    background: #fff;
    z-index: 2;
    flex-wrap: wrap;
    gap: 8px;
  }
  body.sf-mobile .modal-footer .btn { flex: 1 1 auto; }

  /* admin.html 的自定义弹窗 */
  body.sf-mobile .modal-bg { padding: 0 !important; align-items: stretch !important; }
  body.sf-mobile .modal-bg > div {
    max-width: 100% !important;
    width: 100% !important;
    height: 100dvh;
    max-height: 100dvh !important;
    border-radius: 0 !important;
    overflow: auto;
  }

  /* ==========================================================================
   * 10. 提示条 / 空态 / 校验
   * ======================================================================== */
  body.sf-mobile .toast {
    top: calc(64px + env(safe-area-inset-top));
    bottom: auto;
    left: 12px;
    right: 12px;
    text-align: center;
    transform: translateY(-180%);
  }
  body.sf-mobile .toast.show { transform: translateY(0); }

  body.sf-mobile .qc-legend { font-size: 11.5px; padding: 8px 10px; }
  body.sf-mobile .qc-summary { font-size: 12.5px; padding: 9px 10px; gap: 8px; }
  body.sf-mobile .qc-legend,
  body.sf-mobile .qc-summary { word-break: break-word; }
  body.sf-mobile .empty-state { padding: 30px 14px; }

  /* ==========================================================================
   * 11. sales_order.html 专属
   * ======================================================================== */
  body.sf-mobile .dropzone { padding: 26px 14px; }
  body.sf-mobile .dropzone .big { font-size: 30px; }
  body.sf-mobile .dropzone .t1 { font-size: 14px; }
  body.sf-mobile .list-item {
    flex-direction: column;
    align-items: stretch;
    gap: 8px;
    padding: 12px 14px;
  }
  body.sf-mobile .list-item .rr {
    text-align: left;
    white-space: normal;
    display: flex !important;
    flex-direction: row !important;
    flex-wrap: wrap;
    align-items: center !important;
    gap: 6px !important;
  }
  body.sf-mobile .list-item .rr .btn { flex: 1 1 calc(50% - 3px); }
  body.sf-mobile #previewTables { max-height: none; }
  body.sf-mobile .footer-note { margin-top: 18px; font-size: 11px; }

  /* ==========================================================================
   * 12. admin.html 专属
   * ======================================================================== */
  body.sf-mobile .owner-grid { grid-template-columns: 1fr !important; }
  body.sf-mobile .panel { padding: 14px; border-radius: 10px; overflow-x: auto; }
  body.sf-mobile .panel h3 { font-size: 14px; }
  body.sf-mobile table.grid { min-width: 520px; }

  /* ==========================================================================
   * 13. login.html
   * ======================================================================== */
  body.sf-mobile .login-card { width: 100% !important; max-width: 420px; padding: 22px 18px; }
}

/* ---------- login.html（未引入 perm.js，不依赖 body.sf-mobile） ---------- */
@media screen and (max-width: 820px) {
  body.login-page { align-items: flex-start; padding: 24px 0 32px; }
  body.login-page .login-card { width: 100%; max-width: 92vw; padding: 26px 20px; }
  body.login-page .field input { font-size: 16px; }
  body.login-page .field input,
  body.login-page .btn { min-height: 46px; }
  body.login-page .brand .logo { font-size: 32px; }
  body.login-page .brand h1 { font-size: 18px; }
  body.login-page .tip { margin-top: 14px; }
}

/* ---------- 触屏设备：按钮按压反馈 ---------- */
@media screen and (max-width: 820px) and (hover: none) {
  body.sf-mobile .nav-item:active { background: var(--gray-800, #1f2937); }
  body.sf-mobile .btn:active { filter: brightness(.94); }
}

/* ---------- 打印：移动层全部失效，保证打印排版不受影响 ---------- */
@media print {
  .sf-mtop, .sf-moverlay, .sf-rt-allbar { display: none !important; }
}
