/* =========================================================================
 * style.fixes.css — UI 统一与布局瑕疵修复
 * ------------------------------------------------------------------------
 * 用法: 必须在主 style.css 之后加载, 用"最后赢"机制覆盖内部冲突。
 *
 * 设计原则:
 *   1. 非侵入  — 不删原有规则, 只用最小的覆盖纠正最后的胜者。
 *   2. 语义稳定 — 变量在最后一次 :root 的基础上统一。
 *   3. 系统化  — 按令牌(间距/圆角/阴影/按钮/表单/模态/表格) 分组。
 *
 * 排查范围以 css/style.css (~200KB) 为基础。主要冲突点:
 *   - :root 声明 5 次(L3, 3667, 4400, 4903, 7873), 最后一次赢。
 *   - body.light-mode 声明 4 次, 产生变量漂移。
 *   - .btn 声明 4 处, padding 不一致: 858 / 1653 / 3275 / 3894。
 *   - .form-input 声明 10+ 次, height/padding/radius 不一致。
 *   - z-index 乱序: 8 / 90 / 99 / 100 / 1000 / 1200 / 1201 / 2000。
 *   - @media 断点 15+ 不同阈值: 390/420/480/640/768/769/900/1180/1400。
 * ======================================================================= */


/* ------------------------------------------------------------------------ *
 * 1. 设计令牌最终化 — 为后续规则提供唯一事实源
 * ------------------------------------------------------------------------ */
:root {
  /* 间距系统 — 4/8/12/16/24/32 */
  --space-1: 4px;
  --space-2: 8px;
  --space-3: 12px;
  --space-4: 16px;
  --space-5: 20px;
  --space-6: 24px;
  --space-8: 32px;

  /* 统一圆角 */
  --radius-xs: 6px;
  --radius-sm: 8px;
  --radius-md: 12px;
  --radius-lg: 16px;
  --radius-xl: 20px;
  --radius-pill: 9999px;

  /* 控件高度一致性 — 杜绝按钮/输入对不齐 */
  --control-height-sm: 30px;
  --control-height: 38px;
  --control-height-lg: 44px;

  /* 统一过渡 */
  --motion-fast: 120ms ease;
  --motion-base: 180ms ease;

  /* 层级系统 — 杜绝 random z-index */
  --z-sidebar: 80;
  --z-topbar: 90;
  --z-dropdown: 200;
  --z-modal-overlay: 1000;
  --z-modal: 1010;
  --z-toast: 2000;
  --z-tooltip: 2100;
}


/* ------------------------------------------------------------------------ *
 * 2. 基础布局修复 — Flex/Grid 子元素溢出、滚动、文本截断
 *
 * 根因: 不少 flex 容器的子元素没写 min-width:0,
 *       长文本 / 大表格 / 长商品名会撑破布局。
 * ------------------------------------------------------------------------ */
body {
  min-width: 0;
  overflow-x: hidden;
}

.app-layout,
.main-area,
.main-content,
#mainContent {
  min-width: 0;
  min-height: 0;
}

.sidebar,
.sidebar .nav-list,
.sidebar-header,
.sidebar-header .sidebar-brand-copy {
  min-width: 0;
}

.sidebar-header .sidebar-brand-copy h1,
.sidebar-header .sidebar-brand-copy p {
  white-space: nowrap;
  overflow: hidden;
  text-overflow: ellipsis;
}

.page-header,
.page-header > *,
.filter-bar,
.filter-bar > *,
.header-actions {
  min-width: 0;
}

/* 长产品名截断 */
.product-card .product-name,
.purchase-record-card .product-name,
.sale-item-input .product-name,
.ai-item-row .product-name {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
}

/* 统一卡片行内按钮组避免换行抖动 */
.header-actions,
.page-header .header-actions {
  display: flex;
  flex-wrap: wrap;
  gap: var(--space-2);
  align-items: center;
}


