/* ============================================================
   移动端适配 — 手机 / 平板窄屏
   原则：
   1) 不改动桌面端规则，本文件仅在窄屏生效（媒体查询内）
   2) 表格由横向滚动改为「卡片流」布局，字段名以 data-label 显示
   3) 触摸目标不小于 44×44（移动端可点按标准）
   4) 弹层全屏化，避免被软键盘/地址栏遮挡
   ============================================================ */

/* ============================================================
   1. 基础：安全区、字号、溢出
   ============================================================ */
:root {
  /* 移动端独立令牌，不污染桌面端（仅在窄屏覆盖） */
  --mobile-tap: 44px;
  --mobile-gap: 12px;
}

@media (max-width: 768px) {
  /* 刘海屏 / 底部横条安全区 */
  body {
    padding-bottom: env(safe-area-inset-bottom, 0px);
  }

  /* 页面左右留白收窄，把空间让给内容 */
  .container { padding: 0 12px; }

  .page { padding: var(--spacing-16) 0 var(--spacing-48); }

  /* 标题缩放：桌面 30px 在手机上过大 */
  h1 { font-size: 24px; }
  h2 { font-size: 21px; }
  h3 { font-size: 18px; }
  .page-head h1 { font-size: 22px; }
  .page-head { margin-bottom: var(--spacing-16); }

  /* 卡片内边距收窄 */
  .card { padding: 16px; border-radius: 10px; }

  /* 去掉卡片 hover 位移效果（触屏无 hover，且会引发误触） */
  .card.hoverable:hover,
  .list-item:hover,
  .btn-primary:hover,
  .btn-secondary:hover,
  .btn-success:hover,
  .btn-danger:hover {
    transform: none;
  }
  .card.hoverable:hover { box-shadow: var(--shadow-rest); }
  .list-item:hover { box-shadow: var(--shadow-rest); }
  .btn-primary:hover { background: var(--color-primary); }
  .btn-secondary:hover { border-color: var(--color-border-strong); color: var(--color-text); }
  .btn-success:hover { filter: none; }
  .btn-danger:hover { filter: none; }

  /* 表单控件：字号不小于 16px，避免 iOS Safari 聚焦时自动放大页面 */
  input[type='text'], input[type='search'], input[type='date'],
  input[type='number'], input[type='password'], select, textarea {
    font-size: 16px;
    padding: 10px 12px;
    min-height: var(--mobile-tap);
  }
  textarea { min-height: 96px; }

  /* 触摸目标放大 */
  .btn { min-height: var(--mobile-tap); padding: 10px 16px; }
  .btn-sm { min-height: 36px; padding: 6px 12px; }
  .icon-btn { width: var(--mobile-tap); height: var(--mobile-tap); }
  .multiselect-trigger { min-height: var(--mobile-tap); padding: 10px 12px; }
  .multiselect-option { padding: 12px; min-height: var(--mobile-tap); }

  /* 时间在窄屏用紧凑格式（完整格式见 .t-full） */
  .t-full { display: none; }
  .t-short { display: inline; }

  /* 表格单元格行距放宽，拇指点按更准 */
  tbody td { padding: 12px 14px; }
  thead th { padding: 10px 14px; }

  /* 项目名称在窄屏允许占满整行，不再限制 320px */
  .contract-name { max-width: 100%; }
}

/* ============================================================
   2. 顶部导航：折叠为汉堡菜单
   ============================================================ */
/* 桌面端：隐藏汉堡按钮与移动端用户信息（必须写在媒体查询之前，
   否则同特异度下后写的规则会覆盖窄屏样式） */
.nav-toggle,
.nav-user-mobile { display: none; }

