:root {
  --bg: #f2f4f7; --panel: #ffffff; --ink: #1f2733; --muted: #8a929e;
  --line: #e6e9ee; --brand: #0ca678; --brand-hover: #0a8c67; --brand-soft: #e8f7f1;
  --good: #0ca678; --warn: #d97706; --bad: #e5484d; --shade: #f7f9fb;
  --side-bg: #ffffff; --side-line: #edf0f3;
  --shadow: 0 1px 2px rgba(16,24,40,.04), 0 6px 20px rgba(16,24,40,.05);
}
* { box-sizing: border-box; }
body {
  margin: 0; font: 14px/1.5 -apple-system, BlinkMacSystemFont, "PingFang SC", "Microsoft YaHei", system-ui, sans-serif;
  color: var(--ink); background: var(--bg);
}
.app { display: grid; grid-template-columns: 280px 1fr; min-height: 100vh; }

/* sidebar（浅色）*/
.side { background: var(--side-bg); color: #48505b; padding: 16px 12px; display: flex; flex-direction: column; border-right: 1px solid var(--side-line); }
.side h1 { font-size: 16px; color: var(--ink); margin: 4px 8px 2px; letter-spacing: .3px; }
.side .sub { font-size: 12px; color: var(--muted); margin: 0 8px 16px; }

/* 分组导航菜单 */
.menu { flex: 1; overflow: auto; }
.menu .group { margin-bottom: 8px; }
.menu .group-title { font-size: 11px; color: #8a929e; padding: 10px 12px 5px; letter-spacing: .5px; display: flex; align-items: center; justify-content: space-between; cursor: pointer; user-select: none; border-radius: 6px; }
.menu .group-title:hover { color: var(--brand); }
.menu .group-title .chev { font-size: 11px; color: #c2c8d0; }
.menu .group.collapsed .group-items { display: none; }
.menu .item { display: flex; align-items: center; gap: 8px; padding: 9px 12px; border-radius: 8px; cursor: pointer; color: #4a525d; font-size: 14px; }
.menu .item:hover { background: var(--shade); }
.menu .item.active { background: var(--brand-soft); color: var(--brand); font-weight: 600; box-shadow: inset 3px 0 0 var(--brand); }
.menu .item .badge { margin-left: auto; font-size: 11px; background: #eef1f4; color: #6b7686; border-radius: 999px; padding: 0 7px; min-width: 18px; text-align: center; }
.menu .item.active .badge { background: var(--brand); color: #fff; }
.menu .item.new { background: var(--brand); color: #fff; font-weight: 600; justify-content: center; margin-bottom: 4px; }
.menu .item.new:hover { background: var(--brand-hover); }
.menu .sep { height: 1px; background: var(--side-line); margin: 10px 6px; }

/* 列表视图 */
.list-head { display: flex; align-items: baseline; gap: 10px; margin-bottom: 14px; }
.list-head h2 { margin: 0; font-size: 18px; }
.list-count { color: var(--muted); font-size: 13px; }
.list-table { background: var(--panel); border: 1px solid var(--line); border-radius: 12px; overflow: hidden; box-shadow: var(--shadow); }
.list-table th { background: var(--shade); }
.list-table tbody tr { cursor: pointer; }
.list-table tbody tr:hover { background: var(--brand-soft); }
.list-table .empty td { text-align: center; color: var(--muted); padding: 26px; cursor: default; }
.list-table .st { font-size: 12px; padding: 2px 9px; border-radius: 999px; }
.list-table .st.draft { background: #eef2ff; color: #3730a3; }
.list-table .st.submitted { background: #fff3e0; color: #b45309; }
.list-table .st.approved { background: #e7f7ee; color: #15803d; }
.list-table .st.delivered { background: #e0f2fe; color: #0369a1; }
.perms-note { font-size: 13px; color: var(--muted); background: var(--shade); padding: 10px 12px; border-radius: 9px; margin: 0 0 14px; }
#perms-table td.no { color: #cbd5e1; }
#perms-table td.yes { color: var(--good); font-weight: 700; }
.side .new { width: 100%; margin-bottom: 14px; }
.hist { list-style: none; padding: 0; margin: 0; }
.hist li { padding: 9px 10px; border-radius: 8px; cursor: pointer; margin-bottom: 4px; border: 1px solid transparent; }
.hist li:hover { background: #1e293b; }
.hist li.active { background: #1e293b; border-color: #334155; }
.hist .t { color: #e2e8f0; font-weight: 600; display: flex; justify-content: space-between; gap: 6px; }
.hist .tag { font-size: 11px; padding: 1px 7px; border-radius: 999px; background: #334155; color: #cbd5e1; }
.hist .tag.HK { background: #134e4a; color: #5eead4; }
.hist .tag.US { background: #3b2f63; color: #c4b5fd; }
.hist .m { color: #94a3b8; font-size: 12px; margin-top: 2px; }
.hist .empty { color: #64748b; font-size: 12px; padding: 8px 10px; }

/* main */
.main { padding: 22px 28px 60px; overflow: auto; }
.topbar { display: flex; align-items: center; gap: 14px; margin-bottom: 18px; flex-wrap: wrap; }
.brandtitle { font-size: 18px; font-weight: 700; }
.brandtitle small { display: block; font-size: 12px; color: var(--muted); font-weight: 500; }
.market-picker { display: inline-flex; align-items: center; gap: 8px; }
.market-picker label { font-size: 12px; color: var(--muted); }
.market-picker select { width: auto; padding: 8px 16px; font-weight: 600; color: var(--brand); border-color: #dbe2ff; background: var(--brand-soft); }
.seg { display: inline-flex; background: var(--brand-soft); border-radius: 10px; padding: 3px; }
.seg button { border: 0; background: transparent; padding: 7px 16px; border-radius: 8px; cursor: pointer; font-weight: 600; color: var(--brand); }
.seg button.on { background: var(--brand); color: #fff; }
.spacer { flex: 1; }

.card { background: var(--panel); border: 1px solid var(--line); border-radius: 14px; box-shadow: var(--shadow); padding: 18px 20px; margin-bottom: 18px; }
.card.doc-title { padding: 10px 18px; }
.card.doc-title input { border: 0; font-size: 16px; font-weight: 600; padding: 4px 2px; }
.card.doc-title input:focus { box-shadow: none; }
.card h2 { font-size: 14px; margin: 0 0 14px; color: var(--brand); display: flex; align-items: center; gap: 8px; }
.card h2 .star { color: var(--warn); }
.card h2 .inline-field { display: inline-flex; align-items: center; gap: 8px; font-size: 13px; color: #fff; font-weight: 600; background: var(--brand); padding: 4px 6px 4px 14px; border-radius: 999px; margin-left: 4px; }
.card h2 .inline-field select.mini { width: auto; padding: 4px 12px; font-size: 13px; font-weight: 700; color: var(--brand); background: #fff; border: 0; border-radius: 999px; }
.grid { display: grid; grid-template-columns: repeat(2, minmax(0,1fr)); gap: 12px 22px; }
.grid.three { grid-template-columns: repeat(3, minmax(0,1fr)); }
.field { display: flex; flex-direction: column; gap: 5px; }
.field label { font-size: 12px; color: var(--muted); }
.field .hint { font-size: 11px; color: #9aa4b2; }
input, select { font: inherit; padding: 9px 11px; border: 1px solid var(--line); border-radius: 9px; background: #fff; color: var(--ink); width: 100%; }
input:focus, select:focus { outline: none; border-color: var(--brand); box-shadow: 0 0 0 3px var(--brand-soft); }
input[readonly] { background: var(--shade); color: var(--muted); }

.kpis { display: grid; grid-template-columns: repeat(4, 1fr); gap: 12px; }
.kpi { background: var(--shade); border: 1px solid var(--line); border-radius: 11px; padding: 12px 14px; }
.kpi .k { font-size: 12px; color: var(--muted); }
.kpi .v { font-size: 19px; font-weight: 700; margin-top: 3px; }
.kpi.accent { background: linear-gradient(135deg, #eef2ff, #f5f3ff); border-color: #dbe2ff; }

table { width: 100%; border-collapse: collapse; }
th, td { padding: 10px 12px; border-bottom: 1px solid var(--line); text-align: left; font-size: 13px; }
th { color: #5b6472; font-weight: 600; background: var(--shade); font-size: 12px; }
/* 数据表格：网格线（STAVIA 风格）*/
.list-table th, .list-table td, .cust-table th, .cust-table td, .perms-matrix th, .perms-matrix td {
  border-right: 1px solid var(--line);
}
.list-table th:last-child, .list-table td:last-child, .cust-table th:last-child, .cust-table td:last-child,
.perms-matrix th:last-child, .perms-matrix td:last-child { border-right: 0; }
td input { padding: 6px 8px; }
td.num, th.num { text-align: right; }
.rowtools button { border: 0; background: transparent; color: var(--bad); cursor: pointer; font-size: 16px; }
tfoot td { font-weight: 700; background: var(--shade); }
.addrow { margin-top: 10px; }

.btn { border: 1px solid var(--line); background: #fff; color: var(--ink); padding: 9px 16px; border-radius: 9px; cursor: pointer; font-weight: 600; }
.btn:hover { background: var(--shade); }
.btn.primary { background: var(--brand); color: #fff; border-color: var(--brand); }
.btn.primary:hover { background: var(--brand-hover); border-color: var(--brand-hover); }
.btn.ghost { background: transparent; }
.btn.danger { color: var(--bad); border-color: #f0c5b8; }
.actions { display: flex; gap: 10px; align-items: center; }

.banner { padding: 9px 12px; border-radius: 9px; font-size: 13px; margin-bottom: 12px; display: none; }
.banner.show { display: block; }
.banner.ok { background: #e7f7ee; color: var(--good); }
.banner.err { background: #fde8e2; color: var(--bad); }
.match { font-size: 12px; padding: 3px 9px; border-radius: 999px; }
.match.ok { background: #e7f7ee; color: var(--good); }
.match.no { background: #fff3e0; color: var(--warn); }
.unit { color: var(--muted); font-weight: 500; font-size: 13px; }
.toast { position: fixed; right: 22px; bottom: 22px; background: #0f172a; color: #fff; padding: 11px 16px; border-radius: 10px; box-shadow: var(--shadow); opacity: 0; transform: translateY(8px); transition: .2s; pointer-events: none; z-index: 50; }
.toast.show { opacity: 1; transform: none; }

/* 登录 */
.login { position: fixed; inset: 0; display: flex; align-items: center; justify-content: center; background: linear-gradient(135deg, #0f172a, #1e293b); }
.login-card { background: #fff; padding: 30px 32px; border-radius: 16px; width: 340px; box-shadow: 0 20px 60px rgba(0,0,0,.35); }
.login-card h1 { margin: 0 0 2px; font-size: 20px; }
.login-card .sub { margin: 0 0 20px; color: var(--muted); font-size: 13px; }
.login-card label { display: block; font-size: 12px; color: var(--muted); margin: 12px 0 5px; }
.login-err { color: var(--bad); font-size: 13px; min-height: 18px; margin-top: 8px; }

/* 侧栏筛选 + 用户盒 */
.filters { margin-bottom: 10px; }
.userbox { margin-top: 16px; padding-top: 14px; border-top: 1px solid var(--side-line); }
.userbox .me { display: flex; align-items: center; gap: 8px; margin-bottom: 8px; }
.userbox .me #me-name { color: var(--ink); font-weight: 600; }
.role-tag { font-size: 11px; padding: 1px 8px; border-radius: 999px; background: var(--brand-soft); color: var(--brand); }
.ulinks { display: flex; gap: 14px; flex-wrap: wrap; }
.ulinks a { color: var(--muted); font-size: 12px; cursor: pointer; text-decoration: none; }
.ulinks a:hover { color: var(--brand); text-decoration: underline; }

/* 状态徽章 */
.status-badge { font-size: 12px; padding: 3px 11px; border-radius: 999px; font-weight: 600; }
.status-badge.draft { background: #eef2ff; color: #3730a3; }
.status-badge.submitted { background: #fff3e0; color: #b45309; }
.status-badge.approved { background: #e7f7ee; color: #15803d; }
.status-badge.delivered { background: #e0f2fe; color: #0369a1; }

/* 工作流按钮 */
.btn.wf { background: #fff; }
.btn.approve { border-color: #86efac; color: #15803d; }
.btn.deliver { border-color: #7dd3fc; color: #0369a1; }
.btn.return { border-color: #fca5a5; color: var(--bad); }

/* 派生字段覆盖标记 */
.derived-field label { display: flex; align-items: center; gap: 6px; }
.reset { display: none; border: 1px solid var(--line); background: #fff; color: var(--brand); font-size: 10px; padding: 1px 6px; border-radius: 6px; cursor: pointer; margin-left: auto; }
.derived-field.manual .reset { display: inline-block; }
input.derived.manual { border-color: var(--warn); background: #fffbeb; }
td input.manual { border-color: var(--warn); background: #fffbeb; }

/* 只读锁定（仅作用于详情表单）*/
.app.locked #detail-view input, .app.locked #detail-view select { background: var(--shade); pointer-events: none; color: var(--muted); }
.app.locked #detail-view .reset, .app.locked #detail-view .rowtools, .app.locked #detail-view #btn-addrow { display: none !important; }

/* 权限矩阵 */
.perms-matrix th, .perms-matrix td.ck { text-align: center; }
.perms-matrix td.yes { color: var(--good); font-weight: 700; }
.perms-matrix input[type=checkbox] { width: 17px; height: 17px; cursor: pointer; }

/* 审计 */
.audit { list-style: none; padding: 0; margin: 0; }
.audit li { padding: 7px 0; border-bottom: 1px solid var(--line); font-size: 13px; color: var(--ink); display: flex; gap: 10px; }
.audit .who { font-weight: 600; }
.audit .when { color: var(--muted); margin-left: auto; font-size: 12px; }
.audit .act { color: var(--brand); }

/* 弹窗 */
.modal { position: fixed; inset: 0; background: rgba(15,23,42,.5); display: flex; align-items: center; justify-content: center; z-index: 40; }
.modal-card { background: #fff; border-radius: 14px; padding: 20px 22px; width: 720px; max-width: 92vw; max-height: 86vh; overflow: auto; box-shadow: var(--shadow); }
.modal-head { display: flex; align-items: center; margin-bottom: 12px; }
.modal-head h2 { margin: 0; color: var(--brand); font-size: 16px; }
.modal-head .x { margin-left: auto; border: 0; background: transparent; font-size: 18px; cursor: pointer; color: var(--muted); }
.utable td, .utable th { font-size: 13px; }
.newuser { margin-top: 18px; border-top: 1px solid var(--line); padding-top: 14px; }
.newuser h3 { font-size: 13px; margin: 0 0 10px; }
.newuser-row { display: flex; gap: 8px; flex-wrap: wrap; }
.newuser-row input, .newuser-row select { width: auto; flex: 1; min-width: 110px; }
.cust-table-wrap { max-height: 52vh; overflow: auto; margin-top: 12px; border: 1px solid var(--line); border-radius: 10px; }
.cust-table-wrap table { width: 100%; }
.cust-table-wrap thead th { position: sticky; top: 0; z-index: 1; }
.cust-table-wrap td input { width: 100%; padding: 6px 8px; }

/* 客户管理：功能键在最上、筛选其次、表格在下 */
.cust-toolbar { display: flex; gap: 8px; margin-bottom: 12px; }
.cust-toolbar #cust-edit-toggle.on { background: var(--brand); color: #fff; border-color: var(--brand); }
.cust-filters { display: flex; gap: 10px; }
.cust-filters input { max-width: 260px; }
/* 操作列默认隐藏，点「操作」进入编辑态才显示 */
.cust-table .op-col, .cust-table td.op { display: none; }
#customers-view.editing .cust-table .op-col, #customers-view.editing .cust-table td.op { display: table-cell; }
.cust-table td { vertical-align: middle; }
