:root {
  --primary: #2563eb;
  --primary-dark: #1d4ed8;
  --bg: #f4f6fa;
  --card: #fff;
  --text: #1e293b;
  --muted: #64748b;
  --border: #e2e8f0;
  --danger: #dc2626;
  --ok: #16a34a;
  --warn: #d97706;
  --radius: 10px;
}
* { box-sizing: border-box; margin: 0; padding: 0; }
body { font-family: -apple-system, "PingFang SC", "Microsoft YaHei", sans-serif; background: var(--bg); color: var(--text); font-size: 15px; }
.hidden { display: none !important; }

/* ---------- 登录 ---------- */
.login-wrap { min-height: 100vh; min-height: 100dvh; display: flex; align-items: center; justify-content: center;
  padding: calc(20px + env(safe-area-inset-top)) 20px calc(20px + env(safe-area-inset-bottom));
  background: linear-gradient(135deg, #1e3a8a, #2563eb); }
.login-card { background: #fff; padding: 36px 30px; border-radius: 16px; width: 100%; max-width: 360px; box-shadow: 0 20px 50px rgba(0,0,0,.25); }
.login-card h1 { font-size: 22px; text-align: center; }
.login-card .sub { text-align: center; color: var(--muted); margin: 8px 0 24px; font-size: 13px; }
.login-card input { display: block; width: 100%; margin-bottom: 12px; }
.remember { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); margin: -4px 0 14px; cursor: pointer; }
.remember input { width: auto; margin: 0; }
.err { color: var(--danger); font-size: 13px; margin-top: 10px; min-height: 1em; }

/* ---------- 布局 ---------- */
#app { display: flex; min-height: 100vh; min-height: 100dvh; }
.sidebar { width: 200px; background: #f5f6f8; color: #334155; display: flex; flex-direction: column; position: fixed; inset: 0 auto 0 0; border-right: 1px solid #e2e8f0; }
.brand { padding: 16px 18px 14px; display: flex; align-items: center; justify-content: flex-start; }
.brand-logo { max-width: 158px; width: auto; height: auto; max-height: 52px; display: block; }
#side-nav { flex: 1; padding: 6px 10px; }
#side-nav a, #bottom-nav a { display: flex; align-items: center; gap: 10px; padding: 11px 12px; border-radius: 8px; color: inherit; text-decoration: none; font-size: 14px; }
#side-nav a:hover { background: #e2e8f0; }
#side-nav a.active { background: var(--primary); color: #fff; }
.sidebar-foot { padding: 12px 14px; border-top: 1px solid #e2e8f0; font-size: 13px; display: flex; align-items: center; justify-content: space-between; gap: 8px; }
#main { flex: 1; margin-left: 200px; padding: 20px 22px 80px; max-width: 1100px; }
#bottom-nav { display: none; }

#company-bar { position: fixed; top: 0; left: 200px; right: 0; z-index: 60; display: flex; align-items: center; gap: 10px;
  padding: calc(10px + env(safe-area-inset-top)) 22px 10px; background: rgba(255,255,255,.92); backdrop-filter: blur(6px); border-bottom: 1px solid var(--border); }
#company-bar .cb-label { font-size: 13px; color: var(--muted); }
#company-select { flex: 0 1 320px; font-weight: 600; }
#main { padding-top: calc(66px + env(safe-area-inset-top)); }

.page-title { font-size: 20px; font-weight: 700; margin-bottom: 16px; }
.toolbar { display: flex; gap: 10px; margin-bottom: 14px; flex-wrap: wrap; }

/* ---------- 卡片 / 表格 ---------- */
.card { background: var(--card); border-radius: var(--radius); padding: 16px; margin-bottom: 14px; box-shadow: 0 1px 3px rgba(0,0,0,.06); }
.stats { display: grid; grid-template-columns: repeat(auto-fill, minmax(150px, 1fr)); gap: 12px; margin-bottom: 16px; }
.stat { background: var(--card); border-radius: var(--radius); padding: 14px 16px; box-shadow: 0 1px 3px rgba(0,0,0,.06); }
.stat .v { font-size: 22px; font-weight: 700; margin-top: 4px; }
.stat .l { font-size: 12px; color: var(--muted); }
.table-wrap { overflow-x: auto; -webkit-overflow-scrolling: touch; }
table { width: 100%; border-collapse: collapse; font-size: 14px; background: var(--card); border-radius: var(--radius); overflow: hidden; }
.table-wrap > table { width: max-content; min-width: 100%; }
th { background: #f1f5f9; text-align: left; padding: 10px 12px; font-size: 13px; color: var(--muted); white-space: nowrap; }
td { padding: 10px 12px; border-top: 1px solid var(--border); vertical-align: top; }
tr.clickable:hover { background: #f8fafc; cursor: pointer; }
.badge { display: inline-block; padding: 2px 10px; border-radius: 20px; font-size: 12px; }
.badge.ok { background: #dcfce7; color: var(--ok); }
.badge.warn { background: #fef3c7; color: var(--warn); }
.badge.danger { background: #fee2e2; color: var(--danger); }
.low { color: var(--danger); font-weight: 700; }

/* ---------- 表单 ---------- */
input, select, textarea { padding: 9px 11px; border: 1px solid var(--border); border-radius: 8px; font-size: 14px; font-family: inherit; background: #fff; }
input:focus, select:focus { outline: 2px solid #bfdbfe; border-color: var(--primary); }
.field { display: flex; flex-direction: column; gap: 5px; margin-bottom: 12px; }
.field label { font-size: 13px; color: var(--muted); }
.form-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 0 14px; }
.item-row { display: grid; grid-template-columns: 1.4fr .8fr .8fr 32px; gap: 8px; margin-bottom: 8px; align-items: center; }
.bom-row { display: grid; grid-template-columns: 1.6fr .6fr 32px; gap: 8px; margin-bottom: 8px; align-items: center; }
.doc-row { display: grid; grid-template-columns: 1.2fr 1.1fr 1fr .9fr .55fr .5fr .7fr 32px; gap: 6px; margin-bottom: 8px; align-items: center; }
.doc-row input, .doc-row select { padding: 7px 8px; font-size: 13px; }
.ops-td { white-space: nowrap; }
.ops-td .btn { margin: 1px 2px; }
.total-line { text-align: right; font-weight: 700; padding: 10px 4px; }

.btn { padding: 9px 16px; border: none; border-radius: 8px; background: #e2e8f0; color: var(--text); font-size: 14px; cursor: pointer; font-family: inherit; }
.btn:hover { filter: brightness(.95); }
.btn.primary { background: var(--primary); color: #fff; }
.btn.danger { background: var(--danger); color: #fff; }
.btn.small { padding: 5px 10px; font-size: 12px; }
.btn.block { width: 100%; }
.icon-btn { border: none; background: none; font-size: 16px; cursor: pointer; color: var(--muted); }

/* ---------- 弹窗 / 提示 ---------- */
#modal-mask { position: fixed; inset: 0; background: rgba(15,23,42,.65); display: flex; align-items: center; justify-content: center; z-index: 1000; padding: 16px; }
.modal { background: #fff; border-radius: 14px; width: 100%; max-width: 640px; max-height: 88vh; overflow-y: auto; }
.modal-head { display: flex; justify-content: space-between; align-items: center; padding: 16px 18px; border-bottom: 1px solid var(--border); font-weight: 700; position: sticky; top: 0; background: #fff; z-index: 1; }
#modal-body { padding: 18px; }
#toast { position: fixed; top: 18px; left: 50%; transform: translateX(-50%); background: #0f172a; color: #fff; padding: 10px 20px; border-radius: 10px; font-size: 14px; z-index: 200; max-width: 90vw; }
#toast.error { background: var(--danger); }

/* ---------- 手机端 ---------- */
@media (max-width: 760px) {
  .sidebar { display: none; }
  #main { margin-left: 0; padding: calc(76px + env(safe-area-inset-top)) 12px calc(92px + env(safe-area-inset-bottom)); }
  #company-bar { left: 0; padding: calc(8px + env(safe-area-inset-top)) 12px 8px; }
  #company-select { flex: 1; }
  #bottom-nav { display: flex; position: fixed; bottom: 0; left: 0; right: 0; background: #fff; border-top: 1px solid var(--border);
    padding: 6px 4px calc(6px + env(safe-area-inset-bottom)); z-index: 1; overflow-x: auto; justify-content: flex-start;
    -webkit-overflow-scrolling: touch; scrollbar-width: none; }
  #bottom-nav::-webkit-scrollbar { display: none; }
  #bottom-nav a { flex: 0 0 auto; min-width: 74px; flex-direction: column; gap: 2px; font-size: 11px; color: var(--muted); padding: 6px 2px; justify-content: center; text-align: center; white-space: nowrap; }
  #bottom-nav a.active { color: var(--primary); background: none; }
  #bottom-nav .bn-refresh { color: var(--primary); border-left: 1px solid var(--border); margin-left: 3px; }
  #bottom-nav .bn-refresh:active { transform: scale(.92); }
  body.modal-open { overflow: hidden; }
  body.modal-open #bottom-nav,
  body.modal-open #ai-fab,
  body.modal-open #ai-panel { display: none !important; }
  #bottom-nav .ico { font-size: 19px; }
  .form-grid { grid-template-columns: 1fr; }
  .item-row { grid-template-columns: 1fr 1fr; }
  .bom-row { grid-template-columns: 1.8fr .8fr 32px; }
  .doc-row { grid-template-columns: 1fr; }
  .doc-row .do-name-en { display: none; }
  .stats { grid-template-columns: 1fr 1fr; }
  .page-title { font-size: 18px; }
  .toolbar .btn { flex: 1; }
  input, select, textarea { padding: 12px 13px; font-size: 16px; }
  .btn { padding: 12px 16px; }
  .item-row, .bom-row, .doc-row { gap: 10px; }
  .table-wrap table, table { font-size: 13px; }
  td, th { padding: 12px 10px; }
  #toast { top: calc(18px + env(safe-area-inset-top)); }
  .ai-panel { bottom: calc(150px + env(safe-area-inset-bottom)); }
  #main { overflow-x: hidden; }
  #modal-mask { padding: calc(16px + env(safe-area-inset-top)) 12px calc(16px + env(safe-area-inset-bottom)); }
  .modal { max-height: calc(100dvh - 32px); padding-bottom: env(safe-area-inset-bottom); }
}
.toolbar--sticky { position: sticky; top: calc(54px + env(safe-area-inset-top)); background: var(--bg); z-index: 5; padding-top: 4px; }

/* ---------- AI 助手 ---------- */
.ai-fab { position: fixed; right: 18px; bottom: calc(84px + env(safe-area-inset-bottom)); width: 56px; height: 56px; border-radius: 50%; border: none;
  background: var(--primary); color: #fff; font-size: 26px; cursor: pointer; box-shadow: 0 8px 22px rgba(37,99,235,.4); z-index: 80; }
.ai-fab:hover { filter: brightness(1.05); }
.ai-panel { position: fixed; right: 16px; bottom: 150px; width: 380px; max-width: calc(100vw - 24px); height: 72vh; max-height: 560px;
  background: #fff; border-radius: 14px; box-shadow: 0 16px 50px rgba(0,0,0,.25); display: flex; flex-direction: column; z-index: 90; overflow: hidden; }
.ai-head { display: flex; justify-content: space-between; align-items: center; padding: 12px 14px; background: var(--primary); color: #fff; font-weight: 700; }
.ai-msgs { flex: 1; overflow-y: auto; padding: 12px; display: flex; flex-direction: column; gap: 10px; background: #f8fafc; }
.ai-msg { max-width: 86%; padding: 9px 12px; border-radius: 12px; font-size: 14px; line-height: 1.5; white-space: pre-wrap; word-break: break-word; }
.ai-msg.user { align-self: flex-end; background: var(--primary); color: #fff; }
.ai-msg.bot { align-self: flex-start; background: #fff; border: 1px solid var(--border); }
.ai-input { display: flex; gap: 8px; padding: 10px; border-top: 1px solid var(--border); }
.ai-input input { flex: 1; }
.ai-mic { font-size: 20px; line-height: 1; background: #fff; border: 1px solid var(--border); border-radius: 50%; width: 38px; height: 38px; flex: 0 0 auto; }
.ai-mic.recording { background: #ef4444; border-color: #ef4444; color: #fff; animation: aimic 1s infinite; }
@keyframes aimic { 0%,100% { box-shadow: 0 0 0 0 rgba(239,68,68,.5); } 50% { box-shadow: 0 0 0 8px rgba(239,68,68,0); } }
.ai-cursor::after { content: '▋'; color: var(--primary); animation: aiblink 1s steps(1) infinite; }
@keyframes aiblink { 50% { opacity: 0; } }

/* ---------- 日志 ---------- */
.log-table .log-detail { font-size: 12px; color: var(--muted); max-width: 360px; word-break: break-all; }