@media (max-width: 768px) {
  .topnav { height: 56px; }

  .topnav .container {
    gap: 12px;
    justify-content: space-between;
  }

  /* 品牌区允许收缩，标题过长时省略 */
  .brand {
    flex: 1;
    min-width: 0;
    font-size: 16px;
    overflow: hidden;
    text-overflow: ellipsis;
  }
  .brand-mark { width: 26px; height: 26px; font-size: 13px; flex: none; }

  /* 汉堡按钮：仅窄屏显示；置于顶栏最右（移动端主导航习惯） */
  .nav-toggle {
    order: 4;
    display: inline-flex;
    align-items: center;
    justify-content: center;
    width: var(--mobile-tap);
    height: var(--mobile-tap);
    flex: none;
    border: 1px solid var(--color-border);
    border-radius: var(--radius-buttons);
    background: var(--color-surface);
    color: var(--color-text);
    cursor: pointer;
    font-size: 18px;
    line-height: 1;
  }
  .nav-toggle:focus-visible { outline: none; box-shadow: var(--color-ring); }

  /* 导航链接：窄屏改为下拉面板 */
  .nav-links {
    position: absolute;
    top: 56px;
    left: 0;
    right: 0;
    flex-direction: column;
    gap: 0;
    background: var(--color-surface);
    border-bottom: 1px solid var(--color-border);
    box-shadow: var(--shadow-hover);
    padding: 6px 12px 10px;
    /* 折叠态：隐藏并脱离文档流 */
    display: none;
  }
  .nav-links.open { display: flex; }

  .nav-links a {
    padding: 13px 8px;
    font-size: var(--text-body);
    border-bottom: 1px solid var(--color-border);
    border-radius: 0;
  }
  .nav-links a:last-child { border-bottom: none; }
  .nav-links a.active {
    color: var(--color-primary);
    font-weight: var(--font-weight-semibold);
    background: var(--color-primary-soft);
    border-radius: var(--radius-buttons);
  }

  /* 右侧区：只保留图标按钮，用户信息挪到菜单底部 */
  .nav-right { order: 3; gap: 8px; flex: none; }
  .nav-right .user-chip { display: none; }

  /* 内联 SVG 图标尺寸（替代缺字形的字符图标） */
  .nav-toggle svg {
    width: 18px;
    height: 18px;
    display: block;
  }

  /* 菜单里的用户信息块（由 JS 注入到 .nav-links 内） */
  .nav-user-mobile {
    display: flex;
    align-items: center;
    gap: 8px;
    padding: 12px 8px 4px;
    margin-top: 4px;
    border-top: 1px solid var(--color-border);
    font-size: var(--text-body-sm);
    color: var(--color-text-muted);
  }
  .nav-user-mobile .user-name {
    color: var(--color-text);
    font-weight: var(--font-weight-medium);
  }
}

/* ============================================================
   3. 表格 → 卡片流布局
   依赖 JS 为每个 td 注入 data-label（字段中文名）
   ============================================================ */