/* ------------------------------------------------------------------------ *
 * 3. 按钮系统统一 — 高度 / 内边距 / 对齐
 *
 * 主 CSS 在 L858 / L1653 / L3275 / L3894 重复定义 .btn 的 padding,
 * 这里用一次清晰的系统覆盖收拢。
 * ------------------------------------------------------------------------ */
.btn {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  gap: var(--space-2);
  height: var(--control-height);
  padding: 0 var(--space-4);
  line-height: 1;
  border-radius: var(--radius-sm);
  font-weight: 600;
  font-size: 13px;
  transition: background var(--motion-base), color var(--motion-base), transform var(--motion-fast), opacity var(--motion-base);
  white-space: nowrap;
  flex-shrink: 0;
}

.btn-sm {
  height: var(--control-height-sm);
  padding: 0 var(--space-3);
  font-size: 12px;
}

.btn-lg {
  height: var(--control-height-lg);
  padding: 0 var(--space-5);
  font-size: 14px;
}

.btn-icon {
  min-width: var(--control-height);
  width: var(--control-height);
  padding: 0;
}

.btn-sm.btn-icon {
  min-width: var(--control-height-sm);
  width: var(--control-height-sm);
}

.btn-lg.btn-icon {
  min-width: var(--control-height-lg);
  width: var(--control-height-lg);
}

/* 禁用状态视觉统一 */
.btn[disabled],
.btn:disabled {
  opacity: 0.55;
  cursor: not-allowed;
  transform: none !important;
}

/* 行内按钮组 — 解决按钮高度和表单不对齐 */
.filter-bar .btn,
.header-actions .btn,
.modal-footer .btn,
.form-actions .btn,
.confirm-actions .btn {
  height: var(--control-height);
}


/* ------------------------------------------------------------------------ *
 * 4. 表单控件统一 — 高度 / 边框 / 焦点环
 *
 * 主 CSS 的 .form-input 在 10+ 处重复定义。
 * 这里硬统一高度,让一行表单内的 input/select/按钮都齐平。
 * ------------------------------------------------------------------------ */
.form-input,
.form-select,
select.form-input,
textarea.form-input,
input.form-input {
  height: var(--control-height);
  padding: 0 var(--space-3);
  border-radius: var(--radius-sm);
  font-size: 13px;
  line-height: var(--control-height);
  box-sizing: border-box;
  min-width: 0;
  width: 100%;
}

textarea.form-input {
  height: auto;
  min-height: calc(var(--control-height) * 2);
  padding: var(--space-2) var(--space-3);
  line-height: 1.5;
}

.form-input-sm,
.form-select-sm {
  height: var(--control-height-sm);
  padding: 0 var(--space-2);
  line-height: var(--control-height-sm);
  font-size: 12px;
}

.form-input:focus,
.form-select:focus,
textarea.form-input:focus {
  outline: none;
  border-color: var(--accent-blue);
  box-shadow: 0 0 0 3px rgba(79, 143, 255, 0.18);
}

.form-group {
  display: flex;
  flex-direction: column;
  gap: var(--space-2);
  min-width: 0;
}

.form-group label {
  font-size: 12px;
  font-weight: 600;
  color: var(--text-secondary);
  line-height: 1.2;
}

/* 密码切换按钮垂直居中 — 解决 eye icon 错位 */
.password-input-wrap {
  position: relative;
  display: flex;
  align-items: center;
}

.password-input-wrap .form-input {
  padding-right: calc(var(--control-height) + var(--space-1));
}

.password-input-wrap .password-toggle {
  position: absolute;
  right: 4px;
  top: 50%;
  transform: translateY(-50%);
  height: calc(var(--control-height) - 8px);
  width: calc(var(--control-height) - 8px);
  min-width: 0;
}


/* ------------------------------------------------------------------------ *
 * 5. 模态 / 对话框 / Toast 层级系统化
 * ------------------------------------------------------------------------ */
.sidebar {
  z-index: var(--z-sidebar);
}

.top-bar,
.topbar {
  z-index: var(--z-topbar);
}

