/* 手机适配层：最后加载。设计前提是桌面内网桌面端，这里按触屏重新组织，
   不隐藏任何核心功能，只改变排列、密度与触摸目标。 */
@media (max-width: 640px) {
  /* ---- 顶栏：两行。第一行品牌 + 图标控件，第二行导航横向滚动 ---- */
  .topbar { height: auto; min-height: 0; flex-wrap: wrap; gap: 6px 8px; padding: 6px 12px;
    padding-left: max(12px, env(safe-area-inset-left));
    padding-right: max(12px, env(safe-area-inset-right)); }
  .topbar > * { flex: none; white-space: nowrap; }
  .brand { font-size: var(--fs-sm); }
  .brand-mark { width: 24px; height: 24px; font-size: 12px; }
  .topnav { order: 3; flex: 1 0 100%; gap: 4px; overflow-x: auto; padding-bottom: 2px;
    scrollbar-width: none; -webkit-overflow-scrolling: touch; }
  .topnav::-webkit-scrollbar { display: none; }
  .topnav a { display: inline-flex; align-items: center; min-height: 34px; padding: 4px 10px; }
  .topbar .btn-sm { min-height: 36px; min-width: 36px; padding: 0 8px; }
  .theme-toggle .theme-label, .palette-open .palette-label, .palette-open kbd, .user-menu .user-name { display: none; }
  .user-menu > summary { min-height: 36px; padding: 0 4px; }
  .user-menu .caret { display: none; }
  .user-menu .menu { min-width: 150px; }

  /* ---- 页面骨架：单列、窄边距、流式宽度 ---- */
  .main { padding: var(--sp-3) 12px calc(var(--sp-6) + env(safe-area-inset-bottom));
    padding-left: max(12px, env(safe-area-inset-left));
    padding-right: max(12px, env(safe-area-inset-right)); }
  .main.narrow { max-width: 100%; }
  .page-head { flex-direction: column; align-items: stretch; gap: var(--sp-2); margin-bottom: var(--sp-3); }
  .page-head h1 { font-size: 1.2rem; }
  .h1-note { display: block; margin: 2px 0 0; }
  .page-sub { font-size: var(--fs-sm); max-width: none; white-space: normal; overflow-wrap: anywhere; }
  .page-actions { width: 100%; gap: var(--sp-2); }
  .page-actions .search, .page-actions .form-search-wrap { width: 100%; flex: 1 1 100%; }
  .form-search-wrap input { width: 100%; }
  .card { padding: var(--sp-3) 12px; }
  .section-title { flex-wrap: wrap; gap: 6px; }
  .stack > * + * { margin-top: var(--sp-3); }

  /* ---- 触摸目标与 iOS 聚焦缩放 ---- */
  .btn { min-height: 40px; }
  .btn-sm { min-height: 36px; }
  .btn-lg { min-height: 44px; }
  .btn-icon { width: 40px; height: 40px; }
  .chip { height: 26px; padding: 0 10px; }
  .nav-item, .idx-list a { min-height: 40px; }
  .dot { width: 20px; height: 20px; }
  .help { width: 24px; height: 24px; }
  .help::after { max-width: min(280px, 78vw); }
  input, select, textarea { font-size: 16px; height: 40px; }
  textarea { height: auto; }
  input[type=checkbox], input[type=radio] { width: 18px; height: 18px; }
  .perm-chip input { width: 100%; height: 100%; }
  .perm-chip span { height: 32px; padding: 0 10px; }
  .list-tabs .tab { min-height: 40px; padding: 6px 14px; }
  .form-row .dl { width: 36px; height: 36px; }
  .form-search-wrap kbd { display: none; }

  /* ---- 表单栅格与通用块改单列 ---- */
  .fields, .choices, .rec-grid { grid-template-columns: 1fr; }
  .fields { gap: var(--sp-3); }
  .field.wide { grid-column: auto; }
  .tiles { grid-template-columns: repeat(2, minmax(0, 1fr)); gap: var(--sp-2); }
  .tile { padding: var(--sp-3) 12px; }
  .kv { grid-template-columns: minmax(0, max-content) minmax(0, 1fr); }
  .kv dd { overflow-wrap: anywhere; }
  .cols { grid-template-columns: 1fr; }
  .inline-form { gap: var(--sp-2); }

  /* ---- 项目列表 ---- */
  .list-tools { flex-direction: column; align-items: stretch; gap: var(--sp-2); }
  .list-tabs { flex-wrap: nowrap; overflow-x: auto; scrollbar-width: none; -webkit-overflow-scrolling: touch; }
  .list-tabs::-webkit-scrollbar { display: none; }
  .list-tabs .tab { flex: none; }
  .list-tools .sort { width: 100%; height: 40px; }
  .proj-cards { grid-template-columns: 1fr; }
  .next { flex-wrap: wrap; gap: var(--sp-3); padding: var(--sp-3) 12px; }
  .next .title { font-size: var(--fs-md); }
  .stepper li { min-width: 64px; font-size: 11px; }

  /* ---- 受控表格目录 ---- */
  .forms-layout { grid-template-columns: 1fr; gap: var(--sp-3); }
  .idx-list { overflow-x: auto; scrollbar-width: none; -webkit-overflow-scrolling: touch; }
  .idx-list::-webkit-scrollbar { display: none; }
  .forms-main { max-height: none; overflow: visible; border: 0; }
  .forms-table .c-code { width: 84px; }
  .form-row td { padding: 8px 8px; }
  .g-toggle { min-height: 40px; }
  .cap-mobile { display: inline-block; }
  .group-row .g-src { display: none; }

  /* ---- 用户管理 ---- */
  .risk-panel { padding: var(--sp-3) 12px; }
  .risk-head { flex-direction: column; align-items: flex-start; gap: 2px; }
  .risk-row { grid-template-columns: 84px minmax(50px, 1fr) 32px; gap: var(--sp-2); }
  .risk-tag { grid-column: 1 / -1; text-align: left; padding-left: 2px; }
  .risk-foot { gap: var(--sp-3); }
  .risk-foot-total { margin-left: 0; }
  .help-fold > summary { min-height: 42px; }
  .perm-cell { min-width: 0; }
  .perm-form { flex-direction: column; align-items: stretch; }
  .perm-save { align-self: flex-start; }
  .data-table th, .data-table td { padding: 8px 8px; }
  .table-wrap { border-radius: var(--radius-sm); -webkit-overflow-scrolling: touch; }

  /* ---- 录入表：保持横向滚动，但格子要能点 ---- */
  .grid-toolbar { gap: var(--sp-2); }
  .grid-toolbar .search { width: 100%; flex: 1 1 100%; }
  .grid-wrap { max-height: 60vh; -webkit-overflow-scrolling: touch; }
  .grid-table td { height: 38px; }
  .grid-table thead th { height: 34px; }
  .grid-table td.in input, .grid-table td.in select { height: 38px; font-size: 16px; }
  .savebar { flex-wrap: wrap; gap: var(--sp-2); }
  .savebar .keys { display: none; }
  .savebar .btn { min-height: 40px; }

  /* ---- 浮层 ---- */
  .palette { padding-top: 8vh; }
  .palette-box { width: calc(100vw - 24px); }
  .toasts { top: 8px; left: 12px; right: 12px; transform: none; }
  .toast { max-width: 100%; }
  .undo-bar { left: 12px; right: 12px; bottom: 12px; max-width: none; flex-wrap: wrap; }
  .draft-bar { left: 12px; right: 12px; transform: translateY(-50%); animation: none;
    max-width: none; flex-wrap: wrap; justify-content: center; }

  /* ---- 登录 / 注册：品牌区压成一行，表单首屏可见 ---- */
  .auth-split { min-height: 100vh; min-height: 100dvh; grid-template-columns: 1fr; grid-template-rows: auto 1fr; }
  .auth-brand { padding: var(--sp-3) var(--sp-4); gap: var(--sp-2); }
  .auth-brand .brand { font-size: var(--fs-sm); }
  .auth-pitch h1 { font-size: 1.1rem; line-height: 1.3; margin: 0; }
  .auth-pitch h1::after, .auth-pitch p, .auth-pitch ul, .auth-kicker, .auth-org { display: none; }
  .auth-main { display: flex; flex-direction: column; align-items: stretch; justify-content: flex-start;
    padding: var(--sp-4) 12px calc(var(--sp-5) + env(safe-area-inset-bottom)); }
  .auth-main > .theme-toggle { position: static; align-self: flex-end; margin-bottom: var(--sp-3); }
  .auth-box { max-width: 100%; padding: var(--sp-4) var(--sp-3); }
  .auth-box-kicker { margin-bottom: var(--sp-3); padding-bottom: var(--sp-2); }
  .auth-box .field > input { height: 44px; }
  .auth-foot { flex-wrap: wrap; }
  .auth-foot .muted { margin-left: 0; }
}

/* 极窄屏（320–360px）收起品牌文字，只留标记 */
@media (max-width: 360px) {
  .brand { font-size: 0; }
  .brand-mark { font-size: 13px; }
  .topbar { gap: 4px; }
  .tiles { grid-template-columns: 1fr; }
}

/* 触摸设备：不依赖 hover 的下载入口始终可见；帮助提示可按住查看 */
@media (hover: none) {
  .form-row .dl { opacity: 1; }
  .help:active::after, .help:active::before { opacity: 1; visibility: visible; }
}

/* 大屏触摸设备（平板 / 触屏笔记本）同样放大点击区 */
@media (pointer: coarse) and (min-width: 641px) {
  .btn { min-height: 40px; }
  .btn-icon { width: 40px; height: 40px; }
  .list-tabs .tab, .nav-item { min-height: 40px; }
  .perm-chip span { height: 30px; }
  .form-row .dl { opacity: 1; }
}