@media (max-width: 768px) {
  /* 取消最小宽度限制，外层不再横向滚动 */
  .table-wrap table,
  .table-wrap.narrow table,
  .table-wrap.wide table {
    min-width: 0;
  }

  .table-scroll { overflow-x: visible; }

  /* 隐藏表头 */
  .table-wrap thead { display: none; }

  /* 每个 tr 变成一张卡片 */
  .table-wrap tbody tr {
    display: block;
    padding: 12px 14px;
    border-bottom: 1px solid var(--color-border);
    background: var(--color-surface);
  }
  .table-wrap tbody tr:last-child { border-bottom: none; }
  .table-wrap tbody tr:hover { background: var(--color-surface); }

  /* 每个 td 变成「字段名: 值」一行 */
  .table-wrap tbody td {
    display: flex;
    align-items: baseline;
    justify-content: space-between;
    gap: 16px;
    padding: 5px 0;
    border: none;
    text-align: left !important;
    white-space: normal;
  }

  /* 字段名：取自 data-label */
  .table-wrap tbody td::before {
    content: attr(data-label);
    flex: none;
    font-size: var(--text-label);
    font-weight: var(--font-weight-medium);
    color: var(--color-text-muted);
    letter-spacing: var(--tracking-label);
  }

  /* 无 data-label 的单元格（如空状态 colspan）不显示伪标签 */
  .table-wrap tbody td:not([data-label])::before { content: none; }

  /* 金额等右对齐内容在卡片里改为左对齐跟随 */
  .table-wrap tbody td.num,
  .table-wrap tbody td.amount-cell {
    text-align: left !important;
    font-variant-numeric: tabular-nums;
  }

  /* 项目名称独占一行且允许换行 */
  .table-wrap tbody td[data-label='项目名称'] {
    display: block;
    font-size: var(--text-body);
    margin-bottom: 4px;
  }
  .table-wrap tbody td[data-label='项目名称']::before {
    display: block;
    margin-bottom: 2px;
  }

  /* 操作按钮：整行显示，方便点按 */
  .table-wrap tbody td[data-label='操作'] {
    display: block;
    padding-top: 10px;
  }
  .table-wrap tbody td[data-label='操作']::before { display: none; }
  .table-wrap tbody td[data-label='操作'] .btn { width: 100%; }

  /* 表格容器在卡片模式下不再需要边框包裹感 */
  .table-wrap {
    border-radius: var(--radius-cards);
  }

  /* 历史/审批表的固定列宽在卡片模式下失效，需重置 */
  .table-history,
  .table-approvals { table-layout: auto; }
  .table-history th,
  .table-history td,
  .table-approvals th,
  .table-approvals td {
    width: auto !important;
    text-align: left !important;
  }

  /* 状态徽章在窄屏右对齐更好看 */
  .table-wrap tbody td[data-label='状态'] {
    justify-content: flex-start;
  }

  /* 合同额：卡片内突出显示 */
  .table-wrap tbody td[data-label='合同额'],
  .table-wrap tbody td[data-label='合同额合计'] {
    border-top: 1px dashed var(--color-border);
    margin-top: 4px;
    padding-top: 8px;
  }
  .table-wrap tbody td[data-label='合同额']::before,
  .table-wrap tbody td[data-label='合同额合计']::before {
    font-weight: var(--font-weight-semibold);
  }

  /* ---------- 可折叠列表卡片（审批中心 / 我的查询） ----------
     旧设计：每个字段独占一行、标签值左右撑开，5 行才放 5 个字段，
             且「查询条件」这个最关键的信息排在最后、字号最小。
     新设计（两行摘要卡）：
       第 1 行  查询条件（主标题） ................. 状态徽章 + ▾
       第 2 行  #编号 · 发起人 · 时间 ................. 操作按钮
     展开后追加：结果数 · 合同额 / 审批意见
     ------------------------------------------------------------------ */
  .table-wrap tbody.rows-collapsible {
    display: block;
  }

  .table-wrap tbody tr.row-collapsible {
    display: flex !important;
    flex-flow: row wrap;
    align-items: center;
    gap: 3px 8px;
    padding: 12px 14px;
    cursor: pointer;
  }

  /* 换行符：把卡片切成「标题行 / 元信息行 / 详情行」 */
  .table-wrap tbody tr.row-collapsible::after {
    content: '';
    order: 3;
    flex: 0 0 100%;
    height: 0;
    margin: 0;
  }
  .table-wrap tbody tr.row-collapsible::before {
    content: '';
    order: 8;
    flex: 0 0 100%;
    height: 0;
    margin: 0;
  }

  /* 卡片内单元格基线：紧排，标签与值贴合 */
  .table-wrap tbody tr.row-collapsible td {
    display: flex;
    align-items: center;
    justify-content: flex-start;
    gap: 2px;
    padding: 0;
    width: auto !important;
    border: none;
    text-align: left !important;
    font-size: var(--text-body-sm);
    color: var(--color-text-muted);
    white-space: normal;
  }
  .table-wrap tbody tr.row-collapsible td::before {
    font-weight: var(--font-weight-regular);
    letter-spacing: normal;
  }

  /* 第 1 行：查询条件为主标题 */
  .table-wrap tbody tr.row-collapsible td[data-cell='cond'] {
    order: 1;
    /* 不抢占整行：按内容收缩，长文本在自身内部换行，把空间让给状态徽章 */
    flex: 0 1 auto;
    min-width: 0;
    max-width: calc(100% - 104px);
    display: block;
    font-size: var(--text-body);
    font-weight: var(--font-weight-medium);
    color: var(--color-text);
    line-height: 1.45;
    overflow-wrap: anywhere;
  }
  .table-wrap tbody tr.row-collapsible td[data-cell='cond']::before { content: none; }

  /* 状态徽章：贴右；▾ 表示可展开 */
  .table-wrap tbody tr.row-collapsible td[data-cell='status'] {
    order: 2;
    flex: 0 0 auto;
    margin-left: auto;
    align-self: flex-start;
  }
  .table-wrap tbody tr.row-collapsible td[data-cell='status']::before { content: none; }
  .table-wrap tbody tr.row-collapsible td[data-cell='status']::after {
    content: '\25BE';
    margin-left: 6px;
    font-size: 13px;
    color: var(--color-text-muted);
    transition: transform var(--transition);
    display: inline-block;
    line-height: 1;
  }
  .table-wrap tbody tr.row-collapsible.expanded td[data-cell='status']::after {
    transform: rotate(180deg);
  }

  /* 第 2 行：元信息 */
  .table-wrap tbody tr.row-collapsible td[data-cell='id'] {
    order: 4;
    flex: 0 0 auto;
  }
  .table-wrap tbody tr.row-collapsible td[data-cell='id']::before { content: '#'; }
  .table-wrap tbody tr.row-collapsible td[data-cell='id']::after {
    content: '\00B7';
    margin-left: 5px;
    color: var(--color-text-faint);
  }
  .table-wrap tbody tr.row-collapsible td[data-cell='requester'] {
    order: 5;
    flex: 0 0 auto;
  }
  .table-wrap tbody tr.row-collapsible td[data-cell='requester']::before { content: none; }
  .table-wrap tbody tr.row-collapsible td[data-cell='requester']::after {
    content: '\00B7';
    margin-left: 5px;
    color: var(--color-text-faint);
  }
  .table-wrap tbody tr.row-collapsible td[data-cell='time'] {
    order: 6;
    flex: 0 0 auto;
  }
  .table-wrap tbody tr.row-collapsible td[data-cell='time']::before { content: none; }

  /* 操作按钮：右对齐紧凑按钮，不再通栏 */
  .table-wrap tbody tr.row-collapsible td[data-cell='action'] {
    order: 7;
    flex: 0 0 auto;
    margin-left: auto;
    padding: 0 !important;
  }
  .table-wrap tbody tr.row-collapsible td[data-cell='action']::before { content: none; }
  .table-wrap tbody tr.row-collapsible td[data-cell='action'] .btn {
    width: auto;
    min-width: 68px;
    min-height: 36px;
    padding: 6px 16px;
    font-size: var(--text-body-sm);
    border-radius: var(--radius-buttons);
  }

  /* 展开态追加：结果数 · 合同额 / 审批意见 */
  .table-wrap tbody tr.row-collapsible td[data-cell='count'],
  .table-wrap tbody tr.row-collapsible td[data-cell='amount'],
  .table-wrap tbody tr.row-collapsible td[data-cell='opinion'] {
    display: none;
  }
  .table-wrap tbody tr.row-collapsible.expanded td[data-cell='count'] {
    display: flex;
    order: 9;
    flex: 0 0 auto;
    margin-top: 3px;
  }
  .table-wrap tbody tr.row-collapsible.expanded td[data-cell='amount'] {
    display: flex;
    order: 10;
    flex: 0 0 auto;
    margin-top: 3px;
    border-top: none;
    padding-top: 0;
  }
  .table-wrap tbody tr.row-collapsible.expanded td[data-cell='opinion'] {
    display: flex;
    order: 11;
    flex: 0 0 100%;
    margin-top: 3px;
  }
  .table-wrap tbody tr.row-collapsible td[data-cell='amount']::before { content: '合同额'; }

  /* 未审批时结果数为「—」，不占位 */
  .table-wrap tbody tr.row-collapsible td.is-empty { display: none; }

  /* ---------- 通知记录卡（独立布局，字段语义与审批/历史不同） ----------
     第 1 行  [渠道徽章]  通知标题（主标题，可换行）....... [状态徽章]
     第 2 行  接收人 · 类型 · 时间
     通过 #notify-body 精确限定，避免影响审批/历史表。 */
  #notify-body tr.row-collapsible td[data-cell='channel'] {
    order: 1;
    flex: 0 0 auto;
    margin-right: 8px;
    align-self: flex-start;
    white-space: nowrap;
  }
  /* 渠道徽章本身已表达渠道，去掉重复的「渠道」前缀 */
  #notify-body tr.row-collapsible td[data-cell='channel']::before { content: none; }
  #notify-body tr.row-collapsible td[data-cell='cond'] {
    order: 2;
    flex: 1 1 0;
    min-width: 0;
    max-width: none;
    align-self: flex-start;
    font-size: var(--text-body-sm);
    overflow: hidden;
    text-overflow: ellipsis;
    white-space: nowrap;
  }
  #notify-body tr.row-collapsible td[data-cell='status'] {
    order: 3;
    flex: 0 1 auto;
    min-width: 0;
    margin-left: auto;
    align-self: flex-start;
  }
  /* 状态徽章：窄屏用短文案 */
  #notify-body .badge-label-full { display: none; }
  #notify-body .badge-label-short { display: inline; }
  #notify-body tr.row-collapsible td[data-cell='recipient'] {
    order: 4;
    flex: 0 0 auto;
  }
  #notify-body tr.row-collapsible td[data-cell='recipient']::after {
    content: '\00B7';
    margin-left: 5px;
    color: var(--color-text-faint);
  }
  #notify-body tr.row-collapsible td[data-cell='kind'] {
    order: 5;
    flex: 0 0 auto;
  }
  #notify-body tr.row-collapsible td[data-cell='kind']::after {
    content: '\00B7';
    margin-left: 5px;
    color: var(--color-text-faint);
  }
  #notify-body tr.row-collapsible td[data-cell='time'] {
    order: 6;
    flex: 0 0 auto;
  }
  /* 通知记录无操作列，首行不需要为它预留空间 */
  #notify-body tr.row-collapsible td[data-cell='cond']::after {
    content: none;
  }
}

