/* =====================================================================
 * 君易德进销存 - 手机端 H5 叠加样式
 * 触发条件: html.ims-mobile 由 mobile.js 注入
 * 桌面端完全不受影响（无 ims-mobile 类时所有规则不生效）
 * ===================================================================== */

/* ---------- 基础：16px 输入框防 iOS 缩放，安全区适配 ---------- */
html.ims-mobile,
html.ims-mobile body {
  font-size: 15px;
  -webkit-text-size-adjust: 100%;
}
html.ims-mobile input,
html.ims-mobile select,
html.ims-mobile textarea {
  font-size: 16px !important;
  border-radius: 8px;
}
html.ims-mobile button {
  min-height: 40px;
}
html.ims-mobile .ims-mobile-only {
  display: block !important;
}

/* ---------- 顶栏：留出安全区高度，隐藏侧边栏触发按钮 ---------- */
html.ims-mobile header.sticky.top-0 {
  padding-top: env(safe-area-inset-top);
  height: calc(56px + env(safe-area-inset-top));
  min-height: calc(56px + env(safe-area-inset-top));
}
html.ims-mobile button[data-slot="sidebar-trigger"] {
  display: none !important;
}
/* 移动端：强制隐藏 shadcn sidebar（不占用主内容宽度） */
html.ims-mobile [data-sidebar="sidebar"],
html.ims-mobile [data-slot="sidebar-wrapper"] > [data-sidebar="sidebar"] {
  display: none !important;
}
html.ims-mobile [data-sidebar="rail"],
html.ims-mobile [data-slot="sidebar-rail"] {
  display: none !important;
}
/* 移动端：让 sidebar 容器自身也回到全宽（避免预留空间） */
html.ims-mobile .group\/sidebar-wrapper {
  display: block !important;
}

/* ---------- 主内容：底部留出 Tab 栏空间 ---------- */
html.ims-mobile main {
  padding-bottom: calc(72px + env(safe-area-inset-bottom)) !important;
}
html.ims-mobile main > div[class*="rounded-xl"] {
  border-radius: 12px !important;
  margin: 8px !important;
  padding: 12px !important;
}

/* ---------- 登录页：全屏沉浸式 ---------- */
html.ims-mobile body > div.relative.flex {
  align-items: flex-start !important;
  padding-top: env(safe-area-inset-top) !important;
}
html.ims-mobile form.ims-login-form,
html.ims-mobile form.space-y-4 {
  max-width: 100% !important;
}
html.ims-mobile .ims-login-card,
html.ims-mobile form.space-y-4 {
  border-radius: 14px;
  padding: 20px 16px !important;
  margin: 12px !important;
}
html.ims-mobile .ims-login-input {
  height: 46px !important;
  font-size: 16px !important;
}
html.ims-mobile .ims-login-button,
html.ims-mobile form.space-y-4 button[type="submit"] {
  height: 48px !important;
  font-size: 16px !important;
  border-radius: 10px;
}

