:root {
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --primary-light: #eff6ff;
  --success: #16a34a;
  --success-light: #dcfce7;
  --warning: #d97706;
  --warning-light: #fef3c7;
  --danger: #dc2626;
  --danger-light: #fee2e2;
  --ink: #1e293b;
  --muted: #64748b;
  --line: #e2e8f0;
  --bg: #f1f5f9;
  --card: #ffffff;
  --radius: 12px;
  --shadow: 0 1px 3px rgba(15, 23, 42, .08), 0 1px 2px rgba(15, 23, 42, .04);
}

* { box-sizing: border-box; margin: 0; padding: 0; }
html, body { height: 100%; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  background: var(--bg);
  color: var(--ink);
  font-size: 14px;
  line-height: 1.5;
}

/* 布局 */
.layout { display: flex; min-height: 100vh; }

/* 侧边栏 */
.sidebar {
  width: 230px;
  background: #fff;
  border-right: 1px solid var(--line);
  display: flex;
  flex-direction: column;
  position: fixed;
  top: 0; left: 0; bottom: 0;
  z-index: 40;
  transition: transform .25s ease;
}
.brand {
  padding: 18px 20px;
  font-size: 17px;
  font-weight: 700;
  color: var(--primary);
  border-bottom: 1px solid var(--line);
  display: flex; align-items: center; gap: 8px;
}
.brand .logo { width: 30px; height: 30px; border-radius: 8px; background: linear-gradient(135deg, #2563eb, #06b6d4); display: flex; align-items: center; justify-content: center; color: #fff; font-size: 16px; }
.nav { padding: 10px; flex: 1; overflow-y: auto; }
.nav-item {
  display: flex; align-items: center; gap: 10px;
  padding: 11px 14px; margin: 4px 0;
  border-radius: 10px; cursor: pointer;
  color: var(--muted); font-weight: 500;
  transition: background .15s, color .15s;
}
.nav-item:hover { background: var(--primary-light); color: var(--primary); }
.nav-item.active { background: var(--primary); color: #fff; }
.nav-item .ico { font-size: 16px; width: 20px; text-align: center; }
.nav-section { padding: 14px 14px 4px; font-size: 11px; font-weight: 700; color: #94a3b8; letter-spacing: .5px; text-transform: uppercase; }

/* 主区域 */
.main { flex: 1; margin-left: 230px; display: flex; flex-direction: column; min-width: 0; }
.topbar {
  height: 60px; background: #fff; border-bottom: 1px solid var(--line);
  display: flex; align-items: center; justify-content: space-between;
  padding: 0 22px; position: sticky; top: 0; z-index: 30;
}
.topbar h1 { font-size: 18px; font-weight: 700; }
.topbar .sub { color: var(--muted); font-size: 13px; }
.menu-btn { display: none; background: none; border: none; font-size: 22px; cursor: pointer; color: var(--ink); }

.content { padding: 22px; }
.page-head { display: flex; align-items: center; justify-content: space-between; margin-bottom: 18px; flex-wrap: wrap; gap: 12px; }
.page-head h2 { font-size: 20px; }
.page-head .desc { color: var(--muted); font-size: 13px; margin-top: 2px; }

/* 卡片 */
.card { background: var(--card); border: 1px solid var(--line); border-radius: var(--radius); box-shadow: var(--shadow); }
.card-pad { padding: 18px; }

/* 统计卡 */
.stat-grid { display: grid; grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 16px; margin-bottom: 20px; }
.stat {
  background: var(--card); border: 1px solid var(--line); border-radius: var(--radius);
  padding: 18px; box-shadow: var(--shadow); position: relative; overflow: hidden;
}
.stat .label { color: var(--muted); font-size: 13px; }
.stat .value { font-size: 26px; font-weight: 700; margin-top: 6px; }
.stat .icon { position: absolute; right: 14px; top: 14px; font-size: 26px; opacity: .15; }
.stat.blue .value { color: var(--primary); }
.stat.green .value { color: var(--success); }
.stat.orange .value { color: var(--warning); }
.stat.red .value { color: var(--danger); }

/* 表格 */
.table-wrap { overflow-x: auto; border-radius: var(--radius); }
table { width: 100%; border-collapse: collapse; font-size: 13.5px; }
th, td { padding: 11px 12px; text-align: left; border-bottom: 1px solid var(--line); white-space: nowrap; }
th { background: #f8fafc; color: var(--muted); font-weight: 600; position: sticky; top: 0; }
tbody tr:hover { background: #f8fafc; }
td.num, th.num { text-align: right; font-variant-numeric: tabular-nums; }

/* 按钮 */
.btn {
  display: inline-flex; align-items: center; gap: 6px;
  padding: 9px 16px; border-radius: 9px; border: 1px solid transparent;
  font-size: 13.5px; font-weight: 600; cursor: pointer; transition: all .15s;
  background: var(--primary); color: #fff; white-space: nowrap;
}
.btn:hover { background: var(--primary-dark); }
.btn.secondary { background: #fff; color: var(--ink); border-color: var(--line); }
.btn.secondary:hover { background: #f8fafc; }
.btn.ghost { background: transparent; color: var(--primary); }
.btn.sm { padding: 6px 11px; font-size: 12.5px; }
.btn.danger { background: var(--danger); }
.btn.danger:hover { background: #b91c1c; }
.btn.warn { background: var(--warning); }
.btn.success { background: var(--success); }
.btn:disabled { opacity: .5; cursor: not-allowed; }

/* 徽标 */
.badge { display: inline-block; padding: 3px 10px; border-radius: 999px; font-size: 12px; font-weight: 600; }
.badge.ok { background: var(--success-light); color: var(--success); }
.badge.low { background: var(--danger-light); color: var(--danger); }
.badge.draft { background: #e2e8f0; color: #475569; }
.badge.pending { background: var(--warning-light); color: var(--warning); }
.badge.approved { background: var(--primary-light); color: var(--primary); }
.badge.received { background: var(--success-light); color: var(--success); }
.badge.done { background: var(--success-light); color: var(--success); }
.badge.green { background: var(--success-light); color: var(--success); }
.badge.red { background: var(--danger-light); color: var(--danger); }
.badge.orange { background: var(--warning-light); color: var(--warning); }

/* 表单 / 弹窗 */
.modal-mask { position: fixed; inset: 0; background: rgba(15, 23, 42, .45); display: none; align-items: flex-start; justify-content: center; z-index: 100; padding: 30px 14px; overflow-y: auto; }
.modal-mask.show { display: flex; }
.modal { background: #fff; border-radius: 14px; width: 100%; max-width: 560px; box-shadow: 0 20px 50px rgba(0,0,0,.25); animation: pop .2s ease; }
.modal.wide { max-width: 720px; }
@keyframes pop { from { transform: translateY(12px) scale(.98); opacity: 0; } to { transform: none; opacity: 1; } }
.modal-head { padding: 16px 20px; border-bottom: 1px solid var(--line); display: flex; justify-content: space-between; align-items: center; }
.modal-head h3 { font-size: 16px; }
.modal-close { background: none; border: none; font-size: 22px; cursor: pointer; color: var(--muted); line-height: 1; }
.modal-body { padding: 20px; max-height: 70vh; overflow-y: auto; }
.modal-foot { padding: 14px 20px; border-top: 1px solid var(--line); display: flex; justify-content: flex-end; gap: 10px; }

.field { margin-bottom: 14px; }
.field label { display: block; font-size: 13px; color: var(--muted); margin-bottom: 5px; font-weight: 600; }
.field input, .field select, .field textarea {
  width: 100%; padding: 9px 11px; border: 1px solid var(--line); border-radius: 9px;
  font-size: 14px; font-family: inherit; color: var(--ink); background: #fff;
}
.field input:focus, .field select:focus, .field textarea:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-light); }
.field-row { display: flex; gap: 12px; }
.field-row .field { flex: 1; }
.grid2 { display: grid; grid-template-columns: 1fr 1fr; gap: 0 14px; }

/* 明细行 */
.items-editor { border: 1px solid var(--line); border-radius: 10px; padding: 12px; background: #f8fafc; }
.item-row { display: grid; grid-template-columns: 2fr 1fr 1fr 36px; gap: 8px; margin-bottom: 8px; align-items: center; }
.item-row input, .item-row select { padding: 7px 9px; font-size: 13px; }
.item-del { background: var(--danger-light); border: none; color: var(--danger); border-radius: 8px; height: 34px; cursor: pointer; font-size: 16px; }
.add-item { margin-top: 4px; }

/* 退货明细行（含批次选择） */
.ret-row { display: flex; gap: 8px; align-items: center; margin-bottom: 6px; flex-wrap: wrap; }
.ret-row select, .ret-row input { flex: 1 1 120px; }
.ret-row .r-product { flex: 2 1 160px; }

/* FIFO 批次分解面板 */
.fifo-panel { background: var(--bg-soft); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; margin-bottom: 10px; }
.fifo-item { font-size: 13px; padding: 3px 0; border-bottom: 1px dashed var(--line); }
.fifo-item:last-child { border-bottom: none; }
.batch-chip { display: inline-block; background: #eef2ff; color: #334155; border: 1px solid #c7d2fe; border-radius: 999px; padding: 1px 9px; margin: 2px 4px 2px 0; font-size: 12px; }

/* Tabs */
.tabs { display: flex; gap: 8px; margin-bottom: 14px; flex-wrap: wrap; }
.tab { padding: 8px 16px; border: 1px solid var(--line); background: #fff; border-radius: 999px; cursor: pointer; font-size: 13px; }
.tab.active { background: var(--primary); color: #fff; border-color: var(--primary); }
.fee-row { display: grid; grid-template-columns: 2fr 1fr 36px; gap: 8px; margin-bottom: 8px; align-items: center; }
.fee-row input { padding: 7px 9px; font-size: 13px; }

/* BOM 组件编辑器 */
.bom-items { border: 1px solid var(--line); border-radius: 10px; padding: 12px; background: #f8fafc; }

/* 组装单组件表 */
.asm-table { width: 100%; border-collapse: collapse; margin-bottom: 8px; }
.asm-table th, .asm-table td { border: 1px solid var(--line); padding: 7px 9px; font-size: 13px; text-align: left; }
.asm-table th { background: #f8fafc; color: var(--muted); font-weight: 600; }
.asm-table .a-prod { min-width: 150px; }
.asm-table input { width: 90px; padding: 6px 8px; font-size: 13px; }
.asm-table .a-need, .asm-table .a-uc, .asm-table .a-amt { font-variant-numeric: tabular-nums; }
.asm-summary { margin-top: 8px; }
.sum-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 10px; }
.sum-grid > div { background: var(--bg); border: 1px solid var(--line); border-radius: 10px; padding: 10px 12px; }
.sum-grid span { font-size: 12px; color: var(--muted); }
.sum-grid b { display: block; font-size: 16px; margin-top: 4px; color: var(--ink); font-variant-numeric: tabular-nums; }
@media (max-width: 860px) { .sum-grid { grid-template-columns: 1fr 1fr; } }

/* 提示 */
.toast { position: fixed; bottom: 26px; left: 50%; transform: translateX(-50%) translateY(20px); background: #0f172a; color: #fff; padding: 12px 20px; border-radius: 10px; font-size: 14px; opacity: 0; transition: all .25s; z-index: 200; pointer-events: none; }
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.ok { background: #16a34a; }
.toast.err { background: #dc2626; }

.empty { text-align: center; color: var(--muted); padding: 40px 10px; }
.muted { color: var(--muted); }
.right { text-align: right; }
.mt { margin-top: 16px; }
.flex { display: flex; gap: 10px; align-items: center; }
.flex.wrap { flex-wrap: wrap; }
.spacer { flex: 1; }

/* 移动端 */
@media (max-width: 860px) {
  .sidebar { transform: translateX(-100%); width: 220px; box-shadow: 0 0 30px rgba(0,0,0,.2); }
  .sidebar.open { transform: translateX(0); }
  .main { margin-left: 0; }
  .menu-btn { display: block; }
  .mask-bg { position: fixed; inset: 0; background: rgba(0,0,0,.3); z-index: 35; display: none; }
  .mask-bg.show { display: block; }
  .content { padding: 14px; }
  .page-head h2 { font-size: 18px; }
  /* 移动端表格转卡片 */
  .responsive-table table, .responsive-table thead, .responsive-table tbody, .responsive-table th, .responsive-table td, .responsive-table tr { display: block; }
  .responsive-table thead { display: none; }
  .responsive-table tr { border: 1px solid var(--line); border-radius: 10px; margin-bottom: 12px; padding: 6px 10px; background: #fff; }
  .responsive-table td { border: none; padding: 8px 4px; white-space: normal; display: flex; justify-content: space-between; gap: 12px; }
  .responsive-table td::before { content: attr(data-label); color: var(--muted); font-weight: 600; flex-shrink: 0; }
  .responsive-table td.num { text-align: right; }
  .grid2 { grid-template-columns: 1fr; }
  .item-row { grid-template-columns: 1fr 1fr; }
  .item-row .item-del { grid-column: span 2; }

  /* 顶栏自适应：允许换行、隐藏次要信息，防止文字被挤压成竖排覆盖 */
  .topbar { height: auto; min-height: 54px; flex-wrap: wrap; padding: 8px 12px; row-gap: 8px; }
  .topbar > .flex { flex-wrap: wrap; gap: 6px; row-gap: 6px; min-width: 0; }
  .topbar > .flex:first-child { flex: 1 1 auto; }
  .topbar > .flex:first-child > div { min-width: 0; overflow: hidden; }
  .topbar h1 { font-size: 16px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
  #clock { display: none; }
  .user-chip { padding: 4px; }
  .user-chip .u-meta { display: none; }   /* 手机端只留头像，姓名/角色不占位 */
  #companySel { flex: 1 1 120px; max-width: 200px; min-width: 0; }
  .topbar .btn { flex-shrink: 0; padding: 6px 10px; }
}

/* ============ 登录 ============ */
.login-mask { position: fixed; inset: 0; z-index: 200; display: flex; align-items: center; justify-content: center;
  background: linear-gradient(135deg, #eef2ff 0%, #f7f9fc 45%, #eaf3ff 100%); padding: 16px; }
.login-mask.hide { display: none; }
.login-box { width: 100%; max-width: 380px; background: #fff; border: 1px solid var(--line); border-radius: 16px;
  box-shadow: 0 18px 50px rgba(30,41,59,.14); padding: 28px 26px 22px; }
.login-brand { display: flex; align-items: center; gap: 10px; margin-bottom: 18px; }
.login-brand .logo { font-size: 30px; }
.login-brand h2 { font-size: 19px; margin: 0; }
.login-box .field { margin-bottom: 14px; }
.login-box input { width: 100%; }
.login-btn { width: 100%; padding: 11px; font-size: 15px; margin-top: 4px; }
.login-box .err { color: var(--danger); font-size: 13px; min-height: 20px; text-align: center; }
.center { text-align: center; }

/* ============ 顶栏用户 ============ */
.user-chip { display: flex; align-items: center; gap: 8px; padding: 4px 10px 4px 4px; border: 1px solid var(--line);
  border-radius: 999px; background: #fff; }
.user-chip .avatar { width: 30px; height: 30px; border-radius: 50%; background: var(--primary); color: #fff;
  display: flex; align-items: center; justify-content: center; font-weight: 700; font-size: 13px; flex-shrink: 0; }
.user-chip .u-meta { display: flex; flex-direction: column; line-height: 1.25; }
.user-chip .u-meta b { font-size: 13px; }
.user-chip .u-meta i { font-style: normal; font-size: 11px; color: var(--muted); }

/* ============ 权限矩阵 ============ */
.perm-grid { display: grid; gap: 8px; max-height: 320px; overflow: auto; padding-right: 4px; }
.perm-mod { border: 1px solid var(--line); border-radius: 10px; padding: 8px 10px; background: #fff; }
.perm-mod > label { display: flex; align-items: center; gap: 8px; font-weight: 600; font-size: 13px; cursor: pointer; }
.perm-acts { display: flex; flex-wrap: wrap; gap: 10px 16px; margin-top: 8px; padding-left: 4px; }
.perm-acts label { display: flex; align-items: center; gap: 5px; font-size: 13px; color: var(--ink); cursor: pointer; font-weight: 400; }
.role-desc { font-size: 12px; color: var(--muted); margin-top: 2px; }
