/* ============================================================
   移动端适配（≤720px）
   分三块：① 布局改造（侧栏→底部标签栏）② 表单控件触摸优化
          ③ 表格卡片化（依赖 td[data-label]，由 UI.labelTables 注入）
   全部写在媒体查询内，桌面端零影响。
   ============================================================ */

@media (max-width: 720px) {

  /* ---------------------------------------------------- ① 布局 */
  #app { flex-direction: column; }

  /* 侧边栏 → 底部标签栏（横向滚动，带 safe-area） */
  .sidebar {
    position: fixed; left: 0; right: 0; bottom: 0; z-index: 40;
    width: auto; flex: none; flex-direction: row;
    border-right: none; border-top: 1px solid var(--line);
    box-shadow: 0 -2px 14px rgba(15, 23, 42, 0.08);
    padding-bottom: env(safe-area-inset-bottom);
  }
  .brand-mini, .side-foot, .nav-sep { display: none !important; }
  #nav {
    display: flex; flex: 1; padding: 6px;
    overflow-x: auto; -webkit-overflow-scrolling: touch; scrollbar-width: none;
  }
  #nav::-webkit-scrollbar { display: none; }
  #nav a {
    flex: 0 0 auto; margin: 0 4px 0 0; padding: 9px 12px;
    font-size: 13px; min-height: 40px; white-space: nowrap;
  }
  main { flex: 1 1 auto; padding-bottom: 72px; overflow-y: auto; }

  .topbar { padding: 14px 16px 2px; flex-wrap: wrap; gap: 8px; }
  .topbar h1 { font-size: 18px; }
  .page-desc { font-size: 12px; }
  .view { padding: 12px 16px 24px; }

  .cards { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: 10px; }
  .card { padding: 12px; }
  .card .value { font-size: 22px; }
  .panel { margin-top: 12px; border-radius: 10px; }
  .panel-head { padding: 12px; flex-wrap: wrap; gap: 8px; }
  .panel-body { padding: 12px; }

  /* 工具行：页面里写死了 style="display:flex;gap:8px" + select 内联固定宽度，
     内联样式 @media 覆盖不了，窄屏会把右侧按钮挤出屏幕（异常中心）或把按钮文字挤竖排（报表）。
     用 !important 覆盖内联样式：容器强制换行，下拉改为弹性宽度（约半行宽，放不下自动换行）。 */
  .panel-head > div[style*="display:flex"] {
    flex-wrap: wrap !important;
    width: 100%;
    row-gap: 8px;
  }
  .panel-head > div[style*="display:flex"] > select {
    width: auto !important;
    flex: 1 1 45%;
    min-width: 0;
  }
  .empty { padding: 32px 12px; }

  /* 标签页：窄屏标签会横向溢出，滚动 + 吸顶，当前项由 JS 滚进视野 */
  .tab-bar {
    position: sticky; top: 0; z-index: 20;
    margin: 0 -16px; padding: 0 12px; background: #fff;
    -webkit-overflow-scrolling: touch;
    box-shadow: 0 1px 0 rgba(15, 23, 42, 0.05);
  }
  .tab-item { min-height: 44px; padding: 10px 12px; }
  .pager { justify-content: center; gap: 8px; }
  /* 注意：必须写 .btn.tiny 三级选择器，否则会被下面的 .btn.tiny 规则（同优先级、更靠后）覆盖 */
  .pager .btn.tiny { min-height: 44px; padding: 8px 14px; }
  /* 手机上分页条三段竖排：信息 → 每页条数 → 页码按钮，避免挤成一团 */
  .pager-info { margin-right: 0; width: 100%; text-align: center; }
  .pager-size { justify-content: center; width: 100%; }
  .pager-btns { justify-content: center; width: 100%; }

  /* ---------------------------------------------------- ② 表单控件 */
  /* iOS 聚焦时字号 <16px 会整页放大，这里统一 16px */
  input, select, textarea {
    font-size: 16px;
    padding: 11px 12px;
    min-height: 44px;          /* 触摸目标 ≥44px */
    border-radius: 8px;
  }
  textarea { min-height: 120px; font-size: 15px; line-height: 1.55; }

  /* 原生 select 在 iOS/Android 上样式割裂，统一为自绘箭头 */
  select {
    -webkit-appearance: none; appearance: none;
    padding-right: 36px;
    background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'><path d='M1 1.5 6 6.5 11 1.5' fill='none' stroke='%239aa3af' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round'/></svg>");
    background-repeat: no-repeat;
    background-position: right 13px center;
    background-size: 12px 8px;
  }
  select:focus { background-image: url("data:image/svg+xml;utf8,<svg xmlns='http://www.w3.org/2000/svg' width='12' height='8' viewBox='0 0 12 8'><path d='M1 1.5 6 6.5 11 1.5' fill='none' stroke='%230e8cff' stroke-width='1.6' stroke-linecap='round' stroke-linejoin='round'/></svg>"); }

  /* 勾选框放大，整行可点 */
  input[type="checkbox"], input[type="radio"] {
    width: 20px; height: 20px; min-height: 0; flex: 0 0 20px; margin: 0;
  }
  .cb { min-height: 44px; gap: 10px; font-size: 14px; }

  /* 表单一律单列，字段之间拉开距离 */
  .form-row { flex-direction: column; gap: 0; margin-bottom: 4px; }
  .form-row > div { min-width: 0; margin-bottom: 14px; }
  .form-row label { font-size: 13px; margin-bottom: 6px; }
  .modal .form-row:last-child > div { margin-bottom: 0; }

  /* 按钮触摸目标 */
  .btn { min-height: 42px; padding: 9px 14px; font-size: 14px; }
  .btn.tiny { min-height: 36px; padding: 7px 11px; font-size: 13px; }
  .btn.block { min-height: 48px; font-size: 16px; }
  .btn.danger, .btn.primary { min-height: 42px; }

  /* 干掉 300ms 点击延迟与双击缩放 */
  button, a, input, select, textarea, label { touch-action: manipulation; }
  body { -webkit-text-size-adjust: 100%; }

  /* ---------------------------------------------------- 对话框 → 底部抽屉 */
  .modal-mask { align-items: flex-end !important; padding: 0; overflow: hidden; }
  .modal {
    max-width: 100% !important; width: 100%;
    border-radius: 16px 16px 0 0;
    max-height: 92vh; display: flex; flex-direction: column;
    animation: sheetIn .22s cubic-bezier(.2, .8, .3, 1);
  }
  .modal-head { padding: 14px 16px; }
  .modal-head h3 { font-size: 16px; }
  .modal-x { font-size: 26px; padding: 2px 8px; }
  .modal-body {
    flex: 1 1 auto; overflow-y: auto; -webkit-overflow-scrolling: touch;
    padding: 14px 16px; max-height: none;
  }
  .modal-foot {
    flex-wrap: wrap; gap: 8px; border-radius: 0;
    padding: 10px 16px;
    padding-bottom: calc(10px + env(safe-area-inset-bottom));
  }
  .modal-foot-left, .modal-foot-right { flex: 1 1 auto; gap: 8px; }
  .modal-foot-left:empty { display: none; }
  .modal-foot .btn { flex: 1; min-height: 46px; font-size: 15px; }
  @keyframes sheetIn { from { transform: translateY(100%); } to { transform: none; } }

  /* 引导步骤条在窄屏收紧 */
  .steps { gap: 4px; margin-bottom: 12px; }
  .steps .step { padding: 8px 2px; font-size: 12px; }
  pre.preview { max-height: 150px; overflow: auto; font-size: 12px; }

  /* ---------------------------------------------------- ③ 表格卡片化 */
  table[data-labelled] { display: block; }
  table[data-labelled] thead { display: none; }
  table[data-labelled] tbody { display: block; }
  table[data-labelled] tr {
    display: block; background: #fff; border: 1px solid var(--line);
    border-radius: 10px; padding: 10px 12px; margin-bottom: 10px;
  }
  table[data-labelled] tbody tr:hover { background: #fff; }
  table[data-labelled] td {
    display: flex; align-items: baseline; justify-content: space-between;
    gap: 12px; border: none; padding: 5px 0; text-align: right; flex-wrap: wrap;
  }
  table[data-labelled] td::before {
    content: attr(data-label);
    flex: 0 0 auto; color: var(--text-3); font-size: 12px; text-align: left;
  }
  table[data-labelled] td:empty { display: none; }
  table[data-labelled] td .btn { margin: 2px 0 2px 6px; }
  table[data-labelled] td.num::before { flex: 1 1 auto; }

  /* ---------------------------------------------------- 其它 */
  .toast {
    left: 16px; right: 16px; bottom: calc(76px + env(safe-area-inset-bottom));
    text-align: center; border-radius: 8px; padding: 12px 16px;
  }
  .login-wrap, .take-wrap {
    padding: 16px; padding-top: 48px; align-items: flex-start; height: auto; min-height: 100vh;
  }
  .login-card, .take-card { width: 100%; max-width: 420px; padding: 20px; border-radius: 14px; }
  .login-card label { margin: 16px 0 6px; }
  .phone { max-width: 100%; border-width: 6px; border-radius: 20px; }
  .grid2 { grid-template-columns: 1fr; }
}

