:root {
  --primary: #2563eb;
  --primary-light: #dbeafe;
  --primary-dark: #1d4ed8;
  --bg: #f8fafc;
  --card-bg: #ffffff;
  --text: #1e293b;
  --text-secondary: #64748b;
  --text-light: #94a3b8;
  --border: #e2e8f0;
  --success: #22c55e;
  --warning: #f59e0b;
  --danger: #ef4444;
  --sidebar-bg: #1e293b;
  --sidebar-text: #cbd5e1;
  --sidebar-active: #2563eb;
  --shadow: 0 1px 3px rgba(0,0,0,0.08);
  --shadow-lg: 0 4px 12px rgba(0,0,0,0.1);
  --radius: 10px;
  --radius-sm: 6px;
}
* { margin: 0; padding: 0; box-sizing: border-box; }
body {
  font-family: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  background: var(--bg); color: var(--text); font-size: 14px; line-height: 1.6;
}
.app { display: flex; min-height: 100vh; }

/* 侧边栏 */
.sidebar {
  width: 220px; background: var(--sidebar-bg); color: var(--sidebar-text);
  display: flex; flex-direction: column; flex-shrink: 0;
}
.logo { display: flex; align-items: center; gap: 10px; padding: 18px 20px; font-size: 18px; font-weight: 700; color: #fff; border-bottom: 1px solid rgba(255,255,255,0.08); }
.sidebar-close { display: none; }
.logo-icon { font-size: 22px; }
.nav { flex: 1; padding: 12px 0; }
.nav-item {
  display: flex; align-items: center; gap: 10px; padding: 11px 20px; color: var(--sidebar-text);
  text-decoration: none; font-size: 14px; border-left: 3px solid transparent; transition: .15s;
}
.nav-item:hover { background: rgba(255,255,255,0.06); color: #fff; }
.nav-item.active { background: rgba(37,99,235,0.18); color: #fff; border-left-color: var(--sidebar-active); }
.nav-ico { font-size: 16px; }
.nav-logout { margin-top: 6px; border-top: 1px solid rgba(255,255,255,0.08); color: #fca5a5; }
.nav-logout:hover { background: rgba(220,38,38,0.16); color: #fff; }
.nav-sync { margin-top: 6px; border-top: 1px solid rgba(255,255,255,0.08); color: #7dd3fc; }
.nav-sync:hover { background: rgba(56,189,248,0.16); color: #fff; }
.sidebar-foot { padding: 14px 16px; border-top: 1px solid rgba(255,255,255,0.08); }

/* 主区域 */
.main { flex: 1; display: flex; flex-direction: column; min-width: 0; }
.topbar { display: flex; align-items: center; justify-content: space-between; padding: 0 24px; height: 60px; background: var(--card-bg); border-bottom: 1px solid var(--border); }
.topbar-left { display: flex; align-items: center; gap: 12px; }
.topbar h1 { font-size: 18px; font-weight: 600; }
.menu-btn { display: none; background: none; border: none; font-size: 20px; cursor: pointer; color: var(--text); }
.today { color: var(--text-secondary); font-size: 13px; }
.content { padding: 24px; flex: 1; overflow-y: auto; }

/* 卡片 */
.card { background: var(--card-bg); border: 1px solid var(--border); border-radius: var(--radius); box-shadow: var(--shadow); }
.section-title { font-size: 15px; font-weight: 600; margin: 4px 0 14px; display: flex; align-items: center; gap: 8px; }
.muted { color: var(--text-secondary); }
.row { display: flex; gap: 16px; flex-wrap: wrap; }
.grid-stats { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; margin-bottom: 20px; }
.stat { padding: 18px; }
.stat .stat-label { color: var(--text-secondary); font-size: 13px; }
.stat .stat-value { font-size: 26px; font-weight: 700; margin-top: 6px; }
.stat .stat-sub { font-size: 12px; color: var(--text-light); margin-top: 4px; }
.stat-accent { border-top: 3px solid var(--primary); }
.stat-warn { border-top: 3px solid var(--warning); }
.stat-ok { border-top: 3px solid var(--success); }
.stat-info { border-top: 3px solid #8b5cf6; }

/* 按钮 */
.btn { display: inline-flex; align-items: center; gap: 6px; padding: 8px 14px; border: 1px solid var(--border); background: #fff; color: var(--text); border-radius: var(--radius-sm); cursor: pointer; font-size: 13px; transition: .15s; text-decoration: none; }
.btn:hover { border-color: var(--primary); color: var(--primary); }
.btn-primary { background: var(--primary); color: #fff; border-color: var(--primary); }
.btn-primary:hover { background: var(--primary-dark); color: #fff; }
.btn-danger { color: var(--danger); border-color: #fecaca; }
.btn-danger:hover { background: var(--danger); color: #fff; border-color: var(--danger); }
.btn-sm { padding: 5px 10px; font-size: 12px; }
.btn-ghost { background: transparent; border-color: transparent; color: var(--text-secondary); }
.btn-ghost:hover { background: var(--bg); color: var(--text); }
.toolbar { display: flex; gap: 10px; align-items: center; flex-wrap: wrap; margin-bottom: 16px; }
.spacer { flex: 1; }

/* 筛选区标签（如"仅看低库存"） */
.filter-label {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 13px; color: var(--text-secondary);
  white-space: nowrap; flex-shrink: 0;
}
.filter-label input[type="checkbox"] { flex-shrink: 0; }

/* 表格 */
.table-wrap { overflow-x: auto; }
table { width: 100%; border-collapse: collapse; font-size: 13px; }
th, td { padding: 11px 12px; text-align: left; border-bottom: 1px solid var(--border); white-space: nowrap; }
th { background: #f1f5f9; color: var(--text-secondary); font-weight: 600; position: sticky; top: 0; }
tbody tr:hover { background: #f8fafc; }
td.wrap, th.wrap { white-space: normal; }
.tag { display: inline-block; padding: 2px 8px; border-radius: 20px; font-size: 12px; background: var(--primary-light); color: var(--primary-dark); }
.tag-warn { background: #fef3c7; color: #b45309; }
.tag-ok { background: #dcfce7; color: #15803d; }
.tag-gray { background: #f1f5f9; color: var(--text-secondary); }
.tag-blue { background: #dbeafe; color: #1d4ed8; }
.tag-orange { background: #ffedd5; color: #c2410c; }
.low-stock { color: var(--danger); font-weight: 700; }
.empty { text-align: center; color: var(--text-light); padding: 40px 0; }

/* 表单 */
.field { margin-bottom: 14px; }
.field label { display: block; font-size: 13px; color: var(--text-secondary); margin-bottom: 6px; }
.field .req { color: var(--danger); }
input, select, textarea {
  width: 100%; padding: 9px 11px; border: 1px solid var(--border); border-radius: var(--radius-sm);
  font-size: 14px; font-family: inherit; background: #fff; color: var(--text);
}
input:focus, select:focus, textarea:focus { outline: none; border-color: var(--primary); box-shadow: 0 0 0 3px var(--primary-light); }
textarea { resize: vertical; min-height: 64px; }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 14px; }
.form-actions { display: flex; gap: 10px; justify-content: flex-end; margin-top: 8px; }
.chk-group { display: flex; flex-wrap: wrap; gap: 8px 16px; padding: 10px 12px; border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--bg); }
.chk { display: inline-flex; align-items: center; gap: 6px; font-size: 14px; color: var(--text); cursor: pointer; }
.chk input { width: auto; margin: 0; }
.hint { font-size: 12px; color: var(--text-light); margin-top: 6px; }
.scan-result { margin-top: 10px; padding: 12px; border: 1px solid var(--border); border-radius: var(--radius-sm); background: var(--bg); font-size: 15px; line-height: 1.5; text-align: center; }
.scan-confirm-row { display: flex; gap: 10px; margin-top: 12px; }
.scan-confirm-row .btn { flex: 1; justify-content: center; }
.inline-search { display: flex; gap: 8px; align-items: center; }
.inline-search input { width: 240px; }

/* 弹窗 */
.modal-mask { position: fixed; inset: 0; background: rgba(15,23,42,0.5); display: none; align-items: center; justify-content: center; z-index: 100; padding: 20px; }
.modal-mask.show { display: flex; }
.modal { background: #fff; border-radius: var(--radius); width: 560px; max-width: 100%; max-height: 90vh; display: flex; flex-direction: column; box-shadow: var(--shadow-lg); }
.modal-sm { width: 420px; }
.modal-head { display: flex; align-items: center; justify-content: space-between; padding: 16px 20px; border-bottom: 1px solid var(--border); }
.modal-head h3 { font-size: 16px; }
.modal-close { background: none; border: none; font-size: 18px; cursor: pointer; color: var(--text-secondary); min-width: 36px; min-height: 36px; display: flex; align-items: center; justify-content: center; }
.modal-body { padding: 20px; overflow-y: auto; }
.modal-foot { padding: 14px 20px; border-top: 1px solid var(--border); display: flex; gap: 10px; justify-content: flex-end; }

/* 徽章 */
.badge { display: inline-block; padding: 3px 10px; border-radius: 20px; font-size: 12px; font-weight: 500; }
.badge-on { background: #dcfce7; color: #15803d; }
.badge-off { background: #fee2e2; color: #b91c1c; }

/* 提示 */
.toast { position: fixed; bottom: 30px; left: 50%; transform: translateX(-50%) translateY(20px); background: #1e293b; color: #fff; padding: 12px 22px; border-radius: 8px; font-size: 14px; opacity: 0; pointer-events: none; transition: .25s; z-index: 200; box-shadow: var(--shadow-lg); }
.toast.show { opacity: 1; transform: translateX(-50%) translateY(0); }
.toast.ok { background: #15803d; }
.toast.err { background: #b91c1c; }

/* 预警列表 */
.alert-item { display: flex; align-items: center; justify-content: space-between; padding: 12px 16px; border: 1px solid var(--border); border-radius: var(--radius-sm); margin-bottom: 10px; }
.alert-item .ai-name { font-weight: 600; }
.alert-item .ai-stock { color: var(--danger); font-weight: 700; }

/* 报告 */
.report-filters { display: flex; gap: 12px; flex-wrap: wrap; align-items: flex-end; margin-bottom: 16px; }
.report-filters .field { margin-bottom: 0; }
.kpi-row { display: grid; grid-template-columns: repeat(3,1fr); gap: 16px; margin-bottom: 20px; }

/* 设置 */
.setting-line { display: flex; align-items: center; justify-content: space-between; padding: 14px 0; border-bottom: 1px solid var(--border); }
.setting-line:last-child { border-bottom: none; }

/* ===================== 移动端优化（手机显示与操作） ===================== */

/* 1) 基础布局：900px 以下侧边栏改抽屉式 */
@media (max-width: 900px) {
  .grid-stats { grid-template-columns: repeat(2,1fr); }
  .kpi-row { grid-template-columns: 1fr; }
  .form-grid { grid-template-columns: 1fr; }
  .topbar { position: relative; z-index: 60; }
  .sidebar {
    position: fixed; top: 60px; left: 0; z-index: 50;
    height: calc(100% - 60px);
    transform: translateX(-100%); transition: .25s ease;
    width: 264px;
  }
  .sidebar.open { transform: translateX(0); box-shadow: 4px 0 20px rgba(0,0,0,0.3); }
  /* 侧边栏关闭按钮（仅移动端显示） */
  .sidebar-close {
    display: flex; align-items: center; justify-content: center;
    margin-left: auto; width: 40px; height: 40px; border-radius: 8px;
    background: rgba(255,255,255,0.12); border: none; color: #fff;
    font-size: 18px; cursor: pointer;
  }
  .sidebar-close:active { background: rgba(255,255,255,0.25); }
  /* 侧边栏遮罩 */
  .sidebar-backdrop {
    display: none; position: fixed; inset: 0; background: rgba(0,0,0,0.4);
    z-index: 49; transition: .25s;
  }
  .sidebar-backdrop.show { display: block; }
  .menu-btn { display: flex; align-items: center; justify-content: center;
    width: 42px; height: 42px; border-radius: var(--radius-sm); font-size: 20px;
    background: var(--bg); border: 1px solid var(--border); cursor: pointer; color: var(--text); }
  .menu-btn:active { background: var(--border); }
  .content { padding: 14px; }
  .topbar { padding: 0 12px; }
  .topbar h1 { font-size: 15px; }
  /* 统计卡片 */
  .stat { padding: 14px; }
  .stat .stat-value { font-size: 22px; }
  /* 弹窗 */
  .modal { width: 96vw; max-height: 85vh; margin: 0 auto; }
  .modal-sm { width: 94vw; }
  .modal-body { padding: 16px; }
  .modal-head { padding: 14px 16px; }
  .modal-foot { padding: 12px 16px; flex-wrap: wrap; }
  /* 工具栏 */
  .toolbar { gap: 8px; }
  .toolbar .btn { font-size: 13px; padding: 9px 12px; }
  .inline-search input { width: 100%; flex: 1; min-width: 0; }
  .filter-label { font-size: 13px; white-space: normal; line-height: 1.4; }
  .report-filters { gap: 8px; }
  .form-actions { flex-wrap: wrap; }
  /* 输入框/下拉 16px：修复 iOS 聚焦时页面被自动放大 */
  input, select, textarea { font-size: 16px; padding: 11px 12px; }
  /* 触控目标加大，手机更好点 */
  .nav-item { min-height: 46px; padding-top: 13px; padding-bottom: 13px; font-size: 15px; }
  .btn { min-height: 44px; padding: 10px 16px; font-size: 14px; justify-content: center; }
  .btn-sm { min-height: 38px; }
}

/* 2) 600px 以下：单列统计卡 + 顶栏精简 */
@media (max-width: 600px) {
  .grid-stats { grid-template-columns: 1fr; }
  .grid-stats .stat { padding: 12px; }
  .stat .stat-value { font-size: 21px; }
  .content { padding: 10px; }
  .card { border-radius: 8px; }
  /* 顶栏隐藏日期，避免与标题/退出按钮挤在一起 */
  .today { display: none; }
  .topbar-right { gap: 6px; }
  .topbar-right .user-tag { font-size: 12px; max-width: 84px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
  .topbar h1 { font-size: 16px; }
}

/* 3) 700px 以下：弹窗全屏 + 表格卡片化 */
@media (max-width: 700px) {
  /* 弹窗转为全屏：键盘不被遮挡、更好操作 */
  .modal-mask { padding: 0; }
  .modal { width: 100vw; max-width: 100%; height: 100vh; max-height: 100vh; border-radius: 0; }
  .modal-head { position: sticky; top: 0; background: #fff; z-index: 2; }
  .modal-body { flex: 1; }
  .modal-foot { position: sticky; bottom: 0; background: #fff; flex-wrap: wrap; }
  .modal-foot .btn { flex: 1; min-width: 120px; }
  #scanMask .modal { width: 100vw; height: 100vh; }
  #scanReader { min-height: 62vh; }

  /* 表格卡片化：每条记录变成一张卡片，带列名标签，无需左右滑动 */
  .table-wrap { margin: 0; padding: 0; overflow: visible; }
  table { min-width: 0 !important; display: block; }
  thead { position: absolute; width: 1px; height: 1px; overflow: hidden; clip: rect(0 0 0 0); }
  tbody { display: block; }
  tr { display: block; background: #fff; border: 1px solid var(--border); border-radius: var(--radius); margin-bottom: 12px; padding: 4px 12px; box-shadow: var(--shadow); }
  tbody tr:hover { background: #fff; }
  td { display: flex; justify-content: space-between; align-items: center; gap: 12px; padding: 10px 2px; border-bottom: 1px solid var(--border); white-space: normal; text-align: right; }
  td:last-child { border-bottom: none; }
  td::before { content: attr(data-label); font-weight: 600; color: var(--text-secondary); font-size: 13px; text-align: left; flex-shrink: 0; }
  td[data-label=""]::before { content: none; }
  /* 表格内的操作按钮加大、可横向排列 */
  td .btn, td button { min-height: 40px; padding: 8px 12px; margin: 2px 0; }
}

/* 4) 刘海屏 / 圆角屏安全区适配 */
@supports (padding: env(safe-area-inset-top)) {
  .topbar { padding-top: max(0px, env(safe-area-inset-top)); min-height: 60px; height: auto;
            padding-left: max(12px, env(safe-area-inset-left)); padding-right: max(12px, env(safe-area-inset-right)); }
  .sidebar { top: calc(60px + env(safe-area-inset-top)); height: calc(100% - 60px - env(safe-area-inset-top) - env(safe-area-inset-bottom)); }
  .toast { bottom: calc(30px + env(safe-area-inset-bottom)); }
  .modal-foot { padding-bottom: max(12px, env(safe-area-inset-bottom)); }
}

.user-tag { font-size: 13px; color: var(--text-secondary); margin-right: 4px; }

/* 登录 / 初始化 */
.auth-mask { position: fixed; inset: 0; background: linear-gradient(135deg,#1e293b,#334155); display: none; align-items: center; justify-content: center; z-index: 300; }
.auth-mask.show { display: flex; }
.auth-card { background: #fff; border-radius: 14px; padding: 32px; width: 360px; max-width: 92vw; box-shadow: 0 20px 60px rgba(0,0,0,0.35); }
@media (max-width: 500px) {
  .auth-card { padding: 24px 18px; width: 94vw; }
  .auth-card h2 { font-size: 16px; }
}
.auth-card h2 { font-size: 18px; margin-bottom: 16px; }
.auth-logo { font-size: 22px; font-weight: 800; color: var(--primary); text-align: center; margin-bottom: 18px; }
.auth-card .btn { margin-top: 6px; }
.role-badge { display:inline-block; padding:2px 8px; border-radius:20px; font-size:12px; background:var(--primary-light); color:var(--primary-dark); }

/* 打印（报表） */
@media print {
  .sidebar, .topbar, .toolbar, .modal-mask, .toast { display: none !important; }
  .app { display: block; }
  .content { padding: 0; }
  .card { box-shadow: none; border: 1px solid #ccc; break-inside: avoid; }
  body { background: #fff; }
}

/* 库存查询 SN 模糊搜索下拉 */
.sn-suggest {
  position: absolute; top: calc(100% + 4px); left: 0; right: 0; z-index: 60;
  background: var(--card-bg); border: 1px solid var(--border); border-radius: var(--radius-sm);
  box-shadow: 0 8px 24px rgba(0,0,0,.14); max-height: 280px; overflow: auto;
}
.sn-suggest .opt { padding: 8px 11px; cursor: pointer; font-size: 13px; border-bottom: 1px solid var(--border); white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sn-suggest .opt:last-child { border-bottom: none; }
.sn-suggest .opt:hover { background: var(--bg); }
.sn-suggest .opt .sn { font-family: ui-monospace, Menlo, Consolas, monospace; font-weight: 600; }
.sn-suggest .opt .meta { color: var(--text-secondary); font-size: 12px; margin-left: 8px; }

/* 产品/备件页 · 搜索框与自动补全下拉 */
.fkw-wrap { position: relative; display: inline-block; }
.fkw-suggest { min-width: 320px; left: auto; right: 0; max-width: 420px; }
.sn-suggest .opt .sku-line { font-weight: 600; font-family: ui-monospace, Menlo, Consolas, monospace; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.sn-suggest .opt .meta-line { color: var(--text-secondary); font-size: 12px; margin-top: 2px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
mark.kw-hl { background: #fde68a; color: inherit; padding: 0 1px; border-radius: 3px; }

/* 库存查询明细列（防溢出） */
.inv-detail-cell {
  max-width: 260px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap;
}
.inv-detail-cell:hover { overflow: visible; white-space: normal; word-break: break-all; }

/* 库存查询 · 库位列（让"货在哪"一眼可见） */
.inv-loc-cell { max-width: 140px; overflow: hidden; text-overflow: ellipsis; white-space: nowrap; }
.inv-loc-cell:hover { overflow: visible; white-space: normal; word-break: break-all; }
.loc-mark {
  display: inline-block; padding: 2px 8px; border-radius: 6px;
  background: #eef6ff; color: var(--primary-dark);
  font-size: 12px; font-weight: 600; letter-spacing: .2px;
  font-family: ui-monospace, Menlo, Consolas, monospace;
  white-space: nowrap;
}

/* 库存查询 · 每个 SN 一行：序号徽标 / SN 等宽字体 / 同物品续行淡底 */
.seq-badge {
  display: inline-block; padding: 1px 6px; border-radius: 99px;
  background: #f1f5f9; color: var(--text-secondary);
  font-size: 11px; font-weight: 600; font-family: ui-monospace, Menlo, Consolas, monospace;
  vertical-align: middle;
}
.sn-code { font-family: ui-monospace, Menlo, Consolas, monospace; font-weight: 600; color: var(--text); }
tr.inv-sn-cont > td { background: #fcfdfe; }

/* 入库 · 逐台库位（每个 SN 一行：SN + 库位输入） */
.sn-loc-box { margin-top: 10px; border: 1px solid var(--border); border-radius: var(--radius-sm); overflow: hidden; }
.sn-loc-head { padding: 8px 12px; background: #f1f5f9; font-size: 12px; font-weight: 600; color: var(--text-secondary); }
.sn-loc-grid { max-height: 240px; overflow-y: auto; padding: 6px 12px 2px; }
.sn-loc-row { display: flex; align-items: center; gap: 10px; padding: 4px 0; }
.sn-loc-row .sn-loc-sn {
  flex: 0 0 auto; min-width: 150px; max-width: 220px; overflow: hidden; text-overflow: ellipsis;
  font-family: ui-monospace, Menlo, Consolas, monospace; font-size: 12px; font-weight: 600;
}
.sn-loc-row input { flex: 1; min-width: 90px; }
.sn-loc-box .hint { padding: 0 12px 8px; }
@media (max-width: 500px) {
  .sn-loc-row { flex-wrap: wrap; gap: 4px 8px; }
  .sn-loc-row .sn-loc-sn { min-width: 0; max-width: 100%; flex: 1 1 100%; }
}

/* 同步结果明细 */
.sync-pre { margin: 0; white-space: pre-wrap; word-break: break-word; font-size: 13px; line-height: 1.7; color: var(--text); }