/* ---------- 表格转卡片（响应式表格） ---------- */
html.ims-mobile .ims-card-table {
  /* wrapper 加了 .ims-card-table 才会启用，避免误改其它表格 */
}
html.ims-mobile .ims-card-table table {
  display: block !important;
  width: 100% !important;
  border: none !important;
}
html.ims-mobile .ims-card-table thead {
  display: none !important;
}
html.ims-mobile .ims-card-table tbody {
  display: block !important;
  width: 100% !important;
}
html.ims-mobile .ims-card-table tbody tr {
  display: block !important;
  background: hsl(var(--card, 0 0% 100%));
  border: 1px solid hsl(var(--border, 220 13% 91%));
  border-radius: 12px;
  margin: 0 0 10px;
  padding: 8px 12px;
  box-shadow: 0 1px 2px rgba(0, 0, 0, 0.03);
}
html.ims-mobile .ims-card-table tbody tr:last-child {
  margin-bottom: 0;
}
html.ims-mobile .ims-card-table td {
  display: flex !important;
  align-items: center;
  justify-content: space-between;
  gap: 10px;
  padding: 6px 0 !important;
  border: none !important;
  text-align: right;
  min-height: 30px;
  font-size: 14px;
}
html.ims-mobile .ims-card-table td::before {
  content: attr(data-mlabel);
  flex: 0 0 auto;
  font-weight: 500;
  font-size: 12px;
  color: hsl(var(--muted-foreground, 220 9% 46%));
  text-align: left;
}
html.ims-mobile .ims-card-table td[data-mfull="1"] {
  /* 整行单独显示的列（如备注） */
  display: block !important;
  text-align: left;
  padding-top: 6px !important;
  border-top: 1px dashed hsl(var(--border, 220 13% 91%)) !important;
  margin-top: 6px;
}
html.ims-mobile .ims-card-table td[data-mfull="1"]::before {
  display: block;
  margin-bottom: 2px;
  font-size: 11px;
}
html.ims-mobile .ims-card-table td:first-child {
  font-weight: 600;
  font-size: 15px;
}
html.ims-mobile .ims-card-table tbody tr.is-pending {
  border-left: 3px solid hsl(var(--primary, 221 83% 53%));
}
html.ims-mobile .ims-card-table tbody tr.is-completed {
  opacity: 0.85;
}

/* ---------- 弹窗转底部 Sheet（仪表盘 PO 弹窗、报关运费弹窗等） ----------
 * 修复策略：内容区固定 max-height + overflow-y:auto 自身滚动，
 * 底部 DialogFooter 用 position:sticky 始终钉在弹窗底部
 */
html.ims-mobile [data-slot="dialog-content"],
html.ims-mobile [data-slot="alert-dialog-content"] {
  left: 0 !important;
  top: auto !important;
  bottom: 0 !important;
  right: 0 !important;
  transform: none !important;
  width: 100% !important;
  max-width: 100% !important;
  max-height: 90vh !important;
  border-radius: 16px 16px 0 0 !important;
  border-bottom: none !important;
  padding: 16px !important;
  padding-bottom: calc(12px + env(safe-area-inset-bottom)) !important;
}
html.ims-mobile [data-slot="dialog-content"] > .absolute,
html.ims-mobile [data-slot="dialog-content"] button[class*="absolute"] {
  top: 10px !important;
  right: 10px !important;
  z-index: 5;
}
/* 拖拽手柄 */
html.ims-mobile [data-slot="dialog-content"]::before {
  content: "";
  display: block;
  width: 36px;
  height: 4px;
  background: hsl(var(--muted-foreground, 220 9% 46%) / 0.3);
  border-radius: 2px;
  margin: 0 auto 8px;
}
/* 主内容区：可滚动，固定 max-height 留出 header + footer 空间 */
html.ims-mobile [data-slot="dialog-content"] > div:not([data-slot]):not(.absolute) {
  max-height: calc(90vh - 200px) !important;
  overflow-y: auto !important;
  -webkit-overflow-scrolling: touch;
  padding-right: 2px;
}
/* DialogFooter 用 sticky 钉在弹窗底部 */
html.ims-mobile [data-slot="dialog-footer"] {
  position: sticky !important;
  bottom: 0 !important;
  margin: 12px -16px -12px !important;
  padding: 12px 16px calc(12px + env(safe-area-inset-bottom)) !important;
  border-top: 1px solid hsl(var(--border, 220 13% 91%)) !important;
  background: hsl(var(--background, 0 0% 100%)) !important;
  box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.04);
}
/* Footer 按钮在手机上占满宽度 */
html.ims-mobile [data-slot="dialog-footer"].flex-col-reverse {
  flex-direction: column-reverse !important;
}
html.ims-mobile [data-slot="dialog-footer"] button {
  width: 100% !important;
  height: 44px !important;
  font-size: 15px !important;
}

