:root {
  --navy: #1f3864;
  --navy-2: #2e5fa3;
  --blue: #2e6fd8;
  --green: #1e8e4e;
  --red: #c0392b;
  --orange: #b9770e;
  --bg: #eef1f5;
  --line: #d7dde5;
  --text: #2b3440;
  --muted: #7a8694;
}

* { box-sizing: border-box; margin: 0; padding: 0; }
[hidden] { display: none !important; }

body {
  font-family: "Segoe UI", "Microsoft YaHei", system-ui, sans-serif;
  background: var(--bg);
  color: var(--text);
  font-size: 14px;
}

/* ---------- 顶栏 ---------- */
.topbar {
  background: linear-gradient(120deg, #16294a 0%, #1f3864 55%, #2e5fa3 100%);
  color: #fff;
  padding: 14px 0;
  box-shadow: 0 2px 10px rgba(20, 40, 80, .25);
}
.topbar-inner {
  max-width: 1440px; margin: 0 auto; padding: 0 24px;
  display: flex; align-items: center; justify-content: space-between;
}
.brand { display: flex; align-items: center; gap: 14px; }
.brand-icon {
  width: 46px; height: 46px; border-radius: 10px;
  background: rgba(255, 255, 255, .14);
  border: 1px solid rgba(255, 255, 255, .3);
  display: flex; align-items: center; justify-content: center;
  font-size: 24px; font-weight: 700; font-family: Georgia, serif;
}
.brand h1 { font-size: 20px; font-weight: 700; letter-spacing: 1px; }
.brand p { font-size: 12px; color: #c6d4ec; margin-top: 3px; }

/* ---------- 按钮 ---------- */
.btn {
  border: 1px solid transparent; border-radius: 8px;
  padding: 9px 18px; font-size: 14px; cursor: pointer;
  transition: all .15s; font-family: inherit;
}
.btn:disabled { opacity: .45; cursor: not-allowed; }
.btn-sm { padding: 6px 12px; font-size: 13px; }
.btn-block { width: 100%; }
.btn-primary { background: var(--blue); color: #fff; }
.btn-primary:hover:not(:disabled) { background: #235bb8; }
.btn-success { background: var(--green); color: #fff; }
.btn-success:hover:not(:disabled) { background: #17723e; }
.btn-plain { background: #fff; border-color: var(--line); color: var(--text); }
.btn-plain:hover:not(:disabled) { border-color: var(--blue); color: var(--blue); }
.btn-plain.danger:hover { border-color: var(--red); color: var(--red); }
.btn-ghost {
  background: rgba(255,255,255,.12); color: #fff; text-decoration: none;
  border: 1px solid rgba(255,255,255,.35); border-radius: 8px; padding: 8px 16px; font-size: 13px;
}
.btn-ghost:hover { background: rgba(255,255,255,.22); }
.btn-link { background: none; border: none; color: var(--blue); cursor: pointer; font-size: 13px; padding: 4px; font-family: inherit; }
.btn-link:hover { text-decoration: underline; }
.btn-link.danger { color: var(--red); }

/* ---------- 布局：侧栏 + 内容 ---------- */
.container { max-width: 1440px; margin: 20px auto 0; padding: 0 24px 30px; }
.layout { display: grid; grid-template-columns: 330px minmax(0, 1fr); gap: 16px; align-items: start; }
.side {
  position: sticky; top: 12px;
  display: flex; flex-direction: column; gap: 16px;
  height: calc(100vh - 24px);
  overflow-y: auto;                 /* 视口不够高时侧栏内部滚动，内容绝不外溢 */
  scrollbar-width: thin;
  padding-right: 2px;
}
.side::-webkit-scrollbar { width: 6px; }
.side::-webkit-scrollbar-thumb { background: #c8d2de; border-radius: 3px; }
.side > .card:not(.side-lib) { flex: none; }
.side .card { padding: 14px 16px; }
.side .card-title { margin-bottom: 10px; }
.side-actions { margin-top: 10px; }
.content-col { min-width: 0; }

.card {
  background: #fff; border-radius: 12px; padding: 18px 20px;
  box-shadow: 0 1px 4px rgba(30, 50, 90, .08); border: 1px solid #e4e8ee;
  margin-bottom: 16px;
}
.side .card { margin-bottom: 0; }
.card-title {
  font-size: 15px; color: var(--navy); margin-bottom: 14px;
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
}
.card-title .spacer { flex: 1; }
.step-no {
  width: 22px; height: 22px; border-radius: 50%; background: var(--navy); color: #fff;
  font-size: 12px; display: inline-flex; align-items: center; justify-content: center; flex: none;
}
.card-tip { font-size: 12px; color: var(--muted); font-weight: 400; }
.badge {
  background: #e8eef8; color: var(--navy-2); font-size: 12px;
  border-radius: 10px; padding: 2px 10px; font-weight: 600;
}

/* ---------- 侧栏：预计条件 ---------- */
.side-cond { display: flex; flex-direction: column; }
.form-grid { display: grid; grid-template-columns: 1fr; gap: 8px; }
.form-grid label { font-size: 12.5px; color: var(--muted); display: flex; flex-direction: column; gap: 5px; }
.span2 { grid-column: 1 / -1; }
input[type="text"], input[type="number"], select {
  border: 1px solid var(--line); border-radius: 7px; padding: 7px 9px;
  font-size: 13px; font-family: inherit; color: var(--text); background: #fff; outline: none;
}
input:focus, select:focus { border-color: var(--blue); box-shadow: 0 0 0 3px rgba(46, 111, 216, .12); }
input:disabled, select:disabled { background: #f2f4f7; color: #9aa5b3; }
.env-desc { font-size: 11.5px; color: var(--muted); line-height: 1.45; margin-top: -2px; min-height: 15px; }
.method-note { font-size: 11.5px; color: var(--orange); background: #fff8ea; border-radius: 6px; padding: 6px 9px; }
.temp-row { display: flex; gap: 10px; align-items: center; }
.temp-row input[type="range"] { flex: 1; accent-color: var(--blue); }
.temp-row input[type="number"] { width: 72px; }
.side-actions { margin-top: 14px; display: flex; flex-direction: column; gap: 8px; }
.btn-row { display: flex; gap: 8px; }
.btn-row .btn { flex: 1; }

/* ---------- 侧栏：器件库 ---------- */
.side-lib { flex: 1 1 0; min-height: 160px; overflow: hidden; display: flex; flex-direction: column; }
.lib-search { margin-bottom: 10px; }
.lib-tree { flex: 1 1 auto; min-height: 60px; overflow-y: auto; padding-right: 4px; scrollbar-width: thin; }
.lib-tree::-webkit-scrollbar { width: 6px; }
.lib-tree::-webkit-scrollbar-thumb { background: #c8d2de; border-radius: 3px; }
.card-title .btn-sm { white-space: nowrap; }
.lib-group { margin-bottom: 10px; }
.lib-group-name {
  font-size: 11.5px; color: var(--muted); font-weight: 700; letter-spacing: 1px;
  padding: 4px 6px; border-bottom: 1px solid #eef1f5; margin-bottom: 4px;
}
.lib-item {
  display: flex; align-items: center; gap: 6px; padding: 5px 6px;
  border-radius: 7px; cursor: pointer; transition: background .1s;
}
.lib-item:hover { background: #eef4fc; }
.lib-name { flex: 1; font-size: 13px; white-space: nowrap; overflow: hidden; text-overflow: ellipsis; }
.lib-lb { font-size: 11px; color: var(--navy-2); font-variant-numeric: tabular-nums; background: #eef3fb; border-radius: 6px; padding: 1px 6px; }
.lib-add {
  border: 1px solid var(--line); background: #fff; color: var(--blue);
  width: 22px; height: 22px; border-radius: 6px; cursor: pointer; font-size: 13px; line-height: 1; flex: none;
}
.lib-item:hover .lib-add { background: var(--blue); color: #fff; border-color: var(--blue); }
.lib-empty { text-align: center; color: var(--muted); font-size: 12.5px; padding: 22px 0; }

/* ---------- 上传区（紧凑） ---------- */
.upload-card { padding: 14px 16px; }
.dropzone {
  border: 2px dashed #b9c6d8; border-radius: 10px; background: #f7fafd;
  padding: 26px 16px; text-align: center; cursor: pointer; transition: all .15s;
}
.dropzone.compact {
  display: flex; align-items: center; gap: 14px; text-align: left; padding: 13px 16px;
}
.dropzone:hover, .dropzone.drag { border-color: var(--blue); background: #eef4fc; }
.dz-icon { font-size: 34px; margin-bottom: 6px; }
.dz-icon-sm { font-size: 26px; }
.dz-main { flex: 1; font-size: 13.5px; }
.dz-sub { font-size: 12px; color: var(--muted); margin-top: 3px; }
.dz-links { display: flex; flex-direction: column; gap: 2px; }
.file-info {
  margin-top: 10px; background: #eef7f0; border: 1px solid #bfe3cb;
  border-radius: 8px; padding: 8px 12px; display: flex; align-items: center; gap: 10px; font-size: 13px;
}
.file-name { font-weight: 600; color: var(--green); overflow: hidden; text-overflow: ellipsis; white-space: nowrap; max-width: 220px; }
.file-meta { color: var(--muted); flex: 1; }

/* ---------- 表格 ---------- */
.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: 10px; }
.bom-table { width: 100%; border-collapse: collapse; min-width: 700px; }
.bom-table thead th {
  background: #f2f6fb; color: var(--navy); font-size: 12.5px; font-weight: 700;
  padding: 9px 10px; text-align: left; border-bottom: 2px solid #d9e2ef;
  position: sticky; top: 0; white-space: nowrap; z-index: 1;
}
.bom-table tbody td { padding: 5px 8px; border-bottom: 1px solid #ecf0f5; vertical-align: middle; }
.bom-table tbody tr:hover { background: #f7faff; }
.bom-table .seq { text-align: center; color: var(--muted); font-size: 12px; }
.bom-table td input, .bom-table td select {
  width: 100%; border: 1px solid transparent; background: transparent;
  padding: 6px 7px; border-radius: 6px; font-size: 13px;
}
.bom-table td input:hover, .bom-table td select:hover { border-color: var(--line); background: #fff; }
.bom-table td input:focus, .bom-table td select:focus { border-color: var(--blue); background: #fff; }
.bom-table td input[type="number"] { text-align: center; }
.row-del {
  background: none; border: none; color: #b6c0cc; cursor: pointer; font-size: 16px;
  width: 28px; height: 28px; border-radius: 6px;
}
.row-del:hover { color: var(--red); background: #fdeeec; }
.empty-hint { padding: 26px; text-align: center; color: var(--muted); font-size: 13px; }

/* ---------- 结果 ---------- */
.summary-grid { display: grid; grid-template-columns: repeat(4, 1fr); gap: 16px; margin-bottom: 16px; }
@media (max-width: 1200px) { .summary-grid { grid-template-columns: repeat(2, 1fr); } }
.stat-card {
  background: #fff; border-radius: 12px; padding: 16px 20px;
  border: 1px solid #e4e8ee; box-shadow: 0 1px 4px rgba(30, 50, 90, .08);
  border-top: 4px solid var(--navy-2);
}
.stat-card.main { border-top-color: var(--blue); background: linear-gradient(180deg, #f4f8ff, #fff); }
.stat-label { font-size: 12.5px; color: var(--muted); }
.stat-value { font-size: 30px; font-weight: 700; color: var(--navy); margin: 6px 0 2px; font-variant-numeric: tabular-nums; }
.stat-card.main .stat-value { color: var(--blue); font-size: 32px; }
.stat-unit { font-size: 12px; color: var(--muted); }

.warn-box {
  background: #fff8ea; border: 1px solid #f0d9a0; border-radius: 10px;
  padding: 12px 16px; margin-bottom: 16px; font-size: 13px; color: #7a5b16; line-height: 1.7;
}
.warn-box b { color: var(--orange); }

.result-table .num { text-align: right; font-variant-numeric: tabular-nums; }
tr.unmatched td { background: #fff8ea !important; }
tr.unmatched td.um-tag { color: var(--orange); font-size: 12px; }
.share-cell { display: flex; align-items: center; gap: 8px; }
.share-bar { height: 7px; border-radius: 4px; background: #e4e9f0; flex: 1; overflow: hidden; }
.share-bar > div { height: 100%; background: linear-gradient(90deg, #4d8bea, #2e5fa3); border-radius: 4px; }
.share-pct { font-size: 12px; color: var(--muted); width: 44px; text-align: right; font-variant-numeric: tabular-nums; }
.formula-note {
  margin-top: 12px; font-size: 12px; color: var(--muted);
  background: #f6f8fb; border-radius: 8px; padding: 8px 12px;
}

.footer { text-align: center; color: #9aa5b3; font-size: 12px; padding: 14px 0 4px; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; top: 20px; left: 50%; transform: translateX(-50%);
  background: #323d4d; color: #fff; padding: 11px 22px; border-radius: 8px;
  font-size: 13.5px; box-shadow: 0 6px 20px rgba(0,0,0,.25); z-index: 99;
  max-width: 80vw;
}
.toast.err { background: #a93226; }
.toast.ok { background: #1e8e4e; }

.loading-mask { opacity: .55; pointer-events: none; }

@media (max-width: 1100px) {
  .layout { grid-template-columns: 1fr; }
  .side { position: static; height: auto; max-height: none; overflow: visible; }
  .lib-tree { max-height: 300px; }
}

/* ---------- 单器件计算器 ---------- */
.calculator { width: min(700px, 94vw); }
.calc-grid { display: grid; grid-template-columns: 1fr 1fr; gap: 10px 14px; }
.calc-grid label { font-size: 12.5px; color: var(--muted); display: flex; flex-direction: column; gap: 5px; }
.calc-grid .span2 { grid-column: 1 / -1; }
.tj-row { display: flex; gap: 8px; align-items: center; flex-direction: row; }
.tj-row input[type="number"] { width: 90px; }
.tj-row input[type="checkbox"] { width: auto; }
.tj-hint { color: #9aa5b3; font-size: 11.5px; }
.calc-meta { font-size: 12px; color: var(--muted); background: #f6f8fb; border-radius: 8px; padding: 8px 12px; margin-top: 12px; line-height: 1.7; }
.calc-result { margin-top: 14px; background: #eef3f9; border: 1px solid #c9d4e0; border-radius: 10px; padding: 12px 16px; }
.calc-result .big { font-size: 26px; font-weight: 700; color: var(--blue); }
.calc-factors { display: flex; flex-wrap: wrap; gap: 8px; margin-top: 8px; font-size: 12px; }
.calc-factors span { background: #fff; border: 1px solid var(--line); border-radius: 8px; padding: 3px 10px; }

/* ---------- 通用弹窗（单器件计算器等） ---------- */
.modal-mask {
  position: fixed; inset: 0; background: rgba(15, 25, 40, .45);
  display: flex; align-items: center; justify-content: center; z-index: 98;
}
.modal {
  background: #fff; border-radius: 12px;
  width: min(700px, 94vw); max-width: 94vw;
  max-height: 88vh; overflow-y: auto;
  display: flex; flex-direction: column;
  padding: 20px 22px; box-shadow: 0 10px 40px rgba(0, 0, 0, .25);
}
.modal h3 {
  color: var(--navy); font-size: 15px; margin-bottom: 10px;
  display: flex; align-items: center; gap: 8px; flex-wrap: wrap;
}
.modal-actions { display: flex; gap: 10px; margin-top: 14px; justify-content: flex-end; }

/* ---------- 列映射弹窗 ---------- */
#mapModal .modal { width: min(780px, 94vw); }
