:root {
  --bg: #f6f1e7;
  --panel: #fffdf8;
  --ink: #3b2f2a;
  --muted: #8a7a6d;
  --line: #e3d8c6;
  --accent: #8d6e63;
  --accent2: #b07d56;
  --accent-ink: #fff;
  --ok: #2e7d32;
  --warn: #c77700;
  --bad: #c0392b;
  --shadow: 0 1px 3px rgba(90, 60, 40, .12);
}

@media (prefers-color-scheme: dark) {
  :root {
    --bg: #1d1814;
    --panel: #2a231d;
    --ink: #f0e6da;
    --muted: #b3a294;
    --line: #3a312a;
    --accent: #c79a76;
    --accent2: #d8a371;
    --accent-ink: #1d1814;
    --ok: #7bc47f;
    --warn: #e0a44a;
    --bad: #ef8a7a;
    --shadow: 0 1px 3px rgba(0, 0, 0, .4);
  }
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: "Microsoft YaHei", "Malgun Gothic", "Yu Gothic", "Meiryo", "Segoe UI", system-ui, sans-serif;
  background: var(--bg);
  color: var(--ink);
  font-size: 15px;
  line-height: 1.5;
}

.topbar {
  display: flex; align-items: center; justify-content: space-between;
  gap: 16px; padding: 12px 18px;
  background: var(--panel); border-bottom: 1px solid var(--line);
  position: sticky; top: 0; z-index: 10; box-shadow: var(--shadow);
  flex-wrap: wrap;
}
.brand { display: flex; align-items: center; gap: 10px; font-size: 18px; font-weight: 700; }
.brand .logo { font-size: 22px; }
.controls { display: flex; gap: 14px; flex-wrap: wrap; }
.ctl { display: flex; align-items: center; gap: 6px; font-size: 13px; color: var(--muted); }
.topbtn {
  display: inline-flex; align-items: center; gap: 6px;
  font-size: 13px; text-decoration: none; color: var(--accent-ink);
  background: var(--accent); border: 1px solid var(--accent);
  border-radius: 6px; padding: 6px 10px; cursor: pointer; font-weight: 600; white-space: nowrap;
}
.topbtn:hover { background: var(--accent2); border-color: var(--accent2); }
select, input, textarea, button { font-family: inherit; font-size: 14px; color: var(--ink); }
select, input, textarea {
  background: var(--panel); border: 1px solid var(--line); border-radius: 6px;
  padding: 6px 8px; outline: none;
}
input:focus, select:focus, textarea:focus { border-color: var(--accent); }
textarea { resize: vertical; }

.layout { display: flex; align-items: flex-start; min-height: calc(100vh - 120px); }
.sidebar {
  width: 190px; flex: 0 0 190px; padding: 14px 10px;
  background: var(--panel); border-right: 1px solid var(--line);
  position: sticky; top: 61px; align-self: flex-start; max-height: calc(100vh - 61px); overflow-y: auto;
}
.nav-item {
  display: block; width: 100%; text-align: left; border: none; background: transparent;
  padding: 9px 12px; border-radius: 8px; cursor: pointer; color: var(--ink); margin-bottom: 2px;
  font-size: 14px;
}
.nav-item:hover { background: var(--bg); }
.nav-item.active { background: var(--accent); color: var(--accent-ink); font-weight: 600; }

.content { flex: 1; padding: 18px 22px; min-width: 0; }
.grp {
  background: var(--panel); border: 1px solid var(--line); border-radius: 12px;
  padding: 14px 16px; margin-bottom: 16px; box-shadow: var(--shadow);
}
.grp h3 { margin: 0 0 10px; font-size: 15px; color: var(--accent); border-left: 4px solid var(--accent2); padding-left: 8px; }
.grp-body { display: flex; flex-direction: column; gap: 10px; }

.fld { display: flex; flex-direction: column; gap: 4px; font-size: 13px; color: var(--muted); }
.fld > span { font-size: 13px; }
.row { display: flex; gap: 12px; flex-wrap: wrap; align-items: flex-end; }
.row .fld { min-width: 130px; flex: 1; }

button.btn {
  background: var(--accent); color: var(--accent-ink); border: none; border-radius: 8px;
  padding: 8px 16px; cursor: pointer; font-weight: 600; font-size: 14px;
}
button.btn:hover { background: var(--accent2); }
button.ghost { background: transparent; border: 1px solid var(--line); color: var(--ink); border-radius: 8px; padding: 7px 12px; cursor: pointer; }
button.ghost:hover { background: var(--bg); }
button.small { padding: 5px 10px; font-size: 13px; }

.result {
  white-space: pre-wrap; background: var(--bg); border: 1px solid var(--line);
  border-radius: 8px; padding: 10px 12px; font-size: 14px; min-height: 1.2em;
}
.note { font-size: 12px; color: var(--muted); }
.hint { font-size: 12px; color: var(--accent2); }