/* ============================================================
   4. 结果区 / 统计块
   ============================================================ */
@media (max-width: 768px) {
  .result-summary { grid-template-columns: repeat(2, minmax(0, 1fr)) !important; gap: 10px; }
  .result-summary .stat-value { font-size: 20px; }
  .result-summary .stat-label { font-size: 11px; }

  /* 审批页的结果概览为 2 列内联样式，这里覆盖为 1 列更稳 */
  .grid-2 > .card .result-summary { grid-template-columns: repeat(2, minmax(0, 1fr)) !important; }

  .stat-value { font-size: 24px; }
  .stat-value.sm { font-size: 17px; }

  /* 工具栏改为纵向堆叠 */
  .result-toolbar { flex-direction: column; align-items: stretch; }

  /* 空状态留白收窄 */
  .empty { padding: 36px 16px; }
  .empty-icon { width: 40px; height: 40px; font-size: 18px; }

  /* 审批决策按钮竖排，避免并排挤压 */
  .decision-box .flex.gap-8,
  #decision-pending .flex.gap-8 {
    flex-direction: column;
  }
  .decision-box .flex.gap-8 .btn,
  #decision-pending .flex.gap-8 .btn { width: 100%; }

  /* 审批结论单选按钮平分宽度 */
  .radio-group { gap: 10px; }
  .radio { flex: 1; justify-content: center; padding: 10px 12px; }

  /* 筛选栏在窄屏改为 2 列网格，高度约减半 */
  .filter-bar {
    display: grid;
    grid-template-columns: 1fr 1fr;
    gap: 10px 12px;
    padding: 12px;
  }
  .filter-bar .field { min-width: 0; }
  .filter-bar .field:nth-child(3) { grid-column: 1 / -1; }
  .filter-bar .btn {
    grid-column: 1 / -1;
    width: auto;
    justify-self: start;
  }

  /* 映射行：标签在上、控件在下 */
  .mapping-row { grid-template-columns: minmax(0, 1fr); gap: 6px; padding: 12px 0; }
  .mapping-row label { font-size: var(--text-body); }

  /* 字段对应关系项 */
  .mapping-item { grid-template-columns: minmax(0, 1fr); }

  /* 上传拖拽区 */
  .dropzone { padding: 20px 14px; }

  /* 聊天工具栏：按钮占满 */
  .chat-toolbar { flex-direction: column; align-items: stretch; }
  .chat-toolbar .btn { width: 100%; }
  .chat-suggest { justify-content: flex-start; }

  /* 页面头：返回按钮提到标题上方（移动端习惯），按钮宽度自适应 */
  .page-head .flex-between {
    flex-direction: column-reverse;
    align-items: flex-start;
    gap: 10px;
  }
  .page-head .flex-between > div { width: 100%; }
  .page-head .flex-between .btn { width: auto; }

  /* 顶部统计卡的标题 */
  .kv-list { gap: 6px 16px; }
  .kv { width: 100%; justify-content: space-between; }
}

