/* ============================================================================
   OA 后台 admin.html —— H5 / 移动端显示适配层
   ----------------------------------------------------------------------------
   设计原则：
     · 作用域隔离：全部规则包裹在 max-width:768px 内 —— 桌面端零影响，可整文件回滚
     · 阅读列表：表格按内容自然宽度自适应、首列横向固定、字号回到可读区间、
                分页从「悬浮胶囊」回归普通流，避免压住最后一行
     · 填写报表：表单单列铺满、触控目标 ≥44px、输入字号 16px（防 iOS 聚焦放大）、
                弹窗内 header/footer 常驻，长表单只在 body 内滚动
     · 不依赖 JS：仅用 CSS 覆盖既有内联样式（!important 可覆盖 inline style），
                不修改任何现有 js，避免影响桌面端已调好的列表逻辑
     · 兼容基线 Chrome 63~79：不使用 min()/max()/clamp()/:has()/dvh
   维护约定：内容变化才 bump admin.html 中本文件的 ?v=
   ========================================================================== */

@media (max-width: 768px) {

  :root { --h5-header-h: 52px; }

  /* ---------- 1. 全局与内容容器 ---------- */
  html { -webkit-text-size-adjust: 100%; }
  body { overflow-x: hidden; }

  .content {
    padding: 12px 10px 16px !important;
    -webkit-overflow-scrolling: touch;
  }
  /* 刘海屏底部安全区（不支持 env 的浏览器走上面的固定值） */
  @supports (padding: env(safe-area-inset-bottom)) {
    .content { padding-bottom: calc(16px + env(safe-area-inset-bottom)) !important; }
  }

  /* 媒体元素一律不溢出 */
  #mainContent img,
  #mainContent iframe,
  #mainContent video { max-width: 100% !important; }
  #mainContent iframe { height: 60vh !important; min-height: 260px; }

  /* ★ 关键修复：body 是 flex 容器，.main-wrapper 默认 min-width:auto 会被内部的宽表格撑开，
     导致整页按 599px 布局、被浏览器缩小约 35%（390 屏字体变小、右侧内容被裁）。
     显式允许它收缩，溢出交给各表格容器自己横向滚动。 */
  body > .main-wrapper { min-width: 0; }
  body > .main-wrapper > .content,
  #mainContent > * { min-width: 0; }

  /* 内联写死的两列栅格（仪表盘图表区等）在窄屏统一落为单列 */
  #mainContent [style*="grid-template-columns:1fr 1fr"],
  #mainContent [style*="grid-template-columns: 1fr 1fr"] {
    grid-template-columns: 1fr !important;
  }
  /* 3/4/5 列的内联指标栅格（记账/报销顶部「收入合计/支出合计/余额/记录数」等）→ 2 列，
     否则每格只剩 80px，金额必然被裁 */
  #mainContent [style*="grid-template-columns:repeat(3,1fr)"],
  #mainContent [style*="grid-template-columns:repeat(4,1fr)"],
  #mainContent [style*="grid-template-columns:repeat(5,1fr)"],
  #mainContent [style*="grid-template-columns: repeat(3, 1fr)"],
  #mainContent [style*="grid-template-columns: repeat(4, 1fr)"],
  #mainContent [style*="grid-template-columns: repeat(5, 1fr)"] {
    grid-template-columns: repeat(2, 1fr) !important;
  }

  /* 模块自绘的内联 flex 行（记账管理顶部 Tab 条等）：不撑破内容区，超出部分横向滑动 */
  #mainContent > div,
  #mainContent > div > div { min-width: 0; }
  #mainContent > div > div[style*="display:flex"] {
    max-width: 100%;
    overflow-x: auto;
    -webkit-overflow-scrolling: touch;
    scrollbar-width: none;
  }
  #mainContent > div > div[style*="display:flex"]::-webkit-scrollbar { display: none; }
  #mainContent > div > div[style*="display:flex"] > * { flex-shrink: 0; }

  /* 筛选栏（标准 .toolbar-left 与模块自绘的内联 flex-wrap 容器）：
     统一为两列等宽栅格 —— 控件宽窄一致、左右对齐；「~」这类纯装饰连接符不占格子。
     ★ 不要用 flex-grow 拉伸控件：130px 的日期框会被拉到 171px，一行只放得下两个，
       整个筛选栏被撑成五六行（2026-11-02 实测回归）。 */
  #mainContent .toolbar-left,
  #mainContent div[style*="flex-wrap:wrap"][style*="align-items:center"] {
    display: grid !important;
    grid-template-columns: repeat(2, minmax(0, 1fr)) !important;
    gap: 8px !important;
    align-items: stretch !important;
    width: 100%;
  }
  #mainContent .toolbar-left > span,
  #mainContent div[style*="flex-wrap:wrap"][style*="align-items:center"] > span {
    display: none !important;
  }
  /* 搜索框独占整行 */
  #mainContent .toolbar-left > .search-wrapper,
  #mainContent div[style*="flex-wrap:wrap"][style*="align-items:center"] > input:first-child {
    grid-column: 1 / -1;
  }
  /* 按钮保持自然宽度、靠左，不拉成半屏 */
  #mainContent .toolbar-left > .btn,
  #mainContent div[style*="flex-wrap:wrap"][style*="align-items:center"] > .btn,
  #mainContent div[style*="flex-wrap:wrap"][style*="align-items:center"] > button {
    justify-self: start;
    width: auto !important;
    min-width: 0;
  }
  /* 栅格内的输入/下拉一律撑满格子 */
  #mainContent .toolbar-left > input,
  #mainContent .toolbar-left > select,
  #mainContent .toolbar-left > .form-input,
  #mainContent .toolbar-left > .form-select,
  #mainContent div[style*="flex-wrap:wrap"][style*="align-items:center"] > input,
  #mainContent div[style*="flex-wrap:wrap"][style*="align-items:center"] > select {
    width: 100% !important;
    max-width: 100% !important;
    min-width: 0;
  }

  /* ---------- 2. 顶部栏 ---------- */
  .header { height: var(--h5-header-h) !important; padding: 0 10px !important; }
  .header-left { flex: 1 1 auto; min-width: 0; overflow: hidden; gap: 8px; }
  .breadcrumb {
    flex: 1 1 auto; min-width: 0; max-width: none !important;
    font-size: 13px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  }
  .header-right { flex: 0 0 auto; }
  .header-btn, .collapse-btn { width: 38px !important; height: 38px !important; }
  .header-btn svg, .collapse-btn svg { width: 19px !important; height: 19px !important; }

  /* ---------- 3. 统计卡片（仪表盘 / 各模块顶部指标） ---------- */
  .stat-grid,
  .stat-grid-2row { grid-template-columns: repeat(2, 1fr) !important; gap: 8px !important; }
  .stat-card { padding: 12px !important; gap: 10px !important; border-radius: 12px; }
  .stat-icon { width: 34px !important; height: 34px !important; flex: 0 0 34px; border-radius: 10px; }
  .stat-icon svg { width: 18px !important; height: 18px !important; }
  .stat-info { min-width: 0; }
  .stat-label { font-size: 11px !important; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  /* 金额可能很长：允许换行而不是被卡片裁掉（不在数字中间硬断） */
  .stat-value {
    font-size: 17px !important; line-height: 1.25;
    word-break: normal; word-wrap: break-word; overflow-wrap: break-word;
    font-variant-numeric: tabular-nums;
  }
  /* 仪表盘固定尺寸画布等比缩放，避免右侧被裁 */
  #mainContent #pieChart,
  #mainContent #barChart { max-width: 100% !important; height: auto !important; }

  /* ★ 奇数个指标卡时，最后一个独占整行：
     否则「部门报销排行」这类内容多的卡片会被压在半列（181px）里，
     排名、部门名、金额挤成一团、部门名被拆成两行。 */
  #mainContent .stat-grid > .stat-card:last-child:nth-child(odd) { grid-column: 1 / -1; }

  /* 排行/明细型卡片内部的「名称 … 金额」行：中间留空隙，名称单行省略，金额不收缩 */
  #mainContent .stat-card div[style*="justify-content:space-between"] { gap: 10px; }
  #mainContent .stat-card div[style*="justify-content:space-between"] > span {
    min-width: 0; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
  }
  #mainContent .stat-card div[style*="justify-content:space-between"] > strong {
    flex: 0 0 auto; white-space: nowrap; font-variant-numeric: tabular-nums;
  }

  /* ---------- 4. 卡片 ---------- */
  .card { padding: 14px 12px !important; margin-bottom: 12px !important; border-radius: 12px !important; }
  .card-header { margin-bottom: 10px !important; gap: 8px; }
  .card-title { font-size: 14px !important; }

  /* ---------- 5. 阅读列表 ---------- */
  /* 5.1 列表容器：取消内联/脚本写入的固定高度（JS 会给 .acc-table-scroll 写 max-height），
         让手机端回到整页滚动，不再出现「页内小窗」双层滚动 */
  #mainContent .table-wrapper,
  #mainContent .acc-table-scroll,
  #mainContent .tt-table-scroll,
  #mainContent #contractTable,
  #mainContent .table-scroll-box {
    max-height: none !important;
    overflow-x: auto !important;
    -webkit-overflow-scrolling: touch;
    border-radius: 10px;
  }

  /* 5.2 表格：不再强制 600/500px 最小宽，列少时不横滚，列多时按住内容自然撑开 */
  #mainContent table { min-width: 100% !important; font-size: 13px !important; }
  #mainContent thead th {
    font-size: 12px !important; padding: 9px 10px !important;
    letter-spacing: 0 !important; text-transform: none;
  }
  #mainContent tbody td { font-size: 13px !important; padding: 11px 10px !important; }
  /* 行高一提，长列表目视定位更轻松 */
  #mainContent tbody tr { min-height: 44px; }

  /* 5.3 首列横向固定：左右滑动看金额/状态时，姓名、日期、单号始终在左边可对照
         （separate 是 sticky 列正确显示的前提，collapse 会导致透底/边框丢失） */
  #mainContent .acc-table-scroll table,
  #mainContent .table-wrapper table,
  #mainContent .tt-table-scroll table {
    border-collapse: separate; border-spacing: 0;
  }
  #mainContent .acc-table-scroll th:first-child,
  #mainContent .acc-table-scroll td:first-child,
  #mainContent .table-wrapper th:first-child,
  #mainContent .table-wrapper td:first-child,
  #mainContent .tt-table-scroll th:first-child,
  #mainContent .tt-table-scroll td:first-child {
    position: -webkit-sticky; position: sticky; left: 0; z-index: 2;
    background: var(--bg-card-solid, #fff);
    box-shadow: 1px 0 0 rgba(15, 23, 42, 0.07);
    padding-left: 10px;
  }
  #mainContent .acc-table-scroll thead th:first-child,
  #mainContent .table-wrapper thead th:first-child,
  #mainContent .tt-table-scroll thead th:first-child {
    z-index: 7; background: #f3f4f8;
  }
  /* 行悬停时首列跟随，避免出现「白块」 */
  #mainContent .acc-table-scroll tbody tr:hover td:first-child { background: #f0f1f5; }

  /* 5.3b 右侧固定的操作列（td.acc-sticky-r）在窄屏取消固定：
     费用报销的操作列长 293px（详情/出纳→会计进度/登记/打印），与首列 40px 合计占掉 390 屏的 85%，
     中间「日期/部门/金额/状态」全被压在它下面，手机上只能看到「单号 + 一堆操作按钮」。
     改为跟随横向滚动（滑到最右即可操作，行对应关系由首列固定维持），数据列恢复完整可见。 */
  #mainContent th.acc-sticky-r,
  #mainContent td.acc-sticky-r {
    position: static !important;
    right: auto !important;
    z-index: auto !important;
    width: auto !important;
    white-space: nowrap !important;
    box-shadow: none !important;
    padding: 9px 10px !important;
  }
  #mainContent td.acc-sticky-r .btn,
  #mainContent td.acc-sticky-r > .btn { padding: 5px 8px !important; font-size: 12px !important; }

  /* 5.4 分页：悬浮胶囊在窄屏回归普通流（JS 会给它写内联 position/width，此处强制复位），
         否则 780px 宽的胶囊会盖住列表最后一行 */
  #mainContent .oa-pager-float {
    position: static !important;
    left: auto !important; right: auto !important; bottom: auto !important;
    transform: none !important;
    width: auto !important; max-width: none !important;
    margin: 10px 0 0 !important;
    padding: 10px !important;
    border: 1px solid var(--border) !important;
    border-radius: 10px !important;
    background: var(--bg-card-solid, #fff) !important;
    box-shadow: none !important;
    -webkit-backdrop-filter: none !important; backdrop-filter: none !important;
    display: flex !important; flex-wrap: wrap;
    justify-content: center; gap: 8px;
  }
  /* ★ 分页拆两行：第一行「每页 N 条 + 第 x-y 条，共 z 条」，第二行页码按钮。
     原本两个子容器各 254/402px 挤在一行，页码组溢出 32px，"跳至"被压成竖排。 */
  #mainContent .oa-pager-float > div {
    flex: 1 1 100% !important;
    min-width: 0 !important;
    justify-content: center !important;
    flex-wrap: wrap !important;
    white-space: nowrap !important;
    row-gap: 6px;
  }
  #mainContent .oa-pager-float > div > span { white-space: nowrap !important; font-size: 12px; }
  #mainContent .oa-pager-float .btn,
  #mainContent .oa-pager-float .btn-sm {
    min-height: 30px !important; height: 30px !important;
    padding: 0 7px !important; font-size: 11.5px !important;
    flex: 0 0 auto;
  }
  #mainContent .oa-pager-float select { min-height: 30px !important; height: 30px !important; font-size: 12px !important; }
  #mainContent .oa-pager-float input {
    min-height: 30px !important; height: 30px !important; width: 44px !important;
    font-size: 12px !important; text-align: center; flex: 0 0 auto;
  }
  /* 分页预留的「占位尾行」只在悬浮胶囊盖住列表时需要，移动端分页已回到普通流，隐藏 */
  #mainContent .oa-tail-row,
  #mainContent tr.oa-tail-row { display: none !important; }
  .oa-pager-float-wrap::after { display: none !important; }
  .oa-pager-float-wrap .acc-table-scroll,
  .oa-pager-float-wrap .tt-table-scroll,
  .oa-pager-float-wrap #contractTable { padding-bottom: 0 !important; }

  #mainContent .pagination {
    flex-direction: column; align-items: stretch; gap: 8px;
    padding: 10px 0 2px !important;
  }
  #mainContent .page-info { text-align: center; font-size: 12px !important; color: var(--text-muted); }
  #mainContent .pagination-btns {
    justify-content: center; flex-wrap: nowrap !important;
    overflow-x: auto; -webkit-overflow-scrolling: touch;
    padding-bottom: 2px;
  }
  #mainContent .pagination-btns::-webkit-scrollbar { display: none; }
  #mainContent .page-btn {
    min-width: 40px !important; height: 40px !important;
    font-size: 13px !important; padding: 0 10px; flex: 0 0 auto;
  }
  #mainContent .pagination select.form-input { width: auto; min-height: 38px; }

  /* ---------- 6. 填写报表（表单） ---------- */
  #mainContent .form-grid { grid-template-columns: 1fr !important; gap: 0 !important; }
  #mainContent .form-group { margin-bottom: 14px; }
  #mainContent .form-group.full { grid-column: auto !important; }

  #mainContent .form-label {
    font-size: 13px !important; font-weight: 600;
    letter-spacing: 0 !important; text-transform: none;
    margin-bottom: 6px !important; color: var(--text-secondary);
  }
  #mainContent .form-label .required { font-size: 15px; line-height: 1; }

  #mainContent .form-input,
  #mainContent .form-select,
  #mainContent .form-textarea {
    min-height: 46px !important;
    padding: 12px 13px !important;
    font-size: 16px !important;      /* 低于 16px iOS 会在聚焦时自动放大整页 */
    border-radius: 10px !important;
    box-shadow: none;                /* 移动端去掉内阴影，输入框边界更清楚 */
  }
  #mainContent .form-textarea { min-height: 96px !important; line-height: 1.55; }
  /* 日期/月份类控件在 iOS 上需要显式高度才与其它输入框对齐 */
  #mainContent input[type="date"],
  #mainContent input[type="month"],
  #mainContent input[type="time"],
  #mainContent input[type="datetime-local"] { min-height: 46px !important; line-height: normal; }

  /* 记账风格表单（.acc-*）同步放宽 */
  #mainContent .acc-form-grid { grid-template-columns: 1fr !important; }
  #mainContent .acc-field-full { grid-column: auto !important; }
  #mainContent .acc-input,
  #mainContent .acc-select,
  #mainContent .acc-textarea {
    min-height: 46px !important; padding: 12px 13px !important; font-size: 16px !important;
  }
  #mainContent .acc-label { font-size: 13px !important; }

  /* 报销/差旅等「一行一条明细」的表单：窄屏改为纵向堆叠，避免每个输入框只剩 40px 宽 */
  #mainContent .expense-item-row {
    flex-direction: column !important;
    align-items: stretch !important;
    gap: 8px !important;
  }
  #mainContent .expense-item-row > .form-group { width: 100% !important; min-width: 0 !important; }
  /* 末位是「删除本行」按钮容器（原本内联 width:40px、按钮上 margin-top:22px 对齐同行输入框），
     纵向堆叠后靠右对齐、去掉对齐用的上边距 */
  #mainContent .expense-item-row > .form-group:last-child {
    width: auto !important; align-self: flex-end; margin-bottom: 0;
  }
  #mainContent .expense-item-row > .form-group:last-child .btn { margin-top: 0 !important; }
  #mainContent .expense-total-row {
    flex-direction: column; align-items: flex-start !important; gap: 6px;
    padding: 12px 14px !important;
  }

  /* ---------- 7. 弹窗（填写报表主战场） ---------- */
  .modal-overlay { padding: 0 !important; }
  .modal {
    width: 100% !important; max-width: 100% !important;
    max-height: 92vh !important;
    border-radius: 16px 16px 0 0 !important;
  }
  .modal.modal-large,
  .modal.modal-xl,
  .modal.modal-xxl,
  .modal.modal-contract-preview { width: 100vw !important; max-width: 100vw !important; }

  .modal-header { flex: 0 0 auto; padding: 13px 14px !important; }
  .modal-title { font-size: 16px !important; }
  /* 关键：长表单只在 body 内滚动，标题与底部按钮始终可见 */
  .modal-body {
    flex: 1 1 auto; min-height: 0; overflow-y: auto !important;
    -webkit-overflow-scrolling: touch;
    padding: 14px !important;
  }
  .modal-footer {
    flex: 0 0 auto; padding: 12px 14px !important;
    gap: 10px !important; background: var(--bg-card-solid, #fff);
    border-top: 1px solid var(--border);
  }
  .modal-footer .btn { flex: 1 1 0; min-width: 0; }
  @supports (padding: env(safe-area-inset-bottom)) {
    .modal-footer { padding-bottom: calc(12px + env(safe-area-inset-bottom)) !important; }
  }

  .acc-modal-overlay { padding: 0 !important; }
  .acc-modal-container {
    width: 100% !important; max-width: 100% !important;
    max-height: 92vh !important; border-radius: 16px 16px 0 0 !important;
  }
  .acc-modal-body { flex: 1 1 auto; min-height: 0; overflow-y: auto; padding: 14px !important; }
  .acc-modal-footer { padding: 12px 14px !important; }
  .acc-modal-footer .acc-btn-save,
  .acc-modal-footer .acc-btn-cancel { flex: 1 1 0; }

  /* ---------- 8. 其它零散元素 ---------- */
  .btn { min-height: 44px !important; font-size: 14px; padding: 10px 16px; }
  .btn-sm { min-height: 34px !important; padding: 6px 12px; font-size: 12px; }
  .tag { font-size: 12px; padding: 3px 8px; }

  /* Tab 条：可横向滑动，但隐藏滚动条保持整洁 */
  .dynamic-tabs { flex-wrap: nowrap !important; overflow-x: auto; -webkit-overflow-scrolling: touch; }
  .dynamic-tabs::-webkit-scrollbar { display: none; }
  .dynamic-tab { flex: 0 0 auto; font-size: 13px !important; padding: 8px 12px !important; }

  /* 文件上传区：触控目标够大，提示文字不挤 */
  .file-upload-area { padding: 18px 12px !important; border-radius: 12px; }
  .acc-file-placeholder { font-size: 13px; }

  /* 审核面板：贴边全宽 */
  .audit-panel { width: calc(100vw - 16px) !important; right: 0 !important; }

  /* 空态与加载态垂直居中更自然 */
  .empty-state { padding: 32px 16px !important; }
  .loading-spinner { padding: 32px !important; }
}

/* ---------- 9. 小屏（≤375px）再收一档 ---------- */
@media (max-width: 375px) {
  .header { height: 48px !important; }
  :root { --h5-header-h: 48px; }
  .stat-card { padding: 10px !important; gap: 8px !important; }
  .stat-icon { width: 30px !important; height: 30px !important; flex: 0 0 30px; }
  .stat-value { font-size: 15px !important; }
  .content { padding-left: 8px !important; padding-right: 8px !important; }
  .card { padding: 12px 10px !important; }
  #mainContent thead th { padding: 8px 9px !important; font-size: 11px !important; }
  #mainContent tbody td { padding: 10px 9px !important; font-size: 12px !important; }
}

/* ---------- 10. 极窄屏（≤360px）：指标卡片改单列，避免六位以上金额被折断 ---------- */
@media (max-width: 360px) {
  .stat-grid,
  .stat-grid-2row { grid-template-columns: 1fr !important; }
  .stat-value { font-size: 16px !important; }
}