table.tbl { width: 100%; border-collapse: collapse; font-size: 13px; }
table.tbl th, table.tbl td { border: 1px solid var(--line); padding: 5px 7px; text-align: left; }
table.tbl th { background: var(--bg); color: var(--muted); font-weight: 600; }
table.tbl input { width: 100%; padding: 4px 6px; }
table.tbl td.ctr, table.tbl th.ctr { text-align: center; }

canvas.nest { background: #faf6ee; border: 1px solid var(--line); border-radius: 8px; width: 100%; max-width: 600px; }
canvas.nest3d { background: #fffdf8; border: 1px solid var(--line); border-radius: 8px; width: 300px; height: 200px; }
canvas.histchart { background: #fffdf8; border: 1px solid var(--line); border-radius: 8px; width: 100%; max-width: 540px; }
label.chk { display: inline-flex; align-items: center; gap: 5px; font-size: 13px; color: var(--muted); cursor: pointer; }
button.topbtn { background: var(--bg); border: 1px solid var(--line); color: var(--ink); border-radius: 8px; padding: 6px 12px; cursor: pointer; font-size: 13px; text-decoration: none; display: inline-flex; align-items: center; }
button.topbtn:hover { background: var(--accent); color: var(--accent-ink); }

.kerf-table-panel { margin: 8px 0; padding: 12px; border: 1px dashed var(--line); border-radius: 10px; background: var(--bg); }
.kerf-table-panel .row .fld { min-width: 120px; flex: 1 1 140px; }
.kerf-table-panel input:disabled { color: var(--muted); background: var(--line); }

.footer { padding: 12px 18px; font-size: 12px; color: var(--muted); text-align: center; border-top: 1px solid var(--line); }

.tag { display: inline-block; background: var(--bg); border: 1px solid var(--line); border-radius: 6px; padding: 1px 7px; font-size: 12px; color: var(--muted); margin-left: 6px; }
.badge-ok { color: var(--ok); } .badge-warn { color: var(--warn); } .badge-bad { color: var(--bad); }

/* 合并下料单：零件级数量/旋转约束 */
.merge-part-row { display: flex; align-items: center; gap: 10px; flex-wrap: wrap; padding: 1px 0; }
.merge-part-row .mp-ctrl { display: inline-flex; align-items: center; gap: 4px; font-size: 12px; color: var(--muted); }
.merge-part-row .mp-qty { width: 64px; }
.merge-part-row .mp-preview { font-size: 12px; color: var(--accent2); margin-left: 4px; white-space: nowrap; font-weight: 600; }
.qr-cell { text-align: center; vertical-align: middle; }
.qr-sm { width: 56px; height: 56px; image-rendering: pixelated; }

/* BOM 缺件提醒 */
.result.alert { background: #ffecec; border-color: #e6a6a6; color: var(--bad); }
table.tbl tr.row-short > td { background: #ffecec; }

/* 下载完成提示 toast */
#toastBox { position: fixed; left: 50%; bottom: 22px; transform: translateX(-50%); z-index: 9999; display: flex; flex-direction: column; gap: 8px; align-items: center; pointer-events: none; }
.toast { background: rgba(33,33,33,0.92); color: #fff; padding: 10px 16px; border-radius: 8px; font-size: 13px; box-shadow: 0 6px 20px rgba(0,0,0,0.25); opacity: 0; transform: translateY(10px); transition: opacity .25s ease, transform .25s ease; max-width: 90vw; white-space: nowrap; }
.toast.show { opacity: 1; transform: translateY(0); }

@media (max-width: 720px) {
  .layout { flex-direction: column; }
  .sidebar { width: 100%; flex: none; position: static; max-height: none; display: flex; flex-wrap: wrap; gap: 4px; border-right: none; border-bottom: 1px solid var(--line); }
  .nav-item { width: auto; flex: 1 1 auto; }
}

/* 顶部撤销/重做/车间按钮的禁用与激活态 */
button.topbtn:disabled { opacity: .45; cursor: not-allowed; }
button.topbtn.active { background: var(--accent); color: var(--accent-ink); }

/* 车间触屏模式：放大按钮与字体，便于触屏/戴手套操作 */
body.workshop { font-size: 17px; }
body.workshop select, body.workshop input, body.workshop textarea, body.workshop button { font-size: 16px; }
body.workshop button.btn, body.workshop button.ghost, body.workshop button.topbtn, body.workshop .nav-item {
  padding: 14px 20px; font-size: 16px; min-height: 48px; border-radius: 10px;
}
body.workshop .nav-item { padding: 14px 14px; }
body.workshop .fld { font-size: 15px; }
body.workshop table.tbl th, body.workshop table.tbl td { padding: 9px 10px; font-size: 15px; }
body.workshop .grp { padding: 18px 20px; }
body.workshop .topbtn { min-width: 48px; justify-content: center; }

/* 合并下料单：来源勾选列表 */
.src-list { display: flex; flex-wrap: wrap; gap: 6px 18px; max-width: 520px; }
.src-list .chk { margin: 0; white-space: nowrap; }

/* 下料单 Logo / 二维码（打印页内） */
.brandrow { display: flex; align-items: center; gap: 16px; margin: 6px 0 10px; }
.brandrow .logo { max-height: 54px; width: auto; }
.brandrow .qr { width: 84px; height: 84px; border: 1px solid #ccc; }

/* 历史记录：货币 chip */
.cur-chip { display: inline-block; margin-left: 6px; padding: 1px 6px; font-size: 11px; line-height: 16px;
  border-radius: 9px; background: #efe7e0; color: #6d4c41; border: 1px solid #d7c4b4; vertical-align: middle; }

/* 材料库：收藏星标 */
button.star { background: none; border: 1px solid #d7c4b4; border-radius: 6px; cursor: pointer;
  width: 30px; height: 30px; font-size: 16px; line-height: 1; color: #b0a08f; padding: 0; }
button.star.on { color: #e6a700; border-color: #e6a700; background: #fff8e6; }

/* 汇率设置弹窗 */
.modal-overlay { position: fixed; inset: 0; background: rgba(0,0,0,0.42); display: flex;
  align-items: center; justify-content: center; z-index: 999; padding: 16px; }
.modal { background: #fff; color: #2b2b2b; border-radius: 12px; box-shadow: 0 10px 40px rgba(0,0,0,0.28);
  width: min(440px, 94vw); max-height: 88vh; overflow: auto; padding: 18px 20px; }
.modal h3 { margin: 0 0 6px; }
.rate-grid { display: grid; grid-template-columns: 1fr; gap: 8px; margin: 12px 0; }
.rate-row { display: flex; align-items: center; justify-content: space-between; gap: 12px;
  padding: 6px 8px; border: 1px solid #eee; border-radius: 8px; }
.rate-row .rate-name { font-size: 14px; }
.rate-row input { width: 120px; }
.src-list .chk.part { padding-left: 16px; font-size: 13px; color: var(--muted); }

/* 扫码分享弹窗 */
.qr-img { display: block; margin: 10px auto; width: 200px; height: 200px; image-rendering: pixelated; background: #fff; padding: 6px; border: 1px solid #eee; border-radius: 8px; }
.qr-url { width: 100%; font-size: 12px; color: var(--muted); }
.modal .qr-img { background: #fff; }



/* M7: 手动主题切换（覆盖系统偏好） */
body[data-theme="dark"] {
  --bg: #1d1814;
  --panel: #2a231d;
  --ink: #f0e6da;
  --muted: #b3a294;
  --line: #3a312a;
  --accent: #c79a76;
  --accent2: #d8a371;
  --accent-ink: #1d1814;
  --ok: #7bc47f;
  --warn: #e0a44a;
  --bad: #ef8a7a;
  --shadow: 0 1px 3px rgba(0, 0, 0, .4);
}
body[data-theme="light"] {
  --bg: #f6f1e7;
  --panel: #fffdf8;
  --ink: #3b2f2a;
  --muted: #8a7a6d;
  --line: #e3d8c6;
  --accent: #8d6e63;
  --accent2: #b07d56;
  --accent-ink: #fff;
  --ok: #2e7d32;
  --warn: #c77700;
  --bad: #c0392b;
  --shadow: 0 1px 3px rgba(90, 60, 40, .12);
}

/* M7: 项目照片网格 */
.photo-grid { display: flex; flex-wrap: wrap; gap: 10px; margin-top: 8px; }
.photo-card { position: relative; width: 130px; border: 1px solid var(--line); border-radius: 8px; overflow: hidden; background: var(--panel); box-shadow: var(--shadow); }
.photo-card img { display: block; width: 100%; height: 100px; object-fit: cover; cursor: zoom-in; }
.photo-card .photo-del { position: absolute; top: 4px; right: 4px; background: rgba(0,0,0,.55); color: #fff; border: none; border-radius: 6px; font-size: 11px; padding: 2px 7px; cursor: pointer; }
.photo-view-overlay { position: fixed; inset: 0; background: rgba(0,0,0,.8); z-index: 10000; display: flex; align-items: center; justify-content: center; cursor: zoom-out; }
.photo-view-overlay img { max-width: 92vw; max-height: 92vh; border-radius: 8px; }

/* M7: 移动端强化 */
@media (max-width: 720px) {
  .topbar { padding: 8px 10px; gap: 8px; }
  .controls { gap: 8px; }
  .row { flex-direction: column; align-items: stretch; }
  .fld { width: 100%; }
  .fld input, .fld select { width: 100%; min-width: 0; }
  table.tbl { display: block; overflow-x: auto; white-space: nowrap; }
  .grp { padding: 12px 12px; }
  button.btn, button.ghost { min-height: 42px; }
  canvas { max-width: 100%; height: auto; }
}
@media (max-width: 420px) {
  body { font-size: 14px; }
  .brand { font-size: 15px; }
  .topbtn { padding: 5px 8px; font-size: 12px; }
}
