:root {
  --ink: #1a1a1a;
  --muted: #6b7280;
  --line: #1a1a1a;
  --soft-line: #cfd4dc;
  --bg: #f4f5f7;
  --accent: #8a1a1a;
  --accent-ink: #fff;
  --row-hover: #fff7e6;
}

* { box-sizing: border-box; }
html, body { margin: 0; padding: 0; }
body {
  font-family: -apple-system, "Helvetica Neue", Arial, "PingFang SC", "Microsoft YaHei", sans-serif;
  color: var(--ink);
  background: var(--bg);
  font-size: 14px;
}

button {
  font: inherit;
  padding: 6px 12px;
  border: 1px solid #c5cad2;
  background: #fff;
  border-radius: 6px;
  cursor: pointer;
}
button:hover { background: #f0f2f5; }
button.primary { background: var(--accent); color: var(--accent-ink); border-color: var(--accent); }
button.primary:hover { filter: brightness(1.1); }
button.ghost { border: 1px dashed #aaa; background: transparent; }
input, textarea {
  font: inherit;
  color: var(--ink);
  border: 1px solid #c5cad2;
  border-radius: 4px;
  padding: 4px 6px;
  background: #fff;
}
input[type="date"] { min-width: 130px; }

kbd {
  background: #f3f4f6; border: 1px solid #d1d5db; border-bottom-width: 2px;
  border-radius: 3px; padding: 0 5px; font-size: 12px; font-family: ui-monospace, Menlo, monospace;
}

/* ---------- Top bar ---------- */
.topbar {
  position: sticky;
  top: 0;
  z-index: 10;
  background: #fff;
  border-bottom: 1px solid #e5e7eb;
  padding: 8px 16px;
  display: flex;
  align-items: center;
  gap: 16px;
  flex-wrap: wrap;
}
.tabs { display: flex; gap: 4px; }
.tabs .tab {
  border: none;
  background: transparent;
  padding: 8px 14px;
  border-radius: 6px;
  color: var(--muted);
  font-weight: 600;
}
.tabs .tab.active { background: var(--ink); color: #fff; }
.topbar .actions { margin-left: auto; display: flex; gap: 6px; flex-wrap: wrap; }

.tab-panel { display: none; padding: 18px; }
.tab-panel.active { display: block; }

/* ---------- Invoice sheet ---------- */
.sheet {
  max-width: 1100px;
  margin: 0 auto;
  background: #fff;
  color: var(--ink);
  padding: 24px 28px;
  border: 1px solid var(--soft-line);
  box-shadow: 0 2px 8px rgba(0,0,0,.04);
}

.sheet-header {
  display: grid;
  grid-template-columns: 110px 1fr 260px;
  gap: 18px;
  border-bottom: 1.5px solid var(--line);
  padding-bottom: 16px;
}
.logo {
  width: 100px; height: 100px; border-radius: 50%;
  border: 2px solid var(--accent);
  display: flex; align-items: center; justify-content: center;
  color: var(--accent); font-family: "STKaiti","KaiTi","Kaiti SC",serif;
  font-size: 22px; line-height: 1.1; text-align: center; font-weight: 700;
  background: #fffdf5;
}
.c-name { margin: 0 0 6px 0; font-size: 18px; letter-spacing: .5px; }
.company div { margin: 2px 0; }
.company [contenteditable="true"] { outline: none; border-bottom: 1px dashed transparent; }
.company [contenteditable="true"]:hover,
.company [contenteditable="true"]:focus { border-bottom-color: var(--soft-line); }

.meta { display: flex; flex-direction: column; gap: 6px; }
.meta .row { display: grid; grid-template-columns: 100px 1fr; align-items: center; gap: 6px; }
.meta label { font-weight: 600; color: var(--muted); font-size: 12px; letter-spacing: .5px; }

.to-block {
  display: grid; grid-template-columns: repeat(4, 1fr);
  gap: 10px; margin: 14px 0;
}
.to-cell { border: 1px solid var(--line); padding: 6px 8px; }
.to-cell label { display:block; font-size: 11px; color: var(--muted); font-weight: 600; letter-spacing:.4px; }
.to-cell input { border: none; padding: 2px 0; width: 100%; background: transparent; }
.to-cell input:focus { outline: none; background: #fff7e6; }

/* Items table */
.items {
  width: 100%; border-collapse: collapse; margin-top: 6px;
}
.items th, .items td {
  border: 1px solid var(--line);
  padding: 6px 8px;
  text-align: left;
  vertical-align: middle;
}
.items th { background: #fafafa; font-size: 12px; letter-spacing: .3px; }
.items td input {
  width: 100%;
  border: none;
  padding: 2px 0;
  background: transparent;
}
.items td input:focus { outline: none; background: var(--row-hover); }
.items td.num input { text-align: right; }
.items .sku-input { font-family: ui-monospace, Menlo, monospace; text-transform: uppercase; }
.items .amount { text-align: right; font-variant-numeric: tabular-nums; }
.items tr.unknown .sku-input { color: #b91c1c; }

.row-del { color: #b91c1c; border-color: #fca5a5; background: #fff5f5; padding: 2px 8px; }
.row-del:hover { background: #fee; }

.below-table {
  margin-top: 10px;
  display: flex; align-items: center; gap: 12px; flex-wrap: wrap;
}
.hint { color: var(--muted); font-size: 12px; }

/* Summary / totals */
.summary {
  display: grid;
  grid-template-columns: 2fr 1fr;
  gap: 12px;
  margin-top: 14px;
  border-top: 1px solid var(--line);
  padding-top: 10px;
}
.terms-title { font-weight: 700; text-decoration: underline; margin-bottom: 4px; }
.terms p { margin: 4px 0; font-size: 12.5px; }
.terms .bold { font-weight: 700; }

.totals { border: 1.5px solid var(--line); padding: 8px 12px; }
.totals .t-row {
  display: grid; grid-template-columns: 1fr auto;
  gap: 8px; padding: 4px 0;
  border-bottom: 1px dashed var(--soft-line);
  align-items: center;
}
.totals .t-row:last-child { border-bottom: none; }
.totals .t-row span:last-child { font-variant-numeric: tabular-nums; text-align: right; min-width: 90px; }
.totals .t-row input { width: 100px; text-align: right; }
.totals .total { font-weight: 800; font-size: 16px; }
.totals .total span:first-child { color: var(--accent); }

/* Remittance */
.remittance {
  display: grid; grid-template-columns: 2fr 1fr;
  gap: 12px; margin-top: 14px;
}
.r-block { border: 1px solid var(--line); padding: 8px 12px; min-height: 130px; }
.r-title { font-weight: 700; margin-bottom: 4px; }
.sig-box { margin-top: 40px; border-top: 1px solid #666; padding-top: 4px; }

.sheet-footer {
  margin-top: 14px;
  text-align: center;
  padding-top: 8px;
  border-top: 1.5px solid var(--line);
  font-size: 13px;
}

/* ---------- Products panel ---------- */
.panel {
  max-width: 1100px; margin: 0 auto;
  background: #fff; border: 1px solid var(--soft-line);
  padding: 16px 20px;
  box-shadow: 0 2px 8px rgba(0,0,0,.04);
}
.panel-head { display: flex; align-items: center; gap: 10px; margin-bottom: 10px; flex-wrap: wrap; }
.panel-head h2 { margin: 0; }
.panel-head .actions { margin-left: auto; display: flex; gap: 6px; flex-wrap: wrap; }
#product-search { min-width: 260px; }

.products { width: 100%; border-collapse: collapse; }
.products th, .products td { border: 1px solid var(--soft-line); padding: 6px 8px; text-align: left; }
.products th { background: #fafafa; }
.products td input { width: 100%; border: none; padding: 2px 0; background: transparent; }
.products td input:focus { outline: none; background: var(--row-hover); }
.products td.num input { text-align: right; }

/* ---------- Scan modal ---------- */
.modal {
  position: fixed; inset: 0; background: rgba(0,0,0,.5);
  display: flex; align-items: center; justify-content: center; z-index: 50;
}
.modal[hidden] { display: none; }
.modal-inner {
  background: #fff; border-radius: 10px; padding: 16px;
  width: min(520px, 94vw);
}
.modal-inner h3 { margin: 0 0 10px 0; }
.modal-actions { display: flex; justify-content: flex-end; margin-top: 10px; gap: 8px; }

/* ---------- Toast ---------- */
.toast {
  position: fixed; bottom: 24px; left: 50%; transform: translateX(-50%);
  background: #1a1a1a; color: #fff; padding: 10px 16px; border-radius: 6px;
  z-index: 100; max-width: 90vw; box-shadow: 0 4px 12px rgba(0,0,0,.2);
  font-size: 13px;
}
.toast.error { background: #b91c1c; }
.toast[hidden] { display: none; }

/* ---------- Print ---------- */
@media print {
  @page { size: A4; margin: 12mm; }
  body { background: #fff; font-size: 12px; }
  .no-print, .topbar { display: none !important; }
  .tab-panel { padding: 0; }
  .tab-panel#products-tab { display: none !important; }
  .sheet {
    max-width: none; border: none; box-shadow: none;
    padding: 0; margin: 0;
  }
  input, .company [contenteditable="true"] {
    border: none !important; background: transparent !important;
    color: #000 !important;
    -webkit-print-color-adjust: exact;
  }
  .items { page-break-inside: auto; }
  .items tr { page-break-inside: avoid; }
  .logo { color: #8a1a1a; border-color: #8a1a1a; }
}