.modal-overlay {
  z-index: var(--z-modal-overlay);
  /* 避免 iOS Safari 动态工具栏顶到内容 */
  align-items: flex-start;
}

.modal-dialog,
.confirm-dialog {
  z-index: var(--z-modal);
  /* 安全区: 顶栏 / 底部 home 指示器 */
  margin: max(var(--space-4), env(safe-area-inset-top, 0px)) auto
          max(var(--space-4), env(safe-area-inset-bottom, 0px));
  max-height: calc(100dvh - 2 * var(--space-4));
  max-height: calc(var(--app-visual-height, 100vh) - 2 * var(--space-4));
  display: flex;
  flex-direction: column;
  min-width: 0;
}

.modal-dialog .modal-body {
  overflow: auto;
  -webkit-overflow-scrolling: touch;
  min-height: 0;
  flex: 1 1 auto;
}

.modal-footer {
  flex-shrink: 0;
  display: flex;
  flex-wrap: wrap;
  justify-content: flex-end;
  gap: var(--space-2);
  padding: var(--space-3) var(--space-5);
}

.modal-footer .btn {
  margin: 0;
  min-width: 80px;
}

/* Toast 层级统一 */
.toast,
.toast-container,
.vm-toast {
  z-index: var(--z-toast);
}


/* ------------------------------------------------------------------------ *
 * 6. 表格容器防溢出
 *
 * 不同页面的 table 在窄屏总会出现水平撑破或 sticky thead 失效。
 * 这里对所有 data-table 的外层容器统一 overflow 行为。
 * ------------------------------------------------------------------------ */
.data-table-wrap,
.table-wrap,
#purchaseTableWrap,
#salesTableWrap,
#productsTableWrap {
  overflow-x: auto;
  -webkit-overflow-scrolling: touch;
  border-radius: var(--radius-md);
  min-width: 0;
}

.data-table {
  width: 100%;
  min-width: 560px; /* 让 overflow-x 生效, 而不是被折行 */
  border-collapse: collapse;
}

.data-table th,
.data-table td {
  vertical-align: middle;
}

/* 操作列紧凑, 避免占满 */
.data-table td:last-child {
  white-space: nowrap;
}

.data-table td:last-child .btn {
  padding: 0 var(--space-2);
  height: var(--control-height-sm);
  min-width: 0;
}


/* ------------------------------------------------------------------------ *
 * 7. Skeleton 加载态 — 为 navigateTo 的 SWR 准备
 * ------------------------------------------------------------------------ */
.page-skeleton {
  padding: var(--space-4);
  display: flex;
  flex-direction: column;
  gap: var(--space-4);
}

.page-skeleton .skeleton-title {
  height: 28px;
  width: 40%;
  min-width: 120px;
  border-radius: var(--radius-sm);
}

.page-skeleton .skeleton-grid {
  display: grid;
  grid-template-columns: repeat(auto-fill, minmax(220px, 1fr));
  gap: var(--space-3);
}

.page-skeleton .skeleton-card {
  height: 120px;
  border-radius: var(--radius-md);
}

.page-skeleton .skeleton-title,
.page-skeleton .skeleton-card {
  background: linear-gradient(
    90deg,
    var(--bg-card) 0%,
    var(--bg-card-hover) 50%,
    var(--bg-card) 100%
  );
  background-size: 200% 100%;
  animation: fxShimmer 1.2s ease-in-out infinite;
}

@keyframes fxShimmer {
  0% { background-position: 100% 0; }
  100% { background-position: -100% 0; }
}

.page-revalidating {
  position: relative;
}

.page-revalidating::before {
  content: "";
  position: absolute;
  inset: 0 0 auto 0;
  height: 2px;
  background: linear-gradient(90deg, transparent, var(--accent-blue), transparent);
  background-size: 200% 100%;
  animation: fxSweep 1.2s linear infinite;
  pointer-events: none;
  z-index: var(--z-dropdown);
}

@keyframes fxSweep {
  0% { background-position: 100% 0; }
  100% { background-position: -100% 0; }
}

