/* ===== 颜色与尺寸 token：浅色 / 深色 ===== */
:root {
  --bg: #e9edf2;
  --surface: #ffffff;
  --surface-2: #f8f9fb;
  --border: #c5ced8;
  --border-strong: #aebbc8;
  --text: #223247;
  --muted: #58728e;
  --faint: #8a93a2;
  --accent: #1976d2;
  --accent-soft: #e4f0fc;
  --accent-text: #ffffff;
  --ok: #0b8f46;
  --warn: #9a6700;
  --warn-soft: #fff4d6;
  --danger: #cf222e;
  --danger-soft: #ffebe9;
  --auto-bg: #f1f3f6;
  --weekend-bg: #fbf6ec;
  --sel-bg: rgba(31, 111, 235, .12);
  --shadow: 0 8px 28px rgba(15, 23, 42, .16);
  --radius: 6px;
  --font: -apple-system, BlinkMacSystemFont, "Segoe UI", "PingFang SC", "Microsoft YaHei", sans-serif;
  --mono: ui-monospace, SFMono-Regular, Menlo, Consolas, monospace;
  color-scheme: light;
}

* { box-sizing: border-box; }
html, body, #app { margin: 0; height: 100%; }
body { background: var(--bg); color: var(--text); font: 14px/1.5 var(--font); }
button, input, select, textarea { font: inherit; color: inherit; }
a { color: var(--accent); text-decoration: none; }
a:hover { text-decoration: underline; }
.boot { padding: 48px; color: var(--muted); text-align: center; }
.muted { color: var(--muted); }
.faint { color: var(--faint); }
.hidden { display: none !important; }
.spacer { flex: 1; }

/* ===== 按钮与表单 ===== */
.btn {
  display: inline-flex; align-items: center; gap: 6px; height: 32px; padding: 0 12px;
  border: 1px solid var(--border-strong); border-radius: var(--radius); background: var(--surface);
  cursor: pointer; white-space: nowrap;
}
.btn:hover { background: var(--surface-2); }
.btn:disabled { opacity: .5; cursor: not-allowed; }
.btn.primary { background: var(--accent); border-color: var(--accent); color: var(--accent-text); }
.btn.primary:hover { filter: brightness(1.06); }
.btn.danger { color: var(--danger); border-color: var(--danger); }
.btn.warn { color: var(--warn); border-color: var(--warn); }
.btn.ghost { border-color: transparent; background: transparent; }
.btn.ghost:hover { background: var(--surface-2); }
.btn.small { height: 26px; padding: 0 8px; font-size: 13px; }
.input, select.input {
  height: 32px; padding: 0 10px; border: 1px solid var(--border-strong); border-radius: var(--radius);
  background: var(--surface); min-width: 0;
}
.input:focus, .btn:focus-visible, select:focus { outline: 2px solid var(--accent); outline-offset: 1px; }
.field { display: flex; flex-direction: column; gap: 4px; margin-bottom: 12px; }
.field > span { font-size: 13px; color: var(--muted); }
.row { display: flex; align-items: center; gap: 8px; flex-wrap: wrap; }
.error-text { color: var(--danger); font-size: 13px; min-height: 18px; }

.badge { display: inline-block; padding: 0 7px; border-radius: 10px; font-size: 12px; line-height: 20px;
  background: var(--surface-2); border: 1px solid var(--border); color: var(--muted); }