/* 超窄屏（≤380px）：统计卡片改单列，避免数字被挤断 */
@media (max-width: 380px) {
  .cards { grid-template-columns: 1fr; }
}

/* ------------------------------------------------------------
   自绘下拉面板（触摸设备替代原生 select 弹层）
   原生弹层位置由浏览器绘制、无法控制（移动视口下会跑到屏幕左上角）。
   JS 侧已按 pointer:coarse 启用（js/ui.js 尾部），桌面不触发。
   z-index 200：高于 modal(50) / toast(100)，可在抽屉内使用。
   ------------------------------------------------------------ */
.select-sheet-mask {
  position: fixed; inset: 0; z-index: 200;
  background: rgba(15, 23, 42, 0.45);
  display: flex; align-items: flex-end;
  animation: maskIn .12s ease-out;
}
.select-sheet-mask.out { opacity: 0; transition: opacity .14s ease-in; }
.select-sheet {
  width: 100%; background: #fff;
  border-radius: 16px 16px 0 0;
  padding-bottom: env(safe-area-inset-bottom);
  display: flex; flex-direction: column; max-height: 70vh;
  animation: sheetIn .22s cubic-bezier(.2, .8, .3, 1);
}
.select-sheet-title {
  padding: 14px 16px 4px; font-size: 13px; color: var(--text-3); text-align: center;
}
.select-sheet-opts { overflow-y: auto; -webkit-overflow-scrolling: touch; padding: 6px 10px; }
.select-sheet-opt {
  display: flex; align-items: center; justify-content: space-between; gap: 10px;
  width: 100%; min-height: 46px; padding: 10px 12px;
  border: none; background: none; font-size: 16px; text-align: left;
  border-radius: 10px; color: var(--text); line-height: 1.4;
}
.select-sheet-opt:active { background: #f1f5f9; }
.select-sheet-opt.on { color: var(--primary); font-weight: 600; }
.select-sheet-opt .ck { font-style: normal; color: var(--primary); flex: 0 0 auto; }
.select-sheet-cancel {
  margin: 6px 12px 12px; min-height: 46px; flex: none;
  border: none; background: #f1f5f9; border-radius: 12px;
  font-size: 16px; color: var(--text);
}