@media (prefers-reduced-motion: reduce) {
  .page-skeleton .skeleton-title,
  .page-skeleton .skeleton-card,
  .page-revalidating::before {
    animation: none;
  }
}


/* ------------------------------------------------------------------------ *
 * 8. 侧栏 / 顶栏视觉一致性
 * ------------------------------------------------------------------------ */
.sidebar-overlay {
  z-index: calc(var(--z-sidebar) - 1);
}

/* 移动端菜单按钮和顶栏对齐 */
.top-bar #menuToggle,
.top-bar button {
  display: inline-flex;
  align-items: center;
  justify-content: center;
  height: var(--control-height);
  width: var(--control-height);
  border-radius: var(--radius-sm);
}


/* ------------------------------------------------------------------------ *
 * 9. AI 识别面板进度反馈 — 与 js/ai.js 的 callAIStream 配套
 * ------------------------------------------------------------------------ */
.ai-loading {
  display: flex;
  flex-direction: column;
  align-items: center;
  justify-content: center;
  gap: var(--space-3);
  padding: var(--space-6) var(--space-4);
  min-height: 160px;
}

.ai-loading .pulse-dot,
.ai-loading .spinner {
  flex-shrink: 0;
}

#aiRecognizeStatusText,
#salesAIStatusText,
#refundAIStatusText,
#aiAdviceProgress {
  font-variant-numeric: tabular-nums;
  color: var(--text-secondary);
  font-size: 13px;
  line-height: 1.4;
  text-align: center;
  word-break: keep-all;
  overflow-wrap: anywhere;
}


/* ------------------------------------------------------------------------ *
 * 10. 通用图像防 CLS
 * ------------------------------------------------------------------------ */
.product-card img,
.record-image,
.purchase-record-card img,
.sale-record-card img {
  aspect-ratio: 4 / 3;
  object-fit: cover;
  width: 100%;
  height: auto;
  background: var(--bg-card);
  border-radius: var(--radius-sm);
}


/* ------------------------------------------------------------------------ *
 * 11. 细节抛光
 * ------------------------------------------------------------------------ */
/* 去掉移动端双击放大延迟, 让按钮点击更灵敏 */
button,
.btn,
.nav-item {
  touch-action: manipulation;
}

/* 侧栏激活态对齐, 消除轻微错位 */
.sidebar .nav-item {
  display: flex;
  align-items: center;
  gap: var(--space-3);
  min-height: var(--control-height-lg);
  min-width: 0;
}

.sidebar .nav-item > span:not(.nav-icon) {
  min-width: 0;
  overflow: hidden;
  text-overflow: ellipsis;
  white-space: nowrap;
}

/* 登录页密码切换按钮与输入对齐(覆盖 main CSS 的零散 override) */
.login-form .password-input-wrap .password-toggle {
  right: 6px;
}

/* 修复移动端顶栏遮住 sticky filter-bar 的情况 */
@media (max-width: 768px) {
  .filter-bar {
    position: sticky;
    top: 0;
    z-index: calc(var(--z-topbar) - 1);
    background: var(--bg-primary);
  }
}


/* ------------------------------------------------------------------------ *
 * 12. 黑暗模式下 skeleton 的可见度保底
 * ------------------------------------------------------------------------ */
body:not(.light-mode) .page-skeleton .skeleton-title,
body:not(.light-mode) .page-skeleton .skeleton-card {
  background: linear-gradient(
    90deg,
    rgba(255, 255, 255, 0.04) 0%,
    rgba(255, 255, 255, 0.08) 50%,
    rgba(255, 255, 255, 0.04) 100%
  );
  background-size: 200% 100%;
}

body.light-mode .page-skeleton .skeleton-title,
body.light-mode .page-skeleton .skeleton-card {
  background: linear-gradient(
    90deg,
    rgba(0, 0, 0, 0.05) 0%,
    rgba(0, 0, 0, 0.09) 50%,
    rgba(0, 0, 0, 0.05) 100%
  );
  background-size: 200% 100%;
}