.badge.accent { background: var(--accent-soft); border-color: transparent; color: var(--accent); }
.badge.ok { background: #e7f6eb; border-color: transparent; color: var(--ok); }
.badge.warn { background: var(--warn-soft); border-color: transparent; color: var(--warn); }
.badge.danger { background: var(--danger-soft); border-color: transparent; color: var(--danger); }

/* ===== 页面框架 ===== */
.shell { display: flex; flex-direction: column; height: 100%; }
.topbar {
  display: flex; align-items: center; gap: 16px; height: 48px; padding: 0 16px; flex: none;
  background: #1f2d3d; border-bottom: 5px solid #1976d2; color: #eaf2fa;
}
.menu-toggle { flex: none; min-height: 36px; padding: 0 10px; white-space: nowrap; border: 1px solid #71859a; border-radius: 6px; background: #2c3e50; color: #fff; cursor: pointer; font-size: 14px; }
.menu-toggle:hover { background: #395269; }
.menu-toggle:focus-visible { outline: 2px solid #fff; outline-offset: 2px; }
.top-context { color: #eaf2fa; font-size: 13px; }
.topbar .muted { color: #c7d5e3; }
.topbar .btn.ghost { color: #eaf2fa; }
.main { flex: 1; min-height: 0; display: flex; }
.content { flex: 1; min-width: 0; overflow: auto; padding: 20px 24px; }
.page-title { display: flex; align-items: center; gap: 12px; margin: 0 0 16px; font-size: 18px; font-weight: 600; flex-wrap: wrap; }
.context-title h1 { margin: 0; font-size: inherit; }
.crumb { color: var(--muted); font-weight: 400; font-size: 14px; }

/* ===== 组织树侧栏 ===== */
.sidebar { width: 250px; flex: none; overflow: auto; background: #2c3e50; color: #eaf2fa; border-right: 1px solid #435a70; padding: 0 12px 16px; transition: width .15s ease, padding .15s ease; }
.sidebar[hidden], .menu-collapsed .sidebar { display: none; }
.menu-brand { display: flex; flex-direction: column; gap: 2px; padding: 18px 12px 16px; color: #fff; border-bottom: 1px solid #435a70; }
.menu-brand:hover { text-decoration: none; }
.menu-brand strong { font-size: 18px; letter-spacing: .5px; }
.menu-brand small { color: #aebfd0; font-size: 12px; }
.menu-label { margin: 18px 8px 6px; color: #aebfd0; font-size: 12px; }
.menu-item { display: block; padding: 10px 12px; border-left: 3px solid transparent; color: #eaf2fa; font-size: 15px; }
.menu-item:hover, .menu-item.active { background: #1f2d3d; color: #fff; text-decoration: none; }
.menu-item.active { border-left-color: #f4c542; font-weight: 600; }
.org-label { border-top: 1px solid #435a70; padding-top: 16px; }
.tree-group { margin-bottom: 8px; }
.tree-factory { padding: 4px 8px; color: #fff; font-weight: 600; font-size: 13px; }
.tree-line { padding: 2px 8px 2px 18px; color: #c3d0dc; font-size: 13px; }
.tree-team { display: flex; align-items: center; gap: 8px; min-height: 48px; margin: 5px 0; padding: 9px 10px 9px 16px; border: 1px solid #435a70; border-radius: 10px; background: #344b60; color: #f8f5f0; font-size: 16px; }
.tree-team:hover { background: #3d5870; text-decoration: none; }
.tree-team.active { background: #1f2d3d; color: #fff; font-weight: 600; }
.tree-team.filled { background: #0b8f46; border-color: #55bd7c; color: #fff; }
.tree-team.filled.active { background: #087438; }
.tree-team .team-status { margin-left: auto; font-size: 12px; color: #c3d0dc; }
.tree-team.filled .team-status { color: #9be7aa; font-weight: 600; }
.tree-team .badge { margin-left: 6px; }

/* ===== 报表卡片 ===== */
.cards { display: grid; grid-template-columns: repeat(auto-fill, minmax(220px, 1fr)); gap: 12px; }
.card {
  display: block; padding: 14px 16px; background: var(--surface); border: 1px solid var(--border);
  border-radius: 8px; color: var(--text);
}
.card:hover { border-color: var(--accent); text-decoration: none; }
.card h3 { margin: 0 0 6px; font-size: 15px; display: flex; align-items: center; gap: 8px; }
.card .meta { font-size: 12px; color: var(--muted); }
.card.filled { background: #e7f6eb; border-color: #63b878; }
.org-tree-node { display: block; }
.org-tree-node > summary { cursor: pointer; min-height: 44px; }
.org-tree-node > summary .org-count { margin-left: 8px; }
.org-team-node { padding: 8px 4px; border-top: 1px solid var(--border); }
.org-count { font-size: 12px; }
.org-card { min-height: 112px; padding: 20px; }
.org-card h2 { margin: 0 0 12px; font-size: 20px; }
.org-card.filled { background: #e7f6eb; border-color: #63b878; }
.org-card.filled .meta { color: var(--ok); font-weight: 600; }
.section-label { margin: 20px 0 8px; font-size: 13px; color: var(--muted); }
.metric-section, .loss-overview { margin-top: 18px; }
.metric-section h2, .loss-overview h2 { margin: 0 0 10px; font-size: 18px; }
.metric-primary { grid-template-columns: repeat(auto-fit, minmax(180px, 1fr)); gap: 10px; }
.metric-secondary { margin-top: 10px; grid-template-columns: repeat(auto-fit, minmax(200px, 1fr)); }
.metric-card { min-height: 86px; padding: 12px 14px; }
.metric-card h3 { margin-bottom: 4px; font-size: 14px; }
.metric-card p { margin: 0; font-size: 16px; font-weight: 600; }
.metric-card small { display: block; margin-top: 4px; color: var(--muted); font-size: 12px; }
.metric-detail { line-height: 1.35; }
.metric-more { margin-top: 12px; padding: 8px 10px; border: 1px solid var(--border); border-radius: var(--radius); background: var(--surface-2); }
.metric-more summary { cursor: pointer; color: var(--accent); font-weight: 600; }
.loss-overview .loss-total { border-color: #d49b4d; background: #fff8eb; }
.loss-reason { font-size: 13px !important; font-weight: 500 !important; margin: 2px 0 !important; }
.production-workspace { max-width: 1500px; }
.empty-state { padding: 16px; margin: 8px 0 12px; border: 1px dashed var(--border-strong); border-radius: var(--radius); background: var(--surface-2); }
.empty-state p { margin: 6px 0 10px; color: var(--muted); }
.scope-trail { margin: 8px 0 12px; color: var(--muted); font-weight: 600; }
.production-actions { position: sticky; bottom: 0; z-index: 5; padding: 10px; margin: 16px -10px 0; background: color-mix(in srgb, var(--surface) 92%, transparent); border-top: 1px solid var(--border); }

/* ===== 表格（管理页） ===== */
table.list { width: 100%; border-collapse: collapse; background: var(--surface); border: 1px solid var(--border); }
table.list th, table.list td { padding: 8px 10px; border-bottom: 1px solid var(--border); text-align: left; }
table.list th { background: var(--surface-2); font-weight: 600; font-size: 13px; color: var(--muted); }
table.list tr.inactive td { color: var(--faint); }

/* ===== 登录 ===== */
.login-wrap { display: grid; place-items: center; min-height: 100%; padding: 16px; }
.login-box { width: 100%; max-width: 360px; background: var(--surface); border: 1px solid var(--border); border-radius: 10px; padding: 28px; }
.login-box h1 { margin: 0 0 20px; font-size: 20px; }

/* ===== 对话框与提示 ===== */
.modal-mask { position: fixed; inset: 0; background: rgba(0, 0, 0, .35); display: grid; place-items: center; z-index: 50; padding: 16px; }
.modal { width: 100%; max-width: 520px; max-height: 90vh; display: flex; flex-direction: column; background: var(--surface);
  border-radius: 10px; box-shadow: var(--shadow); }
.modal.wide { max-width: 820px; }
.modal header { padding: 16px 20px 8px; font-size: 16px; font-weight: 600; }
.modal .body { padding: 8px 20px; overflow: auto; }
.modal footer { padding: 12px 20px 16px; display: flex; justify-content: flex-end; gap: 8px; }
#toasts { position: fixed; right: 16px; bottom: 16px; display: flex; flex-direction: column; gap: 8px; z-index: 60; }
.toast { white-space: pre-line; padding: 10px 14px; border-radius: var(--radius); background: var(--text); color: var(--bg); box-shadow: var(--shadow); max-width: 360px; }
.toast.error { background: var(--danger); color: #fff; }

@media (max-width: 760px) {
  .main { flex-direction: column; }
  .sidebar { width: auto; max-height: 32vh; border-right: none; border-bottom: 1px solid var(--border); overscroll-behavior: contain; }
  .tree-factory, .tree-line { min-height: 40px; display: flex; align-items: center; }
  .tree-team { padding-top: 8px; padding-bottom: 8px; }
  .content { padding: 14px 12px; }
  .topbar { gap: 8px; padding: 0 12px; }
  .nav { min-width: 0; overflow-x: auto; }
  .nav a { min-height: 40px; display: inline-flex; align-items: center; }
  .cards { grid-template-columns: repeat(auto-fit, minmax(min(100%, 240px), 1fr)); gap: 10px; }
  .card { padding: 16px; }
  table.list { display: block; max-width: 100%; overflow-x: auto; overscroll-behavior-x: contain; }
  table.list th, table.list td { white-space: nowrap; }
  .btn, .btn.small, .input, select.input { min-height: 40px; }
}

@media (max-width: 480px) {
  .top-context { display: none; }
  .content { padding: 12px 10px; }
  .cards { grid-template-columns: minmax(0, 1fr); }
  .page-title { gap: 8px; }
  .modal-mask { padding: 8px; }
  .modal header { padding: 14px 16px 8px; }
  .modal .body { padding: 8px 16px; }
  .modal footer { padding: 10px 16px 14px; }
}

@media (pointer: coarse) {
  .menu-toggle, .btn, .btn.small, .nav a, .tree-factory, .tree-line { min-height: 44px; }
}

/* 组织树里可点的产线 / 工厂（汇总入口） */
.tree-link { display: flex; align-items: center; justify-content: space-between; border-radius: var(--radius); color: var(--text); }
.tree-link:hover { background: var(--surface-2); text-decoration: none; }
.tree-link.active { background: var(--accent-soft); color: var(--accent); }
.tree-sum { font-size: 11px; font-weight: 400; color: var(--faint); border: 1px solid var(--border); border-radius: 8px; padding: 0 6px; }
.tree-link.tree-line { color: var(--muted); }
