:root {
  --bg: #f4f6f9;
  --surface: #ffffff;
  --ink: #152033;
  --muted: #5b677a;
  --line: #e3e8ef;
  --brand: #0f6e6a;
  --brand-dark: #0a5451;
  --brand-soft: #e7f5f4;
  --warn: #b45309;
  --danger: #b91c1c;
  --shadow: 0 10px 30px rgba(21, 32, 51, 0.08);
  --radius: 14px;
  --font: "PingFang SC", "Noto Sans SC", "Microsoft YaHei", system-ui, sans-serif;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: var(--font);
  color: var(--ink);
  background:
    radial-gradient(1200px 500px at 10% -10%, #d9f3f1 0%, transparent 55%),
    radial-gradient(900px 400px at 100% 0%, #e8eefc 0%, transparent 50%),
    var(--bg);
  min-height: 100vh;
}

button, input, select, textarea { font: inherit; color: inherit; }
button { cursor: pointer; }
input, select, textarea {
  width: 100%;
  border: 1px solid var(--line);
  border-radius: 10px;
  padding: 10px 12px;
  background: #fff;
  outline: none;
  transition: border-color .15s, box-shadow .15s;
}
input:focus, select:focus, textarea:focus {
  border-color: #7ec7c3;
  box-shadow: 0 0 0 3px rgba(15, 110, 106, 0.12);
}
textarea { resize: vertical; }

.app { max-width: 1180px; margin: 0 auto; padding: 18px 16px 48px; }

.topbar {
  display: flex;
  flex-wrap: wrap;
  gap: 14px;
  align-items: center;
  justify-content: space-between;
  background: rgba(255,255,255,0.9);
  backdrop-filter: blur(10px);
  border: 1px solid var(--line);
  border-radius: 18px;
  padding: 14px 16px;
  box-shadow: var(--shadow);
  position: sticky;
  top: 10px;
  z-index: 20;
}

.brand { display: flex; gap: 12px; align-items: center; }
.brand-mark {
  width: 44px; height: 44px; border-radius: 12px;
  display: grid; place-items: center;
  background: linear-gradient(145deg, var(--brand), #148f89);
  color: #fff; font-weight: 700; font-size: 18px;
  box-shadow: 0 8px 18px rgba(15,110,106,.28);
}
.brand h1 { margin: 0; font-size: 18px; letter-spacing: .02em; }
.brand p { margin: 2px 0 0; color: var(--muted); font-size: 12px; }

.top-actions { display: flex; flex-wrap: wrap; gap: 8px; }
.btn, .btn-sm, .file-btn {
  border: 1px solid var(--line);
  background: #fff;
  border-radius: 999px;
  padding: 9px 14px;
  font-size: 13px;
  font-weight: 600;
}
.btn-sm { padding: 7px 10px; border-radius: 8px; font-size: 12px; white-space: nowrap; }
.btn-primary {
  background: var(--brand);
  border-color: var(--brand);
  color: #fff;
}
.btn-primary:hover { background: var(--brand-dark); }
.btn-ghost:hover, .btn-sm:hover { background: var(--brand-soft); border-color: #b7dedc; }
.file-btn { display: inline-flex; align-items: center; }

.layout { display: grid; gap: 14px; margin-top: 16px; }
.panel {
  background: var(--surface);
  border: 1px solid var(--line);
  border-radius: var(--radius);
  padding: 16px;
  box-shadow: var(--shadow);
}
.panel-hd {
  display: flex; flex-wrap: wrap; gap: 10px;
  align-items: center; justify-content: space-between;
  margin-bottom: 14px;
}
.panel-hd h2 { margin: 0; font-size: 16px; }
.chip {
  font-size: 12px; color: var(--brand);
  background: var(--brand-soft);
  border-radius: 999px; padding: 4px 10px;
}

.grid-3, .grid-2 { display: grid; gap: 12px; }
.grid-3 { grid-template-columns: repeat(3, minmax(0, 1fr)); }
.grid-2 { grid-template-columns: repeat(2, minmax(0, 1fr)); }
.field { display: grid; gap: 6px; font-size: 13px; color: var(--muted); }
.field > span { font-weight: 600; }
.field.full { grid-column: 1 / -1; }
.inline { display: flex; gap: 8px; align-items: center; }
.inline > input, .inline > select { flex: 1; }

.role-toggle { display: flex; flex-wrap: wrap; gap: 8px; align-items: center; font-size: 13px; color: var(--muted); }
.seg { display: inline-flex; background: #eef2f7; border-radius: 999px; padding: 3px; }
.seg-btn {
  border: 0; background: transparent; border-radius: 999px;
  padding: 7px 12px; font-size: 12px; font-weight: 600; color: var(--muted);
}
.seg-btn.active { background: #fff; color: var(--brand); box-shadow: 0 2px 8px rgba(21,32,51,.08); }

.parties { display: grid; grid-template-columns: 1fr 1fr; gap: 12px; }
.party-card {
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px;
  background: linear-gradient(180deg, #fbfcfe, #fff);
}
.party-hd {
  display: flex; flex-wrap: wrap; gap: 8px;
  align-items: center; justify-content: space-between;
  margin-bottom: 10px;
}

.table-wrap { overflow-x: auto; border: 1px solid var(--line); border-radius: 12px; }
.items-table { width: 100%; border-collapse: collapse; min-width: 760px; }
.items-table th, .items-table td {
  border-bottom: 1px solid var(--line);
  padding: 8px;
  text-align: left;
  vertical-align: middle;
  font-size: 13px;
}
.items-table th {
  background: #f7fafc;
  color: var(--muted);
  font-weight: 700;
  white-space: nowrap;
}
.items-table tbody tr:last-child td { border-bottom: 0; }
.items-table input, .items-table select {
  padding: 8px 10px;
  border-radius: 8px;
}
.col-idx { width: 42px; text-align: center !important; color: var(--muted); }
.col-num { width: 110px; }
.col-unit { width: 88px; }
.col-act { width: 54px; }
.amount-cell {
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  text-align: right;
  padding-right: 12px !important;
  white-space: nowrap;
}
.btn-icon {
  border: 0; background: transparent; color: var(--danger);
  font-size: 18px; line-height: 1; padding: 4px 8px;
}
.btn-icon:hover { background: #fee2e2; border-radius: 8px; }

.totals {
  margin-top: 14px;
  display: flex; flex-wrap: wrap; gap: 12px;
  align-items: flex-end; justify-content: space-between;
}
.totals-note { color: var(--muted); font-size: 12px; }
.totals-box {
  margin: 0; min-width: 240px;
  background: var(--brand-soft);
  border-radius: 12px;
  padding: 12px 14px;
}
.totals-box > div {
  display: flex; justify-content: space-between; gap: 24px;
  padding: 4px 0; font-size: 14px;
}
.totals-box dt { margin: 0; color: var(--muted); }
.totals-box dd { margin: 0; font-variant-numeric: tabular-nums; font-weight: 700; }
.totals-box .grand {
  border-top: 1px dashed #b7dedc;
  margin-top: 6px; padding-top: 8px;
  font-size: 16px;
}
.totals-box .grand dd { color: var(--brand-dark); }

.preview-shell { margin-top: 16px; }
.preview-toolbar {
  display: flex; justify-content: space-between; gap: 10px; flex-wrap: wrap;
  margin-bottom: 12px;
}
.po-sheet {
  background: #fff;
  width: min(210mm, 100%);
  margin: 0 auto;
  padding: 16mm 14mm;
  border: 1px solid var(--line);
  border-radius: 8px;
  box-shadow: var(--shadow);
  color: #111;
}
.po-sheet h1 {
  margin: 0 0 4px;
  text-align: center;
  font-size: 22px;
  letter-spacing: .2em;
}
.po-sheet .doc-sub {
  text-align: center;
  color: #666;
  font-size: 12px;
  margin-bottom: 18px;
}
.po-meta {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 8px 18px;
  font-size: 13px;
  margin-bottom: 14px;
}
.po-meta div { display: flex; gap: 6px; }
.po-meta .k { color: #666; min-width: 4.5em; }
.po-parties {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 12px;
  margin-bottom: 14px;
}
.po-party {
  border: 1px solid #d7dde7;
  border-radius: 6px;
  padding: 10px 12px;
  font-size: 12.5px;
  line-height: 1.55;
}
.po-party h3 {
  margin: 0 0 6px;
  font-size: 13px;
  color: var(--brand-dark);
  border-bottom: 1px solid #e8edf4;
  padding-bottom: 4px;
}
.po-table {
  width: 100%;
  border-collapse: collapse;
  font-size: 12.5px;
  margin-bottom: 12px;
}
.po-table th, .po-table td {
  border: 1px solid #cfd6e2;
  padding: 7px 8px;
  vertical-align: top;
}
.po-table th { background: #f3f6fa; font-weight: 700; }
.po-table .num { text-align: right; font-variant-numeric: tabular-nums; }
.po-table .center { text-align: center; }
.po-summary {
  display: flex;
  justify-content: flex-end;
  margin-bottom: 14px;
}
.po-summary table { border-collapse: collapse; font-size: 13px; min-width: 220px; }
.po-summary td { padding: 4px 0 4px 18px; }
.po-summary td:last-child { text-align: right; font-variant-numeric: tabular-nums; font-weight: 700; }
.po-summary .grand td { font-size: 15px; padding-top: 8px; border-top: 1px solid #cfd6e2; }
.po-remarks {
  border: 1px dashed #cfd6e2;
  border-radius: 6px;
  padding: 10px 12px;
  font-size: 12.5px;
  margin-bottom: 28px;
  white-space: pre-wrap;
}
.po-sign {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 24px;
  font-size: 13px;
  margin-top: 8px;
}
.po-sign .line { margin-top: 36px; border-top: 1px solid #999; padding-top: 6px; color: #555; }

.modal-backdrop {
  position: fixed; inset: 0;
  background: rgba(15, 23, 42, 0.45);
  display: grid; place-items: center;
  padding: 16px; z-index: 50;
}
.modal {
  width: min(640px, 100%);
  background: #fff;
  border-radius: 16px;
  box-shadow: 0 20px 50px rgba(0,0,0,.2);
  overflow: hidden;
}
.modal-hd {
  display: flex; align-items: center; justify-content: space-between;
  padding: 14px 16px; border-bottom: 1px solid var(--line);
}
.modal-bd { padding: 14px 16px 18px; max-height: 70vh; overflow: auto; }
.hint { color: var(--muted); font-size: 12px; margin: 0 0 12px; }
.drafts-list { display: grid; gap: 8px; }
.draft-item {
  display: flex; gap: 10px; align-items: center; justify-content: space-between;
  border: 1px solid var(--line); border-radius: 12px; padding: 10px 12px;
}
.draft-item .meta { font-size: 12px; color: var(--muted); margin-top: 2px; }
.draft-item .actions { display: flex; gap: 6px; flex-shrink: 0; }
.empty-drafts {
  text-align: center; color: var(--muted);
  padding: 28px 12px; border: 1px dashed var(--line); border-radius: 12px;
}

.toast {
  position: fixed; left: 50%; bottom: 24px; transform: translateX(-50%);
  background: #152033; color: #fff; padding: 10px 16px;
  border-radius: 999px; font-size: 13px; z-index: 80;
  box-shadow: 0 10px 30px rgba(0,0,0,.25);
}
.hidden { display: none !important; }

@media (max-width: 900px) {
  .grid-3, .grid-2, .parties, .po-parties, .po-meta, .po-sign { grid-template-columns: 1fr; }
  .topbar { position: static; }
  .brand h1 { font-size: 16px; }
}

@media print {
  body { background: #fff; }
  .no-print, .topbar, .preview-toolbar, .toast, .modal-backdrop { display: none !important; }
  .app { max-width: none; padding: 0; }
  .po-sheet {
    width: auto; margin: 0; padding: 0;
    border: 0; border-radius: 0; box-shadow: none;
  }
  .preview-shell { margin: 0; }
}


/* product image cells */
.col-img { width: 88px; }
.items-table { min-width: 880px; }
.img-cell { vertical-align: middle !important; }
.img-box {
  display: flex;
  flex-direction: column;
  align-items: center;
  gap: 6px;
}
.item-thumb, .item-thumb-empty {
  width: 56px;
  height: 56px;
  border-radius: 8px;
  border: 1px solid var(--line);
  object-fit: cover;
  background: #f7fafc;
  display: block;
}
.item-thumb-empty {
  display: grid;
  place-items: center;
  color: #9aa7b8;
  font-size: 11px;
  user-select: none;
}
.img-actions {
  display: flex;
  gap: 4px;
  flex-wrap: wrap;
  justify-content: center;
}
.img-actions .btn-sm { padding: 4px 7px; font-size: 11px; }
.items-hint { margin: 10px 0 0; }
#storageHint { color: var(--warn); font-weight: 600; }
#storageHint.ok { color: var(--muted); font-weight: 500; }

.po-table .po-thumb {
  width: 42px;
  height: 42px;
  object-fit: cover;
  border-radius: 4px;
  border: 1px solid #d7dde7;
  display: block;
  margin: 0 auto;
}
.po-table .po-thumb-empty {
  width: 42px;
  height: 42px;
  margin: 0 auto;
  border: 1px dashed #d7dde7;
  border-radius: 4px;
  background: #fafbfd;
}
.po-name-cell {
  display: flex;
  gap: 8px;
  align-items: flex-start;
}
.po-name-cell .po-thumb,
.po-name-cell .po-thumb-empty { flex-shrink: 0; }
.po-name-text { flex: 1; min-width: 0; word-break: break-word; }

@media print {
  .item-thumb, .po-thumb { -webkit-print-color-adjust: exact; print-color-adjust: exact; }
}


.modal-wide { width: min(760px, 100%); }
.drafts-toolbar {
  display: flex; flex-wrap: wrap; gap: 10px; align-items: center;
  margin-bottom: 12px;
}
.chk { display: inline-flex; gap: 6px; align-items: center; font-size: 13px; color: var(--muted); }
.chk input { width: auto; }
.draft-item {
  display: grid;
  grid-template-columns: auto 1fr auto;
  gap: 12px;
  align-items: center;
}
.draft-item .draft-main { min-width: 0; cursor: pointer; }
.draft-item .draft-main:hover strong { color: var(--brand); }
.draft-item .draft-title {
  display: flex; flex-wrap: wrap; gap: 8px; align-items: baseline;
}
.draft-item .draft-total {
  font-variant-numeric: tabular-nums;
  font-weight: 700;
  color: var(--brand-dark);
}
.draft-item .thumb-mini {
  width: 36px; height: 36px; border-radius: 6px; object-fit: cover;
  border: 1px solid var(--line); background: #f7fafc;
}
.row-active { outline: 2px solid rgba(15,110,106,.35); outline-offset: -2px; background: #f3fbfa; }


.product-layout {
  display: grid;
  grid-template-columns: 1.2fr 1fr;
  gap: 14px;
  align-items: start;
}
.products-list { display: grid; gap: 8px; max-height: 58vh; overflow: auto; }
.product-card {
  display: grid;
  grid-template-columns: 52px 1fr auto;
  gap: 10px;
  align-items: center;
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 10px;
  background: #fff;
}
.product-card .meta { font-size: 12px; color: var(--muted); margin-top: 2px; }
.product-card .actions { display: flex; flex-direction: column; gap: 6px; }
.panel-like {
  border: 1px solid var(--line);
  border-radius: 12px;
  padding: 12px;
  background: linear-gradient(180deg, #fbfcfe, #fff);
}
.product-form h3 { margin: 0 0 10px; font-size: 15px; }
.prod-img-row { display: flex; gap: 12px; align-items: center; flex-wrap: wrap; }
.pick-card { cursor: pointer; }
.pick-card:hover { border-color: #7ec7c3; box-shadow: 0 0 0 3px rgba(15,110,106,.1); }
.row-pick-btn { white-space: nowrap; }

@media (max-width: 900px) {
  .product-layout { grid-template-columns: 1fr; }
}