/* ---------- 报关运费弹窗内部布局优化 ---------- */
/* 3 列输入（关税/报关费/运费）在手机上改为单列 */
html.ims-mobile [data-slot="dialog-content"] .grid.grid-cols-3 {
  grid-template-columns: 1fr !important;
  gap: 12px !important;
}
/* 2 列信息网格加间距 */
html.ims-mobile [data-slot="dialog-content"] .grid.grid-cols-2.gap-3 {
  gap: 12px !important;
}
/* 商品明细表格内嵌滚动区域收紧 */
html.ims-mobile [data-slot="dialog-content"] .overflow-y-auto {
  max-height: 160px !important;
}
/* 商品明细表格字体适配 */
html.ims-mobile [data-slot="dialog-content"] table.text-xs {
  font-size: 11px !important;
}
html.ims-mobile [data-slot="dialog-content"] table.text-xs th,
html.ims-mobile [data-slot="dialog-content"] table.text-xs td {
  padding: 4px 6px !important;
}

/* ---------- 底部 Tab 栏 ---------- */
#ims-mtab {
  position: fixed;
  left: 0;
  right: 0;
  bottom: 0;
  z-index: 60;
  display: none;
  height: calc(64px + env(safe-area-inset-bottom));
  padding-bottom: env(safe-area-inset-bottom);
  background: hsl(var(--background, 0 0% 100%) / 0.96);
  backdrop-filter: saturate(180%) blur(16px);
  -webkit-backdrop-filter: saturate(180%) blur(16px);
  border-top: 1px solid hsl(var(--border, 220 13% 91%));
  box-shadow: 0 -2px 8px rgba(0, 0, 0, 0.04);
  font-family: inherit;
  user-select: none;
  -webkit-user-select: none;
}
html.ims-mobile #ims-mtab { display: block; }
#ims-mtab-inner {
  display: grid;
  grid-template-columns: repeat(5, 1fr);
  height: 64px;
  max-width: 640px;
  margin: 0 auto;
}
.ims-mtab-btn {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: 2px;
  background: none;
  border: none;
  cursor: pointer;
  color: hsl(var(--muted-foreground, 220 9% 46%));
  font-size: 11px;
  font-weight: 500;
  padding: 6px 0;
  min-height: 48px;
  transition: color 0.15s;
  -webkit-tap-highlight-color: transparent;
}
.ims-mtab-btn:active { opacity: 0.6; }
.ims-mtab-btn svg {
  width: 22px;
  height: 22px;
  stroke: currentColor;
  fill: none;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}
.ims-mtab-btn.is-active {
  color: hsl(var(--primary, 221 83% 53%));
}
.ims-mtab-btn.is-active svg {
  stroke: hsl(var(--primary, 221 83% 53%));
  fill: hsl(var(--primary, 221 83% 53%)) / 0.1;
}

/* ---------- 「更多」底部 Sheet ---------- */
#ims-more-sheet {
  position: fixed;
  inset: 0;
  z-index: 70;
  display: none;
  background: rgba(0, 0, 0, 0.45);
  align-items: flex-end;
  justify-content: center;
}
html.ims-mobile #ims-more-sheet.open { display: flex; }
#ims-more-card {
  width: 100%;
  max-width: 640px;
  background: hsl(var(--background, 0 0% 100%));
  border-radius: 16px 16px 0 0;
  padding: 8px 0 calc(16px + env(safe-area-inset-bottom));
  animation: imsSlideUp 0.2s ease-out;
}
@keyframes imsSlideUp {
  from { transform: translateY(100%); }
  to   { transform: translateY(0); }
}
#ims-more-handle {
  width: 36px;
  height: 4px;
  background: hsl(var(--muted-foreground, 220 9% 46%));
  opacity: 0.3;
  border-radius: 2px;
  margin: 0 auto 12px;
}
#ims-more-title {
  font-size: 13px;
  font-weight: 600;
  color: hsl(var(--muted-foreground, 220 9% 46%));
  padding: 0 20px 8px;
}
#ims-more-list {
  display: grid;
  grid-template-columns: repeat(4, 1fr);
  gap: 4px 0;
  padding: 0 8px;
}
.ims-more-link {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
  padding: 12px 4px;
  text-decoration: none;
  color: hsl(var(--foreground, 220 13% 13%));
  font-size: 12px;
  border-radius: 8px;
  cursor: pointer;
}
.ims-more-link:active { background: hsl(var(--accent, 220 14% 96%)); }
.ims-more-link svg {
  width: 24px;
  height: 24px;
  stroke: hsl(var(--primary, 221 83% 53%));
  fill: none;
  stroke-width: 2;
  stroke-linecap: round;
  stroke-linejoin: round;
}