/* ============================================================
   5. 弹层 / Toast / 抽屉
   ============================================================ */
@media (max-width: 768px) {
  /* 弹层贴近全屏，避免内容被压缩 */
  .modal-backdrop { padding: 0; align-items: flex-end; }
  .modal {
    max-width: 100%;
    max-height: 92vh;
    border-radius: var(--radius-cards) var(--radius-cards) 0 0;
    padding: 20px 16px calc(20px + env(safe-area-inset-bottom, 0px));
  }

  /* Toast 移到底部通栏，拇指可达 */
  .toast-wrap {
    left: 12px;
    right: 12px;
    bottom: calc(12px + env(safe-area-inset-bottom, 0px));
    gap: 8px;
  }
  .toast { max-width: 100%; }

  /* 提示条竖排 */
  .alert { flex-direction: column; gap: 4px; }

  /* 审批决策区单选按钮竖排时保持可点 */
  .approval-hero { padding: var(--spacing-24) 0 var(--spacing-16); }
}

/* ============================================================
   6. 登录页
   ============================================================ */
@media (max-width: 768px) {
  .login-card { margin: 24px auto !important; }
  .page .card[style*='max-width:520px'] { margin: 24px auto !important; }
}

/* ============================================================
   7. 平板中间断点（641~900px）
   ============================================================ */
@media (min-width: 641px) and (max-width: 900px) {
  .container { padding: 0 20px; }
  .grid-3, .grid-4 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
  .result-summary { grid-template-columns: repeat(3, minmax(0, 1fr)); }
  /* 两卡布局保持两列，避免出现空位 */
  .result-summary.two-col { grid-template-columns: repeat(2, minmax(0, 1fr)); }
}

/* ============================================================
   8. 横屏矮屏（手机横放）
   ============================================================ */
@media (max-width: 900px) and (max-height: 480px) {
  .topnav { height: 48px; }
  .nav-links { top: 48px; }
  .approval-hero { padding: var(--spacing-16) 0; }
}

/* ============================================================
   9. 打印（保持桌面表格形态）
   ============================================================ */
@media print {
  .nav-toggle, .nav-links, .nav-right, .toast-wrap { display: none !important; }
  .table-wrap thead { display: table-header-group; }
  .table-wrap tbody tr { display: table-row; }
  .table-wrap tbody td { display: table-cell; }
  .table-wrap tbody td::before { content: none; }
}