/* ---------- FAB 浮动按钮（仅在销售/采购/商品列表显示） ---------- */
#ims-fab {
  position: fixed;
  right: 16px;
  bottom: calc(80px + env(safe-area-inset-bottom));
  z-index: 55;
  display: none;
  width: 56px;
  height: 56px;
  border-radius: 50%;
  background: hsl(var(--primary, 221 83% 53%));
  color: #fff;
  border: none;
  box-shadow: 0 4px 14px rgba(37, 99, 235, 0.4);
  align-items: center;
  justify-content: center;
  cursor: pointer;
  -webkit-tap-highlight-color: transparent;
}
html.ims-mobile #ims-fab.show { display: flex; }
#ims-fab:active { transform: scale(0.94); }
#ims-fab svg {
  width: 26px;
  height: 26px;
  stroke: currentColor;
  fill: none;
  stroke-width: 2.4;
  stroke-linecap: round;
  stroke-linejoin: round;
}
#ims-fab-label {
  position: absolute;
  right: 64px;
  background: rgba(0, 0, 0, 0.75);
  color: #fff;
  font-size: 12px;
  padding: 4px 10px;
  border-radius: 6px;
  white-space: nowrap;
  pointer-events: none;
  opacity: 0;
  transition: opacity 0.15s;
}
#ims-fab:hover #ims-fab-label,
#ims-fab:active #ims-fab-label { opacity: 1; }

/* ---------- 仪表盘：在手机上给 PO 区块单独优化 ---------- */
html.ims-mobile .ims-mobile-dashboard-po {
  /* PO 卡片在移动端占满宽度 */
  display: grid !important;
  grid-template-columns: 1fr 1fr !important;
  gap: 8px !important;
}

/* ---------- 响应式细节 ---------- */
html.ims-mobile h1 { font-size: 1.35rem !important; }
html.ims-mobile h2 { font-size: 1.15rem !important; }
html.ims-mobile h3 { font-size: 1rem !important; }

/* 顶部右侧按钮组在小屏上更紧凑 */
html.ims-mobile header.sticky.top-0 > div:last-child {
  gap: 4px !important;
}
html.ims-mobile header.sticky.top-0 button {
  padding: 0 10px !important;
  font-size: 13px !important;
}

/* 列表页顶部操作按钮行：自动换行，避免溢出 */
html.ims-mobile main > div > div[class*="flex"][class*="gap"] {
  flex-wrap: wrap !important;
  row-gap: 8px !important;
}
/* 操作按钮组的内层（flex shrink-0 items-center gap-2）也允许换行 */
html.ims-mobile main div.flex.shrink-0[class*="gap-2"] {
  flex-wrap: wrap !important;
  row-gap: 8px !important;
}

/* 横屏手机（双栏） */
@media (max-width: 932px) and (orientation: landscape) and (pointer: coarse) {
  html.ims-mobile main {
    padding-bottom: calc(64px + env(safe-area-inset-bottom)) !important;
  }
  html.ims-mobile .ims-card-table tbody tr {
    display: grid !important;
    grid-template-columns: repeat(2, 1fr);
    gap: 0 16px;
  }
}
